1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 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"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
80 // Implementation of the RAII helper for creating new local
82 local_specialization_stack::local_specialization_stack (lss_policy policy
)
83 : saved (local_specializations
)
85 if (policy
== lss_blank
|| !saved
)
86 local_specializations
= new hash_map
<tree
, tree
>;
88 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
91 local_specialization_stack::~local_specialization_stack ()
93 delete local_specializations
;
94 local_specializations
= saved
;
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth
;
100 struct GTY((for_user
)) spec_entry
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
115 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132 enum template_base_result
{
134 tbr_ambiguous_baseclass
,
138 static void push_access_scope (tree
);
139 static void pop_access_scope (tree
);
140 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
141 unification_kind_t
, int,
143 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
144 unification_kind_t
, int, bool, bool);
145 static int unify (tree
, tree
, tree
, tree
, int, bool);
146 static void add_pending_template (tree
);
147 static tree
reopen_tinst_level (struct tinst_level
*);
148 static tree
tsubst_initializer_list (tree
, tree
);
149 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
150 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
154 static void tsubst_enum (tree
, tree
, tree
);
155 static tree
add_to_template_args (tree
, tree
);
156 static tree
add_outermost_template_args (tree
, tree
);
157 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
158 static int check_non_deducible_conversion (tree
, tree
, int, int,
159 struct conversion
**, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
162 static int type_unification_real (tree
, tree
, tree
, const tree
*,
163 unsigned int, int, unification_kind_t
,
164 vec
<deferred_access_check
, va_gc
> **,
166 static void note_template_header (int);
167 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
168 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
169 static tree
convert_template_argument (tree
, tree
, tree
,
170 tsubst_flags_t
, int, tree
);
171 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
172 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
173 static tree
expand_template_argument_pack (tree
);
174 static tree
build_template_parm_index (int, int, int, tree
, tree
);
175 static bool inline_needs_template_parms (tree
, bool);
176 static void push_inline_template_parms_recursive (tree
, int);
177 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
178 static int mark_template_parm (tree
, void *);
179 static int template_parm_this_level_p (tree
, void *);
180 static tree
tsubst_friend_function (tree
, tree
);
181 static tree
tsubst_friend_class (tree
, tree
);
182 static int can_complete_type_without_circularity (tree
);
183 static tree
get_bindings (tree
, tree
, tree
, bool);
184 static int template_decl_level (tree
);
185 static int check_cv_quals_for_unify (int, tree
, tree
);
186 static void template_parm_level_and_index (tree
, int*, int*);
187 static int unify_pack_expansion (tree
, tree
, tree
,
188 tree
, unification_kind_t
, bool, bool);
189 static tree
copy_template_args (tree
);
190 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
192 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
193 static 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 int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
205 static bool template_template_parm_bindings_ok_p (tree
, tree
);
206 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
207 static tree
for_each_template_parm_r (tree
*, int *, void *);
208 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
209 static void copy_default_args_to_explicit_spec (tree
);
210 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
211 static bool dependent_template_arg_p (tree
);
212 static bool any_template_arguments_need_structural_equality_p (tree
);
213 static bool dependent_type_p_r (tree
);
214 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
215 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
216 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
217 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
225 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
226 static tree
make_argument_pack (tree
);
227 static void register_parameter_specializations (tree
, tree
);
228 static tree
enclosing_instantiation_of (tree tctx
);
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
236 push_access_scope (tree t
)
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
239 || TREE_CODE (t
) == TYPE_DECL
);
241 if (DECL_FRIEND_CONTEXT (t
))
242 push_nested_class (DECL_FRIEND_CONTEXT (t
));
243 else if (DECL_CLASS_SCOPE_P (t
))
244 push_nested_class (DECL_CONTEXT (t
));
246 push_to_top_level ();
248 if (TREE_CODE (t
) == FUNCTION_DECL
)
250 vec_safe_push (saved_access_scope
, current_function_decl
);
251 current_function_decl
= t
;
255 /* Restore the scope set up by push_access_scope. T is the node we
259 pop_access_scope (tree t
)
261 if (TREE_CODE (t
) == FUNCTION_DECL
)
262 current_function_decl
= saved_access_scope
->pop();
264 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
267 pop_from_top_level ();
270 /* Do any processing required when DECL (a member template
271 declaration) is finished. Returns the TEMPLATE_DECL corresponding
272 to DECL, unless it is a specialization, in which case the DECL
273 itself is returned. */
276 finish_member_template_decl (tree decl
)
278 if (decl
== error_mark_node
)
279 return error_mark_node
;
281 gcc_assert (DECL_P (decl
));
283 if (TREE_CODE (decl
) == TYPE_DECL
)
287 type
= TREE_TYPE (decl
);
288 if (type
== error_mark_node
)
289 return error_mark_node
;
290 if (MAYBE_CLASS_TYPE_P (type
)
291 && CLASSTYPE_TEMPLATE_INFO (type
)
292 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
294 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
295 check_member_template (tmpl
);
300 else if (TREE_CODE (decl
) == FIELD_DECL
)
301 error_at (DECL_SOURCE_LOCATION (decl
),
302 "data member %qD cannot be a member template", decl
);
303 else if (DECL_TEMPLATE_INFO (decl
))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
307 check_member_template (DECL_TI_TEMPLATE (decl
));
308 return DECL_TI_TEMPLATE (decl
);
314 error_at (DECL_SOURCE_LOCATION (decl
),
315 "invalid member template declaration %qD", decl
);
317 return error_mark_node
;
320 /* Create a template info node. */
323 build_template_info (tree template_decl
, tree template_args
)
325 tree result
= make_node (TEMPLATE_INFO
);
326 TI_TEMPLATE (result
) = template_decl
;
327 TI_ARGS (result
) = template_args
;
331 /* Return the template info node corresponding to T, whatever T is. */
334 get_template_info (const_tree t
)
336 tree tinfo
= NULL_TREE
;
338 if (!t
|| t
== error_mark_node
)
341 if (TREE_CODE (t
) == NAMESPACE_DECL
342 || TREE_CODE (t
) == PARM_DECL
)
345 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
346 tinfo
= DECL_TEMPLATE_INFO (t
);
348 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
351 if (OVERLOAD_TYPE_P (t
))
352 tinfo
= TYPE_TEMPLATE_INFO (t
);
353 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
354 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
359 /* Returns the template nesting level of the indicated class TYPE.
369 A<T>::B<U> has depth two, while A<T> has depth one.
370 Both A<T>::B<int> and A<int>::B<U> have depth one, if
371 they are instantiations, not specializations.
373 This function is guaranteed to return 0 if passed NULL_TREE so
374 that, for example, `template_class_depth (current_class_type)' is
378 template_class_depth (tree type
)
382 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
384 tree tinfo
= get_template_info (type
);
386 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
387 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
391 type
= CP_DECL_CONTEXT (type
);
392 else if (LAMBDA_TYPE_P (type
))
393 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
395 type
= CP_TYPE_CONTEXT (type
);
401 /* Return TRUE if NODE instantiates a template that has arguments of
402 its own, be it directly a primary template or indirectly through a
403 partial specializations. */
405 instantiates_primary_template_p (tree node
)
407 tree tinfo
= get_template_info (node
);
411 tree tmpl
= TI_TEMPLATE (tinfo
);
412 if (PRIMARY_TEMPLATE_P (tmpl
))
415 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
418 /* So now we know we have a specialization, but it could be a full
419 or a partial specialization. To tell which, compare the depth of
420 its template arguments with those of its context. */
422 tree ctxt
= DECL_CONTEXT (tmpl
);
423 tree ctinfo
= get_template_info (ctxt
);
427 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
428 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
431 /* Subroutine of maybe_begin_member_template_processing.
432 Returns true if processing DECL needs us to push template parms. */
435 inline_needs_template_parms (tree decl
, bool nsdmi
)
437 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
440 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
441 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
444 /* Subroutine of maybe_begin_member_template_processing.
445 Push the template parms in PARMS, starting from LEVELS steps into the
446 chain, and ending at the beginning, since template parms are listed
450 push_inline_template_parms_recursive (tree parmlist
, int levels
)
452 tree parms
= TREE_VALUE (parmlist
);
456 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
458 ++processing_template_decl
;
459 current_template_parms
460 = tree_cons (size_int (processing_template_decl
),
461 parms
, current_template_parms
);
462 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
464 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
466 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
468 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
470 if (error_operand_p (parm
))
473 gcc_assert (DECL_P (parm
));
475 switch (TREE_CODE (parm
))
483 /* Push the CONST_DECL. */
484 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
493 /* Restore the template parameter context for a member template, a
494 friend template defined in a class definition, or a non-template
495 member of template class. */
498 maybe_begin_member_template_processing (tree decl
)
502 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
506 tree ctx
= DECL_CONTEXT (decl
);
507 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
508 /* Disregard full specializations (c++/60999). */
509 && uses_template_parms (ctx
)
510 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
513 if (inline_needs_template_parms (decl
, nsdmi
))
515 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
516 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
518 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
521 parms
= TREE_CHAIN (parms
);
524 push_inline_template_parms_recursive (parms
, levels
);
527 /* Remember how many levels of template parameters we pushed so that
528 we can pop them later. */
529 inline_parm_levels
.safe_push (levels
);
532 /* Undo the effects of maybe_begin_member_template_processing. */
535 maybe_end_member_template_processing (void)
540 if (inline_parm_levels
.length () == 0)
543 last
= inline_parm_levels
.pop ();
544 for (i
= 0; i
< last
; ++i
)
546 --processing_template_decl
;
547 current_template_parms
= TREE_CHAIN (current_template_parms
);
552 /* Return a new template argument vector which contains all of ARGS,
553 but has as its innermost set of arguments the EXTRA_ARGS. */
556 add_to_template_args (tree args
, tree extra_args
)
563 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
566 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
567 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
569 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
570 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
572 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
573 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
578 /* Like add_to_template_args, but only the outermost ARGS are added to
579 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
580 (EXTRA_ARGS) levels are added. This function is used to combine
581 the template arguments from a partial instantiation with the
582 template arguments used to attain the full instantiation from the
583 partial instantiation. */
586 add_outermost_template_args (tree args
, tree extra_args
)
590 /* If there are more levels of EXTRA_ARGS than there are ARGS,
591 something very fishy is going on. */
592 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
594 /* If *all* the new arguments will be the EXTRA_ARGS, just return
596 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
599 /* For the moment, we make ARGS look like it contains fewer levels. */
600 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
602 new_args
= add_to_template_args (args
, extra_args
);
604 /* Now, we restore ARGS to its full dimensions. */
605 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
610 /* Return the N levels of innermost template arguments from the ARGS. */
613 get_innermost_template_args (tree args
, int n
)
621 /* If N is 1, just return the innermost set of template arguments. */
623 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
625 /* If we're not removing anything, just return the arguments we were
627 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
628 gcc_assert (extra_levels
>= 0);
629 if (extra_levels
== 0)
632 /* Make a new set of arguments, not containing the outer arguments. */
633 new_args
= make_tree_vec (n
);
634 for (i
= 1; i
<= n
; ++i
)
635 SET_TMPL_ARGS_LEVEL (new_args
, i
,
636 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
641 /* The inverse of get_innermost_template_args: Return all but the innermost
642 EXTRA_LEVELS levels of template arguments from the ARGS. */
645 strip_innermost_template_args (tree args
, int extra_levels
)
648 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
653 /* If N is 1, just return the outermost set of template arguments. */
655 return TMPL_ARGS_LEVEL (args
, 1);
657 /* If we're not removing anything, just return the arguments we were
659 gcc_assert (extra_levels
>= 0);
660 if (extra_levels
== 0)
663 /* Make a new set of arguments, not containing the inner arguments. */
664 new_args
= make_tree_vec (n
);
665 for (i
= 1; i
<= n
; ++i
)
666 SET_TMPL_ARGS_LEVEL (new_args
, i
,
667 TMPL_ARGS_LEVEL (args
, i
));
672 /* We've got a template header coming up; push to a new level for storing
676 begin_template_parm_list (void)
678 /* We use a non-tag-transparent scope here, which causes pushtag to
679 put tags in this scope, rather than in the enclosing class or
680 namespace scope. This is the right thing, since we want
681 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
682 global template class, push_template_decl handles putting the
683 TEMPLATE_DECL into top-level scope. For a nested template class,
686 template <class T> struct S1 {
687 template <class T> struct S2 {};
690 pushtag contains special code to insert the TEMPLATE_DECL for S2
691 at the right scope. */
692 begin_scope (sk_template_parms
, NULL
);
693 ++processing_template_decl
;
694 ++processing_template_parmlist
;
695 note_template_header (0);
697 /* Add a dummy parameter level while we process the parameter list. */
698 current_template_parms
699 = tree_cons (size_int (processing_template_decl
),
701 current_template_parms
);
704 /* This routine is called when a specialization is declared. If it is
705 invalid to declare a specialization here, an error is reported and
706 false is returned, otherwise this routine will return true. */
709 check_specialization_scope (void)
711 tree scope
= current_scope ();
715 An explicit specialization shall be declared in the namespace of
716 which the template is a member, or, for member templates, in the
717 namespace of which the enclosing class or enclosing class
718 template is a member. An explicit specialization of a member
719 function, member class or static data member of a class template
720 shall be declared in the namespace of which the class template
722 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
724 error ("explicit specialization in non-namespace scope %qD", scope
);
730 In an explicit specialization declaration for a member of a class
731 template or a member template that appears in namespace scope,
732 the member template and some of its enclosing class templates may
733 remain unspecialized, except that the declaration shall not
734 explicitly specialize a class member template if its enclosing
735 class templates are not explicitly specialized as well. */
736 if (current_template_parms
)
738 error ("enclosing class templates are not explicitly specialized");
745 /* We've just seen template <>. */
748 begin_specialization (void)
750 begin_scope (sk_template_spec
, NULL
);
751 note_template_header (1);
752 return check_specialization_scope ();
755 /* Called at then end of processing a declaration preceded by
759 end_specialization (void)
762 reset_specialization ();
765 /* Any template <>'s that we have seen thus far are not referring to a
766 function specialization. */
769 reset_specialization (void)
771 processing_specialization
= 0;
772 template_header_count
= 0;
775 /* We've just seen a template header. If SPECIALIZATION is nonzero,
776 it was of the form template <>. */
779 note_template_header (int specialization
)
781 processing_specialization
= specialization
;
782 template_header_count
++;
785 /* We're beginning an explicit instantiation. */
788 begin_explicit_instantiation (void)
790 gcc_assert (!processing_explicit_instantiation
);
791 processing_explicit_instantiation
= true;
796 end_explicit_instantiation (void)
798 gcc_assert (processing_explicit_instantiation
);
799 processing_explicit_instantiation
= false;
802 /* An explicit specialization or partial specialization of TMPL is being
803 declared. Check that the namespace in which the specialization is
804 occurring is permissible. Returns false iff it is invalid to
805 specialize TMPL in the current namespace. */
808 check_specialization_namespace (tree tmpl
)
810 tree tpl_ns
= decl_namespace_context (tmpl
);
814 An explicit specialization shall be declared in a namespace enclosing the
815 specialized template. An explicit specialization whose declarator-id is
816 not qualified shall be declared in the nearest enclosing namespace of the
817 template, or, if the namespace is inline (7.3.1), any namespace from its
818 enclosing namespace set. */
819 if (current_scope() != DECL_CONTEXT (tmpl
)
820 && !at_namespace_scope_p ())
822 error ("specialization of %qD must appear at namespace scope", tmpl
);
826 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
827 /* Same or enclosing namespace. */
831 auto_diagnostic_group d
;
832 if (permerror (input_location
,
833 "specialization of %qD in different namespace", tmpl
))
834 inform (DECL_SOURCE_LOCATION (tmpl
),
835 " from definition of %q#D", tmpl
);
840 /* SPEC is an explicit instantiation. Check that it is valid to
841 perform this explicit instantiation in the current namespace. */
844 check_explicit_instantiation_namespace (tree spec
)
848 /* DR 275: An explicit instantiation shall appear in an enclosing
849 namespace of its template. */
850 ns
= decl_namespace_context (spec
);
851 if (!is_nested_namespace (current_namespace
, ns
))
852 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
853 "(which does not enclose namespace %qD)",
854 spec
, current_namespace
, ns
);
857 // Returns the type of a template specialization only if that
858 // specialization needs to be defined. Otherwise (e.g., if the type has
859 // already been defined), the function returns NULL_TREE.
861 maybe_new_partial_specialization (tree type
)
863 // An implicit instantiation of an incomplete type implies
864 // the definition of a new class template.
866 // template<typename T>
869 // template<typename T>
872 // Here, S<T*> is an implicit instantiation of S whose type
874 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
877 // It can also be the case that TYPE is a completed specialization.
878 // Continuing the previous example, suppose we also declare:
880 // template<typename T>
881 // requires Integral<T>
884 // Here, S<T*> refers to the specialization S<T*> defined
885 // above. However, we need to differentiate definitions because
886 // we intend to define a new partial specialization. In this case,
887 // we rely on the fact that the constraints are different for
888 // this declaration than that above.
890 // Note that we also get here for injected class names and
891 // late-parsed template definitions. We must ensure that we
892 // do not create new type declarations for those cases.
893 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
895 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
896 tree args
= CLASSTYPE_TI_ARGS (type
);
898 // If there are no template parameters, this cannot be a new
899 // partial template specializtion?
900 if (!current_template_parms
)
903 // The injected-class-name is not a new partial specialization.
904 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
907 // If the constraints are not the same as those of the primary
908 // then, we can probably create a new specialization.
909 tree type_constr
= current_template_constraints ();
911 if (type
== TREE_TYPE (tmpl
))
913 tree main_constr
= get_constraints (tmpl
);
914 if (equivalent_constraints (type_constr
, main_constr
))
918 // Also, if there's a pre-existing specialization with matching
919 // constraints, then this also isn't new.
920 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
923 tree spec_tmpl
= TREE_VALUE (specs
);
924 tree spec_args
= TREE_PURPOSE (specs
);
925 tree spec_constr
= get_constraints (spec_tmpl
);
926 if (comp_template_args (args
, spec_args
)
927 && equivalent_constraints (type_constr
, spec_constr
))
929 specs
= TREE_CHAIN (specs
);
932 // Create a new type node (and corresponding type decl)
933 // for the newly declared specialization.
934 tree t
= make_class_type (TREE_CODE (type
));
935 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
936 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
938 /* We only need a separate type node for storing the definition of this
939 partial specialization; uses of S<T*> are unconstrained, so all are
940 equivalent. So keep TYPE_CANONICAL the same. */
941 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
943 // Build the corresponding type decl.
944 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
945 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
946 DECL_SOURCE_LOCATION (d
) = input_location
;
954 /* The TYPE is being declared. If it is a template type, that means it
955 is a partial specialization. Do appropriate error-checking. */
958 maybe_process_partial_specialization (tree type
)
962 if (type
== error_mark_node
)
963 return error_mark_node
;
965 /* A lambda that appears in specialization context is not itself a
967 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
970 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
972 error ("name of class shadows template template parameter %qD",
974 return error_mark_node
;
977 context
= TYPE_CONTEXT (type
);
979 if (TYPE_ALIAS_P (type
))
981 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
983 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
984 error ("specialization of alias template %qD",
985 TI_TEMPLATE (tinfo
));
987 error ("explicit specialization of non-template %qT", type
);
988 return error_mark_node
;
990 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
992 /* This is for ordinary explicit specialization and partial
993 specialization of a template class such as:
995 template <> class C<int>;
999 template <class T> class C<T*>;
1001 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1003 if (tree t
= maybe_new_partial_specialization (type
))
1005 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1006 && !at_namespace_scope_p ())
1007 return error_mark_node
;
1008 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1009 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1010 if (processing_template_decl
)
1012 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1013 if (decl
== error_mark_node
)
1014 return error_mark_node
;
1015 return TREE_TYPE (decl
);
1018 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1019 error ("specialization of %qT after instantiation", type
);
1020 else if (errorcount
&& !processing_specialization
1021 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1022 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1023 /* Trying to define a specialization either without a template<> header
1024 or in an inappropriate place. We've already given an error, so just
1025 bail now so we don't actually define the specialization. */
1026 return error_mark_node
;
1028 else if (CLASS_TYPE_P (type
)
1029 && !CLASSTYPE_USE_TEMPLATE (type
)
1030 && CLASSTYPE_TEMPLATE_INFO (type
)
1031 && context
&& CLASS_TYPE_P (context
)
1032 && CLASSTYPE_TEMPLATE_INFO (context
))
1034 /* This is for an explicit specialization of member class
1035 template according to [temp.expl.spec/18]:
1037 template <> template <class U> class C<int>::D;
1039 The context `C<int>' must be an implicit instantiation.
1040 Otherwise this is just a member class template declared
1043 template <> class C<int> { template <class U> class D; };
1044 template <> template <class U> class C<int>::D;
1046 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1047 while in the second case, `C<int>::D' is a primary template
1048 and `C<T>::D' may not exist. */
1050 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1051 && !COMPLETE_TYPE_P (type
))
1054 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1056 if (current_namespace
1057 != decl_namespace_context (tmpl
))
1059 if (permerror (input_location
,
1060 "specialization of %qD in different namespace",
1062 inform (DECL_SOURCE_LOCATION (tmpl
),
1063 "from definition of %q#D", tmpl
);
1066 /* Check for invalid specialization after instantiation:
1068 template <> template <> class C<int>::D<int>;
1069 template <> template <class U> class C<int>::D; */
1071 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1072 t
; t
= TREE_CHAIN (t
))
1074 tree inst
= TREE_VALUE (t
);
1075 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1076 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1078 /* We already have a full specialization of this partial
1079 instantiation, or a full specialization has been
1080 looked up but not instantiated. Reassign it to the
1081 new member specialization template. */
1085 elt
.tmpl
= most_general_template (tmpl
);
1086 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1089 type_specializations
->remove_elt (&elt
);
1092 CLASSTYPE_TI_ARGS (inst
)
1093 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1096 = type_specializations
->find_slot (&elt
, INSERT
);
1097 entry
= ggc_alloc
<spec_entry
> ();
1102 /* But if we've had an implicit instantiation, that's a
1103 problem ([temp.expl.spec]/6). */
1104 error ("specialization %qT after instantiation %qT",
1108 /* Mark TYPE as a specialization. And as a result, we only
1109 have one level of template argument for the innermost
1111 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1112 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1113 CLASSTYPE_TI_ARGS (type
)
1114 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1117 else if (processing_specialization
)
1119 /* Someday C++0x may allow for enum template specialization. */
1120 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1121 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1122 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1123 "of %qD not allowed by ISO C++", type
);
1126 error ("explicit specialization of non-template %qT", type
);
1127 return error_mark_node
;
1134 /* Returns nonzero if we can optimize the retrieval of specializations
1135 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1136 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1139 optimize_specialization_lookup_p (tree tmpl
)
1141 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1142 && DECL_CLASS_SCOPE_P (tmpl
)
1143 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1145 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1146 /* The optimized lookup depends on the fact that the
1147 template arguments for the member function template apply
1148 purely to the containing class, which is not true if the
1149 containing class is an explicit or partial
1151 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1152 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1153 && !DECL_CONV_FN_P (tmpl
)
1154 /* It is possible to have a template that is not a member
1155 template and is not a member of a template class:
1157 template <typename T>
1158 struct S { friend A::f(); };
1160 Here, the friend function is a template, but the context does
1161 not have template information. The optimized lookup relies
1162 on having ARGS be the template arguments for both the class
1163 and the function template. */
1164 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1167 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1168 gone through coerce_template_parms by now. */
1171 verify_unstripped_args_1 (tree inner
)
1173 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1175 tree arg
= TREE_VEC_ELT (inner
, i
);
1176 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1178 else if (TYPE_P (arg
))
1179 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1180 else if (ARGUMENT_PACK_P (arg
))
1181 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1182 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1183 /* Allow typedefs on the type of a non-type argument, since a
1184 parameter can have them. */;
1186 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1191 verify_unstripped_args (tree args
)
1193 ++processing_template_decl
;
1194 if (!any_dependent_template_arguments_p (args
))
1195 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1196 --processing_template_decl
;
1199 /* Retrieve the specialization (in the sense of [temp.spec] - a
1200 specialization is either an instantiation or an explicit
1201 specialization) of TMPL for the given template ARGS. If there is
1202 no such specialization, return NULL_TREE. The ARGS are a vector of
1203 arguments, or a vector of vectors of arguments, in the case of
1204 templates with more than one level of parameters.
1206 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1207 then we search for a partial specialization matching ARGS. This
1208 parameter is ignored if TMPL is not a class template.
1210 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1211 result is a NONTYPE_ARGUMENT_PACK. */
1214 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1216 if (tmpl
== NULL_TREE
)
1219 if (args
== error_mark_node
)
1222 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1223 || TREE_CODE (tmpl
) == FIELD_DECL
);
1225 /* There should be as many levels of arguments as there are
1226 levels of parameters. */
1227 gcc_assert (TMPL_ARGS_DEPTH (args
)
1228 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1229 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1230 : template_class_depth (DECL_CONTEXT (tmpl
))));
1233 verify_unstripped_args (args
);
1235 /* Lambda functions in templates aren't instantiated normally, but through
1236 tsubst_lambda_expr. */
1237 if (lambda_fn_in_template_p (tmpl
))
1240 if (optimize_specialization_lookup_p (tmpl
))
1242 /* The template arguments actually apply to the containing
1243 class. Find the class specialization with those
1245 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1246 tree class_specialization
1247 = retrieve_specialization (class_template
, args
, 0);
1248 if (!class_specialization
)
1251 /* Find the instance of TMPL. */
1252 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1253 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1256 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1257 /* using-declarations can add base methods to the method vec,
1258 and we don't want those here. */
1259 && DECL_CONTEXT (fn
) == class_specialization
)
1268 hash_table
<spec_hasher
> *specializations
;
1272 elt
.spec
= NULL_TREE
;
1274 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1275 specializations
= type_specializations
;
1277 specializations
= decl_specializations
;
1280 hash
= spec_hasher::hash (&elt
);
1281 found
= specializations
->find_with_hash (&elt
, hash
);
1289 /* Like retrieve_specialization, but for local declarations. */
1292 retrieve_local_specialization (tree tmpl
)
1294 if (local_specializations
== NULL
)
1297 tree
*slot
= local_specializations
->get (tmpl
);
1298 return slot
? *slot
: NULL_TREE
;
1301 /* Returns nonzero iff DECL is a specialization of TMPL. */
1304 is_specialization_of (tree decl
, tree tmpl
)
1308 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1312 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1318 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1320 for (t
= TREE_TYPE (decl
);
1322 t
= CLASSTYPE_USE_TEMPLATE (t
)
1323 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1324 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1331 /* Returns nonzero iff DECL is a specialization of friend declaration
1332 FRIEND_DECL according to [temp.friend]. */
1335 is_specialization_of_friend (tree decl
, tree friend_decl
)
1337 bool need_template
= true;
1340 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1341 || TREE_CODE (decl
) == TYPE_DECL
);
1343 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1344 of a template class, we want to check if DECL is a specialization
1346 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1347 && DECL_TEMPLATE_INFO (friend_decl
)
1348 && !DECL_USE_TEMPLATE (friend_decl
))
1350 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1351 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1352 need_template
= false;
1354 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1355 && !PRIMARY_TEMPLATE_P (friend_decl
))
1356 need_template
= false;
1358 /* There is nothing to do if this is not a template friend. */
1359 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1362 if (is_specialization_of (decl
, friend_decl
))
1366 A member of a class template may be declared to be a friend of a
1367 non-template class. In this case, the corresponding member of
1368 every specialization of the class template is a friend of the
1369 class granting friendship.
1371 For example, given a template friend declaration
1373 template <class T> friend void A<T>::f();
1375 the member function below is considered a friend
1377 template <> struct A<int> {
1381 For this type of template friend, TEMPLATE_DEPTH below will be
1382 nonzero. To determine if DECL is a friend of FRIEND, we first
1383 check if the enclosing class is a specialization of another. */
1385 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1387 && DECL_CLASS_SCOPE_P (decl
)
1388 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1389 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1391 /* Next, we check the members themselves. In order to handle
1392 a few tricky cases, such as when FRIEND_DECL's are
1394 template <class T> friend void A<T>::g(T t);
1395 template <class T> template <T t> friend void A<T>::h();
1399 void A<int>::g(int);
1400 template <int> void A<int>::h();
1402 we need to figure out ARGS, the template arguments from
1403 the context of DECL. This is required for template substitution
1404 of `T' in the function parameter of `g' and template parameter
1405 of `h' in the above examples. Here ARGS corresponds to `int'. */
1407 tree context
= DECL_CONTEXT (decl
);
1408 tree args
= NULL_TREE
;
1409 int current_depth
= 0;
1411 while (current_depth
< template_depth
)
1413 if (CLASSTYPE_TEMPLATE_INFO (context
))
1415 if (current_depth
== 0)
1416 args
= TYPE_TI_ARGS (context
);
1418 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1421 context
= TYPE_CONTEXT (context
);
1424 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1429 tree friend_args_type
;
1430 tree decl_args_type
;
1432 /* Make sure that both DECL and FRIEND_DECL are templates or
1434 is_template
= DECL_TEMPLATE_INFO (decl
)
1435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1436 if (need_template
^ is_template
)
1438 else if (is_template
)
1440 /* If both are templates, check template parameter list. */
1442 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1444 if (!comp_template_parms
1445 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1449 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1452 decl_type
= TREE_TYPE (decl
);
1454 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1455 tf_none
, NULL_TREE
);
1456 if (friend_type
== error_mark_node
)
1459 /* Check if return types match. */
1460 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1463 /* Check if function parameter types match, ignoring the
1464 `this' parameter. */
1465 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1466 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1468 friend_args_type
= TREE_CHAIN (friend_args_type
);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1470 decl_args_type
= TREE_CHAIN (decl_args_type
);
1472 return compparms (decl_args_type
, friend_args_type
);
1476 /* DECL is a TYPE_DECL */
1478 tree decl_type
= TREE_TYPE (decl
);
1480 /* Make sure that both DECL and FRIEND_DECL are templates or
1483 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1484 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1486 if (need_template
^ is_template
)
1488 else if (is_template
)
1491 /* If both are templates, check the name of the two
1492 TEMPLATE_DECL's first because is_friend didn't. */
1493 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1494 != DECL_NAME (friend_decl
))
1497 /* Now check template parameter list. */
1499 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1501 return comp_template_parms
1502 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1506 return (DECL_NAME (decl
)
1507 == DECL_NAME (friend_decl
));
1513 /* Register the specialization SPEC as a specialization of TMPL with
1514 the indicated ARGS. IS_FRIEND indicates whether the specialization
1515 is actually just a friend declaration. ATTRLIST is the list of
1516 attributes that the specialization is declared with or NULL when
1517 it isn't. Returns SPEC, or an equivalent prior declaration, if
1520 We also store instantiations of field packs in the hash table, even
1521 though they are not themselves templates, to make lookup easier. */
1524 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1528 spec_entry
**slot
= NULL
;
1531 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1532 || (TREE_CODE (tmpl
) == FIELD_DECL
1533 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1535 if (TREE_CODE (spec
) == FUNCTION_DECL
1536 && uses_template_parms (DECL_TI_ARGS (spec
)))
1537 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1538 register it; we want the corresponding TEMPLATE_DECL instead.
1539 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1540 the more obvious `uses_template_parms (spec)' to avoid problems
1541 with default function arguments. In particular, given
1542 something like this:
1544 template <class T> void f(T t1, T t = T())
1546 the default argument expression is not substituted for in an
1547 instantiation unless and until it is actually needed. */
1550 if (optimize_specialization_lookup_p (tmpl
))
1551 /* We don't put these specializations in the hash table, but we might
1552 want to give an error about a mismatch. */
1553 fn
= retrieve_specialization (tmpl
, args
, 0);
1561 hash
= spec_hasher::hash (&elt
);
1564 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1566 fn
= ((spec_entry
*) *slot
)->spec
;
1571 /* We can sometimes try to re-register a specialization that we've
1572 already got. In particular, regenerate_decl_from_template calls
1573 duplicate_decls which will update the specialization list. But,
1574 we'll still get called again here anyhow. It's more convenient
1575 to simply allow this than to try to prevent it. */
1578 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1580 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1582 if (DECL_ODR_USED (fn
)
1583 || DECL_EXPLICIT_INSTANTIATION (fn
))
1585 error ("specialization of %qD after instantiation",
1587 return error_mark_node
;
1592 /* This situation should occur only if the first
1593 specialization is an implicit instantiation, the
1594 second is an explicit specialization, and the
1595 implicit instantiation has not yet been used. That
1596 situation can occur if we have implicitly
1597 instantiated a member function and then specialized
1600 We can also wind up here if a friend declaration that
1601 looked like an instantiation turns out to be a
1604 template <class T> void foo(T);
1605 class S { friend void foo<>(int) };
1606 template <> void foo(int);
1608 We transform the existing DECL in place so that any
1609 pointers to it become pointers to the updated
1612 If there was a definition for the template, but not
1613 for the specialization, we want this to look as if
1614 there were no definition, and vice versa. */
1615 DECL_INITIAL (fn
) = NULL_TREE
;
1616 duplicate_decls (spec
, fn
, is_friend
);
1617 /* The call to duplicate_decls will have applied
1620 An explicit specialization of a function template
1621 is inline only if it is explicitly declared to be,
1622 and independently of whether its function template
1625 to the primary function; now copy the inline bits to
1626 the various clones. */
1627 FOR_EACH_CLONE (clone
, fn
)
1629 DECL_DECLARED_INLINE_P (clone
)
1630 = DECL_DECLARED_INLINE_P (fn
);
1631 DECL_SOURCE_LOCATION (clone
)
1632 = DECL_SOURCE_LOCATION (fn
);
1633 DECL_DELETED_FN (clone
)
1634 = DECL_DELETED_FN (fn
);
1636 check_specialization_namespace (tmpl
);
1641 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1643 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1644 if (dd
== error_mark_node
)
1645 /* We've already complained in duplicate_decls. */
1646 return error_mark_node
;
1648 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1649 /* Dup decl failed, but this is a new definition. Set the
1650 line number so any errors match this new
1652 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1658 return duplicate_decls (spec
, fn
, is_friend
);
1660 /* A specialization must be declared in the same namespace as the
1661 template it is specializing. */
1662 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1663 && !check_specialization_namespace (tmpl
))
1664 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1666 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1668 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1669 gcc_assert (tmpl
&& args
&& spec
);
1672 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1673 && PRIMARY_TEMPLATE_P (tmpl
)
1674 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1675 || variable_template_p (tmpl
))
1676 /* If TMPL is a forward declaration of a template function, keep a list
1677 of all specializations in case we need to reassign them to a friend
1678 template later in tsubst_friend_function.
1680 Also keep a list of all variable template instantiations so that
1681 process_partial_specialization can check whether a later partial
1682 specialization would have used it. */
1683 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1684 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1690 /* Returns true iff two spec_entry nodes are equivalent. */
1692 int comparing_specializations
;
1695 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1699 ++comparing_specializations
;
1700 equal
= (e1
->tmpl
== e2
->tmpl
1701 && comp_template_args (e1
->args
, e2
->args
));
1702 if (equal
&& flag_concepts
1703 /* tmpl could be a FIELD_DECL for a capture pack. */
1704 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1705 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1706 && uses_template_parms (e1
->args
))
1708 /* Partial specializations of a variable template can be distinguished by
1710 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1711 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1712 equal
= equivalent_constraints (c1
, c2
);
1714 --comparing_specializations
;
1719 /* Returns a hash for a template TMPL and template arguments ARGS. */
1722 hash_tmpl_and_args (tree tmpl
, tree args
)
1724 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1725 return iterative_hash_template_arg (args
, val
);
1728 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1732 spec_hasher::hash (spec_entry
*e
)
1734 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1737 /* Recursively calculate a hash value for a template argument ARG, for use
1738 in the hash tables of template specializations. */
1741 iterative_hash_template_arg (tree arg
, hashval_t val
)
1743 unsigned HOST_WIDE_INT i
;
1744 enum tree_code code
;
1747 if (arg
== NULL_TREE
)
1748 return iterative_hash_object (arg
, val
);
1753 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1756 code
= TREE_CODE (arg
);
1757 tclass
= TREE_CODE_CLASS (code
);
1759 val
= iterative_hash_object (code
, val
);
1766 case IDENTIFIER_NODE
:
1767 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1771 int i
, len
= TREE_VEC_LENGTH (arg
);
1772 for (i
= 0; i
< len
; ++i
)
1773 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1777 case TYPE_PACK_EXPANSION
:
1778 case EXPR_PACK_EXPANSION
:
1779 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1780 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1782 case TYPE_ARGUMENT_PACK
:
1783 case NONTYPE_ARGUMENT_PACK
:
1784 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1787 for (; arg
; arg
= TREE_CHAIN (arg
))
1788 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1792 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1793 val
= iterative_hash_template_arg (*iter
, val
);
1799 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1802 val
= iterative_hash_template_arg (field
, val
);
1803 val
= iterative_hash_template_arg (value
, val
);
1809 if (!DECL_ARTIFICIAL (arg
))
1811 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1812 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1814 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1817 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1820 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1821 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1823 case TEMPLATE_PARM_INDEX
:
1824 val
= iterative_hash_template_arg
1825 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1826 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1827 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1830 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1831 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1832 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1835 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1837 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1841 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1842 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1843 val
= iterative_hash_object (code
, val
);
1844 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1847 /* [temp.over.link] Two lambda-expressions are never considered
1850 So just hash the closure type. */
1851 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1854 case IMPLICIT_CONV_EXPR
:
1855 case STATIC_CAST_EXPR
:
1856 case REINTERPRET_CAST_EXPR
:
1857 case CONST_CAST_EXPR
:
1858 case DYNAMIC_CAST_EXPR
:
1860 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1861 /* Now hash operands as usual. */
1866 tree fn
= CALL_EXPR_FN (arg
);
1867 if (tree name
= dependent_name (fn
))
1869 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1870 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1873 val
= iterative_hash_template_arg (fn
, val
);
1874 call_expr_arg_iterator ai
;
1875 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1876 x
= next_call_expr_arg (&ai
))
1877 val
= iterative_hash_template_arg (x
, val
);
1888 if (alias_template_specialization_p (arg
))
1890 // We want an alias specialization that survived strip_typedefs
1891 // to hash differently from its TYPE_CANONICAL, to avoid hash
1892 // collisions that compare as different in template_args_equal.
1893 // These could be dependent specializations that strip_typedefs
1894 // left alone, or untouched specializations because
1895 // coerce_template_parms returns the unconverted template
1896 // arguments if it sees incomplete argument packs.
1897 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1898 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1900 if (TYPE_CANONICAL (arg
))
1901 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1903 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1904 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1905 /* Otherwise just compare the types during lookup. */
1908 case tcc_declaration
:
1910 return iterative_hash_expr (arg
, val
);
1913 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1915 unsigned n
= cp_tree_operand_length (arg
);
1916 for (i
= 0; i
< n
; ++i
)
1917 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1925 /* Unregister the specialization SPEC as a specialization of TMPL.
1926 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1927 if the SPEC was listed as a specialization of TMPL.
1929 Note that SPEC has been ggc_freed, so we can't look inside it. */
1932 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1937 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1938 elt
.args
= TI_ARGS (tinfo
);
1939 elt
.spec
= NULL_TREE
;
1941 entry
= decl_specializations
->find (&elt
);
1944 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1945 gcc_assert (new_spec
!= NULL_TREE
);
1946 entry
->spec
= new_spec
;
1953 /* Like register_specialization, but for local declarations. We are
1954 registering SPEC, an instantiation of TMPL. */
1957 register_local_specialization (tree spec
, tree tmpl
)
1959 gcc_assert (tmpl
!= spec
);
1960 local_specializations
->put (tmpl
, spec
);
1963 /* TYPE is a class type. Returns true if TYPE is an explicitly
1964 specialized class. */
1967 explicit_class_specialization_p (tree type
)
1969 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1971 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1974 /* Print the list of functions at FNS, going through all the overloads
1975 for each element of the list. Alternatively, FNS cannot be a
1976 TREE_LIST, in which case it will be printed together with all the
1979 MORE and *STR should respectively be FALSE and NULL when the function
1980 is called from the outside. They are used internally on recursive
1981 calls. print_candidates manages the two parameters and leaves NULL
1982 in *STR when it ends. */
1985 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1987 if (TREE_CODE (fns
) == TREE_LIST
)
1988 for (; fns
; fns
= TREE_CHAIN (fns
))
1989 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1991 for (lkp_iterator
iter (fns
); iter
;)
1996 const char *pfx
= *str
;
2000 pfx
= _("candidates are:");
2002 pfx
= _("candidate is:");
2003 *str
= get_spaces (pfx
);
2005 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2009 /* Print the list of candidate FNS in an error message. FNS can also
2010 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2013 print_candidates (tree fns
)
2016 print_candidates_1 (fns
, &str
);
2020 /* Get a (possibly) constrained template declaration for the
2021 purpose of ordering candidates. */
2023 get_template_for_ordering (tree list
)
2025 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2026 tree f
= TREE_VALUE (list
);
2027 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2028 return TI_TEMPLATE (ti
);
2032 /* Among candidates having the same signature, return the
2033 most constrained or NULL_TREE if there is no best candidate.
2034 If the signatures of candidates vary (e.g., template
2035 specialization vs. member function), then there can be no
2038 Note that we don't compare constraints on the functions
2039 themselves, but rather those of their templates. */
2041 most_constrained_function (tree candidates
)
2043 // Try to find the best candidate in a first pass.
2044 tree champ
= candidates
;
2045 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2047 int winner
= more_constrained (get_template_for_ordering (champ
),
2048 get_template_for_ordering (c
));
2050 champ
= c
; // The candidate is more constrained
2051 else if (winner
== 0)
2052 return NULL_TREE
; // Neither is more constrained
2055 // Verify that the champ is better than previous candidates.
2056 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2057 if (!more_constrained (get_template_for_ordering (champ
),
2058 get_template_for_ordering (c
)))
2066 /* Returns the template (one of the functions given by TEMPLATE_ID)
2067 which can be specialized to match the indicated DECL with the
2068 explicit template args given in TEMPLATE_ID. The DECL may be
2069 NULL_TREE if none is available. In that case, the functions in
2070 TEMPLATE_ID are non-members.
2072 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2073 specialization of a member template.
2075 The TEMPLATE_COUNT is the number of references to qualifying
2076 template classes that appeared in the name of the function. See
2077 check_explicit_specialization for a more accurate description.
2079 TSK indicates what kind of template declaration (if any) is being
2080 declared. TSK_TEMPLATE indicates that the declaration given by
2081 DECL, though a FUNCTION_DECL, has template parameters, and is
2082 therefore a template function.
2084 The template args (those explicitly specified and those deduced)
2085 are output in a newly created vector *TARGS_OUT.
2087 If it is impossible to determine the result, an error message is
2088 issued. The error_mark_node is returned to indicate failure. */
2091 determine_specialization (tree template_id
,
2094 int need_member_template
,
2100 tree explicit_targs
;
2101 tree candidates
= NULL_TREE
;
2103 /* A TREE_LIST of templates of which DECL may be a specialization.
2104 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2105 corresponding TREE_PURPOSE is the set of template arguments that,
2106 when used to instantiate the template, would produce a function
2107 with the signature of DECL. */
2108 tree templates
= NULL_TREE
;
2110 cp_binding_level
*b
;
2112 *targs_out
= NULL_TREE
;
2114 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2115 return error_mark_node
;
2117 /* We shouldn't be specializing a member template of an
2118 unspecialized class template; we already gave an error in
2119 check_specialization_scope, now avoid crashing. */
2121 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2122 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2124 gcc_assert (errorcount
);
2125 return error_mark_node
;
2128 fns
= TREE_OPERAND (template_id
, 0);
2129 explicit_targs
= TREE_OPERAND (template_id
, 1);
2131 if (fns
== error_mark_node
)
2132 return error_mark_node
;
2134 /* Check for baselinks. */
2135 if (BASELINK_P (fns
))
2136 fns
= BASELINK_FUNCTIONS (fns
);
2138 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2140 error ("%qD is not a function template", fns
);
2141 return error_mark_node
;
2143 else if (VAR_P (decl
) && !variable_template_p (fns
))
2145 error ("%qD is not a variable template", fns
);
2146 return error_mark_node
;
2149 /* Count the number of template headers specified for this
2152 for (b
= current_binding_level
;
2153 b
->kind
== sk_template_parms
;
2157 tree orig_fns
= fns
;
2159 if (variable_template_p (fns
))
2161 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2162 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2163 tf_warning_or_error
,
2164 /*req_all*/true, /*use_defarg*/true);
2165 if (targs
!= error_mark_node
)
2166 templates
= tree_cons (targs
, fns
, templates
);
2168 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2172 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2174 tree decl_arg_types
;
2178 /* In case of explicit specialization, we need to check if
2179 the number of template headers appearing in the specialization
2180 is correct. This is usually done in check_explicit_specialization,
2181 but the check done there cannot be exhaustive when specializing
2182 member functions. Consider the following code:
2184 template <> void A<int>::f(int);
2185 template <> template <> void A<int>::f(int);
2187 Assuming that A<int> is not itself an explicit specialization
2188 already, the first line specializes "f" which is a non-template
2189 member function, whilst the second line specializes "f" which
2190 is a template member function. So both lines are syntactically
2191 correct, and check_explicit_specialization does not reject
2194 Here, we can do better, as we are matching the specialization
2195 against the declarations. We count the number of template
2196 headers, and we check if they match TEMPLATE_COUNT + 1
2197 (TEMPLATE_COUNT is the number of qualifying template classes,
2198 plus there must be another header for the member template
2201 Notice that if header_count is zero, this is not a
2202 specialization but rather a template instantiation, so there
2203 is no check we can perform here. */
2204 if (header_count
&& header_count
!= template_count
+ 1)
2207 /* Check that the number of template arguments at the
2208 innermost level for DECL is the same as for FN. */
2209 if (current_binding_level
->kind
== sk_template_parms
2210 && !current_binding_level
->explicit_spec_p
2211 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2212 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2213 (current_template_parms
))))
2216 /* DECL might be a specialization of FN. */
2217 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2218 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2220 /* For a non-static member function, we need to make sure
2221 that the const qualification is the same. Since
2222 get_bindings does not try to merge the "this" parameter,
2223 we must do the comparison explicitly. */
2224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2226 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2227 TREE_VALUE (decl_arg_types
)))
2230 /* And the ref-qualification. */
2231 if (type_memfn_rqual (TREE_TYPE (decl
))
2232 != type_memfn_rqual (TREE_TYPE (fn
)))
2236 /* Skip the "this" parameter and, for constructors of
2237 classes with virtual bases, the VTT parameter. A
2238 full specialization of a constructor will have a VTT
2239 parameter, but a template never will. */
2241 = skip_artificial_parms_for (decl
, decl_arg_types
);
2243 = skip_artificial_parms_for (fn
, fn_arg_types
);
2245 /* Function templates cannot be specializations; there are
2246 no partial specializations of functions. Therefore, if
2247 the type of DECL does not match FN, there is no
2250 Note that it should never be the case that we have both
2251 candidates added here, and for regular member functions
2253 if (tsk
== tsk_template
)
2255 if (compparms (fn_arg_types
, decl_arg_types
))
2256 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2260 /* See whether this function might be a specialization of this
2261 template. Suppress access control because we might be trying
2262 to make this specialization a friend, and we have already done
2263 access control for the declaration of the specialization. */
2264 push_deferring_access_checks (dk_no_check
);
2265 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2266 pop_deferring_access_checks ();
2269 /* We cannot deduce template arguments that when used to
2270 specialize TMPL will produce DECL. */
2273 if (uses_template_parms (targs
))
2274 /* We deduced something involving 'auto', which isn't a valid
2275 template argument. */
2278 /* Remove, from the set of candidates, all those functions
2279 whose constraints are not satisfied. */
2280 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2283 // Then, try to form the new function type.
2284 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2285 if (insttype
== error_mark_node
)
2288 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2289 if (!compparms (fn_arg_types
, decl_arg_types
))
2292 /* Save this template, and the arguments deduced. */
2293 templates
= tree_cons (targs
, fn
, templates
);
2295 else if (need_member_template
)
2296 /* FN is an ordinary member function, and we need a
2297 specialization of a member template. */
2299 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2300 /* We can get IDENTIFIER_NODEs here in certain erroneous
2303 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2304 /* This is just an ordinary non-member function. Nothing can
2305 be a specialization of that. */
2307 else if (DECL_ARTIFICIAL (fn
))
2308 /* Cannot specialize functions that are created implicitly. */
2312 tree decl_arg_types
;
2314 /* This is an ordinary member function. However, since
2315 we're here, we can assume its enclosing class is a
2316 template class. For example,
2318 template <typename T> struct S { void f(); };
2319 template <> void S<int>::f() {}
2321 Here, S<int>::f is a non-template, but S<int> is a
2322 template class. If FN has the same type as DECL, we
2323 might be in business. */
2325 if (!DECL_TEMPLATE_INFO (fn
))
2326 /* Its enclosing class is an explicit specialization
2327 of a template class. This is not a candidate. */
2330 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2331 TREE_TYPE (TREE_TYPE (fn
))))
2332 /* The return types differ. */
2335 /* Adjust the type of DECL in case FN is a static member. */
2336 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2337 if (DECL_STATIC_FUNCTION_P (fn
)
2338 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2339 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2341 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2345 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2346 && (type_memfn_rqual (TREE_TYPE (decl
))
2347 != type_memfn_rqual (TREE_TYPE (fn
))))
2350 // If the deduced arguments do not satisfy the constraints,
2351 // this is not a candidate.
2352 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2355 // Add the candidate.
2356 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2360 if (templates
&& TREE_CHAIN (templates
))
2366 It is possible for a specialization with a given function
2367 signature to be instantiated from more than one function
2368 template. In such cases, explicit specification of the
2369 template arguments must be used to uniquely identify the
2370 function template specialization being specialized.
2372 Note that here, there's no suggestion that we're supposed to
2373 determine which of the candidate templates is most
2374 specialized. However, we, also have:
2378 Partial ordering of overloaded function template
2379 declarations is used in the following contexts to select
2380 the function template to which a function template
2381 specialization refers:
2383 -- when an explicit specialization refers to a function
2386 So, we do use the partial ordering rules, at least for now.
2387 This extension can only serve to make invalid programs valid,
2388 so it's safe. And, there is strong anecdotal evidence that
2389 the committee intended the partial ordering rules to apply;
2390 the EDG front end has that behavior, and John Spicer claims
2391 that the committee simply forgot to delete the wording in
2392 [temp.expl.spec]. */
2393 tree tmpl
= most_specialized_instantiation (templates
);
2394 if (tmpl
!= error_mark_node
)
2397 TREE_CHAIN (templates
) = NULL_TREE
;
2401 // Concepts allows multiple declarations of member functions
2402 // with the same signature. Like above, we need to rely on
2403 // on the partial ordering of those candidates to determine which
2405 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2407 if (tree cand
= most_constrained_function (candidates
))
2410 TREE_CHAIN (cand
) = NULL_TREE
;
2414 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2416 error ("template-id %qD for %q+D does not match any template "
2417 "declaration", template_id
, decl
);
2418 if (header_count
&& header_count
!= template_count
+ 1)
2419 inform (input_location
, "saw %d %<template<>%>, need %d for "
2420 "specializing a member function template",
2421 header_count
, template_count
+ 1);
2423 print_candidates (orig_fns
);
2424 return error_mark_node
;
2426 else if ((templates
&& TREE_CHAIN (templates
))
2427 || (candidates
&& TREE_CHAIN (candidates
))
2428 || (templates
&& candidates
))
2430 error ("ambiguous template specialization %qD for %q+D",
2432 candidates
= chainon (candidates
, templates
);
2433 print_candidates (candidates
);
2434 return error_mark_node
;
2437 /* We have one, and exactly one, match. */
2440 tree fn
= TREE_VALUE (candidates
);
2441 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2443 /* Propagate the candidate's constraints to the declaration. */
2444 set_constraints (decl
, get_constraints (fn
));
2446 /* DECL is a re-declaration or partial instantiation of a template
2448 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2450 /* It was a specialization of an ordinary member function in a
2452 return DECL_TI_TEMPLATE (fn
);
2455 /* It was a specialization of a template. */
2456 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2457 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2459 *targs_out
= copy_node (targs
);
2460 SET_TMPL_ARGS_LEVEL (*targs_out
,
2461 TMPL_ARGS_DEPTH (*targs_out
),
2462 TREE_PURPOSE (templates
));
2465 *targs_out
= TREE_PURPOSE (templates
);
2466 return TREE_VALUE (templates
);
2469 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2470 but with the default argument values filled in from those in the
2474 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2477 tree new_spec_types
;
2482 if (spec_types
== void_list_node
)
2483 return void_list_node
;
2485 /* Substitute into the rest of the list. */
2487 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2488 TREE_CHAIN (tmpl_types
));
2490 /* Add the default argument for this parameter. */
2491 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2492 TREE_VALUE (spec_types
),
2496 /* DECL is an explicit specialization. Replicate default arguments
2497 from the template it specializes. (That way, code like:
2499 template <class T> void f(T = 3);
2500 template <> void f(double);
2503 works, as required.) An alternative approach would be to look up
2504 the correct default arguments at the call-site, but this approach
2505 is consistent with how implicit instantiations are handled. */
2508 copy_default_args_to_explicit_spec (tree decl
)
2513 tree new_spec_types
;
2517 tree object_type
= NULL_TREE
;
2518 tree in_charge
= NULL_TREE
;
2519 tree vtt
= NULL_TREE
;
2521 /* See if there's anything we need to do. */
2522 tmpl
= DECL_TI_TEMPLATE (decl
);
2523 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2524 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2525 if (TREE_PURPOSE (t
))
2530 old_type
= TREE_TYPE (decl
);
2531 spec_types
= TYPE_ARG_TYPES (old_type
);
2533 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2535 /* Remove the this pointer, but remember the object's type for
2537 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2538 spec_types
= TREE_CHAIN (spec_types
);
2539 tmpl_types
= TREE_CHAIN (tmpl_types
);
2541 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2543 /* DECL may contain more parameters than TMPL due to the extra
2544 in-charge parameter in constructors and destructors. */
2545 in_charge
= spec_types
;
2546 spec_types
= TREE_CHAIN (spec_types
);
2548 if (DECL_HAS_VTT_PARM_P (decl
))
2551 spec_types
= TREE_CHAIN (spec_types
);
2555 /* Compute the merged default arguments. */
2557 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2559 /* Compute the new FUNCTION_TYPE. */
2563 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2568 /* Put the in-charge parameter back. */
2569 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2570 TREE_VALUE (in_charge
),
2573 new_type
= build_method_type_directly (object_type
,
2574 TREE_TYPE (old_type
),
2578 new_type
= build_function_type (TREE_TYPE (old_type
),
2580 new_type
= cp_build_type_attribute_variant (new_type
,
2581 TYPE_ATTRIBUTES (old_type
));
2582 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2584 TREE_TYPE (decl
) = new_type
;
2587 /* Return the number of template headers we expect to see for a definition
2588 or specialization of CTYPE or one of its non-template members. */
2591 num_template_headers_for_class (tree ctype
)
2593 int num_templates
= 0;
2595 while (ctype
&& CLASS_TYPE_P (ctype
))
2597 /* You're supposed to have one `template <...>' for every
2598 template class, but you don't need one for a full
2599 specialization. For example:
2601 template <class T> struct S{};
2602 template <> struct S<int> { void f(); };
2603 void S<int>::f () {}
2605 is correct; there shouldn't be a `template <>' for the
2606 definition of `S<int>::f'. */
2607 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2608 /* If CTYPE does not have template information of any
2609 kind, then it is not a template, nor is it nested
2610 within a template. */
2612 if (explicit_class_specialization_p (ctype
))
2614 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2617 ctype
= TYPE_CONTEXT (ctype
);
2620 return num_templates
;
2623 /* Do a simple sanity check on the template headers that precede the
2624 variable declaration DECL. */
2627 check_template_variable (tree decl
)
2629 tree ctx
= CP_DECL_CONTEXT (decl
);
2630 int wanted
= num_template_headers_for_class (ctx
);
2631 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2632 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2634 if (cxx_dialect
< cxx14
)
2635 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2636 "variable templates only available with "
2637 "%<-std=c++14%> or %<-std=gnu++14%>");
2639 // Namespace-scope variable templates should have a template header.
2642 if (template_header_count
> wanted
)
2644 auto_diagnostic_group d
;
2645 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2646 "too many template headers for %qD "
2649 if (warned
&& CLASS_TYPE_P (ctx
)
2650 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2651 inform (DECL_SOURCE_LOCATION (decl
),
2652 "members of an explicitly specialized class are defined "
2653 "without a template header");
2657 /* An explicit specialization whose declarator-id or class-head-name is not
2658 qualified shall be declared in the nearest enclosing namespace of the
2659 template, or, if the namespace is inline (7.3.1), any namespace from its
2660 enclosing namespace set.
2662 If the name declared in the explicit instantiation is an unqualified name,
2663 the explicit instantiation shall appear in the namespace where its template
2664 is declared or, if that namespace is inline (7.3.1), any namespace from its
2665 enclosing namespace set. */
2668 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2670 tree tmpl
= most_general_template (t
);
2671 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2672 && !is_nested_namespace (current_namespace
,
2673 CP_DECL_CONTEXT (tmpl
), true))
2675 if (processing_specialization
)
2676 permerror (loc
, "explicit specialization of %qD outside its "
2677 "namespace must use a nested-name-specifier", tmpl
);
2678 else if (processing_explicit_instantiation
2679 && cxx_dialect
>= cxx11
)
2680 /* This was allowed in C++98, so only pedwarn. */
2681 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2682 "outside its namespace must use a nested-name-"
2687 /* Warn for a template specialization SPEC that is missing some of a set
2688 of function or type attributes that the template TEMPL is declared with.
2689 ATTRLIST is a list of additional attributes that SPEC should be taken
2690 to ultimately be declared with. */
2693 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2695 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2696 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2698 /* Avoid warning if the difference between the primary and
2699 the specialization is not in one of the attributes below. */
2700 const char* const blacklist
[] = {
2701 "alloc_align", "alloc_size", "assume_aligned", "format",
2702 "format_arg", "malloc", "nonnull", NULL
2705 /* Put together a list of the black listed attributes that the primary
2706 template is declared with that the specialization is not, in case
2707 it's not apparent from the most recent declaration of the primary. */
2709 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2715 auto_diagnostic_group d
;
2716 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2717 "explicit specialization %q#D may be missing attributes",
2719 inform (DECL_SOURCE_LOCATION (tmpl
),
2721 ? G_("missing primary template attributes %s")
2722 : G_("missing primary template attribute %s"),
2723 pp_formatted_text (&str
));
2726 /* Check to see if the function just declared, as indicated in
2727 DECLARATOR, and in DECL, is a specialization of a function
2728 template. We may also discover that the declaration is an explicit
2729 instantiation at this point.
2731 Returns DECL, or an equivalent declaration that should be used
2732 instead if all goes well. Issues an error message if something is
2733 amiss. Returns error_mark_node if the error is not easily
2736 FLAGS is a bitmask consisting of the following flags:
2738 2: The function has a definition.
2739 4: The function is a friend.
2741 The TEMPLATE_COUNT is the number of references to qualifying
2742 template classes that appeared in the name of the function. For
2745 template <class T> struct S { void f(); };
2748 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2749 classes are not counted in the TEMPLATE_COUNT, so that in
2751 template <class T> struct S {};
2752 template <> struct S<int> { void f(); }
2753 template <> void S<int>::f();
2755 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2756 invalid; there should be no template <>.)
2758 If the function is a specialization, it is marked as such via
2759 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2760 is set up correctly, and it is added to the list of specializations
2761 for that template. */
2764 check_explicit_specialization (tree declarator
,
2770 int have_def
= flags
& 2;
2771 int is_friend
= flags
& 4;
2772 bool is_concept
= flags
& 8;
2773 int specialization
= 0;
2774 int explicit_instantiation
= 0;
2775 int member_specialization
= 0;
2776 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2777 tree dname
= DECL_NAME (decl
);
2782 if (!processing_specialization
)
2785 tsk
= tsk_excessive_parms
;
2788 tsk
= current_tmpl_spec_kind (template_count
);
2793 if (processing_specialization
&& !VAR_P (decl
))
2796 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2798 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2801 /* This could be something like:
2803 template <class T> void f(T);
2804 class S { friend void f<>(int); } */
2808 /* This case handles bogus declarations like template <>
2809 template <class T> void f<int>(); */
2811 error_at (cp_expr_loc_or_input_loc (declarator
),
2812 "template-id %qE in declaration of primary template",
2819 case tsk_invalid_member_spec
:
2820 /* The error has already been reported in
2821 check_specialization_scope. */
2822 return error_mark_node
;
2824 case tsk_invalid_expl_inst
:
2825 error ("template parameter list used in explicit instantiation");
2831 error ("definition provided for explicit instantiation");
2833 explicit_instantiation
= 1;
2836 case tsk_excessive_parms
:
2837 case tsk_insufficient_parms
:
2838 if (tsk
== tsk_excessive_parms
)
2839 error ("too many template parameter lists in declaration of %qD",
2841 else if (template_header_count
)
2842 error("too few template parameter lists in declaration of %qD", decl
);
2844 error("explicit specialization of %qD must be introduced by "
2845 "%<template <>%>", decl
);
2850 error ("explicit specialization declared %<concept%>");
2852 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2853 /* In cases like template<> constexpr bool v = true;
2854 We'll give an error in check_template_variable. */
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2859 member_specialization
= 1;
2865 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2867 /* This case handles bogus declarations like template <>
2868 template <class T> void f<int>(); */
2870 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2871 error_at (cp_expr_loc_or_input_loc (declarator
),
2872 "template-id %qE in declaration of primary template",
2874 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2876 /* Partial specialization of variable template. */
2877 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2881 else if (cxx_dialect
< cxx14
)
2882 error_at (cp_expr_loc_or_input_loc (declarator
),
2883 "non-type partial specialization %qE "
2884 "is not allowed", declarator
);
2886 error_at (cp_expr_loc_or_input_loc (declarator
),
2887 "non-class, non-variable partial specialization %qE "
2888 "is not allowed", declarator
);
2893 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2894 /* This is a specialization of a member template, without
2895 specialization the containing class. Something like:
2897 template <class T> struct S {
2898 template <class U> void f (U);
2900 template <> template <class U> void S<int>::f(U) {}
2902 That's a specialization -- but of the entire template. */
2910 if ((specialization
|| member_specialization
)
2911 /* This doesn't apply to variable templates. */
2912 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2914 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2915 for (; t
; t
= TREE_CHAIN (t
))
2916 if (TREE_PURPOSE (t
))
2918 permerror (input_location
,
2919 "default argument specified in explicit specialization");
2924 if (specialization
|| member_specialization
|| explicit_instantiation
)
2926 tree tmpl
= NULL_TREE
;
2927 tree targs
= NULL_TREE
;
2928 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2930 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2931 if (!was_template_id
)
2935 gcc_assert (identifier_p (declarator
));
2940 /* If there is no class context, the explicit instantiation
2941 must be at namespace scope. */
2942 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2944 /* Find the namespace binding, using the declaration
2946 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2948 if (fns
== error_mark_node
)
2949 /* If lookup fails, look for a friend declaration so we can
2950 give a better diagnostic. */
2951 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2952 /*type*/false, /*complain*/true,
2955 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2957 error ("%qD is not a template function", dname
);
2958 fns
= error_mark_node
;
2962 declarator
= lookup_template_function (fns
, NULL_TREE
);
2965 if (declarator
== error_mark_node
)
2966 return error_mark_node
;
2968 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2970 if (!explicit_instantiation
)
2971 /* A specialization in class scope. This is invalid,
2972 but the error will already have been flagged by
2973 check_specialization_scope. */
2974 return error_mark_node
;
2977 /* It's not valid to write an explicit instantiation in
2980 class C { template void f(); }
2982 This case is caught by the parser. However, on
2985 template class C { void f(); };
2987 (which is invalid) we can get here. The error will be
2994 else if (ctype
!= NULL_TREE
2995 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2997 // We'll match variable templates in start_decl.
3001 /* Find the list of functions in ctype that have the same
3002 name as the declared function. */
3003 tree name
= TREE_OPERAND (declarator
, 0);
3005 if (constructor_name_p (name
, ctype
))
3007 if (DECL_CONSTRUCTOR_P (decl
)
3008 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3009 : !CLASSTYPE_DESTRUCTOR (ctype
))
3011 /* From [temp.expl.spec]:
3013 If such an explicit specialization for the member
3014 of a class template names an implicitly-declared
3015 special member function (clause _special_), the
3016 program is ill-formed.
3018 Similar language is found in [temp.explicit]. */
3019 error ("specialization of implicitly-declared special member function");
3020 return error_mark_node
;
3023 name
= DECL_NAME (decl
);
3026 /* For a type-conversion operator, We might be looking for
3027 `operator int' which will be a specialization of
3028 `operator T'. Grab all the conversion operators, and
3029 then select from them. */
3030 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3031 ? conv_op_identifier
: name
);
3033 if (fns
== NULL_TREE
)
3035 error ("no member function %qD declared in %qT", name
, ctype
);
3036 return error_mark_node
;
3039 TREE_OPERAND (declarator
, 0) = fns
;
3042 /* Figure out what exactly is being specialized at this point.
3043 Note that for an explicit instantiation, even one for a
3044 member function, we cannot tell a priori whether the
3045 instantiation is for a member template, or just a member
3046 function of a template class. Even if a member template is
3047 being instantiated, the member template arguments may be
3048 elided if they can be deduced from the rest of the
3050 tmpl
= determine_specialization (declarator
, decl
,
3052 member_specialization
,
3056 if (!tmpl
|| tmpl
== error_mark_node
)
3057 /* We couldn't figure out what this declaration was
3059 return error_mark_node
;
3062 if (TREE_CODE (decl
) == FUNCTION_DECL
3063 && DECL_HIDDEN_FRIEND_P (tmpl
))
3065 auto_diagnostic_group d
;
3066 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3067 "friend declaration %qD is not visible to "
3068 "explicit specialization", tmpl
))
3069 inform (DECL_SOURCE_LOCATION (tmpl
),
3070 "friend declaration here");
3072 else if (!ctype
&& !is_friend
3073 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3074 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3076 tree gen_tmpl
= most_general_template (tmpl
);
3078 if (explicit_instantiation
)
3080 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3081 is done by do_decl_instantiation later. */
3083 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3084 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3086 if (arg_depth
> parm_depth
)
3088 /* If TMPL is not the most general template (for
3089 example, if TMPL is a friend template that is
3090 injected into namespace scope), then there will
3091 be too many levels of TARGS. Remove some of them
3096 new_targs
= make_tree_vec (parm_depth
);
3097 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3098 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3099 = TREE_VEC_ELT (targs
, i
);
3103 return instantiate_template (tmpl
, targs
, tf_error
);
3106 /* If we thought that the DECL was a member function, but it
3107 turns out to be specializing a static member function,
3108 make DECL a static member function as well. */
3109 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3110 && DECL_STATIC_FUNCTION_P (tmpl
)
3111 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3112 revert_static_member_fn (decl
);
3114 /* If this is a specialization of a member template of a
3115 template class, we want to return the TEMPLATE_DECL, not
3116 the specialization of it. */
3117 if (tsk
== tsk_template
&& !was_template_id
)
3119 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3120 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3121 DECL_INITIAL (result
) = NULL_TREE
;
3125 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3126 DECL_SOURCE_LOCATION (result
)
3127 = DECL_SOURCE_LOCATION (decl
);
3128 /* We want to use the argument list specified in the
3129 definition, not in the original declaration. */
3130 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3131 for (parm
= DECL_ARGUMENTS (result
); parm
;
3132 parm
= DECL_CHAIN (parm
))
3133 DECL_CONTEXT (parm
) = result
;
3135 return register_specialization (tmpl
, gen_tmpl
, targs
,
3139 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3140 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3142 if (was_template_id
)
3143 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3145 /* Inherit default function arguments from the template
3146 DECL is specializing. */
3147 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3148 copy_default_args_to_explicit_spec (decl
);
3150 /* This specialization has the same protection as the
3151 template it specializes. */
3152 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3153 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3155 /* 7.1.1-1 [dcl.stc]
3157 A storage-class-specifier shall not be specified in an
3158 explicit specialization...
3160 The parser rejects these, so unless action is taken here,
3161 explicit function specializations will always appear with
3164 The action recommended by the C++ CWG in response to C++
3165 defect report 605 is to make the storage class and linkage
3166 of the explicit specialization match the templated function:
3168 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3170 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3172 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3173 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3175 /* A concept cannot be specialized. */
3176 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3178 error ("explicit specialization of function concept %qD",
3180 return error_mark_node
;
3183 /* This specialization has the same linkage and visibility as
3184 the function template it specializes. */
3185 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3186 if (! TREE_PUBLIC (decl
))
3188 DECL_INTERFACE_KNOWN (decl
) = 1;
3189 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3191 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3192 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3194 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3195 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3199 /* If DECL is a friend declaration, declared using an
3200 unqualified name, the namespace associated with DECL may
3201 have been set incorrectly. For example, in:
3203 template <typename T> void f(T);
3205 struct S { friend void f<int>(int); }
3208 we will have set the DECL_CONTEXT for the friend
3209 declaration to N, rather than to the global namespace. */
3210 if (DECL_NAMESPACE_SCOPE_P (decl
))
3211 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3213 if (is_friend
&& !have_def
)
3214 /* This is not really a declaration of a specialization.
3215 It's just the name of an instantiation. But, it's not
3216 a request for an instantiation, either. */
3217 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3218 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3219 /* A specialization is not necessarily COMDAT. */
3220 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3221 && DECL_DECLARED_INLINE_P (decl
));
3222 else if (VAR_P (decl
))
3223 DECL_COMDAT (decl
) = false;
3225 /* If this is a full specialization, register it so that we can find
3226 it again. Partial specializations will be registered in
3227 process_partial_specialization. */
3228 if (!processing_template_decl
)
3230 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3232 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3237 /* A 'structor should already have clones. */
3238 gcc_assert (decl
== error_mark_node
3239 || variable_template_p (tmpl
)
3240 || !(DECL_CONSTRUCTOR_P (decl
)
3241 || DECL_DESTRUCTOR_P (decl
))
3242 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3249 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3250 parameters. These are represented in the same format used for
3251 DECL_TEMPLATE_PARMS. */
3254 comp_template_parms (const_tree parms1
, const_tree parms2
)
3259 if (parms1
== parms2
)
3262 for (p1
= parms1
, p2
= parms2
;
3263 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3264 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3266 tree t1
= TREE_VALUE (p1
);
3267 tree t2
= TREE_VALUE (p2
);
3270 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3271 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3273 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3276 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3278 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3279 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3281 /* If either of the template parameters are invalid, assume
3282 they match for the sake of error recovery. */
3283 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3286 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3289 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3290 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3291 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3293 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3298 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3299 /* One set of parameters has more parameters lists than the
3306 /* Determine whether PARM is a parameter pack. */
3309 template_parameter_pack_p (const_tree parm
)
3311 /* Determine if we have a non-type template parameter pack. */
3312 if (TREE_CODE (parm
) == PARM_DECL
)
3313 return (DECL_TEMPLATE_PARM_P (parm
)
3314 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3315 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3316 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3318 /* If this is a list of template parameters, we could get a
3319 TYPE_DECL or a TEMPLATE_DECL. */
3320 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3321 parm
= TREE_TYPE (parm
);
3323 /* Otherwise it must be a type template parameter. */
3324 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3325 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3326 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3329 /* Determine if T is a function parameter pack. */
3332 function_parameter_pack_p (const_tree t
)
3334 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3335 return DECL_PACK_P (t
);
3339 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3340 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3343 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3345 if (! primary_func_tmpl_inst
3346 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3347 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3350 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3353 /* Return true iff the function parameter PARAM_DECL was expanded
3354 from the function parameter pack PACK. */
3357 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3359 if (DECL_ARTIFICIAL (param_decl
)
3360 || !function_parameter_pack_p (pack
))
3363 /* The parameter pack and its pack arguments have the same
3365 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3368 /* Determine whether ARGS describes a variadic template args list,
3369 i.e., one that is terminated by a template argument pack. */
3372 template_args_variadic_p (tree args
)
3377 if (args
== NULL_TREE
)
3380 args
= INNERMOST_TEMPLATE_ARGS (args
);
3381 nargs
= TREE_VEC_LENGTH (args
);
3386 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3388 return ARGUMENT_PACK_P (last_parm
);
3391 /* Generate a new name for the parameter pack name NAME (an
3392 IDENTIFIER_NODE) that incorporates its */
3395 make_ith_pack_parameter_name (tree name
, int i
)
3397 /* Munge the name to include the parameter index. */
3398 #define NUMBUF_LEN 128
3399 char numbuf
[NUMBUF_LEN
];
3403 if (name
== NULL_TREE
)
3405 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3406 newname_len
= IDENTIFIER_LENGTH (name
)
3407 + strlen (numbuf
) + 2;
3408 newname
= (char*)alloca (newname_len
);
3409 snprintf (newname
, newname_len
,
3410 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3411 return get_identifier (newname
);
3414 /* Return true if T is a primary function, class or alias template
3415 specialization, not including the template pattern. */
3418 primary_template_specialization_p (const_tree t
)
3423 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3424 return (DECL_LANG_SPECIFIC (t
)
3425 && DECL_USE_TEMPLATE (t
)
3426 && DECL_TEMPLATE_INFO (t
)
3427 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3428 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3429 return (CLASSTYPE_TEMPLATE_INFO (t
)
3430 && CLASSTYPE_USE_TEMPLATE (t
)
3431 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3432 else if (alias_template_specialization_p (t
))
3437 /* Return true if PARM is a template template parameter. */
3440 template_template_parameter_p (const_tree parm
)
3442 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3445 /* Return true iff PARM is a DECL representing a type template
3449 template_type_parameter_p (const_tree parm
)
3452 && (TREE_CODE (parm
) == TYPE_DECL
3453 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3454 && DECL_TEMPLATE_PARM_P (parm
));
3457 /* Return the template parameters of T if T is a
3458 primary template instantiation, NULL otherwise. */
3461 get_primary_template_innermost_parameters (const_tree t
)
3463 tree parms
= NULL
, template_info
= NULL
;
3465 if ((template_info
= get_template_info (t
))
3466 && primary_template_specialization_p (t
))
3467 parms
= INNERMOST_TEMPLATE_PARMS
3468 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3473 /* Return the template parameters of the LEVELth level from the full list
3474 of template parameters PARMS. */
3477 get_template_parms_at_level (tree parms
, int level
)
3481 || TREE_CODE (parms
) != TREE_LIST
3482 || level
> TMPL_PARMS_DEPTH (parms
))
3485 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3486 if (TMPL_PARMS_DEPTH (p
) == level
)
3492 /* Returns the template arguments of T if T is a template instantiation,
3496 get_template_innermost_arguments (const_tree t
)
3498 tree args
= NULL
, template_info
= NULL
;
3500 if ((template_info
= get_template_info (t
))
3501 && TI_ARGS (template_info
))
3502 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3507 /* Return the argument pack elements of T if T is a template argument pack,
3511 get_template_argument_pack_elems (const_tree t
)
3513 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3514 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3517 return ARGUMENT_PACK_ARGS (t
);
3520 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3521 ARGUMENT_PACK_SELECT represents. */
3524 argument_pack_select_arg (tree t
)
3526 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3527 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3529 /* If the selected argument is an expansion E, that most likely means we were
3530 called from gen_elem_of_pack_expansion_instantiation during the
3531 substituting of an argument pack (of which the Ith element is a pack
3532 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3533 In this case, the Ith element resulting from this substituting is going to
3534 be a pack expansion, which pattern is the pattern of E. Let's return the
3535 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3536 resulting pack expansion from it. */
3537 if (PACK_EXPANSION_P (arg
))
3539 /* Make sure we aren't throwing away arg info. */
3540 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3541 arg
= PACK_EXPANSION_PATTERN (arg
);
3548 /* True iff FN is a function representing a built-in variadic parameter
3552 builtin_pack_fn_p (tree fn
)
3555 || TREE_CODE (fn
) != FUNCTION_DECL
3556 || !DECL_IS_BUILTIN (fn
))
3559 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3565 /* True iff CALL is a call to a function representing a built-in variadic
3569 builtin_pack_call_p (tree call
)
3571 if (TREE_CODE (call
) != CALL_EXPR
)
3573 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3576 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3579 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3582 tree ohi
= CALL_EXPR_ARG (call
, 0);
3583 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3584 false/*fn*/, true/*int_cst*/);
3586 if (value_dependent_expression_p (hi
))
3590 call
= copy_node (call
);
3591 CALL_EXPR_ARG (call
, 0) = hi
;
3593 tree ex
= make_pack_expansion (call
, complain
);
3594 tree vec
= make_tree_vec (1);
3595 TREE_VEC_ELT (vec
, 0) = ex
;
3600 hi
= cxx_constant_value (hi
);
3601 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3603 /* Calculate the largest value of len that won't make the size of the vec
3604 overflow an int. The compiler will exceed resource limits long before
3605 this, but it seems a decent place to diagnose. */
3606 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3608 if (len
< 0 || len
> max
)
3610 if ((complain
& tf_error
)
3611 && hi
!= error_mark_node
)
3612 error ("argument to %<__integer_pack%> must be between 0 and %d",
3614 return error_mark_node
;
3617 tree vec
= make_tree_vec (len
);
3619 for (int i
= 0; i
< len
; ++i
)
3620 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3626 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3630 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3633 if (!builtin_pack_call_p (call
))
3636 tree fn
= CALL_EXPR_FN (call
);
3638 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3639 return expand_integer_pack (call
, args
, complain
, in_decl
);
3644 /* Structure used to track the progress of find_parameter_packs_r. */
3645 struct find_parameter_pack_data
3647 /* TREE_LIST that will contain all of the parameter packs found by
3649 tree
* parameter_packs
;
3651 /* Set of AST nodes that have been visited by the traversal. */
3652 hash_set
<tree
> *visited
;
3654 /* True iff we're making a type pack expansion. */
3655 bool type_pack_expansion_p
;
3658 /* Identifies all of the argument packs that occur in a template
3659 argument and appends them to the TREE_LIST inside DATA, which is a
3660 find_parameter_pack_data structure. This is a subroutine of
3661 make_pack_expansion and uses_parameter_packs. */
3663 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3666 struct find_parameter_pack_data
* ppd
=
3667 (struct find_parameter_pack_data
*)data
;
3668 bool parameter_pack_p
= false;
3670 /* Handle type aliases/typedefs. */
3671 if (TYPE_ALIAS_P (t
))
3673 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3674 cp_walk_tree (&TI_ARGS (tinfo
),
3675 &find_parameter_packs_r
,
3681 /* Identify whether this is a parameter pack or not. */
3682 switch (TREE_CODE (t
))
3684 case TEMPLATE_PARM_INDEX
:
3685 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3686 parameter_pack_p
= true;
3689 case TEMPLATE_TYPE_PARM
:
3690 t
= TYPE_MAIN_VARIANT (t
);
3692 case TEMPLATE_TEMPLATE_PARM
:
3693 /* If the placeholder appears in the decl-specifier-seq of a function
3694 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3695 is a pack expansion, the invented template parameter is a template
3697 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3698 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3699 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3700 parameter_pack_p
= true;
3705 if (DECL_PACK_P (t
))
3707 /* We don't want to walk into the type of a PARM_DECL,
3708 because we don't want to see the type parameter pack. */
3710 parameter_pack_p
= true;
3715 if (DECL_PACK_P (t
))
3717 /* We don't want to walk into the type of a variadic capture proxy,
3718 because we don't want to see the type parameter pack. */
3720 parameter_pack_p
= true;
3722 else if (variable_template_specialization_p (t
))
3724 cp_walk_tree (&DECL_TI_ARGS (t
),
3725 find_parameter_packs_r
,
3732 if (builtin_pack_call_p (t
))
3733 parameter_pack_p
= true;
3737 parameter_pack_p
= true;
3740 /* Not a parameter pack. */
3744 if (parameter_pack_p
)
3746 /* Add this parameter pack to the list. */
3747 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3751 cp_walk_tree (&TYPE_CONTEXT (t
),
3752 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3754 /* This switch statement will return immediately if we don't find a
3756 switch (TREE_CODE (t
))
3758 case TEMPLATE_PARM_INDEX
:
3761 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3762 /* Check the template itself. */
3763 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3764 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3765 /* Check the template arguments. */
3766 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3771 case TEMPLATE_TYPE_PARM
:
3772 case TEMPLATE_TEMPLATE_PARM
:
3779 /* Ignore the declaration of a capture proxy for a parameter pack. */
3780 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3785 if (TYPE_PTRMEMFUNC_P (t
))
3791 if (TYPE_TEMPLATE_INFO (t
))
3792 cp_walk_tree (&TYPE_TI_ARGS (t
),
3793 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3799 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3804 cp_walk_tree (&TREE_TYPE (t
),
3805 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3809 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3814 case TYPE_PACK_EXPANSION
:
3815 case EXPR_PACK_EXPANSION
:
3820 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3825 case IDENTIFIER_NODE
:
3826 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3833 /* Look at explicit captures. */
3834 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3835 cap
; cap
= TREE_CHAIN (cap
))
3836 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3838 /* Since we defer implicit capture, look in the parms and body. */
3839 tree fn
= lambda_function (t
);
3840 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3842 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3850 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3851 type_pack_expansion_p to false so that any placeholders
3852 within the expression don't get marked as parameter packs. */
3853 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3854 ppd
->type_pack_expansion_p
= false;
3855 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3857 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3863 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3865 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3867 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
3869 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3880 /* Determines if the expression or type T uses any parameter packs. */
3882 uses_parameter_packs (tree t
)
3884 tree parameter_packs
= NULL_TREE
;
3885 struct find_parameter_pack_data ppd
;
3886 ppd
.parameter_packs
= ¶meter_packs
;
3887 ppd
.visited
= new hash_set
<tree
>;
3888 ppd
.type_pack_expansion_p
= false;
3889 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3891 return parameter_packs
;
3894 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3895 representation a base-class initializer into a parameter pack
3896 expansion. If all goes well, the resulting node will be an
3897 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3900 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3903 tree parameter_packs
= NULL_TREE
;
3904 bool for_types
= false;
3905 struct find_parameter_pack_data ppd
;
3907 if (!arg
|| arg
== error_mark_node
)
3910 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3912 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3913 class initializer. In this case, the TREE_PURPOSE will be a
3914 _TYPE node (representing the base class expansion we're
3915 initializing) and the TREE_VALUE will be a TREE_LIST
3916 containing the initialization arguments.
3918 The resulting expansion looks somewhat different from most
3919 expansions. Rather than returning just one _EXPANSION, we
3920 return a TREE_LIST whose TREE_PURPOSE is a
3921 TYPE_PACK_EXPANSION containing the bases that will be
3922 initialized. The TREE_VALUE will be identical to the
3923 original TREE_VALUE, which is a list of arguments that will
3924 be passed to each base. We do not introduce any new pack
3925 expansion nodes into the TREE_VALUE (although it is possible
3926 that some already exist), because the TREE_PURPOSE and
3927 TREE_VALUE all need to be expanded together with the same
3928 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3929 resulting TREE_PURPOSE will mention the parameter packs in
3930 both the bases and the arguments to the bases. */
3933 tree parameter_packs
= NULL_TREE
;
3935 /* Determine which parameter packs will be used by the base
3937 ppd
.visited
= new hash_set
<tree
>;
3938 ppd
.parameter_packs
= ¶meter_packs
;
3939 ppd
.type_pack_expansion_p
= false;
3940 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3941 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3944 if (parameter_packs
== NULL_TREE
)
3946 if (complain
& tf_error
)
3947 error ("base initializer expansion %qT contains no parameter packs",
3950 return error_mark_node
;
3953 if (TREE_VALUE (arg
) != void_type_node
)
3955 /* Collect the sets of parameter packs used in each of the
3956 initialization arguments. */
3957 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3959 /* Determine which parameter packs will be expanded in this
3961 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3968 /* Create the pack expansion type for the base type. */
3969 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3970 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3971 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3972 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3974 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3975 they will rarely be compared to anything. */
3976 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3978 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3981 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3984 /* Build the PACK_EXPANSION_* node. */
3986 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3987 : make_node (EXPR_PACK_EXPANSION
);
3988 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3989 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3991 /* Propagate type and const-expression information. */
3992 TREE_TYPE (result
) = TREE_TYPE (arg
);
3993 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3994 /* Mark this read now, since the expansion might be length 0. */
3995 mark_exp_read (arg
);
3998 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3999 they will rarely be compared to anything. */
4000 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4002 /* Determine which parameter packs will be expanded. */
4003 ppd
.parameter_packs
= ¶meter_packs
;
4004 ppd
.visited
= new hash_set
<tree
>;
4005 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4006 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4009 /* Make sure we found some parameter packs. */
4010 if (parameter_packs
== NULL_TREE
)
4012 if (complain
& tf_error
)
4015 error ("expansion pattern %qT contains no parameter packs", arg
);
4017 error ("expansion pattern %qE contains no parameter packs", arg
);
4019 return error_mark_node
;
4021 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4023 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4028 /* Checks T for any "bare" parameter packs, which have not yet been
4029 expanded, and issues an error if any are found. This operation can
4030 only be done on full expressions or types (e.g., an expression
4031 statement, "if" condition, etc.), because we could have expressions like:
4033 foo(f(g(h(args)))...)
4035 where "args" is a parameter pack. check_for_bare_parameter_packs
4036 should not be called for the subexpressions args, h(args),
4037 g(h(args)), or f(g(h(args))), because we would produce erroneous
4040 Returns TRUE and emits an error if there were bare parameter packs,
4041 returns FALSE otherwise. */
4043 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4045 tree parameter_packs
= NULL_TREE
;
4046 struct find_parameter_pack_data ppd
;
4048 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4051 /* A lambda might use a parameter pack from the containing context. */
4052 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4053 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4056 if (TREE_CODE (t
) == TYPE_DECL
)
4059 ppd
.parameter_packs
= ¶meter_packs
;
4060 ppd
.visited
= new hash_set
<tree
>;
4061 ppd
.type_pack_expansion_p
= false;
4062 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4065 if (parameter_packs
)
4067 if (loc
== UNKNOWN_LOCATION
)
4068 loc
= cp_expr_loc_or_input_loc (t
);
4069 error_at (loc
, "parameter packs not expanded with %<...%>:");
4070 while (parameter_packs
)
4072 tree pack
= TREE_VALUE (parameter_packs
);
4073 tree name
= NULL_TREE
;
4075 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4076 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4077 name
= TYPE_NAME (pack
);
4078 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4079 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4080 else if (TREE_CODE (pack
) == CALL_EXPR
)
4081 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4083 name
= DECL_NAME (pack
);
4086 inform (loc
, " %qD", name
);
4088 inform (loc
, " %s", "<anonymous>");
4090 parameter_packs
= TREE_CHAIN (parameter_packs
);
4099 /* Expand any parameter packs that occur in the template arguments in
4102 expand_template_argument_pack (tree args
)
4104 if (args
== error_mark_node
)
4105 return error_mark_node
;
4107 tree result_args
= NULL_TREE
;
4108 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4109 int num_result_args
= -1;
4110 int non_default_args_count
= -1;
4112 /* First, determine if we need to expand anything, and the number of
4113 slots we'll need. */
4114 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4116 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4117 if (arg
== NULL_TREE
)
4119 if (ARGUMENT_PACK_P (arg
))
4121 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4122 if (num_result_args
< 0)
4123 num_result_args
= in_arg
+ num_packed
;
4125 num_result_args
+= num_packed
;
4129 if (num_result_args
>= 0)
4134 /* If no expansion is necessary, we're done. */
4135 if (num_result_args
< 0)
4138 /* Expand arguments. */
4139 result_args
= make_tree_vec (num_result_args
);
4140 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4141 non_default_args_count
=
4142 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4143 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4145 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4146 if (ARGUMENT_PACK_P (arg
))
4148 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4149 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4150 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4151 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4152 if (non_default_args_count
> 0)
4153 non_default_args_count
+= num_packed
- 1;
4157 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4161 if (non_default_args_count
>= 0)
4162 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4166 /* Checks if DECL shadows a template parameter.
4168 [temp.local]: A template-parameter shall not be redeclared within its
4169 scope (including nested scopes).
4171 Emits an error and returns TRUE if the DECL shadows a parameter,
4172 returns FALSE otherwise. */
4175 check_template_shadow (tree decl
)
4179 /* If we're not in a template, we can't possibly shadow a template
4181 if (!current_template_parms
)
4184 /* Figure out what we're shadowing. */
4185 decl
= OVL_FIRST (decl
);
4186 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4188 /* If there's no previous binding for this name, we're not shadowing
4189 anything, let alone a template parameter. */
4193 /* If we're not shadowing a template parameter, we're done. Note
4194 that OLDDECL might be an OVERLOAD (or perhaps even an
4195 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4197 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4200 /* We check for decl != olddecl to avoid bogus errors for using a
4201 name inside a class. We check TPFI to avoid duplicate errors for
4202 inline member templates. */
4204 || (DECL_TEMPLATE_PARM_P (decl
)
4205 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4208 /* Don't complain about the injected class name, as we've already
4209 complained about the class itself. */
4210 if (DECL_SELF_REFERENCE_P (decl
))
4213 if (DECL_TEMPLATE_PARM_P (decl
))
4214 error ("declaration of template parameter %q+D shadows "
4215 "template parameter", decl
);
4217 error ("declaration of %q+#D shadows template parameter", decl
);
4218 inform (DECL_SOURCE_LOCATION (olddecl
),
4219 "template parameter %qD declared here", olddecl
);
4223 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4224 ORIG_LEVEL, DECL, and TYPE. */
4227 build_template_parm_index (int index
,
4233 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4234 TEMPLATE_PARM_IDX (t
) = index
;
4235 TEMPLATE_PARM_LEVEL (t
) = level
;
4236 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4237 TEMPLATE_PARM_DECL (t
) = decl
;
4238 TREE_TYPE (t
) = type
;
4239 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4240 TREE_READONLY (t
) = TREE_READONLY (decl
);
4245 /* Find the canonical type parameter for the given template type
4246 parameter. Returns the canonical type parameter, which may be TYPE
4247 if no such parameter existed. */
4250 canonical_type_parameter (tree type
)
4253 int idx
= TEMPLATE_TYPE_IDX (type
);
4254 if (!canonical_template_parms
)
4255 vec_alloc (canonical_template_parms
, idx
+ 1);
4257 if (canonical_template_parms
->length () <= (unsigned) idx
)
4258 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4260 list
= (*canonical_template_parms
)[idx
];
4261 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4262 list
= TREE_CHAIN (list
);
4265 return TREE_VALUE (list
);
4268 (*canonical_template_parms
)[idx
]
4269 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4274 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4275 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4276 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4277 new one is created. */
4280 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4281 tsubst_flags_t complain
)
4283 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4284 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4285 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4286 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4288 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4291 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4292 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4293 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4294 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4295 DECL_ARTIFICIAL (decl
) = 1;
4296 SET_DECL_TEMPLATE_PARM_P (decl
);
4298 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4299 TEMPLATE_PARM_LEVEL (index
) - levels
,
4300 TEMPLATE_PARM_ORIG_LEVEL (index
),
4302 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4303 TEMPLATE_PARM_PARAMETER_PACK (t
)
4304 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4306 /* Template template parameters need this. */
4307 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4309 DECL_TEMPLATE_RESULT (decl
)
4310 = build_decl (DECL_SOURCE_LOCATION (decl
),
4311 TYPE_DECL
, DECL_NAME (decl
), type
);
4312 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4313 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4314 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4318 return TEMPLATE_PARM_DESCENDANTS (index
);
4321 /* Process information from new template parameter PARM and append it
4322 to the LIST being built. This new parameter is a non-type
4323 parameter iff IS_NON_TYPE is true. This new parameter is a
4324 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4328 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4329 bool is_non_type
, bool is_parameter_pack
)
4334 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4335 tree defval
= TREE_PURPOSE (parm
);
4336 tree constr
= TREE_TYPE (parm
);
4340 tree p
= tree_last (list
);
4342 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4345 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4346 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4348 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4356 parm
= TREE_VALUE (parm
);
4358 SET_DECL_TEMPLATE_PARM_P (parm
);
4360 if (TREE_TYPE (parm
) != error_mark_node
)
4364 The top-level cv-qualifiers on the template-parameter are
4365 ignored when determining its type. */
4366 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4367 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4368 TREE_TYPE (parm
) = error_mark_node
;
4369 else if (uses_parameter_packs (TREE_TYPE (parm
))
4370 && !is_parameter_pack
4371 /* If we're in a nested template parameter list, the template
4372 template parameter could be a parameter pack. */
4373 && processing_template_parmlist
== 1)
4375 /* This template parameter is not a parameter pack, but it
4376 should be. Complain about "bare" parameter packs. */
4377 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4379 /* Recover by calling this a parameter pack. */
4380 is_parameter_pack
= true;
4384 /* A template parameter is not modifiable. */
4385 TREE_CONSTANT (parm
) = 1;
4386 TREE_READONLY (parm
) = 1;
4387 decl
= build_decl (parm_loc
,
4388 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4389 TREE_CONSTANT (decl
) = 1;
4390 TREE_READONLY (decl
) = 1;
4391 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4392 = build_template_parm_index (idx
, processing_template_decl
,
4393 processing_template_decl
,
4394 decl
, TREE_TYPE (parm
));
4396 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4397 = is_parameter_pack
;
4402 parm
= TREE_VALUE (TREE_VALUE (parm
));
4404 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4406 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4407 /* This is for distinguishing between real templates and template
4408 template parameters */
4409 TREE_TYPE (parm
) = t
;
4410 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4415 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4416 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4417 decl
= build_decl (parm_loc
,
4418 TYPE_DECL
, parm
, t
);
4421 TYPE_NAME (t
) = decl
;
4422 TYPE_STUB_DECL (t
) = decl
;
4424 TEMPLATE_TYPE_PARM_INDEX (t
)
4425 = build_template_parm_index (idx
, processing_template_decl
,
4426 processing_template_decl
,
4427 decl
, TREE_TYPE (parm
));
4428 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4429 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4431 DECL_ARTIFICIAL (decl
) = 1;
4432 SET_DECL_TEMPLATE_PARM_P (decl
);
4434 /* Build requirements for the type/template parameter.
4435 This must be done after SET_DECL_TEMPLATE_PARM_P or
4436 process_template_parm could fail. */
4437 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4439 decl
= pushdecl (decl
);
4443 /* Build the parameter node linking the parameter declaration,
4444 its default argument (if any), and its constraints (if any). */
4445 parm
= build_tree_list (defval
, parm
);
4446 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4448 return chainon (list
, parm
);
4451 /* The end of a template parameter list has been reached. Process the
4452 tree list into a parameter vector, converting each parameter into a more
4453 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4457 end_template_parm_list (tree parms
)
4461 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4463 /* Pop the dummy parameter level and add the real one. */
4464 current_template_parms
= TREE_CHAIN (current_template_parms
);
4466 current_template_parms
4467 = tree_cons (size_int (processing_template_decl
),
4468 saved_parmlist
, current_template_parms
);
4470 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4472 next
= TREE_CHAIN (parm
);
4473 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4474 TREE_CHAIN (parm
) = NULL_TREE
;
4477 --processing_template_parmlist
;
4479 return saved_parmlist
;
4482 // Explicitly indicate the end of the template parameter list. We assume
4483 // that the current template parameters have been constructed and/or
4484 // managed explicitly, as when creating new template template parameters
4485 // from a shorthand constraint.
4487 end_template_parm_list ()
4489 --processing_template_parmlist
;
4492 /* end_template_decl is called after a template declaration is seen. */
4495 end_template_decl (void)
4497 reset_specialization ();
4499 if (! processing_template_decl
)
4502 /* This matches the pushlevel in begin_template_parm_list. */
4505 --processing_template_decl
;
4506 current_template_parms
= TREE_CHAIN (current_template_parms
);
4509 /* Takes a TREE_LIST representing a template parameter and convert it
4510 into an argument suitable to be passed to the type substitution
4511 functions. Note that If the TREE_LIST contains an error_mark
4512 node, the returned argument is error_mark_node. */
4515 template_parm_to_arg (tree t
)
4519 || TREE_CODE (t
) != TREE_LIST
)
4522 if (error_operand_p (TREE_VALUE (t
)))
4523 return error_mark_node
;
4527 if (TREE_CODE (t
) == TYPE_DECL
4528 || TREE_CODE (t
) == TEMPLATE_DECL
)
4532 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4534 /* Turn this argument into a TYPE_ARGUMENT_PACK
4535 with a single element, which expands T. */
4536 tree vec
= make_tree_vec (1);
4538 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4540 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4542 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4543 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4548 t
= DECL_INITIAL (t
);
4550 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4552 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4553 with a single element, which expands T. */
4554 tree vec
= make_tree_vec (1);
4556 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4558 t
= convert_from_reference (t
);
4559 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4561 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4562 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4565 t
= convert_from_reference (t
);
4570 /* Given a single level of template parameters (a TREE_VEC), return it
4571 as a set of template arguments. */
4574 template_parms_level_to_args (tree parms
)
4576 tree a
= copy_node (parms
);
4577 TREE_TYPE (a
) = NULL_TREE
;
4578 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4579 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4582 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4587 /* Given a set of template parameters, return them as a set of template
4588 arguments. The template parameters are represented as a TREE_VEC, in
4589 the form documented in cp-tree.h for template arguments. */
4592 template_parms_to_args (tree parms
)
4595 tree args
= NULL_TREE
;
4596 int length
= TMPL_PARMS_DEPTH (parms
);
4599 /* If there is only one level of template parameters, we do not
4600 create a TREE_VEC of TREE_VECs. Instead, we return a single
4601 TREE_VEC containing the arguments. */
4603 args
= make_tree_vec (length
);
4605 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4607 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4610 TREE_VEC_ELT (args
, --l
) = a
;
4618 /* Within the declaration of a template, return the currently active
4619 template parameters as an argument TREE_VEC. */
4622 current_template_args (void)
4624 return template_parms_to_args (current_template_parms
);
4627 /* Update the declared TYPE by doing any lookups which were thought to be
4628 dependent, but are not now that we know the SCOPE of the declarator. */
4631 maybe_update_decl_type (tree orig_type
, tree scope
)
4633 tree type
= orig_type
;
4635 if (type
== NULL_TREE
)
4638 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4639 type
= TREE_TYPE (type
);
4641 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4642 && dependent_type_p (type
)
4643 /* Don't bother building up the args in this case. */
4644 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4646 /* tsubst in the args corresponding to the template parameters,
4647 including auto if present. Most things will be unchanged, but
4648 make_typename_type and tsubst_qualified_id will resolve
4649 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4650 tree args
= current_template_args ();
4651 tree auto_node
= type_uses_auto (type
);
4655 tree auto_vec
= make_tree_vec (1);
4656 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4657 args
= add_to_template_args (args
, auto_vec
);
4659 pushed
= push_scope (scope
);
4660 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4665 if (type
== error_mark_node
)
4668 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4670 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4673 type
= TYPE_NAME (type
);
4678 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4679 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4680 the new template is a member template. */
4683 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4685 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4686 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4687 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4688 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4689 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4690 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4695 struct template_parm_data
4697 /* The level of the template parameters we are currently
4701 /* The index of the specialization argument we are currently
4705 /* An array whose size is the number of template parameters. The
4706 elements are nonzero if the parameter has been used in any one
4707 of the arguments processed so far. */
4710 /* An array whose size is the number of template arguments. The
4711 elements are nonzero if the argument makes use of template
4712 parameters of this level. */
4713 int* arg_uses_template_parms
;
4716 /* Subroutine of push_template_decl used to see if each template
4717 parameter in a partial specialization is used in the explicit
4718 argument list. If T is of the LEVEL given in DATA (which is
4719 treated as a template_parm_data*), then DATA->PARMS is marked
4723 mark_template_parm (tree t
, void* data
)
4727 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4729 template_parm_level_and_index (t
, &level
, &idx
);
4731 if (level
== tpd
->level
)
4733 tpd
->parms
[idx
] = 1;
4734 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4737 /* In C++17 the type of a non-type argument is a deduced context. */
4738 if (cxx_dialect
>= cxx17
4739 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4740 for_each_template_parm (TREE_TYPE (t
),
4741 &mark_template_parm
,
4744 /*include_nondeduced_p=*/false);
4746 /* Return zero so that for_each_template_parm will continue the
4747 traversal of the tree; we want to mark *every* template parm. */
4751 /* Process the partial specialization DECL. */
4754 process_partial_specialization (tree decl
)
4756 tree type
= TREE_TYPE (decl
);
4757 tree tinfo
= get_template_info (decl
);
4758 tree maintmpl
= TI_TEMPLATE (tinfo
);
4759 tree specargs
= TI_ARGS (tinfo
);
4760 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4761 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4764 int nargs
= TREE_VEC_LENGTH (inner_args
);
4767 bool did_error_intro
= false;
4768 struct template_parm_data tpd
;
4769 struct template_parm_data tpd2
;
4771 gcc_assert (current_template_parms
);
4773 /* A concept cannot be specialized. */
4774 if (flag_concepts
&& variable_concept_p (maintmpl
))
4776 error ("specialization of variable concept %q#D", maintmpl
);
4777 return error_mark_node
;
4780 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4781 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4783 /* We check that each of the template parameters given in the
4784 partial specialization is used in the argument list to the
4785 specialization. For example:
4787 template <class T> struct S;
4788 template <class T> struct S<T*>;
4790 The second declaration is OK because `T*' uses the template
4791 parameter T, whereas
4793 template <class T> struct S<int>;
4795 is no good. Even trickier is:
4806 The S2<T> declaration is actually invalid; it is a
4807 full-specialization. Of course,
4810 struct S2<T (*)(U)>;
4812 or some such would have been OK. */
4813 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4814 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4815 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4817 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4818 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4819 for (i
= 0; i
< nargs
; ++i
)
4821 tpd
.current_arg
= i
;
4822 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4823 &mark_template_parm
,
4826 /*include_nondeduced_p=*/false);
4828 for (i
= 0; i
< ntparms
; ++i
)
4829 if (tpd
.parms
[i
] == 0)
4831 /* One of the template parms was not used in a deduced context in the
4833 if (!did_error_intro
)
4835 error ("template parameters not deducible in "
4836 "partial specialization:");
4837 did_error_intro
= true;
4840 inform (input_location
, " %qD",
4841 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4844 if (did_error_intro
)
4845 return error_mark_node
;
4847 /* [temp.class.spec]
4849 The argument list of the specialization shall not be identical to
4850 the implicit argument list of the primary template. */
4852 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4853 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4855 || !strictly_subsumes (current_template_constraints (),
4856 get_constraints (maintmpl
))))
4859 error ("partial specialization %q+D does not specialize "
4860 "any template arguments; to define the primary template, "
4861 "remove the template argument list", decl
);
4863 error ("partial specialization %q+D does not specialize any "
4864 "template arguments and is not more constrained than "
4865 "the primary template; to define the primary template, "
4866 "remove the template argument list", decl
);
4867 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4870 /* A partial specialization that replaces multiple parameters of the
4871 primary template with a pack expansion is less specialized for those
4873 if (nargs
< DECL_NTPARMS (maintmpl
))
4875 error ("partial specialization is not more specialized than the "
4876 "primary template because it replaces multiple parameters "
4877 "with a pack expansion");
4878 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4879 /* Avoid crash in process_partial_specialization. */
4883 /* If we aren't in a dependent class, we can actually try deduction. */
4884 else if (tpd
.level
== 1
4885 /* FIXME we should be able to handle a partial specialization of a
4886 partial instantiation, but currently we can't (c++/41727). */
4887 && TMPL_ARGS_DEPTH (specargs
) == 1
4888 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4890 auto_diagnostic_group d
;
4891 if (permerror (input_location
, "partial specialization %qD is not "
4892 "more specialized than", decl
))
4893 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4897 /* [temp.class.spec]
4899 A partially specialized non-type argument expression shall not
4900 involve template parameters of the partial specialization except
4901 when the argument expression is a simple identifier.
4903 The type of a template parameter corresponding to a specialized
4904 non-type argument shall not be dependent on a parameter of the
4907 Also, we verify that pack expansions only occur at the
4908 end of the argument list. */
4909 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4911 for (i
= 0; i
< nargs
; ++i
)
4913 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4914 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4915 tree packed_args
= NULL_TREE
;
4918 if (ARGUMENT_PACK_P (arg
))
4920 /* Extract the arguments from the argument pack. We'll be
4921 iterating over these in the following loop. */
4922 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4923 len
= TREE_VEC_LENGTH (packed_args
);
4926 for (j
= 0; j
< len
; j
++)
4929 /* Get the Jth argument in the parameter pack. */
4930 arg
= TREE_VEC_ELT (packed_args
, j
);
4932 if (PACK_EXPANSION_P (arg
))
4934 /* Pack expansions must come at the end of the
4936 if ((packed_args
&& j
< len
- 1)
4937 || (!packed_args
&& i
< nargs
- 1))
4939 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4940 error ("parameter pack argument %qE must be at the "
4941 "end of the template argument list", arg
);
4943 error ("parameter pack argument %qT must be at the "
4944 "end of the template argument list", arg
);
4948 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4949 /* We only care about the pattern. */
4950 arg
= PACK_EXPANSION_PATTERN (arg
);
4952 if (/* These first two lines are the `non-type' bit. */
4954 && TREE_CODE (arg
) != TEMPLATE_DECL
4955 /* This next two lines are the `argument expression is not just a
4956 simple identifier' condition and also the `specialized
4957 non-type argument' bit. */
4958 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4959 && !((REFERENCE_REF_P (arg
)
4960 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
4961 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4963 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4964 || (packed_args
&& uses_template_parms (arg
)))
4965 error_at (cp_expr_loc_or_input_loc (arg
),
4966 "template argument %qE involves template "
4967 "parameter(s)", arg
);
4970 /* Look at the corresponding template parameter,
4971 marking which template parameters its type depends
4973 tree type
= TREE_TYPE (parm
);
4977 /* We haven't yet initialized TPD2. Do so now. */
4978 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4979 /* The number of parameters here is the number in the
4980 main template, which, as checked in the assertion
4982 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4984 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4987 /* Mark the template parameters. But this time, we're
4988 looking for the template parameters of the main
4989 template, not in the specialization. */
4990 tpd2
.current_arg
= i
;
4991 tpd2
.arg_uses_template_parms
[i
] = 0;
4992 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4993 for_each_template_parm (type
,
4994 &mark_template_parm
,
4997 /*include_nondeduced_p=*/false);
4999 if (tpd2
.arg_uses_template_parms
[i
])
5001 /* The type depended on some template parameters.
5002 If they are fully specialized in the
5003 specialization, that's OK. */
5006 for (j
= 0; j
< nargs
; ++j
)
5007 if (tpd2
.parms
[j
] != 0
5008 && tpd
.arg_uses_template_parms
[j
])
5011 error_n (input_location
, count
,
5012 "type %qT of template argument %qE depends "
5013 "on a template parameter",
5014 "type %qT of template argument %qE depends "
5015 "on template parameters",
5024 /* We should only get here once. */
5025 if (TREE_CODE (decl
) == TYPE_DECL
)
5026 gcc_assert (!COMPLETE_TYPE_P (type
));
5028 // Build the template decl.
5029 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5030 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5031 TREE_TYPE (tmpl
) = type
;
5032 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5033 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5034 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5035 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5037 /* Give template template parms a DECL_CONTEXT of the template
5038 for which they are a parameter. */
5039 for (i
= 0; i
< ntparms
; ++i
)
5041 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5042 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5043 DECL_CONTEXT (parm
) = tmpl
;
5047 /* We didn't register this in check_explicit_specialization so we could
5048 wait until the constraints were set. */
5049 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5051 associate_classtype_constraints (type
);
5053 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5054 = tree_cons (specargs
, tmpl
,
5055 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5056 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5058 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5059 inst
= TREE_CHAIN (inst
))
5061 tree instance
= TREE_VALUE (inst
);
5062 if (TYPE_P (instance
)
5063 ? (COMPLETE_TYPE_P (instance
)
5064 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5065 : DECL_TEMPLATE_INSTANTIATION (instance
))
5067 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5068 tree inst_decl
= (DECL_P (instance
)
5069 ? instance
: TYPE_NAME (instance
));
5072 else if (spec
== error_mark_node
)
5073 permerror (input_location
,
5074 "declaration of %qD ambiguates earlier template "
5075 "instantiation for %qD", decl
, inst_decl
);
5076 else if (TREE_VALUE (spec
) == tmpl
)
5077 permerror (input_location
,
5078 "partial specialization of %qD after instantiation "
5079 "of %qD", decl
, inst_decl
);
5086 /* PARM is a template parameter of some form; return the corresponding
5087 TEMPLATE_PARM_INDEX. */
5090 get_template_parm_index (tree parm
)
5092 if (TREE_CODE (parm
) == PARM_DECL
5093 || TREE_CODE (parm
) == CONST_DECL
)
5094 parm
= DECL_INITIAL (parm
);
5095 else if (TREE_CODE (parm
) == TYPE_DECL
5096 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5097 parm
= TREE_TYPE (parm
);
5098 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5099 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5100 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5101 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5102 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5106 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5107 parameter packs used by the template parameter PARM. */
5110 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5112 /* A type parm can't refer to another parm. */
5113 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5115 else if (TREE_CODE (parm
) == PARM_DECL
)
5117 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5122 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5124 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5125 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5127 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5128 if (template_parameter_pack_p (p
))
5129 /* Any packs in the type are expanded by this parameter. */;
5131 fixed_parameter_pack_p_1 (p
, ppd
);
5135 /* PARM is a template parameter pack. Return any parameter packs used in
5136 its type or the type of any of its template parameters. If there are
5137 any such packs, it will be instantiated into a fixed template parameter
5138 list by partial instantiation rather than be fully deduced. */
5141 fixed_parameter_pack_p (tree parm
)
5143 /* This can only be true in a member template. */
5144 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5146 /* This can only be true for a parameter pack. */
5147 if (!template_parameter_pack_p (parm
))
5149 /* A type parm can't refer to another parm. */
5150 if (TREE_CODE (parm
) == TYPE_DECL
)
5153 tree parameter_packs
= NULL_TREE
;
5154 struct find_parameter_pack_data ppd
;
5155 ppd
.parameter_packs
= ¶meter_packs
;
5156 ppd
.visited
= new hash_set
<tree
>;
5157 ppd
.type_pack_expansion_p
= false;
5159 fixed_parameter_pack_p_1 (parm
, &ppd
);
5162 return parameter_packs
;
5165 /* Check that a template declaration's use of default arguments and
5166 parameter packs is not invalid. Here, PARMS are the template
5167 parameters. IS_PRIMARY is true if DECL is the thing declared by
5168 a primary template. IS_PARTIAL is true if DECL is a partial
5171 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5172 function template declaration or a friend class template
5173 declaration. In the function case, 1 indicates a declaration, 2
5174 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5175 emitted for extraneous default arguments.
5177 Returns TRUE if there were no errors found, FALSE otherwise. */
5180 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5181 bool is_partial
, int is_friend_decl
)
5184 int last_level_to_check
;
5186 bool no_errors
= true;
5190 A default template-argument shall not be specified in a
5191 function template declaration or a function template definition, nor
5192 in the template-parameter-list of the definition of a member of a
5195 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5196 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5197 /* You can't have a function template declaration in a local
5198 scope, nor you can you define a member of a class template in a
5202 if ((TREE_CODE (decl
) == TYPE_DECL
5204 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5205 || (TREE_CODE (decl
) == FUNCTION_DECL
5206 && LAMBDA_FUNCTION_P (decl
)))
5207 /* A lambda doesn't have an explicit declaration; don't complain
5208 about the parms of the enclosing class. */
5211 if (current_class_type
5212 && !TYPE_BEING_DEFINED (current_class_type
)
5213 && DECL_LANG_SPECIFIC (decl
)
5214 && DECL_DECLARES_FUNCTION_P (decl
)
5215 /* If this is either a friend defined in the scope of the class
5216 or a member function. */
5217 && (DECL_FUNCTION_MEMBER_P (decl
)
5218 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5219 : DECL_FRIEND_CONTEXT (decl
)
5220 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5222 /* And, if it was a member function, it really was defined in
5223 the scope of the class. */
5224 && (!DECL_FUNCTION_MEMBER_P (decl
)
5225 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5226 /* We already checked these parameters when the template was
5227 declared, so there's no need to do it again now. This function
5228 was defined in class scope, but we're processing its body now
5229 that the class is complete. */
5232 /* Core issue 226 (C++0x only): the following only applies to class
5235 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5239 If a template-parameter has a default template-argument, all
5240 subsequent template-parameters shall have a default
5241 template-argument supplied. */
5242 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5244 tree inner_parms
= TREE_VALUE (parm_level
);
5245 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5246 int seen_def_arg_p
= 0;
5249 for (i
= 0; i
< ntparms
; ++i
)
5251 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5253 if (parm
== error_mark_node
)
5256 if (TREE_PURPOSE (parm
))
5258 else if (seen_def_arg_p
5259 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5261 error ("no default argument for %qD", TREE_VALUE (parm
));
5262 /* For better subsequent error-recovery, we indicate that
5263 there should have been a default argument. */
5264 TREE_PURPOSE (parm
) = error_mark_node
;
5267 else if (!is_partial
5269 /* Don't complain about an enclosing partial
5271 && parm_level
== parms
5272 && TREE_CODE (decl
) == TYPE_DECL
5274 && template_parameter_pack_p (TREE_VALUE (parm
))
5275 /* A fixed parameter pack will be partially
5276 instantiated into a fixed length list. */
5277 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5279 /* A primary class template can only have one
5280 parameter pack, at the end of the template
5283 error ("parameter pack %q+D must be at the end of the"
5284 " template parameter list", TREE_VALUE (parm
));
5286 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5294 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5298 /* For an ordinary class template, default template arguments are
5299 allowed at the innermost level, e.g.:
5300 template <class T = int>
5302 but, in a partial specialization, they're not allowed even
5303 there, as we have in [temp.class.spec]:
5305 The template parameter list of a specialization shall not
5306 contain default template argument values.
5308 So, for a partial specialization, or for a function template
5309 (in C++98/C++03), we look at all of them. */
5312 /* But, for a primary class template that is not a partial
5313 specialization we look at all template parameters except the
5315 parms
= TREE_CHAIN (parms
);
5317 /* Figure out what error message to issue. */
5318 if (is_friend_decl
== 2)
5319 msg
= G_("default template arguments may not be used in function template "
5320 "friend re-declaration");
5321 else if (is_friend_decl
)
5322 msg
= G_("default template arguments may not be used in template "
5323 "friend declarations");
5324 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5325 msg
= G_("default template arguments may not be used in function templates "
5326 "without %<-std=c++11%> or %<-std=gnu++11%>");
5327 else if (is_partial
)
5328 msg
= G_("default template arguments may not be used in "
5329 "partial specializations");
5330 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5331 msg
= G_("default argument for template parameter for class enclosing %qD");
5333 /* Per [temp.param]/9, "A default template-argument shall not be
5334 specified in the template-parameter-lists of the definition of
5335 a member of a class template that appears outside of the member's
5336 class.", thus if we aren't handling a member of a class template
5337 there is no need to examine the parameters. */
5340 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5341 /* If we're inside a class definition, there's no need to
5342 examine the parameters to the class itself. On the one
5343 hand, they will be checked when the class is defined, and,
5344 on the other, default arguments are valid in things like:
5345 template <class T = double>
5346 struct S { template <class U> void f(U); };
5347 Here the default argument for `S' has no bearing on the
5348 declaration of `f'. */
5349 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5351 /* Check everything. */
5352 last_level_to_check
= 0;
5354 for (parm_level
= parms
;
5355 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5356 parm_level
= TREE_CHAIN (parm_level
))
5358 tree inner_parms
= TREE_VALUE (parm_level
);
5362 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5363 for (i
= 0; i
< ntparms
; ++i
)
5365 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5368 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5373 if (is_friend_decl
== 2)
5380 /* Clear out the default argument so that we are not
5382 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5386 /* At this point, if we're still interested in issuing messages,
5387 they must apply to classes surrounding the object declared. */
5389 msg
= G_("default argument for template parameter for class "
5396 /* Worker for push_template_decl_real, called via
5397 for_each_template_parm. DATA is really an int, indicating the
5398 level of the parameters we are interested in. If T is a template
5399 parameter of that level, return nonzero. */
5402 template_parm_this_level_p (tree t
, void* data
)
5404 int this_level
= *(int *)data
;
5407 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5408 level
= TEMPLATE_PARM_LEVEL (t
);
5410 level
= TEMPLATE_TYPE_LEVEL (t
);
5411 return level
== this_level
;
5414 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5415 DATA is really an int, indicating the innermost outer level of parameters.
5416 If T is a template parameter of that level or further out, return
5420 template_parm_outer_level (tree t
, void *data
)
5422 int this_level
= *(int *)data
;
5425 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5426 level
= TEMPLATE_PARM_LEVEL (t
);
5428 level
= TEMPLATE_TYPE_LEVEL (t
);
5429 return level
<= this_level
;
5432 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5433 parameters given by current_template_args, or reuses a
5434 previously existing one, if appropriate. Returns the DECL, or an
5435 equivalent one, if it is replaced via a call to duplicate_decls.
5437 If IS_FRIEND is true, DECL is a friend declaration. */
5440 push_template_decl_real (tree decl
, bool is_friend
)
5448 int new_template_p
= 0;
5449 /* True if the template is a member template, in the sense of
5451 bool member_template_p
= false;
5453 if (decl
== error_mark_node
|| !current_template_parms
)
5454 return error_mark_node
;
5456 /* See if this is a partial specialization. */
5457 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5458 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5459 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5461 && DECL_LANG_SPECIFIC (decl
)
5462 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5463 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5465 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5469 /* For a friend, we want the context of the friend, not
5470 the type of which it is a friend. */
5471 ctx
= CP_DECL_CONTEXT (decl
);
5472 else if (CP_DECL_CONTEXT (decl
)
5473 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5474 /* In the case of a virtual function, we want the class in which
5476 ctx
= CP_DECL_CONTEXT (decl
);
5478 /* Otherwise, if we're currently defining some class, the DECL
5479 is assumed to be a member of the class. */
5480 ctx
= current_scope ();
5482 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5485 if (!DECL_CONTEXT (decl
))
5486 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5488 /* See if this is a primary template. */
5489 if (is_friend
&& ctx
5490 && uses_template_parms_level (ctx
, processing_template_decl
))
5491 /* A friend template that specifies a class context, i.e.
5492 template <typename T> friend void A<T>::f();
5495 else if (TREE_CODE (decl
) == TYPE_DECL
5496 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5499 is_primary
= template_parm_scope_p ();
5503 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5505 if (DECL_CLASS_SCOPE_P (decl
))
5506 member_template_p
= true;
5507 if (TREE_CODE (decl
) == TYPE_DECL
5508 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5510 error ("template class without a name");
5511 return error_mark_node
;
5513 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5515 if (member_template_p
)
5517 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5518 error ("member template %qD may not have virt-specifiers", decl
);
5520 if (DECL_DESTRUCTOR_P (decl
))
5524 A destructor shall not be a member template. */
5525 error_at (DECL_SOURCE_LOCATION (decl
),
5526 "destructor %qD declared as member template", decl
);
5527 return error_mark_node
;
5529 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5530 && (!prototype_p (TREE_TYPE (decl
))
5531 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5532 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5533 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5534 == void_list_node
)))
5536 /* [basic.stc.dynamic.allocation]
5538 An allocation function can be a function
5539 template. ... Template allocation functions shall
5540 have two or more parameters. */
5541 error ("invalid template declaration of %qD", decl
);
5542 return error_mark_node
;
5545 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5546 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5548 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5549 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5550 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5552 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5553 if (TREE_CODE (t
) == TYPE_DECL
)
5555 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5556 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5559 else if (TREE_CODE (decl
) == TYPE_DECL
5560 && TYPE_DECL_ALIAS_P (decl
))
5561 /* alias-declaration */
5562 gcc_assert (!DECL_ARTIFICIAL (decl
));
5563 else if (VAR_P (decl
))
5564 /* C++14 variable template. */;
5567 error ("template declaration of %q#D", decl
);
5568 return error_mark_node
;
5572 /* Check to see that the rules regarding the use of default
5573 arguments are not being violated. We check args for a friend
5574 functions when we know whether it's a definition, introducing
5575 declaration or re-declaration. */
5576 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5577 check_default_tmpl_args (decl
, current_template_parms
,
5578 is_primary
, is_partial
, is_friend
);
5580 /* Ensure that there are no parameter packs in the type of this
5581 declaration that have not been expanded. */
5582 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5584 /* Check each of the arguments individually to see if there are
5585 any bare parameter packs. */
5586 tree type
= TREE_TYPE (decl
);
5587 tree arg
= DECL_ARGUMENTS (decl
);
5588 tree argtype
= TYPE_ARG_TYPES (type
);
5590 while (arg
&& argtype
)
5592 if (!DECL_PACK_P (arg
)
5593 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5595 /* This is a PARM_DECL that contains unexpanded parameter
5596 packs. We have already complained about this in the
5597 check_for_bare_parameter_packs call, so just replace
5598 these types with ERROR_MARK_NODE. */
5599 TREE_TYPE (arg
) = error_mark_node
;
5600 TREE_VALUE (argtype
) = error_mark_node
;
5603 arg
= DECL_CHAIN (arg
);
5604 argtype
= TREE_CHAIN (argtype
);
5607 /* Check for bare parameter packs in the return type and the
5608 exception specifiers. */
5609 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5610 /* Errors were already issued, set return type to int
5611 as the frontend doesn't expect error_mark_node as
5613 TREE_TYPE (type
) = integer_type_node
;
5614 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5615 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5617 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5618 && TYPE_DECL_ALIAS_P (decl
))
5619 ? DECL_ORIGINAL_TYPE (decl
)
5620 : TREE_TYPE (decl
)))
5622 TREE_TYPE (decl
) = error_mark_node
;
5623 return error_mark_node
;
5627 return process_partial_specialization (decl
);
5629 args
= current_template_args ();
5632 || TREE_CODE (ctx
) == FUNCTION_DECL
5633 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5634 || (TREE_CODE (decl
) == TYPE_DECL
5635 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5636 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5638 if (DECL_LANG_SPECIFIC (decl
)
5639 && DECL_TEMPLATE_INFO (decl
)
5640 && DECL_TI_TEMPLATE (decl
))
5641 tmpl
= DECL_TI_TEMPLATE (decl
);
5642 /* If DECL is a TYPE_DECL for a class-template, then there won't
5643 be DECL_LANG_SPECIFIC. The information equivalent to
5644 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5645 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5646 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5647 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5649 /* Since a template declaration already existed for this
5650 class-type, we must be redeclaring it here. Make sure
5651 that the redeclaration is valid. */
5652 redeclare_class_template (TREE_TYPE (decl
),
5653 current_template_parms
,
5654 current_template_constraints ());
5655 /* We don't need to create a new TEMPLATE_DECL; just use the
5656 one we already had. */
5657 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5661 tmpl
= build_template_decl (decl
, current_template_parms
,
5665 if (DECL_LANG_SPECIFIC (decl
)
5666 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5668 /* A specialization of a member template of a template
5670 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5671 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5672 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5678 tree a
, t
, current
, parms
;
5680 tree tinfo
= get_template_info (decl
);
5684 error ("template definition of non-template %q#D", decl
);
5685 return error_mark_node
;
5688 tmpl
= TI_TEMPLATE (tinfo
);
5690 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5691 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5692 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5693 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5697 /* The declaration is a specialization of a member
5698 template, declared outside the class. Therefore, the
5699 innermost template arguments will be NULL, so we
5700 replace them with the arguments determined by the
5701 earlier call to check_explicit_specialization. */
5702 args
= DECL_TI_ARGS (decl
);
5705 = build_template_decl (decl
, current_template_parms
,
5707 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5708 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5709 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5710 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5711 DECL_TEMPLATE_INFO (new_tmpl
)
5712 = build_template_info (tmpl
, args
);
5714 register_specialization (new_tmpl
,
5715 most_general_template (tmpl
),
5721 /* Make sure the template headers we got make sense. */
5723 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5724 i
= TMPL_PARMS_DEPTH (parms
);
5725 if (TMPL_ARGS_DEPTH (args
) != i
)
5727 error ("expected %d levels of template parms for %q#D, got %d",
5728 i
, decl
, TMPL_ARGS_DEPTH (args
));
5729 DECL_INTERFACE_KNOWN (decl
) = 1;
5730 return error_mark_node
;
5733 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5735 a
= TMPL_ARGS_LEVEL (args
, i
);
5736 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5738 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5740 if (current
== decl
)
5741 error ("got %d template parameters for %q#D",
5742 TREE_VEC_LENGTH (a
), decl
);
5744 error ("got %d template parameters for %q#T",
5745 TREE_VEC_LENGTH (a
), current
);
5746 error (" but %d required", TREE_VEC_LENGTH (t
));
5747 /* Avoid crash in import_export_decl. */
5748 DECL_INTERFACE_KNOWN (decl
) = 1;
5749 return error_mark_node
;
5752 if (current
== decl
)
5754 else if (current
== NULL_TREE
)
5755 /* Can happen in erroneous input. */
5758 current
= get_containing_scope (current
);
5761 /* Check that the parms are used in the appropriate qualifying scopes
5762 in the declarator. */
5763 if (!comp_template_args
5765 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5767 error ("template arguments to %qD do not match original "
5768 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5769 if (!uses_template_parms (TI_ARGS (tinfo
)))
5770 inform (input_location
, "use %<template<>%> for"
5771 " an explicit specialization");
5772 /* Avoid crash in import_export_decl. */
5773 DECL_INTERFACE_KNOWN (decl
) = 1;
5774 return error_mark_node
;
5778 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5779 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5781 /* Push template declarations for global functions and types. Note
5782 that we do not try to push a global template friend declared in a
5783 template class; such a thing may well depend on the template
5784 parameters of the class. */
5785 if (new_template_p
&& !ctx
5786 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5788 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5789 if (tmpl
== error_mark_node
)
5790 return error_mark_node
;
5792 /* Hide template friend classes that haven't been declared yet. */
5793 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5795 DECL_ANTICIPATED (tmpl
) = 1;
5796 DECL_FRIEND_P (tmpl
) = 1;
5802 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5804 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5806 /* Give template template parms a DECL_CONTEXT of the template
5807 for which they are a parameter. */
5808 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5809 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5811 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5812 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5813 DECL_CONTEXT (parm
) = tmpl
;
5816 if (TREE_CODE (decl
) == TYPE_DECL
5817 && TYPE_DECL_ALIAS_P (decl
)
5818 && complex_alias_template_p (tmpl
))
5819 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5822 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5823 back to its most general template. If TMPL is a specialization,
5824 ARGS may only have the innermost set of arguments. Add the missing
5825 argument levels if necessary. */
5826 if (DECL_TEMPLATE_INFO (tmpl
))
5827 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5829 info
= build_template_info (tmpl
, args
);
5831 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5832 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5836 retrofit_lang_decl (decl
);
5837 if (DECL_LANG_SPECIFIC (decl
))
5838 DECL_TEMPLATE_INFO (decl
) = info
;
5841 if (flag_implicit_templates
5843 && TREE_PUBLIC (decl
)
5844 && VAR_OR_FUNCTION_DECL_P (decl
))
5845 /* Set DECL_COMDAT on template instantiations; if we force
5846 them to be emitted by explicit instantiation,
5847 mark_needed will tell cgraph to do the right thing. */
5848 DECL_COMDAT (decl
) = true;
5850 return DECL_TEMPLATE_RESULT (tmpl
);
5854 push_template_decl (tree decl
)
5856 return push_template_decl_real (decl
, false);
5859 /* FN is an inheriting constructor that inherits from the constructor
5860 template INHERITED; turn FN into a constructor template with a matching
5864 add_inherited_template_parms (tree fn
, tree inherited
)
5867 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5868 inner_parms
= copy_node (inner_parms
);
5870 = tree_cons (size_int (processing_template_decl
+ 1),
5871 inner_parms
, current_template_parms
);
5872 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5873 tree args
= template_parms_to_args (parms
);
5874 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5875 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5876 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5877 DECL_ARTIFICIAL (tmpl
) = true;
5878 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5882 /* Called when a class template TYPE is redeclared with the indicated
5883 template PARMS, e.g.:
5885 template <class T> struct S;
5886 template <class T> struct S {}; */
5889 redeclare_class_template (tree type
, tree parms
, tree cons
)
5895 if (!TYPE_TEMPLATE_INFO (type
))
5897 error ("%qT is not a template type", type
);
5901 tmpl
= TYPE_TI_TEMPLATE (type
);
5902 if (!PRIMARY_TEMPLATE_P (tmpl
))
5903 /* The type is nested in some template class. Nothing to worry
5904 about here; there are no new template parameters for the nested
5910 error ("template specifiers not specified in declaration of %qD",
5915 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5916 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5918 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5920 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5921 "redeclared with %d template parameter",
5922 "redeclared with %d template parameters",
5923 TREE_VEC_LENGTH (parms
));
5924 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5925 "previous declaration %qD used %d template parameter",
5926 "previous declaration %qD used %d template parameters",
5927 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5931 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5938 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5939 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5942 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5943 if (error_operand_p (tmpl_parm
))
5946 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5947 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5948 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5950 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5952 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5953 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5954 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5955 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5956 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5957 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5958 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5959 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5960 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5962 error ("template parameter %q+#D", tmpl_parm
);
5963 error ("redeclared here as %q#D", parm
);
5967 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5969 /* We have in [temp.param]:
5971 A template-parameter may not be given default arguments
5972 by two different declarations in the same scope. */
5973 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5974 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5975 "original definition appeared here");
5979 if (parm_default
!= NULL_TREE
)
5980 /* Update the previous template parameters (which are the ones
5981 that will really count) with the new default value. */
5982 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5983 else if (tmpl_default
!= NULL_TREE
)
5984 /* Update the new parameters, too; they'll be used as the
5985 parameters for any members. */
5986 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5988 /* Give each template template parm in this redeclaration a
5989 DECL_CONTEXT of the template for which they are a parameter. */
5990 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5992 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5993 DECL_CONTEXT (parm
) = tmpl
;
5996 if (TREE_CODE (parm
) == TYPE_DECL
)
5997 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6000 // Cannot redeclare a class template with a different set of constraints.
6001 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
6003 error_at (input_location
, "redeclaration %q#D with different "
6004 "constraints", tmpl
);
6005 inform (DECL_SOURCE_LOCATION (tmpl
),
6006 "original declaration appeared here");
6012 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6013 to be used when the caller has already checked
6014 (processing_template_decl
6015 && !instantiation_dependent_expression_p (expr)
6016 && potential_constant_expression (expr))
6017 and cleared processing_template_decl. */
6020 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6022 return tsubst_copy_and_build (expr
,
6025 /*in_decl=*/NULL_TREE
,
6026 /*function_p=*/false,
6027 /*integral_constant_expression_p=*/true);
6030 /* Simplify EXPR if it is a non-dependent expression. Returns the
6031 (possibly simplified) expression. */
6034 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6036 if (expr
== NULL_TREE
)
6039 /* If we're in a template, but EXPR isn't value dependent, simplify
6040 it. We're supposed to treat:
6042 template <typename T> void f(T[1 + 1]);
6043 template <typename T> void f(T[2]);
6045 as two declarations of the same function, for example. */
6046 if (processing_template_decl
6047 && is_nondependent_constant_expression (expr
))
6049 processing_template_decl_sentinel s
;
6050 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6056 instantiate_non_dependent_expr (tree expr
)
6058 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6061 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6062 an uninstantiated expression. */
6065 instantiate_non_dependent_or_null (tree expr
)
6067 if (expr
== NULL_TREE
)
6069 if (processing_template_decl
)
6071 if (!is_nondependent_constant_expression (expr
))
6075 processing_template_decl_sentinel s
;
6076 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6082 /* True iff T is a specialization of a variable template. */
6085 variable_template_specialization_p (tree t
)
6087 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6089 tree tmpl
= DECL_TI_TEMPLATE (t
);
6090 return variable_template_p (tmpl
);
6093 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6094 template declaration, or a TYPE_DECL for an alias declaration. */
6097 alias_type_or_template_p (tree t
)
6101 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6104 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6105 || DECL_ALIAS_TEMPLATE_P (t
));
6108 /* Return TRUE iff T is a specialization of an alias template. */
6111 alias_template_specialization_p (const_tree t
)
6113 /* It's an alias template specialization if it's an alias and its
6114 TYPE_NAME is a specialization of a primary template. */
6115 if (TYPE_ALIAS_P (t
))
6116 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6117 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6122 /* An alias template is complex from a SFINAE perspective if a template-id
6123 using that alias can be ill-formed when the expansion is not, as with
6124 the void_t template. We determine this by checking whether the
6125 expansion for the alias template uses all its template parameters. */
6127 struct uses_all_template_parms_data
6134 uses_all_template_parms_r (tree t
, void *data_
)
6136 struct uses_all_template_parms_data
&data
6137 = *(struct uses_all_template_parms_data
*)data_
;
6138 tree idx
= get_template_parm_index (t
);
6140 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6141 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6146 complex_alias_template_p (const_tree tmpl
)
6148 struct uses_all_template_parms_data data
;
6149 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6150 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6151 data
.level
= TMPL_PARMS_DEPTH (parms
);
6152 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6153 data
.seen
= XALLOCAVEC (bool, len
);
6154 for (int i
= 0; i
< len
; ++i
)
6155 data
.seen
[i
] = false;
6157 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6158 for (int i
= 0; i
< len
; ++i
)
6164 /* Return TRUE iff T is a specialization of a complex alias template with
6165 dependent template-arguments. */
6168 dependent_alias_template_spec_p (const_tree t
)
6170 if (!alias_template_specialization_p (t
))
6173 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6174 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6177 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6178 if (!any_dependent_template_arguments_p (args
))
6184 /* Return the number of innermost template parameters in TMPL. */
6187 num_innermost_template_parms (tree tmpl
)
6189 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6190 return TREE_VEC_LENGTH (parms
);
6193 /* Return either TMPL or another template that it is equivalent to under DR
6194 1286: An alias that just changes the name of a template is equivalent to
6195 the other template. */
6198 get_underlying_template (tree tmpl
)
6200 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6201 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6203 /* Determine if the alias is equivalent to an underlying template. */
6204 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6205 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6209 tree underlying
= TI_TEMPLATE (tinfo
);
6210 if (!PRIMARY_TEMPLATE_P (underlying
)
6211 || (num_innermost_template_parms (tmpl
)
6212 != num_innermost_template_parms (underlying
)))
6215 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6216 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6217 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6220 /* Alias is equivalent. Strip it and repeat. */
6227 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6228 must be a reference-to-function or a pointer-to-function type, as specified
6229 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6230 and check that the resulting function has external linkage. */
6233 convert_nontype_argument_function (tree type
, tree expr
,
6234 tsubst_flags_t complain
)
6238 linkage_kind linkage
;
6240 fn
= instantiate_type (type
, fns
, tf_none
);
6241 if (fn
== error_mark_node
)
6242 return error_mark_node
;
6244 if (value_dependent_expression_p (fn
))
6247 fn_no_ptr
= strip_fnptr_conv (fn
);
6248 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6249 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6250 if (BASELINK_P (fn_no_ptr
))
6251 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6253 /* [temp.arg.nontype]/1
6255 A template-argument for a non-type, non-template template-parameter
6258 -- the address of an object or function with external [C++11: or
6259 internal] linkage. */
6261 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6262 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6264 if (complain
& tf_error
)
6266 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6267 error_at (loc
, "%qE is not a valid template argument for type %qT",
6269 if (TYPE_PTR_P (type
))
6270 inform (loc
, "it must be the address of a function "
6271 "with external linkage");
6273 inform (loc
, "it must be the name of a function with "
6274 "external linkage");
6279 linkage
= decl_linkage (fn_no_ptr
);
6280 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6282 if (complain
& tf_error
)
6284 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6285 if (cxx_dialect
>= cxx11
)
6286 error_at (loc
, "%qE is not a valid template argument for type "
6287 "%qT because %qD has no linkage",
6288 expr
, type
, fn_no_ptr
);
6290 error_at (loc
, "%qE is not a valid template argument for type "
6291 "%qT because %qD does not have external linkage",
6292 expr
, type
, fn_no_ptr
);
6298 if (TYPE_REF_P (type
))
6300 if (REFERENCE_REF_P (fn
))
6301 fn
= TREE_OPERAND (fn
, 0);
6303 fn
= build_address (fn
);
6305 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6306 fn
= build_nop (type
, fn
);
6311 /* Subroutine of convert_nontype_argument.
6312 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6313 Emit an error otherwise. */
6316 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6317 tsubst_flags_t complain
)
6319 tree orig_expr
= expr
;
6321 if (null_ptr_cst_p (expr
))
6323 if (TREE_CODE (expr
) == PTRMEM_CST
6324 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6325 PTRMEM_CST_CLASS (expr
)))
6327 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6329 if (processing_template_decl
6330 && TREE_CODE (expr
) == ADDR_EXPR
6331 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6333 if (complain
& tf_error
)
6335 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6336 error_at (loc
, "%qE is not a valid template argument for type %qT",
6338 if (TREE_CODE (expr
) != PTRMEM_CST
)
6339 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6341 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6346 /* Returns TRUE iff the address of OP is value-dependent.
6348 14.6.2.4 [temp.dep.temp]:
6349 A non-integral non-type template-argument is dependent if its type is
6350 dependent or it has either of the following forms
6353 and contains a nested-name-specifier which specifies a class-name that
6354 names a dependent type.
6356 We generalize this to just say that the address of a member of a
6357 dependent class is value-dependent; the above doesn't cover the
6358 address of a static data member named with an unqualified-id. */
6361 has_value_dependent_address (tree op
)
6363 /* We could use get_inner_reference here, but there's no need;
6364 this is only relevant for template non-type arguments, which
6365 can only be expressed as &id-expression. */
6368 tree ctx
= CP_DECL_CONTEXT (op
);
6369 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6376 /* The next set of functions are used for providing helpful explanatory
6377 diagnostics for failed overload resolution. Their messages should be
6378 indented by two spaces for consistency with the messages in
6382 unify_success (bool /*explain_p*/)
6387 /* Other failure functions should call this one, to provide a single function
6388 for setting a breakpoint on. */
6391 unify_invalid (bool /*explain_p*/)
6397 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6400 inform (input_location
,
6401 " couldn%'t deduce template parameter %qD", parm
);
6402 return unify_invalid (explain_p
);
6406 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6409 inform (input_location
,
6410 " types %qT and %qT have incompatible cv-qualifiers",
6412 return unify_invalid (explain_p
);
6416 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6419 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6420 return unify_invalid (explain_p
);
6424 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6427 inform (input_location
,
6428 " template parameter %qD is not a parameter pack, but "
6431 return unify_invalid (explain_p
);
6435 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6438 inform (input_location
,
6439 " template argument %qE does not match "
6440 "pointer-to-member constant %qE",
6442 return unify_invalid (explain_p
);
6446 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6449 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6450 return unify_invalid (explain_p
);
6454 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6457 inform (input_location
,
6458 " inconsistent parameter pack deduction with %qT and %qT",
6460 return unify_invalid (explain_p
);
6464 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6469 inform (input_location
,
6470 " deduced conflicting types for parameter %qT (%qT and %qT)",
6471 parm
, first
, second
);
6473 inform (input_location
,
6474 " deduced conflicting values for non-type parameter "
6475 "%qE (%qE and %qE)", parm
, first
, second
);
6477 return unify_invalid (explain_p
);
6481 unify_vla_arg (bool explain_p
, tree arg
)
6484 inform (input_location
,
6485 " variable-sized array type %qT is not "
6486 "a valid template argument",
6488 return unify_invalid (explain_p
);
6492 unify_method_type_error (bool explain_p
, tree arg
)
6495 inform (input_location
,
6496 " member function type %qT is not a valid template argument",
6498 return unify_invalid (explain_p
);
6502 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6507 inform_n (input_location
, wanted
,
6508 " candidate expects at least %d argument, %d provided",
6509 " candidate expects at least %d arguments, %d provided",
6512 inform_n (input_location
, wanted
,
6513 " candidate expects %d argument, %d provided",
6514 " candidate expects %d arguments, %d provided",
6517 return unify_invalid (explain_p
);
6521 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6523 return unify_arity (explain_p
, have
, wanted
);
6527 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6528 bool least_p
= false)
6530 return unify_arity (explain_p
, have
, wanted
, least_p
);
6534 unify_arg_conversion (bool explain_p
, tree to_type
,
6535 tree from_type
, tree arg
)
6538 inform (cp_expr_loc_or_input_loc (arg
),
6539 " cannot convert %qE (type %qT) to type %qT",
6540 arg
, from_type
, to_type
);
6541 return unify_invalid (explain_p
);
6545 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6546 tree parm
, tree arg
)
6551 case tbr_ambiguous_baseclass
:
6552 inform (input_location
, " %qT is an ambiguous base class of %qT",
6556 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6559 return unify_invalid (explain_p
);
6563 unify_inconsistent_template_template_parameters (bool explain_p
)
6566 inform (input_location
,
6567 " template parameters of a template template argument are "
6568 "inconsistent with other deduced template arguments");
6569 return unify_invalid (explain_p
);
6573 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6576 inform (input_location
,
6577 " cannot deduce a template for %qT from non-template type %qT",
6579 return unify_invalid (explain_p
);
6583 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6586 inform (input_location
,
6587 " template argument %qE does not match %qE", arg
, parm
);
6588 return unify_invalid (explain_p
);
6591 /* True if T is a C++20 template parameter object to store the argument for a
6592 template parameter of class type. */
6595 template_parm_object_p (const_tree t
)
6597 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6598 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6601 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6602 argument for TYPE, points to an unsuitable object. */
6605 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6607 switch (TREE_CODE (expr
))
6610 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6614 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6619 unsigned i
; tree elt
;
6620 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6621 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6628 tree decl
= TREE_OPERAND (expr
, 0);
6632 if (complain
& tf_error
)
6633 error_at (cp_expr_loc_or_input_loc (expr
),
6634 "%qE is not a valid template argument of type %qT "
6635 "because %qE is not a variable", expr
, type
, decl
);
6638 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6640 if (complain
& tf_error
)
6641 error_at (cp_expr_loc_or_input_loc (expr
),
6642 "%qE is not a valid template argument of type %qT "
6643 "in C++98 because %qD does not have external linkage",
6647 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6648 && decl_linkage (decl
) == lk_none
)
6650 if (complain
& tf_error
)
6651 error_at (cp_expr_loc_or_input_loc (expr
),
6652 "%qE is not a valid template argument of type %qT "
6653 "because %qD has no linkage", expr
, type
, decl
);
6656 /* C++17: For a non-type template-parameter of reference or pointer
6657 type, the value of the constant expression shall not refer to (or
6658 for a pointer type, shall not be the address of):
6659 * a subobject (4.5),
6660 * a temporary object (15.2),
6661 * a string literal (5.13.5),
6662 * the result of a typeid expression (8.2.8), or
6663 * a predefined __func__ variable (11.4.1). */
6664 else if (DECL_ARTIFICIAL (decl
))
6666 if (complain
& tf_error
)
6667 error ("the address of %qD is not a valid template argument",
6671 else if (!same_type_ignoring_top_level_qualifiers_p
6672 (strip_array_types (TREE_TYPE (type
)),
6673 strip_array_types (TREE_TYPE (decl
))))
6675 if (complain
& tf_error
)
6676 error ("the address of the %qT subobject of %qD is not a "
6677 "valid template argument", TREE_TYPE (type
), decl
);
6680 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6682 if (complain
& tf_error
)
6683 error ("the address of %qD is not a valid template argument "
6684 "because it does not have static storage duration",
6692 if (!INDIRECT_TYPE_P (type
))
6693 /* We're only concerned about pointers and references here. */;
6694 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6695 /* Null pointer values are OK in C++11. */;
6700 if (complain
& tf_error
)
6701 error ("%qD is not a valid template argument "
6702 "because %qD is a variable, not the address of "
6703 "a variable", expr
, expr
);
6708 if (complain
& tf_error
)
6709 error ("%qE is not a valid template argument for %qT "
6710 "because it is not the address of a variable",
6720 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6721 template argument EXPR. */
6724 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6726 if (TREE_CODE (expr
) == TARGET_EXPR
)
6727 expr
= TARGET_EXPR_INITIAL (expr
);
6729 if (!TREE_CONSTANT (expr
))
6731 if ((complain
& tf_error
)
6732 && require_rvalue_constant_expression (expr
))
6733 cxx_constant_value (expr
);
6734 return error_mark_node
;
6736 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6737 return error_mark_node
;
6739 tree name
= mangle_template_parm_object (expr
);
6740 tree decl
= get_global_binding (name
);
6744 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6745 decl
= create_temporary_var (type
);
6746 TREE_STATIC (decl
) = true;
6747 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6748 TREE_READONLY (decl
) = true;
6749 DECL_NAME (decl
) = name
;
6750 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6751 DECL_CONTEXT (decl
) = global_namespace
;
6752 comdat_linkage (decl
);
6753 pushdecl_top_level_and_finish (decl
, expr
);
6757 /* Attempt to convert the non-type template parameter EXPR to the
6758 indicated TYPE. If the conversion is successful, return the
6759 converted value. If the conversion is unsuccessful, return
6760 NULL_TREE if we issued an error message, or error_mark_node if we
6761 did not. We issue error messages for out-and-out bad template
6762 parameters, but not simply because the conversion failed, since we
6763 might be just trying to do argument deduction. Both TYPE and EXPR
6764 must be non-dependent.
6766 The conversion follows the special rules described in
6767 [temp.arg.nontype], and it is much more strict than an implicit
6770 This function is called twice for each template argument (see
6771 lookup_template_class for a more accurate description of this
6772 problem). This means that we need to handle expressions which
6773 are not valid in a C++ source, but can be created from the
6774 first call (for instance, casts to perform conversions). These
6775 hacks can go away after we fix the double coercion problem. */
6778 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6781 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6783 /* Detect immediately string literals as invalid non-type argument.
6784 This special-case is not needed for correctness (we would easily
6785 catch this later), but only to provide better diagnostic for this
6786 common user mistake. As suggested by DR 100, we do not mention
6787 linkage issues in the diagnostic as this is not the point. */
6788 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
6790 if (complain
& tf_error
)
6791 error ("%qE is not a valid template argument for type %qT "
6792 "because string literals can never be used in this context",
6797 /* Add the ADDR_EXPR now for the benefit of
6798 value_dependent_expression_p. */
6799 if (TYPE_PTROBV_P (type
)
6800 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6802 expr
= decay_conversion (expr
, complain
);
6803 if (expr
== error_mark_node
)
6804 return error_mark_node
;
6807 /* If we are in a template, EXPR may be non-dependent, but still
6808 have a syntactic, rather than semantic, form. For example, EXPR
6809 might be a SCOPE_REF, rather than the VAR_DECL to which the
6810 SCOPE_REF refers. Preserving the qualifying scope is necessary
6811 so that access checking can be performed when the template is
6812 instantiated -- but here we need the resolved form so that we can
6813 convert the argument. */
6814 bool non_dep
= false;
6815 if (TYPE_REF_OBJ_P (type
)
6816 && has_value_dependent_address (expr
))
6817 /* If we want the address and it's value-dependent, don't fold. */;
6818 else if (processing_template_decl
6819 && is_nondependent_constant_expression (expr
))
6821 if (error_operand_p (expr
))
6822 return error_mark_node
;
6823 expr_type
= TREE_TYPE (expr
);
6825 /* If the argument is non-dependent, perform any conversions in
6826 non-dependent context as well. */
6827 processing_template_decl_sentinel
s (non_dep
);
6829 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6831 if (value_dependent_expression_p (expr
))
6832 expr
= canonicalize_expr_argument (expr
, complain
);
6834 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6835 to a non-type argument of "nullptr". */
6836 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6837 expr
= fold_simple (convert (type
, expr
));
6839 /* In C++11, integral or enumeration non-type template arguments can be
6840 arbitrary constant expressions. Pointer and pointer to
6841 member arguments can be general constant expressions that evaluate
6842 to a null value, but otherwise still need to be of a specific form. */
6843 if (cxx_dialect
>= cxx11
)
6845 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
6846 /* A PTRMEM_CST is already constant, and a valid template
6847 argument for a parameter of pointer to member type, we just want
6848 to leave it in that form rather than lower it to a
6850 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6851 || cxx_dialect
>= cxx17
)
6853 /* Calling build_converted_constant_expr might create a call to
6854 a conversion function with a value-dependent argument, which
6855 could invoke taking the address of a temporary representing
6856 the result of the conversion. */
6857 if (COMPOUND_LITERAL_P (expr
)
6858 && CONSTRUCTOR_IS_DEPENDENT (expr
)
6859 && MAYBE_CLASS_TYPE_P (expr_type
)
6860 && TYPE_HAS_CONVERSION (expr_type
))
6862 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
6863 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
6866 /* C++17: A template-argument for a non-type template-parameter shall
6867 be a converted constant expression (8.20) of the type of the
6868 template-parameter. */
6869 expr
= build_converted_constant_expr (type
, expr
, complain
);
6870 if (expr
== error_mark_node
)
6871 /* Make sure we return NULL_TREE only if we have really issued
6872 an error, as described above. */
6873 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
6874 expr
= maybe_constant_value (expr
, NULL_TREE
,
6875 /*manifestly_const_eval=*/true);
6876 expr
= convert_from_reference (expr
);
6878 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6880 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
6881 /*manifestly_const_eval=*/true);
6882 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6883 : null_member_pointer_value_p (folded
))
6888 if (TYPE_REF_P (type
))
6889 expr
= mark_lvalue_use (expr
);
6891 expr
= mark_rvalue_use (expr
);
6893 /* HACK: Due to double coercion, we can get a
6894 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6895 which is the tree that we built on the first call (see
6896 below when coercing to reference to object or to reference to
6897 function). We just strip everything and get to the arg.
6898 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6900 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6902 tree probe_type
, probe
= expr
;
6903 if (REFERENCE_REF_P (probe
))
6904 probe
= TREE_OPERAND (probe
, 0);
6905 probe_type
= TREE_TYPE (probe
);
6906 if (TREE_CODE (probe
) == NOP_EXPR
)
6908 /* ??? Maybe we could use convert_from_reference here, but we
6909 would need to relax its constraints because the NOP_EXPR
6910 could actually change the type to something more cv-qualified,
6911 and this is not folded by convert_from_reference. */
6912 tree addr
= TREE_OPERAND (probe
, 0);
6913 if (TYPE_REF_P (probe_type
)
6914 && TREE_CODE (addr
) == ADDR_EXPR
6915 && TYPE_PTR_P (TREE_TYPE (addr
))
6916 && (same_type_ignoring_top_level_qualifiers_p
6917 (TREE_TYPE (probe_type
),
6918 TREE_TYPE (TREE_TYPE (addr
)))))
6920 expr
= TREE_OPERAND (addr
, 0);
6921 expr_type
= TREE_TYPE (probe_type
);
6926 /* [temp.arg.nontype]/5, bullet 1
6928 For a non-type template-parameter of integral or enumeration type,
6929 integral promotions (_conv.prom_) and integral conversions
6930 (_conv.integral_) are applied. */
6931 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6933 if (cxx_dialect
< cxx11
)
6935 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6936 t
= maybe_constant_value (t
);
6937 if (t
!= error_mark_node
)
6941 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6942 return error_mark_node
;
6944 /* Notice that there are constant expressions like '4 % 0' which
6945 do not fold into integer constants. */
6946 if (TREE_CODE (expr
) != INTEGER_CST
6947 && !value_dependent_expression_p (expr
))
6949 if (complain
& tf_error
)
6951 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6952 if (!require_potential_constant_expression (expr
))
6953 expr
= error_mark_node
;
6955 expr
= cxx_constant_value (expr
);
6956 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6957 inform (loc
, "in template argument for type %qT", type
);
6958 if (expr
== error_mark_node
)
6960 /* else cxx_constant_value complained but gave us
6961 a real constant, so go ahead. */
6962 if (TREE_CODE (expr
) != INTEGER_CST
)
6964 /* Some assemble time constant expressions like
6965 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6966 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6967 as we can emit them into .rodata initializers of
6968 variables, yet they can't fold into an INTEGER_CST at
6969 compile time. Refuse them here. */
6970 gcc_checking_assert (reduced_constant_expression_p (expr
));
6971 error_at (loc
, "template argument %qE for type %qT not "
6972 "a constant integer", expr
, type
);
6980 /* Avoid typedef problems. */
6981 if (TREE_TYPE (expr
) != type
)
6982 expr
= fold_convert (type
, expr
);
6984 /* [temp.arg.nontype]/5, bullet 2
6986 For a non-type template-parameter of type pointer to object,
6987 qualification conversions (_conv.qual_) and the array-to-pointer
6988 conversion (_conv.array_) are applied. */
6989 else if (TYPE_PTROBV_P (type
))
6991 tree decayed
= expr
;
6993 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6994 decay_conversion or an explicit cast. If it's a problematic cast,
6995 we'll complain about it below. */
6996 if (TREE_CODE (expr
) == NOP_EXPR
)
7000 if (TREE_CODE (probe
) == ADDR_EXPR
7001 && TYPE_PTR_P (TREE_TYPE (probe
)))
7004 expr_type
= TREE_TYPE (expr
);
7008 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7010 A template-argument for a non-type, non-template template-parameter
7011 shall be one of: [...]
7013 -- the name of a non-type template-parameter;
7014 -- the address of an object or function with external linkage, [...]
7015 expressed as "& id-expression" where the & is optional if the name
7016 refers to a function or array, or if the corresponding
7017 template-parameter is a reference.
7019 Here, we do not care about functions, as they are invalid anyway
7020 for a parameter of type pointer-to-object. */
7022 if (value_dependent_expression_p (expr
))
7023 /* Non-type template parameters are OK. */
7025 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7026 /* Null pointer values are OK in C++11. */;
7027 else if (TREE_CODE (expr
) != ADDR_EXPR
7028 && !INDIRECT_TYPE_P (expr_type
))
7029 /* Other values, like integer constants, might be valid
7030 non-type arguments of some other type. */
7031 return error_mark_node
;
7032 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7037 expr
= perform_qualification_conversions (type
, expr
);
7038 if (expr
== error_mark_node
)
7039 return error_mark_node
;
7041 /* [temp.arg.nontype]/5, bullet 3
7043 For a non-type template-parameter of type reference to object, no
7044 conversions apply. The type referred to by the reference may be more
7045 cv-qualified than the (otherwise identical) type of the
7046 template-argument. The template-parameter is bound directly to the
7047 template-argument, which must be an lvalue. */
7048 else if (TYPE_REF_OBJ_P (type
))
7050 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7052 return error_mark_node
;
7054 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7056 if (complain
& tf_error
)
7057 error ("%qE is not a valid template argument for type %qT "
7058 "because of conflicts in cv-qualification", expr
, type
);
7062 if (!lvalue_p (expr
))
7064 if (complain
& tf_error
)
7065 error ("%qE is not a valid template argument for type %qT "
7066 "because it is not an lvalue", expr
, type
);
7070 /* [temp.arg.nontype]/1
7072 A template-argument for a non-type, non-template template-parameter
7073 shall be one of: [...]
7075 -- the address of an object or function with external linkage. */
7076 if (INDIRECT_REF_P (expr
)
7077 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7079 expr
= TREE_OPERAND (expr
, 0);
7082 if (complain
& tf_error
)
7083 error ("%q#D is not a valid template argument for type %qT "
7084 "because a reference variable does not have a constant "
7085 "address", expr
, type
);
7090 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7091 && value_dependent_expression_p (expr
))
7092 /* OK, dependent reference. We don't want to ask whether a DECL is
7093 itself value-dependent, since what we want here is its address. */;
7096 expr
= build_address (expr
);
7098 if (invalid_tparm_referent_p (type
, expr
, complain
))
7102 if (!same_type_p (type
, TREE_TYPE (expr
)))
7103 expr
= build_nop (type
, expr
);
7105 /* [temp.arg.nontype]/5, bullet 4
7107 For a non-type template-parameter of type pointer to function, only
7108 the function-to-pointer conversion (_conv.func_) is applied. If the
7109 template-argument represents a set of overloaded functions (or a
7110 pointer to such), the matching function is selected from the set
7112 else if (TYPE_PTRFN_P (type
))
7114 /* If the argument is a template-id, we might not have enough
7115 context information to decay the pointer. */
7116 if (!type_unknown_p (expr_type
))
7118 expr
= decay_conversion (expr
, complain
);
7119 if (expr
== error_mark_node
)
7120 return error_mark_node
;
7123 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7124 /* Null pointer values are OK in C++11. */
7125 return perform_qualification_conversions (type
, expr
);
7127 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7128 if (!expr
|| expr
== error_mark_node
)
7131 /* [temp.arg.nontype]/5, bullet 5
7133 For a non-type template-parameter of type reference to function, no
7134 conversions apply. If the template-argument represents a set of
7135 overloaded functions, the matching function is selected from the set
7137 else if (TYPE_REFFN_P (type
))
7139 if (TREE_CODE (expr
) == ADDR_EXPR
)
7141 if (complain
& tf_error
)
7143 error ("%qE is not a valid template argument for type %qT "
7144 "because it is a pointer", expr
, type
);
7145 inform (input_location
, "try using %qE instead",
7146 TREE_OPERAND (expr
, 0));
7151 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7152 if (!expr
|| expr
== error_mark_node
)
7155 /* [temp.arg.nontype]/5, bullet 6
7157 For a non-type template-parameter of type pointer to member function,
7158 no conversions apply. If the template-argument represents a set of
7159 overloaded member functions, the matching member function is selected
7160 from the set (_over.over_). */
7161 else if (TYPE_PTRMEMFUNC_P (type
))
7163 expr
= instantiate_type (type
, expr
, tf_none
);
7164 if (expr
== error_mark_node
)
7165 return error_mark_node
;
7167 /* [temp.arg.nontype] bullet 1 says the pointer to member
7168 expression must be a pointer-to-member constant. */
7169 if (!value_dependent_expression_p (expr
)
7170 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7173 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7174 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7175 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7176 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7178 /* [temp.arg.nontype]/5, bullet 7
7180 For a non-type template-parameter of type pointer to data member,
7181 qualification conversions (_conv.qual_) are applied. */
7182 else if (TYPE_PTRDATAMEM_P (type
))
7184 /* [temp.arg.nontype] bullet 1 says the pointer to member
7185 expression must be a pointer-to-member constant. */
7186 if (!value_dependent_expression_p (expr
)
7187 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7190 expr
= perform_qualification_conversions (type
, expr
);
7191 if (expr
== error_mark_node
)
7194 else if (NULLPTR_TYPE_P (type
))
7196 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7198 if (complain
& tf_error
)
7199 error ("%qE is not a valid template argument for type %qT "
7200 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7205 else if (CLASS_TYPE_P (type
))
7207 /* Replace the argument with a reference to the corresponding template
7208 parameter object. */
7209 if (!value_dependent_expression_p (expr
))
7210 expr
= get_template_parm_object (expr
, complain
);
7211 if (expr
== error_mark_node
)
7214 /* A template non-type parameter must be one of the above. */
7218 /* Sanity check: did we actually convert the argument to the
7220 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7221 (type
, TREE_TYPE (expr
)));
7222 return convert_from_reference (expr
);
7225 /* Subroutine of coerce_template_template_parms, which returns 1 if
7226 PARM_PARM and ARG_PARM match using the rule for the template
7227 parameters of template template parameters. Both PARM and ARG are
7228 template parameters; the rest of the arguments are the same as for
7229 coerce_template_template_parms.
7232 coerce_template_template_parm (tree parm
,
7234 tsubst_flags_t complain
,
7238 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7239 || parm
== NULL_TREE
|| error_operand_p (parm
))
7242 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7245 switch (TREE_CODE (parm
))
7248 /* We encounter instantiations of templates like
7249 template <template <template <class> class> class TT>
7252 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7253 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7255 if (!coerce_template_template_parms
7256 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7262 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7263 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7264 /* Argument is a parameter pack but parameter is not. */
7269 /* The tsubst call is used to handle cases such as
7271 template <int> class C {};
7272 template <class T, template <T> class TT> class D {};
7275 i.e. the parameter list of TT depends on earlier parameters. */
7276 if (!uses_template_parms (TREE_TYPE (arg
)))
7278 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7279 if (!uses_template_parms (t
)
7280 && !same_type_p (t
, TREE_TYPE (arg
)))
7284 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7285 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7286 /* Argument is a parameter pack but parameter is not. */
7298 /* Coerce template argument list ARGLIST for use with template
7299 template-parameter TEMPL. */
7302 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7303 tsubst_flags_t complain
)
7305 /* Consider an example where a template template parameter declared as
7307 template <class T, class U = std::allocator<T> > class TT
7309 The template parameter level of T and U are one level larger than
7310 of TT. To proper process the default argument of U, say when an
7311 instantiation `TT<int>' is seen, we need to build the full
7312 arguments containing {int} as the innermost level. Outer levels,
7313 available when not appearing as default template argument, can be
7314 obtained from the arguments of the enclosing template.
7316 Suppose that TT is later substituted with std::vector. The above
7317 instantiation is `TT<int, std::allocator<T> >' with TT at
7318 level 1, and T at level 2, while the template arguments at level 1
7319 becomes {std::vector} and the inner level 2 is {int}. */
7321 tree outer
= DECL_CONTEXT (templ
);
7324 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7325 /* We want arguments for the partial specialization, not arguments for
7326 the primary template. */
7327 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7329 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7331 else if (current_template_parms
)
7333 /* This is an argument of the current template, so we haven't set
7334 DECL_CONTEXT yet. */
7335 tree relevant_template_parms
;
7337 /* Parameter levels that are greater than the level of the given
7338 template template parm are irrelevant. */
7339 relevant_template_parms
= current_template_parms
;
7340 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7341 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7342 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7344 outer
= template_parms_to_args (relevant_template_parms
);
7348 arglist
= add_to_template_args (outer
, arglist
);
7350 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7351 return coerce_template_parms (parmlist
, arglist
, templ
,
7353 /*require_all_args=*/true,
7354 /*use_default_args=*/true);
7357 /* A cache of template template parameters with match-all default
7359 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7361 store_defaulted_ttp (tree v
, tree t
)
7363 if (!defaulted_ttp_cache
)
7364 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7365 defaulted_ttp_cache
->put (v
, t
);
7368 lookup_defaulted_ttp (tree v
)
7370 if (defaulted_ttp_cache
)
7371 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7376 /* T is a bound template template-parameter. Copy its arguments into default
7377 arguments of the template template-parameter's template parameters. */
7380 add_defaults_to_ttp (tree otmpl
)
7382 if (tree c
= lookup_defaulted_ttp (otmpl
))
7385 tree ntmpl
= copy_node (otmpl
);
7387 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7388 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7389 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7390 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7391 TYPE_NAME (ntype
) = ntmpl
;
7392 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7394 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7395 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7396 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7397 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7399 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7400 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7401 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7402 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7403 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7405 tree o
= TREE_VEC_ELT (vec
, i
);
7406 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7408 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7409 TREE_PURPOSE (n
) = any_targ_node
;
7413 store_defaulted_ttp (otmpl
, ntmpl
);
7417 /* ARG is a bound potential template template-argument, and PARGS is a list
7418 of arguments for the corresponding template template-parameter. Adjust
7419 PARGS as appropriate for application to ARG's template, and if ARG is a
7420 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7421 arguments to the template template parameter. */
7424 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7426 ++processing_template_decl
;
7427 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7428 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7430 /* When comparing two template template-parameters in partial ordering,
7431 rewrite the one currently being used as an argument to have default
7432 arguments for all parameters. */
7433 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7434 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7435 if (pargs
!= error_mark_node
)
7436 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7437 TYPE_TI_ARGS (arg
));
7442 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7443 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7444 /*require_all*/true,
7445 /*use_default*/true);
7447 --processing_template_decl
;
7451 /* Subroutine of unify for the case when PARM is a
7452 BOUND_TEMPLATE_TEMPLATE_PARM. */
7455 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7458 tree parmvec
= TYPE_TI_ARGS (parm
);
7459 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7461 /* The template template parm might be variadic and the argument
7462 not, so flatten both argument lists. */
7463 parmvec
= expand_template_argument_pack (parmvec
);
7464 argvec
= expand_template_argument_pack (argvec
);
7468 /* In keeping with P0522R0, adjust P's template arguments
7469 to apply to A's template; then flatten it again. */
7470 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7471 nparmvec
= expand_template_argument_pack (nparmvec
);
7473 if (unify (tparms
, targs
, nparmvec
, argvec
,
7474 UNIFY_ALLOW_NONE
, explain_p
))
7477 /* If the P0522 adjustment eliminated a pack expansion, deduce
7480 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7481 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7482 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7487 /* Deduce arguments T, i from TT<T> or TT<i>.
7488 We check each element of PARMVEC and ARGVEC individually
7489 rather than the whole TREE_VEC since they can have
7490 different number of elements, which is allowed under N2555. */
7492 int len
= TREE_VEC_LENGTH (parmvec
);
7494 /* Check if the parameters end in a pack, making them
7496 int parm_variadic_p
= 0;
7498 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7499 parm_variadic_p
= 1;
7501 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7502 /* If the template argument list of P contains a pack
7503 expansion that is not the last template argument, the
7504 entire template argument list is a non-deduced
7506 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7507 return unify_success (explain_p
);
7509 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7510 return unify_too_few_arguments (explain_p
,
7511 TREE_VEC_LENGTH (argvec
), len
);
7513 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7514 if (unify (tparms
, targs
,
7515 TREE_VEC_ELT (parmvec
, i
),
7516 TREE_VEC_ELT (argvec
, i
),
7517 UNIFY_ALLOW_NONE
, explain_p
))
7521 && unify_pack_expansion (tparms
, targs
,
7524 /*subr=*/true, explain_p
))
7531 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7532 template template parameters. Both PARM_PARMS and ARG_PARMS are
7533 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7536 Consider the example:
7537 template <class T> class A;
7538 template<template <class U> class TT> class B;
7540 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7541 the parameters to A, and OUTER_ARGS contains A. */
7544 coerce_template_template_parms (tree parm_parms
,
7546 tsubst_flags_t complain
,
7550 int nparms
, nargs
, i
;
7554 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7555 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7557 nparms
= TREE_VEC_LENGTH (parm_parms
);
7558 nargs
= TREE_VEC_LENGTH (arg_parms
);
7562 /* P0522R0: A template template-parameter P is at least as specialized as
7563 a template template-argument A if, given the following rewrite to two
7564 function templates, the function template corresponding to P is at
7565 least as specialized as the function template corresponding to A
7566 according to the partial ordering rules for function templates
7567 ([temp.func.order]). Given an invented class template X with the
7568 template parameter list of A (including default arguments):
7570 * Each of the two function templates has the same template parameters,
7571 respectively, as P or A.
7573 * Each function template has a single function parameter whose type is
7574 a specialization of X with template arguments corresponding to the
7575 template parameters from the respective function template where, for
7576 each template parameter PP in the template parameter list of the
7577 function template, a corresponding template argument AA is formed. If
7578 PP declares a parameter pack, then AA is the pack expansion
7579 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7581 If the rewrite produces an invalid type, then P is not at least as
7582 specialized as A. */
7584 /* So coerce P's args to apply to A's parms, and then deduce between A's
7585 args and the converted args. If that succeeds, A is at least as
7586 specialized as P, so they match.*/
7587 tree pargs
= template_parms_level_to_args (parm_parms
);
7588 pargs
= add_outermost_template_args (outer_args
, pargs
);
7589 ++processing_template_decl
;
7590 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7591 /*require_all*/true, /*use_default*/true);
7592 --processing_template_decl
;
7593 if (pargs
!= error_mark_node
)
7595 tree targs
= make_tree_vec (nargs
);
7596 tree aargs
= template_parms_level_to_args (arg_parms
);
7597 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7603 /* Determine whether we have a parameter pack at the end of the
7604 template template parameter's template parameter list. */
7605 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7607 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7609 if (error_operand_p (parm
))
7612 switch (TREE_CODE (parm
))
7616 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7621 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7631 && !(variadic_p
&& nargs
>= nparms
- 1))
7634 /* Check all of the template parameters except the parameter pack at
7635 the end (if any). */
7636 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7638 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7639 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7642 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7643 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7645 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7653 /* Check each of the template parameters in the template
7654 argument against the template parameter pack at the end of
7655 the template template parameter. */
7656 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7659 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7661 for (; i
< nargs
; ++i
)
7663 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7666 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7668 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7677 /* Verifies that the deduced template arguments (in TARGS) for the
7678 template template parameters (in TPARMS) represent valid bindings,
7679 by comparing the template parameter list of each template argument
7680 to the template parameter list of its corresponding template
7681 template parameter, in accordance with DR150. This
7682 routine can only be called after all template arguments have been
7683 deduced. It will return TRUE if all of the template template
7684 parameter bindings are okay, FALSE otherwise. */
7686 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7688 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7691 /* We're dealing with template parms in this process. */
7692 ++processing_template_decl
;
7694 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7696 for (i
= 0; i
< ntparms
; ++i
)
7698 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7699 tree targ
= TREE_VEC_ELT (targs
, i
);
7701 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7703 tree packed_args
= NULL_TREE
;
7706 if (ARGUMENT_PACK_P (targ
))
7708 /* Look inside the argument pack. */
7709 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7710 len
= TREE_VEC_LENGTH (packed_args
);
7713 for (idx
= 0; idx
< len
; ++idx
)
7715 tree targ_parms
= NULL_TREE
;
7718 /* Extract the next argument from the argument
7720 targ
= TREE_VEC_ELT (packed_args
, idx
);
7722 if (PACK_EXPANSION_P (targ
))
7723 /* Look at the pattern of the pack expansion. */
7724 targ
= PACK_EXPANSION_PATTERN (targ
);
7726 /* Extract the template parameters from the template
7728 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7729 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7730 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7731 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7733 /* Verify that we can coerce the template template
7734 parameters from the template argument to the template
7735 parameter. This requires an exact match. */
7737 && !coerce_template_template_parms
7738 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7753 --processing_template_decl
;
7757 /* Since type attributes aren't mangled, we need to strip them from
7758 template type arguments. */
7761 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7763 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7765 bool removed_attributes
= false;
7766 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7767 if (removed_attributes
7768 && (complain
& tf_warning
))
7769 warning (OPT_Wignored_attributes
,
7770 "ignoring attributes on template argument %qT", arg
);
7774 /* And from inside dependent non-type arguments like sizeof(Type). */
7777 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7779 if (!arg
|| arg
== error_mark_node
)
7781 bool removed_attributes
= false;
7782 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7783 if (removed_attributes
7784 && (complain
& tf_warning
))
7785 warning (OPT_Wignored_attributes
,
7786 "ignoring attributes in template argument %qE", arg
);
7790 // A template declaration can be substituted for a constrained
7791 // template template parameter only when the argument is more
7792 // constrained than the parameter.
7794 is_compatible_template_arg (tree parm
, tree arg
)
7796 tree parm_cons
= get_constraints (parm
);
7798 /* For now, allow constrained template template arguments
7799 and unconstrained template template parameters. */
7800 if (parm_cons
== NULL_TREE
)
7803 tree arg_cons
= get_constraints (arg
);
7805 /* If the template parameter is constrained, we need to rewrite its
7806 constraints in terms of the ARG's template parameters. This ensures
7807 that all of the template parameter types will have the same depth.
7809 Note that this is only valid when coerce_template_template_parm is
7810 true for the innermost template parameters of PARM and ARG. In other
7811 words, because coercion is successful, this conversion will be valid. */
7814 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7815 parm_cons
= tsubst_constraint_info (parm_cons
,
7816 INNERMOST_TEMPLATE_ARGS (args
),
7817 tf_none
, NULL_TREE
);
7818 if (parm_cons
== error_mark_node
)
7822 return subsumes (parm_cons
, arg_cons
);
7825 // Convert a placeholder argument into a binding to the original
7826 // parameter. The original parameter is saved as the TREE_TYPE of
7829 convert_wildcard_argument (tree parm
, tree arg
)
7831 TREE_TYPE (arg
) = parm
;
7835 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7836 because one of them is dependent. But we need to represent the
7837 conversion for the benefit of cp_tree_equal. */
7840 maybe_convert_nontype_argument (tree type
, tree arg
)
7842 /* Auto parms get no conversion. */
7843 if (type_uses_auto (type
))
7845 /* We don't need or want to add this conversion now if we're going to use the
7846 argument for deduction. */
7847 if (value_dependent_expression_p (arg
))
7850 type
= cv_unqualified (type
);
7851 tree argtype
= TREE_TYPE (arg
);
7852 if (same_type_p (type
, argtype
))
7855 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7856 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7860 /* Convert the indicated template ARG as necessary to match the
7861 indicated template PARM. Returns the converted ARG, or
7862 error_mark_node if the conversion was unsuccessful. Error and
7863 warning messages are issued under control of COMPLAIN. This
7864 conversion is for the Ith parameter in the parameter list. ARGS is
7865 the full set of template arguments deduced so far. */
7868 convert_template_argument (tree parm
,
7871 tsubst_flags_t complain
,
7877 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7879 if (parm
== error_mark_node
|| error_operand_p (arg
))
7880 return error_mark_node
;
7882 /* Trivially convert placeholders. */
7883 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7884 return convert_wildcard_argument (parm
, arg
);
7886 if (arg
== any_targ_node
)
7889 if (TREE_CODE (arg
) == TREE_LIST
7890 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7892 /* The template argument was the name of some
7893 member function. That's usually
7894 invalid, but static members are OK. In any
7895 case, grab the underlying fields/functions
7896 and issue an error later if required. */
7897 TREE_TYPE (arg
) = unknown_type_node
;
7902 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7903 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7904 || requires_tmpl_type
);
7906 /* When determining whether an argument pack expansion is a template,
7907 look at the pattern. */
7908 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7909 arg
= PACK_EXPANSION_PATTERN (arg
);
7911 /* Deal with an injected-class-name used as a template template arg. */
7912 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7914 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7915 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7917 if (cxx_dialect
>= cxx11
)
7918 /* OK under DR 1004. */;
7919 else if (complain
& tf_warning_or_error
)
7920 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7921 " used as template template argument", TYPE_NAME (arg
));
7922 else if (flag_pedantic_errors
)
7930 ((TREE_CODE (arg
) == TEMPLATE_DECL
7931 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7932 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7933 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7934 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7937 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7938 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7939 arg
= TYPE_STUB_DECL (arg
);
7941 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7943 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7944 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7946 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7948 if (complain
& tf_error
)
7949 error ("invalid use of destructor %qE as a type", orig_arg
);
7950 return error_mark_node
;
7953 permerror (input_location
,
7954 "to refer to a type member of a template parameter, "
7955 "use %<typename %E%>", orig_arg
);
7957 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7958 TREE_OPERAND (arg
, 1),
7964 if (is_type
!= requires_type
)
7968 if (complain
& tf_error
)
7970 error ("type/value mismatch at argument %d in template "
7971 "parameter list for %qD",
7975 /* The template argument is a type, but we're expecting
7977 inform (input_location
,
7978 " expected a constant of type %qT, got %qT",
7980 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7981 /* [temp.arg]/2: "In a template-argument, an ambiguity
7982 between a type-id and an expression is resolved to a
7983 type-id, regardless of the form of the corresponding
7984 template-parameter." So give the user a clue. */
7985 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
7986 inform (input_location
, " ambiguous template argument "
7987 "for non-type template parameter is treated as "
7990 else if (requires_tmpl_type
)
7991 inform (input_location
,
7992 " expected a class template, got %qE", orig_arg
);
7994 inform (input_location
,
7995 " expected a type, got %qE", orig_arg
);
7998 return error_mark_node
;
8000 if (is_tmpl_type
^ requires_tmpl_type
)
8002 if (in_decl
&& (complain
& tf_error
))
8004 error ("type/value mismatch at argument %d in template "
8005 "parameter list for %qD",
8008 inform (input_location
,
8009 " expected a type, got %qT", DECL_NAME (arg
));
8011 inform (input_location
,
8012 " expected a class template, got %qT", orig_arg
);
8014 return error_mark_node
;
8017 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8018 /* We already did the appropriate conversion when packing args. */
8022 if (requires_tmpl_type
)
8024 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8025 /* The number of argument required is not known yet.
8026 Just accept it for now. */
8030 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8033 /* Strip alias templates that are equivalent to another
8035 arg
= get_underlying_template (arg
);
8036 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8038 if (coerce_template_template_parms (parmparm
, argparm
,
8044 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8046 if (val
!= error_mark_node
)
8048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8049 val
= TREE_TYPE (val
);
8050 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8051 val
= make_pack_expansion (val
, complain
);
8056 if (in_decl
&& (complain
& tf_error
))
8058 error ("type/value mismatch at argument %d in "
8059 "template parameter list for %qD",
8061 inform (input_location
,
8062 " expected a template of type %qD, got %qT",
8066 val
= error_mark_node
;
8069 // Check that the constraints are compatible before allowing the
8071 if (val
!= error_mark_node
)
8072 if (!is_compatible_template_arg (parm
, arg
))
8074 if (in_decl
&& (complain
& tf_error
))
8076 error ("constraint mismatch at argument %d in "
8077 "template parameter list for %qD",
8079 inform (input_location
, " expected %qD but got %qD",
8082 val
= error_mark_node
;
8088 /* We only form one instance of each template specialization.
8089 Therefore, if we use a non-canonical variant (i.e., a
8090 typedef), any future messages referring to the type will use
8091 the typedef, which is confusing if those future uses do not
8092 themselves also use the typedef. */
8094 val
= canonicalize_type_argument (val
, complain
);
8098 tree t
= TREE_TYPE (parm
);
8100 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8101 > TMPL_ARGS_DEPTH (args
))
8102 /* We don't have enough levels of args to do any substitution. This
8103 can happen in the context of -fnew-ttp-matching. */;
8104 else if (tree a
= type_uses_auto (t
))
8106 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8107 if (t
== error_mark_node
)
8108 return error_mark_node
;
8111 t
= tsubst (t
, args
, complain
, in_decl
);
8113 if (invalid_nontype_parm_type_p (t
, complain
))
8114 return error_mark_node
;
8116 if (t
!= TREE_TYPE (parm
))
8117 t
= canonicalize_type_argument (t
, complain
);
8119 if (!type_dependent_expression_p (orig_arg
)
8120 && !uses_template_parms (t
))
8121 /* We used to call digest_init here. However, digest_init
8122 will report errors, which we don't want when complain
8123 is zero. More importantly, digest_init will try too
8124 hard to convert things: for example, `0' should not be
8125 converted to pointer type at this point according to
8126 the standard. Accepting this is not merely an
8127 extension, since deciding whether or not these
8128 conversions can occur is part of determining which
8129 function template to call, or whether a given explicit
8130 argument specification is valid. */
8131 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8134 val
= canonicalize_expr_argument (orig_arg
, complain
);
8135 val
= maybe_convert_nontype_argument (t
, val
);
8139 if (val
== NULL_TREE
)
8140 val
= error_mark_node
;
8141 else if (val
== error_mark_node
&& (complain
& tf_error
))
8142 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8143 "could not convert template argument %qE from %qT to %qT",
8144 orig_arg
, TREE_TYPE (orig_arg
), t
);
8146 if (INDIRECT_REF_P (val
))
8148 /* Reject template arguments that are references to built-in
8149 functions with no library fallbacks. */
8150 const_tree inner
= TREE_OPERAND (val
, 0);
8151 const_tree innertype
= TREE_TYPE (inner
);
8153 && TYPE_REF_P (innertype
)
8154 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8155 && TREE_OPERAND_LENGTH (inner
) > 0
8156 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8157 return error_mark_node
;
8160 if (TREE_CODE (val
) == SCOPE_REF
)
8162 /* Strip typedefs from the SCOPE_REF. */
8163 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8164 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8166 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8167 QUALIFIED_NAME_IS_TEMPLATE (val
));
8174 /* Coerces the remaining template arguments in INNER_ARGS (from
8175 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8176 Returns the coerced argument pack. PARM_IDX is the position of this
8177 parameter in the template parameter list. ARGS is the original
8178 template argument list. */
8180 coerce_template_parameter_pack (tree parms
,
8188 tsubst_flags_t complain
)
8190 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8191 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8194 tree packed_parms
= NULL_TREE
;
8196 if (arg_idx
> nargs
)
8199 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8201 /* When the template parameter is a non-type template parameter pack
8202 or template template parameter pack whose type or template
8203 parameters use parameter packs, we know exactly how many arguments
8204 we are looking for. Build a vector of the instantiated decls for
8205 these template parameters in PACKED_PARMS. */
8206 /* We can't use make_pack_expansion here because it would interpret a
8207 _DECL as a use rather than a declaration. */
8208 tree decl
= TREE_VALUE (parm
);
8209 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8210 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8211 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8212 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8214 TREE_VEC_LENGTH (args
)--;
8215 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8216 TREE_VEC_LENGTH (args
)++;
8218 if (packed_parms
== error_mark_node
)
8219 return error_mark_node
;
8221 /* If we're doing a partial instantiation of a member template,
8222 verify that all of the types used for the non-type
8223 template parameter pack are, in fact, valid for non-type
8224 template parameters. */
8226 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8228 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8229 for (j
= 0; j
< len
; ++j
)
8231 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8232 if (TREE_CODE (t
) == PARM_DECL
8233 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8234 return error_mark_node
;
8236 /* We don't know how many args we have yet, just
8237 use the unconverted ones for now. */
8241 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8243 /* Check if we have a placeholder pack, which indicates we're
8244 in the context of a introduction list. In that case we want
8245 to match this pack to the single placeholder. */
8246 else if (arg_idx
< nargs
8247 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8248 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8250 nargs
= arg_idx
+ 1;
8251 packed_args
= make_tree_vec (1);
8254 packed_args
= make_tree_vec (nargs
- arg_idx
);
8256 /* Convert the remaining arguments, which will be a part of the
8257 parameter pack "parm". */
8258 int first_pack_arg
= arg_idx
;
8259 for (; arg_idx
< nargs
; ++arg_idx
)
8261 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8262 tree actual_parm
= TREE_VALUE (parm
);
8263 int pack_idx
= arg_idx
- first_pack_arg
;
8267 /* Once we've packed as many args as we have types, stop. */
8268 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8270 else if (PACK_EXPANSION_P (arg
))
8271 /* We don't know how many args we have yet, just
8272 use the unconverted ones for now. */
8275 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8278 if (arg
== error_mark_node
)
8280 if (complain
& tf_error
)
8281 error ("template argument %d is invalid", arg_idx
+ 1);
8284 arg
= convert_template_argument (actual_parm
,
8285 arg
, new_args
, complain
, parm_idx
,
8287 if (arg
== error_mark_node
)
8289 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8292 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8293 && TREE_VEC_LENGTH (packed_args
) > 0)
8295 if (complain
& tf_error
)
8296 error ("wrong number of template arguments (%d, should be %d)",
8297 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8298 return error_mark_node
;
8301 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8302 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8303 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8306 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8307 TREE_CONSTANT (argument_pack
) = 1;
8310 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8312 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8313 TREE_VEC_LENGTH (packed_args
));
8314 return argument_pack
;
8317 /* Returns the number of pack expansions in the template argument vector
8321 pack_expansion_args_count (tree args
)
8326 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8328 tree elt
= TREE_VEC_ELT (args
, i
);
8329 if (elt
&& PACK_EXPANSION_P (elt
))
8335 /* Convert all template arguments to their appropriate types, and
8336 return a vector containing the innermost resulting template
8337 arguments. If any error occurs, return error_mark_node. Error and
8338 warning messages are issued under control of COMPLAIN.
8340 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8341 for arguments not specified in ARGS. Otherwise, if
8342 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8343 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8344 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8348 coerce_template_parms (tree parms
,
8351 tsubst_flags_t complain
,
8352 bool require_all_args
,
8353 bool use_default_args
)
8355 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8356 tree orig_inner_args
;
8359 tree new_inner_args
;
8361 /* When used as a boolean value, indicates whether this is a
8362 variadic template parameter list. Since it's an int, we can also
8363 subtract it from nparms to get the number of non-variadic
8366 int variadic_args_p
= 0;
8367 int post_variadic_parms
= 0;
8369 /* Adjustment to nparms for fixed parameter packs. */
8370 int fixed_pack_adjust
= 0;
8371 int fixed_packs
= 0;
8374 /* Likewise for parameters with default arguments. */
8377 if (args
== error_mark_node
)
8378 return error_mark_node
;
8380 nparms
= TREE_VEC_LENGTH (parms
);
8382 /* Determine if there are any parameter packs or default arguments. */
8383 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8385 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8387 ++post_variadic_parms
;
8388 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8390 if (TREE_PURPOSE (parm
))
8394 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8395 /* If there are no parameters that follow a parameter pack, we need to
8396 expand any argument packs so that we can deduce a parameter pack from
8397 some non-packed args followed by an argument pack, as in variadic85.C.
8398 If there are such parameters, we need to leave argument packs intact
8399 so the arguments are assigned properly. This can happen when dealing
8400 with a nested class inside a partial specialization of a class
8401 template, as in variadic92.C, or when deducing a template parameter pack
8402 from a sub-declarator, as in variadic114.C. */
8403 if (!post_variadic_parms
)
8404 inner_args
= expand_template_argument_pack (inner_args
);
8406 /* Count any pack expansion args. */
8407 variadic_args_p
= pack_expansion_args_count (inner_args
);
8409 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8410 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8411 || (nargs
< nparms
- variadic_p
8414 && (!use_default_args
8415 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8416 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8419 if (complain
& tf_error
)
8421 if (variadic_p
|| default_p
)
8423 nparms
-= variadic_p
+ default_p
;
8424 error ("wrong number of template arguments "
8425 "(%d, should be at least %d)", nargs
, nparms
);
8428 error ("wrong number of template arguments "
8429 "(%d, should be %d)", nargs
, nparms
);
8432 inform (DECL_SOURCE_LOCATION (in_decl
),
8433 "provided for %qD", in_decl
);
8436 return error_mark_node
;
8438 /* We can't pass a pack expansion to a non-pack parameter of an alias
8439 template (DR 1430). */
8441 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8442 || concept_template_p (in_decl
))
8444 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8446 if (complain
& tf_error
)
8448 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8450 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8451 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8453 if (PACK_EXPANSION_P (arg
)
8454 && !template_parameter_pack_p (parm
))
8456 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8457 error_at (location_of (arg
),
8458 "pack expansion argument for non-pack parameter "
8459 "%qD of alias template %qD", parm
, in_decl
);
8461 error_at (location_of (arg
),
8462 "pack expansion argument for non-pack parameter "
8463 "%qD of concept %qD", parm
, in_decl
);
8464 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8471 return error_mark_node
;
8474 /* We need to evaluate the template arguments, even though this
8475 template-id may be nested within a "sizeof". */
8478 new_inner_args
= make_tree_vec (nparms
);
8479 new_args
= add_outermost_template_args (args
, new_inner_args
);
8480 int pack_adjust
= 0;
8481 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8486 /* Get the Ith template parameter. */
8487 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8489 if (parm
== error_mark_node
)
8491 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8495 /* Calculate the next argument. */
8496 if (arg_idx
< nargs
)
8497 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8501 if (template_parameter_pack_p (TREE_VALUE (parm
))
8502 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8503 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8505 /* Some arguments will be placed in the
8506 template parameter pack PARM. */
8507 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8508 inner_args
, arg_idx
,
8512 if (arg
== NULL_TREE
)
8514 /* We don't know how many args we have yet, just use the
8515 unconverted (and still packed) ones for now. */
8516 new_inner_args
= orig_inner_args
;
8521 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8523 /* Store this argument. */
8524 if (arg
== error_mark_node
)
8527 /* We are done with all of the arguments. */
8533 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8534 arg_idx
+= pack_adjust
;
8535 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8538 fixed_pack_adjust
+= pack_adjust
;
8546 if (PACK_EXPANSION_P (arg
))
8548 /* "If every valid specialization of a variadic template
8549 requires an empty template parameter pack, the template is
8550 ill-formed, no diagnostic required." So check that the
8551 pattern works with this parameter. */
8552 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8553 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8557 if (conv
== error_mark_node
)
8559 if (complain
& tf_error
)
8560 inform (input_location
, "so any instantiation with a "
8561 "non-empty parameter pack would be ill-formed");
8564 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8565 /* Recover from missing typename. */
8566 TREE_VEC_ELT (inner_args
, arg_idx
)
8567 = make_pack_expansion (conv
, complain
);
8569 /* We don't know how many args we have yet, just
8570 use the unconverted ones for now. */
8571 new_inner_args
= inner_args
;
8576 else if (require_all_args
)
8578 /* There must be a default arg in this case. */
8579 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8581 /* The position of the first default template argument,
8582 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8584 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8585 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8586 arg_idx
- pack_adjust
);
8591 if (arg
== error_mark_node
)
8593 if (complain
& tf_error
)
8594 error ("template argument %d is invalid", arg_idx
+ 1);
8598 /* This can occur if there was an error in the template
8599 parameter list itself (which we would already have
8600 reported) that we are trying to recover from, e.g., a class
8601 template with a parameter list such as
8602 template<typename..., typename> (cpp0x/variadic150.C). */
8605 /* This can also happen with a fixed parameter pack (71834). */
8606 if (arg_idx
>= nargs
)
8610 arg
= convert_template_argument (TREE_VALUE (parm
),
8611 arg
, new_args
, complain
,
8614 if (arg
== error_mark_node
)
8616 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8619 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8621 /* If we had fixed parameter packs, we didn't know how many arguments we
8622 actually needed earlier; now we do. */
8623 nparms
+= fixed_pack_adjust
;
8624 variadic_p
-= fixed_packs
;
8628 if (arg_idx
< nargs
)
8630 /* We had some pack expansion arguments that will only work if the packs
8631 are empty, but wait until instantiation time to complain.
8632 See variadic-ttp3.C. */
8633 int len
= nparms
+ (nargs
- arg_idx
);
8634 tree args
= make_tree_vec (len
);
8636 for (; i
< nparms
; ++i
)
8637 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8638 for (; i
< len
; ++i
, ++arg_idx
)
8639 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8640 arg_idx
- pack_adjust
);
8641 new_inner_args
= args
;
8646 gcc_assert (!(complain
& tf_error
) || seen_error ());
8647 return error_mark_node
;
8650 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8651 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8652 TREE_VEC_LENGTH (new_inner_args
));
8654 return new_inner_args
;
8657 /* Convert all template arguments to their appropriate types, and
8658 return a vector containing the innermost resulting template
8659 arguments. If any error occurs, return error_mark_node. Error and
8660 warning messages are not issued.
8662 Note that no function argument deduction is performed, and default
8663 arguments are used to fill in unspecified arguments. */
8665 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8667 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8670 /* Convert all template arguments to their appropriate type, and
8671 instantiate default arguments as needed. This returns a vector
8672 containing the innermost resulting template arguments, or
8673 error_mark_node if unsuccessful. */
8675 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8676 tsubst_flags_t complain
)
8678 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8681 /* Like coerce_template_parms. If PARMS represents all template
8682 parameters levels, this function returns a vector of vectors
8683 representing all the resulting argument levels. Note that in this
8684 case, only the innermost arguments are coerced because the
8685 outermost ones are supposed to have been coerced already.
8687 Otherwise, if PARMS represents only (the innermost) vector of
8688 parameters, this function returns a vector containing just the
8689 innermost resulting arguments. */
8692 coerce_innermost_template_parms (tree parms
,
8695 tsubst_flags_t complain
,
8696 bool require_all_args
,
8697 bool use_default_args
)
8699 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8700 int args_depth
= TMPL_ARGS_DEPTH (args
);
8703 if (parms_depth
> 1)
8705 coerced_args
= make_tree_vec (parms_depth
);
8709 for (level
= parms
, cur_depth
= parms_depth
;
8710 parms_depth
> 0 && level
!= NULL_TREE
;
8711 level
= TREE_CHAIN (level
), --cur_depth
)
8714 if (cur_depth
== args_depth
)
8715 l
= coerce_template_parms (TREE_VALUE (level
),
8716 args
, in_decl
, complain
,
8720 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8722 if (l
== error_mark_node
)
8723 return error_mark_node
;
8725 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8729 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8730 args
, in_decl
, complain
,
8733 return coerced_args
;
8736 /* Returns 1 if template args OT and NT are equivalent. */
8739 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8743 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8745 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8748 if (TREE_CODE (nt
) == TREE_VEC
)
8749 /* For member templates */
8750 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8751 else if (PACK_EXPANSION_P (ot
))
8752 return (PACK_EXPANSION_P (nt
)
8753 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8754 PACK_EXPANSION_PATTERN (nt
))
8755 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8756 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8757 else if (ARGUMENT_PACK_P (ot
))
8762 if (!ARGUMENT_PACK_P (nt
))
8765 opack
= ARGUMENT_PACK_ARGS (ot
);
8766 npack
= ARGUMENT_PACK_ARGS (nt
);
8767 len
= TREE_VEC_LENGTH (opack
);
8768 if (TREE_VEC_LENGTH (npack
) != len
)
8770 for (i
= 0; i
< len
; ++i
)
8771 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8772 TREE_VEC_ELT (npack
, i
)))
8776 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8778 else if (TYPE_P (nt
))
8782 /* Don't treat an alias template specialization with dependent
8783 arguments as equivalent to its underlying type when used as a
8784 template argument; we need them to be distinct so that we
8785 substitute into the specialization arguments at instantiation
8786 time. And aliases can't be equivalent without being ==, so
8787 we don't need to look any deeper.
8789 During partial ordering, however, we need to treat them normally so
8790 that we can order uses of the same alias with different
8791 cv-qualification (79960). */
8793 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8796 return same_type_p (ot
, nt
);
8798 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8802 /* Try to treat a template non-type argument that has been converted
8803 to the parameter type as equivalent to one that hasn't yet. */
8804 for (enum tree_code code1
= TREE_CODE (ot
);
8805 CONVERT_EXPR_CODE_P (code1
)
8806 || code1
== NON_LVALUE_EXPR
;
8807 code1
= TREE_CODE (ot
))
8808 ot
= TREE_OPERAND (ot
, 0);
8809 for (enum tree_code code2
= TREE_CODE (nt
);
8810 CONVERT_EXPR_CODE_P (code2
)
8811 || code2
== NON_LVALUE_EXPR
;
8812 code2
= TREE_CODE (nt
))
8813 nt
= TREE_OPERAND (nt
, 0);
8815 return cp_tree_equal (ot
, nt
);
8819 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8820 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8821 NEWARG_PTR with the offending arguments if they are non-NULL. */
8824 comp_template_args (tree oldargs
, tree newargs
,
8825 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8830 if (oldargs
== newargs
)
8833 if (!oldargs
|| !newargs
)
8836 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8839 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8841 tree nt
= TREE_VEC_ELT (newargs
, i
);
8842 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8844 if (! template_args_equal (ot
, nt
, partial_order
))
8846 if (oldarg_ptr
!= NULL
)
8848 if (newarg_ptr
!= NULL
)
8857 comp_template_args_porder (tree oargs
, tree nargs
)
8859 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8862 /* Implement a freelist interface for objects of type T.
8864 Head is a separate object, rather than a regular member, so that we
8865 can define it as a GTY deletable pointer, which is highly
8866 desirable. A data member could be declared that way, but then the
8867 containing object would implicitly get GTY((user)), which would
8868 prevent us from instantiating freelists as global objects.
8869 Although this way we can create freelist global objects, they're
8870 such thin wrappers that instantiating temporaries at every use
8871 loses nothing and saves permanent storage for the freelist object.
8873 Member functions next, anew, poison and reinit have default
8874 implementations that work for most of the types we're interested
8875 in, but if they don't work for some type, they should be explicitly
8876 specialized. See the comments before them for requirements, and
8877 the example specializations for the tree_list_freelist. */
8878 template <typename T
>
8881 /* Return the next object in a chain. We could just do type
8882 punning, but if we access the object with its underlying type, we
8883 avoid strict-aliasing trouble. This needs only work between
8884 poison and reinit. */
8885 static T
*&next (T
*obj
) { return obj
->next
; }
8887 /* Return a newly allocated, uninitialized or minimally-initialized
8888 object of type T. Any initialization performed by anew should
8889 either remain across the life of the object and the execution of
8890 poison, or be redone by reinit. */
8891 static T
*anew () { return ggc_alloc
<T
> (); }
8893 /* Optionally scribble all over the bits holding the object, so that
8894 they become (mostly?) uninitialized memory. This is called while
8895 preparing to make the object part of the free list. */
8896 static void poison (T
*obj
) {
8897 T
*p ATTRIBUTE_UNUSED
= obj
;
8898 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8900 #ifdef ENABLE_GC_CHECKING
8901 /* Poison the data, to indicate the data is garbage. */
8902 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
8903 memset (p
, 0xa5, sizeof (*p
));
8905 /* Let valgrind know the object is free. */
8906 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
8908 /* Let valgrind know the next portion of the object is available,
8909 but uninitialized. */
8910 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8913 /* Bring an object that underwent at least one lifecycle after anew
8914 and before the most recent free and poison, back to a usable
8915 state, reinitializing whatever is needed for it to be
8916 functionally equivalent to an object just allocated and returned
8917 by anew. This may poison or clear the next field, used by
8918 freelist housekeeping after poison was called. */
8919 static void reinit (T
*obj
) {
8920 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8922 #ifdef ENABLE_GC_CHECKING
8923 memset (q
, 0xa5, sizeof (*q
));
8925 /* Let valgrind know the entire object is available, but
8927 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
8930 /* Reference a GTY-deletable pointer that points to the first object
8931 in the free list proper. */
8934 /* Construct a freelist object chaining objects off of HEAD. */
8935 freelist (T
*&head
) : head(head
) {}
8937 /* Add OBJ to the free object list. The former head becomes OBJ's
8946 /* Take an object from the free list, if one is available, or
8947 allocate a new one. Objects taken from the free list should be
8948 regarded as filled with garbage, except for bits that are
8949 configured to be preserved across free and alloc. */
8964 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8965 want to allocate a TREE_LIST using the usual interface, and ensure
8966 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8967 build_tree_list logic in reinit, so this could go out of sync. */
8970 freelist
<tree_node
>::next (tree obj
)
8972 return TREE_CHAIN (obj
);
8976 freelist
<tree_node
>::anew ()
8978 return build_tree_list (NULL
, NULL
);
8982 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
8984 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
8985 tree p ATTRIBUTE_UNUSED
= obj
;
8986 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8987 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
8989 #ifdef ENABLE_GC_CHECKING
8990 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8992 /* Poison the data, to indicate the data is garbage. */
8993 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
8994 memset (p
, 0xa5, size
);
8996 /* Let valgrind know the object is free. */
8997 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
8998 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8999 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9000 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9002 #ifdef ENABLE_GC_CHECKING
9003 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9004 /* Keep TREE_CHAIN functional. */
9005 TREE_SET_CODE (obj
, TREE_LIST
);
9007 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9012 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9014 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9016 #ifdef ENABLE_GC_CHECKING
9017 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9018 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9019 memset (obj
, 0, sizeof (tree_list
));
9022 /* Let valgrind know the entire object is available, but
9024 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9026 #ifdef ENABLE_GC_CHECKING
9027 TREE_SET_CODE (obj
, TREE_LIST
);
9029 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9033 /* Point to the first object in the TREE_LIST freelist. */
9034 static GTY((deletable
)) tree tree_list_freelist_head
;
9035 /* Return the/an actual TREE_LIST freelist. */
9036 static inline freelist
<tree_node
>
9037 tree_list_freelist ()
9039 return tree_list_freelist_head
;
9042 /* Point to the first object in the tinst_level freelist. */
9043 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9044 /* Return the/an actual tinst_level freelist. */
9045 static inline freelist
<tinst_level
>
9046 tinst_level_freelist ()
9048 return tinst_level_freelist_head
;
9051 /* Point to the first object in the pending_template freelist. */
9052 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9053 /* Return the/an actual pending_template freelist. */
9054 static inline freelist
<pending_template
>
9055 pending_template_freelist ()
9057 return pending_template_freelist_head
;
9060 /* Build the TREE_LIST object out of a split list, store it
9061 permanently, and return it. */
9063 tinst_level::to_list ()
9065 gcc_assert (split_list_p ());
9066 tree ret
= tree_list_freelist ().alloc ();
9067 TREE_PURPOSE (ret
) = tldcl
;
9068 TREE_VALUE (ret
) = targs
;
9071 gcc_assert (tree_list_p ());
9075 const unsigned short tinst_level::refcount_infinity
;
9077 /* Increment OBJ's refcount unless it is already infinite. */
9078 static tinst_level
*
9079 inc_refcount_use (tinst_level
*obj
)
9081 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9086 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9088 tinst_level::free (tinst_level
*obj
)
9090 if (obj
->tree_list_p ())
9091 tree_list_freelist ().free (obj
->get_node ());
9092 tinst_level_freelist ().free (obj
);
9095 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9096 OBJ's DECL and OBJ, and start over with the tinst_level object that
9097 used to be referenced by OBJ's NEXT. */
9099 dec_refcount_use (tinst_level
*obj
)
9102 && obj
->refcount
!= tinst_level::refcount_infinity
9103 && !--obj
->refcount
)
9105 tinst_level
*next
= obj
->next
;
9106 tinst_level::free (obj
);
9111 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9112 and of the former PTR. Omitting the second argument is equivalent
9113 to passing (T*)NULL; this is allowed because passing the
9114 zero-valued integral constant NULL confuses type deduction and/or
9115 overload resolution. */
9116 template <typename T
>
9118 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9121 ptr
= inc_refcount_use (obj
);
9122 dec_refcount_use (save
);
9126 add_pending_template (tree d
)
9128 tree ti
= (TYPE_P (d
)
9129 ? CLASSTYPE_TEMPLATE_INFO (d
)
9130 : DECL_TEMPLATE_INFO (d
));
9131 struct pending_template
*pt
;
9134 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9137 /* We are called both from instantiate_decl, where we've already had a
9138 tinst_level pushed, and instantiate_template, where we haven't.
9140 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9141 level
= !current_tinst_level
9142 || current_tinst_level
->maybe_get_node () != d
;
9145 push_tinst_level (d
);
9147 pt
= pending_template_freelist ().alloc ();
9150 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9151 if (last_pending_template
)
9152 last_pending_template
->next
= pt
;
9154 pending_templates
= pt
;
9156 last_pending_template
= pt
;
9158 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9165 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9166 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9167 documentation for TEMPLATE_ID_EXPR. */
9170 lookup_template_function (tree fns
, tree arglist
)
9172 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9173 return error_mark_node
;
9175 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9177 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9179 error ("%q#D is not a function template", fns
);
9180 return error_mark_node
;
9183 if (BASELINK_P (fns
))
9185 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9187 BASELINK_FUNCTIONS (fns
),
9192 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9195 /* Within the scope of a template class S<T>, the name S gets bound
9196 (in build_self_reference) to a TYPE_DECL for the class, not a
9197 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9198 or one of its enclosing classes, and that type is a template,
9199 return the associated TEMPLATE_DECL. Otherwise, the original
9202 Also handle the case when DECL is a TREE_LIST of ambiguous
9203 injected-class-names from different bases. */
9206 maybe_get_template_decl_from_type_decl (tree decl
)
9208 if (decl
== NULL_TREE
)
9211 /* DR 176: A lookup that finds an injected-class-name (10.2
9212 [class.member.lookup]) can result in an ambiguity in certain cases
9213 (for example, if it is found in more than one base class). If all of
9214 the injected-class-names that are found refer to specializations of
9215 the same class template, and if the name is followed by a
9216 template-argument-list, the reference refers to the class template
9217 itself and not a specialization thereof, and is not ambiguous. */
9218 if (TREE_CODE (decl
) == TREE_LIST
)
9220 tree t
, tmpl
= NULL_TREE
;
9221 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9223 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9226 else if (tmpl
!= elt
)
9229 if (tmpl
&& t
== NULL_TREE
)
9235 return (decl
!= NULL_TREE
9236 && DECL_SELF_REFERENCE_P (decl
)
9237 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9238 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9241 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9242 parameters, find the desired type.
9244 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9246 IN_DECL, if non-NULL, is the template declaration we are trying to
9249 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9250 the class we are looking up.
9252 Issue error and warning messages under control of COMPLAIN.
9254 If the template class is really a local class in a template
9255 function, then the FUNCTION_CONTEXT is the function in which it is
9258 ??? Note that this function is currently called *twice* for each
9259 template-id: the first time from the parser, while creating the
9260 incomplete type (finish_template_type), and the second type during the
9261 real instantiation (instantiate_template_class). This is surely something
9262 that we want to avoid. It also causes some problems with argument
9263 coercion (see convert_nontype_argument for more information on this). */
9266 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9267 int entering_scope
, tsubst_flags_t complain
)
9269 tree templ
= NULL_TREE
, parmlist
;
9276 if (identifier_p (d1
))
9278 tree value
= innermost_non_namespace_value (d1
);
9279 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9284 push_decl_namespace (context
);
9285 templ
= lookup_name (d1
);
9286 templ
= maybe_get_template_decl_from_type_decl (templ
);
9288 pop_decl_namespace ();
9291 context
= DECL_CONTEXT (templ
);
9293 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9295 tree type
= TREE_TYPE (d1
);
9297 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9298 an implicit typename for the second A. Deal with it. */
9299 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9300 type
= TREE_TYPE (type
);
9302 if (CLASSTYPE_TEMPLATE_INFO (type
))
9304 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9305 d1
= DECL_NAME (templ
);
9308 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9309 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9311 templ
= TYPE_TI_TEMPLATE (d1
);
9312 d1
= DECL_NAME (templ
);
9314 else if (DECL_TYPE_TEMPLATE_P (d1
))
9317 d1
= DECL_NAME (templ
);
9318 context
= DECL_CONTEXT (templ
);
9320 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9323 d1
= DECL_NAME (templ
);
9326 /* Issue an error message if we didn't find a template. */
9329 if (complain
& tf_error
)
9330 error ("%qT is not a template", d1
);
9331 return error_mark_node
;
9334 if (TREE_CODE (templ
) != TEMPLATE_DECL
9335 /* Make sure it's a user visible template, if it was named by
9337 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9338 && !PRIMARY_TEMPLATE_P (templ
)))
9340 if (complain
& tf_error
)
9342 error ("non-template type %qT used as a template", d1
);
9344 error ("for template declaration %q+D", in_decl
);
9346 return error_mark_node
;
9349 complain
&= ~tf_user
;
9351 /* An alias that just changes the name of a template is equivalent to the
9352 other template, so if any of the arguments are pack expansions, strip
9353 the alias to avoid problems with a pack expansion passed to a non-pack
9354 alias template parameter (DR 1430). */
9355 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9356 templ
= get_underlying_template (templ
);
9358 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9361 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9362 if (arglist2
== error_mark_node
9363 || (!uses_template_parms (arglist2
)
9364 && check_instantiated_args (templ
, arglist2
, complain
)))
9365 return error_mark_node
;
9367 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9372 tree template_type
= TREE_TYPE (templ
);
9375 tree found
= NULL_TREE
;
9378 int is_dependent_type
;
9379 int use_partial_inst_tmpl
= false;
9381 if (template_type
== error_mark_node
)
9382 /* An error occurred while building the template TEMPL, and a
9383 diagnostic has most certainly been emitted for that
9384 already. Let's propagate that error. */
9385 return error_mark_node
;
9387 gen_tmpl
= most_general_template (templ
);
9388 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9389 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9390 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9392 if (arg_depth
== 1 && parm_depth
> 1)
9394 /* We've been given an incomplete set of template arguments.
9397 template <class T> struct S1 {
9398 template <class U> struct S2 {};
9399 template <class U> struct S2<U*> {};
9402 we will be called with an ARGLIST of `U*', but the
9403 TEMPLATE will be `template <class T> template
9404 <class U> struct S1<T>::S2'. We must fill in the missing
9406 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9407 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9408 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9411 /* Now we should have enough arguments. */
9412 gcc_assert (parm_depth
== arg_depth
);
9414 /* From here on, we're only interested in the most general
9417 /* Calculate the BOUND_ARGS. These will be the args that are
9418 actually tsubst'd into the definition to create the
9420 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9422 /*require_all_args=*/true,
9423 /*use_default_args=*/true);
9425 if (arglist
== error_mark_node
)
9426 /* We were unable to bind the arguments. */
9427 return error_mark_node
;
9429 /* In the scope of a template class, explicit references to the
9430 template class refer to the type of the template, not any
9431 instantiation of it. For example, in:
9433 template <class T> class C { void f(C<T>); }
9435 the `C<T>' is just the same as `C'. Outside of the
9436 class, however, such a reference is an instantiation. */
9438 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9439 || currently_open_class (template_type
))
9441 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9443 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9444 return template_type
;
9447 /* If we already have this specialization, return it. */
9448 elt
.tmpl
= gen_tmpl
;
9450 elt
.spec
= NULL_TREE
;
9451 hash
= spec_hasher::hash (&elt
);
9452 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9457 /* If the the template's constraints are not satisfied,
9458 then we cannot form a valid type.
9460 Note that the check is deferred until after the hash
9461 lookup. This prevents redundant checks on previously
9462 instantiated specializations. */
9463 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9465 if (complain
& tf_error
)
9467 auto_diagnostic_group d
;
9468 error ("template constraint failure");
9469 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9471 return error_mark_node
;
9474 is_dependent_type
= uses_template_parms (arglist
);
9476 /* If the deduced arguments are invalid, then the binding
9478 if (!is_dependent_type
9479 && check_instantiated_args (gen_tmpl
,
9480 INNERMOST_TEMPLATE_ARGS (arglist
),
9482 return error_mark_node
;
9484 if (!is_dependent_type
9485 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9486 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9487 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9489 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9490 DECL_NAME (gen_tmpl
),
9491 /*tag_scope=*/ts_global
);
9495 context
= DECL_CONTEXT (gen_tmpl
);
9496 if (context
&& TYPE_P (context
))
9498 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9499 context
= complete_type (context
);
9502 context
= tsubst (context
, arglist
, complain
, in_decl
);
9504 if (context
== error_mark_node
)
9505 return error_mark_node
;
9508 context
= global_namespace
;
9510 /* Create the type. */
9511 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9513 /* The user referred to a specialization of an alias
9514 template represented by GEN_TMPL.
9516 [temp.alias]/2 says:
9518 When a template-id refers to the specialization of an
9519 alias template, it is equivalent to the associated
9520 type obtained by substitution of its
9521 template-arguments for the template-parameters in the
9522 type-id of the alias template. */
9524 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9525 /* Note that the call above (by indirectly calling
9526 register_specialization in tsubst_decl) registers the
9527 TYPE_DECL representing the specialization of the alias
9528 template. So next time someone substitutes ARGLIST for
9529 the template parms into the alias template (GEN_TMPL),
9530 she'll get that TYPE_DECL back. */
9532 if (t
== error_mark_node
)
9535 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9537 if (!is_dependent_type
)
9539 set_current_access_from_decl (TYPE_NAME (template_type
));
9540 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9541 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9542 arglist
, complain
, in_decl
),
9543 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9544 arglist
, complain
, in_decl
),
9545 SCOPED_ENUM_P (template_type
), NULL
);
9547 if (t
== error_mark_node
)
9552 /* We don't want to call start_enum for this type, since
9553 the values for the enumeration constants may involve
9554 template parameters. And, no one should be interested
9555 in the enumeration constants for such a type. */
9556 t
= cxx_make_type (ENUMERAL_TYPE
);
9557 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9559 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9560 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9561 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9563 else if (CLASS_TYPE_P (template_type
))
9565 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9566 instantiated here. */
9567 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9569 t
= make_class_type (TREE_CODE (template_type
));
9570 CLASSTYPE_DECLARED_CLASS (t
)
9571 = CLASSTYPE_DECLARED_CLASS (template_type
);
9572 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9574 /* A local class. Make sure the decl gets registered properly. */
9575 if (context
== current_function_decl
)
9576 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9578 return error_mark_node
;
9580 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9581 /* This instantiation is another name for the primary
9582 template type. Set the TYPE_CANONICAL field
9584 TYPE_CANONICAL (t
) = template_type
;
9585 else if (any_template_arguments_need_structural_equality_p (arglist
))
9586 /* Some of the template arguments require structural
9587 equality testing, so this template class requires
9588 structural equality testing. */
9589 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9594 /* If we called start_enum or pushtag above, this information
9595 will already be set up. */
9598 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9600 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9601 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9602 DECL_SOURCE_LOCATION (type_decl
)
9603 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9606 type_decl
= TYPE_NAME (t
);
9608 if (CLASS_TYPE_P (template_type
))
9610 TREE_PRIVATE (type_decl
)
9611 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9612 TREE_PROTECTED (type_decl
)
9613 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9614 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9616 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9617 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9621 if (OVERLOAD_TYPE_P (t
)
9622 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9624 static const char *tags
[] = {"abi_tag", "may_alias"};
9626 for (unsigned ix
= 0; ix
!= 2; ix
++)
9629 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9633 = tree_cons (TREE_PURPOSE (attributes
),
9634 TREE_VALUE (attributes
),
9635 TYPE_ATTRIBUTES (t
));
9639 /* Let's consider the explicit specialization of a member
9640 of a class template specialization that is implicitly instantiated,
9645 template<class U> struct M {}; //#0
9650 struct S<int>::M<char> //#1
9654 [temp.expl.spec]/4 says this is valid.
9656 In this case, when we write:
9659 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9662 When we encounter #1, we want to store the partial instantiation
9663 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9665 For all cases other than this "explicit specialization of member of a
9666 class template", we just want to store the most general template into
9667 the CLASSTYPE_TI_TEMPLATE of M.
9669 This case of "explicit specialization of member of a class template"
9671 1/ the enclosing class is an instantiation of, and therefore not
9672 the same as, the context of the most general template, and
9673 2/ we aren't looking at the partial instantiation itself, i.e.
9674 the innermost arguments are not the same as the innermost parms of
9675 the most general template.
9677 So it's only when 1/ and 2/ happens that we want to use the partial
9678 instantiation of the member template in lieu of its most general
9681 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9682 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9683 /* the enclosing class must be an instantiation... */
9684 && CLASS_TYPE_P (context
)
9685 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9687 TREE_VEC_LENGTH (arglist
)--;
9688 ++processing_template_decl
;
9689 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9690 tree partial_inst_args
=
9691 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9692 arglist
, complain
, NULL_TREE
);
9693 --processing_template_decl
;
9694 TREE_VEC_LENGTH (arglist
)++;
9695 if (partial_inst_args
== error_mark_node
)
9696 return error_mark_node
;
9697 use_partial_inst_tmpl
=
9698 /*...and we must not be looking at the partial instantiation
9700 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9704 if (!use_partial_inst_tmpl
)
9705 /* This case is easy; there are no member templates involved. */
9709 /* This is a full instantiation of a member template. Find
9710 the partial instantiation of which this is an instance. */
9712 /* Temporarily reduce by one the number of levels in the ARGLIST
9713 so as to avoid comparing the last set of arguments. */
9714 TREE_VEC_LENGTH (arglist
)--;
9715 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9716 TREE_VEC_LENGTH (arglist
)++;
9717 /* FOUND is either a proper class type, or an alias
9718 template specialization. In the later case, it's a
9719 TYPE_DECL, resulting from the substituting of arguments
9720 for parameters in the TYPE_DECL of the alias template
9721 done earlier. So be careful while getting the template
9723 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9725 : (TREE_CODE (found
) == TYPE_DECL
9726 ? DECL_TI_TEMPLATE (found
)
9727 : CLASSTYPE_TI_TEMPLATE (found
)));
9729 if (DECL_CLASS_TEMPLATE_P (found
)
9730 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
9732 /* If this partial instantiation is specialized, we want to
9733 use it for hash table lookup. */
9735 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
9736 hash
= spec_hasher::hash (&elt
);
9740 // Build template info for the new specialization.
9741 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9744 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9745 gcc_checking_assert (*slot
== NULL
);
9746 entry
= ggc_alloc
<spec_entry
> ();
9750 /* Note this use of the partial instantiation so we can check it
9751 later in maybe_process_partial_specialization. */
9752 DECL_TEMPLATE_INSTANTIATIONS (found
)
9753 = tree_cons (arglist
, t
,
9754 DECL_TEMPLATE_INSTANTIATIONS (found
));
9756 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9757 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9758 /* Now that the type has been registered on the instantiations
9759 list, we set up the enumerators. Because the enumeration
9760 constants may involve the enumeration type itself, we make
9761 sure to register the type first, and then create the
9762 constants. That way, doing tsubst_expr for the enumeration
9763 constants won't result in recursive calls here; we'll find
9764 the instantiation and exit above. */
9765 tsubst_enum (template_type
, t
, arglist
);
9767 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9768 /* If the type makes use of template parameters, the
9769 code that generates debugging information will crash. */
9770 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9772 /* Possibly limit visibility based on template args. */
9773 TREE_PUBLIC (type_decl
) = 1;
9774 determine_visibility (type_decl
);
9776 inherit_targ_abi_tags (t
);
9782 /* Wrapper for lookup_template_class_1. */
9785 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9786 int entering_scope
, tsubst_flags_t complain
)
9789 timevar_push (TV_TEMPLATE_INST
);
9790 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9791 entering_scope
, complain
);
9792 timevar_pop (TV_TEMPLATE_INST
);
9796 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9799 lookup_template_variable (tree templ
, tree arglist
)
9801 /* The type of the expression is NULL_TREE since the template-id could refer
9802 to an explicit or partial specialization. */
9803 tree type
= NULL_TREE
;
9804 if (flag_concepts
&& variable_concept_p (templ
))
9805 /* Except that concepts are always bool. */
9806 type
= boolean_type_node
;
9807 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9810 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9813 finish_template_variable (tree var
, tsubst_flags_t complain
)
9815 tree templ
= TREE_OPERAND (var
, 0);
9816 tree arglist
= TREE_OPERAND (var
, 1);
9818 /* We never want to return a VAR_DECL for a variable concept, since they
9819 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9820 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9821 if (concept_p
&& processing_template_decl
)
9824 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9825 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9827 templ
= most_general_template (templ
);
9828 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9829 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9831 /*use_default*/true);
9833 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9835 if (complain
& tf_error
)
9837 auto_diagnostic_group d
;
9838 error ("use of invalid variable template %qE", var
);
9839 diagnose_constraints (location_of (var
), templ
, arglist
);
9841 return error_mark_node
;
9844 /* If a template-id refers to a specialization of a variable
9845 concept, then the expression is true if and only if the
9846 concept's constraints are satisfied by the given template
9849 NOTE: This is an extension of Concepts Lite TS that
9850 allows constraints to be used in expressions. */
9853 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9854 return evaluate_variable_concept (decl
, arglist
);
9857 return instantiate_template (templ
, arglist
, complain
);
9860 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9861 TARGS template args, and instantiate it if it's not dependent. */
9864 lookup_and_finish_template_variable (tree templ
, tree targs
,
9865 tsubst_flags_t complain
)
9867 templ
= lookup_template_variable (templ
, targs
);
9868 if (!any_dependent_template_arguments_p (targs
))
9870 templ
= finish_template_variable (templ
, complain
);
9874 return convert_from_reference (templ
);
9883 /* True when we should also visit template parameters that occur in
9884 non-deduced contexts. */
9885 bool include_nondeduced_p
;
9886 hash_set
<tree
> *visited
;
9889 /* Called from for_each_template_parm via walk_tree. */
9892 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9895 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9896 tree_fn_t fn
= pfd
->fn
;
9897 void *data
= pfd
->data
;
9898 tree result
= NULL_TREE
;
9900 #define WALK_SUBTREE(NODE) \
9903 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9904 pfd->include_nondeduced_p, \
9906 if (result) goto out; \
9910 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9914 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9915 WALK_SUBTREE (TYPE_CONTEXT (t
));
9917 switch (TREE_CODE (t
))
9920 if (TYPE_PTRMEMFUNC_P (t
))
9926 if (!TYPE_TEMPLATE_INFO (t
))
9929 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9933 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9934 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9938 /* Since we're not going to walk subtrees, we have to do this
9940 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9944 /* Check the return type. */
9945 WALK_SUBTREE (TREE_TYPE (t
));
9947 /* Check the parameter types. Since default arguments are not
9948 instantiated until they are needed, the TYPE_ARG_TYPES may
9949 contain expressions that involve template parameters. But,
9950 no-one should be looking at them yet. And, once they're
9951 instantiated, they don't contain template parameters, so
9952 there's no point in looking at them then, either. */
9956 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9957 WALK_SUBTREE (TREE_VALUE (parm
));
9959 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9960 want walk_tree walking into them itself. */
9964 if (flag_noexcept_type
)
9966 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9968 WALK_SUBTREE (TREE_PURPOSE (spec
));
9974 case UNDERLYING_TYPE
:
9975 if (pfd
->include_nondeduced_p
9976 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9978 pfd
->include_nondeduced_p
,
9980 return error_mark_node
;
9981 *walk_subtrees
= false;
9986 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9987 WALK_SUBTREE (DECL_TI_ARGS (t
));
9992 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9993 WALK_SUBTREE (DECL_INITIAL (t
));
9994 if (DECL_CONTEXT (t
)
9995 && pfd
->include_nondeduced_p
)
9996 WALK_SUBTREE (DECL_CONTEXT (t
));
9999 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10000 /* Record template parameters such as `T' inside `TT<T>'. */
10001 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10002 /* Fall through. */
10004 case TEMPLATE_TEMPLATE_PARM
:
10005 case TEMPLATE_TYPE_PARM
:
10006 case TEMPLATE_PARM_INDEX
:
10007 if (fn
&& (*fn
)(t
, data
))
10013 case TEMPLATE_DECL
:
10014 /* A template template parameter is encountered. */
10015 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10016 WALK_SUBTREE (TREE_TYPE (t
));
10018 /* Already substituted template template parameter */
10019 *walk_subtrees
= 0;
10022 case TYPENAME_TYPE
:
10023 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10024 partial instantiation. */
10025 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10029 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10030 && pfd
->include_nondeduced_p
)
10031 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10035 case COMPONENT_REF
:
10036 /* If there's no type, then this thing must be some expression
10037 involving template parameters. */
10038 if (!fn
&& !TREE_TYPE (t
))
10039 return error_mark_node
;
10044 case IMPLICIT_CONV_EXPR
:
10045 case REINTERPRET_CAST_EXPR
:
10046 case CONST_CAST_EXPR
:
10047 case STATIC_CAST_EXPR
:
10048 case DYNAMIC_CAST_EXPR
:
10052 case PSEUDO_DTOR_EXPR
:
10054 return error_mark_node
;
10061 #undef WALK_SUBTREE
10063 /* We didn't find any template parameters we liked. */
10068 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10069 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10070 call FN with the parameter and the DATA.
10071 If FN returns nonzero, the iteration is terminated, and
10072 for_each_template_parm returns 1. Otherwise, the iteration
10073 continues. If FN never returns a nonzero value, the value
10074 returned by for_each_template_parm is 0. If FN is NULL, it is
10075 considered to be the function which always returns 1.
10077 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10078 parameters that occur in non-deduced contexts. When false, only
10079 visits those template parameters that can be deduced. */
10082 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10083 hash_set
<tree
> *visited
,
10084 bool include_nondeduced_p
,
10087 struct pair_fn_data pfd
;
10092 pfd
.any_fn
= any_fn
;
10094 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10096 /* Walk the tree. (Conceptually, we would like to walk without
10097 duplicates, but for_each_template_parm_r recursively calls
10098 for_each_template_parm, so we would need to reorganize a fair
10099 bit to use walk_tree_without_duplicates, so we keep our own
10102 pfd
.visited
= visited
;
10104 pfd
.visited
= new hash_set
<tree
>;
10105 result
= cp_walk_tree (&t
,
10106 for_each_template_parm_r
,
10113 delete pfd
.visited
;
10120 /* Returns true if T depends on any template parameter. */
10123 uses_template_parms (tree t
)
10125 if (t
== NULL_TREE
)
10129 int saved_processing_template_decl
;
10131 saved_processing_template_decl
= processing_template_decl
;
10132 if (!saved_processing_template_decl
)
10133 processing_template_decl
= 1;
10135 dependent_p
= dependent_type_p (t
);
10136 else if (TREE_CODE (t
) == TREE_VEC
)
10137 dependent_p
= any_dependent_template_arguments_p (t
);
10138 else if (TREE_CODE (t
) == TREE_LIST
)
10139 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10140 || uses_template_parms (TREE_CHAIN (t
)));
10141 else if (TREE_CODE (t
) == TYPE_DECL
)
10142 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10143 else if (DECL_P (t
)
10145 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10146 || TREE_CODE (t
) == OVERLOAD
10148 || identifier_p (t
)
10149 || TREE_CODE (t
) == TRAIT_EXPR
10150 || TREE_CODE (t
) == CONSTRUCTOR
10151 || CONSTANT_CLASS_P (t
))
10152 dependent_p
= (type_dependent_expression_p (t
)
10153 || value_dependent_expression_p (t
));
10156 gcc_assert (t
== error_mark_node
);
10157 dependent_p
= false;
10160 processing_template_decl
= saved_processing_template_decl
;
10162 return dependent_p
;
10165 /* Returns true iff current_function_decl is an incompletely instantiated
10166 template. Useful instead of processing_template_decl because the latter
10167 is set to 0 during instantiate_non_dependent_expr. */
10170 in_template_function (void)
10172 tree fn
= current_function_decl
;
10174 ++processing_template_decl
;
10175 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10176 && DECL_TEMPLATE_INFO (fn
)
10177 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10178 --processing_template_decl
;
10182 /* Returns true if T depends on any template parameter with level LEVEL. */
10185 uses_template_parms_level (tree t
, int level
)
10187 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10188 /*include_nondeduced_p=*/true);
10191 /* Returns true if the signature of DECL depends on any template parameter from
10192 its enclosing class. */
10195 uses_outer_template_parms (tree decl
)
10197 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10200 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10201 &depth
, NULL
, /*include_nondeduced_p=*/true))
10203 if (PRIMARY_TEMPLATE_P (decl
)
10204 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10205 (DECL_TEMPLATE_PARMS (decl
)),
10206 template_parm_outer_level
,
10207 &depth
, NULL
, /*include_nondeduced_p=*/true))
10209 tree ci
= get_constraints (decl
);
10211 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10212 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10213 &depth
, NULL
, /*nondeduced*/true))
10218 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10219 ill-formed translation unit, i.e. a variable or function that isn't
10220 usable in a constant expression. */
10223 neglectable_inst_p (tree d
)
10225 return (d
&& DECL_P (d
)
10226 && !undeduced_auto_decl (d
)
10227 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10228 : decl_maybe_constant_var_p (d
)));
10231 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10232 neglectable and instantiated from within an erroneous instantiation. */
10235 limit_bad_template_recursion (tree decl
)
10237 struct tinst_level
*lev
= current_tinst_level
;
10238 int errs
= errorcount
+ sorrycount
;
10239 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10242 for (; lev
; lev
= lev
->next
)
10243 if (neglectable_inst_p (lev
->maybe_get_node ()))
10246 return (lev
&& errs
> lev
->errors
);
10249 static int tinst_depth
;
10250 extern int max_tinst_depth
;
10253 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10255 /* We're starting to instantiate D; record the template instantiation context
10256 at LOC for diagnostics and to restore it later. */
10259 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10261 struct tinst_level
*new_level
;
10263 if (tinst_depth
>= max_tinst_depth
)
10265 /* Tell error.c not to try to instantiate any templates. */
10267 fatal_error (input_location
,
10268 "template instantiation depth exceeds maximum of %d"
10269 " (use %<-ftemplate-depth=%> to increase the maximum)",
10274 /* If the current instantiation caused problems, don't let it instantiate
10275 anything else. Do allow deduction substitution and decls usable in
10276 constant expressions. */
10277 if (!targs
&& limit_bad_template_recursion (tldcl
))
10280 /* When not -quiet, dump template instantiations other than functions, since
10281 announce_function will take care of those. */
10282 if (!quiet_flag
&& !targs
10283 && TREE_CODE (tldcl
) != TREE_LIST
10284 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10285 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10287 new_level
= tinst_level_freelist ().alloc ();
10288 new_level
->tldcl
= tldcl
;
10289 new_level
->targs
= targs
;
10290 new_level
->locus
= loc
;
10291 new_level
->errors
= errorcount
+ sorrycount
;
10292 new_level
->next
= NULL
;
10293 new_level
->refcount
= 0;
10294 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10295 set_refcount_ptr (current_tinst_level
, new_level
);
10298 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10299 depth_reached
= tinst_depth
;
10304 /* We're starting substitution of TMPL<ARGS>; record the template
10305 substitution context for diagnostics and to restore it later. */
10308 push_tinst_level (tree tmpl
, tree args
)
10310 return push_tinst_level_loc (tmpl
, args
, input_location
);
10313 /* We're starting to instantiate D; record INPUT_LOCATION and the
10314 template instantiation context for diagnostics and to restore it
10318 push_tinst_level (tree d
)
10320 return push_tinst_level_loc (d
, input_location
);
10323 /* Likewise, but record LOC as the program location. */
10326 push_tinst_level_loc (tree d
, location_t loc
)
10328 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10329 return push_tinst_level_loc (d
, NULL
, loc
);
10332 /* We're done instantiating this template; return to the instantiation
10336 pop_tinst_level (void)
10338 /* Restore the filename and line number stashed away when we started
10339 this instantiation. */
10340 input_location
= current_tinst_level
->locus
;
10341 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10345 /* We're instantiating a deferred template; restore the template
10346 instantiation context in which the instantiation was requested, which
10347 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10350 reopen_tinst_level (struct tinst_level
*level
)
10352 struct tinst_level
*t
;
10355 for (t
= level
; t
; t
= t
->next
)
10358 set_refcount_ptr (current_tinst_level
, level
);
10359 pop_tinst_level ();
10360 if (current_tinst_level
)
10361 current_tinst_level
->errors
= errorcount
+sorrycount
;
10362 return level
->maybe_get_node ();
10365 /* Returns the TINST_LEVEL which gives the original instantiation
10368 struct tinst_level
*
10369 outermost_tinst_level (void)
10371 struct tinst_level
*level
= current_tinst_level
;
10373 while (level
->next
)
10374 level
= level
->next
;
10378 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10379 vector of template arguments, as for tsubst.
10381 Returns an appropriate tsubst'd friend declaration. */
10384 tsubst_friend_function (tree decl
, tree args
)
10388 if (TREE_CODE (decl
) == FUNCTION_DECL
10389 && DECL_TEMPLATE_INSTANTIATION (decl
)
10390 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10391 /* This was a friend declared with an explicit template
10392 argument list, e.g.:
10394 friend void f<>(T);
10396 to indicate that f was a template instantiation, not a new
10397 function declaration. Now, we have to figure out what
10398 instantiation of what template. */
10400 tree template_id
, arglist
, fns
;
10403 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10405 /* Friend functions are looked up in the containing namespace scope.
10406 We must enter that scope, to avoid finding member functions of the
10407 current class with same name. */
10408 push_nested_namespace (ns
);
10409 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10410 tf_warning_or_error
, NULL_TREE
,
10411 /*integral_constant_expression_p=*/false);
10412 pop_nested_namespace (ns
);
10413 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10414 tf_warning_or_error
, NULL_TREE
);
10415 template_id
= lookup_template_function (fns
, arglist
);
10417 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10418 tmpl
= determine_specialization (template_id
, new_friend
,
10420 /*need_member_template=*/0,
10421 TREE_VEC_LENGTH (args
),
10423 return instantiate_template (tmpl
, new_args
, tf_error
);
10426 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10428 /* The NEW_FRIEND will look like an instantiation, to the
10429 compiler, but is not an instantiation from the point of view of
10430 the language. For example, we might have had:
10432 template <class T> struct S {
10433 template <class U> friend void f(T, U);
10436 Then, in S<int>, template <class U> void f(int, U) is not an
10437 instantiation of anything. */
10438 if (new_friend
== error_mark_node
)
10439 return error_mark_node
;
10441 DECL_USE_TEMPLATE (new_friend
) = 0;
10442 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10444 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10445 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10446 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10449 /* The mangled name for the NEW_FRIEND is incorrect. The function
10450 is not a template instantiation and should not be mangled like
10451 one. Therefore, we forget the mangling here; we'll recompute it
10452 later if we need it. */
10453 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10455 SET_DECL_RTL (new_friend
, NULL
);
10456 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10459 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10462 tree new_friend_template_info
;
10463 tree new_friend_result_template_info
;
10465 int new_friend_is_defn
;
10467 /* We must save some information from NEW_FRIEND before calling
10468 duplicate decls since that function will free NEW_FRIEND if
10470 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10471 new_friend_is_defn
=
10472 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10473 (template_for_substitution (new_friend
)))
10475 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10477 /* This declaration is a `primary' template. */
10478 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10480 new_friend_result_template_info
10481 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10484 new_friend_result_template_info
= NULL_TREE
;
10486 /* Inside pushdecl_namespace_level, we will push into the
10487 current namespace. However, the friend function should go
10488 into the namespace of the template. */
10489 ns
= decl_namespace_context (new_friend
);
10490 push_nested_namespace (ns
);
10491 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10492 pop_nested_namespace (ns
);
10494 if (old_decl
== error_mark_node
)
10495 return error_mark_node
;
10497 if (old_decl
!= new_friend
)
10499 /* This new friend declaration matched an existing
10500 declaration. For example, given:
10502 template <class T> void f(T);
10503 template <class U> class C {
10504 template <class T> friend void f(T) {}
10507 the friend declaration actually provides the definition
10508 of `f', once C has been instantiated for some type. So,
10509 old_decl will be the out-of-class template declaration,
10510 while new_friend is the in-class definition.
10512 But, if `f' was called before this point, the
10513 instantiation of `f' will have DECL_TI_ARGS corresponding
10514 to `T' but not to `U', references to which might appear
10515 in the definition of `f'. Previously, the most general
10516 template for an instantiation of `f' was the out-of-class
10517 version; now it is the in-class version. Therefore, we
10518 run through all specialization of `f', adding to their
10519 DECL_TI_ARGS appropriately. In particular, they need a
10520 new set of outer arguments, corresponding to the
10521 arguments for this class instantiation.
10523 The same situation can arise with something like this:
10525 friend void f(int);
10526 template <class T> class C {
10527 friend void f(T) {}
10530 when `C<int>' is instantiated. Now, `f(int)' is defined
10533 if (!new_friend_is_defn
)
10534 /* On the other hand, if the in-class declaration does
10535 *not* provide a definition, then we don't want to alter
10536 existing definitions. We can just leave everything
10541 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10542 tree new_args
= TI_ARGS (new_friend_template_info
);
10544 /* Overwrite whatever template info was there before, if
10545 any, with the new template information pertaining to
10546 the declaration. */
10547 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10549 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10551 /* We should have called reregister_specialization in
10552 duplicate_decls. */
10553 gcc_assert (retrieve_specialization (new_template
,
10557 /* Instantiate it if the global has already been used. */
10558 if (DECL_ODR_USED (old_decl
))
10559 instantiate_decl (old_decl
, /*defer_ok=*/true,
10560 /*expl_inst_class_mem_p=*/false);
10566 /* Indicate that the old function template is a partial
10568 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10569 = new_friend_result_template_info
;
10571 gcc_assert (new_template
10572 == most_general_template (new_template
));
10573 gcc_assert (new_template
!= old_decl
);
10575 /* Reassign any specializations already in the hash table
10576 to the new more general template, and add the
10577 additional template args. */
10578 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10580 t
= TREE_CHAIN (t
))
10582 tree spec
= TREE_VALUE (t
);
10585 elt
.tmpl
= old_decl
;
10586 elt
.args
= DECL_TI_ARGS (spec
);
10587 elt
.spec
= NULL_TREE
;
10589 decl_specializations
->remove_elt (&elt
);
10591 DECL_TI_ARGS (spec
)
10592 = add_outermost_template_args (new_args
,
10593 DECL_TI_ARGS (spec
));
10595 register_specialization
10596 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10599 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10603 /* The information from NEW_FRIEND has been merged into OLD_DECL
10604 by duplicate_decls. */
10605 new_friend
= old_decl
;
10610 tree context
= DECL_CONTEXT (new_friend
);
10614 template <class T> class C {
10615 template <class U> friend void C1<U>::f (); // case 1
10616 friend void C2<T>::f (); // case 2
10618 we only need to make sure CONTEXT is a complete type for
10619 case 2. To distinguish between the two cases, we note that
10620 CONTEXT of case 1 remains dependent type after tsubst while
10621 this isn't true for case 2. */
10622 ++processing_template_decl
;
10623 dependent_p
= dependent_type_p (context
);
10624 --processing_template_decl
;
10627 && !complete_type_or_else (context
, NULL_TREE
))
10628 return error_mark_node
;
10630 if (COMPLETE_TYPE_P (context
))
10632 tree fn
= new_friend
;
10633 /* do_friend adds the TEMPLATE_DECL for any member friend
10634 template even if it isn't a member template, i.e.
10635 template <class T> friend A<T>::f();
10636 Look through it in that case. */
10637 if (TREE_CODE (fn
) == TEMPLATE_DECL
10638 && !PRIMARY_TEMPLATE_P (fn
))
10639 fn
= DECL_TEMPLATE_RESULT (fn
);
10640 /* Check to see that the declaration is really present, and,
10641 possibly obtain an improved declaration. */
10642 fn
= check_classfn (context
, fn
, NULL_TREE
);
10652 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10653 template arguments, as for tsubst.
10655 Returns an appropriate tsubst'd friend type or error_mark_node on
10659 tsubst_friend_class (tree friend_tmpl
, tree args
)
10663 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10665 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10666 return TREE_TYPE (tmpl
);
10669 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10670 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10671 push_nested_namespace (context
);
10674 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
10675 push_nested_class (context
);
10678 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10679 /*non_class=*/false, /*block_p=*/false,
10680 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10682 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10684 /* The friend template has already been declared. Just
10685 check to see that the declarations match, and install any new
10686 default parameters. We must tsubst the default parameters,
10687 of course. We only need the innermost template parameters
10688 because that is all that redeclare_class_template will look
10690 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10691 > TMPL_ARGS_DEPTH (args
))
10693 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10694 args
, tf_warning_or_error
);
10695 location_t saved_input_location
= input_location
;
10696 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10697 tree cons
= get_constraints (tmpl
);
10698 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10699 input_location
= saved_input_location
;
10704 /* The friend template has not already been declared. In this
10705 case, the instantiation of the template class will cause the
10706 injection of this template into the namespace scope. */
10707 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10709 if (tmpl
!= error_mark_node
)
10711 /* The new TMPL is not an instantiation of anything, so we
10712 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10713 for the new type because that is supposed to be the
10714 corresponding template decl, i.e., TMPL. */
10715 DECL_USE_TEMPLATE (tmpl
) = 0;
10716 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10717 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10718 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10719 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10721 /* It is hidden. */
10722 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10723 DECL_ANTICIPATED (tmpl
)
10724 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10726 /* Inject this template into the enclosing namspace scope. */
10727 tmpl
= pushdecl_namespace_level (tmpl
, true);
10731 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10732 pop_nested_namespace (context
);
10734 pop_nested_class ();
10736 return TREE_TYPE (tmpl
);
10739 /* Returns zero if TYPE cannot be completed later due to circularity.
10740 Otherwise returns one. */
10743 can_complete_type_without_circularity (tree type
)
10745 if (type
== NULL_TREE
|| type
== error_mark_node
)
10747 else if (COMPLETE_TYPE_P (type
))
10749 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10750 return can_complete_type_without_circularity (TREE_TYPE (type
));
10751 else if (CLASS_TYPE_P (type
)
10752 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10758 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10759 tsubst_flags_t
, tree
);
10761 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10762 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10765 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10766 tsubst_flags_t complain
, tree in_decl
)
10768 gcc_assert (ATTR_IS_DEPENDENT (t
));
10770 tree val
= TREE_VALUE (t
);
10771 if (val
== NULL_TREE
)
10772 /* Nothing to do. */;
10773 else if ((flag_openmp
|| flag_openmp_simd
)
10774 && is_attribute_p ("omp declare simd",
10775 get_attribute_name (t
)))
10777 tree clauses
= TREE_VALUE (val
);
10778 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10779 complain
, in_decl
);
10780 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10781 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10782 tree parms
= DECL_ARGUMENTS (*decl_p
);
10784 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10786 val
= build_tree_list (NULL_TREE
, clauses
);
10790 /* If the first attribute argument is an identifier, don't
10791 pass it through tsubst. Attributes like mode, format,
10792 cleanup and several target specific attributes expect it
10794 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10797 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10798 /*integral_constant_expression_p=*/false);
10799 if (chain
!= TREE_CHAIN (val
))
10800 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10802 else if (PACK_EXPANSION_P (val
))
10804 /* An attribute pack expansion. */
10805 tree purp
= TREE_PURPOSE (t
);
10806 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10807 if (pack
== error_mark_node
)
10808 return error_mark_node
;
10809 int len
= TREE_VEC_LENGTH (pack
);
10810 tree list
= NULL_TREE
;
10812 for (int i
= 0; i
< len
; ++i
)
10814 tree elt
= TREE_VEC_ELT (pack
, i
);
10815 *q
= build_tree_list (purp
, elt
);
10816 q
= &TREE_CHAIN (*q
);
10821 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10822 /*integral_constant_expression_p=*/false);
10824 if (val
!= TREE_VALUE (t
))
10825 return build_tree_list (TREE_PURPOSE (t
), val
);
10829 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10830 unchanged or a new TREE_LIST chain. */
10833 tsubst_attributes (tree attributes
, tree args
,
10834 tsubst_flags_t complain
, tree in_decl
)
10836 tree last_dep
= NULL_TREE
;
10838 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10839 if (ATTR_IS_DEPENDENT (t
))
10842 attributes
= copy_list (attributes
);
10847 for (tree
*p
= &attributes
; *p
; )
10850 if (ATTR_IS_DEPENDENT (t
))
10852 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10857 p
= &TREE_CHAIN (*p
);
10858 *p
= TREE_CHAIN (t
);
10862 p
= &TREE_CHAIN (*p
);
10868 /* Apply any attributes which had to be deferred until instantiation
10869 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10870 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10873 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10874 tree args
, tsubst_flags_t complain
, tree in_decl
)
10876 tree last_dep
= NULL_TREE
;
10880 if (attributes
== NULL_TREE
)
10883 if (DECL_P (*decl_p
))
10885 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10887 p
= &DECL_ATTRIBUTES (*decl_p
);
10888 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10889 to our attributes parameter. */
10890 gcc_assert (*p
== attributes
);
10894 p
= &TYPE_ATTRIBUTES (*decl_p
);
10895 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10896 lookup_template_class_1, and should be preserved. */
10897 gcc_assert (*p
!= attributes
);
10899 p
= &TREE_CHAIN (*p
);
10902 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10903 if (ATTR_IS_DEPENDENT (t
))
10906 attributes
= copy_list (attributes
);
10913 tree late_attrs
= NULL_TREE
;
10914 tree
*q
= &late_attrs
;
10919 if (ATTR_IS_DEPENDENT (t
))
10921 *p
= TREE_CHAIN (t
);
10922 TREE_CHAIN (t
) = NULL_TREE
;
10923 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10925 q
= &TREE_CHAIN (*q
);
10928 p
= &TREE_CHAIN (t
);
10931 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10935 /* Perform (or defer) access check for typedefs that were referenced
10936 from within the template TMPL code.
10937 This is a subroutine of instantiate_decl and instantiate_class_template.
10938 TMPL is the template to consider and TARGS is the list of arguments of
10942 perform_typedefs_access_check (tree tmpl
, tree targs
)
10945 qualified_typedef_usage_t
*iter
;
10948 || (!CLASS_TYPE_P (tmpl
)
10949 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10952 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10954 tree type_decl
= iter
->typedef_decl
;
10955 tree type_scope
= iter
->context
;
10957 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10960 if (uses_template_parms (type_decl
))
10961 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10962 if (uses_template_parms (type_scope
))
10963 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10965 /* Make access check error messages point to the location
10966 of the use of the typedef. */
10967 iloc_sentinel
ils (iter
->locus
);
10968 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10969 type_decl
, type_decl
,
10970 tf_warning_or_error
);
10975 instantiate_class_template_1 (tree type
)
10977 tree templ
, args
, pattern
, t
, member
;
10981 unsigned int saved_maximum_field_alignment
;
10984 if (type
== error_mark_node
)
10985 return error_mark_node
;
10987 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10988 || uses_template_parms (type
))
10991 /* Figure out which template is being instantiated. */
10992 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10993 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10995 /* Mark the type as in the process of being defined. */
10996 TYPE_BEING_DEFINED (type
) = 1;
10998 /* We may be in the middle of deferred access check. Disable
11000 deferring_access_check_sentinel
acs (dk_no_deferred
);
11002 /* Determine what specialization of the original template to
11004 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11005 if (t
== error_mark_node
)
11006 return error_mark_node
;
11009 /* This TYPE is actually an instantiation of a partial
11010 specialization. We replace the innermost set of ARGS with
11011 the arguments appropriate for substitution. For example,
11014 template <class T> struct S {};
11015 template <class T> struct S<T*> {};
11017 and supposing that we are instantiating S<int*>, ARGS will
11018 presently be {int*} -- but we need {int}. */
11019 pattern
= TREE_TYPE (t
);
11020 args
= TREE_PURPOSE (t
);
11024 pattern
= TREE_TYPE (templ
);
11025 args
= CLASSTYPE_TI_ARGS (type
);
11028 /* If the template we're instantiating is incomplete, then clearly
11029 there's nothing we can do. */
11030 if (!COMPLETE_TYPE_P (pattern
))
11032 /* We can try again later. */
11033 TYPE_BEING_DEFINED (type
) = 0;
11037 /* If we've recursively instantiated too many templates, stop. */
11038 if (! push_tinst_level (type
))
11041 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11042 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11044 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11045 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11046 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11047 fn_context
= error_mark_node
;
11049 push_to_top_level ();
11052 cp_unevaluated_operand
= 0;
11053 c_inhibit_evaluation_warnings
= 0;
11055 /* Use #pragma pack from the template context. */
11056 saved_maximum_field_alignment
= maximum_field_alignment
;
11057 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11059 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11061 /* Set the input location to the most specialized template definition.
11062 This is needed if tsubsting causes an error. */
11063 typedecl
= TYPE_MAIN_DECL (pattern
);
11064 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11065 DECL_SOURCE_LOCATION (typedecl
);
11067 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11068 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11069 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11070 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11071 if (ANON_AGGR_TYPE_P (pattern
))
11072 SET_ANON_AGGR_TYPE_P (type
);
11073 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11075 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11076 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11077 /* Adjust visibility for template arguments. */
11078 determine_visibility (TYPE_MAIN_DECL (type
));
11080 if (CLASS_TYPE_P (type
))
11081 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11083 pbinfo
= TYPE_BINFO (pattern
);
11085 /* We should never instantiate a nested class before its enclosing
11086 class; we need to look up the nested class by name before we can
11087 instantiate it, and that lookup should instantiate the enclosing
11089 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11090 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11092 base_list
= NULL_TREE
;
11093 if (BINFO_N_BASE_BINFOS (pbinfo
))
11099 /* We must enter the scope containing the type, as that is where
11100 the accessibility of types named in dependent bases are
11102 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11104 /* Substitute into each of the bases to determine the actual
11106 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11109 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11110 tree expanded_bases
= NULL_TREE
;
11113 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11116 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11117 args
, tf_error
, NULL_TREE
);
11118 if (expanded_bases
== error_mark_node
)
11121 len
= TREE_VEC_LENGTH (expanded_bases
);
11124 for (idx
= 0; idx
< len
; idx
++)
11126 if (expanded_bases
)
11127 /* Extract the already-expanded base class. */
11128 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11130 /* Substitute to figure out the base class. */
11131 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11134 if (base
== error_mark_node
)
11137 base_list
= tree_cons (access
, base
, base_list
);
11138 if (BINFO_VIRTUAL_P (pbase_binfo
))
11139 TREE_TYPE (base_list
) = integer_type_node
;
11143 /* The list is now in reverse order; correct that. */
11144 base_list
= nreverse (base_list
);
11147 pop_scope (pushed_scope
);
11149 /* Now call xref_basetypes to set up all the base-class
11151 xref_basetypes (type
, base_list
);
11153 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11154 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11155 args
, tf_error
, NULL_TREE
);
11156 fixup_attribute_variants (type
);
11158 /* Now that our base classes are set up, enter the scope of the
11159 class, so that name lookups into base classes, etc. will work
11160 correctly. This is precisely analogous to what we do in
11161 begin_class_definition when defining an ordinary non-template
11162 class, except we also need to push the enclosing classes. */
11163 push_nested_class (type
);
11165 /* Now members are processed in the order of declaration. */
11166 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11167 member
; member
= TREE_CHAIN (member
))
11169 tree t
= TREE_VALUE (member
);
11171 if (TREE_PURPOSE (member
))
11175 if (LAMBDA_TYPE_P (t
))
11176 /* A closure type for a lambda in an NSDMI or default argument.
11177 Ignore it; it will be regenerated when needed. */
11180 /* Build new CLASSTYPE_NESTED_UTDS. */
11183 bool class_template_p
;
11185 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11186 && TYPE_LANG_SPECIFIC (t
)
11187 && CLASSTYPE_IS_TEMPLATE (t
));
11188 /* If the member is a class template, then -- even after
11189 substitution -- there may be dependent types in the
11190 template argument list for the class. We increment
11191 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11192 that function will assume that no types are dependent
11193 when outside of a template. */
11194 if (class_template_p
)
11195 ++processing_template_decl
;
11196 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11197 if (class_template_p
)
11198 --processing_template_decl
;
11199 if (newtag
== error_mark_node
)
11202 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11204 tree name
= TYPE_IDENTIFIER (t
);
11206 if (class_template_p
)
11207 /* Unfortunately, lookup_template_class sets
11208 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11209 instantiation (i.e., for the type of a member
11210 template class nested within a template class.)
11211 This behavior is required for
11212 maybe_process_partial_specialization to work
11213 correctly, but is not accurate in this case;
11214 the TAG is not an instantiation of anything.
11215 (The corresponding TEMPLATE_DECL is an
11216 instantiation, but the TYPE is not.) */
11217 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11219 /* Now, we call pushtag to put this NEWTAG into the scope of
11220 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11221 pushtag calling push_template_decl. We don't have to do
11222 this for enums because it will already have been done in
11225 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11226 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11229 else if (DECL_DECLARES_FUNCTION_P (t
))
11233 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11234 ++processing_template_decl
;
11235 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11236 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11237 --processing_template_decl
;
11238 set_current_access_from_decl (r
);
11239 finish_member_declaration (r
);
11240 /* Instantiate members marked with attribute used. */
11241 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11243 if (TREE_CODE (r
) == FUNCTION_DECL
11244 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11245 cp_check_omp_declare_reduction (r
);
11247 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11248 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11249 /* A closure type for a lambda in an NSDMI or default argument.
11250 Ignore it; it will be regenerated when needed. */;
11253 /* Build new TYPE_FIELDS. */
11254 if (TREE_CODE (t
) == STATIC_ASSERT
)
11258 ++c_inhibit_evaluation_warnings
;
11260 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11261 tf_warning_or_error
, NULL_TREE
,
11262 /*integral_constant_expression_p=*/true);
11263 --c_inhibit_evaluation_warnings
;
11265 finish_static_assert (condition
,
11266 STATIC_ASSERT_MESSAGE (t
),
11267 STATIC_ASSERT_SOURCE_LOCATION (t
),
11268 /*member_p=*/true);
11270 else if (TREE_CODE (t
) != CONST_DECL
)
11273 tree vec
= NULL_TREE
;
11276 /* The file and line for this declaration, to
11277 assist in error message reporting. Since we
11278 called push_tinst_level above, we don't need to
11280 input_location
= DECL_SOURCE_LOCATION (t
);
11282 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11283 ++processing_template_decl
;
11284 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11285 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11286 --processing_template_decl
;
11288 if (TREE_CODE (r
) == TREE_VEC
)
11290 /* A capture pack became multiple fields. */
11292 len
= TREE_VEC_LENGTH (vec
);
11295 for (int i
= 0; i
< len
; ++i
)
11298 r
= TREE_VEC_ELT (vec
, i
);
11303 [t]he initialization (and any associated
11304 side-effects) of a static data member does
11305 not occur unless the static data member is
11306 itself used in a way that requires the
11307 definition of the static data member to
11310 Therefore, we do not substitute into the
11311 initialized for the static data member here. */
11312 finish_static_data_member_decl
11314 /*init=*/NULL_TREE
,
11315 /*init_const_expr_p=*/false,
11316 /*asmspec_tree=*/NULL_TREE
,
11318 /* Instantiate members marked with attribute used. */
11319 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11322 else if (TREE_CODE (r
) == FIELD_DECL
)
11324 /* Determine whether R has a valid type and can be
11325 completed later. If R is invalid, then its type
11326 is replaced by error_mark_node. */
11327 tree rtype
= TREE_TYPE (r
);
11328 if (can_complete_type_without_circularity (rtype
))
11329 complete_type (rtype
);
11331 if (!complete_or_array_type_p (rtype
))
11333 /* If R's type couldn't be completed and
11334 it isn't a flexible array member (whose
11335 type is incomplete by definition) give
11337 cxx_incomplete_type_error (r
, rtype
);
11338 TREE_TYPE (r
) = error_mark_node
;
11340 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11341 && TYPE_DOMAIN (rtype
) == NULL_TREE
11342 && (TREE_CODE (type
) == UNION_TYPE
11343 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11345 error ("flexible array member %qD in union", r
);
11346 TREE_TYPE (r
) = error_mark_node
;
11350 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11351 such a thing will already have been added to the field
11352 list by tsubst_enum in finish_member_declaration in the
11353 CLASSTYPE_NESTED_UTDS case above. */
11354 if (!(TREE_CODE (r
) == TYPE_DECL
11355 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11356 && DECL_ARTIFICIAL (r
)))
11358 set_current_access_from_decl (r
);
11359 finish_member_declaration (r
);
11367 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11368 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11370 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11372 tree friend_type
= t
;
11373 bool adjust_processing_template_decl
= false;
11375 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11377 /* template <class T> friend class C; */
11378 friend_type
= tsubst_friend_class (friend_type
, args
);
11379 adjust_processing_template_decl
= true;
11381 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11383 /* template <class T> friend class C::D; */
11384 friend_type
= tsubst (friend_type
, args
,
11385 tf_warning_or_error
, NULL_TREE
);
11386 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11387 friend_type
= TREE_TYPE (friend_type
);
11388 adjust_processing_template_decl
= true;
11390 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11391 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11393 /* This could be either
11397 when dependent_type_p is false or
11399 template <class U> friend class T::C;
11402 /* Bump processing_template_decl in case this is something like
11403 template <class T> friend struct A<T>::B. */
11404 ++processing_template_decl
;
11405 friend_type
= tsubst (friend_type
, args
,
11406 tf_warning_or_error
, NULL_TREE
);
11407 if (dependent_type_p (friend_type
))
11408 adjust_processing_template_decl
= true;
11409 --processing_template_decl
;
11411 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11412 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11413 && TYPE_HIDDEN_P (friend_type
))
11417 where C hasn't been declared yet. Let's lookup name
11418 from namespace scope directly, bypassing any name that
11419 come from dependent base class. */
11420 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11422 /* The call to xref_tag_from_type does injection for friend
11424 push_nested_namespace (ns
);
11426 xref_tag_from_type (friend_type
, NULL_TREE
,
11427 /*tag_scope=*/ts_current
);
11428 pop_nested_namespace (ns
);
11430 else if (uses_template_parms (friend_type
))
11431 /* friend class C<T>; */
11432 friend_type
= tsubst (friend_type
, args
,
11433 tf_warning_or_error
, NULL_TREE
);
11438 where C is already declared or
11440 friend class C<int>;
11442 We don't have to do anything in these cases. */
11444 if (adjust_processing_template_decl
)
11445 /* Trick make_friend_class into realizing that the friend
11446 we're adding is a template, not an ordinary class. It's
11447 important that we use make_friend_class since it will
11448 perform some error-checking and output cross-reference
11450 ++processing_template_decl
;
11452 if (friend_type
!= error_mark_node
)
11453 make_friend_class (type
, friend_type
, /*complain=*/false);
11455 if (adjust_processing_template_decl
)
11456 --processing_template_decl
;
11460 /* Build new DECL_FRIENDLIST. */
11463 /* The file and line for this declaration, to
11464 assist in error message reporting. Since we
11465 called push_tinst_level above, we don't need to
11467 input_location
= DECL_SOURCE_LOCATION (t
);
11469 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11471 ++processing_template_decl
;
11472 push_deferring_access_checks (dk_no_check
);
11475 r
= tsubst_friend_function (t
, args
);
11476 add_friend (type
, r
, /*complain=*/false);
11477 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11479 pop_deferring_access_checks ();
11480 --processing_template_decl
;
11488 /* Restore these before substituting into the lambda capture
11490 cp_unevaluated_operand
= saved_unevaluated_operand
;
11491 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11494 /* Set the file and line number information to whatever is given for
11495 the class itself. This puts error messages involving generated
11496 implicit functions at a predictable point, and the same point
11497 that would be used for non-template classes. */
11498 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11500 unreverse_member_declarations (type
);
11501 finish_struct_1 (type
);
11502 TYPE_BEING_DEFINED (type
) = 0;
11504 /* We don't instantiate default arguments for member functions. 14.7.1:
11506 The implicit instantiation of a class template specialization causes
11507 the implicit instantiation of the declarations, but not of the
11508 definitions or default arguments, of the class member functions,
11509 member classes, static data members and member templates.... */
11511 /* Some typedefs referenced from within the template code need to be access
11512 checked at template instantiation time, i.e now. These types were
11513 added to the template at parsing time. Let's get those and perform
11514 the access checks then. */
11515 perform_typedefs_access_check (pattern
, args
);
11516 perform_deferred_access_checks (tf_warning_or_error
);
11517 pop_nested_class ();
11518 maximum_field_alignment
= saved_maximum_field_alignment
;
11520 pop_from_top_level ();
11521 pop_tinst_level ();
11523 /* The vtable for a template class can be emitted in any translation
11524 unit in which the class is instantiated. When there is no key
11525 method, however, finish_struct_1 will already have added TYPE to
11526 the keyed_classes. */
11527 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11528 vec_safe_push (keyed_classes
, type
);
11533 /* Wrapper for instantiate_class_template_1. */
11536 instantiate_class_template (tree type
)
11539 timevar_push (TV_TEMPLATE_INST
);
11540 ret
= instantiate_class_template_1 (type
);
11541 timevar_pop (TV_TEMPLATE_INST
);
11546 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11552 else if (TYPE_P (t
))
11553 r
= tsubst (t
, args
, complain
, in_decl
);
11556 if (!(complain
& tf_warning
))
11557 ++c_inhibit_evaluation_warnings
;
11558 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11559 /*integral_constant_expression_p=*/true);
11560 if (!(complain
& tf_warning
))
11561 --c_inhibit_evaluation_warnings
;
11566 /* Given a function parameter pack TMPL_PARM and some function parameters
11567 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11568 and set *SPEC_P to point at the next point in the list. */
11571 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11573 /* Collect all of the extra "packed" parameters into an
11576 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11577 tree spec_parm
= *spec_p
;
11580 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11582 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11585 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11586 parmvec
= make_tree_vec (len
);
11587 spec_parm
= *spec_p
;
11588 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11590 tree elt
= spec_parm
;
11591 if (DECL_PACK_P (elt
))
11592 elt
= make_pack_expansion (elt
);
11593 TREE_VEC_ELT (parmvec
, i
) = elt
;
11596 /* Build the argument packs. */
11597 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11598 *spec_p
= spec_parm
;
11603 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11604 NONTYPE_ARGUMENT_PACK. */
11607 make_fnparm_pack (tree spec_parm
)
11609 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11612 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11613 pack expansion with no extra args, 2 if it has extra args, or 0
11614 if it is not a pack expansion. */
11617 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11619 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11620 /* We're being called before this happens in tsubst_pack_expansion. */
11621 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11622 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11623 if (i
>= TREE_VEC_LENGTH (vec
))
11625 tree elt
= TREE_VEC_ELT (vec
, i
);
11627 /* A decl pack is itself an expansion. */
11628 elt
= TREE_TYPE (elt
);
11629 if (!PACK_EXPANSION_P (elt
))
11631 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11637 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11640 make_argument_pack_select (tree arg_pack
, unsigned index
)
11642 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11644 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11645 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11650 /* This is a subroutine of tsubst_pack_expansion.
11652 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11653 mechanism to store the (non complete list of) arguments of the
11654 substitution and return a non substituted pack expansion, in order
11655 to wait for when we have enough arguments to really perform the
11659 use_pack_expansion_extra_args_p (tree parm_packs
,
11661 bool has_empty_arg
)
11663 /* If one pack has an expansion and another pack has a normal
11664 argument or if one pack has an empty argument and an another
11665 one hasn't then tsubst_pack_expansion cannot perform the
11666 substitution and need to fall back on the
11667 PACK_EXPANSION_EXTRA mechanism. */
11668 if (parm_packs
== NULL_TREE
)
11670 else if (has_empty_arg
)
11673 bool has_expansion_arg
= false;
11674 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11676 bool has_non_expansion_arg
= false;
11677 for (tree parm_pack
= parm_packs
;
11679 parm_pack
= TREE_CHAIN (parm_pack
))
11681 tree arg
= TREE_VALUE (parm_pack
);
11683 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11685 /* We can't substitute a pack expansion with extra args into
11689 has_expansion_arg
= true;
11691 has_non_expansion_arg
= true;
11694 if (has_expansion_arg
&& has_non_expansion_arg
)
11700 /* [temp.variadic]/6 says that:
11702 The instantiation of a pack expansion [...]
11703 produces a list E1,E2, ..., En, where N is the number of elements
11704 in the pack expansion parameters.
11706 This subroutine of tsubst_pack_expansion produces one of these Ei.
11708 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11709 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11710 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11711 INDEX is the index 'i' of the element Ei to produce. ARGS,
11712 COMPLAIN, and IN_DECL are the same parameters as for the
11713 tsubst_pack_expansion function.
11715 The function returns the resulting Ei upon successful completion,
11716 or error_mark_node.
11718 Note that this function possibly modifies the ARGS parameter, so
11719 it's the responsibility of the caller to restore it. */
11722 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11725 tree args
/* This parm gets
11727 tsubst_flags_t complain
,
11731 bool ith_elem_is_expansion
= false;
11733 /* For each parameter pack, change the substitution of the parameter
11734 pack to the ith argument in its argument pack, then expand the
11736 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11738 tree parm
= TREE_PURPOSE (pack
);
11739 tree arg_pack
= TREE_VALUE (pack
);
11740 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11742 ith_elem_is_expansion
|=
11743 argument_pack_element_is_expansion_p (arg_pack
, index
);
11745 /* Select the Ith argument from the pack. */
11746 if (TREE_CODE (parm
) == PARM_DECL
11748 || TREE_CODE (parm
) == FIELD_DECL
)
11752 aps
= make_argument_pack_select (arg_pack
, index
);
11753 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11754 return error_mark_node
;
11755 register_local_specialization (aps
, parm
);
11758 aps
= retrieve_local_specialization (parm
);
11763 template_parm_level_and_index (parm
, &level
, &idx
);
11767 aps
= make_argument_pack_select (arg_pack
, index
);
11768 /* Update the corresponding argument. */
11769 TMPL_ARG (args
, level
, idx
) = aps
;
11772 /* Re-use the ARGUMENT_PACK_SELECT. */
11773 aps
= TMPL_ARG (args
, level
, idx
);
11775 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11778 /* Substitute into the PATTERN with the (possibly altered)
11780 if (pattern
== in_decl
)
11781 /* Expanding a fixed parameter pack from
11782 coerce_template_parameter_pack. */
11783 t
= tsubst_decl (pattern
, args
, complain
);
11784 else if (pattern
== error_mark_node
)
11785 t
= error_mark_node
;
11786 else if (constraint_p (pattern
))
11788 if (processing_template_decl
)
11789 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11791 t
= (constraints_satisfied_p (pattern
, args
)
11792 ? boolean_true_node
: boolean_false_node
);
11794 else if (!TYPE_P (pattern
))
11795 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11796 /*integral_constant_expression_p=*/false);
11798 t
= tsubst (pattern
, args
, complain
, in_decl
);
11800 /* If the Ith argument pack element is a pack expansion, then
11801 the Ith element resulting from the substituting is going to
11802 be a pack expansion as well. */
11803 if (ith_elem_is_expansion
)
11804 t
= make_pack_expansion (t
, complain
);
11809 /* When the unexpanded parameter pack in a fold expression expands to an empty
11810 sequence, the value of the expression is as follows; the program is
11811 ill-formed if the operator is not listed in this table.
11818 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11820 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11821 if (!FOLD_EXPR_MODIFY_P (t
))
11824 case TRUTH_ANDIF_EXPR
:
11825 return boolean_true_node
;
11826 case TRUTH_ORIF_EXPR
:
11827 return boolean_false_node
;
11828 case COMPOUND_EXPR
:
11834 if (complain
& tf_error
)
11835 error_at (location_of (t
),
11836 "fold of empty expansion over %O", code
);
11837 return error_mark_node
;
11840 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11841 form an expression that combines the two terms using the
11845 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11847 tree op
= FOLD_EXPR_OP (t
);
11848 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11850 // Handle compound assignment operators.
11851 if (FOLD_EXPR_MODIFY_P (t
))
11852 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11856 case COMPOUND_EXPR
:
11857 return build_x_compound_expr (input_location
, left
, right
, complain
);
11859 return build_x_binary_op (input_location
, code
,
11860 left
, TREE_CODE (left
),
11861 right
, TREE_CODE (right
),
11867 /* Substitute ARGS into the pack of a fold expression T. */
11870 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11872 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11875 /* Substitute ARGS into the pack of a fold expression T. */
11878 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11880 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11883 /* Expand a PACK of arguments into a grouped as left fold.
11884 Given a pack containing elements A0, A1, ..., An and an
11885 operator @, this builds the expression:
11887 ((A0 @ A1) @ A2) ... @ An
11889 Note that PACK must not be empty.
11891 The operator is defined by the original fold expression T. */
11894 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11896 tree left
= TREE_VEC_ELT (pack
, 0);
11897 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11899 tree right
= TREE_VEC_ELT (pack
, i
);
11900 left
= fold_expression (t
, left
, right
, complain
);
11905 /* Substitute into a unary left fold expression. */
11908 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11911 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11912 if (pack
== error_mark_node
)
11913 return error_mark_node
;
11914 if (PACK_EXPANSION_P (pack
))
11916 tree r
= copy_node (t
);
11917 FOLD_EXPR_PACK (r
) = pack
;
11920 if (TREE_VEC_LENGTH (pack
) == 0)
11921 return expand_empty_fold (t
, complain
);
11923 return expand_left_fold (t
, pack
, complain
);
11926 /* Substitute into a binary left fold expression.
11928 Do ths by building a single (non-empty) vector of argumnts and
11929 building the expression from those elements. */
11932 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11935 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11936 if (pack
== error_mark_node
)
11937 return error_mark_node
;
11938 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11939 if (init
== error_mark_node
)
11940 return error_mark_node
;
11942 if (PACK_EXPANSION_P (pack
))
11944 tree r
= copy_node (t
);
11945 FOLD_EXPR_PACK (r
) = pack
;
11946 FOLD_EXPR_INIT (r
) = init
;
11950 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11951 TREE_VEC_ELT (vec
, 0) = init
;
11952 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11953 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11955 return expand_left_fold (t
, vec
, complain
);
11958 /* Expand a PACK of arguments into a grouped as right fold.
11959 Given a pack containing elementns A0, A1, ..., and an
11960 operator @, this builds the expression:
11962 A0@ ... (An-2 @ (An-1 @ An))
11964 Note that PACK must not be empty.
11966 The operator is defined by the original fold expression T. */
11969 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11971 // Build the expression.
11972 int n
= TREE_VEC_LENGTH (pack
);
11973 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11974 for (--n
; n
!= 0; --n
)
11976 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11977 right
= fold_expression (t
, left
, right
, complain
);
11982 /* Substitute into a unary right fold expression. */
11985 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11988 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11989 if (pack
== error_mark_node
)
11990 return error_mark_node
;
11991 if (PACK_EXPANSION_P (pack
))
11993 tree r
= copy_node (t
);
11994 FOLD_EXPR_PACK (r
) = pack
;
11997 if (TREE_VEC_LENGTH (pack
) == 0)
11998 return expand_empty_fold (t
, complain
);
12000 return expand_right_fold (t
, pack
, complain
);
12003 /* Substitute into a binary right fold expression.
12005 Do ths by building a single (non-empty) vector of arguments and
12006 building the expression from those elements. */
12009 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12012 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12013 if (pack
== error_mark_node
)
12014 return error_mark_node
;
12015 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12016 if (init
== error_mark_node
)
12017 return error_mark_node
;
12019 if (PACK_EXPANSION_P (pack
))
12021 tree r
= copy_node (t
);
12022 FOLD_EXPR_PACK (r
) = pack
;
12023 FOLD_EXPR_INIT (r
) = init
;
12027 int n
= TREE_VEC_LENGTH (pack
);
12028 tree vec
= make_tree_vec (n
+ 1);
12029 for (int i
= 0; i
< n
; ++i
)
12030 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12031 TREE_VEC_ELT (vec
, n
) = init
;
12033 return expand_right_fold (t
, vec
, complain
);
12036 /* Walk through the pattern of a pack expansion, adding everything in
12037 local_specializations to a list. */
12042 hash_set
<tree
> internal
;
12044 tsubst_flags_t complain
;
12046 el_data (tsubst_flags_t c
)
12047 : extra (NULL_TREE
), complain (c
) {}
12050 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12052 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12053 tree
*extra
= &data
.extra
;
12054 tsubst_flags_t complain
= data
.complain
;
12056 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12057 /* Remember local typedefs (85214). */
12058 tp
= &TYPE_NAME (*tp
);
12060 if (TREE_CODE (*tp
) == DECL_EXPR
)
12061 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12062 else if (tree spec
= retrieve_local_specialization (*tp
))
12064 if (data
.internal
.contains (*tp
))
12065 /* Don't mess with variables declared within the pattern. */
12067 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12069 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12070 tree args
= ARGUMENT_PACK_ARGS (spec
);
12071 if (TREE_VEC_LENGTH (args
) == 1)
12073 tree elt
= TREE_VEC_ELT (args
, 0);
12074 if (PACK_EXPANSION_P (elt
))
12075 elt
= PACK_EXPANSION_PATTERN (elt
);
12076 if (DECL_PACK_P (elt
))
12079 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12081 /* Handle lambda capture here, since we aren't doing any
12082 substitution now, and so tsubst_copy won't call
12083 process_outer_var_ref. */
12084 tree args
= ARGUMENT_PACK_ARGS (spec
);
12085 int len
= TREE_VEC_LENGTH (args
);
12086 for (int i
= 0; i
< len
; ++i
)
12088 tree arg
= TREE_VEC_ELT (args
, i
);
12090 if (outer_automatic_var_p (arg
))
12091 carg
= process_outer_var_ref (arg
, complain
);
12094 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12098 spec
= copy_node (spec
);
12099 args
= copy_node (args
);
12100 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12101 register_local_specialization (spec
, *tp
);
12103 TREE_VEC_ELT (args
, i
) = carg
;
12108 if (outer_automatic_var_p (spec
))
12109 spec
= process_outer_var_ref (spec
, complain
);
12110 *extra
= tree_cons (*tp
, spec
, *extra
);
12115 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12117 el_data
data (complain
);
12118 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12122 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12123 for use in PACK_EXPANSION_EXTRA_ARGS. */
12126 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12129 if (local_specializations
)
12130 if (tree locals
= extract_local_specs (pattern
, complain
))
12131 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12135 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12136 normal template args to ARGS. */
12139 add_extra_args (tree extra
, tree args
)
12141 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12143 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12145 /* The partial instantiation involved local declarations collected in
12146 extract_local_specs; map from the general template to our local
12148 tree gen
= TREE_PURPOSE (elt
);
12149 tree inst
= TREE_VALUE (elt
);
12151 if (tree local
= retrieve_local_specialization (inst
))
12153 /* else inst is already a full instantiation of the pack. */
12154 register_local_specialization (inst
, gen
);
12156 gcc_assert (!TREE_PURPOSE (extra
));
12157 extra
= TREE_VALUE (extra
);
12159 return add_to_template_args (extra
, args
);
12162 /* Substitute ARGS into T, which is an pack expansion
12163 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12164 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12165 (if only a partial substitution could be performed) or
12166 ERROR_MARK_NODE if there was an error. */
12168 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12172 tree pack
, packs
= NULL_TREE
;
12173 bool unsubstituted_packs
= false;
12174 bool unsubstituted_fn_pack
= false;
12177 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12178 bool need_local_specializations
= false;
12181 gcc_assert (PACK_EXPANSION_P (t
));
12182 pattern
= PACK_EXPANSION_PATTERN (t
);
12184 /* Add in any args remembered from an earlier partial instantiation. */
12185 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12187 levels
= TMPL_ARGS_DEPTH (args
);
12189 /* Determine the argument packs that will instantiate the parameter
12190 packs used in the expansion expression. While we're at it,
12191 compute the number of arguments to be expanded and make sure it
12193 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12194 pack
= TREE_CHAIN (pack
))
12196 tree parm_pack
= TREE_VALUE (pack
);
12197 tree arg_pack
= NULL_TREE
;
12198 tree orig_arg
= NULL_TREE
;
12201 if (TREE_CODE (parm_pack
) == BASES
)
12203 gcc_assert (parm_pack
== pattern
);
12204 if (BASES_DIRECT (parm_pack
))
12205 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12210 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12211 args
, complain
, in_decl
,
12214 else if (builtin_pack_call_p (parm_pack
))
12216 if (parm_pack
!= pattern
)
12218 if (complain
& tf_error
)
12219 sorry ("%qE is not the entire pattern of the pack expansion",
12221 return error_mark_node
;
12223 return expand_builtin_pack_call (parm_pack
, args
,
12224 complain
, in_decl
);
12226 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12228 /* We know we have correct local_specializations if this
12229 expansion is at function scope, or if we're dealing with a
12230 local parameter in a requires expression; for the latter,
12231 tsubst_requires_expr set it up appropriately. */
12232 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12233 arg_pack
= retrieve_local_specialization (parm_pack
);
12235 /* We can't rely on local_specializations for a parameter
12236 name used later in a function declaration (such as in a
12237 late-specified return type). Even if it exists, it might
12238 have the wrong value for a recursive call. */
12239 need_local_specializations
= true;
12243 /* This parameter pack was used in an unevaluated context. Just
12244 make a dummy decl, since it's only used for its type. */
12245 ++cp_unevaluated_operand
;
12246 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12247 --cp_unevaluated_operand
;
12248 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12249 /* Partial instantiation of the parm_pack, we can't build
12250 up an argument pack yet. */
12251 arg_pack
= NULL_TREE
;
12253 arg_pack
= make_fnparm_pack (arg_pack
);
12255 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12256 /* This argument pack isn't fully instantiated yet. We set this
12257 flag rather than clear arg_pack because we do want to do the
12258 optimization below, and we don't want to substitute directly
12259 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12260 where it isn't expected). */
12261 unsubstituted_fn_pack
= true;
12263 else if (is_capture_proxy (parm_pack
))
12265 arg_pack
= retrieve_local_specialization (parm_pack
);
12266 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12267 unsubstituted_fn_pack
= true;
12272 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12274 if (level
<= levels
)
12275 arg_pack
= TMPL_ARG (args
, level
, idx
);
12278 orig_arg
= arg_pack
;
12279 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12280 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12282 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12283 /* This can only happen if we forget to expand an argument
12284 pack somewhere else. Just return an error, silently. */
12286 result
= make_tree_vec (1);
12287 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12294 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12296 /* Don't bother trying to do a partial substitution with
12297 incomplete packs; we'll try again after deduction. */
12298 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12303 else if (len
!= my_len
12304 && !unsubstituted_fn_pack
)
12306 if (!(complain
& tf_error
))
12307 /* Fail quietly. */;
12308 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12309 error ("mismatched argument pack lengths while expanding %qT",
12312 error ("mismatched argument pack lengths while expanding %qE",
12314 return error_mark_node
;
12317 /* Keep track of the parameter packs and their corresponding
12319 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12320 TREE_TYPE (packs
) = orig_arg
;
12324 /* We can't substitute for this parameter pack. We use a flag as
12325 well as the missing_level counter because function parameter
12326 packs don't have a level. */
12327 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12328 unsubstituted_packs
= true;
12332 /* If the expansion is just T..., return the matching argument pack, unless
12333 we need to call convert_from_reference on all the elements. This is an
12334 important optimization; see c++/68422. */
12335 if (!unsubstituted_packs
12336 && TREE_PURPOSE (packs
) == pattern
)
12338 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12340 /* If the argument pack is a single pack expansion, pull it out. */
12341 if (TREE_VEC_LENGTH (args
) == 1
12342 && pack_expansion_args_count (args
))
12343 return TREE_VEC_ELT (args
, 0);
12345 /* Types need no adjustment, nor does sizeof..., and if we still have
12346 some pack expansion args we won't do anything yet. */
12347 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12348 || PACK_EXPANSION_SIZEOF_P (t
)
12349 || pack_expansion_args_count (args
))
12351 /* Also optimize expression pack expansions if we can tell that the
12352 elements won't have reference type. */
12353 tree type
= TREE_TYPE (pattern
);
12354 if (type
&& !TYPE_REF_P (type
)
12355 && !PACK_EXPANSION_P (type
)
12356 && !WILDCARD_TYPE_P (type
))
12358 /* Otherwise use the normal path so we get convert_from_reference. */
12361 /* We cannot expand this expansion expression, because we don't have
12362 all of the argument packs we need. */
12363 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12365 /* We got some full packs, but we can't substitute them in until we
12366 have values for all the packs. So remember these until then. */
12368 t
= make_pack_expansion (pattern
, complain
);
12369 PACK_EXPANSION_EXTRA_ARGS (t
)
12370 = build_extra_args (pattern
, args
, complain
);
12373 else if (unsubstituted_packs
)
12375 /* There were no real arguments, we're just replacing a parameter
12376 pack with another version of itself. Substitute into the
12377 pattern and return a PACK_EXPANSION_*. The caller will need to
12379 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12380 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12381 /*integral_constant_expression_p=*/false);
12383 t
= tsubst (pattern
, args
, complain
, in_decl
);
12384 t
= make_pack_expansion (t
, complain
);
12388 gcc_assert (len
>= 0);
12390 if (need_local_specializations
)
12392 /* We're in a late-specified return type, so create our own local
12393 specializations map; the current map is either NULL or (in the
12394 case of recursive unification) might have bindings that we don't
12395 want to use or alter. */
12396 saved_local_specializations
= local_specializations
;
12397 local_specializations
= new hash_map
<tree
, tree
>;
12400 /* For each argument in each argument pack, substitute into the
12402 result
= make_tree_vec (len
);
12403 tree elem_args
= copy_template_args (args
);
12404 for (i
= 0; i
< len
; ++i
)
12406 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12408 elem_args
, complain
,
12410 TREE_VEC_ELT (result
, i
) = t
;
12411 if (t
== error_mark_node
)
12413 result
= error_mark_node
;
12418 /* Update ARGS to restore the substitution from parameter packs to
12419 their argument packs. */
12420 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12422 tree parm
= TREE_PURPOSE (pack
);
12424 if (TREE_CODE (parm
) == PARM_DECL
12426 || TREE_CODE (parm
) == FIELD_DECL
)
12427 register_local_specialization (TREE_TYPE (pack
), parm
);
12432 if (TREE_VALUE (pack
) == NULL_TREE
)
12435 template_parm_level_and_index (parm
, &level
, &idx
);
12437 /* Update the corresponding argument. */
12438 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12439 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12442 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12446 if (need_local_specializations
)
12448 delete local_specializations
;
12449 local_specializations
= saved_local_specializations
;
12452 /* If the dependent pack arguments were such that we end up with only a
12453 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12454 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12455 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12456 return TREE_VEC_ELT (result
, 0);
12461 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12462 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12463 parameter packs; all parms generated from a function parameter pack will
12464 have the same DECL_PARM_INDEX. */
12467 get_pattern_parm (tree parm
, tree tmpl
)
12469 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12472 if (DECL_ARTIFICIAL (parm
))
12474 for (patparm
= DECL_ARGUMENTS (pattern
);
12475 patparm
; patparm
= DECL_CHAIN (patparm
))
12476 if (DECL_ARTIFICIAL (patparm
)
12477 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12482 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12483 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12484 gcc_assert (DECL_PARM_INDEX (patparm
)
12485 == DECL_PARM_INDEX (parm
));
12491 /* Make an argument pack out of the TREE_VEC VEC. */
12494 make_argument_pack (tree vec
)
12497 tree elt
= TREE_VEC_ELT (vec
, 0);
12499 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12502 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12503 TREE_CONSTANT (pack
) = 1;
12505 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12509 /* Return an exact copy of template args T that can be modified
12513 copy_template_args (tree t
)
12515 if (t
== error_mark_node
)
12518 int len
= TREE_VEC_LENGTH (t
);
12519 tree new_vec
= make_tree_vec (len
);
12521 for (int i
= 0; i
< len
; ++i
)
12523 tree elt
= TREE_VEC_ELT (t
, i
);
12524 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12525 elt
= copy_template_args (elt
);
12526 TREE_VEC_ELT (new_vec
, i
) = elt
;
12529 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12530 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12535 /* Substitute ARGS into the vector or list of template arguments T. */
12538 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12541 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12544 if (t
== error_mark_node
)
12545 return error_mark_node
;
12547 len
= TREE_VEC_LENGTH (t
);
12548 elts
= XALLOCAVEC (tree
, len
);
12550 for (i
= 0; i
< len
; i
++)
12552 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12555 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12556 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12557 else if (PACK_EXPANSION_P (orig_arg
))
12559 /* Substitute into an expansion expression. */
12560 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12562 if (TREE_CODE (new_arg
) == TREE_VEC
)
12563 /* Add to the expanded length adjustment the number of
12564 expanded arguments. We subtract one from this
12565 measurement, because the argument pack expression
12566 itself is already counted as 1 in
12567 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12568 the argument pack is empty. */
12569 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12571 else if (ARGUMENT_PACK_P (orig_arg
))
12573 /* Substitute into each of the arguments. */
12574 new_arg
= TYPE_P (orig_arg
)
12575 ? cxx_make_type (TREE_CODE (orig_arg
))
12576 : make_node (TREE_CODE (orig_arg
));
12578 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12579 args
, complain
, in_decl
);
12580 if (pack_args
== error_mark_node
)
12581 new_arg
= error_mark_node
;
12583 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12585 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12586 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12589 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12591 if (new_arg
== error_mark_node
)
12592 return error_mark_node
;
12595 if (new_arg
!= orig_arg
)
12602 /* Make space for the expanded arguments coming from template
12604 t
= make_tree_vec (len
+ expanded_len_adjust
);
12605 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12606 arguments for a member template.
12607 In that case each TREE_VEC in ORIG_T represents a level of template
12608 arguments, and ORIG_T won't carry any non defaulted argument count.
12609 It will rather be the nested TREE_VECs that will carry one.
12610 In other words, ORIG_T carries a non defaulted argument count only
12611 if it doesn't contain any nested TREE_VEC. */
12612 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12614 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12615 count
+= expanded_len_adjust
;
12616 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12618 for (i
= 0, out
= 0; i
< len
; i
++)
12620 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12621 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12622 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12626 /* Now expand the template argument pack "in place". */
12627 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12628 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12632 TREE_VEC_ELT (t
, out
) = elts
[i
];
12640 /* Substitute ARGS into one level PARMS of template parameters. */
12643 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12645 if (parms
== error_mark_node
)
12646 return error_mark_node
;
12648 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12650 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12652 tree tuple
= TREE_VEC_ELT (parms
, i
);
12654 if (tuple
== error_mark_node
)
12657 TREE_VEC_ELT (new_vec
, i
) =
12658 tsubst_template_parm (tuple
, args
, complain
);
12664 /* Return the result of substituting ARGS into the template parameters
12665 given by PARMS. If there are m levels of ARGS and m + n levels of
12666 PARMS, then the result will contain n levels of PARMS. For
12667 example, if PARMS is `template <class T> template <class U>
12668 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12669 result will be `template <int*, double, class V>'. */
12672 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12674 tree r
= NULL_TREE
;
12677 /* When substituting into a template, we must set
12678 PROCESSING_TEMPLATE_DECL as the template parameters may be
12679 dependent if they are based on one-another, and the dependency
12680 predicates are short-circuit outside of templates. */
12681 ++processing_template_decl
;
12683 for (new_parms
= &r
;
12684 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12685 new_parms
= &(TREE_CHAIN (*new_parms
)),
12686 parms
= TREE_CHAIN (parms
))
12688 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12691 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12692 - TMPL_ARGS_DEPTH (args
)),
12693 new_vec
, NULL_TREE
);
12696 --processing_template_decl
;
12701 /* Return the result of substituting ARGS into one template parameter
12702 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12703 parameter and which TREE_PURPOSE is the default argument of the
12704 template parameter. */
12707 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12709 tree default_value
, parm_decl
;
12711 if (args
== NULL_TREE
12713 || t
== error_mark_node
)
12716 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12718 default_value
= TREE_PURPOSE (t
);
12719 parm_decl
= TREE_VALUE (t
);
12721 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12722 if (TREE_CODE (parm_decl
) == PARM_DECL
12723 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12724 parm_decl
= error_mark_node
;
12725 default_value
= tsubst_template_arg (default_value
, args
,
12726 complain
, NULL_TREE
);
12728 return build_tree_list (default_value
, parm_decl
);
12731 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12732 type T. If T is not an aggregate or enumeration type, it is
12733 handled as if by tsubst. IN_DECL is as for tsubst. If
12734 ENTERING_SCOPE is nonzero, T is the context for a template which
12735 we are presently tsubst'ing. Return the substituted value. */
12738 tsubst_aggr_type (tree t
,
12740 tsubst_flags_t complain
,
12742 int entering_scope
)
12744 if (t
== NULL_TREE
)
12747 switch (TREE_CODE (t
))
12750 if (TYPE_PTRMEMFUNC_P (t
))
12751 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12753 /* Fall through. */
12754 case ENUMERAL_TYPE
:
12756 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12762 /* In "sizeof(X<I>)" we need to evaluate "I". */
12765 /* First, determine the context for the type we are looking
12767 context
= TYPE_CONTEXT (t
);
12768 if (context
&& TYPE_P (context
))
12770 context
= tsubst_aggr_type (context
, args
, complain
,
12771 in_decl
, /*entering_scope=*/1);
12772 /* If context is a nested class inside a class template,
12773 it may still need to be instantiated (c++/33959). */
12774 context
= complete_type (context
);
12777 /* Then, figure out what arguments are appropriate for the
12778 type we are trying to find. For example, given:
12780 template <class T> struct S;
12781 template <class T, class U> void f(T, U) { S<U> su; }
12783 and supposing that we are instantiating f<int, double>,
12784 then our ARGS will be {int, double}, but, when looking up
12785 S we only want {double}. */
12786 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12787 complain
, in_decl
);
12788 if (argvec
== error_mark_node
)
12789 r
= error_mark_node
;
12792 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12793 entering_scope
, complain
);
12794 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12800 /* This is not a template type, so there's nothing to do. */
12804 return tsubst (t
, args
, complain
, in_decl
);
12808 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12810 /* Substitute into the default argument ARG (a default argument for
12811 FN), which has the indicated TYPE. */
12814 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12815 tsubst_flags_t complain
)
12817 int errs
= errorcount
+ sorrycount
;
12819 /* This can happen in invalid code. */
12820 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
12823 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12824 parm
= chain_index (parmnum
, parm
);
12825 tree parmtype
= TREE_TYPE (parm
);
12826 if (DECL_BY_REFERENCE (parm
))
12827 parmtype
= TREE_TYPE (parmtype
);
12828 if (parmtype
== error_mark_node
)
12829 return error_mark_node
;
12831 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12834 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12837 /* This default argument came from a template. Instantiate the
12838 default argument here, not in tsubst. In the case of
12847 we must be careful to do name lookup in the scope of S<T>,
12848 rather than in the current class. */
12849 push_to_top_level ();
12850 push_access_scope (fn
);
12851 push_deferring_access_checks (dk_no_deferred
);
12852 start_lambda_scope (parm
);
12854 /* The default argument expression may cause implicitly defined
12855 member functions to be synthesized, which will result in garbage
12856 collection. We must treat this situation as if we were within
12857 the body of function so as to avoid collecting live data on the
12860 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12861 complain
, NULL_TREE
,
12862 /*integral_constant_expression_p=*/false);
12865 finish_lambda_scope ();
12867 /* Make sure the default argument is reasonable. */
12868 arg
= check_default_argument (type
, arg
, complain
);
12870 if (errorcount
+sorrycount
> errs
12871 && (complain
& tf_warning_or_error
))
12872 inform (input_location
,
12873 " when instantiating default argument for call to %qD", fn
);
12875 pop_deferring_access_checks ();
12876 pop_access_scope (fn
);
12877 pop_from_top_level ();
12879 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12882 defarg_inst
= tree_cache_map::create_ggc (37);
12883 defarg_inst
->put (parm
, arg
);
12889 /* Substitute into all the default arguments for FN. */
12892 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12897 tmpl_args
= DECL_TI_ARGS (fn
);
12899 /* If this function is not yet instantiated, we certainly don't need
12900 its default arguments. */
12901 if (uses_template_parms (tmpl_args
))
12903 /* Don't do this again for clones. */
12904 if (DECL_CLONED_FUNCTION_P (fn
))
12908 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12910 arg
= TREE_CHAIN (arg
), ++i
)
12911 if (TREE_PURPOSE (arg
))
12912 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12914 TREE_PURPOSE (arg
),
12918 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12919 static GTY((cache
)) tree_cache_map
*explicit_specifier_map
;
12921 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12924 store_explicit_specifier (tree v
, tree t
)
12926 if (!explicit_specifier_map
)
12927 explicit_specifier_map
= tree_cache_map::create_ggc (37);
12928 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
12929 explicit_specifier_map
->put (v
, t
);
12932 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12935 lookup_explicit_specifier (tree v
)
12937 return *explicit_specifier_map
->get (v
);
12940 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12943 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12944 tree lambda_fntype
)
12946 tree gen_tmpl
, argvec
;
12947 hashval_t hash
= 0;
12950 /* Nobody should be tsubst'ing into non-template functions. */
12951 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12953 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12955 /* If T is not dependent, just return it. */
12956 if (!uses_template_parms (DECL_TI_ARGS (t
))
12957 && !LAMBDA_FUNCTION_P (t
))
12960 /* Calculate the most general template of which R is a
12962 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12964 /* We're substituting a lambda function under tsubst_lambda_expr but not
12965 directly from it; find the matching function we're already inside.
12966 But don't do this if T is a generic lambda with a single level of
12967 template parms, as in that case we're doing a normal instantiation. */
12968 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12969 && (!generic_lambda_fn_p (t
)
12970 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12971 return enclosing_instantiation_of (t
);
12973 /* Calculate the complete set of arguments used to
12975 argvec
= tsubst_template_args (DECL_TI_ARGS
12976 (DECL_TEMPLATE_RESULT
12977 (DECL_TI_TEMPLATE (t
))),
12978 args
, complain
, in_decl
);
12979 if (argvec
== error_mark_node
)
12980 return error_mark_node
;
12982 /* Check to see if we already have this specialization. */
12983 if (!lambda_fntype
)
12985 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12986 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12990 /* We can see more levels of arguments than parameters if
12991 there was a specialization of a member template, like
12994 template <class T> struct S { template <class U> void f(); }
12995 template <> template <class U> void S<int>::f(U);
12997 Here, we'll be substituting into the specialization,
12998 because that's where we can find the code we actually
12999 want to generate, but we'll have enough arguments for
13000 the most general template.
13002 We also deal with the peculiar case:
13004 template <class T> struct S {
13005 template <class U> friend void f();
13007 template <class U> void f() {}
13009 template void f<double>();
13011 Here, the ARGS for the instantiation of will be {int,
13012 double}. But, we only need as many ARGS as there are
13013 levels of template parameters in CODE_PATTERN. We are
13014 careful not to get fooled into reducing the ARGS in
13017 template <class T> struct S { template <class U> void f(U); }
13018 template <class T> template <> void S<T>::f(int) {}
13020 which we can spot because the pattern will be a
13021 specialization in this case. */
13022 int args_depth
= TMPL_ARGS_DEPTH (args
);
13024 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13026 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13027 args
= get_innermost_template_args (args
, parms_depth
);
13031 /* This special case arises when we have something like this:
13033 template <class T> struct S {
13034 friend void f<int>(int, double);
13037 Here, the DECL_TI_TEMPLATE for the friend declaration
13038 will be an IDENTIFIER_NODE. We are being called from
13039 tsubst_friend_function, and we want only to create a
13040 new decl (R) with appropriate types so that we can call
13041 determine_specialization. */
13042 gen_tmpl
= NULL_TREE
;
13043 argvec
= NULL_TREE
;
13046 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13048 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13049 bool member
= ctx
&& TYPE_P (ctx
);
13051 if (member
&& !closure
)
13052 ctx
= tsubst_aggr_type (ctx
, args
,
13053 complain
, t
, /*entering_scope=*/1);
13055 tree type
= (lambda_fntype
? lambda_fntype
13056 : tsubst (TREE_TYPE (t
), args
,
13057 complain
| tf_fndecl_type
, in_decl
));
13058 if (type
== error_mark_node
)
13059 return error_mark_node
;
13061 /* If we hit excessive deduction depth, the type is bogus even if
13062 it isn't error_mark_node, so don't build a decl. */
13063 if (excessive_deduction_depth
)
13064 return error_mark_node
;
13066 /* We do NOT check for matching decls pushed separately at this
13067 point, as they may not represent instantiations of this
13068 template, and in any case are considered separate under the
13070 tree r
= copy_decl (t
);
13071 DECL_USE_TEMPLATE (r
) = 0;
13072 TREE_TYPE (r
) = type
;
13073 /* Clear out the mangled name and RTL for the instantiation. */
13074 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13075 SET_DECL_RTL (r
, NULL
);
13076 /* Leave DECL_INITIAL set on deleted instantiations. */
13077 if (!DECL_DELETED_FN (r
))
13078 DECL_INITIAL (r
) = NULL_TREE
;
13079 DECL_CONTEXT (r
) = ctx
;
13081 /* Handle explicit(dependent-expr). */
13082 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13084 tree spec
= lookup_explicit_specifier (t
);
13085 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13086 /*function_p=*/false,
13088 spec
= build_explicit_specifier (spec
, complain
);
13089 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13092 /* OpenMP UDRs have the only argument a reference to the declared
13093 type. We want to diagnose if the declared type is a reference,
13094 which is invalid, but as references to references are usually
13095 quietly merged, diagnose it here. */
13096 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13099 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13100 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13101 if (TYPE_REF_P (argtype
))
13102 error_at (DECL_SOURCE_LOCATION (t
),
13103 "reference type %qT in "
13104 "%<#pragma omp declare reduction%>", argtype
);
13105 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13106 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13110 if (member
&& DECL_CONV_FN_P (r
))
13111 /* Type-conversion operator. Reconstruct the name, in
13112 case it's the name of one of the template's parameters. */
13113 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13115 tree parms
= DECL_ARGUMENTS (t
);
13117 parms
= DECL_CHAIN (parms
);
13118 parms
= tsubst (parms
, args
, complain
, t
);
13119 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13120 DECL_CONTEXT (parm
) = r
;
13123 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13124 DECL_CHAIN (tparm
) = parms
;
13127 DECL_ARGUMENTS (r
) = parms
;
13128 DECL_RESULT (r
) = NULL_TREE
;
13130 TREE_STATIC (r
) = 0;
13131 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13132 DECL_EXTERNAL (r
) = 1;
13133 /* If this is an instantiation of a function with internal
13134 linkage, we already know what object file linkage will be
13135 assigned to the instantiation. */
13136 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13137 DECL_DEFER_OUTPUT (r
) = 0;
13138 DECL_CHAIN (r
) = NULL_TREE
;
13139 DECL_PENDING_INLINE_INFO (r
) = 0;
13140 DECL_PENDING_INLINE_P (r
) = 0;
13141 DECL_SAVED_TREE (r
) = NULL_TREE
;
13142 DECL_STRUCT_FUNCTION (r
) = NULL
;
13144 /* We'll re-clone as appropriate in instantiate_template. */
13145 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13147 /* If we aren't complaining now, return on error before we register
13148 the specialization so that we'll complain eventually. */
13149 if ((complain
& tf_error
) == 0
13150 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13151 && !grok_op_properties (r
, /*complain=*/false))
13152 return error_mark_node
;
13154 /* When instantiating a constrained member, substitute
13155 into the constraints to create a new constraint. */
13156 if (tree ci
= get_constraints (t
))
13159 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
13160 set_constraints (r
, ci
);
13163 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13164 SET_DECL_FRIEND_CONTEXT (r
,
13165 tsubst (DECL_FRIEND_CONTEXT (t
),
13166 args
, complain
, in_decl
));
13168 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13169 this in the special friend case mentioned above where
13170 GEN_TMPL is NULL. */
13171 if (gen_tmpl
&& !closure
)
13173 DECL_TEMPLATE_INFO (r
)
13174 = build_template_info (gen_tmpl
, argvec
);
13175 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13178 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13180 /* We instantiated this while substituting into
13181 the type earlier (template/friend54.C). */
13184 /* We're not supposed to instantiate default arguments
13185 until they are called, for a template. But, for a
13188 template <class T> void f ()
13189 { extern void g(int i = T()); }
13191 we should do the substitution when the template is
13192 instantiated. We handle the member function case in
13193 instantiate_class_template since the default arguments
13194 might refer to other members of the class. */
13196 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13197 && !uses_template_parms (argvec
))
13198 tsubst_default_arguments (r
, complain
);
13201 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13203 /* Copy the list of befriending classes. */
13204 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13206 friends
= &TREE_CHAIN (*friends
))
13208 *friends
= copy_node (*friends
);
13209 TREE_VALUE (*friends
)
13210 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13213 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13215 maybe_retrofit_in_chrg (r
);
13216 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13217 return error_mark_node
;
13218 /* If this is an instantiation of a member template, clone it.
13219 If it isn't, that'll be handled by
13220 clone_constructors_and_destructors. */
13221 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13222 clone_function_decl (r
, /*update_methods=*/false);
13224 else if ((complain
& tf_error
) != 0
13225 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13226 && !grok_op_properties (r
, /*complain=*/true))
13227 return error_mark_node
;
13229 /* Possibly limit visibility based on template args. */
13230 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13231 if (DECL_VISIBILITY_SPECIFIED (t
))
13233 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13234 DECL_ATTRIBUTES (r
)
13235 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13237 determine_visibility (r
);
13238 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13239 && !processing_template_decl
)
13240 defaulted_late_check (r
);
13242 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13243 args
, complain
, in_decl
);
13247 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13250 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13251 tree lambda_fntype
)
13253 /* We can get here when processing a member function template,
13254 member class template, or template template parameter. */
13255 tree decl
= DECL_TEMPLATE_RESULT (t
);
13261 hashval_t hash
= 0;
13263 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13265 /* Template template parameter is treated here. */
13266 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13267 if (new_type
== error_mark_node
)
13268 r
= error_mark_node
;
13269 /* If we get a real template back, return it. This can happen in
13270 the context of most_specialized_partial_spec. */
13271 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13274 /* The new TEMPLATE_DECL was built in
13275 reduce_template_parm_level. */
13276 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13280 if (!lambda_fntype
)
13282 /* We might already have an instance of this template.
13283 The ARGS are for the surrounding class type, so the
13284 full args contain the tsubst'd args for the context,
13285 plus the innermost args from the template decl. */
13286 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13287 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13288 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13289 /* Because this is a template, the arguments will still be
13290 dependent, even after substitution. If
13291 PROCESSING_TEMPLATE_DECL is not set, the dependency
13292 predicates will short-circuit. */
13293 ++processing_template_decl
;
13294 full_args
= tsubst_template_args (tmpl_args
, args
,
13295 complain
, in_decl
);
13296 --processing_template_decl
;
13297 if (full_args
== error_mark_node
)
13298 return error_mark_node
;
13300 /* If this is a default template template argument,
13301 tsubst might not have changed anything. */
13302 if (full_args
== tmpl_args
)
13305 hash
= hash_tmpl_and_args (t
, full_args
);
13306 spec
= retrieve_specialization (t
, full_args
, hash
);
13307 if (spec
!= NULL_TREE
)
13310 /* Type partial instantiations are stored as the type by
13311 lookup_template_class_1, not here as the template. */
13312 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
13317 /* Make a new template decl. It will be similar to the
13318 original, but will record the current template arguments.
13319 We also create a new function declaration, which is just
13320 like the old one, but points to this new template, rather
13321 than the old one. */
13323 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13324 DECL_CHAIN (r
) = NULL_TREE
;
13326 // Build new template info linking to the original template decl.
13327 if (!lambda_fntype
)
13329 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13330 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13333 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13335 /* The template parameters for this new template are all the
13336 template parameters for the old template, except the
13337 outermost level of parameters. */
13338 DECL_TEMPLATE_PARMS (r
)
13339 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13342 if (TREE_CODE (decl
) == TYPE_DECL
13343 && !TYPE_DECL_ALIAS_P (decl
))
13346 ++processing_template_decl
;
13347 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13348 --processing_template_decl
;
13349 if (new_type
== error_mark_node
)
13350 return error_mark_node
;
13352 TREE_TYPE (r
) = new_type
;
13353 /* For a partial specialization, we need to keep pointing to
13354 the primary template. */
13355 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13356 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13357 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13358 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13359 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13364 ++processing_template_decl
;
13365 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13366 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13368 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13369 --processing_template_decl
;
13370 if (new_decl
== error_mark_node
)
13371 return error_mark_node
;
13373 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13374 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13375 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13378 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13379 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13383 DECL_TI_TEMPLATE (new_decl
) = r
;
13384 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13388 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13389 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13391 if (PRIMARY_TEMPLATE_P (t
))
13392 DECL_PRIMARY_TEMPLATE (r
) = r
;
13394 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13396 /* Record this non-type partial instantiation. */
13397 register_specialization (r
, t
,
13398 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13404 /* True if FN is the op() for a lambda in an uninstantiated template. */
13407 lambda_fn_in_template_p (tree fn
)
13409 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13411 tree closure
= DECL_CONTEXT (fn
);
13412 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13415 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13416 which the above is true. */
13419 instantiated_lambda_fn_p (tree fn
)
13421 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13423 tree closure
= DECL_CONTEXT (fn
);
13424 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13425 return LAMBDA_EXPR_INSTANTIATED (lam
);
13428 /* We're instantiating a variable from template function TCTX. Return the
13429 corresponding current enclosing scope. This gets complicated because lambda
13430 functions in templates are regenerated rather than instantiated, but generic
13431 lambda functions are subsequently instantiated. */
13434 enclosing_instantiation_of (tree otctx
)
13437 tree fn
= current_function_decl
;
13438 int lambda_count
= 0;
13440 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
13441 || instantiated_lambda_fn_p (tctx
));
13442 tctx
= decl_function_context (tctx
))
13444 for (; fn
; fn
= decl_function_context (fn
))
13447 int flambda_count
= 0;
13448 for (; fn
&& instantiated_lambda_fn_p (fn
);
13449 fn
= decl_function_context (fn
))
13451 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13452 ? most_general_template (fn
) != most_general_template (tctx
)
13455 if (flambda_count
!= lambda_count
)
13457 gcc_assert (flambda_count
> lambda_count
);
13458 for (; flambda_count
> lambda_count
; --flambda_count
)
13459 ofn
= decl_function_context (ofn
);
13461 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13462 || DECL_CONV_FN_P (ofn
));
13465 gcc_unreachable ();
13468 /* Substitute the ARGS into the T, which is a _DECL. Return the
13469 result of the substitution. Issue error and warning messages under
13470 control of COMPLAIN. */
13473 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13475 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13476 location_t saved_loc
;
13477 tree r
= NULL_TREE
;
13479 hashval_t hash
= 0;
13481 /* Set the filename and linenumber to improve error-reporting. */
13482 saved_loc
= input_location
;
13483 input_location
= DECL_SOURCE_LOCATION (t
);
13485 switch (TREE_CODE (t
))
13487 case TEMPLATE_DECL
:
13488 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13491 case FUNCTION_DECL
:
13492 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13497 tree type
= NULL_TREE
;
13499 tree expanded_types
= NULL_TREE
;
13500 tree prev_r
= NULL_TREE
;
13501 tree first_r
= NULL_TREE
;
13503 if (DECL_PACK_P (t
))
13505 /* If there is a local specialization that isn't a
13506 parameter pack, it means that we're doing a "simple"
13507 substitution from inside tsubst_pack_expansion. Just
13508 return the local specialization (which will be a single
13510 tree spec
= retrieve_local_specialization (t
);
13512 && TREE_CODE (spec
) == PARM_DECL
13513 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13516 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13517 the parameters in this function parameter pack. */
13518 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13519 complain
, in_decl
);
13520 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13522 len
= TREE_VEC_LENGTH (expanded_types
);
13524 /* Zero-length parameter packs are boring. Just substitute
13527 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13532 /* All we did was update the type. Make a note of that. */
13533 type
= expanded_types
;
13534 expanded_types
= NULL_TREE
;
13538 /* Loop through all of the parameters we'll build. When T is
13539 a function parameter pack, LEN is the number of expanded
13540 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13542 for (i
= 0; i
< len
; ++i
)
13546 if (DECL_TEMPLATE_PARM_P (t
))
13547 SET_DECL_TEMPLATE_PARM_P (r
);
13549 if (expanded_types
)
13550 /* We're on the Ith parameter of the function parameter
13553 /* Get the Ith type. */
13554 type
= TREE_VEC_ELT (expanded_types
, i
);
13556 /* Rename the parameter to include the index. */
13558 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13561 /* We're dealing with a normal parameter. */
13562 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13564 type
= type_decays_to (type
);
13565 TREE_TYPE (r
) = type
;
13566 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13568 if (DECL_INITIAL (r
))
13570 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13571 DECL_INITIAL (r
) = TREE_TYPE (r
);
13573 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13574 complain
, in_decl
);
13577 DECL_CONTEXT (r
) = NULL_TREE
;
13579 if (!DECL_TEMPLATE_PARM_P (r
))
13580 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13582 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13583 args
, complain
, in_decl
);
13585 /* Keep track of the first new parameter we
13586 generate. That's what will be returned to the
13591 /* Build a proper chain of parameters when substituting
13592 into a function parameter pack. */
13594 DECL_CHAIN (prev_r
) = r
;
13597 /* If cp_unevaluated_operand is set, we're just looking for a
13598 single dummy parameter, so don't keep going. */
13599 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13600 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13601 complain
, DECL_CHAIN (t
));
13603 /* FIRST_R contains the start of the chain we've built. */
13610 tree type
= NULL_TREE
;
13611 tree vec
= NULL_TREE
;
13612 tree expanded_types
= NULL_TREE
;
13615 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13617 /* This field is a lambda capture pack. Return a TREE_VEC of
13618 the expanded fields to instantiate_class_template_1. */
13619 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13620 complain
, in_decl
);
13621 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13623 len
= TREE_VEC_LENGTH (expanded_types
);
13624 vec
= make_tree_vec (len
);
13628 /* All we did was update the type. Make a note of that. */
13629 type
= expanded_types
;
13630 expanded_types
= NULL_TREE
;
13634 for (int i
= 0; i
< len
; ++i
)
13637 if (expanded_types
)
13639 type
= TREE_VEC_ELT (expanded_types
, i
);
13641 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13644 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13646 if (type
== error_mark_node
)
13647 RETURN (error_mark_node
);
13648 TREE_TYPE (r
) = type
;
13649 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13651 if (DECL_C_BIT_FIELD (r
))
13652 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13654 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13655 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13657 /*integral_constant_expression_p=*/true);
13658 if (DECL_INITIAL (t
))
13660 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13661 NSDMI in perform_member_init. Still set DECL_INITIAL
13662 so that we know there is one. */
13663 DECL_INITIAL (r
) = void_node
;
13664 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13665 retrofit_lang_decl (r
);
13666 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13668 /* We don't have to set DECL_CONTEXT here; it is set by
13669 finish_member_declaration. */
13670 DECL_CHAIN (r
) = NULL_TREE
;
13672 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13673 args
, complain
, in_decl
);
13676 TREE_VEC_ELT (vec
, i
) = r
;
13685 /* We reach here only for member using decls. We also need to check
13686 uses_template_parms because DECL_DEPENDENT_P is not set for a
13687 using-declaration that designates a member of the current
13688 instantiation (c++/53549). */
13689 if (DECL_DEPENDENT_P (t
)
13690 || uses_template_parms (USING_DECL_SCOPE (t
)))
13692 tree scope
= USING_DECL_SCOPE (t
);
13693 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13694 if (PACK_EXPANSION_P (scope
))
13696 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13697 int len
= TREE_VEC_LENGTH (vec
);
13698 r
= make_tree_vec (len
);
13699 for (int i
= 0; i
< len
; ++i
)
13701 tree escope
= TREE_VEC_ELT (vec
, i
);
13702 tree elt
= do_class_using_decl (escope
, name
);
13705 r
= error_mark_node
;
13710 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13711 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13713 TREE_VEC_ELT (r
, i
) = elt
;
13718 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13719 complain
, in_decl
);
13720 r
= do_class_using_decl (inst_scope
, name
);
13722 r
= error_mark_node
;
13725 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13726 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13733 DECL_CHAIN (r
) = NULL_TREE
;
13740 tree argvec
= NULL_TREE
;
13741 tree gen_tmpl
= NULL_TREE
;
13743 tree tmpl
= NULL_TREE
;
13745 tree type
= NULL_TREE
;
13748 if (TREE_TYPE (t
) == error_mark_node
)
13749 RETURN (error_mark_node
);
13751 if (TREE_CODE (t
) == TYPE_DECL
13752 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13754 /* If this is the canonical decl, we don't have to
13755 mess with instantiations, and often we can't (for
13756 typename, template type parms and such). Note that
13757 TYPE_NAME is not correct for the above test if
13758 we've copied the type for a typedef. */
13759 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13760 if (type
== error_mark_node
)
13761 RETURN (error_mark_node
);
13762 r
= TYPE_NAME (type
);
13766 /* Check to see if we already have the specialization we
13769 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13771 /* T is a static data member or namespace-scope entity.
13772 We have to substitute into namespace-scope variables
13773 (not just variable templates) because of cases like:
13775 template <class T> void f() { extern T t; }
13777 where the entity referenced is not known until
13778 instantiation time. */
13780 ctx
= DECL_CONTEXT (t
);
13781 if (DECL_CLASS_SCOPE_P (t
))
13783 ctx
= tsubst_aggr_type (ctx
, args
,
13785 in_decl
, /*entering_scope=*/1);
13786 /* If CTX is unchanged, then T is in fact the
13787 specialization we want. That situation occurs when
13788 referencing a static data member within in its own
13789 class. We can use pointer equality, rather than
13790 same_type_p, because DECL_CONTEXT is always
13792 if (ctx
== DECL_CONTEXT (t
)
13793 /* ... unless T is a member template; in which
13794 case our caller can be willing to create a
13795 specialization of that template represented
13797 && !(DECL_TI_TEMPLATE (t
)
13798 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13804 tmpl
= DECL_TI_TEMPLATE (t
);
13805 gen_tmpl
= most_general_template (tmpl
);
13806 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13807 if (argvec
!= error_mark_node
)
13808 argvec
= (coerce_innermost_template_parms
13809 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13810 argvec
, t
, complain
,
13811 /*all*/true, /*defarg*/true));
13812 if (argvec
== error_mark_node
)
13813 RETURN (error_mark_node
);
13814 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13815 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13820 /* A local variable. */
13822 /* Subsequent calls to pushdecl will fill this in. */
13824 /* Unless this is a reference to a static variable from an
13825 enclosing function, in which case we need to fill it in now. */
13826 if (TREE_STATIC (t
))
13828 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13829 if (fn
!= current_function_decl
)
13832 spec
= retrieve_local_specialization (t
);
13834 /* If we already have the specialization we need, there is
13835 nothing more to do. */
13842 /* Create a new node for the specialization we need. */
13843 if (type
== NULL_TREE
)
13845 if (is_typedef_decl (t
))
13846 type
= DECL_ORIGINAL_TYPE (t
);
13848 type
= TREE_TYPE (t
);
13850 && VAR_HAD_UNKNOWN_BOUND (t
)
13851 && type
!= error_mark_node
)
13852 type
= strip_array_domain (type
);
13853 tree sub_args
= args
;
13854 if (tree auto_node
= type_uses_auto (type
))
13856 /* Mask off any template args past the variable's context so we
13857 don't replace the auto with an unrelated argument. */
13858 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13859 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13861 /* This should never happen with the new lambda instantiation
13862 model, but keep the handling just in case. */
13863 gcc_assert (!CHECKING_P
),
13864 sub_args
= strip_innermost_template_args (args
, extra
);
13866 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13867 /* Substituting the type might have recursively instantiated this
13868 same alias (c++/86171). */
13869 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
13870 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
13879 DECL_INITIALIZED_P (r
) = 0;
13880 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13881 if (type
== error_mark_node
)
13882 RETURN (error_mark_node
);
13883 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13885 /* It may seem that this case cannot occur, since:
13890 declares a function, not a variable. However:
13893 template <typename T> void g() { T t; }
13894 template void g<f>();
13896 is an attempt to declare a variable with function
13898 error ("variable %qD has function type",
13899 /* R is not yet sufficiently initialized, so we
13900 just use its name. */
13902 RETURN (error_mark_node
);
13904 type
= complete_type (type
);
13905 /* Wait until cp_finish_decl to set this again, to handle
13906 circular dependency (template/instantiate6.C). */
13907 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13908 type
= check_var_type (DECL_NAME (r
), type
,
13909 DECL_SOURCE_LOCATION (r
));
13910 if (DECL_HAS_VALUE_EXPR_P (t
))
13912 tree ve
= DECL_VALUE_EXPR (t
);
13913 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13914 /*constant_expression_p=*/false);
13915 if (REFERENCE_REF_P (ve
))
13917 gcc_assert (TYPE_REF_P (type
));
13918 ve
= TREE_OPERAND (ve
, 0);
13920 SET_DECL_VALUE_EXPR (r
, ve
);
13922 if (CP_DECL_THREAD_LOCAL_P (r
)
13923 && !processing_template_decl
)
13924 set_decl_tls_model (r
, decl_default_tls_model (r
));
13926 else if (DECL_SELF_REFERENCE_P (t
))
13927 SET_DECL_SELF_REFERENCE_P (r
);
13928 TREE_TYPE (r
) = type
;
13929 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13930 DECL_CONTEXT (r
) = ctx
;
13931 /* Clear out the mangled name and RTL for the instantiation. */
13932 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13933 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13934 SET_DECL_RTL (r
, NULL
);
13935 /* The initializer must not be expanded until it is required;
13936 see [temp.inst]. */
13937 DECL_INITIAL (r
) = NULL_TREE
;
13938 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13941 if (DECL_LANG_SPECIFIC (r
))
13942 SET_DECL_DEPENDENT_INIT_P (r
, false);
13944 SET_DECL_MODE (r
, VOIDmode
);
13946 /* Possibly limit visibility based on template args. */
13947 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13948 if (DECL_VISIBILITY_SPECIFIED (t
))
13950 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13951 DECL_ATTRIBUTES (r
)
13952 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13954 determine_visibility (r
);
13959 /* A static data member declaration is always marked
13960 external when it is declared in-class, even if an
13961 initializer is present. We mimic the non-template
13962 processing here. */
13963 DECL_EXTERNAL (r
) = 1;
13964 if (DECL_NAMESPACE_SCOPE_P (t
))
13965 DECL_NOT_REALLY_EXTERN (r
) = 1;
13967 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13968 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13969 /* Remember whether we require constant initialization of
13970 a non-constant template variable. */
13971 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
13972 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
13973 if (!error_operand_p (r
) || (complain
& tf_error
))
13974 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13978 if (DECL_LANG_SPECIFIC (r
))
13979 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13980 if (!cp_unevaluated_operand
)
13981 register_local_specialization (r
, t
);
13984 DECL_CHAIN (r
) = NULL_TREE
;
13986 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13988 args
, complain
, in_decl
);
13990 /* Preserve a typedef that names a type. */
13991 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13993 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13994 set_underlying_type (r
);
13995 if (TYPE_DECL_ALIAS_P (r
))
13996 /* An alias template specialization can be dependent
13997 even if its underlying type is not. */
13998 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14001 layout_decl (r
, 0);
14006 gcc_unreachable ();
14011 /* Restore the file and line information. */
14012 input_location
= saved_loc
;
14017 /* Substitute into the ARG_TYPES of a function type.
14018 If END is a TREE_CHAIN, leave it and any following types
14022 tsubst_arg_types (tree arg_types
,
14025 tsubst_flags_t complain
,
14028 tree remaining_arg_types
;
14029 tree type
= NULL_TREE
;
14031 tree expanded_args
= NULL_TREE
;
14034 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14037 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14038 args
, end
, complain
, in_decl
);
14039 if (remaining_arg_types
== error_mark_node
)
14040 return error_mark_node
;
14042 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14044 /* For a pack expansion, perform substitution on the
14045 entire expression. Later on, we'll handle the arguments
14047 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14048 args
, complain
, in_decl
);
14050 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14051 /* So that we'll spin through the parameters, one by one. */
14052 i
= TREE_VEC_LENGTH (expanded_args
);
14055 /* We only partially substituted into the parameter
14056 pack. Our type is TYPE_PACK_EXPANSION. */
14057 type
= expanded_args
;
14058 expanded_args
= NULL_TREE
;
14066 type
= TREE_VEC_ELT (expanded_args
, i
);
14068 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14070 if (type
== error_mark_node
)
14071 return error_mark_node
;
14072 if (VOID_TYPE_P (type
))
14074 if (complain
& tf_error
)
14076 error ("invalid parameter type %qT", type
);
14078 error ("in declaration %q+D", in_decl
);
14080 return error_mark_node
;
14083 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14084 return error_mark_node
;
14086 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14087 top-level qualifiers as required. */
14088 type
= cv_unqualified (type_decays_to (type
));
14090 /* We do not substitute into default arguments here. The standard
14091 mandates that they be instantiated only when needed, which is
14092 done in build_over_call. */
14093 default_arg
= TREE_PURPOSE (arg_types
);
14095 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14096 since the new op() won't have any associated template arguments for us
14097 to refer to later. */
14098 if (lambda_fn_in_template_p (in_decl
))
14099 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14100 false/*fn*/, false/*constexpr*/);
14102 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14104 /* We've instantiated a template before its default arguments
14105 have been parsed. This can happen for a nested template
14106 class, and is not an error unless we require the default
14107 argument in a call of this function. */
14108 remaining_arg_types
=
14109 tree_cons (default_arg
, type
, remaining_arg_types
);
14110 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14111 remaining_arg_types
);
14114 remaining_arg_types
=
14115 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14118 return remaining_arg_types
;
14121 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14122 *not* handle the exception-specification for FNTYPE, because the
14123 initial substitution of explicitly provided template parameters
14124 during argument deduction forbids substitution into the
14125 exception-specification:
14129 All references in the function type of the function template to the
14130 corresponding template parameters are replaced by the specified tem-
14131 plate argument values. If a substitution in a template parameter or
14132 in the function type of the function template results in an invalid
14133 type, type deduction fails. [Note: The equivalent substitution in
14134 exception specifications is done only when the function is instanti-
14135 ated, at which point a program is ill-formed if the substitution
14136 results in an invalid type.] */
14139 tsubst_function_type (tree t
,
14141 tsubst_flags_t complain
,
14145 tree arg_types
= NULL_TREE
;
14148 /* The TYPE_CONTEXT is not used for function/method types. */
14149 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14151 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14153 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14155 if (late_return_type_p
)
14157 /* Substitute the argument types. */
14158 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14159 complain
, in_decl
);
14160 if (arg_types
== error_mark_node
)
14161 return error_mark_node
;
14163 tree save_ccp
= current_class_ptr
;
14164 tree save_ccr
= current_class_ref
;
14165 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14166 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14167 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14170 /* DR 1207: 'this' is in scope in the trailing return type. */
14171 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14174 /* Substitute the return type. */
14175 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14179 current_class_ptr
= save_ccp
;
14180 current_class_ref
= save_ccr
;
14184 /* Substitute the return type. */
14185 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14187 if (return_type
== error_mark_node
)
14188 return error_mark_node
;
14189 /* DR 486 clarifies that creation of a function type with an
14190 invalid return type is a deduction failure. */
14191 if (TREE_CODE (return_type
) == ARRAY_TYPE
14192 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14194 if (complain
& tf_error
)
14196 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14197 error ("function returning an array");
14199 error ("function returning a function");
14201 return error_mark_node
;
14204 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14205 return error_mark_node
;
14207 if (!late_return_type_p
)
14209 /* Substitute the argument types. */
14210 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14211 complain
, in_decl
);
14212 if (arg_types
== error_mark_node
)
14213 return error_mark_node
;
14216 /* Construct a new type node and return it. */
14217 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14219 fntype
= build_function_type (return_type
, arg_types
);
14220 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14224 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14225 /* Don't pick up extra function qualifiers from the basetype. */
14226 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14227 if (! MAYBE_CLASS_TYPE_P (r
))
14231 Type deduction may fail for any of the following
14234 -- Attempting to create "pointer to member of T" when T
14235 is not a class type. */
14236 if (complain
& tf_error
)
14237 error ("creating pointer to member function of non-class type %qT",
14239 return error_mark_node
;
14242 fntype
= build_method_type_directly (r
, return_type
,
14243 TREE_CHAIN (arg_types
));
14245 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14247 /* See comment above. */
14248 tree raises
= NULL_TREE
;
14249 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14250 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14255 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14256 ARGS into that specification, and return the substituted
14257 specification. If there is no specification, return NULL_TREE. */
14260 tsubst_exception_specification (tree fntype
,
14262 tsubst_flags_t complain
,
14269 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14270 new_specs
= NULL_TREE
;
14271 if (specs
&& TREE_PURPOSE (specs
))
14273 /* A noexcept-specifier. */
14274 tree expr
= TREE_PURPOSE (specs
);
14275 if (TREE_CODE (expr
) == INTEGER_CST
)
14279 /* Defer instantiation of noexcept-specifiers to avoid
14280 excessive instantiations (c++/49107). */
14281 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14282 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14284 /* We already partially instantiated this member template,
14285 so combine the new args with the old. */
14286 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14287 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14288 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14289 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14293 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14294 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14299 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14301 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14303 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14305 new_specs
= tsubst_copy_and_build
14306 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14307 /*integral_constant_expression_p=*/true);
14309 new_specs
= build_noexcept_spec (new_specs
, complain
);
14313 if (! TREE_VALUE (specs
))
14320 tree expanded_specs
= NULL_TREE
;
14322 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14324 /* Expand the pack expansion type. */
14325 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14329 if (expanded_specs
== error_mark_node
)
14330 return error_mark_node
;
14331 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14332 len
= TREE_VEC_LENGTH (expanded_specs
);
14335 /* We're substituting into a member template, so
14336 we got a TYPE_PACK_EXPANSION back. Add that
14337 expansion and move on. */
14338 gcc_assert (TREE_CODE (expanded_specs
)
14339 == TYPE_PACK_EXPANSION
);
14340 new_specs
= add_exception_specifier (new_specs
,
14343 specs
= TREE_CHAIN (specs
);
14348 for (i
= 0; i
< len
; ++i
)
14350 if (expanded_specs
)
14351 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14353 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14354 if (spec
== error_mark_node
)
14356 new_specs
= add_exception_specifier (new_specs
, spec
,
14360 specs
= TREE_CHAIN (specs
);
14366 /* Take the tree structure T and replace template parameters used
14367 therein with the argument vector ARGS. IN_DECL is an associated
14368 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14369 Issue error and warning messages under control of COMPLAIN. Note
14370 that we must be relatively non-tolerant of extensions here, in
14371 order to preserve conformance; if we allow substitutions that
14372 should not be allowed, we may allow argument deductions that should
14373 not succeed, and therefore report ambiguous overload situations
14374 where there are none. In theory, we could allow the substitution,
14375 but indicate that it should have failed, and allow our caller to
14376 make sure that the right thing happens, but we don't try to do this
14379 This function is used for dealing with types, decls and the like;
14380 for expressions, use tsubst_expr or tsubst_copy. */
14383 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14385 enum tree_code code
;
14386 tree type
, r
= NULL_TREE
;
14388 if (t
== NULL_TREE
|| t
== error_mark_node
14389 || t
== integer_type_node
14390 || t
== void_type_node
14391 || t
== char_type_node
14392 || t
== unknown_type_node
14393 || TREE_CODE (t
) == NAMESPACE_DECL
14394 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14398 return tsubst_decl (t
, args
, complain
);
14400 if (args
== NULL_TREE
)
14403 code
= TREE_CODE (t
);
14405 if (code
== IDENTIFIER_NODE
)
14406 type
= IDENTIFIER_TYPE_VALUE (t
);
14408 type
= TREE_TYPE (t
);
14410 gcc_assert (type
!= unknown_type_node
);
14412 /* Reuse typedefs. We need to do this to handle dependent attributes,
14413 such as attribute aligned. */
14415 && typedef_variant_p (t
))
14417 tree decl
= TYPE_NAME (t
);
14419 if (alias_template_specialization_p (t
))
14421 /* DECL represents an alias template and we want to
14423 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14424 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14425 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14427 else if (DECL_CLASS_SCOPE_P (decl
)
14428 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14429 && uses_template_parms (DECL_CONTEXT (decl
)))
14431 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14432 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14433 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14435 else if (DECL_FUNCTION_SCOPE_P (decl
)
14436 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14437 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14438 r
= retrieve_local_specialization (decl
);
14440 /* The typedef is from a non-template context. */
14446 r
= cp_build_qualified_type_real
14447 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14448 complain
| tf_ignore_bad_quals
);
14453 /* We don't have an instantiation yet, so drop the typedef. */
14454 int quals
= cp_type_quals (t
);
14455 t
= DECL_ORIGINAL_TYPE (decl
);
14456 t
= cp_build_qualified_type_real (t
, quals
,
14457 complain
| tf_ignore_bad_quals
);
14461 bool fndecl_type
= (complain
& tf_fndecl_type
);
14462 complain
&= ~tf_fndecl_type
;
14465 && code
!= TYPENAME_TYPE
14466 && code
!= TEMPLATE_TYPE_PARM
14467 && code
!= TEMPLATE_PARM_INDEX
14468 && code
!= IDENTIFIER_NODE
14469 && code
!= FUNCTION_TYPE
14470 && code
!= METHOD_TYPE
)
14471 type
= tsubst (type
, args
, complain
, in_decl
);
14472 if (type
== error_mark_node
)
14473 return error_mark_node
;
14479 case ENUMERAL_TYPE
:
14480 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14481 /*entering_scope=*/0);
14484 case IDENTIFIER_NODE
:
14495 if (t
== integer_type_node
)
14498 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14499 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14503 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14505 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14506 /*integral_constant_expression_p=*/false);
14508 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14510 if (TREE_CODE (max
) == NOP_EXPR
14511 && TREE_SIDE_EFFECTS (omax
)
14512 && !TREE_TYPE (max
))
14513 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14515 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14516 with TREE_SIDE_EFFECTS that indicates this is not an integral
14517 constant expression. */
14518 if (processing_template_decl
14519 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14521 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14522 TREE_SIDE_EFFECTS (max
) = 1;
14525 return compute_array_index_type (NULL_TREE
, max
, complain
);
14528 case TEMPLATE_TYPE_PARM
:
14529 case TEMPLATE_TEMPLATE_PARM
:
14530 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14531 case TEMPLATE_PARM_INDEX
:
14536 tree arg
= NULL_TREE
;
14538 /* Early in template argument deduction substitution, we don't
14539 want to reduce the level of 'auto', or it will be confused
14540 with a normal template parm in subsequent deduction. */
14541 if (is_auto (t
) && (complain
& tf_partial
))
14546 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14547 template_parm_level_and_index (t
, &level
, &idx
);
14549 levels
= TMPL_ARGS_DEPTH (args
);
14550 if (level
<= levels
14551 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14553 arg
= TMPL_ARG (args
, level
, idx
);
14555 /* See through ARGUMENT_PACK_SELECT arguments. */
14556 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14557 arg
= argument_pack_select_arg (arg
);
14560 if (arg
== error_mark_node
)
14561 return error_mark_node
;
14562 else if (arg
!= NULL_TREE
)
14564 if (ARGUMENT_PACK_P (arg
))
14565 /* If ARG is an argument pack, we don't actually want to
14566 perform a substitution here, because substitutions
14567 for argument packs are only done
14568 element-by-element. We can get to this point when
14569 substituting the type of a non-type template
14570 parameter pack, when that type actually contains
14571 template parameter packs from an outer template, e.g.,
14573 template<typename... Types> struct A {
14574 template<Types... Values> struct B { };
14578 if (code
== TEMPLATE_TYPE_PARM
)
14581 gcc_assert (TYPE_P (arg
));
14583 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14585 return cp_build_qualified_type_real
14586 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14588 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14590 /* We are processing a type constructed from a
14591 template template parameter. */
14592 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14593 args
, complain
, in_decl
);
14594 if (argvec
== error_mark_node
)
14595 return error_mark_node
;
14597 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14598 || TREE_CODE (arg
) == TEMPLATE_DECL
14599 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14601 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14602 /* Consider this code:
14604 template <template <class> class Template>
14606 template <class Arg> using Bind = Template<Arg>;
14609 template <template <class> class Template, class Arg>
14610 using Instantiate = Template<Arg>; //#0
14612 template <template <class> class Template,
14615 Instantiate<Internal<Template>::template Bind,
14618 When #1 is parsed, the
14619 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14620 parameter `Template' in #0 matches the
14621 UNBOUND_CLASS_TEMPLATE representing the argument
14622 `Internal<Template>::template Bind'; We then want
14623 to assemble the type `Bind<Argument>' that can't
14624 be fully created right now, because
14625 `Internal<Template>' not being complete, the Bind
14626 template cannot be looked up in that context. So
14627 we need to "store" `Bind<Argument>' for later
14628 when the context of Bind becomes complete. Let's
14629 store that in a TYPENAME_TYPE. */
14630 return make_typename_type (TYPE_CONTEXT (arg
),
14631 build_nt (TEMPLATE_ID_EXPR
,
14632 TYPE_IDENTIFIER (arg
),
14637 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14638 are resolving nested-types in the signature of a
14639 member function templates. Otherwise ARG is a
14640 TEMPLATE_DECL and is the real template to be
14642 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14643 arg
= TYPE_NAME (arg
);
14645 r
= lookup_template_class (arg
,
14647 DECL_CONTEXT (arg
),
14648 /*entering_scope=*/0,
14650 return cp_build_qualified_type_real
14651 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14653 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14656 /* TEMPLATE_PARM_INDEX. */
14657 return convert_from_reference (unshare_expr (arg
));
14661 /* This can happen during the attempted tsubst'ing in
14662 unify. This means that we don't yet have any information
14663 about the template parameter in question. */
14666 /* If we get here, we must have been looking at a parm for a
14667 more deeply nested template. Make a new version of this
14668 template parameter, but with a lower level. */
14671 case TEMPLATE_TYPE_PARM
:
14672 case TEMPLATE_TEMPLATE_PARM
:
14673 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14674 if (cp_type_quals (t
))
14676 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14677 r
= cp_build_qualified_type_real
14678 (r
, cp_type_quals (t
),
14679 complain
| (code
== TEMPLATE_TYPE_PARM
14680 ? tf_ignore_bad_quals
: 0));
14682 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14683 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14684 && (r
= (TEMPLATE_PARM_DESCENDANTS
14685 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14686 && (r
= TREE_TYPE (r
))
14687 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14688 /* Break infinite recursion when substituting the constraints
14689 of a constrained placeholder. */;
14690 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14691 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14692 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
14693 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
14694 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
14695 && (TEMPLATE_PARM_LEVEL (r
)
14696 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
14697 /* Cache the simple case of lowering a type parameter. */
14702 TEMPLATE_TYPE_PARM_INDEX (r
)
14703 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14704 r
, levels
, args
, complain
);
14705 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14706 TYPE_MAIN_VARIANT (r
) = r
;
14707 TYPE_POINTER_TO (r
) = NULL_TREE
;
14708 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14710 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14712 /* Propagate constraints on placeholders. */
14713 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14714 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14715 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14716 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14718 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14719 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14723 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14724 /* We have reduced the level of the template
14725 template parameter, but not the levels of its
14726 template parameters, so canonical_type_parameter
14727 will not be able to find the canonical template
14728 template parameter for this level. Thus, we
14729 require structural equality checking to compare
14730 TEMPLATE_TEMPLATE_PARMs. */
14731 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14732 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14733 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14735 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14737 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14739 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14740 /* We might need to substitute into the types of non-type
14741 template parameters. */
14742 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14743 complain
, in_decl
);
14744 if (tmpl
== error_mark_node
)
14745 return error_mark_node
;
14746 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14747 complain
, in_decl
);
14748 if (argvec
== error_mark_node
)
14749 return error_mark_node
;
14751 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14752 = build_template_info (tmpl
, argvec
);
14757 case TEMPLATE_PARM_INDEX
:
14758 /* OK, now substitute the type of the non-type parameter. We
14759 couldn't do it earlier because it might be an auto parameter,
14760 and we wouldn't need to if we had an argument. */
14761 type
= tsubst (type
, args
, complain
, in_decl
);
14762 if (type
== error_mark_node
)
14763 return error_mark_node
;
14764 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14768 gcc_unreachable ();
14776 tree purpose
, value
, chain
;
14778 if (t
== void_list_node
)
14781 purpose
= TREE_PURPOSE (t
);
14784 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14785 if (purpose
== error_mark_node
)
14786 return error_mark_node
;
14788 value
= TREE_VALUE (t
);
14791 value
= tsubst (value
, args
, complain
, in_decl
);
14792 if (value
== error_mark_node
)
14793 return error_mark_node
;
14795 chain
= TREE_CHAIN (t
);
14796 if (chain
&& chain
!= void_type_node
)
14798 chain
= tsubst (chain
, args
, complain
, in_decl
);
14799 if (chain
== error_mark_node
)
14800 return error_mark_node
;
14802 if (purpose
== TREE_PURPOSE (t
)
14803 && value
== TREE_VALUE (t
)
14804 && chain
== TREE_CHAIN (t
))
14806 return hash_tree_cons (purpose
, value
, chain
);
14810 /* We should never be tsubsting a binfo. */
14811 gcc_unreachable ();
14814 /* A vector of template arguments. */
14815 gcc_assert (!type
);
14816 return tsubst_template_args (t
, args
, complain
, in_decl
);
14819 case REFERENCE_TYPE
:
14821 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14826 Type deduction may fail for any of the following
14829 -- Attempting to create a pointer to reference type.
14830 -- Attempting to create a reference to a reference type or
14831 a reference to void.
14833 Core issue 106 says that creating a reference to a reference
14834 during instantiation is no longer a cause for failure. We
14835 only enforce this check in strict C++98 mode. */
14836 if ((TYPE_REF_P (type
)
14837 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14838 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14840 static location_t last_loc
;
14842 /* We keep track of the last time we issued this error
14843 message to avoid spewing a ton of messages during a
14844 single bad template instantiation. */
14845 if (complain
& tf_error
14846 && last_loc
!= input_location
)
14848 if (VOID_TYPE_P (type
))
14849 error ("forming reference to void");
14850 else if (code
== POINTER_TYPE
)
14851 error ("forming pointer to reference type %qT", type
);
14853 error ("forming reference to reference type %qT", type
);
14854 last_loc
= input_location
;
14857 return error_mark_node
;
14859 else if (TREE_CODE (type
) == FUNCTION_TYPE
14860 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14861 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14863 if (complain
& tf_error
)
14865 if (code
== POINTER_TYPE
)
14866 error ("forming pointer to qualified function type %qT",
14869 error ("forming reference to qualified function type %qT",
14872 return error_mark_node
;
14874 else if (code
== POINTER_TYPE
)
14876 r
= build_pointer_type (type
);
14877 if (TREE_CODE (type
) == METHOD_TYPE
)
14878 r
= build_ptrmemfunc_type (r
);
14880 else if (TYPE_REF_P (type
))
14881 /* In C++0x, during template argument substitution, when there is an
14882 attempt to create a reference to a reference type, reference
14883 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14885 "If a template-argument for a template-parameter T names a type
14886 that is a reference to a type A, an attempt to create the type
14887 'lvalue reference to cv T' creates the type 'lvalue reference to
14888 A,' while an attempt to create the type type rvalue reference to
14889 cv T' creates the type T"
14891 r
= cp_build_reference_type
14893 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14895 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14896 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14898 if (r
!= error_mark_node
)
14899 /* Will this ever be needed for TYPE_..._TO values? */
14906 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14907 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14911 Type deduction may fail for any of the following
14914 -- Attempting to create "pointer to member of T" when T
14915 is not a class type. */
14916 if (complain
& tf_error
)
14917 error ("creating pointer to member of non-class type %qT", r
);
14918 return error_mark_node
;
14920 if (TYPE_REF_P (type
))
14922 if (complain
& tf_error
)
14923 error ("creating pointer to member reference type %qT", type
);
14924 return error_mark_node
;
14926 if (VOID_TYPE_P (type
))
14928 if (complain
& tf_error
)
14929 error ("creating pointer to member of type void");
14930 return error_mark_node
;
14932 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14933 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14935 /* The type of the implicit object parameter gets its
14936 cv-qualifiers from the FUNCTION_TYPE. */
14939 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14940 type_memfn_rqual (type
));
14941 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14942 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14946 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14950 case FUNCTION_TYPE
:
14955 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14956 if (fntype
== error_mark_node
)
14957 return error_mark_node
;
14959 /* Substitute the exception specification. */
14960 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14961 /*defer_ok*/fndecl_type
);
14962 if (specs
== error_mark_node
)
14963 return error_mark_node
;
14965 fntype
= build_exception_variant (fntype
, specs
);
14970 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14971 if (domain
== error_mark_node
)
14972 return error_mark_node
;
14974 /* As an optimization, we avoid regenerating the array type if
14975 it will obviously be the same as T. */
14976 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14979 /* These checks should match the ones in create_array_type_for_decl.
14983 The deduction may fail for any of the following reasons:
14985 -- Attempting to create an array with an element type that
14986 is void, a function type, or a reference type, or [DR337]
14987 an abstract class type. */
14988 if (VOID_TYPE_P (type
)
14989 || TREE_CODE (type
) == FUNCTION_TYPE
14990 || (TREE_CODE (type
) == ARRAY_TYPE
14991 && TYPE_DOMAIN (type
) == NULL_TREE
)
14992 || TYPE_REF_P (type
))
14994 if (complain
& tf_error
)
14995 error ("creating array of %qT", type
);
14996 return error_mark_node
;
14999 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15000 return error_mark_node
;
15002 r
= build_cplus_array_type (type
, domain
);
15004 if (!valid_array_size_p (input_location
, r
, in_decl
,
15005 (complain
& tf_error
)))
15006 return error_mark_node
;
15008 if (TYPE_USER_ALIGN (t
))
15010 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15011 TYPE_USER_ALIGN (r
) = 1;
15017 case TYPENAME_TYPE
:
15019 tree ctx
= TYPE_CONTEXT (t
);
15020 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15022 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15023 if (ctx
== error_mark_node
15024 || TREE_VEC_LENGTH (ctx
) > 1)
15025 return error_mark_node
;
15026 if (TREE_VEC_LENGTH (ctx
) == 0)
15028 if (complain
& tf_error
)
15029 error ("%qD is instantiated for an empty pack",
15030 TYPENAME_TYPE_FULLNAME (t
));
15031 return error_mark_node
;
15033 ctx
= TREE_VEC_ELT (ctx
, 0);
15036 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15037 /*entering_scope=*/1);
15038 if (ctx
== error_mark_node
)
15039 return error_mark_node
;
15041 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15042 complain
, in_decl
);
15043 if (f
== error_mark_node
)
15044 return error_mark_node
;
15046 if (!MAYBE_CLASS_TYPE_P (ctx
))
15048 if (complain
& tf_error
)
15049 error ("%qT is not a class, struct, or union type", ctx
);
15050 return error_mark_node
;
15052 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15054 /* Normally, make_typename_type does not require that the CTX
15055 have complete type in order to allow things like:
15057 template <class T> struct S { typename S<T>::X Y; };
15059 But, such constructs have already been resolved by this
15060 point, so here CTX really should have complete type, unless
15061 it's a partial instantiation. */
15062 ctx
= complete_type (ctx
);
15063 if (!COMPLETE_TYPE_P (ctx
))
15065 if (complain
& tf_error
)
15066 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15067 return error_mark_node
;
15071 f
= make_typename_type (ctx
, f
, typename_type
,
15072 complain
| tf_keep_type_decl
);
15073 if (f
== error_mark_node
)
15075 if (TREE_CODE (f
) == TYPE_DECL
)
15077 complain
|= tf_ignore_bad_quals
;
15081 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15083 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15085 if (complain
& tf_error
)
15086 error ("%qT resolves to %qT, which is not an enumeration type",
15089 return error_mark_node
;
15091 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15093 if (complain
& tf_error
)
15094 error ("%qT resolves to %qT, which is is not a class type",
15097 return error_mark_node
;
15101 return cp_build_qualified_type_real
15102 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15105 case UNBOUND_CLASS_TEMPLATE
:
15107 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15108 in_decl
, /*entering_scope=*/1);
15109 tree name
= TYPE_IDENTIFIER (t
);
15110 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15112 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15113 return error_mark_node
;
15116 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15117 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15124 ++cp_unevaluated_operand
;
15125 ++c_inhibit_evaluation_warnings
;
15127 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15129 /*integral_constant_expression_p=*/false);
15131 --cp_unevaluated_operand
;
15132 --c_inhibit_evaluation_warnings
;
15134 type
= finish_typeof (type
);
15135 return cp_build_qualified_type_real (type
,
15137 | cp_type_quals (type
),
15141 case DECLTYPE_TYPE
:
15145 ++cp_unevaluated_operand
;
15146 ++c_inhibit_evaluation_warnings
;
15148 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15149 complain
|tf_decltype
, in_decl
,
15150 /*function_p*/false,
15151 /*integral_constant_expression*/false);
15153 --cp_unevaluated_operand
;
15154 --c_inhibit_evaluation_warnings
;
15156 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15157 type
= lambda_capture_field_type (type
,
15158 false /*explicit_init*/,
15159 DECLTYPE_FOR_REF_CAPTURE (t
));
15160 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15161 type
= lambda_proxy_type (type
);
15164 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15165 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15167 /* In a template ~id could be either a complement expression
15168 or an unqualified-id naming a destructor; if instantiating
15169 it produces an expression, it's not an id-expression or
15172 type
= finish_decltype_type (type
, id
, complain
);
15174 return cp_build_qualified_type_real (type
,
15176 | cp_type_quals (type
),
15177 complain
| tf_ignore_bad_quals
);
15180 case UNDERLYING_TYPE
:
15182 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15183 complain
, in_decl
);
15184 return finish_underlying_type (type
);
15187 case TYPE_ARGUMENT_PACK
:
15188 case NONTYPE_ARGUMENT_PACK
:
15192 if (code
== NONTYPE_ARGUMENT_PACK
)
15193 r
= make_node (code
);
15195 r
= cxx_make_type (code
);
15197 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15198 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15199 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15217 /* We should use one of the expression tsubsts for these codes. */
15218 gcc_unreachable ();
15221 sorry ("use of %qs in template", get_tree_code_name (code
));
15222 return error_mark_node
;
15226 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15227 expression on the left-hand side of the "." or "->" operator. We
15228 only do the lookup if we had a dependent BASELINK. Otherwise we
15229 adjust it onto the instantiated heirarchy. */
15232 tsubst_baselink (tree baselink
, tree object_type
,
15233 tree args
, tsubst_flags_t complain
, tree in_decl
)
15235 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15236 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15237 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15239 tree optype
= BASELINK_OPTYPE (baselink
);
15240 optype
= tsubst (optype
, args
, complain
, in_decl
);
15242 tree template_args
= NULL_TREE
;
15243 bool template_id_p
= false;
15244 tree fns
= BASELINK_FUNCTIONS (baselink
);
15245 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15247 template_id_p
= true;
15248 template_args
= TREE_OPERAND (fns
, 1);
15249 fns
= TREE_OPERAND (fns
, 0);
15251 template_args
= tsubst_template_args (template_args
, args
,
15252 complain
, in_decl
);
15255 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15256 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15257 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15261 tree name
= OVL_NAME (fns
);
15262 if (IDENTIFIER_CONV_OP_P (name
))
15263 name
= make_conv_op_name (optype
);
15265 if (name
== complete_dtor_identifier
)
15266 /* Treat as-if non-dependent below. */
15267 dependent_p
= false;
15269 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15272 if ((complain
& tf_error
)
15273 && constructor_name_p (name
, qualifying_scope
))
15274 error ("cannot call constructor %<%T::%D%> directly",
15275 qualifying_scope
, name
);
15276 return error_mark_node
;
15279 if (BASELINK_P (baselink
))
15280 fns
= BASELINK_FUNCTIONS (baselink
);
15283 /* We're going to overwrite pieces below, make a duplicate. */
15284 baselink
= copy_node (baselink
);
15286 /* If lookup found a single function, mark it as used at this point.
15287 (If lookup found multiple functions the one selected later by
15288 overload resolution will be marked as used at that point.) */
15289 if (!template_id_p
&& !really_overloaded_fn (fns
))
15291 tree fn
= OVL_FIRST (fns
);
15292 bool ok
= mark_used (fn
, complain
);
15293 if (!ok
&& !(complain
& tf_error
))
15294 return error_mark_node
;
15295 if (ok
&& BASELINK_P (baselink
))
15296 /* We might have instantiated an auto function. */
15297 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15300 if (BASELINK_P (baselink
))
15302 /* Add back the template arguments, if present. */
15304 BASELINK_FUNCTIONS (baselink
)
15305 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15307 /* Update the conversion operator type. */
15308 BASELINK_OPTYPE (baselink
) = optype
;
15312 object_type
= current_class_type
;
15314 if (qualified_p
|| !dependent_p
)
15316 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15320 /* We need to call adjust_result_of_qualified_name_lookup in case the
15321 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15322 so that we still get virtual function binding. */
15323 BASELINK_QUALIFIED_P (baselink
) = false;
15329 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15330 true if the qualified-id will be a postfix-expression in-and-of
15331 itself; false if more of the postfix-expression follows the
15332 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15336 tsubst_qualified_id (tree qualified_id
, tree args
,
15337 tsubst_flags_t complain
, tree in_decl
,
15338 bool done
, bool address_p
)
15344 tree template_args
;
15345 location_t loc
= UNKNOWN_LOCATION
;
15347 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15349 /* Figure out what name to look up. */
15350 name
= TREE_OPERAND (qualified_id
, 1);
15351 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15353 is_template
= true;
15354 loc
= EXPR_LOCATION (name
);
15355 template_args
= TREE_OPERAND (name
, 1);
15357 template_args
= tsubst_template_args (template_args
, args
,
15358 complain
, in_decl
);
15359 if (template_args
== error_mark_node
)
15360 return error_mark_node
;
15361 name
= TREE_OPERAND (name
, 0);
15365 is_template
= false;
15366 template_args
= NULL_TREE
;
15369 /* Substitute into the qualifying scope. When there are no ARGS, we
15370 are just trying to simplify a non-dependent expression. In that
15371 case the qualifying scope may be dependent, and, in any case,
15372 substituting will not help. */
15373 scope
= TREE_OPERAND (qualified_id
, 0);
15376 scope
= tsubst (scope
, args
, complain
, in_decl
);
15377 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15382 if (dependent_scope_p (scope
))
15385 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15386 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15387 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15388 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15392 if (!BASELINK_P (name
) && !DECL_P (expr
))
15394 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15396 /* A BIT_NOT_EXPR is used to represent a destructor. */
15397 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15399 error ("qualifying type %qT does not match destructor name ~%qT",
15400 scope
, TREE_OPERAND (expr
, 0));
15401 expr
= error_mark_node
;
15404 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15405 /*is_type_p=*/0, false);
15408 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15409 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15410 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15412 if (complain
& tf_error
)
15414 error ("dependent-name %qE is parsed as a non-type, but "
15415 "instantiation yields a type", qualified_id
);
15416 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15418 return error_mark_node
;
15424 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15426 /* Remember that there was a reference to this entity. */
15427 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15428 return error_mark_node
;
15431 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15433 if (complain
& tf_error
)
15434 qualified_name_lookup_error (scope
,
15435 TREE_OPERAND (qualified_id
, 1),
15436 expr
, input_location
);
15437 return error_mark_node
;
15442 /* We may be repeating a check already done during parsing, but
15443 if it was well-formed and passed then, it will pass again
15444 now, and if it didn't, we wouldn't have got here. The case
15445 we want to catch is when we couldn't tell then, and can now,
15446 namely when templ prior to substitution was an
15448 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15449 return error_mark_node
;
15451 if (variable_template_p (expr
))
15452 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15455 expr
= lookup_template_function (expr
, template_args
);
15458 if (expr
== error_mark_node
&& complain
& tf_error
)
15459 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15460 expr
, input_location
);
15461 else if (TYPE_P (scope
))
15463 expr
= (adjust_result_of_qualified_name_lookup
15464 (expr
, scope
, current_nonlambda_class_type ()));
15465 expr
= (finish_qualified_id_expr
15466 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15467 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15468 /*template_arg_p=*/false, complain
));
15471 /* Expressions do not generally have reference type. */
15472 if (TREE_CODE (expr
) != SCOPE_REF
15473 /* However, if we're about to form a pointer-to-member, we just
15474 want the referenced member referenced. */
15475 && TREE_CODE (expr
) != OFFSET_REF
)
15476 expr
= convert_from_reference (expr
);
15478 if (REF_PARENTHESIZED_P (qualified_id
))
15479 expr
= force_paren_expr (expr
);
15484 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15485 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15489 tsubst_init (tree init
, tree decl
, tree args
,
15490 tsubst_flags_t complain
, tree in_decl
)
15495 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15497 tree type
= TREE_TYPE (decl
);
15499 if (!init
&& type
!= error_mark_node
)
15501 if (tree auto_node
= type_uses_auto (type
))
15503 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
15505 if (complain
& tf_error
)
15506 error ("initializer for %q#D expands to an empty list "
15507 "of expressions", decl
);
15508 return error_mark_node
;
15511 else if (!dependent_type_p (type
))
15513 /* If we had an initializer but it
15514 instantiated to nothing,
15515 value-initialize the object. This will
15516 only occur when the initializer was a
15517 pack expansion where the parameter packs
15518 used in that expansion were of length
15520 init
= build_value_init (type
, complain
);
15521 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
15522 init
= get_target_expr_sfinae (init
, complain
);
15523 if (TREE_CODE (init
) == TARGET_EXPR
)
15524 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
15531 /* Like tsubst, but deals with expressions. This function just replaces
15532 template parms; to finish processing the resultant expression, use
15533 tsubst_copy_and_build or tsubst_expr. */
15536 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15538 enum tree_code code
;
15541 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15544 code
= TREE_CODE (t
);
15549 r
= retrieve_local_specialization (t
);
15551 if (r
== NULL_TREE
)
15553 /* We get here for a use of 'this' in an NSDMI. */
15554 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15555 return current_class_ptr
;
15557 /* This can happen for a parameter name used later in a function
15558 declaration (such as in a late-specified return type). Just
15559 make a dummy decl, since it's only used for its type. */
15560 gcc_assert (cp_unevaluated_operand
!= 0);
15561 r
= tsubst_decl (t
, args
, complain
);
15562 /* Give it the template pattern as its context; its true context
15563 hasn't been instantiated yet and this is good enough for
15565 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15568 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15569 r
= argument_pack_select_arg (r
);
15570 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15571 return error_mark_node
;
15579 if (DECL_TEMPLATE_PARM_P (t
))
15580 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15581 /* There is no need to substitute into namespace-scope
15583 if (DECL_NAMESPACE_SCOPE_P (t
))
15585 /* If ARGS is NULL, then T is known to be non-dependent. */
15586 if (args
== NULL_TREE
)
15587 return scalar_constant_value (t
);
15589 /* Unfortunately, we cannot just call lookup_name here.
15592 template <int I> int f() {
15594 struct S { void g() { E e = a; } };
15597 When we instantiate f<7>::S::g(), say, lookup_name is not
15598 clever enough to find f<7>::a. */
15600 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15601 /*entering_scope=*/0);
15603 for (v
= TYPE_VALUES (enum_type
);
15605 v
= TREE_CHAIN (v
))
15606 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15607 return TREE_VALUE (v
);
15609 /* We didn't find the name. That should never happen; if
15610 name-lookup found it during preliminary parsing, we
15611 should find it again here during instantiation. */
15612 gcc_unreachable ();
15617 if (DECL_CONTEXT (t
))
15621 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15622 /*entering_scope=*/1);
15623 if (ctx
!= DECL_CONTEXT (t
))
15625 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15628 if (complain
& tf_error
)
15629 error ("using invalid field %qD", t
);
15630 return error_mark_node
;
15639 case FUNCTION_DECL
:
15640 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15641 r
= tsubst (t
, args
, complain
, in_decl
);
15642 else if (local_variable_p (t
)
15643 && uses_template_parms (DECL_CONTEXT (t
)))
15645 r
= retrieve_local_specialization (t
);
15646 if (r
== NULL_TREE
)
15648 /* First try name lookup to find the instantiation. */
15649 r
= lookup_name (DECL_NAME (t
));
15654 /* During error-recovery we may find a non-variable,
15655 even an OVERLOAD: just bail out and avoid ICEs and
15656 duplicate diagnostics (c++/62207). */
15657 gcc_assert (seen_error ());
15658 return error_mark_node
;
15660 if (!is_capture_proxy (r
))
15662 /* Make sure the one we found is the one we want. */
15663 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15664 if (ctx
!= DECL_CONTEXT (r
))
15673 /* This can happen for a variable used in a
15674 late-specified return type of a local lambda, or for a
15675 local static or constant. Building a new VAR_DECL
15676 should be OK in all those cases. */
15677 r
= tsubst_decl (t
, args
, complain
);
15678 if (local_specializations
)
15679 /* Avoid infinite recursion (79640). */
15680 register_local_specialization (r
, t
);
15681 if (decl_maybe_constant_var_p (r
))
15683 /* We can't call cp_finish_decl, so handle the
15684 initializer by hand. */
15685 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15686 complain
, in_decl
);
15687 if (!processing_template_decl
)
15688 init
= maybe_constant_init (init
);
15689 if (processing_template_decl
15690 ? potential_constant_expression (init
)
15691 : reduced_constant_expression_p (init
))
15692 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15693 = TREE_CONSTANT (r
) = true;
15694 DECL_INITIAL (r
) = init
;
15695 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15697 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15698 complain
, adc_variable_type
);
15700 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15701 || decl_constant_var_p (r
)
15703 if (!processing_template_decl
15704 && !TREE_STATIC (r
))
15705 r
= process_outer_var_ref (r
, complain
);
15707 /* Remember this for subsequent uses. */
15708 if (local_specializations
)
15709 register_local_specialization (r
, t
);
15711 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15712 r
= argument_pack_select_arg (r
);
15716 if (!mark_used (r
, complain
))
15717 return error_mark_node
;
15720 case NAMESPACE_DECL
:
15727 return tsubst_baselink (t
, current_nonlambda_class_type (),
15728 args
, complain
, in_decl
);
15730 case TEMPLATE_DECL
:
15731 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15732 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15733 args
, complain
, in_decl
);
15734 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15735 return tsubst (t
, args
, complain
, in_decl
);
15736 else if (DECL_CLASS_SCOPE_P (t
)
15737 && uses_template_parms (DECL_CONTEXT (t
)))
15739 /* Template template argument like the following example need
15742 template <template <class> class TT> struct C {};
15743 template <class T> struct D {
15744 template <class U> struct E {};
15749 We are processing the template argument `E' in #1 for
15750 the template instantiation #2. Originally, `E' is a
15751 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15752 have to substitute this with one having context `D<int>'. */
15754 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15755 if (dependent_scope_p (context
))
15757 /* When rewriting a constructor into a deduction guide, a
15758 non-dependent name can become dependent, so memtmpl<args>
15759 becomes context::template memtmpl<args>. */
15760 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15761 return build_qualified_name (type
, context
, DECL_NAME (t
),
15764 return lookup_field (context
, DECL_NAME(t
), 0, false);
15767 /* Ordinary template template argument. */
15770 case NON_LVALUE_EXPR
:
15771 case VIEW_CONVERT_EXPR
:
15773 /* Handle location wrappers by substituting the wrapped node
15774 first, *then* reusing the resulting type. Doing the type
15775 first ensures that we handle template parameters and
15776 parameter pack expansions. */
15777 if (location_wrapper_p (t
))
15779 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
15780 complain
, in_decl
);
15781 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15783 tree op
= TREE_OPERAND (t
, 0);
15784 if (code
== VIEW_CONVERT_EXPR
15785 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15787 /* Wrapper to make a C++20 template parameter object const. */
15788 op
= tsubst_copy (op
, args
, complain
, in_decl
);
15789 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15791 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15792 return build1 (code
, type
, op
);
15796 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
15800 /* We shouldn't see any other uses of these in templates. */
15801 gcc_unreachable ();
15805 case REINTERPRET_CAST_EXPR
:
15806 case CONST_CAST_EXPR
:
15807 case STATIC_CAST_EXPR
:
15808 case DYNAMIC_CAST_EXPR
:
15809 case IMPLICIT_CONV_EXPR
:
15813 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15814 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15815 return build1 (code
, type
, op0
);
15819 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15820 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15822 tree expanded
, op
= TREE_OPERAND (t
, 0);
15825 if (SIZEOF_EXPR_TYPE_P (t
))
15826 op
= TREE_TYPE (op
);
15828 ++cp_unevaluated_operand
;
15829 ++c_inhibit_evaluation_warnings
;
15830 /* We only want to compute the number of arguments. */
15831 if (PACK_EXPANSION_P (op
))
15832 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15834 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15835 args
, complain
, in_decl
);
15836 --cp_unevaluated_operand
;
15837 --c_inhibit_evaluation_warnings
;
15839 if (TREE_CODE (expanded
) == TREE_VEC
)
15841 len
= TREE_VEC_LENGTH (expanded
);
15842 /* Set TREE_USED for the benefit of -Wunused. */
15843 for (int i
= 0; i
< len
; i
++)
15844 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15845 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15848 if (expanded
== error_mark_node
)
15849 return error_mark_node
;
15850 else if (PACK_EXPANSION_P (expanded
)
15851 || (TREE_CODE (expanded
) == TREE_VEC
15852 && pack_expansion_args_count (expanded
)))
15855 if (PACK_EXPANSION_P (expanded
))
15857 else if (TREE_VEC_LENGTH (expanded
) == 1)
15858 expanded
= TREE_VEC_ELT (expanded
, 0);
15860 expanded
= make_argument_pack (expanded
);
15862 if (TYPE_P (expanded
))
15863 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15865 complain
& tf_error
);
15867 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15868 complain
& tf_error
);
15871 return build_int_cst (size_type_node
, len
);
15873 if (SIZEOF_EXPR_TYPE_P (t
))
15875 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15876 args
, complain
, in_decl
);
15877 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15878 r
= build1 (SIZEOF_EXPR
,
15879 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15880 SIZEOF_EXPR_TYPE_P (r
) = 1;
15887 case TRUTH_NOT_EXPR
:
15890 case UNARY_PLUS_EXPR
: /* Unary + */
15892 case AT_ENCODE_EXPR
:
15896 case REALPART_EXPR
:
15897 case IMAGPART_EXPR
:
15900 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15901 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15902 r
= build1 (code
, type
, op0
);
15903 if (code
== ALIGNOF_EXPR
)
15904 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
15908 case COMPONENT_REF
:
15913 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15914 name
= TREE_OPERAND (t
, 1);
15915 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15917 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15918 complain
, in_decl
);
15919 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15921 else if (TREE_CODE (name
) == SCOPE_REF
15922 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15924 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15925 complain
, in_decl
);
15926 name
= TREE_OPERAND (name
, 1);
15927 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15928 complain
, in_decl
);
15929 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15930 name
= build_qualified_name (/*type=*/NULL_TREE
,
15932 /*template_p=*/false);
15934 else if (BASELINK_P (name
))
15935 name
= tsubst_baselink (name
,
15936 non_reference (TREE_TYPE (object
)),
15940 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15941 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15947 case TRUNC_DIV_EXPR
:
15948 case CEIL_DIV_EXPR
:
15949 case FLOOR_DIV_EXPR
:
15950 case ROUND_DIV_EXPR
:
15951 case EXACT_DIV_EXPR
:
15955 case TRUNC_MOD_EXPR
:
15956 case FLOOR_MOD_EXPR
:
15957 case TRUTH_ANDIF_EXPR
:
15958 case TRUTH_ORIF_EXPR
:
15959 case TRUTH_AND_EXPR
:
15960 case TRUTH_OR_EXPR
:
15973 case COMPOUND_EXPR
:
15976 case PREDECREMENT_EXPR
:
15977 case PREINCREMENT_EXPR
:
15978 case POSTDECREMENT_EXPR
:
15979 case POSTINCREMENT_EXPR
:
15981 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15982 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15983 return build_nt (code
, op0
, op1
);
15988 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15989 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15990 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15991 QUALIFIED_NAME_IS_TEMPLATE (t
));
15996 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15997 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15998 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16003 int n
= VL_EXP_OPERAND_LENGTH (t
);
16004 tree result
= build_vl_exp (CALL_EXPR
, n
);
16006 for (i
= 0; i
< n
; i
++)
16007 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16008 complain
, in_decl
);
16014 case PSEUDO_DTOR_EXPR
:
16015 case VEC_PERM_EXPR
:
16017 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16018 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16019 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16020 r
= build_nt (code
, op0
, op1
, op2
);
16021 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16027 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16028 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16029 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16030 r
= build_nt (code
, op0
, op1
, op2
);
16031 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16037 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16038 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16039 r
= build_nt (code
, op0
, op1
);
16040 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16041 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16045 case TEMPLATE_ID_EXPR
:
16047 /* Substituted template arguments */
16048 tree fn
= TREE_OPERAND (t
, 0);
16049 tree targs
= TREE_OPERAND (t
, 1);
16051 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16053 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16055 return lookup_template_function (fn
, targs
);
16060 tree purpose
, value
, chain
;
16062 if (t
== void_list_node
)
16065 purpose
= TREE_PURPOSE (t
);
16067 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16068 value
= TREE_VALUE (t
);
16070 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16071 chain
= TREE_CHAIN (t
);
16072 if (chain
&& chain
!= void_type_node
)
16073 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16074 if (purpose
== TREE_PURPOSE (t
)
16075 && value
== TREE_VALUE (t
)
16076 && chain
== TREE_CHAIN (t
))
16078 return tree_cons (purpose
, value
, chain
);
16083 case ENUMERAL_TYPE
:
16085 case TEMPLATE_TYPE_PARM
:
16086 case TEMPLATE_TEMPLATE_PARM
:
16087 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16088 case TEMPLATE_PARM_INDEX
:
16090 case REFERENCE_TYPE
:
16092 case FUNCTION_TYPE
:
16095 case TYPENAME_TYPE
:
16096 case UNBOUND_CLASS_TEMPLATE
:
16098 case DECLTYPE_TYPE
:
16100 return tsubst (t
, args
, complain
, in_decl
);
16104 /* Fall through. */
16105 case IDENTIFIER_NODE
:
16106 if (IDENTIFIER_CONV_OP_P (t
))
16108 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16109 return make_conv_op_name (new_type
);
16115 /* This is handled by tsubst_copy_and_build. */
16116 gcc_unreachable ();
16120 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16121 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16122 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16125 case CLEANUP_POINT_EXPR
:
16126 /* We shouldn't have built any of these during initial template
16127 generation. Instead, they should be built during instantiation
16128 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16129 gcc_unreachable ();
16133 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16134 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16135 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16136 r
= build2 (code
, type
, op0
, op1
);
16137 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16138 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16139 && !(complain
& tf_error
))
16140 return error_mark_node
;
16144 case EXPR_PACK_EXPANSION
:
16145 error ("invalid use of pack expansion expression");
16146 return error_mark_node
;
16148 case NONTYPE_ARGUMENT_PACK
:
16149 error ("use %<...%> to expand argument pack");
16150 return error_mark_node
;
16153 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16161 /* Instantiate any typedefs in the type. */
16162 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16163 r
= fold_convert (type
, t
);
16164 gcc_assert (TREE_CODE (r
) == code
);
16169 /* These can sometimes show up in a partial instantiation, but never
16170 involve template parms. */
16171 gcc_assert (!uses_template_parms (t
));
16174 case UNARY_LEFT_FOLD_EXPR
:
16175 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16176 case UNARY_RIGHT_FOLD_EXPR
:
16177 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16178 case BINARY_LEFT_FOLD_EXPR
:
16179 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16180 case BINARY_RIGHT_FOLD_EXPR
:
16181 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16185 case DEBUG_BEGIN_STMT
:
16186 /* ??? There's no point in copying it for now, but maybe some
16187 day it will contain more information, such as a pointer back
16188 to the containing function, inlined copy or so. */
16192 /* We shouldn't get here, but keep going if !flag_checking. */
16194 gcc_unreachable ();
16199 /* Helper function for tsubst_omp_clauses, used for instantiation of
16200 OMP_CLAUSE_DECL of clauses. */
16203 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16204 tree in_decl
, tree
*iterator_cache
)
16206 if (decl
== NULL_TREE
)
16209 /* Handle OpenMP iterators. */
16210 if (TREE_CODE (decl
) == TREE_LIST
16211 && TREE_PURPOSE (decl
)
16212 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16215 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16216 ret
= iterator_cache
[1];
16220 begin_scope (sk_omp
, NULL
);
16221 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16223 *tp
= copy_node (it
);
16224 TREE_VEC_ELT (*tp
, 0)
16225 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16226 TREE_VEC_ELT (*tp
, 1)
16227 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16228 /*integral_constant_expression_p=*/false);
16229 TREE_VEC_ELT (*tp
, 2)
16230 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16231 /*integral_constant_expression_p=*/false);
16232 TREE_VEC_ELT (*tp
, 3)
16233 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16234 /*integral_constant_expression_p=*/false);
16235 TREE_CHAIN (*tp
) = NULL_TREE
;
16236 tp
= &TREE_CHAIN (*tp
);
16238 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16239 iterator_cache
[0] = TREE_PURPOSE (decl
);
16240 iterator_cache
[1] = ret
;
16242 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16247 /* Handle an OpenMP array section represented as a TREE_LIST (or
16248 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16249 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16250 TREE_LIST. We can handle it exactly the same as an array section
16251 (purpose, value, and a chain), even though the nomenclature
16252 (low_bound, length, etc) is different. */
16253 if (TREE_CODE (decl
) == TREE_LIST
)
16256 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16257 /*integral_constant_expression_p=*/false);
16258 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16259 /*integral_constant_expression_p=*/false);
16260 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16262 if (TREE_PURPOSE (decl
) == low_bound
16263 && TREE_VALUE (decl
) == length
16264 && TREE_CHAIN (decl
) == chain
)
16266 tree ret
= tree_cons (low_bound
, length
, chain
);
16267 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16268 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16271 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16272 /*integral_constant_expression_p=*/false);
16273 /* Undo convert_from_reference tsubst_expr could have called. */
16275 && REFERENCE_REF_P (ret
)
16276 && !REFERENCE_REF_P (decl
))
16277 ret
= TREE_OPERAND (ret
, 0);
16281 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16284 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16285 tree args
, tsubst_flags_t complain
, tree in_decl
)
16287 tree new_clauses
= NULL_TREE
, nc
, oc
;
16288 tree linear_no_step
= NULL_TREE
;
16289 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16291 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16293 nc
= copy_node (oc
);
16294 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16297 switch (OMP_CLAUSE_CODE (nc
))
16299 case OMP_CLAUSE_LASTPRIVATE
:
16300 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16302 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16303 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16304 in_decl
, /*integral_constant_expression_p=*/false);
16305 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16306 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16309 case OMP_CLAUSE_PRIVATE
:
16310 case OMP_CLAUSE_SHARED
:
16311 case OMP_CLAUSE_FIRSTPRIVATE
:
16312 case OMP_CLAUSE_COPYIN
:
16313 case OMP_CLAUSE_COPYPRIVATE
:
16314 case OMP_CLAUSE_UNIFORM
:
16315 case OMP_CLAUSE_DEPEND
:
16316 case OMP_CLAUSE_FROM
:
16317 case OMP_CLAUSE_TO
:
16318 case OMP_CLAUSE_MAP
:
16319 case OMP_CLAUSE_NONTEMPORAL
:
16320 case OMP_CLAUSE_USE_DEVICE_PTR
:
16321 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16322 case OMP_CLAUSE_IS_DEVICE_PTR
:
16323 case OMP_CLAUSE_INCLUSIVE
:
16324 case OMP_CLAUSE_EXCLUSIVE
:
16325 OMP_CLAUSE_DECL (nc
)
16326 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16327 in_decl
, iterator_cache
);
16329 case OMP_CLAUSE_TILE
:
16330 case OMP_CLAUSE_IF
:
16331 case OMP_CLAUSE_NUM_THREADS
:
16332 case OMP_CLAUSE_SCHEDULE
:
16333 case OMP_CLAUSE_COLLAPSE
:
16334 case OMP_CLAUSE_FINAL
:
16335 case OMP_CLAUSE_DEVICE
:
16336 case OMP_CLAUSE_DIST_SCHEDULE
:
16337 case OMP_CLAUSE_NUM_TEAMS
:
16338 case OMP_CLAUSE_THREAD_LIMIT
:
16339 case OMP_CLAUSE_SAFELEN
:
16340 case OMP_CLAUSE_SIMDLEN
:
16341 case OMP_CLAUSE_NUM_TASKS
:
16342 case OMP_CLAUSE_GRAINSIZE
:
16343 case OMP_CLAUSE_PRIORITY
:
16344 case OMP_CLAUSE_ORDERED
:
16345 case OMP_CLAUSE_HINT
:
16346 case OMP_CLAUSE_NUM_GANGS
:
16347 case OMP_CLAUSE_NUM_WORKERS
:
16348 case OMP_CLAUSE_VECTOR_LENGTH
:
16349 case OMP_CLAUSE_WORKER
:
16350 case OMP_CLAUSE_VECTOR
:
16351 case OMP_CLAUSE_ASYNC
:
16352 case OMP_CLAUSE_WAIT
:
16353 OMP_CLAUSE_OPERAND (nc
, 0)
16354 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16355 in_decl
, /*integral_constant_expression_p=*/false);
16357 case OMP_CLAUSE_REDUCTION
:
16358 case OMP_CLAUSE_IN_REDUCTION
:
16359 case OMP_CLAUSE_TASK_REDUCTION
:
16360 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16362 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16363 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16365 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16366 complain
, in_decl
);
16367 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16368 = build_qualified_name (NULL_TREE
, scope
,
16369 TREE_OPERAND (placeholder
, 1),
16373 gcc_assert (identifier_p (placeholder
));
16375 OMP_CLAUSE_DECL (nc
)
16376 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16379 case OMP_CLAUSE_GANG
:
16380 case OMP_CLAUSE_ALIGNED
:
16381 OMP_CLAUSE_DECL (nc
)
16382 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16384 OMP_CLAUSE_OPERAND (nc
, 1)
16385 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16386 in_decl
, /*integral_constant_expression_p=*/false);
16388 case OMP_CLAUSE_LINEAR
:
16389 OMP_CLAUSE_DECL (nc
)
16390 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16392 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16394 gcc_assert (!linear_no_step
);
16395 linear_no_step
= nc
;
16397 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16398 OMP_CLAUSE_LINEAR_STEP (nc
)
16399 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16400 complain
, in_decl
, NULL
);
16402 OMP_CLAUSE_LINEAR_STEP (nc
)
16403 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16405 /*integral_constant_expression_p=*/false);
16407 case OMP_CLAUSE_NOWAIT
:
16408 case OMP_CLAUSE_DEFAULT
:
16409 case OMP_CLAUSE_UNTIED
:
16410 case OMP_CLAUSE_MERGEABLE
:
16411 case OMP_CLAUSE_INBRANCH
:
16412 case OMP_CLAUSE_NOTINBRANCH
:
16413 case OMP_CLAUSE_PROC_BIND
:
16414 case OMP_CLAUSE_FOR
:
16415 case OMP_CLAUSE_PARALLEL
:
16416 case OMP_CLAUSE_SECTIONS
:
16417 case OMP_CLAUSE_TASKGROUP
:
16418 case OMP_CLAUSE_NOGROUP
:
16419 case OMP_CLAUSE_THREADS
:
16420 case OMP_CLAUSE_SIMD
:
16421 case OMP_CLAUSE_DEFAULTMAP
:
16422 case OMP_CLAUSE_ORDER
:
16423 case OMP_CLAUSE_BIND
:
16424 case OMP_CLAUSE_INDEPENDENT
:
16425 case OMP_CLAUSE_AUTO
:
16426 case OMP_CLAUSE_SEQ
:
16427 case OMP_CLAUSE_IF_PRESENT
:
16428 case OMP_CLAUSE_FINALIZE
:
16431 gcc_unreachable ();
16433 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16434 switch (OMP_CLAUSE_CODE (nc
))
16436 case OMP_CLAUSE_SHARED
:
16437 case OMP_CLAUSE_PRIVATE
:
16438 case OMP_CLAUSE_FIRSTPRIVATE
:
16439 case OMP_CLAUSE_LASTPRIVATE
:
16440 case OMP_CLAUSE_COPYPRIVATE
:
16441 case OMP_CLAUSE_LINEAR
:
16442 case OMP_CLAUSE_REDUCTION
:
16443 case OMP_CLAUSE_IN_REDUCTION
:
16444 case OMP_CLAUSE_TASK_REDUCTION
:
16445 case OMP_CLAUSE_USE_DEVICE_PTR
:
16446 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16447 case OMP_CLAUSE_IS_DEVICE_PTR
:
16448 case OMP_CLAUSE_INCLUSIVE
:
16449 case OMP_CLAUSE_EXCLUSIVE
:
16450 /* tsubst_expr on SCOPE_REF results in returning
16451 finish_non_static_data_member result. Undo that here. */
16452 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16453 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16454 == IDENTIFIER_NODE
))
16456 tree t
= OMP_CLAUSE_DECL (nc
);
16459 switch (TREE_CODE (v
))
16461 case COMPONENT_REF
:
16465 case POINTER_PLUS_EXPR
:
16466 v
= TREE_OPERAND (v
, 0);
16469 if (DECL_CONTEXT (v
) == current_function_decl
16470 && DECL_ARTIFICIAL (v
)
16471 && DECL_NAME (v
) == this_identifier
)
16472 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16479 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16480 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16481 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16482 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16483 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16485 tree decl
= OMP_CLAUSE_DECL (nc
);
16488 retrofit_lang_decl (decl
);
16489 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16498 new_clauses
= nreverse (new_clauses
);
16499 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16501 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16502 if (linear_no_step
)
16503 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16504 if (nc
== linear_no_step
)
16506 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16510 return new_clauses
;
16513 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16516 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16519 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16521 tree purpose
, value
, chain
;
16526 if (TREE_CODE (t
) != TREE_LIST
)
16527 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16528 /*function_p=*/false,
16529 /*integral_constant_expression_p=*/false);
16531 if (t
== void_list_node
)
16534 purpose
= TREE_PURPOSE (t
);
16536 purpose
= RECUR (purpose
);
16537 value
= TREE_VALUE (t
);
16540 if (TREE_CODE (value
) != LABEL_DECL
)
16541 value
= RECUR (value
);
16544 value
= lookup_label (DECL_NAME (value
));
16545 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
16546 TREE_USED (value
) = 1;
16549 chain
= TREE_CHAIN (t
);
16550 if (chain
&& chain
!= void_type_node
)
16551 chain
= RECUR (chain
);
16552 return tree_cons (purpose
, value
, chain
);
16556 /* Used to temporarily communicate the list of #pragma omp parallel
16557 clauses to #pragma omp for instantiation if they are combined
16560 static tree
*omp_parallel_combined_clauses
;
16562 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
16563 tree
*, unsigned int *);
16565 /* Substitute one OMP_FOR iterator. */
16568 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
16569 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
16570 tree args
, tsubst_flags_t complain
, tree in_decl
,
16571 bool integral_constant_expression_p
)
16573 #define RECUR(NODE) \
16574 tsubst_expr ((NODE), args, complain, in_decl, \
16575 integral_constant_expression_p)
16576 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
16579 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
16580 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
16582 decl
= TREE_OPERAND (init
, 0);
16583 init
= TREE_OPERAND (init
, 1);
16584 tree decl_expr
= NULL_TREE
;
16585 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
16588 bool decomp
= false;
16589 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
16591 tree v
= DECL_VALUE_EXPR (decl
);
16592 if (TREE_CODE (v
) == ARRAY_REF
16593 && VAR_P (TREE_OPERAND (v
, 0))
16594 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
16596 tree decomp_first
= NULL_TREE
;
16597 unsigned decomp_cnt
= 0;
16598 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
16599 maybe_push_decl (d
);
16600 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
16601 in_decl
, &decomp_first
, &decomp_cnt
);
16603 if (d
== error_mark_node
)
16604 decl
= error_mark_node
;
16606 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
16608 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
16610 tree v
= build_nt (ARRAY_REF
, d
,
16611 size_int (decomp_cnt
- i
- 1),
16612 NULL_TREE
, NULL_TREE
);
16613 SET_DECL_VALUE_EXPR (decomp_first
, v
);
16614 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
16616 fit_decomposition_lang_decl (decomp_first
, d
);
16617 decomp_first
= DECL_CHAIN (decomp_first
);
16621 decl
= tsubst_decl (decl
, args
, complain
);
16623 maybe_push_decl (decl
);
16625 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
16627 /* We need to jump through some hoops to handle declarations in the
16628 init-statement, since we might need to handle auto deduction,
16629 but we need to keep control of initialization. */
16631 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
16632 decl
= tsubst_decl (decl
, args
, complain
);
16636 if (TREE_CODE (decl
) == SCOPE_REF
)
16638 decl
= RECUR (decl
);
16639 if (TREE_CODE (decl
) == COMPONENT_REF
)
16643 switch (TREE_CODE (v
))
16645 case COMPONENT_REF
:
16649 case POINTER_PLUS_EXPR
:
16650 v
= TREE_OPERAND (v
, 0);
16653 if (DECL_CONTEXT (v
) == current_function_decl
16654 && DECL_ARTIFICIAL (v
)
16655 && DECL_NAME (v
) == this_identifier
)
16657 decl
= TREE_OPERAND (decl
, 1);
16658 decl
= omp_privatize_field (decl
, false);
16668 decl
= RECUR (decl
);
16670 init
= RECUR (init
);
16672 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
16674 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
16675 if (TREE_CODE (o
) == TREE_LIST
)
16676 TREE_VEC_ELT (orig_declv
, i
)
16677 = tree_cons (RECUR (TREE_PURPOSE (o
)),
16678 RECUR (TREE_VALUE (o
)),
16681 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
16686 tree this_pre_body
= NULL_TREE
;
16687 tree orig_init
= NULL_TREE
;
16688 tree orig_decl
= NULL_TREE
;
16689 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
16690 orig_init
, cond
, incr
);
16693 if (orig_declv
== NULL_TREE
)
16694 orig_declv
= copy_node (declv
);
16695 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
16698 else if (orig_declv
)
16699 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16702 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16703 if (!range_for
&& auto_node
&& init
)
16705 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16707 gcc_assert (!type_dependent_expression_p (decl
));
16709 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
16713 /* Declare the variable, but don't let that initialize it. */
16714 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16715 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16717 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16722 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16723 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16724 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16726 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16727 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16728 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16729 NOP_EXPR
, rhs
, complain
);
16732 incr
= RECUR (incr
);
16733 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16734 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16736 TREE_VEC_ELT (declv
, i
) = decl
;
16737 TREE_VEC_ELT (initv
, i
) = init
;
16738 TREE_VEC_ELT (condv
, i
) = cond
;
16739 TREE_VEC_ELT (incrv
, i
) = incr
;
16745 /* Declare and initialize the variable. */
16753 for (j
= ((omp_parallel_combined_clauses
== NULL
16754 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
16756 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16758 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16759 && OMP_CLAUSE_DECL (*pc
) == decl
)
16761 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16762 && OMP_CLAUSE_DECL (*pc
) == decl
)
16766 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16768 *pc
= OMP_CLAUSE_CHAIN (c
);
16769 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16772 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16773 && OMP_CLAUSE_DECL (*pc
) == decl
)
16775 error ("iteration variable %qD should not be firstprivate",
16777 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16779 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16780 && OMP_CLAUSE_DECL (*pc
) == decl
)
16782 error ("iteration variable %qD should not be reduction",
16784 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16787 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16792 if (*pc
== NULL_TREE
)
16794 tree c
= build_omp_clause (input_location
,
16795 TREE_CODE (t
) == OMP_LOOP
16796 ? OMP_CLAUSE_LASTPRIVATE
16797 : OMP_CLAUSE_PRIVATE
);
16798 OMP_CLAUSE_DECL (c
) = decl
;
16799 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16802 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16807 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16808 if (COMPARISON_CLASS_P (cond
))
16810 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16811 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16812 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16815 cond
= RECUR (cond
);
16816 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16817 switch (TREE_CODE (incr
))
16819 case PREINCREMENT_EXPR
:
16820 case PREDECREMENT_EXPR
:
16821 case POSTINCREMENT_EXPR
:
16822 case POSTDECREMENT_EXPR
:
16823 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16824 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16827 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16828 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16830 tree rhs
= TREE_OPERAND (incr
, 1);
16831 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16832 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16833 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16834 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16835 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16839 incr
= RECUR (incr
);
16842 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16843 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16845 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16846 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16847 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16848 TREE_TYPE (decl
), lhs
,
16849 RECUR (TREE_OPERAND (incr
, 2))));
16851 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16852 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16853 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16855 tree rhs
= TREE_OPERAND (incr
, 2);
16856 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16857 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16858 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16859 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16860 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16864 incr
= RECUR (incr
);
16867 incr
= RECUR (incr
);
16871 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16872 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16873 TREE_VEC_ELT (declv
, i
) = decl
;
16874 TREE_VEC_ELT (initv
, i
) = init
;
16875 TREE_VEC_ELT (condv
, i
) = cond
;
16876 TREE_VEC_ELT (incrv
, i
) = incr
;
16881 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16882 of OMP_TARGET's body. */
16885 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16887 *walk_subtrees
= 0;
16888 switch (TREE_CODE (*tp
))
16893 case STATEMENT_LIST
:
16894 *walk_subtrees
= 1;
16902 /* Helper function for tsubst_expr. For decomposition declaration
16903 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16904 also the corresponding decls representing the identifiers
16905 of the decomposition declaration. Return DECL if successful
16906 or error_mark_node otherwise, set *FIRST to the first decl
16907 in the list chained through DECL_CHAIN and *CNT to the number
16911 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16912 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16915 tree decl2
, decl3
, prev
= decl
;
16917 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16918 for (decl2
= DECL_CHAIN (pattern_decl
);
16921 && DECL_DECOMPOSITION_P (decl2
)
16922 && DECL_NAME (decl2
);
16923 decl2
= DECL_CHAIN (decl2
))
16925 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16927 gcc_assert (errorcount
);
16928 return error_mark_node
;
16931 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16932 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16933 tree v
= DECL_VALUE_EXPR (decl2
);
16934 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16935 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16936 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16937 SET_DECL_VALUE_EXPR (decl2
, v
);
16938 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16940 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16943 gcc_assert (errorcount
);
16944 decl
= error_mark_node
;
16947 maybe_push_decl (decl3
);
16948 if (error_operand_p (decl3
))
16949 decl
= error_mark_node
;
16950 else if (decl
!= error_mark_node
16951 && DECL_CHAIN (decl3
) != prev
16954 gcc_assert (errorcount
);
16955 decl
= error_mark_node
;
16964 /* Return the proper local_specialization for init-capture pack DECL. */
16967 lookup_init_capture_pack (tree decl
)
16969 /* We handle normal pack captures by forwarding to the specialization of the
16970 captured parameter. We can't do that for pack init-captures; we need them
16971 to have their own local_specialization. We created the individual
16972 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16973 when we process the DECL_EXPR for the pack init-capture in the template.
16974 So, how do we find them? We don't know the capture proxy pack when
16975 building the individual resulting proxies, and we don't know the
16976 individual proxies when instantiating the pack. What we have in common is
16979 So...when we instantiate the FIELD_DECL, we stick the result in
16980 local_specializations. Then at the DECL_EXPR we look up that result, see
16981 how many elements it has, synthesize the names, and look them up. */
16983 tree cname
= DECL_NAME (decl
);
16984 tree val
= DECL_VALUE_EXPR (decl
);
16985 tree field
= TREE_OPERAND (val
, 1);
16986 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
16987 tree fpack
= retrieve_local_specialization (field
);
16988 if (fpack
== error_mark_node
)
16989 return error_mark_node
;
16992 tree vec
= NULL_TREE
;
16993 tree r
= NULL_TREE
;
16994 if (TREE_CODE (fpack
) == TREE_VEC
)
16996 len
= TREE_VEC_LENGTH (fpack
);
16997 vec
= make_tree_vec (len
);
16998 r
= make_node (NONTYPE_ARGUMENT_PACK
);
16999 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17001 for (int i
= 0; i
< len
; ++i
)
17003 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17004 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17006 TREE_VEC_ELT (vec
, i
) = elt
;
17013 /* Like tsubst_copy for expressions, etc. but also does semantic
17017 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17018 bool integral_constant_expression_p
)
17020 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17021 #define RECUR(NODE) \
17022 tsubst_expr ((NODE), args, complain, in_decl, \
17023 integral_constant_expression_p)
17029 if (t
== NULL_TREE
|| t
== error_mark_node
)
17032 loc
= input_location
;
17033 if (location_t eloc
= cp_expr_location (t
))
17034 input_location
= eloc
;
17035 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17036 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17038 switch (TREE_CODE (t
))
17040 case STATEMENT_LIST
:
17042 tree_stmt_iterator i
;
17043 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17044 RECUR (tsi_stmt (i
));
17048 case CTOR_INITIALIZER
:
17049 finish_mem_initializers (tsubst_initializer_list
17050 (TREE_OPERAND (t
, 0), args
));
17054 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17058 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17059 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17060 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17062 finish_expr_stmt (tmp
);
17066 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17071 tree decl
, pattern_decl
;
17074 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17075 if (TREE_CODE (decl
) == LABEL_DECL
)
17076 finish_label_decl (DECL_NAME (decl
));
17077 else if (TREE_CODE (decl
) == USING_DECL
)
17079 tree scope
= USING_DECL_SCOPE (decl
);
17080 tree name
= DECL_NAME (decl
);
17082 scope
= tsubst (scope
, args
, complain
, in_decl
);
17083 finish_nonmember_using_decl (scope
, name
);
17085 else if (is_capture_proxy (decl
)
17086 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17088 /* We're in tsubst_lambda_expr, we've already inserted a new
17089 capture proxy, so look it up and register it. */
17091 if (!DECL_PACK_P (decl
))
17093 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17094 /*nonclass*/1, /*block_p=*/true,
17095 /*ns_only*/0, LOOKUP_HIDDEN
);
17096 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17098 else if (is_normal_capture_proxy (decl
))
17100 inst
= (retrieve_local_specialization
17101 (DECL_CAPTURED_VARIABLE (decl
)));
17102 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
17105 inst
= lookup_init_capture_pack (decl
);
17107 register_local_specialization (inst
, decl
);
17110 else if (DECL_PRETTY_FUNCTION_P (decl
))
17111 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17113 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17114 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17115 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17116 /* Don't copy the old closure; we'll create a new one in
17117 tsubst_lambda_expr. */
17121 init
= DECL_INITIAL (decl
);
17122 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17123 for local variables, so save if DECL was declared constinit. */
17124 const bool constinit_p
17126 && DECL_LANG_SPECIFIC (decl
)
17127 && DECL_TEMPLATE_INFO (decl
)
17128 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17129 decl
= tsubst (decl
, args
, complain
, in_decl
);
17130 if (decl
!= error_mark_node
)
17132 /* By marking the declaration as instantiated, we avoid
17133 trying to instantiate it. Since instantiate_decl can't
17134 handle local variables, and since we've already done
17135 all that needs to be done, that's the right thing to
17138 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17139 if (VAR_P (decl
) && !DECL_NAME (decl
)
17140 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17141 /* Anonymous aggregates are a special case. */
17142 finish_anon_union (decl
);
17143 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17145 DECL_CONTEXT (decl
) = current_function_decl
;
17146 if (DECL_NAME (decl
) == this_identifier
)
17148 tree lam
= DECL_CONTEXT (current_function_decl
);
17149 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17150 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17152 insert_capture_proxy (decl
);
17154 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17155 /* We already did a pushtag. */;
17156 else if (TREE_CODE (decl
) == FUNCTION_DECL
17157 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17158 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17160 DECL_CONTEXT (decl
) = NULL_TREE
;
17162 DECL_CONTEXT (decl
) = current_function_decl
;
17163 cp_check_omp_declare_reduction (decl
);
17167 bool const_init
= false;
17168 unsigned int cnt
= 0;
17169 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17170 maybe_push_decl (decl
);
17173 && DECL_DECOMPOSITION_P (decl
)
17174 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17175 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17176 complain
, in_decl
, &first
,
17179 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17182 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17185 if (ndecl
!= error_mark_node
)
17186 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17188 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17189 constinit_p
? LOOKUP_CONSTINIT
: 0);
17191 if (ndecl
!= error_mark_node
)
17192 cp_finish_decomp (ndecl
, first
, cnt
);
17201 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17202 RECUR (FOR_INIT_STMT (t
));
17203 finish_init_stmt (stmt
);
17204 tmp
= RECUR (FOR_COND (t
));
17205 finish_for_cond (tmp
, stmt
, false, 0);
17206 tmp
= RECUR (FOR_EXPR (t
));
17207 finish_for_expr (tmp
, stmt
);
17209 bool prev
= note_iteration_stmt_body_start ();
17210 RECUR (FOR_BODY (t
));
17211 note_iteration_stmt_body_end (prev
);
17213 finish_for_stmt (stmt
);
17216 case RANGE_FOR_STMT
:
17218 /* Construct another range_for, if this is not a final
17219 substitution (for inside inside a generic lambda of a
17220 template). Otherwise convert to a regular for. */
17222 stmt
= (processing_template_decl
17223 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17224 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17225 RECUR (RANGE_FOR_INIT_STMT (t
));
17226 decl
= RANGE_FOR_DECL (t
);
17227 decl
= tsubst (decl
, args
, complain
, in_decl
);
17228 maybe_push_decl (decl
);
17229 expr
= RECUR (RANGE_FOR_EXPR (t
));
17231 tree decomp_first
= NULL_TREE
;
17232 unsigned decomp_cnt
= 0;
17233 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17234 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17236 &decomp_first
, &decomp_cnt
);
17238 if (processing_template_decl
)
17240 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17241 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17242 finish_range_for_decl (stmt
, decl
, expr
);
17243 if (decomp_first
&& decl
!= error_mark_node
)
17244 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17248 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17249 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17250 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17251 decomp_first
, decomp_cnt
,
17252 RANGE_FOR_IVDEP (t
), unroll
);
17255 bool prev
= note_iteration_stmt_body_start ();
17256 RECUR (RANGE_FOR_BODY (t
));
17257 note_iteration_stmt_body_end (prev
);
17258 finish_for_stmt (stmt
);
17263 stmt
= begin_while_stmt ();
17264 tmp
= RECUR (WHILE_COND (t
));
17265 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17267 bool prev
= note_iteration_stmt_body_start ();
17268 RECUR (WHILE_BODY (t
));
17269 note_iteration_stmt_body_end (prev
);
17271 finish_while_stmt (stmt
);
17275 stmt
= begin_do_stmt ();
17277 bool prev
= note_iteration_stmt_body_start ();
17278 RECUR (DO_BODY (t
));
17279 note_iteration_stmt_body_end (prev
);
17281 finish_do_body (stmt
);
17282 tmp
= RECUR (DO_COND (t
));
17283 finish_do_stmt (tmp
, stmt
, false, 0);
17287 stmt
= begin_if_stmt ();
17288 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17289 if (IF_STMT_CONSTEXPR_P (t
))
17290 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17291 tmp
= RECUR (IF_COND (t
));
17292 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17293 if (IF_STMT_CONSTEXPR_P (t
)
17294 && instantiation_dependent_expression_p (tmp
))
17296 /* We're partially instantiating a generic lambda, but the condition
17297 of the constexpr if is still dependent. Don't substitute into the
17298 branches now, just remember the template arguments. */
17299 do_poplevel (IF_SCOPE (stmt
));
17300 IF_COND (stmt
) = IF_COND (t
);
17301 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17302 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17303 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17307 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17308 /* Don't instantiate the THEN_CLAUSE. */;
17311 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17312 bool inhibit
= integer_zerop (folded
);
17314 ++c_inhibit_evaluation_warnings
;
17315 RECUR (THEN_CLAUSE (t
));
17317 --c_inhibit_evaluation_warnings
;
17319 finish_then_clause (stmt
);
17321 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17322 /* Don't instantiate the ELSE_CLAUSE. */;
17323 else if (ELSE_CLAUSE (t
))
17325 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17326 bool inhibit
= integer_nonzerop (folded
);
17327 begin_else_clause (stmt
);
17329 ++c_inhibit_evaluation_warnings
;
17330 RECUR (ELSE_CLAUSE (t
));
17332 --c_inhibit_evaluation_warnings
;
17333 finish_else_clause (stmt
);
17336 finish_if_stmt (stmt
);
17340 if (BIND_EXPR_BODY_BLOCK (t
))
17341 stmt
= begin_function_body ();
17343 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17344 ? BCS_TRY_BLOCK
: 0);
17346 RECUR (BIND_EXPR_BODY (t
));
17348 if (BIND_EXPR_BODY_BLOCK (t
))
17349 finish_function_body (stmt
);
17351 finish_compound_stmt (stmt
);
17355 finish_break_stmt ();
17358 case CONTINUE_STMT
:
17359 finish_continue_stmt ();
17363 stmt
= begin_switch_stmt ();
17364 tmp
= RECUR (SWITCH_STMT_COND (t
));
17365 finish_switch_cond (tmp
, stmt
);
17366 RECUR (SWITCH_STMT_BODY (t
));
17367 finish_switch_stmt (stmt
);
17370 case CASE_LABEL_EXPR
:
17372 tree decl
= CASE_LABEL (t
);
17373 tree low
= RECUR (CASE_LOW (t
));
17374 tree high
= RECUR (CASE_HIGH (t
));
17375 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17376 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17378 tree label
= CASE_LABEL (l
);
17379 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17380 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17381 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17388 tree decl
= LABEL_EXPR_LABEL (t
);
17391 label
= finish_label_stmt (DECL_NAME (decl
));
17392 if (TREE_CODE (label
) == LABEL_DECL
)
17393 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17394 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17395 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17400 tmp
= GOTO_DESTINATION (t
);
17401 if (TREE_CODE (tmp
) != LABEL_DECL
)
17402 /* Computed goto's must be tsubst'd into. On the other hand,
17403 non-computed gotos must not be; the identifier in question
17404 will have no binding. */
17407 tmp
= DECL_NAME (tmp
);
17408 finish_goto_stmt (tmp
);
17413 tree string
= RECUR (ASM_STRING (t
));
17414 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17415 complain
, in_decl
);
17416 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17417 complain
, in_decl
);
17418 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17419 complain
, in_decl
);
17420 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17421 complain
, in_decl
);
17422 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
17423 outputs
, inputs
, clobbers
, labels
,
17425 tree asm_expr
= tmp
;
17426 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17427 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17428 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17435 stmt
= begin_try_block ();
17436 RECUR (TRY_STMTS (t
));
17437 finish_cleanup_try_block (stmt
);
17438 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17442 tree compound_stmt
= NULL_TREE
;
17444 if (FN_TRY_BLOCK_P (t
))
17445 stmt
= begin_function_try_block (&compound_stmt
);
17447 stmt
= begin_try_block ();
17449 RECUR (TRY_STMTS (t
));
17451 if (FN_TRY_BLOCK_P (t
))
17452 finish_function_try_block (stmt
);
17454 finish_try_block (stmt
);
17456 RECUR (TRY_HANDLERS (t
));
17457 if (FN_TRY_BLOCK_P (t
))
17458 finish_function_handler_sequence (stmt
, compound_stmt
);
17460 finish_handler_sequence (stmt
);
17466 tree decl
= HANDLER_PARMS (t
);
17470 decl
= tsubst (decl
, args
, complain
, in_decl
);
17471 /* Prevent instantiate_decl from trying to instantiate
17472 this variable. We've already done all that needs to be
17474 if (decl
!= error_mark_node
)
17475 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17477 stmt
= begin_handler ();
17478 finish_handler_parms (decl
, stmt
);
17479 RECUR (HANDLER_BODY (t
));
17480 finish_handler (stmt
);
17485 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17486 if (CLASS_TYPE_P (tmp
))
17488 /* Local classes are not independent templates; they are
17489 instantiated along with their containing function. And this
17490 way we don't have to deal with pushing out of one local class
17491 to instantiate a member of another local class. */
17492 /* Closures are handled by the LAMBDA_EXPR. */
17493 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17494 complete_type (tmp
);
17495 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17497 || (TREE_CODE (fld
) == FUNCTION_DECL
17498 && !DECL_ARTIFICIAL (fld
)))
17499 && DECL_TEMPLATE_INSTANTIATION (fld
))
17500 instantiate_decl (fld
, /*defer_ok=*/false,
17501 /*expl_inst_class=*/false);
17505 case STATIC_ASSERT
:
17509 ++c_inhibit_evaluation_warnings
;
17511 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17514 /*integral_constant_expression_p=*/true);
17515 --c_inhibit_evaluation_warnings
;
17517 finish_static_assert (condition
,
17518 STATIC_ASSERT_MESSAGE (t
),
17519 STATIC_ASSERT_SOURCE_LOCATION (t
),
17520 /*member_p=*/false);
17525 case OACC_PARALLEL
:
17526 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
17528 stmt
= begin_omp_parallel ();
17529 RECUR (OMP_BODY (t
));
17530 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
17534 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
17535 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
17536 complain
, in_decl
);
17537 if (OMP_PARALLEL_COMBINED (t
))
17538 omp_parallel_combined_clauses
= &tmp
;
17539 stmt
= begin_omp_parallel ();
17540 RECUR (OMP_PARALLEL_BODY (t
));
17541 gcc_assert (omp_parallel_combined_clauses
== NULL
);
17542 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
17543 = OMP_PARALLEL_COMBINED (t
);
17544 pop_omp_privatization_clauses (r
);
17548 if (OMP_TASK_BODY (t
) == NULL_TREE
)
17550 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17551 complain
, in_decl
);
17553 OMP_TASK_CLAUSES (t
) = tmp
;
17557 r
= push_omp_privatization_clauses (false);
17558 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17559 complain
, in_decl
);
17560 stmt
= begin_omp_task ();
17561 RECUR (OMP_TASK_BODY (t
));
17562 finish_omp_task (tmp
, stmt
);
17563 pop_omp_privatization_clauses (r
);
17569 case OMP_DISTRIBUTE
:
17573 tree clauses
, body
, pre_body
;
17574 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
17575 tree orig_declv
= NULL_TREE
;
17576 tree incrv
= NULL_TREE
;
17577 enum c_omp_region_type ort
= C_ORT_OMP
;
17578 bool any_range_for
= false;
17581 if (TREE_CODE (t
) == OACC_LOOP
)
17584 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
17585 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
17587 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17589 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17590 if (OMP_FOR_ORIG_DECLS (t
))
17591 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17592 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17593 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17594 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17597 keep_next_level (true);
17598 stmt
= begin_omp_structured_block ();
17600 pre_body
= push_stmt_list ();
17601 RECUR (OMP_FOR_PRE_BODY (t
));
17602 pre_body
= pop_stmt_list (pre_body
);
17604 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17605 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17607 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
17608 condv
, incrv
, &clauses
, args
,
17610 integral_constant_expression_p
);
17611 omp_parallel_combined_clauses
= NULL
;
17615 gcc_assert (orig_declv
);
17616 body
= begin_omp_structured_block ();
17617 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17618 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
17619 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
17620 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
17621 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
17622 TREE_VEC_ELT (declv
, i
));
17625 body
= push_stmt_list ();
17626 RECUR (OMP_FOR_BODY (t
));
17628 body
= finish_omp_structured_block (body
);
17630 body
= pop_stmt_list (body
);
17632 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17633 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
17634 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
17638 t
= make_node (TREE_CODE (t
));
17639 TREE_TYPE (t
) = void_type_node
;
17640 OMP_FOR_BODY (t
) = body
;
17641 OMP_FOR_PRE_BODY (t
) = pre_body
;
17642 OMP_FOR_CLAUSES (t
) = clauses
;
17643 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
17647 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
17649 pop_omp_privatization_clauses (r
);
17654 omp_parallel_combined_clauses
= NULL
;
17659 case OMP_TASKGROUP
:
17661 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
17662 && OMP_TEAMS_COMBINED (t
));
17663 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
17665 if (TREE_CODE (t
) == OMP_TEAMS
)
17667 keep_next_level (true);
17668 stmt
= begin_omp_structured_block ();
17669 RECUR (OMP_BODY (t
));
17670 stmt
= finish_omp_structured_block (stmt
);
17674 stmt
= push_stmt_list ();
17675 RECUR (OMP_BODY (t
));
17676 stmt
= pop_stmt_list (stmt
);
17680 OMP_BODY (t
) = stmt
;
17681 OMP_CLAUSES (t
) = tmp
;
17683 pop_omp_privatization_clauses (r
);
17687 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
17688 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
17690 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
17691 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
17693 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
17694 args
, complain
, in_decl
);
17695 if (tmp
== NULL_TREE
)
17696 tmp
= error_mark_node
;
17700 kind
= (enum omp_clause_depend_kind
)
17701 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
17704 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
17707 finish_omp_depobj (EXPR_LOCATION (t
), r
,
17708 OMP_CLAUSE_DEPEND_SOURCE
,
17709 OMP_DEPOBJ_CLAUSES (t
));
17713 case OMP_TARGET_DATA
:
17715 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
17716 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
17718 keep_next_level (true);
17719 stmt
= begin_omp_structured_block ();
17721 RECUR (OMP_BODY (t
));
17722 stmt
= finish_omp_structured_block (stmt
);
17725 OMP_BODY (t
) = stmt
;
17726 OMP_CLAUSES (t
) = tmp
;
17727 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
17729 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
17732 /* For combined target teams, ensure the num_teams and
17733 thread_limit clause expressions are evaluated on the host,
17734 before entering the target construct. */
17736 for (c
= OMP_TEAMS_CLAUSES (teams
);
17737 c
; c
= OMP_CLAUSE_CHAIN (c
))
17738 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
17739 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
17740 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
17742 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
17743 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
17744 if (expr
== error_mark_node
)
17746 tmp
= TARGET_EXPR_SLOT (expr
);
17748 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
17749 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
17750 OMP_CLAUSE_FIRSTPRIVATE
);
17751 OMP_CLAUSE_DECL (tc
) = tmp
;
17752 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
17753 OMP_TARGET_CLAUSES (t
) = tc
;
17762 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
17763 complain
, in_decl
);
17764 OACC_DECLARE_CLAUSES (t
) = tmp
;
17768 case OMP_TARGET_UPDATE
:
17769 case OMP_TARGET_ENTER_DATA
:
17770 case OMP_TARGET_EXIT_DATA
:
17771 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
17772 complain
, in_decl
);
17774 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17778 case OACC_ENTER_DATA
:
17779 case OACC_EXIT_DATA
:
17781 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
17782 complain
, in_decl
);
17784 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17789 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
17790 complain
, in_decl
);
17791 stmt
= push_stmt_list ();
17792 RECUR (OMP_BODY (t
));
17793 stmt
= pop_stmt_list (stmt
);
17796 OMP_BODY (t
) = stmt
;
17797 OMP_ORDERED_CLAUSES (t
) = tmp
;
17803 stmt
= push_stmt_list ();
17804 RECUR (OMP_BODY (t
));
17805 stmt
= pop_stmt_list (stmt
);
17808 OMP_BODY (t
) = stmt
;
17813 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
17815 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
17816 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
17817 complain
, in_decl
);
17818 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
17820 tree op1
= TREE_OPERAND (t
, 1);
17821 tree rhs1
= NULL_TREE
;
17823 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
17825 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
17826 op1
= TREE_OPERAND (op1
, 1);
17828 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17829 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17830 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
17831 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
17832 OMP_ATOMIC_MEMORY_ORDER (t
));
17836 tree op1
= TREE_OPERAND (t
, 1);
17837 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
17838 tree rhs1
= NULL_TREE
;
17839 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
17840 enum tree_code opcode
= NOP_EXPR
;
17841 if (code
== OMP_ATOMIC_READ
)
17843 v
= RECUR (TREE_OPERAND (op1
, 0));
17844 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17846 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17847 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17849 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17850 v
= RECUR (TREE_OPERAND (op1
, 0));
17851 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17852 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17854 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17855 op11
= TREE_OPERAND (op11
, 1);
17857 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17858 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17859 opcode
= TREE_CODE (op11
);
17860 if (opcode
== MODIFY_EXPR
)
17866 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17867 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17869 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
17870 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
17874 case TRANSACTION_EXPR
:
17877 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17878 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17880 if (TRANSACTION_EXPR_IS_STMT (t
))
17882 tree body
= TRANSACTION_EXPR_BODY (t
);
17883 tree noex
= NULL_TREE
;
17884 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17886 noex
= MUST_NOT_THROW_COND (body
);
17887 if (noex
== NULL_TREE
)
17888 noex
= boolean_true_node
;
17889 body
= TREE_OPERAND (body
, 0);
17891 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17893 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17897 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17898 RECUR (TRANSACTION_EXPR_BODY (t
)),
17905 case MUST_NOT_THROW_EXPR
:
17907 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17908 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17909 RETURN (build_must_not_throw_expr (op0
, cond
));
17912 case EXPR_PACK_EXPANSION
:
17913 error ("invalid use of pack expansion expression");
17914 RETURN (error_mark_node
);
17916 case NONTYPE_ARGUMENT_PACK
:
17917 error ("use %<...%> to expand argument pack");
17918 RETURN (error_mark_node
);
17920 case COMPOUND_EXPR
:
17921 tmp
= RECUR (TREE_OPERAND (t
, 0));
17922 if (tmp
== NULL_TREE
)
17923 /* If the first operand was a statement, we're done with it. */
17924 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17925 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17926 RECUR (TREE_OPERAND (t
, 1)),
17929 case ANNOTATE_EXPR
:
17930 tmp
= RECUR (TREE_OPERAND (t
, 0));
17931 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17932 TREE_TYPE (tmp
), tmp
,
17933 RECUR (TREE_OPERAND (t
, 1)),
17934 RECUR (TREE_OPERAND (t
, 2))));
17937 RETURN (add_stmt (copy_node (t
)));
17940 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17942 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17943 /*function_p=*/false,
17944 integral_constant_expression_p
));
17947 RETURN (NULL_TREE
);
17949 input_location
= loc
;
17955 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17956 function. For description of the body see comment above
17957 cp_parser_omp_declare_reduction_exprs. */
17960 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17962 if (t
== NULL_TREE
|| t
== error_mark_node
)
17965 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17967 tree_stmt_iterator tsi
;
17970 memset (stmts
, 0, sizeof stmts
);
17971 for (i
= 0, tsi
= tsi_start (t
);
17972 i
< 7 && !tsi_end_p (tsi
);
17973 i
++, tsi_next (&tsi
))
17974 stmts
[i
] = tsi_stmt (tsi
);
17975 gcc_assert (tsi_end_p (tsi
));
17979 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17980 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17981 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17982 args
, complain
, in_decl
);
17983 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17984 args
, complain
, in_decl
);
17985 DECL_CONTEXT (omp_out
) = current_function_decl
;
17986 DECL_CONTEXT (omp_in
) = current_function_decl
;
17987 keep_next_level (true);
17988 tree block
= begin_omp_structured_block ();
17989 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17990 block
= finish_omp_structured_block (block
);
17991 block
= maybe_cleanup_point_expr_void (block
);
17992 add_decl_expr (omp_out
);
17993 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17994 TREE_NO_WARNING (omp_out
) = 1;
17995 add_decl_expr (omp_in
);
17996 finish_expr_stmt (block
);
18000 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18001 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18002 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18003 args
, complain
, in_decl
);
18004 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18005 args
, complain
, in_decl
);
18006 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18007 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18008 keep_next_level (true);
18009 tree block
= begin_omp_structured_block ();
18010 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18011 block
= finish_omp_structured_block (block
);
18012 block
= maybe_cleanup_point_expr_void (block
);
18013 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18014 add_decl_expr (omp_priv
);
18015 add_decl_expr (omp_orig
);
18016 finish_expr_stmt (block
);
18018 add_decl_expr (omp_orig
);
18022 /* T is a postfix-expression that is not being used in a function
18023 call. Return the substituted version of T. */
18026 tsubst_non_call_postfix_expression (tree t
, tree args
,
18027 tsubst_flags_t complain
,
18030 if (TREE_CODE (t
) == SCOPE_REF
)
18031 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18032 /*done=*/false, /*address_p=*/false);
18034 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18035 /*function_p=*/false,
18036 /*integral_constant_expression_p=*/false);
18041 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18042 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18043 dependent init-capture. */
18046 prepend_one_capture (tree field
, tree init
, tree
&list
,
18047 tsubst_flags_t complain
)
18049 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18051 tree type
= NULL_TREE
;
18054 if (complain
& tf_error
)
18055 error ("empty initializer in lambda init-capture");
18056 init
= error_mark_node
;
18058 else if (TREE_CODE (init
) == TREE_LIST
)
18059 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18061 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18062 TREE_TYPE (field
) = type
;
18063 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18065 list
= tree_cons (field
, init
, list
);
18068 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18069 instantiation context. Instantiating a pack expansion containing a lambda
18070 might result in multiple lambdas all based on the same lambda in the
18074 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18076 tree oldfn
= lambda_function (t
);
18079 tree r
= build_lambda_expr ();
18081 LAMBDA_EXPR_LOCATION (r
)
18082 = LAMBDA_EXPR_LOCATION (t
);
18083 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18084 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18085 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18086 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18088 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18089 /* A lambda in a default argument outside a class gets no
18090 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18091 tsubst_default_argument calls start_lambda_scope, so we need to
18092 specifically ignore it here, and use the global scope. */
18093 record_null_lambda_scope (r
);
18095 record_lambda_scope (r
);
18097 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18098 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18100 vec
<tree
,va_gc
>* field_packs
= NULL
;
18102 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18103 cap
= TREE_CHAIN (cap
))
18105 tree ofield
= TREE_PURPOSE (cap
);
18106 if (PACK_EXPANSION_P (ofield
))
18107 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18108 tree field
= tsubst_decl (ofield
, args
, complain
);
18110 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18112 /* Remember these for when we've pushed local_specializations. */
18113 vec_safe_push (field_packs
, ofield
);
18114 vec_safe_push (field_packs
, field
);
18117 if (field
== error_mark_node
)
18118 return error_mark_node
;
18120 tree init
= TREE_VALUE (cap
);
18121 if (PACK_EXPANSION_P (init
))
18122 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18124 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18125 /*fn*/false, /*constexpr*/false);
18127 if (TREE_CODE (field
) == TREE_VEC
)
18129 int len
= TREE_VEC_LENGTH (field
);
18130 gcc_assert (TREE_CODE (init
) == TREE_VEC
18131 && TREE_VEC_LENGTH (init
) == len
);
18132 for (int i
= 0; i
< len
; ++i
)
18133 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18134 TREE_VEC_ELT (init
, i
),
18135 LAMBDA_EXPR_CAPTURE_LIST (r
),
18140 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18143 if (id_equal (DECL_NAME (field
), "__this"))
18144 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18148 tree type
= begin_lambda_type (r
);
18149 if (type
== error_mark_node
)
18150 return error_mark_node
;
18152 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18153 determine_visibility (TYPE_NAME (type
));
18155 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18157 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18158 ? DECL_TI_TEMPLATE (oldfn
)
18161 tree fntype
= static_fn_type (oldfn
);
18163 ++processing_template_decl
;
18164 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18166 --processing_template_decl
;
18168 if (fntype
== error_mark_node
)
18169 r
= error_mark_node
;
18172 /* The body of a lambda-expression is not a subexpression of the
18173 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18174 which would be skipped if cp_unevaluated_operand. */
18177 /* Fix the type of 'this'. */
18178 fntype
= build_memfn_type (fntype
, type
,
18179 type_memfn_quals (fntype
),
18180 type_memfn_rqual (fntype
));
18184 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18185 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18186 finish_member_declaration (tmpl
);
18191 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18192 finish_member_declaration (fn
);
18195 /* Let finish_function set this. */
18196 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18198 bool nested
= cfun
;
18200 push_function_context ();
18202 /* Still increment function_depth so that we don't GC in the
18203 middle of an expression. */
18206 local_specialization_stack
s (lss_copy
);
18208 tree body
= start_lambda_function (fn
, r
);
18210 /* Now record them for lookup_init_capture_pack. */
18211 int fplen
= vec_safe_length (field_packs
);
18212 for (int i
= 0; i
< fplen
; )
18214 tree pack
= (*field_packs
)[i
++];
18215 tree inst
= (*field_packs
)[i
++];
18216 register_local_specialization (inst
, pack
);
18218 release_tree_vector (field_packs
);
18220 register_parameter_specializations (oldfn
, fn
);
18224 /* We might not partially instantiate some parts of the function, so
18225 copy these flags from the original template. */
18226 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18227 current_function_returns_value
= ol
->returns_value
;
18228 current_function_returns_null
= ol
->returns_null
;
18229 current_function_returns_abnormally
= ol
->returns_abnormally
;
18230 current_function_infinite_loop
= ol
->infinite_loop
;
18233 /* [temp.deduct] A lambda-expression appearing in a function type or a
18234 template parameter is not considered part of the immediate context for
18235 the purposes of template argument deduction. */
18236 complain
= tf_warning_or_error
;
18238 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18239 /*constexpr*/false);
18241 finish_lambda_function (body
);
18244 pop_function_context ();
18248 /* The capture list was built up in reverse order; fix that now. */
18249 LAMBDA_EXPR_CAPTURE_LIST (r
)
18250 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18252 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18254 maybe_add_lambda_conv_op (type
);
18257 finish_struct (type
, /*attr*/NULL_TREE
);
18259 insert_pending_capture_proxies ();
18264 /* Like tsubst but deals with expressions and performs semantic
18265 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18268 tsubst_copy_and_build (tree t
,
18270 tsubst_flags_t complain
,
18273 bool integral_constant_expression_p
)
18275 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18276 #define RECUR(NODE) \
18277 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18278 /*function_p=*/false, \
18279 integral_constant_expression_p)
18284 if (t
== NULL_TREE
|| t
== error_mark_node
)
18287 loc
= input_location
;
18288 if (location_t eloc
= cp_expr_location (t
))
18289 input_location
= eloc
;
18291 /* N3276 decltype magic only applies to calls at the top level or on the
18292 right side of a comma. */
18293 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18294 complain
&= ~tf_decltype
;
18296 switch (TREE_CODE (t
))
18300 /* Fall through. */
18301 case IDENTIFIER_NODE
:
18305 bool non_integral_constant_expression_p
;
18306 const char *error_msg
;
18308 if (IDENTIFIER_CONV_OP_P (t
))
18310 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18311 t
= make_conv_op_name (new_type
);
18314 /* Look up the name. */
18315 decl
= lookup_name (t
);
18317 /* By convention, expressions use ERROR_MARK_NODE to indicate
18318 failure, not NULL_TREE. */
18319 if (decl
== NULL_TREE
)
18320 decl
= error_mark_node
;
18322 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18324 integral_constant_expression_p
,
18325 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18326 &non_integral_constant_expression_p
,
18327 /*template_p=*/false,
18329 /*address_p=*/false,
18330 /*template_arg_p=*/false,
18335 if (!function_p
&& identifier_p (decl
))
18337 if (complain
& tf_error
)
18338 unqualified_name_lookup_error (decl
);
18339 decl
= error_mark_node
;
18344 case TEMPLATE_ID_EXPR
:
18347 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18348 tree targs
= TREE_OPERAND (t
, 1);
18351 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18352 if (targs
== error_mark_node
)
18353 RETURN (error_mark_node
);
18355 if (TREE_CODE (templ
) == SCOPE_REF
)
18357 tree name
= TREE_OPERAND (templ
, 1);
18358 tree tid
= lookup_template_function (name
, targs
);
18359 TREE_OPERAND (templ
, 1) = tid
;
18363 if (variable_template_p (templ
))
18364 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
18366 if (TREE_CODE (templ
) == COMPONENT_REF
)
18368 object
= TREE_OPERAND (templ
, 0);
18369 templ
= TREE_OPERAND (templ
, 1);
18372 object
= NULL_TREE
;
18373 templ
= lookup_template_function (templ
, targs
);
18376 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18377 object
, templ
, NULL_TREE
));
18379 RETURN (baselink_for_fns (templ
));
18384 tree r
= RECUR (TREE_OPERAND (t
, 0));
18386 if (REFERENCE_REF_P (t
))
18388 /* A type conversion to reference type will be enclosed in
18389 such an indirect ref, but the substitution of the cast
18390 will have also added such an indirect ref. */
18391 r
= convert_from_reference (r
);
18394 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18395 complain
|decltype_flag
);
18397 if (REF_PARENTHESIZED_P (t
))
18398 r
= force_paren_expr (r
);
18405 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18406 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18407 RETURN (build_nop (type
, op0
));
18410 case IMPLICIT_CONV_EXPR
:
18412 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18413 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18414 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18416 retval
= copy_node (t
);
18417 TREE_TYPE (retval
) = type
;
18418 TREE_OPERAND (retval
, 0) = expr
;
18421 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18422 /* We'll pass this to convert_nontype_argument again, we don't need
18423 to actually perform any conversion here. */
18425 int flags
= LOOKUP_IMPLICIT
;
18426 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18427 flags
= LOOKUP_NORMAL
;
18428 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
18429 flags
|= LOOKUP_NO_NARROWING
;
18430 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18436 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18437 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18438 if (op0
== error_mark_node
)
18439 RETURN (error_mark_node
);
18440 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
18444 case REINTERPRET_CAST_EXPR
:
18445 case CONST_CAST_EXPR
:
18446 case DYNAMIC_CAST_EXPR
:
18447 case STATIC_CAST_EXPR
:
18450 tree op
, r
= NULL_TREE
;
18452 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18453 if (integral_constant_expression_p
18454 && !cast_valid_in_integral_constant_expression_p (type
))
18456 if (complain
& tf_error
)
18457 error ("a cast to a type other than an integral or "
18458 "enumeration type cannot appear in a constant-expression");
18459 RETURN (error_mark_node
);
18462 op
= RECUR (TREE_OPERAND (t
, 0));
18464 warning_sentinel
s(warn_useless_cast
);
18465 warning_sentinel
s2(warn_ignored_qualifiers
);
18466 switch (TREE_CODE (t
))
18469 r
= build_functional_cast (type
, op
, complain
);
18471 case REINTERPRET_CAST_EXPR
:
18472 r
= build_reinterpret_cast (type
, op
, complain
);
18474 case CONST_CAST_EXPR
:
18475 r
= build_const_cast (type
, op
, complain
);
18477 case DYNAMIC_CAST_EXPR
:
18478 r
= build_dynamic_cast (type
, op
, complain
);
18480 case STATIC_CAST_EXPR
:
18481 r
= build_static_cast (type
, op
, complain
);
18484 gcc_unreachable ();
18490 case POSTDECREMENT_EXPR
:
18491 case POSTINCREMENT_EXPR
:
18492 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18493 args
, complain
, in_decl
);
18494 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
18495 complain
|decltype_flag
));
18497 case PREDECREMENT_EXPR
:
18498 case PREINCREMENT_EXPR
:
18502 case TRUTH_NOT_EXPR
:
18503 case UNARY_PLUS_EXPR
: /* Unary + */
18504 case REALPART_EXPR
:
18505 case IMAGPART_EXPR
:
18506 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
18507 RECUR (TREE_OPERAND (t
, 0)),
18508 complain
|decltype_flag
));
18510 case FIX_TRUNC_EXPR
:
18511 gcc_unreachable ();
18514 op1
= TREE_OPERAND (t
, 0);
18515 if (TREE_CODE (op1
) == LABEL_DECL
)
18516 RETURN (finish_label_address_expr (DECL_NAME (op1
),
18517 EXPR_LOCATION (op1
)));
18518 if (TREE_CODE (op1
) == SCOPE_REF
)
18519 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
18520 /*done=*/true, /*address_p=*/true);
18522 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
18524 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
18525 complain
|decltype_flag
));
18530 case TRUNC_DIV_EXPR
:
18531 case CEIL_DIV_EXPR
:
18532 case FLOOR_DIV_EXPR
:
18533 case ROUND_DIV_EXPR
:
18534 case EXACT_DIV_EXPR
:
18538 case TRUNC_MOD_EXPR
:
18539 case FLOOR_MOD_EXPR
:
18540 case TRUTH_ANDIF_EXPR
:
18541 case TRUTH_ORIF_EXPR
:
18542 case TRUTH_AND_EXPR
:
18543 case TRUTH_OR_EXPR
:
18559 warning_sentinel
s1(warn_type_limits
);
18560 warning_sentinel
s2(warn_div_by_zero
);
18561 warning_sentinel
s3(warn_logical_op
);
18562 warning_sentinel
s4(warn_tautological_compare
);
18563 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18564 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18565 tree r
= build_x_binary_op
18566 (input_location
, TREE_CODE (t
),
18568 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
18570 : TREE_CODE (TREE_OPERAND (t
, 0))),
18572 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
18574 : TREE_CODE (TREE_OPERAND (t
, 1))),
18576 complain
|decltype_flag
);
18577 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
18578 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18583 case POINTER_PLUS_EXPR
:
18585 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18586 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18587 RETURN (fold_build_pointer_plus (op0
, op1
));
18591 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
18592 /*address_p=*/false));
18594 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18595 args
, complain
, in_decl
);
18596 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
18597 RECUR (TREE_OPERAND (t
, 1)),
18598 complain
|decltype_flag
));
18601 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
18602 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
18603 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18610 op1
= TREE_OPERAND (t
, 0);
18611 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
18612 op1
= TREE_TYPE (op1
);
18613 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
18614 && ALIGNOF_EXPR_STD_P (t
));
18617 /* When there are no ARGS, we are trying to evaluate a
18618 non-dependent expression from the parser. Trying to do
18619 the substitutions may not work. */
18621 op1
= TREE_TYPE (op1
);
18625 ++cp_unevaluated_operand
;
18626 ++c_inhibit_evaluation_warnings
;
18628 op1
= tsubst (op1
, args
, complain
, in_decl
);
18630 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18631 /*function_p=*/false,
18632 /*integral_constant_expression_p=*/
18634 --cp_unevaluated_operand
;
18635 --c_inhibit_evaluation_warnings
;
18638 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
18639 complain
& tf_error
);
18641 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
18642 complain
& tf_error
);
18643 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
18645 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
18647 if (!processing_template_decl
&& TYPE_P (op1
))
18649 r
= build_min (SIZEOF_EXPR
, size_type_node
,
18650 build1 (NOP_EXPR
, op1
, error_mark_node
));
18651 SIZEOF_EXPR_TYPE_P (r
) = 1;
18654 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
18655 TREE_SIDE_EFFECTS (r
) = 0;
18656 TREE_READONLY (r
) = 1;
18658 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
18663 case AT_ENCODE_EXPR
:
18665 op1
= TREE_OPERAND (t
, 0);
18666 ++cp_unevaluated_operand
;
18667 ++c_inhibit_evaluation_warnings
;
18668 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18669 /*function_p=*/false,
18670 /*integral_constant_expression_p=*/false);
18671 --cp_unevaluated_operand
;
18672 --c_inhibit_evaluation_warnings
;
18673 RETURN (objc_build_encode_expr (op1
));
18676 case NOEXCEPT_EXPR
:
18677 op1
= TREE_OPERAND (t
, 0);
18678 ++cp_unevaluated_operand
;
18679 ++c_inhibit_evaluation_warnings
;
18680 ++cp_noexcept_operand
;
18681 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18682 /*function_p=*/false,
18683 /*integral_constant_expression_p=*/false);
18684 --cp_unevaluated_operand
;
18685 --c_inhibit_evaluation_warnings
;
18686 --cp_noexcept_operand
;
18687 RETURN (finish_noexcept_expr (op1
, complain
));
18691 warning_sentinel
s(warn_div_by_zero
);
18692 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
18693 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
18694 tree r
= build_x_modify_expr
18695 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
18696 complain
|decltype_flag
);
18697 /* TREE_NO_WARNING must be set if either the expression was
18698 parenthesized or it uses an operator such as >>= rather
18699 than plain assignment. In the former case, it was already
18700 set and must be copied. In the latter case,
18701 build_x_modify_expr sets it and it must not be reset
18703 if (TREE_NO_WARNING (t
))
18704 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18710 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18711 args
, complain
, in_decl
);
18712 /* Remember that there was a reference to this entity. */
18714 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
18715 RETURN (error_mark_node
);
18716 RETURN (build_x_arrow (input_location
, op1
, complain
));
18720 tree placement
= RECUR (TREE_OPERAND (t
, 0));
18721 tree init
= RECUR (TREE_OPERAND (t
, 3));
18722 vec
<tree
, va_gc
> *placement_vec
;
18723 vec
<tree
, va_gc
> *init_vec
;
18726 if (placement
== NULL_TREE
)
18727 placement_vec
= NULL
;
18730 placement_vec
= make_tree_vector ();
18731 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
18732 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
18735 /* If there was an initializer in the original tree, but it
18736 instantiated to an empty list, then we should pass a
18737 non-NULL empty vector to tell build_new that it was an
18738 empty initializer() rather than no initializer. This can
18739 only happen when the initializer is a pack expansion whose
18740 parameter packs are of length zero. */
18741 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
18745 init_vec
= make_tree_vector ();
18746 if (init
== void_node
)
18747 gcc_assert (init_vec
!= NULL
);
18750 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
18751 vec_safe_push (init_vec
, TREE_VALUE (init
));
18755 /* Avoid passing an enclosing decl to valid_array_size_p. */
18756 in_decl
= NULL_TREE
;
18758 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
18759 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18760 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
18761 NEW_EXPR_USE_GLOBAL (t
),
18764 if (placement_vec
!= NULL
)
18765 release_tree_vector (placement_vec
);
18766 if (init_vec
!= NULL
)
18767 release_tree_vector (init_vec
);
18774 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18775 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18776 RETURN (delete_sanity (op0
, op1
,
18777 DELETE_EXPR_USE_VEC (t
),
18778 DELETE_EXPR_USE_GLOBAL (t
),
18782 case COMPOUND_EXPR
:
18784 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
18785 complain
& ~tf_decltype
, in_decl
,
18786 /*function_p=*/false,
18787 integral_constant_expression_p
);
18788 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
18790 RECUR (TREE_OPERAND (t
, 1)),
18791 complain
|decltype_flag
));
18797 unsigned int nargs
, i
;
18802 function
= CALL_EXPR_FN (t
);
18803 /* Internal function with no arguments. */
18804 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
18807 /* When we parsed the expression, we determined whether or
18808 not Koenig lookup should be performed. */
18809 koenig_p
= KOENIG_LOOKUP_P (t
);
18810 if (function
== NULL_TREE
)
18813 qualified_p
= false;
18815 else if (TREE_CODE (function
) == SCOPE_REF
)
18817 qualified_p
= true;
18818 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
18820 /*address_p=*/false);
18822 else if (koenig_p
&& identifier_p (function
))
18824 /* Do nothing; calling tsubst_copy_and_build on an identifier
18825 would incorrectly perform unqualified lookup again.
18827 Note that we can also have an IDENTIFIER_NODE if the earlier
18828 unqualified lookup found a member function; in that case
18829 koenig_p will be false and we do want to do the lookup
18830 again to find the instantiated member function.
18832 FIXME but doing that causes c++/15272, so we need to stop
18833 using IDENTIFIER_NODE in that situation. */
18834 qualified_p
= false;
18838 if (TREE_CODE (function
) == COMPONENT_REF
)
18840 tree op
= TREE_OPERAND (function
, 1);
18842 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
18843 || (BASELINK_P (op
)
18844 && BASELINK_QUALIFIED_P (op
)));
18847 qualified_p
= false;
18849 if (TREE_CODE (function
) == ADDR_EXPR
18850 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
18851 /* Avoid error about taking the address of a constructor. */
18852 function
= TREE_OPERAND (function
, 0);
18854 function
= tsubst_copy_and_build (function
, args
, complain
,
18857 integral_constant_expression_p
);
18859 if (BASELINK_P (function
))
18860 qualified_p
= true;
18863 nargs
= call_expr_nargs (t
);
18864 releasing_vec call_args
;
18865 for (i
= 0; i
< nargs
; ++i
)
18867 tree arg
= CALL_EXPR_ARG (t
, i
);
18869 if (!PACK_EXPANSION_P (arg
))
18870 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
18873 /* Expand the pack expansion and push each entry onto
18875 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
18876 if (TREE_CODE (arg
) == TREE_VEC
)
18878 unsigned int len
, j
;
18880 len
= TREE_VEC_LENGTH (arg
);
18881 for (j
= 0; j
< len
; ++j
)
18883 tree value
= TREE_VEC_ELT (arg
, j
);
18884 if (value
!= NULL_TREE
)
18885 value
= convert_from_reference (value
);
18886 vec_safe_push (call_args
, value
);
18891 /* A partial substitution. Add one entry. */
18892 vec_safe_push (call_args
, arg
);
18897 /* Stripped-down processing for a call in a thunk. Specifically, in
18898 the thunk template for a generic lambda. */
18899 if (CALL_FROM_THUNK_P (t
))
18901 /* Now that we've expanded any packs, the number of call args
18902 might be different. */
18903 unsigned int cargs
= call_args
->length ();
18904 tree thisarg
= NULL_TREE
;
18905 if (TREE_CODE (function
) == COMPONENT_REF
)
18907 thisarg
= TREE_OPERAND (function
, 0);
18908 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
18909 thisarg
= TREE_OPERAND (thisarg
, 0);
18910 function
= TREE_OPERAND (function
, 1);
18911 if (TREE_CODE (function
) == BASELINK
)
18912 function
= BASELINK_FUNCTIONS (function
);
18914 /* We aren't going to do normal overload resolution, so force the
18915 template-id to resolve. */
18916 function
= resolve_nondeduced_context (function
, complain
);
18917 for (unsigned i
= 0; i
< cargs
; ++i
)
18919 /* In a thunk, pass through args directly, without any
18921 tree arg
= (*call_args
)[i
];
18922 while (TREE_CODE (arg
) != PARM_DECL
)
18923 arg
= TREE_OPERAND (arg
, 0);
18924 (*call_args
)[i
] = arg
;
18928 /* If there are no other args, just push 'this'. */
18930 vec_safe_push (call_args
, thisarg
);
18933 /* Otherwise, shift the other args over to make room. */
18934 tree last
= (*call_args
)[cargs
- 1];
18935 vec_safe_push (call_args
, last
);
18936 for (int i
= cargs
- 1; i
> 0; --i
)
18937 (*call_args
)[i
] = (*call_args
)[i
- 1];
18938 (*call_args
)[0] = thisarg
;
18941 ret
= build_call_a (function
, call_args
->length (),
18942 call_args
->address ());
18943 /* The thunk location is not interesting. */
18944 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
18945 CALL_FROM_THUNK_P (ret
) = true;
18946 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
18947 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
18952 /* We do not perform argument-dependent lookup if normal
18953 lookup finds a non-function, in accordance with the
18954 expected resolution of DR 218. */
18956 && ((is_overloaded_fn (function
)
18957 /* If lookup found a member function, the Koenig lookup is
18958 not appropriate, even if an unqualified-name was used
18959 to denote the function. */
18960 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
18961 || identifier_p (function
))
18962 /* Only do this when substitution turns a dependent call
18963 into a non-dependent call. */
18964 && type_dependent_expression_p_push (t
)
18965 && !any_type_dependent_arguments_p (call_args
))
18966 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
18968 if (function
!= NULL_TREE
18969 && identifier_p (function
)
18970 && !any_type_dependent_arguments_p (call_args
))
18972 if (koenig_p
&& (complain
& tf_warning_or_error
))
18974 /* For backwards compatibility and good diagnostics, try
18975 the unqualified lookup again if we aren't in SFINAE
18977 tree unq
= (tsubst_copy_and_build
18978 (function
, args
, complain
, in_decl
, true,
18979 integral_constant_expression_p
));
18980 if (unq
== error_mark_node
)
18981 RETURN (error_mark_node
);
18983 if (unq
!= function
)
18985 /* In a lambda fn, we have to be careful to not
18986 introduce new this captures. Legacy code can't
18987 be using lambdas anyway, so it's ok to be
18989 bool in_lambda
= (current_class_type
18990 && LAMBDA_TYPE_P (current_class_type
));
18991 char const *const msg
18992 = G_("%qD was not declared in this scope, "
18993 "and no declarations were found by "
18994 "argument-dependent lookup at the point "
18995 "of instantiation");
18999 error_at (cp_expr_loc_or_input_loc (t
),
19002 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19008 if (INDIRECT_REF_P (fn
))
19009 fn
= TREE_OPERAND (fn
, 0);
19010 if (is_overloaded_fn (fn
))
19011 fn
= get_first_fn (fn
);
19014 /* Can't say anything more. */;
19015 else if (DECL_CLASS_SCOPE_P (fn
))
19017 location_t loc
= cp_expr_loc_or_input_loc (t
);
19019 "declarations in dependent base %qT are "
19020 "not found by unqualified lookup",
19021 DECL_CLASS_CONTEXT (fn
));
19022 if (current_class_ptr
)
19024 "use %<this->%D%> instead", function
);
19027 "use %<%T::%D%> instead",
19028 current_class_name
, function
);
19031 inform (DECL_SOURCE_LOCATION (fn
),
19032 "%qD declared here, later in the "
19033 "translation unit", fn
);
19035 RETURN (error_mark_node
);
19041 if (identifier_p (function
))
19043 if (complain
& tf_error
)
19044 unqualified_name_lookup_error (function
);
19045 RETURN (error_mark_node
);
19049 /* Remember that there was a reference to this entity. */
19050 if (function
!= NULL_TREE
19051 && DECL_P (function
)
19052 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19053 RETURN (error_mark_node
);
19055 /* Put back tf_decltype for the actual call. */
19056 complain
|= decltype_flag
;
19058 if (function
== NULL_TREE
)
19059 switch (CALL_EXPR_IFN (t
))
19062 gcc_assert (nargs
== 1);
19063 if (vec_safe_length (call_args
) != 1)
19065 error_at (cp_expr_loc_or_input_loc (t
),
19066 "wrong number of arguments to "
19067 "%<__builtin_launder%>");
19068 ret
= error_mark_node
;
19071 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19072 (*call_args
)[0], complain
);
19075 case IFN_VEC_CONVERT
:
19076 gcc_assert (nargs
== 1);
19077 if (vec_safe_length (call_args
) != 1)
19079 error_at (cp_expr_loc_or_input_loc (t
),
19080 "wrong number of arguments to "
19081 "%<__builtin_convertvector%>");
19082 ret
= error_mark_node
;
19085 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19086 tsubst (TREE_TYPE (t
), args
,
19087 complain
, in_decl
),
19089 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19094 /* Unsupported internal function with arguments. */
19095 gcc_unreachable ();
19097 else if (TREE_CODE (function
) == OFFSET_REF
19098 || TREE_CODE (function
) == DOTSTAR_EXPR
19099 || TREE_CODE (function
) == MEMBER_REF
)
19100 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19102 else if (TREE_CODE (function
) == COMPONENT_REF
)
19104 tree instance
= TREE_OPERAND (function
, 0);
19105 tree fn
= TREE_OPERAND (function
, 1);
19107 if (processing_template_decl
19108 && (type_dependent_expression_p (instance
)
19109 || (!BASELINK_P (fn
)
19110 && TREE_CODE (fn
) != FIELD_DECL
)
19111 || type_dependent_expression_p (fn
)
19112 || any_type_dependent_arguments_p (call_args
)))
19113 ret
= build_min_nt_call_vec (function
, call_args
);
19114 else if (!BASELINK_P (fn
))
19115 ret
= finish_call_expr (function
, &call_args
,
19116 /*disallow_virtual=*/false,
19117 /*koenig_p=*/false,
19120 ret
= (build_new_method_call
19122 &call_args
, NULL_TREE
,
19123 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19128 ret
= finish_call_expr (function
, &call_args
,
19129 /*disallow_virtual=*/qualified_p
,
19133 if (ret
!= error_mark_node
)
19135 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
19136 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
19137 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
19138 if (op
|| ord
|| rev
)
19140 function
= extract_call_expr (ret
);
19141 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
19142 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
19143 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
19152 tree cond
= RECUR (TREE_OPERAND (t
, 0));
19153 cond
= mark_rvalue_use (cond
);
19154 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19157 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19159 if (integer_zerop (folded_cond
))
19161 ++c_inhibit_evaluation_warnings
;
19162 exp1
= RECUR (TREE_OPERAND (t
, 1));
19163 --c_inhibit_evaluation_warnings
;
19164 exp2
= RECUR (TREE_OPERAND (t
, 2));
19168 exp1
= RECUR (TREE_OPERAND (t
, 1));
19169 ++c_inhibit_evaluation_warnings
;
19170 exp2
= RECUR (TREE_OPERAND (t
, 2));
19171 --c_inhibit_evaluation_warnings
;
19173 cond
= folded_cond
;
19177 exp1
= RECUR (TREE_OPERAND (t
, 1));
19178 exp2
= RECUR (TREE_OPERAND (t
, 2));
19181 warning_sentinel
s(warn_duplicated_branches
);
19182 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19183 cond
, exp1
, exp2
, complain
));
19186 case PSEUDO_DTOR_EXPR
:
19188 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19189 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19190 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19191 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19197 tree purpose
, value
, chain
;
19199 if (t
== void_list_node
)
19202 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19203 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19205 /* We have pack expansions, so expand those and
19206 create a new list out of it. */
19207 tree purposevec
= NULL_TREE
;
19208 tree valuevec
= NULL_TREE
;
19212 /* Expand the argument expressions. */
19213 if (TREE_PURPOSE (t
))
19214 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19215 complain
, in_decl
);
19216 if (TREE_VALUE (t
))
19217 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19218 complain
, in_decl
);
19220 /* Build the rest of the list. */
19221 chain
= TREE_CHAIN (t
);
19222 if (chain
&& chain
!= void_type_node
)
19223 chain
= RECUR (chain
);
19225 /* Determine the number of arguments. */
19226 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19228 len
= TREE_VEC_LENGTH (purposevec
);
19229 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19231 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19232 len
= TREE_VEC_LENGTH (valuevec
);
19235 /* Since we only performed a partial substitution into
19236 the argument pack, we only RETURN (a single list
19238 if (purposevec
== TREE_PURPOSE (t
)
19239 && valuevec
== TREE_VALUE (t
)
19240 && chain
== TREE_CHAIN (t
))
19243 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19246 /* Convert the argument vectors into a TREE_LIST */
19250 /* Grab the Ith values. */
19252 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19255 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19258 /* Build the list (backwards). */
19259 chain
= tree_cons (purpose
, value
, chain
);
19265 purpose
= TREE_PURPOSE (t
);
19267 purpose
= RECUR (purpose
);
19268 value
= TREE_VALUE (t
);
19270 value
= RECUR (value
);
19271 chain
= TREE_CHAIN (t
);
19272 if (chain
&& chain
!= void_type_node
)
19273 chain
= RECUR (chain
);
19274 if (purpose
== TREE_PURPOSE (t
)
19275 && value
== TREE_VALUE (t
)
19276 && chain
== TREE_CHAIN (t
))
19278 RETURN (tree_cons (purpose
, value
, chain
));
19281 case COMPONENT_REF
:
19288 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19289 args
, complain
, in_decl
);
19290 /* Remember that there was a reference to this entity. */
19291 if (DECL_P (object
)
19292 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19293 RETURN (error_mark_node
);
19294 object_type
= TREE_TYPE (object
);
19296 member
= TREE_OPERAND (t
, 1);
19297 if (BASELINK_P (member
))
19298 member
= tsubst_baselink (member
,
19299 non_reference (TREE_TYPE (object
)),
19300 args
, complain
, in_decl
);
19302 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19303 if (member
== error_mark_node
)
19304 RETURN (error_mark_node
);
19306 if (TREE_CODE (member
) == FIELD_DECL
)
19308 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19309 if (TREE_CODE (r
) == COMPONENT_REF
)
19310 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19313 else if (type_dependent_expression_p (object
))
19314 /* We can't do much here. */;
19315 else if (!CLASS_TYPE_P (object_type
))
19317 if (scalarish_type_p (object_type
))
19319 tree s
= NULL_TREE
;
19320 tree dtor
= member
;
19322 if (TREE_CODE (dtor
) == SCOPE_REF
)
19324 s
= TREE_OPERAND (dtor
, 0);
19325 dtor
= TREE_OPERAND (dtor
, 1);
19327 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19329 dtor
= TREE_OPERAND (dtor
, 0);
19331 RETURN (finish_pseudo_destructor_expr
19332 (object
, s
, dtor
, input_location
));
19336 else if (TREE_CODE (member
) == SCOPE_REF
19337 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19339 /* Lookup the template functions now that we know what the
19341 tree scope
= TREE_OPERAND (member
, 0);
19342 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19343 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19344 member
= lookup_qualified_name (scope
, tmpl
,
19345 /*is_type_p=*/false,
19346 /*complain=*/false);
19347 if (BASELINK_P (member
))
19349 BASELINK_FUNCTIONS (member
)
19350 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19352 member
= (adjust_result_of_qualified_name_lookup
19353 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19358 qualified_name_lookup_error (scope
, tmpl
, member
,
19360 RETURN (error_mark_node
);
19363 else if (TREE_CODE (member
) == SCOPE_REF
19364 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19365 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19367 if (complain
& tf_error
)
19369 if (TYPE_P (TREE_OPERAND (member
, 0)))
19370 error ("%qT is not a class or namespace",
19371 TREE_OPERAND (member
, 0));
19373 error ("%qD is not a class or namespace",
19374 TREE_OPERAND (member
, 0));
19376 RETURN (error_mark_node
);
19379 r
= finish_class_member_access_expr (object
, member
,
19380 /*template_p=*/false,
19382 if (TREE_CODE (r
) == COMPONENT_REF
)
19383 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19388 RETURN (build_throw
19389 (RECUR (TREE_OPERAND (t
, 0))));
19393 vec
<constructor_elt
, va_gc
> *n
;
19394 constructor_elt
*ce
;
19395 unsigned HOST_WIDE_INT idx
;
19396 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19397 bool process_index_p
;
19399 bool need_copy_p
= false;
19402 if (type
== error_mark_node
)
19403 RETURN (error_mark_node
);
19405 /* We do not want to process the index of aggregate
19406 initializers as they are identifier nodes which will be
19407 looked up by digest_init. */
19408 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19410 if (null_member_pointer_value_p (t
))
19412 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
19416 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19417 newlen
= vec_safe_length (n
);
19418 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19420 if (ce
->index
&& process_index_p
19421 /* An identifier index is looked up in the type
19422 being initialized, not the current scope. */
19423 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
19424 ce
->index
= RECUR (ce
->index
);
19426 if (PACK_EXPANSION_P (ce
->value
))
19428 /* Substitute into the pack expansion. */
19429 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
19432 if (ce
->value
== error_mark_node
19433 || PACK_EXPANSION_P (ce
->value
))
19435 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
19436 /* Just move the argument into place. */
19437 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
19440 /* Update the length of the final CONSTRUCTOR
19441 arguments vector, and note that we will need to
19443 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
19444 need_copy_p
= true;
19448 ce
->value
= RECUR (ce
->value
);
19453 vec
<constructor_elt
, va_gc
> *old_n
= n
;
19455 vec_alloc (n
, newlen
);
19456 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
19458 if (TREE_CODE (ce
->value
) == TREE_VEC
)
19460 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
19461 for (i
= 0; i
< len
; ++i
)
19462 CONSTRUCTOR_APPEND_ELT (n
, 0,
19463 TREE_VEC_ELT (ce
->value
, i
));
19466 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
19470 r
= build_constructor (init_list_type_node
, n
);
19471 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
19472 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
19473 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
19475 if (TREE_HAS_CONSTRUCTOR (t
))
19477 fcl_t cl
= fcl_functional
;
19478 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
19480 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
19483 TREE_TYPE (r
) = type
;
19489 tree operand_0
= TREE_OPERAND (t
, 0);
19490 if (TYPE_P (operand_0
))
19492 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
19493 RETURN (get_typeid (operand_0
, complain
));
19497 operand_0
= RECUR (operand_0
);
19498 RETURN (build_typeid (operand_0
, complain
));
19509 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
19510 /* ??? We're doing a subset of finish_id_expression here. */
19511 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
19512 /* Replace an evaluated use of the thread_local variable with
19513 a call to its wrapper. */
19515 else if (outer_automatic_var_p (r
))
19516 r
= process_outer_var_ref (r
, complain
);
19518 if (!TYPE_REF_P (TREE_TYPE (t
)))
19519 /* If the original type was a reference, we'll be wrapped in
19520 the appropriate INDIRECT_REF. */
19521 r
= convert_from_reference (r
);
19527 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19528 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19529 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
19532 case OFFSETOF_EXPR
:
19535 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
19536 in_decl
, /*function_p=*/false,
19537 /*integral_constant_expression_p=*/false);
19538 RETURN (finish_offsetof (object_ptr
,
19539 RECUR (TREE_OPERAND (t
, 0)),
19540 EXPR_LOCATION (t
)));
19543 case ADDRESSOF_EXPR
:
19544 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
19545 RECUR (TREE_OPERAND (t
, 0)), complain
));
19549 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
19550 complain
, in_decl
);
19552 tree type2
= TRAIT_EXPR_TYPE2 (t
);
19553 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
19554 type2
= RECUR (type2
);
19556 type2
= tsubst (type2
, args
, complain
, in_decl
);
19558 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
19559 TRAIT_EXPR_KIND (t
), type1
, type2
));
19564 tree old_stmt_expr
= cur_stmt_expr
;
19565 tree stmt_expr
= begin_stmt_expr ();
19567 cur_stmt_expr
= stmt_expr
;
19568 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
19569 integral_constant_expression_p
);
19570 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
19571 cur_stmt_expr
= old_stmt_expr
;
19573 /* If the resulting list of expression statement is empty,
19574 fold it further into void_node. */
19575 if (empty_expr_stmt_p (stmt_expr
))
19576 stmt_expr
= void_node
;
19578 RETURN (stmt_expr
);
19583 if (complain
& tf_partial
)
19585 /* We don't have a full set of template arguments yet; don't touch
19586 the lambda at all. */
19587 gcc_assert (processing_template_decl
);
19590 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
19592 RETURN (build_lambda_object (r
));
19596 /* We can get here for a constant initializer of non-dependent type.
19597 FIXME stop folding in cp_parser_initializer_clause. */
19599 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
19604 case TRANSACTION_EXPR
:
19605 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
19606 integral_constant_expression_p
));
19609 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
19611 case VEC_PERM_EXPR
:
19613 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19614 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19615 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19616 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
19620 case REQUIRES_EXPR
:
19621 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
19624 /* No need to substitute further, a RANGE_EXPR will always be built
19625 with constant operands. */
19628 case NON_LVALUE_EXPR
:
19629 case VIEW_CONVERT_EXPR
:
19630 if (location_wrapper_p (t
))
19631 /* We need to do this here as well as in tsubst_copy so we get the
19632 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19633 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
19634 EXPR_LOCATION (t
)));
19638 /* Handle Objective-C++ constructs, if appropriate. */
19641 = objcp_tsubst_copy_and_build (t
, args
, complain
,
19642 in_decl
, /*function_p=*/false);
19646 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19652 input_location
= loc
;
19656 /* Verify that the instantiated ARGS are valid. For type arguments,
19657 make sure that the type's linkage is ok. For non-type arguments,
19658 make sure they are constants if they are integral or enumerations.
19659 Emit an error under control of COMPLAIN, and return TRUE on error. */
19662 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
19664 if (dependent_template_arg_p (t
))
19666 if (ARGUMENT_PACK_P (t
))
19668 tree vec
= ARGUMENT_PACK_ARGS (t
);
19669 int len
= TREE_VEC_LENGTH (vec
);
19670 bool result
= false;
19673 for (i
= 0; i
< len
; ++i
)
19674 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
19678 else if (TYPE_P (t
))
19680 /* [basic.link]: A name with no linkage (notably, the name
19681 of a class or enumeration declared in a local scope)
19682 shall not be used to declare an entity with linkage.
19683 This implies that names with no linkage cannot be used as
19686 DR 757 relaxes this restriction for C++0x. */
19687 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
19688 : no_linkage_check (t
, /*relaxed_p=*/false));
19692 /* DR 488 makes use of a type with no linkage cause
19693 type deduction to fail. */
19694 if (complain
& tf_error
)
19696 if (TYPE_UNNAMED_P (nt
))
19697 error ("%qT is/uses unnamed type", t
);
19699 error ("template argument for %qD uses local type %qT",
19704 /* In order to avoid all sorts of complications, we do not
19705 allow variably-modified types as template arguments. */
19706 else if (variably_modified_type_p (t
, NULL_TREE
))
19708 if (complain
& tf_error
)
19709 error ("%qT is a variably modified type", t
);
19713 /* Class template and alias template arguments should be OK. */
19714 else if (DECL_TYPE_TEMPLATE_P (t
))
19716 /* A non-type argument of integral or enumerated type must be a
19718 else if (TREE_TYPE (t
)
19719 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
19720 && !REFERENCE_REF_P (t
)
19721 && !TREE_CONSTANT (t
))
19723 if (complain
& tf_error
)
19724 error ("integral expression %qE is not constant", t
);
19731 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
19733 int ix
, len
= DECL_NTPARMS (tmpl
);
19734 bool result
= false;
19736 for (ix
= 0; ix
!= len
; ix
++)
19738 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
19741 if (result
&& (complain
& tf_error
))
19742 error (" trying to instantiate %qD", tmpl
);
19746 /* We're out of SFINAE context now, so generate diagnostics for the access
19747 errors we saw earlier when instantiating D from TMPL and ARGS. */
19750 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
19752 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19753 tree type
= TREE_TYPE (pattern
);
19754 location_t loc
= input_location
;
19756 push_access_scope (d
);
19757 push_deferring_access_checks (dk_no_deferred
);
19758 input_location
= DECL_SOURCE_LOCATION (pattern
);
19759 tsubst (type
, args
, tf_warning_or_error
, d
);
19760 input_location
= loc
;
19761 pop_deferring_access_checks ();
19762 pop_access_scope (d
);
19765 /* Instantiate the indicated variable, function, or alias template TMPL with
19766 the template arguments in TARG_PTR. */
19769 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19771 tree targ_ptr
= orig_args
;
19775 bool access_ok
= true;
19777 if (tmpl
== error_mark_node
)
19778 return error_mark_node
;
19780 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
19782 /* If this function is a clone, handle it specially. */
19783 if (DECL_CLONED_FUNCTION_P (tmpl
))
19788 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19789 DECL_CLONED_FUNCTION. */
19790 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
19791 targ_ptr
, complain
);
19792 if (spec
== error_mark_node
)
19793 return error_mark_node
;
19795 /* Look for the clone. */
19796 FOR_EACH_CLONE (clone
, spec
)
19797 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
19799 /* We should always have found the clone by now. */
19800 gcc_unreachable ();
19804 if (targ_ptr
== error_mark_node
)
19805 return error_mark_node
;
19807 /* Check to see if we already have this specialization. */
19808 gen_tmpl
= most_general_template (tmpl
);
19809 if (TMPL_ARGS_DEPTH (targ_ptr
)
19810 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
19811 /* targ_ptr only has the innermost template args, so add the outer ones
19812 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19813 the case of a non-dependent call within a template definition). */
19814 targ_ptr
= (add_outermost_template_args
19815 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
19818 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19819 but it doesn't seem to be on the hot path. */
19820 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
19822 gcc_assert (tmpl
== gen_tmpl
19823 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
19825 || fndecl
== NULL_TREE
);
19827 if (spec
!= NULL_TREE
)
19829 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
19831 if (complain
& tf_error
)
19832 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
19833 return error_mark_node
;
19838 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
19840 return error_mark_node
;
19842 /* We are building a FUNCTION_DECL, during which the access of its
19843 parameters and return types have to be checked. However this
19844 FUNCTION_DECL which is the desired context for access checking
19845 is not built yet. We solve this chicken-and-egg problem by
19846 deferring all checks until we have the FUNCTION_DECL. */
19847 push_deferring_access_checks (dk_deferred
);
19849 /* Instantiation of the function happens in the context of the function
19850 template, not the context of the overload resolution we're doing. */
19851 push_to_top_level ();
19852 /* If there are dependent arguments, e.g. because we're doing partial
19853 ordering, make sure processing_template_decl stays set. */
19854 if (uses_template_parms (targ_ptr
))
19855 ++processing_template_decl
;
19856 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19858 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
19859 complain
, gen_tmpl
, true);
19860 push_nested_class (ctx
);
19863 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
19865 fndecl
= NULL_TREE
;
19866 if (VAR_P (pattern
))
19868 /* We need to determine if we're using a partial or explicit
19869 specialization now, because the type of the variable could be
19871 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
19872 tree elt
= most_specialized_partial_spec (tid
, complain
);
19873 if (elt
== error_mark_node
)
19874 pattern
= error_mark_node
;
19877 tree partial_tmpl
= TREE_VALUE (elt
);
19878 tree partial_args
= TREE_PURPOSE (elt
);
19879 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
19880 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
19884 /* Substitute template parameters to obtain the specialization. */
19885 if (fndecl
== NULL_TREE
)
19886 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
19887 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19888 pop_nested_class ();
19889 pop_from_top_level ();
19891 if (fndecl
== error_mark_node
)
19893 pop_deferring_access_checks ();
19894 return error_mark_node
;
19897 /* The DECL_TI_TEMPLATE should always be the immediate parent
19898 template, not the most general template. */
19899 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
19900 DECL_TI_ARGS (fndecl
) = targ_ptr
;
19902 /* Now we know the specialization, compute access previously
19903 deferred. Do no access control for inheriting constructors,
19904 as we already checked access for the inherited constructor. */
19905 if (!(flag_new_inheriting_ctors
19906 && DECL_INHERITED_CTOR (fndecl
)))
19908 push_access_scope (fndecl
);
19909 if (!perform_deferred_access_checks (complain
))
19911 pop_access_scope (fndecl
);
19913 pop_deferring_access_checks ();
19915 /* If we've just instantiated the main entry point for a function,
19916 instantiate all the alternate entry points as well. We do this
19917 by cloning the instantiation of the main entry point, not by
19918 instantiating the template clones. */
19919 if (tree chain
= DECL_CHAIN (gen_tmpl
))
19920 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
19921 clone_function_decl (fndecl
, /*update_methods=*/false);
19925 if (!(complain
& tf_error
))
19927 /* Remember to reinstantiate when we're out of SFINAE so the user
19928 can see the errors. */
19929 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
19931 return error_mark_node
;
19936 /* Wrapper for instantiate_template_1. */
19939 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19942 timevar_push (TV_TEMPLATE_INST
);
19943 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
19944 timevar_pop (TV_TEMPLATE_INST
);
19948 /* Instantiate the alias template TMPL with ARGS. Also push a template
19949 instantiation level, which instantiate_template doesn't do because
19950 functions and variables have sufficient context established by the
19954 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
19956 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
19957 return error_mark_node
;
19958 if (!push_tinst_level (tmpl
, args
))
19959 return error_mark_node
;
19962 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
19963 args
, tmpl
, complain
,
19964 /*require_all_args=*/true,
19965 /*use_default_args=*/true);
19967 tree r
= instantiate_template (tmpl
, args
, complain
);
19968 pop_tinst_level ();
19973 /* PARM is a template parameter pack for FN. Returns true iff
19974 PARM is used in a deducible way in the argument list of FN. */
19977 pack_deducible_p (tree parm
, tree fn
)
19979 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19980 for (; t
; t
= TREE_CHAIN (t
))
19982 tree type
= TREE_VALUE (t
);
19984 if (!PACK_EXPANSION_P (type
))
19986 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19987 packs
; packs
= TREE_CHAIN (packs
))
19988 if (template_args_equal (TREE_VALUE (packs
), parm
))
19990 /* The template parameter pack is used in a function parameter
19991 pack. If this is the end of the parameter list, the
19992 template parameter pack is deducible. */
19993 if (TREE_CHAIN (t
) == void_list_node
)
19996 /* Otherwise, not. Well, it could be deduced from
19997 a non-pack parameter, but doing so would end up with
19998 a deduction mismatch, so don't bother. */
20002 /* The template parameter pack isn't used in any function parameter
20003 packs, but it might be used deeper, e.g. tuple<Args...>. */
20007 /* Subroutine of fn_type_unification: check non-dependent parms for
20011 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20012 tree fn
, unification_kind_t strict
, int flags
,
20013 struct conversion
**convs
, bool explain_p
)
20015 /* Non-constructor methods need to leave a conversion for 'this', which
20016 isn't included in nargs here. */
20017 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20018 && !DECL_CONSTRUCTOR_P (fn
));
20020 for (unsigned ia
= 0;
20021 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20023 tree parm
= TREE_VALUE (parms
);
20025 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20026 && (!TREE_CHAIN (parms
)
20027 || TREE_CHAIN (parms
) == void_list_node
))
20028 /* For a function parameter pack that occurs at the end of the
20029 parameter-declaration-list, the type A of each remaining
20030 argument of the call is compared with the type P of the
20031 declarator-id of the function parameter pack. */
20034 parms
= TREE_CHAIN (parms
);
20036 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20037 /* For a function parameter pack that does not occur at the
20038 end of the parameter-declaration-list, the type of the
20039 parameter pack is a non-deduced context. */
20042 if (!uses_template_parms (parm
))
20044 tree arg
= args
[ia
];
20045 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20046 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20048 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20049 conv_p
, explain_p
))
20059 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20060 NARGS elements of the arguments that are being used when calling
20061 it. TARGS is a vector into which the deduced template arguments
20064 Returns either a FUNCTION_DECL for the matching specialization of FN or
20065 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20066 true, diagnostics will be printed to explain why it failed.
20068 If FN is a conversion operator, or we are trying to produce a specific
20069 specialization, RETURN_TYPE is the return type desired.
20071 The EXPLICIT_TARGS are explicit template arguments provided via a
20074 The parameter STRICT is one of:
20077 We are deducing arguments for a function call, as in
20078 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20079 deducing arguments for a call to the result of a conversion
20080 function template, as in [over.call.object].
20083 We are deducing arguments for a conversion function, as in
20084 [temp.deduct.conv].
20087 We are deducing arguments when doing an explicit instantiation
20088 as in [temp.explicit], when determining an explicit specialization
20089 as in [temp.expl.spec], or when taking the address of a function
20090 template, as in [temp.deduct.funcaddr]. */
20093 fn_type_unification (tree fn
,
20094 tree explicit_targs
,
20097 unsigned int nargs
,
20099 unification_kind_t strict
,
20101 struct conversion
**convs
,
20107 tree decl
= NULL_TREE
;
20108 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20110 static int deduction_depth
;
20111 /* type_unification_real will pass back any access checks from default
20112 template argument substitution. */
20113 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20114 /* We don't have all the template args yet. */
20115 bool incomplete
= true;
20118 if (flag_new_inheriting_ctors
)
20119 fn
= strip_inheriting_ctors (fn
);
20121 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20122 tree r
= error_mark_node
;
20124 tree full_targs
= targs
;
20125 if (TMPL_ARGS_DEPTH (targs
)
20126 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20127 full_targs
= (add_outermost_template_args
20128 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20132 complain
|= tf_decltype
;
20134 /* In C++0x, it's possible to have a function template whose type depends
20135 on itself recursively. This is most obvious with decltype, but can also
20136 occur with enumeration scope (c++/48969). So we need to catch infinite
20137 recursion and reject the substitution at deduction time; this function
20138 will return error_mark_node for any repeated substitution.
20140 This also catches excessive recursion such as when f<N> depends on
20141 f<N-1> across all integers, and returns error_mark_node for all the
20142 substitutions back up to the initial one.
20144 This is, of course, not reentrant. */
20145 if (excessive_deduction_depth
)
20146 return error_mark_node
;
20149 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20151 fntype
= TREE_TYPE (fn
);
20152 if (explicit_targs
)
20156 The specified template arguments must match the template
20157 parameters in kind (i.e., type, nontype, template), and there
20158 must not be more arguments than there are parameters;
20159 otherwise type deduction fails.
20161 Nontype arguments must match the types of the corresponding
20162 nontype template parameters, or must be convertible to the
20163 types of the corresponding nontype parameters as specified in
20164 _temp.arg.nontype_, otherwise type deduction fails.
20166 All references in the function type of the function template
20167 to the corresponding template parameters are replaced by the
20168 specified template argument values. If a substitution in a
20169 template parameter or in the function type of the function
20170 template results in an invalid type, type deduction fails. */
20171 int i
, len
= TREE_VEC_LENGTH (tparms
);
20172 location_t loc
= input_location
;
20173 incomplete
= false;
20175 if (explicit_targs
== error_mark_node
)
20178 if (TMPL_ARGS_DEPTH (explicit_targs
)
20179 < TMPL_ARGS_DEPTH (full_targs
))
20180 explicit_targs
= add_outermost_template_args (full_targs
,
20183 /* Adjust any explicit template arguments before entering the
20184 substitution context. */
20186 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20187 complain
|tf_partial
,
20188 /*require_all_args=*/false,
20189 /*use_default_args=*/false));
20190 if (explicit_targs
== error_mark_node
)
20193 /* Substitute the explicit args into the function type. This is
20194 necessary so that, for instance, explicitly declared function
20195 arguments can match null pointed constants. If we were given
20196 an incomplete set of explicit args, we must not do semantic
20197 processing during substitution as we could create partial
20199 for (i
= 0; i
< len
; i
++)
20201 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20202 bool parameter_pack
= false;
20203 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20205 /* Dig out the actual parm. */
20206 if (TREE_CODE (parm
) == TYPE_DECL
20207 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20209 parm
= TREE_TYPE (parm
);
20210 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20212 else if (TREE_CODE (parm
) == PARM_DECL
)
20214 parm
= DECL_INITIAL (parm
);
20215 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20218 if (targ
== NULL_TREE
)
20219 /* No explicit argument for this template parameter. */
20221 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20223 /* Mark the argument pack as "incomplete". We could
20224 still deduce more arguments during unification.
20225 We remove this mark in type_unification_real. */
20226 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20227 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20228 = ARGUMENT_PACK_ARGS (targ
);
20230 /* We have some incomplete argument packs. */
20237 if (!push_tinst_level (fn
, explicit_targs
))
20239 excessive_deduction_depth
= true;
20242 ++processing_template_decl
;
20243 input_location
= DECL_SOURCE_LOCATION (fn
);
20244 /* Ignore any access checks; we'll see them again in
20245 instantiate_template and they might have the wrong
20246 access path at this point. */
20247 push_deferring_access_checks (dk_deferred
);
20248 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20249 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20250 pop_deferring_access_checks ();
20251 input_location
= loc
;
20252 --processing_template_decl
;
20253 pop_tinst_level ();
20255 if (fntype
== error_mark_node
)
20259 /* Place the explicitly specified arguments in TARGS. */
20260 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20261 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20262 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20263 if (!incomplete
&& CHECKING_P
20264 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20265 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20266 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20269 if (return_type
&& strict
!= DEDUCE_CALL
)
20271 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20272 new_args
[0] = return_type
;
20273 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20281 /* Never do unification on the 'this' parameter. */
20282 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20284 if (return_type
&& strict
== DEDUCE_CALL
)
20286 /* We're deducing for a call to the result of a template conversion
20287 function. The parms we really want are in return_type. */
20288 if (INDIRECT_TYPE_P (return_type
))
20289 return_type
= TREE_TYPE (return_type
);
20290 parms
= TYPE_ARG_TYPES (return_type
);
20292 else if (return_type
)
20294 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20297 /* We allow incomplete unification without an error message here
20298 because the standard doesn't seem to explicitly prohibit it. Our
20299 callers must be ready to deal with unification failures in any
20302 /* If we aren't explaining yet, push tinst context so we can see where
20303 any errors (e.g. from class instantiations triggered by instantiation
20304 of default template arguments) come from. If we are explaining, this
20305 context is redundant. */
20306 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20308 excessive_deduction_depth
= true;
20312 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20313 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20314 strict
, &checks
, explain_p
);
20316 pop_tinst_level ();
20320 /* Now that we have bindings for all of the template arguments,
20321 ensure that the arguments deduced for the template template
20322 parameters have compatible template parameter lists. We cannot
20323 check this property before we have deduced all template
20324 arguments, because the template parameter types of a template
20325 template parameter might depend on prior template parameters
20326 deduced after the template template parameter. The following
20327 ill-formed example illustrates this issue:
20329 template<typename T, template<T> class C> void f(C<5>, T);
20331 template<int N> struct X {};
20334 f(X<5>(), 5l); // error: template argument deduction fails
20337 The template parameter list of 'C' depends on the template type
20338 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20339 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20340 time that we deduce 'C'. */
20341 if (!template_template_parm_bindings_ok_p
20342 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20344 unify_inconsistent_template_template_parameters (explain_p
);
20348 /* DR 1391: All parameters have args, now check non-dependent parms for
20350 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20355 /* All is well so far. Now, check:
20359 When all template arguments have been deduced, all uses of
20360 template parameters in nondeduced contexts are replaced with
20361 the corresponding deduced argument values. If the
20362 substitution results in an invalid type, as described above,
20363 type deduction fails. */
20364 if (!push_tinst_level (fn
, targs
))
20366 excessive_deduction_depth
= true;
20370 /* Also collect access checks from the instantiation. */
20371 reopen_deferring_access_checks (checks
);
20373 decl
= instantiate_template (fn
, targs
, complain
);
20375 checks
= get_deferred_access_checks ();
20376 pop_deferring_access_checks ();
20378 pop_tinst_level ();
20380 if (decl
== error_mark_node
)
20383 /* Now perform any access checks encountered during substitution. */
20384 push_access_scope (decl
);
20385 ok
= perform_access_checks (checks
, complain
);
20386 pop_access_scope (decl
);
20390 /* If we're looking for an exact match, check that what we got
20391 is indeed an exact match. It might not be if some template
20392 parameters are used in non-deduced contexts. But don't check
20393 for an exact match if we have dependent template arguments;
20394 in that case we're doing partial ordering, and we already know
20395 that we have two candidates that will provide the actual type. */
20396 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20398 tree substed
= TREE_TYPE (decl
);
20402 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
20404 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
20405 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
20406 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
20408 unify_type_mismatch (explain_p
, args
[i
],
20409 TREE_VALUE (sarg
));
20414 /* After doing deduction with the inherited constructor, actually return an
20415 instantiation of the inheriting constructor. */
20417 decl
= instantiate_template (orig_fn
, targs
, complain
);
20423 if (excessive_deduction_depth
)
20425 if (deduction_depth
== 0)
20426 /* Reset once we're all the way out. */
20427 excessive_deduction_depth
= false;
20433 /* Adjust types before performing type deduction, as described in
20434 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20435 sections are symmetric. PARM is the type of a function parameter
20436 or the return type of the conversion function. ARG is the type of
20437 the argument passed to the call, or the type of the value
20438 initialized with the result of the conversion function.
20439 ARG_EXPR is the original argument expression, which may be null. */
20442 maybe_adjust_types_for_deduction (unification_kind_t strict
,
20455 /* Swap PARM and ARG throughout the remainder of this
20456 function; the handling is precisely symmetric since PARM
20457 will initialize ARG rather than vice versa. */
20458 std::swap (parm
, arg
);
20462 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20463 too, but here handle it by stripping the reference from PARM
20464 rather than by adding it to ARG. */
20465 if (TYPE_REF_P (*parm
)
20466 && TYPE_REF_IS_RVALUE (*parm
)
20467 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20468 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20469 && TYPE_REF_P (*arg
)
20470 && !TYPE_REF_IS_RVALUE (*arg
))
20471 *parm
= TREE_TYPE (*parm
);
20472 /* Nothing else to do in this case. */
20476 gcc_unreachable ();
20479 if (!TYPE_REF_P (*parm
))
20481 /* [temp.deduct.call]
20483 If P is not a reference type:
20485 --If A is an array type, the pointer type produced by the
20486 array-to-pointer standard conversion (_conv.array_) is
20487 used in place of A for type deduction; otherwise,
20489 --If A is a function type, the pointer type produced by
20490 the function-to-pointer standard conversion
20491 (_conv.func_) is used in place of A for type deduction;
20494 --If A is a cv-qualified type, the top level
20495 cv-qualifiers of A's type are ignored for type
20497 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
20498 *arg
= build_pointer_type (TREE_TYPE (*arg
));
20499 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
20500 *arg
= build_pointer_type (*arg
);
20502 *arg
= TYPE_MAIN_VARIANT (*arg
);
20505 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20506 reference to a cv-unqualified template parameter that does not represent a
20507 template parameter of a class template (during class template argument
20508 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20509 an lvalue, the type "lvalue reference to A" is used in place of A for type
20511 if (TYPE_REF_P (*parm
)
20512 && TYPE_REF_IS_RVALUE (*parm
)
20513 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20514 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
20515 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20516 && (arg_expr
? lvalue_p (arg_expr
)
20517 /* try_one_overload doesn't provide an arg_expr, but
20518 functions are always lvalues. */
20519 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
20520 *arg
= build_reference_type (*arg
);
20522 /* [temp.deduct.call]
20524 If P is a cv-qualified type, the top level cv-qualifiers
20525 of P's type are ignored for type deduction. If P is a
20526 reference type, the type referred to by P is used for
20528 *parm
= TYPE_MAIN_VARIANT (*parm
);
20529 if (TYPE_REF_P (*parm
))
20531 *parm
= TREE_TYPE (*parm
);
20532 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20535 /* DR 322. For conversion deduction, remove a reference type on parm
20536 too (which has been swapped into ARG). */
20537 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
20538 *arg
= TREE_TYPE (*arg
);
20543 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20544 template which doesn't contain any deducible template parameters; check if
20545 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20546 unify_one_argument. */
20549 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
20550 int flags
, struct conversion
**conv_p
,
20556 type
= TREE_TYPE (arg
);
20560 if (same_type_p (parm
, type
))
20561 return unify_success (explain_p
);
20563 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20564 if (strict
== DEDUCE_CONV
)
20566 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
20567 return unify_success (explain_p
);
20569 else if (strict
!= DEDUCE_EXACT
)
20572 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
20574 /* Avoid recalculating this in add_function_candidate. */
20576 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
20578 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
20580 return unify_success (explain_p
);
20583 if (strict
== DEDUCE_EXACT
)
20584 return unify_type_mismatch (explain_p
, parm
, arg
);
20586 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
20589 static bool uses_deducible_template_parms (tree type
);
20591 /* Returns true iff the expression EXPR is one from which a template
20592 argument can be deduced. In other words, if it's an undecorated
20593 use of a template non-type parameter. */
20596 deducible_expression (tree expr
)
20598 /* Strip implicit conversions. */
20599 while (CONVERT_EXPR_P (expr
))
20600 expr
= TREE_OPERAND (expr
, 0);
20601 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
20604 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20605 deducible way; that is, if it has a max value of <PARM> - 1. */
20608 deducible_array_bound (tree domain
)
20610 if (domain
== NULL_TREE
)
20613 tree max
= TYPE_MAX_VALUE (domain
);
20614 if (TREE_CODE (max
) != MINUS_EXPR
)
20617 return deducible_expression (TREE_OPERAND (max
, 0));
20620 /* Returns true iff the template arguments ARGS use a template parameter
20621 in a deducible way. */
20624 deducible_template_args (tree args
)
20626 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
20629 tree elt
= TREE_VEC_ELT (args
, i
);
20630 if (ARGUMENT_PACK_P (elt
))
20631 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
20634 if (PACK_EXPANSION_P (elt
))
20635 elt
= PACK_EXPANSION_PATTERN (elt
);
20636 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
20638 else if (TYPE_P (elt
))
20639 deducible
= uses_deducible_template_parms (elt
);
20641 deducible
= deducible_expression (elt
);
20649 /* Returns true iff TYPE contains any deducible references to template
20650 parameters, as per 14.8.2.5. */
20653 uses_deducible_template_parms (tree type
)
20655 if (PACK_EXPANSION_P (type
))
20656 type
= PACK_EXPANSION_PATTERN (type
);
20663 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20664 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20670 if (INDIRECT_TYPE_P (type
))
20671 return uses_deducible_template_parms (TREE_TYPE (type
));
20673 /* T[integer-constant ]
20675 if (TREE_CODE (type
) == ARRAY_TYPE
)
20676 return (uses_deducible_template_parms (TREE_TYPE (type
))
20677 || deducible_array_bound (TYPE_DOMAIN (type
)));
20689 if (TYPE_PTRMEM_P (type
))
20690 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
20691 || (uses_deducible_template_parms
20692 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
20694 /* template-name <T> (where template-name refers to a class template)
20695 template-name <i> (where template-name refers to a class template) */
20696 if (CLASS_TYPE_P (type
)
20697 && CLASSTYPE_TEMPLATE_INFO (type
)
20698 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
20699 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20700 (CLASSTYPE_TI_ARGS (type
)));
20705 if (FUNC_OR_METHOD_TYPE_P (type
))
20707 if (uses_deducible_template_parms (TREE_TYPE (type
)))
20709 tree parm
= TYPE_ARG_TYPES (type
);
20710 if (TREE_CODE (type
) == METHOD_TYPE
)
20711 parm
= TREE_CHAIN (parm
);
20712 for (; parm
; parm
= TREE_CHAIN (parm
))
20713 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
20720 /* Subroutine of type_unification_real and unify_pack_expansion to
20721 handle unification of a single P/A pair. Parameters are as
20722 for those functions. */
20725 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
20726 int subr
, unification_kind_t strict
,
20729 tree arg_expr
= NULL_TREE
;
20732 if (arg
== error_mark_node
|| parm
== error_mark_node
)
20733 return unify_invalid (explain_p
);
20734 if (arg
== unknown_type_node
)
20735 /* We can't deduce anything from this, but we might get all the
20736 template args from other function args. */
20737 return unify_success (explain_p
);
20739 /* Implicit conversions (Clause 4) will be performed on a function
20740 argument to convert it to the type of the corresponding function
20741 parameter if the parameter type contains no template-parameters that
20742 participate in template argument deduction. */
20743 if (strict
!= DEDUCE_EXACT
20744 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
20745 /* For function parameters with no deducible template parameters,
20746 just return. We'll check non-dependent conversions later. */
20747 return unify_success (explain_p
);
20752 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
20753 | UNIFY_ALLOW_MORE_CV_QUAL
20754 | UNIFY_ALLOW_DERIVED
);
20758 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
20762 arg_strict
= UNIFY_ALLOW_NONE
;
20766 gcc_unreachable ();
20769 /* We only do these transformations if this is the top-level
20770 parameter_type_list in a call or declaration matching; in other
20771 situations (nested function declarators, template argument lists) we
20772 won't be comparing a type to an expression, and we don't do any type
20778 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
20779 if (type_unknown_p (arg
))
20781 /* [temp.deduct.type] A template-argument can be
20782 deduced from a pointer to function or pointer
20783 to member function argument if the set of
20784 overloaded functions does not contain function
20785 templates and at most one of a set of
20786 overloaded functions provides a unique
20788 resolve_overloaded_unification (tparms
, targs
, parm
,
20790 arg_strict
, explain_p
);
20791 /* If a unique match was not found, this is a
20792 non-deduced context, so we still succeed. */
20793 return unify_success (explain_p
);
20797 arg
= unlowered_expr_type (arg
);
20798 if (arg
== error_mark_node
)
20799 return unify_invalid (explain_p
);
20803 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
20806 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
20807 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
20808 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20810 /* For deduction from an init-list we need the actual list. */
20811 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
20813 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
20816 /* for_each_template_parm callback that always returns 0. */
20819 zero_r (tree
, void *)
20824 /* for_each_template_parm any_fn callback to handle deduction of a template
20825 type argument from the type of an array bound. */
20828 array_deduction_r (tree t
, void *data
)
20830 tree_pair_p d
= (tree_pair_p
)data
;
20831 tree
&tparms
= d
->purpose
;
20832 tree
&targs
= d
->value
;
20834 if (TREE_CODE (t
) == ARRAY_TYPE
)
20835 if (tree dom
= TYPE_DOMAIN (t
))
20836 if (tree max
= TYPE_MAX_VALUE (dom
))
20838 if (TREE_CODE (max
) == MINUS_EXPR
)
20839 max
= TREE_OPERAND (max
, 0);
20840 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
20841 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
20842 UNIFY_ALLOW_NONE
, /*explain*/false);
20845 /* Keep walking. */
20849 /* Try to deduce any not-yet-deduced template type arguments from the type of
20850 an array bound. This is handled separately from unify because 14.8.2.5 says
20851 "The type of a type parameter is only deduced from an array bound if it is
20852 not otherwise deduced." */
20855 try_array_deduction (tree tparms
, tree targs
, tree parm
)
20857 tree_pair_s data
= { tparms
, targs
};
20858 hash_set
<tree
> visited
;
20859 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
20860 /*nondeduced*/false, array_deduction_r
);
20863 /* Most parms like fn_type_unification.
20865 If SUBR is 1, we're being called recursively (to unify the
20866 arguments of a function or method parameter of a function
20869 CHECKS is a pointer to a vector of access checks encountered while
20870 substituting default template arguments. */
20873 type_unification_real (tree tparms
,
20877 unsigned int xnargs
,
20879 unification_kind_t strict
,
20880 vec
<deferred_access_check
, va_gc
> **checks
,
20885 int ntparms
= TREE_VEC_LENGTH (tparms
);
20886 int saw_undeduced
= 0;
20889 unsigned int nargs
;
20892 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
20893 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
20894 gcc_assert (ntparms
> 0);
20896 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
20898 /* Reset the number of non-defaulted template arguments contained
20900 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
20908 while (parms
&& parms
!= void_list_node
20911 parm
= TREE_VALUE (parms
);
20913 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20914 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
20915 /* For a function parameter pack that occurs at the end of the
20916 parameter-declaration-list, the type A of each remaining
20917 argument of the call is compared with the type P of the
20918 declarator-id of the function parameter pack. */
20921 parms
= TREE_CHAIN (parms
);
20923 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20924 /* For a function parameter pack that does not occur at the
20925 end of the parameter-declaration-list, the type of the
20926 parameter pack is a non-deduced context. */
20932 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
20938 && parms
!= void_list_node
20939 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
20941 /* Unify the remaining arguments with the pack expansion type. */
20943 tree parmvec
= make_tree_vec (1);
20945 /* Allocate a TREE_VEC and copy in all of the arguments */
20946 argvec
= make_tree_vec (nargs
- ia
);
20947 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
20948 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
20950 /* Copy the parameter into parmvec. */
20951 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
20952 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
20953 /*subr=*/subr
, explain_p
))
20956 /* Advance to the end of the list of parameters. */
20957 parms
= TREE_CHAIN (parms
);
20960 /* Fail if we've reached the end of the parm list, and more args
20961 are present, and the parm list isn't variadic. */
20962 if (ia
< nargs
&& parms
== void_list_node
)
20963 return unify_too_many_arguments (explain_p
, nargs
, ia
);
20964 /* Fail if parms are left and they don't have default values and
20965 they aren't all deduced as empty packs (c++/57397). This is
20966 consistent with sufficient_parms_p. */
20967 if (parms
&& parms
!= void_list_node
20968 && TREE_PURPOSE (parms
) == NULL_TREE
)
20970 unsigned int count
= nargs
;
20975 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
20978 p
= TREE_CHAIN (p
);
20980 while (p
&& p
!= void_list_node
);
20981 if (count
!= nargs
)
20982 return unify_too_few_arguments (explain_p
, ia
, count
,
20988 tsubst_flags_t complain
= (explain_p
20989 ? tf_warning_or_error
20991 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
20993 for (i
= 0; i
< ntparms
; i
++)
20995 tree targ
= TREE_VEC_ELT (targs
, i
);
20996 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20998 /* Clear the "incomplete" flags on all argument packs now so that
20999 substituting them into later default arguments works. */
21000 if (targ
&& ARGUMENT_PACK_P (targ
))
21002 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21003 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21006 if (targ
|| tparm
== error_mark_node
)
21008 tparm
= TREE_VALUE (tparm
);
21010 if (TREE_CODE (tparm
) == TYPE_DECL
21011 && !tried_array_deduction
)
21013 try_array_deduction (tparms
, targs
, xparms
);
21014 tried_array_deduction
= true;
21015 if (TREE_VEC_ELT (targs
, i
))
21019 /* If this is an undeduced nontype parameter that depends on
21020 a type parameter, try another pass; its type may have been
21021 deduced from a later argument than the one from which
21022 this parameter can be deduced. */
21023 if (TREE_CODE (tparm
) == PARM_DECL
21024 && uses_template_parms (TREE_TYPE (tparm
))
21025 && saw_undeduced
< 2)
21031 /* Core issue #226 (C++0x) [temp.deduct]:
21033 If a template argument has not been deduced, its
21034 default template argument, if any, is used.
21036 When we are in C++98 mode, TREE_PURPOSE will either
21037 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21038 to explicitly check cxx_dialect here. */
21039 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21040 /* OK, there is a default argument. Wait until after the
21041 conversion check to do substitution. */
21044 /* If the type parameter is a parameter pack, then it will
21045 be deduced to an empty parameter pack. */
21046 if (template_parameter_pack_p (tparm
))
21050 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21052 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21053 TREE_CONSTANT (arg
) = 1;
21056 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21058 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21060 TREE_VEC_ELT (targs
, i
) = arg
;
21064 return unify_parameter_deduction_failure (explain_p
, tparm
);
21067 /* Now substitute into the default template arguments. */
21068 for (i
= 0; i
< ntparms
; i
++)
21070 tree targ
= TREE_VEC_ELT (targs
, i
);
21071 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21073 if (targ
|| tparm
== error_mark_node
)
21075 tree parm
= TREE_VALUE (tparm
);
21076 tree arg
= TREE_PURPOSE (tparm
);
21077 reopen_deferring_access_checks (*checks
);
21078 location_t save_loc
= input_location
;
21080 input_location
= DECL_SOURCE_LOCATION (parm
);
21082 if (saw_undeduced
== 1
21083 && TREE_CODE (parm
) == PARM_DECL
21084 && uses_template_parms (TREE_TYPE (parm
)))
21086 /* The type of this non-type parameter depends on undeduced
21087 parameters. Don't try to use its default argument yet,
21088 since we might deduce an argument for it on the next pass,
21089 but do check whether the arguments we already have cause
21090 substitution failure, so that that happens before we try
21091 later default arguments (78489). */
21092 ++processing_template_decl
;
21093 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21095 --processing_template_decl
;
21096 if (type
== error_mark_node
)
21097 arg
= error_mark_node
;
21103 /* Even if the call is happening in template context, getting
21104 here means it's non-dependent, and a default argument is
21105 considered a separate definition under [temp.decls], so we can
21106 do this substitution without processing_template_decl. This
21107 is important if the default argument contains something that
21108 might be instantiation-dependent like access (87480). */
21109 processing_template_decl_sentinel s
;
21110 tree substed
= NULL_TREE
;
21111 if (saw_undeduced
== 1)
21113 /* First instatiate in template context, in case we still
21114 depend on undeduced template parameters. */
21115 ++processing_template_decl
;
21116 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21118 --processing_template_decl
;
21119 if (substed
!= error_mark_node
21120 && !uses_template_parms (substed
))
21121 /* We replaced all the tparms, substitute again out of
21122 template context. */
21123 substed
= NULL_TREE
;
21126 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21129 if (!uses_template_parms (substed
))
21130 arg
= convert_template_argument (parm
, substed
, full_targs
,
21131 complain
, i
, NULL_TREE
);
21132 else if (saw_undeduced
== 1)
21135 arg
= error_mark_node
;
21138 input_location
= save_loc
;
21139 *checks
= get_deferred_access_checks ();
21140 pop_deferring_access_checks ();
21142 if (arg
== error_mark_node
)
21146 TREE_VEC_ELT (targs
, i
) = arg
;
21147 /* The position of the first default template argument,
21148 is also the number of non-defaulted arguments in TARGS.
21150 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21151 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21155 if (saw_undeduced
++ == 1)
21159 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21160 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21162 return unify_success (explain_p
);
21165 /* Subroutine of type_unification_real. Args are like the variables
21166 at the call site. ARG is an overloaded function (or template-id);
21167 we try deducing template args from each of the overloads, and if
21168 only one succeeds, we go with that. Modifies TARGS and returns
21169 true on success. */
21172 resolve_overloaded_unification (tree tparms
,
21176 unification_kind_t strict
,
21180 tree tempargs
= copy_node (targs
);
21182 tree goodfn
= NULL_TREE
;
21185 if (TREE_CODE (arg
) == ADDR_EXPR
)
21187 arg
= TREE_OPERAND (arg
, 0);
21193 if (TREE_CODE (arg
) == COMPONENT_REF
)
21194 /* Handle `&x' where `x' is some static or non-static member
21196 arg
= TREE_OPERAND (arg
, 1);
21198 if (TREE_CODE (arg
) == OFFSET_REF
)
21199 arg
= TREE_OPERAND (arg
, 1);
21201 /* Strip baselink information. */
21202 if (BASELINK_P (arg
))
21203 arg
= BASELINK_FUNCTIONS (arg
);
21205 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21207 /* If we got some explicit template args, we need to plug them into
21208 the affected templates before we try to unify, in case the
21209 explicit args will completely resolve the templates in question. */
21212 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21213 arg
= TREE_OPERAND (arg
, 0);
21215 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21218 tree subargs
, elem
;
21220 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21223 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21224 expl_subargs
, NULL_TREE
, tf_none
,
21225 /*require_all_args=*/true,
21226 /*use_default_args=*/true);
21227 if (subargs
!= error_mark_node
21228 && !any_dependent_template_arguments_p (subargs
))
21230 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21231 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21232 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21233 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21242 /* If no templates (or more than one) are fully resolved by the
21243 explicit arguments, this template-id is a non-deduced context; it
21244 could still be OK if we deduce all template arguments for the
21245 enclosing call through other arguments. */
21249 else if (!OVL_P (arg
))
21250 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21251 -- but the deduction does not succeed because the expression is
21252 not just the function on its own. */
21255 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21258 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21259 strict
, sub_strict
, addr_p
, explain_p
)
21260 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21267 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21268 to function or pointer to member function argument if the set of
21269 overloaded functions does not contain function templates and at most
21270 one of a set of overloaded functions provides a unique match.
21272 So if we found multiple possibilities, we return success but don't
21273 deduce anything. */
21277 int i
= TREE_VEC_LENGTH (targs
);
21279 if (TREE_VEC_ELT (tempargs
, i
))
21281 tree old
= TREE_VEC_ELT (targs
, i
);
21282 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21283 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21284 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21285 /* Don't forget explicit template arguments in a pack. */
21286 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21287 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21288 TREE_VEC_ELT (targs
, i
) = new_
;
21297 /* Core DR 115: In contexts where deduction is done and fails, or in
21298 contexts where deduction is not done, if a template argument list is
21299 specified and it, along with any default template arguments, identifies
21300 a single function template specialization, then the template-id is an
21301 lvalue for the function template specialization. */
21304 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21306 tree expr
, offset
, baselink
;
21309 if (!type_unknown_p (orig_expr
))
21314 offset
= NULL_TREE
;
21315 baselink
= NULL_TREE
;
21317 if (TREE_CODE (expr
) == ADDR_EXPR
)
21319 expr
= TREE_OPERAND (expr
, 0);
21322 if (TREE_CODE (expr
) == OFFSET_REF
)
21325 expr
= TREE_OPERAND (expr
, 1);
21327 if (BASELINK_P (expr
))
21330 expr
= BASELINK_FUNCTIONS (expr
);
21333 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21336 tree goodfn
= NULL_TREE
;
21338 /* If we got some explicit template args, we need to plug them into
21339 the affected templates before we try to unify, in case the
21340 explicit args will completely resolve the templates in question. */
21342 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21343 tree arg
= TREE_OPERAND (expr
, 0);
21344 tree badfn
= NULL_TREE
;
21345 tree badargs
= NULL_TREE
;
21347 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21350 tree subargs
, elem
;
21352 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21355 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21356 expl_subargs
, NULL_TREE
, tf_none
,
21357 /*require_all_args=*/true,
21358 /*use_default_args=*/true);
21359 if (subargs
!= error_mark_node
21360 && !any_dependent_template_arguments_p (subargs
))
21362 elem
= instantiate_template (fn
, subargs
, tf_none
);
21363 if (elem
== error_mark_node
)
21368 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21377 mark_used (goodfn
);
21380 expr
= build_baselink (BASELINK_BINFO (baselink
),
21381 BASELINK_ACCESS_BINFO (baselink
),
21382 expr
, BASELINK_OPTYPE (baselink
));
21386 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21387 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21390 expr
= cp_build_addr_expr (expr
, complain
);
21393 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21394 /* There were no good options and at least one bad one, so let the
21395 user know what the problem is. */
21396 instantiate_template (badfn
, badargs
, complain
);
21401 /* As above, but error out if the expression remains overloaded. */
21404 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
21406 exp
= resolve_nondeduced_context (exp
, complain
);
21407 if (type_unknown_p (exp
))
21409 if (complain
& tf_error
)
21410 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
21411 return error_mark_node
;
21416 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21417 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21418 different overloads deduce different arguments for a given parm.
21419 ADDR_P is true if the expression for which deduction is being
21420 performed was of the form "& fn" rather than simply "fn".
21422 Returns 1 on success. */
21425 try_one_overload (tree tparms
,
21430 unification_kind_t strict
,
21439 if (arg
== error_mark_node
)
21442 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21443 to function or pointer to member function argument if the set of
21444 overloaded functions does not contain function templates and at most
21445 one of a set of overloaded functions provides a unique match.
21447 So if this is a template, just return success. */
21449 if (uses_template_parms (arg
))
21452 if (TREE_CODE (arg
) == METHOD_TYPE
)
21453 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
21455 arg
= build_pointer_type (arg
);
21457 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
21459 /* We don't copy orig_targs for this because if we have already deduced
21460 some template args from previous args, unify would complain when we
21461 try to deduce a template parameter for the same argument, even though
21462 there isn't really a conflict. */
21463 nargs
= TREE_VEC_LENGTH (targs
);
21464 tempargs
= make_tree_vec (nargs
);
21466 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
21469 /* First make sure we didn't deduce anything that conflicts with
21470 explicitly specified args. */
21471 for (i
= nargs
; i
--; )
21473 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21474 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
21478 else if (uses_template_parms (elt
))
21479 /* Since we're unifying against ourselves, we will fill in
21480 template args used in the function parm list with our own
21481 template parms. Discard them. */
21482 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
21483 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
21485 /* Check that the argument at each index of the deduced argument pack
21486 is equivalent to the corresponding explicitly specified argument.
21487 We may have deduced more arguments than were explicitly specified,
21490 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21491 that's wrong if we deduce the same argument pack from multiple
21492 function arguments: it's only incomplete the first time. */
21494 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
21495 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
21497 if (TREE_VEC_LENGTH (deduced_pack
)
21498 < TREE_VEC_LENGTH (explicit_pack
))
21501 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
21502 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
21503 TREE_VEC_ELT (deduced_pack
, j
)))
21506 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
21510 for (i
= nargs
; i
--; )
21512 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21515 TREE_VEC_ELT (targs
, i
) = elt
;
21521 /* PARM is a template class (perhaps with unbound template
21522 parameters). ARG is a fully instantiated type. If ARG can be
21523 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21524 TARGS are as for unify. */
21527 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
21530 tree copy_of_targs
;
21532 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21534 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21535 /* Matches anything. */;
21536 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
21537 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
21540 /* We need to make a new template argument vector for the call to
21541 unify. If we used TARGS, we'd clutter it up with the result of
21542 the attempted unification, even if this class didn't work out.
21543 We also don't want to commit ourselves to all the unifications
21544 we've already done, since unification is supposed to be done on
21545 an argument-by-argument basis. In other words, consider the
21546 following pathological case:
21548 template <int I, int J, int K>
21551 template <int I, int J>
21552 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21554 template <int I, int J, int K>
21555 void f(S<I, J, K>, S<I, I, I>);
21564 Now, by the time we consider the unification involving `s2', we
21565 already know that we must have `f<0, 0, 0>'. But, even though
21566 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21567 because there are two ways to unify base classes of S<0, 1, 2>
21568 with S<I, I, I>. If we kept the already deduced knowledge, we
21569 would reject the possibility I=1. */
21570 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
21572 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21574 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
21579 /* If unification failed, we're done. */
21580 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
21581 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
21587 /* Given a template type PARM and a class type ARG, find the unique
21588 base type in ARG that is an instance of PARM. We do not examine
21589 ARG itself; only its base-classes. If there is not exactly one
21590 appropriate base class, return NULL_TREE. PARM may be the type of
21591 a partial specialization, as well as a plain template type. Used
21594 static enum template_base_result
21595 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
21596 bool explain_p
, tree
*result
)
21598 tree rval
= NULL_TREE
;
21601 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
21603 binfo
= TYPE_BINFO (complete_type (arg
));
21606 /* The type could not be completed. */
21607 *result
= NULL_TREE
;
21608 return tbr_incomplete_type
;
21611 /* Walk in inheritance graph order. The search order is not
21612 important, and this avoids multiple walks of virtual bases. */
21613 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
21615 tree r
= try_class_unification (tparms
, targs
, parm
,
21616 BINFO_TYPE (binfo
), explain_p
);
21620 /* If there is more than one satisfactory baseclass, then:
21624 If they yield more than one possible deduced A, the type
21628 if (rval
&& !same_type_p (r
, rval
))
21630 *result
= NULL_TREE
;
21631 return tbr_ambiguous_baseclass
;
21639 return tbr_success
;
21642 /* Returns the level of DECL, which declares a template parameter. */
21645 template_decl_level (tree decl
)
21647 switch (TREE_CODE (decl
))
21650 case TEMPLATE_DECL
:
21651 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
21654 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
21657 gcc_unreachable ();
21662 /* Decide whether ARG can be unified with PARM, considering only the
21663 cv-qualifiers of each type, given STRICT as documented for unify.
21664 Returns nonzero iff the unification is OK on that basis. */
21667 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
21669 int arg_quals
= cp_type_quals (arg
);
21670 int parm_quals
= cp_type_quals (parm
);
21672 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21673 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21675 /* Although a CVR qualifier is ignored when being applied to a
21676 substituted template parameter ([8.3.2]/1 for example), that
21677 does not allow us to unify "const T" with "int&" because both
21678 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21679 It is ok when we're allowing additional CV qualifiers
21680 at the outer level [14.8.2.1]/3,1st bullet. */
21681 if ((TYPE_REF_P (arg
)
21682 || FUNC_OR_METHOD_TYPE_P (arg
))
21683 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
21686 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
21687 && (parm_quals
& TYPE_QUAL_RESTRICT
))
21691 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21692 && (arg_quals
& parm_quals
) != parm_quals
)
21695 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
21696 && (parm_quals
& arg_quals
) != arg_quals
)
21702 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21704 template_parm_level_and_index (tree parm
, int* level
, int* index
)
21706 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21707 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21708 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21710 *index
= TEMPLATE_TYPE_IDX (parm
);
21711 *level
= TEMPLATE_TYPE_LEVEL (parm
);
21715 *index
= TEMPLATE_PARM_IDX (parm
);
21716 *level
= TEMPLATE_PARM_LEVEL (parm
);
21720 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21722 if (unify (TP, TA, P, A, S, EP)) \
21726 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21727 expansion at the end of PACKED_PARMS. Returns 0 if the type
21728 deduction succeeds, 1 otherwise. STRICT is the same as in
21729 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21730 function call argument list. We'll need to adjust the arguments to make them
21731 types. SUBR tells us if this is from a recursive call to
21732 type_unification_real, or for comparing two template argument
21736 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
21737 tree packed_args
, unification_kind_t strict
,
21738 bool subr
, bool explain_p
)
21741 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
21742 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
21743 tree pack
, packs
= NULL_TREE
;
21744 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
21746 /* Add in any args remembered from an earlier partial instantiation. */
21747 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
21748 int levels
= TMPL_ARGS_DEPTH (targs
);
21750 packed_args
= expand_template_argument_pack (packed_args
);
21752 int len
= TREE_VEC_LENGTH (packed_args
);
21754 /* Determine the parameter packs we will be deducing from the
21755 pattern, and record their current deductions. */
21756 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
21757 pack
; pack
= TREE_CHAIN (pack
))
21759 tree parm_pack
= TREE_VALUE (pack
);
21762 /* Only template parameter packs can be deduced, not e.g. function
21763 parameter packs or __bases or __integer_pack. */
21764 if (!TEMPLATE_PARM_P (parm_pack
))
21767 /* Determine the index and level of this parameter pack. */
21768 template_parm_level_and_index (parm_pack
, &level
, &idx
);
21769 if (level
< levels
)
21772 /* Keep track of the parameter packs and their corresponding
21774 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
21775 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
21778 /* Loop through all of the arguments that have not yet been
21779 unified and unify each with the pattern. */
21780 for (i
= start
; i
< len
; i
++)
21783 bool any_explicit
= false;
21784 tree arg
= TREE_VEC_ELT (packed_args
, i
);
21786 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21787 or the element of its argument pack at the current index if
21788 this argument was explicitly specified. */
21789 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21793 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21796 if (TREE_VALUE (pack
)
21797 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
21798 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
21800 any_explicit
= true;
21801 arg
= TREE_VEC_ELT (pargs
, i
- start
);
21803 TMPL_ARG (targs
, level
, idx
) = arg
;
21806 /* If we had explicit template arguments, substitute them into the
21807 pattern before deduction. */
21810 /* Some arguments might still be unspecified or dependent. */
21812 ++processing_template_decl
;
21813 dependent
= any_dependent_template_arguments_p (targs
);
21815 --processing_template_decl
;
21816 parm
= tsubst (pattern
, targs
,
21817 explain_p
? tf_warning_or_error
: tf_none
,
21820 --processing_template_decl
;
21821 if (parm
== error_mark_node
)
21827 /* Unify the pattern with the current argument. */
21828 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
21832 /* For each parameter pack, collect the deduced value. */
21833 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21836 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21838 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
21839 TMPL_ARG (targs
, level
, idx
);
21843 /* Verify that the results of unification with the parameter packs
21844 produce results consistent with what we've seen before, and make
21845 the deduced argument packs available. */
21846 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21848 tree old_pack
= TREE_VALUE (pack
);
21849 tree new_args
= TREE_TYPE (pack
);
21850 int i
, len
= TREE_VEC_LENGTH (new_args
);
21852 bool nondeduced_p
= false;
21854 /* By default keep the original deduced argument pack.
21855 If necessary, more specific code is going to update the
21856 resulting deduced argument later down in this function. */
21857 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21858 TMPL_ARG (targs
, level
, idx
) = old_pack
;
21860 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21861 actually deduce anything. */
21862 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
21863 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
21864 nondeduced_p
= true;
21868 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
21870 /* If we had fewer function args than explicit template args,
21871 just use the explicits. */
21872 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21873 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
21874 if (len
< explicit_len
)
21875 new_args
= explicit_args
;
21881 /* Build the deduced *_ARGUMENT_PACK. */
21882 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
21884 result
= make_node (NONTYPE_ARGUMENT_PACK
);
21885 TREE_CONSTANT (result
) = 1;
21888 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21890 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
21892 /* Note the deduced argument packs for this parameter
21894 TMPL_ARG (targs
, level
, idx
) = result
;
21896 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
21897 && (ARGUMENT_PACK_ARGS (old_pack
)
21898 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
21900 /* We only had the explicitly-provided arguments before, but
21901 now we have a complete set of arguments. */
21902 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21904 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
21905 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
21906 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
21910 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
21911 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
21913 if (!comp_template_args (old_args
, new_args
,
21914 &bad_old_arg
, &bad_new_arg
))
21915 /* Inconsistent unification of this parameter pack. */
21916 return unify_parameter_pack_inconsistent (explain_p
,
21922 return unify_success (explain_p
);
21925 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21926 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21927 parameters and return value are as for unify. */
21930 unify_array_domain (tree tparms
, tree targs
,
21931 tree parm_dom
, tree arg_dom
,
21939 /* Our representation of array types uses "N - 1" as the
21940 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21941 not an integer constant. We cannot unify arbitrarily
21942 complex expressions, so we eliminate the MINUS_EXPRs
21944 parm_max
= TYPE_MAX_VALUE (parm_dom
);
21945 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
21948 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
21949 parm_max
= TREE_OPERAND (parm_max
, 0);
21951 arg_max
= TYPE_MAX_VALUE (arg_dom
);
21952 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
21955 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21956 trying to unify the type of a variable with the type
21957 of a template parameter. For example:
21959 template <unsigned int N>
21960 void f (char (&) [N]);
21967 Here, the type of the ARG will be "int [g(i)]", and
21968 may be a SAVE_EXPR, etc. */
21969 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
21970 return unify_vla_arg (explain_p
, arg_dom
);
21971 arg_max
= TREE_OPERAND (arg_max
, 0);
21974 /* If only one of the bounds used a MINUS_EXPR, compensate
21975 by adding one to the other bound. */
21976 if (parm_cst
&& !arg_cst
)
21977 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21981 else if (arg_cst
&& !parm_cst
)
21982 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21987 return unify (tparms
, targs
, parm_max
, arg_max
,
21988 UNIFY_ALLOW_INTEGER
, explain_p
);
21991 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21993 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
21998 if (PACK_EXPANSION_P (t
))
21999 t
= PACK_EXPANSION_PATTERN (t
);
22000 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22001 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22002 || DECL_TYPE_TEMPLATE_P (t
))
22004 else if (TYPE_P (t
))
22010 /* Deduce the value of template parameters. TPARMS is the (innermost)
22011 set of template parameters to a template. TARGS is the bindings
22012 for those template parameters, as determined thus far; TARGS may
22013 include template arguments for outer levels of template parameters
22014 as well. PARM is a parameter to a template function, or a
22015 subcomponent of that parameter; ARG is the corresponding argument.
22016 This function attempts to match PARM with ARG in a manner
22017 consistent with the existing assignments in TARGS. If more values
22018 are deduced, then TARGS is updated.
22020 Returns 0 if the type deduction succeeds, 1 otherwise. The
22021 parameter STRICT is a bitwise or of the following flags:
22024 Require an exact match between PARM and ARG.
22025 UNIFY_ALLOW_MORE_CV_QUAL:
22026 Allow the deduced ARG to be more cv-qualified (by qualification
22027 conversion) than ARG.
22028 UNIFY_ALLOW_LESS_CV_QUAL:
22029 Allow the deduced ARG to be less cv-qualified than ARG.
22030 UNIFY_ALLOW_DERIVED:
22031 Allow the deduced ARG to be a template base class of ARG,
22032 or a pointer to a template base class of the type pointed to by
22034 UNIFY_ALLOW_INTEGER:
22035 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22036 case for more information.
22037 UNIFY_ALLOW_OUTER_LEVEL:
22038 This is the outermost level of a deduction. Used to determine validity
22039 of qualification conversions. A valid qualification conversion must
22040 have const qualified pointers leading up to the inner type which
22041 requires additional CV quals, except at the outer level, where const
22042 is not required [conv.qual]. It would be normal to set this flag in
22043 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22044 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22045 This is the outermost level of a deduction, and PARM can be more CV
22046 qualified at this point.
22047 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22048 This is the outermost level of a deduction, and PARM can be less CV
22049 qualified at this point. */
22052 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22058 int strict_in
= strict
;
22059 tsubst_flags_t complain
= (explain_p
22060 ? tf_warning_or_error
22063 /* I don't think this will do the right thing with respect to types.
22064 But the only case I've seen it in so far has been array bounds, where
22065 signedness is the only information lost, and I think that will be
22067 while (CONVERT_EXPR_P (parm
))
22068 parm
= TREE_OPERAND (parm
, 0);
22070 if (arg
== error_mark_node
)
22071 return unify_invalid (explain_p
);
22072 if (arg
== unknown_type_node
22073 || arg
== init_list_type_node
)
22074 /* We can't deduce anything from this, but we might get all the
22075 template args from other function args. */
22076 return unify_success (explain_p
);
22078 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22079 return unify_success (explain_p
);
22081 /* If PARM uses template parameters, then we can't bail out here,
22082 even if ARG == PARM, since we won't record unifications for the
22083 template parameters. We might need them if we're trying to
22084 figure out which of two things is more specialized. */
22085 if (arg
== parm
&& !uses_template_parms (parm
))
22086 return unify_success (explain_p
);
22088 /* Handle init lists early, so the rest of the function can assume
22089 we're dealing with a type. */
22090 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22094 tree orig_parm
= parm
;
22096 if (!is_std_init_list (parm
)
22097 && TREE_CODE (parm
) != ARRAY_TYPE
)
22098 /* We can only deduce from an initializer list argument if the
22099 parameter is std::initializer_list or an array; otherwise this
22100 is a non-deduced context. */
22101 return unify_success (explain_p
);
22103 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22104 elttype
= TREE_TYPE (parm
);
22107 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22108 /* Deduction is defined in terms of a single type, so just punt
22109 on the (bizarre) std::initializer_list<T...>. */
22110 if (PACK_EXPANSION_P (elttype
))
22111 return unify_success (explain_p
);
22114 if (strict
!= DEDUCE_EXACT
22115 && TYPE_P (elttype
)
22116 && !uses_deducible_template_parms (elttype
))
22117 /* If ELTTYPE has no deducible template parms, skip deduction from
22118 the list elements. */;
22120 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22122 int elt_strict
= strict
;
22124 if (elt
== error_mark_node
)
22125 return unify_invalid (explain_p
);
22127 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22129 tree type
= TREE_TYPE (elt
);
22130 if (type
== error_mark_node
)
22131 return unify_invalid (explain_p
);
22132 /* It should only be possible to get here for a call. */
22133 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22134 elt_strict
|= maybe_adjust_types_for_deduction
22135 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22139 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22143 if (TREE_CODE (parm
) == ARRAY_TYPE
22144 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22146 /* Also deduce from the length of the initializer list. */
22147 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22148 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22149 if (idx
== error_mark_node
)
22150 return unify_invalid (explain_p
);
22151 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22155 /* If the std::initializer_list<T> deduction worked, replace the
22156 deduced A with std::initializer_list<A>. */
22157 if (orig_parm
!= parm
)
22159 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22160 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22161 targ
= listify (targ
);
22162 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22164 return unify_success (explain_p
);
22167 /* If parm and arg aren't the same kind of thing (template, type, or
22168 expression), fail early. */
22169 if (pa_kind (parm
) != pa_kind (arg
))
22170 return unify_invalid (explain_p
);
22172 /* Immediately reject some pairs that won't unify because of
22173 cv-qualification mismatches. */
22174 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22176 /* It is the elements of the array which hold the cv quals of an array
22177 type, and the elements might be template type parms. We'll check
22178 when we recurse. */
22179 && TREE_CODE (arg
) != ARRAY_TYPE
22180 /* We check the cv-qualifiers when unifying with template type
22181 parameters below. We want to allow ARG `const T' to unify with
22182 PARM `T' for example, when computing which of two templates
22183 is more specialized, for example. */
22184 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22185 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22186 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22188 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22189 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22190 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22191 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22192 strict
&= ~UNIFY_ALLOW_DERIVED
;
22193 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22194 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22196 switch (TREE_CODE (parm
))
22198 case TYPENAME_TYPE
:
22200 case UNBOUND_CLASS_TEMPLATE
:
22201 /* In a type which contains a nested-name-specifier, template
22202 argument values cannot be deduced for template parameters used
22203 within the nested-name-specifier. */
22204 return unify_success (explain_p
);
22206 case TEMPLATE_TYPE_PARM
:
22207 case TEMPLATE_TEMPLATE_PARM
:
22208 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22209 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22210 if (error_operand_p (tparm
))
22211 return unify_invalid (explain_p
);
22213 if (TEMPLATE_TYPE_LEVEL (parm
)
22214 != template_decl_level (tparm
))
22215 /* The PARM is not one we're trying to unify. Just check
22216 to see if it matches ARG. */
22218 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22219 && (is_auto (parm
) ? is_auto (arg
)
22220 : same_type_p (parm
, arg
)))
22221 return unify_success (explain_p
);
22223 return unify_type_mismatch (explain_p
, parm
, arg
);
22225 idx
= TEMPLATE_TYPE_IDX (parm
);
22226 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22227 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22228 if (error_operand_p (tparm
))
22229 return unify_invalid (explain_p
);
22231 /* Check for mixed types and values. */
22232 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22233 && TREE_CODE (tparm
) != TYPE_DECL
)
22234 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22235 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22236 gcc_unreachable ();
22238 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22240 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22241 && CLASS_TYPE_P (arg
))
22243 /* First try to match ARG directly. */
22244 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22248 /* Otherwise, look for a suitable base of ARG, as below. */
22249 enum template_base_result r
;
22250 r
= get_template_base (tparms
, targs
, parm
, arg
,
22253 return unify_no_common_base (explain_p
, r
, parm
, arg
);
22257 /* ARG must be constructed from a template class or a template
22258 template parameter. */
22259 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
22260 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22261 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22263 /* Deduce arguments T, i from TT<T> or TT<i>. */
22264 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22267 arg
= TYPE_TI_TEMPLATE (arg
);
22269 /* Fall through to deduce template name. */
22272 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22273 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22275 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22277 /* Simple cases: Value already set, does match or doesn't. */
22278 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22279 return unify_success (explain_p
);
22281 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22285 /* If PARM is `const T' and ARG is only `int', we don't have
22286 a match unless we are allowing additional qualification.
22287 If ARG is `const int' and PARM is just `T' that's OK;
22288 that binds `const int' to `T'. */
22289 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22291 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22293 /* Consider the case where ARG is `const volatile int' and
22294 PARM is `const T'. Then, T should be `volatile int'. */
22295 arg
= cp_build_qualified_type_real
22296 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22297 if (arg
== error_mark_node
)
22298 return unify_invalid (explain_p
);
22300 /* Simple cases: Value already set, does match or doesn't. */
22301 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22302 return unify_success (explain_p
);
22304 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22306 /* Make sure that ARG is not a variable-sized array. (Note
22307 that were talking about variable-sized arrays (like
22308 `int[n]'), rather than arrays of unknown size (like
22309 `int[]').) We'll get very confused by such a type since
22310 the bound of the array is not constant, and therefore
22311 not mangleable. Besides, such types are not allowed in
22312 ISO C++, so we can do as we please here. We do allow
22313 them for 'auto' deduction, since that isn't ABI-exposed. */
22314 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22315 return unify_vla_arg (explain_p
, arg
);
22317 /* Strip typedefs as in convert_template_argument. */
22318 arg
= canonicalize_type_argument (arg
, tf_none
);
22321 /* If ARG is a parameter pack or an expansion, we cannot unify
22322 against it unless PARM is also a parameter pack. */
22323 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22324 && !template_parameter_pack_p (parm
))
22325 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22327 /* If the argument deduction results is a METHOD_TYPE,
22328 then there is a problem.
22329 METHOD_TYPE doesn't map to any real C++ type the result of
22330 the deduction cannot be of that type. */
22331 if (TREE_CODE (arg
) == METHOD_TYPE
)
22332 return unify_method_type_error (explain_p
, arg
);
22334 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22335 return unify_success (explain_p
);
22337 case TEMPLATE_PARM_INDEX
:
22338 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22339 if (error_operand_p (tparm
))
22340 return unify_invalid (explain_p
);
22342 if (TEMPLATE_PARM_LEVEL (parm
)
22343 != template_decl_level (tparm
))
22345 /* The PARM is not one we're trying to unify. Just check
22346 to see if it matches ARG. */
22347 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22348 && cp_tree_equal (parm
, arg
));
22350 unify_expression_unequal (explain_p
, parm
, arg
);
22354 idx
= TEMPLATE_PARM_IDX (parm
);
22355 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22359 if ((strict
& UNIFY_ALLOW_INTEGER
)
22360 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22361 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22362 /* We're deducing from an array bound, the type doesn't matter. */
22363 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22364 int x
= !cp_tree_equal (targ
, arg
);
22366 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22370 /* [temp.deduct.type] If, in the declaration of a function template
22371 with a non-type template-parameter, the non-type
22372 template-parameter is used in an expression in the function
22373 parameter-list and, if the corresponding template-argument is
22374 deduced, the template-argument type shall match the type of the
22375 template-parameter exactly, except that a template-argument
22376 deduced from an array bound may be of any integral type.
22377 The non-type parameter might use already deduced type parameters. */
22378 tparm
= TREE_TYPE (parm
);
22379 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22380 /* We don't have enough levels of args to do any substitution. This
22381 can happen in the context of -fnew-ttp-matching. */;
22384 ++processing_template_decl
;
22385 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22386 --processing_template_decl
;
22388 if (tree a
= type_uses_auto (tparm
))
22390 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22391 if (tparm
== error_mark_node
)
22396 if (!TREE_TYPE (arg
))
22397 /* Template-parameter dependent expression. Just accept it for now.
22398 It will later be processed in convert_template_argument. */
22400 else if (same_type_ignoring_top_level_qualifiers_p
22401 (non_reference (TREE_TYPE (arg
)),
22402 non_reference (tparm
)))
22403 /* OK. Ignore top-level quals here because a class-type template
22404 parameter object is const. */;
22405 else if ((strict
& UNIFY_ALLOW_INTEGER
)
22406 && CP_INTEGRAL_TYPE_P (tparm
))
22407 /* Convert the ARG to the type of PARM; the deduced non-type
22408 template argument must exactly match the types of the
22409 corresponding parameter. */
22410 arg
= fold (build_nop (tparm
, arg
));
22411 else if (uses_template_parms (tparm
))
22413 /* We haven't deduced the type of this parameter yet. */
22414 if (cxx_dialect
>= cxx17
22415 /* We deduce from array bounds in try_array_deduction. */
22416 && !(strict
& UNIFY_ALLOW_INTEGER
))
22418 /* Deduce it from the non-type argument. */
22419 tree atype
= TREE_TYPE (arg
);
22420 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22422 UNIFY_ALLOW_NONE
, explain_p
);
22425 /* Try again later. */
22426 return unify_success (explain_p
);
22429 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
22431 /* If ARG is a parameter pack or an expansion, we cannot unify
22432 against it unless PARM is also a parameter pack. */
22433 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22434 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
22435 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22438 bool removed_attr
= false;
22439 arg
= strip_typedefs_expr (arg
, &removed_attr
);
22441 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22442 return unify_success (explain_p
);
22446 /* A pointer-to-member constant can be unified only with
22447 another constant. */
22448 if (TREE_CODE (arg
) != PTRMEM_CST
)
22449 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
22451 /* Just unify the class member. It would be useless (and possibly
22452 wrong, depending on the strict flags) to unify also
22453 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22454 arg refer to the same variable, even if through different
22455 classes. For instance:
22457 struct A { int x; };
22460 Unification of &A::x and &B::x must succeed. */
22461 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
22462 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
22467 if (!TYPE_PTR_P (arg
))
22468 return unify_type_mismatch (explain_p
, parm
, arg
);
22470 /* [temp.deduct.call]
22472 A can be another pointer or pointer to member type that can
22473 be converted to the deduced A via a qualification
22474 conversion (_conv.qual_).
22476 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22477 This will allow for additional cv-qualification of the
22478 pointed-to types if appropriate. */
22480 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
22481 /* The derived-to-base conversion only persists through one
22482 level of pointers. */
22483 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
22485 return unify (tparms
, targs
, TREE_TYPE (parm
),
22486 TREE_TYPE (arg
), strict
, explain_p
);
22489 case REFERENCE_TYPE
:
22490 if (!TYPE_REF_P (arg
))
22491 return unify_type_mismatch (explain_p
, parm
, arg
);
22492 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22493 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22496 if (TREE_CODE (arg
) != ARRAY_TYPE
)
22497 return unify_type_mismatch (explain_p
, parm
, arg
);
22498 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
22499 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
22500 return unify_type_mismatch (explain_p
, parm
, arg
);
22501 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22502 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22503 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
22504 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22505 TYPE_DOMAIN (arg
), explain_p
);
22506 return unify_success (explain_p
);
22513 case ENUMERAL_TYPE
:
22516 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22517 return unify_type_mismatch (explain_p
, parm
, arg
);
22519 /* We have already checked cv-qualification at the top of the
22521 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
22522 return unify_type_mismatch (explain_p
, parm
, arg
);
22524 /* As far as unification is concerned, this wins. Later checks
22525 will invalidate it if necessary. */
22526 return unify_success (explain_p
);
22528 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22529 /* Type INTEGER_CST can come from ordinary constant template args. */
22531 while (CONVERT_EXPR_P (arg
))
22532 arg
= TREE_OPERAND (arg
, 0);
22534 if (TREE_CODE (arg
) != INTEGER_CST
)
22535 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22536 return (tree_int_cst_equal (parm
, arg
)
22537 ? unify_success (explain_p
)
22538 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
22542 int i
, len
, argslen
;
22543 int parm_variadic_p
= 0;
22545 if (TREE_CODE (arg
) != TREE_VEC
)
22546 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22548 len
= TREE_VEC_LENGTH (parm
);
22549 argslen
= TREE_VEC_LENGTH (arg
);
22551 /* Check for pack expansions in the parameters. */
22552 for (i
= 0; i
< len
; ++i
)
22554 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
22557 /* We can unify against something with a trailing
22559 parm_variadic_p
= 1;
22561 /* [temp.deduct.type]/9: If the template argument list of
22562 P contains a pack expansion that is not the last
22563 template argument, the entire template argument list
22564 is a non-deduced context. */
22565 return unify_success (explain_p
);
22569 /* If we don't have enough arguments to satisfy the parameters
22570 (not counting the pack expression at the end), or we have
22571 too many arguments for a parameter list that doesn't end in
22572 a pack expression, we can't unify. */
22573 if (parm_variadic_p
22574 ? argslen
< len
- parm_variadic_p
22576 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
22578 /* Unify all of the parameters that precede the (optional)
22579 pack expression. */
22580 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
22582 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22583 TREE_VEC_ELT (parm
, i
),
22584 TREE_VEC_ELT (arg
, i
),
22585 UNIFY_ALLOW_NONE
, explain_p
);
22587 if (parm_variadic_p
)
22588 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
22590 /*subr=*/true, explain_p
);
22591 return unify_success (explain_p
);
22596 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22597 return unify_type_mismatch (explain_p
, parm
, arg
);
22599 if (TYPE_PTRMEMFUNC_P (parm
))
22601 if (!TYPE_PTRMEMFUNC_P (arg
))
22602 return unify_type_mismatch (explain_p
, parm
, arg
);
22604 return unify (tparms
, targs
,
22605 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
22606 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
22607 strict
, explain_p
);
22609 else if (TYPE_PTRMEMFUNC_P (arg
))
22610 return unify_type_mismatch (explain_p
, parm
, arg
);
22612 if (CLASSTYPE_TEMPLATE_INFO (parm
))
22614 tree t
= NULL_TREE
;
22616 if (strict_in
& UNIFY_ALLOW_DERIVED
)
22618 /* First, we try to unify the PARM and ARG directly. */
22619 t
= try_class_unification (tparms
, targs
,
22620 parm
, arg
, explain_p
);
22624 /* Fallback to the special case allowed in
22625 [temp.deduct.call]:
22627 If P is a class, and P has the form
22628 template-id, then A can be a derived class of
22629 the deduced A. Likewise, if P is a pointer to
22630 a class of the form template-id, A can be a
22631 pointer to a derived class pointed to by the
22633 enum template_base_result r
;
22634 r
= get_template_base (tparms
, targs
, parm
, arg
,
22639 /* Don't give the derived diagnostic if we're
22640 already dealing with the same template. */
22642 = (CLASSTYPE_TEMPLATE_INFO (arg
)
22643 && (CLASSTYPE_TI_TEMPLATE (parm
)
22644 == CLASSTYPE_TI_TEMPLATE (arg
)));
22645 return unify_no_common_base (explain_p
&& !same_template
,
22650 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
22651 && (CLASSTYPE_TI_TEMPLATE (parm
)
22652 == CLASSTYPE_TI_TEMPLATE (arg
)))
22653 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22654 Then, we should unify `int' and `U'. */
22657 /* There's no chance of unification succeeding. */
22658 return unify_type_mismatch (explain_p
, parm
, arg
);
22660 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
22661 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
22663 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
22664 return unify_type_mismatch (explain_p
, parm
, arg
);
22665 return unify_success (explain_p
);
22668 case FUNCTION_TYPE
:
22670 unsigned int nargs
;
22675 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22676 return unify_type_mismatch (explain_p
, parm
, arg
);
22678 /* CV qualifications for methods can never be deduced, they must
22679 match exactly. We need to check them explicitly here,
22680 because type_unification_real treats them as any other
22681 cv-qualified parameter. */
22682 if (TREE_CODE (parm
) == METHOD_TYPE
22683 && (!check_cv_quals_for_unify
22685 class_of_this_parm (arg
),
22686 class_of_this_parm (parm
))))
22687 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22688 if (TREE_CODE (arg
) == FUNCTION_TYPE
22689 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
22690 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22691 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
22692 return unify_type_mismatch (explain_p
, parm
, arg
);
22694 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
22695 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
22697 nargs
= list_length (TYPE_ARG_TYPES (arg
));
22698 args
= XALLOCAVEC (tree
, nargs
);
22699 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
22700 a
!= NULL_TREE
&& a
!= void_list_node
;
22701 a
= TREE_CHAIN (a
), ++i
)
22702 args
[i
] = TREE_VALUE (a
);
22705 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
22706 args
, nargs
, 1, DEDUCE_EXACT
,
22710 if (flag_noexcept_type
)
22712 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
22713 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
22714 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
22715 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
22716 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
22717 && uses_template_parms (TREE_PURPOSE (pspec
)))
22718 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
22719 TREE_PURPOSE (aspec
),
22720 UNIFY_ALLOW_NONE
, explain_p
);
22721 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
22722 return unify_type_mismatch (explain_p
, parm
, arg
);
22729 /* Unify a pointer to member with a pointer to member function, which
22730 deduces the type of the member as a function type. */
22731 if (TYPE_PTRMEMFUNC_P (arg
))
22733 /* Check top-level cv qualifiers */
22734 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
22735 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22737 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22738 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
22739 UNIFY_ALLOW_NONE
, explain_p
);
22741 /* Determine the type of the function we are unifying against. */
22742 tree fntype
= static_fn_type (arg
);
22744 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
22747 if (TREE_CODE (arg
) != OFFSET_TYPE
)
22748 return unify_type_mismatch (explain_p
, parm
, arg
);
22749 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22750 TYPE_OFFSET_BASETYPE (arg
),
22751 UNIFY_ALLOW_NONE
, explain_p
);
22752 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22753 strict
, explain_p
);
22756 if (DECL_TEMPLATE_PARM_P (parm
))
22757 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
22758 if (arg
!= scalar_constant_value (parm
))
22759 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22760 return unify_success (explain_p
);
22763 case TEMPLATE_DECL
:
22764 /* Matched cases are handled by the ARG == PARM test above. */
22765 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22768 /* We might get a variable as a non-type template argument in parm if the
22769 corresponding parameter is type-dependent. Make any necessary
22770 adjustments based on whether arg is a reference. */
22771 if (CONSTANT_CLASS_P (arg
))
22772 parm
= fold_non_dependent_expr (parm
, complain
);
22773 else if (REFERENCE_REF_P (arg
))
22775 tree sub
= TREE_OPERAND (arg
, 0);
22777 if (TREE_CODE (sub
) == ADDR_EXPR
)
22778 arg
= TREE_OPERAND (sub
, 0);
22780 /* Now use the normal expression code to check whether they match. */
22783 case TYPE_ARGUMENT_PACK
:
22784 case NONTYPE_ARGUMENT_PACK
:
22785 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
22786 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
22789 case DECLTYPE_TYPE
:
22790 case UNDERLYING_TYPE
:
22791 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22792 or UNDERLYING_TYPE nodes. */
22793 return unify_success (explain_p
);
22796 /* Unification fails if we hit an error node. */
22797 return unify_invalid (explain_p
);
22800 if (REFERENCE_REF_P (parm
))
22802 bool pexp
= PACK_EXPANSION_P (arg
);
22804 arg
= PACK_EXPANSION_PATTERN (arg
);
22805 if (REFERENCE_REF_P (arg
))
22806 arg
= TREE_OPERAND (arg
, 0);
22808 arg
= make_pack_expansion (arg
, complain
);
22809 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
22810 strict
, explain_p
);
22815 /* An unresolved overload is a nondeduced context. */
22816 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
22817 return unify_success (explain_p
);
22818 gcc_assert (EXPR_P (parm
)
22819 || COMPOUND_LITERAL_P (parm
)
22820 || TREE_CODE (parm
) == TRAIT_EXPR
);
22822 /* We must be looking at an expression. This can happen with
22826 void foo(S<I>, S<I + 2>);
22830 template<typename T>
22831 void foo(A<T, T{}>);
22833 This is a "non-deduced context":
22837 The non-deduced contexts are:
22839 --A non-type template argument or an array bound in which
22840 a subexpression references a template parameter.
22842 In these cases, we assume deduction succeeded, but don't
22843 actually infer any unifications. */
22845 if (!uses_template_parms (parm
)
22846 && !template_args_equal (parm
, arg
))
22847 return unify_expression_unequal (explain_p
, parm
, arg
);
22849 return unify_success (explain_p
);
22852 #undef RECUR_AND_CHECK_FAILURE
22854 /* Note that DECL can be defined in this translation unit, if
22858 mark_definable (tree decl
)
22861 DECL_NOT_REALLY_EXTERN (decl
) = 1;
22862 FOR_EACH_CLONE (clone
, decl
)
22863 DECL_NOT_REALLY_EXTERN (clone
) = 1;
22866 /* Called if RESULT is explicitly instantiated, or is a member of an
22867 explicitly instantiated class. */
22870 mark_decl_instantiated (tree result
, int extern_p
)
22872 SET_DECL_EXPLICIT_INSTANTIATION (result
);
22874 /* If this entity has already been written out, it's too late to
22875 make any modifications. */
22876 if (TREE_ASM_WRITTEN (result
))
22879 /* For anonymous namespace we don't need to do anything. */
22880 if (decl_anon_ns_mem_p (result
))
22882 gcc_assert (!TREE_PUBLIC (result
));
22886 if (TREE_CODE (result
) != FUNCTION_DECL
)
22887 /* The TREE_PUBLIC flag for function declarations will have been
22888 set correctly by tsubst. */
22889 TREE_PUBLIC (result
) = 1;
22891 /* This might have been set by an earlier implicit instantiation. */
22892 DECL_COMDAT (result
) = 0;
22895 DECL_NOT_REALLY_EXTERN (result
) = 0;
22898 mark_definable (result
);
22899 mark_needed (result
);
22900 /* Always make artificials weak. */
22901 if (DECL_ARTIFICIAL (result
) && flag_weak
)
22902 comdat_linkage (result
);
22903 /* For WIN32 we also want to put explicit instantiations in
22904 linkonce sections. */
22905 else if (TREE_PUBLIC (result
))
22906 maybe_make_one_only (result
);
22907 if (TREE_CODE (result
) == FUNCTION_DECL
22908 && DECL_TEMPLATE_INSTANTIATED (result
))
22909 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22910 since start_preparsed_function wouldn't have if we had an earlier
22911 extern explicit instantiation. */
22912 DECL_EXTERNAL (result
) = 0;
22915 /* If EXTERN_P, then this function will not be emitted -- unless
22916 followed by an explicit instantiation, at which point its linkage
22917 will be adjusted. If !EXTERN_P, then this function will be
22918 emitted here. In neither circumstance do we want
22919 import_export_decl to adjust the linkage. */
22920 DECL_INTERFACE_KNOWN (result
) = 1;
22923 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22924 important template arguments. If any are missing, we check whether
22925 they're important by using error_mark_node for substituting into any
22926 args that were used for partial ordering (the ones between ARGS and END)
22927 and seeing if it bubbles up. */
22930 check_undeduced_parms (tree targs
, tree args
, tree end
)
22932 bool found
= false;
22934 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
22935 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
22938 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
22942 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
22943 if (substed
== error_mark_node
)
22949 /* Given two function templates PAT1 and PAT2, return:
22951 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22952 -1 if PAT2 is more specialized than PAT1.
22953 0 if neither is more specialized.
22955 LEN indicates the number of parameters we should consider
22956 (defaulted parameters should not be considered).
22958 The 1998 std underspecified function template partial ordering, and
22959 DR214 addresses the issue. We take pairs of arguments, one from
22960 each of the templates, and deduce them against each other. One of
22961 the templates will be more specialized if all the *other*
22962 template's arguments deduce against its arguments and at least one
22963 of its arguments *does* *not* deduce against the other template's
22964 corresponding argument. Deduction is done as for class templates.
22965 The arguments used in deduction have reference and top level cv
22966 qualifiers removed. Iff both arguments were originally reference
22967 types *and* deduction succeeds in both directions, an lvalue reference
22968 wins against an rvalue reference and otherwise the template
22969 with the more cv-qualified argument wins for that pairing (if
22970 neither is more cv-qualified, they both are equal). Unlike regular
22971 deduction, after all the arguments have been deduced in this way,
22972 we do *not* verify the deduced template argument values can be
22973 substituted into non-deduced contexts.
22975 The logic can be a bit confusing here, because we look at deduce1 and
22976 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22977 can find template arguments for pat1 to make arg1 look like arg2, that
22978 means that arg2 is at least as specialized as arg1. */
22981 more_specialized_fn (tree pat1
, tree pat2
, int len
)
22983 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
22984 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
22985 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
22986 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
22987 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
22988 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
22989 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
22990 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
22991 tree origs1
, origs2
;
22992 bool lose1
= false;
22993 bool lose2
= false;
22995 /* Remove the this parameter from non-static member functions. If
22996 one is a non-static member function and the other is not a static
22997 member function, remove the first parameter from that function
22998 also. This situation occurs for operator functions where we
22999 locate both a member function (with this pointer) and non-member
23000 operator (with explicit first operand). */
23001 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23003 len
--; /* LEN is the number of significant arguments for DECL1 */
23004 args1
= TREE_CHAIN (args1
);
23005 if (!DECL_STATIC_FUNCTION_P (decl2
))
23006 args2
= TREE_CHAIN (args2
);
23008 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23010 args2
= TREE_CHAIN (args2
);
23011 if (!DECL_STATIC_FUNCTION_P (decl1
))
23014 args1
= TREE_CHAIN (args1
);
23018 /* If only one is a conversion operator, they are unordered. */
23019 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23022 /* Consider the return type for a conversion function */
23023 if (DECL_CONV_FN_P (decl1
))
23025 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23026 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23030 processing_template_decl
++;
23036 /* Stop when an ellipsis is seen. */
23037 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23039 tree arg1
= TREE_VALUE (args1
);
23040 tree arg2
= TREE_VALUE (args2
);
23041 int deduce1
, deduce2
;
23047 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23048 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23050 /* When both arguments are pack expansions, we need only
23051 unify the patterns themselves. */
23052 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23053 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23055 /* This is the last comparison we need to do. */
23059 /* DR 1847: If a particular P contains no template-parameters that
23060 participate in template argument deduction, that P is not used to
23061 determine the ordering. */
23062 if (!uses_deducible_template_parms (arg1
)
23063 && !uses_deducible_template_parms (arg2
))
23066 if (TYPE_REF_P (arg1
))
23068 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23069 arg1
= TREE_TYPE (arg1
);
23070 quals1
= cp_type_quals (arg1
);
23073 if (TYPE_REF_P (arg2
))
23075 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23076 arg2
= TREE_TYPE (arg2
);
23077 quals2
= cp_type_quals (arg2
);
23080 arg1
= TYPE_MAIN_VARIANT (arg1
);
23081 arg2
= TYPE_MAIN_VARIANT (arg2
);
23083 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23085 int i
, len2
= remaining_arguments (args2
);
23086 tree parmvec
= make_tree_vec (1);
23087 tree argvec
= make_tree_vec (len2
);
23090 /* Setup the parameter vector, which contains only ARG1. */
23091 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23093 /* Setup the argument vector, which contains the remaining
23095 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23096 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23098 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23099 argvec
, DEDUCE_EXACT
,
23100 /*subr=*/true, /*explain_p=*/false)
23103 /* We cannot deduce in the other direction, because ARG1 is
23104 a pack expansion but ARG2 is not. */
23107 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23109 int i
, len1
= remaining_arguments (args1
);
23110 tree parmvec
= make_tree_vec (1);
23111 tree argvec
= make_tree_vec (len1
);
23114 /* Setup the parameter vector, which contains only ARG1. */
23115 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23117 /* Setup the argument vector, which contains the remaining
23119 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23120 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23122 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23123 argvec
, DEDUCE_EXACT
,
23124 /*subr=*/true, /*explain_p=*/false)
23127 /* We cannot deduce in the other direction, because ARG2 is
23128 a pack expansion but ARG1 is not.*/
23134 /* The normal case, where neither argument is a pack
23136 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23137 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23139 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23140 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23144 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23145 arg2, then arg2 is not as specialized as arg1. */
23151 /* "If, for a given type, deduction succeeds in both directions
23152 (i.e., the types are identical after the transformations above)
23153 and both P and A were reference types (before being replaced with
23154 the type referred to above):
23155 - if the type from the argument template was an lvalue reference and
23156 the type from the parameter template was not, the argument type is
23157 considered to be more specialized than the other; otherwise,
23158 - if the type from the argument template is more cv-qualified
23159 than the type from the parameter template (as described above),
23160 the argument type is considered to be more specialized than the other;
23162 - neither type is more specialized than the other." */
23164 if (deduce1
&& deduce2
)
23166 if (ref1
&& ref2
&& ref1
!= ref2
)
23173 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23175 if ((quals1
& quals2
) == quals2
)
23177 if ((quals1
& quals2
) == quals1
)
23182 if (lose1
&& lose2
)
23183 /* We've failed to deduce something in either direction.
23184 These must be unordered. */
23189 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23190 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23191 /* We have already processed all of the arguments in our
23192 handing of the pack expansion type. */
23195 args1
= TREE_CHAIN (args1
);
23196 args2
= TREE_CHAIN (args2
);
23199 /* "In most cases, all template parameters must have values in order for
23200 deduction to succeed, but for partial ordering purposes a template
23201 parameter may remain without a value provided it is not used in the
23202 types being used for partial ordering."
23204 Thus, if we are missing any of the targs1 we need to substitute into
23205 origs1, then pat2 is not as specialized as pat1. This can happen when
23206 there is a nondeduced context. */
23207 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23209 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23212 processing_template_decl
--;
23214 /* If both deductions succeed, the partial ordering selects the more
23215 constrained template. */
23216 if (!lose1
&& !lose2
)
23218 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
23219 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
23220 lose1
= !subsumes_constraints (c1
, c2
);
23221 lose2
= !subsumes_constraints (c2
, c1
);
23224 /* All things being equal, if the next argument is a pack expansion
23225 for one function but not for the other, prefer the
23226 non-variadic function. FIXME this is bogus; see c++/41958. */
23228 && args1
&& TREE_VALUE (args1
)
23229 && args2
&& TREE_VALUE (args2
))
23231 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23232 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23235 if (lose1
== lose2
)
23243 /* Determine which of two partial specializations of TMPL is more
23246 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23247 to the first partial specialization. The TREE_PURPOSE is the
23248 innermost set of template parameters for the partial
23249 specialization. PAT2 is similar, but for the second template.
23251 Return 1 if the first partial specialization is more specialized;
23252 -1 if the second is more specialized; 0 if neither is more
23255 See [temp.class.order] for information about determining which of
23256 two templates is more specialized. */
23259 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
23263 bool any_deductions
= false;
23265 tree tmpl1
= TREE_VALUE (pat1
);
23266 tree tmpl2
= TREE_VALUE (pat2
);
23267 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
23268 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23270 /* Just like what happens for functions, if we are ordering between
23271 different template specializations, we may encounter dependent
23272 types in the arguments, and we need our dependency check functions
23273 to behave correctly. */
23274 ++processing_template_decl
;
23275 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23279 any_deductions
= true;
23282 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23286 any_deductions
= true;
23288 --processing_template_decl
;
23290 /* If both deductions succeed, the partial ordering selects the more
23291 constrained template. */
23292 if (!winner
&& any_deductions
)
23293 return more_constrained (tmpl1
, tmpl2
);
23295 /* In the case of a tie where at least one of the templates
23296 has a parameter pack at the end, the template with the most
23297 non-packed parameters wins. */
23300 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23301 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23303 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23304 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23305 int len1
= TREE_VEC_LENGTH (args1
);
23306 int len2
= TREE_VEC_LENGTH (args2
);
23308 /* We don't count the pack expansion at the end. */
23309 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23311 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23316 else if (len1
< len2
)
23323 /* Return the template arguments that will produce the function signature
23324 DECL from the function template FN, with the explicit template
23325 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23326 also match. Return NULL_TREE if no satisfactory arguments could be
23330 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23332 int ntparms
= DECL_NTPARMS (fn
);
23333 tree targs
= make_tree_vec (ntparms
);
23334 tree decl_type
= TREE_TYPE (decl
);
23335 tree decl_arg_types
;
23337 unsigned int nargs
, ix
;
23340 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23342 /* Never do unification on the 'this' parameter. */
23343 decl_arg_types
= skip_artificial_parms_for (decl
,
23344 TYPE_ARG_TYPES (decl_type
));
23346 nargs
= list_length (decl_arg_types
);
23347 args
= XALLOCAVEC (tree
, nargs
);
23348 for (arg
= decl_arg_types
, ix
= 0;
23349 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23350 arg
= TREE_CHAIN (arg
), ++ix
)
23351 args
[ix
] = TREE_VALUE (arg
);
23353 if (fn_type_unification (fn
, explicit_args
, targs
,
23355 (check_rettype
|| DECL_CONV_FN_P (fn
)
23356 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23357 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23358 /*explain_p=*/false,
23360 == error_mark_node
)
23366 /* Return the innermost template arguments that, when applied to a partial
23367 specialization SPEC_TMPL of TMPL, yield the ARGS.
23369 For example, suppose we have:
23371 template <class T, class U> struct S {};
23372 template <class T> struct S<T*, int> {};
23374 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23375 partial specialization and the ARGS will be {double*, int}. The resulting
23376 vector will be {double}, indicating that `T' is bound to `double'. */
23379 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23381 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23383 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23384 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23386 tree innermost_deduced_args
;
23388 innermost_deduced_args
= make_tree_vec (ntparms
);
23389 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23391 deduced_args
= copy_node (args
);
23392 SET_TMPL_ARGS_LEVEL (deduced_args
,
23393 TMPL_ARGS_DEPTH (deduced_args
),
23394 innermost_deduced_args
);
23397 deduced_args
= innermost_deduced_args
;
23399 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23401 if (unify (tparms
, deduced_args
,
23402 INNERMOST_TEMPLATE_ARGS (spec_args
),
23403 INNERMOST_TEMPLATE_ARGS (args
),
23404 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
23407 for (i
= 0; i
< ntparms
; ++i
)
23408 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
23410 if (!tried_array_deduction
)
23412 try_array_deduction (tparms
, innermost_deduced_args
,
23413 INNERMOST_TEMPLATE_ARGS (spec_args
));
23414 tried_array_deduction
= true;
23415 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
23421 if (!push_tinst_level (spec_tmpl
, deduced_args
))
23423 excessive_deduction_depth
= true;
23427 /* Verify that nondeduced template arguments agree with the type
23428 obtained from argument deduction.
23432 struct A { typedef int X; };
23433 template <class T, class U> struct C {};
23434 template <class T> struct C<T, typename T::X> {};
23436 Then with the instantiation `C<A, int>', we can deduce that
23437 `T' is `A' but unify () does not check whether `typename T::X'
23439 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
23441 if (spec_args
!= error_mark_node
)
23442 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
23443 INNERMOST_TEMPLATE_ARGS (spec_args
),
23444 tmpl
, tf_none
, false, false);
23446 pop_tinst_level ();
23448 if (spec_args
== error_mark_node
23449 /* We only need to check the innermost arguments; the other
23450 arguments will always agree. */
23451 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
23452 INNERMOST_TEMPLATE_ARGS (args
)))
23455 /* Now that we have bindings for all of the template arguments,
23456 ensure that the arguments deduced for the template template
23457 parameters have compatible template parameter lists. See the use
23458 of template_template_parm_bindings_ok_p in fn_type_unification
23459 for more information. */
23460 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
23463 return deduced_args
;
23466 // Compare two function templates T1 and T2 by deducing bindings
23467 // from one against the other. If both deductions succeed, compare
23468 // constraints to see which is more constrained.
23470 more_specialized_inst (tree t1
, tree t2
)
23475 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
23481 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
23487 // If both deductions succeed, then one may be more constrained.
23488 if (count
== 2 && fate
== 0)
23489 fate
= more_constrained (t1
, t2
);
23494 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23495 Return the TREE_LIST node with the most specialized template, if
23496 any. If there is no most specialized template, the error_mark_node
23499 Note that this function does not look at, or modify, the
23500 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23501 returned is one of the elements of INSTANTIATIONS, callers may
23502 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23503 and retrieve it from the value returned. */
23506 most_specialized_instantiation (tree templates
)
23510 ++processing_template_decl
;
23513 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
23515 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
23516 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
23521 /* Equally specialized, move to next function. If there
23522 is no next function, nothing's most specialized. */
23523 fn
= TREE_CHAIN (fn
);
23531 /* Now verify that champ is better than everything earlier in the
23532 instantiation list. */
23533 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
23534 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
23541 processing_template_decl
--;
23544 return error_mark_node
;
23549 /* If DECL is a specialization of some template, return the most
23550 general such template. Otherwise, returns NULL_TREE.
23552 For example, given:
23554 template <class T> struct S { template <class U> void f(U); };
23556 if TMPL is `template <class U> void S<int>::f(U)' this will return
23557 the full template. This function will not trace past partial
23558 specializations, however. For example, given in addition:
23560 template <class T> struct S<T*> { template <class U> void f(U); };
23562 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23563 `template <class T> template <class U> S<T*>::f(U)'. */
23566 most_general_template (tree decl
)
23568 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23570 if (tree tinfo
= get_template_info (decl
))
23571 decl
= TI_TEMPLATE (tinfo
);
23572 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23573 template friend, or a FIELD_DECL for a capture pack. */
23574 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23578 /* Look for more and more general templates. */
23579 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
23581 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23582 (See cp-tree.h for details.) */
23583 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
23586 if (CLASS_TYPE_P (TREE_TYPE (decl
))
23587 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
23588 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
23591 /* Stop if we run into an explicitly specialized class template. */
23592 if (!DECL_NAMESPACE_SCOPE_P (decl
)
23593 && DECL_CONTEXT (decl
)
23594 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
23597 decl
= DECL_TI_TEMPLATE (decl
);
23603 /* Return the most specialized of the template partial specializations
23604 which can produce TARGET, a specialization of some class or variable
23605 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23606 a TEMPLATE_DECL node corresponding to the partial specialization, while
23607 the TREE_PURPOSE is the set of template arguments that must be
23608 substituted into the template pattern in order to generate TARGET.
23610 If the choice of partial specialization is ambiguous, a diagnostic
23611 is issued, and the error_mark_node is returned. If there are no
23612 partial specializations matching TARGET, then NULL_TREE is
23613 returned, indicating that the primary template should be used. */
23616 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
23618 tree list
= NULL_TREE
;
23623 tree outer_args
= NULL_TREE
;
23626 if (TYPE_P (target
))
23628 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
23629 tmpl
= TI_TEMPLATE (tinfo
);
23630 args
= TI_ARGS (tinfo
);
23632 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
23634 tmpl
= TREE_OPERAND (target
, 0);
23635 args
= TREE_OPERAND (target
, 1);
23637 else if (VAR_P (target
))
23639 tree tinfo
= DECL_TEMPLATE_INFO (target
);
23640 tmpl
= TI_TEMPLATE (tinfo
);
23641 args
= TI_ARGS (tinfo
);
23644 gcc_unreachable ();
23646 tree main_tmpl
= most_general_template (tmpl
);
23648 /* For determining which partial specialization to use, only the
23649 innermost args are interesting. */
23650 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23652 outer_args
= strip_innermost_template_args (args
, 1);
23653 args
= INNERMOST_TEMPLATE_ARGS (args
);
23656 /* The caller hasn't called push_to_top_level yet, but we need
23657 get_partial_spec_bindings to be done in non-template context so that we'll
23658 fully resolve everything. */
23659 processing_template_decl_sentinel ptds
;
23661 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
23664 tree spec_tmpl
= TREE_VALUE (t
);
23668 /* Substitute in the template args from the enclosing class. */
23669 ++processing_template_decl
;
23670 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
23671 --processing_template_decl
;
23674 if (spec_tmpl
== error_mark_node
)
23675 return error_mark_node
;
23677 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
23681 spec_args
= add_to_template_args (outer_args
, spec_args
);
23683 /* Keep the candidate only if the constraints are satisfied,
23684 or if we're not compiling with concepts. */
23686 || constraints_satisfied_p (spec_tmpl
, spec_args
))
23688 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
23689 TREE_TYPE (list
) = TREE_TYPE (t
);
23697 ambiguous_p
= false;
23700 t
= TREE_CHAIN (t
);
23701 for (; t
; t
= TREE_CHAIN (t
))
23703 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23710 t
= TREE_CHAIN (t
);
23713 ambiguous_p
= true;
23722 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
23724 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23727 ambiguous_p
= true;
23735 char *spaces
= NULL
;
23736 if (!(complain
& tf_error
))
23737 return error_mark_node
;
23738 if (TYPE_P (target
))
23739 error ("ambiguous template instantiation for %q#T", target
);
23741 error ("ambiguous template instantiation for %q#D", target
);
23742 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
23743 for (t
= list
; t
; t
= TREE_CHAIN (t
))
23745 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
23746 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
23747 "%s %#qS", spaces
? spaces
: str
, subst
);
23748 spaces
= spaces
? spaces
: get_spaces (str
);
23751 return error_mark_node
;
23757 /* Explicitly instantiate DECL. */
23760 do_decl_instantiation (tree decl
, tree storage
)
23762 tree result
= NULL_TREE
;
23765 if (!decl
|| decl
== error_mark_node
)
23766 /* An error occurred, for which grokdeclarator has already issued
23767 an appropriate message. */
23769 else if (! DECL_LANG_SPECIFIC (decl
))
23771 error ("explicit instantiation of non-template %q#D", decl
);
23774 else if (DECL_DECLARED_CONCEPT_P (decl
))
23777 error ("explicit instantiation of variable concept %q#D", decl
);
23779 error ("explicit instantiation of function concept %q#D", decl
);
23783 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
23784 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
23786 if (VAR_P (decl
) && !var_templ
)
23788 /* There is an asymmetry here in the way VAR_DECLs and
23789 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23790 the latter, the DECL we get back will be marked as a
23791 template instantiation, and the appropriate
23792 DECL_TEMPLATE_INFO will be set up. This does not happen for
23793 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23794 should handle VAR_DECLs as it currently handles
23796 if (!DECL_CLASS_SCOPE_P (decl
))
23798 error ("%qD is not a static data member of a class template", decl
);
23801 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
23802 if (!result
|| !VAR_P (result
))
23804 error ("no matching template for %qD found", decl
);
23807 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
23809 error ("type %qT for explicit instantiation %qD does not match "
23810 "declared type %qT", TREE_TYPE (result
), decl
,
23815 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
23817 error ("explicit instantiation of %q#D", decl
);
23823 /* Check for various error cases. Note that if the explicit
23824 instantiation is valid the RESULT will currently be marked as an
23825 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23826 until we get here. */
23828 if (DECL_TEMPLATE_SPECIALIZATION (result
))
23830 /* DR 259 [temp.spec].
23832 Both an explicit instantiation and a declaration of an explicit
23833 specialization shall not appear in a program unless the explicit
23834 instantiation follows a declaration of the explicit specialization.
23836 For a given set of template parameters, if an explicit
23837 instantiation of a template appears after a declaration of an
23838 explicit specialization for that template, the explicit
23839 instantiation has no effect. */
23842 else if (DECL_EXPLICIT_INSTANTIATION (result
))
23846 No program shall explicitly instantiate any template more
23849 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23850 the first instantiation was `extern' and the second is not,
23851 and EXTERN_P for the opposite case. */
23852 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
23853 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
23854 /* If an "extern" explicit instantiation follows an ordinary
23855 explicit instantiation, the template is instantiated. */
23859 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
23861 error ("no matching template for %qD found", result
);
23864 else if (!DECL_TEMPLATE_INFO (result
))
23866 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
23870 if (storage
== NULL_TREE
)
23872 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23874 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
23875 pedwarn (input_location
, OPT_Wpedantic
,
23876 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23881 error ("storage class %qD applied to template instantiation", storage
);
23883 check_explicit_instantiation_namespace (result
);
23884 mark_decl_instantiated (result
, extern_p
);
23886 instantiate_decl (result
, /*defer_ok=*/true,
23887 /*expl_inst_class_mem_p=*/false);
23891 mark_class_instantiated (tree t
, int extern_p
)
23893 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
23894 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
23895 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
23896 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
23899 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
23900 rest_of_type_compilation (t
, 1);
23904 /* Called from do_type_instantiation through binding_table_foreach to
23905 do recursive instantiation for the type bound in ENTRY. */
23907 bt_instantiate_type_proc (binding_entry entry
, void *data
)
23909 tree storage
= *(tree
*) data
;
23911 if (MAYBE_CLASS_TYPE_P (entry
->type
)
23912 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
23913 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
23914 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
23917 /* Perform an explicit instantiation of template class T. STORAGE, if
23918 non-null, is the RID for extern, inline or static. COMPLAIN is
23919 nonzero if this is called from the parser, zero if called recursively,
23920 since the standard is unclear (as detailed below). */
23923 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
23928 int previous_instantiation_extern_p
= 0;
23930 if (TREE_CODE (t
) == TYPE_DECL
)
23933 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
23936 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
23938 error ("explicit instantiation of non-class template %qD", tmpl
);
23940 error ("explicit instantiation of non-template type %qT", t
);
23946 if (!COMPLETE_TYPE_P (t
))
23948 if (complain
& tf_error
)
23949 error ("explicit instantiation of %q#T before definition of template",
23954 if (storage
!= NULL_TREE
)
23956 if (!in_system_header_at (input_location
))
23958 if (storage
== ridpointers
[(int) RID_EXTERN
])
23960 if (cxx_dialect
== cxx98
)
23961 pedwarn (input_location
, OPT_Wpedantic
,
23962 "ISO C++ 1998 forbids the use of %<extern%> on "
23963 "explicit instantiations");
23966 pedwarn (input_location
, OPT_Wpedantic
,
23967 "ISO C++ forbids the use of %qE"
23968 " on explicit instantiations", storage
);
23971 if (storage
== ridpointers
[(int) RID_INLINE
])
23973 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23975 else if (storage
== ridpointers
[(int) RID_STATIC
])
23979 error ("storage class %qD applied to template instantiation",
23985 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
23987 /* DR 259 [temp.spec].
23989 Both an explicit instantiation and a declaration of an explicit
23990 specialization shall not appear in a program unless the explicit
23991 instantiation follows a declaration of the explicit specialization.
23993 For a given set of template parameters, if an explicit
23994 instantiation of a template appears after a declaration of an
23995 explicit specialization for that template, the explicit
23996 instantiation has no effect. */
23999 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24003 No program shall explicitly instantiate any template more
24006 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24007 instantiation was `extern'. If EXTERN_P then the second is.
24008 These cases are OK. */
24009 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24011 if (!previous_instantiation_extern_p
&& !extern_p
24012 && (complain
& tf_error
))
24013 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24015 /* If we've already instantiated the template, just return now. */
24016 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24020 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24021 mark_class_instantiated (t
, extern_p
);
24026 /* In contrast to implicit instantiation, where only the
24027 declarations, and not the definitions, of members are
24028 instantiated, we have here:
24032 The explicit instantiation of a class template specialization
24033 implies the instantiation of all of its members not
24034 previously explicitly specialized in the translation unit
24035 containing the explicit instantiation.
24037 Of course, we can't instantiate member template classes, since we
24038 don't have any arguments for them. Note that the standard is
24039 unclear on whether the instantiation of the members are
24040 *explicit* instantiations or not. However, the most natural
24041 interpretation is that it should be an explicit
24043 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24045 || (TREE_CODE (fld
) == FUNCTION_DECL
24047 && user_provided_p (fld
)))
24048 && DECL_TEMPLATE_INSTANTIATION (fld
))
24050 mark_decl_instantiated (fld
, extern_p
);
24052 instantiate_decl (fld
, /*defer_ok=*/true,
24053 /*expl_inst_class_mem_p=*/true);
24056 if (CLASSTYPE_NESTED_UTDS (t
))
24057 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24058 bt_instantiate_type_proc
, &storage
);
24061 /* Given a function DECL, which is a specialization of TMPL, modify
24062 DECL to be a re-instantiation of TMPL with the same template
24063 arguments. TMPL should be the template into which tsubst'ing
24064 should occur for DECL, not the most general template.
24066 One reason for doing this is a scenario like this:
24069 void f(const T&, int i);
24071 void g() { f(3, 7); }
24074 void f(const T& t, const int i) { }
24076 Note that when the template is first instantiated, with
24077 instantiate_template, the resulting DECL will have no name for the
24078 first parameter, and the wrong type for the second. So, when we go
24079 to instantiate the DECL, we regenerate it. */
24082 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24084 /* The arguments used to instantiate DECL, from the most general
24088 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24090 /* Make sure that we can see identifiers, and compute access
24092 push_access_scope (decl
);
24094 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24102 args_depth
= TMPL_ARGS_DEPTH (args
);
24103 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24104 if (args_depth
> parms_depth
)
24105 args
= get_innermost_template_args (args
, parms_depth
);
24107 /* Instantiate a dynamic exception-specification. noexcept will be
24109 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24110 if (TREE_VALUE (raises
))
24112 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24113 args
, tf_error
, NULL_TREE
,
24114 /*defer_ok*/false);
24115 if (specs
&& specs
!= error_mark_node
)
24116 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24120 /* Merge parameter declarations. */
24121 decl_parm
= skip_artificial_parms_for (decl
,
24122 DECL_ARGUMENTS (decl
));
24124 = skip_artificial_parms_for (code_pattern
,
24125 DECL_ARGUMENTS (code_pattern
));
24126 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24131 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24132 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24133 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24135 parm_type
= type_decays_to (parm_type
);
24136 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24137 TREE_TYPE (decl_parm
) = parm_type
;
24138 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24139 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24141 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24142 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24144 decl_parm
= DECL_CHAIN (decl_parm
);
24145 pattern_parm
= DECL_CHAIN (pattern_parm
);
24147 /* Merge any parameters that match with the function parameter
24149 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24152 tree expanded_types
;
24153 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24154 the parameters in this function parameter pack. */
24155 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24156 args
, tf_error
, NULL_TREE
);
24157 len
= TREE_VEC_LENGTH (expanded_types
);
24158 for (i
= 0; i
< len
; i
++)
24163 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24164 /* Rename the parameter to include the index. */
24165 DECL_NAME (decl_parm
) =
24166 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24167 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24168 parm_type
= type_decays_to (parm_type
);
24169 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24170 TREE_TYPE (decl_parm
) = parm_type
;
24171 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24172 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24174 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24175 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24177 decl_parm
= DECL_CHAIN (decl_parm
);
24180 /* Merge additional specifiers from the CODE_PATTERN. */
24181 if (DECL_DECLARED_INLINE_P (code_pattern
)
24182 && !DECL_DECLARED_INLINE_P (decl
))
24183 DECL_DECLARED_INLINE_P (decl
) = 1;
24185 maybe_instantiate_noexcept (decl
, tf_error
);
24187 else if (VAR_P (decl
))
24189 start_lambda_scope (decl
);
24190 DECL_INITIAL (decl
) =
24191 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
24192 tf_error
, DECL_TI_TEMPLATE (decl
));
24193 finish_lambda_scope ();
24194 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24195 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24196 tf_error
, DECL_TI_TEMPLATE (decl
));
24199 gcc_unreachable ();
24201 pop_access_scope (decl
);
24204 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24205 substituted to get DECL. */
24208 template_for_substitution (tree decl
)
24210 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24212 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24213 for the instantiation. This is not always the most general
24214 template. Consider, for example:
24217 struct S { template <class U> void f();
24218 template <> void f<int>(); };
24220 and an instantiation of S<double>::f<int>. We want TD to be the
24221 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24222 while (/* An instantiation cannot have a definition, so we need a
24223 more general template. */
24224 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24225 /* We must also deal with friend templates. Given:
24227 template <class T> struct S {
24228 template <class U> friend void f() {};
24231 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24232 so far as the language is concerned, but that's still
24233 where we get the pattern for the instantiation from. On
24234 other hand, if the definition comes outside the class, say:
24236 template <class T> struct S {
24237 template <class U> friend void f();
24239 template <class U> friend void f() {}
24241 we don't need to look any further. That's what the check for
24242 DECL_INITIAL is for. */
24243 || (TREE_CODE (decl
) == FUNCTION_DECL
24244 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24245 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24247 /* The present template, TD, should not be a definition. If it
24248 were a definition, we should be using it! Note that we
24249 cannot restructure the loop to just keep going until we find
24250 a template with a definition, since that might go too far if
24251 a specialization was declared, but not defined. */
24253 /* Fetch the more general template. */
24254 tmpl
= DECL_TI_TEMPLATE (tmpl
);
24260 /* Returns true if we need to instantiate this template instance even if we
24261 know we aren't going to emit it. */
24264 always_instantiate_p (tree decl
)
24266 /* We always instantiate inline functions so that we can inline them. An
24267 explicit instantiation declaration prohibits implicit instantiation of
24268 non-inline functions. With high levels of optimization, we would
24269 normally inline non-inline functions -- but we're not allowed to do
24270 that for "extern template" functions. Therefore, we check
24271 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24272 return ((TREE_CODE (decl
) == FUNCTION_DECL
24273 && (DECL_DECLARED_INLINE_P (decl
)
24274 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
24275 /* And we need to instantiate static data members so that
24276 their initializers are available in integral constant
24279 && decl_maybe_constant_var_p (decl
)));
24282 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24283 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24284 error, true otherwise. */
24287 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24289 tree fntype
, spec
, noex
, clone
;
24291 /* Don't instantiate a noexcept-specification from template context. */
24292 if (processing_template_decl
24293 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24296 if (DECL_CLONED_FUNCTION_P (fn
))
24297 fn
= DECL_CLONED_FUNCTION (fn
);
24299 tree orig_fn
= NULL_TREE
;
24300 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24301 its FUNCTION_DECL for the rest of this function -- push_access_scope
24302 doesn't accept TEMPLATE_DECLs. */
24303 if (DECL_FUNCTION_TEMPLATE_P (fn
))
24306 fn
= DECL_TEMPLATE_RESULT (fn
);
24309 fntype
= TREE_TYPE (fn
);
24310 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24312 if (!spec
|| !TREE_PURPOSE (spec
))
24315 noex
= TREE_PURPOSE (spec
);
24317 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24319 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24320 bool added
= false;
24321 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24323 spec
= get_defaulted_eh_spec (fn
, complain
);
24324 if (spec
== error_mark_node
)
24325 /* This might have failed because of an unparsed DMI, so
24326 let's try again later. */
24329 else if (!(added
= !fns
->add (fn
)))
24331 /* If hash_set::add returns true, the element was already there. */
24332 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24333 DECL_SOURCE_LOCATION (fn
));
24335 "exception specification of %qD depends on itself",
24337 spec
= noexcept_false_spec
;
24339 else if (push_tinst_level (fn
))
24341 push_to_top_level ();
24342 push_access_scope (fn
);
24343 push_deferring_access_checks (dk_no_deferred
);
24344 input_location
= DECL_SOURCE_LOCATION (fn
);
24346 /* If needed, set current_class_ptr for the benefit of
24347 tsubst_copy/PARM_DECL. */
24348 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
24349 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
24351 tree this_parm
= DECL_ARGUMENTS (tdecl
);
24352 current_class_ptr
= NULL_TREE
;
24353 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
24354 current_class_ptr
= this_parm
;
24357 /* If this function is represented by a TEMPLATE_DECL, then
24358 the deferred noexcept-specification might still contain
24359 dependent types, even after substitution. And we need the
24360 dependency check functions to work in build_noexcept_spec. */
24362 ++processing_template_decl
;
24364 /* Do deferred instantiation of the noexcept-specifier. */
24365 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24366 DEFERRED_NOEXCEPT_ARGS (noex
),
24367 tf_warning_or_error
, fn
,
24368 /*function_p=*/false,
24371 /* Build up the noexcept-specification. */
24372 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24375 --processing_template_decl
;
24377 pop_deferring_access_checks ();
24378 pop_access_scope (fn
);
24379 pop_tinst_level ();
24380 pop_from_top_level ();
24383 spec
= noexcept_false_spec
;
24388 if (spec
== error_mark_node
)
24390 /* This failed with a hard error, so let's go with false. */
24391 gcc_assert (seen_error ());
24392 spec
= noexcept_false_spec
;
24395 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24397 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
24400 FOR_EACH_CLONE (clone
, fn
)
24402 if (TREE_TYPE (clone
) == fntype
)
24403 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24405 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
24411 /* We're starting to process the function INST, an instantiation of PATTERN;
24412 add their parameters to local_specializations. */
24415 register_parameter_specializations (tree pattern
, tree inst
)
24417 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
24418 tree spec_parm
= DECL_ARGUMENTS (inst
);
24419 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
24421 register_local_specialization (spec_parm
, tmpl_parm
);
24422 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
24423 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
24425 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
24427 if (!DECL_PACK_P (tmpl_parm
))
24429 register_local_specialization (spec_parm
, tmpl_parm
);
24430 spec_parm
= DECL_CHAIN (spec_parm
);
24434 /* Register the (value) argument pack as a specialization of
24435 TMPL_PARM, then move on. */
24436 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
24437 register_local_specialization (argpack
, tmpl_parm
);
24440 gcc_assert (!spec_parm
);
24443 /* Produce the definition of D, a _DECL generated from a template. If
24444 DEFER_OK is true, then we don't have to actually do the
24445 instantiation now; we just have to do it sometime. Normally it is
24446 an error if this is an explicit instantiation but D is undefined.
24447 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24448 instantiated class template. */
24451 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
24453 tree tmpl
= DECL_TI_TEMPLATE (d
);
24460 bool pattern_defined
;
24461 location_t saved_loc
= input_location
;
24462 int saved_unevaluated_operand
= cp_unevaluated_operand
;
24463 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24467 /* This function should only be used to instantiate templates for
24468 functions and static member variables. */
24469 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
24471 /* A concept is never instantiated. */
24472 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
24474 /* Variables are never deferred; if instantiation is required, they
24475 are instantiated right away. That allows for better code in the
24476 case that an expression refers to the value of the variable --
24477 if the variable has a constant value the referring expression can
24478 take advantage of that fact. */
24482 /* Don't instantiate cloned functions. Instead, instantiate the
24483 functions they cloned. */
24484 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
24485 d
= DECL_CLONED_FUNCTION (d
);
24487 if (DECL_TEMPLATE_INSTANTIATED (d
)
24488 || (TREE_CODE (d
) == FUNCTION_DECL
24489 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
24490 || DECL_TEMPLATE_SPECIALIZATION (d
))
24491 /* D has already been instantiated or explicitly specialized, so
24492 there's nothing for us to do here.
24494 It might seem reasonable to check whether or not D is an explicit
24495 instantiation, and, if so, stop here. But when an explicit
24496 instantiation is deferred until the end of the compilation,
24497 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24498 the instantiation. */
24501 /* Check to see whether we know that this template will be
24502 instantiated in some other file, as with "extern template"
24504 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
24506 /* In general, we do not instantiate such templates. */
24507 if (external_p
&& !always_instantiate_p (d
))
24510 gen_tmpl
= most_general_template (tmpl
);
24511 gen_args
= DECL_TI_ARGS (d
);
24513 if (tmpl
!= gen_tmpl
)
24514 /* We should already have the extra args. */
24515 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
24516 == TMPL_ARGS_DEPTH (gen_args
));
24517 /* And what's in the hash table should match D. */
24518 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
24519 || spec
== NULL_TREE
);
24521 /* This needs to happen before any tsubsting. */
24522 if (! push_tinst_level (d
))
24525 timevar_push (TV_TEMPLATE_INST
);
24527 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24528 for the instantiation. */
24529 td
= template_for_substitution (d
);
24534 /* Look up an explicit specialization, if any. */
24535 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
24536 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
24537 if (elt
&& elt
!= error_mark_node
)
24539 td
= TREE_VALUE (elt
);
24540 args
= TREE_PURPOSE (elt
);
24544 code_pattern
= DECL_TEMPLATE_RESULT (td
);
24546 /* We should never be trying to instantiate a member of a class
24547 template or partial specialization. */
24548 gcc_assert (d
!= code_pattern
);
24550 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
24551 || DECL_TEMPLATE_SPECIALIZATION (td
))
24552 /* In the case of a friend template whose definition is provided
24553 outside the class, we may have too many arguments. Drop the
24554 ones we don't need. The same is true for specializations. */
24555 args
= get_innermost_template_args
24556 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
24558 if (TREE_CODE (d
) == FUNCTION_DECL
)
24560 deleted_p
= DECL_DELETED_FN (code_pattern
);
24561 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
24562 && DECL_INITIAL (code_pattern
) != error_mark_node
)
24563 || DECL_DEFAULTED_FN (code_pattern
)
24569 if (DECL_CLASS_SCOPE_P (code_pattern
))
24570 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
24572 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
24575 /* We may be in the middle of deferred access check. Disable it now. */
24576 push_deferring_access_checks (dk_no_deferred
);
24578 /* Unless an explicit instantiation directive has already determined
24579 the linkage of D, remember that a definition is available for
24581 if (pattern_defined
24582 && !DECL_INTERFACE_KNOWN (d
)
24583 && !DECL_NOT_REALLY_EXTERN (d
))
24584 mark_definable (d
);
24586 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
24587 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
24588 input_location
= DECL_SOURCE_LOCATION (d
);
24590 /* If D is a member of an explicitly instantiated class template,
24591 and no definition is available, treat it like an implicit
24593 if (!pattern_defined
&& expl_inst_class_mem_p
24594 && DECL_EXPLICIT_INSTANTIATION (d
))
24596 /* Leave linkage flags alone on instantiations with anonymous
24598 if (TREE_PUBLIC (d
))
24600 DECL_NOT_REALLY_EXTERN (d
) = 0;
24601 DECL_INTERFACE_KNOWN (d
) = 0;
24603 SET_DECL_IMPLICIT_INSTANTIATION (d
);
24606 /* Defer all other templates, unless we have been explicitly
24607 forbidden from doing so. */
24608 if (/* If there is no definition, we cannot instantiate the
24611 /* If it's OK to postpone instantiation, do so. */
24613 /* If this is a static data member that will be defined
24614 elsewhere, we don't want to instantiate the entire data
24615 member, but we do want to instantiate the initializer so that
24616 we can substitute that elsewhere. */
24617 || (external_p
&& VAR_P (d
))
24618 /* Handle here a deleted function too, avoid generating
24619 its body (c++/61080). */
24622 /* The definition of the static data member is now required so
24623 we must substitute the initializer. */
24625 && !DECL_INITIAL (d
)
24626 && DECL_INITIAL (code_pattern
))
24630 bool const_init
= false;
24631 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24633 ns
= decl_namespace_context (d
);
24634 push_nested_namespace (ns
);
24636 push_nested_class (DECL_CONTEXT (d
));
24637 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
24639 tf_warning_or_error
, NULL_TREE
,
24640 /*integral_constant_expression_p=*/false);
24641 /* If instantiating the initializer involved instantiating this
24642 again, don't call cp_finish_decl twice. */
24643 if (!DECL_INITIAL (d
))
24645 /* Make sure the initializer is still constant, in case of
24646 circular dependency (template/instantiate6.C). */
24648 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24649 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
24650 /*asmspec_tree=*/NULL_TREE
,
24651 LOOKUP_ONLYCONVERTING
);
24654 pop_nested_class ();
24655 pop_nested_namespace (ns
);
24658 /* We restore the source position here because it's used by
24659 add_pending_template. */
24660 input_location
= saved_loc
;
24662 if (at_eof
&& !pattern_defined
24663 && DECL_EXPLICIT_INSTANTIATION (d
)
24664 && DECL_NOT_REALLY_EXTERN (d
))
24667 The definition of a non-exported function template, a
24668 non-exported member function template, or a non-exported
24669 member function or static data member of a class template
24670 shall be present in every translation unit in which it is
24671 explicitly instantiated. */
24672 permerror (input_location
, "explicit instantiation of %qD "
24673 "but no definition available", d
);
24675 /* If we're in unevaluated context, we just wanted to get the
24676 constant value; this isn't an odr use, so don't queue
24677 a full instantiation. */
24678 if (cp_unevaluated_operand
!= 0)
24680 /* ??? Historically, we have instantiated inline functions, even
24681 when marked as "extern template". */
24682 if (!(external_p
&& VAR_P (d
)))
24683 add_pending_template (d
);
24687 bool push_to_top
, nested
;
24689 fn_context
= decl_function_context (d
);
24690 if (LAMBDA_FUNCTION_P (d
))
24691 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24692 fn_context
= NULL_TREE
;
24693 nested
= current_function_decl
!= NULL_TREE
;
24694 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
24696 vec
<tree
> omp_privatization_save
;
24698 save_omp_privatization_clauses (omp_privatization_save
);
24701 push_to_top_level ();
24704 gcc_assert (!processing_template_decl
);
24705 push_function_context ();
24706 cp_unevaluated_operand
= 0;
24707 c_inhibit_evaluation_warnings
= 0;
24710 /* Mark D as instantiated so that recursive calls to
24711 instantiate_decl do not try to instantiate it again. */
24712 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
24714 /* Regenerate the declaration in case the template has been modified
24715 by a subsequent redeclaration. */
24716 regenerate_decl_from_template (d
, td
, args
);
24718 /* We already set the file and line above. Reset them now in case
24719 they changed as a result of calling regenerate_decl_from_template. */
24720 input_location
= DECL_SOURCE_LOCATION (d
);
24725 bool const_init
= false;
24727 /* Clear out DECL_RTL; whatever was there before may not be right
24728 since we've reset the type of the declaration. */
24729 SET_DECL_RTL (d
, NULL
);
24730 DECL_IN_AGGR_P (d
) = 0;
24732 /* The initializer is placed in DECL_INITIAL by
24733 regenerate_decl_from_template so we don't need to
24734 push/pop_access_scope again here. Pull it out so that
24735 cp_finish_decl can process it. */
24736 init
= DECL_INITIAL (d
);
24737 DECL_INITIAL (d
) = NULL_TREE
;
24738 DECL_INITIALIZED_P (d
) = 0;
24740 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24741 initializer. That function will defer actual emission until
24742 we have a chance to determine linkage. */
24743 DECL_EXTERNAL (d
) = 0;
24745 /* Enter the scope of D so that access-checking works correctly. */
24746 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24748 push_nested_class (DECL_CONTEXT (d
));
24750 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24751 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
24752 ? LOOKUP_CONSTINIT
: 0);
24753 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
24756 pop_nested_class ();
24758 if (variable_template_p (gen_tmpl
))
24759 note_variable_template_instantiation (d
);
24761 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
24762 synthesize_method (d
);
24763 else if (TREE_CODE (d
) == FUNCTION_DECL
)
24765 /* Set up the list of local specializations. */
24766 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
24767 tree block
= NULL_TREE
;
24769 /* Set up context. */
24770 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24771 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24772 block
= push_stmt_list ();
24774 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
24776 /* Some typedefs referenced from within the template code need to be
24777 access checked at template instantiation time, i.e now. These
24778 types were added to the template at parsing time. Let's get those
24779 and perform the access checks then. */
24780 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
24783 /* Create substitution entries for the parameters. */
24784 register_parameter_specializations (code_pattern
, d
);
24786 /* Substitute into the body of the function. */
24787 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24788 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
24789 tf_warning_or_error
, tmpl
);
24792 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
24793 tf_warning_or_error
, tmpl
,
24794 /*integral_constant_expression_p=*/false);
24796 /* Set the current input_location to the end of the function
24797 so that finish_function knows where we are. */
24799 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
24801 /* Remember if we saw an infinite loop in the template. */
24802 current_function_infinite_loop
24803 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
24806 /* Finish the function. */
24807 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24808 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24809 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
24812 d
= finish_function (/*inline_p=*/false);
24813 expand_or_defer_fn (d
);
24816 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24817 cp_check_omp_declare_reduction (d
);
24820 /* We're not deferring instantiation any more. */
24821 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
24824 pop_from_top_level ();
24826 pop_function_context ();
24829 restore_omp_privatization_clauses (omp_privatization_save
);
24832 pop_deferring_access_checks ();
24833 timevar_pop (TV_TEMPLATE_INST
);
24834 pop_tinst_level ();
24835 input_location
= saved_loc
;
24836 cp_unevaluated_operand
= saved_unevaluated_operand
;
24837 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24842 /* Run through the list of templates that we wish we could
24843 instantiate, and instantiate any we can. RETRIES is the
24844 number of times we retry pending template instantiation. */
24847 instantiate_pending_templates (int retries
)
24850 location_t saved_loc
= input_location
;
24852 /* Instantiating templates may trigger vtable generation. This in turn
24853 may require further template instantiations. We place a limit here
24854 to avoid infinite loop. */
24855 if (pending_templates
&& retries
>= max_tinst_depth
)
24857 tree decl
= pending_templates
->tinst
->maybe_get_node ();
24859 fatal_error (input_location
,
24860 "template instantiation depth exceeds maximum of %d"
24861 " instantiating %q+D, possibly from virtual table generation"
24862 " (use %<-ftemplate-depth=%> to increase the maximum)",
24863 max_tinst_depth
, decl
);
24864 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24865 /* Pretend that we defined it. */
24866 DECL_INITIAL (decl
) = error_mark_node
;
24872 struct pending_template
**t
= &pending_templates
;
24873 struct pending_template
*last
= NULL
;
24877 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
24878 bool complete
= false;
24880 if (TYPE_P (instantiation
))
24882 if (!COMPLETE_TYPE_P (instantiation
))
24884 instantiate_class_template (instantiation
);
24885 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
24886 for (tree fld
= TYPE_FIELDS (instantiation
);
24887 fld
; fld
= TREE_CHAIN (fld
))
24889 || (TREE_CODE (fld
) == FUNCTION_DECL
24890 && !DECL_ARTIFICIAL (fld
)))
24891 && DECL_TEMPLATE_INSTANTIATION (fld
))
24892 instantiate_decl (fld
,
24893 /*defer_ok=*/false,
24894 /*expl_inst_class_mem_p=*/false);
24896 if (COMPLETE_TYPE_P (instantiation
))
24900 complete
= COMPLETE_TYPE_P (instantiation
);
24904 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24905 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
24908 = instantiate_decl (instantiation
,
24909 /*defer_ok=*/false,
24910 /*expl_inst_class_mem_p=*/false);
24911 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
24915 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24916 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
24921 /* If INSTANTIATION has been instantiated, then we don't
24922 need to consider it again in the future. */
24923 struct pending_template
*drop
= *t
;
24925 set_refcount_ptr (drop
->tinst
);
24926 pending_template_freelist ().free (drop
);
24934 set_refcount_ptr (current_tinst_level
);
24936 last_pending_template
= last
;
24938 while (reconsider
);
24940 input_location
= saved_loc
;
24943 /* Substitute ARGVEC into T, which is a list of initializers for
24944 either base class or a non-static data member. The TREE_PURPOSEs
24945 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24946 instantiate_decl. */
24949 tsubst_initializer_list (tree t
, tree argvec
)
24951 tree inits
= NULL_TREE
;
24952 tree target_ctor
= error_mark_node
;
24954 for (; t
; t
= TREE_CHAIN (t
))
24958 tree expanded_bases
= NULL_TREE
;
24959 tree expanded_arguments
= NULL_TREE
;
24962 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
24967 /* Expand the base class expansion type into separate base
24969 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
24970 tf_warning_or_error
,
24972 if (expanded_bases
== error_mark_node
)
24975 /* We'll be building separate TREE_LISTs of arguments for
24977 len
= TREE_VEC_LENGTH (expanded_bases
);
24978 expanded_arguments
= make_tree_vec (len
);
24979 for (i
= 0; i
< len
; i
++)
24980 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
24982 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24983 expand each argument in the TREE_VALUE of t. */
24984 expr
= make_node (EXPR_PACK_EXPANSION
);
24985 PACK_EXPANSION_LOCAL_P (expr
) = true;
24986 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
24987 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
24989 if (TREE_VALUE (t
) == void_type_node
)
24990 /* VOID_TYPE_NODE is used to indicate
24991 value-initialization. */
24993 for (i
= 0; i
< len
; i
++)
24994 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
24998 /* Substitute parameter packs into each argument in the
25000 in_base_initializer
= 1;
25001 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25003 tree expanded_exprs
;
25005 /* Expand the argument. */
25006 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25008 = tsubst_pack_expansion (expr
, argvec
,
25009 tf_warning_or_error
,
25011 if (expanded_exprs
== error_mark_node
)
25014 /* Prepend each of the expanded expressions to the
25015 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25016 for (i
= 0; i
< len
; i
++)
25018 TREE_VEC_ELT (expanded_arguments
, i
) =
25019 tree_cons (NULL_TREE
,
25020 TREE_VEC_ELT (expanded_exprs
, i
),
25021 TREE_VEC_ELT (expanded_arguments
, i
));
25024 in_base_initializer
= 0;
25026 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25027 since we built them backwards. */
25028 for (i
= 0; i
< len
; i
++)
25030 TREE_VEC_ELT (expanded_arguments
, i
) =
25031 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25036 for (i
= 0; i
< len
; ++i
)
25038 if (expanded_bases
)
25040 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25041 decl
= expand_member_init (decl
);
25042 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25047 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25048 tf_warning_or_error
, NULL_TREE
);
25050 decl
= expand_member_init (decl
);
25051 if (decl
&& !DECL_P (decl
))
25052 in_base_initializer
= 1;
25054 init
= TREE_VALUE (t
);
25056 if (init
!= void_type_node
)
25057 init
= tsubst_expr (init
, argvec
,
25058 tf_warning_or_error
, NULL_TREE
,
25059 /*integral_constant_expression_p=*/false);
25060 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25061 /* If we had an initializer but it instantiated to nothing,
25062 value-initialize the object. This will only occur when
25063 the initializer was a pack expansion where the parameter
25064 packs used in that expansion were of length zero. */
25065 init
= void_type_node
;
25066 in_base_initializer
= 0;
25069 if (target_ctor
!= error_mark_node
25070 && init
!= error_mark_node
)
25072 error ("mem-initializer for %qD follows constructor delegation",
25076 /* Look for a target constructor. */
25077 if (init
!= error_mark_node
25078 && decl
&& CLASS_TYPE_P (decl
)
25079 && same_type_p (decl
, current_class_type
))
25081 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25084 error ("constructor delegation follows mem-initializer for %qD",
25085 TREE_PURPOSE (inits
));
25088 target_ctor
= init
;
25093 init
= build_tree_list (decl
, init
);
25094 TREE_CHAIN (init
) = inits
;
25102 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25105 set_current_access_from_decl (tree decl
)
25107 if (TREE_PRIVATE (decl
))
25108 current_access_specifier
= access_private_node
;
25109 else if (TREE_PROTECTED (decl
))
25110 current_access_specifier
= access_protected_node
;
25112 current_access_specifier
= access_public_node
;
25115 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25116 is the instantiation (which should have been created with
25117 start_enum) and ARGS are the template arguments to use. */
25120 tsubst_enum (tree tag
, tree newtag
, tree args
)
25124 if (SCOPED_ENUM_P (newtag
))
25125 begin_scope (sk_scoped_enum
, newtag
);
25127 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25132 decl
= TREE_VALUE (e
);
25133 /* Note that in a template enum, the TREE_VALUE is the
25134 CONST_DECL, not the corresponding INTEGER_CST. */
25135 value
= tsubst_expr (DECL_INITIAL (decl
),
25136 args
, tf_warning_or_error
, NULL_TREE
,
25137 /*integral_constant_expression_p=*/true);
25139 /* Give this enumeration constant the correct access. */
25140 set_current_access_from_decl (decl
);
25142 /* Actually build the enumerator itself. Here we're assuming that
25143 enumerators can't have dependent attributes. */
25144 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25145 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25148 if (SCOPED_ENUM_P (newtag
))
25151 finish_enum_value_list (newtag
);
25152 finish_enum (newtag
);
25154 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25155 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25158 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25159 its type -- but without substituting the innermost set of template
25160 arguments. So, innermost set of template parameters will appear in
25164 get_mostly_instantiated_function_type (tree decl
)
25166 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25167 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
25170 /* Return truthvalue if we're processing a template different from
25171 the last one involved in diagnostics. */
25173 problematic_instantiation_changed (void)
25175 return current_tinst_level
!= last_error_tinst_level
;
25178 /* Remember current template involved in diagnostics. */
25180 record_last_problematic_instantiation (void)
25182 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
25185 struct tinst_level
*
25186 current_instantiation (void)
25188 return current_tinst_level
;
25191 /* Return TRUE if current_function_decl is being instantiated, false
25195 instantiating_current_function_p (void)
25197 return (current_instantiation ()
25198 && (current_instantiation ()->maybe_get_node ()
25199 == current_function_decl
));
25202 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25203 type. Return false for ok, true for disallowed. Issue error and
25204 inform messages under control of COMPLAIN. */
25207 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25209 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25211 else if (TYPE_PTR_P (type
))
25213 else if (TYPE_REF_P (type
)
25214 && !TYPE_REF_IS_RVALUE (type
))
25216 else if (TYPE_PTRMEM_P (type
))
25218 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25220 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25222 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25224 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25226 /* A bound template template parm could later be instantiated to have a valid
25227 nontype parm type via an alias template. */
25228 else if (cxx_dialect
>= cxx11
25229 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25231 else if (CLASS_TYPE_P (type
))
25233 if (cxx_dialect
< cxx2a
)
25235 if (complain
& tf_error
)
25236 error ("non-type template parameters of class type only available "
25237 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25240 if (dependent_type_p (type
))
25242 if (!complete_type_or_else (type
, NULL_TREE
))
25244 if (!literal_type_p (type
))
25246 error ("%qT is not a valid type for a template non-type parameter "
25247 "because it is not literal", type
);
25248 explain_non_literal_class (type
);
25251 if (cp_has_mutable_p (type
))
25253 error ("%qT is not a valid type for a template non-type parameter "
25254 "because it has a mutable member", type
);
25257 /* FIXME check op<=> and strong structural equality once spaceship is
25262 if (complain
& tf_error
)
25264 if (type
== error_mark_node
)
25265 inform (input_location
, "invalid template non-type parameter");
25267 error ("%q#T is not a valid type for a template non-type parameter",
25273 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25274 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25277 dependent_type_p_r (tree type
)
25283 A type is dependent if it is:
25285 -- a template parameter. Template template parameters are types
25286 for us (since TYPE_P holds true for them) so we handle
25288 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25289 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
25291 /* -- a qualified-id with a nested-name-specifier which contains a
25292 class-name that names a dependent type or whose unqualified-id
25293 names a dependent type. */
25294 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25297 /* An alias template specialization can be dependent even if the
25298 resulting type is not. */
25299 if (dependent_alias_template_spec_p (type
))
25302 /* -- a cv-qualified type where the cv-unqualified type is
25304 No code is necessary for this bullet; the code below handles
25305 cv-qualified types, and we don't want to strip aliases with
25306 TYPE_MAIN_VARIANT because of DR 1558. */
25307 /* -- a compound type constructed from any dependent type. */
25308 if (TYPE_PTRMEM_P (type
))
25309 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
25310 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25312 else if (INDIRECT_TYPE_P (type
))
25313 return dependent_type_p (TREE_TYPE (type
));
25314 else if (FUNC_OR_METHOD_TYPE_P (type
))
25318 if (dependent_type_p (TREE_TYPE (type
)))
25320 for (arg_type
= TYPE_ARG_TYPES (type
);
25322 arg_type
= TREE_CHAIN (arg_type
))
25323 if (dependent_type_p (TREE_VALUE (arg_type
)))
25325 if (cxx_dialect
>= cxx17
)
25326 /* A value-dependent noexcept-specifier makes the type dependent. */
25327 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25328 if (tree noex
= TREE_PURPOSE (spec
))
25329 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25330 affect overload resolution and treating it as dependent breaks
25331 things. Same for an unparsed noexcept expression. */
25332 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25333 && TREE_CODE (noex
) != DEFERRED_PARSE
25334 && value_dependent_expression_p (noex
))
25338 /* -- an array type constructed from any dependent type or whose
25339 size is specified by a constant expression that is
25342 We checked for type- and value-dependence of the bounds in
25343 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25344 if (TREE_CODE (type
) == ARRAY_TYPE
)
25346 if (TYPE_DOMAIN (type
)
25347 && dependent_type_p (TYPE_DOMAIN (type
)))
25349 return dependent_type_p (TREE_TYPE (type
));
25352 /* -- a template-id in which either the template name is a template
25354 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25356 /* ... or any of the template arguments is a dependent type or
25357 an expression that is type-dependent or value-dependent. */
25358 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25359 && (any_dependent_template_arguments_p
25360 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25363 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25364 dependent; if the argument of the `typeof' expression is not
25365 type-dependent, then it should already been have resolved. */
25366 if (TREE_CODE (type
) == TYPEOF_TYPE
25367 || TREE_CODE (type
) == DECLTYPE_TYPE
25368 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25371 /* A template argument pack is dependent if any of its packed
25373 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25375 tree args
= ARGUMENT_PACK_ARGS (type
);
25376 int i
, len
= TREE_VEC_LENGTH (args
);
25377 for (i
= 0; i
< len
; ++i
)
25378 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25382 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25383 be template parameters. */
25384 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25387 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25390 /* The standard does not specifically mention types that are local
25391 to template functions or local classes, but they should be
25392 considered dependent too. For example:
25394 template <int I> void f() {
25399 The size of `E' cannot be known until the value of `I' has been
25400 determined. Therefore, `E' must be considered dependent. */
25401 scope
= TYPE_CONTEXT (type
);
25402 if (scope
&& TYPE_P (scope
))
25403 return dependent_type_p (scope
);
25404 /* Don't use type_dependent_expression_p here, as it can lead
25405 to infinite recursion trying to determine whether a lambda
25406 nested in a lambda is dependent (c++/47687). */
25407 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25408 && DECL_LANG_SPECIFIC (scope
)
25409 && DECL_TEMPLATE_INFO (scope
)
25410 && (any_dependent_template_arguments_p
25411 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
25414 /* Other types are non-dependent. */
25418 /* Returns TRUE if TYPE is dependent, in the sense of
25419 [temp.dep.type]. Note that a NULL type is considered dependent. */
25422 dependent_type_p (tree type
)
25424 /* If there are no template parameters in scope, then there can't be
25425 any dependent types. */
25426 if (!processing_template_decl
)
25428 /* If we are not processing a template, then nobody should be
25429 providing us with a dependent type. */
25431 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
25435 /* If the type is NULL, we have not computed a type for the entity
25436 in question; in that case, the type is dependent. */
25440 /* Erroneous types can be considered non-dependent. */
25441 if (type
== error_mark_node
)
25444 /* Getting here with global_type_node means we improperly called this
25445 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25446 gcc_checking_assert (type
!= global_type_node
);
25448 /* If we have not already computed the appropriate value for TYPE,
25450 if (!TYPE_DEPENDENT_P_VALID (type
))
25452 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
25453 TYPE_DEPENDENT_P_VALID (type
) = 1;
25456 return TYPE_DEPENDENT_P (type
);
25459 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25460 lookup. In other words, a dependent type that is not the current
25464 dependent_scope_p (tree scope
)
25466 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
25467 && !currently_open_class (scope
));
25470 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25471 an unknown base of 'this' (and is therefore instantiation-dependent). */
25474 unknown_base_ref_p (tree t
)
25476 if (!current_class_ptr
)
25479 tree mem
= TREE_OPERAND (t
, 1);
25480 if (shared_member_p (mem
))
25483 tree cur
= current_nonlambda_class_type ();
25484 if (!any_dependent_bases_p (cur
))
25487 tree ctx
= TREE_OPERAND (t
, 0);
25488 if (DERIVED_FROM_P (ctx
, cur
))
25494 /* T is a SCOPE_REF; return whether we need to consider it
25495 instantiation-dependent so that we can check access at instantiation
25496 time even though we know which member it resolves to. */
25499 instantiation_dependent_scope_ref_p (tree t
)
25501 if (DECL_P (TREE_OPERAND (t
, 1))
25502 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
25503 && !unknown_base_ref_p (t
)
25504 && accessible_in_template_p (TREE_OPERAND (t
, 0),
25505 TREE_OPERAND (t
, 1)))
25511 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25512 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25515 /* Note that this predicate is not appropriate for general expressions;
25516 only constant expressions (that satisfy potential_constant_expression)
25517 can be tested for value dependence. */
25520 value_dependent_expression_p (tree expression
)
25522 if (!processing_template_decl
|| expression
== NULL_TREE
)
25525 /* A type-dependent expression is also value-dependent. */
25526 if (type_dependent_expression_p (expression
))
25529 switch (TREE_CODE (expression
))
25532 /* A dependent member function of the current instantiation. */
25533 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
25535 case FUNCTION_DECL
:
25536 /* A dependent member function of the current instantiation. */
25537 if (DECL_CLASS_SCOPE_P (expression
)
25538 && dependent_type_p (DECL_CONTEXT (expression
)))
25542 case IDENTIFIER_NODE
:
25543 /* A name that has not been looked up -- must be dependent. */
25546 case TEMPLATE_PARM_INDEX
:
25547 /* A non-type template parm. */
25551 /* A non-type template parm. */
25552 if (DECL_TEMPLATE_PARM_P (expression
))
25554 return value_dependent_expression_p (DECL_INITIAL (expression
));
25557 /* A constant with literal type and is initialized
25558 with an expression that is value-dependent. */
25559 if (DECL_DEPENDENT_INIT_P (expression
)
25560 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25561 || TYPE_REF_P (TREE_TYPE (expression
)))
25563 if (DECL_HAS_VALUE_EXPR_P (expression
))
25565 tree value_expr
= DECL_VALUE_EXPR (expression
);
25566 if (value_dependent_expression_p (value_expr
)
25567 /* __PRETTY_FUNCTION__ inside a template function is dependent
25568 on the name of the function. */
25569 || (DECL_PRETTY_FUNCTION_P (expression
)
25570 /* It might be used in a template, but not a template
25571 function, in which case its DECL_VALUE_EXPR will be
25573 && value_expr
== error_mark_node
))
25578 case DYNAMIC_CAST_EXPR
:
25579 case STATIC_CAST_EXPR
:
25580 case CONST_CAST_EXPR
:
25581 case REINTERPRET_CAST_EXPR
:
25583 case IMPLICIT_CONV_EXPR
:
25584 /* These expressions are value-dependent if the type to which
25585 the cast occurs is dependent or the expression being casted
25586 is value-dependent. */
25588 tree type
= TREE_TYPE (expression
);
25590 if (dependent_type_p (type
))
25593 /* A functional cast has a list of operands. */
25594 expression
= TREE_OPERAND (expression
, 0);
25597 /* If there are no operands, it must be an expression such
25598 as "int()". This should not happen for aggregate types
25599 because it would form non-constant expressions. */
25600 gcc_assert (cxx_dialect
>= cxx11
25601 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
25606 if (TREE_CODE (expression
) == TREE_LIST
)
25607 return any_value_dependent_elements_p (expression
);
25609 return value_dependent_expression_p (expression
);
25613 if (SIZEOF_EXPR_TYPE_P (expression
))
25614 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
25618 /* A `sizeof' expression is value-dependent if the operand is
25619 type-dependent or is a pack expansion. */
25620 expression
= TREE_OPERAND (expression
, 0);
25621 if (PACK_EXPANSION_P (expression
))
25623 else if (TYPE_P (expression
))
25624 return dependent_type_p (expression
);
25625 return instantiation_dependent_uneval_expression_p (expression
);
25627 case AT_ENCODE_EXPR
:
25628 /* An 'encode' expression is value-dependent if the operand is
25630 expression
= TREE_OPERAND (expression
, 0);
25631 return dependent_type_p (expression
);
25633 case NOEXCEPT_EXPR
:
25634 expression
= TREE_OPERAND (expression
, 0);
25635 return instantiation_dependent_uneval_expression_p (expression
);
25638 /* All instantiation-dependent expressions should also be considered
25639 value-dependent. */
25640 return instantiation_dependent_scope_ref_p (expression
);
25642 case COMPONENT_REF
:
25643 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
25644 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
25646 case NONTYPE_ARGUMENT_PACK
:
25647 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25648 is value-dependent. */
25650 tree values
= ARGUMENT_PACK_ARGS (expression
);
25651 int i
, len
= TREE_VEC_LENGTH (values
);
25653 for (i
= 0; i
< len
; ++i
)
25654 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
25662 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
25664 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
25670 if (TREE_CODE (type2
) != TREE_LIST
)
25671 return dependent_type_p (type2
);
25673 for (; type2
; type2
= TREE_CHAIN (type2
))
25674 if (dependent_type_p (TREE_VALUE (type2
)))
25681 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25682 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
25685 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25686 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
25690 tree op
= TREE_OPERAND (expression
, 0);
25691 return (value_dependent_expression_p (op
)
25692 || has_value_dependent_address (op
));
25695 case REQUIRES_EXPR
:
25696 /* Treat all requires-expressions as value-dependent so
25697 we don't try to fold them. */
25701 return dependent_type_p (TREE_OPERAND (expression
, 0));
25705 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
25707 tree fn
= get_callee_fndecl (expression
);
25709 nargs
= call_expr_nargs (expression
);
25710 for (i
= 0; i
< nargs
; ++i
)
25712 tree op
= CALL_EXPR_ARG (expression
, i
);
25713 /* In a call to a constexpr member function, look through the
25714 implicit ADDR_EXPR on the object argument so that it doesn't
25715 cause the call to be considered value-dependent. We also
25716 look through it in potential_constant_expression. */
25717 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
25718 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
25719 && TREE_CODE (op
) == ADDR_EXPR
)
25720 op
= TREE_OPERAND (op
, 0);
25721 if (value_dependent_expression_p (op
))
25727 case TEMPLATE_ID_EXPR
:
25728 return variable_concept_p (TREE_OPERAND (expression
, 0));
25734 if (dependent_type_p (TREE_TYPE (expression
)))
25736 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
25737 if (value_dependent_expression_p (val
))
25743 /* Treat a GNU statement expression as dependent to avoid crashing
25744 under instantiate_non_dependent_expr; it can't be constant. */
25748 /* A constant expression is value-dependent if any subexpression is
25749 value-dependent. */
25750 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
25752 case tcc_reference
:
25754 case tcc_comparison
:
25756 case tcc_expression
:
25759 int i
, len
= cp_tree_operand_length (expression
);
25761 for (i
= 0; i
< len
; i
++)
25763 tree t
= TREE_OPERAND (expression
, i
);
25765 /* In some cases, some of the operands may be missing.
25766 (For example, in the case of PREDECREMENT_EXPR, the
25767 amount to increment by may be missing.) That doesn't
25768 make the expression dependent. */
25769 if (t
&& value_dependent_expression_p (t
))
25780 /* The expression is not value-dependent. */
25784 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25785 [temp.dep.expr]. Note that an expression with no type is
25786 considered dependent. Other parts of the compiler arrange for an
25787 expression with type-dependent subexpressions to have no type, so
25788 this function doesn't have to be fully recursive. */
25791 type_dependent_expression_p (tree expression
)
25793 if (!processing_template_decl
)
25796 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
25799 STRIP_ANY_LOCATION_WRAPPER (expression
);
25801 /* An unresolved name is always dependent. */
25802 if (identifier_p (expression
)
25803 || TREE_CODE (expression
) == USING_DECL
25804 || TREE_CODE (expression
) == WILDCARD_DECL
)
25807 /* A lambda-expression in template context is dependent. dependent_type_p is
25808 true for a lambda in the scope of a class or function template, but that
25809 doesn't cover all template contexts, like a default template argument. */
25810 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
25813 /* A fold expression is type-dependent. */
25814 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
25815 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
25816 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
25817 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
25820 /* Some expression forms are never type-dependent. */
25821 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
25822 || TREE_CODE (expression
) == SIZEOF_EXPR
25823 || TREE_CODE (expression
) == ALIGNOF_EXPR
25824 || TREE_CODE (expression
) == AT_ENCODE_EXPR
25825 || TREE_CODE (expression
) == NOEXCEPT_EXPR
25826 || TREE_CODE (expression
) == TRAIT_EXPR
25827 || TREE_CODE (expression
) == TYPEID_EXPR
25828 || TREE_CODE (expression
) == DELETE_EXPR
25829 || TREE_CODE (expression
) == VEC_DELETE_EXPR
25830 || TREE_CODE (expression
) == THROW_EXPR
25831 || TREE_CODE (expression
) == REQUIRES_EXPR
)
25834 /* The types of these expressions depends only on the type to which
25835 the cast occurs. */
25836 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
25837 || TREE_CODE (expression
) == STATIC_CAST_EXPR
25838 || TREE_CODE (expression
) == CONST_CAST_EXPR
25839 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
25840 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
25841 || TREE_CODE (expression
) == CAST_EXPR
)
25842 return dependent_type_p (TREE_TYPE (expression
));
25844 /* The types of these expressions depends only on the type created
25845 by the expression. */
25846 if (TREE_CODE (expression
) == NEW_EXPR
25847 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
25849 /* For NEW_EXPR tree nodes created inside a template, either
25850 the object type itself or a TREE_LIST may appear as the
25852 tree type
= TREE_OPERAND (expression
, 1);
25853 if (TREE_CODE (type
) == TREE_LIST
)
25854 /* This is an array type. We need to check array dimensions
25856 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
25857 || value_dependent_expression_p
25858 (TREE_OPERAND (TREE_VALUE (type
), 1));
25860 return dependent_type_p (type
);
25863 if (TREE_CODE (expression
) == SCOPE_REF
)
25865 tree scope
= TREE_OPERAND (expression
, 0);
25866 tree name
= TREE_OPERAND (expression
, 1);
25868 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25869 contains an identifier associated by name lookup with one or more
25870 declarations declared with a dependent type, or...a
25871 nested-name-specifier or qualified-id that names a member of an
25872 unknown specialization. */
25873 return (type_dependent_expression_p (name
)
25874 || dependent_scope_p (scope
));
25877 if (TREE_CODE (expression
) == TEMPLATE_DECL
25878 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
25879 return uses_outer_template_parms (expression
);
25881 if (TREE_CODE (expression
) == STMT_EXPR
)
25882 expression
= stmt_expr_value_expr (expression
);
25884 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
25889 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
25891 if (type_dependent_expression_p (elt
))
25897 /* A static data member of the current instantiation with incomplete
25898 array type is type-dependent, as the definition and specializations
25899 can have different bounds. */
25900 if (VAR_P (expression
)
25901 && DECL_CLASS_SCOPE_P (expression
)
25902 && dependent_type_p (DECL_CONTEXT (expression
))
25903 && VAR_HAD_UNKNOWN_BOUND (expression
))
25906 /* An array of unknown bound depending on a variadic parameter, eg:
25908 template<typename... Args>
25909 void foo (Args... args)
25911 int arr[] = { args... };
25914 template<int... vals>
25917 int arr[] = { vals... };
25920 If the array has no length and has an initializer, it must be that
25921 we couldn't determine its length in cp_complete_array_type because
25922 it is dependent. */
25923 if (VAR_P (expression
)
25924 && TREE_TYPE (expression
) != NULL_TREE
25925 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
25926 && !TYPE_DOMAIN (TREE_TYPE (expression
))
25927 && DECL_INITIAL (expression
))
25930 /* A function or variable template-id is type-dependent if it has any
25931 dependent template arguments. */
25932 if (VAR_OR_FUNCTION_DECL_P (expression
)
25933 && DECL_LANG_SPECIFIC (expression
)
25934 && DECL_TEMPLATE_INFO (expression
))
25936 /* Consider the innermost template arguments, since those are the ones
25937 that come from the template-id; the template arguments for the
25938 enclosing class do not make it type-dependent unless they are used in
25939 the type of the decl. */
25940 if (instantiates_primary_template_p (expression
)
25941 && (any_dependent_template_arguments_p
25942 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
25946 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25947 type-dependent. Checking this is important for functions with auto return
25948 type, which looks like a dependent type. */
25949 if (TREE_CODE (expression
) == FUNCTION_DECL
25950 && !(DECL_CLASS_SCOPE_P (expression
)
25951 && dependent_type_p (DECL_CONTEXT (expression
)))
25952 && !(DECL_LANG_SPECIFIC (expression
)
25953 && DECL_FRIEND_P (expression
)
25954 && (!DECL_FRIEND_CONTEXT (expression
)
25955 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
25956 && !DECL_LOCAL_FUNCTION_P (expression
))
25958 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
25959 || undeduced_auto_decl (expression
));
25963 /* Always dependent, on the number of arguments if nothing else. */
25964 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
25967 if (TREE_TYPE (expression
) == unknown_type_node
)
25969 if (TREE_CODE (expression
) == ADDR_EXPR
)
25970 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
25971 if (TREE_CODE (expression
) == COMPONENT_REF
25972 || TREE_CODE (expression
) == OFFSET_REF
)
25974 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25976 expression
= TREE_OPERAND (expression
, 1);
25977 if (identifier_p (expression
))
25980 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25981 if (TREE_CODE (expression
) == SCOPE_REF
)
25984 if (BASELINK_P (expression
))
25986 if (BASELINK_OPTYPE (expression
)
25987 && dependent_type_p (BASELINK_OPTYPE (expression
)))
25989 expression
= BASELINK_FUNCTIONS (expression
);
25992 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
25994 if (any_dependent_template_arguments_p
25995 (TREE_OPERAND (expression
, 1)))
25997 expression
= TREE_OPERAND (expression
, 0);
25998 if (identifier_p (expression
))
26002 gcc_assert (OVL_P (expression
));
26004 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26005 if (type_dependent_expression_p (*iter
))
26011 /* The type of a non-type template parm declared with a placeholder type
26012 depends on the corresponding template argument, even though
26013 placeholders are not normally considered dependent. */
26014 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26015 && is_auto (TREE_TYPE (expression
)))
26018 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26020 /* Dependent type attributes might not have made it from the decl to
26022 if (DECL_P (expression
)
26023 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26026 return (dependent_type_p (TREE_TYPE (expression
)));
26029 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26030 type-dependent if the expression refers to a member of the current
26031 instantiation and the type of the referenced member is dependent, or the
26032 class member access expression refers to a member of an unknown
26035 This function returns true if the OBJECT in such a class member access
26036 expression is of an unknown specialization. */
26039 type_dependent_object_expression_p (tree object
)
26041 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26043 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26045 tree scope
= TREE_TYPE (object
);
26046 return (!scope
|| dependent_scope_p (scope
));
26049 /* walk_tree callback function for instantiation_dependent_expression_p,
26050 below. Returns non-zero if a dependent subexpression is found. */
26053 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26058 /* We don't have to worry about decltype currently because decltype
26059 of an instantiation-dependent expr is a dependent type. This
26060 might change depending on the resolution of DR 1172. */
26061 *walk_subtrees
= false;
26064 enum tree_code code
= TREE_CODE (*tp
);
26067 /* Don't treat an argument list as dependent just because it has no
26071 case NONTYPE_ARGUMENT_PACK
:
26074 case TEMPLATE_PARM_INDEX
:
26075 if (dependent_type_p (TREE_TYPE (*tp
)))
26077 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26079 /* We'll check value-dependence separately. */
26082 /* Handle expressions with type operands. */
26086 case AT_ENCODE_EXPR
:
26088 tree op
= TREE_OPERAND (*tp
, 0);
26089 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26090 op
= TREE_TYPE (op
);
26093 if (dependent_type_p (op
))
26097 *walk_subtrees
= false;
26104 case COMPONENT_REF
:
26105 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26106 /* In a template, finish_class_member_access_expr creates a
26107 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26108 type-dependent, so that we can check access control at
26109 instantiation time (PR 42277). See also Core issue 1273. */
26114 if (instantiation_dependent_scope_ref_p (*tp
))
26119 /* Treat statement-expressions as dependent. */
26123 /* Treat requires-expressions as dependent. */
26124 case REQUIRES_EXPR
:
26128 /* Treat calls to function concepts as dependent. */
26129 if (function_concept_check_p (*tp
))
26133 case TEMPLATE_ID_EXPR
:
26134 /* And variable concepts. */
26135 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
26140 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26148 if (type_dependent_expression_p (*tp
))
26154 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26155 sense defined by the ABI:
26157 "An expression is instantiation-dependent if it is type-dependent
26158 or value-dependent, or it has a subexpression that is type-dependent
26159 or value-dependent."
26161 Except don't actually check value-dependence for unevaluated expressions,
26162 because in sizeof(i) we don't care about the value of i. Checking
26163 type-dependence will in turn check value-dependence of array bounds/template
26164 arguments as needed. */
26167 instantiation_dependent_uneval_expression_p (tree expression
)
26171 if (!processing_template_decl
)
26174 if (expression
== error_mark_node
)
26177 result
= cp_walk_tree_without_duplicates (&expression
,
26178 instantiation_dependent_r
, NULL
);
26179 return result
!= NULL_TREE
;
26182 /* As above, but also check value-dependence of the expression as a whole. */
26185 instantiation_dependent_expression_p (tree expression
)
26187 return (instantiation_dependent_uneval_expression_p (expression
)
26188 || value_dependent_expression_p (expression
));
26191 /* Like type_dependent_expression_p, but it also works while not processing
26192 a template definition, i.e. during substitution or mangling. */
26195 type_dependent_expression_p_push (tree expr
)
26198 ++processing_template_decl
;
26199 b
= type_dependent_expression_p (expr
);
26200 --processing_template_decl
;
26204 /* Returns TRUE if ARGS contains a type-dependent expression. */
26207 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
26212 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
26214 if (type_dependent_expression_p (arg
))
26220 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26221 expressions) contains any type-dependent expressions. */
26224 any_type_dependent_elements_p (const_tree list
)
26226 for (; list
; list
= TREE_CHAIN (list
))
26227 if (type_dependent_expression_p (TREE_VALUE (list
)))
26233 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26234 expressions) contains any value-dependent expressions. */
26237 any_value_dependent_elements_p (const_tree list
)
26239 for (; list
; list
= TREE_CHAIN (list
))
26240 if (value_dependent_expression_p (TREE_VALUE (list
)))
26246 /* Returns TRUE if the ARG (a template argument) is dependent. */
26249 dependent_template_arg_p (tree arg
)
26251 if (!processing_template_decl
)
26254 /* Assume a template argument that was wrongly written by the user
26255 is dependent. This is consistent with what
26256 any_dependent_template_arguments_p [that calls this function]
26258 if (!arg
|| arg
== error_mark_node
)
26261 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
26262 arg
= argument_pack_select_arg (arg
);
26264 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
26266 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26268 if (DECL_TEMPLATE_PARM_P (arg
))
26270 /* A member template of a dependent class is not necessarily
26271 type-dependent, but it is a dependent template argument because it
26272 will be a member of an unknown specialization to that template. */
26273 tree scope
= CP_DECL_CONTEXT (arg
);
26274 return TYPE_P (scope
) && dependent_type_p (scope
);
26276 else if (ARGUMENT_PACK_P (arg
))
26278 tree args
= ARGUMENT_PACK_ARGS (arg
);
26279 int i
, len
= TREE_VEC_LENGTH (args
);
26280 for (i
= 0; i
< len
; ++i
)
26282 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26288 else if (TYPE_P (arg
))
26289 return dependent_type_p (arg
);
26291 return (type_dependent_expression_p (arg
)
26292 || value_dependent_expression_p (arg
));
26295 /* Returns true if ARGS (a collection of template arguments) contains
26296 any types that require structural equality testing. */
26299 any_template_arguments_need_structural_equality_p (tree args
)
26306 if (args
== error_mark_node
)
26309 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26311 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26312 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26314 tree arg
= TREE_VEC_ELT (level
, j
);
26315 tree packed_args
= NULL_TREE
;
26318 if (ARGUMENT_PACK_P (arg
))
26320 /* Look inside the argument pack. */
26321 packed_args
= ARGUMENT_PACK_ARGS (arg
);
26322 len
= TREE_VEC_LENGTH (packed_args
);
26325 for (k
= 0; k
< len
; ++k
)
26328 arg
= TREE_VEC_ELT (packed_args
, k
);
26330 if (error_operand_p (arg
))
26332 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26334 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
26336 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
26337 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26346 /* Returns true if ARGS (a collection of template arguments) contains
26347 any dependent arguments. */
26350 any_dependent_template_arguments_p (const_tree args
)
26357 if (args
== error_mark_node
)
26360 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26362 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26363 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26364 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26371 /* Returns true if ARGS contains any errors. */
26374 any_erroneous_template_args_p (const_tree args
)
26379 if (args
== error_mark_node
)
26382 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26384 if (tree ti
= get_template_info (args
))
26385 args
= TI_ARGS (ti
);
26393 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26395 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26396 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26397 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26404 /* Returns TRUE if the template TMPL is type-dependent. */
26407 dependent_template_p (tree tmpl
)
26409 if (TREE_CODE (tmpl
) == OVERLOAD
)
26411 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
26412 if (dependent_template_p (*iter
))
26417 /* Template template parameters are dependent. */
26418 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
26419 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
26421 /* So are names that have not been looked up. */
26422 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
26427 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26430 dependent_template_id_p (tree tmpl
, tree args
)
26432 return (dependent_template_p (tmpl
)
26433 || any_dependent_template_arguments_p (args
));
26436 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26440 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
26444 if (!processing_template_decl
)
26447 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
26449 tree decl
= TREE_VEC_ELT (declv
, i
);
26450 tree init
= TREE_VEC_ELT (initv
, i
);
26451 tree cond
= TREE_VEC_ELT (condv
, i
);
26452 tree incr
= TREE_VEC_ELT (incrv
, i
);
26454 if (type_dependent_expression_p (decl
)
26455 || TREE_CODE (decl
) == SCOPE_REF
)
26458 if (init
&& type_dependent_expression_p (init
))
26461 if (cond
== global_namespace
)
26464 if (type_dependent_expression_p (cond
))
26467 if (COMPARISON_CLASS_P (cond
)
26468 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
26469 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
26472 if (TREE_CODE (incr
) == MODOP_EXPR
)
26474 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
26475 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
26478 else if (type_dependent_expression_p (incr
))
26480 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
26482 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
26484 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
26486 tree t
= TREE_OPERAND (incr
, 1);
26487 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
26488 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
26491 /* If this loop has a class iterator with != comparison
26492 with increment other than i++/++i/i--/--i, make sure the
26493 increment is constant. */
26494 if (CLASS_TYPE_P (TREE_TYPE (decl
))
26495 && TREE_CODE (cond
) == NE_EXPR
)
26497 if (TREE_OPERAND (t
, 0) == decl
)
26498 t
= TREE_OPERAND (t
, 1);
26500 t
= TREE_OPERAND (t
, 0);
26501 if (TREE_CODE (t
) != INTEGER_CST
)
26511 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26512 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26513 no such TYPE can be found. Note that this function peers inside
26514 uninstantiated templates and therefore should be used only in
26515 extremely limited situations. ONLY_CURRENT_P restricts this
26516 peering to the currently open classes hierarchy (which is required
26517 when comparing types). */
26520 resolve_typename_type (tree type
, bool only_current_p
)
26529 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
26531 scope
= TYPE_CONTEXT (type
);
26532 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26533 gcc_checking_assert (uses_template_parms (scope
));
26535 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26536 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26537 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26538 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26539 identifier of the TYPENAME_TYPE anymore.
26540 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26541 TYPENAME_TYPE instead, we avoid messing up with a possible
26542 typedef variant case. */
26543 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
26545 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26546 it first before we can figure out what NAME refers to. */
26547 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
26549 if (TYPENAME_IS_RESOLVING_P (scope
))
26550 /* Given a class template A with a dependent base with nested type C,
26551 typedef typename A::C::C C will land us here, as trying to resolve
26552 the initial A::C leads to the local C typedef, which leads back to
26553 A::C::C. So we break the recursion now. */
26556 scope
= resolve_typename_type (scope
, only_current_p
);
26558 /* If we don't know what SCOPE refers to, then we cannot resolve the
26560 if (!CLASS_TYPE_P (scope
))
26562 /* If this is a typedef, we don't want to look inside (c++/11987). */
26563 if (typedef_variant_p (type
))
26565 /* If SCOPE isn't the template itself, it will not have a valid
26566 TYPE_FIELDS list. */
26567 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
26568 /* scope is either the template itself or a compatible instantiation
26569 like X<T>, so look up the name in the original template. */
26570 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
26571 /* If scope has no fields, it can't be a current instantiation. Check this
26572 before currently_open_class to avoid infinite recursion (71515). */
26573 if (!TYPE_FIELDS (scope
))
26575 /* If the SCOPE is not the current instantiation, there's no reason
26576 to look inside it. */
26577 if (only_current_p
&& !currently_open_class (scope
))
26579 /* Enter the SCOPE so that name lookup will be resolved as if we
26580 were in the class definition. In particular, SCOPE will no
26581 longer be considered a dependent type. */
26582 pushed_scope
= push_scope (scope
);
26583 /* Look up the declaration. */
26584 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
26585 tf_warning_or_error
);
26587 result
= NULL_TREE
;
26589 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26590 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26591 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
26594 else if (identifier_p (fullname
)
26595 && TREE_CODE (decl
) == TYPE_DECL
)
26597 result
= TREE_TYPE (decl
);
26598 if (result
== error_mark_node
)
26599 result
= NULL_TREE
;
26601 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
26602 && DECL_CLASS_TEMPLATE_P (decl
))
26604 /* Obtain the template and the arguments. */
26605 tree tmpl
= TREE_OPERAND (fullname
, 0);
26606 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
26608 /* We get here with a plain identifier because a previous tentative
26609 parse of the nested-name-specifier as part of a ptr-operator saw
26610 ::template X<A>. The use of ::template is necessary in a
26611 ptr-operator, but wrong in a declarator-id.
26613 [temp.names]: In a qualified-id of a declarator-id, the keyword
26614 template shall not appear at the top level. */
26615 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
26616 "keyword %<template%> not allowed in declarator-id");
26619 tree args
= TREE_OPERAND (fullname
, 1);
26620 /* Instantiate the template. */
26621 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
26622 /*entering_scope=*/true,
26623 tf_error
| tf_user
);
26624 if (result
== error_mark_node
)
26625 result
= NULL_TREE
;
26628 /* Leave the SCOPE. */
26630 pop_scope (pushed_scope
);
26632 /* If we failed to resolve it, return the original typename. */
26636 /* If lookup found a typename type, resolve that too. */
26637 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
26639 /* Ill-formed programs can cause infinite recursion here, so we
26640 must catch that. */
26641 TYPENAME_IS_RESOLVING_P (result
) = 1;
26642 result
= resolve_typename_type (result
, only_current_p
);
26643 TYPENAME_IS_RESOLVING_P (result
) = 0;
26646 /* Qualify the resulting type. */
26647 quals
= cp_type_quals (type
);
26649 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
26654 /* EXPR is an expression which is not type-dependent. Return a proxy
26655 for EXPR that can be used to compute the types of larger
26656 expressions containing EXPR. */
26659 build_non_dependent_expr (tree expr
)
26661 tree orig_expr
= expr
;
26664 /* When checking, try to get a constant value for all non-dependent
26665 expressions in order to expose bugs in *_dependent_expression_p
26666 and constexpr. This can affect code generation, see PR70704, so
26667 only do this for -fchecking=2. */
26668 if (flag_checking
> 1
26669 && cxx_dialect
>= cxx11
26670 /* Don't do this during nsdmi parsing as it can lead to
26671 unexpected recursive instantiations. */
26672 && !parsing_nsdmi ()
26673 /* Don't do this during concept expansion either and for
26674 the same reason. */
26675 && !expanding_concept ())
26676 fold_non_dependent_expr (expr
, tf_none
);
26678 STRIP_ANY_LOCATION_WRAPPER (expr
);
26680 /* Preserve OVERLOADs; the functions must be available to resolve
26683 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
26684 inner_expr
= stmt_expr_value_expr (inner_expr
);
26685 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
26686 inner_expr
= TREE_OPERAND (inner_expr
, 0);
26687 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
26688 inner_expr
= TREE_OPERAND (inner_expr
, 1);
26689 if (is_overloaded_fn (inner_expr
)
26690 || TREE_CODE (inner_expr
) == OFFSET_REF
)
26692 /* There is no need to return a proxy for a variable or enumerator. */
26693 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
26695 /* Preserve string constants; conversions from string constants to
26696 "char *" are allowed, even though normally a "const char *"
26697 cannot be used to initialize a "char *". */
26698 if (TREE_CODE (expr
) == STRING_CST
)
26700 /* Preserve void and arithmetic constants, as an optimization -- there is no
26701 reason to create a new node. */
26702 if (TREE_CODE (expr
) == VOID_CST
26703 || TREE_CODE (expr
) == INTEGER_CST
26704 || TREE_CODE (expr
) == REAL_CST
)
26706 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26707 There is at least one place where we want to know that a
26708 particular expression is a throw-expression: when checking a ?:
26709 expression, there are special rules if the second or third
26710 argument is a throw-expression. */
26711 if (TREE_CODE (expr
) == THROW_EXPR
)
26714 /* Don't wrap an initializer list, we need to be able to look inside. */
26715 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
26718 /* Don't wrap a dummy object, we need to be able to test for it. */
26719 if (is_dummy_object (expr
))
26722 if (TREE_CODE (expr
) == COND_EXPR
)
26723 return build3 (COND_EXPR
,
26725 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
26726 (TREE_OPERAND (expr
, 1)
26727 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
26728 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
26729 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
26730 if (TREE_CODE (expr
) == COMPOUND_EXPR
26731 && !COMPOUND_EXPR_OVERLOADED (expr
))
26732 return build2 (COMPOUND_EXPR
,
26734 TREE_OPERAND (expr
, 0),
26735 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
26737 /* If the type is unknown, it can't really be non-dependent */
26738 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
26740 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26741 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
26742 TREE_TYPE (expr
), expr
);
26745 /* ARGS is a vector of expressions as arguments to a function call.
26746 Replace the arguments with equivalent non-dependent expressions.
26747 This modifies ARGS in place. */
26750 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
26755 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
26757 tree newarg
= build_non_dependent_expr (arg
);
26759 (*args
)[ix
] = newarg
;
26763 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26764 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26765 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26768 make_auto_1 (tree name
, bool set_canonical
)
26770 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
26771 TYPE_NAME (au
) = build_decl (input_location
,
26772 TYPE_DECL
, name
, au
);
26773 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
26774 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
26775 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
26776 TYPE_NAME (au
), NULL_TREE
);
26778 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
26779 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
26780 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
26786 make_decltype_auto (void)
26788 return make_auto_1 (decltype_auto_identifier
, true);
26794 return make_auto_1 (auto_identifier
, true);
26797 /* Return a C++17 deduction placeholder for class template TMPL. */
26800 make_template_placeholder (tree tmpl
)
26802 tree t
= make_auto_1 (auto_identifier
, false);
26803 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
26804 /* Our canonical type depends on the placeholder. */
26805 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26809 /* True iff T is a C++17 class template deduction placeholder. */
26812 template_placeholder_p (tree t
)
26814 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
26817 /* Make a "constrained auto" type-specifier. This is an
26818 auto type with constraints that must be associated after
26819 deduction. The constraint is formed from the given
26820 CONC and its optional sequence of arguments, which are
26821 non-null if written as partial-concept-id. */
26824 make_constrained_auto (tree con
, tree args
)
26826 tree type
= make_auto_1 (auto_identifier
, false);
26828 /* Build the constraint. */
26829 tree tmpl
= DECL_TI_TEMPLATE (con
);
26830 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
26831 expr
= build_concept_check (expr
, type
, args
);
26833 tree constr
= normalize_expression (expr
);
26834 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
26836 /* Our canonical type depends on the constraint. */
26837 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
26839 /* Attach the constraint to the type declaration. */
26840 tree decl
= TYPE_NAME (type
);
26844 /* Given type ARG, return std::initializer_list<ARG>. */
26849 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
26851 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
26853 gcc_rich_location
richloc (input_location
);
26854 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
26855 error_at (&richloc
,
26856 "deducing from brace-enclosed initializer list"
26857 " requires %<#include <initializer_list>%>");
26859 return error_mark_node
;
26861 tree argvec
= make_tree_vec (1);
26862 TREE_VEC_ELT (argvec
, 0) = arg
;
26864 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
26865 NULL_TREE
, 0, tf_warning_or_error
);
26868 /* Replace auto in TYPE with std::initializer_list<auto>. */
26871 listify_autos (tree type
, tree auto_node
)
26873 tree init_auto
= listify (strip_top_quals (auto_node
));
26874 tree argvec
= make_tree_vec (1);
26875 TREE_VEC_ELT (argvec
, 0) = init_auto
;
26876 if (processing_template_decl
)
26877 argvec
= add_to_template_args (current_template_args (), argvec
);
26878 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
26881 /* Hash traits for hashing possibly constrained 'auto'
26882 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26884 struct auto_hash
: default_hash_traits
<tree
>
26886 static inline hashval_t
hash (tree
);
26887 static inline bool equal (tree
, tree
);
26890 /* Hash the 'auto' T. */
26893 auto_hash::hash (tree t
)
26895 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
26896 /* Matching constrained-type-specifiers denote the same template
26897 parameter, so hash the constraint. */
26898 return hash_placeholder_constraint (c
);
26900 /* But unconstrained autos are all separate, so just hash the pointer. */
26901 return iterative_hash_object (t
, 0);
26904 /* Compare two 'auto's. */
26907 auto_hash::equal (tree t1
, tree t2
)
26912 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
26913 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
26915 /* Two unconstrained autos are distinct. */
26919 return equivalent_placeholder_constraints (c1
, c2
);
26922 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26923 constrained) auto, add it to the vector. */
26926 extract_autos_r (tree t
, void *data
)
26928 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
26931 /* All the autos were built with index 0; fix that up now. */
26932 tree
*p
= hash
.find_slot (t
, INSERT
);
26935 /* If this is a repeated constrained-type-specifier, use the index we
26937 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
26940 /* Otherwise this is new, so use the current count. */
26942 idx
= hash
.elements () - 1;
26944 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
26947 /* Always keep walking. */
26951 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26952 says they can appear anywhere in the type. */
26955 extract_autos (tree type
)
26957 hash_set
<tree
> visited
;
26958 hash_table
<auto_hash
> hash (2);
26960 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
26962 tree tree_vec
= make_tree_vec (hash
.elements());
26963 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
26964 iter
!= hash
.end(); ++iter
)
26967 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
26968 TREE_VEC_ELT (tree_vec
, i
)
26969 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
26975 /* The stem for deduction guide names. */
26976 const char *const dguide_base
= "__dguide_";
26978 /* Return the name for a deduction guide for class template TMPL. */
26981 dguide_name (tree tmpl
)
26983 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
26984 tree tname
= TYPE_IDENTIFIER (type
);
26985 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
26986 + IDENTIFIER_LENGTH (tname
));
26987 memcpy (buf
, dguide_base
, strlen (dguide_base
));
26988 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
26989 IDENTIFIER_LENGTH (tname
) + 1);
26990 tree dname
= get_identifier (buf
);
26991 TREE_TYPE (dname
) = type
;
26995 /* True if NAME is the name of a deduction guide. */
26998 dguide_name_p (tree name
)
27000 return (TREE_CODE (name
) == IDENTIFIER_NODE
27001 && TREE_TYPE (name
)
27002 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27003 strlen (dguide_base
)));
27006 /* True if FN is a deduction guide. */
27009 deduction_guide_p (const_tree fn
)
27012 if (tree name
= DECL_NAME (fn
))
27013 return dguide_name_p (name
);
27017 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27020 copy_guide_p (const_tree fn
)
27022 gcc_assert (deduction_guide_p (fn
));
27023 if (!DECL_ARTIFICIAL (fn
))
27025 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27026 return (TREE_CHAIN (parms
) == void_list_node
27027 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27030 /* True if FN is a guide generated from a constructor template. */
27033 template_guide_p (const_tree fn
)
27035 gcc_assert (deduction_guide_p (fn
));
27036 if (!DECL_ARTIFICIAL (fn
))
27038 tree tmpl
= DECL_TI_TEMPLATE (fn
);
27039 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
27040 return PRIMARY_TEMPLATE_P (org
);
27044 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27045 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27046 template parameter types. Note that the handling of template template
27047 parameters relies on current_template_parms being set appropriately for the
27051 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
27052 tree tsubst_args
, tsubst_flags_t complain
)
27054 if (olddecl
== error_mark_node
)
27055 return error_mark_node
;
27057 tree oldidx
= get_template_parm_index (olddecl
);
27060 if (TREE_CODE (olddecl
) == TYPE_DECL
27061 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27063 tree oldtype
= TREE_TYPE (olddecl
);
27064 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27065 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27066 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27067 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27068 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27072 newtype
= TREE_TYPE (olddecl
);
27073 if (type_uses_auto (newtype
))
27075 // Substitute once to fix references to other template parameters.
27076 newtype
= tsubst (newtype
, tsubst_args
,
27077 complain
|tf_partial
, NULL_TREE
);
27078 // Now substitute again to reduce the level of the auto.
27079 newtype
= tsubst (newtype
, current_template_args (),
27080 complain
, NULL_TREE
);
27083 newtype
= tsubst (newtype
, tsubst_args
,
27084 complain
, NULL_TREE
);
27088 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27089 DECL_NAME (olddecl
), newtype
);
27090 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27093 if (TREE_CODE (olddecl
) == TYPE_DECL
27094 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27096 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27097 = build_template_parm_index (index
, level
, level
,
27099 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27100 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27101 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27102 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
27104 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27106 DECL_TEMPLATE_RESULT (newdecl
)
27107 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
27108 DECL_NAME (olddecl
), newtype
);
27109 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
27110 // First create a copy (ttargs) of tsubst_args with an
27111 // additional level for the template template parameter's own
27112 // template parameters (ttparms).
27113 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
27114 (DECL_TEMPLATE_PARMS (olddecl
)));
27115 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
27116 tree ttargs
= make_tree_vec (depth
+ 1);
27117 for (int i
= 0; i
< depth
; ++i
)
27118 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
27119 TREE_VEC_ELT (ttargs
, depth
)
27120 = template_parms_level_to_args (ttparms
);
27121 // Substitute ttargs into ttparms to fix references to
27122 // other template parameters.
27123 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27124 complain
|tf_partial
);
27125 // Now substitute again with args based on tparms, to reduce
27126 // the level of the ttparms.
27127 ttargs
= current_template_args ();
27128 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27130 // Finally, tack the adjusted parms onto tparms.
27131 ttparms
= tree_cons (size_int (depth
), ttparms
,
27132 current_template_parms
);
27133 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
27138 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
27140 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
27141 TREE_CODE (oldconst
),
27142 DECL_NAME (oldconst
), newtype
);
27143 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
27144 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
27145 SET_DECL_TEMPLATE_PARM_P (newconst
);
27146 newidx
= build_template_parm_index (index
, level
, level
,
27147 newconst
, newtype
);
27148 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27149 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27150 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
27156 /* Returns a C++17 class deduction guide template based on the constructor
27157 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27158 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27161 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
27163 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
27164 bool memtmpl
= false;
27167 tree fn_tmpl
= NULL_TREE
;
27172 bool copy_p
= TYPE_REF_P (type
);
27175 type
= TREE_TYPE (type
);
27176 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
27179 fparms
= void_list_node
;
27181 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
27182 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
27183 targs
= CLASSTYPE_TI_ARGS (type
);
27186 loc
= DECL_SOURCE_LOCATION (ctmpl
);
27187 explicit_p
= false;
27191 ++processing_template_decl
;
27195 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
27196 : DECL_TI_TEMPLATE (ctor
));
27198 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
27199 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
27201 type
= DECL_CONTEXT (ctor
);
27203 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
27204 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27205 fully specialized args for the enclosing class. Strip those off, as
27206 the deduction guide won't have those template parameters. */
27207 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
27208 TMPL_PARMS_DEPTH (tparms
));
27209 /* Discard the 'this' parameter. */
27210 fparms
= FUNCTION_ARG_CHAIN (ctor
);
27211 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
27212 ci
= get_constraints (ctor
);
27213 loc
= DECL_SOURCE_LOCATION (ctor
);
27214 explicit_p
= DECL_NONCONVERTING_P (ctor
);
27216 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
27220 /* For a member template constructor, we need to flatten the two
27221 template parameter lists into one, and then adjust the function
27222 signature accordingly. This gets...complicated. */
27223 tree save_parms
= current_template_parms
;
27225 /* For a member template we should have two levels of parms/args, one
27226 for the class and one for the constructor. We stripped
27227 specialized args for further enclosing classes above. */
27228 const int depth
= 2;
27229 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
27231 /* Template args for translating references to the two-level template
27232 parameters into references to the one-level template parameters we
27234 tree tsubst_args
= copy_node (targs
);
27235 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
27236 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
27238 /* Template parms for the constructor template. */
27239 tree ftparms
= TREE_VALUE (tparms
);
27240 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
27241 /* Template parms for the class template. */
27242 tparms
= TREE_CHAIN (tparms
);
27243 tree ctparms
= TREE_VALUE (tparms
);
27244 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
27245 /* Template parms for the deduction guide start as a copy of the
27246 template parms for the class. We set current_template_parms for
27247 lookup_template_class_1. */
27248 current_template_parms
= tparms
= copy_node (tparms
);
27249 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
27250 for (unsigned i
= 0; i
< clen
; ++i
)
27251 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
27253 /* Now we need to rewrite the constructor parms to append them to the
27255 for (unsigned i
= 0; i
< flen
; ++i
)
27257 unsigned index
= i
+ clen
;
27258 unsigned level
= 1;
27259 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
27260 tree olddecl
= TREE_VALUE (oldelt
);
27261 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27262 tsubst_args
, complain
);
27263 if (newdecl
== error_mark_node
)
27265 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27266 tsubst_args
, complain
, ctor
);
27267 tree list
= build_tree_list (newdef
, newdecl
);
27268 TEMPLATE_PARM_CONSTRAINTS (list
)
27269 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27270 tsubst_args
, complain
, ctor
);
27271 TREE_VEC_ELT (new_vec
, index
) = list
;
27272 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
27275 /* Now we have a final set of template parms to substitute into the
27276 function signature. */
27277 targs
= template_parms_to_args (tparms
);
27278 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
27280 if (fparms
== error_mark_node
)
27282 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
27284 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
27286 current_template_parms
= save_parms
;
27289 --processing_template_decl
;
27291 return error_mark_node
;
27296 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27297 tparms
= copy_node (tparms
);
27298 INNERMOST_TEMPLATE_PARMS (tparms
)
27299 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
27302 tree fntype
= build_function_type (type
, fparms
);
27303 tree ded_fn
= build_lang_decl_loc (loc
,
27305 dguide_name (type
), fntype
);
27306 DECL_ARGUMENTS (ded_fn
) = fargs
;
27307 DECL_ARTIFICIAL (ded_fn
) = true;
27308 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
27309 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
27310 DECL_ARTIFICIAL (ded_tmpl
) = true;
27311 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
27312 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
27313 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
27314 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
27316 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
27318 set_constraints (ded_tmpl
, ci
);
27323 /* Deduce template arguments for the class template placeholder PTYPE for
27324 template TMPL based on the initializer INIT, and return the resulting
27328 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
27329 tsubst_flags_t complain
)
27331 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
27333 /* We should have handled this in the caller. */
27334 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27336 if (complain
& tf_error
)
27337 error ("non-class template %qT used without template arguments", tmpl
);
27338 return error_mark_node
;
27340 if (init
&& TREE_TYPE (init
) == ptype
)
27341 /* Using the template parm as its own argument. */
27344 tree type
= TREE_TYPE (tmpl
);
27346 bool try_list_ctor
= false;
27348 releasing_vec rv_args
= NULL
;
27349 vec
<tree
,va_gc
> *&args
= *&rv_args
;
27350 if (init
== NULL_TREE
27351 || TREE_CODE (init
) == TREE_LIST
)
27352 args
= make_tree_vector_from_list (init
);
27353 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27355 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
27356 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
27358 /* As an exception, the first phase in 16.3.1.7 (considering the
27359 initializer list as a single argument) is omitted if the
27360 initializer list consists of a single expression of type cv U,
27361 where U is a specialization of C or a class derived from a
27362 specialization of C. */
27363 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
27364 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
27366 tree etype
= TREE_TYPE (elt
);
27367 tree tparms
= (INNERMOST_TEMPLATE_PARMS
27368 (DECL_TEMPLATE_PARMS (tmpl
)));
27369 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27370 int err
= unify (tparms
, targs
, type
, etype
,
27371 UNIFY_ALLOW_DERIVED
, /*explain*/false);
27373 try_list_ctor
= false;
27377 if (try_list_ctor
|| is_std_init_list (type
))
27378 args
= make_tree_vector_single (init
);
27380 args
= make_tree_vector_from_ctor (init
);
27383 args
= make_tree_vector_single (init
);
27385 tree dname
= dguide_name (tmpl
);
27386 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
27387 /*type*/false, /*complain*/false,
27389 bool elided
= false;
27390 if (cands
== error_mark_node
)
27393 /* Prune explicit deduction guides in copy-initialization context. */
27394 if (flags
& LOOKUP_ONLYCONVERTING
)
27396 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
27397 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27402 /* Found a nonconverting guide, prune the candidates. */
27403 tree pruned
= NULL_TREE
;
27404 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27405 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27406 pruned
= lookup_add (*iter
, pruned
);
27412 tree outer_args
= NULL_TREE
;
27413 if (DECL_CLASS_SCOPE_P (tmpl
)
27414 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
27416 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
27417 type
= TREE_TYPE (most_general_template (tmpl
));
27420 bool saw_ctor
= false;
27421 // FIXME cache artificial deduction guides
27422 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
27424 /* Skip inherited constructors. */
27425 if (iter
.using_p ())
27428 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
27429 if (guide
== error_mark_node
)
27430 return error_mark_node
;
27431 if ((flags
& LOOKUP_ONLYCONVERTING
)
27432 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
27435 cands
= lookup_add (guide
, cands
);
27440 tree call
= error_mark_node
;
27442 /* If this is list-initialization and the class has a list constructor, first
27443 try deducing from the list as a single argument, as [over.match.list]. */
27444 tree list_cands
= NULL_TREE
;
27445 if (try_list_ctor
&& cands
)
27446 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27449 if (is_list_ctor (dg
))
27450 list_cands
= lookup_add (dg
, list_cands
);
27454 ++cp_unevaluated_operand
;
27455 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
27456 --cp_unevaluated_operand
;
27458 if (call
== error_mark_node
)
27460 /* That didn't work, now try treating the list as a sequence of
27462 release_tree_vector (args
);
27463 args
= make_tree_vector_from_ctor (init
);
27467 /* Maybe generate an implicit deduction guide. */
27468 if (call
== error_mark_node
&& args
->length () < 2)
27470 tree gtype
= NULL_TREE
;
27472 if (args
->length () == 1)
27473 /* Generate a copy guide. */
27474 gtype
= build_reference_type (type
);
27475 else if (!saw_ctor
)
27476 /* Generate a default guide. */
27481 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
27482 if (guide
== error_mark_node
)
27483 return error_mark_node
;
27484 cands
= lookup_add (guide
, cands
);
27488 if (elided
&& !cands
)
27490 error ("cannot deduce template arguments for copy-initialization"
27491 " of %qT, as it has no non-explicit deduction guides or "
27492 "user-declared constructors", type
);
27493 return error_mark_node
;
27495 else if (!cands
&& call
== error_mark_node
)
27497 error ("cannot deduce template arguments of %qT, as it has no viable "
27498 "deduction guides", type
);
27499 return error_mark_node
;
27502 if (call
== error_mark_node
)
27504 ++cp_unevaluated_operand
;
27505 call
= build_new_function_call (cands
, &args
, tf_decltype
);
27506 --cp_unevaluated_operand
;
27509 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
27511 error ("class template argument deduction failed:");
27513 ++cp_unevaluated_operand
;
27514 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
27515 --cp_unevaluated_operand
;
27518 inform (input_location
, "explicit deduction guides not considered "
27519 "for copy-initialization");
27522 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
27525 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27526 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27527 The CONTEXT determines the context in which auto deduction is performed
27528 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27529 OUTER_TARGS are used during template argument deduction
27530 (context == adc_unify) to properly substitute the result, and is ignored
27533 For partial-concept-ids, extra args may be appended to the list of deduced
27534 template arguments prior to determining constraint satisfaction. */
27537 do_auto_deduction (tree type
, tree init
, tree auto_node
,
27538 tsubst_flags_t complain
, auto_deduction_context context
,
27539 tree outer_targs
, int flags
)
27543 if (init
== error_mark_node
)
27544 return error_mark_node
;
27546 if (init
&& type_dependent_expression_p (init
)
27547 && context
!= adc_unify
)
27548 /* Defining a subset of type-dependent expressions that we can deduce
27549 from ahead of time isn't worth the trouble. */
27552 /* Similarly, we can't deduce from another undeduced decl. */
27553 if (init
&& undeduced_auto_decl (init
))
27556 /* We may be doing a partial substitution, but we still want to replace
27558 complain
&= ~tf_partial
;
27560 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
27561 /* C++17 class template argument deduction. */
27562 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
27564 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
27565 /* Nothing we can do with this, even in deduction context. */
27568 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27569 with either a new invented type template parameter U or, if the
27570 initializer is a braced-init-list (8.5.4), with
27571 std::initializer_list<U>. */
27572 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27574 if (!DIRECT_LIST_INIT_P (init
))
27575 type
= listify_autos (type
, auto_node
);
27576 else if (CONSTRUCTOR_NELTS (init
) == 1)
27577 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
27580 if (complain
& tf_warning_or_error
)
27582 if (permerror (input_location
, "direct-list-initialization of "
27583 "%<auto%> requires exactly one element"))
27584 inform (input_location
,
27585 "for deduction to %<std::initializer_list%>, use copy-"
27586 "list-initialization (i.e. add %<=%> before the %<{%>)");
27588 type
= listify_autos (type
, auto_node
);
27592 if (type
== error_mark_node
)
27593 return error_mark_node
;
27595 init
= resolve_nondeduced_context (init
, complain
);
27597 if (context
== adc_decomp_type
27598 && auto_node
== type
27599 && init
!= error_mark_node
27600 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
27601 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27602 and initializer has array type, deduce cv-qualified array type. */
27603 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
27605 else if (AUTO_IS_DECLTYPE (auto_node
))
27607 tree stripped_init
= tree_strip_any_location_wrapper (init
);
27608 bool id
= (DECL_P (stripped_init
)
27609 || ((TREE_CODE (init
) == COMPONENT_REF
27610 || TREE_CODE (init
) == SCOPE_REF
)
27611 && !REF_PARENTHESIZED_P (init
)));
27612 targs
= make_tree_vec (1);
27613 TREE_VEC_ELT (targs
, 0)
27614 = finish_decltype_type (init
, id
, tf_warning_or_error
);
27615 if (type
!= auto_node
)
27617 if (complain
& tf_error
)
27618 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
27619 return error_mark_node
;
27624 if (error_operand_p (init
))
27625 return error_mark_node
;
27627 tree parms
= build_tree_list (NULL_TREE
, type
);
27631 tparms
= extract_autos (type
);
27634 tparms
= make_tree_vec (1);
27635 TREE_VEC_ELT (tparms
, 0)
27636 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
27639 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27640 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27642 NULL
, /*explain_p=*/false);
27645 if (processing_template_decl
)
27646 /* Try again at instantiation time. */
27648 if (type
&& type
!= error_mark_node
27649 && (complain
& tf_error
))
27650 /* If type is error_mark_node a diagnostic must have been
27651 emitted by now. Also, having a mention to '<type error>'
27652 in the diagnostic is not really useful to the user. */
27655 && FNDECL_USED_AUTO (current_function_decl
)
27657 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
27658 && LAMBDA_FUNCTION_P (current_function_decl
))
27659 error ("unable to deduce lambda return type from %qE", init
);
27661 error ("unable to deduce %qT from %qE", type
, init
);
27662 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27664 NULL
, /*explain_p=*/true);
27666 return error_mark_node
;
27670 /* Check any placeholder constraints against the deduced type. */
27671 if (flag_concepts
&& !processing_template_decl
)
27672 if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
27674 /* Use the deduced type to check the associated constraints. If we
27675 have a partial-concept-id, rebuild the argument list so that
27676 we check using the extra arguments. */
27677 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
27678 tree cargs
= CHECK_CONSTR_ARGS (constr
);
27679 if (TREE_VEC_LENGTH (cargs
) > 1)
27681 cargs
= copy_node (cargs
);
27682 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
27686 if (!constraints_satisfied_p (constr
, cargs
))
27688 if (complain
& tf_warning_or_error
)
27690 auto_diagnostic_group d
;
27693 case adc_unspecified
:
27695 error("placeholder constraints not satisfied");
27697 case adc_variable_type
:
27698 case adc_decomp_type
:
27699 error ("deduced initializer does not satisfy "
27700 "placeholder constraints");
27702 case adc_return_type
:
27703 error ("deduced return type does not satisfy "
27704 "placeholder constraints");
27706 case adc_requirement
:
27707 error ("deduced expression type does not satisfy "
27708 "placeholder constraints");
27711 diagnose_constraints (input_location
, constr
, targs
);
27713 return error_mark_node
;
27717 if (processing_template_decl
&& context
!= adc_unify
)
27718 outer_targs
= current_template_args ();
27719 targs
= add_to_template_args (outer_targs
, targs
);
27720 return tsubst (type
, targs
, complain
, NULL_TREE
);
27723 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27727 splice_late_return_type (tree type
, tree late_return_type
)
27729 if (is_auto (type
))
27731 if (late_return_type
)
27732 return late_return_type
;
27734 tree idx
= get_template_parm_index (type
);
27735 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
27736 /* In an abbreviated function template we didn't know we were dealing
27737 with a function template when we saw the auto return type, so update
27738 it to have the correct level. */
27739 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
27744 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27745 'decltype(auto)' or a deduced class template. */
27748 is_auto (const_tree type
)
27750 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27751 && (TYPE_IDENTIFIER (type
) == auto_identifier
27752 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
27758 /* for_each_template_parm callback for type_uses_auto. */
27761 is_auto_r (tree tp
, void */
*data*/
)
27763 return is_auto (tp
);
27766 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27767 a use of `auto'. Returns NULL_TREE otherwise. */
27770 type_uses_auto (tree type
)
27772 if (type
== NULL_TREE
)
27774 else if (flag_concepts
)
27776 /* The Concepts TS allows multiple autos in one type-specifier; just
27777 return the first one we find, do_auto_deduction will collect all of
27779 if (uses_template_parms (type
))
27780 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
27781 /*visited*/NULL
, /*nondeduced*/false);
27786 return find_type_usage (type
, is_auto
);
27789 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27790 concepts are enabled, auto is acceptable in template arguments, but
27791 only when TEMPL identifies a template class. Return TRUE if any
27792 such errors were reported. */
27795 check_auto_in_tmpl_args (tree tmpl
, tree args
)
27797 /* If there were previous errors, nevermind. */
27798 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
27801 /* If TMPL is an identifier, we're parsing and we can't tell yet
27802 whether TMPL is supposed to be a type, a function or a variable.
27803 We'll only be able to tell during template substitution, so we
27804 expect to be called again then. If concepts are enabled and we
27805 know we have a type, we're ok. */
27807 && (identifier_p (tmpl
)
27809 && (DECL_TYPE_TEMPLATE_P (tmpl
)
27810 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
27813 /* Quickly search for any occurrences of auto; usually there won't
27814 be any, and then we'll avoid allocating the vector. */
27815 if (!type_uses_auto (args
))
27818 bool errors
= false;
27820 tree vec
= extract_autos (args
);
27821 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
27823 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
27824 error_at (DECL_SOURCE_LOCATION (xauto
),
27825 "invalid use of %qT in template argument", xauto
);
27832 /* For a given template T, return the vector of typedefs referenced
27833 in T for which access check is needed at T instantiation time.
27834 T is either a FUNCTION_DECL or a RECORD_TYPE.
27835 Those typedefs were added to T by the function
27836 append_type_to_template_for_access_check. */
27838 vec
<qualified_typedef_usage_t
, va_gc
> *
27839 get_types_needing_access_check (tree t
)
27842 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
27844 if (!t
|| t
== error_mark_node
)
27847 if (!(ti
= get_template_info (t
)))
27850 if (CLASS_TYPE_P (t
)
27851 || TREE_CODE (t
) == FUNCTION_DECL
)
27853 if (!TI_TEMPLATE (ti
))
27856 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
27862 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27863 tied to T. That list of typedefs will be access checked at
27864 T instantiation time.
27865 T is either a FUNCTION_DECL or a RECORD_TYPE.
27866 TYPE_DECL is a TYPE_DECL node representing a typedef.
27867 SCOPE is the scope through which TYPE_DECL is accessed.
27868 LOCATION is the location of the usage point of TYPE_DECL.
27870 This function is a subroutine of
27871 append_type_to_template_for_access_check. */
27874 append_type_to_template_for_access_check_1 (tree t
,
27877 location_t location
)
27879 qualified_typedef_usage_t typedef_usage
;
27882 if (!t
|| t
== error_mark_node
)
27885 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
27886 || CLASS_TYPE_P (t
))
27888 && TREE_CODE (type_decl
) == TYPE_DECL
27891 if (!(ti
= get_template_info (t
)))
27894 gcc_assert (TI_TEMPLATE (ti
));
27896 typedef_usage
.typedef_decl
= type_decl
;
27897 typedef_usage
.context
= scope
;
27898 typedef_usage
.locus
= location
;
27900 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
27903 /* Append TYPE_DECL to the template TEMPL.
27904 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27905 At TEMPL instanciation time, TYPE_DECL will be checked to see
27906 if it can be accessed through SCOPE.
27907 LOCATION is the location of the usage point of TYPE_DECL.
27909 e.g. consider the following code snippet:
27916 template<class U> struct S
27918 C::myint mi; // <-- usage point of the typedef C::myint
27923 At S<char> instantiation time, we need to check the access of C::myint
27924 In other words, we need to check the access of the myint typedef through
27925 the C scope. For that purpose, this function will add the myint typedef
27926 and the scope C through which its being accessed to a list of typedefs
27927 tied to the template S. That list will be walked at template instantiation
27928 time and access check performed on each typedefs it contains.
27929 Note that this particular code snippet should yield an error because
27930 myint is private to C. */
27933 append_type_to_template_for_access_check (tree templ
,
27936 location_t location
)
27938 qualified_typedef_usage_t
*iter
;
27941 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
27943 /* Make sure we don't append the type to the template twice. */
27944 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
27945 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
27948 append_type_to_template_for_access_check_1 (templ
, type_decl
,
27952 /* Convert the generic type parameters in PARM that match the types given in the
27953 range [START_IDX, END_IDX) from the current_template_parms into generic type
27957 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
27959 tree current
= current_template_parms
;
27960 int depth
= TMPL_PARMS_DEPTH (current
);
27961 current
= INNERMOST_TEMPLATE_PARMS (current
);
27962 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
27964 for (int i
= 0; i
< start_idx
; ++i
)
27965 TREE_VEC_ELT (replacement
, i
)
27966 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27968 for (int i
= start_idx
; i
< end_idx
; ++i
)
27970 /* Create a distinct parameter pack type from the current parm and add it
27971 to the replacement args to tsubst below into the generic function
27974 tree o
= TREE_TYPE (TREE_VALUE
27975 (TREE_VEC_ELT (current
, i
)));
27976 tree t
= copy_type (o
);
27977 TEMPLATE_TYPE_PARM_INDEX (t
)
27978 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
27980 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
27981 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
27982 TYPE_MAIN_VARIANT (t
) = t
;
27983 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
27984 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27985 TREE_VEC_ELT (replacement
, i
) = t
;
27986 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
27989 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
27990 TREE_VEC_ELT (replacement
, i
)
27991 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27993 /* If there are more levels then build up the replacement with the outer
27996 replacement
= add_to_template_args (template_parms_to_args
27997 (TREE_CHAIN (current_template_parms
)),
28000 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
28003 /* Entries in the decl_constraint hash table. */
28004 struct GTY((for_user
)) constr_entry
28010 /* Hashing function and equality for constraint entries. */
28011 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
28013 static hashval_t
hash (constr_entry
*e
)
28015 return (hashval_t
)DECL_UID (e
->decl
);
28018 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
28020 return e1
->decl
== e2
->decl
;
28024 /* A mapping from declarations to constraint information. Note that
28025 both templates and their underlying declarations are mapped to the
28026 same constraint information.
28028 FIXME: This is defined in pt.c because garbage collection
28029 code is not being generated for constraint.cc. */
28031 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
28033 /* Returns the template constraints of declaration T. If T is not
28034 constrained, return NULL_TREE. Note that T must be non-null. */
28037 get_constraints (tree t
)
28039 if (!flag_concepts
)
28042 gcc_assert (DECL_P (t
));
28043 if (TREE_CODE (t
) == TEMPLATE_DECL
)
28044 t
= DECL_TEMPLATE_RESULT (t
);
28045 constr_entry elt
= { t
, NULL_TREE
};
28046 constr_entry
* found
= decl_constraints
->find (&elt
);
28053 /* Associate the given constraint information CI with the declaration
28054 T. If T is a template, then the constraints are associated with
28055 its underlying declaration. Don't build associations if CI is
28059 set_constraints (tree t
, tree ci
)
28063 gcc_assert (t
&& flag_concepts
);
28064 if (TREE_CODE (t
) == TEMPLATE_DECL
)
28065 t
= DECL_TEMPLATE_RESULT (t
);
28066 gcc_assert (!get_constraints (t
));
28067 constr_entry elt
= {t
, ci
};
28068 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
28069 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
28074 /* Remove the associated constraints of the declaration T. */
28077 remove_constraints (tree t
)
28079 gcc_assert (DECL_P (t
));
28080 if (TREE_CODE (t
) == TEMPLATE_DECL
)
28081 t
= DECL_TEMPLATE_RESULT (t
);
28083 constr_entry elt
= {t
, NULL_TREE
};
28084 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
28086 decl_constraints
->clear_slot (slot
);
28089 /* Memoized satisfaction results for declarations. This
28090 maps the pair (constraint_info, arguments) to the result computed
28091 by constraints_satisfied_p. */
28093 struct GTY((for_user
)) constraint_sat_entry
28100 /* Hashing function and equality for constraint entries. */
28102 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
28104 static hashval_t
hash (constraint_sat_entry
*e
)
28106 hashval_t val
= iterative_hash_object(e
->ci
, 0);
28107 return iterative_hash_template_arg (e
->args
, val
);
28110 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
28112 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
28116 /* Memoized satisfaction results for concept checks. */
28118 struct GTY((for_user
)) concept_spec_entry
28125 /* Hashing function and equality for constraint entries. */
28127 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
28129 static hashval_t
hash (concept_spec_entry
*e
)
28131 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
28134 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
28136 ++comparing_specializations
;
28137 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
28138 --comparing_specializations
;
28143 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
28144 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
28146 /* Search for a memoized satisfaction result. Returns one of the
28147 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28150 lookup_constraint_satisfaction (tree ci
, tree args
)
28152 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
28153 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
28155 return found
->result
;
28160 /* Memoize the result of a satisfication test. Returns the saved result. */
28163 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
28165 constraint_sat_entry elt
= {ci
, args
, result
};
28166 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
28167 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
28173 /* Search for a memoized satisfaction result for a concept. */
28176 lookup_concept_satisfaction (tree tmpl
, tree args
)
28178 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
28179 concept_spec_entry
* found
= concept_memos
->find (&elt
);
28181 return found
->result
;
28186 /* Memoize the result of a concept check. Returns the saved result. */
28189 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
28191 concept_spec_entry elt
= {tmpl
, args
, result
};
28192 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
28193 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
28199 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
28201 /* Returns a prior concept specialization. This returns the substituted
28202 and normalized constraints defined by the concept. */
28205 get_concept_expansion (tree tmpl
, tree args
)
28207 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
28208 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
28210 return found
->result
;
28215 /* Save a concept expansion for later. */
28218 save_concept_expansion (tree tmpl
, tree args
, tree def
)
28220 concept_spec_entry elt
= {tmpl
, args
, def
};
28221 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
28222 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
28229 hash_subsumption_args (tree t1
, tree t2
)
28231 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
28232 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
28234 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
28235 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
28236 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
28237 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
28241 /* Compare the constraints of two subsumption entries. The LEFT1 and
28242 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28243 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28246 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
28248 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
28249 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
28250 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
28251 CHECK_CONSTR_ARGS (right1
)))
28252 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
28253 CHECK_CONSTR_ARGS (right2
));
28257 /* Key/value pair for learning and memoizing subsumption results. This
28258 associates a pair of check constraints (including arguments) with
28259 a boolean value indicating the result. */
28261 struct GTY((for_user
)) subsumption_entry
28268 /* Hashing function and equality for constraint entries. */
28270 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
28272 static hashval_t
hash (subsumption_entry
*e
)
28274 return hash_subsumption_args (e
->t1
, e
->t2
);
28277 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
28279 ++comparing_specializations
;
28280 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
28281 --comparing_specializations
;
28286 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
28288 /* Search for a previously cached subsumption result. */
28291 lookup_subsumption_result (tree t1
, tree t2
)
28293 subsumption_entry elt
= { t1
, t2
, false };
28294 subsumption_entry
* found
= subsumption_table
->find (&elt
);
28296 return &found
->result
;
28301 /* Save a subsumption result. */
28304 save_subsumption_result (tree t1
, tree t2
, bool result
)
28306 subsumption_entry elt
= {t1
, t2
, result
};
28307 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
28308 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
28314 /* Set up the hash table for constraint association. */
28317 init_constraint_processing (void)
28319 if (!flag_concepts
)
28322 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
28323 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
28324 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
28325 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
28326 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
28329 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28333 declare_integer_pack (void)
28335 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
28336 build_function_type_list (integer_type_node
,
28339 NULL_TREE
, ECF_CONST
);
28340 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
28341 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
28342 CP_BUILT_IN_INTEGER_PACK
);
28345 /* Set up the hash tables for template instantiations. */
28348 init_template_processing (void)
28350 /* FIXME: enable sanitization (PR87847) */
28351 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
28352 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
28354 if (cxx_dialect
>= cxx11
)
28355 declare_integer_pack ();
28358 /* Print stats about the template hash tables for -fstats. */
28361 print_template_statistics (void)
28363 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
28364 "%f collisions\n", (long) decl_specializations
->size (),
28365 (long) decl_specializations
->elements (),
28366 decl_specializations
->collisions ());
28367 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
28368 "%f collisions\n", (long) type_specializations
->size (),
28369 (long) type_specializations
->elements (),
28370 type_specializations
->collisions ());
28375 namespace selftest
{
28377 /* Verify that build_non_dependent_expr () works, for various expressions,
28378 and that location wrappers don't affect the results. */
28381 test_build_non_dependent_expr ()
28383 location_t loc
= BUILTINS_LOCATION
;
28385 /* Verify constants, without and with location wrappers. */
28386 tree int_cst
= build_int_cst (integer_type_node
, 42);
28387 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
28389 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
28390 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
28391 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
28393 tree string_lit
= build_string (4, "foo");
28394 TREE_TYPE (string_lit
) = char_array_type_node
;
28395 string_lit
= fix_string_type (string_lit
);
28396 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
28398 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
28399 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
28400 ASSERT_EQ (wrapped_string_lit
,
28401 build_non_dependent_expr (wrapped_string_lit
));
28404 /* Verify that type_dependent_expression_p () works correctly, even
28405 in the presence of location wrapper nodes. */
28408 test_type_dependent_expression_p ()
28410 location_t loc
= BUILTINS_LOCATION
;
28412 tree name
= get_identifier ("foo");
28414 /* If no templates are involved, nothing is type-dependent. */
28415 gcc_assert (!processing_template_decl
);
28416 ASSERT_FALSE (type_dependent_expression_p (name
));
28418 ++processing_template_decl
;
28420 /* Within a template, an unresolved name is always type-dependent. */
28421 ASSERT_TRUE (type_dependent_expression_p (name
));
28423 /* Ensure it copes with NULL_TREE and errors. */
28424 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
28425 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
28427 /* A USING_DECL in a template should be type-dependent, even if wrapped
28428 with a location wrapper (PR c++/83799). */
28429 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
28430 TREE_TYPE (using_decl
) = integer_type_node
;
28431 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
28432 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
28433 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
28434 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
28436 --processing_template_decl
;
28439 /* Run all of the selftests within this file. */
28444 test_build_non_dependent_expr ();
28445 test_type_dependent_expression_p ();
28448 } // namespace selftest
28450 #endif /* #if CHECKING_P */
28452 #include "gt-cp-pt.h"