1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_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 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
121 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
123 /* Contains canonical template parameter types. The vector is indexed by
124 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
125 TREE_LIST, whose TREE_VALUEs contain the canonical template
126 parameters of various types and levels. */
127 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
129 #define UNIFY_ALLOW_NONE 0
130 #define UNIFY_ALLOW_MORE_CV_QUAL 1
131 #define UNIFY_ALLOW_LESS_CV_QUAL 2
132 #define UNIFY_ALLOW_DERIVED 4
133 #define UNIFY_ALLOW_INTEGER 8
134 #define UNIFY_ALLOW_OUTER_LEVEL 16
135 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
136 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
138 enum template_base_result
{
140 tbr_ambiguous_baseclass
,
144 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
145 unification_kind_t
, int,
147 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
148 unification_kind_t
, int, bool, bool);
149 static int unify (tree
, tree
, tree
, tree
, int, bool);
150 static void add_pending_template (tree
);
151 static tree
reopen_tinst_level (struct tinst_level
*);
152 static tree
tsubst_initializer_list (tree
, tree
);
153 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
154 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
156 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
158 static void tsubst_enum (tree
, tree
, tree
);
159 static tree
add_to_template_args (tree
, tree
);
160 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
161 static int check_non_deducible_conversion (tree
, tree
, int, int,
162 struct conversion
**, bool);
163 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
165 static int type_unification_real (tree
, tree
, tree
, const tree
*,
166 unsigned int, int, unification_kind_t
,
167 vec
<deferred_access_check
, va_gc
> **,
169 static void note_template_header (int);
170 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
171 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
172 static tree
convert_template_argument (tree
, tree
, tree
,
173 tsubst_flags_t
, int, tree
);
174 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
175 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
176 static tree
expand_template_argument_pack (tree
);
177 static tree
build_template_parm_index (int, int, int, tree
, tree
);
178 static bool inline_needs_template_parms (tree
, bool);
179 static void push_inline_template_parms_recursive (tree
, int);
180 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
181 static int mark_template_parm (tree
, void *);
182 static int template_parm_this_level_p (tree
, void *);
183 static tree
tsubst_friend_function (tree
, tree
);
184 static tree
tsubst_friend_class (tree
, tree
);
185 static int can_complete_type_without_circularity (tree
);
186 static tree
get_bindings (tree
, tree
, tree
, bool);
187 static int template_decl_level (tree
);
188 static int check_cv_quals_for_unify (int, tree
, tree
);
189 static int unify_pack_expansion (tree
, tree
, tree
,
190 tree
, unification_kind_t
, bool, bool);
191 static tree
copy_template_args (tree
);
192 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
193 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
194 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
195 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
197 static bool check_specialization_scope (void);
198 static tree
process_partial_specialization (tree
);
199 static void set_current_access_from_decl (tree
);
200 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
202 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
203 static bool class_nttp_const_wrapper_p (tree t
);
204 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
206 static bool template_template_parm_bindings_ok_p (tree
, tree
);
207 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
208 static tree
for_each_template_parm_r (tree
*, int *, void *);
209 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
210 static void copy_default_args_to_explicit_spec (tree
);
211 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
212 static bool dependent_template_arg_p (tree
);
213 static bool any_template_arguments_need_structural_equality_p (tree
);
214 static bool dependent_type_p_r (tree
);
215 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
216 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
217 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
218 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
220 static tree
listify (tree
);
221 static tree
listify_autos (tree
, tree
);
222 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
223 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
224 static bool complex_alias_template_p (const_tree tmpl
);
225 static tree
get_underlying_template (tree
);
226 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
227 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
228 static tree
make_argument_pack (tree
);
229 static void register_parameter_specializations (tree
, tree
);
230 static tree
enclosing_instantiation_of (tree tctx
);
232 /* Make the current scope suitable for access checking when we are
233 processing T. T can be FUNCTION_DECL for instantiated function
234 template, VAR_DECL for static member variable, or TYPE_DECL for
235 alias template (needed by instantiate_decl). */
238 push_access_scope (tree t
)
240 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
241 || TREE_CODE (t
) == TYPE_DECL
);
243 if (DECL_FRIEND_CONTEXT (t
))
244 push_nested_class (DECL_FRIEND_CONTEXT (t
));
245 else if (DECL_CLASS_SCOPE_P (t
))
246 push_nested_class (DECL_CONTEXT (t
));
248 push_to_top_level ();
250 if (TREE_CODE (t
) == FUNCTION_DECL
)
252 vec_safe_push (saved_access_scope
, current_function_decl
);
253 current_function_decl
= t
;
257 /* Restore the scope set up by push_access_scope. T is the node we
261 pop_access_scope (tree t
)
263 if (TREE_CODE (t
) == FUNCTION_DECL
)
264 current_function_decl
= saved_access_scope
->pop();
266 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
269 pop_from_top_level ();
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
278 finish_member_template_decl (tree decl
)
280 if (decl
== error_mark_node
)
281 return error_mark_node
;
283 gcc_assert (DECL_P (decl
));
285 if (TREE_CODE (decl
) == TYPE_DECL
)
289 type
= TREE_TYPE (decl
);
290 if (type
== error_mark_node
)
291 return error_mark_node
;
292 if (MAYBE_CLASS_TYPE_P (type
)
293 && CLASSTYPE_TEMPLATE_INFO (type
)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
296 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
297 check_member_template (tmpl
);
302 else if (TREE_CODE (decl
) == FIELD_DECL
)
303 error_at (DECL_SOURCE_LOCATION (decl
),
304 "data member %qD cannot be a member template", decl
);
305 else if (DECL_TEMPLATE_INFO (decl
))
307 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
309 check_member_template (DECL_TI_TEMPLATE (decl
));
310 return DECL_TI_TEMPLATE (decl
);
316 error_at (DECL_SOURCE_LOCATION (decl
),
317 "invalid member template declaration %qD", decl
);
319 return error_mark_node
;
322 /* Create a template info node. */
325 build_template_info (tree template_decl
, tree template_args
)
327 tree result
= make_node (TEMPLATE_INFO
);
328 TI_TEMPLATE (result
) = template_decl
;
329 TI_ARGS (result
) = template_args
;
333 /* Return the template info node corresponding to T, whatever T is. */
336 get_template_info (const_tree t
)
338 tree tinfo
= NULL_TREE
;
340 if (!t
|| t
== error_mark_node
)
343 if (TREE_CODE (t
) == NAMESPACE_DECL
344 || TREE_CODE (t
) == PARM_DECL
)
347 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
348 tinfo
= DECL_TEMPLATE_INFO (t
);
350 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
353 if (OVERLOAD_TYPE_P (t
))
354 tinfo
= TYPE_TEMPLATE_INFO (t
);
355 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
356 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
361 /* Returns the template nesting level of the indicated class TYPE.
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
380 template_class_depth (tree type
)
384 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
386 tree tinfo
= get_template_info (type
);
388 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
394 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
397 type
= CP_DECL_CONTEXT (type
);
399 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
400 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
402 type
= CP_TYPE_CONTEXT (type
);
408 /* Return TRUE if NODE instantiates a template that has arguments of
409 its own, be it directly a primary template or indirectly through a
410 partial specializations. */
412 instantiates_primary_template_p (tree node
)
414 tree tinfo
= get_template_info (node
);
418 tree tmpl
= TI_TEMPLATE (tinfo
);
419 if (PRIMARY_TEMPLATE_P (tmpl
))
422 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
425 /* So now we know we have a specialization, but it could be a full
426 or a partial specialization. To tell which, compare the depth of
427 its template arguments with those of its context. */
429 tree ctxt
= DECL_CONTEXT (tmpl
);
430 tree ctinfo
= get_template_info (ctxt
);
434 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
435 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
438 /* Subroutine of maybe_begin_member_template_processing.
439 Returns true if processing DECL needs us to push template parms. */
442 inline_needs_template_parms (tree decl
, bool nsdmi
)
444 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
447 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
448 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
451 /* Subroutine of maybe_begin_member_template_processing.
452 Push the template parms in PARMS, starting from LEVELS steps into the
453 chain, and ending at the beginning, since template parms are listed
457 push_inline_template_parms_recursive (tree parmlist
, int levels
)
459 tree parms
= TREE_VALUE (parmlist
);
463 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
465 ++processing_template_decl
;
466 current_template_parms
467 = tree_cons (size_int (processing_template_decl
),
468 parms
, current_template_parms
);
469 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
471 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
473 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
475 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
477 if (error_operand_p (parm
))
480 gcc_assert (DECL_P (parm
));
482 switch (TREE_CODE (parm
))
490 /* Push the CONST_DECL. */
491 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
500 /* Restore the template parameter context for a member template, a
501 friend template defined in a class definition, or a non-template
502 member of template class. */
505 maybe_begin_member_template_processing (tree decl
)
509 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
513 tree ctx
= DECL_CONTEXT (decl
);
514 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
515 /* Disregard full specializations (c++/60999). */
516 && uses_template_parms (ctx
)
517 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
520 if (inline_needs_template_parms (decl
, nsdmi
))
522 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
523 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
525 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
528 parms
= TREE_CHAIN (parms
);
531 push_inline_template_parms_recursive (parms
, levels
);
534 /* Remember how many levels of template parameters we pushed so that
535 we can pop them later. */
536 inline_parm_levels
.safe_push (levels
);
539 /* Undo the effects of maybe_begin_member_template_processing. */
542 maybe_end_member_template_processing (void)
547 if (inline_parm_levels
.length () == 0)
550 last
= inline_parm_levels
.pop ();
551 for (i
= 0; i
< last
; ++i
)
553 --processing_template_decl
;
554 current_template_parms
= TREE_CHAIN (current_template_parms
);
559 /* Return a new template argument vector which contains all of ARGS,
560 but has as its innermost set of arguments the EXTRA_ARGS. */
563 add_to_template_args (tree args
, tree extra_args
)
570 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
573 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
574 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
576 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
577 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
579 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
580 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
585 /* Like add_to_template_args, but only the outermost ARGS are added to
586 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
587 (EXTRA_ARGS) levels are added. This function is used to combine
588 the template arguments from a partial instantiation with the
589 template arguments used to attain the full instantiation from the
590 partial instantiation. */
593 add_outermost_template_args (tree args
, tree extra_args
)
597 /* If there are more levels of EXTRA_ARGS than there are ARGS,
598 something very fishy is going on. */
599 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
601 /* If *all* the new arguments will be the EXTRA_ARGS, just return
603 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
606 /* For the moment, we make ARGS look like it contains fewer levels. */
607 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
609 new_args
= add_to_template_args (args
, extra_args
);
611 /* Now, we restore ARGS to its full dimensions. */
612 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
617 /* Return the N levels of innermost template arguments from the ARGS. */
620 get_innermost_template_args (tree args
, int n
)
628 /* If N is 1, just return the innermost set of template arguments. */
630 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
632 /* If we're not removing anything, just return the arguments we were
634 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
635 gcc_assert (extra_levels
>= 0);
636 if (extra_levels
== 0)
639 /* Make a new set of arguments, not containing the outer arguments. */
640 new_args
= make_tree_vec (n
);
641 for (i
= 1; i
<= n
; ++i
)
642 SET_TMPL_ARGS_LEVEL (new_args
, i
,
643 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
648 /* The inverse of get_innermost_template_args: Return all but the innermost
649 EXTRA_LEVELS levels of template arguments from the ARGS. */
652 strip_innermost_template_args (tree args
, int extra_levels
)
655 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
660 /* If N is 1, just return the outermost set of template arguments. */
662 return TMPL_ARGS_LEVEL (args
, 1);
664 /* If we're not removing anything, just return the arguments we were
666 gcc_assert (extra_levels
>= 0);
667 if (extra_levels
== 0)
670 /* Make a new set of arguments, not containing the inner arguments. */
671 new_args
= make_tree_vec (n
);
672 for (i
= 1; i
<= n
; ++i
)
673 SET_TMPL_ARGS_LEVEL (new_args
, i
,
674 TMPL_ARGS_LEVEL (args
, i
));
679 /* We've got a template header coming up; push to a new level for storing
683 begin_template_parm_list (void)
685 /* We use a non-tag-transparent scope here, which causes pushtag to
686 put tags in this scope, rather than in the enclosing class or
687 namespace scope. This is the right thing, since we want
688 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
689 global template class, push_template_decl handles putting the
690 TEMPLATE_DECL into top-level scope. For a nested template class,
693 template <class T> struct S1 {
694 template <class T> struct S2 {};
697 pushtag contains special code to insert the TEMPLATE_DECL for S2
698 at the right scope. */
699 begin_scope (sk_template_parms
, NULL
);
700 ++processing_template_decl
;
701 ++processing_template_parmlist
;
702 note_template_header (0);
704 /* Add a dummy parameter level while we process the parameter list. */
705 current_template_parms
706 = tree_cons (size_int (processing_template_decl
),
708 current_template_parms
);
711 /* This routine is called when a specialization is declared. If it is
712 invalid to declare a specialization here, an error is reported and
713 false is returned, otherwise this routine will return true. */
716 check_specialization_scope (void)
718 tree scope
= current_scope ();
722 An explicit specialization shall be declared in the namespace of
723 which the template is a member, or, for member templates, in the
724 namespace of which the enclosing class or enclosing class
725 template is a member. An explicit specialization of a member
726 function, member class or static data member of a class template
727 shall be declared in the namespace of which the class template
729 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
731 error ("explicit specialization in non-namespace scope %qD", scope
);
737 In an explicit specialization declaration for a member of a class
738 template or a member template that appears in namespace scope,
739 the member template and some of its enclosing class templates may
740 remain unspecialized, except that the declaration shall not
741 explicitly specialize a class member template if its enclosing
742 class templates are not explicitly specialized as well. */
743 if (current_template_parms
)
745 error ("enclosing class templates are not explicitly specialized");
752 /* We've just seen template <>. */
755 begin_specialization (void)
757 begin_scope (sk_template_spec
, NULL
);
758 note_template_header (1);
759 return check_specialization_scope ();
762 /* Called at then end of processing a declaration preceded by
766 end_specialization (void)
769 reset_specialization ();
772 /* Any template <>'s that we have seen thus far are not referring to a
773 function specialization. */
776 reset_specialization (void)
778 processing_specialization
= 0;
779 template_header_count
= 0;
782 /* We've just seen a template header. If SPECIALIZATION is nonzero,
783 it was of the form template <>. */
786 note_template_header (int specialization
)
788 processing_specialization
= specialization
;
789 template_header_count
++;
792 /* We're beginning an explicit instantiation. */
795 begin_explicit_instantiation (void)
797 gcc_assert (!processing_explicit_instantiation
);
798 processing_explicit_instantiation
= true;
803 end_explicit_instantiation (void)
805 gcc_assert (processing_explicit_instantiation
);
806 processing_explicit_instantiation
= false;
809 /* An explicit specialization or partial specialization of TMPL is being
810 declared. Check that the namespace in which the specialization is
811 occurring is permissible. Returns false iff it is invalid to
812 specialize TMPL in the current namespace. */
815 check_specialization_namespace (tree tmpl
)
817 tree tpl_ns
= decl_namespace_context (tmpl
);
821 An explicit specialization shall be declared in a namespace enclosing the
822 specialized template. An explicit specialization whose declarator-id is
823 not qualified shall be declared in the nearest enclosing namespace of the
824 template, or, if the namespace is inline (7.3.1), any namespace from its
825 enclosing namespace set. */
826 if (current_scope() != DECL_CONTEXT (tmpl
)
827 && !at_namespace_scope_p ())
829 error ("specialization of %qD must appear at namespace scope", tmpl
);
833 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
834 /* Same or enclosing namespace. */
838 auto_diagnostic_group d
;
839 if (permerror (input_location
,
840 "specialization of %qD in different namespace", tmpl
))
841 inform (DECL_SOURCE_LOCATION (tmpl
),
842 " from definition of %q#D", tmpl
);
847 /* SPEC is an explicit instantiation. Check that it is valid to
848 perform this explicit instantiation in the current namespace. */
851 check_explicit_instantiation_namespace (tree spec
)
855 /* DR 275: An explicit instantiation shall appear in an enclosing
856 namespace of its template. */
857 ns
= decl_namespace_context (spec
);
858 if (!is_nested_namespace (current_namespace
, ns
))
859 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
860 "(which does not enclose namespace %qD)",
861 spec
, current_namespace
, ns
);
864 /* Returns the type of a template specialization only if that
865 specialization needs to be defined. Otherwise (e.g., if the type has
866 already been defined), the function returns NULL_TREE. */
869 maybe_new_partial_specialization (tree type
)
871 /* An implicit instantiation of an incomplete type implies
872 the definition of a new class template.
880 Here, S<T*> is an implicit instantiation of S whose type
882 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
885 /* It can also be the case that TYPE is a completed specialization.
886 Continuing the previous example, suppose we also declare:
892 Here, S<T*> refers to the specialization S<T*> defined
893 above. However, we need to differentiate definitions because
894 we intend to define a new partial specialization. In this case,
895 we rely on the fact that the constraints are different for
896 this declaration than that above.
898 Note that we also get here for injected class names and
899 late-parsed template definitions. We must ensure that we
900 do not create new type declarations for those cases. */
901 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
903 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
904 tree args
= CLASSTYPE_TI_ARGS (type
);
906 /* If there are no template parameters, this cannot be a new
907 partial template specialization? */
908 if (!current_template_parms
)
911 /* The injected-class-name is not a new partial specialization. */
912 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
915 /* If the constraints are not the same as those of the primary
916 then, we can probably create a new specialization. */
917 tree type_constr
= current_template_constraints ();
919 if (type
== TREE_TYPE (tmpl
))
921 tree main_constr
= get_constraints (tmpl
);
922 if (equivalent_constraints (type_constr
, main_constr
))
926 /* Also, if there's a pre-existing specialization with matching
927 constraints, then this also isn't new. */
928 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
931 tree spec_tmpl
= TREE_VALUE (specs
);
932 tree spec_args
= TREE_PURPOSE (specs
);
933 tree spec_constr
= get_constraints (spec_tmpl
);
934 if (comp_template_args (args
, spec_args
)
935 && equivalent_constraints (type_constr
, spec_constr
))
937 specs
= TREE_CHAIN (specs
);
940 /* Create a new type node (and corresponding type decl)
941 for the newly declared specialization. */
942 tree t
= make_class_type (TREE_CODE (type
));
943 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
944 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
946 /* We only need a separate type node for storing the definition of this
947 partial specialization; uses of S<T*> are unconstrained, so all are
948 equivalent. So keep TYPE_CANONICAL the same. */
949 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
951 /* Build the corresponding type decl. */
952 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
953 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
954 DECL_SOURCE_LOCATION (d
) = input_location
;
955 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
956 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
964 /* The TYPE is being declared. If it is a template type, that means it
965 is a partial specialization. Do appropriate error-checking. */
968 maybe_process_partial_specialization (tree type
)
972 if (type
== error_mark_node
)
973 return error_mark_node
;
975 /* A lambda that appears in specialization context is not itself a
977 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
980 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
982 error ("name of class shadows template template parameter %qD",
984 return error_mark_node
;
987 context
= TYPE_CONTEXT (type
);
989 if (TYPE_ALIAS_P (type
))
991 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
993 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
994 error ("specialization of alias template %qD",
995 TI_TEMPLATE (tinfo
));
997 error ("explicit specialization of non-template %qT", type
);
998 return error_mark_node
;
1000 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1002 /* This is for ordinary explicit specialization and partial
1003 specialization of a template class such as:
1005 template <> class C<int>;
1009 template <class T> class C<T*>;
1011 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1013 if (tree t
= maybe_new_partial_specialization (type
))
1015 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1016 && !at_namespace_scope_p ())
1017 return error_mark_node
;
1018 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1019 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1020 if (processing_template_decl
)
1022 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1023 if (decl
== error_mark_node
)
1024 return error_mark_node
;
1025 return TREE_TYPE (decl
);
1028 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1029 error ("specialization of %qT after instantiation", type
);
1030 else if (errorcount
&& !processing_specialization
1031 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1032 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1033 /* Trying to define a specialization either without a template<> header
1034 or in an inappropriate place. We've already given an error, so just
1035 bail now so we don't actually define the specialization. */
1036 return error_mark_node
;
1038 else if (CLASS_TYPE_P (type
)
1039 && !CLASSTYPE_USE_TEMPLATE (type
)
1040 && CLASSTYPE_TEMPLATE_INFO (type
)
1041 && context
&& CLASS_TYPE_P (context
)
1042 && CLASSTYPE_TEMPLATE_INFO (context
))
1044 /* This is for an explicit specialization of member class
1045 template according to [temp.expl.spec/18]:
1047 template <> template <class U> class C<int>::D;
1049 The context `C<int>' must be an implicit instantiation.
1050 Otherwise this is just a member class template declared
1053 template <> class C<int> { template <class U> class D; };
1054 template <> template <class U> class C<int>::D;
1056 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1057 while in the second case, `C<int>::D' is a primary template
1058 and `C<T>::D' may not exist. */
1060 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1061 && !COMPLETE_TYPE_P (type
))
1064 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1066 if (current_namespace
1067 != decl_namespace_context (tmpl
))
1069 if (permerror (input_location
,
1070 "specialization of %qD in different namespace",
1072 inform (DECL_SOURCE_LOCATION (tmpl
),
1073 "from definition of %q#D", tmpl
);
1076 /* Check for invalid specialization after instantiation:
1078 template <> template <> class C<int>::D<int>;
1079 template <> template <class U> class C<int>::D; */
1081 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1082 t
; t
= TREE_CHAIN (t
))
1084 tree inst
= TREE_VALUE (t
);
1085 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1086 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1088 /* We already have a full specialization of this partial
1089 instantiation, or a full specialization has been
1090 looked up but not instantiated. Reassign it to the
1091 new member specialization template. */
1095 elt
.tmpl
= most_general_template (tmpl
);
1096 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1099 type_specializations
->remove_elt (&elt
);
1102 CLASSTYPE_TI_ARGS (inst
)
1103 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1106 = type_specializations
->find_slot (&elt
, INSERT
);
1107 entry
= ggc_alloc
<spec_entry
> ();
1112 /* But if we've had an implicit instantiation, that's a
1113 problem ([temp.expl.spec]/6). */
1114 error ("specialization %qT after instantiation %qT",
1118 /* Mark TYPE as a specialization. And as a result, we only
1119 have one level of template argument for the innermost
1121 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1122 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1123 CLASSTYPE_TI_ARGS (type
)
1124 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1127 else if (processing_specialization
)
1129 /* Someday C++0x may allow for enum template specialization. */
1130 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1131 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1132 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1133 "of %qD not allowed by ISO C++", type
);
1136 error ("explicit specialization of non-template %qT", type
);
1137 return error_mark_node
;
1144 /* Returns nonzero if we can optimize the retrieval of specializations
1145 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1146 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1149 optimize_specialization_lookup_p (tree tmpl
)
1151 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1152 && DECL_CLASS_SCOPE_P (tmpl
)
1153 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1155 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1156 /* The optimized lookup depends on the fact that the
1157 template arguments for the member function template apply
1158 purely to the containing class, which is not true if the
1159 containing class is an explicit or partial
1161 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1162 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1163 && !DECL_CONV_FN_P (tmpl
)
1164 /* It is possible to have a template that is not a member
1165 template and is not a member of a template class:
1167 template <typename T>
1168 struct S { friend A::f(); };
1170 Here, the friend function is a template, but the context does
1171 not have template information. The optimized lookup relies
1172 on having ARGS be the template arguments for both the class
1173 and the function template. */
1174 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1177 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1178 gone through coerce_template_parms by now. */
1181 verify_unstripped_args_1 (tree inner
)
1183 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1185 tree arg
= TREE_VEC_ELT (inner
, i
);
1186 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1188 else if (TYPE_P (arg
))
1189 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1190 else if (ARGUMENT_PACK_P (arg
))
1191 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1192 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1193 /* Allow typedefs on the type of a non-type argument, since a
1194 parameter can have them. */;
1196 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1201 verify_unstripped_args (tree args
)
1203 ++processing_template_decl
;
1204 if (!any_dependent_template_arguments_p (args
))
1205 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1206 --processing_template_decl
;
1209 /* Retrieve the specialization (in the sense of [temp.spec] - a
1210 specialization is either an instantiation or an explicit
1211 specialization) of TMPL for the given template ARGS. If there is
1212 no such specialization, return NULL_TREE. The ARGS are a vector of
1213 arguments, or a vector of vectors of arguments, in the case of
1214 templates with more than one level of parameters.
1216 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1217 then we search for a partial specialization matching ARGS. This
1218 parameter is ignored if TMPL is not a class template.
1220 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1221 result is a NONTYPE_ARGUMENT_PACK. */
1224 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1226 if (tmpl
== NULL_TREE
)
1229 if (args
== error_mark_node
)
1232 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1233 || TREE_CODE (tmpl
) == FIELD_DECL
);
1235 /* There should be as many levels of arguments as there are
1236 levels of parameters. */
1237 gcc_assert (TMPL_ARGS_DEPTH (args
)
1238 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1239 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1240 : template_class_depth (DECL_CONTEXT (tmpl
))));
1243 verify_unstripped_args (args
);
1245 /* Lambda functions in templates aren't instantiated normally, but through
1246 tsubst_lambda_expr. */
1247 if (lambda_fn_in_template_p (tmpl
))
1250 if (optimize_specialization_lookup_p (tmpl
))
1252 /* The template arguments actually apply to the containing
1253 class. Find the class specialization with those
1255 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1256 tree class_specialization
1257 = retrieve_specialization (class_template
, args
, 0);
1258 if (!class_specialization
)
1261 /* Find the instance of TMPL. */
1262 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1263 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1266 if (tree ti
= get_template_info (fn
))
1267 if (TI_TEMPLATE (ti
) == tmpl
1268 /* using-declarations can bring in a different
1269 instantiation of tmpl as a member of a different
1270 instantiation of tmpl's class. We don't want those
1272 && DECL_CONTEXT (fn
) == class_specialization
)
1281 hash_table
<spec_hasher
> *specializations
;
1285 elt
.spec
= NULL_TREE
;
1287 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1288 specializations
= type_specializations
;
1290 specializations
= decl_specializations
;
1293 hash
= spec_hasher::hash (&elt
);
1294 found
= specializations
->find_with_hash (&elt
, hash
);
1302 /* Like retrieve_specialization, but for local declarations. */
1305 retrieve_local_specialization (tree tmpl
)
1307 if (local_specializations
== NULL
)
1310 tree
*slot
= local_specializations
->get (tmpl
);
1311 return slot
? *slot
: NULL_TREE
;
1314 /* Returns nonzero iff DECL is a specialization of TMPL. */
1317 is_specialization_of (tree decl
, tree tmpl
)
1321 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1325 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1331 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1333 for (t
= TREE_TYPE (decl
);
1335 t
= CLASSTYPE_USE_TEMPLATE (t
)
1336 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1337 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1344 /* Returns nonzero iff DECL is a specialization of friend declaration
1345 FRIEND_DECL according to [temp.friend]. */
1348 is_specialization_of_friend (tree decl
, tree friend_decl
)
1350 bool need_template
= true;
1353 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1354 || TREE_CODE (decl
) == TYPE_DECL
);
1356 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1357 of a template class, we want to check if DECL is a specialization
1359 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1360 && DECL_TEMPLATE_INFO (friend_decl
)
1361 && !DECL_USE_TEMPLATE (friend_decl
))
1363 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1364 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1365 need_template
= false;
1367 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1368 && !PRIMARY_TEMPLATE_P (friend_decl
))
1369 need_template
= false;
1371 /* There is nothing to do if this is not a template friend. */
1372 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1375 if (is_specialization_of (decl
, friend_decl
))
1379 A member of a class template may be declared to be a friend of a
1380 non-template class. In this case, the corresponding member of
1381 every specialization of the class template is a friend of the
1382 class granting friendship.
1384 For example, given a template friend declaration
1386 template <class T> friend void A<T>::f();
1388 the member function below is considered a friend
1390 template <> struct A<int> {
1394 For this type of template friend, TEMPLATE_DEPTH below will be
1395 nonzero. To determine if DECL is a friend of FRIEND, we first
1396 check if the enclosing class is a specialization of another. */
1398 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1400 && DECL_CLASS_SCOPE_P (decl
)
1401 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1402 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1404 /* Next, we check the members themselves. In order to handle
1405 a few tricky cases, such as when FRIEND_DECL's are
1407 template <class T> friend void A<T>::g(T t);
1408 template <class T> template <T t> friend void A<T>::h();
1412 void A<int>::g(int);
1413 template <int> void A<int>::h();
1415 we need to figure out ARGS, the template arguments from
1416 the context of DECL. This is required for template substitution
1417 of `T' in the function parameter of `g' and template parameter
1418 of `h' in the above examples. Here ARGS corresponds to `int'. */
1420 tree context
= DECL_CONTEXT (decl
);
1421 tree args
= NULL_TREE
;
1422 int current_depth
= 0;
1424 while (current_depth
< template_depth
)
1426 if (CLASSTYPE_TEMPLATE_INFO (context
))
1428 if (current_depth
== 0)
1429 args
= TYPE_TI_ARGS (context
);
1431 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1434 context
= TYPE_CONTEXT (context
);
1437 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1442 tree friend_args_type
;
1443 tree decl_args_type
;
1445 /* Make sure that both DECL and FRIEND_DECL are templates or
1447 is_template
= DECL_TEMPLATE_INFO (decl
)
1448 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1449 if (need_template
^ is_template
)
1451 else if (is_template
)
1453 /* If both are templates, check template parameter list. */
1455 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1457 if (!comp_template_parms
1458 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1462 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1465 decl_type
= TREE_TYPE (decl
);
1467 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1468 tf_none
, NULL_TREE
);
1469 if (friend_type
== error_mark_node
)
1472 /* Check if return types match. */
1473 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1476 /* Check if function parameter types match, ignoring the
1477 `this' parameter. */
1478 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1479 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1480 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1481 friend_args_type
= TREE_CHAIN (friend_args_type
);
1482 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1483 decl_args_type
= TREE_CHAIN (decl_args_type
);
1485 return compparms (decl_args_type
, friend_args_type
);
1489 /* DECL is a TYPE_DECL */
1491 tree decl_type
= TREE_TYPE (decl
);
1493 /* Make sure that both DECL and FRIEND_DECL are templates or
1496 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1497 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1499 if (need_template
^ is_template
)
1501 else if (is_template
)
1504 /* If both are templates, check the name of the two
1505 TEMPLATE_DECL's first because is_friend didn't. */
1506 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1507 != DECL_NAME (friend_decl
))
1510 /* Now check template parameter list. */
1512 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1514 return comp_template_parms
1515 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1519 return (DECL_NAME (decl
)
1520 == DECL_NAME (friend_decl
));
1526 /* Register the specialization SPEC as a specialization of TMPL with
1527 the indicated ARGS. IS_FRIEND indicates whether the specialization
1528 is actually just a friend declaration. ATTRLIST is the list of
1529 attributes that the specialization is declared with or NULL when
1530 it isn't. Returns SPEC, or an equivalent prior declaration, if
1533 We also store instantiations of field packs in the hash table, even
1534 though they are not themselves templates, to make lookup easier. */
1537 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1541 spec_entry
**slot
= NULL
;
1544 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1545 || (TREE_CODE (tmpl
) == FIELD_DECL
1546 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1548 if (TREE_CODE (spec
) == FUNCTION_DECL
1549 && uses_template_parms (DECL_TI_ARGS (spec
)))
1550 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1551 register it; we want the corresponding TEMPLATE_DECL instead.
1552 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1553 the more obvious `uses_template_parms (spec)' to avoid problems
1554 with default function arguments. In particular, given
1555 something like this:
1557 template <class T> void f(T t1, T t = T())
1559 the default argument expression is not substituted for in an
1560 instantiation unless and until it is actually needed. */
1563 if (optimize_specialization_lookup_p (tmpl
))
1564 /* We don't put these specializations in the hash table, but we might
1565 want to give an error about a mismatch. */
1566 fn
= retrieve_specialization (tmpl
, args
, 0);
1574 hash
= spec_hasher::hash (&elt
);
1577 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1579 fn
= ((spec_entry
*) *slot
)->spec
;
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 (compparms (fn_arg_types
, decl_arg_types
))
2286 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2290 /* See whether this function might be a specialization of this
2291 template. Suppress access control because we might be trying
2292 to make this specialization a friend, and we have already done
2293 access control for the declaration of the specialization. */
2294 push_deferring_access_checks (dk_no_check
);
2295 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2296 pop_deferring_access_checks ();
2299 /* We cannot deduce template arguments that when used to
2300 specialize TMPL will produce DECL. */
2303 if (uses_template_parms (targs
))
2304 /* We deduced something involving 'auto', which isn't a valid
2305 template argument. */
2308 /* Remove, from the set of candidates, all those functions
2309 whose constraints are not satisfied. */
2310 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2313 // Then, try to form the new function type.
2314 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2315 if (insttype
== error_mark_node
)
2318 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2319 if (!compparms (fn_arg_types
, decl_arg_types
))
2322 /* Save this template, and the arguments deduced. */
2323 templates
= tree_cons (targs
, fn
, templates
);
2325 else if (need_member_template
)
2326 /* FN is an ordinary member function, and we need a
2327 specialization of a member template. */
2329 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2330 /* We can get IDENTIFIER_NODEs here in certain erroneous
2333 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2334 /* This is just an ordinary non-member function. Nothing can
2335 be a specialization of that. */
2337 else if (DECL_ARTIFICIAL (fn
))
2338 /* Cannot specialize functions that are created implicitly. */
2342 tree decl_arg_types
;
2344 /* This is an ordinary member function. However, since
2345 we're here, we can assume its enclosing class is a
2346 template class. For example,
2348 template <typename T> struct S { void f(); };
2349 template <> void S<int>::f() {}
2351 Here, S<int>::f is a non-template, but S<int> is a
2352 template class. If FN has the same type as DECL, we
2353 might be in business. */
2355 if (!DECL_TEMPLATE_INFO (fn
))
2356 /* Its enclosing class is an explicit specialization
2357 of a template class. This is not a candidate. */
2360 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2361 TREE_TYPE (TREE_TYPE (fn
))))
2362 /* The return types differ. */
2365 /* Adjust the type of DECL in case FN is a static member. */
2366 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2367 if (DECL_STATIC_FUNCTION_P (fn
)
2368 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2369 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2371 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2375 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2376 && (type_memfn_rqual (TREE_TYPE (decl
))
2377 != type_memfn_rqual (TREE_TYPE (fn
))))
2380 // If the deduced arguments do not satisfy the constraints,
2381 // this is not a candidate.
2382 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2385 // Add the candidate.
2386 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2390 if (templates
&& TREE_CHAIN (templates
))
2396 It is possible for a specialization with a given function
2397 signature to be instantiated from more than one function
2398 template. In such cases, explicit specification of the
2399 template arguments must be used to uniquely identify the
2400 function template specialization being specialized.
2402 Note that here, there's no suggestion that we're supposed to
2403 determine which of the candidate templates is most
2404 specialized. However, we, also have:
2408 Partial ordering of overloaded function template
2409 declarations is used in the following contexts to select
2410 the function template to which a function template
2411 specialization refers:
2413 -- when an explicit specialization refers to a function
2416 So, we do use the partial ordering rules, at least for now.
2417 This extension can only serve to make invalid programs valid,
2418 so it's safe. And, there is strong anecdotal evidence that
2419 the committee intended the partial ordering rules to apply;
2420 the EDG front end has that behavior, and John Spicer claims
2421 that the committee simply forgot to delete the wording in
2422 [temp.expl.spec]. */
2423 tree tmpl
= most_specialized_instantiation (templates
);
2424 if (tmpl
!= error_mark_node
)
2427 TREE_CHAIN (templates
) = NULL_TREE
;
2431 // Concepts allows multiple declarations of member functions
2432 // with the same signature. Like above, we need to rely on
2433 // on the partial ordering of those candidates to determine which
2435 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2437 if (tree cand
= most_constrained_function (candidates
))
2440 TREE_CHAIN (cand
) = NULL_TREE
;
2444 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2446 error ("template-id %qD for %q+D does not match any template "
2447 "declaration", template_id
, decl
);
2448 if (header_count
&& header_count
!= template_count
+ 1)
2449 inform (DECL_SOURCE_LOCATION (decl
),
2450 "saw %d %<template<>%>, need %d for "
2451 "specializing a member function template",
2452 header_count
, template_count
+ 1);
2454 print_candidates (orig_fns
);
2455 return error_mark_node
;
2457 else if ((templates
&& TREE_CHAIN (templates
))
2458 || (candidates
&& TREE_CHAIN (candidates
))
2459 || (templates
&& candidates
))
2461 error ("ambiguous template specialization %qD for %q+D",
2463 candidates
= chainon (candidates
, templates
);
2464 print_candidates (candidates
);
2465 return error_mark_node
;
2468 /* We have one, and exactly one, match. */
2471 tree fn
= TREE_VALUE (candidates
);
2472 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2474 /* Propagate the candidate's constraints to the declaration. */
2475 set_constraints (decl
, get_constraints (fn
));
2477 /* DECL is a re-declaration or partial instantiation of a template
2479 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2481 /* It was a specialization of an ordinary member function in a
2483 return DECL_TI_TEMPLATE (fn
);
2486 /* It was a specialization of a template. */
2487 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2488 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2490 *targs_out
= copy_node (targs
);
2491 SET_TMPL_ARGS_LEVEL (*targs_out
,
2492 TMPL_ARGS_DEPTH (*targs_out
),
2493 TREE_PURPOSE (templates
));
2496 *targs_out
= TREE_PURPOSE (templates
);
2497 return TREE_VALUE (templates
);
2500 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2501 but with the default argument values filled in from those in the
2505 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2508 tree new_spec_types
;
2513 if (spec_types
== void_list_node
)
2514 return void_list_node
;
2516 /* Substitute into the rest of the list. */
2518 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2519 TREE_CHAIN (tmpl_types
));
2521 /* Add the default argument for this parameter. */
2522 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2523 TREE_VALUE (spec_types
),
2527 /* DECL is an explicit specialization. Replicate default arguments
2528 from the template it specializes. (That way, code like:
2530 template <class T> void f(T = 3);
2531 template <> void f(double);
2534 works, as required.) An alternative approach would be to look up
2535 the correct default arguments at the call-site, but this approach
2536 is consistent with how implicit instantiations are handled. */
2539 copy_default_args_to_explicit_spec (tree decl
)
2544 tree new_spec_types
;
2548 tree object_type
= NULL_TREE
;
2549 tree in_charge
= NULL_TREE
;
2550 tree vtt
= NULL_TREE
;
2552 /* See if there's anything we need to do. */
2553 tmpl
= DECL_TI_TEMPLATE (decl
);
2554 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2555 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2556 if (TREE_PURPOSE (t
))
2561 old_type
= TREE_TYPE (decl
);
2562 spec_types
= TYPE_ARG_TYPES (old_type
);
2564 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2566 /* Remove the this pointer, but remember the object's type for
2568 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2569 spec_types
= TREE_CHAIN (spec_types
);
2570 tmpl_types
= TREE_CHAIN (tmpl_types
);
2572 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2574 /* DECL may contain more parameters than TMPL due to the extra
2575 in-charge parameter in constructors and destructors. */
2576 in_charge
= spec_types
;
2577 spec_types
= TREE_CHAIN (spec_types
);
2579 if (DECL_HAS_VTT_PARM_P (decl
))
2582 spec_types
= TREE_CHAIN (spec_types
);
2586 /* Compute the merged default arguments. */
2588 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2590 /* Compute the new FUNCTION_TYPE. */
2594 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2599 /* Put the in-charge parameter back. */
2600 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2601 TREE_VALUE (in_charge
),
2604 new_type
= build_method_type_directly (object_type
,
2605 TREE_TYPE (old_type
),
2609 new_type
= build_function_type (TREE_TYPE (old_type
),
2611 new_type
= cp_build_type_attribute_variant (new_type
,
2612 TYPE_ATTRIBUTES (old_type
));
2613 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2615 TREE_TYPE (decl
) = new_type
;
2618 /* Return the number of template headers we expect to see for a definition
2619 or specialization of CTYPE or one of its non-template members. */
2622 num_template_headers_for_class (tree ctype
)
2624 int num_templates
= 0;
2626 while (ctype
&& CLASS_TYPE_P (ctype
))
2628 /* You're supposed to have one `template <...>' for every
2629 template class, but you don't need one for a full
2630 specialization. For example:
2632 template <class T> struct S{};
2633 template <> struct S<int> { void f(); };
2634 void S<int>::f () {}
2636 is correct; there shouldn't be a `template <>' for the
2637 definition of `S<int>::f'. */
2638 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2639 /* If CTYPE does not have template information of any
2640 kind, then it is not a template, nor is it nested
2641 within a template. */
2643 if (explicit_class_specialization_p (ctype
))
2645 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2648 ctype
= TYPE_CONTEXT (ctype
);
2651 return num_templates
;
2654 /* Do a simple sanity check on the template headers that precede the
2655 variable declaration DECL. */
2658 check_template_variable (tree decl
)
2660 tree ctx
= CP_DECL_CONTEXT (decl
);
2661 int wanted
= num_template_headers_for_class (ctx
);
2662 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2663 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2665 if (cxx_dialect
< cxx14
)
2666 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2667 "variable templates only available with "
2668 "%<-std=c++14%> or %<-std=gnu++14%>");
2670 // Namespace-scope variable templates should have a template header.
2673 if (template_header_count
> wanted
)
2675 auto_diagnostic_group d
;
2676 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2677 "too many template headers for %qD "
2680 if (warned
&& CLASS_TYPE_P (ctx
)
2681 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2682 inform (DECL_SOURCE_LOCATION (decl
),
2683 "members of an explicitly specialized class are defined "
2684 "without a template header");
2688 /* An explicit specialization whose declarator-id or class-head-name is not
2689 qualified shall be declared in the nearest enclosing namespace of the
2690 template, or, if the namespace is inline (7.3.1), any namespace from its
2691 enclosing namespace set.
2693 If the name declared in the explicit instantiation is an unqualified name,
2694 the explicit instantiation shall appear in the namespace where its template
2695 is declared or, if that namespace is inline (7.3.1), any namespace from its
2696 enclosing namespace set. */
2699 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2701 tree tmpl
= most_general_template (t
);
2702 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2703 && !is_nested_namespace (current_namespace
,
2704 CP_DECL_CONTEXT (tmpl
), true))
2706 if (processing_specialization
)
2707 permerror (loc
, "explicit specialization of %qD outside its "
2708 "namespace must use a nested-name-specifier", tmpl
);
2709 else if (processing_explicit_instantiation
2710 && cxx_dialect
>= cxx11
)
2711 /* This was allowed in C++98, so only pedwarn. */
2712 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2713 "outside its namespace must use a nested-name-"
2718 /* Warn for a template specialization SPEC that is missing some of a set
2719 of function or type attributes that the template TEMPL is declared with.
2720 ATTRLIST is a list of additional attributes that SPEC should be taken
2721 to ultimately be declared with. */
2724 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2726 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2727 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2729 /* Avoid warning if the difference between the primary and
2730 the specialization is not in one of the attributes below. */
2731 const char* const blacklist
[] = {
2732 "alloc_align", "alloc_size", "assume_aligned", "format",
2733 "format_arg", "malloc", "nonnull", NULL
2736 /* Put together a list of the black listed attributes that the primary
2737 template is declared with that the specialization is not, in case
2738 it's not apparent from the most recent declaration of the primary. */
2740 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2746 auto_diagnostic_group d
;
2747 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2748 "explicit specialization %q#D may be missing attributes",
2750 inform (DECL_SOURCE_LOCATION (tmpl
),
2752 ? G_("missing primary template attributes %s")
2753 : G_("missing primary template attribute %s"),
2754 pp_formatted_text (&str
));
2757 /* Check to see if the function just declared, as indicated in
2758 DECLARATOR, and in DECL, is a specialization of a function
2759 template. We may also discover that the declaration is an explicit
2760 instantiation at this point.
2762 Returns DECL, or an equivalent declaration that should be used
2763 instead if all goes well. Issues an error message if something is
2764 amiss. Returns error_mark_node if the error is not easily
2767 FLAGS is a bitmask consisting of the following flags:
2769 2: The function has a definition.
2770 4: The function is a friend.
2772 The TEMPLATE_COUNT is the number of references to qualifying
2773 template classes that appeared in the name of the function. For
2776 template <class T> struct S { void f(); };
2779 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2780 classes are not counted in the TEMPLATE_COUNT, so that in
2782 template <class T> struct S {};
2783 template <> struct S<int> { void f(); }
2784 template <> void S<int>::f();
2786 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2787 invalid; there should be no template <>.)
2789 If the function is a specialization, it is marked as such via
2790 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2791 is set up correctly, and it is added to the list of specializations
2792 for that template. */
2795 check_explicit_specialization (tree declarator
,
2801 int have_def
= flags
& 2;
2802 int is_friend
= flags
& 4;
2803 bool is_concept
= flags
& 8;
2804 int specialization
= 0;
2805 int explicit_instantiation
= 0;
2806 int member_specialization
= 0;
2807 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2808 tree dname
= DECL_NAME (decl
);
2813 if (!processing_specialization
)
2816 tsk
= tsk_excessive_parms
;
2819 tsk
= current_tmpl_spec_kind (template_count
);
2824 if (processing_specialization
&& !VAR_P (decl
))
2827 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2829 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2832 /* This could be something like:
2834 template <class T> void f(T);
2835 class S { friend void f<>(int); } */
2839 /* This case handles bogus declarations like template <>
2840 template <class T> void f<int>(); */
2842 error_at (cp_expr_loc_or_input_loc (declarator
),
2843 "template-id %qE in declaration of primary template",
2850 case tsk_invalid_member_spec
:
2851 /* The error has already been reported in
2852 check_specialization_scope. */
2853 return error_mark_node
;
2855 case tsk_invalid_expl_inst
:
2856 error ("template parameter list used in explicit instantiation");
2862 error ("definition provided for explicit instantiation");
2864 explicit_instantiation
= 1;
2867 case tsk_excessive_parms
:
2868 case tsk_insufficient_parms
:
2869 if (tsk
== tsk_excessive_parms
)
2870 error ("too many template parameter lists in declaration of %qD",
2872 else if (template_header_count
)
2873 error("too few template parameter lists in declaration of %qD", decl
);
2875 error("explicit specialization of %qD must be introduced by "
2876 "%<template <>%>", decl
);
2881 error ("explicit specialization declared %<concept%>");
2883 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2884 /* In cases like template<> constexpr bool v = true;
2885 We'll give an error in check_template_variable. */
2888 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2890 member_specialization
= 1;
2896 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2898 /* This case handles bogus declarations like template <>
2899 template <class T> void f<int>(); */
2901 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2902 error_at (cp_expr_loc_or_input_loc (declarator
),
2903 "template-id %qE in declaration of primary template",
2905 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2907 /* Partial specialization of variable template. */
2908 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2912 else if (cxx_dialect
< cxx14
)
2913 error_at (cp_expr_loc_or_input_loc (declarator
),
2914 "non-type partial specialization %qE "
2915 "is not allowed", declarator
);
2917 error_at (cp_expr_loc_or_input_loc (declarator
),
2918 "non-class, non-variable partial specialization %qE "
2919 "is not allowed", declarator
);
2924 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2925 /* This is a specialization of a member template, without
2926 specialization the containing class. Something like:
2928 template <class T> struct S {
2929 template <class U> void f (U);
2931 template <> template <class U> void S<int>::f(U) {}
2933 That's a specialization -- but of the entire template. */
2941 if ((specialization
|| member_specialization
)
2942 /* This doesn't apply to variable templates. */
2943 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2945 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2946 for (; t
; t
= TREE_CHAIN (t
))
2947 if (TREE_PURPOSE (t
))
2949 permerror (input_location
,
2950 "default argument specified in explicit specialization");
2955 if (specialization
|| member_specialization
|| explicit_instantiation
)
2957 tree tmpl
= NULL_TREE
;
2958 tree targs
= NULL_TREE
;
2959 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2961 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2962 if (!was_template_id
)
2966 gcc_assert (identifier_p (declarator
));
2971 /* If there is no class context, the explicit instantiation
2972 must be at namespace scope. */
2973 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2975 /* Find the namespace binding, using the declaration
2977 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2979 if (fns
== error_mark_node
)
2980 /* If lookup fails, look for a friend declaration so we can
2981 give a better diagnostic. */
2982 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2983 /*type*/false, /*complain*/true,
2986 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2988 error ("%qD is not a template function", dname
);
2989 fns
= error_mark_node
;
2993 declarator
= lookup_template_function (fns
, NULL_TREE
);
2996 if (declarator
== error_mark_node
)
2997 return error_mark_node
;
2999 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3001 if (!explicit_instantiation
)
3002 /* A specialization in class scope. This is invalid,
3003 but the error will already have been flagged by
3004 check_specialization_scope. */
3005 return error_mark_node
;
3008 /* It's not valid to write an explicit instantiation in
3011 class C { template void f(); }
3013 This case is caught by the parser. However, on
3016 template class C { void f(); };
3018 (which is invalid) we can get here. The error will be
3025 else if (ctype
!= NULL_TREE
3026 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3028 // We'll match variable templates in start_decl.
3032 /* Find the list of functions in ctype that have the same
3033 name as the declared function. */
3034 tree name
= TREE_OPERAND (declarator
, 0);
3036 if (constructor_name_p (name
, ctype
))
3038 if (DECL_CONSTRUCTOR_P (decl
)
3039 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3040 : !CLASSTYPE_DESTRUCTOR (ctype
))
3042 /* From [temp.expl.spec]:
3044 If such an explicit specialization for the member
3045 of a class template names an implicitly-declared
3046 special member function (clause _special_), the
3047 program is ill-formed.
3049 Similar language is found in [temp.explicit]. */
3050 error ("specialization of implicitly-declared special member function");
3051 return error_mark_node
;
3054 name
= DECL_NAME (decl
);
3057 /* For a type-conversion operator, We might be looking for
3058 `operator int' which will be a specialization of
3059 `operator T'. Grab all the conversion operators, and
3060 then select from them. */
3061 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3062 ? conv_op_identifier
: name
);
3064 if (fns
== NULL_TREE
)
3066 error ("no member function %qD declared in %qT", name
, ctype
);
3067 return error_mark_node
;
3070 TREE_OPERAND (declarator
, 0) = fns
;
3073 /* Figure out what exactly is being specialized at this point.
3074 Note that for an explicit instantiation, even one for a
3075 member function, we cannot tell a priori whether the
3076 instantiation is for a member template, or just a member
3077 function of a template class. Even if a member template is
3078 being instantiated, the member template arguments may be
3079 elided if they can be deduced from the rest of the
3081 tmpl
= determine_specialization (declarator
, decl
,
3083 member_specialization
,
3087 if (!tmpl
|| tmpl
== error_mark_node
)
3088 /* We couldn't figure out what this declaration was
3090 return error_mark_node
;
3093 if (TREE_CODE (decl
) == FUNCTION_DECL
3094 && DECL_HIDDEN_FRIEND_P (tmpl
))
3096 auto_diagnostic_group d
;
3097 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3098 "friend declaration %qD is not visible to "
3099 "explicit specialization", tmpl
))
3100 inform (DECL_SOURCE_LOCATION (tmpl
),
3101 "friend declaration here");
3103 else if (!ctype
&& !is_friend
3104 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3105 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3107 tree gen_tmpl
= most_general_template (tmpl
);
3109 if (explicit_instantiation
)
3111 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3112 is done by do_decl_instantiation later. */
3114 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3115 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3117 if (arg_depth
> parm_depth
)
3119 /* If TMPL is not the most general template (for
3120 example, if TMPL is a friend template that is
3121 injected into namespace scope), then there will
3122 be too many levels of TARGS. Remove some of them
3127 new_targs
= make_tree_vec (parm_depth
);
3128 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3129 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3130 = TREE_VEC_ELT (targs
, i
);
3134 return instantiate_template (tmpl
, targs
, tf_error
);
3137 /* If we thought that the DECL was a member function, but it
3138 turns out to be specializing a static member function,
3139 make DECL a static member function as well. */
3140 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3141 && DECL_STATIC_FUNCTION_P (tmpl
)
3142 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3143 revert_static_member_fn (decl
);
3145 /* If this is a specialization of a member template of a
3146 template class, we want to return the TEMPLATE_DECL, not
3147 the specialization of it. */
3148 if (tsk
== tsk_template
&& !was_template_id
)
3150 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3151 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3152 DECL_INITIAL (result
) = NULL_TREE
;
3156 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3157 DECL_SOURCE_LOCATION (result
)
3158 = DECL_SOURCE_LOCATION (decl
);
3159 /* We want to use the argument list specified in the
3160 definition, not in the original declaration. */
3161 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3162 for (parm
= DECL_ARGUMENTS (result
); parm
;
3163 parm
= DECL_CHAIN (parm
))
3164 DECL_CONTEXT (parm
) = result
;
3166 return register_specialization (tmpl
, gen_tmpl
, targs
,
3170 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3171 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3173 if (was_template_id
)
3174 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3176 /* Inherit default function arguments from the template
3177 DECL is specializing. */
3178 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3179 copy_default_args_to_explicit_spec (decl
);
3181 /* This specialization has the same protection as the
3182 template it specializes. */
3183 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3184 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3186 /* 7.1.1-1 [dcl.stc]
3188 A storage-class-specifier shall not be specified in an
3189 explicit specialization...
3191 The parser rejects these, so unless action is taken here,
3192 explicit function specializations will always appear with
3195 The action recommended by the C++ CWG in response to C++
3196 defect report 605 is to make the storage class and linkage
3197 of the explicit specialization match the templated function:
3199 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3201 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3203 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3204 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3206 /* A concept cannot be specialized. */
3207 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3209 error ("explicit specialization of function concept %qD",
3211 return error_mark_node
;
3214 /* This specialization has the same linkage and visibility as
3215 the function template it specializes. */
3216 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3217 if (! TREE_PUBLIC (decl
))
3219 DECL_INTERFACE_KNOWN (decl
) = 1;
3220 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3222 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3223 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3225 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3226 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3230 /* If DECL is a friend declaration, declared using an
3231 unqualified name, the namespace associated with DECL may
3232 have been set incorrectly. For example, in:
3234 template <typename T> void f(T);
3236 struct S { friend void f<int>(int); }
3239 we will have set the DECL_CONTEXT for the friend
3240 declaration to N, rather than to the global namespace. */
3241 if (DECL_NAMESPACE_SCOPE_P (decl
))
3242 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3244 if (is_friend
&& !have_def
)
3245 /* This is not really a declaration of a specialization.
3246 It's just the name of an instantiation. But, it's not
3247 a request for an instantiation, either. */
3248 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3249 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3250 /* A specialization is not necessarily COMDAT. */
3251 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3252 && DECL_DECLARED_INLINE_P (decl
));
3253 else if (VAR_P (decl
))
3254 DECL_COMDAT (decl
) = false;
3256 /* If this is a full specialization, register it so that we can find
3257 it again. Partial specializations will be registered in
3258 process_partial_specialization. */
3259 if (!processing_template_decl
)
3261 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3263 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3268 /* A 'structor should already have clones. */
3269 gcc_assert (decl
== error_mark_node
3270 || variable_template_p (tmpl
)
3271 || !(DECL_CONSTRUCTOR_P (decl
)
3272 || DECL_DESTRUCTOR_P (decl
))
3273 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3280 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3281 parameters. These are represented in the same format used for
3282 DECL_TEMPLATE_PARMS. */
3285 comp_template_parms (const_tree parms1
, const_tree parms2
)
3290 if (parms1
== parms2
)
3293 for (p1
= parms1
, p2
= parms2
;
3294 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3295 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3297 tree t1
= TREE_VALUE (p1
);
3298 tree t2
= TREE_VALUE (p2
);
3301 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3302 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3304 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3307 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3309 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3310 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3312 /* If either of the template parameters are invalid, assume
3313 they match for the sake of error recovery. */
3314 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3317 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3320 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3321 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3322 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3324 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3329 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3330 /* One set of parameters has more parameters lists than the
3337 /* Returns true if two template parameters are declared with
3338 equivalent constraints. */
3341 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3343 tree req1
= TREE_TYPE (parm1
);
3344 tree req2
= TREE_TYPE (parm2
);
3348 return cp_tree_equal (req1
, req2
);
3352 /* Returns true when two template parameters are equivalent. */
3355 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3357 tree decl1
= TREE_VALUE (parm1
);
3358 tree decl2
= TREE_VALUE (parm2
);
3360 /* If either of the template parameters are invalid, assume
3361 they match for the sake of error recovery. */
3362 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3365 /* ... they declare parameters of the same kind. */
3366 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3369 /* ... one parameter was introduced by a parameter declaration, then
3370 both are. This case arises as a result of eagerly rewriting declarations
3372 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3375 /* ... if either declares a pack, they both do. */
3376 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3379 if (TREE_CODE (decl1
) == PARM_DECL
)
3381 /* ... if they declare non-type parameters, the types are equivalent. */
3382 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3385 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3387 /* ... if they declare template template parameters, their template
3388 parameter lists are equivalent. */
3389 if (!template_heads_equivalent_p (decl1
, decl2
))
3393 /* ... if they are declared with a qualified-concept name, they both
3394 are, and those names are equivalent. */
3395 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3398 /* Returns true if two template parameters lists are equivalent.
3399 Two template parameter lists are equivalent if they have the
3400 same length and their corresponding parameters are equivalent.
3402 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3403 data structure returned by DECL_TEMPLATE_PARMS.
3405 This is generally the same implementation as comp_template_parms
3406 except that it also the concept names and arguments used to
3407 introduce parameters. */
3410 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3412 if (parms1
== parms2
)
3415 const_tree p1
= parms1
;
3416 const_tree p2
= parms2
;
3417 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3419 tree list1
= TREE_VALUE (p1
);
3420 tree list2
= TREE_VALUE (p2
);
3422 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3425 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3427 tree parm1
= TREE_VEC_ELT (list1
, i
);
3428 tree parm2
= TREE_VEC_ELT (list2
, i
);
3429 if (!template_parameters_equivalent_p (parm1
, parm2
))
3433 p1
= TREE_CHAIN (p1
);
3434 p2
= TREE_CHAIN (p2
);
3437 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3443 /* Return true if the requires-clause of the template parameter lists are
3444 equivalent and false otherwise. */
3446 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3448 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3449 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3450 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3452 if (!cp_tree_equal (req1
, req2
))
3457 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3458 Two template heads are equivalent if their template parameter
3459 lists are equivalent and their requires clauses are equivalent.
3461 In pre-C++20, this is equivalent to calling comp_template_parms
3462 for the template parameters of TMPL1 and TMPL2. */
3465 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3467 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3468 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3470 /* Don't change the matching rules for pre-C++20. */
3471 if (cxx_dialect
< cxx2a
)
3472 return comp_template_parms (parms1
, parms2
);
3474 /* ... have the same number of template parameters, and their
3475 corresponding parameters are equivalent. */
3476 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3479 /* ... if either has a requires-clause, they both do and their
3480 corresponding constraint-expressions are equivalent. */
3481 return template_requirements_equivalent_p (parms1
, parms2
);
3484 /* Determine whether PARM is a parameter pack. */
3487 template_parameter_pack_p (const_tree parm
)
3489 /* Determine if we have a non-type template parameter pack. */
3490 if (TREE_CODE (parm
) == PARM_DECL
)
3491 return (DECL_TEMPLATE_PARM_P (parm
)
3492 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3493 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3494 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3496 /* If this is a list of template parameters, we could get a
3497 TYPE_DECL or a TEMPLATE_DECL. */
3498 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3499 parm
= TREE_TYPE (parm
);
3501 /* Otherwise it must be a type template parameter. */
3502 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3503 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3504 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3507 /* Determine if T is a function parameter pack. */
3510 function_parameter_pack_p (const_tree t
)
3512 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3513 return DECL_PACK_P (t
);
3517 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3518 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3521 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3523 if (! primary_func_tmpl_inst
3524 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3525 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3528 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3531 /* Return true iff the function parameter PARAM_DECL was expanded
3532 from the function parameter pack PACK. */
3535 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3537 if (DECL_ARTIFICIAL (param_decl
)
3538 || !function_parameter_pack_p (pack
))
3541 /* The parameter pack and its pack arguments have the same
3543 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3546 /* Determine whether ARGS describes a variadic template args list,
3547 i.e., one that is terminated by a template argument pack. */
3550 template_args_variadic_p (tree args
)
3555 if (args
== NULL_TREE
)
3558 args
= INNERMOST_TEMPLATE_ARGS (args
);
3559 nargs
= TREE_VEC_LENGTH (args
);
3564 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3566 return ARGUMENT_PACK_P (last_parm
);
3569 /* Generate a new name for the parameter pack name NAME (an
3570 IDENTIFIER_NODE) that incorporates its */
3573 make_ith_pack_parameter_name (tree name
, int i
)
3575 /* Munge the name to include the parameter index. */
3576 #define NUMBUF_LEN 128
3577 char numbuf
[NUMBUF_LEN
];
3581 if (name
== NULL_TREE
)
3583 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3584 newname_len
= IDENTIFIER_LENGTH (name
)
3585 + strlen (numbuf
) + 2;
3586 newname
= (char*)alloca (newname_len
);
3587 snprintf (newname
, newname_len
,
3588 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3589 return get_identifier (newname
);
3592 /* Return true if T is a primary function, class or alias template
3593 specialization, not including the template pattern. */
3596 primary_template_specialization_p (const_tree t
)
3601 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3602 return (DECL_LANG_SPECIFIC (t
)
3603 && DECL_USE_TEMPLATE (t
)
3604 && DECL_TEMPLATE_INFO (t
)
3605 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3606 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3607 return (CLASSTYPE_TEMPLATE_INFO (t
)
3608 && CLASSTYPE_USE_TEMPLATE (t
)
3609 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3610 else if (alias_template_specialization_p (t
, nt_transparent
))
3615 /* Return true if PARM is a template template parameter. */
3618 template_template_parameter_p (const_tree parm
)
3620 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3623 /* Return true iff PARM is a DECL representing a type template
3627 template_type_parameter_p (const_tree parm
)
3630 && (TREE_CODE (parm
) == TYPE_DECL
3631 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3632 && DECL_TEMPLATE_PARM_P (parm
));
3635 /* Return the template parameters of T if T is a
3636 primary template instantiation, NULL otherwise. */
3639 get_primary_template_innermost_parameters (const_tree t
)
3641 tree parms
= NULL
, template_info
= NULL
;
3643 if ((template_info
= get_template_info (t
))
3644 && primary_template_specialization_p (t
))
3645 parms
= INNERMOST_TEMPLATE_PARMS
3646 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3651 /* Return the template parameters of the LEVELth level from the full list
3652 of template parameters PARMS. */
3655 get_template_parms_at_level (tree parms
, int level
)
3659 || TREE_CODE (parms
) != TREE_LIST
3660 || level
> TMPL_PARMS_DEPTH (parms
))
3663 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3664 if (TMPL_PARMS_DEPTH (p
) == level
)
3670 /* Returns the template arguments of T if T is a template instantiation,
3674 get_template_innermost_arguments (const_tree t
)
3676 tree args
= NULL
, template_info
= NULL
;
3678 if ((template_info
= get_template_info (t
))
3679 && TI_ARGS (template_info
))
3680 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3685 /* Return the argument pack elements of T if T is a template argument pack,
3689 get_template_argument_pack_elems (const_tree t
)
3691 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3692 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3695 return ARGUMENT_PACK_ARGS (t
);
3698 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3699 ARGUMENT_PACK_SELECT represents. */
3702 argument_pack_select_arg (tree t
)
3704 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3705 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3707 /* If the selected argument is an expansion E, that most likely means we were
3708 called from gen_elem_of_pack_expansion_instantiation during the
3709 substituting of an argument pack (of which the Ith element is a pack
3710 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3711 In this case, the Ith element resulting from this substituting is going to
3712 be a pack expansion, which pattern is the pattern of E. Let's return the
3713 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3714 resulting pack expansion from it. */
3715 if (PACK_EXPANSION_P (arg
))
3717 /* Make sure we aren't throwing away arg info. */
3718 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3719 arg
= PACK_EXPANSION_PATTERN (arg
);
3726 /* True iff FN is a function representing a built-in variadic parameter
3730 builtin_pack_fn_p (tree fn
)
3733 || TREE_CODE (fn
) != FUNCTION_DECL
3734 || !DECL_IS_BUILTIN (fn
))
3737 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3743 /* True iff CALL is a call to a function representing a built-in variadic
3747 builtin_pack_call_p (tree call
)
3749 if (TREE_CODE (call
) != CALL_EXPR
)
3751 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3754 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3757 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3760 tree ohi
= CALL_EXPR_ARG (call
, 0);
3761 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3762 false/*fn*/, true/*int_cst*/);
3764 if (value_dependent_expression_p (hi
))
3768 call
= copy_node (call
);
3769 CALL_EXPR_ARG (call
, 0) = hi
;
3771 tree ex
= make_pack_expansion (call
, complain
);
3772 tree vec
= make_tree_vec (1);
3773 TREE_VEC_ELT (vec
, 0) = ex
;
3778 hi
= cxx_constant_value (hi
);
3779 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3781 /* Calculate the largest value of len that won't make the size of the vec
3782 overflow an int. The compiler will exceed resource limits long before
3783 this, but it seems a decent place to diagnose. */
3784 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3786 if (len
< 0 || len
> max
)
3788 if ((complain
& tf_error
)
3789 && hi
!= error_mark_node
)
3790 error ("argument to %<__integer_pack%> must be between 0 and %d",
3792 return error_mark_node
;
3795 tree vec
= make_tree_vec (len
);
3797 for (int i
= 0; i
< len
; ++i
)
3798 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3804 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3808 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3811 if (!builtin_pack_call_p (call
))
3814 tree fn
= CALL_EXPR_FN (call
);
3816 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3817 return expand_integer_pack (call
, args
, complain
, in_decl
);
3822 /* Structure used to track the progress of find_parameter_packs_r. */
3823 struct find_parameter_pack_data
3825 /* TREE_LIST that will contain all of the parameter packs found by
3827 tree
* parameter_packs
;
3829 /* Set of AST nodes that have been visited by the traversal. */
3830 hash_set
<tree
> *visited
;
3832 /* True iff we're making a type pack expansion. */
3833 bool type_pack_expansion_p
;
3836 /* Identifies all of the argument packs that occur in a template
3837 argument and appends them to the TREE_LIST inside DATA, which is a
3838 find_parameter_pack_data structure. This is a subroutine of
3839 make_pack_expansion and uses_parameter_packs. */
3841 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3844 struct find_parameter_pack_data
* ppd
=
3845 (struct find_parameter_pack_data
*)data
;
3846 bool parameter_pack_p
= false;
3848 /* Don't look through typedefs; we are interested in whether a
3849 parameter pack is actually written in the expression/type we're
3850 looking at, not the target type. */
3851 if (TYPE_P (t
) && typedef_variant_p (t
))
3853 /* But do look at arguments for an alias template. */
3854 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3855 cp_walk_tree (&TI_ARGS (tinfo
),
3856 &find_parameter_packs_r
,
3862 /* Identify whether this is a parameter pack or not. */
3863 switch (TREE_CODE (t
))
3865 case TEMPLATE_PARM_INDEX
:
3866 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3867 parameter_pack_p
= true;
3870 case TEMPLATE_TYPE_PARM
:
3871 t
= TYPE_MAIN_VARIANT (t
);
3873 case TEMPLATE_TEMPLATE_PARM
:
3874 /* If the placeholder appears in the decl-specifier-seq of a function
3875 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3876 is a pack expansion, the invented template parameter is a template
3878 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3879 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3880 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3881 parameter_pack_p
= true;
3886 if (DECL_PACK_P (t
))
3888 /* We don't want to walk into the type of a PARM_DECL,
3889 because we don't want to see the type parameter pack. */
3891 parameter_pack_p
= true;
3896 if (DECL_PACK_P (t
))
3898 /* We don't want to walk into the type of a variadic capture proxy,
3899 because we don't want to see the type parameter pack. */
3901 parameter_pack_p
= true;
3903 else if (variable_template_specialization_p (t
))
3905 cp_walk_tree (&DECL_TI_ARGS (t
),
3906 find_parameter_packs_r
,
3913 if (builtin_pack_call_p (t
))
3914 parameter_pack_p
= true;
3918 parameter_pack_p
= true;
3921 /* Not a parameter pack. */
3925 if (parameter_pack_p
)
3927 /* Add this parameter pack to the list. */
3928 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3932 cp_walk_tree (&TYPE_CONTEXT (t
),
3933 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3935 /* This switch statement will return immediately if we don't find a
3936 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3937 switch (TREE_CODE (t
))
3939 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3940 /* Check the template itself. */
3941 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3942 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3947 tree decl
= DECL_EXPR_DECL (t
);
3948 /* Ignore the declaration of a capture proxy for a parameter pack. */
3949 if (is_capture_proxy (decl
))
3951 if (is_typedef_decl (decl
))
3952 /* Since we stop at typedefs above, we need to look through them at
3953 the point of the DECL_EXPR. */
3954 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3955 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3960 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3962 cp_walk_tree (&TREE_TYPE (t
),
3963 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3967 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3972 case TYPE_PACK_EXPANSION
:
3973 case EXPR_PACK_EXPANSION
:
3978 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3983 case IDENTIFIER_NODE
:
3984 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3991 /* Since we defer implicit capture, look in the parms and body. */
3992 tree fn
= lambda_function (t
);
3993 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3995 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4002 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4003 type_pack_expansion_p to false so that any placeholders
4004 within the expression don't get marked as parameter packs. */
4005 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4006 ppd
->type_pack_expansion_p
= false;
4007 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4009 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4015 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4017 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4019 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4021 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4032 /* Determines if the expression or type T uses any parameter packs. */
4034 uses_parameter_packs (tree t
)
4036 tree parameter_packs
= NULL_TREE
;
4037 struct find_parameter_pack_data ppd
;
4038 ppd
.parameter_packs
= ¶meter_packs
;
4039 ppd
.visited
= new hash_set
<tree
>;
4040 ppd
.type_pack_expansion_p
= false;
4041 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4043 return parameter_packs
;
4046 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4047 representation a base-class initializer into a parameter pack
4048 expansion. If all goes well, the resulting node will be an
4049 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4052 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4055 tree parameter_packs
= NULL_TREE
;
4056 bool for_types
= false;
4057 struct find_parameter_pack_data ppd
;
4059 if (!arg
|| arg
== error_mark_node
)
4062 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4064 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4065 class initializer. In this case, the TREE_PURPOSE will be a
4066 _TYPE node (representing the base class expansion we're
4067 initializing) and the TREE_VALUE will be a TREE_LIST
4068 containing the initialization arguments.
4070 The resulting expansion looks somewhat different from most
4071 expansions. Rather than returning just one _EXPANSION, we
4072 return a TREE_LIST whose TREE_PURPOSE is a
4073 TYPE_PACK_EXPANSION containing the bases that will be
4074 initialized. The TREE_VALUE will be identical to the
4075 original TREE_VALUE, which is a list of arguments that will
4076 be passed to each base. We do not introduce any new pack
4077 expansion nodes into the TREE_VALUE (although it is possible
4078 that some already exist), because the TREE_PURPOSE and
4079 TREE_VALUE all need to be expanded together with the same
4080 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4081 resulting TREE_PURPOSE will mention the parameter packs in
4082 both the bases and the arguments to the bases. */
4085 tree parameter_packs
= NULL_TREE
;
4087 /* Determine which parameter packs will be used by the base
4089 ppd
.visited
= new hash_set
<tree
>;
4090 ppd
.parameter_packs
= ¶meter_packs
;
4091 ppd
.type_pack_expansion_p
= false;
4092 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4093 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4096 if (parameter_packs
== NULL_TREE
)
4098 if (complain
& tf_error
)
4099 error ("base initializer expansion %qT contains no parameter packs",
4102 return error_mark_node
;
4105 if (TREE_VALUE (arg
) != void_type_node
)
4107 /* Collect the sets of parameter packs used in each of the
4108 initialization arguments. */
4109 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4111 /* Determine which parameter packs will be expanded in this
4113 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4120 /* Create the pack expansion type for the base type. */
4121 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4122 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4123 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4124 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4126 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4127 they will rarely be compared to anything. */
4128 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4130 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4133 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4136 /* Build the PACK_EXPANSION_* node. */
4138 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4139 : make_node (EXPR_PACK_EXPANSION
);
4140 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4141 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4143 /* Propagate type and const-expression information. */
4144 TREE_TYPE (result
) = TREE_TYPE (arg
);
4145 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4146 /* Mark this read now, since the expansion might be length 0. */
4147 mark_exp_read (arg
);
4150 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4151 they will rarely be compared to anything. */
4152 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4154 /* Determine which parameter packs will be expanded. */
4155 ppd
.parameter_packs
= ¶meter_packs
;
4156 ppd
.visited
= new hash_set
<tree
>;
4157 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4158 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4161 /* Make sure we found some parameter packs. */
4162 if (parameter_packs
== NULL_TREE
)
4164 if (complain
& tf_error
)
4167 error ("expansion pattern %qT contains no parameter packs", arg
);
4169 error ("expansion pattern %qE contains no parameter packs", arg
);
4171 return error_mark_node
;
4173 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4175 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4180 /* Checks T for any "bare" parameter packs, which have not yet been
4181 expanded, and issues an error if any are found. This operation can
4182 only be done on full expressions or types (e.g., an expression
4183 statement, "if" condition, etc.), because we could have expressions like:
4185 foo(f(g(h(args)))...)
4187 where "args" is a parameter pack. check_for_bare_parameter_packs
4188 should not be called for the subexpressions args, h(args),
4189 g(h(args)), or f(g(h(args))), because we would produce erroneous
4192 Returns TRUE and emits an error if there were bare parameter packs,
4193 returns FALSE otherwise. */
4195 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4197 tree parameter_packs
= NULL_TREE
;
4198 struct find_parameter_pack_data ppd
;
4200 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4203 /* A lambda might use a parameter pack from the containing context. */
4204 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4205 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4208 if (TREE_CODE (t
) == TYPE_DECL
)
4211 ppd
.parameter_packs
= ¶meter_packs
;
4212 ppd
.visited
= new hash_set
<tree
>;
4213 ppd
.type_pack_expansion_p
= false;
4214 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4217 if (parameter_packs
)
4219 if (loc
== UNKNOWN_LOCATION
)
4220 loc
= cp_expr_loc_or_input_loc (t
);
4221 error_at (loc
, "parameter packs not expanded with %<...%>:");
4222 while (parameter_packs
)
4224 tree pack
= TREE_VALUE (parameter_packs
);
4225 tree name
= NULL_TREE
;
4227 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4228 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4229 name
= TYPE_NAME (pack
);
4230 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4231 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4232 else if (TREE_CODE (pack
) == CALL_EXPR
)
4233 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4235 name
= DECL_NAME (pack
);
4238 inform (loc
, " %qD", name
);
4240 inform (loc
, " %s", "<anonymous>");
4242 parameter_packs
= TREE_CHAIN (parameter_packs
);
4251 /* Expand any parameter packs that occur in the template arguments in
4254 expand_template_argument_pack (tree args
)
4256 if (args
== error_mark_node
)
4257 return error_mark_node
;
4259 tree result_args
= NULL_TREE
;
4260 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4261 int num_result_args
= -1;
4262 int non_default_args_count
= -1;
4264 /* First, determine if we need to expand anything, and the number of
4265 slots we'll need. */
4266 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4268 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4269 if (arg
== NULL_TREE
)
4271 if (ARGUMENT_PACK_P (arg
))
4273 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4274 if (num_result_args
< 0)
4275 num_result_args
= in_arg
+ num_packed
;
4277 num_result_args
+= num_packed
;
4281 if (num_result_args
>= 0)
4286 /* If no expansion is necessary, we're done. */
4287 if (num_result_args
< 0)
4290 /* Expand arguments. */
4291 result_args
= make_tree_vec (num_result_args
);
4292 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4293 non_default_args_count
=
4294 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4295 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4297 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4298 if (ARGUMENT_PACK_P (arg
))
4300 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4301 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4302 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4303 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4304 if (non_default_args_count
> 0)
4305 non_default_args_count
+= num_packed
- 1;
4309 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4313 if (non_default_args_count
>= 0)
4314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4318 /* Checks if DECL shadows a template parameter.
4320 [temp.local]: A template-parameter shall not be redeclared within its
4321 scope (including nested scopes).
4323 Emits an error and returns TRUE if the DECL shadows a parameter,
4324 returns FALSE otherwise. */
4327 check_template_shadow (tree decl
)
4331 /* If we're not in a template, we can't possibly shadow a template
4333 if (!current_template_parms
)
4336 /* Figure out what we're shadowing. */
4337 decl
= OVL_FIRST (decl
);
4338 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4340 /* If there's no previous binding for this name, we're not shadowing
4341 anything, let alone a template parameter. */
4345 /* If we're not shadowing a template parameter, we're done. Note
4346 that OLDDECL might be an OVERLOAD (or perhaps even an
4347 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4349 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4352 /* We check for decl != olddecl to avoid bogus errors for using a
4353 name inside a class. We check TPFI to avoid duplicate errors for
4354 inline member templates. */
4356 || (DECL_TEMPLATE_PARM_P (decl
)
4357 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4360 /* Don't complain about the injected class name, as we've already
4361 complained about the class itself. */
4362 if (DECL_SELF_REFERENCE_P (decl
))
4365 if (DECL_TEMPLATE_PARM_P (decl
))
4366 error ("declaration of template parameter %q+D shadows "
4367 "template parameter", decl
);
4369 error ("declaration of %q+#D shadows template parameter", decl
);
4370 inform (DECL_SOURCE_LOCATION (olddecl
),
4371 "template parameter %qD declared here", olddecl
);
4375 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4376 ORIG_LEVEL, DECL, and TYPE. */
4379 build_template_parm_index (int index
,
4385 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4386 TEMPLATE_PARM_IDX (t
) = index
;
4387 TEMPLATE_PARM_LEVEL (t
) = level
;
4388 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4389 TEMPLATE_PARM_DECL (t
) = decl
;
4390 TREE_TYPE (t
) = type
;
4391 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4392 TREE_READONLY (t
) = TREE_READONLY (decl
);
4397 /* Find the canonical type parameter for the given template type
4398 parameter. Returns the canonical type parameter, which may be TYPE
4399 if no such parameter existed. */
4402 canonical_type_parameter (tree type
)
4405 int idx
= TEMPLATE_TYPE_IDX (type
);
4407 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4409 if (!canonical_template_parms
)
4410 vec_alloc (canonical_template_parms
, idx
+ 1);
4412 if (canonical_template_parms
->length () <= (unsigned) idx
)
4413 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4415 list
= (*canonical_template_parms
)[idx
];
4416 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4417 list
= TREE_CHAIN (list
);
4420 return TREE_VALUE (list
);
4423 (*canonical_template_parms
)[idx
]
4424 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4429 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4430 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4431 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4432 new one is created. */
4435 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4436 tsubst_flags_t complain
)
4438 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4439 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4440 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4441 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4443 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4445 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4446 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4448 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4449 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4450 DECL_ARTIFICIAL (decl
) = 1;
4451 SET_DECL_TEMPLATE_PARM_P (decl
);
4453 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4454 TEMPLATE_PARM_LEVEL (index
) - levels
,
4455 TEMPLATE_PARM_ORIG_LEVEL (index
),
4457 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4458 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4459 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4461 /* Template template parameters need this. */
4463 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4465 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4466 TYPE_DECL
, DECL_NAME (decl
), type
);
4467 DECL_TEMPLATE_RESULT (decl
) = inner
;
4468 DECL_ARTIFICIAL (inner
) = true;
4469 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4470 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4473 /* Attach the TPI to the decl. */
4474 if (TREE_CODE (inner
) == TYPE_DECL
)
4475 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4477 DECL_INITIAL (decl
) = tpi
;
4480 return TEMPLATE_PARM_DESCENDANTS (index
);
4483 /* Process information from new template parameter PARM and append it
4484 to the LIST being built. This new parameter is a non-type
4485 parameter iff IS_NON_TYPE is true. This new parameter is a
4486 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4490 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4491 bool is_non_type
, bool is_parameter_pack
)
4493 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4494 tree prev
= NULL_TREE
;
4499 prev
= tree_last (list
);
4501 tree p
= TREE_VALUE (prev
);
4502 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4503 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4504 else if (TREE_CODE (p
) == PARM_DECL
)
4505 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4510 tree decl
= NULL_TREE
;
4511 tree defval
= TREE_PURPOSE (parm
);
4512 tree constr
= TREE_TYPE (parm
);
4516 parm
= TREE_VALUE (parm
);
4518 SET_DECL_TEMPLATE_PARM_P (parm
);
4520 if (TREE_TYPE (parm
) != error_mark_node
)
4524 The top-level cv-qualifiers on the template-parameter are
4525 ignored when determining its type. */
4526 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4527 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4528 TREE_TYPE (parm
) = error_mark_node
;
4529 else if (uses_parameter_packs (TREE_TYPE (parm
))
4530 && !is_parameter_pack
4531 /* If we're in a nested template parameter list, the template
4532 template parameter could be a parameter pack. */
4533 && processing_template_parmlist
== 1)
4535 /* This template parameter is not a parameter pack, but it
4536 should be. Complain about "bare" parameter packs. */
4537 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4539 /* Recover by calling this a parameter pack. */
4540 is_parameter_pack
= true;
4544 /* A template parameter is not modifiable. */
4545 TREE_CONSTANT (parm
) = 1;
4546 TREE_READONLY (parm
) = 1;
4547 decl
= build_decl (parm_loc
,
4548 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4549 TREE_CONSTANT (decl
) = 1;
4550 TREE_READONLY (decl
) = 1;
4551 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4552 = build_template_parm_index (idx
, processing_template_decl
,
4553 processing_template_decl
,
4554 decl
, TREE_TYPE (parm
));
4556 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4557 = is_parameter_pack
;
4562 parm
= TREE_VALUE (TREE_VALUE (parm
));
4564 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4566 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4567 /* This is for distinguishing between real templates and template
4568 template parameters */
4569 TREE_TYPE (parm
) = t
;
4570 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4575 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4576 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4577 decl
= build_decl (parm_loc
,
4578 TYPE_DECL
, parm
, t
);
4581 TYPE_NAME (t
) = decl
;
4582 TYPE_STUB_DECL (t
) = decl
;
4584 TEMPLATE_TYPE_PARM_INDEX (t
)
4585 = build_template_parm_index (idx
, processing_template_decl
,
4586 processing_template_decl
,
4587 decl
, TREE_TYPE (parm
));
4588 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4589 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4590 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4592 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4594 DECL_ARTIFICIAL (decl
) = 1;
4595 SET_DECL_TEMPLATE_PARM_P (decl
);
4597 /* Build requirements for the type/template parameter.
4598 This must be done after SET_DECL_TEMPLATE_PARM_P or
4599 process_template_parm could fail. */
4600 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4602 decl
= pushdecl (decl
);
4606 /* Build the parameter node linking the parameter declaration,
4607 its default argument (if any), and its constraints (if any). */
4608 parm
= build_tree_list (defval
, parm
);
4609 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4612 TREE_CHAIN (prev
) = parm
;
4619 /* The end of a template parameter list has been reached. Process the
4620 tree list into a parameter vector, converting each parameter into a more
4621 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4625 end_template_parm_list (tree parms
)
4627 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4629 /* Pop the dummy parameter level and add the real one. We do not
4630 morph the dummy parameter in place, as it might have been
4631 captured by a (nested) template-template-parm. */
4632 current_template_parms
= TREE_CHAIN (current_template_parms
);
4634 current_template_parms
4635 = tree_cons (size_int (processing_template_decl
),
4636 saved_parmlist
, current_template_parms
);
4638 for (unsigned ix
= 0; parms
; ix
++)
4641 parms
= TREE_CHAIN (parms
);
4642 TREE_CHAIN (parm
) = NULL_TREE
;
4644 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4647 --processing_template_parmlist
;
4649 return saved_parmlist
;
4652 // Explicitly indicate the end of the template parameter list. We assume
4653 // that the current template parameters have been constructed and/or
4654 // managed explicitly, as when creating new template template parameters
4655 // from a shorthand constraint.
4657 end_template_parm_list ()
4659 --processing_template_parmlist
;
4662 /* end_template_decl is called after a template declaration is seen. */
4665 end_template_decl (void)
4667 reset_specialization ();
4669 if (! processing_template_decl
)
4672 /* This matches the pushlevel in begin_template_parm_list. */
4675 --processing_template_decl
;
4676 current_template_parms
= TREE_CHAIN (current_template_parms
);
4679 /* Takes a TREE_LIST representing a template parameter and convert it
4680 into an argument suitable to be passed to the type substitution
4681 functions. Note that If the TREE_LIST contains an error_mark
4682 node, the returned argument is error_mark_node. */
4685 template_parm_to_arg (tree t
)
4689 || TREE_CODE (t
) != TREE_LIST
)
4692 if (error_operand_p (TREE_VALUE (t
)))
4693 return error_mark_node
;
4697 if (TREE_CODE (t
) == TYPE_DECL
4698 || TREE_CODE (t
) == TEMPLATE_DECL
)
4702 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4704 /* Turn this argument into a TYPE_ARGUMENT_PACK
4705 with a single element, which expands T. */
4706 tree vec
= make_tree_vec (1);
4708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4710 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4712 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4713 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4718 t
= DECL_INITIAL (t
);
4720 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4722 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4723 with a single element, which expands T. */
4724 tree vec
= make_tree_vec (1);
4726 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4728 t
= convert_from_reference (t
);
4729 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4731 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4732 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4735 t
= convert_from_reference (t
);
4740 /* Given a single level of template parameters (a TREE_VEC), return it
4741 as a set of template arguments. */
4744 template_parms_level_to_args (tree parms
)
4746 tree a
= copy_node (parms
);
4747 TREE_TYPE (a
) = NULL_TREE
;
4748 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4749 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4752 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4757 /* Given a set of template parameters, return them as a set of template
4758 arguments. The template parameters are represented as a TREE_VEC, in
4759 the form documented in cp-tree.h for template arguments. */
4762 template_parms_to_args (tree parms
)
4765 tree args
= NULL_TREE
;
4766 int length
= TMPL_PARMS_DEPTH (parms
);
4769 /* If there is only one level of template parameters, we do not
4770 create a TREE_VEC of TREE_VECs. Instead, we return a single
4771 TREE_VEC containing the arguments. */
4773 args
= make_tree_vec (length
);
4775 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4777 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4780 TREE_VEC_ELT (args
, --l
) = a
;
4788 /* Within the declaration of a template, return the currently active
4789 template parameters as an argument TREE_VEC. */
4792 current_template_args (void)
4794 return template_parms_to_args (current_template_parms
);
4797 /* Return the fully generic arguments for of TMPL, i.e. what
4798 current_template_args would be while parsing it. */
4801 generic_targs_for (tree tmpl
)
4803 if (tmpl
== NULL_TREE
)
4805 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4806 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4807 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4808 template parameter, it has no TEMPLATE_INFO; for a partial
4809 specialization, it has the arguments for the primary template, and we
4810 want the arguments for the partial specialization. */;
4811 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4812 if (tree ti
= get_template_info (result
))
4813 return TI_ARGS (ti
);
4814 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4817 /* Update the declared TYPE by doing any lookups which were thought to be
4818 dependent, but are not now that we know the SCOPE of the declarator. */
4821 maybe_update_decl_type (tree orig_type
, tree scope
)
4823 tree type
= orig_type
;
4825 if (type
== NULL_TREE
)
4828 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4829 type
= TREE_TYPE (type
);
4831 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4832 && dependent_type_p (type
)
4833 /* Don't bother building up the args in this case. */
4834 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4836 /* tsubst in the args corresponding to the template parameters,
4837 including auto if present. Most things will be unchanged, but
4838 make_typename_type and tsubst_qualified_id will resolve
4839 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4840 tree args
= current_template_args ();
4841 tree auto_node
= type_uses_auto (type
);
4845 tree auto_vec
= make_tree_vec (1);
4846 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4847 args
= add_to_template_args (args
, auto_vec
);
4849 pushed
= push_scope (scope
);
4850 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4855 if (type
== error_mark_node
)
4858 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4860 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4863 type
= TYPE_NAME (type
);
4868 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4869 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4870 the new template is a member template. */
4873 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4875 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4876 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4877 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4878 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4879 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4880 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4885 struct template_parm_data
4887 /* The level of the template parameters we are currently
4891 /* The index of the specialization argument we are currently
4895 /* An array whose size is the number of template parameters. The
4896 elements are nonzero if the parameter has been used in any one
4897 of the arguments processed so far. */
4900 /* An array whose size is the number of template arguments. The
4901 elements are nonzero if the argument makes use of template
4902 parameters of this level. */
4903 int* arg_uses_template_parms
;
4906 /* Subroutine of push_template_decl used to see if each template
4907 parameter in a partial specialization is used in the explicit
4908 argument list. If T is of the LEVEL given in DATA (which is
4909 treated as a template_parm_data*), then DATA->PARMS is marked
4913 mark_template_parm (tree t
, void* data
)
4917 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4919 template_parm_level_and_index (t
, &level
, &idx
);
4921 if (level
== tpd
->level
)
4923 tpd
->parms
[idx
] = 1;
4924 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4927 /* In C++17 the type of a non-type argument is a deduced context. */
4928 if (cxx_dialect
>= cxx17
4929 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4930 for_each_template_parm (TREE_TYPE (t
),
4931 &mark_template_parm
,
4934 /*include_nondeduced_p=*/false);
4936 /* Return zero so that for_each_template_parm will continue the
4937 traversal of the tree; we want to mark *every* template parm. */
4941 /* Process the partial specialization DECL. */
4944 process_partial_specialization (tree decl
)
4946 tree type
= TREE_TYPE (decl
);
4947 tree tinfo
= get_template_info (decl
);
4948 tree maintmpl
= TI_TEMPLATE (tinfo
);
4949 tree specargs
= TI_ARGS (tinfo
);
4950 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4951 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4954 int nargs
= TREE_VEC_LENGTH (inner_args
);
4957 bool did_error_intro
= false;
4958 struct template_parm_data tpd
;
4959 struct template_parm_data tpd2
;
4961 gcc_assert (current_template_parms
);
4963 /* A concept cannot be specialized. */
4964 if (flag_concepts
&& variable_concept_p (maintmpl
))
4966 error ("specialization of variable concept %q#D", maintmpl
);
4967 return error_mark_node
;
4970 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4971 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4973 /* We check that each of the template parameters given in the
4974 partial specialization is used in the argument list to the
4975 specialization. For example:
4977 template <class T> struct S;
4978 template <class T> struct S<T*>;
4980 The second declaration is OK because `T*' uses the template
4981 parameter T, whereas
4983 template <class T> struct S<int>;
4985 is no good. Even trickier is:
4996 The S2<T> declaration is actually invalid; it is a
4997 full-specialization. Of course,
5000 struct S2<T (*)(U)>;
5002 or some such would have been OK. */
5003 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5004 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5005 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5007 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5008 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5009 for (i
= 0; i
< nargs
; ++i
)
5011 tpd
.current_arg
= i
;
5012 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5013 &mark_template_parm
,
5016 /*include_nondeduced_p=*/false);
5018 for (i
= 0; i
< ntparms
; ++i
)
5019 if (tpd
.parms
[i
] == 0)
5021 /* One of the template parms was not used in a deduced context in the
5023 if (!did_error_intro
)
5025 error ("template parameters not deducible in "
5026 "partial specialization:");
5027 did_error_intro
= true;
5030 inform (input_location
, " %qD",
5031 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5034 if (did_error_intro
)
5035 return error_mark_node
;
5037 /* [temp.class.spec]
5039 The argument list of the specialization shall not be identical to
5040 the implicit argument list of the primary template. */
5042 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5043 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5045 || !strictly_subsumes (current_template_constraints (),
5046 inner_args
, maintmpl
)))
5049 error ("partial specialization %q+D does not specialize "
5050 "any template arguments; to define the primary template, "
5051 "remove the template argument list", decl
);
5053 error ("partial specialization %q+D does not specialize any "
5054 "template arguments and is not more constrained than "
5055 "the primary template; to define the primary template, "
5056 "remove the template argument list", decl
);
5057 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5060 /* A partial specialization that replaces multiple parameters of the
5061 primary template with a pack expansion is less specialized for those
5063 if (nargs
< DECL_NTPARMS (maintmpl
))
5065 error ("partial specialization is not more specialized than the "
5066 "primary template because it replaces multiple parameters "
5067 "with a pack expansion");
5068 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5069 /* Avoid crash in process_partial_specialization. */
5073 else if (nargs
> DECL_NTPARMS (maintmpl
))
5075 error ("too many arguments for partial specialization %qT", type
);
5076 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5077 /* Avoid crash below. */
5081 /* If we aren't in a dependent class, we can actually try deduction. */
5082 else if (tpd
.level
== 1
5083 /* FIXME we should be able to handle a partial specialization of a
5084 partial instantiation, but currently we can't (c++/41727). */
5085 && TMPL_ARGS_DEPTH (specargs
) == 1
5086 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5088 auto_diagnostic_group d
;
5089 if (permerror (input_location
, "partial specialization %qD is not "
5090 "more specialized than", decl
))
5091 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5095 /* [temp.class.spec]
5097 A partially specialized non-type argument expression shall not
5098 involve template parameters of the partial specialization except
5099 when the argument expression is a simple identifier.
5101 The type of a template parameter corresponding to a specialized
5102 non-type argument shall not be dependent on a parameter of the
5105 Also, we verify that pack expansions only occur at the
5106 end of the argument list. */
5108 for (i
= 0; i
< nargs
; ++i
)
5110 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5111 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5112 tree packed_args
= NULL_TREE
;
5115 if (ARGUMENT_PACK_P (arg
))
5117 /* Extract the arguments from the argument pack. We'll be
5118 iterating over these in the following loop. */
5119 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5120 len
= TREE_VEC_LENGTH (packed_args
);
5123 for (j
= 0; j
< len
; j
++)
5126 /* Get the Jth argument in the parameter pack. */
5127 arg
= TREE_VEC_ELT (packed_args
, j
);
5129 if (PACK_EXPANSION_P (arg
))
5131 /* Pack expansions must come at the end of the
5133 if ((packed_args
&& j
< len
- 1)
5134 || (!packed_args
&& i
< nargs
- 1))
5136 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5137 error ("parameter pack argument %qE must be at the "
5138 "end of the template argument list", arg
);
5140 error ("parameter pack argument %qT must be at the "
5141 "end of the template argument list", arg
);
5145 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5146 /* We only care about the pattern. */
5147 arg
= PACK_EXPANSION_PATTERN (arg
);
5149 if (/* These first two lines are the `non-type' bit. */
5151 && TREE_CODE (arg
) != TEMPLATE_DECL
5152 /* This next two lines are the `argument expression is not just a
5153 simple identifier' condition and also the `specialized
5154 non-type argument' bit. */
5155 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5156 && !((REFERENCE_REF_P (arg
)
5157 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5158 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5160 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5161 || (packed_args
&& uses_template_parms (arg
)))
5162 error_at (cp_expr_loc_or_input_loc (arg
),
5163 "template argument %qE involves template "
5164 "parameter(s)", arg
);
5167 /* Look at the corresponding template parameter,
5168 marking which template parameters its type depends
5170 tree type
= TREE_TYPE (parm
);
5174 /* We haven't yet initialized TPD2. Do so now. */
5175 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5176 /* The number of parameters here is the number in the
5177 main template, which, as checked in the assertion
5179 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5181 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5184 /* Mark the template parameters. But this time, we're
5185 looking for the template parameters of the main
5186 template, not in the specialization. */
5187 tpd2
.current_arg
= i
;
5188 tpd2
.arg_uses_template_parms
[i
] = 0;
5189 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5190 for_each_template_parm (type
,
5191 &mark_template_parm
,
5194 /*include_nondeduced_p=*/false);
5196 if (tpd2
.arg_uses_template_parms
[i
])
5198 /* The type depended on some template parameters.
5199 If they are fully specialized in the
5200 specialization, that's OK. */
5203 for (j
= 0; j
< nargs
; ++j
)
5204 if (tpd2
.parms
[j
] != 0
5205 && tpd
.arg_uses_template_parms
[j
])
5208 error_n (input_location
, count
,
5209 "type %qT of template argument %qE depends "
5210 "on a template parameter",
5211 "type %qT of template argument %qE depends "
5212 "on template parameters",
5221 /* We should only get here once. */
5222 if (TREE_CODE (decl
) == TYPE_DECL
)
5223 gcc_assert (!COMPLETE_TYPE_P (type
));
5225 // Build the template decl.
5226 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5227 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5228 TREE_TYPE (tmpl
) = type
;
5229 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5230 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5231 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5232 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5234 /* Give template template parms a DECL_CONTEXT of the template
5235 for which they are a parameter. */
5236 for (i
= 0; i
< ntparms
; ++i
)
5238 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5239 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5240 DECL_CONTEXT (parm
) = tmpl
;
5244 /* We didn't register this in check_explicit_specialization so we could
5245 wait until the constraints were set. */
5246 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5248 associate_classtype_constraints (type
);
5250 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5251 = tree_cons (specargs
, tmpl
,
5252 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5253 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5255 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5256 inst
= TREE_CHAIN (inst
))
5258 tree instance
= TREE_VALUE (inst
);
5259 if (TYPE_P (instance
)
5260 ? (COMPLETE_TYPE_P (instance
)
5261 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5262 : DECL_TEMPLATE_INSTANTIATION (instance
))
5264 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5265 tree inst_decl
= (DECL_P (instance
)
5266 ? instance
: TYPE_NAME (instance
));
5269 else if (spec
== error_mark_node
)
5270 permerror (input_location
,
5271 "declaration of %qD ambiguates earlier template "
5272 "instantiation for %qD", decl
, inst_decl
);
5273 else if (TREE_VALUE (spec
) == tmpl
)
5274 permerror (input_location
,
5275 "partial specialization of %qD after instantiation "
5276 "of %qD", decl
, inst_decl
);
5283 /* PARM is a template parameter of some form; return the corresponding
5284 TEMPLATE_PARM_INDEX. */
5287 get_template_parm_index (tree parm
)
5289 if (TREE_CODE (parm
) == PARM_DECL
5290 || TREE_CODE (parm
) == CONST_DECL
)
5291 parm
= DECL_INITIAL (parm
);
5292 else if (TREE_CODE (parm
) == TYPE_DECL
5293 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5294 parm
= TREE_TYPE (parm
);
5295 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5296 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5297 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5298 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5299 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5303 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5304 parameter packs used by the template parameter PARM. */
5307 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5309 /* A type parm can't refer to another parm. */
5310 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5312 else if (TREE_CODE (parm
) == PARM_DECL
)
5314 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5319 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5321 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5322 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5324 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5325 if (template_parameter_pack_p (p
))
5326 /* Any packs in the type are expanded by this parameter. */;
5328 fixed_parameter_pack_p_1 (p
, ppd
);
5332 /* PARM is a template parameter pack. Return any parameter packs used in
5333 its type or the type of any of its template parameters. If there are
5334 any such packs, it will be instantiated into a fixed template parameter
5335 list by partial instantiation rather than be fully deduced. */
5338 fixed_parameter_pack_p (tree parm
)
5340 /* This can only be true in a member template. */
5341 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5343 /* This can only be true for a parameter pack. */
5344 if (!template_parameter_pack_p (parm
))
5346 /* A type parm can't refer to another parm. */
5347 if (TREE_CODE (parm
) == TYPE_DECL
)
5350 tree parameter_packs
= NULL_TREE
;
5351 struct find_parameter_pack_data ppd
;
5352 ppd
.parameter_packs
= ¶meter_packs
;
5353 ppd
.visited
= new hash_set
<tree
>;
5354 ppd
.type_pack_expansion_p
= false;
5356 fixed_parameter_pack_p_1 (parm
, &ppd
);
5359 return parameter_packs
;
5362 /* Check that a template declaration's use of default arguments and
5363 parameter packs is not invalid. Here, PARMS are the template
5364 parameters. IS_PRIMARY is true if DECL is the thing declared by
5365 a primary template. IS_PARTIAL is true if DECL is a partial
5368 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5369 function template declaration or a friend class template
5370 declaration. In the function case, 1 indicates a declaration, 2
5371 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5372 emitted for extraneous default arguments.
5374 Returns TRUE if there were no errors found, FALSE otherwise. */
5377 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5378 bool is_partial
, int is_friend_decl
)
5381 int last_level_to_check
;
5383 bool no_errors
= true;
5387 A default template-argument shall not be specified in a
5388 function template declaration or a function template definition, nor
5389 in the template-parameter-list of the definition of a member of a
5392 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5393 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5394 /* You can't have a function template declaration in a local
5395 scope, nor you can you define a member of a class template in a
5399 if ((TREE_CODE (decl
) == TYPE_DECL
5401 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5402 || (TREE_CODE (decl
) == FUNCTION_DECL
5403 && LAMBDA_FUNCTION_P (decl
)))
5404 /* A lambda doesn't have an explicit declaration; don't complain
5405 about the parms of the enclosing class. */
5408 if (current_class_type
5409 && !TYPE_BEING_DEFINED (current_class_type
)
5410 && DECL_LANG_SPECIFIC (decl
)
5411 && DECL_DECLARES_FUNCTION_P (decl
)
5412 /* If this is either a friend defined in the scope of the class
5413 or a member function. */
5414 && (DECL_FUNCTION_MEMBER_P (decl
)
5415 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5416 : DECL_FRIEND_CONTEXT (decl
)
5417 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5419 /* And, if it was a member function, it really was defined in
5420 the scope of the class. */
5421 && (!DECL_FUNCTION_MEMBER_P (decl
)
5422 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5423 /* We already checked these parameters when the template was
5424 declared, so there's no need to do it again now. This function
5425 was defined in class scope, but we're processing its body now
5426 that the class is complete. */
5429 /* Core issue 226 (C++0x only): the following only applies to class
5432 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5436 If a template-parameter has a default template-argument, all
5437 subsequent template-parameters shall have a default
5438 template-argument supplied. */
5439 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5441 tree inner_parms
= TREE_VALUE (parm_level
);
5442 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5443 int seen_def_arg_p
= 0;
5446 for (i
= 0; i
< ntparms
; ++i
)
5448 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5450 if (parm
== error_mark_node
)
5453 if (TREE_PURPOSE (parm
))
5455 else if (seen_def_arg_p
5456 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5458 error ("no default argument for %qD", TREE_VALUE (parm
));
5459 /* For better subsequent error-recovery, we indicate that
5460 there should have been a default argument. */
5461 TREE_PURPOSE (parm
) = error_mark_node
;
5464 else if (!is_partial
5466 /* Don't complain about an enclosing partial
5468 && parm_level
== parms
5469 && TREE_CODE (decl
) == TYPE_DECL
5471 && template_parameter_pack_p (TREE_VALUE (parm
))
5472 /* A fixed parameter pack will be partially
5473 instantiated into a fixed length list. */
5474 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5476 /* A primary class template can only have one
5477 parameter pack, at the end of the template
5480 error ("parameter pack %q+D must be at the end of the"
5481 " template parameter list", TREE_VALUE (parm
));
5483 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5491 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5495 /* For an ordinary class template, default template arguments are
5496 allowed at the innermost level, e.g.:
5497 template <class T = int>
5499 but, in a partial specialization, they're not allowed even
5500 there, as we have in [temp.class.spec]:
5502 The template parameter list of a specialization shall not
5503 contain default template argument values.
5505 So, for a partial specialization, or for a function template
5506 (in C++98/C++03), we look at all of them. */
5509 /* But, for a primary class template that is not a partial
5510 specialization we look at all template parameters except the
5512 parms
= TREE_CHAIN (parms
);
5514 /* Figure out what error message to issue. */
5515 if (is_friend_decl
== 2)
5516 msg
= G_("default template arguments may not be used in function template "
5517 "friend re-declaration");
5518 else if (is_friend_decl
)
5519 msg
= G_("default template arguments may not be used in template "
5520 "friend declarations");
5521 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5522 msg
= G_("default template arguments may not be used in function templates "
5523 "without %<-std=c++11%> or %<-std=gnu++11%>");
5524 else if (is_partial
)
5525 msg
= G_("default template arguments may not be used in "
5526 "partial specializations");
5527 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5528 msg
= G_("default argument for template parameter for class enclosing %qD");
5530 /* Per [temp.param]/9, "A default template-argument shall not be
5531 specified in the template-parameter-lists of the definition of
5532 a member of a class template that appears outside of the member's
5533 class.", thus if we aren't handling a member of a class template
5534 there is no need to examine the parameters. */
5537 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5538 /* If we're inside a class definition, there's no need to
5539 examine the parameters to the class itself. On the one
5540 hand, they will be checked when the class is defined, and,
5541 on the other, default arguments are valid in things like:
5542 template <class T = double>
5543 struct S { template <class U> void f(U); };
5544 Here the default argument for `S' has no bearing on the
5545 declaration of `f'. */
5546 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5548 /* Check everything. */
5549 last_level_to_check
= 0;
5551 for (parm_level
= parms
;
5552 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5553 parm_level
= TREE_CHAIN (parm_level
))
5555 tree inner_parms
= TREE_VALUE (parm_level
);
5559 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5560 for (i
= 0; i
< ntparms
; ++i
)
5562 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5565 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5570 if (is_friend_decl
== 2)
5577 /* Clear out the default argument so that we are not
5579 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5583 /* At this point, if we're still interested in issuing messages,
5584 they must apply to classes surrounding the object declared. */
5586 msg
= G_("default argument for template parameter for class "
5593 /* Worker for push_template_decl_real, called via
5594 for_each_template_parm. DATA is really an int, indicating the
5595 level of the parameters we are interested in. If T is a template
5596 parameter of that level, return nonzero. */
5599 template_parm_this_level_p (tree t
, void* data
)
5601 int this_level
= *(int *)data
;
5604 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5605 level
= TEMPLATE_PARM_LEVEL (t
);
5607 level
= TEMPLATE_TYPE_LEVEL (t
);
5608 return level
== this_level
;
5611 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5612 DATA is really an int, indicating the innermost outer level of parameters.
5613 If T is a template parameter of that level or further out, return
5617 template_parm_outer_level (tree t
, void *data
)
5619 int this_level
= *(int *)data
;
5622 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5623 level
= TEMPLATE_PARM_LEVEL (t
);
5625 level
= TEMPLATE_TYPE_LEVEL (t
);
5626 return level
<= this_level
;
5629 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5630 parameters given by current_template_args, or reuses a
5631 previously existing one, if appropriate. Returns the DECL, or an
5632 equivalent one, if it is replaced via a call to duplicate_decls.
5634 If IS_FRIEND is true, DECL is a friend declaration. */
5637 push_template_decl_real (tree decl
, bool is_friend
)
5645 int new_template_p
= 0;
5646 /* True if the template is a member template, in the sense of
5648 bool member_template_p
= false;
5650 if (decl
== error_mark_node
|| !current_template_parms
)
5651 return error_mark_node
;
5653 /* See if this is a partial specialization. */
5654 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5655 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5656 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5658 && DECL_LANG_SPECIFIC (decl
)
5659 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5660 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5662 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5666 /* For a friend, we want the context of the friend, not
5667 the type of which it is a friend. */
5668 ctx
= CP_DECL_CONTEXT (decl
);
5669 else if (CP_DECL_CONTEXT (decl
)
5670 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5671 /* In the case of a virtual function, we want the class in which
5673 ctx
= CP_DECL_CONTEXT (decl
);
5675 /* Otherwise, if we're currently defining some class, the DECL
5676 is assumed to be a member of the class. */
5677 ctx
= current_scope ();
5679 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5682 if (!DECL_CONTEXT (decl
))
5683 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5685 /* See if this is a primary template. */
5686 if (is_friend
&& ctx
5687 && uses_template_parms_level (ctx
, processing_template_decl
))
5688 /* A friend template that specifies a class context, i.e.
5689 template <typename T> friend void A<T>::f();
5692 else if (TREE_CODE (decl
) == TYPE_DECL
5693 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5696 is_primary
= template_parm_scope_p ();
5700 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5702 if (DECL_CLASS_SCOPE_P (decl
))
5703 member_template_p
= true;
5704 if (TREE_CODE (decl
) == TYPE_DECL
5705 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5707 error ("template class without a name");
5708 return error_mark_node
;
5710 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5712 if (member_template_p
)
5714 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5715 error ("member template %qD may not have virt-specifiers", decl
);
5717 if (DECL_DESTRUCTOR_P (decl
))
5721 A destructor shall not be a member template. */
5722 error_at (DECL_SOURCE_LOCATION (decl
),
5723 "destructor %qD declared as member template", decl
);
5724 return error_mark_node
;
5726 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5727 && (!prototype_p (TREE_TYPE (decl
))
5728 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5729 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5730 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5731 == void_list_node
)))
5733 /* [basic.stc.dynamic.allocation]
5735 An allocation function can be a function
5736 template. ... Template allocation functions shall
5737 have two or more parameters. */
5738 error ("invalid template declaration of %qD", decl
);
5739 return error_mark_node
;
5742 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5743 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5745 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5746 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5747 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5749 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5750 if (TREE_CODE (t
) == TYPE_DECL
)
5752 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5753 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5756 else if (TREE_CODE (decl
) == TYPE_DECL
5757 && TYPE_DECL_ALIAS_P (decl
))
5758 /* alias-declaration */
5759 gcc_assert (!DECL_ARTIFICIAL (decl
));
5760 else if (VAR_P (decl
))
5761 /* C++14 variable template. */;
5762 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5763 /* C++2a concept definitions. */;
5766 error ("template declaration of %q#D", decl
);
5767 return error_mark_node
;
5771 /* Check to see that the rules regarding the use of default
5772 arguments are not being violated. We check args for a friend
5773 functions when we know whether it's a definition, introducing
5774 declaration or re-declaration. */
5775 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5776 check_default_tmpl_args (decl
, current_template_parms
,
5777 is_primary
, is_partial
, is_friend
);
5779 /* Ensure that there are no parameter packs in the type of this
5780 declaration that have not been expanded. */
5781 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5783 /* Check each of the arguments individually to see if there are
5784 any bare parameter packs. */
5785 tree type
= TREE_TYPE (decl
);
5786 tree arg
= DECL_ARGUMENTS (decl
);
5787 tree argtype
= TYPE_ARG_TYPES (type
);
5789 while (arg
&& argtype
)
5791 if (!DECL_PACK_P (arg
)
5792 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5794 /* This is a PARM_DECL that contains unexpanded parameter
5795 packs. We have already complained about this in the
5796 check_for_bare_parameter_packs call, so just replace
5797 these types with ERROR_MARK_NODE. */
5798 TREE_TYPE (arg
) = error_mark_node
;
5799 TREE_VALUE (argtype
) = error_mark_node
;
5802 arg
= DECL_CHAIN (arg
);
5803 argtype
= TREE_CHAIN (argtype
);
5806 /* Check for bare parameter packs in the return type and the
5807 exception specifiers. */
5808 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5809 /* Errors were already issued, set return type to int
5810 as the frontend doesn't expect error_mark_node as
5812 TREE_TYPE (type
) = integer_type_node
;
5813 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5814 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5816 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5817 ? DECL_ORIGINAL_TYPE (decl
)
5818 : TREE_TYPE (decl
)))
5820 TREE_TYPE (decl
) = error_mark_node
;
5821 return error_mark_node
;
5825 return process_partial_specialization (decl
);
5827 args
= current_template_args ();
5830 || TREE_CODE (ctx
) == FUNCTION_DECL
5831 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5832 || (TREE_CODE (decl
) == TYPE_DECL
5833 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5834 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5836 if (DECL_LANG_SPECIFIC (decl
)
5837 && DECL_TEMPLATE_INFO (decl
)
5838 && DECL_TI_TEMPLATE (decl
))
5839 tmpl
= DECL_TI_TEMPLATE (decl
);
5840 /* If DECL is a TYPE_DECL for a class-template, then there won't
5841 be DECL_LANG_SPECIFIC. The information equivalent to
5842 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5843 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5844 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5845 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5847 /* Since a template declaration already existed for this
5848 class-type, we must be redeclaring it here. Make sure
5849 that the redeclaration is valid. */
5850 redeclare_class_template (TREE_TYPE (decl
),
5851 current_template_parms
,
5852 current_template_constraints ());
5853 /* We don't need to create a new TEMPLATE_DECL; just use the
5854 one we already had. */
5855 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5859 tmpl
= build_template_decl (decl
, current_template_parms
,
5863 if (DECL_LANG_SPECIFIC (decl
)
5864 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5866 /* A specialization of a member template of a template
5868 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5869 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5870 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5876 tree a
, t
, current
, parms
;
5878 tree tinfo
= get_template_info (decl
);
5882 error ("template definition of non-template %q#D", decl
);
5883 return error_mark_node
;
5886 tmpl
= TI_TEMPLATE (tinfo
);
5888 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5889 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5890 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5891 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5895 /* The declaration is a specialization of a member
5896 template, declared outside the class. Therefore, the
5897 innermost template arguments will be NULL, so we
5898 replace them with the arguments determined by the
5899 earlier call to check_explicit_specialization. */
5900 args
= DECL_TI_ARGS (decl
);
5903 = build_template_decl (decl
, current_template_parms
,
5905 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5906 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5907 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5908 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5909 DECL_TEMPLATE_INFO (new_tmpl
)
5910 = build_template_info (tmpl
, args
);
5912 register_specialization (new_tmpl
,
5913 most_general_template (tmpl
),
5919 /* Make sure the template headers we got make sense. */
5921 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5922 i
= TMPL_PARMS_DEPTH (parms
);
5923 if (TMPL_ARGS_DEPTH (args
) != i
)
5925 error ("expected %d levels of template parms for %q#D, got %d",
5926 i
, decl
, TMPL_ARGS_DEPTH (args
));
5927 DECL_INTERFACE_KNOWN (decl
) = 1;
5928 return error_mark_node
;
5931 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5933 a
= TMPL_ARGS_LEVEL (args
, i
);
5934 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5936 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5938 if (current
== decl
)
5939 error ("got %d template parameters for %q#D",
5940 TREE_VEC_LENGTH (a
), decl
);
5942 error ("got %d template parameters for %q#T",
5943 TREE_VEC_LENGTH (a
), current
);
5944 error (" but %d required", TREE_VEC_LENGTH (t
));
5945 /* Avoid crash in import_export_decl. */
5946 DECL_INTERFACE_KNOWN (decl
) = 1;
5947 return error_mark_node
;
5950 if (current
== decl
)
5952 else if (current
== NULL_TREE
)
5953 /* Can happen in erroneous input. */
5956 current
= get_containing_scope (current
);
5959 /* Check that the parms are used in the appropriate qualifying scopes
5960 in the declarator. */
5961 if (!comp_template_args
5963 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5965 error ("template arguments to %qD do not match original "
5966 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5967 if (!uses_template_parms (TI_ARGS (tinfo
)))
5968 inform (input_location
, "use %<template<>%> for"
5969 " an explicit specialization");
5970 /* Avoid crash in import_export_decl. */
5971 DECL_INTERFACE_KNOWN (decl
) = 1;
5972 return error_mark_node
;
5976 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5977 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5979 /* Push template declarations for global functions and types. Note
5980 that we do not try to push a global template friend declared in a
5981 template class; such a thing may well depend on the template
5982 parameters of the class. */
5983 if (new_template_p
&& !ctx
5984 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5986 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5987 if (tmpl
== error_mark_node
)
5988 return error_mark_node
;
5990 /* Hide template friend classes that haven't been declared yet. */
5991 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5993 DECL_ANTICIPATED (tmpl
) = 1;
5994 DECL_FRIEND_P (tmpl
) = 1;
6000 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6002 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6004 /* Give template template parms a DECL_CONTEXT of the template
6005 for which they are a parameter. */
6006 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6007 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6009 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6010 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6011 DECL_CONTEXT (parm
) = tmpl
;
6014 if (TREE_CODE (decl
) == TYPE_DECL
6015 && TYPE_DECL_ALIAS_P (decl
))
6018 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6020 /* ??? Why don't we do this here for all templates? */
6021 constr
= build_constraints (constr
, NULL_TREE
);
6022 set_constraints (decl
, constr
);
6024 if (complex_alias_template_p (tmpl
))
6025 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6029 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6030 back to its most general template. If TMPL is a specialization,
6031 ARGS may only have the innermost set of arguments. Add the missing
6032 argument levels if necessary. */
6033 if (DECL_TEMPLATE_INFO (tmpl
))
6034 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6036 info
= build_template_info (tmpl
, args
);
6038 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6039 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6043 retrofit_lang_decl (decl
);
6044 if (DECL_LANG_SPECIFIC (decl
))
6045 DECL_TEMPLATE_INFO (decl
) = info
;
6048 if (flag_implicit_templates
6050 && TREE_PUBLIC (decl
)
6051 && VAR_OR_FUNCTION_DECL_P (decl
))
6052 /* Set DECL_COMDAT on template instantiations; if we force
6053 them to be emitted by explicit instantiation,
6054 mark_needed will tell cgraph to do the right thing. */
6055 DECL_COMDAT (decl
) = true;
6057 return DECL_TEMPLATE_RESULT (tmpl
);
6061 push_template_decl (tree decl
)
6063 return push_template_decl_real (decl
, false);
6066 /* FN is an inheriting constructor that inherits from the constructor
6067 template INHERITED; turn FN into a constructor template with a matching
6071 add_inherited_template_parms (tree fn
, tree inherited
)
6074 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6075 inner_parms
= copy_node (inner_parms
);
6077 = tree_cons (size_int (processing_template_decl
+ 1),
6078 inner_parms
, current_template_parms
);
6079 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6080 tree args
= template_parms_to_args (parms
);
6081 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6082 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
6083 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
6084 DECL_ARTIFICIAL (tmpl
) = true;
6085 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6089 /* Called when a class template TYPE is redeclared with the indicated
6090 template PARMS, e.g.:
6092 template <class T> struct S;
6093 template <class T> struct S {}; */
6096 redeclare_class_template (tree type
, tree parms
, tree cons
)
6102 if (!TYPE_TEMPLATE_INFO (type
))
6104 error ("%qT is not a template type", type
);
6108 tmpl
= TYPE_TI_TEMPLATE (type
);
6109 if (!PRIMARY_TEMPLATE_P (tmpl
))
6110 /* The type is nested in some template class. Nothing to worry
6111 about here; there are no new template parameters for the nested
6117 error ("template specifiers not specified in declaration of %qD",
6122 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6123 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6125 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6127 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6128 "redeclared with %d template parameter",
6129 "redeclared with %d template parameters",
6130 TREE_VEC_LENGTH (parms
));
6131 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6132 "previous declaration %qD used %d template parameter",
6133 "previous declaration %qD used %d template parameters",
6134 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6138 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6145 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6146 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6149 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6150 if (error_operand_p (tmpl_parm
))
6153 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6154 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6155 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6157 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6159 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6160 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6161 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6162 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6163 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6164 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6165 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6166 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6167 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6169 auto_diagnostic_group d
;
6170 error ("template parameter %q+#D", tmpl_parm
);
6171 inform (input_location
, "redeclared here as %q#D", parm
);
6175 /* The parameters can be declared to introduce different
6177 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6178 tree p2
= TREE_VEC_ELT (parms
, i
);
6179 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6181 auto_diagnostic_group d
;
6182 error ("declaration of template parameter %q+#D with different "
6183 "constraints", parm
);
6184 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6185 "original declaration appeared here");
6189 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6191 /* We have in [temp.param]:
6193 A template-parameter may not be given default arguments
6194 by two different declarations in the same scope. */
6195 auto_diagnostic_group d
;
6196 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6197 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6198 "original definition appeared here");
6202 if (parm_default
!= NULL_TREE
)
6203 /* Update the previous template parameters (which are the ones
6204 that will really count) with the new default value. */
6205 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6206 else if (tmpl_default
!= NULL_TREE
)
6207 /* Update the new parameters, too; they'll be used as the
6208 parameters for any members. */
6209 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6211 /* Give each template template parm in this redeclaration a
6212 DECL_CONTEXT of the template for which they are a parameter. */
6213 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6215 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6216 DECL_CONTEXT (parm
) = tmpl
;
6219 if (TREE_CODE (parm
) == TYPE_DECL
)
6220 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6223 tree ci
= get_constraints (tmpl
);
6224 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6225 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6227 /* Two classes with different constraints declare different entities. */
6228 if (!cp_tree_equal (req1
, req2
))
6230 auto_diagnostic_group d
;
6231 error_at (input_location
, "redeclaration %q#D with different "
6232 "constraints", tmpl
);
6233 inform (DECL_SOURCE_LOCATION (tmpl
),
6234 "original declaration appeared here");
6241 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6242 to be used when the caller has already checked
6243 (processing_template_decl
6244 && !instantiation_dependent_expression_p (expr)
6245 && potential_constant_expression (expr))
6246 and cleared processing_template_decl. */
6249 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6251 return tsubst_copy_and_build (expr
,
6254 /*in_decl=*/NULL_TREE
,
6255 /*function_p=*/false,
6256 /*integral_constant_expression_p=*/true);
6259 /* Simplify EXPR if it is a non-dependent expression. Returns the
6260 (possibly simplified) expression. */
6263 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6265 if (expr
== NULL_TREE
)
6268 /* If we're in a template, but EXPR isn't value dependent, simplify
6269 it. We're supposed to treat:
6271 template <typename T> void f(T[1 + 1]);
6272 template <typename T> void f(T[2]);
6274 as two declarations of the same function, for example. */
6275 if (processing_template_decl
6276 && is_nondependent_constant_expression (expr
))
6278 processing_template_decl_sentinel s
;
6279 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6285 instantiate_non_dependent_expr (tree expr
)
6287 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6290 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6291 an uninstantiated expression. */
6294 instantiate_non_dependent_or_null (tree expr
)
6296 if (expr
== NULL_TREE
)
6298 if (processing_template_decl
)
6300 if (!is_nondependent_constant_expression (expr
))
6304 processing_template_decl_sentinel s
;
6305 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6311 /* True iff T is a specialization of a variable template. */
6314 variable_template_specialization_p (tree t
)
6316 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6318 tree tmpl
= DECL_TI_TEMPLATE (t
);
6319 return variable_template_p (tmpl
);
6322 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6323 template declaration, or a TYPE_DECL for an alias declaration. */
6326 alias_type_or_template_p (tree t
)
6330 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6333 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6334 || DECL_ALIAS_TEMPLATE_P (t
));
6337 /* If T is a specialization of an alias template, return it; otherwise return
6338 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6341 alias_template_specialization_p (const_tree t
,
6342 bool transparent_typedefs
)
6347 /* It's an alias template specialization if it's an alias and its
6348 TYPE_NAME is a specialization of a primary template. */
6349 if (typedef_variant_p (t
))
6351 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6352 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6353 return CONST_CAST_TREE (t
);
6354 if (transparent_typedefs
)
6355 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6357 transparent_typedefs
);
6363 /* An alias template is complex from a SFINAE perspective if a template-id
6364 using that alias can be ill-formed when the expansion is not, as with
6365 the void_t template. We determine this by checking whether the
6366 expansion for the alias template uses all its template parameters. */
6368 struct uses_all_template_parms_data
6375 uses_all_template_parms_r (tree t
, void *data_
)
6377 struct uses_all_template_parms_data
&data
6378 = *(struct uses_all_template_parms_data
*)data_
;
6379 tree idx
= get_template_parm_index (t
);
6381 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6382 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6386 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6389 complex_pack_expansion_r (tree t
, void *data_
)
6391 /* An alias template with a pack expansion that expands a pack from the
6392 enclosing class needs to be considered complex, to avoid confusion with
6393 the same pack being used as an argument to the alias's own template
6394 parameter (91966). */
6395 if (!PACK_EXPANSION_P (t
))
6397 struct uses_all_template_parms_data
&data
6398 = *(struct uses_all_template_parms_data
*)data_
;
6399 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6400 pack
= TREE_CHAIN (pack
))
6402 tree parm_pack
= TREE_VALUE (pack
);
6403 if (!TEMPLATE_PARM_P (parm_pack
))
6406 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6407 if (level
< data
.level
)
6414 complex_alias_template_p (const_tree tmpl
)
6416 /* A renaming alias isn't complex. */
6417 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6420 /* Any other constrained alias is complex. */
6421 if (get_constraints (tmpl
))
6424 struct uses_all_template_parms_data data
;
6425 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6426 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6427 data
.level
= TMPL_PARMS_DEPTH (parms
);
6428 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6429 data
.seen
= XALLOCAVEC (bool, len
);
6430 for (int i
= 0; i
< len
; ++i
)
6431 data
.seen
[i
] = false;
6433 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6434 NULL
, true, complex_pack_expansion_r
))
6436 for (int i
= 0; i
< len
; ++i
)
6442 /* If T is a specialization of a complex alias template with dependent
6443 template-arguments, return it; otherwise return NULL_TREE. If T is a
6444 typedef to such a specialization, return the specialization. */
6447 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6449 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6452 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6454 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6455 && (any_dependent_template_arguments_p
6456 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6457 return CONST_CAST_TREE (t
);
6459 if (transparent_typedefs
)
6461 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6462 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6468 /* Return the number of innermost template parameters in TMPL. */
6471 num_innermost_template_parms (const_tree tmpl
)
6473 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6474 return TREE_VEC_LENGTH (parms
);
6477 /* Return either TMPL or another template that it is equivalent to under DR
6478 1286: An alias that just changes the name of a template is equivalent to
6479 the other template. */
6482 get_underlying_template (tree tmpl
)
6484 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6485 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6487 /* Determine if the alias is equivalent to an underlying template. */
6488 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6489 /* The underlying type may have been ill-formed. Don't proceed. */
6492 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6496 tree underlying
= TI_TEMPLATE (tinfo
);
6497 if (!PRIMARY_TEMPLATE_P (underlying
)
6498 || (num_innermost_template_parms (tmpl
)
6499 != num_innermost_template_parms (underlying
)))
6502 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6503 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6506 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6507 it's appropriate to treat a less-constrained alias as equivalent. */
6508 if (!at_least_as_constrained (underlying
, tmpl
))
6511 /* Alias is equivalent. Strip it and repeat. */
6518 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6519 must be a reference-to-function or a pointer-to-function type, as specified
6520 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6521 and check that the resulting function has external linkage. */
6524 convert_nontype_argument_function (tree type
, tree expr
,
6525 tsubst_flags_t complain
)
6529 linkage_kind linkage
;
6531 fn
= instantiate_type (type
, fns
, tf_none
);
6532 if (fn
== error_mark_node
)
6533 return error_mark_node
;
6535 if (value_dependent_expression_p (fn
))
6538 fn_no_ptr
= strip_fnptr_conv (fn
);
6539 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6540 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6541 if (BASELINK_P (fn_no_ptr
))
6542 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6544 /* [temp.arg.nontype]/1
6546 A template-argument for a non-type, non-template template-parameter
6549 -- the address of an object or function with external [C++11: or
6550 internal] linkage. */
6552 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6553 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6555 if (complain
& tf_error
)
6557 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6558 error_at (loc
, "%qE is not a valid template argument for type %qT",
6560 if (TYPE_PTR_P (type
))
6561 inform (loc
, "it must be the address of a function "
6562 "with external linkage");
6564 inform (loc
, "it must be the name of a function with "
6565 "external linkage");
6570 linkage
= decl_linkage (fn_no_ptr
);
6571 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6573 if (complain
& tf_error
)
6575 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6576 if (cxx_dialect
>= cxx11
)
6577 error_at (loc
, "%qE is not a valid template argument for type "
6578 "%qT because %qD has no linkage",
6579 expr
, type
, fn_no_ptr
);
6581 error_at (loc
, "%qE is not a valid template argument for type "
6582 "%qT because %qD does not have external linkage",
6583 expr
, type
, fn_no_ptr
);
6589 if (TYPE_REF_P (type
))
6591 if (REFERENCE_REF_P (fn
))
6592 fn
= TREE_OPERAND (fn
, 0);
6594 fn
= build_address (fn
);
6596 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6597 fn
= build_nop (type
, fn
);
6602 /* Subroutine of convert_nontype_argument.
6603 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6604 Emit an error otherwise. */
6607 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6608 tsubst_flags_t complain
)
6610 tree orig_expr
= expr
;
6612 if (null_ptr_cst_p (expr
))
6614 if (TREE_CODE (expr
) == PTRMEM_CST
6615 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6616 PTRMEM_CST_CLASS (expr
)))
6618 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6620 if (processing_template_decl
6621 && TREE_CODE (expr
) == ADDR_EXPR
6622 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6624 if (complain
& tf_error
)
6626 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6627 error_at (loc
, "%qE is not a valid template argument for type %qT",
6629 if (TREE_CODE (expr
) != PTRMEM_CST
)
6630 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6632 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6637 /* Returns TRUE iff the address of OP is value-dependent.
6639 14.6.2.4 [temp.dep.temp]:
6640 A non-integral non-type template-argument is dependent if its type is
6641 dependent or it has either of the following forms
6644 and contains a nested-name-specifier which specifies a class-name that
6645 names a dependent type.
6647 We generalize this to just say that the address of a member of a
6648 dependent class is value-dependent; the above doesn't cover the
6649 address of a static data member named with an unqualified-id. */
6652 has_value_dependent_address (tree op
)
6654 STRIP_ANY_LOCATION_WRAPPER (op
);
6656 /* We could use get_inner_reference here, but there's no need;
6657 this is only relevant for template non-type arguments, which
6658 can only be expressed as &id-expression. */
6661 tree ctx
= CP_DECL_CONTEXT (op
);
6662 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6669 /* The next set of functions are used for providing helpful explanatory
6670 diagnostics for failed overload resolution. Their messages should be
6671 indented by two spaces for consistency with the messages in
6675 unify_success (bool /*explain_p*/)
6680 /* Other failure functions should call this one, to provide a single function
6681 for setting a breakpoint on. */
6684 unify_invalid (bool /*explain_p*/)
6690 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6693 inform (input_location
,
6694 " couldn%'t deduce template parameter %qD", parm
);
6695 return unify_invalid (explain_p
);
6699 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6702 inform (input_location
,
6703 " types %qT and %qT have incompatible cv-qualifiers",
6705 return unify_invalid (explain_p
);
6709 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6712 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6713 return unify_invalid (explain_p
);
6717 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6720 inform (input_location
,
6721 " template parameter %qD is not a parameter pack, but "
6724 return unify_invalid (explain_p
);
6728 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6731 inform (input_location
,
6732 " template argument %qE does not match "
6733 "pointer-to-member constant %qE",
6735 return unify_invalid (explain_p
);
6739 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6742 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6743 return unify_invalid (explain_p
);
6747 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6750 inform (input_location
,
6751 " inconsistent parameter pack deduction with %qT and %qT",
6753 return unify_invalid (explain_p
);
6757 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6762 inform (input_location
,
6763 " deduced conflicting types for parameter %qT (%qT and %qT)",
6764 parm
, first
, second
);
6766 inform (input_location
,
6767 " deduced conflicting values for non-type parameter "
6768 "%qE (%qE and %qE)", parm
, first
, second
);
6770 return unify_invalid (explain_p
);
6774 unify_vla_arg (bool explain_p
, tree arg
)
6777 inform (input_location
,
6778 " variable-sized array type %qT is not "
6779 "a valid template argument",
6781 return unify_invalid (explain_p
);
6785 unify_method_type_error (bool explain_p
, tree arg
)
6788 inform (input_location
,
6789 " member function type %qT is not a valid template argument",
6791 return unify_invalid (explain_p
);
6795 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6800 inform_n (input_location
, wanted
,
6801 " candidate expects at least %d argument, %d provided",
6802 " candidate expects at least %d arguments, %d provided",
6805 inform_n (input_location
, wanted
,
6806 " candidate expects %d argument, %d provided",
6807 " candidate expects %d arguments, %d provided",
6810 return unify_invalid (explain_p
);
6814 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6816 return unify_arity (explain_p
, have
, wanted
);
6820 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6821 bool least_p
= false)
6823 return unify_arity (explain_p
, have
, wanted
, least_p
);
6827 unify_arg_conversion (bool explain_p
, tree to_type
,
6828 tree from_type
, tree arg
)
6831 inform (cp_expr_loc_or_input_loc (arg
),
6832 " cannot convert %qE (type %qT) to type %qT",
6833 arg
, from_type
, to_type
);
6834 return unify_invalid (explain_p
);
6838 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6839 tree parm
, tree arg
)
6844 case tbr_ambiguous_baseclass
:
6845 inform (input_location
, " %qT is an ambiguous base class of %qT",
6849 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6852 return unify_invalid (explain_p
);
6856 unify_inconsistent_template_template_parameters (bool explain_p
)
6859 inform (input_location
,
6860 " template parameters of a template template argument are "
6861 "inconsistent with other deduced template arguments");
6862 return unify_invalid (explain_p
);
6866 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6869 inform (input_location
,
6870 " cannot deduce a template for %qT from non-template type %qT",
6872 return unify_invalid (explain_p
);
6876 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6879 inform (input_location
,
6880 " template argument %qE does not match %qE", arg
, parm
);
6881 return unify_invalid (explain_p
);
6884 /* True if T is a C++20 template parameter object to store the argument for a
6885 template parameter of class type. */
6888 template_parm_object_p (const_tree t
)
6890 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6891 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6894 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6895 argument for TYPE, points to an unsuitable object. */
6898 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6900 switch (TREE_CODE (expr
))
6903 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6907 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6912 unsigned i
; tree elt
;
6913 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6914 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6921 tree decl
= TREE_OPERAND (expr
, 0);
6925 if (complain
& tf_error
)
6926 error_at (cp_expr_loc_or_input_loc (expr
),
6927 "%qE is not a valid template argument of type %qT "
6928 "because %qE is not a variable", expr
, type
, decl
);
6931 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6933 if (complain
& tf_error
)
6934 error_at (cp_expr_loc_or_input_loc (expr
),
6935 "%qE is not a valid template argument of type %qT "
6936 "in C++98 because %qD does not have external linkage",
6940 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6941 && decl_linkage (decl
) == lk_none
)
6943 if (complain
& tf_error
)
6944 error_at (cp_expr_loc_or_input_loc (expr
),
6945 "%qE is not a valid template argument of type %qT "
6946 "because %qD has no linkage", expr
, type
, decl
);
6949 /* C++17: For a non-type template-parameter of reference or pointer
6950 type, the value of the constant expression shall not refer to (or
6951 for a pointer type, shall not be the address of):
6952 * a subobject (4.5),
6953 * a temporary object (15.2),
6954 * a string literal (5.13.5),
6955 * the result of a typeid expression (8.2.8), or
6956 * a predefined __func__ variable (11.4.1). */
6957 else if (DECL_ARTIFICIAL (decl
))
6959 if (complain
& tf_error
)
6960 error ("the address of %qD is not a valid template argument",
6964 else if (!same_type_ignoring_top_level_qualifiers_p
6965 (strip_array_types (TREE_TYPE (type
)),
6966 strip_array_types (TREE_TYPE (decl
))))
6968 if (complain
& tf_error
)
6969 error ("the address of the %qT subobject of %qD is not a "
6970 "valid template argument", TREE_TYPE (type
), decl
);
6973 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6975 if (complain
& tf_error
)
6976 error ("the address of %qD is not a valid template argument "
6977 "because it does not have static storage duration",
6985 if (!INDIRECT_TYPE_P (type
))
6986 /* We're only concerned about pointers and references here. */;
6987 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6988 /* Null pointer values are OK in C++11. */;
6993 if (complain
& tf_error
)
6994 error ("%qD is not a valid template argument "
6995 "because %qD is a variable, not the address of "
6996 "a variable", expr
, expr
);
7001 if (complain
& tf_error
)
7002 error ("%qE is not a valid template argument for %qT "
7003 "because it is not the address of a variable",
7013 /* The template arguments corresponding to template parameter objects of types
7014 that contain pointers to members. */
7016 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7018 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7019 template argument EXPR. */
7022 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7024 if (TREE_CODE (expr
) == TARGET_EXPR
)
7025 expr
= TARGET_EXPR_INITIAL (expr
);
7027 if (!TREE_CONSTANT (expr
))
7029 if ((complain
& tf_error
)
7030 && require_rvalue_constant_expression (expr
))
7031 cxx_constant_value (expr
);
7032 return error_mark_node
;
7034 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7035 return error_mark_node
;
7037 tree name
= mangle_template_parm_object (expr
);
7038 tree decl
= get_global_binding (name
);
7042 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7043 decl
= create_temporary_var (type
);
7044 TREE_STATIC (decl
) = true;
7045 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7046 TREE_READONLY (decl
) = true;
7047 DECL_NAME (decl
) = name
;
7048 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7049 DECL_CONTEXT (decl
) = global_namespace
;
7050 comdat_linkage (decl
);
7052 if (!zero_init_p (type
))
7054 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7055 lower_var_init before we're done mangling. So store the original
7057 tree copy
= unshare_constructor (expr
);
7058 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7061 pushdecl_top_level_and_finish (decl
, expr
);
7066 /* Return the actual template argument corresponding to template parameter
7070 tparm_object_argument (tree var
)
7072 if (zero_init_p (TREE_TYPE (var
)))
7073 return DECL_INITIAL (var
);
7074 return *(tparm_obj_values
->get (var
));
7077 /* Attempt to convert the non-type template parameter EXPR to the
7078 indicated TYPE. If the conversion is successful, return the
7079 converted value. If the conversion is unsuccessful, return
7080 NULL_TREE if we issued an error message, or error_mark_node if we
7081 did not. We issue error messages for out-and-out bad template
7082 parameters, but not simply because the conversion failed, since we
7083 might be just trying to do argument deduction. Both TYPE and EXPR
7084 must be non-dependent.
7086 The conversion follows the special rules described in
7087 [temp.arg.nontype], and it is much more strict than an implicit
7090 This function is called twice for each template argument (see
7091 lookup_template_class for a more accurate description of this
7092 problem). This means that we need to handle expressions which
7093 are not valid in a C++ source, but can be created from the
7094 first call (for instance, casts to perform conversions). These
7095 hacks can go away after we fix the double coercion problem. */
7098 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7101 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7103 /* Detect immediately string literals as invalid non-type argument.
7104 This special-case is not needed for correctness (we would easily
7105 catch this later), but only to provide better diagnostic for this
7106 common user mistake. As suggested by DR 100, we do not mention
7107 linkage issues in the diagnostic as this is not the point. */
7108 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7110 if (complain
& tf_error
)
7111 error ("%qE is not a valid template argument for type %qT "
7112 "because string literals can never be used in this context",
7117 /* Add the ADDR_EXPR now for the benefit of
7118 value_dependent_expression_p. */
7119 if (TYPE_PTROBV_P (type
)
7120 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7122 expr
= decay_conversion (expr
, complain
);
7123 if (expr
== error_mark_node
)
7124 return error_mark_node
;
7127 /* If we are in a template, EXPR may be non-dependent, but still
7128 have a syntactic, rather than semantic, form. For example, EXPR
7129 might be a SCOPE_REF, rather than the VAR_DECL to which the
7130 SCOPE_REF refers. Preserving the qualifying scope is necessary
7131 so that access checking can be performed when the template is
7132 instantiated -- but here we need the resolved form so that we can
7133 convert the argument. */
7134 bool non_dep
= false;
7135 if (TYPE_REF_OBJ_P (type
)
7136 && has_value_dependent_address (expr
))
7137 /* If we want the address and it's value-dependent, don't fold. */;
7138 else if (processing_template_decl
7139 && is_nondependent_constant_expression (expr
))
7141 if (error_operand_p (expr
))
7142 return error_mark_node
;
7143 expr_type
= TREE_TYPE (expr
);
7145 /* If the argument is non-dependent, perform any conversions in
7146 non-dependent context as well. */
7147 processing_template_decl_sentinel
s (non_dep
);
7149 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7151 const bool val_dep_p
= value_dependent_expression_p (expr
);
7153 expr
= canonicalize_expr_argument (expr
, complain
);
7155 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7156 to a non-type argument of "nullptr". */
7157 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7158 expr
= fold_simple (convert (type
, expr
));
7160 /* In C++11, integral or enumeration non-type template arguments can be
7161 arbitrary constant expressions. Pointer and pointer to
7162 member arguments can be general constant expressions that evaluate
7163 to a null value, but otherwise still need to be of a specific form. */
7164 if (cxx_dialect
>= cxx11
)
7166 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7167 /* A PTRMEM_CST is already constant, and a valid template
7168 argument for a parameter of pointer to member type, we just want
7169 to leave it in that form rather than lower it to a
7171 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7172 || cxx_dialect
>= cxx17
)
7174 /* C++17: A template-argument for a non-type template-parameter shall
7175 be a converted constant expression (8.20) of the type of the
7176 template-parameter. */
7177 expr
= build_converted_constant_expr (type
, expr
, complain
);
7178 if (expr
== error_mark_node
)
7179 /* Make sure we return NULL_TREE only if we have really issued
7180 an error, as described above. */
7181 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7182 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7184 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7187 expr
= maybe_constant_value (expr
, NULL_TREE
,
7188 /*manifestly_const_eval=*/true);
7189 expr
= convert_from_reference (expr
);
7191 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7193 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7194 /*manifestly_const_eval=*/true);
7195 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7196 : null_member_pointer_value_p (folded
))
7201 if (TYPE_REF_P (type
))
7202 expr
= mark_lvalue_use (expr
);
7204 expr
= mark_rvalue_use (expr
);
7206 /* HACK: Due to double coercion, we can get a
7207 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7208 which is the tree that we built on the first call (see
7209 below when coercing to reference to object or to reference to
7210 function). We just strip everything and get to the arg.
7211 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7213 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7215 tree probe_type
, probe
= expr
;
7216 if (REFERENCE_REF_P (probe
))
7217 probe
= TREE_OPERAND (probe
, 0);
7218 probe_type
= TREE_TYPE (probe
);
7219 if (TREE_CODE (probe
) == NOP_EXPR
)
7221 /* ??? Maybe we could use convert_from_reference here, but we
7222 would need to relax its constraints because the NOP_EXPR
7223 could actually change the type to something more cv-qualified,
7224 and this is not folded by convert_from_reference. */
7225 tree addr
= TREE_OPERAND (probe
, 0);
7226 if (TYPE_REF_P (probe_type
)
7227 && TREE_CODE (addr
) == ADDR_EXPR
7228 && TYPE_PTR_P (TREE_TYPE (addr
))
7229 && (same_type_ignoring_top_level_qualifiers_p
7230 (TREE_TYPE (probe_type
),
7231 TREE_TYPE (TREE_TYPE (addr
)))))
7233 expr
= TREE_OPERAND (addr
, 0);
7234 expr_type
= TREE_TYPE (probe_type
);
7239 /* [temp.arg.nontype]/5, bullet 1
7241 For a non-type template-parameter of integral or enumeration type,
7242 integral promotions (_conv.prom_) and integral conversions
7243 (_conv.integral_) are applied. */
7244 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7246 if (cxx_dialect
< cxx11
)
7248 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7249 t
= maybe_constant_value (t
);
7250 if (t
!= error_mark_node
)
7254 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7255 return error_mark_node
;
7257 /* Notice that there are constant expressions like '4 % 0' which
7258 do not fold into integer constants. */
7259 if (TREE_CODE (expr
) != INTEGER_CST
&& !val_dep_p
)
7261 if (complain
& tf_error
)
7263 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7264 if (!require_potential_constant_expression (expr
))
7265 expr
= error_mark_node
;
7267 expr
= cxx_constant_value (expr
);
7268 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7269 inform (loc
, "in template argument for type %qT", type
);
7270 if (expr
== error_mark_node
)
7272 /* else cxx_constant_value complained but gave us
7273 a real constant, so go ahead. */
7274 if (TREE_CODE (expr
) != INTEGER_CST
)
7276 /* Some assemble time constant expressions like
7277 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7278 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7279 as we can emit them into .rodata initializers of
7280 variables, yet they can't fold into an INTEGER_CST at
7281 compile time. Refuse them here. */
7282 gcc_checking_assert (reduced_constant_expression_p (expr
));
7283 error_at (loc
, "template argument %qE for type %qT not "
7284 "a constant integer", expr
, type
);
7292 /* Avoid typedef problems. */
7293 if (TREE_TYPE (expr
) != type
)
7294 expr
= fold_convert (type
, expr
);
7296 /* [temp.arg.nontype]/5, bullet 2
7298 For a non-type template-parameter of type pointer to object,
7299 qualification conversions (_conv.qual_) and the array-to-pointer
7300 conversion (_conv.array_) are applied. */
7301 else if (TYPE_PTROBV_P (type
))
7303 tree decayed
= expr
;
7305 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7306 decay_conversion or an explicit cast. If it's a problematic cast,
7307 we'll complain about it below. */
7308 if (TREE_CODE (expr
) == NOP_EXPR
)
7312 if (TREE_CODE (probe
) == ADDR_EXPR
7313 && TYPE_PTR_P (TREE_TYPE (probe
)))
7316 expr_type
= TREE_TYPE (expr
);
7320 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7322 A template-argument for a non-type, non-template template-parameter
7323 shall be one of: [...]
7325 -- the name of a non-type template-parameter;
7326 -- the address of an object or function with external linkage, [...]
7327 expressed as "& id-expression" where the & is optional if the name
7328 refers to a function or array, or if the corresponding
7329 template-parameter is a reference.
7331 Here, we do not care about functions, as they are invalid anyway
7332 for a parameter of type pointer-to-object. */
7335 /* Non-type template parameters are OK. */
7337 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7338 /* Null pointer values are OK in C++11. */;
7339 else if (TREE_CODE (expr
) != ADDR_EXPR
7340 && !INDIRECT_TYPE_P (expr_type
))
7341 /* Other values, like integer constants, might be valid
7342 non-type arguments of some other type. */
7343 return error_mark_node
;
7344 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7349 expr
= perform_qualification_conversions (type
, expr
);
7350 if (expr
== error_mark_node
)
7351 return error_mark_node
;
7353 /* [temp.arg.nontype]/5, bullet 3
7355 For a non-type template-parameter of type reference to object, no
7356 conversions apply. The type referred to by the reference may be more
7357 cv-qualified than the (otherwise identical) type of the
7358 template-argument. The template-parameter is bound directly to the
7359 template-argument, which must be an lvalue. */
7360 else if (TYPE_REF_OBJ_P (type
))
7362 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7364 return error_mark_node
;
7366 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7368 if (complain
& tf_error
)
7369 error ("%qE is not a valid template argument for type %qT "
7370 "because of conflicts in cv-qualification", expr
, type
);
7374 if (!lvalue_p (expr
))
7376 if (complain
& tf_error
)
7377 error ("%qE is not a valid template argument for type %qT "
7378 "because it is not an lvalue", expr
, type
);
7382 /* [temp.arg.nontype]/1
7384 A template-argument for a non-type, non-template template-parameter
7385 shall be one of: [...]
7387 -- the address of an object or function with external linkage. */
7388 if (INDIRECT_REF_P (expr
)
7389 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7391 expr
= TREE_OPERAND (expr
, 0);
7394 if (complain
& tf_error
)
7395 error ("%q#D is not a valid template argument for type %qT "
7396 "because a reference variable does not have a constant "
7397 "address", expr
, type
);
7402 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7403 /* OK, dependent reference. We don't want to ask whether a DECL is
7404 itself value-dependent, since what we want here is its address. */;
7407 expr
= build_address (expr
);
7409 if (invalid_tparm_referent_p (type
, expr
, complain
))
7413 if (!same_type_p (type
, TREE_TYPE (expr
)))
7414 expr
= build_nop (type
, expr
);
7416 /* [temp.arg.nontype]/5, bullet 4
7418 For a non-type template-parameter of type pointer to function, only
7419 the function-to-pointer conversion (_conv.func_) is applied. If the
7420 template-argument represents a set of overloaded functions (or a
7421 pointer to such), the matching function is selected from the set
7423 else if (TYPE_PTRFN_P (type
))
7425 /* If the argument is a template-id, we might not have enough
7426 context information to decay the pointer. */
7427 if (!type_unknown_p (expr_type
))
7429 expr
= decay_conversion (expr
, complain
);
7430 if (expr
== error_mark_node
)
7431 return error_mark_node
;
7434 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7435 /* Null pointer values are OK in C++11. */
7436 return perform_qualification_conversions (type
, expr
);
7438 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7439 if (!expr
|| expr
== error_mark_node
)
7442 /* [temp.arg.nontype]/5, bullet 5
7444 For a non-type template-parameter of type reference to function, no
7445 conversions apply. If the template-argument represents a set of
7446 overloaded functions, the matching function is selected from the set
7448 else if (TYPE_REFFN_P (type
))
7450 if (TREE_CODE (expr
) == ADDR_EXPR
)
7452 if (complain
& tf_error
)
7454 error ("%qE is not a valid template argument for type %qT "
7455 "because it is a pointer", expr
, type
);
7456 inform (input_location
, "try using %qE instead",
7457 TREE_OPERAND (expr
, 0));
7462 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7463 if (!expr
|| expr
== error_mark_node
)
7466 /* [temp.arg.nontype]/5, bullet 6
7468 For a non-type template-parameter of type pointer to member function,
7469 no conversions apply. If the template-argument represents a set of
7470 overloaded member functions, the matching member function is selected
7471 from the set (_over.over_). */
7472 else if (TYPE_PTRMEMFUNC_P (type
))
7474 expr
= instantiate_type (type
, expr
, tf_none
);
7475 if (expr
== error_mark_node
)
7476 return error_mark_node
;
7478 /* [temp.arg.nontype] bullet 1 says the pointer to member
7479 expression must be a pointer-to-member constant. */
7481 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7484 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7485 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7486 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7487 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7489 /* [temp.arg.nontype]/5, bullet 7
7491 For a non-type template-parameter of type pointer to data member,
7492 qualification conversions (_conv.qual_) are applied. */
7493 else if (TYPE_PTRDATAMEM_P (type
))
7495 /* [temp.arg.nontype] bullet 1 says the pointer to member
7496 expression must be a pointer-to-member constant. */
7498 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7501 expr
= perform_qualification_conversions (type
, expr
);
7502 if (expr
== error_mark_node
)
7505 else if (NULLPTR_TYPE_P (type
))
7507 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7509 if (complain
& tf_error
)
7510 error ("%qE is not a valid template argument for type %qT "
7511 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7516 else if (CLASS_TYPE_P (type
))
7518 /* Replace the argument with a reference to the corresponding template
7519 parameter object. */
7521 expr
= get_template_parm_object (expr
, complain
);
7522 if (expr
== error_mark_node
)
7525 /* A template non-type parameter must be one of the above. */
7529 /* Sanity check: did we actually convert the argument to the
7531 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7532 (type
, TREE_TYPE (expr
)));
7533 return convert_from_reference (expr
);
7536 /* Subroutine of coerce_template_template_parms, which returns 1 if
7537 PARM_PARM and ARG_PARM match using the rule for the template
7538 parameters of template template parameters. Both PARM and ARG are
7539 template parameters; the rest of the arguments are the same as for
7540 coerce_template_template_parms.
7543 coerce_template_template_parm (tree parm
,
7545 tsubst_flags_t complain
,
7549 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7550 || parm
== NULL_TREE
|| error_operand_p (parm
))
7553 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7556 switch (TREE_CODE (parm
))
7559 /* We encounter instantiations of templates like
7560 template <template <template <class> class> class TT>
7563 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7564 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7566 if (!coerce_template_template_parms
7567 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7573 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7574 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7575 /* Argument is a parameter pack but parameter is not. */
7580 /* The tsubst call is used to handle cases such as
7582 template <int> class C {};
7583 template <class T, template <T> class TT> class D {};
7586 i.e. the parameter list of TT depends on earlier parameters. */
7587 if (!uses_template_parms (TREE_TYPE (arg
)))
7589 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7590 if (!uses_template_parms (t
)
7591 && !same_type_p (t
, TREE_TYPE (arg
)))
7595 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7596 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7597 /* Argument is a parameter pack but parameter is not. */
7609 /* Coerce template argument list ARGLIST for use with template
7610 template-parameter TEMPL. */
7613 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7614 tsubst_flags_t complain
)
7616 /* Consider an example where a template template parameter declared as
7618 template <class T, class U = std::allocator<T> > class TT
7620 The template parameter level of T and U are one level larger than
7621 of TT. To proper process the default argument of U, say when an
7622 instantiation `TT<int>' is seen, we need to build the full
7623 arguments containing {int} as the innermost level. Outer levels,
7624 available when not appearing as default template argument, can be
7625 obtained from the arguments of the enclosing template.
7627 Suppose that TT is later substituted with std::vector. The above
7628 instantiation is `TT<int, std::allocator<T> >' with TT at
7629 level 1, and T at level 2, while the template arguments at level 1
7630 becomes {std::vector} and the inner level 2 is {int}. */
7632 tree outer
= DECL_CONTEXT (templ
);
7634 outer
= generic_targs_for (outer
);
7635 else if (current_template_parms
)
7637 /* This is an argument of the current template, so we haven't set
7638 DECL_CONTEXT yet. */
7639 tree relevant_template_parms
;
7641 /* Parameter levels that are greater than the level of the given
7642 template template parm are irrelevant. */
7643 relevant_template_parms
= current_template_parms
;
7644 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7645 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7646 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7648 outer
= template_parms_to_args (relevant_template_parms
);
7652 arglist
= add_to_template_args (outer
, arglist
);
7654 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7655 return coerce_template_parms (parmlist
, arglist
, templ
,
7657 /*require_all_args=*/true,
7658 /*use_default_args=*/true);
7661 /* A cache of template template parameters with match-all default
7663 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7665 /* T is a bound template template-parameter. Copy its arguments into default
7666 arguments of the template template-parameter's template parameters. */
7669 add_defaults_to_ttp (tree otmpl
)
7671 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7674 tree ntmpl
= copy_node (otmpl
);
7676 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7677 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7678 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7679 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7680 TYPE_NAME (ntype
) = ntmpl
;
7681 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7683 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7684 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7685 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7686 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7688 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7689 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7690 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7691 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7692 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7694 tree o
= TREE_VEC_ELT (vec
, i
);
7695 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7697 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7698 TREE_PURPOSE (n
) = any_targ_node
;
7702 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7706 /* ARG is a bound potential template template-argument, and PARGS is a list
7707 of arguments for the corresponding template template-parameter. Adjust
7708 PARGS as appropriate for application to ARG's template, and if ARG is a
7709 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7710 arguments to the template template parameter. */
7713 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7715 ++processing_template_decl
;
7716 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7717 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7719 /* When comparing two template template-parameters in partial ordering,
7720 rewrite the one currently being used as an argument to have default
7721 arguments for all parameters. */
7722 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7723 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7724 if (pargs
!= error_mark_node
)
7725 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7726 TYPE_TI_ARGS (arg
));
7731 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7732 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7733 /*require_all*/true,
7734 /*use_default*/true);
7736 --processing_template_decl
;
7740 /* Subroutine of unify for the case when PARM is a
7741 BOUND_TEMPLATE_TEMPLATE_PARM. */
7744 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7747 tree parmvec
= TYPE_TI_ARGS (parm
);
7748 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7750 /* The template template parm might be variadic and the argument
7751 not, so flatten both argument lists. */
7752 parmvec
= expand_template_argument_pack (parmvec
);
7753 argvec
= expand_template_argument_pack (argvec
);
7757 /* In keeping with P0522R0, adjust P's template arguments
7758 to apply to A's template; then flatten it again. */
7759 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7760 nparmvec
= expand_template_argument_pack (nparmvec
);
7762 if (unify (tparms
, targs
, nparmvec
, argvec
,
7763 UNIFY_ALLOW_NONE
, explain_p
))
7766 /* If the P0522 adjustment eliminated a pack expansion, deduce
7769 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7770 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7771 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7776 /* Deduce arguments T, i from TT<T> or TT<i>.
7777 We check each element of PARMVEC and ARGVEC individually
7778 rather than the whole TREE_VEC since they can have
7779 different number of elements, which is allowed under N2555. */
7781 int len
= TREE_VEC_LENGTH (parmvec
);
7783 /* Check if the parameters end in a pack, making them
7785 int parm_variadic_p
= 0;
7787 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7788 parm_variadic_p
= 1;
7790 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7791 /* If the template argument list of P contains a pack
7792 expansion that is not the last template argument, the
7793 entire template argument list is a non-deduced
7795 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7796 return unify_success (explain_p
);
7798 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7799 return unify_too_few_arguments (explain_p
,
7800 TREE_VEC_LENGTH (argvec
), len
);
7802 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7803 if (unify (tparms
, targs
,
7804 TREE_VEC_ELT (parmvec
, i
),
7805 TREE_VEC_ELT (argvec
, i
),
7806 UNIFY_ALLOW_NONE
, explain_p
))
7810 && unify_pack_expansion (tparms
, targs
,
7813 /*subr=*/true, explain_p
))
7820 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7821 template template parameters. Both PARM_PARMS and ARG_PARMS are
7822 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7825 Consider the example:
7826 template <class T> class A;
7827 template<template <class U> class TT> class B;
7829 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7830 the parameters to A, and OUTER_ARGS contains A. */
7833 coerce_template_template_parms (tree parm_parms
,
7835 tsubst_flags_t complain
,
7839 int nparms
, nargs
, i
;
7843 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7844 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7846 nparms
= TREE_VEC_LENGTH (parm_parms
);
7847 nargs
= TREE_VEC_LENGTH (arg_parms
);
7851 /* P0522R0: A template template-parameter P is at least as specialized as
7852 a template template-argument A if, given the following rewrite to two
7853 function templates, the function template corresponding to P is at
7854 least as specialized as the function template corresponding to A
7855 according to the partial ordering rules for function templates
7856 ([temp.func.order]). Given an invented class template X with the
7857 template parameter list of A (including default arguments):
7859 * Each of the two function templates has the same template parameters,
7860 respectively, as P or A.
7862 * Each function template has a single function parameter whose type is
7863 a specialization of X with template arguments corresponding to the
7864 template parameters from the respective function template where, for
7865 each template parameter PP in the template parameter list of the
7866 function template, a corresponding template argument AA is formed. If
7867 PP declares a parameter pack, then AA is the pack expansion
7868 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7870 If the rewrite produces an invalid type, then P is not at least as
7871 specialized as A. */
7873 /* So coerce P's args to apply to A's parms, and then deduce between A's
7874 args and the converted args. If that succeeds, A is at least as
7875 specialized as P, so they match.*/
7876 tree pargs
= template_parms_level_to_args (parm_parms
);
7877 pargs
= add_outermost_template_args (outer_args
, pargs
);
7878 ++processing_template_decl
;
7879 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7880 /*require_all*/true, /*use_default*/true);
7881 --processing_template_decl
;
7882 if (pargs
!= error_mark_node
)
7884 tree targs
= make_tree_vec (nargs
);
7885 tree aargs
= template_parms_level_to_args (arg_parms
);
7886 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7892 /* Determine whether we have a parameter pack at the end of the
7893 template template parameter's template parameter list. */
7894 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7896 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7898 if (error_operand_p (parm
))
7901 switch (TREE_CODE (parm
))
7905 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7910 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7920 && !(variadic_p
&& nargs
>= nparms
- 1))
7923 /* Check all of the template parameters except the parameter pack at
7924 the end (if any). */
7925 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7927 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7928 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7931 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7932 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7934 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7942 /* Check each of the template parameters in the template
7943 argument against the template parameter pack at the end of
7944 the template template parameter. */
7945 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7948 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7950 for (; i
< nargs
; ++i
)
7952 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7955 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7957 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7966 /* Verifies that the deduced template arguments (in TARGS) for the
7967 template template parameters (in TPARMS) represent valid bindings,
7968 by comparing the template parameter list of each template argument
7969 to the template parameter list of its corresponding template
7970 template parameter, in accordance with DR150. This
7971 routine can only be called after all template arguments have been
7972 deduced. It will return TRUE if all of the template template
7973 parameter bindings are okay, FALSE otherwise. */
7975 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7977 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7980 /* We're dealing with template parms in this process. */
7981 ++processing_template_decl
;
7983 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7985 for (i
= 0; i
< ntparms
; ++i
)
7987 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7988 tree targ
= TREE_VEC_ELT (targs
, i
);
7990 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7992 tree packed_args
= NULL_TREE
;
7995 if (ARGUMENT_PACK_P (targ
))
7997 /* Look inside the argument pack. */
7998 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7999 len
= TREE_VEC_LENGTH (packed_args
);
8002 for (idx
= 0; idx
< len
; ++idx
)
8004 tree targ_parms
= NULL_TREE
;
8007 /* Extract the next argument from the argument
8009 targ
= TREE_VEC_ELT (packed_args
, idx
);
8011 if (PACK_EXPANSION_P (targ
))
8012 /* Look at the pattern of the pack expansion. */
8013 targ
= PACK_EXPANSION_PATTERN (targ
);
8015 /* Extract the template parameters from the template
8017 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8018 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8019 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8020 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8022 /* Verify that we can coerce the template template
8023 parameters from the template argument to the template
8024 parameter. This requires an exact match. */
8026 && !coerce_template_template_parms
8027 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8042 --processing_template_decl
;
8046 /* Since type attributes aren't mangled, we need to strip them from
8047 template type arguments. */
8050 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8052 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8054 bool removed_attributes
= false;
8055 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8056 if (removed_attributes
8057 && (complain
& tf_warning
))
8058 warning (OPT_Wignored_attributes
,
8059 "ignoring attributes on template argument %qT", arg
);
8063 /* And from inside dependent non-type arguments like sizeof(Type). */
8066 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8068 if (!arg
|| arg
== error_mark_node
)
8070 bool removed_attributes
= false;
8071 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8072 if (removed_attributes
8073 && (complain
& tf_warning
))
8074 warning (OPT_Wignored_attributes
,
8075 "ignoring attributes in template argument %qE", arg
);
8079 // A template declaration can be substituted for a constrained
8080 // template template parameter only when the argument is more
8081 // constrained than the parameter.
8083 is_compatible_template_arg (tree parm
, tree arg
)
8085 tree parm_cons
= get_constraints (parm
);
8087 /* For now, allow constrained template template arguments
8088 and unconstrained template template parameters. */
8089 if (parm_cons
== NULL_TREE
)
8092 /* If the template parameter is constrained, we need to rewrite its
8093 constraints in terms of the ARG's template parameters. This ensures
8094 that all of the template parameter types will have the same depth.
8096 Note that this is only valid when coerce_template_template_parm is
8097 true for the innermost template parameters of PARM and ARG. In other
8098 words, because coercion is successful, this conversion will be valid. */
8099 tree new_args
= NULL_TREE
;
8102 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8103 new_args
= template_parms_level_to_args (aparms
);
8104 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8105 tf_none
, NULL_TREE
);
8106 if (parm_cons
== error_mark_node
)
8110 return weakly_subsumes (parm_cons
, new_args
, arg
);
8113 // Convert a placeholder argument into a binding to the original
8114 // parameter. The original parameter is saved as the TREE_TYPE of
8117 convert_wildcard_argument (tree parm
, tree arg
)
8119 TREE_TYPE (arg
) = parm
;
8123 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8124 because one of them is dependent. But we need to represent the
8125 conversion for the benefit of cp_tree_equal. */
8128 maybe_convert_nontype_argument (tree type
, tree arg
)
8130 /* Auto parms get no conversion. */
8131 if (type_uses_auto (type
))
8133 /* We don't need or want to add this conversion now if we're going to use the
8134 argument for deduction. */
8135 if (value_dependent_expression_p (arg
))
8138 type
= cv_unqualified (type
);
8139 tree argtype
= TREE_TYPE (arg
);
8140 if (same_type_p (type
, argtype
))
8143 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8144 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8148 /* Convert the indicated template ARG as necessary to match the
8149 indicated template PARM. Returns the converted ARG, or
8150 error_mark_node if the conversion was unsuccessful. Error and
8151 warning messages are issued under control of COMPLAIN. This
8152 conversion is for the Ith parameter in the parameter list. ARGS is
8153 the full set of template arguments deduced so far. */
8156 convert_template_argument (tree parm
,
8159 tsubst_flags_t complain
,
8165 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8167 if (parm
== error_mark_node
|| error_operand_p (arg
))
8168 return error_mark_node
;
8170 /* Trivially convert placeholders. */
8171 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8172 return convert_wildcard_argument (parm
, arg
);
8174 if (arg
== any_targ_node
)
8177 if (TREE_CODE (arg
) == TREE_LIST
8178 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8180 /* The template argument was the name of some
8181 member function. That's usually
8182 invalid, but static members are OK. In any
8183 case, grab the underlying fields/functions
8184 and issue an error later if required. */
8185 TREE_TYPE (arg
) = unknown_type_node
;
8190 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8191 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8192 || requires_tmpl_type
);
8194 /* When determining whether an argument pack expansion is a template,
8195 look at the pattern. */
8196 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8197 arg
= PACK_EXPANSION_PATTERN (arg
);
8199 /* Deal with an injected-class-name used as a template template arg. */
8200 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8202 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8203 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8205 if (cxx_dialect
>= cxx11
)
8206 /* OK under DR 1004. */;
8207 else if (complain
& tf_warning_or_error
)
8208 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8209 " used as template template argument", TYPE_NAME (arg
));
8210 else if (flag_pedantic_errors
)
8218 ((TREE_CODE (arg
) == TEMPLATE_DECL
8219 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8220 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8221 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8222 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8225 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8226 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8227 arg
= TYPE_STUB_DECL (arg
);
8229 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8231 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8232 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8234 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8236 if (complain
& tf_error
)
8237 error ("invalid use of destructor %qE as a type", orig_arg
);
8238 return error_mark_node
;
8241 permerror (input_location
,
8242 "to refer to a type member of a template parameter, "
8243 "use %<typename %E%>", orig_arg
);
8245 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8246 TREE_OPERAND (arg
, 1),
8252 if (is_type
!= requires_type
)
8256 if (complain
& tf_error
)
8258 error ("type/value mismatch at argument %d in template "
8259 "parameter list for %qD",
8263 /* The template argument is a type, but we're expecting
8265 inform (input_location
,
8266 " expected a constant of type %qT, got %qT",
8268 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8269 /* [temp.arg]/2: "In a template-argument, an ambiguity
8270 between a type-id and an expression is resolved to a
8271 type-id, regardless of the form of the corresponding
8272 template-parameter." So give the user a clue. */
8273 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8274 inform (input_location
, " ambiguous template argument "
8275 "for non-type template parameter is treated as "
8278 else if (requires_tmpl_type
)
8279 inform (input_location
,
8280 " expected a class template, got %qE", orig_arg
);
8282 inform (input_location
,
8283 " expected a type, got %qE", orig_arg
);
8286 return error_mark_node
;
8288 if (is_tmpl_type
^ requires_tmpl_type
)
8290 if (in_decl
&& (complain
& tf_error
))
8292 error ("type/value mismatch at argument %d in template "
8293 "parameter list for %qD",
8296 inform (input_location
,
8297 " expected a type, got %qT", DECL_NAME (arg
));
8299 inform (input_location
,
8300 " expected a class template, got %qT", orig_arg
);
8302 return error_mark_node
;
8305 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8306 /* We already did the appropriate conversion when packing args. */
8310 if (requires_tmpl_type
)
8312 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8313 /* The number of argument required is not known yet.
8314 Just accept it for now. */
8318 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8321 /* Strip alias templates that are equivalent to another
8323 arg
= get_underlying_template (arg
);
8324 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8326 if (coerce_template_template_parms (parmparm
, argparm
,
8332 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8334 if (val
!= error_mark_node
)
8336 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8337 val
= TREE_TYPE (val
);
8338 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8339 val
= make_pack_expansion (val
, complain
);
8344 if (in_decl
&& (complain
& tf_error
))
8346 error ("type/value mismatch at argument %d in "
8347 "template parameter list for %qD",
8349 inform (input_location
,
8350 " expected a template of type %qD, got %qT",
8354 val
= error_mark_node
;
8357 // Check that the constraints are compatible before allowing the
8359 if (val
!= error_mark_node
)
8360 if (!is_compatible_template_arg (parm
, arg
))
8362 if (in_decl
&& (complain
& tf_error
))
8364 error ("constraint mismatch at argument %d in "
8365 "template parameter list for %qD",
8367 inform (input_location
, " expected %qD but got %qD",
8370 val
= error_mark_node
;
8376 /* We only form one instance of each template specialization.
8377 Therefore, if we use a non-canonical variant (i.e., a
8378 typedef), any future messages referring to the type will use
8379 the typedef, which is confusing if those future uses do not
8380 themselves also use the typedef. */
8382 val
= canonicalize_type_argument (val
, complain
);
8386 tree t
= TREE_TYPE (parm
);
8388 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8389 > TMPL_ARGS_DEPTH (args
))
8390 /* We don't have enough levels of args to do any substitution. This
8391 can happen in the context of -fnew-ttp-matching. */;
8392 else if (tree a
= type_uses_auto (t
))
8394 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8395 if (t
== error_mark_node
)
8396 return error_mark_node
;
8399 t
= tsubst (t
, args
, complain
, in_decl
);
8401 if (invalid_nontype_parm_type_p (t
, complain
))
8402 return error_mark_node
;
8404 if (t
!= TREE_TYPE (parm
))
8405 t
= canonicalize_type_argument (t
, complain
);
8407 if (!type_dependent_expression_p (orig_arg
)
8408 && !uses_template_parms (t
))
8409 /* We used to call digest_init here. However, digest_init
8410 will report errors, which we don't want when complain
8411 is zero. More importantly, digest_init will try too
8412 hard to convert things: for example, `0' should not be
8413 converted to pointer type at this point according to
8414 the standard. Accepting this is not merely an
8415 extension, since deciding whether or not these
8416 conversions can occur is part of determining which
8417 function template to call, or whether a given explicit
8418 argument specification is valid. */
8419 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8422 val
= canonicalize_expr_argument (orig_arg
, complain
);
8423 val
= maybe_convert_nontype_argument (t
, val
);
8427 if (val
== NULL_TREE
)
8428 val
= error_mark_node
;
8429 else if (val
== error_mark_node
&& (complain
& tf_error
))
8430 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8431 "could not convert template argument %qE from %qT to %qT",
8432 orig_arg
, TREE_TYPE (orig_arg
), t
);
8434 if (INDIRECT_REF_P (val
))
8436 /* Reject template arguments that are references to built-in
8437 functions with no library fallbacks. */
8438 const_tree inner
= TREE_OPERAND (val
, 0);
8439 const_tree innertype
= TREE_TYPE (inner
);
8441 && TYPE_REF_P (innertype
)
8442 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8443 && TREE_OPERAND_LENGTH (inner
) > 0
8444 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8445 return error_mark_node
;
8448 if (TREE_CODE (val
) == SCOPE_REF
)
8450 /* Strip typedefs from the SCOPE_REF. */
8451 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8452 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8454 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8455 QUALIFIED_NAME_IS_TEMPLATE (val
));
8462 /* Coerces the remaining template arguments in INNER_ARGS (from
8463 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8464 Returns the coerced argument pack. PARM_IDX is the position of this
8465 parameter in the template parameter list. ARGS is the original
8466 template argument list. */
8468 coerce_template_parameter_pack (tree parms
,
8476 tsubst_flags_t complain
)
8478 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8479 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8482 tree packed_parms
= NULL_TREE
;
8484 if (arg_idx
> nargs
)
8487 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8489 /* When the template parameter is a non-type template parameter pack
8490 or template template parameter pack whose type or template
8491 parameters use parameter packs, we know exactly how many arguments
8492 we are looking for. Build a vector of the instantiated decls for
8493 these template parameters in PACKED_PARMS. */
8494 /* We can't use make_pack_expansion here because it would interpret a
8495 _DECL as a use rather than a declaration. */
8496 tree decl
= TREE_VALUE (parm
);
8497 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8498 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8499 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8500 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8502 TREE_VEC_LENGTH (args
)--;
8503 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8504 TREE_VEC_LENGTH (args
)++;
8506 if (packed_parms
== error_mark_node
)
8507 return error_mark_node
;
8509 /* If we're doing a partial instantiation of a member template,
8510 verify that all of the types used for the non-type
8511 template parameter pack are, in fact, valid for non-type
8512 template parameters. */
8514 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8516 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8517 for (j
= 0; j
< len
; ++j
)
8519 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8520 if (TREE_CODE (t
) == PARM_DECL
8521 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8522 return error_mark_node
;
8524 /* We don't know how many args we have yet, just
8525 use the unconverted ones for now. */
8529 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8531 /* Check if we have a placeholder pack, which indicates we're
8532 in the context of a introduction list. In that case we want
8533 to match this pack to the single placeholder. */
8534 else if (arg_idx
< nargs
8535 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8536 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8538 nargs
= arg_idx
+ 1;
8539 packed_args
= make_tree_vec (1);
8542 packed_args
= make_tree_vec (nargs
- arg_idx
);
8544 /* Convert the remaining arguments, which will be a part of the
8545 parameter pack "parm". */
8546 int first_pack_arg
= arg_idx
;
8547 for (; arg_idx
< nargs
; ++arg_idx
)
8549 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8550 tree actual_parm
= TREE_VALUE (parm
);
8551 int pack_idx
= arg_idx
- first_pack_arg
;
8555 /* Once we've packed as many args as we have types, stop. */
8556 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8558 else if (PACK_EXPANSION_P (arg
))
8559 /* We don't know how many args we have yet, just
8560 use the unconverted ones for now. */
8563 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8566 if (arg
== error_mark_node
)
8568 if (complain
& tf_error
)
8569 error ("template argument %d is invalid", arg_idx
+ 1);
8572 arg
= convert_template_argument (actual_parm
,
8573 arg
, new_args
, complain
, parm_idx
,
8575 if (arg
== error_mark_node
)
8577 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8580 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8581 && TREE_VEC_LENGTH (packed_args
) > 0)
8583 if (complain
& tf_error
)
8584 error ("wrong number of template arguments (%d, should be %d)",
8585 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8586 return error_mark_node
;
8589 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8590 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8591 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8594 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8595 TREE_CONSTANT (argument_pack
) = 1;
8598 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8600 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8601 TREE_VEC_LENGTH (packed_args
));
8602 return argument_pack
;
8605 /* Returns the number of pack expansions in the template argument vector
8609 pack_expansion_args_count (tree args
)
8614 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8616 tree elt
= TREE_VEC_ELT (args
, i
);
8617 if (elt
&& PACK_EXPANSION_P (elt
))
8623 /* Convert all template arguments to their appropriate types, and
8624 return a vector containing the innermost resulting template
8625 arguments. If any error occurs, return error_mark_node. Error and
8626 warning messages are issued under control of COMPLAIN.
8628 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8629 for arguments not specified in ARGS. Otherwise, if
8630 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8631 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8632 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8636 coerce_template_parms (tree parms
,
8639 tsubst_flags_t complain
,
8640 bool require_all_args
,
8641 bool use_default_args
)
8643 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8644 tree orig_inner_args
;
8647 tree new_inner_args
;
8649 /* When used as a boolean value, indicates whether this is a
8650 variadic template parameter list. Since it's an int, we can also
8651 subtract it from nparms to get the number of non-variadic
8654 int variadic_args_p
= 0;
8655 int post_variadic_parms
= 0;
8657 /* Adjustment to nparms for fixed parameter packs. */
8658 int fixed_pack_adjust
= 0;
8659 int fixed_packs
= 0;
8662 /* Likewise for parameters with default arguments. */
8665 if (args
== error_mark_node
)
8666 return error_mark_node
;
8668 nparms
= TREE_VEC_LENGTH (parms
);
8670 /* Determine if there are any parameter packs or default arguments. */
8671 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8673 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8675 ++post_variadic_parms
;
8676 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8678 if (TREE_PURPOSE (parm
))
8682 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8683 /* If there are no parameters that follow a parameter pack, we need to
8684 expand any argument packs so that we can deduce a parameter pack from
8685 some non-packed args followed by an argument pack, as in variadic85.C.
8686 If there are such parameters, we need to leave argument packs intact
8687 so the arguments are assigned properly. This can happen when dealing
8688 with a nested class inside a partial specialization of a class
8689 template, as in variadic92.C, or when deducing a template parameter pack
8690 from a sub-declarator, as in variadic114.C. */
8691 if (!post_variadic_parms
)
8692 inner_args
= expand_template_argument_pack (inner_args
);
8694 /* Count any pack expansion args. */
8695 variadic_args_p
= pack_expansion_args_count (inner_args
);
8697 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8698 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8699 || (nargs
< nparms
- variadic_p
8702 && (!use_default_args
8703 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8704 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8707 if (complain
& tf_error
)
8709 if (variadic_p
|| default_p
)
8711 nparms
-= variadic_p
+ default_p
;
8712 error ("wrong number of template arguments "
8713 "(%d, should be at least %d)", nargs
, nparms
);
8716 error ("wrong number of template arguments "
8717 "(%d, should be %d)", nargs
, nparms
);
8720 inform (DECL_SOURCE_LOCATION (in_decl
),
8721 "provided for %qD", in_decl
);
8724 return error_mark_node
;
8726 /* We can't pass a pack expansion to a non-pack parameter of an alias
8727 template (DR 1430). */
8729 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8730 || concept_definition_p (in_decl
))
8732 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8734 if (complain
& tf_error
)
8736 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8738 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8739 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8741 if (PACK_EXPANSION_P (arg
)
8742 && !template_parameter_pack_p (parm
))
8744 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8745 error_at (location_of (arg
),
8746 "pack expansion argument for non-pack parameter "
8747 "%qD of alias template %qD", parm
, in_decl
);
8749 error_at (location_of (arg
),
8750 "pack expansion argument for non-pack parameter "
8751 "%qD of concept %qD", parm
, in_decl
);
8752 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8759 return error_mark_node
;
8762 /* We need to evaluate the template arguments, even though this
8763 template-id may be nested within a "sizeof". */
8766 new_inner_args
= make_tree_vec (nparms
);
8767 new_args
= add_outermost_template_args (args
, new_inner_args
);
8768 int pack_adjust
= 0;
8769 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8774 /* Get the Ith template parameter. */
8775 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8777 if (parm
== error_mark_node
)
8779 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8783 /* Calculate the next argument. */
8784 if (arg_idx
< nargs
)
8785 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8789 if (template_parameter_pack_p (TREE_VALUE (parm
))
8790 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8791 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8793 /* Some arguments will be placed in the
8794 template parameter pack PARM. */
8795 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8796 inner_args
, arg_idx
,
8800 if (arg
== NULL_TREE
)
8802 /* We don't know how many args we have yet, just use the
8803 unconverted (and still packed) ones for now. */
8804 new_inner_args
= orig_inner_args
;
8809 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8811 /* Store this argument. */
8812 if (arg
== error_mark_node
)
8815 /* We are done with all of the arguments. */
8821 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8822 arg_idx
+= pack_adjust
;
8823 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8826 fixed_pack_adjust
+= pack_adjust
;
8834 if (PACK_EXPANSION_P (arg
))
8836 /* "If every valid specialization of a variadic template
8837 requires an empty template parameter pack, the template is
8838 ill-formed, no diagnostic required." So check that the
8839 pattern works with this parameter. */
8840 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8841 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8845 if (conv
== error_mark_node
)
8847 if (complain
& tf_error
)
8848 inform (input_location
, "so any instantiation with a "
8849 "non-empty parameter pack would be ill-formed");
8852 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8853 /* Recover from missing typename. */
8854 TREE_VEC_ELT (inner_args
, arg_idx
)
8855 = make_pack_expansion (conv
, complain
);
8857 /* We don't know how many args we have yet, just
8858 use the unconverted ones for now. */
8859 new_inner_args
= inner_args
;
8864 else if (require_all_args
)
8866 /* There must be a default arg in this case. */
8867 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8869 /* The position of the first default template argument,
8870 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8872 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8873 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8874 arg_idx
- pack_adjust
);
8879 if (arg
== error_mark_node
)
8881 if (complain
& tf_error
)
8882 error ("template argument %d is invalid", arg_idx
+ 1);
8886 /* This can occur if there was an error in the template
8887 parameter list itself (which we would already have
8888 reported) that we are trying to recover from, e.g., a class
8889 template with a parameter list such as
8890 template<typename..., typename> (cpp0x/variadic150.C). */
8893 /* This can also happen with a fixed parameter pack (71834). */
8894 if (arg_idx
>= nargs
)
8898 arg
= convert_template_argument (TREE_VALUE (parm
),
8899 arg
, new_args
, complain
,
8902 if (arg
== error_mark_node
)
8905 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8908 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8910 /* If we had fixed parameter packs, we didn't know how many arguments we
8911 actually needed earlier; now we do. */
8912 nparms
+= fixed_pack_adjust
;
8913 variadic_p
-= fixed_packs
;
8917 if (arg_idx
< nargs
)
8919 /* We had some pack expansion arguments that will only work if the packs
8920 are empty, but wait until instantiation time to complain.
8921 See variadic-ttp3.C. */
8923 /* Except that we can't provide empty packs to alias templates or
8924 concepts when there are no corresponding parameters. Basically,
8925 we can get here with this:
8927 template<typename T> concept C = true;
8929 template<typename... Args>
8933 When parsing C<Args...>, we try to form a concept check of
8934 C<?, Args...>. Without the extra check for substituting an empty
8935 pack past the last parameter, we can accept the check as valid.
8937 FIXME: This may be valid for alias templates (but I doubt it).
8939 FIXME: The error could be better also. */
8940 if (in_decl
&& concept_definition_p (in_decl
))
8942 if (complain
& tf_error
)
8943 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8944 "too many arguments");
8945 return error_mark_node
;
8948 int len
= nparms
+ (nargs
- arg_idx
);
8949 tree args
= make_tree_vec (len
);
8951 for (; i
< nparms
; ++i
)
8952 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8953 for (; i
< len
; ++i
, ++arg_idx
)
8954 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8955 arg_idx
- pack_adjust
);
8956 new_inner_args
= args
;
8961 gcc_assert (!(complain
& tf_error
) || seen_error ());
8962 return error_mark_node
;
8965 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8966 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8967 TREE_VEC_LENGTH (new_inner_args
));
8969 return new_inner_args
;
8972 /* Convert all template arguments to their appropriate types, and
8973 return a vector containing the innermost resulting template
8974 arguments. If any error occurs, return error_mark_node. Error and
8975 warning messages are not issued.
8977 Note that no function argument deduction is performed, and default
8978 arguments are used to fill in unspecified arguments. */
8980 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8982 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8985 /* Convert all template arguments to their appropriate type, and
8986 instantiate default arguments as needed. This returns a vector
8987 containing the innermost resulting template arguments, or
8988 error_mark_node if unsuccessful. */
8990 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8991 tsubst_flags_t complain
)
8993 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8996 /* Like coerce_template_parms. If PARMS represents all template
8997 parameters levels, this function returns a vector of vectors
8998 representing all the resulting argument levels. Note that in this
8999 case, only the innermost arguments are coerced because the
9000 outermost ones are supposed to have been coerced already.
9002 Otherwise, if PARMS represents only (the innermost) vector of
9003 parameters, this function returns a vector containing just the
9004 innermost resulting arguments. */
9007 coerce_innermost_template_parms (tree parms
,
9010 tsubst_flags_t complain
,
9011 bool require_all_args
,
9012 bool use_default_args
)
9014 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9015 int args_depth
= TMPL_ARGS_DEPTH (args
);
9018 if (parms_depth
> 1)
9020 coerced_args
= make_tree_vec (parms_depth
);
9024 for (level
= parms
, cur_depth
= parms_depth
;
9025 parms_depth
> 0 && level
!= NULL_TREE
;
9026 level
= TREE_CHAIN (level
), --cur_depth
)
9029 if (cur_depth
== args_depth
)
9030 l
= coerce_template_parms (TREE_VALUE (level
),
9031 args
, in_decl
, complain
,
9035 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9037 if (l
== error_mark_node
)
9038 return error_mark_node
;
9040 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9044 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9045 args
, in_decl
, complain
,
9048 return coerced_args
;
9051 /* Returns true if T is a wrapper to make a C++20 template parameter
9055 class_nttp_const_wrapper_p (tree t
)
9057 if (cxx_dialect
< cxx2a
)
9059 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9060 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9061 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9064 /* Returns 1 if template args OT and NT are equivalent. */
9067 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9071 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9073 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9076 if (class_nttp_const_wrapper_p (nt
))
9077 nt
= TREE_OPERAND (nt
, 0);
9078 if (class_nttp_const_wrapper_p (ot
))
9079 ot
= TREE_OPERAND (ot
, 0);
9081 if (TREE_CODE (nt
) == TREE_VEC
)
9082 /* For member templates */
9083 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
9084 else if (PACK_EXPANSION_P (ot
))
9085 return (PACK_EXPANSION_P (nt
)
9086 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9087 PACK_EXPANSION_PATTERN (nt
))
9088 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9089 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9090 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9091 return cp_tree_equal (ot
, nt
);
9092 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9094 else if (TYPE_P (nt
))
9098 /* Don't treat an alias template specialization with dependent
9099 arguments as equivalent to its underlying type when used as a
9100 template argument; we need them to be distinct so that we
9101 substitute into the specialization arguments at instantiation
9102 time. And aliases can't be equivalent without being ==, so
9103 we don't need to look any deeper.
9105 During partial ordering, however, we need to treat them normally so
9106 that we can order uses of the same alias with different
9107 cv-qualification (79960). */
9109 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9112 return same_type_p (ot
, nt
);
9114 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
9118 /* Try to treat a template non-type argument that has been converted
9119 to the parameter type as equivalent to one that hasn't yet. */
9120 for (enum tree_code code1
= TREE_CODE (ot
);
9121 CONVERT_EXPR_CODE_P (code1
)
9122 || code1
== NON_LVALUE_EXPR
;
9123 code1
= TREE_CODE (ot
))
9124 ot
= TREE_OPERAND (ot
, 0);
9125 for (enum tree_code code2
= TREE_CODE (nt
);
9126 CONVERT_EXPR_CODE_P (code2
)
9127 || code2
== NON_LVALUE_EXPR
;
9128 code2
= TREE_CODE (nt
))
9129 nt
= TREE_OPERAND (nt
, 0);
9131 return cp_tree_equal (ot
, nt
);
9135 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9136 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9137 NEWARG_PTR with the offending arguments if they are non-NULL. */
9140 comp_template_args (tree oldargs
, tree newargs
,
9141 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9146 if (oldargs
== newargs
)
9149 if (!oldargs
|| !newargs
)
9152 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9155 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9157 tree nt
= TREE_VEC_ELT (newargs
, i
);
9158 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9160 if (! template_args_equal (ot
, nt
, partial_order
))
9162 if (oldarg_ptr
!= NULL
)
9164 if (newarg_ptr
!= NULL
)
9173 comp_template_args_porder (tree oargs
, tree nargs
)
9175 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9178 /* Implement a freelist interface for objects of type T.
9180 Head is a separate object, rather than a regular member, so that we
9181 can define it as a GTY deletable pointer, which is highly
9182 desirable. A data member could be declared that way, but then the
9183 containing object would implicitly get GTY((user)), which would
9184 prevent us from instantiating freelists as global objects.
9185 Although this way we can create freelist global objects, they're
9186 such thin wrappers that instantiating temporaries at every use
9187 loses nothing and saves permanent storage for the freelist object.
9189 Member functions next, anew, poison and reinit have default
9190 implementations that work for most of the types we're interested
9191 in, but if they don't work for some type, they should be explicitly
9192 specialized. See the comments before them for requirements, and
9193 the example specializations for the tree_list_freelist. */
9194 template <typename T
>
9197 /* Return the next object in a chain. We could just do type
9198 punning, but if we access the object with its underlying type, we
9199 avoid strict-aliasing trouble. This needs only work between
9200 poison and reinit. */
9201 static T
*&next (T
*obj
) { return obj
->next
; }
9203 /* Return a newly allocated, uninitialized or minimally-initialized
9204 object of type T. Any initialization performed by anew should
9205 either remain across the life of the object and the execution of
9206 poison, or be redone by reinit. */
9207 static T
*anew () { return ggc_alloc
<T
> (); }
9209 /* Optionally scribble all over the bits holding the object, so that
9210 they become (mostly?) uninitialized memory. This is called while
9211 preparing to make the object part of the free list. */
9212 static void poison (T
*obj
) {
9213 T
*p ATTRIBUTE_UNUSED
= obj
;
9214 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9216 #ifdef ENABLE_GC_CHECKING
9217 /* Poison the data, to indicate the data is garbage. */
9218 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9219 memset (p
, 0xa5, sizeof (*p
));
9221 /* Let valgrind know the object is free. */
9222 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9224 /* Let valgrind know the next portion of the object is available,
9225 but uninitialized. */
9226 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9229 /* Bring an object that underwent at least one lifecycle after anew
9230 and before the most recent free and poison, back to a usable
9231 state, reinitializing whatever is needed for it to be
9232 functionally equivalent to an object just allocated and returned
9233 by anew. This may poison or clear the next field, used by
9234 freelist housekeeping after poison was called. */
9235 static void reinit (T
*obj
) {
9236 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9238 #ifdef ENABLE_GC_CHECKING
9239 memset (q
, 0xa5, sizeof (*q
));
9241 /* Let valgrind know the entire object is available, but
9243 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9246 /* Reference a GTY-deletable pointer that points to the first object
9247 in the free list proper. */
9250 /* Construct a freelist object chaining objects off of HEAD. */
9251 freelist (T
*&head
) : head(head
) {}
9253 /* Add OBJ to the free object list. The former head becomes OBJ's
9262 /* Take an object from the free list, if one is available, or
9263 allocate a new one. Objects taken from the free list should be
9264 regarded as filled with garbage, except for bits that are
9265 configured to be preserved across free and alloc. */
9280 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9281 want to allocate a TREE_LIST using the usual interface, and ensure
9282 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9283 build_tree_list logic in reinit, so this could go out of sync. */
9286 freelist
<tree_node
>::next (tree obj
)
9288 return TREE_CHAIN (obj
);
9292 freelist
<tree_node
>::anew ()
9294 return build_tree_list (NULL
, NULL
);
9298 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9300 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9301 tree p ATTRIBUTE_UNUSED
= obj
;
9302 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9303 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9305 #ifdef ENABLE_GC_CHECKING
9306 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9308 /* Poison the data, to indicate the data is garbage. */
9309 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9310 memset (p
, 0xa5, size
);
9312 /* Let valgrind know the object is free. */
9313 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9314 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9315 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9316 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9318 #ifdef ENABLE_GC_CHECKING
9319 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9320 /* Keep TREE_CHAIN functional. */
9321 TREE_SET_CODE (obj
, TREE_LIST
);
9323 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9328 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9330 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9332 #ifdef ENABLE_GC_CHECKING
9333 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9334 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9335 memset (obj
, 0, sizeof (tree_list
));
9338 /* Let valgrind know the entire object is available, but
9340 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9342 #ifdef ENABLE_GC_CHECKING
9343 TREE_SET_CODE (obj
, TREE_LIST
);
9345 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9349 /* Point to the first object in the TREE_LIST freelist. */
9350 static GTY((deletable
)) tree tree_list_freelist_head
;
9351 /* Return the/an actual TREE_LIST freelist. */
9352 static inline freelist
<tree_node
>
9353 tree_list_freelist ()
9355 return tree_list_freelist_head
;
9358 /* Point to the first object in the tinst_level freelist. */
9359 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9360 /* Return the/an actual tinst_level freelist. */
9361 static inline freelist
<tinst_level
>
9362 tinst_level_freelist ()
9364 return tinst_level_freelist_head
;
9367 /* Point to the first object in the pending_template freelist. */
9368 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9369 /* Return the/an actual pending_template freelist. */
9370 static inline freelist
<pending_template
>
9371 pending_template_freelist ()
9373 return pending_template_freelist_head
;
9376 /* Build the TREE_LIST object out of a split list, store it
9377 permanently, and return it. */
9379 tinst_level::to_list ()
9381 gcc_assert (split_list_p ());
9382 tree ret
= tree_list_freelist ().alloc ();
9383 TREE_PURPOSE (ret
) = tldcl
;
9384 TREE_VALUE (ret
) = targs
;
9387 gcc_assert (tree_list_p ());
9391 const unsigned short tinst_level::refcount_infinity
;
9393 /* Increment OBJ's refcount unless it is already infinite. */
9394 static tinst_level
*
9395 inc_refcount_use (tinst_level
*obj
)
9397 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9402 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9404 tinst_level::free (tinst_level
*obj
)
9406 if (obj
->tree_list_p ())
9407 tree_list_freelist ().free (obj
->get_node ());
9408 tinst_level_freelist ().free (obj
);
9411 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9412 OBJ's DECL and OBJ, and start over with the tinst_level object that
9413 used to be referenced by OBJ's NEXT. */
9415 dec_refcount_use (tinst_level
*obj
)
9418 && obj
->refcount
!= tinst_level::refcount_infinity
9419 && !--obj
->refcount
)
9421 tinst_level
*next
= obj
->next
;
9422 tinst_level::free (obj
);
9427 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9428 and of the former PTR. Omitting the second argument is equivalent
9429 to passing (T*)NULL; this is allowed because passing the
9430 zero-valued integral constant NULL confuses type deduction and/or
9431 overload resolution. */
9432 template <typename T
>
9434 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9437 ptr
= inc_refcount_use (obj
);
9438 dec_refcount_use (save
);
9442 add_pending_template (tree d
)
9444 tree ti
= (TYPE_P (d
)
9445 ? CLASSTYPE_TEMPLATE_INFO (d
)
9446 : DECL_TEMPLATE_INFO (d
));
9447 struct pending_template
*pt
;
9450 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9453 /* We are called both from instantiate_decl, where we've already had a
9454 tinst_level pushed, and instantiate_template, where we haven't.
9456 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9457 level
= !current_tinst_level
9458 || current_tinst_level
->maybe_get_node () != d
;
9461 push_tinst_level (d
);
9463 pt
= pending_template_freelist ().alloc ();
9466 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9467 if (last_pending_template
)
9468 last_pending_template
->next
= pt
;
9470 pending_templates
= pt
;
9472 last_pending_template
= pt
;
9474 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9481 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9482 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9483 documentation for TEMPLATE_ID_EXPR. */
9486 lookup_template_function (tree fns
, tree arglist
)
9488 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9489 return error_mark_node
;
9491 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9493 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9495 error ("%q#D is not a function template", fns
);
9496 return error_mark_node
;
9499 if (BASELINK_P (fns
))
9501 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9503 BASELINK_FUNCTIONS (fns
),
9508 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9511 /* Within the scope of a template class S<T>, the name S gets bound
9512 (in build_self_reference) to a TYPE_DECL for the class, not a
9513 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9514 or one of its enclosing classes, and that type is a template,
9515 return the associated TEMPLATE_DECL. Otherwise, the original
9518 Also handle the case when DECL is a TREE_LIST of ambiguous
9519 injected-class-names from different bases. */
9522 maybe_get_template_decl_from_type_decl (tree decl
)
9524 if (decl
== NULL_TREE
)
9527 /* DR 176: A lookup that finds an injected-class-name (10.2
9528 [class.member.lookup]) can result in an ambiguity in certain cases
9529 (for example, if it is found in more than one base class). If all of
9530 the injected-class-names that are found refer to specializations of
9531 the same class template, and if the name is followed by a
9532 template-argument-list, the reference refers to the class template
9533 itself and not a specialization thereof, and is not ambiguous. */
9534 if (TREE_CODE (decl
) == TREE_LIST
)
9536 tree t
, tmpl
= NULL_TREE
;
9537 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9539 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9542 else if (tmpl
!= elt
)
9545 if (tmpl
&& t
== NULL_TREE
)
9551 return (decl
!= NULL_TREE
9552 && DECL_SELF_REFERENCE_P (decl
)
9553 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9554 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9557 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9558 parameters, find the desired type.
9560 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9562 IN_DECL, if non-NULL, is the template declaration we are trying to
9565 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9566 the class we are looking up.
9568 Issue error and warning messages under control of COMPLAIN.
9570 If the template class is really a local class in a template
9571 function, then the FUNCTION_CONTEXT is the function in which it is
9574 ??? Note that this function is currently called *twice* for each
9575 template-id: the first time from the parser, while creating the
9576 incomplete type (finish_template_type), and the second type during the
9577 real instantiation (instantiate_template_class). This is surely something
9578 that we want to avoid. It also causes some problems with argument
9579 coercion (see convert_nontype_argument for more information on this). */
9582 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9583 int entering_scope
, tsubst_flags_t complain
)
9585 tree templ
= NULL_TREE
, parmlist
;
9592 if (identifier_p (d1
))
9594 tree value
= innermost_non_namespace_value (d1
);
9595 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9600 push_decl_namespace (context
);
9601 templ
= lookup_name (d1
);
9602 templ
= maybe_get_template_decl_from_type_decl (templ
);
9604 pop_decl_namespace ();
9607 context
= DECL_CONTEXT (templ
);
9609 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9611 tree type
= TREE_TYPE (d1
);
9613 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9614 an implicit typename for the second A. Deal with it. */
9615 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9616 type
= TREE_TYPE (type
);
9618 if (CLASSTYPE_TEMPLATE_INFO (type
))
9620 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9621 d1
= DECL_NAME (templ
);
9624 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9625 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9627 templ
= TYPE_TI_TEMPLATE (d1
);
9628 d1
= DECL_NAME (templ
);
9630 else if (DECL_TYPE_TEMPLATE_P (d1
))
9633 d1
= DECL_NAME (templ
);
9634 context
= DECL_CONTEXT (templ
);
9636 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9639 d1
= DECL_NAME (templ
);
9642 /* Issue an error message if we didn't find a template. */
9645 if (complain
& tf_error
)
9646 error ("%qT is not a template", d1
);
9647 return error_mark_node
;
9650 if (TREE_CODE (templ
) != TEMPLATE_DECL
9651 /* Make sure it's a user visible template, if it was named by
9653 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9654 && !PRIMARY_TEMPLATE_P (templ
)))
9656 if (complain
& tf_error
)
9658 error ("non-template type %qT used as a template", d1
);
9660 error ("for template declaration %q+D", in_decl
);
9662 return error_mark_node
;
9665 complain
&= ~tf_user
;
9667 /* An alias that just changes the name of a template is equivalent to the
9668 other template, so if any of the arguments are pack expansions, strip
9669 the alias to avoid problems with a pack expansion passed to a non-pack
9670 alias template parameter (DR 1430). */
9671 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9672 templ
= get_underlying_template (templ
);
9674 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9677 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9678 if (arglist2
== error_mark_node
9679 || (!uses_template_parms (arglist2
)
9680 && check_instantiated_args (templ
, arglist2
, complain
)))
9681 return error_mark_node
;
9683 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9688 tree template_type
= TREE_TYPE (templ
);
9691 tree found
= NULL_TREE
;
9694 int is_dependent_type
;
9695 int use_partial_inst_tmpl
= false;
9697 if (template_type
== error_mark_node
)
9698 /* An error occurred while building the template TEMPL, and a
9699 diagnostic has most certainly been emitted for that
9700 already. Let's propagate that error. */
9701 return error_mark_node
;
9703 gen_tmpl
= most_general_template (templ
);
9704 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9705 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9706 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9708 if (arg_depth
== 1 && parm_depth
> 1)
9710 /* We've been given an incomplete set of template arguments.
9713 template <class T> struct S1 {
9714 template <class U> struct S2 {};
9715 template <class U> struct S2<U*> {};
9718 we will be called with an ARGLIST of `U*', but the
9719 TEMPLATE will be `template <class T> template
9720 <class U> struct S1<T>::S2'. We must fill in the missing
9722 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9723 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9724 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9727 /* Now we should have enough arguments. */
9728 gcc_assert (parm_depth
== arg_depth
);
9730 /* From here on, we're only interested in the most general
9733 /* Calculate the BOUND_ARGS. These will be the args that are
9734 actually tsubst'd into the definition to create the
9736 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9738 /*require_all_args=*/true,
9739 /*use_default_args=*/true);
9741 if (arglist
== error_mark_node
)
9742 /* We were unable to bind the arguments. */
9743 return error_mark_node
;
9745 /* In the scope of a template class, explicit references to the
9746 template class refer to the type of the template, not any
9747 instantiation of it. For example, in:
9749 template <class T> class C { void f(C<T>); }
9751 the `C<T>' is just the same as `C'. Outside of the
9752 class, however, such a reference is an instantiation. */
9754 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9755 || currently_open_class (template_type
))
9757 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9759 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9760 return template_type
;
9763 /* If we already have this specialization, return it. */
9764 elt
.tmpl
= gen_tmpl
;
9766 elt
.spec
= NULL_TREE
;
9767 hash
= spec_hasher::hash (&elt
);
9768 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9773 /* If the template's constraints are not satisfied,
9774 then we cannot form a valid type.
9776 Note that the check is deferred until after the hash
9777 lookup. This prevents redundant checks on previously
9778 instantiated specializations. */
9780 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9781 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9783 if (complain
& tf_error
)
9785 auto_diagnostic_group d
;
9786 error ("template constraint failure for %qD", gen_tmpl
);
9787 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9789 return error_mark_node
;
9792 is_dependent_type
= uses_template_parms (arglist
);
9794 /* If the deduced arguments are invalid, then the binding
9796 if (!is_dependent_type
9797 && check_instantiated_args (gen_tmpl
,
9798 INNERMOST_TEMPLATE_ARGS (arglist
),
9800 return error_mark_node
;
9802 if (!is_dependent_type
9803 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9804 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9805 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9807 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9808 DECL_NAME (gen_tmpl
),
9809 /*tag_scope=*/ts_global
);
9813 context
= DECL_CONTEXT (gen_tmpl
);
9814 if (context
&& TYPE_P (context
))
9816 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9817 context
= complete_type (context
);
9820 context
= tsubst (context
, arglist
, complain
, in_decl
);
9822 if (context
== error_mark_node
)
9823 return error_mark_node
;
9826 context
= global_namespace
;
9828 /* Create the type. */
9829 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9831 /* The user referred to a specialization of an alias
9832 template represented by GEN_TMPL.
9834 [temp.alias]/2 says:
9836 When a template-id refers to the specialization of an
9837 alias template, it is equivalent to the associated
9838 type obtained by substitution of its
9839 template-arguments for the template-parameters in the
9840 type-id of the alias template. */
9842 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9843 /* Note that the call above (by indirectly calling
9844 register_specialization in tsubst_decl) registers the
9845 TYPE_DECL representing the specialization of the alias
9846 template. So next time someone substitutes ARGLIST for
9847 the template parms into the alias template (GEN_TMPL),
9848 she'll get that TYPE_DECL back. */
9850 if (t
== error_mark_node
)
9853 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9855 if (!is_dependent_type
)
9857 set_current_access_from_decl (TYPE_NAME (template_type
));
9858 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9859 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9860 arglist
, complain
, in_decl
),
9861 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9862 arglist
, complain
, in_decl
),
9863 SCOPED_ENUM_P (template_type
), NULL
);
9865 if (t
== error_mark_node
)
9870 /* We don't want to call start_enum for this type, since
9871 the values for the enumeration constants may involve
9872 template parameters. And, no one should be interested
9873 in the enumeration constants for such a type. */
9874 t
= cxx_make_type (ENUMERAL_TYPE
);
9875 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9877 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9878 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9879 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9881 else if (CLASS_TYPE_P (template_type
))
9883 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9884 instantiated here. */
9885 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9887 t
= make_class_type (TREE_CODE (template_type
));
9888 CLASSTYPE_DECLARED_CLASS (t
)
9889 = CLASSTYPE_DECLARED_CLASS (template_type
);
9890 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9892 /* A local class. Make sure the decl gets registered properly. */
9893 if (context
== current_function_decl
)
9894 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9896 return error_mark_node
;
9898 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9899 /* This instantiation is another name for the primary
9900 template type. Set the TYPE_CANONICAL field
9902 TYPE_CANONICAL (t
) = template_type
;
9903 else if (any_template_arguments_need_structural_equality_p (arglist
))
9904 /* Some of the template arguments require structural
9905 equality testing, so this template class requires
9906 structural equality testing. */
9907 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9912 /* If we called start_enum or pushtag above, this information
9913 will already be set up. */
9916 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9918 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9919 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9920 DECL_SOURCE_LOCATION (type_decl
)
9921 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9924 type_decl
= TYPE_NAME (t
);
9926 if (CLASS_TYPE_P (template_type
))
9928 TREE_PRIVATE (type_decl
)
9929 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9930 TREE_PROTECTED (type_decl
)
9931 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9932 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9934 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9935 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9939 if (OVERLOAD_TYPE_P (t
)
9940 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9942 static const char *tags
[] = {"abi_tag", "may_alias"};
9944 for (unsigned ix
= 0; ix
!= 2; ix
++)
9947 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9951 = tree_cons (TREE_PURPOSE (attributes
),
9952 TREE_VALUE (attributes
),
9953 TYPE_ATTRIBUTES (t
));
9957 /* Let's consider the explicit specialization of a member
9958 of a class template specialization that is implicitly instantiated,
9963 template<class U> struct M {}; //#0
9968 struct S<int>::M<char> //#1
9972 [temp.expl.spec]/4 says this is valid.
9974 In this case, when we write:
9977 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9980 When we encounter #1, we want to store the partial instantiation
9981 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9983 For all cases other than this "explicit specialization of member of a
9984 class template", we just want to store the most general template into
9985 the CLASSTYPE_TI_TEMPLATE of M.
9987 This case of "explicit specialization of member of a class template"
9989 1/ the enclosing class is an instantiation of, and therefore not
9990 the same as, the context of the most general template, and
9991 2/ we aren't looking at the partial instantiation itself, i.e.
9992 the innermost arguments are not the same as the innermost parms of
9993 the most general template.
9995 So it's only when 1/ and 2/ happens that we want to use the partial
9996 instantiation of the member template in lieu of its most general
9999 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10000 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10001 /* the enclosing class must be an instantiation... */
10002 && CLASS_TYPE_P (context
)
10003 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10005 TREE_VEC_LENGTH (arglist
)--;
10006 ++processing_template_decl
;
10007 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10008 tree partial_inst_args
=
10009 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10010 arglist
, complain
, NULL_TREE
);
10011 --processing_template_decl
;
10012 TREE_VEC_LENGTH (arglist
)++;
10013 if (partial_inst_args
== error_mark_node
)
10014 return error_mark_node
;
10015 use_partial_inst_tmpl
=
10016 /*...and we must not be looking at the partial instantiation
10018 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10019 partial_inst_args
);
10022 if (!use_partial_inst_tmpl
)
10023 /* This case is easy; there are no member templates involved. */
10027 /* This is a full instantiation of a member template. Find
10028 the partial instantiation of which this is an instance. */
10030 /* Temporarily reduce by one the number of levels in the ARGLIST
10031 so as to avoid comparing the last set of arguments. */
10032 TREE_VEC_LENGTH (arglist
)--;
10033 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10034 TREE_VEC_LENGTH (arglist
)++;
10035 /* FOUND is either a proper class type, or an alias
10036 template specialization. In the later case, it's a
10037 TYPE_DECL, resulting from the substituting of arguments
10038 for parameters in the TYPE_DECL of the alias template
10039 done earlier. So be careful while getting the template
10041 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10043 : (TREE_CODE (found
) == TYPE_DECL
10044 ? DECL_TI_TEMPLATE (found
)
10045 : CLASSTYPE_TI_TEMPLATE (found
)));
10047 if (DECL_CLASS_TEMPLATE_P (found
)
10048 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10050 /* If this partial instantiation is specialized, we want to
10051 use it for hash table lookup. */
10053 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10054 hash
= spec_hasher::hash (&elt
);
10058 // Build template info for the new specialization.
10059 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10062 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10063 gcc_checking_assert (*slot
== NULL
);
10064 entry
= ggc_alloc
<spec_entry
> ();
10068 /* Note this use of the partial instantiation so we can check it
10069 later in maybe_process_partial_specialization. */
10070 DECL_TEMPLATE_INSTANTIATIONS (found
)
10071 = tree_cons (arglist
, t
,
10072 DECL_TEMPLATE_INSTANTIATIONS (found
));
10074 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10075 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10076 /* Now that the type has been registered on the instantiations
10077 list, we set up the enumerators. Because the enumeration
10078 constants may involve the enumeration type itself, we make
10079 sure to register the type first, and then create the
10080 constants. That way, doing tsubst_expr for the enumeration
10081 constants won't result in recursive calls here; we'll find
10082 the instantiation and exit above. */
10083 tsubst_enum (template_type
, t
, arglist
);
10085 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10086 /* If the type makes use of template parameters, the
10087 code that generates debugging information will crash. */
10088 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10090 /* Possibly limit visibility based on template args. */
10091 TREE_PUBLIC (type_decl
) = 1;
10092 determine_visibility (type_decl
);
10094 inherit_targ_abi_tags (t
);
10100 /* Wrapper for lookup_template_class_1. */
10103 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10104 int entering_scope
, tsubst_flags_t complain
)
10107 timevar_push (TV_TEMPLATE_INST
);
10108 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10109 entering_scope
, complain
);
10110 timevar_pop (TV_TEMPLATE_INST
);
10114 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10117 lookup_template_variable (tree templ
, tree arglist
)
10119 if (flag_concepts
&& variable_concept_p (templ
))
10120 return build_concept_check (templ
, arglist
, tf_none
);
10122 /* The type of the expression is NULL_TREE since the template-id could refer
10123 to an explicit or partial specialization. */
10124 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10127 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10130 finish_template_variable (tree var
, tsubst_flags_t complain
)
10132 tree templ
= TREE_OPERAND (var
, 0);
10133 tree arglist
= TREE_OPERAND (var
, 1);
10135 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10136 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10138 templ
= most_general_template (templ
);
10139 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10140 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10142 /*use_default*/true);
10144 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10146 if (complain
& tf_error
)
10148 auto_diagnostic_group d
;
10149 error ("use of invalid variable template %qE", var
);
10150 diagnose_constraints (location_of (var
), templ
, arglist
);
10152 return error_mark_node
;
10155 return instantiate_template (templ
, arglist
, complain
);
10158 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10159 TARGS template args, and instantiate it if it's not dependent. */
10162 lookup_and_finish_template_variable (tree templ
, tree targs
,
10163 tsubst_flags_t complain
)
10165 templ
= lookup_template_variable (templ
, targs
);
10166 if (!any_dependent_template_arguments_p (targs
))
10168 templ
= finish_template_variable (templ
, complain
);
10172 return convert_from_reference (templ
);
10176 struct pair_fn_data
10181 /* True when we should also visit template parameters that occur in
10182 non-deduced contexts. */
10183 bool include_nondeduced_p
;
10184 hash_set
<tree
> *visited
;
10187 /* Called from for_each_template_parm via walk_tree. */
10190 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10193 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10194 tree_fn_t fn
= pfd
->fn
;
10195 void *data
= pfd
->data
;
10196 tree result
= NULL_TREE
;
10198 #define WALK_SUBTREE(NODE) \
10201 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10202 pfd->include_nondeduced_p, \
10204 if (result) goto out; \
10208 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10212 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10213 WALK_SUBTREE (TYPE_CONTEXT (t
));
10215 switch (TREE_CODE (t
))
10218 if (TYPE_PTRMEMFUNC_P (t
))
10220 /* Fall through. */
10223 case ENUMERAL_TYPE
:
10224 if (!TYPE_TEMPLATE_INFO (t
))
10225 *walk_subtrees
= 0;
10227 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10231 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10232 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10236 /* Since we're not going to walk subtrees, we have to do this
10237 explicitly here. */
10238 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10239 /* Fall through. */
10241 case FUNCTION_TYPE
:
10242 /* Check the return type. */
10243 WALK_SUBTREE (TREE_TYPE (t
));
10245 /* Check the parameter types. Since default arguments are not
10246 instantiated until they are needed, the TYPE_ARG_TYPES may
10247 contain expressions that involve template parameters. But,
10248 no-one should be looking at them yet. And, once they're
10249 instantiated, they don't contain template parameters, so
10250 there's no point in looking at them then, either. */
10254 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10255 WALK_SUBTREE (TREE_VALUE (parm
));
10257 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10258 want walk_tree walking into them itself. */
10259 *walk_subtrees
= 0;
10262 if (flag_noexcept_type
)
10264 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10266 WALK_SUBTREE (TREE_PURPOSE (spec
));
10271 case DECLTYPE_TYPE
:
10272 case UNDERLYING_TYPE
:
10273 if (pfd
->include_nondeduced_p
10274 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10276 pfd
->include_nondeduced_p
,
10278 return error_mark_node
;
10279 *walk_subtrees
= false;
10282 case FUNCTION_DECL
:
10284 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10285 WALK_SUBTREE (DECL_TI_ARGS (t
));
10286 /* Fall through. */
10290 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10291 WALK_SUBTREE (DECL_INITIAL (t
));
10292 if (DECL_CONTEXT (t
)
10293 && pfd
->include_nondeduced_p
)
10294 WALK_SUBTREE (DECL_CONTEXT (t
));
10297 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10298 /* Record template parameters such as `T' inside `TT<T>'. */
10299 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10300 /* Fall through. */
10302 case TEMPLATE_TEMPLATE_PARM
:
10303 case TEMPLATE_TYPE_PARM
:
10304 case TEMPLATE_PARM_INDEX
:
10305 if (fn
&& (*fn
)(t
, data
))
10311 case TEMPLATE_DECL
:
10312 /* A template template parameter is encountered. */
10313 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10314 WALK_SUBTREE (TREE_TYPE (t
));
10316 /* Already substituted template template parameter */
10317 *walk_subtrees
= 0;
10320 case TYPENAME_TYPE
:
10321 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10322 partial instantiation. */
10323 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10327 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10328 && pfd
->include_nondeduced_p
)
10329 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10333 case COMPONENT_REF
:
10334 /* If there's no type, then this thing must be some expression
10335 involving template parameters. */
10336 if (!fn
&& !TREE_TYPE (t
))
10337 return error_mark_node
;
10342 case IMPLICIT_CONV_EXPR
:
10343 case REINTERPRET_CAST_EXPR
:
10344 case CONST_CAST_EXPR
:
10345 case STATIC_CAST_EXPR
:
10346 case DYNAMIC_CAST_EXPR
:
10350 case PSEUDO_DTOR_EXPR
:
10352 return error_mark_node
;
10356 if (pfd
->include_nondeduced_p
)
10357 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10360 case REQUIRES_EXPR
:
10363 return error_mark_node
;
10365 /* Recursively walk the type of each constraint variable. */
10366 tree p
= TREE_OPERAND (t
, 0);
10369 WALK_SUBTREE (TREE_TYPE (p
));
10370 p
= TREE_CHAIN (p
);
10379 #undef WALK_SUBTREE
10381 /* We didn't find any template parameters we liked. */
10386 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10387 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10388 call FN with the parameter and the DATA.
10389 If FN returns nonzero, the iteration is terminated, and
10390 for_each_template_parm returns 1. Otherwise, the iteration
10391 continues. If FN never returns a nonzero value, the value
10392 returned by for_each_template_parm is 0. If FN is NULL, it is
10393 considered to be the function which always returns 1.
10395 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10396 parameters that occur in non-deduced contexts. When false, only
10397 visits those template parameters that can be deduced. */
10400 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10401 hash_set
<tree
> *visited
,
10402 bool include_nondeduced_p
,
10405 struct pair_fn_data pfd
;
10410 pfd
.any_fn
= any_fn
;
10412 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10414 /* Walk the tree. (Conceptually, we would like to walk without
10415 duplicates, but for_each_template_parm_r recursively calls
10416 for_each_template_parm, so we would need to reorganize a fair
10417 bit to use walk_tree_without_duplicates, so we keep our own
10420 pfd
.visited
= visited
;
10422 pfd
.visited
= new hash_set
<tree
>;
10423 result
= cp_walk_tree (&t
,
10424 for_each_template_parm_r
,
10431 delete pfd
.visited
;
10438 struct find_template_parameter_info
10440 explicit find_template_parameter_info (tree ctx_parms
)
10441 : parm_list (NULL_TREE
),
10442 ctx_parms (ctx_parms
),
10443 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10446 hash_set
<tree
> visited
;
10447 hash_set
<tree
> parms
;
10453 /* Appends the declaration of T to the list in DATA. */
10456 keep_template_parm (tree t
, void* data
)
10458 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10460 /* Template parameters declared within the expression are not part of
10461 the parameter mapping. For example, in this concept:
10463 template<typename T>
10464 concept C = requires { <expr> } -> same_as<int>;
10466 the return specifier same_as<int> declares a new decltype parameter
10467 that must not be part of the parameter mapping. The same is true
10468 for generic lambda parameters, lambda template parameters, etc. */
10471 template_parm_level_and_index (t
, &level
, &index
);
10472 if (level
> ftpi
->max_depth
)
10475 /* Arguments like const T yield parameters like const T. This means that
10476 a template-id like X<T, const T> would yield two distinct parameters:
10477 T and const T. Adjust types to their unqualified versions. */
10479 t
= TYPE_MAIN_VARIANT (t
);
10480 if (!ftpi
->parms
.add (t
))
10481 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10486 /* Ensure that we recursively examine certain terms that are not normally
10487 visited in for_each_template_parm_r. */
10490 any_template_parm_r (tree t
, void *data
)
10492 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10494 #define WALK_SUBTREE(NODE) \
10497 for_each_template_parm (NODE, keep_template_parm, data, \
10498 &ftpi->visited, true, \
10499 any_template_parm_r); \
10503 /* A mention of a member alias/typedef is a use of all of its template
10504 arguments, including those from the enclosing class, so we don't use
10505 alias_template_specialization_p here. */
10506 if (TYPE_P (t
) && typedef_variant_p (t
))
10507 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10508 WALK_SUBTREE (TI_ARGS (tinfo
));
10510 switch (TREE_CODE (t
))
10512 case TEMPLATE_TYPE_PARM
:
10513 /* Type constraints of a placeholder type may contain parameters. */
10515 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10516 WALK_SUBTREE (constr
);
10519 case TEMPLATE_ID_EXPR
:
10520 /* Search through references to variable templates. */
10521 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10522 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10525 case TEMPLATE_PARM_INDEX
:
10527 /* A parameter or constraint variable may also depend on a template
10528 parameter without explicitly naming it. */
10529 WALK_SUBTREE (TREE_TYPE (t
));
10532 case TEMPLATE_DECL
:
10534 /* If T is a member template that shares template parameters with
10535 ctx_parms, we need to mark all those parameters for mapping. */
10536 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10537 tree cparms
= ftpi
->ctx_parms
;
10538 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10539 dparms
= TREE_CHAIN (dparms
);
10540 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10541 cparms
= TREE_CHAIN (cparms
);
10543 && (TREE_TYPE (TREE_VALUE (dparms
))
10544 != TREE_TYPE (TREE_VALUE (cparms
))))
10545 dparms
= TREE_CHAIN (dparms
),
10546 cparms
= TREE_CHAIN (cparms
);
10549 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10550 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10551 for (int i
= 0; i
< ddepth
; ++i
)
10552 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10559 /* Look in the parms and body. */
10560 tree fn
= lambda_function (t
);
10561 WALK_SUBTREE (TREE_TYPE (fn
));
10562 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10566 case IDENTIFIER_NODE
:
10567 if (IDENTIFIER_CONV_OP_P (t
))
10568 /* The conversion-type-id of a conversion operator may be dependent. */
10569 WALK_SUBTREE (TREE_TYPE (t
));
10576 /* Keep walking. */
10580 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10581 are the template parameters in scope. */
10584 find_template_parameters (tree t
, tree ctx_parms
)
10589 find_template_parameter_info
ftpi (ctx_parms
);
10590 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10591 /*include_nondeduced*/true, any_template_parm_r
);
10592 return ftpi
.parm_list
;
10595 /* Returns true if T depends on any template parameter. */
10598 uses_template_parms (tree t
)
10600 if (t
== NULL_TREE
)
10604 int saved_processing_template_decl
;
10606 saved_processing_template_decl
= processing_template_decl
;
10607 if (!saved_processing_template_decl
)
10608 processing_template_decl
= 1;
10610 dependent_p
= dependent_type_p (t
);
10611 else if (TREE_CODE (t
) == TREE_VEC
)
10612 dependent_p
= any_dependent_template_arguments_p (t
);
10613 else if (TREE_CODE (t
) == TREE_LIST
)
10614 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10615 || uses_template_parms (TREE_CHAIN (t
)));
10616 else if (TREE_CODE (t
) == TYPE_DECL
)
10617 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10618 else if (t
== error_mark_node
)
10619 dependent_p
= false;
10621 dependent_p
= value_dependent_expression_p (t
);
10623 processing_template_decl
= saved_processing_template_decl
;
10625 return dependent_p
;
10628 /* Returns true iff current_function_decl is an incompletely instantiated
10629 template. Useful instead of processing_template_decl because the latter
10630 is set to 0 during instantiate_non_dependent_expr. */
10633 in_template_function (void)
10635 tree fn
= current_function_decl
;
10637 ++processing_template_decl
;
10638 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10639 && DECL_TEMPLATE_INFO (fn
)
10640 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10641 --processing_template_decl
;
10645 /* Returns true if T depends on any template parameter with level LEVEL. */
10648 uses_template_parms_level (tree t
, int level
)
10650 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10651 /*include_nondeduced_p=*/true);
10654 /* Returns true if the signature of DECL depends on any template parameter from
10655 its enclosing class. */
10658 uses_outer_template_parms (tree decl
)
10660 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10663 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10664 &depth
, NULL
, /*include_nondeduced_p=*/true))
10666 if (PRIMARY_TEMPLATE_P (decl
)
10667 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10668 (DECL_TEMPLATE_PARMS (decl
)),
10669 template_parm_outer_level
,
10670 &depth
, NULL
, /*include_nondeduced_p=*/true))
10672 tree ci
= get_constraints (decl
);
10674 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10675 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10676 &depth
, NULL
, /*nondeduced*/true))
10681 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10682 ill-formed translation unit, i.e. a variable or function that isn't
10683 usable in a constant expression. */
10686 neglectable_inst_p (tree d
)
10688 return (d
&& DECL_P (d
)
10689 && !undeduced_auto_decl (d
)
10690 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10691 : decl_maybe_constant_var_p (d
)));
10694 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10695 neglectable and instantiated from within an erroneous instantiation. */
10698 limit_bad_template_recursion (tree decl
)
10700 struct tinst_level
*lev
= current_tinst_level
;
10701 int errs
= errorcount
+ sorrycount
;
10702 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10705 for (; lev
; lev
= lev
->next
)
10706 if (neglectable_inst_p (lev
->maybe_get_node ()))
10709 return (lev
&& errs
> lev
->errors
);
10712 static int tinst_depth
;
10713 extern int max_tinst_depth
;
10716 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10718 /* We're starting to instantiate D; record the template instantiation context
10719 at LOC for diagnostics and to restore it later. */
10722 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10724 struct tinst_level
*new_level
;
10726 if (tinst_depth
>= max_tinst_depth
)
10728 /* Tell error.c not to try to instantiate any templates. */
10730 fatal_error (input_location
,
10731 "template instantiation depth exceeds maximum of %d"
10732 " (use %<-ftemplate-depth=%> to increase the maximum)",
10737 /* If the current instantiation caused problems, don't let it instantiate
10738 anything else. Do allow deduction substitution and decls usable in
10739 constant expressions. */
10740 if (!targs
&& limit_bad_template_recursion (tldcl
))
10742 /* Avoid no_linkage_errors and unused function warnings for this
10744 TREE_NO_WARNING (tldcl
) = 1;
10748 /* When not -quiet, dump template instantiations other than functions, since
10749 announce_function will take care of those. */
10750 if (!quiet_flag
&& !targs
10751 && TREE_CODE (tldcl
) != TREE_LIST
10752 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10753 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10755 new_level
= tinst_level_freelist ().alloc ();
10756 new_level
->tldcl
= tldcl
;
10757 new_level
->targs
= targs
;
10758 new_level
->locus
= loc
;
10759 new_level
->errors
= errorcount
+ sorrycount
;
10760 new_level
->next
= NULL
;
10761 new_level
->refcount
= 0;
10762 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10763 set_refcount_ptr (current_tinst_level
, new_level
);
10766 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10767 depth_reached
= tinst_depth
;
10772 /* We're starting substitution of TMPL<ARGS>; record the template
10773 substitution context for diagnostics and to restore it later. */
10776 push_tinst_level (tree tmpl
, tree args
)
10778 return push_tinst_level_loc (tmpl
, args
, input_location
);
10781 /* We're starting to instantiate D; record INPUT_LOCATION and the
10782 template instantiation context for diagnostics and to restore it
10786 push_tinst_level (tree d
)
10788 return push_tinst_level_loc (d
, input_location
);
10791 /* Likewise, but record LOC as the program location. */
10794 push_tinst_level_loc (tree d
, location_t loc
)
10796 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10797 return push_tinst_level_loc (d
, NULL
, loc
);
10800 /* We're done instantiating this template; return to the instantiation
10804 pop_tinst_level (void)
10806 /* Restore the filename and line number stashed away when we started
10807 this instantiation. */
10808 input_location
= current_tinst_level
->locus
;
10809 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10813 /* We're instantiating a deferred template; restore the template
10814 instantiation context in which the instantiation was requested, which
10815 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10818 reopen_tinst_level (struct tinst_level
*level
)
10820 struct tinst_level
*t
;
10823 for (t
= level
; t
; t
= t
->next
)
10826 set_refcount_ptr (current_tinst_level
, level
);
10827 pop_tinst_level ();
10828 if (current_tinst_level
)
10829 current_tinst_level
->errors
= errorcount
+sorrycount
;
10830 return level
->maybe_get_node ();
10833 /* Returns the TINST_LEVEL which gives the original instantiation
10836 struct tinst_level
*
10837 outermost_tinst_level (void)
10839 struct tinst_level
*level
= current_tinst_level
;
10841 while (level
->next
)
10842 level
= level
->next
;
10846 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10847 vector of template arguments, as for tsubst.
10849 Returns an appropriate tsubst'd friend declaration. */
10852 tsubst_friend_function (tree decl
, tree args
)
10856 if (TREE_CODE (decl
) == FUNCTION_DECL
10857 && DECL_TEMPLATE_INSTANTIATION (decl
)
10858 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10859 /* This was a friend declared with an explicit template
10860 argument list, e.g.:
10862 friend void f<>(T);
10864 to indicate that f was a template instantiation, not a new
10865 function declaration. Now, we have to figure out what
10866 instantiation of what template. */
10868 tree template_id
, arglist
, fns
;
10871 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10873 /* Friend functions are looked up in the containing namespace scope.
10874 We must enter that scope, to avoid finding member functions of the
10875 current class with same name. */
10876 push_nested_namespace (ns
);
10877 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10878 tf_warning_or_error
, NULL_TREE
,
10879 /*integral_constant_expression_p=*/false);
10880 pop_nested_namespace (ns
);
10881 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10882 tf_warning_or_error
, NULL_TREE
);
10883 template_id
= lookup_template_function (fns
, arglist
);
10885 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10886 tmpl
= determine_specialization (template_id
, new_friend
,
10888 /*need_member_template=*/0,
10889 TREE_VEC_LENGTH (args
),
10891 return instantiate_template (tmpl
, new_args
, tf_error
);
10894 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10896 /* The NEW_FRIEND will look like an instantiation, to the
10897 compiler, but is not an instantiation from the point of view of
10898 the language. For example, we might have had:
10900 template <class T> struct S {
10901 template <class U> friend void f(T, U);
10904 Then, in S<int>, template <class U> void f(int, U) is not an
10905 instantiation of anything. */
10906 if (new_friend
== error_mark_node
)
10907 return error_mark_node
;
10909 DECL_USE_TEMPLATE (new_friend
) = 0;
10910 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10912 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10913 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10914 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10916 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10917 match in decls_match. */
10918 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10919 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
10920 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
10921 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10924 /* The mangled name for the NEW_FRIEND is incorrect. The function
10925 is not a template instantiation and should not be mangled like
10926 one. Therefore, we forget the mangling here; we'll recompute it
10927 later if we need it. */
10928 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10930 SET_DECL_RTL (new_friend
, NULL
);
10931 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10934 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10937 tree new_friend_template_info
;
10938 tree new_friend_result_template_info
;
10940 int new_friend_is_defn
;
10942 /* We must save some information from NEW_FRIEND before calling
10943 duplicate decls since that function will free NEW_FRIEND if
10945 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10946 new_friend_is_defn
=
10947 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10948 (template_for_substitution (new_friend
)))
10950 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10952 /* This declaration is a `primary' template. */
10953 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10955 new_friend_result_template_info
10956 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10959 new_friend_result_template_info
= NULL_TREE
;
10961 /* Inside pushdecl_namespace_level, we will push into the
10962 current namespace. However, the friend function should go
10963 into the namespace of the template. */
10964 ns
= decl_namespace_context (new_friend
);
10965 push_nested_namespace (ns
);
10966 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10967 pop_nested_namespace (ns
);
10969 if (old_decl
== error_mark_node
)
10970 return error_mark_node
;
10972 if (old_decl
!= new_friend
)
10974 /* This new friend declaration matched an existing
10975 declaration. For example, given:
10977 template <class T> void f(T);
10978 template <class U> class C {
10979 template <class T> friend void f(T) {}
10982 the friend declaration actually provides the definition
10983 of `f', once C has been instantiated for some type. So,
10984 old_decl will be the out-of-class template declaration,
10985 while new_friend is the in-class definition.
10987 But, if `f' was called before this point, the
10988 instantiation of `f' will have DECL_TI_ARGS corresponding
10989 to `T' but not to `U', references to which might appear
10990 in the definition of `f'. Previously, the most general
10991 template for an instantiation of `f' was the out-of-class
10992 version; now it is the in-class version. Therefore, we
10993 run through all specialization of `f', adding to their
10994 DECL_TI_ARGS appropriately. In particular, they need a
10995 new set of outer arguments, corresponding to the
10996 arguments for this class instantiation.
10998 The same situation can arise with something like this:
11000 friend void f(int);
11001 template <class T> class C {
11002 friend void f(T) {}
11005 when `C<int>' is instantiated. Now, `f(int)' is defined
11008 if (!new_friend_is_defn
)
11009 /* On the other hand, if the in-class declaration does
11010 *not* provide a definition, then we don't want to alter
11011 existing definitions. We can just leave everything
11016 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11017 tree new_args
= TI_ARGS (new_friend_template_info
);
11019 /* Overwrite whatever template info was there before, if
11020 any, with the new template information pertaining to
11021 the declaration. */
11022 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11024 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11026 /* We should have called reregister_specialization in
11027 duplicate_decls. */
11028 gcc_assert (retrieve_specialization (new_template
,
11032 /* Instantiate it if the global has already been used. */
11033 if (DECL_ODR_USED (old_decl
))
11034 instantiate_decl (old_decl
, /*defer_ok=*/true,
11035 /*expl_inst_class_mem_p=*/false);
11041 /* Indicate that the old function template is a partial
11043 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11044 = new_friend_result_template_info
;
11046 gcc_assert (new_template
11047 == most_general_template (new_template
));
11048 gcc_assert (new_template
!= old_decl
);
11050 /* Reassign any specializations already in the hash table
11051 to the new more general template, and add the
11052 additional template args. */
11053 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11055 t
= TREE_CHAIN (t
))
11057 tree spec
= TREE_VALUE (t
);
11060 elt
.tmpl
= old_decl
;
11061 elt
.args
= DECL_TI_ARGS (spec
);
11062 elt
.spec
= NULL_TREE
;
11064 decl_specializations
->remove_elt (&elt
);
11066 DECL_TI_ARGS (spec
)
11067 = add_outermost_template_args (new_args
,
11068 DECL_TI_ARGS (spec
));
11070 register_specialization
11071 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11074 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11078 /* The information from NEW_FRIEND has been merged into OLD_DECL
11079 by duplicate_decls. */
11080 new_friend
= old_decl
;
11085 tree context
= DECL_CONTEXT (new_friend
);
11089 template <class T> class C {
11090 template <class U> friend void C1<U>::f (); // case 1
11091 friend void C2<T>::f (); // case 2
11093 we only need to make sure CONTEXT is a complete type for
11094 case 2. To distinguish between the two cases, we note that
11095 CONTEXT of case 1 remains dependent type after tsubst while
11096 this isn't true for case 2. */
11097 ++processing_template_decl
;
11098 dependent_p
= dependent_type_p (context
);
11099 --processing_template_decl
;
11102 && !complete_type_or_else (context
, NULL_TREE
))
11103 return error_mark_node
;
11105 if (COMPLETE_TYPE_P (context
))
11107 tree fn
= new_friend
;
11108 /* do_friend adds the TEMPLATE_DECL for any member friend
11109 template even if it isn't a member template, i.e.
11110 template <class T> friend A<T>::f();
11111 Look through it in that case. */
11112 if (TREE_CODE (fn
) == TEMPLATE_DECL
11113 && !PRIMARY_TEMPLATE_P (fn
))
11114 fn
= DECL_TEMPLATE_RESULT (fn
);
11115 /* Check to see that the declaration is really present, and,
11116 possibly obtain an improved declaration. */
11117 fn
= check_classfn (context
, fn
, NULL_TREE
);
11127 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11128 template arguments, as for tsubst.
11130 Returns an appropriate tsubst'd friend type or error_mark_node on
11134 tsubst_friend_class (tree friend_tmpl
, tree args
)
11138 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11140 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11141 return TREE_TYPE (tmpl
);
11144 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11145 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11146 push_nested_namespace (context
);
11149 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11150 push_nested_class (context
);
11153 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11154 /*non_class=*/false, /*block_p=*/false,
11155 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11157 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11159 /* The friend template has already been declared. Just
11160 check to see that the declarations match, and install any new
11161 default parameters. We must tsubst the default parameters,
11162 of course. We only need the innermost template parameters
11163 because that is all that redeclare_class_template will look
11165 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11166 > TMPL_ARGS_DEPTH (args
))
11168 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11169 args
, tf_warning_or_error
);
11170 location_t saved_input_location
= input_location
;
11171 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11172 tree cons
= get_constraints (tmpl
);
11173 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11174 input_location
= saved_input_location
;
11179 /* The friend template has not already been declared. In this
11180 case, the instantiation of the template class will cause the
11181 injection of this template into the namespace scope. */
11182 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11184 if (tmpl
!= error_mark_node
)
11186 /* The new TMPL is not an instantiation of anything, so we
11187 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11188 for the new type because that is supposed to be the
11189 corresponding template decl, i.e., TMPL. */
11190 DECL_USE_TEMPLATE (tmpl
) = 0;
11191 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11192 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11193 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11194 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11196 /* It is hidden. */
11197 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11198 DECL_ANTICIPATED (tmpl
)
11199 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11201 /* Inject this template into the enclosing namspace scope. */
11202 tmpl
= pushdecl_namespace_level (tmpl
, true);
11206 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11207 pop_nested_namespace (context
);
11209 pop_nested_class ();
11211 return TREE_TYPE (tmpl
);
11214 /* Returns zero if TYPE cannot be completed later due to circularity.
11215 Otherwise returns one. */
11218 can_complete_type_without_circularity (tree type
)
11220 if (type
== NULL_TREE
|| type
== error_mark_node
)
11222 else if (COMPLETE_TYPE_P (type
))
11224 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11225 return can_complete_type_without_circularity (TREE_TYPE (type
));
11226 else if (CLASS_TYPE_P (type
)
11227 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11233 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11234 tsubst_flags_t
, tree
);
11236 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11237 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11240 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11241 tsubst_flags_t complain
, tree in_decl
)
11243 gcc_assert (ATTR_IS_DEPENDENT (t
));
11245 tree val
= TREE_VALUE (t
);
11246 if (val
== NULL_TREE
)
11247 /* Nothing to do. */;
11248 else if ((flag_openmp
|| flag_openmp_simd
)
11249 && is_attribute_p ("omp declare simd",
11250 get_attribute_name (t
)))
11252 tree clauses
= TREE_VALUE (val
);
11253 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11254 complain
, in_decl
);
11255 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11256 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11257 tree parms
= DECL_ARGUMENTS (*decl_p
);
11259 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11261 val
= build_tree_list (NULL_TREE
, clauses
);
11265 else if (flag_openmp
11266 && is_attribute_p ("omp declare variant base",
11267 get_attribute_name (t
)))
11269 ++cp_unevaluated_operand
;
11271 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11272 in_decl
, /*integral_constant_expression_p=*/false);
11273 --cp_unevaluated_operand
;
11274 tree chain
= TREE_CHAIN (val
);
11275 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11276 tree ctx
= copy_list (TREE_VALUE (val
));
11277 tree simd
= get_identifier ("simd");
11278 tree score
= get_identifier (" score");
11279 tree condition
= get_identifier ("condition");
11280 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11282 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11283 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11284 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11286 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11288 tree clauses
= TREE_VALUE (t2
);
11289 clauses
= tsubst_omp_clauses (clauses
,
11290 C_ORT_OMP_DECLARE_SIMD
, args
,
11291 complain
, in_decl
);
11292 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11293 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11294 TREE_VALUE (t2
) = clauses
;
11298 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11299 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11300 if (TREE_VALUE (t3
))
11303 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11304 && TREE_PURPOSE (t3
) != score
);
11305 tree v
= TREE_VALUE (t3
);
11306 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11308 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11309 v
= fold_non_dependent_expr (v
);
11310 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11311 || (TREE_PURPOSE (t3
) == score
11312 ? TREE_CODE (v
) != INTEGER_CST
11313 : !tree_fits_shwi_p (v
)))
11316 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11318 if (TREE_PURPOSE (t3
) == score
)
11319 error_at (loc
, "score argument must be "
11320 "constant integer expression");
11321 else if (allow_string
)
11322 error_at (loc
, "property must be constant "
11323 "integer expression or string "
11326 error_at (loc
, "property must be constant "
11327 "integer expression");
11330 else if (TREE_PURPOSE (t3
) == score
11331 && tree_int_cst_sgn (v
) < 0)
11334 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11336 error_at (loc
, "score argument must be "
11340 TREE_VALUE (t3
) = v
;
11345 val
= tree_cons (varid
, ctx
, chain
);
11347 /* If the first attribute argument is an identifier, don't
11348 pass it through tsubst. Attributes like mode, format,
11349 cleanup and several target specific attributes expect it
11351 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11354 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11355 /*integral_constant_expression_p=*/false);
11356 if (chain
!= TREE_CHAIN (val
))
11357 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11359 else if (PACK_EXPANSION_P (val
))
11361 /* An attribute pack expansion. */
11362 tree purp
= TREE_PURPOSE (t
);
11363 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11364 if (pack
== error_mark_node
)
11365 return error_mark_node
;
11366 int len
= TREE_VEC_LENGTH (pack
);
11367 tree list
= NULL_TREE
;
11369 for (int i
= 0; i
< len
; ++i
)
11371 tree elt
= TREE_VEC_ELT (pack
, i
);
11372 *q
= build_tree_list (purp
, elt
);
11373 q
= &TREE_CHAIN (*q
);
11378 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11379 /*integral_constant_expression_p=*/false);
11381 if (val
!= TREE_VALUE (t
))
11382 return build_tree_list (TREE_PURPOSE (t
), val
);
11386 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11387 unchanged or a new TREE_LIST chain. */
11390 tsubst_attributes (tree attributes
, tree args
,
11391 tsubst_flags_t complain
, tree in_decl
)
11393 tree last_dep
= NULL_TREE
;
11395 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11396 if (ATTR_IS_DEPENDENT (t
))
11399 attributes
= copy_list (attributes
);
11404 for (tree
*p
= &attributes
; *p
; )
11407 if (ATTR_IS_DEPENDENT (t
))
11409 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11414 p
= &TREE_CHAIN (*p
);
11415 *p
= TREE_CHAIN (t
);
11419 p
= &TREE_CHAIN (*p
);
11425 /* Apply any attributes which had to be deferred until instantiation
11426 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11427 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11430 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11431 tree args
, tsubst_flags_t complain
, tree in_decl
)
11433 tree last_dep
= NULL_TREE
;
11437 if (attributes
== NULL_TREE
)
11440 if (DECL_P (*decl_p
))
11442 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11444 p
= &DECL_ATTRIBUTES (*decl_p
);
11445 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11446 to our attributes parameter. */
11447 gcc_assert (*p
== attributes
);
11451 p
= &TYPE_ATTRIBUTES (*decl_p
);
11452 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11453 lookup_template_class_1, and should be preserved. */
11454 gcc_assert (*p
!= attributes
);
11456 p
= &TREE_CHAIN (*p
);
11459 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11460 if (ATTR_IS_DEPENDENT (t
))
11463 attributes
= copy_list (attributes
);
11470 tree late_attrs
= NULL_TREE
;
11471 tree
*q
= &late_attrs
;
11476 if (ATTR_IS_DEPENDENT (t
))
11478 *p
= TREE_CHAIN (t
);
11479 TREE_CHAIN (t
) = NULL_TREE
;
11480 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11482 q
= &TREE_CHAIN (*q
);
11485 p
= &TREE_CHAIN (t
);
11488 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11492 /* Perform (or defer) access check for typedefs that were referenced
11493 from within the template TMPL code.
11494 This is a subroutine of instantiate_decl and instantiate_class_template.
11495 TMPL is the template to consider and TARGS is the list of arguments of
11499 perform_typedefs_access_check (tree tmpl
, tree targs
)
11502 qualified_typedef_usage_t
*iter
;
11505 || (!CLASS_TYPE_P (tmpl
)
11506 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
11509 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
11511 tree type_decl
= iter
->typedef_decl
;
11512 tree type_scope
= iter
->context
;
11514 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
11517 if (uses_template_parms (type_decl
))
11518 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
11519 if (uses_template_parms (type_scope
))
11520 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11522 /* Make access check error messages point to the location
11523 of the use of the typedef. */
11524 iloc_sentinel
ils (iter
->locus
);
11525 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11526 type_decl
, type_decl
,
11527 tf_warning_or_error
);
11532 instantiate_class_template_1 (tree type
)
11534 tree templ
, args
, pattern
, t
, member
;
11538 unsigned int saved_maximum_field_alignment
;
11541 if (type
== error_mark_node
)
11542 return error_mark_node
;
11544 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11545 || uses_template_parms (type
))
11548 /* Figure out which template is being instantiated. */
11549 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11550 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11552 /* Mark the type as in the process of being defined. */
11553 TYPE_BEING_DEFINED (type
) = 1;
11555 /* We may be in the middle of deferred access check. Disable
11557 deferring_access_check_sentinel
acs (dk_no_deferred
);
11559 /* Determine what specialization of the original template to
11561 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11562 if (t
== error_mark_node
)
11563 return error_mark_node
;
11566 /* This TYPE is actually an instantiation of a partial
11567 specialization. We replace the innermost set of ARGS with
11568 the arguments appropriate for substitution. For example,
11571 template <class T> struct S {};
11572 template <class T> struct S<T*> {};
11574 and supposing that we are instantiating S<int*>, ARGS will
11575 presently be {int*} -- but we need {int}. */
11576 pattern
= TREE_TYPE (t
);
11577 args
= TREE_PURPOSE (t
);
11581 pattern
= TREE_TYPE (templ
);
11582 args
= CLASSTYPE_TI_ARGS (type
);
11585 /* If the template we're instantiating is incomplete, then clearly
11586 there's nothing we can do. */
11587 if (!COMPLETE_TYPE_P (pattern
))
11589 /* We can try again later. */
11590 TYPE_BEING_DEFINED (type
) = 0;
11594 /* If we've recursively instantiated too many templates, stop. */
11595 if (! push_tinst_level (type
))
11598 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11599 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11601 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11602 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11603 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11604 fn_context
= error_mark_node
;
11606 push_to_top_level ();
11609 cp_unevaluated_operand
= 0;
11610 c_inhibit_evaluation_warnings
= 0;
11612 /* Use #pragma pack from the template context. */
11613 saved_maximum_field_alignment
= maximum_field_alignment
;
11614 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11616 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11618 /* Set the input location to the most specialized template definition.
11619 This is needed if tsubsting causes an error. */
11620 typedecl
= TYPE_MAIN_DECL (pattern
);
11621 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11622 DECL_SOURCE_LOCATION (typedecl
);
11624 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11625 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11626 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11627 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11628 if (ANON_AGGR_TYPE_P (pattern
))
11629 SET_ANON_AGGR_TYPE_P (type
);
11630 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11632 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11633 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11634 /* Adjust visibility for template arguments. */
11635 determine_visibility (TYPE_MAIN_DECL (type
));
11637 if (CLASS_TYPE_P (type
))
11638 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11640 pbinfo
= TYPE_BINFO (pattern
);
11642 /* We should never instantiate a nested class before its enclosing
11643 class; we need to look up the nested class by name before we can
11644 instantiate it, and that lookup should instantiate the enclosing
11646 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11647 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11649 base_list
= NULL_TREE
;
11650 if (BINFO_N_BASE_BINFOS (pbinfo
))
11656 /* We must enter the scope containing the type, as that is where
11657 the accessibility of types named in dependent bases are
11659 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11661 /* Substitute into each of the bases to determine the actual
11663 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11666 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11667 tree expanded_bases
= NULL_TREE
;
11670 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11673 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11674 args
, tf_error
, NULL_TREE
);
11675 if (expanded_bases
== error_mark_node
)
11678 len
= TREE_VEC_LENGTH (expanded_bases
);
11681 for (idx
= 0; idx
< len
; idx
++)
11683 if (expanded_bases
)
11684 /* Extract the already-expanded base class. */
11685 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11687 /* Substitute to figure out the base class. */
11688 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11691 if (base
== error_mark_node
)
11694 base_list
= tree_cons (access
, base
, base_list
);
11695 if (BINFO_VIRTUAL_P (pbase_binfo
))
11696 TREE_TYPE (base_list
) = integer_type_node
;
11700 /* The list is now in reverse order; correct that. */
11701 base_list
= nreverse (base_list
);
11704 pop_scope (pushed_scope
);
11706 /* Now call xref_basetypes to set up all the base-class
11708 xref_basetypes (type
, base_list
);
11710 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11711 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11712 args
, tf_error
, NULL_TREE
);
11713 fixup_attribute_variants (type
);
11715 /* Now that our base classes are set up, enter the scope of the
11716 class, so that name lookups into base classes, etc. will work
11717 correctly. This is precisely analogous to what we do in
11718 begin_class_definition when defining an ordinary non-template
11719 class, except we also need to push the enclosing classes. */
11720 push_nested_class (type
);
11722 /* Now members are processed in the order of declaration. */
11723 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11724 member
; member
= TREE_CHAIN (member
))
11726 tree t
= TREE_VALUE (member
);
11728 if (TREE_PURPOSE (member
))
11732 if (LAMBDA_TYPE_P (t
))
11733 /* A closure type for a lambda in an NSDMI or default argument.
11734 Ignore it; it will be regenerated when needed. */
11737 /* Build new CLASSTYPE_NESTED_UTDS. */
11740 bool class_template_p
;
11742 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11743 && TYPE_LANG_SPECIFIC (t
)
11744 && CLASSTYPE_IS_TEMPLATE (t
));
11745 /* If the member is a class template, then -- even after
11746 substitution -- there may be dependent types in the
11747 template argument list for the class. We increment
11748 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11749 that function will assume that no types are dependent
11750 when outside of a template. */
11751 if (class_template_p
)
11752 ++processing_template_decl
;
11753 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11754 if (class_template_p
)
11755 --processing_template_decl
;
11756 if (newtag
== error_mark_node
)
11759 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11761 tree name
= TYPE_IDENTIFIER (t
);
11763 if (class_template_p
)
11764 /* Unfortunately, lookup_template_class sets
11765 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11766 instantiation (i.e., for the type of a member
11767 template class nested within a template class.)
11768 This behavior is required for
11769 maybe_process_partial_specialization to work
11770 correctly, but is not accurate in this case;
11771 the TAG is not an instantiation of anything.
11772 (The corresponding TEMPLATE_DECL is an
11773 instantiation, but the TYPE is not.) */
11774 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11776 /* Now, we call pushtag to put this NEWTAG into the scope of
11777 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11778 pushtag calling push_template_decl. We don't have to do
11779 this for enums because it will already have been done in
11782 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11783 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11786 else if (DECL_DECLARES_FUNCTION_P (t
))
11790 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11791 ++processing_template_decl
;
11792 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11793 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11794 --processing_template_decl
;
11795 set_current_access_from_decl (r
);
11796 finish_member_declaration (r
);
11797 /* Instantiate members marked with attribute used. */
11798 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11800 if (TREE_CODE (r
) == FUNCTION_DECL
11801 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11802 cp_check_omp_declare_reduction (r
);
11804 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11805 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11806 /* A closure type for a lambda in an NSDMI or default argument.
11807 Ignore it; it will be regenerated when needed. */;
11810 /* Build new TYPE_FIELDS. */
11811 if (TREE_CODE (t
) == STATIC_ASSERT
)
11812 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11813 /*integral_constant_expression_p=*/true);
11814 else if (TREE_CODE (t
) != CONST_DECL
)
11817 tree vec
= NULL_TREE
;
11820 /* The file and line for this declaration, to
11821 assist in error message reporting. Since we
11822 called push_tinst_level above, we don't need to
11824 input_location
= DECL_SOURCE_LOCATION (t
);
11826 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11827 ++processing_template_decl
;
11828 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11829 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11830 --processing_template_decl
;
11832 if (TREE_CODE (r
) == TREE_VEC
)
11834 /* A capture pack became multiple fields. */
11836 len
= TREE_VEC_LENGTH (vec
);
11839 for (int i
= 0; i
< len
; ++i
)
11842 r
= TREE_VEC_ELT (vec
, i
);
11847 [t]he initialization (and any associated
11848 side-effects) of a static data member does
11849 not occur unless the static data member is
11850 itself used in a way that requires the
11851 definition of the static data member to
11854 Therefore, we do not substitute into the
11855 initialized for the static data member here. */
11856 finish_static_data_member_decl
11858 /*init=*/NULL_TREE
,
11859 /*init_const_expr_p=*/false,
11860 /*asmspec_tree=*/NULL_TREE
,
11862 /* Instantiate members marked with attribute used. */
11863 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11866 else if (TREE_CODE (r
) == FIELD_DECL
)
11868 /* Determine whether R has a valid type and can be
11869 completed later. If R is invalid, then its type
11870 is replaced by error_mark_node. */
11871 tree rtype
= TREE_TYPE (r
);
11872 if (can_complete_type_without_circularity (rtype
))
11873 complete_type (rtype
);
11875 if (!complete_or_array_type_p (rtype
))
11877 /* If R's type couldn't be completed and
11878 it isn't a flexible array member (whose
11879 type is incomplete by definition) give
11881 cxx_incomplete_type_error (r
, rtype
);
11882 TREE_TYPE (r
) = error_mark_node
;
11884 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11885 && TYPE_DOMAIN (rtype
) == NULL_TREE
11886 && (TREE_CODE (type
) == UNION_TYPE
11887 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11889 error ("flexible array member %qD in union", r
);
11890 TREE_TYPE (r
) = error_mark_node
;
11892 else if (!verify_type_context (input_location
,
11893 TCTX_FIELD
, rtype
))
11894 TREE_TYPE (r
) = error_mark_node
;
11897 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11898 such a thing will already have been added to the field
11899 list by tsubst_enum in finish_member_declaration in the
11900 CLASSTYPE_NESTED_UTDS case above. */
11901 if (!(TREE_CODE (r
) == TYPE_DECL
11902 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11903 && DECL_ARTIFICIAL (r
)))
11905 set_current_access_from_decl (r
);
11906 finish_member_declaration (r
);
11914 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11915 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11917 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11919 tree friend_type
= t
;
11920 bool adjust_processing_template_decl
= false;
11922 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11924 /* template <class T> friend class C; */
11925 friend_type
= tsubst_friend_class (friend_type
, args
);
11926 adjust_processing_template_decl
= true;
11928 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11930 /* template <class T> friend class C::D; */
11931 friend_type
= tsubst (friend_type
, args
,
11932 tf_warning_or_error
, NULL_TREE
);
11933 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11934 friend_type
= TREE_TYPE (friend_type
);
11935 adjust_processing_template_decl
= true;
11937 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11938 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11940 /* This could be either
11944 when dependent_type_p is false or
11946 template <class U> friend class T::C;
11949 /* Bump processing_template_decl in case this is something like
11950 template <class T> friend struct A<T>::B. */
11951 ++processing_template_decl
;
11952 friend_type
= tsubst (friend_type
, args
,
11953 tf_warning_or_error
, NULL_TREE
);
11954 if (dependent_type_p (friend_type
))
11955 adjust_processing_template_decl
= true;
11956 --processing_template_decl
;
11958 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11959 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11960 && TYPE_HIDDEN_P (friend_type
))
11964 where C hasn't been declared yet. Let's lookup name
11965 from namespace scope directly, bypassing any name that
11966 come from dependent base class. */
11967 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11969 /* The call to xref_tag_from_type does injection for friend
11971 push_nested_namespace (ns
);
11973 xref_tag_from_type (friend_type
, NULL_TREE
,
11974 /*tag_scope=*/ts_current
);
11975 pop_nested_namespace (ns
);
11977 else if (uses_template_parms (friend_type
))
11978 /* friend class C<T>; */
11979 friend_type
= tsubst (friend_type
, args
,
11980 tf_warning_or_error
, NULL_TREE
);
11985 where C is already declared or
11987 friend class C<int>;
11989 We don't have to do anything in these cases. */
11991 if (adjust_processing_template_decl
)
11992 /* Trick make_friend_class into realizing that the friend
11993 we're adding is a template, not an ordinary class. It's
11994 important that we use make_friend_class since it will
11995 perform some error-checking and output cross-reference
11997 ++processing_template_decl
;
11999 if (friend_type
!= error_mark_node
)
12000 make_friend_class (type
, friend_type
, /*complain=*/false);
12002 if (adjust_processing_template_decl
)
12003 --processing_template_decl
;
12007 /* Build new DECL_FRIENDLIST. */
12010 /* The file and line for this declaration, to
12011 assist in error message reporting. Since we
12012 called push_tinst_level above, we don't need to
12014 input_location
= DECL_SOURCE_LOCATION (t
);
12016 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12018 ++processing_template_decl
;
12019 push_deferring_access_checks (dk_no_check
);
12022 r
= tsubst_friend_function (t
, args
);
12023 add_friend (type
, r
, /*complain=*/false);
12024 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12026 pop_deferring_access_checks ();
12027 --processing_template_decl
;
12035 /* Restore these before substituting into the lambda capture
12037 cp_unevaluated_operand
= saved_unevaluated_operand
;
12038 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12041 /* Set the file and line number information to whatever is given for
12042 the class itself. This puts error messages involving generated
12043 implicit functions at a predictable point, and the same point
12044 that would be used for non-template classes. */
12045 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12047 unreverse_member_declarations (type
);
12048 finish_struct_1 (type
);
12049 TYPE_BEING_DEFINED (type
) = 0;
12051 /* We don't instantiate default arguments for member functions. 14.7.1:
12053 The implicit instantiation of a class template specialization causes
12054 the implicit instantiation of the declarations, but not of the
12055 definitions or default arguments, of the class member functions,
12056 member classes, static data members and member templates.... */
12058 /* Some typedefs referenced from within the template code need to be access
12059 checked at template instantiation time, i.e now. These types were
12060 added to the template at parsing time. Let's get those and perform
12061 the access checks then. */
12062 perform_typedefs_access_check (pattern
, args
);
12063 perform_deferred_access_checks (tf_warning_or_error
);
12064 pop_nested_class ();
12065 maximum_field_alignment
= saved_maximum_field_alignment
;
12067 pop_from_top_level ();
12068 pop_tinst_level ();
12070 /* The vtable for a template class can be emitted in any translation
12071 unit in which the class is instantiated. When there is no key
12072 method, however, finish_struct_1 will already have added TYPE to
12073 the keyed_classes. */
12074 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12075 vec_safe_push (keyed_classes
, type
);
12080 /* Wrapper for instantiate_class_template_1. */
12083 instantiate_class_template (tree type
)
12086 timevar_push (TV_TEMPLATE_INST
);
12087 ret
= instantiate_class_template_1 (type
);
12088 timevar_pop (TV_TEMPLATE_INST
);
12093 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12099 else if (TYPE_P (t
))
12100 r
= tsubst (t
, args
, complain
, in_decl
);
12103 if (!(complain
& tf_warning
))
12104 ++c_inhibit_evaluation_warnings
;
12105 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12106 /*integral_constant_expression_p=*/true);
12107 if (!(complain
& tf_warning
))
12108 --c_inhibit_evaluation_warnings
;
12114 /* Given a function parameter pack TMPL_PARM and some function parameters
12115 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12116 and set *SPEC_P to point at the next point in the list. */
12119 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12121 /* Collect all of the extra "packed" parameters into an
12124 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12125 tree spec_parm
= *spec_p
;
12128 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12130 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12133 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12134 parmvec
= make_tree_vec (len
);
12135 spec_parm
= *spec_p
;
12136 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12138 tree elt
= spec_parm
;
12139 if (DECL_PACK_P (elt
))
12140 elt
= make_pack_expansion (elt
);
12141 TREE_VEC_ELT (parmvec
, i
) = elt
;
12144 /* Build the argument packs. */
12145 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12146 *spec_p
= spec_parm
;
12151 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12152 NONTYPE_ARGUMENT_PACK. */
12155 make_fnparm_pack (tree spec_parm
)
12157 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12160 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12161 pack expansion with no extra args, 2 if it has extra args, or 0
12162 if it is not a pack expansion. */
12165 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12167 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12168 /* We're being called before this happens in tsubst_pack_expansion. */
12169 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12170 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12171 if (i
>= TREE_VEC_LENGTH (vec
))
12173 tree elt
= TREE_VEC_ELT (vec
, i
);
12175 /* A decl pack is itself an expansion. */
12176 elt
= TREE_TYPE (elt
);
12177 if (!PACK_EXPANSION_P (elt
))
12179 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12185 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12188 make_argument_pack_select (tree arg_pack
, unsigned index
)
12190 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12192 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12193 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12198 /* This is a subroutine of tsubst_pack_expansion.
12200 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12201 mechanism to store the (non complete list of) arguments of the
12202 substitution and return a non substituted pack expansion, in order
12203 to wait for when we have enough arguments to really perform the
12207 use_pack_expansion_extra_args_p (tree parm_packs
,
12209 bool has_empty_arg
)
12211 /* If one pack has an expansion and another pack has a normal
12212 argument or if one pack has an empty argument and an another
12213 one hasn't then tsubst_pack_expansion cannot perform the
12214 substitution and need to fall back on the
12215 PACK_EXPANSION_EXTRA mechanism. */
12216 if (parm_packs
== NULL_TREE
)
12218 else if (has_empty_arg
)
12220 /* If all the actual packs are pack expansions, we can still
12221 subsitute directly. */
12222 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12224 tree a
= TREE_VALUE (p
);
12225 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12226 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12227 a
= ARGUMENT_PACK_ARGS (a
);
12228 if (TREE_VEC_LENGTH (a
) == 1)
12229 a
= TREE_VEC_ELT (a
, 0);
12230 if (PACK_EXPANSION_P (a
))
12237 bool has_expansion_arg
= false;
12238 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12240 bool has_non_expansion_arg
= false;
12241 for (tree parm_pack
= parm_packs
;
12243 parm_pack
= TREE_CHAIN (parm_pack
))
12245 tree arg
= TREE_VALUE (parm_pack
);
12247 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12249 /* We can't substitute a pack expansion with extra args into
12253 has_expansion_arg
= true;
12255 has_non_expansion_arg
= true;
12258 if (has_expansion_arg
&& has_non_expansion_arg
)
12264 /* [temp.variadic]/6 says that:
12266 The instantiation of a pack expansion [...]
12267 produces a list E1,E2, ..., En, where N is the number of elements
12268 in the pack expansion parameters.
12270 This subroutine of tsubst_pack_expansion produces one of these Ei.
12272 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12273 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12274 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12275 INDEX is the index 'i' of the element Ei to produce. ARGS,
12276 COMPLAIN, and IN_DECL are the same parameters as for the
12277 tsubst_pack_expansion function.
12279 The function returns the resulting Ei upon successful completion,
12280 or error_mark_node.
12282 Note that this function possibly modifies the ARGS parameter, so
12283 it's the responsibility of the caller to restore it. */
12286 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12289 tree args
/* This parm gets
12291 tsubst_flags_t complain
,
12295 bool ith_elem_is_expansion
= false;
12297 /* For each parameter pack, change the substitution of the parameter
12298 pack to the ith argument in its argument pack, then expand the
12300 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12302 tree parm
= TREE_PURPOSE (pack
);
12303 tree arg_pack
= TREE_VALUE (pack
);
12304 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12306 ith_elem_is_expansion
|=
12307 argument_pack_element_is_expansion_p (arg_pack
, index
);
12309 /* Select the Ith argument from the pack. */
12310 if (TREE_CODE (parm
) == PARM_DECL
12312 || TREE_CODE (parm
) == FIELD_DECL
)
12316 aps
= make_argument_pack_select (arg_pack
, index
);
12317 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12318 return error_mark_node
;
12319 register_local_specialization (aps
, parm
);
12322 aps
= retrieve_local_specialization (parm
);
12327 template_parm_level_and_index (parm
, &level
, &idx
);
12331 aps
= make_argument_pack_select (arg_pack
, index
);
12332 /* Update the corresponding argument. */
12333 TMPL_ARG (args
, level
, idx
) = aps
;
12336 /* Re-use the ARGUMENT_PACK_SELECT. */
12337 aps
= TMPL_ARG (args
, level
, idx
);
12339 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12342 /* Substitute into the PATTERN with the (possibly altered)
12344 if (pattern
== in_decl
)
12345 /* Expanding a fixed parameter pack from
12346 coerce_template_parameter_pack. */
12347 t
= tsubst_decl (pattern
, args
, complain
);
12348 else if (pattern
== error_mark_node
)
12349 t
= error_mark_node
;
12350 else if (!TYPE_P (pattern
))
12351 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12352 /*integral_constant_expression_p=*/false);
12354 t
= tsubst (pattern
, args
, complain
, in_decl
);
12356 /* If the Ith argument pack element is a pack expansion, then
12357 the Ith element resulting from the substituting is going to
12358 be a pack expansion as well. */
12359 if (ith_elem_is_expansion
)
12360 t
= make_pack_expansion (t
, complain
);
12365 /* When the unexpanded parameter pack in a fold expression expands to an empty
12366 sequence, the value of the expression is as follows; the program is
12367 ill-formed if the operator is not listed in this table.
12374 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12376 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12377 if (!FOLD_EXPR_MODIFY_P (t
))
12380 case TRUTH_ANDIF_EXPR
:
12381 return boolean_true_node
;
12382 case TRUTH_ORIF_EXPR
:
12383 return boolean_false_node
;
12384 case COMPOUND_EXPR
:
12390 if (complain
& tf_error
)
12391 error_at (location_of (t
),
12392 "fold of empty expansion over %O", code
);
12393 return error_mark_node
;
12396 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12397 form an expression that combines the two terms using the
12401 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12403 tree op
= FOLD_EXPR_OP (t
);
12404 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12406 // Handle compound assignment operators.
12407 if (FOLD_EXPR_MODIFY_P (t
))
12408 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12410 warning_sentinel
s(warn_parentheses
);
12413 case COMPOUND_EXPR
:
12414 return build_x_compound_expr (input_location
, left
, right
, complain
);
12416 return build_x_binary_op (input_location
, code
,
12417 left
, TREE_CODE (left
),
12418 right
, TREE_CODE (right
),
12424 /* Substitute ARGS into the pack of a fold expression T. */
12427 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12429 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12432 /* Substitute ARGS into the pack of a fold expression T. */
12435 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12437 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12440 /* Expand a PACK of arguments into a grouped as left fold.
12441 Given a pack containing elements A0, A1, ..., An and an
12442 operator @, this builds the expression:
12444 ((A0 @ A1) @ A2) ... @ An
12446 Note that PACK must not be empty.
12448 The operator is defined by the original fold expression T. */
12451 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12453 tree left
= TREE_VEC_ELT (pack
, 0);
12454 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12456 tree right
= TREE_VEC_ELT (pack
, i
);
12457 left
= fold_expression (t
, left
, right
, complain
);
12462 /* Substitute into a unary left fold expression. */
12465 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12468 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12469 if (pack
== error_mark_node
)
12470 return error_mark_node
;
12471 if (PACK_EXPANSION_P (pack
))
12473 tree r
= copy_node (t
);
12474 FOLD_EXPR_PACK (r
) = pack
;
12477 if (TREE_VEC_LENGTH (pack
) == 0)
12478 return expand_empty_fold (t
, complain
);
12480 return expand_left_fold (t
, pack
, complain
);
12483 /* Substitute into a binary left fold expression.
12485 Do ths by building a single (non-empty) vector of argumnts and
12486 building the expression from those elements. */
12489 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12492 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12493 if (pack
== error_mark_node
)
12494 return error_mark_node
;
12495 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12496 if (init
== error_mark_node
)
12497 return error_mark_node
;
12499 if (PACK_EXPANSION_P (pack
))
12501 tree r
= copy_node (t
);
12502 FOLD_EXPR_PACK (r
) = pack
;
12503 FOLD_EXPR_INIT (r
) = init
;
12507 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12508 TREE_VEC_ELT (vec
, 0) = init
;
12509 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12510 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12512 return expand_left_fold (t
, vec
, complain
);
12515 /* Expand a PACK of arguments into a grouped as right fold.
12516 Given a pack containing elementns A0, A1, ..., and an
12517 operator @, this builds the expression:
12519 A0@ ... (An-2 @ (An-1 @ An))
12521 Note that PACK must not be empty.
12523 The operator is defined by the original fold expression T. */
12526 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12528 // Build the expression.
12529 int n
= TREE_VEC_LENGTH (pack
);
12530 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12531 for (--n
; n
!= 0; --n
)
12533 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12534 right
= fold_expression (t
, left
, right
, complain
);
12539 /* Substitute into a unary right fold expression. */
12542 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12545 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12546 if (pack
== error_mark_node
)
12547 return error_mark_node
;
12548 if (PACK_EXPANSION_P (pack
))
12550 tree r
= copy_node (t
);
12551 FOLD_EXPR_PACK (r
) = pack
;
12554 if (TREE_VEC_LENGTH (pack
) == 0)
12555 return expand_empty_fold (t
, complain
);
12557 return expand_right_fold (t
, pack
, complain
);
12560 /* Substitute into a binary right fold expression.
12562 Do ths by building a single (non-empty) vector of arguments and
12563 building the expression from those elements. */
12566 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12569 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12570 if (pack
== error_mark_node
)
12571 return error_mark_node
;
12572 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12573 if (init
== error_mark_node
)
12574 return error_mark_node
;
12576 if (PACK_EXPANSION_P (pack
))
12578 tree r
= copy_node (t
);
12579 FOLD_EXPR_PACK (r
) = pack
;
12580 FOLD_EXPR_INIT (r
) = init
;
12584 int n
= TREE_VEC_LENGTH (pack
);
12585 tree vec
= make_tree_vec (n
+ 1);
12586 for (int i
= 0; i
< n
; ++i
)
12587 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12588 TREE_VEC_ELT (vec
, n
) = init
;
12590 return expand_right_fold (t
, vec
, complain
);
12593 /* Walk through the pattern of a pack expansion, adding everything in
12594 local_specializations to a list. */
12599 hash_set
<tree
> internal
;
12601 tsubst_flags_t complain
;
12603 el_data (tsubst_flags_t c
)
12604 : extra (NULL_TREE
), complain (c
) {}
12607 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12609 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12610 tree
*extra
= &data
.extra
;
12611 tsubst_flags_t complain
= data
.complain
;
12613 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12614 /* Remember local typedefs (85214). */
12615 tp
= &TYPE_NAME (*tp
);
12617 if (TREE_CODE (*tp
) == DECL_EXPR
)
12618 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12619 else if (tree spec
= retrieve_local_specialization (*tp
))
12621 if (data
.internal
.contains (*tp
))
12622 /* Don't mess with variables declared within the pattern. */
12624 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12626 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12627 tree args
= ARGUMENT_PACK_ARGS (spec
);
12628 if (TREE_VEC_LENGTH (args
) == 1)
12630 tree elt
= TREE_VEC_ELT (args
, 0);
12631 if (PACK_EXPANSION_P (elt
))
12632 elt
= PACK_EXPANSION_PATTERN (elt
);
12633 if (DECL_PACK_P (elt
))
12636 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12638 /* Handle lambda capture here, since we aren't doing any
12639 substitution now, and so tsubst_copy won't call
12640 process_outer_var_ref. */
12641 tree args
= ARGUMENT_PACK_ARGS (spec
);
12642 int len
= TREE_VEC_LENGTH (args
);
12643 for (int i
= 0; i
< len
; ++i
)
12645 tree arg
= TREE_VEC_ELT (args
, i
);
12647 if (outer_automatic_var_p (arg
))
12648 carg
= process_outer_var_ref (arg
, complain
);
12651 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12655 spec
= copy_node (spec
);
12656 args
= copy_node (args
);
12657 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12658 register_local_specialization (spec
, *tp
);
12660 TREE_VEC_ELT (args
, i
) = carg
;
12665 if (outer_automatic_var_p (spec
))
12666 spec
= process_outer_var_ref (spec
, complain
);
12667 *extra
= tree_cons (*tp
, spec
, *extra
);
12672 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12674 el_data
data (complain
);
12675 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12679 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12680 for use in PACK_EXPANSION_EXTRA_ARGS. */
12683 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12686 if (local_specializations
)
12687 if (tree locals
= extract_local_specs (pattern
, complain
))
12688 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12692 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12693 normal template args to ARGS. */
12696 add_extra_args (tree extra
, tree args
)
12698 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12700 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12702 /* The partial instantiation involved local declarations collected in
12703 extract_local_specs; map from the general template to our local
12705 tree gen
= TREE_PURPOSE (elt
);
12706 tree inst
= TREE_VALUE (elt
);
12708 if (tree local
= retrieve_local_specialization (inst
))
12710 /* else inst is already a full instantiation of the pack. */
12711 register_local_specialization (inst
, gen
);
12713 gcc_assert (!TREE_PURPOSE (extra
));
12714 extra
= TREE_VALUE (extra
);
12717 /* I think we should always be able to substitute dependent args into the
12718 pattern. If that turns out to be incorrect in some cases, enable the
12719 alternate code (and add complain/in_decl parms to this function). */
12720 gcc_checking_assert (!uses_template_parms (extra
));
12722 if (!uses_template_parms (extra
))
12724 gcc_unreachable ();
12725 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12726 args
= add_outermost_template_args (args
, extra
);
12730 args
= add_to_template_args (extra
, args
);
12734 /* Substitute ARGS into T, which is an pack expansion
12735 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12736 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12737 (if only a partial substitution could be performed) or
12738 ERROR_MARK_NODE if there was an error. */
12740 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12744 tree pack
, packs
= NULL_TREE
;
12745 bool unsubstituted_packs
= false;
12748 bool need_local_specializations
= false;
12751 gcc_assert (PACK_EXPANSION_P (t
));
12752 pattern
= PACK_EXPANSION_PATTERN (t
);
12754 /* Add in any args remembered from an earlier partial instantiation. */
12755 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12757 levels
= TMPL_ARGS_DEPTH (args
);
12759 /* Determine the argument packs that will instantiate the parameter
12760 packs used in the expansion expression. While we're at it,
12761 compute the number of arguments to be expanded and make sure it
12763 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12764 pack
= TREE_CHAIN (pack
))
12766 tree parm_pack
= TREE_VALUE (pack
);
12767 tree arg_pack
= NULL_TREE
;
12768 tree orig_arg
= NULL_TREE
;
12771 if (TREE_CODE (parm_pack
) == BASES
)
12773 gcc_assert (parm_pack
== pattern
);
12774 if (BASES_DIRECT (parm_pack
))
12775 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12780 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12781 args
, complain
, in_decl
,
12784 else if (builtin_pack_call_p (parm_pack
))
12786 if (parm_pack
!= pattern
)
12788 if (complain
& tf_error
)
12789 sorry ("%qE is not the entire pattern of the pack expansion",
12791 return error_mark_node
;
12793 return expand_builtin_pack_call (parm_pack
, args
,
12794 complain
, in_decl
);
12796 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12798 /* We know we have correct local_specializations if this
12799 expansion is at function scope, or if we're dealing with a
12800 local parameter in a requires expression; for the latter,
12801 tsubst_requires_expr set it up appropriately. */
12802 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12803 arg_pack
= retrieve_local_specialization (parm_pack
);
12805 /* We can't rely on local_specializations for a parameter
12806 name used later in a function declaration (such as in a
12807 late-specified return type). Even if it exists, it might
12808 have the wrong value for a recursive call. */
12809 need_local_specializations
= true;
12813 /* This parameter pack was used in an unevaluated context. Just
12814 make a dummy decl, since it's only used for its type. */
12815 ++cp_unevaluated_operand
;
12816 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12817 --cp_unevaluated_operand
;
12818 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12819 /* Partial instantiation of the parm_pack, we can't build
12820 up an argument pack yet. */
12821 arg_pack
= NULL_TREE
;
12823 arg_pack
= make_fnparm_pack (arg_pack
);
12825 else if (DECL_PACK_P (arg_pack
))
12826 /* This argument pack isn't fully instantiated yet. */
12827 arg_pack
= NULL_TREE
;
12829 else if (is_capture_proxy (parm_pack
))
12831 arg_pack
= retrieve_local_specialization (parm_pack
);
12832 if (DECL_PACK_P (arg_pack
))
12833 arg_pack
= NULL_TREE
;
12838 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12839 if (level
<= levels
)
12840 arg_pack
= TMPL_ARG (args
, level
, idx
);
12843 orig_arg
= arg_pack
;
12844 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12845 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12847 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12848 /* This can only happen if we forget to expand an argument
12849 pack somewhere else. Just return an error, silently. */
12851 result
= make_tree_vec (1);
12852 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12859 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12861 /* Don't bother trying to do a partial substitution with
12862 incomplete packs; we'll try again after deduction. */
12863 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12868 else if (len
!= my_len
)
12870 if (!(complain
& tf_error
))
12871 /* Fail quietly. */;
12872 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12873 error ("mismatched argument pack lengths while expanding %qT",
12876 error ("mismatched argument pack lengths while expanding %qE",
12878 return error_mark_node
;
12881 /* Keep track of the parameter packs and their corresponding
12883 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12884 TREE_TYPE (packs
) = orig_arg
;
12888 /* We can't substitute for this parameter pack. We use a flag as
12889 well as the missing_level counter because function parameter
12890 packs don't have a level. */
12891 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12892 unsubstituted_packs
= true;
12896 /* If the expansion is just T..., return the matching argument pack, unless
12897 we need to call convert_from_reference on all the elements. This is an
12898 important optimization; see c++/68422. */
12899 if (!unsubstituted_packs
12900 && TREE_PURPOSE (packs
) == pattern
)
12902 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12904 /* If the argument pack is a single pack expansion, pull it out. */
12905 if (TREE_VEC_LENGTH (args
) == 1
12906 && pack_expansion_args_count (args
))
12907 return TREE_VEC_ELT (args
, 0);
12909 /* Types need no adjustment, nor does sizeof..., and if we still have
12910 some pack expansion args we won't do anything yet. */
12911 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12912 || PACK_EXPANSION_SIZEOF_P (t
)
12913 || pack_expansion_args_count (args
))
12915 /* Also optimize expression pack expansions if we can tell that the
12916 elements won't have reference type. */
12917 tree type
= TREE_TYPE (pattern
);
12918 if (type
&& !TYPE_REF_P (type
)
12919 && !PACK_EXPANSION_P (type
)
12920 && !WILDCARD_TYPE_P (type
))
12922 /* Otherwise use the normal path so we get convert_from_reference. */
12925 /* We cannot expand this expansion expression, because we don't have
12926 all of the argument packs we need. */
12927 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12929 /* We got some full packs, but we can't substitute them in until we
12930 have values for all the packs. So remember these until then. */
12932 t
= make_pack_expansion (pattern
, complain
);
12933 PACK_EXPANSION_EXTRA_ARGS (t
)
12934 = build_extra_args (pattern
, args
, complain
);
12938 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12939 type, so create our own local specializations map; the current map is
12940 either NULL or (in the case of recursive unification) might have
12941 bindings that we don't want to use or alter. */
12942 local_specialization_stack
lss (need_local_specializations
12943 ? lss_blank
: lss_nop
);
12945 if (unsubstituted_packs
)
12947 /* There were no real arguments, we're just replacing a parameter
12948 pack with another version of itself. Substitute into the
12949 pattern and return a PACK_EXPANSION_*. The caller will need to
12951 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12952 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12953 /*integral_constant_expression_p=*/false);
12955 t
= tsubst (pattern
, args
, complain
, in_decl
);
12956 t
= make_pack_expansion (t
, complain
);
12960 gcc_assert (len
>= 0);
12962 /* For each argument in each argument pack, substitute into the
12964 result
= make_tree_vec (len
);
12965 tree elem_args
= copy_template_args (args
);
12966 for (i
= 0; i
< len
; ++i
)
12968 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12970 elem_args
, complain
,
12972 TREE_VEC_ELT (result
, i
) = t
;
12973 if (t
== error_mark_node
)
12975 result
= error_mark_node
;
12980 /* Update ARGS to restore the substitution from parameter packs to
12981 their argument packs. */
12982 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12984 tree parm
= TREE_PURPOSE (pack
);
12986 if (TREE_CODE (parm
) == PARM_DECL
12988 || TREE_CODE (parm
) == FIELD_DECL
)
12989 register_local_specialization (TREE_TYPE (pack
), parm
);
12994 if (TREE_VALUE (pack
) == NULL_TREE
)
12997 template_parm_level_and_index (parm
, &level
, &idx
);
12999 /* Update the corresponding argument. */
13000 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13001 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13004 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13008 /* If the dependent pack arguments were such that we end up with only a
13009 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13010 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13011 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13012 return TREE_VEC_ELT (result
, 0);
13017 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13018 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13019 parameter packs; all parms generated from a function parameter pack will
13020 have the same DECL_PARM_INDEX. */
13023 get_pattern_parm (tree parm
, tree tmpl
)
13025 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13028 if (DECL_ARTIFICIAL (parm
))
13030 for (patparm
= DECL_ARGUMENTS (pattern
);
13031 patparm
; patparm
= DECL_CHAIN (patparm
))
13032 if (DECL_ARTIFICIAL (patparm
)
13033 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13038 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13039 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13040 gcc_assert (DECL_PARM_INDEX (patparm
)
13041 == DECL_PARM_INDEX (parm
));
13047 /* Make an argument pack out of the TREE_VEC VEC. */
13050 make_argument_pack (tree vec
)
13053 tree elt
= TREE_VEC_ELT (vec
, 0);
13055 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13058 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13059 TREE_CONSTANT (pack
) = 1;
13061 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13065 /* Return an exact copy of template args T that can be modified
13069 copy_template_args (tree t
)
13071 if (t
== error_mark_node
)
13074 int len
= TREE_VEC_LENGTH (t
);
13075 tree new_vec
= make_tree_vec (len
);
13077 for (int i
= 0; i
< len
; ++i
)
13079 tree elt
= TREE_VEC_ELT (t
, i
);
13080 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13081 elt
= copy_template_args (elt
);
13082 TREE_VEC_ELT (new_vec
, i
) = elt
;
13085 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13086 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13091 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13094 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13097 /* Substitute into each of the arguments. */
13098 tree new_arg
= TYPE_P (orig_arg
)
13099 ? cxx_make_type (TREE_CODE (orig_arg
))
13100 : make_node (TREE_CODE (orig_arg
));
13102 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13103 args
, complain
, in_decl
);
13104 if (pack_args
== error_mark_node
)
13105 new_arg
= error_mark_node
;
13107 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13109 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13110 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13115 /* Substitute ARGS into the vector or list of template arguments T. */
13118 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13121 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13124 if (t
== error_mark_node
)
13125 return error_mark_node
;
13127 len
= TREE_VEC_LENGTH (t
);
13128 elts
= XALLOCAVEC (tree
, len
);
13130 for (i
= 0; i
< len
; i
++)
13132 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13135 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13136 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13137 else if (PACK_EXPANSION_P (orig_arg
))
13139 /* Substitute into an expansion expression. */
13140 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13142 if (TREE_CODE (new_arg
) == TREE_VEC
)
13143 /* Add to the expanded length adjustment the number of
13144 expanded arguments. We subtract one from this
13145 measurement, because the argument pack expression
13146 itself is already counted as 1 in
13147 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13148 the argument pack is empty. */
13149 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13151 else if (ARGUMENT_PACK_P (orig_arg
))
13152 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13154 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13156 if (new_arg
== error_mark_node
)
13157 return error_mark_node
;
13160 if (new_arg
!= orig_arg
)
13167 /* Make space for the expanded arguments coming from template
13169 t
= make_tree_vec (len
+ expanded_len_adjust
);
13170 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13171 arguments for a member template.
13172 In that case each TREE_VEC in ORIG_T represents a level of template
13173 arguments, and ORIG_T won't carry any non defaulted argument count.
13174 It will rather be the nested TREE_VECs that will carry one.
13175 In other words, ORIG_T carries a non defaulted argument count only
13176 if it doesn't contain any nested TREE_VEC. */
13177 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13179 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13180 count
+= expanded_len_adjust
;
13181 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13183 for (i
= 0, out
= 0; i
< len
; i
++)
13185 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13186 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13187 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13191 /* Now expand the template argument pack "in place". */
13192 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13193 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13197 TREE_VEC_ELT (t
, out
) = elts
[i
];
13205 /* Substitute ARGS into one level PARMS of template parameters. */
13208 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13210 if (parms
== error_mark_node
)
13211 return error_mark_node
;
13213 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13215 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13217 tree tuple
= TREE_VEC_ELT (parms
, i
);
13219 if (tuple
== error_mark_node
)
13222 TREE_VEC_ELT (new_vec
, i
) =
13223 tsubst_template_parm (tuple
, args
, complain
);
13229 /* Return the result of substituting ARGS into the template parameters
13230 given by PARMS. If there are m levels of ARGS and m + n levels of
13231 PARMS, then the result will contain n levels of PARMS. For
13232 example, if PARMS is `template <class T> template <class U>
13233 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13234 result will be `template <int*, double, class V>'. */
13237 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13239 tree r
= NULL_TREE
;
13242 /* When substituting into a template, we must set
13243 PROCESSING_TEMPLATE_DECL as the template parameters may be
13244 dependent if they are based on one-another, and the dependency
13245 predicates are short-circuit outside of templates. */
13246 ++processing_template_decl
;
13248 for (new_parms
= &r
;
13249 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13250 new_parms
= &(TREE_CHAIN (*new_parms
)),
13251 parms
= TREE_CHAIN (parms
))
13253 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13256 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13257 - TMPL_ARGS_DEPTH (args
)),
13258 new_vec
, NULL_TREE
);
13259 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13260 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13263 --processing_template_decl
;
13268 /* Return the result of substituting ARGS into one template parameter
13269 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13270 parameter and which TREE_PURPOSE is the default argument of the
13271 template parameter. */
13274 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13276 tree default_value
, parm_decl
;
13278 if (args
== NULL_TREE
13280 || t
== error_mark_node
)
13283 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13285 default_value
= TREE_PURPOSE (t
);
13286 parm_decl
= TREE_VALUE (t
);
13287 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13289 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13290 if (TREE_CODE (parm_decl
) == PARM_DECL
13291 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13292 parm_decl
= error_mark_node
;
13293 default_value
= tsubst_template_arg (default_value
, args
,
13294 complain
, NULL_TREE
);
13295 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13297 tree r
= build_tree_list (default_value
, parm_decl
);
13298 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13302 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13303 type T. If T is not an aggregate or enumeration type, it is
13304 handled as if by tsubst. IN_DECL is as for tsubst. If
13305 ENTERING_SCOPE is nonzero, T is the context for a template which
13306 we are presently tsubst'ing. Return the substituted value. */
13309 tsubst_aggr_type (tree t
,
13311 tsubst_flags_t complain
,
13313 int entering_scope
)
13315 if (t
== NULL_TREE
)
13318 switch (TREE_CODE (t
))
13321 if (TYPE_PTRMEMFUNC_P (t
))
13322 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13324 /* Fall through. */
13325 case ENUMERAL_TYPE
:
13327 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13333 /* In "sizeof(X<I>)" we need to evaluate "I". */
13336 /* First, determine the context for the type we are looking
13338 context
= TYPE_CONTEXT (t
);
13339 if (context
&& TYPE_P (context
))
13341 context
= tsubst_aggr_type (context
, args
, complain
,
13342 in_decl
, /*entering_scope=*/1);
13343 /* If context is a nested class inside a class template,
13344 it may still need to be instantiated (c++/33959). */
13345 context
= complete_type (context
);
13348 /* Then, figure out what arguments are appropriate for the
13349 type we are trying to find. For example, given:
13351 template <class T> struct S;
13352 template <class T, class U> void f(T, U) { S<U> su; }
13354 and supposing that we are instantiating f<int, double>,
13355 then our ARGS will be {int, double}, but, when looking up
13356 S we only want {double}. */
13357 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13358 complain
, in_decl
);
13359 if (argvec
== error_mark_node
)
13360 r
= error_mark_node
;
13363 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13364 entering_scope
, complain
);
13365 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13371 /* This is not a template type, so there's nothing to do. */
13375 return tsubst (t
, args
, complain
, in_decl
);
13379 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13381 /* Substitute into the default argument ARG (a default argument for
13382 FN), which has the indicated TYPE. */
13385 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13386 tsubst_flags_t complain
)
13388 int errs
= errorcount
+ sorrycount
;
13390 /* This can happen in invalid code. */
13391 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13394 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13395 parm
= chain_index (parmnum
, parm
);
13396 tree parmtype
= TREE_TYPE (parm
);
13397 if (DECL_BY_REFERENCE (parm
))
13398 parmtype
= TREE_TYPE (parmtype
);
13399 if (parmtype
== error_mark_node
)
13400 return error_mark_node
;
13402 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13405 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13408 /* This default argument came from a template. Instantiate the
13409 default argument here, not in tsubst. In the case of
13418 we must be careful to do name lookup in the scope of S<T>,
13419 rather than in the current class. */
13420 push_to_top_level ();
13421 push_access_scope (fn
);
13422 push_deferring_access_checks (dk_no_deferred
);
13423 start_lambda_scope (parm
);
13425 /* The default argument expression may cause implicitly defined
13426 member functions to be synthesized, which will result in garbage
13427 collection. We must treat this situation as if we were within
13428 the body of function so as to avoid collecting live data on the
13431 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13432 complain
, NULL_TREE
,
13433 /*integral_constant_expression_p=*/false);
13436 finish_lambda_scope ();
13438 /* Make sure the default argument is reasonable. */
13439 arg
= check_default_argument (type
, arg
, complain
);
13441 if (errorcount
+sorrycount
> errs
13442 && (complain
& tf_warning_or_error
))
13443 inform (input_location
,
13444 " when instantiating default argument for call to %qD", fn
);
13446 pop_deferring_access_checks ();
13447 pop_access_scope (fn
);
13448 pop_from_top_level ();
13450 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13453 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13454 defarg_inst
->put (parm
, arg
);
13460 /* Substitute into all the default arguments for FN. */
13463 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13468 tmpl_args
= DECL_TI_ARGS (fn
);
13470 /* If this function is not yet instantiated, we certainly don't need
13471 its default arguments. */
13472 if (uses_template_parms (tmpl_args
))
13474 /* Don't do this again for clones. */
13475 if (DECL_CLONED_FUNCTION_P (fn
))
13479 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13481 arg
= TREE_CHAIN (arg
), ++i
)
13482 if (TREE_PURPOSE (arg
))
13483 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13485 TREE_PURPOSE (arg
),
13489 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13490 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13492 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13495 store_explicit_specifier (tree v
, tree t
)
13497 if (!explicit_specifier_map
)
13498 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13499 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13500 explicit_specifier_map
->put (v
, t
);
13503 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13506 lookup_explicit_specifier (tree v
)
13508 return *explicit_specifier_map
->get (v
);
13511 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13512 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13513 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13517 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13518 tree raises
, tsubst_flags_t complain
)
13520 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13523 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13525 new_type
= build_function_type (return_type
, arg_types
);
13526 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13530 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13531 /* Don't pick up extra function qualifiers from the basetype. */
13532 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13533 if (! MAYBE_CLASS_TYPE_P (r
))
13537 Type deduction may fail for any of the following
13540 -- Attempting to create "pointer to member of T" when T
13541 is not a class type. */
13542 if (complain
& tf_error
)
13543 error ("creating pointer to member function of non-class type %qT",
13545 return error_mark_node
;
13548 new_type
= build_method_type_directly (r
, return_type
,
13549 TREE_CHAIN (arg_types
));
13551 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13553 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13554 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13555 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13558 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13559 each of its formal parameters. If there is a disagreement then rebuild
13560 DECL's function type according to its formal parameter types, as part of a
13561 resolution for Core issues 1001/1322. */
13564 maybe_rebuild_function_decl_type (tree decl
)
13566 bool function_type_needs_rebuilding
= false;
13567 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13569 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13570 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13572 tree parm_type
= TREE_VALUE (parm_type_list
);
13573 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13574 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13576 function_type_needs_rebuilding
= true;
13580 parm_list
= DECL_CHAIN (parm_list
);
13581 parm_type_list
= TREE_CHAIN (parm_type_list
);
13585 if (!function_type_needs_rebuilding
)
13588 const tree fntype
= TREE_TYPE (decl
);
13589 tree parm_list
= DECL_ARGUMENTS (decl
);
13590 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13591 tree new_parm_type_list
= NULL_TREE
;
13592 tree
*q
= &new_parm_type_list
;
13593 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13595 *q
= copy_node (old_parm_type_list
);
13596 parm_list
= DECL_CHAIN (parm_list
);
13597 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13598 q
= &TREE_CHAIN (*q
);
13600 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13602 *q
= copy_node (old_parm_type_list
);
13603 tree
*new_parm_type
= &TREE_VALUE (*q
);
13604 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13605 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13606 *new_parm_type
= formal_parm_type_unqual
;
13608 parm_list
= DECL_CHAIN (parm_list
);
13609 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13610 q
= &TREE_CHAIN (*q
);
13612 if (old_parm_type_list
== void_list_node
)
13613 *q
= void_list_node
;
13616 = rebuild_function_or_method_type (fntype
,
13617 TREE_TYPE (fntype
), new_parm_type_list
,
13618 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13621 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13624 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13625 tree lambda_fntype
)
13627 tree gen_tmpl
, argvec
;
13628 hashval_t hash
= 0;
13631 /* Nobody should be tsubst'ing into non-template functions. */
13632 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13634 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13636 /* If T is not dependent, just return it. */
13637 if (!uses_template_parms (DECL_TI_ARGS (t
))
13638 && !LAMBDA_FUNCTION_P (t
))
13641 /* Calculate the most general template of which R is a
13643 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13645 /* We're substituting a lambda function under tsubst_lambda_expr but not
13646 directly from it; find the matching function we're already inside.
13647 But don't do this if T is a generic lambda with a single level of
13648 template parms, as in that case we're doing a normal instantiation. */
13649 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13650 && (!generic_lambda_fn_p (t
)
13651 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13652 return enclosing_instantiation_of (t
);
13654 /* Calculate the complete set of arguments used to
13656 argvec
= tsubst_template_args (DECL_TI_ARGS
13657 (DECL_TEMPLATE_RESULT
13658 (DECL_TI_TEMPLATE (t
))),
13659 args
, complain
, in_decl
);
13660 if (argvec
== error_mark_node
)
13661 return error_mark_node
;
13663 /* Check to see if we already have this specialization. */
13664 if (!lambda_fntype
)
13666 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13667 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13671 /* We can see more levels of arguments than parameters if
13672 there was a specialization of a member template, like
13675 template <class T> struct S { template <class U> void f(); }
13676 template <> template <class U> void S<int>::f(U);
13678 Here, we'll be substituting into the specialization,
13679 because that's where we can find the code we actually
13680 want to generate, but we'll have enough arguments for
13681 the most general template.
13683 We also deal with the peculiar case:
13685 template <class T> struct S {
13686 template <class U> friend void f();
13688 template <class U> void f() {}
13690 template void f<double>();
13692 Here, the ARGS for the instantiation of will be {int,
13693 double}. But, we only need as many ARGS as there are
13694 levels of template parameters in CODE_PATTERN. We are
13695 careful not to get fooled into reducing the ARGS in
13698 template <class T> struct S { template <class U> void f(U); }
13699 template <class T> template <> void S<T>::f(int) {}
13701 which we can spot because the pattern will be a
13702 specialization in this case. */
13703 int args_depth
= TMPL_ARGS_DEPTH (args
);
13705 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13707 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13708 args
= get_innermost_template_args (args
, parms_depth
);
13712 /* This special case arises when we have something like this:
13714 template <class T> struct S {
13715 friend void f<int>(int, double);
13718 Here, the DECL_TI_TEMPLATE for the friend declaration
13719 will be an IDENTIFIER_NODE. We are being called from
13720 tsubst_friend_function, and we want only to create a
13721 new decl (R) with appropriate types so that we can call
13722 determine_specialization. */
13723 gen_tmpl
= NULL_TREE
;
13724 argvec
= NULL_TREE
;
13727 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13729 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13730 bool member
= ctx
&& TYPE_P (ctx
);
13732 if (member
&& !closure
)
13733 ctx
= tsubst_aggr_type (ctx
, args
,
13734 complain
, t
, /*entering_scope=*/1);
13736 tree type
= (lambda_fntype
? lambda_fntype
13737 : tsubst (TREE_TYPE (t
), args
,
13738 complain
| tf_fndecl_type
, in_decl
));
13739 if (type
== error_mark_node
)
13740 return error_mark_node
;
13742 /* If we hit excessive deduction depth, the type is bogus even if
13743 it isn't error_mark_node, so don't build a decl. */
13744 if (excessive_deduction_depth
)
13745 return error_mark_node
;
13747 /* We do NOT check for matching decls pushed separately at this
13748 point, as they may not represent instantiations of this
13749 template, and in any case are considered separate under the
13751 tree r
= copy_decl (t
);
13752 DECL_USE_TEMPLATE (r
) = 0;
13753 TREE_TYPE (r
) = type
;
13754 /* Clear out the mangled name and RTL for the instantiation. */
13755 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13756 SET_DECL_RTL (r
, NULL
);
13757 /* Leave DECL_INITIAL set on deleted instantiations. */
13758 if (!DECL_DELETED_FN (r
))
13759 DECL_INITIAL (r
) = NULL_TREE
;
13760 DECL_CONTEXT (r
) = ctx
;
13762 /* Handle explicit(dependent-expr). */
13763 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13765 tree spec
= lookup_explicit_specifier (t
);
13766 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13767 /*function_p=*/false,
13769 spec
= build_explicit_specifier (spec
, complain
);
13770 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13773 /* OpenMP UDRs have the only argument a reference to the declared
13774 type. We want to diagnose if the declared type is a reference,
13775 which is invalid, but as references to references are usually
13776 quietly merged, diagnose it here. */
13777 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13780 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13781 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13782 if (TYPE_REF_P (argtype
))
13783 error_at (DECL_SOURCE_LOCATION (t
),
13784 "reference type %qT in "
13785 "%<#pragma omp declare reduction%>", argtype
);
13786 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13787 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13791 if (member
&& DECL_CONV_FN_P (r
))
13792 /* Type-conversion operator. Reconstruct the name, in
13793 case it's the name of one of the template's parameters. */
13794 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13796 tree parms
= DECL_ARGUMENTS (t
);
13798 parms
= DECL_CHAIN (parms
);
13799 parms
= tsubst (parms
, args
, complain
, t
);
13800 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13801 DECL_CONTEXT (parm
) = r
;
13804 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13805 DECL_NAME (tparm
) = closure_identifier
;
13806 DECL_CHAIN (tparm
) = parms
;
13809 DECL_ARGUMENTS (r
) = parms
;
13810 DECL_RESULT (r
) = NULL_TREE
;
13812 maybe_rebuild_function_decl_type (r
);
13814 TREE_STATIC (r
) = 0;
13815 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13816 DECL_EXTERNAL (r
) = 1;
13817 /* If this is an instantiation of a function with internal
13818 linkage, we already know what object file linkage will be
13819 assigned to the instantiation. */
13820 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13821 DECL_DEFER_OUTPUT (r
) = 0;
13822 DECL_CHAIN (r
) = NULL_TREE
;
13823 DECL_PENDING_INLINE_INFO (r
) = 0;
13824 DECL_PENDING_INLINE_P (r
) = 0;
13825 DECL_SAVED_TREE (r
) = NULL_TREE
;
13826 DECL_STRUCT_FUNCTION (r
) = NULL
;
13828 /* We'll re-clone as appropriate in instantiate_template. */
13829 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13831 /* If we aren't complaining now, return on error before we register
13832 the specialization so that we'll complain eventually. */
13833 if ((complain
& tf_error
) == 0
13834 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13835 && !grok_op_properties (r
, /*complain=*/false))
13836 return error_mark_node
;
13838 /* Associate the constraints directly with the instantiation. We
13839 don't substitute through the constraints; that's only done when
13840 they are checked. */
13841 if (tree ci
= get_constraints (t
))
13842 set_constraints (r
, ci
);
13844 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13845 SET_DECL_FRIEND_CONTEXT (r
,
13846 tsubst (DECL_FRIEND_CONTEXT (t
),
13847 args
, complain
, in_decl
));
13849 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13850 this in the special friend case mentioned above where
13851 GEN_TMPL is NULL. */
13852 if (gen_tmpl
&& !closure
)
13854 DECL_TEMPLATE_INFO (r
)
13855 = build_template_info (gen_tmpl
, argvec
);
13856 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13859 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13861 /* We instantiated this while substituting into
13862 the type earlier (template/friend54.C). */
13865 /* We're not supposed to instantiate default arguments
13866 until they are called, for a template. But, for a
13869 template <class T> void f ()
13870 { extern void g(int i = T()); }
13872 we should do the substitution when the template is
13873 instantiated. We handle the member function case in
13874 instantiate_class_template since the default arguments
13875 might refer to other members of the class. */
13877 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13878 && !uses_template_parms (argvec
))
13879 tsubst_default_arguments (r
, complain
);
13882 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13884 /* Copy the list of befriending classes. */
13885 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13887 friends
= &TREE_CHAIN (*friends
))
13889 *friends
= copy_node (*friends
);
13890 TREE_VALUE (*friends
)
13891 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13894 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13896 maybe_retrofit_in_chrg (r
);
13897 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13898 return error_mark_node
;
13899 /* If this is an instantiation of a member template, clone it.
13900 If it isn't, that'll be handled by
13901 clone_constructors_and_destructors. */
13902 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13903 clone_function_decl (r
, /*update_methods=*/false);
13905 else if ((complain
& tf_error
) != 0
13906 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13907 && !grok_op_properties (r
, /*complain=*/true))
13908 return error_mark_node
;
13910 /* Possibly limit visibility based on template args. */
13911 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13912 if (DECL_VISIBILITY_SPECIFIED (t
))
13914 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13915 DECL_ATTRIBUTES (r
)
13916 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13918 determine_visibility (r
);
13919 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13920 && !processing_template_decl
)
13921 defaulted_late_check (r
);
13923 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13924 args
, complain
, in_decl
);
13926 if (tree attr
= lookup_attribute ("omp declare variant base",
13927 DECL_ATTRIBUTES (r
)))
13928 omp_declare_variant_finalize (r
, attr
);
13933 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13936 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13937 tree lambda_fntype
)
13939 /* We can get here when processing a member function template,
13940 member class template, or template template parameter. */
13941 tree decl
= DECL_TEMPLATE_RESULT (t
);
13947 hashval_t hash
= 0;
13949 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13951 /* Template template parameter is treated here. */
13952 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13953 if (new_type
== error_mark_node
)
13954 r
= error_mark_node
;
13955 /* If we get a real template back, return it. This can happen in
13956 the context of most_specialized_partial_spec. */
13957 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13960 /* The new TEMPLATE_DECL was built in
13961 reduce_template_parm_level. */
13962 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13966 if (!lambda_fntype
)
13968 /* We might already have an instance of this template.
13969 The ARGS are for the surrounding class type, so the
13970 full args contain the tsubst'd args for the context,
13971 plus the innermost args from the template decl. */
13972 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13973 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13974 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13975 /* Because this is a template, the arguments will still be
13976 dependent, even after substitution. If
13977 PROCESSING_TEMPLATE_DECL is not set, the dependency
13978 predicates will short-circuit. */
13979 ++processing_template_decl
;
13980 full_args
= tsubst_template_args (tmpl_args
, args
,
13981 complain
, in_decl
);
13982 --processing_template_decl
;
13983 if (full_args
== error_mark_node
)
13984 return error_mark_node
;
13986 /* If this is a default template template argument,
13987 tsubst might not have changed anything. */
13988 if (full_args
== tmpl_args
)
13991 hash
= hash_tmpl_and_args (t
, full_args
);
13992 spec
= retrieve_specialization (t
, full_args
, hash
);
13993 if (spec
!= NULL_TREE
)
13996 /* Type partial instantiations are stored as the type by
13997 lookup_template_class_1, not here as the template. */
13998 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14003 /* Make a new template decl. It will be similar to the
14004 original, but will record the current template arguments.
14005 We also create a new function declaration, which is just
14006 like the old one, but points to this new template, rather
14007 than the old one. */
14009 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14010 DECL_CHAIN (r
) = NULL_TREE
;
14012 // Build new template info linking to the original template decl.
14013 if (!lambda_fntype
)
14015 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14016 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14019 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14021 /* The template parameters for this new template are all the
14022 template parameters for the old template, except the
14023 outermost level of parameters. */
14024 DECL_TEMPLATE_PARMS (r
)
14025 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14028 if (TREE_CODE (decl
) == TYPE_DECL
14029 && !TYPE_DECL_ALIAS_P (decl
))
14032 ++processing_template_decl
;
14033 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14034 --processing_template_decl
;
14035 if (new_type
== error_mark_node
)
14036 return error_mark_node
;
14038 TREE_TYPE (r
) = new_type
;
14039 /* For a partial specialization, we need to keep pointing to
14040 the primary template. */
14041 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14042 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
14043 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
14044 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
14045 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
14050 ++processing_template_decl
;
14051 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14052 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
14054 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
14055 --processing_template_decl
;
14056 if (new_decl
== error_mark_node
)
14057 return error_mark_node
;
14059 DECL_TEMPLATE_RESULT (r
) = new_decl
;
14060 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
14061 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
14064 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14065 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
14069 DECL_TI_TEMPLATE (new_decl
) = r
;
14070 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
14074 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14075 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14077 if (PRIMARY_TEMPLATE_P (t
))
14078 DECL_PRIMARY_TEMPLATE (r
) = r
;
14080 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14082 /* Record this non-type partial instantiation. */
14083 register_specialization (r
, t
,
14084 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14090 /* True if FN is the op() for a lambda in an uninstantiated template. */
14093 lambda_fn_in_template_p (tree fn
)
14095 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14097 tree closure
= DECL_CONTEXT (fn
);
14098 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14101 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14102 which the above is true. */
14105 instantiated_lambda_fn_p (tree fn
)
14107 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14109 tree closure
= DECL_CONTEXT (fn
);
14110 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14111 return LAMBDA_EXPR_INSTANTIATED (lam
);
14114 /* We're instantiating a variable from template function TCTX. Return the
14115 corresponding current enclosing scope. This gets complicated because lambda
14116 functions in templates are regenerated rather than instantiated, but generic
14117 lambda functions are subsequently instantiated. */
14120 enclosing_instantiation_of (tree otctx
)
14123 tree fn
= current_function_decl
;
14124 int lambda_count
= 0;
14126 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14127 || instantiated_lambda_fn_p (tctx
));
14128 tctx
= decl_function_context (tctx
))
14130 for (; fn
; fn
= decl_function_context (fn
))
14133 int flambda_count
= 0;
14134 for (; fn
&& instantiated_lambda_fn_p (fn
);
14135 fn
= decl_function_context (fn
))
14137 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14138 ? most_general_template (fn
) != most_general_template (tctx
)
14141 if (flambda_count
!= lambda_count
)
14143 gcc_assert (flambda_count
> lambda_count
);
14144 for (; flambda_count
> lambda_count
; --flambda_count
)
14145 ofn
= decl_function_context (ofn
);
14147 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14148 || DECL_CONV_FN_P (ofn
));
14151 gcc_unreachable ();
14154 /* Substitute the ARGS into the T, which is a _DECL. Return the
14155 result of the substitution. Issue error and warning messages under
14156 control of COMPLAIN. */
14159 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14161 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14162 location_t saved_loc
;
14163 tree r
= NULL_TREE
;
14165 hashval_t hash
= 0;
14167 /* Set the filename and linenumber to improve error-reporting. */
14168 saved_loc
= input_location
;
14169 input_location
= DECL_SOURCE_LOCATION (t
);
14171 switch (TREE_CODE (t
))
14173 case TEMPLATE_DECL
:
14174 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14177 case FUNCTION_DECL
:
14178 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14183 tree type
= NULL_TREE
;
14185 tree expanded_types
= NULL_TREE
;
14186 tree prev_r
= NULL_TREE
;
14187 tree first_r
= NULL_TREE
;
14189 if (DECL_PACK_P (t
))
14191 /* If there is a local specialization that isn't a
14192 parameter pack, it means that we're doing a "simple"
14193 substitution from inside tsubst_pack_expansion. Just
14194 return the local specialization (which will be a single
14196 tree spec
= retrieve_local_specialization (t
);
14198 && TREE_CODE (spec
) == PARM_DECL
14199 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14202 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14203 the parameters in this function parameter pack. */
14204 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14205 complain
, in_decl
);
14206 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14208 len
= TREE_VEC_LENGTH (expanded_types
);
14210 /* Zero-length parameter packs are boring. Just substitute
14212 if (len
== 0 && !cp_unevaluated_operand
)
14213 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14218 /* All we did was update the type. Make a note of that. */
14219 type
= expanded_types
;
14220 expanded_types
= NULL_TREE
;
14224 /* Loop through all of the parameters we'll build. When T is
14225 a function parameter pack, LEN is the number of expanded
14226 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14228 for (i
= 0; i
< len
; ++i
)
14232 if (DECL_TEMPLATE_PARM_P (t
))
14233 SET_DECL_TEMPLATE_PARM_P (r
);
14235 if (expanded_types
)
14236 /* We're on the Ith parameter of the function parameter
14239 /* Get the Ith type. */
14240 type
= TREE_VEC_ELT (expanded_types
, i
);
14242 /* Rename the parameter to include the index. */
14244 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14247 /* We're dealing with a normal parameter. */
14248 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14250 type
= type_decays_to (type
);
14251 TREE_TYPE (r
) = type
;
14252 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14254 if (DECL_INITIAL (r
))
14256 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14257 DECL_INITIAL (r
) = TREE_TYPE (r
);
14259 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14260 complain
, in_decl
);
14263 DECL_CONTEXT (r
) = NULL_TREE
;
14265 if (!DECL_TEMPLATE_PARM_P (r
))
14266 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14268 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14269 args
, complain
, in_decl
);
14271 /* Keep track of the first new parameter we
14272 generate. That's what will be returned to the
14277 /* Build a proper chain of parameters when substituting
14278 into a function parameter pack. */
14280 DECL_CHAIN (prev_r
) = r
;
14283 /* If cp_unevaluated_operand is set, we're just looking for a
14284 single dummy parameter, so don't keep going. */
14285 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14286 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14287 complain
, DECL_CHAIN (t
));
14289 /* FIRST_R contains the start of the chain we've built. */
14296 tree type
= NULL_TREE
;
14297 tree vec
= NULL_TREE
;
14298 tree expanded_types
= NULL_TREE
;
14301 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14303 /* This field is a lambda capture pack. Return a TREE_VEC of
14304 the expanded fields to instantiate_class_template_1. */
14305 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14306 complain
, in_decl
);
14307 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14309 len
= TREE_VEC_LENGTH (expanded_types
);
14310 vec
= make_tree_vec (len
);
14314 /* All we did was update the type. Make a note of that. */
14315 type
= expanded_types
;
14316 expanded_types
= NULL_TREE
;
14320 for (int i
= 0; i
< len
; ++i
)
14323 if (expanded_types
)
14325 type
= TREE_VEC_ELT (expanded_types
, i
);
14327 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14330 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14332 if (type
== error_mark_node
)
14333 RETURN (error_mark_node
);
14334 TREE_TYPE (r
) = type
;
14335 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14337 if (DECL_C_BIT_FIELD (r
))
14338 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14340 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14341 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14343 /*integral_constant_expression_p=*/true);
14344 if (DECL_INITIAL (t
))
14346 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14347 NSDMI in perform_member_init. Still set DECL_INITIAL
14348 so that we know there is one. */
14349 DECL_INITIAL (r
) = void_node
;
14350 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14351 retrofit_lang_decl (r
);
14352 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14354 /* We don't have to set DECL_CONTEXT here; it is set by
14355 finish_member_declaration. */
14356 DECL_CHAIN (r
) = NULL_TREE
;
14358 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14359 args
, complain
, in_decl
);
14362 TREE_VEC_ELT (vec
, i
) = r
;
14371 /* We reach here only for member using decls. We also need to check
14372 uses_template_parms because DECL_DEPENDENT_P is not set for a
14373 using-declaration that designates a member of the current
14374 instantiation (c++/53549). */
14375 if (DECL_DEPENDENT_P (t
)
14376 || uses_template_parms (USING_DECL_SCOPE (t
)))
14378 tree scope
= USING_DECL_SCOPE (t
);
14379 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14380 if (PACK_EXPANSION_P (scope
))
14382 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14383 int len
= TREE_VEC_LENGTH (vec
);
14384 r
= make_tree_vec (len
);
14385 for (int i
= 0; i
< len
; ++i
)
14387 tree escope
= TREE_VEC_ELT (vec
, i
);
14388 tree elt
= do_class_using_decl (escope
, name
);
14391 r
= error_mark_node
;
14396 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14397 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14399 TREE_VEC_ELT (r
, i
) = elt
;
14404 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14405 complain
, in_decl
);
14406 r
= do_class_using_decl (inst_scope
, name
);
14408 r
= error_mark_node
;
14411 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14412 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14419 DECL_CHAIN (r
) = NULL_TREE
;
14426 tree argvec
= NULL_TREE
;
14427 tree gen_tmpl
= NULL_TREE
;
14429 tree tmpl
= NULL_TREE
;
14431 tree type
= NULL_TREE
;
14434 if (TREE_TYPE (t
) == error_mark_node
)
14435 RETURN (error_mark_node
);
14437 if (TREE_CODE (t
) == TYPE_DECL
14438 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14440 /* If this is the canonical decl, we don't have to
14441 mess with instantiations, and often we can't (for
14442 typename, template type parms and such). Note that
14443 TYPE_NAME is not correct for the above test if
14444 we've copied the type for a typedef. */
14445 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14446 if (type
== error_mark_node
)
14447 RETURN (error_mark_node
);
14448 r
= TYPE_NAME (type
);
14452 /* Check to see if we already have the specialization we
14455 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14457 /* T is a static data member or namespace-scope entity.
14458 We have to substitute into namespace-scope variables
14459 (not just variable templates) because of cases like:
14461 template <class T> void f() { extern T t; }
14463 where the entity referenced is not known until
14464 instantiation time. */
14466 ctx
= DECL_CONTEXT (t
);
14467 if (DECL_CLASS_SCOPE_P (t
))
14469 ctx
= tsubst_aggr_type (ctx
, args
,
14471 in_decl
, /*entering_scope=*/1);
14472 /* If CTX is unchanged, then T is in fact the
14473 specialization we want. That situation occurs when
14474 referencing a static data member within in its own
14475 class. We can use pointer equality, rather than
14476 same_type_p, because DECL_CONTEXT is always
14478 if (ctx
== DECL_CONTEXT (t
)
14479 /* ... unless T is a member template; in which
14480 case our caller can be willing to create a
14481 specialization of that template represented
14483 && !(DECL_TI_TEMPLATE (t
)
14484 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14490 tmpl
= DECL_TI_TEMPLATE (t
);
14491 gen_tmpl
= most_general_template (tmpl
);
14492 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14493 if (argvec
!= error_mark_node
)
14494 argvec
= (coerce_innermost_template_parms
14495 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14496 argvec
, t
, complain
,
14497 /*all*/true, /*defarg*/true));
14498 if (argvec
== error_mark_node
)
14499 RETURN (error_mark_node
);
14500 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14501 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14506 /* A local variable. */
14508 /* Subsequent calls to pushdecl will fill this in. */
14510 /* Unless this is a reference to a static variable from an
14511 enclosing function, in which case we need to fill it in now. */
14512 if (TREE_STATIC (t
))
14514 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14515 if (fn
!= current_function_decl
)
14518 spec
= retrieve_local_specialization (t
);
14520 /* If we already have the specialization we need, there is
14521 nothing more to do. */
14528 /* Create a new node for the specialization we need. */
14529 if (type
== NULL_TREE
)
14531 if (is_typedef_decl (t
))
14532 type
= DECL_ORIGINAL_TYPE (t
);
14534 type
= TREE_TYPE (t
);
14536 && VAR_HAD_UNKNOWN_BOUND (t
)
14537 && type
!= error_mark_node
)
14538 type
= strip_array_domain (type
);
14539 tree sub_args
= args
;
14540 if (tree auto_node
= type_uses_auto (type
))
14542 /* Mask off any template args past the variable's context so we
14543 don't replace the auto with an unrelated argument. */
14544 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14545 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14547 /* This should never happen with the new lambda instantiation
14548 model, but keep the handling just in case. */
14549 gcc_assert (!CHECKING_P
),
14550 sub_args
= strip_innermost_template_args (args
, extra
);
14552 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14553 /* Substituting the type might have recursively instantiated this
14554 same alias (c++/86171). */
14555 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14556 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14565 DECL_INITIALIZED_P (r
) = 0;
14566 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14567 if (type
== error_mark_node
)
14568 RETURN (error_mark_node
);
14569 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14571 /* It may seem that this case cannot occur, since:
14576 declares a function, not a variable. However:
14579 template <typename T> void g() { T t; }
14580 template void g<f>();
14582 is an attempt to declare a variable with function
14584 error ("variable %qD has function type",
14585 /* R is not yet sufficiently initialized, so we
14586 just use its name. */
14588 RETURN (error_mark_node
);
14590 type
= complete_type (type
);
14591 /* Wait until cp_finish_decl to set this again, to handle
14592 circular dependency (template/instantiate6.C). */
14593 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14594 type
= check_var_type (DECL_NAME (r
), type
,
14595 DECL_SOURCE_LOCATION (r
));
14596 if (DECL_HAS_VALUE_EXPR_P (t
))
14598 tree ve
= DECL_VALUE_EXPR (t
);
14599 /* If the DECL_VALUE_EXPR is converted to the declared type,
14600 preserve the identity so that gimplify_type_sizes works. */
14601 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14603 ve
= TREE_OPERAND (ve
, 0);
14604 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14605 /*constant_expression_p=*/false);
14606 if (REFERENCE_REF_P (ve
))
14608 gcc_assert (TYPE_REF_P (type
));
14609 ve
= TREE_OPERAND (ve
, 0);
14612 ve
= build_nop (type
, ve
);
14614 gcc_checking_assert (TREE_TYPE (ve
) == type
);
14615 SET_DECL_VALUE_EXPR (r
, ve
);
14617 if (CP_DECL_THREAD_LOCAL_P (r
)
14618 && !processing_template_decl
)
14619 set_decl_tls_model (r
, decl_default_tls_model (r
));
14621 else if (DECL_SELF_REFERENCE_P (t
))
14622 SET_DECL_SELF_REFERENCE_P (r
);
14623 TREE_TYPE (r
) = type
;
14624 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14625 DECL_CONTEXT (r
) = ctx
;
14626 /* Clear out the mangled name and RTL for the instantiation. */
14627 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14628 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14629 SET_DECL_RTL (r
, NULL
);
14630 /* The initializer must not be expanded until it is required;
14631 see [temp.inst]. */
14632 DECL_INITIAL (r
) = NULL_TREE
;
14633 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14636 if (DECL_LANG_SPECIFIC (r
))
14637 SET_DECL_DEPENDENT_INIT_P (r
, false);
14639 SET_DECL_MODE (r
, VOIDmode
);
14641 /* Possibly limit visibility based on template args. */
14642 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14643 if (DECL_VISIBILITY_SPECIFIED (t
))
14645 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14646 DECL_ATTRIBUTES (r
)
14647 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14649 determine_visibility (r
);
14654 /* A static data member declaration is always marked
14655 external when it is declared in-class, even if an
14656 initializer is present. We mimic the non-template
14657 processing here. */
14658 DECL_EXTERNAL (r
) = 1;
14659 if (DECL_NAMESPACE_SCOPE_P (t
))
14660 DECL_NOT_REALLY_EXTERN (r
) = 1;
14662 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14663 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14664 /* Remember whether we require constant initialization of
14665 a non-constant template variable. */
14666 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14667 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14668 if (!error_operand_p (r
) || (complain
& tf_error
))
14669 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14673 if (DECL_LANG_SPECIFIC (r
))
14674 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14675 if (!cp_unevaluated_operand
)
14676 register_local_specialization (r
, t
);
14679 DECL_CHAIN (r
) = NULL_TREE
;
14681 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14683 args
, complain
, in_decl
);
14685 /* Preserve a typedef that names a type. */
14686 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14688 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14689 set_underlying_type (r
);
14690 if (TYPE_DECL_ALIAS_P (r
))
14691 /* An alias template specialization can be dependent
14692 even if its underlying type is not. */
14693 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14696 layout_decl (r
, 0);
14701 gcc_unreachable ();
14706 /* Restore the file and line information. */
14707 input_location
= saved_loc
;
14712 /* Substitute into the complete parameter type list PARMS. */
14715 tsubst_function_parms (tree parms
,
14717 tsubst_flags_t complain
,
14720 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14723 /* Substitute into the ARG_TYPES of a function type.
14724 If END is a TREE_CHAIN, leave it and any following types
14728 tsubst_arg_types (tree arg_types
,
14731 tsubst_flags_t complain
,
14734 tree remaining_arg_types
;
14735 tree type
= NULL_TREE
;
14737 tree expanded_args
= NULL_TREE
;
14740 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14743 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14744 args
, end
, complain
, in_decl
);
14745 if (remaining_arg_types
== error_mark_node
)
14746 return error_mark_node
;
14748 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14750 /* For a pack expansion, perform substitution on the
14751 entire expression. Later on, we'll handle the arguments
14753 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14754 args
, complain
, in_decl
);
14756 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14757 /* So that we'll spin through the parameters, one by one. */
14758 i
= TREE_VEC_LENGTH (expanded_args
);
14761 /* We only partially substituted into the parameter
14762 pack. Our type is TYPE_PACK_EXPANSION. */
14763 type
= expanded_args
;
14764 expanded_args
= NULL_TREE
;
14772 type
= TREE_VEC_ELT (expanded_args
, i
);
14774 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14776 if (type
== error_mark_node
)
14777 return error_mark_node
;
14778 if (VOID_TYPE_P (type
))
14780 if (complain
& tf_error
)
14782 error ("invalid parameter type %qT", type
);
14784 error ("in declaration %q+D", in_decl
);
14786 return error_mark_node
;
14789 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14790 return error_mark_node
;
14792 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14793 top-level qualifiers as required. */
14794 type
= cv_unqualified (type_decays_to (type
));
14796 /* We do not substitute into default arguments here. The standard
14797 mandates that they be instantiated only when needed, which is
14798 done in build_over_call. */
14799 default_arg
= TREE_PURPOSE (arg_types
);
14801 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14802 since the new op() won't have any associated template arguments for us
14803 to refer to later. */
14804 if (lambda_fn_in_template_p (in_decl
))
14805 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14806 false/*fn*/, false/*constexpr*/);
14808 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14810 /* We've instantiated a template before its default arguments
14811 have been parsed. This can happen for a nested template
14812 class, and is not an error unless we require the default
14813 argument in a call of this function. */
14814 remaining_arg_types
=
14815 tree_cons (default_arg
, type
, remaining_arg_types
);
14816 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14817 remaining_arg_types
);
14820 remaining_arg_types
=
14821 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14824 return remaining_arg_types
;
14827 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14828 *not* handle the exception-specification for FNTYPE, because the
14829 initial substitution of explicitly provided template parameters
14830 during argument deduction forbids substitution into the
14831 exception-specification:
14835 All references in the function type of the function template to the
14836 corresponding template parameters are replaced by the specified tem-
14837 plate argument values. If a substitution in a template parameter or
14838 in the function type of the function template results in an invalid
14839 type, type deduction fails. [Note: The equivalent substitution in
14840 exception specifications is done only when the function is instanti-
14841 ated, at which point a program is ill-formed if the substitution
14842 results in an invalid type.] */
14845 tsubst_function_type (tree t
,
14847 tsubst_flags_t complain
,
14851 tree arg_types
= NULL_TREE
;
14853 /* The TYPE_CONTEXT is not used for function/method types. */
14854 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14856 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14858 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14860 if (late_return_type_p
)
14862 /* Substitute the argument types. */
14863 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14864 complain
, in_decl
);
14865 if (arg_types
== error_mark_node
)
14866 return error_mark_node
;
14868 tree save_ccp
= current_class_ptr
;
14869 tree save_ccr
= current_class_ref
;
14870 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14871 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14872 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14875 /* DR 1207: 'this' is in scope in the trailing return type. */
14876 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14879 /* Substitute the return type. */
14880 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14884 current_class_ptr
= save_ccp
;
14885 current_class_ref
= save_ccr
;
14889 /* Substitute the return type. */
14890 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14892 if (return_type
== error_mark_node
)
14893 return error_mark_node
;
14894 /* DR 486 clarifies that creation of a function type with an
14895 invalid return type is a deduction failure. */
14896 if (TREE_CODE (return_type
) == ARRAY_TYPE
14897 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14899 if (complain
& tf_error
)
14901 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14902 error ("function returning an array");
14904 error ("function returning a function");
14906 return error_mark_node
;
14909 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14910 return error_mark_node
;
14912 if (!late_return_type_p
)
14914 /* Substitute the argument types. */
14915 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14916 complain
, in_decl
);
14917 if (arg_types
== error_mark_node
)
14918 return error_mark_node
;
14921 /* Construct a new type node and return it. */
14922 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
14923 /*raises=*/NULL_TREE
, complain
);
14926 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14927 ARGS into that specification, and return the substituted
14928 specification. If there is no specification, return NULL_TREE. */
14931 tsubst_exception_specification (tree fntype
,
14933 tsubst_flags_t complain
,
14940 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14941 new_specs
= NULL_TREE
;
14942 if (specs
&& TREE_PURPOSE (specs
))
14944 /* A noexcept-specifier. */
14945 tree expr
= TREE_PURPOSE (specs
);
14946 if (TREE_CODE (expr
) == INTEGER_CST
)
14950 /* Defer instantiation of noexcept-specifiers to avoid
14951 excessive instantiations (c++/49107). */
14952 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14953 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14955 /* We already partially instantiated this member template,
14956 so combine the new args with the old. */
14957 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14958 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14959 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14960 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14964 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14965 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14970 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14972 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14974 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14976 new_specs
= tsubst_copy_and_build
14977 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14978 /*integral_constant_expression_p=*/true);
14980 new_specs
= build_noexcept_spec (new_specs
, complain
);
14984 if (! TREE_VALUE (specs
))
14991 tree expanded_specs
= NULL_TREE
;
14993 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14995 /* Expand the pack expansion type. */
14996 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15000 if (expanded_specs
== error_mark_node
)
15001 return error_mark_node
;
15002 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15003 len
= TREE_VEC_LENGTH (expanded_specs
);
15006 /* We're substituting into a member template, so
15007 we got a TYPE_PACK_EXPANSION back. Add that
15008 expansion and move on. */
15009 gcc_assert (TREE_CODE (expanded_specs
)
15010 == TYPE_PACK_EXPANSION
);
15011 new_specs
= add_exception_specifier (new_specs
,
15014 specs
= TREE_CHAIN (specs
);
15019 for (i
= 0; i
< len
; ++i
)
15021 if (expanded_specs
)
15022 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15024 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15025 if (spec
== error_mark_node
)
15027 new_specs
= add_exception_specifier (new_specs
, spec
,
15031 specs
= TREE_CHAIN (specs
);
15037 /* Take the tree structure T and replace template parameters used
15038 therein with the argument vector ARGS. IN_DECL is an associated
15039 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15040 Issue error and warning messages under control of COMPLAIN. Note
15041 that we must be relatively non-tolerant of extensions here, in
15042 order to preserve conformance; if we allow substitutions that
15043 should not be allowed, we may allow argument deductions that should
15044 not succeed, and therefore report ambiguous overload situations
15045 where there are none. In theory, we could allow the substitution,
15046 but indicate that it should have failed, and allow our caller to
15047 make sure that the right thing happens, but we don't try to do this
15050 This function is used for dealing with types, decls and the like;
15051 for expressions, use tsubst_expr or tsubst_copy. */
15054 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15056 enum tree_code code
;
15057 tree type
, r
= NULL_TREE
;
15059 if (t
== NULL_TREE
|| t
== error_mark_node
15060 || t
== integer_type_node
15061 || t
== void_type_node
15062 || t
== char_type_node
15063 || t
== unknown_type_node
15064 || TREE_CODE (t
) == NAMESPACE_DECL
15065 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15069 return tsubst_decl (t
, args
, complain
);
15071 if (args
== NULL_TREE
)
15074 code
= TREE_CODE (t
);
15076 if (code
== IDENTIFIER_NODE
)
15077 type
= IDENTIFIER_TYPE_VALUE (t
);
15079 type
= TREE_TYPE (t
);
15081 gcc_assert (type
!= unknown_type_node
);
15083 /* Reuse typedefs. We need to do this to handle dependent attributes,
15084 such as attribute aligned. */
15086 && typedef_variant_p (t
))
15088 tree decl
= TYPE_NAME (t
);
15090 if (alias_template_specialization_p (t
, nt_opaque
))
15092 /* DECL represents an alias template and we want to
15094 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15095 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15096 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15098 else if (DECL_CLASS_SCOPE_P (decl
)
15099 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15100 && uses_template_parms (DECL_CONTEXT (decl
)))
15102 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15103 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15104 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15106 else if (DECL_FUNCTION_SCOPE_P (decl
)
15107 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15108 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15109 r
= retrieve_local_specialization (decl
);
15111 /* The typedef is from a non-template context. */
15117 r
= cp_build_qualified_type_real
15118 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15119 complain
| tf_ignore_bad_quals
);
15124 /* We don't have an instantiation yet, so drop the typedef. */
15125 int quals
= cp_type_quals (t
);
15126 t
= DECL_ORIGINAL_TYPE (decl
);
15127 t
= cp_build_qualified_type_real (t
, quals
,
15128 complain
| tf_ignore_bad_quals
);
15132 bool fndecl_type
= (complain
& tf_fndecl_type
);
15133 complain
&= ~tf_fndecl_type
;
15136 && code
!= TYPENAME_TYPE
15137 && code
!= TEMPLATE_TYPE_PARM
15138 && code
!= TEMPLATE_PARM_INDEX
15139 && code
!= IDENTIFIER_NODE
15140 && code
!= FUNCTION_TYPE
15141 && code
!= METHOD_TYPE
)
15142 type
= tsubst (type
, args
, complain
, in_decl
);
15143 if (type
== error_mark_node
)
15144 return error_mark_node
;
15150 case ENUMERAL_TYPE
:
15151 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15152 /*entering_scope=*/0);
15155 case IDENTIFIER_NODE
:
15166 if (t
== integer_type_node
)
15169 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15170 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15174 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15176 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15177 /*integral_constant_expression_p=*/false);
15179 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15181 if (TREE_CODE (max
) == NOP_EXPR
15182 && TREE_SIDE_EFFECTS (omax
)
15183 && !TREE_TYPE (max
))
15184 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15186 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15187 with TREE_SIDE_EFFECTS that indicates this is not an integral
15188 constant expression. */
15189 if (processing_template_decl
15190 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15192 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15193 TREE_SIDE_EFFECTS (max
) = 1;
15196 return compute_array_index_type (NULL_TREE
, max
, complain
);
15199 case TEMPLATE_TYPE_PARM
:
15200 case TEMPLATE_TEMPLATE_PARM
:
15201 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15202 case TEMPLATE_PARM_INDEX
:
15207 tree arg
= NULL_TREE
;
15211 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15212 template_parm_level_and_index (t
, &level
, &idx
);
15214 levels
= TMPL_ARGS_DEPTH (args
);
15215 if (level
<= levels
15216 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15218 arg
= TMPL_ARG (args
, level
, idx
);
15220 /* See through ARGUMENT_PACK_SELECT arguments. */
15221 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15222 arg
= argument_pack_select_arg (arg
);
15225 if (arg
== error_mark_node
)
15226 return error_mark_node
;
15227 else if (arg
!= NULL_TREE
)
15229 if (ARGUMENT_PACK_P (arg
))
15230 /* If ARG is an argument pack, we don't actually want to
15231 perform a substitution here, because substitutions
15232 for argument packs are only done
15233 element-by-element. We can get to this point when
15234 substituting the type of a non-type template
15235 parameter pack, when that type actually contains
15236 template parameter packs from an outer template, e.g.,
15238 template<typename... Types> struct A {
15239 template<Types... Values> struct B { };
15243 if (code
== TEMPLATE_TYPE_PARM
)
15247 /* When building concept checks for the purpose of
15248 deducing placeholders, we can end up with wildcards
15249 where types are expected. Adjust this to the deduced
15251 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15252 arg
= TREE_TYPE (TREE_TYPE (arg
));
15254 gcc_assert (TYPE_P (arg
));
15256 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15258 return cp_build_qualified_type_real
15259 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15261 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15263 /* We are processing a type constructed from a
15264 template template parameter. */
15265 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15266 args
, complain
, in_decl
);
15267 if (argvec
== error_mark_node
)
15268 return error_mark_node
;
15270 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15271 || TREE_CODE (arg
) == TEMPLATE_DECL
15272 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15274 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15275 /* Consider this code:
15277 template <template <class> class Template>
15279 template <class Arg> using Bind = Template<Arg>;
15282 template <template <class> class Template, class Arg>
15283 using Instantiate = Template<Arg>; //#0
15285 template <template <class> class Template,
15288 Instantiate<Internal<Template>::template Bind,
15291 When #1 is parsed, the
15292 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15293 parameter `Template' in #0 matches the
15294 UNBOUND_CLASS_TEMPLATE representing the argument
15295 `Internal<Template>::template Bind'; We then want
15296 to assemble the type `Bind<Argument>' that can't
15297 be fully created right now, because
15298 `Internal<Template>' not being complete, the Bind
15299 template cannot be looked up in that context. So
15300 we need to "store" `Bind<Argument>' for later
15301 when the context of Bind becomes complete. Let's
15302 store that in a TYPENAME_TYPE. */
15303 return make_typename_type (TYPE_CONTEXT (arg
),
15304 build_nt (TEMPLATE_ID_EXPR
,
15305 TYPE_IDENTIFIER (arg
),
15310 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15311 are resolving nested-types in the signature of a
15312 member function templates. Otherwise ARG is a
15313 TEMPLATE_DECL and is the real template to be
15315 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15316 arg
= TYPE_NAME (arg
);
15318 r
= lookup_template_class (arg
,
15320 DECL_CONTEXT (arg
),
15321 /*entering_scope=*/0,
15323 return cp_build_qualified_type_real
15324 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15326 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15329 /* TEMPLATE_PARM_INDEX. */
15330 return convert_from_reference (unshare_expr (arg
));
15334 /* This can happen during the attempted tsubst'ing in
15335 unify. This means that we don't yet have any information
15336 about the template parameter in question. */
15339 /* Early in template argument deduction substitution, we don't
15340 want to reduce the level of 'auto', or it will be confused
15341 with a normal template parm in subsequent deduction.
15342 Similarly, don't reduce the level of template parameters to
15343 avoid mismatches when deducing their types. */
15344 if (complain
& tf_partial
)
15347 /* If we get here, we must have been looking at a parm for a
15348 more deeply nested template. Make a new version of this
15349 template parameter, but with a lower level. */
15352 case TEMPLATE_TYPE_PARM
:
15353 case TEMPLATE_TEMPLATE_PARM
:
15354 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15355 if (cp_type_quals (t
))
15357 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15358 r
= cp_build_qualified_type_real
15359 (r
, cp_type_quals (t
),
15360 complain
| (code
== TEMPLATE_TYPE_PARM
15361 ? tf_ignore_bad_quals
: 0));
15363 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15364 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15365 && (r
= (TEMPLATE_PARM_DESCENDANTS
15366 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15367 && (r
= TREE_TYPE (r
))
15368 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15369 /* Break infinite recursion when substituting the constraints
15370 of a constrained placeholder. */;
15371 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15372 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15373 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15374 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15375 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15376 && (TEMPLATE_PARM_LEVEL (r
)
15377 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15378 /* Cache the simple case of lowering a type parameter. */
15383 TEMPLATE_TYPE_PARM_INDEX (r
)
15384 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15385 r
, levels
, args
, complain
);
15386 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15387 TYPE_MAIN_VARIANT (r
) = r
;
15388 TYPE_POINTER_TO (r
) = NULL_TREE
;
15389 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15391 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15393 /* Propagate constraints on placeholders since they are
15394 only instantiated during satisfaction. */
15395 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15396 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15397 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15399 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15400 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15404 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15405 /* We have reduced the level of the template
15406 template parameter, but not the levels of its
15407 template parameters, so canonical_type_parameter
15408 will not be able to find the canonical template
15409 template parameter for this level. Thus, we
15410 require structural equality checking to compare
15411 TEMPLATE_TEMPLATE_PARMs. */
15412 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15413 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15414 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15416 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15418 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15420 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15421 /* We might need to substitute into the types of non-type
15422 template parameters. */
15423 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15424 complain
, in_decl
);
15425 if (tmpl
== error_mark_node
)
15426 return error_mark_node
;
15427 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15428 complain
, in_decl
);
15429 if (argvec
== error_mark_node
)
15430 return error_mark_node
;
15432 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15433 = build_template_info (tmpl
, argvec
);
15438 case TEMPLATE_PARM_INDEX
:
15439 /* OK, now substitute the type of the non-type parameter. We
15440 couldn't do it earlier because it might be an auto parameter,
15441 and we wouldn't need to if we had an argument. */
15442 type
= tsubst (type
, args
, complain
, in_decl
);
15443 if (type
== error_mark_node
)
15444 return error_mark_node
;
15445 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15449 gcc_unreachable ();
15457 tree purpose
, value
, chain
;
15459 if (t
== void_list_node
)
15462 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
15463 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
15465 /* We have pack expansions, so expand those and
15466 create a new list out of it. */
15468 /* Expand the argument expressions. */
15469 tree purposevec
= NULL_TREE
;
15470 if (TREE_PURPOSE (t
))
15471 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
15472 complain
, in_decl
);
15473 if (purposevec
== error_mark_node
)
15474 return error_mark_node
;
15476 tree valuevec
= NULL_TREE
;
15477 if (TREE_VALUE (t
))
15478 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
15479 complain
, in_decl
);
15480 if (valuevec
== error_mark_node
)
15481 return error_mark_node
;
15483 /* Build the rest of the list. */
15484 tree chain
= TREE_CHAIN (t
);
15485 if (chain
&& chain
!= void_type_node
)
15486 chain
= tsubst (chain
, args
, complain
, in_decl
);
15487 if (chain
== error_mark_node
)
15488 return error_mark_node
;
15490 /* Determine the number of arguments. */
15492 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
15494 len
= TREE_VEC_LENGTH (purposevec
);
15495 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15497 else if (TREE_CODE (valuevec
) == TREE_VEC
)
15498 len
= TREE_VEC_LENGTH (valuevec
);
15501 /* Since we only performed a partial substitution into
15502 the argument pack, we only RETURN (a single list
15504 if (purposevec
== TREE_PURPOSE (t
)
15505 && valuevec
== TREE_VALUE (t
)
15506 && chain
== TREE_CHAIN (t
))
15509 return tree_cons (purposevec
, valuevec
, chain
);
15512 /* Convert the argument vectors into a TREE_LIST. */
15513 for (int i
= len
; i
-- > 0; )
15515 purpose
= (purposevec
? TREE_VEC_ELT (purposevec
, i
)
15517 value
= (valuevec
? TREE_VEC_ELT (valuevec
, i
)
15520 /* Build the list (backwards). */
15521 chain
= hash_tree_cons (purpose
, value
, chain
);
15527 purpose
= TREE_PURPOSE (t
);
15530 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15531 if (purpose
== error_mark_node
)
15532 return error_mark_node
;
15534 value
= TREE_VALUE (t
);
15537 value
= tsubst (value
, args
, complain
, in_decl
);
15538 if (value
== error_mark_node
)
15539 return error_mark_node
;
15541 chain
= TREE_CHAIN (t
);
15542 if (chain
&& chain
!= void_type_node
)
15544 chain
= tsubst (chain
, args
, complain
, in_decl
);
15545 if (chain
== error_mark_node
)
15546 return error_mark_node
;
15548 if (purpose
== TREE_PURPOSE (t
)
15549 && value
== TREE_VALUE (t
)
15550 && chain
== TREE_CHAIN (t
))
15552 return hash_tree_cons (purpose
, value
, chain
);
15556 /* We should never be tsubsting a binfo. */
15557 gcc_unreachable ();
15560 /* A vector of template arguments. */
15561 gcc_assert (!type
);
15562 return tsubst_template_args (t
, args
, complain
, in_decl
);
15565 case REFERENCE_TYPE
:
15567 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15572 Type deduction may fail for any of the following
15575 -- Attempting to create a pointer to reference type.
15576 -- Attempting to create a reference to a reference type or
15577 a reference to void.
15579 Core issue 106 says that creating a reference to a reference
15580 during instantiation is no longer a cause for failure. We
15581 only enforce this check in strict C++98 mode. */
15582 if ((TYPE_REF_P (type
)
15583 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15584 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15586 static location_t last_loc
;
15588 /* We keep track of the last time we issued this error
15589 message to avoid spewing a ton of messages during a
15590 single bad template instantiation. */
15591 if (complain
& tf_error
15592 && last_loc
!= input_location
)
15594 if (VOID_TYPE_P (type
))
15595 error ("forming reference to void");
15596 else if (code
== POINTER_TYPE
)
15597 error ("forming pointer to reference type %qT", type
);
15599 error ("forming reference to reference type %qT", type
);
15600 last_loc
= input_location
;
15603 return error_mark_node
;
15605 else if (TREE_CODE (type
) == FUNCTION_TYPE
15606 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15607 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15609 if (complain
& tf_error
)
15611 if (code
== POINTER_TYPE
)
15612 error ("forming pointer to qualified function type %qT",
15615 error ("forming reference to qualified function type %qT",
15618 return error_mark_node
;
15620 else if (code
== POINTER_TYPE
)
15622 r
= build_pointer_type (type
);
15623 if (TREE_CODE (type
) == METHOD_TYPE
)
15624 r
= build_ptrmemfunc_type (r
);
15626 else if (TYPE_REF_P (type
))
15627 /* In C++0x, during template argument substitution, when there is an
15628 attempt to create a reference to a reference type, reference
15629 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15631 "If a template-argument for a template-parameter T names a type
15632 that is a reference to a type A, an attempt to create the type
15633 'lvalue reference to cv T' creates the type 'lvalue reference to
15634 A,' while an attempt to create the type type rvalue reference to
15635 cv T' creates the type T"
15637 r
= cp_build_reference_type
15639 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15641 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15642 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15644 if (r
!= error_mark_node
)
15645 /* Will this ever be needed for TYPE_..._TO values? */
15652 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15653 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15657 Type deduction may fail for any of the following
15660 -- Attempting to create "pointer to member of T" when T
15661 is not a class type. */
15662 if (complain
& tf_error
)
15663 error ("creating pointer to member of non-class type %qT", r
);
15664 return error_mark_node
;
15666 if (TYPE_REF_P (type
))
15668 if (complain
& tf_error
)
15669 error ("creating pointer to member reference type %qT", type
);
15670 return error_mark_node
;
15672 if (VOID_TYPE_P (type
))
15674 if (complain
& tf_error
)
15675 error ("creating pointer to member of type void");
15676 return error_mark_node
;
15678 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15679 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15681 /* The type of the implicit object parameter gets its
15682 cv-qualifiers from the FUNCTION_TYPE. */
15685 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15686 type_memfn_rqual (type
));
15687 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15688 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15692 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15696 case FUNCTION_TYPE
:
15701 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15702 if (fntype
== error_mark_node
)
15703 return error_mark_node
;
15705 /* Substitute the exception specification. */
15706 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15707 /*defer_ok*/fndecl_type
);
15708 if (specs
== error_mark_node
)
15709 return error_mark_node
;
15711 fntype
= build_exception_variant (fntype
, specs
);
15716 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15717 if (domain
== error_mark_node
)
15718 return error_mark_node
;
15720 /* As an optimization, we avoid regenerating the array type if
15721 it will obviously be the same as T. */
15722 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15725 /* These checks should match the ones in create_array_type_for_decl.
15729 The deduction may fail for any of the following reasons:
15731 -- Attempting to create an array with an element type that
15732 is void, a function type, or a reference type, or [DR337]
15733 an abstract class type. */
15734 if (VOID_TYPE_P (type
)
15735 || TREE_CODE (type
) == FUNCTION_TYPE
15736 || (TREE_CODE (type
) == ARRAY_TYPE
15737 && TYPE_DOMAIN (type
) == NULL_TREE
)
15738 || TYPE_REF_P (type
))
15740 if (complain
& tf_error
)
15741 error ("creating array of %qT", type
);
15742 return error_mark_node
;
15745 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15746 return error_mark_node
;
15748 r
= build_cplus_array_type (type
, domain
);
15750 if (!valid_array_size_p (input_location
, r
, in_decl
,
15751 (complain
& tf_error
)))
15752 return error_mark_node
;
15754 if (TYPE_USER_ALIGN (t
))
15756 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15757 TYPE_USER_ALIGN (r
) = 1;
15763 case TYPENAME_TYPE
:
15765 tree ctx
= TYPE_CONTEXT (t
);
15766 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15768 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15769 if (ctx
== error_mark_node
15770 || TREE_VEC_LENGTH (ctx
) > 1)
15771 return error_mark_node
;
15772 if (TREE_VEC_LENGTH (ctx
) == 0)
15774 if (complain
& tf_error
)
15775 error ("%qD is instantiated for an empty pack",
15776 TYPENAME_TYPE_FULLNAME (t
));
15777 return error_mark_node
;
15779 ctx
= TREE_VEC_ELT (ctx
, 0);
15782 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15783 /*entering_scope=*/1);
15784 if (ctx
== error_mark_node
)
15785 return error_mark_node
;
15787 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15788 complain
, in_decl
);
15789 if (f
== error_mark_node
)
15790 return error_mark_node
;
15792 if (!MAYBE_CLASS_TYPE_P (ctx
))
15794 if (complain
& tf_error
)
15795 error ("%qT is not a class, struct, or union type", ctx
);
15796 return error_mark_node
;
15798 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15800 /* Normally, make_typename_type does not require that the CTX
15801 have complete type in order to allow things like:
15803 template <class T> struct S { typename S<T>::X Y; };
15805 But, such constructs have already been resolved by this
15806 point, so here CTX really should have complete type, unless
15807 it's a partial instantiation. */
15808 ctx
= complete_type (ctx
);
15809 if (!COMPLETE_TYPE_P (ctx
))
15811 if (complain
& tf_error
)
15812 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15813 return error_mark_node
;
15817 f
= make_typename_type (ctx
, f
, typename_type
,
15818 complain
| tf_keep_type_decl
);
15819 if (f
== error_mark_node
)
15821 if (TREE_CODE (f
) == TYPE_DECL
)
15823 complain
|= tf_ignore_bad_quals
;
15827 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15829 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15831 if (complain
& tf_error
)
15832 error ("%qT resolves to %qT, which is not an enumeration type",
15835 return error_mark_node
;
15837 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15839 if (complain
& tf_error
)
15840 error ("%qT resolves to %qT, which is not a class type",
15843 return error_mark_node
;
15847 return cp_build_qualified_type_real
15848 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15851 case UNBOUND_CLASS_TEMPLATE
:
15853 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15854 in_decl
, /*entering_scope=*/1);
15855 tree name
= TYPE_IDENTIFIER (t
);
15856 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15858 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15859 return error_mark_node
;
15862 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15863 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15870 ++cp_unevaluated_operand
;
15871 ++c_inhibit_evaluation_warnings
;
15873 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15875 /*integral_constant_expression_p=*/false);
15877 --cp_unevaluated_operand
;
15878 --c_inhibit_evaluation_warnings
;
15880 type
= finish_typeof (type
);
15881 return cp_build_qualified_type_real (type
,
15883 | cp_type_quals (type
),
15887 case DECLTYPE_TYPE
:
15891 ++cp_unevaluated_operand
;
15892 ++c_inhibit_evaluation_warnings
;
15894 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15895 complain
|tf_decltype
, in_decl
,
15896 /*function_p*/false,
15897 /*integral_constant_expression*/false);
15899 --cp_unevaluated_operand
;
15900 --c_inhibit_evaluation_warnings
;
15902 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15903 type
= lambda_capture_field_type (type
,
15904 false /*explicit_init*/,
15905 DECLTYPE_FOR_REF_CAPTURE (t
));
15906 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15907 type
= lambda_proxy_type (type
);
15910 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15911 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15913 /* In a template ~id could be either a complement expression
15914 or an unqualified-id naming a destructor; if instantiating
15915 it produces an expression, it's not an id-expression or
15918 type
= finish_decltype_type (type
, id
, complain
);
15920 return cp_build_qualified_type_real (type
,
15922 | cp_type_quals (type
),
15923 complain
| tf_ignore_bad_quals
);
15926 case UNDERLYING_TYPE
:
15928 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15929 complain
, in_decl
);
15930 return finish_underlying_type (type
);
15933 case TYPE_ARGUMENT_PACK
:
15934 case NONTYPE_ARGUMENT_PACK
:
15938 if (code
== NONTYPE_ARGUMENT_PACK
)
15939 r
= make_node (code
);
15941 r
= cxx_make_type (code
);
15943 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15944 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15945 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15963 /* We should use one of the expression tsubsts for these codes. */
15964 gcc_unreachable ();
15967 sorry ("use of %qs in template", get_tree_code_name (code
));
15968 return error_mark_node
;
15972 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15973 expression on the left-hand side of the "." or "->" operator. We
15974 only do the lookup if we had a dependent BASELINK. Otherwise we
15975 adjust it onto the instantiated heirarchy. */
15978 tsubst_baselink (tree baselink
, tree object_type
,
15979 tree args
, tsubst_flags_t complain
, tree in_decl
)
15981 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15982 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15983 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15985 tree optype
= BASELINK_OPTYPE (baselink
);
15986 optype
= tsubst (optype
, args
, complain
, in_decl
);
15988 tree template_args
= NULL_TREE
;
15989 bool template_id_p
= false;
15990 tree fns
= BASELINK_FUNCTIONS (baselink
);
15991 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15993 template_id_p
= true;
15994 template_args
= TREE_OPERAND (fns
, 1);
15995 fns
= TREE_OPERAND (fns
, 0);
15997 template_args
= tsubst_template_args (template_args
, args
,
15998 complain
, in_decl
);
16001 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16002 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16003 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16007 tree name
= OVL_NAME (fns
);
16008 if (IDENTIFIER_CONV_OP_P (name
))
16009 name
= make_conv_op_name (optype
);
16011 if (name
== complete_dtor_identifier
)
16012 /* Treat as-if non-dependent below. */
16013 dependent_p
= false;
16015 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
16018 if ((complain
& tf_error
)
16019 && constructor_name_p (name
, qualifying_scope
))
16020 error ("cannot call constructor %<%T::%D%> directly",
16021 qualifying_scope
, name
);
16022 return error_mark_node
;
16025 if (BASELINK_P (baselink
))
16026 fns
= BASELINK_FUNCTIONS (baselink
);
16029 /* We're going to overwrite pieces below, make a duplicate. */
16030 baselink
= copy_node (baselink
);
16032 /* If lookup found a single function, mark it as used at this point.
16033 (If lookup found multiple functions the one selected later by
16034 overload resolution will be marked as used at that point.) */
16035 if (!template_id_p
&& !really_overloaded_fn (fns
))
16037 tree fn
= OVL_FIRST (fns
);
16038 bool ok
= mark_used (fn
, complain
);
16039 if (!ok
&& !(complain
& tf_error
))
16040 return error_mark_node
;
16041 if (ok
&& BASELINK_P (baselink
))
16042 /* We might have instantiated an auto function. */
16043 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16046 if (BASELINK_P (baselink
))
16048 /* Add back the template arguments, if present. */
16050 BASELINK_FUNCTIONS (baselink
)
16051 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16053 /* Update the conversion operator type. */
16054 BASELINK_OPTYPE (baselink
) = optype
;
16058 object_type
= current_class_type
;
16060 if (qualified_p
|| !dependent_p
)
16062 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16066 /* We need to call adjust_result_of_qualified_name_lookup in case the
16067 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16068 so that we still get virtual function binding. */
16069 BASELINK_QUALIFIED_P (baselink
) = false;
16075 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16076 true if the qualified-id will be a postfix-expression in-and-of
16077 itself; false if more of the postfix-expression follows the
16078 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16082 tsubst_qualified_id (tree qualified_id
, tree args
,
16083 tsubst_flags_t complain
, tree in_decl
,
16084 bool done
, bool address_p
)
16090 tree template_args
;
16091 location_t loc
= UNKNOWN_LOCATION
;
16093 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16095 /* Figure out what name to look up. */
16096 name
= TREE_OPERAND (qualified_id
, 1);
16097 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16099 is_template
= true;
16100 loc
= EXPR_LOCATION (name
);
16101 template_args
= TREE_OPERAND (name
, 1);
16103 template_args
= tsubst_template_args (template_args
, args
,
16104 complain
, in_decl
);
16105 if (template_args
== error_mark_node
)
16106 return error_mark_node
;
16107 name
= TREE_OPERAND (name
, 0);
16111 is_template
= false;
16112 template_args
= NULL_TREE
;
16115 /* Substitute into the qualifying scope. When there are no ARGS, we
16116 are just trying to simplify a non-dependent expression. In that
16117 case the qualifying scope may be dependent, and, in any case,
16118 substituting will not help. */
16119 scope
= TREE_OPERAND (qualified_id
, 0);
16122 scope
= tsubst (scope
, args
, complain
, in_decl
);
16123 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16128 if (dependent_scope_p (scope
))
16131 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16132 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16133 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16134 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16138 if (!BASELINK_P (name
) && !DECL_P (expr
))
16140 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16142 /* A BIT_NOT_EXPR is used to represent a destructor. */
16143 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16145 error ("qualifying type %qT does not match destructor name ~%qT",
16146 scope
, TREE_OPERAND (expr
, 0));
16147 expr
= error_mark_node
;
16150 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16151 /*is_type_p=*/0, false);
16154 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
16155 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16156 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16158 if (complain
& tf_error
)
16160 error ("dependent-name %qE is parsed as a non-type, but "
16161 "instantiation yields a type", qualified_id
);
16162 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16164 return error_mark_node
;
16170 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16172 return error_mark_node
;
16173 /* Remember that there was a reference to this entity. */
16174 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16175 return error_mark_node
;
16178 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16180 if (complain
& tf_error
)
16181 qualified_name_lookup_error (scope
,
16182 TREE_OPERAND (qualified_id
, 1),
16183 expr
, input_location
);
16184 return error_mark_node
;
16189 /* We may be repeating a check already done during parsing, but
16190 if it was well-formed and passed then, it will pass again
16191 now, and if it didn't, we wouldn't have got here. The case
16192 we want to catch is when we couldn't tell then, and can now,
16193 namely when templ prior to substitution was an
16195 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16196 return error_mark_node
;
16198 if (variable_template_p (expr
))
16199 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16202 expr
= lookup_template_function (expr
, template_args
);
16205 if (expr
== error_mark_node
&& complain
& tf_error
)
16206 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16207 expr
, input_location
);
16208 else if (TYPE_P (scope
))
16210 expr
= (adjust_result_of_qualified_name_lookup
16211 (expr
, scope
, current_nonlambda_class_type ()));
16212 expr
= (finish_qualified_id_expr
16213 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16214 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16215 /*template_arg_p=*/false, complain
));
16218 /* Expressions do not generally have reference type. */
16219 if (TREE_CODE (expr
) != SCOPE_REF
16220 /* However, if we're about to form a pointer-to-member, we just
16221 want the referenced member referenced. */
16222 && TREE_CODE (expr
) != OFFSET_REF
)
16223 expr
= convert_from_reference (expr
);
16225 if (REF_PARENTHESIZED_P (qualified_id
))
16226 expr
= force_paren_expr (expr
);
16231 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16232 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16236 tsubst_init (tree init
, tree decl
, tree args
,
16237 tsubst_flags_t complain
, tree in_decl
)
16242 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16244 tree type
= TREE_TYPE (decl
);
16246 if (!init
&& type
!= error_mark_node
)
16248 if (tree auto_node
= type_uses_auto (type
))
16250 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16252 if (complain
& tf_error
)
16253 error ("initializer for %q#D expands to an empty list "
16254 "of expressions", decl
);
16255 return error_mark_node
;
16258 else if (!dependent_type_p (type
))
16260 /* If we had an initializer but it
16261 instantiated to nothing,
16262 value-initialize the object. This will
16263 only occur when the initializer was a
16264 pack expansion where the parameter packs
16265 used in that expansion were of length
16267 init
= build_value_init (type
, complain
);
16268 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16269 init
= get_target_expr_sfinae (init
, complain
);
16270 if (TREE_CODE (init
) == TARGET_EXPR
)
16271 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16278 /* Like tsubst, but deals with expressions. This function just replaces
16279 template parms; to finish processing the resultant expression, use
16280 tsubst_copy_and_build or tsubst_expr. */
16283 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16285 enum tree_code code
;
16288 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16291 code
= TREE_CODE (t
);
16296 r
= retrieve_local_specialization (t
);
16298 if (r
== NULL_TREE
)
16300 /* We get here for a use of 'this' in an NSDMI. */
16301 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16302 return current_class_ptr
;
16304 /* This can happen for a parameter name used later in a function
16305 declaration (such as in a late-specified return type). Just
16306 make a dummy decl, since it's only used for its type. */
16307 gcc_assert (cp_unevaluated_operand
!= 0);
16308 r
= tsubst_decl (t
, args
, complain
);
16309 /* Give it the template pattern as its context; its true context
16310 hasn't been instantiated yet and this is good enough for
16312 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16315 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16316 r
= argument_pack_select_arg (r
);
16317 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16318 return error_mark_node
;
16326 if (DECL_TEMPLATE_PARM_P (t
))
16327 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16328 /* There is no need to substitute into namespace-scope
16330 if (DECL_NAMESPACE_SCOPE_P (t
))
16332 /* If ARGS is NULL, then T is known to be non-dependent. */
16333 if (args
== NULL_TREE
)
16334 return scalar_constant_value (t
);
16336 /* Unfortunately, we cannot just call lookup_name here.
16339 template <int I> int f() {
16341 struct S { void g() { E e = a; } };
16344 When we instantiate f<7>::S::g(), say, lookup_name is not
16345 clever enough to find f<7>::a. */
16347 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16348 /*entering_scope=*/0);
16350 for (v
= TYPE_VALUES (enum_type
);
16352 v
= TREE_CHAIN (v
))
16353 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16354 return TREE_VALUE (v
);
16356 /* We didn't find the name. That should never happen; if
16357 name-lookup found it during preliminary parsing, we
16358 should find it again here during instantiation. */
16359 gcc_unreachable ();
16364 if (DECL_CONTEXT (t
))
16368 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16369 /*entering_scope=*/1);
16370 if (ctx
!= DECL_CONTEXT (t
))
16372 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16375 if (complain
& tf_error
)
16376 error ("using invalid field %qD", t
);
16377 return error_mark_node
;
16386 case FUNCTION_DECL
:
16387 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16388 r
= tsubst (t
, args
, complain
, in_decl
);
16389 else if (local_variable_p (t
)
16390 && uses_template_parms (DECL_CONTEXT (t
)))
16392 r
= retrieve_local_specialization (t
);
16393 if (r
== NULL_TREE
)
16395 /* First try name lookup to find the instantiation. */
16396 r
= lookup_name (DECL_NAME (t
));
16401 /* During error-recovery we may find a non-variable,
16402 even an OVERLOAD: just bail out and avoid ICEs and
16403 duplicate diagnostics (c++/62207). */
16404 gcc_assert (seen_error ());
16405 return error_mark_node
;
16407 if (!is_capture_proxy (r
))
16409 /* Make sure the one we found is the one we want. */
16410 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16411 if (ctx
!= DECL_CONTEXT (r
))
16420 /* This can happen for a variable used in a
16421 late-specified return type of a local lambda, or for a
16422 local static or constant. Building a new VAR_DECL
16423 should be OK in all those cases. */
16424 r
= tsubst_decl (t
, args
, complain
);
16425 if (local_specializations
)
16426 /* Avoid infinite recursion (79640). */
16427 register_local_specialization (r
, t
);
16428 if (decl_maybe_constant_var_p (r
))
16430 /* We can't call cp_finish_decl, so handle the
16431 initializer by hand. */
16432 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16433 complain
, in_decl
);
16434 if (!processing_template_decl
)
16435 init
= maybe_constant_init (init
);
16436 if (processing_template_decl
16437 ? potential_constant_expression (init
)
16438 : reduced_constant_expression_p (init
))
16439 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16440 = TREE_CONSTANT (r
) = true;
16441 DECL_INITIAL (r
) = init
;
16442 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16444 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16445 complain
, adc_variable_type
);
16447 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16448 || decl_constant_var_p (r
)
16450 if (!processing_template_decl
16451 && !TREE_STATIC (r
))
16452 r
= process_outer_var_ref (r
, complain
);
16454 /* Remember this for subsequent uses. */
16455 if (local_specializations
)
16456 register_local_specialization (r
, t
);
16458 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16459 r
= argument_pack_select_arg (r
);
16463 if (!mark_used (r
, complain
))
16464 return error_mark_node
;
16467 case NAMESPACE_DECL
:
16474 return tsubst_baselink (t
, current_nonlambda_class_type (),
16475 args
, complain
, in_decl
);
16477 case TEMPLATE_DECL
:
16478 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16479 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16480 args
, complain
, in_decl
);
16481 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16482 return tsubst (t
, args
, complain
, in_decl
);
16483 else if (DECL_CLASS_SCOPE_P (t
)
16484 && uses_template_parms (DECL_CONTEXT (t
)))
16486 /* Template template argument like the following example need
16489 template <template <class> class TT> struct C {};
16490 template <class T> struct D {
16491 template <class U> struct E {};
16496 We are processing the template argument `E' in #1 for
16497 the template instantiation #2. Originally, `E' is a
16498 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16499 have to substitute this with one having context `D<int>'. */
16501 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16502 if (dependent_scope_p (context
))
16504 /* When rewriting a constructor into a deduction guide, a
16505 non-dependent name can become dependent, so memtmpl<args>
16506 becomes context::template memtmpl<args>. */
16507 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16508 return build_qualified_name (type
, context
, DECL_NAME (t
),
16511 return lookup_field (context
, DECL_NAME(t
), 0, false);
16514 /* Ordinary template template argument. */
16517 case NON_LVALUE_EXPR
:
16518 case VIEW_CONVERT_EXPR
:
16520 /* Handle location wrappers by substituting the wrapped node
16521 first, *then* reusing the resulting type. Doing the type
16522 first ensures that we handle template parameters and
16523 parameter pack expansions. */
16524 if (location_wrapper_p (t
))
16526 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16527 complain
, in_decl
);
16528 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16530 tree op
= TREE_OPERAND (t
, 0);
16531 if (code
== VIEW_CONVERT_EXPR
16532 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16534 /* Wrapper to make a C++20 template parameter object const. */
16535 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16536 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16538 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16539 return build1 (code
, type
, op
);
16543 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16544 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16545 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16549 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16550 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16552 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16553 op
= build1 (code
, TREE_TYPE (op
), op
);
16554 REF_PARENTHESIZED_P (op
) = true;
16557 /* We shouldn't see any other uses of these in templates. */
16558 gcc_unreachable ();
16562 case REINTERPRET_CAST_EXPR
:
16563 case CONST_CAST_EXPR
:
16564 case STATIC_CAST_EXPR
:
16565 case DYNAMIC_CAST_EXPR
:
16566 case IMPLICIT_CONV_EXPR
:
16570 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16571 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16572 return build1 (code
, type
, op0
);
16576 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16577 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16579 tree expanded
, op
= TREE_OPERAND (t
, 0);
16582 if (SIZEOF_EXPR_TYPE_P (t
))
16583 op
= TREE_TYPE (op
);
16585 ++cp_unevaluated_operand
;
16586 ++c_inhibit_evaluation_warnings
;
16587 /* We only want to compute the number of arguments. */
16588 if (PACK_EXPANSION_P (op
))
16589 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16591 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16592 args
, complain
, in_decl
);
16593 --cp_unevaluated_operand
;
16594 --c_inhibit_evaluation_warnings
;
16596 if (TREE_CODE (expanded
) == TREE_VEC
)
16598 len
= TREE_VEC_LENGTH (expanded
);
16599 /* Set TREE_USED for the benefit of -Wunused. */
16600 for (int i
= 0; i
< len
; i
++)
16601 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16602 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16605 if (expanded
== error_mark_node
)
16606 return error_mark_node
;
16607 else if (PACK_EXPANSION_P (expanded
)
16608 || (TREE_CODE (expanded
) == TREE_VEC
16609 && pack_expansion_args_count (expanded
)))
16612 if (PACK_EXPANSION_P (expanded
))
16614 else if (TREE_VEC_LENGTH (expanded
) == 1)
16615 expanded
= TREE_VEC_ELT (expanded
, 0);
16617 expanded
= make_argument_pack (expanded
);
16619 if (TYPE_P (expanded
))
16620 return cxx_sizeof_or_alignof_type (input_location
,
16621 expanded
, SIZEOF_EXPR
,
16623 complain
& tf_error
);
16625 return cxx_sizeof_or_alignof_expr (input_location
,
16626 expanded
, SIZEOF_EXPR
,
16627 complain
& tf_error
);
16630 return build_int_cst (size_type_node
, len
);
16632 if (SIZEOF_EXPR_TYPE_P (t
))
16634 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16635 args
, complain
, in_decl
);
16636 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16637 r
= build1 (SIZEOF_EXPR
,
16638 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16639 SIZEOF_EXPR_TYPE_P (r
) = 1;
16646 case TRUTH_NOT_EXPR
:
16649 case UNARY_PLUS_EXPR
: /* Unary + */
16651 case AT_ENCODE_EXPR
:
16655 case REALPART_EXPR
:
16656 case IMAGPART_EXPR
:
16659 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16660 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16661 r
= build1 (code
, type
, op0
);
16662 if (code
== ALIGNOF_EXPR
)
16663 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16667 case COMPONENT_REF
:
16672 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16673 name
= TREE_OPERAND (t
, 1);
16674 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16676 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16677 complain
, in_decl
);
16678 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16680 else if (TREE_CODE (name
) == SCOPE_REF
16681 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16683 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16684 complain
, in_decl
);
16685 name
= TREE_OPERAND (name
, 1);
16686 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16687 complain
, in_decl
);
16688 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16689 name
= build_qualified_name (/*type=*/NULL_TREE
,
16691 /*template_p=*/false);
16693 else if (BASELINK_P (name
))
16694 name
= tsubst_baselink (name
,
16695 non_reference (TREE_TYPE (object
)),
16699 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16700 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16706 case TRUNC_DIV_EXPR
:
16707 case CEIL_DIV_EXPR
:
16708 case FLOOR_DIV_EXPR
:
16709 case ROUND_DIV_EXPR
:
16710 case EXACT_DIV_EXPR
:
16714 case TRUNC_MOD_EXPR
:
16715 case FLOOR_MOD_EXPR
:
16716 case TRUTH_ANDIF_EXPR
:
16717 case TRUTH_ORIF_EXPR
:
16718 case TRUTH_AND_EXPR
:
16719 case TRUTH_OR_EXPR
:
16730 case COMPOUND_EXPR
:
16733 case PREDECREMENT_EXPR
:
16734 case PREINCREMENT_EXPR
:
16735 case POSTDECREMENT_EXPR
:
16736 case POSTINCREMENT_EXPR
:
16738 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16739 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16740 return build_nt (code
, op0
, op1
);
16745 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16746 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16747 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16748 QUALIFIED_NAME_IS_TEMPLATE (t
));
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 (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16760 int n
= VL_EXP_OPERAND_LENGTH (t
);
16761 tree result
= build_vl_exp (CALL_EXPR
, n
);
16763 for (i
= 0; i
< n
; i
++)
16764 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16765 complain
, in_decl
);
16771 case PSEUDO_DTOR_EXPR
:
16772 case VEC_PERM_EXPR
:
16774 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16775 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16776 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16777 r
= build_nt (code
, op0
, op1
, op2
);
16778 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16784 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16785 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16786 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16787 r
= build_nt (code
, op0
, op1
, op2
);
16788 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16794 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16795 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16796 r
= build_nt (code
, op0
, op1
);
16797 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16798 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16802 case TEMPLATE_ID_EXPR
:
16804 /* Substituted template arguments */
16805 tree fn
= TREE_OPERAND (t
, 0);
16806 tree targs
= TREE_OPERAND (t
, 1);
16808 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16810 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16812 return lookup_template_function (fn
, targs
);
16817 tree purpose
, value
, chain
;
16819 if (t
== void_list_node
)
16822 purpose
= TREE_PURPOSE (t
);
16824 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16825 value
= TREE_VALUE (t
);
16827 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16828 chain
= TREE_CHAIN (t
);
16829 if (chain
&& chain
!= void_type_node
)
16830 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16831 if (purpose
== TREE_PURPOSE (t
)
16832 && value
== TREE_VALUE (t
)
16833 && chain
== TREE_CHAIN (t
))
16835 return tree_cons (purpose
, value
, chain
);
16840 case ENUMERAL_TYPE
:
16842 case TEMPLATE_TYPE_PARM
:
16843 case TEMPLATE_TEMPLATE_PARM
:
16844 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16845 case TEMPLATE_PARM_INDEX
:
16847 case REFERENCE_TYPE
:
16849 case FUNCTION_TYPE
:
16852 case TYPENAME_TYPE
:
16853 case UNBOUND_CLASS_TEMPLATE
:
16855 case DECLTYPE_TYPE
:
16857 return tsubst (t
, args
, complain
, in_decl
);
16861 /* Fall through. */
16862 case IDENTIFIER_NODE
:
16863 if (IDENTIFIER_CONV_OP_P (t
))
16865 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16866 return make_conv_op_name (new_type
);
16872 /* This is handled by tsubst_copy_and_build. */
16873 gcc_unreachable ();
16877 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16878 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16879 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16882 case CLEANUP_POINT_EXPR
:
16883 /* We shouldn't have built any of these during initial template
16884 generation. Instead, they should be built during instantiation
16885 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16886 gcc_unreachable ();
16890 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16891 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16892 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16893 r
= build2 (code
, type
, op0
, op1
);
16894 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16895 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16896 && !(complain
& tf_error
))
16897 return error_mark_node
;
16901 case EXPR_PACK_EXPANSION
:
16902 error ("invalid use of pack expansion expression");
16903 return error_mark_node
;
16905 case NONTYPE_ARGUMENT_PACK
:
16906 error ("use %<...%> to expand argument pack");
16907 return error_mark_node
;
16910 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16917 /* Instantiate any typedefs in the type. */
16918 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16919 r
= fold_convert (type
, t
);
16920 gcc_assert (TREE_CODE (r
) == code
);
16926 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16928 if (type
!= TREE_TYPE (t
))
16931 TREE_TYPE (r
) = type
;
16937 /* These can sometimes show up in a partial instantiation, but never
16938 involve template parms. */
16939 gcc_assert (!uses_template_parms (t
));
16942 case UNARY_LEFT_FOLD_EXPR
:
16943 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16944 case UNARY_RIGHT_FOLD_EXPR
:
16945 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16946 case BINARY_LEFT_FOLD_EXPR
:
16947 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16948 case BINARY_RIGHT_FOLD_EXPR
:
16949 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16953 case DEBUG_BEGIN_STMT
:
16954 /* ??? There's no point in copying it for now, but maybe some
16955 day it will contain more information, such as a pointer back
16956 to the containing function, inlined copy or so. */
16959 case CO_AWAIT_EXPR
:
16960 return tsubst_expr (t
, args
, complain
, in_decl
,
16961 /*integral_constant_expression_p=*/false);
16965 /* We shouldn't get here, but keep going if !flag_checking. */
16967 gcc_unreachable ();
16972 /* Helper function for tsubst_omp_clauses, used for instantiation of
16973 OMP_CLAUSE_DECL of clauses. */
16976 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16977 tree in_decl
, tree
*iterator_cache
)
16979 if (decl
== NULL_TREE
)
16982 /* Handle OpenMP iterators. */
16983 if (TREE_CODE (decl
) == TREE_LIST
16984 && TREE_PURPOSE (decl
)
16985 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16988 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16989 ret
= iterator_cache
[1];
16993 begin_scope (sk_omp
, NULL
);
16994 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16996 *tp
= copy_node (it
);
16997 TREE_VEC_ELT (*tp
, 0)
16998 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16999 TREE_VEC_ELT (*tp
, 1)
17000 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17001 /*integral_constant_expression_p=*/false);
17002 TREE_VEC_ELT (*tp
, 2)
17003 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17004 /*integral_constant_expression_p=*/false);
17005 TREE_VEC_ELT (*tp
, 3)
17006 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17007 /*integral_constant_expression_p=*/false);
17008 TREE_CHAIN (*tp
) = NULL_TREE
;
17009 tp
= &TREE_CHAIN (*tp
);
17011 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17012 iterator_cache
[0] = TREE_PURPOSE (decl
);
17013 iterator_cache
[1] = ret
;
17015 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17020 /* Handle an OpenMP array section represented as a TREE_LIST (or
17021 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17022 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17023 TREE_LIST. We can handle it exactly the same as an array section
17024 (purpose, value, and a chain), even though the nomenclature
17025 (low_bound, length, etc) is different. */
17026 if (TREE_CODE (decl
) == TREE_LIST
)
17029 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17030 /*integral_constant_expression_p=*/false);
17031 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17032 /*integral_constant_expression_p=*/false);
17033 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17035 if (TREE_PURPOSE (decl
) == low_bound
17036 && TREE_VALUE (decl
) == length
17037 && TREE_CHAIN (decl
) == chain
)
17039 tree ret
= tree_cons (low_bound
, length
, chain
);
17040 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17041 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17044 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17045 /*integral_constant_expression_p=*/false);
17046 /* Undo convert_from_reference tsubst_expr could have called. */
17048 && REFERENCE_REF_P (ret
)
17049 && !REFERENCE_REF_P (decl
))
17050 ret
= TREE_OPERAND (ret
, 0);
17054 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17057 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17058 tree args
, tsubst_flags_t complain
, tree in_decl
)
17060 tree new_clauses
= NULL_TREE
, nc
, oc
;
17061 tree linear_no_step
= NULL_TREE
;
17062 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17064 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17066 nc
= copy_node (oc
);
17067 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17070 switch (OMP_CLAUSE_CODE (nc
))
17072 case OMP_CLAUSE_LASTPRIVATE
:
17073 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17075 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17076 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17077 in_decl
, /*integral_constant_expression_p=*/false);
17078 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17079 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17082 case OMP_CLAUSE_PRIVATE
:
17083 case OMP_CLAUSE_SHARED
:
17084 case OMP_CLAUSE_FIRSTPRIVATE
:
17085 case OMP_CLAUSE_COPYIN
:
17086 case OMP_CLAUSE_COPYPRIVATE
:
17087 case OMP_CLAUSE_UNIFORM
:
17088 case OMP_CLAUSE_DEPEND
:
17089 case OMP_CLAUSE_FROM
:
17090 case OMP_CLAUSE_TO
:
17091 case OMP_CLAUSE_MAP
:
17092 case OMP_CLAUSE_NONTEMPORAL
:
17093 case OMP_CLAUSE_USE_DEVICE_PTR
:
17094 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17095 case OMP_CLAUSE_IS_DEVICE_PTR
:
17096 case OMP_CLAUSE_INCLUSIVE
:
17097 case OMP_CLAUSE_EXCLUSIVE
:
17098 OMP_CLAUSE_DECL (nc
)
17099 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17100 in_decl
, iterator_cache
);
17102 case OMP_CLAUSE_TILE
:
17103 case OMP_CLAUSE_IF
:
17104 case OMP_CLAUSE_NUM_THREADS
:
17105 case OMP_CLAUSE_SCHEDULE
:
17106 case OMP_CLAUSE_COLLAPSE
:
17107 case OMP_CLAUSE_FINAL
:
17108 case OMP_CLAUSE_DEVICE
:
17109 case OMP_CLAUSE_DIST_SCHEDULE
:
17110 case OMP_CLAUSE_NUM_TEAMS
:
17111 case OMP_CLAUSE_THREAD_LIMIT
:
17112 case OMP_CLAUSE_SAFELEN
:
17113 case OMP_CLAUSE_SIMDLEN
:
17114 case OMP_CLAUSE_NUM_TASKS
:
17115 case OMP_CLAUSE_GRAINSIZE
:
17116 case OMP_CLAUSE_PRIORITY
:
17117 case OMP_CLAUSE_ORDERED
:
17118 case OMP_CLAUSE_HINT
:
17119 case OMP_CLAUSE_NUM_GANGS
:
17120 case OMP_CLAUSE_NUM_WORKERS
:
17121 case OMP_CLAUSE_VECTOR_LENGTH
:
17122 case OMP_CLAUSE_WORKER
:
17123 case OMP_CLAUSE_VECTOR
:
17124 case OMP_CLAUSE_ASYNC
:
17125 case OMP_CLAUSE_WAIT
:
17126 OMP_CLAUSE_OPERAND (nc
, 0)
17127 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17128 in_decl
, /*integral_constant_expression_p=*/false);
17130 case OMP_CLAUSE_REDUCTION
:
17131 case OMP_CLAUSE_IN_REDUCTION
:
17132 case OMP_CLAUSE_TASK_REDUCTION
:
17133 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17135 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17136 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17138 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17139 complain
, in_decl
);
17140 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17141 = build_qualified_name (NULL_TREE
, scope
,
17142 TREE_OPERAND (placeholder
, 1),
17146 gcc_assert (identifier_p (placeholder
));
17148 OMP_CLAUSE_DECL (nc
)
17149 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17152 case OMP_CLAUSE_GANG
:
17153 case OMP_CLAUSE_ALIGNED
:
17154 OMP_CLAUSE_DECL (nc
)
17155 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17157 OMP_CLAUSE_OPERAND (nc
, 1)
17158 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17159 in_decl
, /*integral_constant_expression_p=*/false);
17161 case OMP_CLAUSE_LINEAR
:
17162 OMP_CLAUSE_DECL (nc
)
17163 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17165 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17167 gcc_assert (!linear_no_step
);
17168 linear_no_step
= nc
;
17170 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17171 OMP_CLAUSE_LINEAR_STEP (nc
)
17172 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17173 complain
, in_decl
, NULL
);
17175 OMP_CLAUSE_LINEAR_STEP (nc
)
17176 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17178 /*integral_constant_expression_p=*/false);
17180 case OMP_CLAUSE_NOWAIT
:
17181 case OMP_CLAUSE_DEFAULT
:
17182 case OMP_CLAUSE_UNTIED
:
17183 case OMP_CLAUSE_MERGEABLE
:
17184 case OMP_CLAUSE_INBRANCH
:
17185 case OMP_CLAUSE_NOTINBRANCH
:
17186 case OMP_CLAUSE_PROC_BIND
:
17187 case OMP_CLAUSE_FOR
:
17188 case OMP_CLAUSE_PARALLEL
:
17189 case OMP_CLAUSE_SECTIONS
:
17190 case OMP_CLAUSE_TASKGROUP
:
17191 case OMP_CLAUSE_NOGROUP
:
17192 case OMP_CLAUSE_THREADS
:
17193 case OMP_CLAUSE_SIMD
:
17194 case OMP_CLAUSE_DEFAULTMAP
:
17195 case OMP_CLAUSE_ORDER
:
17196 case OMP_CLAUSE_BIND
:
17197 case OMP_CLAUSE_INDEPENDENT
:
17198 case OMP_CLAUSE_AUTO
:
17199 case OMP_CLAUSE_SEQ
:
17200 case OMP_CLAUSE_IF_PRESENT
:
17201 case OMP_CLAUSE_FINALIZE
:
17204 gcc_unreachable ();
17206 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17207 switch (OMP_CLAUSE_CODE (nc
))
17209 case OMP_CLAUSE_SHARED
:
17210 case OMP_CLAUSE_PRIVATE
:
17211 case OMP_CLAUSE_FIRSTPRIVATE
:
17212 case OMP_CLAUSE_LASTPRIVATE
:
17213 case OMP_CLAUSE_COPYPRIVATE
:
17214 case OMP_CLAUSE_LINEAR
:
17215 case OMP_CLAUSE_REDUCTION
:
17216 case OMP_CLAUSE_IN_REDUCTION
:
17217 case OMP_CLAUSE_TASK_REDUCTION
:
17218 case OMP_CLAUSE_USE_DEVICE_PTR
:
17219 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17220 case OMP_CLAUSE_IS_DEVICE_PTR
:
17221 case OMP_CLAUSE_INCLUSIVE
:
17222 case OMP_CLAUSE_EXCLUSIVE
:
17223 /* tsubst_expr on SCOPE_REF results in returning
17224 finish_non_static_data_member result. Undo that here. */
17225 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17226 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17227 == IDENTIFIER_NODE
))
17229 tree t
= OMP_CLAUSE_DECL (nc
);
17232 switch (TREE_CODE (v
))
17234 case COMPONENT_REF
:
17238 case POINTER_PLUS_EXPR
:
17239 v
= TREE_OPERAND (v
, 0);
17242 if (DECL_CONTEXT (v
) == current_function_decl
17243 && DECL_ARTIFICIAL (v
)
17244 && DECL_NAME (v
) == this_identifier
)
17245 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17252 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17253 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17254 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17255 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17256 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17258 tree decl
= OMP_CLAUSE_DECL (nc
);
17261 retrofit_lang_decl (decl
);
17262 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17271 new_clauses
= nreverse (new_clauses
);
17272 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17274 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17275 if (linear_no_step
)
17276 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17277 if (nc
== linear_no_step
)
17279 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17283 return new_clauses
;
17286 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17289 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17292 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17294 tree purpose
, value
, chain
;
17299 if (TREE_CODE (t
) != TREE_LIST
)
17300 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17301 /*function_p=*/false,
17302 /*integral_constant_expression_p=*/false);
17304 if (t
== void_list_node
)
17307 purpose
= TREE_PURPOSE (t
);
17309 purpose
= RECUR (purpose
);
17310 value
= TREE_VALUE (t
);
17313 if (TREE_CODE (value
) != LABEL_DECL
)
17314 value
= RECUR (value
);
17317 value
= lookup_label (DECL_NAME (value
));
17318 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17319 TREE_USED (value
) = 1;
17322 chain
= TREE_CHAIN (t
);
17323 if (chain
&& chain
!= void_type_node
)
17324 chain
= RECUR (chain
);
17325 return tree_cons (purpose
, value
, chain
);
17329 /* Used to temporarily communicate the list of #pragma omp parallel
17330 clauses to #pragma omp for instantiation if they are combined
17333 static tree
*omp_parallel_combined_clauses
;
17335 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17336 tree
*, unsigned int *);
17338 /* Substitute one OMP_FOR iterator. */
17341 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17342 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17343 tree args
, tsubst_flags_t complain
, tree in_decl
,
17344 bool integral_constant_expression_p
)
17346 #define RECUR(NODE) \
17347 tsubst_expr ((NODE), args, complain, in_decl, \
17348 integral_constant_expression_p)
17349 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17352 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17353 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17355 decl
= TREE_OPERAND (init
, 0);
17356 init
= TREE_OPERAND (init
, 1);
17357 tree decl_expr
= NULL_TREE
;
17358 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17361 bool decomp
= false;
17362 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17364 tree v
= DECL_VALUE_EXPR (decl
);
17365 if (TREE_CODE (v
) == ARRAY_REF
17366 && VAR_P (TREE_OPERAND (v
, 0))
17367 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17369 tree decomp_first
= NULL_TREE
;
17370 unsigned decomp_cnt
= 0;
17371 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17372 maybe_push_decl (d
);
17373 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17374 in_decl
, &decomp_first
, &decomp_cnt
);
17376 if (d
== error_mark_node
)
17377 decl
= error_mark_node
;
17379 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17381 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17383 tree v
= build_nt (ARRAY_REF
, d
,
17384 size_int (decomp_cnt
- i
- 1),
17385 NULL_TREE
, NULL_TREE
);
17386 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17387 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17389 fit_decomposition_lang_decl (decomp_first
, d
);
17390 decomp_first
= DECL_CHAIN (decomp_first
);
17394 decl
= tsubst_decl (decl
, args
, complain
);
17396 maybe_push_decl (decl
);
17398 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17400 /* We need to jump through some hoops to handle declarations in the
17401 init-statement, since we might need to handle auto deduction,
17402 but we need to keep control of initialization. */
17404 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17405 decl
= tsubst_decl (decl
, args
, complain
);
17409 if (TREE_CODE (decl
) == SCOPE_REF
)
17411 decl
= RECUR (decl
);
17412 if (TREE_CODE (decl
) == COMPONENT_REF
)
17416 switch (TREE_CODE (v
))
17418 case COMPONENT_REF
:
17422 case POINTER_PLUS_EXPR
:
17423 v
= TREE_OPERAND (v
, 0);
17426 if (DECL_CONTEXT (v
) == current_function_decl
17427 && DECL_ARTIFICIAL (v
)
17428 && DECL_NAME (v
) == this_identifier
)
17430 decl
= TREE_OPERAND (decl
, 1);
17431 decl
= omp_privatize_field (decl
, false);
17441 decl
= RECUR (decl
);
17443 init
= RECUR (init
);
17445 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17447 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17448 if (TREE_CODE (o
) == TREE_LIST
)
17449 TREE_VEC_ELT (orig_declv
, i
)
17450 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17451 RECUR (TREE_VALUE (o
)),
17454 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17459 tree this_pre_body
= NULL_TREE
;
17460 tree orig_init
= NULL_TREE
;
17461 tree orig_decl
= NULL_TREE
;
17462 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17463 orig_init
, cond
, incr
);
17466 if (orig_declv
== NULL_TREE
)
17467 orig_declv
= copy_node (declv
);
17468 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17471 else if (orig_declv
)
17472 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17475 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17476 if (!range_for
&& auto_node
&& init
)
17478 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17480 gcc_assert (!type_dependent_expression_p (decl
));
17482 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17486 /* Declare the variable, but don't let that initialize it. */
17487 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17488 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17490 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17495 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
17496 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17497 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17499 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17500 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17501 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17502 NOP_EXPR
, rhs
, complain
);
17505 incr
= RECUR (incr
);
17506 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17507 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17509 TREE_VEC_ELT (declv
, i
) = decl
;
17510 TREE_VEC_ELT (initv
, i
) = init
;
17511 TREE_VEC_ELT (condv
, i
) = cond
;
17512 TREE_VEC_ELT (incrv
, i
) = incr
;
17518 /* Declare and initialize the variable. */
17526 for (j
= ((omp_parallel_combined_clauses
== NULL
17527 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17529 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17531 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17532 && OMP_CLAUSE_DECL (*pc
) == decl
)
17534 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17535 && OMP_CLAUSE_DECL (*pc
) == decl
)
17539 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17541 *pc
= OMP_CLAUSE_CHAIN (c
);
17542 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17545 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17546 && OMP_CLAUSE_DECL (*pc
) == decl
)
17548 error ("iteration variable %qD should not be firstprivate",
17550 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17552 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17553 && OMP_CLAUSE_DECL (*pc
) == decl
)
17555 error ("iteration variable %qD should not be reduction",
17557 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17560 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17565 if (*pc
== NULL_TREE
)
17567 tree c
= build_omp_clause (input_location
,
17568 TREE_CODE (t
) == OMP_LOOP
17569 ? OMP_CLAUSE_LASTPRIVATE
17570 : OMP_CLAUSE_PRIVATE
);
17571 OMP_CLAUSE_DECL (c
) = decl
;
17572 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17575 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17580 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17581 if (COMPARISON_CLASS_P (cond
))
17583 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17584 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17585 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17588 cond
= RECUR (cond
);
17589 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17590 switch (TREE_CODE (incr
))
17592 case PREINCREMENT_EXPR
:
17593 case PREDECREMENT_EXPR
:
17594 case POSTINCREMENT_EXPR
:
17595 case POSTDECREMENT_EXPR
:
17596 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17597 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17600 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17601 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17603 tree rhs
= TREE_OPERAND (incr
, 1);
17604 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17605 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17606 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17607 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17608 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17612 incr
= RECUR (incr
);
17615 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17616 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17618 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17619 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17620 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17621 TREE_TYPE (decl
), lhs
,
17622 RECUR (TREE_OPERAND (incr
, 2))));
17624 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17625 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17626 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17628 tree rhs
= TREE_OPERAND (incr
, 2);
17629 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17630 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17631 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17632 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17633 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17637 incr
= RECUR (incr
);
17640 incr
= RECUR (incr
);
17644 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17645 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17646 TREE_VEC_ELT (declv
, i
) = decl
;
17647 TREE_VEC_ELT (initv
, i
) = init
;
17648 TREE_VEC_ELT (condv
, i
) = cond
;
17649 TREE_VEC_ELT (incrv
, i
) = incr
;
17654 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17655 of OMP_TARGET's body. */
17658 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17660 *walk_subtrees
= 0;
17661 switch (TREE_CODE (*tp
))
17666 case STATEMENT_LIST
:
17667 *walk_subtrees
= 1;
17675 /* Helper function for tsubst_expr. For decomposition declaration
17676 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17677 also the corresponding decls representing the identifiers
17678 of the decomposition declaration. Return DECL if successful
17679 or error_mark_node otherwise, set *FIRST to the first decl
17680 in the list chained through DECL_CHAIN and *CNT to the number
17684 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17685 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17688 tree decl2
, decl3
, prev
= decl
;
17690 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17691 for (decl2
= DECL_CHAIN (pattern_decl
);
17694 && DECL_DECOMPOSITION_P (decl2
)
17695 && DECL_NAME (decl2
);
17696 decl2
= DECL_CHAIN (decl2
))
17698 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17700 gcc_assert (errorcount
);
17701 return error_mark_node
;
17704 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17705 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17706 tree v
= DECL_VALUE_EXPR (decl2
);
17707 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17708 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17709 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17710 SET_DECL_VALUE_EXPR (decl2
, v
);
17711 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17713 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17716 gcc_assert (errorcount
);
17717 decl
= error_mark_node
;
17720 maybe_push_decl (decl3
);
17721 if (error_operand_p (decl3
))
17722 decl
= error_mark_node
;
17723 else if (decl
!= error_mark_node
17724 && DECL_CHAIN (decl3
) != prev
17727 gcc_assert (errorcount
);
17728 decl
= error_mark_node
;
17737 /* Return the proper local_specialization for init-capture pack DECL. */
17740 lookup_init_capture_pack (tree decl
)
17742 /* We handle normal pack captures by forwarding to the specialization of the
17743 captured parameter. We can't do that for pack init-captures; we need them
17744 to have their own local_specialization. We created the individual
17745 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17746 when we process the DECL_EXPR for the pack init-capture in the template.
17747 So, how do we find them? We don't know the capture proxy pack when
17748 building the individual resulting proxies, and we don't know the
17749 individual proxies when instantiating the pack. What we have in common is
17752 So...when we instantiate the FIELD_DECL, we stick the result in
17753 local_specializations. Then at the DECL_EXPR we look up that result, see
17754 how many elements it has, synthesize the names, and look them up. */
17756 tree cname
= DECL_NAME (decl
);
17757 tree val
= DECL_VALUE_EXPR (decl
);
17758 tree field
= TREE_OPERAND (val
, 1);
17759 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17760 tree fpack
= retrieve_local_specialization (field
);
17761 if (fpack
== error_mark_node
)
17762 return error_mark_node
;
17765 tree vec
= NULL_TREE
;
17766 tree r
= NULL_TREE
;
17767 if (TREE_CODE (fpack
) == TREE_VEC
)
17769 len
= TREE_VEC_LENGTH (fpack
);
17770 vec
= make_tree_vec (len
);
17771 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17772 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17774 for (int i
= 0; i
< len
; ++i
)
17776 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17777 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17779 TREE_VEC_ELT (vec
, i
) = elt
;
17786 /* Like tsubst_copy for expressions, etc. but also does semantic
17790 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17791 bool integral_constant_expression_p
)
17793 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17794 #define RECUR(NODE) \
17795 tsubst_expr ((NODE), args, complain, in_decl, \
17796 integral_constant_expression_p)
17802 if (t
== NULL_TREE
|| t
== error_mark_node
)
17805 loc
= input_location
;
17806 if (location_t eloc
= cp_expr_location (t
))
17807 input_location
= eloc
;
17808 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17809 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17811 switch (TREE_CODE (t
))
17813 case STATEMENT_LIST
:
17815 tree_stmt_iterator i
;
17816 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17817 RECUR (tsi_stmt (i
));
17821 case CTOR_INITIALIZER
:
17822 finish_mem_initializers (tsubst_initializer_list
17823 (TREE_OPERAND (t
, 0), args
));
17827 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17830 case CO_RETURN_EXPR
:
17831 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
17834 case CO_YIELD_EXPR
:
17835 stmt
= finish_co_yield_expr (input_location
,
17836 RECUR (TREE_OPERAND (t
, 0)));
17840 case CO_AWAIT_EXPR
:
17841 stmt
= finish_co_await_expr (input_location
,
17842 RECUR (TREE_OPERAND (t
, 0)));
17847 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17848 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17849 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17851 finish_expr_stmt (tmp
);
17855 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17860 tree decl
, pattern_decl
;
17863 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17864 if (TREE_CODE (decl
) == LABEL_DECL
)
17865 finish_label_decl (DECL_NAME (decl
));
17866 else if (TREE_CODE (decl
) == USING_DECL
)
17868 tree scope
= USING_DECL_SCOPE (decl
);
17869 tree name
= DECL_NAME (decl
);
17871 scope
= tsubst (scope
, args
, complain
, in_decl
);
17872 finish_nonmember_using_decl (scope
, name
);
17874 else if (is_capture_proxy (decl
)
17875 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17877 /* We're in tsubst_lambda_expr, we've already inserted a new
17878 capture proxy, so look it up and register it. */
17880 if (!DECL_PACK_P (decl
))
17882 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17883 /*nonclass*/1, /*block_p=*/true,
17884 /*ns_only*/0, LOOKUP_HIDDEN
);
17885 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17887 else if (is_normal_capture_proxy (decl
))
17889 inst
= (retrieve_local_specialization
17890 (DECL_CAPTURED_VARIABLE (decl
)));
17891 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
17892 || DECL_PACK_P (inst
));
17895 inst
= lookup_init_capture_pack (decl
);
17897 register_local_specialization (inst
, decl
);
17900 else if (DECL_PRETTY_FUNCTION_P (decl
))
17901 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17903 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17904 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17905 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17906 /* Don't copy the old closure; we'll create a new one in
17907 tsubst_lambda_expr. */
17911 init
= DECL_INITIAL (decl
);
17912 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17913 for local variables, so save if DECL was declared constinit. */
17914 const bool constinit_p
17916 && DECL_LANG_SPECIFIC (decl
)
17917 && DECL_TEMPLATE_INFO (decl
)
17918 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17919 decl
= tsubst (decl
, args
, complain
, in_decl
);
17920 if (decl
!= error_mark_node
)
17922 /* By marking the declaration as instantiated, we avoid
17923 trying to instantiate it. Since instantiate_decl can't
17924 handle local variables, and since we've already done
17925 all that needs to be done, that's the right thing to
17928 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17929 if (VAR_P (decl
) && !DECL_NAME (decl
)
17930 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17931 /* Anonymous aggregates are a special case. */
17932 finish_anon_union (decl
);
17933 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17935 DECL_CONTEXT (decl
) = current_function_decl
;
17936 if (DECL_NAME (decl
) == this_identifier
)
17938 tree lam
= DECL_CONTEXT (current_function_decl
);
17939 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17940 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17942 insert_capture_proxy (decl
);
17944 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17945 /* We already did a pushtag. */;
17946 else if (TREE_CODE (decl
) == FUNCTION_DECL
17947 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17948 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17950 DECL_CONTEXT (decl
) = NULL_TREE
;
17952 DECL_CONTEXT (decl
) = current_function_decl
;
17953 cp_check_omp_declare_reduction (decl
);
17957 bool const_init
= false;
17958 unsigned int cnt
= 0;
17959 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17960 maybe_push_decl (decl
);
17963 && DECL_DECOMPOSITION_P (decl
)
17964 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17965 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17966 complain
, in_decl
, &first
,
17969 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17972 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17975 if (ndecl
!= error_mark_node
)
17976 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17978 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17979 constinit_p
? LOOKUP_CONSTINIT
: 0);
17981 if (ndecl
!= error_mark_node
)
17982 cp_finish_decomp (ndecl
, first
, cnt
);
17991 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17992 RECUR (FOR_INIT_STMT (t
));
17993 finish_init_stmt (stmt
);
17994 tmp
= RECUR (FOR_COND (t
));
17995 finish_for_cond (tmp
, stmt
, false, 0);
17996 tmp
= RECUR (FOR_EXPR (t
));
17997 finish_for_expr (tmp
, stmt
);
17999 bool prev
= note_iteration_stmt_body_start ();
18000 RECUR (FOR_BODY (t
));
18001 note_iteration_stmt_body_end (prev
);
18003 finish_for_stmt (stmt
);
18006 case RANGE_FOR_STMT
:
18008 /* Construct another range_for, if this is not a final
18009 substitution (for inside a generic lambda of a
18010 template). Otherwise convert to a regular for. */
18012 stmt
= (processing_template_decl
18013 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18014 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18015 RECUR (RANGE_FOR_INIT_STMT (t
));
18016 decl
= RANGE_FOR_DECL (t
);
18017 decl
= tsubst (decl
, args
, complain
, in_decl
);
18018 maybe_push_decl (decl
);
18019 expr
= RECUR (RANGE_FOR_EXPR (t
));
18021 tree decomp_first
= NULL_TREE
;
18022 unsigned decomp_cnt
= 0;
18023 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18024 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18026 &decomp_first
, &decomp_cnt
);
18028 if (processing_template_decl
)
18030 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18031 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18032 finish_range_for_decl (stmt
, decl
, expr
);
18033 if (decomp_first
&& decl
!= error_mark_node
)
18034 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18038 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18039 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18040 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18041 decomp_first
, decomp_cnt
,
18042 RANGE_FOR_IVDEP (t
), unroll
);
18045 bool prev
= note_iteration_stmt_body_start ();
18046 RECUR (RANGE_FOR_BODY (t
));
18047 note_iteration_stmt_body_end (prev
);
18048 finish_for_stmt (stmt
);
18053 stmt
= begin_while_stmt ();
18054 tmp
= RECUR (WHILE_COND (t
));
18055 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18057 bool prev
= note_iteration_stmt_body_start ();
18058 RECUR (WHILE_BODY (t
));
18059 note_iteration_stmt_body_end (prev
);
18061 finish_while_stmt (stmt
);
18065 stmt
= begin_do_stmt ();
18067 bool prev
= note_iteration_stmt_body_start ();
18068 RECUR (DO_BODY (t
));
18069 note_iteration_stmt_body_end (prev
);
18071 finish_do_body (stmt
);
18072 tmp
= RECUR (DO_COND (t
));
18073 finish_do_stmt (tmp
, stmt
, false, 0);
18077 stmt
= begin_if_stmt ();
18078 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18079 if (IF_STMT_CONSTEXPR_P (t
))
18080 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18081 tmp
= RECUR (IF_COND (t
));
18082 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18083 if (IF_STMT_CONSTEXPR_P (t
)
18084 && instantiation_dependent_expression_p (tmp
))
18086 /* We're partially instantiating a generic lambda, but the condition
18087 of the constexpr if is still dependent. Don't substitute into the
18088 branches now, just remember the template arguments. */
18089 do_poplevel (IF_SCOPE (stmt
));
18090 IF_COND (stmt
) = IF_COND (t
);
18091 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18092 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18093 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18097 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18098 /* Don't instantiate the THEN_CLAUSE. */;
18101 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18102 bool inhibit
= integer_zerop (folded
);
18104 ++c_inhibit_evaluation_warnings
;
18105 RECUR (THEN_CLAUSE (t
));
18107 --c_inhibit_evaluation_warnings
;
18109 finish_then_clause (stmt
);
18111 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18112 /* Don't instantiate the ELSE_CLAUSE. */;
18113 else if (ELSE_CLAUSE (t
))
18115 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18116 bool inhibit
= integer_nonzerop (folded
);
18117 begin_else_clause (stmt
);
18119 ++c_inhibit_evaluation_warnings
;
18120 RECUR (ELSE_CLAUSE (t
));
18122 --c_inhibit_evaluation_warnings
;
18123 finish_else_clause (stmt
);
18126 finish_if_stmt (stmt
);
18130 if (BIND_EXPR_BODY_BLOCK (t
))
18131 stmt
= begin_function_body ();
18133 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18134 ? BCS_TRY_BLOCK
: 0);
18136 RECUR (BIND_EXPR_BODY (t
));
18138 if (BIND_EXPR_BODY_BLOCK (t
))
18139 finish_function_body (stmt
);
18141 finish_compound_stmt (stmt
);
18145 finish_break_stmt ();
18148 case CONTINUE_STMT
:
18149 finish_continue_stmt ();
18153 stmt
= begin_switch_stmt ();
18154 tmp
= RECUR (SWITCH_STMT_COND (t
));
18155 finish_switch_cond (tmp
, stmt
);
18156 RECUR (SWITCH_STMT_BODY (t
));
18157 finish_switch_stmt (stmt
);
18160 case CASE_LABEL_EXPR
:
18162 tree decl
= CASE_LABEL (t
);
18163 tree low
= RECUR (CASE_LOW (t
));
18164 tree high
= RECUR (CASE_HIGH (t
));
18165 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18166 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18168 tree label
= CASE_LABEL (l
);
18169 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18170 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18171 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18178 tree decl
= LABEL_EXPR_LABEL (t
);
18181 label
= finish_label_stmt (DECL_NAME (decl
));
18182 if (TREE_CODE (label
) == LABEL_DECL
)
18183 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18184 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18185 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18190 tmp
= GOTO_DESTINATION (t
);
18191 if (TREE_CODE (tmp
) != LABEL_DECL
)
18192 /* Computed goto's must be tsubst'd into. On the other hand,
18193 non-computed gotos must not be; the identifier in question
18194 will have no binding. */
18197 tmp
= DECL_NAME (tmp
);
18198 finish_goto_stmt (tmp
);
18203 tree string
= RECUR (ASM_STRING (t
));
18204 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18205 complain
, in_decl
);
18206 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18207 complain
, in_decl
);
18208 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18209 complain
, in_decl
);
18210 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18211 complain
, in_decl
);
18212 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18213 outputs
, inputs
, clobbers
, labels
,
18215 tree asm_expr
= tmp
;
18216 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18217 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18218 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18225 stmt
= begin_try_block ();
18226 RECUR (TRY_STMTS (t
));
18227 finish_cleanup_try_block (stmt
);
18228 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18232 tree compound_stmt
= NULL_TREE
;
18234 if (FN_TRY_BLOCK_P (t
))
18235 stmt
= begin_function_try_block (&compound_stmt
);
18237 stmt
= begin_try_block ();
18239 RECUR (TRY_STMTS (t
));
18241 if (FN_TRY_BLOCK_P (t
))
18242 finish_function_try_block (stmt
);
18244 finish_try_block (stmt
);
18246 RECUR (TRY_HANDLERS (t
));
18247 if (FN_TRY_BLOCK_P (t
))
18248 finish_function_handler_sequence (stmt
, compound_stmt
);
18250 finish_handler_sequence (stmt
);
18256 tree decl
= HANDLER_PARMS (t
);
18260 decl
= tsubst (decl
, args
, complain
, in_decl
);
18261 /* Prevent instantiate_decl from trying to instantiate
18262 this variable. We've already done all that needs to be
18264 if (decl
!= error_mark_node
)
18265 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18267 stmt
= begin_handler ();
18268 finish_handler_parms (decl
, stmt
);
18269 RECUR (HANDLER_BODY (t
));
18270 finish_handler (stmt
);
18275 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18276 if (CLASS_TYPE_P (tmp
))
18278 /* Local classes are not independent templates; they are
18279 instantiated along with their containing function. And this
18280 way we don't have to deal with pushing out of one local class
18281 to instantiate a member of another local class. */
18282 /* Closures are handled by the LAMBDA_EXPR. */
18283 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18284 complete_type (tmp
);
18285 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18287 || (TREE_CODE (fld
) == FUNCTION_DECL
18288 && !DECL_ARTIFICIAL (fld
)))
18289 && DECL_TEMPLATE_INSTANTIATION (fld
))
18290 instantiate_decl (fld
, /*defer_ok=*/false,
18291 /*expl_inst_class=*/false);
18295 case STATIC_ASSERT
:
18299 ++c_inhibit_evaluation_warnings
;
18301 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18304 /*integral_constant_expression_p=*/true);
18305 --c_inhibit_evaluation_warnings
;
18307 finish_static_assert (condition
,
18308 STATIC_ASSERT_MESSAGE (t
),
18309 STATIC_ASSERT_SOURCE_LOCATION (t
),
18310 /*member_p=*/false);
18315 case OACC_PARALLEL
:
18317 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18319 stmt
= begin_omp_parallel ();
18320 RECUR (OMP_BODY (t
));
18321 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18325 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18326 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18327 complain
, in_decl
);
18328 if (OMP_PARALLEL_COMBINED (t
))
18329 omp_parallel_combined_clauses
= &tmp
;
18330 stmt
= begin_omp_parallel ();
18331 RECUR (OMP_PARALLEL_BODY (t
));
18332 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18333 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18334 = OMP_PARALLEL_COMBINED (t
);
18335 pop_omp_privatization_clauses (r
);
18339 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18341 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18342 complain
, in_decl
);
18344 OMP_TASK_CLAUSES (t
) = tmp
;
18348 r
= push_omp_privatization_clauses (false);
18349 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18350 complain
, in_decl
);
18351 stmt
= begin_omp_task ();
18352 RECUR (OMP_TASK_BODY (t
));
18353 finish_omp_task (tmp
, stmt
);
18354 pop_omp_privatization_clauses (r
);
18360 case OMP_DISTRIBUTE
:
18364 tree clauses
, body
, pre_body
;
18365 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18366 tree orig_declv
= NULL_TREE
;
18367 tree incrv
= NULL_TREE
;
18368 enum c_omp_region_type ort
= C_ORT_OMP
;
18369 bool any_range_for
= false;
18372 if (TREE_CODE (t
) == OACC_LOOP
)
18375 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18376 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18378 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18380 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18381 if (OMP_FOR_ORIG_DECLS (t
))
18382 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18383 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18384 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18385 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18388 keep_next_level (true);
18389 stmt
= begin_omp_structured_block ();
18391 pre_body
= push_stmt_list ();
18392 RECUR (OMP_FOR_PRE_BODY (t
));
18393 pre_body
= pop_stmt_list (pre_body
);
18395 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18396 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18398 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18399 condv
, incrv
, &clauses
, args
,
18401 integral_constant_expression_p
);
18402 omp_parallel_combined_clauses
= NULL
;
18406 gcc_assert (orig_declv
);
18407 body
= begin_omp_structured_block ();
18408 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18409 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18410 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18411 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18412 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18413 TREE_VEC_ELT (declv
, i
));
18416 body
= push_stmt_list ();
18417 RECUR (OMP_FOR_BODY (t
));
18419 body
= finish_omp_structured_block (body
);
18421 body
= pop_stmt_list (body
);
18423 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18424 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18425 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18429 t
= make_node (TREE_CODE (t
));
18430 TREE_TYPE (t
) = void_type_node
;
18431 OMP_FOR_BODY (t
) = body
;
18432 OMP_FOR_PRE_BODY (t
) = pre_body
;
18433 OMP_FOR_CLAUSES (t
) = clauses
;
18434 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18438 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18440 pop_omp_privatization_clauses (r
);
18445 omp_parallel_combined_clauses
= NULL
;
18450 case OMP_TASKGROUP
:
18452 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18453 && OMP_TEAMS_COMBINED (t
));
18454 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18456 if (TREE_CODE (t
) == OMP_TEAMS
)
18458 keep_next_level (true);
18459 stmt
= begin_omp_structured_block ();
18460 RECUR (OMP_BODY (t
));
18461 stmt
= finish_omp_structured_block (stmt
);
18465 stmt
= push_stmt_list ();
18466 RECUR (OMP_BODY (t
));
18467 stmt
= pop_stmt_list (stmt
);
18471 OMP_BODY (t
) = stmt
;
18472 OMP_CLAUSES (t
) = tmp
;
18474 pop_omp_privatization_clauses (r
);
18478 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18479 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18481 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18482 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18484 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18485 args
, complain
, in_decl
);
18486 if (tmp
== NULL_TREE
)
18487 tmp
= error_mark_node
;
18491 kind
= (enum omp_clause_depend_kind
)
18492 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18495 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18498 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18499 OMP_CLAUSE_DEPEND_SOURCE
,
18500 OMP_DEPOBJ_CLAUSES (t
));
18504 case OMP_TARGET_DATA
:
18506 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18507 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18509 keep_next_level (true);
18510 stmt
= begin_omp_structured_block ();
18512 RECUR (OMP_BODY (t
));
18513 stmt
= finish_omp_structured_block (stmt
);
18516 OMP_BODY (t
) = stmt
;
18517 OMP_CLAUSES (t
) = tmp
;
18518 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18520 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18523 /* For combined target teams, ensure the num_teams and
18524 thread_limit clause expressions are evaluated on the host,
18525 before entering the target construct. */
18527 for (c
= OMP_TEAMS_CLAUSES (teams
);
18528 c
; c
= OMP_CLAUSE_CHAIN (c
))
18529 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18530 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18531 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18533 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18534 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18535 if (expr
== error_mark_node
)
18537 tmp
= TARGET_EXPR_SLOT (expr
);
18539 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18540 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18541 OMP_CLAUSE_FIRSTPRIVATE
);
18542 OMP_CLAUSE_DECL (tc
) = tmp
;
18543 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18544 OMP_TARGET_CLAUSES (t
) = tc
;
18553 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18554 complain
, in_decl
);
18555 OACC_DECLARE_CLAUSES (t
) = tmp
;
18559 case OMP_TARGET_UPDATE
:
18560 case OMP_TARGET_ENTER_DATA
:
18561 case OMP_TARGET_EXIT_DATA
:
18562 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18563 complain
, in_decl
);
18565 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18569 case OACC_ENTER_DATA
:
18570 case OACC_EXIT_DATA
:
18572 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18573 complain
, in_decl
);
18575 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18580 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18581 complain
, in_decl
);
18582 stmt
= push_stmt_list ();
18583 RECUR (OMP_BODY (t
));
18584 stmt
= pop_stmt_list (stmt
);
18587 OMP_BODY (t
) = stmt
;
18588 OMP_ORDERED_CLAUSES (t
) = tmp
;
18593 omp_parallel_combined_clauses
= NULL
;
18596 stmt
= push_stmt_list ();
18597 RECUR (OMP_BODY (t
));
18598 stmt
= pop_stmt_list (stmt
);
18601 OMP_BODY (t
) = stmt
;
18606 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18608 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18609 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18610 complain
, in_decl
);
18611 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18613 tree op1
= TREE_OPERAND (t
, 1);
18614 tree rhs1
= NULL_TREE
;
18616 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18618 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18619 op1
= TREE_OPERAND (op1
, 1);
18621 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18622 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18623 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18624 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18625 OMP_ATOMIC_MEMORY_ORDER (t
));
18629 tree op1
= TREE_OPERAND (t
, 1);
18630 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18631 tree rhs1
= NULL_TREE
;
18632 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18633 enum tree_code opcode
= NOP_EXPR
;
18634 if (code
== OMP_ATOMIC_READ
)
18636 v
= RECUR (TREE_OPERAND (op1
, 0));
18637 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18639 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18640 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18642 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18643 v
= RECUR (TREE_OPERAND (op1
, 0));
18644 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18645 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18647 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18648 op11
= TREE_OPERAND (op11
, 1);
18650 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18651 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18652 opcode
= TREE_CODE (op11
);
18653 if (opcode
== MODIFY_EXPR
)
18659 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18660 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18662 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18663 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18667 case TRANSACTION_EXPR
:
18670 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18671 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18673 if (TRANSACTION_EXPR_IS_STMT (t
))
18675 tree body
= TRANSACTION_EXPR_BODY (t
);
18676 tree noex
= NULL_TREE
;
18677 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18679 noex
= MUST_NOT_THROW_COND (body
);
18680 if (noex
== NULL_TREE
)
18681 noex
= boolean_true_node
;
18682 body
= TREE_OPERAND (body
, 0);
18684 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18686 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18690 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18691 RECUR (TRANSACTION_EXPR_BODY (t
)),
18698 case MUST_NOT_THROW_EXPR
:
18700 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18701 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18702 RETURN (build_must_not_throw_expr (op0
, cond
));
18705 case EXPR_PACK_EXPANSION
:
18706 error ("invalid use of pack expansion expression");
18707 RETURN (error_mark_node
);
18709 case NONTYPE_ARGUMENT_PACK
:
18710 error ("use %<...%> to expand argument pack");
18711 RETURN (error_mark_node
);
18713 case COMPOUND_EXPR
:
18714 tmp
= RECUR (TREE_OPERAND (t
, 0));
18715 if (tmp
== NULL_TREE
)
18716 /* If the first operand was a statement, we're done with it. */
18717 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18718 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18719 RECUR (TREE_OPERAND (t
, 1)),
18722 case ANNOTATE_EXPR
:
18723 tmp
= RECUR (TREE_OPERAND (t
, 0));
18724 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18725 TREE_TYPE (tmp
), tmp
,
18726 RECUR (TREE_OPERAND (t
, 1)),
18727 RECUR (TREE_OPERAND (t
, 2))));
18730 RETURN (add_stmt (copy_node (t
)));
18733 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18735 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18736 /*function_p=*/false,
18737 integral_constant_expression_p
));
18740 RETURN (NULL_TREE
);
18742 input_location
= loc
;
18748 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18749 function. For description of the body see comment above
18750 cp_parser_omp_declare_reduction_exprs. */
18753 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18755 if (t
== NULL_TREE
|| t
== error_mark_node
)
18758 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18760 tree_stmt_iterator tsi
;
18763 memset (stmts
, 0, sizeof stmts
);
18764 for (i
= 0, tsi
= tsi_start (t
);
18765 i
< 7 && !tsi_end_p (tsi
);
18766 i
++, tsi_next (&tsi
))
18767 stmts
[i
] = tsi_stmt (tsi
);
18768 gcc_assert (tsi_end_p (tsi
));
18772 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18773 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18774 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18775 args
, complain
, in_decl
);
18776 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18777 args
, complain
, in_decl
);
18778 DECL_CONTEXT (omp_out
) = current_function_decl
;
18779 DECL_CONTEXT (omp_in
) = current_function_decl
;
18780 keep_next_level (true);
18781 tree block
= begin_omp_structured_block ();
18782 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18783 block
= finish_omp_structured_block (block
);
18784 block
= maybe_cleanup_point_expr_void (block
);
18785 add_decl_expr (omp_out
);
18786 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18787 TREE_NO_WARNING (omp_out
) = 1;
18788 add_decl_expr (omp_in
);
18789 finish_expr_stmt (block
);
18793 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18794 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18795 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18796 args
, complain
, in_decl
);
18797 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18798 args
, complain
, in_decl
);
18799 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18800 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18801 keep_next_level (true);
18802 tree block
= begin_omp_structured_block ();
18803 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18804 block
= finish_omp_structured_block (block
);
18805 block
= maybe_cleanup_point_expr_void (block
);
18806 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18807 add_decl_expr (omp_priv
);
18808 add_decl_expr (omp_orig
);
18809 finish_expr_stmt (block
);
18811 add_decl_expr (omp_orig
);
18815 /* T is a postfix-expression that is not being used in a function
18816 call. Return the substituted version of T. */
18819 tsubst_non_call_postfix_expression (tree t
, tree args
,
18820 tsubst_flags_t complain
,
18823 if (TREE_CODE (t
) == SCOPE_REF
)
18824 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18825 /*done=*/false, /*address_p=*/false);
18827 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18828 /*function_p=*/false,
18829 /*integral_constant_expression_p=*/false);
18834 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18835 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18836 dependent init-capture. */
18839 prepend_one_capture (tree field
, tree init
, tree
&list
,
18840 tsubst_flags_t complain
)
18842 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18844 tree type
= NULL_TREE
;
18847 if (complain
& tf_error
)
18848 error ("empty initializer in lambda init-capture");
18849 init
= error_mark_node
;
18851 else if (TREE_CODE (init
) == TREE_LIST
)
18852 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18854 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18855 TREE_TYPE (field
) = type
;
18856 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18858 list
= tree_cons (field
, init
, list
);
18861 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18862 instantiation context. Instantiating a pack expansion containing a lambda
18863 might result in multiple lambdas all based on the same lambda in the
18867 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18869 tree oldfn
= lambda_function (t
);
18872 tree r
= build_lambda_expr ();
18874 LAMBDA_EXPR_LOCATION (r
)
18875 = LAMBDA_EXPR_LOCATION (t
);
18876 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18877 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18878 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18879 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18881 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18882 /* A lambda in a default argument outside a class gets no
18883 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18884 tsubst_default_argument calls start_lambda_scope, so we need to
18885 specifically ignore it here, and use the global scope. */
18886 record_null_lambda_scope (r
);
18888 record_lambda_scope (r
);
18890 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18891 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18893 vec
<tree
,va_gc
>* field_packs
= NULL
;
18895 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18896 cap
= TREE_CHAIN (cap
))
18898 tree ofield
= TREE_PURPOSE (cap
);
18899 tree init
= TREE_VALUE (cap
);
18900 if (PACK_EXPANSION_P (init
))
18901 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18903 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18904 /*fn*/false, /*constexpr*/false);
18906 if (init
== error_mark_node
)
18907 return error_mark_node
;
18909 if (init
&& TREE_CODE (init
) == TREE_LIST
)
18910 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18912 if (!processing_template_decl
18913 && init
&& TREE_CODE (init
) != TREE_VEC
18914 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
18916 /* For a VLA, simply tsubsting the field type won't work, we need to
18917 go through add_capture again. XXX do we want to do this for all
18919 tree name
= (get_identifier
18920 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
18921 tree ftype
= TREE_TYPE (ofield
);
18922 bool by_ref
= (TYPE_REF_P (ftype
)
18923 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
18924 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
18925 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
18929 if (PACK_EXPANSION_P (ofield
))
18930 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18931 tree field
= tsubst_decl (ofield
, args
, complain
);
18933 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18935 /* Remember these for when we've pushed local_specializations. */
18936 vec_safe_push (field_packs
, ofield
);
18937 vec_safe_push (field_packs
, field
);
18940 if (field
== error_mark_node
)
18941 return error_mark_node
;
18943 if (TREE_CODE (field
) == TREE_VEC
)
18945 int len
= TREE_VEC_LENGTH (field
);
18946 gcc_assert (TREE_CODE (init
) == TREE_VEC
18947 && TREE_VEC_LENGTH (init
) == len
);
18948 for (int i
= 0; i
< len
; ++i
)
18949 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18950 TREE_VEC_ELT (init
, i
),
18951 LAMBDA_EXPR_CAPTURE_LIST (r
),
18956 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18959 if (id_equal (DECL_NAME (field
), "__this"))
18960 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18964 tree type
= begin_lambda_type (r
);
18965 if (type
== error_mark_node
)
18966 return error_mark_node
;
18968 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18969 determine_visibility (TYPE_NAME (type
));
18971 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18973 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18974 ? DECL_TI_TEMPLATE (oldfn
)
18977 tree fntype
= static_fn_type (oldfn
);
18979 ++processing_template_decl
;
18980 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18982 --processing_template_decl
;
18984 if (fntype
== error_mark_node
)
18985 r
= error_mark_node
;
18988 /* The body of a lambda-expression is not a subexpression of the
18989 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18990 which would be skipped if cp_unevaluated_operand. */
18993 /* Fix the type of 'this'. */
18994 fntype
= build_memfn_type (fntype
, type
,
18995 type_memfn_quals (fntype
),
18996 type_memfn_rqual (fntype
));
19000 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19001 if (tmpl
== error_mark_node
)
19003 r
= error_mark_node
;
19006 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19007 finish_member_declaration (tmpl
);
19012 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19013 if (fn
== error_mark_node
)
19015 r
= error_mark_node
;
19018 finish_member_declaration (fn
);
19021 /* Let finish_function set this. */
19022 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19024 bool nested
= cfun
;
19026 push_function_context ();
19028 /* Still increment function_depth so that we don't GC in the
19029 middle of an expression. */
19032 local_specialization_stack
s (lss_copy
);
19034 tree body
= start_lambda_function (fn
, r
);
19036 /* Now record them for lookup_init_capture_pack. */
19037 int fplen
= vec_safe_length (field_packs
);
19038 for (int i
= 0; i
< fplen
; )
19040 tree pack
= (*field_packs
)[i
++];
19041 tree inst
= (*field_packs
)[i
++];
19042 register_local_specialization (inst
, pack
);
19044 release_tree_vector (field_packs
);
19046 register_parameter_specializations (oldfn
, fn
);
19050 /* We might not partially instantiate some parts of the function, so
19051 copy these flags from the original template. */
19052 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19053 current_function_returns_value
= ol
->returns_value
;
19054 current_function_returns_null
= ol
->returns_null
;
19055 current_function_returns_abnormally
= ol
->returns_abnormally
;
19056 current_function_infinite_loop
= ol
->infinite_loop
;
19059 /* [temp.deduct] A lambda-expression appearing in a function type or a
19060 template parameter is not considered part of the immediate context for
19061 the purposes of template argument deduction. */
19062 complain
= tf_warning_or_error
;
19064 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19065 /*constexpr*/false);
19067 finish_lambda_function (body
);
19070 pop_function_context ();
19074 /* The capture list was built up in reverse order; fix that now. */
19075 LAMBDA_EXPR_CAPTURE_LIST (r
)
19076 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19078 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19080 maybe_add_lambda_conv_op (type
);
19084 finish_struct (type
, /*attr*/NULL_TREE
);
19086 insert_pending_capture_proxies ();
19091 /* Like tsubst but deals with expressions and performs semantic
19092 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19095 tsubst_copy_and_build (tree t
,
19097 tsubst_flags_t complain
,
19100 bool integral_constant_expression_p
)
19102 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19103 #define RECUR(NODE) \
19104 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19105 /*function_p=*/false, \
19106 integral_constant_expression_p)
19109 location_t save_loc
;
19111 if (t
== NULL_TREE
|| t
== error_mark_node
)
19114 save_loc
= input_location
;
19115 if (location_t eloc
= cp_expr_location (t
))
19116 input_location
= eloc
;
19118 /* N3276 decltype magic only applies to calls at the top level or on the
19119 right side of a comma. */
19120 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19121 complain
&= ~tf_decltype
;
19123 switch (TREE_CODE (t
))
19127 /* Fall through. */
19128 case IDENTIFIER_NODE
:
19132 bool non_integral_constant_expression_p
;
19133 const char *error_msg
;
19135 if (IDENTIFIER_CONV_OP_P (t
))
19137 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19138 t
= make_conv_op_name (new_type
);
19141 /* Look up the name. */
19142 decl
= lookup_name (t
);
19144 /* By convention, expressions use ERROR_MARK_NODE to indicate
19145 failure, not NULL_TREE. */
19146 if (decl
== NULL_TREE
)
19147 decl
= error_mark_node
;
19149 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19151 integral_constant_expression_p
,
19152 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19153 &non_integral_constant_expression_p
,
19154 /*template_p=*/false,
19156 /*address_p=*/false,
19157 /*template_arg_p=*/false,
19162 if (!function_p
&& identifier_p (decl
))
19164 if (complain
& tf_error
)
19165 unqualified_name_lookup_error (decl
);
19166 decl
= error_mark_node
;
19171 case TEMPLATE_ID_EXPR
:
19174 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19175 tree targs
= TREE_OPERAND (t
, 1);
19178 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19179 if (targs
== error_mark_node
)
19180 RETURN (error_mark_node
);
19182 if (TREE_CODE (templ
) == SCOPE_REF
)
19184 tree name
= TREE_OPERAND (templ
, 1);
19185 tree tid
= lookup_template_function (name
, targs
);
19186 TREE_OPERAND (templ
, 1) = tid
;
19190 if (concept_definition_p (templ
))
19192 tree check
= build_concept_check (templ
, targs
, complain
);
19193 if (check
== error_mark_node
)
19194 RETURN (error_mark_node
);
19196 tree id
= unpack_concept_check (check
);
19198 /* If we built a function concept check, return the underlying
19199 template-id. So we can evaluate it as a function call. */
19200 if (function_concept_p (TREE_OPERAND (id
, 0)))
19206 if (variable_template_p (templ
))
19208 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19210 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19214 if (TREE_CODE (templ
) == COMPONENT_REF
)
19216 object
= TREE_OPERAND (templ
, 0);
19217 templ
= TREE_OPERAND (templ
, 1);
19220 object
= NULL_TREE
;
19221 templ
= lookup_template_function (templ
, targs
);
19224 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19225 object
, templ
, NULL_TREE
));
19227 RETURN (baselink_for_fns (templ
));
19232 tree r
= RECUR (TREE_OPERAND (t
, 0));
19234 if (REFERENCE_REF_P (t
))
19236 /* A type conversion to reference type will be enclosed in
19237 such an indirect ref, but the substitution of the cast
19238 will have also added such an indirect ref. */
19239 r
= convert_from_reference (r
);
19242 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19243 complain
|decltype_flag
);
19245 if (REF_PARENTHESIZED_P (t
))
19246 r
= force_paren_expr (r
);
19253 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19254 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19255 RETURN (build_nop (type
, op0
));
19258 case IMPLICIT_CONV_EXPR
:
19260 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19261 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19262 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19264 retval
= copy_node (t
);
19265 TREE_TYPE (retval
) = type
;
19266 TREE_OPERAND (retval
, 0) = expr
;
19269 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19270 /* We'll pass this to convert_nontype_argument again, we don't need
19271 to actually perform any conversion here. */
19273 int flags
= LOOKUP_IMPLICIT
;
19274 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19275 flags
= LOOKUP_NORMAL
;
19276 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19277 flags
|= LOOKUP_NO_NARROWING
;
19278 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19284 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19285 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19286 if (op0
== error_mark_node
)
19287 RETURN (error_mark_node
);
19288 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19292 case REINTERPRET_CAST_EXPR
:
19293 case CONST_CAST_EXPR
:
19294 case DYNAMIC_CAST_EXPR
:
19295 case STATIC_CAST_EXPR
:
19298 tree op
, r
= NULL_TREE
;
19300 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19301 if (integral_constant_expression_p
19302 && !cast_valid_in_integral_constant_expression_p (type
))
19304 if (complain
& tf_error
)
19305 error ("a cast to a type other than an integral or "
19306 "enumeration type cannot appear in a constant-expression");
19307 RETURN (error_mark_node
);
19310 op
= RECUR (TREE_OPERAND (t
, 0));
19312 warning_sentinel
s(warn_useless_cast
);
19313 warning_sentinel
s2(warn_ignored_qualifiers
);
19314 switch (TREE_CODE (t
))
19317 r
= build_functional_cast (input_location
, type
, op
, complain
);
19319 case REINTERPRET_CAST_EXPR
:
19320 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19322 case CONST_CAST_EXPR
:
19323 r
= build_const_cast (input_location
, type
, op
, complain
);
19325 case DYNAMIC_CAST_EXPR
:
19326 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19328 case STATIC_CAST_EXPR
:
19329 r
= build_static_cast (input_location
, type
, op
, complain
);
19332 gcc_unreachable ();
19338 case POSTDECREMENT_EXPR
:
19339 case POSTINCREMENT_EXPR
:
19340 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19341 args
, complain
, in_decl
);
19342 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19343 complain
|decltype_flag
));
19345 case PREDECREMENT_EXPR
:
19346 case PREINCREMENT_EXPR
:
19350 case TRUTH_NOT_EXPR
:
19351 case UNARY_PLUS_EXPR
: /* Unary + */
19352 case REALPART_EXPR
:
19353 case IMAGPART_EXPR
:
19354 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19355 RECUR (TREE_OPERAND (t
, 0)),
19356 complain
|decltype_flag
));
19358 case FIX_TRUNC_EXPR
:
19359 gcc_unreachable ();
19362 op1
= TREE_OPERAND (t
, 0);
19363 if (TREE_CODE (op1
) == LABEL_DECL
)
19364 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19365 EXPR_LOCATION (op1
)));
19366 if (TREE_CODE (op1
) == SCOPE_REF
)
19367 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19368 /*done=*/true, /*address_p=*/true);
19370 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19372 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19373 complain
|decltype_flag
));
19378 case TRUNC_DIV_EXPR
:
19379 case CEIL_DIV_EXPR
:
19380 case FLOOR_DIV_EXPR
:
19381 case ROUND_DIV_EXPR
:
19382 case EXACT_DIV_EXPR
:
19386 case TRUNC_MOD_EXPR
:
19387 case FLOOR_MOD_EXPR
:
19388 case TRUTH_ANDIF_EXPR
:
19389 case TRUTH_ORIF_EXPR
:
19390 case TRUTH_AND_EXPR
:
19391 case TRUTH_OR_EXPR
:
19402 case SPACESHIP_EXPR
:
19406 /* If T was type-dependent, suppress warnings that depend on the range
19407 of the types involved. */
19408 bool was_dep
= type_dependent_expression_p_push (t
);
19410 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19411 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19413 warning_sentinel
s1(warn_type_limits
, was_dep
);
19414 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19415 warning_sentinel
s3(warn_logical_op
, was_dep
);
19416 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19418 tree r
= build_x_binary_op
19419 (input_location
, TREE_CODE (t
),
19421 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19423 : TREE_CODE (TREE_OPERAND (t
, 0))),
19425 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19427 : TREE_CODE (TREE_OPERAND (t
, 1))),
19429 complain
|decltype_flag
);
19430 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19431 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19436 case POINTER_PLUS_EXPR
:
19438 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19439 if (op0
== error_mark_node
)
19440 RETURN (error_mark_node
);
19441 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19442 if (op1
== error_mark_node
)
19443 RETURN (error_mark_node
);
19444 RETURN (fold_build_pointer_plus (op0
, op1
));
19448 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19449 /*address_p=*/false));
19451 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19452 args
, complain
, in_decl
);
19453 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19454 RECUR (TREE_OPERAND (t
, 1)),
19455 complain
|decltype_flag
));
19458 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19459 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19460 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19467 op1
= TREE_OPERAND (t
, 0);
19468 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19469 op1
= TREE_TYPE (op1
);
19470 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19471 && ALIGNOF_EXPR_STD_P (t
));
19474 /* When there are no ARGS, we are trying to evaluate a
19475 non-dependent expression from the parser. Trying to do
19476 the substitutions may not work. */
19478 op1
= TREE_TYPE (op1
);
19482 ++cp_unevaluated_operand
;
19483 ++c_inhibit_evaluation_warnings
;
19485 op1
= tsubst (op1
, args
, complain
, in_decl
);
19487 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19488 /*function_p=*/false,
19489 /*integral_constant_expression_p=*/
19491 --cp_unevaluated_operand
;
19492 --c_inhibit_evaluation_warnings
;
19495 r
= cxx_sizeof_or_alignof_type (input_location
,
19496 op1
, TREE_CODE (t
), std_alignof
,
19497 complain
& tf_error
);
19499 r
= cxx_sizeof_or_alignof_expr (input_location
,
19500 op1
, TREE_CODE (t
),
19501 complain
& tf_error
);
19502 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19504 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19506 if (!processing_template_decl
&& TYPE_P (op1
))
19508 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19509 build1 (NOP_EXPR
, op1
, error_mark_node
));
19510 SIZEOF_EXPR_TYPE_P (r
) = 1;
19513 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19514 TREE_SIDE_EFFECTS (r
) = 0;
19515 TREE_READONLY (r
) = 1;
19517 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19522 case AT_ENCODE_EXPR
:
19524 op1
= TREE_OPERAND (t
, 0);
19525 ++cp_unevaluated_operand
;
19526 ++c_inhibit_evaluation_warnings
;
19527 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19528 /*function_p=*/false,
19529 /*integral_constant_expression_p=*/false);
19530 --cp_unevaluated_operand
;
19531 --c_inhibit_evaluation_warnings
;
19532 RETURN (objc_build_encode_expr (op1
));
19535 case NOEXCEPT_EXPR
:
19536 op1
= TREE_OPERAND (t
, 0);
19537 ++cp_unevaluated_operand
;
19538 ++c_inhibit_evaluation_warnings
;
19539 ++cp_noexcept_operand
;
19540 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19541 /*function_p=*/false,
19542 /*integral_constant_expression_p=*/false);
19543 --cp_unevaluated_operand
;
19544 --c_inhibit_evaluation_warnings
;
19545 --cp_noexcept_operand
;
19546 RETURN (finish_noexcept_expr (op1
, complain
));
19550 warning_sentinel
s(warn_div_by_zero
);
19551 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19552 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19553 tree r
= build_x_modify_expr
19554 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19555 complain
|decltype_flag
);
19556 /* TREE_NO_WARNING must be set if either the expression was
19557 parenthesized or it uses an operator such as >>= rather
19558 than plain assignment. In the former case, it was already
19559 set and must be copied. In the latter case,
19560 build_x_modify_expr sets it and it must not be reset
19562 if (TREE_NO_WARNING (t
))
19563 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19569 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19570 args
, complain
, in_decl
);
19571 /* Remember that there was a reference to this entity. */
19573 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19574 RETURN (error_mark_node
);
19575 RETURN (build_x_arrow (input_location
, op1
, complain
));
19579 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19580 tree init
= RECUR (TREE_OPERAND (t
, 3));
19581 vec
<tree
, va_gc
> *placement_vec
;
19582 vec
<tree
, va_gc
> *init_vec
;
19584 location_t loc
= EXPR_LOCATION (t
);
19586 if (placement
== NULL_TREE
)
19587 placement_vec
= NULL
;
19590 placement_vec
= make_tree_vector ();
19591 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19592 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19595 /* If there was an initializer in the original tree, but it
19596 instantiated to an empty list, then we should pass a
19597 non-NULL empty vector to tell build_new that it was an
19598 empty initializer() rather than no initializer. This can
19599 only happen when the initializer is a pack expansion whose
19600 parameter packs are of length zero. */
19601 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19605 init_vec
= make_tree_vector ();
19606 if (init
== void_node
)
19607 gcc_assert (init_vec
!= NULL
);
19610 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19611 vec_safe_push (init_vec
, TREE_VALUE (init
));
19615 /* Avoid passing an enclosing decl to valid_array_size_p. */
19616 in_decl
= NULL_TREE
;
19618 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19619 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19620 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19621 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19624 if (placement_vec
!= NULL
)
19625 release_tree_vector (placement_vec
);
19626 if (init_vec
!= NULL
)
19627 release_tree_vector (init_vec
);
19634 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19635 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19636 RETURN (delete_sanity (input_location
, op0
, op1
,
19637 DELETE_EXPR_USE_VEC (t
),
19638 DELETE_EXPR_USE_GLOBAL (t
),
19642 case COMPOUND_EXPR
:
19644 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19645 complain
& ~tf_decltype
, in_decl
,
19646 /*function_p=*/false,
19647 integral_constant_expression_p
);
19648 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19650 RECUR (TREE_OPERAND (t
, 1)),
19651 complain
|decltype_flag
));
19657 unsigned int nargs
, i
;
19662 function
= CALL_EXPR_FN (t
);
19663 /* Internal function with no arguments. */
19664 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19667 /* When we parsed the expression, we determined whether or
19668 not Koenig lookup should be performed. */
19669 koenig_p
= KOENIG_LOOKUP_P (t
);
19670 if (function
== NULL_TREE
)
19673 qualified_p
= false;
19675 else if (TREE_CODE (function
) == SCOPE_REF
)
19677 qualified_p
= true;
19678 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19680 /*address_p=*/false);
19682 else if (koenig_p
&& identifier_p (function
))
19684 /* Do nothing; calling tsubst_copy_and_build on an identifier
19685 would incorrectly perform unqualified lookup again.
19687 Note that we can also have an IDENTIFIER_NODE if the earlier
19688 unqualified lookup found a member function; in that case
19689 koenig_p will be false and we do want to do the lookup
19690 again to find the instantiated member function.
19692 FIXME but doing that causes c++/15272, so we need to stop
19693 using IDENTIFIER_NODE in that situation. */
19694 qualified_p
= false;
19698 if (TREE_CODE (function
) == COMPONENT_REF
)
19700 tree op
= TREE_OPERAND (function
, 1);
19702 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19703 || (BASELINK_P (op
)
19704 && BASELINK_QUALIFIED_P (op
)));
19707 qualified_p
= false;
19709 if (TREE_CODE (function
) == ADDR_EXPR
19710 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19711 /* Avoid error about taking the address of a constructor. */
19712 function
= TREE_OPERAND (function
, 0);
19714 function
= tsubst_copy_and_build (function
, args
, complain
,
19717 integral_constant_expression_p
);
19719 if (BASELINK_P (function
))
19720 qualified_p
= true;
19723 nargs
= call_expr_nargs (t
);
19724 releasing_vec call_args
;
19725 for (i
= 0; i
< nargs
; ++i
)
19727 tree arg
= CALL_EXPR_ARG (t
, i
);
19729 if (!PACK_EXPANSION_P (arg
))
19730 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19733 /* Expand the pack expansion and push each entry onto
19735 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19736 if (TREE_CODE (arg
) == TREE_VEC
)
19738 unsigned int len
, j
;
19740 len
= TREE_VEC_LENGTH (arg
);
19741 for (j
= 0; j
< len
; ++j
)
19743 tree value
= TREE_VEC_ELT (arg
, j
);
19744 if (value
!= NULL_TREE
)
19745 value
= convert_from_reference (value
);
19746 vec_safe_push (call_args
, value
);
19751 /* A partial substitution. Add one entry. */
19752 vec_safe_push (call_args
, arg
);
19757 /* Stripped-down processing for a call in a thunk. Specifically, in
19758 the thunk template for a generic lambda. */
19759 if (CALL_FROM_THUNK_P (t
))
19761 /* Now that we've expanded any packs, the number of call args
19762 might be different. */
19763 unsigned int cargs
= call_args
->length ();
19764 tree thisarg
= NULL_TREE
;
19765 if (TREE_CODE (function
) == COMPONENT_REF
)
19767 thisarg
= TREE_OPERAND (function
, 0);
19768 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19769 thisarg
= TREE_OPERAND (thisarg
, 0);
19770 function
= TREE_OPERAND (function
, 1);
19771 if (TREE_CODE (function
) == BASELINK
)
19772 function
= BASELINK_FUNCTIONS (function
);
19774 /* We aren't going to do normal overload resolution, so force the
19775 template-id to resolve. */
19776 function
= resolve_nondeduced_context (function
, complain
);
19777 for (unsigned i
= 0; i
< cargs
; ++i
)
19779 /* In a thunk, pass through args directly, without any
19781 tree arg
= (*call_args
)[i
];
19782 while (TREE_CODE (arg
) != PARM_DECL
)
19783 arg
= TREE_OPERAND (arg
, 0);
19784 (*call_args
)[i
] = arg
;
19788 /* If there are no other args, just push 'this'. */
19790 vec_safe_push (call_args
, thisarg
);
19793 /* Otherwise, shift the other args over to make room. */
19794 tree last
= (*call_args
)[cargs
- 1];
19795 vec_safe_push (call_args
, last
);
19796 for (int i
= cargs
- 1; i
> 0; --i
)
19797 (*call_args
)[i
] = (*call_args
)[i
- 1];
19798 (*call_args
)[0] = thisarg
;
19801 ret
= build_call_a (function
, call_args
->length (),
19802 call_args
->address ());
19803 /* The thunk location is not interesting. */
19804 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19805 CALL_FROM_THUNK_P (ret
) = true;
19806 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19807 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19812 /* We do not perform argument-dependent lookup if normal
19813 lookup finds a non-function, in accordance with the
19814 expected resolution of DR 218. */
19816 && ((is_overloaded_fn (function
)
19817 /* If lookup found a member function, the Koenig lookup is
19818 not appropriate, even if an unqualified-name was used
19819 to denote the function. */
19820 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19821 || identifier_p (function
))
19822 /* Only do this when substitution turns a dependent call
19823 into a non-dependent call. */
19824 && type_dependent_expression_p_push (t
)
19825 && !any_type_dependent_arguments_p (call_args
))
19826 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19828 if (function
!= NULL_TREE
19829 && identifier_p (function
)
19830 && !any_type_dependent_arguments_p (call_args
))
19832 if (koenig_p
&& (complain
& tf_warning_or_error
))
19834 /* For backwards compatibility and good diagnostics, try
19835 the unqualified lookup again if we aren't in SFINAE
19837 tree unq
= (tsubst_copy_and_build
19838 (function
, args
, complain
, in_decl
, true,
19839 integral_constant_expression_p
));
19840 if (unq
== error_mark_node
)
19841 RETURN (error_mark_node
);
19843 if (unq
!= function
)
19845 /* In a lambda fn, we have to be careful to not
19846 introduce new this captures. Legacy code can't
19847 be using lambdas anyway, so it's ok to be
19849 bool in_lambda
= (current_class_type
19850 && LAMBDA_TYPE_P (current_class_type
));
19851 char const *const msg
19852 = G_("%qD was not declared in this scope, "
19853 "and no declarations were found by "
19854 "argument-dependent lookup at the point "
19855 "of instantiation");
19859 error_at (cp_expr_loc_or_input_loc (t
),
19862 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19868 if (INDIRECT_REF_P (fn
))
19869 fn
= TREE_OPERAND (fn
, 0);
19870 if (is_overloaded_fn (fn
))
19871 fn
= get_first_fn (fn
);
19874 /* Can't say anything more. */;
19875 else if (DECL_CLASS_SCOPE_P (fn
))
19877 location_t loc
= cp_expr_loc_or_input_loc (t
);
19879 "declarations in dependent base %qT are "
19880 "not found by unqualified lookup",
19881 DECL_CLASS_CONTEXT (fn
));
19882 if (current_class_ptr
)
19884 "use %<this->%D%> instead", function
);
19887 "use %<%T::%D%> instead",
19888 current_class_name
, function
);
19891 inform (DECL_SOURCE_LOCATION (fn
),
19892 "%qD declared here, later in the "
19893 "translation unit", fn
);
19895 RETURN (error_mark_node
);
19901 if (identifier_p (function
))
19903 if (complain
& tf_error
)
19904 unqualified_name_lookup_error (function
);
19905 RETURN (error_mark_node
);
19909 /* Remember that there was a reference to this entity. */
19910 if (function
!= NULL_TREE
19911 && DECL_P (function
)
19912 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19913 RETURN (error_mark_node
);
19915 /* Put back tf_decltype for the actual call. */
19916 complain
|= decltype_flag
;
19918 if (function
== NULL_TREE
)
19919 switch (CALL_EXPR_IFN (t
))
19922 gcc_assert (nargs
== 1);
19923 if (vec_safe_length (call_args
) != 1)
19925 error_at (cp_expr_loc_or_input_loc (t
),
19926 "wrong number of arguments to "
19927 "%<__builtin_launder%>");
19928 ret
= error_mark_node
;
19931 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19932 (*call_args
)[0], complain
);
19935 case IFN_VEC_CONVERT
:
19936 gcc_assert (nargs
== 1);
19937 if (vec_safe_length (call_args
) != 1)
19939 error_at (cp_expr_loc_or_input_loc (t
),
19940 "wrong number of arguments to "
19941 "%<__builtin_convertvector%>");
19942 ret
= error_mark_node
;
19945 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19946 tsubst (TREE_TYPE (t
), args
,
19947 complain
, in_decl
),
19949 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19954 /* Unsupported internal function with arguments. */
19955 gcc_unreachable ();
19957 else if (TREE_CODE (function
) == OFFSET_REF
19958 || TREE_CODE (function
) == DOTSTAR_EXPR
19959 || TREE_CODE (function
) == MEMBER_REF
)
19960 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19962 else if (TREE_CODE (function
) == COMPONENT_REF
)
19964 tree instance
= TREE_OPERAND (function
, 0);
19965 tree fn
= TREE_OPERAND (function
, 1);
19967 if (processing_template_decl
19968 && (type_dependent_expression_p (instance
)
19969 || (!BASELINK_P (fn
)
19970 && TREE_CODE (fn
) != FIELD_DECL
)
19971 || type_dependent_expression_p (fn
)
19972 || any_type_dependent_arguments_p (call_args
)))
19973 ret
= build_min_nt_call_vec (function
, call_args
);
19974 else if (!BASELINK_P (fn
))
19975 ret
= finish_call_expr (function
, &call_args
,
19976 /*disallow_virtual=*/false,
19977 /*koenig_p=*/false,
19980 ret
= (build_new_method_call
19982 &call_args
, NULL_TREE
,
19983 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19987 else if (concept_check_p (function
))
19989 /* FUNCTION is a template-id referring to a concept definition. */
19990 tree id
= unpack_concept_check (function
);
19991 tree tmpl
= TREE_OPERAND (id
, 0);
19992 tree args
= TREE_OPERAND (id
, 1);
19994 /* Calls to standard and variable concepts should have been
19995 previously diagnosed. */
19996 gcc_assert (function_concept_p (tmpl
));
19998 /* Ensure the result is wrapped as a call expression. */
19999 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20002 ret
= finish_call_expr (function
, &call_args
,
20003 /*disallow_virtual=*/qualified_p
,
20007 if (ret
!= error_mark_node
)
20009 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20010 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20011 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20012 if (op
|| ord
|| rev
)
20014 function
= extract_call_expr (ret
);
20015 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20016 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20017 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20026 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20027 cond
= mark_rvalue_use (cond
);
20028 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20031 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20033 if (integer_zerop (folded_cond
))
20035 ++c_inhibit_evaluation_warnings
;
20036 exp1
= RECUR (TREE_OPERAND (t
, 1));
20037 --c_inhibit_evaluation_warnings
;
20038 exp2
= RECUR (TREE_OPERAND (t
, 2));
20042 exp1
= RECUR (TREE_OPERAND (t
, 1));
20043 ++c_inhibit_evaluation_warnings
;
20044 exp2
= RECUR (TREE_OPERAND (t
, 2));
20045 --c_inhibit_evaluation_warnings
;
20047 cond
= folded_cond
;
20051 exp1
= RECUR (TREE_OPERAND (t
, 1));
20052 exp2
= RECUR (TREE_OPERAND (t
, 2));
20055 warning_sentinel
s(warn_duplicated_branches
);
20056 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20057 cond
, exp1
, exp2
, complain
));
20060 case PSEUDO_DTOR_EXPR
:
20062 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20063 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20064 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20065 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20071 tree purpose
, value
, chain
;
20073 if (t
== void_list_node
)
20076 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
20077 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
20079 /* We have pack expansions, so expand those and
20080 create a new list out of it. */
20081 tree purposevec
= NULL_TREE
;
20082 tree valuevec
= NULL_TREE
;
20086 /* Expand the argument expressions. */
20087 if (TREE_PURPOSE (t
))
20088 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
20089 complain
, in_decl
);
20090 if (TREE_VALUE (t
))
20091 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
20092 complain
, in_decl
);
20094 /* Build the rest of the list. */
20095 chain
= TREE_CHAIN (t
);
20096 if (chain
&& chain
!= void_type_node
)
20097 chain
= RECUR (chain
);
20099 /* Determine the number of arguments. */
20100 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
20102 len
= TREE_VEC_LENGTH (purposevec
);
20103 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
20105 else if (TREE_CODE (valuevec
) == TREE_VEC
)
20106 len
= TREE_VEC_LENGTH (valuevec
);
20109 /* Since we only performed a partial substitution into
20110 the argument pack, we only RETURN (a single list
20112 if (purposevec
== TREE_PURPOSE (t
)
20113 && valuevec
== TREE_VALUE (t
)
20114 && chain
== TREE_CHAIN (t
))
20117 RETURN (tree_cons (purposevec
, valuevec
, chain
));
20120 /* Convert the argument vectors into a TREE_LIST */
20124 /* Grab the Ith values. */
20126 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
20129 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
20132 /* Build the list (backwards). */
20133 chain
= tree_cons (purpose
, value
, chain
);
20139 purpose
= TREE_PURPOSE (t
);
20141 purpose
= RECUR (purpose
);
20142 value
= TREE_VALUE (t
);
20144 value
= RECUR (value
);
20145 chain
= TREE_CHAIN (t
);
20146 if (chain
&& chain
!= void_type_node
)
20147 chain
= RECUR (chain
);
20148 if (purpose
== TREE_PURPOSE (t
)
20149 && value
== TREE_VALUE (t
)
20150 && chain
== TREE_CHAIN (t
))
20152 RETURN (tree_cons (purpose
, value
, chain
));
20155 case COMPONENT_REF
:
20162 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20163 args
, complain
, in_decl
);
20164 /* Remember that there was a reference to this entity. */
20165 if (DECL_P (object
)
20166 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20167 RETURN (error_mark_node
);
20168 object_type
= TREE_TYPE (object
);
20170 member
= TREE_OPERAND (t
, 1);
20171 if (BASELINK_P (member
))
20172 member
= tsubst_baselink (member
,
20173 non_reference (TREE_TYPE (object
)),
20174 args
, complain
, in_decl
);
20176 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20177 if (member
== error_mark_node
)
20178 RETURN (error_mark_node
);
20180 if (TREE_CODE (member
) == FIELD_DECL
)
20182 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20183 if (TREE_CODE (r
) == COMPONENT_REF
)
20184 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20187 else if (type_dependent_expression_p (object
))
20188 /* We can't do much here. */;
20189 else if (!CLASS_TYPE_P (object_type
))
20191 if (scalarish_type_p (object_type
))
20193 tree s
= NULL_TREE
;
20194 tree dtor
= member
;
20196 if (TREE_CODE (dtor
) == SCOPE_REF
)
20198 s
= TREE_OPERAND (dtor
, 0);
20199 dtor
= TREE_OPERAND (dtor
, 1);
20201 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20203 dtor
= TREE_OPERAND (dtor
, 0);
20205 RETURN (finish_pseudo_destructor_expr
20206 (object
, s
, dtor
, input_location
));
20210 else if (TREE_CODE (member
) == SCOPE_REF
20211 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20213 /* Lookup the template functions now that we know what the
20215 tree scope
= TREE_OPERAND (member
, 0);
20216 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20217 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20218 member
= lookup_qualified_name (scope
, tmpl
,
20219 /*is_type_p=*/false,
20220 /*complain=*/false);
20221 if (BASELINK_P (member
))
20223 BASELINK_FUNCTIONS (member
)
20224 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20226 member
= (adjust_result_of_qualified_name_lookup
20227 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20232 qualified_name_lookup_error (scope
, tmpl
, member
,
20234 RETURN (error_mark_node
);
20237 else if (TREE_CODE (member
) == SCOPE_REF
20238 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20239 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20241 if (complain
& tf_error
)
20243 if (TYPE_P (TREE_OPERAND (member
, 0)))
20244 error ("%qT is not a class or namespace",
20245 TREE_OPERAND (member
, 0));
20247 error ("%qD is not a class or namespace",
20248 TREE_OPERAND (member
, 0));
20250 RETURN (error_mark_node
);
20253 r
= finish_class_member_access_expr (object
, member
,
20254 /*template_p=*/false,
20256 if (TREE_CODE (r
) == COMPONENT_REF
)
20257 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20262 RETURN (build_throw
20263 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20267 vec
<constructor_elt
, va_gc
> *n
;
20268 constructor_elt
*ce
;
20269 unsigned HOST_WIDE_INT idx
;
20270 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20271 bool process_index_p
;
20273 bool need_copy_p
= false;
20276 if (type
== error_mark_node
)
20277 RETURN (error_mark_node
);
20279 /* We do not want to process the index of aggregate
20280 initializers as they are identifier nodes which will be
20281 looked up by digest_init. */
20282 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20284 if (null_member_pointer_value_p (t
))
20286 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20290 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20291 newlen
= vec_safe_length (n
);
20292 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20294 if (ce
->index
&& process_index_p
20295 /* An identifier index is looked up in the type
20296 being initialized, not the current scope. */
20297 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20298 ce
->index
= RECUR (ce
->index
);
20300 if (PACK_EXPANSION_P (ce
->value
))
20302 /* Substitute into the pack expansion. */
20303 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20306 if (ce
->value
== error_mark_node
20307 || PACK_EXPANSION_P (ce
->value
))
20309 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20310 /* Just move the argument into place. */
20311 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20314 /* Update the length of the final CONSTRUCTOR
20315 arguments vector, and note that we will need to
20317 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20318 need_copy_p
= true;
20322 ce
->value
= RECUR (ce
->value
);
20327 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20329 vec_alloc (n
, newlen
);
20330 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20332 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20334 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20335 for (i
= 0; i
< len
; ++i
)
20336 CONSTRUCTOR_APPEND_ELT (n
, 0,
20337 TREE_VEC_ELT (ce
->value
, i
));
20340 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20344 r
= build_constructor (init_list_type_node
, n
);
20345 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20346 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20347 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20349 if (TREE_HAS_CONSTRUCTOR (t
))
20351 fcl_t cl
= fcl_functional
;
20352 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20354 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20357 TREE_TYPE (r
) = type
;
20363 tree operand_0
= TREE_OPERAND (t
, 0);
20364 if (TYPE_P (operand_0
))
20366 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20367 RETURN (get_typeid (operand_0
, complain
));
20371 operand_0
= RECUR (operand_0
);
20372 RETURN (build_typeid (operand_0
, complain
));
20383 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20384 /* ??? We're doing a subset of finish_id_expression here. */
20385 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20386 /* Replace an evaluated use of the thread_local variable with
20387 a call to its wrapper. */
20389 else if (outer_automatic_var_p (r
))
20390 r
= process_outer_var_ref (r
, complain
);
20392 if (!TYPE_REF_P (TREE_TYPE (t
)))
20393 /* If the original type was a reference, we'll be wrapped in
20394 the appropriate INDIRECT_REF. */
20395 r
= convert_from_reference (r
);
20401 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20402 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20403 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20406 case OFFSETOF_EXPR
:
20409 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20410 in_decl
, /*function_p=*/false,
20411 /*integral_constant_expression_p=*/false);
20412 RETURN (finish_offsetof (object_ptr
,
20413 RECUR (TREE_OPERAND (t
, 0)),
20414 EXPR_LOCATION (t
)));
20417 case ADDRESSOF_EXPR
:
20418 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20419 RECUR (TREE_OPERAND (t
, 0)), complain
));
20423 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20424 complain
, in_decl
);
20425 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20426 complain
, in_decl
);
20427 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20428 TRAIT_EXPR_KIND (t
), type1
, type2
));
20433 tree old_stmt_expr
= cur_stmt_expr
;
20434 tree stmt_expr
= begin_stmt_expr ();
20436 cur_stmt_expr
= stmt_expr
;
20437 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20438 integral_constant_expression_p
);
20439 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20440 cur_stmt_expr
= old_stmt_expr
;
20442 /* If the resulting list of expression statement is empty,
20443 fold it further into void_node. */
20444 if (empty_expr_stmt_p (stmt_expr
))
20445 stmt_expr
= void_node
;
20447 RETURN (stmt_expr
);
20452 if (complain
& tf_partial
)
20454 /* We don't have a full set of template arguments yet; don't touch
20455 the lambda at all. */
20456 gcc_assert (processing_template_decl
);
20459 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20461 RETURN (build_lambda_object (r
));
20465 /* We can get here for a constant initializer of non-dependent type.
20466 FIXME stop folding in cp_parser_initializer_clause. */
20468 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20473 case TRANSACTION_EXPR
:
20474 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20475 integral_constant_expression_p
));
20478 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20480 case VEC_PERM_EXPR
:
20482 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20483 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20484 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20485 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20489 case REQUIRES_EXPR
:
20491 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20496 /* No need to substitute further, a RANGE_EXPR will always be built
20497 with constant operands. */
20500 case NON_LVALUE_EXPR
:
20501 case VIEW_CONVERT_EXPR
:
20502 if (location_wrapper_p (t
))
20503 /* We need to do this here as well as in tsubst_copy so we get the
20504 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20505 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20506 EXPR_LOCATION (t
)));
20510 /* Handle Objective-C++ constructs, if appropriate. */
20513 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20514 in_decl
, /*function_p=*/false);
20518 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20524 input_location
= save_loc
;
20528 /* Verify that the instantiated ARGS are valid. For type arguments,
20529 make sure that the type's linkage is ok. For non-type arguments,
20530 make sure they are constants if they are integral or enumerations.
20531 Emit an error under control of COMPLAIN, and return TRUE on error. */
20534 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20536 if (dependent_template_arg_p (t
))
20538 if (ARGUMENT_PACK_P (t
))
20540 tree vec
= ARGUMENT_PACK_ARGS (t
);
20541 int len
= TREE_VEC_LENGTH (vec
);
20542 bool result
= false;
20545 for (i
= 0; i
< len
; ++i
)
20546 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20550 else if (TYPE_P (t
))
20552 /* [basic.link]: A name with no linkage (notably, the name
20553 of a class or enumeration declared in a local scope)
20554 shall not be used to declare an entity with linkage.
20555 This implies that names with no linkage cannot be used as
20558 DR 757 relaxes this restriction for C++0x. */
20559 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20560 : no_linkage_check (t
, /*relaxed_p=*/false));
20564 /* DR 488 makes use of a type with no linkage cause
20565 type deduction to fail. */
20566 if (complain
& tf_error
)
20568 if (TYPE_UNNAMED_P (nt
))
20569 error ("%qT is/uses unnamed type", t
);
20571 error ("template argument for %qD uses local type %qT",
20576 /* In order to avoid all sorts of complications, we do not
20577 allow variably-modified types as template arguments. */
20578 else if (variably_modified_type_p (t
, NULL_TREE
))
20580 if (complain
& tf_error
)
20581 error ("%qT is a variably modified type", t
);
20585 /* Class template and alias template arguments should be OK. */
20586 else if (DECL_TYPE_TEMPLATE_P (t
))
20588 /* A non-type argument of integral or enumerated type must be a
20590 else if (TREE_TYPE (t
)
20591 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20592 && !REFERENCE_REF_P (t
)
20593 && !TREE_CONSTANT (t
))
20595 if (complain
& tf_error
)
20596 error ("integral expression %qE is not constant", t
);
20603 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20605 int ix
, len
= DECL_NTPARMS (tmpl
);
20606 bool result
= false;
20608 for (ix
= 0; ix
!= len
; ix
++)
20610 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20613 if (result
&& (complain
& tf_error
))
20614 error (" trying to instantiate %qD", tmpl
);
20618 /* We're out of SFINAE context now, so generate diagnostics for the access
20619 errors we saw earlier when instantiating D from TMPL and ARGS. */
20622 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20624 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20625 tree type
= TREE_TYPE (pattern
);
20626 location_t loc
= input_location
;
20628 push_access_scope (d
);
20629 push_deferring_access_checks (dk_no_deferred
);
20630 input_location
= DECL_SOURCE_LOCATION (pattern
);
20631 tsubst (type
, args
, tf_warning_or_error
, d
);
20632 input_location
= loc
;
20633 pop_deferring_access_checks ();
20634 pop_access_scope (d
);
20637 /* Instantiate the indicated variable, function, or alias template TMPL with
20638 the template arguments in TARG_PTR. */
20641 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20643 tree targ_ptr
= orig_args
;
20647 bool access_ok
= true;
20649 if (tmpl
== error_mark_node
)
20650 return error_mark_node
;
20652 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20654 /* If this function is a clone, handle it specially. */
20655 if (DECL_CLONED_FUNCTION_P (tmpl
))
20660 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20661 DECL_CLONED_FUNCTION. */
20662 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20663 targ_ptr
, complain
);
20664 if (spec
== error_mark_node
)
20665 return error_mark_node
;
20667 /* Look for the clone. */
20668 FOR_EACH_CLONE (clone
, spec
)
20669 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20671 /* We should always have found the clone by now. */
20672 gcc_unreachable ();
20676 if (targ_ptr
== error_mark_node
)
20677 return error_mark_node
;
20679 /* Check to see if we already have this specialization. */
20680 gen_tmpl
= most_general_template (tmpl
);
20681 if (TMPL_ARGS_DEPTH (targ_ptr
)
20682 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20683 /* targ_ptr only has the innermost template args, so add the outer ones
20684 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20685 the case of a non-dependent call within a template definition). */
20686 targ_ptr
= (add_outermost_template_args
20687 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20690 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20691 but it doesn't seem to be on the hot path. */
20692 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20694 gcc_assert (tmpl
== gen_tmpl
20695 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
20697 || fndecl
== NULL_TREE
);
20699 if (spec
!= NULL_TREE
)
20701 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20703 if (complain
& tf_error
)
20704 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20705 return error_mark_node
;
20710 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20712 return error_mark_node
;
20714 /* We are building a FUNCTION_DECL, during which the access of its
20715 parameters and return types have to be checked. However this
20716 FUNCTION_DECL which is the desired context for access checking
20717 is not built yet. We solve this chicken-and-egg problem by
20718 deferring all checks until we have the FUNCTION_DECL. */
20719 push_deferring_access_checks (dk_deferred
);
20721 /* Instantiation of the function happens in the context of the function
20722 template, not the context of the overload resolution we're doing. */
20723 push_to_top_level ();
20724 /* If there are dependent arguments, e.g. because we're doing partial
20725 ordering, make sure processing_template_decl stays set. */
20726 if (uses_template_parms (targ_ptr
))
20727 ++processing_template_decl
;
20728 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20730 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20731 complain
, gen_tmpl
, true);
20732 push_nested_class (ctx
);
20735 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20737 fndecl
= NULL_TREE
;
20738 if (VAR_P (pattern
))
20740 /* We need to determine if we're using a partial or explicit
20741 specialization now, because the type of the variable could be
20743 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20744 tree elt
= most_specialized_partial_spec (tid
, complain
);
20745 if (elt
== error_mark_node
)
20746 pattern
= error_mark_node
;
20749 tree partial_tmpl
= TREE_VALUE (elt
);
20750 tree partial_args
= TREE_PURPOSE (elt
);
20751 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20752 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20756 /* Substitute template parameters to obtain the specialization. */
20757 if (fndecl
== NULL_TREE
)
20758 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20759 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20760 pop_nested_class ();
20761 pop_from_top_level ();
20763 if (fndecl
== error_mark_node
)
20765 pop_deferring_access_checks ();
20766 return error_mark_node
;
20769 /* The DECL_TI_TEMPLATE should always be the immediate parent
20770 template, not the most general template. */
20771 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20772 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20774 /* Now we know the specialization, compute access previously
20775 deferred. Do no access control for inheriting constructors,
20776 as we already checked access for the inherited constructor. */
20777 if (!(flag_new_inheriting_ctors
20778 && DECL_INHERITED_CTOR (fndecl
)))
20780 push_access_scope (fndecl
);
20781 if (!perform_deferred_access_checks (complain
))
20783 pop_access_scope (fndecl
);
20785 pop_deferring_access_checks ();
20787 /* If we've just instantiated the main entry point for a function,
20788 instantiate all the alternate entry points as well. We do this
20789 by cloning the instantiation of the main entry point, not by
20790 instantiating the template clones. */
20791 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20792 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20793 clone_function_decl (fndecl
, /*update_methods=*/false);
20797 if (!(complain
& tf_error
))
20799 /* Remember to reinstantiate when we're out of SFINAE so the user
20800 can see the errors. */
20801 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20803 return error_mark_node
;
20808 /* Wrapper for instantiate_template_1. */
20811 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20814 timevar_push (TV_TEMPLATE_INST
);
20815 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20816 timevar_pop (TV_TEMPLATE_INST
);
20820 /* Instantiate the alias template TMPL with ARGS. Also push a template
20821 instantiation level, which instantiate_template doesn't do because
20822 functions and variables have sufficient context established by the
20826 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20828 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20829 return error_mark_node
;
20832 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20833 args
, tmpl
, complain
,
20834 /*require_all_args=*/true,
20835 /*use_default_args=*/true);
20837 /* FIXME check for satisfaction in check_instantiated_args. */
20839 && !any_dependent_template_arguments_p (args
)
20840 && !constraints_satisfied_p (tmpl
, args
))
20842 if (complain
& tf_error
)
20844 auto_diagnostic_group d
;
20845 error ("template constraint failure for %qD", tmpl
);
20846 diagnose_constraints (input_location
, tmpl
, args
);
20848 return error_mark_node
;
20851 if (!push_tinst_level (tmpl
, args
))
20852 return error_mark_node
;
20853 tree r
= instantiate_template (tmpl
, args
, complain
);
20854 pop_tinst_level ();
20859 /* PARM is a template parameter pack for FN. Returns true iff
20860 PARM is used in a deducible way in the argument list of FN. */
20863 pack_deducible_p (tree parm
, tree fn
)
20865 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20866 for (; t
; t
= TREE_CHAIN (t
))
20868 tree type
= TREE_VALUE (t
);
20870 if (!PACK_EXPANSION_P (type
))
20872 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20873 packs
; packs
= TREE_CHAIN (packs
))
20874 if (template_args_equal (TREE_VALUE (packs
), parm
))
20876 /* The template parameter pack is used in a function parameter
20877 pack. If this is the end of the parameter list, the
20878 template parameter pack is deducible. */
20879 if (TREE_CHAIN (t
) == void_list_node
)
20882 /* Otherwise, not. Well, it could be deduced from
20883 a non-pack parameter, but doing so would end up with
20884 a deduction mismatch, so don't bother. */
20888 /* The template parameter pack isn't used in any function parameter
20889 packs, but it might be used deeper, e.g. tuple<Args...>. */
20893 /* Subroutine of fn_type_unification: check non-dependent parms for
20897 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20898 tree fn
, unification_kind_t strict
, int flags
,
20899 struct conversion
**convs
, bool explain_p
)
20901 /* Non-constructor methods need to leave a conversion for 'this', which
20902 isn't included in nargs here. */
20903 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20904 && !DECL_CONSTRUCTOR_P (fn
));
20906 for (unsigned ia
= 0;
20907 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20909 tree parm
= TREE_VALUE (parms
);
20911 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20912 && (!TREE_CHAIN (parms
)
20913 || TREE_CHAIN (parms
) == void_list_node
))
20914 /* For a function parameter pack that occurs at the end of the
20915 parameter-declaration-list, the type A of each remaining
20916 argument of the call is compared with the type P of the
20917 declarator-id of the function parameter pack. */
20920 parms
= TREE_CHAIN (parms
);
20922 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20923 /* For a function parameter pack that does not occur at the
20924 end of the parameter-declaration-list, the type of the
20925 parameter pack is a non-deduced context. */
20928 if (!uses_template_parms (parm
))
20930 tree arg
= args
[ia
];
20931 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20932 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20934 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20935 conv_p
, explain_p
))
20945 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20946 NARGS elements of the arguments that are being used when calling
20947 it. TARGS is a vector into which the deduced template arguments
20950 Returns either a FUNCTION_DECL for the matching specialization of FN or
20951 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20952 true, diagnostics will be printed to explain why it failed.
20954 If FN is a conversion operator, or we are trying to produce a specific
20955 specialization, RETURN_TYPE is the return type desired.
20957 The EXPLICIT_TARGS are explicit template arguments provided via a
20960 The parameter STRICT is one of:
20963 We are deducing arguments for a function call, as in
20964 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20965 deducing arguments for a call to the result of a conversion
20966 function template, as in [over.call.object].
20969 We are deducing arguments for a conversion function, as in
20970 [temp.deduct.conv].
20973 We are deducing arguments when doing an explicit instantiation
20974 as in [temp.explicit], when determining an explicit specialization
20975 as in [temp.expl.spec], or when taking the address of a function
20976 template, as in [temp.deduct.funcaddr]. */
20979 fn_type_unification (tree fn
,
20980 tree explicit_targs
,
20983 unsigned int nargs
,
20985 unification_kind_t strict
,
20987 struct conversion
**convs
,
20993 tree decl
= NULL_TREE
;
20994 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20996 static int deduction_depth
;
20997 /* type_unification_real will pass back any access checks from default
20998 template argument substitution. */
20999 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21000 /* We don't have all the template args yet. */
21001 bool incomplete
= true;
21004 if (flag_new_inheriting_ctors
)
21005 fn
= strip_inheriting_ctors (fn
);
21007 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21008 tree r
= error_mark_node
;
21010 tree full_targs
= targs
;
21011 if (TMPL_ARGS_DEPTH (targs
)
21012 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21013 full_targs
= (add_outermost_template_args
21014 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21018 complain
|= tf_decltype
;
21020 /* In C++0x, it's possible to have a function template whose type depends
21021 on itself recursively. This is most obvious with decltype, but can also
21022 occur with enumeration scope (c++/48969). So we need to catch infinite
21023 recursion and reject the substitution at deduction time; this function
21024 will return error_mark_node for any repeated substitution.
21026 This also catches excessive recursion such as when f<N> depends on
21027 f<N-1> across all integers, and returns error_mark_node for all the
21028 substitutions back up to the initial one.
21030 This is, of course, not reentrant. */
21031 if (excessive_deduction_depth
)
21032 return error_mark_node
;
21035 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21037 fntype
= TREE_TYPE (fn
);
21038 if (explicit_targs
)
21042 The specified template arguments must match the template
21043 parameters in kind (i.e., type, nontype, template), and there
21044 must not be more arguments than there are parameters;
21045 otherwise type deduction fails.
21047 Nontype arguments must match the types of the corresponding
21048 nontype template parameters, or must be convertible to the
21049 types of the corresponding nontype parameters as specified in
21050 _temp.arg.nontype_, otherwise type deduction fails.
21052 All references in the function type of the function template
21053 to the corresponding template parameters are replaced by the
21054 specified template argument values. If a substitution in a
21055 template parameter or in the function type of the function
21056 template results in an invalid type, type deduction fails. */
21057 int i
, len
= TREE_VEC_LENGTH (tparms
);
21058 location_t loc
= input_location
;
21059 incomplete
= false;
21061 if (explicit_targs
== error_mark_node
)
21064 if (TMPL_ARGS_DEPTH (explicit_targs
)
21065 < TMPL_ARGS_DEPTH (full_targs
))
21066 explicit_targs
= add_outermost_template_args (full_targs
,
21069 /* Adjust any explicit template arguments before entering the
21070 substitution context. */
21072 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
21073 complain
|tf_partial
,
21074 /*require_all_args=*/false,
21075 /*use_default_args=*/false));
21076 if (explicit_targs
== error_mark_node
)
21079 /* Substitute the explicit args into the function type. This is
21080 necessary so that, for instance, explicitly declared function
21081 arguments can match null pointed constants. If we were given
21082 an incomplete set of explicit args, we must not do semantic
21083 processing during substitution as we could create partial
21085 for (i
= 0; i
< len
; i
++)
21087 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21088 bool parameter_pack
= false;
21089 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21091 /* Dig out the actual parm. */
21092 if (TREE_CODE (parm
) == TYPE_DECL
21093 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21095 parm
= TREE_TYPE (parm
);
21096 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21098 else if (TREE_CODE (parm
) == PARM_DECL
)
21100 parm
= DECL_INITIAL (parm
);
21101 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21104 if (targ
== NULL_TREE
)
21105 /* No explicit argument for this template parameter. */
21107 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21109 /* Mark the argument pack as "incomplete". We could
21110 still deduce more arguments during unification.
21111 We remove this mark in type_unification_real. */
21112 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21113 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21114 = ARGUMENT_PACK_ARGS (targ
);
21116 /* We have some incomplete argument packs. */
21123 if (!push_tinst_level (fn
, explicit_targs
))
21125 excessive_deduction_depth
= true;
21128 ++processing_template_decl
;
21129 input_location
= DECL_SOURCE_LOCATION (fn
);
21130 /* Ignore any access checks; we'll see them again in
21131 instantiate_template and they might have the wrong
21132 access path at this point. */
21133 push_deferring_access_checks (dk_deferred
);
21134 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21135 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21136 pop_deferring_access_checks ();
21137 input_location
= loc
;
21138 --processing_template_decl
;
21139 pop_tinst_level ();
21141 if (fntype
== error_mark_node
)
21145 /* Place the explicitly specified arguments in TARGS. */
21146 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21147 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21148 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21149 if (!incomplete
&& CHECKING_P
21150 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21151 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21152 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21155 if (return_type
&& strict
!= DEDUCE_CALL
)
21157 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21158 new_args
[0] = return_type
;
21159 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21167 /* Never do unification on the 'this' parameter. */
21168 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21170 if (return_type
&& strict
== DEDUCE_CALL
)
21172 /* We're deducing for a call to the result of a template conversion
21173 function. The parms we really want are in return_type. */
21174 if (INDIRECT_TYPE_P (return_type
))
21175 return_type
= TREE_TYPE (return_type
);
21176 parms
= TYPE_ARG_TYPES (return_type
);
21178 else if (return_type
)
21180 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21183 /* We allow incomplete unification without an error message here
21184 because the standard doesn't seem to explicitly prohibit it. Our
21185 callers must be ready to deal with unification failures in any
21188 /* If we aren't explaining yet, push tinst context so we can see where
21189 any errors (e.g. from class instantiations triggered by instantiation
21190 of default template arguments) come from. If we are explaining, this
21191 context is redundant. */
21192 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21194 excessive_deduction_depth
= true;
21198 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21199 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21200 strict
, &checks
, explain_p
);
21202 pop_tinst_level ();
21206 /* Now that we have bindings for all of the template arguments,
21207 ensure that the arguments deduced for the template template
21208 parameters have compatible template parameter lists. We cannot
21209 check this property before we have deduced all template
21210 arguments, because the template parameter types of a template
21211 template parameter might depend on prior template parameters
21212 deduced after the template template parameter. The following
21213 ill-formed example illustrates this issue:
21215 template<typename T, template<T> class C> void f(C<5>, T);
21217 template<int N> struct X {};
21220 f(X<5>(), 5l); // error: template argument deduction fails
21223 The template parameter list of 'C' depends on the template type
21224 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21225 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21226 time that we deduce 'C'. */
21227 if (!template_template_parm_bindings_ok_p
21228 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21230 unify_inconsistent_template_template_parameters (explain_p
);
21234 /* DR 1391: All parameters have args, now check non-dependent parms for
21236 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21241 /* All is well so far. Now, check:
21245 When all template arguments have been deduced, all uses of
21246 template parameters in nondeduced contexts are replaced with
21247 the corresponding deduced argument values. If the
21248 substitution results in an invalid type, as described above,
21249 type deduction fails. */
21250 if (!push_tinst_level (fn
, targs
))
21252 excessive_deduction_depth
= true;
21256 /* Also collect access checks from the instantiation. */
21257 reopen_deferring_access_checks (checks
);
21259 decl
= instantiate_template (fn
, targs
, complain
);
21261 checks
= get_deferred_access_checks ();
21262 pop_deferring_access_checks ();
21264 pop_tinst_level ();
21266 if (decl
== error_mark_node
)
21269 /* Now perform any access checks encountered during substitution. */
21270 push_access_scope (decl
);
21271 ok
= perform_access_checks (checks
, complain
);
21272 pop_access_scope (decl
);
21276 /* If we're looking for an exact match, check that what we got
21277 is indeed an exact match. It might not be if some template
21278 parameters are used in non-deduced contexts. But don't check
21279 for an exact match if we have dependent template arguments;
21280 in that case we're doing partial ordering, and we already know
21281 that we have two candidates that will provide the actual type. */
21282 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21284 tree substed
= TREE_TYPE (decl
);
21288 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21290 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21291 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21292 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21294 unify_type_mismatch (explain_p
, args
[i
],
21295 TREE_VALUE (sarg
));
21300 /* After doing deduction with the inherited constructor, actually return an
21301 instantiation of the inheriting constructor. */
21303 decl
= instantiate_template (orig_fn
, targs
, complain
);
21309 if (excessive_deduction_depth
)
21311 if (deduction_depth
== 0)
21312 /* Reset once we're all the way out. */
21313 excessive_deduction_depth
= false;
21319 /* Adjust types before performing type deduction, as described in
21320 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21321 sections are symmetric. PARM is the type of a function parameter
21322 or the return type of the conversion function. ARG is the type of
21323 the argument passed to the call, or the type of the value
21324 initialized with the result of the conversion function.
21325 ARG_EXPR is the original argument expression, which may be null. */
21328 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21341 /* Swap PARM and ARG throughout the remainder of this
21342 function; the handling is precisely symmetric since PARM
21343 will initialize ARG rather than vice versa. */
21344 std::swap (parm
, arg
);
21348 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21349 too, but here handle it by stripping the reference from PARM
21350 rather than by adding it to ARG. */
21351 if (TYPE_REF_P (*parm
)
21352 && TYPE_REF_IS_RVALUE (*parm
)
21353 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21354 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21355 && TYPE_REF_P (*arg
)
21356 && !TYPE_REF_IS_RVALUE (*arg
))
21357 *parm
= TREE_TYPE (*parm
);
21358 /* Nothing else to do in this case. */
21362 gcc_unreachable ();
21365 if (!TYPE_REF_P (*parm
))
21367 /* [temp.deduct.call]
21369 If P is not a reference type:
21371 --If A is an array type, the pointer type produced by the
21372 array-to-pointer standard conversion (_conv.array_) is
21373 used in place of A for type deduction; otherwise,
21375 --If A is a function type, the pointer type produced by
21376 the function-to-pointer standard conversion
21377 (_conv.func_) is used in place of A for type deduction;
21380 --If A is a cv-qualified type, the top level
21381 cv-qualifiers of A's type are ignored for type
21383 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21384 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21385 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21386 *arg
= build_pointer_type (*arg
);
21388 *arg
= TYPE_MAIN_VARIANT (*arg
);
21391 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21392 reference to a cv-unqualified template parameter that does not represent a
21393 template parameter of a class template (during class template argument
21394 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21395 an lvalue, the type "lvalue reference to A" is used in place of A for type
21397 if (TYPE_REF_P (*parm
)
21398 && TYPE_REF_IS_RVALUE (*parm
)
21399 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21400 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21401 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21402 && (arg_expr
? lvalue_p (arg_expr
)
21403 /* try_one_overload doesn't provide an arg_expr, but
21404 functions are always lvalues. */
21405 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21406 *arg
= build_reference_type (*arg
);
21408 /* [temp.deduct.call]
21410 If P is a cv-qualified type, the top level cv-qualifiers
21411 of P's type are ignored for type deduction. If P is a
21412 reference type, the type referred to by P is used for
21414 *parm
= TYPE_MAIN_VARIANT (*parm
);
21415 if (TYPE_REF_P (*parm
))
21417 *parm
= TREE_TYPE (*parm
);
21418 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21421 /* DR 322. For conversion deduction, remove a reference type on parm
21422 too (which has been swapped into ARG). */
21423 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21424 *arg
= TREE_TYPE (*arg
);
21429 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21430 template which doesn't contain any deducible template parameters; check if
21431 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21432 unify_one_argument. */
21435 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21436 int flags
, struct conversion
**conv_p
,
21442 type
= TREE_TYPE (arg
);
21446 if (same_type_p (parm
, type
))
21447 return unify_success (explain_p
);
21449 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21450 if (strict
== DEDUCE_CONV
)
21452 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21453 return unify_success (explain_p
);
21455 else if (strict
!= DEDUCE_EXACT
)
21458 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21460 /* Avoid recalculating this in add_function_candidate. */
21462 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21464 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21466 return unify_success (explain_p
);
21469 if (strict
== DEDUCE_EXACT
)
21470 return unify_type_mismatch (explain_p
, parm
, arg
);
21472 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21475 static bool uses_deducible_template_parms (tree type
);
21477 /* Returns true iff the expression EXPR is one from which a template
21478 argument can be deduced. In other words, if it's an undecorated
21479 use of a template non-type parameter. */
21482 deducible_expression (tree expr
)
21484 /* Strip implicit conversions. */
21485 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21486 expr
= TREE_OPERAND (expr
, 0);
21487 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21490 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21491 deducible way; that is, if it has a max value of <PARM> - 1. */
21494 deducible_array_bound (tree domain
)
21496 if (domain
== NULL_TREE
)
21499 tree max
= TYPE_MAX_VALUE (domain
);
21500 if (TREE_CODE (max
) != MINUS_EXPR
)
21503 return deducible_expression (TREE_OPERAND (max
, 0));
21506 /* Returns true iff the template arguments ARGS use a template parameter
21507 in a deducible way. */
21510 deducible_template_args (tree args
)
21512 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21515 tree elt
= TREE_VEC_ELT (args
, i
);
21516 if (ARGUMENT_PACK_P (elt
))
21517 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21520 if (PACK_EXPANSION_P (elt
))
21521 elt
= PACK_EXPANSION_PATTERN (elt
);
21522 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21524 else if (TYPE_P (elt
))
21525 deducible
= uses_deducible_template_parms (elt
);
21527 deducible
= deducible_expression (elt
);
21535 /* Returns true iff TYPE contains any deducible references to template
21536 parameters, as per 14.8.2.5. */
21539 uses_deducible_template_parms (tree type
)
21541 if (PACK_EXPANSION_P (type
))
21542 type
= PACK_EXPANSION_PATTERN (type
);
21549 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21550 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21556 if (INDIRECT_TYPE_P (type
))
21557 return uses_deducible_template_parms (TREE_TYPE (type
));
21559 /* T[integer-constant ]
21561 if (TREE_CODE (type
) == ARRAY_TYPE
)
21562 return (uses_deducible_template_parms (TREE_TYPE (type
))
21563 || deducible_array_bound (TYPE_DOMAIN (type
)));
21575 if (TYPE_PTRMEM_P (type
))
21576 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21577 || (uses_deducible_template_parms
21578 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21580 /* template-name <T> (where template-name refers to a class template)
21581 template-name <i> (where template-name refers to a class template) */
21582 if (CLASS_TYPE_P (type
)
21583 && CLASSTYPE_TEMPLATE_INFO (type
)
21584 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21585 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21586 (CLASSTYPE_TI_ARGS (type
)));
21591 if (FUNC_OR_METHOD_TYPE_P (type
))
21593 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21595 tree parm
= TYPE_ARG_TYPES (type
);
21596 if (TREE_CODE (type
) == METHOD_TYPE
)
21597 parm
= TREE_CHAIN (parm
);
21598 for (; parm
; parm
= TREE_CHAIN (parm
))
21599 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21606 /* Subroutine of type_unification_real and unify_pack_expansion to
21607 handle unification of a single P/A pair. Parameters are as
21608 for those functions. */
21611 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21612 int subr
, unification_kind_t strict
,
21615 tree arg_expr
= NULL_TREE
;
21618 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21619 return unify_invalid (explain_p
);
21620 if (arg
== unknown_type_node
)
21621 /* We can't deduce anything from this, but we might get all the
21622 template args from other function args. */
21623 return unify_success (explain_p
);
21625 /* Implicit conversions (Clause 4) will be performed on a function
21626 argument to convert it to the type of the corresponding function
21627 parameter if the parameter type contains no template-parameters that
21628 participate in template argument deduction. */
21629 if (strict
!= DEDUCE_EXACT
21630 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21631 /* For function parameters with no deducible template parameters,
21632 just return. We'll check non-dependent conversions later. */
21633 return unify_success (explain_p
);
21638 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21639 | UNIFY_ALLOW_MORE_CV_QUAL
21640 | UNIFY_ALLOW_DERIVED
);
21644 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21648 arg_strict
= UNIFY_ALLOW_NONE
;
21652 gcc_unreachable ();
21655 /* We only do these transformations if this is the top-level
21656 parameter_type_list in a call or declaration matching; in other
21657 situations (nested function declarators, template argument lists) we
21658 won't be comparing a type to an expression, and we don't do any type
21664 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21665 if (type_unknown_p (arg
))
21667 /* [temp.deduct.type] A template-argument can be
21668 deduced from a pointer to function or pointer
21669 to member function argument if the set of
21670 overloaded functions does not contain function
21671 templates and at most one of a set of
21672 overloaded functions provides a unique
21674 resolve_overloaded_unification (tparms
, targs
, parm
,
21676 arg_strict
, explain_p
);
21677 /* If a unique match was not found, this is a
21678 non-deduced context, so we still succeed. */
21679 return unify_success (explain_p
);
21683 arg
= unlowered_expr_type (arg
);
21684 if (arg
== error_mark_node
)
21685 return unify_invalid (explain_p
);
21689 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21692 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21693 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21694 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21696 /* For deduction from an init-list we need the actual list. */
21697 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21699 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21702 /* for_each_template_parm callback that always returns 0. */
21705 zero_r (tree
, void *)
21710 /* for_each_template_parm any_fn callback to handle deduction of a template
21711 type argument from the type of an array bound. */
21714 array_deduction_r (tree t
, void *data
)
21716 tree_pair_p d
= (tree_pair_p
)data
;
21717 tree
&tparms
= d
->purpose
;
21718 tree
&targs
= d
->value
;
21720 if (TREE_CODE (t
) == ARRAY_TYPE
)
21721 if (tree dom
= TYPE_DOMAIN (t
))
21722 if (tree max
= TYPE_MAX_VALUE (dom
))
21724 if (TREE_CODE (max
) == MINUS_EXPR
)
21725 max
= TREE_OPERAND (max
, 0);
21726 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21727 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21728 UNIFY_ALLOW_NONE
, /*explain*/false);
21731 /* Keep walking. */
21735 /* Try to deduce any not-yet-deduced template type arguments from the type of
21736 an array bound. This is handled separately from unify because 14.8.2.5 says
21737 "The type of a type parameter is only deduced from an array bound if it is
21738 not otherwise deduced." */
21741 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21743 tree_pair_s data
= { tparms
, targs
};
21744 hash_set
<tree
> visited
;
21745 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21746 /*nondeduced*/false, array_deduction_r
);
21749 /* Most parms like fn_type_unification.
21751 If SUBR is 1, we're being called recursively (to unify the
21752 arguments of a function or method parameter of a function
21755 CHECKS is a pointer to a vector of access checks encountered while
21756 substituting default template arguments. */
21759 type_unification_real (tree tparms
,
21763 unsigned int xnargs
,
21765 unification_kind_t strict
,
21766 vec
<deferred_access_check
, va_gc
> **checks
,
21771 int ntparms
= TREE_VEC_LENGTH (tparms
);
21772 int saw_undeduced
= 0;
21775 unsigned int nargs
;
21778 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21779 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21780 gcc_assert (ntparms
> 0);
21782 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21784 /* Reset the number of non-defaulted template arguments contained
21786 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21794 while (parms
&& parms
!= void_list_node
21797 parm
= TREE_VALUE (parms
);
21799 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21800 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21801 /* For a function parameter pack that occurs at the end of the
21802 parameter-declaration-list, the type A of each remaining
21803 argument of the call is compared with the type P of the
21804 declarator-id of the function parameter pack. */
21807 parms
= TREE_CHAIN (parms
);
21809 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21810 /* For a function parameter pack that does not occur at the
21811 end of the parameter-declaration-list, the type of the
21812 parameter pack is a non-deduced context. */
21818 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21824 && parms
!= void_list_node
21825 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21827 /* Unify the remaining arguments with the pack expansion type. */
21829 tree parmvec
= make_tree_vec (1);
21831 /* Allocate a TREE_VEC and copy in all of the arguments */
21832 argvec
= make_tree_vec (nargs
- ia
);
21833 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21834 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21836 /* Copy the parameter into parmvec. */
21837 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21838 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21839 /*subr=*/subr
, explain_p
))
21842 /* Advance to the end of the list of parameters. */
21843 parms
= TREE_CHAIN (parms
);
21846 /* Fail if we've reached the end of the parm list, and more args
21847 are present, and the parm list isn't variadic. */
21848 if (ia
< nargs
&& parms
== void_list_node
)
21849 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21850 /* Fail if parms are left and they don't have default values and
21851 they aren't all deduced as empty packs (c++/57397). This is
21852 consistent with sufficient_parms_p. */
21853 if (parms
&& parms
!= void_list_node
21854 && TREE_PURPOSE (parms
) == NULL_TREE
)
21856 unsigned int count
= nargs
;
21861 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21864 p
= TREE_CHAIN (p
);
21866 while (p
&& p
!= void_list_node
);
21867 if (count
!= nargs
)
21868 return unify_too_few_arguments (explain_p
, ia
, count
,
21874 tsubst_flags_t complain
= (explain_p
21875 ? tf_warning_or_error
21877 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21879 for (i
= 0; i
< ntparms
; i
++)
21881 tree targ
= TREE_VEC_ELT (targs
, i
);
21882 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21884 /* Clear the "incomplete" flags on all argument packs now so that
21885 substituting them into later default arguments works. */
21886 if (targ
&& ARGUMENT_PACK_P (targ
))
21888 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21889 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21892 if (targ
|| tparm
== error_mark_node
)
21894 tparm
= TREE_VALUE (tparm
);
21896 if (TREE_CODE (tparm
) == TYPE_DECL
21897 && !tried_array_deduction
)
21899 try_array_deduction (tparms
, targs
, xparms
);
21900 tried_array_deduction
= true;
21901 if (TREE_VEC_ELT (targs
, i
))
21905 /* If this is an undeduced nontype parameter that depends on
21906 a type parameter, try another pass; its type may have been
21907 deduced from a later argument than the one from which
21908 this parameter can be deduced. */
21909 if (TREE_CODE (tparm
) == PARM_DECL
21910 && uses_template_parms (TREE_TYPE (tparm
))
21911 && saw_undeduced
< 2)
21917 /* Core issue #226 (C++0x) [temp.deduct]:
21919 If a template argument has not been deduced, its
21920 default template argument, if any, is used.
21922 When we are in C++98 mode, TREE_PURPOSE will either
21923 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21924 to explicitly check cxx_dialect here. */
21925 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21926 /* OK, there is a default argument. Wait until after the
21927 conversion check to do substitution. */
21930 /* If the type parameter is a parameter pack, then it will
21931 be deduced to an empty parameter pack. */
21932 if (template_parameter_pack_p (tparm
))
21936 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21938 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21939 TREE_CONSTANT (arg
) = 1;
21942 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21944 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21946 TREE_VEC_ELT (targs
, i
) = arg
;
21950 return unify_parameter_deduction_failure (explain_p
, tparm
);
21953 /* Now substitute into the default template arguments. */
21954 for (i
= 0; i
< ntparms
; i
++)
21956 tree targ
= TREE_VEC_ELT (targs
, i
);
21957 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21959 if (targ
|| tparm
== error_mark_node
)
21961 tree parm
= TREE_VALUE (tparm
);
21962 tree arg
= TREE_PURPOSE (tparm
);
21963 reopen_deferring_access_checks (*checks
);
21964 location_t save_loc
= input_location
;
21966 input_location
= DECL_SOURCE_LOCATION (parm
);
21968 if (saw_undeduced
== 1
21969 && TREE_CODE (parm
) == PARM_DECL
21970 && uses_template_parms (TREE_TYPE (parm
)))
21972 /* The type of this non-type parameter depends on undeduced
21973 parameters. Don't try to use its default argument yet,
21974 since we might deduce an argument for it on the next pass,
21975 but do check whether the arguments we already have cause
21976 substitution failure, so that that happens before we try
21977 later default arguments (78489). */
21978 ++processing_template_decl
;
21979 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21981 --processing_template_decl
;
21982 if (type
== error_mark_node
)
21983 arg
= error_mark_node
;
21989 /* Even if the call is happening in template context, getting
21990 here means it's non-dependent, and a default argument is
21991 considered a separate definition under [temp.decls], so we can
21992 do this substitution without processing_template_decl. This
21993 is important if the default argument contains something that
21994 might be instantiation-dependent like access (87480). */
21995 processing_template_decl_sentinel s
;
21996 tree substed
= NULL_TREE
;
21997 if (saw_undeduced
== 1)
21999 /* First instatiate in template context, in case we still
22000 depend on undeduced template parameters. */
22001 ++processing_template_decl
;
22002 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22004 --processing_template_decl
;
22005 if (substed
!= error_mark_node
22006 && !uses_template_parms (substed
))
22007 /* We replaced all the tparms, substitute again out of
22008 template context. */
22009 substed
= NULL_TREE
;
22012 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22015 if (!uses_template_parms (substed
))
22016 arg
= convert_template_argument (parm
, substed
, full_targs
,
22017 complain
, i
, NULL_TREE
);
22018 else if (saw_undeduced
== 1)
22021 arg
= error_mark_node
;
22024 input_location
= save_loc
;
22025 *checks
= get_deferred_access_checks ();
22026 pop_deferring_access_checks ();
22028 if (arg
== error_mark_node
)
22032 TREE_VEC_ELT (targs
, i
) = arg
;
22033 /* The position of the first default template argument,
22034 is also the number of non-defaulted arguments in TARGS.
22036 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22037 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22041 if (saw_undeduced
++ == 1)
22045 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22046 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22048 return unify_success (explain_p
);
22051 /* Subroutine of type_unification_real. Args are like the variables
22052 at the call site. ARG is an overloaded function (or template-id);
22053 we try deducing template args from each of the overloads, and if
22054 only one succeeds, we go with that. Modifies TARGS and returns
22055 true on success. */
22058 resolve_overloaded_unification (tree tparms
,
22062 unification_kind_t strict
,
22066 tree tempargs
= copy_node (targs
);
22068 tree goodfn
= NULL_TREE
;
22071 if (TREE_CODE (arg
) == ADDR_EXPR
)
22073 arg
= TREE_OPERAND (arg
, 0);
22079 if (TREE_CODE (arg
) == COMPONENT_REF
)
22080 /* Handle `&x' where `x' is some static or non-static member
22082 arg
= TREE_OPERAND (arg
, 1);
22084 if (TREE_CODE (arg
) == OFFSET_REF
)
22085 arg
= TREE_OPERAND (arg
, 1);
22087 /* Strip baselink information. */
22088 if (BASELINK_P (arg
))
22089 arg
= BASELINK_FUNCTIONS (arg
);
22091 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22093 /* If we got some explicit template args, we need to plug them into
22094 the affected templates before we try to unify, in case the
22095 explicit args will completely resolve the templates in question. */
22098 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22099 arg
= TREE_OPERAND (arg
, 0);
22101 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22104 tree subargs
, elem
;
22106 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22109 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22110 expl_subargs
, NULL_TREE
, tf_none
,
22111 /*require_all_args=*/true,
22112 /*use_default_args=*/true);
22113 if (subargs
!= error_mark_node
22114 && !any_dependent_template_arguments_p (subargs
))
22116 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
22117 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22118 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22119 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22128 /* If no templates (or more than one) are fully resolved by the
22129 explicit arguments, this template-id is a non-deduced context; it
22130 could still be OK if we deduce all template arguments for the
22131 enclosing call through other arguments. */
22135 else if (!OVL_P (arg
))
22136 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22137 -- but the deduction does not succeed because the expression is
22138 not just the function on its own. */
22141 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22144 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22145 strict
, sub_strict
, addr_p
, explain_p
)
22146 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22153 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22154 to function or pointer to member function argument if the set of
22155 overloaded functions does not contain function templates and at most
22156 one of a set of overloaded functions provides a unique match.
22158 So if we found multiple possibilities, we return success but don't
22159 deduce anything. */
22163 int i
= TREE_VEC_LENGTH (targs
);
22165 if (TREE_VEC_ELT (tempargs
, i
))
22167 tree old
= TREE_VEC_ELT (targs
, i
);
22168 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22169 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22170 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22171 /* Don't forget explicit template arguments in a pack. */
22172 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22173 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22174 TREE_VEC_ELT (targs
, i
) = new_
;
22183 /* Core DR 115: In contexts where deduction is done and fails, or in
22184 contexts where deduction is not done, if a template argument list is
22185 specified and it, along with any default template arguments, identifies
22186 a single function template specialization, then the template-id is an
22187 lvalue for the function template specialization. */
22190 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22192 tree expr
, offset
, baselink
;
22195 if (!type_unknown_p (orig_expr
))
22200 offset
= NULL_TREE
;
22201 baselink
= NULL_TREE
;
22203 if (TREE_CODE (expr
) == ADDR_EXPR
)
22205 expr
= TREE_OPERAND (expr
, 0);
22208 if (TREE_CODE (expr
) == OFFSET_REF
)
22211 expr
= TREE_OPERAND (expr
, 1);
22213 if (BASELINK_P (expr
))
22216 expr
= BASELINK_FUNCTIONS (expr
);
22219 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22222 tree goodfn
= NULL_TREE
;
22224 /* If we got some explicit template args, we need to plug them into
22225 the affected templates before we try to unify, in case the
22226 explicit args will completely resolve the templates in question. */
22228 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22229 tree arg
= TREE_OPERAND (expr
, 0);
22230 tree badfn
= NULL_TREE
;
22231 tree badargs
= NULL_TREE
;
22233 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22236 tree subargs
, elem
;
22238 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22241 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22242 expl_subargs
, NULL_TREE
, tf_none
,
22243 /*require_all_args=*/true,
22244 /*use_default_args=*/true);
22245 if (subargs
!= error_mark_node
22246 && !any_dependent_template_arguments_p (subargs
))
22248 elem
= instantiate_template (fn
, subargs
, tf_none
);
22249 if (elem
== error_mark_node
)
22254 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
22263 mark_used (goodfn
);
22266 expr
= build_baselink (BASELINK_BINFO (baselink
),
22267 BASELINK_ACCESS_BINFO (baselink
),
22268 expr
, BASELINK_OPTYPE (baselink
));
22272 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22273 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22276 expr
= cp_build_addr_expr (expr
, complain
);
22279 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22280 /* There were no good options and at least one bad one, so let the
22281 user know what the problem is. */
22282 instantiate_template (badfn
, badargs
, complain
);
22287 /* As above, but error out if the expression remains overloaded. */
22290 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22292 exp
= resolve_nondeduced_context (exp
, complain
);
22293 if (type_unknown_p (exp
))
22295 if (complain
& tf_error
)
22296 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22297 return error_mark_node
;
22302 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22303 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22304 different overloads deduce different arguments for a given parm.
22305 ADDR_P is true if the expression for which deduction is being
22306 performed was of the form "& fn" rather than simply "fn".
22308 Returns 1 on success. */
22311 try_one_overload (tree tparms
,
22316 unification_kind_t strict
,
22325 if (arg
== error_mark_node
)
22328 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22329 to function or pointer to member function argument if the set of
22330 overloaded functions does not contain function templates and at most
22331 one of a set of overloaded functions provides a unique match.
22333 So if this is a template, just return success. */
22335 if (uses_template_parms (arg
))
22338 if (TREE_CODE (arg
) == METHOD_TYPE
)
22339 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22341 arg
= build_pointer_type (arg
);
22343 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22345 /* We don't copy orig_targs for this because if we have already deduced
22346 some template args from previous args, unify would complain when we
22347 try to deduce a template parameter for the same argument, even though
22348 there isn't really a conflict. */
22349 nargs
= TREE_VEC_LENGTH (targs
);
22350 tempargs
= make_tree_vec (nargs
);
22352 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22355 /* First make sure we didn't deduce anything that conflicts with
22356 explicitly specified args. */
22357 for (i
= nargs
; i
--; )
22359 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22360 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22364 else if (uses_template_parms (elt
))
22365 /* Since we're unifying against ourselves, we will fill in
22366 template args used in the function parm list with our own
22367 template parms. Discard them. */
22368 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22369 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22371 /* Check that the argument at each index of the deduced argument pack
22372 is equivalent to the corresponding explicitly specified argument.
22373 We may have deduced more arguments than were explicitly specified,
22376 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22377 that's wrong if we deduce the same argument pack from multiple
22378 function arguments: it's only incomplete the first time. */
22380 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22381 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22383 if (TREE_VEC_LENGTH (deduced_pack
)
22384 < TREE_VEC_LENGTH (explicit_pack
))
22387 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22388 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22389 TREE_VEC_ELT (deduced_pack
, j
)))
22392 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22396 for (i
= nargs
; i
--; )
22398 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22401 TREE_VEC_ELT (targs
, i
) = elt
;
22407 /* PARM is a template class (perhaps with unbound template
22408 parameters). ARG is a fully instantiated type. If ARG can be
22409 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22410 TARGS are as for unify. */
22413 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22416 tree copy_of_targs
;
22418 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22420 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22421 /* Matches anything. */;
22422 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22423 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22426 /* We need to make a new template argument vector for the call to
22427 unify. If we used TARGS, we'd clutter it up with the result of
22428 the attempted unification, even if this class didn't work out.
22429 We also don't want to commit ourselves to all the unifications
22430 we've already done, since unification is supposed to be done on
22431 an argument-by-argument basis. In other words, consider the
22432 following pathological case:
22434 template <int I, int J, int K>
22437 template <int I, int J>
22438 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22440 template <int I, int J, int K>
22441 void f(S<I, J, K>, S<I, I, I>);
22450 Now, by the time we consider the unification involving `s2', we
22451 already know that we must have `f<0, 0, 0>'. But, even though
22452 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22453 because there are two ways to unify base classes of S<0, 1, 2>
22454 with S<I, I, I>. If we kept the already deduced knowledge, we
22455 would reject the possibility I=1. */
22456 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22458 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22460 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22465 /* If unification failed, we're done. */
22466 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22467 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22473 /* Given a template type PARM and a class type ARG, find the unique
22474 base type in ARG that is an instance of PARM. We do not examine
22475 ARG itself; only its base-classes. If there is not exactly one
22476 appropriate base class, return NULL_TREE. PARM may be the type of
22477 a partial specialization, as well as a plain template type. Used
22480 static enum template_base_result
22481 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22482 bool explain_p
, tree
*result
)
22484 tree rval
= NULL_TREE
;
22487 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22489 binfo
= TYPE_BINFO (complete_type (arg
));
22492 /* The type could not be completed. */
22493 *result
= NULL_TREE
;
22494 return tbr_incomplete_type
;
22497 /* Walk in inheritance graph order. The search order is not
22498 important, and this avoids multiple walks of virtual bases. */
22499 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22501 tree r
= try_class_unification (tparms
, targs
, parm
,
22502 BINFO_TYPE (binfo
), explain_p
);
22506 /* If there is more than one satisfactory baseclass, then:
22510 If they yield more than one possible deduced A, the type
22514 if (rval
&& !same_type_p (r
, rval
))
22516 *result
= NULL_TREE
;
22517 return tbr_ambiguous_baseclass
;
22525 return tbr_success
;
22528 /* Returns the level of DECL, which declares a template parameter. */
22531 template_decl_level (tree decl
)
22533 switch (TREE_CODE (decl
))
22536 case TEMPLATE_DECL
:
22537 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22540 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22543 gcc_unreachable ();
22548 /* Decide whether ARG can be unified with PARM, considering only the
22549 cv-qualifiers of each type, given STRICT as documented for unify.
22550 Returns nonzero iff the unification is OK on that basis. */
22553 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22555 int arg_quals
= cp_type_quals (arg
);
22556 int parm_quals
= cp_type_quals (parm
);
22558 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22559 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22561 /* Although a CVR qualifier is ignored when being applied to a
22562 substituted template parameter ([8.3.2]/1 for example), that
22563 does not allow us to unify "const T" with "int&" because both
22564 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22565 It is ok when we're allowing additional CV qualifiers
22566 at the outer level [14.8.2.1]/3,1st bullet. */
22567 if ((TYPE_REF_P (arg
)
22568 || FUNC_OR_METHOD_TYPE_P (arg
))
22569 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22572 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22573 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22577 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22578 && (arg_quals
& parm_quals
) != parm_quals
)
22581 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22582 && (parm_quals
& arg_quals
) != arg_quals
)
22588 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22590 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22592 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22593 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22594 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22596 *index
= TEMPLATE_TYPE_IDX (parm
);
22597 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22601 *index
= TEMPLATE_PARM_IDX (parm
);
22602 *level
= TEMPLATE_PARM_LEVEL (parm
);
22606 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22608 if (unify (TP, TA, P, A, S, EP)) \
22612 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22613 expansion at the end of PACKED_PARMS. Returns 0 if the type
22614 deduction succeeds, 1 otherwise. STRICT is the same as in
22615 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22616 function call argument list. We'll need to adjust the arguments to make them
22617 types. SUBR tells us if this is from a recursive call to
22618 type_unification_real, or for comparing two template argument
22622 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22623 tree packed_args
, unification_kind_t strict
,
22624 bool subr
, bool explain_p
)
22627 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22628 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22629 tree pack
, packs
= NULL_TREE
;
22630 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22632 /* Add in any args remembered from an earlier partial instantiation. */
22633 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22634 int levels
= TMPL_ARGS_DEPTH (targs
);
22636 packed_args
= expand_template_argument_pack (packed_args
);
22638 int len
= TREE_VEC_LENGTH (packed_args
);
22640 /* Determine the parameter packs we will be deducing from the
22641 pattern, and record their current deductions. */
22642 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22643 pack
; pack
= TREE_CHAIN (pack
))
22645 tree parm_pack
= TREE_VALUE (pack
);
22648 /* Only template parameter packs can be deduced, not e.g. function
22649 parameter packs or __bases or __integer_pack. */
22650 if (!TEMPLATE_PARM_P (parm_pack
))
22653 /* Determine the index and level of this parameter pack. */
22654 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22655 if (level
< levels
)
22658 /* Keep track of the parameter packs and their corresponding
22660 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22661 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22664 /* Loop through all of the arguments that have not yet been
22665 unified and unify each with the pattern. */
22666 for (i
= start
; i
< len
; i
++)
22669 bool any_explicit
= false;
22670 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22672 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22673 or the element of its argument pack at the current index if
22674 this argument was explicitly specified. */
22675 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22679 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22682 if (TREE_VALUE (pack
)
22683 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22684 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22686 any_explicit
= true;
22687 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22689 TMPL_ARG (targs
, level
, idx
) = arg
;
22692 /* If we had explicit template arguments, substitute them into the
22693 pattern before deduction. */
22696 /* Some arguments might still be unspecified or dependent. */
22698 ++processing_template_decl
;
22699 dependent
= any_dependent_template_arguments_p (targs
);
22701 --processing_template_decl
;
22702 parm
= tsubst (pattern
, targs
,
22703 explain_p
? tf_warning_or_error
: tf_none
,
22706 --processing_template_decl
;
22707 if (parm
== error_mark_node
)
22713 /* Unify the pattern with the current argument. */
22714 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22718 /* For each parameter pack, collect the deduced value. */
22719 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22722 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22724 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22725 TMPL_ARG (targs
, level
, idx
);
22729 /* Verify that the results of unification with the parameter packs
22730 produce results consistent with what we've seen before, and make
22731 the deduced argument packs available. */
22732 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22734 tree old_pack
= TREE_VALUE (pack
);
22735 tree new_args
= TREE_TYPE (pack
);
22736 int i
, len
= TREE_VEC_LENGTH (new_args
);
22738 bool nondeduced_p
= false;
22740 /* By default keep the original deduced argument pack.
22741 If necessary, more specific code is going to update the
22742 resulting deduced argument later down in this function. */
22743 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22744 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22746 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22747 actually deduce anything. */
22748 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22749 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22750 nondeduced_p
= true;
22754 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22756 /* If we had fewer function args than explicit template args,
22757 just use the explicits. */
22758 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22759 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22760 if (len
< explicit_len
)
22761 new_args
= explicit_args
;
22767 /* Build the deduced *_ARGUMENT_PACK. */
22768 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22770 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22771 TREE_CONSTANT (result
) = 1;
22774 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22776 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22778 /* Note the deduced argument packs for this parameter
22780 TMPL_ARG (targs
, level
, idx
) = result
;
22782 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22783 && (ARGUMENT_PACK_ARGS (old_pack
)
22784 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22786 /* We only had the explicitly-provided arguments before, but
22787 now we have a complete set of arguments. */
22788 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22790 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22791 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22792 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22796 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22797 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22799 if (!comp_template_args (old_args
, new_args
,
22800 &bad_old_arg
, &bad_new_arg
))
22801 /* Inconsistent unification of this parameter pack. */
22802 return unify_parameter_pack_inconsistent (explain_p
,
22808 return unify_success (explain_p
);
22811 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22812 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22813 parameters and return value are as for unify. */
22816 unify_array_domain (tree tparms
, tree targs
,
22817 tree parm_dom
, tree arg_dom
,
22825 /* Our representation of array types uses "N - 1" as the
22826 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22827 not an integer constant. We cannot unify arbitrarily
22828 complex expressions, so we eliminate the MINUS_EXPRs
22830 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22831 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22834 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22835 parm_max
= TREE_OPERAND (parm_max
, 0);
22837 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22838 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22841 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22842 trying to unify the type of a variable with the type
22843 of a template parameter. For example:
22845 template <unsigned int N>
22846 void f (char (&) [N]);
22853 Here, the type of the ARG will be "int [g(i)]", and
22854 may be a SAVE_EXPR, etc. */
22855 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22856 return unify_vla_arg (explain_p
, arg_dom
);
22857 arg_max
= TREE_OPERAND (arg_max
, 0);
22860 /* If only one of the bounds used a MINUS_EXPR, compensate
22861 by adding one to the other bound. */
22862 if (parm_cst
&& !arg_cst
)
22863 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22867 else if (arg_cst
&& !parm_cst
)
22868 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22873 return unify (tparms
, targs
, parm_max
, arg_max
,
22874 UNIFY_ALLOW_INTEGER
, explain_p
);
22877 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22879 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22884 if (PACK_EXPANSION_P (t
))
22885 t
= PACK_EXPANSION_PATTERN (t
);
22886 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22887 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22888 || DECL_TYPE_TEMPLATE_P (t
))
22890 else if (TYPE_P (t
))
22896 /* Deduce the value of template parameters. TPARMS is the (innermost)
22897 set of template parameters to a template. TARGS is the bindings
22898 for those template parameters, as determined thus far; TARGS may
22899 include template arguments for outer levels of template parameters
22900 as well. PARM is a parameter to a template function, or a
22901 subcomponent of that parameter; ARG is the corresponding argument.
22902 This function attempts to match PARM with ARG in a manner
22903 consistent with the existing assignments in TARGS. If more values
22904 are deduced, then TARGS is updated.
22906 Returns 0 if the type deduction succeeds, 1 otherwise. The
22907 parameter STRICT is a bitwise or of the following flags:
22910 Require an exact match between PARM and ARG.
22911 UNIFY_ALLOW_MORE_CV_QUAL:
22912 Allow the deduced ARG to be more cv-qualified (by qualification
22913 conversion) than ARG.
22914 UNIFY_ALLOW_LESS_CV_QUAL:
22915 Allow the deduced ARG to be less cv-qualified than ARG.
22916 UNIFY_ALLOW_DERIVED:
22917 Allow the deduced ARG to be a template base class of ARG,
22918 or a pointer to a template base class of the type pointed to by
22920 UNIFY_ALLOW_INTEGER:
22921 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22922 case for more information.
22923 UNIFY_ALLOW_OUTER_LEVEL:
22924 This is the outermost level of a deduction. Used to determine validity
22925 of qualification conversions. A valid qualification conversion must
22926 have const qualified pointers leading up to the inner type which
22927 requires additional CV quals, except at the outer level, where const
22928 is not required [conv.qual]. It would be normal to set this flag in
22929 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22930 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22931 This is the outermost level of a deduction, and PARM can be more CV
22932 qualified at this point.
22933 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22934 This is the outermost level of a deduction, and PARM can be less CV
22935 qualified at this point. */
22938 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22944 int strict_in
= strict
;
22945 tsubst_flags_t complain
= (explain_p
22946 ? tf_warning_or_error
22949 /* I don't think this will do the right thing with respect to types.
22950 But the only case I've seen it in so far has been array bounds, where
22951 signedness is the only information lost, and I think that will be
22952 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22953 finish_id_expression_1, and are also OK. */
22954 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22955 parm
= TREE_OPERAND (parm
, 0);
22957 if (arg
== error_mark_node
)
22958 return unify_invalid (explain_p
);
22959 if (arg
== unknown_type_node
22960 || arg
== init_list_type_node
)
22961 /* We can't deduce anything from this, but we might get all the
22962 template args from other function args. */
22963 return unify_success (explain_p
);
22965 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22966 return unify_success (explain_p
);
22968 /* If PARM uses template parameters, then we can't bail out here,
22969 even if ARG == PARM, since we won't record unifications for the
22970 template parameters. We might need them if we're trying to
22971 figure out which of two things is more specialized. */
22972 if (arg
== parm
&& !uses_template_parms (parm
))
22973 return unify_success (explain_p
);
22975 /* Handle init lists early, so the rest of the function can assume
22976 we're dealing with a type. */
22977 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22981 tree orig_parm
= parm
;
22983 if (!is_std_init_list (parm
)
22984 && TREE_CODE (parm
) != ARRAY_TYPE
)
22985 /* We can only deduce from an initializer list argument if the
22986 parameter is std::initializer_list or an array; otherwise this
22987 is a non-deduced context. */
22988 return unify_success (explain_p
);
22990 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22991 elttype
= TREE_TYPE (parm
);
22994 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22995 /* Deduction is defined in terms of a single type, so just punt
22996 on the (bizarre) std::initializer_list<T...>. */
22997 if (PACK_EXPANSION_P (elttype
))
22998 return unify_success (explain_p
);
23001 if (strict
!= DEDUCE_EXACT
23002 && TYPE_P (elttype
)
23003 && !uses_deducible_template_parms (elttype
))
23004 /* If ELTTYPE has no deducible template parms, skip deduction from
23005 the list elements. */;
23007 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23009 int elt_strict
= strict
;
23011 if (elt
== error_mark_node
)
23012 return unify_invalid (explain_p
);
23014 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23016 tree type
= TREE_TYPE (elt
);
23017 if (type
== error_mark_node
)
23018 return unify_invalid (explain_p
);
23019 /* It should only be possible to get here for a call. */
23020 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23021 elt_strict
|= maybe_adjust_types_for_deduction
23022 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23026 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23030 if (TREE_CODE (parm
) == ARRAY_TYPE
23031 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23033 /* Also deduce from the length of the initializer list. */
23034 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23035 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23036 if (idx
== error_mark_node
)
23037 return unify_invalid (explain_p
);
23038 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23042 /* If the std::initializer_list<T> deduction worked, replace the
23043 deduced A with std::initializer_list<A>. */
23044 if (orig_parm
!= parm
)
23046 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23047 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23048 targ
= listify (targ
);
23049 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23051 return unify_success (explain_p
);
23054 /* If parm and arg aren't the same kind of thing (template, type, or
23055 expression), fail early. */
23056 if (pa_kind (parm
) != pa_kind (arg
))
23057 return unify_invalid (explain_p
);
23059 /* Immediately reject some pairs that won't unify because of
23060 cv-qualification mismatches. */
23061 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23063 /* It is the elements of the array which hold the cv quals of an array
23064 type, and the elements might be template type parms. We'll check
23065 when we recurse. */
23066 && TREE_CODE (arg
) != ARRAY_TYPE
23067 /* We check the cv-qualifiers when unifying with template type
23068 parameters below. We want to allow ARG `const T' to unify with
23069 PARM `T' for example, when computing which of two templates
23070 is more specialized, for example. */
23071 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23072 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23073 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23075 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23076 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23077 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23078 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23079 strict
&= ~UNIFY_ALLOW_DERIVED
;
23080 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23081 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23083 switch (TREE_CODE (parm
))
23085 case TYPENAME_TYPE
:
23087 case UNBOUND_CLASS_TEMPLATE
:
23088 /* In a type which contains a nested-name-specifier, template
23089 argument values cannot be deduced for template parameters used
23090 within the nested-name-specifier. */
23091 return unify_success (explain_p
);
23093 case TEMPLATE_TYPE_PARM
:
23094 case TEMPLATE_TEMPLATE_PARM
:
23095 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23096 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23097 if (error_operand_p (tparm
))
23098 return unify_invalid (explain_p
);
23100 if (TEMPLATE_TYPE_LEVEL (parm
)
23101 != template_decl_level (tparm
))
23102 /* The PARM is not one we're trying to unify. Just check
23103 to see if it matches ARG. */
23105 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23106 && (is_auto (parm
) ? is_auto (arg
)
23107 : same_type_p (parm
, arg
)))
23108 return unify_success (explain_p
);
23110 return unify_type_mismatch (explain_p
, parm
, arg
);
23112 idx
= TEMPLATE_TYPE_IDX (parm
);
23113 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23114 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23115 if (error_operand_p (tparm
))
23116 return unify_invalid (explain_p
);
23118 /* Check for mixed types and values. */
23119 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23120 && TREE_CODE (tparm
) != TYPE_DECL
)
23121 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23122 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23123 gcc_unreachable ();
23125 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23127 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23128 && CLASS_TYPE_P (arg
))
23130 /* First try to match ARG directly. */
23131 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23135 /* Otherwise, look for a suitable base of ARG, as below. */
23136 enum template_base_result r
;
23137 r
= get_template_base (tparms
, targs
, parm
, arg
,
23140 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23144 /* ARG must be constructed from a template class or a template
23145 template parameter. */
23146 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23147 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23148 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23150 /* Deduce arguments T, i from TT<T> or TT<i>. */
23151 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23154 arg
= TYPE_TI_TEMPLATE (arg
);
23156 /* Fall through to deduce template name. */
23159 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23160 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23162 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23164 /* Simple cases: Value already set, does match or doesn't. */
23165 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23166 return unify_success (explain_p
);
23168 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23172 /* If PARM is `const T' and ARG is only `int', we don't have
23173 a match unless we are allowing additional qualification.
23174 If ARG is `const int' and PARM is just `T' that's OK;
23175 that binds `const int' to `T'. */
23176 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23178 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23180 /* Consider the case where ARG is `const volatile int' and
23181 PARM is `const T'. Then, T should be `volatile int'. */
23182 arg
= cp_build_qualified_type_real
23183 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23184 if (arg
== error_mark_node
)
23185 return unify_invalid (explain_p
);
23187 /* Simple cases: Value already set, does match or doesn't. */
23188 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23189 return unify_success (explain_p
);
23191 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23193 /* Make sure that ARG is not a variable-sized array. (Note
23194 that were talking about variable-sized arrays (like
23195 `int[n]'), rather than arrays of unknown size (like
23196 `int[]').) We'll get very confused by such a type since
23197 the bound of the array is not constant, and therefore
23198 not mangleable. Besides, such types are not allowed in
23199 ISO C++, so we can do as we please here. We do allow
23200 them for 'auto' deduction, since that isn't ABI-exposed. */
23201 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23202 return unify_vla_arg (explain_p
, arg
);
23204 /* Strip typedefs as in convert_template_argument. */
23205 arg
= canonicalize_type_argument (arg
, tf_none
);
23208 /* If ARG is a parameter pack or an expansion, we cannot unify
23209 against it unless PARM is also a parameter pack. */
23210 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23211 && !template_parameter_pack_p (parm
))
23212 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23214 /* If the argument deduction results is a METHOD_TYPE,
23215 then there is a problem.
23216 METHOD_TYPE doesn't map to any real C++ type the result of
23217 the deduction cannot be of that type. */
23218 if (TREE_CODE (arg
) == METHOD_TYPE
)
23219 return unify_method_type_error (explain_p
, arg
);
23221 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23222 return unify_success (explain_p
);
23224 case TEMPLATE_PARM_INDEX
:
23225 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23226 if (error_operand_p (tparm
))
23227 return unify_invalid (explain_p
);
23229 if (TEMPLATE_PARM_LEVEL (parm
)
23230 != template_decl_level (tparm
))
23232 /* The PARM is not one we're trying to unify. Just check
23233 to see if it matches ARG. */
23234 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23235 && cp_tree_equal (parm
, arg
));
23237 unify_expression_unequal (explain_p
, parm
, arg
);
23241 idx
= TEMPLATE_PARM_IDX (parm
);
23242 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23246 if ((strict
& UNIFY_ALLOW_INTEGER
)
23247 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23248 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23249 /* We're deducing from an array bound, the type doesn't matter. */
23250 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23251 int x
= !cp_tree_equal (targ
, arg
);
23253 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23257 /* [temp.deduct.type] If, in the declaration of a function template
23258 with a non-type template-parameter, the non-type
23259 template-parameter is used in an expression in the function
23260 parameter-list and, if the corresponding template-argument is
23261 deduced, the template-argument type shall match the type of the
23262 template-parameter exactly, except that a template-argument
23263 deduced from an array bound may be of any integral type.
23264 The non-type parameter might use already deduced type parameters. */
23265 tparm
= TREE_TYPE (parm
);
23266 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23267 /* We don't have enough levels of args to do any substitution. This
23268 can happen in the context of -fnew-ttp-matching. */;
23271 ++processing_template_decl
;
23272 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23273 --processing_template_decl
;
23275 if (tree a
= type_uses_auto (tparm
))
23277 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23278 if (tparm
== error_mark_node
)
23283 if (!TREE_TYPE (arg
))
23284 /* Template-parameter dependent expression. Just accept it for now.
23285 It will later be processed in convert_template_argument. */
23287 else if (same_type_ignoring_top_level_qualifiers_p
23288 (non_reference (TREE_TYPE (arg
)),
23289 non_reference (tparm
)))
23290 /* OK. Ignore top-level quals here because a class-type template
23291 parameter object is const. */;
23292 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23293 && CP_INTEGRAL_TYPE_P (tparm
))
23294 /* Convert the ARG to the type of PARM; the deduced non-type
23295 template argument must exactly match the types of the
23296 corresponding parameter. */
23297 arg
= fold (build_nop (tparm
, arg
));
23298 else if (uses_template_parms (tparm
))
23300 /* We haven't deduced the type of this parameter yet. */
23301 if (cxx_dialect
>= cxx17
23302 /* We deduce from array bounds in try_array_deduction. */
23303 && !(strict
& UNIFY_ALLOW_INTEGER
))
23305 /* Deduce it from the non-type argument. */
23306 tree atype
= TREE_TYPE (arg
);
23307 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23309 UNIFY_ALLOW_NONE
, explain_p
);
23312 /* Try again later. */
23313 return unify_success (explain_p
);
23316 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23318 /* If ARG is a parameter pack or an expansion, we cannot unify
23319 against it unless PARM is also a parameter pack. */
23320 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23321 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23322 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23325 bool removed_attr
= false;
23326 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23328 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23329 return unify_success (explain_p
);
23333 /* A pointer-to-member constant can be unified only with
23334 another constant. */
23335 if (TREE_CODE (arg
) != PTRMEM_CST
)
23336 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23338 /* Just unify the class member. It would be useless (and possibly
23339 wrong, depending on the strict flags) to unify also
23340 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23341 arg refer to the same variable, even if through different
23342 classes. For instance:
23344 struct A { int x; };
23347 Unification of &A::x and &B::x must succeed. */
23348 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23349 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23354 if (!TYPE_PTR_P (arg
))
23355 return unify_type_mismatch (explain_p
, parm
, arg
);
23357 /* [temp.deduct.call]
23359 A can be another pointer or pointer to member type that can
23360 be converted to the deduced A via a qualification
23361 conversion (_conv.qual_).
23363 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23364 This will allow for additional cv-qualification of the
23365 pointed-to types if appropriate. */
23367 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23368 /* The derived-to-base conversion only persists through one
23369 level of pointers. */
23370 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23372 return unify (tparms
, targs
, TREE_TYPE (parm
),
23373 TREE_TYPE (arg
), strict
, explain_p
);
23376 case REFERENCE_TYPE
:
23377 if (!TYPE_REF_P (arg
))
23378 return unify_type_mismatch (explain_p
, parm
, arg
);
23379 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23380 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23383 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23384 return unify_type_mismatch (explain_p
, parm
, arg
);
23385 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23386 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23387 return unify_type_mismatch (explain_p
, parm
, arg
);
23388 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23389 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23390 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23391 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23392 TYPE_DOMAIN (arg
), explain_p
);
23393 return unify_success (explain_p
);
23400 case ENUMERAL_TYPE
:
23403 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23404 return unify_type_mismatch (explain_p
, parm
, arg
);
23406 /* We have already checked cv-qualification at the top of the
23408 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23409 return unify_type_mismatch (explain_p
, parm
, arg
);
23411 /* As far as unification is concerned, this wins. Later checks
23412 will invalidate it if necessary. */
23413 return unify_success (explain_p
);
23415 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23416 /* Type INTEGER_CST can come from ordinary constant template args. */
23418 while (CONVERT_EXPR_P (arg
))
23419 arg
= TREE_OPERAND (arg
, 0);
23421 if (TREE_CODE (arg
) != INTEGER_CST
)
23422 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23423 return (tree_int_cst_equal (parm
, arg
)
23424 ? unify_success (explain_p
)
23425 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23429 int i
, len
, argslen
;
23430 int parm_variadic_p
= 0;
23432 if (TREE_CODE (arg
) != TREE_VEC
)
23433 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23435 len
= TREE_VEC_LENGTH (parm
);
23436 argslen
= TREE_VEC_LENGTH (arg
);
23438 /* Check for pack expansions in the parameters. */
23439 for (i
= 0; i
< len
; ++i
)
23441 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23444 /* We can unify against something with a trailing
23446 parm_variadic_p
= 1;
23448 /* [temp.deduct.type]/9: If the template argument list of
23449 P contains a pack expansion that is not the last
23450 template argument, the entire template argument list
23451 is a non-deduced context. */
23452 return unify_success (explain_p
);
23456 /* If we don't have enough arguments to satisfy the parameters
23457 (not counting the pack expression at the end), or we have
23458 too many arguments for a parameter list that doesn't end in
23459 a pack expression, we can't unify. */
23460 if (parm_variadic_p
23461 ? argslen
< len
- parm_variadic_p
23463 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23465 /* Unify all of the parameters that precede the (optional)
23466 pack expression. */
23467 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23469 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23470 TREE_VEC_ELT (parm
, i
),
23471 TREE_VEC_ELT (arg
, i
),
23472 UNIFY_ALLOW_NONE
, explain_p
);
23474 if (parm_variadic_p
)
23475 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23477 /*subr=*/true, explain_p
);
23478 return unify_success (explain_p
);
23483 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23484 return unify_type_mismatch (explain_p
, parm
, arg
);
23486 if (TYPE_PTRMEMFUNC_P (parm
))
23488 if (!TYPE_PTRMEMFUNC_P (arg
))
23489 return unify_type_mismatch (explain_p
, parm
, arg
);
23491 return unify (tparms
, targs
,
23492 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23493 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23494 strict
, explain_p
);
23496 else if (TYPE_PTRMEMFUNC_P (arg
))
23497 return unify_type_mismatch (explain_p
, parm
, arg
);
23499 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23501 tree t
= NULL_TREE
;
23503 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23505 /* First, we try to unify the PARM and ARG directly. */
23506 t
= try_class_unification (tparms
, targs
,
23507 parm
, arg
, explain_p
);
23511 /* Fallback to the special case allowed in
23512 [temp.deduct.call]:
23514 If P is a class, and P has the form
23515 template-id, then A can be a derived class of
23516 the deduced A. Likewise, if P is a pointer to
23517 a class of the form template-id, A can be a
23518 pointer to a derived class pointed to by the
23520 enum template_base_result r
;
23521 r
= get_template_base (tparms
, targs
, parm
, arg
,
23526 /* Don't give the derived diagnostic if we're
23527 already dealing with the same template. */
23529 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23530 && (CLASSTYPE_TI_TEMPLATE (parm
)
23531 == CLASSTYPE_TI_TEMPLATE (arg
)));
23532 return unify_no_common_base (explain_p
&& !same_template
,
23537 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23538 && (CLASSTYPE_TI_TEMPLATE (parm
)
23539 == CLASSTYPE_TI_TEMPLATE (arg
)))
23540 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23541 Then, we should unify `int' and `U'. */
23544 /* There's no chance of unification succeeding. */
23545 return unify_type_mismatch (explain_p
, parm
, arg
);
23547 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23548 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23550 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23551 return unify_type_mismatch (explain_p
, parm
, arg
);
23552 return unify_success (explain_p
);
23555 case FUNCTION_TYPE
:
23557 unsigned int nargs
;
23562 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23563 return unify_type_mismatch (explain_p
, parm
, arg
);
23565 /* CV qualifications for methods can never be deduced, they must
23566 match exactly. We need to check them explicitly here,
23567 because type_unification_real treats them as any other
23568 cv-qualified parameter. */
23569 if (TREE_CODE (parm
) == METHOD_TYPE
23570 && (!check_cv_quals_for_unify
23572 class_of_this_parm (arg
),
23573 class_of_this_parm (parm
))))
23574 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23575 if (TREE_CODE (arg
) == FUNCTION_TYPE
23576 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23577 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23578 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23579 return unify_type_mismatch (explain_p
, parm
, arg
);
23581 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23582 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23584 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23585 args
= XALLOCAVEC (tree
, nargs
);
23586 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23587 a
!= NULL_TREE
&& a
!= void_list_node
;
23588 a
= TREE_CHAIN (a
), ++i
)
23589 args
[i
] = TREE_VALUE (a
);
23592 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23593 args
, nargs
, 1, DEDUCE_EXACT
,
23597 if (flag_noexcept_type
)
23599 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23600 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23601 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23602 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23603 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23604 && uses_template_parms (TREE_PURPOSE (pspec
)))
23605 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23606 TREE_PURPOSE (aspec
),
23607 UNIFY_ALLOW_NONE
, explain_p
);
23608 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23609 return unify_type_mismatch (explain_p
, parm
, arg
);
23616 /* Unify a pointer to member with a pointer to member function, which
23617 deduces the type of the member as a function type. */
23618 if (TYPE_PTRMEMFUNC_P (arg
))
23620 /* Check top-level cv qualifiers */
23621 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23622 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23624 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23625 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23626 UNIFY_ALLOW_NONE
, explain_p
);
23628 /* Determine the type of the function we are unifying against. */
23629 tree fntype
= static_fn_type (arg
);
23631 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23634 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23635 return unify_type_mismatch (explain_p
, parm
, arg
);
23636 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23637 TYPE_OFFSET_BASETYPE (arg
),
23638 UNIFY_ALLOW_NONE
, explain_p
);
23639 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23640 strict
, explain_p
);
23643 if (DECL_TEMPLATE_PARM_P (parm
))
23644 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23645 if (arg
!= scalar_constant_value (parm
))
23646 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23647 return unify_success (explain_p
);
23650 case TEMPLATE_DECL
:
23651 /* Matched cases are handled by the ARG == PARM test above. */
23652 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23655 /* We might get a variable as a non-type template argument in parm if the
23656 corresponding parameter is type-dependent. Make any necessary
23657 adjustments based on whether arg is a reference. */
23658 if (CONSTANT_CLASS_P (arg
))
23659 parm
= fold_non_dependent_expr (parm
, complain
);
23660 else if (REFERENCE_REF_P (arg
))
23662 tree sub
= TREE_OPERAND (arg
, 0);
23664 if (TREE_CODE (sub
) == ADDR_EXPR
)
23665 arg
= TREE_OPERAND (sub
, 0);
23667 /* Now use the normal expression code to check whether they match. */
23670 case TYPE_ARGUMENT_PACK
:
23671 case NONTYPE_ARGUMENT_PACK
:
23672 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23673 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23676 case DECLTYPE_TYPE
:
23677 case UNDERLYING_TYPE
:
23678 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23679 or UNDERLYING_TYPE nodes. */
23680 return unify_success (explain_p
);
23683 /* Unification fails if we hit an error node. */
23684 return unify_invalid (explain_p
);
23687 if (REFERENCE_REF_P (parm
))
23689 bool pexp
= PACK_EXPANSION_P (arg
);
23691 arg
= PACK_EXPANSION_PATTERN (arg
);
23692 if (REFERENCE_REF_P (arg
))
23693 arg
= TREE_OPERAND (arg
, 0);
23695 arg
= make_pack_expansion (arg
, complain
);
23696 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23697 strict
, explain_p
);
23702 /* An unresolved overload is a nondeduced context. */
23703 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23704 return unify_success (explain_p
);
23705 gcc_assert (EXPR_P (parm
)
23706 || COMPOUND_LITERAL_P (parm
)
23707 || TREE_CODE (parm
) == TRAIT_EXPR
);
23709 /* We must be looking at an expression. This can happen with
23713 void foo(S<I>, S<I + 2>);
23717 template<typename T>
23718 void foo(A<T, T{}>);
23720 This is a "non-deduced context":
23724 The non-deduced contexts are:
23726 --A non-type template argument or an array bound in which
23727 a subexpression references a template parameter.
23729 In these cases, we assume deduction succeeded, but don't
23730 actually infer any unifications. */
23732 if (!uses_template_parms (parm
)
23733 && !template_args_equal (parm
, arg
))
23734 return unify_expression_unequal (explain_p
, parm
, arg
);
23736 return unify_success (explain_p
);
23739 #undef RECUR_AND_CHECK_FAILURE
23741 /* Note that DECL can be defined in this translation unit, if
23745 mark_definable (tree decl
)
23748 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23749 FOR_EACH_CLONE (clone
, decl
)
23750 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23753 /* Called if RESULT is explicitly instantiated, or is a member of an
23754 explicitly instantiated class. */
23757 mark_decl_instantiated (tree result
, int extern_p
)
23759 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23761 /* If this entity has already been written out, it's too late to
23762 make any modifications. */
23763 if (TREE_ASM_WRITTEN (result
))
23766 /* For anonymous namespace we don't need to do anything. */
23767 if (decl_anon_ns_mem_p (result
))
23769 gcc_assert (!TREE_PUBLIC (result
));
23773 if (TREE_CODE (result
) != FUNCTION_DECL
)
23774 /* The TREE_PUBLIC flag for function declarations will have been
23775 set correctly by tsubst. */
23776 TREE_PUBLIC (result
) = 1;
23778 /* This might have been set by an earlier implicit instantiation. */
23779 DECL_COMDAT (result
) = 0;
23782 DECL_NOT_REALLY_EXTERN (result
) = 0;
23785 mark_definable (result
);
23786 mark_needed (result
);
23787 /* Always make artificials weak. */
23788 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23789 comdat_linkage (result
);
23790 /* For WIN32 we also want to put explicit instantiations in
23791 linkonce sections. */
23792 else if (TREE_PUBLIC (result
))
23793 maybe_make_one_only (result
);
23794 if (TREE_CODE (result
) == FUNCTION_DECL
23795 && DECL_TEMPLATE_INSTANTIATED (result
))
23796 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23797 since start_preparsed_function wouldn't have if we had an earlier
23798 extern explicit instantiation. */
23799 DECL_EXTERNAL (result
) = 0;
23802 /* If EXTERN_P, then this function will not be emitted -- unless
23803 followed by an explicit instantiation, at which point its linkage
23804 will be adjusted. If !EXTERN_P, then this function will be
23805 emitted here. In neither circumstance do we want
23806 import_export_decl to adjust the linkage. */
23807 DECL_INTERFACE_KNOWN (result
) = 1;
23810 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23811 important template arguments. If any are missing, we check whether
23812 they're important by using error_mark_node for substituting into any
23813 args that were used for partial ordering (the ones between ARGS and END)
23814 and seeing if it bubbles up. */
23817 check_undeduced_parms (tree targs
, tree args
, tree end
)
23819 bool found
= false;
23821 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23822 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23825 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23829 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23830 if (substed
== error_mark_node
)
23836 /* Given two function templates PAT1 and PAT2, return:
23838 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23839 -1 if PAT2 is more specialized than PAT1.
23840 0 if neither is more specialized.
23842 LEN indicates the number of parameters we should consider
23843 (defaulted parameters should not be considered).
23845 The 1998 std underspecified function template partial ordering, and
23846 DR214 addresses the issue. We take pairs of arguments, one from
23847 each of the templates, and deduce them against each other. One of
23848 the templates will be more specialized if all the *other*
23849 template's arguments deduce against its arguments and at least one
23850 of its arguments *does* *not* deduce against the other template's
23851 corresponding argument. Deduction is done as for class templates.
23852 The arguments used in deduction have reference and top level cv
23853 qualifiers removed. Iff both arguments were originally reference
23854 types *and* deduction succeeds in both directions, an lvalue reference
23855 wins against an rvalue reference and otherwise the template
23856 with the more cv-qualified argument wins for that pairing (if
23857 neither is more cv-qualified, they both are equal). Unlike regular
23858 deduction, after all the arguments have been deduced in this way,
23859 we do *not* verify the deduced template argument values can be
23860 substituted into non-deduced contexts.
23862 The logic can be a bit confusing here, because we look at deduce1 and
23863 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23864 can find template arguments for pat1 to make arg1 look like arg2, that
23865 means that arg2 is at least as specialized as arg1. */
23868 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23870 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23871 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23872 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23873 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23874 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23875 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23876 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23877 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23878 tree origs1
, origs2
;
23879 bool lose1
= false;
23880 bool lose2
= false;
23882 /* Remove the this parameter from non-static member functions. If
23883 one is a non-static member function and the other is not a static
23884 member function, remove the first parameter from that function
23885 also. This situation occurs for operator functions where we
23886 locate both a member function (with this pointer) and non-member
23887 operator (with explicit first operand). */
23888 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23890 len
--; /* LEN is the number of significant arguments for DECL1 */
23891 args1
= TREE_CHAIN (args1
);
23892 if (!DECL_STATIC_FUNCTION_P (decl2
))
23893 args2
= TREE_CHAIN (args2
);
23895 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23897 args2
= TREE_CHAIN (args2
);
23898 if (!DECL_STATIC_FUNCTION_P (decl1
))
23901 args1
= TREE_CHAIN (args1
);
23905 /* If only one is a conversion operator, they are unordered. */
23906 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23909 /* Consider the return type for a conversion function */
23910 if (DECL_CONV_FN_P (decl1
))
23912 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23913 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23917 processing_template_decl
++;
23923 /* Stop when an ellipsis is seen. */
23924 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23926 tree arg1
= TREE_VALUE (args1
);
23927 tree arg2
= TREE_VALUE (args2
);
23928 int deduce1
, deduce2
;
23934 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23935 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23937 /* When both arguments are pack expansions, we need only
23938 unify the patterns themselves. */
23939 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23940 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23942 /* This is the last comparison we need to do. */
23946 if (TYPE_REF_P (arg1
))
23948 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23949 arg1
= TREE_TYPE (arg1
);
23950 quals1
= cp_type_quals (arg1
);
23953 if (TYPE_REF_P (arg2
))
23955 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23956 arg2
= TREE_TYPE (arg2
);
23957 quals2
= cp_type_quals (arg2
);
23960 arg1
= TYPE_MAIN_VARIANT (arg1
);
23961 arg2
= TYPE_MAIN_VARIANT (arg2
);
23963 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23965 int i
, len2
= remaining_arguments (args2
);
23966 tree parmvec
= make_tree_vec (1);
23967 tree argvec
= make_tree_vec (len2
);
23970 /* Setup the parameter vector, which contains only ARG1. */
23971 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23973 /* Setup the argument vector, which contains the remaining
23975 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23976 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23978 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23979 argvec
, DEDUCE_EXACT
,
23980 /*subr=*/true, /*explain_p=*/false)
23983 /* We cannot deduce in the other direction, because ARG1 is
23984 a pack expansion but ARG2 is not. */
23987 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23989 int i
, len1
= remaining_arguments (args1
);
23990 tree parmvec
= make_tree_vec (1);
23991 tree argvec
= make_tree_vec (len1
);
23994 /* Setup the parameter vector, which contains only ARG1. */
23995 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23997 /* Setup the argument vector, which contains the remaining
23999 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24000 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24002 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24003 argvec
, DEDUCE_EXACT
,
24004 /*subr=*/true, /*explain_p=*/false)
24007 /* We cannot deduce in the other direction, because ARG2 is
24008 a pack expansion but ARG1 is not.*/
24014 /* The normal case, where neither argument is a pack
24016 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24017 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24019 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24020 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24024 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24025 arg2, then arg2 is not as specialized as arg1. */
24031 /* "If, for a given type, deduction succeeds in both directions
24032 (i.e., the types are identical after the transformations above)
24033 and both P and A were reference types (before being replaced with
24034 the type referred to above):
24035 - if the type from the argument template was an lvalue reference and
24036 the type from the parameter template was not, the argument type is
24037 considered to be more specialized than the other; otherwise,
24038 - if the type from the argument template is more cv-qualified
24039 than the type from the parameter template (as described above),
24040 the argument type is considered to be more specialized than the other;
24042 - neither type is more specialized than the other." */
24044 if (deduce1
&& deduce2
)
24046 if (ref1
&& ref2
&& ref1
!= ref2
)
24053 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24055 if ((quals1
& quals2
) == quals2
)
24057 if ((quals1
& quals2
) == quals1
)
24062 if (lose1
&& lose2
)
24063 /* We've failed to deduce something in either direction.
24064 These must be unordered. */
24067 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24068 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24069 /* We have already processed all of the arguments in our
24070 handing of the pack expansion type. */
24073 args1
= TREE_CHAIN (args1
);
24074 args2
= TREE_CHAIN (args2
);
24077 /* "In most cases, all template parameters must have values in order for
24078 deduction to succeed, but for partial ordering purposes a template
24079 parameter may remain without a value provided it is not used in the
24080 types being used for partial ordering."
24082 Thus, if we are missing any of the targs1 we need to substitute into
24083 origs1, then pat2 is not as specialized as pat1. This can happen when
24084 there is a nondeduced context. */
24085 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24087 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24090 processing_template_decl
--;
24092 /* If both deductions succeed, the partial ordering selects the more
24093 constrained template. */
24094 if (!lose1
&& !lose2
)
24096 int winner
= more_constrained (decl1
, decl2
);
24099 else if (winner
< 0)
24103 /* All things being equal, if the next argument is a pack expansion
24104 for one function but not for the other, prefer the
24105 non-variadic function. FIXME this is bogus; see c++/41958. */
24107 && args1
&& TREE_VALUE (args1
)
24108 && args2
&& TREE_VALUE (args2
))
24110 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24111 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24114 if (lose1
== lose2
)
24122 /* Determine which of two partial specializations of TMPL is more
24125 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24126 to the first partial specialization. The TREE_PURPOSE is the
24127 innermost set of template parameters for the partial
24128 specialization. PAT2 is similar, but for the second template.
24130 Return 1 if the first partial specialization is more specialized;
24131 -1 if the second is more specialized; 0 if neither is more
24134 See [temp.class.order] for information about determining which of
24135 two templates is more specialized. */
24138 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24142 bool any_deductions
= false;
24144 tree tmpl1
= TREE_VALUE (pat1
);
24145 tree tmpl2
= TREE_VALUE (pat2
);
24146 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24147 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24149 /* Just like what happens for functions, if we are ordering between
24150 different template specializations, we may encounter dependent
24151 types in the arguments, and we need our dependency check functions
24152 to behave correctly. */
24153 ++processing_template_decl
;
24154 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24158 any_deductions
= true;
24161 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24165 any_deductions
= true;
24167 --processing_template_decl
;
24169 /* If both deductions succeed, the partial ordering selects the more
24170 constrained template. */
24171 if (!winner
&& any_deductions
)
24172 winner
= more_constrained (tmpl1
, tmpl2
);
24174 /* In the case of a tie where at least one of the templates
24175 has a parameter pack at the end, the template with the most
24176 non-packed parameters wins. */
24179 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24180 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24182 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24183 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24184 int len1
= TREE_VEC_LENGTH (args1
);
24185 int len2
= TREE_VEC_LENGTH (args2
);
24187 /* We don't count the pack expansion at the end. */
24188 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24190 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24195 else if (len1
< len2
)
24202 /* Return the template arguments that will produce the function signature
24203 DECL from the function template FN, with the explicit template
24204 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24205 also match. Return NULL_TREE if no satisfactory arguments could be
24209 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24211 int ntparms
= DECL_NTPARMS (fn
);
24212 tree targs
= make_tree_vec (ntparms
);
24213 tree decl_type
= TREE_TYPE (decl
);
24214 tree decl_arg_types
;
24216 unsigned int nargs
, ix
;
24219 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24221 /* Never do unification on the 'this' parameter. */
24222 decl_arg_types
= skip_artificial_parms_for (decl
,
24223 TYPE_ARG_TYPES (decl_type
));
24225 nargs
= list_length (decl_arg_types
);
24226 args
= XALLOCAVEC (tree
, nargs
);
24227 for (arg
= decl_arg_types
, ix
= 0;
24228 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24229 arg
= TREE_CHAIN (arg
), ++ix
)
24230 args
[ix
] = TREE_VALUE (arg
);
24232 if (fn_type_unification (fn
, explicit_args
, targs
,
24234 (check_rettype
|| DECL_CONV_FN_P (fn
)
24235 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24236 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24237 /*explain_p=*/false,
24239 == error_mark_node
)
24245 /* Return the innermost template arguments that, when applied to a partial
24246 specialization SPEC_TMPL of TMPL, yield the ARGS.
24248 For example, suppose we have:
24250 template <class T, class U> struct S {};
24251 template <class T> struct S<T*, int> {};
24253 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24254 partial specialization and the ARGS will be {double*, int}. The resulting
24255 vector will be {double}, indicating that `T' is bound to `double'. */
24258 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24260 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24262 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24263 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24265 tree innermost_deduced_args
;
24267 innermost_deduced_args
= make_tree_vec (ntparms
);
24268 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24270 deduced_args
= copy_node (args
);
24271 SET_TMPL_ARGS_LEVEL (deduced_args
,
24272 TMPL_ARGS_DEPTH (deduced_args
),
24273 innermost_deduced_args
);
24276 deduced_args
= innermost_deduced_args
;
24278 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24280 if (unify (tparms
, deduced_args
,
24281 INNERMOST_TEMPLATE_ARGS (spec_args
),
24282 INNERMOST_TEMPLATE_ARGS (args
),
24283 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24286 for (i
= 0; i
< ntparms
; ++i
)
24287 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24289 if (!tried_array_deduction
)
24291 try_array_deduction (tparms
, innermost_deduced_args
,
24292 INNERMOST_TEMPLATE_ARGS (spec_args
));
24293 tried_array_deduction
= true;
24294 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24300 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24302 excessive_deduction_depth
= true;
24306 /* Verify that nondeduced template arguments agree with the type
24307 obtained from argument deduction.
24311 struct A { typedef int X; };
24312 template <class T, class U> struct C {};
24313 template <class T> struct C<T, typename T::X> {};
24315 Then with the instantiation `C<A, int>', we can deduce that
24316 `T' is `A' but unify () does not check whether `typename T::X'
24318 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24320 if (spec_args
!= error_mark_node
)
24321 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24322 INNERMOST_TEMPLATE_ARGS (spec_args
),
24323 tmpl
, tf_none
, false, false);
24325 pop_tinst_level ();
24327 if (spec_args
== error_mark_node
24328 /* We only need to check the innermost arguments; the other
24329 arguments will always agree. */
24330 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24331 INNERMOST_TEMPLATE_ARGS (args
)))
24334 /* Now that we have bindings for all of the template arguments,
24335 ensure that the arguments deduced for the template template
24336 parameters have compatible template parameter lists. See the use
24337 of template_template_parm_bindings_ok_p in fn_type_unification
24338 for more information. */
24339 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24342 return deduced_args
;
24345 // Compare two function templates T1 and T2 by deducing bindings
24346 // from one against the other. If both deductions succeed, compare
24347 // constraints to see which is more constrained.
24349 more_specialized_inst (tree t1
, tree t2
)
24354 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24360 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24366 // If both deductions succeed, then one may be more constrained.
24367 if (count
== 2 && fate
== 0)
24368 fate
= more_constrained (t1
, t2
);
24373 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24374 Return the TREE_LIST node with the most specialized template, if
24375 any. If there is no most specialized template, the error_mark_node
24378 Note that this function does not look at, or modify, the
24379 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24380 returned is one of the elements of INSTANTIATIONS, callers may
24381 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24382 and retrieve it from the value returned. */
24385 most_specialized_instantiation (tree templates
)
24389 ++processing_template_decl
;
24392 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24394 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24395 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24400 /* Equally specialized, move to next function. If there
24401 is no next function, nothing's most specialized. */
24402 fn
= TREE_CHAIN (fn
);
24410 /* Now verify that champ is better than everything earlier in the
24411 instantiation list. */
24412 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24413 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24420 processing_template_decl
--;
24423 return error_mark_node
;
24428 /* If DECL is a specialization of some template, return the most
24429 general such template. Otherwise, returns NULL_TREE.
24431 For example, given:
24433 template <class T> struct S { template <class U> void f(U); };
24435 if TMPL is `template <class U> void S<int>::f(U)' this will return
24436 the full template. This function will not trace past partial
24437 specializations, however. For example, given in addition:
24439 template <class T> struct S<T*> { template <class U> void f(U); };
24441 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24442 `template <class T> template <class U> S<T*>::f(U)'. */
24445 most_general_template (tree decl
)
24447 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24449 if (tree tinfo
= get_template_info (decl
))
24450 decl
= TI_TEMPLATE (tinfo
);
24451 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24452 template friend, or a FIELD_DECL for a capture pack. */
24453 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24457 /* Look for more and more general templates. */
24458 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24460 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24461 (See cp-tree.h for details.) */
24462 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24465 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24466 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24467 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24470 /* Stop if we run into an explicitly specialized class template. */
24471 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24472 && DECL_CONTEXT (decl
)
24473 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24476 decl
= DECL_TI_TEMPLATE (decl
);
24482 /* Return the most specialized of the template partial specializations
24483 which can produce TARGET, a specialization of some class or variable
24484 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24485 a TEMPLATE_DECL node corresponding to the partial specialization, while
24486 the TREE_PURPOSE is the set of template arguments that must be
24487 substituted into the template pattern in order to generate TARGET.
24489 If the choice of partial specialization is ambiguous, a diagnostic
24490 is issued, and the error_mark_node is returned. If there are no
24491 partial specializations matching TARGET, then NULL_TREE is
24492 returned, indicating that the primary template should be used. */
24495 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24497 tree list
= NULL_TREE
;
24502 tree outer_args
= NULL_TREE
;
24505 if (TYPE_P (target
))
24507 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24508 tmpl
= TI_TEMPLATE (tinfo
);
24509 args
= TI_ARGS (tinfo
);
24511 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24513 tmpl
= TREE_OPERAND (target
, 0);
24514 args
= TREE_OPERAND (target
, 1);
24516 else if (VAR_P (target
))
24518 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24519 tmpl
= TI_TEMPLATE (tinfo
);
24520 args
= TI_ARGS (tinfo
);
24523 gcc_unreachable ();
24525 tree main_tmpl
= most_general_template (tmpl
);
24527 /* For determining which partial specialization to use, only the
24528 innermost args are interesting. */
24529 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24531 outer_args
= strip_innermost_template_args (args
, 1);
24532 args
= INNERMOST_TEMPLATE_ARGS (args
);
24535 /* The caller hasn't called push_to_top_level yet, but we need
24536 get_partial_spec_bindings to be done in non-template context so that we'll
24537 fully resolve everything. */
24538 processing_template_decl_sentinel ptds
;
24540 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24543 tree spec_tmpl
= TREE_VALUE (t
);
24547 /* Substitute in the template args from the enclosing class. */
24548 ++processing_template_decl
;
24549 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24550 --processing_template_decl
;
24553 if (spec_tmpl
== error_mark_node
)
24554 return error_mark_node
;
24556 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24560 spec_args
= add_to_template_args (outer_args
, spec_args
);
24562 /* Keep the candidate only if the constraints are satisfied,
24563 or if we're not compiling with concepts. */
24565 || constraints_satisfied_p (spec_tmpl
, spec_args
))
24567 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
24568 TREE_TYPE (list
) = TREE_TYPE (t
);
24576 ambiguous_p
= false;
24579 t
= TREE_CHAIN (t
);
24580 for (; t
; t
= TREE_CHAIN (t
))
24582 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24589 t
= TREE_CHAIN (t
);
24592 ambiguous_p
= true;
24601 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24603 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24606 ambiguous_p
= true;
24614 char *spaces
= NULL
;
24615 if (!(complain
& tf_error
))
24616 return error_mark_node
;
24617 if (TYPE_P (target
))
24618 error ("ambiguous template instantiation for %q#T", target
);
24620 error ("ambiguous template instantiation for %q#D", target
);
24621 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24622 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24624 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24625 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24626 "%s %#qS", spaces
? spaces
: str
, subst
);
24627 spaces
= spaces
? spaces
: get_spaces (str
);
24630 return error_mark_node
;
24636 /* Explicitly instantiate DECL. */
24639 do_decl_instantiation (tree decl
, tree storage
)
24641 tree result
= NULL_TREE
;
24644 if (!decl
|| decl
== error_mark_node
)
24645 /* An error occurred, for which grokdeclarator has already issued
24646 an appropriate message. */
24648 else if (! DECL_LANG_SPECIFIC (decl
))
24650 error ("explicit instantiation of non-template %q#D", decl
);
24653 else if (DECL_DECLARED_CONCEPT_P (decl
))
24656 error ("explicit instantiation of variable concept %q#D", decl
);
24658 error ("explicit instantiation of function concept %q#D", decl
);
24662 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24663 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24665 if (VAR_P (decl
) && !var_templ
)
24667 /* There is an asymmetry here in the way VAR_DECLs and
24668 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24669 the latter, the DECL we get back will be marked as a
24670 template instantiation, and the appropriate
24671 DECL_TEMPLATE_INFO will be set up. This does not happen for
24672 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24673 should handle VAR_DECLs as it currently handles
24675 if (!DECL_CLASS_SCOPE_P (decl
))
24677 error ("%qD is not a static data member of a class template", decl
);
24680 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24681 if (!result
|| !VAR_P (result
))
24683 error ("no matching template for %qD found", decl
);
24686 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24688 error ("type %qT for explicit instantiation %qD does not match "
24689 "declared type %qT", TREE_TYPE (result
), decl
,
24694 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24696 error ("explicit instantiation of %q#D", decl
);
24702 /* Check for various error cases. Note that if the explicit
24703 instantiation is valid the RESULT will currently be marked as an
24704 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24705 until we get here. */
24707 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24709 /* DR 259 [temp.spec].
24711 Both an explicit instantiation and a declaration of an explicit
24712 specialization shall not appear in a program unless the explicit
24713 instantiation follows a declaration of the explicit specialization.
24715 For a given set of template parameters, if an explicit
24716 instantiation of a template appears after a declaration of an
24717 explicit specialization for that template, the explicit
24718 instantiation has no effect. */
24721 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24725 No program shall explicitly instantiate any template more
24728 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24729 the first instantiation was `extern' and the second is not,
24730 and EXTERN_P for the opposite case. */
24731 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24732 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24733 /* If an "extern" explicit instantiation follows an ordinary
24734 explicit instantiation, the template is instantiated. */
24738 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24740 error ("no matching template for %qD found", result
);
24743 else if (!DECL_TEMPLATE_INFO (result
))
24745 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24749 if (storage
== NULL_TREE
)
24751 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24753 if (cxx_dialect
== cxx98
)
24754 pedwarn (input_location
, OPT_Wpedantic
,
24755 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24760 error ("storage class %qD applied to template instantiation", storage
);
24762 check_explicit_instantiation_namespace (result
);
24763 mark_decl_instantiated (result
, extern_p
);
24765 instantiate_decl (result
, /*defer_ok=*/true,
24766 /*expl_inst_class_mem_p=*/false);
24770 mark_class_instantiated (tree t
, int extern_p
)
24772 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24773 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24774 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24775 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24778 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24779 rest_of_type_compilation (t
, 1);
24783 /* Called from do_type_instantiation through binding_table_foreach to
24784 do recursive instantiation for the type bound in ENTRY. */
24786 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24788 tree storage
= *(tree
*) data
;
24790 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24791 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24792 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24793 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24796 /* Perform an explicit instantiation of template class T. STORAGE, if
24797 non-null, is the RID for extern, inline or static. COMPLAIN is
24798 nonzero if this is called from the parser, zero if called recursively,
24799 since the standard is unclear (as detailed below). */
24802 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24807 int previous_instantiation_extern_p
= 0;
24809 if (TREE_CODE (t
) == TYPE_DECL
)
24812 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24815 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24817 error ("explicit instantiation of non-class template %qD", tmpl
);
24819 error ("explicit instantiation of non-template type %qT", t
);
24825 if (!COMPLETE_TYPE_P (t
))
24827 if (complain
& tf_error
)
24828 error ("explicit instantiation of %q#T before definition of template",
24833 if (storage
!= NULL_TREE
)
24835 if (storage
== ridpointers
[(int) RID_EXTERN
])
24837 if (cxx_dialect
== cxx98
)
24838 pedwarn (input_location
, OPT_Wpedantic
,
24839 "ISO C++ 1998 forbids the use of %<extern%> on "
24840 "explicit instantiations");
24843 pedwarn (input_location
, OPT_Wpedantic
,
24844 "ISO C++ forbids the use of %qE"
24845 " on explicit instantiations", storage
);
24847 if (storage
== ridpointers
[(int) RID_INLINE
])
24849 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24851 else if (storage
== ridpointers
[(int) RID_STATIC
])
24855 error ("storage class %qD applied to template instantiation",
24861 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24863 /* DR 259 [temp.spec].
24865 Both an explicit instantiation and a declaration of an explicit
24866 specialization shall not appear in a program unless the explicit
24867 instantiation follows a declaration of the explicit specialization.
24869 For a given set of template parameters, if an explicit
24870 instantiation of a template appears after a declaration of an
24871 explicit specialization for that template, the explicit
24872 instantiation has no effect. */
24875 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24879 No program shall explicitly instantiate any template more
24882 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24883 instantiation was `extern'. If EXTERN_P then the second is.
24884 These cases are OK. */
24885 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24887 if (!previous_instantiation_extern_p
&& !extern_p
24888 && (complain
& tf_error
))
24889 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24891 /* If we've already instantiated the template, just return now. */
24892 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24896 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24897 mark_class_instantiated (t
, extern_p
);
24902 /* In contrast to implicit instantiation, where only the
24903 declarations, and not the definitions, of members are
24904 instantiated, we have here:
24908 The explicit instantiation of a class template specialization
24909 implies the instantiation of all of its members not
24910 previously explicitly specialized in the translation unit
24911 containing the explicit instantiation.
24913 Of course, we can't instantiate member template classes, since we
24914 don't have any arguments for them. Note that the standard is
24915 unclear on whether the instantiation of the members are
24916 *explicit* instantiations or not. However, the most natural
24917 interpretation is that it should be an explicit
24919 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24921 || (TREE_CODE (fld
) == FUNCTION_DECL
24923 && user_provided_p (fld
)))
24924 && DECL_TEMPLATE_INSTANTIATION (fld
))
24926 mark_decl_instantiated (fld
, extern_p
);
24928 instantiate_decl (fld
, /*defer_ok=*/true,
24929 /*expl_inst_class_mem_p=*/true);
24932 if (CLASSTYPE_NESTED_UTDS (t
))
24933 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24934 bt_instantiate_type_proc
, &storage
);
24937 /* Given a function DECL, which is a specialization of TMPL, modify
24938 DECL to be a re-instantiation of TMPL with the same template
24939 arguments. TMPL should be the template into which tsubst'ing
24940 should occur for DECL, not the most general template.
24942 One reason for doing this is a scenario like this:
24945 void f(const T&, int i);
24947 void g() { f(3, 7); }
24950 void f(const T& t, const int i) { }
24952 Note that when the template is first instantiated, with
24953 instantiate_template, the resulting DECL will have no name for the
24954 first parameter, and the wrong type for the second. So, when we go
24955 to instantiate the DECL, we regenerate it. */
24958 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24960 /* The arguments used to instantiate DECL, from the most general
24964 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24966 /* Make sure that we can see identifiers, and compute access
24968 push_access_scope (decl
);
24970 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24978 args_depth
= TMPL_ARGS_DEPTH (args
);
24979 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24980 if (args_depth
> parms_depth
)
24981 args
= get_innermost_template_args (args
, parms_depth
);
24983 /* Instantiate a dynamic exception-specification. noexcept will be
24985 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24986 if (TREE_VALUE (raises
))
24988 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24989 args
, tf_error
, NULL_TREE
,
24990 /*defer_ok*/false);
24991 if (specs
&& specs
!= error_mark_node
)
24992 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24996 /* Merge parameter declarations. */
24997 decl_parm
= skip_artificial_parms_for (decl
,
24998 DECL_ARGUMENTS (decl
));
25000 = skip_artificial_parms_for (code_pattern
,
25001 DECL_ARGUMENTS (code_pattern
));
25002 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25007 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25008 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25009 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25011 parm_type
= type_decays_to (parm_type
);
25012 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25013 TREE_TYPE (decl_parm
) = parm_type
;
25014 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25015 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25017 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25018 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25020 decl_parm
= DECL_CHAIN (decl_parm
);
25021 pattern_parm
= DECL_CHAIN (pattern_parm
);
25023 /* Merge any parameters that match with the function parameter
25025 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25028 tree expanded_types
;
25029 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25030 the parameters in this function parameter pack. */
25031 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25032 args
, tf_error
, NULL_TREE
);
25033 len
= TREE_VEC_LENGTH (expanded_types
);
25034 for (i
= 0; i
< len
; i
++)
25039 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25040 /* Rename the parameter to include the index. */
25041 DECL_NAME (decl_parm
) =
25042 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25043 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25044 parm_type
= type_decays_to (parm_type
);
25045 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25046 TREE_TYPE (decl_parm
) = parm_type
;
25047 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25048 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25050 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25051 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25053 decl_parm
= DECL_CHAIN (decl_parm
);
25056 /* Merge additional specifiers from the CODE_PATTERN. */
25057 if (DECL_DECLARED_INLINE_P (code_pattern
)
25058 && !DECL_DECLARED_INLINE_P (decl
))
25059 DECL_DECLARED_INLINE_P (decl
) = 1;
25061 maybe_instantiate_noexcept (decl
, tf_error
);
25063 else if (VAR_P (decl
))
25065 start_lambda_scope (decl
);
25066 DECL_INITIAL (decl
) =
25067 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25068 tf_error
, DECL_TI_TEMPLATE (decl
));
25069 finish_lambda_scope ();
25070 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25071 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25072 tf_error
, DECL_TI_TEMPLATE (decl
));
25075 gcc_unreachable ();
25077 pop_access_scope (decl
);
25080 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25081 substituted to get DECL. */
25084 template_for_substitution (tree decl
)
25086 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25088 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25089 for the instantiation. This is not always the most general
25090 template. Consider, for example:
25093 struct S { template <class U> void f();
25094 template <> void f<int>(); };
25096 and an instantiation of S<double>::f<int>. We want TD to be the
25097 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25098 while (/* An instantiation cannot have a definition, so we need a
25099 more general template. */
25100 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25101 /* We must also deal with friend templates. Given:
25103 template <class T> struct S {
25104 template <class U> friend void f() {};
25107 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25108 so far as the language is concerned, but that's still
25109 where we get the pattern for the instantiation from. On
25110 other hand, if the definition comes outside the class, say:
25112 template <class T> struct S {
25113 template <class U> friend void f();
25115 template <class U> friend void f() {}
25117 we don't need to look any further. That's what the check for
25118 DECL_INITIAL is for. */
25119 || (TREE_CODE (decl
) == FUNCTION_DECL
25120 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25121 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25123 /* The present template, TD, should not be a definition. If it
25124 were a definition, we should be using it! Note that we
25125 cannot restructure the loop to just keep going until we find
25126 a template with a definition, since that might go too far if
25127 a specialization was declared, but not defined. */
25129 /* Fetch the more general template. */
25130 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25136 /* Returns true if we need to instantiate this template instance even if we
25137 know we aren't going to emit it. */
25140 always_instantiate_p (tree decl
)
25142 /* We always instantiate inline functions so that we can inline them. An
25143 explicit instantiation declaration prohibits implicit instantiation of
25144 non-inline functions. With high levels of optimization, we would
25145 normally inline non-inline functions -- but we're not allowed to do
25146 that for "extern template" functions. Therefore, we check
25147 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25148 return ((TREE_CODE (decl
) == FUNCTION_DECL
25149 && (DECL_DECLARED_INLINE_P (decl
)
25150 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25151 /* And we need to instantiate static data members so that
25152 their initializers are available in integral constant
25155 && decl_maybe_constant_var_p (decl
)));
25158 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25159 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25160 error, true otherwise. */
25163 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25165 tree fntype
, spec
, noex
, clone
;
25167 /* Don't instantiate a noexcept-specification from template context. */
25168 if (processing_template_decl
25169 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25172 if (DECL_MAYBE_DELETED (fn
))
25174 if (fn
== current_function_decl
)
25175 /* We're in start_preparsed_function, keep going. */
25179 synthesize_method (fn
);
25181 return !DECL_MAYBE_DELETED (fn
);
25184 if (DECL_CLONED_FUNCTION_P (fn
))
25185 fn
= DECL_CLONED_FUNCTION (fn
);
25187 tree orig_fn
= NULL_TREE
;
25188 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25189 its FUNCTION_DECL for the rest of this function -- push_access_scope
25190 doesn't accept TEMPLATE_DECLs. */
25191 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25194 fn
= DECL_TEMPLATE_RESULT (fn
);
25197 fntype
= TREE_TYPE (fn
);
25198 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25200 if (!spec
|| !TREE_PURPOSE (spec
))
25203 noex
= TREE_PURPOSE (spec
);
25205 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25207 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25208 bool added
= false;
25209 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25211 spec
= get_defaulted_eh_spec (fn
, complain
);
25212 if (spec
== error_mark_node
)
25213 /* This might have failed because of an unparsed DMI, so
25214 let's try again later. */
25217 else if (!(added
= !fns
->add (fn
)))
25219 /* If hash_set::add returns true, the element was already there. */
25220 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25221 DECL_SOURCE_LOCATION (fn
));
25223 "exception specification of %qD depends on itself",
25225 spec
= noexcept_false_spec
;
25227 else if (push_tinst_level (fn
))
25229 push_to_top_level ();
25230 push_access_scope (fn
);
25231 push_deferring_access_checks (dk_no_deferred
);
25232 input_location
= DECL_SOURCE_LOCATION (fn
);
25234 /* If needed, set current_class_ptr for the benefit of
25235 tsubst_copy/PARM_DECL. */
25236 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25237 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25239 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25240 current_class_ptr
= NULL_TREE
;
25241 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25242 current_class_ptr
= this_parm
;
25245 /* If this function is represented by a TEMPLATE_DECL, then
25246 the deferred noexcept-specification might still contain
25247 dependent types, even after substitution. And we need the
25248 dependency check functions to work in build_noexcept_spec. */
25250 ++processing_template_decl
;
25252 /* Do deferred instantiation of the noexcept-specifier. */
25253 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25254 DEFERRED_NOEXCEPT_ARGS (noex
),
25255 tf_warning_or_error
, fn
,
25256 /*function_p=*/false,
25259 /* Build up the noexcept-specification. */
25260 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25263 --processing_template_decl
;
25265 pop_deferring_access_checks ();
25266 pop_access_scope (fn
);
25267 pop_tinst_level ();
25268 pop_from_top_level ();
25271 spec
= noexcept_false_spec
;
25276 if (spec
== error_mark_node
)
25278 /* This failed with a hard error, so let's go with false. */
25279 gcc_assert (seen_error ());
25280 spec
= noexcept_false_spec
;
25283 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25285 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25288 FOR_EACH_CLONE (clone
, fn
)
25290 if (TREE_TYPE (clone
) == fntype
)
25291 TREE_TYPE (clone
) = TREE_TYPE (fn
);
25293 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
25299 /* We're starting to process the function INST, an instantiation of PATTERN;
25300 add their parameters to local_specializations. */
25303 register_parameter_specializations (tree pattern
, tree inst
)
25305 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25306 tree spec_parm
= DECL_ARGUMENTS (inst
);
25307 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25309 register_local_specialization (spec_parm
, tmpl_parm
);
25310 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25311 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25313 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25315 if (!DECL_PACK_P (tmpl_parm
)
25316 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25318 register_local_specialization (spec_parm
, tmpl_parm
);
25319 spec_parm
= DECL_CHAIN (spec_parm
);
25323 /* Register the (value) argument pack as a specialization of
25324 TMPL_PARM, then move on. */
25325 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25326 register_local_specialization (argpack
, tmpl_parm
);
25329 gcc_assert (!spec_parm
);
25332 /* Produce the definition of D, a _DECL generated from a template. If
25333 DEFER_OK is true, then we don't have to actually do the
25334 instantiation now; we just have to do it sometime. Normally it is
25335 an error if this is an explicit instantiation but D is undefined.
25336 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25337 instantiated class template. */
25340 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25342 tree tmpl
= DECL_TI_TEMPLATE (d
);
25349 bool pattern_defined
;
25350 location_t saved_loc
= input_location
;
25351 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25352 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25356 /* This function should only be used to instantiate templates for
25357 functions and static member variables. */
25358 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25360 /* A concept is never instantiated. */
25361 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25363 /* Variables are never deferred; if instantiation is required, they
25364 are instantiated right away. That allows for better code in the
25365 case that an expression refers to the value of the variable --
25366 if the variable has a constant value the referring expression can
25367 take advantage of that fact. */
25371 /* Don't instantiate cloned functions. Instead, instantiate the
25372 functions they cloned. */
25373 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25374 d
= DECL_CLONED_FUNCTION (d
);
25376 if (DECL_TEMPLATE_INSTANTIATED (d
)
25377 || (TREE_CODE (d
) == FUNCTION_DECL
25378 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25379 || DECL_TEMPLATE_SPECIALIZATION (d
))
25380 /* D has already been instantiated or explicitly specialized, so
25381 there's nothing for us to do here.
25383 It might seem reasonable to check whether or not D is an explicit
25384 instantiation, and, if so, stop here. But when an explicit
25385 instantiation is deferred until the end of the compilation,
25386 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25387 the instantiation. */
25390 /* Check to see whether we know that this template will be
25391 instantiated in some other file, as with "extern template"
25393 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25395 /* In general, we do not instantiate such templates. */
25396 if (external_p
&& !always_instantiate_p (d
))
25399 gen_tmpl
= most_general_template (tmpl
);
25400 gen_args
= DECL_TI_ARGS (d
);
25402 if (tmpl
!= gen_tmpl
)
25403 /* We should already have the extra args. */
25404 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25405 == TMPL_ARGS_DEPTH (gen_args
));
25406 /* And what's in the hash table should match D. */
25407 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
25408 || spec
== NULL_TREE
);
25410 /* This needs to happen before any tsubsting. */
25411 if (! push_tinst_level (d
))
25414 timevar_push (TV_TEMPLATE_INST
);
25416 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25417 for the instantiation. */
25418 td
= template_for_substitution (d
);
25423 /* Look up an explicit specialization, if any. */
25424 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25425 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25426 if (elt
&& elt
!= error_mark_node
)
25428 td
= TREE_VALUE (elt
);
25429 args
= TREE_PURPOSE (elt
);
25433 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25435 /* We should never be trying to instantiate a member of a class
25436 template or partial specialization. */
25437 gcc_assert (d
!= code_pattern
);
25439 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25440 || DECL_TEMPLATE_SPECIALIZATION (td
))
25441 /* In the case of a friend template whose definition is provided
25442 outside the class, we may have too many arguments. Drop the
25443 ones we don't need. The same is true for specializations. */
25444 args
= get_innermost_template_args
25445 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25447 if (TREE_CODE (d
) == FUNCTION_DECL
)
25449 deleted_p
= DECL_DELETED_FN (code_pattern
);
25450 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25451 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25452 || DECL_DEFAULTED_FN (code_pattern
)
25458 if (DECL_CLASS_SCOPE_P (code_pattern
))
25459 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25461 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25464 /* We may be in the middle of deferred access check. Disable it now. */
25465 push_deferring_access_checks (dk_no_deferred
);
25467 /* Unless an explicit instantiation directive has already determined
25468 the linkage of D, remember that a definition is available for
25470 if (pattern_defined
25471 && !DECL_INTERFACE_KNOWN (d
)
25472 && !DECL_NOT_REALLY_EXTERN (d
))
25473 mark_definable (d
);
25475 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25476 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25477 input_location
= DECL_SOURCE_LOCATION (d
);
25479 /* If D is a member of an explicitly instantiated class template,
25480 and no definition is available, treat it like an implicit
25482 if (!pattern_defined
&& expl_inst_class_mem_p
25483 && DECL_EXPLICIT_INSTANTIATION (d
))
25485 /* Leave linkage flags alone on instantiations with anonymous
25487 if (TREE_PUBLIC (d
))
25489 DECL_NOT_REALLY_EXTERN (d
) = 0;
25490 DECL_INTERFACE_KNOWN (d
) = 0;
25492 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25495 /* Defer all other templates, unless we have been explicitly
25496 forbidden from doing so. */
25497 if (/* If there is no definition, we cannot instantiate the
25500 /* If it's OK to postpone instantiation, do so. */
25502 /* If this is a static data member that will be defined
25503 elsewhere, we don't want to instantiate the entire data
25504 member, but we do want to instantiate the initializer so that
25505 we can substitute that elsewhere. */
25506 || (external_p
&& VAR_P (d
))
25507 /* Handle here a deleted function too, avoid generating
25508 its body (c++/61080). */
25511 /* The definition of the static data member is now required so
25512 we must substitute the initializer. */
25514 && !DECL_INITIAL (d
)
25515 && DECL_INITIAL (code_pattern
))
25519 bool const_init
= false;
25520 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25522 ns
= decl_namespace_context (d
);
25523 push_nested_namespace (ns
);
25525 push_nested_class (DECL_CONTEXT (d
));
25526 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25528 tf_warning_or_error
, NULL_TREE
,
25529 /*integral_constant_expression_p=*/false);
25530 /* If instantiating the initializer involved instantiating this
25531 again, don't call cp_finish_decl twice. */
25532 if (!DECL_INITIAL (d
))
25534 /* Make sure the initializer is still constant, in case of
25535 circular dependency (template/instantiate6.C). */
25537 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25538 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25539 /*asmspec_tree=*/NULL_TREE
,
25540 LOOKUP_ONLYCONVERTING
);
25543 pop_nested_class ();
25544 pop_nested_namespace (ns
);
25547 /* We restore the source position here because it's used by
25548 add_pending_template. */
25549 input_location
= saved_loc
;
25551 if (at_eof
&& !pattern_defined
25552 && DECL_EXPLICIT_INSTANTIATION (d
)
25553 && DECL_NOT_REALLY_EXTERN (d
))
25556 The definition of a non-exported function template, a
25557 non-exported member function template, or a non-exported
25558 member function or static data member of a class template
25559 shall be present in every translation unit in which it is
25560 explicitly instantiated. */
25561 permerror (input_location
, "explicit instantiation of %qD "
25562 "but no definition available", d
);
25564 /* If we're in unevaluated context, we just wanted to get the
25565 constant value; this isn't an odr use, so don't queue
25566 a full instantiation. */
25567 if (cp_unevaluated_operand
!= 0)
25569 /* ??? Historically, we have instantiated inline functions, even
25570 when marked as "extern template". */
25571 if (!(external_p
&& VAR_P (d
)))
25572 add_pending_template (d
);
25576 bool push_to_top
, nested
;
25578 fn_context
= decl_function_context (d
);
25579 if (LAMBDA_FUNCTION_P (d
))
25580 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25581 fn_context
= NULL_TREE
;
25582 nested
= current_function_decl
!= NULL_TREE
;
25583 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25585 vec
<tree
> omp_privatization_save
;
25587 save_omp_privatization_clauses (omp_privatization_save
);
25590 push_to_top_level ();
25593 gcc_assert (!processing_template_decl
);
25594 push_function_context ();
25595 cp_unevaluated_operand
= 0;
25596 c_inhibit_evaluation_warnings
= 0;
25601 /* The variable might be a lambda's extra scope, and that
25602 lambda's visibility depends on D's. */
25603 maybe_commonize_var (d
);
25604 determine_visibility (d
);
25607 /* Mark D as instantiated so that recursive calls to
25608 instantiate_decl do not try to instantiate it again. */
25609 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25611 /* Regenerate the declaration in case the template has been modified
25612 by a subsequent redeclaration. */
25613 regenerate_decl_from_template (d
, td
, args
);
25615 /* We already set the file and line above. Reset them now in case
25616 they changed as a result of calling regenerate_decl_from_template. */
25617 input_location
= DECL_SOURCE_LOCATION (d
);
25622 bool const_init
= false;
25624 /* Clear out DECL_RTL; whatever was there before may not be right
25625 since we've reset the type of the declaration. */
25626 SET_DECL_RTL (d
, NULL
);
25627 DECL_IN_AGGR_P (d
) = 0;
25629 /* The initializer is placed in DECL_INITIAL by
25630 regenerate_decl_from_template so we don't need to
25631 push/pop_access_scope again here. Pull it out so that
25632 cp_finish_decl can process it. */
25633 init
= DECL_INITIAL (d
);
25634 DECL_INITIAL (d
) = NULL_TREE
;
25635 DECL_INITIALIZED_P (d
) = 0;
25637 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25638 initializer. That function will defer actual emission until
25639 we have a chance to determine linkage. */
25640 DECL_EXTERNAL (d
) = 0;
25642 /* Enter the scope of D so that access-checking works correctly. */
25643 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25645 push_nested_class (DECL_CONTEXT (d
));
25647 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25648 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25649 ? LOOKUP_CONSTINIT
: 0);
25650 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25653 pop_nested_class ();
25655 if (variable_template_p (gen_tmpl
))
25656 note_variable_template_instantiation (d
);
25658 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25659 synthesize_method (d
);
25660 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25662 /* Set up the list of local specializations. */
25663 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25664 tree block
= NULL_TREE
;
25666 /* Set up context. */
25667 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25668 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25669 block
= push_stmt_list ();
25671 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25673 /* Some typedefs referenced from within the template code need to be
25674 access checked at template instantiation time, i.e now. These
25675 types were added to the template at parsing time. Let's get those
25676 and perform the access checks then. */
25677 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
25680 /* Create substitution entries for the parameters. */
25681 register_parameter_specializations (code_pattern
, d
);
25683 /* Substitute into the body of the function. */
25684 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25685 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25686 tf_warning_or_error
, tmpl
);
25689 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25690 tf_warning_or_error
, tmpl
,
25691 /*integral_constant_expression_p=*/false);
25693 /* Set the current input_location to the end of the function
25694 so that finish_function knows where we are. */
25696 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25698 /* Remember if we saw an infinite loop in the template. */
25699 current_function_infinite_loop
25700 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25703 /* Finish the function. */
25704 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25705 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25706 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25709 d
= finish_function (/*inline_p=*/false);
25710 expand_or_defer_fn (d
);
25713 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25714 cp_check_omp_declare_reduction (d
);
25717 /* We're not deferring instantiation any more. */
25718 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25721 pop_from_top_level ();
25723 pop_function_context ();
25726 restore_omp_privatization_clauses (omp_privatization_save
);
25729 pop_deferring_access_checks ();
25730 timevar_pop (TV_TEMPLATE_INST
);
25731 pop_tinst_level ();
25732 input_location
= saved_loc
;
25733 cp_unevaluated_operand
= saved_unevaluated_operand
;
25734 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25739 /* Run through the list of templates that we wish we could
25740 instantiate, and instantiate any we can. RETRIES is the
25741 number of times we retry pending template instantiation. */
25744 instantiate_pending_templates (int retries
)
25747 location_t saved_loc
= input_location
;
25749 /* Instantiating templates may trigger vtable generation. This in turn
25750 may require further template instantiations. We place a limit here
25751 to avoid infinite loop. */
25752 if (pending_templates
&& retries
>= max_tinst_depth
)
25754 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25756 fatal_error (input_location
,
25757 "template instantiation depth exceeds maximum of %d"
25758 " instantiating %q+D, possibly from virtual table generation"
25759 " (use %<-ftemplate-depth=%> to increase the maximum)",
25760 max_tinst_depth
, decl
);
25761 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25762 /* Pretend that we defined it. */
25763 DECL_INITIAL (decl
) = error_mark_node
;
25769 struct pending_template
**t
= &pending_templates
;
25770 struct pending_template
*last
= NULL
;
25774 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25775 bool complete
= false;
25777 if (TYPE_P (instantiation
))
25779 if (!COMPLETE_TYPE_P (instantiation
))
25781 instantiate_class_template (instantiation
);
25782 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25783 for (tree fld
= TYPE_FIELDS (instantiation
);
25784 fld
; fld
= TREE_CHAIN (fld
))
25786 || (TREE_CODE (fld
) == FUNCTION_DECL
25787 && !DECL_ARTIFICIAL (fld
)))
25788 && DECL_TEMPLATE_INSTANTIATION (fld
))
25789 instantiate_decl (fld
,
25790 /*defer_ok=*/false,
25791 /*expl_inst_class_mem_p=*/false);
25793 if (COMPLETE_TYPE_P (instantiation
))
25797 complete
= COMPLETE_TYPE_P (instantiation
);
25801 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25802 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25805 = instantiate_decl (instantiation
,
25806 /*defer_ok=*/false,
25807 /*expl_inst_class_mem_p=*/false);
25808 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25812 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25813 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25818 /* If INSTANTIATION has been instantiated, then we don't
25819 need to consider it again in the future. */
25820 struct pending_template
*drop
= *t
;
25822 set_refcount_ptr (drop
->tinst
);
25823 pending_template_freelist ().free (drop
);
25831 set_refcount_ptr (current_tinst_level
);
25833 last_pending_template
= last
;
25835 while (reconsider
);
25837 input_location
= saved_loc
;
25840 /* Substitute ARGVEC into T, which is a list of initializers for
25841 either base class or a non-static data member. The TREE_PURPOSEs
25842 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25843 instantiate_decl. */
25846 tsubst_initializer_list (tree t
, tree argvec
)
25848 tree inits
= NULL_TREE
;
25849 tree target_ctor
= error_mark_node
;
25851 for (; t
; t
= TREE_CHAIN (t
))
25855 tree expanded_bases
= NULL_TREE
;
25856 tree expanded_arguments
= NULL_TREE
;
25859 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25864 /* Expand the base class expansion type into separate base
25866 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25867 tf_warning_or_error
,
25869 if (expanded_bases
== error_mark_node
)
25872 /* We'll be building separate TREE_LISTs of arguments for
25874 len
= TREE_VEC_LENGTH (expanded_bases
);
25875 expanded_arguments
= make_tree_vec (len
);
25876 for (i
= 0; i
< len
; i
++)
25877 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25879 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25880 expand each argument in the TREE_VALUE of t. */
25881 expr
= make_node (EXPR_PACK_EXPANSION
);
25882 PACK_EXPANSION_LOCAL_P (expr
) = true;
25883 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25884 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25886 if (TREE_VALUE (t
) == void_type_node
)
25887 /* VOID_TYPE_NODE is used to indicate
25888 value-initialization. */
25890 for (i
= 0; i
< len
; i
++)
25891 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25895 /* Substitute parameter packs into each argument in the
25897 in_base_initializer
= 1;
25898 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25900 tree expanded_exprs
;
25902 /* Expand the argument. */
25903 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25905 = tsubst_pack_expansion (expr
, argvec
,
25906 tf_warning_or_error
,
25908 if (expanded_exprs
== error_mark_node
)
25911 /* Prepend each of the expanded expressions to the
25912 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25913 for (i
= 0; i
< len
; i
++)
25915 TREE_VEC_ELT (expanded_arguments
, i
) =
25916 tree_cons (NULL_TREE
,
25917 TREE_VEC_ELT (expanded_exprs
, i
),
25918 TREE_VEC_ELT (expanded_arguments
, i
));
25921 in_base_initializer
= 0;
25923 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25924 since we built them backwards. */
25925 for (i
= 0; i
< len
; i
++)
25927 TREE_VEC_ELT (expanded_arguments
, i
) =
25928 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25933 for (i
= 0; i
< len
; ++i
)
25935 if (expanded_bases
)
25937 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25938 decl
= expand_member_init (decl
);
25939 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25944 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25945 tf_warning_or_error
, NULL_TREE
);
25947 decl
= expand_member_init (decl
);
25948 if (decl
&& !DECL_P (decl
))
25949 in_base_initializer
= 1;
25951 init
= TREE_VALUE (t
);
25953 if (init
!= void_type_node
)
25954 init
= tsubst_expr (init
, argvec
,
25955 tf_warning_or_error
, NULL_TREE
,
25956 /*integral_constant_expression_p=*/false);
25957 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25958 /* If we had an initializer but it instantiated to nothing,
25959 value-initialize the object. This will only occur when
25960 the initializer was a pack expansion where the parameter
25961 packs used in that expansion were of length zero. */
25962 init
= void_type_node
;
25963 in_base_initializer
= 0;
25966 if (target_ctor
!= error_mark_node
25967 && init
!= error_mark_node
)
25969 error ("mem-initializer for %qD follows constructor delegation",
25973 /* Look for a target constructor. */
25974 if (init
!= error_mark_node
25975 && decl
&& CLASS_TYPE_P (decl
)
25976 && same_type_p (decl
, current_class_type
))
25978 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25981 error ("constructor delegation follows mem-initializer for %qD",
25982 TREE_PURPOSE (inits
));
25985 target_ctor
= init
;
25990 init
= build_tree_list (decl
, init
);
25991 TREE_CHAIN (init
) = inits
;
25999 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26002 set_current_access_from_decl (tree decl
)
26004 if (TREE_PRIVATE (decl
))
26005 current_access_specifier
= access_private_node
;
26006 else if (TREE_PROTECTED (decl
))
26007 current_access_specifier
= access_protected_node
;
26009 current_access_specifier
= access_public_node
;
26012 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26013 is the instantiation (which should have been created with
26014 start_enum) and ARGS are the template arguments to use. */
26017 tsubst_enum (tree tag
, tree newtag
, tree args
)
26021 if (SCOPED_ENUM_P (newtag
))
26022 begin_scope (sk_scoped_enum
, newtag
);
26024 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26029 decl
= TREE_VALUE (e
);
26030 /* Note that in a template enum, the TREE_VALUE is the
26031 CONST_DECL, not the corresponding INTEGER_CST. */
26032 value
= tsubst_expr (DECL_INITIAL (decl
),
26033 args
, tf_warning_or_error
, NULL_TREE
,
26034 /*integral_constant_expression_p=*/true);
26036 /* Give this enumeration constant the correct access. */
26037 set_current_access_from_decl (decl
);
26039 /* Actually build the enumerator itself. Here we're assuming that
26040 enumerators can't have dependent attributes. */
26041 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26042 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26045 if (SCOPED_ENUM_P (newtag
))
26048 finish_enum_value_list (newtag
);
26049 finish_enum (newtag
);
26051 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26052 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26055 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26056 its type -- but without substituting the innermost set of template
26057 arguments. So, innermost set of template parameters will appear in
26061 get_mostly_instantiated_function_type (tree decl
)
26063 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26064 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26067 /* Return truthvalue if we're processing a template different from
26068 the last one involved in diagnostics. */
26070 problematic_instantiation_changed (void)
26072 return current_tinst_level
!= last_error_tinst_level
;
26075 /* Remember current template involved in diagnostics. */
26077 record_last_problematic_instantiation (void)
26079 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26082 struct tinst_level
*
26083 current_instantiation (void)
26085 return current_tinst_level
;
26088 /* Return TRUE if current_function_decl is being instantiated, false
26092 instantiating_current_function_p (void)
26094 return (current_instantiation ()
26095 && (current_instantiation ()->maybe_get_node ()
26096 == current_function_decl
));
26099 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26100 type. Return false for ok, true for disallowed. Issue error and
26101 inform messages under control of COMPLAIN. */
26104 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26106 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26108 else if (TYPE_PTR_P (type
))
26110 else if (TYPE_REF_P (type
)
26111 && !TYPE_REF_IS_RVALUE (type
))
26113 else if (TYPE_PTRMEM_P (type
))
26115 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26117 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx2a
)
26119 if (complain
& tf_error
)
26120 error ("non-type template parameters of deduced class type only "
26121 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
26126 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26128 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26130 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26132 /* A bound template template parm could later be instantiated to have a valid
26133 nontype parm type via an alias template. */
26134 else if (cxx_dialect
>= cxx11
26135 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26137 else if (CLASS_TYPE_P (type
))
26139 if (cxx_dialect
< cxx2a
)
26141 if (complain
& tf_error
)
26142 error ("non-type template parameters of class type only available "
26143 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
26146 if (dependent_type_p (type
))
26148 if (!complete_type_or_else (type
, NULL_TREE
))
26150 if (!structural_type_p (type
))
26152 if (complain
& tf_error
)
26154 auto_diagnostic_group d
;
26155 error ("%qT is not a valid type for a template non-type "
26156 "parameter because it is not structural", type
);
26157 structural_type_p (type
, true);
26164 if (complain
& tf_error
)
26166 if (type
== error_mark_node
)
26167 inform (input_location
, "invalid template non-type parameter");
26169 error ("%q#T is not a valid type for a template non-type parameter",
26175 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26176 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26179 dependent_type_p_r (tree type
)
26185 A type is dependent if it is:
26187 -- a template parameter. Template template parameters are types
26188 for us (since TYPE_P holds true for them) so we handle
26190 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26191 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26193 /* -- a qualified-id with a nested-name-specifier which contains a
26194 class-name that names a dependent type or whose unqualified-id
26195 names a dependent type. */
26196 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26199 /* An alias template specialization can be dependent even if the
26200 resulting type is not. */
26201 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26204 /* -- a cv-qualified type where the cv-unqualified type is
26206 No code is necessary for this bullet; the code below handles
26207 cv-qualified types, and we don't want to strip aliases with
26208 TYPE_MAIN_VARIANT because of DR 1558. */
26209 /* -- a compound type constructed from any dependent type. */
26210 if (TYPE_PTRMEM_P (type
))
26211 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26212 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26214 else if (INDIRECT_TYPE_P (type
))
26215 return dependent_type_p (TREE_TYPE (type
));
26216 else if (FUNC_OR_METHOD_TYPE_P (type
))
26220 if (dependent_type_p (TREE_TYPE (type
)))
26222 for (arg_type
= TYPE_ARG_TYPES (type
);
26224 arg_type
= TREE_CHAIN (arg_type
))
26225 if (dependent_type_p (TREE_VALUE (arg_type
)))
26227 if (cxx_dialect
>= cxx17
)
26228 /* A value-dependent noexcept-specifier makes the type dependent. */
26229 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26230 if (tree noex
= TREE_PURPOSE (spec
))
26231 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26232 affect overload resolution and treating it as dependent breaks
26233 things. Same for an unparsed noexcept expression. */
26234 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26235 && TREE_CODE (noex
) != DEFERRED_PARSE
26236 && value_dependent_expression_p (noex
))
26240 /* -- an array type constructed from any dependent type or whose
26241 size is specified by a constant expression that is
26244 We checked for type- and value-dependence of the bounds in
26245 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26246 if (TREE_CODE (type
) == ARRAY_TYPE
)
26248 if (TYPE_DOMAIN (type
)
26249 && dependent_type_p (TYPE_DOMAIN (type
)))
26251 return dependent_type_p (TREE_TYPE (type
));
26254 /* -- a template-id in which either the template name is a template
26256 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26258 /* ... or any of the template arguments is a dependent type or
26259 an expression that is type-dependent or value-dependent. */
26260 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26261 && (any_dependent_template_arguments_p
26262 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26265 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26266 dependent; if the argument of the `typeof' expression is not
26267 type-dependent, then it should already been have resolved. */
26268 if (TREE_CODE (type
) == TYPEOF_TYPE
26269 || TREE_CODE (type
) == DECLTYPE_TYPE
26270 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26273 /* A template argument pack is dependent if any of its packed
26275 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26277 tree args
= ARGUMENT_PACK_ARGS (type
);
26278 int i
, len
= TREE_VEC_LENGTH (args
);
26279 for (i
= 0; i
< len
; ++i
)
26280 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26284 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26285 be template parameters. */
26286 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26289 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26292 /* The standard does not specifically mention types that are local
26293 to template functions or local classes, but they should be
26294 considered dependent too. For example:
26296 template <int I> void f() {
26301 The size of `E' cannot be known until the value of `I' has been
26302 determined. Therefore, `E' must be considered dependent. */
26303 scope
= TYPE_CONTEXT (type
);
26304 if (scope
&& TYPE_P (scope
))
26305 return dependent_type_p (scope
);
26306 /* Don't use type_dependent_expression_p here, as it can lead
26307 to infinite recursion trying to determine whether a lambda
26308 nested in a lambda is dependent (c++/47687). */
26309 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26310 && DECL_LANG_SPECIFIC (scope
)
26311 && DECL_TEMPLATE_INFO (scope
)
26312 && (any_dependent_template_arguments_p
26313 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26316 /* Other types are non-dependent. */
26320 /* Returns TRUE if TYPE is dependent, in the sense of
26321 [temp.dep.type]. Note that a NULL type is considered dependent. */
26324 dependent_type_p (tree type
)
26326 /* If there are no template parameters in scope, then there can't be
26327 any dependent types. */
26328 if (!processing_template_decl
)
26330 /* If we are not processing a template, then nobody should be
26331 providing us with a dependent type. */
26333 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26337 /* If the type is NULL, we have not computed a type for the entity
26338 in question; in that case, the type is dependent. */
26342 /* Erroneous types can be considered non-dependent. */
26343 if (type
== error_mark_node
)
26346 /* Getting here with global_type_node means we improperly called this
26347 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26348 gcc_checking_assert (type
!= global_type_node
);
26350 /* If we have not already computed the appropriate value for TYPE,
26352 if (!TYPE_DEPENDENT_P_VALID (type
))
26354 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26355 TYPE_DEPENDENT_P_VALID (type
) = 1;
26358 return TYPE_DEPENDENT_P (type
);
26361 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26362 lookup. In other words, a dependent type that is not the current
26366 dependent_scope_p (tree scope
)
26368 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26369 && !currently_open_class (scope
));
26372 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26373 an unknown base of 'this' (and is therefore instantiation-dependent). */
26376 unknown_base_ref_p (tree t
)
26378 if (!current_class_ptr
)
26381 tree mem
= TREE_OPERAND (t
, 1);
26382 if (shared_member_p (mem
))
26385 tree cur
= current_nonlambda_class_type ();
26386 if (!any_dependent_bases_p (cur
))
26389 tree ctx
= TREE_OPERAND (t
, 0);
26390 if (DERIVED_FROM_P (ctx
, cur
))
26396 /* T is a SCOPE_REF; return whether we need to consider it
26397 instantiation-dependent so that we can check access at instantiation
26398 time even though we know which member it resolves to. */
26401 instantiation_dependent_scope_ref_p (tree t
)
26403 if (DECL_P (TREE_OPERAND (t
, 1))
26404 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26405 && !unknown_base_ref_p (t
)
26406 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26407 TREE_OPERAND (t
, 1)))
26413 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26414 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26417 /* Note that this predicate is not appropriate for general expressions;
26418 only constant expressions (that satisfy potential_constant_expression)
26419 can be tested for value dependence. */
26422 value_dependent_expression_p (tree expression
)
26424 if (!processing_template_decl
|| expression
== NULL_TREE
)
26427 /* A type-dependent expression is also value-dependent. */
26428 if (type_dependent_expression_p (expression
))
26431 switch (TREE_CODE (expression
))
26434 /* A dependent member function of the current instantiation. */
26435 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26437 case FUNCTION_DECL
:
26438 /* A dependent member function of the current instantiation. */
26439 if (DECL_CLASS_SCOPE_P (expression
)
26440 && dependent_type_p (DECL_CONTEXT (expression
)))
26444 case IDENTIFIER_NODE
:
26445 /* A name that has not been looked up -- must be dependent. */
26448 case TEMPLATE_PARM_INDEX
:
26449 /* A non-type template parm. */
26453 /* A non-type template parm. */
26454 if (DECL_TEMPLATE_PARM_P (expression
))
26456 return value_dependent_expression_p (DECL_INITIAL (expression
));
26459 /* A constant with literal type and is initialized
26460 with an expression that is value-dependent. */
26461 if (DECL_DEPENDENT_INIT_P (expression
)
26462 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26463 || TYPE_REF_P (TREE_TYPE (expression
)))
26465 if (DECL_HAS_VALUE_EXPR_P (expression
))
26467 tree value_expr
= DECL_VALUE_EXPR (expression
);
26468 if (value_dependent_expression_p (value_expr
)
26469 /* __PRETTY_FUNCTION__ inside a template function is dependent
26470 on the name of the function. */
26471 || (DECL_PRETTY_FUNCTION_P (expression
)
26472 /* It might be used in a template, but not a template
26473 function, in which case its DECL_VALUE_EXPR will be
26475 && value_expr
== error_mark_node
))
26480 case DYNAMIC_CAST_EXPR
:
26481 case STATIC_CAST_EXPR
:
26482 case CONST_CAST_EXPR
:
26483 case REINTERPRET_CAST_EXPR
:
26485 case IMPLICIT_CONV_EXPR
:
26486 /* These expressions are value-dependent if the type to which
26487 the cast occurs is dependent or the expression being casted
26488 is value-dependent. */
26490 tree type
= TREE_TYPE (expression
);
26492 if (dependent_type_p (type
))
26495 /* A functional cast has a list of operands. */
26496 expression
= TREE_OPERAND (expression
, 0);
26499 /* If there are no operands, it must be an expression such
26500 as "int()". This should not happen for aggregate types
26501 because it would form non-constant expressions. */
26502 gcc_assert (cxx_dialect
>= cxx11
26503 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26508 if (TREE_CODE (expression
) == TREE_LIST
)
26509 return any_value_dependent_elements_p (expression
);
26511 return value_dependent_expression_p (expression
);
26515 if (SIZEOF_EXPR_TYPE_P (expression
))
26516 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26520 /* A `sizeof' expression is value-dependent if the operand is
26521 type-dependent or is a pack expansion. */
26522 expression
= TREE_OPERAND (expression
, 0);
26523 if (PACK_EXPANSION_P (expression
))
26525 else if (TYPE_P (expression
))
26526 return dependent_type_p (expression
);
26527 return instantiation_dependent_uneval_expression_p (expression
);
26529 case AT_ENCODE_EXPR
:
26530 /* An 'encode' expression is value-dependent if the operand is
26532 expression
= TREE_OPERAND (expression
, 0);
26533 return dependent_type_p (expression
);
26535 case NOEXCEPT_EXPR
:
26536 expression
= TREE_OPERAND (expression
, 0);
26537 return instantiation_dependent_uneval_expression_p (expression
);
26540 /* All instantiation-dependent expressions should also be considered
26541 value-dependent. */
26542 return instantiation_dependent_scope_ref_p (expression
);
26544 case COMPONENT_REF
:
26545 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26546 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26548 case NONTYPE_ARGUMENT_PACK
:
26549 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26550 is value-dependent. */
26552 tree values
= ARGUMENT_PACK_ARGS (expression
);
26553 int i
, len
= TREE_VEC_LENGTH (values
);
26555 for (i
= 0; i
< len
; ++i
)
26556 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26564 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26566 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26572 if (TREE_CODE (type2
) != TREE_LIST
)
26573 return dependent_type_p (type2
);
26575 for (; type2
; type2
= TREE_CHAIN (type2
))
26576 if (dependent_type_p (TREE_VALUE (type2
)))
26583 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26584 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26587 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26588 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26592 tree op
= TREE_OPERAND (expression
, 0);
26593 return (value_dependent_expression_p (op
)
26594 || has_value_dependent_address (op
));
26597 case REQUIRES_EXPR
:
26598 /* Treat all requires-expressions as value-dependent so
26599 we don't try to fold them. */
26603 return dependent_type_p (TREE_OPERAND (expression
, 0));
26607 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26609 tree fn
= get_callee_fndecl (expression
);
26611 nargs
= call_expr_nargs (expression
);
26612 for (i
= 0; i
< nargs
; ++i
)
26614 tree op
= CALL_EXPR_ARG (expression
, i
);
26615 /* In a call to a constexpr member function, look through the
26616 implicit ADDR_EXPR on the object argument so that it doesn't
26617 cause the call to be considered value-dependent. We also
26618 look through it in potential_constant_expression. */
26619 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26620 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26621 && TREE_CODE (op
) == ADDR_EXPR
)
26622 op
= TREE_OPERAND (op
, 0);
26623 if (value_dependent_expression_p (op
))
26629 case TEMPLATE_ID_EXPR
:
26630 return concept_definition_p (TREE_OPERAND (expression
, 0));
26636 if (dependent_type_p (TREE_TYPE (expression
)))
26638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26639 if (value_dependent_expression_p (val
))
26645 /* Treat a GNU statement expression as dependent to avoid crashing
26646 under instantiate_non_dependent_expr; it can't be constant. */
26650 /* A constant expression is value-dependent if any subexpression is
26651 value-dependent. */
26652 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26654 case tcc_reference
:
26656 case tcc_comparison
:
26658 case tcc_expression
:
26661 int i
, len
= cp_tree_operand_length (expression
);
26663 for (i
= 0; i
< len
; i
++)
26665 tree t
= TREE_OPERAND (expression
, i
);
26667 /* In some cases, some of the operands may be missing.
26668 (For example, in the case of PREDECREMENT_EXPR, the
26669 amount to increment by may be missing.) That doesn't
26670 make the expression dependent. */
26671 if (t
&& value_dependent_expression_p (t
))
26682 /* The expression is not value-dependent. */
26686 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26687 [temp.dep.expr]. Note that an expression with no type is
26688 considered dependent. Other parts of the compiler arrange for an
26689 expression with type-dependent subexpressions to have no type, so
26690 this function doesn't have to be fully recursive. */
26693 type_dependent_expression_p (tree expression
)
26695 if (!processing_template_decl
)
26698 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26701 STRIP_ANY_LOCATION_WRAPPER (expression
);
26703 /* An unresolved name is always dependent. */
26704 if (identifier_p (expression
)
26705 || TREE_CODE (expression
) == USING_DECL
26706 || TREE_CODE (expression
) == WILDCARD_DECL
)
26709 /* A lambda-expression in template context is dependent. dependent_type_p is
26710 true for a lambda in the scope of a class or function template, but that
26711 doesn't cover all template contexts, like a default template argument. */
26712 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26715 /* A fold expression is type-dependent. */
26716 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26717 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26718 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26719 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26722 /* Some expression forms are never type-dependent. */
26723 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
26724 || TREE_CODE (expression
) == SIZEOF_EXPR
26725 || TREE_CODE (expression
) == ALIGNOF_EXPR
26726 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26727 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26728 || TREE_CODE (expression
) == TRAIT_EXPR
26729 || TREE_CODE (expression
) == TYPEID_EXPR
26730 || TREE_CODE (expression
) == DELETE_EXPR
26731 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26732 || TREE_CODE (expression
) == THROW_EXPR
26733 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26736 /* The types of these expressions depends only on the type to which
26737 the cast occurs. */
26738 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26739 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26740 || TREE_CODE (expression
) == CONST_CAST_EXPR
26741 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26742 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26743 || TREE_CODE (expression
) == CAST_EXPR
)
26744 return dependent_type_p (TREE_TYPE (expression
));
26746 /* The types of these expressions depends only on the type created
26747 by the expression. */
26748 if (TREE_CODE (expression
) == NEW_EXPR
26749 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26751 /* For NEW_EXPR tree nodes created inside a template, either
26752 the object type itself or a TREE_LIST may appear as the
26754 tree type
= TREE_OPERAND (expression
, 1);
26755 if (TREE_CODE (type
) == TREE_LIST
)
26756 /* This is an array type. We need to check array dimensions
26758 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26759 || value_dependent_expression_p
26760 (TREE_OPERAND (TREE_VALUE (type
), 1));
26762 return dependent_type_p (type
);
26765 if (TREE_CODE (expression
) == SCOPE_REF
)
26767 tree scope
= TREE_OPERAND (expression
, 0);
26768 tree name
= TREE_OPERAND (expression
, 1);
26770 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26771 contains an identifier associated by name lookup with one or more
26772 declarations declared with a dependent type, or...a
26773 nested-name-specifier or qualified-id that names a member of an
26774 unknown specialization. */
26775 return (type_dependent_expression_p (name
)
26776 || dependent_scope_p (scope
));
26779 if (TREE_CODE (expression
) == TEMPLATE_DECL
26780 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26781 return uses_outer_template_parms (expression
);
26783 if (TREE_CODE (expression
) == STMT_EXPR
)
26784 expression
= stmt_expr_value_expr (expression
);
26786 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26791 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26793 if (type_dependent_expression_p (elt
))
26799 /* A static data member of the current instantiation with incomplete
26800 array type is type-dependent, as the definition and specializations
26801 can have different bounds. */
26802 if (VAR_P (expression
)
26803 && DECL_CLASS_SCOPE_P (expression
)
26804 && dependent_type_p (DECL_CONTEXT (expression
))
26805 && VAR_HAD_UNKNOWN_BOUND (expression
))
26808 /* An array of unknown bound depending on a variadic parameter, eg:
26810 template<typename... Args>
26811 void foo (Args... args)
26813 int arr[] = { args... };
26816 template<int... vals>
26819 int arr[] = { vals... };
26822 If the array has no length and has an initializer, it must be that
26823 we couldn't determine its length in cp_complete_array_type because
26824 it is dependent. */
26825 if (VAR_P (expression
)
26826 && TREE_TYPE (expression
) != NULL_TREE
26827 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26828 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26829 && DECL_INITIAL (expression
))
26832 /* A function or variable template-id is type-dependent if it has any
26833 dependent template arguments. */
26834 if (VAR_OR_FUNCTION_DECL_P (expression
)
26835 && DECL_LANG_SPECIFIC (expression
)
26836 && DECL_TEMPLATE_INFO (expression
))
26838 /* Consider the innermost template arguments, since those are the ones
26839 that come from the template-id; the template arguments for the
26840 enclosing class do not make it type-dependent unless they are used in
26841 the type of the decl. */
26842 if (instantiates_primary_template_p (expression
)
26843 && (any_dependent_template_arguments_p
26844 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26848 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26849 type-dependent. Checking this is important for functions with auto return
26850 type, which looks like a dependent type. */
26851 if (TREE_CODE (expression
) == FUNCTION_DECL
26852 && !(DECL_CLASS_SCOPE_P (expression
)
26853 && dependent_type_p (DECL_CONTEXT (expression
)))
26854 && !(DECL_LANG_SPECIFIC (expression
)
26855 && DECL_FRIEND_P (expression
)
26856 && (!DECL_FRIEND_CONTEXT (expression
)
26857 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26858 && !DECL_LOCAL_FUNCTION_P (expression
))
26860 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26861 || undeduced_auto_decl (expression
));
26865 /* Always dependent, on the number of arguments if nothing else. */
26866 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26869 if (TREE_TYPE (expression
) == unknown_type_node
)
26871 if (TREE_CODE (expression
) == ADDR_EXPR
)
26872 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26873 if (TREE_CODE (expression
) == COMPONENT_REF
26874 || TREE_CODE (expression
) == OFFSET_REF
)
26876 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26878 expression
= TREE_OPERAND (expression
, 1);
26879 if (identifier_p (expression
))
26882 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26883 if (TREE_CODE (expression
) == SCOPE_REF
)
26886 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26887 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
26888 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
26891 if (BASELINK_P (expression
))
26893 if (BASELINK_OPTYPE (expression
)
26894 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26896 expression
= BASELINK_FUNCTIONS (expression
);
26899 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26901 if (any_dependent_template_arguments_p
26902 (TREE_OPERAND (expression
, 1)))
26904 expression
= TREE_OPERAND (expression
, 0);
26905 if (identifier_p (expression
))
26909 gcc_assert (OVL_P (expression
));
26911 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26912 if (type_dependent_expression_p (*iter
))
26918 /* The type of a non-type template parm declared with a placeholder type
26919 depends on the corresponding template argument, even though
26920 placeholders are not normally considered dependent. */
26921 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26922 && is_auto (TREE_TYPE (expression
)))
26925 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26927 /* Dependent type attributes might not have made it from the decl to
26929 if (DECL_P (expression
)
26930 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26933 return (dependent_type_p (TREE_TYPE (expression
)));
26936 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26937 type-dependent if the expression refers to a member of the current
26938 instantiation and the type of the referenced member is dependent, or the
26939 class member access expression refers to a member of an unknown
26942 This function returns true if the OBJECT in such a class member access
26943 expression is of an unknown specialization. */
26946 type_dependent_object_expression_p (tree object
)
26948 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26950 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26952 tree scope
= TREE_TYPE (object
);
26953 return (!scope
|| dependent_scope_p (scope
));
26956 /* walk_tree callback function for instantiation_dependent_expression_p,
26957 below. Returns non-zero if a dependent subexpression is found. */
26960 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26965 /* We don't have to worry about decltype currently because decltype
26966 of an instantiation-dependent expr is a dependent type. This
26967 might change depending on the resolution of DR 1172. */
26968 *walk_subtrees
= false;
26971 enum tree_code code
= TREE_CODE (*tp
);
26974 /* Don't treat an argument list as dependent just because it has no
26978 case NONTYPE_ARGUMENT_PACK
:
26981 case TEMPLATE_PARM_INDEX
:
26982 if (dependent_type_p (TREE_TYPE (*tp
)))
26984 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26986 /* We'll check value-dependence separately. */
26989 /* Handle expressions with type operands. */
26993 case AT_ENCODE_EXPR
:
26995 tree op
= TREE_OPERAND (*tp
, 0);
26996 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26997 op
= TREE_TYPE (op
);
27000 if (dependent_type_p (op
))
27004 *walk_subtrees
= false;
27011 case COMPONENT_REF
:
27012 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27013 /* In a template, finish_class_member_access_expr creates a
27014 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27015 type-dependent, so that we can check access control at
27016 instantiation time (PR 42277). See also Core issue 1273. */
27021 if (instantiation_dependent_scope_ref_p (*tp
))
27026 /* Treat statement-expressions as dependent. */
27030 /* Treat requires-expressions as dependent. */
27031 case REQUIRES_EXPR
:
27035 /* Treat concept checks as dependent. */
27036 if (concept_check_p (*tp
))
27040 case TEMPLATE_ID_EXPR
:
27041 /* Treat concept checks as dependent. */
27042 if (concept_check_p (*tp
))
27047 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27055 if (type_dependent_expression_p (*tp
))
27061 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27062 sense defined by the ABI:
27064 "An expression is instantiation-dependent if it is type-dependent
27065 or value-dependent, or it has a subexpression that is type-dependent
27066 or value-dependent."
27068 Except don't actually check value-dependence for unevaluated expressions,
27069 because in sizeof(i) we don't care about the value of i. Checking
27070 type-dependence will in turn check value-dependence of array bounds/template
27071 arguments as needed. */
27074 instantiation_dependent_uneval_expression_p (tree expression
)
27078 if (!processing_template_decl
)
27081 if (expression
== error_mark_node
)
27084 result
= cp_walk_tree_without_duplicates (&expression
,
27085 instantiation_dependent_r
, NULL
);
27086 return result
!= NULL_TREE
;
27089 /* As above, but also check value-dependence of the expression as a whole. */
27092 instantiation_dependent_expression_p (tree expression
)
27094 return (instantiation_dependent_uneval_expression_p (expression
)
27095 || value_dependent_expression_p (expression
));
27098 /* Like type_dependent_expression_p, but it also works while not processing
27099 a template definition, i.e. during substitution or mangling. */
27102 type_dependent_expression_p_push (tree expr
)
27105 ++processing_template_decl
;
27106 b
= type_dependent_expression_p (expr
);
27107 --processing_template_decl
;
27111 /* Returns TRUE if ARGS contains a type-dependent expression. */
27114 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27119 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27121 if (type_dependent_expression_p (arg
))
27127 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27128 expressions) contains any type-dependent expressions. */
27131 any_type_dependent_elements_p (const_tree list
)
27133 for (; list
; list
= TREE_CHAIN (list
))
27134 if (type_dependent_expression_p (TREE_VALUE (list
)))
27140 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27141 expressions) contains any value-dependent expressions. */
27144 any_value_dependent_elements_p (const_tree list
)
27146 for (; list
; list
= TREE_CHAIN (list
))
27147 if (value_dependent_expression_p (TREE_VALUE (list
)))
27153 /* Returns TRUE if the ARG (a template argument) is dependent. */
27156 dependent_template_arg_p (tree arg
)
27158 if (!processing_template_decl
)
27161 /* Assume a template argument that was wrongly written by the user
27162 is dependent. This is consistent with what
27163 any_dependent_template_arguments_p [that calls this function]
27165 if (!arg
|| arg
== error_mark_node
)
27168 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27169 arg
= argument_pack_select_arg (arg
);
27171 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27173 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27175 if (DECL_TEMPLATE_PARM_P (arg
))
27177 /* A member template of a dependent class is not necessarily
27178 type-dependent, but it is a dependent template argument because it
27179 will be a member of an unknown specialization to that template. */
27180 tree scope
= CP_DECL_CONTEXT (arg
);
27181 return TYPE_P (scope
) && dependent_type_p (scope
);
27183 else if (ARGUMENT_PACK_P (arg
))
27185 tree args
= ARGUMENT_PACK_ARGS (arg
);
27186 int i
, len
= TREE_VEC_LENGTH (args
);
27187 for (i
= 0; i
< len
; ++i
)
27189 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27195 else if (TYPE_P (arg
))
27196 return dependent_type_p (arg
);
27198 return value_dependent_expression_p (arg
);
27201 /* Returns true if ARGS (a collection of template arguments) contains
27202 any types that require structural equality testing. */
27205 any_template_arguments_need_structural_equality_p (tree args
)
27212 if (args
== error_mark_node
)
27215 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27217 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27218 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27220 tree arg
= TREE_VEC_ELT (level
, j
);
27221 tree packed_args
= NULL_TREE
;
27224 if (ARGUMENT_PACK_P (arg
))
27226 /* Look inside the argument pack. */
27227 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27228 len
= TREE_VEC_LENGTH (packed_args
);
27231 for (k
= 0; k
< len
; ++k
)
27234 arg
= TREE_VEC_ELT (packed_args
, k
);
27236 if (error_operand_p (arg
))
27238 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27240 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27242 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27243 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27252 /* Returns true if ARGS (a collection of template arguments) contains
27253 any dependent arguments. */
27256 any_dependent_template_arguments_p (const_tree args
)
27263 if (args
== error_mark_node
)
27266 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27268 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27269 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27270 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27277 /* Returns true if ARGS contains any errors. */
27280 any_erroneous_template_args_p (const_tree args
)
27285 if (args
== error_mark_node
)
27288 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27290 if (tree ti
= get_template_info (args
))
27291 args
= TI_ARGS (ti
);
27299 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27301 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27302 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27303 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27310 /* Returns TRUE if the template TMPL is type-dependent. */
27313 dependent_template_p (tree tmpl
)
27315 if (TREE_CODE (tmpl
) == OVERLOAD
)
27317 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27318 if (dependent_template_p (*iter
))
27323 /* Template template parameters are dependent. */
27324 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27325 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27327 /* So are names that have not been looked up. */
27328 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27333 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27336 dependent_template_id_p (tree tmpl
, tree args
)
27338 return (dependent_template_p (tmpl
)
27339 || any_dependent_template_arguments_p (args
));
27342 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27346 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27350 if (!processing_template_decl
)
27353 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27355 tree decl
= TREE_VEC_ELT (declv
, i
);
27356 tree init
= TREE_VEC_ELT (initv
, i
);
27357 tree cond
= TREE_VEC_ELT (condv
, i
);
27358 tree incr
= TREE_VEC_ELT (incrv
, i
);
27360 if (type_dependent_expression_p (decl
)
27361 || TREE_CODE (decl
) == SCOPE_REF
)
27364 if (init
&& type_dependent_expression_p (init
))
27367 if (cond
== global_namespace
)
27370 if (type_dependent_expression_p (cond
))
27373 if (COMPARISON_CLASS_P (cond
)
27374 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27375 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27378 if (TREE_CODE (incr
) == MODOP_EXPR
)
27380 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27381 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27384 else if (type_dependent_expression_p (incr
))
27386 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27388 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27390 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27392 tree t
= TREE_OPERAND (incr
, 1);
27393 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27394 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27397 /* If this loop has a class iterator with != comparison
27398 with increment other than i++/++i/i--/--i, make sure the
27399 increment is constant. */
27400 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27401 && TREE_CODE (cond
) == NE_EXPR
)
27403 if (TREE_OPERAND (t
, 0) == decl
)
27404 t
= TREE_OPERAND (t
, 1);
27406 t
= TREE_OPERAND (t
, 0);
27407 if (TREE_CODE (t
) != INTEGER_CST
)
27417 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27418 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27419 no such TYPE can be found. Note that this function peers inside
27420 uninstantiated templates and therefore should be used only in
27421 extremely limited situations. ONLY_CURRENT_P restricts this
27422 peering to the currently open classes hierarchy (which is required
27423 when comparing types). */
27426 resolve_typename_type (tree type
, bool only_current_p
)
27435 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27437 scope
= TYPE_CONTEXT (type
);
27438 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27439 gcc_checking_assert (uses_template_parms (scope
));
27441 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27442 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27443 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27444 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27445 identifier of the TYPENAME_TYPE anymore.
27446 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27447 TYPENAME_TYPE instead, we avoid messing up with a possible
27448 typedef variant case. */
27449 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27451 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27452 it first before we can figure out what NAME refers to. */
27453 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27455 if (TYPENAME_IS_RESOLVING_P (scope
))
27456 /* Given a class template A with a dependent base with nested type C,
27457 typedef typename A::C::C C will land us here, as trying to resolve
27458 the initial A::C leads to the local C typedef, which leads back to
27459 A::C::C. So we break the recursion now. */
27462 scope
= resolve_typename_type (scope
, only_current_p
);
27464 /* If we don't know what SCOPE refers to, then we cannot resolve the
27466 if (!CLASS_TYPE_P (scope
))
27468 /* If this is a typedef, we don't want to look inside (c++/11987). */
27469 if (typedef_variant_p (type
))
27471 /* If SCOPE isn't the template itself, it will not have a valid
27472 TYPE_FIELDS list. */
27473 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27474 /* scope is either the template itself or a compatible instantiation
27475 like X<T>, so look up the name in the original template. */
27476 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27477 /* If scope has no fields, it can't be a current instantiation. Check this
27478 before currently_open_class to avoid infinite recursion (71515). */
27479 if (!TYPE_FIELDS (scope
))
27481 /* If the SCOPE is not the current instantiation, there's no reason
27482 to look inside it. */
27483 if (only_current_p
&& !currently_open_class (scope
))
27485 /* Enter the SCOPE so that name lookup will be resolved as if we
27486 were in the class definition. In particular, SCOPE will no
27487 longer be considered a dependent type. */
27488 pushed_scope
= push_scope (scope
);
27489 /* Look up the declaration. */
27490 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27491 tf_warning_or_error
);
27493 result
= NULL_TREE
;
27495 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27496 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27497 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27500 else if (identifier_p (fullname
)
27501 && TREE_CODE (decl
) == TYPE_DECL
)
27503 result
= TREE_TYPE (decl
);
27504 if (result
== error_mark_node
)
27505 result
= NULL_TREE
;
27507 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27508 && DECL_CLASS_TEMPLATE_P (decl
))
27510 /* Obtain the template and the arguments. */
27511 tree tmpl
= TREE_OPERAND (fullname
, 0);
27512 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27514 /* We get here with a plain identifier because a previous tentative
27515 parse of the nested-name-specifier as part of a ptr-operator saw
27516 ::template X<A>. The use of ::template is necessary in a
27517 ptr-operator, but wrong in a declarator-id.
27519 [temp.names]: In a qualified-id of a declarator-id, the keyword
27520 template shall not appear at the top level. */
27521 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27522 "keyword %<template%> not allowed in declarator-id");
27525 tree args
= TREE_OPERAND (fullname
, 1);
27526 /* Instantiate the template. */
27527 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27528 /*entering_scope=*/true,
27529 tf_error
| tf_user
);
27530 if (result
== error_mark_node
)
27531 result
= NULL_TREE
;
27534 /* Leave the SCOPE. */
27536 pop_scope (pushed_scope
);
27538 /* If we failed to resolve it, return the original typename. */
27542 /* If lookup found a typename type, resolve that too. */
27543 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27545 /* Ill-formed programs can cause infinite recursion here, so we
27546 must catch that. */
27547 TYPENAME_IS_RESOLVING_P (result
) = 1;
27548 result
= resolve_typename_type (result
, only_current_p
);
27549 TYPENAME_IS_RESOLVING_P (result
) = 0;
27552 /* Qualify the resulting type. */
27553 quals
= cp_type_quals (type
);
27555 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27560 /* EXPR is an expression which is not type-dependent. Return a proxy
27561 for EXPR that can be used to compute the types of larger
27562 expressions containing EXPR. */
27565 build_non_dependent_expr (tree expr
)
27567 tree orig_expr
= expr
;
27570 /* When checking, try to get a constant value for all non-dependent
27571 expressions in order to expose bugs in *_dependent_expression_p
27572 and constexpr. This can affect code generation, see PR70704, so
27573 only do this for -fchecking=2. */
27574 if (flag_checking
> 1
27575 && cxx_dialect
>= cxx11
27576 /* Don't do this during nsdmi parsing as it can lead to
27577 unexpected recursive instantiations. */
27578 && !parsing_nsdmi ()
27579 /* Don't do this during concept processing either and for
27580 the same reason. */
27581 && !processing_constraint_expression_p ())
27582 fold_non_dependent_expr (expr
, tf_none
);
27584 STRIP_ANY_LOCATION_WRAPPER (expr
);
27586 /* Preserve OVERLOADs; the functions must be available to resolve
27589 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27590 inner_expr
= stmt_expr_value_expr (inner_expr
);
27591 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27592 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27593 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27594 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27595 if (is_overloaded_fn (inner_expr
)
27596 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27598 /* There is no need to return a proxy for a variable or enumerator. */
27599 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27601 /* Preserve string constants; conversions from string constants to
27602 "char *" are allowed, even though normally a "const char *"
27603 cannot be used to initialize a "char *". */
27604 if (TREE_CODE (expr
) == STRING_CST
)
27606 /* Preserve void and arithmetic constants, as an optimization -- there is no
27607 reason to create a new node. */
27608 if (TREE_CODE (expr
) == VOID_CST
27609 || TREE_CODE (expr
) == INTEGER_CST
27610 || TREE_CODE (expr
) == REAL_CST
)
27612 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27613 There is at least one place where we want to know that a
27614 particular expression is a throw-expression: when checking a ?:
27615 expression, there are special rules if the second or third
27616 argument is a throw-expression. */
27617 if (TREE_CODE (expr
) == THROW_EXPR
)
27620 /* Don't wrap an initializer list, we need to be able to look inside. */
27621 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27624 /* Don't wrap a dummy object, we need to be able to test for it. */
27625 if (is_dummy_object (expr
))
27628 if (TREE_CODE (expr
) == COND_EXPR
)
27629 return build3 (COND_EXPR
,
27631 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27632 (TREE_OPERAND (expr
, 1)
27633 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27634 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27635 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27636 if (TREE_CODE (expr
) == COMPOUND_EXPR
27637 && !COMPOUND_EXPR_OVERLOADED (expr
))
27638 return build2 (COMPOUND_EXPR
,
27640 TREE_OPERAND (expr
, 0),
27641 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27643 /* If the type is unknown, it can't really be non-dependent */
27644 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27646 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27647 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27648 TREE_TYPE (expr
), expr
);
27651 /* ARGS is a vector of expressions as arguments to a function call.
27652 Replace the arguments with equivalent non-dependent expressions.
27653 This modifies ARGS in place. */
27656 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27661 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27663 tree newarg
= build_non_dependent_expr (arg
);
27665 (*args
)[ix
] = newarg
;
27669 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27670 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27671 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27674 make_auto_1 (tree name
, bool set_canonical
)
27676 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27677 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27678 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27679 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27680 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27681 TYPE_NAME (au
), NULL_TREE
);
27683 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27684 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27685 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27686 if (name
== decltype_auto_identifier
)
27687 AUTO_IS_DECLTYPE (au
) = true;
27693 make_decltype_auto (void)
27695 return make_auto_1 (decltype_auto_identifier
, true);
27701 return make_auto_1 (auto_identifier
, true);
27704 /* Return a C++17 deduction placeholder for class template TMPL. */
27707 make_template_placeholder (tree tmpl
)
27709 tree t
= make_auto_1 (auto_identifier
, false);
27710 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27711 /* Our canonical type depends on the placeholder. */
27712 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27716 /* True iff T is a C++17 class template deduction placeholder. */
27719 template_placeholder_p (tree t
)
27721 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27724 /* Make a "constrained auto" type-specifier. This is an auto or
27725 decltype(auto) type with constraints that must be associated after
27726 deduction. The constraint is formed from the given concept CON
27727 and its optional sequence of template arguments ARGS.
27729 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27732 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27734 /* Build the constraint. */
27735 tree tmpl
= DECL_TI_TEMPLATE (con
);
27737 if (TREE_CODE (con
) == FUNCTION_DECL
)
27738 expr
= ovl_make (tmpl
);
27739 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27741 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27743 /* Our canonical type depends on the constraint. */
27744 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27746 /* Attach the constraint to the type declaration. */
27747 return TYPE_NAME (type
);
27750 /* Make a "constrained auto" type-specifier. */
27753 make_constrained_auto (tree con
, tree args
)
27755 tree type
= make_auto_1 (auto_identifier
, false);
27756 return make_constrained_placeholder_type (type
, con
, args
);
27759 /* Make a "constrained decltype(auto)" type-specifier. */
27762 make_constrained_decltype_auto (tree con
, tree args
)
27764 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27765 return make_constrained_placeholder_type (type
, con
, args
);
27768 /* Build and return a concept definition. Like other templates, the
27769 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27770 the TEMPLATE_DECL. */
27773 finish_concept_definition (cp_expr id
, tree init
)
27775 gcc_assert (identifier_p (id
));
27776 gcc_assert (processing_template_decl
);
27778 location_t loc
= id
.get_location();
27780 /* A concept-definition shall not have associated constraints. */
27781 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27783 error_at (loc
, "a concept cannot be constrained");
27784 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27787 /* A concept-definition shall appear in namespace scope. Templates
27788 aren't allowed in block scope, so we only need to check for class
27790 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27792 error_at (loc
, "concept %qE not in namespace scope", *id
);
27793 return error_mark_node
;
27796 /* Initially build the concept declaration; it's type is bool. */
27797 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27798 DECL_CONTEXT (decl
) = current_scope ();
27799 DECL_INITIAL (decl
) = init
;
27801 /* Push the enclosing template. */
27802 return push_template_decl (decl
);
27805 /* Given type ARG, return std::initializer_list<ARG>. */
27810 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27812 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27814 gcc_rich_location
richloc (input_location
);
27815 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27816 error_at (&richloc
,
27817 "deducing from brace-enclosed initializer list"
27818 " requires %<#include <initializer_list>%>");
27820 return error_mark_node
;
27822 tree argvec
= make_tree_vec (1);
27823 TREE_VEC_ELT (argvec
, 0) = arg
;
27825 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27826 NULL_TREE
, 0, tf_warning_or_error
);
27829 /* Replace auto in TYPE with std::initializer_list<auto>. */
27832 listify_autos (tree type
, tree auto_node
)
27834 tree init_auto
= listify (strip_top_quals (auto_node
));
27835 tree argvec
= make_tree_vec (1);
27836 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27837 if (processing_template_decl
)
27838 argvec
= add_to_template_args (current_template_args (), argvec
);
27839 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27842 /* Hash traits for hashing possibly constrained 'auto'
27843 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27845 struct auto_hash
: default_hash_traits
<tree
>
27847 static inline hashval_t
hash (tree
);
27848 static inline bool equal (tree
, tree
);
27851 /* Hash the 'auto' T. */
27854 auto_hash::hash (tree t
)
27856 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27857 /* Matching constrained-type-specifiers denote the same template
27858 parameter, so hash the constraint. */
27859 return hash_placeholder_constraint (c
);
27861 /* But unconstrained autos are all separate, so just hash the pointer. */
27862 return iterative_hash_object (t
, 0);
27865 /* Compare two 'auto's. */
27868 auto_hash::equal (tree t1
, tree t2
)
27873 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27874 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27876 /* Two unconstrained autos are distinct. */
27880 return equivalent_placeholder_constraints (c1
, c2
);
27883 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27884 constrained) auto, add it to the vector. */
27887 extract_autos_r (tree t
, void *data
)
27889 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27892 /* All the autos were built with index 0; fix that up now. */
27893 tree
*p
= hash
.find_slot (t
, INSERT
);
27896 /* If this is a repeated constrained-type-specifier, use the index we
27898 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27901 /* Otherwise this is new, so use the current count. */
27903 idx
= hash
.elements () - 1;
27905 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27908 /* Always keep walking. */
27912 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27913 says they can appear anywhere in the type. */
27916 extract_autos (tree type
)
27918 hash_set
<tree
> visited
;
27919 hash_table
<auto_hash
> hash (2);
27921 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27923 tree tree_vec
= make_tree_vec (hash
.elements());
27924 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27925 iter
!= hash
.end(); ++iter
)
27928 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27929 TREE_VEC_ELT (tree_vec
, i
)
27930 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27936 /* The stem for deduction guide names. */
27937 const char *const dguide_base
= "__dguide_";
27939 /* Return the name for a deduction guide for class template TMPL. */
27942 dguide_name (tree tmpl
)
27944 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27945 tree tname
= TYPE_IDENTIFIER (type
);
27946 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27947 + IDENTIFIER_LENGTH (tname
));
27948 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27949 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27950 IDENTIFIER_LENGTH (tname
) + 1);
27951 tree dname
= get_identifier (buf
);
27952 TREE_TYPE (dname
) = type
;
27956 /* True if NAME is the name of a deduction guide. */
27959 dguide_name_p (tree name
)
27961 return (TREE_CODE (name
) == IDENTIFIER_NODE
27962 && TREE_TYPE (name
)
27963 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27964 strlen (dguide_base
)));
27967 /* True if FN is a deduction guide. */
27970 deduction_guide_p (const_tree fn
)
27973 if (tree name
= DECL_NAME (fn
))
27974 return dguide_name_p (name
);
27978 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27981 copy_guide_p (const_tree fn
)
27983 gcc_assert (deduction_guide_p (fn
));
27984 if (!DECL_ARTIFICIAL (fn
))
27986 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27987 return (TREE_CHAIN (parms
) == void_list_node
27988 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27991 /* True if FN is a guide generated from a constructor template. */
27994 template_guide_p (const_tree fn
)
27996 gcc_assert (deduction_guide_p (fn
));
27997 if (!DECL_ARTIFICIAL (fn
))
27999 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28000 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28001 return PRIMARY_TEMPLATE_P (org
);
28005 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28006 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28007 template parameter types. Note that the handling of template template
28008 parameters relies on current_template_parms being set appropriately for the
28012 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28013 tree tsubst_args
, tsubst_flags_t complain
)
28015 if (olddecl
== error_mark_node
)
28016 return error_mark_node
;
28018 tree oldidx
= get_template_parm_index (olddecl
);
28021 if (TREE_CODE (olddecl
) == TYPE_DECL
28022 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28024 tree oldtype
= TREE_TYPE (olddecl
);
28025 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28026 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28027 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28028 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28029 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28033 newtype
= TREE_TYPE (olddecl
);
28034 if (type_uses_auto (newtype
))
28036 // Substitute once to fix references to other template parameters.
28037 newtype
= tsubst (newtype
, tsubst_args
,
28038 complain
|tf_partial
, NULL_TREE
);
28039 // Now substitute again to reduce the level of the auto.
28040 newtype
= tsubst (newtype
, current_template_args (),
28041 complain
, NULL_TREE
);
28044 newtype
= tsubst (newtype
, tsubst_args
,
28045 complain
, NULL_TREE
);
28049 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28050 DECL_NAME (olddecl
), newtype
);
28051 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28054 if (TREE_CODE (olddecl
) == TYPE_DECL
28055 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28057 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28058 = build_template_parm_index (index
, level
, level
,
28060 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28061 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28062 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28063 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28064 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28066 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28068 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28070 DECL_TEMPLATE_RESULT (newdecl
)
28071 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28072 DECL_NAME (olddecl
), newtype
);
28073 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28074 // First create a copy (ttargs) of tsubst_args with an
28075 // additional level for the template template parameter's own
28076 // template parameters (ttparms).
28077 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28078 (DECL_TEMPLATE_PARMS (olddecl
)));
28079 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28080 tree ttargs
= make_tree_vec (depth
+ 1);
28081 for (int i
= 0; i
< depth
; ++i
)
28082 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28083 TREE_VEC_ELT (ttargs
, depth
)
28084 = template_parms_level_to_args (ttparms
);
28085 // Substitute ttargs into ttparms to fix references to
28086 // other template parameters.
28087 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28088 complain
|tf_partial
);
28089 // Now substitute again with args based on tparms, to reduce
28090 // the level of the ttparms.
28091 ttargs
= current_template_args ();
28092 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28094 // Finally, tack the adjusted parms onto tparms.
28095 ttparms
= tree_cons (size_int (depth
), ttparms
,
28096 current_template_parms
);
28097 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28102 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28104 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28105 TREE_CODE (oldconst
),
28106 DECL_NAME (oldconst
), newtype
);
28107 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28108 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28109 SET_DECL_TEMPLATE_PARM_P (newconst
);
28110 newidx
= build_template_parm_index (index
, level
, level
,
28111 newconst
, newtype
);
28112 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28113 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28114 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28120 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28121 template parameter. */
28124 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28125 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28127 tree olddecl
= TREE_VALUE (oldelt
);
28128 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28130 if (newdecl
== error_mark_node
)
28131 return error_mark_node
;
28132 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28133 targs
, complain
, NULL_TREE
);
28134 tree list
= build_tree_list (newdef
, newdecl
);
28135 TEMPLATE_PARM_CONSTRAINTS (list
)
28136 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28137 targs
, complain
, NULL_TREE
);
28138 int depth
= TMPL_ARGS_DEPTH (targs
);
28139 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28143 /* Returns a C++17 class deduction guide template based on the constructor
28144 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28145 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28146 aggregate initialization guide. */
28149 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28151 tree tparms
, targs
, fparms
, fargs
, ci
;
28152 bool memtmpl
= false;
28155 tree fn_tmpl
= NULL_TREE
;
28159 ++processing_template_decl
;
28160 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28161 --processing_template_decl
;
28164 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28168 bool copy_p
= TYPE_REF_P (ctor
);
28170 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28172 fparms
= void_list_node
;
28174 else if (TREE_CODE (ctor
) == TREE_LIST
)
28177 gcc_unreachable ();
28179 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28180 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28181 targs
= CLASSTYPE_TI_ARGS (type
);
28184 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28185 explicit_p
= false;
28189 ++processing_template_decl
;
28193 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28194 : DECL_TI_TEMPLATE (ctor
));
28196 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28197 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28199 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28200 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28201 fully specialized args for the enclosing class. Strip those off, as
28202 the deduction guide won't have those template parameters. */
28203 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28204 TMPL_PARMS_DEPTH (tparms
));
28205 /* Discard the 'this' parameter. */
28206 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28207 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28208 ci
= get_constraints (ctor
);
28209 loc
= DECL_SOURCE_LOCATION (ctor
);
28210 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28212 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28216 /* For a member template constructor, we need to flatten the two
28217 template parameter lists into one, and then adjust the function
28218 signature accordingly. This gets...complicated. */
28219 tree save_parms
= current_template_parms
;
28221 /* For a member template we should have two levels of parms/args, one
28222 for the class and one for the constructor. We stripped
28223 specialized args for further enclosing classes above. */
28224 const int depth
= 2;
28225 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28227 /* Template args for translating references to the two-level template
28228 parameters into references to the one-level template parameters we
28230 tree tsubst_args
= copy_node (targs
);
28231 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28232 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28234 /* Template parms for the constructor template. */
28235 tree ftparms
= TREE_VALUE (tparms
);
28236 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28237 /* Template parms for the class template. */
28238 tparms
= TREE_CHAIN (tparms
);
28239 tree ctparms
= TREE_VALUE (tparms
);
28240 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28241 /* Template parms for the deduction guide start as a copy of the
28242 template parms for the class. We set current_template_parms for
28243 lookup_template_class_1. */
28244 current_template_parms
= tparms
= copy_node (tparms
);
28245 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28246 for (unsigned i
= 0; i
< clen
; ++i
)
28247 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28249 /* Now we need to rewrite the constructor parms to append them to the
28251 for (unsigned i
= 0; i
< flen
; ++i
)
28253 unsigned index
= i
+ clen
;
28254 unsigned level
= 1;
28255 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28257 = rewrite_tparm_list (oldelt
, index
, level
,
28258 tsubst_args
, i
, complain
);
28259 if (newelt
== error_mark_node
)
28261 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28264 /* Now we have a final set of template parms to substitute into the
28265 function signature. */
28266 targs
= template_parms_to_args (tparms
);
28267 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28269 if (fparms
== error_mark_node
)
28272 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28274 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28275 cp_unevaluated_operand. */
28277 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28278 current_template_parms
= save_parms
;
28281 --processing_template_decl
;
28283 return error_mark_node
;
28288 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28289 tparms
= copy_node (tparms
);
28290 INNERMOST_TEMPLATE_PARMS (tparms
)
28291 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28294 tree fntype
= build_function_type (type
, fparms
);
28295 tree ded_fn
= build_lang_decl_loc (loc
,
28297 dguide_name (type
), fntype
);
28298 DECL_ARGUMENTS (ded_fn
) = fargs
;
28299 DECL_ARTIFICIAL (ded_fn
) = true;
28300 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28301 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28302 DECL_ARTIFICIAL (ded_tmpl
) = true;
28303 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
28304 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
28305 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28306 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28308 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28310 set_constraints (ded_tmpl
, ci
);
28315 /* Add to LIST the member types for the reshaped initializer CTOR. */
28318 collect_ctor_idx_types (tree ctor
, tree list
)
28320 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28321 tree idx
, val
; unsigned i
;
28322 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28324 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28325 && CONSTRUCTOR_NELTS (val
))
28326 if (tree subidx
= CONSTRUCTOR_ELT (val
, 0)->index
)
28327 if (TREE_CODE (subidx
) == FIELD_DECL
)
28329 list
= collect_ctor_idx_types (val
, list
);
28332 tree ftype
= finish_decltype_type (idx
, true, tf_none
);
28333 list
= tree_cons (NULL_TREE
, ftype
, list
);
28339 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28342 is_spec_or_derived (tree etype
, tree tmpl
)
28344 if (!etype
|| !CLASS_TYPE_P (etype
))
28347 tree type
= TREE_TYPE (tmpl
);
28348 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28349 (DECL_TEMPLATE_PARMS (tmpl
)));
28350 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28351 int err
= unify (tparms
, targs
, type
, etype
,
28352 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28357 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28361 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28363 if (cxx_dialect
< cxx2a
)
28366 if (init
== NULL_TREE
)
28369 tree type
= TREE_TYPE (tmpl
);
28370 if (!CP_AGGREGATE_TYPE_P (type
))
28373 /* No aggregate candidate for copy-initialization. */
28374 if (args
->length() == 1)
28376 tree val
= (*args
)[0];
28377 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28381 /* If we encounter a problem, we just won't add the candidate. */
28382 tsubst_flags_t complain
= tf_none
;
28384 tree parms
= NULL_TREE
;
28385 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28387 init
= reshape_init (type
, init
, complain
);
28388 if (init
== error_mark_node
)
28390 parms
= collect_ctor_idx_types (init
, parms
);
28392 else if (TREE_CODE (init
) == TREE_LIST
)
28394 int len
= list_length (init
);
28395 for (tree field
= TYPE_FIELDS (type
);
28397 --len
, field
= DECL_CHAIN (field
))
28399 field
= next_initializable_field (field
);
28402 tree ftype
= finish_decltype_type (field
, true, complain
);
28403 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28407 /* Aggregate initialization doesn't apply to an initializer expression. */
28413 parms
= nreverse (parms
);
28414 TREE_CHAIN (last
) = void_list_node
;
28415 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28422 /* UGUIDES are the deduction guides for the underlying template of alias
28423 template TMPL; adjust them to be deduction guides for TMPL. */
28426 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28428 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28429 class type (9.2.8.2) where the template-name names an alias template A,
28430 the defining-type-id of A must be of the form
28432 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28434 as specified in 9.2.8.2. The guides of A are the set of functions or
28435 function templates formed as follows. For each function or function
28436 template f in the guides of the template named by the simple-template-id
28437 of the defining-type-id, the template arguments of the return type of f
28438 are deduced from the defining-type-id of A according to the process in
28439 13.10.2.5 with the exception that deduction does not fail if not all
28440 template arguments are deduced. Let g denote the result of substituting
28441 these deductions into f. If substitution succeeds, form a function or
28442 function template f' with the following properties and add it to the set
28445 * The function type of f' is the function type of g.
28447 * If f is a function template, f' is a function template whose template
28448 parameter list consists of all the template parameters of A (including
28449 their default template arguments) that appear in the above deductions or
28450 (recursively) in their default template arguments, followed by the
28451 template parameters of f that were not deduced (including their default
28452 template arguments), otherwise f' is not a function template.
28454 * The associated constraints (13.5.2) are the conjunction of the
28455 associated constraints of g and a constraint that is satisfied if and only
28456 if the arguments of A are deducible (see below) from the return type.
28458 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28461 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28462 considered to be so as well.
28464 * The explicit-specifier of f' is the explicit-specifier of g (if
28467 /* This implementation differs from the above in two significant ways:
28469 1) We include all template parameters of A, not just some.
28470 2) The added constraint is same_type instead of deducible.
28472 I believe that while it's probably possible to construct a testcase that
28473 behaves differently with this simplification, it should have the same
28474 effect for real uses. Including all template parameters means that we
28475 deduce all parameters of A when resolving the call, so when we're in the
28476 constraint we don't need to deduce them again, we can just check whether
28477 the deduction produced the desired result. */
28479 tsubst_flags_t complain
= tf_warning_or_error
;
28480 tree atype
= TREE_TYPE (tmpl
);
28481 tree aguides
= NULL_TREE
;
28482 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28483 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28484 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28485 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28489 location_t loc
= DECL_SOURCE_LOCATION (f
);
28490 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28492 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28494 processing_template_decl_sentinel
ptds (/*reset*/false);
28495 ++processing_template_decl
;
28497 /* Deduce template arguments for f from the type-id of A. */
28498 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28499 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28500 tree targs
= make_tree_vec (len
);
28501 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28504 /* The number of parms for f' is the number of parms for A plus
28505 non-deduced parms of f. */
28506 unsigned ndlen
= 0;
28508 for (unsigned i
= 0; i
< len
; ++i
)
28509 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28511 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28513 /* First copy over the parms of A. */
28514 for (j
= 0; j
< natparms
; ++j
)
28515 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28516 /* Now rewrite the non-deduced parms of f. */
28517 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28518 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28521 unsigned index
= j
++;
28522 unsigned level
= 1;
28523 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28524 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28525 targs
, i
, complain
);
28526 TREE_VEC_ELT (gtparms
, index
) = list
;
28528 gtparms
= build_tree_list (size_one_node
, gtparms
);
28530 /* Substitute the deduced arguments plus the rewritten template
28531 parameters into f to get g. This covers the type, copyness,
28532 guideness, and explicit-specifier. */
28533 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28534 if (g
== error_mark_node
)
28535 return error_mark_node
;
28536 DECL_USE_TEMPLATE (g
) = 0;
28537 fprime
= build_template_decl (g
, gtparms
, false);
28538 DECL_TEMPLATE_RESULT (fprime
) = g
;
28539 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28540 tree gtargs
= template_parms_to_args (gtparms
);
28541 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28542 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28544 /* Substitute the associated constraints. */
28545 tree ci
= get_constraints (f
);
28547 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28548 if (ci
== error_mark_node
)
28549 return error_mark_node
;
28551 /* Add a constraint that the return type matches the instantiation of
28552 A with the same template arguments. */
28553 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28554 if (!same_type_p (atype
, ret
)
28555 /* FIXME this should mean they don't compare as equivalent. */
28556 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28558 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28559 ci
= append_constraint (ci
, same
);
28563 set_constraints (fprime
, ci
);
28567 /* For a non-template deduction guide, if the arguments of A aren't
28568 deducible from the return type, don't add the candidate. */
28569 tree targs
= make_tree_vec (natparms
);
28570 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28571 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28572 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28578 aguides
= lookup_add (fprime
, aguides
);
28584 /* Return artificial deduction guides built from the constructors of class
28588 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28590 tree type
= TREE_TYPE (tmpl
);
28591 tree outer_args
= NULL_TREE
;
28592 if (DECL_CLASS_SCOPE_P (tmpl
)
28593 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28595 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28596 type
= TREE_TYPE (most_general_template (tmpl
));
28599 tree cands
= NULL_TREE
;
28601 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28603 /* Skip inherited constructors. */
28604 if (iter
.using_p ())
28607 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28608 cands
= lookup_add (guide
, cands
);
28611 /* Add implicit default constructor deduction guide. */
28612 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28614 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28616 cands
= lookup_add (guide
, cands
);
28619 /* Add copy guide. */
28621 tree gtype
= build_reference_type (type
);
28622 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28624 cands
= lookup_add (guide
, cands
);
28630 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28632 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28633 aggregate candidate is added separately because it depends on the
28637 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28639 tree guides
= NULL_TREE
;
28640 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28642 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28643 tree tinfo
= get_template_info (under
);
28644 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28648 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28649 dguide_name (tmpl
),
28650 /*type*/false, /*complain*/false,
28652 if (guides
== error_mark_node
)
28653 guides
= NULL_TREE
;
28656 /* Cache the deduction guides for a template. We also remember the result of
28657 lookup, and rebuild everything if it changes; should be very rare. */
28658 tree_pair_p cache
= NULL
;
28660 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28663 if (cache
->purpose
== guides
)
28664 return cache
->value
;
28668 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28669 cache
->purpose
= guides
;
28672 tree cands
= NULL_TREE
;
28673 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28674 cands
= alias_ctad_tweaks (tmpl
, guides
);
28677 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28678 for (ovl_iterator
it (guides
); it
; ++it
)
28679 cands
= lookup_add (*it
, cands
);
28682 cache
->value
= cands
;
28686 /* Return whether TMPL is a (class template argument-) deducible template. */
28689 ctad_template_p (tree tmpl
)
28691 /* A deducible template is either a class template or is an alias template
28692 whose defining-type-id is of the form
28694 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28696 where the nested-name-specifier (if any) is non-dependent and the
28697 template-name of the simple-template-id names a deducible template. */
28699 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28700 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28702 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28704 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28705 if (tree tinfo
= get_template_info (orig
))
28706 return ctad_template_p (TI_TEMPLATE (tinfo
));
28710 /* Deduce template arguments for the class template placeholder PTYPE for
28711 template TMPL based on the initializer INIT, and return the resulting
28715 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28716 int flags
, tsubst_flags_t complain
)
28718 /* We should have handled this in the caller. */
28719 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28722 /* Look through alias templates that just rename another template. */
28723 tmpl
= get_underlying_template (tmpl
);
28724 if (!ctad_template_p (tmpl
))
28726 if (complain
& tf_error
)
28727 error ("non-deducible template %qT used without template arguments", tmpl
);
28728 return error_mark_node
;
28730 else if (cxx_dialect
< cxx2a
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28732 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28733 if (complain
& tf_warning_or_error
)
28734 pedwarn (input_location
, 0, "alias template deduction only available "
28735 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28738 if (init
&& TREE_TYPE (init
) == ptype
)
28739 /* Using the template parm as its own argument. */
28742 tree type
= TREE_TYPE (tmpl
);
28744 bool try_list_ctor
= false;
28746 releasing_vec rv_args
= NULL
;
28747 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28748 if (init
== NULL_TREE
)
28749 args
= make_tree_vector ();
28750 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28752 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28753 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28755 /* As an exception, the first phase in 16.3.1.7 (considering the
28756 initializer list as a single argument) is omitted if the
28757 initializer list consists of a single expression of type cv U,
28758 where U is a specialization of C or a class derived from a
28759 specialization of C. */
28760 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28761 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28762 try_list_ctor
= false;
28764 if (try_list_ctor
|| is_std_init_list (type
))
28765 args
= make_tree_vector_single (init
);
28767 args
= make_tree_vector_from_ctor (init
);
28769 else if (TREE_CODE (init
) == TREE_LIST
)
28770 args
= make_tree_vector_from_list (init
);
28772 args
= make_tree_vector_single (init
);
28774 /* Do this now to avoid problems with erroneous args later on. */
28775 args
= resolve_args (args
, complain
);
28777 return error_mark_node
;
28779 tree cands
= deduction_guides_for (tmpl
, complain
);
28780 if (cands
== error_mark_node
)
28781 return error_mark_node
;
28783 /* Prune explicit deduction guides in copy-initialization context. */
28784 bool elided
= false;
28785 if (flags
& LOOKUP_ONLYCONVERTING
)
28787 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28788 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28793 /* Found a nonconverting guide, prune the candidates. */
28794 tree pruned
= NULL_TREE
;
28795 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28796 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28797 pruned
= lookup_add (*iter
, pruned
);
28803 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
28804 cands
= lookup_add (guide
, cands
);
28806 tree call
= error_mark_node
;
28808 /* If this is list-initialization and the class has a list constructor, first
28809 try deducing from the list as a single argument, as [over.match.list]. */
28810 tree list_cands
= NULL_TREE
;
28811 if (try_list_ctor
&& cands
)
28812 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28815 if (is_list_ctor (dg
))
28816 list_cands
= lookup_add (dg
, list_cands
);
28820 ++cp_unevaluated_operand
;
28821 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28822 --cp_unevaluated_operand
;
28824 if (call
== error_mark_node
)
28826 /* That didn't work, now try treating the list as a sequence of
28828 release_tree_vector (args
);
28829 args
= make_tree_vector_from_ctor (init
);
28833 if (elided
&& !cands
)
28835 error ("cannot deduce template arguments for copy-initialization"
28836 " of %qT, as it has no non-explicit deduction guides or "
28837 "user-declared constructors", type
);
28838 return error_mark_node
;
28840 else if (!cands
&& call
== error_mark_node
)
28842 error ("cannot deduce template arguments of %qT, as it has no viable "
28843 "deduction guides", type
);
28844 return error_mark_node
;
28847 if (call
== error_mark_node
)
28849 ++cp_unevaluated_operand
;
28850 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28851 --cp_unevaluated_operand
;
28854 if (call
== error_mark_node
28855 && (complain
& tf_warning_or_error
))
28857 error ("class template argument deduction failed:");
28859 ++cp_unevaluated_operand
;
28860 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28861 --cp_unevaluated_operand
;
28864 inform (input_location
, "explicit deduction guides not considered "
28865 "for copy-initialization");
28868 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28871 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28872 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28873 The CONTEXT determines the context in which auto deduction is performed
28874 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28875 OUTER_TARGS are used during template argument deduction
28876 (context == adc_unify) to properly substitute the result, and is ignored
28879 For partial-concept-ids, extra args may be appended to the list of deduced
28880 template arguments prior to determining constraint satisfaction. */
28883 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28884 tsubst_flags_t complain
, auto_deduction_context context
,
28885 tree outer_targs
, int flags
)
28889 if (init
== error_mark_node
)
28890 return error_mark_node
;
28892 if (init
&& type_dependent_expression_p (init
)
28893 && context
!= adc_unify
)
28894 /* Defining a subset of type-dependent expressions that we can deduce
28895 from ahead of time isn't worth the trouble. */
28898 /* Similarly, we can't deduce from another undeduced decl. */
28899 if (init
&& undeduced_auto_decl (init
))
28902 /* We may be doing a partial substitution, but we still want to replace
28904 complain
&= ~tf_partial
;
28906 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28907 /* C++17 class template argument deduction. */
28908 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28910 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28911 /* Nothing we can do with this, even in deduction context. */
28914 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28915 with either a new invented type template parameter U or, if the
28916 initializer is a braced-init-list (8.5.4), with
28917 std::initializer_list<U>. */
28918 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28920 if (!DIRECT_LIST_INIT_P (init
))
28921 type
= listify_autos (type
, auto_node
);
28922 else if (CONSTRUCTOR_NELTS (init
) == 1)
28923 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28926 if (complain
& tf_warning_or_error
)
28928 if (permerror (input_location
, "direct-list-initialization of "
28929 "%<auto%> requires exactly one element"))
28930 inform (input_location
,
28931 "for deduction to %<std::initializer_list%>, use copy-"
28932 "list-initialization (i.e. add %<=%> before the %<{%>)");
28934 type
= listify_autos (type
, auto_node
);
28938 if (type
== error_mark_node
)
28939 return error_mark_node
;
28941 init
= resolve_nondeduced_context (init
, complain
);
28943 if (context
== adc_decomp_type
28944 && auto_node
== type
28945 && init
!= error_mark_node
28946 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
28947 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28948 and initializer has array type, deduce cv-qualified array type. */
28949 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
28951 else if (AUTO_IS_DECLTYPE (auto_node
))
28953 tree stripped_init
= tree_strip_any_location_wrapper (init
);
28954 bool id
= (DECL_P (stripped_init
)
28955 || ((TREE_CODE (init
) == COMPONENT_REF
28956 || TREE_CODE (init
) == SCOPE_REF
)
28957 && !REF_PARENTHESIZED_P (init
)));
28958 targs
= make_tree_vec (1);
28959 TREE_VEC_ELT (targs
, 0)
28960 = finish_decltype_type (init
, id
, tf_warning_or_error
);
28961 if (type
!= auto_node
)
28963 if (complain
& tf_error
)
28964 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
28965 return error_mark_node
;
28970 if (error_operand_p (init
))
28971 return error_mark_node
;
28973 tree parms
= build_tree_list (NULL_TREE
, type
);
28977 tparms
= extract_autos (type
);
28980 tparms
= make_tree_vec (1);
28981 TREE_VEC_ELT (tparms
, 0)
28982 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
28985 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28986 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28988 NULL
, /*explain_p=*/false);
28991 if (processing_template_decl
)
28992 /* Try again at instantiation time. */
28994 if (type
&& type
!= error_mark_node
28995 && (complain
& tf_error
))
28996 /* If type is error_mark_node a diagnostic must have been
28997 emitted by now. Also, having a mention to '<type error>'
28998 in the diagnostic is not really useful to the user. */
29001 && FNDECL_USED_AUTO (current_function_decl
)
29003 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29004 && LAMBDA_FUNCTION_P (current_function_decl
))
29005 error ("unable to deduce lambda return type from %qE", init
);
29007 error ("unable to deduce %qT from %qE", type
, init
);
29008 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29010 NULL
, /*explain_p=*/true);
29012 return error_mark_node
;
29016 /* Check any placeholder constraints against the deduced type. */
29017 if (flag_concepts
&& !processing_template_decl
)
29018 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29020 /* Use the deduced type to check the associated constraints. If we
29021 have a partial-concept-id, rebuild the argument list so that
29022 we check using the extra arguments. */
29023 check
= unpack_concept_check (check
);
29024 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29025 tree
cdecl = TREE_OPERAND (check
, 0);
29027 cdecl = OVL_FIRST (cdecl);
29028 tree cargs
= TREE_OPERAND (check
, 1);
29029 if (TREE_VEC_LENGTH (cargs
) > 1)
29031 cargs
= copy_node (cargs
);
29032 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29037 /* Rebuild the check using the deduced arguments. */
29038 check
= build_concept_check (cdecl, cargs
, tf_none
);
29040 if (!constraints_satisfied_p (check
))
29042 if (complain
& tf_warning_or_error
)
29044 auto_diagnostic_group d
;
29047 case adc_unspecified
:
29049 error("placeholder constraints not satisfied");
29051 case adc_variable_type
:
29052 case adc_decomp_type
:
29053 error ("deduced initializer does not satisfy "
29054 "placeholder constraints");
29056 case adc_return_type
:
29057 error ("deduced return type does not satisfy "
29058 "placeholder constraints");
29060 case adc_requirement
:
29061 error ("deduced expression type does not satisfy "
29062 "placeholder constraints");
29065 diagnose_constraints (input_location
, check
, targs
);
29067 return error_mark_node
;
29071 if (processing_template_decl
&& context
!= adc_unify
)
29072 outer_targs
= current_template_args ();
29073 targs
= add_to_template_args (outer_targs
, targs
);
29074 return tsubst (type
, targs
, complain
, NULL_TREE
);
29077 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29081 splice_late_return_type (tree type
, tree late_return_type
)
29083 if (late_return_type
)
29085 gcc_assert (is_auto (type
) || seen_error ());
29086 return late_return_type
;
29089 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29091 tree idx
= get_template_parm_index (*auto_node
);
29092 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29094 /* In an abbreviated function template we didn't know we were dealing
29095 with a function template when we saw the auto return type, so update
29096 it to have the correct level. */
29097 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29098 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29099 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29100 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29101 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29102 *auto_node
= new_auto
;
29108 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29109 'decltype(auto)' or a deduced class template. */
29112 is_auto (const_tree type
)
29114 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29115 && (TYPE_IDENTIFIER (type
) == auto_identifier
29116 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29122 /* for_each_template_parm callback for type_uses_auto. */
29125 is_auto_r (tree tp
, void */
*data*/
)
29127 return is_auto (tp
);
29130 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29131 a use of `auto'. Returns NULL_TREE otherwise. */
29134 type_uses_auto (tree type
)
29136 if (type
== NULL_TREE
)
29138 else if (flag_concepts
)
29140 /* The Concepts TS allows multiple autos in one type-specifier; just
29141 return the first one we find, do_auto_deduction will collect all of
29143 if (uses_template_parms (type
))
29144 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29145 /*visited*/NULL
, /*nondeduced*/false);
29149 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29155 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29156 concepts are enabled, auto is acceptable in template arguments, but
29157 only when TEMPL identifies a template class. Return TRUE if any
29158 such errors were reported. */
29161 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29163 /* If there were previous errors, nevermind. */
29164 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29167 /* If TMPL is an identifier, we're parsing and we can't tell yet
29168 whether TMPL is supposed to be a type, a function or a variable.
29169 We'll only be able to tell during template substitution, so we
29170 expect to be called again then. If concepts are enabled and we
29171 know we have a type, we're ok. */
29173 && (identifier_p (tmpl
)
29175 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29176 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29179 /* Quickly search for any occurrences of auto; usually there won't
29180 be any, and then we'll avoid allocating the vector. */
29181 if (!type_uses_auto (args
))
29184 bool errors
= false;
29186 tree vec
= extract_autos (args
);
29187 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29189 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29190 error_at (DECL_SOURCE_LOCATION (xauto
),
29191 "invalid use of %qT in template argument", xauto
);
29198 /* For a given template T, return the vector of typedefs referenced
29199 in T for which access check is needed at T instantiation time.
29200 T is either a FUNCTION_DECL or a RECORD_TYPE.
29201 Those typedefs were added to T by the function
29202 append_type_to_template_for_access_check. */
29204 vec
<qualified_typedef_usage_t
, va_gc
> *
29205 get_types_needing_access_check (tree t
)
29208 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
29210 if (!t
|| t
== error_mark_node
)
29213 if (!(ti
= get_template_info (t
)))
29216 if (CLASS_TYPE_P (t
)
29217 || TREE_CODE (t
) == FUNCTION_DECL
)
29219 if (!TI_TEMPLATE (ti
))
29222 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
29228 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29229 tied to T. That list of typedefs will be access checked at
29230 T instantiation time.
29231 T is either a FUNCTION_DECL or a RECORD_TYPE.
29232 TYPE_DECL is a TYPE_DECL node representing a typedef.
29233 SCOPE is the scope through which TYPE_DECL is accessed.
29234 LOCATION is the location of the usage point of TYPE_DECL.
29236 This function is a subroutine of
29237 append_type_to_template_for_access_check. */
29240 append_type_to_template_for_access_check_1 (tree t
,
29243 location_t location
)
29245 qualified_typedef_usage_t typedef_usage
;
29248 if (!t
|| t
== error_mark_node
)
29251 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
29252 || CLASS_TYPE_P (t
))
29254 && TREE_CODE (type_decl
) == TYPE_DECL
29257 if (!(ti
= get_template_info (t
)))
29260 gcc_assert (TI_TEMPLATE (ti
));
29262 typedef_usage
.typedef_decl
= type_decl
;
29263 typedef_usage
.context
= scope
;
29264 typedef_usage
.locus
= location
;
29266 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
29269 /* Append TYPE_DECL to the template TEMPL.
29270 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29271 At TEMPL instanciation time, TYPE_DECL will be checked to see
29272 if it can be accessed through SCOPE.
29273 LOCATION is the location of the usage point of TYPE_DECL.
29275 e.g. consider the following code snippet:
29282 template<class U> struct S
29284 C::myint mi; // <-- usage point of the typedef C::myint
29289 At S<char> instantiation time, we need to check the access of C::myint
29290 In other words, we need to check the access of the myint typedef through
29291 the C scope. For that purpose, this function will add the myint typedef
29292 and the scope C through which its being accessed to a list of typedefs
29293 tied to the template S. That list will be walked at template instantiation
29294 time and access check performed on each typedefs it contains.
29295 Note that this particular code snippet should yield an error because
29296 myint is private to C. */
29299 append_type_to_template_for_access_check (tree templ
,
29302 location_t location
)
29304 qualified_typedef_usage_t
*iter
;
29307 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
29309 /* Make sure we don't append the type to the template twice. */
29310 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
29311 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
29314 append_type_to_template_for_access_check_1 (templ
, type_decl
,
29318 /* Recursively walk over && expressions searching for EXPR. Return a reference
29319 to that expression. */
29321 static tree
*find_template_requirement (tree
*t
, tree key
)
29325 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29327 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29329 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29335 /* Convert the generic type parameters in PARM that match the types given in the
29336 range [START_IDX, END_IDX) from the current_template_parms into generic type
29340 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29342 tree current
= current_template_parms
;
29343 int depth
= TMPL_PARMS_DEPTH (current
);
29344 current
= INNERMOST_TEMPLATE_PARMS (current
);
29345 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29347 for (int i
= 0; i
< start_idx
; ++i
)
29348 TREE_VEC_ELT (replacement
, i
)
29349 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29351 for (int i
= start_idx
; i
< end_idx
; ++i
)
29353 /* Create a distinct parameter pack type from the current parm and add it
29354 to the replacement args to tsubst below into the generic function
29356 tree node
= TREE_VEC_ELT (current
, i
);
29357 tree o
= TREE_TYPE (TREE_VALUE (node
));
29358 tree t
= copy_type (o
);
29359 TEMPLATE_TYPE_PARM_INDEX (t
)
29360 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29362 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29363 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29364 TYPE_MAIN_VARIANT (t
) = t
;
29365 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29366 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29367 TREE_VEC_ELT (replacement
, i
) = t
;
29369 /* Replace the current template parameter with new pack. */
29370 TREE_VALUE (node
) = TREE_CHAIN (t
);
29372 /* Surgically adjust the associated constraint of adjusted parameter
29373 and it's corresponding contribution to the current template
29375 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29377 tree id
= unpack_concept_check (constr
);
29378 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
29379 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29380 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29382 /* If there was a constraint, we also need to replace that in
29383 the template requirements, which we've already built. */
29384 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29385 reqs
= find_template_requirement (reqs
, constr
);
29390 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29391 TREE_VEC_ELT (replacement
, i
)
29392 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29394 /* If there are more levels then build up the replacement with the outer
29397 replacement
= add_to_template_args (template_parms_to_args
29398 (TREE_CHAIN (current_template_parms
)),
29401 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29404 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29408 declare_integer_pack (void)
29410 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29411 build_function_type_list (integer_type_node
,
29414 NULL_TREE
, ECF_CONST
);
29415 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29416 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29417 CP_BUILT_IN_INTEGER_PACK
);
29420 /* Set up the hash tables for template instantiations. */
29423 init_template_processing (void)
29425 /* FIXME: enable sanitization (PR87847) */
29426 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
29427 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
29429 if (cxx_dialect
>= cxx11
)
29430 declare_integer_pack ();
29433 /* Print stats about the template hash tables for -fstats. */
29436 print_template_statistics (void)
29438 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29439 "%f collisions\n", (long) decl_specializations
->size (),
29440 (long) decl_specializations
->elements (),
29441 decl_specializations
->collisions ());
29442 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29443 "%f collisions\n", (long) type_specializations
->size (),
29444 (long) type_specializations
->elements (),
29445 type_specializations
->collisions ());
29450 namespace selftest
{
29452 /* Verify that build_non_dependent_expr () works, for various expressions,
29453 and that location wrappers don't affect the results. */
29456 test_build_non_dependent_expr ()
29458 location_t loc
= BUILTINS_LOCATION
;
29460 /* Verify constants, without and with location wrappers. */
29461 tree int_cst
= build_int_cst (integer_type_node
, 42);
29462 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29464 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29465 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29466 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29468 tree string_lit
= build_string (4, "foo");
29469 TREE_TYPE (string_lit
) = char_array_type_node
;
29470 string_lit
= fix_string_type (string_lit
);
29471 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29473 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29474 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29475 ASSERT_EQ (wrapped_string_lit
,
29476 build_non_dependent_expr (wrapped_string_lit
));
29479 /* Verify that type_dependent_expression_p () works correctly, even
29480 in the presence of location wrapper nodes. */
29483 test_type_dependent_expression_p ()
29485 location_t loc
= BUILTINS_LOCATION
;
29487 tree name
= get_identifier ("foo");
29489 /* If no templates are involved, nothing is type-dependent. */
29490 gcc_assert (!processing_template_decl
);
29491 ASSERT_FALSE (type_dependent_expression_p (name
));
29493 ++processing_template_decl
;
29495 /* Within a template, an unresolved name is always type-dependent. */
29496 ASSERT_TRUE (type_dependent_expression_p (name
));
29498 /* Ensure it copes with NULL_TREE and errors. */
29499 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29500 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29502 /* A USING_DECL in a template should be type-dependent, even if wrapped
29503 with a location wrapper (PR c++/83799). */
29504 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29505 TREE_TYPE (using_decl
) = integer_type_node
;
29506 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29507 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29508 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29509 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29511 --processing_template_decl
;
29514 /* Run all of the selftests within this file. */
29519 test_build_non_dependent_expr ();
29520 test_type_dependent_expression_p ();
29523 } // namespace selftest
29525 #endif /* #if CHECKING_P */
29527 #include "gt-cp-pt.h"