1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 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"
45 /* The type of functions taking a tree, and some additional data, and
47 typedef int (*tree_fn_t
) (tree
, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template
{
53 struct pending_template
*next
;
54 struct tinst_level
*tinst
;
57 static GTY(()) struct pending_template
*pending_templates
;
58 static GTY(()) struct pending_template
*last_pending_template
;
60 int processing_template_parmlist
;
61 static int template_header_count
;
63 static GTY(()) tree saved_trees
;
64 static vec
<int> inline_parm_levels
;
66 static GTY(()) struct tinst_level
*current_tinst_level
;
68 static GTY(()) tree saved_access_scope
;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr
;
75 // -------------------------------------------------------------------------- //
76 // Local Specialization Stack
78 // Implementation of the RAII helper for creating new local
80 local_specialization_stack::local_specialization_stack (lss_policy policy
)
81 : saved (local_specializations
)
83 if (policy
== lss_blank
|| !saved
)
84 local_specializations
= new hash_map
<tree
, tree
>;
86 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
89 local_specialization_stack::~local_specialization_stack ()
91 delete local_specializations
;
92 local_specializations
= saved
;
95 /* True if we've recursed into fn_type_unification too many times. */
96 static bool excessive_deduction_depth
;
98 struct GTY((for_user
)) spec_entry
105 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
107 static hashval_t
hash (spec_entry
*);
108 static bool equal (spec_entry
*, spec_entry
*);
111 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
113 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
115 /* Contains canonical template parameter types. The vector is indexed by
116 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
117 TREE_LIST, whose TREE_VALUEs contain the canonical template
118 parameters of various types and levels. */
119 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
121 #define UNIFY_ALLOW_NONE 0
122 #define UNIFY_ALLOW_MORE_CV_QUAL 1
123 #define UNIFY_ALLOW_LESS_CV_QUAL 2
124 #define UNIFY_ALLOW_DERIVED 4
125 #define UNIFY_ALLOW_INTEGER 8
126 #define UNIFY_ALLOW_OUTER_LEVEL 16
127 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
128 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130 enum template_base_result
{
132 tbr_ambiguous_baseclass
,
136 static void push_access_scope (tree
);
137 static void pop_access_scope (tree
);
138 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
139 unification_kind_t
, int,
141 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
142 unification_kind_t
, int, bool, bool);
143 static int unify (tree
, tree
, tree
, tree
, int, bool);
144 static void add_pending_template (tree
);
145 static tree
reopen_tinst_level (struct tinst_level
*);
146 static tree
tsubst_initializer_list (tree
, tree
);
147 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
148 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
150 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static void tsubst_enum (tree
, tree
, tree
);
153 static tree
add_to_template_args (tree
, tree
);
154 static tree
add_outermost_template_args (tree
, tree
);
155 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
156 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
158 static int type_unification_real (tree
, tree
, tree
, const tree
*,
159 unsigned int, int, unification_kind_t
, int,
160 vec
<deferred_access_check
, va_gc
> **,
162 static void note_template_header (int);
163 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
164 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_template_argument (tree
, tree
, tree
,
166 tsubst_flags_t
, int, tree
);
167 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
168 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
169 static tree
expand_template_argument_pack (tree
);
170 static tree
build_template_parm_index (int, int, int, tree
, tree
);
171 static bool inline_needs_template_parms (tree
, bool);
172 static void push_inline_template_parms_recursive (tree
, int);
173 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
174 static int mark_template_parm (tree
, void *);
175 static int template_parm_this_level_p (tree
, void *);
176 static tree
tsubst_friend_function (tree
, tree
);
177 static tree
tsubst_friend_class (tree
, tree
);
178 static int can_complete_type_without_circularity (tree
);
179 static tree
get_bindings (tree
, tree
, tree
, bool);
180 static int template_decl_level (tree
);
181 static int check_cv_quals_for_unify (int, tree
, tree
);
182 static void template_parm_level_and_index (tree
, int*, int*);
183 static int unify_pack_expansion (tree
, tree
, tree
,
184 tree
, unification_kind_t
, bool, bool);
185 static tree
copy_template_args (tree
);
186 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
187 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
189 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
190 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
191 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
192 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
193 static bool check_specialization_scope (void);
194 static tree
process_partial_specialization (tree
);
195 static void set_current_access_from_decl (tree
);
196 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
198 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
199 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
201 static bool template_template_parm_bindings_ok_p (tree
, tree
);
202 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
203 static tree
for_each_template_parm_r (tree
*, int *, void *);
204 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
205 static void copy_default_args_to_explicit_spec (tree
);
206 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
207 static bool dependent_template_arg_p (tree
);
208 static bool any_template_arguments_need_structural_equality_p (tree
);
209 static bool dependent_type_p_r (tree
);
210 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
211 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
212 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
213 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
215 static tree
listify (tree
);
216 static tree
listify_autos (tree
, tree
);
217 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
218 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
219 static bool complex_alias_template_p (const_tree tmpl
);
220 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
221 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
222 static tree
make_argument_pack (tree
);
223 static void register_parameter_specializations (tree
, tree
);
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
231 push_access_scope (tree t
)
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
234 || TREE_CODE (t
) == TYPE_DECL
);
236 if (DECL_FRIEND_CONTEXT (t
))
237 push_nested_class (DECL_FRIEND_CONTEXT (t
));
238 else if (DECL_CLASS_SCOPE_P (t
))
239 push_nested_class (DECL_CONTEXT (t
));
241 push_to_top_level ();
243 if (TREE_CODE (t
) == FUNCTION_DECL
)
245 saved_access_scope
= tree_cons
246 (NULL_TREE
, current_function_decl
, saved_access_scope
);
247 current_function_decl
= t
;
251 /* Restore the scope set up by push_access_scope. T is the node we
255 pop_access_scope (tree t
)
257 if (TREE_CODE (t
) == FUNCTION_DECL
)
259 current_function_decl
= TREE_VALUE (saved_access_scope
);
260 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
263 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
266 pop_from_top_level ();
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
275 finish_member_template_decl (tree decl
)
277 if (decl
== error_mark_node
)
278 return error_mark_node
;
280 gcc_assert (DECL_P (decl
));
282 if (TREE_CODE (decl
) == TYPE_DECL
)
286 type
= TREE_TYPE (decl
);
287 if (type
== error_mark_node
)
288 return error_mark_node
;
289 if (MAYBE_CLASS_TYPE_P (type
)
290 && CLASSTYPE_TEMPLATE_INFO (type
)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
293 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
294 check_member_template (tmpl
);
299 else if (TREE_CODE (decl
) == FIELD_DECL
)
300 error ("data member %qD cannot be a member template", decl
);
301 else if (DECL_TEMPLATE_INFO (decl
))
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
305 check_member_template (DECL_TI_TEMPLATE (decl
));
306 return DECL_TI_TEMPLATE (decl
);
312 error ("invalid member template declaration %qD", decl
);
314 return error_mark_node
;
317 /* Create a template info node. */
320 build_template_info (tree template_decl
, tree template_args
)
322 tree result
= make_node (TEMPLATE_INFO
);
323 TI_TEMPLATE (result
) = template_decl
;
324 TI_ARGS (result
) = template_args
;
328 /* Return the template info node corresponding to T, whatever T is. */
331 get_template_info (const_tree t
)
333 tree tinfo
= NULL_TREE
;
335 if (!t
|| t
== error_mark_node
)
338 if (TREE_CODE (t
) == NAMESPACE_DECL
339 || TREE_CODE (t
) == PARM_DECL
)
342 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
343 tinfo
= DECL_TEMPLATE_INFO (t
);
345 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
348 if (OVERLOAD_TYPE_P (t
))
349 tinfo
= TYPE_TEMPLATE_INFO (t
);
350 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
351 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
356 /* Returns the template nesting level of the indicated class TYPE.
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
375 template_class_depth (tree type
)
379 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
381 tree tinfo
= get_template_info (type
);
383 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
388 type
= CP_DECL_CONTEXT (type
);
389 else if (LAMBDA_TYPE_P (type
))
390 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
392 type
= CP_TYPE_CONTEXT (type
);
398 /* Subroutine of maybe_begin_member_template_processing.
399 Returns true if processing DECL needs us to push template parms. */
402 inline_needs_template_parms (tree decl
, bool nsdmi
)
404 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
407 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
408 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
411 /* Subroutine of maybe_begin_member_template_processing.
412 Push the template parms in PARMS, starting from LEVELS steps into the
413 chain, and ending at the beginning, since template parms are listed
417 push_inline_template_parms_recursive (tree parmlist
, int levels
)
419 tree parms
= TREE_VALUE (parmlist
);
423 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
425 ++processing_template_decl
;
426 current_template_parms
427 = tree_cons (size_int (processing_template_decl
),
428 parms
, current_template_parms
);
429 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
431 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
433 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
435 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
437 if (error_operand_p (parm
))
440 gcc_assert (DECL_P (parm
));
442 switch (TREE_CODE (parm
))
450 /* Push the CONST_DECL. */
451 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
460 /* Restore the template parameter context for a member template, a
461 friend template defined in a class definition, or a non-template
462 member of template class. */
465 maybe_begin_member_template_processing (tree decl
)
469 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
473 tree ctx
= DECL_CONTEXT (decl
);
474 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
475 /* Disregard full specializations (c++/60999). */
476 && uses_template_parms (ctx
)
477 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
480 if (inline_needs_template_parms (decl
, nsdmi
))
482 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
483 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
485 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
488 parms
= TREE_CHAIN (parms
);
491 push_inline_template_parms_recursive (parms
, levels
);
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels
.safe_push (levels
);
499 /* Undo the effects of maybe_begin_member_template_processing. */
502 maybe_end_member_template_processing (void)
507 if (inline_parm_levels
.length () == 0)
510 last
= inline_parm_levels
.pop ();
511 for (i
= 0; i
< last
; ++i
)
513 --processing_template_decl
;
514 current_template_parms
= TREE_CHAIN (current_template_parms
);
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
523 add_to_template_args (tree args
, tree extra_args
)
530 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
533 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
534 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
536 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
537 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
539 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
540 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
553 add_outermost_template_args (tree args
, tree extra_args
)
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
563 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
569 new_args
= add_to_template_args (args
, extra_args
);
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
577 /* Return the N levels of innermost template arguments from the ARGS. */
580 get_innermost_template_args (tree args
, int n
)
588 /* If N is 1, just return the innermost set of template arguments. */
590 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
592 /* If we're not removing anything, just return the arguments we were
594 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
595 gcc_assert (extra_levels
>= 0);
596 if (extra_levels
== 0)
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args
= make_tree_vec (n
);
601 for (i
= 1; i
<= n
; ++i
)
602 SET_TMPL_ARGS_LEVEL (new_args
, i
,
603 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
612 strip_innermost_template_args (tree args
, int extra_levels
)
615 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
620 /* If N is 1, just return the outermost set of template arguments. */
622 return TMPL_ARGS_LEVEL (args
, 1);
624 /* If we're not removing anything, just return the arguments we were
626 gcc_assert (extra_levels
>= 0);
627 if (extra_levels
== 0)
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args
= make_tree_vec (n
);
632 for (i
= 1; i
<= n
; ++i
)
633 SET_TMPL_ARGS_LEVEL (new_args
, i
,
634 TMPL_ARGS_LEVEL (args
, i
));
639 /* We've got a template header coming up; push to a new level for storing
643 begin_template_parm_list (void)
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
657 pushtag contains special code to insert the TEMPLATE_DECL for S2
658 at the right scope. */
659 begin_scope (sk_template_parms
, NULL
);
660 ++processing_template_decl
;
661 ++processing_template_parmlist
;
662 note_template_header (0);
664 /* Add a dummy parameter level while we process the parameter list. */
665 current_template_parms
666 = tree_cons (size_int (processing_template_decl
),
668 current_template_parms
);
671 /* This routine is called when a specialization is declared. If it is
672 invalid to declare a specialization here, an error is reported and
673 false is returned, otherwise this routine will return true. */
676 check_specialization_scope (void)
678 tree scope
= current_scope ();
682 An explicit specialization shall be declared in the namespace of
683 which the template is a member, or, for member templates, in the
684 namespace of which the enclosing class or enclosing class
685 template is a member. An explicit specialization of a member
686 function, member class or static data member of a class template
687 shall be declared in the namespace of which the class template
689 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
691 error ("explicit specialization in non-namespace scope %qD", scope
);
697 In an explicit specialization declaration for a member of a class
698 template or a member template that appears in namespace scope,
699 the member template and some of its enclosing class templates may
700 remain unspecialized, except that the declaration shall not
701 explicitly specialize a class member template if its enclosing
702 class templates are not explicitly specialized as well. */
703 if (current_template_parms
)
705 error ("enclosing class templates are not explicitly specialized");
712 /* We've just seen template <>. */
715 begin_specialization (void)
717 begin_scope (sk_template_spec
, NULL
);
718 note_template_header (1);
719 return check_specialization_scope ();
722 /* Called at then end of processing a declaration preceded by
726 end_specialization (void)
729 reset_specialization ();
732 /* Any template <>'s that we have seen thus far are not referring to a
733 function specialization. */
736 reset_specialization (void)
738 processing_specialization
= 0;
739 template_header_count
= 0;
742 /* We've just seen a template header. If SPECIALIZATION is nonzero,
743 it was of the form template <>. */
746 note_template_header (int specialization
)
748 processing_specialization
= specialization
;
749 template_header_count
++;
752 /* We're beginning an explicit instantiation. */
755 begin_explicit_instantiation (void)
757 gcc_assert (!processing_explicit_instantiation
);
758 processing_explicit_instantiation
= true;
763 end_explicit_instantiation (void)
765 gcc_assert (processing_explicit_instantiation
);
766 processing_explicit_instantiation
= false;
769 /* An explicit specialization or partial specialization of TMPL is being
770 declared. Check that the namespace in which the specialization is
771 occurring is permissible. Returns false iff it is invalid to
772 specialize TMPL in the current namespace. */
775 check_specialization_namespace (tree tmpl
)
777 tree tpl_ns
= decl_namespace_context (tmpl
);
781 An explicit specialization shall be declared in a namespace enclosing the
782 specialized template. An explicit specialization whose declarator-id is
783 not qualified shall be declared in the nearest enclosing namespace of the
784 template, or, if the namespace is inline (7.3.1), any namespace from its
785 enclosing namespace set. */
786 if (current_scope() != DECL_CONTEXT (tmpl
)
787 && !at_namespace_scope_p ())
789 error ("specialization of %qD must appear at namespace scope", tmpl
);
793 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
794 /* Same or enclosing namespace. */
798 permerror (input_location
,
799 "specialization of %qD in different namespace", tmpl
);
800 inform (DECL_SOURCE_LOCATION (tmpl
),
801 " from definition of %q#D", tmpl
);
806 /* SPEC is an explicit instantiation. Check that it is valid to
807 perform this explicit instantiation in the current namespace. */
810 check_explicit_instantiation_namespace (tree spec
)
814 /* DR 275: An explicit instantiation shall appear in an enclosing
815 namespace of its template. */
816 ns
= decl_namespace_context (spec
);
817 if (!is_nested_namespace (current_namespace
, ns
))
818 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
819 "(which does not enclose namespace %qD)",
820 spec
, current_namespace
, ns
);
823 // Returns the type of a template specialization only if that
824 // specialization needs to be defined. Otherwise (e.g., if the type has
825 // already been defined), the function returns NULL_TREE.
827 maybe_new_partial_specialization (tree type
)
829 // An implicit instantiation of an incomplete type implies
830 // the definition of a new class template.
832 // template<typename T>
835 // template<typename T>
838 // Here, S<T*> is an implicit instantiation of S whose type
840 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
843 // It can also be the case that TYPE is a completed specialization.
844 // Continuing the previous example, suppose we also declare:
846 // template<typename T>
847 // requires Integral<T>
850 // Here, S<T*> refers to the specialization S<T*> defined
851 // above. However, we need to differentiate definitions because
852 // we intend to define a new partial specialization. In this case,
853 // we rely on the fact that the constraints are different for
854 // this declaration than that above.
856 // Note that we also get here for injected class names and
857 // late-parsed template definitions. We must ensure that we
858 // do not create new type declarations for those cases.
859 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
861 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
862 tree args
= CLASSTYPE_TI_ARGS (type
);
864 // If there are no template parameters, this cannot be a new
865 // partial template specializtion?
866 if (!current_template_parms
)
869 // The injected-class-name is not a new partial specialization.
870 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
873 // If the constraints are not the same as those of the primary
874 // then, we can probably create a new specialization.
875 tree type_constr
= current_template_constraints ();
877 if (type
== TREE_TYPE (tmpl
))
879 tree main_constr
= get_constraints (tmpl
);
880 if (equivalent_constraints (type_constr
, main_constr
))
884 // Also, if there's a pre-existing specialization with matching
885 // constraints, then this also isn't new.
886 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
889 tree spec_tmpl
= TREE_VALUE (specs
);
890 tree spec_args
= TREE_PURPOSE (specs
);
891 tree spec_constr
= get_constraints (spec_tmpl
);
892 if (comp_template_args (args
, spec_args
)
893 && equivalent_constraints (type_constr
, spec_constr
))
895 specs
= TREE_CHAIN (specs
);
898 // Create a new type node (and corresponding type decl)
899 // for the newly declared specialization.
900 tree t
= make_class_type (TREE_CODE (type
));
901 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
902 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
904 /* We only need a separate type node for storing the definition of this
905 partial specialization; uses of S<T*> are unconstrained, so all are
906 equivalent. So keep TYPE_CANONICAL the same. */
907 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
909 // Build the corresponding type decl.
910 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
911 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
912 DECL_SOURCE_LOCATION (d
) = input_location
;
920 /* The TYPE is being declared. If it is a template type, that means it
921 is a partial specialization. Do appropriate error-checking. */
924 maybe_process_partial_specialization (tree type
)
928 if (type
== error_mark_node
)
929 return error_mark_node
;
931 /* A lambda that appears in specialization context is not itself a
933 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
936 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
938 error ("name of class shadows template template parameter %qD",
940 return error_mark_node
;
943 context
= TYPE_CONTEXT (type
);
945 if (TYPE_ALIAS_P (type
))
947 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
949 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
950 error ("specialization of alias template %qD",
951 TI_TEMPLATE (tinfo
));
953 error ("explicit specialization of non-template %qT", type
);
954 return error_mark_node
;
956 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
958 /* This is for ordinary explicit specialization and partial
959 specialization of a template class such as:
961 template <> class C<int>;
965 template <class T> class C<T*>;
967 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
969 if (tree t
= maybe_new_partial_specialization (type
))
971 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
972 && !at_namespace_scope_p ())
973 return error_mark_node
;
974 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
975 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
976 if (processing_template_decl
)
978 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
979 if (decl
== error_mark_node
)
980 return error_mark_node
;
981 return TREE_TYPE (decl
);
984 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
985 error ("specialization of %qT after instantiation", type
);
986 else if (errorcount
&& !processing_specialization
987 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
988 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
989 /* Trying to define a specialization either without a template<> header
990 or in an inappropriate place. We've already given an error, so just
991 bail now so we don't actually define the specialization. */
992 return error_mark_node
;
994 else if (CLASS_TYPE_P (type
)
995 && !CLASSTYPE_USE_TEMPLATE (type
)
996 && CLASSTYPE_TEMPLATE_INFO (type
)
997 && context
&& CLASS_TYPE_P (context
)
998 && CLASSTYPE_TEMPLATE_INFO (context
))
1000 /* This is for an explicit specialization of member class
1001 template according to [temp.expl.spec/18]:
1003 template <> template <class U> class C<int>::D;
1005 The context `C<int>' must be an implicit instantiation.
1006 Otherwise this is just a member class template declared
1009 template <> class C<int> { template <class U> class D; };
1010 template <> template <class U> class C<int>::D;
1012 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1013 while in the second case, `C<int>::D' is a primary template
1014 and `C<T>::D' may not exist. */
1016 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1017 && !COMPLETE_TYPE_P (type
))
1020 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1022 if (current_namespace
1023 != decl_namespace_context (tmpl
))
1025 permerror (input_location
,
1026 "specializing %q#T in different namespace", type
);
1027 permerror (DECL_SOURCE_LOCATION (tmpl
),
1028 " from definition of %q#D", tmpl
);
1031 /* Check for invalid specialization after instantiation:
1033 template <> template <> class C<int>::D<int>;
1034 template <> template <class U> class C<int>::D; */
1036 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1037 t
; t
= TREE_CHAIN (t
))
1039 tree inst
= TREE_VALUE (t
);
1040 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1041 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1043 /* We already have a full specialization of this partial
1044 instantiation, or a full specialization has been
1045 looked up but not instantiated. Reassign it to the
1046 new member specialization template. */
1050 elt
.tmpl
= most_general_template (tmpl
);
1051 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1054 type_specializations
->remove_elt (&elt
);
1057 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1060 = type_specializations
->find_slot (&elt
, INSERT
);
1061 entry
= ggc_alloc
<spec_entry
> ();
1066 /* But if we've had an implicit instantiation, that's a
1067 problem ([temp.expl.spec]/6). */
1068 error ("specialization %qT after instantiation %qT",
1072 /* Mark TYPE as a specialization. And as a result, we only
1073 have one level of template argument for the innermost
1075 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1076 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1077 CLASSTYPE_TI_ARGS (type
)
1078 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1081 else if (processing_specialization
)
1083 /* Someday C++0x may allow for enum template specialization. */
1084 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1085 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1086 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1087 "of %qD not allowed by ISO C++", type
);
1090 error ("explicit specialization of non-template %qT", type
);
1091 return error_mark_node
;
1098 /* Returns nonzero if we can optimize the retrieval of specializations
1099 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1100 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1103 optimize_specialization_lookup_p (tree tmpl
)
1105 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1106 && DECL_CLASS_SCOPE_P (tmpl
)
1107 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1109 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1110 /* The optimized lookup depends on the fact that the
1111 template arguments for the member function template apply
1112 purely to the containing class, which is not true if the
1113 containing class is an explicit or partial
1115 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1116 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1117 && !DECL_CONV_FN_P (tmpl
)
1118 /* It is possible to have a template that is not a member
1119 template and is not a member of a template class:
1121 template <typename T>
1122 struct S { friend A::f(); };
1124 Here, the friend function is a template, but the context does
1125 not have template information. The optimized lookup relies
1126 on having ARGS be the template arguments for both the class
1127 and the function template. */
1128 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1131 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1132 gone through coerce_template_parms by now. */
1135 verify_unstripped_args (tree args
)
1137 ++processing_template_decl
;
1138 if (!any_dependent_template_arguments_p (args
))
1140 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1141 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1143 tree arg
= TREE_VEC_ELT (inner
, i
);
1144 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1146 else if (TYPE_P (arg
))
1147 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1148 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1152 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1155 --processing_template_decl
;
1158 /* Retrieve the specialization (in the sense of [temp.spec] - a
1159 specialization is either an instantiation or an explicit
1160 specialization) of TMPL for the given template ARGS. If there is
1161 no such specialization, return NULL_TREE. The ARGS are a vector of
1162 arguments, or a vector of vectors of arguments, in the case of
1163 templates with more than one level of parameters.
1165 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1166 then we search for a partial specialization matching ARGS. This
1167 parameter is ignored if TMPL is not a class template.
1169 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1170 result is a NONTYPE_ARGUMENT_PACK. */
1173 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1175 if (tmpl
== NULL_TREE
)
1178 if (args
== error_mark_node
)
1181 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1182 || TREE_CODE (tmpl
) == FIELD_DECL
);
1184 /* There should be as many levels of arguments as there are
1185 levels of parameters. */
1186 gcc_assert (TMPL_ARGS_DEPTH (args
)
1187 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1188 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1189 : template_class_depth (DECL_CONTEXT (tmpl
))));
1192 verify_unstripped_args (args
);
1194 /* Lambda functions in templates aren't instantiated normally, but through
1195 tsubst_lambda_expr. */
1196 if (lambda_fn_in_template_p (tmpl
))
1199 if (optimize_specialization_lookup_p (tmpl
))
1201 /* The template arguments actually apply to the containing
1202 class. Find the class specialization with those
1204 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1205 tree class_specialization
1206 = retrieve_specialization (class_template
, args
, 0);
1207 if (!class_specialization
)
1210 /* Find the instance of TMPL. */
1211 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1212 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1215 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn
) == class_specialization
)
1227 hash_table
<spec_hasher
> *specializations
;
1231 elt
.spec
= NULL_TREE
;
1233 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1234 specializations
= type_specializations
;
1236 specializations
= decl_specializations
;
1239 hash
= spec_hasher::hash (&elt
);
1240 found
= specializations
->find_with_hash (&elt
, hash
);
1248 /* Like retrieve_specialization, but for local declarations. */
1251 retrieve_local_specialization (tree tmpl
)
1253 if (local_specializations
== NULL
)
1256 tree
*slot
= local_specializations
->get (tmpl
);
1257 return slot
? *slot
: NULL_TREE
;
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263 is_specialization_of (tree decl
, tree tmpl
)
1267 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1271 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1277 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1279 for (t
= TREE_TYPE (decl
);
1281 t
= CLASSTYPE_USE_TEMPLATE (t
)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1283 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1294 is_specialization_of_friend (tree decl
, tree friend_decl
)
1296 bool need_template
= true;
1299 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1300 || TREE_CODE (decl
) == TYPE_DECL
);
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1305 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl
)
1307 && !DECL_USE_TEMPLATE (friend_decl
))
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1311 need_template
= false;
1313 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl
))
1315 need_template
= false;
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1321 if (is_specialization_of (decl
, friend_decl
))
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1330 For example, given a template friend declaration
1332 template <class T> friend void A<T>::f();
1334 the member function below is considered a friend
1336 template <> struct A<int> {
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1344 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1346 && DECL_CLASS_SCOPE_P (decl
)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1366 tree context
= DECL_CONTEXT (decl
);
1367 tree args
= NULL_TREE
;
1368 int current_depth
= 0;
1370 while (current_depth
< template_depth
)
1372 if (CLASSTYPE_TEMPLATE_INFO (context
))
1374 if (current_depth
== 0)
1375 args
= TYPE_TI_ARGS (context
);
1377 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1380 context
= TYPE_CONTEXT (context
);
1383 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1388 tree friend_args_type
;
1389 tree decl_args_type
;
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1393 is_template
= DECL_TEMPLATE_INFO (decl
)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1395 if (need_template
^ is_template
)
1397 else if (is_template
)
1399 /* If both are templates, check template parameter list. */
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1408 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1411 decl_type
= TREE_TYPE (decl
);
1413 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1414 tf_none
, NULL_TREE
);
1415 if (friend_type
== error_mark_node
)
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1425 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1427 friend_args_type
= TREE_CHAIN (friend_args_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1429 decl_args_type
= TREE_CHAIN (decl_args_type
);
1431 return compparms (decl_args_type
, friend_args_type
);
1435 /* DECL is a TYPE_DECL */
1437 tree decl_type
= TREE_TYPE (decl
);
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1445 if (need_template
^ is_template
)
1447 else if (is_template
)
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1453 != DECL_NAME (friend_decl
))
1456 /* Now check template parameter list. */
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1465 return (DECL_NAME (decl
)
1466 == DECL_NAME (friend_decl
));
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1481 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1485 spec_entry
**slot
= NULL
;
1488 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1489 || (TREE_CODE (tmpl
) == FIELD_DECL
1490 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1492 if (TREE_CODE (spec
) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec
)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1501 template <class T> void f(T t1, T t = T())
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1507 if (optimize_specialization_lookup_p (tmpl
))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn
= retrieve_specialization (tmpl
, args
, 0);
1518 hash
= spec_hasher::hash (&elt
);
1521 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1523 fn
= ((spec_entry
*) *slot
)->spec
;
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1535 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1537 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1539 if (DECL_ODR_USED (fn
)
1540 || DECL_EXPLICIT_INSTANTIATION (fn
))
1542 error ("specialization of %qD after instantiation",
1544 return error_mark_node
;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn
) = NULL_TREE
;
1573 duplicate_decls (spec
, fn
, is_friend
);
1574 /* The call to duplicate_decls will have applied
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone
, fn
)
1586 DECL_DECLARED_INLINE_P (clone
)
1587 = DECL_DECLARED_INLINE_P (fn
);
1588 DECL_SOURCE_LOCATION (clone
)
1589 = DECL_SOURCE_LOCATION (fn
);
1590 DECL_DELETED_FN (clone
)
1591 = DECL_DELETED_FN (fn
);
1593 check_specialization_namespace (tmpl
);
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1600 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1601 if (dd
== error_mark_node
)
1602 /* We've already complained in duplicate_decls. */
1603 return error_mark_node
;
1605 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1606 /* Dup decl failed, but this is a new definition. Set the
1607 line number so any errors match this new
1609 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1615 return duplicate_decls (spec
, fn
, is_friend
);
1617 /* A specialization must be declared in the same namespace as the
1618 template it is specializing. */
1619 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1620 && !check_specialization_namespace (tmpl
))
1621 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1623 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1625 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1626 gcc_assert (tmpl
&& args
&& spec
);
1629 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1630 && PRIMARY_TEMPLATE_P (tmpl
)
1631 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1632 || variable_template_p (tmpl
))
1633 /* If TMPL is a forward declaration of a template function, keep a list
1634 of all specializations in case we need to reassign them to a friend
1635 template later in tsubst_friend_function.
1637 Also keep a list of all variable template instantiations so that
1638 process_partial_specialization can check whether a later partial
1639 specialization would have used it. */
1640 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1641 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1647 /* Returns true iff two spec_entry nodes are equivalent. */
1649 int comparing_specializations
;
1652 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1656 ++comparing_specializations
;
1657 equal
= (e1
->tmpl
== e2
->tmpl
1658 && comp_template_args (e1
->args
, e2
->args
));
1659 if (equal
&& flag_concepts
1660 /* tmpl could be a FIELD_DECL for a capture pack. */
1661 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1662 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1663 && uses_template_parms (e1
->args
))
1665 /* Partial specializations of a variable template can be distinguished by
1667 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1668 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1669 equal
= equivalent_constraints (c1
, c2
);
1671 --comparing_specializations
;
1676 /* Returns a hash for a template TMPL and template arguments ARGS. */
1679 hash_tmpl_and_args (tree tmpl
, tree args
)
1681 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1682 return iterative_hash_template_arg (args
, val
);
1685 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1689 spec_hasher::hash (spec_entry
*e
)
1691 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1694 /* Recursively calculate a hash value for a template argument ARG, for use
1695 in the hash tables of template specializations. */
1698 iterative_hash_template_arg (tree arg
, hashval_t val
)
1700 unsigned HOST_WIDE_INT i
;
1701 enum tree_code code
;
1704 if (arg
== NULL_TREE
)
1705 return iterative_hash_object (arg
, val
);
1710 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1713 code
= TREE_CODE (arg
);
1714 tclass
= TREE_CODE_CLASS (code
);
1716 val
= iterative_hash_object (code
, val
);
1723 case IDENTIFIER_NODE
:
1724 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1728 int i
, len
= TREE_VEC_LENGTH (arg
);
1729 for (i
= 0; i
< len
; ++i
)
1730 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1734 case TYPE_PACK_EXPANSION
:
1735 case EXPR_PACK_EXPANSION
:
1736 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1737 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1739 case TYPE_ARGUMENT_PACK
:
1740 case NONTYPE_ARGUMENT_PACK
:
1741 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1744 for (; arg
; arg
= TREE_CHAIN (arg
))
1745 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1749 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1750 val
= iterative_hash_template_arg (*iter
, val
);
1756 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1757 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1759 val
= iterative_hash_template_arg (field
, val
);
1760 val
= iterative_hash_template_arg (value
, val
);
1766 if (!DECL_ARTIFICIAL (arg
))
1768 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1769 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1771 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1774 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1777 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1778 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1780 case TEMPLATE_PARM_INDEX
:
1781 val
= iterative_hash_template_arg
1782 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1783 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1784 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1787 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1788 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1789 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1792 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1794 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1798 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1799 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1800 val
= iterative_hash_object (code
, val
);
1801 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1804 /* A lambda can't appear in a template arg, but don't crash on
1806 gcc_assert (seen_error ());
1810 case IMPLICIT_CONV_EXPR
:
1811 case STATIC_CAST_EXPR
:
1812 case REINTERPRET_CAST_EXPR
:
1813 case CONST_CAST_EXPR
:
1814 case DYNAMIC_CAST_EXPR
:
1816 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1817 /* Now hash operands as usual. */
1827 if (alias_template_specialization_p (arg
))
1829 // We want an alias specialization that survived strip_typedefs
1830 // to hash differently from its TYPE_CANONICAL, to avoid hash
1831 // collisions that compare as different in template_args_equal.
1832 // These could be dependent specializations that strip_typedefs
1833 // left alone, or untouched specializations because
1834 // coerce_template_parms returns the unconverted template
1835 // arguments if it sees incomplete argument packs.
1836 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1837 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1839 if (TYPE_CANONICAL (arg
))
1840 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1842 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1843 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1844 /* Otherwise just compare the types during lookup. */
1847 case tcc_declaration
:
1849 return iterative_hash_expr (arg
, val
);
1852 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1854 unsigned n
= cp_tree_operand_length (arg
);
1855 for (i
= 0; i
< n
; ++i
)
1856 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1864 /* Unregister the specialization SPEC as a specialization of TMPL.
1865 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1866 if the SPEC was listed as a specialization of TMPL.
1868 Note that SPEC has been ggc_freed, so we can't look inside it. */
1871 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1876 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1877 elt
.args
= TI_ARGS (tinfo
);
1878 elt
.spec
= NULL_TREE
;
1880 entry
= decl_specializations
->find (&elt
);
1883 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1884 gcc_assert (new_spec
!= NULL_TREE
);
1885 entry
->spec
= new_spec
;
1892 /* Like register_specialization, but for local declarations. We are
1893 registering SPEC, an instantiation of TMPL. */
1896 register_local_specialization (tree spec
, tree tmpl
)
1898 gcc_assert (tmpl
!= spec
);
1899 local_specializations
->put (tmpl
, spec
);
1902 /* TYPE is a class type. Returns true if TYPE is an explicitly
1903 specialized class. */
1906 explicit_class_specialization_p (tree type
)
1908 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1910 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1913 /* Print the list of functions at FNS, going through all the overloads
1914 for each element of the list. Alternatively, FNS can not be a
1915 TREE_LIST, in which case it will be printed together with all the
1918 MORE and *STR should respectively be FALSE and NULL when the function
1919 is called from the outside. They are used internally on recursive
1920 calls. print_candidates manages the two parameters and leaves NULL
1921 in *STR when it ends. */
1924 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1926 if (TREE_CODE (fns
) == TREE_LIST
)
1927 for (; fns
; fns
= TREE_CHAIN (fns
))
1928 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1930 for (lkp_iterator
iter (fns
); iter
;)
1935 const char *pfx
= *str
;
1939 pfx
= _("candidates are:");
1941 pfx
= _("candidate is:");
1942 *str
= get_spaces (pfx
);
1944 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1948 /* Print the list of candidate FNS in an error message. FNS can also
1949 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1952 print_candidates (tree fns
)
1955 print_candidates_1 (fns
, &str
);
1959 /* Get a (possibly) constrained template declaration for the
1960 purpose of ordering candidates. */
1962 get_template_for_ordering (tree list
)
1964 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1965 tree f
= TREE_VALUE (list
);
1966 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1967 return TI_TEMPLATE (ti
);
1971 /* Among candidates having the same signature, return the
1972 most constrained or NULL_TREE if there is no best candidate.
1973 If the signatures of candidates vary (e.g., template
1974 specialization vs. member function), then there can be no
1977 Note that we don't compare constraints on the functions
1978 themselves, but rather those of their templates. */
1980 most_constrained_function (tree candidates
)
1982 // Try to find the best candidate in a first pass.
1983 tree champ
= candidates
;
1984 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1986 int winner
= more_constrained (get_template_for_ordering (champ
),
1987 get_template_for_ordering (c
));
1989 champ
= c
; // The candidate is more constrained
1990 else if (winner
== 0)
1991 return NULL_TREE
; // Neither is more constrained
1994 // Verify that the champ is better than previous candidates.
1995 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
1996 if (!more_constrained (get_template_for_ordering (champ
),
1997 get_template_for_ordering (c
)))
2005 /* Returns the template (one of the functions given by TEMPLATE_ID)
2006 which can be specialized to match the indicated DECL with the
2007 explicit template args given in TEMPLATE_ID. The DECL may be
2008 NULL_TREE if none is available. In that case, the functions in
2009 TEMPLATE_ID are non-members.
2011 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2012 specialization of a member template.
2014 The TEMPLATE_COUNT is the number of references to qualifying
2015 template classes that appeared in the name of the function. See
2016 check_explicit_specialization for a more accurate description.
2018 TSK indicates what kind of template declaration (if any) is being
2019 declared. TSK_TEMPLATE indicates that the declaration given by
2020 DECL, though a FUNCTION_DECL, has template parameters, and is
2021 therefore a template function.
2023 The template args (those explicitly specified and those deduced)
2024 are output in a newly created vector *TARGS_OUT.
2026 If it is impossible to determine the result, an error message is
2027 issued. The error_mark_node is returned to indicate failure. */
2030 determine_specialization (tree template_id
,
2033 int need_member_template
,
2039 tree explicit_targs
;
2040 tree candidates
= NULL_TREE
;
2042 /* A TREE_LIST of templates of which DECL may be a specialization.
2043 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2044 corresponding TREE_PURPOSE is the set of template arguments that,
2045 when used to instantiate the template, would produce a function
2046 with the signature of DECL. */
2047 tree templates
= NULL_TREE
;
2049 cp_binding_level
*b
;
2051 *targs_out
= NULL_TREE
;
2053 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2054 return error_mark_node
;
2056 /* We shouldn't be specializing a member template of an
2057 unspecialized class template; we already gave an error in
2058 check_specialization_scope, now avoid crashing. */
2059 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2060 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2062 gcc_assert (errorcount
);
2063 return error_mark_node
;
2066 fns
= TREE_OPERAND (template_id
, 0);
2067 explicit_targs
= TREE_OPERAND (template_id
, 1);
2069 if (fns
== error_mark_node
)
2070 return error_mark_node
;
2072 /* Check for baselinks. */
2073 if (BASELINK_P (fns
))
2074 fns
= BASELINK_FUNCTIONS (fns
);
2076 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2078 error ("%qD is not a function template", fns
);
2079 return error_mark_node
;
2081 else if (VAR_P (decl
) && !variable_template_p (fns
))
2083 error ("%qD is not a variable template", fns
);
2084 return error_mark_node
;
2087 /* Count the number of template headers specified for this
2090 for (b
= current_binding_level
;
2091 b
->kind
== sk_template_parms
;
2095 tree orig_fns
= fns
;
2097 if (variable_template_p (fns
))
2099 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2100 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2101 tf_warning_or_error
,
2102 /*req_all*/true, /*use_defarg*/true);
2103 if (targs
!= error_mark_node
)
2104 templates
= tree_cons (targs
, fns
, templates
);
2106 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2110 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2112 tree decl_arg_types
;
2116 /* In case of explicit specialization, we need to check if
2117 the number of template headers appearing in the specialization
2118 is correct. This is usually done in check_explicit_specialization,
2119 but the check done there cannot be exhaustive when specializing
2120 member functions. Consider the following code:
2122 template <> void A<int>::f(int);
2123 template <> template <> void A<int>::f(int);
2125 Assuming that A<int> is not itself an explicit specialization
2126 already, the first line specializes "f" which is a non-template
2127 member function, whilst the second line specializes "f" which
2128 is a template member function. So both lines are syntactically
2129 correct, and check_explicit_specialization does not reject
2132 Here, we can do better, as we are matching the specialization
2133 against the declarations. We count the number of template
2134 headers, and we check if they match TEMPLATE_COUNT + 1
2135 (TEMPLATE_COUNT is the number of qualifying template classes,
2136 plus there must be another header for the member template
2139 Notice that if header_count is zero, this is not a
2140 specialization but rather a template instantiation, so there
2141 is no check we can perform here. */
2142 if (header_count
&& header_count
!= template_count
+ 1)
2145 /* Check that the number of template arguments at the
2146 innermost level for DECL is the same as for FN. */
2147 if (current_binding_level
->kind
== sk_template_parms
2148 && !current_binding_level
->explicit_spec_p
2149 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2150 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2151 (current_template_parms
))))
2154 /* DECL might be a specialization of FN. */
2155 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2156 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2158 /* For a non-static member function, we need to make sure
2159 that the const qualification is the same. Since
2160 get_bindings does not try to merge the "this" parameter,
2161 we must do the comparison explicitly. */
2162 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2163 && !same_type_p (TREE_VALUE (fn_arg_types
),
2164 TREE_VALUE (decl_arg_types
)))
2167 /* Skip the "this" parameter and, for constructors of
2168 classes with virtual bases, the VTT parameter. A
2169 full specialization of a constructor will have a VTT
2170 parameter, but a template never will. */
2172 = skip_artificial_parms_for (decl
, decl_arg_types
);
2174 = skip_artificial_parms_for (fn
, fn_arg_types
);
2176 /* Function templates cannot be specializations; there are
2177 no partial specializations of functions. Therefore, if
2178 the type of DECL does not match FN, there is no
2181 Note that it should never be the case that we have both
2182 candidates added here, and for regular member functions
2184 if (tsk
== tsk_template
)
2186 if (compparms (fn_arg_types
, decl_arg_types
))
2187 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2191 /* See whether this function might be a specialization of this
2192 template. Suppress access control because we might be trying
2193 to make this specialization a friend, and we have already done
2194 access control for the declaration of the specialization. */
2195 push_deferring_access_checks (dk_no_check
);
2196 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2197 pop_deferring_access_checks ();
2200 /* We cannot deduce template arguments that when used to
2201 specialize TMPL will produce DECL. */
2204 /* Remove, from the set of candidates, all those functions
2205 whose constraints are not satisfied. */
2206 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2209 // Then, try to form the new function type.
2210 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2211 if (insttype
== error_mark_node
)
2214 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2215 if (!compparms (fn_arg_types
, decl_arg_types
))
2218 /* Save this template, and the arguments deduced. */
2219 templates
= tree_cons (targs
, fn
, templates
);
2221 else if (need_member_template
)
2222 /* FN is an ordinary member function, and we need a
2223 specialization of a member template. */
2225 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2226 /* We can get IDENTIFIER_NODEs here in certain erroneous
2229 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2230 /* This is just an ordinary non-member function. Nothing can
2231 be a specialization of that. */
2233 else if (DECL_ARTIFICIAL (fn
))
2234 /* Cannot specialize functions that are created implicitly. */
2238 tree decl_arg_types
;
2240 /* This is an ordinary member function. However, since
2241 we're here, we can assume its enclosing class is a
2242 template class. For example,
2244 template <typename T> struct S { void f(); };
2245 template <> void S<int>::f() {}
2247 Here, S<int>::f is a non-template, but S<int> is a
2248 template class. If FN has the same type as DECL, we
2249 might be in business. */
2251 if (!DECL_TEMPLATE_INFO (fn
))
2252 /* Its enclosing class is an explicit specialization
2253 of a template class. This is not a candidate. */
2256 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2257 TREE_TYPE (TREE_TYPE (fn
))))
2258 /* The return types differ. */
2261 /* Adjust the type of DECL in case FN is a static member. */
2262 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2263 if (DECL_STATIC_FUNCTION_P (fn
)
2264 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2265 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2267 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2271 // If the deduced arguments do not satisfy the constraints,
2272 // this is not a candidate.
2273 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2276 // Add the candidate.
2277 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2281 if (templates
&& TREE_CHAIN (templates
))
2287 It is possible for a specialization with a given function
2288 signature to be instantiated from more than one function
2289 template. In such cases, explicit specification of the
2290 template arguments must be used to uniquely identify the
2291 function template specialization being specialized.
2293 Note that here, there's no suggestion that we're supposed to
2294 determine which of the candidate templates is most
2295 specialized. However, we, also have:
2299 Partial ordering of overloaded function template
2300 declarations is used in the following contexts to select
2301 the function template to which a function template
2302 specialization refers:
2304 -- when an explicit specialization refers to a function
2307 So, we do use the partial ordering rules, at least for now.
2308 This extension can only serve to make invalid programs valid,
2309 so it's safe. And, there is strong anecdotal evidence that
2310 the committee intended the partial ordering rules to apply;
2311 the EDG front end has that behavior, and John Spicer claims
2312 that the committee simply forgot to delete the wording in
2313 [temp.expl.spec]. */
2314 tree tmpl
= most_specialized_instantiation (templates
);
2315 if (tmpl
!= error_mark_node
)
2318 TREE_CHAIN (templates
) = NULL_TREE
;
2322 // Concepts allows multiple declarations of member functions
2323 // with the same signature. Like above, we need to rely on
2324 // on the partial ordering of those candidates to determine which
2326 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2328 if (tree cand
= most_constrained_function (candidates
))
2331 TREE_CHAIN (cand
) = NULL_TREE
;
2335 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2337 error ("template-id %qD for %q+D does not match any template "
2338 "declaration", template_id
, decl
);
2339 if (header_count
&& header_count
!= template_count
+ 1)
2340 inform (input_location
, "saw %d %<template<>%>, need %d for "
2341 "specializing a member function template",
2342 header_count
, template_count
+ 1);
2344 print_candidates (orig_fns
);
2345 return error_mark_node
;
2347 else if ((templates
&& TREE_CHAIN (templates
))
2348 || (candidates
&& TREE_CHAIN (candidates
))
2349 || (templates
&& candidates
))
2351 error ("ambiguous template specialization %qD for %q+D",
2353 candidates
= chainon (candidates
, templates
);
2354 print_candidates (candidates
);
2355 return error_mark_node
;
2358 /* We have one, and exactly one, match. */
2361 tree fn
= TREE_VALUE (candidates
);
2362 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2364 // Propagate the candidate's constraints to the declaration.
2365 set_constraints (decl
, get_constraints (fn
));
2367 /* DECL is a re-declaration or partial instantiation of a template
2369 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2371 /* It was a specialization of an ordinary member function in a
2373 return DECL_TI_TEMPLATE (fn
);
2376 /* It was a specialization of a template. */
2377 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2378 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2380 *targs_out
= copy_node (targs
);
2381 SET_TMPL_ARGS_LEVEL (*targs_out
,
2382 TMPL_ARGS_DEPTH (*targs_out
),
2383 TREE_PURPOSE (templates
));
2386 *targs_out
= TREE_PURPOSE (templates
);
2387 return TREE_VALUE (templates
);
2390 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2391 but with the default argument values filled in from those in the
2395 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2398 tree new_spec_types
;
2403 if (spec_types
== void_list_node
)
2404 return void_list_node
;
2406 /* Substitute into the rest of the list. */
2408 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2409 TREE_CHAIN (tmpl_types
));
2411 /* Add the default argument for this parameter. */
2412 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2413 TREE_VALUE (spec_types
),
2417 /* DECL is an explicit specialization. Replicate default arguments
2418 from the template it specializes. (That way, code like:
2420 template <class T> void f(T = 3);
2421 template <> void f(double);
2424 works, as required.) An alternative approach would be to look up
2425 the correct default arguments at the call-site, but this approach
2426 is consistent with how implicit instantiations are handled. */
2429 copy_default_args_to_explicit_spec (tree decl
)
2434 tree new_spec_types
;
2438 tree object_type
= NULL_TREE
;
2439 tree in_charge
= NULL_TREE
;
2440 tree vtt
= NULL_TREE
;
2442 /* See if there's anything we need to do. */
2443 tmpl
= DECL_TI_TEMPLATE (decl
);
2444 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2445 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2446 if (TREE_PURPOSE (t
))
2451 old_type
= TREE_TYPE (decl
);
2452 spec_types
= TYPE_ARG_TYPES (old_type
);
2454 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2456 /* Remove the this pointer, but remember the object's type for
2458 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2459 spec_types
= TREE_CHAIN (spec_types
);
2460 tmpl_types
= TREE_CHAIN (tmpl_types
);
2462 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2464 /* DECL may contain more parameters than TMPL due to the extra
2465 in-charge parameter in constructors and destructors. */
2466 in_charge
= spec_types
;
2467 spec_types
= TREE_CHAIN (spec_types
);
2469 if (DECL_HAS_VTT_PARM_P (decl
))
2472 spec_types
= TREE_CHAIN (spec_types
);
2476 /* Compute the merged default arguments. */
2478 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2480 /* Compute the new FUNCTION_TYPE. */
2484 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2489 /* Put the in-charge parameter back. */
2490 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2491 TREE_VALUE (in_charge
),
2494 new_type
= build_method_type_directly (object_type
,
2495 TREE_TYPE (old_type
),
2499 new_type
= build_function_type (TREE_TYPE (old_type
),
2501 new_type
= cp_build_type_attribute_variant (new_type
,
2502 TYPE_ATTRIBUTES (old_type
));
2503 new_type
= build_exception_variant (new_type
,
2504 TYPE_RAISES_EXCEPTIONS (old_type
));
2506 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2507 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2509 TREE_TYPE (decl
) = new_type
;
2512 /* Return the number of template headers we expect to see for a definition
2513 or specialization of CTYPE or one of its non-template members. */
2516 num_template_headers_for_class (tree ctype
)
2518 int num_templates
= 0;
2520 while (ctype
&& CLASS_TYPE_P (ctype
))
2522 /* You're supposed to have one `template <...>' for every
2523 template class, but you don't need one for a full
2524 specialization. For example:
2526 template <class T> struct S{};
2527 template <> struct S<int> { void f(); };
2528 void S<int>::f () {}
2530 is correct; there shouldn't be a `template <>' for the
2531 definition of `S<int>::f'. */
2532 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2533 /* If CTYPE does not have template information of any
2534 kind, then it is not a template, nor is it nested
2535 within a template. */
2537 if (explicit_class_specialization_p (ctype
))
2539 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2542 ctype
= TYPE_CONTEXT (ctype
);
2545 return num_templates
;
2548 /* Do a simple sanity check on the template headers that precede the
2549 variable declaration DECL. */
2552 check_template_variable (tree decl
)
2554 tree ctx
= CP_DECL_CONTEXT (decl
);
2555 int wanted
= num_template_headers_for_class (ctx
);
2556 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2557 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2559 if (cxx_dialect
< cxx14
)
2560 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2561 "variable templates only available with "
2562 "-std=c++14 or -std=gnu++14");
2564 // Namespace-scope variable templates should have a template header.
2567 if (template_header_count
> wanted
)
2569 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2570 "too many template headers for %qD "
2573 if (warned
&& CLASS_TYPE_P (ctx
)
2574 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2575 inform (DECL_SOURCE_LOCATION (decl
),
2576 "members of an explicitly specialized class are defined "
2577 "without a template header");
2581 /* An explicit specialization whose declarator-id or class-head-name is not
2582 qualified shall be declared in the nearest enclosing namespace of the
2583 template, or, if the namespace is inline (7.3.1), any namespace from its
2584 enclosing namespace set.
2586 If the name declared in the explicit instantiation is an unqualified name,
2587 the explicit instantiation shall appear in the namespace where its template
2588 is declared or, if that namespace is inline (7.3.1), any namespace from its
2589 enclosing namespace set. */
2592 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2594 tree tmpl
= most_general_template (t
);
2595 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2596 && !is_nested_namespace (current_namespace
,
2597 CP_DECL_CONTEXT (tmpl
), true))
2599 if (processing_specialization
)
2600 permerror (loc
, "explicit specialization of %qD outside its "
2601 "namespace must use a nested-name-specifier", tmpl
);
2602 else if (processing_explicit_instantiation
2603 && cxx_dialect
>= cxx11
)
2604 /* This was allowed in C++98, so only pedwarn. */
2605 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2606 "outside its namespace must use a nested-name-"
2611 /* Check to see if the function just declared, as indicated in
2612 DECLARATOR, and in DECL, is a specialization of a function
2613 template. We may also discover that the declaration is an explicit
2614 instantiation at this point.
2616 Returns DECL, or an equivalent declaration that should be used
2617 instead if all goes well. Issues an error message if something is
2618 amiss. Returns error_mark_node if the error is not easily
2621 FLAGS is a bitmask consisting of the following flags:
2623 2: The function has a definition.
2624 4: The function is a friend.
2626 The TEMPLATE_COUNT is the number of references to qualifying
2627 template classes that appeared in the name of the function. For
2630 template <class T> struct S { void f(); };
2633 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2634 classes are not counted in the TEMPLATE_COUNT, so that in
2636 template <class T> struct S {};
2637 template <> struct S<int> { void f(); }
2638 template <> void S<int>::f();
2640 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2641 invalid; there should be no template <>.)
2643 If the function is a specialization, it is marked as such via
2644 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2645 is set up correctly, and it is added to the list of specializations
2646 for that template. */
2649 check_explicit_specialization (tree declarator
,
2654 int have_def
= flags
& 2;
2655 int is_friend
= flags
& 4;
2656 bool is_concept
= flags
& 8;
2657 int specialization
= 0;
2658 int explicit_instantiation
= 0;
2659 int member_specialization
= 0;
2660 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2661 tree dname
= DECL_NAME (decl
);
2666 if (!processing_specialization
)
2669 tsk
= tsk_excessive_parms
;
2672 tsk
= current_tmpl_spec_kind (template_count
);
2677 if (processing_specialization
&& !VAR_P (decl
))
2680 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2682 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2685 /* This could be something like:
2687 template <class T> void f(T);
2688 class S { friend void f<>(int); } */
2692 /* This case handles bogus declarations like template <>
2693 template <class T> void f<int>(); */
2695 error ("template-id %qD in declaration of primary template",
2702 case tsk_invalid_member_spec
:
2703 /* The error has already been reported in
2704 check_specialization_scope. */
2705 return error_mark_node
;
2707 case tsk_invalid_expl_inst
:
2708 error ("template parameter list used in explicit instantiation");
2714 error ("definition provided for explicit instantiation");
2716 explicit_instantiation
= 1;
2719 case tsk_excessive_parms
:
2720 case tsk_insufficient_parms
:
2721 if (tsk
== tsk_excessive_parms
)
2722 error ("too many template parameter lists in declaration of %qD",
2724 else if (template_header_count
)
2725 error("too few template parameter lists in declaration of %qD", decl
);
2727 error("explicit specialization of %qD must be introduced by "
2728 "%<template <>%>", decl
);
2733 error ("explicit specialization declared %<concept%>");
2735 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2736 /* In cases like template<> constexpr bool v = true;
2737 We'll give an error in check_template_variable. */
2740 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2742 member_specialization
= 1;
2748 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2750 /* This case handles bogus declarations like template <>
2751 template <class T> void f<int>(); */
2753 if (!uses_template_parms (declarator
))
2754 error ("template-id %qD in declaration of primary template",
2756 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2758 /* Partial specialization of variable template. */
2759 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2763 else if (cxx_dialect
< cxx14
)
2764 error ("non-type partial specialization %qD "
2765 "is not allowed", declarator
);
2767 error ("non-class, non-variable partial specialization %qD "
2768 "is not allowed", declarator
);
2773 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2774 /* This is a specialization of a member template, without
2775 specialization the containing class. Something like:
2777 template <class T> struct S {
2778 template <class U> void f (U);
2780 template <> template <class U> void S<int>::f(U) {}
2782 That's a specialization -- but of the entire template. */
2790 if ((specialization
|| member_specialization
)
2791 /* This doesn't apply to variable templates. */
2792 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2793 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2795 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2796 for (; t
; t
= TREE_CHAIN (t
))
2797 if (TREE_PURPOSE (t
))
2799 permerror (input_location
,
2800 "default argument specified in explicit specialization");
2805 if (specialization
|| member_specialization
|| explicit_instantiation
)
2807 tree tmpl
= NULL_TREE
;
2808 tree targs
= NULL_TREE
;
2809 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2811 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2812 if (!was_template_id
)
2816 gcc_assert (identifier_p (declarator
));
2821 /* If there is no class context, the explicit instantiation
2822 must be at namespace scope. */
2823 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2825 /* Find the namespace binding, using the declaration
2827 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2829 if (fns
== error_mark_node
)
2830 /* If lookup fails, look for a friend declaration so we can
2831 give a better diagnostic. */
2832 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2833 /*type*/false, /*complain*/true,
2836 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2838 error ("%qD is not a template function", dname
);
2839 fns
= error_mark_node
;
2843 declarator
= lookup_template_function (fns
, NULL_TREE
);
2846 if (declarator
== error_mark_node
)
2847 return error_mark_node
;
2849 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2851 if (!explicit_instantiation
)
2852 /* A specialization in class scope. This is invalid,
2853 but the error will already have been flagged by
2854 check_specialization_scope. */
2855 return error_mark_node
;
2858 /* It's not valid to write an explicit instantiation in
2861 class C { template void f(); }
2863 This case is caught by the parser. However, on
2866 template class C { void f(); };
2868 (which is invalid) we can get here. The error will be
2875 else if (ctype
!= NULL_TREE
2876 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2878 // We'll match variable templates in start_decl.
2882 /* Find the list of functions in ctype that have the same
2883 name as the declared function. */
2884 tree name
= TREE_OPERAND (declarator
, 0);
2886 if (constructor_name_p (name
, ctype
))
2888 if (DECL_CONSTRUCTOR_P (decl
)
2889 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2890 : !CLASSTYPE_DESTRUCTOR (ctype
))
2892 /* From [temp.expl.spec]:
2894 If such an explicit specialization for the member
2895 of a class template names an implicitly-declared
2896 special member function (clause _special_), the
2897 program is ill-formed.
2899 Similar language is found in [temp.explicit]. */
2900 error ("specialization of implicitly-declared special member function");
2901 return error_mark_node
;
2904 name
= DECL_NAME (decl
);
2907 /* For a type-conversion operator, We might be looking for
2908 `operator int' which will be a specialization of
2909 `operator T'. Grab all the conversion operators, and
2910 then select from them. */
2911 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2912 ? conv_op_identifier
: name
);
2914 if (fns
== NULL_TREE
)
2916 error ("no member function %qD declared in %qT", name
, ctype
);
2917 return error_mark_node
;
2920 TREE_OPERAND (declarator
, 0) = fns
;
2923 /* Figure out what exactly is being specialized at this point.
2924 Note that for an explicit instantiation, even one for a
2925 member function, we cannot tell a priori whether the
2926 instantiation is for a member template, or just a member
2927 function of a template class. Even if a member template is
2928 being instantiated, the member template arguments may be
2929 elided if they can be deduced from the rest of the
2931 tmpl
= determine_specialization (declarator
, decl
,
2933 member_specialization
,
2937 if (!tmpl
|| tmpl
== error_mark_node
)
2938 /* We couldn't figure out what this declaration was
2940 return error_mark_node
;
2943 if (TREE_CODE (decl
) == FUNCTION_DECL
2944 && DECL_HIDDEN_FRIEND_P (tmpl
))
2946 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2947 "friend declaration %qD is not visible to "
2948 "explicit specialization", tmpl
))
2949 inform (DECL_SOURCE_LOCATION (tmpl
),
2950 "friend declaration here");
2952 else if (!ctype
&& !is_friend
2953 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2954 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2956 tree gen_tmpl
= most_general_template (tmpl
);
2958 if (explicit_instantiation
)
2960 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2961 is done by do_decl_instantiation later. */
2963 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2964 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2966 if (arg_depth
> parm_depth
)
2968 /* If TMPL is not the most general template (for
2969 example, if TMPL is a friend template that is
2970 injected into namespace scope), then there will
2971 be too many levels of TARGS. Remove some of them
2976 new_targs
= make_tree_vec (parm_depth
);
2977 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2978 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2979 = TREE_VEC_ELT (targs
, i
);
2983 return instantiate_template (tmpl
, targs
, tf_error
);
2986 /* If we thought that the DECL was a member function, but it
2987 turns out to be specializing a static member function,
2988 make DECL a static member function as well. */
2989 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2990 && DECL_STATIC_FUNCTION_P (tmpl
)
2991 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2992 revert_static_member_fn (decl
);
2994 /* If this is a specialization of a member template of a
2995 template class, we want to return the TEMPLATE_DECL, not
2996 the specialization of it. */
2997 if (tsk
== tsk_template
&& !was_template_id
)
2999 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3000 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3001 DECL_INITIAL (result
) = NULL_TREE
;
3005 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3006 DECL_SOURCE_LOCATION (result
)
3007 = DECL_SOURCE_LOCATION (decl
);
3008 /* We want to use the argument list specified in the
3009 definition, not in the original declaration. */
3010 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3011 for (parm
= DECL_ARGUMENTS (result
); parm
;
3012 parm
= DECL_CHAIN (parm
))
3013 DECL_CONTEXT (parm
) = result
;
3015 return register_specialization (tmpl
, gen_tmpl
, targs
,
3019 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3020 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3022 if (was_template_id
)
3023 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3025 /* Inherit default function arguments from the template
3026 DECL is specializing. */
3027 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3028 copy_default_args_to_explicit_spec (decl
);
3030 /* This specialization has the same protection as the
3031 template it specializes. */
3032 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3033 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3035 /* 7.1.1-1 [dcl.stc]
3037 A storage-class-specifier shall not be specified in an
3038 explicit specialization...
3040 The parser rejects these, so unless action is taken here,
3041 explicit function specializations will always appear with
3044 The action recommended by the C++ CWG in response to C++
3045 defect report 605 is to make the storage class and linkage
3046 of the explicit specialization match the templated function:
3048 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3050 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3052 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3053 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3055 /* A concept cannot be specialized. */
3056 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3058 error ("explicit specialization of function concept %qD",
3060 return error_mark_node
;
3063 /* This specialization has the same linkage and visibility as
3064 the function template it specializes. */
3065 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3066 if (! TREE_PUBLIC (decl
))
3068 DECL_INTERFACE_KNOWN (decl
) = 1;
3069 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3071 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3072 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3074 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3075 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3079 /* If DECL is a friend declaration, declared using an
3080 unqualified name, the namespace associated with DECL may
3081 have been set incorrectly. For example, in:
3083 template <typename T> void f(T);
3085 struct S { friend void f<int>(int); }
3088 we will have set the DECL_CONTEXT for the friend
3089 declaration to N, rather than to the global namespace. */
3090 if (DECL_NAMESPACE_SCOPE_P (decl
))
3091 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3093 if (is_friend
&& !have_def
)
3094 /* This is not really a declaration of a specialization.
3095 It's just the name of an instantiation. But, it's not
3096 a request for an instantiation, either. */
3097 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3098 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3099 /* A specialization is not necessarily COMDAT. */
3100 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3101 && DECL_DECLARED_INLINE_P (decl
));
3102 else if (VAR_P (decl
))
3103 DECL_COMDAT (decl
) = false;
3105 /* If this is a full specialization, register it so that we can find
3106 it again. Partial specializations will be registered in
3107 process_partial_specialization. */
3108 if (!processing_template_decl
)
3109 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3112 /* A 'structor should already have clones. */
3113 gcc_assert (decl
== error_mark_node
3114 || variable_template_p (tmpl
)
3115 || !(DECL_CONSTRUCTOR_P (decl
)
3116 || DECL_DESTRUCTOR_P (decl
))
3117 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3124 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3125 parameters. These are represented in the same format used for
3126 DECL_TEMPLATE_PARMS. */
3129 comp_template_parms (const_tree parms1
, const_tree parms2
)
3134 if (parms1
== parms2
)
3137 for (p1
= parms1
, p2
= parms2
;
3138 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3139 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3141 tree t1
= TREE_VALUE (p1
);
3142 tree t2
= TREE_VALUE (p2
);
3145 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3146 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3148 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3151 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3153 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3154 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3156 /* If either of the template parameters are invalid, assume
3157 they match for the sake of error recovery. */
3158 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3161 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3164 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3165 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3166 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3168 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3173 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3174 /* One set of parameters has more parameters lists than the
3181 /* Determine whether PARM is a parameter pack. */
3184 template_parameter_pack_p (const_tree parm
)
3186 /* Determine if we have a non-type template parameter pack. */
3187 if (TREE_CODE (parm
) == PARM_DECL
)
3188 return (DECL_TEMPLATE_PARM_P (parm
)
3189 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3190 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3191 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3193 /* If this is a list of template parameters, we could get a
3194 TYPE_DECL or a TEMPLATE_DECL. */
3195 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3196 parm
= TREE_TYPE (parm
);
3198 /* Otherwise it must be a type template parameter. */
3199 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3200 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3201 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3204 /* Determine if T is a function parameter pack. */
3207 function_parameter_pack_p (const_tree t
)
3209 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3210 return DECL_PACK_P (t
);
3214 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3215 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3218 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3220 if (! primary_func_tmpl_inst
3221 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3222 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3225 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3228 /* Return true iff the function parameter PARAM_DECL was expanded
3229 from the function parameter pack PACK. */
3232 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3234 if (DECL_ARTIFICIAL (param_decl
)
3235 || !function_parameter_pack_p (pack
))
3238 /* The parameter pack and its pack arguments have the same
3240 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3243 /* Determine whether ARGS describes a variadic template args list,
3244 i.e., one that is terminated by a template argument pack. */
3247 template_args_variadic_p (tree args
)
3252 if (args
== NULL_TREE
)
3255 args
= INNERMOST_TEMPLATE_ARGS (args
);
3256 nargs
= TREE_VEC_LENGTH (args
);
3261 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3263 return ARGUMENT_PACK_P (last_parm
);
3266 /* Generate a new name for the parameter pack name NAME (an
3267 IDENTIFIER_NODE) that incorporates its */
3270 make_ith_pack_parameter_name (tree name
, int i
)
3272 /* Munge the name to include the parameter index. */
3273 #define NUMBUF_LEN 128
3274 char numbuf
[NUMBUF_LEN
];
3278 if (name
== NULL_TREE
)
3280 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3281 newname_len
= IDENTIFIER_LENGTH (name
)
3282 + strlen (numbuf
) + 2;
3283 newname
= (char*)alloca (newname_len
);
3284 snprintf (newname
, newname_len
,
3285 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3286 return get_identifier (newname
);
3289 /* Return true if T is a primary function, class or alias template
3293 primary_template_instantiation_p (const_tree t
)
3298 if (TREE_CODE (t
) == FUNCTION_DECL
)
3299 return DECL_LANG_SPECIFIC (t
)
3300 && DECL_TEMPLATE_INSTANTIATION (t
)
3301 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3302 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3303 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3304 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3305 else if (alias_template_specialization_p (t
))
3310 /* Return true if PARM is a template template parameter. */
3313 template_template_parameter_p (const_tree parm
)
3315 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3318 /* Return true iff PARM is a DECL representing a type template
3322 template_type_parameter_p (const_tree parm
)
3325 && (TREE_CODE (parm
) == TYPE_DECL
3326 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3327 && DECL_TEMPLATE_PARM_P (parm
));
3330 /* Return the template parameters of T if T is a
3331 primary template instantiation, NULL otherwise. */
3334 get_primary_template_innermost_parameters (const_tree t
)
3336 tree parms
= NULL
, template_info
= NULL
;
3338 if ((template_info
= get_template_info (t
))
3339 && primary_template_instantiation_p (t
))
3340 parms
= INNERMOST_TEMPLATE_PARMS
3341 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3346 /* Return the template parameters of the LEVELth level from the full list
3347 of template parameters PARMS. */
3350 get_template_parms_at_level (tree parms
, int level
)
3354 || TREE_CODE (parms
) != TREE_LIST
3355 || level
> TMPL_PARMS_DEPTH (parms
))
3358 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3359 if (TMPL_PARMS_DEPTH (p
) == level
)
3365 /* Returns the template arguments of T if T is a template instantiation,
3369 get_template_innermost_arguments (const_tree t
)
3371 tree args
= NULL
, template_info
= NULL
;
3373 if ((template_info
= get_template_info (t
))
3374 && TI_ARGS (template_info
))
3375 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3380 /* Return the argument pack elements of T if T is a template argument pack,
3384 get_template_argument_pack_elems (const_tree t
)
3386 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3387 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3390 return ARGUMENT_PACK_ARGS (t
);
3393 /* True iff FN is a function representing a built-in variadic parameter
3397 builtin_pack_fn_p (tree fn
)
3400 || TREE_CODE (fn
) != FUNCTION_DECL
3401 || !DECL_IS_BUILTIN (fn
))
3404 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3410 /* True iff CALL is a call to a function representing a built-in variadic
3414 builtin_pack_call_p (tree call
)
3416 if (TREE_CODE (call
) != CALL_EXPR
)
3418 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3421 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3424 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3427 tree ohi
= CALL_EXPR_ARG (call
, 0);
3428 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3429 false/*fn*/, true/*int_cst*/);
3431 if (value_dependent_expression_p (hi
))
3435 call
= copy_node (call
);
3436 CALL_EXPR_ARG (call
, 0) = hi
;
3438 tree ex
= make_pack_expansion (call
, complain
);
3439 tree vec
= make_tree_vec (1);
3440 TREE_VEC_ELT (vec
, 0) = ex
;
3445 hi
= cxx_constant_value (hi
);
3446 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3448 /* Calculate the largest value of len that won't make the size of the vec
3449 overflow an int. The compiler will exceed resource limits long before
3450 this, but it seems a decent place to diagnose. */
3451 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3453 if (len
< 0 || len
> max
)
3455 if ((complain
& tf_error
)
3456 && hi
!= error_mark_node
)
3457 error ("argument to __integer_pack must be between 0 and %d", max
);
3458 return error_mark_node
;
3461 tree vec
= make_tree_vec (len
);
3463 for (int i
= 0; i
< len
; ++i
)
3464 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3470 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3474 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3477 if (!builtin_pack_call_p (call
))
3480 tree fn
= CALL_EXPR_FN (call
);
3482 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3483 return expand_integer_pack (call
, args
, complain
, in_decl
);
3488 /* Structure used to track the progress of find_parameter_packs_r. */
3489 struct find_parameter_pack_data
3491 /* TREE_LIST that will contain all of the parameter packs found by
3493 tree
* parameter_packs
;
3495 /* Set of AST nodes that have been visited by the traversal. */
3496 hash_set
<tree
> *visited
;
3498 /* True iff we're making a type pack expansion. */
3499 bool type_pack_expansion_p
;
3502 /* Identifies all of the argument packs that occur in a template
3503 argument and appends them to the TREE_LIST inside DATA, which is a
3504 find_parameter_pack_data structure. This is a subroutine of
3505 make_pack_expansion and uses_parameter_packs. */
3507 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3510 struct find_parameter_pack_data
* ppd
=
3511 (struct find_parameter_pack_data
*)data
;
3512 bool parameter_pack_p
= false;
3514 /* Handle type aliases/typedefs. */
3515 if (TYPE_ALIAS_P (t
))
3517 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3518 cp_walk_tree (&TI_ARGS (tinfo
),
3519 &find_parameter_packs_r
,
3525 /* Identify whether this is a parameter pack or not. */
3526 switch (TREE_CODE (t
))
3528 case TEMPLATE_PARM_INDEX
:
3529 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3530 parameter_pack_p
= true;
3533 case TEMPLATE_TYPE_PARM
:
3534 t
= TYPE_MAIN_VARIANT (t
);
3536 case TEMPLATE_TEMPLATE_PARM
:
3537 /* If the placeholder appears in the decl-specifier-seq of a function
3538 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3539 is a pack expansion, the invented template parameter is a template
3541 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3542 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3543 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3544 parameter_pack_p
= true;
3549 if (DECL_PACK_P (t
))
3551 /* We don't want to walk into the type of a PARM_DECL,
3552 because we don't want to see the type parameter pack. */
3554 parameter_pack_p
= true;
3558 /* Look through a lambda capture proxy to the field pack. */
3560 if (DECL_HAS_VALUE_EXPR_P (t
))
3562 tree v
= DECL_VALUE_EXPR (t
);
3564 &find_parameter_packs_r
,
3568 else if (variable_template_specialization_p (t
))
3570 cp_walk_tree (&DECL_TI_ARGS (t
),
3571 find_parameter_packs_r
,
3578 if (builtin_pack_call_p (t
))
3579 parameter_pack_p
= true;
3583 parameter_pack_p
= true;
3586 /* Not a parameter pack. */
3590 if (parameter_pack_p
)
3592 /* Add this parameter pack to the list. */
3593 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3597 cp_walk_tree (&TYPE_CONTEXT (t
),
3598 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3600 /* This switch statement will return immediately if we don't find a
3602 switch (TREE_CODE (t
))
3604 case TEMPLATE_PARM_INDEX
:
3607 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3608 /* Check the template itself. */
3609 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3610 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3611 /* Check the template arguments. */
3612 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3617 case TEMPLATE_TYPE_PARM
:
3618 case TEMPLATE_TEMPLATE_PARM
:
3625 /* Ignore the declaration of a capture proxy for a parameter pack. */
3626 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3631 if (TYPE_PTRMEMFUNC_P (t
))
3637 if (TYPE_TEMPLATE_INFO (t
))
3638 cp_walk_tree (&TYPE_TI_ARGS (t
),
3639 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3645 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3650 cp_walk_tree (&TREE_TYPE (t
),
3651 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3655 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3660 case TYPE_PACK_EXPANSION
:
3661 case EXPR_PACK_EXPANSION
:
3666 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3671 case IDENTIFIER_NODE
:
3672 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3679 tree fn
= lambda_function (t
);
3680 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3688 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3689 type_pack_expansion_p to false so that any placeholders
3690 within the expression don't get marked as parameter packs. */
3691 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3692 ppd
->type_pack_expansion_p
= false;
3693 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3695 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3707 /* Determines if the expression or type T uses any parameter packs. */
3709 uses_parameter_packs (tree t
)
3711 tree parameter_packs
= NULL_TREE
;
3712 struct find_parameter_pack_data ppd
;
3713 ppd
.parameter_packs
= ¶meter_packs
;
3714 ppd
.visited
= new hash_set
<tree
>;
3715 ppd
.type_pack_expansion_p
= false;
3716 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3718 return parameter_packs
!= NULL_TREE
;
3721 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3722 representation a base-class initializer into a parameter pack
3723 expansion. If all goes well, the resulting node will be an
3724 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3727 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3730 tree parameter_packs
= NULL_TREE
;
3731 bool for_types
= false;
3732 struct find_parameter_pack_data ppd
;
3734 if (!arg
|| arg
== error_mark_node
)
3737 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3739 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3740 class initializer. In this case, the TREE_PURPOSE will be a
3741 _TYPE node (representing the base class expansion we're
3742 initializing) and the TREE_VALUE will be a TREE_LIST
3743 containing the initialization arguments.
3745 The resulting expansion looks somewhat different from most
3746 expansions. Rather than returning just one _EXPANSION, we
3747 return a TREE_LIST whose TREE_PURPOSE is a
3748 TYPE_PACK_EXPANSION containing the bases that will be
3749 initialized. The TREE_VALUE will be identical to the
3750 original TREE_VALUE, which is a list of arguments that will
3751 be passed to each base. We do not introduce any new pack
3752 expansion nodes into the TREE_VALUE (although it is possible
3753 that some already exist), because the TREE_PURPOSE and
3754 TREE_VALUE all need to be expanded together with the same
3755 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3756 resulting TREE_PURPOSE will mention the parameter packs in
3757 both the bases and the arguments to the bases. */
3760 tree parameter_packs
= NULL_TREE
;
3762 /* Determine which parameter packs will be used by the base
3764 ppd
.visited
= new hash_set
<tree
>;
3765 ppd
.parameter_packs
= ¶meter_packs
;
3766 ppd
.type_pack_expansion_p
= true;
3767 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3768 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3771 if (parameter_packs
== NULL_TREE
)
3773 if (complain
& tf_error
)
3774 error ("base initializer expansion %qT contains no parameter packs",
3777 return error_mark_node
;
3780 if (TREE_VALUE (arg
) != void_type_node
)
3782 /* Collect the sets of parameter packs used in each of the
3783 initialization arguments. */
3784 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3786 /* Determine which parameter packs will be expanded in this
3788 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3795 /* Create the pack expansion type for the base type. */
3796 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3797 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3798 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3799 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3801 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3802 they will rarely be compared to anything. */
3803 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3805 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3808 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3811 /* Build the PACK_EXPANSION_* node. */
3813 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3814 : make_node (EXPR_PACK_EXPANSION
);
3815 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3816 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3818 /* Propagate type and const-expression information. */
3819 TREE_TYPE (result
) = TREE_TYPE (arg
);
3820 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3821 /* Mark this read now, since the expansion might be length 0. */
3822 mark_exp_read (arg
);
3825 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3826 they will rarely be compared to anything. */
3827 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3829 /* Determine which parameter packs will be expanded. */
3830 ppd
.parameter_packs
= ¶meter_packs
;
3831 ppd
.visited
= new hash_set
<tree
>;
3832 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3833 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3836 /* Make sure we found some parameter packs. */
3837 if (parameter_packs
== NULL_TREE
)
3839 if (complain
& tf_error
)
3842 error ("expansion pattern %qT contains no argument packs", arg
);
3844 error ("expansion pattern %qE contains no argument packs", arg
);
3846 return error_mark_node
;
3848 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3850 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3855 /* Checks T for any "bare" parameter packs, which have not yet been
3856 expanded, and issues an error if any are found. This operation can
3857 only be done on full expressions or types (e.g., an expression
3858 statement, "if" condition, etc.), because we could have expressions like:
3860 foo(f(g(h(args)))...)
3862 where "args" is a parameter pack. check_for_bare_parameter_packs
3863 should not be called for the subexpressions args, h(args),
3864 g(h(args)), or f(g(h(args))), because we would produce erroneous
3867 Returns TRUE and emits an error if there were bare parameter packs,
3868 returns FALSE otherwise. */
3870 check_for_bare_parameter_packs (tree t
)
3872 tree parameter_packs
= NULL_TREE
;
3873 struct find_parameter_pack_data ppd
;
3875 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3878 /* A lambda might use a parameter pack from the containing context. */
3879 if (current_function_decl
&& LAMBDA_FUNCTION_P (current_function_decl
))
3882 if (TREE_CODE (t
) == TYPE_DECL
)
3885 ppd
.parameter_packs
= ¶meter_packs
;
3886 ppd
.visited
= new hash_set
<tree
>;
3887 ppd
.type_pack_expansion_p
= false;
3888 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3891 if (parameter_packs
)
3893 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3894 error_at (loc
, "parameter packs not expanded with %<...%>:");
3895 while (parameter_packs
)
3897 tree pack
= TREE_VALUE (parameter_packs
);
3898 tree name
= NULL_TREE
;
3900 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3901 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3902 name
= TYPE_NAME (pack
);
3903 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3904 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3905 else if (TREE_CODE (pack
) == CALL_EXPR
)
3906 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3908 name
= DECL_NAME (pack
);
3911 inform (loc
, " %qD", name
);
3913 inform (loc
, " <anonymous>");
3915 parameter_packs
= TREE_CHAIN (parameter_packs
);
3924 /* Expand any parameter packs that occur in the template arguments in
3927 expand_template_argument_pack (tree args
)
3929 if (args
== error_mark_node
)
3930 return error_mark_node
;
3932 tree result_args
= NULL_TREE
;
3933 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3934 int num_result_args
= -1;
3935 int non_default_args_count
= -1;
3937 /* First, determine if we need to expand anything, and the number of
3938 slots we'll need. */
3939 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3941 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3942 if (arg
== NULL_TREE
)
3944 if (ARGUMENT_PACK_P (arg
))
3946 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3947 if (num_result_args
< 0)
3948 num_result_args
= in_arg
+ num_packed
;
3950 num_result_args
+= num_packed
;
3954 if (num_result_args
>= 0)
3959 /* If no expansion is necessary, we're done. */
3960 if (num_result_args
< 0)
3963 /* Expand arguments. */
3964 result_args
= make_tree_vec (num_result_args
);
3965 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3966 non_default_args_count
=
3967 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3968 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3970 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3971 if (ARGUMENT_PACK_P (arg
))
3973 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3974 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3975 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3976 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3977 if (non_default_args_count
> 0)
3978 non_default_args_count
+= num_packed
- 1;
3982 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3986 if (non_default_args_count
>= 0)
3987 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3991 /* Checks if DECL shadows a template parameter.
3993 [temp.local]: A template-parameter shall not be redeclared within its
3994 scope (including nested scopes).
3996 Emits an error and returns TRUE if the DECL shadows a parameter,
3997 returns FALSE otherwise. */
4000 check_template_shadow (tree decl
)
4004 /* If we're not in a template, we can't possibly shadow a template
4006 if (!current_template_parms
)
4009 /* Figure out what we're shadowing. */
4010 decl
= OVL_FIRST (decl
);
4011 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4013 /* If there's no previous binding for this name, we're not shadowing
4014 anything, let alone a template parameter. */
4018 /* If we're not shadowing a template parameter, we're done. Note
4019 that OLDDECL might be an OVERLOAD (or perhaps even an
4020 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4022 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4025 /* We check for decl != olddecl to avoid bogus errors for using a
4026 name inside a class. We check TPFI to avoid duplicate errors for
4027 inline member templates. */
4029 || (DECL_TEMPLATE_PARM_P (decl
)
4030 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4033 /* Don't complain about the injected class name, as we've already
4034 complained about the class itself. */
4035 if (DECL_SELF_REFERENCE_P (decl
))
4038 if (DECL_TEMPLATE_PARM_P (decl
))
4039 error ("declaration of template parameter %q+D shadows "
4040 "template parameter", decl
);
4042 error ("declaration of %q+#D shadows template parameter", decl
);
4043 inform (DECL_SOURCE_LOCATION (olddecl
),
4044 "template parameter %qD declared here", olddecl
);
4048 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4049 ORIG_LEVEL, DECL, and TYPE. */
4052 build_template_parm_index (int index
,
4058 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4059 TEMPLATE_PARM_IDX (t
) = index
;
4060 TEMPLATE_PARM_LEVEL (t
) = level
;
4061 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4062 TEMPLATE_PARM_DECL (t
) = decl
;
4063 TREE_TYPE (t
) = type
;
4064 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4065 TREE_READONLY (t
) = TREE_READONLY (decl
);
4070 /* Find the canonical type parameter for the given template type
4071 parameter. Returns the canonical type parameter, which may be TYPE
4072 if no such parameter existed. */
4075 canonical_type_parameter (tree type
)
4078 int idx
= TEMPLATE_TYPE_IDX (type
);
4079 if (!canonical_template_parms
)
4080 vec_alloc (canonical_template_parms
, idx
+ 1);
4082 if (canonical_template_parms
->length () <= (unsigned) idx
)
4083 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4085 list
= (*canonical_template_parms
)[idx
];
4086 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4087 list
= TREE_CHAIN (list
);
4090 return TREE_VALUE (list
);
4093 (*canonical_template_parms
)[idx
]
4094 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4099 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4100 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4101 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4102 new one is created. */
4105 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4106 tsubst_flags_t complain
)
4108 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4109 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4110 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4111 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4113 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4116 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4117 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4118 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4119 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4120 DECL_ARTIFICIAL (decl
) = 1;
4121 SET_DECL_TEMPLATE_PARM_P (decl
);
4123 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4124 TEMPLATE_PARM_LEVEL (index
) - levels
,
4125 TEMPLATE_PARM_ORIG_LEVEL (index
),
4127 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4128 TEMPLATE_PARM_PARAMETER_PACK (t
)
4129 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4131 /* Template template parameters need this. */
4132 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4134 DECL_TEMPLATE_RESULT (decl
)
4135 = build_decl (DECL_SOURCE_LOCATION (decl
),
4136 TYPE_DECL
, DECL_NAME (decl
), type
);
4137 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4138 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4139 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4143 return TEMPLATE_PARM_DESCENDANTS (index
);
4146 /* Process information from new template parameter PARM and append it
4147 to the LIST being built. This new parameter is a non-type
4148 parameter iff IS_NON_TYPE is true. This new parameter is a
4149 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4153 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4154 bool is_non_type
, bool is_parameter_pack
)
4159 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4160 tree defval
= TREE_PURPOSE (parm
);
4161 tree constr
= TREE_TYPE (parm
);
4165 tree p
= tree_last (list
);
4167 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4170 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4171 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4173 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4181 parm
= TREE_VALUE (parm
);
4183 SET_DECL_TEMPLATE_PARM_P (parm
);
4185 if (TREE_TYPE (parm
) != error_mark_node
)
4189 The top-level cv-qualifiers on the template-parameter are
4190 ignored when determining its type. */
4191 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4192 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4193 TREE_TYPE (parm
) = error_mark_node
;
4194 else if (uses_parameter_packs (TREE_TYPE (parm
))
4195 && !is_parameter_pack
4196 /* If we're in a nested template parameter list, the template
4197 template parameter could be a parameter pack. */
4198 && processing_template_parmlist
== 1)
4200 /* This template parameter is not a parameter pack, but it
4201 should be. Complain about "bare" parameter packs. */
4202 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4204 /* Recover by calling this a parameter pack. */
4205 is_parameter_pack
= true;
4209 /* A template parameter is not modifiable. */
4210 TREE_CONSTANT (parm
) = 1;
4211 TREE_READONLY (parm
) = 1;
4212 decl
= build_decl (parm_loc
,
4213 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4214 TREE_CONSTANT (decl
) = 1;
4215 TREE_READONLY (decl
) = 1;
4216 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4217 = build_template_parm_index (idx
, processing_template_decl
,
4218 processing_template_decl
,
4219 decl
, TREE_TYPE (parm
));
4221 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4222 = is_parameter_pack
;
4227 parm
= TREE_VALUE (TREE_VALUE (parm
));
4229 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4231 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4232 /* This is for distinguishing between real templates and template
4233 template parameters */
4234 TREE_TYPE (parm
) = t
;
4235 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4240 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4241 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4242 decl
= build_decl (parm_loc
,
4243 TYPE_DECL
, parm
, t
);
4246 TYPE_NAME (t
) = decl
;
4247 TYPE_STUB_DECL (t
) = decl
;
4249 TEMPLATE_TYPE_PARM_INDEX (t
)
4250 = build_template_parm_index (idx
, processing_template_decl
,
4251 processing_template_decl
,
4252 decl
, TREE_TYPE (parm
));
4253 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4254 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4256 DECL_ARTIFICIAL (decl
) = 1;
4257 SET_DECL_TEMPLATE_PARM_P (decl
);
4259 /* Build requirements for the type/template parameter.
4260 This must be done after SET_DECL_TEMPLATE_PARM_P or
4261 process_template_parm could fail. */
4262 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4266 /* Build the parameter node linking the parameter declaration,
4267 its default argument (if any), and its constraints (if any). */
4268 parm
= build_tree_list (defval
, parm
);
4269 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4271 return chainon (list
, parm
);
4274 /* The end of a template parameter list has been reached. Process the
4275 tree list into a parameter vector, converting each parameter into a more
4276 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4280 end_template_parm_list (tree parms
)
4284 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4286 /* Pop the dummy parameter level and add the real one. */
4287 current_template_parms
= TREE_CHAIN (current_template_parms
);
4289 current_template_parms
4290 = tree_cons (size_int (processing_template_decl
),
4291 saved_parmlist
, current_template_parms
);
4293 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4295 next
= TREE_CHAIN (parm
);
4296 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4297 TREE_CHAIN (parm
) = NULL_TREE
;
4300 --processing_template_parmlist
;
4302 return saved_parmlist
;
4305 // Explicitly indicate the end of the template parameter list. We assume
4306 // that the current template parameters have been constructed and/or
4307 // managed explicitly, as when creating new template template parameters
4308 // from a shorthand constraint.
4310 end_template_parm_list ()
4312 --processing_template_parmlist
;
4315 /* end_template_decl is called after a template declaration is seen. */
4318 end_template_decl (void)
4320 reset_specialization ();
4322 if (! processing_template_decl
)
4325 /* This matches the pushlevel in begin_template_parm_list. */
4328 --processing_template_decl
;
4329 current_template_parms
= TREE_CHAIN (current_template_parms
);
4332 /* Takes a TREE_LIST representing a template parameter and convert it
4333 into an argument suitable to be passed to the type substitution
4334 functions. Note that If the TREE_LIST contains an error_mark
4335 node, the returned argument is error_mark_node. */
4338 template_parm_to_arg (tree t
)
4342 || TREE_CODE (t
) != TREE_LIST
)
4345 if (error_operand_p (TREE_VALUE (t
)))
4346 return error_mark_node
;
4350 if (TREE_CODE (t
) == TYPE_DECL
4351 || TREE_CODE (t
) == TEMPLATE_DECL
)
4355 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4357 /* Turn this argument into a TYPE_ARGUMENT_PACK
4358 with a single element, which expands T. */
4359 tree vec
= make_tree_vec (1);
4361 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4363 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4365 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4366 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4371 t
= DECL_INITIAL (t
);
4373 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4375 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4376 with a single element, which expands T. */
4377 tree vec
= make_tree_vec (1);
4379 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4381 t
= convert_from_reference (t
);
4382 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4384 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4385 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4388 t
= convert_from_reference (t
);
4393 /* Given a single level of template parameters (a TREE_VEC), return it
4394 as a set of template arguments. */
4397 template_parms_level_to_args (tree parms
)
4399 tree a
= copy_node (parms
);
4400 TREE_TYPE (a
) = NULL_TREE
;
4401 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4402 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4405 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4410 /* Given a set of template parameters, return them as a set of template
4411 arguments. The template parameters are represented as a TREE_VEC, in
4412 the form documented in cp-tree.h for template arguments. */
4415 template_parms_to_args (tree parms
)
4418 tree args
= NULL_TREE
;
4419 int length
= TMPL_PARMS_DEPTH (parms
);
4422 /* If there is only one level of template parameters, we do not
4423 create a TREE_VEC of TREE_VECs. Instead, we return a single
4424 TREE_VEC containing the arguments. */
4426 args
= make_tree_vec (length
);
4428 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4430 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4433 TREE_VEC_ELT (args
, --l
) = a
;
4441 /* Within the declaration of a template, return the currently active
4442 template parameters as an argument TREE_VEC. */
4445 current_template_args (void)
4447 return template_parms_to_args (current_template_parms
);
4450 /* Update the declared TYPE by doing any lookups which were thought to be
4451 dependent, but are not now that we know the SCOPE of the declarator. */
4454 maybe_update_decl_type (tree orig_type
, tree scope
)
4456 tree type
= orig_type
;
4458 if (type
== NULL_TREE
)
4461 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4462 type
= TREE_TYPE (type
);
4464 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4465 && dependent_type_p (type
)
4466 /* Don't bother building up the args in this case. */
4467 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4469 /* tsubst in the args corresponding to the template parameters,
4470 including auto if present. Most things will be unchanged, but
4471 make_typename_type and tsubst_qualified_id will resolve
4472 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4473 tree args
= current_template_args ();
4474 tree auto_node
= type_uses_auto (type
);
4478 tree auto_vec
= make_tree_vec (1);
4479 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4480 args
= add_to_template_args (args
, auto_vec
);
4482 pushed
= push_scope (scope
);
4483 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4488 if (type
== error_mark_node
)
4491 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4493 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4496 type
= TYPE_NAME (type
);
4501 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4502 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4503 the new template is a member template. */
4506 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4508 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4509 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4510 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4511 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4512 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4517 struct template_parm_data
4519 /* The level of the template parameters we are currently
4523 /* The index of the specialization argument we are currently
4527 /* An array whose size is the number of template parameters. The
4528 elements are nonzero if the parameter has been used in any one
4529 of the arguments processed so far. */
4532 /* An array whose size is the number of template arguments. The
4533 elements are nonzero if the argument makes use of template
4534 parameters of this level. */
4535 int* arg_uses_template_parms
;
4538 /* Subroutine of push_template_decl used to see if each template
4539 parameter in a partial specialization is used in the explicit
4540 argument list. If T is of the LEVEL given in DATA (which is
4541 treated as a template_parm_data*), then DATA->PARMS is marked
4545 mark_template_parm (tree t
, void* data
)
4549 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4551 template_parm_level_and_index (t
, &level
, &idx
);
4553 if (level
== tpd
->level
)
4555 tpd
->parms
[idx
] = 1;
4556 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4559 /* In C++17 the type of a non-type argument is a deduced context. */
4560 if (cxx_dialect
>= cxx17
4561 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4562 for_each_template_parm (TREE_TYPE (t
),
4563 &mark_template_parm
,
4566 /*include_nondeduced_p=*/false);
4568 /* Return zero so that for_each_template_parm will continue the
4569 traversal of the tree; we want to mark *every* template parm. */
4573 /* Process the partial specialization DECL. */
4576 process_partial_specialization (tree decl
)
4578 tree type
= TREE_TYPE (decl
);
4579 tree tinfo
= get_template_info (decl
);
4580 tree maintmpl
= TI_TEMPLATE (tinfo
);
4581 tree specargs
= TI_ARGS (tinfo
);
4582 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4583 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4586 int nargs
= TREE_VEC_LENGTH (inner_args
);
4589 bool did_error_intro
= false;
4590 struct template_parm_data tpd
;
4591 struct template_parm_data tpd2
;
4593 gcc_assert (current_template_parms
);
4595 /* A concept cannot be specialized. */
4596 if (flag_concepts
&& variable_concept_p (maintmpl
))
4598 error ("specialization of variable concept %q#D", maintmpl
);
4599 return error_mark_node
;
4602 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4603 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4605 /* We check that each of the template parameters given in the
4606 partial specialization is used in the argument list to the
4607 specialization. For example:
4609 template <class T> struct S;
4610 template <class T> struct S<T*>;
4612 The second declaration is OK because `T*' uses the template
4613 parameter T, whereas
4615 template <class T> struct S<int>;
4617 is no good. Even trickier is:
4628 The S2<T> declaration is actually invalid; it is a
4629 full-specialization. Of course,
4632 struct S2<T (*)(U)>;
4634 or some such would have been OK. */
4635 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4636 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4637 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4639 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4640 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4641 for (i
= 0; i
< nargs
; ++i
)
4643 tpd
.current_arg
= i
;
4644 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4645 &mark_template_parm
,
4648 /*include_nondeduced_p=*/false);
4650 for (i
= 0; i
< ntparms
; ++i
)
4651 if (tpd
.parms
[i
] == 0)
4653 /* One of the template parms was not used in a deduced context in the
4655 if (!did_error_intro
)
4657 error ("template parameters not deducible in "
4658 "partial specialization:");
4659 did_error_intro
= true;
4662 inform (input_location
, " %qD",
4663 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4666 if (did_error_intro
)
4667 return error_mark_node
;
4669 /* [temp.class.spec]
4671 The argument list of the specialization shall not be identical to
4672 the implicit argument list of the primary template. */
4674 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4675 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4677 || !strictly_subsumes (current_template_constraints (),
4678 get_constraints (maintmpl
))))
4681 error ("partial specialization %q+D does not specialize "
4682 "any template arguments", decl
);
4684 error ("partial specialization %q+D does not specialize any "
4685 "template arguments and is not more constrained than", decl
);
4686 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4689 /* A partial specialization that replaces multiple parameters of the
4690 primary template with a pack expansion is less specialized for those
4692 if (nargs
< DECL_NTPARMS (maintmpl
))
4694 error ("partial specialization is not more specialized than the "
4695 "primary template because it replaces multiple parameters "
4696 "with a pack expansion");
4697 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4698 /* Avoid crash in process_partial_specialization. */
4702 /* If we aren't in a dependent class, we can actually try deduction. */
4703 else if (tpd
.level
== 1
4704 /* FIXME we should be able to handle a partial specialization of a
4705 partial instantiation, but currently we can't (c++/41727). */
4706 && TMPL_ARGS_DEPTH (specargs
) == 1
4707 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4709 if (permerror (input_location
, "partial specialization %qD is not "
4710 "more specialized than", decl
))
4711 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4715 /* [temp.class.spec]
4717 A partially specialized non-type argument expression shall not
4718 involve template parameters of the partial specialization except
4719 when the argument expression is a simple identifier.
4721 The type of a template parameter corresponding to a specialized
4722 non-type argument shall not be dependent on a parameter of the
4725 Also, we verify that pack expansions only occur at the
4726 end of the argument list. */
4727 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4729 for (i
= 0; i
< nargs
; ++i
)
4731 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4732 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4733 tree packed_args
= NULL_TREE
;
4736 if (ARGUMENT_PACK_P (arg
))
4738 /* Extract the arguments from the argument pack. We'll be
4739 iterating over these in the following loop. */
4740 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4741 len
= TREE_VEC_LENGTH (packed_args
);
4744 for (j
= 0; j
< len
; j
++)
4747 /* Get the Jth argument in the parameter pack. */
4748 arg
= TREE_VEC_ELT (packed_args
, j
);
4750 if (PACK_EXPANSION_P (arg
))
4752 /* Pack expansions must come at the end of the
4754 if ((packed_args
&& j
< len
- 1)
4755 || (!packed_args
&& i
< nargs
- 1))
4757 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4758 error ("parameter pack argument %qE must be at the "
4759 "end of the template argument list", arg
);
4761 error ("parameter pack argument %qT must be at the "
4762 "end of the template argument list", arg
);
4766 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4767 /* We only care about the pattern. */
4768 arg
= PACK_EXPANSION_PATTERN (arg
);
4770 if (/* These first two lines are the `non-type' bit. */
4772 && TREE_CODE (arg
) != TEMPLATE_DECL
4773 /* This next two lines are the `argument expression is not just a
4774 simple identifier' condition and also the `specialized
4775 non-type argument' bit. */
4776 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4777 && !(REFERENCE_REF_P (arg
)
4778 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4780 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4781 || (packed_args
&& uses_template_parms (arg
)))
4782 error ("template argument %qE involves template parameter(s)",
4786 /* Look at the corresponding template parameter,
4787 marking which template parameters its type depends
4789 tree type
= TREE_TYPE (parm
);
4793 /* We haven't yet initialized TPD2. Do so now. */
4794 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4795 /* The number of parameters here is the number in the
4796 main template, which, as checked in the assertion
4798 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4800 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4803 /* Mark the template parameters. But this time, we're
4804 looking for the template parameters of the main
4805 template, not in the specialization. */
4806 tpd2
.current_arg
= i
;
4807 tpd2
.arg_uses_template_parms
[i
] = 0;
4808 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4809 for_each_template_parm (type
,
4810 &mark_template_parm
,
4813 /*include_nondeduced_p=*/false);
4815 if (tpd2
.arg_uses_template_parms
[i
])
4817 /* The type depended on some template parameters.
4818 If they are fully specialized in the
4819 specialization, that's OK. */
4822 for (j
= 0; j
< nargs
; ++j
)
4823 if (tpd2
.parms
[j
] != 0
4824 && tpd
.arg_uses_template_parms
[j
])
4827 error_n (input_location
, count
,
4828 "type %qT of template argument %qE depends "
4829 "on a template parameter",
4830 "type %qT of template argument %qE depends "
4831 "on template parameters",
4840 /* We should only get here once. */
4841 if (TREE_CODE (decl
) == TYPE_DECL
)
4842 gcc_assert (!COMPLETE_TYPE_P (type
));
4844 // Build the template decl.
4845 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4846 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4847 TREE_TYPE (tmpl
) = type
;
4848 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4849 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4850 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4851 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4853 /* Give template template parms a DECL_CONTEXT of the template
4854 for which they are a parameter. */
4855 for (i
= 0; i
< ntparms
; ++i
)
4857 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4858 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4859 DECL_CONTEXT (parm
) = tmpl
;
4863 /* We didn't register this in check_explicit_specialization so we could
4864 wait until the constraints were set. */
4865 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4867 associate_classtype_constraints (type
);
4869 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4870 = tree_cons (specargs
, tmpl
,
4871 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4872 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4874 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4875 inst
= TREE_CHAIN (inst
))
4877 tree instance
= TREE_VALUE (inst
);
4878 if (TYPE_P (instance
)
4879 ? (COMPLETE_TYPE_P (instance
)
4880 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4881 : DECL_TEMPLATE_INSTANTIATION (instance
))
4883 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4884 tree inst_decl
= (DECL_P (instance
)
4885 ? instance
: TYPE_NAME (instance
));
4888 else if (spec
== error_mark_node
)
4889 permerror (input_location
,
4890 "declaration of %qD ambiguates earlier template "
4891 "instantiation for %qD", decl
, inst_decl
);
4892 else if (TREE_VALUE (spec
) == tmpl
)
4893 permerror (input_location
,
4894 "partial specialization of %qD after instantiation "
4895 "of %qD", decl
, inst_decl
);
4902 /* PARM is a template parameter of some form; return the corresponding
4903 TEMPLATE_PARM_INDEX. */
4906 get_template_parm_index (tree parm
)
4908 if (TREE_CODE (parm
) == PARM_DECL
4909 || TREE_CODE (parm
) == CONST_DECL
)
4910 parm
= DECL_INITIAL (parm
);
4911 else if (TREE_CODE (parm
) == TYPE_DECL
4912 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4913 parm
= TREE_TYPE (parm
);
4914 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4915 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4916 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4917 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4918 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4922 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4923 parameter packs used by the template parameter PARM. */
4926 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4928 /* A type parm can't refer to another parm. */
4929 if (TREE_CODE (parm
) == TYPE_DECL
)
4931 else if (TREE_CODE (parm
) == PARM_DECL
)
4933 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4938 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4940 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4941 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4942 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4945 /* PARM is a template parameter pack. Return any parameter packs used in
4946 its type or the type of any of its template parameters. If there are
4947 any such packs, it will be instantiated into a fixed template parameter
4948 list by partial instantiation rather than be fully deduced. */
4951 fixed_parameter_pack_p (tree parm
)
4953 /* This can only be true in a member template. */
4954 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4956 /* This can only be true for a parameter pack. */
4957 if (!template_parameter_pack_p (parm
))
4959 /* A type parm can't refer to another parm. */
4960 if (TREE_CODE (parm
) == TYPE_DECL
)
4963 tree parameter_packs
= NULL_TREE
;
4964 struct find_parameter_pack_data ppd
;
4965 ppd
.parameter_packs
= ¶meter_packs
;
4966 ppd
.visited
= new hash_set
<tree
>;
4967 ppd
.type_pack_expansion_p
= false;
4969 fixed_parameter_pack_p_1 (parm
, &ppd
);
4972 return parameter_packs
;
4975 /* Check that a template declaration's use of default arguments and
4976 parameter packs is not invalid. Here, PARMS are the template
4977 parameters. IS_PRIMARY is true if DECL is the thing declared by
4978 a primary template. IS_PARTIAL is true if DECL is a partial
4981 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4982 declaration (but not a definition); 1 indicates a declaration, 2
4983 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4984 emitted for extraneous default arguments.
4986 Returns TRUE if there were no errors found, FALSE otherwise. */
4989 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4990 bool is_partial
, int is_friend_decl
)
4993 int last_level_to_check
;
4995 bool no_errors
= true;
4999 A default template-argument shall not be specified in a
5000 function template declaration or a function template definition, nor
5001 in the template-parameter-list of the definition of a member of a
5004 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5005 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5006 /* You can't have a function template declaration in a local
5007 scope, nor you can you define a member of a class template in a
5011 if ((TREE_CODE (decl
) == TYPE_DECL
5013 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5014 || (TREE_CODE (decl
) == FUNCTION_DECL
5015 && LAMBDA_FUNCTION_P (decl
)))
5016 /* A lambda doesn't have an explicit declaration; don't complain
5017 about the parms of the enclosing class. */
5020 if (current_class_type
5021 && !TYPE_BEING_DEFINED (current_class_type
)
5022 && DECL_LANG_SPECIFIC (decl
)
5023 && DECL_DECLARES_FUNCTION_P (decl
)
5024 /* If this is either a friend defined in the scope of the class
5025 or a member function. */
5026 && (DECL_FUNCTION_MEMBER_P (decl
)
5027 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5028 : DECL_FRIEND_CONTEXT (decl
)
5029 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5031 /* And, if it was a member function, it really was defined in
5032 the scope of the class. */
5033 && (!DECL_FUNCTION_MEMBER_P (decl
)
5034 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5035 /* We already checked these parameters when the template was
5036 declared, so there's no need to do it again now. This function
5037 was defined in class scope, but we're processing its body now
5038 that the class is complete. */
5041 /* Core issue 226 (C++0x only): the following only applies to class
5044 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5048 If a template-parameter has a default template-argument, all
5049 subsequent template-parameters shall have a default
5050 template-argument supplied. */
5051 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5053 tree inner_parms
= TREE_VALUE (parm_level
);
5054 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5055 int seen_def_arg_p
= 0;
5058 for (i
= 0; i
< ntparms
; ++i
)
5060 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5062 if (parm
== error_mark_node
)
5065 if (TREE_PURPOSE (parm
))
5067 else if (seen_def_arg_p
5068 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5070 error ("no default argument for %qD", TREE_VALUE (parm
));
5071 /* For better subsequent error-recovery, we indicate that
5072 there should have been a default argument. */
5073 TREE_PURPOSE (parm
) = error_mark_node
;
5076 else if (!is_partial
5078 /* Don't complain about an enclosing partial
5080 && parm_level
== parms
5081 && TREE_CODE (decl
) == TYPE_DECL
5083 && template_parameter_pack_p (TREE_VALUE (parm
))
5084 /* A fixed parameter pack will be partially
5085 instantiated into a fixed length list. */
5086 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5088 /* A primary class template can only have one
5089 parameter pack, at the end of the template
5092 error ("parameter pack %q+D must be at the end of the"
5093 " template parameter list", TREE_VALUE (parm
));
5095 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5103 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5107 /* For an ordinary class template, default template arguments are
5108 allowed at the innermost level, e.g.:
5109 template <class T = int>
5111 but, in a partial specialization, they're not allowed even
5112 there, as we have in [temp.class.spec]:
5114 The template parameter list of a specialization shall not
5115 contain default template argument values.
5117 So, for a partial specialization, or for a function template
5118 (in C++98/C++03), we look at all of them. */
5121 /* But, for a primary class template that is not a partial
5122 specialization we look at all template parameters except the
5124 parms
= TREE_CHAIN (parms
);
5126 /* Figure out what error message to issue. */
5127 if (is_friend_decl
== 2)
5128 msg
= G_("default template arguments may not be used in function template "
5129 "friend re-declaration");
5130 else if (is_friend_decl
)
5131 msg
= G_("default template arguments may not be used in function template "
5132 "friend declarations");
5133 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5134 msg
= G_("default template arguments may not be used in function templates "
5135 "without -std=c++11 or -std=gnu++11");
5136 else if (is_partial
)
5137 msg
= G_("default template arguments may not be used in "
5138 "partial specializations");
5139 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5140 msg
= G_("default argument for template parameter for class enclosing %qD");
5142 /* Per [temp.param]/9, "A default template-argument shall not be
5143 specified in the template-parameter-lists of the definition of
5144 a member of a class template that appears outside of the member's
5145 class.", thus if we aren't handling a member of a class template
5146 there is no need to examine the parameters. */
5149 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5150 /* If we're inside a class definition, there's no need to
5151 examine the parameters to the class itself. On the one
5152 hand, they will be checked when the class is defined, and,
5153 on the other, default arguments are valid in things like:
5154 template <class T = double>
5155 struct S { template <class U> void f(U); };
5156 Here the default argument for `S' has no bearing on the
5157 declaration of `f'. */
5158 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5160 /* Check everything. */
5161 last_level_to_check
= 0;
5163 for (parm_level
= parms
;
5164 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5165 parm_level
= TREE_CHAIN (parm_level
))
5167 tree inner_parms
= TREE_VALUE (parm_level
);
5171 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5172 for (i
= 0; i
< ntparms
; ++i
)
5174 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5177 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5182 if (is_friend_decl
== 2)
5189 /* Clear out the default argument so that we are not
5191 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5195 /* At this point, if we're still interested in issuing messages,
5196 they must apply to classes surrounding the object declared. */
5198 msg
= G_("default argument for template parameter for class "
5205 /* Worker for push_template_decl_real, called via
5206 for_each_template_parm. DATA is really an int, indicating the
5207 level of the parameters we are interested in. If T is a template
5208 parameter of that level, return nonzero. */
5211 template_parm_this_level_p (tree t
, void* data
)
5213 int this_level
= *(int *)data
;
5216 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5217 level
= TEMPLATE_PARM_LEVEL (t
);
5219 level
= TEMPLATE_TYPE_LEVEL (t
);
5220 return level
== this_level
;
5223 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5224 DATA is really an int, indicating the innermost outer level of parameters.
5225 If T is a template parameter of that level or further out, return
5229 template_parm_outer_level (tree t
, void *data
)
5231 int this_level
= *(int *)data
;
5234 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5235 level
= TEMPLATE_PARM_LEVEL (t
);
5237 level
= TEMPLATE_TYPE_LEVEL (t
);
5238 return level
<= this_level
;
5241 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5242 parameters given by current_template_args, or reuses a
5243 previously existing one, if appropriate. Returns the DECL, or an
5244 equivalent one, if it is replaced via a call to duplicate_decls.
5246 If IS_FRIEND is true, DECL is a friend declaration. */
5249 push_template_decl_real (tree decl
, bool is_friend
)
5257 int new_template_p
= 0;
5258 /* True if the template is a member template, in the sense of
5260 bool member_template_p
= false;
5262 if (decl
== error_mark_node
|| !current_template_parms
)
5263 return error_mark_node
;
5265 /* See if this is a partial specialization. */
5266 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5267 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5268 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5270 && DECL_LANG_SPECIFIC (decl
)
5271 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5272 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5274 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5278 /* For a friend, we want the context of the friend function, not
5279 the type of which it is a friend. */
5280 ctx
= CP_DECL_CONTEXT (decl
);
5281 else if (CP_DECL_CONTEXT (decl
)
5282 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5283 /* In the case of a virtual function, we want the class in which
5285 ctx
= CP_DECL_CONTEXT (decl
);
5287 /* Otherwise, if we're currently defining some class, the DECL
5288 is assumed to be a member of the class. */
5289 ctx
= current_scope ();
5291 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5294 if (!DECL_CONTEXT (decl
))
5295 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5297 /* See if this is a primary template. */
5298 if (is_friend
&& ctx
5299 && uses_template_parms_level (ctx
, processing_template_decl
))
5300 /* A friend template that specifies a class context, i.e.
5301 template <typename T> friend void A<T>::f();
5304 else if (TREE_CODE (decl
) == TYPE_DECL
5305 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5308 is_primary
= template_parm_scope_p ();
5312 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5314 if (DECL_CLASS_SCOPE_P (decl
))
5315 member_template_p
= true;
5316 if (TREE_CODE (decl
) == TYPE_DECL
5317 && anon_aggrname_p (DECL_NAME (decl
)))
5319 error ("template class without a name");
5320 return error_mark_node
;
5322 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5324 if (member_template_p
)
5326 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5327 error ("member template %qD may not have virt-specifiers", decl
);
5329 if (DECL_DESTRUCTOR_P (decl
))
5333 A destructor shall not be a member template. */
5334 error ("destructor %qD declared as member template", decl
);
5335 return error_mark_node
;
5337 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5338 && (!prototype_p (TREE_TYPE (decl
))
5339 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5340 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5341 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5342 == void_list_node
)))
5344 /* [basic.stc.dynamic.allocation]
5346 An allocation function can be a function
5347 template. ... Template allocation functions shall
5348 have two or more parameters. */
5349 error ("invalid template declaration of %qD", decl
);
5350 return error_mark_node
;
5353 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5354 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5356 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5357 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5358 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5360 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5361 if (TREE_CODE (t
) == TYPE_DECL
)
5363 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5364 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5367 else if (TREE_CODE (decl
) == TYPE_DECL
5368 && TYPE_DECL_ALIAS_P (decl
))
5369 /* alias-declaration */
5370 gcc_assert (!DECL_ARTIFICIAL (decl
));
5371 else if (VAR_P (decl
))
5372 /* C++14 variable template. */;
5375 error ("template declaration of %q#D", decl
);
5376 return error_mark_node
;
5380 /* Check to see that the rules regarding the use of default
5381 arguments are not being violated. */
5382 check_default_tmpl_args (decl
, current_template_parms
,
5383 is_primary
, is_partial
, /*is_friend_decl=*/0);
5385 /* Ensure that there are no parameter packs in the type of this
5386 declaration that have not been expanded. */
5387 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5389 /* Check each of the arguments individually to see if there are
5390 any bare parameter packs. */
5391 tree type
= TREE_TYPE (decl
);
5392 tree arg
= DECL_ARGUMENTS (decl
);
5393 tree argtype
= TYPE_ARG_TYPES (type
);
5395 while (arg
&& argtype
)
5397 if (!DECL_PACK_P (arg
)
5398 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5400 /* This is a PARM_DECL that contains unexpanded parameter
5401 packs. We have already complained about this in the
5402 check_for_bare_parameter_packs call, so just replace
5403 these types with ERROR_MARK_NODE. */
5404 TREE_TYPE (arg
) = error_mark_node
;
5405 TREE_VALUE (argtype
) = error_mark_node
;
5408 arg
= DECL_CHAIN (arg
);
5409 argtype
= TREE_CHAIN (argtype
);
5412 /* Check for bare parameter packs in the return type and the
5413 exception specifiers. */
5414 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5415 /* Errors were already issued, set return type to int
5416 as the frontend doesn't expect error_mark_node as
5418 TREE_TYPE (type
) = integer_type_node
;
5419 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5420 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5422 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5423 && TYPE_DECL_ALIAS_P (decl
))
5424 ? DECL_ORIGINAL_TYPE (decl
)
5425 : TREE_TYPE (decl
)))
5427 TREE_TYPE (decl
) = error_mark_node
;
5428 return error_mark_node
;
5432 return process_partial_specialization (decl
);
5434 args
= current_template_args ();
5437 || TREE_CODE (ctx
) == FUNCTION_DECL
5438 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5439 || (TREE_CODE (decl
) == TYPE_DECL
5440 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5441 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5443 if (DECL_LANG_SPECIFIC (decl
)
5444 && DECL_TEMPLATE_INFO (decl
)
5445 && DECL_TI_TEMPLATE (decl
))
5446 tmpl
= DECL_TI_TEMPLATE (decl
);
5447 /* If DECL is a TYPE_DECL for a class-template, then there won't
5448 be DECL_LANG_SPECIFIC. The information equivalent to
5449 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5450 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5451 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5452 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5454 /* Since a template declaration already existed for this
5455 class-type, we must be redeclaring it here. Make sure
5456 that the redeclaration is valid. */
5457 redeclare_class_template (TREE_TYPE (decl
),
5458 current_template_parms
,
5459 current_template_constraints ());
5460 /* We don't need to create a new TEMPLATE_DECL; just use the
5461 one we already had. */
5462 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5466 tmpl
= build_template_decl (decl
, current_template_parms
,
5470 if (DECL_LANG_SPECIFIC (decl
)
5471 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5473 /* A specialization of a member template of a template
5475 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5476 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5477 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5483 tree a
, t
, current
, parms
;
5485 tree tinfo
= get_template_info (decl
);
5489 error ("template definition of non-template %q#D", decl
);
5490 return error_mark_node
;
5493 tmpl
= TI_TEMPLATE (tinfo
);
5495 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5496 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5497 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5498 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5502 /* The declaration is a specialization of a member
5503 template, declared outside the class. Therefore, the
5504 innermost template arguments will be NULL, so we
5505 replace them with the arguments determined by the
5506 earlier call to check_explicit_specialization. */
5507 args
= DECL_TI_ARGS (decl
);
5510 = build_template_decl (decl
, current_template_parms
,
5512 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5513 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5514 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5515 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5516 DECL_TEMPLATE_INFO (new_tmpl
)
5517 = build_template_info (tmpl
, args
);
5519 register_specialization (new_tmpl
,
5520 most_general_template (tmpl
),
5526 /* Make sure the template headers we got make sense. */
5528 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5529 i
= TMPL_PARMS_DEPTH (parms
);
5530 if (TMPL_ARGS_DEPTH (args
) != i
)
5532 error ("expected %d levels of template parms for %q#D, got %d",
5533 i
, decl
, TMPL_ARGS_DEPTH (args
));
5534 DECL_INTERFACE_KNOWN (decl
) = 1;
5535 return error_mark_node
;
5538 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5540 a
= TMPL_ARGS_LEVEL (args
, i
);
5541 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5543 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5545 if (current
== decl
)
5546 error ("got %d template parameters for %q#D",
5547 TREE_VEC_LENGTH (a
), decl
);
5549 error ("got %d template parameters for %q#T",
5550 TREE_VEC_LENGTH (a
), current
);
5551 error (" but %d required", TREE_VEC_LENGTH (t
));
5552 /* Avoid crash in import_export_decl. */
5553 DECL_INTERFACE_KNOWN (decl
) = 1;
5554 return error_mark_node
;
5557 if (current
== decl
)
5559 else if (current
== NULL_TREE
)
5560 /* Can happen in erroneous input. */
5563 current
= get_containing_scope (current
);
5566 /* Check that the parms are used in the appropriate qualifying scopes
5567 in the declarator. */
5568 if (!comp_template_args
5570 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5572 error ("template arguments to %qD do not match original "
5573 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5574 if (!uses_template_parms (TI_ARGS (tinfo
)))
5575 inform (input_location
, "use %<template<>%> for"
5576 " an explicit specialization");
5577 /* Avoid crash in import_export_decl. */
5578 DECL_INTERFACE_KNOWN (decl
) = 1;
5579 return error_mark_node
;
5583 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5584 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5586 /* Push template declarations for global functions and types. Note
5587 that we do not try to push a global template friend declared in a
5588 template class; such a thing may well depend on the template
5589 parameters of the class. */
5590 if (new_template_p
&& !ctx
5591 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5593 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5594 if (tmpl
== error_mark_node
)
5595 return error_mark_node
;
5597 /* Hide template friend classes that haven't been declared yet. */
5598 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5600 DECL_ANTICIPATED (tmpl
) = 1;
5601 DECL_FRIEND_P (tmpl
) = 1;
5607 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5609 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5611 /* Give template template parms a DECL_CONTEXT of the template
5612 for which they are a parameter. */
5613 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5614 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5616 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5617 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5618 DECL_CONTEXT (parm
) = tmpl
;
5621 if (TREE_CODE (decl
) == TYPE_DECL
5622 && TYPE_DECL_ALIAS_P (decl
)
5623 && complex_alias_template_p (tmpl
))
5624 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5627 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5628 back to its most general template. If TMPL is a specialization,
5629 ARGS may only have the innermost set of arguments. Add the missing
5630 argument levels if necessary. */
5631 if (DECL_TEMPLATE_INFO (tmpl
))
5632 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5634 info
= build_template_info (tmpl
, args
);
5636 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5637 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5641 retrofit_lang_decl (decl
);
5642 if (DECL_LANG_SPECIFIC (decl
))
5643 DECL_TEMPLATE_INFO (decl
) = info
;
5646 if (flag_implicit_templates
5648 && TREE_PUBLIC (decl
)
5649 && VAR_OR_FUNCTION_DECL_P (decl
))
5650 /* Set DECL_COMDAT on template instantiations; if we force
5651 them to be emitted by explicit instantiation or -frepo,
5652 mark_needed will tell cgraph to do the right thing. */
5653 DECL_COMDAT (decl
) = true;
5655 return DECL_TEMPLATE_RESULT (tmpl
);
5659 push_template_decl (tree decl
)
5661 return push_template_decl_real (decl
, false);
5664 /* FN is an inheriting constructor that inherits from the constructor
5665 template INHERITED; turn FN into a constructor template with a matching
5669 add_inherited_template_parms (tree fn
, tree inherited
)
5672 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5673 inner_parms
= copy_node (inner_parms
);
5675 = tree_cons (size_int (processing_template_decl
+ 1),
5676 inner_parms
, current_template_parms
);
5677 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5678 tree args
= template_parms_to_args (parms
);
5679 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5680 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5681 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5682 DECL_ARTIFICIAL (tmpl
) = true;
5683 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5687 /* Called when a class template TYPE is redeclared with the indicated
5688 template PARMS, e.g.:
5690 template <class T> struct S;
5691 template <class T> struct S {}; */
5694 redeclare_class_template (tree type
, tree parms
, tree cons
)
5700 if (!TYPE_TEMPLATE_INFO (type
))
5702 error ("%qT is not a template type", type
);
5706 tmpl
= TYPE_TI_TEMPLATE (type
);
5707 if (!PRIMARY_TEMPLATE_P (tmpl
))
5708 /* The type is nested in some template class. Nothing to worry
5709 about here; there are no new template parameters for the nested
5715 error ("template specifiers not specified in declaration of %qD",
5720 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5721 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5723 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5725 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5726 "redeclared with %d template parameter",
5727 "redeclared with %d template parameters",
5728 TREE_VEC_LENGTH (parms
));
5729 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5730 "previous declaration %qD used %d template parameter",
5731 "previous declaration %qD used %d template parameters",
5732 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5736 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5743 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5744 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5747 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5748 if (error_operand_p (tmpl_parm
))
5751 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5752 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5753 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5755 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5757 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5758 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5759 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5760 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5761 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5762 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5763 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5764 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5765 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5767 error ("template parameter %q+#D", tmpl_parm
);
5768 error ("redeclared here as %q#D", parm
);
5772 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5774 /* We have in [temp.param]:
5776 A template-parameter may not be given default arguments
5777 by two different declarations in the same scope. */
5778 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5779 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5780 "original definition appeared here");
5784 if (parm_default
!= NULL_TREE
)
5785 /* Update the previous template parameters (which are the ones
5786 that will really count) with the new default value. */
5787 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5788 else if (tmpl_default
!= NULL_TREE
)
5789 /* Update the new parameters, too; they'll be used as the
5790 parameters for any members. */
5791 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5793 /* Give each template template parm in this redeclaration a
5794 DECL_CONTEXT of the template for which they are a parameter. */
5795 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5797 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5798 DECL_CONTEXT (parm
) = tmpl
;
5801 if (TREE_CODE (parm
) == TYPE_DECL
)
5802 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5805 // Cannot redeclare a class template with a different set of constraints.
5806 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5808 error_at (input_location
, "redeclaration %q#D with different "
5809 "constraints", tmpl
);
5810 inform (DECL_SOURCE_LOCATION (tmpl
),
5811 "original declaration appeared here");
5817 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5818 to be used when the caller has already checked
5819 (processing_template_decl
5820 && !instantiation_dependent_expression_p (expr)
5821 && potential_constant_expression (expr))
5822 and cleared processing_template_decl. */
5825 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5827 return tsubst_copy_and_build (expr
,
5830 /*in_decl=*/NULL_TREE
,
5831 /*function_p=*/false,
5832 /*integral_constant_expression_p=*/true);
5835 /* Simplify EXPR if it is a non-dependent expression. Returns the
5836 (possibly simplified) expression. */
5839 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5841 if (expr
== NULL_TREE
)
5844 /* If we're in a template, but EXPR isn't value dependent, simplify
5845 it. We're supposed to treat:
5847 template <typename T> void f(T[1 + 1]);
5848 template <typename T> void f(T[2]);
5850 as two declarations of the same function, for example. */
5851 if (processing_template_decl
5852 && is_nondependent_constant_expression (expr
))
5854 processing_template_decl_sentinel s
;
5855 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5861 instantiate_non_dependent_expr (tree expr
)
5863 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5866 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5867 an uninstantiated expression. */
5870 instantiate_non_dependent_or_null (tree expr
)
5872 if (expr
== NULL_TREE
)
5874 if (processing_template_decl
)
5876 if (!is_nondependent_constant_expression (expr
))
5880 processing_template_decl_sentinel s
;
5881 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5887 /* True iff T is a specialization of a variable template. */
5890 variable_template_specialization_p (tree t
)
5892 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5894 tree tmpl
= DECL_TI_TEMPLATE (t
);
5895 return variable_template_p (tmpl
);
5898 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5899 template declaration, or a TYPE_DECL for an alias declaration. */
5902 alias_type_or_template_p (tree t
)
5906 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5909 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5910 || DECL_ALIAS_TEMPLATE_P (t
));
5913 /* Return TRUE iff T is a specialization of an alias template. */
5916 alias_template_specialization_p (const_tree t
)
5918 /* It's an alias template specialization if it's an alias and its
5919 TYPE_NAME is a specialization of a primary template. */
5920 if (TYPE_ALIAS_P (t
))
5921 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5922 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5927 /* An alias template is complex from a SFINAE perspective if a template-id
5928 using that alias can be ill-formed when the expansion is not, as with
5929 the void_t template. We determine this by checking whether the
5930 expansion for the alias template uses all its template parameters. */
5932 struct uses_all_template_parms_data
5939 uses_all_template_parms_r (tree t
, void *data_
)
5941 struct uses_all_template_parms_data
&data
5942 = *(struct uses_all_template_parms_data
*)data_
;
5943 tree idx
= get_template_parm_index (t
);
5945 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5946 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5951 complex_alias_template_p (const_tree tmpl
)
5953 struct uses_all_template_parms_data data
;
5954 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5955 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5956 data
.level
= TMPL_PARMS_DEPTH (parms
);
5957 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5958 data
.seen
= XALLOCAVEC (bool, len
);
5959 for (int i
= 0; i
< len
; ++i
)
5960 data
.seen
[i
] = false;
5962 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5963 for (int i
= 0; i
< len
; ++i
)
5969 /* Return TRUE iff T is a specialization of a complex alias template with
5970 dependent template-arguments. */
5973 dependent_alias_template_spec_p (const_tree t
)
5975 if (!alias_template_specialization_p (t
))
5978 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5979 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5982 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5983 if (!any_dependent_template_arguments_p (args
))
5989 /* Return the number of innermost template parameters in TMPL. */
5992 num_innermost_template_parms (tree tmpl
)
5994 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5995 return TREE_VEC_LENGTH (parms
);
5998 /* Return either TMPL or another template that it is equivalent to under DR
5999 1286: An alias that just changes the name of a template is equivalent to
6000 the other template. */
6003 get_underlying_template (tree tmpl
)
6005 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6006 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6008 /* Determine if the alias is equivalent to an underlying template. */
6009 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6010 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6014 tree underlying
= TI_TEMPLATE (tinfo
);
6015 if (!PRIMARY_TEMPLATE_P (underlying
)
6016 || (num_innermost_template_parms (tmpl
)
6017 != num_innermost_template_parms (underlying
)))
6020 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6021 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6022 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6025 /* Alias is equivalent. Strip it and repeat. */
6032 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6033 must be a reference-to-function or a pointer-to-function type, as specified
6034 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6035 and check that the resulting function has external linkage. */
6038 convert_nontype_argument_function (tree type
, tree expr
,
6039 tsubst_flags_t complain
)
6043 linkage_kind linkage
;
6045 fn
= instantiate_type (type
, fns
, tf_none
);
6046 if (fn
== error_mark_node
)
6047 return error_mark_node
;
6049 if (value_dependent_expression_p (fn
))
6052 fn_no_ptr
= strip_fnptr_conv (fn
);
6053 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6054 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6055 if (BASELINK_P (fn_no_ptr
))
6056 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6058 /* [temp.arg.nontype]/1
6060 A template-argument for a non-type, non-template template-parameter
6063 -- the address of an object or function with external [C++11: or
6064 internal] linkage. */
6066 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6068 if (complain
& tf_error
)
6070 error ("%qE is not a valid template argument for type %qT",
6072 if (TYPE_PTR_P (type
))
6073 inform (input_location
, "it must be the address of a function "
6074 "with external linkage");
6076 inform (input_location
, "it must be the name of a function with "
6077 "external linkage");
6082 linkage
= decl_linkage (fn_no_ptr
);
6083 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6085 if (complain
& tf_error
)
6087 if (cxx_dialect
>= cxx11
)
6088 error ("%qE is not a valid template argument for type %qT "
6089 "because %qD has no linkage",
6090 expr
, type
, fn_no_ptr
);
6092 error ("%qE is not a valid template argument for type %qT "
6093 "because %qD does not have external linkage",
6094 expr
, type
, fn_no_ptr
);
6100 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6101 fn
= build_address (fn
);
6102 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6103 fn
= build_nop (type
, fn
);
6108 /* Subroutine of convert_nontype_argument.
6109 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6110 Emit an error otherwise. */
6113 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6114 tsubst_flags_t complain
)
6116 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6117 tree orig_expr
= expr
;
6119 if (null_ptr_cst_p (expr
))
6121 if (TREE_CODE (expr
) == PTRMEM_CST
6122 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6123 PTRMEM_CST_CLASS (expr
)))
6125 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6127 if (processing_template_decl
6128 && TREE_CODE (expr
) == ADDR_EXPR
6129 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6131 if (complain
& tf_error
)
6133 error_at (loc
, "%qE is not a valid template argument for type %qT",
6135 if (TREE_CODE (expr
) != PTRMEM_CST
)
6136 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6138 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6143 /* Returns TRUE iff the address of OP is value-dependent.
6145 14.6.2.4 [temp.dep.temp]:
6146 A non-integral non-type template-argument is dependent if its type is
6147 dependent or it has either of the following forms
6150 and contains a nested-name-specifier which specifies a class-name that
6151 names a dependent type.
6153 We generalize this to just say that the address of a member of a
6154 dependent class is value-dependent; the above doesn't cover the
6155 address of a static data member named with an unqualified-id. */
6158 has_value_dependent_address (tree op
)
6160 /* We could use get_inner_reference here, but there's no need;
6161 this is only relevant for template non-type arguments, which
6162 can only be expressed as &id-expression. */
6165 tree ctx
= CP_DECL_CONTEXT (op
);
6166 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6173 /* The next set of functions are used for providing helpful explanatory
6174 diagnostics for failed overload resolution. Their messages should be
6175 indented by two spaces for consistency with the messages in
6179 unify_success (bool /*explain_p*/)
6184 /* Other failure functions should call this one, to provide a single function
6185 for setting a breakpoint on. */
6188 unify_invalid (bool /*explain_p*/)
6194 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6197 inform (input_location
,
6198 " couldn't deduce template parameter %qD", parm
);
6199 return unify_invalid (explain_p
);
6203 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6206 inform (input_location
,
6207 " types %qT and %qT have incompatible cv-qualifiers",
6209 return unify_invalid (explain_p
);
6213 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6216 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6217 return unify_invalid (explain_p
);
6221 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6224 inform (input_location
,
6225 " template parameter %qD is not a parameter pack, but "
6228 return unify_invalid (explain_p
);
6232 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6235 inform (input_location
,
6236 " template argument %qE does not match "
6237 "pointer-to-member constant %qE",
6239 return unify_invalid (explain_p
);
6243 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6246 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6247 return unify_invalid (explain_p
);
6251 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6254 inform (input_location
,
6255 " inconsistent parameter pack deduction with %qT and %qT",
6257 return unify_invalid (explain_p
);
6261 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6266 inform (input_location
,
6267 " deduced conflicting types for parameter %qT (%qT and %qT)",
6268 parm
, first
, second
);
6270 inform (input_location
,
6271 " deduced conflicting values for non-type parameter "
6272 "%qE (%qE and %qE)", parm
, first
, second
);
6274 return unify_invalid (explain_p
);
6278 unify_vla_arg (bool explain_p
, tree arg
)
6281 inform (input_location
,
6282 " variable-sized array type %qT is not "
6283 "a valid template argument",
6285 return unify_invalid (explain_p
);
6289 unify_method_type_error (bool explain_p
, tree arg
)
6292 inform (input_location
,
6293 " member function type %qT is not a valid template argument",
6295 return unify_invalid (explain_p
);
6299 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6304 inform_n (input_location
, wanted
,
6305 " candidate expects at least %d argument, %d provided",
6306 " candidate expects at least %d arguments, %d provided",
6309 inform_n (input_location
, wanted
,
6310 " candidate expects %d argument, %d provided",
6311 " candidate expects %d arguments, %d provided",
6314 return unify_invalid (explain_p
);
6318 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6320 return unify_arity (explain_p
, have
, wanted
);
6324 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6325 bool least_p
= false)
6327 return unify_arity (explain_p
, have
, wanted
, least_p
);
6331 unify_arg_conversion (bool explain_p
, tree to_type
,
6332 tree from_type
, tree arg
)
6335 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6336 " cannot convert %qE (type %qT) to type %qT",
6337 arg
, from_type
, to_type
);
6338 return unify_invalid (explain_p
);
6342 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6343 tree parm
, tree arg
)
6348 case tbr_ambiguous_baseclass
:
6349 inform (input_location
, " %qT is an ambiguous base class of %qT",
6353 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6356 return unify_invalid (explain_p
);
6360 unify_inconsistent_template_template_parameters (bool explain_p
)
6363 inform (input_location
,
6364 " template parameters of a template template argument are "
6365 "inconsistent with other deduced template arguments");
6366 return unify_invalid (explain_p
);
6370 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6373 inform (input_location
,
6374 " can't deduce a template for %qT from non-template type %qT",
6376 return unify_invalid (explain_p
);
6380 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6383 inform (input_location
,
6384 " template argument %qE does not match %qE", arg
, parm
);
6385 return unify_invalid (explain_p
);
6388 /* Attempt to convert the non-type template parameter EXPR to the
6389 indicated TYPE. If the conversion is successful, return the
6390 converted value. If the conversion is unsuccessful, return
6391 NULL_TREE if we issued an error message, or error_mark_node if we
6392 did not. We issue error messages for out-and-out bad template
6393 parameters, but not simply because the conversion failed, since we
6394 might be just trying to do argument deduction. Both TYPE and EXPR
6395 must be non-dependent.
6397 The conversion follows the special rules described in
6398 [temp.arg.nontype], and it is much more strict than an implicit
6401 This function is called twice for each template argument (see
6402 lookup_template_class for a more accurate description of this
6403 problem). This means that we need to handle expressions which
6404 are not valid in a C++ source, but can be created from the
6405 first call (for instance, casts to perform conversions). These
6406 hacks can go away after we fix the double coercion problem. */
6409 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6412 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6413 tree orig_expr
= expr
;
6415 /* Detect immediately string literals as invalid non-type argument.
6416 This special-case is not needed for correctness (we would easily
6417 catch this later), but only to provide better diagnostic for this
6418 common user mistake. As suggested by DR 100, we do not mention
6419 linkage issues in the diagnostic as this is not the point. */
6420 /* FIXME we're making this OK. */
6421 if (TREE_CODE (expr
) == STRING_CST
)
6423 if (complain
& tf_error
)
6424 error ("%qE is not a valid template argument for type %qT "
6425 "because string literals can never be used in this context",
6430 /* Add the ADDR_EXPR now for the benefit of
6431 value_dependent_expression_p. */
6432 if (TYPE_PTROBV_P (type
)
6433 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6435 expr
= decay_conversion (expr
, complain
);
6436 if (expr
== error_mark_node
)
6437 return error_mark_node
;
6440 /* If we are in a template, EXPR may be non-dependent, but still
6441 have a syntactic, rather than semantic, form. For example, EXPR
6442 might be a SCOPE_REF, rather than the VAR_DECL to which the
6443 SCOPE_REF refers. Preserving the qualifying scope is necessary
6444 so that access checking can be performed when the template is
6445 instantiated -- but here we need the resolved form so that we can
6446 convert the argument. */
6447 bool non_dep
= false;
6448 if (TYPE_REF_OBJ_P (type
)
6449 && has_value_dependent_address (expr
))
6450 /* If we want the address and it's value-dependent, don't fold. */;
6451 else if (processing_template_decl
6452 && is_nondependent_constant_expression (expr
))
6454 if (error_operand_p (expr
))
6455 return error_mark_node
;
6456 expr_type
= TREE_TYPE (expr
);
6458 /* If the argument is non-dependent, perform any conversions in
6459 non-dependent context as well. */
6460 processing_template_decl_sentinel
s (non_dep
);
6462 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6464 if (value_dependent_expression_p (expr
))
6465 expr
= canonicalize_expr_argument (expr
, complain
);
6467 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6468 to a non-type argument of "nullptr". */
6469 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6470 expr
= fold_simple (convert (type
, expr
));
6472 /* In C++11, integral or enumeration non-type template arguments can be
6473 arbitrary constant expressions. Pointer and pointer to
6474 member arguments can be general constant expressions that evaluate
6475 to a null value, but otherwise still need to be of a specific form. */
6476 if (cxx_dialect
>= cxx11
)
6478 if (TREE_CODE (expr
) == PTRMEM_CST
)
6479 /* A PTRMEM_CST is already constant, and a valid template
6480 argument for a parameter of pointer to member type, we just want
6481 to leave it in that form rather than lower it to a
6483 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6484 || cxx_dialect
>= cxx17
)
6486 /* C++17: A template-argument for a non-type template-parameter shall
6487 be a converted constant expression (8.20) of the type of the
6488 template-parameter. */
6489 expr
= build_converted_constant_expr (type
, expr
, complain
);
6490 if (expr
== error_mark_node
)
6491 return error_mark_node
;
6492 expr
= maybe_constant_value (expr
);
6493 expr
= convert_from_reference (expr
);
6495 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6497 tree folded
= maybe_constant_value (expr
);
6498 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6499 : null_member_pointer_value_p (folded
))
6504 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6505 expr
= mark_lvalue_use (expr
);
6507 expr
= mark_rvalue_use (expr
);
6509 /* HACK: Due to double coercion, we can get a
6510 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6511 which is the tree that we built on the first call (see
6512 below when coercing to reference to object or to reference to
6513 function). We just strip everything and get to the arg.
6514 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6516 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6518 tree probe_type
, probe
= expr
;
6519 if (REFERENCE_REF_P (probe
))
6520 probe
= TREE_OPERAND (probe
, 0);
6521 probe_type
= TREE_TYPE (probe
);
6522 if (TREE_CODE (probe
) == NOP_EXPR
)
6524 /* ??? Maybe we could use convert_from_reference here, but we
6525 would need to relax its constraints because the NOP_EXPR
6526 could actually change the type to something more cv-qualified,
6527 and this is not folded by convert_from_reference. */
6528 tree addr
= TREE_OPERAND (probe
, 0);
6529 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6530 && TREE_CODE (addr
) == ADDR_EXPR
6531 && TYPE_PTR_P (TREE_TYPE (addr
))
6532 && (same_type_ignoring_top_level_qualifiers_p
6533 (TREE_TYPE (probe_type
),
6534 TREE_TYPE (TREE_TYPE (addr
)))))
6536 expr
= TREE_OPERAND (addr
, 0);
6537 expr_type
= TREE_TYPE (probe_type
);
6542 /* [temp.arg.nontype]/5, bullet 1
6544 For a non-type template-parameter of integral or enumeration type,
6545 integral promotions (_conv.prom_) and integral conversions
6546 (_conv.integral_) are applied. */
6547 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6549 if (cxx_dialect
< cxx11
)
6551 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6552 t
= maybe_constant_value (t
);
6553 if (t
!= error_mark_node
)
6557 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6558 return error_mark_node
;
6560 /* Notice that there are constant expressions like '4 % 0' which
6561 do not fold into integer constants. */
6562 if (TREE_CODE (expr
) != INTEGER_CST
6563 && !value_dependent_expression_p (expr
))
6565 if (complain
& tf_error
)
6567 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6568 if (!require_potential_constant_expression (expr
))
6569 expr
= error_mark_node
;
6571 expr
= cxx_constant_value (expr
);
6572 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6573 inform (loc
, "in template argument for type %qT ", type
);
6574 if (expr
== error_mark_node
)
6576 /* else cxx_constant_value complained but gave us
6577 a real constant, so go ahead. */
6578 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6584 /* Avoid typedef problems. */
6585 if (TREE_TYPE (expr
) != type
)
6586 expr
= fold_convert (type
, expr
);
6588 /* [temp.arg.nontype]/5, bullet 2
6590 For a non-type template-parameter of type pointer to object,
6591 qualification conversions (_conv.qual_) and the array-to-pointer
6592 conversion (_conv.array_) are applied. */
6593 else if (TYPE_PTROBV_P (type
))
6595 tree decayed
= expr
;
6597 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6598 decay_conversion or an explicit cast. If it's a problematic cast,
6599 we'll complain about it below. */
6600 if (TREE_CODE (expr
) == NOP_EXPR
)
6604 if (TREE_CODE (probe
) == ADDR_EXPR
6605 && TYPE_PTR_P (TREE_TYPE (probe
)))
6608 expr_type
= TREE_TYPE (expr
);
6612 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6614 A template-argument for a non-type, non-template template-parameter
6615 shall be one of: [...]
6617 -- the name of a non-type template-parameter;
6618 -- the address of an object or function with external linkage, [...]
6619 expressed as "& id-expression" where the & is optional if the name
6620 refers to a function or array, or if the corresponding
6621 template-parameter is a reference.
6623 Here, we do not care about functions, as they are invalid anyway
6624 for a parameter of type pointer-to-object. */
6626 if (value_dependent_expression_p (expr
))
6627 /* Non-type template parameters are OK. */
6629 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6630 /* Null pointer values are OK in C++11. */;
6631 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6635 if (complain
& tf_error
)
6636 error ("%qD is not a valid template argument "
6637 "because %qD is a variable, not the address of "
6638 "a variable", orig_expr
, expr
);
6641 if (POINTER_TYPE_P (expr_type
))
6643 if (complain
& tf_error
)
6644 error ("%qE is not a valid template argument for %qT "
6645 "because it is not the address of a variable",
6649 /* Other values, like integer constants, might be valid
6650 non-type arguments of some other type. */
6651 return error_mark_node
;
6655 tree decl
= TREE_OPERAND (expr
, 0);
6659 if (complain
& tf_error
)
6660 error ("%qE is not a valid template argument of type %qT "
6661 "because %qE is not a variable", orig_expr
, type
, decl
);
6664 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6666 if (complain
& tf_error
)
6667 error ("%qE is not a valid template argument of type %qT "
6668 "because %qD does not have external linkage",
6669 orig_expr
, type
, decl
);
6672 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6673 && decl_linkage (decl
) == lk_none
)
6675 if (complain
& tf_error
)
6676 error ("%qE is not a valid template argument of type %qT "
6677 "because %qD has no linkage", orig_expr
, type
, decl
);
6680 /* C++17: For a non-type template-parameter of reference or pointer
6681 type, the value of the constant expression shall not refer to (or
6682 for a pointer type, shall not be the address of):
6683 * a subobject (4.5),
6684 * a temporary object (15.2),
6685 * a string literal (5.13.5),
6686 * the result of a typeid expression (8.2.8), or
6687 * a predefined __func__ variable (11.4.1). */
6688 else if (DECL_ARTIFICIAL (decl
))
6690 if (complain
& tf_error
)
6691 error ("the address of %qD is not a valid template argument",
6695 else if (!same_type_ignoring_top_level_qualifiers_p
6696 (strip_array_types (TREE_TYPE (type
)),
6697 strip_array_types (TREE_TYPE (decl
))))
6699 if (complain
& tf_error
)
6700 error ("the address of the %qT subobject of %qD is not a "
6701 "valid template argument", TREE_TYPE (type
), decl
);
6704 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6706 if (complain
& tf_error
)
6707 error ("the address of %qD is not a valid template argument "
6708 "because it does not have static storage duration",
6716 expr
= perform_qualification_conversions (type
, expr
);
6717 if (expr
== error_mark_node
)
6718 return error_mark_node
;
6720 /* [temp.arg.nontype]/5, bullet 3
6722 For a non-type template-parameter of type reference to object, no
6723 conversions apply. The type referred to by the reference may be more
6724 cv-qualified than the (otherwise identical) type of the
6725 template-argument. The template-parameter is bound directly to the
6726 template-argument, which must be an lvalue. */
6727 else if (TYPE_REF_OBJ_P (type
))
6729 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6731 return error_mark_node
;
6733 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6735 if (complain
& tf_error
)
6736 error ("%qE is not a valid template argument for type %qT "
6737 "because of conflicts in cv-qualification", expr
, type
);
6741 if (!lvalue_p (expr
))
6743 if (complain
& tf_error
)
6744 error ("%qE is not a valid template argument for type %qT "
6745 "because it is not an lvalue", expr
, type
);
6749 /* [temp.arg.nontype]/1
6751 A template-argument for a non-type, non-template template-parameter
6752 shall be one of: [...]
6754 -- the address of an object or function with external linkage. */
6755 if (INDIRECT_REF_P (expr
)
6756 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6758 expr
= TREE_OPERAND (expr
, 0);
6761 if (complain
& tf_error
)
6762 error ("%q#D is not a valid template argument for type %qT "
6763 "because a reference variable does not have a constant "
6764 "address", expr
, type
);
6769 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6770 && value_dependent_expression_p (expr
))
6771 /* OK, dependent reference. We don't want to ask whether a DECL is
6772 itself value-dependent, since what we want here is its address. */;
6777 if (complain
& tf_error
)
6778 error ("%qE is not a valid template argument for type %qT "
6779 "because it is not an object with linkage",
6784 /* DR 1155 allows internal linkage in C++11 and up. */
6785 linkage_kind linkage
= decl_linkage (expr
);
6786 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6788 if (complain
& tf_error
)
6789 error ("%qE is not a valid template argument for type %qT "
6790 "because object %qD does not have linkage",
6795 expr
= build_address (expr
);
6798 if (!same_type_p (type
, TREE_TYPE (expr
)))
6799 expr
= build_nop (type
, expr
);
6801 /* [temp.arg.nontype]/5, bullet 4
6803 For a non-type template-parameter of type pointer to function, only
6804 the function-to-pointer conversion (_conv.func_) is applied. If the
6805 template-argument represents a set of overloaded functions (or a
6806 pointer to such), the matching function is selected from the set
6808 else if (TYPE_PTRFN_P (type
))
6810 /* If the argument is a template-id, we might not have enough
6811 context information to decay the pointer. */
6812 if (!type_unknown_p (expr_type
))
6814 expr
= decay_conversion (expr
, complain
);
6815 if (expr
== error_mark_node
)
6816 return error_mark_node
;
6819 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6820 /* Null pointer values are OK in C++11. */
6821 return perform_qualification_conversions (type
, expr
);
6823 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6824 if (!expr
|| expr
== error_mark_node
)
6827 /* [temp.arg.nontype]/5, bullet 5
6829 For a non-type template-parameter of type reference to function, no
6830 conversions apply. If the template-argument represents a set of
6831 overloaded functions, the matching function is selected from the set
6833 else if (TYPE_REFFN_P (type
))
6835 if (TREE_CODE (expr
) == ADDR_EXPR
)
6837 if (complain
& tf_error
)
6839 error ("%qE is not a valid template argument for type %qT "
6840 "because it is a pointer", expr
, type
);
6841 inform (input_location
, "try using %qE instead",
6842 TREE_OPERAND (expr
, 0));
6847 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6848 if (!expr
|| expr
== error_mark_node
)
6851 /* [temp.arg.nontype]/5, bullet 6
6853 For a non-type template-parameter of type pointer to member function,
6854 no conversions apply. If the template-argument represents a set of
6855 overloaded member functions, the matching member function is selected
6856 from the set (_over.over_). */
6857 else if (TYPE_PTRMEMFUNC_P (type
))
6859 expr
= instantiate_type (type
, expr
, tf_none
);
6860 if (expr
== error_mark_node
)
6861 return error_mark_node
;
6863 /* [temp.arg.nontype] bullet 1 says the pointer to member
6864 expression must be a pointer-to-member constant. */
6865 if (!value_dependent_expression_p (expr
)
6866 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6869 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6870 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6871 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6872 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6874 /* [temp.arg.nontype]/5, bullet 7
6876 For a non-type template-parameter of type pointer to data member,
6877 qualification conversions (_conv.qual_) are applied. */
6878 else if (TYPE_PTRDATAMEM_P (type
))
6880 /* [temp.arg.nontype] bullet 1 says the pointer to member
6881 expression must be a pointer-to-member constant. */
6882 if (!value_dependent_expression_p (expr
)
6883 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6886 expr
= perform_qualification_conversions (type
, expr
);
6887 if (expr
== error_mark_node
)
6890 else if (NULLPTR_TYPE_P (type
))
6892 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6894 if (complain
& tf_error
)
6895 error ("%qE is not a valid template argument for type %qT "
6896 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6901 /* A template non-type parameter must be one of the above. */
6905 /* Sanity check: did we actually convert the argument to the
6907 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6908 (type
, TREE_TYPE (expr
)));
6909 return convert_from_reference (expr
);
6912 /* Subroutine of coerce_template_template_parms, which returns 1 if
6913 PARM_PARM and ARG_PARM match using the rule for the template
6914 parameters of template template parameters. Both PARM and ARG are
6915 template parameters; the rest of the arguments are the same as for
6916 coerce_template_template_parms.
6919 coerce_template_template_parm (tree parm
,
6921 tsubst_flags_t complain
,
6925 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6926 || parm
== NULL_TREE
|| error_operand_p (parm
))
6929 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6932 switch (TREE_CODE (parm
))
6935 /* We encounter instantiations of templates like
6936 template <template <template <class> class> class TT>
6939 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6940 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6942 if (!coerce_template_template_parms
6943 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6949 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6950 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6951 /* Argument is a parameter pack but parameter is not. */
6956 /* The tsubst call is used to handle cases such as
6958 template <int> class C {};
6959 template <class T, template <T> class TT> class D {};
6962 i.e. the parameter list of TT depends on earlier parameters. */
6963 if (!uses_template_parms (TREE_TYPE (arg
)))
6965 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6966 if (!uses_template_parms (t
)
6967 && !same_type_p (t
, TREE_TYPE (arg
)))
6971 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6972 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6973 /* Argument is a parameter pack but parameter is not. */
6985 /* Coerce template argument list ARGLIST for use with template
6986 template-parameter TEMPL. */
6989 coerce_template_args_for_ttp (tree templ
, tree arglist
,
6990 tsubst_flags_t complain
)
6992 /* Consider an example where a template template parameter declared as
6994 template <class T, class U = std::allocator<T> > class TT
6996 The template parameter level of T and U are one level larger than
6997 of TT. To proper process the default argument of U, say when an
6998 instantiation `TT<int>' is seen, we need to build the full
6999 arguments containing {int} as the innermost level. Outer levels,
7000 available when not appearing as default template argument, can be
7001 obtained from the arguments of the enclosing template.
7003 Suppose that TT is later substituted with std::vector. The above
7004 instantiation is `TT<int, std::allocator<T> >' with TT at
7005 level 1, and T at level 2, while the template arguments at level 1
7006 becomes {std::vector} and the inner level 2 is {int}. */
7008 tree outer
= DECL_CONTEXT (templ
);
7011 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7012 /* We want arguments for the partial specialization, not arguments for
7013 the primary template. */
7014 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7016 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7018 else if (current_template_parms
)
7020 /* This is an argument of the current template, so we haven't set
7021 DECL_CONTEXT yet. */
7022 tree relevant_template_parms
;
7024 /* Parameter levels that are greater than the level of the given
7025 template template parm are irrelevant. */
7026 relevant_template_parms
= current_template_parms
;
7027 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7028 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7029 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7031 outer
= template_parms_to_args (relevant_template_parms
);
7035 arglist
= add_to_template_args (outer
, arglist
);
7037 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7038 return coerce_template_parms (parmlist
, arglist
, templ
,
7040 /*require_all_args=*/true,
7041 /*use_default_args=*/true);
7044 /* A cache of template template parameters with match-all default
7046 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7048 store_defaulted_ttp (tree v
, tree t
)
7050 if (!defaulted_ttp_cache
)
7051 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7052 defaulted_ttp_cache
->put (v
, t
);
7055 lookup_defaulted_ttp (tree v
)
7057 if (defaulted_ttp_cache
)
7058 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7063 /* T is a bound template template-parameter. Copy its arguments into default
7064 arguments of the template template-parameter's template parameters. */
7067 add_defaults_to_ttp (tree otmpl
)
7069 if (tree c
= lookup_defaulted_ttp (otmpl
))
7072 tree ntmpl
= copy_node (otmpl
);
7074 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7075 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7076 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7077 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7078 TYPE_NAME (ntype
) = ntmpl
;
7079 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7081 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7082 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7083 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7084 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7086 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7087 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7088 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7089 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7090 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7092 tree o
= TREE_VEC_ELT (vec
, i
);
7093 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7095 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7096 TREE_PURPOSE (n
) = any_targ_node
;
7100 store_defaulted_ttp (otmpl
, ntmpl
);
7104 /* ARG is a bound potential template template-argument, and PARGS is a list
7105 of arguments for the corresponding template template-parameter. Adjust
7106 PARGS as appropriate for application to ARG's template, and if ARG is a
7107 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7108 arguments to the template template parameter. */
7111 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7113 ++processing_template_decl
;
7114 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7115 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7117 /* When comparing two template template-parameters in partial ordering,
7118 rewrite the one currently being used as an argument to have default
7119 arguments for all parameters. */
7120 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7121 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7122 if (pargs
!= error_mark_node
)
7123 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7124 TYPE_TI_ARGS (arg
));
7129 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7130 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7131 /*require_all*/true,
7132 /*use_default*/true);
7134 --processing_template_decl
;
7138 /* Subroutine of unify for the case when PARM is a
7139 BOUND_TEMPLATE_TEMPLATE_PARM. */
7142 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7145 tree parmvec
= TYPE_TI_ARGS (parm
);
7146 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7148 /* The template template parm might be variadic and the argument
7149 not, so flatten both argument lists. */
7150 parmvec
= expand_template_argument_pack (parmvec
);
7151 argvec
= expand_template_argument_pack (argvec
);
7155 /* In keeping with P0522R0, adjust P's template arguments
7156 to apply to A's template; then flatten it again. */
7157 tree nparmvec
= parmvec
;
7158 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7159 nparmvec
= expand_template_argument_pack (nparmvec
);
7161 if (unify (tparms
, targs
, nparmvec
, argvec
,
7162 UNIFY_ALLOW_NONE
, explain_p
))
7165 /* If the P0522 adjustment eliminated a pack expansion, deduce
7168 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7169 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7170 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7175 /* Deduce arguments T, i from TT<T> or TT<i>.
7176 We check each element of PARMVEC and ARGVEC individually
7177 rather than the whole TREE_VEC since they can have
7178 different number of elements, which is allowed under N2555. */
7180 int len
= TREE_VEC_LENGTH (parmvec
);
7182 /* Check if the parameters end in a pack, making them
7184 int parm_variadic_p
= 0;
7186 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7187 parm_variadic_p
= 1;
7189 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7190 /* If the template argument list of P contains a pack
7191 expansion that is not the last template argument, the
7192 entire template argument list is a non-deduced
7194 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7195 return unify_success (explain_p
);
7197 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7198 return unify_too_few_arguments (explain_p
,
7199 TREE_VEC_LENGTH (argvec
), len
);
7201 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7202 if (unify (tparms
, targs
,
7203 TREE_VEC_ELT (parmvec
, i
),
7204 TREE_VEC_ELT (argvec
, i
),
7205 UNIFY_ALLOW_NONE
, explain_p
))
7209 && unify_pack_expansion (tparms
, targs
,
7212 /*subr=*/true, explain_p
))
7219 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7220 template template parameters. Both PARM_PARMS and ARG_PARMS are
7221 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7224 Consider the example:
7225 template <class T> class A;
7226 template<template <class U> class TT> class B;
7228 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7229 the parameters to A, and OUTER_ARGS contains A. */
7232 coerce_template_template_parms (tree parm_parms
,
7234 tsubst_flags_t complain
,
7238 int nparms
, nargs
, i
;
7242 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7243 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7245 nparms
= TREE_VEC_LENGTH (parm_parms
);
7246 nargs
= TREE_VEC_LENGTH (arg_parms
);
7250 /* P0522R0: A template template-parameter P is at least as specialized as
7251 a template template-argument A if, given the following rewrite to two
7252 function templates, the function template corresponding to P is at
7253 least as specialized as the function template corresponding to A
7254 according to the partial ordering rules for function templates
7255 ([temp.func.order]). Given an invented class template X with the
7256 template parameter list of A (including default arguments):
7258 * Each of the two function templates has the same template parameters,
7259 respectively, as P or A.
7261 * Each function template has a single function parameter whose type is
7262 a specialization of X with template arguments corresponding to the
7263 template parameters from the respective function template where, for
7264 each template parameter PP in the template parameter list of the
7265 function template, a corresponding template argument AA is formed. If
7266 PP declares a parameter pack, then AA is the pack expansion
7267 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7269 If the rewrite produces an invalid type, then P is not at least as
7270 specialized as A. */
7272 /* So coerce P's args to apply to A's parms, and then deduce between A's
7273 args and the converted args. If that succeeds, A is at least as
7274 specialized as P, so they match.*/
7275 tree pargs
= template_parms_level_to_args (parm_parms
);
7276 ++processing_template_decl
;
7277 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7278 /*require_all*/true, /*use_default*/true);
7279 --processing_template_decl
;
7280 if (pargs
!= error_mark_node
)
7282 tree targs
= make_tree_vec (nargs
);
7283 tree aargs
= template_parms_level_to_args (arg_parms
);
7284 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7290 /* Determine whether we have a parameter pack at the end of the
7291 template template parameter's template parameter list. */
7292 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7294 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7296 if (error_operand_p (parm
))
7299 switch (TREE_CODE (parm
))
7303 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7308 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7318 && !(variadic_p
&& nargs
>= nparms
- 1))
7321 /* Check all of the template parameters except the parameter pack at
7322 the end (if any). */
7323 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7325 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7326 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7329 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7330 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7332 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7340 /* Check each of the template parameters in the template
7341 argument against the template parameter pack at the end of
7342 the template template parameter. */
7343 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7346 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7348 for (; i
< nargs
; ++i
)
7350 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7353 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7355 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7364 /* Verifies that the deduced template arguments (in TARGS) for the
7365 template template parameters (in TPARMS) represent valid bindings,
7366 by comparing the template parameter list of each template argument
7367 to the template parameter list of its corresponding template
7368 template parameter, in accordance with DR150. This
7369 routine can only be called after all template arguments have been
7370 deduced. It will return TRUE if all of the template template
7371 parameter bindings are okay, FALSE otherwise. */
7373 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7375 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7378 /* We're dealing with template parms in this process. */
7379 ++processing_template_decl
;
7381 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7383 for (i
= 0; i
< ntparms
; ++i
)
7385 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7386 tree targ
= TREE_VEC_ELT (targs
, i
);
7388 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7390 tree packed_args
= NULL_TREE
;
7393 if (ARGUMENT_PACK_P (targ
))
7395 /* Look inside the argument pack. */
7396 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7397 len
= TREE_VEC_LENGTH (packed_args
);
7400 for (idx
= 0; idx
< len
; ++idx
)
7402 tree targ_parms
= NULL_TREE
;
7405 /* Extract the next argument from the argument
7407 targ
= TREE_VEC_ELT (packed_args
, idx
);
7409 if (PACK_EXPANSION_P (targ
))
7410 /* Look at the pattern of the pack expansion. */
7411 targ
= PACK_EXPANSION_PATTERN (targ
);
7413 /* Extract the template parameters from the template
7415 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7416 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7417 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7418 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7420 /* Verify that we can coerce the template template
7421 parameters from the template argument to the template
7422 parameter. This requires an exact match. */
7424 && !coerce_template_template_parms
7425 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7440 --processing_template_decl
;
7444 /* Since type attributes aren't mangled, we need to strip them from
7445 template type arguments. */
7448 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7450 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7452 bool removed_attributes
= false;
7453 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7454 if (removed_attributes
7455 && (complain
& tf_warning
))
7456 warning (OPT_Wignored_attributes
,
7457 "ignoring attributes on template argument %qT", arg
);
7461 /* And from inside dependent non-type arguments like sizeof(Type). */
7464 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7466 if (!arg
|| arg
== error_mark_node
)
7468 bool removed_attributes
= false;
7469 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7470 if (removed_attributes
7471 && (complain
& tf_warning
))
7472 warning (OPT_Wignored_attributes
,
7473 "ignoring attributes in template argument %qE", arg
);
7477 // A template declaration can be substituted for a constrained
7478 // template template parameter only when the argument is more
7479 // constrained than the parameter.
7481 is_compatible_template_arg (tree parm
, tree arg
)
7483 tree parm_cons
= get_constraints (parm
);
7485 /* For now, allow constrained template template arguments
7486 and unconstrained template template parameters. */
7487 if (parm_cons
== NULL_TREE
)
7490 tree arg_cons
= get_constraints (arg
);
7492 // If the template parameter is constrained, we need to rewrite its
7493 // constraints in terms of the ARG's template parameters. This ensures
7494 // that all of the template parameter types will have the same depth.
7496 // Note that this is only valid when coerce_template_template_parm is
7497 // true for the innermost template parameters of PARM and ARG. In other
7498 // words, because coercion is successful, this conversion will be valid.
7501 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7502 parm_cons
= tsubst_constraint_info (parm_cons
,
7503 INNERMOST_TEMPLATE_ARGS (args
),
7504 tf_none
, NULL_TREE
);
7505 if (parm_cons
== error_mark_node
)
7509 return subsumes (parm_cons
, arg_cons
);
7512 // Convert a placeholder argument into a binding to the original
7513 // parameter. The original parameter is saved as the TREE_TYPE of
7516 convert_wildcard_argument (tree parm
, tree arg
)
7518 TREE_TYPE (arg
) = parm
;
7522 /* Convert the indicated template ARG as necessary to match the
7523 indicated template PARM. Returns the converted ARG, or
7524 error_mark_node if the conversion was unsuccessful. Error and
7525 warning messages are issued under control of COMPLAIN. This
7526 conversion is for the Ith parameter in the parameter list. ARGS is
7527 the full set of template arguments deduced so far. */
7530 convert_template_argument (tree parm
,
7533 tsubst_flags_t complain
,
7539 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7541 if (parm
== error_mark_node
)
7542 return error_mark_node
;
7544 /* Trivially convert placeholders. */
7545 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7546 return convert_wildcard_argument (parm
, arg
);
7548 if (arg
== any_targ_node
)
7551 if (TREE_CODE (arg
) == TREE_LIST
7552 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7554 /* The template argument was the name of some
7555 member function. That's usually
7556 invalid, but static members are OK. In any
7557 case, grab the underlying fields/functions
7558 and issue an error later if required. */
7559 orig_arg
= TREE_VALUE (arg
);
7560 TREE_TYPE (arg
) = unknown_type_node
;
7565 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7566 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7567 || requires_tmpl_type
);
7569 /* When determining whether an argument pack expansion is a template,
7570 look at the pattern. */
7571 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7572 arg
= PACK_EXPANSION_PATTERN (arg
);
7574 /* Deal with an injected-class-name used as a template template arg. */
7575 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7577 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7578 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7580 if (cxx_dialect
>= cxx11
)
7581 /* OK under DR 1004. */;
7582 else if (complain
& tf_warning_or_error
)
7583 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7584 " used as template template argument", TYPE_NAME (arg
));
7585 else if (flag_pedantic_errors
)
7593 ((TREE_CODE (arg
) == TEMPLATE_DECL
7594 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7595 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7596 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7597 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7600 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7601 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7602 arg
= TYPE_STUB_DECL (arg
);
7604 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7606 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7607 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7609 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7611 if (complain
& tf_error
)
7612 error ("invalid use of destructor %qE as a type", orig_arg
);
7613 return error_mark_node
;
7616 permerror (input_location
,
7617 "to refer to a type member of a template parameter, "
7618 "use %<typename %E%>", orig_arg
);
7620 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7621 TREE_OPERAND (arg
, 1),
7627 if (is_type
!= requires_type
)
7631 if (complain
& tf_error
)
7633 error ("type/value mismatch at argument %d in template "
7634 "parameter list for %qD",
7637 inform (input_location
,
7638 " expected a constant of type %qT, got %qT",
7640 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7641 else if (requires_tmpl_type
)
7642 inform (input_location
,
7643 " expected a class template, got %qE", orig_arg
);
7645 inform (input_location
,
7646 " expected a type, got %qE", orig_arg
);
7649 return error_mark_node
;
7651 if (is_tmpl_type
^ requires_tmpl_type
)
7653 if (in_decl
&& (complain
& tf_error
))
7655 error ("type/value mismatch at argument %d in template "
7656 "parameter list for %qD",
7659 inform (input_location
,
7660 " expected a type, got %qT", DECL_NAME (arg
));
7662 inform (input_location
,
7663 " expected a class template, got %qT", orig_arg
);
7665 return error_mark_node
;
7668 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7669 /* We already did the appropriate conversion when packing args. */
7673 if (requires_tmpl_type
)
7675 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7676 /* The number of argument required is not known yet.
7677 Just accept it for now. */
7681 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7684 /* Strip alias templates that are equivalent to another
7686 arg
= get_underlying_template (arg
);
7687 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7689 if (coerce_template_template_parms (parmparm
, argparm
,
7695 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7697 if (val
!= error_mark_node
)
7699 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7700 val
= TREE_TYPE (val
);
7701 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7702 val
= make_pack_expansion (val
, complain
);
7707 if (in_decl
&& (complain
& tf_error
))
7709 error ("type/value mismatch at argument %d in "
7710 "template parameter list for %qD",
7712 inform (input_location
,
7713 " expected a template of type %qD, got %qT",
7717 val
= error_mark_node
;
7720 // Check that the constraints are compatible before allowing the
7722 if (val
!= error_mark_node
)
7723 if (!is_compatible_template_arg (parm
, arg
))
7725 if (in_decl
&& (complain
& tf_error
))
7727 error ("constraint mismatch at argument %d in "
7728 "template parameter list for %qD",
7730 inform (input_location
, " expected %qD but got %qD",
7733 val
= error_mark_node
;
7739 /* We only form one instance of each template specialization.
7740 Therefore, if we use a non-canonical variant (i.e., a
7741 typedef), any future messages referring to the type will use
7742 the typedef, which is confusing if those future uses do not
7743 themselves also use the typedef. */
7745 val
= canonicalize_type_argument (val
, complain
);
7749 tree t
= TREE_TYPE (parm
);
7751 if (tree a
= type_uses_auto (t
))
7753 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7754 if (t
== error_mark_node
)
7755 return error_mark_node
;
7758 t
= tsubst (t
, args
, complain
, in_decl
);
7760 if (invalid_nontype_parm_type_p (t
, complain
))
7761 return error_mark_node
;
7763 if (!type_dependent_expression_p (orig_arg
)
7764 && !uses_template_parms (t
))
7765 /* We used to call digest_init here. However, digest_init
7766 will report errors, which we don't want when complain
7767 is zero. More importantly, digest_init will try too
7768 hard to convert things: for example, `0' should not be
7769 converted to pointer type at this point according to
7770 the standard. Accepting this is not merely an
7771 extension, since deciding whether or not these
7772 conversions can occur is part of determining which
7773 function template to call, or whether a given explicit
7774 argument specification is valid. */
7775 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7777 val
= canonicalize_expr_argument (orig_arg
, complain
);
7779 if (val
== NULL_TREE
)
7780 val
= error_mark_node
;
7781 else if (val
== error_mark_node
&& (complain
& tf_error
))
7782 error ("could not convert template argument %qE from %qT to %qT",
7783 orig_arg
, TREE_TYPE (orig_arg
), t
);
7785 if (INDIRECT_REF_P (val
))
7787 /* Reject template arguments that are references to built-in
7788 functions with no library fallbacks. */
7789 const_tree inner
= TREE_OPERAND (val
, 0);
7790 const_tree innertype
= TREE_TYPE (inner
);
7792 && TREE_CODE (innertype
) == REFERENCE_TYPE
7793 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7794 && 0 < TREE_OPERAND_LENGTH (inner
)
7795 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7796 return error_mark_node
;
7799 if (TREE_CODE (val
) == SCOPE_REF
)
7801 /* Strip typedefs from the SCOPE_REF. */
7802 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7803 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7805 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7806 QUALIFIED_NAME_IS_TEMPLATE (val
));
7813 /* Coerces the remaining template arguments in INNER_ARGS (from
7814 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7815 Returns the coerced argument pack. PARM_IDX is the position of this
7816 parameter in the template parameter list. ARGS is the original
7817 template argument list. */
7819 coerce_template_parameter_pack (tree parms
,
7827 tsubst_flags_t complain
)
7829 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7830 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7833 tree packed_parms
= NULL_TREE
;
7835 if (arg_idx
> nargs
)
7838 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7840 /* When the template parameter is a non-type template parameter pack
7841 or template template parameter pack whose type or template
7842 parameters use parameter packs, we know exactly how many arguments
7843 we are looking for. Build a vector of the instantiated decls for
7844 these template parameters in PACKED_PARMS. */
7845 /* We can't use make_pack_expansion here because it would interpret a
7846 _DECL as a use rather than a declaration. */
7847 tree decl
= TREE_VALUE (parm
);
7848 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7849 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7850 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7851 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7853 TREE_VEC_LENGTH (args
)--;
7854 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7855 TREE_VEC_LENGTH (args
)++;
7857 if (packed_parms
== error_mark_node
)
7858 return error_mark_node
;
7860 /* If we're doing a partial instantiation of a member template,
7861 verify that all of the types used for the non-type
7862 template parameter pack are, in fact, valid for non-type
7863 template parameters. */
7865 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7867 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7868 for (j
= 0; j
< len
; ++j
)
7870 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7871 if (invalid_nontype_parm_type_p (t
, complain
))
7872 return error_mark_node
;
7874 /* We don't know how many args we have yet, just
7875 use the unconverted ones for now. */
7879 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7881 /* Check if we have a placeholder pack, which indicates we're
7882 in the context of a introduction list. In that case we want
7883 to match this pack to the single placeholder. */
7884 else if (arg_idx
< nargs
7885 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7886 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7888 nargs
= arg_idx
+ 1;
7889 packed_args
= make_tree_vec (1);
7892 packed_args
= make_tree_vec (nargs
- arg_idx
);
7894 /* Convert the remaining arguments, which will be a part of the
7895 parameter pack "parm". */
7896 int first_pack_arg
= arg_idx
;
7897 for (; arg_idx
< nargs
; ++arg_idx
)
7899 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7900 tree actual_parm
= TREE_VALUE (parm
);
7901 int pack_idx
= arg_idx
- first_pack_arg
;
7905 /* Once we've packed as many args as we have types, stop. */
7906 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7908 else if (PACK_EXPANSION_P (arg
))
7909 /* We don't know how many args we have yet, just
7910 use the unconverted ones for now. */
7913 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7916 if (arg
== error_mark_node
)
7918 if (complain
& tf_error
)
7919 error ("template argument %d is invalid", arg_idx
+ 1);
7922 arg
= convert_template_argument (actual_parm
,
7923 arg
, new_args
, complain
, parm_idx
,
7925 if (arg
== error_mark_node
)
7927 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7930 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7931 && TREE_VEC_LENGTH (packed_args
) > 0)
7933 if (complain
& tf_error
)
7934 error ("wrong number of template arguments (%d, should be %d)",
7935 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7936 return error_mark_node
;
7939 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7940 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7941 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7944 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7945 TREE_CONSTANT (argument_pack
) = 1;
7948 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7950 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7951 TREE_VEC_LENGTH (packed_args
));
7952 return argument_pack
;
7955 /* Returns the number of pack expansions in the template argument vector
7959 pack_expansion_args_count (tree args
)
7964 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7966 tree elt
= TREE_VEC_ELT (args
, i
);
7967 if (elt
&& PACK_EXPANSION_P (elt
))
7973 /* Convert all template arguments to their appropriate types, and
7974 return a vector containing the innermost resulting template
7975 arguments. If any error occurs, return error_mark_node. Error and
7976 warning messages are issued under control of COMPLAIN.
7978 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7979 for arguments not specified in ARGS. Otherwise, if
7980 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7981 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7982 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7986 coerce_template_parms (tree parms
,
7989 tsubst_flags_t complain
,
7990 bool require_all_args
,
7991 bool use_default_args
)
7993 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7994 tree orig_inner_args
;
7997 tree new_inner_args
;
7998 int saved_unevaluated_operand
;
7999 int saved_inhibit_evaluation_warnings
;
8001 /* When used as a boolean value, indicates whether this is a
8002 variadic template parameter list. Since it's an int, we can also
8003 subtract it from nparms to get the number of non-variadic
8006 int variadic_args_p
= 0;
8007 int post_variadic_parms
= 0;
8009 /* Likewise for parameters with default arguments. */
8012 if (args
== error_mark_node
)
8013 return error_mark_node
;
8015 nparms
= TREE_VEC_LENGTH (parms
);
8017 /* Determine if there are any parameter packs or default arguments. */
8018 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8020 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8022 ++post_variadic_parms
;
8023 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8025 if (TREE_PURPOSE (parm
))
8029 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8030 /* If there are no parameters that follow a parameter pack, we need to
8031 expand any argument packs so that we can deduce a parameter pack from
8032 some non-packed args followed by an argument pack, as in variadic85.C.
8033 If there are such parameters, we need to leave argument packs intact
8034 so the arguments are assigned properly. This can happen when dealing
8035 with a nested class inside a partial specialization of a class
8036 template, as in variadic92.C, or when deducing a template parameter pack
8037 from a sub-declarator, as in variadic114.C. */
8038 if (!post_variadic_parms
)
8039 inner_args
= expand_template_argument_pack (inner_args
);
8041 /* Count any pack expansion args. */
8042 variadic_args_p
= pack_expansion_args_count (inner_args
);
8044 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8045 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8046 || (nargs
< nparms
- variadic_p
8049 && (!use_default_args
8050 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8051 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8053 if (complain
& tf_error
)
8055 if (variadic_p
|| default_p
)
8057 nparms
-= variadic_p
+ default_p
;
8058 error ("wrong number of template arguments "
8059 "(%d, should be at least %d)", nargs
, nparms
);
8062 error ("wrong number of template arguments "
8063 "(%d, should be %d)", nargs
, nparms
);
8066 inform (DECL_SOURCE_LOCATION (in_decl
),
8067 "provided for %qD", in_decl
);
8070 return error_mark_node
;
8072 /* We can't pass a pack expansion to a non-pack parameter of an alias
8073 template (DR 1430). */
8075 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8076 || concept_template_p (in_decl
))
8078 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8080 if (complain
& tf_error
)
8082 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8084 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8085 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8087 if (PACK_EXPANSION_P (arg
)
8088 && !template_parameter_pack_p (parm
))
8090 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8091 error_at (location_of (arg
),
8092 "pack expansion argument for non-pack parameter "
8093 "%qD of alias template %qD", parm
, in_decl
);
8095 error_at (location_of (arg
),
8096 "pack expansion argument for non-pack parameter "
8097 "%qD of concept %qD", parm
, in_decl
);
8098 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8105 return error_mark_node
;
8108 /* We need to evaluate the template arguments, even though this
8109 template-id may be nested within a "sizeof". */
8110 saved_unevaluated_operand
= cp_unevaluated_operand
;
8111 cp_unevaluated_operand
= 0;
8112 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8113 c_inhibit_evaluation_warnings
= 0;
8114 new_inner_args
= make_tree_vec (nparms
);
8115 new_args
= add_outermost_template_args (args
, new_inner_args
);
8116 int pack_adjust
= 0;
8117 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8122 /* Get the Ith template parameter. */
8123 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8125 if (parm
== error_mark_node
)
8127 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8131 /* Calculate the next argument. */
8132 if (arg_idx
< nargs
)
8133 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8137 if (template_parameter_pack_p (TREE_VALUE (parm
))
8138 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8140 /* Some arguments will be placed in the
8141 template parameter pack PARM. */
8142 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8143 inner_args
, arg_idx
,
8147 if (arg
== NULL_TREE
)
8149 /* We don't know how many args we have yet, just use the
8150 unconverted (and still packed) ones for now. */
8151 new_inner_args
= orig_inner_args
;
8156 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8158 /* Store this argument. */
8159 if (arg
== error_mark_node
)
8162 /* We are done with all of the arguments. */
8167 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8168 arg_idx
+= pack_adjust
;
8175 if (PACK_EXPANSION_P (arg
))
8177 /* "If every valid specialization of a variadic template
8178 requires an empty template parameter pack, the template is
8179 ill-formed, no diagnostic required." So check that the
8180 pattern works with this parameter. */
8181 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8182 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8186 if (conv
== error_mark_node
)
8188 if (complain
& tf_error
)
8189 inform (input_location
, "so any instantiation with a "
8190 "non-empty parameter pack would be ill-formed");
8193 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8194 /* Recover from missing typename. */
8195 TREE_VEC_ELT (inner_args
, arg_idx
)
8196 = make_pack_expansion (conv
, complain
);
8198 /* We don't know how many args we have yet, just
8199 use the unconverted ones for now. */
8200 new_inner_args
= inner_args
;
8205 else if (require_all_args
)
8207 /* There must be a default arg in this case. */
8208 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8210 /* The position of the first default template argument,
8211 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8213 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8214 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8215 arg_idx
- pack_adjust
);
8220 if (arg
== error_mark_node
)
8222 if (complain
& tf_error
)
8223 error ("template argument %d is invalid", arg_idx
+ 1);
8226 /* This only occurs if there was an error in the template
8227 parameter list itself (which we would already have
8228 reported) that we are trying to recover from, e.g., a class
8229 template with a parameter list such as
8230 template<typename..., typename>. */
8233 arg
= convert_template_argument (TREE_VALUE (parm
),
8234 arg
, new_args
, complain
,
8237 if (arg
== error_mark_node
)
8239 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8241 cp_unevaluated_operand
= saved_unevaluated_operand
;
8242 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8244 if (variadic_p
&& arg_idx
< nargs
)
8246 if (complain
& tf_error
)
8248 error ("wrong number of template arguments "
8249 "(%d, should be %d)", nargs
, arg_idx
);
8251 error ("provided for %q+D", in_decl
);
8253 return error_mark_node
;
8257 return error_mark_node
;
8259 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8260 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8261 TREE_VEC_LENGTH (new_inner_args
));
8263 return new_inner_args
;
8266 /* Convert all template arguments to their appropriate types, and
8267 return a vector containing the innermost resulting template
8268 arguments. If any error occurs, return error_mark_node. Error and
8269 warning messages are not issued.
8271 Note that no function argument deduction is performed, and default
8272 arguments are used to fill in unspecified arguments. */
8274 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8276 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8279 /* Convert all template arguments to their appropriate type, and
8280 instantiate default arguments as needed. This returns a vector
8281 containing the innermost resulting template arguments, or
8282 error_mark_node if unsuccessful. */
8284 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8285 tsubst_flags_t complain
)
8287 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8290 /* Like coerce_template_parms. If PARMS represents all template
8291 parameters levels, this function returns a vector of vectors
8292 representing all the resulting argument levels. Note that in this
8293 case, only the innermost arguments are coerced because the
8294 outermost ones are supposed to have been coerced already.
8296 Otherwise, if PARMS represents only (the innermost) vector of
8297 parameters, this function returns a vector containing just the
8298 innermost resulting arguments. */
8301 coerce_innermost_template_parms (tree parms
,
8304 tsubst_flags_t complain
,
8305 bool require_all_args
,
8306 bool use_default_args
)
8308 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8309 int args_depth
= TMPL_ARGS_DEPTH (args
);
8312 if (parms_depth
> 1)
8314 coerced_args
= make_tree_vec (parms_depth
);
8318 for (level
= parms
, cur_depth
= parms_depth
;
8319 parms_depth
> 0 && level
!= NULL_TREE
;
8320 level
= TREE_CHAIN (level
), --cur_depth
)
8323 if (cur_depth
== args_depth
)
8324 l
= coerce_template_parms (TREE_VALUE (level
),
8325 args
, in_decl
, complain
,
8329 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8331 if (l
== error_mark_node
)
8332 return error_mark_node
;
8334 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8338 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8339 args
, in_decl
, complain
,
8342 return coerced_args
;
8345 /* Returns 1 if template args OT and NT are equivalent. */
8348 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8352 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8354 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8357 if (TREE_CODE (nt
) == TREE_VEC
)
8358 /* For member templates */
8359 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8360 else if (PACK_EXPANSION_P (ot
))
8361 return (PACK_EXPANSION_P (nt
)
8362 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8363 PACK_EXPANSION_PATTERN (nt
))
8364 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8365 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8366 else if (ARGUMENT_PACK_P (ot
))
8371 if (!ARGUMENT_PACK_P (nt
))
8374 opack
= ARGUMENT_PACK_ARGS (ot
);
8375 npack
= ARGUMENT_PACK_ARGS (nt
);
8376 len
= TREE_VEC_LENGTH (opack
);
8377 if (TREE_VEC_LENGTH (npack
) != len
)
8379 for (i
= 0; i
< len
; ++i
)
8380 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8381 TREE_VEC_ELT (npack
, i
)))
8385 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8387 else if (TYPE_P (nt
))
8391 /* Don't treat an alias template specialization with dependent
8392 arguments as equivalent to its underlying type when used as a
8393 template argument; we need them to be distinct so that we
8394 substitute into the specialization arguments at instantiation
8395 time. And aliases can't be equivalent without being ==, so
8396 we don't need to look any deeper.
8398 During partial ordering, however, we need to treat them normally so
8399 that we can order uses of the same alias with different
8400 cv-qualification (79960). */
8402 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8405 return same_type_p (ot
, nt
);
8407 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8411 /* Try to treat a template non-type argument that has been converted
8412 to the parameter type as equivalent to one that hasn't yet. */
8413 for (enum tree_code code1
= TREE_CODE (ot
);
8414 CONVERT_EXPR_CODE_P (code1
)
8415 || code1
== NON_LVALUE_EXPR
;
8416 code1
= TREE_CODE (ot
))
8417 ot
= TREE_OPERAND (ot
, 0);
8418 for (enum tree_code code2
= TREE_CODE (nt
);
8419 CONVERT_EXPR_CODE_P (code2
)
8420 || code2
== NON_LVALUE_EXPR
;
8421 code2
= TREE_CODE (nt
))
8422 nt
= TREE_OPERAND (nt
, 0);
8424 return cp_tree_equal (ot
, nt
);
8428 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8429 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8430 NEWARG_PTR with the offending arguments if they are non-NULL. */
8433 comp_template_args (tree oldargs
, tree newargs
,
8434 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8439 if (oldargs
== newargs
)
8442 if (!oldargs
|| !newargs
)
8445 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8448 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8450 tree nt
= TREE_VEC_ELT (newargs
, i
);
8451 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8453 if (! template_args_equal (ot
, nt
, partial_order
))
8455 if (oldarg_ptr
!= NULL
)
8457 if (newarg_ptr
!= NULL
)
8466 comp_template_args_porder (tree oargs
, tree nargs
)
8468 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8472 add_pending_template (tree d
)
8474 tree ti
= (TYPE_P (d
)
8475 ? CLASSTYPE_TEMPLATE_INFO (d
)
8476 : DECL_TEMPLATE_INFO (d
));
8477 struct pending_template
*pt
;
8480 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8483 /* We are called both from instantiate_decl, where we've already had a
8484 tinst_level pushed, and instantiate_template, where we haven't.
8486 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8489 push_tinst_level (d
);
8491 pt
= ggc_alloc
<pending_template
> ();
8493 pt
->tinst
= current_tinst_level
;
8494 if (last_pending_template
)
8495 last_pending_template
->next
= pt
;
8497 pending_templates
= pt
;
8499 last_pending_template
= pt
;
8501 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8508 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8509 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8510 documentation for TEMPLATE_ID_EXPR. */
8513 lookup_template_function (tree fns
, tree arglist
)
8517 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8518 return error_mark_node
;
8520 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8522 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8524 error ("%q#D is not a function template", fns
);
8525 return error_mark_node
;
8528 if (BASELINK_P (fns
))
8530 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8532 BASELINK_FUNCTIONS (fns
),
8537 type
= TREE_TYPE (fns
);
8538 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8539 type
= unknown_type_node
;
8541 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8544 /* Within the scope of a template class S<T>, the name S gets bound
8545 (in build_self_reference) to a TYPE_DECL for the class, not a
8546 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8547 or one of its enclosing classes, and that type is a template,
8548 return the associated TEMPLATE_DECL. Otherwise, the original
8551 Also handle the case when DECL is a TREE_LIST of ambiguous
8552 injected-class-names from different bases. */
8555 maybe_get_template_decl_from_type_decl (tree decl
)
8557 if (decl
== NULL_TREE
)
8560 /* DR 176: A lookup that finds an injected-class-name (10.2
8561 [class.member.lookup]) can result in an ambiguity in certain cases
8562 (for example, if it is found in more than one base class). If all of
8563 the injected-class-names that are found refer to specializations of
8564 the same class template, and if the name is followed by a
8565 template-argument-list, the reference refers to the class template
8566 itself and not a specialization thereof, and is not ambiguous. */
8567 if (TREE_CODE (decl
) == TREE_LIST
)
8569 tree t
, tmpl
= NULL_TREE
;
8570 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8572 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8575 else if (tmpl
!= elt
)
8578 if (tmpl
&& t
== NULL_TREE
)
8584 return (decl
!= NULL_TREE
8585 && DECL_SELF_REFERENCE_P (decl
)
8586 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8587 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8590 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8591 parameters, find the desired type.
8593 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8595 IN_DECL, if non-NULL, is the template declaration we are trying to
8598 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8599 the class we are looking up.
8601 Issue error and warning messages under control of COMPLAIN.
8603 If the template class is really a local class in a template
8604 function, then the FUNCTION_CONTEXT is the function in which it is
8607 ??? Note that this function is currently called *twice* for each
8608 template-id: the first time from the parser, while creating the
8609 incomplete type (finish_template_type), and the second type during the
8610 real instantiation (instantiate_template_class). This is surely something
8611 that we want to avoid. It also causes some problems with argument
8612 coercion (see convert_nontype_argument for more information on this). */
8615 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8616 int entering_scope
, tsubst_flags_t complain
)
8618 tree templ
= NULL_TREE
, parmlist
;
8625 if (identifier_p (d1
))
8627 tree value
= innermost_non_namespace_value (d1
);
8628 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8633 push_decl_namespace (context
);
8634 templ
= lookup_name (d1
);
8635 templ
= maybe_get_template_decl_from_type_decl (templ
);
8637 pop_decl_namespace ();
8640 context
= DECL_CONTEXT (templ
);
8642 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8644 tree type
= TREE_TYPE (d1
);
8646 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8647 an implicit typename for the second A. Deal with it. */
8648 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8649 type
= TREE_TYPE (type
);
8651 if (CLASSTYPE_TEMPLATE_INFO (type
))
8653 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8654 d1
= DECL_NAME (templ
);
8657 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8658 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8660 templ
= TYPE_TI_TEMPLATE (d1
);
8661 d1
= DECL_NAME (templ
);
8663 else if (DECL_TYPE_TEMPLATE_P (d1
))
8666 d1
= DECL_NAME (templ
);
8667 context
= DECL_CONTEXT (templ
);
8669 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8672 d1
= DECL_NAME (templ
);
8675 /* Issue an error message if we didn't find a template. */
8678 if (complain
& tf_error
)
8679 error ("%qT is not a template", d1
);
8680 return error_mark_node
;
8683 if (TREE_CODE (templ
) != TEMPLATE_DECL
8684 /* Make sure it's a user visible template, if it was named by
8686 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8687 && !PRIMARY_TEMPLATE_P (templ
)))
8689 if (complain
& tf_error
)
8691 error ("non-template type %qT used as a template", d1
);
8693 error ("for template declaration %q+D", in_decl
);
8695 return error_mark_node
;
8698 complain
&= ~tf_user
;
8700 /* An alias that just changes the name of a template is equivalent to the
8701 other template, so if any of the arguments are pack expansions, strip
8702 the alias to avoid problems with a pack expansion passed to a non-pack
8703 alias template parameter (DR 1430). */
8704 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8705 templ
= get_underlying_template (templ
);
8707 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8710 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8711 if (arglist2
== error_mark_node
8712 || (!uses_template_parms (arglist2
)
8713 && check_instantiated_args (templ
, arglist2
, complain
)))
8714 return error_mark_node
;
8716 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8721 tree template_type
= TREE_TYPE (templ
);
8724 tree found
= NULL_TREE
;
8727 int is_dependent_type
;
8728 int use_partial_inst_tmpl
= false;
8730 if (template_type
== error_mark_node
)
8731 /* An error occurred while building the template TEMPL, and a
8732 diagnostic has most certainly been emitted for that
8733 already. Let's propagate that error. */
8734 return error_mark_node
;
8736 gen_tmpl
= most_general_template (templ
);
8737 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8738 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8739 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8741 if (arg_depth
== 1 && parm_depth
> 1)
8743 /* We've been given an incomplete set of template arguments.
8746 template <class T> struct S1 {
8747 template <class U> struct S2 {};
8748 template <class U> struct S2<U*> {};
8751 we will be called with an ARGLIST of `U*', but the
8752 TEMPLATE will be `template <class T> template
8753 <class U> struct S1<T>::S2'. We must fill in the missing
8755 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8756 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8757 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8760 /* Now we should have enough arguments. */
8761 gcc_assert (parm_depth
== arg_depth
);
8763 /* From here on, we're only interested in the most general
8766 /* Calculate the BOUND_ARGS. These will be the args that are
8767 actually tsubst'd into the definition to create the
8769 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8771 /*require_all_args=*/true,
8772 /*use_default_args=*/true);
8774 if (arglist
== error_mark_node
)
8775 /* We were unable to bind the arguments. */
8776 return error_mark_node
;
8778 /* In the scope of a template class, explicit references to the
8779 template class refer to the type of the template, not any
8780 instantiation of it. For example, in:
8782 template <class T> class C { void f(C<T>); }
8784 the `C<T>' is just the same as `C'. Outside of the
8785 class, however, such a reference is an instantiation. */
8787 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8788 || currently_open_class (template_type
))
8790 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8792 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8793 return template_type
;
8796 /* If we already have this specialization, return it. */
8797 elt
.tmpl
= gen_tmpl
;
8799 elt
.spec
= NULL_TREE
;
8800 hash
= spec_hasher::hash (&elt
);
8801 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8806 /* If the the template's constraints are not satisfied,
8807 then we cannot form a valid type.
8809 Note that the check is deferred until after the hash
8810 lookup. This prevents redundant checks on previously
8811 instantiated specializations. */
8812 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8814 if (complain
& tf_error
)
8816 error ("template constraint failure");
8817 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8819 return error_mark_node
;
8822 is_dependent_type
= uses_template_parms (arglist
);
8824 /* If the deduced arguments are invalid, then the binding
8826 if (!is_dependent_type
8827 && check_instantiated_args (gen_tmpl
,
8828 INNERMOST_TEMPLATE_ARGS (arglist
),
8830 return error_mark_node
;
8832 if (!is_dependent_type
8833 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8834 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8835 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8837 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8838 DECL_NAME (gen_tmpl
),
8839 /*tag_scope=*/ts_global
);
8843 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8845 if (context
== error_mark_node
)
8846 return error_mark_node
;
8849 context
= global_namespace
;
8851 /* Create the type. */
8852 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8854 /* The user referred to a specialization of an alias
8855 template represented by GEN_TMPL.
8857 [temp.alias]/2 says:
8859 When a template-id refers to the specialization of an
8860 alias template, it is equivalent to the associated
8861 type obtained by substitution of its
8862 template-arguments for the template-parameters in the
8863 type-id of the alias template. */
8865 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8866 /* Note that the call above (by indirectly calling
8867 register_specialization in tsubst_decl) registers the
8868 TYPE_DECL representing the specialization of the alias
8869 template. So next time someone substitutes ARGLIST for
8870 the template parms into the alias template (GEN_TMPL),
8871 she'll get that TYPE_DECL back. */
8873 if (t
== error_mark_node
)
8876 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8878 if (!is_dependent_type
)
8880 set_current_access_from_decl (TYPE_NAME (template_type
));
8881 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8882 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8883 arglist
, complain
, in_decl
),
8884 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8885 arglist
, complain
, in_decl
),
8886 SCOPED_ENUM_P (template_type
), NULL
);
8888 if (t
== error_mark_node
)
8893 /* We don't want to call start_enum for this type, since
8894 the values for the enumeration constants may involve
8895 template parameters. And, no one should be interested
8896 in the enumeration constants for such a type. */
8897 t
= cxx_make_type (ENUMERAL_TYPE
);
8898 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8900 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8901 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8902 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8904 else if (CLASS_TYPE_P (template_type
))
8906 t
= make_class_type (TREE_CODE (template_type
));
8907 CLASSTYPE_DECLARED_CLASS (t
)
8908 = CLASSTYPE_DECLARED_CLASS (template_type
);
8909 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8911 /* A local class. Make sure the decl gets registered properly. */
8912 if (context
== current_function_decl
)
8913 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8915 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8916 /* This instantiation is another name for the primary
8917 template type. Set the TYPE_CANONICAL field
8919 TYPE_CANONICAL (t
) = template_type
;
8920 else if (any_template_arguments_need_structural_equality_p (arglist
))
8921 /* Some of the template arguments require structural
8922 equality testing, so this template class requires
8923 structural equality testing. */
8924 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8929 /* If we called start_enum or pushtag above, this information
8930 will already be set up. */
8933 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8935 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8936 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8937 DECL_SOURCE_LOCATION (type_decl
)
8938 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8941 type_decl
= TYPE_NAME (t
);
8943 if (CLASS_TYPE_P (template_type
))
8945 TREE_PRIVATE (type_decl
)
8946 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8947 TREE_PROTECTED (type_decl
)
8948 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8949 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8951 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8952 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8956 if (OVERLOAD_TYPE_P (t
)
8957 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8959 static const char *tags
[] = {"abi_tag", "may_alias"};
8961 for (unsigned ix
= 0; ix
!= 2; ix
++)
8964 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8968 = tree_cons (TREE_PURPOSE (attributes
),
8969 TREE_VALUE (attributes
),
8970 TYPE_ATTRIBUTES (t
));
8974 /* Let's consider the explicit specialization of a member
8975 of a class template specialization that is implicitly instantiated,
8980 template<class U> struct M {}; //#0
8985 struct S<int>::M<char> //#1
8989 [temp.expl.spec]/4 says this is valid.
8991 In this case, when we write:
8994 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8997 When we encounter #1, we want to store the partial instantiation
8998 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9000 For all cases other than this "explicit specialization of member of a
9001 class template", we just want to store the most general template into
9002 the CLASSTYPE_TI_TEMPLATE of M.
9004 This case of "explicit specialization of member of a class template"
9006 1/ the enclosing class is an instantiation of, and therefore not
9007 the same as, the context of the most general template, and
9008 2/ we aren't looking at the partial instantiation itself, i.e.
9009 the innermost arguments are not the same as the innermost parms of
9010 the most general template.
9012 So it's only when 1/ and 2/ happens that we want to use the partial
9013 instantiation of the member template in lieu of its most general
9016 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9017 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9018 /* the enclosing class must be an instantiation... */
9019 && CLASS_TYPE_P (context
)
9020 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9022 TREE_VEC_LENGTH (arglist
)--;
9023 ++processing_template_decl
;
9024 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9025 tree partial_inst_args
=
9026 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9027 arglist
, complain
, NULL_TREE
);
9028 --processing_template_decl
;
9029 TREE_VEC_LENGTH (arglist
)++;
9030 if (partial_inst_args
== error_mark_node
)
9031 return error_mark_node
;
9032 use_partial_inst_tmpl
=
9033 /*...and we must not be looking at the partial instantiation
9035 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9039 if (!use_partial_inst_tmpl
)
9040 /* This case is easy; there are no member templates involved. */
9044 /* This is a full instantiation of a member template. Find
9045 the partial instantiation of which this is an instance. */
9047 /* Temporarily reduce by one the number of levels in the ARGLIST
9048 so as to avoid comparing the last set of arguments. */
9049 TREE_VEC_LENGTH (arglist
)--;
9050 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9051 TREE_VEC_LENGTH (arglist
)++;
9052 /* FOUND is either a proper class type, or an alias
9053 template specialization. In the later case, it's a
9054 TYPE_DECL, resulting from the substituting of arguments
9055 for parameters in the TYPE_DECL of the alias template
9056 done earlier. So be careful while getting the template
9058 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9060 : (TREE_CODE (found
) == TYPE_DECL
9061 ? DECL_TI_TEMPLATE (found
)
9062 : CLASSTYPE_TI_TEMPLATE (found
)));
9065 // Build template info for the new specialization.
9066 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9069 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9070 entry
= ggc_alloc
<spec_entry
> ();
9074 /* Note this use of the partial instantiation so we can check it
9075 later in maybe_process_partial_specialization. */
9076 DECL_TEMPLATE_INSTANTIATIONS (found
)
9077 = tree_cons (arglist
, t
,
9078 DECL_TEMPLATE_INSTANTIATIONS (found
));
9080 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9081 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9082 /* Now that the type has been registered on the instantiations
9083 list, we set up the enumerators. Because the enumeration
9084 constants may involve the enumeration type itself, we make
9085 sure to register the type first, and then create the
9086 constants. That way, doing tsubst_expr for the enumeration
9087 constants won't result in recursive calls here; we'll find
9088 the instantiation and exit above. */
9089 tsubst_enum (template_type
, t
, arglist
);
9091 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9092 /* If the type makes use of template parameters, the
9093 code that generates debugging information will crash. */
9094 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9096 /* Possibly limit visibility based on template args. */
9097 TREE_PUBLIC (type_decl
) = 1;
9098 determine_visibility (type_decl
);
9100 inherit_targ_abi_tags (t
);
9106 /* Wrapper for lookup_template_class_1. */
9109 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9110 int entering_scope
, tsubst_flags_t complain
)
9113 timevar_push (TV_TEMPLATE_INST
);
9114 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9115 entering_scope
, complain
);
9116 timevar_pop (TV_TEMPLATE_INST
);
9120 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9123 lookup_template_variable (tree templ
, tree arglist
)
9125 /* The type of the expression is NULL_TREE since the template-id could refer
9126 to an explicit or partial specialization. */
9127 tree type
= NULL_TREE
;
9128 if (flag_concepts
&& variable_concept_p (templ
))
9129 /* Except that concepts are always bool. */
9130 type
= boolean_type_node
;
9131 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9134 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9137 finish_template_variable (tree var
, tsubst_flags_t complain
)
9139 tree templ
= TREE_OPERAND (var
, 0);
9140 tree arglist
= TREE_OPERAND (var
, 1);
9142 /* We never want to return a VAR_DECL for a variable concept, since they
9143 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9144 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9145 if (concept_p
&& processing_template_decl
)
9148 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9149 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9151 templ
= most_general_template (templ
);
9152 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9153 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9155 /*use_default*/true);
9157 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9159 if (complain
& tf_error
)
9161 error ("use of invalid variable template %qE", var
);
9162 diagnose_constraints (location_of (var
), templ
, arglist
);
9164 return error_mark_node
;
9167 /* If a template-id refers to a specialization of a variable
9168 concept, then the expression is true if and only if the
9169 concept's constraints are satisfied by the given template
9172 NOTE: This is an extension of Concepts Lite TS that
9173 allows constraints to be used in expressions. */
9176 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9177 return evaluate_variable_concept (decl
, arglist
);
9180 return instantiate_template (templ
, arglist
, complain
);
9183 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9184 TARGS template args, and instantiate it if it's not dependent. */
9187 lookup_and_finish_template_variable (tree templ
, tree targs
,
9188 tsubst_flags_t complain
)
9190 templ
= lookup_template_variable (templ
, targs
);
9191 if (!any_dependent_template_arguments_p (targs
))
9193 templ
= finish_template_variable (templ
, complain
);
9197 return convert_from_reference (templ
);
9206 /* True when we should also visit template parameters that occur in
9207 non-deduced contexts. */
9208 bool include_nondeduced_p
;
9209 hash_set
<tree
> *visited
;
9212 /* Called from for_each_template_parm via walk_tree. */
9215 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9218 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9219 tree_fn_t fn
= pfd
->fn
;
9220 void *data
= pfd
->data
;
9221 tree result
= NULL_TREE
;
9223 #define WALK_SUBTREE(NODE) \
9226 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9227 pfd->include_nondeduced_p, \
9229 if (result) goto out; \
9233 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9237 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9238 WALK_SUBTREE (TYPE_CONTEXT (t
));
9240 switch (TREE_CODE (t
))
9243 if (TYPE_PTRMEMFUNC_P (t
))
9249 if (!TYPE_TEMPLATE_INFO (t
))
9252 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9256 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9257 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9261 /* Since we're not going to walk subtrees, we have to do this
9263 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9267 /* Check the return type. */
9268 WALK_SUBTREE (TREE_TYPE (t
));
9270 /* Check the parameter types. Since default arguments are not
9271 instantiated until they are needed, the TYPE_ARG_TYPES may
9272 contain expressions that involve template parameters. But,
9273 no-one should be looking at them yet. And, once they're
9274 instantiated, they don't contain template parameters, so
9275 there's no point in looking at them then, either. */
9279 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9280 WALK_SUBTREE (TREE_VALUE (parm
));
9282 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9283 want walk_tree walking into them itself. */
9287 if (flag_noexcept_type
)
9289 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9291 WALK_SUBTREE (TREE_PURPOSE (spec
));
9296 case UNDERLYING_TYPE
:
9297 if (pfd
->include_nondeduced_p
9298 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9300 pfd
->include_nondeduced_p
,
9302 return error_mark_node
;
9307 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9308 WALK_SUBTREE (DECL_TI_ARGS (t
));
9313 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9314 WALK_SUBTREE (DECL_INITIAL (t
));
9315 if (DECL_CONTEXT (t
)
9316 && pfd
->include_nondeduced_p
)
9317 WALK_SUBTREE (DECL_CONTEXT (t
));
9320 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9321 /* Record template parameters such as `T' inside `TT<T>'. */
9322 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9325 case TEMPLATE_TEMPLATE_PARM
:
9326 case TEMPLATE_TYPE_PARM
:
9327 case TEMPLATE_PARM_INDEX
:
9328 if (fn
&& (*fn
)(t
, data
))
9335 /* A template template parameter is encountered. */
9336 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9337 WALK_SUBTREE (TREE_TYPE (t
));
9339 /* Already substituted template template parameter */
9344 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9345 partial instantiation. */
9346 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9350 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9351 && pfd
->include_nondeduced_p
)
9352 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9357 /* If there's no type, then this thing must be some expression
9358 involving template parameters. */
9359 if (!fn
&& !TREE_TYPE (t
))
9360 return error_mark_node
;
9365 case IMPLICIT_CONV_EXPR
:
9366 case REINTERPRET_CAST_EXPR
:
9367 case CONST_CAST_EXPR
:
9368 case STATIC_CAST_EXPR
:
9369 case DYNAMIC_CAST_EXPR
:
9373 case PSEUDO_DTOR_EXPR
:
9375 return error_mark_node
;
9384 /* We didn't find any template parameters we liked. */
9389 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9390 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9391 call FN with the parameter and the DATA.
9392 If FN returns nonzero, the iteration is terminated, and
9393 for_each_template_parm returns 1. Otherwise, the iteration
9394 continues. If FN never returns a nonzero value, the value
9395 returned by for_each_template_parm is 0. If FN is NULL, it is
9396 considered to be the function which always returns 1.
9398 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9399 parameters that occur in non-deduced contexts. When false, only
9400 visits those template parameters that can be deduced. */
9403 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9404 hash_set
<tree
> *visited
,
9405 bool include_nondeduced_p
,
9408 struct pair_fn_data pfd
;
9413 pfd
.any_fn
= any_fn
;
9415 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9417 /* Walk the tree. (Conceptually, we would like to walk without
9418 duplicates, but for_each_template_parm_r recursively calls
9419 for_each_template_parm, so we would need to reorganize a fair
9420 bit to use walk_tree_without_duplicates, so we keep our own
9423 pfd
.visited
= visited
;
9425 pfd
.visited
= new hash_set
<tree
>;
9426 result
= cp_walk_tree (&t
,
9427 for_each_template_parm_r
,
9441 /* Returns true if T depends on any template parameter. */
9444 uses_template_parms (tree t
)
9450 int saved_processing_template_decl
;
9452 saved_processing_template_decl
= processing_template_decl
;
9453 if (!saved_processing_template_decl
)
9454 processing_template_decl
= 1;
9456 dependent_p
= dependent_type_p (t
);
9457 else if (TREE_CODE (t
) == TREE_VEC
)
9458 dependent_p
= any_dependent_template_arguments_p (t
);
9459 else if (TREE_CODE (t
) == TREE_LIST
)
9460 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9461 || uses_template_parms (TREE_CHAIN (t
)));
9462 else if (TREE_CODE (t
) == TYPE_DECL
)
9463 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9466 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9467 || TREE_CODE (t
) == OVERLOAD
9470 || TREE_CODE (t
) == TRAIT_EXPR
9471 || TREE_CODE (t
) == CONSTRUCTOR
9472 || CONSTANT_CLASS_P (t
))
9473 dependent_p
= (type_dependent_expression_p (t
)
9474 || value_dependent_expression_p (t
));
9477 gcc_assert (t
== error_mark_node
);
9478 dependent_p
= false;
9481 processing_template_decl
= saved_processing_template_decl
;
9486 /* Returns true iff current_function_decl is an incompletely instantiated
9487 template. Useful instead of processing_template_decl because the latter
9488 is set to 0 during instantiate_non_dependent_expr. */
9491 in_template_function (void)
9493 tree fn
= current_function_decl
;
9495 ++processing_template_decl
;
9496 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9497 && DECL_TEMPLATE_INFO (fn
)
9498 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9499 --processing_template_decl
;
9503 /* Returns true iff we are currently within a template other than a
9504 default-capturing generic lambda, so we don't need to worry about semantic
9508 processing_nonlambda_template (void)
9510 return processing_template_decl
&& !need_generic_capture ();
9513 /* Returns true if T depends on any template parameter with level LEVEL. */
9516 uses_template_parms_level (tree t
, int level
)
9518 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9519 /*include_nondeduced_p=*/true);
9522 /* Returns true if the signature of DECL depends on any template parameter from
9523 its enclosing class. */
9526 uses_outer_template_parms (tree decl
)
9528 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9531 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9532 &depth
, NULL
, /*include_nondeduced_p=*/true))
9534 if (PRIMARY_TEMPLATE_P (decl
)
9535 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9536 (DECL_TEMPLATE_PARMS (decl
)),
9537 template_parm_outer_level
,
9538 &depth
, NULL
, /*include_nondeduced_p=*/true))
9540 tree ci
= get_constraints (decl
);
9542 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9543 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9544 &depth
, NULL
, /*nondeduced*/true))
9549 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9550 ill-formed translation unit, i.e. a variable or function that isn't
9551 usable in a constant expression. */
9554 neglectable_inst_p (tree d
)
9557 && !undeduced_auto_decl (d
)
9558 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9559 : decl_maybe_constant_var_p (d
)));
9562 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9563 neglectable and instantiated from within an erroneous instantiation. */
9566 limit_bad_template_recursion (tree decl
)
9568 struct tinst_level
*lev
= current_tinst_level
;
9569 int errs
= errorcount
+ sorrycount
;
9570 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9573 for (; lev
; lev
= lev
->next
)
9574 if (neglectable_inst_p (lev
->decl
))
9577 return (lev
&& errs
> lev
->errors
);
9580 static int tinst_depth
;
9581 extern int max_tinst_depth
;
9584 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9586 /* We're starting to instantiate D; record the template instantiation context
9587 for diagnostics and to restore it later. */
9590 push_tinst_level (tree d
)
9592 return push_tinst_level_loc (d
, input_location
);
9595 /* We're starting to instantiate D; record the template instantiation context
9596 at LOC for diagnostics and to restore it later. */
9599 push_tinst_level_loc (tree d
, location_t loc
)
9601 struct tinst_level
*new_level
;
9603 if (tinst_depth
>= max_tinst_depth
)
9605 /* Tell error.c not to try to instantiate any templates. */
9607 fatal_error (input_location
,
9608 "template instantiation depth exceeds maximum of %d"
9609 " (use -ftemplate-depth= to increase the maximum)",
9614 /* If the current instantiation caused problems, don't let it instantiate
9615 anything else. Do allow deduction substitution and decls usable in
9616 constant expressions. */
9617 if (limit_bad_template_recursion (d
))
9620 /* When not -quiet, dump template instantiations other than functions, since
9621 announce_function will take care of those. */
9623 && TREE_CODE (d
) != TREE_LIST
9624 && TREE_CODE (d
) != FUNCTION_DECL
)
9625 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9627 new_level
= ggc_alloc
<tinst_level
> ();
9628 new_level
->decl
= d
;
9629 new_level
->locus
= loc
;
9630 new_level
->errors
= errorcount
+sorrycount
;
9631 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9632 new_level
->next
= current_tinst_level
;
9633 current_tinst_level
= new_level
;
9636 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9637 depth_reached
= tinst_depth
;
9642 /* We're done instantiating this template; return to the instantiation
9646 pop_tinst_level (void)
9648 /* Restore the filename and line number stashed away when we started
9649 this instantiation. */
9650 input_location
= current_tinst_level
->locus
;
9651 current_tinst_level
= current_tinst_level
->next
;
9655 /* We're instantiating a deferred template; restore the template
9656 instantiation context in which the instantiation was requested, which
9657 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9660 reopen_tinst_level (struct tinst_level
*level
)
9662 struct tinst_level
*t
;
9665 for (t
= level
; t
; t
= t
->next
)
9668 current_tinst_level
= level
;
9670 if (current_tinst_level
)
9671 current_tinst_level
->errors
= errorcount
+sorrycount
;
9675 /* Returns the TINST_LEVEL which gives the original instantiation
9678 struct tinst_level
*
9679 outermost_tinst_level (void)
9681 struct tinst_level
*level
= current_tinst_level
;
9684 level
= level
->next
;
9688 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9689 vector of template arguments, as for tsubst.
9691 Returns an appropriate tsubst'd friend declaration. */
9694 tsubst_friend_function (tree decl
, tree args
)
9698 if (TREE_CODE (decl
) == FUNCTION_DECL
9699 && DECL_TEMPLATE_INSTANTIATION (decl
)
9700 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9701 /* This was a friend declared with an explicit template
9702 argument list, e.g.:
9706 to indicate that f was a template instantiation, not a new
9707 function declaration. Now, we have to figure out what
9708 instantiation of what template. */
9710 tree template_id
, arglist
, fns
;
9713 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9715 /* Friend functions are looked up in the containing namespace scope.
9716 We must enter that scope, to avoid finding member functions of the
9717 current class with same name. */
9718 push_nested_namespace (ns
);
9719 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9720 tf_warning_or_error
, NULL_TREE
,
9721 /*integral_constant_expression_p=*/false);
9722 pop_nested_namespace (ns
);
9723 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9724 tf_warning_or_error
, NULL_TREE
);
9725 template_id
= lookup_template_function (fns
, arglist
);
9727 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9728 tmpl
= determine_specialization (template_id
, new_friend
,
9730 /*need_member_template=*/0,
9731 TREE_VEC_LENGTH (args
),
9733 return instantiate_template (tmpl
, new_args
, tf_error
);
9736 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9738 /* The NEW_FRIEND will look like an instantiation, to the
9739 compiler, but is not an instantiation from the point of view of
9740 the language. For example, we might have had:
9742 template <class T> struct S {
9743 template <class U> friend void f(T, U);
9746 Then, in S<int>, template <class U> void f(int, U) is not an
9747 instantiation of anything. */
9748 if (new_friend
== error_mark_node
)
9749 return error_mark_node
;
9751 DECL_USE_TEMPLATE (new_friend
) = 0;
9752 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9754 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9755 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9756 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9759 /* The mangled name for the NEW_FRIEND is incorrect. The function
9760 is not a template instantiation and should not be mangled like
9761 one. Therefore, we forget the mangling here; we'll recompute it
9762 later if we need it. */
9763 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9765 SET_DECL_RTL (new_friend
, NULL
);
9766 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9769 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9772 tree new_friend_template_info
;
9773 tree new_friend_result_template_info
;
9775 int new_friend_is_defn
;
9777 /* We must save some information from NEW_FRIEND before calling
9778 duplicate decls since that function will free NEW_FRIEND if
9780 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9781 new_friend_is_defn
=
9782 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9783 (template_for_substitution (new_friend
)))
9785 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9787 /* This declaration is a `primary' template. */
9788 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9790 new_friend_result_template_info
9791 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9794 new_friend_result_template_info
= NULL_TREE
;
9796 /* Inside pushdecl_namespace_level, we will push into the
9797 current namespace. However, the friend function should go
9798 into the namespace of the template. */
9799 ns
= decl_namespace_context (new_friend
);
9800 push_nested_namespace (ns
);
9801 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9802 pop_nested_namespace (ns
);
9804 if (old_decl
== error_mark_node
)
9805 return error_mark_node
;
9807 if (old_decl
!= new_friend
)
9809 /* This new friend declaration matched an existing
9810 declaration. For example, given:
9812 template <class T> void f(T);
9813 template <class U> class C {
9814 template <class T> friend void f(T) {}
9817 the friend declaration actually provides the definition
9818 of `f', once C has been instantiated for some type. So,
9819 old_decl will be the out-of-class template declaration,
9820 while new_friend is the in-class definition.
9822 But, if `f' was called before this point, the
9823 instantiation of `f' will have DECL_TI_ARGS corresponding
9824 to `T' but not to `U', references to which might appear
9825 in the definition of `f'. Previously, the most general
9826 template for an instantiation of `f' was the out-of-class
9827 version; now it is the in-class version. Therefore, we
9828 run through all specialization of `f', adding to their
9829 DECL_TI_ARGS appropriately. In particular, they need a
9830 new set of outer arguments, corresponding to the
9831 arguments for this class instantiation.
9833 The same situation can arise with something like this:
9836 template <class T> class C {
9840 when `C<int>' is instantiated. Now, `f(int)' is defined
9843 if (!new_friend_is_defn
)
9844 /* On the other hand, if the in-class declaration does
9845 *not* provide a definition, then we don't want to alter
9846 existing definitions. We can just leave everything
9851 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9852 tree new_args
= TI_ARGS (new_friend_template_info
);
9854 /* Overwrite whatever template info was there before, if
9855 any, with the new template information pertaining to
9857 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9859 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9861 /* We should have called reregister_specialization in
9863 gcc_assert (retrieve_specialization (new_template
,
9867 /* Instantiate it if the global has already been used. */
9868 if (DECL_ODR_USED (old_decl
))
9869 instantiate_decl (old_decl
, /*defer_ok=*/true,
9870 /*expl_inst_class_mem_p=*/false);
9876 /* Indicate that the old function template is a partial
9878 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9879 = new_friend_result_template_info
;
9881 gcc_assert (new_template
9882 == most_general_template (new_template
));
9883 gcc_assert (new_template
!= old_decl
);
9885 /* Reassign any specializations already in the hash table
9886 to the new more general template, and add the
9887 additional template args. */
9888 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9892 tree spec
= TREE_VALUE (t
);
9895 elt
.tmpl
= old_decl
;
9896 elt
.args
= DECL_TI_ARGS (spec
);
9897 elt
.spec
= NULL_TREE
;
9899 decl_specializations
->remove_elt (&elt
);
9902 = add_outermost_template_args (new_args
,
9903 DECL_TI_ARGS (spec
));
9905 register_specialization
9906 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9909 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9913 /* The information from NEW_FRIEND has been merged into OLD_DECL
9914 by duplicate_decls. */
9915 new_friend
= old_decl
;
9920 tree context
= DECL_CONTEXT (new_friend
);
9924 template <class T> class C {
9925 template <class U> friend void C1<U>::f (); // case 1
9926 friend void C2<T>::f (); // case 2
9928 we only need to make sure CONTEXT is a complete type for
9929 case 2. To distinguish between the two cases, we note that
9930 CONTEXT of case 1 remains dependent type after tsubst while
9931 this isn't true for case 2. */
9932 ++processing_template_decl
;
9933 dependent_p
= dependent_type_p (context
);
9934 --processing_template_decl
;
9937 && !complete_type_or_else (context
, NULL_TREE
))
9938 return error_mark_node
;
9940 if (COMPLETE_TYPE_P (context
))
9942 tree fn
= new_friend
;
9943 /* do_friend adds the TEMPLATE_DECL for any member friend
9944 template even if it isn't a member template, i.e.
9945 template <class T> friend A<T>::f();
9946 Look through it in that case. */
9947 if (TREE_CODE (fn
) == TEMPLATE_DECL
9948 && !PRIMARY_TEMPLATE_P (fn
))
9949 fn
= DECL_TEMPLATE_RESULT (fn
);
9950 /* Check to see that the declaration is really present, and,
9951 possibly obtain an improved declaration. */
9952 fn
= check_classfn (context
, fn
, NULL_TREE
);
9962 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9963 template arguments, as for tsubst.
9965 Returns an appropriate tsubst'd friend type or error_mark_node on
9969 tsubst_friend_class (tree friend_tmpl
, tree args
)
9975 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9977 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9978 return TREE_TYPE (t
);
9981 context
= CP_DECL_CONTEXT (friend_tmpl
);
9983 if (context
!= global_namespace
)
9985 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9986 push_nested_namespace (context
);
9988 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9991 /* Look for a class template declaration. We look for hidden names
9992 because two friend declarations of the same template are the
9993 same. For example, in:
9996 template <typename> friend class F;
9998 template <typename> struct B {
9999 template <typename> friend class F;
10002 both F templates are the same. */
10003 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
10004 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
10006 /* But, if we don't find one, it might be because we're in a
10007 situation like this:
10015 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10016 for `S<int>', not the TEMPLATE_DECL. */
10017 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
10019 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
10020 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
10023 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10025 /* The friend template has already been declared. Just
10026 check to see that the declarations match, and install any new
10027 default parameters. We must tsubst the default parameters,
10028 of course. We only need the innermost template parameters
10029 because that is all that redeclare_class_template will look
10031 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10032 > TMPL_ARGS_DEPTH (args
))
10035 location_t saved_input_location
;
10036 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10037 args
, tf_warning_or_error
);
10039 saved_input_location
= input_location
;
10040 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10041 tree cons
= get_constraints (tmpl
);
10042 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10043 input_location
= saved_input_location
;
10047 friend_type
= TREE_TYPE (tmpl
);
10051 /* The friend template has not already been declared. In this
10052 case, the instantiation of the template class will cause the
10053 injection of this template into the global scope. */
10054 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10055 if (tmpl
== error_mark_node
)
10056 return error_mark_node
;
10058 /* The new TMPL is not an instantiation of anything, so we
10059 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10060 the new type because that is supposed to be the corresponding
10061 template decl, i.e., TMPL. */
10062 DECL_USE_TEMPLATE (tmpl
) = 0;
10063 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10064 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10065 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10066 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10068 /* Inject this template into the global scope. */
10069 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
, true));
10072 if (context
!= global_namespace
)
10074 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10075 pop_nested_namespace (context
);
10077 pop_nested_class ();
10080 return friend_type
;
10083 /* Returns zero if TYPE cannot be completed later due to circularity.
10084 Otherwise returns one. */
10087 can_complete_type_without_circularity (tree type
)
10089 if (type
== NULL_TREE
|| type
== error_mark_node
)
10091 else if (COMPLETE_TYPE_P (type
))
10093 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10094 return can_complete_type_without_circularity (TREE_TYPE (type
));
10095 else if (CLASS_TYPE_P (type
)
10096 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10102 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10103 tsubst_flags_t
, tree
);
10105 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10106 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10109 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10110 tsubst_flags_t complain
, tree in_decl
)
10112 gcc_assert (ATTR_IS_DEPENDENT (t
));
10114 tree val
= TREE_VALUE (t
);
10115 if (val
== NULL_TREE
)
10116 /* Nothing to do. */;
10117 else if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
10118 && is_attribute_p ("omp declare simd",
10119 get_attribute_name (t
)))
10121 tree clauses
= TREE_VALUE (val
);
10122 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10123 complain
, in_decl
);
10124 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10125 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10126 tree parms
= DECL_ARGUMENTS (*decl_p
);
10128 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10130 val
= build_tree_list (NULL_TREE
, clauses
);
10134 /* If the first attribute argument is an identifier, don't
10135 pass it through tsubst. Attributes like mode, format,
10136 cleanup and several target specific attributes expect it
10138 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10141 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10142 /*integral_constant_expression_p=*/false);
10143 if (chain
!= TREE_CHAIN (val
))
10144 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10146 else if (PACK_EXPANSION_P (val
))
10148 /* An attribute pack expansion. */
10149 tree purp
= TREE_PURPOSE (t
);
10150 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10151 if (pack
== error_mark_node
)
10152 return error_mark_node
;
10153 int len
= TREE_VEC_LENGTH (pack
);
10154 tree list
= NULL_TREE
;
10156 for (int i
= 0; i
< len
; ++i
)
10158 tree elt
= TREE_VEC_ELT (pack
, i
);
10159 *q
= build_tree_list (purp
, elt
);
10160 q
= &TREE_CHAIN (*q
);
10165 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10166 /*integral_constant_expression_p=*/false);
10168 if (val
!= TREE_VALUE (t
))
10169 return build_tree_list (TREE_PURPOSE (t
), val
);
10173 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10174 unchanged or a new TREE_LIST chain. */
10177 tsubst_attributes (tree attributes
, tree args
,
10178 tsubst_flags_t complain
, tree in_decl
)
10180 tree last_dep
= NULL_TREE
;
10182 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10183 if (ATTR_IS_DEPENDENT (t
))
10186 attributes
= copy_list (attributes
);
10191 for (tree
*p
= &attributes
; *p
; )
10194 if (ATTR_IS_DEPENDENT (t
))
10196 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10201 p
= &TREE_CHAIN (*p
);
10203 *p
= TREE_CHAIN (t
);
10207 p
= &TREE_CHAIN (*p
);
10213 /* Apply any attributes which had to be deferred until instantiation
10214 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10215 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10218 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10219 tree args
, tsubst_flags_t complain
, tree in_decl
)
10221 tree last_dep
= NULL_TREE
;
10225 if (attributes
== NULL_TREE
)
10228 if (DECL_P (*decl_p
))
10230 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10232 p
= &DECL_ATTRIBUTES (*decl_p
);
10233 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10234 to our attributes parameter. */
10235 gcc_assert (*p
== attributes
);
10239 p
= &TYPE_ATTRIBUTES (*decl_p
);
10240 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10241 lookup_template_class_1, and should be preserved. */
10242 gcc_assert (*p
!= attributes
);
10244 p
= &TREE_CHAIN (*p
);
10247 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10248 if (ATTR_IS_DEPENDENT (t
))
10251 attributes
= copy_list (attributes
);
10258 tree late_attrs
= NULL_TREE
;
10259 tree
*q
= &late_attrs
;
10264 if (ATTR_IS_DEPENDENT (t
))
10266 *p
= TREE_CHAIN (t
);
10267 TREE_CHAIN (t
) = NULL_TREE
;
10268 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10270 q
= &TREE_CHAIN (*q
);
10274 p
= &TREE_CHAIN (t
);
10277 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10281 /* Perform (or defer) access check for typedefs that were referenced
10282 from within the template TMPL code.
10283 This is a subroutine of instantiate_decl and instantiate_class_template.
10284 TMPL is the template to consider and TARGS is the list of arguments of
10288 perform_typedefs_access_check (tree tmpl
, tree targs
)
10290 location_t saved_location
;
10292 qualified_typedef_usage_t
*iter
;
10295 || (!CLASS_TYPE_P (tmpl
)
10296 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10299 saved_location
= input_location
;
10300 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10302 tree type_decl
= iter
->typedef_decl
;
10303 tree type_scope
= iter
->context
;
10305 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10308 if (uses_template_parms (type_decl
))
10309 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10310 if (uses_template_parms (type_scope
))
10311 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10313 /* Make access check error messages point to the location
10314 of the use of the typedef. */
10315 input_location
= iter
->locus
;
10316 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10317 type_decl
, type_decl
,
10318 tf_warning_or_error
);
10320 input_location
= saved_location
;
10324 instantiate_class_template_1 (tree type
)
10326 tree templ
, args
, pattern
, t
, member
;
10330 unsigned int saved_maximum_field_alignment
;
10333 if (type
== error_mark_node
)
10334 return error_mark_node
;
10336 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10337 || uses_template_parms (type
))
10340 /* Figure out which template is being instantiated. */
10341 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10342 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10344 /* Determine what specialization of the original template to
10346 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10347 if (t
== error_mark_node
)
10349 TYPE_BEING_DEFINED (type
) = 1;
10350 return error_mark_node
;
10354 /* This TYPE is actually an instantiation of a partial
10355 specialization. We replace the innermost set of ARGS with
10356 the arguments appropriate for substitution. For example,
10359 template <class T> struct S {};
10360 template <class T> struct S<T*> {};
10362 and supposing that we are instantiating S<int*>, ARGS will
10363 presently be {int*} -- but we need {int}. */
10364 pattern
= TREE_TYPE (t
);
10365 args
= TREE_PURPOSE (t
);
10369 pattern
= TREE_TYPE (templ
);
10370 args
= CLASSTYPE_TI_ARGS (type
);
10373 /* If the template we're instantiating is incomplete, then clearly
10374 there's nothing we can do. */
10375 if (!COMPLETE_TYPE_P (pattern
))
10378 /* If we've recursively instantiated too many templates, stop. */
10379 if (! push_tinst_level (type
))
10382 /* Now we're really doing the instantiation. Mark the type as in
10383 the process of being defined. */
10384 TYPE_BEING_DEFINED (type
) = 1;
10386 /* We may be in the middle of deferred access check. Disable
10388 push_deferring_access_checks (dk_no_deferred
);
10390 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10391 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10393 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10394 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10395 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10396 fn_context
= error_mark_node
;
10398 push_to_top_level ();
10401 cp_unevaluated_operand
= 0;
10402 c_inhibit_evaluation_warnings
= 0;
10404 /* Use #pragma pack from the template context. */
10405 saved_maximum_field_alignment
= maximum_field_alignment
;
10406 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10408 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10410 /* Set the input location to the most specialized template definition.
10411 This is needed if tsubsting causes an error. */
10412 typedecl
= TYPE_MAIN_DECL (pattern
);
10413 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10414 DECL_SOURCE_LOCATION (typedecl
);
10416 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10417 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10418 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10419 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10420 if (ANON_AGGR_TYPE_P (pattern
))
10421 SET_ANON_AGGR_TYPE_P (type
);
10422 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10424 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10425 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10426 /* Adjust visibility for template arguments. */
10427 determine_visibility (TYPE_MAIN_DECL (type
));
10429 if (CLASS_TYPE_P (type
))
10430 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10432 pbinfo
= TYPE_BINFO (pattern
);
10434 /* We should never instantiate a nested class before its enclosing
10435 class; we need to look up the nested class by name before we can
10436 instantiate it, and that lookup should instantiate the enclosing
10438 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10439 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10441 base_list
= NULL_TREE
;
10442 if (BINFO_N_BASE_BINFOS (pbinfo
))
10448 /* We must enter the scope containing the type, as that is where
10449 the accessibility of types named in dependent bases are
10451 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10453 /* Substitute into each of the bases to determine the actual
10455 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10458 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10459 tree expanded_bases
= NULL_TREE
;
10462 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10465 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10466 args
, tf_error
, NULL_TREE
);
10467 if (expanded_bases
== error_mark_node
)
10470 len
= TREE_VEC_LENGTH (expanded_bases
);
10473 for (idx
= 0; idx
< len
; idx
++)
10475 if (expanded_bases
)
10476 /* Extract the already-expanded base class. */
10477 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10479 /* Substitute to figure out the base class. */
10480 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10483 if (base
== error_mark_node
)
10486 base_list
= tree_cons (access
, base
, base_list
);
10487 if (BINFO_VIRTUAL_P (pbase_binfo
))
10488 TREE_TYPE (base_list
) = integer_type_node
;
10492 /* The list is now in reverse order; correct that. */
10493 base_list
= nreverse (base_list
);
10496 pop_scope (pushed_scope
);
10498 /* Now call xref_basetypes to set up all the base-class
10500 xref_basetypes (type
, base_list
);
10502 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10503 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10504 args
, tf_error
, NULL_TREE
);
10505 fixup_attribute_variants (type
);
10507 /* Now that our base classes are set up, enter the scope of the
10508 class, so that name lookups into base classes, etc. will work
10509 correctly. This is precisely analogous to what we do in
10510 begin_class_definition when defining an ordinary non-template
10511 class, except we also need to push the enclosing classes. */
10512 push_nested_class (type
);
10514 /* Now members are processed in the order of declaration. */
10515 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10516 member
; member
= TREE_CHAIN (member
))
10518 tree t
= TREE_VALUE (member
);
10520 if (TREE_PURPOSE (member
))
10524 /* Build new CLASSTYPE_NESTED_UTDS. */
10527 bool class_template_p
;
10529 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10530 && TYPE_LANG_SPECIFIC (t
)
10531 && CLASSTYPE_IS_TEMPLATE (t
));
10532 /* If the member is a class template, then -- even after
10533 substitution -- there may be dependent types in the
10534 template argument list for the class. We increment
10535 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10536 that function will assume that no types are dependent
10537 when outside of a template. */
10538 if (class_template_p
)
10539 ++processing_template_decl
;
10540 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10541 if (class_template_p
)
10542 --processing_template_decl
;
10543 if (newtag
== error_mark_node
)
10546 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10548 tree name
= TYPE_IDENTIFIER (t
);
10550 if (class_template_p
)
10551 /* Unfortunately, lookup_template_class sets
10552 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10553 instantiation (i.e., for the type of a member
10554 template class nested within a template class.)
10555 This behavior is required for
10556 maybe_process_partial_specialization to work
10557 correctly, but is not accurate in this case;
10558 the TAG is not an instantiation of anything.
10559 (The corresponding TEMPLATE_DECL is an
10560 instantiation, but the TYPE is not.) */
10561 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10563 /* Now, we call pushtag to put this NEWTAG into the scope of
10564 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10565 pushtag calling push_template_decl. We don't have to do
10566 this for enums because it will already have been done in
10569 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10570 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10573 else if (DECL_DECLARES_FUNCTION_P (t
))
10577 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10578 ++processing_template_decl
;
10579 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10580 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10581 --processing_template_decl
;
10582 set_current_access_from_decl (r
);
10583 finish_member_declaration (r
);
10584 /* Instantiate members marked with attribute used. */
10585 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10587 if (TREE_CODE (r
) == FUNCTION_DECL
10588 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10589 cp_check_omp_declare_reduction (r
);
10591 else if (DECL_CLASS_TEMPLATE_P (t
)
10592 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10593 /* A closure type for a lambda in a default argument for a
10594 member template. Ignore it; it will be instantiated with
10595 the default argument. */;
10598 /* Build new TYPE_FIELDS. */
10599 if (TREE_CODE (t
) == STATIC_ASSERT
)
10603 ++c_inhibit_evaluation_warnings
;
10605 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10606 tf_warning_or_error
, NULL_TREE
,
10607 /*integral_constant_expression_p=*/true);
10608 --c_inhibit_evaluation_warnings
;
10610 finish_static_assert (condition
,
10611 STATIC_ASSERT_MESSAGE (t
),
10612 STATIC_ASSERT_SOURCE_LOCATION (t
),
10613 /*member_p=*/true);
10615 else if (TREE_CODE (t
) != CONST_DECL
)
10618 tree vec
= NULL_TREE
;
10621 /* The file and line for this declaration, to
10622 assist in error message reporting. Since we
10623 called push_tinst_level above, we don't need to
10625 input_location
= DECL_SOURCE_LOCATION (t
);
10627 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10628 ++processing_template_decl
;
10629 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10630 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10631 --processing_template_decl
;
10633 if (TREE_CODE (r
) == TREE_VEC
)
10635 /* A capture pack became multiple fields. */
10637 len
= TREE_VEC_LENGTH (vec
);
10640 for (int i
= 0; i
< len
; ++i
)
10643 r
= TREE_VEC_ELT (vec
, i
);
10648 [t]he initialization (and any associated
10649 side-effects) of a static data member does
10650 not occur unless the static data member is
10651 itself used in a way that requires the
10652 definition of the static data member to
10655 Therefore, we do not substitute into the
10656 initialized for the static data member here. */
10657 finish_static_data_member_decl
10659 /*init=*/NULL_TREE
,
10660 /*init_const_expr_p=*/false,
10661 /*asmspec_tree=*/NULL_TREE
,
10663 /* Instantiate members marked with attribute used. */
10664 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10667 else if (TREE_CODE (r
) == FIELD_DECL
)
10669 /* Determine whether R has a valid type and can be
10670 completed later. If R is invalid, then its type
10671 is replaced by error_mark_node. */
10672 tree rtype
= TREE_TYPE (r
);
10673 if (can_complete_type_without_circularity (rtype
))
10674 complete_type (rtype
);
10676 if (!complete_or_array_type_p (rtype
))
10678 /* If R's type couldn't be completed and
10679 it isn't a flexible array member (whose
10680 type is incomplete by definition) give
10682 cxx_incomplete_type_error (r
, rtype
);
10683 TREE_TYPE (r
) = error_mark_node
;
10687 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10688 such a thing will already have been added to the field
10689 list by tsubst_enum in finish_member_declaration in the
10690 CLASSTYPE_NESTED_UTDS case above. */
10691 if (!(TREE_CODE (r
) == TYPE_DECL
10692 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10693 && DECL_ARTIFICIAL (r
)))
10695 set_current_access_from_decl (r
);
10696 finish_member_declaration (r
);
10704 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10705 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10707 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10709 tree friend_type
= t
;
10710 bool adjust_processing_template_decl
= false;
10712 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10714 /* template <class T> friend class C; */
10715 friend_type
= tsubst_friend_class (friend_type
, args
);
10716 adjust_processing_template_decl
= true;
10718 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10720 /* template <class T> friend class C::D; */
10721 friend_type
= tsubst (friend_type
, args
,
10722 tf_warning_or_error
, NULL_TREE
);
10723 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10724 friend_type
= TREE_TYPE (friend_type
);
10725 adjust_processing_template_decl
= true;
10727 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10728 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10730 /* This could be either
10734 when dependent_type_p is false or
10736 template <class U> friend class T::C;
10739 /* Bump processing_template_decl in case this is something like
10740 template <class T> friend struct A<T>::B. */
10741 ++processing_template_decl
;
10742 friend_type
= tsubst (friend_type
, args
,
10743 tf_warning_or_error
, NULL_TREE
);
10744 if (dependent_type_p (friend_type
))
10745 adjust_processing_template_decl
= true;
10746 --processing_template_decl
;
10748 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10749 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10750 && TYPE_HIDDEN_P (friend_type
))
10754 where C hasn't been declared yet. Let's lookup name
10755 from namespace scope directly, bypassing any name that
10756 come from dependent base class. */
10757 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10759 /* The call to xref_tag_from_type does injection for friend
10761 push_nested_namespace (ns
);
10763 xref_tag_from_type (friend_type
, NULL_TREE
,
10764 /*tag_scope=*/ts_current
);
10765 pop_nested_namespace (ns
);
10767 else if (uses_template_parms (friend_type
))
10768 /* friend class C<T>; */
10769 friend_type
= tsubst (friend_type
, args
,
10770 tf_warning_or_error
, NULL_TREE
);
10775 where C is already declared or
10777 friend class C<int>;
10779 We don't have to do anything in these cases. */
10781 if (adjust_processing_template_decl
)
10782 /* Trick make_friend_class into realizing that the friend
10783 we're adding is a template, not an ordinary class. It's
10784 important that we use make_friend_class since it will
10785 perform some error-checking and output cross-reference
10787 ++processing_template_decl
;
10789 if (friend_type
!= error_mark_node
)
10790 make_friend_class (type
, friend_type
, /*complain=*/false);
10792 if (adjust_processing_template_decl
)
10793 --processing_template_decl
;
10797 /* Build new DECL_FRIENDLIST. */
10800 /* The file and line for this declaration, to
10801 assist in error message reporting. Since we
10802 called push_tinst_level above, we don't need to
10804 input_location
= DECL_SOURCE_LOCATION (t
);
10806 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10808 ++processing_template_decl
;
10809 push_deferring_access_checks (dk_no_check
);
10812 r
= tsubst_friend_function (t
, args
);
10813 add_friend (type
, r
, /*complain=*/false);
10814 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10816 pop_deferring_access_checks ();
10817 --processing_template_decl
;
10825 /* Restore these before substituting into the lambda capture
10827 cp_unevaluated_operand
= saved_unevaluated_operand
;
10828 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10831 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10833 tree decl
= lambda_function (type
);
10836 if (cxx_dialect
>= cxx17
)
10837 CLASSTYPE_LITERAL_P (type
) = true;
10839 if (!DECL_TEMPLATE_INFO (decl
)
10840 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10842 /* Set function_depth to avoid garbage collection. */
10844 instantiate_decl (decl
, /*defer_ok=*/false, false);
10848 /* We need to instantiate the capture list from the template
10849 after we've instantiated the closure members, but before we
10850 consider adding the conversion op. Also keep any captures
10851 that may have been added during instantiation of the op(). */
10852 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10854 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10855 args
, tf_warning_or_error
, NULL_TREE
,
10858 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10859 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10861 maybe_add_lambda_conv_op (type
);
10864 gcc_assert (errorcount
);
10867 /* Set the file and line number information to whatever is given for
10868 the class itself. This puts error messages involving generated
10869 implicit functions at a predictable point, and the same point
10870 that would be used for non-template classes. */
10871 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10873 unreverse_member_declarations (type
);
10874 finish_struct_1 (type
);
10875 TYPE_BEING_DEFINED (type
) = 0;
10877 /* We don't instantiate default arguments for member functions. 14.7.1:
10879 The implicit instantiation of a class template specialization causes
10880 the implicit instantiation of the declarations, but not of the
10881 definitions or default arguments, of the class member functions,
10882 member classes, static data members and member templates.... */
10884 /* Some typedefs referenced from within the template code need to be access
10885 checked at template instantiation time, i.e now. These types were
10886 added to the template at parsing time. Let's get those and perform
10887 the access checks then. */
10888 perform_typedefs_access_check (pattern
, args
);
10889 perform_deferred_access_checks (tf_warning_or_error
);
10890 pop_nested_class ();
10891 maximum_field_alignment
= saved_maximum_field_alignment
;
10893 pop_from_top_level ();
10894 pop_deferring_access_checks ();
10895 pop_tinst_level ();
10897 /* The vtable for a template class can be emitted in any translation
10898 unit in which the class is instantiated. When there is no key
10899 method, however, finish_struct_1 will already have added TYPE to
10900 the keyed_classes. */
10901 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10902 vec_safe_push (keyed_classes
, type
);
10907 /* Wrapper for instantiate_class_template_1. */
10910 instantiate_class_template (tree type
)
10913 timevar_push (TV_TEMPLATE_INST
);
10914 ret
= instantiate_class_template_1 (type
);
10915 timevar_pop (TV_TEMPLATE_INST
);
10920 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10926 else if (TYPE_P (t
))
10927 r
= tsubst (t
, args
, complain
, in_decl
);
10930 if (!(complain
& tf_warning
))
10931 ++c_inhibit_evaluation_warnings
;
10932 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10933 /*integral_constant_expression_p=*/true);
10934 if (!(complain
& tf_warning
))
10935 --c_inhibit_evaluation_warnings
;
10940 /* Given a function parameter pack TMPL_PARM and some function parameters
10941 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10942 and set *SPEC_P to point at the next point in the list. */
10945 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10947 /* Collect all of the extra "packed" parameters into an
10950 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10951 tree spec_parm
= *spec_p
;
10954 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10956 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10959 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10960 parmvec
= make_tree_vec (len
);
10961 spec_parm
= *spec_p
;
10962 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10963 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10965 /* Build the argument packs. */
10966 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10967 *spec_p
= spec_parm
;
10972 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10973 NONTYPE_ARGUMENT_PACK. */
10976 make_fnparm_pack (tree spec_parm
)
10978 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10981 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10982 pack expansion with no extra args, 2 if it has extra args, or 0
10983 if it is not a pack expansion. */
10986 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10988 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10989 if (i
>= TREE_VEC_LENGTH (vec
))
10991 tree elt
= TREE_VEC_ELT (vec
, i
);
10993 /* A decl pack is itself an expansion. */
10994 elt
= TREE_TYPE (elt
);
10995 if (!PACK_EXPANSION_P (elt
))
10997 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11003 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11006 make_argument_pack_select (tree arg_pack
, unsigned index
)
11008 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11010 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11011 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11016 /* This is a subroutine of tsubst_pack_expansion.
11018 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11019 mechanism to store the (non complete list of) arguments of the
11020 substitution and return a non substituted pack expansion, in order
11021 to wait for when we have enough arguments to really perform the
11025 use_pack_expansion_extra_args_p (tree parm_packs
,
11027 bool has_empty_arg
)
11029 /* If one pack has an expansion and another pack has a normal
11030 argument or if one pack has an empty argument and an another
11031 one hasn't then tsubst_pack_expansion cannot perform the
11032 substitution and need to fall back on the
11033 PACK_EXPANSION_EXTRA mechanism. */
11034 if (parm_packs
== NULL_TREE
)
11036 else if (has_empty_arg
)
11039 bool has_expansion_arg
= false;
11040 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11042 bool has_non_expansion_arg
= false;
11043 for (tree parm_pack
= parm_packs
;
11045 parm_pack
= TREE_CHAIN (parm_pack
))
11047 tree arg
= TREE_VALUE (parm_pack
);
11049 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11051 /* We can't substitute a pack expansion with extra args into
11055 has_expansion_arg
= true;
11057 has_non_expansion_arg
= true;
11060 if (has_expansion_arg
&& has_non_expansion_arg
)
11066 /* [temp.variadic]/6 says that:
11068 The instantiation of a pack expansion [...]
11069 produces a list E1,E2, ..., En, where N is the number of elements
11070 in the pack expansion parameters.
11072 This subroutine of tsubst_pack_expansion produces one of these Ei.
11074 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11075 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11076 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11077 INDEX is the index 'i' of the element Ei to produce. ARGS,
11078 COMPLAIN, and IN_DECL are the same parameters as for the
11079 tsubst_pack_expansion function.
11081 The function returns the resulting Ei upon successful completion,
11082 or error_mark_node.
11084 Note that this function possibly modifies the ARGS parameter, so
11085 it's the responsibility of the caller to restore it. */
11088 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11091 tree args
/* This parm gets
11093 tsubst_flags_t complain
,
11097 bool ith_elem_is_expansion
= false;
11099 /* For each parameter pack, change the substitution of the parameter
11100 pack to the ith argument in its argument pack, then expand the
11102 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11104 tree parm
= TREE_PURPOSE (pack
);
11105 tree arg_pack
= TREE_VALUE (pack
);
11106 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11108 ith_elem_is_expansion
|=
11109 argument_pack_element_is_expansion_p (arg_pack
, index
);
11111 /* Select the Ith argument from the pack. */
11112 if (TREE_CODE (parm
) == PARM_DECL
11113 || TREE_CODE (parm
) == FIELD_DECL
)
11117 aps
= make_argument_pack_select (arg_pack
, index
);
11118 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11119 return error_mark_node
;
11120 register_local_specialization (aps
, parm
);
11123 aps
= retrieve_local_specialization (parm
);
11128 template_parm_level_and_index (parm
, &level
, &idx
);
11132 aps
= make_argument_pack_select (arg_pack
, index
);
11133 /* Update the corresponding argument. */
11134 TMPL_ARG (args
, level
, idx
) = aps
;
11137 /* Re-use the ARGUMENT_PACK_SELECT. */
11138 aps
= TMPL_ARG (args
, level
, idx
);
11140 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11143 /* Substitute into the PATTERN with the (possibly altered)
11145 if (pattern
== in_decl
)
11146 /* Expanding a fixed parameter pack from
11147 coerce_template_parameter_pack. */
11148 t
= tsubst_decl (pattern
, args
, complain
);
11149 else if (pattern
== error_mark_node
)
11150 t
= error_mark_node
;
11151 else if (constraint_p (pattern
))
11153 if (processing_template_decl
)
11154 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11156 t
= (constraints_satisfied_p (pattern
, args
)
11157 ? boolean_true_node
: boolean_false_node
);
11159 else if (!TYPE_P (pattern
))
11160 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11161 /*integral_constant_expression_p=*/false);
11163 t
= tsubst (pattern
, args
, complain
, in_decl
);
11165 /* If the Ith argument pack element is a pack expansion, then
11166 the Ith element resulting from the substituting is going to
11167 be a pack expansion as well. */
11168 if (ith_elem_is_expansion
)
11169 t
= make_pack_expansion (t
, complain
);
11174 /* When the unexpanded parameter pack in a fold expression expands to an empty
11175 sequence, the value of the expression is as follows; the program is
11176 ill-formed if the operator is not listed in this table.
11183 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11185 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11186 if (!FOLD_EXPR_MODIFY_P (t
))
11189 case TRUTH_ANDIF_EXPR
:
11190 return boolean_true_node
;
11191 case TRUTH_ORIF_EXPR
:
11192 return boolean_false_node
;
11193 case COMPOUND_EXPR
:
11199 if (complain
& tf_error
)
11200 error_at (location_of (t
),
11201 "fold of empty expansion over %O", code
);
11202 return error_mark_node
;
11205 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11206 form an expression that combines the two terms using the
11210 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11212 tree op
= FOLD_EXPR_OP (t
);
11213 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11215 // Handle compound assignment operators.
11216 if (FOLD_EXPR_MODIFY_P (t
))
11217 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11221 case COMPOUND_EXPR
:
11222 return build_x_compound_expr (input_location
, left
, right
, complain
);
11224 return build_m_component_ref (left
, right
, complain
);
11226 return build_x_binary_op (input_location
, code
,
11227 left
, TREE_CODE (left
),
11228 right
, TREE_CODE (right
),
11234 /* Substitute ARGS into the pack of a fold expression T. */
11237 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11239 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11242 /* Substitute ARGS into the pack of a fold expression T. */
11245 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11247 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11250 /* Expand a PACK of arguments into a grouped as left fold.
11251 Given a pack containing elements A0, A1, ..., An and an
11252 operator @, this builds the expression:
11254 ((A0 @ A1) @ A2) ... @ An
11256 Note that PACK must not be empty.
11258 The operator is defined by the original fold expression T. */
11261 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11263 tree left
= TREE_VEC_ELT (pack
, 0);
11264 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11266 tree right
= TREE_VEC_ELT (pack
, i
);
11267 left
= fold_expression (t
, left
, right
, complain
);
11272 /* Substitute into a unary left fold expression. */
11275 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11278 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11279 if (pack
== error_mark_node
)
11280 return error_mark_node
;
11281 if (PACK_EXPANSION_P (pack
))
11283 tree r
= copy_node (t
);
11284 FOLD_EXPR_PACK (r
) = pack
;
11287 if (TREE_VEC_LENGTH (pack
) == 0)
11288 return expand_empty_fold (t
, complain
);
11290 return expand_left_fold (t
, pack
, complain
);
11293 /* Substitute into a binary left fold expression.
11295 Do ths by building a single (non-empty) vector of argumnts and
11296 building the expression from those elements. */
11299 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11302 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11303 if (pack
== error_mark_node
)
11304 return error_mark_node
;
11305 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11306 if (init
== error_mark_node
)
11307 return error_mark_node
;
11309 if (PACK_EXPANSION_P (pack
))
11311 tree r
= copy_node (t
);
11312 FOLD_EXPR_PACK (r
) = pack
;
11313 FOLD_EXPR_INIT (r
) = init
;
11317 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11318 TREE_VEC_ELT (vec
, 0) = init
;
11319 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11320 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11322 return expand_left_fold (t
, vec
, complain
);
11325 /* Expand a PACK of arguments into a grouped as right fold.
11326 Given a pack containing elementns A0, A1, ..., and an
11327 operator @, this builds the expression:
11329 A0@ ... (An-2 @ (An-1 @ An))
11331 Note that PACK must not be empty.
11333 The operator is defined by the original fold expression T. */
11336 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11338 // Build the expression.
11339 int n
= TREE_VEC_LENGTH (pack
);
11340 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11341 for (--n
; n
!= 0; --n
)
11343 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11344 right
= fold_expression (t
, left
, right
, complain
);
11349 /* Substitute into a unary right fold expression. */
11352 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11355 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11356 if (pack
== error_mark_node
)
11357 return error_mark_node
;
11358 if (PACK_EXPANSION_P (pack
))
11360 tree r
= copy_node (t
);
11361 FOLD_EXPR_PACK (r
) = pack
;
11364 if (TREE_VEC_LENGTH (pack
) == 0)
11365 return expand_empty_fold (t
, complain
);
11367 return expand_right_fold (t
, pack
, complain
);
11370 /* Substitute into a binary right fold expression.
11372 Do ths by building a single (non-empty) vector of arguments and
11373 building the expression from those elements. */
11376 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11379 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11380 if (pack
== error_mark_node
)
11381 return error_mark_node
;
11382 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11383 if (init
== error_mark_node
)
11384 return error_mark_node
;
11386 if (PACK_EXPANSION_P (pack
))
11388 tree r
= copy_node (t
);
11389 FOLD_EXPR_PACK (r
) = pack
;
11390 FOLD_EXPR_INIT (r
) = init
;
11394 int n
= TREE_VEC_LENGTH (pack
);
11395 tree vec
= make_tree_vec (n
+ 1);
11396 for (int i
= 0; i
< n
; ++i
)
11397 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11398 TREE_VEC_ELT (vec
, n
) = init
;
11400 return expand_right_fold (t
, vec
, complain
);
11404 /* Substitute ARGS into T, which is an pack expansion
11405 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11406 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11407 (if only a partial substitution could be performed) or
11408 ERROR_MARK_NODE if there was an error. */
11410 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11414 tree pack
, packs
= NULL_TREE
;
11415 bool unsubstituted_packs
= false;
11418 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11419 bool need_local_specializations
= false;
11422 gcc_assert (PACK_EXPANSION_P (t
));
11423 pattern
= PACK_EXPANSION_PATTERN (t
);
11425 /* Add in any args remembered from an earlier partial instantiation. */
11426 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11428 levels
= TMPL_ARGS_DEPTH (args
);
11430 /* Determine the argument packs that will instantiate the parameter
11431 packs used in the expansion expression. While we're at it,
11432 compute the number of arguments to be expanded and make sure it
11434 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11435 pack
= TREE_CHAIN (pack
))
11437 tree parm_pack
= TREE_VALUE (pack
);
11438 tree arg_pack
= NULL_TREE
;
11439 tree orig_arg
= NULL_TREE
;
11442 if (TREE_CODE (parm_pack
) == BASES
)
11444 gcc_assert (parm_pack
== pattern
);
11445 if (BASES_DIRECT (parm_pack
))
11446 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11447 args
, complain
, in_decl
, false));
11449 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11450 args
, complain
, in_decl
, false));
11452 else if (builtin_pack_call_p (parm_pack
))
11454 /* ??? Support use in other patterns. */
11455 gcc_assert (parm_pack
== pattern
);
11456 return expand_builtin_pack_call (parm_pack
, args
,
11457 complain
, in_decl
);
11459 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11461 /* We know we have correct local_specializations if this
11462 expansion is at function scope, or if we're dealing with a
11463 local parameter in a requires expression; for the latter,
11464 tsubst_requires_expr set it up appropriately. */
11465 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11466 arg_pack
= retrieve_local_specialization (parm_pack
);
11468 /* We can't rely on local_specializations for a parameter
11469 name used later in a function declaration (such as in a
11470 late-specified return type). Even if it exists, it might
11471 have the wrong value for a recursive call. */
11472 need_local_specializations
= true;
11476 /* This parameter pack was used in an unevaluated context. Just
11477 make a dummy decl, since it's only used for its type. */
11478 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11479 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11480 /* Partial instantiation of the parm_pack, we can't build
11481 up an argument pack yet. */
11482 arg_pack
= NULL_TREE
;
11484 arg_pack
= make_fnparm_pack (arg_pack
);
11487 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11488 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11492 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11494 if (level
<= levels
)
11495 arg_pack
= TMPL_ARG (args
, level
, idx
);
11498 orig_arg
= arg_pack
;
11499 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11500 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11502 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11503 /* This can only happen if we forget to expand an argument
11504 pack somewhere else. Just return an error, silently. */
11506 result
= make_tree_vec (1);
11507 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11514 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11516 /* Don't bother trying to do a partial substitution with
11517 incomplete packs; we'll try again after deduction. */
11518 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11523 else if (len
!= my_len
)
11525 if (!(complain
& tf_error
))
11526 /* Fail quietly. */;
11527 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11528 error ("mismatched argument pack lengths while expanding %qT",
11531 error ("mismatched argument pack lengths while expanding %qE",
11533 return error_mark_node
;
11536 /* Keep track of the parameter packs and their corresponding
11538 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11539 TREE_TYPE (packs
) = orig_arg
;
11543 /* We can't substitute for this parameter pack. We use a flag as
11544 well as the missing_level counter because function parameter
11545 packs don't have a level. */
11546 gcc_assert (processing_template_decl
);
11547 unsubstituted_packs
= true;
11551 /* If the expansion is just T..., return the matching argument pack, unless
11552 we need to call convert_from_reference on all the elements. This is an
11553 important optimization; see c++/68422. */
11554 if (!unsubstituted_packs
11555 && TREE_PURPOSE (packs
) == pattern
)
11557 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11558 /* Types need no adjustment, nor does sizeof..., and if we still have
11559 some pack expansion args we won't do anything yet. */
11560 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11561 || PACK_EXPANSION_SIZEOF_P (t
)
11562 || pack_expansion_args_count (args
))
11564 /* Also optimize expression pack expansions if we can tell that the
11565 elements won't have reference type. */
11566 tree type
= TREE_TYPE (pattern
);
11567 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11568 && !PACK_EXPANSION_P (type
)
11569 && !WILDCARD_TYPE_P (type
))
11571 /* Otherwise use the normal path so we get convert_from_reference. */
11574 /* We cannot expand this expansion expression, because we don't have
11575 all of the argument packs we need. */
11576 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11578 /* We got some full packs, but we can't substitute them in until we
11579 have values for all the packs. So remember these until then. */
11581 t
= make_pack_expansion (pattern
, complain
);
11582 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11585 else if (unsubstituted_packs
)
11587 /* There were no real arguments, we're just replacing a parameter
11588 pack with another version of itself. Substitute into the
11589 pattern and return a PACK_EXPANSION_*. The caller will need to
11591 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11592 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11593 /*integral_constant_expression_p=*/false);
11595 t
= tsubst (pattern
, args
, complain
, in_decl
);
11596 t
= make_pack_expansion (t
, complain
);
11600 gcc_assert (len
>= 0);
11602 if (need_local_specializations
)
11604 /* We're in a late-specified return type, so create our own local
11605 specializations map; the current map is either NULL or (in the
11606 case of recursive unification) might have bindings that we don't
11607 want to use or alter. */
11608 saved_local_specializations
= local_specializations
;
11609 local_specializations
= new hash_map
<tree
, tree
>;
11612 /* For each argument in each argument pack, substitute into the
11614 result
= make_tree_vec (len
);
11615 tree elem_args
= copy_template_args (args
);
11616 for (i
= 0; i
< len
; ++i
)
11618 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11620 elem_args
, complain
,
11622 TREE_VEC_ELT (result
, i
) = t
;
11623 if (t
== error_mark_node
)
11625 result
= error_mark_node
;
11630 /* Update ARGS to restore the substitution from parameter packs to
11631 their argument packs. */
11632 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11634 tree parm
= TREE_PURPOSE (pack
);
11636 if (TREE_CODE (parm
) == PARM_DECL
11637 || TREE_CODE (parm
) == FIELD_DECL
)
11638 register_local_specialization (TREE_TYPE (pack
), parm
);
11643 if (TREE_VALUE (pack
) == NULL_TREE
)
11646 template_parm_level_and_index (parm
, &level
, &idx
);
11648 /* Update the corresponding argument. */
11649 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11650 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11653 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11657 if (need_local_specializations
)
11659 delete local_specializations
;
11660 local_specializations
= saved_local_specializations
;
11663 /* If the dependent pack arguments were such that we end up with only a
11664 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11665 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11666 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11667 return TREE_VEC_ELT (result
, 0);
11672 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11673 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11674 parameter packs; all parms generated from a function parameter pack will
11675 have the same DECL_PARM_INDEX. */
11678 get_pattern_parm (tree parm
, tree tmpl
)
11680 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11683 if (DECL_ARTIFICIAL (parm
))
11685 for (patparm
= DECL_ARGUMENTS (pattern
);
11686 patparm
; patparm
= DECL_CHAIN (patparm
))
11687 if (DECL_ARTIFICIAL (patparm
)
11688 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11693 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11694 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11695 gcc_assert (DECL_PARM_INDEX (patparm
)
11696 == DECL_PARM_INDEX (parm
));
11702 /* Make an argument pack out of the TREE_VEC VEC. */
11705 make_argument_pack (tree vec
)
11708 tree elt
= TREE_VEC_ELT (vec
, 0);
11710 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11713 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11714 TREE_CONSTANT (pack
) = 1;
11716 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11720 /* Return an exact copy of template args T that can be modified
11724 copy_template_args (tree t
)
11726 if (t
== error_mark_node
)
11729 int len
= TREE_VEC_LENGTH (t
);
11730 tree new_vec
= make_tree_vec (len
);
11732 for (int i
= 0; i
< len
; ++i
)
11734 tree elt
= TREE_VEC_ELT (t
, i
);
11735 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11736 elt
= copy_template_args (elt
);
11737 TREE_VEC_ELT (new_vec
, i
) = elt
;
11740 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11741 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11746 /* Substitute ARGS into the vector or list of template arguments T. */
11749 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11752 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11755 if (t
== error_mark_node
)
11756 return error_mark_node
;
11758 len
= TREE_VEC_LENGTH (t
);
11759 elts
= XALLOCAVEC (tree
, len
);
11761 for (i
= 0; i
< len
; i
++)
11763 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11766 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11767 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11768 else if (PACK_EXPANSION_P (orig_arg
))
11770 /* Substitute into an expansion expression. */
11771 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11773 if (TREE_CODE (new_arg
) == TREE_VEC
)
11774 /* Add to the expanded length adjustment the number of
11775 expanded arguments. We subtract one from this
11776 measurement, because the argument pack expression
11777 itself is already counted as 1 in
11778 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11779 the argument pack is empty. */
11780 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11782 else if (ARGUMENT_PACK_P (orig_arg
))
11784 /* Substitute into each of the arguments. */
11785 new_arg
= TYPE_P (orig_arg
)
11786 ? cxx_make_type (TREE_CODE (orig_arg
))
11787 : make_node (TREE_CODE (orig_arg
));
11789 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11790 args
, complain
, in_decl
);
11791 if (pack_args
== error_mark_node
)
11792 new_arg
= error_mark_node
;
11794 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11796 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11797 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11800 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11802 if (new_arg
== error_mark_node
)
11803 return error_mark_node
;
11806 if (new_arg
!= orig_arg
)
11813 /* Make space for the expanded arguments coming from template
11815 t
= make_tree_vec (len
+ expanded_len_adjust
);
11816 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11817 arguments for a member template.
11818 In that case each TREE_VEC in ORIG_T represents a level of template
11819 arguments, and ORIG_T won't carry any non defaulted argument count.
11820 It will rather be the nested TREE_VECs that will carry one.
11821 In other words, ORIG_T carries a non defaulted argument count only
11822 if it doesn't contain any nested TREE_VEC. */
11823 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11825 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11826 count
+= expanded_len_adjust
;
11827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11829 for (i
= 0, out
= 0; i
< len
; i
++)
11831 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11832 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11833 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11837 /* Now expand the template argument pack "in place". */
11838 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11839 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11843 TREE_VEC_ELT (t
, out
) = elts
[i
];
11851 /* Substitute ARGS into one level PARMS of template parameters. */
11854 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11856 if (parms
== error_mark_node
)
11857 return error_mark_node
;
11859 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11861 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11863 tree tuple
= TREE_VEC_ELT (parms
, i
);
11865 if (tuple
== error_mark_node
)
11868 TREE_VEC_ELT (new_vec
, i
) =
11869 tsubst_template_parm (tuple
, args
, complain
);
11875 /* Return the result of substituting ARGS into the template parameters
11876 given by PARMS. If there are m levels of ARGS and m + n levels of
11877 PARMS, then the result will contain n levels of PARMS. For
11878 example, if PARMS is `template <class T> template <class U>
11879 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11880 result will be `template <int*, double, class V>'. */
11883 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11885 tree r
= NULL_TREE
;
11888 /* When substituting into a template, we must set
11889 PROCESSING_TEMPLATE_DECL as the template parameters may be
11890 dependent if they are based on one-another, and the dependency
11891 predicates are short-circuit outside of templates. */
11892 ++processing_template_decl
;
11894 for (new_parms
= &r
;
11895 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11896 new_parms
= &(TREE_CHAIN (*new_parms
)),
11897 parms
= TREE_CHAIN (parms
))
11899 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11902 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11903 - TMPL_ARGS_DEPTH (args
)),
11904 new_vec
, NULL_TREE
);
11907 --processing_template_decl
;
11912 /* Return the result of substituting ARGS into one template parameter
11913 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11914 parameter and which TREE_PURPOSE is the default argument of the
11915 template parameter. */
11918 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11920 tree default_value
, parm_decl
;
11922 if (args
== NULL_TREE
11924 || t
== error_mark_node
)
11927 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11929 default_value
= TREE_PURPOSE (t
);
11930 parm_decl
= TREE_VALUE (t
);
11932 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11933 if (TREE_CODE (parm_decl
) == PARM_DECL
11934 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11935 parm_decl
= error_mark_node
;
11936 default_value
= tsubst_template_arg (default_value
, args
,
11937 complain
, NULL_TREE
);
11939 return build_tree_list (default_value
, parm_decl
);
11942 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11943 type T. If T is not an aggregate or enumeration type, it is
11944 handled as if by tsubst. IN_DECL is as for tsubst. If
11945 ENTERING_SCOPE is nonzero, T is the context for a template which
11946 we are presently tsubst'ing. Return the substituted value. */
11949 tsubst_aggr_type (tree t
,
11951 tsubst_flags_t complain
,
11953 int entering_scope
)
11955 if (t
== NULL_TREE
)
11958 switch (TREE_CODE (t
))
11961 if (TYPE_PTRMEMFUNC_P (t
))
11962 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11964 /* Fall through. */
11965 case ENUMERAL_TYPE
:
11967 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11972 int saved_unevaluated_operand
;
11973 int saved_inhibit_evaluation_warnings
;
11975 /* In "sizeof(X<I>)" we need to evaluate "I". */
11976 saved_unevaluated_operand
= cp_unevaluated_operand
;
11977 cp_unevaluated_operand
= 0;
11978 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11979 c_inhibit_evaluation_warnings
= 0;
11981 /* First, determine the context for the type we are looking
11983 context
= TYPE_CONTEXT (t
);
11984 if (context
&& TYPE_P (context
))
11986 context
= tsubst_aggr_type (context
, args
, complain
,
11987 in_decl
, /*entering_scope=*/1);
11988 /* If context is a nested class inside a class template,
11989 it may still need to be instantiated (c++/33959). */
11990 context
= complete_type (context
);
11993 /* Then, figure out what arguments are appropriate for the
11994 type we are trying to find. For example, given:
11996 template <class T> struct S;
11997 template <class T, class U> void f(T, U) { S<U> su; }
11999 and supposing that we are instantiating f<int, double>,
12000 then our ARGS will be {int, double}, but, when looking up
12001 S we only want {double}. */
12002 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12003 complain
, in_decl
);
12004 if (argvec
== error_mark_node
)
12005 r
= error_mark_node
;
12008 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12009 entering_scope
, complain
);
12010 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12013 cp_unevaluated_operand
= saved_unevaluated_operand
;
12014 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12019 /* This is not a template type, so there's nothing to do. */
12023 return tsubst (t
, args
, complain
, in_decl
);
12027 static GTY(()) hash_map
<tree
, tree
> *defarg_inst
;
12029 /* Substitute into the default argument ARG (a default argument for
12030 FN), which has the indicated TYPE. */
12033 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12034 tsubst_flags_t complain
)
12036 tree saved_class_ptr
= NULL_TREE
;
12037 tree saved_class_ref
= NULL_TREE
;
12038 int errs
= errorcount
+ sorrycount
;
12040 /* This can happen in invalid code. */
12041 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12044 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12045 parm
= chain_index (parmnum
, parm
);
12046 tree parmtype
= TREE_TYPE (parm
);
12047 if (DECL_BY_REFERENCE (parm
))
12048 parmtype
= TREE_TYPE (parmtype
);
12049 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12052 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12055 /* This default argument came from a template. Instantiate the
12056 default argument here, not in tsubst. In the case of
12065 we must be careful to do name lookup in the scope of S<T>,
12066 rather than in the current class. */
12067 push_access_scope (fn
);
12068 /* The "this" pointer is not valid in a default argument. */
12071 saved_class_ptr
= current_class_ptr
;
12072 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12073 saved_class_ref
= current_class_ref
;
12074 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12077 start_lambda_scope (parm
);
12079 push_deferring_access_checks(dk_no_deferred
);
12080 /* The default argument expression may cause implicitly defined
12081 member functions to be synthesized, which will result in garbage
12082 collection. We must treat this situation as if we were within
12083 the body of function so as to avoid collecting live data on the
12086 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12087 complain
, NULL_TREE
,
12088 /*integral_constant_expression_p=*/false);
12090 pop_deferring_access_checks();
12092 finish_lambda_scope ();
12094 /* Restore the "this" pointer. */
12097 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12098 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12101 if (errorcount
+sorrycount
> errs
12102 && (complain
& tf_warning_or_error
))
12103 inform (input_location
,
12104 " when instantiating default argument for call to %qD", fn
);
12106 /* Make sure the default argument is reasonable. */
12107 arg
= check_default_argument (type
, arg
, complain
);
12109 pop_access_scope (fn
);
12111 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12114 defarg_inst
= hash_map
<tree
,tree
>::create_ggc (37);
12115 defarg_inst
->put (parm
, arg
);
12121 /* Substitute into all the default arguments for FN. */
12124 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12129 tmpl_args
= DECL_TI_ARGS (fn
);
12131 /* If this function is not yet instantiated, we certainly don't need
12132 its default arguments. */
12133 if (uses_template_parms (tmpl_args
))
12135 /* Don't do this again for clones. */
12136 if (DECL_CLONED_FUNCTION_P (fn
))
12140 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12142 arg
= TREE_CHAIN (arg
), ++i
)
12143 if (TREE_PURPOSE (arg
))
12144 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12146 TREE_PURPOSE (arg
),
12150 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12153 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12154 tree lambda_fntype
)
12156 tree gen_tmpl
, argvec
;
12157 hashval_t hash
= 0;
12160 /* Nobody should be tsubst'ing into non-template functions. */
12161 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12163 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12165 /* If T is not dependent, just return it. */
12166 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12169 /* Calculate the most general template of which R is a
12170 specialization, and the complete set of arguments used to
12172 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12173 argvec
= tsubst_template_args (DECL_TI_ARGS
12174 (DECL_TEMPLATE_RESULT
12175 (DECL_TI_TEMPLATE (t
))),
12176 args
, complain
, in_decl
);
12177 if (argvec
== error_mark_node
)
12178 return error_mark_node
;
12180 /* Check to see if we already have this specialization. */
12181 if (!lambda_fntype
)
12183 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12184 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12188 /* We can see more levels of arguments than parameters if
12189 there was a specialization of a member template, like
12192 template <class T> struct S { template <class U> void f(); }
12193 template <> template <class U> void S<int>::f(U);
12195 Here, we'll be substituting into the specialization,
12196 because that's where we can find the code we actually
12197 want to generate, but we'll have enough arguments for
12198 the most general template.
12200 We also deal with the peculiar case:
12202 template <class T> struct S {
12203 template <class U> friend void f();
12205 template <class U> void f() {}
12207 template void f<double>();
12209 Here, the ARGS for the instantiation of will be {int,
12210 double}. But, we only need as many ARGS as there are
12211 levels of template parameters in CODE_PATTERN. We are
12212 careful not to get fooled into reducing the ARGS in
12215 template <class T> struct S { template <class U> void f(U); }
12216 template <class T> template <> void S<T>::f(int) {}
12218 which we can spot because the pattern will be a
12219 specialization in this case. */
12220 int args_depth
= TMPL_ARGS_DEPTH (args
);
12222 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12224 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12225 args
= get_innermost_template_args (args
, parms_depth
);
12229 /* This special case arises when we have something like this:
12231 template <class T> struct S {
12232 friend void f<int>(int, double);
12235 Here, the DECL_TI_TEMPLATE for the friend declaration
12236 will be an IDENTIFIER_NODE. We are being called from
12237 tsubst_friend_function, and we want only to create a
12238 new decl (R) with appropriate types so that we can call
12239 determine_specialization. */
12240 gen_tmpl
= NULL_TREE
;
12241 argvec
= NULL_TREE
;
12244 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12246 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12247 bool member
= ctx
&& TYPE_P (ctx
);
12249 if (member
&& !closure
)
12250 ctx
= tsubst_aggr_type (ctx
, args
,
12251 complain
, t
, /*entering_scope=*/1);
12253 tree type
= (lambda_fntype
? lambda_fntype
12254 : tsubst (TREE_TYPE (t
), args
,
12255 complain
| tf_fndecl_type
, in_decl
));
12256 if (type
== error_mark_node
)
12257 return error_mark_node
;
12259 /* If we hit excessive deduction depth, the type is bogus even if
12260 it isn't error_mark_node, so don't build a decl. */
12261 if (excessive_deduction_depth
)
12262 return error_mark_node
;
12264 /* We do NOT check for matching decls pushed separately at this
12265 point, as they may not represent instantiations of this
12266 template, and in any case are considered separate under the
12268 tree r
= copy_decl (t
);
12269 DECL_USE_TEMPLATE (r
) = 0;
12270 TREE_TYPE (r
) = type
;
12271 /* Clear out the mangled name and RTL for the instantiation. */
12272 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12273 SET_DECL_RTL (r
, NULL
);
12274 /* Leave DECL_INITIAL set on deleted instantiations. */
12275 if (!DECL_DELETED_FN (r
))
12276 DECL_INITIAL (r
) = NULL_TREE
;
12277 DECL_CONTEXT (r
) = ctx
;
12279 /* OpenMP UDRs have the only argument a reference to the declared
12280 type. We want to diagnose if the declared type is a reference,
12281 which is invalid, but as references to references are usually
12282 quietly merged, diagnose it here. */
12283 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12286 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12287 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12288 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12289 error_at (DECL_SOURCE_LOCATION (t
),
12290 "reference type %qT in "
12291 "%<#pragma omp declare reduction%>", argtype
);
12292 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12293 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12297 if (member
&& DECL_CONV_FN_P (r
))
12298 /* Type-conversion operator. Reconstruct the name, in
12299 case it's the name of one of the template's parameters. */
12300 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12302 tree parms
= DECL_ARGUMENTS (t
);
12304 parms
= DECL_CHAIN (parms
);
12305 parms
= tsubst (parms
, args
, complain
, t
);
12306 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12307 DECL_CONTEXT (parm
) = r
;
12310 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12311 DECL_CHAIN (tparm
) = parms
;
12314 DECL_ARGUMENTS (r
) = parms
;
12315 DECL_RESULT (r
) = NULL_TREE
;
12317 TREE_STATIC (r
) = 0;
12318 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12319 DECL_EXTERNAL (r
) = 1;
12320 /* If this is an instantiation of a function with internal
12321 linkage, we already know what object file linkage will be
12322 assigned to the instantiation. */
12323 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12324 DECL_DEFER_OUTPUT (r
) = 0;
12325 DECL_CHAIN (r
) = NULL_TREE
;
12326 DECL_PENDING_INLINE_INFO (r
) = 0;
12327 DECL_PENDING_INLINE_P (r
) = 0;
12328 DECL_SAVED_TREE (r
) = NULL_TREE
;
12329 DECL_STRUCT_FUNCTION (r
) = NULL
;
12331 /* We'll re-clone as appropriate in instantiate_template. */
12332 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12334 /* If we aren't complaining now, return on error before we register
12335 the specialization so that we'll complain eventually. */
12336 if ((complain
& tf_error
) == 0
12337 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12338 && !grok_op_properties (r
, /*complain=*/false))
12339 return error_mark_node
;
12341 /* When instantiating a constrained member, substitute
12342 into the constraints to create a new constraint. */
12343 if (tree ci
= get_constraints (t
))
12346 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12347 set_constraints (r
, ci
);
12350 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12351 this in the special friend case mentioned above where
12352 GEN_TMPL is NULL. */
12353 if (gen_tmpl
&& !closure
)
12355 DECL_TEMPLATE_INFO (r
)
12356 = build_template_info (gen_tmpl
, argvec
);
12357 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12360 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12362 /* We instantiated this while substituting into
12363 the type earlier (template/friend54.C). */
12366 /* We're not supposed to instantiate default arguments
12367 until they are called, for a template. But, for a
12370 template <class T> void f ()
12371 { extern void g(int i = T()); }
12373 we should do the substitution when the template is
12374 instantiated. We handle the member function case in
12375 instantiate_class_template since the default arguments
12376 might refer to other members of the class. */
12378 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12379 && !uses_template_parms (argvec
))
12380 tsubst_default_arguments (r
, complain
);
12383 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12385 /* Copy the list of befriending classes. */
12386 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12388 friends
= &TREE_CHAIN (*friends
))
12390 *friends
= copy_node (*friends
);
12391 TREE_VALUE (*friends
)
12392 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12395 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12397 maybe_retrofit_in_chrg (r
);
12398 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12399 return error_mark_node
;
12400 /* If this is an instantiation of a member template, clone it.
12401 If it isn't, that'll be handled by
12402 clone_constructors_and_destructors. */
12403 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12404 clone_function_decl (r
, /*update_methods=*/false);
12406 else if ((complain
& tf_error
) != 0
12407 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12408 && !grok_op_properties (r
, /*complain=*/true))
12409 return error_mark_node
;
12411 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12412 SET_DECL_FRIEND_CONTEXT (r
,
12413 tsubst (DECL_FRIEND_CONTEXT (t
),
12414 args
, complain
, in_decl
));
12416 /* Possibly limit visibility based on template args. */
12417 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12418 if (DECL_VISIBILITY_SPECIFIED (t
))
12420 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12421 DECL_ATTRIBUTES (r
)
12422 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12424 determine_visibility (r
);
12425 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12426 && !processing_template_decl
)
12427 defaulted_late_check (r
);
12429 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12430 args
, complain
, in_decl
);
12434 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12437 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12438 tree lambda_fntype
)
12440 /* We can get here when processing a member function template,
12441 member class template, or template template parameter. */
12442 tree decl
= DECL_TEMPLATE_RESULT (t
);
12448 hashval_t hash
= 0;
12450 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12452 /* Template template parameter is treated here. */
12453 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12454 if (new_type
== error_mark_node
)
12455 r
= error_mark_node
;
12456 /* If we get a real template back, return it. This can happen in
12457 the context of most_specialized_partial_spec. */
12458 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12461 /* The new TEMPLATE_DECL was built in
12462 reduce_template_parm_level. */
12463 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12467 if (!lambda_fntype
)
12469 /* We might already have an instance of this template.
12470 The ARGS are for the surrounding class type, so the
12471 full args contain the tsubst'd args for the context,
12472 plus the innermost args from the template decl. */
12473 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12474 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12475 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12476 /* Because this is a template, the arguments will still be
12477 dependent, even after substitution. If
12478 PROCESSING_TEMPLATE_DECL is not set, the dependency
12479 predicates will short-circuit. */
12480 ++processing_template_decl
;
12481 full_args
= tsubst_template_args (tmpl_args
, args
,
12482 complain
, in_decl
);
12483 --processing_template_decl
;
12484 if (full_args
== error_mark_node
)
12485 return error_mark_node
;
12487 /* If this is a default template template argument,
12488 tsubst might not have changed anything. */
12489 if (full_args
== tmpl_args
)
12492 hash
= hash_tmpl_and_args (t
, full_args
);
12493 spec
= retrieve_specialization (t
, full_args
, hash
);
12494 if (spec
!= NULL_TREE
)
12498 /* Make a new template decl. It will be similar to the
12499 original, but will record the current template arguments.
12500 We also create a new function declaration, which is just
12501 like the old one, but points to this new template, rather
12502 than the old one. */
12504 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12505 DECL_CHAIN (r
) = NULL_TREE
;
12507 // Build new template info linking to the original template decl.
12508 if (!lambda_fntype
)
12510 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12511 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12514 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12516 /* The template parameters for this new template are all the
12517 template parameters for the old template, except the
12518 outermost level of parameters. */
12519 DECL_TEMPLATE_PARMS (r
)
12520 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12523 if (TREE_CODE (decl
) == TYPE_DECL
12524 && !TYPE_DECL_ALIAS_P (decl
))
12527 ++processing_template_decl
;
12528 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12529 --processing_template_decl
;
12530 if (new_type
== error_mark_node
)
12531 return error_mark_node
;
12533 TREE_TYPE (r
) = new_type
;
12534 /* For a partial specialization, we need to keep pointing to
12535 the primary template. */
12536 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12537 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12538 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12539 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12540 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12545 ++processing_template_decl
;
12546 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12547 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12549 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12550 --processing_template_decl
;
12551 if (new_decl
== error_mark_node
)
12552 return error_mark_node
;
12554 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12555 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12556 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12559 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12560 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12564 DECL_TI_TEMPLATE (new_decl
) = r
;
12565 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12569 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12570 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12572 if (PRIMARY_TEMPLATE_P (t
))
12573 DECL_PRIMARY_TEMPLATE (r
) = r
;
12575 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12577 /* Record this non-type partial instantiation. */
12578 register_specialization (r
, t
,
12579 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12585 /* True if FN is the op() for a lambda in an uninstantiated template. */
12588 lambda_fn_in_template_p (tree fn
)
12590 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12592 tree closure
= DECL_CONTEXT (fn
);
12593 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12596 /* True if FN is the op() for a lambda regenerated from a lambda in an
12597 uninstantiated template. */
12600 regenerated_lambda_fn_p (tree fn
)
12602 return (LAMBDA_FUNCTION_P (fn
)
12603 && !DECL_TEMPLATE_INSTANTIATION (fn
));
12606 /* We're instantiating a variable from template function TCTX. Return the
12607 corresponding current enclosing scope. This gets complicated because lambda
12608 functions in templates are regenerated rather than instantiated, but generic
12609 lambda functions are subsequently instantiated. */
12612 enclosing_instantiation_of (tree tctx
)
12614 tree fn
= current_function_decl
;
12615 int lambda_count
= 0;
12617 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12618 tctx
= decl_function_context (tctx
))
12620 for (; fn
; fn
= decl_function_context (fn
))
12623 int flambda_count
= 0;
12624 for (; fn
&& regenerated_lambda_fn_p (fn
);
12625 fn
= decl_function_context (fn
))
12627 if (DECL_TEMPLATE_INFO (fn
)
12628 ? most_general_template (fn
) != most_general_template (tctx
)
12634 while (flambda_count
-- > lambda_count
)
12635 fn
= decl_function_context (fn
);
12639 gcc_unreachable ();
12642 /* Substitute the ARGS into the T, which is a _DECL. Return the
12643 result of the substitution. Issue error and warning messages under
12644 control of COMPLAIN. */
12647 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12649 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12650 location_t saved_loc
;
12651 tree r
= NULL_TREE
;
12653 hashval_t hash
= 0;
12655 /* Set the filename and linenumber to improve error-reporting. */
12656 saved_loc
= input_location
;
12657 input_location
= DECL_SOURCE_LOCATION (t
);
12659 switch (TREE_CODE (t
))
12661 case TEMPLATE_DECL
:
12662 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12665 case FUNCTION_DECL
:
12666 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12671 tree type
= NULL_TREE
;
12673 tree expanded_types
= NULL_TREE
;
12674 tree prev_r
= NULL_TREE
;
12675 tree first_r
= NULL_TREE
;
12677 if (DECL_PACK_P (t
))
12679 /* If there is a local specialization that isn't a
12680 parameter pack, it means that we're doing a "simple"
12681 substitution from inside tsubst_pack_expansion. Just
12682 return the local specialization (which will be a single
12684 tree spec
= retrieve_local_specialization (t
);
12686 && TREE_CODE (spec
) == PARM_DECL
12687 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12690 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12691 the parameters in this function parameter pack. */
12692 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12693 complain
, in_decl
);
12694 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12696 len
= TREE_VEC_LENGTH (expanded_types
);
12698 /* Zero-length parameter packs are boring. Just substitute
12701 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12706 /* All we did was update the type. Make a note of that. */
12707 type
= expanded_types
;
12708 expanded_types
= NULL_TREE
;
12712 /* Loop through all of the parameters we'll build. When T is
12713 a function parameter pack, LEN is the number of expanded
12714 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12716 for (i
= 0; i
< len
; ++i
)
12720 if (DECL_TEMPLATE_PARM_P (t
))
12721 SET_DECL_TEMPLATE_PARM_P (r
);
12723 if (expanded_types
)
12724 /* We're on the Ith parameter of the function parameter
12727 /* Get the Ith type. */
12728 type
= TREE_VEC_ELT (expanded_types
, i
);
12730 /* Rename the parameter to include the index. */
12732 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12735 /* We're dealing with a normal parameter. */
12736 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12738 type
= type_decays_to (type
);
12739 TREE_TYPE (r
) = type
;
12740 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12742 if (DECL_INITIAL (r
))
12744 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12745 DECL_INITIAL (r
) = TREE_TYPE (r
);
12747 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12748 complain
, in_decl
);
12751 DECL_CONTEXT (r
) = NULL_TREE
;
12753 if (!DECL_TEMPLATE_PARM_P (r
))
12754 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12756 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12757 args
, complain
, in_decl
);
12759 /* Keep track of the first new parameter we
12760 generate. That's what will be returned to the
12765 /* Build a proper chain of parameters when substituting
12766 into a function parameter pack. */
12768 DECL_CHAIN (prev_r
) = r
;
12771 /* If cp_unevaluated_operand is set, we're just looking for a
12772 single dummy parameter, so don't keep going. */
12773 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12774 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12775 complain
, DECL_CHAIN (t
));
12777 /* FIRST_R contains the start of the chain we've built. */
12784 tree type
= NULL_TREE
;
12785 tree vec
= NULL_TREE
;
12786 tree expanded_types
= NULL_TREE
;
12789 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12791 /* This field is a lambda capture pack. Return a TREE_VEC of
12792 the expanded fields to instantiate_class_template_1 and
12793 store them in the specializations hash table as a
12794 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12795 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12796 complain
, in_decl
);
12797 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12799 len
= TREE_VEC_LENGTH (expanded_types
);
12800 vec
= make_tree_vec (len
);
12804 /* All we did was update the type. Make a note of that. */
12805 type
= expanded_types
;
12806 expanded_types
= NULL_TREE
;
12810 for (int i
= 0; i
< len
; ++i
)
12813 if (expanded_types
)
12815 type
= TREE_VEC_ELT (expanded_types
, i
);
12817 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12820 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12822 if (type
== error_mark_node
)
12823 RETURN (error_mark_node
);
12824 TREE_TYPE (r
) = type
;
12825 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12827 if (DECL_C_BIT_FIELD (r
))
12828 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12830 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12831 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12833 /*integral_constant_expression_p=*/true);
12834 if (DECL_INITIAL (t
))
12836 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12837 NSDMI in perform_member_init. Still set DECL_INITIAL
12838 so that we know there is one. */
12839 DECL_INITIAL (r
) = void_node
;
12840 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12841 retrofit_lang_decl (r
);
12842 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12844 /* We don't have to set DECL_CONTEXT here; it is set by
12845 finish_member_declaration. */
12846 DECL_CHAIN (r
) = NULL_TREE
;
12848 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12849 args
, complain
, in_decl
);
12852 TREE_VEC_ELT (vec
, i
) = r
;
12858 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12859 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12860 register_specialization (pack
, t
, args
, false, 0);
12866 /* We reach here only for member using decls. We also need to check
12867 uses_template_parms because DECL_DEPENDENT_P is not set for a
12868 using-declaration that designates a member of the current
12869 instantiation (c++/53549). */
12870 if (DECL_DEPENDENT_P (t
)
12871 || uses_template_parms (USING_DECL_SCOPE (t
)))
12873 tree scope
= USING_DECL_SCOPE (t
);
12874 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12875 if (PACK_EXPANSION_P (scope
))
12877 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12878 int len
= TREE_VEC_LENGTH (vec
);
12879 r
= make_tree_vec (len
);
12880 for (int i
= 0; i
< len
; ++i
)
12882 tree escope
= TREE_VEC_ELT (vec
, i
);
12883 tree elt
= do_class_using_decl (escope
, name
);
12886 r
= error_mark_node
;
12891 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12892 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12894 TREE_VEC_ELT (r
, i
) = elt
;
12899 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12900 complain
, in_decl
);
12901 r
= do_class_using_decl (inst_scope
, name
);
12903 r
= error_mark_node
;
12906 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12907 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12914 DECL_CHAIN (r
) = NULL_TREE
;
12921 tree argvec
= NULL_TREE
;
12922 tree gen_tmpl
= NULL_TREE
;
12924 tree tmpl
= NULL_TREE
;
12926 tree type
= NULL_TREE
;
12929 if (TREE_TYPE (t
) == error_mark_node
)
12930 RETURN (error_mark_node
);
12932 if (TREE_CODE (t
) == TYPE_DECL
12933 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12935 /* If this is the canonical decl, we don't have to
12936 mess with instantiations, and often we can't (for
12937 typename, template type parms and such). Note that
12938 TYPE_NAME is not correct for the above test if
12939 we've copied the type for a typedef. */
12940 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12941 if (type
== error_mark_node
)
12942 RETURN (error_mark_node
);
12943 r
= TYPE_NAME (type
);
12947 /* Check to see if we already have the specialization we
12950 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12952 /* T is a static data member or namespace-scope entity.
12953 We have to substitute into namespace-scope variables
12954 (not just variable templates) because of cases like:
12956 template <class T> void f() { extern T t; }
12958 where the entity referenced is not known until
12959 instantiation time. */
12961 ctx
= DECL_CONTEXT (t
);
12962 if (DECL_CLASS_SCOPE_P (t
))
12964 ctx
= tsubst_aggr_type (ctx
, args
,
12966 in_decl
, /*entering_scope=*/1);
12967 /* If CTX is unchanged, then T is in fact the
12968 specialization we want. That situation occurs when
12969 referencing a static data member within in its own
12970 class. We can use pointer equality, rather than
12971 same_type_p, because DECL_CONTEXT is always
12973 if (ctx
== DECL_CONTEXT (t
)
12974 /* ... unless T is a member template; in which
12975 case our caller can be willing to create a
12976 specialization of that template represented
12978 && !(DECL_TI_TEMPLATE (t
)
12979 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12985 tmpl
= DECL_TI_TEMPLATE (t
);
12986 gen_tmpl
= most_general_template (tmpl
);
12987 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12988 if (argvec
!= error_mark_node
)
12989 argvec
= (coerce_innermost_template_parms
12990 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12991 argvec
, t
, complain
,
12992 /*all*/true, /*defarg*/true));
12993 if (argvec
== error_mark_node
)
12994 RETURN (error_mark_node
);
12995 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12996 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13001 /* A local variable. */
13003 /* Subsequent calls to pushdecl will fill this in. */
13005 /* Unless this is a reference to a static variable from an
13006 enclosing function, in which case we need to fill it in now. */
13007 if (TREE_STATIC (t
))
13009 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13010 if (fn
!= current_function_decl
)
13013 spec
= retrieve_local_specialization (t
);
13015 /* If we already have the specialization we need, there is
13016 nothing more to do. */
13023 /* Create a new node for the specialization we need. */
13025 if (type
== NULL_TREE
)
13027 if (is_typedef_decl (t
))
13028 type
= DECL_ORIGINAL_TYPE (t
);
13030 type
= TREE_TYPE (t
);
13032 && VAR_HAD_UNKNOWN_BOUND (t
)
13033 && type
!= error_mark_node
)
13034 type
= strip_array_domain (type
);
13035 tree sub_args
= args
;
13036 if (tree auto_node
= type_uses_auto (type
))
13038 /* Mask off any template args past the variable's context so we
13039 don't replace the auto with an unrelated argument. */
13040 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13041 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13043 /* This should never happen with the new lambda instantiation
13044 model, but keep the handling just in case. */
13045 gcc_assert (!CHECKING_P
),
13046 sub_args
= strip_innermost_template_args (args
, extra
);
13048 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13052 /* Even if the original location is out of scope, the
13053 newly substituted one is not. */
13054 DECL_DEAD_FOR_LOCAL (r
) = 0;
13055 DECL_INITIALIZED_P (r
) = 0;
13056 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13057 if (type
== error_mark_node
)
13058 RETURN (error_mark_node
);
13059 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13061 /* It may seem that this case cannot occur, since:
13066 declares a function, not a variable. However:
13069 template <typename T> void g() { T t; }
13070 template void g<f>();
13072 is an attempt to declare a variable with function
13074 error ("variable %qD has function type",
13075 /* R is not yet sufficiently initialized, so we
13076 just use its name. */
13078 RETURN (error_mark_node
);
13080 type
= complete_type (type
);
13081 /* Wait until cp_finish_decl to set this again, to handle
13082 circular dependency (template/instantiate6.C). */
13083 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13084 type
= check_var_type (DECL_NAME (r
), type
);
13086 if (DECL_HAS_VALUE_EXPR_P (t
))
13088 tree ve
= DECL_VALUE_EXPR (t
);
13089 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13090 /*constant_expression_p=*/false);
13091 if (REFERENCE_REF_P (ve
))
13093 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13094 ve
= TREE_OPERAND (ve
, 0);
13096 SET_DECL_VALUE_EXPR (r
, ve
);
13098 if (CP_DECL_THREAD_LOCAL_P (r
)
13099 && !processing_template_decl
)
13100 set_decl_tls_model (r
, decl_default_tls_model (r
));
13102 else if (DECL_SELF_REFERENCE_P (t
))
13103 SET_DECL_SELF_REFERENCE_P (r
);
13104 TREE_TYPE (r
) = type
;
13105 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13106 DECL_CONTEXT (r
) = ctx
;
13107 /* Clear out the mangled name and RTL for the instantiation. */
13108 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13109 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13110 SET_DECL_RTL (r
, NULL
);
13111 /* The initializer must not be expanded until it is required;
13112 see [temp.inst]. */
13113 DECL_INITIAL (r
) = NULL_TREE
;
13114 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13117 SET_DECL_MODE (r
, VOIDmode
);
13119 /* Possibly limit visibility based on template args. */
13120 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13121 if (DECL_VISIBILITY_SPECIFIED (t
))
13123 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13124 DECL_ATTRIBUTES (r
)
13125 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13127 determine_visibility (r
);
13132 /* A static data member declaration is always marked
13133 external when it is declared in-class, even if an
13134 initializer is present. We mimic the non-template
13135 processing here. */
13136 DECL_EXTERNAL (r
) = 1;
13137 if (DECL_NAMESPACE_SCOPE_P (t
))
13138 DECL_NOT_REALLY_EXTERN (r
) = 1;
13140 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13141 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13142 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13146 if (DECL_LANG_SPECIFIC (r
))
13147 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13148 if (!cp_unevaluated_operand
)
13149 register_local_specialization (r
, t
);
13152 DECL_CHAIN (r
) = NULL_TREE
;
13154 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13156 args
, complain
, in_decl
);
13158 /* Preserve a typedef that names a type. */
13159 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13161 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13162 set_underlying_type (r
);
13163 if (TYPE_DECL_ALIAS_P (r
))
13164 /* An alias template specialization can be dependent
13165 even if its underlying type is not. */
13166 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13169 layout_decl (r
, 0);
13174 gcc_unreachable ();
13179 /* Restore the file and line information. */
13180 input_location
= saved_loc
;
13185 /* Substitute into the ARG_TYPES of a function type.
13186 If END is a TREE_CHAIN, leave it and any following types
13190 tsubst_arg_types (tree arg_types
,
13193 tsubst_flags_t complain
,
13196 tree remaining_arg_types
;
13197 tree type
= NULL_TREE
;
13199 tree expanded_args
= NULL_TREE
;
13202 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13205 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13206 args
, end
, complain
, in_decl
);
13207 if (remaining_arg_types
== error_mark_node
)
13208 return error_mark_node
;
13210 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13212 /* For a pack expansion, perform substitution on the
13213 entire expression. Later on, we'll handle the arguments
13215 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13216 args
, complain
, in_decl
);
13218 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13219 /* So that we'll spin through the parameters, one by one. */
13220 i
= TREE_VEC_LENGTH (expanded_args
);
13223 /* We only partially substituted into the parameter
13224 pack. Our type is TYPE_PACK_EXPANSION. */
13225 type
= expanded_args
;
13226 expanded_args
= NULL_TREE
;
13234 type
= TREE_VEC_ELT (expanded_args
, i
);
13236 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13238 if (type
== error_mark_node
)
13239 return error_mark_node
;
13240 if (VOID_TYPE_P (type
))
13242 if (complain
& tf_error
)
13244 error ("invalid parameter type %qT", type
);
13246 error ("in declaration %q+D", in_decl
);
13248 return error_mark_node
;
13251 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13252 return error_mark_node
;
13254 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13255 top-level qualifiers as required. */
13256 type
= cv_unqualified (type_decays_to (type
));
13258 /* We do not substitute into default arguments here. The standard
13259 mandates that they be instantiated only when needed, which is
13260 done in build_over_call. */
13261 default_arg
= TREE_PURPOSE (arg_types
);
13263 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13264 since the new op() won't have any associated template arguments for us
13265 to refer to later. */
13266 if (lambda_fn_in_template_p (in_decl
))
13267 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13268 false/*fn*/, false/*constexpr*/);
13270 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13272 /* We've instantiated a template before its default arguments
13273 have been parsed. This can happen for a nested template
13274 class, and is not an error unless we require the default
13275 argument in a call of this function. */
13276 remaining_arg_types
=
13277 tree_cons (default_arg
, type
, remaining_arg_types
);
13278 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13281 remaining_arg_types
=
13282 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13285 return remaining_arg_types
;
13288 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13289 *not* handle the exception-specification for FNTYPE, because the
13290 initial substitution of explicitly provided template parameters
13291 during argument deduction forbids substitution into the
13292 exception-specification:
13296 All references in the function type of the function template to the
13297 corresponding template parameters are replaced by the specified tem-
13298 plate argument values. If a substitution in a template parameter or
13299 in the function type of the function template results in an invalid
13300 type, type deduction fails. [Note: The equivalent substitution in
13301 exception specifications is done only when the function is instanti-
13302 ated, at which point a program is ill-formed if the substitution
13303 results in an invalid type.] */
13306 tsubst_function_type (tree t
,
13308 tsubst_flags_t complain
,
13312 tree arg_types
= NULL_TREE
;
13315 /* The TYPE_CONTEXT is not used for function/method types. */
13316 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13318 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13320 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13322 if (late_return_type_p
)
13324 /* Substitute the argument types. */
13325 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13326 complain
, in_decl
);
13327 if (arg_types
== error_mark_node
)
13328 return error_mark_node
;
13330 tree save_ccp
= current_class_ptr
;
13331 tree save_ccr
= current_class_ref
;
13332 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13333 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13334 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13337 /* DR 1207: 'this' is in scope in the trailing return type. */
13338 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13341 /* Substitute the return type. */
13342 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13346 current_class_ptr
= save_ccp
;
13347 current_class_ref
= save_ccr
;
13351 /* Substitute the return type. */
13352 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13354 if (return_type
== error_mark_node
)
13355 return error_mark_node
;
13356 /* DR 486 clarifies that creation of a function type with an
13357 invalid return type is a deduction failure. */
13358 if (TREE_CODE (return_type
) == ARRAY_TYPE
13359 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13361 if (complain
& tf_error
)
13363 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13364 error ("function returning an array");
13366 error ("function returning a function");
13368 return error_mark_node
;
13371 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13372 return error_mark_node
;
13374 if (!late_return_type_p
)
13376 /* Substitute the argument types. */
13377 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13378 complain
, in_decl
);
13379 if (arg_types
== error_mark_node
)
13380 return error_mark_node
;
13383 /* Construct a new type node and return it. */
13384 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13386 fntype
= build_function_type (return_type
, arg_types
);
13387 fntype
= apply_memfn_quals (fntype
,
13388 type_memfn_quals (t
),
13389 type_memfn_rqual (t
));
13393 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13394 /* Don't pick up extra function qualifiers from the basetype. */
13395 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13396 if (! MAYBE_CLASS_TYPE_P (r
))
13400 Type deduction may fail for any of the following
13403 -- Attempting to create "pointer to member of T" when T
13404 is not a class type. */
13405 if (complain
& tf_error
)
13406 error ("creating pointer to member function of non-class type %qT",
13408 return error_mark_node
;
13411 fntype
= build_method_type_directly (r
, return_type
,
13412 TREE_CHAIN (arg_types
));
13413 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13415 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13417 if (late_return_type_p
)
13418 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13423 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13424 ARGS into that specification, and return the substituted
13425 specification. If there is no specification, return NULL_TREE. */
13428 tsubst_exception_specification (tree fntype
,
13430 tsubst_flags_t complain
,
13437 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13438 new_specs
= NULL_TREE
;
13439 if (specs
&& TREE_PURPOSE (specs
))
13441 /* A noexcept-specifier. */
13442 tree expr
= TREE_PURPOSE (specs
);
13443 if (TREE_CODE (expr
) == INTEGER_CST
)
13447 /* Defer instantiation of noexcept-specifiers to avoid
13448 excessive instantiations (c++/49107). */
13449 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13450 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13452 /* We already partially instantiated this member template,
13453 so combine the new args with the old. */
13454 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13455 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13456 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13457 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13461 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13462 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13466 new_specs
= tsubst_copy_and_build
13467 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13468 /*integral_constant_expression_p=*/true);
13469 new_specs
= build_noexcept_spec (new_specs
, complain
);
13473 if (! TREE_VALUE (specs
))
13480 tree expanded_specs
= NULL_TREE
;
13482 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13484 /* Expand the pack expansion type. */
13485 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13489 if (expanded_specs
== error_mark_node
)
13490 return error_mark_node
;
13491 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13492 len
= TREE_VEC_LENGTH (expanded_specs
);
13495 /* We're substituting into a member template, so
13496 we got a TYPE_PACK_EXPANSION back. Add that
13497 expansion and move on. */
13498 gcc_assert (TREE_CODE (expanded_specs
)
13499 == TYPE_PACK_EXPANSION
);
13500 new_specs
= add_exception_specifier (new_specs
,
13503 specs
= TREE_CHAIN (specs
);
13508 for (i
= 0; i
< len
; ++i
)
13510 if (expanded_specs
)
13511 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13513 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13514 if (spec
== error_mark_node
)
13516 new_specs
= add_exception_specifier (new_specs
, spec
,
13520 specs
= TREE_CHAIN (specs
);
13526 /* Take the tree structure T and replace template parameters used
13527 therein with the argument vector ARGS. IN_DECL is an associated
13528 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13529 Issue error and warning messages under control of COMPLAIN. Note
13530 that we must be relatively non-tolerant of extensions here, in
13531 order to preserve conformance; if we allow substitutions that
13532 should not be allowed, we may allow argument deductions that should
13533 not succeed, and therefore report ambiguous overload situations
13534 where there are none. In theory, we could allow the substitution,
13535 but indicate that it should have failed, and allow our caller to
13536 make sure that the right thing happens, but we don't try to do this
13539 This function is used for dealing with types, decls and the like;
13540 for expressions, use tsubst_expr or tsubst_copy. */
13543 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13545 enum tree_code code
;
13546 tree type
, r
= NULL_TREE
;
13548 if (t
== NULL_TREE
|| t
== error_mark_node
13549 || t
== integer_type_node
13550 || t
== void_type_node
13551 || t
== char_type_node
13552 || t
== unknown_type_node
13553 || TREE_CODE (t
) == NAMESPACE_DECL
13554 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13558 return tsubst_decl (t
, args
, complain
);
13560 if (args
== NULL_TREE
)
13563 code
= TREE_CODE (t
);
13565 if (code
== IDENTIFIER_NODE
)
13566 type
= IDENTIFIER_TYPE_VALUE (t
);
13568 type
= TREE_TYPE (t
);
13570 gcc_assert (type
!= unknown_type_node
);
13572 /* Reuse typedefs. We need to do this to handle dependent attributes,
13573 such as attribute aligned. */
13575 && typedef_variant_p (t
))
13577 tree decl
= TYPE_NAME (t
);
13579 if (alias_template_specialization_p (t
))
13581 /* DECL represents an alias template and we want to
13583 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13584 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13585 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13587 else if (DECL_CLASS_SCOPE_P (decl
)
13588 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13589 && uses_template_parms (DECL_CONTEXT (decl
)))
13591 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13592 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13593 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13595 else if (DECL_FUNCTION_SCOPE_P (decl
)
13596 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13597 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13598 r
= retrieve_local_specialization (decl
);
13600 /* The typedef is from a non-template context. */
13606 r
= cp_build_qualified_type_real
13607 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13608 complain
| tf_ignore_bad_quals
);
13613 /* We don't have an instantiation yet, so drop the typedef. */
13614 int quals
= cp_type_quals (t
);
13615 t
= DECL_ORIGINAL_TYPE (decl
);
13616 t
= cp_build_qualified_type_real (t
, quals
,
13617 complain
| tf_ignore_bad_quals
);
13621 bool fndecl_type
= (complain
& tf_fndecl_type
);
13622 complain
&= ~tf_fndecl_type
;
13625 && code
!= TYPENAME_TYPE
13626 && code
!= TEMPLATE_TYPE_PARM
13627 && code
!= TEMPLATE_PARM_INDEX
13628 && code
!= IDENTIFIER_NODE
13629 && code
!= FUNCTION_TYPE
13630 && code
!= METHOD_TYPE
)
13631 type
= tsubst (type
, args
, complain
, in_decl
);
13632 if (type
== error_mark_node
)
13633 return error_mark_node
;
13639 case ENUMERAL_TYPE
:
13640 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13641 /*entering_scope=*/0);
13644 case IDENTIFIER_NODE
:
13655 if (t
== integer_type_node
)
13658 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13659 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13663 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13665 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13666 /*integral_constant_expression_p=*/false);
13668 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13670 if (TREE_CODE (max
) == NOP_EXPR
13671 && TREE_SIDE_EFFECTS (omax
)
13672 && !TREE_TYPE (max
))
13673 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13675 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13676 with TREE_SIDE_EFFECTS that indicates this is not an integral
13677 constant expression. */
13678 if (processing_template_decl
13679 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13681 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13682 TREE_SIDE_EFFECTS (max
) = 1;
13685 return compute_array_index_type (NULL_TREE
, max
, complain
);
13688 case TEMPLATE_TYPE_PARM
:
13689 case TEMPLATE_TEMPLATE_PARM
:
13690 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13691 case TEMPLATE_PARM_INDEX
:
13696 tree arg
= NULL_TREE
;
13698 /* Early in template argument deduction substitution, we don't
13699 want to reduce the level of 'auto', or it will be confused
13700 with a normal template parm in subsequent deduction. */
13701 if (is_auto (t
) && (complain
& tf_partial
))
13706 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13707 template_parm_level_and_index (t
, &level
, &idx
);
13709 levels
= TMPL_ARGS_DEPTH (args
);
13710 if (level
<= levels
13711 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13713 arg
= TMPL_ARG (args
, level
, idx
);
13715 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13717 /* See through ARGUMENT_PACK_SELECT arguments. */
13718 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13719 /* If the selected argument is an expansion E, that most
13720 likely means we were called from
13721 gen_elem_of_pack_expansion_instantiation during the
13722 substituting of pack an argument pack (which Ith
13723 element is a pack expansion, where I is
13724 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13725 In this case, the Ith element resulting from this
13726 substituting is going to be a pack expansion, which
13727 pattern is the pattern of E. Let's return the
13729 gen_elem_of_pack_expansion_instantiation will
13730 build the resulting pack expansion from it. */
13731 if (PACK_EXPANSION_P (arg
))
13733 /* Make sure we aren't throwing away arg info. */
13734 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13735 arg
= PACK_EXPANSION_PATTERN (arg
);
13740 if (arg
== error_mark_node
)
13741 return error_mark_node
;
13742 else if (arg
!= NULL_TREE
)
13744 if (ARGUMENT_PACK_P (arg
))
13745 /* If ARG is an argument pack, we don't actually want to
13746 perform a substitution here, because substitutions
13747 for argument packs are only done
13748 element-by-element. We can get to this point when
13749 substituting the type of a non-type template
13750 parameter pack, when that type actually contains
13751 template parameter packs from an outer template, e.g.,
13753 template<typename... Types> struct A {
13754 template<Types... Values> struct B { };
13758 if (code
== TEMPLATE_TYPE_PARM
)
13761 gcc_assert (TYPE_P (arg
));
13763 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13765 return cp_build_qualified_type_real
13766 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13768 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13770 /* We are processing a type constructed from a
13771 template template parameter. */
13772 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13773 args
, complain
, in_decl
);
13774 if (argvec
== error_mark_node
)
13775 return error_mark_node
;
13777 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13778 || TREE_CODE (arg
) == TEMPLATE_DECL
13779 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13781 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13782 /* Consider this code:
13784 template <template <class> class Template>
13786 template <class Arg> using Bind = Template<Arg>;
13789 template <template <class> class Template, class Arg>
13790 using Instantiate = Template<Arg>; //#0
13792 template <template <class> class Template,
13795 Instantiate<Internal<Template>::template Bind,
13798 When #1 is parsed, the
13799 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13800 parameter `Template' in #0 matches the
13801 UNBOUND_CLASS_TEMPLATE representing the argument
13802 `Internal<Template>::template Bind'; We then want
13803 to assemble the type `Bind<Argument>' that can't
13804 be fully created right now, because
13805 `Internal<Template>' not being complete, the Bind
13806 template cannot be looked up in that context. So
13807 we need to "store" `Bind<Argument>' for later
13808 when the context of Bind becomes complete. Let's
13809 store that in a TYPENAME_TYPE. */
13810 return make_typename_type (TYPE_CONTEXT (arg
),
13811 build_nt (TEMPLATE_ID_EXPR
,
13812 TYPE_IDENTIFIER (arg
),
13817 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13818 are resolving nested-types in the signature of a
13819 member function templates. Otherwise ARG is a
13820 TEMPLATE_DECL and is the real template to be
13822 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13823 arg
= TYPE_NAME (arg
);
13825 r
= lookup_template_class (arg
,
13827 DECL_CONTEXT (arg
),
13828 /*entering_scope=*/0,
13830 return cp_build_qualified_type_real
13831 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13833 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13836 /* TEMPLATE_PARM_INDEX. */
13837 return convert_from_reference (unshare_expr (arg
));
13841 /* This can happen during the attempted tsubst'ing in
13842 unify. This means that we don't yet have any information
13843 about the template parameter in question. */
13846 /* If we get here, we must have been looking at a parm for a
13847 more deeply nested template. Make a new version of this
13848 template parameter, but with a lower level. */
13851 case TEMPLATE_TYPE_PARM
:
13852 case TEMPLATE_TEMPLATE_PARM
:
13853 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13854 if (cp_type_quals (t
))
13856 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13857 r
= cp_build_qualified_type_real
13858 (r
, cp_type_quals (t
),
13859 complain
| (code
== TEMPLATE_TYPE_PARM
13860 ? tf_ignore_bad_quals
: 0));
13862 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13863 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13864 && (r
= (TEMPLATE_PARM_DESCENDANTS
13865 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13866 && (r
= TREE_TYPE (r
))
13867 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13868 /* Break infinite recursion when substituting the constraints
13869 of a constrained placeholder. */;
13873 TEMPLATE_TYPE_PARM_INDEX (r
)
13874 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13875 r
, levels
, args
, complain
);
13876 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13877 TYPE_MAIN_VARIANT (r
) = r
;
13878 TYPE_POINTER_TO (r
) = NULL_TREE
;
13879 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13881 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13883 /* Propagate constraints on placeholders. */
13884 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13885 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13886 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13887 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13889 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13890 pl
= tsubst (pl
, args
, complain
, in_decl
);
13891 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13895 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13896 /* We have reduced the level of the template
13897 template parameter, but not the levels of its
13898 template parameters, so canonical_type_parameter
13899 will not be able to find the canonical template
13900 template parameter for this level. Thus, we
13901 require structural equality checking to compare
13902 TEMPLATE_TEMPLATE_PARMs. */
13903 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13904 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13905 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13907 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13909 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13911 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13912 /* We might need to substitute into the types of non-type
13913 template parameters. */
13914 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13915 complain
, in_decl
);
13916 if (tmpl
== error_mark_node
)
13917 return error_mark_node
;
13918 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13919 complain
, in_decl
);
13920 if (argvec
== error_mark_node
)
13921 return error_mark_node
;
13923 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13924 = build_template_info (tmpl
, argvec
);
13929 case TEMPLATE_PARM_INDEX
:
13930 /* OK, now substitute the type of the non-type parameter. We
13931 couldn't do it earlier because it might be an auto parameter,
13932 and we wouldn't need to if we had an argument. */
13933 type
= tsubst (type
, args
, complain
, in_decl
);
13934 if (type
== error_mark_node
)
13935 return error_mark_node
;
13936 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13940 gcc_unreachable ();
13948 tree purpose
, value
, chain
;
13950 if (t
== void_list_node
)
13953 purpose
= TREE_PURPOSE (t
);
13956 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13957 if (purpose
== error_mark_node
)
13958 return error_mark_node
;
13960 value
= TREE_VALUE (t
);
13963 value
= tsubst (value
, args
, complain
, in_decl
);
13964 if (value
== error_mark_node
)
13965 return error_mark_node
;
13967 chain
= TREE_CHAIN (t
);
13968 if (chain
&& chain
!= void_type_node
)
13970 chain
= tsubst (chain
, args
, complain
, in_decl
);
13971 if (chain
== error_mark_node
)
13972 return error_mark_node
;
13974 if (purpose
== TREE_PURPOSE (t
)
13975 && value
== TREE_VALUE (t
)
13976 && chain
== TREE_CHAIN (t
))
13978 return hash_tree_cons (purpose
, value
, chain
);
13982 /* We should never be tsubsting a binfo. */
13983 gcc_unreachable ();
13986 /* A vector of template arguments. */
13987 gcc_assert (!type
);
13988 return tsubst_template_args (t
, args
, complain
, in_decl
);
13991 case REFERENCE_TYPE
:
13993 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13998 Type deduction may fail for any of the following
14001 -- Attempting to create a pointer to reference type.
14002 -- Attempting to create a reference to a reference type or
14003 a reference to void.
14005 Core issue 106 says that creating a reference to a reference
14006 during instantiation is no longer a cause for failure. We
14007 only enforce this check in strict C++98 mode. */
14008 if ((TREE_CODE (type
) == REFERENCE_TYPE
14009 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14010 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14012 static location_t last_loc
;
14014 /* We keep track of the last time we issued this error
14015 message to avoid spewing a ton of messages during a
14016 single bad template instantiation. */
14017 if (complain
& tf_error
14018 && last_loc
!= input_location
)
14020 if (VOID_TYPE_P (type
))
14021 error ("forming reference to void");
14022 else if (code
== POINTER_TYPE
)
14023 error ("forming pointer to reference type %qT", type
);
14025 error ("forming reference to reference type %qT", type
);
14026 last_loc
= input_location
;
14029 return error_mark_node
;
14031 else if (TREE_CODE (type
) == FUNCTION_TYPE
14032 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14033 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14035 if (complain
& tf_error
)
14037 if (code
== POINTER_TYPE
)
14038 error ("forming pointer to qualified function type %qT",
14041 error ("forming reference to qualified function type %qT",
14044 return error_mark_node
;
14046 else if (code
== POINTER_TYPE
)
14048 r
= build_pointer_type (type
);
14049 if (TREE_CODE (type
) == METHOD_TYPE
)
14050 r
= build_ptrmemfunc_type (r
);
14052 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14053 /* In C++0x, during template argument substitution, when there is an
14054 attempt to create a reference to a reference type, reference
14055 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14057 "If a template-argument for a template-parameter T names a type
14058 that is a reference to a type A, an attempt to create the type
14059 'lvalue reference to cv T' creates the type 'lvalue reference to
14060 A,' while an attempt to create the type type rvalue reference to
14061 cv T' creates the type T"
14063 r
= cp_build_reference_type
14065 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14067 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14068 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14070 if (r
!= error_mark_node
)
14071 /* Will this ever be needed for TYPE_..._TO values? */
14078 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14079 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14083 Type deduction may fail for any of the following
14086 -- Attempting to create "pointer to member of T" when T
14087 is not a class type. */
14088 if (complain
& tf_error
)
14089 error ("creating pointer to member of non-class type %qT", r
);
14090 return error_mark_node
;
14092 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14094 if (complain
& tf_error
)
14095 error ("creating pointer to member reference type %qT", type
);
14096 return error_mark_node
;
14098 if (VOID_TYPE_P (type
))
14100 if (complain
& tf_error
)
14101 error ("creating pointer to member of type void");
14102 return error_mark_node
;
14104 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14105 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14107 /* The type of the implicit object parameter gets its
14108 cv-qualifiers from the FUNCTION_TYPE. */
14111 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14112 type_memfn_rqual (type
));
14113 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14114 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14118 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14122 case FUNCTION_TYPE
:
14127 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14128 if (fntype
== error_mark_node
)
14129 return error_mark_node
;
14131 /* Substitute the exception specification. */
14132 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14133 /*defer_ok*/fndecl_type
);
14134 if (specs
== error_mark_node
)
14135 return error_mark_node
;
14137 fntype
= build_exception_variant (fntype
, specs
);
14142 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14143 if (domain
== error_mark_node
)
14144 return error_mark_node
;
14146 /* As an optimization, we avoid regenerating the array type if
14147 it will obviously be the same as T. */
14148 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14151 /* These checks should match the ones in create_array_type_for_decl.
14155 The deduction may fail for any of the following reasons:
14157 -- Attempting to create an array with an element type that
14158 is void, a function type, or a reference type, or [DR337]
14159 an abstract class type. */
14160 if (VOID_TYPE_P (type
)
14161 || TREE_CODE (type
) == FUNCTION_TYPE
14162 || (TREE_CODE (type
) == ARRAY_TYPE
14163 && TYPE_DOMAIN (type
) == NULL_TREE
)
14164 || TREE_CODE (type
) == REFERENCE_TYPE
)
14166 if (complain
& tf_error
)
14167 error ("creating array of %qT", type
);
14168 return error_mark_node
;
14171 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14172 return error_mark_node
;
14174 r
= build_cplus_array_type (type
, domain
);
14176 if (TYPE_USER_ALIGN (t
))
14178 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14179 TYPE_USER_ALIGN (r
) = 1;
14185 case TYPENAME_TYPE
:
14187 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14188 in_decl
, /*entering_scope=*/1);
14189 if (ctx
== error_mark_node
)
14190 return error_mark_node
;
14192 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14193 complain
, in_decl
);
14194 if (f
== error_mark_node
)
14195 return error_mark_node
;
14197 if (!MAYBE_CLASS_TYPE_P (ctx
))
14199 if (complain
& tf_error
)
14200 error ("%qT is not a class, struct, or union type", ctx
);
14201 return error_mark_node
;
14203 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14205 /* Normally, make_typename_type does not require that the CTX
14206 have complete type in order to allow things like:
14208 template <class T> struct S { typename S<T>::X Y; };
14210 But, such constructs have already been resolved by this
14211 point, so here CTX really should have complete type, unless
14212 it's a partial instantiation. */
14213 ctx
= complete_type (ctx
);
14214 if (!COMPLETE_TYPE_P (ctx
))
14216 if (complain
& tf_error
)
14217 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14218 return error_mark_node
;
14222 f
= make_typename_type (ctx
, f
, typename_type
,
14223 complain
| tf_keep_type_decl
);
14224 if (f
== error_mark_node
)
14226 if (TREE_CODE (f
) == TYPE_DECL
)
14228 complain
|= tf_ignore_bad_quals
;
14232 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14234 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14236 if (complain
& tf_error
)
14237 error ("%qT resolves to %qT, which is not an enumeration type",
14240 return error_mark_node
;
14242 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14244 if (complain
& tf_error
)
14245 error ("%qT resolves to %qT, which is is not a class type",
14248 return error_mark_node
;
14252 return cp_build_qualified_type_real
14253 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14256 case UNBOUND_CLASS_TEMPLATE
:
14258 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14259 in_decl
, /*entering_scope=*/1);
14260 tree name
= TYPE_IDENTIFIER (t
);
14261 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14263 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14264 return error_mark_node
;
14267 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14268 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14275 ++cp_unevaluated_operand
;
14276 ++c_inhibit_evaluation_warnings
;
14278 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14280 /*integral_constant_expression_p=*/false);
14282 --cp_unevaluated_operand
;
14283 --c_inhibit_evaluation_warnings
;
14285 type
= finish_typeof (type
);
14286 return cp_build_qualified_type_real (type
,
14288 | cp_type_quals (type
),
14292 case DECLTYPE_TYPE
:
14296 ++cp_unevaluated_operand
;
14297 ++c_inhibit_evaluation_warnings
;
14299 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14300 complain
|tf_decltype
, in_decl
,
14301 /*function_p*/false,
14302 /*integral_constant_expression*/false);
14304 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14306 if (type
== NULL_TREE
)
14308 if (complain
& tf_error
)
14309 error ("empty initializer in lambda init-capture");
14310 type
= error_mark_node
;
14312 else if (TREE_CODE (type
) == TREE_LIST
)
14313 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14316 --cp_unevaluated_operand
;
14317 --c_inhibit_evaluation_warnings
;
14319 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14320 type
= lambda_capture_field_type (type
,
14321 DECLTYPE_FOR_INIT_CAPTURE (t
),
14322 DECLTYPE_FOR_REF_CAPTURE (t
));
14323 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14324 type
= lambda_proxy_type (type
);
14327 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14328 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14330 /* In a template ~id could be either a complement expression
14331 or an unqualified-id naming a destructor; if instantiating
14332 it produces an expression, it's not an id-expression or
14335 type
= finish_decltype_type (type
, id
, complain
);
14337 return cp_build_qualified_type_real (type
,
14339 | cp_type_quals (type
),
14340 complain
| tf_ignore_bad_quals
);
14343 case UNDERLYING_TYPE
:
14345 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14346 complain
, in_decl
);
14347 return finish_underlying_type (type
);
14350 case TYPE_ARGUMENT_PACK
:
14351 case NONTYPE_ARGUMENT_PACK
:
14355 if (code
== NONTYPE_ARGUMENT_PACK
)
14356 r
= make_node (code
);
14358 r
= cxx_make_type (code
);
14360 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14361 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14362 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14380 /* We should use one of the expression tsubsts for these codes. */
14381 gcc_unreachable ();
14384 sorry ("use of %qs in template", get_tree_code_name (code
));
14385 return error_mark_node
;
14389 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14390 expression on the left-hand side of the "." or "->" operator. A
14391 baselink indicates a function from a base class. Both the
14392 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14393 bases of the template class, rather than the instantiated class.
14394 In addition, lookups that were not ambiguous before may be
14395 ambiguous now. Therefore, we perform the lookup again. */
14398 tsubst_baselink (tree baselink
, tree object_type
,
14399 tree args
, tsubst_flags_t complain
, tree in_decl
)
14401 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
14403 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14404 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14406 tree optype
= BASELINK_OPTYPE (baselink
);
14407 optype
= tsubst (optype
, args
, complain
, in_decl
);
14409 tree template_args
= NULL_TREE
;
14410 bool template_id_p
= false;
14411 tree fns
= BASELINK_FUNCTIONS (baselink
);
14412 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14414 template_id_p
= true;
14415 template_args
= TREE_OPERAND (fns
, 1);
14416 fns
= TREE_OPERAND (fns
, 0);
14418 template_args
= tsubst_template_args (template_args
, args
,
14419 complain
, in_decl
);
14422 tree name
= OVL_NAME (fns
);
14423 if (IDENTIFIER_CONV_OP_P (name
))
14424 name
= make_conv_op_name (optype
);
14426 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14429 if ((complain
& tf_error
) && constructor_name_p (name
, qualifying_scope
))
14430 error ("cannot call constructor %<%T::%D%> directly",
14431 qualifying_scope
, name
);
14432 return error_mark_node
;
14435 /* If lookup found a single function, mark it as used at this point.
14436 (If it lookup found multiple functions the one selected later by
14437 overload resolution will be marked as used at that point.) */
14438 if (BASELINK_P (baselink
))
14439 fns
= BASELINK_FUNCTIONS (baselink
);
14440 if (!template_id_p
&& !really_overloaded_fn (fns
)
14441 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14442 return error_mark_node
;
14444 if (BASELINK_P (baselink
))
14446 /* Add back the template arguments, if present. */
14448 BASELINK_FUNCTIONS (baselink
)
14449 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
,
14450 BASELINK_FUNCTIONS (baselink
), template_args
);
14452 /* Update the conversion operator type. */
14453 BASELINK_OPTYPE (baselink
) = optype
;
14457 object_type
= current_class_type
;
14459 if (qualified
|| name
== complete_dtor_identifier
)
14461 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14465 /* We need to call adjust_result_of_qualified_name_lookup in case the
14466 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14467 so that we still get virtual function binding. */
14468 BASELINK_QUALIFIED_P (baselink
) = false;
14474 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14475 true if the qualified-id will be a postfix-expression in-and-of
14476 itself; false if more of the postfix-expression follows the
14477 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14481 tsubst_qualified_id (tree qualified_id
, tree args
,
14482 tsubst_flags_t complain
, tree in_decl
,
14483 bool done
, bool address_p
)
14489 tree template_args
;
14490 location_t loc
= UNKNOWN_LOCATION
;
14492 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14494 /* Figure out what name to look up. */
14495 name
= TREE_OPERAND (qualified_id
, 1);
14496 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14498 is_template
= true;
14499 loc
= EXPR_LOCATION (name
);
14500 template_args
= TREE_OPERAND (name
, 1);
14502 template_args
= tsubst_template_args (template_args
, args
,
14503 complain
, in_decl
);
14504 if (template_args
== error_mark_node
)
14505 return error_mark_node
;
14506 name
= TREE_OPERAND (name
, 0);
14510 is_template
= false;
14511 template_args
= NULL_TREE
;
14514 /* Substitute into the qualifying scope. When there are no ARGS, we
14515 are just trying to simplify a non-dependent expression. In that
14516 case the qualifying scope may be dependent, and, in any case,
14517 substituting will not help. */
14518 scope
= TREE_OPERAND (qualified_id
, 0);
14521 scope
= tsubst (scope
, args
, complain
, in_decl
);
14522 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14527 if (dependent_scope_p (scope
))
14530 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14531 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14532 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14533 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14537 if (!BASELINK_P (name
) && !DECL_P (expr
))
14539 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14541 /* A BIT_NOT_EXPR is used to represent a destructor. */
14542 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14544 error ("qualifying type %qT does not match destructor name ~%qT",
14545 scope
, TREE_OPERAND (expr
, 0));
14546 expr
= error_mark_node
;
14549 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14550 /*is_type_p=*/0, false);
14553 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14554 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14555 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14557 if (complain
& tf_error
)
14559 error ("dependent-name %qE is parsed as a non-type, but "
14560 "instantiation yields a type", qualified_id
);
14561 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14563 return error_mark_node
;
14569 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14571 /* Remember that there was a reference to this entity. */
14572 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14573 return error_mark_node
;
14576 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14578 if (complain
& tf_error
)
14579 qualified_name_lookup_error (scope
,
14580 TREE_OPERAND (qualified_id
, 1),
14581 expr
, input_location
);
14582 return error_mark_node
;
14587 if (variable_template_p (expr
))
14588 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14591 expr
= lookup_template_function (expr
, template_args
);
14594 if (expr
== error_mark_node
&& complain
& tf_error
)
14595 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14596 expr
, input_location
);
14597 else if (TYPE_P (scope
))
14599 expr
= (adjust_result_of_qualified_name_lookup
14600 (expr
, scope
, current_nonlambda_class_type ()));
14601 expr
= (finish_qualified_id_expr
14602 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14603 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14604 /*template_arg_p=*/false, complain
));
14607 /* Expressions do not generally have reference type. */
14608 if (TREE_CODE (expr
) != SCOPE_REF
14609 /* However, if we're about to form a pointer-to-member, we just
14610 want the referenced member referenced. */
14611 && TREE_CODE (expr
) != OFFSET_REF
)
14612 expr
= convert_from_reference (expr
);
14614 if (REF_PARENTHESIZED_P (qualified_id
))
14615 expr
= force_paren_expr (expr
);
14620 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14621 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14625 tsubst_init (tree init
, tree decl
, tree args
,
14626 tsubst_flags_t complain
, tree in_decl
)
14631 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14633 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14635 /* If we had an initializer but it
14636 instantiated to nothing,
14637 value-initialize the object. This will
14638 only occur when the initializer was a
14639 pack expansion where the parameter packs
14640 used in that expansion were of length
14642 init
= build_value_init (TREE_TYPE (decl
),
14644 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14645 init
= get_target_expr_sfinae (init
, complain
);
14646 if (TREE_CODE (init
) == TARGET_EXPR
)
14647 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14653 /* Like tsubst, but deals with expressions. This function just replaces
14654 template parms; to finish processing the resultant expression, use
14655 tsubst_copy_and_build or tsubst_expr. */
14658 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14660 enum tree_code code
;
14663 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14666 code
= TREE_CODE (t
);
14671 r
= retrieve_local_specialization (t
);
14673 if (r
== NULL_TREE
)
14675 /* We get here for a use of 'this' in an NSDMI. */
14676 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14677 return current_class_ptr
;
14679 /* This can happen for a parameter name used later in a function
14680 declaration (such as in a late-specified return type). Just
14681 make a dummy decl, since it's only used for its type. */
14682 gcc_assert (cp_unevaluated_operand
!= 0);
14683 r
= tsubst_decl (t
, args
, complain
);
14684 /* Give it the template pattern as its context; its true context
14685 hasn't been instantiated yet and this is good enough for
14687 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14690 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14691 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14692 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14693 return error_mark_node
;
14701 if (DECL_TEMPLATE_PARM_P (t
))
14702 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14703 /* There is no need to substitute into namespace-scope
14705 if (DECL_NAMESPACE_SCOPE_P (t
))
14707 /* If ARGS is NULL, then T is known to be non-dependent. */
14708 if (args
== NULL_TREE
)
14709 return scalar_constant_value (t
);
14711 /* Unfortunately, we cannot just call lookup_name here.
14714 template <int I> int f() {
14716 struct S { void g() { E e = a; } };
14719 When we instantiate f<7>::S::g(), say, lookup_name is not
14720 clever enough to find f<7>::a. */
14722 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14723 /*entering_scope=*/0);
14725 for (v
= TYPE_VALUES (enum_type
);
14727 v
= TREE_CHAIN (v
))
14728 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14729 return TREE_VALUE (v
);
14731 /* We didn't find the name. That should never happen; if
14732 name-lookup found it during preliminary parsing, we
14733 should find it again here during instantiation. */
14734 gcc_unreachable ();
14739 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14741 /* Check for a local specialization set up by
14742 tsubst_pack_expansion. */
14743 if (tree r
= retrieve_local_specialization (t
))
14745 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14746 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14750 /* When retrieving a capture pack from a generic lambda, remove the
14751 lambda call op's own template argument list from ARGS. Only the
14752 template arguments active for the closure type should be used to
14753 retrieve the pack specialization. */
14754 if (LAMBDA_FUNCTION_P (current_function_decl
)
14755 && (template_class_depth (DECL_CONTEXT (t
))
14756 != TMPL_ARGS_DEPTH (args
)))
14757 args
= strip_innermost_template_args (args
, 1);
14759 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14760 tsubst_decl put in the hash table. */
14761 return retrieve_specialization (t
, args
, 0);
14764 if (DECL_CONTEXT (t
))
14768 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14769 /*entering_scope=*/1);
14770 if (ctx
!= DECL_CONTEXT (t
))
14772 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14775 if (complain
& tf_error
)
14776 error ("using invalid field %qD", t
);
14777 return error_mark_node
;
14786 case FUNCTION_DECL
:
14787 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14788 r
= tsubst (t
, args
, complain
, in_decl
);
14789 else if (local_variable_p (t
)
14790 && uses_template_parms (DECL_CONTEXT (t
)))
14792 r
= retrieve_local_specialization (t
);
14793 if (r
== NULL_TREE
)
14795 /* First try name lookup to find the instantiation. */
14796 r
= lookup_name (DECL_NAME (t
));
14797 if (r
&& !is_capture_proxy (r
))
14799 /* Make sure that the one we found is the one we want. */
14800 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14801 if (ctx
!= DECL_CONTEXT (r
))
14809 /* This can happen for a variable used in a
14810 late-specified return type of a local lambda, or for a
14811 local static or constant. Building a new VAR_DECL
14812 should be OK in all those cases. */
14813 r
= tsubst_decl (t
, args
, complain
);
14814 if (local_specializations
)
14815 /* Avoid infinite recursion (79640). */
14816 register_local_specialization (r
, t
);
14817 if (decl_maybe_constant_var_p (r
))
14819 /* We can't call cp_finish_decl, so handle the
14820 initializer by hand. */
14821 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14822 complain
, in_decl
);
14823 if (!processing_template_decl
)
14824 init
= maybe_constant_init (init
);
14825 if (processing_template_decl
14826 ? potential_constant_expression (init
)
14827 : reduced_constant_expression_p (init
))
14828 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14829 = TREE_CONSTANT (r
) = true;
14830 DECL_INITIAL (r
) = init
;
14831 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14833 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14834 complain
, adc_variable_type
);
14836 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14837 || decl_constant_var_p (r
)
14838 || errorcount
|| sorrycount
);
14839 if (!processing_template_decl
14840 && !TREE_STATIC (r
))
14841 r
= process_outer_var_ref (r
, complain
);
14843 /* Remember this for subsequent uses. */
14844 if (local_specializations
)
14845 register_local_specialization (r
, t
);
14850 if (!mark_used (r
, complain
))
14851 return error_mark_node
;
14854 case NAMESPACE_DECL
:
14858 /* An OVERLOAD will always be a non-dependent overload set; an
14859 overload set from function scope will just be represented with an
14860 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14861 gcc_assert (!uses_template_parms (t
));
14862 /* We must have marked any lookups as persistent. */
14863 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14867 return tsubst_baselink (t
, current_nonlambda_class_type (),
14868 args
, complain
, in_decl
);
14870 case TEMPLATE_DECL
:
14871 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14872 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14873 args
, complain
, in_decl
);
14874 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14875 return tsubst (t
, args
, complain
, in_decl
);
14876 else if (DECL_CLASS_SCOPE_P (t
)
14877 && uses_template_parms (DECL_CONTEXT (t
)))
14879 /* Template template argument like the following example need
14882 template <template <class> class TT> struct C {};
14883 template <class T> struct D {
14884 template <class U> struct E {};
14889 We are processing the template argument `E' in #1 for
14890 the template instantiation #2. Originally, `E' is a
14891 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14892 have to substitute this with one having context `D<int>'. */
14894 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14895 if (dependent_scope_p (context
))
14897 /* When rewriting a constructor into a deduction guide, a
14898 non-dependent name can become dependent, so memtmpl<args>
14899 becomes context::template memtmpl<args>. */
14900 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14901 return build_qualified_name (type
, context
, DECL_NAME (t
),
14904 return lookup_field (context
, DECL_NAME(t
), 0, false);
14907 /* Ordinary template template argument. */
14911 case REINTERPRET_CAST_EXPR
:
14912 case CONST_CAST_EXPR
:
14913 case STATIC_CAST_EXPR
:
14914 case DYNAMIC_CAST_EXPR
:
14915 case IMPLICIT_CONV_EXPR
:
14919 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14920 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14921 return build1 (code
, type
, op0
);
14925 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14926 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14928 tree expanded
, op
= TREE_OPERAND (t
, 0);
14931 if (SIZEOF_EXPR_TYPE_P (t
))
14932 op
= TREE_TYPE (op
);
14934 ++cp_unevaluated_operand
;
14935 ++c_inhibit_evaluation_warnings
;
14936 /* We only want to compute the number of arguments. */
14937 if (PACK_EXPANSION_P (op
))
14938 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14940 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14941 args
, complain
, in_decl
);
14942 --cp_unevaluated_operand
;
14943 --c_inhibit_evaluation_warnings
;
14945 if (TREE_CODE (expanded
) == TREE_VEC
)
14947 len
= TREE_VEC_LENGTH (expanded
);
14948 /* Set TREE_USED for the benefit of -Wunused. */
14949 for (int i
= 0; i
< len
; i
++)
14950 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14951 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14954 if (expanded
== error_mark_node
)
14955 return error_mark_node
;
14956 else if (PACK_EXPANSION_P (expanded
)
14957 || (TREE_CODE (expanded
) == TREE_VEC
14958 && pack_expansion_args_count (expanded
)))
14961 if (PACK_EXPANSION_P (expanded
))
14963 else if (TREE_VEC_LENGTH (expanded
) == 1)
14964 expanded
= TREE_VEC_ELT (expanded
, 0);
14966 expanded
= make_argument_pack (expanded
);
14968 if (TYPE_P (expanded
))
14969 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14970 complain
& tf_error
);
14972 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14973 complain
& tf_error
);
14976 return build_int_cst (size_type_node
, len
);
14978 if (SIZEOF_EXPR_TYPE_P (t
))
14980 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14981 args
, complain
, in_decl
);
14982 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14983 r
= build1 (SIZEOF_EXPR
,
14984 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14985 SIZEOF_EXPR_TYPE_P (r
) = 1;
14992 case TRUTH_NOT_EXPR
:
14995 case UNARY_PLUS_EXPR
: /* Unary + */
14997 case AT_ENCODE_EXPR
:
15001 case REALPART_EXPR
:
15002 case IMAGPART_EXPR
:
15005 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15006 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15007 return build1 (code
, type
, op0
);
15010 case COMPONENT_REF
:
15015 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15016 name
= TREE_OPERAND (t
, 1);
15017 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15019 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15020 complain
, in_decl
);
15021 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15023 else if (TREE_CODE (name
) == SCOPE_REF
15024 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15026 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15027 complain
, in_decl
);
15028 name
= TREE_OPERAND (name
, 1);
15029 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15030 complain
, in_decl
);
15031 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15032 name
= build_qualified_name (/*type=*/NULL_TREE
,
15034 /*template_p=*/false);
15036 else if (BASELINK_P (name
))
15037 name
= tsubst_baselink (name
,
15038 non_reference (TREE_TYPE (object
)),
15042 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15043 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15049 case TRUNC_DIV_EXPR
:
15050 case CEIL_DIV_EXPR
:
15051 case FLOOR_DIV_EXPR
:
15052 case ROUND_DIV_EXPR
:
15053 case EXACT_DIV_EXPR
:
15057 case TRUNC_MOD_EXPR
:
15058 case FLOOR_MOD_EXPR
:
15059 case TRUTH_ANDIF_EXPR
:
15060 case TRUTH_ORIF_EXPR
:
15061 case TRUTH_AND_EXPR
:
15062 case TRUTH_OR_EXPR
:
15075 case COMPOUND_EXPR
:
15078 case PREDECREMENT_EXPR
:
15079 case PREINCREMENT_EXPR
:
15080 case POSTDECREMENT_EXPR
:
15081 case POSTINCREMENT_EXPR
:
15083 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15084 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15085 return build_nt (code
, op0
, op1
);
15090 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15091 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15092 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15093 QUALIFIED_NAME_IS_TEMPLATE (t
));
15098 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15099 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15100 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15105 int n
= VL_EXP_OPERAND_LENGTH (t
);
15106 tree result
= build_vl_exp (CALL_EXPR
, n
);
15108 for (i
= 0; i
< n
; i
++)
15109 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15110 complain
, in_decl
);
15116 case PSEUDO_DTOR_EXPR
:
15117 case VEC_PERM_EXPR
:
15119 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15120 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15121 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15122 r
= build_nt (code
, op0
, op1
, op2
);
15123 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15129 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15130 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15131 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15132 r
= build_nt (code
, op0
, op1
, op2
);
15133 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15139 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15140 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15141 r
= build_nt (code
, op0
, op1
);
15142 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15143 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15147 case TEMPLATE_ID_EXPR
:
15149 /* Substituted template arguments */
15150 tree fn
= TREE_OPERAND (t
, 0);
15151 tree targs
= TREE_OPERAND (t
, 1);
15153 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15155 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15157 return lookup_template_function (fn
, targs
);
15162 tree purpose
, value
, chain
;
15164 if (t
== void_list_node
)
15167 purpose
= TREE_PURPOSE (t
);
15169 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15170 value
= TREE_VALUE (t
);
15172 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15173 chain
= TREE_CHAIN (t
);
15174 if (chain
&& chain
!= void_type_node
)
15175 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15176 if (purpose
== TREE_PURPOSE (t
)
15177 && value
== TREE_VALUE (t
)
15178 && chain
== TREE_CHAIN (t
))
15180 return tree_cons (purpose
, value
, chain
);
15185 case ENUMERAL_TYPE
:
15187 case TEMPLATE_TYPE_PARM
:
15188 case TEMPLATE_TEMPLATE_PARM
:
15189 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15190 case TEMPLATE_PARM_INDEX
:
15192 case REFERENCE_TYPE
:
15194 case FUNCTION_TYPE
:
15197 case TYPENAME_TYPE
:
15198 case UNBOUND_CLASS_TEMPLATE
:
15200 case DECLTYPE_TYPE
:
15202 return tsubst (t
, args
, complain
, in_decl
);
15206 /* Fall through. */
15207 case IDENTIFIER_NODE
:
15208 if (IDENTIFIER_CONV_OP_P (t
))
15210 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15211 return make_conv_op_name (new_type
);
15217 /* This is handled by tsubst_copy_and_build. */
15218 gcc_unreachable ();
15222 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15223 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15224 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15227 case CLEANUP_POINT_EXPR
:
15228 /* We shouldn't have built any of these during initial template
15229 generation. Instead, they should be built during instantiation
15230 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15231 gcc_unreachable ();
15235 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15236 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15237 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15238 r
= build2 (code
, type
, op0
, op1
);
15239 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15240 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15241 && !(complain
& tf_error
))
15242 return error_mark_node
;
15246 case EXPR_PACK_EXPANSION
:
15247 error ("invalid use of pack expansion expression");
15248 return error_mark_node
;
15250 case NONTYPE_ARGUMENT_PACK
:
15251 error ("use %<...%> to expand argument pack");
15252 return error_mark_node
;
15255 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15263 /* Instantiate any typedefs in the type. */
15264 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15265 r
= fold_convert (type
, t
);
15266 gcc_assert (TREE_CODE (r
) == code
);
15271 /* These can sometimes show up in a partial instantiation, but never
15272 involve template parms. */
15273 gcc_assert (!uses_template_parms (t
));
15276 case UNARY_LEFT_FOLD_EXPR
:
15277 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15278 case UNARY_RIGHT_FOLD_EXPR
:
15279 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15280 case BINARY_LEFT_FOLD_EXPR
:
15281 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15282 case BINARY_RIGHT_FOLD_EXPR
:
15283 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15288 /* We shouldn't get here, but keep going if !flag_checking. */
15290 gcc_unreachable ();
15295 /* Helper function for tsubst_omp_clauses, used for instantiation of
15296 OMP_CLAUSE_DECL of clauses. */
15299 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15302 if (decl
== NULL_TREE
)
15305 /* Handle an OpenMP array section represented as a TREE_LIST (or
15306 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15307 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15308 TREE_LIST. We can handle it exactly the same as an array section
15309 (purpose, value, and a chain), even though the nomenclature
15310 (low_bound, length, etc) is different. */
15311 if (TREE_CODE (decl
) == TREE_LIST
)
15314 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15315 /*integral_constant_expression_p=*/false);
15316 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15317 /*integral_constant_expression_p=*/false);
15318 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15320 if (TREE_PURPOSE (decl
) == low_bound
15321 && TREE_VALUE (decl
) == length
15322 && TREE_CHAIN (decl
) == chain
)
15324 tree ret
= tree_cons (low_bound
, length
, chain
);
15325 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15326 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15329 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15330 /*integral_constant_expression_p=*/false);
15331 /* Undo convert_from_reference tsubst_expr could have called. */
15333 && REFERENCE_REF_P (ret
)
15334 && !REFERENCE_REF_P (decl
))
15335 ret
= TREE_OPERAND (ret
, 0);
15339 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15342 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15343 tree args
, tsubst_flags_t complain
, tree in_decl
)
15345 tree new_clauses
= NULL_TREE
, nc
, oc
;
15346 tree linear_no_step
= NULL_TREE
;
15348 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15350 nc
= copy_node (oc
);
15351 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15354 switch (OMP_CLAUSE_CODE (nc
))
15356 case OMP_CLAUSE_LASTPRIVATE
:
15357 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15359 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15360 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15361 in_decl
, /*integral_constant_expression_p=*/false);
15362 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15363 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15366 case OMP_CLAUSE_PRIVATE
:
15367 case OMP_CLAUSE_SHARED
:
15368 case OMP_CLAUSE_FIRSTPRIVATE
:
15369 case OMP_CLAUSE_COPYIN
:
15370 case OMP_CLAUSE_COPYPRIVATE
:
15371 case OMP_CLAUSE_UNIFORM
:
15372 case OMP_CLAUSE_DEPEND
:
15373 case OMP_CLAUSE_FROM
:
15374 case OMP_CLAUSE_TO
:
15375 case OMP_CLAUSE_MAP
:
15376 case OMP_CLAUSE_USE_DEVICE_PTR
:
15377 case OMP_CLAUSE_IS_DEVICE_PTR
:
15378 OMP_CLAUSE_DECL (nc
)
15379 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15382 case OMP_CLAUSE_TILE
:
15383 case OMP_CLAUSE_IF
:
15384 case OMP_CLAUSE_NUM_THREADS
:
15385 case OMP_CLAUSE_SCHEDULE
:
15386 case OMP_CLAUSE_COLLAPSE
:
15387 case OMP_CLAUSE_FINAL
:
15388 case OMP_CLAUSE_DEVICE
:
15389 case OMP_CLAUSE_DIST_SCHEDULE
:
15390 case OMP_CLAUSE_NUM_TEAMS
:
15391 case OMP_CLAUSE_THREAD_LIMIT
:
15392 case OMP_CLAUSE_SAFELEN
:
15393 case OMP_CLAUSE_SIMDLEN
:
15394 case OMP_CLAUSE_NUM_TASKS
:
15395 case OMP_CLAUSE_GRAINSIZE
:
15396 case OMP_CLAUSE_PRIORITY
:
15397 case OMP_CLAUSE_ORDERED
:
15398 case OMP_CLAUSE_HINT
:
15399 case OMP_CLAUSE_NUM_GANGS
:
15400 case OMP_CLAUSE_NUM_WORKERS
:
15401 case OMP_CLAUSE_VECTOR_LENGTH
:
15402 case OMP_CLAUSE_WORKER
:
15403 case OMP_CLAUSE_VECTOR
:
15404 case OMP_CLAUSE_ASYNC
:
15405 case OMP_CLAUSE_WAIT
:
15406 OMP_CLAUSE_OPERAND (nc
, 0)
15407 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15408 in_decl
, /*integral_constant_expression_p=*/false);
15410 case OMP_CLAUSE_REDUCTION
:
15411 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15413 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15414 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15416 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15417 complain
, in_decl
);
15418 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15419 = build_qualified_name (NULL_TREE
, scope
,
15420 TREE_OPERAND (placeholder
, 1),
15424 gcc_assert (identifier_p (placeholder
));
15426 OMP_CLAUSE_DECL (nc
)
15427 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15430 case OMP_CLAUSE_GANG
:
15431 case OMP_CLAUSE_ALIGNED
:
15432 OMP_CLAUSE_DECL (nc
)
15433 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15435 OMP_CLAUSE_OPERAND (nc
, 1)
15436 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15437 in_decl
, /*integral_constant_expression_p=*/false);
15439 case OMP_CLAUSE_LINEAR
:
15440 OMP_CLAUSE_DECL (nc
)
15441 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15443 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15445 gcc_assert (!linear_no_step
);
15446 linear_no_step
= nc
;
15448 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15449 OMP_CLAUSE_LINEAR_STEP (nc
)
15450 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15451 complain
, in_decl
);
15453 OMP_CLAUSE_LINEAR_STEP (nc
)
15454 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15456 /*integral_constant_expression_p=*/false);
15458 case OMP_CLAUSE_NOWAIT
:
15459 case OMP_CLAUSE_DEFAULT
:
15460 case OMP_CLAUSE_UNTIED
:
15461 case OMP_CLAUSE_MERGEABLE
:
15462 case OMP_CLAUSE_INBRANCH
:
15463 case OMP_CLAUSE_NOTINBRANCH
:
15464 case OMP_CLAUSE_PROC_BIND
:
15465 case OMP_CLAUSE_FOR
:
15466 case OMP_CLAUSE_PARALLEL
:
15467 case OMP_CLAUSE_SECTIONS
:
15468 case OMP_CLAUSE_TASKGROUP
:
15469 case OMP_CLAUSE_NOGROUP
:
15470 case OMP_CLAUSE_THREADS
:
15471 case OMP_CLAUSE_SIMD
:
15472 case OMP_CLAUSE_DEFAULTMAP
:
15473 case OMP_CLAUSE_INDEPENDENT
:
15474 case OMP_CLAUSE_AUTO
:
15475 case OMP_CLAUSE_SEQ
:
15478 gcc_unreachable ();
15480 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15481 switch (OMP_CLAUSE_CODE (nc
))
15483 case OMP_CLAUSE_SHARED
:
15484 case OMP_CLAUSE_PRIVATE
:
15485 case OMP_CLAUSE_FIRSTPRIVATE
:
15486 case OMP_CLAUSE_LASTPRIVATE
:
15487 case OMP_CLAUSE_COPYPRIVATE
:
15488 case OMP_CLAUSE_LINEAR
:
15489 case OMP_CLAUSE_REDUCTION
:
15490 case OMP_CLAUSE_USE_DEVICE_PTR
:
15491 case OMP_CLAUSE_IS_DEVICE_PTR
:
15492 /* tsubst_expr on SCOPE_REF results in returning
15493 finish_non_static_data_member result. Undo that here. */
15494 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15495 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15496 == IDENTIFIER_NODE
))
15498 tree t
= OMP_CLAUSE_DECL (nc
);
15501 switch (TREE_CODE (v
))
15503 case COMPONENT_REF
:
15507 case POINTER_PLUS_EXPR
:
15508 v
= TREE_OPERAND (v
, 0);
15511 if (DECL_CONTEXT (v
) == current_function_decl
15512 && DECL_ARTIFICIAL (v
)
15513 && DECL_NAME (v
) == this_identifier
)
15514 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15521 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15522 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15523 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15524 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15525 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15527 tree decl
= OMP_CLAUSE_DECL (nc
);
15530 retrofit_lang_decl (decl
);
15531 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15540 new_clauses
= nreverse (new_clauses
);
15541 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15543 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15544 if (linear_no_step
)
15545 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15546 if (nc
== linear_no_step
)
15548 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15552 return new_clauses
;
15555 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15558 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15561 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15563 tree purpose
, value
, chain
;
15568 if (TREE_CODE (t
) != TREE_LIST
)
15569 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15570 /*function_p=*/false,
15571 /*integral_constant_expression_p=*/false);
15573 if (t
== void_list_node
)
15576 purpose
= TREE_PURPOSE (t
);
15578 purpose
= RECUR (purpose
);
15579 value
= TREE_VALUE (t
);
15582 if (TREE_CODE (value
) != LABEL_DECL
)
15583 value
= RECUR (value
);
15586 value
= lookup_label (DECL_NAME (value
));
15587 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15588 TREE_USED (value
) = 1;
15591 chain
= TREE_CHAIN (t
);
15592 if (chain
&& chain
!= void_type_node
)
15593 chain
= RECUR (chain
);
15594 return tree_cons (purpose
, value
, chain
);
15598 /* Used to temporarily communicate the list of #pragma omp parallel
15599 clauses to #pragma omp for instantiation if they are combined
15602 static tree
*omp_parallel_combined_clauses
;
15604 /* Substitute one OMP_FOR iterator. */
15607 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15608 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15609 tree args
, tsubst_flags_t complain
, tree in_decl
,
15610 bool integral_constant_expression_p
)
15612 #define RECUR(NODE) \
15613 tsubst_expr ((NODE), args, complain, in_decl, \
15614 integral_constant_expression_p)
15615 tree decl
, init
, cond
, incr
;
15617 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15618 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15620 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15622 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15623 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15626 decl
= TREE_OPERAND (init
, 0);
15627 init
= TREE_OPERAND (init
, 1);
15628 tree decl_expr
= NULL_TREE
;
15629 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15631 /* We need to jump through some hoops to handle declarations in the
15632 init-statement, since we might need to handle auto deduction,
15633 but we need to keep control of initialization. */
15635 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15636 decl
= tsubst_decl (decl
, args
, complain
);
15640 if (TREE_CODE (decl
) == SCOPE_REF
)
15642 decl
= RECUR (decl
);
15643 if (TREE_CODE (decl
) == COMPONENT_REF
)
15647 switch (TREE_CODE (v
))
15649 case COMPONENT_REF
:
15653 case POINTER_PLUS_EXPR
:
15654 v
= TREE_OPERAND (v
, 0);
15657 if (DECL_CONTEXT (v
) == current_function_decl
15658 && DECL_ARTIFICIAL (v
)
15659 && DECL_NAME (v
) == this_identifier
)
15661 decl
= TREE_OPERAND (decl
, 1);
15662 decl
= omp_privatize_field (decl
, false);
15672 decl
= RECUR (decl
);
15674 init
= RECUR (init
);
15676 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15677 if (auto_node
&& init
)
15679 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15681 gcc_assert (!type_dependent_expression_p (decl
));
15683 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15687 /* Declare the variable, but don't let that initialize it. */
15688 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15689 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15691 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15694 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15695 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15696 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15698 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15699 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15700 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15701 NOP_EXPR
, rhs
, complain
);
15704 incr
= RECUR (incr
);
15705 TREE_VEC_ELT (declv
, i
) = decl
;
15706 TREE_VEC_ELT (initv
, i
) = init
;
15707 TREE_VEC_ELT (condv
, i
) = cond
;
15708 TREE_VEC_ELT (incrv
, i
) = incr
;
15714 /* Declare and initialize the variable. */
15722 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15724 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15726 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15727 && OMP_CLAUSE_DECL (*pc
) == decl
)
15729 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15730 && OMP_CLAUSE_DECL (*pc
) == decl
)
15734 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15736 *pc
= OMP_CLAUSE_CHAIN (c
);
15737 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15740 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15741 && OMP_CLAUSE_DECL (*pc
) == decl
)
15743 error ("iteration variable %qD should not be firstprivate",
15745 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15747 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15748 && OMP_CLAUSE_DECL (*pc
) == decl
)
15750 error ("iteration variable %qD should not be reduction",
15752 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15755 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15760 if (*pc
== NULL_TREE
)
15762 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15763 OMP_CLAUSE_DECL (c
) = decl
;
15764 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15767 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15772 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15773 if (COMPARISON_CLASS_P (cond
))
15775 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15776 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15777 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15780 cond
= RECUR (cond
);
15781 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15782 switch (TREE_CODE (incr
))
15784 case PREINCREMENT_EXPR
:
15785 case PREDECREMENT_EXPR
:
15786 case POSTINCREMENT_EXPR
:
15787 case POSTDECREMENT_EXPR
:
15788 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15789 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15792 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15793 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15795 tree rhs
= TREE_OPERAND (incr
, 1);
15796 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15797 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15798 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15799 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15800 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15804 incr
= RECUR (incr
);
15807 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15808 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15810 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15811 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15812 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15813 TREE_TYPE (decl
), lhs
,
15814 RECUR (TREE_OPERAND (incr
, 2))));
15816 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15817 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15818 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15820 tree rhs
= TREE_OPERAND (incr
, 2);
15821 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15822 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15823 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15824 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15825 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15829 incr
= RECUR (incr
);
15832 incr
= RECUR (incr
);
15836 TREE_VEC_ELT (declv
, i
) = decl
;
15837 TREE_VEC_ELT (initv
, i
) = init
;
15838 TREE_VEC_ELT (condv
, i
) = cond
;
15839 TREE_VEC_ELT (incrv
, i
) = incr
;
15843 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15844 of OMP_TARGET's body. */
15847 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15849 *walk_subtrees
= 0;
15850 switch (TREE_CODE (*tp
))
15855 case STATEMENT_LIST
:
15856 *walk_subtrees
= 1;
15864 /* Helper function for tsubst_expr. For decomposition declaration
15865 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15866 also the corresponding decls representing the identifiers
15867 of the decomposition declaration. Return DECL if successful
15868 or error_mark_node otherwise, set *FIRST to the first decl
15869 in the list chained through DECL_CHAIN and *CNT to the number
15873 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15874 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15877 tree decl2
, decl3
, prev
= decl
;
15879 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15880 for (decl2
= DECL_CHAIN (pattern_decl
);
15883 && DECL_DECOMPOSITION_P (decl2
)
15884 && DECL_NAME (decl2
);
15885 decl2
= DECL_CHAIN (decl2
))
15887 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15889 gcc_assert (errorcount
);
15890 return error_mark_node
;
15893 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
15894 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15895 tree v
= DECL_VALUE_EXPR (decl2
);
15896 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15897 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15898 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15899 SET_DECL_VALUE_EXPR (decl2
, v
);
15900 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15902 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15903 maybe_push_decl (decl3
);
15904 if (error_operand_p (decl3
))
15905 decl
= error_mark_node
;
15906 else if (decl
!= error_mark_node
15907 && DECL_CHAIN (decl3
) != prev
)
15909 gcc_assert (errorcount
);
15910 decl
= error_mark_node
;
15919 /* Like tsubst_copy for expressions, etc. but also does semantic
15923 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15924 bool integral_constant_expression_p
)
15926 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15927 #define RECUR(NODE) \
15928 tsubst_expr ((NODE), args, complain, in_decl, \
15929 integral_constant_expression_p)
15935 if (t
== NULL_TREE
|| t
== error_mark_node
)
15938 loc
= input_location
;
15939 if (EXPR_HAS_LOCATION (t
))
15940 input_location
= EXPR_LOCATION (t
);
15941 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15942 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15944 switch (TREE_CODE (t
))
15946 case STATEMENT_LIST
:
15948 tree_stmt_iterator i
;
15949 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15950 RECUR (tsi_stmt (i
));
15954 case CTOR_INITIALIZER
:
15955 finish_mem_initializers (tsubst_initializer_list
15956 (TREE_OPERAND (t
, 0), args
));
15960 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
15964 tmp
= RECUR (EXPR_STMT_EXPR (t
));
15965 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
15966 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
15968 finish_expr_stmt (tmp
);
15972 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
15973 /*attribs=*/NULL_TREE
);
15978 tree decl
, pattern_decl
;
15981 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
15982 if (TREE_CODE (decl
) == LABEL_DECL
)
15983 finish_label_decl (DECL_NAME (decl
));
15984 else if (TREE_CODE (decl
) == USING_DECL
)
15986 tree scope
= USING_DECL_SCOPE (decl
);
15987 tree name
= DECL_NAME (decl
);
15989 scope
= tsubst (scope
, args
, complain
, in_decl
);
15990 decl
= lookup_qualified_name (scope
, name
,
15991 /*is_type_p=*/false,
15992 /*complain=*/false);
15993 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15994 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15996 finish_local_using_decl (decl
, scope
, name
);
15998 else if (DECL_PACK_P (decl
))
16000 /* Don't build up decls for a variadic capture proxy, we'll
16001 instantiate the elements directly as needed. */
16004 else if (is_capture_proxy (decl
)
16005 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16007 /* We're in tsubst_lambda_expr, we've already inserted a new
16008 capture proxy, so look it up and register it. */
16009 tree inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16010 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16011 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16012 register_local_specialization (inst
, decl
);
16015 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16016 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16017 /* Don't copy the old closure; we'll create a new one in
16018 tsubst_lambda_expr. */
16022 init
= DECL_INITIAL (decl
);
16023 decl
= tsubst (decl
, args
, complain
, in_decl
);
16024 if (decl
!= error_mark_node
)
16026 /* By marking the declaration as instantiated, we avoid
16027 trying to instantiate it. Since instantiate_decl can't
16028 handle local variables, and since we've already done
16029 all that needs to be done, that's the right thing to
16032 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16034 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16035 /* Anonymous aggregates are a special case. */
16036 finish_anon_union (decl
);
16037 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16039 DECL_CONTEXT (decl
) = current_function_decl
;
16040 if (DECL_NAME (decl
) == this_identifier
)
16042 tree lam
= DECL_CONTEXT (current_function_decl
);
16043 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16044 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16046 insert_capture_proxy (decl
);
16048 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16049 /* We already did a pushtag. */;
16050 else if (TREE_CODE (decl
) == FUNCTION_DECL
16051 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16052 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16054 DECL_CONTEXT (decl
) = NULL_TREE
;
16056 DECL_CONTEXT (decl
) = current_function_decl
;
16057 cp_check_omp_declare_reduction (decl
);
16061 int const_init
= false;
16062 maybe_push_decl (decl
);
16064 && DECL_PRETTY_FUNCTION_P (decl
))
16066 /* For __PRETTY_FUNCTION__ we have to adjust the
16068 const char *const name
16069 = cxx_printable_name (current_function_decl
, 2);
16070 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16073 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16076 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16078 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16080 && DECL_DECOMPOSITION_P (decl
)
16081 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16085 decl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
16086 complain
, in_decl
, &first
,
16088 if (decl
!= error_mark_node
)
16089 cp_finish_decomp (decl
, first
, cnt
);
16099 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16100 RECUR (FOR_INIT_STMT (t
));
16101 finish_init_stmt (stmt
);
16102 tmp
= RECUR (FOR_COND (t
));
16103 finish_for_cond (tmp
, stmt
, false);
16104 tmp
= RECUR (FOR_EXPR (t
));
16105 finish_for_expr (tmp
, stmt
);
16106 RECUR (FOR_BODY (t
));
16107 finish_for_stmt (stmt
);
16110 case RANGE_FOR_STMT
:
16113 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16114 decl
= RANGE_FOR_DECL (t
);
16115 decl
= tsubst (decl
, args
, complain
, in_decl
);
16116 maybe_push_decl (decl
);
16117 expr
= RECUR (RANGE_FOR_EXPR (t
));
16118 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16122 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16123 complain
, in_decl
, &first
, &cnt
);
16124 stmt
= cp_convert_range_for (stmt
, decl
, expr
, first
, cnt
,
16125 RANGE_FOR_IVDEP (t
));
16128 stmt
= cp_convert_range_for (stmt
, decl
, expr
, NULL_TREE
, 0,
16129 RANGE_FOR_IVDEP (t
));
16130 RECUR (RANGE_FOR_BODY (t
));
16131 finish_for_stmt (stmt
);
16136 stmt
= begin_while_stmt ();
16137 tmp
= RECUR (WHILE_COND (t
));
16138 finish_while_stmt_cond (tmp
, stmt
, false);
16139 RECUR (WHILE_BODY (t
));
16140 finish_while_stmt (stmt
);
16144 stmt
= begin_do_stmt ();
16145 RECUR (DO_BODY (t
));
16146 finish_do_body (stmt
);
16147 tmp
= RECUR (DO_COND (t
));
16148 finish_do_stmt (tmp
, stmt
, false);
16152 stmt
= begin_if_stmt ();
16153 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16154 tmp
= RECUR (IF_COND (t
));
16155 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16156 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16157 /* Don't instantiate the THEN_CLAUSE. */;
16160 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16162 ++c_inhibit_evaluation_warnings
;
16163 RECUR (THEN_CLAUSE (t
));
16165 --c_inhibit_evaluation_warnings
;
16167 finish_then_clause (stmt
);
16169 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16170 /* Don't instantiate the ELSE_CLAUSE. */;
16171 else if (ELSE_CLAUSE (t
))
16173 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16174 begin_else_clause (stmt
);
16176 ++c_inhibit_evaluation_warnings
;
16177 RECUR (ELSE_CLAUSE (t
));
16179 --c_inhibit_evaluation_warnings
;
16180 finish_else_clause (stmt
);
16183 finish_if_stmt (stmt
);
16187 if (BIND_EXPR_BODY_BLOCK (t
))
16188 stmt
= begin_function_body ();
16190 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16191 ? BCS_TRY_BLOCK
: 0);
16193 RECUR (BIND_EXPR_BODY (t
));
16195 if (BIND_EXPR_BODY_BLOCK (t
))
16196 finish_function_body (stmt
);
16198 finish_compound_stmt (stmt
);
16202 finish_break_stmt ();
16205 case CONTINUE_STMT
:
16206 finish_continue_stmt ();
16210 stmt
= begin_switch_stmt ();
16211 tmp
= RECUR (SWITCH_STMT_COND (t
));
16212 finish_switch_cond (tmp
, stmt
);
16213 RECUR (SWITCH_STMT_BODY (t
));
16214 finish_switch_stmt (stmt
);
16217 case CASE_LABEL_EXPR
:
16219 tree low
= RECUR (CASE_LOW (t
));
16220 tree high
= RECUR (CASE_HIGH (t
));
16221 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16222 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16223 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16224 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16230 tree decl
= LABEL_EXPR_LABEL (t
);
16233 label
= finish_label_stmt (DECL_NAME (decl
));
16234 if (TREE_CODE (label
) == LABEL_DECL
)
16235 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16236 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16237 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16242 tmp
= GOTO_DESTINATION (t
);
16243 if (TREE_CODE (tmp
) != LABEL_DECL
)
16244 /* Computed goto's must be tsubst'd into. On the other hand,
16245 non-computed gotos must not be; the identifier in question
16246 will have no binding. */
16249 tmp
= DECL_NAME (tmp
);
16250 finish_goto_stmt (tmp
);
16255 tree string
= RECUR (ASM_STRING (t
));
16256 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16257 complain
, in_decl
);
16258 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16259 complain
, in_decl
);
16260 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16261 complain
, in_decl
);
16262 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16263 complain
, in_decl
);
16264 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16266 tree asm_expr
= tmp
;
16267 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16268 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16269 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16276 stmt
= begin_try_block ();
16277 RECUR (TRY_STMTS (t
));
16278 finish_cleanup_try_block (stmt
);
16279 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16283 tree compound_stmt
= NULL_TREE
;
16285 if (FN_TRY_BLOCK_P (t
))
16286 stmt
= begin_function_try_block (&compound_stmt
);
16288 stmt
= begin_try_block ();
16290 RECUR (TRY_STMTS (t
));
16292 if (FN_TRY_BLOCK_P (t
))
16293 finish_function_try_block (stmt
);
16295 finish_try_block (stmt
);
16297 RECUR (TRY_HANDLERS (t
));
16298 if (FN_TRY_BLOCK_P (t
))
16299 finish_function_handler_sequence (stmt
, compound_stmt
);
16301 finish_handler_sequence (stmt
);
16307 tree decl
= HANDLER_PARMS (t
);
16311 decl
= tsubst (decl
, args
, complain
, in_decl
);
16312 /* Prevent instantiate_decl from trying to instantiate
16313 this variable. We've already done all that needs to be
16315 if (decl
!= error_mark_node
)
16316 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16318 stmt
= begin_handler ();
16319 finish_handler_parms (decl
, stmt
);
16320 RECUR (HANDLER_BODY (t
));
16321 finish_handler (stmt
);
16326 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16327 if (CLASS_TYPE_P (tmp
))
16329 /* Local classes are not independent templates; they are
16330 instantiated along with their containing function. And this
16331 way we don't have to deal with pushing out of one local class
16332 to instantiate a member of another local class. */
16333 /* Closures are handled by the LAMBDA_EXPR. */
16334 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16335 complete_type (tmp
);
16336 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16338 || (TREE_CODE (fld
) == FUNCTION_DECL
16339 && !DECL_ARTIFICIAL (fld
)))
16340 && DECL_TEMPLATE_INSTANTIATION (fld
))
16341 instantiate_decl (fld
, /*defer_ok=*/false,
16342 /*expl_inst_class=*/false);
16346 case STATIC_ASSERT
:
16350 ++c_inhibit_evaluation_warnings
;
16352 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16355 /*integral_constant_expression_p=*/true);
16356 --c_inhibit_evaluation_warnings
;
16358 finish_static_assert (condition
,
16359 STATIC_ASSERT_MESSAGE (t
),
16360 STATIC_ASSERT_SOURCE_LOCATION (t
),
16361 /*member_p=*/false);
16366 case OACC_PARALLEL
:
16367 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16369 stmt
= begin_omp_parallel ();
16370 RECUR (OMP_BODY (t
));
16371 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16375 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16376 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16377 complain
, in_decl
);
16378 if (OMP_PARALLEL_COMBINED (t
))
16379 omp_parallel_combined_clauses
= &tmp
;
16380 stmt
= begin_omp_parallel ();
16381 RECUR (OMP_PARALLEL_BODY (t
));
16382 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16383 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16384 = OMP_PARALLEL_COMBINED (t
);
16385 pop_omp_privatization_clauses (r
);
16389 r
= push_omp_privatization_clauses (false);
16390 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16391 complain
, in_decl
);
16392 stmt
= begin_omp_task ();
16393 RECUR (OMP_TASK_BODY (t
));
16394 finish_omp_task (tmp
, stmt
);
16395 pop_omp_privatization_clauses (r
);
16402 case OMP_DISTRIBUTE
:
16406 tree clauses
, body
, pre_body
;
16407 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16408 tree orig_declv
= NULL_TREE
;
16409 tree incrv
= NULL_TREE
;
16410 enum c_omp_region_type ort
= C_ORT_OMP
;
16413 if (TREE_CODE (t
) == CILK_SIMD
|| TREE_CODE (t
) == CILK_FOR
)
16415 else if (TREE_CODE (t
) == OACC_LOOP
)
16418 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16419 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16421 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16423 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16424 if (OMP_FOR_ORIG_DECLS (t
))
16425 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16426 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16427 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16428 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16431 stmt
= begin_omp_structured_block ();
16433 pre_body
= push_stmt_list ();
16434 RECUR (OMP_FOR_PRE_BODY (t
));
16435 pre_body
= pop_stmt_list (pre_body
);
16437 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16438 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16439 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16440 incrv
, &clauses
, args
, complain
, in_decl
,
16441 integral_constant_expression_p
);
16442 omp_parallel_combined_clauses
= NULL
;
16444 body
= push_stmt_list ();
16445 RECUR (OMP_FOR_BODY (t
));
16446 body
= pop_stmt_list (body
);
16448 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16449 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16450 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16454 t
= make_node (TREE_CODE (t
));
16455 TREE_TYPE (t
) = void_type_node
;
16456 OMP_FOR_BODY (t
) = body
;
16457 OMP_FOR_PRE_BODY (t
) = pre_body
;
16458 OMP_FOR_CLAUSES (t
) = clauses
;
16459 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16463 add_stmt (finish_omp_structured_block (stmt
));
16464 pop_omp_privatization_clauses (r
);
16469 omp_parallel_combined_clauses
= NULL
;
16474 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16475 && OMP_TEAMS_COMBINED (t
));
16476 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16478 stmt
= push_stmt_list ();
16479 RECUR (OMP_BODY (t
));
16480 stmt
= pop_stmt_list (stmt
);
16483 OMP_BODY (t
) = stmt
;
16484 OMP_CLAUSES (t
) = tmp
;
16486 pop_omp_privatization_clauses (r
);
16490 case OMP_TARGET_DATA
:
16492 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16493 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16495 keep_next_level (true);
16496 stmt
= begin_omp_structured_block ();
16498 RECUR (OMP_BODY (t
));
16499 stmt
= finish_omp_structured_block (stmt
);
16502 OMP_BODY (t
) = stmt
;
16503 OMP_CLAUSES (t
) = tmp
;
16504 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16506 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16509 /* For combined target teams, ensure the num_teams and
16510 thread_limit clause expressions are evaluated on the host,
16511 before entering the target construct. */
16513 for (c
= OMP_TEAMS_CLAUSES (teams
);
16514 c
; c
= OMP_CLAUSE_CHAIN (c
))
16515 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16516 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16517 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16519 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16520 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16521 if (expr
== error_mark_node
)
16523 tmp
= TARGET_EXPR_SLOT (expr
);
16525 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16526 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16527 OMP_CLAUSE_FIRSTPRIVATE
);
16528 OMP_CLAUSE_DECL (tc
) = tmp
;
16529 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16530 OMP_TARGET_CLAUSES (t
) = tc
;
16539 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16540 complain
, in_decl
);
16541 OACC_DECLARE_CLAUSES (t
) = tmp
;
16545 case OMP_TARGET_UPDATE
:
16546 case OMP_TARGET_ENTER_DATA
:
16547 case OMP_TARGET_EXIT_DATA
:
16548 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16549 complain
, in_decl
);
16551 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16555 case OACC_ENTER_DATA
:
16556 case OACC_EXIT_DATA
:
16558 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16559 complain
, in_decl
);
16561 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16566 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16567 complain
, in_decl
);
16568 stmt
= push_stmt_list ();
16569 RECUR (OMP_BODY (t
));
16570 stmt
= pop_stmt_list (stmt
);
16573 OMP_BODY (t
) = stmt
;
16574 OMP_ORDERED_CLAUSES (t
) = tmp
;
16580 case OMP_TASKGROUP
:
16581 stmt
= push_stmt_list ();
16582 RECUR (OMP_BODY (t
));
16583 stmt
= pop_stmt_list (stmt
);
16586 OMP_BODY (t
) = stmt
;
16591 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16592 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16594 tree op1
= TREE_OPERAND (t
, 1);
16595 tree rhs1
= NULL_TREE
;
16597 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16599 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16600 op1
= TREE_OPERAND (op1
, 1);
16602 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16603 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16604 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16605 NULL_TREE
, NULL_TREE
, rhs1
,
16606 OMP_ATOMIC_SEQ_CST (t
));
16610 tree op1
= TREE_OPERAND (t
, 1);
16611 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16612 tree rhs1
= NULL_TREE
;
16613 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16614 enum tree_code opcode
= NOP_EXPR
;
16615 if (code
== OMP_ATOMIC_READ
)
16617 v
= RECUR (TREE_OPERAND (op1
, 0));
16618 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16620 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16621 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16623 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16624 v
= RECUR (TREE_OPERAND (op1
, 0));
16625 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16626 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16628 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16629 op11
= TREE_OPERAND (op11
, 1);
16631 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16632 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16633 opcode
= TREE_CODE (op11
);
16634 if (opcode
== MODIFY_EXPR
)
16640 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16641 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16643 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16644 OMP_ATOMIC_SEQ_CST (t
));
16648 case TRANSACTION_EXPR
:
16651 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16652 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16654 if (TRANSACTION_EXPR_IS_STMT (t
))
16656 tree body
= TRANSACTION_EXPR_BODY (t
);
16657 tree noex
= NULL_TREE
;
16658 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16660 noex
= MUST_NOT_THROW_COND (body
);
16661 if (noex
== NULL_TREE
)
16662 noex
= boolean_true_node
;
16663 body
= TREE_OPERAND (body
, 0);
16665 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16667 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16671 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16672 RECUR (TRANSACTION_EXPR_BODY (t
)),
16679 case MUST_NOT_THROW_EXPR
:
16681 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16682 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16683 RETURN (build_must_not_throw_expr (op0
, cond
));
16686 case EXPR_PACK_EXPANSION
:
16687 error ("invalid use of pack expansion expression");
16688 RETURN (error_mark_node
);
16690 case NONTYPE_ARGUMENT_PACK
:
16691 error ("use %<...%> to expand argument pack");
16692 RETURN (error_mark_node
);
16694 case CILK_SPAWN_STMT
:
16695 cfun
->calls_cilk_spawn
= 1;
16696 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
16698 case CILK_SYNC_STMT
:
16699 RETURN (build_cilk_sync ());
16701 case COMPOUND_EXPR
:
16702 tmp
= RECUR (TREE_OPERAND (t
, 0));
16703 if (tmp
== NULL_TREE
)
16704 /* If the first operand was a statement, we're done with it. */
16705 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16706 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16707 RECUR (TREE_OPERAND (t
, 1)),
16710 case ANNOTATE_EXPR
:
16711 tmp
= RECUR (TREE_OPERAND (t
, 0));
16712 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16713 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
16716 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16718 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16719 /*function_p=*/false,
16720 integral_constant_expression_p
));
16723 RETURN (NULL_TREE
);
16725 input_location
= loc
;
16731 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16732 function. For description of the body see comment above
16733 cp_parser_omp_declare_reduction_exprs. */
16736 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16738 if (t
== NULL_TREE
|| t
== error_mark_node
)
16741 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16743 tree_stmt_iterator tsi
;
16746 memset (stmts
, 0, sizeof stmts
);
16747 for (i
= 0, tsi
= tsi_start (t
);
16748 i
< 7 && !tsi_end_p (tsi
);
16749 i
++, tsi_next (&tsi
))
16750 stmts
[i
] = tsi_stmt (tsi
);
16751 gcc_assert (tsi_end_p (tsi
));
16755 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16756 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16757 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16758 args
, complain
, in_decl
);
16759 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16760 args
, complain
, in_decl
);
16761 DECL_CONTEXT (omp_out
) = current_function_decl
;
16762 DECL_CONTEXT (omp_in
) = current_function_decl
;
16763 keep_next_level (true);
16764 tree block
= begin_omp_structured_block ();
16765 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16766 block
= finish_omp_structured_block (block
);
16767 block
= maybe_cleanup_point_expr_void (block
);
16768 add_decl_expr (omp_out
);
16769 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16770 TREE_NO_WARNING (omp_out
) = 1;
16771 add_decl_expr (omp_in
);
16772 finish_expr_stmt (block
);
16776 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16777 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16778 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16779 args
, complain
, in_decl
);
16780 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16781 args
, complain
, in_decl
);
16782 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16783 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16784 keep_next_level (true);
16785 tree block
= begin_omp_structured_block ();
16786 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16787 block
= finish_omp_structured_block (block
);
16788 block
= maybe_cleanup_point_expr_void (block
);
16789 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16790 add_decl_expr (omp_priv
);
16791 add_decl_expr (omp_orig
);
16792 finish_expr_stmt (block
);
16794 add_decl_expr (omp_orig
);
16798 /* T is a postfix-expression that is not being used in a function
16799 call. Return the substituted version of T. */
16802 tsubst_non_call_postfix_expression (tree t
, tree args
,
16803 tsubst_flags_t complain
,
16806 if (TREE_CODE (t
) == SCOPE_REF
)
16807 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16808 /*done=*/false, /*address_p=*/false);
16810 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16811 /*function_p=*/false,
16812 /*integral_constant_expression_p=*/false);
16817 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16818 instantiation context. Instantiating a pack expansion containing a lambda
16819 might result in multiple lambdas all based on the same lambda in the
16823 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16825 tree oldfn
= lambda_function (t
);
16828 tree r
= build_lambda_expr ();
16830 LAMBDA_EXPR_LOCATION (r
)
16831 = LAMBDA_EXPR_LOCATION (t
);
16832 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16833 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16834 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16836 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16837 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16839 record_lambda_scope (r
);
16841 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16842 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16844 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16845 cap
= TREE_CHAIN (cap
))
16847 tree field
= TREE_PURPOSE (cap
);
16848 if (PACK_EXPANSION_P (field
))
16849 field
= PACK_EXPANSION_PATTERN (field
);
16850 field
= tsubst_decl (field
, args
, complain
);
16852 if (field
== error_mark_node
)
16853 return error_mark_node
;
16855 tree init
= TREE_VALUE (cap
);
16856 if (PACK_EXPANSION_P (init
))
16857 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
16859 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
16860 /*fn*/false, /*constexpr*/false);
16862 if (TREE_CODE (field
) == TREE_VEC
)
16864 int len
= TREE_VEC_LENGTH (field
);
16865 gcc_assert (TREE_CODE (init
) == TREE_VEC
16866 && TREE_VEC_LENGTH (init
) == len
);
16867 for (int i
= 0; i
< len
; ++i
)
16868 LAMBDA_EXPR_CAPTURE_LIST (r
)
16869 = tree_cons (TREE_VEC_ELT (field
, i
),
16870 TREE_VEC_ELT (init
, i
),
16871 LAMBDA_EXPR_CAPTURE_LIST (r
));
16875 LAMBDA_EXPR_CAPTURE_LIST (r
)
16876 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
16878 if (id_equal (DECL_NAME (field
), "__this"))
16879 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
16883 tree type
= begin_lambda_type (r
);
16885 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16886 determine_visibility (TYPE_NAME (type
));
16888 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
16890 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
16891 ? DECL_TI_TEMPLATE (oldfn
)
16894 tree fntype
= static_fn_type (oldfn
);
16896 ++processing_template_decl
;
16897 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
16899 --processing_template_decl
;
16901 if (fntype
== error_mark_node
)
16902 r
= error_mark_node
;
16905 /* Fix the type of 'this'. */
16906 fntype
= build_memfn_type (fntype
, type
,
16907 type_memfn_quals (fntype
),
16908 type_memfn_rqual (fntype
));
16912 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
16913 fn
= DECL_TEMPLATE_RESULT (tmpl
);
16914 finish_member_declaration (tmpl
);
16919 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
16920 finish_member_declaration (fn
);
16923 /* Let finish_function set this. */
16924 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
16926 bool nested
= cfun
;
16928 push_function_context ();
16930 local_specialization_stack
s (lss_copy
);
16932 tree body
= start_lambda_function (fn
, r
);
16934 register_parameter_specializations (oldfn
, fn
);
16936 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
16937 /*constexpr*/false);
16939 finish_lambda_function (body
);
16942 pop_function_context ();
16944 /* The capture list was built up in reverse order; fix that now. */
16945 LAMBDA_EXPR_CAPTURE_LIST (r
)
16946 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
16948 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
16950 maybe_add_lambda_conv_op (type
);
16953 finish_struct (type
, /*attr*/NULL_TREE
);
16955 insert_pending_capture_proxies ();
16960 /* Like tsubst but deals with expressions and performs semantic
16961 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16964 tsubst_copy_and_build (tree t
,
16966 tsubst_flags_t complain
,
16969 bool integral_constant_expression_p
)
16971 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16972 #define RECUR(NODE) \
16973 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16974 /*function_p=*/false, \
16975 integral_constant_expression_p)
16980 if (t
== NULL_TREE
|| t
== error_mark_node
)
16983 loc
= input_location
;
16984 if (EXPR_HAS_LOCATION (t
))
16985 input_location
= EXPR_LOCATION (t
);
16987 /* N3276 decltype magic only applies to calls at the top level or on the
16988 right side of a comma. */
16989 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
16990 complain
&= ~tf_decltype
;
16992 switch (TREE_CODE (t
))
16996 /* Fall through. */
16997 case IDENTIFIER_NODE
:
17001 bool non_integral_constant_expression_p
;
17002 const char *error_msg
;
17004 if (IDENTIFIER_CONV_OP_P (t
))
17006 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17007 t
= make_conv_op_name (new_type
);
17010 /* Look up the name. */
17011 decl
= lookup_name (t
);
17013 /* By convention, expressions use ERROR_MARK_NODE to indicate
17014 failure, not NULL_TREE. */
17015 if (decl
== NULL_TREE
)
17016 decl
= error_mark_node
;
17018 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17020 integral_constant_expression_p
,
17021 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17022 &non_integral_constant_expression_p
,
17023 /*template_p=*/false,
17025 /*address_p=*/false,
17026 /*template_arg_p=*/false,
17031 if (!function_p
&& identifier_p (decl
))
17033 if (complain
& tf_error
)
17034 unqualified_name_lookup_error (decl
);
17035 decl
= error_mark_node
;
17040 case TEMPLATE_ID_EXPR
:
17043 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17044 tree targs
= TREE_OPERAND (t
, 1);
17047 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17048 if (targs
== error_mark_node
)
17049 return error_mark_node
;
17051 if (TREE_CODE (templ
) == SCOPE_REF
)
17053 tree name
= TREE_OPERAND (templ
, 1);
17054 tree tid
= lookup_template_function (name
, targs
);
17055 TREE_OPERAND (templ
, 1) = tid
;
17059 if (variable_template_p (templ
))
17060 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17062 if (TREE_CODE (templ
) == COMPONENT_REF
)
17064 object
= TREE_OPERAND (templ
, 0);
17065 templ
= TREE_OPERAND (templ
, 1);
17068 object
= NULL_TREE
;
17069 templ
= lookup_template_function (templ
, targs
);
17072 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17073 object
, templ
, NULL_TREE
));
17075 RETURN (baselink_for_fns (templ
));
17080 tree r
= RECUR (TREE_OPERAND (t
, 0));
17082 if (REFERENCE_REF_P (t
))
17084 /* A type conversion to reference type will be enclosed in
17085 such an indirect ref, but the substitution of the cast
17086 will have also added such an indirect ref. */
17087 r
= convert_from_reference (r
);
17090 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17091 complain
|decltype_flag
);
17093 if (TREE_CODE (r
) == INDIRECT_REF
)
17094 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17101 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17102 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17103 RETURN (build_nop (type
, op0
));
17106 case IMPLICIT_CONV_EXPR
:
17108 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17109 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17110 int flags
= LOOKUP_IMPLICIT
;
17111 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17112 flags
= LOOKUP_NORMAL
;
17113 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17119 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17120 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17121 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17125 case REINTERPRET_CAST_EXPR
:
17126 case CONST_CAST_EXPR
:
17127 case DYNAMIC_CAST_EXPR
:
17128 case STATIC_CAST_EXPR
:
17131 tree op
, r
= NULL_TREE
;
17133 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17134 if (integral_constant_expression_p
17135 && !cast_valid_in_integral_constant_expression_p (type
))
17137 if (complain
& tf_error
)
17138 error ("a cast to a type other than an integral or "
17139 "enumeration type cannot appear in a constant-expression");
17140 RETURN (error_mark_node
);
17143 op
= RECUR (TREE_OPERAND (t
, 0));
17145 warning_sentinel
s(warn_useless_cast
);
17146 switch (TREE_CODE (t
))
17149 r
= build_functional_cast (type
, op
, complain
);
17151 case REINTERPRET_CAST_EXPR
:
17152 r
= build_reinterpret_cast (type
, op
, complain
);
17154 case CONST_CAST_EXPR
:
17155 r
= build_const_cast (type
, op
, complain
);
17157 case DYNAMIC_CAST_EXPR
:
17158 r
= build_dynamic_cast (type
, op
, complain
);
17160 case STATIC_CAST_EXPR
:
17161 r
= build_static_cast (type
, op
, complain
);
17164 gcc_unreachable ();
17170 case POSTDECREMENT_EXPR
:
17171 case POSTINCREMENT_EXPR
:
17172 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17173 args
, complain
, in_decl
);
17174 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17175 complain
|decltype_flag
));
17177 case PREDECREMENT_EXPR
:
17178 case PREINCREMENT_EXPR
:
17182 case TRUTH_NOT_EXPR
:
17183 case UNARY_PLUS_EXPR
: /* Unary + */
17184 case REALPART_EXPR
:
17185 case IMAGPART_EXPR
:
17186 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17187 RECUR (TREE_OPERAND (t
, 0)),
17188 complain
|decltype_flag
));
17190 case FIX_TRUNC_EXPR
:
17191 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17195 op1
= TREE_OPERAND (t
, 0);
17196 if (TREE_CODE (op1
) == LABEL_DECL
)
17197 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17198 EXPR_LOCATION (op1
)));
17199 if (TREE_CODE (op1
) == SCOPE_REF
)
17200 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17201 /*done=*/true, /*address_p=*/true);
17203 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17205 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17206 complain
|decltype_flag
));
17211 case TRUNC_DIV_EXPR
:
17212 case CEIL_DIV_EXPR
:
17213 case FLOOR_DIV_EXPR
:
17214 case ROUND_DIV_EXPR
:
17215 case EXACT_DIV_EXPR
:
17219 case TRUNC_MOD_EXPR
:
17220 case FLOOR_MOD_EXPR
:
17221 case TRUTH_ANDIF_EXPR
:
17222 case TRUTH_ORIF_EXPR
:
17223 case TRUTH_AND_EXPR
:
17224 case TRUTH_OR_EXPR
:
17240 warning_sentinel
s1(warn_type_limits
);
17241 warning_sentinel
s2(warn_div_by_zero
);
17242 warning_sentinel
s3(warn_logical_op
);
17243 warning_sentinel
s4(warn_tautological_compare
);
17244 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17245 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17246 tree r
= build_x_binary_op
17247 (input_location
, TREE_CODE (t
),
17249 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17251 : TREE_CODE (TREE_OPERAND (t
, 0))),
17253 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17255 : TREE_CODE (TREE_OPERAND (t
, 1))),
17257 complain
|decltype_flag
);
17258 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17259 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17264 case POINTER_PLUS_EXPR
:
17266 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17267 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17268 return fold_build_pointer_plus (op0
, op1
);
17272 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17273 /*address_p=*/false));
17275 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17276 args
, complain
, in_decl
);
17277 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17278 RECUR (TREE_OPERAND (t
, 1)),
17279 complain
|decltype_flag
));
17281 case ARRAY_NOTATION_REF
:
17283 tree start_index
, length
, stride
;
17284 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
17285 args
, complain
, in_decl
);
17286 start_index
= RECUR (ARRAY_NOTATION_START (t
));
17287 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
17288 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
17289 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
17290 length
, stride
, TREE_TYPE (op1
)));
17293 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17294 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17295 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17302 op1
= TREE_OPERAND (t
, 0);
17303 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17304 op1
= TREE_TYPE (op1
);
17307 /* When there are no ARGS, we are trying to evaluate a
17308 non-dependent expression from the parser. Trying to do
17309 the substitutions may not work. */
17311 op1
= TREE_TYPE (op1
);
17315 ++cp_unevaluated_operand
;
17316 ++c_inhibit_evaluation_warnings
;
17318 op1
= tsubst (op1
, args
, complain
, in_decl
);
17320 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17321 /*function_p=*/false,
17322 /*integral_constant_expression_p=*/
17324 --cp_unevaluated_operand
;
17325 --c_inhibit_evaluation_warnings
;
17328 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17329 complain
& tf_error
);
17331 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17332 complain
& tf_error
);
17333 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17335 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17337 if (!processing_template_decl
&& TYPE_P (op1
))
17339 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17340 build1 (NOP_EXPR
, op1
, error_mark_node
));
17341 SIZEOF_EXPR_TYPE_P (r
) = 1;
17344 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17345 TREE_SIDE_EFFECTS (r
) = 0;
17346 TREE_READONLY (r
) = 1;
17348 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17353 case AT_ENCODE_EXPR
:
17355 op1
= TREE_OPERAND (t
, 0);
17356 ++cp_unevaluated_operand
;
17357 ++c_inhibit_evaluation_warnings
;
17358 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17359 /*function_p=*/false,
17360 /*integral_constant_expression_p=*/false);
17361 --cp_unevaluated_operand
;
17362 --c_inhibit_evaluation_warnings
;
17363 RETURN (objc_build_encode_expr (op1
));
17366 case NOEXCEPT_EXPR
:
17367 op1
= TREE_OPERAND (t
, 0);
17368 ++cp_unevaluated_operand
;
17369 ++c_inhibit_evaluation_warnings
;
17370 ++cp_noexcept_operand
;
17371 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17372 /*function_p=*/false,
17373 /*integral_constant_expression_p=*/false);
17374 --cp_unevaluated_operand
;
17375 --c_inhibit_evaluation_warnings
;
17376 --cp_noexcept_operand
;
17377 RETURN (finish_noexcept_expr (op1
, complain
));
17381 warning_sentinel
s(warn_div_by_zero
);
17382 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17383 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17384 tree r
= build_x_modify_expr
17385 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17386 complain
|decltype_flag
);
17387 /* TREE_NO_WARNING must be set if either the expression was
17388 parenthesized or it uses an operator such as >>= rather
17389 than plain assignment. In the former case, it was already
17390 set and must be copied. In the latter case,
17391 build_x_modify_expr sets it and it must not be reset
17393 if (TREE_NO_WARNING (t
))
17394 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17400 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17401 args
, complain
, in_decl
);
17402 /* Remember that there was a reference to this entity. */
17404 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17405 RETURN (error_mark_node
);
17406 RETURN (build_x_arrow (input_location
, op1
, complain
));
17410 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17411 tree init
= RECUR (TREE_OPERAND (t
, 3));
17412 vec
<tree
, va_gc
> *placement_vec
;
17413 vec
<tree
, va_gc
> *init_vec
;
17416 if (placement
== NULL_TREE
)
17417 placement_vec
= NULL
;
17420 placement_vec
= make_tree_vector ();
17421 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17422 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17425 /* If there was an initializer in the original tree, but it
17426 instantiated to an empty list, then we should pass a
17427 non-NULL empty vector to tell build_new that it was an
17428 empty initializer() rather than no initializer. This can
17429 only happen when the initializer is a pack expansion whose
17430 parameter packs are of length zero. */
17431 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17435 init_vec
= make_tree_vector ();
17436 if (init
== void_node
)
17437 gcc_assert (init_vec
!= NULL
);
17440 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17441 vec_safe_push (init_vec
, TREE_VALUE (init
));
17445 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17446 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17447 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17448 NEW_EXPR_USE_GLOBAL (t
),
17451 if (placement_vec
!= NULL
)
17452 release_tree_vector (placement_vec
);
17453 if (init_vec
!= NULL
)
17454 release_tree_vector (init_vec
);
17461 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17462 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17463 RETURN (delete_sanity (op0
, op1
,
17464 DELETE_EXPR_USE_VEC (t
),
17465 DELETE_EXPR_USE_GLOBAL (t
),
17469 case COMPOUND_EXPR
:
17471 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17472 complain
& ~tf_decltype
, in_decl
,
17473 /*function_p=*/false,
17474 integral_constant_expression_p
);
17475 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17477 RECUR (TREE_OPERAND (t
, 1)),
17478 complain
|decltype_flag
));
17484 vec
<tree
, va_gc
> *call_args
;
17485 unsigned int nargs
, i
;
17490 function
= CALL_EXPR_FN (t
);
17491 /* Internal function with no arguments. */
17492 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17495 /* When we parsed the expression, we determined whether or
17496 not Koenig lookup should be performed. */
17497 koenig_p
= KOENIG_LOOKUP_P (t
);
17498 if (function
== NULL_TREE
)
17501 qualified_p
= false;
17503 else if (TREE_CODE (function
) == SCOPE_REF
)
17505 qualified_p
= true;
17506 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17508 /*address_p=*/false);
17510 else if (koenig_p
&& identifier_p (function
))
17512 /* Do nothing; calling tsubst_copy_and_build on an identifier
17513 would incorrectly perform unqualified lookup again.
17515 Note that we can also have an IDENTIFIER_NODE if the earlier
17516 unqualified lookup found a member function; in that case
17517 koenig_p will be false and we do want to do the lookup
17518 again to find the instantiated member function.
17520 FIXME but doing that causes c++/15272, so we need to stop
17521 using IDENTIFIER_NODE in that situation. */
17522 qualified_p
= false;
17526 if (TREE_CODE (function
) == COMPONENT_REF
)
17528 tree op
= TREE_OPERAND (function
, 1);
17530 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17531 || (BASELINK_P (op
)
17532 && BASELINK_QUALIFIED_P (op
)));
17535 qualified_p
= false;
17537 if (TREE_CODE (function
) == ADDR_EXPR
17538 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17539 /* Avoid error about taking the address of a constructor. */
17540 function
= TREE_OPERAND (function
, 0);
17542 function
= tsubst_copy_and_build (function
, args
, complain
,
17545 integral_constant_expression_p
);
17547 if (BASELINK_P (function
))
17548 qualified_p
= true;
17551 nargs
= call_expr_nargs (t
);
17552 call_args
= make_tree_vector ();
17553 for (i
= 0; i
< nargs
; ++i
)
17555 tree arg
= CALL_EXPR_ARG (t
, i
);
17557 if (!PACK_EXPANSION_P (arg
))
17558 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17561 /* Expand the pack expansion and push each entry onto
17563 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17564 if (TREE_CODE (arg
) == TREE_VEC
)
17566 unsigned int len
, j
;
17568 len
= TREE_VEC_LENGTH (arg
);
17569 for (j
= 0; j
< len
; ++j
)
17571 tree value
= TREE_VEC_ELT (arg
, j
);
17572 if (value
!= NULL_TREE
)
17573 value
= convert_from_reference (value
);
17574 vec_safe_push (call_args
, value
);
17579 /* A partial substitution. Add one entry. */
17580 vec_safe_push (call_args
, arg
);
17585 /* We do not perform argument-dependent lookup if normal
17586 lookup finds a non-function, in accordance with the
17587 expected resolution of DR 218. */
17589 && ((is_overloaded_fn (function
)
17590 /* If lookup found a member function, the Koenig lookup is
17591 not appropriate, even if an unqualified-name was used
17592 to denote the function. */
17593 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17594 || identifier_p (function
))
17595 /* Only do this when substitution turns a dependent call
17596 into a non-dependent call. */
17597 && type_dependent_expression_p_push (t
)
17598 && !any_type_dependent_arguments_p (call_args
))
17599 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17601 if (function
!= NULL_TREE
17602 && identifier_p (function
)
17603 && !any_type_dependent_arguments_p (call_args
))
17605 if (koenig_p
&& (complain
& tf_warning_or_error
))
17607 /* For backwards compatibility and good diagnostics, try
17608 the unqualified lookup again if we aren't in SFINAE
17610 tree unq
= (tsubst_copy_and_build
17611 (function
, args
, complain
, in_decl
, true,
17612 integral_constant_expression_p
));
17613 if (unq
== error_mark_node
)
17615 release_tree_vector (call_args
);
17616 RETURN (error_mark_node
);
17619 if (unq
!= function
)
17621 /* In a lambda fn, we have to be careful to not
17622 introduce new this captures. Legacy code can't
17623 be using lambdas anyway, so it's ok to be
17625 bool in_lambda
= (current_class_type
17626 && LAMBDA_TYPE_P (current_class_type
));
17627 char const *const msg
17628 = G_("%qD was not declared in this scope, "
17629 "and no declarations were found by "
17630 "argument-dependent lookup at the point "
17631 "of instantiation");
17635 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17638 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17644 if (INDIRECT_REF_P (fn
))
17645 fn
= TREE_OPERAND (fn
, 0);
17646 if (is_overloaded_fn (fn
))
17647 fn
= get_first_fn (fn
);
17650 /* Can't say anything more. */;
17651 else if (DECL_CLASS_SCOPE_P (fn
))
17653 location_t loc
= EXPR_LOC_OR_LOC (t
,
17656 "declarations in dependent base %qT are "
17657 "not found by unqualified lookup",
17658 DECL_CLASS_CONTEXT (fn
));
17659 if (current_class_ptr
)
17661 "use %<this->%D%> instead", function
);
17664 "use %<%T::%D%> instead",
17665 current_class_name
, function
);
17668 inform (DECL_SOURCE_LOCATION (fn
),
17669 "%qD declared here, later in the "
17670 "translation unit", fn
);
17673 release_tree_vector (call_args
);
17674 RETURN (error_mark_node
);
17681 if (identifier_p (function
))
17683 if (complain
& tf_error
)
17684 unqualified_name_lookup_error (function
);
17685 release_tree_vector (call_args
);
17686 RETURN (error_mark_node
);
17690 /* Remember that there was a reference to this entity. */
17691 if (function
!= NULL_TREE
17692 && DECL_P (function
)
17693 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17695 release_tree_vector (call_args
);
17696 RETURN (error_mark_node
);
17699 /* Put back tf_decltype for the actual call. */
17700 complain
|= decltype_flag
;
17702 if (function
== NULL_TREE
)
17703 switch (CALL_EXPR_IFN (t
))
17706 gcc_assert (nargs
== 1);
17707 if (vec_safe_length (call_args
) != 1)
17709 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17710 "wrong number of arguments to "
17711 "%<__builtin_launder%>");
17712 ret
= error_mark_node
;
17715 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17717 (*call_args
)[0], complain
);
17721 /* Unsupported internal function with arguments. */
17722 gcc_unreachable ();
17724 else if (TREE_CODE (function
) == OFFSET_REF
)
17725 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17727 else if (TREE_CODE (function
) == COMPONENT_REF
)
17729 tree instance
= TREE_OPERAND (function
, 0);
17730 tree fn
= TREE_OPERAND (function
, 1);
17732 if (processing_template_decl
17733 && (type_dependent_expression_p (instance
)
17734 || (!BASELINK_P (fn
)
17735 && TREE_CODE (fn
) != FIELD_DECL
)
17736 || type_dependent_expression_p (fn
)
17737 || any_type_dependent_arguments_p (call_args
)))
17738 ret
= build_min_nt_call_vec (function
, call_args
);
17739 else if (!BASELINK_P (fn
))
17740 ret
= finish_call_expr (function
, &call_args
,
17741 /*disallow_virtual=*/false,
17742 /*koenig_p=*/false,
17745 ret
= (build_new_method_call
17747 &call_args
, NULL_TREE
,
17748 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17753 ret
= finish_call_expr (function
, &call_args
,
17754 /*disallow_virtual=*/qualified_p
,
17758 release_tree_vector (call_args
);
17760 if (ret
!= error_mark_node
)
17762 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17763 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17764 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17765 bool thk
= CALL_FROM_THUNK_P (t
);
17766 if (op
|| ord
|| rev
|| thk
)
17768 function
= extract_call_expr (ret
);
17769 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17770 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17771 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17774 CALL_FROM_THUNK_P (function
) = true;
17775 /* The thunk location is not interesting. */
17776 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17786 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17787 tree folded_cond
= fold_non_dependent_expr (cond
);
17790 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17792 if (integer_zerop (folded_cond
))
17794 ++c_inhibit_evaluation_warnings
;
17795 exp1
= RECUR (TREE_OPERAND (t
, 1));
17796 --c_inhibit_evaluation_warnings
;
17797 exp2
= RECUR (TREE_OPERAND (t
, 2));
17801 exp1
= RECUR (TREE_OPERAND (t
, 1));
17802 ++c_inhibit_evaluation_warnings
;
17803 exp2
= RECUR (TREE_OPERAND (t
, 2));
17804 --c_inhibit_evaluation_warnings
;
17806 cond
= folded_cond
;
17810 exp1
= RECUR (TREE_OPERAND (t
, 1));
17811 exp2
= RECUR (TREE_OPERAND (t
, 2));
17814 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17815 cond
, exp1
, exp2
, complain
));
17818 case PSEUDO_DTOR_EXPR
:
17820 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17821 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17822 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17823 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17829 tree purpose
, value
, chain
;
17831 if (t
== void_list_node
)
17834 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17835 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17837 /* We have pack expansions, so expand those and
17838 create a new list out of it. */
17839 tree purposevec
= NULL_TREE
;
17840 tree valuevec
= NULL_TREE
;
17844 /* Expand the argument expressions. */
17845 if (TREE_PURPOSE (t
))
17846 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17847 complain
, in_decl
);
17848 if (TREE_VALUE (t
))
17849 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17850 complain
, in_decl
);
17852 /* Build the rest of the list. */
17853 chain
= TREE_CHAIN (t
);
17854 if (chain
&& chain
!= void_type_node
)
17855 chain
= RECUR (chain
);
17857 /* Determine the number of arguments. */
17858 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17860 len
= TREE_VEC_LENGTH (purposevec
);
17861 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17863 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17864 len
= TREE_VEC_LENGTH (valuevec
);
17867 /* Since we only performed a partial substitution into
17868 the argument pack, we only RETURN (a single list
17870 if (purposevec
== TREE_PURPOSE (t
)
17871 && valuevec
== TREE_VALUE (t
)
17872 && chain
== TREE_CHAIN (t
))
17875 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17878 /* Convert the argument vectors into a TREE_LIST */
17882 /* Grab the Ith values. */
17884 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17887 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17890 /* Build the list (backwards). */
17891 chain
= tree_cons (purpose
, value
, chain
);
17897 purpose
= TREE_PURPOSE (t
);
17899 purpose
= RECUR (purpose
);
17900 value
= TREE_VALUE (t
);
17902 value
= RECUR (value
);
17903 chain
= TREE_CHAIN (t
);
17904 if (chain
&& chain
!= void_type_node
)
17905 chain
= RECUR (chain
);
17906 if (purpose
== TREE_PURPOSE (t
)
17907 && value
== TREE_VALUE (t
)
17908 && chain
== TREE_CHAIN (t
))
17910 RETURN (tree_cons (purpose
, value
, chain
));
17913 case COMPONENT_REF
:
17920 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17921 args
, complain
, in_decl
);
17922 /* Remember that there was a reference to this entity. */
17923 if (DECL_P (object
)
17924 && !mark_used (object
, complain
) && !(complain
& tf_error
))
17925 RETURN (error_mark_node
);
17926 object_type
= TREE_TYPE (object
);
17928 member
= TREE_OPERAND (t
, 1);
17929 if (BASELINK_P (member
))
17930 member
= tsubst_baselink (member
,
17931 non_reference (TREE_TYPE (object
)),
17932 args
, complain
, in_decl
);
17934 member
= tsubst_copy (member
, args
, complain
, in_decl
);
17935 if (member
== error_mark_node
)
17936 RETURN (error_mark_node
);
17938 if (TREE_CODE (member
) == FIELD_DECL
)
17940 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
17941 if (TREE_CODE (r
) == COMPONENT_REF
)
17942 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17945 else if (type_dependent_expression_p (object
))
17946 /* We can't do much here. */;
17947 else if (!CLASS_TYPE_P (object_type
))
17949 if (scalarish_type_p (object_type
))
17951 tree s
= NULL_TREE
;
17952 tree dtor
= member
;
17954 if (TREE_CODE (dtor
) == SCOPE_REF
)
17956 s
= TREE_OPERAND (dtor
, 0);
17957 dtor
= TREE_OPERAND (dtor
, 1);
17959 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
17961 dtor
= TREE_OPERAND (dtor
, 0);
17963 RETURN (finish_pseudo_destructor_expr
17964 (object
, s
, dtor
, input_location
));
17968 else if (TREE_CODE (member
) == SCOPE_REF
17969 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
17971 /* Lookup the template functions now that we know what the
17973 tree scope
= TREE_OPERAND (member
, 0);
17974 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
17975 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
17976 member
= lookup_qualified_name (scope
, tmpl
,
17977 /*is_type_p=*/false,
17978 /*complain=*/false);
17979 if (BASELINK_P (member
))
17981 BASELINK_FUNCTIONS (member
)
17982 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
17984 member
= (adjust_result_of_qualified_name_lookup
17985 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
17990 qualified_name_lookup_error (scope
, tmpl
, member
,
17992 RETURN (error_mark_node
);
17995 else if (TREE_CODE (member
) == SCOPE_REF
17996 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
17997 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
17999 if (complain
& tf_error
)
18001 if (TYPE_P (TREE_OPERAND (member
, 0)))
18002 error ("%qT is not a class or namespace",
18003 TREE_OPERAND (member
, 0));
18005 error ("%qD is not a class or namespace",
18006 TREE_OPERAND (member
, 0));
18008 RETURN (error_mark_node
);
18011 r
= finish_class_member_access_expr (object
, member
,
18012 /*template_p=*/false,
18014 if (TREE_CODE (r
) == COMPONENT_REF
)
18015 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18020 RETURN (build_throw
18021 (RECUR (TREE_OPERAND (t
, 0))));
18025 vec
<constructor_elt
, va_gc
> *n
;
18026 constructor_elt
*ce
;
18027 unsigned HOST_WIDE_INT idx
;
18028 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18029 bool process_index_p
;
18031 bool need_copy_p
= false;
18034 if (type
== error_mark_node
)
18035 RETURN (error_mark_node
);
18037 /* digest_init will do the wrong thing if we let it. */
18038 if (type
&& TYPE_PTRMEMFUNC_P (type
))
18041 /* We do not want to process the index of aggregate
18042 initializers as they are identifier nodes which will be
18043 looked up by digest_init. */
18044 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18046 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18047 newlen
= vec_safe_length (n
);
18048 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18050 if (ce
->index
&& process_index_p
18051 /* An identifier index is looked up in the type
18052 being initialized, not the current scope. */
18053 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18054 ce
->index
= RECUR (ce
->index
);
18056 if (PACK_EXPANSION_P (ce
->value
))
18058 /* Substitute into the pack expansion. */
18059 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18062 if (ce
->value
== error_mark_node
18063 || PACK_EXPANSION_P (ce
->value
))
18065 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18066 /* Just move the argument into place. */
18067 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18070 /* Update the length of the final CONSTRUCTOR
18071 arguments vector, and note that we will need to
18073 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18074 need_copy_p
= true;
18078 ce
->value
= RECUR (ce
->value
);
18083 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18085 vec_alloc (n
, newlen
);
18086 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18088 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18090 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18091 for (i
= 0; i
< len
; ++i
)
18092 CONSTRUCTOR_APPEND_ELT (n
, 0,
18093 TREE_VEC_ELT (ce
->value
, i
));
18096 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18100 r
= build_constructor (init_list_type_node
, n
);
18101 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18103 if (TREE_HAS_CONSTRUCTOR (t
))
18105 fcl_t cl
= fcl_functional
;
18106 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18108 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18111 TREE_TYPE (r
) = type
;
18117 tree operand_0
= TREE_OPERAND (t
, 0);
18118 if (TYPE_P (operand_0
))
18120 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18121 RETURN (get_typeid (operand_0
, complain
));
18125 operand_0
= RECUR (operand_0
);
18126 RETURN (build_typeid (operand_0
, complain
));
18133 else if (DECL_PACK_P (t
))
18135 /* We don't build decls for an instantiation of a
18136 variadic capture proxy, we instantiate the elements
18138 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
18139 return RECUR (DECL_VALUE_EXPR (t
));
18145 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18146 /* ??? We're doing a subset of finish_id_expression here. */
18148 && !processing_template_decl
18149 && !cp_unevaluated_operand
18150 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18151 && CP_DECL_THREAD_LOCAL_P (r
))
18153 if (tree wrap
= get_tls_wrapper_fn (r
))
18154 /* Replace an evaluated use of the thread_local variable with
18155 a call to its wrapper. */
18156 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18158 else if (outer_automatic_var_p (r
))
18159 r
= process_outer_var_ref (r
, complain
);
18161 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18162 /* If the original type was a reference, we'll be wrapped in
18163 the appropriate INDIRECT_REF. */
18164 r
= convert_from_reference (r
);
18170 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18171 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18172 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18175 case OFFSETOF_EXPR
:
18178 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18179 in_decl
, /*function_p=*/false,
18180 /*integral_constant_expression_p=*/false);
18181 RETURN (finish_offsetof (object_ptr
,
18182 RECUR (TREE_OPERAND (t
, 0)),
18183 EXPR_LOCATION (t
)));
18186 case ADDRESSOF_EXPR
:
18187 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18188 RECUR (TREE_OPERAND (t
, 0)), complain
));
18192 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18193 complain
, in_decl
);
18195 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18196 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18197 type2
= RECUR (type2
);
18199 type2
= tsubst (type2
, args
, complain
, in_decl
);
18201 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18206 tree old_stmt_expr
= cur_stmt_expr
;
18207 tree stmt_expr
= begin_stmt_expr ();
18209 cur_stmt_expr
= stmt_expr
;
18210 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18211 integral_constant_expression_p
);
18212 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18213 cur_stmt_expr
= old_stmt_expr
;
18215 /* If the resulting list of expression statement is empty,
18216 fold it further into void_node. */
18217 if (empty_expr_stmt_p (stmt_expr
))
18218 stmt_expr
= void_node
;
18220 RETURN (stmt_expr
);
18225 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18227 RETURN (build_lambda_object (r
));
18231 /* We can get here for a constant initializer of non-dependent type.
18232 FIXME stop folding in cp_parser_initializer_clause. */
18234 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18239 case TRANSACTION_EXPR
:
18240 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18241 integral_constant_expression_p
));
18244 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18246 case VEC_PERM_EXPR
:
18248 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18249 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18250 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18251 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18255 case REQUIRES_EXPR
:
18256 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18259 /* Handle Objective-C++ constructs, if appropriate. */
18262 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18263 in_decl
, /*function_p=*/false);
18267 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18273 input_location
= loc
;
18277 /* Verify that the instantiated ARGS are valid. For type arguments,
18278 make sure that the type's linkage is ok. For non-type arguments,
18279 make sure they are constants if they are integral or enumerations.
18280 Emit an error under control of COMPLAIN, and return TRUE on error. */
18283 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18285 if (dependent_template_arg_p (t
))
18287 if (ARGUMENT_PACK_P (t
))
18289 tree vec
= ARGUMENT_PACK_ARGS (t
);
18290 int len
= TREE_VEC_LENGTH (vec
);
18291 bool result
= false;
18294 for (i
= 0; i
< len
; ++i
)
18295 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18299 else if (TYPE_P (t
))
18301 /* [basic.link]: A name with no linkage (notably, the name
18302 of a class or enumeration declared in a local scope)
18303 shall not be used to declare an entity with linkage.
18304 This implies that names with no linkage cannot be used as
18307 DR 757 relaxes this restriction for C++0x. */
18308 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18309 : no_linkage_check (t
, /*relaxed_p=*/false));
18313 /* DR 488 makes use of a type with no linkage cause
18314 type deduction to fail. */
18315 if (complain
& tf_error
)
18317 if (TYPE_UNNAMED_P (nt
))
18318 error ("%qT is/uses unnamed type", t
);
18320 error ("template argument for %qD uses local type %qT",
18325 /* In order to avoid all sorts of complications, we do not
18326 allow variably-modified types as template arguments. */
18327 else if (variably_modified_type_p (t
, NULL_TREE
))
18329 if (complain
& tf_error
)
18330 error ("%qT is a variably modified type", t
);
18334 /* Class template and alias template arguments should be OK. */
18335 else if (DECL_TYPE_TEMPLATE_P (t
))
18337 /* A non-type argument of integral or enumerated type must be a
18339 else if (TREE_TYPE (t
)
18340 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18341 && !REFERENCE_REF_P (t
)
18342 && !TREE_CONSTANT (t
))
18344 if (complain
& tf_error
)
18345 error ("integral expression %qE is not constant", t
);
18352 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18354 int ix
, len
= DECL_NTPARMS (tmpl
);
18355 bool result
= false;
18357 for (ix
= 0; ix
!= len
; ix
++)
18359 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18362 if (result
&& (complain
& tf_error
))
18363 error (" trying to instantiate %qD", tmpl
);
18367 /* We're out of SFINAE context now, so generate diagnostics for the access
18368 errors we saw earlier when instantiating D from TMPL and ARGS. */
18371 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18373 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18374 tree type
= TREE_TYPE (pattern
);
18375 location_t loc
= input_location
;
18377 push_access_scope (d
);
18378 push_deferring_access_checks (dk_no_deferred
);
18379 input_location
= DECL_SOURCE_LOCATION (pattern
);
18380 tsubst (type
, args
, tf_warning_or_error
, d
);
18381 input_location
= loc
;
18382 pop_deferring_access_checks ();
18383 pop_access_scope (d
);
18386 /* Instantiate the indicated variable, function, or alias template TMPL with
18387 the template arguments in TARG_PTR. */
18390 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18392 tree targ_ptr
= orig_args
;
18396 bool access_ok
= true;
18398 if (tmpl
== error_mark_node
)
18399 return error_mark_node
;
18401 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18403 /* If this function is a clone, handle it specially. */
18404 if (DECL_CLONED_FUNCTION_P (tmpl
))
18409 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18410 DECL_CLONED_FUNCTION. */
18411 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18412 targ_ptr
, complain
);
18413 if (spec
== error_mark_node
)
18414 return error_mark_node
;
18416 /* Look for the clone. */
18417 FOR_EACH_CLONE (clone
, spec
)
18418 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18420 /* We should always have found the clone by now. */
18421 gcc_unreachable ();
18425 if (targ_ptr
== error_mark_node
)
18426 return error_mark_node
;
18428 /* Check to see if we already have this specialization. */
18429 gen_tmpl
= most_general_template (tmpl
);
18430 if (TMPL_ARGS_DEPTH (targ_ptr
)
18431 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18432 /* targ_ptr only has the innermost template args, so add the outer ones
18433 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18434 the case of a non-dependent call within a template definition). */
18435 targ_ptr
= (add_outermost_template_args
18436 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18439 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18440 but it doesn't seem to be on the hot path. */
18441 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18443 gcc_assert (tmpl
== gen_tmpl
18444 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18446 || fndecl
== NULL_TREE
);
18448 if (spec
!= NULL_TREE
)
18450 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18452 if (complain
& tf_error
)
18453 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18454 return error_mark_node
;
18459 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18461 return error_mark_node
;
18463 /* We are building a FUNCTION_DECL, during which the access of its
18464 parameters and return types have to be checked. However this
18465 FUNCTION_DECL which is the desired context for access checking
18466 is not built yet. We solve this chicken-and-egg problem by
18467 deferring all checks until we have the FUNCTION_DECL. */
18468 push_deferring_access_checks (dk_deferred
);
18470 /* Instantiation of the function happens in the context of the function
18471 template, not the context of the overload resolution we're doing. */
18472 push_to_top_level ();
18473 /* If there are dependent arguments, e.g. because we're doing partial
18474 ordering, make sure processing_template_decl stays set. */
18475 if (uses_template_parms (targ_ptr
))
18476 ++processing_template_decl
;
18477 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18479 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18480 complain
, gen_tmpl
, true);
18481 push_nested_class (ctx
);
18484 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18486 fndecl
= NULL_TREE
;
18487 if (VAR_P (pattern
))
18489 /* We need to determine if we're using a partial or explicit
18490 specialization now, because the type of the variable could be
18492 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18493 tree elt
= most_specialized_partial_spec (tid
, complain
);
18494 if (elt
== error_mark_node
)
18495 pattern
= error_mark_node
;
18498 tree partial_tmpl
= TREE_VALUE (elt
);
18499 tree partial_args
= TREE_PURPOSE (elt
);
18500 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18501 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18505 /* Substitute template parameters to obtain the specialization. */
18506 if (fndecl
== NULL_TREE
)
18507 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18508 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18509 pop_nested_class ();
18510 pop_from_top_level ();
18512 if (fndecl
== error_mark_node
)
18514 pop_deferring_access_checks ();
18515 return error_mark_node
;
18518 /* The DECL_TI_TEMPLATE should always be the immediate parent
18519 template, not the most general template. */
18520 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18521 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18523 /* Now we know the specialization, compute access previously
18524 deferred. Do no access control for inheriting constructors,
18525 as we already checked access for the inherited constructor. */
18526 if (!(flag_new_inheriting_ctors
18527 && DECL_INHERITED_CTOR (fndecl
)))
18529 push_access_scope (fndecl
);
18530 if (!perform_deferred_access_checks (complain
))
18532 pop_access_scope (fndecl
);
18534 pop_deferring_access_checks ();
18536 /* If we've just instantiated the main entry point for a function,
18537 instantiate all the alternate entry points as well. We do this
18538 by cloning the instantiation of the main entry point, not by
18539 instantiating the template clones. */
18540 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18541 clone_function_decl (fndecl
, /*update_methods=*/false);
18545 if (!(complain
& tf_error
))
18547 /* Remember to reinstantiate when we're out of SFINAE so the user
18548 can see the errors. */
18549 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18551 return error_mark_node
;
18556 /* Wrapper for instantiate_template_1. */
18559 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18562 timevar_push (TV_TEMPLATE_INST
);
18563 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18564 timevar_pop (TV_TEMPLATE_INST
);
18568 /* Instantiate the alias template TMPL with ARGS. Also push a template
18569 instantiation level, which instantiate_template doesn't do because
18570 functions and variables have sufficient context established by the
18574 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18576 struct pending_template
*old_last_pend
= last_pending_template
;
18577 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18578 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18579 return error_mark_node
;
18580 tree tinst
= build_tree_list (tmpl
, args
);
18581 if (!push_tinst_level (tinst
))
18584 return error_mark_node
;
18588 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18589 args
, tmpl
, complain
,
18590 /*require_all_args=*/true,
18591 /*use_default_args=*/true);
18593 tree r
= instantiate_template (tmpl
, args
, complain
);
18594 pop_tinst_level ();
18595 /* We can't free this if a pending_template entry or last_error_tinst_level
18596 is pointing at it. */
18597 if (last_pending_template
== old_last_pend
18598 && last_error_tinst_level
== old_error_tinst
)
18604 /* PARM is a template parameter pack for FN. Returns true iff
18605 PARM is used in a deducible way in the argument list of FN. */
18608 pack_deducible_p (tree parm
, tree fn
)
18610 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18611 for (; t
; t
= TREE_CHAIN (t
))
18613 tree type
= TREE_VALUE (t
);
18615 if (!PACK_EXPANSION_P (type
))
18617 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18618 packs
; packs
= TREE_CHAIN (packs
))
18619 if (template_args_equal (TREE_VALUE (packs
), parm
))
18621 /* The template parameter pack is used in a function parameter
18622 pack. If this is the end of the parameter list, the
18623 template parameter pack is deducible. */
18624 if (TREE_CHAIN (t
) == void_list_node
)
18627 /* Otherwise, not. Well, it could be deduced from
18628 a non-pack parameter, but doing so would end up with
18629 a deduction mismatch, so don't bother. */
18633 /* The template parameter pack isn't used in any function parameter
18634 packs, but it might be used deeper, e.g. tuple<Args...>. */
18638 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18639 NARGS elements of the arguments that are being used when calling
18640 it. TARGS is a vector into which the deduced template arguments
18643 Returns either a FUNCTION_DECL for the matching specialization of FN or
18644 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18645 true, diagnostics will be printed to explain why it failed.
18647 If FN is a conversion operator, or we are trying to produce a specific
18648 specialization, RETURN_TYPE is the return type desired.
18650 The EXPLICIT_TARGS are explicit template arguments provided via a
18653 The parameter STRICT is one of:
18656 We are deducing arguments for a function call, as in
18657 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18658 deducing arguments for a call to the result of a conversion
18659 function template, as in [over.call.object].
18662 We are deducing arguments for a conversion function, as in
18663 [temp.deduct.conv].
18666 We are deducing arguments when doing an explicit instantiation
18667 as in [temp.explicit], when determining an explicit specialization
18668 as in [temp.expl.spec], or when taking the address of a function
18669 template, as in [temp.deduct.funcaddr]. */
18672 fn_type_unification (tree fn
,
18673 tree explicit_targs
,
18676 unsigned int nargs
,
18678 unification_kind_t strict
,
18685 tree decl
= NULL_TREE
;
18686 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18688 static int deduction_depth
;
18689 struct pending_template
*old_last_pend
= last_pending_template
;
18690 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18693 if (flag_new_inheriting_ctors
)
18694 fn
= strip_inheriting_ctors (fn
);
18696 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18698 tree r
= error_mark_node
;
18700 tree full_targs
= targs
;
18701 if (TMPL_ARGS_DEPTH (targs
)
18702 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18703 full_targs
= (add_outermost_template_args
18704 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18708 complain
|= tf_decltype
;
18710 /* In C++0x, it's possible to have a function template whose type depends
18711 on itself recursively. This is most obvious with decltype, but can also
18712 occur with enumeration scope (c++/48969). So we need to catch infinite
18713 recursion and reject the substitution at deduction time; this function
18714 will return error_mark_node for any repeated substitution.
18716 This also catches excessive recursion such as when f<N> depends on
18717 f<N-1> across all integers, and returns error_mark_node for all the
18718 substitutions back up to the initial one.
18720 This is, of course, not reentrant. */
18721 if (excessive_deduction_depth
)
18722 return error_mark_node
;
18723 tinst
= build_tree_list (fn
, NULL_TREE
);
18726 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18728 fntype
= TREE_TYPE (fn
);
18729 if (explicit_targs
)
18733 The specified template arguments must match the template
18734 parameters in kind (i.e., type, nontype, template), and there
18735 must not be more arguments than there are parameters;
18736 otherwise type deduction fails.
18738 Nontype arguments must match the types of the corresponding
18739 nontype template parameters, or must be convertible to the
18740 types of the corresponding nontype parameters as specified in
18741 _temp.arg.nontype_, otherwise type deduction fails.
18743 All references in the function type of the function template
18744 to the corresponding template parameters are replaced by the
18745 specified template argument values. If a substitution in a
18746 template parameter or in the function type of the function
18747 template results in an invalid type, type deduction fails. */
18748 int i
, len
= TREE_VEC_LENGTH (tparms
);
18749 location_t loc
= input_location
;
18750 bool incomplete
= false;
18752 if (explicit_targs
== error_mark_node
)
18755 if (TMPL_ARGS_DEPTH (explicit_targs
)
18756 < TMPL_ARGS_DEPTH (full_targs
))
18757 explicit_targs
= add_outermost_template_args (full_targs
,
18760 /* Adjust any explicit template arguments before entering the
18761 substitution context. */
18763 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18765 /*require_all_args=*/false,
18766 /*use_default_args=*/false));
18767 if (explicit_targs
== error_mark_node
)
18770 /* Substitute the explicit args into the function type. This is
18771 necessary so that, for instance, explicitly declared function
18772 arguments can match null pointed constants. If we were given
18773 an incomplete set of explicit args, we must not do semantic
18774 processing during substitution as we could create partial
18776 for (i
= 0; i
< len
; i
++)
18778 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18779 bool parameter_pack
= false;
18780 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18782 /* Dig out the actual parm. */
18783 if (TREE_CODE (parm
) == TYPE_DECL
18784 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18786 parm
= TREE_TYPE (parm
);
18787 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18789 else if (TREE_CODE (parm
) == PARM_DECL
)
18791 parm
= DECL_INITIAL (parm
);
18792 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18795 if (!parameter_pack
&& targ
== NULL_TREE
)
18796 /* No explicit argument for this template parameter. */
18799 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18801 /* Mark the argument pack as "incomplete". We could
18802 still deduce more arguments during unification.
18803 We remove this mark in type_unification_real. */
18806 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18807 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18808 = ARGUMENT_PACK_ARGS (targ
);
18811 /* We have some incomplete argument packs. */
18816 TREE_VALUE (tinst
) = explicit_targs
;
18817 if (!push_tinst_level (tinst
))
18819 excessive_deduction_depth
= true;
18822 processing_template_decl
+= incomplete
;
18823 input_location
= DECL_SOURCE_LOCATION (fn
);
18824 /* Ignore any access checks; we'll see them again in
18825 instantiate_template and they might have the wrong
18826 access path at this point. */
18827 push_deferring_access_checks (dk_deferred
);
18828 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18829 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18830 pop_deferring_access_checks ();
18831 input_location
= loc
;
18832 processing_template_decl
-= incomplete
;
18833 pop_tinst_level ();
18835 if (fntype
== error_mark_node
)
18838 /* Place the explicitly specified arguments in TARGS. */
18839 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18840 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18841 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18844 /* Never do unification on the 'this' parameter. */
18845 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18847 if (return_type
&& strict
== DEDUCE_CALL
)
18849 /* We're deducing for a call to the result of a template conversion
18850 function. The parms we really want are in return_type. */
18851 if (POINTER_TYPE_P (return_type
))
18852 return_type
= TREE_TYPE (return_type
);
18853 parms
= TYPE_ARG_TYPES (return_type
);
18855 else if (return_type
)
18859 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18860 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18861 new_args
[0] = return_type
;
18862 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18867 /* We allow incomplete unification without an error message here
18868 because the standard doesn't seem to explicitly prohibit it. Our
18869 callers must be ready to deal with unification failures in any
18872 TREE_VALUE (tinst
) = targs
;
18873 /* If we aren't explaining yet, push tinst context so we can see where
18874 any errors (e.g. from class instantiations triggered by instantiation
18875 of default template arguments) come from. If we are explaining, this
18876 context is redundant. */
18877 if (!explain_p
&& !push_tinst_level (tinst
))
18879 excessive_deduction_depth
= true;
18883 /* type_unification_real will pass back any access checks from default
18884 template argument substitution. */
18885 vec
<deferred_access_check
, va_gc
> *checks
;
18888 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18889 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18890 strict
, flags
, &checks
, explain_p
);
18892 pop_tinst_level ();
18896 /* Now that we have bindings for all of the template arguments,
18897 ensure that the arguments deduced for the template template
18898 parameters have compatible template parameter lists. We cannot
18899 check this property before we have deduced all template
18900 arguments, because the template parameter types of a template
18901 template parameter might depend on prior template parameters
18902 deduced after the template template parameter. The following
18903 ill-formed example illustrates this issue:
18905 template<typename T, template<T> class C> void f(C<5>, T);
18907 template<int N> struct X {};
18910 f(X<5>(), 5l); // error: template argument deduction fails
18913 The template parameter list of 'C' depends on the template type
18914 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18915 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18916 time that we deduce 'C'. */
18917 if (!template_template_parm_bindings_ok_p
18918 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
18920 unify_inconsistent_template_template_parameters (explain_p
);
18924 /* All is well so far. Now, check:
18928 When all template arguments have been deduced, all uses of
18929 template parameters in nondeduced contexts are replaced with
18930 the corresponding deduced argument values. If the
18931 substitution results in an invalid type, as described above,
18932 type deduction fails. */
18933 TREE_VALUE (tinst
) = targs
;
18934 if (!push_tinst_level (tinst
))
18936 excessive_deduction_depth
= true;
18940 /* Also collect access checks from the instantiation. */
18941 reopen_deferring_access_checks (checks
);
18943 decl
= instantiate_template (fn
, targs
, complain
);
18945 checks
= get_deferred_access_checks ();
18946 pop_deferring_access_checks ();
18948 pop_tinst_level ();
18950 if (decl
== error_mark_node
)
18953 /* Now perform any access checks encountered during substitution. */
18954 push_access_scope (decl
);
18955 ok
= perform_access_checks (checks
, complain
);
18956 pop_access_scope (decl
);
18960 /* If we're looking for an exact match, check that what we got
18961 is indeed an exact match. It might not be if some template
18962 parameters are used in non-deduced contexts. But don't check
18963 for an exact match if we have dependent template arguments;
18964 in that case we're doing partial ordering, and we already know
18965 that we have two candidates that will provide the actual type. */
18966 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
18968 tree substed
= TREE_TYPE (decl
);
18972 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
18974 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
18975 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
18976 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
18978 unify_type_mismatch (explain_p
, args
[i
],
18979 TREE_VALUE (sarg
));
18984 /* After doing deduction with the inherited constructor, actually return an
18985 instantiation of the inheriting constructor. */
18987 decl
= instantiate_template (orig_fn
, targs
, complain
);
18993 if (excessive_deduction_depth
)
18995 if (deduction_depth
== 0)
18996 /* Reset once we're all the way out. */
18997 excessive_deduction_depth
= false;
19000 /* We can't free this if a pending_template entry or last_error_tinst_level
19001 is pointing at it. */
19002 if (last_pending_template
== old_last_pend
19003 && last_error_tinst_level
== old_error_tinst
)
19009 /* Adjust types before performing type deduction, as described in
19010 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19011 sections are symmetric. PARM is the type of a function parameter
19012 or the return type of the conversion function. ARG is the type of
19013 the argument passed to the call, or the type of the value
19014 initialized with the result of the conversion function.
19015 ARG_EXPR is the original argument expression, which may be null. */
19018 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19031 /* Swap PARM and ARG throughout the remainder of this
19032 function; the handling is precisely symmetric since PARM
19033 will initialize ARG rather than vice versa. */
19034 std::swap (parm
, arg
);
19038 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19039 too, but here handle it by stripping the reference from PARM
19040 rather than by adding it to ARG. */
19041 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19042 && TYPE_REF_IS_RVALUE (*parm
)
19043 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19044 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19045 && TREE_CODE (*arg
) == REFERENCE_TYPE
19046 && !TYPE_REF_IS_RVALUE (*arg
))
19047 *parm
= TREE_TYPE (*parm
);
19048 /* Nothing else to do in this case. */
19052 gcc_unreachable ();
19055 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19057 /* [temp.deduct.call]
19059 If P is not a reference type:
19061 --If A is an array type, the pointer type produced by the
19062 array-to-pointer standard conversion (_conv.array_) is
19063 used in place of A for type deduction; otherwise,
19065 --If A is a function type, the pointer type produced by
19066 the function-to-pointer standard conversion
19067 (_conv.func_) is used in place of A for type deduction;
19070 --If A is a cv-qualified type, the top level
19071 cv-qualifiers of A's type are ignored for type
19073 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19074 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19075 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19076 *arg
= build_pointer_type (*arg
);
19078 *arg
= TYPE_MAIN_VARIANT (*arg
);
19081 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19082 reference to a cv-unqualified template parameter that does not represent a
19083 template parameter of a class template (during class template argument
19084 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19085 an lvalue, the type "lvalue reference to A" is used in place of A for type
19087 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19088 && TYPE_REF_IS_RVALUE (*parm
)
19089 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19090 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19091 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19092 && (arg_expr
? lvalue_p (arg_expr
)
19093 /* try_one_overload doesn't provide an arg_expr, but
19094 functions are always lvalues. */
19095 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19096 *arg
= build_reference_type (*arg
);
19098 /* [temp.deduct.call]
19100 If P is a cv-qualified type, the top level cv-qualifiers
19101 of P's type are ignored for type deduction. If P is a
19102 reference type, the type referred to by P is used for
19104 *parm
= TYPE_MAIN_VARIANT (*parm
);
19105 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19107 *parm
= TREE_TYPE (*parm
);
19108 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19111 /* DR 322. For conversion deduction, remove a reference type on parm
19112 too (which has been swapped into ARG). */
19113 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19114 *arg
= TREE_TYPE (*arg
);
19119 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19120 template which does contain any deducible template parameters; check if
19121 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19122 unify_one_argument. */
19125 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19126 int flags
, bool explain_p
)
19131 type
= TREE_TYPE (arg
);
19135 if (same_type_p (parm
, type
))
19136 return unify_success (explain_p
);
19138 if (strict
== DEDUCE_CONV
)
19140 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19141 explain_p
? tf_warning_or_error
: tf_none
))
19142 return unify_success (explain_p
);
19144 else if (strict
!= DEDUCE_EXACT
)
19146 if (can_convert_arg (parm
, type
,
19147 TYPE_P (arg
) ? NULL_TREE
: arg
,
19148 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19149 return unify_success (explain_p
);
19152 if (strict
== DEDUCE_EXACT
)
19153 return unify_type_mismatch (explain_p
, parm
, arg
);
19155 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19158 static bool uses_deducible_template_parms (tree type
);
19160 /* Returns true iff the expression EXPR is one from which a template
19161 argument can be deduced. In other words, if it's an undecorated
19162 use of a template non-type parameter. */
19165 deducible_expression (tree expr
)
19167 /* Strip implicit conversions. */
19168 while (CONVERT_EXPR_P (expr
))
19169 expr
= TREE_OPERAND (expr
, 0);
19170 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19173 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19174 deducible way; that is, if it has a max value of <PARM> - 1. */
19177 deducible_array_bound (tree domain
)
19179 if (domain
== NULL_TREE
)
19182 tree max
= TYPE_MAX_VALUE (domain
);
19183 if (TREE_CODE (max
) != MINUS_EXPR
)
19186 return deducible_expression (TREE_OPERAND (max
, 0));
19189 /* Returns true iff the template arguments ARGS use a template parameter
19190 in a deducible way. */
19193 deducible_template_args (tree args
)
19195 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19198 tree elt
= TREE_VEC_ELT (args
, i
);
19199 if (ARGUMENT_PACK_P (elt
))
19200 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19203 if (PACK_EXPANSION_P (elt
))
19204 elt
= PACK_EXPANSION_PATTERN (elt
);
19205 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19207 else if (TYPE_P (elt
))
19208 deducible
= uses_deducible_template_parms (elt
);
19210 deducible
= deducible_expression (elt
);
19218 /* Returns true iff TYPE contains any deducible references to template
19219 parameters, as per 14.8.2.5. */
19222 uses_deducible_template_parms (tree type
)
19224 if (PACK_EXPANSION_P (type
))
19225 type
= PACK_EXPANSION_PATTERN (type
);
19232 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19233 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19239 if (POINTER_TYPE_P (type
))
19240 return uses_deducible_template_parms (TREE_TYPE (type
));
19242 /* T[integer-constant ]
19244 if (TREE_CODE (type
) == ARRAY_TYPE
)
19245 return (uses_deducible_template_parms (TREE_TYPE (type
))
19246 || deducible_array_bound (TYPE_DOMAIN (type
)));
19258 if (TYPE_PTRMEM_P (type
))
19259 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19260 || (uses_deducible_template_parms
19261 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19263 /* template-name <T> (where template-name refers to a class template)
19264 template-name <i> (where template-name refers to a class template) */
19265 if (CLASS_TYPE_P (type
)
19266 && CLASSTYPE_TEMPLATE_INFO (type
)
19267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19268 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19269 (CLASSTYPE_TI_ARGS (type
)));
19274 if (TREE_CODE (type
) == FUNCTION_TYPE
19275 || TREE_CODE (type
) == METHOD_TYPE
)
19277 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19279 tree parm
= TYPE_ARG_TYPES (type
);
19280 if (TREE_CODE (type
) == METHOD_TYPE
)
19281 parm
= TREE_CHAIN (parm
);
19282 for (; parm
; parm
= TREE_CHAIN (parm
))
19283 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19290 /* Subroutine of type_unification_real and unify_pack_expansion to
19291 handle unification of a single P/A pair. Parameters are as
19292 for those functions. */
19295 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19296 int subr
, unification_kind_t strict
,
19299 tree arg_expr
= NULL_TREE
;
19302 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19303 return unify_invalid (explain_p
);
19304 if (arg
== unknown_type_node
)
19305 /* We can't deduce anything from this, but we might get all the
19306 template args from other function args. */
19307 return unify_success (explain_p
);
19309 /* Implicit conversions (Clause 4) will be performed on a function
19310 argument to convert it to the type of the corresponding function
19311 parameter if the parameter type contains no template-parameters that
19312 participate in template argument deduction. */
19313 if (strict
!= DEDUCE_EXACT
19314 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19315 /* For function parameters with no deducible template parameters,
19316 just return. We'll check non-dependent conversions later. */
19317 return unify_success (explain_p
);
19322 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19323 | UNIFY_ALLOW_MORE_CV_QUAL
19324 | UNIFY_ALLOW_DERIVED
);
19328 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19332 arg_strict
= UNIFY_ALLOW_NONE
;
19336 gcc_unreachable ();
19339 /* We only do these transformations if this is the top-level
19340 parameter_type_list in a call or declaration matching; in other
19341 situations (nested function declarators, template argument lists) we
19342 won't be comparing a type to an expression, and we don't do any type
19348 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19349 if (type_unknown_p (arg
))
19351 /* [temp.deduct.type] A template-argument can be
19352 deduced from a pointer to function or pointer
19353 to member function argument if the set of
19354 overloaded functions does not contain function
19355 templates and at most one of a set of
19356 overloaded functions provides a unique
19358 resolve_overloaded_unification (tparms
, targs
, parm
,
19360 arg_strict
, explain_p
);
19361 /* If a unique match was not found, this is a
19362 non-deduced context, so we still succeed. */
19363 return unify_success (explain_p
);
19367 arg
= unlowered_expr_type (arg
);
19368 if (arg
== error_mark_node
)
19369 return unify_invalid (explain_p
);
19373 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19376 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19377 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19378 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19380 /* For deduction from an init-list we need the actual list. */
19381 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19383 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19386 /* for_each_template_parm callback that always returns 0. */
19389 zero_r (tree
, void *)
19394 /* for_each_template_parm any_fn callback to handle deduction of a template
19395 type argument from the type of an array bound. */
19398 array_deduction_r (tree t
, void *data
)
19400 tree_pair_p d
= (tree_pair_p
)data
;
19401 tree
&tparms
= d
->purpose
;
19402 tree
&targs
= d
->value
;
19404 if (TREE_CODE (t
) == ARRAY_TYPE
)
19405 if (tree dom
= TYPE_DOMAIN (t
))
19406 if (tree max
= TYPE_MAX_VALUE (dom
))
19408 if (TREE_CODE (max
) == MINUS_EXPR
)
19409 max
= TREE_OPERAND (max
, 0);
19410 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19411 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19412 UNIFY_ALLOW_NONE
, /*explain*/false);
19415 /* Keep walking. */
19419 /* Try to deduce any not-yet-deduced template type arguments from the type of
19420 an array bound. This is handled separately from unify because 14.8.2.5 says
19421 "The type of a type parameter is only deduced from an array bound if it is
19422 not otherwise deduced." */
19425 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19427 tree_pair_s data
= { tparms
, targs
};
19428 hash_set
<tree
> visited
;
19429 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19430 /*nondeduced*/false, array_deduction_r
);
19433 /* Most parms like fn_type_unification.
19435 If SUBR is 1, we're being called recursively (to unify the
19436 arguments of a function or method parameter of a function
19439 CHECKS is a pointer to a vector of access checks encountered while
19440 substituting default template arguments. */
19443 type_unification_real (tree tparms
,
19447 unsigned int xnargs
,
19449 unification_kind_t strict
,
19451 vec
<deferred_access_check
, va_gc
> **checks
,
19456 int ntparms
= TREE_VEC_LENGTH (tparms
);
19457 int saw_undeduced
= 0;
19460 unsigned int nargs
;
19463 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19464 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19465 gcc_assert (ntparms
> 0);
19467 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19469 /* Reset the number of non-defaulted template arguments contained
19471 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19479 while (parms
&& parms
!= void_list_node
19482 parm
= TREE_VALUE (parms
);
19484 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19485 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19486 /* For a function parameter pack that occurs at the end of the
19487 parameter-declaration-list, the type A of each remaining
19488 argument of the call is compared with the type P of the
19489 declarator-id of the function parameter pack. */
19492 parms
= TREE_CHAIN (parms
);
19494 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19495 /* For a function parameter pack that does not occur at the
19496 end of the parameter-declaration-list, the type of the
19497 parameter pack is a non-deduced context. */
19503 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19509 && parms
!= void_list_node
19510 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19512 /* Unify the remaining arguments with the pack expansion type. */
19514 tree parmvec
= make_tree_vec (1);
19516 /* Allocate a TREE_VEC and copy in all of the arguments */
19517 argvec
= make_tree_vec (nargs
- ia
);
19518 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19519 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19521 /* Copy the parameter into parmvec. */
19522 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19523 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19524 /*subr=*/subr
, explain_p
))
19527 /* Advance to the end of the list of parameters. */
19528 parms
= TREE_CHAIN (parms
);
19531 /* Fail if we've reached the end of the parm list, and more args
19532 are present, and the parm list isn't variadic. */
19533 if (ia
< nargs
&& parms
== void_list_node
)
19534 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19535 /* Fail if parms are left and they don't have default values and
19536 they aren't all deduced as empty packs (c++/57397). This is
19537 consistent with sufficient_parms_p. */
19538 if (parms
&& parms
!= void_list_node
19539 && TREE_PURPOSE (parms
) == NULL_TREE
)
19541 unsigned int count
= nargs
;
19546 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19549 p
= TREE_CHAIN (p
);
19551 while (p
&& p
!= void_list_node
);
19552 if (count
!= nargs
)
19553 return unify_too_few_arguments (explain_p
, ia
, count
,
19559 tsubst_flags_t complain
= (explain_p
19560 ? tf_warning_or_error
19562 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19564 for (i
= 0; i
< ntparms
; i
++)
19566 tree targ
= TREE_VEC_ELT (targs
, i
);
19567 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19569 /* Clear the "incomplete" flags on all argument packs now so that
19570 substituting them into later default arguments works. */
19571 if (targ
&& ARGUMENT_PACK_P (targ
))
19573 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19574 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19577 if (targ
|| tparm
== error_mark_node
)
19579 tparm
= TREE_VALUE (tparm
);
19581 if (TREE_CODE (tparm
) == TYPE_DECL
19582 && !tried_array_deduction
)
19584 try_array_deduction (tparms
, targs
, xparms
);
19585 tried_array_deduction
= true;
19586 if (TREE_VEC_ELT (targs
, i
))
19590 /* If this is an undeduced nontype parameter that depends on
19591 a type parameter, try another pass; its type may have been
19592 deduced from a later argument than the one from which
19593 this parameter can be deduced. */
19594 if (TREE_CODE (tparm
) == PARM_DECL
19595 && uses_template_parms (TREE_TYPE (tparm
))
19596 && saw_undeduced
< 2)
19602 /* Core issue #226 (C++0x) [temp.deduct]:
19604 If a template argument has not been deduced, its
19605 default template argument, if any, is used.
19607 When we are in C++98 mode, TREE_PURPOSE will either
19608 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19609 to explicitly check cxx_dialect here. */
19610 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19611 /* OK, there is a default argument. Wait until after the
19612 conversion check to do substitution. */
19615 /* If the type parameter is a parameter pack, then it will
19616 be deduced to an empty parameter pack. */
19617 if (template_parameter_pack_p (tparm
))
19621 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19623 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19624 TREE_CONSTANT (arg
) = 1;
19627 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19629 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19631 TREE_VEC_ELT (targs
, i
) = arg
;
19635 return unify_parameter_deduction_failure (explain_p
, tparm
);
19638 /* DR 1391: All parameters have args, now check non-dependent parms for
19640 if (saw_undeduced
< 2)
19641 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19642 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19644 parm
= TREE_VALUE (parms
);
19646 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19647 && (!TREE_CHAIN (parms
)
19648 || TREE_CHAIN (parms
) == void_list_node
))
19649 /* For a function parameter pack that occurs at the end of the
19650 parameter-declaration-list, the type A of each remaining
19651 argument of the call is compared with the type P of the
19652 declarator-id of the function parameter pack. */
19655 parms
= TREE_CHAIN (parms
);
19657 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19658 /* For a function parameter pack that does not occur at the
19659 end of the parameter-declaration-list, the type of the
19660 parameter pack is a non-deduced context. */
19666 if (uses_template_parms (parm
))
19668 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19673 /* Now substitute into the default template arguments. */
19674 for (i
= 0; i
< ntparms
; i
++)
19676 tree targ
= TREE_VEC_ELT (targs
, i
);
19677 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19679 if (targ
|| tparm
== error_mark_node
)
19681 tree parm
= TREE_VALUE (tparm
);
19683 if (TREE_CODE (parm
) == PARM_DECL
19684 && uses_template_parms (TREE_TYPE (parm
))
19685 && saw_undeduced
< 2)
19688 tree arg
= TREE_PURPOSE (tparm
);
19689 reopen_deferring_access_checks (*checks
);
19690 location_t save_loc
= input_location
;
19692 input_location
= DECL_SOURCE_LOCATION (parm
);
19693 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19694 if (!uses_template_parms (arg
))
19695 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19697 else if (saw_undeduced
< 2)
19700 arg
= error_mark_node
;
19701 input_location
= save_loc
;
19702 *checks
= get_deferred_access_checks ();
19703 pop_deferring_access_checks ();
19704 if (arg
== error_mark_node
)
19708 TREE_VEC_ELT (targs
, i
) = arg
;
19709 /* The position of the first default template argument,
19710 is also the number of non-defaulted arguments in TARGS.
19712 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19713 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19717 if (saw_undeduced
++ == 1)
19721 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19722 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19724 return unify_success (explain_p
);
19727 /* Subroutine of type_unification_real. Args are like the variables
19728 at the call site. ARG is an overloaded function (or template-id);
19729 we try deducing template args from each of the overloads, and if
19730 only one succeeds, we go with that. Modifies TARGS and returns
19731 true on success. */
19734 resolve_overloaded_unification (tree tparms
,
19738 unification_kind_t strict
,
19742 tree tempargs
= copy_node (targs
);
19744 tree goodfn
= NULL_TREE
;
19747 if (TREE_CODE (arg
) == ADDR_EXPR
)
19749 arg
= TREE_OPERAND (arg
, 0);
19755 if (TREE_CODE (arg
) == COMPONENT_REF
)
19756 /* Handle `&x' where `x' is some static or non-static member
19758 arg
= TREE_OPERAND (arg
, 1);
19760 if (TREE_CODE (arg
) == OFFSET_REF
)
19761 arg
= TREE_OPERAND (arg
, 1);
19763 /* Strip baselink information. */
19764 if (BASELINK_P (arg
))
19765 arg
= BASELINK_FUNCTIONS (arg
);
19767 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19769 /* If we got some explicit template args, we need to plug them into
19770 the affected templates before we try to unify, in case the
19771 explicit args will completely resolve the templates in question. */
19774 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19775 arg
= TREE_OPERAND (arg
, 0);
19777 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19780 tree subargs
, elem
;
19782 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19785 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19786 expl_subargs
, NULL_TREE
, tf_none
,
19787 /*require_all_args=*/true,
19788 /*use_default_args=*/true);
19789 if (subargs
!= error_mark_node
19790 && !any_dependent_template_arguments_p (subargs
))
19792 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19793 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19794 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19795 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19804 /* If no templates (or more than one) are fully resolved by the
19805 explicit arguments, this template-id is a non-deduced context; it
19806 could still be OK if we deduce all template arguments for the
19807 enclosing call through other arguments. */
19811 else if (TREE_CODE (arg
) != OVERLOAD
19812 && TREE_CODE (arg
) != FUNCTION_DECL
)
19813 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19814 -- but the deduction does not succeed because the expression is
19815 not just the function on its own. */
19818 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19821 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19822 strict
, sub_strict
, addr_p
, explain_p
)
19823 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19830 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19831 to function or pointer to member function argument if the set of
19832 overloaded functions does not contain function templates and at most
19833 one of a set of overloaded functions provides a unique match.
19835 So if we found multiple possibilities, we return success but don't
19836 deduce anything. */
19840 int i
= TREE_VEC_LENGTH (targs
);
19842 if (TREE_VEC_ELT (tempargs
, i
))
19844 tree old
= TREE_VEC_ELT (targs
, i
);
19845 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19846 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19847 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19848 /* Don't forget explicit template arguments in a pack. */
19849 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19850 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19851 TREE_VEC_ELT (targs
, i
) = new_
;
19860 /* Core DR 115: In contexts where deduction is done and fails, or in
19861 contexts where deduction is not done, if a template argument list is
19862 specified and it, along with any default template arguments, identifies
19863 a single function template specialization, then the template-id is an
19864 lvalue for the function template specialization. */
19867 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19869 tree expr
, offset
, baselink
;
19872 if (!type_unknown_p (orig_expr
))
19877 offset
= NULL_TREE
;
19878 baselink
= NULL_TREE
;
19880 if (TREE_CODE (expr
) == ADDR_EXPR
)
19882 expr
= TREE_OPERAND (expr
, 0);
19885 if (TREE_CODE (expr
) == OFFSET_REF
)
19888 expr
= TREE_OPERAND (expr
, 1);
19890 if (BASELINK_P (expr
))
19893 expr
= BASELINK_FUNCTIONS (expr
);
19896 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19899 tree goodfn
= NULL_TREE
;
19901 /* If we got some explicit template args, we need to plug them into
19902 the affected templates before we try to unify, in case the
19903 explicit args will completely resolve the templates in question. */
19905 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19906 tree arg
= TREE_OPERAND (expr
, 0);
19907 tree badfn
= NULL_TREE
;
19908 tree badargs
= NULL_TREE
;
19910 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19913 tree subargs
, elem
;
19915 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19918 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19919 expl_subargs
, NULL_TREE
, tf_none
,
19920 /*require_all_args=*/true,
19921 /*use_default_args=*/true);
19922 if (subargs
!= error_mark_node
19923 && !any_dependent_template_arguments_p (subargs
))
19925 elem
= instantiate_template (fn
, subargs
, tf_none
);
19926 if (elem
== error_mark_node
)
19931 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
19940 mark_used (goodfn
);
19943 expr
= build_baselink (BASELINK_BINFO (baselink
),
19944 BASELINK_ACCESS_BINFO (baselink
),
19945 expr
, BASELINK_OPTYPE (baselink
));
19949 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
19950 expr
= build_offset_ref (base
, expr
, addr
, complain
);
19953 expr
= cp_build_addr_expr (expr
, complain
);
19956 else if (good
== 0 && badargs
&& (complain
& tf_error
))
19957 /* There were no good options and at least one bad one, so let the
19958 user know what the problem is. */
19959 instantiate_template (badfn
, badargs
, complain
);
19964 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19965 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19966 different overloads deduce different arguments for a given parm.
19967 ADDR_P is true if the expression for which deduction is being
19968 performed was of the form "& fn" rather than simply "fn".
19970 Returns 1 on success. */
19973 try_one_overload (tree tparms
,
19978 unification_kind_t strict
,
19987 if (arg
== error_mark_node
)
19990 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19991 to function or pointer to member function argument if the set of
19992 overloaded functions does not contain function templates and at most
19993 one of a set of overloaded functions provides a unique match.
19995 So if this is a template, just return success. */
19997 if (uses_template_parms (arg
))
20000 if (TREE_CODE (arg
) == METHOD_TYPE
)
20001 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20003 arg
= build_pointer_type (arg
);
20005 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20007 /* We don't copy orig_targs for this because if we have already deduced
20008 some template args from previous args, unify would complain when we
20009 try to deduce a template parameter for the same argument, even though
20010 there isn't really a conflict. */
20011 nargs
= TREE_VEC_LENGTH (targs
);
20012 tempargs
= make_tree_vec (nargs
);
20014 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20017 /* First make sure we didn't deduce anything that conflicts with
20018 explicitly specified args. */
20019 for (i
= nargs
; i
--; )
20021 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20022 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20026 else if (uses_template_parms (elt
))
20027 /* Since we're unifying against ourselves, we will fill in
20028 template args used in the function parm list with our own
20029 template parms. Discard them. */
20030 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20031 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20033 /* Check that the argument at each index of the deduced argument pack
20034 is equivalent to the corresponding explicitly specified argument.
20035 We may have deduced more arguments than were explicitly specified,
20038 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20039 that's wrong if we deduce the same argument pack from multiple
20040 function arguments: it's only incomplete the first time. */
20042 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20043 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20045 if (TREE_VEC_LENGTH (deduced_pack
)
20046 < TREE_VEC_LENGTH (explicit_pack
))
20049 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20050 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20051 TREE_VEC_ELT (deduced_pack
, j
)))
20054 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20058 for (i
= nargs
; i
--; )
20060 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20063 TREE_VEC_ELT (targs
, i
) = elt
;
20069 /* PARM is a template class (perhaps with unbound template
20070 parameters). ARG is a fully instantiated type. If ARG can be
20071 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20072 TARGS are as for unify. */
20075 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20078 tree copy_of_targs
;
20080 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20082 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20083 /* Matches anything. */;
20084 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20085 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20088 /* We need to make a new template argument vector for the call to
20089 unify. If we used TARGS, we'd clutter it up with the result of
20090 the attempted unification, even if this class didn't work out.
20091 We also don't want to commit ourselves to all the unifications
20092 we've already done, since unification is supposed to be done on
20093 an argument-by-argument basis. In other words, consider the
20094 following pathological case:
20096 template <int I, int J, int K>
20099 template <int I, int J>
20100 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20102 template <int I, int J, int K>
20103 void f(S<I, J, K>, S<I, I, I>);
20112 Now, by the time we consider the unification involving `s2', we
20113 already know that we must have `f<0, 0, 0>'. But, even though
20114 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20115 because there are two ways to unify base classes of S<0, 1, 2>
20116 with S<I, I, I>. If we kept the already deduced knowledge, we
20117 would reject the possibility I=1. */
20118 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20120 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20122 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20127 /* If unification failed, we're done. */
20128 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20129 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20135 /* Given a template type PARM and a class type ARG, find the unique
20136 base type in ARG that is an instance of PARM. We do not examine
20137 ARG itself; only its base-classes. If there is not exactly one
20138 appropriate base class, return NULL_TREE. PARM may be the type of
20139 a partial specialization, as well as a plain template type. Used
20142 static enum template_base_result
20143 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20144 bool explain_p
, tree
*result
)
20146 tree rval
= NULL_TREE
;
20149 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20151 binfo
= TYPE_BINFO (complete_type (arg
));
20154 /* The type could not be completed. */
20155 *result
= NULL_TREE
;
20156 return tbr_incomplete_type
;
20159 /* Walk in inheritance graph order. The search order is not
20160 important, and this avoids multiple walks of virtual bases. */
20161 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20163 tree r
= try_class_unification (tparms
, targs
, parm
,
20164 BINFO_TYPE (binfo
), explain_p
);
20168 /* If there is more than one satisfactory baseclass, then:
20172 If they yield more than one possible deduced A, the type
20176 if (rval
&& !same_type_p (r
, rval
))
20178 *result
= NULL_TREE
;
20179 return tbr_ambiguous_baseclass
;
20187 return tbr_success
;
20190 /* Returns the level of DECL, which declares a template parameter. */
20193 template_decl_level (tree decl
)
20195 switch (TREE_CODE (decl
))
20198 case TEMPLATE_DECL
:
20199 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20202 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20205 gcc_unreachable ();
20210 /* Decide whether ARG can be unified with PARM, considering only the
20211 cv-qualifiers of each type, given STRICT as documented for unify.
20212 Returns nonzero iff the unification is OK on that basis. */
20215 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20217 int arg_quals
= cp_type_quals (arg
);
20218 int parm_quals
= cp_type_quals (parm
);
20220 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20221 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20223 /* Although a CVR qualifier is ignored when being applied to a
20224 substituted template parameter ([8.3.2]/1 for example), that
20225 does not allow us to unify "const T" with "int&" because both
20226 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20227 It is ok when we're allowing additional CV qualifiers
20228 at the outer level [14.8.2.1]/3,1st bullet. */
20229 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20230 || TREE_CODE (arg
) == FUNCTION_TYPE
20231 || TREE_CODE (arg
) == METHOD_TYPE
)
20232 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20235 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20236 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20240 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20241 && (arg_quals
& parm_quals
) != parm_quals
)
20244 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20245 && (parm_quals
& arg_quals
) != arg_quals
)
20251 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20253 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20255 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20256 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20257 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20259 *index
= TEMPLATE_TYPE_IDX (parm
);
20260 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20264 *index
= TEMPLATE_PARM_IDX (parm
);
20265 *level
= TEMPLATE_PARM_LEVEL (parm
);
20269 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20271 if (unify (TP, TA, P, A, S, EP)) \
20275 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20276 expansion at the end of PACKED_PARMS. Returns 0 if the type
20277 deduction succeeds, 1 otherwise. STRICT is the same as in
20278 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20279 function call argument list. We'll need to adjust the arguments to make them
20280 types. SUBR tells us if this is from a recursive call to
20281 type_unification_real, or for comparing two template argument
20285 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20286 tree packed_args
, unification_kind_t strict
,
20287 bool subr
, bool explain_p
)
20290 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20291 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20292 tree pack
, packs
= NULL_TREE
;
20293 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20295 /* Add in any args remembered from an earlier partial instantiation. */
20296 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20298 packed_args
= expand_template_argument_pack (packed_args
);
20300 int len
= TREE_VEC_LENGTH (packed_args
);
20302 /* Determine the parameter packs we will be deducing from the
20303 pattern, and record their current deductions. */
20304 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20305 pack
; pack
= TREE_CHAIN (pack
))
20307 tree parm_pack
= TREE_VALUE (pack
);
20310 /* Determine the index and level of this parameter pack. */
20311 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20313 /* Keep track of the parameter packs and their corresponding
20315 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20316 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20319 /* Loop through all of the arguments that have not yet been
20320 unified and unify each with the pattern. */
20321 for (i
= start
; i
< len
; i
++)
20324 bool any_explicit
= false;
20325 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20327 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20328 or the element of its argument pack at the current index if
20329 this argument was explicitly specified. */
20330 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20334 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20337 if (TREE_VALUE (pack
)
20338 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20339 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20341 any_explicit
= true;
20342 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20344 TMPL_ARG (targs
, level
, idx
) = arg
;
20347 /* If we had explicit template arguments, substitute them into the
20348 pattern before deduction. */
20351 /* Some arguments might still be unspecified or dependent. */
20353 ++processing_template_decl
;
20354 dependent
= any_dependent_template_arguments_p (targs
);
20356 --processing_template_decl
;
20357 parm
= tsubst (pattern
, targs
,
20358 explain_p
? tf_warning_or_error
: tf_none
,
20361 --processing_template_decl
;
20362 if (parm
== error_mark_node
)
20368 /* Unify the pattern with the current argument. */
20369 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20373 /* For each parameter pack, collect the deduced value. */
20374 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20377 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20379 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20380 TMPL_ARG (targs
, level
, idx
);
20384 /* Verify that the results of unification with the parameter packs
20385 produce results consistent with what we've seen before, and make
20386 the deduced argument packs available. */
20387 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20389 tree old_pack
= TREE_VALUE (pack
);
20390 tree new_args
= TREE_TYPE (pack
);
20391 int i
, len
= TREE_VEC_LENGTH (new_args
);
20393 bool nondeduced_p
= false;
20395 /* By default keep the original deduced argument pack.
20396 If necessary, more specific code is going to update the
20397 resulting deduced argument later down in this function. */
20398 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20399 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20401 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20402 actually deduce anything. */
20403 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20404 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20405 nondeduced_p
= true;
20409 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20411 /* If we had fewer function args than explicit template args,
20412 just use the explicits. */
20413 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20414 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20415 if (len
< explicit_len
)
20416 new_args
= explicit_args
;
20422 /* Build the deduced *_ARGUMENT_PACK. */
20423 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20425 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20426 TREE_CONSTANT (result
) = 1;
20429 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20431 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20433 /* Note the deduced argument packs for this parameter
20435 TMPL_ARG (targs
, level
, idx
) = result
;
20437 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20438 && (ARGUMENT_PACK_ARGS (old_pack
)
20439 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20441 /* We only had the explicitly-provided arguments before, but
20442 now we have a complete set of arguments. */
20443 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20445 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20446 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20447 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20451 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20452 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20454 if (!comp_template_args (old_args
, new_args
,
20455 &bad_old_arg
, &bad_new_arg
))
20456 /* Inconsistent unification of this parameter pack. */
20457 return unify_parameter_pack_inconsistent (explain_p
,
20463 return unify_success (explain_p
);
20466 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20467 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20468 parameters and return value are as for unify. */
20471 unify_array_domain (tree tparms
, tree targs
,
20472 tree parm_dom
, tree arg_dom
,
20480 /* Our representation of array types uses "N - 1" as the
20481 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20482 not an integer constant. We cannot unify arbitrarily
20483 complex expressions, so we eliminate the MINUS_EXPRs
20485 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20486 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20489 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20490 parm_max
= TREE_OPERAND (parm_max
, 0);
20492 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20493 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20496 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20497 trying to unify the type of a variable with the type
20498 of a template parameter. For example:
20500 template <unsigned int N>
20501 void f (char (&) [N]);
20508 Here, the type of the ARG will be "int [g(i)]", and
20509 may be a SAVE_EXPR, etc. */
20510 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20511 return unify_vla_arg (explain_p
, arg_dom
);
20512 arg_max
= TREE_OPERAND (arg_max
, 0);
20515 /* If only one of the bounds used a MINUS_EXPR, compensate
20516 by adding one to the other bound. */
20517 if (parm_cst
&& !arg_cst
)
20518 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20522 else if (arg_cst
&& !parm_cst
)
20523 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20528 return unify (tparms
, targs
, parm_max
, arg_max
,
20529 UNIFY_ALLOW_INTEGER
, explain_p
);
20532 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20534 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20539 if (PACK_EXPANSION_P (t
))
20540 t
= PACK_EXPANSION_PATTERN (t
);
20541 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20542 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20543 || DECL_TYPE_TEMPLATE_P (t
))
20545 else if (TYPE_P (t
))
20551 /* Deduce the value of template parameters. TPARMS is the (innermost)
20552 set of template parameters to a template. TARGS is the bindings
20553 for those template parameters, as determined thus far; TARGS may
20554 include template arguments for outer levels of template parameters
20555 as well. PARM is a parameter to a template function, or a
20556 subcomponent of that parameter; ARG is the corresponding argument.
20557 This function attempts to match PARM with ARG in a manner
20558 consistent with the existing assignments in TARGS. If more values
20559 are deduced, then TARGS is updated.
20561 Returns 0 if the type deduction succeeds, 1 otherwise. The
20562 parameter STRICT is a bitwise or of the following flags:
20565 Require an exact match between PARM and ARG.
20566 UNIFY_ALLOW_MORE_CV_QUAL:
20567 Allow the deduced ARG to be more cv-qualified (by qualification
20568 conversion) than ARG.
20569 UNIFY_ALLOW_LESS_CV_QUAL:
20570 Allow the deduced ARG to be less cv-qualified than ARG.
20571 UNIFY_ALLOW_DERIVED:
20572 Allow the deduced ARG to be a template base class of ARG,
20573 or a pointer to a template base class of the type pointed to by
20575 UNIFY_ALLOW_INTEGER:
20576 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20577 case for more information.
20578 UNIFY_ALLOW_OUTER_LEVEL:
20579 This is the outermost level of a deduction. Used to determine validity
20580 of qualification conversions. A valid qualification conversion must
20581 have const qualified pointers leading up to the inner type which
20582 requires additional CV quals, except at the outer level, where const
20583 is not required [conv.qual]. It would be normal to set this flag in
20584 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20585 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20586 This is the outermost level of a deduction, and PARM can be more CV
20587 qualified at this point.
20588 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20589 This is the outermost level of a deduction, and PARM can be less CV
20590 qualified at this point. */
20593 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20599 int strict_in
= strict
;
20600 tsubst_flags_t complain
= (explain_p
20601 ? tf_warning_or_error
20604 /* I don't think this will do the right thing with respect to types.
20605 But the only case I've seen it in so far has been array bounds, where
20606 signedness is the only information lost, and I think that will be
20608 while (CONVERT_EXPR_P (parm
))
20609 parm
= TREE_OPERAND (parm
, 0);
20611 if (arg
== error_mark_node
)
20612 return unify_invalid (explain_p
);
20613 if (arg
== unknown_type_node
20614 || arg
== init_list_type_node
)
20615 /* We can't deduce anything from this, but we might get all the
20616 template args from other function args. */
20617 return unify_success (explain_p
);
20619 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20620 return unify_success (explain_p
);
20622 /* If PARM uses template parameters, then we can't bail out here,
20623 even if ARG == PARM, since we won't record unifications for the
20624 template parameters. We might need them if we're trying to
20625 figure out which of two things is more specialized. */
20626 if (arg
== parm
&& !uses_template_parms (parm
))
20627 return unify_success (explain_p
);
20629 /* Handle init lists early, so the rest of the function can assume
20630 we're dealing with a type. */
20631 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20635 tree orig_parm
= parm
;
20637 /* Replace T with std::initializer_list<T> for deduction. */
20638 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20639 && flag_deduce_init_list
)
20640 parm
= listify (parm
);
20642 if (!is_std_init_list (parm
)
20643 && TREE_CODE (parm
) != ARRAY_TYPE
)
20644 /* We can only deduce from an initializer list argument if the
20645 parameter is std::initializer_list or an array; otherwise this
20646 is a non-deduced context. */
20647 return unify_success (explain_p
);
20649 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20650 elttype
= TREE_TYPE (parm
);
20653 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20654 /* Deduction is defined in terms of a single type, so just punt
20655 on the (bizarre) std::initializer_list<T...>. */
20656 if (PACK_EXPANSION_P (elttype
))
20657 return unify_success (explain_p
);
20660 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20662 int elt_strict
= strict
;
20664 if (elt
== error_mark_node
)
20665 return unify_invalid (explain_p
);
20667 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20669 tree type
= TREE_TYPE (elt
);
20670 if (type
== error_mark_node
)
20671 return unify_invalid (explain_p
);
20672 /* It should only be possible to get here for a call. */
20673 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20674 elt_strict
|= maybe_adjust_types_for_deduction
20675 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20679 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20683 if (TREE_CODE (parm
) == ARRAY_TYPE
20684 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20686 /* Also deduce from the length of the initializer list. */
20687 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20688 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20689 if (idx
== error_mark_node
)
20690 return unify_invalid (explain_p
);
20691 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20695 /* If the std::initializer_list<T> deduction worked, replace the
20696 deduced A with std::initializer_list<A>. */
20697 if (orig_parm
!= parm
)
20699 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20700 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20701 targ
= listify (targ
);
20702 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20704 return unify_success (explain_p
);
20707 /* If parm and arg aren't the same kind of thing (template, type, or
20708 expression), fail early. */
20709 if (pa_kind (parm
) != pa_kind (arg
))
20710 return unify_invalid (explain_p
);
20712 /* Immediately reject some pairs that won't unify because of
20713 cv-qualification mismatches. */
20714 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20716 /* It is the elements of the array which hold the cv quals of an array
20717 type, and the elements might be template type parms. We'll check
20718 when we recurse. */
20719 && TREE_CODE (arg
) != ARRAY_TYPE
20720 /* We check the cv-qualifiers when unifying with template type
20721 parameters below. We want to allow ARG `const T' to unify with
20722 PARM `T' for example, when computing which of two templates
20723 is more specialized, for example. */
20724 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20725 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20726 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20728 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20729 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20730 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20731 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20732 strict
&= ~UNIFY_ALLOW_DERIVED
;
20733 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20734 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20736 switch (TREE_CODE (parm
))
20738 case TYPENAME_TYPE
:
20740 case UNBOUND_CLASS_TEMPLATE
:
20741 /* In a type which contains a nested-name-specifier, template
20742 argument values cannot be deduced for template parameters used
20743 within the nested-name-specifier. */
20744 return unify_success (explain_p
);
20746 case TEMPLATE_TYPE_PARM
:
20747 case TEMPLATE_TEMPLATE_PARM
:
20748 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20749 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20750 if (error_operand_p (tparm
))
20751 return unify_invalid (explain_p
);
20753 if (TEMPLATE_TYPE_LEVEL (parm
)
20754 != template_decl_level (tparm
))
20755 /* The PARM is not one we're trying to unify. Just check
20756 to see if it matches ARG. */
20758 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20759 && (is_auto (parm
) ? is_auto (arg
)
20760 : same_type_p (parm
, arg
)))
20761 return unify_success (explain_p
);
20763 return unify_type_mismatch (explain_p
, parm
, arg
);
20765 idx
= TEMPLATE_TYPE_IDX (parm
);
20766 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20767 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20768 if (error_operand_p (tparm
))
20769 return unify_invalid (explain_p
);
20771 /* Check for mixed types and values. */
20772 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20773 && TREE_CODE (tparm
) != TYPE_DECL
)
20774 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20775 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20776 gcc_unreachable ();
20778 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20780 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20781 && CLASS_TYPE_P (arg
))
20783 /* First try to match ARG directly. */
20784 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20788 /* Otherwise, look for a suitable base of ARG, as below. */
20789 enum template_base_result r
;
20790 r
= get_template_base (tparms
, targs
, parm
, arg
,
20793 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20797 /* ARG must be constructed from a template class or a template
20798 template parameter. */
20799 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20800 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20801 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20803 /* Deduce arguments T, i from TT<T> or TT<i>. */
20804 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20807 arg
= TYPE_TI_TEMPLATE (arg
);
20809 /* Fall through to deduce template name. */
20812 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20813 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20815 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20817 /* Simple cases: Value already set, does match or doesn't. */
20818 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20819 return unify_success (explain_p
);
20821 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20825 /* If PARM is `const T' and ARG is only `int', we don't have
20826 a match unless we are allowing additional qualification.
20827 If ARG is `const int' and PARM is just `T' that's OK;
20828 that binds `const int' to `T'. */
20829 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20831 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20833 /* Consider the case where ARG is `const volatile int' and
20834 PARM is `const T'. Then, T should be `volatile int'. */
20835 arg
= cp_build_qualified_type_real
20836 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20837 if (arg
== error_mark_node
)
20838 return unify_invalid (explain_p
);
20840 /* Simple cases: Value already set, does match or doesn't. */
20841 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20842 return unify_success (explain_p
);
20844 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20846 /* Make sure that ARG is not a variable-sized array. (Note
20847 that were talking about variable-sized arrays (like
20848 `int[n]'), rather than arrays of unknown size (like
20849 `int[]').) We'll get very confused by such a type since
20850 the bound of the array is not constant, and therefore
20851 not mangleable. Besides, such types are not allowed in
20852 ISO C++, so we can do as we please here. We do allow
20853 them for 'auto' deduction, since that isn't ABI-exposed. */
20854 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20855 return unify_vla_arg (explain_p
, arg
);
20857 /* Strip typedefs as in convert_template_argument. */
20858 arg
= canonicalize_type_argument (arg
, tf_none
);
20861 /* If ARG is a parameter pack or an expansion, we cannot unify
20862 against it unless PARM is also a parameter pack. */
20863 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20864 && !template_parameter_pack_p (parm
))
20865 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20867 /* If the argument deduction results is a METHOD_TYPE,
20868 then there is a problem.
20869 METHOD_TYPE doesn't map to any real C++ type the result of
20870 the deduction can not be of that type. */
20871 if (TREE_CODE (arg
) == METHOD_TYPE
)
20872 return unify_method_type_error (explain_p
, arg
);
20874 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20875 return unify_success (explain_p
);
20877 case TEMPLATE_PARM_INDEX
:
20878 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20879 if (error_operand_p (tparm
))
20880 return unify_invalid (explain_p
);
20882 if (TEMPLATE_PARM_LEVEL (parm
)
20883 != template_decl_level (tparm
))
20885 /* The PARM is not one we're trying to unify. Just check
20886 to see if it matches ARG. */
20887 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20888 && cp_tree_equal (parm
, arg
));
20890 unify_expression_unequal (explain_p
, parm
, arg
);
20894 idx
= TEMPLATE_PARM_IDX (parm
);
20895 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20899 int x
= !cp_tree_equal (targ
, arg
);
20901 unify_inconsistency (explain_p
, parm
, targ
, arg
);
20905 /* [temp.deduct.type] If, in the declaration of a function template
20906 with a non-type template-parameter, the non-type
20907 template-parameter is used in an expression in the function
20908 parameter-list and, if the corresponding template-argument is
20909 deduced, the template-argument type shall match the type of the
20910 template-parameter exactly, except that a template-argument
20911 deduced from an array bound may be of any integral type.
20912 The non-type parameter might use already deduced type parameters. */
20913 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
20914 if (tree a
= type_uses_auto (tparm
))
20916 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
20917 if (tparm
== error_mark_node
)
20921 if (!TREE_TYPE (arg
))
20922 /* Template-parameter dependent expression. Just accept it for now.
20923 It will later be processed in convert_template_argument. */
20925 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
20926 non_reference (tparm
)))
20928 else if ((strict
& UNIFY_ALLOW_INTEGER
)
20929 && CP_INTEGRAL_TYPE_P (tparm
))
20930 /* Convert the ARG to the type of PARM; the deduced non-type
20931 template argument must exactly match the types of the
20932 corresponding parameter. */
20933 arg
= fold (build_nop (tparm
, arg
));
20934 else if (uses_template_parms (tparm
))
20936 /* We haven't deduced the type of this parameter yet. */
20937 if (cxx_dialect
>= cxx17
20938 /* We deduce from array bounds in try_array_deduction. */
20939 && !(strict
& UNIFY_ALLOW_INTEGER
))
20941 /* Deduce it from the non-type argument. */
20942 tree atype
= TREE_TYPE (arg
);
20943 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20945 UNIFY_ALLOW_NONE
, explain_p
);
20948 /* Try again later. */
20949 return unify_success (explain_p
);
20952 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
20954 /* If ARG is a parameter pack or an expansion, we cannot unify
20955 against it unless PARM is also a parameter pack. */
20956 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20957 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
20958 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20961 bool removed_attr
= false;
20962 arg
= strip_typedefs_expr (arg
, &removed_attr
);
20964 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20965 return unify_success (explain_p
);
20969 /* A pointer-to-member constant can be unified only with
20970 another constant. */
20971 if (TREE_CODE (arg
) != PTRMEM_CST
)
20972 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
20974 /* Just unify the class member. It would be useless (and possibly
20975 wrong, depending on the strict flags) to unify also
20976 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20977 arg refer to the same variable, even if through different
20978 classes. For instance:
20980 struct A { int x; };
20983 Unification of &A::x and &B::x must succeed. */
20984 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
20985 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
20990 if (!TYPE_PTR_P (arg
))
20991 return unify_type_mismatch (explain_p
, parm
, arg
);
20993 /* [temp.deduct.call]
20995 A can be another pointer or pointer to member type that can
20996 be converted to the deduced A via a qualification
20997 conversion (_conv.qual_).
20999 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21000 This will allow for additional cv-qualification of the
21001 pointed-to types if appropriate. */
21003 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21004 /* The derived-to-base conversion only persists through one
21005 level of pointers. */
21006 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21008 return unify (tparms
, targs
, TREE_TYPE (parm
),
21009 TREE_TYPE (arg
), strict
, explain_p
);
21012 case REFERENCE_TYPE
:
21013 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21014 return unify_type_mismatch (explain_p
, parm
, arg
);
21015 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21016 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21019 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21020 return unify_type_mismatch (explain_p
, parm
, arg
);
21021 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21022 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21023 return unify_type_mismatch (explain_p
, parm
, arg
);
21024 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21025 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21026 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21027 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21028 TYPE_DOMAIN (arg
), explain_p
);
21029 return unify_success (explain_p
);
21036 case ENUMERAL_TYPE
:
21039 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21040 return unify_type_mismatch (explain_p
, parm
, arg
);
21042 /* We have already checked cv-qualification at the top of the
21044 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21045 return unify_type_mismatch (explain_p
, parm
, arg
);
21047 /* As far as unification is concerned, this wins. Later checks
21048 will invalidate it if necessary. */
21049 return unify_success (explain_p
);
21051 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21052 /* Type INTEGER_CST can come from ordinary constant template args. */
21054 while (CONVERT_EXPR_P (arg
))
21055 arg
= TREE_OPERAND (arg
, 0);
21057 if (TREE_CODE (arg
) != INTEGER_CST
)
21058 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21059 return (tree_int_cst_equal (parm
, arg
)
21060 ? unify_success (explain_p
)
21061 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21065 int i
, len
, argslen
;
21066 int parm_variadic_p
= 0;
21068 if (TREE_CODE (arg
) != TREE_VEC
)
21069 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21071 len
= TREE_VEC_LENGTH (parm
);
21072 argslen
= TREE_VEC_LENGTH (arg
);
21074 /* Check for pack expansions in the parameters. */
21075 for (i
= 0; i
< len
; ++i
)
21077 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21080 /* We can unify against something with a trailing
21082 parm_variadic_p
= 1;
21084 /* [temp.deduct.type]/9: If the template argument list of
21085 P contains a pack expansion that is not the last
21086 template argument, the entire template argument list
21087 is a non-deduced context. */
21088 return unify_success (explain_p
);
21092 /* If we don't have enough arguments to satisfy the parameters
21093 (not counting the pack expression at the end), or we have
21094 too many arguments for a parameter list that doesn't end in
21095 a pack expression, we can't unify. */
21096 if (parm_variadic_p
21097 ? argslen
< len
- parm_variadic_p
21099 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21101 /* Unify all of the parameters that precede the (optional)
21102 pack expression. */
21103 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21105 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21106 TREE_VEC_ELT (parm
, i
),
21107 TREE_VEC_ELT (arg
, i
),
21108 UNIFY_ALLOW_NONE
, explain_p
);
21110 if (parm_variadic_p
)
21111 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21113 /*subr=*/true, explain_p
);
21114 return unify_success (explain_p
);
21119 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21120 return unify_type_mismatch (explain_p
, parm
, arg
);
21122 if (TYPE_PTRMEMFUNC_P (parm
))
21124 if (!TYPE_PTRMEMFUNC_P (arg
))
21125 return unify_type_mismatch (explain_p
, parm
, arg
);
21127 return unify (tparms
, targs
,
21128 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21129 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21130 strict
, explain_p
);
21132 else if (TYPE_PTRMEMFUNC_P (arg
))
21133 return unify_type_mismatch (explain_p
, parm
, arg
);
21135 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21137 tree t
= NULL_TREE
;
21139 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21141 /* First, we try to unify the PARM and ARG directly. */
21142 t
= try_class_unification (tparms
, targs
,
21143 parm
, arg
, explain_p
);
21147 /* Fallback to the special case allowed in
21148 [temp.deduct.call]:
21150 If P is a class, and P has the form
21151 template-id, then A can be a derived class of
21152 the deduced A. Likewise, if P is a pointer to
21153 a class of the form template-id, A can be a
21154 pointer to a derived class pointed to by the
21156 enum template_base_result r
;
21157 r
= get_template_base (tparms
, targs
, parm
, arg
,
21162 /* Don't give the derived diagnostic if we're
21163 already dealing with the same template. */
21165 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21166 && (CLASSTYPE_TI_TEMPLATE (parm
)
21167 == CLASSTYPE_TI_TEMPLATE (arg
)));
21168 return unify_no_common_base (explain_p
&& !same_template
,
21173 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21174 && (CLASSTYPE_TI_TEMPLATE (parm
)
21175 == CLASSTYPE_TI_TEMPLATE (arg
)))
21176 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21177 Then, we should unify `int' and `U'. */
21180 /* There's no chance of unification succeeding. */
21181 return unify_type_mismatch (explain_p
, parm
, arg
);
21183 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21184 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21186 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21187 return unify_type_mismatch (explain_p
, parm
, arg
);
21188 return unify_success (explain_p
);
21191 case FUNCTION_TYPE
:
21193 unsigned int nargs
;
21198 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21199 return unify_type_mismatch (explain_p
, parm
, arg
);
21201 /* CV qualifications for methods can never be deduced, they must
21202 match exactly. We need to check them explicitly here,
21203 because type_unification_real treats them as any other
21204 cv-qualified parameter. */
21205 if (TREE_CODE (parm
) == METHOD_TYPE
21206 && (!check_cv_quals_for_unify
21208 class_of_this_parm (arg
),
21209 class_of_this_parm (parm
))))
21210 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21211 if (TREE_CODE (arg
) == FUNCTION_TYPE
21212 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21213 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21214 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21215 return unify_type_mismatch (explain_p
, parm
, arg
);
21217 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21218 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21220 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21221 args
= XALLOCAVEC (tree
, nargs
);
21222 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21223 a
!= NULL_TREE
&& a
!= void_list_node
;
21224 a
= TREE_CHAIN (a
), ++i
)
21225 args
[i
] = TREE_VALUE (a
);
21228 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21229 args
, nargs
, 1, DEDUCE_EXACT
,
21230 LOOKUP_NORMAL
, NULL
, explain_p
))
21233 if (flag_noexcept_type
)
21235 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21236 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21237 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21238 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21239 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21240 && uses_template_parms (TREE_PURPOSE (pspec
)))
21241 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21242 TREE_PURPOSE (aspec
),
21243 UNIFY_ALLOW_NONE
, explain_p
);
21244 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21245 return unify_type_mismatch (explain_p
, parm
, arg
);
21252 /* Unify a pointer to member with a pointer to member function, which
21253 deduces the type of the member as a function type. */
21254 if (TYPE_PTRMEMFUNC_P (arg
))
21256 /* Check top-level cv qualifiers */
21257 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21258 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21260 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21261 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21262 UNIFY_ALLOW_NONE
, explain_p
);
21264 /* Determine the type of the function we are unifying against. */
21265 tree fntype
= static_fn_type (arg
);
21267 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21270 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21271 return unify_type_mismatch (explain_p
, parm
, arg
);
21272 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21273 TYPE_OFFSET_BASETYPE (arg
),
21274 UNIFY_ALLOW_NONE
, explain_p
);
21275 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21276 strict
, explain_p
);
21279 if (DECL_TEMPLATE_PARM_P (parm
))
21280 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21281 if (arg
!= scalar_constant_value (parm
))
21282 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21283 return unify_success (explain_p
);
21286 case TEMPLATE_DECL
:
21287 /* Matched cases are handled by the ARG == PARM test above. */
21288 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21291 /* We might get a variable as a non-type template argument in parm if the
21292 corresponding parameter is type-dependent. Make any necessary
21293 adjustments based on whether arg is a reference. */
21294 if (CONSTANT_CLASS_P (arg
))
21295 parm
= fold_non_dependent_expr (parm
);
21296 else if (REFERENCE_REF_P (arg
))
21298 tree sub
= TREE_OPERAND (arg
, 0);
21300 if (TREE_CODE (sub
) == ADDR_EXPR
)
21301 arg
= TREE_OPERAND (sub
, 0);
21303 /* Now use the normal expression code to check whether they match. */
21306 case TYPE_ARGUMENT_PACK
:
21307 case NONTYPE_ARGUMENT_PACK
:
21308 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21309 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21312 case DECLTYPE_TYPE
:
21313 case UNDERLYING_TYPE
:
21314 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21315 or UNDERLYING_TYPE nodes. */
21316 return unify_success (explain_p
);
21319 /* Unification fails if we hit an error node. */
21320 return unify_invalid (explain_p
);
21323 if (REFERENCE_REF_P (parm
))
21325 bool pexp
= PACK_EXPANSION_P (arg
);
21327 arg
= PACK_EXPANSION_PATTERN (arg
);
21328 if (REFERENCE_REF_P (arg
))
21329 arg
= TREE_OPERAND (arg
, 0);
21331 arg
= make_pack_expansion (arg
, complain
);
21332 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21333 strict
, explain_p
);
21338 /* An unresolved overload is a nondeduced context. */
21339 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21340 return unify_success (explain_p
);
21341 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21343 /* We must be looking at an expression. This can happen with
21347 void foo(S<I>, S<I + 2>);
21349 This is a "nondeduced context":
21353 The nondeduced contexts are:
21355 --A type that is a template-id in which one or more of
21356 the template-arguments is an expression that references
21357 a template-parameter.
21359 In these cases, we assume deduction succeeded, but don't
21360 actually infer any unifications. */
21362 if (!uses_template_parms (parm
)
21363 && !template_args_equal (parm
, arg
))
21364 return unify_expression_unequal (explain_p
, parm
, arg
);
21366 return unify_success (explain_p
);
21369 #undef RECUR_AND_CHECK_FAILURE
21371 /* Note that DECL can be defined in this translation unit, if
21375 mark_definable (tree decl
)
21378 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21379 FOR_EACH_CLONE (clone
, decl
)
21380 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21383 /* Called if RESULT is explicitly instantiated, or is a member of an
21384 explicitly instantiated class. */
21387 mark_decl_instantiated (tree result
, int extern_p
)
21389 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21391 /* If this entity has already been written out, it's too late to
21392 make any modifications. */
21393 if (TREE_ASM_WRITTEN (result
))
21396 /* For anonymous namespace we don't need to do anything. */
21397 if (decl_anon_ns_mem_p (result
))
21399 gcc_assert (!TREE_PUBLIC (result
));
21403 if (TREE_CODE (result
) != FUNCTION_DECL
)
21404 /* The TREE_PUBLIC flag for function declarations will have been
21405 set correctly by tsubst. */
21406 TREE_PUBLIC (result
) = 1;
21408 /* This might have been set by an earlier implicit instantiation. */
21409 DECL_COMDAT (result
) = 0;
21412 DECL_NOT_REALLY_EXTERN (result
) = 0;
21415 mark_definable (result
);
21416 mark_needed (result
);
21417 /* Always make artificials weak. */
21418 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21419 comdat_linkage (result
);
21420 /* For WIN32 we also want to put explicit instantiations in
21421 linkonce sections. */
21422 else if (TREE_PUBLIC (result
))
21423 maybe_make_one_only (result
);
21426 /* If EXTERN_P, then this function will not be emitted -- unless
21427 followed by an explicit instantiation, at which point its linkage
21428 will be adjusted. If !EXTERN_P, then this function will be
21429 emitted here. In neither circumstance do we want
21430 import_export_decl to adjust the linkage. */
21431 DECL_INTERFACE_KNOWN (result
) = 1;
21434 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21435 important template arguments. If any are missing, we check whether
21436 they're important by using error_mark_node for substituting into any
21437 args that were used for partial ordering (the ones between ARGS and END)
21438 and seeing if it bubbles up. */
21441 check_undeduced_parms (tree targs
, tree args
, tree end
)
21443 bool found
= false;
21445 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21446 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21449 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21453 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21454 if (substed
== error_mark_node
)
21460 /* Given two function templates PAT1 and PAT2, return:
21462 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21463 -1 if PAT2 is more specialized than PAT1.
21464 0 if neither is more specialized.
21466 LEN indicates the number of parameters we should consider
21467 (defaulted parameters should not be considered).
21469 The 1998 std underspecified function template partial ordering, and
21470 DR214 addresses the issue. We take pairs of arguments, one from
21471 each of the templates, and deduce them against each other. One of
21472 the templates will be more specialized if all the *other*
21473 template's arguments deduce against its arguments and at least one
21474 of its arguments *does* *not* deduce against the other template's
21475 corresponding argument. Deduction is done as for class templates.
21476 The arguments used in deduction have reference and top level cv
21477 qualifiers removed. Iff both arguments were originally reference
21478 types *and* deduction succeeds in both directions, an lvalue reference
21479 wins against an rvalue reference and otherwise the template
21480 with the more cv-qualified argument wins for that pairing (if
21481 neither is more cv-qualified, they both are equal). Unlike regular
21482 deduction, after all the arguments have been deduced in this way,
21483 we do *not* verify the deduced template argument values can be
21484 substituted into non-deduced contexts.
21486 The logic can be a bit confusing here, because we look at deduce1 and
21487 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21488 can find template arguments for pat1 to make arg1 look like arg2, that
21489 means that arg2 is at least as specialized as arg1. */
21492 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21494 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21495 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21496 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21497 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21498 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21499 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21500 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21501 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21502 tree origs1
, origs2
;
21503 bool lose1
= false;
21504 bool lose2
= false;
21506 /* Remove the this parameter from non-static member functions. If
21507 one is a non-static member function and the other is not a static
21508 member function, remove the first parameter from that function
21509 also. This situation occurs for operator functions where we
21510 locate both a member function (with this pointer) and non-member
21511 operator (with explicit first operand). */
21512 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21514 len
--; /* LEN is the number of significant arguments for DECL1 */
21515 args1
= TREE_CHAIN (args1
);
21516 if (!DECL_STATIC_FUNCTION_P (decl2
))
21517 args2
= TREE_CHAIN (args2
);
21519 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21521 args2
= TREE_CHAIN (args2
);
21522 if (!DECL_STATIC_FUNCTION_P (decl1
))
21525 args1
= TREE_CHAIN (args1
);
21529 /* If only one is a conversion operator, they are unordered. */
21530 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21533 /* Consider the return type for a conversion function */
21534 if (DECL_CONV_FN_P (decl1
))
21536 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21537 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21541 processing_template_decl
++;
21547 /* Stop when an ellipsis is seen. */
21548 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21550 tree arg1
= TREE_VALUE (args1
);
21551 tree arg2
= TREE_VALUE (args2
);
21552 int deduce1
, deduce2
;
21558 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21559 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21561 /* When both arguments are pack expansions, we need only
21562 unify the patterns themselves. */
21563 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21564 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21566 /* This is the last comparison we need to do. */
21570 /* DR 1847: If a particular P contains no template-parameters that
21571 participate in template argument deduction, that P is not used to
21572 determine the ordering. */
21573 if (!uses_deducible_template_parms (arg1
)
21574 && !uses_deducible_template_parms (arg2
))
21577 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21579 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21580 arg1
= TREE_TYPE (arg1
);
21581 quals1
= cp_type_quals (arg1
);
21584 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21586 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21587 arg2
= TREE_TYPE (arg2
);
21588 quals2
= cp_type_quals (arg2
);
21591 arg1
= TYPE_MAIN_VARIANT (arg1
);
21592 arg2
= TYPE_MAIN_VARIANT (arg2
);
21594 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21596 int i
, len2
= remaining_arguments (args2
);
21597 tree parmvec
= make_tree_vec (1);
21598 tree argvec
= make_tree_vec (len2
);
21601 /* Setup the parameter vector, which contains only ARG1. */
21602 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21604 /* Setup the argument vector, which contains the remaining
21606 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21607 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21609 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21610 argvec
, DEDUCE_EXACT
,
21611 /*subr=*/true, /*explain_p=*/false)
21614 /* We cannot deduce in the other direction, because ARG1 is
21615 a pack expansion but ARG2 is not. */
21618 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21620 int i
, len1
= remaining_arguments (args1
);
21621 tree parmvec
= make_tree_vec (1);
21622 tree argvec
= make_tree_vec (len1
);
21625 /* Setup the parameter vector, which contains only ARG1. */
21626 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21628 /* Setup the argument vector, which contains the remaining
21630 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21631 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21633 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21634 argvec
, DEDUCE_EXACT
,
21635 /*subr=*/true, /*explain_p=*/false)
21638 /* We cannot deduce in the other direction, because ARG2 is
21639 a pack expansion but ARG1 is not.*/
21645 /* The normal case, where neither argument is a pack
21647 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21648 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21650 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21651 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21655 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21656 arg2, then arg2 is not as specialized as arg1. */
21662 /* "If, for a given type, deduction succeeds in both directions
21663 (i.e., the types are identical after the transformations above)
21664 and both P and A were reference types (before being replaced with
21665 the type referred to above):
21666 - if the type from the argument template was an lvalue reference and
21667 the type from the parameter template was not, the argument type is
21668 considered to be more specialized than the other; otherwise,
21669 - if the type from the argument template is more cv-qualified
21670 than the type from the parameter template (as described above),
21671 the argument type is considered to be more specialized than the other;
21673 - neither type is more specialized than the other." */
21675 if (deduce1
&& deduce2
)
21677 if (ref1
&& ref2
&& ref1
!= ref2
)
21684 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21686 if ((quals1
& quals2
) == quals2
)
21688 if ((quals1
& quals2
) == quals1
)
21693 if (lose1
&& lose2
)
21694 /* We've failed to deduce something in either direction.
21695 These must be unordered. */
21700 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21701 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21702 /* We have already processed all of the arguments in our
21703 handing of the pack expansion type. */
21706 args1
= TREE_CHAIN (args1
);
21707 args2
= TREE_CHAIN (args2
);
21710 /* "In most cases, all template parameters must have values in order for
21711 deduction to succeed, but for partial ordering purposes a template
21712 parameter may remain without a value provided it is not used in the
21713 types being used for partial ordering."
21715 Thus, if we are missing any of the targs1 we need to substitute into
21716 origs1, then pat2 is not as specialized as pat1. This can happen when
21717 there is a nondeduced context. */
21718 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21720 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21723 processing_template_decl
--;
21725 /* If both deductions succeed, the partial ordering selects the more
21726 constrained template. */
21727 if (!lose1
&& !lose2
)
21729 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21730 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21731 lose1
= !subsumes_constraints (c1
, c2
);
21732 lose2
= !subsumes_constraints (c2
, c1
);
21735 /* All things being equal, if the next argument is a pack expansion
21736 for one function but not for the other, prefer the
21737 non-variadic function. FIXME this is bogus; see c++/41958. */
21739 && args1
&& TREE_VALUE (args1
)
21740 && args2
&& TREE_VALUE (args2
))
21742 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21743 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21746 if (lose1
== lose2
)
21754 /* Determine which of two partial specializations of TMPL is more
21757 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21758 to the first partial specialization. The TREE_PURPOSE is the
21759 innermost set of template parameters for the partial
21760 specialization. PAT2 is similar, but for the second template.
21762 Return 1 if the first partial specialization is more specialized;
21763 -1 if the second is more specialized; 0 if neither is more
21766 See [temp.class.order] for information about determining which of
21767 two templates is more specialized. */
21770 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21774 bool any_deductions
= false;
21776 tree tmpl1
= TREE_VALUE (pat1
);
21777 tree tmpl2
= TREE_VALUE (pat2
);
21778 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21779 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21781 /* Just like what happens for functions, if we are ordering between
21782 different template specializations, we may encounter dependent
21783 types in the arguments, and we need our dependency check functions
21784 to behave correctly. */
21785 ++processing_template_decl
;
21786 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21790 any_deductions
= true;
21793 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21797 any_deductions
= true;
21799 --processing_template_decl
;
21801 /* If both deductions succeed, the partial ordering selects the more
21802 constrained template. */
21803 if (!winner
&& any_deductions
)
21804 return more_constrained (tmpl1
, tmpl2
);
21806 /* In the case of a tie where at least one of the templates
21807 has a parameter pack at the end, the template with the most
21808 non-packed parameters wins. */
21811 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21812 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21814 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21815 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21816 int len1
= TREE_VEC_LENGTH (args1
);
21817 int len2
= TREE_VEC_LENGTH (args2
);
21819 /* We don't count the pack expansion at the end. */
21820 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21822 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21827 else if (len1
< len2
)
21834 /* Return the template arguments that will produce the function signature
21835 DECL from the function template FN, with the explicit template
21836 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21837 also match. Return NULL_TREE if no satisfactory arguments could be
21841 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21843 int ntparms
= DECL_NTPARMS (fn
);
21844 tree targs
= make_tree_vec (ntparms
);
21845 tree decl_type
= TREE_TYPE (decl
);
21846 tree decl_arg_types
;
21848 unsigned int nargs
, ix
;
21851 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21853 /* Never do unification on the 'this' parameter. */
21854 decl_arg_types
= skip_artificial_parms_for (decl
,
21855 TYPE_ARG_TYPES (decl_type
));
21857 nargs
= list_length (decl_arg_types
);
21858 args
= XALLOCAVEC (tree
, nargs
);
21859 for (arg
= decl_arg_types
, ix
= 0;
21860 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21861 arg
= TREE_CHAIN (arg
), ++ix
)
21862 args
[ix
] = TREE_VALUE (arg
);
21864 if (fn_type_unification (fn
, explicit_args
, targs
,
21866 (check_rettype
|| DECL_CONV_FN_P (fn
)
21867 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21868 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21870 == error_mark_node
)
21876 /* Return the innermost template arguments that, when applied to a partial
21877 specialization SPEC_TMPL of TMPL, yield the ARGS.
21879 For example, suppose we have:
21881 template <class T, class U> struct S {};
21882 template <class T> struct S<T*, int> {};
21884 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21885 partial specialization and the ARGS will be {double*, int}. The resulting
21886 vector will be {double}, indicating that `T' is bound to `double'. */
21889 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21891 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21893 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21894 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21896 tree innermost_deduced_args
;
21898 innermost_deduced_args
= make_tree_vec (ntparms
);
21899 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21901 deduced_args
= copy_node (args
);
21902 SET_TMPL_ARGS_LEVEL (deduced_args
,
21903 TMPL_ARGS_DEPTH (deduced_args
),
21904 innermost_deduced_args
);
21907 deduced_args
= innermost_deduced_args
;
21909 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21911 if (unify (tparms
, deduced_args
,
21912 INNERMOST_TEMPLATE_ARGS (spec_args
),
21913 INNERMOST_TEMPLATE_ARGS (args
),
21914 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
21917 for (i
= 0; i
< ntparms
; ++i
)
21918 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
21920 if (!tried_array_deduction
)
21922 try_array_deduction (tparms
, innermost_deduced_args
,
21923 INNERMOST_TEMPLATE_ARGS (spec_args
));
21924 tried_array_deduction
= true;
21925 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
21931 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
21932 if (!push_tinst_level (tinst
))
21934 excessive_deduction_depth
= true;
21938 /* Verify that nondeduced template arguments agree with the type
21939 obtained from argument deduction.
21943 struct A { typedef int X; };
21944 template <class T, class U> struct C {};
21945 template <class T> struct C<T, typename T::X> {};
21947 Then with the instantiation `C<A, int>', we can deduce that
21948 `T' is `A' but unify () does not check whether `typename T::X'
21950 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
21952 if (spec_args
!= error_mark_node
)
21953 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
21954 INNERMOST_TEMPLATE_ARGS (spec_args
),
21955 tmpl
, tf_none
, false, false);
21957 pop_tinst_level ();
21959 if (spec_args
== error_mark_node
21960 /* We only need to check the innermost arguments; the other
21961 arguments will always agree. */
21962 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
21963 INNERMOST_TEMPLATE_ARGS (args
)))
21966 /* Now that we have bindings for all of the template arguments,
21967 ensure that the arguments deduced for the template template
21968 parameters have compatible template parameter lists. See the use
21969 of template_template_parm_bindings_ok_p in fn_type_unification
21970 for more information. */
21971 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
21974 return deduced_args
;
21977 // Compare two function templates T1 and T2 by deducing bindings
21978 // from one against the other. If both deductions succeed, compare
21979 // constraints to see which is more constrained.
21981 more_specialized_inst (tree t1
, tree t2
)
21986 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
21992 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
21998 // If both deductions succeed, then one may be more constrained.
21999 if (count
== 2 && fate
== 0)
22000 fate
= more_constrained (t1
, t2
);
22005 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22006 Return the TREE_LIST node with the most specialized template, if
22007 any. If there is no most specialized template, the error_mark_node
22010 Note that this function does not look at, or modify, the
22011 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22012 returned is one of the elements of INSTANTIATIONS, callers may
22013 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22014 and retrieve it from the value returned. */
22017 most_specialized_instantiation (tree templates
)
22021 ++processing_template_decl
;
22024 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22026 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22027 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22032 /* Equally specialized, move to next function. If there
22033 is no next function, nothing's most specialized. */
22034 fn
= TREE_CHAIN (fn
);
22042 /* Now verify that champ is better than everything earlier in the
22043 instantiation list. */
22044 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22045 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22052 processing_template_decl
--;
22055 return error_mark_node
;
22060 /* If DECL is a specialization of some template, return the most
22061 general such template. Otherwise, returns NULL_TREE.
22063 For example, given:
22065 template <class T> struct S { template <class U> void f(U); };
22067 if TMPL is `template <class U> void S<int>::f(U)' this will return
22068 the full template. This function will not trace past partial
22069 specializations, however. For example, given in addition:
22071 template <class T> struct S<T*> { template <class U> void f(U); };
22073 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22074 `template <class T> template <class U> S<T*>::f(U)'. */
22077 most_general_template (tree decl
)
22079 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22081 if (tree tinfo
= get_template_info (decl
))
22082 decl
= TI_TEMPLATE (tinfo
);
22083 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22084 template friend, or a FIELD_DECL for a capture pack. */
22085 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22089 /* Look for more and more general templates. */
22090 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22092 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22093 (See cp-tree.h for details.) */
22094 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22097 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22098 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22099 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22102 /* Stop if we run into an explicitly specialized class template. */
22103 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22104 && DECL_CONTEXT (decl
)
22105 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22108 decl
= DECL_TI_TEMPLATE (decl
);
22114 /* Return the most specialized of the template partial specializations
22115 which can produce TARGET, a specialization of some class or variable
22116 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22117 a TEMPLATE_DECL node corresponding to the partial specialization, while
22118 the TREE_PURPOSE is the set of template arguments that must be
22119 substituted into the template pattern in order to generate TARGET.
22121 If the choice of partial specialization is ambiguous, a diagnostic
22122 is issued, and the error_mark_node is returned. If there are no
22123 partial specializations matching TARGET, then NULL_TREE is
22124 returned, indicating that the primary template should be used. */
22127 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22129 tree list
= NULL_TREE
;
22134 tree outer_args
= NULL_TREE
;
22137 if (TYPE_P (target
))
22139 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22140 tmpl
= TI_TEMPLATE (tinfo
);
22141 args
= TI_ARGS (tinfo
);
22143 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22145 tmpl
= TREE_OPERAND (target
, 0);
22146 args
= TREE_OPERAND (target
, 1);
22148 else if (VAR_P (target
))
22150 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22151 tmpl
= TI_TEMPLATE (tinfo
);
22152 args
= TI_ARGS (tinfo
);
22155 gcc_unreachable ();
22157 tree main_tmpl
= most_general_template (tmpl
);
22159 /* For determining which partial specialization to use, only the
22160 innermost args are interesting. */
22161 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22163 outer_args
= strip_innermost_template_args (args
, 1);
22164 args
= INNERMOST_TEMPLATE_ARGS (args
);
22167 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22170 tree spec_tmpl
= TREE_VALUE (t
);
22174 /* Substitute in the template args from the enclosing class. */
22175 ++processing_template_decl
;
22176 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22177 --processing_template_decl
;
22180 if (spec_tmpl
== error_mark_node
)
22181 return error_mark_node
;
22183 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22187 spec_args
= add_to_template_args (outer_args
, spec_args
);
22189 /* Keep the candidate only if the constraints are satisfied,
22190 or if we're not compiling with concepts. */
22192 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22194 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22195 TREE_TYPE (list
) = TREE_TYPE (t
);
22203 ambiguous_p
= false;
22206 t
= TREE_CHAIN (t
);
22207 for (; t
; t
= TREE_CHAIN (t
))
22209 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22216 t
= TREE_CHAIN (t
);
22219 ambiguous_p
= true;
22228 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22230 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22233 ambiguous_p
= true;
22241 char *spaces
= NULL
;
22242 if (!(complain
& tf_error
))
22243 return error_mark_node
;
22244 if (TYPE_P (target
))
22245 error ("ambiguous template instantiation for %q#T", target
);
22247 error ("ambiguous template instantiation for %q#D", target
);
22248 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22249 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22251 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22252 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22253 "%s %#qS", spaces
? spaces
: str
, subst
);
22254 spaces
= spaces
? spaces
: get_spaces (str
);
22257 return error_mark_node
;
22263 /* Explicitly instantiate DECL. */
22266 do_decl_instantiation (tree decl
, tree storage
)
22268 tree result
= NULL_TREE
;
22271 if (!decl
|| decl
== error_mark_node
)
22272 /* An error occurred, for which grokdeclarator has already issued
22273 an appropriate message. */
22275 else if (! DECL_LANG_SPECIFIC (decl
))
22277 error ("explicit instantiation of non-template %q#D", decl
);
22281 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22282 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22284 if (VAR_P (decl
) && !var_templ
)
22286 /* There is an asymmetry here in the way VAR_DECLs and
22287 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22288 the latter, the DECL we get back will be marked as a
22289 template instantiation, and the appropriate
22290 DECL_TEMPLATE_INFO will be set up. This does not happen for
22291 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22292 should handle VAR_DECLs as it currently handles
22294 if (!DECL_CLASS_SCOPE_P (decl
))
22296 error ("%qD is not a static data member of a class template", decl
);
22299 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22300 if (!result
|| !VAR_P (result
))
22302 error ("no matching template for %qD found", decl
);
22305 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22307 error ("type %qT for explicit instantiation %qD does not match "
22308 "declared type %qT", TREE_TYPE (result
), decl
,
22313 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22315 error ("explicit instantiation of %q#D", decl
);
22321 /* Check for various error cases. Note that if the explicit
22322 instantiation is valid the RESULT will currently be marked as an
22323 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22324 until we get here. */
22326 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22328 /* DR 259 [temp.spec].
22330 Both an explicit instantiation and a declaration of an explicit
22331 specialization shall not appear in a program unless the explicit
22332 instantiation follows a declaration of the explicit specialization.
22334 For a given set of template parameters, if an explicit
22335 instantiation of a template appears after a declaration of an
22336 explicit specialization for that template, the explicit
22337 instantiation has no effect. */
22340 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22344 No program shall explicitly instantiate any template more
22347 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22348 the first instantiation was `extern' and the second is not,
22349 and EXTERN_P for the opposite case. */
22350 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22351 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22352 /* If an "extern" explicit instantiation follows an ordinary
22353 explicit instantiation, the template is instantiated. */
22357 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22359 error ("no matching template for %qD found", result
);
22362 else if (!DECL_TEMPLATE_INFO (result
))
22364 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22368 if (storage
== NULL_TREE
)
22370 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22372 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22373 pedwarn (input_location
, OPT_Wpedantic
,
22374 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22379 error ("storage class %qD applied to template instantiation", storage
);
22381 check_explicit_instantiation_namespace (result
);
22382 mark_decl_instantiated (result
, extern_p
);
22384 instantiate_decl (result
, /*defer_ok=*/true,
22385 /*expl_inst_class_mem_p=*/false);
22389 mark_class_instantiated (tree t
, int extern_p
)
22391 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22392 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22393 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22394 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22397 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22398 rest_of_type_compilation (t
, 1);
22402 /* Called from do_type_instantiation through binding_table_foreach to
22403 do recursive instantiation for the type bound in ENTRY. */
22405 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22407 tree storage
= *(tree
*) data
;
22409 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22410 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22411 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22414 /* Perform an explicit instantiation of template class T. STORAGE, if
22415 non-null, is the RID for extern, inline or static. COMPLAIN is
22416 nonzero if this is called from the parser, zero if called recursively,
22417 since the standard is unclear (as detailed below). */
22420 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22425 int previous_instantiation_extern_p
= 0;
22427 if (TREE_CODE (t
) == TYPE_DECL
)
22430 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22433 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22435 error ("explicit instantiation of non-class template %qD", tmpl
);
22437 error ("explicit instantiation of non-template type %qT", t
);
22443 if (!COMPLETE_TYPE_P (t
))
22445 if (complain
& tf_error
)
22446 error ("explicit instantiation of %q#T before definition of template",
22451 if (storage
!= NULL_TREE
)
22453 if (!in_system_header_at (input_location
))
22455 if (storage
== ridpointers
[(int) RID_EXTERN
])
22457 if (cxx_dialect
== cxx98
)
22458 pedwarn (input_location
, OPT_Wpedantic
,
22459 "ISO C++ 1998 forbids the use of %<extern%> on "
22460 "explicit instantiations");
22463 pedwarn (input_location
, OPT_Wpedantic
,
22464 "ISO C++ forbids the use of %qE"
22465 " on explicit instantiations", storage
);
22468 if (storage
== ridpointers
[(int) RID_INLINE
])
22470 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22472 else if (storage
== ridpointers
[(int) RID_STATIC
])
22476 error ("storage class %qD applied to template instantiation",
22482 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22484 /* DR 259 [temp.spec].
22486 Both an explicit instantiation and a declaration of an explicit
22487 specialization shall not appear in a program unless the explicit
22488 instantiation follows a declaration of the explicit specialization.
22490 For a given set of template parameters, if an explicit
22491 instantiation of a template appears after a declaration of an
22492 explicit specialization for that template, the explicit
22493 instantiation has no effect. */
22496 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22500 No program shall explicitly instantiate any template more
22503 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22504 instantiation was `extern'. If EXTERN_P then the second is.
22505 These cases are OK. */
22506 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22508 if (!previous_instantiation_extern_p
&& !extern_p
22509 && (complain
& tf_error
))
22510 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22512 /* If we've already instantiated the template, just return now. */
22513 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22517 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22518 mark_class_instantiated (t
, extern_p
);
22523 /* In contrast to implicit instantiation, where only the
22524 declarations, and not the definitions, of members are
22525 instantiated, we have here:
22529 The explicit instantiation of a class template specialization
22530 implies the instantiation of all of its members not
22531 previously explicitly specialized in the translation unit
22532 containing the explicit instantiation.
22534 Of course, we can't instantiate member template classes, since we
22535 don't have any arguments for them. Note that the standard is
22536 unclear on whether the instantiation of the members are
22537 *explicit* instantiations or not. However, the most natural
22538 interpretation is that it should be an explicit
22540 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22542 || (TREE_CODE (fld
) == FUNCTION_DECL
22544 && user_provided_p (fld
)))
22545 && DECL_TEMPLATE_INSTANTIATION (fld
))
22547 mark_decl_instantiated (fld
, extern_p
);
22549 instantiate_decl (fld
, /*defer_ok=*/true,
22550 /*expl_inst_class_mem_p=*/true);
22553 if (CLASSTYPE_NESTED_UTDS (t
))
22554 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22555 bt_instantiate_type_proc
, &storage
);
22558 /* Given a function DECL, which is a specialization of TMPL, modify
22559 DECL to be a re-instantiation of TMPL with the same template
22560 arguments. TMPL should be the template into which tsubst'ing
22561 should occur for DECL, not the most general template.
22563 One reason for doing this is a scenario like this:
22566 void f(const T&, int i);
22568 void g() { f(3, 7); }
22571 void f(const T& t, const int i) { }
22573 Note that when the template is first instantiated, with
22574 instantiate_template, the resulting DECL will have no name for the
22575 first parameter, and the wrong type for the second. So, when we go
22576 to instantiate the DECL, we regenerate it. */
22579 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22581 /* The arguments used to instantiate DECL, from the most general
22585 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22587 /* Make sure that we can see identifiers, and compute access
22589 push_access_scope (decl
);
22591 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22599 args_depth
= TMPL_ARGS_DEPTH (args
);
22600 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22601 if (args_depth
> parms_depth
)
22602 args
= get_innermost_template_args (args
, parms_depth
);
22604 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22605 args
, tf_error
, NULL_TREE
,
22606 /*defer_ok*/false);
22607 if (specs
&& specs
!= error_mark_node
)
22608 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22611 /* Merge parameter declarations. */
22612 decl_parm
= skip_artificial_parms_for (decl
,
22613 DECL_ARGUMENTS (decl
));
22615 = skip_artificial_parms_for (code_pattern
,
22616 DECL_ARGUMENTS (code_pattern
));
22617 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22622 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22623 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22624 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22626 parm_type
= type_decays_to (parm_type
);
22627 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22628 TREE_TYPE (decl_parm
) = parm_type
;
22629 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22630 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22632 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22633 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22635 decl_parm
= DECL_CHAIN (decl_parm
);
22636 pattern_parm
= DECL_CHAIN (pattern_parm
);
22638 /* Merge any parameters that match with the function parameter
22640 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22643 tree expanded_types
;
22644 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22645 the parameters in this function parameter pack. */
22646 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22647 args
, tf_error
, NULL_TREE
);
22648 len
= TREE_VEC_LENGTH (expanded_types
);
22649 for (i
= 0; i
< len
; i
++)
22654 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22655 /* Rename the parameter to include the index. */
22656 DECL_NAME (decl_parm
) =
22657 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22658 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22659 parm_type
= type_decays_to (parm_type
);
22660 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22661 TREE_TYPE (decl_parm
) = parm_type
;
22662 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22663 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22665 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22666 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22668 decl_parm
= DECL_CHAIN (decl_parm
);
22671 /* Merge additional specifiers from the CODE_PATTERN. */
22672 if (DECL_DECLARED_INLINE_P (code_pattern
)
22673 && !DECL_DECLARED_INLINE_P (decl
))
22674 DECL_DECLARED_INLINE_P (decl
) = 1;
22676 else if (VAR_P (decl
))
22678 start_lambda_scope (decl
);
22679 DECL_INITIAL (decl
) =
22680 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22681 tf_error
, DECL_TI_TEMPLATE (decl
),
22682 /*integral_constant_expression_p=*/false);
22683 finish_lambda_scope ();
22684 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22685 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22686 tf_error
, DECL_TI_TEMPLATE (decl
));
22689 gcc_unreachable ();
22691 pop_access_scope (decl
);
22694 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22695 substituted to get DECL. */
22698 template_for_substitution (tree decl
)
22700 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22702 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22703 for the instantiation. This is not always the most general
22704 template. Consider, for example:
22707 struct S { template <class U> void f();
22708 template <> void f<int>(); };
22710 and an instantiation of S<double>::f<int>. We want TD to be the
22711 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22712 while (/* An instantiation cannot have a definition, so we need a
22713 more general template. */
22714 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22715 /* We must also deal with friend templates. Given:
22717 template <class T> struct S {
22718 template <class U> friend void f() {};
22721 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22722 so far as the language is concerned, but that's still
22723 where we get the pattern for the instantiation from. On
22724 other hand, if the definition comes outside the class, say:
22726 template <class T> struct S {
22727 template <class U> friend void f();
22729 template <class U> friend void f() {}
22731 we don't need to look any further. That's what the check for
22732 DECL_INITIAL is for. */
22733 || (TREE_CODE (decl
) == FUNCTION_DECL
22734 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22735 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22737 /* The present template, TD, should not be a definition. If it
22738 were a definition, we should be using it! Note that we
22739 cannot restructure the loop to just keep going until we find
22740 a template with a definition, since that might go too far if
22741 a specialization was declared, but not defined. */
22743 /* Fetch the more general template. */
22744 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22750 /* Returns true if we need to instantiate this template instance even if we
22751 know we aren't going to emit it. */
22754 always_instantiate_p (tree decl
)
22756 /* We always instantiate inline functions so that we can inline them. An
22757 explicit instantiation declaration prohibits implicit instantiation of
22758 non-inline functions. With high levels of optimization, we would
22759 normally inline non-inline functions -- but we're not allowed to do
22760 that for "extern template" functions. Therefore, we check
22761 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22762 return ((TREE_CODE (decl
) == FUNCTION_DECL
22763 && (DECL_DECLARED_INLINE_P (decl
)
22764 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22765 /* And we need to instantiate static data members so that
22766 their initializers are available in integral constant
22769 && decl_maybe_constant_var_p (decl
)));
22772 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22773 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22774 error, true otherwise. */
22777 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22779 tree fntype
, spec
, noex
, clone
;
22781 /* Don't instantiate a noexcept-specification from template context. */
22782 if (processing_template_decl
)
22785 if (DECL_CLONED_FUNCTION_P (fn
))
22786 fn
= DECL_CLONED_FUNCTION (fn
);
22787 fntype
= TREE_TYPE (fn
);
22788 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22790 if (!spec
|| !TREE_PURPOSE (spec
))
22793 noex
= TREE_PURPOSE (spec
);
22795 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22797 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22798 bool added
= false;
22799 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22800 spec
= get_defaulted_eh_spec (fn
, complain
);
22801 else if (!(added
= !fns
->add (fn
)))
22803 /* If hash_set::add returns true, the element was already there. */
22804 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22805 DECL_SOURCE_LOCATION (fn
));
22807 "exception specification of %qD depends on itself",
22809 spec
= noexcept_false_spec
;
22811 else if (push_tinst_level (fn
))
22813 push_access_scope (fn
);
22814 push_deferring_access_checks (dk_no_deferred
);
22815 input_location
= DECL_SOURCE_LOCATION (fn
);
22816 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22817 DEFERRED_NOEXCEPT_ARGS (noex
),
22818 tf_warning_or_error
, fn
,
22819 /*function_p=*/false,
22820 /*integral_constant_expression_p=*/true);
22821 pop_deferring_access_checks ();
22822 pop_access_scope (fn
);
22823 pop_tinst_level ();
22824 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22825 if (spec
== error_mark_node
)
22826 spec
= noexcept_false_spec
;
22829 spec
= noexcept_false_spec
;
22834 if (spec
== error_mark_node
)
22837 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22840 FOR_EACH_CLONE (clone
, fn
)
22842 if (TREE_TYPE (clone
) == fntype
)
22843 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22845 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22851 /* We're starting to process the function INST, an instantiation of PATTERN;
22852 add their parameters to local_specializations. */
22855 register_parameter_specializations (tree pattern
, tree inst
)
22857 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
22858 tree spec_parm
= DECL_ARGUMENTS (inst
);
22859 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
22861 register_local_specialization (spec_parm
, tmpl_parm
);
22862 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
22863 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
22865 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22867 if (!DECL_PACK_P (tmpl_parm
))
22869 register_local_specialization (spec_parm
, tmpl_parm
);
22870 spec_parm
= DECL_CHAIN (spec_parm
);
22874 /* Register the (value) argument pack as a specialization of
22875 TMPL_PARM, then move on. */
22876 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22877 register_local_specialization (argpack
, tmpl_parm
);
22880 gcc_assert (!spec_parm
);
22883 /* Produce the definition of D, a _DECL generated from a template. If
22884 DEFER_OK is true, then we don't have to actually do the
22885 instantiation now; we just have to do it sometime. Normally it is
22886 an error if this is an explicit instantiation but D is undefined.
22887 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22888 instantiated class template. */
22891 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
22893 tree tmpl
= DECL_TI_TEMPLATE (d
);
22900 bool pattern_defined
;
22901 location_t saved_loc
= input_location
;
22902 int saved_unevaluated_operand
= cp_unevaluated_operand
;
22903 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22907 /* This function should only be used to instantiate templates for
22908 functions and static member variables. */
22909 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
22911 /* A concept is never instantiated. */
22912 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
22914 /* Variables are never deferred; if instantiation is required, they
22915 are instantiated right away. That allows for better code in the
22916 case that an expression refers to the value of the variable --
22917 if the variable has a constant value the referring expression can
22918 take advantage of that fact. */
22922 /* Don't instantiate cloned functions. Instead, instantiate the
22923 functions they cloned. */
22924 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
22925 d
= DECL_CLONED_FUNCTION (d
);
22927 if (DECL_TEMPLATE_INSTANTIATED (d
)
22928 || (TREE_CODE (d
) == FUNCTION_DECL
22929 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
22930 || DECL_TEMPLATE_SPECIALIZATION (d
))
22931 /* D has already been instantiated or explicitly specialized, so
22932 there's nothing for us to do here.
22934 It might seem reasonable to check whether or not D is an explicit
22935 instantiation, and, if so, stop here. But when an explicit
22936 instantiation is deferred until the end of the compilation,
22937 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22938 the instantiation. */
22941 /* Check to see whether we know that this template will be
22942 instantiated in some other file, as with "extern template"
22944 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
22946 /* In general, we do not instantiate such templates. */
22947 if (external_p
&& !always_instantiate_p (d
))
22950 gen_tmpl
= most_general_template (tmpl
);
22951 gen_args
= DECL_TI_ARGS (d
);
22953 if (tmpl
!= gen_tmpl
)
22954 /* We should already have the extra args. */
22955 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
22956 == TMPL_ARGS_DEPTH (gen_args
));
22957 /* And what's in the hash table should match D. */
22958 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
22959 || spec
== NULL_TREE
);
22961 /* This needs to happen before any tsubsting. */
22962 if (! push_tinst_level (d
))
22965 timevar_push (TV_TEMPLATE_INST
);
22967 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22968 for the instantiation. */
22969 td
= template_for_substitution (d
);
22974 /* Look up an explicit specialization, if any. */
22975 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
22976 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
22977 if (elt
&& elt
!= error_mark_node
)
22979 td
= TREE_VALUE (elt
);
22980 args
= TREE_PURPOSE (elt
);
22984 code_pattern
= DECL_TEMPLATE_RESULT (td
);
22986 /* We should never be trying to instantiate a member of a class
22987 template or partial specialization. */
22988 gcc_assert (d
!= code_pattern
);
22990 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
22991 || DECL_TEMPLATE_SPECIALIZATION (td
))
22992 /* In the case of a friend template whose definition is provided
22993 outside the class, we may have too many arguments. Drop the
22994 ones we don't need. The same is true for specializations. */
22995 args
= get_innermost_template_args
22996 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
22998 if (TREE_CODE (d
) == FUNCTION_DECL
)
23000 deleted_p
= DECL_DELETED_FN (code_pattern
);
23001 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23002 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23003 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23009 if (DECL_CLASS_SCOPE_P (code_pattern
))
23010 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23011 || DECL_INLINE_VAR_P (code_pattern
));
23013 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23016 /* We may be in the middle of deferred access check. Disable it now. */
23017 push_deferring_access_checks (dk_no_deferred
);
23019 /* Unless an explicit instantiation directive has already determined
23020 the linkage of D, remember that a definition is available for
23022 if (pattern_defined
23023 && !DECL_INTERFACE_KNOWN (d
)
23024 && !DECL_NOT_REALLY_EXTERN (d
))
23025 mark_definable (d
);
23027 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23028 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23029 input_location
= DECL_SOURCE_LOCATION (d
);
23031 /* If D is a member of an explicitly instantiated class template,
23032 and no definition is available, treat it like an implicit
23034 if (!pattern_defined
&& expl_inst_class_mem_p
23035 && DECL_EXPLICIT_INSTANTIATION (d
))
23037 /* Leave linkage flags alone on instantiations with anonymous
23039 if (TREE_PUBLIC (d
))
23041 DECL_NOT_REALLY_EXTERN (d
) = 0;
23042 DECL_INTERFACE_KNOWN (d
) = 0;
23044 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23047 /* Defer all other templates, unless we have been explicitly
23048 forbidden from doing so. */
23049 if (/* If there is no definition, we cannot instantiate the
23052 /* If it's OK to postpone instantiation, do so. */
23054 /* If this is a static data member that will be defined
23055 elsewhere, we don't want to instantiate the entire data
23056 member, but we do want to instantiate the initializer so that
23057 we can substitute that elsewhere. */
23058 || (external_p
&& VAR_P (d
))
23059 /* Handle here a deleted function too, avoid generating
23060 its body (c++/61080). */
23063 /* The definition of the static data member is now required so
23064 we must substitute the initializer. */
23066 && !DECL_INITIAL (d
)
23067 && DECL_INITIAL (code_pattern
))
23071 bool const_init
= false;
23072 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23074 ns
= decl_namespace_context (d
);
23075 push_nested_namespace (ns
);
23077 push_nested_class (DECL_CONTEXT (d
));
23078 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23080 tf_warning_or_error
, NULL_TREE
,
23081 /*integral_constant_expression_p=*/false);
23082 /* If instantiating the initializer involved instantiating this
23083 again, don't call cp_finish_decl twice. */
23084 if (!DECL_INITIAL (d
))
23086 /* Make sure the initializer is still constant, in case of
23087 circular dependency (template/instantiate6.C). */
23089 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23090 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23091 /*asmspec_tree=*/NULL_TREE
,
23092 LOOKUP_ONLYCONVERTING
);
23095 pop_nested_class ();
23096 pop_nested_namespace (ns
);
23099 /* We restore the source position here because it's used by
23100 add_pending_template. */
23101 input_location
= saved_loc
;
23103 if (at_eof
&& !pattern_defined
23104 && DECL_EXPLICIT_INSTANTIATION (d
)
23105 && DECL_NOT_REALLY_EXTERN (d
))
23108 The definition of a non-exported function template, a
23109 non-exported member function template, or a non-exported
23110 member function or static data member of a class template
23111 shall be present in every translation unit in which it is
23112 explicitly instantiated. */
23113 permerror (input_location
, "explicit instantiation of %qD "
23114 "but no definition available", d
);
23116 /* If we're in unevaluated context, we just wanted to get the
23117 constant value; this isn't an odr use, so don't queue
23118 a full instantiation. */
23119 if (cp_unevaluated_operand
!= 0)
23121 /* ??? Historically, we have instantiated inline functions, even
23122 when marked as "extern template". */
23123 if (!(external_p
&& VAR_P (d
)))
23124 add_pending_template (d
);
23127 /* Tell the repository that D is available in this translation unit
23128 -- and see if it is supposed to be instantiated here. */
23129 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23131 /* In a PCH file, despite the fact that the repository hasn't
23132 requested instantiation in the PCH it is still possible that
23133 an instantiation will be required in a file that includes the
23136 add_pending_template (d
);
23137 /* Instantiate inline functions so that the inliner can do its
23138 job, even though we'll not be emitting a copy of this
23140 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23144 bool push_to_top
, nested
;
23146 fn_context
= decl_function_context (d
);
23147 nested
= current_function_decl
!= NULL_TREE
;
23148 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23150 vec
<tree
> omp_privatization_save
;
23152 save_omp_privatization_clauses (omp_privatization_save
);
23155 push_to_top_level ();
23158 push_function_context ();
23159 cp_unevaluated_operand
= 0;
23160 c_inhibit_evaluation_warnings
= 0;
23163 /* Mark D as instantiated so that recursive calls to
23164 instantiate_decl do not try to instantiate it again. */
23165 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23167 /* Regenerate the declaration in case the template has been modified
23168 by a subsequent redeclaration. */
23169 regenerate_decl_from_template (d
, td
, args
);
23171 /* We already set the file and line above. Reset them now in case
23172 they changed as a result of calling regenerate_decl_from_template. */
23173 input_location
= DECL_SOURCE_LOCATION (d
);
23178 bool const_init
= false;
23180 /* Clear out DECL_RTL; whatever was there before may not be right
23181 since we've reset the type of the declaration. */
23182 SET_DECL_RTL (d
, NULL
);
23183 DECL_IN_AGGR_P (d
) = 0;
23185 /* The initializer is placed in DECL_INITIAL by
23186 regenerate_decl_from_template so we don't need to
23187 push/pop_access_scope again here. Pull it out so that
23188 cp_finish_decl can process it. */
23189 init
= DECL_INITIAL (d
);
23190 DECL_INITIAL (d
) = NULL_TREE
;
23191 DECL_INITIALIZED_P (d
) = 0;
23193 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23194 initializer. That function will defer actual emission until
23195 we have a chance to determine linkage. */
23196 DECL_EXTERNAL (d
) = 0;
23198 /* Enter the scope of D so that access-checking works correctly. */
23199 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23201 push_nested_class (DECL_CONTEXT (d
));
23203 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23204 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23207 pop_nested_class ();
23209 if (variable_template_p (gen_tmpl
))
23210 note_variable_template_instantiation (d
);
23212 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23213 synthesize_method (d
);
23214 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23216 /* Set up the list of local specializations. */
23217 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23218 tree block
= NULL_TREE
;
23220 /* Set up context. */
23221 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23222 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23223 block
= push_stmt_list ();
23225 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23227 /* Some typedefs referenced from within the template code need to be
23228 access checked at template instantiation time, i.e now. These
23229 types were added to the template at parsing time. Let's get those
23230 and perform the access checks then. */
23231 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23234 /* Create substitution entries for the parameters. */
23235 register_parameter_specializations (code_pattern
, d
);
23237 /* Substitute into the body of the function. */
23238 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23239 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23240 tf_warning_or_error
, tmpl
);
23243 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23244 tf_warning_or_error
, tmpl
,
23245 /*integral_constant_expression_p=*/false);
23247 /* Set the current input_location to the end of the function
23248 so that finish_function knows where we are. */
23250 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23252 /* Remember if we saw an infinite loop in the template. */
23253 current_function_infinite_loop
23254 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23257 /* Finish the function. */
23258 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23259 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23260 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23263 d
= finish_function (/*inline_p=*/false);
23264 expand_or_defer_fn (d
);
23267 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23268 cp_check_omp_declare_reduction (d
);
23271 /* We're not deferring instantiation any more. */
23272 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23275 pop_from_top_level ();
23277 pop_function_context ();
23280 restore_omp_privatization_clauses (omp_privatization_save
);
23283 pop_deferring_access_checks ();
23284 timevar_pop (TV_TEMPLATE_INST
);
23285 pop_tinst_level ();
23286 input_location
= saved_loc
;
23287 cp_unevaluated_operand
= saved_unevaluated_operand
;
23288 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23293 /* Run through the list of templates that we wish we could
23294 instantiate, and instantiate any we can. RETRIES is the
23295 number of times we retry pending template instantiation. */
23298 instantiate_pending_templates (int retries
)
23301 location_t saved_loc
= input_location
;
23303 /* Instantiating templates may trigger vtable generation. This in turn
23304 may require further template instantiations. We place a limit here
23305 to avoid infinite loop. */
23306 if (pending_templates
&& retries
>= max_tinst_depth
)
23308 tree decl
= pending_templates
->tinst
->decl
;
23310 fatal_error (input_location
,
23311 "template instantiation depth exceeds maximum of %d"
23312 " instantiating %q+D, possibly from virtual table generation"
23313 " (use -ftemplate-depth= to increase the maximum)",
23314 max_tinst_depth
, decl
);
23315 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23316 /* Pretend that we defined it. */
23317 DECL_INITIAL (decl
) = error_mark_node
;
23323 struct pending_template
**t
= &pending_templates
;
23324 struct pending_template
*last
= NULL
;
23328 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23329 bool complete
= false;
23331 if (TYPE_P (instantiation
))
23333 if (!COMPLETE_TYPE_P (instantiation
))
23335 instantiate_class_template (instantiation
);
23336 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23337 for (tree fld
= TYPE_FIELDS (instantiation
);
23338 fld
; fld
= TREE_CHAIN (fld
))
23340 || (TREE_CODE (fld
) == FUNCTION_DECL
23341 && !DECL_ARTIFICIAL (fld
)))
23342 && DECL_TEMPLATE_INSTANTIATION (fld
))
23343 instantiate_decl (fld
,
23344 /*defer_ok=*/false,
23345 /*expl_inst_class_mem_p=*/false);
23347 if (COMPLETE_TYPE_P (instantiation
))
23351 complete
= COMPLETE_TYPE_P (instantiation
);
23355 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23356 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23359 = instantiate_decl (instantiation
,
23360 /*defer_ok=*/false,
23361 /*expl_inst_class_mem_p=*/false);
23362 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23366 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23367 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23371 /* If INSTANTIATION has been instantiated, then we don't
23372 need to consider it again in the future. */
23380 current_tinst_level
= NULL
;
23382 last_pending_template
= last
;
23384 while (reconsider
);
23386 input_location
= saved_loc
;
23389 /* Substitute ARGVEC into T, which is a list of initializers for
23390 either base class or a non-static data member. The TREE_PURPOSEs
23391 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23392 instantiate_decl. */
23395 tsubst_initializer_list (tree t
, tree argvec
)
23397 tree inits
= NULL_TREE
;
23399 for (; t
; t
= TREE_CHAIN (t
))
23403 tree expanded_bases
= NULL_TREE
;
23404 tree expanded_arguments
= NULL_TREE
;
23407 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23412 /* Expand the base class expansion type into separate base
23414 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23415 tf_warning_or_error
,
23417 if (expanded_bases
== error_mark_node
)
23420 /* We'll be building separate TREE_LISTs of arguments for
23422 len
= TREE_VEC_LENGTH (expanded_bases
);
23423 expanded_arguments
= make_tree_vec (len
);
23424 for (i
= 0; i
< len
; i
++)
23425 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23427 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23428 expand each argument in the TREE_VALUE of t. */
23429 expr
= make_node (EXPR_PACK_EXPANSION
);
23430 PACK_EXPANSION_LOCAL_P (expr
) = true;
23431 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23432 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23434 if (TREE_VALUE (t
) == void_type_node
)
23435 /* VOID_TYPE_NODE is used to indicate
23436 value-initialization. */
23438 for (i
= 0; i
< len
; i
++)
23439 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23443 /* Substitute parameter packs into each argument in the
23445 in_base_initializer
= 1;
23446 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23448 tree expanded_exprs
;
23450 /* Expand the argument. */
23451 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23453 = tsubst_pack_expansion (expr
, argvec
,
23454 tf_warning_or_error
,
23456 if (expanded_exprs
== error_mark_node
)
23459 /* Prepend each of the expanded expressions to the
23460 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23461 for (i
= 0; i
< len
; i
++)
23463 TREE_VEC_ELT (expanded_arguments
, i
) =
23464 tree_cons (NULL_TREE
,
23465 TREE_VEC_ELT (expanded_exprs
, i
),
23466 TREE_VEC_ELT (expanded_arguments
, i
));
23469 in_base_initializer
= 0;
23471 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23472 since we built them backwards. */
23473 for (i
= 0; i
< len
; i
++)
23475 TREE_VEC_ELT (expanded_arguments
, i
) =
23476 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23481 for (i
= 0; i
< len
; ++i
)
23483 if (expanded_bases
)
23485 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23486 decl
= expand_member_init (decl
);
23487 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23492 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23493 tf_warning_or_error
, NULL_TREE
);
23495 decl
= expand_member_init (decl
);
23496 if (decl
&& !DECL_P (decl
))
23497 in_base_initializer
= 1;
23499 init
= TREE_VALUE (t
);
23501 if (init
!= void_type_node
)
23502 init
= tsubst_expr (init
, argvec
,
23503 tf_warning_or_error
, NULL_TREE
,
23504 /*integral_constant_expression_p=*/false);
23505 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23506 /* If we had an initializer but it instantiated to nothing,
23507 value-initialize the object. This will only occur when
23508 the initializer was a pack expansion where the parameter
23509 packs used in that expansion were of length zero. */
23510 init
= void_type_node
;
23511 in_base_initializer
= 0;
23516 init
= build_tree_list (decl
, init
);
23517 TREE_CHAIN (init
) = inits
;
23525 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23528 set_current_access_from_decl (tree decl
)
23530 if (TREE_PRIVATE (decl
))
23531 current_access_specifier
= access_private_node
;
23532 else if (TREE_PROTECTED (decl
))
23533 current_access_specifier
= access_protected_node
;
23535 current_access_specifier
= access_public_node
;
23538 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23539 is the instantiation (which should have been created with
23540 start_enum) and ARGS are the template arguments to use. */
23543 tsubst_enum (tree tag
, tree newtag
, tree args
)
23547 if (SCOPED_ENUM_P (newtag
))
23548 begin_scope (sk_scoped_enum
, newtag
);
23550 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23555 decl
= TREE_VALUE (e
);
23556 /* Note that in a template enum, the TREE_VALUE is the
23557 CONST_DECL, not the corresponding INTEGER_CST. */
23558 value
= tsubst_expr (DECL_INITIAL (decl
),
23559 args
, tf_warning_or_error
, NULL_TREE
,
23560 /*integral_constant_expression_p=*/true);
23562 /* Give this enumeration constant the correct access. */
23563 set_current_access_from_decl (decl
);
23565 /* Actually build the enumerator itself. Here we're assuming that
23566 enumerators can't have dependent attributes. */
23567 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23568 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23571 if (SCOPED_ENUM_P (newtag
))
23574 finish_enum_value_list (newtag
);
23575 finish_enum (newtag
);
23577 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23578 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23581 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23582 its type -- but without substituting the innermost set of template
23583 arguments. So, innermost set of template parameters will appear in
23587 get_mostly_instantiated_function_type (tree decl
)
23589 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23590 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23593 /* Return truthvalue if we're processing a template different from
23594 the last one involved in diagnostics. */
23596 problematic_instantiation_changed (void)
23598 return current_tinst_level
!= last_error_tinst_level
;
23601 /* Remember current template involved in diagnostics. */
23603 record_last_problematic_instantiation (void)
23605 last_error_tinst_level
= current_tinst_level
;
23608 struct tinst_level
*
23609 current_instantiation (void)
23611 return current_tinst_level
;
23614 /* Return TRUE if current_function_decl is being instantiated, false
23618 instantiating_current_function_p (void)
23620 return (current_instantiation ()
23621 && current_instantiation ()->decl
== current_function_decl
);
23624 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23625 type. Return false for ok, true for disallowed. Issue error and
23626 inform messages under control of COMPLAIN. */
23629 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23631 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23633 else if (POINTER_TYPE_P (type
))
23635 else if (TYPE_PTRMEM_P (type
))
23637 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23639 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23641 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23643 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23645 /* A bound template template parm could later be instantiated to have a valid
23646 nontype parm type via an alias template. */
23647 else if (cxx_dialect
>= cxx11
23648 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23651 if (complain
& tf_error
)
23653 if (type
== error_mark_node
)
23654 inform (input_location
, "invalid template non-type parameter");
23656 error ("%q#T is not a valid type for a template non-type parameter",
23662 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23663 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23666 dependent_type_p_r (tree type
)
23672 A type is dependent if it is:
23674 -- a template parameter. Template template parameters are types
23675 for us (since TYPE_P holds true for them) so we handle
23677 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23678 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23680 /* -- a qualified-id with a nested-name-specifier which contains a
23681 class-name that names a dependent type or whose unqualified-id
23682 names a dependent type. */
23683 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23686 /* An alias template specialization can be dependent even if the
23687 resulting type is not. */
23688 if (dependent_alias_template_spec_p (type
))
23691 /* -- a cv-qualified type where the cv-unqualified type is
23693 No code is necessary for this bullet; the code below handles
23694 cv-qualified types, and we don't want to strip aliases with
23695 TYPE_MAIN_VARIANT because of DR 1558. */
23696 /* -- a compound type constructed from any dependent type. */
23697 if (TYPE_PTRMEM_P (type
))
23698 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23699 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23701 else if (TYPE_PTR_P (type
)
23702 || TREE_CODE (type
) == REFERENCE_TYPE
)
23703 return dependent_type_p (TREE_TYPE (type
));
23704 else if (TREE_CODE (type
) == FUNCTION_TYPE
23705 || TREE_CODE (type
) == METHOD_TYPE
)
23709 if (dependent_type_p (TREE_TYPE (type
)))
23711 for (arg_type
= TYPE_ARG_TYPES (type
);
23713 arg_type
= TREE_CHAIN (arg_type
))
23714 if (dependent_type_p (TREE_VALUE (arg_type
)))
23716 if (cxx_dialect
>= cxx17
)
23718 /* A value-dependent noexcept-specifier makes the type dependent. */
23719 tree spec
= TYPE_RAISES_EXCEPTIONS (type
);
23720 if (spec
&& TREE_PURPOSE (spec
)
23721 && value_dependent_expression_p (TREE_PURPOSE (spec
)))
23726 /* -- an array type constructed from any dependent type or whose
23727 size is specified by a constant expression that is
23730 We checked for type- and value-dependence of the bounds in
23731 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23732 if (TREE_CODE (type
) == ARRAY_TYPE
)
23734 if (TYPE_DOMAIN (type
)
23735 && dependent_type_p (TYPE_DOMAIN (type
)))
23737 return dependent_type_p (TREE_TYPE (type
));
23740 /* -- a template-id in which either the template name is a template
23742 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23744 /* ... or any of the template arguments is a dependent type or
23745 an expression that is type-dependent or value-dependent. */
23746 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23747 && (any_dependent_template_arguments_p
23748 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23751 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23752 dependent; if the argument of the `typeof' expression is not
23753 type-dependent, then it should already been have resolved. */
23754 if (TREE_CODE (type
) == TYPEOF_TYPE
23755 || TREE_CODE (type
) == DECLTYPE_TYPE
23756 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23759 /* A template argument pack is dependent if any of its packed
23761 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23763 tree args
= ARGUMENT_PACK_ARGS (type
);
23764 int i
, len
= TREE_VEC_LENGTH (args
);
23765 for (i
= 0; i
< len
; ++i
)
23766 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23770 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23771 be template parameters. */
23772 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23775 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23778 /* The standard does not specifically mention types that are local
23779 to template functions or local classes, but they should be
23780 considered dependent too. For example:
23782 template <int I> void f() {
23787 The size of `E' cannot be known until the value of `I' has been
23788 determined. Therefore, `E' must be considered dependent. */
23789 scope
= TYPE_CONTEXT (type
);
23790 if (scope
&& TYPE_P (scope
))
23791 return dependent_type_p (scope
);
23792 /* Don't use type_dependent_expression_p here, as it can lead
23793 to infinite recursion trying to determine whether a lambda
23794 nested in a lambda is dependent (c++/47687). */
23795 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23796 && DECL_LANG_SPECIFIC (scope
)
23797 && DECL_TEMPLATE_INFO (scope
)
23798 && (any_dependent_template_arguments_p
23799 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23802 /* Other types are non-dependent. */
23806 /* Returns TRUE if TYPE is dependent, in the sense of
23807 [temp.dep.type]. Note that a NULL type is considered dependent. */
23810 dependent_type_p (tree type
)
23812 /* If there are no template parameters in scope, then there can't be
23813 any dependent types. */
23814 if (!processing_template_decl
)
23816 /* If we are not processing a template, then nobody should be
23817 providing us with a dependent type. */
23819 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23823 /* If the type is NULL, we have not computed a type for the entity
23824 in question; in that case, the type is dependent. */
23828 /* Erroneous types can be considered non-dependent. */
23829 if (type
== error_mark_node
)
23832 /* Getting here with global_type_node means we improperly called this
23833 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23834 gcc_checking_assert (type
!= global_type_node
);
23836 /* If we have not already computed the appropriate value for TYPE,
23838 if (!TYPE_DEPENDENT_P_VALID (type
))
23840 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23841 TYPE_DEPENDENT_P_VALID (type
) = 1;
23844 return TYPE_DEPENDENT_P (type
);
23847 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23848 lookup. In other words, a dependent type that is not the current
23852 dependent_scope_p (tree scope
)
23854 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23855 && !currently_open_class (scope
));
23858 /* T is a SCOPE_REF; return whether we need to consider it
23859 instantiation-dependent so that we can check access at instantiation
23860 time even though we know which member it resolves to. */
23863 instantiation_dependent_scope_ref_p (tree t
)
23865 if (DECL_P (TREE_OPERAND (t
, 1))
23866 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23867 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23868 TREE_OPERAND (t
, 1)))
23874 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23875 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23878 /* Note that this predicate is not appropriate for general expressions;
23879 only constant expressions (that satisfy potential_constant_expression)
23880 can be tested for value dependence. */
23883 value_dependent_expression_p (tree expression
)
23885 if (!processing_template_decl
|| expression
== NULL_TREE
)
23888 /* A name declared with a dependent type. */
23889 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
23892 switch (TREE_CODE (expression
))
23895 /* A dependent member function of the current instantiation. */
23896 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
23898 case FUNCTION_DECL
:
23899 /* A dependent member function of the current instantiation. */
23900 if (DECL_CLASS_SCOPE_P (expression
)
23901 && dependent_type_p (DECL_CONTEXT (expression
)))
23905 case IDENTIFIER_NODE
:
23906 /* A name that has not been looked up -- must be dependent. */
23909 case TEMPLATE_PARM_INDEX
:
23910 /* A non-type template parm. */
23914 /* A non-type template parm. */
23915 if (DECL_TEMPLATE_PARM_P (expression
))
23917 return value_dependent_expression_p (DECL_INITIAL (expression
));
23920 /* A constant with literal type and is initialized
23921 with an expression that is value-dependent.
23923 Note that a non-dependent parenthesized initializer will have
23924 already been replaced with its constant value, so if we see
23925 a TREE_LIST it must be dependent. */
23926 if (DECL_INITIAL (expression
)
23927 && decl_constant_var_p (expression
)
23928 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
23929 /* cp_finish_decl doesn't fold reference initializers. */
23930 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
23931 || type_dependent_expression_p (DECL_INITIAL (expression
))
23932 || value_dependent_expression_p (DECL_INITIAL (expression
))))
23934 if (DECL_HAS_VALUE_EXPR_P (expression
))
23936 tree value_expr
= DECL_VALUE_EXPR (expression
);
23937 if (type_dependent_expression_p (value_expr
))
23942 case DYNAMIC_CAST_EXPR
:
23943 case STATIC_CAST_EXPR
:
23944 case CONST_CAST_EXPR
:
23945 case REINTERPRET_CAST_EXPR
:
23947 /* These expressions are value-dependent if the type to which
23948 the cast occurs is dependent or the expression being casted
23949 is value-dependent. */
23951 tree type
= TREE_TYPE (expression
);
23953 if (dependent_type_p (type
))
23956 /* A functional cast has a list of operands. */
23957 expression
= TREE_OPERAND (expression
, 0);
23960 /* If there are no operands, it must be an expression such
23961 as "int()". This should not happen for aggregate types
23962 because it would form non-constant expressions. */
23963 gcc_assert (cxx_dialect
>= cxx11
23964 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
23969 if (TREE_CODE (expression
) == TREE_LIST
)
23970 return any_value_dependent_elements_p (expression
);
23972 return value_dependent_expression_p (expression
);
23976 if (SIZEOF_EXPR_TYPE_P (expression
))
23977 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
23981 /* A `sizeof' expression is value-dependent if the operand is
23982 type-dependent or is a pack expansion. */
23983 expression
= TREE_OPERAND (expression
, 0);
23984 if (PACK_EXPANSION_P (expression
))
23986 else if (TYPE_P (expression
))
23987 return dependent_type_p (expression
);
23988 return instantiation_dependent_uneval_expression_p (expression
);
23990 case AT_ENCODE_EXPR
:
23991 /* An 'encode' expression is value-dependent if the operand is
23993 expression
= TREE_OPERAND (expression
, 0);
23994 return dependent_type_p (expression
);
23996 case NOEXCEPT_EXPR
:
23997 expression
= TREE_OPERAND (expression
, 0);
23998 return instantiation_dependent_uneval_expression_p (expression
);
24001 /* All instantiation-dependent expressions should also be considered
24002 value-dependent. */
24003 return instantiation_dependent_scope_ref_p (expression
);
24005 case COMPONENT_REF
:
24006 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24007 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24009 case NONTYPE_ARGUMENT_PACK
:
24010 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24011 is value-dependent. */
24013 tree values
= ARGUMENT_PACK_ARGS (expression
);
24014 int i
, len
= TREE_VEC_LENGTH (values
);
24016 for (i
= 0; i
< len
; ++i
)
24017 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24025 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24027 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24033 if (TREE_CODE (type2
) != TREE_LIST
)
24034 return dependent_type_p (type2
);
24036 for (; type2
; type2
= TREE_CHAIN (type2
))
24037 if (dependent_type_p (TREE_VALUE (type2
)))
24044 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24045 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24048 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24049 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24053 tree op
= TREE_OPERAND (expression
, 0);
24054 return (value_dependent_expression_p (op
)
24055 || has_value_dependent_address (op
));
24058 case REQUIRES_EXPR
:
24059 /* Treat all requires-expressions as value-dependent so
24060 we don't try to fold them. */
24064 return dependent_type_p (TREE_OPERAND (expression
, 0));
24068 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24070 tree fn
= get_callee_fndecl (expression
);
24072 nargs
= call_expr_nargs (expression
);
24073 for (i
= 0; i
< nargs
; ++i
)
24075 tree op
= CALL_EXPR_ARG (expression
, i
);
24076 /* In a call to a constexpr member function, look through the
24077 implicit ADDR_EXPR on the object argument so that it doesn't
24078 cause the call to be considered value-dependent. We also
24079 look through it in potential_constant_expression. */
24080 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24081 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24082 && TREE_CODE (op
) == ADDR_EXPR
)
24083 op
= TREE_OPERAND (op
, 0);
24084 if (value_dependent_expression_p (op
))
24090 case TEMPLATE_ID_EXPR
:
24091 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24093 return type_dependent_expression_p (expression
)
24094 || variable_concept_p (TREE_OPERAND (expression
, 0));
24100 if (dependent_type_p (TREE_TYPE (expression
)))
24102 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24103 if (value_dependent_expression_p (val
))
24109 /* Treat a GNU statement expression as dependent to avoid crashing
24110 under instantiate_non_dependent_expr; it can't be constant. */
24114 /* A constant expression is value-dependent if any subexpression is
24115 value-dependent. */
24116 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24118 case tcc_reference
:
24120 case tcc_comparison
:
24122 case tcc_expression
:
24125 int i
, len
= cp_tree_operand_length (expression
);
24127 for (i
= 0; i
< len
; i
++)
24129 tree t
= TREE_OPERAND (expression
, i
);
24131 /* In some cases, some of the operands may be missing.
24132 (For example, in the case of PREDECREMENT_EXPR, the
24133 amount to increment by may be missing.) That doesn't
24134 make the expression dependent. */
24135 if (t
&& value_dependent_expression_p (t
))
24146 /* The expression is not value-dependent. */
24150 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24151 [temp.dep.expr]. Note that an expression with no type is
24152 considered dependent. Other parts of the compiler arrange for an
24153 expression with type-dependent subexpressions to have no type, so
24154 this function doesn't have to be fully recursive. */
24157 type_dependent_expression_p (tree expression
)
24159 if (!processing_template_decl
)
24162 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24165 /* An unresolved name is always dependent. */
24166 if (identifier_p (expression
)
24167 || TREE_CODE (expression
) == USING_DECL
24168 || TREE_CODE (expression
) == WILDCARD_DECL
)
24171 /* A fold expression is type-dependent. */
24172 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24173 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24174 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24175 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24178 /* Some expression forms are never type-dependent. */
24179 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24180 || TREE_CODE (expression
) == SIZEOF_EXPR
24181 || TREE_CODE (expression
) == ALIGNOF_EXPR
24182 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24183 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24184 || TREE_CODE (expression
) == TRAIT_EXPR
24185 || TREE_CODE (expression
) == TYPEID_EXPR
24186 || TREE_CODE (expression
) == DELETE_EXPR
24187 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24188 || TREE_CODE (expression
) == THROW_EXPR
24189 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24192 /* The types of these expressions depends only on the type to which
24193 the cast occurs. */
24194 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24195 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24196 || TREE_CODE (expression
) == CONST_CAST_EXPR
24197 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24198 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24199 || TREE_CODE (expression
) == CAST_EXPR
)
24200 return dependent_type_p (TREE_TYPE (expression
));
24202 /* The types of these expressions depends only on the type created
24203 by the expression. */
24204 if (TREE_CODE (expression
) == NEW_EXPR
24205 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24207 /* For NEW_EXPR tree nodes created inside a template, either
24208 the object type itself or a TREE_LIST may appear as the
24210 tree type
= TREE_OPERAND (expression
, 1);
24211 if (TREE_CODE (type
) == TREE_LIST
)
24212 /* This is an array type. We need to check array dimensions
24214 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24215 || value_dependent_expression_p
24216 (TREE_OPERAND (TREE_VALUE (type
), 1));
24218 return dependent_type_p (type
);
24221 if (TREE_CODE (expression
) == SCOPE_REF
)
24223 tree scope
= TREE_OPERAND (expression
, 0);
24224 tree name
= TREE_OPERAND (expression
, 1);
24226 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24227 contains an identifier associated by name lookup with one or more
24228 declarations declared with a dependent type, or...a
24229 nested-name-specifier or qualified-id that names a member of an
24230 unknown specialization. */
24231 return (type_dependent_expression_p (name
)
24232 || dependent_scope_p (scope
));
24235 if (TREE_CODE (expression
) == TEMPLATE_DECL
24236 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24237 return uses_outer_template_parms (expression
);
24239 if (TREE_CODE (expression
) == STMT_EXPR
)
24240 expression
= stmt_expr_value_expr (expression
);
24242 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24247 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24249 if (type_dependent_expression_p (elt
))
24255 /* A static data member of the current instantiation with incomplete
24256 array type is type-dependent, as the definition and specializations
24257 can have different bounds. */
24258 if (VAR_P (expression
)
24259 && DECL_CLASS_SCOPE_P (expression
)
24260 && dependent_type_p (DECL_CONTEXT (expression
))
24261 && VAR_HAD_UNKNOWN_BOUND (expression
))
24264 /* An array of unknown bound depending on a variadic parameter, eg:
24266 template<typename... Args>
24267 void foo (Args... args)
24269 int arr[] = { args... };
24272 template<int... vals>
24275 int arr[] = { vals... };
24278 If the array has no length and has an initializer, it must be that
24279 we couldn't determine its length in cp_complete_array_type because
24280 it is dependent. */
24281 if (VAR_P (expression
)
24282 && TREE_TYPE (expression
) != NULL_TREE
24283 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24284 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24285 && DECL_INITIAL (expression
))
24288 /* A function or variable template-id is type-dependent if it has any
24289 dependent template arguments. */
24290 if (VAR_OR_FUNCTION_DECL_P (expression
)
24291 && DECL_LANG_SPECIFIC (expression
)
24292 && DECL_TEMPLATE_INFO (expression
))
24294 /* Consider the innermost template arguments, since those are the ones
24295 that come from the template-id; the template arguments for the
24296 enclosing class do not make it type-dependent unless they are used in
24297 the type of the decl. */
24298 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24299 && (any_dependent_template_arguments_p
24300 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24304 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24305 type-dependent. Checking this is important for functions with auto return
24306 type, which looks like a dependent type. */
24307 if (TREE_CODE (expression
) == FUNCTION_DECL
24308 && !(DECL_CLASS_SCOPE_P (expression
)
24309 && dependent_type_p (DECL_CONTEXT (expression
)))
24310 && !(DECL_FRIEND_P (expression
)
24311 && (!DECL_FRIEND_CONTEXT (expression
)
24312 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24313 && !DECL_LOCAL_FUNCTION_P (expression
))
24315 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24316 || undeduced_auto_decl (expression
));
24320 /* Always dependent, on the number of arguments if nothing else. */
24321 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24324 if (TREE_TYPE (expression
) == unknown_type_node
)
24326 if (TREE_CODE (expression
) == ADDR_EXPR
)
24327 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24328 if (TREE_CODE (expression
) == COMPONENT_REF
24329 || TREE_CODE (expression
) == OFFSET_REF
)
24331 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24333 expression
= TREE_OPERAND (expression
, 1);
24334 if (identifier_p (expression
))
24337 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24338 if (TREE_CODE (expression
) == SCOPE_REF
)
24341 if (BASELINK_P (expression
))
24343 if (BASELINK_OPTYPE (expression
)
24344 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24346 expression
= BASELINK_FUNCTIONS (expression
);
24349 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24351 if (any_dependent_template_arguments_p
24352 (TREE_OPERAND (expression
, 1)))
24354 expression
= TREE_OPERAND (expression
, 0);
24355 if (identifier_p (expression
))
24359 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24360 || TREE_CODE (expression
) == FUNCTION_DECL
);
24362 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24363 if (type_dependent_expression_p (*iter
))
24369 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24371 /* Dependent type attributes might not have made it from the decl to
24373 if (DECL_P (expression
)
24374 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24377 return (dependent_type_p (TREE_TYPE (expression
)));
24380 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24381 type-dependent if the expression refers to a member of the current
24382 instantiation and the type of the referenced member is dependent, or the
24383 class member access expression refers to a member of an unknown
24386 This function returns true if the OBJECT in such a class member access
24387 expression is of an unknown specialization. */
24390 type_dependent_object_expression_p (tree object
)
24392 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24394 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24396 tree scope
= TREE_TYPE (object
);
24397 return (!scope
|| dependent_scope_p (scope
));
24400 /* walk_tree callback function for instantiation_dependent_expression_p,
24401 below. Returns non-zero if a dependent subexpression is found. */
24404 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24409 /* We don't have to worry about decltype currently because decltype
24410 of an instantiation-dependent expr is a dependent type. This
24411 might change depending on the resolution of DR 1172. */
24412 *walk_subtrees
= false;
24415 enum tree_code code
= TREE_CODE (*tp
);
24418 /* Don't treat an argument list as dependent just because it has no
24424 case TEMPLATE_PARM_INDEX
:
24427 /* Handle expressions with type operands. */
24431 case AT_ENCODE_EXPR
:
24433 tree op
= TREE_OPERAND (*tp
, 0);
24434 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24435 op
= TREE_TYPE (op
);
24438 if (dependent_type_p (op
))
24442 *walk_subtrees
= false;
24449 case COMPONENT_REF
:
24450 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24451 /* In a template, finish_class_member_access_expr creates a
24452 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24453 type-dependent, so that we can check access control at
24454 instantiation time (PR 42277). See also Core issue 1273. */
24459 if (instantiation_dependent_scope_ref_p (*tp
))
24464 /* Treat statement-expressions as dependent. */
24468 /* Treat requires-expressions as dependent. */
24469 case REQUIRES_EXPR
:
24473 /* Treat calls to function concepts as dependent. */
24474 if (function_concept_check_p (*tp
))
24478 case TEMPLATE_ID_EXPR
:
24479 /* And variable concepts. */
24480 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24488 if (type_dependent_expression_p (*tp
))
24494 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24495 sense defined by the ABI:
24497 "An expression is instantiation-dependent if it is type-dependent
24498 or value-dependent, or it has a subexpression that is type-dependent
24499 or value-dependent."
24501 Except don't actually check value-dependence for unevaluated expressions,
24502 because in sizeof(i) we don't care about the value of i. Checking
24503 type-dependence will in turn check value-dependence of array bounds/template
24504 arguments as needed. */
24507 instantiation_dependent_uneval_expression_p (tree expression
)
24511 if (!processing_template_decl
)
24514 if (expression
== error_mark_node
)
24517 result
= cp_walk_tree_without_duplicates (&expression
,
24518 instantiation_dependent_r
, NULL
);
24519 return result
!= NULL_TREE
;
24522 /* As above, but also check value-dependence of the expression as a whole. */
24525 instantiation_dependent_expression_p (tree expression
)
24527 return (instantiation_dependent_uneval_expression_p (expression
)
24528 || value_dependent_expression_p (expression
));
24531 /* Like type_dependent_expression_p, but it also works while not processing
24532 a template definition, i.e. during substitution or mangling. */
24535 type_dependent_expression_p_push (tree expr
)
24538 ++processing_template_decl
;
24539 b
= type_dependent_expression_p (expr
);
24540 --processing_template_decl
;
24544 /* Returns TRUE if ARGS contains a type-dependent expression. */
24547 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24552 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24554 if (type_dependent_expression_p (arg
))
24560 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24561 expressions) contains any type-dependent expressions. */
24564 any_type_dependent_elements_p (const_tree list
)
24566 for (; list
; list
= TREE_CHAIN (list
))
24567 if (type_dependent_expression_p (TREE_VALUE (list
)))
24573 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24574 expressions) contains any value-dependent expressions. */
24577 any_value_dependent_elements_p (const_tree list
)
24579 for (; list
; list
= TREE_CHAIN (list
))
24580 if (value_dependent_expression_p (TREE_VALUE (list
)))
24586 /* Returns TRUE if the ARG (a template argument) is dependent. */
24589 dependent_template_arg_p (tree arg
)
24591 if (!processing_template_decl
)
24594 /* Assume a template argument that was wrongly written by the user
24595 is dependent. This is consistent with what
24596 any_dependent_template_arguments_p [that calls this function]
24598 if (!arg
|| arg
== error_mark_node
)
24601 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24602 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24604 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24606 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24608 if (DECL_TEMPLATE_PARM_P (arg
))
24610 /* A member template of a dependent class is not necessarily
24611 type-dependent, but it is a dependent template argument because it
24612 will be a member of an unknown specialization to that template. */
24613 tree scope
= CP_DECL_CONTEXT (arg
);
24614 return TYPE_P (scope
) && dependent_type_p (scope
);
24616 else if (ARGUMENT_PACK_P (arg
))
24618 tree args
= ARGUMENT_PACK_ARGS (arg
);
24619 int i
, len
= TREE_VEC_LENGTH (args
);
24620 for (i
= 0; i
< len
; ++i
)
24622 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24628 else if (TYPE_P (arg
))
24629 return dependent_type_p (arg
);
24631 return (type_dependent_expression_p (arg
)
24632 || value_dependent_expression_p (arg
));
24635 /* Returns true if ARGS (a collection of template arguments) contains
24636 any types that require structural equality testing. */
24639 any_template_arguments_need_structural_equality_p (tree args
)
24646 if (args
== error_mark_node
)
24649 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24651 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24652 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24654 tree arg
= TREE_VEC_ELT (level
, j
);
24655 tree packed_args
= NULL_TREE
;
24658 if (ARGUMENT_PACK_P (arg
))
24660 /* Look inside the argument pack. */
24661 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24662 len
= TREE_VEC_LENGTH (packed_args
);
24665 for (k
= 0; k
< len
; ++k
)
24668 arg
= TREE_VEC_ELT (packed_args
, k
);
24670 if (error_operand_p (arg
))
24672 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24674 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24676 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24677 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24686 /* Returns true if ARGS (a collection of template arguments) contains
24687 any dependent arguments. */
24690 any_dependent_template_arguments_p (const_tree args
)
24697 if (args
== error_mark_node
)
24700 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24702 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24703 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24704 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24711 /* Returns TRUE if the template TMPL is type-dependent. */
24714 dependent_template_p (tree tmpl
)
24716 if (TREE_CODE (tmpl
) == OVERLOAD
)
24718 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24719 if (dependent_template_p (*iter
))
24724 /* Template template parameters are dependent. */
24725 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24726 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24728 /* So are names that have not been looked up. */
24729 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24734 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24737 dependent_template_id_p (tree tmpl
, tree args
)
24739 return (dependent_template_p (tmpl
)
24740 || any_dependent_template_arguments_p (args
));
24743 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24747 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24751 if (!processing_template_decl
)
24754 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24756 tree decl
= TREE_VEC_ELT (declv
, i
);
24757 tree init
= TREE_VEC_ELT (initv
, i
);
24758 tree cond
= TREE_VEC_ELT (condv
, i
);
24759 tree incr
= TREE_VEC_ELT (incrv
, i
);
24761 if (type_dependent_expression_p (decl
)
24762 || TREE_CODE (decl
) == SCOPE_REF
)
24765 if (init
&& type_dependent_expression_p (init
))
24768 if (type_dependent_expression_p (cond
))
24771 if (COMPARISON_CLASS_P (cond
)
24772 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24773 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24776 if (TREE_CODE (incr
) == MODOP_EXPR
)
24778 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24779 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24782 else if (type_dependent_expression_p (incr
))
24784 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24786 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24788 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24790 tree t
= TREE_OPERAND (incr
, 1);
24791 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24792 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24801 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24802 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24803 no such TYPE can be found. Note that this function peers inside
24804 uninstantiated templates and therefore should be used only in
24805 extremely limited situations. ONLY_CURRENT_P restricts this
24806 peering to the currently open classes hierarchy (which is required
24807 when comparing types). */
24810 resolve_typename_type (tree type
, bool only_current_p
)
24819 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24821 scope
= TYPE_CONTEXT (type
);
24822 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24823 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24824 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24825 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24826 identifier of the TYPENAME_TYPE anymore.
24827 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24828 TYPENAME_TYPE instead, we avoid messing up with a possible
24829 typedef variant case. */
24830 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24832 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24833 it first before we can figure out what NAME refers to. */
24834 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24836 if (TYPENAME_IS_RESOLVING_P (scope
))
24837 /* Given a class template A with a dependent base with nested type C,
24838 typedef typename A::C::C C will land us here, as trying to resolve
24839 the initial A::C leads to the local C typedef, which leads back to
24840 A::C::C. So we break the recursion now. */
24843 scope
= resolve_typename_type (scope
, only_current_p
);
24845 /* If we don't know what SCOPE refers to, then we cannot resolve the
24847 if (!CLASS_TYPE_P (scope
))
24849 /* If this is a typedef, we don't want to look inside (c++/11987). */
24850 if (typedef_variant_p (type
))
24852 /* If SCOPE isn't the template itself, it will not have a valid
24853 TYPE_FIELDS list. */
24854 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24855 /* scope is either the template itself or a compatible instantiation
24856 like X<T>, so look up the name in the original template. */
24857 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24858 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24859 gcc_checking_assert (uses_template_parms (scope
));
24860 /* If scope has no fields, it can't be a current instantiation. Check this
24861 before currently_open_class to avoid infinite recursion (71515). */
24862 if (!TYPE_FIELDS (scope
))
24864 /* If the SCOPE is not the current instantiation, there's no reason
24865 to look inside it. */
24866 if (only_current_p
&& !currently_open_class (scope
))
24868 /* Enter the SCOPE so that name lookup will be resolved as if we
24869 were in the class definition. In particular, SCOPE will no
24870 longer be considered a dependent type. */
24871 pushed_scope
= push_scope (scope
);
24872 /* Look up the declaration. */
24873 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24874 tf_warning_or_error
);
24876 result
= NULL_TREE
;
24878 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24879 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24880 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
24883 else if (identifier_p (fullname
)
24884 && TREE_CODE (decl
) == TYPE_DECL
)
24886 result
= TREE_TYPE (decl
);
24887 if (result
== error_mark_node
)
24888 result
= NULL_TREE
;
24890 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
24891 && DECL_CLASS_TEMPLATE_P (decl
))
24893 /* Obtain the template and the arguments. */
24894 tree tmpl
= TREE_OPERAND (fullname
, 0);
24895 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
24897 /* We get here with a plain identifier because a previous tentative
24898 parse of the nested-name-specifier as part of a ptr-operator saw
24899 ::template X<A>. The use of ::template is necessary in a
24900 ptr-operator, but wrong in a declarator-id.
24902 [temp.names]: In a qualified-id of a declarator-id, the keyword
24903 template shall not appear at the top level. */
24904 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
24905 "keyword %<template%> not allowed in declarator-id");
24908 tree args
= TREE_OPERAND (fullname
, 1);
24909 /* Instantiate the template. */
24910 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
24911 /*entering_scope=*/true,
24912 tf_error
| tf_user
);
24913 if (result
== error_mark_node
)
24914 result
= NULL_TREE
;
24917 /* Leave the SCOPE. */
24919 pop_scope (pushed_scope
);
24921 /* If we failed to resolve it, return the original typename. */
24925 /* If lookup found a typename type, resolve that too. */
24926 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
24928 /* Ill-formed programs can cause infinite recursion here, so we
24929 must catch that. */
24930 TYPENAME_IS_RESOLVING_P (result
) = 1;
24931 result
= resolve_typename_type (result
, only_current_p
);
24932 TYPENAME_IS_RESOLVING_P (result
) = 0;
24935 /* Qualify the resulting type. */
24936 quals
= cp_type_quals (type
);
24938 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
24943 /* EXPR is an expression which is not type-dependent. Return a proxy
24944 for EXPR that can be used to compute the types of larger
24945 expressions containing EXPR. */
24948 build_non_dependent_expr (tree expr
)
24952 /* When checking, try to get a constant value for all non-dependent
24953 expressions in order to expose bugs in *_dependent_expression_p
24954 and constexpr. This can affect code generation, see PR70704, so
24955 only do this for -fchecking=2. */
24956 if (flag_checking
> 1
24957 && cxx_dialect
>= cxx11
24958 /* Don't do this during nsdmi parsing as it can lead to
24959 unexpected recursive instantiations. */
24960 && !parsing_nsdmi ()
24961 /* Don't do this during concept expansion either and for
24962 the same reason. */
24963 && !expanding_concept ())
24964 fold_non_dependent_expr (expr
);
24966 /* Preserve OVERLOADs; the functions must be available to resolve
24969 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
24970 inner_expr
= stmt_expr_value_expr (inner_expr
);
24971 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
24972 inner_expr
= TREE_OPERAND (inner_expr
, 0);
24973 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
24974 inner_expr
= TREE_OPERAND (inner_expr
, 1);
24975 if (is_overloaded_fn (inner_expr
)
24976 || TREE_CODE (inner_expr
) == OFFSET_REF
)
24978 /* There is no need to return a proxy for a variable. */
24981 /* Preserve string constants; conversions from string constants to
24982 "char *" are allowed, even though normally a "const char *"
24983 cannot be used to initialize a "char *". */
24984 if (TREE_CODE (expr
) == STRING_CST
)
24986 /* Preserve void and arithmetic constants, as an optimization -- there is no
24987 reason to create a new node. */
24988 if (TREE_CODE (expr
) == VOID_CST
24989 || TREE_CODE (expr
) == INTEGER_CST
24990 || TREE_CODE (expr
) == REAL_CST
)
24992 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24993 There is at least one place where we want to know that a
24994 particular expression is a throw-expression: when checking a ?:
24995 expression, there are special rules if the second or third
24996 argument is a throw-expression. */
24997 if (TREE_CODE (expr
) == THROW_EXPR
)
25000 /* Don't wrap an initializer list, we need to be able to look inside. */
25001 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25004 /* Don't wrap a dummy object, we need to be able to test for it. */
25005 if (is_dummy_object (expr
))
25008 if (TREE_CODE (expr
) == COND_EXPR
)
25009 return build3 (COND_EXPR
,
25011 TREE_OPERAND (expr
, 0),
25012 (TREE_OPERAND (expr
, 1)
25013 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25014 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25015 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25016 if (TREE_CODE (expr
) == COMPOUND_EXPR
25017 && !COMPOUND_EXPR_OVERLOADED (expr
))
25018 return build2 (COMPOUND_EXPR
,
25020 TREE_OPERAND (expr
, 0),
25021 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25023 /* If the type is unknown, it can't really be non-dependent */
25024 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25026 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25027 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25030 /* ARGS is a vector of expressions as arguments to a function call.
25031 Replace the arguments with equivalent non-dependent expressions.
25032 This modifies ARGS in place. */
25035 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25040 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25042 tree newarg
= build_non_dependent_expr (arg
);
25044 (*args
)[ix
] = newarg
;
25048 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25049 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25050 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25053 make_auto_1 (tree name
, bool set_canonical
)
25055 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25056 TYPE_NAME (au
) = build_decl (input_location
,
25057 TYPE_DECL
, name
, au
);
25058 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25059 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25060 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25061 TYPE_NAME (au
), NULL_TREE
);
25063 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25064 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25065 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25071 make_decltype_auto (void)
25073 return make_auto_1 (decltype_auto_identifier
, true);
25079 return make_auto_1 (auto_identifier
, true);
25082 /* Return a C++17 deduction placeholder for class template TMPL. */
25085 make_template_placeholder (tree tmpl
)
25087 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25088 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25092 /* True iff T is a C++17 class template deduction placeholder. */
25095 template_placeholder_p (tree t
)
25097 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25100 /* Make a "constrained auto" type-specifier. This is an
25101 auto type with constraints that must be associated after
25102 deduction. The constraint is formed from the given
25103 CONC and its optional sequence of arguments, which are
25104 non-null if written as partial-concept-id. */
25107 make_constrained_auto (tree con
, tree args
)
25109 tree type
= make_auto_1 (auto_identifier
, false);
25111 /* Build the constraint. */
25112 tree tmpl
= DECL_TI_TEMPLATE (con
);
25113 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25114 expr
= build_concept_check (expr
, type
, args
);
25116 tree constr
= normalize_expression (expr
);
25117 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25119 /* Our canonical type depends on the constraint. */
25120 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25122 /* Attach the constraint to the type declaration. */
25123 tree decl
= TYPE_NAME (type
);
25127 /* Given type ARG, return std::initializer_list<ARG>. */
25132 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25134 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25136 gcc_rich_location
richloc (input_location
);
25137 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25138 error_at (&richloc
,
25139 "deducing from brace-enclosed initializer list"
25140 " requires %<#include <initializer_list>%>");
25142 return error_mark_node
;
25144 tree argvec
= make_tree_vec (1);
25145 TREE_VEC_ELT (argvec
, 0) = arg
;
25147 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25148 NULL_TREE
, 0, tf_warning_or_error
);
25151 /* Replace auto in TYPE with std::initializer_list<auto>. */
25154 listify_autos (tree type
, tree auto_node
)
25156 tree init_auto
= listify (auto_node
);
25157 tree argvec
= make_tree_vec (1);
25158 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25159 if (processing_template_decl
)
25160 argvec
= add_to_template_args (current_template_args (), argvec
);
25161 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25164 /* Hash traits for hashing possibly constrained 'auto'
25165 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25167 struct auto_hash
: default_hash_traits
<tree
>
25169 static inline hashval_t
hash (tree
);
25170 static inline bool equal (tree
, tree
);
25173 /* Hash the 'auto' T. */
25176 auto_hash::hash (tree t
)
25178 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25179 /* Matching constrained-type-specifiers denote the same template
25180 parameter, so hash the constraint. */
25181 return hash_placeholder_constraint (c
);
25183 /* But unconstrained autos are all separate, so just hash the pointer. */
25184 return iterative_hash_object (t
, 0);
25187 /* Compare two 'auto's. */
25190 auto_hash::equal (tree t1
, tree t2
)
25195 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25196 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25198 /* Two unconstrained autos are distinct. */
25202 return equivalent_placeholder_constraints (c1
, c2
);
25205 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25206 constrained) auto, add it to the vector. */
25209 extract_autos_r (tree t
, void *data
)
25211 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25214 /* All the autos were built with index 0; fix that up now. */
25215 tree
*p
= hash
.find_slot (t
, INSERT
);
25218 /* If this is a repeated constrained-type-specifier, use the index we
25220 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25223 /* Otherwise this is new, so use the current count. */
25225 idx
= hash
.elements () - 1;
25227 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25230 /* Always keep walking. */
25234 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25235 says they can appear anywhere in the type. */
25238 extract_autos (tree type
)
25240 hash_set
<tree
> visited
;
25241 hash_table
<auto_hash
> hash (2);
25243 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25245 tree tree_vec
= make_tree_vec (hash
.elements());
25246 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25247 iter
!= hash
.end(); ++iter
)
25250 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25251 TREE_VEC_ELT (tree_vec
, i
)
25252 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25258 /* The stem for deduction guide names. */
25259 const char *const dguide_base
= "__dguide_";
25261 /* Return the name for a deduction guide for class template TMPL. */
25264 dguide_name (tree tmpl
)
25266 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25267 tree tname
= TYPE_IDENTIFIER (type
);
25268 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25269 + IDENTIFIER_LENGTH (tname
));
25270 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25271 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25272 IDENTIFIER_LENGTH (tname
) + 1);
25273 tree dname
= get_identifier (buf
);
25274 TREE_TYPE (dname
) = type
;
25278 /* True if NAME is the name of a deduction guide. */
25281 dguide_name_p (tree name
)
25283 return (TREE_TYPE (name
)
25284 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25285 strlen (dguide_base
)));
25288 /* True if FN is a deduction guide. */
25291 deduction_guide_p (const_tree fn
)
25294 if (tree name
= DECL_NAME (fn
))
25295 return dguide_name_p (name
);
25299 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25302 copy_guide_p (const_tree fn
)
25304 gcc_assert (deduction_guide_p (fn
));
25305 if (!DECL_ARTIFICIAL (fn
))
25307 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25308 return (TREE_CHAIN (parms
) == void_list_node
25309 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25312 /* True if FN is a guide generated from a constructor template. */
25315 template_guide_p (const_tree fn
)
25317 gcc_assert (deduction_guide_p (fn
));
25318 if (!DECL_ARTIFICIAL (fn
))
25320 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25321 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25322 return PRIMARY_TEMPLATE_P (org
);
25326 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25327 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25328 template parameter types. Note that the handling of template template
25329 parameters relies on current_template_parms being set appropriately for the
25333 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25334 tree tsubst_args
, tsubst_flags_t complain
)
25336 tree oldidx
= get_template_parm_index (olddecl
);
25339 if (TREE_CODE (olddecl
) == TYPE_DECL
25340 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25342 tree oldtype
= TREE_TYPE (olddecl
);
25343 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25344 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25345 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25346 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25347 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25350 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25351 complain
, NULL_TREE
);
25354 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25355 DECL_NAME (olddecl
), newtype
);
25356 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25359 if (TREE_CODE (olddecl
) == TYPE_DECL
25360 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25362 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25363 = build_template_parm_index (index
, level
, level
,
25365 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25366 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25367 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25368 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25370 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25372 DECL_TEMPLATE_RESULT (newdecl
)
25373 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25374 DECL_NAME (olddecl
), newtype
);
25375 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25376 // First create a copy (ttargs) of tsubst_args with an
25377 // additional level for the template template parameter's own
25378 // template parameters (ttparms).
25379 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25380 (DECL_TEMPLATE_PARMS (olddecl
)));
25381 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25382 tree ttargs
= make_tree_vec (depth
+ 1);
25383 for (int i
= 0; i
< depth
; ++i
)
25384 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25385 TREE_VEC_ELT (ttargs
, depth
)
25386 = template_parms_level_to_args (ttparms
);
25387 // Substitute ttargs into ttparms to fix references to
25388 // other template parameters.
25389 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25391 // Now substitute again with args based on tparms, to reduce
25392 // the level of the ttparms.
25393 ttargs
= current_template_args ();
25394 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25396 // Finally, tack the adjusted parms onto tparms.
25397 ttparms
= tree_cons (size_int (depth
), ttparms
,
25398 current_template_parms
);
25399 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25404 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25406 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25407 TREE_CODE (oldconst
),
25408 DECL_NAME (oldconst
), newtype
);
25409 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25410 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25411 SET_DECL_TEMPLATE_PARM_P (newconst
);
25412 newidx
= build_template_parm_index (index
, level
, level
,
25413 newconst
, newtype
);
25414 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25415 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25416 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25422 /* Returns a C++17 class deduction guide template based on the constructor
25423 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25424 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25427 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25429 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25430 bool memtmpl
= false;
25433 tree fn_tmpl
= NULL_TREE
;
25438 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25441 type
= TREE_TYPE (type
);
25442 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25445 fparms
= void_list_node
;
25447 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25448 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25449 targs
= CLASSTYPE_TI_ARGS (type
);
25452 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25453 explicit_p
= false;
25457 ++processing_template_decl
;
25460 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25461 : DECL_TI_TEMPLATE (ctor
));
25463 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25464 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25466 type
= DECL_CONTEXT (ctor
);
25468 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25469 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25470 fully specialized args for the enclosing class. Strip those off, as
25471 the deduction guide won't have those template parameters. */
25472 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25473 TMPL_PARMS_DEPTH (tparms
));
25474 /* Discard the 'this' parameter. */
25475 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25476 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25477 ci
= get_constraints (ctor
);
25478 loc
= DECL_SOURCE_LOCATION (ctor
);
25479 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25481 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25485 /* For a member template constructor, we need to flatten the two
25486 template parameter lists into one, and then adjust the function
25487 signature accordingly. This gets...complicated. */
25488 tree save_parms
= current_template_parms
;
25490 /* For a member template we should have two levels of parms/args, one
25491 for the class and one for the constructor. We stripped
25492 specialized args for further enclosing classes above. */
25493 const int depth
= 2;
25494 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25496 /* Template args for translating references to the two-level template
25497 parameters into references to the one-level template parameters we
25499 tree tsubst_args
= copy_node (targs
);
25500 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25501 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25503 /* Template parms for the constructor template. */
25504 tree ftparms
= TREE_VALUE (tparms
);
25505 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25506 /* Template parms for the class template. */
25507 tparms
= TREE_CHAIN (tparms
);
25508 tree ctparms
= TREE_VALUE (tparms
);
25509 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25510 /* Template parms for the deduction guide start as a copy of the
25511 template parms for the class. We set current_template_parms for
25512 lookup_template_class_1. */
25513 current_template_parms
= tparms
= copy_node (tparms
);
25514 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25515 for (unsigned i
= 0; i
< clen
; ++i
)
25516 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25518 /* Now we need to rewrite the constructor parms to append them to the
25520 for (unsigned i
= 0; i
< flen
; ++i
)
25522 unsigned index
= i
+ clen
;
25523 unsigned level
= 1;
25524 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25525 tree olddecl
= TREE_VALUE (oldelt
);
25526 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25527 tsubst_args
, complain
);
25528 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25529 tsubst_args
, complain
, ctor
);
25530 tree list
= build_tree_list (newdef
, newdecl
);
25531 TEMPLATE_PARM_CONSTRAINTS (list
)
25532 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25533 tsubst_args
, complain
, ctor
);
25534 TREE_VEC_ELT (new_vec
, index
) = list
;
25535 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25538 /* Now we have a final set of template parms to substitute into the
25539 function signature. */
25540 targs
= template_parms_to_args (tparms
);
25541 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25543 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25545 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25547 current_template_parms
= save_parms
;
25549 --processing_template_decl
;
25554 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25555 tparms
= copy_node (tparms
);
25556 INNERMOST_TEMPLATE_PARMS (tparms
)
25557 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25560 tree fntype
= build_function_type (type
, fparms
);
25561 tree ded_fn
= build_lang_decl_loc (loc
,
25563 dguide_name (type
), fntype
);
25564 DECL_ARGUMENTS (ded_fn
) = fargs
;
25565 DECL_ARTIFICIAL (ded_fn
) = true;
25566 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25567 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25568 DECL_ARTIFICIAL (ded_tmpl
) = true;
25569 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25570 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25571 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25572 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25574 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25576 set_constraints (ded_tmpl
, ci
);
25581 /* Deduce template arguments for the class template placeholder PTYPE for
25582 template TMPL based on the initializer INIT, and return the resulting
25586 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25587 tsubst_flags_t complain
)
25589 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25591 /* We should have handled this in the caller. */
25592 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25594 if (complain
& tf_error
)
25595 error ("non-class template %qT used without template arguments", tmpl
);
25596 return error_mark_node
;
25599 tree type
= TREE_TYPE (tmpl
);
25601 bool try_list_ctor
= false;
25603 vec
<tree
,va_gc
> *args
;
25604 if (init
== NULL_TREE
25605 || TREE_CODE (init
) == TREE_LIST
)
25606 args
= make_tree_vector_from_list (init
);
25607 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25609 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25610 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25612 /* As an exception, the first phase in 16.3.1.7 (considering the
25613 initializer list as a single argument) is omitted if the
25614 initializer list consists of a single expression of type cv U,
25615 where U is a specialization of C or a class derived from a
25616 specialization of C. */
25617 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25618 tree etype
= TREE_TYPE (elt
);
25620 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25621 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25622 int err
= unify (tparms
, targs
, type
, etype
,
25623 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25625 try_list_ctor
= false;
25628 if (try_list_ctor
|| is_std_init_list (type
))
25629 args
= make_tree_vector_single (init
);
25631 args
= make_tree_vector_from_ctor (init
);
25634 args
= make_tree_vector_single (init
);
25636 tree dname
= dguide_name (tmpl
);
25637 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25638 /*type*/false, /*complain*/false,
25640 bool elided
= false;
25641 if (cands
== error_mark_node
)
25644 /* Prune explicit deduction guides in copy-initialization context. */
25645 if (flags
& LOOKUP_ONLYCONVERTING
)
25647 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25648 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25653 /* Found a nonconverting guide, prune the candidates. */
25654 tree pruned
= NULL_TREE
;
25655 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25656 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25657 pruned
= lookup_add (*iter
, pruned
);
25663 tree outer_args
= NULL_TREE
;
25664 if (DECL_CLASS_SCOPE_P (tmpl
)
25665 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25667 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25668 type
= TREE_TYPE (most_general_template (tmpl
));
25671 bool saw_ctor
= false;
25672 // FIXME cache artificial deduction guides
25673 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25675 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25676 if ((flags
& LOOKUP_ONLYCONVERTING
)
25677 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25680 cands
= lookup_add (guide
, cands
);
25685 tree call
= error_mark_node
;
25687 /* If this is list-initialization and the class has a list constructor, first
25688 try deducing from the list as a single argument, as [over.match.list]. */
25689 tree list_cands
= NULL_TREE
;
25690 if (try_list_ctor
&& cands
)
25691 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25694 if (is_list_ctor (dg
))
25695 list_cands
= lookup_add (dg
, list_cands
);
25699 ++cp_unevaluated_operand
;
25700 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25701 --cp_unevaluated_operand
;
25703 if (call
== error_mark_node
)
25705 /* That didn't work, now try treating the list as a sequence of
25707 release_tree_vector (args
);
25708 args
= make_tree_vector_from_ctor (init
);
25712 /* Maybe generate an implicit deduction guide. */
25713 if (call
== error_mark_node
&& args
->length () < 2)
25715 tree gtype
= NULL_TREE
;
25717 if (args
->length () == 1)
25718 /* Generate a copy guide. */
25719 gtype
= build_reference_type (type
);
25720 else if (!saw_ctor
)
25721 /* Generate a default guide. */
25726 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25727 cands
= lookup_add (guide
, cands
);
25731 if (elided
&& !cands
)
25733 error ("cannot deduce template arguments for copy-initialization"
25734 " of %qT, as it has no non-explicit deduction guides or "
25735 "user-declared constructors", type
);
25736 return error_mark_node
;
25738 else if (!cands
&& call
== error_mark_node
)
25740 error ("cannot deduce template arguments of %qT, as it has no viable "
25741 "deduction guides", type
);
25742 return error_mark_node
;
25745 if (call
== error_mark_node
)
25747 ++cp_unevaluated_operand
;
25748 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25749 --cp_unevaluated_operand
;
25752 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25754 error ("class template argument deduction failed:");
25756 ++cp_unevaluated_operand
;
25757 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25758 --cp_unevaluated_operand
;
25761 inform (input_location
, "explicit deduction guides not considered "
25762 "for copy-initialization");
25765 release_tree_vector (args
);
25767 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25770 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25771 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25774 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25776 return do_auto_deduction (type
, init
, auto_node
,
25777 tf_warning_or_error
,
25781 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25782 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25783 The CONTEXT determines the context in which auto deduction is performed
25784 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25785 OUTER_TARGS are used during template argument deduction
25786 (context == adc_unify) to properly substitute the result, and is ignored
25789 For partial-concept-ids, extra args may be appended to the list of deduced
25790 template arguments prior to determining constraint satisfaction. */
25793 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25794 tsubst_flags_t complain
, auto_deduction_context context
,
25795 tree outer_targs
, int flags
)
25799 if (init
== error_mark_node
)
25800 return error_mark_node
;
25802 if (init
&& type_dependent_expression_p (init
)
25803 && context
!= adc_unify
)
25804 /* Defining a subset of type-dependent expressions that we can deduce
25805 from ahead of time isn't worth the trouble. */
25808 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25809 /* C++17 class template argument deduction. */
25810 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25812 if (TREE_TYPE (init
) == NULL_TREE
)
25813 /* Nothing we can do with this, even in deduction context. */
25816 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25817 with either a new invented type template parameter U or, if the
25818 initializer is a braced-init-list (8.5.4), with
25819 std::initializer_list<U>. */
25820 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25822 if (!DIRECT_LIST_INIT_P (init
))
25823 type
= listify_autos (type
, auto_node
);
25824 else if (CONSTRUCTOR_NELTS (init
) == 1)
25825 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25828 if (complain
& tf_warning_or_error
)
25830 if (permerror (input_location
, "direct-list-initialization of "
25831 "%<auto%> requires exactly one element"))
25832 inform (input_location
,
25833 "for deduction to %<std::initializer_list%>, use copy-"
25834 "list-initialization (i.e. add %<=%> before the %<{%>)");
25836 type
= listify_autos (type
, auto_node
);
25840 if (type
== error_mark_node
)
25841 return error_mark_node
;
25843 init
= resolve_nondeduced_context (init
, complain
);
25845 if (context
== adc_decomp_type
25846 && auto_node
== type
25847 && init
!= error_mark_node
25848 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
25849 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25850 and initializer has array type, deduce cv-qualified array type. */
25851 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
25853 else if (AUTO_IS_DECLTYPE (auto_node
))
25855 bool id
= (DECL_P (init
)
25856 || ((TREE_CODE (init
) == COMPONENT_REF
25857 || TREE_CODE (init
) == SCOPE_REF
)
25858 && !REF_PARENTHESIZED_P (init
)));
25859 targs
= make_tree_vec (1);
25860 TREE_VEC_ELT (targs
, 0)
25861 = finish_decltype_type (init
, id
, tf_warning_or_error
);
25862 if (type
!= auto_node
)
25864 if (complain
& tf_error
)
25865 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
25866 return error_mark_node
;
25871 tree parms
= build_tree_list (NULL_TREE
, type
);
25875 tparms
= extract_autos (type
);
25878 tparms
= make_tree_vec (1);
25879 TREE_VEC_ELT (tparms
, 0)
25880 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
25883 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25884 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25885 DEDUCE_CALL
, LOOKUP_NORMAL
,
25886 NULL
, /*explain_p=*/false);
25889 if (processing_template_decl
)
25890 /* Try again at instantiation time. */
25892 if (type
&& type
!= error_mark_node
25893 && (complain
& tf_error
))
25894 /* If type is error_mark_node a diagnostic must have been
25895 emitted by now. Also, having a mention to '<type error>'
25896 in the diagnostic is not really useful to the user. */
25898 if (cfun
&& auto_node
== current_function_auto_return_pattern
25899 && LAMBDA_FUNCTION_P (current_function_decl
))
25900 error ("unable to deduce lambda return type from %qE", init
);
25902 error ("unable to deduce %qT from %qE", type
, init
);
25903 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25904 DEDUCE_CALL
, LOOKUP_NORMAL
,
25905 NULL
, /*explain_p=*/true);
25907 return error_mark_node
;
25911 /* Check any placeholder constraints against the deduced type. */
25912 if (flag_concepts
&& !processing_template_decl
)
25913 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
25915 /* Use the deduced type to check the associated constraints. If we
25916 have a partial-concept-id, rebuild the argument list so that
25917 we check using the extra arguments. */
25918 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
25919 tree cargs
= CHECK_CONSTR_ARGS (constr
);
25920 if (TREE_VEC_LENGTH (cargs
) > 1)
25922 cargs
= copy_node (cargs
);
25923 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
25927 if (!constraints_satisfied_p (constr
, cargs
))
25929 if (complain
& tf_warning_or_error
)
25933 case adc_unspecified
:
25935 error("placeholder constraints not satisfied");
25937 case adc_variable_type
:
25938 case adc_decomp_type
:
25939 error ("deduced initializer does not satisfy "
25940 "placeholder constraints");
25942 case adc_return_type
:
25943 error ("deduced return type does not satisfy "
25944 "placeholder constraints");
25946 case adc_requirement
:
25947 error ("deduced expression type does not satisfy "
25948 "placeholder constraints");
25951 diagnose_constraints (input_location
, constr
, targs
);
25953 return error_mark_node
;
25957 if (processing_template_decl
&& context
!= adc_unify
)
25958 outer_targs
= current_template_args ();
25959 targs
= add_to_template_args (outer_targs
, targs
);
25960 return tsubst (type
, targs
, complain
, NULL_TREE
);
25963 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25967 splice_late_return_type (tree type
, tree late_return_type
)
25969 if (is_auto (type
))
25971 if (late_return_type
)
25972 return late_return_type
;
25974 tree idx
= get_template_parm_index (type
);
25975 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
25976 /* In an abbreviated function template we didn't know we were dealing
25977 with a function template when we saw the auto return type, so update
25978 it to have the correct level. */
25979 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
25984 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25985 'decltype(auto)' or a deduced class template. */
25988 is_auto (const_tree type
)
25990 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25991 && (TYPE_IDENTIFIER (type
) == auto_identifier
25992 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
25993 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
25999 /* for_each_template_parm callback for type_uses_auto. */
26002 is_auto_r (tree tp
, void */
*data*/
)
26004 return is_auto (tp
);
26007 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26008 a use of `auto'. Returns NULL_TREE otherwise. */
26011 type_uses_auto (tree type
)
26013 if (type
== NULL_TREE
)
26015 else if (flag_concepts
)
26017 /* The Concepts TS allows multiple autos in one type-specifier; just
26018 return the first one we find, do_auto_deduction will collect all of
26020 if (uses_template_parms (type
))
26021 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26022 /*visited*/NULL
, /*nondeduced*/true);
26027 return find_type_usage (type
, is_auto
);
26030 /* For a given template T, return the vector of typedefs referenced
26031 in T for which access check is needed at T instantiation time.
26032 T is either a FUNCTION_DECL or a RECORD_TYPE.
26033 Those typedefs were added to T by the function
26034 append_type_to_template_for_access_check. */
26036 vec
<qualified_typedef_usage_t
, va_gc
> *
26037 get_types_needing_access_check (tree t
)
26040 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26042 if (!t
|| t
== error_mark_node
)
26045 if (!(ti
= get_template_info (t
)))
26048 if (CLASS_TYPE_P (t
)
26049 || TREE_CODE (t
) == FUNCTION_DECL
)
26051 if (!TI_TEMPLATE (ti
))
26054 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26060 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26061 tied to T. That list of typedefs will be access checked at
26062 T instantiation time.
26063 T is either a FUNCTION_DECL or a RECORD_TYPE.
26064 TYPE_DECL is a TYPE_DECL node representing a typedef.
26065 SCOPE is the scope through which TYPE_DECL is accessed.
26066 LOCATION is the location of the usage point of TYPE_DECL.
26068 This function is a subroutine of
26069 append_type_to_template_for_access_check. */
26072 append_type_to_template_for_access_check_1 (tree t
,
26075 location_t location
)
26077 qualified_typedef_usage_t typedef_usage
;
26080 if (!t
|| t
== error_mark_node
)
26083 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26084 || CLASS_TYPE_P (t
))
26086 && TREE_CODE (type_decl
) == TYPE_DECL
26089 if (!(ti
= get_template_info (t
)))
26092 gcc_assert (TI_TEMPLATE (ti
));
26094 typedef_usage
.typedef_decl
= type_decl
;
26095 typedef_usage
.context
= scope
;
26096 typedef_usage
.locus
= location
;
26098 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26101 /* Append TYPE_DECL to the template TEMPL.
26102 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26103 At TEMPL instanciation time, TYPE_DECL will be checked to see
26104 if it can be accessed through SCOPE.
26105 LOCATION is the location of the usage point of TYPE_DECL.
26107 e.g. consider the following code snippet:
26114 template<class U> struct S
26116 C::myint mi; // <-- usage point of the typedef C::myint
26121 At S<char> instantiation time, we need to check the access of C::myint
26122 In other words, we need to check the access of the myint typedef through
26123 the C scope. For that purpose, this function will add the myint typedef
26124 and the scope C through which its being accessed to a list of typedefs
26125 tied to the template S. That list will be walked at template instantiation
26126 time and access check performed on each typedefs it contains.
26127 Note that this particular code snippet should yield an error because
26128 myint is private to C. */
26131 append_type_to_template_for_access_check (tree templ
,
26134 location_t location
)
26136 qualified_typedef_usage_t
*iter
;
26139 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26141 /* Make sure we don't append the type to the template twice. */
26142 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26143 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26146 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26150 /* Convert the generic type parameters in PARM that match the types given in the
26151 range [START_IDX, END_IDX) from the current_template_parms into generic type
26155 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26157 tree current
= current_template_parms
;
26158 int depth
= TMPL_PARMS_DEPTH (current
);
26159 current
= INNERMOST_TEMPLATE_PARMS (current
);
26160 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26162 for (int i
= 0; i
< start_idx
; ++i
)
26163 TREE_VEC_ELT (replacement
, i
)
26164 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26166 for (int i
= start_idx
; i
< end_idx
; ++i
)
26168 /* Create a distinct parameter pack type from the current parm and add it
26169 to the replacement args to tsubst below into the generic function
26172 tree o
= TREE_TYPE (TREE_VALUE
26173 (TREE_VEC_ELT (current
, i
)));
26174 tree t
= copy_type (o
);
26175 TEMPLATE_TYPE_PARM_INDEX (t
)
26176 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26178 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26179 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26180 TYPE_MAIN_VARIANT (t
) = t
;
26181 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26182 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26183 TREE_VEC_ELT (replacement
, i
) = t
;
26184 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26187 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26188 TREE_VEC_ELT (replacement
, i
)
26189 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26191 /* If there are more levels then build up the replacement with the outer
26194 replacement
= add_to_template_args (template_parms_to_args
26195 (TREE_CHAIN (current_template_parms
)),
26198 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26201 /* Entries in the decl_constraint hash table. */
26202 struct GTY((for_user
)) constr_entry
26208 /* Hashing function and equality for constraint entries. */
26209 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26211 static hashval_t
hash (constr_entry
*e
)
26213 return (hashval_t
)DECL_UID (e
->decl
);
26216 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26218 return e1
->decl
== e2
->decl
;
26222 /* A mapping from declarations to constraint information. Note that
26223 both templates and their underlying declarations are mapped to the
26224 same constraint information.
26226 FIXME: This is defined in pt.c because garbage collection
26227 code is not being generated for constraint.cc. */
26229 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26231 /* Returns the template constraints of declaration T. If T is not
26232 constrained, return NULL_TREE. Note that T must be non-null. */
26235 get_constraints (tree t
)
26237 if (!flag_concepts
)
26240 gcc_assert (DECL_P (t
));
26241 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26242 t
= DECL_TEMPLATE_RESULT (t
);
26243 constr_entry elt
= { t
, NULL_TREE
};
26244 constr_entry
* found
= decl_constraints
->find (&elt
);
26251 /* Associate the given constraint information CI with the declaration
26252 T. If T is a template, then the constraints are associated with
26253 its underlying declaration. Don't build associations if CI is
26257 set_constraints (tree t
, tree ci
)
26261 gcc_assert (t
&& flag_concepts
);
26262 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26263 t
= DECL_TEMPLATE_RESULT (t
);
26264 gcc_assert (!get_constraints (t
));
26265 constr_entry elt
= {t
, ci
};
26266 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26267 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26272 /* Remove the associated constraints of the declaration T. */
26275 remove_constraints (tree t
)
26277 gcc_assert (DECL_P (t
));
26278 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26279 t
= DECL_TEMPLATE_RESULT (t
);
26281 constr_entry elt
= {t
, NULL_TREE
};
26282 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26284 decl_constraints
->clear_slot (slot
);
26287 /* Memoized satisfaction results for declarations. This
26288 maps the pair (constraint_info, arguments) to the result computed
26289 by constraints_satisfied_p. */
26291 struct GTY((for_user
)) constraint_sat_entry
26298 /* Hashing function and equality for constraint entries. */
26300 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26302 static hashval_t
hash (constraint_sat_entry
*e
)
26304 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26305 return iterative_hash_template_arg (e
->args
, val
);
26308 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26310 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26314 /* Memoized satisfaction results for concept checks. */
26316 struct GTY((for_user
)) concept_spec_entry
26323 /* Hashing function and equality for constraint entries. */
26325 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26327 static hashval_t
hash (concept_spec_entry
*e
)
26329 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26332 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26334 ++comparing_specializations
;
26335 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26336 --comparing_specializations
;
26341 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26342 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26344 /* Search for a memoized satisfaction result. Returns one of the
26345 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26348 lookup_constraint_satisfaction (tree ci
, tree args
)
26350 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26351 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26353 return found
->result
;
26358 /* Memoize the result of a satisfication test. Returns the saved result. */
26361 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26363 constraint_sat_entry elt
= {ci
, args
, result
};
26364 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26365 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26371 /* Search for a memoized satisfaction result for a concept. */
26374 lookup_concept_satisfaction (tree tmpl
, tree args
)
26376 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26377 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26379 return found
->result
;
26384 /* Memoize the result of a concept check. Returns the saved result. */
26387 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26389 concept_spec_entry elt
= {tmpl
, args
, result
};
26390 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26391 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26397 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26399 /* Returns a prior concept specialization. This returns the substituted
26400 and normalized constraints defined by the concept. */
26403 get_concept_expansion (tree tmpl
, tree args
)
26405 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26406 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26408 return found
->result
;
26413 /* Save a concept expansion for later. */
26416 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26418 concept_spec_entry elt
= {tmpl
, args
, def
};
26419 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26420 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26427 hash_subsumption_args (tree t1
, tree t2
)
26429 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26430 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26432 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26433 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26434 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26435 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26439 /* Compare the constraints of two subsumption entries. The LEFT1 and
26440 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26441 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26444 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26446 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26447 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26448 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26449 CHECK_CONSTR_ARGS (right1
)))
26450 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26451 CHECK_CONSTR_ARGS (right2
));
26455 /* Key/value pair for learning and memoizing subsumption results. This
26456 associates a pair of check constraints (including arguments) with
26457 a boolean value indicating the result. */
26459 struct GTY((for_user
)) subsumption_entry
26466 /* Hashing function and equality for constraint entries. */
26468 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26470 static hashval_t
hash (subsumption_entry
*e
)
26472 return hash_subsumption_args (e
->t1
, e
->t2
);
26475 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26477 ++comparing_specializations
;
26478 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26479 --comparing_specializations
;
26484 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26486 /* Search for a previously cached subsumption result. */
26489 lookup_subsumption_result (tree t1
, tree t2
)
26491 subsumption_entry elt
= { t1
, t2
, false };
26492 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26494 return &found
->result
;
26499 /* Save a subsumption result. */
26502 save_subsumption_result (tree t1
, tree t2
, bool result
)
26504 subsumption_entry elt
= {t1
, t2
, result
};
26505 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26506 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26512 /* Set up the hash table for constraint association. */
26515 init_constraint_processing (void)
26517 if (!flag_concepts
)
26520 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26521 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26522 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26523 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26524 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26527 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26531 declare_integer_pack (void)
26533 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26534 build_function_type_list (integer_type_node
,
26537 NULL_TREE
, ECF_CONST
);
26538 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26539 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26542 /* Set up the hash tables for template instantiations. */
26545 init_template_processing (void)
26547 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26548 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26550 if (cxx_dialect
>= cxx11
)
26551 declare_integer_pack ();
26554 /* Print stats about the template hash tables for -fstats. */
26557 print_template_statistics (void)
26559 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26560 "%f collisions\n", (long) decl_specializations
->size (),
26561 (long) decl_specializations
->elements (),
26562 decl_specializations
->collisions ());
26563 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26564 "%f collisions\n", (long) type_specializations
->size (),
26565 (long) type_specializations
->elements (),
26566 type_specializations
->collisions ());
26569 #include "gt-cp-pt.h"