1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_nop
)
88 else if (policy
== lss_blank
|| !saved
)
89 local_specializations
= new hash_map
<tree
, tree
>;
91 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
94 local_specialization_stack::~local_specialization_stack ()
96 if (local_specializations
!= saved
)
98 delete local_specializations
;
99 local_specializations
= saved
;
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth
;
106 struct GTY((for_user
)) spec_entry
113 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
115 static hashval_t
hash (spec_entry
*);
116 static bool equal (spec_entry
*, spec_entry
*);
119 /* The general template is not in these tables. */
120 typedef hash_table
<spec_hasher
> spec_hash_table
;
121 static GTY (()) spec_hash_table
*decl_specializations
;
122 static GTY (()) spec_hash_table
*type_specializations
;
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
139 enum template_base_result
{
141 tbr_ambiguous_baseclass
,
145 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
146 unification_kind_t
, int,
148 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
149 unification_kind_t
, int, bool, bool);
150 static int unify (tree
, tree
, tree
, tree
, int, bool);
151 static void add_pending_template (tree
);
152 static tree
reopen_tinst_level (struct tinst_level
*);
153 static tree
tsubst_initializer_list (tree
, tree
);
154 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
155 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
157 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
159 static void tsubst_enum (tree
, tree
, tree
);
160 static tree
add_to_template_args (tree
, tree
);
161 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
162 static int check_non_deducible_conversion (tree
, tree
, int, int,
163 struct conversion
**, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
166 static int type_unification_real (tree
, tree
, tree
, const tree
*,
167 unsigned int, int, unification_kind_t
,
168 vec
<deferred_access_check
, va_gc
> **,
170 static void note_template_header (int);
171 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
172 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
173 static tree
convert_template_argument (tree
, tree
, tree
,
174 tsubst_flags_t
, int, tree
);
175 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
176 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
177 static tree
expand_template_argument_pack (tree
);
178 static tree
build_template_parm_index (int, int, int, tree
, tree
);
179 static bool inline_needs_template_parms (tree
, bool);
180 static void push_inline_template_parms_recursive (tree
, int);
181 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
182 static int mark_template_parm (tree
, void *);
183 static int template_parm_this_level_p (tree
, void *);
184 static tree
tsubst_friend_function (tree
, tree
);
185 static tree
tsubst_friend_class (tree
, tree
);
186 static int can_complete_type_without_circularity (tree
);
187 static tree
get_bindings (tree
, tree
, tree
, bool);
188 static int template_decl_level (tree
);
189 static int check_cv_quals_for_unify (int, tree
, tree
);
190 static int unify_pack_expansion (tree
, tree
, tree
,
191 tree
, unification_kind_t
, bool, bool);
192 static tree
copy_template_args (tree
);
193 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
194 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
195 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
196 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
198 static bool check_specialization_scope (void);
199 static tree
process_partial_specialization (tree
);
200 static void set_current_access_from_decl (tree
);
201 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
203 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
204 static bool class_nttp_const_wrapper_p (tree t
);
205 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
207 static bool template_template_parm_bindings_ok_p (tree
, tree
);
208 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
209 static tree
for_each_template_parm_r (tree
*, int *, void *);
210 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
211 static void copy_default_args_to_explicit_spec (tree
);
212 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
213 static bool dependent_template_arg_p (tree
);
214 static bool any_template_arguments_need_structural_equality_p (tree
);
215 static bool dependent_type_p_r (tree
);
216 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
218 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
219 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
221 static tree
listify (tree
);
222 static tree
listify_autos (tree
, tree
);
223 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
224 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
225 static bool complex_alias_template_p (const_tree tmpl
);
226 static tree
get_underlying_template (tree
);
227 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
228 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
229 static tree
make_argument_pack (tree
);
230 static void register_parameter_specializations (tree
, tree
);
231 static tree
enclosing_instantiation_of (tree tctx
);
233 /* Make the current scope suitable for access checking when we are
234 processing T. T can be FUNCTION_DECL for instantiated function
235 template, VAR_DECL for static member variable, or TYPE_DECL for
236 alias template (needed by instantiate_decl). */
239 push_access_scope (tree t
)
241 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
242 || TREE_CODE (t
) == TYPE_DECL
);
244 if (DECL_FRIEND_CONTEXT (t
))
245 push_nested_class (DECL_FRIEND_CONTEXT (t
));
246 else if (DECL_CLASS_SCOPE_P (t
))
247 push_nested_class (DECL_CONTEXT (t
));
249 push_to_top_level ();
251 if (TREE_CODE (t
) == FUNCTION_DECL
)
253 vec_safe_push (saved_access_scope
, current_function_decl
);
254 current_function_decl
= t
;
258 /* Restore the scope set up by push_access_scope. T is the node we
262 pop_access_scope (tree t
)
264 if (TREE_CODE (t
) == FUNCTION_DECL
)
265 current_function_decl
= saved_access_scope
->pop();
267 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
270 pop_from_top_level ();
273 /* Do any processing required when DECL (a member template
274 declaration) is finished. Returns the TEMPLATE_DECL corresponding
275 to DECL, unless it is a specialization, in which case the DECL
276 itself is returned. */
279 finish_member_template_decl (tree decl
)
281 if (decl
== error_mark_node
)
282 return error_mark_node
;
284 gcc_assert (DECL_P (decl
));
286 if (TREE_CODE (decl
) == TYPE_DECL
)
290 type
= TREE_TYPE (decl
);
291 if (type
== error_mark_node
)
292 return error_mark_node
;
293 if (MAYBE_CLASS_TYPE_P (type
)
294 && CLASSTYPE_TEMPLATE_INFO (type
)
295 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
297 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
298 check_member_template (tmpl
);
303 else if (TREE_CODE (decl
) == FIELD_DECL
)
304 error_at (DECL_SOURCE_LOCATION (decl
),
305 "data member %qD cannot be a member template", decl
);
306 else if (DECL_TEMPLATE_INFO (decl
))
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
310 check_member_template (DECL_TI_TEMPLATE (decl
));
311 return DECL_TI_TEMPLATE (decl
);
317 error_at (DECL_SOURCE_LOCATION (decl
),
318 "invalid member template declaration %qD", decl
);
320 return error_mark_node
;
323 /* Create a template info node. */
326 build_template_info (tree template_decl
, tree template_args
)
328 tree result
= make_node (TEMPLATE_INFO
);
329 TI_TEMPLATE (result
) = template_decl
;
330 TI_ARGS (result
) = template_args
;
334 /* Return the template info node corresponding to T, whatever T is. */
337 get_template_info (const_tree t
)
339 tree tinfo
= NULL_TREE
;
341 if (!t
|| t
== error_mark_node
)
344 if (TREE_CODE (t
) == NAMESPACE_DECL
345 || TREE_CODE (t
) == PARM_DECL
)
348 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
349 tinfo
= DECL_TEMPLATE_INFO (t
);
351 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
354 if (OVERLOAD_TYPE_P (t
))
355 tinfo
= TYPE_TEMPLATE_INFO (t
);
356 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
357 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
362 /* Returns the template nesting level of the indicated class TYPE.
372 A<T>::B<U> has depth two, while A<T> has depth one.
373 Both A<T>::B<int> and A<int>::B<U> have depth one, if
374 they are instantiations, not specializations.
376 This function is guaranteed to return 0 if passed NULL_TREE so
377 that, for example, `template_class_depth (current_class_type)' is
381 template_class_depth (tree type
)
385 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
387 tree tinfo
= get_template_info (type
);
389 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
390 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
395 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
398 type
= CP_DECL_CONTEXT (type
);
400 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
401 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
403 type
= CP_TYPE_CONTEXT (type
);
409 /* Return TRUE if NODE instantiates a template that has arguments of
410 its own, be it directly a primary template or indirectly through a
411 partial specializations. */
413 instantiates_primary_template_p (tree node
)
415 tree tinfo
= get_template_info (node
);
419 tree tmpl
= TI_TEMPLATE (tinfo
);
420 if (PRIMARY_TEMPLATE_P (tmpl
))
423 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
426 /* So now we know we have a specialization, but it could be a full
427 or a partial specialization. To tell which, compare the depth of
428 its template arguments with those of its context. */
430 tree ctxt
= DECL_CONTEXT (tmpl
);
431 tree ctinfo
= get_template_info (ctxt
);
435 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
436 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
439 /* Subroutine of maybe_begin_member_template_processing.
440 Returns true if processing DECL needs us to push template parms. */
443 inline_needs_template_parms (tree decl
, bool nsdmi
)
445 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
448 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
449 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
452 /* Subroutine of maybe_begin_member_template_processing.
453 Push the template parms in PARMS, starting from LEVELS steps into the
454 chain, and ending at the beginning, since template parms are listed
458 push_inline_template_parms_recursive (tree parmlist
, int levels
)
460 tree parms
= TREE_VALUE (parmlist
);
464 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
466 ++processing_template_decl
;
467 current_template_parms
468 = tree_cons (size_int (processing_template_decl
),
469 parms
, current_template_parms
);
470 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
472 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
474 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
476 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
478 if (error_operand_p (parm
))
481 gcc_assert (DECL_P (parm
));
483 switch (TREE_CODE (parm
))
491 /* Push the CONST_DECL. */
492 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
501 /* Restore the template parameter context for a member template, a
502 friend template defined in a class definition, or a non-template
503 member of template class. */
506 maybe_begin_member_template_processing (tree decl
)
510 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
514 tree ctx
= DECL_CONTEXT (decl
);
515 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
516 /* Disregard full specializations (c++/60999). */
517 && uses_template_parms (ctx
)
518 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
521 if (inline_needs_template_parms (decl
, nsdmi
))
523 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
524 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
526 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
529 parms
= TREE_CHAIN (parms
);
532 push_inline_template_parms_recursive (parms
, levels
);
535 /* Remember how many levels of template parameters we pushed so that
536 we can pop them later. */
537 inline_parm_levels
.safe_push (levels
);
540 /* Undo the effects of maybe_begin_member_template_processing. */
543 maybe_end_member_template_processing (void)
548 if (inline_parm_levels
.length () == 0)
551 last
= inline_parm_levels
.pop ();
552 for (i
= 0; i
< last
; ++i
)
554 --processing_template_decl
;
555 current_template_parms
= TREE_CHAIN (current_template_parms
);
560 /* Return a new template argument vector which contains all of ARGS,
561 but has as its innermost set of arguments the EXTRA_ARGS. */
564 add_to_template_args (tree args
, tree extra_args
)
571 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
574 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
575 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
577 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
578 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
580 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
581 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
586 /* Like add_to_template_args, but only the outermost ARGS are added to
587 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
588 (EXTRA_ARGS) levels are added. This function is used to combine
589 the template arguments from a partial instantiation with the
590 template arguments used to attain the full instantiation from the
591 partial instantiation. */
594 add_outermost_template_args (tree args
, tree extra_args
)
598 /* If there are more levels of EXTRA_ARGS than there are ARGS,
599 something very fishy is going on. */
600 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
602 /* If *all* the new arguments will be the EXTRA_ARGS, just return
604 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
607 /* For the moment, we make ARGS look like it contains fewer levels. */
608 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
610 new_args
= add_to_template_args (args
, extra_args
);
612 /* Now, we restore ARGS to its full dimensions. */
613 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
618 /* Return the N levels of innermost template arguments from the ARGS. */
621 get_innermost_template_args (tree args
, int n
)
629 /* If N is 1, just return the innermost set of template arguments. */
631 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
633 /* If we're not removing anything, just return the arguments we were
635 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
636 gcc_assert (extra_levels
>= 0);
637 if (extra_levels
== 0)
640 /* Make a new set of arguments, not containing the outer arguments. */
641 new_args
= make_tree_vec (n
);
642 for (i
= 1; i
<= n
; ++i
)
643 SET_TMPL_ARGS_LEVEL (new_args
, i
,
644 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
649 /* The inverse of get_innermost_template_args: Return all but the innermost
650 EXTRA_LEVELS levels of template arguments from the ARGS. */
653 strip_innermost_template_args (tree args
, int extra_levels
)
656 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
661 /* If N is 1, just return the outermost set of template arguments. */
663 return TMPL_ARGS_LEVEL (args
, 1);
665 /* If we're not removing anything, just return the arguments we were
667 gcc_assert (extra_levels
>= 0);
668 if (extra_levels
== 0)
671 /* Make a new set of arguments, not containing the inner arguments. */
672 new_args
= make_tree_vec (n
);
673 for (i
= 1; i
<= n
; ++i
)
674 SET_TMPL_ARGS_LEVEL (new_args
, i
,
675 TMPL_ARGS_LEVEL (args
, i
));
680 /* We've got a template header coming up; push to a new level for storing
684 begin_template_parm_list (void)
686 /* We use a non-tag-transparent scope here, which causes pushtag to
687 put tags in this scope, rather than in the enclosing class or
688 namespace scope. This is the right thing, since we want
689 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
690 global template class, push_template_decl handles putting the
691 TEMPLATE_DECL into top-level scope. For a nested template class,
694 template <class T> struct S1 {
695 template <class T> struct S2 {};
698 pushtag contains special code to insert the TEMPLATE_DECL for S2
699 at the right scope. */
700 begin_scope (sk_template_parms
, NULL
);
701 ++processing_template_decl
;
702 ++processing_template_parmlist
;
703 note_template_header (0);
705 /* Add a dummy parameter level while we process the parameter list. */
706 current_template_parms
707 = tree_cons (size_int (processing_template_decl
),
709 current_template_parms
);
712 /* This routine is called when a specialization is declared. If it is
713 invalid to declare a specialization here, an error is reported and
714 false is returned, otherwise this routine will return true. */
717 check_specialization_scope (void)
719 tree scope
= current_scope ();
723 An explicit specialization shall be declared in the namespace of
724 which the template is a member, or, for member templates, in the
725 namespace of which the enclosing class or enclosing class
726 template is a member. An explicit specialization of a member
727 function, member class or static data member of a class template
728 shall be declared in the namespace of which the class template
730 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
732 error ("explicit specialization in non-namespace scope %qD", scope
);
738 In an explicit specialization declaration for a member of a class
739 template or a member template that appears in namespace scope,
740 the member template and some of its enclosing class templates may
741 remain unspecialized, except that the declaration shall not
742 explicitly specialize a class member template if its enclosing
743 class templates are not explicitly specialized as well. */
744 if (current_template_parms
)
746 error ("enclosing class templates are not explicitly specialized");
753 /* We've just seen template <>. */
756 begin_specialization (void)
758 begin_scope (sk_template_spec
, NULL
);
759 note_template_header (1);
760 return check_specialization_scope ();
763 /* Called at then end of processing a declaration preceded by
767 end_specialization (void)
770 reset_specialization ();
773 /* Any template <>'s that we have seen thus far are not referring to a
774 function specialization. */
777 reset_specialization (void)
779 processing_specialization
= 0;
780 template_header_count
= 0;
783 /* We've just seen a template header. If SPECIALIZATION is nonzero,
784 it was of the form template <>. */
787 note_template_header (int specialization
)
789 processing_specialization
= specialization
;
790 template_header_count
++;
793 /* We're beginning an explicit instantiation. */
796 begin_explicit_instantiation (void)
798 gcc_assert (!processing_explicit_instantiation
);
799 processing_explicit_instantiation
= true;
804 end_explicit_instantiation (void)
806 gcc_assert (processing_explicit_instantiation
);
807 processing_explicit_instantiation
= false;
810 /* An explicit specialization or partial specialization of TMPL is being
811 declared. Check that the namespace in which the specialization is
812 occurring is permissible. Returns false iff it is invalid to
813 specialize TMPL in the current namespace. */
816 check_specialization_namespace (tree tmpl
)
818 tree tpl_ns
= decl_namespace_context (tmpl
);
822 An explicit specialization shall be declared in a namespace enclosing the
823 specialized template. An explicit specialization whose declarator-id is
824 not qualified shall be declared in the nearest enclosing namespace of the
825 template, or, if the namespace is inline (7.3.1), any namespace from its
826 enclosing namespace set. */
827 if (current_scope() != DECL_CONTEXT (tmpl
)
828 && !at_namespace_scope_p ())
830 error ("specialization of %qD must appear at namespace scope", tmpl
);
834 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
835 /* Same or enclosing namespace. */
839 auto_diagnostic_group d
;
840 if (permerror (input_location
,
841 "specialization of %qD in different namespace", tmpl
))
842 inform (DECL_SOURCE_LOCATION (tmpl
),
843 " from definition of %q#D", tmpl
);
848 /* SPEC is an explicit instantiation. Check that it is valid to
849 perform this explicit instantiation in the current namespace. */
852 check_explicit_instantiation_namespace (tree spec
)
856 /* DR 275: An explicit instantiation shall appear in an enclosing
857 namespace of its template. */
858 ns
= decl_namespace_context (spec
);
859 if (!is_nested_namespace (current_namespace
, ns
))
860 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
861 "(which does not enclose namespace %qD)",
862 spec
, current_namespace
, ns
);
865 /* Returns the type of a template specialization only if that
866 specialization needs to be defined. Otherwise (e.g., if the type has
867 already been defined), the function returns NULL_TREE. */
870 maybe_new_partial_specialization (tree type
)
872 /* An implicit instantiation of an incomplete type implies
873 the definition of a new class template.
881 Here, S<T*> is an implicit instantiation of S whose type
883 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
886 /* It can also be the case that TYPE is a completed specialization.
887 Continuing the previous example, suppose we also declare:
893 Here, S<T*> refers to the specialization S<T*> defined
894 above. However, we need to differentiate definitions because
895 we intend to define a new partial specialization. In this case,
896 we rely on the fact that the constraints are different for
897 this declaration than that above.
899 Note that we also get here for injected class names and
900 late-parsed template definitions. We must ensure that we
901 do not create new type declarations for those cases. */
902 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
904 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
905 tree args
= CLASSTYPE_TI_ARGS (type
);
907 /* If there are no template parameters, this cannot be a new
908 partial template specialization? */
909 if (!current_template_parms
)
912 /* The injected-class-name is not a new partial specialization. */
913 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
916 /* If the constraints are not the same as those of the primary
917 then, we can probably create a new specialization. */
918 tree type_constr
= current_template_constraints ();
920 if (type
== TREE_TYPE (tmpl
))
922 tree main_constr
= get_constraints (tmpl
);
923 if (equivalent_constraints (type_constr
, main_constr
))
927 /* Also, if there's a pre-existing specialization with matching
928 constraints, then this also isn't new. */
929 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
932 tree spec_tmpl
= TREE_VALUE (specs
);
933 tree spec_args
= TREE_PURPOSE (specs
);
934 tree spec_constr
= get_constraints (spec_tmpl
);
935 if (comp_template_args (args
, spec_args
)
936 && equivalent_constraints (type_constr
, spec_constr
))
938 specs
= TREE_CHAIN (specs
);
941 /* Create a new type node (and corresponding type decl)
942 for the newly declared specialization. */
943 tree t
= make_class_type (TREE_CODE (type
));
944 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
945 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
947 /* We only need a separate type node for storing the definition of this
948 partial specialization; uses of S<T*> are unconstrained, so all are
949 equivalent. So keep TYPE_CANONICAL the same. */
950 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
952 /* Build the corresponding type decl. */
953 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
954 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
955 DECL_SOURCE_LOCATION (d
) = input_location
;
956 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
957 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
965 /* The TYPE is being declared. If it is a template type, that means it
966 is a partial specialization. Do appropriate error-checking. */
969 maybe_process_partial_specialization (tree type
)
973 if (type
== error_mark_node
)
974 return error_mark_node
;
976 /* A lambda that appears in specialization context is not itself a
978 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
981 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
983 error ("name of class shadows template template parameter %qD",
985 return error_mark_node
;
988 context
= TYPE_CONTEXT (type
);
990 if (TYPE_ALIAS_P (type
))
992 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
994 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
995 error ("specialization of alias template %qD",
996 TI_TEMPLATE (tinfo
));
998 error ("explicit specialization of non-template %qT", type
);
999 return error_mark_node
;
1001 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1003 /* This is for ordinary explicit specialization and partial
1004 specialization of a template class such as:
1006 template <> class C<int>;
1010 template <class T> class C<T*>;
1012 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1014 if (tree t
= maybe_new_partial_specialization (type
))
1016 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1017 && !at_namespace_scope_p ())
1018 return error_mark_node
;
1019 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1020 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1021 if (processing_template_decl
)
1023 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1024 if (decl
== error_mark_node
)
1025 return error_mark_node
;
1026 return TREE_TYPE (decl
);
1029 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1030 error ("specialization of %qT after instantiation", type
);
1031 else if (errorcount
&& !processing_specialization
1032 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1033 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1034 /* Trying to define a specialization either without a template<> header
1035 or in an inappropriate place. We've already given an error, so just
1036 bail now so we don't actually define the specialization. */
1037 return error_mark_node
;
1039 else if (CLASS_TYPE_P (type
)
1040 && !CLASSTYPE_USE_TEMPLATE (type
)
1041 && CLASSTYPE_TEMPLATE_INFO (type
)
1042 && context
&& CLASS_TYPE_P (context
)
1043 && CLASSTYPE_TEMPLATE_INFO (context
))
1045 /* This is for an explicit specialization of member class
1046 template according to [temp.expl.spec/18]:
1048 template <> template <class U> class C<int>::D;
1050 The context `C<int>' must be an implicit instantiation.
1051 Otherwise this is just a member class template declared
1054 template <> class C<int> { template <class U> class D; };
1055 template <> template <class U> class C<int>::D;
1057 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1058 while in the second case, `C<int>::D' is a primary template
1059 and `C<T>::D' may not exist. */
1061 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1062 && !COMPLETE_TYPE_P (type
))
1065 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1067 if (current_namespace
1068 != decl_namespace_context (tmpl
))
1070 if (permerror (input_location
,
1071 "specialization of %qD in different namespace",
1073 inform (DECL_SOURCE_LOCATION (tmpl
),
1074 "from definition of %q#D", tmpl
);
1077 /* Check for invalid specialization after instantiation:
1079 template <> template <> class C<int>::D<int>;
1080 template <> template <class U> class C<int>::D; */
1082 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1083 t
; t
= TREE_CHAIN (t
))
1085 tree inst
= TREE_VALUE (t
);
1086 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1087 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1089 /* We already have a full specialization of this partial
1090 instantiation, or a full specialization has been
1091 looked up but not instantiated. Reassign it to the
1092 new member specialization template. */
1096 elt
.tmpl
= most_general_template (tmpl
);
1097 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1100 type_specializations
->remove_elt (&elt
);
1103 CLASSTYPE_TI_ARGS (inst
)
1104 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1107 = type_specializations
->find_slot (&elt
, INSERT
);
1108 entry
= ggc_alloc
<spec_entry
> ();
1113 /* But if we've had an implicit instantiation, that's a
1114 problem ([temp.expl.spec]/6). */
1115 error ("specialization %qT after instantiation %qT",
1119 /* Mark TYPE as a specialization. And as a result, we only
1120 have one level of template argument for the innermost
1122 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1123 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1124 CLASSTYPE_TI_ARGS (type
)
1125 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1128 else if (processing_specialization
)
1130 /* Someday C++0x may allow for enum template specialization. */
1131 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1132 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1133 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1134 "of %qD not allowed by ISO C++", type
);
1137 error ("explicit specialization of non-template %qT", type
);
1138 return error_mark_node
;
1145 /* Returns nonzero if we can optimize the retrieval of specializations
1146 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1147 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1150 optimize_specialization_lookup_p (tree tmpl
)
1152 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1153 && DECL_CLASS_SCOPE_P (tmpl
)
1154 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1156 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1157 /* The optimized lookup depends on the fact that the
1158 template arguments for the member function template apply
1159 purely to the containing class, which is not true if the
1160 containing class is an explicit or partial
1162 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1163 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1164 && !DECL_CONV_FN_P (tmpl
)
1165 /* It is possible to have a template that is not a member
1166 template and is not a member of a template class:
1168 template <typename T>
1169 struct S { friend A::f(); };
1171 Here, the friend function is a template, but the context does
1172 not have template information. The optimized lookup relies
1173 on having ARGS be the template arguments for both the class
1174 and the function template. */
1175 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1178 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1179 gone through coerce_template_parms by now. */
1182 verify_unstripped_args_1 (tree inner
)
1184 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1186 tree arg
= TREE_VEC_ELT (inner
, i
);
1187 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1189 else if (TYPE_P (arg
))
1190 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1191 else if (ARGUMENT_PACK_P (arg
))
1192 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1193 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1194 /* Allow typedefs on the type of a non-type argument, since a
1195 parameter can have them. */;
1197 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1202 verify_unstripped_args (tree args
)
1204 ++processing_template_decl
;
1205 if (!any_dependent_template_arguments_p (args
))
1206 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1207 --processing_template_decl
;
1210 /* Retrieve the specialization (in the sense of [temp.spec] - a
1211 specialization is either an instantiation or an explicit
1212 specialization) of TMPL for the given template ARGS. If there is
1213 no such specialization, return NULL_TREE. The ARGS are a vector of
1214 arguments, or a vector of vectors of arguments, in the case of
1215 templates with more than one level of parameters.
1217 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1218 then we search for a partial specialization matching ARGS. This
1219 parameter is ignored if TMPL is not a class template.
1221 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1222 result is a NONTYPE_ARGUMENT_PACK. */
1225 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1227 if (tmpl
== NULL_TREE
)
1230 if (args
== error_mark_node
)
1233 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1234 || TREE_CODE (tmpl
) == FIELD_DECL
);
1236 /* There should be as many levels of arguments as there are
1237 levels of parameters. */
1238 gcc_assert (TMPL_ARGS_DEPTH (args
)
1239 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1240 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1241 : template_class_depth (DECL_CONTEXT (tmpl
))));
1244 verify_unstripped_args (args
);
1246 /* Lambda functions in templates aren't instantiated normally, but through
1247 tsubst_lambda_expr. */
1248 if (lambda_fn_in_template_p (tmpl
))
1251 if (optimize_specialization_lookup_p (tmpl
))
1253 /* The template arguments actually apply to the containing
1254 class. Find the class specialization with those
1256 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1257 tree class_specialization
1258 = retrieve_specialization (class_template
, args
, 0);
1259 if (!class_specialization
)
1262 /* Find the instance of TMPL. */
1263 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1264 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1267 if (tree ti
= get_template_info (fn
))
1268 if (TI_TEMPLATE (ti
) == tmpl
1269 /* using-declarations can bring in a different
1270 instantiation of tmpl as a member of a different
1271 instantiation of tmpl's class. We don't want those
1273 && DECL_CONTEXT (fn
) == class_specialization
)
1282 spec_hash_table
*specializations
;
1286 elt
.spec
= NULL_TREE
;
1288 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1289 specializations
= type_specializations
;
1291 specializations
= decl_specializations
;
1294 hash
= spec_hasher::hash (&elt
);
1295 found
= specializations
->find_with_hash (&elt
, hash
);
1303 /* Like retrieve_specialization, but for local declarations. */
1306 retrieve_local_specialization (tree tmpl
)
1308 if (local_specializations
== NULL
)
1311 tree
*slot
= local_specializations
->get (tmpl
);
1312 return slot
? *slot
: NULL_TREE
;
1315 /* Returns nonzero iff DECL is a specialization of TMPL. */
1318 is_specialization_of (tree decl
, tree tmpl
)
1322 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1326 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1332 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1334 for (t
= TREE_TYPE (decl
);
1336 t
= CLASSTYPE_USE_TEMPLATE (t
)
1337 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1338 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1345 /* Returns nonzero iff DECL is a specialization of friend declaration
1346 FRIEND_DECL according to [temp.friend]. */
1349 is_specialization_of_friend (tree decl
, tree friend_decl
)
1351 bool need_template
= true;
1354 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1355 || TREE_CODE (decl
) == TYPE_DECL
);
1357 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1358 of a template class, we want to check if DECL is a specialization
1360 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1361 && DECL_TEMPLATE_INFO (friend_decl
)
1362 && !DECL_USE_TEMPLATE (friend_decl
))
1364 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1365 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1366 need_template
= false;
1368 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1369 && !PRIMARY_TEMPLATE_P (friend_decl
))
1370 need_template
= false;
1372 /* There is nothing to do if this is not a template friend. */
1373 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1376 if (is_specialization_of (decl
, friend_decl
))
1380 A member of a class template may be declared to be a friend of a
1381 non-template class. In this case, the corresponding member of
1382 every specialization of the class template is a friend of the
1383 class granting friendship.
1385 For example, given a template friend declaration
1387 template <class T> friend void A<T>::f();
1389 the member function below is considered a friend
1391 template <> struct A<int> {
1395 For this type of template friend, TEMPLATE_DEPTH below will be
1396 nonzero. To determine if DECL is a friend of FRIEND, we first
1397 check if the enclosing class is a specialization of another. */
1399 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1401 && DECL_CLASS_SCOPE_P (decl
)
1402 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1403 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1405 /* Next, we check the members themselves. In order to handle
1406 a few tricky cases, such as when FRIEND_DECL's are
1408 template <class T> friend void A<T>::g(T t);
1409 template <class T> template <T t> friend void A<T>::h();
1413 void A<int>::g(int);
1414 template <int> void A<int>::h();
1416 we need to figure out ARGS, the template arguments from
1417 the context of DECL. This is required for template substitution
1418 of `T' in the function parameter of `g' and template parameter
1419 of `h' in the above examples. Here ARGS corresponds to `int'. */
1421 tree context
= DECL_CONTEXT (decl
);
1422 tree args
= NULL_TREE
;
1423 int current_depth
= 0;
1425 while (current_depth
< template_depth
)
1427 if (CLASSTYPE_TEMPLATE_INFO (context
))
1429 if (current_depth
== 0)
1430 args
= TYPE_TI_ARGS (context
);
1432 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1435 context
= TYPE_CONTEXT (context
);
1438 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1443 tree friend_args_type
;
1444 tree decl_args_type
;
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1448 is_template
= DECL_TEMPLATE_INFO (decl
)
1449 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1450 if (need_template
^ is_template
)
1452 else if (is_template
)
1454 /* If both are templates, check template parameter list. */
1456 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1458 if (!comp_template_parms
1459 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1463 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1466 decl_type
= TREE_TYPE (decl
);
1468 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1469 tf_none
, NULL_TREE
);
1470 if (friend_type
== error_mark_node
)
1473 /* Check if return types match. */
1474 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1477 /* Check if function parameter types match, ignoring the
1478 `this' parameter. */
1479 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1480 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1482 friend_args_type
= TREE_CHAIN (friend_args_type
);
1483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1484 decl_args_type
= TREE_CHAIN (decl_args_type
);
1486 return compparms (decl_args_type
, friend_args_type
);
1490 /* DECL is a TYPE_DECL */
1492 tree decl_type
= TREE_TYPE (decl
);
1494 /* Make sure that both DECL and FRIEND_DECL are templates or
1497 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1498 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1500 if (need_template
^ is_template
)
1502 else if (is_template
)
1505 /* If both are templates, check the name of the two
1506 TEMPLATE_DECL's first because is_friend didn't. */
1507 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1508 != DECL_NAME (friend_decl
))
1511 /* Now check template parameter list. */
1513 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1515 return comp_template_parms
1516 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1520 return (DECL_NAME (decl
)
1521 == DECL_NAME (friend_decl
));
1527 /* Register the specialization SPEC as a specialization of TMPL with
1528 the indicated ARGS. IS_FRIEND indicates whether the specialization
1529 is actually just a friend declaration. ATTRLIST is the list of
1530 attributes that the specialization is declared with or NULL when
1531 it isn't. Returns SPEC, or an equivalent prior declaration, if
1534 We also store instantiations of field packs in the hash table, even
1535 though they are not themselves templates, to make lookup easier. */
1538 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1542 spec_entry
**slot
= NULL
;
1545 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1546 || (TREE_CODE (tmpl
) == FIELD_DECL
1547 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1549 if (TREE_CODE (spec
) == FUNCTION_DECL
1550 && uses_template_parms (DECL_TI_ARGS (spec
)))
1551 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1552 register it; we want the corresponding TEMPLATE_DECL instead.
1553 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1554 the more obvious `uses_template_parms (spec)' to avoid problems
1555 with default function arguments. In particular, given
1556 something like this:
1558 template <class T> void f(T t1, T t = T())
1560 the default argument expression is not substituted for in an
1561 instantiation unless and until it is actually needed. */
1564 if (optimize_specialization_lookup_p (tmpl
))
1565 /* We don't put these specializations in the hash table, but we might
1566 want to give an error about a mismatch. */
1567 fn
= retrieve_specialization (tmpl
, args
, 0);
1575 hash
= spec_hasher::hash (&elt
);
1577 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1584 /* We can sometimes try to re-register a specialization that we've
1585 already got. In particular, regenerate_decl_from_template calls
1586 duplicate_decls which will update the specialization list. But,
1587 we'll still get called again here anyhow. It's more convenient
1588 to simply allow this than to try to prevent it. */
1591 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1593 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1595 if (DECL_ODR_USED (fn
)
1596 || DECL_EXPLICIT_INSTANTIATION (fn
))
1598 error ("specialization of %qD after instantiation",
1600 return error_mark_node
;
1605 /* This situation should occur only if the first
1606 specialization is an implicit instantiation, the
1607 second is an explicit specialization, and the
1608 implicit instantiation has not yet been used. That
1609 situation can occur if we have implicitly
1610 instantiated a member function and then specialized
1613 We can also wind up here if a friend declaration that
1614 looked like an instantiation turns out to be a
1617 template <class T> void foo(T);
1618 class S { friend void foo<>(int) };
1619 template <> void foo(int);
1621 We transform the existing DECL in place so that any
1622 pointers to it become pointers to the updated
1625 If there was a definition for the template, but not
1626 for the specialization, we want this to look as if
1627 there were no definition, and vice versa. */
1628 DECL_INITIAL (fn
) = NULL_TREE
;
1629 duplicate_decls (spec
, fn
, is_friend
);
1630 /* The call to duplicate_decls will have applied
1633 An explicit specialization of a function template
1634 is inline only if it is explicitly declared to be,
1635 and independently of whether its function template
1638 to the primary function; now copy the inline bits to
1639 the various clones. */
1640 FOR_EACH_CLONE (clone
, fn
)
1642 DECL_DECLARED_INLINE_P (clone
)
1643 = DECL_DECLARED_INLINE_P (fn
);
1644 DECL_SOURCE_LOCATION (clone
)
1645 = DECL_SOURCE_LOCATION (fn
);
1646 DECL_DELETED_FN (clone
)
1647 = DECL_DELETED_FN (fn
);
1649 check_specialization_namespace (tmpl
);
1654 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1656 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1657 if (dd
== error_mark_node
)
1658 /* We've already complained in duplicate_decls. */
1659 return error_mark_node
;
1661 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1662 /* Dup decl failed, but this is a new definition. Set the
1663 line number so any errors match this new
1665 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1671 return duplicate_decls (spec
, fn
, is_friend
);
1673 /* A specialization must be declared in the same namespace as the
1674 template it is specializing. */
1675 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1676 && !check_specialization_namespace (tmpl
))
1677 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1679 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1681 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1682 gcc_assert (tmpl
&& args
&& spec
);
1685 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1686 && PRIMARY_TEMPLATE_P (tmpl
)
1687 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1688 || variable_template_p (tmpl
))
1689 /* If TMPL is a forward declaration of a template function, keep a list
1690 of all specializations in case we need to reassign them to a friend
1691 template later in tsubst_friend_function.
1693 Also keep a list of all variable template instantiations so that
1694 process_partial_specialization can check whether a later partial
1695 specialization would have used it. */
1696 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1697 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1703 /* Returns true iff two spec_entry nodes are equivalent. */
1705 int comparing_specializations
;
1708 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1712 ++comparing_specializations
;
1713 equal
= (e1
->tmpl
== e2
->tmpl
1714 && comp_template_args (e1
->args
, e2
->args
));
1715 if (equal
&& flag_concepts
1716 /* tmpl could be a FIELD_DECL for a capture pack. */
1717 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1718 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1719 && uses_template_parms (e1
->args
))
1721 /* Partial specializations of a variable template can be distinguished by
1723 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1724 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1725 equal
= equivalent_constraints (c1
, c2
);
1727 --comparing_specializations
;
1732 /* Returns a hash for a template TMPL and template arguments ARGS. */
1735 hash_tmpl_and_args (tree tmpl
, tree args
)
1737 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1738 return iterative_hash_template_arg (args
, val
);
1741 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1745 spec_hasher::hash (spec_entry
*e
)
1747 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1750 /* Recursively calculate a hash value for a template argument ARG, for use
1751 in the hash tables of template specializations. We must be
1752 careful to (at least) skip the same entities template_args_equal
1756 iterative_hash_template_arg (tree arg
, hashval_t val
)
1758 if (arg
== NULL_TREE
)
1759 return iterative_hash_object (arg
, val
);
1762 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1763 while (CONVERT_EXPR_P (arg
)
1764 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1765 || class_nttp_const_wrapper_p (arg
))
1766 arg
= TREE_OPERAND (arg
, 0);
1768 enum tree_code code
= TREE_CODE (arg
);
1770 val
= iterative_hash_object (code
, val
);
1774 case ARGUMENT_PACK_SELECT
:
1780 case IDENTIFIER_NODE
:
1781 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1784 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1785 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1788 case TYPE_PACK_EXPANSION
:
1789 case EXPR_PACK_EXPANSION
:
1790 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1791 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1793 case TYPE_ARGUMENT_PACK
:
1794 case NONTYPE_ARGUMENT_PACK
:
1795 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1798 for (; arg
; arg
= TREE_CHAIN (arg
))
1799 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1803 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1804 val
= iterative_hash_template_arg (*iter
, val
);
1811 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1812 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1814 val
= iterative_hash_template_arg (field
, val
);
1815 val
= iterative_hash_template_arg (value
, val
);
1821 if (!DECL_ARTIFICIAL (arg
))
1823 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1824 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1826 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1829 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1832 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1833 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1835 case TEMPLATE_PARM_INDEX
:
1836 val
= iterative_hash_template_arg
1837 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1838 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1839 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1842 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1843 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1844 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1847 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1849 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1853 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1854 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1855 val
= iterative_hash_object (code
, val
);
1856 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1859 /* [temp.over.link] Two lambda-expressions are never considered
1862 So just hash the closure type. */
1863 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1866 case IMPLICIT_CONV_EXPR
:
1867 case STATIC_CAST_EXPR
:
1868 case REINTERPRET_CAST_EXPR
:
1869 case CONST_CAST_EXPR
:
1870 case DYNAMIC_CAST_EXPR
:
1872 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1873 /* Now hash operands as usual. */
1878 tree fn
= CALL_EXPR_FN (arg
);
1879 if (tree name
= dependent_name (fn
))
1881 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1882 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1885 val
= iterative_hash_template_arg (fn
, val
);
1886 call_expr_arg_iterator ai
;
1887 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1888 x
= next_call_expr_arg (&ai
))
1889 val
= iterative_hash_template_arg (x
, val
);
1897 char tclass
= TREE_CODE_CLASS (code
);
1901 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1903 // We want an alias specialization that survived strip_typedefs
1904 // to hash differently from its TYPE_CANONICAL, to avoid hash
1905 // collisions that compare as different in template_args_equal.
1906 // These could be dependent specializations that strip_typedefs
1907 // left alone, or untouched specializations because
1908 // coerce_template_parms returns the unconverted template
1909 // arguments if it sees incomplete argument packs.
1910 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1911 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1914 switch (TREE_CODE (arg
))
1916 case TEMPLATE_TEMPLATE_PARM
:
1918 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1920 /* Do not recurse with TPI directly, as that is unbounded
1922 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1923 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1928 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1932 if (tree canonical
= TYPE_CANONICAL (arg
))
1933 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1939 case tcc_declaration
:
1941 return iterative_hash_expr (arg
, val
);
1944 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1945 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1946 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1954 /* Unregister the specialization SPEC as a specialization of TMPL.
1955 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1956 if the SPEC was listed as a specialization of TMPL.
1958 Note that SPEC has been ggc_freed, so we can't look inside it. */
1961 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1966 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1967 elt
.args
= TI_ARGS (tinfo
);
1968 elt
.spec
= NULL_TREE
;
1970 entry
= decl_specializations
->find (&elt
);
1973 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1974 gcc_assert (new_spec
!= NULL_TREE
);
1975 entry
->spec
= new_spec
;
1982 /* Like register_specialization, but for local declarations. We are
1983 registering SPEC, an instantiation of TMPL. */
1986 register_local_specialization (tree spec
, tree tmpl
)
1988 gcc_assert (tmpl
!= spec
);
1989 local_specializations
->put (tmpl
, spec
);
1992 /* TYPE is a class type. Returns true if TYPE is an explicitly
1993 specialized class. */
1996 explicit_class_specialization_p (tree type
)
1998 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2000 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2003 /* Print the list of functions at FNS, going through all the overloads
2004 for each element of the list. Alternatively, FNS cannot be a
2005 TREE_LIST, in which case it will be printed together with all the
2008 MORE and *STR should respectively be FALSE and NULL when the function
2009 is called from the outside. They are used internally on recursive
2010 calls. print_candidates manages the two parameters and leaves NULL
2011 in *STR when it ends. */
2014 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2016 if (TREE_CODE (fns
) == TREE_LIST
)
2017 for (; fns
; fns
= TREE_CHAIN (fns
))
2018 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2020 for (lkp_iterator
iter (fns
); iter
;)
2025 const char *pfx
= *str
;
2029 pfx
= _("candidates are:");
2031 pfx
= _("candidate is:");
2032 *str
= get_spaces (pfx
);
2034 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2038 /* Print the list of candidate FNS in an error message. FNS can also
2039 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2042 print_candidates (tree fns
)
2045 print_candidates_1 (fns
, &str
);
2049 /* Get a (possibly) constrained template declaration for the
2050 purpose of ordering candidates. */
2052 get_template_for_ordering (tree list
)
2054 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2055 tree f
= TREE_VALUE (list
);
2056 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2057 return TI_TEMPLATE (ti
);
2061 /* Among candidates having the same signature, return the
2062 most constrained or NULL_TREE if there is no best candidate.
2063 If the signatures of candidates vary (e.g., template
2064 specialization vs. member function), then there can be no
2067 Note that we don't compare constraints on the functions
2068 themselves, but rather those of their templates. */
2070 most_constrained_function (tree candidates
)
2072 // Try to find the best candidate in a first pass.
2073 tree champ
= candidates
;
2074 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2076 int winner
= more_constrained (get_template_for_ordering (champ
),
2077 get_template_for_ordering (c
));
2079 champ
= c
; // The candidate is more constrained
2080 else if (winner
== 0)
2081 return NULL_TREE
; // Neither is more constrained
2084 // Verify that the champ is better than previous candidates.
2085 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2086 if (!more_constrained (get_template_for_ordering (champ
),
2087 get_template_for_ordering (c
)))
2095 /* Returns the template (one of the functions given by TEMPLATE_ID)
2096 which can be specialized to match the indicated DECL with the
2097 explicit template args given in TEMPLATE_ID. The DECL may be
2098 NULL_TREE if none is available. In that case, the functions in
2099 TEMPLATE_ID are non-members.
2101 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2102 specialization of a member template.
2104 The TEMPLATE_COUNT is the number of references to qualifying
2105 template classes that appeared in the name of the function. See
2106 check_explicit_specialization for a more accurate description.
2108 TSK indicates what kind of template declaration (if any) is being
2109 declared. TSK_TEMPLATE indicates that the declaration given by
2110 DECL, though a FUNCTION_DECL, has template parameters, and is
2111 therefore a template function.
2113 The template args (those explicitly specified and those deduced)
2114 are output in a newly created vector *TARGS_OUT.
2116 If it is impossible to determine the result, an error message is
2117 issued. The error_mark_node is returned to indicate failure. */
2120 determine_specialization (tree template_id
,
2123 int need_member_template
,
2129 tree explicit_targs
;
2130 tree candidates
= NULL_TREE
;
2132 /* A TREE_LIST of templates of which DECL may be a specialization.
2133 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2134 corresponding TREE_PURPOSE is the set of template arguments that,
2135 when used to instantiate the template, would produce a function
2136 with the signature of DECL. */
2137 tree templates
= NULL_TREE
;
2139 cp_binding_level
*b
;
2141 *targs_out
= NULL_TREE
;
2143 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2144 return error_mark_node
;
2146 /* We shouldn't be specializing a member template of an
2147 unspecialized class template; we already gave an error in
2148 check_specialization_scope, now avoid crashing. */
2150 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2151 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2153 gcc_assert (errorcount
);
2154 return error_mark_node
;
2157 fns
= TREE_OPERAND (template_id
, 0);
2158 explicit_targs
= TREE_OPERAND (template_id
, 1);
2160 if (fns
== error_mark_node
)
2161 return error_mark_node
;
2163 /* Check for baselinks. */
2164 if (BASELINK_P (fns
))
2165 fns
= BASELINK_FUNCTIONS (fns
);
2167 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2169 error_at (DECL_SOURCE_LOCATION (decl
),
2170 "%qD is not a function template", fns
);
2171 return error_mark_node
;
2173 else if (VAR_P (decl
) && !variable_template_p (fns
))
2175 error ("%qD is not a variable template", fns
);
2176 return error_mark_node
;
2179 /* Count the number of template headers specified for this
2182 for (b
= current_binding_level
;
2183 b
->kind
== sk_template_parms
;
2187 tree orig_fns
= fns
;
2189 if (variable_template_p (fns
))
2191 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2192 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2193 tf_warning_or_error
,
2194 /*req_all*/true, /*use_defarg*/true);
2195 if (targs
!= error_mark_node
)
2196 templates
= tree_cons (targs
, fns
, templates
);
2198 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2202 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2204 tree decl_arg_types
;
2208 /* In case of explicit specialization, we need to check if
2209 the number of template headers appearing in the specialization
2210 is correct. This is usually done in check_explicit_specialization,
2211 but the check done there cannot be exhaustive when specializing
2212 member functions. Consider the following code:
2214 template <> void A<int>::f(int);
2215 template <> template <> void A<int>::f(int);
2217 Assuming that A<int> is not itself an explicit specialization
2218 already, the first line specializes "f" which is a non-template
2219 member function, whilst the second line specializes "f" which
2220 is a template member function. So both lines are syntactically
2221 correct, and check_explicit_specialization does not reject
2224 Here, we can do better, as we are matching the specialization
2225 against the declarations. We count the number of template
2226 headers, and we check if they match TEMPLATE_COUNT + 1
2227 (TEMPLATE_COUNT is the number of qualifying template classes,
2228 plus there must be another header for the member template
2231 Notice that if header_count is zero, this is not a
2232 specialization but rather a template instantiation, so there
2233 is no check we can perform here. */
2234 if (header_count
&& header_count
!= template_count
+ 1)
2237 /* Check that the number of template arguments at the
2238 innermost level for DECL is the same as for FN. */
2239 if (current_binding_level
->kind
== sk_template_parms
2240 && !current_binding_level
->explicit_spec_p
2241 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2242 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2243 (current_template_parms
))))
2246 /* DECL might be a specialization of FN. */
2247 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2248 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2250 /* For a non-static member function, we need to make sure
2251 that the const qualification is the same. Since
2252 get_bindings does not try to merge the "this" parameter,
2253 we must do the comparison explicitly. */
2254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2256 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2257 TREE_VALUE (decl_arg_types
)))
2260 /* And the ref-qualification. */
2261 if (type_memfn_rqual (TREE_TYPE (decl
))
2262 != type_memfn_rqual (TREE_TYPE (fn
)))
2266 /* Skip the "this" parameter and, for constructors of
2267 classes with virtual bases, the VTT parameter. A
2268 full specialization of a constructor will have a VTT
2269 parameter, but a template never will. */
2271 = skip_artificial_parms_for (decl
, decl_arg_types
);
2273 = skip_artificial_parms_for (fn
, fn_arg_types
);
2275 /* Function templates cannot be specializations; there are
2276 no partial specializations of functions. Therefore, if
2277 the type of DECL does not match FN, there is no
2280 Note that it should never be the case that we have both
2281 candidates added here, and for regular member functions
2283 if (tsk
== tsk_template
)
2285 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2286 current_template_parms
))
2288 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2289 TREE_TYPE (TREE_TYPE (fn
))))
2291 if (!compparms (fn_arg_types
, decl_arg_types
))
2294 tree freq
= get_trailing_function_requirements (fn
);
2295 tree dreq
= get_trailing_function_requirements (decl
);
2300 tree fargs
= DECL_TI_ARGS (fn
);
2301 tsubst_flags_t complain
= tf_none
;
2302 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2303 if (!cp_tree_equal (freq
, dreq
))
2307 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2311 /* See whether this function might be a specialization of this
2312 template. Suppress access control because we might be trying
2313 to make this specialization a friend, and we have already done
2314 access control for the declaration of the specialization. */
2315 push_deferring_access_checks (dk_no_check
);
2316 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2317 pop_deferring_access_checks ();
2320 /* We cannot deduce template arguments that when used to
2321 specialize TMPL will produce DECL. */
2324 if (uses_template_parms (targs
))
2325 /* We deduced something involving 'auto', which isn't a valid
2326 template argument. */
2329 /* Remove, from the set of candidates, all those functions
2330 whose constraints are not satisfied. */
2331 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2334 // Then, try to form the new function type.
2335 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2336 if (insttype
== error_mark_node
)
2339 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2340 if (!compparms (fn_arg_types
, decl_arg_types
))
2343 /* Save this template, and the arguments deduced. */
2344 templates
= tree_cons (targs
, fn
, templates
);
2346 else if (need_member_template
)
2347 /* FN is an ordinary member function, and we need a
2348 specialization of a member template. */
2350 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2351 /* We can get IDENTIFIER_NODEs here in certain erroneous
2354 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2355 /* This is just an ordinary non-member function. Nothing can
2356 be a specialization of that. */
2358 else if (DECL_ARTIFICIAL (fn
))
2359 /* Cannot specialize functions that are created implicitly. */
2363 tree decl_arg_types
;
2365 /* This is an ordinary member function. However, since
2366 we're here, we can assume its enclosing class is a
2367 template class. For example,
2369 template <typename T> struct S { void f(); };
2370 template <> void S<int>::f() {}
2372 Here, S<int>::f is a non-template, but S<int> is a
2373 template class. If FN has the same type as DECL, we
2374 might be in business. */
2376 if (!DECL_TEMPLATE_INFO (fn
))
2377 /* Its enclosing class is an explicit specialization
2378 of a template class. This is not a candidate. */
2381 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2382 TREE_TYPE (TREE_TYPE (fn
))))
2383 /* The return types differ. */
2386 /* Adjust the type of DECL in case FN is a static member. */
2387 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2388 if (DECL_STATIC_FUNCTION_P (fn
)
2389 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2390 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2392 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2396 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2397 && (type_memfn_rqual (TREE_TYPE (decl
))
2398 != type_memfn_rqual (TREE_TYPE (fn
))))
2401 // If the deduced arguments do not satisfy the constraints,
2402 // this is not a candidate.
2403 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2406 // Add the candidate.
2407 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2411 if (templates
&& TREE_CHAIN (templates
))
2417 It is possible for a specialization with a given function
2418 signature to be instantiated from more than one function
2419 template. In such cases, explicit specification of the
2420 template arguments must be used to uniquely identify the
2421 function template specialization being specialized.
2423 Note that here, there's no suggestion that we're supposed to
2424 determine which of the candidate templates is most
2425 specialized. However, we, also have:
2429 Partial ordering of overloaded function template
2430 declarations is used in the following contexts to select
2431 the function template to which a function template
2432 specialization refers:
2434 -- when an explicit specialization refers to a function
2437 So, we do use the partial ordering rules, at least for now.
2438 This extension can only serve to make invalid programs valid,
2439 so it's safe. And, there is strong anecdotal evidence that
2440 the committee intended the partial ordering rules to apply;
2441 the EDG front end has that behavior, and John Spicer claims
2442 that the committee simply forgot to delete the wording in
2443 [temp.expl.spec]. */
2444 tree tmpl
= most_specialized_instantiation (templates
);
2445 if (tmpl
!= error_mark_node
)
2448 TREE_CHAIN (templates
) = NULL_TREE
;
2452 // Concepts allows multiple declarations of member functions
2453 // with the same signature. Like above, we need to rely on
2454 // on the partial ordering of those candidates to determine which
2456 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2458 if (tree cand
= most_constrained_function (candidates
))
2461 TREE_CHAIN (cand
) = NULL_TREE
;
2465 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2467 error ("template-id %qD for %q+D does not match any template "
2468 "declaration", template_id
, decl
);
2469 if (header_count
&& header_count
!= template_count
+ 1)
2470 inform (DECL_SOURCE_LOCATION (decl
),
2471 "saw %d %<template<>%>, need %d for "
2472 "specializing a member function template",
2473 header_count
, template_count
+ 1);
2475 print_candidates (orig_fns
);
2476 return error_mark_node
;
2478 else if ((templates
&& TREE_CHAIN (templates
))
2479 || (candidates
&& TREE_CHAIN (candidates
))
2480 || (templates
&& candidates
))
2482 error ("ambiguous template specialization %qD for %q+D",
2484 candidates
= chainon (candidates
, templates
);
2485 print_candidates (candidates
);
2486 return error_mark_node
;
2489 /* We have one, and exactly one, match. */
2492 tree fn
= TREE_VALUE (candidates
);
2493 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2495 /* Propagate the candidate's constraints to the declaration. */
2496 if (tsk
!= tsk_template
)
2497 set_constraints (decl
, get_constraints (fn
));
2499 /* DECL is a re-declaration or partial instantiation of a template
2501 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2503 /* It was a specialization of an ordinary member function in a
2505 return DECL_TI_TEMPLATE (fn
);
2508 /* It was a specialization of a template. */
2509 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2510 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2512 *targs_out
= copy_node (targs
);
2513 SET_TMPL_ARGS_LEVEL (*targs_out
,
2514 TMPL_ARGS_DEPTH (*targs_out
),
2515 TREE_PURPOSE (templates
));
2518 *targs_out
= TREE_PURPOSE (templates
);
2519 return TREE_VALUE (templates
);
2522 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2523 but with the default argument values filled in from those in the
2527 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2530 tree new_spec_types
;
2535 if (spec_types
== void_list_node
)
2536 return void_list_node
;
2538 /* Substitute into the rest of the list. */
2540 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2541 TREE_CHAIN (tmpl_types
));
2543 /* Add the default argument for this parameter. */
2544 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2545 TREE_VALUE (spec_types
),
2549 /* DECL is an explicit specialization. Replicate default arguments
2550 from the template it specializes. (That way, code like:
2552 template <class T> void f(T = 3);
2553 template <> void f(double);
2556 works, as required.) An alternative approach would be to look up
2557 the correct default arguments at the call-site, but this approach
2558 is consistent with how implicit instantiations are handled. */
2561 copy_default_args_to_explicit_spec (tree decl
)
2566 tree new_spec_types
;
2570 tree object_type
= NULL_TREE
;
2571 tree in_charge
= NULL_TREE
;
2572 tree vtt
= NULL_TREE
;
2574 /* See if there's anything we need to do. */
2575 tmpl
= DECL_TI_TEMPLATE (decl
);
2576 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2577 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2578 if (TREE_PURPOSE (t
))
2583 old_type
= TREE_TYPE (decl
);
2584 spec_types
= TYPE_ARG_TYPES (old_type
);
2586 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2588 /* Remove the this pointer, but remember the object's type for
2590 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2591 spec_types
= TREE_CHAIN (spec_types
);
2592 tmpl_types
= TREE_CHAIN (tmpl_types
);
2594 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2596 /* DECL may contain more parameters than TMPL due to the extra
2597 in-charge parameter in constructors and destructors. */
2598 in_charge
= spec_types
;
2599 spec_types
= TREE_CHAIN (spec_types
);
2601 if (DECL_HAS_VTT_PARM_P (decl
))
2604 spec_types
= TREE_CHAIN (spec_types
);
2608 /* Compute the merged default arguments. */
2610 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2612 /* Compute the new FUNCTION_TYPE. */
2616 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2621 /* Put the in-charge parameter back. */
2622 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2623 TREE_VALUE (in_charge
),
2626 new_type
= build_method_type_directly (object_type
,
2627 TREE_TYPE (old_type
),
2631 new_type
= build_function_type (TREE_TYPE (old_type
),
2633 new_type
= cp_build_type_attribute_variant (new_type
,
2634 TYPE_ATTRIBUTES (old_type
));
2635 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2637 TREE_TYPE (decl
) = new_type
;
2640 /* Return the number of template headers we expect to see for a definition
2641 or specialization of CTYPE or one of its non-template members. */
2644 num_template_headers_for_class (tree ctype
)
2646 int num_templates
= 0;
2648 while (ctype
&& CLASS_TYPE_P (ctype
))
2650 /* You're supposed to have one `template <...>' for every
2651 template class, but you don't need one for a full
2652 specialization. For example:
2654 template <class T> struct S{};
2655 template <> struct S<int> { void f(); };
2656 void S<int>::f () {}
2658 is correct; there shouldn't be a `template <>' for the
2659 definition of `S<int>::f'. */
2660 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2661 /* If CTYPE does not have template information of any
2662 kind, then it is not a template, nor is it nested
2663 within a template. */
2665 if (explicit_class_specialization_p (ctype
))
2667 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2670 ctype
= TYPE_CONTEXT (ctype
);
2673 return num_templates
;
2676 /* Do a simple sanity check on the template headers that precede the
2677 variable declaration DECL. */
2680 check_template_variable (tree decl
)
2682 tree ctx
= CP_DECL_CONTEXT (decl
);
2683 int wanted
= num_template_headers_for_class (ctx
);
2684 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2685 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2687 if (cxx_dialect
< cxx14
)
2688 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2689 "variable templates only available with "
2690 "%<-std=c++14%> or %<-std=gnu++14%>");
2692 // Namespace-scope variable templates should have a template header.
2695 if (template_header_count
> wanted
)
2697 auto_diagnostic_group d
;
2698 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2699 "too many template headers for %qD "
2702 if (warned
&& CLASS_TYPE_P (ctx
)
2703 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2704 inform (DECL_SOURCE_LOCATION (decl
),
2705 "members of an explicitly specialized class are defined "
2706 "without a template header");
2710 /* An explicit specialization whose declarator-id or class-head-name is not
2711 qualified shall be declared in the nearest enclosing namespace of the
2712 template, or, if the namespace is inline (7.3.1), any namespace from its
2713 enclosing namespace set.
2715 If the name declared in the explicit instantiation is an unqualified name,
2716 the explicit instantiation shall appear in the namespace where its template
2717 is declared or, if that namespace is inline (7.3.1), any namespace from its
2718 enclosing namespace set. */
2721 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2723 tree tmpl
= most_general_template (t
);
2724 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2725 && !is_nested_namespace (current_namespace
,
2726 CP_DECL_CONTEXT (tmpl
), true))
2728 if (processing_specialization
)
2729 permerror (loc
, "explicit specialization of %qD outside its "
2730 "namespace must use a nested-name-specifier", tmpl
);
2731 else if (processing_explicit_instantiation
2732 && cxx_dialect
>= cxx11
)
2733 /* This was allowed in C++98, so only pedwarn. */
2734 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2735 "outside its namespace must use a nested-name-"
2740 /* Warn for a template specialization SPEC that is missing some of a set
2741 of function or type attributes that the template TEMPL is declared with.
2742 ATTRLIST is a list of additional attributes that SPEC should be taken
2743 to ultimately be declared with. */
2746 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2748 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2749 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2751 /* Avoid warning if the difference between the primary and
2752 the specialization is not in one of the attributes below. */
2753 const char* const blacklist
[] = {
2754 "alloc_align", "alloc_size", "assume_aligned", "format",
2755 "format_arg", "malloc", "nonnull", NULL
2758 /* Put together a list of the black listed attributes that the primary
2759 template is declared with that the specialization is not, in case
2760 it's not apparent from the most recent declaration of the primary. */
2762 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2768 auto_diagnostic_group d
;
2769 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2770 "explicit specialization %q#D may be missing attributes",
2772 inform (DECL_SOURCE_LOCATION (tmpl
),
2774 ? G_("missing primary template attributes %s")
2775 : G_("missing primary template attribute %s"),
2776 pp_formatted_text (&str
));
2779 /* Check to see if the function just declared, as indicated in
2780 DECLARATOR, and in DECL, is a specialization of a function
2781 template. We may also discover that the declaration is an explicit
2782 instantiation at this point.
2784 Returns DECL, or an equivalent declaration that should be used
2785 instead if all goes well. Issues an error message if something is
2786 amiss. Returns error_mark_node if the error is not easily
2789 FLAGS is a bitmask consisting of the following flags:
2791 2: The function has a definition.
2792 4: The function is a friend.
2794 The TEMPLATE_COUNT is the number of references to qualifying
2795 template classes that appeared in the name of the function. For
2798 template <class T> struct S { void f(); };
2801 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2802 classes are not counted in the TEMPLATE_COUNT, so that in
2804 template <class T> struct S {};
2805 template <> struct S<int> { void f(); }
2806 template <> void S<int>::f();
2808 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2809 invalid; there should be no template <>.)
2811 If the function is a specialization, it is marked as such via
2812 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2813 is set up correctly, and it is added to the list of specializations
2814 for that template. */
2817 check_explicit_specialization (tree declarator
,
2823 int have_def
= flags
& 2;
2824 int is_friend
= flags
& 4;
2825 bool is_concept
= flags
& 8;
2826 int specialization
= 0;
2827 int explicit_instantiation
= 0;
2828 int member_specialization
= 0;
2829 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2830 tree dname
= DECL_NAME (decl
);
2835 if (!processing_specialization
)
2838 tsk
= tsk_excessive_parms
;
2841 tsk
= current_tmpl_spec_kind (template_count
);
2846 if (processing_specialization
&& !VAR_P (decl
))
2849 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2851 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2854 /* This could be something like:
2856 template <class T> void f(T);
2857 class S { friend void f<>(int); } */
2861 /* This case handles bogus declarations like template <>
2862 template <class T> void f<int>(); */
2864 error_at (cp_expr_loc_or_input_loc (declarator
),
2865 "template-id %qE in declaration of primary template",
2872 case tsk_invalid_member_spec
:
2873 /* The error has already been reported in
2874 check_specialization_scope. */
2875 return error_mark_node
;
2877 case tsk_invalid_expl_inst
:
2878 error ("template parameter list used in explicit instantiation");
2884 error ("definition provided for explicit instantiation");
2886 explicit_instantiation
= 1;
2889 case tsk_excessive_parms
:
2890 case tsk_insufficient_parms
:
2891 if (tsk
== tsk_excessive_parms
)
2892 error ("too many template parameter lists in declaration of %qD",
2894 else if (template_header_count
)
2895 error("too few template parameter lists in declaration of %qD", decl
);
2897 error("explicit specialization of %qD must be introduced by "
2898 "%<template <>%>", decl
);
2903 error ("explicit specialization declared %<concept%>");
2905 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2906 /* In cases like template<> constexpr bool v = true;
2907 We'll give an error in check_template_variable. */
2910 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2912 member_specialization
= 1;
2918 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2920 /* This case handles bogus declarations like template <>
2921 template <class T> void f<int>(); */
2923 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2924 error_at (cp_expr_loc_or_input_loc (declarator
),
2925 "template-id %qE in declaration of primary template",
2927 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2929 /* Partial specialization of variable template. */
2930 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2934 else if (cxx_dialect
< cxx14
)
2935 error_at (cp_expr_loc_or_input_loc (declarator
),
2936 "non-type partial specialization %qE "
2937 "is not allowed", declarator
);
2939 error_at (cp_expr_loc_or_input_loc (declarator
),
2940 "non-class, non-variable partial specialization %qE "
2941 "is not allowed", declarator
);
2946 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2947 /* This is a specialization of a member template, without
2948 specialization the containing class. Something like:
2950 template <class T> struct S {
2951 template <class U> void f (U);
2953 template <> template <class U> void S<int>::f(U) {}
2955 That's a specialization -- but of the entire template. */
2963 if ((specialization
|| member_specialization
)
2964 /* This doesn't apply to variable templates. */
2965 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2967 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2968 for (; t
; t
= TREE_CHAIN (t
))
2969 if (TREE_PURPOSE (t
))
2971 permerror (input_location
,
2972 "default argument specified in explicit specialization");
2977 if (specialization
|| member_specialization
|| explicit_instantiation
)
2979 tree tmpl
= NULL_TREE
;
2980 tree targs
= NULL_TREE
;
2981 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2983 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2984 if (!was_template_id
)
2988 gcc_assert (identifier_p (declarator
));
2993 /* If there is no class context, the explicit instantiation
2994 must be at namespace scope. */
2995 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2997 /* Find the namespace binding, using the declaration
2999 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3001 if (fns
== error_mark_node
)
3002 /* If lookup fails, look for a friend declaration so we can
3003 give a better diagnostic. */
3004 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3005 /*type*/false, /*complain*/true,
3008 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3010 error ("%qD is not a template function", dname
);
3011 fns
= error_mark_node
;
3015 declarator
= lookup_template_function (fns
, NULL_TREE
);
3018 if (declarator
== error_mark_node
)
3019 return error_mark_node
;
3021 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3023 if (!explicit_instantiation
)
3024 /* A specialization in class scope. This is invalid,
3025 but the error will already have been flagged by
3026 check_specialization_scope. */
3027 return error_mark_node
;
3030 /* It's not valid to write an explicit instantiation in
3033 class C { template void f(); }
3035 This case is caught by the parser. However, on
3038 template class C { void f(); };
3040 (which is invalid) we can get here. The error will be
3047 else if (ctype
!= NULL_TREE
3048 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3050 // We'll match variable templates in start_decl.
3054 /* Find the list of functions in ctype that have the same
3055 name as the declared function. */
3056 tree name
= TREE_OPERAND (declarator
, 0);
3058 if (constructor_name_p (name
, ctype
))
3060 if (DECL_CONSTRUCTOR_P (decl
)
3061 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3062 : !CLASSTYPE_DESTRUCTOR (ctype
))
3064 /* From [temp.expl.spec]:
3066 If such an explicit specialization for the member
3067 of a class template names an implicitly-declared
3068 special member function (clause _special_), the
3069 program is ill-formed.
3071 Similar language is found in [temp.explicit]. */
3072 error ("specialization of implicitly-declared special member function");
3073 return error_mark_node
;
3076 name
= DECL_NAME (decl
);
3079 /* For a type-conversion operator, We might be looking for
3080 `operator int' which will be a specialization of
3081 `operator T'. Grab all the conversion operators, and
3082 then select from them. */
3083 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3084 ? conv_op_identifier
: name
);
3086 if (fns
== NULL_TREE
)
3088 error ("no member function %qD declared in %qT", name
, ctype
);
3089 return error_mark_node
;
3092 TREE_OPERAND (declarator
, 0) = fns
;
3095 /* Figure out what exactly is being specialized at this point.
3096 Note that for an explicit instantiation, even one for a
3097 member function, we cannot tell a priori whether the
3098 instantiation is for a member template, or just a member
3099 function of a template class. Even if a member template is
3100 being instantiated, the member template arguments may be
3101 elided if they can be deduced from the rest of the
3103 tmpl
= determine_specialization (declarator
, decl
,
3105 member_specialization
,
3109 if (!tmpl
|| tmpl
== error_mark_node
)
3110 /* We couldn't figure out what this declaration was
3112 return error_mark_node
;
3115 if (TREE_CODE (decl
) == FUNCTION_DECL
3116 && DECL_HIDDEN_FRIEND_P (tmpl
))
3118 auto_diagnostic_group d
;
3119 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3120 "friend declaration %qD is not visible to "
3121 "explicit specialization", tmpl
))
3122 inform (DECL_SOURCE_LOCATION (tmpl
),
3123 "friend declaration here");
3125 else if (!ctype
&& !is_friend
3126 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3127 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3129 tree gen_tmpl
= most_general_template (tmpl
);
3131 if (explicit_instantiation
)
3133 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3134 is done by do_decl_instantiation later. */
3136 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3137 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3139 if (arg_depth
> parm_depth
)
3141 /* If TMPL is not the most general template (for
3142 example, if TMPL is a friend template that is
3143 injected into namespace scope), then there will
3144 be too many levels of TARGS. Remove some of them
3149 new_targs
= make_tree_vec (parm_depth
);
3150 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3151 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3152 = TREE_VEC_ELT (targs
, i
);
3156 return instantiate_template (tmpl
, targs
, tf_error
);
3159 /* If we thought that the DECL was a member function, but it
3160 turns out to be specializing a static member function,
3161 make DECL a static member function as well. */
3162 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3163 && DECL_STATIC_FUNCTION_P (tmpl
)
3164 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3165 revert_static_member_fn (decl
);
3167 /* If this is a specialization of a member template of a
3168 template class, we want to return the TEMPLATE_DECL, not
3169 the specialization of it. */
3170 if (tsk
== tsk_template
&& !was_template_id
)
3172 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3173 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3174 DECL_INITIAL (result
) = NULL_TREE
;
3178 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3179 DECL_SOURCE_LOCATION (result
)
3180 = DECL_SOURCE_LOCATION (decl
);
3181 /* We want to use the argument list specified in the
3182 definition, not in the original declaration. */
3183 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3184 for (parm
= DECL_ARGUMENTS (result
); parm
;
3185 parm
= DECL_CHAIN (parm
))
3186 DECL_CONTEXT (parm
) = result
;
3188 return register_specialization (tmpl
, gen_tmpl
, targs
,
3192 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3193 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3195 if (was_template_id
)
3196 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3198 /* Inherit default function arguments from the template
3199 DECL is specializing. */
3200 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3201 copy_default_args_to_explicit_spec (decl
);
3203 /* This specialization has the same protection as the
3204 template it specializes. */
3205 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3206 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3208 /* 7.1.1-1 [dcl.stc]
3210 A storage-class-specifier shall not be specified in an
3211 explicit specialization...
3213 The parser rejects these, so unless action is taken here,
3214 explicit function specializations will always appear with
3217 The action recommended by the C++ CWG in response to C++
3218 defect report 605 is to make the storage class and linkage
3219 of the explicit specialization match the templated function:
3221 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3223 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3225 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3226 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3228 /* A concept cannot be specialized. */
3229 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3231 error ("explicit specialization of function concept %qD",
3233 return error_mark_node
;
3236 /* This specialization has the same linkage and visibility as
3237 the function template it specializes. */
3238 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3239 if (! TREE_PUBLIC (decl
))
3241 DECL_INTERFACE_KNOWN (decl
) = 1;
3242 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3244 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3245 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3247 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3248 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3252 /* If DECL is a friend declaration, declared using an
3253 unqualified name, the namespace associated with DECL may
3254 have been set incorrectly. For example, in:
3256 template <typename T> void f(T);
3258 struct S { friend void f<int>(int); }
3261 we will have set the DECL_CONTEXT for the friend
3262 declaration to N, rather than to the global namespace. */
3263 if (DECL_NAMESPACE_SCOPE_P (decl
))
3264 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3266 if (is_friend
&& !have_def
)
3267 /* This is not really a declaration of a specialization.
3268 It's just the name of an instantiation. But, it's not
3269 a request for an instantiation, either. */
3270 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3271 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3272 /* A specialization is not necessarily COMDAT. */
3273 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3274 && DECL_DECLARED_INLINE_P (decl
));
3275 else if (VAR_P (decl
))
3276 DECL_COMDAT (decl
) = false;
3278 /* If this is a full specialization, register it so that we can find
3279 it again. Partial specializations will be registered in
3280 process_partial_specialization. */
3281 if (!processing_template_decl
)
3283 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3285 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3290 /* A 'structor should already have clones. */
3291 gcc_assert (decl
== error_mark_node
3292 || variable_template_p (tmpl
)
3293 || !(DECL_CONSTRUCTOR_P (decl
)
3294 || DECL_DESTRUCTOR_P (decl
))
3295 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3302 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3303 parameters. These are represented in the same format used for
3304 DECL_TEMPLATE_PARMS. */
3307 comp_template_parms (const_tree parms1
, const_tree parms2
)
3312 if (parms1
== parms2
)
3315 for (p1
= parms1
, p2
= parms2
;
3316 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3317 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3319 tree t1
= TREE_VALUE (p1
);
3320 tree t2
= TREE_VALUE (p2
);
3323 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3324 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3326 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3329 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3331 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3332 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3334 /* If either of the template parameters are invalid, assume
3335 they match for the sake of error recovery. */
3336 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3339 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3342 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3343 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3344 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3346 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3351 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3352 /* One set of parameters has more parameters lists than the
3359 /* Returns true if two template parameters are declared with
3360 equivalent constraints. */
3363 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3365 tree req1
= TREE_TYPE (parm1
);
3366 tree req2
= TREE_TYPE (parm2
);
3370 return cp_tree_equal (req1
, req2
);
3374 /* Returns true when two template parameters are equivalent. */
3377 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3379 tree decl1
= TREE_VALUE (parm1
);
3380 tree decl2
= TREE_VALUE (parm2
);
3382 /* If either of the template parameters are invalid, assume
3383 they match for the sake of error recovery. */
3384 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3387 /* ... they declare parameters of the same kind. */
3388 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3391 /* ... one parameter was introduced by a parameter declaration, then
3392 both are. This case arises as a result of eagerly rewriting declarations
3394 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3397 /* ... if either declares a pack, they both do. */
3398 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3401 if (TREE_CODE (decl1
) == PARM_DECL
)
3403 /* ... if they declare non-type parameters, the types are equivalent. */
3404 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3407 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3409 /* ... if they declare template template parameters, their template
3410 parameter lists are equivalent. */
3411 if (!template_heads_equivalent_p (decl1
, decl2
))
3415 /* ... if they are declared with a qualified-concept name, they both
3416 are, and those names are equivalent. */
3417 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3420 /* Returns true if two template parameters lists are equivalent.
3421 Two template parameter lists are equivalent if they have the
3422 same length and their corresponding parameters are equivalent.
3424 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3425 data structure returned by DECL_TEMPLATE_PARMS.
3427 This is generally the same implementation as comp_template_parms
3428 except that it also the concept names and arguments used to
3429 introduce parameters. */
3432 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3434 if (parms1
== parms2
)
3437 const_tree p1
= parms1
;
3438 const_tree p2
= parms2
;
3439 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3441 tree list1
= TREE_VALUE (p1
);
3442 tree list2
= TREE_VALUE (p2
);
3444 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3447 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3449 tree parm1
= TREE_VEC_ELT (list1
, i
);
3450 tree parm2
= TREE_VEC_ELT (list2
, i
);
3451 if (!template_parameters_equivalent_p (parm1
, parm2
))
3455 p1
= TREE_CHAIN (p1
);
3456 p2
= TREE_CHAIN (p2
);
3459 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3465 /* Return true if the requires-clause of the template parameter lists are
3466 equivalent and false otherwise. */
3468 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3470 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3471 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3472 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3474 if (!cp_tree_equal (req1
, req2
))
3479 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3480 Two template heads are equivalent if their template parameter
3481 lists are equivalent and their requires clauses are equivalent.
3483 In pre-C++20, this is equivalent to calling comp_template_parms
3484 for the template parameters of TMPL1 and TMPL2. */
3487 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3489 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3490 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3492 /* Don't change the matching rules for pre-C++20. */
3493 if (cxx_dialect
< cxx20
)
3494 return comp_template_parms (parms1
, parms2
);
3496 /* ... have the same number of template parameters, and their
3497 corresponding parameters are equivalent. */
3498 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3501 /* ... if either has a requires-clause, they both do and their
3502 corresponding constraint-expressions are equivalent. */
3503 return template_requirements_equivalent_p (parms1
, parms2
);
3506 /* Determine whether PARM is a parameter pack. */
3509 template_parameter_pack_p (const_tree parm
)
3511 /* Determine if we have a non-type template parameter pack. */
3512 if (TREE_CODE (parm
) == PARM_DECL
)
3513 return (DECL_TEMPLATE_PARM_P (parm
)
3514 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3515 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3516 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3518 /* If this is a list of template parameters, we could get a
3519 TYPE_DECL or a TEMPLATE_DECL. */
3520 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3521 parm
= TREE_TYPE (parm
);
3523 /* Otherwise it must be a type template parameter. */
3524 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3525 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3526 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3529 /* Determine if T is a function parameter pack. */
3532 function_parameter_pack_p (const_tree t
)
3534 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3535 return DECL_PACK_P (t
);
3539 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3540 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3543 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3545 if (! primary_func_tmpl_inst
3546 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3547 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3550 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3553 /* Return true iff the function parameter PARAM_DECL was expanded
3554 from the function parameter pack PACK. */
3557 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3559 if (DECL_ARTIFICIAL (param_decl
)
3560 || !function_parameter_pack_p (pack
))
3563 /* The parameter pack and its pack arguments have the same
3565 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3568 /* Determine whether ARGS describes a variadic template args list,
3569 i.e., one that is terminated by a template argument pack. */
3572 template_args_variadic_p (tree args
)
3577 if (args
== NULL_TREE
)
3580 args
= INNERMOST_TEMPLATE_ARGS (args
);
3581 nargs
= TREE_VEC_LENGTH (args
);
3586 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3588 return ARGUMENT_PACK_P (last_parm
);
3591 /* Generate a new name for the parameter pack name NAME (an
3592 IDENTIFIER_NODE) that incorporates its */
3595 make_ith_pack_parameter_name (tree name
, int i
)
3597 /* Munge the name to include the parameter index. */
3598 #define NUMBUF_LEN 128
3599 char numbuf
[NUMBUF_LEN
];
3603 if (name
== NULL_TREE
)
3605 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3606 newname_len
= IDENTIFIER_LENGTH (name
)
3607 + strlen (numbuf
) + 2;
3608 newname
= (char*)alloca (newname_len
);
3609 snprintf (newname
, newname_len
,
3610 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3611 return get_identifier (newname
);
3614 /* Return true if T is a primary function, class or alias template
3615 specialization, not including the template pattern. */
3618 primary_template_specialization_p (const_tree t
)
3623 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3624 return (DECL_LANG_SPECIFIC (t
)
3625 && DECL_USE_TEMPLATE (t
)
3626 && DECL_TEMPLATE_INFO (t
)
3627 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3628 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3629 return (CLASSTYPE_TEMPLATE_INFO (t
)
3630 && CLASSTYPE_USE_TEMPLATE (t
)
3631 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3632 else if (alias_template_specialization_p (t
, nt_transparent
))
3637 /* Return true if PARM is a template template parameter. */
3640 template_template_parameter_p (const_tree parm
)
3642 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3645 /* Return true iff PARM is a DECL representing a type template
3649 template_type_parameter_p (const_tree parm
)
3652 && (TREE_CODE (parm
) == TYPE_DECL
3653 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3654 && DECL_TEMPLATE_PARM_P (parm
));
3657 /* Return the template parameters of T if T is a
3658 primary template instantiation, NULL otherwise. */
3661 get_primary_template_innermost_parameters (const_tree t
)
3663 tree parms
= NULL
, template_info
= NULL
;
3665 if ((template_info
= get_template_info (t
))
3666 && primary_template_specialization_p (t
))
3667 parms
= INNERMOST_TEMPLATE_PARMS
3668 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3673 /* Return the template parameters of the LEVELth level from the full list
3674 of template parameters PARMS. */
3677 get_template_parms_at_level (tree parms
, int level
)
3681 || TREE_CODE (parms
) != TREE_LIST
3682 || level
> TMPL_PARMS_DEPTH (parms
))
3685 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3686 if (TMPL_PARMS_DEPTH (p
) == level
)
3692 /* Returns the template arguments of T if T is a template instantiation,
3696 get_template_innermost_arguments (const_tree t
)
3698 tree args
= NULL
, template_info
= NULL
;
3700 if ((template_info
= get_template_info (t
))
3701 && TI_ARGS (template_info
))
3702 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3707 /* Return the argument pack elements of T if T is a template argument pack,
3711 get_template_argument_pack_elems (const_tree t
)
3713 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3714 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3717 return ARGUMENT_PACK_ARGS (t
);
3720 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3721 ARGUMENT_PACK_SELECT represents. */
3724 argument_pack_select_arg (tree t
)
3726 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3727 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3729 /* If the selected argument is an expansion E, that most likely means we were
3730 called from gen_elem_of_pack_expansion_instantiation during the
3731 substituting of an argument pack (of which the Ith element is a pack
3732 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3733 In this case, the Ith element resulting from this substituting is going to
3734 be a pack expansion, which pattern is the pattern of E. Let's return the
3735 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3736 resulting pack expansion from it. */
3737 if (PACK_EXPANSION_P (arg
))
3739 /* Make sure we aren't throwing away arg info. */
3740 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3741 arg
= PACK_EXPANSION_PATTERN (arg
);
3748 /* True iff FN is a function representing a built-in variadic parameter
3752 builtin_pack_fn_p (tree fn
)
3755 || TREE_CODE (fn
) != FUNCTION_DECL
3756 || !DECL_IS_BUILTIN (fn
))
3759 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3765 /* True iff CALL is a call to a function representing a built-in variadic
3769 builtin_pack_call_p (tree call
)
3771 if (TREE_CODE (call
) != CALL_EXPR
)
3773 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3776 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3779 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3782 tree ohi
= CALL_EXPR_ARG (call
, 0);
3783 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3784 false/*fn*/, true/*int_cst*/);
3786 if (value_dependent_expression_p (hi
))
3790 call
= copy_node (call
);
3791 CALL_EXPR_ARG (call
, 0) = hi
;
3793 tree ex
= make_pack_expansion (call
, complain
);
3794 tree vec
= make_tree_vec (1);
3795 TREE_VEC_ELT (vec
, 0) = ex
;
3800 hi
= cxx_constant_value (hi
);
3801 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3803 /* Calculate the largest value of len that won't make the size of the vec
3804 overflow an int. The compiler will exceed resource limits long before
3805 this, but it seems a decent place to diagnose. */
3806 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3808 if (len
< 0 || len
> max
)
3810 if ((complain
& tf_error
)
3811 && hi
!= error_mark_node
)
3812 error ("argument to %<__integer_pack%> must be between 0 and %d",
3814 return error_mark_node
;
3817 tree vec
= make_tree_vec (len
);
3819 for (int i
= 0; i
< len
; ++i
)
3820 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3826 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3830 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3833 if (!builtin_pack_call_p (call
))
3836 tree fn
= CALL_EXPR_FN (call
);
3838 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3839 return expand_integer_pack (call
, args
, complain
, in_decl
);
3844 /* Structure used to track the progress of find_parameter_packs_r. */
3845 struct find_parameter_pack_data
3847 /* TREE_LIST that will contain all of the parameter packs found by
3849 tree
* parameter_packs
;
3851 /* Set of AST nodes that have been visited by the traversal. */
3852 hash_set
<tree
> *visited
;
3854 /* True iff we're making a type pack expansion. */
3855 bool type_pack_expansion_p
;
3858 /* Identifies all of the argument packs that occur in a template
3859 argument and appends them to the TREE_LIST inside DATA, which is a
3860 find_parameter_pack_data structure. This is a subroutine of
3861 make_pack_expansion and uses_parameter_packs. */
3863 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3866 struct find_parameter_pack_data
* ppd
=
3867 (struct find_parameter_pack_data
*)data
;
3868 bool parameter_pack_p
= false;
3870 /* Don't look through typedefs; we are interested in whether a
3871 parameter pack is actually written in the expression/type we're
3872 looking at, not the target type. */
3873 if (TYPE_P (t
) && typedef_variant_p (t
))
3875 /* But do look at arguments for an alias template. */
3876 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3877 cp_walk_tree (&TI_ARGS (tinfo
),
3878 &find_parameter_packs_r
,
3884 /* Identify whether this is a parameter pack or not. */
3885 switch (TREE_CODE (t
))
3887 case TEMPLATE_PARM_INDEX
:
3888 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3889 parameter_pack_p
= true;
3892 case TEMPLATE_TYPE_PARM
:
3893 t
= TYPE_MAIN_VARIANT (t
);
3895 case TEMPLATE_TEMPLATE_PARM
:
3896 /* If the placeholder appears in the decl-specifier-seq of a function
3897 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3898 is a pack expansion, the invented template parameter is a template
3900 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3901 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3902 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3903 parameter_pack_p
= true;
3908 if (DECL_PACK_P (t
))
3910 /* We don't want to walk into the type of a PARM_DECL,
3911 because we don't want to see the type parameter pack. */
3913 parameter_pack_p
= true;
3918 if (DECL_PACK_P (t
))
3920 /* We don't want to walk into the type of a variadic capture proxy,
3921 because we don't want to see the type parameter pack. */
3923 parameter_pack_p
= true;
3925 else if (variable_template_specialization_p (t
))
3927 cp_walk_tree (&DECL_TI_ARGS (t
),
3928 find_parameter_packs_r
,
3935 if (builtin_pack_call_p (t
))
3936 parameter_pack_p
= true;
3940 parameter_pack_p
= true;
3943 /* Not a parameter pack. */
3947 if (parameter_pack_p
)
3949 /* Add this parameter pack to the list. */
3950 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3954 cp_walk_tree (&TYPE_CONTEXT (t
),
3955 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3957 /* This switch statement will return immediately if we don't find a
3958 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3959 switch (TREE_CODE (t
))
3961 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3962 /* Check the template itself. */
3963 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3964 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3969 tree decl
= DECL_EXPR_DECL (t
);
3970 /* Ignore the declaration of a capture proxy for a parameter pack. */
3971 if (is_capture_proxy (decl
))
3973 if (is_typedef_decl (decl
))
3974 /* Since we stop at typedefs above, we need to look through them at
3975 the point of the DECL_EXPR. */
3976 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3977 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3982 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3984 cp_walk_tree (&TREE_TYPE (t
),
3985 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3988 case TYPE_PACK_EXPANSION
:
3989 case EXPR_PACK_EXPANSION
:
3994 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3999 case IDENTIFIER_NODE
:
4000 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4007 /* Since we defer implicit capture, look in the parms and body. */
4008 tree fn
= lambda_function (t
);
4009 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4011 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4018 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4019 type_pack_expansion_p to false so that any placeholders
4020 within the expression don't get marked as parameter packs. */
4021 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4022 ppd
->type_pack_expansion_p
= false;
4023 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4025 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4031 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4033 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4035 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4037 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4048 /* Determines if the expression or type T uses any parameter packs. */
4050 uses_parameter_packs (tree t
)
4052 tree parameter_packs
= NULL_TREE
;
4053 struct find_parameter_pack_data ppd
;
4054 ppd
.parameter_packs
= ¶meter_packs
;
4055 ppd
.visited
= new hash_set
<tree
>;
4056 ppd
.type_pack_expansion_p
= false;
4057 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4059 return parameter_packs
;
4062 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4063 representation a base-class initializer into a parameter pack
4064 expansion. If all goes well, the resulting node will be an
4065 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4068 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4071 tree parameter_packs
= NULL_TREE
;
4072 bool for_types
= false;
4073 struct find_parameter_pack_data ppd
;
4075 if (!arg
|| arg
== error_mark_node
)
4078 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4080 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4081 class initializer. In this case, the TREE_PURPOSE will be a
4082 _TYPE node (representing the base class expansion we're
4083 initializing) and the TREE_VALUE will be a TREE_LIST
4084 containing the initialization arguments.
4086 The resulting expansion looks somewhat different from most
4087 expansions. Rather than returning just one _EXPANSION, we
4088 return a TREE_LIST whose TREE_PURPOSE is a
4089 TYPE_PACK_EXPANSION containing the bases that will be
4090 initialized. The TREE_VALUE will be identical to the
4091 original TREE_VALUE, which is a list of arguments that will
4092 be passed to each base. We do not introduce any new pack
4093 expansion nodes into the TREE_VALUE (although it is possible
4094 that some already exist), because the TREE_PURPOSE and
4095 TREE_VALUE all need to be expanded together with the same
4096 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4097 resulting TREE_PURPOSE will mention the parameter packs in
4098 both the bases and the arguments to the bases. */
4101 tree parameter_packs
= NULL_TREE
;
4103 /* Determine which parameter packs will be used by the base
4105 ppd
.visited
= new hash_set
<tree
>;
4106 ppd
.parameter_packs
= ¶meter_packs
;
4107 ppd
.type_pack_expansion_p
= false;
4108 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4109 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4112 if (parameter_packs
== NULL_TREE
)
4114 if (complain
& tf_error
)
4115 error ("base initializer expansion %qT contains no parameter packs",
4118 return error_mark_node
;
4121 if (TREE_VALUE (arg
) != void_type_node
)
4123 /* Collect the sets of parameter packs used in each of the
4124 initialization arguments. */
4125 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4127 /* Determine which parameter packs will be expanded in this
4129 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4136 /* Create the pack expansion type for the base type. */
4137 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4138 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4139 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4140 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4142 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4143 they will rarely be compared to anything. */
4144 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4146 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4149 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4152 /* Build the PACK_EXPANSION_* node. */
4154 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4155 : make_node (EXPR_PACK_EXPANSION
);
4156 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4157 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4159 /* Propagate type and const-expression information. */
4160 TREE_TYPE (result
) = TREE_TYPE (arg
);
4161 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4162 /* Mark this read now, since the expansion might be length 0. */
4163 mark_exp_read (arg
);
4166 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4167 they will rarely be compared to anything. */
4168 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4170 /* Determine which parameter packs will be expanded. */
4171 ppd
.parameter_packs
= ¶meter_packs
;
4172 ppd
.visited
= new hash_set
<tree
>;
4173 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4174 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4177 /* Make sure we found some parameter packs. */
4178 if (parameter_packs
== NULL_TREE
)
4180 if (complain
& tf_error
)
4183 error ("expansion pattern %qT contains no parameter packs", arg
);
4185 error ("expansion pattern %qE contains no parameter packs", arg
);
4187 return error_mark_node
;
4189 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4191 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4196 /* Checks T for any "bare" parameter packs, which have not yet been
4197 expanded, and issues an error if any are found. This operation can
4198 only be done on full expressions or types (e.g., an expression
4199 statement, "if" condition, etc.), because we could have expressions like:
4201 foo(f(g(h(args)))...)
4203 where "args" is a parameter pack. check_for_bare_parameter_packs
4204 should not be called for the subexpressions args, h(args),
4205 g(h(args)), or f(g(h(args))), because we would produce erroneous
4208 Returns TRUE and emits an error if there were bare parameter packs,
4209 returns FALSE otherwise. */
4211 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4213 tree parameter_packs
= NULL_TREE
;
4214 struct find_parameter_pack_data ppd
;
4216 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4219 /* A lambda might use a parameter pack from the containing context. */
4220 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4221 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4224 if (TREE_CODE (t
) == TYPE_DECL
)
4227 ppd
.parameter_packs
= ¶meter_packs
;
4228 ppd
.visited
= new hash_set
<tree
>;
4229 ppd
.type_pack_expansion_p
= false;
4230 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4233 if (parameter_packs
)
4235 if (loc
== UNKNOWN_LOCATION
)
4236 loc
= cp_expr_loc_or_input_loc (t
);
4237 error_at (loc
, "parameter packs not expanded with %<...%>:");
4238 while (parameter_packs
)
4240 tree pack
= TREE_VALUE (parameter_packs
);
4241 tree name
= NULL_TREE
;
4243 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4244 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4245 name
= TYPE_NAME (pack
);
4246 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4247 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4248 else if (TREE_CODE (pack
) == CALL_EXPR
)
4249 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4251 name
= DECL_NAME (pack
);
4254 inform (loc
, " %qD", name
);
4256 inform (loc
, " %s", "<anonymous>");
4258 parameter_packs
= TREE_CHAIN (parameter_packs
);
4267 /* Expand any parameter packs that occur in the template arguments in
4270 expand_template_argument_pack (tree args
)
4272 if (args
== error_mark_node
)
4273 return error_mark_node
;
4275 tree result_args
= NULL_TREE
;
4276 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4277 int num_result_args
= -1;
4278 int non_default_args_count
= -1;
4280 /* First, determine if we need to expand anything, and the number of
4281 slots we'll need. */
4282 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4284 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4285 if (arg
== NULL_TREE
)
4287 if (ARGUMENT_PACK_P (arg
))
4289 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4290 if (num_result_args
< 0)
4291 num_result_args
= in_arg
+ num_packed
;
4293 num_result_args
+= num_packed
;
4297 if (num_result_args
>= 0)
4302 /* If no expansion is necessary, we're done. */
4303 if (num_result_args
< 0)
4306 /* Expand arguments. */
4307 result_args
= make_tree_vec (num_result_args
);
4308 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4309 non_default_args_count
=
4310 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4311 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4313 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4314 if (ARGUMENT_PACK_P (arg
))
4316 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4317 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4318 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4319 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4320 if (non_default_args_count
> 0)
4321 non_default_args_count
+= num_packed
- 1;
4325 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4329 if (non_default_args_count
>= 0)
4330 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4334 /* Checks if DECL shadows a template parameter.
4336 [temp.local]: A template-parameter shall not be redeclared within its
4337 scope (including nested scopes).
4339 Emits an error and returns TRUE if the DECL shadows a parameter,
4340 returns FALSE otherwise. */
4343 check_template_shadow (tree decl
)
4347 /* If we're not in a template, we can't possibly shadow a template
4349 if (!current_template_parms
)
4352 /* Figure out what we're shadowing. */
4353 decl
= OVL_FIRST (decl
);
4354 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4356 /* If there's no previous binding for this name, we're not shadowing
4357 anything, let alone a template parameter. */
4361 /* If we're not shadowing a template parameter, we're done. Note
4362 that OLDDECL might be an OVERLOAD (or perhaps even an
4363 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4365 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4368 /* We check for decl != olddecl to avoid bogus errors for using a
4369 name inside a class. We check TPFI to avoid duplicate errors for
4370 inline member templates. */
4372 || (DECL_TEMPLATE_PARM_P (decl
)
4373 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4376 /* Don't complain about the injected class name, as we've already
4377 complained about the class itself. */
4378 if (DECL_SELF_REFERENCE_P (decl
))
4381 if (DECL_TEMPLATE_PARM_P (decl
))
4382 error ("declaration of template parameter %q+D shadows "
4383 "template parameter", decl
);
4385 error ("declaration of %q+#D shadows template parameter", decl
);
4386 inform (DECL_SOURCE_LOCATION (olddecl
),
4387 "template parameter %qD declared here", olddecl
);
4391 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4392 ORIG_LEVEL, DECL, and TYPE. */
4395 build_template_parm_index (int index
,
4401 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4402 TEMPLATE_PARM_IDX (t
) = index
;
4403 TEMPLATE_PARM_LEVEL (t
) = level
;
4404 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4405 TEMPLATE_PARM_DECL (t
) = decl
;
4406 TREE_TYPE (t
) = type
;
4407 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4408 TREE_READONLY (t
) = TREE_READONLY (decl
);
4413 /* Find the canonical type parameter for the given template type
4414 parameter. Returns the canonical type parameter, which may be TYPE
4415 if no such parameter existed. */
4418 canonical_type_parameter (tree type
)
4420 int idx
= TEMPLATE_TYPE_IDX (type
);
4422 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4424 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4425 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4427 for (tree list
= (*canonical_template_parms
)[idx
];
4428 list
; list
= TREE_CHAIN (list
))
4429 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4430 return TREE_VALUE (list
);
4432 (*canonical_template_parms
)[idx
]
4433 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4437 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4438 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4439 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4440 new one is created. */
4443 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4444 tsubst_flags_t complain
)
4446 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4447 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4448 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4449 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4451 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4453 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4454 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4456 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4457 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4458 DECL_ARTIFICIAL (decl
) = 1;
4459 SET_DECL_TEMPLATE_PARM_P (decl
);
4461 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4462 TEMPLATE_PARM_LEVEL (index
) - levels
,
4463 TEMPLATE_PARM_ORIG_LEVEL (index
),
4465 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4466 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4467 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4469 /* Template template parameters need this. */
4471 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4473 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4474 TYPE_DECL
, DECL_NAME (decl
), type
);
4475 DECL_TEMPLATE_RESULT (decl
) = inner
;
4476 DECL_ARTIFICIAL (inner
) = true;
4477 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4478 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4481 /* Attach the TPI to the decl. */
4482 if (TREE_CODE (inner
) == TYPE_DECL
)
4483 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4485 DECL_INITIAL (decl
) = tpi
;
4488 return TEMPLATE_PARM_DESCENDANTS (index
);
4491 /* Process information from new template parameter PARM and append it
4492 to the LIST being built. This new parameter is a non-type
4493 parameter iff IS_NON_TYPE is true. This new parameter is a
4494 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4498 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4499 bool is_non_type
, bool is_parameter_pack
)
4501 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4502 tree prev
= NULL_TREE
;
4507 prev
= tree_last (list
);
4509 tree p
= TREE_VALUE (prev
);
4510 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4511 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4512 else if (TREE_CODE (p
) == PARM_DECL
)
4513 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4518 tree decl
= NULL_TREE
;
4519 tree defval
= TREE_PURPOSE (parm
);
4520 tree constr
= TREE_TYPE (parm
);
4524 parm
= TREE_VALUE (parm
);
4526 SET_DECL_TEMPLATE_PARM_P (parm
);
4528 if (TREE_TYPE (parm
) != error_mark_node
)
4532 The top-level cv-qualifiers on the template-parameter are
4533 ignored when determining its type. */
4534 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4535 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4536 TREE_TYPE (parm
) = error_mark_node
;
4537 else if (uses_parameter_packs (TREE_TYPE (parm
))
4538 && !is_parameter_pack
4539 /* If we're in a nested template parameter list, the template
4540 template parameter could be a parameter pack. */
4541 && processing_template_parmlist
== 1)
4543 /* This template parameter is not a parameter pack, but it
4544 should be. Complain about "bare" parameter packs. */
4545 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4547 /* Recover by calling this a parameter pack. */
4548 is_parameter_pack
= true;
4552 /* A template parameter is not modifiable. */
4553 TREE_CONSTANT (parm
) = 1;
4554 TREE_READONLY (parm
) = 1;
4555 decl
= build_decl (parm_loc
,
4556 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4557 TREE_CONSTANT (decl
) = 1;
4558 TREE_READONLY (decl
) = 1;
4559 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4560 = build_template_parm_index (idx
, processing_template_decl
,
4561 processing_template_decl
,
4562 decl
, TREE_TYPE (parm
));
4564 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4565 = is_parameter_pack
;
4570 parm
= TREE_VALUE (TREE_VALUE (parm
));
4572 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4574 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4575 /* This is for distinguishing between real templates and template
4576 template parameters */
4577 TREE_TYPE (parm
) = t
;
4578 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4583 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4584 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4585 decl
= build_decl (parm_loc
,
4586 TYPE_DECL
, parm
, t
);
4589 TYPE_NAME (t
) = decl
;
4590 TYPE_STUB_DECL (t
) = decl
;
4592 TEMPLATE_TYPE_PARM_INDEX (t
)
4593 = build_template_parm_index (idx
, processing_template_decl
,
4594 processing_template_decl
,
4595 decl
, TREE_TYPE (parm
));
4596 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4597 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4598 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4600 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4602 DECL_ARTIFICIAL (decl
) = 1;
4603 SET_DECL_TEMPLATE_PARM_P (decl
);
4605 /* Build requirements for the type/template parameter.
4606 This must be done after SET_DECL_TEMPLATE_PARM_P or
4607 process_template_parm could fail. */
4608 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4610 decl
= pushdecl (decl
);
4614 /* Build the parameter node linking the parameter declaration,
4615 its default argument (if any), and its constraints (if any). */
4616 parm
= build_tree_list (defval
, parm
);
4617 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4620 TREE_CHAIN (prev
) = parm
;
4627 /* The end of a template parameter list has been reached. Process the
4628 tree list into a parameter vector, converting each parameter into a more
4629 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4633 end_template_parm_list (tree parms
)
4635 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4637 /* Pop the dummy parameter level and add the real one. We do not
4638 morph the dummy parameter in place, as it might have been
4639 captured by a (nested) template-template-parm. */
4640 current_template_parms
= TREE_CHAIN (current_template_parms
);
4642 current_template_parms
4643 = tree_cons (size_int (processing_template_decl
),
4644 saved_parmlist
, current_template_parms
);
4646 for (unsigned ix
= 0; parms
; ix
++)
4649 parms
= TREE_CHAIN (parms
);
4650 TREE_CHAIN (parm
) = NULL_TREE
;
4652 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4655 --processing_template_parmlist
;
4657 return saved_parmlist
;
4660 // Explicitly indicate the end of the template parameter list. We assume
4661 // that the current template parameters have been constructed and/or
4662 // managed explicitly, as when creating new template template parameters
4663 // from a shorthand constraint.
4665 end_template_parm_list ()
4667 --processing_template_parmlist
;
4670 /* end_template_decl is called after a template declaration is seen. */
4673 end_template_decl (void)
4675 reset_specialization ();
4677 if (! processing_template_decl
)
4680 /* This matches the pushlevel in begin_template_parm_list. */
4683 --processing_template_decl
;
4684 current_template_parms
= TREE_CHAIN (current_template_parms
);
4687 /* Takes a TREE_LIST representing a template parameter and convert it
4688 into an argument suitable to be passed to the type substitution
4689 functions. Note that If the TREE_LIST contains an error_mark
4690 node, the returned argument is error_mark_node. */
4693 template_parm_to_arg (tree t
)
4697 || TREE_CODE (t
) != TREE_LIST
)
4700 if (error_operand_p (TREE_VALUE (t
)))
4701 return error_mark_node
;
4705 if (TREE_CODE (t
) == TYPE_DECL
4706 || TREE_CODE (t
) == TEMPLATE_DECL
)
4710 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4712 /* Turn this argument into a TYPE_ARGUMENT_PACK
4713 with a single element, which expands T. */
4714 tree vec
= make_tree_vec (1);
4716 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4718 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4720 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4721 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4726 t
= DECL_INITIAL (t
);
4728 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4730 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4731 with a single element, which expands T. */
4732 tree vec
= make_tree_vec (1);
4734 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4736 t
= convert_from_reference (t
);
4737 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4739 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4740 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4743 t
= convert_from_reference (t
);
4748 /* Given a single level of template parameters (a TREE_VEC), return it
4749 as a set of template arguments. */
4752 template_parms_level_to_args (tree parms
)
4754 tree a
= copy_node (parms
);
4755 TREE_TYPE (a
) = NULL_TREE
;
4756 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4757 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4760 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4765 /* Given a set of template parameters, return them as a set of template
4766 arguments. The template parameters are represented as a TREE_VEC, in
4767 the form documented in cp-tree.h for template arguments. */
4770 template_parms_to_args (tree parms
)
4773 tree args
= NULL_TREE
;
4774 int length
= TMPL_PARMS_DEPTH (parms
);
4777 /* If there is only one level of template parameters, we do not
4778 create a TREE_VEC of TREE_VECs. Instead, we return a single
4779 TREE_VEC containing the arguments. */
4781 args
= make_tree_vec (length
);
4783 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4785 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4788 TREE_VEC_ELT (args
, --l
) = a
;
4796 /* Within the declaration of a template, return the currently active
4797 template parameters as an argument TREE_VEC. */
4800 current_template_args (void)
4802 return template_parms_to_args (current_template_parms
);
4805 /* Return the fully generic arguments for of TMPL, i.e. what
4806 current_template_args would be while parsing it. */
4809 generic_targs_for (tree tmpl
)
4811 if (tmpl
== NULL_TREE
)
4813 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4814 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4815 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4816 template parameter, it has no TEMPLATE_INFO; for a partial
4817 specialization, it has the arguments for the primary template, and we
4818 want the arguments for the partial specialization. */;
4819 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4820 if (tree ti
= get_template_info (result
))
4821 return TI_ARGS (ti
);
4822 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4825 /* Update the declared TYPE by doing any lookups which were thought to be
4826 dependent, but are not now that we know the SCOPE of the declarator. */
4829 maybe_update_decl_type (tree orig_type
, tree scope
)
4831 tree type
= orig_type
;
4833 if (type
== NULL_TREE
)
4836 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4837 type
= TREE_TYPE (type
);
4839 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4840 && dependent_type_p (type
)
4841 /* Don't bother building up the args in this case. */
4842 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4844 /* tsubst in the args corresponding to the template parameters,
4845 including auto if present. Most things will be unchanged, but
4846 make_typename_type and tsubst_qualified_id will resolve
4847 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4848 tree args
= current_template_args ();
4849 tree auto_node
= type_uses_auto (type
);
4853 tree auto_vec
= make_tree_vec (1);
4854 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4855 args
= add_to_template_args (args
, auto_vec
);
4857 pushed
= push_scope (scope
);
4858 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4863 if (type
== error_mark_node
)
4866 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4868 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4871 type
= TYPE_NAME (type
);
4876 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4877 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4878 the new template is a member template. */
4881 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4883 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4884 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4885 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4886 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4887 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4888 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4889 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4890 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4895 struct template_parm_data
4897 /* The level of the template parameters we are currently
4901 /* The index of the specialization argument we are currently
4905 /* An array whose size is the number of template parameters. The
4906 elements are nonzero if the parameter has been used in any one
4907 of the arguments processed so far. */
4910 /* An array whose size is the number of template arguments. The
4911 elements are nonzero if the argument makes use of template
4912 parameters of this level. */
4913 int* arg_uses_template_parms
;
4916 /* Subroutine of push_template_decl used to see if each template
4917 parameter in a partial specialization is used in the explicit
4918 argument list. If T is of the LEVEL given in DATA (which is
4919 treated as a template_parm_data*), then DATA->PARMS is marked
4923 mark_template_parm (tree t
, void* data
)
4927 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4929 template_parm_level_and_index (t
, &level
, &idx
);
4931 if (level
== tpd
->level
)
4933 tpd
->parms
[idx
] = 1;
4934 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4937 /* In C++17 the type of a non-type argument is a deduced context. */
4938 if (cxx_dialect
>= cxx17
4939 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4940 for_each_template_parm (TREE_TYPE (t
),
4941 &mark_template_parm
,
4944 /*include_nondeduced_p=*/false);
4946 /* Return zero so that for_each_template_parm will continue the
4947 traversal of the tree; we want to mark *every* template parm. */
4951 /* Process the partial specialization DECL. */
4954 process_partial_specialization (tree decl
)
4956 tree type
= TREE_TYPE (decl
);
4957 tree tinfo
= get_template_info (decl
);
4958 tree maintmpl
= TI_TEMPLATE (tinfo
);
4959 tree specargs
= TI_ARGS (tinfo
);
4960 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4961 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4964 int nargs
= TREE_VEC_LENGTH (inner_args
);
4967 bool did_error_intro
= false;
4968 struct template_parm_data tpd
;
4969 struct template_parm_data tpd2
;
4971 gcc_assert (current_template_parms
);
4973 /* A concept cannot be specialized. */
4974 if (flag_concepts
&& variable_concept_p (maintmpl
))
4976 error ("specialization of variable concept %q#D", maintmpl
);
4977 return error_mark_node
;
4980 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4981 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4983 /* We check that each of the template parameters given in the
4984 partial specialization is used in the argument list to the
4985 specialization. For example:
4987 template <class T> struct S;
4988 template <class T> struct S<T*>;
4990 The second declaration is OK because `T*' uses the template
4991 parameter T, whereas
4993 template <class T> struct S<int>;
4995 is no good. Even trickier is:
5006 The S2<T> declaration is actually invalid; it is a
5007 full-specialization. Of course,
5010 struct S2<T (*)(U)>;
5012 or some such would have been OK. */
5013 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5014 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5015 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5017 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5018 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5019 for (i
= 0; i
< nargs
; ++i
)
5021 tpd
.current_arg
= i
;
5022 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5023 &mark_template_parm
,
5026 /*include_nondeduced_p=*/false);
5028 for (i
= 0; i
< ntparms
; ++i
)
5029 if (tpd
.parms
[i
] == 0)
5031 /* One of the template parms was not used in a deduced context in the
5033 if (!did_error_intro
)
5035 error ("template parameters not deducible in "
5036 "partial specialization:");
5037 did_error_intro
= true;
5040 inform (input_location
, " %qD",
5041 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5044 if (did_error_intro
)
5045 return error_mark_node
;
5047 /* [temp.class.spec]
5049 The argument list of the specialization shall not be identical to
5050 the implicit argument list of the primary template. */
5052 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5053 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5055 || !strictly_subsumes (current_template_constraints (),
5056 inner_args
, maintmpl
)))
5059 error ("partial specialization %q+D does not specialize "
5060 "any template arguments; to define the primary template, "
5061 "remove the template argument list", decl
);
5063 error ("partial specialization %q+D does not specialize any "
5064 "template arguments and is not more constrained than "
5065 "the primary template; to define the primary template, "
5066 "remove the template argument list", decl
);
5067 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5070 /* A partial specialization that replaces multiple parameters of the
5071 primary template with a pack expansion is less specialized for those
5073 if (nargs
< DECL_NTPARMS (maintmpl
))
5075 error ("partial specialization is not more specialized than the "
5076 "primary template because it replaces multiple parameters "
5077 "with a pack expansion");
5078 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5079 /* Avoid crash in process_partial_specialization. */
5083 else if (nargs
> DECL_NTPARMS (maintmpl
))
5085 error ("too many arguments for partial specialization %qT", type
);
5086 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5087 /* Avoid crash below. */
5091 /* If we aren't in a dependent class, we can actually try deduction. */
5092 else if (tpd
.level
== 1
5093 /* FIXME we should be able to handle a partial specialization of a
5094 partial instantiation, but currently we can't (c++/41727). */
5095 && TMPL_ARGS_DEPTH (specargs
) == 1
5096 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5098 auto_diagnostic_group d
;
5099 if (permerror (input_location
, "partial specialization %qD is not "
5100 "more specialized than", decl
))
5101 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5105 /* [temp.class.spec]
5107 A partially specialized non-type argument expression shall not
5108 involve template parameters of the partial specialization except
5109 when the argument expression is a simple identifier.
5111 The type of a template parameter corresponding to a specialized
5112 non-type argument shall not be dependent on a parameter of the
5115 Also, we verify that pack expansions only occur at the
5116 end of the argument list. */
5118 for (i
= 0; i
< nargs
; ++i
)
5120 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5121 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5122 tree packed_args
= NULL_TREE
;
5125 if (ARGUMENT_PACK_P (arg
))
5127 /* Extract the arguments from the argument pack. We'll be
5128 iterating over these in the following loop. */
5129 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5130 len
= TREE_VEC_LENGTH (packed_args
);
5133 for (j
= 0; j
< len
; j
++)
5136 /* Get the Jth argument in the parameter pack. */
5137 arg
= TREE_VEC_ELT (packed_args
, j
);
5139 if (PACK_EXPANSION_P (arg
))
5141 /* Pack expansions must come at the end of the
5143 if ((packed_args
&& j
< len
- 1)
5144 || (!packed_args
&& i
< nargs
- 1))
5146 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5147 error ("parameter pack argument %qE must be at the "
5148 "end of the template argument list", arg
);
5150 error ("parameter pack argument %qT must be at the "
5151 "end of the template argument list", arg
);
5155 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5156 /* We only care about the pattern. */
5157 arg
= PACK_EXPANSION_PATTERN (arg
);
5159 if (/* These first two lines are the `non-type' bit. */
5161 && TREE_CODE (arg
) != TEMPLATE_DECL
5162 /* This next two lines are the `argument expression is not just a
5163 simple identifier' condition and also the `specialized
5164 non-type argument' bit. */
5165 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5166 && !((REFERENCE_REF_P (arg
)
5167 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5168 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5170 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5171 || (packed_args
&& uses_template_parms (arg
)))
5172 error_at (cp_expr_loc_or_input_loc (arg
),
5173 "template argument %qE involves template "
5174 "parameter(s)", arg
);
5177 /* Look at the corresponding template parameter,
5178 marking which template parameters its type depends
5180 tree type
= TREE_TYPE (parm
);
5184 /* We haven't yet initialized TPD2. Do so now. */
5185 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5186 /* The number of parameters here is the number in the
5187 main template, which, as checked in the assertion
5189 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5191 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5194 /* Mark the template parameters. But this time, we're
5195 looking for the template parameters of the main
5196 template, not in the specialization. */
5197 tpd2
.current_arg
= i
;
5198 tpd2
.arg_uses_template_parms
[i
] = 0;
5199 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5200 for_each_template_parm (type
,
5201 &mark_template_parm
,
5204 /*include_nondeduced_p=*/false);
5206 if (tpd2
.arg_uses_template_parms
[i
])
5208 /* The type depended on some template parameters.
5209 If they are fully specialized in the
5210 specialization, that's OK. */
5213 for (j
= 0; j
< nargs
; ++j
)
5214 if (tpd2
.parms
[j
] != 0
5215 && tpd
.arg_uses_template_parms
[j
])
5218 error_n (input_location
, count
,
5219 "type %qT of template argument %qE depends "
5220 "on a template parameter",
5221 "type %qT of template argument %qE depends "
5222 "on template parameters",
5231 /* We should only get here once. */
5232 if (TREE_CODE (decl
) == TYPE_DECL
)
5233 gcc_assert (!COMPLETE_TYPE_P (type
));
5235 // Build the template decl.
5236 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5237 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5238 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5239 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5240 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5242 /* Give template template parms a DECL_CONTEXT of the template
5243 for which they are a parameter. */
5244 for (i
= 0; i
< ntparms
; ++i
)
5246 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5247 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5248 DECL_CONTEXT (parm
) = tmpl
;
5252 /* We didn't register this in check_explicit_specialization so we could
5253 wait until the constraints were set. */
5254 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5256 associate_classtype_constraints (type
);
5258 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5259 = tree_cons (specargs
, tmpl
,
5260 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5261 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5263 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5264 inst
= TREE_CHAIN (inst
))
5266 tree instance
= TREE_VALUE (inst
);
5267 if (TYPE_P (instance
)
5268 ? (COMPLETE_TYPE_P (instance
)
5269 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5270 : DECL_TEMPLATE_INSTANTIATION (instance
))
5272 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5273 tree inst_decl
= (DECL_P (instance
)
5274 ? instance
: TYPE_NAME (instance
));
5277 else if (spec
== error_mark_node
)
5278 permerror (input_location
,
5279 "declaration of %qD ambiguates earlier template "
5280 "instantiation for %qD", decl
, inst_decl
);
5281 else if (TREE_VALUE (spec
) == tmpl
)
5282 permerror (input_location
,
5283 "partial specialization of %qD after instantiation "
5284 "of %qD", decl
, inst_decl
);
5291 /* PARM is a template parameter of some form; return the corresponding
5292 TEMPLATE_PARM_INDEX. */
5295 get_template_parm_index (tree parm
)
5297 if (TREE_CODE (parm
) == PARM_DECL
5298 || TREE_CODE (parm
) == CONST_DECL
)
5299 parm
= DECL_INITIAL (parm
);
5300 else if (TREE_CODE (parm
) == TYPE_DECL
5301 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5302 parm
= TREE_TYPE (parm
);
5303 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5304 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5305 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5306 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5307 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5311 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5312 parameter packs used by the template parameter PARM. */
5315 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5317 /* A type parm can't refer to another parm. */
5318 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5320 else if (TREE_CODE (parm
) == PARM_DECL
)
5322 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5327 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5329 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5330 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5332 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5333 if (template_parameter_pack_p (p
))
5334 /* Any packs in the type are expanded by this parameter. */;
5336 fixed_parameter_pack_p_1 (p
, ppd
);
5340 /* PARM is a template parameter pack. Return any parameter packs used in
5341 its type or the type of any of its template parameters. If there are
5342 any such packs, it will be instantiated into a fixed template parameter
5343 list by partial instantiation rather than be fully deduced. */
5346 fixed_parameter_pack_p (tree parm
)
5348 /* This can only be true in a member template. */
5349 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5351 /* This can only be true for a parameter pack. */
5352 if (!template_parameter_pack_p (parm
))
5354 /* A type parm can't refer to another parm. */
5355 if (TREE_CODE (parm
) == TYPE_DECL
)
5358 tree parameter_packs
= NULL_TREE
;
5359 struct find_parameter_pack_data ppd
;
5360 ppd
.parameter_packs
= ¶meter_packs
;
5361 ppd
.visited
= new hash_set
<tree
>;
5362 ppd
.type_pack_expansion_p
= false;
5364 fixed_parameter_pack_p_1 (parm
, &ppd
);
5367 return parameter_packs
;
5370 /* Check that a template declaration's use of default arguments and
5371 parameter packs is not invalid. Here, PARMS are the template
5372 parameters. IS_PRIMARY is true if DECL is the thing declared by
5373 a primary template. IS_PARTIAL is true if DECL is a partial
5376 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5377 function template declaration or a friend class template
5378 declaration. In the function case, 1 indicates a declaration, 2
5379 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5380 emitted for extraneous default arguments.
5382 Returns TRUE if there were no errors found, FALSE otherwise. */
5385 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5386 bool is_partial
, int is_friend_decl
)
5389 int last_level_to_check
;
5391 bool no_errors
= true;
5395 A default template-argument shall not be specified in a
5396 function template declaration or a function template definition, nor
5397 in the template-parameter-list of the definition of a member of a
5400 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5401 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5402 /* You can't have a function template declaration in a local
5403 scope, nor you can you define a member of a class template in a
5407 if ((TREE_CODE (decl
) == TYPE_DECL
5409 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5410 || (TREE_CODE (decl
) == FUNCTION_DECL
5411 && LAMBDA_FUNCTION_P (decl
)))
5412 /* A lambda doesn't have an explicit declaration; don't complain
5413 about the parms of the enclosing class. */
5416 if (current_class_type
5417 && !TYPE_BEING_DEFINED (current_class_type
)
5418 && DECL_LANG_SPECIFIC (decl
)
5419 && DECL_DECLARES_FUNCTION_P (decl
)
5420 /* If this is either a friend defined in the scope of the class
5421 or a member function. */
5422 && (DECL_FUNCTION_MEMBER_P (decl
)
5423 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5424 : DECL_FRIEND_CONTEXT (decl
)
5425 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5427 /* And, if it was a member function, it really was defined in
5428 the scope of the class. */
5429 && (!DECL_FUNCTION_MEMBER_P (decl
)
5430 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5431 /* We already checked these parameters when the template was
5432 declared, so there's no need to do it again now. This function
5433 was defined in class scope, but we're processing its body now
5434 that the class is complete. */
5437 /* Core issue 226 (C++0x only): the following only applies to class
5440 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5444 If a template-parameter has a default template-argument, all
5445 subsequent template-parameters shall have a default
5446 template-argument supplied. */
5447 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5449 tree inner_parms
= TREE_VALUE (parm_level
);
5450 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5451 int seen_def_arg_p
= 0;
5454 for (i
= 0; i
< ntparms
; ++i
)
5456 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5458 if (parm
== error_mark_node
)
5461 if (TREE_PURPOSE (parm
))
5463 else if (seen_def_arg_p
5464 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5466 error ("no default argument for %qD", TREE_VALUE (parm
));
5467 /* For better subsequent error-recovery, we indicate that
5468 there should have been a default argument. */
5469 TREE_PURPOSE (parm
) = error_mark_node
;
5472 else if (!is_partial
5474 /* Don't complain about an enclosing partial
5476 && parm_level
== parms
5477 && TREE_CODE (decl
) == TYPE_DECL
5479 && template_parameter_pack_p (TREE_VALUE (parm
))
5480 /* A fixed parameter pack will be partially
5481 instantiated into a fixed length list. */
5482 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5484 /* A primary class template can only have one
5485 parameter pack, at the end of the template
5488 error ("parameter pack %q+D must be at the end of the"
5489 " template parameter list", TREE_VALUE (parm
));
5491 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5499 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5503 /* For an ordinary class template, default template arguments are
5504 allowed at the innermost level, e.g.:
5505 template <class T = int>
5507 but, in a partial specialization, they're not allowed even
5508 there, as we have in [temp.class.spec]:
5510 The template parameter list of a specialization shall not
5511 contain default template argument values.
5513 So, for a partial specialization, or for a function template
5514 (in C++98/C++03), we look at all of them. */
5517 /* But, for a primary class template that is not a partial
5518 specialization we look at all template parameters except the
5520 parms
= TREE_CHAIN (parms
);
5522 /* Figure out what error message to issue. */
5523 if (is_friend_decl
== 2)
5524 msg
= G_("default template arguments may not be used in function template "
5525 "friend re-declaration");
5526 else if (is_friend_decl
)
5527 msg
= G_("default template arguments may not be used in template "
5528 "friend declarations");
5529 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5530 msg
= G_("default template arguments may not be used in function templates "
5531 "without %<-std=c++11%> or %<-std=gnu++11%>");
5532 else if (is_partial
)
5533 msg
= G_("default template arguments may not be used in "
5534 "partial specializations");
5535 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5536 msg
= G_("default argument for template parameter for class enclosing %qD");
5538 /* Per [temp.param]/9, "A default template-argument shall not be
5539 specified in the template-parameter-lists of the definition of
5540 a member of a class template that appears outside of the member's
5541 class.", thus if we aren't handling a member of a class template
5542 there is no need to examine the parameters. */
5545 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5546 /* If we're inside a class definition, there's no need to
5547 examine the parameters to the class itself. On the one
5548 hand, they will be checked when the class is defined, and,
5549 on the other, default arguments are valid in things like:
5550 template <class T = double>
5551 struct S { template <class U> void f(U); };
5552 Here the default argument for `S' has no bearing on the
5553 declaration of `f'. */
5554 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5556 /* Check everything. */
5557 last_level_to_check
= 0;
5559 for (parm_level
= parms
;
5560 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5561 parm_level
= TREE_CHAIN (parm_level
))
5563 tree inner_parms
= TREE_VALUE (parm_level
);
5567 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5568 for (i
= 0; i
< ntparms
; ++i
)
5570 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5573 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5578 if (is_friend_decl
== 2)
5585 /* Clear out the default argument so that we are not
5587 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5591 /* At this point, if we're still interested in issuing messages,
5592 they must apply to classes surrounding the object declared. */
5594 msg
= G_("default argument for template parameter for class "
5601 /* Worker for push_template_decl_real, called via
5602 for_each_template_parm. DATA is really an int, indicating the
5603 level of the parameters we are interested in. If T is a template
5604 parameter of that level, return nonzero. */
5607 template_parm_this_level_p (tree t
, void* data
)
5609 int this_level
= *(int *)data
;
5612 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5613 level
= TEMPLATE_PARM_LEVEL (t
);
5615 level
= TEMPLATE_TYPE_LEVEL (t
);
5616 return level
== this_level
;
5619 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5620 DATA is really an int, indicating the innermost outer level of parameters.
5621 If T is a template parameter of that level or further out, return
5625 template_parm_outer_level (tree t
, void *data
)
5627 int this_level
= *(int *)data
;
5630 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5631 level
= TEMPLATE_PARM_LEVEL (t
);
5633 level
= TEMPLATE_TYPE_LEVEL (t
);
5634 return level
<= this_level
;
5637 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5638 parameters given by current_template_args, or reuses a
5639 previously existing one, if appropriate. Returns the DECL, or an
5640 equivalent one, if it is replaced via a call to duplicate_decls.
5642 If IS_FRIEND is true, DECL is a friend declaration. */
5645 push_template_decl_real (tree decl
, bool is_friend
)
5653 int new_template_p
= 0;
5654 /* True if the template is a member template, in the sense of
5656 bool member_template_p
= false;
5658 if (decl
== error_mark_node
|| !current_template_parms
)
5659 return error_mark_node
;
5661 /* See if this is a partial specialization. */
5662 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5663 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5664 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5666 && DECL_LANG_SPECIFIC (decl
)
5667 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5668 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5670 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5674 /* For a friend, we want the context of the friend, not
5675 the type of which it is a friend. */
5676 ctx
= CP_DECL_CONTEXT (decl
);
5677 else if (CP_DECL_CONTEXT (decl
)
5678 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5679 /* In the case of a virtual function, we want the class in which
5681 ctx
= CP_DECL_CONTEXT (decl
);
5683 /* Otherwise, if we're currently defining some class, the DECL
5684 is assumed to be a member of the class. */
5685 ctx
= current_scope ();
5687 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5690 if (!DECL_CONTEXT (decl
))
5691 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5693 /* See if this is a primary template. */
5694 if (is_friend
&& ctx
5695 && uses_template_parms_level (ctx
, processing_template_decl
))
5696 /* A friend template that specifies a class context, i.e.
5697 template <typename T> friend void A<T>::f();
5700 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5703 is_primary
= template_parm_scope_p ();
5707 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5709 if (DECL_CLASS_SCOPE_P (decl
))
5710 member_template_p
= true;
5711 if (TREE_CODE (decl
) == TYPE_DECL
5712 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5714 error ("template class without a name");
5715 return error_mark_node
;
5717 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5719 if (member_template_p
)
5721 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5722 error ("member template %qD may not have virt-specifiers", decl
);
5724 if (DECL_DESTRUCTOR_P (decl
))
5728 A destructor shall not be a member template. */
5729 error_at (DECL_SOURCE_LOCATION (decl
),
5730 "destructor %qD declared as member template", decl
);
5731 return error_mark_node
;
5733 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5734 && (!prototype_p (TREE_TYPE (decl
))
5735 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5736 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5737 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5738 == void_list_node
)))
5740 /* [basic.stc.dynamic.allocation]
5742 An allocation function can be a function
5743 template. ... Template allocation functions shall
5744 have two or more parameters. */
5745 error ("invalid template declaration of %qD", decl
);
5746 return error_mark_node
;
5749 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5750 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5752 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5753 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5754 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5756 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5757 if (TREE_CODE (t
) == TYPE_DECL
)
5759 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5760 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5763 else if (TREE_CODE (decl
) == TYPE_DECL
5764 && TYPE_DECL_ALIAS_P (decl
))
5765 /* alias-declaration */
5766 gcc_assert (!DECL_ARTIFICIAL (decl
));
5767 else if (VAR_P (decl
))
5768 /* C++14 variable template. */;
5769 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5770 /* C++20 concept definitions. */;
5773 error ("template declaration of %q#D", decl
);
5774 return error_mark_node
;
5778 /* Check to see that the rules regarding the use of default
5779 arguments are not being violated. We check args for a friend
5780 functions when we know whether it's a definition, introducing
5781 declaration or re-declaration. */
5782 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5783 check_default_tmpl_args (decl
, current_template_parms
,
5784 is_primary
, is_partial
, is_friend
);
5786 /* Ensure that there are no parameter packs in the type of this
5787 declaration that have not been expanded. */
5788 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5790 /* Check each of the arguments individually to see if there are
5791 any bare parameter packs. */
5792 tree type
= TREE_TYPE (decl
);
5793 tree arg
= DECL_ARGUMENTS (decl
);
5794 tree argtype
= TYPE_ARG_TYPES (type
);
5796 while (arg
&& argtype
)
5798 if (!DECL_PACK_P (arg
)
5799 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5801 /* This is a PARM_DECL that contains unexpanded parameter
5802 packs. We have already complained about this in the
5803 check_for_bare_parameter_packs call, so just replace
5804 these types with ERROR_MARK_NODE. */
5805 TREE_TYPE (arg
) = error_mark_node
;
5806 TREE_VALUE (argtype
) = error_mark_node
;
5809 arg
= DECL_CHAIN (arg
);
5810 argtype
= TREE_CHAIN (argtype
);
5813 /* Check for bare parameter packs in the return type and the
5814 exception specifiers. */
5815 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5816 /* Errors were already issued, set return type to int
5817 as the frontend doesn't expect error_mark_node as
5819 TREE_TYPE (type
) = integer_type_node
;
5820 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5821 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5823 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5824 ? DECL_ORIGINAL_TYPE (decl
)
5825 : TREE_TYPE (decl
)))
5827 TREE_TYPE (decl
) = error_mark_node
;
5828 return error_mark_node
;
5832 return process_partial_specialization (decl
);
5834 args
= current_template_args ();
5837 || TREE_CODE (ctx
) == FUNCTION_DECL
5838 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5839 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5840 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5842 if (DECL_LANG_SPECIFIC (decl
)
5843 && DECL_TEMPLATE_INFO (decl
)
5844 && DECL_TI_TEMPLATE (decl
))
5845 tmpl
= DECL_TI_TEMPLATE (decl
);
5846 /* If DECL is a TYPE_DECL for a class-template, then there won't
5847 be DECL_LANG_SPECIFIC. The information equivalent to
5848 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5849 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5850 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5851 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5853 /* Since a template declaration already existed for this
5854 class-type, we must be redeclaring it here. Make sure
5855 that the redeclaration is valid. */
5856 redeclare_class_template (TREE_TYPE (decl
),
5857 current_template_parms
,
5858 current_template_constraints ());
5859 /* We don't need to create a new TEMPLATE_DECL; just use the
5860 one we already had. */
5861 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5865 tmpl
= build_template_decl (decl
, current_template_parms
,
5869 if (DECL_LANG_SPECIFIC (decl
)
5870 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5872 /* A specialization of a member template of a template
5874 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5875 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5876 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5882 tree a
, t
, current
, parms
;
5884 tree tinfo
= get_template_info (decl
);
5888 error ("template definition of non-template %q#D", decl
);
5889 return error_mark_node
;
5892 tmpl
= TI_TEMPLATE (tinfo
);
5894 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5895 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5896 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5897 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5901 /* The declaration is a specialization of a member
5902 template, declared outside the class. Therefore, the
5903 innermost template arguments will be NULL, so we
5904 replace them with the arguments determined by the
5905 earlier call to check_explicit_specialization. */
5906 args
= DECL_TI_ARGS (decl
);
5909 = build_template_decl (decl
, current_template_parms
,
5911 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5912 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5913 DECL_TEMPLATE_INFO (new_tmpl
)
5914 = build_template_info (tmpl
, args
);
5916 register_specialization (new_tmpl
,
5917 most_general_template (tmpl
),
5923 /* Make sure the template headers we got make sense. */
5925 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5926 i
= TMPL_PARMS_DEPTH (parms
);
5927 if (TMPL_ARGS_DEPTH (args
) != i
)
5929 error ("expected %d levels of template parms for %q#D, got %d",
5930 i
, decl
, TMPL_ARGS_DEPTH (args
));
5931 DECL_INTERFACE_KNOWN (decl
) = 1;
5932 return error_mark_node
;
5935 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5937 a
= TMPL_ARGS_LEVEL (args
, i
);
5938 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5940 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5942 if (current
== decl
)
5943 error ("got %d template parameters for %q#D",
5944 TREE_VEC_LENGTH (a
), decl
);
5946 error ("got %d template parameters for %q#T",
5947 TREE_VEC_LENGTH (a
), current
);
5948 error (" but %d required", TREE_VEC_LENGTH (t
));
5949 /* Avoid crash in import_export_decl. */
5950 DECL_INTERFACE_KNOWN (decl
) = 1;
5951 return error_mark_node
;
5954 if (current
== decl
)
5956 else if (current
== NULL_TREE
)
5957 /* Can happen in erroneous input. */
5960 current
= get_containing_scope (current
);
5963 /* Check that the parms are used in the appropriate qualifying scopes
5964 in the declarator. */
5965 if (!comp_template_args
5967 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5969 error ("template arguments to %qD do not match original "
5970 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5971 if (!uses_template_parms (TI_ARGS (tinfo
)))
5972 inform (input_location
, "use %<template<>%> for"
5973 " an explicit specialization");
5974 /* Avoid crash in import_export_decl. */
5975 DECL_INTERFACE_KNOWN (decl
) = 1;
5976 return error_mark_node
;
5980 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl
) == decl
);
5985 /* Push template declarations for global functions and types.
5986 Note that we do not try to push a global template friend
5987 declared in a template class; such a thing may well depend on
5988 the template parameters of the class and we'll push it when
5989 instantiating the befriending class. */
5991 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5993 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5994 if (tmpl
== error_mark_node
)
5995 return error_mark_node
;
5997 /* Hide template friend classes that haven't been declared yet. */
5998 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
6000 DECL_ANTICIPATED (tmpl
) = 1;
6001 DECL_FRIEND_P (tmpl
) = 1;
6006 /* The type may have been completed, or (erroneously) changed. */
6007 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6011 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6013 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6015 /* Give template template parms a DECL_CONTEXT of the template
6016 for which they are a parameter. */
6017 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6018 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6020 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6021 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6022 DECL_CONTEXT (parm
) = tmpl
;
6025 if (TREE_CODE (decl
) == TYPE_DECL
6026 && TYPE_DECL_ALIAS_P (decl
))
6029 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6031 /* ??? Why don't we do this here for all templates? */
6032 constr
= build_constraints (constr
, NULL_TREE
);
6033 set_constraints (decl
, constr
);
6035 if (complex_alias_template_p (tmpl
))
6036 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6040 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6041 back to its most general template. If TMPL is a specialization,
6042 ARGS may only have the innermost set of arguments. Add the missing
6043 argument levels if necessary. */
6044 if (DECL_TEMPLATE_INFO (tmpl
))
6045 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6047 info
= build_template_info (tmpl
, args
);
6049 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6050 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6054 retrofit_lang_decl (decl
);
6055 if (DECL_LANG_SPECIFIC (decl
))
6056 DECL_TEMPLATE_INFO (decl
) = info
;
6059 if (flag_implicit_templates
6061 && TREE_PUBLIC (decl
)
6062 && VAR_OR_FUNCTION_DECL_P (decl
))
6063 /* Set DECL_COMDAT on template instantiations; if we force
6064 them to be emitted by explicit instantiation,
6065 mark_needed will tell cgraph to do the right thing. */
6066 DECL_COMDAT (decl
) = true;
6068 return DECL_TEMPLATE_RESULT (tmpl
);
6072 push_template_decl (tree decl
)
6074 return push_template_decl_real (decl
, false);
6077 /* FN is an inheriting constructor that inherits from the constructor
6078 template INHERITED; turn FN into a constructor template with a matching
6082 add_inherited_template_parms (tree fn
, tree inherited
)
6085 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6086 inner_parms
= copy_node (inner_parms
);
6088 = tree_cons (size_int (processing_template_decl
+ 1),
6089 inner_parms
, current_template_parms
);
6090 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6091 tree args
= template_parms_to_args (parms
);
6092 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6093 DECL_ARTIFICIAL (tmpl
) = true;
6094 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6098 /* Called when a class template TYPE is redeclared with the indicated
6099 template PARMS, e.g.:
6101 template <class T> struct S;
6102 template <class T> struct S {}; */
6105 redeclare_class_template (tree type
, tree parms
, tree cons
)
6111 if (!TYPE_TEMPLATE_INFO (type
))
6113 error ("%qT is not a template type", type
);
6117 tmpl
= TYPE_TI_TEMPLATE (type
);
6118 if (!PRIMARY_TEMPLATE_P (tmpl
))
6119 /* The type is nested in some template class. Nothing to worry
6120 about here; there are no new template parameters for the nested
6126 error ("template specifiers not specified in declaration of %qD",
6131 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6132 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6134 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6136 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6137 "redeclared with %d template parameter",
6138 "redeclared with %d template parameters",
6139 TREE_VEC_LENGTH (parms
));
6140 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6141 "previous declaration %qD used %d template parameter",
6142 "previous declaration %qD used %d template parameters",
6143 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6147 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6154 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6155 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6158 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6159 if (error_operand_p (tmpl_parm
))
6162 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6163 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6164 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6166 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6168 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6169 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6170 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6171 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6172 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6173 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6174 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6175 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6176 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6178 auto_diagnostic_group d
;
6179 error ("template parameter %q+#D", tmpl_parm
);
6180 inform (input_location
, "redeclared here as %q#D", parm
);
6184 /* The parameters can be declared to introduce different
6186 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6187 tree p2
= TREE_VEC_ELT (parms
, i
);
6188 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6190 auto_diagnostic_group d
;
6191 error ("declaration of template parameter %q+#D with different "
6192 "constraints", parm
);
6193 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6194 "original declaration appeared here");
6198 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6200 /* We have in [temp.param]:
6202 A template-parameter may not be given default arguments
6203 by two different declarations in the same scope. */
6204 auto_diagnostic_group d
;
6205 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6206 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6207 "original definition appeared here");
6211 if (parm_default
!= NULL_TREE
)
6212 /* Update the previous template parameters (which are the ones
6213 that will really count) with the new default value. */
6214 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6215 else if (tmpl_default
!= NULL_TREE
)
6216 /* Update the new parameters, too; they'll be used as the
6217 parameters for any members. */
6218 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6220 /* Give each template template parm in this redeclaration a
6221 DECL_CONTEXT of the template for which they are a parameter. */
6222 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6224 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6225 DECL_CONTEXT (parm
) = tmpl
;
6228 if (TREE_CODE (parm
) == TYPE_DECL
)
6229 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6232 tree ci
= get_constraints (tmpl
);
6233 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6234 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6236 /* Two classes with different constraints declare different entities. */
6237 if (!cp_tree_equal (req1
, req2
))
6239 auto_diagnostic_group d
;
6240 error_at (input_location
, "redeclaration %q#D with different "
6241 "constraints", tmpl
);
6242 inform (DECL_SOURCE_LOCATION (tmpl
),
6243 "original declaration appeared here");
6250 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6251 to be used when the caller has already checked
6252 (processing_template_decl
6253 && !instantiation_dependent_expression_p (expr)
6254 && potential_constant_expression (expr))
6255 and cleared processing_template_decl. */
6258 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6260 return tsubst_copy_and_build (expr
,
6263 /*in_decl=*/NULL_TREE
,
6264 /*function_p=*/false,
6265 /*integral_constant_expression_p=*/true);
6268 /* Simplify EXPR if it is a non-dependent expression. Returns the
6269 (possibly simplified) expression. */
6272 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6274 if (expr
== NULL_TREE
)
6277 /* If we're in a template, but EXPR isn't value dependent, simplify
6278 it. We're supposed to treat:
6280 template <typename T> void f(T[1 + 1]);
6281 template <typename T> void f(T[2]);
6283 as two declarations of the same function, for example. */
6284 if (processing_template_decl
6285 && is_nondependent_constant_expression (expr
))
6287 processing_template_decl_sentinel s
;
6288 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6294 instantiate_non_dependent_expr (tree expr
)
6296 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6299 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6300 an uninstantiated expression. */
6303 instantiate_non_dependent_or_null (tree expr
)
6305 if (expr
== NULL_TREE
)
6307 if (processing_template_decl
)
6309 if (!is_nondependent_constant_expression (expr
))
6313 processing_template_decl_sentinel s
;
6314 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6320 /* True iff T is a specialization of a variable template. */
6323 variable_template_specialization_p (tree t
)
6325 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6327 tree tmpl
= DECL_TI_TEMPLATE (t
);
6328 return variable_template_p (tmpl
);
6331 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6332 template declaration, or a TYPE_DECL for an alias declaration. */
6335 alias_type_or_template_p (tree t
)
6339 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6342 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6343 || DECL_ALIAS_TEMPLATE_P (t
));
6346 /* If T is a specialization of an alias template, return it; otherwise return
6347 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6350 alias_template_specialization_p (const_tree t
,
6351 bool transparent_typedefs
)
6356 /* It's an alias template specialization if it's an alias and its
6357 TYPE_NAME is a specialization of a primary template. */
6358 if (typedef_variant_p (t
))
6360 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6361 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6362 return CONST_CAST_TREE (t
);
6363 if (transparent_typedefs
)
6364 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6366 transparent_typedefs
);
6372 /* An alias template is complex from a SFINAE perspective if a template-id
6373 using that alias can be ill-formed when the expansion is not, as with
6374 the void_t template. We determine this by checking whether the
6375 expansion for the alias template uses all its template parameters. */
6377 struct uses_all_template_parms_data
6384 uses_all_template_parms_r (tree t
, void *data_
)
6386 struct uses_all_template_parms_data
&data
6387 = *(struct uses_all_template_parms_data
*)data_
;
6388 tree idx
= get_template_parm_index (t
);
6390 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6391 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6395 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6398 complex_pack_expansion_r (tree t
, void *data_
)
6400 /* An alias template with a pack expansion that expands a pack from the
6401 enclosing class needs to be considered complex, to avoid confusion with
6402 the same pack being used as an argument to the alias's own template
6403 parameter (91966). */
6404 if (!PACK_EXPANSION_P (t
))
6406 struct uses_all_template_parms_data
&data
6407 = *(struct uses_all_template_parms_data
*)data_
;
6408 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6409 pack
= TREE_CHAIN (pack
))
6411 tree parm_pack
= TREE_VALUE (pack
);
6412 if (!TEMPLATE_PARM_P (parm_pack
))
6415 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6416 if (level
< data
.level
)
6423 complex_alias_template_p (const_tree tmpl
)
6425 /* A renaming alias isn't complex. */
6426 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6429 /* Any other constrained alias is complex. */
6430 if (get_constraints (tmpl
))
6433 struct uses_all_template_parms_data data
;
6434 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6435 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6436 data
.level
= TMPL_PARMS_DEPTH (parms
);
6437 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6438 data
.seen
= XALLOCAVEC (bool, len
);
6439 for (int i
= 0; i
< len
; ++i
)
6440 data
.seen
[i
] = false;
6442 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6443 NULL
, true, complex_pack_expansion_r
))
6445 for (int i
= 0; i
< len
; ++i
)
6451 /* If T is a specialization of a complex alias template with dependent
6452 template-arguments, return it; otherwise return NULL_TREE. If T is a
6453 typedef to such a specialization, return the specialization. */
6456 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6458 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6461 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6463 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6464 && (any_dependent_template_arguments_p
6465 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6466 return CONST_CAST_TREE (t
);
6468 if (transparent_typedefs
)
6470 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6471 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6477 /* Return the number of innermost template parameters in TMPL. */
6480 num_innermost_template_parms (const_tree tmpl
)
6482 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6483 return TREE_VEC_LENGTH (parms
);
6486 /* Return either TMPL or another template that it is equivalent to under DR
6487 1286: An alias that just changes the name of a template is equivalent to
6488 the other template. */
6491 get_underlying_template (tree tmpl
)
6493 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6494 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6496 /* Determine if the alias is equivalent to an underlying template. */
6497 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6498 /* The underlying type may have been ill-formed. Don't proceed. */
6501 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6505 tree underlying
= TI_TEMPLATE (tinfo
);
6506 if (!PRIMARY_TEMPLATE_P (underlying
)
6507 || (num_innermost_template_parms (tmpl
)
6508 != num_innermost_template_parms (underlying
)))
6511 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6512 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6515 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6516 it's appropriate to treat a less-constrained alias as equivalent. */
6517 if (!at_least_as_constrained (underlying
, tmpl
))
6520 /* Alias is equivalent. Strip it and repeat. */
6527 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6528 must be a reference-to-function or a pointer-to-function type, as specified
6529 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6530 and check that the resulting function has external linkage. */
6533 convert_nontype_argument_function (tree type
, tree expr
,
6534 tsubst_flags_t complain
)
6538 linkage_kind linkage
;
6540 fn
= instantiate_type (type
, fns
, tf_none
);
6541 if (fn
== error_mark_node
)
6542 return error_mark_node
;
6544 if (value_dependent_expression_p (fn
))
6547 fn_no_ptr
= strip_fnptr_conv (fn
);
6548 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6549 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6550 if (BASELINK_P (fn_no_ptr
))
6551 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6553 /* [temp.arg.nontype]/1
6555 A template-argument for a non-type, non-template template-parameter
6558 -- the address of an object or function with external [C++11: or
6559 internal] linkage. */
6561 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6562 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6564 if (complain
& tf_error
)
6566 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6567 error_at (loc
, "%qE is not a valid template argument for type %qT",
6569 if (TYPE_PTR_P (type
))
6570 inform (loc
, "it must be the address of a function "
6571 "with external linkage");
6573 inform (loc
, "it must be the name of a function with "
6574 "external linkage");
6579 linkage
= decl_linkage (fn_no_ptr
);
6580 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6582 if (complain
& tf_error
)
6584 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6585 if (cxx_dialect
>= cxx11
)
6586 error_at (loc
, "%qE is not a valid template argument for type "
6587 "%qT because %qD has no linkage",
6588 expr
, type
, fn_no_ptr
);
6590 error_at (loc
, "%qE is not a valid template argument for type "
6591 "%qT because %qD does not have external linkage",
6592 expr
, type
, fn_no_ptr
);
6598 if (TYPE_REF_P (type
))
6600 if (REFERENCE_REF_P (fn
))
6601 fn
= TREE_OPERAND (fn
, 0);
6603 fn
= build_address (fn
);
6605 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6606 fn
= build_nop (type
, fn
);
6611 /* Subroutine of convert_nontype_argument.
6612 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6613 Emit an error otherwise. */
6616 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6617 tsubst_flags_t complain
)
6619 tree orig_expr
= expr
;
6621 if (null_ptr_cst_p (expr
))
6623 if (TREE_CODE (expr
) == PTRMEM_CST
6624 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6625 PTRMEM_CST_CLASS (expr
)))
6627 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6629 if (processing_template_decl
6630 && TREE_CODE (expr
) == ADDR_EXPR
6631 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6633 if (complain
& tf_error
)
6635 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6636 error_at (loc
, "%qE is not a valid template argument for type %qT",
6638 if (TREE_CODE (expr
) != PTRMEM_CST
)
6639 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6641 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6646 /* Returns TRUE iff the address of OP is value-dependent.
6648 14.6.2.4 [temp.dep.temp]:
6649 A non-integral non-type template-argument is dependent if its type is
6650 dependent or it has either of the following forms
6653 and contains a nested-name-specifier which specifies a class-name that
6654 names a dependent type.
6656 We generalize this to just say that the address of a member of a
6657 dependent class is value-dependent; the above doesn't cover the
6658 address of a static data member named with an unqualified-id. */
6661 has_value_dependent_address (tree op
)
6663 STRIP_ANY_LOCATION_WRAPPER (op
);
6665 /* We could use get_inner_reference here, but there's no need;
6666 this is only relevant for template non-type arguments, which
6667 can only be expressed as &id-expression. */
6670 tree ctx
= CP_DECL_CONTEXT (op
);
6671 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6678 /* The next set of functions are used for providing helpful explanatory
6679 diagnostics for failed overload resolution. Their messages should be
6680 indented by two spaces for consistency with the messages in
6684 unify_success (bool /*explain_p*/)
6689 /* Other failure functions should call this one, to provide a single function
6690 for setting a breakpoint on. */
6693 unify_invalid (bool /*explain_p*/)
6699 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6702 inform (input_location
,
6703 " couldn%'t deduce template parameter %qD", parm
);
6704 return unify_invalid (explain_p
);
6708 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6711 inform (input_location
,
6712 " types %qT and %qT have incompatible cv-qualifiers",
6714 return unify_invalid (explain_p
);
6718 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6721 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6722 return unify_invalid (explain_p
);
6726 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6729 inform (input_location
,
6730 " template parameter %qD is not a parameter pack, but "
6733 return unify_invalid (explain_p
);
6737 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6740 inform (input_location
,
6741 " template argument %qE does not match "
6742 "pointer-to-member constant %qE",
6744 return unify_invalid (explain_p
);
6748 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6751 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6752 return unify_invalid (explain_p
);
6756 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6759 inform (input_location
,
6760 " inconsistent parameter pack deduction with %qT and %qT",
6762 return unify_invalid (explain_p
);
6766 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6771 inform (input_location
,
6772 " deduced conflicting types for parameter %qT (%qT and %qT)",
6773 parm
, first
, second
);
6775 inform (input_location
,
6776 " deduced conflicting values for non-type parameter "
6777 "%qE (%qE and %qE)", parm
, first
, second
);
6779 return unify_invalid (explain_p
);
6783 unify_vla_arg (bool explain_p
, tree arg
)
6786 inform (input_location
,
6787 " variable-sized array type %qT is not "
6788 "a valid template argument",
6790 return unify_invalid (explain_p
);
6794 unify_method_type_error (bool explain_p
, tree arg
)
6797 inform (input_location
,
6798 " member function type %qT is not a valid template argument",
6800 return unify_invalid (explain_p
);
6804 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6809 inform_n (input_location
, wanted
,
6810 " candidate expects at least %d argument, %d provided",
6811 " candidate expects at least %d arguments, %d provided",
6814 inform_n (input_location
, wanted
,
6815 " candidate expects %d argument, %d provided",
6816 " candidate expects %d arguments, %d provided",
6819 return unify_invalid (explain_p
);
6823 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6825 return unify_arity (explain_p
, have
, wanted
);
6829 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6830 bool least_p
= false)
6832 return unify_arity (explain_p
, have
, wanted
, least_p
);
6836 unify_arg_conversion (bool explain_p
, tree to_type
,
6837 tree from_type
, tree arg
)
6840 inform (cp_expr_loc_or_input_loc (arg
),
6841 " cannot convert %qE (type %qT) to type %qT",
6842 arg
, from_type
, to_type
);
6843 return unify_invalid (explain_p
);
6847 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6848 tree parm
, tree arg
)
6853 case tbr_ambiguous_baseclass
:
6854 inform (input_location
, " %qT is an ambiguous base class of %qT",
6858 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6861 return unify_invalid (explain_p
);
6865 unify_inconsistent_template_template_parameters (bool explain_p
)
6868 inform (input_location
,
6869 " template parameters of a template template argument are "
6870 "inconsistent with other deduced template arguments");
6871 return unify_invalid (explain_p
);
6875 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6878 inform (input_location
,
6879 " cannot deduce a template for %qT from non-template type %qT",
6881 return unify_invalid (explain_p
);
6885 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6888 inform (input_location
,
6889 " template argument %qE does not match %qE", arg
, parm
);
6890 return unify_invalid (explain_p
);
6893 /* True if T is a C++20 template parameter object to store the argument for a
6894 template parameter of class type. */
6897 template_parm_object_p (const_tree t
)
6899 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6900 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6903 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6904 argument for TYPE, points to an unsuitable object. */
6907 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6909 switch (TREE_CODE (expr
))
6912 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6916 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6921 unsigned i
; tree elt
;
6922 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6923 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6930 tree decl
= TREE_OPERAND (expr
, 0);
6934 if (complain
& tf_error
)
6935 error_at (cp_expr_loc_or_input_loc (expr
),
6936 "%qE is not a valid template argument of type %qT "
6937 "because %qE is not a variable", expr
, type
, decl
);
6940 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6942 if (complain
& tf_error
)
6943 error_at (cp_expr_loc_or_input_loc (expr
),
6944 "%qE is not a valid template argument of type %qT "
6945 "in C++98 because %qD does not have external linkage",
6949 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6950 && decl_linkage (decl
) == lk_none
)
6952 if (complain
& tf_error
)
6953 error_at (cp_expr_loc_or_input_loc (expr
),
6954 "%qE is not a valid template argument of type %qT "
6955 "because %qD has no linkage", expr
, type
, decl
);
6958 /* C++17: For a non-type template-parameter of reference or pointer
6959 type, the value of the constant expression shall not refer to (or
6960 for a pointer type, shall not be the address of):
6961 * a subobject (4.5),
6962 * a temporary object (15.2),
6963 * a string literal (5.13.5),
6964 * the result of a typeid expression (8.2.8), or
6965 * a predefined __func__ variable (11.4.1). */
6966 else if (DECL_ARTIFICIAL (decl
))
6968 if (complain
& tf_error
)
6969 error ("the address of %qD is not a valid template argument",
6973 else if (!same_type_ignoring_top_level_qualifiers_p
6974 (strip_array_types (TREE_TYPE (type
)),
6975 strip_array_types (TREE_TYPE (decl
))))
6977 if (complain
& tf_error
)
6978 error ("the address of the %qT subobject of %qD is not a "
6979 "valid template argument", TREE_TYPE (type
), decl
);
6982 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6984 if (complain
& tf_error
)
6985 error ("the address of %qD is not a valid template argument "
6986 "because it does not have static storage duration",
6994 if (!INDIRECT_TYPE_P (type
))
6995 /* We're only concerned about pointers and references here. */;
6996 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6997 /* Null pointer values are OK in C++11. */;
7002 if (complain
& tf_error
)
7003 error ("%qD is not a valid template argument "
7004 "because %qD is a variable, not the address of "
7005 "a variable", expr
, expr
);
7010 if (complain
& tf_error
)
7011 error ("%qE is not a valid template argument for %qT "
7012 "because it is not the address of a variable",
7022 /* The template arguments corresponding to template parameter objects of types
7023 that contain pointers to members. */
7025 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7027 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7028 template argument EXPR. */
7031 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7033 if (TREE_CODE (expr
) == TARGET_EXPR
)
7034 expr
= TARGET_EXPR_INITIAL (expr
);
7036 if (!TREE_CONSTANT (expr
))
7038 if ((complain
& tf_error
)
7039 && require_rvalue_constant_expression (expr
))
7040 cxx_constant_value (expr
);
7041 return error_mark_node
;
7043 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7044 return error_mark_node
;
7046 tree name
= mangle_template_parm_object (expr
);
7047 tree decl
= get_global_binding (name
);
7051 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7052 decl
= create_temporary_var (type
);
7053 TREE_STATIC (decl
) = true;
7054 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7055 TREE_READONLY (decl
) = true;
7056 DECL_NAME (decl
) = name
;
7057 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7058 DECL_CONTEXT (decl
) = global_namespace
;
7059 comdat_linkage (decl
);
7061 if (!zero_init_p (type
))
7063 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7064 lower_var_init before we're done mangling. So store the original
7066 tree copy
= unshare_constructor (expr
);
7067 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7070 pushdecl_top_level_and_finish (decl
, expr
);
7075 /* Return the actual template argument corresponding to template parameter
7079 tparm_object_argument (tree var
)
7081 if (zero_init_p (TREE_TYPE (var
)))
7082 return DECL_INITIAL (var
);
7083 return *(tparm_obj_values
->get (var
));
7086 /* Attempt to convert the non-type template parameter EXPR to the
7087 indicated TYPE. If the conversion is successful, return the
7088 converted value. If the conversion is unsuccessful, return
7089 NULL_TREE if we issued an error message, or error_mark_node if we
7090 did not. We issue error messages for out-and-out bad template
7091 parameters, but not simply because the conversion failed, since we
7092 might be just trying to do argument deduction. Both TYPE and EXPR
7093 must be non-dependent.
7095 The conversion follows the special rules described in
7096 [temp.arg.nontype], and it is much more strict than an implicit
7099 This function is called twice for each template argument (see
7100 lookup_template_class for a more accurate description of this
7101 problem). This means that we need to handle expressions which
7102 are not valid in a C++ source, but can be created from the
7103 first call (for instance, casts to perform conversions). These
7104 hacks can go away after we fix the double coercion problem. */
7107 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7110 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7112 /* Detect immediately string literals as invalid non-type argument.
7113 This special-case is not needed for correctness (we would easily
7114 catch this later), but only to provide better diagnostic for this
7115 common user mistake. As suggested by DR 100, we do not mention
7116 linkage issues in the diagnostic as this is not the point. */
7117 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7119 if (complain
& tf_error
)
7120 error ("%qE is not a valid template argument for type %qT "
7121 "because string literals can never be used in this context",
7126 /* Add the ADDR_EXPR now for the benefit of
7127 value_dependent_expression_p. */
7128 if (TYPE_PTROBV_P (type
)
7129 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7131 expr
= decay_conversion (expr
, complain
);
7132 if (expr
== error_mark_node
)
7133 return error_mark_node
;
7136 /* If we are in a template, EXPR may be non-dependent, but still
7137 have a syntactic, rather than semantic, form. For example, EXPR
7138 might be a SCOPE_REF, rather than the VAR_DECL to which the
7139 SCOPE_REF refers. Preserving the qualifying scope is necessary
7140 so that access checking can be performed when the template is
7141 instantiated -- but here we need the resolved form so that we can
7142 convert the argument. */
7143 bool non_dep
= false;
7144 if (TYPE_REF_OBJ_P (type
)
7145 && has_value_dependent_address (expr
))
7146 /* If we want the address and it's value-dependent, don't fold. */;
7147 else if (processing_template_decl
7148 && is_nondependent_constant_expression (expr
))
7150 if (error_operand_p (expr
))
7151 return error_mark_node
;
7152 expr_type
= TREE_TYPE (expr
);
7154 /* If the argument is non-dependent, perform any conversions in
7155 non-dependent context as well. */
7156 processing_template_decl_sentinel
s (non_dep
);
7158 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7160 const bool val_dep_p
= value_dependent_expression_p (expr
);
7162 expr
= canonicalize_expr_argument (expr
, complain
);
7164 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7165 to a non-type argument of "nullptr". */
7166 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7167 expr
= fold_simple (convert (type
, expr
));
7169 /* In C++11, integral or enumeration non-type template arguments can be
7170 arbitrary constant expressions. Pointer and pointer to
7171 member arguments can be general constant expressions that evaluate
7172 to a null value, but otherwise still need to be of a specific form. */
7173 if (cxx_dialect
>= cxx11
)
7175 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7176 /* A PTRMEM_CST is already constant, and a valid template
7177 argument for a parameter of pointer to member type, we just want
7178 to leave it in that form rather than lower it to a
7180 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7181 || cxx_dialect
>= cxx17
)
7183 /* C++17: A template-argument for a non-type template-parameter shall
7184 be a converted constant expression (8.20) of the type of the
7185 template-parameter. */
7186 expr
= build_converted_constant_expr (type
, expr
, complain
);
7187 if (expr
== error_mark_node
)
7188 /* Make sure we return NULL_TREE only if we have really issued
7189 an error, as described above. */
7190 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7191 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7193 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7196 expr
= maybe_constant_value (expr
, NULL_TREE
,
7197 /*manifestly_const_eval=*/true);
7198 expr
= convert_from_reference (expr
);
7200 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7202 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7203 /*manifestly_const_eval=*/true);
7204 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7205 : null_member_pointer_value_p (folded
))
7210 if (TYPE_REF_P (type
))
7211 expr
= mark_lvalue_use (expr
);
7213 expr
= mark_rvalue_use (expr
);
7215 /* HACK: Due to double coercion, we can get a
7216 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7217 which is the tree that we built on the first call (see
7218 below when coercing to reference to object or to reference to
7219 function). We just strip everything and get to the arg.
7220 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7222 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7224 tree probe_type
, probe
= expr
;
7225 if (REFERENCE_REF_P (probe
))
7226 probe
= TREE_OPERAND (probe
, 0);
7227 probe_type
= TREE_TYPE (probe
);
7228 if (TREE_CODE (probe
) == NOP_EXPR
)
7230 /* ??? Maybe we could use convert_from_reference here, but we
7231 would need to relax its constraints because the NOP_EXPR
7232 could actually change the type to something more cv-qualified,
7233 and this is not folded by convert_from_reference. */
7234 tree addr
= TREE_OPERAND (probe
, 0);
7235 if (TYPE_REF_P (probe_type
)
7236 && TREE_CODE (addr
) == ADDR_EXPR
7237 && TYPE_PTR_P (TREE_TYPE (addr
))
7238 && (same_type_ignoring_top_level_qualifiers_p
7239 (TREE_TYPE (probe_type
),
7240 TREE_TYPE (TREE_TYPE (addr
)))))
7242 expr
= TREE_OPERAND (addr
, 0);
7243 expr_type
= TREE_TYPE (probe_type
);
7248 /* [temp.arg.nontype]/5, bullet 1
7250 For a non-type template-parameter of integral or enumeration type,
7251 integral promotions (_conv.prom_) and integral conversions
7252 (_conv.integral_) are applied. */
7253 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7255 if (cxx_dialect
< cxx11
)
7257 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7258 t
= maybe_constant_value (t
);
7259 if (t
!= error_mark_node
)
7263 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7264 return error_mark_node
;
7266 /* Notice that there are constant expressions like '4 % 0' which
7267 do not fold into integer constants. */
7268 if (TREE_CODE (expr
) != INTEGER_CST
&& !val_dep_p
)
7270 if (complain
& tf_error
)
7272 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7273 if (!require_potential_constant_expression (expr
))
7274 expr
= error_mark_node
;
7276 expr
= cxx_constant_value (expr
);
7277 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7278 inform (loc
, "in template argument for type %qT", type
);
7279 if (expr
== error_mark_node
)
7281 /* else cxx_constant_value complained but gave us
7282 a real constant, so go ahead. */
7283 if (TREE_CODE (expr
) != INTEGER_CST
)
7285 /* Some assemble time constant expressions like
7286 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7287 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7288 as we can emit them into .rodata initializers of
7289 variables, yet they can't fold into an INTEGER_CST at
7290 compile time. Refuse them here. */
7291 gcc_checking_assert (reduced_constant_expression_p (expr
));
7292 error_at (loc
, "template argument %qE for type %qT not "
7293 "a constant integer", expr
, type
);
7301 /* Avoid typedef problems. */
7302 if (TREE_TYPE (expr
) != type
)
7303 expr
= fold_convert (type
, expr
);
7305 /* [temp.arg.nontype]/5, bullet 2
7307 For a non-type template-parameter of type pointer to object,
7308 qualification conversions (_conv.qual_) and the array-to-pointer
7309 conversion (_conv.array_) are applied. */
7310 else if (TYPE_PTROBV_P (type
))
7312 tree decayed
= expr
;
7314 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7315 decay_conversion or an explicit cast. If it's a problematic cast,
7316 we'll complain about it below. */
7317 if (TREE_CODE (expr
) == NOP_EXPR
)
7321 if (TREE_CODE (probe
) == ADDR_EXPR
7322 && TYPE_PTR_P (TREE_TYPE (probe
)))
7325 expr_type
= TREE_TYPE (expr
);
7329 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7331 A template-argument for a non-type, non-template template-parameter
7332 shall be one of: [...]
7334 -- the name of a non-type template-parameter;
7335 -- the address of an object or function with external linkage, [...]
7336 expressed as "& id-expression" where the & is optional if the name
7337 refers to a function or array, or if the corresponding
7338 template-parameter is a reference.
7340 Here, we do not care about functions, as they are invalid anyway
7341 for a parameter of type pointer-to-object. */
7344 /* Non-type template parameters are OK. */
7346 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7347 /* Null pointer values are OK in C++11. */;
7348 else if (TREE_CODE (expr
) != ADDR_EXPR
7349 && !INDIRECT_TYPE_P (expr_type
))
7350 /* Other values, like integer constants, might be valid
7351 non-type arguments of some other type. */
7352 return error_mark_node
;
7353 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7358 expr
= perform_qualification_conversions (type
, expr
);
7359 if (expr
== error_mark_node
)
7360 return error_mark_node
;
7362 /* [temp.arg.nontype]/5, bullet 3
7364 For a non-type template-parameter of type reference to object, no
7365 conversions apply. The type referred to by the reference may be more
7366 cv-qualified than the (otherwise identical) type of the
7367 template-argument. The template-parameter is bound directly to the
7368 template-argument, which must be an lvalue. */
7369 else if (TYPE_REF_OBJ_P (type
))
7371 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7373 return error_mark_node
;
7375 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7377 if (complain
& tf_error
)
7378 error ("%qE is not a valid template argument for type %qT "
7379 "because of conflicts in cv-qualification", expr
, type
);
7383 if (!lvalue_p (expr
))
7385 if (complain
& tf_error
)
7386 error ("%qE is not a valid template argument for type %qT "
7387 "because it is not an lvalue", expr
, type
);
7391 /* [temp.arg.nontype]/1
7393 A template-argument for a non-type, non-template template-parameter
7394 shall be one of: [...]
7396 -- the address of an object or function with external linkage. */
7397 if (INDIRECT_REF_P (expr
)
7398 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7400 expr
= TREE_OPERAND (expr
, 0);
7403 if (complain
& tf_error
)
7404 error ("%q#D is not a valid template argument for type %qT "
7405 "because a reference variable does not have a constant "
7406 "address", expr
, type
);
7411 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7412 /* OK, dependent reference. We don't want to ask whether a DECL is
7413 itself value-dependent, since what we want here is its address. */;
7416 expr
= build_address (expr
);
7418 if (invalid_tparm_referent_p (type
, expr
, complain
))
7422 if (!same_type_p (type
, TREE_TYPE (expr
)))
7423 expr
= build_nop (type
, expr
);
7425 /* [temp.arg.nontype]/5, bullet 4
7427 For a non-type template-parameter of type pointer to function, only
7428 the function-to-pointer conversion (_conv.func_) is applied. If the
7429 template-argument represents a set of overloaded functions (or a
7430 pointer to such), the matching function is selected from the set
7432 else if (TYPE_PTRFN_P (type
))
7434 /* If the argument is a template-id, we might not have enough
7435 context information to decay the pointer. */
7436 if (!type_unknown_p (expr_type
))
7438 expr
= decay_conversion (expr
, complain
);
7439 if (expr
== error_mark_node
)
7440 return error_mark_node
;
7443 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7444 /* Null pointer values are OK in C++11. */
7445 return perform_qualification_conversions (type
, expr
);
7447 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7448 if (!expr
|| expr
== error_mark_node
)
7451 /* [temp.arg.nontype]/5, bullet 5
7453 For a non-type template-parameter of type reference to function, no
7454 conversions apply. If the template-argument represents a set of
7455 overloaded functions, the matching function is selected from the set
7457 else if (TYPE_REFFN_P (type
))
7459 if (TREE_CODE (expr
) == ADDR_EXPR
)
7461 if (complain
& tf_error
)
7463 error ("%qE is not a valid template argument for type %qT "
7464 "because it is a pointer", expr
, type
);
7465 inform (input_location
, "try using %qE instead",
7466 TREE_OPERAND (expr
, 0));
7471 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7472 if (!expr
|| expr
== error_mark_node
)
7475 /* [temp.arg.nontype]/5, bullet 6
7477 For a non-type template-parameter of type pointer to member function,
7478 no conversions apply. If the template-argument represents a set of
7479 overloaded member functions, the matching member function is selected
7480 from the set (_over.over_). */
7481 else if (TYPE_PTRMEMFUNC_P (type
))
7483 expr
= instantiate_type (type
, expr
, tf_none
);
7484 if (expr
== error_mark_node
)
7485 return error_mark_node
;
7487 /* [temp.arg.nontype] bullet 1 says the pointer to member
7488 expression must be a pointer-to-member constant. */
7490 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7493 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7494 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7495 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7496 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7498 /* [temp.arg.nontype]/5, bullet 7
7500 For a non-type template-parameter of type pointer to data member,
7501 qualification conversions (_conv.qual_) are applied. */
7502 else if (TYPE_PTRDATAMEM_P (type
))
7504 /* [temp.arg.nontype] bullet 1 says the pointer to member
7505 expression must be a pointer-to-member constant. */
7507 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7510 expr
= perform_qualification_conversions (type
, expr
);
7511 if (expr
== error_mark_node
)
7514 else if (NULLPTR_TYPE_P (type
))
7516 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7518 if (complain
& tf_error
)
7519 error ("%qE is not a valid template argument for type %qT "
7520 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7525 else if (CLASS_TYPE_P (type
))
7527 /* Replace the argument with a reference to the corresponding template
7528 parameter object. */
7530 expr
= get_template_parm_object (expr
, complain
);
7531 if (expr
== error_mark_node
)
7534 /* A template non-type parameter must be one of the above. */
7538 /* Sanity check: did we actually convert the argument to the
7540 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7541 (type
, TREE_TYPE (expr
)));
7542 return convert_from_reference (expr
);
7545 /* Subroutine of coerce_template_template_parms, which returns 1 if
7546 PARM_PARM and ARG_PARM match using the rule for the template
7547 parameters of template template parameters. Both PARM and ARG are
7548 template parameters; the rest of the arguments are the same as for
7549 coerce_template_template_parms.
7552 coerce_template_template_parm (tree parm
,
7554 tsubst_flags_t complain
,
7558 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7559 || parm
== NULL_TREE
|| error_operand_p (parm
))
7562 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7565 switch (TREE_CODE (parm
))
7568 /* We encounter instantiations of templates like
7569 template <template <template <class> class> class TT>
7572 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7573 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7575 if (!coerce_template_template_parms
7576 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7582 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7583 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7584 /* Argument is a parameter pack but parameter is not. */
7589 /* The tsubst call is used to handle cases such as
7591 template <int> class C {};
7592 template <class T, template <T> class TT> class D {};
7595 i.e. the parameter list of TT depends on earlier parameters. */
7596 if (!uses_template_parms (TREE_TYPE (arg
)))
7598 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7599 if (!uses_template_parms (t
)
7600 && !same_type_p (t
, TREE_TYPE (arg
)))
7604 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7605 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7606 /* Argument is a parameter pack but parameter is not. */
7618 /* Coerce template argument list ARGLIST for use with template
7619 template-parameter TEMPL. */
7622 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7623 tsubst_flags_t complain
)
7625 /* Consider an example where a template template parameter declared as
7627 template <class T, class U = std::allocator<T> > class TT
7629 The template parameter level of T and U are one level larger than
7630 of TT. To proper process the default argument of U, say when an
7631 instantiation `TT<int>' is seen, we need to build the full
7632 arguments containing {int} as the innermost level. Outer levels,
7633 available when not appearing as default template argument, can be
7634 obtained from the arguments of the enclosing template.
7636 Suppose that TT is later substituted with std::vector. The above
7637 instantiation is `TT<int, std::allocator<T> >' with TT at
7638 level 1, and T at level 2, while the template arguments at level 1
7639 becomes {std::vector} and the inner level 2 is {int}. */
7641 tree outer
= DECL_CONTEXT (templ
);
7643 outer
= generic_targs_for (outer
);
7644 else if (current_template_parms
)
7646 /* This is an argument of the current template, so we haven't set
7647 DECL_CONTEXT yet. */
7648 tree relevant_template_parms
;
7650 /* Parameter levels that are greater than the level of the given
7651 template template parm are irrelevant. */
7652 relevant_template_parms
= current_template_parms
;
7653 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7654 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7655 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7657 outer
= template_parms_to_args (relevant_template_parms
);
7661 arglist
= add_to_template_args (outer
, arglist
);
7663 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7664 return coerce_template_parms (parmlist
, arglist
, templ
,
7666 /*require_all_args=*/true,
7667 /*use_default_args=*/true);
7670 /* A cache of template template parameters with match-all default
7672 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7674 /* T is a bound template template-parameter. Copy its arguments into default
7675 arguments of the template template-parameter's template parameters. */
7678 add_defaults_to_ttp (tree otmpl
)
7680 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7683 tree ntmpl
= copy_node (otmpl
);
7685 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7686 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7687 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7688 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7689 TYPE_NAME (ntype
) = ntmpl
;
7690 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7692 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7693 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7694 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7695 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7697 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7698 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7699 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7700 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7701 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7703 tree o
= TREE_VEC_ELT (vec
, i
);
7704 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7706 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7707 TREE_PURPOSE (n
) = any_targ_node
;
7711 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7715 /* ARG is a bound potential template template-argument, and PARGS is a list
7716 of arguments for the corresponding template template-parameter. Adjust
7717 PARGS as appropriate for application to ARG's template, and if ARG is a
7718 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7719 arguments to the template template parameter. */
7722 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7724 ++processing_template_decl
;
7725 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7726 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7728 /* When comparing two template template-parameters in partial ordering,
7729 rewrite the one currently being used as an argument to have default
7730 arguments for all parameters. */
7731 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7732 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7733 if (pargs
!= error_mark_node
)
7734 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7735 TYPE_TI_ARGS (arg
));
7740 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7741 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7742 /*require_all*/true,
7743 /*use_default*/true);
7745 --processing_template_decl
;
7749 /* Subroutine of unify for the case when PARM is a
7750 BOUND_TEMPLATE_TEMPLATE_PARM. */
7753 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7756 tree parmvec
= TYPE_TI_ARGS (parm
);
7757 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7759 /* The template template parm might be variadic and the argument
7760 not, so flatten both argument lists. */
7761 parmvec
= expand_template_argument_pack (parmvec
);
7762 argvec
= expand_template_argument_pack (argvec
);
7766 /* In keeping with P0522R0, adjust P's template arguments
7767 to apply to A's template; then flatten it again. */
7768 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7769 nparmvec
= expand_template_argument_pack (nparmvec
);
7771 if (unify (tparms
, targs
, nparmvec
, argvec
,
7772 UNIFY_ALLOW_NONE
, explain_p
))
7775 /* If the P0522 adjustment eliminated a pack expansion, deduce
7778 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7779 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7780 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7785 /* Deduce arguments T, i from TT<T> or TT<i>.
7786 We check each element of PARMVEC and ARGVEC individually
7787 rather than the whole TREE_VEC since they can have
7788 different number of elements, which is allowed under N2555. */
7790 int len
= TREE_VEC_LENGTH (parmvec
);
7792 /* Check if the parameters end in a pack, making them
7794 int parm_variadic_p
= 0;
7796 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7797 parm_variadic_p
= 1;
7799 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7800 /* If the template argument list of P contains a pack
7801 expansion that is not the last template argument, the
7802 entire template argument list is a non-deduced
7804 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7805 return unify_success (explain_p
);
7807 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7808 return unify_too_few_arguments (explain_p
,
7809 TREE_VEC_LENGTH (argvec
), len
);
7811 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7812 if (unify (tparms
, targs
,
7813 TREE_VEC_ELT (parmvec
, i
),
7814 TREE_VEC_ELT (argvec
, i
),
7815 UNIFY_ALLOW_NONE
, explain_p
))
7819 && unify_pack_expansion (tparms
, targs
,
7822 /*subr=*/true, explain_p
))
7829 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7830 template template parameters. Both PARM_PARMS and ARG_PARMS are
7831 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7834 Consider the example:
7835 template <class T> class A;
7836 template<template <class U> class TT> class B;
7838 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7839 the parameters to A, and OUTER_ARGS contains A. */
7842 coerce_template_template_parms (tree parm_parms
,
7844 tsubst_flags_t complain
,
7848 int nparms
, nargs
, i
;
7852 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7853 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7855 nparms
= TREE_VEC_LENGTH (parm_parms
);
7856 nargs
= TREE_VEC_LENGTH (arg_parms
);
7860 /* P0522R0: A template template-parameter P is at least as specialized as
7861 a template template-argument A if, given the following rewrite to two
7862 function templates, the function template corresponding to P is at
7863 least as specialized as the function template corresponding to A
7864 according to the partial ordering rules for function templates
7865 ([temp.func.order]). Given an invented class template X with the
7866 template parameter list of A (including default arguments):
7868 * Each of the two function templates has the same template parameters,
7869 respectively, as P or A.
7871 * Each function template has a single function parameter whose type is
7872 a specialization of X with template arguments corresponding to the
7873 template parameters from the respective function template where, for
7874 each template parameter PP in the template parameter list of the
7875 function template, a corresponding template argument AA is formed. If
7876 PP declares a parameter pack, then AA is the pack expansion
7877 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7879 If the rewrite produces an invalid type, then P is not at least as
7880 specialized as A. */
7882 /* So coerce P's args to apply to A's parms, and then deduce between A's
7883 args and the converted args. If that succeeds, A is at least as
7884 specialized as P, so they match.*/
7885 tree pargs
= template_parms_level_to_args (parm_parms
);
7886 pargs
= add_outermost_template_args (outer_args
, pargs
);
7887 ++processing_template_decl
;
7888 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7889 /*require_all*/true, /*use_default*/true);
7890 --processing_template_decl
;
7891 if (pargs
!= error_mark_node
)
7893 tree targs
= make_tree_vec (nargs
);
7894 tree aargs
= template_parms_level_to_args (arg_parms
);
7895 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7901 /* Determine whether we have a parameter pack at the end of the
7902 template template parameter's template parameter list. */
7903 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7905 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7907 if (error_operand_p (parm
))
7910 switch (TREE_CODE (parm
))
7914 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7919 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7929 && !(variadic_p
&& nargs
>= nparms
- 1))
7932 /* Check all of the template parameters except the parameter pack at
7933 the end (if any). */
7934 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7936 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7937 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7940 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7941 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7943 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7951 /* Check each of the template parameters in the template
7952 argument against the template parameter pack at the end of
7953 the template template parameter. */
7954 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7957 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7959 for (; i
< nargs
; ++i
)
7961 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7964 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7966 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7975 /* Verifies that the deduced template arguments (in TARGS) for the
7976 template template parameters (in TPARMS) represent valid bindings,
7977 by comparing the template parameter list of each template argument
7978 to the template parameter list of its corresponding template
7979 template parameter, in accordance with DR150. This
7980 routine can only be called after all template arguments have been
7981 deduced. It will return TRUE if all of the template template
7982 parameter bindings are okay, FALSE otherwise. */
7984 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7986 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7989 /* We're dealing with template parms in this process. */
7990 ++processing_template_decl
;
7992 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7994 for (i
= 0; i
< ntparms
; ++i
)
7996 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7997 tree targ
= TREE_VEC_ELT (targs
, i
);
7999 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8001 tree packed_args
= NULL_TREE
;
8004 if (ARGUMENT_PACK_P (targ
))
8006 /* Look inside the argument pack. */
8007 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8008 len
= TREE_VEC_LENGTH (packed_args
);
8011 for (idx
= 0; idx
< len
; ++idx
)
8013 tree targ_parms
= NULL_TREE
;
8016 /* Extract the next argument from the argument
8018 targ
= TREE_VEC_ELT (packed_args
, idx
);
8020 if (PACK_EXPANSION_P (targ
))
8021 /* Look at the pattern of the pack expansion. */
8022 targ
= PACK_EXPANSION_PATTERN (targ
);
8024 /* Extract the template parameters from the template
8026 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8027 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8028 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8029 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8031 /* Verify that we can coerce the template template
8032 parameters from the template argument to the template
8033 parameter. This requires an exact match. */
8035 && !coerce_template_template_parms
8036 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8051 --processing_template_decl
;
8055 /* Since type attributes aren't mangled, we need to strip them from
8056 template type arguments. */
8059 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8061 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8063 bool removed_attributes
= false;
8064 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8065 if (removed_attributes
8066 && (complain
& tf_warning
))
8067 warning (OPT_Wignored_attributes
,
8068 "ignoring attributes on template argument %qT", arg
);
8072 /* And from inside dependent non-type arguments like sizeof(Type). */
8075 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8077 if (!arg
|| arg
== error_mark_node
)
8079 bool removed_attributes
= false;
8080 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8081 if (removed_attributes
8082 && (complain
& tf_warning
))
8083 warning (OPT_Wignored_attributes
,
8084 "ignoring attributes in template argument %qE", arg
);
8088 // A template declaration can be substituted for a constrained
8089 // template template parameter only when the argument is more
8090 // constrained than the parameter.
8092 is_compatible_template_arg (tree parm
, tree arg
)
8094 tree parm_cons
= get_constraints (parm
);
8096 /* For now, allow constrained template template arguments
8097 and unconstrained template template parameters. */
8098 if (parm_cons
== NULL_TREE
)
8101 /* If the template parameter is constrained, we need to rewrite its
8102 constraints in terms of the ARG's template parameters. This ensures
8103 that all of the template parameter types will have the same depth.
8105 Note that this is only valid when coerce_template_template_parm is
8106 true for the innermost template parameters of PARM and ARG. In other
8107 words, because coercion is successful, this conversion will be valid. */
8108 tree new_args
= NULL_TREE
;
8111 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8112 new_args
= template_parms_level_to_args (aparms
);
8113 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8114 tf_none
, NULL_TREE
);
8115 if (parm_cons
== error_mark_node
)
8119 return weakly_subsumes (parm_cons
, new_args
, arg
);
8122 // Convert a placeholder argument into a binding to the original
8123 // parameter. The original parameter is saved as the TREE_TYPE of
8126 convert_wildcard_argument (tree parm
, tree arg
)
8128 TREE_TYPE (arg
) = parm
;
8132 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8133 because one of them is dependent. But we need to represent the
8134 conversion for the benefit of cp_tree_equal. */
8137 maybe_convert_nontype_argument (tree type
, tree arg
)
8139 /* Auto parms get no conversion. */
8140 if (type_uses_auto (type
))
8142 /* We don't need or want to add this conversion now if we're going to use the
8143 argument for deduction. */
8144 if (value_dependent_expression_p (arg
))
8147 type
= cv_unqualified (type
);
8148 tree argtype
= TREE_TYPE (arg
);
8149 if (same_type_p (type
, argtype
))
8152 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8153 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8157 /* Convert the indicated template ARG as necessary to match the
8158 indicated template PARM. Returns the converted ARG, or
8159 error_mark_node if the conversion was unsuccessful. Error and
8160 warning messages are issued under control of COMPLAIN. This
8161 conversion is for the Ith parameter in the parameter list. ARGS is
8162 the full set of template arguments deduced so far. */
8165 convert_template_argument (tree parm
,
8168 tsubst_flags_t complain
,
8174 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8176 if (parm
== error_mark_node
|| error_operand_p (arg
))
8177 return error_mark_node
;
8179 /* Trivially convert placeholders. */
8180 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8181 return convert_wildcard_argument (parm
, arg
);
8183 if (arg
== any_targ_node
)
8186 if (TREE_CODE (arg
) == TREE_LIST
8187 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8189 /* The template argument was the name of some
8190 member function. That's usually
8191 invalid, but static members are OK. In any
8192 case, grab the underlying fields/functions
8193 and issue an error later if required. */
8194 TREE_TYPE (arg
) = unknown_type_node
;
8199 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8200 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8201 || requires_tmpl_type
);
8203 /* When determining whether an argument pack expansion is a template,
8204 look at the pattern. */
8205 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8206 arg
= PACK_EXPANSION_PATTERN (arg
);
8208 /* Deal with an injected-class-name used as a template template arg. */
8209 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8211 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8212 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8214 if (cxx_dialect
>= cxx11
)
8215 /* OK under DR 1004. */;
8216 else if (complain
& tf_warning_or_error
)
8217 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8218 " used as template template argument", TYPE_NAME (arg
));
8219 else if (flag_pedantic_errors
)
8227 ((TREE_CODE (arg
) == TEMPLATE_DECL
8228 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8229 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8230 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8231 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8234 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8235 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8236 arg
= TYPE_STUB_DECL (arg
);
8238 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8240 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8241 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8243 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8245 if (complain
& tf_error
)
8246 error ("invalid use of destructor %qE as a type", orig_arg
);
8247 return error_mark_node
;
8250 permerror (input_location
,
8251 "to refer to a type member of a template parameter, "
8252 "use %<typename %E%>", orig_arg
);
8254 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8255 TREE_OPERAND (arg
, 1),
8261 if (is_type
!= requires_type
)
8265 if (complain
& tf_error
)
8267 error ("type/value mismatch at argument %d in template "
8268 "parameter list for %qD",
8272 /* The template argument is a type, but we're expecting
8274 inform (input_location
,
8275 " expected a constant of type %qT, got %qT",
8277 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8278 /* [temp.arg]/2: "In a template-argument, an ambiguity
8279 between a type-id and an expression is resolved to a
8280 type-id, regardless of the form of the corresponding
8281 template-parameter." So give the user a clue. */
8282 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8283 inform (input_location
, " ambiguous template argument "
8284 "for non-type template parameter is treated as "
8287 else if (requires_tmpl_type
)
8288 inform (input_location
,
8289 " expected a class template, got %qE", orig_arg
);
8291 inform (input_location
,
8292 " expected a type, got %qE", orig_arg
);
8295 return error_mark_node
;
8297 if (is_tmpl_type
^ requires_tmpl_type
)
8299 if (in_decl
&& (complain
& tf_error
))
8301 error ("type/value mismatch at argument %d in template "
8302 "parameter list for %qD",
8305 inform (input_location
,
8306 " expected a type, got %qT", DECL_NAME (arg
));
8308 inform (input_location
,
8309 " expected a class template, got %qT", orig_arg
);
8311 return error_mark_node
;
8314 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8315 /* We already did the appropriate conversion when packing args. */
8319 if (requires_tmpl_type
)
8321 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8322 /* The number of argument required is not known yet.
8323 Just accept it for now. */
8327 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8330 /* Strip alias templates that are equivalent to another
8332 arg
= get_underlying_template (arg
);
8333 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8335 if (coerce_template_template_parms (parmparm
, argparm
,
8341 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8343 if (val
!= error_mark_node
)
8345 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8346 val
= TREE_TYPE (val
);
8347 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8348 val
= make_pack_expansion (val
, complain
);
8353 if (in_decl
&& (complain
& tf_error
))
8355 error ("type/value mismatch at argument %d in "
8356 "template parameter list for %qD",
8358 inform (input_location
,
8359 " expected a template of type %qD, got %qT",
8363 val
= error_mark_node
;
8366 // Check that the constraints are compatible before allowing the
8368 if (val
!= error_mark_node
)
8369 if (!is_compatible_template_arg (parm
, arg
))
8371 if (in_decl
&& (complain
& tf_error
))
8373 error ("constraint mismatch at argument %d in "
8374 "template parameter list for %qD",
8376 inform (input_location
, " expected %qD but got %qD",
8379 val
= error_mark_node
;
8385 /* We only form one instance of each template specialization.
8386 Therefore, if we use a non-canonical variant (i.e., a
8387 typedef), any future messages referring to the type will use
8388 the typedef, which is confusing if those future uses do not
8389 themselves also use the typedef. */
8391 val
= canonicalize_type_argument (val
, complain
);
8395 tree t
= TREE_TYPE (parm
);
8397 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8398 > TMPL_ARGS_DEPTH (args
))
8399 /* We don't have enough levels of args to do any substitution. This
8400 can happen in the context of -fnew-ttp-matching. */;
8401 else if (tree a
= type_uses_auto (t
))
8403 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8404 if (t
== error_mark_node
)
8405 return error_mark_node
;
8408 t
= tsubst (t
, args
, complain
, in_decl
);
8410 if (invalid_nontype_parm_type_p (t
, complain
))
8411 return error_mark_node
;
8413 if (t
!= TREE_TYPE (parm
))
8414 t
= canonicalize_type_argument (t
, complain
);
8416 if (!type_dependent_expression_p (orig_arg
)
8417 && !uses_template_parms (t
))
8418 /* We used to call digest_init here. However, digest_init
8419 will report errors, which we don't want when complain
8420 is zero. More importantly, digest_init will try too
8421 hard to convert things: for example, `0' should not be
8422 converted to pointer type at this point according to
8423 the standard. Accepting this is not merely an
8424 extension, since deciding whether or not these
8425 conversions can occur is part of determining which
8426 function template to call, or whether a given explicit
8427 argument specification is valid. */
8428 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8431 val
= canonicalize_expr_argument (orig_arg
, complain
);
8432 val
= maybe_convert_nontype_argument (t
, val
);
8436 if (val
== NULL_TREE
)
8437 val
= error_mark_node
;
8438 else if (val
== error_mark_node
&& (complain
& tf_error
))
8439 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8440 "could not convert template argument %qE from %qT to %qT",
8441 orig_arg
, TREE_TYPE (orig_arg
), t
);
8443 if (INDIRECT_REF_P (val
))
8445 /* Reject template arguments that are references to built-in
8446 functions with no library fallbacks. */
8447 const_tree inner
= TREE_OPERAND (val
, 0);
8448 const_tree innertype
= TREE_TYPE (inner
);
8450 && TYPE_REF_P (innertype
)
8451 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8452 && TREE_OPERAND_LENGTH (inner
) > 0
8453 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8454 return error_mark_node
;
8457 if (TREE_CODE (val
) == SCOPE_REF
)
8459 /* Strip typedefs from the SCOPE_REF. */
8460 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8461 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8463 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8464 QUALIFIED_NAME_IS_TEMPLATE (val
));
8471 /* Coerces the remaining template arguments in INNER_ARGS (from
8472 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8473 Returns the coerced argument pack. PARM_IDX is the position of this
8474 parameter in the template parameter list. ARGS is the original
8475 template argument list. */
8477 coerce_template_parameter_pack (tree parms
,
8485 tsubst_flags_t complain
)
8487 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8488 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8491 tree packed_parms
= NULL_TREE
;
8493 if (arg_idx
> nargs
)
8496 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8498 /* When the template parameter is a non-type template parameter pack
8499 or template template parameter pack whose type or template
8500 parameters use parameter packs, we know exactly how many arguments
8501 we are looking for. Build a vector of the instantiated decls for
8502 these template parameters in PACKED_PARMS. */
8503 /* We can't use make_pack_expansion here because it would interpret a
8504 _DECL as a use rather than a declaration. */
8505 tree decl
= TREE_VALUE (parm
);
8506 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8507 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8508 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8509 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8511 TREE_VEC_LENGTH (args
)--;
8512 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8513 TREE_VEC_LENGTH (args
)++;
8515 if (packed_parms
== error_mark_node
)
8516 return error_mark_node
;
8518 /* If we're doing a partial instantiation of a member template,
8519 verify that all of the types used for the non-type
8520 template parameter pack are, in fact, valid for non-type
8521 template parameters. */
8523 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8525 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8526 for (j
= 0; j
< len
; ++j
)
8528 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8529 if (TREE_CODE (t
) == PARM_DECL
8530 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8531 return error_mark_node
;
8533 /* We don't know how many args we have yet, just
8534 use the unconverted ones for now. */
8538 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8540 /* Check if we have a placeholder pack, which indicates we're
8541 in the context of a introduction list. In that case we want
8542 to match this pack to the single placeholder. */
8543 else if (arg_idx
< nargs
8544 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8545 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8547 nargs
= arg_idx
+ 1;
8548 packed_args
= make_tree_vec (1);
8551 packed_args
= make_tree_vec (nargs
- arg_idx
);
8553 /* Convert the remaining arguments, which will be a part of the
8554 parameter pack "parm". */
8555 int first_pack_arg
= arg_idx
;
8556 for (; arg_idx
< nargs
; ++arg_idx
)
8558 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8559 tree actual_parm
= TREE_VALUE (parm
);
8560 int pack_idx
= arg_idx
- first_pack_arg
;
8564 /* Once we've packed as many args as we have types, stop. */
8565 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8567 else if (PACK_EXPANSION_P (arg
))
8568 /* We don't know how many args we have yet, just
8569 use the unconverted ones for now. */
8572 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8575 if (arg
== error_mark_node
)
8577 if (complain
& tf_error
)
8578 error ("template argument %d is invalid", arg_idx
+ 1);
8581 arg
= convert_template_argument (actual_parm
,
8582 arg
, new_args
, complain
, parm_idx
,
8584 if (arg
== error_mark_node
)
8586 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8589 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8590 && TREE_VEC_LENGTH (packed_args
) > 0)
8592 if (complain
& tf_error
)
8593 error ("wrong number of template arguments (%d, should be %d)",
8594 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8595 return error_mark_node
;
8598 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8599 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8600 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8603 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8604 TREE_CONSTANT (argument_pack
) = 1;
8607 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8609 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8610 TREE_VEC_LENGTH (packed_args
));
8611 return argument_pack
;
8614 /* Returns the number of pack expansions in the template argument vector
8618 pack_expansion_args_count (tree args
)
8623 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8625 tree elt
= TREE_VEC_ELT (args
, i
);
8626 if (elt
&& PACK_EXPANSION_P (elt
))
8632 /* Convert all template arguments to their appropriate types, and
8633 return a vector containing the innermost resulting template
8634 arguments. If any error occurs, return error_mark_node. Error and
8635 warning messages are issued under control of COMPLAIN.
8637 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8638 for arguments not specified in ARGS. Otherwise, if
8639 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8640 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8641 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8645 coerce_template_parms (tree parms
,
8648 tsubst_flags_t complain
,
8649 bool require_all_args
,
8650 bool use_default_args
)
8652 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8653 tree orig_inner_args
;
8656 tree new_inner_args
;
8658 /* When used as a boolean value, indicates whether this is a
8659 variadic template parameter list. Since it's an int, we can also
8660 subtract it from nparms to get the number of non-variadic
8663 int variadic_args_p
= 0;
8664 int post_variadic_parms
= 0;
8666 /* Adjustment to nparms for fixed parameter packs. */
8667 int fixed_pack_adjust
= 0;
8668 int fixed_packs
= 0;
8671 /* Likewise for parameters with default arguments. */
8674 if (args
== error_mark_node
)
8675 return error_mark_node
;
8677 nparms
= TREE_VEC_LENGTH (parms
);
8679 /* Determine if there are any parameter packs or default arguments. */
8680 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8682 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8684 ++post_variadic_parms
;
8685 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8687 if (TREE_PURPOSE (parm
))
8691 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8692 /* If there are no parameters that follow a parameter pack, we need to
8693 expand any argument packs so that we can deduce a parameter pack from
8694 some non-packed args followed by an argument pack, as in variadic85.C.
8695 If there are such parameters, we need to leave argument packs intact
8696 so the arguments are assigned properly. This can happen when dealing
8697 with a nested class inside a partial specialization of a class
8698 template, as in variadic92.C, or when deducing a template parameter pack
8699 from a sub-declarator, as in variadic114.C. */
8700 if (!post_variadic_parms
)
8701 inner_args
= expand_template_argument_pack (inner_args
);
8703 /* Count any pack expansion args. */
8704 variadic_args_p
= pack_expansion_args_count (inner_args
);
8706 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8707 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8708 || (nargs
< nparms
- variadic_p
8711 && (!use_default_args
8712 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8713 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8716 if (complain
& tf_error
)
8718 if (variadic_p
|| default_p
)
8720 nparms
-= variadic_p
+ default_p
;
8721 error ("wrong number of template arguments "
8722 "(%d, should be at least %d)", nargs
, nparms
);
8725 error ("wrong number of template arguments "
8726 "(%d, should be %d)", nargs
, nparms
);
8729 inform (DECL_SOURCE_LOCATION (in_decl
),
8730 "provided for %qD", in_decl
);
8733 return error_mark_node
;
8735 /* We can't pass a pack expansion to a non-pack parameter of an alias
8736 template (DR 1430). */
8738 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8739 || concept_definition_p (in_decl
))
8741 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8743 if (complain
& tf_error
)
8745 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8747 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8748 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8750 if (PACK_EXPANSION_P (arg
)
8751 && !template_parameter_pack_p (parm
))
8753 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8754 error_at (location_of (arg
),
8755 "pack expansion argument for non-pack parameter "
8756 "%qD of alias template %qD", parm
, in_decl
);
8758 error_at (location_of (arg
),
8759 "pack expansion argument for non-pack parameter "
8760 "%qD of concept %qD", parm
, in_decl
);
8761 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8768 return error_mark_node
;
8771 /* We need to evaluate the template arguments, even though this
8772 template-id may be nested within a "sizeof". */
8775 new_inner_args
= make_tree_vec (nparms
);
8776 new_args
= add_outermost_template_args (args
, new_inner_args
);
8777 int pack_adjust
= 0;
8778 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8783 /* Get the Ith template parameter. */
8784 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8786 if (parm
== error_mark_node
)
8788 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8792 /* Calculate the next argument. */
8793 if (arg_idx
< nargs
)
8794 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8798 if (template_parameter_pack_p (TREE_VALUE (parm
))
8799 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8800 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8802 /* Some arguments will be placed in the
8803 template parameter pack PARM. */
8804 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8805 inner_args
, arg_idx
,
8809 if (arg
== NULL_TREE
)
8811 /* We don't know how many args we have yet, just use the
8812 unconverted (and still packed) ones for now. */
8813 new_inner_args
= orig_inner_args
;
8818 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8820 /* Store this argument. */
8821 if (arg
== error_mark_node
)
8824 /* We are done with all of the arguments. */
8830 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8831 arg_idx
+= pack_adjust
;
8832 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8835 fixed_pack_adjust
+= pack_adjust
;
8843 if (PACK_EXPANSION_P (arg
))
8845 /* "If every valid specialization of a variadic template
8846 requires an empty template parameter pack, the template is
8847 ill-formed, no diagnostic required." So check that the
8848 pattern works with this parameter. */
8849 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8850 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8854 if (conv
== error_mark_node
)
8856 if (complain
& tf_error
)
8857 inform (input_location
, "so any instantiation with a "
8858 "non-empty parameter pack would be ill-formed");
8861 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8862 /* Recover from missing typename. */
8863 TREE_VEC_ELT (inner_args
, arg_idx
)
8864 = make_pack_expansion (conv
, complain
);
8866 /* We don't know how many args we have yet, just
8867 use the unconverted ones for now. */
8868 new_inner_args
= inner_args
;
8873 else if (require_all_args
)
8875 /* There must be a default arg in this case. */
8876 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8878 /* The position of the first default template argument,
8879 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8881 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8883 arg_idx
- pack_adjust
);
8888 if (arg
== error_mark_node
)
8890 if (complain
& tf_error
)
8891 error ("template argument %d is invalid", arg_idx
+ 1);
8895 /* This can occur if there was an error in the template
8896 parameter list itself (which we would already have
8897 reported) that we are trying to recover from, e.g., a class
8898 template with a parameter list such as
8899 template<typename..., typename> (cpp0x/variadic150.C). */
8902 /* This can also happen with a fixed parameter pack (71834). */
8903 if (arg_idx
>= nargs
)
8907 arg
= convert_template_argument (TREE_VALUE (parm
),
8908 arg
, new_args
, complain
,
8911 if (arg
== error_mark_node
)
8914 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8917 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8919 /* If we had fixed parameter packs, we didn't know how many arguments we
8920 actually needed earlier; now we do. */
8921 nparms
+= fixed_pack_adjust
;
8922 variadic_p
-= fixed_packs
;
8926 if (arg_idx
< nargs
)
8928 /* We had some pack expansion arguments that will only work if the packs
8929 are empty, but wait until instantiation time to complain.
8930 See variadic-ttp3.C. */
8932 /* Except that we can't provide empty packs to alias templates or
8933 concepts when there are no corresponding parameters. Basically,
8934 we can get here with this:
8936 template<typename T> concept C = true;
8938 template<typename... Args>
8942 When parsing C<Args...>, we try to form a concept check of
8943 C<?, Args...>. Without the extra check for substituting an empty
8944 pack past the last parameter, we can accept the check as valid.
8946 FIXME: This may be valid for alias templates (but I doubt it).
8948 FIXME: The error could be better also. */
8949 if (in_decl
&& concept_definition_p (in_decl
))
8951 if (complain
& tf_error
)
8952 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8953 "too many arguments");
8954 return error_mark_node
;
8957 int len
= nparms
+ (nargs
- arg_idx
);
8958 tree args
= make_tree_vec (len
);
8960 for (; i
< nparms
; ++i
)
8961 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8962 for (; i
< len
; ++i
, ++arg_idx
)
8963 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8964 arg_idx
- pack_adjust
);
8965 new_inner_args
= args
;
8970 gcc_assert (!(complain
& tf_error
) || seen_error ());
8971 return error_mark_node
;
8974 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8975 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8976 TREE_VEC_LENGTH (new_inner_args
));
8978 return new_inner_args
;
8981 /* Convert all template arguments to their appropriate types, and
8982 return a vector containing the innermost resulting template
8983 arguments. If any error occurs, return error_mark_node. Error and
8984 warning messages are not issued.
8986 Note that no function argument deduction is performed, and default
8987 arguments are used to fill in unspecified arguments. */
8989 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8991 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8994 /* Convert all template arguments to their appropriate type, and
8995 instantiate default arguments as needed. This returns a vector
8996 containing the innermost resulting template arguments, or
8997 error_mark_node if unsuccessful. */
8999 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9000 tsubst_flags_t complain
)
9002 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9005 /* Like coerce_template_parms. If PARMS represents all template
9006 parameters levels, this function returns a vector of vectors
9007 representing all the resulting argument levels. Note that in this
9008 case, only the innermost arguments are coerced because the
9009 outermost ones are supposed to have been coerced already.
9011 Otherwise, if PARMS represents only (the innermost) vector of
9012 parameters, this function returns a vector containing just the
9013 innermost resulting arguments. */
9016 coerce_innermost_template_parms (tree parms
,
9019 tsubst_flags_t complain
,
9020 bool require_all_args
,
9021 bool use_default_args
)
9023 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9024 int args_depth
= TMPL_ARGS_DEPTH (args
);
9027 if (parms_depth
> 1)
9029 coerced_args
= make_tree_vec (parms_depth
);
9033 for (level
= parms
, cur_depth
= parms_depth
;
9034 parms_depth
> 0 && level
!= NULL_TREE
;
9035 level
= TREE_CHAIN (level
), --cur_depth
)
9038 if (cur_depth
== args_depth
)
9039 l
= coerce_template_parms (TREE_VALUE (level
),
9040 args
, in_decl
, complain
,
9044 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9046 if (l
== error_mark_node
)
9047 return error_mark_node
;
9049 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9053 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9054 args
, in_decl
, complain
,
9057 return coerced_args
;
9060 /* Returns true if T is a wrapper to make a C++20 template parameter
9064 class_nttp_const_wrapper_p (tree t
)
9066 if (cxx_dialect
< cxx20
)
9068 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9069 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9070 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9073 /* Returns 1 if template args OT and NT are equivalent. */
9076 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9080 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9082 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9085 if (class_nttp_const_wrapper_p (nt
))
9086 nt
= TREE_OPERAND (nt
, 0);
9087 if (class_nttp_const_wrapper_p (ot
))
9088 ot
= TREE_OPERAND (ot
, 0);
9090 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9091 /* For member templates */
9092 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9093 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9094 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9095 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9096 PACK_EXPANSION_PATTERN (nt
))
9097 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9098 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9099 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9100 return cp_tree_equal (ot
, nt
);
9101 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9103 else if (TYPE_P (nt
) || TYPE_P (ot
))
9105 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9107 /* Don't treat an alias template specialization with dependent
9108 arguments as equivalent to its underlying type when used as a
9109 template argument; we need them to be distinct so that we
9110 substitute into the specialization arguments at instantiation
9111 time. And aliases can't be equivalent without being ==, so
9112 we don't need to look any deeper.
9114 During partial ordering, however, we need to treat them normally so
9115 that we can order uses of the same alias with different
9116 cv-qualification (79960). */
9118 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9121 return same_type_p (ot
, nt
);
9125 /* Try to treat a template non-type argument that has been converted
9126 to the parameter type as equivalent to one that hasn't yet. */
9127 for (enum tree_code code1
= TREE_CODE (ot
);
9128 CONVERT_EXPR_CODE_P (code1
)
9129 || code1
== NON_LVALUE_EXPR
;
9130 code1
= TREE_CODE (ot
))
9131 ot
= TREE_OPERAND (ot
, 0);
9133 for (enum tree_code code2
= TREE_CODE (nt
);
9134 CONVERT_EXPR_CODE_P (code2
)
9135 || code2
== NON_LVALUE_EXPR
;
9136 code2
= TREE_CODE (nt
))
9137 nt
= TREE_OPERAND (nt
, 0);
9139 return cp_tree_equal (ot
, nt
);
9143 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9144 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9145 NEWARG_PTR with the offending arguments if they are non-NULL. */
9148 comp_template_args (tree oldargs
, tree newargs
,
9149 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9154 if (oldargs
== newargs
)
9157 if (!oldargs
|| !newargs
)
9160 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9163 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9165 tree nt
= TREE_VEC_ELT (newargs
, i
);
9166 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9168 if (! template_args_equal (ot
, nt
, partial_order
))
9170 if (oldarg_ptr
!= NULL
)
9172 if (newarg_ptr
!= NULL
)
9181 comp_template_args_porder (tree oargs
, tree nargs
)
9183 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9186 /* Implement a freelist interface for objects of type T.
9188 Head is a separate object, rather than a regular member, so that we
9189 can define it as a GTY deletable pointer, which is highly
9190 desirable. A data member could be declared that way, but then the
9191 containing object would implicitly get GTY((user)), which would
9192 prevent us from instantiating freelists as global objects.
9193 Although this way we can create freelist global objects, they're
9194 such thin wrappers that instantiating temporaries at every use
9195 loses nothing and saves permanent storage for the freelist object.
9197 Member functions next, anew, poison and reinit have default
9198 implementations that work for most of the types we're interested
9199 in, but if they don't work for some type, they should be explicitly
9200 specialized. See the comments before them for requirements, and
9201 the example specializations for the tree_list_freelist. */
9202 template <typename T
>
9205 /* Return the next object in a chain. We could just do type
9206 punning, but if we access the object with its underlying type, we
9207 avoid strict-aliasing trouble. This needs only work between
9208 poison and reinit. */
9209 static T
*&next (T
*obj
) { return obj
->next
; }
9211 /* Return a newly allocated, uninitialized or minimally-initialized
9212 object of type T. Any initialization performed by anew should
9213 either remain across the life of the object and the execution of
9214 poison, or be redone by reinit. */
9215 static T
*anew () { return ggc_alloc
<T
> (); }
9217 /* Optionally scribble all over the bits holding the object, so that
9218 they become (mostly?) uninitialized memory. This is called while
9219 preparing to make the object part of the free list. */
9220 static void poison (T
*obj
) {
9221 T
*p ATTRIBUTE_UNUSED
= obj
;
9222 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9224 #ifdef ENABLE_GC_CHECKING
9225 /* Poison the data, to indicate the data is garbage. */
9226 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9227 memset (p
, 0xa5, sizeof (*p
));
9229 /* Let valgrind know the object is free. */
9230 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9232 /* Let valgrind know the next portion of the object is available,
9233 but uninitialized. */
9234 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9237 /* Bring an object that underwent at least one lifecycle after anew
9238 and before the most recent free and poison, back to a usable
9239 state, reinitializing whatever is needed for it to be
9240 functionally equivalent to an object just allocated and returned
9241 by anew. This may poison or clear the next field, used by
9242 freelist housekeeping after poison was called. */
9243 static void reinit (T
*obj
) {
9244 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9246 #ifdef ENABLE_GC_CHECKING
9247 memset (q
, 0xa5, sizeof (*q
));
9249 /* Let valgrind know the entire object is available, but
9251 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9254 /* Reference a GTY-deletable pointer that points to the first object
9255 in the free list proper. */
9258 /* Construct a freelist object chaining objects off of HEAD. */
9259 freelist (T
*&head
) : head(head
) {}
9261 /* Add OBJ to the free object list. The former head becomes OBJ's
9270 /* Take an object from the free list, if one is available, or
9271 allocate a new one. Objects taken from the free list should be
9272 regarded as filled with garbage, except for bits that are
9273 configured to be preserved across free and alloc. */
9288 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9289 want to allocate a TREE_LIST using the usual interface, and ensure
9290 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9291 build_tree_list logic in reinit, so this could go out of sync. */
9294 freelist
<tree_node
>::next (tree obj
)
9296 return TREE_CHAIN (obj
);
9300 freelist
<tree_node
>::anew ()
9302 return build_tree_list (NULL
, NULL
);
9306 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9308 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9309 tree p ATTRIBUTE_UNUSED
= obj
;
9310 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9311 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9313 #ifdef ENABLE_GC_CHECKING
9314 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9316 /* Poison the data, to indicate the data is garbage. */
9317 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9318 memset (p
, 0xa5, size
);
9320 /* Let valgrind know the object is free. */
9321 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9322 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9323 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9324 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9326 #ifdef ENABLE_GC_CHECKING
9327 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9328 /* Keep TREE_CHAIN functional. */
9329 TREE_SET_CODE (obj
, TREE_LIST
);
9331 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9336 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9338 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9340 #ifdef ENABLE_GC_CHECKING
9341 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9342 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9343 memset (obj
, 0, sizeof (tree_list
));
9346 /* Let valgrind know the entire object is available, but
9348 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9350 #ifdef ENABLE_GC_CHECKING
9351 TREE_SET_CODE (obj
, TREE_LIST
);
9353 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9357 /* Point to the first object in the TREE_LIST freelist. */
9358 static GTY((deletable
)) tree tree_list_freelist_head
;
9359 /* Return the/an actual TREE_LIST freelist. */
9360 static inline freelist
<tree_node
>
9361 tree_list_freelist ()
9363 return tree_list_freelist_head
;
9366 /* Point to the first object in the tinst_level freelist. */
9367 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9368 /* Return the/an actual tinst_level freelist. */
9369 static inline freelist
<tinst_level
>
9370 tinst_level_freelist ()
9372 return tinst_level_freelist_head
;
9375 /* Point to the first object in the pending_template freelist. */
9376 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9377 /* Return the/an actual pending_template freelist. */
9378 static inline freelist
<pending_template
>
9379 pending_template_freelist ()
9381 return pending_template_freelist_head
;
9384 /* Build the TREE_LIST object out of a split list, store it
9385 permanently, and return it. */
9387 tinst_level::to_list ()
9389 gcc_assert (split_list_p ());
9390 tree ret
= tree_list_freelist ().alloc ();
9391 TREE_PURPOSE (ret
) = tldcl
;
9392 TREE_VALUE (ret
) = targs
;
9395 gcc_assert (tree_list_p ());
9399 const unsigned short tinst_level::refcount_infinity
;
9401 /* Increment OBJ's refcount unless it is already infinite. */
9402 static tinst_level
*
9403 inc_refcount_use (tinst_level
*obj
)
9405 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9410 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9412 tinst_level::free (tinst_level
*obj
)
9414 if (obj
->tree_list_p ())
9415 tree_list_freelist ().free (obj
->get_node ());
9416 tinst_level_freelist ().free (obj
);
9419 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9420 OBJ's DECL and OBJ, and start over with the tinst_level object that
9421 used to be referenced by OBJ's NEXT. */
9423 dec_refcount_use (tinst_level
*obj
)
9426 && obj
->refcount
!= tinst_level::refcount_infinity
9427 && !--obj
->refcount
)
9429 tinst_level
*next
= obj
->next
;
9430 tinst_level::free (obj
);
9435 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9436 and of the former PTR. Omitting the second argument is equivalent
9437 to passing (T*)NULL; this is allowed because passing the
9438 zero-valued integral constant NULL confuses type deduction and/or
9439 overload resolution. */
9440 template <typename T
>
9442 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9445 ptr
= inc_refcount_use (obj
);
9446 dec_refcount_use (save
);
9450 add_pending_template (tree d
)
9452 tree ti
= (TYPE_P (d
)
9453 ? CLASSTYPE_TEMPLATE_INFO (d
)
9454 : DECL_TEMPLATE_INFO (d
));
9455 struct pending_template
*pt
;
9458 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9461 /* We are called both from instantiate_decl, where we've already had a
9462 tinst_level pushed, and instantiate_template, where we haven't.
9464 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9465 level
= !current_tinst_level
9466 || current_tinst_level
->maybe_get_node () != d
;
9469 push_tinst_level (d
);
9471 pt
= pending_template_freelist ().alloc ();
9474 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9475 if (last_pending_template
)
9476 last_pending_template
->next
= pt
;
9478 pending_templates
= pt
;
9480 last_pending_template
= pt
;
9482 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9489 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9490 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9491 documentation for TEMPLATE_ID_EXPR. */
9494 lookup_template_function (tree fns
, tree arglist
)
9496 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9497 return error_mark_node
;
9499 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9501 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9503 error ("%q#D is not a function template", fns
);
9504 return error_mark_node
;
9507 if (BASELINK_P (fns
))
9509 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9511 BASELINK_FUNCTIONS (fns
),
9516 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9519 /* Within the scope of a template class S<T>, the name S gets bound
9520 (in build_self_reference) to a TYPE_DECL for the class, not a
9521 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9522 or one of its enclosing classes, and that type is a template,
9523 return the associated TEMPLATE_DECL. Otherwise, the original
9526 Also handle the case when DECL is a TREE_LIST of ambiguous
9527 injected-class-names from different bases. */
9530 maybe_get_template_decl_from_type_decl (tree decl
)
9532 if (decl
== NULL_TREE
)
9535 /* DR 176: A lookup that finds an injected-class-name (10.2
9536 [class.member.lookup]) can result in an ambiguity in certain cases
9537 (for example, if it is found in more than one base class). If all of
9538 the injected-class-names that are found refer to specializations of
9539 the same class template, and if the name is followed by a
9540 template-argument-list, the reference refers to the class template
9541 itself and not a specialization thereof, and is not ambiguous. */
9542 if (TREE_CODE (decl
) == TREE_LIST
)
9544 tree t
, tmpl
= NULL_TREE
;
9545 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9547 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9550 else if (tmpl
!= elt
)
9553 if (tmpl
&& t
== NULL_TREE
)
9559 return (decl
!= NULL_TREE
9560 && DECL_SELF_REFERENCE_P (decl
)
9561 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9562 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9565 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9566 parameters, find the desired type.
9568 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9570 IN_DECL, if non-NULL, is the template declaration we are trying to
9573 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9574 the class we are looking up.
9576 Issue error and warning messages under control of COMPLAIN.
9578 If the template class is really a local class in a template
9579 function, then the FUNCTION_CONTEXT is the function in which it is
9582 ??? Note that this function is currently called *twice* for each
9583 template-id: the first time from the parser, while creating the
9584 incomplete type (finish_template_type), and the second type during the
9585 real instantiation (instantiate_template_class). This is surely something
9586 that we want to avoid. It also causes some problems with argument
9587 coercion (see convert_nontype_argument for more information on this). */
9590 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9591 int entering_scope
, tsubst_flags_t complain
)
9593 tree templ
= NULL_TREE
, parmlist
;
9600 if (identifier_p (d1
))
9602 tree value
= innermost_non_namespace_value (d1
);
9603 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9608 push_decl_namespace (context
);
9609 templ
= lookup_name (d1
);
9610 templ
= maybe_get_template_decl_from_type_decl (templ
);
9612 pop_decl_namespace ();
9615 context
= DECL_CONTEXT (templ
);
9617 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9619 tree type
= TREE_TYPE (d1
);
9621 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9622 an implicit typename for the second A. Deal with it. */
9623 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9624 type
= TREE_TYPE (type
);
9626 if (CLASSTYPE_TEMPLATE_INFO (type
))
9628 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9629 d1
= DECL_NAME (templ
);
9632 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9633 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9635 templ
= TYPE_TI_TEMPLATE (d1
);
9636 d1
= DECL_NAME (templ
);
9638 else if (DECL_TYPE_TEMPLATE_P (d1
))
9641 d1
= DECL_NAME (templ
);
9642 context
= DECL_CONTEXT (templ
);
9644 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9647 d1
= DECL_NAME (templ
);
9650 /* Issue an error message if we didn't find a template. */
9653 if (complain
& tf_error
)
9654 error ("%qT is not a template", d1
);
9655 return error_mark_node
;
9658 if (TREE_CODE (templ
) != TEMPLATE_DECL
9659 /* Make sure it's a user visible template, if it was named by
9661 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9662 && !PRIMARY_TEMPLATE_P (templ
)))
9664 if (complain
& tf_error
)
9666 error ("non-template type %qT used as a template", d1
);
9668 error ("for template declaration %q+D", in_decl
);
9670 return error_mark_node
;
9673 complain
&= ~tf_user
;
9675 /* An alias that just changes the name of a template is equivalent to the
9676 other template, so if any of the arguments are pack expansions, strip
9677 the alias to avoid problems with a pack expansion passed to a non-pack
9678 alias template parameter (DR 1430). */
9679 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9680 templ
= get_underlying_template (templ
);
9682 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9685 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9686 if (arglist2
== error_mark_node
9687 || (!uses_template_parms (arglist2
)
9688 && check_instantiated_args (templ
, arglist2
, complain
)))
9689 return error_mark_node
;
9691 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9696 tree template_type
= TREE_TYPE (templ
);
9699 tree found
= NULL_TREE
;
9702 int is_dependent_type
;
9703 int use_partial_inst_tmpl
= false;
9705 if (template_type
== error_mark_node
)
9706 /* An error occurred while building the template TEMPL, and a
9707 diagnostic has most certainly been emitted for that
9708 already. Let's propagate that error. */
9709 return error_mark_node
;
9711 gen_tmpl
= most_general_template (templ
);
9712 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9713 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9714 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9716 if (arg_depth
== 1 && parm_depth
> 1)
9718 /* We've been given an incomplete set of template arguments.
9721 template <class T> struct S1 {
9722 template <class U> struct S2 {};
9723 template <class U> struct S2<U*> {};
9726 we will be called with an ARGLIST of `U*', but the
9727 TEMPLATE will be `template <class T> template
9728 <class U> struct S1<T>::S2'. We must fill in the missing
9730 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9731 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9732 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9735 /* Now we should have enough arguments. */
9736 gcc_assert (parm_depth
== arg_depth
);
9738 /* From here on, we're only interested in the most general
9741 /* Calculate the BOUND_ARGS. These will be the args that are
9742 actually tsubst'd into the definition to create the
9744 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9746 /*require_all_args=*/true,
9747 /*use_default_args=*/true);
9749 if (arglist
== error_mark_node
)
9750 /* We were unable to bind the arguments. */
9751 return error_mark_node
;
9753 /* In the scope of a template class, explicit references to the
9754 template class refer to the type of the template, not any
9755 instantiation of it. For example, in:
9757 template <class T> class C { void f(C<T>); }
9759 the `C<T>' is just the same as `C'. Outside of the
9760 class, however, such a reference is an instantiation. */
9762 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9763 || currently_open_class (template_type
))
9765 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9767 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9768 return template_type
;
9771 /* If we already have this specialization, return it. */
9772 elt
.tmpl
= gen_tmpl
;
9774 elt
.spec
= NULL_TREE
;
9775 hash
= spec_hasher::hash (&elt
);
9776 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9781 /* If the template's constraints are not satisfied,
9782 then we cannot form a valid type.
9784 Note that the check is deferred until after the hash
9785 lookup. This prevents redundant checks on previously
9786 instantiated specializations. */
9788 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9789 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9791 if (complain
& tf_error
)
9793 auto_diagnostic_group d
;
9794 error ("template constraint failure for %qD", gen_tmpl
);
9795 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9797 return error_mark_node
;
9800 is_dependent_type
= uses_template_parms (arglist
);
9802 /* If the deduced arguments are invalid, then the binding
9804 if (!is_dependent_type
9805 && check_instantiated_args (gen_tmpl
,
9806 INNERMOST_TEMPLATE_ARGS (arglist
),
9808 return error_mark_node
;
9810 if (!is_dependent_type
9811 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9812 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9813 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9815 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9816 DECL_NAME (gen_tmpl
),
9817 /*tag_scope=*/ts_global
);
9821 context
= DECL_CONTEXT (gen_tmpl
);
9822 if (context
&& TYPE_P (context
))
9824 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9825 context
= complete_type (context
);
9828 context
= tsubst (context
, arglist
, complain
, in_decl
);
9830 if (context
== error_mark_node
)
9831 return error_mark_node
;
9834 context
= global_namespace
;
9836 /* Create the type. */
9837 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9839 /* The user referred to a specialization of an alias
9840 template represented by GEN_TMPL.
9842 [temp.alias]/2 says:
9844 When a template-id refers to the specialization of an
9845 alias template, it is equivalent to the associated
9846 type obtained by substitution of its
9847 template-arguments for the template-parameters in the
9848 type-id of the alias template. */
9850 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9851 /* Note that the call above (by indirectly calling
9852 register_specialization in tsubst_decl) registers the
9853 TYPE_DECL representing the specialization of the alias
9854 template. So next time someone substitutes ARGLIST for
9855 the template parms into the alias template (GEN_TMPL),
9856 she'll get that TYPE_DECL back. */
9858 if (t
== error_mark_node
)
9861 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9863 if (!is_dependent_type
)
9865 set_current_access_from_decl (TYPE_NAME (template_type
));
9866 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9867 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9868 arglist
, complain
, in_decl
),
9869 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9870 arglist
, complain
, in_decl
),
9871 SCOPED_ENUM_P (template_type
), NULL
);
9873 if (t
== error_mark_node
)
9878 /* We don't want to call start_enum for this type, since
9879 the values for the enumeration constants may involve
9880 template parameters. And, no one should be interested
9881 in the enumeration constants for such a type. */
9882 t
= cxx_make_type (ENUMERAL_TYPE
);
9883 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9885 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9886 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9887 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9889 else if (CLASS_TYPE_P (template_type
))
9891 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9892 instantiated here. */
9893 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9895 t
= make_class_type (TREE_CODE (template_type
));
9896 CLASSTYPE_DECLARED_CLASS (t
)
9897 = CLASSTYPE_DECLARED_CLASS (template_type
);
9898 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9900 /* A local class. Make sure the decl gets registered properly. */
9901 if (context
== current_function_decl
)
9902 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9904 return error_mark_node
;
9906 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9907 /* This instantiation is another name for the primary
9908 template type. Set the TYPE_CANONICAL field
9910 TYPE_CANONICAL (t
) = template_type
;
9911 else if (any_template_arguments_need_structural_equality_p (arglist
))
9912 /* Some of the template arguments require structural
9913 equality testing, so this template class requires
9914 structural equality testing. */
9915 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9920 /* If we called start_enum or pushtag above, this information
9921 will already be set up. */
9922 type_decl
= TYPE_NAME (t
);
9925 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9927 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9928 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9929 DECL_SOURCE_LOCATION (type_decl
)
9930 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9933 if (CLASS_TYPE_P (template_type
))
9935 TREE_PRIVATE (type_decl
)
9936 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9937 TREE_PROTECTED (type_decl
)
9938 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9939 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9941 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9942 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9946 if (OVERLOAD_TYPE_P (t
)
9947 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9949 static const char *tags
[] = {"abi_tag", "may_alias"};
9951 for (unsigned ix
= 0; ix
!= 2; ix
++)
9954 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9958 = tree_cons (TREE_PURPOSE (attributes
),
9959 TREE_VALUE (attributes
),
9960 TYPE_ATTRIBUTES (t
));
9964 /* Let's consider the explicit specialization of a member
9965 of a class template specialization that is implicitly instantiated,
9970 template<class U> struct M {}; //#0
9975 struct S<int>::M<char> //#1
9979 [temp.expl.spec]/4 says this is valid.
9981 In this case, when we write:
9984 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9987 When we encounter #1, we want to store the partial instantiation
9988 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9990 For all cases other than this "explicit specialization of member of a
9991 class template", we just want to store the most general template into
9992 the CLASSTYPE_TI_TEMPLATE of M.
9994 This case of "explicit specialization of member of a class template"
9996 1/ the enclosing class is an instantiation of, and therefore not
9997 the same as, the context of the most general template, and
9998 2/ we aren't looking at the partial instantiation itself, i.e.
9999 the innermost arguments are not the same as the innermost parms of
10000 the most general template.
10002 So it's only when 1/ and 2/ happens that we want to use the partial
10003 instantiation of the member template in lieu of its most general
10006 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10007 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10008 /* the enclosing class must be an instantiation... */
10009 && CLASS_TYPE_P (context
)
10010 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10012 TREE_VEC_LENGTH (arglist
)--;
10013 ++processing_template_decl
;
10014 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10015 tree partial_inst_args
=
10016 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10017 arglist
, complain
, NULL_TREE
);
10018 --processing_template_decl
;
10019 TREE_VEC_LENGTH (arglist
)++;
10020 if (partial_inst_args
== error_mark_node
)
10021 return error_mark_node
;
10022 use_partial_inst_tmpl
=
10023 /*...and we must not be looking at the partial instantiation
10025 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10026 partial_inst_args
);
10029 if (!use_partial_inst_tmpl
)
10030 /* This case is easy; there are no member templates involved. */
10034 /* This is a full instantiation of a member template. Find
10035 the partial instantiation of which this is an instance. */
10037 /* Temporarily reduce by one the number of levels in the ARGLIST
10038 so as to avoid comparing the last set of arguments. */
10039 TREE_VEC_LENGTH (arglist
)--;
10040 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10041 TREE_VEC_LENGTH (arglist
)++;
10042 /* FOUND is either a proper class type, or an alias
10043 template specialization. In the later case, it's a
10044 TYPE_DECL, resulting from the substituting of arguments
10045 for parameters in the TYPE_DECL of the alias template
10046 done earlier. So be careful while getting the template
10048 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10050 : (TREE_CODE (found
) == TYPE_DECL
10051 ? DECL_TI_TEMPLATE (found
)
10052 : CLASSTYPE_TI_TEMPLATE (found
)));
10054 if (DECL_CLASS_TEMPLATE_P (found
)
10055 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10057 /* If this partial instantiation is specialized, we want to
10058 use it for hash table lookup. */
10060 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10061 hash
= spec_hasher::hash (&elt
);
10065 /* Build template info for the new specialization. This can
10066 overwrite the existing TEMPLATE_INFO for T (that points to
10067 its instantiated TEMPLATE_DECL), with this one that points to
10068 the most general template, but that's what we want. */
10070 if (TYPE_ALIAS_P (t
))
10072 /* This should already have been constructed during
10073 instantiation of the alias decl. */
10074 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10075 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
)
10076 && TI_TEMPLATE (ti
) == found
);
10079 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10082 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10083 gcc_checking_assert (*slot
== NULL
);
10084 entry
= ggc_alloc
<spec_entry
> ();
10088 /* Note this use of the partial instantiation so we can check it
10089 later in maybe_process_partial_specialization. */
10090 DECL_TEMPLATE_INSTANTIATIONS (found
)
10091 = tree_cons (arglist
, t
,
10092 DECL_TEMPLATE_INSTANTIATIONS (found
));
10094 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10095 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10096 /* Now that the type has been registered on the instantiations
10097 list, we set up the enumerators. Because the enumeration
10098 constants may involve the enumeration type itself, we make
10099 sure to register the type first, and then create the
10100 constants. That way, doing tsubst_expr for the enumeration
10101 constants won't result in recursive calls here; we'll find
10102 the instantiation and exit above. */
10103 tsubst_enum (template_type
, t
, arglist
);
10105 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10106 /* If the type makes use of template parameters, the
10107 code that generates debugging information will crash. */
10108 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10110 /* Possibly limit visibility based on template args. */
10111 TREE_PUBLIC (type_decl
) = 1;
10112 determine_visibility (type_decl
);
10114 inherit_targ_abi_tags (t
);
10120 /* Wrapper for lookup_template_class_1. */
10123 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10124 int entering_scope
, tsubst_flags_t complain
)
10127 timevar_push (TV_TEMPLATE_INST
);
10128 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10129 entering_scope
, complain
);
10130 timevar_pop (TV_TEMPLATE_INST
);
10134 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10137 lookup_template_variable (tree templ
, tree arglist
)
10139 if (flag_concepts
&& variable_concept_p (templ
))
10140 return build_concept_check (templ
, arglist
, tf_none
);
10142 /* The type of the expression is NULL_TREE since the template-id could refer
10143 to an explicit or partial specialization. */
10144 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10147 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10150 finish_template_variable (tree var
, tsubst_flags_t complain
)
10152 tree templ
= TREE_OPERAND (var
, 0);
10153 tree arglist
= TREE_OPERAND (var
, 1);
10155 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10156 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10158 templ
= most_general_template (templ
);
10159 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10160 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10162 /*use_default*/true);
10164 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10166 if (complain
& tf_error
)
10168 auto_diagnostic_group d
;
10169 error ("use of invalid variable template %qE", var
);
10170 diagnose_constraints (location_of (var
), templ
, arglist
);
10172 return error_mark_node
;
10175 return instantiate_template (templ
, arglist
, complain
);
10178 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10179 TARGS template args, and instantiate it if it's not dependent. */
10182 lookup_and_finish_template_variable (tree templ
, tree targs
,
10183 tsubst_flags_t complain
)
10185 templ
= lookup_template_variable (templ
, targs
);
10186 if (!any_dependent_template_arguments_p (targs
))
10188 templ
= finish_template_variable (templ
, complain
);
10192 return convert_from_reference (templ
);
10196 struct pair_fn_data
10201 /* True when we should also visit template parameters that occur in
10202 non-deduced contexts. */
10203 bool include_nondeduced_p
;
10204 hash_set
<tree
> *visited
;
10207 /* Called from for_each_template_parm via walk_tree. */
10210 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10213 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10214 tree_fn_t fn
= pfd
->fn
;
10215 void *data
= pfd
->data
;
10216 tree result
= NULL_TREE
;
10218 #define WALK_SUBTREE(NODE) \
10221 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10222 pfd->include_nondeduced_p, \
10224 if (result) goto out; \
10228 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10232 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10233 WALK_SUBTREE (TYPE_CONTEXT (t
));
10235 switch (TREE_CODE (t
))
10238 if (TYPE_PTRMEMFUNC_P (t
))
10240 /* Fall through. */
10243 case ENUMERAL_TYPE
:
10244 if (!TYPE_TEMPLATE_INFO (t
))
10245 *walk_subtrees
= 0;
10247 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10251 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10252 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10256 /* Since we're not going to walk subtrees, we have to do this
10257 explicitly here. */
10258 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10259 /* Fall through. */
10261 case FUNCTION_TYPE
:
10262 /* Check the return type. */
10263 WALK_SUBTREE (TREE_TYPE (t
));
10265 /* Check the parameter types. Since default arguments are not
10266 instantiated until they are needed, the TYPE_ARG_TYPES may
10267 contain expressions that involve template parameters. But,
10268 no-one should be looking at them yet. And, once they're
10269 instantiated, they don't contain template parameters, so
10270 there's no point in looking at them then, either. */
10274 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10275 WALK_SUBTREE (TREE_VALUE (parm
));
10277 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10278 want walk_tree walking into them itself. */
10279 *walk_subtrees
= 0;
10282 if (flag_noexcept_type
)
10284 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10286 WALK_SUBTREE (TREE_PURPOSE (spec
));
10291 case DECLTYPE_TYPE
:
10292 case UNDERLYING_TYPE
:
10293 if (pfd
->include_nondeduced_p
10294 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10296 pfd
->include_nondeduced_p
,
10298 return error_mark_node
;
10299 *walk_subtrees
= false;
10302 case FUNCTION_DECL
:
10304 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10305 WALK_SUBTREE (DECL_TI_ARGS (t
));
10306 /* Fall through. */
10310 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10311 WALK_SUBTREE (DECL_INITIAL (t
));
10312 if (DECL_CONTEXT (t
)
10313 && pfd
->include_nondeduced_p
)
10314 WALK_SUBTREE (DECL_CONTEXT (t
));
10317 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10318 /* Record template parameters such as `T' inside `TT<T>'. */
10319 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10320 /* Fall through. */
10322 case TEMPLATE_TEMPLATE_PARM
:
10323 case TEMPLATE_TYPE_PARM
:
10324 case TEMPLATE_PARM_INDEX
:
10325 if (fn
&& (*fn
)(t
, data
))
10331 case TEMPLATE_DECL
:
10332 /* A template template parameter is encountered. */
10333 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10334 WALK_SUBTREE (TREE_TYPE (t
));
10336 /* Already substituted template template parameter */
10337 *walk_subtrees
= 0;
10340 case TYPENAME_TYPE
:
10341 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10342 partial instantiation. */
10343 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10344 *walk_subtrees
= 0;
10348 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10349 && pfd
->include_nondeduced_p
)
10350 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10354 case COMPONENT_REF
:
10355 /* If there's no type, then this thing must be some expression
10356 involving template parameters. */
10357 if (!fn
&& !TREE_TYPE (t
))
10358 return error_mark_node
;
10363 case IMPLICIT_CONV_EXPR
:
10364 case REINTERPRET_CAST_EXPR
:
10365 case CONST_CAST_EXPR
:
10366 case STATIC_CAST_EXPR
:
10367 case DYNAMIC_CAST_EXPR
:
10371 case PSEUDO_DTOR_EXPR
:
10373 return error_mark_node
;
10377 if (pfd
->include_nondeduced_p
)
10378 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10381 case REQUIRES_EXPR
:
10384 return error_mark_node
;
10386 /* Recursively walk the type of each constraint variable. */
10387 tree p
= TREE_OPERAND (t
, 0);
10390 WALK_SUBTREE (TREE_TYPE (p
));
10391 p
= TREE_CHAIN (p
);
10400 #undef WALK_SUBTREE
10402 /* We didn't find any template parameters we liked. */
10407 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10408 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10409 call FN with the parameter and the DATA.
10410 If FN returns nonzero, the iteration is terminated, and
10411 for_each_template_parm returns 1. Otherwise, the iteration
10412 continues. If FN never returns a nonzero value, the value
10413 returned by for_each_template_parm is 0. If FN is NULL, it is
10414 considered to be the function which always returns 1.
10416 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10417 parameters that occur in non-deduced contexts. When false, only
10418 visits those template parameters that can be deduced. */
10421 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10422 hash_set
<tree
> *visited
,
10423 bool include_nondeduced_p
,
10426 struct pair_fn_data pfd
;
10431 pfd
.any_fn
= any_fn
;
10433 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10435 /* Walk the tree. (Conceptually, we would like to walk without
10436 duplicates, but for_each_template_parm_r recursively calls
10437 for_each_template_parm, so we would need to reorganize a fair
10438 bit to use walk_tree_without_duplicates, so we keep our own
10441 pfd
.visited
= visited
;
10443 pfd
.visited
= new hash_set
<tree
>;
10444 result
= cp_walk_tree (&t
,
10445 for_each_template_parm_r
,
10452 delete pfd
.visited
;
10459 struct find_template_parameter_info
10461 explicit find_template_parameter_info (tree ctx_parms
)
10462 : parm_list (NULL_TREE
),
10463 ctx_parms (ctx_parms
),
10464 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10467 hash_set
<tree
> visited
;
10468 hash_set
<tree
> parms
;
10474 /* Appends the declaration of T to the list in DATA. */
10477 keep_template_parm (tree t
, void* data
)
10479 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10481 /* Template parameters declared within the expression are not part of
10482 the parameter mapping. For example, in this concept:
10484 template<typename T>
10485 concept C = requires { <expr> } -> same_as<int>;
10487 the return specifier same_as<int> declares a new decltype parameter
10488 that must not be part of the parameter mapping. The same is true
10489 for generic lambda parameters, lambda template parameters, etc. */
10492 template_parm_level_and_index (t
, &level
, &index
);
10493 if (level
> ftpi
->max_depth
)
10496 /* Arguments like const T yield parameters like const T. This means that
10497 a template-id like X<T, const T> would yield two distinct parameters:
10498 T and const T. Adjust types to their unqualified versions. */
10500 t
= TYPE_MAIN_VARIANT (t
);
10501 if (!ftpi
->parms
.add (t
))
10502 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10507 /* Ensure that we recursively examine certain terms that are not normally
10508 visited in for_each_template_parm_r. */
10511 any_template_parm_r (tree t
, void *data
)
10513 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10515 #define WALK_SUBTREE(NODE) \
10518 for_each_template_parm (NODE, keep_template_parm, data, \
10519 &ftpi->visited, true, \
10520 any_template_parm_r); \
10524 /* A mention of a member alias/typedef is a use of all of its template
10525 arguments, including those from the enclosing class, so we don't use
10526 alias_template_specialization_p here. */
10527 if (TYPE_P (t
) && typedef_variant_p (t
))
10528 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10529 WALK_SUBTREE (TI_ARGS (tinfo
));
10531 switch (TREE_CODE (t
))
10533 case TEMPLATE_TYPE_PARM
:
10534 /* Type constraints of a placeholder type may contain parameters. */
10536 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10537 WALK_SUBTREE (constr
);
10540 case TEMPLATE_ID_EXPR
:
10541 /* Search through references to variable templates. */
10542 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10543 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10546 case TEMPLATE_PARM_INDEX
:
10548 /* A parameter or constraint variable may also depend on a template
10549 parameter without explicitly naming it. */
10550 WALK_SUBTREE (TREE_TYPE (t
));
10553 case TEMPLATE_DECL
:
10555 /* If T is a member template that shares template parameters with
10556 ctx_parms, we need to mark all those parameters for mapping. */
10557 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10558 tree cparms
= ftpi
->ctx_parms
;
10559 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10560 dparms
= TREE_CHAIN (dparms
);
10561 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10562 cparms
= TREE_CHAIN (cparms
);
10564 && (TREE_TYPE (TREE_VALUE (dparms
))
10565 != TREE_TYPE (TREE_VALUE (cparms
))))
10566 dparms
= TREE_CHAIN (dparms
),
10567 cparms
= TREE_CHAIN (cparms
);
10570 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10571 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10572 for (int i
= 0; i
< ddepth
; ++i
)
10573 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10580 /* Look in the parms and body. */
10581 tree fn
= lambda_function (t
);
10582 WALK_SUBTREE (TREE_TYPE (fn
));
10583 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10587 case IDENTIFIER_NODE
:
10588 if (IDENTIFIER_CONV_OP_P (t
))
10589 /* The conversion-type-id of a conversion operator may be dependent. */
10590 WALK_SUBTREE (TREE_TYPE (t
));
10597 /* Keep walking. */
10601 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10602 are the template parameters in scope. */
10605 find_template_parameters (tree t
, tree ctx_parms
)
10610 find_template_parameter_info
ftpi (ctx_parms
);
10611 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10612 /*include_nondeduced*/true, any_template_parm_r
);
10613 return ftpi
.parm_list
;
10616 /* Returns true if T depends on any template parameter. */
10619 uses_template_parms (tree t
)
10621 if (t
== NULL_TREE
)
10625 int saved_processing_template_decl
;
10627 saved_processing_template_decl
= processing_template_decl
;
10628 if (!saved_processing_template_decl
)
10629 processing_template_decl
= 1;
10631 dependent_p
= dependent_type_p (t
);
10632 else if (TREE_CODE (t
) == TREE_VEC
)
10633 dependent_p
= any_dependent_template_arguments_p (t
);
10634 else if (TREE_CODE (t
) == TREE_LIST
)
10635 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10636 || uses_template_parms (TREE_CHAIN (t
)));
10637 else if (TREE_CODE (t
) == TYPE_DECL
)
10638 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10639 else if (t
== error_mark_node
)
10640 dependent_p
= false;
10642 dependent_p
= value_dependent_expression_p (t
);
10644 processing_template_decl
= saved_processing_template_decl
;
10646 return dependent_p
;
10649 /* Returns true iff current_function_decl is an incompletely instantiated
10650 template. Useful instead of processing_template_decl because the latter
10651 is set to 0 during instantiate_non_dependent_expr. */
10654 in_template_function (void)
10656 tree fn
= current_function_decl
;
10658 ++processing_template_decl
;
10659 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10660 && DECL_TEMPLATE_INFO (fn
)
10661 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10662 --processing_template_decl
;
10666 /* Returns true if T depends on any template parameter with level LEVEL. */
10669 uses_template_parms_level (tree t
, int level
)
10671 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10672 /*include_nondeduced_p=*/true);
10675 /* Returns true if the signature of DECL depends on any template parameter from
10676 its enclosing class. */
10679 uses_outer_template_parms (tree decl
)
10681 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10684 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10685 &depth
, NULL
, /*include_nondeduced_p=*/true))
10687 if (PRIMARY_TEMPLATE_P (decl
)
10688 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10689 (DECL_TEMPLATE_PARMS (decl
)),
10690 template_parm_outer_level
,
10691 &depth
, NULL
, /*include_nondeduced_p=*/true))
10693 tree ci
= get_constraints (decl
);
10695 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10696 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10697 &depth
, NULL
, /*nondeduced*/true))
10702 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10703 ill-formed translation unit, i.e. a variable or function that isn't
10704 usable in a constant expression. */
10707 neglectable_inst_p (tree d
)
10709 return (d
&& DECL_P (d
)
10710 && !undeduced_auto_decl (d
)
10711 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10712 : decl_maybe_constant_var_p (d
)));
10715 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10716 neglectable and instantiated from within an erroneous instantiation. */
10719 limit_bad_template_recursion (tree decl
)
10721 struct tinst_level
*lev
= current_tinst_level
;
10722 int errs
= errorcount
+ sorrycount
;
10723 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10726 for (; lev
; lev
= lev
->next
)
10727 if (neglectable_inst_p (lev
->maybe_get_node ()))
10730 return (lev
&& errs
> lev
->errors
);
10733 static int tinst_depth
;
10734 extern int max_tinst_depth
;
10737 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10739 /* We're starting to instantiate D; record the template instantiation context
10740 at LOC for diagnostics and to restore it later. */
10743 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10745 struct tinst_level
*new_level
;
10747 if (tinst_depth
>= max_tinst_depth
)
10749 /* Tell error.c not to try to instantiate any templates. */
10751 fatal_error (input_location
,
10752 "template instantiation depth exceeds maximum of %d"
10753 " (use %<-ftemplate-depth=%> to increase the maximum)",
10758 /* If the current instantiation caused problems, don't let it instantiate
10759 anything else. Do allow deduction substitution and decls usable in
10760 constant expressions. */
10761 if (!targs
&& limit_bad_template_recursion (tldcl
))
10763 /* Avoid no_linkage_errors and unused function warnings for this
10765 TREE_NO_WARNING (tldcl
) = 1;
10769 /* When not -quiet, dump template instantiations other than functions, since
10770 announce_function will take care of those. */
10771 if (!quiet_flag
&& !targs
10772 && TREE_CODE (tldcl
) != TREE_LIST
10773 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10774 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10776 new_level
= tinst_level_freelist ().alloc ();
10777 new_level
->tldcl
= tldcl
;
10778 new_level
->targs
= targs
;
10779 new_level
->locus
= loc
;
10780 new_level
->errors
= errorcount
+ sorrycount
;
10781 new_level
->next
= NULL
;
10782 new_level
->refcount
= 0;
10783 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10784 set_refcount_ptr (current_tinst_level
, new_level
);
10787 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10788 depth_reached
= tinst_depth
;
10793 /* We're starting substitution of TMPL<ARGS>; record the template
10794 substitution context for diagnostics and to restore it later. */
10797 push_tinst_level (tree tmpl
, tree args
)
10799 return push_tinst_level_loc (tmpl
, args
, input_location
);
10802 /* We're starting to instantiate D; record INPUT_LOCATION and the
10803 template instantiation context for diagnostics and to restore it
10807 push_tinst_level (tree d
)
10809 return push_tinst_level_loc (d
, input_location
);
10812 /* Likewise, but record LOC as the program location. */
10815 push_tinst_level_loc (tree d
, location_t loc
)
10817 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10818 return push_tinst_level_loc (d
, NULL
, loc
);
10821 /* We're done instantiating this template; return to the instantiation
10825 pop_tinst_level (void)
10827 /* Restore the filename and line number stashed away when we started
10828 this instantiation. */
10829 input_location
= current_tinst_level
->locus
;
10830 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10834 /* We're instantiating a deferred template; restore the template
10835 instantiation context in which the instantiation was requested, which
10836 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10839 reopen_tinst_level (struct tinst_level
*level
)
10841 struct tinst_level
*t
;
10844 for (t
= level
; t
; t
= t
->next
)
10847 set_refcount_ptr (current_tinst_level
, level
);
10848 pop_tinst_level ();
10849 if (current_tinst_level
)
10850 current_tinst_level
->errors
= errorcount
+sorrycount
;
10851 return level
->maybe_get_node ();
10854 /* Returns the TINST_LEVEL which gives the original instantiation
10857 struct tinst_level
*
10858 outermost_tinst_level (void)
10860 struct tinst_level
*level
= current_tinst_level
;
10862 while (level
->next
)
10863 level
= level
->next
;
10867 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10868 vector of template arguments, as for tsubst.
10870 Returns an appropriate tsubst'd friend declaration. */
10873 tsubst_friend_function (tree decl
, tree args
)
10877 if (TREE_CODE (decl
) == FUNCTION_DECL
10878 && DECL_TEMPLATE_INSTANTIATION (decl
)
10879 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10880 /* This was a friend declared with an explicit template
10881 argument list, e.g.:
10883 friend void f<>(T);
10885 to indicate that f was a template instantiation, not a new
10886 function declaration. Now, we have to figure out what
10887 instantiation of what template. */
10889 tree template_id
, arglist
, fns
;
10892 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10894 /* Friend functions are looked up in the containing namespace scope.
10895 We must enter that scope, to avoid finding member functions of the
10896 current class with same name. */
10897 push_nested_namespace (ns
);
10898 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10899 tf_warning_or_error
, NULL_TREE
,
10900 /*integral_constant_expression_p=*/false);
10901 pop_nested_namespace (ns
);
10902 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10903 tf_warning_or_error
, NULL_TREE
);
10904 template_id
= lookup_template_function (fns
, arglist
);
10906 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10907 tmpl
= determine_specialization (template_id
, new_friend
,
10909 /*need_member_template=*/0,
10910 TREE_VEC_LENGTH (args
),
10912 return instantiate_template (tmpl
, new_args
, tf_error
);
10915 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10916 if (new_friend
== error_mark_node
)
10917 return error_mark_node
;
10919 /* The NEW_FRIEND will look like an instantiation, to the
10920 compiler, but is not an instantiation from the point of view of
10921 the language. For example, we might have had:
10923 template <class T> struct S {
10924 template <class U> friend void f(T, U);
10927 Then, in S<int>, template <class U> void f(int, U) is not an
10928 instantiation of anything. */
10930 DECL_USE_TEMPLATE (new_friend
) = 0;
10931 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10933 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10934 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10935 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10937 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10938 match in decls_match. */
10939 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10940 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
10941 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
10942 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10945 /* The mangled name for the NEW_FRIEND is incorrect. The function
10946 is not a template instantiation and should not be mangled like
10947 one. Therefore, we forget the mangling here; we'll recompute it
10948 later if we need it. */
10949 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10951 SET_DECL_RTL (new_friend
, NULL
);
10952 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10955 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10960 /* We must save some information from NEW_FRIEND before calling
10961 duplicate decls since that function will free NEW_FRIEND if
10963 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10964 tree new_friend_result_template_info
= NULL_TREE
;
10965 bool new_friend_is_defn
=
10966 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10967 (template_for_substitution (new_friend
)))
10969 tree not_tmpl
= new_friend
;
10971 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10973 /* This declaration is a `primary' template. */
10974 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10976 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
10977 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
10980 /* Inside pushdecl_namespace_level, we will push into the
10981 current namespace. However, the friend function should go
10982 into the namespace of the template. */
10983 ns
= decl_namespace_context (new_friend
);
10984 push_nested_namespace (ns
);
10985 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10986 pop_nested_namespace (ns
);
10988 if (old_decl
== error_mark_node
)
10989 return error_mark_node
;
10991 if (old_decl
!= new_friend
)
10993 /* This new friend declaration matched an existing
10994 declaration. For example, given:
10996 template <class T> void f(T);
10997 template <class U> class C {
10998 template <class T> friend void f(T) {}
11001 the friend declaration actually provides the definition
11002 of `f', once C has been instantiated for some type. So,
11003 old_decl will be the out-of-class template declaration,
11004 while new_friend is the in-class definition.
11006 But, if `f' was called before this point, the
11007 instantiation of `f' will have DECL_TI_ARGS corresponding
11008 to `T' but not to `U', references to which might appear
11009 in the definition of `f'. Previously, the most general
11010 template for an instantiation of `f' was the out-of-class
11011 version; now it is the in-class version. Therefore, we
11012 run through all specialization of `f', adding to their
11013 DECL_TI_ARGS appropriately. In particular, they need a
11014 new set of outer arguments, corresponding to the
11015 arguments for this class instantiation.
11017 The same situation can arise with something like this:
11019 friend void f(int);
11020 template <class T> class C {
11021 friend void f(T) {}
11024 when `C<int>' is instantiated. Now, `f(int)' is defined
11027 if (!new_friend_is_defn
)
11028 /* On the other hand, if the in-class declaration does
11029 *not* provide a definition, then we don't want to alter
11030 existing definitions. We can just leave everything
11035 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11036 tree new_args
= TI_ARGS (new_friend_template_info
);
11038 /* Overwrite whatever template info was there before, if
11039 any, with the new template information pertaining to
11040 the declaration. */
11041 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11043 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11045 /* We should have called reregister_specialization in
11046 duplicate_decls. */
11047 gcc_assert (retrieve_specialization (new_template
,
11051 /* Instantiate it if the global has already been used. */
11052 if (DECL_ODR_USED (old_decl
))
11053 instantiate_decl (old_decl
, /*defer_ok=*/true,
11054 /*expl_inst_class_mem_p=*/false);
11060 /* Indicate that the old function template is a partial
11062 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11063 = new_friend_result_template_info
;
11065 gcc_assert (new_template
11066 == most_general_template (new_template
));
11067 gcc_assert (new_template
!= old_decl
);
11069 /* Reassign any specializations already in the hash table
11070 to the new more general template, and add the
11071 additional template args. */
11072 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11074 t
= TREE_CHAIN (t
))
11076 tree spec
= TREE_VALUE (t
);
11079 elt
.tmpl
= old_decl
;
11080 elt
.args
= DECL_TI_ARGS (spec
);
11081 elt
.spec
= NULL_TREE
;
11083 decl_specializations
->remove_elt (&elt
);
11085 DECL_TI_ARGS (spec
)
11086 = add_outermost_template_args (new_args
,
11087 DECL_TI_ARGS (spec
));
11089 register_specialization
11090 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11093 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11097 /* The information from NEW_FRIEND has been merged into OLD_DECL
11098 by duplicate_decls. */
11099 new_friend
= old_decl
;
11104 tree context
= DECL_CONTEXT (new_friend
);
11108 template <class T> class C {
11109 template <class U> friend void C1<U>::f (); // case 1
11110 friend void C2<T>::f (); // case 2
11112 we only need to make sure CONTEXT is a complete type for
11113 case 2. To distinguish between the two cases, we note that
11114 CONTEXT of case 1 remains dependent type after tsubst while
11115 this isn't true for case 2. */
11116 ++processing_template_decl
;
11117 dependent_p
= dependent_type_p (context
);
11118 --processing_template_decl
;
11121 && !complete_type_or_else (context
, NULL_TREE
))
11122 return error_mark_node
;
11124 if (COMPLETE_TYPE_P (context
))
11126 tree fn
= new_friend
;
11127 /* do_friend adds the TEMPLATE_DECL for any member friend
11128 template even if it isn't a member template, i.e.
11129 template <class T> friend A<T>::f();
11130 Look through it in that case. */
11131 if (TREE_CODE (fn
) == TEMPLATE_DECL
11132 && !PRIMARY_TEMPLATE_P (fn
))
11133 fn
= DECL_TEMPLATE_RESULT (fn
);
11134 /* Check to see that the declaration is really present, and,
11135 possibly obtain an improved declaration. */
11136 fn
= check_classfn (context
, fn
, NULL_TREE
);
11146 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11147 template arguments, as for tsubst.
11149 Returns an appropriate tsubst'd friend type or error_mark_node on
11153 tsubst_friend_class (tree friend_tmpl
, tree args
)
11157 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11159 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11160 return TREE_TYPE (tmpl
);
11163 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11164 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11165 push_nested_namespace (context
);
11168 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11169 push_nested_class (context
);
11172 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11173 /*non_class=*/false, /*block_p=*/false,
11174 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11176 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11178 /* The friend template has already been declared. Just
11179 check to see that the declarations match, and install any new
11180 default parameters. We must tsubst the default parameters,
11181 of course. We only need the innermost template parameters
11182 because that is all that redeclare_class_template will look
11184 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11185 > TMPL_ARGS_DEPTH (args
))
11187 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11188 args
, tf_warning_or_error
);
11189 location_t saved_input_location
= input_location
;
11190 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11191 tree cons
= get_constraints (tmpl
);
11192 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11193 input_location
= saved_input_location
;
11198 /* The friend template has not already been declared. In this
11199 case, the instantiation of the template class will cause the
11200 injection of this template into the namespace scope. */
11201 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11203 if (tmpl
!= error_mark_node
)
11205 /* The new TMPL is not an instantiation of anything, so we
11206 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11207 for the new type because that is supposed to be the
11208 corresponding template decl, i.e., TMPL. */
11209 DECL_USE_TEMPLATE (tmpl
) = 0;
11210 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11211 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11212 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11213 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11215 /* It is hidden. */
11216 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11217 DECL_ANTICIPATED (tmpl
)
11218 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11220 /* Inject this template into the enclosing namspace scope. */
11221 tmpl
= pushdecl_namespace_level (tmpl
, true);
11225 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11226 pop_nested_namespace (context
);
11228 pop_nested_class ();
11230 return TREE_TYPE (tmpl
);
11233 /* Returns zero if TYPE cannot be completed later due to circularity.
11234 Otherwise returns one. */
11237 can_complete_type_without_circularity (tree type
)
11239 if (type
== NULL_TREE
|| type
== error_mark_node
)
11241 else if (COMPLETE_TYPE_P (type
))
11243 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11244 return can_complete_type_without_circularity (TREE_TYPE (type
));
11245 else if (CLASS_TYPE_P (type
)
11246 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11252 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11253 tsubst_flags_t
, tree
);
11255 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11256 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11259 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11260 tsubst_flags_t complain
, tree in_decl
)
11262 gcc_assert (ATTR_IS_DEPENDENT (t
));
11264 tree val
= TREE_VALUE (t
);
11265 if (val
== NULL_TREE
)
11266 /* Nothing to do. */;
11267 else if ((flag_openmp
|| flag_openmp_simd
)
11268 && is_attribute_p ("omp declare simd",
11269 get_attribute_name (t
)))
11271 tree clauses
= TREE_VALUE (val
);
11272 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11273 complain
, in_decl
);
11274 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11275 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11276 tree parms
= DECL_ARGUMENTS (*decl_p
);
11278 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11280 val
= build_tree_list (NULL_TREE
, clauses
);
11284 else if (flag_openmp
11285 && is_attribute_p ("omp declare variant base",
11286 get_attribute_name (t
)))
11288 ++cp_unevaluated_operand
;
11290 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11291 in_decl
, /*integral_constant_expression_p=*/false);
11292 --cp_unevaluated_operand
;
11293 tree chain
= TREE_CHAIN (val
);
11294 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11295 tree ctx
= copy_list (TREE_VALUE (val
));
11296 tree simd
= get_identifier ("simd");
11297 tree score
= get_identifier (" score");
11298 tree condition
= get_identifier ("condition");
11299 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11301 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11302 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11303 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11305 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11307 tree clauses
= TREE_VALUE (t2
);
11308 clauses
= tsubst_omp_clauses (clauses
,
11309 C_ORT_OMP_DECLARE_SIMD
, args
,
11310 complain
, in_decl
);
11311 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11312 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11313 TREE_VALUE (t2
) = clauses
;
11317 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11318 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11319 if (TREE_VALUE (t3
))
11322 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11323 && TREE_PURPOSE (t3
) != score
);
11324 tree v
= TREE_VALUE (t3
);
11325 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11327 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11328 v
= fold_non_dependent_expr (v
);
11329 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11330 || (TREE_PURPOSE (t3
) == score
11331 ? TREE_CODE (v
) != INTEGER_CST
11332 : !tree_fits_shwi_p (v
)))
11335 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11337 if (TREE_PURPOSE (t3
) == score
)
11338 error_at (loc
, "score argument must be "
11339 "constant integer expression");
11340 else if (allow_string
)
11341 error_at (loc
, "property must be constant "
11342 "integer expression or string "
11345 error_at (loc
, "property must be constant "
11346 "integer expression");
11349 else if (TREE_PURPOSE (t3
) == score
11350 && tree_int_cst_sgn (v
) < 0)
11353 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11355 error_at (loc
, "score argument must be "
11359 TREE_VALUE (t3
) = v
;
11364 val
= tree_cons (varid
, ctx
, chain
);
11366 /* If the first attribute argument is an identifier, don't
11367 pass it through tsubst. Attributes like mode, format,
11368 cleanup and several target specific attributes expect it
11370 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11373 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11374 /*integral_constant_expression_p=*/false);
11375 if (chain
!= TREE_CHAIN (val
))
11376 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11378 else if (PACK_EXPANSION_P (val
))
11380 /* An attribute pack expansion. */
11381 tree purp
= TREE_PURPOSE (t
);
11382 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11383 if (pack
== error_mark_node
)
11384 return error_mark_node
;
11385 int len
= TREE_VEC_LENGTH (pack
);
11386 tree list
= NULL_TREE
;
11388 for (int i
= 0; i
< len
; ++i
)
11390 tree elt
= TREE_VEC_ELT (pack
, i
);
11391 *q
= build_tree_list (purp
, elt
);
11392 q
= &TREE_CHAIN (*q
);
11397 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11398 /*integral_constant_expression_p=*/false);
11400 if (val
!= TREE_VALUE (t
))
11401 return build_tree_list (TREE_PURPOSE (t
), val
);
11405 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11406 unchanged or a new TREE_LIST chain. */
11409 tsubst_attributes (tree attributes
, tree args
,
11410 tsubst_flags_t complain
, tree in_decl
)
11412 tree last_dep
= NULL_TREE
;
11414 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11415 if (ATTR_IS_DEPENDENT (t
))
11418 attributes
= copy_list (attributes
);
11423 for (tree
*p
= &attributes
; *p
; )
11426 if (ATTR_IS_DEPENDENT (t
))
11428 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11433 p
= &TREE_CHAIN (*p
);
11434 *p
= TREE_CHAIN (t
);
11438 p
= &TREE_CHAIN (*p
);
11444 /* Apply any attributes which had to be deferred until instantiation
11445 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11446 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11449 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11450 tree args
, tsubst_flags_t complain
, tree in_decl
)
11452 tree last_dep
= NULL_TREE
;
11456 if (attributes
== NULL_TREE
)
11459 if (DECL_P (*decl_p
))
11461 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11463 p
= &DECL_ATTRIBUTES (*decl_p
);
11464 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11465 to our attributes parameter. */
11466 gcc_assert (*p
== attributes
);
11470 p
= &TYPE_ATTRIBUTES (*decl_p
);
11471 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11472 lookup_template_class_1, and should be preserved. */
11473 gcc_assert (*p
!= attributes
);
11475 p
= &TREE_CHAIN (*p
);
11478 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11479 if (ATTR_IS_DEPENDENT (t
))
11482 attributes
= copy_list (attributes
);
11489 tree late_attrs
= NULL_TREE
;
11490 tree
*q
= &late_attrs
;
11495 if (ATTR_IS_DEPENDENT (t
))
11497 *p
= TREE_CHAIN (t
);
11498 TREE_CHAIN (t
) = NULL_TREE
;
11499 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11501 q
= &TREE_CHAIN (*q
);
11504 p
= &TREE_CHAIN (t
);
11507 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11511 /* Perform (or defer) access check for typedefs that were referenced
11512 from within the template TMPL code.
11513 This is a subroutine of instantiate_decl and instantiate_class_template.
11514 TMPL is the template to consider and TARGS is the list of arguments of
11518 perform_typedefs_access_check (tree tmpl
, tree targs
)
11521 qualified_typedef_usage_t
*iter
;
11524 || (!CLASS_TYPE_P (tmpl
)
11525 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
11528 if (vec
<qualified_typedef_usage_t
, va_gc
> *tdefs
11529 = get_types_needing_access_check (tmpl
))
11530 FOR_EACH_VEC_ELT (*tdefs
, i
, iter
)
11532 tree type_decl
= iter
->typedef_decl
;
11533 tree type_scope
= iter
->context
;
11535 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
11538 if (uses_template_parms (type_decl
))
11539 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
11540 if (uses_template_parms (type_scope
))
11541 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11543 /* Make access check error messages point to the location
11544 of the use of the typedef. */
11545 iloc_sentinel
ils (iter
->locus
);
11546 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11547 type_decl
, type_decl
,
11548 tf_warning_or_error
);
11553 instantiate_class_template_1 (tree type
)
11555 tree templ
, args
, pattern
, t
, member
;
11559 unsigned int saved_maximum_field_alignment
;
11562 if (type
== error_mark_node
)
11563 return error_mark_node
;
11565 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11566 || uses_template_parms (type
))
11569 /* Figure out which template is being instantiated. */
11570 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11571 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11573 /* Mark the type as in the process of being defined. */
11574 TYPE_BEING_DEFINED (type
) = 1;
11576 /* We may be in the middle of deferred access check. Disable
11578 deferring_access_check_sentinel
acs (dk_no_deferred
);
11580 /* Determine what specialization of the original template to
11582 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11583 if (t
== error_mark_node
)
11584 return error_mark_node
;
11587 /* This TYPE is actually an instantiation of a partial
11588 specialization. We replace the innermost set of ARGS with
11589 the arguments appropriate for substitution. For example,
11592 template <class T> struct S {};
11593 template <class T> struct S<T*> {};
11595 and supposing that we are instantiating S<int*>, ARGS will
11596 presently be {int*} -- but we need {int}. */
11597 pattern
= TREE_TYPE (t
);
11598 args
= TREE_PURPOSE (t
);
11602 pattern
= TREE_TYPE (templ
);
11603 args
= CLASSTYPE_TI_ARGS (type
);
11606 /* If the template we're instantiating is incomplete, then clearly
11607 there's nothing we can do. */
11608 if (!COMPLETE_TYPE_P (pattern
))
11610 /* We can try again later. */
11611 TYPE_BEING_DEFINED (type
) = 0;
11615 /* If we've recursively instantiated too many templates, stop. */
11616 if (! push_tinst_level (type
))
11619 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11620 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11622 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11623 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11624 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11625 fn_context
= error_mark_node
;
11627 push_to_top_level ();
11630 cp_unevaluated_operand
= 0;
11631 c_inhibit_evaluation_warnings
= 0;
11633 /* Use #pragma pack from the template context. */
11634 saved_maximum_field_alignment
= maximum_field_alignment
;
11635 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11637 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11639 /* Set the input location to the most specialized template definition.
11640 This is needed if tsubsting causes an error. */
11641 typedecl
= TYPE_MAIN_DECL (pattern
);
11642 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11643 DECL_SOURCE_LOCATION (typedecl
);
11645 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11646 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11647 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11648 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11649 if (ANON_AGGR_TYPE_P (pattern
))
11650 SET_ANON_AGGR_TYPE_P (type
);
11651 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11653 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11654 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11655 /* Adjust visibility for template arguments. */
11656 determine_visibility (TYPE_MAIN_DECL (type
));
11658 if (CLASS_TYPE_P (type
))
11659 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11661 pbinfo
= TYPE_BINFO (pattern
);
11663 /* We should never instantiate a nested class before its enclosing
11664 class; we need to look up the nested class by name before we can
11665 instantiate it, and that lookup should instantiate the enclosing
11667 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11668 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11670 base_list
= NULL_TREE
;
11671 if (BINFO_N_BASE_BINFOS (pbinfo
))
11677 /* We must enter the scope containing the type, as that is where
11678 the accessibility of types named in dependent bases are
11680 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11682 /* Substitute into each of the bases to determine the actual
11684 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11687 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11688 tree expanded_bases
= NULL_TREE
;
11691 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11694 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11695 args
, tf_error
, NULL_TREE
);
11696 if (expanded_bases
== error_mark_node
)
11699 len
= TREE_VEC_LENGTH (expanded_bases
);
11702 for (idx
= 0; idx
< len
; idx
++)
11704 if (expanded_bases
)
11705 /* Extract the already-expanded base class. */
11706 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11708 /* Substitute to figure out the base class. */
11709 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11712 if (base
== error_mark_node
)
11715 base_list
= tree_cons (access
, base
, base_list
);
11716 if (BINFO_VIRTUAL_P (pbase_binfo
))
11717 TREE_TYPE (base_list
) = integer_type_node
;
11721 /* The list is now in reverse order; correct that. */
11722 base_list
= nreverse (base_list
);
11725 pop_scope (pushed_scope
);
11727 /* Now call xref_basetypes to set up all the base-class
11729 xref_basetypes (type
, base_list
);
11731 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11732 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11733 args
, tf_error
, NULL_TREE
);
11734 fixup_attribute_variants (type
);
11736 /* Now that our base classes are set up, enter the scope of the
11737 class, so that name lookups into base classes, etc. will work
11738 correctly. This is precisely analogous to what we do in
11739 begin_class_definition when defining an ordinary non-template
11740 class, except we also need to push the enclosing classes. */
11741 push_nested_class (type
);
11743 /* Now members are processed in the order of declaration. */
11744 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11745 member
; member
= TREE_CHAIN (member
))
11747 tree t
= TREE_VALUE (member
);
11749 if (TREE_PURPOSE (member
))
11753 if (LAMBDA_TYPE_P (t
))
11754 /* A closure type for a lambda in an NSDMI or default argument.
11755 Ignore it; it will be regenerated when needed. */
11758 /* Build new CLASSTYPE_NESTED_UTDS. */
11759 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11760 && TYPE_LANG_SPECIFIC (t
)
11761 && CLASSTYPE_IS_TEMPLATE (t
));
11763 /* If the member is a class template, then -- even after
11764 substitution -- there may be dependent types in the
11765 template argument list for the class. We increment
11766 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11767 that function will assume that no types are dependent
11768 when outside of a template. */
11769 if (class_template_p
)
11770 ++processing_template_decl
;
11771 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11772 if (class_template_p
)
11773 --processing_template_decl
;
11774 if (newtag
== error_mark_node
)
11777 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11779 tree name
= TYPE_IDENTIFIER (t
);
11781 if (class_template_p
)
11782 /* Unfortunately, lookup_template_class sets
11783 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11784 instantiation (i.e., for the type of a member
11785 template class nested within a template class.)
11786 This behavior is required for
11787 maybe_process_partial_specialization to work
11788 correctly, but is not accurate in this case;
11789 the TAG is not an instantiation of anything.
11790 (The corresponding TEMPLATE_DECL is an
11791 instantiation, but the TYPE is not.) */
11792 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11794 /* Now, we call pushtag to put this NEWTAG into the scope of
11795 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11796 pushtag calling push_template_decl. We don't have to do
11797 this for enums because it will already have been done in
11800 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11801 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11804 else if (DECL_DECLARES_FUNCTION_P (t
))
11808 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11809 ++processing_template_decl
;
11810 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11811 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11812 --processing_template_decl
;
11813 set_current_access_from_decl (r
);
11814 finish_member_declaration (r
);
11815 /* Instantiate members marked with attribute used. */
11816 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11818 if (TREE_CODE (r
) == FUNCTION_DECL
11819 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11820 cp_check_omp_declare_reduction (r
);
11822 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11823 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11824 /* A closure type for a lambda in an NSDMI or default argument.
11825 Ignore it; it will be regenerated when needed. */;
11828 /* Build new TYPE_FIELDS. */
11829 if (TREE_CODE (t
) == STATIC_ASSERT
)
11830 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11831 /*integral_constant_expression_p=*/true);
11832 else if (TREE_CODE (t
) != CONST_DECL
)
11835 tree vec
= NULL_TREE
;
11838 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11839 /* The file and line for this declaration, to
11840 assist in error message reporting. Since we
11841 called push_tinst_level above, we don't need to
11843 input_location
= DECL_SOURCE_LOCATION (t
);
11845 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11846 ++processing_template_decl
;
11847 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11848 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11849 --processing_template_decl
;
11851 if (TREE_CODE (r
) == TREE_VEC
)
11853 /* A capture pack became multiple fields. */
11855 len
= TREE_VEC_LENGTH (vec
);
11858 for (int i
= 0; i
< len
; ++i
)
11861 r
= TREE_VEC_ELT (vec
, i
);
11866 [t]he initialization (and any associated
11867 side-effects) of a static data member does
11868 not occur unless the static data member is
11869 itself used in a way that requires the
11870 definition of the static data member to
11873 Therefore, we do not substitute into the
11874 initialized for the static data member here. */
11875 finish_static_data_member_decl
11877 /*init=*/NULL_TREE
,
11878 /*init_const_expr_p=*/false,
11879 /*asmspec_tree=*/NULL_TREE
,
11881 /* Instantiate members marked with attribute used. */
11882 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11885 else if (TREE_CODE (r
) == FIELD_DECL
)
11887 /* Determine whether R has a valid type and can be
11888 completed later. If R is invalid, then its type
11889 is replaced by error_mark_node. */
11890 tree rtype
= TREE_TYPE (r
);
11891 if (can_complete_type_without_circularity (rtype
))
11892 complete_type (rtype
);
11894 if (!complete_or_array_type_p (rtype
))
11896 /* If R's type couldn't be completed and
11897 it isn't a flexible array member (whose
11898 type is incomplete by definition) give
11900 cxx_incomplete_type_error (r
, rtype
);
11901 TREE_TYPE (r
) = error_mark_node
;
11903 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11904 && TYPE_DOMAIN (rtype
) == NULL_TREE
11905 && (TREE_CODE (type
) == UNION_TYPE
11906 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11908 error ("flexible array member %qD in union", r
);
11909 TREE_TYPE (r
) = error_mark_node
;
11911 else if (!verify_type_context (input_location
,
11912 TCTX_FIELD
, rtype
))
11913 TREE_TYPE (r
) = error_mark_node
;
11916 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11917 such a thing will already have been added to the field
11918 list by tsubst_enum in finish_member_declaration in the
11919 CLASSTYPE_NESTED_UTDS case above. */
11920 if (!(TREE_CODE (r
) == TYPE_DECL
11921 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11922 && DECL_ARTIFICIAL (r
)))
11924 set_current_access_from_decl (r
);
11925 finish_member_declaration (r
);
11933 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11934 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11936 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11938 tree friend_type
= t
;
11939 bool adjust_processing_template_decl
= false;
11941 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11943 /* template <class T> friend class C; */
11944 friend_type
= tsubst_friend_class (friend_type
, args
);
11945 adjust_processing_template_decl
= true;
11947 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11949 /* template <class T> friend class C::D; */
11950 friend_type
= tsubst (friend_type
, args
,
11951 tf_warning_or_error
, NULL_TREE
);
11952 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11953 friend_type
= TREE_TYPE (friend_type
);
11954 adjust_processing_template_decl
= true;
11956 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11957 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11959 /* This could be either
11963 when dependent_type_p is false or
11965 template <class U> friend class T::C;
11968 /* Bump processing_template_decl in case this is something like
11969 template <class T> friend struct A<T>::B. */
11970 ++processing_template_decl
;
11971 friend_type
= tsubst (friend_type
, args
,
11972 tf_warning_or_error
, NULL_TREE
);
11973 if (dependent_type_p (friend_type
))
11974 adjust_processing_template_decl
= true;
11975 --processing_template_decl
;
11977 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11978 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11979 && TYPE_HIDDEN_P (friend_type
))
11983 where C hasn't been declared yet. Let's lookup name
11984 from namespace scope directly, bypassing any name that
11985 come from dependent base class. */
11986 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11988 /* The call to xref_tag_from_type does injection for friend
11990 push_nested_namespace (ns
);
11992 xref_tag_from_type (friend_type
, NULL_TREE
,
11993 /*tag_scope=*/ts_current
);
11994 pop_nested_namespace (ns
);
11996 else if (uses_template_parms (friend_type
))
11997 /* friend class C<T>; */
11998 friend_type
= tsubst (friend_type
, args
,
11999 tf_warning_or_error
, NULL_TREE
);
12004 where C is already declared or
12006 friend class C<int>;
12008 We don't have to do anything in these cases. */
12010 if (adjust_processing_template_decl
)
12011 /* Trick make_friend_class into realizing that the friend
12012 we're adding is a template, not an ordinary class. It's
12013 important that we use make_friend_class since it will
12014 perform some error-checking and output cross-reference
12016 ++processing_template_decl
;
12018 if (friend_type
!= error_mark_node
)
12019 make_friend_class (type
, friend_type
, /*complain=*/false);
12021 if (adjust_processing_template_decl
)
12022 --processing_template_decl
;
12026 /* Build new DECL_FRIENDLIST. */
12029 /* The file and line for this declaration, to
12030 assist in error message reporting. Since we
12031 called push_tinst_level above, we don't need to
12033 input_location
= DECL_SOURCE_LOCATION (t
);
12035 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12037 ++processing_template_decl
;
12038 push_deferring_access_checks (dk_no_check
);
12041 r
= tsubst_friend_function (t
, args
);
12042 add_friend (type
, r
, /*complain=*/false);
12043 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12045 pop_deferring_access_checks ();
12046 --processing_template_decl
;
12054 /* Restore these before substituting into the lambda capture
12056 cp_unevaluated_operand
= saved_unevaluated_operand
;
12057 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12060 /* Set the file and line number information to whatever is given for
12061 the class itself. This puts error messages involving generated
12062 implicit functions at a predictable point, and the same point
12063 that would be used for non-template classes. */
12064 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12066 unreverse_member_declarations (type
);
12067 finish_struct_1 (type
);
12068 TYPE_BEING_DEFINED (type
) = 0;
12070 /* We don't instantiate default arguments for member functions. 14.7.1:
12072 The implicit instantiation of a class template specialization causes
12073 the implicit instantiation of the declarations, but not of the
12074 definitions or default arguments, of the class member functions,
12075 member classes, static data members and member templates.... */
12077 /* Some typedefs referenced from within the template code need to be access
12078 checked at template instantiation time, i.e now. These types were
12079 added to the template at parsing time. Let's get those and perform
12080 the access checks then. */
12081 perform_typedefs_access_check (pattern
, args
);
12082 perform_deferred_access_checks (tf_warning_or_error
);
12083 pop_nested_class ();
12084 maximum_field_alignment
= saved_maximum_field_alignment
;
12086 pop_from_top_level ();
12087 pop_tinst_level ();
12089 /* The vtable for a template class can be emitted in any translation
12090 unit in which the class is instantiated. When there is no key
12091 method, however, finish_struct_1 will already have added TYPE to
12092 the keyed_classes. */
12093 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12094 vec_safe_push (keyed_classes
, type
);
12099 /* Wrapper for instantiate_class_template_1. */
12102 instantiate_class_template (tree type
)
12105 timevar_push (TV_TEMPLATE_INST
);
12106 ret
= instantiate_class_template_1 (type
);
12107 timevar_pop (TV_TEMPLATE_INST
);
12112 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12118 else if (TYPE_P (t
))
12119 r
= tsubst (t
, args
, complain
, in_decl
);
12122 if (!(complain
& tf_warning
))
12123 ++c_inhibit_evaluation_warnings
;
12124 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12125 /*integral_constant_expression_p=*/true);
12126 if (!(complain
& tf_warning
))
12127 --c_inhibit_evaluation_warnings
;
12133 /* Given a function parameter pack TMPL_PARM and some function parameters
12134 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12135 and set *SPEC_P to point at the next point in the list. */
12138 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12140 /* Collect all of the extra "packed" parameters into an
12143 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12144 tree spec_parm
= *spec_p
;
12147 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12149 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12152 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12153 parmvec
= make_tree_vec (len
);
12154 spec_parm
= *spec_p
;
12155 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12157 tree elt
= spec_parm
;
12158 if (DECL_PACK_P (elt
))
12159 elt
= make_pack_expansion (elt
);
12160 TREE_VEC_ELT (parmvec
, i
) = elt
;
12163 /* Build the argument packs. */
12164 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12165 *spec_p
= spec_parm
;
12170 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12171 NONTYPE_ARGUMENT_PACK. */
12174 make_fnparm_pack (tree spec_parm
)
12176 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12179 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12180 pack expansion with no extra args, 2 if it has extra args, or 0
12181 if it is not a pack expansion. */
12184 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12186 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12187 /* We're being called before this happens in tsubst_pack_expansion. */
12188 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12189 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12190 if (i
>= TREE_VEC_LENGTH (vec
))
12192 tree elt
= TREE_VEC_ELT (vec
, i
);
12194 /* A decl pack is itself an expansion. */
12195 elt
= TREE_TYPE (elt
);
12196 if (!PACK_EXPANSION_P (elt
))
12198 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12204 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12207 make_argument_pack_select (tree arg_pack
, unsigned index
)
12209 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12211 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12212 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12217 /* This is a subroutine of tsubst_pack_expansion.
12219 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12220 mechanism to store the (non complete list of) arguments of the
12221 substitution and return a non substituted pack expansion, in order
12222 to wait for when we have enough arguments to really perform the
12226 use_pack_expansion_extra_args_p (tree parm_packs
,
12228 bool has_empty_arg
)
12230 /* If one pack has an expansion and another pack has a normal
12231 argument or if one pack has an empty argument and an another
12232 one hasn't then tsubst_pack_expansion cannot perform the
12233 substitution and need to fall back on the
12234 PACK_EXPANSION_EXTRA mechanism. */
12235 if (parm_packs
== NULL_TREE
)
12237 else if (has_empty_arg
)
12239 /* If all the actual packs are pack expansions, we can still
12240 subsitute directly. */
12241 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12243 tree a
= TREE_VALUE (p
);
12244 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12245 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12246 a
= ARGUMENT_PACK_ARGS (a
);
12247 if (TREE_VEC_LENGTH (a
) == 1)
12248 a
= TREE_VEC_ELT (a
, 0);
12249 if (PACK_EXPANSION_P (a
))
12256 bool has_expansion_arg
= false;
12257 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12259 bool has_non_expansion_arg
= false;
12260 for (tree parm_pack
= parm_packs
;
12262 parm_pack
= TREE_CHAIN (parm_pack
))
12264 tree arg
= TREE_VALUE (parm_pack
);
12266 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12268 /* We can't substitute a pack expansion with extra args into
12272 has_expansion_arg
= true;
12274 has_non_expansion_arg
= true;
12277 if (has_expansion_arg
&& has_non_expansion_arg
)
12283 /* [temp.variadic]/6 says that:
12285 The instantiation of a pack expansion [...]
12286 produces a list E1,E2, ..., En, where N is the number of elements
12287 in the pack expansion parameters.
12289 This subroutine of tsubst_pack_expansion produces one of these Ei.
12291 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12292 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12293 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12294 INDEX is the index 'i' of the element Ei to produce. ARGS,
12295 COMPLAIN, and IN_DECL are the same parameters as for the
12296 tsubst_pack_expansion function.
12298 The function returns the resulting Ei upon successful completion,
12299 or error_mark_node.
12301 Note that this function possibly modifies the ARGS parameter, so
12302 it's the responsibility of the caller to restore it. */
12305 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12308 tree args
/* This parm gets
12310 tsubst_flags_t complain
,
12314 bool ith_elem_is_expansion
= false;
12316 /* For each parameter pack, change the substitution of the parameter
12317 pack to the ith argument in its argument pack, then expand the
12319 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12321 tree parm
= TREE_PURPOSE (pack
);
12322 tree arg_pack
= TREE_VALUE (pack
);
12323 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12325 ith_elem_is_expansion
|=
12326 argument_pack_element_is_expansion_p (arg_pack
, index
);
12328 /* Select the Ith argument from the pack. */
12329 if (TREE_CODE (parm
) == PARM_DECL
12331 || TREE_CODE (parm
) == FIELD_DECL
)
12335 aps
= make_argument_pack_select (arg_pack
, index
);
12336 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12337 return error_mark_node
;
12338 register_local_specialization (aps
, parm
);
12341 aps
= retrieve_local_specialization (parm
);
12346 template_parm_level_and_index (parm
, &level
, &idx
);
12350 aps
= make_argument_pack_select (arg_pack
, index
);
12351 /* Update the corresponding argument. */
12352 TMPL_ARG (args
, level
, idx
) = aps
;
12355 /* Re-use the ARGUMENT_PACK_SELECT. */
12356 aps
= TMPL_ARG (args
, level
, idx
);
12358 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12361 /* Substitute into the PATTERN with the (possibly altered)
12363 if (pattern
== in_decl
)
12364 /* Expanding a fixed parameter pack from
12365 coerce_template_parameter_pack. */
12366 t
= tsubst_decl (pattern
, args
, complain
);
12367 else if (pattern
== error_mark_node
)
12368 t
= error_mark_node
;
12369 else if (!TYPE_P (pattern
))
12370 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12371 /*integral_constant_expression_p=*/false);
12373 t
= tsubst (pattern
, args
, complain
, in_decl
);
12375 /* If the Ith argument pack element is a pack expansion, then
12376 the Ith element resulting from the substituting is going to
12377 be a pack expansion as well. */
12378 if (ith_elem_is_expansion
)
12379 t
= make_pack_expansion (t
, complain
);
12384 /* When the unexpanded parameter pack in a fold expression expands to an empty
12385 sequence, the value of the expression is as follows; the program is
12386 ill-formed if the operator is not listed in this table.
12393 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12395 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12396 if (!FOLD_EXPR_MODIFY_P (t
))
12399 case TRUTH_ANDIF_EXPR
:
12400 return boolean_true_node
;
12401 case TRUTH_ORIF_EXPR
:
12402 return boolean_false_node
;
12403 case COMPOUND_EXPR
:
12409 if (complain
& tf_error
)
12410 error_at (location_of (t
),
12411 "fold of empty expansion over %O", code
);
12412 return error_mark_node
;
12415 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12416 form an expression that combines the two terms using the
12420 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12422 tree op
= FOLD_EXPR_OP (t
);
12423 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12425 // Handle compound assignment operators.
12426 if (FOLD_EXPR_MODIFY_P (t
))
12427 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12429 warning_sentinel
s(warn_parentheses
);
12432 case COMPOUND_EXPR
:
12433 return build_x_compound_expr (input_location
, left
, right
, complain
);
12435 return build_x_binary_op (input_location
, code
,
12436 left
, TREE_CODE (left
),
12437 right
, TREE_CODE (right
),
12443 /* Substitute ARGS into the pack of a fold expression T. */
12446 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12448 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12451 /* Substitute ARGS into the pack of a fold expression T. */
12454 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12456 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12459 /* Expand a PACK of arguments into a grouped as left fold.
12460 Given a pack containing elements A0, A1, ..., An and an
12461 operator @, this builds the expression:
12463 ((A0 @ A1) @ A2) ... @ An
12465 Note that PACK must not be empty.
12467 The operator is defined by the original fold expression T. */
12470 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12472 tree left
= TREE_VEC_ELT (pack
, 0);
12473 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12475 tree right
= TREE_VEC_ELT (pack
, i
);
12476 left
= fold_expression (t
, left
, right
, complain
);
12481 /* Substitute into a unary left fold expression. */
12484 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12487 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12488 if (pack
== error_mark_node
)
12489 return error_mark_node
;
12490 if (PACK_EXPANSION_P (pack
))
12492 tree r
= copy_node (t
);
12493 FOLD_EXPR_PACK (r
) = pack
;
12496 if (TREE_VEC_LENGTH (pack
) == 0)
12497 return expand_empty_fold (t
, complain
);
12499 return expand_left_fold (t
, pack
, complain
);
12502 /* Substitute into a binary left fold expression.
12504 Do ths by building a single (non-empty) vector of argumnts and
12505 building the expression from those elements. */
12508 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12511 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12512 if (pack
== error_mark_node
)
12513 return error_mark_node
;
12514 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12515 if (init
== error_mark_node
)
12516 return error_mark_node
;
12518 if (PACK_EXPANSION_P (pack
))
12520 tree r
= copy_node (t
);
12521 FOLD_EXPR_PACK (r
) = pack
;
12522 FOLD_EXPR_INIT (r
) = init
;
12526 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12527 TREE_VEC_ELT (vec
, 0) = init
;
12528 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12529 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12531 return expand_left_fold (t
, vec
, complain
);
12534 /* Expand a PACK of arguments into a grouped as right fold.
12535 Given a pack containing elementns A0, A1, ..., and an
12536 operator @, this builds the expression:
12538 A0@ ... (An-2 @ (An-1 @ An))
12540 Note that PACK must not be empty.
12542 The operator is defined by the original fold expression T. */
12545 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12547 // Build the expression.
12548 int n
= TREE_VEC_LENGTH (pack
);
12549 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12550 for (--n
; n
!= 0; --n
)
12552 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12553 right
= fold_expression (t
, left
, right
, complain
);
12558 /* Substitute into a unary right fold expression. */
12561 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12564 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12565 if (pack
== error_mark_node
)
12566 return error_mark_node
;
12567 if (PACK_EXPANSION_P (pack
))
12569 tree r
= copy_node (t
);
12570 FOLD_EXPR_PACK (r
) = pack
;
12573 if (TREE_VEC_LENGTH (pack
) == 0)
12574 return expand_empty_fold (t
, complain
);
12576 return expand_right_fold (t
, pack
, complain
);
12579 /* Substitute into a binary right fold expression.
12581 Do ths by building a single (non-empty) vector of arguments and
12582 building the expression from those elements. */
12585 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12588 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12589 if (pack
== error_mark_node
)
12590 return error_mark_node
;
12591 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12592 if (init
== error_mark_node
)
12593 return error_mark_node
;
12595 if (PACK_EXPANSION_P (pack
))
12597 tree r
= copy_node (t
);
12598 FOLD_EXPR_PACK (r
) = pack
;
12599 FOLD_EXPR_INIT (r
) = init
;
12603 int n
= TREE_VEC_LENGTH (pack
);
12604 tree vec
= make_tree_vec (n
+ 1);
12605 for (int i
= 0; i
< n
; ++i
)
12606 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12607 TREE_VEC_ELT (vec
, n
) = init
;
12609 return expand_right_fold (t
, vec
, complain
);
12612 /* Walk through the pattern of a pack expansion, adding everything in
12613 local_specializations to a list. */
12618 hash_set
<tree
> internal
;
12620 tsubst_flags_t complain
;
12622 el_data (tsubst_flags_t c
)
12623 : extra (NULL_TREE
), complain (c
) {}
12626 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12628 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12629 tree
*extra
= &data
.extra
;
12630 tsubst_flags_t complain
= data
.complain
;
12632 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12633 /* Remember local typedefs (85214). */
12634 tp
= &TYPE_NAME (*tp
);
12636 if (TREE_CODE (*tp
) == DECL_EXPR
)
12637 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12638 else if (tree spec
= retrieve_local_specialization (*tp
))
12640 if (data
.internal
.contains (*tp
))
12641 /* Don't mess with variables declared within the pattern. */
12643 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12645 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12646 tree args
= ARGUMENT_PACK_ARGS (spec
);
12647 if (TREE_VEC_LENGTH (args
) == 1)
12649 tree elt
= TREE_VEC_ELT (args
, 0);
12650 if (PACK_EXPANSION_P (elt
))
12651 elt
= PACK_EXPANSION_PATTERN (elt
);
12652 if (DECL_PACK_P (elt
))
12655 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12657 /* Handle lambda capture here, since we aren't doing any
12658 substitution now, and so tsubst_copy won't call
12659 process_outer_var_ref. */
12660 tree args
= ARGUMENT_PACK_ARGS (spec
);
12661 int len
= TREE_VEC_LENGTH (args
);
12662 for (int i
= 0; i
< len
; ++i
)
12664 tree arg
= TREE_VEC_ELT (args
, i
);
12666 if (outer_automatic_var_p (arg
))
12667 carg
= process_outer_var_ref (arg
, complain
);
12670 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12674 spec
= copy_node (spec
);
12675 args
= copy_node (args
);
12676 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12677 register_local_specialization (spec
, *tp
);
12679 TREE_VEC_ELT (args
, i
) = carg
;
12684 if (outer_automatic_var_p (spec
))
12685 spec
= process_outer_var_ref (spec
, complain
);
12686 *extra
= tree_cons (*tp
, spec
, *extra
);
12691 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12693 el_data
data (complain
);
12694 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12698 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12699 for use in PACK_EXPANSION_EXTRA_ARGS. */
12702 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12705 if (local_specializations
)
12706 if (tree locals
= extract_local_specs (pattern
, complain
))
12707 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12711 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12712 normal template args to ARGS. */
12715 add_extra_args (tree extra
, tree args
)
12717 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12719 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12721 /* The partial instantiation involved local declarations collected in
12722 extract_local_specs; map from the general template to our local
12724 tree gen
= TREE_PURPOSE (elt
);
12725 tree inst
= TREE_VALUE (elt
);
12727 if (tree local
= retrieve_local_specialization (inst
))
12729 /* else inst is already a full instantiation of the pack. */
12730 register_local_specialization (inst
, gen
);
12732 gcc_assert (!TREE_PURPOSE (extra
));
12733 extra
= TREE_VALUE (extra
);
12736 /* I think we should always be able to substitute dependent args into the
12737 pattern. If that turns out to be incorrect in some cases, enable the
12738 alternate code (and add complain/in_decl parms to this function). */
12739 gcc_checking_assert (!uses_template_parms (extra
));
12741 if (!uses_template_parms (extra
))
12743 gcc_unreachable ();
12744 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12745 args
= add_outermost_template_args (args
, extra
);
12749 args
= add_to_template_args (extra
, args
);
12753 /* Substitute ARGS into T, which is an pack expansion
12754 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12755 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12756 (if only a partial substitution could be performed) or
12757 ERROR_MARK_NODE if there was an error. */
12759 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12763 tree pack
, packs
= NULL_TREE
;
12764 bool unsubstituted_packs
= false;
12767 bool need_local_specializations
= false;
12770 gcc_assert (PACK_EXPANSION_P (t
));
12771 pattern
= PACK_EXPANSION_PATTERN (t
);
12773 /* Add in any args remembered from an earlier partial instantiation. */
12774 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12776 levels
= TMPL_ARGS_DEPTH (args
);
12778 /* Determine the argument packs that will instantiate the parameter
12779 packs used in the expansion expression. While we're at it,
12780 compute the number of arguments to be expanded and make sure it
12782 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12783 pack
= TREE_CHAIN (pack
))
12785 tree parm_pack
= TREE_VALUE (pack
);
12786 tree arg_pack
= NULL_TREE
;
12787 tree orig_arg
= NULL_TREE
;
12790 if (TREE_CODE (parm_pack
) == BASES
)
12792 gcc_assert (parm_pack
== pattern
);
12793 if (BASES_DIRECT (parm_pack
))
12794 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12799 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12800 args
, complain
, in_decl
,
12803 else if (builtin_pack_call_p (parm_pack
))
12805 if (parm_pack
!= pattern
)
12807 if (complain
& tf_error
)
12808 sorry ("%qE is not the entire pattern of the pack expansion",
12810 return error_mark_node
;
12812 return expand_builtin_pack_call (parm_pack
, args
,
12813 complain
, in_decl
);
12815 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12817 /* We know we have correct local_specializations if this
12818 expansion is at function scope, or if we're dealing with a
12819 local parameter in a requires expression; for the latter,
12820 tsubst_requires_expr set it up appropriately. */
12821 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12822 arg_pack
= retrieve_local_specialization (parm_pack
);
12824 /* We can't rely on local_specializations for a parameter
12825 name used later in a function declaration (such as in a
12826 late-specified return type). Even if it exists, it might
12827 have the wrong value for a recursive call. */
12828 need_local_specializations
= true;
12832 /* This parameter pack was used in an unevaluated context. Just
12833 make a dummy decl, since it's only used for its type. */
12834 ++cp_unevaluated_operand
;
12835 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12836 --cp_unevaluated_operand
;
12837 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12838 /* Partial instantiation of the parm_pack, we can't build
12839 up an argument pack yet. */
12840 arg_pack
= NULL_TREE
;
12842 arg_pack
= make_fnparm_pack (arg_pack
);
12844 else if (DECL_PACK_P (arg_pack
))
12845 /* This argument pack isn't fully instantiated yet. */
12846 arg_pack
= NULL_TREE
;
12848 else if (is_capture_proxy (parm_pack
))
12850 arg_pack
= retrieve_local_specialization (parm_pack
);
12851 if (DECL_PACK_P (arg_pack
))
12852 arg_pack
= NULL_TREE
;
12857 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12858 if (level
<= levels
)
12859 arg_pack
= TMPL_ARG (args
, level
, idx
);
12862 orig_arg
= arg_pack
;
12863 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12864 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12866 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12867 /* This can only happen if we forget to expand an argument
12868 pack somewhere else. Just return an error, silently. */
12870 result
= make_tree_vec (1);
12871 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12878 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12880 /* Don't bother trying to do a partial substitution with
12881 incomplete packs; we'll try again after deduction. */
12882 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12887 else if (len
!= my_len
)
12889 if (!(complain
& tf_error
))
12890 /* Fail quietly. */;
12891 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12892 error ("mismatched argument pack lengths while expanding %qT",
12895 error ("mismatched argument pack lengths while expanding %qE",
12897 return error_mark_node
;
12900 /* Keep track of the parameter packs and their corresponding
12902 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12903 TREE_TYPE (packs
) = orig_arg
;
12907 /* We can't substitute for this parameter pack. We use a flag as
12908 well as the missing_level counter because function parameter
12909 packs don't have a level. */
12910 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12911 unsubstituted_packs
= true;
12915 /* If the expansion is just T..., return the matching argument pack, unless
12916 we need to call convert_from_reference on all the elements. This is an
12917 important optimization; see c++/68422. */
12918 if (!unsubstituted_packs
12919 && TREE_PURPOSE (packs
) == pattern
)
12921 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12923 /* If the argument pack is a single pack expansion, pull it out. */
12924 if (TREE_VEC_LENGTH (args
) == 1
12925 && pack_expansion_args_count (args
))
12926 return TREE_VEC_ELT (args
, 0);
12928 /* Types need no adjustment, nor does sizeof..., and if we still have
12929 some pack expansion args we won't do anything yet. */
12930 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12931 || PACK_EXPANSION_SIZEOF_P (t
)
12932 || pack_expansion_args_count (args
))
12934 /* Also optimize expression pack expansions if we can tell that the
12935 elements won't have reference type. */
12936 tree type
= TREE_TYPE (pattern
);
12937 if (type
&& !TYPE_REF_P (type
)
12938 && !PACK_EXPANSION_P (type
)
12939 && !WILDCARD_TYPE_P (type
))
12941 /* Otherwise use the normal path so we get convert_from_reference. */
12944 /* We cannot expand this expansion expression, because we don't have
12945 all of the argument packs we need. */
12946 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12948 /* We got some full packs, but we can't substitute them in until we
12949 have values for all the packs. So remember these until then. */
12951 t
= make_pack_expansion (pattern
, complain
);
12952 PACK_EXPANSION_EXTRA_ARGS (t
)
12953 = build_extra_args (pattern
, args
, complain
);
12957 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12958 type, so create our own local specializations map; the current map is
12959 either NULL or (in the case of recursive unification) might have
12960 bindings that we don't want to use or alter. */
12961 local_specialization_stack
lss (need_local_specializations
12962 ? lss_blank
: lss_nop
);
12964 if (unsubstituted_packs
)
12966 /* There were no real arguments, we're just replacing a parameter
12967 pack with another version of itself. Substitute into the
12968 pattern and return a PACK_EXPANSION_*. The caller will need to
12970 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12971 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12972 /*integral_constant_expression_p=*/false);
12974 t
= tsubst (pattern
, args
, complain
, in_decl
);
12975 t
= make_pack_expansion (t
, complain
);
12979 gcc_assert (len
>= 0);
12981 /* For each argument in each argument pack, substitute into the
12983 result
= make_tree_vec (len
);
12984 tree elem_args
= copy_template_args (args
);
12985 for (i
= 0; i
< len
; ++i
)
12987 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12989 elem_args
, complain
,
12991 TREE_VEC_ELT (result
, i
) = t
;
12992 if (t
== error_mark_node
)
12994 result
= error_mark_node
;
12999 /* Update ARGS to restore the substitution from parameter packs to
13000 their argument packs. */
13001 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13003 tree parm
= TREE_PURPOSE (pack
);
13005 if (TREE_CODE (parm
) == PARM_DECL
13007 || TREE_CODE (parm
) == FIELD_DECL
)
13008 register_local_specialization (TREE_TYPE (pack
), parm
);
13013 if (TREE_VALUE (pack
) == NULL_TREE
)
13016 template_parm_level_and_index (parm
, &level
, &idx
);
13018 /* Update the corresponding argument. */
13019 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13020 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13023 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13027 /* If the dependent pack arguments were such that we end up with only a
13028 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13029 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13030 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13031 return TREE_VEC_ELT (result
, 0);
13036 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13037 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13038 parameter packs; all parms generated from a function parameter pack will
13039 have the same DECL_PARM_INDEX. */
13042 get_pattern_parm (tree parm
, tree tmpl
)
13044 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13047 if (DECL_ARTIFICIAL (parm
))
13049 for (patparm
= DECL_ARGUMENTS (pattern
);
13050 patparm
; patparm
= DECL_CHAIN (patparm
))
13051 if (DECL_ARTIFICIAL (patparm
)
13052 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13057 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13058 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13059 gcc_assert (DECL_PARM_INDEX (patparm
)
13060 == DECL_PARM_INDEX (parm
));
13066 /* Make an argument pack out of the TREE_VEC VEC. */
13069 make_argument_pack (tree vec
)
13073 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13074 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13077 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13078 TREE_CONSTANT (pack
) = 1;
13080 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13084 /* Return an exact copy of template args T that can be modified
13088 copy_template_args (tree t
)
13090 if (t
== error_mark_node
)
13093 int len
= TREE_VEC_LENGTH (t
);
13094 tree new_vec
= make_tree_vec (len
);
13096 for (int i
= 0; i
< len
; ++i
)
13098 tree elt
= TREE_VEC_ELT (t
, i
);
13099 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13100 elt
= copy_template_args (elt
);
13101 TREE_VEC_ELT (new_vec
, i
) = elt
;
13104 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13105 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13110 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13113 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13116 /* Substitute into each of the arguments. */
13117 tree new_arg
= TYPE_P (orig_arg
)
13118 ? cxx_make_type (TREE_CODE (orig_arg
))
13119 : make_node (TREE_CODE (orig_arg
));
13121 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13122 args
, complain
, in_decl
);
13123 if (pack_args
== error_mark_node
)
13124 new_arg
= error_mark_node
;
13126 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13128 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13129 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13134 /* Substitute ARGS into the vector or list of template arguments T. */
13137 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13140 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13143 if (t
== error_mark_node
)
13144 return error_mark_node
;
13146 len
= TREE_VEC_LENGTH (t
);
13147 elts
= XALLOCAVEC (tree
, len
);
13149 for (i
= 0; i
< len
; i
++)
13151 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13154 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13155 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13156 else if (PACK_EXPANSION_P (orig_arg
))
13158 /* Substitute into an expansion expression. */
13159 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13161 if (TREE_CODE (new_arg
) == TREE_VEC
)
13162 /* Add to the expanded length adjustment the number of
13163 expanded arguments. We subtract one from this
13164 measurement, because the argument pack expression
13165 itself is already counted as 1 in
13166 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13167 the argument pack is empty. */
13168 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13170 else if (ARGUMENT_PACK_P (orig_arg
))
13171 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13173 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13175 if (new_arg
== error_mark_node
)
13176 return error_mark_node
;
13179 if (new_arg
!= orig_arg
)
13186 /* Make space for the expanded arguments coming from template
13188 t
= make_tree_vec (len
+ expanded_len_adjust
);
13189 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13190 arguments for a member template.
13191 In that case each TREE_VEC in ORIG_T represents a level of template
13192 arguments, and ORIG_T won't carry any non defaulted argument count.
13193 It will rather be the nested TREE_VECs that will carry one.
13194 In other words, ORIG_T carries a non defaulted argument count only
13195 if it doesn't contain any nested TREE_VEC. */
13196 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13198 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13199 count
+= expanded_len_adjust
;
13200 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13202 for (i
= 0, out
= 0; i
< len
; i
++)
13204 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13205 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13206 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13210 /* Now expand the template argument pack "in place". */
13211 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13212 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13216 TREE_VEC_ELT (t
, out
) = elts
[i
];
13224 /* Substitute ARGS into one level PARMS of template parameters. */
13227 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13229 if (parms
== error_mark_node
)
13230 return error_mark_node
;
13232 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13234 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13236 tree tuple
= TREE_VEC_ELT (parms
, i
);
13238 if (tuple
== error_mark_node
)
13241 TREE_VEC_ELT (new_vec
, i
) =
13242 tsubst_template_parm (tuple
, args
, complain
);
13248 /* Return the result of substituting ARGS into the template parameters
13249 given by PARMS. If there are m levels of ARGS and m + n levels of
13250 PARMS, then the result will contain n levels of PARMS. For
13251 example, if PARMS is `template <class T> template <class U>
13252 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13253 result will be `template <int*, double, class V>'. */
13256 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13258 tree r
= NULL_TREE
;
13261 /* When substituting into a template, we must set
13262 PROCESSING_TEMPLATE_DECL as the template parameters may be
13263 dependent if they are based on one-another, and the dependency
13264 predicates are short-circuit outside of templates. */
13265 ++processing_template_decl
;
13267 for (new_parms
= &r
;
13268 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13269 new_parms
= &(TREE_CHAIN (*new_parms
)),
13270 parms
= TREE_CHAIN (parms
))
13272 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13275 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13276 - TMPL_ARGS_DEPTH (args
)),
13277 new_vec
, NULL_TREE
);
13278 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13279 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13282 --processing_template_decl
;
13287 /* Return the result of substituting ARGS into one template parameter
13288 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13289 parameter and which TREE_PURPOSE is the default argument of the
13290 template parameter. */
13293 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13295 tree default_value
, parm_decl
;
13297 if (args
== NULL_TREE
13299 || t
== error_mark_node
)
13302 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13304 default_value
= TREE_PURPOSE (t
);
13305 parm_decl
= TREE_VALUE (t
);
13306 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13308 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13309 if (TREE_CODE (parm_decl
) == PARM_DECL
13310 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13311 parm_decl
= error_mark_node
;
13312 default_value
= tsubst_template_arg (default_value
, args
,
13313 complain
, NULL_TREE
);
13314 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13316 tree r
= build_tree_list (default_value
, parm_decl
);
13317 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13321 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13322 type T. If T is not an aggregate or enumeration type, it is
13323 handled as if by tsubst. IN_DECL is as for tsubst. If
13324 ENTERING_SCOPE is nonzero, T is the context for a template which
13325 we are presently tsubst'ing. Return the substituted value. */
13328 tsubst_aggr_type (tree t
,
13330 tsubst_flags_t complain
,
13332 int entering_scope
)
13334 if (t
== NULL_TREE
)
13337 switch (TREE_CODE (t
))
13340 if (TYPE_PTRMEMFUNC_P (t
))
13341 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13343 /* Fall through. */
13344 case ENUMERAL_TYPE
:
13346 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13352 /* In "sizeof(X<I>)" we need to evaluate "I". */
13355 /* First, determine the context for the type we are looking
13357 context
= TYPE_CONTEXT (t
);
13358 if (context
&& TYPE_P (context
))
13360 context
= tsubst_aggr_type (context
, args
, complain
,
13361 in_decl
, /*entering_scope=*/1);
13362 /* If context is a nested class inside a class template,
13363 it may still need to be instantiated (c++/33959). */
13364 context
= complete_type (context
);
13367 /* Then, figure out what arguments are appropriate for the
13368 type we are trying to find. For example, given:
13370 template <class T> struct S;
13371 template <class T, class U> void f(T, U) { S<U> su; }
13373 and supposing that we are instantiating f<int, double>,
13374 then our ARGS will be {int, double}, but, when looking up
13375 S we only want {double}. */
13376 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13377 complain
, in_decl
);
13378 if (argvec
== error_mark_node
)
13379 r
= error_mark_node
;
13382 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13383 entering_scope
, complain
);
13384 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13390 /* This is not a template type, so there's nothing to do. */
13394 return tsubst (t
, args
, complain
, in_decl
);
13398 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13400 /* Substitute into the default argument ARG (a default argument for
13401 FN), which has the indicated TYPE. */
13404 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13405 tsubst_flags_t complain
)
13407 int errs
= errorcount
+ sorrycount
;
13409 /* This can happen in invalid code. */
13410 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13413 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13414 parm
= chain_index (parmnum
, parm
);
13415 tree parmtype
= TREE_TYPE (parm
);
13416 if (DECL_BY_REFERENCE (parm
))
13417 parmtype
= TREE_TYPE (parmtype
);
13418 if (parmtype
== error_mark_node
)
13419 return error_mark_node
;
13421 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13424 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13427 /* This default argument came from a template. Instantiate the
13428 default argument here, not in tsubst. In the case of
13437 we must be careful to do name lookup in the scope of S<T>,
13438 rather than in the current class. */
13439 push_to_top_level ();
13440 push_access_scope (fn
);
13441 push_deferring_access_checks (dk_no_deferred
);
13442 start_lambda_scope (parm
);
13444 /* The default argument expression may cause implicitly defined
13445 member functions to be synthesized, which will result in garbage
13446 collection. We must treat this situation as if we were within
13447 the body of function so as to avoid collecting live data on the
13450 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13451 complain
, NULL_TREE
,
13452 /*integral_constant_expression_p=*/false);
13455 finish_lambda_scope ();
13457 /* Make sure the default argument is reasonable. */
13458 arg
= check_default_argument (type
, arg
, complain
);
13460 if (errorcount
+sorrycount
> errs
13461 && (complain
& tf_warning_or_error
))
13462 inform (input_location
,
13463 " when instantiating default argument for call to %qD", fn
);
13465 pop_deferring_access_checks ();
13466 pop_access_scope (fn
);
13467 pop_from_top_level ();
13469 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13472 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13473 defarg_inst
->put (parm
, arg
);
13479 /* Substitute into all the default arguments for FN. */
13482 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13487 tmpl_args
= DECL_TI_ARGS (fn
);
13489 /* If this function is not yet instantiated, we certainly don't need
13490 its default arguments. */
13491 if (uses_template_parms (tmpl_args
))
13493 /* Don't do this again for clones. */
13494 if (DECL_CLONED_FUNCTION_P (fn
))
13498 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13500 arg
= TREE_CHAIN (arg
), ++i
)
13501 if (TREE_PURPOSE (arg
))
13502 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13504 TREE_PURPOSE (arg
),
13508 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13509 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13511 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13514 store_explicit_specifier (tree v
, tree t
)
13516 if (!explicit_specifier_map
)
13517 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13518 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13519 explicit_specifier_map
->put (v
, t
);
13522 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13525 lookup_explicit_specifier (tree v
)
13527 return *explicit_specifier_map
->get (v
);
13530 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13531 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13532 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13536 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13537 tree raises
, tsubst_flags_t complain
)
13539 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13542 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13544 new_type
= build_function_type (return_type
, arg_types
);
13545 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13549 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13550 /* Don't pick up extra function qualifiers from the basetype. */
13551 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13552 if (! MAYBE_CLASS_TYPE_P (r
))
13556 Type deduction may fail for any of the following
13559 -- Attempting to create "pointer to member of T" when T
13560 is not a class type. */
13561 if (complain
& tf_error
)
13562 error ("creating pointer to member function of non-class type %qT",
13564 return error_mark_node
;
13567 new_type
= build_method_type_directly (r
, return_type
,
13568 TREE_CHAIN (arg_types
));
13570 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13572 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13573 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13574 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13577 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13578 each of its formal parameters. If there is a disagreement then rebuild
13579 DECL's function type according to its formal parameter types, as part of a
13580 resolution for Core issues 1001/1322. */
13583 maybe_rebuild_function_decl_type (tree decl
)
13585 bool function_type_needs_rebuilding
= false;
13586 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13588 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13589 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13591 tree parm_type
= TREE_VALUE (parm_type_list
);
13592 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13593 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13595 function_type_needs_rebuilding
= true;
13599 parm_list
= DECL_CHAIN (parm_list
);
13600 parm_type_list
= TREE_CHAIN (parm_type_list
);
13604 if (!function_type_needs_rebuilding
)
13607 const tree fntype
= TREE_TYPE (decl
);
13608 tree parm_list
= DECL_ARGUMENTS (decl
);
13609 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13610 tree new_parm_type_list
= NULL_TREE
;
13611 tree
*q
= &new_parm_type_list
;
13612 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13614 *q
= copy_node (old_parm_type_list
);
13615 parm_list
= DECL_CHAIN (parm_list
);
13616 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13617 q
= &TREE_CHAIN (*q
);
13619 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13621 *q
= copy_node (old_parm_type_list
);
13622 tree
*new_parm_type
= &TREE_VALUE (*q
);
13623 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13624 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13625 *new_parm_type
= formal_parm_type_unqual
;
13627 parm_list
= DECL_CHAIN (parm_list
);
13628 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13629 q
= &TREE_CHAIN (*q
);
13631 if (old_parm_type_list
== void_list_node
)
13632 *q
= void_list_node
;
13635 = rebuild_function_or_method_type (fntype
,
13636 TREE_TYPE (fntype
), new_parm_type_list
,
13637 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13640 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13643 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13644 tree lambda_fntype
)
13646 tree gen_tmpl
, argvec
;
13647 hashval_t hash
= 0;
13650 /* Nobody should be tsubst'ing into non-template functions. */
13651 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13653 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13655 /* If T is not dependent, just return it. */
13656 if (!uses_template_parms (DECL_TI_ARGS (t
))
13657 && !LAMBDA_FUNCTION_P (t
))
13660 /* Calculate the most general template of which R is a
13662 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13664 /* We're substituting a lambda function under tsubst_lambda_expr but not
13665 directly from it; find the matching function we're already inside.
13666 But don't do this if T is a generic lambda with a single level of
13667 template parms, as in that case we're doing a normal instantiation. */
13668 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13669 && (!generic_lambda_fn_p (t
)
13670 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13671 return enclosing_instantiation_of (t
);
13673 /* Calculate the complete set of arguments used to
13675 argvec
= tsubst_template_args (DECL_TI_ARGS
13676 (DECL_TEMPLATE_RESULT
13677 (DECL_TI_TEMPLATE (t
))),
13678 args
, complain
, in_decl
);
13679 if (argvec
== error_mark_node
)
13680 return error_mark_node
;
13682 /* Check to see if we already have this specialization. */
13683 if (!lambda_fntype
)
13685 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13686 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13690 /* We can see more levels of arguments than parameters if
13691 there was a specialization of a member template, like
13694 template <class T> struct S { template <class U> void f(); }
13695 template <> template <class U> void S<int>::f(U);
13697 Here, we'll be substituting into the specialization,
13698 because that's where we can find the code we actually
13699 want to generate, but we'll have enough arguments for
13700 the most general template.
13702 We also deal with the peculiar case:
13704 template <class T> struct S {
13705 template <class U> friend void f();
13707 template <class U> void f() {}
13709 template void f<double>();
13711 Here, the ARGS for the instantiation of will be {int,
13712 double}. But, we only need as many ARGS as there are
13713 levels of template parameters in CODE_PATTERN. We are
13714 careful not to get fooled into reducing the ARGS in
13717 template <class T> struct S { template <class U> void f(U); }
13718 template <class T> template <> void S<T>::f(int) {}
13720 which we can spot because the pattern will be a
13721 specialization in this case. */
13722 int args_depth
= TMPL_ARGS_DEPTH (args
);
13724 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13726 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13727 args
= get_innermost_template_args (args
, parms_depth
);
13731 /* This special case arises when we have something like this:
13733 template <class T> struct S {
13734 friend void f<int>(int, double);
13737 Here, the DECL_TI_TEMPLATE for the friend declaration
13738 will be an IDENTIFIER_NODE. We are being called from
13739 tsubst_friend_function, and we want only to create a
13740 new decl (R) with appropriate types so that we can call
13741 determine_specialization. */
13742 gen_tmpl
= NULL_TREE
;
13743 argvec
= NULL_TREE
;
13746 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13748 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13749 bool member
= ctx
&& TYPE_P (ctx
);
13751 if (member
&& !closure
)
13752 ctx
= tsubst_aggr_type (ctx
, args
,
13753 complain
, t
, /*entering_scope=*/1);
13755 tree type
= (lambda_fntype
? lambda_fntype
13756 : tsubst (TREE_TYPE (t
), args
,
13757 complain
| tf_fndecl_type
, in_decl
));
13758 if (type
== error_mark_node
)
13759 return error_mark_node
;
13761 /* If we hit excessive deduction depth, the type is bogus even if
13762 it isn't error_mark_node, so don't build a decl. */
13763 if (excessive_deduction_depth
)
13764 return error_mark_node
;
13766 /* We do NOT check for matching decls pushed separately at this
13767 point, as they may not represent instantiations of this
13768 template, and in any case are considered separate under the
13770 tree r
= copy_decl (t
);
13771 DECL_USE_TEMPLATE (r
) = 0;
13772 TREE_TYPE (r
) = type
;
13773 /* Clear out the mangled name and RTL for the instantiation. */
13774 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13775 SET_DECL_RTL (r
, NULL
);
13776 /* Leave DECL_INITIAL set on deleted instantiations. */
13777 if (!DECL_DELETED_FN (r
))
13778 DECL_INITIAL (r
) = NULL_TREE
;
13779 DECL_CONTEXT (r
) = ctx
;
13781 /* Handle explicit(dependent-expr). */
13782 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13784 tree spec
= lookup_explicit_specifier (t
);
13785 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13786 /*function_p=*/false,
13788 spec
= build_explicit_specifier (spec
, complain
);
13789 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13792 /* OpenMP UDRs have the only argument a reference to the declared
13793 type. We want to diagnose if the declared type is a reference,
13794 which is invalid, but as references to references are usually
13795 quietly merged, diagnose it here. */
13796 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13799 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13800 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13801 if (TYPE_REF_P (argtype
))
13802 error_at (DECL_SOURCE_LOCATION (t
),
13803 "reference type %qT in "
13804 "%<#pragma omp declare reduction%>", argtype
);
13805 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13806 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13810 if (member
&& DECL_CONV_FN_P (r
))
13811 /* Type-conversion operator. Reconstruct the name, in
13812 case it's the name of one of the template's parameters. */
13813 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13815 tree parms
= DECL_ARGUMENTS (t
);
13817 parms
= DECL_CHAIN (parms
);
13818 parms
= tsubst (parms
, args
, complain
, t
);
13819 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13820 DECL_CONTEXT (parm
) = r
;
13823 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13824 DECL_NAME (tparm
) = closure_identifier
;
13825 DECL_CHAIN (tparm
) = parms
;
13828 DECL_ARGUMENTS (r
) = parms
;
13829 DECL_RESULT (r
) = NULL_TREE
;
13831 maybe_rebuild_function_decl_type (r
);
13833 TREE_STATIC (r
) = 0;
13834 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13835 DECL_EXTERNAL (r
) = 1;
13836 /* If this is an instantiation of a function with internal
13837 linkage, we already know what object file linkage will be
13838 assigned to the instantiation. */
13839 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13840 DECL_DEFER_OUTPUT (r
) = 0;
13841 DECL_CHAIN (r
) = NULL_TREE
;
13842 DECL_PENDING_INLINE_INFO (r
) = 0;
13843 DECL_PENDING_INLINE_P (r
) = 0;
13844 DECL_SAVED_TREE (r
) = NULL_TREE
;
13845 DECL_STRUCT_FUNCTION (r
) = NULL
;
13847 /* We'll re-clone as appropriate in instantiate_template. */
13848 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13850 /* If we aren't complaining now, return on error before we register
13851 the specialization so that we'll complain eventually. */
13852 if ((complain
& tf_error
) == 0
13853 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13854 && !grok_op_properties (r
, /*complain=*/false))
13855 return error_mark_node
;
13857 /* Associate the constraints directly with the instantiation. We
13858 don't substitute through the constraints; that's only done when
13859 they are checked. */
13860 if (tree ci
= get_constraints (t
))
13861 set_constraints (r
, ci
);
13863 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13864 SET_DECL_FRIEND_CONTEXT (r
,
13865 tsubst (DECL_FRIEND_CONTEXT (t
),
13866 args
, complain
, in_decl
));
13868 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13869 this in the special friend case mentioned above where
13870 GEN_TMPL is NULL. */
13871 if (gen_tmpl
&& !closure
)
13873 DECL_TEMPLATE_INFO (r
)
13874 = build_template_info (gen_tmpl
, argvec
);
13875 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13878 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13880 /* We instantiated this while substituting into
13881 the type earlier (template/friend54.C). */
13884 /* We're not supposed to instantiate default arguments
13885 until they are called, for a template. But, for a
13888 template <class T> void f ()
13889 { extern void g(int i = T()); }
13891 we should do the substitution when the template is
13892 instantiated. We handle the member function case in
13893 instantiate_class_template since the default arguments
13894 might refer to other members of the class. */
13896 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13897 && !uses_template_parms (argvec
))
13898 tsubst_default_arguments (r
, complain
);
13901 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13903 /* Copy the list of befriending classes. */
13904 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13906 friends
= &TREE_CHAIN (*friends
))
13908 *friends
= copy_node (*friends
);
13909 TREE_VALUE (*friends
)
13910 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13913 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13915 maybe_retrofit_in_chrg (r
);
13916 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13917 return error_mark_node
;
13918 /* If this is an instantiation of a member template, clone it.
13919 If it isn't, that'll be handled by
13920 clone_constructors_and_destructors. */
13921 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13922 clone_function_decl (r
, /*update_methods=*/false);
13924 else if ((complain
& tf_error
) != 0
13925 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13926 && !grok_op_properties (r
, /*complain=*/true))
13927 return error_mark_node
;
13929 /* Possibly limit visibility based on template args. */
13930 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13931 if (DECL_VISIBILITY_SPECIFIED (t
))
13933 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13934 DECL_ATTRIBUTES (r
)
13935 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13937 determine_visibility (r
);
13938 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13939 && !processing_template_decl
)
13940 defaulted_late_check (r
);
13942 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13943 args
, complain
, in_decl
);
13945 if (tree attr
= lookup_attribute ("omp declare variant base",
13946 DECL_ATTRIBUTES (r
)))
13947 omp_declare_variant_finalize (r
, attr
);
13952 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13955 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13956 tree lambda_fntype
)
13958 /* We can get here when processing a member function template,
13959 member class template, or template template parameter. */
13960 tree decl
= DECL_TEMPLATE_RESULT (t
);
13966 hashval_t hash
= 0;
13968 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13970 /* Template template parameter is treated here. */
13971 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13972 if (new_type
== error_mark_node
)
13973 r
= error_mark_node
;
13974 /* If we get a real template back, return it. This can happen in
13975 the context of most_specialized_partial_spec. */
13976 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13979 /* The new TEMPLATE_DECL was built in
13980 reduce_template_parm_level. */
13981 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13985 if (!lambda_fntype
)
13987 /* We might already have an instance of this template.
13988 The ARGS are for the surrounding class type, so the
13989 full args contain the tsubst'd args for the context,
13990 plus the innermost args from the template decl. */
13991 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13992 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13993 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13994 /* Because this is a template, the arguments will still be
13995 dependent, even after substitution. If
13996 PROCESSING_TEMPLATE_DECL is not set, the dependency
13997 predicates will short-circuit. */
13998 ++processing_template_decl
;
13999 full_args
= tsubst_template_args (tmpl_args
, args
,
14000 complain
, in_decl
);
14001 --processing_template_decl
;
14002 if (full_args
== error_mark_node
)
14003 return error_mark_node
;
14005 /* If this is a default template template argument,
14006 tsubst might not have changed anything. */
14007 if (full_args
== tmpl_args
)
14010 hash
= hash_tmpl_and_args (t
, full_args
);
14011 spec
= retrieve_specialization (t
, full_args
, hash
);
14012 if (spec
!= NULL_TREE
)
14015 /* Type partial instantiations are stored as the type by
14016 lookup_template_class_1, not here as the template. */
14017 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14022 /* Make a new template decl. It will be similar to the
14023 original, but will record the current template arguments.
14024 We also create a new function declaration, which is just
14025 like the old one, but points to this new template, rather
14026 than the old one. */
14028 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14029 DECL_CHAIN (r
) = NULL_TREE
;
14031 // Build new template info linking to the original template decl.
14032 if (!lambda_fntype
)
14034 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14035 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14038 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14040 /* The template parameters for this new template are all the
14041 template parameters for the old template, except the
14042 outermost level of parameters. */
14043 DECL_TEMPLATE_PARMS (r
)
14044 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14047 bool class_p
= false;
14049 ++processing_template_decl
;
14050 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14051 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14054 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14057 inner
= TREE_TYPE (inner
);
14059 inner
= tsubst (inner
, args
, complain
, in_decl
);
14061 --processing_template_decl
;
14062 if (inner
== error_mark_node
)
14063 return error_mark_node
;
14067 /* For a partial specialization, we need to keep pointing to
14068 the primary template. */
14069 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14070 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14072 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14073 inner
= TYPE_MAIN_DECL (inner
);
14075 else if (lambda_fntype
)
14077 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14078 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14082 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14083 DECL_TI_TEMPLATE (inner
) = r
;
14084 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14087 DECL_TEMPLATE_RESULT (r
) = inner
;
14088 TREE_TYPE (r
) = TREE_TYPE (inner
);
14089 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14091 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14092 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14094 if (PRIMARY_TEMPLATE_P (t
))
14095 DECL_PRIMARY_TEMPLATE (r
) = r
;
14097 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14099 /* Record this non-type partial instantiation. */
14100 register_specialization (r
, t
,
14101 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14107 /* True if FN is the op() for a lambda in an uninstantiated template. */
14110 lambda_fn_in_template_p (tree fn
)
14112 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14114 tree closure
= DECL_CONTEXT (fn
);
14115 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14118 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14119 which the above is true. */
14122 instantiated_lambda_fn_p (tree fn
)
14124 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14126 tree closure
= DECL_CONTEXT (fn
);
14127 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14128 return LAMBDA_EXPR_INSTANTIATED (lam
);
14131 /* We're instantiating a variable from template function TCTX. Return the
14132 corresponding current enclosing scope. This gets complicated because lambda
14133 functions in templates are regenerated rather than instantiated, but generic
14134 lambda functions are subsequently instantiated. */
14137 enclosing_instantiation_of (tree otctx
)
14140 tree fn
= current_function_decl
;
14141 int lambda_count
= 0;
14143 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14144 || instantiated_lambda_fn_p (tctx
));
14145 tctx
= decl_function_context (tctx
))
14147 for (; fn
; fn
= decl_function_context (fn
))
14150 int flambda_count
= 0;
14151 for (; fn
&& instantiated_lambda_fn_p (fn
);
14152 fn
= decl_function_context (fn
))
14154 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14155 ? most_general_template (fn
) != most_general_template (tctx
)
14158 if (flambda_count
!= lambda_count
)
14160 gcc_assert (flambda_count
> lambda_count
);
14161 for (; flambda_count
> lambda_count
; --flambda_count
)
14162 ofn
= decl_function_context (ofn
);
14164 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14165 || DECL_CONV_FN_P (ofn
));
14168 gcc_unreachable ();
14171 /* Substitute the ARGS into the T, which is a _DECL. Return the
14172 result of the substitution. Issue error and warning messages under
14173 control of COMPLAIN. */
14176 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14178 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14179 location_t saved_loc
;
14180 tree r
= NULL_TREE
;
14182 hashval_t hash
= 0;
14184 /* Set the filename and linenumber to improve error-reporting. */
14185 saved_loc
= input_location
;
14186 input_location
= DECL_SOURCE_LOCATION (t
);
14188 switch (TREE_CODE (t
))
14190 case TEMPLATE_DECL
:
14191 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14194 case FUNCTION_DECL
:
14195 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14200 tree type
= NULL_TREE
;
14202 tree expanded_types
= NULL_TREE
;
14203 tree prev_r
= NULL_TREE
;
14204 tree first_r
= NULL_TREE
;
14206 if (DECL_PACK_P (t
))
14208 /* If there is a local specialization that isn't a
14209 parameter pack, it means that we're doing a "simple"
14210 substitution from inside tsubst_pack_expansion. Just
14211 return the local specialization (which will be a single
14213 tree spec
= retrieve_local_specialization (t
);
14215 && TREE_CODE (spec
) == PARM_DECL
14216 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14219 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14220 the parameters in this function parameter pack. */
14221 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14222 complain
, in_decl
);
14223 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14225 len
= TREE_VEC_LENGTH (expanded_types
);
14227 /* Zero-length parameter packs are boring. Just substitute
14229 if (len
== 0 && !cp_unevaluated_operand
)
14230 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14235 /* All we did was update the type. Make a note of that. */
14236 type
= expanded_types
;
14237 expanded_types
= NULL_TREE
;
14241 /* Loop through all of the parameters we'll build. When T is
14242 a function parameter pack, LEN is the number of expanded
14243 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14245 for (i
= 0; i
< len
; ++i
)
14249 if (DECL_TEMPLATE_PARM_P (t
))
14250 SET_DECL_TEMPLATE_PARM_P (r
);
14252 if (expanded_types
)
14253 /* We're on the Ith parameter of the function parameter
14256 /* Get the Ith type. */
14257 type
= TREE_VEC_ELT (expanded_types
, i
);
14259 /* Rename the parameter to include the index. */
14261 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14264 /* We're dealing with a normal parameter. */
14265 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14267 type
= type_decays_to (type
);
14268 TREE_TYPE (r
) = type
;
14269 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14271 if (DECL_INITIAL (r
))
14273 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14274 DECL_INITIAL (r
) = TREE_TYPE (r
);
14276 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14277 complain
, in_decl
);
14280 DECL_CONTEXT (r
) = NULL_TREE
;
14282 if (!DECL_TEMPLATE_PARM_P (r
))
14283 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14285 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14286 args
, complain
, in_decl
);
14288 /* Keep track of the first new parameter we
14289 generate. That's what will be returned to the
14294 /* Build a proper chain of parameters when substituting
14295 into a function parameter pack. */
14297 DECL_CHAIN (prev_r
) = r
;
14300 /* If cp_unevaluated_operand is set, we're just looking for a
14301 single dummy parameter, so don't keep going. */
14302 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14303 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14304 complain
, DECL_CHAIN (t
));
14306 /* FIRST_R contains the start of the chain we've built. */
14313 tree type
= NULL_TREE
;
14314 tree vec
= NULL_TREE
;
14315 tree expanded_types
= NULL_TREE
;
14318 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14320 /* This field is a lambda capture pack. Return a TREE_VEC of
14321 the expanded fields to instantiate_class_template_1. */
14322 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14323 complain
, in_decl
);
14324 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14326 len
= TREE_VEC_LENGTH (expanded_types
);
14327 vec
= make_tree_vec (len
);
14331 /* All we did was update the type. Make a note of that. */
14332 type
= expanded_types
;
14333 expanded_types
= NULL_TREE
;
14337 for (int i
= 0; i
< len
; ++i
)
14340 if (expanded_types
)
14342 type
= TREE_VEC_ELT (expanded_types
, i
);
14344 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14347 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14349 if (type
== error_mark_node
)
14350 RETURN (error_mark_node
);
14351 TREE_TYPE (r
) = type
;
14352 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14354 if (DECL_C_BIT_FIELD (r
))
14355 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14357 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14358 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14360 /*integral_constant_expression_p=*/true);
14361 if (DECL_INITIAL (t
))
14363 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14364 NSDMI in perform_member_init. Still set DECL_INITIAL
14365 so that we know there is one. */
14366 DECL_INITIAL (r
) = void_node
;
14367 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14368 retrofit_lang_decl (r
);
14369 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14371 /* We don't have to set DECL_CONTEXT here; it is set by
14372 finish_member_declaration. */
14373 DECL_CHAIN (r
) = NULL_TREE
;
14375 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14376 args
, complain
, in_decl
);
14379 TREE_VEC_ELT (vec
, i
) = r
;
14388 /* We reach here only for member using decls. We also need to check
14389 uses_template_parms because DECL_DEPENDENT_P is not set for a
14390 using-declaration that designates a member of the current
14391 instantiation (c++/53549). */
14392 if (DECL_DEPENDENT_P (t
)
14393 || uses_template_parms (USING_DECL_SCOPE (t
)))
14395 tree scope
= USING_DECL_SCOPE (t
);
14396 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14397 if (PACK_EXPANSION_P (scope
))
14399 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14400 int len
= TREE_VEC_LENGTH (vec
);
14401 r
= make_tree_vec (len
);
14402 for (int i
= 0; i
< len
; ++i
)
14404 tree escope
= TREE_VEC_ELT (vec
, i
);
14405 tree elt
= do_class_using_decl (escope
, name
);
14408 r
= error_mark_node
;
14413 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14414 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14416 TREE_VEC_ELT (r
, i
) = elt
;
14421 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14422 complain
, in_decl
);
14423 r
= do_class_using_decl (inst_scope
, name
);
14425 r
= error_mark_node
;
14428 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14429 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14436 DECL_CHAIN (r
) = NULL_TREE
;
14443 tree argvec
= NULL_TREE
;
14444 tree gen_tmpl
= NULL_TREE
;
14446 tree tmpl
= NULL_TREE
;
14448 tree type
= NULL_TREE
;
14451 if (TREE_TYPE (t
) == error_mark_node
)
14452 RETURN (error_mark_node
);
14454 if (TREE_CODE (t
) == TYPE_DECL
14455 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14457 /* If this is the canonical decl, we don't have to
14458 mess with instantiations, and often we can't (for
14459 typename, template type parms and such). Note that
14460 TYPE_NAME is not correct for the above test if
14461 we've copied the type for a typedef. */
14462 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14463 if (type
== error_mark_node
)
14464 RETURN (error_mark_node
);
14465 r
= TYPE_NAME (type
);
14469 /* Check to see if we already have the specialization we
14472 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14474 /* T is a static data member or namespace-scope entity.
14475 We have to substitute into namespace-scope variables
14476 (not just variable templates) because of cases like:
14478 template <class T> void f() { extern T t; }
14480 where the entity referenced is not known until
14481 instantiation time. */
14483 ctx
= DECL_CONTEXT (t
);
14484 if (DECL_CLASS_SCOPE_P (t
))
14486 ctx
= tsubst_aggr_type (ctx
, args
,
14488 in_decl
, /*entering_scope=*/1);
14489 /* If CTX is unchanged, then T is in fact the
14490 specialization we want. That situation occurs when
14491 referencing a static data member within in its own
14492 class. We can use pointer equality, rather than
14493 same_type_p, because DECL_CONTEXT is always
14495 if (ctx
== DECL_CONTEXT (t
)
14496 /* ... unless T is a member template; in which
14497 case our caller can be willing to create a
14498 specialization of that template represented
14500 && !(DECL_TI_TEMPLATE (t
)
14501 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14507 tmpl
= DECL_TI_TEMPLATE (t
);
14508 gen_tmpl
= most_general_template (tmpl
);
14509 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14510 if (argvec
!= error_mark_node
)
14511 argvec
= (coerce_innermost_template_parms
14512 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14513 argvec
, t
, complain
,
14514 /*all*/true, /*defarg*/true));
14515 if (argvec
== error_mark_node
)
14516 RETURN (error_mark_node
);
14517 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14518 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14523 /* A local variable. */
14525 /* Subsequent calls to pushdecl will fill this in. */
14527 /* Unless this is a reference to a static variable from an
14528 enclosing function, in which case we need to fill it in now. */
14529 if (TREE_STATIC (t
))
14531 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14532 if (fn
!= current_function_decl
)
14535 spec
= retrieve_local_specialization (t
);
14537 /* If we already have the specialization we need, there is
14538 nothing more to do. */
14545 /* Create a new node for the specialization we need. */
14546 if (type
== NULL_TREE
)
14548 if (is_typedef_decl (t
))
14549 type
= DECL_ORIGINAL_TYPE (t
);
14551 type
= TREE_TYPE (t
);
14553 && VAR_HAD_UNKNOWN_BOUND (t
)
14554 && type
!= error_mark_node
)
14555 type
= strip_array_domain (type
);
14556 tree sub_args
= args
;
14557 if (tree auto_node
= type_uses_auto (type
))
14559 /* Mask off any template args past the variable's context so we
14560 don't replace the auto with an unrelated argument. */
14561 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14562 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14564 /* This should never happen with the new lambda instantiation
14565 model, but keep the handling just in case. */
14566 gcc_assert (!CHECKING_P
),
14567 sub_args
= strip_innermost_template_args (args
, extra
);
14569 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14570 /* Substituting the type might have recursively instantiated this
14571 same alias (c++/86171). */
14572 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14573 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14582 DECL_INITIALIZED_P (r
) = 0;
14583 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14584 if (type
== error_mark_node
)
14585 RETURN (error_mark_node
);
14586 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14588 /* It may seem that this case cannot occur, since:
14593 declares a function, not a variable. However:
14596 template <typename T> void g() { T t; }
14597 template void g<f>();
14599 is an attempt to declare a variable with function
14601 error ("variable %qD has function type",
14602 /* R is not yet sufficiently initialized, so we
14603 just use its name. */
14605 RETURN (error_mark_node
);
14607 type
= complete_type (type
);
14608 /* Wait until cp_finish_decl to set this again, to handle
14609 circular dependency (template/instantiate6.C). */
14610 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14611 type
= check_var_type (DECL_NAME (r
), type
,
14612 DECL_SOURCE_LOCATION (r
));
14613 if (DECL_HAS_VALUE_EXPR_P (t
))
14615 tree ve
= DECL_VALUE_EXPR (t
);
14616 /* If the DECL_VALUE_EXPR is converted to the declared type,
14617 preserve the identity so that gimplify_type_sizes works. */
14618 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14620 ve
= TREE_OPERAND (ve
, 0);
14621 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14622 /*constant_expression_p=*/false);
14623 if (REFERENCE_REF_P (ve
))
14625 gcc_assert (TYPE_REF_P (type
));
14626 ve
= TREE_OPERAND (ve
, 0);
14629 ve
= build_nop (type
, ve
);
14630 else if (DECL_LANG_SPECIFIC (t
)
14631 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14632 && TREE_CODE (ve
) == COMPONENT_REF
14633 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14634 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14635 type
= TREE_TYPE (ve
);
14637 gcc_checking_assert (TREE_TYPE (ve
) == type
);
14638 SET_DECL_VALUE_EXPR (r
, ve
);
14640 if (CP_DECL_THREAD_LOCAL_P (r
)
14641 && !processing_template_decl
)
14642 set_decl_tls_model (r
, decl_default_tls_model (r
));
14644 else if (DECL_SELF_REFERENCE_P (t
))
14645 SET_DECL_SELF_REFERENCE_P (r
);
14646 TREE_TYPE (r
) = type
;
14647 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14648 DECL_CONTEXT (r
) = ctx
;
14649 /* Clear out the mangled name and RTL for the instantiation. */
14650 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14651 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14652 SET_DECL_RTL (r
, NULL
);
14653 /* The initializer must not be expanded until it is required;
14654 see [temp.inst]. */
14655 DECL_INITIAL (r
) = NULL_TREE
;
14656 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14659 if (DECL_LANG_SPECIFIC (r
))
14660 SET_DECL_DEPENDENT_INIT_P (r
, false);
14662 SET_DECL_MODE (r
, VOIDmode
);
14664 /* Possibly limit visibility based on template args. */
14665 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14666 if (DECL_VISIBILITY_SPECIFIED (t
))
14668 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14669 DECL_ATTRIBUTES (r
)
14670 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14672 determine_visibility (r
);
14677 /* A static data member declaration is always marked
14678 external when it is declared in-class, even if an
14679 initializer is present. We mimic the non-template
14680 processing here. */
14681 DECL_EXTERNAL (r
) = 1;
14682 if (DECL_NAMESPACE_SCOPE_P (t
))
14683 DECL_NOT_REALLY_EXTERN (r
) = 1;
14685 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14686 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14687 /* Remember whether we require constant initialization of
14688 a non-constant template variable. */
14689 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14690 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14691 if (!error_operand_p (r
) || (complain
& tf_error
))
14692 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14696 if (DECL_LANG_SPECIFIC (r
))
14697 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14698 if (!cp_unevaluated_operand
)
14699 register_local_specialization (r
, t
);
14702 DECL_CHAIN (r
) = NULL_TREE
;
14704 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14706 args
, complain
, in_decl
);
14708 /* Preserve a typedef that names a type. */
14709 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14711 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14712 set_underlying_type (r
);
14713 if (TYPE_DECL_ALIAS_P (r
))
14714 /* An alias template specialization can be dependent
14715 even if its underlying type is not. */
14716 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14719 layout_decl (r
, 0);
14724 gcc_unreachable ();
14729 /* Restore the file and line information. */
14730 input_location
= saved_loc
;
14735 /* Substitute into the complete parameter type list PARMS. */
14738 tsubst_function_parms (tree parms
,
14740 tsubst_flags_t complain
,
14743 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14746 /* Substitute into the ARG_TYPES of a function type.
14747 If END is a TREE_CHAIN, leave it and any following types
14751 tsubst_arg_types (tree arg_types
,
14754 tsubst_flags_t complain
,
14757 tree remaining_arg_types
;
14758 tree type
= NULL_TREE
;
14760 tree expanded_args
= NULL_TREE
;
14763 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14766 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14767 args
, end
, complain
, in_decl
);
14768 if (remaining_arg_types
== error_mark_node
)
14769 return error_mark_node
;
14771 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14773 /* For a pack expansion, perform substitution on the
14774 entire expression. Later on, we'll handle the arguments
14776 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14777 args
, complain
, in_decl
);
14779 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14780 /* So that we'll spin through the parameters, one by one. */
14781 i
= TREE_VEC_LENGTH (expanded_args
);
14784 /* We only partially substituted into the parameter
14785 pack. Our type is TYPE_PACK_EXPANSION. */
14786 type
= expanded_args
;
14787 expanded_args
= NULL_TREE
;
14795 type
= TREE_VEC_ELT (expanded_args
, i
);
14797 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14799 if (type
== error_mark_node
)
14800 return error_mark_node
;
14801 if (VOID_TYPE_P (type
))
14803 if (complain
& tf_error
)
14805 error ("invalid parameter type %qT", type
);
14807 error ("in declaration %q+D", in_decl
);
14809 return error_mark_node
;
14812 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14813 return error_mark_node
;
14815 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14816 top-level qualifiers as required. */
14817 type
= cv_unqualified (type_decays_to (type
));
14819 /* We do not substitute into default arguments here. The standard
14820 mandates that they be instantiated only when needed, which is
14821 done in build_over_call. */
14822 default_arg
= TREE_PURPOSE (arg_types
);
14824 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14825 since the new op() won't have any associated template arguments for us
14826 to refer to later. */
14827 if (lambda_fn_in_template_p (in_decl
))
14828 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14829 false/*fn*/, false/*constexpr*/);
14831 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14833 /* We've instantiated a template before its default arguments
14834 have been parsed. This can happen for a nested template
14835 class, and is not an error unless we require the default
14836 argument in a call of this function. */
14837 remaining_arg_types
=
14838 tree_cons (default_arg
, type
, remaining_arg_types
);
14839 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14840 remaining_arg_types
);
14843 remaining_arg_types
=
14844 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14847 return remaining_arg_types
;
14850 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14851 *not* handle the exception-specification for FNTYPE, because the
14852 initial substitution of explicitly provided template parameters
14853 during argument deduction forbids substitution into the
14854 exception-specification:
14858 All references in the function type of the function template to the
14859 corresponding template parameters are replaced by the specified tem-
14860 plate argument values. If a substitution in a template parameter or
14861 in the function type of the function template results in an invalid
14862 type, type deduction fails. [Note: The equivalent substitution in
14863 exception specifications is done only when the function is instanti-
14864 ated, at which point a program is ill-formed if the substitution
14865 results in an invalid type.] */
14868 tsubst_function_type (tree t
,
14870 tsubst_flags_t complain
,
14874 tree arg_types
= NULL_TREE
;
14876 /* The TYPE_CONTEXT is not used for function/method types. */
14877 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14879 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14881 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14883 if (late_return_type_p
)
14885 /* Substitute the argument types. */
14886 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14887 complain
, in_decl
);
14888 if (arg_types
== error_mark_node
)
14889 return error_mark_node
;
14891 tree save_ccp
= current_class_ptr
;
14892 tree save_ccr
= current_class_ref
;
14893 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14894 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14895 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14898 /* DR 1207: 'this' is in scope in the trailing return type. */
14899 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14902 /* Substitute the return type. */
14903 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14907 current_class_ptr
= save_ccp
;
14908 current_class_ref
= save_ccr
;
14912 /* Substitute the return type. */
14913 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14915 if (return_type
== error_mark_node
)
14916 return error_mark_node
;
14917 /* DR 486 clarifies that creation of a function type with an
14918 invalid return type is a deduction failure. */
14919 if (TREE_CODE (return_type
) == ARRAY_TYPE
14920 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14922 if (complain
& tf_error
)
14924 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14925 error ("function returning an array");
14927 error ("function returning a function");
14929 return error_mark_node
;
14932 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14933 return error_mark_node
;
14935 if (!late_return_type_p
)
14937 /* Substitute the argument types. */
14938 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14939 complain
, in_decl
);
14940 if (arg_types
== error_mark_node
)
14941 return error_mark_node
;
14944 /* Construct a new type node and return it. */
14945 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
14946 /*raises=*/NULL_TREE
, complain
);
14949 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14950 ARGS into that specification, and return the substituted
14951 specification. If there is no specification, return NULL_TREE. */
14954 tsubst_exception_specification (tree fntype
,
14956 tsubst_flags_t complain
,
14963 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14964 new_specs
= NULL_TREE
;
14965 if (specs
&& TREE_PURPOSE (specs
))
14967 /* A noexcept-specifier. */
14968 tree expr
= TREE_PURPOSE (specs
);
14969 if (TREE_CODE (expr
) == INTEGER_CST
)
14973 /* Defer instantiation of noexcept-specifiers to avoid
14974 excessive instantiations (c++/49107). */
14975 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14976 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14978 /* We already partially instantiated this member template,
14979 so combine the new args with the old. */
14980 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14981 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14982 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14983 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14987 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14988 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14993 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14995 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14997 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14999 new_specs
= tsubst_copy_and_build
15000 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15001 /*integral_constant_expression_p=*/true);
15003 new_specs
= build_noexcept_spec (new_specs
, complain
);
15007 if (! TREE_VALUE (specs
))
15014 tree expanded_specs
= NULL_TREE
;
15016 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15018 /* Expand the pack expansion type. */
15019 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15023 if (expanded_specs
== error_mark_node
)
15024 return error_mark_node
;
15025 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15026 len
= TREE_VEC_LENGTH (expanded_specs
);
15029 /* We're substituting into a member template, so
15030 we got a TYPE_PACK_EXPANSION back. Add that
15031 expansion and move on. */
15032 gcc_assert (TREE_CODE (expanded_specs
)
15033 == TYPE_PACK_EXPANSION
);
15034 new_specs
= add_exception_specifier (new_specs
,
15037 specs
= TREE_CHAIN (specs
);
15042 for (i
= 0; i
< len
; ++i
)
15044 if (expanded_specs
)
15045 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15047 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15048 if (spec
== error_mark_node
)
15050 new_specs
= add_exception_specifier (new_specs
, spec
,
15054 specs
= TREE_CHAIN (specs
);
15060 /* Substitute through a TREE_LIST of types or expressions, handling pack
15064 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15066 if (t
== void_list_node
)
15069 tree purpose
= TREE_PURPOSE (t
);
15070 tree purposevec
= NULL_TREE
;
15073 else if (PACK_EXPANSION_P (purpose
))
15075 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15076 if (TREE_CODE (purpose
) == TREE_VEC
)
15077 purposevec
= purpose
;
15079 else if (TYPE_P (purpose
))
15080 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15082 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15083 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15084 return error_mark_node
;
15086 tree value
= TREE_VALUE (t
);
15087 tree valuevec
= NULL_TREE
;
15090 else if (PACK_EXPANSION_P (value
))
15092 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15093 if (TREE_CODE (value
) == TREE_VEC
)
15096 else if (TYPE_P (value
))
15097 value
= tsubst (value
, args
, complain
, in_decl
);
15099 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15100 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15101 return error_mark_node
;
15103 tree chain
= TREE_CHAIN (t
);
15106 else if (TREE_CODE (chain
) == TREE_LIST
)
15107 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15108 else if (TYPE_P (chain
))
15109 chain
= tsubst (chain
, args
, complain
, in_decl
);
15111 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15112 if (chain
== error_mark_node
)
15113 return error_mark_node
;
15115 if (purpose
== TREE_PURPOSE (t
)
15116 && value
== TREE_VALUE (t
)
15117 && chain
== TREE_CHAIN (t
))
15121 /* Determine the number of arguments. */
15124 len
= TREE_VEC_LENGTH (purposevec
);
15125 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15128 len
= TREE_VEC_LENGTH (valuevec
);
15132 for (int i
= len
; i
-- > 0; )
15135 purpose
= TREE_VEC_ELT (purposevec
, i
);
15137 value
= TREE_VEC_ELT (valuevec
, i
);
15139 if (value
&& TYPE_P (value
))
15140 chain
= hash_tree_cons (purpose
, value
, chain
);
15142 chain
= tree_cons (purpose
, value
, chain
);
15148 /* Take the tree structure T and replace template parameters used
15149 therein with the argument vector ARGS. IN_DECL is an associated
15150 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15151 Issue error and warning messages under control of COMPLAIN. Note
15152 that we must be relatively non-tolerant of extensions here, in
15153 order to preserve conformance; if we allow substitutions that
15154 should not be allowed, we may allow argument deductions that should
15155 not succeed, and therefore report ambiguous overload situations
15156 where there are none. In theory, we could allow the substitution,
15157 but indicate that it should have failed, and allow our caller to
15158 make sure that the right thing happens, but we don't try to do this
15161 This function is used for dealing with types, decls and the like;
15162 for expressions, use tsubst_expr or tsubst_copy. */
15165 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15167 enum tree_code code
;
15168 tree type
, r
= NULL_TREE
;
15170 if (t
== NULL_TREE
|| t
== error_mark_node
15171 || t
== integer_type_node
15172 || t
== void_type_node
15173 || t
== char_type_node
15174 || t
== unknown_type_node
15175 || TREE_CODE (t
) == NAMESPACE_DECL
15176 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15180 return tsubst_decl (t
, args
, complain
);
15182 if (args
== NULL_TREE
)
15185 code
= TREE_CODE (t
);
15187 if (code
== IDENTIFIER_NODE
)
15188 type
= IDENTIFIER_TYPE_VALUE (t
);
15190 type
= TREE_TYPE (t
);
15192 gcc_assert (type
!= unknown_type_node
);
15194 /* Reuse typedefs. We need to do this to handle dependent attributes,
15195 such as attribute aligned. */
15197 && typedef_variant_p (t
))
15199 tree decl
= TYPE_NAME (t
);
15201 if (alias_template_specialization_p (t
, nt_opaque
))
15203 /* DECL represents an alias template and we want to
15205 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15206 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15207 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15209 else if (DECL_CLASS_SCOPE_P (decl
)
15210 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15211 && uses_template_parms (DECL_CONTEXT (decl
)))
15213 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15214 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15215 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15217 else if (DECL_FUNCTION_SCOPE_P (decl
)
15218 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15219 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15220 r
= retrieve_local_specialization (decl
);
15222 /* The typedef is from a non-template context. */
15228 r
= cp_build_qualified_type_real
15229 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15230 complain
| tf_ignore_bad_quals
);
15235 /* We don't have an instantiation yet, so drop the typedef. */
15236 int quals
= cp_type_quals (t
);
15237 t
= DECL_ORIGINAL_TYPE (decl
);
15238 t
= cp_build_qualified_type_real (t
, quals
,
15239 complain
| tf_ignore_bad_quals
);
15243 bool fndecl_type
= (complain
& tf_fndecl_type
);
15244 complain
&= ~tf_fndecl_type
;
15247 && code
!= TYPENAME_TYPE
15248 && code
!= TEMPLATE_TYPE_PARM
15249 && code
!= TEMPLATE_PARM_INDEX
15250 && code
!= IDENTIFIER_NODE
15251 && code
!= FUNCTION_TYPE
15252 && code
!= METHOD_TYPE
)
15253 type
= tsubst (type
, args
, complain
, in_decl
);
15254 if (type
== error_mark_node
)
15255 return error_mark_node
;
15261 case ENUMERAL_TYPE
:
15262 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15263 /*entering_scope=*/0);
15266 case IDENTIFIER_NODE
:
15277 if (t
== integer_type_node
)
15280 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15281 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15285 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15287 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15288 /*integral_constant_expression_p=*/false);
15290 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15292 if (TREE_CODE (max
) == NOP_EXPR
15293 && TREE_SIDE_EFFECTS (omax
)
15294 && !TREE_TYPE (max
))
15295 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15297 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15298 with TREE_SIDE_EFFECTS that indicates this is not an integral
15299 constant expression. */
15300 if (processing_template_decl
15301 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15303 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15304 TREE_SIDE_EFFECTS (max
) = 1;
15307 return compute_array_index_type (NULL_TREE
, max
, complain
);
15310 case TEMPLATE_TYPE_PARM
:
15311 case TEMPLATE_TEMPLATE_PARM
:
15312 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15313 case TEMPLATE_PARM_INDEX
:
15318 tree arg
= NULL_TREE
;
15322 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15323 template_parm_level_and_index (t
, &level
, &idx
);
15325 levels
= TMPL_ARGS_DEPTH (args
);
15326 if (level
<= levels
15327 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15329 arg
= TMPL_ARG (args
, level
, idx
);
15331 /* See through ARGUMENT_PACK_SELECT arguments. */
15332 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15333 arg
= argument_pack_select_arg (arg
);
15336 if (arg
== error_mark_node
)
15337 return error_mark_node
;
15338 else if (arg
!= NULL_TREE
)
15340 if (ARGUMENT_PACK_P (arg
))
15341 /* If ARG is an argument pack, we don't actually want to
15342 perform a substitution here, because substitutions
15343 for argument packs are only done
15344 element-by-element. We can get to this point when
15345 substituting the type of a non-type template
15346 parameter pack, when that type actually contains
15347 template parameter packs from an outer template, e.g.,
15349 template<typename... Types> struct A {
15350 template<Types... Values> struct B { };
15354 if (code
== TEMPLATE_TYPE_PARM
)
15358 /* When building concept checks for the purpose of
15359 deducing placeholders, we can end up with wildcards
15360 where types are expected. Adjust this to the deduced
15362 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15363 arg
= TREE_TYPE (TREE_TYPE (arg
));
15365 gcc_assert (TYPE_P (arg
));
15367 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15369 return cp_build_qualified_type_real
15370 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15372 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15374 /* We are processing a type constructed from a
15375 template template parameter. */
15376 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15377 args
, complain
, in_decl
);
15378 if (argvec
== error_mark_node
)
15379 return error_mark_node
;
15381 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15382 || TREE_CODE (arg
) == TEMPLATE_DECL
15383 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15385 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15386 /* Consider this code:
15388 template <template <class> class Template>
15390 template <class Arg> using Bind = Template<Arg>;
15393 template <template <class> class Template, class Arg>
15394 using Instantiate = Template<Arg>; //#0
15396 template <template <class> class Template,
15399 Instantiate<Internal<Template>::template Bind,
15402 When #1 is parsed, the
15403 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15404 parameter `Template' in #0 matches the
15405 UNBOUND_CLASS_TEMPLATE representing the argument
15406 `Internal<Template>::template Bind'; We then want
15407 to assemble the type `Bind<Argument>' that can't
15408 be fully created right now, because
15409 `Internal<Template>' not being complete, the Bind
15410 template cannot be looked up in that context. So
15411 we need to "store" `Bind<Argument>' for later
15412 when the context of Bind becomes complete. Let's
15413 store that in a TYPENAME_TYPE. */
15414 return make_typename_type (TYPE_CONTEXT (arg
),
15415 build_nt (TEMPLATE_ID_EXPR
,
15416 TYPE_IDENTIFIER (arg
),
15421 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15422 are resolving nested-types in the signature of a
15423 member function templates. Otherwise ARG is a
15424 TEMPLATE_DECL and is the real template to be
15426 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15427 arg
= TYPE_NAME (arg
);
15429 r
= lookup_template_class (arg
,
15431 DECL_CONTEXT (arg
),
15432 /*entering_scope=*/0,
15434 return cp_build_qualified_type_real
15435 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15437 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15440 /* TEMPLATE_PARM_INDEX. */
15441 return convert_from_reference (unshare_expr (arg
));
15445 /* This can happen during the attempted tsubst'ing in
15446 unify. This means that we don't yet have any information
15447 about the template parameter in question. */
15450 /* Early in template argument deduction substitution, we don't
15451 want to reduce the level of 'auto', or it will be confused
15452 with a normal template parm in subsequent deduction.
15453 Similarly, don't reduce the level of template parameters to
15454 avoid mismatches when deducing their types. */
15455 if (complain
& tf_partial
)
15458 /* If we get here, we must have been looking at a parm for a
15459 more deeply nested template. Make a new version of this
15460 template parameter, but with a lower level. */
15463 case TEMPLATE_TYPE_PARM
:
15464 case TEMPLATE_TEMPLATE_PARM
:
15465 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15466 if (cp_type_quals (t
))
15468 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15469 r
= cp_build_qualified_type_real
15470 (r
, cp_type_quals (t
),
15471 complain
| (code
== TEMPLATE_TYPE_PARM
15472 ? tf_ignore_bad_quals
: 0));
15474 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15475 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15476 && (r
= (TEMPLATE_PARM_DESCENDANTS
15477 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15478 && (r
= TREE_TYPE (r
))
15479 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15480 /* Break infinite recursion when substituting the constraints
15481 of a constrained placeholder. */;
15482 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15483 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15484 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15485 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15486 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15487 && (TEMPLATE_PARM_LEVEL (r
)
15488 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15489 /* Cache the simple case of lowering a type parameter. */
15494 TEMPLATE_TYPE_PARM_INDEX (r
)
15495 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15496 r
, levels
, args
, complain
);
15497 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15498 TYPE_MAIN_VARIANT (r
) = r
;
15499 TYPE_POINTER_TO (r
) = NULL_TREE
;
15500 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15502 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15504 /* Propagate constraints on placeholders since they are
15505 only instantiated during satisfaction. */
15506 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15507 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15508 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15510 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15511 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15515 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15516 /* We have reduced the level of the template
15517 template parameter, but not the levels of its
15518 template parameters, so canonical_type_parameter
15519 will not be able to find the canonical template
15520 template parameter for this level. Thus, we
15521 require structural equality checking to compare
15522 TEMPLATE_TEMPLATE_PARMs. */
15523 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15524 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15525 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15527 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15529 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15531 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15532 /* We might need to substitute into the types of non-type
15533 template parameters. */
15534 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15535 complain
, in_decl
);
15536 if (tmpl
== error_mark_node
)
15537 return error_mark_node
;
15538 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15539 complain
, in_decl
);
15540 if (argvec
== error_mark_node
)
15541 return error_mark_node
;
15543 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15544 = build_template_info (tmpl
, argvec
);
15549 case TEMPLATE_PARM_INDEX
:
15550 /* OK, now substitute the type of the non-type parameter. We
15551 couldn't do it earlier because it might be an auto parameter,
15552 and we wouldn't need to if we had an argument. */
15553 type
= tsubst (type
, args
, complain
, in_decl
);
15554 if (type
== error_mark_node
)
15555 return error_mark_node
;
15556 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15560 gcc_unreachable ();
15567 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15570 /* We should never be tsubsting a binfo. */
15571 gcc_unreachable ();
15574 /* A vector of template arguments. */
15575 gcc_assert (!type
);
15576 return tsubst_template_args (t
, args
, complain
, in_decl
);
15579 case REFERENCE_TYPE
:
15581 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15586 Type deduction may fail for any of the following
15589 -- Attempting to create a pointer to reference type.
15590 -- Attempting to create a reference to a reference type or
15591 a reference to void.
15593 Core issue 106 says that creating a reference to a reference
15594 during instantiation is no longer a cause for failure. We
15595 only enforce this check in strict C++98 mode. */
15596 if ((TYPE_REF_P (type
)
15597 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15598 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15600 static location_t last_loc
;
15602 /* We keep track of the last time we issued this error
15603 message to avoid spewing a ton of messages during a
15604 single bad template instantiation. */
15605 if (complain
& tf_error
15606 && last_loc
!= input_location
)
15608 if (VOID_TYPE_P (type
))
15609 error ("forming reference to void");
15610 else if (code
== POINTER_TYPE
)
15611 error ("forming pointer to reference type %qT", type
);
15613 error ("forming reference to reference type %qT", type
);
15614 last_loc
= input_location
;
15617 return error_mark_node
;
15619 else if (TREE_CODE (type
) == FUNCTION_TYPE
15620 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15621 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15623 if (complain
& tf_error
)
15625 if (code
== POINTER_TYPE
)
15626 error ("forming pointer to qualified function type %qT",
15629 error ("forming reference to qualified function type %qT",
15632 return error_mark_node
;
15634 else if (code
== POINTER_TYPE
)
15636 r
= build_pointer_type (type
);
15637 if (TREE_CODE (type
) == METHOD_TYPE
)
15638 r
= build_ptrmemfunc_type (r
);
15640 else if (TYPE_REF_P (type
))
15641 /* In C++0x, during template argument substitution, when there is an
15642 attempt to create a reference to a reference type, reference
15643 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15645 "If a template-argument for a template-parameter T names a type
15646 that is a reference to a type A, an attempt to create the type
15647 'lvalue reference to cv T' creates the type 'lvalue reference to
15648 A,' while an attempt to create the type type rvalue reference to
15649 cv T' creates the type T"
15651 r
= cp_build_reference_type
15653 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15655 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15656 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15658 if (r
!= error_mark_node
)
15659 /* Will this ever be needed for TYPE_..._TO values? */
15666 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15667 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15671 Type deduction may fail for any of the following
15674 -- Attempting to create "pointer to member of T" when T
15675 is not a class type. */
15676 if (complain
& tf_error
)
15677 error ("creating pointer to member of non-class type %qT", r
);
15678 return error_mark_node
;
15680 if (TYPE_REF_P (type
))
15682 if (complain
& tf_error
)
15683 error ("creating pointer to member reference type %qT", type
);
15684 return error_mark_node
;
15686 if (VOID_TYPE_P (type
))
15688 if (complain
& tf_error
)
15689 error ("creating pointer to member of type void");
15690 return error_mark_node
;
15692 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15693 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15695 /* The type of the implicit object parameter gets its
15696 cv-qualifiers from the FUNCTION_TYPE. */
15699 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15700 type_memfn_rqual (type
));
15701 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15702 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15706 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15710 case FUNCTION_TYPE
:
15715 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15716 if (fntype
== error_mark_node
)
15717 return error_mark_node
;
15719 /* Substitute the exception specification. */
15720 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15721 /*defer_ok*/fndecl_type
);
15722 if (specs
== error_mark_node
)
15723 return error_mark_node
;
15725 fntype
= build_exception_variant (fntype
, specs
);
15730 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15731 if (domain
== error_mark_node
)
15732 return error_mark_node
;
15734 /* As an optimization, we avoid regenerating the array type if
15735 it will obviously be the same as T. */
15736 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15739 /* These checks should match the ones in create_array_type_for_decl.
15743 The deduction may fail for any of the following reasons:
15745 -- Attempting to create an array with an element type that
15746 is void, a function type, or a reference type, or [DR337]
15747 an abstract class type. */
15748 if (VOID_TYPE_P (type
)
15749 || TREE_CODE (type
) == FUNCTION_TYPE
15750 || (TREE_CODE (type
) == ARRAY_TYPE
15751 && TYPE_DOMAIN (type
) == NULL_TREE
)
15752 || TYPE_REF_P (type
))
15754 if (complain
& tf_error
)
15755 error ("creating array of %qT", type
);
15756 return error_mark_node
;
15759 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15760 return error_mark_node
;
15762 r
= build_cplus_array_type (type
, domain
);
15764 if (!valid_array_size_p (input_location
, r
, in_decl
,
15765 (complain
& tf_error
)))
15766 return error_mark_node
;
15768 if (TYPE_USER_ALIGN (t
))
15770 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15771 TYPE_USER_ALIGN (r
) = 1;
15777 case TYPENAME_TYPE
:
15779 tree ctx
= TYPE_CONTEXT (t
);
15780 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15782 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15783 if (ctx
== error_mark_node
15784 || TREE_VEC_LENGTH (ctx
) > 1)
15785 return error_mark_node
;
15786 if (TREE_VEC_LENGTH (ctx
) == 0)
15788 if (complain
& tf_error
)
15789 error ("%qD is instantiated for an empty pack",
15790 TYPENAME_TYPE_FULLNAME (t
));
15791 return error_mark_node
;
15793 ctx
= TREE_VEC_ELT (ctx
, 0);
15796 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15797 /*entering_scope=*/1);
15798 if (ctx
== error_mark_node
)
15799 return error_mark_node
;
15801 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15802 complain
, in_decl
);
15803 if (f
== error_mark_node
)
15804 return error_mark_node
;
15806 if (!MAYBE_CLASS_TYPE_P (ctx
))
15808 if (complain
& tf_error
)
15809 error ("%qT is not a class, struct, or union type", ctx
);
15810 return error_mark_node
;
15812 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15814 /* Normally, make_typename_type does not require that the CTX
15815 have complete type in order to allow things like:
15817 template <class T> struct S { typename S<T>::X Y; };
15819 But, such constructs have already been resolved by this
15820 point, so here CTX really should have complete type, unless
15821 it's a partial instantiation. */
15822 ctx
= complete_type (ctx
);
15823 if (!COMPLETE_TYPE_P (ctx
))
15825 if (complain
& tf_error
)
15826 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15827 return error_mark_node
;
15831 f
= make_typename_type (ctx
, f
, typename_type
,
15832 complain
| tf_keep_type_decl
);
15833 if (f
== error_mark_node
)
15835 if (TREE_CODE (f
) == TYPE_DECL
)
15837 complain
|= tf_ignore_bad_quals
;
15841 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15843 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15845 if (complain
& tf_error
)
15846 error ("%qT resolves to %qT, which is not an enumeration type",
15849 return error_mark_node
;
15851 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15853 if (complain
& tf_error
)
15854 error ("%qT resolves to %qT, which is not a class type",
15857 return error_mark_node
;
15861 return cp_build_qualified_type_real
15862 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15865 case UNBOUND_CLASS_TEMPLATE
:
15867 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15868 in_decl
, /*entering_scope=*/1);
15869 tree name
= TYPE_IDENTIFIER (t
);
15870 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15872 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15873 return error_mark_node
;
15876 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15877 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15884 ++cp_unevaluated_operand
;
15885 ++c_inhibit_evaluation_warnings
;
15887 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15889 /*integral_constant_expression_p=*/false);
15891 --cp_unevaluated_operand
;
15892 --c_inhibit_evaluation_warnings
;
15894 type
= finish_typeof (type
);
15895 return cp_build_qualified_type_real (type
,
15897 | cp_type_quals (type
),
15901 case DECLTYPE_TYPE
:
15905 ++cp_unevaluated_operand
;
15906 ++c_inhibit_evaluation_warnings
;
15908 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15909 complain
|tf_decltype
, in_decl
,
15910 /*function_p*/false,
15911 /*integral_constant_expression*/false);
15913 --cp_unevaluated_operand
;
15914 --c_inhibit_evaluation_warnings
;
15916 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15917 type
= lambda_capture_field_type (type
,
15918 false /*explicit_init*/,
15919 DECLTYPE_FOR_REF_CAPTURE (t
));
15920 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15921 type
= lambda_proxy_type (type
);
15924 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15925 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15927 /* In a template ~id could be either a complement expression
15928 or an unqualified-id naming a destructor; if instantiating
15929 it produces an expression, it's not an id-expression or
15932 type
= finish_decltype_type (type
, id
, complain
);
15934 return cp_build_qualified_type_real (type
,
15936 | cp_type_quals (type
),
15937 complain
| tf_ignore_bad_quals
);
15940 case UNDERLYING_TYPE
:
15942 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15943 complain
, in_decl
);
15944 return finish_underlying_type (type
);
15947 case TYPE_ARGUMENT_PACK
:
15948 case NONTYPE_ARGUMENT_PACK
:
15952 if (code
== NONTYPE_ARGUMENT_PACK
)
15953 r
= make_node (code
);
15955 r
= cxx_make_type (code
);
15957 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15958 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15959 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15977 /* We should use one of the expression tsubsts for these codes. */
15978 gcc_unreachable ();
15981 sorry ("use of %qs in template", get_tree_code_name (code
));
15982 return error_mark_node
;
15986 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15987 expression on the left-hand side of the "." or "->" operator. We
15988 only do the lookup if we had a dependent BASELINK. Otherwise we
15989 adjust it onto the instantiated heirarchy. */
15992 tsubst_baselink (tree baselink
, tree object_type
,
15993 tree args
, tsubst_flags_t complain
, tree in_decl
)
15995 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15996 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15997 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15999 tree optype
= BASELINK_OPTYPE (baselink
);
16000 optype
= tsubst (optype
, args
, complain
, in_decl
);
16002 tree template_args
= NULL_TREE
;
16003 bool template_id_p
= false;
16004 tree fns
= BASELINK_FUNCTIONS (baselink
);
16005 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16007 template_id_p
= true;
16008 template_args
= TREE_OPERAND (fns
, 1);
16009 fns
= TREE_OPERAND (fns
, 0);
16011 template_args
= tsubst_template_args (template_args
, args
,
16012 complain
, in_decl
);
16015 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16016 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16017 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16021 tree name
= OVL_NAME (fns
);
16022 if (IDENTIFIER_CONV_OP_P (name
))
16023 name
= make_conv_op_name (optype
);
16025 if (name
== complete_dtor_identifier
)
16026 /* Treat as-if non-dependent below. */
16027 dependent_p
= false;
16029 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16033 if ((complain
& tf_error
)
16034 && constructor_name_p (name
, qualifying_scope
))
16035 error ("cannot call constructor %<%T::%D%> directly",
16036 qualifying_scope
, name
);
16037 return error_mark_node
;
16040 if (BASELINK_P (baselink
))
16041 fns
= BASELINK_FUNCTIONS (baselink
);
16044 /* We're going to overwrite pieces below, make a duplicate. */
16045 baselink
= copy_node (baselink
);
16047 /* If lookup found a single function, mark it as used at this point.
16048 (If lookup found multiple functions the one selected later by
16049 overload resolution will be marked as used at that point.) */
16050 if (!template_id_p
&& !really_overloaded_fn (fns
))
16052 tree fn
= OVL_FIRST (fns
);
16053 bool ok
= mark_used (fn
, complain
);
16054 if (!ok
&& !(complain
& tf_error
))
16055 return error_mark_node
;
16056 if (ok
&& BASELINK_P (baselink
))
16057 /* We might have instantiated an auto function. */
16058 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16061 if (BASELINK_P (baselink
))
16063 /* Add back the template arguments, if present. */
16065 BASELINK_FUNCTIONS (baselink
)
16066 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16068 /* Update the conversion operator type. */
16069 BASELINK_OPTYPE (baselink
) = optype
;
16073 object_type
= current_class_type
;
16075 if (qualified_p
|| !dependent_p
)
16077 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16081 /* We need to call adjust_result_of_qualified_name_lookup in case the
16082 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16083 so that we still get virtual function binding. */
16084 BASELINK_QUALIFIED_P (baselink
) = false;
16090 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16091 true if the qualified-id will be a postfix-expression in-and-of
16092 itself; false if more of the postfix-expression follows the
16093 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16097 tsubst_qualified_id (tree qualified_id
, tree args
,
16098 tsubst_flags_t complain
, tree in_decl
,
16099 bool done
, bool address_p
)
16105 tree template_args
;
16106 location_t loc
= UNKNOWN_LOCATION
;
16108 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16110 /* Figure out what name to look up. */
16111 name
= TREE_OPERAND (qualified_id
, 1);
16112 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16114 is_template
= true;
16115 loc
= EXPR_LOCATION (name
);
16116 template_args
= TREE_OPERAND (name
, 1);
16118 template_args
= tsubst_template_args (template_args
, args
,
16119 complain
, in_decl
);
16120 if (template_args
== error_mark_node
)
16121 return error_mark_node
;
16122 name
= TREE_OPERAND (name
, 0);
16126 is_template
= false;
16127 template_args
= NULL_TREE
;
16130 /* Substitute into the qualifying scope. When there are no ARGS, we
16131 are just trying to simplify a non-dependent expression. In that
16132 case the qualifying scope may be dependent, and, in any case,
16133 substituting will not help. */
16134 scope
= TREE_OPERAND (qualified_id
, 0);
16137 scope
= tsubst (scope
, args
, complain
, in_decl
);
16138 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16143 if (dependent_scope_p (scope
))
16146 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16147 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16148 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16149 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16153 if (!BASELINK_P (name
) && !DECL_P (expr
))
16155 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16157 /* A BIT_NOT_EXPR is used to represent a destructor. */
16158 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16160 error ("qualifying type %qT does not match destructor name ~%qT",
16161 scope
, TREE_OPERAND (expr
, 0));
16162 expr
= error_mark_node
;
16165 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16166 /*is_type_p=*/0, false);
16169 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
16170 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16171 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16173 if (complain
& tf_error
)
16175 error ("dependent-name %qE is parsed as a non-type, but "
16176 "instantiation yields a type", qualified_id
);
16177 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16179 return error_mark_node
;
16185 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16187 return error_mark_node
;
16188 /* Remember that there was a reference to this entity. */
16189 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16190 return error_mark_node
;
16193 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16195 if (complain
& tf_error
)
16196 qualified_name_lookup_error (scope
,
16197 TREE_OPERAND (qualified_id
, 1),
16198 expr
, input_location
);
16199 return error_mark_node
;
16204 /* We may be repeating a check already done during parsing, but
16205 if it was well-formed and passed then, it will pass again
16206 now, and if it didn't, we wouldn't have got here. The case
16207 we want to catch is when we couldn't tell then, and can now,
16208 namely when templ prior to substitution was an
16210 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16211 return error_mark_node
;
16213 if (variable_template_p (expr
))
16214 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16217 expr
= lookup_template_function (expr
, template_args
);
16220 if (expr
== error_mark_node
&& complain
& tf_error
)
16221 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16222 expr
, input_location
);
16223 else if (TYPE_P (scope
))
16225 expr
= (adjust_result_of_qualified_name_lookup
16226 (expr
, scope
, current_nonlambda_class_type ()));
16227 expr
= (finish_qualified_id_expr
16228 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16229 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16230 /*template_arg_p=*/false, complain
));
16233 /* Expressions do not generally have reference type. */
16234 if (TREE_CODE (expr
) != SCOPE_REF
16235 /* However, if we're about to form a pointer-to-member, we just
16236 want the referenced member referenced. */
16237 && TREE_CODE (expr
) != OFFSET_REF
)
16238 expr
= convert_from_reference (expr
);
16240 if (REF_PARENTHESIZED_P (qualified_id
))
16241 expr
= force_paren_expr (expr
);
16246 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16247 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16251 tsubst_init (tree init
, tree decl
, tree args
,
16252 tsubst_flags_t complain
, tree in_decl
)
16257 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16259 tree type
= TREE_TYPE (decl
);
16261 if (!init
&& type
!= error_mark_node
)
16263 if (tree auto_node
= type_uses_auto (type
))
16265 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16267 if (complain
& tf_error
)
16268 error ("initializer for %q#D expands to an empty list "
16269 "of expressions", decl
);
16270 return error_mark_node
;
16273 else if (!dependent_type_p (type
))
16275 /* If we had an initializer but it
16276 instantiated to nothing,
16277 value-initialize the object. This will
16278 only occur when the initializer was a
16279 pack expansion where the parameter packs
16280 used in that expansion were of length
16282 init
= build_value_init (type
, complain
);
16283 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16284 init
= get_target_expr_sfinae (init
, complain
);
16285 if (TREE_CODE (init
) == TARGET_EXPR
)
16286 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16293 /* Like tsubst, but deals with expressions. This function just replaces
16294 template parms; to finish processing the resultant expression, use
16295 tsubst_copy_and_build or tsubst_expr. */
16298 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16300 enum tree_code code
;
16303 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16306 code
= TREE_CODE (t
);
16311 r
= retrieve_local_specialization (t
);
16313 if (r
== NULL_TREE
)
16315 /* We get here for a use of 'this' in an NSDMI. */
16316 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16317 return current_class_ptr
;
16319 /* This can happen for a parameter name used later in a function
16320 declaration (such as in a late-specified return type). Just
16321 make a dummy decl, since it's only used for its type. */
16322 gcc_assert (cp_unevaluated_operand
!= 0);
16323 r
= tsubst_decl (t
, args
, complain
);
16324 /* Give it the template pattern as its context; its true context
16325 hasn't been instantiated yet and this is good enough for
16327 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16330 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16331 r
= argument_pack_select_arg (r
);
16332 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16333 return error_mark_node
;
16341 if (DECL_TEMPLATE_PARM_P (t
))
16342 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16343 /* There is no need to substitute into namespace-scope
16345 if (DECL_NAMESPACE_SCOPE_P (t
))
16347 /* If ARGS is NULL, then T is known to be non-dependent. */
16348 if (args
== NULL_TREE
)
16349 return scalar_constant_value (t
);
16351 /* Unfortunately, we cannot just call lookup_name here.
16354 template <int I> int f() {
16356 struct S { void g() { E e = a; } };
16359 When we instantiate f<7>::S::g(), say, lookup_name is not
16360 clever enough to find f<7>::a. */
16362 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16363 /*entering_scope=*/0);
16365 for (v
= TYPE_VALUES (enum_type
);
16367 v
= TREE_CHAIN (v
))
16368 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16369 return TREE_VALUE (v
);
16371 /* We didn't find the name. That should never happen; if
16372 name-lookup found it during preliminary parsing, we
16373 should find it again here during instantiation. */
16374 gcc_unreachable ();
16379 if (DECL_CONTEXT (t
))
16383 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16384 /*entering_scope=*/1);
16385 if (ctx
!= DECL_CONTEXT (t
))
16387 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16390 if (complain
& tf_error
)
16391 error ("using invalid field %qD", t
);
16392 return error_mark_node
;
16401 case FUNCTION_DECL
:
16402 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16403 r
= tsubst (t
, args
, complain
, in_decl
);
16404 else if (local_variable_p (t
)
16405 && uses_template_parms (DECL_CONTEXT (t
)))
16407 r
= retrieve_local_specialization (t
);
16408 if (r
== NULL_TREE
)
16410 /* First try name lookup to find the instantiation. */
16411 r
= lookup_name (DECL_NAME (t
));
16416 /* During error-recovery we may find a non-variable,
16417 even an OVERLOAD: just bail out and avoid ICEs and
16418 duplicate diagnostics (c++/62207). */
16419 gcc_assert (seen_error ());
16420 return error_mark_node
;
16422 if (!is_capture_proxy (r
))
16424 /* Make sure the one we found is the one we want. */
16425 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16426 if (ctx
!= DECL_CONTEXT (r
))
16435 /* This can happen for a variable used in a
16436 late-specified return type of a local lambda, or for a
16437 local static or constant. Building a new VAR_DECL
16438 should be OK in all those cases. */
16439 r
= tsubst_decl (t
, args
, complain
);
16440 if (local_specializations
)
16441 /* Avoid infinite recursion (79640). */
16442 register_local_specialization (r
, t
);
16443 if (decl_maybe_constant_var_p (r
))
16445 /* We can't call cp_finish_decl, so handle the
16446 initializer by hand. */
16447 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16448 complain
, in_decl
);
16449 if (!processing_template_decl
)
16450 init
= maybe_constant_init (init
);
16451 if (processing_template_decl
16452 ? potential_constant_expression (init
)
16453 : reduced_constant_expression_p (init
))
16454 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16455 = TREE_CONSTANT (r
) = true;
16456 DECL_INITIAL (r
) = init
;
16457 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16459 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16460 complain
, adc_variable_type
);
16462 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16463 || decl_constant_var_p (r
)
16465 if (!processing_template_decl
16466 && !TREE_STATIC (r
))
16467 r
= process_outer_var_ref (r
, complain
);
16469 /* Remember this for subsequent uses. */
16470 if (local_specializations
)
16471 register_local_specialization (r
, t
);
16473 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16474 r
= argument_pack_select_arg (r
);
16478 if (!mark_used (r
, complain
))
16479 return error_mark_node
;
16482 case NAMESPACE_DECL
:
16489 return tsubst_baselink (t
, current_nonlambda_class_type (),
16490 args
, complain
, in_decl
);
16492 case TEMPLATE_DECL
:
16493 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16494 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16495 args
, complain
, in_decl
);
16496 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16497 return tsubst (t
, args
, complain
, in_decl
);
16498 else if (DECL_CLASS_SCOPE_P (t
)
16499 && uses_template_parms (DECL_CONTEXT (t
)))
16501 /* Template template argument like the following example need
16504 template <template <class> class TT> struct C {};
16505 template <class T> struct D {
16506 template <class U> struct E {};
16511 We are processing the template argument `E' in #1 for
16512 the template instantiation #2. Originally, `E' is a
16513 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16514 have to substitute this with one having context `D<int>'. */
16516 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16517 if (dependent_scope_p (context
))
16519 /* When rewriting a constructor into a deduction guide, a
16520 non-dependent name can become dependent, so memtmpl<args>
16521 becomes context::template memtmpl<args>. */
16522 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16523 return build_qualified_name (type
, context
, DECL_NAME (t
),
16526 return lookup_field (context
, DECL_NAME(t
), 0, false);
16529 /* Ordinary template template argument. */
16532 case NON_LVALUE_EXPR
:
16533 case VIEW_CONVERT_EXPR
:
16535 /* Handle location wrappers by substituting the wrapped node
16536 first, *then* reusing the resulting type. Doing the type
16537 first ensures that we handle template parameters and
16538 parameter pack expansions. */
16539 if (location_wrapper_p (t
))
16541 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16542 complain
, in_decl
);
16543 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16545 tree op
= TREE_OPERAND (t
, 0);
16546 if (code
== VIEW_CONVERT_EXPR
16547 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16549 /* Wrapper to make a C++20 template parameter object const. */
16550 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16551 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16553 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16554 return build1 (code
, type
, op
);
16558 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16559 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16560 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16564 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16565 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16567 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16568 op
= build1 (code
, TREE_TYPE (op
), op
);
16569 REF_PARENTHESIZED_P (op
) = true;
16572 /* We shouldn't see any other uses of these in templates. */
16573 gcc_unreachable ();
16577 case REINTERPRET_CAST_EXPR
:
16578 case CONST_CAST_EXPR
:
16579 case STATIC_CAST_EXPR
:
16580 case DYNAMIC_CAST_EXPR
:
16581 case IMPLICIT_CONV_EXPR
:
16585 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16586 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16587 return build1 (code
, type
, op0
);
16591 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16592 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16594 tree expanded
, op
= TREE_OPERAND (t
, 0);
16597 if (SIZEOF_EXPR_TYPE_P (t
))
16598 op
= TREE_TYPE (op
);
16600 ++cp_unevaluated_operand
;
16601 ++c_inhibit_evaluation_warnings
;
16602 /* We only want to compute the number of arguments. */
16603 if (PACK_EXPANSION_P (op
))
16604 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16606 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16607 args
, complain
, in_decl
);
16608 --cp_unevaluated_operand
;
16609 --c_inhibit_evaluation_warnings
;
16611 if (TREE_CODE (expanded
) == TREE_VEC
)
16613 len
= TREE_VEC_LENGTH (expanded
);
16614 /* Set TREE_USED for the benefit of -Wunused. */
16615 for (int i
= 0; i
< len
; i
++)
16616 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16617 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16620 if (expanded
== error_mark_node
)
16621 return error_mark_node
;
16622 else if (PACK_EXPANSION_P (expanded
)
16623 || (TREE_CODE (expanded
) == TREE_VEC
16624 && pack_expansion_args_count (expanded
)))
16627 if (PACK_EXPANSION_P (expanded
))
16629 else if (TREE_VEC_LENGTH (expanded
) == 1)
16630 expanded
= TREE_VEC_ELT (expanded
, 0);
16632 expanded
= make_argument_pack (expanded
);
16634 if (TYPE_P (expanded
))
16635 return cxx_sizeof_or_alignof_type (input_location
,
16636 expanded
, SIZEOF_EXPR
,
16638 complain
& tf_error
);
16640 return cxx_sizeof_or_alignof_expr (input_location
,
16641 expanded
, SIZEOF_EXPR
,
16642 complain
& tf_error
);
16645 return build_int_cst (size_type_node
, len
);
16647 if (SIZEOF_EXPR_TYPE_P (t
))
16649 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16650 args
, complain
, in_decl
);
16651 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16652 r
= build1 (SIZEOF_EXPR
,
16653 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16654 SIZEOF_EXPR_TYPE_P (r
) = 1;
16661 case TRUTH_NOT_EXPR
:
16664 case UNARY_PLUS_EXPR
: /* Unary + */
16666 case AT_ENCODE_EXPR
:
16670 case REALPART_EXPR
:
16671 case IMAGPART_EXPR
:
16674 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16675 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16676 r
= build1 (code
, type
, op0
);
16677 if (code
== ALIGNOF_EXPR
)
16678 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16682 case COMPONENT_REF
:
16687 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16688 name
= TREE_OPERAND (t
, 1);
16689 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16691 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16692 complain
, in_decl
);
16693 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16695 else if (TREE_CODE (name
) == SCOPE_REF
16696 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16698 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16699 complain
, in_decl
);
16700 name
= TREE_OPERAND (name
, 1);
16701 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16702 complain
, in_decl
);
16703 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16704 name
= build_qualified_name (/*type=*/NULL_TREE
,
16706 /*template_p=*/false);
16708 else if (BASELINK_P (name
))
16709 name
= tsubst_baselink (name
,
16710 non_reference (TREE_TYPE (object
)),
16714 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16715 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16721 case TRUNC_DIV_EXPR
:
16722 case CEIL_DIV_EXPR
:
16723 case FLOOR_DIV_EXPR
:
16724 case ROUND_DIV_EXPR
:
16725 case EXACT_DIV_EXPR
:
16729 case TRUNC_MOD_EXPR
:
16730 case FLOOR_MOD_EXPR
:
16731 case TRUTH_ANDIF_EXPR
:
16732 case TRUTH_ORIF_EXPR
:
16733 case TRUTH_AND_EXPR
:
16734 case TRUTH_OR_EXPR
:
16745 case COMPOUND_EXPR
:
16748 case PREDECREMENT_EXPR
:
16749 case PREINCREMENT_EXPR
:
16750 case POSTDECREMENT_EXPR
:
16751 case POSTINCREMENT_EXPR
:
16753 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16754 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16755 return build_nt (code
, op0
, op1
);
16760 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16761 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16762 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16763 QUALIFIED_NAME_IS_TEMPLATE (t
));
16768 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16769 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16770 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16775 int n
= VL_EXP_OPERAND_LENGTH (t
);
16776 tree result
= build_vl_exp (CALL_EXPR
, n
);
16778 for (i
= 0; i
< n
; i
++)
16779 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16780 complain
, in_decl
);
16786 case PSEUDO_DTOR_EXPR
:
16787 case VEC_PERM_EXPR
:
16789 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16790 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16791 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16792 r
= build_nt (code
, op0
, op1
, op2
);
16793 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16799 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16800 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16801 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16802 r
= build_nt (code
, op0
, op1
, op2
);
16803 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16809 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16810 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16811 r
= build_nt (code
, op0
, op1
);
16812 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16813 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16817 case TEMPLATE_ID_EXPR
:
16819 /* Substituted template arguments */
16820 tree fn
= TREE_OPERAND (t
, 0);
16821 tree targs
= TREE_OPERAND (t
, 1);
16823 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16825 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16827 return lookup_template_function (fn
, targs
);
16832 tree purpose
, value
, chain
;
16834 if (t
== void_list_node
)
16837 purpose
= TREE_PURPOSE (t
);
16839 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16840 value
= TREE_VALUE (t
);
16842 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16843 chain
= TREE_CHAIN (t
);
16844 if (chain
&& chain
!= void_type_node
)
16845 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16846 if (purpose
== TREE_PURPOSE (t
)
16847 && value
== TREE_VALUE (t
)
16848 && chain
== TREE_CHAIN (t
))
16850 return tree_cons (purpose
, value
, chain
);
16855 case ENUMERAL_TYPE
:
16857 case TEMPLATE_TYPE_PARM
:
16858 case TEMPLATE_TEMPLATE_PARM
:
16859 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16860 case TEMPLATE_PARM_INDEX
:
16862 case REFERENCE_TYPE
:
16864 case FUNCTION_TYPE
:
16867 case TYPENAME_TYPE
:
16868 case UNBOUND_CLASS_TEMPLATE
:
16870 case DECLTYPE_TYPE
:
16872 return tsubst (t
, args
, complain
, in_decl
);
16876 /* Fall through. */
16877 case IDENTIFIER_NODE
:
16878 if (IDENTIFIER_CONV_OP_P (t
))
16880 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16881 return make_conv_op_name (new_type
);
16887 /* This is handled by tsubst_copy_and_build. */
16888 gcc_unreachable ();
16892 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16893 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16894 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16897 case CLEANUP_POINT_EXPR
:
16898 /* We shouldn't have built any of these during initial template
16899 generation. Instead, they should be built during instantiation
16900 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16901 gcc_unreachable ();
16905 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16906 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16907 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16908 r
= build2 (code
, type
, op0
, op1
);
16909 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16910 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16911 && !(complain
& tf_error
))
16912 return error_mark_node
;
16916 case EXPR_PACK_EXPANSION
:
16917 error ("invalid use of pack expansion expression");
16918 return error_mark_node
;
16920 case NONTYPE_ARGUMENT_PACK
:
16921 error ("use %<...%> to expand argument pack");
16922 return error_mark_node
;
16925 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16932 /* Instantiate any typedefs in the type. */
16933 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16934 r
= fold_convert (type
, t
);
16935 gcc_assert (TREE_CODE (r
) == code
);
16941 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16943 if (type
!= TREE_TYPE (t
))
16946 TREE_TYPE (r
) = type
;
16952 /* These can sometimes show up in a partial instantiation, but never
16953 involve template parms. */
16954 gcc_assert (!uses_template_parms (t
));
16957 case UNARY_LEFT_FOLD_EXPR
:
16958 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16959 case UNARY_RIGHT_FOLD_EXPR
:
16960 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16961 case BINARY_LEFT_FOLD_EXPR
:
16962 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16963 case BINARY_RIGHT_FOLD_EXPR
:
16964 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16968 case DEBUG_BEGIN_STMT
:
16969 /* ??? There's no point in copying it for now, but maybe some
16970 day it will contain more information, such as a pointer back
16971 to the containing function, inlined copy or so. */
16974 case CO_AWAIT_EXPR
:
16975 return tsubst_expr (t
, args
, complain
, in_decl
,
16976 /*integral_constant_expression_p=*/false);
16980 /* We shouldn't get here, but keep going if !flag_checking. */
16982 gcc_unreachable ();
16987 /* Helper function for tsubst_omp_clauses, used for instantiation of
16988 OMP_CLAUSE_DECL of clauses. */
16991 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16992 tree in_decl
, tree
*iterator_cache
)
16994 if (decl
== NULL_TREE
)
16997 /* Handle OpenMP iterators. */
16998 if (TREE_CODE (decl
) == TREE_LIST
16999 && TREE_PURPOSE (decl
)
17000 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17003 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17004 ret
= iterator_cache
[1];
17008 begin_scope (sk_omp
, NULL
);
17009 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17011 *tp
= copy_node (it
);
17012 TREE_VEC_ELT (*tp
, 0)
17013 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17014 TREE_VEC_ELT (*tp
, 1)
17015 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17016 /*integral_constant_expression_p=*/false);
17017 TREE_VEC_ELT (*tp
, 2)
17018 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17019 /*integral_constant_expression_p=*/false);
17020 TREE_VEC_ELT (*tp
, 3)
17021 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17022 /*integral_constant_expression_p=*/false);
17023 TREE_CHAIN (*tp
) = NULL_TREE
;
17024 tp
= &TREE_CHAIN (*tp
);
17026 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17027 iterator_cache
[0] = TREE_PURPOSE (decl
);
17028 iterator_cache
[1] = ret
;
17030 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17035 /* Handle an OpenMP array section represented as a TREE_LIST (or
17036 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17037 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17038 TREE_LIST. We can handle it exactly the same as an array section
17039 (purpose, value, and a chain), even though the nomenclature
17040 (low_bound, length, etc) is different. */
17041 if (TREE_CODE (decl
) == TREE_LIST
)
17044 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17045 /*integral_constant_expression_p=*/false);
17046 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17047 /*integral_constant_expression_p=*/false);
17048 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17050 if (TREE_PURPOSE (decl
) == low_bound
17051 && TREE_VALUE (decl
) == length
17052 && TREE_CHAIN (decl
) == chain
)
17054 tree ret
= tree_cons (low_bound
, length
, chain
);
17055 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17056 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17059 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17060 /*integral_constant_expression_p=*/false);
17061 /* Undo convert_from_reference tsubst_expr could have called. */
17063 && REFERENCE_REF_P (ret
)
17064 && !REFERENCE_REF_P (decl
))
17065 ret
= TREE_OPERAND (ret
, 0);
17069 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17072 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17073 tree args
, tsubst_flags_t complain
, tree in_decl
)
17075 tree new_clauses
= NULL_TREE
, nc
, oc
;
17076 tree linear_no_step
= NULL_TREE
;
17077 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17079 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17081 nc
= copy_node (oc
);
17082 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17085 switch (OMP_CLAUSE_CODE (nc
))
17087 case OMP_CLAUSE_LASTPRIVATE
:
17088 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17090 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17091 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17092 in_decl
, /*integral_constant_expression_p=*/false);
17093 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17094 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17097 case OMP_CLAUSE_PRIVATE
:
17098 case OMP_CLAUSE_SHARED
:
17099 case OMP_CLAUSE_FIRSTPRIVATE
:
17100 case OMP_CLAUSE_COPYIN
:
17101 case OMP_CLAUSE_COPYPRIVATE
:
17102 case OMP_CLAUSE_UNIFORM
:
17103 case OMP_CLAUSE_DEPEND
:
17104 case OMP_CLAUSE_FROM
:
17105 case OMP_CLAUSE_TO
:
17106 case OMP_CLAUSE_MAP
:
17107 case OMP_CLAUSE_NONTEMPORAL
:
17108 case OMP_CLAUSE_USE_DEVICE_PTR
:
17109 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17110 case OMP_CLAUSE_IS_DEVICE_PTR
:
17111 case OMP_CLAUSE_INCLUSIVE
:
17112 case OMP_CLAUSE_EXCLUSIVE
:
17113 OMP_CLAUSE_DECL (nc
)
17114 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17115 in_decl
, iterator_cache
);
17117 case OMP_CLAUSE_TILE
:
17118 case OMP_CLAUSE_IF
:
17119 case OMP_CLAUSE_NUM_THREADS
:
17120 case OMP_CLAUSE_SCHEDULE
:
17121 case OMP_CLAUSE_COLLAPSE
:
17122 case OMP_CLAUSE_FINAL
:
17123 case OMP_CLAUSE_DEVICE
:
17124 case OMP_CLAUSE_DIST_SCHEDULE
:
17125 case OMP_CLAUSE_NUM_TEAMS
:
17126 case OMP_CLAUSE_THREAD_LIMIT
:
17127 case OMP_CLAUSE_SAFELEN
:
17128 case OMP_CLAUSE_SIMDLEN
:
17129 case OMP_CLAUSE_NUM_TASKS
:
17130 case OMP_CLAUSE_GRAINSIZE
:
17131 case OMP_CLAUSE_PRIORITY
:
17132 case OMP_CLAUSE_ORDERED
:
17133 case OMP_CLAUSE_HINT
:
17134 case OMP_CLAUSE_NUM_GANGS
:
17135 case OMP_CLAUSE_NUM_WORKERS
:
17136 case OMP_CLAUSE_VECTOR_LENGTH
:
17137 case OMP_CLAUSE_WORKER
:
17138 case OMP_CLAUSE_VECTOR
:
17139 case OMP_CLAUSE_ASYNC
:
17140 case OMP_CLAUSE_WAIT
:
17141 OMP_CLAUSE_OPERAND (nc
, 0)
17142 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17143 in_decl
, /*integral_constant_expression_p=*/false);
17145 case OMP_CLAUSE_REDUCTION
:
17146 case OMP_CLAUSE_IN_REDUCTION
:
17147 case OMP_CLAUSE_TASK_REDUCTION
:
17148 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17150 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17151 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17153 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17154 complain
, in_decl
);
17155 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17156 = build_qualified_name (NULL_TREE
, scope
,
17157 TREE_OPERAND (placeholder
, 1),
17161 gcc_assert (identifier_p (placeholder
));
17163 OMP_CLAUSE_DECL (nc
)
17164 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17167 case OMP_CLAUSE_GANG
:
17168 case OMP_CLAUSE_ALIGNED
:
17169 OMP_CLAUSE_DECL (nc
)
17170 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17172 OMP_CLAUSE_OPERAND (nc
, 1)
17173 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17174 in_decl
, /*integral_constant_expression_p=*/false);
17176 case OMP_CLAUSE_LINEAR
:
17177 OMP_CLAUSE_DECL (nc
)
17178 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17180 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17182 gcc_assert (!linear_no_step
);
17183 linear_no_step
= nc
;
17185 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17186 OMP_CLAUSE_LINEAR_STEP (nc
)
17187 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17188 complain
, in_decl
, NULL
);
17190 OMP_CLAUSE_LINEAR_STEP (nc
)
17191 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17193 /*integral_constant_expression_p=*/false);
17195 case OMP_CLAUSE_NOWAIT
:
17196 case OMP_CLAUSE_DEFAULT
:
17197 case OMP_CLAUSE_UNTIED
:
17198 case OMP_CLAUSE_MERGEABLE
:
17199 case OMP_CLAUSE_INBRANCH
:
17200 case OMP_CLAUSE_NOTINBRANCH
:
17201 case OMP_CLAUSE_PROC_BIND
:
17202 case OMP_CLAUSE_FOR
:
17203 case OMP_CLAUSE_PARALLEL
:
17204 case OMP_CLAUSE_SECTIONS
:
17205 case OMP_CLAUSE_TASKGROUP
:
17206 case OMP_CLAUSE_NOGROUP
:
17207 case OMP_CLAUSE_THREADS
:
17208 case OMP_CLAUSE_SIMD
:
17209 case OMP_CLAUSE_DEFAULTMAP
:
17210 case OMP_CLAUSE_ORDER
:
17211 case OMP_CLAUSE_BIND
:
17212 case OMP_CLAUSE_INDEPENDENT
:
17213 case OMP_CLAUSE_AUTO
:
17214 case OMP_CLAUSE_SEQ
:
17215 case OMP_CLAUSE_IF_PRESENT
:
17216 case OMP_CLAUSE_FINALIZE
:
17219 gcc_unreachable ();
17221 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17222 switch (OMP_CLAUSE_CODE (nc
))
17224 case OMP_CLAUSE_SHARED
:
17225 case OMP_CLAUSE_PRIVATE
:
17226 case OMP_CLAUSE_FIRSTPRIVATE
:
17227 case OMP_CLAUSE_LASTPRIVATE
:
17228 case OMP_CLAUSE_COPYPRIVATE
:
17229 case OMP_CLAUSE_LINEAR
:
17230 case OMP_CLAUSE_REDUCTION
:
17231 case OMP_CLAUSE_IN_REDUCTION
:
17232 case OMP_CLAUSE_TASK_REDUCTION
:
17233 case OMP_CLAUSE_USE_DEVICE_PTR
:
17234 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17235 case OMP_CLAUSE_IS_DEVICE_PTR
:
17236 case OMP_CLAUSE_INCLUSIVE
:
17237 case OMP_CLAUSE_EXCLUSIVE
:
17238 /* tsubst_expr on SCOPE_REF results in returning
17239 finish_non_static_data_member result. Undo that here. */
17240 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17241 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17242 == IDENTIFIER_NODE
))
17244 tree t
= OMP_CLAUSE_DECL (nc
);
17247 switch (TREE_CODE (v
))
17249 case COMPONENT_REF
:
17253 case POINTER_PLUS_EXPR
:
17254 v
= TREE_OPERAND (v
, 0);
17257 if (DECL_CONTEXT (v
) == current_function_decl
17258 && DECL_ARTIFICIAL (v
)
17259 && DECL_NAME (v
) == this_identifier
)
17260 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17267 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17268 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17269 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17270 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17271 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17273 tree decl
= OMP_CLAUSE_DECL (nc
);
17276 retrofit_lang_decl (decl
);
17277 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17286 new_clauses
= nreverse (new_clauses
);
17287 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17289 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17290 if (linear_no_step
)
17291 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17292 if (nc
== linear_no_step
)
17294 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17298 return new_clauses
;
17301 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17304 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17307 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17309 tree purpose
, value
, chain
;
17314 if (TREE_CODE (t
) != TREE_LIST
)
17315 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17316 /*function_p=*/false,
17317 /*integral_constant_expression_p=*/false);
17319 if (t
== void_list_node
)
17322 purpose
= TREE_PURPOSE (t
);
17324 purpose
= RECUR (purpose
);
17325 value
= TREE_VALUE (t
);
17328 if (TREE_CODE (value
) != LABEL_DECL
)
17329 value
= RECUR (value
);
17332 value
= lookup_label (DECL_NAME (value
));
17333 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17334 TREE_USED (value
) = 1;
17337 chain
= TREE_CHAIN (t
);
17338 if (chain
&& chain
!= void_type_node
)
17339 chain
= RECUR (chain
);
17340 return tree_cons (purpose
, value
, chain
);
17344 /* Used to temporarily communicate the list of #pragma omp parallel
17345 clauses to #pragma omp for instantiation if they are combined
17348 static tree
*omp_parallel_combined_clauses
;
17350 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17351 tree
*, unsigned int *);
17353 /* Substitute one OMP_FOR iterator. */
17356 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17357 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17358 tree args
, tsubst_flags_t complain
, tree in_decl
,
17359 bool integral_constant_expression_p
)
17361 #define RECUR(NODE) \
17362 tsubst_expr ((NODE), args, complain, in_decl, \
17363 integral_constant_expression_p)
17364 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17367 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17368 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17370 decl
= TREE_OPERAND (init
, 0);
17371 init
= TREE_OPERAND (init
, 1);
17372 tree decl_expr
= NULL_TREE
;
17373 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17376 bool decomp
= false;
17377 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17379 tree v
= DECL_VALUE_EXPR (decl
);
17380 if (TREE_CODE (v
) == ARRAY_REF
17381 && VAR_P (TREE_OPERAND (v
, 0))
17382 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17384 tree decomp_first
= NULL_TREE
;
17385 unsigned decomp_cnt
= 0;
17386 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17387 maybe_push_decl (d
);
17388 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17389 in_decl
, &decomp_first
, &decomp_cnt
);
17391 if (d
== error_mark_node
)
17392 decl
= error_mark_node
;
17394 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17396 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17398 tree v
= build_nt (ARRAY_REF
, d
,
17399 size_int (decomp_cnt
- i
- 1),
17400 NULL_TREE
, NULL_TREE
);
17401 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17402 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17404 fit_decomposition_lang_decl (decomp_first
, d
);
17405 decomp_first
= DECL_CHAIN (decomp_first
);
17409 decl
= tsubst_decl (decl
, args
, complain
);
17411 maybe_push_decl (decl
);
17413 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17415 /* We need to jump through some hoops to handle declarations in the
17416 init-statement, since we might need to handle auto deduction,
17417 but we need to keep control of initialization. */
17419 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17420 decl
= tsubst_decl (decl
, args
, complain
);
17424 if (TREE_CODE (decl
) == SCOPE_REF
)
17426 decl
= RECUR (decl
);
17427 if (TREE_CODE (decl
) == COMPONENT_REF
)
17431 switch (TREE_CODE (v
))
17433 case COMPONENT_REF
:
17437 case POINTER_PLUS_EXPR
:
17438 v
= TREE_OPERAND (v
, 0);
17441 if (DECL_CONTEXT (v
) == current_function_decl
17442 && DECL_ARTIFICIAL (v
)
17443 && DECL_NAME (v
) == this_identifier
)
17445 decl
= TREE_OPERAND (decl
, 1);
17446 decl
= omp_privatize_field (decl
, false);
17456 decl
= RECUR (decl
);
17458 init
= RECUR (init
);
17460 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17462 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17463 if (TREE_CODE (o
) == TREE_LIST
)
17464 TREE_VEC_ELT (orig_declv
, i
)
17465 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17466 RECUR (TREE_VALUE (o
)),
17469 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17474 tree this_pre_body
= NULL_TREE
;
17475 tree orig_init
= NULL_TREE
;
17476 tree orig_decl
= NULL_TREE
;
17477 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17478 orig_init
, cond
, incr
);
17481 if (orig_declv
== NULL_TREE
)
17482 orig_declv
= copy_node (declv
);
17483 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17486 else if (orig_declv
)
17487 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17490 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17491 if (!range_for
&& auto_node
&& init
)
17493 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17495 gcc_assert (!type_dependent_expression_p (decl
));
17497 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17501 /* Declare the variable, but don't let that initialize it. */
17502 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17503 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17505 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17510 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
17511 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17512 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17514 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17515 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17516 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17517 NOP_EXPR
, rhs
, complain
);
17520 incr
= RECUR (incr
);
17521 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17522 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17524 TREE_VEC_ELT (declv
, i
) = decl
;
17525 TREE_VEC_ELT (initv
, i
) = init
;
17526 TREE_VEC_ELT (condv
, i
) = cond
;
17527 TREE_VEC_ELT (incrv
, i
) = incr
;
17533 /* Declare and initialize the variable. */
17541 for (j
= ((omp_parallel_combined_clauses
== NULL
17542 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17544 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17546 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17547 && OMP_CLAUSE_DECL (*pc
) == decl
)
17549 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17550 && OMP_CLAUSE_DECL (*pc
) == decl
)
17554 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17556 *pc
= OMP_CLAUSE_CHAIN (c
);
17557 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17560 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17561 && OMP_CLAUSE_DECL (*pc
) == decl
)
17563 error ("iteration variable %qD should not be firstprivate",
17565 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17567 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17568 && OMP_CLAUSE_DECL (*pc
) == decl
)
17570 error ("iteration variable %qD should not be reduction",
17572 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17575 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17580 if (*pc
== NULL_TREE
)
17582 tree c
= build_omp_clause (input_location
,
17583 TREE_CODE (t
) == OMP_LOOP
17584 ? OMP_CLAUSE_LASTPRIVATE
17585 : OMP_CLAUSE_PRIVATE
);
17586 OMP_CLAUSE_DECL (c
) = decl
;
17587 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17590 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17595 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17596 if (COMPARISON_CLASS_P (cond
))
17598 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17599 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17600 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17603 cond
= RECUR (cond
);
17604 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17605 switch (TREE_CODE (incr
))
17607 case PREINCREMENT_EXPR
:
17608 case PREDECREMENT_EXPR
:
17609 case POSTINCREMENT_EXPR
:
17610 case POSTDECREMENT_EXPR
:
17611 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17612 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17615 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17616 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17618 tree rhs
= TREE_OPERAND (incr
, 1);
17619 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17620 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17621 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17622 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17623 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17627 incr
= RECUR (incr
);
17630 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17631 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17633 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17634 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17635 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17636 TREE_TYPE (decl
), lhs
,
17637 RECUR (TREE_OPERAND (incr
, 2))));
17639 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17640 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17641 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17643 tree rhs
= TREE_OPERAND (incr
, 2);
17644 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17645 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17646 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17647 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17648 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17652 incr
= RECUR (incr
);
17655 incr
= RECUR (incr
);
17659 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17660 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17661 TREE_VEC_ELT (declv
, i
) = decl
;
17662 TREE_VEC_ELT (initv
, i
) = init
;
17663 TREE_VEC_ELT (condv
, i
) = cond
;
17664 TREE_VEC_ELT (incrv
, i
) = incr
;
17669 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17670 of OMP_TARGET's body. */
17673 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17675 *walk_subtrees
= 0;
17676 switch (TREE_CODE (*tp
))
17681 case STATEMENT_LIST
:
17682 *walk_subtrees
= 1;
17690 /* Helper function for tsubst_expr. For decomposition declaration
17691 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17692 also the corresponding decls representing the identifiers
17693 of the decomposition declaration. Return DECL if successful
17694 or error_mark_node otherwise, set *FIRST to the first decl
17695 in the list chained through DECL_CHAIN and *CNT to the number
17699 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17700 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17703 tree decl2
, decl3
, prev
= decl
;
17705 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17706 for (decl2
= DECL_CHAIN (pattern_decl
);
17709 && DECL_DECOMPOSITION_P (decl2
)
17710 && DECL_NAME (decl2
);
17711 decl2
= DECL_CHAIN (decl2
))
17713 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17715 gcc_assert (errorcount
);
17716 return error_mark_node
;
17719 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17720 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17721 tree v
= DECL_VALUE_EXPR (decl2
);
17722 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17723 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17724 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17725 SET_DECL_VALUE_EXPR (decl2
, v
);
17726 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17728 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17731 gcc_assert (errorcount
);
17732 decl
= error_mark_node
;
17735 maybe_push_decl (decl3
);
17736 if (error_operand_p (decl3
))
17737 decl
= error_mark_node
;
17738 else if (decl
!= error_mark_node
17739 && DECL_CHAIN (decl3
) != prev
17742 gcc_assert (errorcount
);
17743 decl
= error_mark_node
;
17752 /* Return the proper local_specialization for init-capture pack DECL. */
17755 lookup_init_capture_pack (tree decl
)
17757 /* We handle normal pack captures by forwarding to the specialization of the
17758 captured parameter. We can't do that for pack init-captures; we need them
17759 to have their own local_specialization. We created the individual
17760 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17761 when we process the DECL_EXPR for the pack init-capture in the template.
17762 So, how do we find them? We don't know the capture proxy pack when
17763 building the individual resulting proxies, and we don't know the
17764 individual proxies when instantiating the pack. What we have in common is
17767 So...when we instantiate the FIELD_DECL, we stick the result in
17768 local_specializations. Then at the DECL_EXPR we look up that result, see
17769 how many elements it has, synthesize the names, and look them up. */
17771 tree cname
= DECL_NAME (decl
);
17772 tree val
= DECL_VALUE_EXPR (decl
);
17773 tree field
= TREE_OPERAND (val
, 1);
17774 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17775 tree fpack
= retrieve_local_specialization (field
);
17776 if (fpack
== error_mark_node
)
17777 return error_mark_node
;
17780 tree vec
= NULL_TREE
;
17781 tree r
= NULL_TREE
;
17782 if (TREE_CODE (fpack
) == TREE_VEC
)
17784 len
= TREE_VEC_LENGTH (fpack
);
17785 vec
= make_tree_vec (len
);
17786 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17787 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17789 for (int i
= 0; i
< len
; ++i
)
17791 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17792 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17794 TREE_VEC_ELT (vec
, i
) = elt
;
17801 /* Like tsubst_copy for expressions, etc. but also does semantic
17805 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17806 bool integral_constant_expression_p
)
17808 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17809 #define RECUR(NODE) \
17810 tsubst_expr ((NODE), args, complain, in_decl, \
17811 integral_constant_expression_p)
17817 if (t
== NULL_TREE
|| t
== error_mark_node
)
17820 loc
= input_location
;
17821 if (location_t eloc
= cp_expr_location (t
))
17822 input_location
= eloc
;
17823 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17824 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17826 switch (TREE_CODE (t
))
17828 case STATEMENT_LIST
:
17830 tree_stmt_iterator i
;
17831 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17832 RECUR (tsi_stmt (i
));
17836 case CTOR_INITIALIZER
:
17837 finish_mem_initializers (tsubst_initializer_list
17838 (TREE_OPERAND (t
, 0), args
));
17842 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17845 case CO_RETURN_EXPR
:
17846 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
17849 case CO_YIELD_EXPR
:
17850 stmt
= finish_co_yield_expr (input_location
,
17851 RECUR (TREE_OPERAND (t
, 0)));
17855 case CO_AWAIT_EXPR
:
17856 stmt
= finish_co_await_expr (input_location
,
17857 RECUR (TREE_OPERAND (t
, 0)));
17862 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17863 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17864 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17866 finish_expr_stmt (tmp
);
17870 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17875 tree decl
, pattern_decl
;
17878 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17879 if (TREE_CODE (decl
) == LABEL_DECL
)
17880 finish_label_decl (DECL_NAME (decl
));
17881 else if (TREE_CODE (decl
) == USING_DECL
)
17883 tree scope
= USING_DECL_SCOPE (decl
);
17884 tree name
= DECL_NAME (decl
);
17886 scope
= tsubst (scope
, args
, complain
, in_decl
);
17887 finish_nonmember_using_decl (scope
, name
);
17889 else if (is_capture_proxy (decl
)
17890 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17892 /* We're in tsubst_lambda_expr, we've already inserted a new
17893 capture proxy, so look it up and register it. */
17895 if (!DECL_PACK_P (decl
))
17897 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17898 /*nonclass*/1, /*block_p=*/true,
17899 /*ns_only*/0, LOOKUP_HIDDEN
);
17900 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17902 else if (is_normal_capture_proxy (decl
))
17904 inst
= (retrieve_local_specialization
17905 (DECL_CAPTURED_VARIABLE (decl
)));
17906 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
17907 || DECL_PACK_P (inst
));
17910 inst
= lookup_init_capture_pack (decl
);
17912 register_local_specialization (inst
, decl
);
17915 else if (DECL_PRETTY_FUNCTION_P (decl
))
17916 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17918 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17919 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17920 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17921 /* Don't copy the old closure; we'll create a new one in
17922 tsubst_lambda_expr. */
17926 init
= DECL_INITIAL (decl
);
17927 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17928 for local variables, so save if DECL was declared constinit. */
17929 const bool constinit_p
17931 && DECL_LANG_SPECIFIC (decl
)
17932 && DECL_TEMPLATE_INFO (decl
)
17933 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17934 decl
= tsubst (decl
, args
, complain
, in_decl
);
17935 if (decl
!= error_mark_node
)
17937 /* By marking the declaration as instantiated, we avoid
17938 trying to instantiate it. Since instantiate_decl can't
17939 handle local variables, and since we've already done
17940 all that needs to be done, that's the right thing to
17943 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17944 if (VAR_P (decl
) && !DECL_NAME (decl
)
17945 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17946 /* Anonymous aggregates are a special case. */
17947 finish_anon_union (decl
);
17948 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17950 DECL_CONTEXT (decl
) = current_function_decl
;
17951 if (DECL_NAME (decl
) == this_identifier
)
17953 tree lam
= DECL_CONTEXT (current_function_decl
);
17954 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17955 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17957 insert_capture_proxy (decl
);
17959 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17960 /* We already did a pushtag. */;
17961 else if (TREE_CODE (decl
) == FUNCTION_DECL
17962 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17963 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17965 DECL_CONTEXT (decl
) = NULL_TREE
;
17967 DECL_CONTEXT (decl
) = current_function_decl
;
17968 cp_check_omp_declare_reduction (decl
);
17972 bool const_init
= false;
17973 unsigned int cnt
= 0;
17974 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17975 maybe_push_decl (decl
);
17978 && DECL_DECOMPOSITION_P (decl
)
17979 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17980 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17981 complain
, in_decl
, &first
,
17984 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17987 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17990 if (ndecl
!= error_mark_node
)
17991 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17993 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17994 constinit_p
? LOOKUP_CONSTINIT
: 0);
17996 if (ndecl
!= error_mark_node
)
17997 cp_finish_decomp (ndecl
, first
, cnt
);
18006 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18007 RECUR (FOR_INIT_STMT (t
));
18008 finish_init_stmt (stmt
);
18009 tmp
= RECUR (FOR_COND (t
));
18010 finish_for_cond (tmp
, stmt
, false, 0);
18011 tmp
= RECUR (FOR_EXPR (t
));
18012 finish_for_expr (tmp
, stmt
);
18014 bool prev
= note_iteration_stmt_body_start ();
18015 RECUR (FOR_BODY (t
));
18016 note_iteration_stmt_body_end (prev
);
18018 finish_for_stmt (stmt
);
18021 case RANGE_FOR_STMT
:
18023 /* Construct another range_for, if this is not a final
18024 substitution (for inside a generic lambda of a
18025 template). Otherwise convert to a regular for. */
18027 stmt
= (processing_template_decl
18028 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18029 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18030 RECUR (RANGE_FOR_INIT_STMT (t
));
18031 decl
= RANGE_FOR_DECL (t
);
18032 decl
= tsubst (decl
, args
, complain
, in_decl
);
18033 maybe_push_decl (decl
);
18034 expr
= RECUR (RANGE_FOR_EXPR (t
));
18036 tree decomp_first
= NULL_TREE
;
18037 unsigned decomp_cnt
= 0;
18038 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18039 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18041 &decomp_first
, &decomp_cnt
);
18043 if (processing_template_decl
)
18045 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18046 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18047 finish_range_for_decl (stmt
, decl
, expr
);
18048 if (decomp_first
&& decl
!= error_mark_node
)
18049 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18053 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18054 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18055 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18056 decomp_first
, decomp_cnt
,
18057 RANGE_FOR_IVDEP (t
), unroll
);
18060 bool prev
= note_iteration_stmt_body_start ();
18061 RECUR (RANGE_FOR_BODY (t
));
18062 note_iteration_stmt_body_end (prev
);
18063 finish_for_stmt (stmt
);
18068 stmt
= begin_while_stmt ();
18069 tmp
= RECUR (WHILE_COND (t
));
18070 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18072 bool prev
= note_iteration_stmt_body_start ();
18073 RECUR (WHILE_BODY (t
));
18074 note_iteration_stmt_body_end (prev
);
18076 finish_while_stmt (stmt
);
18080 stmt
= begin_do_stmt ();
18082 bool prev
= note_iteration_stmt_body_start ();
18083 RECUR (DO_BODY (t
));
18084 note_iteration_stmt_body_end (prev
);
18086 finish_do_body (stmt
);
18087 tmp
= RECUR (DO_COND (t
));
18088 finish_do_stmt (tmp
, stmt
, false, 0);
18092 stmt
= begin_if_stmt ();
18093 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18094 if (IF_STMT_CONSTEXPR_P (t
))
18095 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18096 tmp
= RECUR (IF_COND (t
));
18097 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18098 if (IF_STMT_CONSTEXPR_P (t
)
18099 && instantiation_dependent_expression_p (tmp
))
18101 /* We're partially instantiating a generic lambda, but the condition
18102 of the constexpr if is still dependent. Don't substitute into the
18103 branches now, just remember the template arguments. */
18104 do_poplevel (IF_SCOPE (stmt
));
18105 IF_COND (stmt
) = IF_COND (t
);
18106 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18107 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18108 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18112 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18113 /* Don't instantiate the THEN_CLAUSE. */;
18116 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18117 bool inhibit
= integer_zerop (folded
);
18119 ++c_inhibit_evaluation_warnings
;
18120 RECUR (THEN_CLAUSE (t
));
18122 --c_inhibit_evaluation_warnings
;
18124 finish_then_clause (stmt
);
18126 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18127 /* Don't instantiate the ELSE_CLAUSE. */;
18128 else if (ELSE_CLAUSE (t
))
18130 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18131 bool inhibit
= integer_nonzerop (folded
);
18132 begin_else_clause (stmt
);
18134 ++c_inhibit_evaluation_warnings
;
18135 RECUR (ELSE_CLAUSE (t
));
18137 --c_inhibit_evaluation_warnings
;
18138 finish_else_clause (stmt
);
18141 finish_if_stmt (stmt
);
18145 if (BIND_EXPR_BODY_BLOCK (t
))
18146 stmt
= begin_function_body ();
18148 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18149 ? BCS_TRY_BLOCK
: 0);
18151 RECUR (BIND_EXPR_BODY (t
));
18153 if (BIND_EXPR_BODY_BLOCK (t
))
18154 finish_function_body (stmt
);
18156 finish_compound_stmt (stmt
);
18160 finish_break_stmt ();
18163 case CONTINUE_STMT
:
18164 finish_continue_stmt ();
18168 stmt
= begin_switch_stmt ();
18169 tmp
= RECUR (SWITCH_STMT_COND (t
));
18170 finish_switch_cond (tmp
, stmt
);
18171 RECUR (SWITCH_STMT_BODY (t
));
18172 finish_switch_stmt (stmt
);
18175 case CASE_LABEL_EXPR
:
18177 tree decl
= CASE_LABEL (t
);
18178 tree low
= RECUR (CASE_LOW (t
));
18179 tree high
= RECUR (CASE_HIGH (t
));
18180 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18181 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18183 tree label
= CASE_LABEL (l
);
18184 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18185 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18186 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18193 tree decl
= LABEL_EXPR_LABEL (t
);
18196 label
= finish_label_stmt (DECL_NAME (decl
));
18197 if (TREE_CODE (label
) == LABEL_DECL
)
18198 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18199 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18200 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18205 tmp
= GOTO_DESTINATION (t
);
18206 if (TREE_CODE (tmp
) != LABEL_DECL
)
18207 /* Computed goto's must be tsubst'd into. On the other hand,
18208 non-computed gotos must not be; the identifier in question
18209 will have no binding. */
18212 tmp
= DECL_NAME (tmp
);
18213 finish_goto_stmt (tmp
);
18218 tree string
= RECUR (ASM_STRING (t
));
18219 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18220 complain
, in_decl
);
18221 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18222 complain
, in_decl
);
18223 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18224 complain
, in_decl
);
18225 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18226 complain
, in_decl
);
18227 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18228 outputs
, inputs
, clobbers
, labels
,
18230 tree asm_expr
= tmp
;
18231 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18232 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18233 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18240 stmt
= begin_try_block ();
18241 RECUR (TRY_STMTS (t
));
18242 finish_cleanup_try_block (stmt
);
18243 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18247 tree compound_stmt
= NULL_TREE
;
18249 if (FN_TRY_BLOCK_P (t
))
18250 stmt
= begin_function_try_block (&compound_stmt
);
18252 stmt
= begin_try_block ();
18254 RECUR (TRY_STMTS (t
));
18256 if (FN_TRY_BLOCK_P (t
))
18257 finish_function_try_block (stmt
);
18259 finish_try_block (stmt
);
18261 RECUR (TRY_HANDLERS (t
));
18262 if (FN_TRY_BLOCK_P (t
))
18263 finish_function_handler_sequence (stmt
, compound_stmt
);
18265 finish_handler_sequence (stmt
);
18271 tree decl
= HANDLER_PARMS (t
);
18275 decl
= tsubst (decl
, args
, complain
, in_decl
);
18276 /* Prevent instantiate_decl from trying to instantiate
18277 this variable. We've already done all that needs to be
18279 if (decl
!= error_mark_node
)
18280 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18282 stmt
= begin_handler ();
18283 finish_handler_parms (decl
, stmt
);
18284 RECUR (HANDLER_BODY (t
));
18285 finish_handler (stmt
);
18290 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18291 if (CLASS_TYPE_P (tmp
))
18293 /* Local classes are not independent templates; they are
18294 instantiated along with their containing function. And this
18295 way we don't have to deal with pushing out of one local class
18296 to instantiate a member of another local class. */
18297 /* Closures are handled by the LAMBDA_EXPR. */
18298 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18299 complete_type (tmp
);
18300 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18302 || (TREE_CODE (fld
) == FUNCTION_DECL
18303 && !DECL_ARTIFICIAL (fld
)))
18304 && DECL_TEMPLATE_INSTANTIATION (fld
))
18305 instantiate_decl (fld
, /*defer_ok=*/false,
18306 /*expl_inst_class=*/false);
18310 case STATIC_ASSERT
:
18314 ++c_inhibit_evaluation_warnings
;
18316 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18319 /*integral_constant_expression_p=*/true);
18320 --c_inhibit_evaluation_warnings
;
18322 finish_static_assert (condition
,
18323 STATIC_ASSERT_MESSAGE (t
),
18324 STATIC_ASSERT_SOURCE_LOCATION (t
),
18325 /*member_p=*/false);
18330 case OACC_PARALLEL
:
18332 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18334 stmt
= begin_omp_parallel ();
18335 RECUR (OMP_BODY (t
));
18336 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18340 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18341 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18342 complain
, in_decl
);
18343 if (OMP_PARALLEL_COMBINED (t
))
18344 omp_parallel_combined_clauses
= &tmp
;
18345 stmt
= begin_omp_parallel ();
18346 RECUR (OMP_PARALLEL_BODY (t
));
18347 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18348 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18349 = OMP_PARALLEL_COMBINED (t
);
18350 pop_omp_privatization_clauses (r
);
18354 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18356 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18357 complain
, in_decl
);
18359 OMP_TASK_CLAUSES (t
) = tmp
;
18363 r
= push_omp_privatization_clauses (false);
18364 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18365 complain
, in_decl
);
18366 stmt
= begin_omp_task ();
18367 RECUR (OMP_TASK_BODY (t
));
18368 finish_omp_task (tmp
, stmt
);
18369 pop_omp_privatization_clauses (r
);
18375 case OMP_DISTRIBUTE
:
18379 tree clauses
, body
, pre_body
;
18380 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18381 tree orig_declv
= NULL_TREE
;
18382 tree incrv
= NULL_TREE
;
18383 enum c_omp_region_type ort
= C_ORT_OMP
;
18384 bool any_range_for
= false;
18387 if (TREE_CODE (t
) == OACC_LOOP
)
18390 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18391 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18393 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18395 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18396 if (OMP_FOR_ORIG_DECLS (t
))
18397 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18398 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18399 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18400 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18403 keep_next_level (true);
18404 stmt
= begin_omp_structured_block ();
18406 pre_body
= push_stmt_list ();
18407 RECUR (OMP_FOR_PRE_BODY (t
));
18408 pre_body
= pop_stmt_list (pre_body
);
18410 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18411 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18413 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18414 condv
, incrv
, &clauses
, args
,
18416 integral_constant_expression_p
);
18417 omp_parallel_combined_clauses
= NULL
;
18421 gcc_assert (orig_declv
);
18422 body
= begin_omp_structured_block ();
18423 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18424 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18425 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18426 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18427 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18428 TREE_VEC_ELT (declv
, i
));
18431 body
= push_stmt_list ();
18432 RECUR (OMP_FOR_BODY (t
));
18434 body
= finish_omp_structured_block (body
);
18436 body
= pop_stmt_list (body
);
18438 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18439 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18440 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18444 t
= make_node (TREE_CODE (t
));
18445 TREE_TYPE (t
) = void_type_node
;
18446 OMP_FOR_BODY (t
) = body
;
18447 OMP_FOR_PRE_BODY (t
) = pre_body
;
18448 OMP_FOR_CLAUSES (t
) = clauses
;
18449 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18453 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18455 pop_omp_privatization_clauses (r
);
18460 omp_parallel_combined_clauses
= NULL
;
18465 case OMP_TASKGROUP
:
18467 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18468 && OMP_TEAMS_COMBINED (t
));
18469 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18471 if (TREE_CODE (t
) == OMP_TEAMS
)
18473 keep_next_level (true);
18474 stmt
= begin_omp_structured_block ();
18475 RECUR (OMP_BODY (t
));
18476 stmt
= finish_omp_structured_block (stmt
);
18480 stmt
= push_stmt_list ();
18481 RECUR (OMP_BODY (t
));
18482 stmt
= pop_stmt_list (stmt
);
18486 OMP_BODY (t
) = stmt
;
18487 OMP_CLAUSES (t
) = tmp
;
18489 pop_omp_privatization_clauses (r
);
18493 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18494 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18496 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18497 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18499 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18500 args
, complain
, in_decl
);
18501 if (tmp
== NULL_TREE
)
18502 tmp
= error_mark_node
;
18506 kind
= (enum omp_clause_depend_kind
)
18507 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18510 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18513 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18514 OMP_CLAUSE_DEPEND_SOURCE
,
18515 OMP_DEPOBJ_CLAUSES (t
));
18519 case OMP_TARGET_DATA
:
18521 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18522 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18524 keep_next_level (true);
18525 stmt
= begin_omp_structured_block ();
18527 RECUR (OMP_BODY (t
));
18528 stmt
= finish_omp_structured_block (stmt
);
18531 OMP_BODY (t
) = stmt
;
18532 OMP_CLAUSES (t
) = tmp
;
18533 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18535 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18538 /* For combined target teams, ensure the num_teams and
18539 thread_limit clause expressions are evaluated on the host,
18540 before entering the target construct. */
18542 for (c
= OMP_TEAMS_CLAUSES (teams
);
18543 c
; c
= OMP_CLAUSE_CHAIN (c
))
18544 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18545 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18546 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18548 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18549 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18550 if (expr
== error_mark_node
)
18552 tmp
= TARGET_EXPR_SLOT (expr
);
18554 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18555 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18556 OMP_CLAUSE_FIRSTPRIVATE
);
18557 OMP_CLAUSE_DECL (tc
) = tmp
;
18558 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18559 OMP_TARGET_CLAUSES (t
) = tc
;
18568 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18569 complain
, in_decl
);
18570 OACC_DECLARE_CLAUSES (t
) = tmp
;
18574 case OMP_TARGET_UPDATE
:
18575 case OMP_TARGET_ENTER_DATA
:
18576 case OMP_TARGET_EXIT_DATA
:
18577 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18578 complain
, in_decl
);
18580 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18584 case OACC_ENTER_DATA
:
18585 case OACC_EXIT_DATA
:
18587 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18588 complain
, in_decl
);
18590 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18595 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18596 complain
, in_decl
);
18597 stmt
= push_stmt_list ();
18598 RECUR (OMP_BODY (t
));
18599 stmt
= pop_stmt_list (stmt
);
18602 OMP_BODY (t
) = stmt
;
18603 OMP_ORDERED_CLAUSES (t
) = tmp
;
18608 omp_parallel_combined_clauses
= NULL
;
18611 stmt
= push_stmt_list ();
18612 RECUR (OMP_BODY (t
));
18613 stmt
= pop_stmt_list (stmt
);
18616 OMP_BODY (t
) = stmt
;
18621 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18623 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18624 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18625 complain
, in_decl
);
18626 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18628 tree op1
= TREE_OPERAND (t
, 1);
18629 tree rhs1
= NULL_TREE
;
18631 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18633 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18634 op1
= TREE_OPERAND (op1
, 1);
18636 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18637 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18638 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18639 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18640 OMP_ATOMIC_MEMORY_ORDER (t
));
18644 tree op1
= TREE_OPERAND (t
, 1);
18645 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18646 tree rhs1
= NULL_TREE
;
18647 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18648 enum tree_code opcode
= NOP_EXPR
;
18649 if (code
== OMP_ATOMIC_READ
)
18651 v
= RECUR (TREE_OPERAND (op1
, 0));
18652 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18654 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18655 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18657 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18658 v
= RECUR (TREE_OPERAND (op1
, 0));
18659 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18660 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18662 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18663 op11
= TREE_OPERAND (op11
, 1);
18665 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18666 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18667 opcode
= TREE_CODE (op11
);
18668 if (opcode
== MODIFY_EXPR
)
18674 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18675 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18677 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18678 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18682 case TRANSACTION_EXPR
:
18685 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18686 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18688 if (TRANSACTION_EXPR_IS_STMT (t
))
18690 tree body
= TRANSACTION_EXPR_BODY (t
);
18691 tree noex
= NULL_TREE
;
18692 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18694 noex
= MUST_NOT_THROW_COND (body
);
18695 if (noex
== NULL_TREE
)
18696 noex
= boolean_true_node
;
18697 body
= TREE_OPERAND (body
, 0);
18699 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18701 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18705 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18706 RECUR (TRANSACTION_EXPR_BODY (t
)),
18713 case MUST_NOT_THROW_EXPR
:
18715 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18716 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18717 RETURN (build_must_not_throw_expr (op0
, cond
));
18720 case EXPR_PACK_EXPANSION
:
18721 error ("invalid use of pack expansion expression");
18722 RETURN (error_mark_node
);
18724 case NONTYPE_ARGUMENT_PACK
:
18725 error ("use %<...%> to expand argument pack");
18726 RETURN (error_mark_node
);
18728 case COMPOUND_EXPR
:
18729 tmp
= RECUR (TREE_OPERAND (t
, 0));
18730 if (tmp
== NULL_TREE
)
18731 /* If the first operand was a statement, we're done with it. */
18732 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18733 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18734 RECUR (TREE_OPERAND (t
, 1)),
18737 case ANNOTATE_EXPR
:
18738 tmp
= RECUR (TREE_OPERAND (t
, 0));
18739 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18740 TREE_TYPE (tmp
), tmp
,
18741 RECUR (TREE_OPERAND (t
, 1)),
18742 RECUR (TREE_OPERAND (t
, 2))));
18745 RETURN (add_stmt (copy_node (t
)));
18748 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18750 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18751 /*function_p=*/false,
18752 integral_constant_expression_p
));
18755 RETURN (NULL_TREE
);
18757 input_location
= loc
;
18763 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18764 function. For description of the body see comment above
18765 cp_parser_omp_declare_reduction_exprs. */
18768 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18770 if (t
== NULL_TREE
|| t
== error_mark_node
)
18773 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18775 tree_stmt_iterator tsi
;
18778 memset (stmts
, 0, sizeof stmts
);
18779 for (i
= 0, tsi
= tsi_start (t
);
18780 i
< 7 && !tsi_end_p (tsi
);
18781 i
++, tsi_next (&tsi
))
18782 stmts
[i
] = tsi_stmt (tsi
);
18783 gcc_assert (tsi_end_p (tsi
));
18787 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18788 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18789 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18790 args
, complain
, in_decl
);
18791 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18792 args
, complain
, in_decl
);
18793 DECL_CONTEXT (omp_out
) = current_function_decl
;
18794 DECL_CONTEXT (omp_in
) = current_function_decl
;
18795 keep_next_level (true);
18796 tree block
= begin_omp_structured_block ();
18797 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18798 block
= finish_omp_structured_block (block
);
18799 block
= maybe_cleanup_point_expr_void (block
);
18800 add_decl_expr (omp_out
);
18801 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18802 TREE_NO_WARNING (omp_out
) = 1;
18803 add_decl_expr (omp_in
);
18804 finish_expr_stmt (block
);
18808 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18809 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18810 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18811 args
, complain
, in_decl
);
18812 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18813 args
, complain
, in_decl
);
18814 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18815 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18816 keep_next_level (true);
18817 tree block
= begin_omp_structured_block ();
18818 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18819 block
= finish_omp_structured_block (block
);
18820 block
= maybe_cleanup_point_expr_void (block
);
18821 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18822 add_decl_expr (omp_priv
);
18823 add_decl_expr (omp_orig
);
18824 finish_expr_stmt (block
);
18826 add_decl_expr (omp_orig
);
18830 /* T is a postfix-expression that is not being used in a function
18831 call. Return the substituted version of T. */
18834 tsubst_non_call_postfix_expression (tree t
, tree args
,
18835 tsubst_flags_t complain
,
18838 if (TREE_CODE (t
) == SCOPE_REF
)
18839 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18840 /*done=*/false, /*address_p=*/false);
18842 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18843 /*function_p=*/false,
18844 /*integral_constant_expression_p=*/false);
18849 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18850 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18851 dependent init-capture. */
18854 prepend_one_capture (tree field
, tree init
, tree
&list
,
18855 tsubst_flags_t complain
)
18857 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18859 tree type
= NULL_TREE
;
18862 if (complain
& tf_error
)
18863 error ("empty initializer in lambda init-capture");
18864 init
= error_mark_node
;
18866 else if (TREE_CODE (init
) == TREE_LIST
)
18867 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18869 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18870 TREE_TYPE (field
) = type
;
18871 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18873 list
= tree_cons (field
, init
, list
);
18876 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18877 instantiation context. Instantiating a pack expansion containing a lambda
18878 might result in multiple lambdas all based on the same lambda in the
18882 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18884 tree oldfn
= lambda_function (t
);
18887 tree r
= build_lambda_expr ();
18889 LAMBDA_EXPR_LOCATION (r
)
18890 = LAMBDA_EXPR_LOCATION (t
);
18891 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18892 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18893 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18894 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18896 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18897 /* A lambda in a default argument outside a class gets no
18898 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18899 tsubst_default_argument calls start_lambda_scope, so we need to
18900 specifically ignore it here, and use the global scope. */
18901 record_null_lambda_scope (r
);
18903 record_lambda_scope (r
);
18905 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18906 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18908 vec
<tree
,va_gc
>* field_packs
= NULL
;
18910 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18911 cap
= TREE_CHAIN (cap
))
18913 tree ofield
= TREE_PURPOSE (cap
);
18914 tree init
= TREE_VALUE (cap
);
18915 if (PACK_EXPANSION_P (init
))
18916 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18918 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18919 /*fn*/false, /*constexpr*/false);
18921 if (init
== error_mark_node
)
18922 return error_mark_node
;
18924 if (init
&& TREE_CODE (init
) == TREE_LIST
)
18925 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18927 if (!processing_template_decl
18928 && init
&& TREE_CODE (init
) != TREE_VEC
18929 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
18931 /* For a VLA, simply tsubsting the field type won't work, we need to
18932 go through add_capture again. XXX do we want to do this for all
18934 tree name
= (get_identifier
18935 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
18936 tree ftype
= TREE_TYPE (ofield
);
18937 bool by_ref
= (TYPE_REF_P (ftype
)
18938 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
18939 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
18940 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
18944 if (PACK_EXPANSION_P (ofield
))
18945 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18946 tree field
= tsubst_decl (ofield
, args
, complain
);
18948 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18950 /* Remember these for when we've pushed local_specializations. */
18951 vec_safe_push (field_packs
, ofield
);
18952 vec_safe_push (field_packs
, field
);
18955 if (field
== error_mark_node
)
18956 return error_mark_node
;
18958 if (TREE_CODE (field
) == TREE_VEC
)
18960 int len
= TREE_VEC_LENGTH (field
);
18961 gcc_assert (TREE_CODE (init
) == TREE_VEC
18962 && TREE_VEC_LENGTH (init
) == len
);
18963 for (int i
= 0; i
< len
; ++i
)
18964 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18965 TREE_VEC_ELT (init
, i
),
18966 LAMBDA_EXPR_CAPTURE_LIST (r
),
18971 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18974 if (id_equal (DECL_NAME (field
), "__this"))
18975 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18979 tree type
= begin_lambda_type (r
);
18980 if (type
== error_mark_node
)
18981 return error_mark_node
;
18983 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18984 determine_visibility (TYPE_NAME (type
));
18986 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18988 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18989 ? DECL_TI_TEMPLATE (oldfn
)
18992 tree fntype
= static_fn_type (oldfn
);
18994 ++processing_template_decl
;
18995 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18997 --processing_template_decl
;
18999 if (fntype
== error_mark_node
)
19000 r
= error_mark_node
;
19003 /* The body of a lambda-expression is not a subexpression of the
19004 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19005 which would be skipped if cp_unevaluated_operand. */
19008 /* Fix the type of 'this'. */
19009 fntype
= build_memfn_type (fntype
, type
,
19010 type_memfn_quals (fntype
),
19011 type_memfn_rqual (fntype
));
19015 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19016 if (tmpl
== error_mark_node
)
19018 r
= error_mark_node
;
19021 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19022 finish_member_declaration (tmpl
);
19027 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19028 if (fn
== error_mark_node
)
19030 r
= error_mark_node
;
19033 finish_member_declaration (fn
);
19036 /* Let finish_function set this. */
19037 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19039 bool nested
= cfun
;
19041 push_function_context ();
19043 /* Still increment function_depth so that we don't GC in the
19044 middle of an expression. */
19047 local_specialization_stack
s (lss_copy
);
19049 tree body
= start_lambda_function (fn
, r
);
19051 /* Now record them for lookup_init_capture_pack. */
19052 int fplen
= vec_safe_length (field_packs
);
19053 for (int i
= 0; i
< fplen
; )
19055 tree pack
= (*field_packs
)[i
++];
19056 tree inst
= (*field_packs
)[i
++];
19057 register_local_specialization (inst
, pack
);
19059 release_tree_vector (field_packs
);
19061 register_parameter_specializations (oldfn
, fn
);
19065 /* We might not partially instantiate some parts of the function, so
19066 copy these flags from the original template. */
19067 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19068 current_function_returns_value
= ol
->returns_value
;
19069 current_function_returns_null
= ol
->returns_null
;
19070 current_function_returns_abnormally
= ol
->returns_abnormally
;
19071 current_function_infinite_loop
= ol
->infinite_loop
;
19074 /* [temp.deduct] A lambda-expression appearing in a function type or a
19075 template parameter is not considered part of the immediate context for
19076 the purposes of template argument deduction. */
19077 complain
= tf_warning_or_error
;
19079 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19080 /*constexpr*/false);
19082 finish_lambda_function (body
);
19085 pop_function_context ();
19089 /* The capture list was built up in reverse order; fix that now. */
19090 LAMBDA_EXPR_CAPTURE_LIST (r
)
19091 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19093 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19095 maybe_add_lambda_conv_op (type
);
19099 finish_struct (type
, /*attr*/NULL_TREE
);
19101 insert_pending_capture_proxies ();
19106 /* Like tsubst but deals with expressions and performs semantic
19107 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19110 tsubst_copy_and_build (tree t
,
19112 tsubst_flags_t complain
,
19115 bool integral_constant_expression_p
)
19117 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19118 #define RECUR(NODE) \
19119 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19120 /*function_p=*/false, \
19121 integral_constant_expression_p)
19124 location_t save_loc
;
19126 if (t
== NULL_TREE
|| t
== error_mark_node
)
19129 save_loc
= input_location
;
19130 if (location_t eloc
= cp_expr_location (t
))
19131 input_location
= eloc
;
19133 /* N3276 decltype magic only applies to calls at the top level or on the
19134 right side of a comma. */
19135 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19136 complain
&= ~tf_decltype
;
19138 switch (TREE_CODE (t
))
19142 /* Fall through. */
19143 case IDENTIFIER_NODE
:
19147 bool non_integral_constant_expression_p
;
19148 const char *error_msg
;
19150 if (IDENTIFIER_CONV_OP_P (t
))
19152 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19153 t
= make_conv_op_name (new_type
);
19156 /* Look up the name. */
19157 decl
= lookup_name (t
);
19159 /* By convention, expressions use ERROR_MARK_NODE to indicate
19160 failure, not NULL_TREE. */
19161 if (decl
== NULL_TREE
)
19162 decl
= error_mark_node
;
19164 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19166 integral_constant_expression_p
,
19167 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19168 &non_integral_constant_expression_p
,
19169 /*template_p=*/false,
19171 /*address_p=*/false,
19172 /*template_arg_p=*/false,
19177 if (!function_p
&& identifier_p (decl
))
19179 if (complain
& tf_error
)
19180 unqualified_name_lookup_error (decl
);
19181 decl
= error_mark_node
;
19186 case TEMPLATE_ID_EXPR
:
19189 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19190 tree targs
= TREE_OPERAND (t
, 1);
19193 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19194 if (targs
== error_mark_node
)
19195 RETURN (error_mark_node
);
19197 if (TREE_CODE (templ
) == SCOPE_REF
)
19199 tree name
= TREE_OPERAND (templ
, 1);
19200 tree tid
= lookup_template_function (name
, targs
);
19201 TREE_OPERAND (templ
, 1) = tid
;
19205 if (concept_definition_p (templ
))
19207 tree check
= build_concept_check (templ
, targs
, complain
);
19208 if (check
== error_mark_node
)
19209 RETURN (error_mark_node
);
19211 tree id
= unpack_concept_check (check
);
19213 /* If we built a function concept check, return the underlying
19214 template-id. So we can evaluate it as a function call. */
19215 if (function_concept_p (TREE_OPERAND (id
, 0)))
19221 if (variable_template_p (templ
))
19223 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19225 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19229 if (TREE_CODE (templ
) == COMPONENT_REF
)
19231 object
= TREE_OPERAND (templ
, 0);
19232 templ
= TREE_OPERAND (templ
, 1);
19235 object
= NULL_TREE
;
19236 templ
= lookup_template_function (templ
, targs
);
19239 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19240 object
, templ
, NULL_TREE
));
19242 RETURN (baselink_for_fns (templ
));
19247 tree r
= RECUR (TREE_OPERAND (t
, 0));
19249 if (REFERENCE_REF_P (t
))
19251 /* A type conversion to reference type will be enclosed in
19252 such an indirect ref, but the substitution of the cast
19253 will have also added such an indirect ref. */
19254 r
= convert_from_reference (r
);
19257 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19258 complain
|decltype_flag
);
19260 if (REF_PARENTHESIZED_P (t
))
19261 r
= force_paren_expr (r
);
19268 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19269 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19270 RETURN (build_nop (type
, op0
));
19273 case IMPLICIT_CONV_EXPR
:
19275 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19276 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19277 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19279 retval
= copy_node (t
);
19280 TREE_TYPE (retval
) = type
;
19281 TREE_OPERAND (retval
, 0) = expr
;
19284 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19285 /* We'll pass this to convert_nontype_argument again, we don't need
19286 to actually perform any conversion here. */
19288 int flags
= LOOKUP_IMPLICIT
;
19289 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19290 flags
= LOOKUP_NORMAL
;
19291 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19292 flags
|= LOOKUP_NO_NARROWING
;
19293 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19299 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19300 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19301 if (op0
== error_mark_node
)
19302 RETURN (error_mark_node
);
19303 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19307 case REINTERPRET_CAST_EXPR
:
19308 case CONST_CAST_EXPR
:
19309 case DYNAMIC_CAST_EXPR
:
19310 case STATIC_CAST_EXPR
:
19313 tree op
, r
= NULL_TREE
;
19315 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19316 if (integral_constant_expression_p
19317 && !cast_valid_in_integral_constant_expression_p (type
))
19319 if (complain
& tf_error
)
19320 error ("a cast to a type other than an integral or "
19321 "enumeration type cannot appear in a constant-expression");
19322 RETURN (error_mark_node
);
19325 op
= RECUR (TREE_OPERAND (t
, 0));
19327 warning_sentinel
s(warn_useless_cast
);
19328 warning_sentinel
s2(warn_ignored_qualifiers
);
19329 switch (TREE_CODE (t
))
19332 r
= build_functional_cast (input_location
, type
, op
, complain
);
19334 case REINTERPRET_CAST_EXPR
:
19335 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19337 case CONST_CAST_EXPR
:
19338 r
= build_const_cast (input_location
, type
, op
, complain
);
19340 case DYNAMIC_CAST_EXPR
:
19341 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19343 case STATIC_CAST_EXPR
:
19344 r
= build_static_cast (input_location
, type
, op
, complain
);
19347 gcc_unreachable ();
19353 case POSTDECREMENT_EXPR
:
19354 case POSTINCREMENT_EXPR
:
19355 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19356 args
, complain
, in_decl
);
19357 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19358 complain
|decltype_flag
));
19360 case PREDECREMENT_EXPR
:
19361 case PREINCREMENT_EXPR
:
19365 case TRUTH_NOT_EXPR
:
19366 case UNARY_PLUS_EXPR
: /* Unary + */
19367 case REALPART_EXPR
:
19368 case IMAGPART_EXPR
:
19369 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19370 RECUR (TREE_OPERAND (t
, 0)),
19371 complain
|decltype_flag
));
19373 case FIX_TRUNC_EXPR
:
19374 gcc_unreachable ();
19377 op1
= TREE_OPERAND (t
, 0);
19378 if (TREE_CODE (op1
) == LABEL_DECL
)
19379 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19380 EXPR_LOCATION (op1
)));
19381 if (TREE_CODE (op1
) == SCOPE_REF
)
19382 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19383 /*done=*/true, /*address_p=*/true);
19385 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19387 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19388 complain
|decltype_flag
));
19393 case TRUNC_DIV_EXPR
:
19394 case CEIL_DIV_EXPR
:
19395 case FLOOR_DIV_EXPR
:
19396 case ROUND_DIV_EXPR
:
19397 case EXACT_DIV_EXPR
:
19401 case TRUNC_MOD_EXPR
:
19402 case FLOOR_MOD_EXPR
:
19403 case TRUTH_ANDIF_EXPR
:
19404 case TRUTH_ORIF_EXPR
:
19405 case TRUTH_AND_EXPR
:
19406 case TRUTH_OR_EXPR
:
19417 case SPACESHIP_EXPR
:
19421 /* If T was type-dependent, suppress warnings that depend on the range
19422 of the types involved. */
19423 bool was_dep
= type_dependent_expression_p_push (t
);
19425 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19426 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19428 warning_sentinel
s1(warn_type_limits
, was_dep
);
19429 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19430 warning_sentinel
s3(warn_logical_op
, was_dep
);
19431 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19433 tree r
= build_x_binary_op
19434 (input_location
, TREE_CODE (t
),
19436 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19438 : TREE_CODE (TREE_OPERAND (t
, 0))),
19440 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19442 : TREE_CODE (TREE_OPERAND (t
, 1))),
19444 complain
|decltype_flag
);
19445 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19446 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19451 case POINTER_PLUS_EXPR
:
19453 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19454 if (op0
== error_mark_node
)
19455 RETURN (error_mark_node
);
19456 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19457 if (op1
== error_mark_node
)
19458 RETURN (error_mark_node
);
19459 RETURN (fold_build_pointer_plus (op0
, op1
));
19463 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19464 /*address_p=*/false));
19466 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19467 args
, complain
, in_decl
);
19468 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19469 RECUR (TREE_OPERAND (t
, 1)),
19470 complain
|decltype_flag
));
19473 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19474 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19475 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19482 op1
= TREE_OPERAND (t
, 0);
19483 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19484 op1
= TREE_TYPE (op1
);
19485 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19486 && ALIGNOF_EXPR_STD_P (t
));
19489 /* When there are no ARGS, we are trying to evaluate a
19490 non-dependent expression from the parser. Trying to do
19491 the substitutions may not work. */
19493 op1
= TREE_TYPE (op1
);
19497 ++cp_unevaluated_operand
;
19498 ++c_inhibit_evaluation_warnings
;
19500 op1
= tsubst (op1
, args
, complain
, in_decl
);
19502 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19503 /*function_p=*/false,
19504 /*integral_constant_expression_p=*/
19506 --cp_unevaluated_operand
;
19507 --c_inhibit_evaluation_warnings
;
19510 r
= cxx_sizeof_or_alignof_type (input_location
,
19511 op1
, TREE_CODE (t
), std_alignof
,
19512 complain
& tf_error
);
19514 r
= cxx_sizeof_or_alignof_expr (input_location
,
19515 op1
, TREE_CODE (t
),
19516 complain
& tf_error
);
19517 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19519 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19521 if (!processing_template_decl
&& TYPE_P (op1
))
19523 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19524 build1 (NOP_EXPR
, op1
, error_mark_node
));
19525 SIZEOF_EXPR_TYPE_P (r
) = 1;
19528 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19529 TREE_SIDE_EFFECTS (r
) = 0;
19530 TREE_READONLY (r
) = 1;
19532 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19537 case AT_ENCODE_EXPR
:
19539 op1
= TREE_OPERAND (t
, 0);
19540 ++cp_unevaluated_operand
;
19541 ++c_inhibit_evaluation_warnings
;
19542 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19543 /*function_p=*/false,
19544 /*integral_constant_expression_p=*/false);
19545 --cp_unevaluated_operand
;
19546 --c_inhibit_evaluation_warnings
;
19547 RETURN (objc_build_encode_expr (op1
));
19550 case NOEXCEPT_EXPR
:
19551 op1
= TREE_OPERAND (t
, 0);
19552 ++cp_unevaluated_operand
;
19553 ++c_inhibit_evaluation_warnings
;
19554 ++cp_noexcept_operand
;
19555 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19556 /*function_p=*/false,
19557 /*integral_constant_expression_p=*/false);
19558 --cp_unevaluated_operand
;
19559 --c_inhibit_evaluation_warnings
;
19560 --cp_noexcept_operand
;
19561 RETURN (finish_noexcept_expr (op1
, complain
));
19565 warning_sentinel
s(warn_div_by_zero
);
19566 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19567 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19568 tree r
= build_x_modify_expr
19569 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19570 complain
|decltype_flag
);
19571 /* TREE_NO_WARNING must be set if either the expression was
19572 parenthesized or it uses an operator such as >>= rather
19573 than plain assignment. In the former case, it was already
19574 set and must be copied. In the latter case,
19575 build_x_modify_expr sets it and it must not be reset
19577 if (TREE_NO_WARNING (t
))
19578 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19584 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19585 args
, complain
, in_decl
);
19586 /* Remember that there was a reference to this entity. */
19588 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19589 RETURN (error_mark_node
);
19590 RETURN (build_x_arrow (input_location
, op1
, complain
));
19594 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19595 tree init
= RECUR (TREE_OPERAND (t
, 3));
19596 vec
<tree
, va_gc
> *placement_vec
;
19597 vec
<tree
, va_gc
> *init_vec
;
19599 location_t loc
= EXPR_LOCATION (t
);
19601 if (placement
== NULL_TREE
)
19602 placement_vec
= NULL
;
19605 placement_vec
= make_tree_vector ();
19606 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19607 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19610 /* If there was an initializer in the original tree, but it
19611 instantiated to an empty list, then we should pass a
19612 non-NULL empty vector to tell build_new that it was an
19613 empty initializer() rather than no initializer. This can
19614 only happen when the initializer is a pack expansion whose
19615 parameter packs are of length zero. */
19616 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19620 init_vec
= make_tree_vector ();
19621 if (init
== void_node
)
19622 gcc_assert (init_vec
!= NULL
);
19625 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19626 vec_safe_push (init_vec
, TREE_VALUE (init
));
19630 /* Avoid passing an enclosing decl to valid_array_size_p. */
19631 in_decl
= NULL_TREE
;
19633 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19634 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19635 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19636 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19639 if (placement_vec
!= NULL
)
19640 release_tree_vector (placement_vec
);
19641 if (init_vec
!= NULL
)
19642 release_tree_vector (init_vec
);
19649 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19650 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19651 RETURN (delete_sanity (input_location
, op0
, op1
,
19652 DELETE_EXPR_USE_VEC (t
),
19653 DELETE_EXPR_USE_GLOBAL (t
),
19657 case COMPOUND_EXPR
:
19659 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19660 complain
& ~tf_decltype
, in_decl
,
19661 /*function_p=*/false,
19662 integral_constant_expression_p
);
19663 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19665 RECUR (TREE_OPERAND (t
, 1)),
19666 complain
|decltype_flag
));
19672 unsigned int nargs
, i
;
19677 function
= CALL_EXPR_FN (t
);
19678 /* Internal function with no arguments. */
19679 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19682 /* When we parsed the expression, we determined whether or
19683 not Koenig lookup should be performed. */
19684 koenig_p
= KOENIG_LOOKUP_P (t
);
19685 if (function
== NULL_TREE
)
19688 qualified_p
= false;
19690 else if (TREE_CODE (function
) == SCOPE_REF
)
19692 qualified_p
= true;
19693 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19695 /*address_p=*/false);
19697 else if (koenig_p
&& identifier_p (function
))
19699 /* Do nothing; calling tsubst_copy_and_build on an identifier
19700 would incorrectly perform unqualified lookup again.
19702 Note that we can also have an IDENTIFIER_NODE if the earlier
19703 unqualified lookup found a member function; in that case
19704 koenig_p will be false and we do want to do the lookup
19705 again to find the instantiated member function.
19707 FIXME but doing that causes c++/15272, so we need to stop
19708 using IDENTIFIER_NODE in that situation. */
19709 qualified_p
= false;
19713 if (TREE_CODE (function
) == COMPONENT_REF
)
19715 tree op
= TREE_OPERAND (function
, 1);
19717 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19718 || (BASELINK_P (op
)
19719 && BASELINK_QUALIFIED_P (op
)));
19722 qualified_p
= false;
19724 if (TREE_CODE (function
) == ADDR_EXPR
19725 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19726 /* Avoid error about taking the address of a constructor. */
19727 function
= TREE_OPERAND (function
, 0);
19729 function
= tsubst_copy_and_build (function
, args
, complain
,
19732 integral_constant_expression_p
);
19734 if (BASELINK_P (function
))
19735 qualified_p
= true;
19738 nargs
= call_expr_nargs (t
);
19739 releasing_vec call_args
;
19740 for (i
= 0; i
< nargs
; ++i
)
19742 tree arg
= CALL_EXPR_ARG (t
, i
);
19744 if (!PACK_EXPANSION_P (arg
))
19745 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19748 /* Expand the pack expansion and push each entry onto
19750 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19751 if (TREE_CODE (arg
) == TREE_VEC
)
19753 unsigned int len
, j
;
19755 len
= TREE_VEC_LENGTH (arg
);
19756 for (j
= 0; j
< len
; ++j
)
19758 tree value
= TREE_VEC_ELT (arg
, j
);
19759 if (value
!= NULL_TREE
)
19760 value
= convert_from_reference (value
);
19761 vec_safe_push (call_args
, value
);
19766 /* A partial substitution. Add one entry. */
19767 vec_safe_push (call_args
, arg
);
19772 /* Stripped-down processing for a call in a thunk. Specifically, in
19773 the thunk template for a generic lambda. */
19774 if (CALL_FROM_THUNK_P (t
))
19776 /* Now that we've expanded any packs, the number of call args
19777 might be different. */
19778 unsigned int cargs
= call_args
->length ();
19779 tree thisarg
= NULL_TREE
;
19780 if (TREE_CODE (function
) == COMPONENT_REF
)
19782 thisarg
= TREE_OPERAND (function
, 0);
19783 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19784 thisarg
= TREE_OPERAND (thisarg
, 0);
19785 function
= TREE_OPERAND (function
, 1);
19786 if (TREE_CODE (function
) == BASELINK
)
19787 function
= BASELINK_FUNCTIONS (function
);
19789 /* We aren't going to do normal overload resolution, so force the
19790 template-id to resolve. */
19791 function
= resolve_nondeduced_context (function
, complain
);
19792 for (unsigned i
= 0; i
< cargs
; ++i
)
19794 /* In a thunk, pass through args directly, without any
19796 tree arg
= (*call_args
)[i
];
19797 while (TREE_CODE (arg
) != PARM_DECL
)
19798 arg
= TREE_OPERAND (arg
, 0);
19799 (*call_args
)[i
] = arg
;
19803 /* If there are no other args, just push 'this'. */
19805 vec_safe_push (call_args
, thisarg
);
19808 /* Otherwise, shift the other args over to make room. */
19809 tree last
= (*call_args
)[cargs
- 1];
19810 vec_safe_push (call_args
, last
);
19811 for (int i
= cargs
- 1; i
> 0; --i
)
19812 (*call_args
)[i
] = (*call_args
)[i
- 1];
19813 (*call_args
)[0] = thisarg
;
19816 ret
= build_call_a (function
, call_args
->length (),
19817 call_args
->address ());
19818 /* The thunk location is not interesting. */
19819 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19820 CALL_FROM_THUNK_P (ret
) = true;
19821 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19822 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19827 /* We do not perform argument-dependent lookup if normal
19828 lookup finds a non-function, in accordance with the
19829 expected resolution of DR 218. */
19831 && ((is_overloaded_fn (function
)
19832 /* If lookup found a member function, the Koenig lookup is
19833 not appropriate, even if an unqualified-name was used
19834 to denote the function. */
19835 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19836 || identifier_p (function
))
19837 /* Only do this when substitution turns a dependent call
19838 into a non-dependent call. */
19839 && type_dependent_expression_p_push (t
)
19840 && !any_type_dependent_arguments_p (call_args
))
19841 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19843 if (function
!= NULL_TREE
19844 && identifier_p (function
)
19845 && !any_type_dependent_arguments_p (call_args
))
19847 if (koenig_p
&& (complain
& tf_warning_or_error
))
19849 /* For backwards compatibility and good diagnostics, try
19850 the unqualified lookup again if we aren't in SFINAE
19852 tree unq
= (tsubst_copy_and_build
19853 (function
, args
, complain
, in_decl
, true,
19854 integral_constant_expression_p
));
19855 if (unq
== error_mark_node
)
19856 RETURN (error_mark_node
);
19858 if (unq
!= function
)
19860 /* In a lambda fn, we have to be careful to not
19861 introduce new this captures. Legacy code can't
19862 be using lambdas anyway, so it's ok to be
19864 bool in_lambda
= (current_class_type
19865 && LAMBDA_TYPE_P (current_class_type
));
19866 char const *const msg
19867 = G_("%qD was not declared in this scope, "
19868 "and no declarations were found by "
19869 "argument-dependent lookup at the point "
19870 "of instantiation");
19874 error_at (cp_expr_loc_or_input_loc (t
),
19877 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19883 if (INDIRECT_REF_P (fn
))
19884 fn
= TREE_OPERAND (fn
, 0);
19885 if (is_overloaded_fn (fn
))
19886 fn
= get_first_fn (fn
);
19889 /* Can't say anything more. */;
19890 else if (DECL_CLASS_SCOPE_P (fn
))
19892 location_t loc
= cp_expr_loc_or_input_loc (t
);
19894 "declarations in dependent base %qT are "
19895 "not found by unqualified lookup",
19896 DECL_CLASS_CONTEXT (fn
));
19897 if (current_class_ptr
)
19899 "use %<this->%D%> instead", function
);
19902 "use %<%T::%D%> instead",
19903 current_class_name
, function
);
19906 inform (DECL_SOURCE_LOCATION (fn
),
19907 "%qD declared here, later in the "
19908 "translation unit", fn
);
19910 RETURN (error_mark_node
);
19916 if (identifier_p (function
))
19918 if (complain
& tf_error
)
19919 unqualified_name_lookup_error (function
);
19920 RETURN (error_mark_node
);
19924 /* Remember that there was a reference to this entity. */
19925 if (function
!= NULL_TREE
19926 && DECL_P (function
)
19927 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19928 RETURN (error_mark_node
);
19930 /* Put back tf_decltype for the actual call. */
19931 complain
|= decltype_flag
;
19933 if (function
== NULL_TREE
)
19934 switch (CALL_EXPR_IFN (t
))
19937 gcc_assert (nargs
== 1);
19938 if (vec_safe_length (call_args
) != 1)
19940 error_at (cp_expr_loc_or_input_loc (t
),
19941 "wrong number of arguments to "
19942 "%<__builtin_launder%>");
19943 ret
= error_mark_node
;
19946 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19947 (*call_args
)[0], complain
);
19950 case IFN_VEC_CONVERT
:
19951 gcc_assert (nargs
== 1);
19952 if (vec_safe_length (call_args
) != 1)
19954 error_at (cp_expr_loc_or_input_loc (t
),
19955 "wrong number of arguments to "
19956 "%<__builtin_convertvector%>");
19957 ret
= error_mark_node
;
19960 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19961 tsubst (TREE_TYPE (t
), args
,
19962 complain
, in_decl
),
19964 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19969 /* Unsupported internal function with arguments. */
19970 gcc_unreachable ();
19972 else if (TREE_CODE (function
) == OFFSET_REF
19973 || TREE_CODE (function
) == DOTSTAR_EXPR
19974 || TREE_CODE (function
) == MEMBER_REF
)
19975 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19977 else if (TREE_CODE (function
) == COMPONENT_REF
)
19979 tree instance
= TREE_OPERAND (function
, 0);
19980 tree fn
= TREE_OPERAND (function
, 1);
19982 if (processing_template_decl
19983 && (type_dependent_expression_p (instance
)
19984 || (!BASELINK_P (fn
)
19985 && TREE_CODE (fn
) != FIELD_DECL
)
19986 || type_dependent_expression_p (fn
)
19987 || any_type_dependent_arguments_p (call_args
)))
19988 ret
= build_min_nt_call_vec (function
, call_args
);
19989 else if (!BASELINK_P (fn
))
19990 ret
= finish_call_expr (function
, &call_args
,
19991 /*disallow_virtual=*/false,
19992 /*koenig_p=*/false,
19995 ret
= (build_new_method_call
19997 &call_args
, NULL_TREE
,
19998 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20002 else if (concept_check_p (function
))
20004 /* FUNCTION is a template-id referring to a concept definition. */
20005 tree id
= unpack_concept_check (function
);
20006 tree tmpl
= TREE_OPERAND (id
, 0);
20007 tree args
= TREE_OPERAND (id
, 1);
20009 /* Calls to standard and variable concepts should have been
20010 previously diagnosed. */
20011 gcc_assert (function_concept_p (tmpl
));
20013 /* Ensure the result is wrapped as a call expression. */
20014 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20017 ret
= finish_call_expr (function
, &call_args
,
20018 /*disallow_virtual=*/qualified_p
,
20022 if (ret
!= error_mark_node
)
20024 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20025 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20026 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20027 if (op
|| ord
|| rev
)
20029 function
= extract_call_expr (ret
);
20030 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20031 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20032 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20041 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20042 cond
= mark_rvalue_use (cond
);
20043 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20046 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20048 if (integer_zerop (folded_cond
))
20050 ++c_inhibit_evaluation_warnings
;
20051 exp1
= RECUR (TREE_OPERAND (t
, 1));
20052 --c_inhibit_evaluation_warnings
;
20053 exp2
= RECUR (TREE_OPERAND (t
, 2));
20057 exp1
= RECUR (TREE_OPERAND (t
, 1));
20058 ++c_inhibit_evaluation_warnings
;
20059 exp2
= RECUR (TREE_OPERAND (t
, 2));
20060 --c_inhibit_evaluation_warnings
;
20062 cond
= folded_cond
;
20066 exp1
= RECUR (TREE_OPERAND (t
, 1));
20067 exp2
= RECUR (TREE_OPERAND (t
, 2));
20070 warning_sentinel
s(warn_duplicated_branches
);
20071 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20072 cond
, exp1
, exp2
, complain
));
20075 case PSEUDO_DTOR_EXPR
:
20077 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20078 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20079 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20080 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20085 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20087 case COMPONENT_REF
:
20094 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20095 args
, complain
, in_decl
);
20096 /* Remember that there was a reference to this entity. */
20097 if (DECL_P (object
)
20098 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20099 RETURN (error_mark_node
);
20100 object_type
= TREE_TYPE (object
);
20102 member
= TREE_OPERAND (t
, 1);
20103 if (BASELINK_P (member
))
20104 member
= tsubst_baselink (member
,
20105 non_reference (TREE_TYPE (object
)),
20106 args
, complain
, in_decl
);
20108 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20109 if (member
== error_mark_node
)
20110 RETURN (error_mark_node
);
20112 if (TREE_CODE (member
) == FIELD_DECL
)
20114 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20115 if (TREE_CODE (r
) == COMPONENT_REF
)
20116 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20119 else if (type_dependent_expression_p (object
))
20120 /* We can't do much here. */;
20121 else if (!CLASS_TYPE_P (object_type
))
20123 if (scalarish_type_p (object_type
))
20125 tree s
= NULL_TREE
;
20126 tree dtor
= member
;
20128 if (TREE_CODE (dtor
) == SCOPE_REF
)
20130 s
= TREE_OPERAND (dtor
, 0);
20131 dtor
= TREE_OPERAND (dtor
, 1);
20133 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20135 dtor
= TREE_OPERAND (dtor
, 0);
20137 RETURN (finish_pseudo_destructor_expr
20138 (object
, s
, dtor
, input_location
));
20142 else if (TREE_CODE (member
) == SCOPE_REF
20143 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20145 /* Lookup the template functions now that we know what the
20147 tree scope
= TREE_OPERAND (member
, 0);
20148 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20149 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20150 member
= lookup_qualified_name (scope
, tmpl
,
20151 /*is_type_p=*/false,
20152 /*complain=*/false);
20153 if (BASELINK_P (member
))
20155 BASELINK_FUNCTIONS (member
)
20156 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20158 member
= (adjust_result_of_qualified_name_lookup
20159 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20164 qualified_name_lookup_error (scope
, tmpl
, member
,
20166 RETURN (error_mark_node
);
20169 else if (TREE_CODE (member
) == SCOPE_REF
20170 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20171 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20173 if (complain
& tf_error
)
20175 if (TYPE_P (TREE_OPERAND (member
, 0)))
20176 error ("%qT is not a class or namespace",
20177 TREE_OPERAND (member
, 0));
20179 error ("%qD is not a class or namespace",
20180 TREE_OPERAND (member
, 0));
20182 RETURN (error_mark_node
);
20185 r
= finish_class_member_access_expr (object
, member
,
20186 /*template_p=*/false,
20188 if (TREE_CODE (r
) == COMPONENT_REF
)
20189 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20194 RETURN (build_throw
20195 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20199 vec
<constructor_elt
, va_gc
> *n
;
20200 constructor_elt
*ce
;
20201 unsigned HOST_WIDE_INT idx
;
20202 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20203 bool process_index_p
;
20205 bool need_copy_p
= false;
20208 if (type
== error_mark_node
)
20209 RETURN (error_mark_node
);
20211 /* We do not want to process the index of aggregate
20212 initializers as they are identifier nodes which will be
20213 looked up by digest_init. */
20214 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20216 if (null_member_pointer_value_p (t
))
20218 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20222 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20223 newlen
= vec_safe_length (n
);
20224 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20226 if (ce
->index
&& process_index_p
20227 /* An identifier index is looked up in the type
20228 being initialized, not the current scope. */
20229 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20230 ce
->index
= RECUR (ce
->index
);
20232 if (PACK_EXPANSION_P (ce
->value
))
20234 /* Substitute into the pack expansion. */
20235 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20238 if (ce
->value
== error_mark_node
20239 || PACK_EXPANSION_P (ce
->value
))
20241 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20242 /* Just move the argument into place. */
20243 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20246 /* Update the length of the final CONSTRUCTOR
20247 arguments vector, and note that we will need to
20249 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20250 need_copy_p
= true;
20254 ce
->value
= RECUR (ce
->value
);
20259 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20261 vec_alloc (n
, newlen
);
20262 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20264 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20266 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20267 for (i
= 0; i
< len
; ++i
)
20268 CONSTRUCTOR_APPEND_ELT (n
, 0,
20269 TREE_VEC_ELT (ce
->value
, i
));
20272 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20276 r
= build_constructor (init_list_type_node
, n
);
20277 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20278 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20279 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20281 if (TREE_HAS_CONSTRUCTOR (t
))
20283 fcl_t cl
= fcl_functional
;
20284 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20286 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20289 TREE_TYPE (r
) = type
;
20295 tree operand_0
= TREE_OPERAND (t
, 0);
20296 if (TYPE_P (operand_0
))
20298 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20299 RETURN (get_typeid (operand_0
, complain
));
20303 operand_0
= RECUR (operand_0
);
20304 RETURN (build_typeid (operand_0
, complain
));
20315 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20316 /* ??? We're doing a subset of finish_id_expression here. */
20317 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20318 /* Replace an evaluated use of the thread_local variable with
20319 a call to its wrapper. */
20321 else if (outer_automatic_var_p (r
))
20322 r
= process_outer_var_ref (r
, complain
);
20324 if (!TYPE_REF_P (TREE_TYPE (t
)))
20325 /* If the original type was a reference, we'll be wrapped in
20326 the appropriate INDIRECT_REF. */
20327 r
= convert_from_reference (r
);
20333 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20334 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20335 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20338 case OFFSETOF_EXPR
:
20341 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20342 in_decl
, /*function_p=*/false,
20343 /*integral_constant_expression_p=*/false);
20344 RETURN (finish_offsetof (object_ptr
,
20345 RECUR (TREE_OPERAND (t
, 0)),
20346 EXPR_LOCATION (t
)));
20349 case ADDRESSOF_EXPR
:
20350 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20351 RECUR (TREE_OPERAND (t
, 0)), complain
));
20355 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20356 complain
, in_decl
);
20357 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20358 complain
, in_decl
);
20359 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20360 TRAIT_EXPR_KIND (t
), type1
, type2
));
20365 tree old_stmt_expr
= cur_stmt_expr
;
20366 tree stmt_expr
= begin_stmt_expr ();
20368 cur_stmt_expr
= stmt_expr
;
20369 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20370 integral_constant_expression_p
);
20371 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20372 cur_stmt_expr
= old_stmt_expr
;
20374 /* If the resulting list of expression statement is empty,
20375 fold it further into void_node. */
20376 if (empty_expr_stmt_p (stmt_expr
))
20377 stmt_expr
= void_node
;
20379 RETURN (stmt_expr
);
20384 if (complain
& tf_partial
)
20386 /* We don't have a full set of template arguments yet; don't touch
20387 the lambda at all. */
20388 gcc_assert (processing_template_decl
);
20391 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20393 RETURN (build_lambda_object (r
));
20397 /* We can get here for a constant initializer of non-dependent type.
20398 FIXME stop folding in cp_parser_initializer_clause. */
20400 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20405 case TRANSACTION_EXPR
:
20406 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20407 integral_constant_expression_p
));
20410 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20412 case VEC_PERM_EXPR
:
20414 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20415 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20416 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20417 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20421 case REQUIRES_EXPR
:
20423 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20428 /* No need to substitute further, a RANGE_EXPR will always be built
20429 with constant operands. */
20432 case NON_LVALUE_EXPR
:
20433 case VIEW_CONVERT_EXPR
:
20434 if (location_wrapper_p (t
))
20435 /* We need to do this here as well as in tsubst_copy so we get the
20436 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20437 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20438 EXPR_LOCATION (t
)));
20442 /* Handle Objective-C++ constructs, if appropriate. */
20445 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20446 in_decl
, /*function_p=*/false);
20450 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20456 input_location
= save_loc
;
20460 /* Verify that the instantiated ARGS are valid. For type arguments,
20461 make sure that the type's linkage is ok. For non-type arguments,
20462 make sure they are constants if they are integral or enumerations.
20463 Emit an error under control of COMPLAIN, and return TRUE on error. */
20466 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20468 if (dependent_template_arg_p (t
))
20470 if (ARGUMENT_PACK_P (t
))
20472 tree vec
= ARGUMENT_PACK_ARGS (t
);
20473 int len
= TREE_VEC_LENGTH (vec
);
20474 bool result
= false;
20477 for (i
= 0; i
< len
; ++i
)
20478 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20482 else if (TYPE_P (t
))
20484 /* [basic.link]: A name with no linkage (notably, the name
20485 of a class or enumeration declared in a local scope)
20486 shall not be used to declare an entity with linkage.
20487 This implies that names with no linkage cannot be used as
20490 DR 757 relaxes this restriction for C++0x. */
20491 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20492 : no_linkage_check (t
, /*relaxed_p=*/false));
20496 /* DR 488 makes use of a type with no linkage cause
20497 type deduction to fail. */
20498 if (complain
& tf_error
)
20500 if (TYPE_UNNAMED_P (nt
))
20501 error ("%qT is/uses unnamed type", t
);
20503 error ("template argument for %qD uses local type %qT",
20508 /* In order to avoid all sorts of complications, we do not
20509 allow variably-modified types as template arguments. */
20510 else if (variably_modified_type_p (t
, NULL_TREE
))
20512 if (complain
& tf_error
)
20513 error ("%qT is a variably modified type", t
);
20517 /* Class template and alias template arguments should be OK. */
20518 else if (DECL_TYPE_TEMPLATE_P (t
))
20520 /* A non-type argument of integral or enumerated type must be a
20522 else if (TREE_TYPE (t
)
20523 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20524 && !REFERENCE_REF_P (t
)
20525 && !TREE_CONSTANT (t
))
20527 if (complain
& tf_error
)
20528 error ("integral expression %qE is not constant", t
);
20535 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20537 int ix
, len
= DECL_NTPARMS (tmpl
);
20538 bool result
= false;
20540 for (ix
= 0; ix
!= len
; ix
++)
20542 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20545 if (result
&& (complain
& tf_error
))
20546 error (" trying to instantiate %qD", tmpl
);
20550 /* We're out of SFINAE context now, so generate diagnostics for the access
20551 errors we saw earlier when instantiating D from TMPL and ARGS. */
20554 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20556 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20557 tree type
= TREE_TYPE (pattern
);
20558 location_t loc
= input_location
;
20560 push_access_scope (d
);
20561 push_deferring_access_checks (dk_no_deferred
);
20562 input_location
= DECL_SOURCE_LOCATION (pattern
);
20563 tsubst (type
, args
, tf_warning_or_error
, d
);
20564 input_location
= loc
;
20565 pop_deferring_access_checks ();
20566 pop_access_scope (d
);
20569 /* Instantiate the indicated variable, function, or alias template TMPL with
20570 the template arguments in TARG_PTR. */
20573 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20575 tree targ_ptr
= orig_args
;
20579 bool access_ok
= true;
20581 if (tmpl
== error_mark_node
)
20582 return error_mark_node
;
20584 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20586 /* If this function is a clone, handle it specially. */
20587 if (DECL_CLONED_FUNCTION_P (tmpl
))
20592 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20593 DECL_CLONED_FUNCTION. */
20594 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20595 targ_ptr
, complain
);
20596 if (spec
== error_mark_node
)
20597 return error_mark_node
;
20599 /* Look for the clone. */
20600 FOR_EACH_CLONE (clone
, spec
)
20601 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20603 /* We should always have found the clone by now. */
20604 gcc_unreachable ();
20608 if (targ_ptr
== error_mark_node
)
20609 return error_mark_node
;
20611 /* Check to see if we already have this specialization. */
20612 gen_tmpl
= most_general_template (tmpl
);
20613 if (TMPL_ARGS_DEPTH (targ_ptr
)
20614 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20615 /* targ_ptr only has the innermost template args, so add the outer ones
20616 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20617 the case of a non-dependent call within a template definition). */
20618 targ_ptr
= (add_outermost_template_args
20619 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20622 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20623 but it doesn't seem to be on the hot path. */
20624 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20626 gcc_checking_assert (tmpl
== gen_tmpl
20628 = retrieve_specialization (tmpl
, orig_args
, 0))
20630 || fndecl
== NULL_TREE
);
20632 if (spec
!= NULL_TREE
)
20634 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20636 if (complain
& tf_error
)
20637 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20638 return error_mark_node
;
20643 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20645 return error_mark_node
;
20647 /* We are building a FUNCTION_DECL, during which the access of its
20648 parameters and return types have to be checked. However this
20649 FUNCTION_DECL which is the desired context for access checking
20650 is not built yet. We solve this chicken-and-egg problem by
20651 deferring all checks until we have the FUNCTION_DECL. */
20652 push_deferring_access_checks (dk_deferred
);
20654 /* Instantiation of the function happens in the context of the function
20655 template, not the context of the overload resolution we're doing. */
20656 push_to_top_level ();
20657 /* If there are dependent arguments, e.g. because we're doing partial
20658 ordering, make sure processing_template_decl stays set. */
20659 if (uses_template_parms (targ_ptr
))
20660 ++processing_template_decl
;
20661 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20663 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20664 complain
, gen_tmpl
, true);
20665 push_nested_class (ctx
);
20668 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20670 fndecl
= NULL_TREE
;
20671 if (VAR_P (pattern
))
20673 /* We need to determine if we're using a partial or explicit
20674 specialization now, because the type of the variable could be
20676 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20677 tree elt
= most_specialized_partial_spec (tid
, complain
);
20678 if (elt
== error_mark_node
)
20679 pattern
= error_mark_node
;
20682 tree partial_tmpl
= TREE_VALUE (elt
);
20683 tree partial_args
= TREE_PURPOSE (elt
);
20684 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20685 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20689 /* Substitute template parameters to obtain the specialization. */
20690 if (fndecl
== NULL_TREE
)
20691 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20692 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20693 pop_nested_class ();
20694 pop_from_top_level ();
20696 if (fndecl
== error_mark_node
)
20698 pop_deferring_access_checks ();
20699 return error_mark_node
;
20702 /* The DECL_TI_TEMPLATE should always be the immediate parent
20703 template, not the most general template. */
20704 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20705 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20707 /* Now we know the specialization, compute access previously
20708 deferred. Do no access control for inheriting constructors,
20709 as we already checked access for the inherited constructor. */
20710 if (!(flag_new_inheriting_ctors
20711 && DECL_INHERITED_CTOR (fndecl
)))
20713 push_access_scope (fndecl
);
20714 if (!perform_deferred_access_checks (complain
))
20716 pop_access_scope (fndecl
);
20718 pop_deferring_access_checks ();
20720 /* If we've just instantiated the main entry point for a function,
20721 instantiate all the alternate entry points as well. We do this
20722 by cloning the instantiation of the main entry point, not by
20723 instantiating the template clones. */
20724 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20725 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20726 clone_function_decl (fndecl
, /*update_methods=*/false);
20730 if (!(complain
& tf_error
))
20732 /* Remember to reinstantiate when we're out of SFINAE so the user
20733 can see the errors. */
20734 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20736 return error_mark_node
;
20741 /* Wrapper for instantiate_template_1. */
20744 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20747 timevar_push (TV_TEMPLATE_INST
);
20748 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20749 timevar_pop (TV_TEMPLATE_INST
);
20753 /* Instantiate the alias template TMPL with ARGS. Also push a template
20754 instantiation level, which instantiate_template doesn't do because
20755 functions and variables have sufficient context established by the
20759 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20761 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20762 return error_mark_node
;
20765 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20766 args
, tmpl
, complain
,
20767 /*require_all_args=*/true,
20768 /*use_default_args=*/true);
20770 /* FIXME check for satisfaction in check_instantiated_args. */
20772 && !any_dependent_template_arguments_p (args
)
20773 && !constraints_satisfied_p (tmpl
, args
))
20775 if (complain
& tf_error
)
20777 auto_diagnostic_group d
;
20778 error ("template constraint failure for %qD", tmpl
);
20779 diagnose_constraints (input_location
, tmpl
, args
);
20781 return error_mark_node
;
20784 if (!push_tinst_level (tmpl
, args
))
20785 return error_mark_node
;
20786 tree r
= instantiate_template (tmpl
, args
, complain
);
20787 pop_tinst_level ();
20792 /* PARM is a template parameter pack for FN. Returns true iff
20793 PARM is used in a deducible way in the argument list of FN. */
20796 pack_deducible_p (tree parm
, tree fn
)
20798 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20799 for (; t
; t
= TREE_CHAIN (t
))
20801 tree type
= TREE_VALUE (t
);
20803 if (!PACK_EXPANSION_P (type
))
20805 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20806 packs
; packs
= TREE_CHAIN (packs
))
20807 if (template_args_equal (TREE_VALUE (packs
), parm
))
20809 /* The template parameter pack is used in a function parameter
20810 pack. If this is the end of the parameter list, the
20811 template parameter pack is deducible. */
20812 if (TREE_CHAIN (t
) == void_list_node
)
20815 /* Otherwise, not. Well, it could be deduced from
20816 a non-pack parameter, but doing so would end up with
20817 a deduction mismatch, so don't bother. */
20821 /* The template parameter pack isn't used in any function parameter
20822 packs, but it might be used deeper, e.g. tuple<Args...>. */
20826 /* Subroutine of fn_type_unification: check non-dependent parms for
20830 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20831 tree fn
, unification_kind_t strict
, int flags
,
20832 struct conversion
**convs
, bool explain_p
)
20834 /* Non-constructor methods need to leave a conversion for 'this', which
20835 isn't included in nargs here. */
20836 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20837 && !DECL_CONSTRUCTOR_P (fn
));
20839 for (unsigned ia
= 0;
20840 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20842 tree parm
= TREE_VALUE (parms
);
20844 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20845 && (!TREE_CHAIN (parms
)
20846 || TREE_CHAIN (parms
) == void_list_node
))
20847 /* For a function parameter pack that occurs at the end of the
20848 parameter-declaration-list, the type A of each remaining
20849 argument of the call is compared with the type P of the
20850 declarator-id of the function parameter pack. */
20853 parms
= TREE_CHAIN (parms
);
20855 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20856 /* For a function parameter pack that does not occur at the
20857 end of the parameter-declaration-list, the type of the
20858 parameter pack is a non-deduced context. */
20861 if (!uses_template_parms (parm
))
20863 tree arg
= args
[ia
];
20864 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20865 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20867 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20868 conv_p
, explain_p
))
20878 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20879 NARGS elements of the arguments that are being used when calling
20880 it. TARGS is a vector into which the deduced template arguments
20883 Returns either a FUNCTION_DECL for the matching specialization of FN or
20884 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20885 true, diagnostics will be printed to explain why it failed.
20887 If FN is a conversion operator, or we are trying to produce a specific
20888 specialization, RETURN_TYPE is the return type desired.
20890 The EXPLICIT_TARGS are explicit template arguments provided via a
20893 The parameter STRICT is one of:
20896 We are deducing arguments for a function call, as in
20897 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20898 deducing arguments for a call to the result of a conversion
20899 function template, as in [over.call.object].
20902 We are deducing arguments for a conversion function, as in
20903 [temp.deduct.conv].
20906 We are deducing arguments when doing an explicit instantiation
20907 as in [temp.explicit], when determining an explicit specialization
20908 as in [temp.expl.spec], or when taking the address of a function
20909 template, as in [temp.deduct.funcaddr]. */
20912 fn_type_unification (tree fn
,
20913 tree explicit_targs
,
20916 unsigned int nargs
,
20918 unification_kind_t strict
,
20920 struct conversion
**convs
,
20926 tree decl
= NULL_TREE
;
20927 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20929 static int deduction_depth
;
20930 /* type_unification_real will pass back any access checks from default
20931 template argument substitution. */
20932 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20933 /* We don't have all the template args yet. */
20934 bool incomplete
= true;
20937 if (flag_new_inheriting_ctors
)
20938 fn
= strip_inheriting_ctors (fn
);
20940 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20941 tree r
= error_mark_node
;
20943 tree full_targs
= targs
;
20944 if (TMPL_ARGS_DEPTH (targs
)
20945 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20946 full_targs
= (add_outermost_template_args
20947 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20951 complain
|= tf_decltype
;
20953 /* In C++0x, it's possible to have a function template whose type depends
20954 on itself recursively. This is most obvious with decltype, but can also
20955 occur with enumeration scope (c++/48969). So we need to catch infinite
20956 recursion and reject the substitution at deduction time; this function
20957 will return error_mark_node for any repeated substitution.
20959 This also catches excessive recursion such as when f<N> depends on
20960 f<N-1> across all integers, and returns error_mark_node for all the
20961 substitutions back up to the initial one.
20963 This is, of course, not reentrant. */
20964 if (excessive_deduction_depth
)
20965 return error_mark_node
;
20968 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20970 fntype
= TREE_TYPE (fn
);
20971 if (explicit_targs
)
20975 The specified template arguments must match the template
20976 parameters in kind (i.e., type, nontype, template), and there
20977 must not be more arguments than there are parameters;
20978 otherwise type deduction fails.
20980 Nontype arguments must match the types of the corresponding
20981 nontype template parameters, or must be convertible to the
20982 types of the corresponding nontype parameters as specified in
20983 _temp.arg.nontype_, otherwise type deduction fails.
20985 All references in the function type of the function template
20986 to the corresponding template parameters are replaced by the
20987 specified template argument values. If a substitution in a
20988 template parameter or in the function type of the function
20989 template results in an invalid type, type deduction fails. */
20990 int i
, len
= TREE_VEC_LENGTH (tparms
);
20991 location_t loc
= input_location
;
20992 incomplete
= false;
20994 if (explicit_targs
== error_mark_node
)
20997 if (TMPL_ARGS_DEPTH (explicit_targs
)
20998 < TMPL_ARGS_DEPTH (full_targs
))
20999 explicit_targs
= add_outermost_template_args (full_targs
,
21002 /* Adjust any explicit template arguments before entering the
21003 substitution context. */
21005 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21006 complain
|tf_partial
,
21007 /*require_all_args=*/false,
21008 /*use_default_args=*/false));
21009 if (explicit_targs
== error_mark_node
)
21012 /* Substitute the explicit args into the function type. This is
21013 necessary so that, for instance, explicitly declared function
21014 arguments can match null pointed constants. If we were given
21015 an incomplete set of explicit args, we must not do semantic
21016 processing during substitution as we could create partial
21018 for (i
= 0; i
< len
; i
++)
21020 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21021 bool parameter_pack
= false;
21022 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21024 /* Dig out the actual parm. */
21025 if (TREE_CODE (parm
) == TYPE_DECL
21026 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21028 parm
= TREE_TYPE (parm
);
21029 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21031 else if (TREE_CODE (parm
) == PARM_DECL
)
21033 parm
= DECL_INITIAL (parm
);
21034 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21037 if (targ
== NULL_TREE
)
21038 /* No explicit argument for this template parameter. */
21040 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21042 /* Mark the argument pack as "incomplete". We could
21043 still deduce more arguments during unification.
21044 We remove this mark in type_unification_real. */
21045 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21046 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21047 = ARGUMENT_PACK_ARGS (targ
);
21049 /* We have some incomplete argument packs. */
21056 if (!push_tinst_level (fn
, explicit_targs
))
21058 excessive_deduction_depth
= true;
21061 ++processing_template_decl
;
21062 input_location
= DECL_SOURCE_LOCATION (fn
);
21063 /* Ignore any access checks; we'll see them again in
21064 instantiate_template and they might have the wrong
21065 access path at this point. */
21066 push_deferring_access_checks (dk_deferred
);
21067 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21068 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21069 pop_deferring_access_checks ();
21070 input_location
= loc
;
21071 --processing_template_decl
;
21072 pop_tinst_level ();
21074 if (fntype
== error_mark_node
)
21078 /* Place the explicitly specified arguments in TARGS. */
21079 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21080 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21081 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21082 if (!incomplete
&& CHECKING_P
21083 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21084 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21085 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21088 if (return_type
&& strict
!= DEDUCE_CALL
)
21090 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21091 new_args
[0] = return_type
;
21092 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21100 /* Never do unification on the 'this' parameter. */
21101 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21103 if (return_type
&& strict
== DEDUCE_CALL
)
21105 /* We're deducing for a call to the result of a template conversion
21106 function. The parms we really want are in return_type. */
21107 if (INDIRECT_TYPE_P (return_type
))
21108 return_type
= TREE_TYPE (return_type
);
21109 parms
= TYPE_ARG_TYPES (return_type
);
21111 else if (return_type
)
21113 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21116 /* We allow incomplete unification without an error message here
21117 because the standard doesn't seem to explicitly prohibit it. Our
21118 callers must be ready to deal with unification failures in any
21121 /* If we aren't explaining yet, push tinst context so we can see where
21122 any errors (e.g. from class instantiations triggered by instantiation
21123 of default template arguments) come from. If we are explaining, this
21124 context is redundant. */
21125 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21127 excessive_deduction_depth
= true;
21131 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21132 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21133 strict
, &checks
, explain_p
);
21135 pop_tinst_level ();
21139 /* Now that we have bindings for all of the template arguments,
21140 ensure that the arguments deduced for the template template
21141 parameters have compatible template parameter lists. We cannot
21142 check this property before we have deduced all template
21143 arguments, because the template parameter types of a template
21144 template parameter might depend on prior template parameters
21145 deduced after the template template parameter. The following
21146 ill-formed example illustrates this issue:
21148 template<typename T, template<T> class C> void f(C<5>, T);
21150 template<int N> struct X {};
21153 f(X<5>(), 5l); // error: template argument deduction fails
21156 The template parameter list of 'C' depends on the template type
21157 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21158 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21159 time that we deduce 'C'. */
21160 if (!template_template_parm_bindings_ok_p
21161 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21163 unify_inconsistent_template_template_parameters (explain_p
);
21167 /* DR 1391: All parameters have args, now check non-dependent parms for
21169 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21174 /* All is well so far. Now, check:
21178 When all template arguments have been deduced, all uses of
21179 template parameters in nondeduced contexts are replaced with
21180 the corresponding deduced argument values. If the
21181 substitution results in an invalid type, as described above,
21182 type deduction fails. */
21183 if (!push_tinst_level (fn
, targs
))
21185 excessive_deduction_depth
= true;
21189 /* Also collect access checks from the instantiation. */
21190 reopen_deferring_access_checks (checks
);
21192 decl
= instantiate_template (fn
, targs
, complain
);
21194 checks
= get_deferred_access_checks ();
21195 pop_deferring_access_checks ();
21197 pop_tinst_level ();
21199 if (decl
== error_mark_node
)
21202 /* Now perform any access checks encountered during substitution. */
21203 push_access_scope (decl
);
21204 ok
= perform_access_checks (checks
, complain
);
21205 pop_access_scope (decl
);
21209 /* If we're looking for an exact match, check that what we got
21210 is indeed an exact match. It might not be if some template
21211 parameters are used in non-deduced contexts. But don't check
21212 for an exact match if we have dependent template arguments;
21213 in that case we're doing partial ordering, and we already know
21214 that we have two candidates that will provide the actual type. */
21215 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21217 tree substed
= TREE_TYPE (decl
);
21221 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21223 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21224 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21225 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21227 unify_type_mismatch (explain_p
, args
[i
],
21228 TREE_VALUE (sarg
));
21233 /* After doing deduction with the inherited constructor, actually return an
21234 instantiation of the inheriting constructor. */
21236 decl
= instantiate_template (orig_fn
, targs
, complain
);
21242 if (excessive_deduction_depth
)
21244 if (deduction_depth
== 0)
21245 /* Reset once we're all the way out. */
21246 excessive_deduction_depth
= false;
21252 /* Adjust types before performing type deduction, as described in
21253 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21254 sections are symmetric. PARM is the type of a function parameter
21255 or the return type of the conversion function. ARG is the type of
21256 the argument passed to the call, or the type of the value
21257 initialized with the result of the conversion function.
21258 ARG_EXPR is the original argument expression, which may be null. */
21261 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21274 /* Swap PARM and ARG throughout the remainder of this
21275 function; the handling is precisely symmetric since PARM
21276 will initialize ARG rather than vice versa. */
21277 std::swap (parm
, arg
);
21281 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21282 too, but here handle it by stripping the reference from PARM
21283 rather than by adding it to ARG. */
21284 if (TYPE_REF_P (*parm
)
21285 && TYPE_REF_IS_RVALUE (*parm
)
21286 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21287 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21288 && TYPE_REF_P (*arg
)
21289 && !TYPE_REF_IS_RVALUE (*arg
))
21290 *parm
= TREE_TYPE (*parm
);
21291 /* Nothing else to do in this case. */
21295 gcc_unreachable ();
21298 if (!TYPE_REF_P (*parm
))
21300 /* [temp.deduct.call]
21302 If P is not a reference type:
21304 --If A is an array type, the pointer type produced by the
21305 array-to-pointer standard conversion (_conv.array_) is
21306 used in place of A for type deduction; otherwise,
21308 --If A is a function type, the pointer type produced by
21309 the function-to-pointer standard conversion
21310 (_conv.func_) is used in place of A for type deduction;
21313 --If A is a cv-qualified type, the top level
21314 cv-qualifiers of A's type are ignored for type
21316 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21317 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21318 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21319 *arg
= build_pointer_type (*arg
);
21321 *arg
= TYPE_MAIN_VARIANT (*arg
);
21324 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21325 reference to a cv-unqualified template parameter that does not represent a
21326 template parameter of a class template (during class template argument
21327 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21328 an lvalue, the type "lvalue reference to A" is used in place of A for type
21330 if (TYPE_REF_P (*parm
)
21331 && TYPE_REF_IS_RVALUE (*parm
)
21332 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21333 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21334 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21335 && (arg_expr
? lvalue_p (arg_expr
)
21336 /* try_one_overload doesn't provide an arg_expr, but
21337 functions are always lvalues. */
21338 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21339 *arg
= build_reference_type (*arg
);
21341 /* [temp.deduct.call]
21343 If P is a cv-qualified type, the top level cv-qualifiers
21344 of P's type are ignored for type deduction. If P is a
21345 reference type, the type referred to by P is used for
21347 *parm
= TYPE_MAIN_VARIANT (*parm
);
21348 if (TYPE_REF_P (*parm
))
21350 *parm
= TREE_TYPE (*parm
);
21351 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21354 /* DR 322. For conversion deduction, remove a reference type on parm
21355 too (which has been swapped into ARG). */
21356 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21357 *arg
= TREE_TYPE (*arg
);
21362 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21363 template which doesn't contain any deducible template parameters; check if
21364 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21365 unify_one_argument. */
21368 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21369 int flags
, struct conversion
**conv_p
,
21375 type
= TREE_TYPE (arg
);
21379 if (same_type_p (parm
, type
))
21380 return unify_success (explain_p
);
21382 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21383 if (strict
== DEDUCE_CONV
)
21385 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21386 return unify_success (explain_p
);
21388 else if (strict
!= DEDUCE_EXACT
)
21391 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21393 /* Avoid recalculating this in add_function_candidate. */
21395 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21397 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21399 return unify_success (explain_p
);
21402 if (strict
== DEDUCE_EXACT
)
21403 return unify_type_mismatch (explain_p
, parm
, arg
);
21405 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21408 static bool uses_deducible_template_parms (tree type
);
21410 /* Returns true iff the expression EXPR is one from which a template
21411 argument can be deduced. In other words, if it's an undecorated
21412 use of a template non-type parameter. */
21415 deducible_expression (tree expr
)
21417 /* Strip implicit conversions. */
21418 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21419 expr
= TREE_OPERAND (expr
, 0);
21420 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21423 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21424 deducible way; that is, if it has a max value of <PARM> - 1. */
21427 deducible_array_bound (tree domain
)
21429 if (domain
== NULL_TREE
)
21432 tree max
= TYPE_MAX_VALUE (domain
);
21433 if (TREE_CODE (max
) != MINUS_EXPR
)
21436 return deducible_expression (TREE_OPERAND (max
, 0));
21439 /* Returns true iff the template arguments ARGS use a template parameter
21440 in a deducible way. */
21443 deducible_template_args (tree args
)
21445 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21448 tree elt
= TREE_VEC_ELT (args
, i
);
21449 if (ARGUMENT_PACK_P (elt
))
21450 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21453 if (PACK_EXPANSION_P (elt
))
21454 elt
= PACK_EXPANSION_PATTERN (elt
);
21455 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21457 else if (TYPE_P (elt
))
21458 deducible
= uses_deducible_template_parms (elt
);
21460 deducible
= deducible_expression (elt
);
21468 /* Returns true iff TYPE contains any deducible references to template
21469 parameters, as per 14.8.2.5. */
21472 uses_deducible_template_parms (tree type
)
21474 if (PACK_EXPANSION_P (type
))
21475 type
= PACK_EXPANSION_PATTERN (type
);
21482 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21483 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21489 if (INDIRECT_TYPE_P (type
))
21490 return uses_deducible_template_parms (TREE_TYPE (type
));
21492 /* T[integer-constant ]
21494 if (TREE_CODE (type
) == ARRAY_TYPE
)
21495 return (uses_deducible_template_parms (TREE_TYPE (type
))
21496 || deducible_array_bound (TYPE_DOMAIN (type
)));
21508 if (TYPE_PTRMEM_P (type
))
21509 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21510 || (uses_deducible_template_parms
21511 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21513 /* template-name <T> (where template-name refers to a class template)
21514 template-name <i> (where template-name refers to a class template) */
21515 if (CLASS_TYPE_P (type
)
21516 && CLASSTYPE_TEMPLATE_INFO (type
)
21517 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21518 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21519 (CLASSTYPE_TI_ARGS (type
)));
21524 if (FUNC_OR_METHOD_TYPE_P (type
))
21526 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21528 tree parm
= TYPE_ARG_TYPES (type
);
21529 if (TREE_CODE (type
) == METHOD_TYPE
)
21530 parm
= TREE_CHAIN (parm
);
21531 for (; parm
; parm
= TREE_CHAIN (parm
))
21532 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21539 /* Subroutine of type_unification_real and unify_pack_expansion to
21540 handle unification of a single P/A pair. Parameters are as
21541 for those functions. */
21544 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21545 int subr
, unification_kind_t strict
,
21548 tree arg_expr
= NULL_TREE
;
21551 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21552 return unify_invalid (explain_p
);
21553 if (arg
== unknown_type_node
)
21554 /* We can't deduce anything from this, but we might get all the
21555 template args from other function args. */
21556 return unify_success (explain_p
);
21558 /* Implicit conversions (Clause 4) will be performed on a function
21559 argument to convert it to the type of the corresponding function
21560 parameter if the parameter type contains no template-parameters that
21561 participate in template argument deduction. */
21562 if (strict
!= DEDUCE_EXACT
21563 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21564 /* For function parameters with no deducible template parameters,
21565 just return. We'll check non-dependent conversions later. */
21566 return unify_success (explain_p
);
21571 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21572 | UNIFY_ALLOW_MORE_CV_QUAL
21573 | UNIFY_ALLOW_DERIVED
);
21577 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21581 arg_strict
= UNIFY_ALLOW_NONE
;
21585 gcc_unreachable ();
21588 /* We only do these transformations if this is the top-level
21589 parameter_type_list in a call or declaration matching; in other
21590 situations (nested function declarators, template argument lists) we
21591 won't be comparing a type to an expression, and we don't do any type
21597 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21598 if (type_unknown_p (arg
))
21600 /* [temp.deduct.type] A template-argument can be
21601 deduced from a pointer to function or pointer
21602 to member function argument if the set of
21603 overloaded functions does not contain function
21604 templates and at most one of a set of
21605 overloaded functions provides a unique
21607 resolve_overloaded_unification (tparms
, targs
, parm
,
21609 arg_strict
, explain_p
);
21610 /* If a unique match was not found, this is a
21611 non-deduced context, so we still succeed. */
21612 return unify_success (explain_p
);
21616 arg
= unlowered_expr_type (arg
);
21617 if (arg
== error_mark_node
)
21618 return unify_invalid (explain_p
);
21622 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21625 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21626 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21627 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21629 /* For deduction from an init-list we need the actual list. */
21630 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21632 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21635 /* for_each_template_parm callback that always returns 0. */
21638 zero_r (tree
, void *)
21643 /* for_each_template_parm any_fn callback to handle deduction of a template
21644 type argument from the type of an array bound. */
21647 array_deduction_r (tree t
, void *data
)
21649 tree_pair_p d
= (tree_pair_p
)data
;
21650 tree
&tparms
= d
->purpose
;
21651 tree
&targs
= d
->value
;
21653 if (TREE_CODE (t
) == ARRAY_TYPE
)
21654 if (tree dom
= TYPE_DOMAIN (t
))
21655 if (tree max
= TYPE_MAX_VALUE (dom
))
21657 if (TREE_CODE (max
) == MINUS_EXPR
)
21658 max
= TREE_OPERAND (max
, 0);
21659 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21660 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21661 UNIFY_ALLOW_NONE
, /*explain*/false);
21664 /* Keep walking. */
21668 /* Try to deduce any not-yet-deduced template type arguments from the type of
21669 an array bound. This is handled separately from unify because 14.8.2.5 says
21670 "The type of a type parameter is only deduced from an array bound if it is
21671 not otherwise deduced." */
21674 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21676 tree_pair_s data
= { tparms
, targs
};
21677 hash_set
<tree
> visited
;
21678 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21679 /*nondeduced*/false, array_deduction_r
);
21682 /* Most parms like fn_type_unification.
21684 If SUBR is 1, we're being called recursively (to unify the
21685 arguments of a function or method parameter of a function
21688 CHECKS is a pointer to a vector of access checks encountered while
21689 substituting default template arguments. */
21692 type_unification_real (tree tparms
,
21696 unsigned int xnargs
,
21698 unification_kind_t strict
,
21699 vec
<deferred_access_check
, va_gc
> **checks
,
21704 int ntparms
= TREE_VEC_LENGTH (tparms
);
21705 int saw_undeduced
= 0;
21708 unsigned int nargs
;
21711 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21712 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21713 gcc_assert (ntparms
> 0);
21715 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21717 /* Reset the number of non-defaulted template arguments contained
21719 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21727 while (parms
&& parms
!= void_list_node
21730 parm
= TREE_VALUE (parms
);
21732 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21733 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21734 /* For a function parameter pack that occurs at the end of the
21735 parameter-declaration-list, the type A of each remaining
21736 argument of the call is compared with the type P of the
21737 declarator-id of the function parameter pack. */
21740 parms
= TREE_CHAIN (parms
);
21742 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21743 /* For a function parameter pack that does not occur at the
21744 end of the parameter-declaration-list, the type of the
21745 parameter pack is a non-deduced context. */
21751 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21757 && parms
!= void_list_node
21758 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21760 /* Unify the remaining arguments with the pack expansion type. */
21762 tree parmvec
= make_tree_vec (1);
21764 /* Allocate a TREE_VEC and copy in all of the arguments */
21765 argvec
= make_tree_vec (nargs
- ia
);
21766 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21767 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21769 /* Copy the parameter into parmvec. */
21770 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21771 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21772 /*subr=*/subr
, explain_p
))
21775 /* Advance to the end of the list of parameters. */
21776 parms
= TREE_CHAIN (parms
);
21779 /* Fail if we've reached the end of the parm list, and more args
21780 are present, and the parm list isn't variadic. */
21781 if (ia
< nargs
&& parms
== void_list_node
)
21782 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21783 /* Fail if parms are left and they don't have default values and
21784 they aren't all deduced as empty packs (c++/57397). This is
21785 consistent with sufficient_parms_p. */
21786 if (parms
&& parms
!= void_list_node
21787 && TREE_PURPOSE (parms
) == NULL_TREE
)
21789 unsigned int count
= nargs
;
21794 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21797 p
= TREE_CHAIN (p
);
21799 while (p
&& p
!= void_list_node
);
21800 if (count
!= nargs
)
21801 return unify_too_few_arguments (explain_p
, ia
, count
,
21807 tsubst_flags_t complain
= (explain_p
21808 ? tf_warning_or_error
21810 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21812 for (i
= 0; i
< ntparms
; i
++)
21814 tree targ
= TREE_VEC_ELT (targs
, i
);
21815 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21817 /* Clear the "incomplete" flags on all argument packs now so that
21818 substituting them into later default arguments works. */
21819 if (targ
&& ARGUMENT_PACK_P (targ
))
21821 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21822 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21825 if (targ
|| tparm
== error_mark_node
)
21827 tparm
= TREE_VALUE (tparm
);
21829 if (TREE_CODE (tparm
) == TYPE_DECL
21830 && !tried_array_deduction
)
21832 try_array_deduction (tparms
, targs
, xparms
);
21833 tried_array_deduction
= true;
21834 if (TREE_VEC_ELT (targs
, i
))
21838 /* If this is an undeduced nontype parameter that depends on
21839 a type parameter, try another pass; its type may have been
21840 deduced from a later argument than the one from which
21841 this parameter can be deduced. */
21842 if (TREE_CODE (tparm
) == PARM_DECL
21843 && uses_template_parms (TREE_TYPE (tparm
))
21844 && saw_undeduced
< 2)
21850 /* Core issue #226 (C++0x) [temp.deduct]:
21852 If a template argument has not been deduced, its
21853 default template argument, if any, is used.
21855 When we are in C++98 mode, TREE_PURPOSE will either
21856 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21857 to explicitly check cxx_dialect here. */
21858 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21859 /* OK, there is a default argument. Wait until after the
21860 conversion check to do substitution. */
21863 /* If the type parameter is a parameter pack, then it will
21864 be deduced to an empty parameter pack. */
21865 if (template_parameter_pack_p (tparm
))
21869 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21871 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21872 TREE_CONSTANT (arg
) = 1;
21875 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21877 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21879 TREE_VEC_ELT (targs
, i
) = arg
;
21883 return unify_parameter_deduction_failure (explain_p
, tparm
);
21886 /* Now substitute into the default template arguments. */
21887 for (i
= 0; i
< ntparms
; i
++)
21889 tree targ
= TREE_VEC_ELT (targs
, i
);
21890 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21892 if (targ
|| tparm
== error_mark_node
)
21894 tree parm
= TREE_VALUE (tparm
);
21895 tree arg
= TREE_PURPOSE (tparm
);
21896 reopen_deferring_access_checks (*checks
);
21897 location_t save_loc
= input_location
;
21899 input_location
= DECL_SOURCE_LOCATION (parm
);
21901 if (saw_undeduced
== 1
21902 && TREE_CODE (parm
) == PARM_DECL
21903 && uses_template_parms (TREE_TYPE (parm
)))
21905 /* The type of this non-type parameter depends on undeduced
21906 parameters. Don't try to use its default argument yet,
21907 since we might deduce an argument for it on the next pass,
21908 but do check whether the arguments we already have cause
21909 substitution failure, so that that happens before we try
21910 later default arguments (78489). */
21911 ++processing_template_decl
;
21912 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21914 --processing_template_decl
;
21915 if (type
== error_mark_node
)
21916 arg
= error_mark_node
;
21922 /* Even if the call is happening in template context, getting
21923 here means it's non-dependent, and a default argument is
21924 considered a separate definition under [temp.decls], so we can
21925 do this substitution without processing_template_decl. This
21926 is important if the default argument contains something that
21927 might be instantiation-dependent like access (87480). */
21928 processing_template_decl_sentinel s
;
21929 tree substed
= NULL_TREE
;
21930 if (saw_undeduced
== 1)
21932 /* First instatiate in template context, in case we still
21933 depend on undeduced template parameters. */
21934 ++processing_template_decl
;
21935 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21937 --processing_template_decl
;
21938 if (substed
!= error_mark_node
21939 && !uses_template_parms (substed
))
21940 /* We replaced all the tparms, substitute again out of
21941 template context. */
21942 substed
= NULL_TREE
;
21945 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21948 if (!uses_template_parms (substed
))
21949 arg
= convert_template_argument (parm
, substed
, full_targs
,
21950 complain
, i
, NULL_TREE
);
21951 else if (saw_undeduced
== 1)
21954 arg
= error_mark_node
;
21957 input_location
= save_loc
;
21958 *checks
= get_deferred_access_checks ();
21959 pop_deferring_access_checks ();
21961 if (arg
== error_mark_node
)
21965 TREE_VEC_ELT (targs
, i
) = arg
;
21966 /* The position of the first default template argument,
21967 is also the number of non-defaulted arguments in TARGS.
21969 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21970 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21974 if (saw_undeduced
++ == 1)
21978 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21979 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21981 return unify_success (explain_p
);
21984 /* Subroutine of type_unification_real. Args are like the variables
21985 at the call site. ARG is an overloaded function (or template-id);
21986 we try deducing template args from each of the overloads, and if
21987 only one succeeds, we go with that. Modifies TARGS and returns
21988 true on success. */
21991 resolve_overloaded_unification (tree tparms
,
21995 unification_kind_t strict
,
21999 tree tempargs
= copy_node (targs
);
22001 tree goodfn
= NULL_TREE
;
22004 if (TREE_CODE (arg
) == ADDR_EXPR
)
22006 arg
= TREE_OPERAND (arg
, 0);
22012 if (TREE_CODE (arg
) == COMPONENT_REF
)
22013 /* Handle `&x' where `x' is some static or non-static member
22015 arg
= TREE_OPERAND (arg
, 1);
22017 if (TREE_CODE (arg
) == OFFSET_REF
)
22018 arg
= TREE_OPERAND (arg
, 1);
22020 /* Strip baselink information. */
22021 if (BASELINK_P (arg
))
22022 arg
= BASELINK_FUNCTIONS (arg
);
22024 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22026 /* If we got some explicit template args, we need to plug them into
22027 the affected templates before we try to unify, in case the
22028 explicit args will completely resolve the templates in question. */
22031 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22032 arg
= TREE_OPERAND (arg
, 0);
22034 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22037 tree subargs
, elem
;
22039 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22042 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22043 expl_subargs
, NULL_TREE
, tf_none
,
22044 /*require_all_args=*/true,
22045 /*use_default_args=*/true);
22046 if (subargs
!= error_mark_node
22047 && !any_dependent_template_arguments_p (subargs
))
22049 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
22050 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22051 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22052 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22061 /* If no templates (or more than one) are fully resolved by the
22062 explicit arguments, this template-id is a non-deduced context; it
22063 could still be OK if we deduce all template arguments for the
22064 enclosing call through other arguments. */
22068 else if (!OVL_P (arg
))
22069 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22070 -- but the deduction does not succeed because the expression is
22071 not just the function on its own. */
22074 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22077 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22078 strict
, sub_strict
, addr_p
, explain_p
)
22079 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22086 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22087 to function or pointer to member function argument if the set of
22088 overloaded functions does not contain function templates and at most
22089 one of a set of overloaded functions provides a unique match.
22091 So if we found multiple possibilities, we return success but don't
22092 deduce anything. */
22096 int i
= TREE_VEC_LENGTH (targs
);
22098 if (TREE_VEC_ELT (tempargs
, i
))
22100 tree old
= TREE_VEC_ELT (targs
, i
);
22101 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22102 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22103 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22104 /* Don't forget explicit template arguments in a pack. */
22105 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22106 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22107 TREE_VEC_ELT (targs
, i
) = new_
;
22116 /* Core DR 115: In contexts where deduction is done and fails, or in
22117 contexts where deduction is not done, if a template argument list is
22118 specified and it, along with any default template arguments, identifies
22119 a single function template specialization, then the template-id is an
22120 lvalue for the function template specialization. */
22123 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22125 tree expr
, offset
, baselink
;
22128 if (!type_unknown_p (orig_expr
))
22133 offset
= NULL_TREE
;
22134 baselink
= NULL_TREE
;
22136 if (TREE_CODE (expr
) == ADDR_EXPR
)
22138 expr
= TREE_OPERAND (expr
, 0);
22141 if (TREE_CODE (expr
) == OFFSET_REF
)
22144 expr
= TREE_OPERAND (expr
, 1);
22146 if (BASELINK_P (expr
))
22149 expr
= BASELINK_FUNCTIONS (expr
);
22152 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22155 tree goodfn
= NULL_TREE
;
22157 /* If we got some explicit template args, we need to plug them into
22158 the affected templates before we try to unify, in case the
22159 explicit args will completely resolve the templates in question. */
22161 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22162 tree arg
= TREE_OPERAND (expr
, 0);
22163 tree badfn
= NULL_TREE
;
22164 tree badargs
= NULL_TREE
;
22166 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22169 tree subargs
, elem
;
22171 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22174 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22175 expl_subargs
, NULL_TREE
, tf_none
,
22176 /*require_all_args=*/true,
22177 /*use_default_args=*/true);
22178 if (subargs
!= error_mark_node
22179 && !any_dependent_template_arguments_p (subargs
))
22181 elem
= instantiate_template (fn
, subargs
, tf_none
);
22182 if (elem
== error_mark_node
)
22187 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
22196 mark_used (goodfn
);
22199 expr
= build_baselink (BASELINK_BINFO (baselink
),
22200 BASELINK_ACCESS_BINFO (baselink
),
22201 expr
, BASELINK_OPTYPE (baselink
));
22205 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22206 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22209 expr
= cp_build_addr_expr (expr
, complain
);
22212 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22213 /* There were no good options and at least one bad one, so let the
22214 user know what the problem is. */
22215 instantiate_template (badfn
, badargs
, complain
);
22220 /* As above, but error out if the expression remains overloaded. */
22223 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22225 exp
= resolve_nondeduced_context (exp
, complain
);
22226 if (type_unknown_p (exp
))
22228 if (complain
& tf_error
)
22229 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22230 return error_mark_node
;
22235 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22236 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22237 different overloads deduce different arguments for a given parm.
22238 ADDR_P is true if the expression for which deduction is being
22239 performed was of the form "& fn" rather than simply "fn".
22241 Returns 1 on success. */
22244 try_one_overload (tree tparms
,
22249 unification_kind_t strict
,
22258 if (arg
== error_mark_node
)
22261 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22262 to function or pointer to member function argument if the set of
22263 overloaded functions does not contain function templates and at most
22264 one of a set of overloaded functions provides a unique match.
22266 So if this is a template, just return success. */
22268 if (uses_template_parms (arg
))
22271 if (TREE_CODE (arg
) == METHOD_TYPE
)
22272 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22274 arg
= build_pointer_type (arg
);
22276 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22278 /* We don't copy orig_targs for this because if we have already deduced
22279 some template args from previous args, unify would complain when we
22280 try to deduce a template parameter for the same argument, even though
22281 there isn't really a conflict. */
22282 nargs
= TREE_VEC_LENGTH (targs
);
22283 tempargs
= make_tree_vec (nargs
);
22285 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22288 /* First make sure we didn't deduce anything that conflicts with
22289 explicitly specified args. */
22290 for (i
= nargs
; i
--; )
22292 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22293 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22297 else if (uses_template_parms (elt
))
22298 /* Since we're unifying against ourselves, we will fill in
22299 template args used in the function parm list with our own
22300 template parms. Discard them. */
22301 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22302 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22304 /* Check that the argument at each index of the deduced argument pack
22305 is equivalent to the corresponding explicitly specified argument.
22306 We may have deduced more arguments than were explicitly specified,
22309 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22310 that's wrong if we deduce the same argument pack from multiple
22311 function arguments: it's only incomplete the first time. */
22313 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22314 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22316 if (TREE_VEC_LENGTH (deduced_pack
)
22317 < TREE_VEC_LENGTH (explicit_pack
))
22320 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22321 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22322 TREE_VEC_ELT (deduced_pack
, j
)))
22325 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22329 for (i
= nargs
; i
--; )
22331 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22334 TREE_VEC_ELT (targs
, i
) = elt
;
22340 /* PARM is a template class (perhaps with unbound template
22341 parameters). ARG is a fully instantiated type. If ARG can be
22342 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22343 TARGS are as for unify. */
22346 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22349 tree copy_of_targs
;
22351 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22353 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22354 /* Matches anything. */;
22355 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22356 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22359 /* We need to make a new template argument vector for the call to
22360 unify. If we used TARGS, we'd clutter it up with the result of
22361 the attempted unification, even if this class didn't work out.
22362 We also don't want to commit ourselves to all the unifications
22363 we've already done, since unification is supposed to be done on
22364 an argument-by-argument basis. In other words, consider the
22365 following pathological case:
22367 template <int I, int J, int K>
22370 template <int I, int J>
22371 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22373 template <int I, int J, int K>
22374 void f(S<I, J, K>, S<I, I, I>);
22383 Now, by the time we consider the unification involving `s2', we
22384 already know that we must have `f<0, 0, 0>'. But, even though
22385 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22386 because there are two ways to unify base classes of S<0, 1, 2>
22387 with S<I, I, I>. If we kept the already deduced knowledge, we
22388 would reject the possibility I=1. */
22389 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22391 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22393 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22398 /* If unification failed, we're done. */
22399 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22400 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22406 /* Given a template type PARM and a class type ARG, find the unique
22407 base type in ARG that is an instance of PARM. We do not examine
22408 ARG itself; only its base-classes. If there is not exactly one
22409 appropriate base class, return NULL_TREE. PARM may be the type of
22410 a partial specialization, as well as a plain template type. Used
22413 static enum template_base_result
22414 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22415 bool explain_p
, tree
*result
)
22417 tree rval
= NULL_TREE
;
22420 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22422 binfo
= TYPE_BINFO (complete_type (arg
));
22425 /* The type could not be completed. */
22426 *result
= NULL_TREE
;
22427 return tbr_incomplete_type
;
22430 /* Walk in inheritance graph order. The search order is not
22431 important, and this avoids multiple walks of virtual bases. */
22432 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22434 tree r
= try_class_unification (tparms
, targs
, parm
,
22435 BINFO_TYPE (binfo
), explain_p
);
22439 /* If there is more than one satisfactory baseclass, then:
22443 If they yield more than one possible deduced A, the type
22447 if (rval
&& !same_type_p (r
, rval
))
22449 *result
= NULL_TREE
;
22450 return tbr_ambiguous_baseclass
;
22458 return tbr_success
;
22461 /* Returns the level of DECL, which declares a template parameter. */
22464 template_decl_level (tree decl
)
22466 switch (TREE_CODE (decl
))
22469 case TEMPLATE_DECL
:
22470 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22473 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22476 gcc_unreachable ();
22481 /* Decide whether ARG can be unified with PARM, considering only the
22482 cv-qualifiers of each type, given STRICT as documented for unify.
22483 Returns nonzero iff the unification is OK on that basis. */
22486 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22488 int arg_quals
= cp_type_quals (arg
);
22489 int parm_quals
= cp_type_quals (parm
);
22491 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22492 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22494 /* Although a CVR qualifier is ignored when being applied to a
22495 substituted template parameter ([8.3.2]/1 for example), that
22496 does not allow us to unify "const T" with "int&" because both
22497 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22498 It is ok when we're allowing additional CV qualifiers
22499 at the outer level [14.8.2.1]/3,1st bullet. */
22500 if ((TYPE_REF_P (arg
)
22501 || FUNC_OR_METHOD_TYPE_P (arg
))
22502 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22505 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22506 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22510 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22511 && (arg_quals
& parm_quals
) != parm_quals
)
22514 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22515 && (parm_quals
& arg_quals
) != arg_quals
)
22521 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22523 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22525 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22526 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22527 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22529 *index
= TEMPLATE_TYPE_IDX (parm
);
22530 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22534 *index
= TEMPLATE_PARM_IDX (parm
);
22535 *level
= TEMPLATE_PARM_LEVEL (parm
);
22539 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22541 if (unify (TP, TA, P, A, S, EP)) \
22545 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22546 expansion at the end of PACKED_PARMS. Returns 0 if the type
22547 deduction succeeds, 1 otherwise. STRICT is the same as in
22548 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22549 function call argument list. We'll need to adjust the arguments to make them
22550 types. SUBR tells us if this is from a recursive call to
22551 type_unification_real, or for comparing two template argument
22555 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22556 tree packed_args
, unification_kind_t strict
,
22557 bool subr
, bool explain_p
)
22560 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22561 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22562 tree pack
, packs
= NULL_TREE
;
22563 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22565 /* Add in any args remembered from an earlier partial instantiation. */
22566 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22567 int levels
= TMPL_ARGS_DEPTH (targs
);
22569 packed_args
= expand_template_argument_pack (packed_args
);
22571 int len
= TREE_VEC_LENGTH (packed_args
);
22573 /* Determine the parameter packs we will be deducing from the
22574 pattern, and record their current deductions. */
22575 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22576 pack
; pack
= TREE_CHAIN (pack
))
22578 tree parm_pack
= TREE_VALUE (pack
);
22581 /* Only template parameter packs can be deduced, not e.g. function
22582 parameter packs or __bases or __integer_pack. */
22583 if (!TEMPLATE_PARM_P (parm_pack
))
22586 /* Determine the index and level of this parameter pack. */
22587 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22588 if (level
< levels
)
22591 /* Keep track of the parameter packs and their corresponding
22593 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22594 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22597 /* Loop through all of the arguments that have not yet been
22598 unified and unify each with the pattern. */
22599 for (i
= start
; i
< len
; i
++)
22602 bool any_explicit
= false;
22603 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22605 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22606 or the element of its argument pack at the current index if
22607 this argument was explicitly specified. */
22608 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22612 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22615 if (TREE_VALUE (pack
)
22616 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22617 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22619 any_explicit
= true;
22620 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22622 TMPL_ARG (targs
, level
, idx
) = arg
;
22625 /* If we had explicit template arguments, substitute them into the
22626 pattern before deduction. */
22629 /* Some arguments might still be unspecified or dependent. */
22631 ++processing_template_decl
;
22632 dependent
= any_dependent_template_arguments_p (targs
);
22634 --processing_template_decl
;
22635 parm
= tsubst (pattern
, targs
,
22636 explain_p
? tf_warning_or_error
: tf_none
,
22639 --processing_template_decl
;
22640 if (parm
== error_mark_node
)
22646 /* Unify the pattern with the current argument. */
22647 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22651 /* For each parameter pack, collect the deduced value. */
22652 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22655 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22657 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22658 TMPL_ARG (targs
, level
, idx
);
22662 /* Verify that the results of unification with the parameter packs
22663 produce results consistent with what we've seen before, and make
22664 the deduced argument packs available. */
22665 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22667 tree old_pack
= TREE_VALUE (pack
);
22668 tree new_args
= TREE_TYPE (pack
);
22669 int i
, len
= TREE_VEC_LENGTH (new_args
);
22671 bool nondeduced_p
= false;
22673 /* By default keep the original deduced argument pack.
22674 If necessary, more specific code is going to update the
22675 resulting deduced argument later down in this function. */
22676 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22677 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22679 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22680 actually deduce anything. */
22681 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22682 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22683 nondeduced_p
= true;
22687 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22689 /* If we had fewer function args than explicit template args,
22690 just use the explicits. */
22691 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22692 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22693 if (len
< explicit_len
)
22694 new_args
= explicit_args
;
22700 /* Build the deduced *_ARGUMENT_PACK. */
22701 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22703 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22704 TREE_CONSTANT (result
) = 1;
22707 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22709 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22711 /* Note the deduced argument packs for this parameter
22713 TMPL_ARG (targs
, level
, idx
) = result
;
22715 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22716 && (ARGUMENT_PACK_ARGS (old_pack
)
22717 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22719 /* We only had the explicitly-provided arguments before, but
22720 now we have a complete set of arguments. */
22721 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22723 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22724 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22725 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22729 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22730 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22732 if (!comp_template_args (old_args
, new_args
,
22733 &bad_old_arg
, &bad_new_arg
))
22734 /* Inconsistent unification of this parameter pack. */
22735 return unify_parameter_pack_inconsistent (explain_p
,
22741 return unify_success (explain_p
);
22744 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22745 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22746 parameters and return value are as for unify. */
22749 unify_array_domain (tree tparms
, tree targs
,
22750 tree parm_dom
, tree arg_dom
,
22758 /* Our representation of array types uses "N - 1" as the
22759 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22760 not an integer constant. We cannot unify arbitrarily
22761 complex expressions, so we eliminate the MINUS_EXPRs
22763 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22764 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22767 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22768 parm_max
= TREE_OPERAND (parm_max
, 0);
22770 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22771 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22774 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22775 trying to unify the type of a variable with the type
22776 of a template parameter. For example:
22778 template <unsigned int N>
22779 void f (char (&) [N]);
22786 Here, the type of the ARG will be "int [g(i)]", and
22787 may be a SAVE_EXPR, etc. */
22788 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22789 return unify_vla_arg (explain_p
, arg_dom
);
22790 arg_max
= TREE_OPERAND (arg_max
, 0);
22793 /* If only one of the bounds used a MINUS_EXPR, compensate
22794 by adding one to the other bound. */
22795 if (parm_cst
&& !arg_cst
)
22796 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22800 else if (arg_cst
&& !parm_cst
)
22801 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22806 return unify (tparms
, targs
, parm_max
, arg_max
,
22807 UNIFY_ALLOW_INTEGER
, explain_p
);
22810 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22812 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22817 if (PACK_EXPANSION_P (t
))
22818 t
= PACK_EXPANSION_PATTERN (t
);
22819 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22820 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22821 || DECL_TYPE_TEMPLATE_P (t
))
22823 else if (TYPE_P (t
))
22829 /* Deduce the value of template parameters. TPARMS is the (innermost)
22830 set of template parameters to a template. TARGS is the bindings
22831 for those template parameters, as determined thus far; TARGS may
22832 include template arguments for outer levels of template parameters
22833 as well. PARM is a parameter to a template function, or a
22834 subcomponent of that parameter; ARG is the corresponding argument.
22835 This function attempts to match PARM with ARG in a manner
22836 consistent with the existing assignments in TARGS. If more values
22837 are deduced, then TARGS is updated.
22839 Returns 0 if the type deduction succeeds, 1 otherwise. The
22840 parameter STRICT is a bitwise or of the following flags:
22843 Require an exact match between PARM and ARG.
22844 UNIFY_ALLOW_MORE_CV_QUAL:
22845 Allow the deduced ARG to be more cv-qualified (by qualification
22846 conversion) than ARG.
22847 UNIFY_ALLOW_LESS_CV_QUAL:
22848 Allow the deduced ARG to be less cv-qualified than ARG.
22849 UNIFY_ALLOW_DERIVED:
22850 Allow the deduced ARG to be a template base class of ARG,
22851 or a pointer to a template base class of the type pointed to by
22853 UNIFY_ALLOW_INTEGER:
22854 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22855 case for more information.
22856 UNIFY_ALLOW_OUTER_LEVEL:
22857 This is the outermost level of a deduction. Used to determine validity
22858 of qualification conversions. A valid qualification conversion must
22859 have const qualified pointers leading up to the inner type which
22860 requires additional CV quals, except at the outer level, where const
22861 is not required [conv.qual]. It would be normal to set this flag in
22862 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22863 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22864 This is the outermost level of a deduction, and PARM can be more CV
22865 qualified at this point.
22866 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22867 This is the outermost level of a deduction, and PARM can be less CV
22868 qualified at this point. */
22871 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22877 int strict_in
= strict
;
22878 tsubst_flags_t complain
= (explain_p
22879 ? tf_warning_or_error
22882 /* I don't think this will do the right thing with respect to types.
22883 But the only case I've seen it in so far has been array bounds, where
22884 signedness is the only information lost, and I think that will be
22885 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22886 finish_id_expression_1, and are also OK. */
22887 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22888 parm
= TREE_OPERAND (parm
, 0);
22890 if (arg
== error_mark_node
)
22891 return unify_invalid (explain_p
);
22892 if (arg
== unknown_type_node
22893 || arg
== init_list_type_node
)
22894 /* We can't deduce anything from this, but we might get all the
22895 template args from other function args. */
22896 return unify_success (explain_p
);
22898 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22899 return unify_success (explain_p
);
22901 /* If PARM uses template parameters, then we can't bail out here,
22902 even if ARG == PARM, since we won't record unifications for the
22903 template parameters. We might need them if we're trying to
22904 figure out which of two things is more specialized. */
22905 if (arg
== parm
&& !uses_template_parms (parm
))
22906 return unify_success (explain_p
);
22908 /* Handle init lists early, so the rest of the function can assume
22909 we're dealing with a type. */
22910 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22914 tree orig_parm
= parm
;
22916 if (!is_std_init_list (parm
)
22917 && TREE_CODE (parm
) != ARRAY_TYPE
)
22918 /* We can only deduce from an initializer list argument if the
22919 parameter is std::initializer_list or an array; otherwise this
22920 is a non-deduced context. */
22921 return unify_success (explain_p
);
22923 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22924 elttype
= TREE_TYPE (parm
);
22927 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22928 /* Deduction is defined in terms of a single type, so just punt
22929 on the (bizarre) std::initializer_list<T...>. */
22930 if (PACK_EXPANSION_P (elttype
))
22931 return unify_success (explain_p
);
22934 if (strict
!= DEDUCE_EXACT
22935 && TYPE_P (elttype
)
22936 && !uses_deducible_template_parms (elttype
))
22937 /* If ELTTYPE has no deducible template parms, skip deduction from
22938 the list elements. */;
22940 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22942 int elt_strict
= strict
;
22944 if (elt
== error_mark_node
)
22945 return unify_invalid (explain_p
);
22947 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22949 tree type
= TREE_TYPE (elt
);
22950 if (type
== error_mark_node
)
22951 return unify_invalid (explain_p
);
22952 /* It should only be possible to get here for a call. */
22953 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22954 elt_strict
|= maybe_adjust_types_for_deduction
22955 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22959 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22963 if (TREE_CODE (parm
) == ARRAY_TYPE
22964 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22966 /* Also deduce from the length of the initializer list. */
22967 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22968 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22969 if (idx
== error_mark_node
)
22970 return unify_invalid (explain_p
);
22971 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22975 /* If the std::initializer_list<T> deduction worked, replace the
22976 deduced A with std::initializer_list<A>. */
22977 if (orig_parm
!= parm
)
22979 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22980 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22981 targ
= listify (targ
);
22982 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22984 return unify_success (explain_p
);
22987 /* If parm and arg aren't the same kind of thing (template, type, or
22988 expression), fail early. */
22989 if (pa_kind (parm
) != pa_kind (arg
))
22990 return unify_invalid (explain_p
);
22992 /* Immediately reject some pairs that won't unify because of
22993 cv-qualification mismatches. */
22994 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22996 /* It is the elements of the array which hold the cv quals of an array
22997 type, and the elements might be template type parms. We'll check
22998 when we recurse. */
22999 && TREE_CODE (arg
) != ARRAY_TYPE
23000 /* We check the cv-qualifiers when unifying with template type
23001 parameters below. We want to allow ARG `const T' to unify with
23002 PARM `T' for example, when computing which of two templates
23003 is more specialized, for example. */
23004 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23005 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23006 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23008 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23009 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23010 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23011 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23012 strict
&= ~UNIFY_ALLOW_DERIVED
;
23013 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23014 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23016 switch (TREE_CODE (parm
))
23018 case TYPENAME_TYPE
:
23020 case UNBOUND_CLASS_TEMPLATE
:
23021 /* In a type which contains a nested-name-specifier, template
23022 argument values cannot be deduced for template parameters used
23023 within the nested-name-specifier. */
23024 return unify_success (explain_p
);
23026 case TEMPLATE_TYPE_PARM
:
23027 case TEMPLATE_TEMPLATE_PARM
:
23028 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23029 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23030 if (error_operand_p (tparm
))
23031 return unify_invalid (explain_p
);
23033 if (TEMPLATE_TYPE_LEVEL (parm
)
23034 != template_decl_level (tparm
))
23035 /* The PARM is not one we're trying to unify. Just check
23036 to see if it matches ARG. */
23038 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23039 && (is_auto (parm
) ? is_auto (arg
)
23040 : same_type_p (parm
, arg
)))
23041 return unify_success (explain_p
);
23043 return unify_type_mismatch (explain_p
, parm
, arg
);
23045 idx
= TEMPLATE_TYPE_IDX (parm
);
23046 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23047 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23048 if (error_operand_p (tparm
))
23049 return unify_invalid (explain_p
);
23051 /* Check for mixed types and values. */
23052 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23053 && TREE_CODE (tparm
) != TYPE_DECL
)
23054 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23055 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23056 gcc_unreachable ();
23058 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23060 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23061 && CLASS_TYPE_P (arg
))
23063 /* First try to match ARG directly. */
23064 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23068 /* Otherwise, look for a suitable base of ARG, as below. */
23069 enum template_base_result r
;
23070 r
= get_template_base (tparms
, targs
, parm
, arg
,
23073 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23077 /* ARG must be constructed from a template class or a template
23078 template parameter. */
23079 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23080 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23081 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23083 /* Deduce arguments T, i from TT<T> or TT<i>. */
23084 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23087 arg
= TYPE_TI_TEMPLATE (arg
);
23089 /* Fall through to deduce template name. */
23092 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23093 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23095 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23097 /* Simple cases: Value already set, does match or doesn't. */
23098 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23099 return unify_success (explain_p
);
23101 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23105 /* If PARM is `const T' and ARG is only `int', we don't have
23106 a match unless we are allowing additional qualification.
23107 If ARG is `const int' and PARM is just `T' that's OK;
23108 that binds `const int' to `T'. */
23109 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23111 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23113 /* Consider the case where ARG is `const volatile int' and
23114 PARM is `const T'. Then, T should be `volatile int'. */
23115 arg
= cp_build_qualified_type_real
23116 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23117 if (arg
== error_mark_node
)
23118 return unify_invalid (explain_p
);
23120 /* Simple cases: Value already set, does match or doesn't. */
23121 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23122 return unify_success (explain_p
);
23124 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23126 /* Make sure that ARG is not a variable-sized array. (Note
23127 that were talking about variable-sized arrays (like
23128 `int[n]'), rather than arrays of unknown size (like
23129 `int[]').) We'll get very confused by such a type since
23130 the bound of the array is not constant, and therefore
23131 not mangleable. Besides, such types are not allowed in
23132 ISO C++, so we can do as we please here. We do allow
23133 them for 'auto' deduction, since that isn't ABI-exposed. */
23134 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23135 return unify_vla_arg (explain_p
, arg
);
23137 /* Strip typedefs as in convert_template_argument. */
23138 arg
= canonicalize_type_argument (arg
, tf_none
);
23141 /* If ARG is a parameter pack or an expansion, we cannot unify
23142 against it unless PARM is also a parameter pack. */
23143 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23144 && !template_parameter_pack_p (parm
))
23145 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23147 /* If the argument deduction results is a METHOD_TYPE,
23148 then there is a problem.
23149 METHOD_TYPE doesn't map to any real C++ type the result of
23150 the deduction cannot be of that type. */
23151 if (TREE_CODE (arg
) == METHOD_TYPE
)
23152 return unify_method_type_error (explain_p
, arg
);
23154 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23155 return unify_success (explain_p
);
23157 case TEMPLATE_PARM_INDEX
:
23158 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23159 if (error_operand_p (tparm
))
23160 return unify_invalid (explain_p
);
23162 if (TEMPLATE_PARM_LEVEL (parm
)
23163 != template_decl_level (tparm
))
23165 /* The PARM is not one we're trying to unify. Just check
23166 to see if it matches ARG. */
23167 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23168 && cp_tree_equal (parm
, arg
));
23170 unify_expression_unequal (explain_p
, parm
, arg
);
23174 idx
= TEMPLATE_PARM_IDX (parm
);
23175 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23179 if ((strict
& UNIFY_ALLOW_INTEGER
)
23180 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23181 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23182 /* We're deducing from an array bound, the type doesn't matter. */
23183 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23184 int x
= !cp_tree_equal (targ
, arg
);
23186 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23190 /* [temp.deduct.type] If, in the declaration of a function template
23191 with a non-type template-parameter, the non-type
23192 template-parameter is used in an expression in the function
23193 parameter-list and, if the corresponding template-argument is
23194 deduced, the template-argument type shall match the type of the
23195 template-parameter exactly, except that a template-argument
23196 deduced from an array bound may be of any integral type.
23197 The non-type parameter might use already deduced type parameters. */
23198 tparm
= TREE_TYPE (parm
);
23199 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23200 /* We don't have enough levels of args to do any substitution. This
23201 can happen in the context of -fnew-ttp-matching. */;
23204 ++processing_template_decl
;
23205 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23206 --processing_template_decl
;
23208 if (tree a
= type_uses_auto (tparm
))
23210 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23211 if (tparm
== error_mark_node
)
23216 if (!TREE_TYPE (arg
))
23217 /* Template-parameter dependent expression. Just accept it for now.
23218 It will later be processed in convert_template_argument. */
23220 else if (same_type_ignoring_top_level_qualifiers_p
23221 (non_reference (TREE_TYPE (arg
)),
23222 non_reference (tparm
)))
23223 /* OK. Ignore top-level quals here because a class-type template
23224 parameter object is const. */;
23225 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23226 && CP_INTEGRAL_TYPE_P (tparm
))
23227 /* Convert the ARG to the type of PARM; the deduced non-type
23228 template argument must exactly match the types of the
23229 corresponding parameter. */
23230 arg
= fold (build_nop (tparm
, arg
));
23231 else if (uses_template_parms (tparm
))
23233 /* We haven't deduced the type of this parameter yet. */
23234 if (cxx_dialect
>= cxx17
23235 /* We deduce from array bounds in try_array_deduction. */
23236 && !(strict
& UNIFY_ALLOW_INTEGER
))
23238 /* Deduce it from the non-type argument. */
23239 tree atype
= TREE_TYPE (arg
);
23240 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23242 UNIFY_ALLOW_NONE
, explain_p
);
23245 /* Try again later. */
23246 return unify_success (explain_p
);
23249 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23251 /* If ARG is a parameter pack or an expansion, we cannot unify
23252 against it unless PARM is also a parameter pack. */
23253 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23254 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23255 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23258 bool removed_attr
= false;
23259 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23261 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23262 return unify_success (explain_p
);
23266 /* A pointer-to-member constant can be unified only with
23267 another constant. */
23268 if (TREE_CODE (arg
) != PTRMEM_CST
)
23269 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23271 /* Just unify the class member. It would be useless (and possibly
23272 wrong, depending on the strict flags) to unify also
23273 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23274 arg refer to the same variable, even if through different
23275 classes. For instance:
23277 struct A { int x; };
23280 Unification of &A::x and &B::x must succeed. */
23281 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23282 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23287 if (!TYPE_PTR_P (arg
))
23288 return unify_type_mismatch (explain_p
, parm
, arg
);
23290 /* [temp.deduct.call]
23292 A can be another pointer or pointer to member type that can
23293 be converted to the deduced A via a qualification
23294 conversion (_conv.qual_).
23296 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23297 This will allow for additional cv-qualification of the
23298 pointed-to types if appropriate. */
23300 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23301 /* The derived-to-base conversion only persists through one
23302 level of pointers. */
23303 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23305 return unify (tparms
, targs
, TREE_TYPE (parm
),
23306 TREE_TYPE (arg
), strict
, explain_p
);
23309 case REFERENCE_TYPE
:
23310 if (!TYPE_REF_P (arg
))
23311 return unify_type_mismatch (explain_p
, parm
, arg
);
23312 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23313 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23316 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23317 return unify_type_mismatch (explain_p
, parm
, arg
);
23318 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23319 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23320 return unify_type_mismatch (explain_p
, parm
, arg
);
23321 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23322 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23323 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23324 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23325 TYPE_DOMAIN (arg
), explain_p
);
23326 return unify_success (explain_p
);
23333 case ENUMERAL_TYPE
:
23336 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23337 return unify_type_mismatch (explain_p
, parm
, arg
);
23339 /* We have already checked cv-qualification at the top of the
23341 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23342 return unify_type_mismatch (explain_p
, parm
, arg
);
23344 /* As far as unification is concerned, this wins. Later checks
23345 will invalidate it if necessary. */
23346 return unify_success (explain_p
);
23348 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23349 /* Type INTEGER_CST can come from ordinary constant template args. */
23351 while (CONVERT_EXPR_P (arg
))
23352 arg
= TREE_OPERAND (arg
, 0);
23354 if (TREE_CODE (arg
) != INTEGER_CST
)
23355 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23356 return (tree_int_cst_equal (parm
, arg
)
23357 ? unify_success (explain_p
)
23358 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23362 int i
, len
, argslen
;
23363 int parm_variadic_p
= 0;
23365 if (TREE_CODE (arg
) != TREE_VEC
)
23366 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23368 len
= TREE_VEC_LENGTH (parm
);
23369 argslen
= TREE_VEC_LENGTH (arg
);
23371 /* Check for pack expansions in the parameters. */
23372 for (i
= 0; i
< len
; ++i
)
23374 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23377 /* We can unify against something with a trailing
23379 parm_variadic_p
= 1;
23381 /* [temp.deduct.type]/9: If the template argument list of
23382 P contains a pack expansion that is not the last
23383 template argument, the entire template argument list
23384 is a non-deduced context. */
23385 return unify_success (explain_p
);
23389 /* If we don't have enough arguments to satisfy the parameters
23390 (not counting the pack expression at the end), or we have
23391 too many arguments for a parameter list that doesn't end in
23392 a pack expression, we can't unify. */
23393 if (parm_variadic_p
23394 ? argslen
< len
- parm_variadic_p
23396 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23398 /* Unify all of the parameters that precede the (optional)
23399 pack expression. */
23400 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23402 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23403 TREE_VEC_ELT (parm
, i
),
23404 TREE_VEC_ELT (arg
, i
),
23405 UNIFY_ALLOW_NONE
, explain_p
);
23407 if (parm_variadic_p
)
23408 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23410 /*subr=*/true, explain_p
);
23411 return unify_success (explain_p
);
23416 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23417 return unify_type_mismatch (explain_p
, parm
, arg
);
23419 if (TYPE_PTRMEMFUNC_P (parm
))
23421 if (!TYPE_PTRMEMFUNC_P (arg
))
23422 return unify_type_mismatch (explain_p
, parm
, arg
);
23424 return unify (tparms
, targs
,
23425 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23426 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23427 strict
, explain_p
);
23429 else if (TYPE_PTRMEMFUNC_P (arg
))
23430 return unify_type_mismatch (explain_p
, parm
, arg
);
23432 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23434 tree t
= NULL_TREE
;
23436 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23438 /* First, we try to unify the PARM and ARG directly. */
23439 t
= try_class_unification (tparms
, targs
,
23440 parm
, arg
, explain_p
);
23444 /* Fallback to the special case allowed in
23445 [temp.deduct.call]:
23447 If P is a class, and P has the form
23448 template-id, then A can be a derived class of
23449 the deduced A. Likewise, if P is a pointer to
23450 a class of the form template-id, A can be a
23451 pointer to a derived class pointed to by the
23453 enum template_base_result r
;
23454 r
= get_template_base (tparms
, targs
, parm
, arg
,
23459 /* Don't give the derived diagnostic if we're
23460 already dealing with the same template. */
23462 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23463 && (CLASSTYPE_TI_TEMPLATE (parm
)
23464 == CLASSTYPE_TI_TEMPLATE (arg
)));
23465 return unify_no_common_base (explain_p
&& !same_template
,
23470 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23471 && (CLASSTYPE_TI_TEMPLATE (parm
)
23472 == CLASSTYPE_TI_TEMPLATE (arg
)))
23473 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23474 Then, we should unify `int' and `U'. */
23477 /* There's no chance of unification succeeding. */
23478 return unify_type_mismatch (explain_p
, parm
, arg
);
23480 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23481 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23483 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23484 return unify_type_mismatch (explain_p
, parm
, arg
);
23485 return unify_success (explain_p
);
23488 case FUNCTION_TYPE
:
23490 unsigned int nargs
;
23495 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23496 return unify_type_mismatch (explain_p
, parm
, arg
);
23498 /* CV qualifications for methods can never be deduced, they must
23499 match exactly. We need to check them explicitly here,
23500 because type_unification_real treats them as any other
23501 cv-qualified parameter. */
23502 if (TREE_CODE (parm
) == METHOD_TYPE
23503 && (!check_cv_quals_for_unify
23505 class_of_this_parm (arg
),
23506 class_of_this_parm (parm
))))
23507 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23508 if (TREE_CODE (arg
) == FUNCTION_TYPE
23509 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23510 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23511 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23512 return unify_type_mismatch (explain_p
, parm
, arg
);
23514 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23515 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23517 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23518 args
= XALLOCAVEC (tree
, nargs
);
23519 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23520 a
!= NULL_TREE
&& a
!= void_list_node
;
23521 a
= TREE_CHAIN (a
), ++i
)
23522 args
[i
] = TREE_VALUE (a
);
23525 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23526 args
, nargs
, 1, DEDUCE_EXACT
,
23530 if (flag_noexcept_type
)
23532 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23533 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23534 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23535 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23536 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23537 && uses_template_parms (TREE_PURPOSE (pspec
)))
23538 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23539 TREE_PURPOSE (aspec
),
23540 UNIFY_ALLOW_NONE
, explain_p
);
23541 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23542 return unify_type_mismatch (explain_p
, parm
, arg
);
23549 /* Unify a pointer to member with a pointer to member function, which
23550 deduces the type of the member as a function type. */
23551 if (TYPE_PTRMEMFUNC_P (arg
))
23553 /* Check top-level cv qualifiers */
23554 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23555 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23557 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23558 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23559 UNIFY_ALLOW_NONE
, explain_p
);
23561 /* Determine the type of the function we are unifying against. */
23562 tree fntype
= static_fn_type (arg
);
23564 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23567 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23568 return unify_type_mismatch (explain_p
, parm
, arg
);
23569 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23570 TYPE_OFFSET_BASETYPE (arg
),
23571 UNIFY_ALLOW_NONE
, explain_p
);
23572 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23573 strict
, explain_p
);
23576 if (DECL_TEMPLATE_PARM_P (parm
))
23577 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23578 if (arg
!= scalar_constant_value (parm
))
23579 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23580 return unify_success (explain_p
);
23583 case TEMPLATE_DECL
:
23584 /* Matched cases are handled by the ARG == PARM test above. */
23585 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23588 /* We might get a variable as a non-type template argument in parm if the
23589 corresponding parameter is type-dependent. Make any necessary
23590 adjustments based on whether arg is a reference. */
23591 if (CONSTANT_CLASS_P (arg
))
23592 parm
= fold_non_dependent_expr (parm
, complain
);
23593 else if (REFERENCE_REF_P (arg
))
23595 tree sub
= TREE_OPERAND (arg
, 0);
23597 if (TREE_CODE (sub
) == ADDR_EXPR
)
23598 arg
= TREE_OPERAND (sub
, 0);
23600 /* Now use the normal expression code to check whether they match. */
23603 case TYPE_ARGUMENT_PACK
:
23604 case NONTYPE_ARGUMENT_PACK
:
23605 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23606 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23609 case DECLTYPE_TYPE
:
23610 case UNDERLYING_TYPE
:
23611 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23612 or UNDERLYING_TYPE nodes. */
23613 return unify_success (explain_p
);
23616 /* Unification fails if we hit an error node. */
23617 return unify_invalid (explain_p
);
23620 if (REFERENCE_REF_P (parm
))
23622 bool pexp
= PACK_EXPANSION_P (arg
);
23624 arg
= PACK_EXPANSION_PATTERN (arg
);
23625 if (REFERENCE_REF_P (arg
))
23626 arg
= TREE_OPERAND (arg
, 0);
23628 arg
= make_pack_expansion (arg
, complain
);
23629 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23630 strict
, explain_p
);
23635 /* An unresolved overload is a nondeduced context. */
23636 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23637 return unify_success (explain_p
);
23638 gcc_assert (EXPR_P (parm
)
23639 || COMPOUND_LITERAL_P (parm
)
23640 || TREE_CODE (parm
) == TRAIT_EXPR
);
23642 /* We must be looking at an expression. This can happen with
23646 void foo(S<I>, S<I + 2>);
23650 template<typename T>
23651 void foo(A<T, T{}>);
23653 This is a "non-deduced context":
23657 The non-deduced contexts are:
23659 --A non-type template argument or an array bound in which
23660 a subexpression references a template parameter.
23662 In these cases, we assume deduction succeeded, but don't
23663 actually infer any unifications. */
23665 if (!uses_template_parms (parm
)
23666 && !template_args_equal (parm
, arg
))
23667 return unify_expression_unequal (explain_p
, parm
, arg
);
23669 return unify_success (explain_p
);
23672 #undef RECUR_AND_CHECK_FAILURE
23674 /* Note that DECL can be defined in this translation unit, if
23678 mark_definable (tree decl
)
23681 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23682 FOR_EACH_CLONE (clone
, decl
)
23683 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23686 /* Called if RESULT is explicitly instantiated, or is a member of an
23687 explicitly instantiated class. */
23690 mark_decl_instantiated (tree result
, int extern_p
)
23692 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23694 /* If this entity has already been written out, it's too late to
23695 make any modifications. */
23696 if (TREE_ASM_WRITTEN (result
))
23699 /* For anonymous namespace we don't need to do anything. */
23700 if (decl_anon_ns_mem_p (result
))
23702 gcc_assert (!TREE_PUBLIC (result
));
23706 if (TREE_CODE (result
) != FUNCTION_DECL
)
23707 /* The TREE_PUBLIC flag for function declarations will have been
23708 set correctly by tsubst. */
23709 TREE_PUBLIC (result
) = 1;
23711 /* This might have been set by an earlier implicit instantiation. */
23712 DECL_COMDAT (result
) = 0;
23715 DECL_NOT_REALLY_EXTERN (result
) = 0;
23718 mark_definable (result
);
23719 mark_needed (result
);
23720 /* Always make artificials weak. */
23721 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23722 comdat_linkage (result
);
23723 /* For WIN32 we also want to put explicit instantiations in
23724 linkonce sections. */
23725 else if (TREE_PUBLIC (result
))
23726 maybe_make_one_only (result
);
23727 if (TREE_CODE (result
) == FUNCTION_DECL
23728 && DECL_TEMPLATE_INSTANTIATED (result
))
23729 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23730 since start_preparsed_function wouldn't have if we had an earlier
23731 extern explicit instantiation. */
23732 DECL_EXTERNAL (result
) = 0;
23735 /* If EXTERN_P, then this function will not be emitted -- unless
23736 followed by an explicit instantiation, at which point its linkage
23737 will be adjusted. If !EXTERN_P, then this function will be
23738 emitted here. In neither circumstance do we want
23739 import_export_decl to adjust the linkage. */
23740 DECL_INTERFACE_KNOWN (result
) = 1;
23743 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23744 important template arguments. If any are missing, we check whether
23745 they're important by using error_mark_node for substituting into any
23746 args that were used for partial ordering (the ones between ARGS and END)
23747 and seeing if it bubbles up. */
23750 check_undeduced_parms (tree targs
, tree args
, tree end
)
23752 bool found
= false;
23754 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23755 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23758 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23762 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23763 if (substed
== error_mark_node
)
23769 /* Given two function templates PAT1 and PAT2, return:
23771 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23772 -1 if PAT2 is more specialized than PAT1.
23773 0 if neither is more specialized.
23775 LEN indicates the number of parameters we should consider
23776 (defaulted parameters should not be considered).
23778 The 1998 std underspecified function template partial ordering, and
23779 DR214 addresses the issue. We take pairs of arguments, one from
23780 each of the templates, and deduce them against each other. One of
23781 the templates will be more specialized if all the *other*
23782 template's arguments deduce against its arguments and at least one
23783 of its arguments *does* *not* deduce against the other template's
23784 corresponding argument. Deduction is done as for class templates.
23785 The arguments used in deduction have reference and top level cv
23786 qualifiers removed. Iff both arguments were originally reference
23787 types *and* deduction succeeds in both directions, an lvalue reference
23788 wins against an rvalue reference and otherwise the template
23789 with the more cv-qualified argument wins for that pairing (if
23790 neither is more cv-qualified, they both are equal). Unlike regular
23791 deduction, after all the arguments have been deduced in this way,
23792 we do *not* verify the deduced template argument values can be
23793 substituted into non-deduced contexts.
23795 The logic can be a bit confusing here, because we look at deduce1 and
23796 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23797 can find template arguments for pat1 to make arg1 look like arg2, that
23798 means that arg2 is at least as specialized as arg1. */
23801 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23803 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23804 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23805 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23806 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23807 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23808 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23809 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23810 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23811 tree origs1
, origs2
;
23812 bool lose1
= false;
23813 bool lose2
= false;
23815 /* Remove the this parameter from non-static member functions. If
23816 one is a non-static member function and the other is not a static
23817 member function, remove the first parameter from that function
23818 also. This situation occurs for operator functions where we
23819 locate both a member function (with this pointer) and non-member
23820 operator (with explicit first operand). */
23821 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23823 len
--; /* LEN is the number of significant arguments for DECL1 */
23824 args1
= TREE_CHAIN (args1
);
23825 if (!DECL_STATIC_FUNCTION_P (decl2
))
23826 args2
= TREE_CHAIN (args2
);
23828 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23830 args2
= TREE_CHAIN (args2
);
23831 if (!DECL_STATIC_FUNCTION_P (decl1
))
23834 args1
= TREE_CHAIN (args1
);
23838 /* If only one is a conversion operator, they are unordered. */
23839 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23842 /* Consider the return type for a conversion function */
23843 if (DECL_CONV_FN_P (decl1
))
23845 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23846 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23850 processing_template_decl
++;
23856 /* Stop when an ellipsis is seen. */
23857 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23859 tree arg1
= TREE_VALUE (args1
);
23860 tree arg2
= TREE_VALUE (args2
);
23861 int deduce1
, deduce2
;
23867 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23868 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23870 /* When both arguments are pack expansions, we need only
23871 unify the patterns themselves. */
23872 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23873 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23875 /* This is the last comparison we need to do. */
23879 if (TYPE_REF_P (arg1
))
23881 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23882 arg1
= TREE_TYPE (arg1
);
23883 quals1
= cp_type_quals (arg1
);
23886 if (TYPE_REF_P (arg2
))
23888 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23889 arg2
= TREE_TYPE (arg2
);
23890 quals2
= cp_type_quals (arg2
);
23893 arg1
= TYPE_MAIN_VARIANT (arg1
);
23894 arg2
= TYPE_MAIN_VARIANT (arg2
);
23896 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23898 int i
, len2
= remaining_arguments (args2
);
23899 tree parmvec
= make_tree_vec (1);
23900 tree argvec
= make_tree_vec (len2
);
23903 /* Setup the parameter vector, which contains only ARG1. */
23904 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23906 /* Setup the argument vector, which contains the remaining
23908 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23909 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23911 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23912 argvec
, DEDUCE_EXACT
,
23913 /*subr=*/true, /*explain_p=*/false)
23916 /* We cannot deduce in the other direction, because ARG1 is
23917 a pack expansion but ARG2 is not. */
23920 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23922 int i
, len1
= remaining_arguments (args1
);
23923 tree parmvec
= make_tree_vec (1);
23924 tree argvec
= make_tree_vec (len1
);
23927 /* Setup the parameter vector, which contains only ARG1. */
23928 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23930 /* Setup the argument vector, which contains the remaining
23932 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23933 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23935 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23936 argvec
, DEDUCE_EXACT
,
23937 /*subr=*/true, /*explain_p=*/false)
23940 /* We cannot deduce in the other direction, because ARG2 is
23941 a pack expansion but ARG1 is not.*/
23947 /* The normal case, where neither argument is a pack
23949 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23950 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23952 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23953 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23957 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23958 arg2, then arg2 is not as specialized as arg1. */
23964 /* "If, for a given type, deduction succeeds in both directions
23965 (i.e., the types are identical after the transformations above)
23966 and both P and A were reference types (before being replaced with
23967 the type referred to above):
23968 - if the type from the argument template was an lvalue reference and
23969 the type from the parameter template was not, the argument type is
23970 considered to be more specialized than the other; otherwise,
23971 - if the type from the argument template is more cv-qualified
23972 than the type from the parameter template (as described above),
23973 the argument type is considered to be more specialized than the other;
23975 - neither type is more specialized than the other." */
23977 if (deduce1
&& deduce2
)
23979 if (ref1
&& ref2
&& ref1
!= ref2
)
23986 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23988 if ((quals1
& quals2
) == quals2
)
23990 if ((quals1
& quals2
) == quals1
)
23995 if (lose1
&& lose2
)
23996 /* We've failed to deduce something in either direction.
23997 These must be unordered. */
24000 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24001 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24002 /* We have already processed all of the arguments in our
24003 handing of the pack expansion type. */
24006 args1
= TREE_CHAIN (args1
);
24007 args2
= TREE_CHAIN (args2
);
24010 /* "In most cases, all template parameters must have values in order for
24011 deduction to succeed, but for partial ordering purposes a template
24012 parameter may remain without a value provided it is not used in the
24013 types being used for partial ordering."
24015 Thus, if we are missing any of the targs1 we need to substitute into
24016 origs1, then pat2 is not as specialized as pat1. This can happen when
24017 there is a nondeduced context. */
24018 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24020 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24023 processing_template_decl
--;
24025 /* If both deductions succeed, the partial ordering selects the more
24026 constrained template. */
24027 if (!lose1
&& !lose2
)
24029 int winner
= more_constrained (decl1
, decl2
);
24032 else if (winner
< 0)
24036 /* All things being equal, if the next argument is a pack expansion
24037 for one function but not for the other, prefer the
24038 non-variadic function. FIXME this is bogus; see c++/41958. */
24040 && args1
&& TREE_VALUE (args1
)
24041 && args2
&& TREE_VALUE (args2
))
24043 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24044 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24047 if (lose1
== lose2
)
24055 /* Determine which of two partial specializations of TMPL is more
24058 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24059 to the first partial specialization. The TREE_PURPOSE is the
24060 innermost set of template parameters for the partial
24061 specialization. PAT2 is similar, but for the second template.
24063 Return 1 if the first partial specialization is more specialized;
24064 -1 if the second is more specialized; 0 if neither is more
24067 See [temp.class.order] for information about determining which of
24068 two templates is more specialized. */
24071 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24075 bool any_deductions
= false;
24077 tree tmpl1
= TREE_VALUE (pat1
);
24078 tree tmpl2
= TREE_VALUE (pat2
);
24079 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24080 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24082 /* Just like what happens for functions, if we are ordering between
24083 different template specializations, we may encounter dependent
24084 types in the arguments, and we need our dependency check functions
24085 to behave correctly. */
24086 ++processing_template_decl
;
24087 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24091 any_deductions
= true;
24094 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24098 any_deductions
= true;
24100 --processing_template_decl
;
24102 /* If both deductions succeed, the partial ordering selects the more
24103 constrained template. */
24104 if (!winner
&& any_deductions
)
24105 winner
= more_constrained (tmpl1
, tmpl2
);
24107 /* In the case of a tie where at least one of the templates
24108 has a parameter pack at the end, the template with the most
24109 non-packed parameters wins. */
24112 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24113 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24115 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24116 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24117 int len1
= TREE_VEC_LENGTH (args1
);
24118 int len2
= TREE_VEC_LENGTH (args2
);
24120 /* We don't count the pack expansion at the end. */
24121 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24123 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24128 else if (len1
< len2
)
24135 /* Return the template arguments that will produce the function signature
24136 DECL from the function template FN, with the explicit template
24137 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24138 also match. Return NULL_TREE if no satisfactory arguments could be
24142 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24144 int ntparms
= DECL_NTPARMS (fn
);
24145 tree targs
= make_tree_vec (ntparms
);
24146 tree decl_type
= TREE_TYPE (decl
);
24147 tree decl_arg_types
;
24149 unsigned int nargs
, ix
;
24152 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24154 /* Never do unification on the 'this' parameter. */
24155 decl_arg_types
= skip_artificial_parms_for (decl
,
24156 TYPE_ARG_TYPES (decl_type
));
24158 nargs
= list_length (decl_arg_types
);
24159 args
= XALLOCAVEC (tree
, nargs
);
24160 for (arg
= decl_arg_types
, ix
= 0;
24161 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24162 arg
= TREE_CHAIN (arg
), ++ix
)
24163 args
[ix
] = TREE_VALUE (arg
);
24165 if (fn_type_unification (fn
, explicit_args
, targs
,
24167 (check_rettype
|| DECL_CONV_FN_P (fn
)
24168 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24169 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24170 /*explain_p=*/false,
24172 == error_mark_node
)
24178 /* Return the innermost template arguments that, when applied to a partial
24179 specialization SPEC_TMPL of TMPL, yield the ARGS.
24181 For example, suppose we have:
24183 template <class T, class U> struct S {};
24184 template <class T> struct S<T*, int> {};
24186 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24187 partial specialization and the ARGS will be {double*, int}. The resulting
24188 vector will be {double}, indicating that `T' is bound to `double'. */
24191 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24193 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24195 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24196 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24198 tree innermost_deduced_args
;
24200 innermost_deduced_args
= make_tree_vec (ntparms
);
24201 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24203 deduced_args
= copy_node (args
);
24204 SET_TMPL_ARGS_LEVEL (deduced_args
,
24205 TMPL_ARGS_DEPTH (deduced_args
),
24206 innermost_deduced_args
);
24209 deduced_args
= innermost_deduced_args
;
24211 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24213 if (unify (tparms
, deduced_args
,
24214 INNERMOST_TEMPLATE_ARGS (spec_args
),
24215 INNERMOST_TEMPLATE_ARGS (args
),
24216 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24219 for (i
= 0; i
< ntparms
; ++i
)
24220 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24222 if (!tried_array_deduction
)
24224 try_array_deduction (tparms
, innermost_deduced_args
,
24225 INNERMOST_TEMPLATE_ARGS (spec_args
));
24226 tried_array_deduction
= true;
24227 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24233 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24235 excessive_deduction_depth
= true;
24239 /* Verify that nondeduced template arguments agree with the type
24240 obtained from argument deduction.
24244 struct A { typedef int X; };
24245 template <class T, class U> struct C {};
24246 template <class T> struct C<T, typename T::X> {};
24248 Then with the instantiation `C<A, int>', we can deduce that
24249 `T' is `A' but unify () does not check whether `typename T::X'
24251 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24253 if (spec_args
!= error_mark_node
)
24254 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24255 INNERMOST_TEMPLATE_ARGS (spec_args
),
24256 tmpl
, tf_none
, false, false);
24258 pop_tinst_level ();
24260 if (spec_args
== error_mark_node
24261 /* We only need to check the innermost arguments; the other
24262 arguments will always agree. */
24263 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24264 INNERMOST_TEMPLATE_ARGS (args
)))
24267 /* Now that we have bindings for all of the template arguments,
24268 ensure that the arguments deduced for the template template
24269 parameters have compatible template parameter lists. See the use
24270 of template_template_parm_bindings_ok_p in fn_type_unification
24271 for more information. */
24272 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24275 return deduced_args
;
24278 // Compare two function templates T1 and T2 by deducing bindings
24279 // from one against the other. If both deductions succeed, compare
24280 // constraints to see which is more constrained.
24282 more_specialized_inst (tree t1
, tree t2
)
24287 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24293 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24299 // If both deductions succeed, then one may be more constrained.
24300 if (count
== 2 && fate
== 0)
24301 fate
= more_constrained (t1
, t2
);
24306 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24307 Return the TREE_LIST node with the most specialized template, if
24308 any. If there is no most specialized template, the error_mark_node
24311 Note that this function does not look at, or modify, the
24312 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24313 returned is one of the elements of INSTANTIATIONS, callers may
24314 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24315 and retrieve it from the value returned. */
24318 most_specialized_instantiation (tree templates
)
24322 ++processing_template_decl
;
24325 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24327 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24328 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24333 /* Equally specialized, move to next function. If there
24334 is no next function, nothing's most specialized. */
24335 fn
= TREE_CHAIN (fn
);
24343 /* Now verify that champ is better than everything earlier in the
24344 instantiation list. */
24345 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24346 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24353 processing_template_decl
--;
24356 return error_mark_node
;
24361 /* If DECL is a specialization of some template, return the most
24362 general such template. Otherwise, returns NULL_TREE.
24364 For example, given:
24366 template <class T> struct S { template <class U> void f(U); };
24368 if TMPL is `template <class U> void S<int>::f(U)' this will return
24369 the full template. This function will not trace past partial
24370 specializations, however. For example, given in addition:
24372 template <class T> struct S<T*> { template <class U> void f(U); };
24374 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24375 `template <class T> template <class U> S<T*>::f(U)'. */
24378 most_general_template (tree decl
)
24380 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24382 if (tree tinfo
= get_template_info (decl
))
24383 decl
= TI_TEMPLATE (tinfo
);
24384 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24385 template friend, or a FIELD_DECL for a capture pack. */
24386 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24390 /* Look for more and more general templates. */
24391 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24393 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24394 (See cp-tree.h for details.) */
24395 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24398 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24399 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24400 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24403 /* Stop if we run into an explicitly specialized class template. */
24404 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24405 && DECL_CONTEXT (decl
)
24406 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24409 decl
= DECL_TI_TEMPLATE (decl
);
24415 /* Return the most specialized of the template partial specializations
24416 which can produce TARGET, a specialization of some class or variable
24417 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24418 a TEMPLATE_DECL node corresponding to the partial specialization, while
24419 the TREE_PURPOSE is the set of template arguments that must be
24420 substituted into the template pattern in order to generate TARGET.
24422 If the choice of partial specialization is ambiguous, a diagnostic
24423 is issued, and the error_mark_node is returned. If there are no
24424 partial specializations matching TARGET, then NULL_TREE is
24425 returned, indicating that the primary template should be used. */
24428 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24430 tree list
= NULL_TREE
;
24435 tree outer_args
= NULL_TREE
;
24438 if (TYPE_P (target
))
24440 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24441 tmpl
= TI_TEMPLATE (tinfo
);
24442 args
= TI_ARGS (tinfo
);
24444 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24446 tmpl
= TREE_OPERAND (target
, 0);
24447 args
= TREE_OPERAND (target
, 1);
24449 else if (VAR_P (target
))
24451 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24452 tmpl
= TI_TEMPLATE (tinfo
);
24453 args
= TI_ARGS (tinfo
);
24456 gcc_unreachable ();
24458 tree main_tmpl
= most_general_template (tmpl
);
24460 /* For determining which partial specialization to use, only the
24461 innermost args are interesting. */
24462 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24464 outer_args
= strip_innermost_template_args (args
, 1);
24465 args
= INNERMOST_TEMPLATE_ARGS (args
);
24468 /* The caller hasn't called push_to_top_level yet, but we need
24469 get_partial_spec_bindings to be done in non-template context so that we'll
24470 fully resolve everything. */
24471 processing_template_decl_sentinel ptds
;
24473 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24476 tree spec_tmpl
= TREE_VALUE (t
);
24480 /* Substitute in the template args from the enclosing class. */
24481 ++processing_template_decl
;
24482 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24483 --processing_template_decl
;
24486 if (spec_tmpl
== error_mark_node
)
24487 return error_mark_node
;
24489 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24493 spec_args
= add_to_template_args (outer_args
, spec_args
);
24495 /* Keep the candidate only if the constraints are satisfied,
24496 or if we're not compiling with concepts. */
24498 || constraints_satisfied_p (spec_tmpl
, spec_args
))
24500 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
24501 TREE_TYPE (list
) = TREE_TYPE (t
);
24509 ambiguous_p
= false;
24512 t
= TREE_CHAIN (t
);
24513 for (; t
; t
= TREE_CHAIN (t
))
24515 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24522 t
= TREE_CHAIN (t
);
24525 ambiguous_p
= true;
24534 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24536 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24539 ambiguous_p
= true;
24547 char *spaces
= NULL
;
24548 if (!(complain
& tf_error
))
24549 return error_mark_node
;
24550 if (TYPE_P (target
))
24551 error ("ambiguous template instantiation for %q#T", target
);
24553 error ("ambiguous template instantiation for %q#D", target
);
24554 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24555 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24557 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24558 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24559 "%s %#qS", spaces
? spaces
: str
, subst
);
24560 spaces
= spaces
? spaces
: get_spaces (str
);
24563 return error_mark_node
;
24569 /* Explicitly instantiate DECL. */
24572 do_decl_instantiation (tree decl
, tree storage
)
24574 tree result
= NULL_TREE
;
24577 if (!decl
|| decl
== error_mark_node
)
24578 /* An error occurred, for which grokdeclarator has already issued
24579 an appropriate message. */
24581 else if (! DECL_LANG_SPECIFIC (decl
))
24583 error ("explicit instantiation of non-template %q#D", decl
);
24586 else if (DECL_DECLARED_CONCEPT_P (decl
))
24589 error ("explicit instantiation of variable concept %q#D", decl
);
24591 error ("explicit instantiation of function concept %q#D", decl
);
24595 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24596 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24598 if (VAR_P (decl
) && !var_templ
)
24600 /* There is an asymmetry here in the way VAR_DECLs and
24601 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24602 the latter, the DECL we get back will be marked as a
24603 template instantiation, and the appropriate
24604 DECL_TEMPLATE_INFO will be set up. This does not happen for
24605 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24606 should handle VAR_DECLs as it currently handles
24608 if (!DECL_CLASS_SCOPE_P (decl
))
24610 error ("%qD is not a static data member of a class template", decl
);
24613 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24614 if (!result
|| !VAR_P (result
))
24616 error ("no matching template for %qD found", decl
);
24619 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24621 error ("type %qT for explicit instantiation %qD does not match "
24622 "declared type %qT", TREE_TYPE (result
), decl
,
24627 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24629 error ("explicit instantiation of %q#D", decl
);
24635 /* Check for various error cases. Note that if the explicit
24636 instantiation is valid the RESULT will currently be marked as an
24637 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24638 until we get here. */
24640 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24642 /* DR 259 [temp.spec].
24644 Both an explicit instantiation and a declaration of an explicit
24645 specialization shall not appear in a program unless the explicit
24646 instantiation follows a declaration of the explicit specialization.
24648 For a given set of template parameters, if an explicit
24649 instantiation of a template appears after a declaration of an
24650 explicit specialization for that template, the explicit
24651 instantiation has no effect. */
24654 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24658 No program shall explicitly instantiate any template more
24661 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24662 the first instantiation was `extern' and the second is not,
24663 and EXTERN_P for the opposite case. */
24664 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24665 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24666 /* If an "extern" explicit instantiation follows an ordinary
24667 explicit instantiation, the template is instantiated. */
24671 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24673 error ("no matching template for %qD found", result
);
24676 else if (!DECL_TEMPLATE_INFO (result
))
24678 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24682 if (storage
== NULL_TREE
)
24684 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24686 if (cxx_dialect
== cxx98
)
24687 pedwarn (input_location
, OPT_Wpedantic
,
24688 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24693 error ("storage class %qD applied to template instantiation", storage
);
24695 check_explicit_instantiation_namespace (result
);
24696 mark_decl_instantiated (result
, extern_p
);
24698 instantiate_decl (result
, /*defer_ok=*/true,
24699 /*expl_inst_class_mem_p=*/false);
24703 mark_class_instantiated (tree t
, int extern_p
)
24705 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24706 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24707 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24708 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24711 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24712 rest_of_type_compilation (t
, 1);
24716 /* Called from do_type_instantiation through binding_table_foreach to
24717 do recursive instantiation for the type bound in ENTRY. */
24719 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24721 tree storage
= *(tree
*) data
;
24723 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24724 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24725 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24726 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24729 /* Perform an explicit instantiation of template class T. STORAGE, if
24730 non-null, is the RID for extern, inline or static. COMPLAIN is
24731 nonzero if this is called from the parser, zero if called recursively,
24732 since the standard is unclear (as detailed below). */
24735 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24740 int previous_instantiation_extern_p
= 0;
24742 if (TREE_CODE (t
) == TYPE_DECL
)
24745 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24748 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24750 error ("explicit instantiation of non-class template %qD", tmpl
);
24752 error ("explicit instantiation of non-template type %qT", t
);
24758 if (!COMPLETE_TYPE_P (t
))
24760 if (complain
& tf_error
)
24761 error ("explicit instantiation of %q#T before definition of template",
24766 if (storage
!= NULL_TREE
)
24768 if (storage
== ridpointers
[(int) RID_EXTERN
])
24770 if (cxx_dialect
== cxx98
)
24771 pedwarn (input_location
, OPT_Wpedantic
,
24772 "ISO C++ 1998 forbids the use of %<extern%> on "
24773 "explicit instantiations");
24776 pedwarn (input_location
, OPT_Wpedantic
,
24777 "ISO C++ forbids the use of %qE"
24778 " on explicit instantiations", storage
);
24780 if (storage
== ridpointers
[(int) RID_INLINE
])
24782 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24784 else if (storage
== ridpointers
[(int) RID_STATIC
])
24788 error ("storage class %qD applied to template instantiation",
24794 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24796 /* DR 259 [temp.spec].
24798 Both an explicit instantiation and a declaration of an explicit
24799 specialization shall not appear in a program unless the explicit
24800 instantiation follows a declaration of the explicit specialization.
24802 For a given set of template parameters, if an explicit
24803 instantiation of a template appears after a declaration of an
24804 explicit specialization for that template, the explicit
24805 instantiation has no effect. */
24808 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24812 No program shall explicitly instantiate any template more
24815 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24816 instantiation was `extern'. If EXTERN_P then the second is.
24817 These cases are OK. */
24818 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24820 if (!previous_instantiation_extern_p
&& !extern_p
24821 && (complain
& tf_error
))
24822 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24824 /* If we've already instantiated the template, just return now. */
24825 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24829 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24830 mark_class_instantiated (t
, extern_p
);
24835 /* In contrast to implicit instantiation, where only the
24836 declarations, and not the definitions, of members are
24837 instantiated, we have here:
24841 The explicit instantiation of a class template specialization
24842 implies the instantiation of all of its members not
24843 previously explicitly specialized in the translation unit
24844 containing the explicit instantiation.
24846 Of course, we can't instantiate member template classes, since we
24847 don't have any arguments for them. Note that the standard is
24848 unclear on whether the instantiation of the members are
24849 *explicit* instantiations or not. However, the most natural
24850 interpretation is that it should be an explicit
24852 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24854 || (TREE_CODE (fld
) == FUNCTION_DECL
24856 && user_provided_p (fld
)))
24857 && DECL_TEMPLATE_INSTANTIATION (fld
))
24859 mark_decl_instantiated (fld
, extern_p
);
24861 instantiate_decl (fld
, /*defer_ok=*/true,
24862 /*expl_inst_class_mem_p=*/true);
24865 if (CLASSTYPE_NESTED_UTDS (t
))
24866 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24867 bt_instantiate_type_proc
, &storage
);
24870 /* Given a function DECL, which is a specialization of TMPL, modify
24871 DECL to be a re-instantiation of TMPL with the same template
24872 arguments. TMPL should be the template into which tsubst'ing
24873 should occur for DECL, not the most general template.
24875 One reason for doing this is a scenario like this:
24878 void f(const T&, int i);
24880 void g() { f(3, 7); }
24883 void f(const T& t, const int i) { }
24885 Note that when the template is first instantiated, with
24886 instantiate_template, the resulting DECL will have no name for the
24887 first parameter, and the wrong type for the second. So, when we go
24888 to instantiate the DECL, we regenerate it. */
24891 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24893 /* The arguments used to instantiate DECL, from the most general
24897 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24899 /* Make sure that we can see identifiers, and compute access
24901 push_access_scope (decl
);
24903 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24911 args_depth
= TMPL_ARGS_DEPTH (args
);
24912 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24913 if (args_depth
> parms_depth
)
24914 args
= get_innermost_template_args (args
, parms_depth
);
24916 /* Instantiate a dynamic exception-specification. noexcept will be
24918 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24919 if (TREE_VALUE (raises
))
24921 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24922 args
, tf_error
, NULL_TREE
,
24923 /*defer_ok*/false);
24924 if (specs
&& specs
!= error_mark_node
)
24925 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24929 /* Merge parameter declarations. */
24930 decl_parm
= skip_artificial_parms_for (decl
,
24931 DECL_ARGUMENTS (decl
));
24933 = skip_artificial_parms_for (code_pattern
,
24934 DECL_ARGUMENTS (code_pattern
));
24935 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24940 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24941 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24942 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24944 parm_type
= type_decays_to (parm_type
);
24945 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24946 TREE_TYPE (decl_parm
) = parm_type
;
24947 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24948 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24950 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24951 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24953 decl_parm
= DECL_CHAIN (decl_parm
);
24954 pattern_parm
= DECL_CHAIN (pattern_parm
);
24956 /* Merge any parameters that match with the function parameter
24958 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24961 tree expanded_types
;
24962 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24963 the parameters in this function parameter pack. */
24964 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24965 args
, tf_error
, NULL_TREE
);
24966 len
= TREE_VEC_LENGTH (expanded_types
);
24967 for (i
= 0; i
< len
; i
++)
24972 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24973 /* Rename the parameter to include the index. */
24974 DECL_NAME (decl_parm
) =
24975 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24976 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24977 parm_type
= type_decays_to (parm_type
);
24978 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24979 TREE_TYPE (decl_parm
) = parm_type
;
24980 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24981 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24983 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24984 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24986 decl_parm
= DECL_CHAIN (decl_parm
);
24989 /* Merge additional specifiers from the CODE_PATTERN. */
24990 if (DECL_DECLARED_INLINE_P (code_pattern
)
24991 && !DECL_DECLARED_INLINE_P (decl
))
24992 DECL_DECLARED_INLINE_P (decl
) = 1;
24994 maybe_instantiate_noexcept (decl
, tf_error
);
24996 else if (VAR_P (decl
))
24998 start_lambda_scope (decl
);
24999 DECL_INITIAL (decl
) =
25000 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25001 tf_error
, DECL_TI_TEMPLATE (decl
));
25002 finish_lambda_scope ();
25003 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25004 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25005 tf_error
, DECL_TI_TEMPLATE (decl
));
25008 gcc_unreachable ();
25010 pop_access_scope (decl
);
25013 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25014 substituted to get DECL. */
25017 template_for_substitution (tree decl
)
25019 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25021 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25022 for the instantiation. This is not always the most general
25023 template. Consider, for example:
25026 struct S { template <class U> void f();
25027 template <> void f<int>(); };
25029 and an instantiation of S<double>::f<int>. We want TD to be the
25030 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25031 while (/* An instantiation cannot have a definition, so we need a
25032 more general template. */
25033 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25034 /* We must also deal with friend templates. Given:
25036 template <class T> struct S {
25037 template <class U> friend void f() {};
25040 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25041 so far as the language is concerned, but that's still
25042 where we get the pattern for the instantiation from. On
25043 other hand, if the definition comes outside the class, say:
25045 template <class T> struct S {
25046 template <class U> friend void f();
25048 template <class U> friend void f() {}
25050 we don't need to look any further. That's what the check for
25051 DECL_INITIAL is for. */
25052 || (TREE_CODE (decl
) == FUNCTION_DECL
25053 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25054 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25056 /* The present template, TD, should not be a definition. If it
25057 were a definition, we should be using it! Note that we
25058 cannot restructure the loop to just keep going until we find
25059 a template with a definition, since that might go too far if
25060 a specialization was declared, but not defined. */
25062 /* Fetch the more general template. */
25063 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25069 /* Returns true if we need to instantiate this template instance even if we
25070 know we aren't going to emit it. */
25073 always_instantiate_p (tree decl
)
25075 /* We always instantiate inline functions so that we can inline them. An
25076 explicit instantiation declaration prohibits implicit instantiation of
25077 non-inline functions. With high levels of optimization, we would
25078 normally inline non-inline functions -- but we're not allowed to do
25079 that for "extern template" functions. Therefore, we check
25080 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25081 return ((TREE_CODE (decl
) == FUNCTION_DECL
25082 && (DECL_DECLARED_INLINE_P (decl
)
25083 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25084 /* And we need to instantiate static data members so that
25085 their initializers are available in integral constant
25088 && decl_maybe_constant_var_p (decl
)));
25091 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25092 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25093 error, true otherwise. */
25096 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25098 tree fntype
, spec
, noex
;
25100 /* Don't instantiate a noexcept-specification from template context. */
25101 if (processing_template_decl
25102 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25105 if (DECL_MAYBE_DELETED (fn
))
25107 if (fn
== current_function_decl
)
25108 /* We're in start_preparsed_function, keep going. */
25112 synthesize_method (fn
);
25114 return !DECL_MAYBE_DELETED (fn
);
25117 fntype
= TREE_TYPE (fn
);
25118 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25120 if (!spec
|| !TREE_PURPOSE (spec
))
25123 noex
= TREE_PURPOSE (spec
);
25124 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25125 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25128 tree orig_fn
= NULL_TREE
;
25129 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25130 its FUNCTION_DECL for the rest of this function -- push_access_scope
25131 doesn't accept TEMPLATE_DECLs. */
25132 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25135 fn
= DECL_TEMPLATE_RESULT (fn
);
25138 if (DECL_CLONED_FUNCTION_P (fn
))
25140 tree prime
= DECL_CLONED_FUNCTION (fn
);
25141 if (!maybe_instantiate_noexcept (prime
, complain
))
25143 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25145 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25147 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25148 bool added
= false;
25149 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25151 spec
= get_defaulted_eh_spec (fn
, complain
);
25152 if (spec
== error_mark_node
)
25153 /* This might have failed because of an unparsed DMI, so
25154 let's try again later. */
25157 else if (!(added
= !fns
->add (fn
)))
25159 /* If hash_set::add returns true, the element was already there. */
25160 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25161 DECL_SOURCE_LOCATION (fn
));
25163 "exception specification of %qD depends on itself",
25165 spec
= noexcept_false_spec
;
25167 else if (push_tinst_level (fn
))
25169 push_to_top_level ();
25170 push_access_scope (fn
);
25171 push_deferring_access_checks (dk_no_deferred
);
25172 input_location
= DECL_SOURCE_LOCATION (fn
);
25174 /* If needed, set current_class_ptr for the benefit of
25175 tsubst_copy/PARM_DECL. */
25176 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25177 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25179 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25180 current_class_ptr
= NULL_TREE
;
25181 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25182 current_class_ptr
= this_parm
;
25185 /* If this function is represented by a TEMPLATE_DECL, then
25186 the deferred noexcept-specification might still contain
25187 dependent types, even after substitution. And we need the
25188 dependency check functions to work in build_noexcept_spec. */
25190 ++processing_template_decl
;
25192 /* Do deferred instantiation of the noexcept-specifier. */
25193 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25194 DEFERRED_NOEXCEPT_ARGS (noex
),
25195 tf_warning_or_error
, fn
,
25196 /*function_p=*/false,
25199 /* Build up the noexcept-specification. */
25200 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25203 --processing_template_decl
;
25205 pop_deferring_access_checks ();
25206 pop_access_scope (fn
);
25207 pop_tinst_level ();
25208 pop_from_top_level ();
25211 spec
= noexcept_false_spec
;
25217 if (spec
== error_mark_node
)
25219 /* This failed with a hard error, so let's go with false. */
25220 gcc_assert (seen_error ());
25221 spec
= noexcept_false_spec
;
25224 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25226 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25231 /* We're starting to process the function INST, an instantiation of PATTERN;
25232 add their parameters to local_specializations. */
25235 register_parameter_specializations (tree pattern
, tree inst
)
25237 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25238 tree spec_parm
= DECL_ARGUMENTS (inst
);
25239 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25241 register_local_specialization (spec_parm
, tmpl_parm
);
25242 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25243 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25245 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25247 if (!DECL_PACK_P (tmpl_parm
)
25248 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25250 register_local_specialization (spec_parm
, tmpl_parm
);
25251 spec_parm
= DECL_CHAIN (spec_parm
);
25255 /* Register the (value) argument pack as a specialization of
25256 TMPL_PARM, then move on. */
25257 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25258 register_local_specialization (argpack
, tmpl_parm
);
25261 gcc_assert (!spec_parm
);
25264 /* Produce the definition of D, a _DECL generated from a template. If
25265 DEFER_OK is true, then we don't have to actually do the
25266 instantiation now; we just have to do it sometime. Normally it is
25267 an error if this is an explicit instantiation but D is undefined.
25268 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25269 instantiated class template. */
25272 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25274 tree tmpl
= DECL_TI_TEMPLATE (d
);
25281 bool pattern_defined
;
25282 location_t saved_loc
= input_location
;
25283 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25284 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25288 /* This function should only be used to instantiate templates for
25289 functions and static member variables. */
25290 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25292 /* A concept is never instantiated. */
25293 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25295 /* Variables are never deferred; if instantiation is required, they
25296 are instantiated right away. That allows for better code in the
25297 case that an expression refers to the value of the variable --
25298 if the variable has a constant value the referring expression can
25299 take advantage of that fact. */
25303 /* Don't instantiate cloned functions. Instead, instantiate the
25304 functions they cloned. */
25305 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25306 d
= DECL_CLONED_FUNCTION (d
);
25308 if (DECL_TEMPLATE_INSTANTIATED (d
)
25309 || (TREE_CODE (d
) == FUNCTION_DECL
25310 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25311 || DECL_TEMPLATE_SPECIALIZATION (d
))
25312 /* D has already been instantiated or explicitly specialized, so
25313 there's nothing for us to do here.
25315 It might seem reasonable to check whether or not D is an explicit
25316 instantiation, and, if so, stop here. But when an explicit
25317 instantiation is deferred until the end of the compilation,
25318 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25319 the instantiation. */
25322 /* Check to see whether we know that this template will be
25323 instantiated in some other file, as with "extern template"
25325 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25327 /* In general, we do not instantiate such templates. */
25328 if (external_p
&& !always_instantiate_p (d
))
25331 gen_tmpl
= most_general_template (tmpl
);
25332 gen_args
= DECL_TI_ARGS (d
);
25334 /* We should already have the extra args. */
25335 gcc_checking_assert (tmpl
== gen_tmpl
25336 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25337 == TMPL_ARGS_DEPTH (gen_args
)));
25338 /* And what's in the hash table should match D. */
25339 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25341 || spec
== NULL_TREE
);
25343 /* This needs to happen before any tsubsting. */
25344 if (! push_tinst_level (d
))
25347 timevar_push (TV_TEMPLATE_INST
);
25349 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25350 for the instantiation. */
25351 td
= template_for_substitution (d
);
25356 /* Look up an explicit specialization, if any. */
25357 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25358 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25359 if (elt
&& elt
!= error_mark_node
)
25361 td
= TREE_VALUE (elt
);
25362 args
= TREE_PURPOSE (elt
);
25366 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25368 /* We should never be trying to instantiate a member of a class
25369 template or partial specialization. */
25370 gcc_assert (d
!= code_pattern
);
25372 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25373 || DECL_TEMPLATE_SPECIALIZATION (td
))
25374 /* In the case of a friend template whose definition is provided
25375 outside the class, we may have too many arguments. Drop the
25376 ones we don't need. The same is true for specializations. */
25377 args
= get_innermost_template_args
25378 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25380 if (TREE_CODE (d
) == FUNCTION_DECL
)
25382 deleted_p
= DECL_DELETED_FN (code_pattern
);
25383 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25384 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25385 || DECL_DEFAULTED_FN (code_pattern
)
25391 if (DECL_CLASS_SCOPE_P (code_pattern
))
25392 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25394 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25397 /* We may be in the middle of deferred access check. Disable it now. */
25398 push_deferring_access_checks (dk_no_deferred
);
25400 /* Unless an explicit instantiation directive has already determined
25401 the linkage of D, remember that a definition is available for
25403 if (pattern_defined
25404 && !DECL_INTERFACE_KNOWN (d
)
25405 && !DECL_NOT_REALLY_EXTERN (d
))
25406 mark_definable (d
);
25408 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25409 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25410 input_location
= DECL_SOURCE_LOCATION (d
);
25412 /* If D is a member of an explicitly instantiated class template,
25413 and no definition is available, treat it like an implicit
25415 if (!pattern_defined
&& expl_inst_class_mem_p
25416 && DECL_EXPLICIT_INSTANTIATION (d
))
25418 /* Leave linkage flags alone on instantiations with anonymous
25420 if (TREE_PUBLIC (d
))
25422 DECL_NOT_REALLY_EXTERN (d
) = 0;
25423 DECL_INTERFACE_KNOWN (d
) = 0;
25425 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25428 /* Defer all other templates, unless we have been explicitly
25429 forbidden from doing so. */
25430 if (/* If there is no definition, we cannot instantiate the
25433 /* If it's OK to postpone instantiation, do so. */
25435 /* If this is a static data member that will be defined
25436 elsewhere, we don't want to instantiate the entire data
25437 member, but we do want to instantiate the initializer so that
25438 we can substitute that elsewhere. */
25439 || (external_p
&& VAR_P (d
))
25440 /* Handle here a deleted function too, avoid generating
25441 its body (c++/61080). */
25444 /* The definition of the static data member is now required so
25445 we must substitute the initializer. */
25447 && !DECL_INITIAL (d
)
25448 && DECL_INITIAL (code_pattern
))
25452 bool const_init
= false;
25453 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25455 ns
= decl_namespace_context (d
);
25456 push_nested_namespace (ns
);
25458 push_nested_class (DECL_CONTEXT (d
));
25459 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25461 tf_warning_or_error
, NULL_TREE
,
25462 /*integral_constant_expression_p=*/false);
25463 /* If instantiating the initializer involved instantiating this
25464 again, don't call cp_finish_decl twice. */
25465 if (!DECL_INITIAL (d
))
25467 /* Make sure the initializer is still constant, in case of
25468 circular dependency (template/instantiate6.C). */
25470 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25471 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25472 /*asmspec_tree=*/NULL_TREE
,
25473 LOOKUP_ONLYCONVERTING
);
25476 pop_nested_class ();
25477 pop_nested_namespace (ns
);
25480 /* We restore the source position here because it's used by
25481 add_pending_template. */
25482 input_location
= saved_loc
;
25484 if (at_eof
&& !pattern_defined
25485 && DECL_EXPLICIT_INSTANTIATION (d
)
25486 && DECL_NOT_REALLY_EXTERN (d
))
25489 The definition of a non-exported function template, a
25490 non-exported member function template, or a non-exported
25491 member function or static data member of a class template
25492 shall be present in every translation unit in which it is
25493 explicitly instantiated. */
25494 permerror (input_location
, "explicit instantiation of %qD "
25495 "but no definition available", d
);
25497 /* If we're in unevaluated context, we just wanted to get the
25498 constant value; this isn't an odr use, so don't queue
25499 a full instantiation. */
25500 if (cp_unevaluated_operand
!= 0)
25502 /* ??? Historically, we have instantiated inline functions, even
25503 when marked as "extern template". */
25504 if (!(external_p
&& VAR_P (d
)))
25505 add_pending_template (d
);
25509 bool push_to_top
, nested
;
25511 fn_context
= decl_function_context (d
);
25512 if (LAMBDA_FUNCTION_P (d
))
25513 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25514 fn_context
= NULL_TREE
;
25515 nested
= current_function_decl
!= NULL_TREE
;
25516 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25518 vec
<tree
> omp_privatization_save
;
25520 save_omp_privatization_clauses (omp_privatization_save
);
25523 push_to_top_level ();
25526 gcc_assert (!processing_template_decl
);
25527 push_function_context ();
25528 cp_unevaluated_operand
= 0;
25529 c_inhibit_evaluation_warnings
= 0;
25534 /* The variable might be a lambda's extra scope, and that
25535 lambda's visibility depends on D's. */
25536 maybe_commonize_var (d
);
25537 determine_visibility (d
);
25540 /* Mark D as instantiated so that recursive calls to
25541 instantiate_decl do not try to instantiate it again. */
25542 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25544 /* Regenerate the declaration in case the template has been modified
25545 by a subsequent redeclaration. */
25546 regenerate_decl_from_template (d
, td
, args
);
25548 /* We already set the file and line above. Reset them now in case
25549 they changed as a result of calling regenerate_decl_from_template. */
25550 input_location
= DECL_SOURCE_LOCATION (d
);
25555 bool const_init
= false;
25557 /* Clear out DECL_RTL; whatever was there before may not be right
25558 since we've reset the type of the declaration. */
25559 SET_DECL_RTL (d
, NULL
);
25560 DECL_IN_AGGR_P (d
) = 0;
25562 /* The initializer is placed in DECL_INITIAL by
25563 regenerate_decl_from_template so we don't need to
25564 push/pop_access_scope again here. Pull it out so that
25565 cp_finish_decl can process it. */
25566 init
= DECL_INITIAL (d
);
25567 DECL_INITIAL (d
) = NULL_TREE
;
25568 DECL_INITIALIZED_P (d
) = 0;
25570 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25571 initializer. That function will defer actual emission until
25572 we have a chance to determine linkage. */
25573 DECL_EXTERNAL (d
) = 0;
25575 /* Enter the scope of D so that access-checking works correctly. */
25576 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25578 push_nested_class (DECL_CONTEXT (d
));
25580 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25581 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25582 ? LOOKUP_CONSTINIT
: 0);
25583 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25586 pop_nested_class ();
25588 if (variable_template_p (gen_tmpl
))
25589 note_variable_template_instantiation (d
);
25591 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25592 synthesize_method (d
);
25593 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25595 /* Set up the list of local specializations. */
25596 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25597 tree block
= NULL_TREE
;
25599 /* Set up context. */
25600 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25601 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25602 block
= push_stmt_list ();
25604 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25606 /* Some typedefs referenced from within the template code need to be
25607 access checked at template instantiation time, i.e now. These
25608 types were added to the template at parsing time. Let's get those
25609 and perform the access checks then. */
25610 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
25613 /* Create substitution entries for the parameters. */
25614 register_parameter_specializations (code_pattern
, d
);
25616 /* Substitute into the body of the function. */
25617 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25618 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25619 tf_warning_or_error
, tmpl
);
25622 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25623 tf_warning_or_error
, tmpl
,
25624 /*integral_constant_expression_p=*/false);
25626 /* Set the current input_location to the end of the function
25627 so that finish_function knows where we are. */
25629 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25631 /* Remember if we saw an infinite loop in the template. */
25632 current_function_infinite_loop
25633 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25636 /* Finish the function. */
25637 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25638 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25639 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25642 d
= finish_function (/*inline_p=*/false);
25643 expand_or_defer_fn (d
);
25646 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25647 cp_check_omp_declare_reduction (d
);
25650 /* We're not deferring instantiation any more. */
25651 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25654 pop_from_top_level ();
25656 pop_function_context ();
25659 restore_omp_privatization_clauses (omp_privatization_save
);
25662 pop_deferring_access_checks ();
25663 timevar_pop (TV_TEMPLATE_INST
);
25664 pop_tinst_level ();
25665 input_location
= saved_loc
;
25666 cp_unevaluated_operand
= saved_unevaluated_operand
;
25667 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25672 /* Run through the list of templates that we wish we could
25673 instantiate, and instantiate any we can. RETRIES is the
25674 number of times we retry pending template instantiation. */
25677 instantiate_pending_templates (int retries
)
25680 location_t saved_loc
= input_location
;
25682 /* Instantiating templates may trigger vtable generation. This in turn
25683 may require further template instantiations. We place a limit here
25684 to avoid infinite loop. */
25685 if (pending_templates
&& retries
>= max_tinst_depth
)
25687 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25689 fatal_error (input_location
,
25690 "template instantiation depth exceeds maximum of %d"
25691 " instantiating %q+D, possibly from virtual table generation"
25692 " (use %<-ftemplate-depth=%> to increase the maximum)",
25693 max_tinst_depth
, decl
);
25694 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25695 /* Pretend that we defined it. */
25696 DECL_INITIAL (decl
) = error_mark_node
;
25702 struct pending_template
**t
= &pending_templates
;
25703 struct pending_template
*last
= NULL
;
25707 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25708 bool complete
= false;
25710 if (TYPE_P (instantiation
))
25712 if (!COMPLETE_TYPE_P (instantiation
))
25714 instantiate_class_template (instantiation
);
25715 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25716 for (tree fld
= TYPE_FIELDS (instantiation
);
25717 fld
; fld
= TREE_CHAIN (fld
))
25719 || (TREE_CODE (fld
) == FUNCTION_DECL
25720 && !DECL_ARTIFICIAL (fld
)))
25721 && DECL_TEMPLATE_INSTANTIATION (fld
))
25722 instantiate_decl (fld
,
25723 /*defer_ok=*/false,
25724 /*expl_inst_class_mem_p=*/false);
25726 if (COMPLETE_TYPE_P (instantiation
))
25730 complete
= COMPLETE_TYPE_P (instantiation
);
25734 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25735 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25738 = instantiate_decl (instantiation
,
25739 /*defer_ok=*/false,
25740 /*expl_inst_class_mem_p=*/false);
25741 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25745 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25746 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25751 /* If INSTANTIATION has been instantiated, then we don't
25752 need to consider it again in the future. */
25753 struct pending_template
*drop
= *t
;
25755 set_refcount_ptr (drop
->tinst
);
25756 pending_template_freelist ().free (drop
);
25764 set_refcount_ptr (current_tinst_level
);
25766 last_pending_template
= last
;
25768 while (reconsider
);
25770 input_location
= saved_loc
;
25773 /* Substitute ARGVEC into T, which is a list of initializers for
25774 either base class or a non-static data member. The TREE_PURPOSEs
25775 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25776 instantiate_decl. */
25779 tsubst_initializer_list (tree t
, tree argvec
)
25781 tree inits
= NULL_TREE
;
25782 tree target_ctor
= error_mark_node
;
25784 for (; t
; t
= TREE_CHAIN (t
))
25788 tree expanded_bases
= NULL_TREE
;
25789 tree expanded_arguments
= NULL_TREE
;
25792 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25797 /* Expand the base class expansion type into separate base
25799 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25800 tf_warning_or_error
,
25802 if (expanded_bases
== error_mark_node
)
25805 /* We'll be building separate TREE_LISTs of arguments for
25807 len
= TREE_VEC_LENGTH (expanded_bases
);
25808 expanded_arguments
= make_tree_vec (len
);
25809 for (i
= 0; i
< len
; i
++)
25810 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25812 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25813 expand each argument in the TREE_VALUE of t. */
25814 expr
= make_node (EXPR_PACK_EXPANSION
);
25815 PACK_EXPANSION_LOCAL_P (expr
) = true;
25816 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25817 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25819 if (TREE_VALUE (t
) == void_type_node
)
25820 /* VOID_TYPE_NODE is used to indicate
25821 value-initialization. */
25823 for (i
= 0; i
< len
; i
++)
25824 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25828 /* Substitute parameter packs into each argument in the
25830 in_base_initializer
= 1;
25831 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25833 tree expanded_exprs
;
25835 /* Expand the argument. */
25836 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25838 = tsubst_pack_expansion (expr
, argvec
,
25839 tf_warning_or_error
,
25841 if (expanded_exprs
== error_mark_node
)
25844 /* Prepend each of the expanded expressions to the
25845 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25846 for (i
= 0; i
< len
; i
++)
25848 TREE_VEC_ELT (expanded_arguments
, i
) =
25849 tree_cons (NULL_TREE
,
25850 TREE_VEC_ELT (expanded_exprs
, i
),
25851 TREE_VEC_ELT (expanded_arguments
, i
));
25854 in_base_initializer
= 0;
25856 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25857 since we built them backwards. */
25858 for (i
= 0; i
< len
; i
++)
25860 TREE_VEC_ELT (expanded_arguments
, i
) =
25861 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25866 for (i
= 0; i
< len
; ++i
)
25868 if (expanded_bases
)
25870 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25871 decl
= expand_member_init (decl
);
25872 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25877 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25878 tf_warning_or_error
, NULL_TREE
);
25880 decl
= expand_member_init (decl
);
25881 if (decl
&& !DECL_P (decl
))
25882 in_base_initializer
= 1;
25884 init
= TREE_VALUE (t
);
25886 if (init
!= void_type_node
)
25887 init
= tsubst_expr (init
, argvec
,
25888 tf_warning_or_error
, NULL_TREE
,
25889 /*integral_constant_expression_p=*/false);
25890 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25891 /* If we had an initializer but it instantiated to nothing,
25892 value-initialize the object. This will only occur when
25893 the initializer was a pack expansion where the parameter
25894 packs used in that expansion were of length zero. */
25895 init
= void_type_node
;
25896 in_base_initializer
= 0;
25899 if (target_ctor
!= error_mark_node
25900 && init
!= error_mark_node
)
25902 error ("mem-initializer for %qD follows constructor delegation",
25906 /* Look for a target constructor. */
25907 if (init
!= error_mark_node
25908 && decl
&& CLASS_TYPE_P (decl
)
25909 && same_type_p (decl
, current_class_type
))
25911 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25914 error ("constructor delegation follows mem-initializer for %qD",
25915 TREE_PURPOSE (inits
));
25918 target_ctor
= init
;
25923 init
= build_tree_list (decl
, init
);
25924 TREE_CHAIN (init
) = inits
;
25932 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25935 set_current_access_from_decl (tree decl
)
25937 if (TREE_PRIVATE (decl
))
25938 current_access_specifier
= access_private_node
;
25939 else if (TREE_PROTECTED (decl
))
25940 current_access_specifier
= access_protected_node
;
25942 current_access_specifier
= access_public_node
;
25945 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25946 is the instantiation (which should have been created with
25947 start_enum) and ARGS are the template arguments to use. */
25950 tsubst_enum (tree tag
, tree newtag
, tree args
)
25954 if (SCOPED_ENUM_P (newtag
))
25955 begin_scope (sk_scoped_enum
, newtag
);
25957 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25962 decl
= TREE_VALUE (e
);
25963 /* Note that in a template enum, the TREE_VALUE is the
25964 CONST_DECL, not the corresponding INTEGER_CST. */
25965 value
= tsubst_expr (DECL_INITIAL (decl
),
25966 args
, tf_warning_or_error
, NULL_TREE
,
25967 /*integral_constant_expression_p=*/true);
25969 /* Give this enumeration constant the correct access. */
25970 set_current_access_from_decl (decl
);
25972 /* Actually build the enumerator itself. Here we're assuming that
25973 enumerators can't have dependent attributes. */
25974 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25975 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25978 if (SCOPED_ENUM_P (newtag
))
25981 finish_enum_value_list (newtag
);
25982 finish_enum (newtag
);
25984 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25985 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25988 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25989 its type -- but without substituting the innermost set of template
25990 arguments. So, innermost set of template parameters will appear in
25994 get_mostly_instantiated_function_type (tree decl
)
25996 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25997 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26000 /* Return truthvalue if we're processing a template different from
26001 the last one involved in diagnostics. */
26003 problematic_instantiation_changed (void)
26005 return current_tinst_level
!= last_error_tinst_level
;
26008 /* Remember current template involved in diagnostics. */
26010 record_last_problematic_instantiation (void)
26012 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26015 struct tinst_level
*
26016 current_instantiation (void)
26018 return current_tinst_level
;
26021 /* Return TRUE if current_function_decl is being instantiated, false
26025 instantiating_current_function_p (void)
26027 return (current_instantiation ()
26028 && (current_instantiation ()->maybe_get_node ()
26029 == current_function_decl
));
26032 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26033 type. Return false for ok, true for disallowed. Issue error and
26034 inform messages under control of COMPLAIN. */
26037 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26039 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26041 else if (TYPE_PTR_P (type
))
26043 else if (TYPE_REF_P (type
)
26044 && !TYPE_REF_IS_RVALUE (type
))
26046 else if (TYPE_PTRMEM_P (type
))
26048 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26050 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26052 if (complain
& tf_error
)
26053 error ("non-type template parameters of deduced class type only "
26054 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26059 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26061 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26063 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26065 /* A bound template template parm could later be instantiated to have a valid
26066 nontype parm type via an alias template. */
26067 else if (cxx_dialect
>= cxx11
26068 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26070 else if (CLASS_TYPE_P (type
))
26072 if (cxx_dialect
< cxx20
)
26074 if (complain
& tf_error
)
26075 error ("non-type template parameters of class type only available "
26076 "with %<-std=c++20%> or %<-std=gnu++20%>");
26079 if (dependent_type_p (type
))
26081 if (!complete_type_or_else (type
, NULL_TREE
))
26083 if (!structural_type_p (type
))
26085 if (complain
& tf_error
)
26087 auto_diagnostic_group d
;
26088 error ("%qT is not a valid type for a template non-type "
26089 "parameter because it is not structural", type
);
26090 structural_type_p (type
, true);
26097 if (complain
& tf_error
)
26099 if (type
== error_mark_node
)
26100 inform (input_location
, "invalid template non-type parameter");
26102 error ("%q#T is not a valid type for a template non-type parameter",
26108 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26109 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26112 dependent_type_p_r (tree type
)
26118 A type is dependent if it is:
26120 -- a template parameter. Template template parameters are types
26121 for us (since TYPE_P holds true for them) so we handle
26123 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26124 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26126 /* -- a qualified-id with a nested-name-specifier which contains a
26127 class-name that names a dependent type or whose unqualified-id
26128 names a dependent type. */
26129 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26132 /* An alias template specialization can be dependent even if the
26133 resulting type is not. */
26134 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26137 /* -- a cv-qualified type where the cv-unqualified type is
26139 No code is necessary for this bullet; the code below handles
26140 cv-qualified types, and we don't want to strip aliases with
26141 TYPE_MAIN_VARIANT because of DR 1558. */
26142 /* -- a compound type constructed from any dependent type. */
26143 if (TYPE_PTRMEM_P (type
))
26144 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26145 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26147 else if (INDIRECT_TYPE_P (type
))
26148 return dependent_type_p (TREE_TYPE (type
));
26149 else if (FUNC_OR_METHOD_TYPE_P (type
))
26153 if (dependent_type_p (TREE_TYPE (type
)))
26155 for (arg_type
= TYPE_ARG_TYPES (type
);
26157 arg_type
= TREE_CHAIN (arg_type
))
26158 if (dependent_type_p (TREE_VALUE (arg_type
)))
26160 if (cxx_dialect
>= cxx17
)
26161 /* A value-dependent noexcept-specifier makes the type dependent. */
26162 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26163 if (tree noex
= TREE_PURPOSE (spec
))
26164 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26165 affect overload resolution and treating it as dependent breaks
26166 things. Same for an unparsed noexcept expression. */
26167 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26168 && TREE_CODE (noex
) != DEFERRED_PARSE
26169 && value_dependent_expression_p (noex
))
26173 /* -- an array type constructed from any dependent type or whose
26174 size is specified by a constant expression that is
26177 We checked for type- and value-dependence of the bounds in
26178 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26179 if (TREE_CODE (type
) == ARRAY_TYPE
)
26181 if (TYPE_DOMAIN (type
)
26182 && dependent_type_p (TYPE_DOMAIN (type
)))
26184 return dependent_type_p (TREE_TYPE (type
));
26187 /* -- a template-id in which either the template name is a template
26189 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26191 /* ... or any of the template arguments is a dependent type or
26192 an expression that is type-dependent or value-dependent. */
26193 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26194 && (any_dependent_template_arguments_p
26195 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26198 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26199 dependent; if the argument of the `typeof' expression is not
26200 type-dependent, then it should already been have resolved. */
26201 if (TREE_CODE (type
) == TYPEOF_TYPE
26202 || TREE_CODE (type
) == DECLTYPE_TYPE
26203 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26206 /* A template argument pack is dependent if any of its packed
26208 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26210 tree args
= ARGUMENT_PACK_ARGS (type
);
26211 int i
, len
= TREE_VEC_LENGTH (args
);
26212 for (i
= 0; i
< len
; ++i
)
26213 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26217 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26218 be template parameters. */
26219 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26222 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26225 /* The standard does not specifically mention types that are local
26226 to template functions or local classes, but they should be
26227 considered dependent too. For example:
26229 template <int I> void f() {
26234 The size of `E' cannot be known until the value of `I' has been
26235 determined. Therefore, `E' must be considered dependent. */
26236 scope
= TYPE_CONTEXT (type
);
26237 if (scope
&& TYPE_P (scope
))
26238 return dependent_type_p (scope
);
26239 /* Don't use type_dependent_expression_p here, as it can lead
26240 to infinite recursion trying to determine whether a lambda
26241 nested in a lambda is dependent (c++/47687). */
26242 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26243 && DECL_LANG_SPECIFIC (scope
)
26244 && DECL_TEMPLATE_INFO (scope
)
26245 && (any_dependent_template_arguments_p
26246 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26249 /* Other types are non-dependent. */
26253 /* Returns TRUE if TYPE is dependent, in the sense of
26254 [temp.dep.type]. Note that a NULL type is considered dependent. */
26257 dependent_type_p (tree type
)
26259 /* If there are no template parameters in scope, then there can't be
26260 any dependent types. */
26261 if (!processing_template_decl
)
26263 /* If we are not processing a template, then nobody should be
26264 providing us with a dependent type. */
26266 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26270 /* If the type is NULL, we have not computed a type for the entity
26271 in question; in that case, the type is dependent. */
26275 /* Erroneous types can be considered non-dependent. */
26276 if (type
== error_mark_node
)
26279 /* Getting here with global_type_node means we improperly called this
26280 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26281 gcc_checking_assert (type
!= global_type_node
);
26283 /* If we have not already computed the appropriate value for TYPE,
26285 if (!TYPE_DEPENDENT_P_VALID (type
))
26287 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26288 TYPE_DEPENDENT_P_VALID (type
) = 1;
26291 return TYPE_DEPENDENT_P (type
);
26294 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26295 lookup. In other words, a dependent type that is not the current
26299 dependent_scope_p (tree scope
)
26301 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26302 && !currently_open_class (scope
));
26305 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26306 an unknown base of 'this' (and is therefore instantiation-dependent). */
26309 unknown_base_ref_p (tree t
)
26311 if (!current_class_ptr
)
26314 tree mem
= TREE_OPERAND (t
, 1);
26315 if (shared_member_p (mem
))
26318 tree cur
= current_nonlambda_class_type ();
26319 if (!any_dependent_bases_p (cur
))
26322 tree ctx
= TREE_OPERAND (t
, 0);
26323 if (DERIVED_FROM_P (ctx
, cur
))
26329 /* T is a SCOPE_REF; return whether we need to consider it
26330 instantiation-dependent so that we can check access at instantiation
26331 time even though we know which member it resolves to. */
26334 instantiation_dependent_scope_ref_p (tree t
)
26336 if (DECL_P (TREE_OPERAND (t
, 1))
26337 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26338 && !unknown_base_ref_p (t
)
26339 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26340 TREE_OPERAND (t
, 1)))
26346 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26347 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26350 /* Note that this predicate is not appropriate for general expressions;
26351 only constant expressions (that satisfy potential_constant_expression)
26352 can be tested for value dependence. */
26355 value_dependent_expression_p (tree expression
)
26357 if (!processing_template_decl
|| expression
== NULL_TREE
)
26360 /* A type-dependent expression is also value-dependent. */
26361 if (type_dependent_expression_p (expression
))
26364 switch (TREE_CODE (expression
))
26367 /* A dependent member function of the current instantiation. */
26368 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26370 case FUNCTION_DECL
:
26371 /* A dependent member function of the current instantiation. */
26372 if (DECL_CLASS_SCOPE_P (expression
)
26373 && dependent_type_p (DECL_CONTEXT (expression
)))
26377 case IDENTIFIER_NODE
:
26378 /* A name that has not been looked up -- must be dependent. */
26381 case TEMPLATE_PARM_INDEX
:
26382 /* A non-type template parm. */
26386 /* A non-type template parm. */
26387 if (DECL_TEMPLATE_PARM_P (expression
))
26389 return value_dependent_expression_p (DECL_INITIAL (expression
));
26392 /* A constant with literal type and is initialized
26393 with an expression that is value-dependent. */
26394 if (DECL_DEPENDENT_INIT_P (expression
)
26395 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26396 || TYPE_REF_P (TREE_TYPE (expression
)))
26398 if (DECL_HAS_VALUE_EXPR_P (expression
))
26400 tree value_expr
= DECL_VALUE_EXPR (expression
);
26401 if (value_dependent_expression_p (value_expr
)
26402 /* __PRETTY_FUNCTION__ inside a template function is dependent
26403 on the name of the function. */
26404 || (DECL_PRETTY_FUNCTION_P (expression
)
26405 /* It might be used in a template, but not a template
26406 function, in which case its DECL_VALUE_EXPR will be
26408 && value_expr
== error_mark_node
))
26413 case DYNAMIC_CAST_EXPR
:
26414 case STATIC_CAST_EXPR
:
26415 case CONST_CAST_EXPR
:
26416 case REINTERPRET_CAST_EXPR
:
26418 case IMPLICIT_CONV_EXPR
:
26419 /* These expressions are value-dependent if the type to which
26420 the cast occurs is dependent or the expression being casted
26421 is value-dependent. */
26423 tree type
= TREE_TYPE (expression
);
26425 if (dependent_type_p (type
))
26428 /* A functional cast has a list of operands. */
26429 expression
= TREE_OPERAND (expression
, 0);
26432 /* If there are no operands, it must be an expression such
26433 as "int()". This should not happen for aggregate types
26434 because it would form non-constant expressions. */
26435 gcc_assert (cxx_dialect
>= cxx11
26436 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26441 if (TREE_CODE (expression
) == TREE_LIST
)
26442 return any_value_dependent_elements_p (expression
);
26444 return value_dependent_expression_p (expression
);
26448 if (SIZEOF_EXPR_TYPE_P (expression
))
26449 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26453 /* A `sizeof' expression is value-dependent if the operand is
26454 type-dependent or is a pack expansion. */
26455 expression
= TREE_OPERAND (expression
, 0);
26456 if (PACK_EXPANSION_P (expression
))
26458 else if (TYPE_P (expression
))
26459 return dependent_type_p (expression
);
26460 return instantiation_dependent_uneval_expression_p (expression
);
26462 case AT_ENCODE_EXPR
:
26463 /* An 'encode' expression is value-dependent if the operand is
26465 expression
= TREE_OPERAND (expression
, 0);
26466 return dependent_type_p (expression
);
26468 case NOEXCEPT_EXPR
:
26469 expression
= TREE_OPERAND (expression
, 0);
26470 return instantiation_dependent_uneval_expression_p (expression
);
26473 /* All instantiation-dependent expressions should also be considered
26474 value-dependent. */
26475 return instantiation_dependent_scope_ref_p (expression
);
26477 case COMPONENT_REF
:
26478 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26479 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26481 case NONTYPE_ARGUMENT_PACK
:
26482 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26483 is value-dependent. */
26485 tree values
= ARGUMENT_PACK_ARGS (expression
);
26486 int i
, len
= TREE_VEC_LENGTH (values
);
26488 for (i
= 0; i
< len
; ++i
)
26489 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26497 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26499 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26505 if (TREE_CODE (type2
) != TREE_LIST
)
26506 return dependent_type_p (type2
);
26508 for (; type2
; type2
= TREE_CHAIN (type2
))
26509 if (dependent_type_p (TREE_VALUE (type2
)))
26516 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26517 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26520 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26521 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26525 tree op
= TREE_OPERAND (expression
, 0);
26526 return (value_dependent_expression_p (op
)
26527 || has_value_dependent_address (op
));
26530 case REQUIRES_EXPR
:
26531 /* Treat all requires-expressions as value-dependent so
26532 we don't try to fold them. */
26536 return dependent_type_p (TREE_OPERAND (expression
, 0));
26540 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26542 tree fn
= get_callee_fndecl (expression
);
26544 nargs
= call_expr_nargs (expression
);
26545 for (i
= 0; i
< nargs
; ++i
)
26547 tree op
= CALL_EXPR_ARG (expression
, i
);
26548 /* In a call to a constexpr member function, look through the
26549 implicit ADDR_EXPR on the object argument so that it doesn't
26550 cause the call to be considered value-dependent. We also
26551 look through it in potential_constant_expression. */
26552 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26553 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26554 && TREE_CODE (op
) == ADDR_EXPR
)
26555 op
= TREE_OPERAND (op
, 0);
26556 if (value_dependent_expression_p (op
))
26562 case TEMPLATE_ID_EXPR
:
26563 return concept_definition_p (TREE_OPERAND (expression
, 0));
26569 if (dependent_type_p (TREE_TYPE (expression
)))
26571 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26572 if (value_dependent_expression_p (val
))
26578 /* Treat a GNU statement expression as dependent to avoid crashing
26579 under instantiate_non_dependent_expr; it can't be constant. */
26583 /* A constant expression is value-dependent if any subexpression is
26584 value-dependent. */
26585 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26587 case tcc_reference
:
26589 case tcc_comparison
:
26591 case tcc_expression
:
26594 int i
, len
= cp_tree_operand_length (expression
);
26596 for (i
= 0; i
< len
; i
++)
26598 tree t
= TREE_OPERAND (expression
, i
);
26600 /* In some cases, some of the operands may be missing.
26601 (For example, in the case of PREDECREMENT_EXPR, the
26602 amount to increment by may be missing.) That doesn't
26603 make the expression dependent. */
26604 if (t
&& value_dependent_expression_p (t
))
26615 /* The expression is not value-dependent. */
26619 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26620 [temp.dep.expr]. Note that an expression with no type is
26621 considered dependent. Other parts of the compiler arrange for an
26622 expression with type-dependent subexpressions to have no type, so
26623 this function doesn't have to be fully recursive. */
26626 type_dependent_expression_p (tree expression
)
26628 if (!processing_template_decl
)
26631 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26634 STRIP_ANY_LOCATION_WRAPPER (expression
);
26636 /* An unresolved name is always dependent. */
26637 if (identifier_p (expression
)
26638 || TREE_CODE (expression
) == USING_DECL
26639 || TREE_CODE (expression
) == WILDCARD_DECL
)
26642 /* A lambda-expression in template context is dependent. dependent_type_p is
26643 true for a lambda in the scope of a class or function template, but that
26644 doesn't cover all template contexts, like a default template argument. */
26645 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26648 /* A fold expression is type-dependent. */
26649 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26650 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26651 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26652 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26655 /* Some expression forms are never type-dependent. */
26656 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
26657 || TREE_CODE (expression
) == SIZEOF_EXPR
26658 || TREE_CODE (expression
) == ALIGNOF_EXPR
26659 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26660 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26661 || TREE_CODE (expression
) == TRAIT_EXPR
26662 || TREE_CODE (expression
) == TYPEID_EXPR
26663 || TREE_CODE (expression
) == DELETE_EXPR
26664 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26665 || TREE_CODE (expression
) == THROW_EXPR
26666 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26669 /* The types of these expressions depends only on the type to which
26670 the cast occurs. */
26671 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26672 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26673 || TREE_CODE (expression
) == CONST_CAST_EXPR
26674 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26675 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26676 || TREE_CODE (expression
) == CAST_EXPR
)
26677 return dependent_type_p (TREE_TYPE (expression
));
26679 /* The types of these expressions depends only on the type created
26680 by the expression. */
26681 if (TREE_CODE (expression
) == NEW_EXPR
26682 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26684 /* For NEW_EXPR tree nodes created inside a template, either
26685 the object type itself or a TREE_LIST may appear as the
26687 tree type
= TREE_OPERAND (expression
, 1);
26688 if (TREE_CODE (type
) == TREE_LIST
)
26689 /* This is an array type. We need to check array dimensions
26691 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26692 || value_dependent_expression_p
26693 (TREE_OPERAND (TREE_VALUE (type
), 1));
26695 return dependent_type_p (type
);
26698 if (TREE_CODE (expression
) == SCOPE_REF
)
26700 tree scope
= TREE_OPERAND (expression
, 0);
26701 tree name
= TREE_OPERAND (expression
, 1);
26703 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26704 contains an identifier associated by name lookup with one or more
26705 declarations declared with a dependent type, or...a
26706 nested-name-specifier or qualified-id that names a member of an
26707 unknown specialization. */
26708 return (type_dependent_expression_p (name
)
26709 || dependent_scope_p (scope
));
26712 if (TREE_CODE (expression
) == TEMPLATE_DECL
26713 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26714 return uses_outer_template_parms (expression
);
26716 if (TREE_CODE (expression
) == STMT_EXPR
)
26717 expression
= stmt_expr_value_expr (expression
);
26719 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26724 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26726 if (type_dependent_expression_p (elt
))
26732 /* A static data member of the current instantiation with incomplete
26733 array type is type-dependent, as the definition and specializations
26734 can have different bounds. */
26735 if (VAR_P (expression
)
26736 && DECL_CLASS_SCOPE_P (expression
)
26737 && dependent_type_p (DECL_CONTEXT (expression
))
26738 && VAR_HAD_UNKNOWN_BOUND (expression
))
26741 /* An array of unknown bound depending on a variadic parameter, eg:
26743 template<typename... Args>
26744 void foo (Args... args)
26746 int arr[] = { args... };
26749 template<int... vals>
26752 int arr[] = { vals... };
26755 If the array has no length and has an initializer, it must be that
26756 we couldn't determine its length in cp_complete_array_type because
26757 it is dependent. */
26758 if (VAR_P (expression
)
26759 && TREE_TYPE (expression
) != NULL_TREE
26760 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26761 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26762 && DECL_INITIAL (expression
))
26765 /* A function or variable template-id is type-dependent if it has any
26766 dependent template arguments. */
26767 if (VAR_OR_FUNCTION_DECL_P (expression
)
26768 && DECL_LANG_SPECIFIC (expression
)
26769 && DECL_TEMPLATE_INFO (expression
))
26771 /* Consider the innermost template arguments, since those are the ones
26772 that come from the template-id; the template arguments for the
26773 enclosing class do not make it type-dependent unless they are used in
26774 the type of the decl. */
26775 if (instantiates_primary_template_p (expression
)
26776 && (any_dependent_template_arguments_p
26777 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26781 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26782 type-dependent. Checking this is important for functions with auto return
26783 type, which looks like a dependent type. */
26784 if (TREE_CODE (expression
) == FUNCTION_DECL
26785 && !(DECL_CLASS_SCOPE_P (expression
)
26786 && dependent_type_p (DECL_CONTEXT (expression
)))
26787 && !(DECL_LANG_SPECIFIC (expression
)
26788 && DECL_FRIEND_P (expression
)
26789 && (!DECL_FRIEND_CONTEXT (expression
)
26790 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26791 && !DECL_LOCAL_FUNCTION_P (expression
))
26793 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26794 || undeduced_auto_decl (expression
));
26798 /* Always dependent, on the number of arguments if nothing else. */
26799 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26802 if (TREE_TYPE (expression
) == unknown_type_node
)
26804 if (TREE_CODE (expression
) == ADDR_EXPR
)
26805 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26806 if (TREE_CODE (expression
) == COMPONENT_REF
26807 || TREE_CODE (expression
) == OFFSET_REF
)
26809 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26811 expression
= TREE_OPERAND (expression
, 1);
26812 if (identifier_p (expression
))
26815 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26816 if (TREE_CODE (expression
) == SCOPE_REF
)
26819 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26820 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
26821 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
26824 if (BASELINK_P (expression
))
26826 if (BASELINK_OPTYPE (expression
)
26827 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26829 expression
= BASELINK_FUNCTIONS (expression
);
26832 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26834 if (any_dependent_template_arguments_p
26835 (TREE_OPERAND (expression
, 1)))
26837 expression
= TREE_OPERAND (expression
, 0);
26838 if (identifier_p (expression
))
26842 gcc_assert (OVL_P (expression
));
26844 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26845 if (type_dependent_expression_p (*iter
))
26851 /* The type of a non-type template parm declared with a placeholder type
26852 depends on the corresponding template argument, even though
26853 placeholders are not normally considered dependent. */
26854 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26855 && is_auto (TREE_TYPE (expression
)))
26858 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26860 /* Dependent type attributes might not have made it from the decl to
26862 if (DECL_P (expression
)
26863 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26866 return (dependent_type_p (TREE_TYPE (expression
)));
26869 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26870 type-dependent if the expression refers to a member of the current
26871 instantiation and the type of the referenced member is dependent, or the
26872 class member access expression refers to a member of an unknown
26875 This function returns true if the OBJECT in such a class member access
26876 expression is of an unknown specialization. */
26879 type_dependent_object_expression_p (tree object
)
26881 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26883 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26885 tree scope
= TREE_TYPE (object
);
26886 return (!scope
|| dependent_scope_p (scope
));
26889 /* walk_tree callback function for instantiation_dependent_expression_p,
26890 below. Returns non-zero if a dependent subexpression is found. */
26893 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26898 /* We don't have to worry about decltype currently because decltype
26899 of an instantiation-dependent expr is a dependent type. This
26900 might change depending on the resolution of DR 1172. */
26901 *walk_subtrees
= false;
26904 enum tree_code code
= TREE_CODE (*tp
);
26907 /* Don't treat an argument list as dependent just because it has no
26911 case NONTYPE_ARGUMENT_PACK
:
26914 case TEMPLATE_PARM_INDEX
:
26915 if (dependent_type_p (TREE_TYPE (*tp
)))
26917 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26919 /* We'll check value-dependence separately. */
26922 /* Handle expressions with type operands. */
26926 case AT_ENCODE_EXPR
:
26928 tree op
= TREE_OPERAND (*tp
, 0);
26929 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26930 op
= TREE_TYPE (op
);
26933 if (dependent_type_p (op
))
26937 *walk_subtrees
= false;
26944 case COMPONENT_REF
:
26945 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26946 /* In a template, finish_class_member_access_expr creates a
26947 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26948 type-dependent, so that we can check access control at
26949 instantiation time (PR 42277). See also Core issue 1273. */
26954 if (instantiation_dependent_scope_ref_p (*tp
))
26959 /* Treat statement-expressions as dependent. */
26963 /* Treat requires-expressions as dependent. */
26964 case REQUIRES_EXPR
:
26968 /* Treat concept checks as dependent. */
26969 if (concept_check_p (*tp
))
26973 case TEMPLATE_ID_EXPR
:
26974 /* Treat concept checks as dependent. */
26975 if (concept_check_p (*tp
))
26980 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26988 if (type_dependent_expression_p (*tp
))
26994 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26995 sense defined by the ABI:
26997 "An expression is instantiation-dependent if it is type-dependent
26998 or value-dependent, or it has a subexpression that is type-dependent
26999 or value-dependent."
27001 Except don't actually check value-dependence for unevaluated expressions,
27002 because in sizeof(i) we don't care about the value of i. Checking
27003 type-dependence will in turn check value-dependence of array bounds/template
27004 arguments as needed. */
27007 instantiation_dependent_uneval_expression_p (tree expression
)
27011 if (!processing_template_decl
)
27014 if (expression
== error_mark_node
)
27017 result
= cp_walk_tree_without_duplicates (&expression
,
27018 instantiation_dependent_r
, NULL
);
27019 return result
!= NULL_TREE
;
27022 /* As above, but also check value-dependence of the expression as a whole. */
27025 instantiation_dependent_expression_p (tree expression
)
27027 return (instantiation_dependent_uneval_expression_p (expression
)
27028 || value_dependent_expression_p (expression
));
27031 /* Like type_dependent_expression_p, but it also works while not processing
27032 a template definition, i.e. during substitution or mangling. */
27035 type_dependent_expression_p_push (tree expr
)
27038 ++processing_template_decl
;
27039 b
= type_dependent_expression_p (expr
);
27040 --processing_template_decl
;
27044 /* Returns TRUE if ARGS contains a type-dependent expression. */
27047 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27052 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27054 if (type_dependent_expression_p (arg
))
27060 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27061 expressions) contains any type-dependent expressions. */
27064 any_type_dependent_elements_p (const_tree list
)
27066 for (; list
; list
= TREE_CHAIN (list
))
27067 if (type_dependent_expression_p (TREE_VALUE (list
)))
27073 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27074 expressions) contains any value-dependent expressions. */
27077 any_value_dependent_elements_p (const_tree list
)
27079 for (; list
; list
= TREE_CHAIN (list
))
27080 if (value_dependent_expression_p (TREE_VALUE (list
)))
27086 /* Returns TRUE if the ARG (a template argument) is dependent. */
27089 dependent_template_arg_p (tree arg
)
27091 if (!processing_template_decl
)
27094 /* Assume a template argument that was wrongly written by the user
27095 is dependent. This is consistent with what
27096 any_dependent_template_arguments_p [that calls this function]
27098 if (!arg
|| arg
== error_mark_node
)
27101 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27102 arg
= argument_pack_select_arg (arg
);
27104 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27106 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27108 if (DECL_TEMPLATE_PARM_P (arg
))
27110 /* A member template of a dependent class is not necessarily
27111 type-dependent, but it is a dependent template argument because it
27112 will be a member of an unknown specialization to that template. */
27113 tree scope
= CP_DECL_CONTEXT (arg
);
27114 return TYPE_P (scope
) && dependent_type_p (scope
);
27116 else if (ARGUMENT_PACK_P (arg
))
27118 tree args
= ARGUMENT_PACK_ARGS (arg
);
27119 int i
, len
= TREE_VEC_LENGTH (args
);
27120 for (i
= 0; i
< len
; ++i
)
27122 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27128 else if (TYPE_P (arg
))
27129 return dependent_type_p (arg
);
27131 return value_dependent_expression_p (arg
);
27134 /* Returns true if ARGS (a collection of template arguments) contains
27135 any types that require structural equality testing. */
27138 any_template_arguments_need_structural_equality_p (tree args
)
27145 if (args
== error_mark_node
)
27148 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27150 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27151 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27153 tree arg
= TREE_VEC_ELT (level
, j
);
27154 tree packed_args
= NULL_TREE
;
27157 if (ARGUMENT_PACK_P (arg
))
27159 /* Look inside the argument pack. */
27160 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27161 len
= TREE_VEC_LENGTH (packed_args
);
27164 for (k
= 0; k
< len
; ++k
)
27167 arg
= TREE_VEC_ELT (packed_args
, k
);
27169 if (error_operand_p (arg
))
27171 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27173 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27175 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27176 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27185 /* Returns true if ARGS (a collection of template arguments) contains
27186 any dependent arguments. */
27189 any_dependent_template_arguments_p (const_tree args
)
27196 if (args
== error_mark_node
)
27199 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27201 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27202 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27203 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27210 /* Returns true if ARGS contains any errors. */
27213 any_erroneous_template_args_p (const_tree args
)
27218 if (args
== error_mark_node
)
27221 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27223 if (tree ti
= get_template_info (args
))
27224 args
= TI_ARGS (ti
);
27232 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27234 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27235 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27236 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27243 /* Returns TRUE if the template TMPL is type-dependent. */
27246 dependent_template_p (tree tmpl
)
27248 if (TREE_CODE (tmpl
) == OVERLOAD
)
27250 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27251 if (dependent_template_p (*iter
))
27256 /* Template template parameters are dependent. */
27257 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27258 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27260 /* So are names that have not been looked up. */
27261 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27266 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27269 dependent_template_id_p (tree tmpl
, tree args
)
27271 return (dependent_template_p (tmpl
)
27272 || any_dependent_template_arguments_p (args
));
27275 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27279 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27283 if (!processing_template_decl
)
27286 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27288 tree decl
= TREE_VEC_ELT (declv
, i
);
27289 tree init
= TREE_VEC_ELT (initv
, i
);
27290 tree cond
= TREE_VEC_ELT (condv
, i
);
27291 tree incr
= TREE_VEC_ELT (incrv
, i
);
27293 if (type_dependent_expression_p (decl
)
27294 || TREE_CODE (decl
) == SCOPE_REF
)
27297 if (init
&& type_dependent_expression_p (init
))
27300 if (cond
== global_namespace
)
27303 if (type_dependent_expression_p (cond
))
27306 if (COMPARISON_CLASS_P (cond
)
27307 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27308 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27311 if (TREE_CODE (incr
) == MODOP_EXPR
)
27313 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27314 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27317 else if (type_dependent_expression_p (incr
))
27319 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27321 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27323 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27325 tree t
= TREE_OPERAND (incr
, 1);
27326 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27327 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27330 /* If this loop has a class iterator with != comparison
27331 with increment other than i++/++i/i--/--i, make sure the
27332 increment is constant. */
27333 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27334 && TREE_CODE (cond
) == NE_EXPR
)
27336 if (TREE_OPERAND (t
, 0) == decl
)
27337 t
= TREE_OPERAND (t
, 1);
27339 t
= TREE_OPERAND (t
, 0);
27340 if (TREE_CODE (t
) != INTEGER_CST
)
27350 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27351 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27352 no such TYPE can be found. Note that this function peers inside
27353 uninstantiated templates and therefore should be used only in
27354 extremely limited situations. ONLY_CURRENT_P restricts this
27355 peering to the currently open classes hierarchy (which is required
27356 when comparing types). */
27359 resolve_typename_type (tree type
, bool only_current_p
)
27368 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27370 scope
= TYPE_CONTEXT (type
);
27371 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27372 gcc_checking_assert (uses_template_parms (scope
));
27374 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27375 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27376 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27377 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27378 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27379 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27380 the TYPENAME_TYPE instead, we avoid messing up with a possible
27381 typedef variant case. */
27382 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27384 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27385 it first before we can figure out what NAME refers to. */
27386 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27388 if (TYPENAME_IS_RESOLVING_P (scope
))
27389 /* Given a class template A with a dependent base with nested type C,
27390 typedef typename A::C::C C will land us here, as trying to resolve
27391 the initial A::C leads to the local C typedef, which leads back to
27392 A::C::C. So we break the recursion now. */
27395 scope
= resolve_typename_type (scope
, only_current_p
);
27397 /* If we don't know what SCOPE refers to, then we cannot resolve the
27399 if (!CLASS_TYPE_P (scope
))
27401 /* If this is a typedef, we don't want to look inside (c++/11987). */
27402 if (typedef_variant_p (type
))
27404 /* If SCOPE isn't the template itself, it will not have a valid
27405 TYPE_FIELDS list. */
27406 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27407 /* scope is either the template itself or a compatible instantiation
27408 like X<T>, so look up the name in the original template. */
27409 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27410 /* If scope has no fields, it can't be a current instantiation. Check this
27411 before currently_open_class to avoid infinite recursion (71515). */
27412 if (!TYPE_FIELDS (scope
))
27414 /* If the SCOPE is not the current instantiation, there's no reason
27415 to look inside it. */
27416 if (only_current_p
&& !currently_open_class (scope
))
27418 /* Enter the SCOPE so that name lookup will be resolved as if we
27419 were in the class definition. In particular, SCOPE will no
27420 longer be considered a dependent type. */
27421 pushed_scope
= push_scope (scope
);
27422 /* Look up the declaration. */
27423 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27424 tf_warning_or_error
);
27426 result
= NULL_TREE
;
27428 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27429 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27430 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27433 else if (identifier_p (fullname
)
27434 && TREE_CODE (decl
) == TYPE_DECL
)
27436 result
= TREE_TYPE (decl
);
27437 if (result
== error_mark_node
)
27438 result
= NULL_TREE
;
27440 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27441 && DECL_CLASS_TEMPLATE_P (decl
))
27443 /* Obtain the template and the arguments. */
27444 tree tmpl
= TREE_OPERAND (fullname
, 0);
27445 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27447 /* We get here with a plain identifier because a previous tentative
27448 parse of the nested-name-specifier as part of a ptr-operator saw
27449 ::template X<A>. The use of ::template is necessary in a
27450 ptr-operator, but wrong in a declarator-id.
27452 [temp.names]: In a qualified-id of a declarator-id, the keyword
27453 template shall not appear at the top level. */
27454 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27455 "keyword %<template%> not allowed in declarator-id");
27458 tree args
= TREE_OPERAND (fullname
, 1);
27459 /* Instantiate the template. */
27460 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27461 /*entering_scope=*/true,
27462 tf_error
| tf_user
);
27463 if (result
== error_mark_node
)
27464 result
= NULL_TREE
;
27467 /* Leave the SCOPE. */
27469 pop_scope (pushed_scope
);
27471 /* If we failed to resolve it, return the original typename. */
27475 /* If lookup found a typename type, resolve that too. */
27476 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27478 /* Ill-formed programs can cause infinite recursion here, so we
27479 must catch that. */
27480 TYPENAME_IS_RESOLVING_P (result
) = 1;
27481 result
= resolve_typename_type (result
, only_current_p
);
27482 TYPENAME_IS_RESOLVING_P (result
) = 0;
27485 /* Qualify the resulting type. */
27486 quals
= cp_type_quals (type
);
27488 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27493 /* EXPR is an expression which is not type-dependent. Return a proxy
27494 for EXPR that can be used to compute the types of larger
27495 expressions containing EXPR. */
27498 build_non_dependent_expr (tree expr
)
27500 tree orig_expr
= expr
;
27503 /* When checking, try to get a constant value for all non-dependent
27504 expressions in order to expose bugs in *_dependent_expression_p
27505 and constexpr. This can affect code generation, see PR70704, so
27506 only do this for -fchecking=2. */
27507 if (flag_checking
> 1
27508 && cxx_dialect
>= cxx11
27509 /* Don't do this during nsdmi parsing as it can lead to
27510 unexpected recursive instantiations. */
27511 && !parsing_nsdmi ()
27512 /* Don't do this during concept processing either and for
27513 the same reason. */
27514 && !processing_constraint_expression_p ())
27515 fold_non_dependent_expr (expr
, tf_none
);
27517 STRIP_ANY_LOCATION_WRAPPER (expr
);
27519 /* Preserve OVERLOADs; the functions must be available to resolve
27522 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27523 inner_expr
= stmt_expr_value_expr (inner_expr
);
27524 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27525 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27526 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27527 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27528 if (is_overloaded_fn (inner_expr
)
27529 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27531 /* There is no need to return a proxy for a variable or enumerator. */
27532 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27534 /* Preserve string constants; conversions from string constants to
27535 "char *" are allowed, even though normally a "const char *"
27536 cannot be used to initialize a "char *". */
27537 if (TREE_CODE (expr
) == STRING_CST
)
27539 /* Preserve void and arithmetic constants, as an optimization -- there is no
27540 reason to create a new node. */
27541 if (TREE_CODE (expr
) == VOID_CST
27542 || TREE_CODE (expr
) == INTEGER_CST
27543 || TREE_CODE (expr
) == REAL_CST
)
27545 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27546 There is at least one place where we want to know that a
27547 particular expression is a throw-expression: when checking a ?:
27548 expression, there are special rules if the second or third
27549 argument is a throw-expression. */
27550 if (TREE_CODE (expr
) == THROW_EXPR
)
27553 /* Don't wrap an initializer list, we need to be able to look inside. */
27554 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27557 /* Don't wrap a dummy object, we need to be able to test for it. */
27558 if (is_dummy_object (expr
))
27561 if (TREE_CODE (expr
) == COND_EXPR
)
27562 return build3 (COND_EXPR
,
27564 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27565 (TREE_OPERAND (expr
, 1)
27566 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27567 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27568 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27569 if (TREE_CODE (expr
) == COMPOUND_EXPR
27570 && !COMPOUND_EXPR_OVERLOADED (expr
))
27571 return build2 (COMPOUND_EXPR
,
27573 TREE_OPERAND (expr
, 0),
27574 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27576 /* If the type is unknown, it can't really be non-dependent */
27577 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27579 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27580 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27581 TREE_TYPE (expr
), expr
);
27584 /* ARGS is a vector of expressions as arguments to a function call.
27585 Replace the arguments with equivalent non-dependent expressions.
27586 This modifies ARGS in place. */
27589 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27594 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27596 tree newarg
= build_non_dependent_expr (arg
);
27598 (*args
)[ix
] = newarg
;
27602 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27603 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27604 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27607 make_auto_1 (tree name
, bool set_canonical
)
27609 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27610 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27611 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27612 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27613 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27614 TYPE_NAME (au
), NULL_TREE
);
27616 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27617 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27618 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27619 if (name
== decltype_auto_identifier
)
27620 AUTO_IS_DECLTYPE (au
) = true;
27626 make_decltype_auto (void)
27628 return make_auto_1 (decltype_auto_identifier
, true);
27634 return make_auto_1 (auto_identifier
, true);
27637 /* Return a C++17 deduction placeholder for class template TMPL. */
27640 make_template_placeholder (tree tmpl
)
27642 tree t
= make_auto_1 (auto_identifier
, false);
27643 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27644 /* Our canonical type depends on the placeholder. */
27645 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27649 /* True iff T is a C++17 class template deduction placeholder. */
27652 template_placeholder_p (tree t
)
27654 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27657 /* Make a "constrained auto" type-specifier. This is an auto or
27658 decltype(auto) type with constraints that must be associated after
27659 deduction. The constraint is formed from the given concept CON
27660 and its optional sequence of template arguments ARGS.
27662 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27665 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27667 /* Build the constraint. */
27668 tree tmpl
= DECL_TI_TEMPLATE (con
);
27670 if (TREE_CODE (con
) == FUNCTION_DECL
)
27671 expr
= ovl_make (tmpl
);
27672 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27674 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27676 /* Our canonical type depends on the constraint. */
27677 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27679 /* Attach the constraint to the type declaration. */
27680 return TYPE_NAME (type
);
27683 /* Make a "constrained auto" type-specifier. */
27686 make_constrained_auto (tree con
, tree args
)
27688 tree type
= make_auto_1 (auto_identifier
, false);
27689 return make_constrained_placeholder_type (type
, con
, args
);
27692 /* Make a "constrained decltype(auto)" type-specifier. */
27695 make_constrained_decltype_auto (tree con
, tree args
)
27697 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27698 return make_constrained_placeholder_type (type
, con
, args
);
27701 /* Build and return a concept definition. Like other templates, the
27702 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27703 the TEMPLATE_DECL. */
27706 finish_concept_definition (cp_expr id
, tree init
)
27708 gcc_assert (identifier_p (id
));
27709 gcc_assert (processing_template_decl
);
27711 location_t loc
= id
.get_location();
27713 /* A concept-definition shall not have associated constraints. */
27714 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27716 error_at (loc
, "a concept cannot be constrained");
27717 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27720 /* A concept-definition shall appear in namespace scope. Templates
27721 aren't allowed in block scope, so we only need to check for class
27723 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27725 error_at (loc
, "concept %qE not in namespace scope", *id
);
27726 return error_mark_node
;
27729 /* Initially build the concept declaration; its type is bool. */
27730 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27731 DECL_CONTEXT (decl
) = current_scope ();
27732 DECL_INITIAL (decl
) = init
;
27734 /* Push the enclosing template. */
27735 return push_template_decl (decl
);
27738 /* Given type ARG, return std::initializer_list<ARG>. */
27743 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27745 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27747 gcc_rich_location
richloc (input_location
);
27748 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27749 error_at (&richloc
,
27750 "deducing from brace-enclosed initializer list"
27751 " requires %<#include <initializer_list>%>");
27753 return error_mark_node
;
27755 tree argvec
= make_tree_vec (1);
27756 TREE_VEC_ELT (argvec
, 0) = arg
;
27758 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27759 NULL_TREE
, 0, tf_warning_or_error
);
27762 /* Replace auto in TYPE with std::initializer_list<auto>. */
27765 listify_autos (tree type
, tree auto_node
)
27767 tree init_auto
= listify (strip_top_quals (auto_node
));
27768 tree argvec
= make_tree_vec (1);
27769 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27770 if (processing_template_decl
)
27771 argvec
= add_to_template_args (current_template_args (), argvec
);
27772 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27775 /* Hash traits for hashing possibly constrained 'auto'
27776 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27778 struct auto_hash
: default_hash_traits
<tree
>
27780 static inline hashval_t
hash (tree
);
27781 static inline bool equal (tree
, tree
);
27784 /* Hash the 'auto' T. */
27787 auto_hash::hash (tree t
)
27789 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27790 /* Matching constrained-type-specifiers denote the same template
27791 parameter, so hash the constraint. */
27792 return hash_placeholder_constraint (c
);
27794 /* But unconstrained autos are all separate, so just hash the pointer. */
27795 return iterative_hash_object (t
, 0);
27798 /* Compare two 'auto's. */
27801 auto_hash::equal (tree t1
, tree t2
)
27806 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27807 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27809 /* Two unconstrained autos are distinct. */
27813 return equivalent_placeholder_constraints (c1
, c2
);
27816 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27817 constrained) auto, add it to the vector. */
27820 extract_autos_r (tree t
, void *data
)
27822 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27825 /* All the autos were built with index 0; fix that up now. */
27826 tree
*p
= hash
.find_slot (t
, INSERT
);
27829 /* If this is a repeated constrained-type-specifier, use the index we
27831 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27834 /* Otherwise this is new, so use the current count. */
27836 idx
= hash
.elements () - 1;
27838 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27841 /* Always keep walking. */
27845 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27846 says they can appear anywhere in the type. */
27849 extract_autos (tree type
)
27851 hash_set
<tree
> visited
;
27852 hash_table
<auto_hash
> hash (2);
27854 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27856 tree tree_vec
= make_tree_vec (hash
.elements());
27857 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27858 iter
!= hash
.end(); ++iter
)
27861 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27862 TREE_VEC_ELT (tree_vec
, i
)
27863 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27869 /* The stem for deduction guide names. */
27870 const char *const dguide_base
= "__dguide_";
27872 /* Return the name for a deduction guide for class template TMPL. */
27875 dguide_name (tree tmpl
)
27877 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27878 tree tname
= TYPE_IDENTIFIER (type
);
27879 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27880 + IDENTIFIER_LENGTH (tname
));
27881 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27882 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27883 IDENTIFIER_LENGTH (tname
) + 1);
27884 tree dname
= get_identifier (buf
);
27885 TREE_TYPE (dname
) = type
;
27889 /* True if NAME is the name of a deduction guide. */
27892 dguide_name_p (tree name
)
27894 return (TREE_CODE (name
) == IDENTIFIER_NODE
27895 && TREE_TYPE (name
)
27896 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27897 strlen (dguide_base
)));
27900 /* True if FN is a deduction guide. */
27903 deduction_guide_p (const_tree fn
)
27906 if (tree name
= DECL_NAME (fn
))
27907 return dguide_name_p (name
);
27911 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27914 copy_guide_p (const_tree fn
)
27916 gcc_assert (deduction_guide_p (fn
));
27917 if (!DECL_ARTIFICIAL (fn
))
27919 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27920 return (TREE_CHAIN (parms
) == void_list_node
27921 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27924 /* True if FN is a guide generated from a constructor template. */
27927 template_guide_p (const_tree fn
)
27929 gcc_assert (deduction_guide_p (fn
));
27930 if (!DECL_ARTIFICIAL (fn
))
27932 tree tmpl
= DECL_TI_TEMPLATE (fn
);
27933 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
27934 return PRIMARY_TEMPLATE_P (org
);
27938 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27939 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27940 template parameter types. Note that the handling of template template
27941 parameters relies on current_template_parms being set appropriately for the
27945 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
27946 tree tsubst_args
, tsubst_flags_t complain
)
27948 if (olddecl
== error_mark_node
)
27949 return error_mark_node
;
27951 tree oldidx
= get_template_parm_index (olddecl
);
27954 if (TREE_CODE (olddecl
) == TYPE_DECL
27955 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27957 tree oldtype
= TREE_TYPE (olddecl
);
27958 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27959 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27960 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27961 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27962 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27966 newtype
= TREE_TYPE (olddecl
);
27967 if (type_uses_auto (newtype
))
27969 // Substitute once to fix references to other template parameters.
27970 newtype
= tsubst (newtype
, tsubst_args
,
27971 complain
|tf_partial
, NULL_TREE
);
27972 // Now substitute again to reduce the level of the auto.
27973 newtype
= tsubst (newtype
, current_template_args (),
27974 complain
, NULL_TREE
);
27977 newtype
= tsubst (newtype
, tsubst_args
,
27978 complain
, NULL_TREE
);
27982 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27983 DECL_NAME (olddecl
), newtype
);
27984 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27987 if (TREE_CODE (olddecl
) == TYPE_DECL
27988 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27990 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27991 = build_template_parm_index (index
, level
, level
,
27993 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27994 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27995 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27996 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
27997 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
27999 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28001 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28003 DECL_TEMPLATE_RESULT (newdecl
)
28004 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28005 DECL_NAME (olddecl
), newtype
);
28006 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28007 // First create a copy (ttargs) of tsubst_args with an
28008 // additional level for the template template parameter's own
28009 // template parameters (ttparms).
28010 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28011 (DECL_TEMPLATE_PARMS (olddecl
)));
28012 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28013 tree ttargs
= make_tree_vec (depth
+ 1);
28014 for (int i
= 0; i
< depth
; ++i
)
28015 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28016 TREE_VEC_ELT (ttargs
, depth
)
28017 = template_parms_level_to_args (ttparms
);
28018 // Substitute ttargs into ttparms to fix references to
28019 // other template parameters.
28020 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28021 complain
|tf_partial
);
28022 // Now substitute again with args based on tparms, to reduce
28023 // the level of the ttparms.
28024 ttargs
= current_template_args ();
28025 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28027 // Finally, tack the adjusted parms onto tparms.
28028 ttparms
= tree_cons (size_int (depth
), ttparms
,
28029 current_template_parms
);
28030 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28035 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28037 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28038 TREE_CODE (oldconst
),
28039 DECL_NAME (oldconst
), newtype
);
28040 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28041 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28042 SET_DECL_TEMPLATE_PARM_P (newconst
);
28043 newidx
= build_template_parm_index (index
, level
, level
,
28044 newconst
, newtype
);
28045 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28046 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28047 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28053 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28054 template parameter. */
28057 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28058 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28060 tree olddecl
= TREE_VALUE (oldelt
);
28061 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28063 if (newdecl
== error_mark_node
)
28064 return error_mark_node
;
28065 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28066 targs
, complain
, NULL_TREE
);
28067 tree list
= build_tree_list (newdef
, newdecl
);
28068 TEMPLATE_PARM_CONSTRAINTS (list
)
28069 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28070 targs
, complain
, NULL_TREE
);
28071 int depth
= TMPL_ARGS_DEPTH (targs
);
28072 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28076 /* Returns a C++17 class deduction guide template based on the constructor
28077 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28078 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28079 aggregate initialization guide. */
28082 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28084 tree tparms
, targs
, fparms
, fargs
, ci
;
28085 bool memtmpl
= false;
28088 tree fn_tmpl
= NULL_TREE
;
28092 ++processing_template_decl
;
28093 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28094 --processing_template_decl
;
28097 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28101 bool copy_p
= TYPE_REF_P (ctor
);
28103 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28105 fparms
= void_list_node
;
28107 else if (TREE_CODE (ctor
) == TREE_LIST
)
28110 gcc_unreachable ();
28112 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28113 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28114 targs
= CLASSTYPE_TI_ARGS (type
);
28117 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28118 explicit_p
= false;
28122 ++processing_template_decl
;
28126 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28127 : DECL_TI_TEMPLATE (ctor
));
28129 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28130 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28132 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28133 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28134 fully specialized args for the enclosing class. Strip those off, as
28135 the deduction guide won't have those template parameters. */
28136 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28137 TMPL_PARMS_DEPTH (tparms
));
28138 /* Discard the 'this' parameter. */
28139 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28140 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28141 ci
= get_constraints (ctor
);
28142 loc
= DECL_SOURCE_LOCATION (ctor
);
28143 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28145 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28149 /* For a member template constructor, we need to flatten the two
28150 template parameter lists into one, and then adjust the function
28151 signature accordingly. This gets...complicated. */
28152 tree save_parms
= current_template_parms
;
28154 /* For a member template we should have two levels of parms/args, one
28155 for the class and one for the constructor. We stripped
28156 specialized args for further enclosing classes above. */
28157 const int depth
= 2;
28158 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28160 /* Template args for translating references to the two-level template
28161 parameters into references to the one-level template parameters we
28163 tree tsubst_args
= copy_node (targs
);
28164 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28165 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28167 /* Template parms for the constructor template. */
28168 tree ftparms
= TREE_VALUE (tparms
);
28169 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28170 /* Template parms for the class template. */
28171 tparms
= TREE_CHAIN (tparms
);
28172 tree ctparms
= TREE_VALUE (tparms
);
28173 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28174 /* Template parms for the deduction guide start as a copy of the
28175 template parms for the class. We set current_template_parms for
28176 lookup_template_class_1. */
28177 current_template_parms
= tparms
= copy_node (tparms
);
28178 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28179 for (unsigned i
= 0; i
< clen
; ++i
)
28180 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28182 /* Now we need to rewrite the constructor parms to append them to the
28184 for (unsigned i
= 0; i
< flen
; ++i
)
28186 unsigned index
= i
+ clen
;
28187 unsigned level
= 1;
28188 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28190 = rewrite_tparm_list (oldelt
, index
, level
,
28191 tsubst_args
, i
, complain
);
28192 if (newelt
== error_mark_node
)
28194 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28197 /* Now we have a final set of template parms to substitute into the
28198 function signature. */
28199 targs
= template_parms_to_args (tparms
);
28200 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28202 if (fparms
== error_mark_node
)
28205 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28207 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28208 cp_unevaluated_operand. */
28210 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28211 current_template_parms
= save_parms
;
28214 --processing_template_decl
;
28216 return error_mark_node
;
28221 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28222 tparms
= copy_node (tparms
);
28223 INNERMOST_TEMPLATE_PARMS (tparms
)
28224 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28227 tree fntype
= build_function_type (type
, fparms
);
28228 tree ded_fn
= build_lang_decl_loc (loc
,
28230 dguide_name (type
), fntype
);
28231 DECL_ARGUMENTS (ded_fn
) = fargs
;
28232 DECL_ARTIFICIAL (ded_fn
) = true;
28233 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28234 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28235 DECL_ARTIFICIAL (ded_tmpl
) = true;
28236 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28237 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28239 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28241 set_constraints (ded_tmpl
, ci
);
28246 /* Add to LIST the member types for the reshaped initializer CTOR. */
28249 collect_ctor_idx_types (tree ctor
, tree list
)
28251 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28252 tree idx
, val
; unsigned i
;
28253 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28255 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28256 && CONSTRUCTOR_NELTS (val
))
28257 if (tree subidx
= CONSTRUCTOR_ELT (val
, 0)->index
)
28258 if (TREE_CODE (subidx
) == FIELD_DECL
)
28260 list
= collect_ctor_idx_types (val
, list
);
28263 tree ftype
= finish_decltype_type (idx
, true, tf_none
);
28264 list
= tree_cons (NULL_TREE
, ftype
, list
);
28270 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28273 is_spec_or_derived (tree etype
, tree tmpl
)
28275 if (!etype
|| !CLASS_TYPE_P (etype
))
28278 tree type
= TREE_TYPE (tmpl
);
28279 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28280 (DECL_TEMPLATE_PARMS (tmpl
)));
28281 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28282 int err
= unify (tparms
, targs
, type
, etype
,
28283 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28288 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28292 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28294 if (cxx_dialect
< cxx20
)
28297 if (init
== NULL_TREE
)
28300 tree type
= TREE_TYPE (tmpl
);
28301 if (!CP_AGGREGATE_TYPE_P (type
))
28304 /* No aggregate candidate for copy-initialization. */
28305 if (args
->length() == 1)
28307 tree val
= (*args
)[0];
28308 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28312 /* If we encounter a problem, we just won't add the candidate. */
28313 tsubst_flags_t complain
= tf_none
;
28315 tree parms
= NULL_TREE
;
28316 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28318 init
= reshape_init (type
, init
, complain
);
28319 if (init
== error_mark_node
)
28321 parms
= collect_ctor_idx_types (init
, parms
);
28323 else if (TREE_CODE (init
) == TREE_LIST
)
28325 int len
= list_length (init
);
28326 for (tree field
= TYPE_FIELDS (type
);
28328 --len
, field
= DECL_CHAIN (field
))
28330 field
= next_initializable_field (field
);
28333 tree ftype
= finish_decltype_type (field
, true, complain
);
28334 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28338 /* Aggregate initialization doesn't apply to an initializer expression. */
28344 parms
= nreverse (parms
);
28345 TREE_CHAIN (last
) = void_list_node
;
28346 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28353 /* UGUIDES are the deduction guides for the underlying template of alias
28354 template TMPL; adjust them to be deduction guides for TMPL. */
28357 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28359 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28360 class type (9.2.8.2) where the template-name names an alias template A,
28361 the defining-type-id of A must be of the form
28363 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28365 as specified in 9.2.8.2. The guides of A are the set of functions or
28366 function templates formed as follows. For each function or function
28367 template f in the guides of the template named by the simple-template-id
28368 of the defining-type-id, the template arguments of the return type of f
28369 are deduced from the defining-type-id of A according to the process in
28370 13.10.2.5 with the exception that deduction does not fail if not all
28371 template arguments are deduced. Let g denote the result of substituting
28372 these deductions into f. If substitution succeeds, form a function or
28373 function template f' with the following properties and add it to the set
28376 * The function type of f' is the function type of g.
28378 * If f is a function template, f' is a function template whose template
28379 parameter list consists of all the template parameters of A (including
28380 their default template arguments) that appear in the above deductions or
28381 (recursively) in their default template arguments, followed by the
28382 template parameters of f that were not deduced (including their default
28383 template arguments), otherwise f' is not a function template.
28385 * The associated constraints (13.5.2) are the conjunction of the
28386 associated constraints of g and a constraint that is satisfied if and only
28387 if the arguments of A are deducible (see below) from the return type.
28389 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28392 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28393 considered to be so as well.
28395 * The explicit-specifier of f' is the explicit-specifier of g (if
28398 /* This implementation differs from the above in two significant ways:
28400 1) We include all template parameters of A, not just some.
28401 2) The added constraint is same_type instead of deducible.
28403 I believe that while it's probably possible to construct a testcase that
28404 behaves differently with this simplification, it should have the same
28405 effect for real uses. Including all template parameters means that we
28406 deduce all parameters of A when resolving the call, so when we're in the
28407 constraint we don't need to deduce them again, we can just check whether
28408 the deduction produced the desired result. */
28410 tsubst_flags_t complain
= tf_warning_or_error
;
28411 tree atype
= TREE_TYPE (tmpl
);
28412 tree aguides
= NULL_TREE
;
28413 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28414 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28415 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28416 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28420 location_t loc
= DECL_SOURCE_LOCATION (f
);
28421 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28423 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28425 processing_template_decl_sentinel
ptds (/*reset*/false);
28426 ++processing_template_decl
;
28428 /* Deduce template arguments for f from the type-id of A. */
28429 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28430 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28431 tree targs
= make_tree_vec (len
);
28432 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28435 /* The number of parms for f' is the number of parms for A plus
28436 non-deduced parms of f. */
28437 unsigned ndlen
= 0;
28439 for (unsigned i
= 0; i
< len
; ++i
)
28440 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28442 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28444 /* First copy over the parms of A. */
28445 for (j
= 0; j
< natparms
; ++j
)
28446 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28447 /* Now rewrite the non-deduced parms of f. */
28448 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28449 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28452 unsigned index
= j
++;
28453 unsigned level
= 1;
28454 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28455 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28456 targs
, i
, complain
);
28457 TREE_VEC_ELT (gtparms
, index
) = list
;
28459 gtparms
= build_tree_list (size_one_node
, gtparms
);
28461 /* Substitute the deduced arguments plus the rewritten template
28462 parameters into f to get g. This covers the type, copyness,
28463 guideness, and explicit-specifier. */
28464 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28465 if (g
== error_mark_node
)
28466 return error_mark_node
;
28467 DECL_USE_TEMPLATE (g
) = 0;
28468 fprime
= build_template_decl (g
, gtparms
, false);
28469 DECL_TEMPLATE_RESULT (fprime
) = g
;
28470 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28471 tree gtargs
= template_parms_to_args (gtparms
);
28472 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28473 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28475 /* Substitute the associated constraints. */
28476 tree ci
= get_constraints (f
);
28478 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28479 if (ci
== error_mark_node
)
28480 return error_mark_node
;
28482 /* Add a constraint that the return type matches the instantiation of
28483 A with the same template arguments. */
28484 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28485 if (!same_type_p (atype
, ret
)
28486 /* FIXME this should mean they don't compare as equivalent. */
28487 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28489 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28490 ci
= append_constraint (ci
, same
);
28494 set_constraints (fprime
, ci
);
28498 /* For a non-template deduction guide, if the arguments of A aren't
28499 deducible from the return type, don't add the candidate. */
28500 tree targs
= make_tree_vec (natparms
);
28501 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28502 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28503 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28509 aguides
= lookup_add (fprime
, aguides
);
28515 /* Return artificial deduction guides built from the constructors of class
28519 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28521 tree type
= TREE_TYPE (tmpl
);
28522 tree outer_args
= NULL_TREE
;
28523 if (DECL_CLASS_SCOPE_P (tmpl
)
28524 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28526 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28527 type
= TREE_TYPE (most_general_template (tmpl
));
28530 tree cands
= NULL_TREE
;
28532 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28534 /* Skip inherited constructors. */
28535 if (iter
.using_p ())
28538 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28539 cands
= lookup_add (guide
, cands
);
28542 /* Add implicit default constructor deduction guide. */
28543 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28545 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28547 cands
= lookup_add (guide
, cands
);
28550 /* Add copy guide. */
28552 tree gtype
= build_reference_type (type
);
28553 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28555 cands
= lookup_add (guide
, cands
);
28561 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28563 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28564 aggregate candidate is added separately because it depends on the
28568 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28570 tree guides
= NULL_TREE
;
28571 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28573 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28574 tree tinfo
= get_template_info (under
);
28575 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28579 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28580 dguide_name (tmpl
),
28581 /*type*/false, /*complain*/false,
28583 if (guides
== error_mark_node
)
28584 guides
= NULL_TREE
;
28587 /* Cache the deduction guides for a template. We also remember the result of
28588 lookup, and rebuild everything if it changes; should be very rare. */
28589 tree_pair_p cache
= NULL
;
28591 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28594 if (cache
->purpose
== guides
)
28595 return cache
->value
;
28599 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28600 cache
->purpose
= guides
;
28603 tree cands
= NULL_TREE
;
28604 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28605 cands
= alias_ctad_tweaks (tmpl
, guides
);
28608 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28609 for (ovl_iterator
it (guides
); it
; ++it
)
28610 cands
= lookup_add (*it
, cands
);
28613 cache
->value
= cands
;
28617 /* Return whether TMPL is a (class template argument-) deducible template. */
28620 ctad_template_p (tree tmpl
)
28622 /* A deducible template is either a class template or is an alias template
28623 whose defining-type-id is of the form
28625 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28627 where the nested-name-specifier (if any) is non-dependent and the
28628 template-name of the simple-template-id names a deducible template. */
28630 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28631 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28633 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28635 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28636 if (tree tinfo
= get_template_info (orig
))
28637 return ctad_template_p (TI_TEMPLATE (tinfo
));
28641 /* Deduce template arguments for the class template placeholder PTYPE for
28642 template TMPL based on the initializer INIT, and return the resulting
28646 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28647 int flags
, tsubst_flags_t complain
)
28649 /* We should have handled this in the caller. */
28650 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28653 /* Look through alias templates that just rename another template. */
28654 tmpl
= get_underlying_template (tmpl
);
28655 if (!ctad_template_p (tmpl
))
28657 if (complain
& tf_error
)
28658 error ("non-deducible template %qT used without template arguments", tmpl
);
28659 return error_mark_node
;
28661 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28663 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28664 if (complain
& tf_warning_or_error
)
28665 pedwarn (input_location
, 0, "alias template deduction only available "
28666 "with %<-std=c++20%> or %<-std=gnu++20%>");
28669 if (init
&& TREE_TYPE (init
) == ptype
)
28670 /* Using the template parm as its own argument. */
28673 tree type
= TREE_TYPE (tmpl
);
28675 bool try_list_ctor
= false;
28677 releasing_vec rv_args
= NULL
;
28678 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28679 if (init
== NULL_TREE
)
28680 args
= make_tree_vector ();
28681 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28683 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28684 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28686 /* As an exception, the first phase in 16.3.1.7 (considering the
28687 initializer list as a single argument) is omitted if the
28688 initializer list consists of a single expression of type cv U,
28689 where U is a specialization of C or a class derived from a
28690 specialization of C. */
28691 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28692 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28693 try_list_ctor
= false;
28695 if (try_list_ctor
|| is_std_init_list (type
))
28696 args
= make_tree_vector_single (init
);
28698 args
= make_tree_vector_from_ctor (init
);
28700 else if (TREE_CODE (init
) == TREE_LIST
)
28701 args
= make_tree_vector_from_list (init
);
28703 args
= make_tree_vector_single (init
);
28705 /* Do this now to avoid problems with erroneous args later on. */
28706 args
= resolve_args (args
, complain
);
28708 return error_mark_node
;
28710 tree cands
= deduction_guides_for (tmpl
, complain
);
28711 if (cands
== error_mark_node
)
28712 return error_mark_node
;
28714 /* Prune explicit deduction guides in copy-initialization context. */
28715 bool elided
= false;
28716 if (flags
& LOOKUP_ONLYCONVERTING
)
28718 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28719 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28724 /* Found a nonconverting guide, prune the candidates. */
28725 tree pruned
= NULL_TREE
;
28726 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28727 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28728 pruned
= lookup_add (*iter
, pruned
);
28734 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
28735 cands
= lookup_add (guide
, cands
);
28737 tree call
= error_mark_node
;
28739 /* If this is list-initialization and the class has a list constructor, first
28740 try deducing from the list as a single argument, as [over.match.list]. */
28741 tree list_cands
= NULL_TREE
;
28742 if (try_list_ctor
&& cands
)
28743 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28746 if (is_list_ctor (dg
))
28747 list_cands
= lookup_add (dg
, list_cands
);
28751 ++cp_unevaluated_operand
;
28752 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28753 --cp_unevaluated_operand
;
28755 if (call
== error_mark_node
)
28757 /* That didn't work, now try treating the list as a sequence of
28759 release_tree_vector (args
);
28760 args
= make_tree_vector_from_ctor (init
);
28764 if (elided
&& !cands
)
28766 error ("cannot deduce template arguments for copy-initialization"
28767 " of %qT, as it has no non-explicit deduction guides or "
28768 "user-declared constructors", type
);
28769 return error_mark_node
;
28771 else if (!cands
&& call
== error_mark_node
)
28773 error ("cannot deduce template arguments of %qT, as it has no viable "
28774 "deduction guides", type
);
28775 return error_mark_node
;
28778 if (call
== error_mark_node
)
28780 ++cp_unevaluated_operand
;
28781 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28782 --cp_unevaluated_operand
;
28785 if (call
== error_mark_node
28786 && (complain
& tf_warning_or_error
))
28788 error ("class template argument deduction failed:");
28790 ++cp_unevaluated_operand
;
28791 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28792 --cp_unevaluated_operand
;
28795 inform (input_location
, "explicit deduction guides not considered "
28796 "for copy-initialization");
28799 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28802 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28803 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28804 The CONTEXT determines the context in which auto deduction is performed
28805 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28806 OUTER_TARGS are used during template argument deduction
28807 (context == adc_unify) to properly substitute the result, and is ignored
28810 For partial-concept-ids, extra args may be appended to the list of deduced
28811 template arguments prior to determining constraint satisfaction. */
28814 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28815 tsubst_flags_t complain
, auto_deduction_context context
,
28816 tree outer_targs
, int flags
)
28820 if (init
== error_mark_node
)
28821 return error_mark_node
;
28823 if (init
&& type_dependent_expression_p (init
)
28824 && context
!= adc_unify
)
28825 /* Defining a subset of type-dependent expressions that we can deduce
28826 from ahead of time isn't worth the trouble. */
28829 /* Similarly, we can't deduce from another undeduced decl. */
28830 if (init
&& undeduced_auto_decl (init
))
28833 /* We may be doing a partial substitution, but we still want to replace
28835 complain
&= ~tf_partial
;
28837 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28838 /* C++17 class template argument deduction. */
28839 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28841 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28842 /* Nothing we can do with this, even in deduction context. */
28845 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28846 with either a new invented type template parameter U or, if the
28847 initializer is a braced-init-list (8.5.4), with
28848 std::initializer_list<U>. */
28849 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28851 if (!DIRECT_LIST_INIT_P (init
))
28852 type
= listify_autos (type
, auto_node
);
28853 else if (CONSTRUCTOR_NELTS (init
) == 1)
28854 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28857 if (complain
& tf_warning_or_error
)
28859 if (permerror (input_location
, "direct-list-initialization of "
28860 "%<auto%> requires exactly one element"))
28861 inform (input_location
,
28862 "for deduction to %<std::initializer_list%>, use copy-"
28863 "list-initialization (i.e. add %<=%> before the %<{%>)");
28865 type
= listify_autos (type
, auto_node
);
28869 if (type
== error_mark_node
)
28870 return error_mark_node
;
28872 init
= resolve_nondeduced_context (init
, complain
);
28874 if (context
== adc_decomp_type
28875 && auto_node
== type
28876 && init
!= error_mark_node
28877 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
28878 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28879 and initializer has array type, deduce cv-qualified array type. */
28880 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
28882 else if (AUTO_IS_DECLTYPE (auto_node
))
28884 tree stripped_init
= tree_strip_any_location_wrapper (init
);
28885 bool id
= (DECL_P (stripped_init
)
28886 || ((TREE_CODE (init
) == COMPONENT_REF
28887 || TREE_CODE (init
) == SCOPE_REF
)
28888 && !REF_PARENTHESIZED_P (init
)));
28889 targs
= make_tree_vec (1);
28890 TREE_VEC_ELT (targs
, 0)
28891 = finish_decltype_type (init
, id
, tf_warning_or_error
);
28892 if (type
!= auto_node
)
28894 if (complain
& tf_error
)
28895 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
28896 return error_mark_node
;
28901 if (error_operand_p (init
))
28902 return error_mark_node
;
28904 tree parms
= build_tree_list (NULL_TREE
, type
);
28908 tparms
= extract_autos (type
);
28911 tparms
= make_tree_vec (1);
28912 TREE_VEC_ELT (tparms
, 0)
28913 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
28916 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28917 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28919 NULL
, /*explain_p=*/false);
28922 if (processing_template_decl
)
28923 /* Try again at instantiation time. */
28925 if (type
&& type
!= error_mark_node
28926 && (complain
& tf_error
))
28927 /* If type is error_mark_node a diagnostic must have been
28928 emitted by now. Also, having a mention to '<type error>'
28929 in the diagnostic is not really useful to the user. */
28932 && FNDECL_USED_AUTO (current_function_decl
)
28934 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
28935 && LAMBDA_FUNCTION_P (current_function_decl
))
28936 error ("unable to deduce lambda return type from %qE", init
);
28938 error ("unable to deduce %qT from %qE", type
, init
);
28939 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28941 NULL
, /*explain_p=*/true);
28943 return error_mark_node
;
28947 /* Check any placeholder constraints against the deduced type. */
28948 if (flag_concepts
&& !processing_template_decl
)
28949 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
28951 /* Use the deduced type to check the associated constraints. If we
28952 have a partial-concept-id, rebuild the argument list so that
28953 we check using the extra arguments. */
28954 check
= unpack_concept_check (check
);
28955 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
28956 tree
cdecl = TREE_OPERAND (check
, 0);
28958 cdecl = OVL_FIRST (cdecl);
28959 tree cargs
= TREE_OPERAND (check
, 1);
28960 if (TREE_VEC_LENGTH (cargs
) > 1)
28962 cargs
= copy_node (cargs
);
28963 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
28968 /* Rebuild the check using the deduced arguments. */
28969 check
= build_concept_check (cdecl, cargs
, tf_none
);
28971 if (!constraints_satisfied_p (check
))
28973 if (complain
& tf_warning_or_error
)
28975 auto_diagnostic_group d
;
28978 case adc_unspecified
:
28980 error("placeholder constraints not satisfied");
28982 case adc_variable_type
:
28983 case adc_decomp_type
:
28984 error ("deduced initializer does not satisfy "
28985 "placeholder constraints");
28987 case adc_return_type
:
28988 error ("deduced return type does not satisfy "
28989 "placeholder constraints");
28991 case adc_requirement
:
28992 error ("deduced expression type does not satisfy "
28993 "placeholder constraints");
28996 diagnose_constraints (input_location
, check
, targs
);
28998 return error_mark_node
;
29002 if (processing_template_decl
&& context
!= adc_unify
)
29003 outer_targs
= current_template_args ();
29004 targs
= add_to_template_args (outer_targs
, targs
);
29005 return tsubst (type
, targs
, complain
, NULL_TREE
);
29008 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29012 splice_late_return_type (tree type
, tree late_return_type
)
29014 if (late_return_type
)
29016 gcc_assert (is_auto (type
) || seen_error ());
29017 return late_return_type
;
29020 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29022 tree idx
= get_template_parm_index (*auto_node
);
29023 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29025 /* In an abbreviated function template we didn't know we were dealing
29026 with a function template when we saw the auto return type, so update
29027 it to have the correct level. */
29028 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29029 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29030 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29031 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29032 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29033 *auto_node
= new_auto
;
29039 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29040 'decltype(auto)' or a deduced class template. */
29043 is_auto (const_tree type
)
29045 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29046 && (TYPE_IDENTIFIER (type
) == auto_identifier
29047 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29053 /* for_each_template_parm callback for type_uses_auto. */
29056 is_auto_r (tree tp
, void */
*data*/
)
29058 return is_auto (tp
);
29061 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29062 a use of `auto'. Returns NULL_TREE otherwise. */
29065 type_uses_auto (tree type
)
29067 if (type
== NULL_TREE
)
29069 else if (flag_concepts
)
29071 /* The Concepts TS allows multiple autos in one type-specifier; just
29072 return the first one we find, do_auto_deduction will collect all of
29074 if (uses_template_parms (type
))
29075 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29076 /*visited*/NULL
, /*nondeduced*/false);
29080 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29086 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29087 concepts are enabled, auto is acceptable in template arguments, but
29088 only when TEMPL identifies a template class. Return TRUE if any
29089 such errors were reported. */
29092 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29094 /* If there were previous errors, nevermind. */
29095 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29098 /* If TMPL is an identifier, we're parsing and we can't tell yet
29099 whether TMPL is supposed to be a type, a function or a variable.
29100 We'll only be able to tell during template substitution, so we
29101 expect to be called again then. If concepts are enabled and we
29102 know we have a type, we're ok. */
29104 && (identifier_p (tmpl
)
29106 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29107 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29110 /* Quickly search for any occurrences of auto; usually there won't
29111 be any, and then we'll avoid allocating the vector. */
29112 if (!type_uses_auto (args
))
29115 bool errors
= false;
29117 tree vec
= extract_autos (args
);
29118 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29120 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29121 error_at (DECL_SOURCE_LOCATION (xauto
),
29122 "invalid use of %qT in template argument", xauto
);
29129 /* For a given template T, return the vector of typedefs referenced
29130 in T for which access check is needed at T instantiation time.
29131 T is either a FUNCTION_DECL or a RECORD_TYPE.
29132 Those typedefs were added to T by the function
29133 append_type_to_template_for_access_check. */
29135 vec
<qualified_typedef_usage_t
, va_gc
> *
29136 get_types_needing_access_check (tree t
)
29138 gcc_checking_assert ((CLASS_TYPE_P (t
) || TREE_CODE (t
) == FUNCTION_DECL
));
29140 if (tree ti
= get_template_info (t
))
29141 if (TI_TEMPLATE (ti
))
29142 return TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
29147 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29148 tied to T. That list of typedefs will be access checked at
29149 T instantiation time.
29150 T is either a FUNCTION_DECL or a RECORD_TYPE.
29151 TYPE_DECL is a TYPE_DECL node representing a typedef.
29152 SCOPE is the scope through which TYPE_DECL is accessed.
29153 LOCATION is the location of the usage point of TYPE_DECL.
29155 This function is a subroutine of
29156 append_type_to_template_for_access_check. */
29159 append_type_to_template_for_access_check_1 (tree t
,
29162 location_t location
)
29164 qualified_typedef_usage_t typedef_usage
;
29167 if (!t
|| t
== error_mark_node
)
29170 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
29171 || CLASS_TYPE_P (t
))
29173 && TREE_CODE (type_decl
) == TYPE_DECL
29176 if (!(ti
= get_template_info (t
)))
29179 gcc_assert (TI_TEMPLATE (ti
));
29181 typedef_usage
.typedef_decl
= type_decl
;
29182 typedef_usage
.context
= scope
;
29183 typedef_usage
.locus
= location
;
29185 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
29188 /* Append TYPE_DECL to the template TEMPL.
29189 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29190 At TEMPL instanciation time, TYPE_DECL will be checked to see
29191 if it can be accessed through SCOPE.
29192 LOCATION is the location of the usage point of TYPE_DECL.
29194 e.g. consider the following code snippet:
29201 template<class U> struct S
29203 C::myint mi; // <-- usage point of the typedef C::myint
29208 At S<char> instantiation time, we need to check the access of C::myint
29209 In other words, we need to check the access of the myint typedef through
29210 the C scope. For that purpose, this function will add the myint typedef
29211 and the scope C through which its being accessed to a list of typedefs
29212 tied to the template S. That list will be walked at template instantiation
29213 time and access check performed on each typedefs it contains.
29214 Note that this particular code snippet should yield an error because
29215 myint is private to C. */
29218 append_type_to_template_for_access_check (tree templ
,
29221 location_t location
)
29223 qualified_typedef_usage_t
*iter
;
29226 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
29228 /* Make sure we don't append the type to the template twice. */
29229 if (vec
<qualified_typedef_usage_t
, va_gc
> *tdefs
29230 = get_types_needing_access_check (templ
))
29231 FOR_EACH_VEC_ELT (*tdefs
, i
, iter
)
29232 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
29235 append_type_to_template_for_access_check_1 (templ
, type_decl
,
29239 /* Recursively walk over && expressions searching for EXPR. Return a reference
29240 to that expression. */
29242 static tree
*find_template_requirement (tree
*t
, tree key
)
29246 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29248 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29250 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29256 /* Convert the generic type parameters in PARM that match the types given in the
29257 range [START_IDX, END_IDX) from the current_template_parms into generic type
29261 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29263 tree current
= current_template_parms
;
29264 int depth
= TMPL_PARMS_DEPTH (current
);
29265 current
= INNERMOST_TEMPLATE_PARMS (current
);
29266 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29268 for (int i
= 0; i
< start_idx
; ++i
)
29269 TREE_VEC_ELT (replacement
, i
)
29270 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29272 for (int i
= start_idx
; i
< end_idx
; ++i
)
29274 /* Create a distinct parameter pack type from the current parm and add it
29275 to the replacement args to tsubst below into the generic function
29277 tree node
= TREE_VEC_ELT (current
, i
);
29278 tree o
= TREE_TYPE (TREE_VALUE (node
));
29279 tree t
= copy_type (o
);
29280 TEMPLATE_TYPE_PARM_INDEX (t
)
29281 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29283 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29284 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29285 TYPE_MAIN_VARIANT (t
) = t
;
29286 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29287 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29288 TREE_VEC_ELT (replacement
, i
) = t
;
29290 /* Replace the current template parameter with new pack. */
29291 TREE_VALUE (node
) = TREE_CHAIN (t
);
29293 /* Surgically adjust the associated constraint of adjusted parameter
29294 and it's corresponding contribution to the current template
29296 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29298 tree id
= unpack_concept_check (constr
);
29299 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
29300 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29301 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29303 /* If there was a constraint, we also need to replace that in
29304 the template requirements, which we've already built. */
29305 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29306 reqs
= find_template_requirement (reqs
, constr
);
29311 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29312 TREE_VEC_ELT (replacement
, i
)
29313 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29315 /* If there are more levels then build up the replacement with the outer
29318 replacement
= add_to_template_args (template_parms_to_args
29319 (TREE_CHAIN (current_template_parms
)),
29322 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29325 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29329 declare_integer_pack (void)
29331 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29332 build_function_type_list (integer_type_node
,
29335 NULL_TREE
, ECF_CONST
);
29336 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29337 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29338 CP_BUILT_IN_INTEGER_PACK
);
29341 /* Set up the hash tables for template instantiations. */
29344 init_template_processing (void)
29346 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29347 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29349 if (cxx_dialect
>= cxx11
)
29350 declare_integer_pack ();
29353 /* Print stats about the template hash tables for -fstats. */
29356 print_template_statistics (void)
29358 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29359 "%f collisions\n", (long) decl_specializations
->size (),
29360 (long) decl_specializations
->elements (),
29361 decl_specializations
->collisions ());
29362 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29363 "%f collisions\n", (long) type_specializations
->size (),
29364 (long) type_specializations
->elements (),
29365 type_specializations
->collisions ());
29370 namespace selftest
{
29372 /* Verify that build_non_dependent_expr () works, for various expressions,
29373 and that location wrappers don't affect the results. */
29376 test_build_non_dependent_expr ()
29378 location_t loc
= BUILTINS_LOCATION
;
29380 /* Verify constants, without and with location wrappers. */
29381 tree int_cst
= build_int_cst (integer_type_node
, 42);
29382 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29384 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29385 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29386 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29388 tree string_lit
= build_string (4, "foo");
29389 TREE_TYPE (string_lit
) = char_array_type_node
;
29390 string_lit
= fix_string_type (string_lit
);
29391 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29393 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29394 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29395 ASSERT_EQ (wrapped_string_lit
,
29396 build_non_dependent_expr (wrapped_string_lit
));
29399 /* Verify that type_dependent_expression_p () works correctly, even
29400 in the presence of location wrapper nodes. */
29403 test_type_dependent_expression_p ()
29405 location_t loc
= BUILTINS_LOCATION
;
29407 tree name
= get_identifier ("foo");
29409 /* If no templates are involved, nothing is type-dependent. */
29410 gcc_assert (!processing_template_decl
);
29411 ASSERT_FALSE (type_dependent_expression_p (name
));
29413 ++processing_template_decl
;
29415 /* Within a template, an unresolved name is always type-dependent. */
29416 ASSERT_TRUE (type_dependent_expression_p (name
));
29418 /* Ensure it copes with NULL_TREE and errors. */
29419 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29420 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29422 /* A USING_DECL in a template should be type-dependent, even if wrapped
29423 with a location wrapper (PR c++/83799). */
29424 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29425 TREE_TYPE (using_decl
) = integer_type_node
;
29426 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29427 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29428 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29429 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29431 --processing_template_decl
;
29434 /* Run all of the selftests within this file. */
29439 test_build_non_dependent_expr ();
29440 test_type_dependent_expression_p ();
29443 } // namespace selftest
29445 #endif /* #if CHECKING_P */
29447 #include "gt-cp-pt.h"