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_specialization_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
3290 specialization, not including the template pattern. */
3293 primary_template_specialization_p (const_tree t
)
3298 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3299 return (DECL_LANG_SPECIFIC (t
)
3300 && DECL_USE_TEMPLATE (t
)
3301 && DECL_TEMPLATE_INFO (t
)
3302 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3303 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3304 return (CLASSTYPE_TEMPLATE_INFO (t
)
3305 && CLASSTYPE_USE_TEMPLATE (t
)
3306 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3307 else if (alias_template_specialization_p (t
))
3312 /* Return true if PARM is a template template parameter. */
3315 template_template_parameter_p (const_tree parm
)
3317 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3320 /* Return true iff PARM is a DECL representing a type template
3324 template_type_parameter_p (const_tree parm
)
3327 && (TREE_CODE (parm
) == TYPE_DECL
3328 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3329 && DECL_TEMPLATE_PARM_P (parm
));
3332 /* Return the template parameters of T if T is a
3333 primary template instantiation, NULL otherwise. */
3336 get_primary_template_innermost_parameters (const_tree t
)
3338 tree parms
= NULL
, template_info
= NULL
;
3340 if ((template_info
= get_template_info (t
))
3341 && primary_template_specialization_p (t
))
3342 parms
= INNERMOST_TEMPLATE_PARMS
3343 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3348 /* Return the template parameters of the LEVELth level from the full list
3349 of template parameters PARMS. */
3352 get_template_parms_at_level (tree parms
, int level
)
3356 || TREE_CODE (parms
) != TREE_LIST
3357 || level
> TMPL_PARMS_DEPTH (parms
))
3360 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3361 if (TMPL_PARMS_DEPTH (p
) == level
)
3367 /* Returns the template arguments of T if T is a template instantiation,
3371 get_template_innermost_arguments (const_tree t
)
3373 tree args
= NULL
, template_info
= NULL
;
3375 if ((template_info
= get_template_info (t
))
3376 && TI_ARGS (template_info
))
3377 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3382 /* Return the argument pack elements of T if T is a template argument pack,
3386 get_template_argument_pack_elems (const_tree t
)
3388 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3389 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3392 return ARGUMENT_PACK_ARGS (t
);
3395 /* True iff FN is a function representing a built-in variadic parameter
3399 builtin_pack_fn_p (tree fn
)
3402 || TREE_CODE (fn
) != FUNCTION_DECL
3403 || !DECL_IS_BUILTIN (fn
))
3406 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3412 /* True iff CALL is a call to a function representing a built-in variadic
3416 builtin_pack_call_p (tree call
)
3418 if (TREE_CODE (call
) != CALL_EXPR
)
3420 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3423 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3426 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3429 tree ohi
= CALL_EXPR_ARG (call
, 0);
3430 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3431 false/*fn*/, true/*int_cst*/);
3433 if (value_dependent_expression_p (hi
))
3437 call
= copy_node (call
);
3438 CALL_EXPR_ARG (call
, 0) = hi
;
3440 tree ex
= make_pack_expansion (call
, complain
);
3441 tree vec
= make_tree_vec (1);
3442 TREE_VEC_ELT (vec
, 0) = ex
;
3447 hi
= cxx_constant_value (hi
);
3448 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3450 /* Calculate the largest value of len that won't make the size of the vec
3451 overflow an int. The compiler will exceed resource limits long before
3452 this, but it seems a decent place to diagnose. */
3453 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3455 if (len
< 0 || len
> max
)
3457 if ((complain
& tf_error
)
3458 && hi
!= error_mark_node
)
3459 error ("argument to __integer_pack must be between 0 and %d", max
);
3460 return error_mark_node
;
3463 tree vec
= make_tree_vec (len
);
3465 for (int i
= 0; i
< len
; ++i
)
3466 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3472 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3476 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3479 if (!builtin_pack_call_p (call
))
3482 tree fn
= CALL_EXPR_FN (call
);
3484 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3485 return expand_integer_pack (call
, args
, complain
, in_decl
);
3490 /* Structure used to track the progress of find_parameter_packs_r. */
3491 struct find_parameter_pack_data
3493 /* TREE_LIST that will contain all of the parameter packs found by
3495 tree
* parameter_packs
;
3497 /* Set of AST nodes that have been visited by the traversal. */
3498 hash_set
<tree
> *visited
;
3500 /* True iff we're making a type pack expansion. */
3501 bool type_pack_expansion_p
;
3504 /* Identifies all of the argument packs that occur in a template
3505 argument and appends them to the TREE_LIST inside DATA, which is a
3506 find_parameter_pack_data structure. This is a subroutine of
3507 make_pack_expansion and uses_parameter_packs. */
3509 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3512 struct find_parameter_pack_data
* ppd
=
3513 (struct find_parameter_pack_data
*)data
;
3514 bool parameter_pack_p
= false;
3516 /* Handle type aliases/typedefs. */
3517 if (TYPE_ALIAS_P (t
))
3519 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3520 cp_walk_tree (&TI_ARGS (tinfo
),
3521 &find_parameter_packs_r
,
3527 /* Identify whether this is a parameter pack or not. */
3528 switch (TREE_CODE (t
))
3530 case TEMPLATE_PARM_INDEX
:
3531 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3532 parameter_pack_p
= true;
3535 case TEMPLATE_TYPE_PARM
:
3536 t
= TYPE_MAIN_VARIANT (t
);
3538 case TEMPLATE_TEMPLATE_PARM
:
3539 /* If the placeholder appears in the decl-specifier-seq of a function
3540 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3541 is a pack expansion, the invented template parameter is a template
3543 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3544 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3545 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3546 parameter_pack_p
= true;
3551 if (DECL_PACK_P (t
))
3553 /* We don't want to walk into the type of a PARM_DECL,
3554 because we don't want to see the type parameter pack. */
3556 parameter_pack_p
= true;
3560 /* Look through a lambda capture proxy to the field pack. */
3562 if (DECL_HAS_VALUE_EXPR_P (t
))
3564 tree v
= DECL_VALUE_EXPR (t
);
3566 &find_parameter_packs_r
,
3570 else if (variable_template_specialization_p (t
))
3572 cp_walk_tree (&DECL_TI_ARGS (t
),
3573 find_parameter_packs_r
,
3580 if (builtin_pack_call_p (t
))
3581 parameter_pack_p
= true;
3585 parameter_pack_p
= true;
3588 /* Not a parameter pack. */
3592 if (parameter_pack_p
)
3594 /* Add this parameter pack to the list. */
3595 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3599 cp_walk_tree (&TYPE_CONTEXT (t
),
3600 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3602 /* This switch statement will return immediately if we don't find a
3604 switch (TREE_CODE (t
))
3606 case TEMPLATE_PARM_INDEX
:
3609 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3610 /* Check the template itself. */
3611 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3612 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3613 /* Check the template arguments. */
3614 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3619 case TEMPLATE_TYPE_PARM
:
3620 case TEMPLATE_TEMPLATE_PARM
:
3627 /* Ignore the declaration of a capture proxy for a parameter pack. */
3628 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3633 if (TYPE_PTRMEMFUNC_P (t
))
3639 if (TYPE_TEMPLATE_INFO (t
))
3640 cp_walk_tree (&TYPE_TI_ARGS (t
),
3641 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3647 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3652 cp_walk_tree (&TREE_TYPE (t
),
3653 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3657 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3662 case TYPE_PACK_EXPANSION
:
3663 case EXPR_PACK_EXPANSION
:
3668 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3673 case IDENTIFIER_NODE
:
3674 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3681 tree fn
= lambda_function (t
);
3682 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3690 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3691 type_pack_expansion_p to false so that any placeholders
3692 within the expression don't get marked as parameter packs. */
3693 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3694 ppd
->type_pack_expansion_p
= false;
3695 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3697 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3709 /* Determines if the expression or type T uses any parameter packs. */
3711 uses_parameter_packs (tree t
)
3713 tree parameter_packs
= NULL_TREE
;
3714 struct find_parameter_pack_data ppd
;
3715 ppd
.parameter_packs
= ¶meter_packs
;
3716 ppd
.visited
= new hash_set
<tree
>;
3717 ppd
.type_pack_expansion_p
= false;
3718 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3720 return parameter_packs
!= NULL_TREE
;
3723 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3724 representation a base-class initializer into a parameter pack
3725 expansion. If all goes well, the resulting node will be an
3726 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3729 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3732 tree parameter_packs
= NULL_TREE
;
3733 bool for_types
= false;
3734 struct find_parameter_pack_data ppd
;
3736 if (!arg
|| arg
== error_mark_node
)
3739 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3741 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3742 class initializer. In this case, the TREE_PURPOSE will be a
3743 _TYPE node (representing the base class expansion we're
3744 initializing) and the TREE_VALUE will be a TREE_LIST
3745 containing the initialization arguments.
3747 The resulting expansion looks somewhat different from most
3748 expansions. Rather than returning just one _EXPANSION, we
3749 return a TREE_LIST whose TREE_PURPOSE is a
3750 TYPE_PACK_EXPANSION containing the bases that will be
3751 initialized. The TREE_VALUE will be identical to the
3752 original TREE_VALUE, which is a list of arguments that will
3753 be passed to each base. We do not introduce any new pack
3754 expansion nodes into the TREE_VALUE (although it is possible
3755 that some already exist), because the TREE_PURPOSE and
3756 TREE_VALUE all need to be expanded together with the same
3757 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3758 resulting TREE_PURPOSE will mention the parameter packs in
3759 both the bases and the arguments to the bases. */
3762 tree parameter_packs
= NULL_TREE
;
3764 /* Determine which parameter packs will be used by the base
3766 ppd
.visited
= new hash_set
<tree
>;
3767 ppd
.parameter_packs
= ¶meter_packs
;
3768 ppd
.type_pack_expansion_p
= true;
3769 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3770 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3773 if (parameter_packs
== NULL_TREE
)
3775 if (complain
& tf_error
)
3776 error ("base initializer expansion %qT contains no parameter packs",
3779 return error_mark_node
;
3782 if (TREE_VALUE (arg
) != void_type_node
)
3784 /* Collect the sets of parameter packs used in each of the
3785 initialization arguments. */
3786 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3788 /* Determine which parameter packs will be expanded in this
3790 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3797 /* Create the pack expansion type for the base type. */
3798 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3799 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3800 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3801 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3803 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3804 they will rarely be compared to anything. */
3805 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3807 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3810 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3813 /* Build the PACK_EXPANSION_* node. */
3815 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3816 : make_node (EXPR_PACK_EXPANSION
);
3817 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3818 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3820 /* Propagate type and const-expression information. */
3821 TREE_TYPE (result
) = TREE_TYPE (arg
);
3822 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3823 /* Mark this read now, since the expansion might be length 0. */
3824 mark_exp_read (arg
);
3827 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3828 they will rarely be compared to anything. */
3829 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3831 /* Determine which parameter packs will be expanded. */
3832 ppd
.parameter_packs
= ¶meter_packs
;
3833 ppd
.visited
= new hash_set
<tree
>;
3834 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3835 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3838 /* Make sure we found some parameter packs. */
3839 if (parameter_packs
== NULL_TREE
)
3841 if (complain
& tf_error
)
3844 error ("expansion pattern %qT contains no argument packs", arg
);
3846 error ("expansion pattern %qE contains no argument packs", arg
);
3848 return error_mark_node
;
3850 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3852 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3857 /* Checks T for any "bare" parameter packs, which have not yet been
3858 expanded, and issues an error if any are found. This operation can
3859 only be done on full expressions or types (e.g., an expression
3860 statement, "if" condition, etc.), because we could have expressions like:
3862 foo(f(g(h(args)))...)
3864 where "args" is a parameter pack. check_for_bare_parameter_packs
3865 should not be called for the subexpressions args, h(args),
3866 g(h(args)), or f(g(h(args))), because we would produce erroneous
3869 Returns TRUE and emits an error if there were bare parameter packs,
3870 returns FALSE otherwise. */
3872 check_for_bare_parameter_packs (tree t
)
3874 tree parameter_packs
= NULL_TREE
;
3875 struct find_parameter_pack_data ppd
;
3877 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3880 /* A lambda might use a parameter pack from the containing context. */
3881 if (current_function_decl
&& LAMBDA_FUNCTION_P (current_function_decl
))
3884 if (TREE_CODE (t
) == TYPE_DECL
)
3887 ppd
.parameter_packs
= ¶meter_packs
;
3888 ppd
.visited
= new hash_set
<tree
>;
3889 ppd
.type_pack_expansion_p
= false;
3890 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3893 if (parameter_packs
)
3895 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3896 error_at (loc
, "parameter packs not expanded with %<...%>:");
3897 while (parameter_packs
)
3899 tree pack
= TREE_VALUE (parameter_packs
);
3900 tree name
= NULL_TREE
;
3902 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3903 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3904 name
= TYPE_NAME (pack
);
3905 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3906 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3907 else if (TREE_CODE (pack
) == CALL_EXPR
)
3908 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3910 name
= DECL_NAME (pack
);
3913 inform (loc
, " %qD", name
);
3915 inform (loc
, " <anonymous>");
3917 parameter_packs
= TREE_CHAIN (parameter_packs
);
3926 /* Expand any parameter packs that occur in the template arguments in
3929 expand_template_argument_pack (tree args
)
3931 if (args
== error_mark_node
)
3932 return error_mark_node
;
3934 tree result_args
= NULL_TREE
;
3935 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3936 int num_result_args
= -1;
3937 int non_default_args_count
= -1;
3939 /* First, determine if we need to expand anything, and the number of
3940 slots we'll need. */
3941 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3943 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3944 if (arg
== NULL_TREE
)
3946 if (ARGUMENT_PACK_P (arg
))
3948 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3949 if (num_result_args
< 0)
3950 num_result_args
= in_arg
+ num_packed
;
3952 num_result_args
+= num_packed
;
3956 if (num_result_args
>= 0)
3961 /* If no expansion is necessary, we're done. */
3962 if (num_result_args
< 0)
3965 /* Expand arguments. */
3966 result_args
= make_tree_vec (num_result_args
);
3967 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3968 non_default_args_count
=
3969 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3970 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3972 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3973 if (ARGUMENT_PACK_P (arg
))
3975 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3976 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3977 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3978 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3979 if (non_default_args_count
> 0)
3980 non_default_args_count
+= num_packed
- 1;
3984 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3988 if (non_default_args_count
>= 0)
3989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3993 /* Checks if DECL shadows a template parameter.
3995 [temp.local]: A template-parameter shall not be redeclared within its
3996 scope (including nested scopes).
3998 Emits an error and returns TRUE if the DECL shadows a parameter,
3999 returns FALSE otherwise. */
4002 check_template_shadow (tree decl
)
4006 /* If we're not in a template, we can't possibly shadow a template
4008 if (!current_template_parms
)
4011 /* Figure out what we're shadowing. */
4012 decl
= OVL_FIRST (decl
);
4013 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4015 /* If there's no previous binding for this name, we're not shadowing
4016 anything, let alone a template parameter. */
4020 /* If we're not shadowing a template parameter, we're done. Note
4021 that OLDDECL might be an OVERLOAD (or perhaps even an
4022 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4024 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4027 /* We check for decl != olddecl to avoid bogus errors for using a
4028 name inside a class. We check TPFI to avoid duplicate errors for
4029 inline member templates. */
4031 || (DECL_TEMPLATE_PARM_P (decl
)
4032 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4035 /* Don't complain about the injected class name, as we've already
4036 complained about the class itself. */
4037 if (DECL_SELF_REFERENCE_P (decl
))
4040 if (DECL_TEMPLATE_PARM_P (decl
))
4041 error ("declaration of template parameter %q+D shadows "
4042 "template parameter", decl
);
4044 error ("declaration of %q+#D shadows template parameter", decl
);
4045 inform (DECL_SOURCE_LOCATION (olddecl
),
4046 "template parameter %qD declared here", olddecl
);
4050 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4051 ORIG_LEVEL, DECL, and TYPE. */
4054 build_template_parm_index (int index
,
4060 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4061 TEMPLATE_PARM_IDX (t
) = index
;
4062 TEMPLATE_PARM_LEVEL (t
) = level
;
4063 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4064 TEMPLATE_PARM_DECL (t
) = decl
;
4065 TREE_TYPE (t
) = type
;
4066 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4067 TREE_READONLY (t
) = TREE_READONLY (decl
);
4072 /* Find the canonical type parameter for the given template type
4073 parameter. Returns the canonical type parameter, which may be TYPE
4074 if no such parameter existed. */
4077 canonical_type_parameter (tree type
)
4080 int idx
= TEMPLATE_TYPE_IDX (type
);
4081 if (!canonical_template_parms
)
4082 vec_alloc (canonical_template_parms
, idx
+ 1);
4084 if (canonical_template_parms
->length () <= (unsigned) idx
)
4085 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4087 list
= (*canonical_template_parms
)[idx
];
4088 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4089 list
= TREE_CHAIN (list
);
4092 return TREE_VALUE (list
);
4095 (*canonical_template_parms
)[idx
]
4096 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4101 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4102 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4103 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4104 new one is created. */
4107 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4108 tsubst_flags_t complain
)
4110 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4111 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4112 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4113 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4115 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4118 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4119 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4120 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4121 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4122 DECL_ARTIFICIAL (decl
) = 1;
4123 SET_DECL_TEMPLATE_PARM_P (decl
);
4125 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4126 TEMPLATE_PARM_LEVEL (index
) - levels
,
4127 TEMPLATE_PARM_ORIG_LEVEL (index
),
4129 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4130 TEMPLATE_PARM_PARAMETER_PACK (t
)
4131 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4133 /* Template template parameters need this. */
4134 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4136 DECL_TEMPLATE_RESULT (decl
)
4137 = build_decl (DECL_SOURCE_LOCATION (decl
),
4138 TYPE_DECL
, DECL_NAME (decl
), type
);
4139 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4140 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4141 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4145 return TEMPLATE_PARM_DESCENDANTS (index
);
4148 /* Process information from new template parameter PARM and append it
4149 to the LIST being built. This new parameter is a non-type
4150 parameter iff IS_NON_TYPE is true. This new parameter is a
4151 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4155 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4156 bool is_non_type
, bool is_parameter_pack
)
4161 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4162 tree defval
= TREE_PURPOSE (parm
);
4163 tree constr
= TREE_TYPE (parm
);
4167 tree p
= tree_last (list
);
4169 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4172 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4173 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4175 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4183 parm
= TREE_VALUE (parm
);
4185 SET_DECL_TEMPLATE_PARM_P (parm
);
4187 if (TREE_TYPE (parm
) != error_mark_node
)
4191 The top-level cv-qualifiers on the template-parameter are
4192 ignored when determining its type. */
4193 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4194 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4195 TREE_TYPE (parm
) = error_mark_node
;
4196 else if (uses_parameter_packs (TREE_TYPE (parm
))
4197 && !is_parameter_pack
4198 /* If we're in a nested template parameter list, the template
4199 template parameter could be a parameter pack. */
4200 && processing_template_parmlist
== 1)
4202 /* This template parameter is not a parameter pack, but it
4203 should be. Complain about "bare" parameter packs. */
4204 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4206 /* Recover by calling this a parameter pack. */
4207 is_parameter_pack
= true;
4211 /* A template parameter is not modifiable. */
4212 TREE_CONSTANT (parm
) = 1;
4213 TREE_READONLY (parm
) = 1;
4214 decl
= build_decl (parm_loc
,
4215 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4216 TREE_CONSTANT (decl
) = 1;
4217 TREE_READONLY (decl
) = 1;
4218 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4219 = build_template_parm_index (idx
, processing_template_decl
,
4220 processing_template_decl
,
4221 decl
, TREE_TYPE (parm
));
4223 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4224 = is_parameter_pack
;
4229 parm
= TREE_VALUE (TREE_VALUE (parm
));
4231 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4233 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4234 /* This is for distinguishing between real templates and template
4235 template parameters */
4236 TREE_TYPE (parm
) = t
;
4237 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4242 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4243 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4244 decl
= build_decl (parm_loc
,
4245 TYPE_DECL
, parm
, t
);
4248 TYPE_NAME (t
) = decl
;
4249 TYPE_STUB_DECL (t
) = decl
;
4251 TEMPLATE_TYPE_PARM_INDEX (t
)
4252 = build_template_parm_index (idx
, processing_template_decl
,
4253 processing_template_decl
,
4254 decl
, TREE_TYPE (parm
));
4255 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4256 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4258 DECL_ARTIFICIAL (decl
) = 1;
4259 SET_DECL_TEMPLATE_PARM_P (decl
);
4261 /* Build requirements for the type/template parameter.
4262 This must be done after SET_DECL_TEMPLATE_PARM_P or
4263 process_template_parm could fail. */
4264 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4268 /* Build the parameter node linking the parameter declaration,
4269 its default argument (if any), and its constraints (if any). */
4270 parm
= build_tree_list (defval
, parm
);
4271 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4273 return chainon (list
, parm
);
4276 /* The end of a template parameter list has been reached. Process the
4277 tree list into a parameter vector, converting each parameter into a more
4278 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4282 end_template_parm_list (tree parms
)
4286 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4288 /* Pop the dummy parameter level and add the real one. */
4289 current_template_parms
= TREE_CHAIN (current_template_parms
);
4291 current_template_parms
4292 = tree_cons (size_int (processing_template_decl
),
4293 saved_parmlist
, current_template_parms
);
4295 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4297 next
= TREE_CHAIN (parm
);
4298 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4299 TREE_CHAIN (parm
) = NULL_TREE
;
4302 --processing_template_parmlist
;
4304 return saved_parmlist
;
4307 // Explicitly indicate the end of the template parameter list. We assume
4308 // that the current template parameters have been constructed and/or
4309 // managed explicitly, as when creating new template template parameters
4310 // from a shorthand constraint.
4312 end_template_parm_list ()
4314 --processing_template_parmlist
;
4317 /* end_template_decl is called after a template declaration is seen. */
4320 end_template_decl (void)
4322 reset_specialization ();
4324 if (! processing_template_decl
)
4327 /* This matches the pushlevel in begin_template_parm_list. */
4330 --processing_template_decl
;
4331 current_template_parms
= TREE_CHAIN (current_template_parms
);
4334 /* Takes a TREE_LIST representing a template parameter and convert it
4335 into an argument suitable to be passed to the type substitution
4336 functions. Note that If the TREE_LIST contains an error_mark
4337 node, the returned argument is error_mark_node. */
4340 template_parm_to_arg (tree t
)
4344 || TREE_CODE (t
) != TREE_LIST
)
4347 if (error_operand_p (TREE_VALUE (t
)))
4348 return error_mark_node
;
4352 if (TREE_CODE (t
) == TYPE_DECL
4353 || TREE_CODE (t
) == TEMPLATE_DECL
)
4357 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4359 /* Turn this argument into a TYPE_ARGUMENT_PACK
4360 with a single element, which expands T. */
4361 tree vec
= make_tree_vec (1);
4363 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4365 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4367 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4368 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4373 t
= DECL_INITIAL (t
);
4375 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4377 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4378 with a single element, which expands T. */
4379 tree vec
= make_tree_vec (1);
4381 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4383 t
= convert_from_reference (t
);
4384 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4386 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4387 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4390 t
= convert_from_reference (t
);
4395 /* Given a single level of template parameters (a TREE_VEC), return it
4396 as a set of template arguments. */
4399 template_parms_level_to_args (tree parms
)
4401 tree a
= copy_node (parms
);
4402 TREE_TYPE (a
) = NULL_TREE
;
4403 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4404 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4407 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4412 /* Given a set of template parameters, return them as a set of template
4413 arguments. The template parameters are represented as a TREE_VEC, in
4414 the form documented in cp-tree.h for template arguments. */
4417 template_parms_to_args (tree parms
)
4420 tree args
= NULL_TREE
;
4421 int length
= TMPL_PARMS_DEPTH (parms
);
4424 /* If there is only one level of template parameters, we do not
4425 create a TREE_VEC of TREE_VECs. Instead, we return a single
4426 TREE_VEC containing the arguments. */
4428 args
= make_tree_vec (length
);
4430 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4432 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4435 TREE_VEC_ELT (args
, --l
) = a
;
4443 /* Within the declaration of a template, return the currently active
4444 template parameters as an argument TREE_VEC. */
4447 current_template_args (void)
4449 return template_parms_to_args (current_template_parms
);
4452 /* Update the declared TYPE by doing any lookups which were thought to be
4453 dependent, but are not now that we know the SCOPE of the declarator. */
4456 maybe_update_decl_type (tree orig_type
, tree scope
)
4458 tree type
= orig_type
;
4460 if (type
== NULL_TREE
)
4463 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4464 type
= TREE_TYPE (type
);
4466 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4467 && dependent_type_p (type
)
4468 /* Don't bother building up the args in this case. */
4469 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4471 /* tsubst in the args corresponding to the template parameters,
4472 including auto if present. Most things will be unchanged, but
4473 make_typename_type and tsubst_qualified_id will resolve
4474 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4475 tree args
= current_template_args ();
4476 tree auto_node
= type_uses_auto (type
);
4480 tree auto_vec
= make_tree_vec (1);
4481 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4482 args
= add_to_template_args (args
, auto_vec
);
4484 pushed
= push_scope (scope
);
4485 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4490 if (type
== error_mark_node
)
4493 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4495 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4498 type
= TYPE_NAME (type
);
4503 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4504 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4505 the new template is a member template. */
4508 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4510 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4511 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4512 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4513 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4514 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4519 struct template_parm_data
4521 /* The level of the template parameters we are currently
4525 /* The index of the specialization argument we are currently
4529 /* An array whose size is the number of template parameters. The
4530 elements are nonzero if the parameter has been used in any one
4531 of the arguments processed so far. */
4534 /* An array whose size is the number of template arguments. The
4535 elements are nonzero if the argument makes use of template
4536 parameters of this level. */
4537 int* arg_uses_template_parms
;
4540 /* Subroutine of push_template_decl used to see if each template
4541 parameter in a partial specialization is used in the explicit
4542 argument list. If T is of the LEVEL given in DATA (which is
4543 treated as a template_parm_data*), then DATA->PARMS is marked
4547 mark_template_parm (tree t
, void* data
)
4551 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4553 template_parm_level_and_index (t
, &level
, &idx
);
4555 if (level
== tpd
->level
)
4557 tpd
->parms
[idx
] = 1;
4558 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4561 /* In C++17 the type of a non-type argument is a deduced context. */
4562 if (cxx_dialect
>= cxx17
4563 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4564 for_each_template_parm (TREE_TYPE (t
),
4565 &mark_template_parm
,
4568 /*include_nondeduced_p=*/false);
4570 /* Return zero so that for_each_template_parm will continue the
4571 traversal of the tree; we want to mark *every* template parm. */
4575 /* Process the partial specialization DECL. */
4578 process_partial_specialization (tree decl
)
4580 tree type
= TREE_TYPE (decl
);
4581 tree tinfo
= get_template_info (decl
);
4582 tree maintmpl
= TI_TEMPLATE (tinfo
);
4583 tree specargs
= TI_ARGS (tinfo
);
4584 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4585 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4588 int nargs
= TREE_VEC_LENGTH (inner_args
);
4591 bool did_error_intro
= false;
4592 struct template_parm_data tpd
;
4593 struct template_parm_data tpd2
;
4595 gcc_assert (current_template_parms
);
4597 /* A concept cannot be specialized. */
4598 if (flag_concepts
&& variable_concept_p (maintmpl
))
4600 error ("specialization of variable concept %q#D", maintmpl
);
4601 return error_mark_node
;
4604 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4605 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4607 /* We check that each of the template parameters given in the
4608 partial specialization is used in the argument list to the
4609 specialization. For example:
4611 template <class T> struct S;
4612 template <class T> struct S<T*>;
4614 The second declaration is OK because `T*' uses the template
4615 parameter T, whereas
4617 template <class T> struct S<int>;
4619 is no good. Even trickier is:
4630 The S2<T> declaration is actually invalid; it is a
4631 full-specialization. Of course,
4634 struct S2<T (*)(U)>;
4636 or some such would have been OK. */
4637 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4638 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4639 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4641 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4642 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4643 for (i
= 0; i
< nargs
; ++i
)
4645 tpd
.current_arg
= i
;
4646 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4647 &mark_template_parm
,
4650 /*include_nondeduced_p=*/false);
4652 for (i
= 0; i
< ntparms
; ++i
)
4653 if (tpd
.parms
[i
] == 0)
4655 /* One of the template parms was not used in a deduced context in the
4657 if (!did_error_intro
)
4659 error ("template parameters not deducible in "
4660 "partial specialization:");
4661 did_error_intro
= true;
4664 inform (input_location
, " %qD",
4665 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4668 if (did_error_intro
)
4669 return error_mark_node
;
4671 /* [temp.class.spec]
4673 The argument list of the specialization shall not be identical to
4674 the implicit argument list of the primary template. */
4676 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4677 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4679 || !strictly_subsumes (current_template_constraints (),
4680 get_constraints (maintmpl
))))
4683 error ("partial specialization %q+D does not specialize "
4684 "any template arguments", decl
);
4686 error ("partial specialization %q+D does not specialize any "
4687 "template arguments and is not more constrained than", decl
);
4688 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4691 /* A partial specialization that replaces multiple parameters of the
4692 primary template with a pack expansion is less specialized for those
4694 if (nargs
< DECL_NTPARMS (maintmpl
))
4696 error ("partial specialization is not more specialized than the "
4697 "primary template because it replaces multiple parameters "
4698 "with a pack expansion");
4699 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4700 /* Avoid crash in process_partial_specialization. */
4704 /* If we aren't in a dependent class, we can actually try deduction. */
4705 else if (tpd
.level
== 1
4706 /* FIXME we should be able to handle a partial specialization of a
4707 partial instantiation, but currently we can't (c++/41727). */
4708 && TMPL_ARGS_DEPTH (specargs
) == 1
4709 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4711 if (permerror (input_location
, "partial specialization %qD is not "
4712 "more specialized than", decl
))
4713 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4717 /* [temp.class.spec]
4719 A partially specialized non-type argument expression shall not
4720 involve template parameters of the partial specialization except
4721 when the argument expression is a simple identifier.
4723 The type of a template parameter corresponding to a specialized
4724 non-type argument shall not be dependent on a parameter of the
4727 Also, we verify that pack expansions only occur at the
4728 end of the argument list. */
4729 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4731 for (i
= 0; i
< nargs
; ++i
)
4733 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4734 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4735 tree packed_args
= NULL_TREE
;
4738 if (ARGUMENT_PACK_P (arg
))
4740 /* Extract the arguments from the argument pack. We'll be
4741 iterating over these in the following loop. */
4742 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4743 len
= TREE_VEC_LENGTH (packed_args
);
4746 for (j
= 0; j
< len
; j
++)
4749 /* Get the Jth argument in the parameter pack. */
4750 arg
= TREE_VEC_ELT (packed_args
, j
);
4752 if (PACK_EXPANSION_P (arg
))
4754 /* Pack expansions must come at the end of the
4756 if ((packed_args
&& j
< len
- 1)
4757 || (!packed_args
&& i
< nargs
- 1))
4759 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4760 error ("parameter pack argument %qE must be at the "
4761 "end of the template argument list", arg
);
4763 error ("parameter pack argument %qT must be at the "
4764 "end of the template argument list", arg
);
4768 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4769 /* We only care about the pattern. */
4770 arg
= PACK_EXPANSION_PATTERN (arg
);
4772 if (/* These first two lines are the `non-type' bit. */
4774 && TREE_CODE (arg
) != TEMPLATE_DECL
4775 /* This next two lines are the `argument expression is not just a
4776 simple identifier' condition and also the `specialized
4777 non-type argument' bit. */
4778 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4779 && !(REFERENCE_REF_P (arg
)
4780 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4782 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4783 || (packed_args
&& uses_template_parms (arg
)))
4784 error ("template argument %qE involves template parameter(s)",
4788 /* Look at the corresponding template parameter,
4789 marking which template parameters its type depends
4791 tree type
= TREE_TYPE (parm
);
4795 /* We haven't yet initialized TPD2. Do so now. */
4796 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4797 /* The number of parameters here is the number in the
4798 main template, which, as checked in the assertion
4800 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4802 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4805 /* Mark the template parameters. But this time, we're
4806 looking for the template parameters of the main
4807 template, not in the specialization. */
4808 tpd2
.current_arg
= i
;
4809 tpd2
.arg_uses_template_parms
[i
] = 0;
4810 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4811 for_each_template_parm (type
,
4812 &mark_template_parm
,
4815 /*include_nondeduced_p=*/false);
4817 if (tpd2
.arg_uses_template_parms
[i
])
4819 /* The type depended on some template parameters.
4820 If they are fully specialized in the
4821 specialization, that's OK. */
4824 for (j
= 0; j
< nargs
; ++j
)
4825 if (tpd2
.parms
[j
] != 0
4826 && tpd
.arg_uses_template_parms
[j
])
4829 error_n (input_location
, count
,
4830 "type %qT of template argument %qE depends "
4831 "on a template parameter",
4832 "type %qT of template argument %qE depends "
4833 "on template parameters",
4842 /* We should only get here once. */
4843 if (TREE_CODE (decl
) == TYPE_DECL
)
4844 gcc_assert (!COMPLETE_TYPE_P (type
));
4846 // Build the template decl.
4847 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4848 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4849 TREE_TYPE (tmpl
) = type
;
4850 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4851 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4852 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4853 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4855 /* Give template template parms a DECL_CONTEXT of the template
4856 for which they are a parameter. */
4857 for (i
= 0; i
< ntparms
; ++i
)
4859 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4860 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4861 DECL_CONTEXT (parm
) = tmpl
;
4865 /* We didn't register this in check_explicit_specialization so we could
4866 wait until the constraints were set. */
4867 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4869 associate_classtype_constraints (type
);
4871 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4872 = tree_cons (specargs
, tmpl
,
4873 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4874 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4876 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4877 inst
= TREE_CHAIN (inst
))
4879 tree instance
= TREE_VALUE (inst
);
4880 if (TYPE_P (instance
)
4881 ? (COMPLETE_TYPE_P (instance
)
4882 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4883 : DECL_TEMPLATE_INSTANTIATION (instance
))
4885 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4886 tree inst_decl
= (DECL_P (instance
)
4887 ? instance
: TYPE_NAME (instance
));
4890 else if (spec
== error_mark_node
)
4891 permerror (input_location
,
4892 "declaration of %qD ambiguates earlier template "
4893 "instantiation for %qD", decl
, inst_decl
);
4894 else if (TREE_VALUE (spec
) == tmpl
)
4895 permerror (input_location
,
4896 "partial specialization of %qD after instantiation "
4897 "of %qD", decl
, inst_decl
);
4904 /* PARM is a template parameter of some form; return the corresponding
4905 TEMPLATE_PARM_INDEX. */
4908 get_template_parm_index (tree parm
)
4910 if (TREE_CODE (parm
) == PARM_DECL
4911 || TREE_CODE (parm
) == CONST_DECL
)
4912 parm
= DECL_INITIAL (parm
);
4913 else if (TREE_CODE (parm
) == TYPE_DECL
4914 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4915 parm
= TREE_TYPE (parm
);
4916 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4917 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4918 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4919 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4920 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4924 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4925 parameter packs used by the template parameter PARM. */
4928 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4930 /* A type parm can't refer to another parm. */
4931 if (TREE_CODE (parm
) == TYPE_DECL
)
4933 else if (TREE_CODE (parm
) == PARM_DECL
)
4935 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4940 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4942 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4943 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4944 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4947 /* PARM is a template parameter pack. Return any parameter packs used in
4948 its type or the type of any of its template parameters. If there are
4949 any such packs, it will be instantiated into a fixed template parameter
4950 list by partial instantiation rather than be fully deduced. */
4953 fixed_parameter_pack_p (tree parm
)
4955 /* This can only be true in a member template. */
4956 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4958 /* This can only be true for a parameter pack. */
4959 if (!template_parameter_pack_p (parm
))
4961 /* A type parm can't refer to another parm. */
4962 if (TREE_CODE (parm
) == TYPE_DECL
)
4965 tree parameter_packs
= NULL_TREE
;
4966 struct find_parameter_pack_data ppd
;
4967 ppd
.parameter_packs
= ¶meter_packs
;
4968 ppd
.visited
= new hash_set
<tree
>;
4969 ppd
.type_pack_expansion_p
= false;
4971 fixed_parameter_pack_p_1 (parm
, &ppd
);
4974 return parameter_packs
;
4977 /* Check that a template declaration's use of default arguments and
4978 parameter packs is not invalid. Here, PARMS are the template
4979 parameters. IS_PRIMARY is true if DECL is the thing declared by
4980 a primary template. IS_PARTIAL is true if DECL is a partial
4983 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4984 declaration (but not a definition); 1 indicates a declaration, 2
4985 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4986 emitted for extraneous default arguments.
4988 Returns TRUE if there were no errors found, FALSE otherwise. */
4991 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4992 bool is_partial
, int is_friend_decl
)
4995 int last_level_to_check
;
4997 bool no_errors
= true;
5001 A default template-argument shall not be specified in a
5002 function template declaration or a function template definition, nor
5003 in the template-parameter-list of the definition of a member of a
5006 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5007 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5008 /* You can't have a function template declaration in a local
5009 scope, nor you can you define a member of a class template in a
5013 if ((TREE_CODE (decl
) == TYPE_DECL
5015 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5016 || (TREE_CODE (decl
) == FUNCTION_DECL
5017 && LAMBDA_FUNCTION_P (decl
)))
5018 /* A lambda doesn't have an explicit declaration; don't complain
5019 about the parms of the enclosing class. */
5022 if (current_class_type
5023 && !TYPE_BEING_DEFINED (current_class_type
)
5024 && DECL_LANG_SPECIFIC (decl
)
5025 && DECL_DECLARES_FUNCTION_P (decl
)
5026 /* If this is either a friend defined in the scope of the class
5027 or a member function. */
5028 && (DECL_FUNCTION_MEMBER_P (decl
)
5029 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5030 : DECL_FRIEND_CONTEXT (decl
)
5031 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5033 /* And, if it was a member function, it really was defined in
5034 the scope of the class. */
5035 && (!DECL_FUNCTION_MEMBER_P (decl
)
5036 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5037 /* We already checked these parameters when the template was
5038 declared, so there's no need to do it again now. This function
5039 was defined in class scope, but we're processing its body now
5040 that the class is complete. */
5043 /* Core issue 226 (C++0x only): the following only applies to class
5046 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5050 If a template-parameter has a default template-argument, all
5051 subsequent template-parameters shall have a default
5052 template-argument supplied. */
5053 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5055 tree inner_parms
= TREE_VALUE (parm_level
);
5056 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5057 int seen_def_arg_p
= 0;
5060 for (i
= 0; i
< ntparms
; ++i
)
5062 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5064 if (parm
== error_mark_node
)
5067 if (TREE_PURPOSE (parm
))
5069 else if (seen_def_arg_p
5070 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5072 error ("no default argument for %qD", TREE_VALUE (parm
));
5073 /* For better subsequent error-recovery, we indicate that
5074 there should have been a default argument. */
5075 TREE_PURPOSE (parm
) = error_mark_node
;
5078 else if (!is_partial
5080 /* Don't complain about an enclosing partial
5082 && parm_level
== parms
5083 && TREE_CODE (decl
) == TYPE_DECL
5085 && template_parameter_pack_p (TREE_VALUE (parm
))
5086 /* A fixed parameter pack will be partially
5087 instantiated into a fixed length list. */
5088 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5090 /* A primary class template can only have one
5091 parameter pack, at the end of the template
5094 error ("parameter pack %q+D must be at the end of the"
5095 " template parameter list", TREE_VALUE (parm
));
5097 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5105 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5109 /* For an ordinary class template, default template arguments are
5110 allowed at the innermost level, e.g.:
5111 template <class T = int>
5113 but, in a partial specialization, they're not allowed even
5114 there, as we have in [temp.class.spec]:
5116 The template parameter list of a specialization shall not
5117 contain default template argument values.
5119 So, for a partial specialization, or for a function template
5120 (in C++98/C++03), we look at all of them. */
5123 /* But, for a primary class template that is not a partial
5124 specialization we look at all template parameters except the
5126 parms
= TREE_CHAIN (parms
);
5128 /* Figure out what error message to issue. */
5129 if (is_friend_decl
== 2)
5130 msg
= G_("default template arguments may not be used in function template "
5131 "friend re-declaration");
5132 else if (is_friend_decl
)
5133 msg
= G_("default template arguments may not be used in function template "
5134 "friend declarations");
5135 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5136 msg
= G_("default template arguments may not be used in function templates "
5137 "without -std=c++11 or -std=gnu++11");
5138 else if (is_partial
)
5139 msg
= G_("default template arguments may not be used in "
5140 "partial specializations");
5141 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5142 msg
= G_("default argument for template parameter for class enclosing %qD");
5144 /* Per [temp.param]/9, "A default template-argument shall not be
5145 specified in the template-parameter-lists of the definition of
5146 a member of a class template that appears outside of the member's
5147 class.", thus if we aren't handling a member of a class template
5148 there is no need to examine the parameters. */
5151 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5152 /* If we're inside a class definition, there's no need to
5153 examine the parameters to the class itself. On the one
5154 hand, they will be checked when the class is defined, and,
5155 on the other, default arguments are valid in things like:
5156 template <class T = double>
5157 struct S { template <class U> void f(U); };
5158 Here the default argument for `S' has no bearing on the
5159 declaration of `f'. */
5160 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5162 /* Check everything. */
5163 last_level_to_check
= 0;
5165 for (parm_level
= parms
;
5166 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5167 parm_level
= TREE_CHAIN (parm_level
))
5169 tree inner_parms
= TREE_VALUE (parm_level
);
5173 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5174 for (i
= 0; i
< ntparms
; ++i
)
5176 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5179 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5184 if (is_friend_decl
== 2)
5191 /* Clear out the default argument so that we are not
5193 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5197 /* At this point, if we're still interested in issuing messages,
5198 they must apply to classes surrounding the object declared. */
5200 msg
= G_("default argument for template parameter for class "
5207 /* Worker for push_template_decl_real, called via
5208 for_each_template_parm. DATA is really an int, indicating the
5209 level of the parameters we are interested in. If T is a template
5210 parameter of that level, return nonzero. */
5213 template_parm_this_level_p (tree t
, void* data
)
5215 int this_level
= *(int *)data
;
5218 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5219 level
= TEMPLATE_PARM_LEVEL (t
);
5221 level
= TEMPLATE_TYPE_LEVEL (t
);
5222 return level
== this_level
;
5225 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5226 DATA is really an int, indicating the innermost outer level of parameters.
5227 If T is a template parameter of that level or further out, return
5231 template_parm_outer_level (tree t
, void *data
)
5233 int this_level
= *(int *)data
;
5236 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5237 level
= TEMPLATE_PARM_LEVEL (t
);
5239 level
= TEMPLATE_TYPE_LEVEL (t
);
5240 return level
<= this_level
;
5243 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5244 parameters given by current_template_args, or reuses a
5245 previously existing one, if appropriate. Returns the DECL, or an
5246 equivalent one, if it is replaced via a call to duplicate_decls.
5248 If IS_FRIEND is true, DECL is a friend declaration. */
5251 push_template_decl_real (tree decl
, bool is_friend
)
5259 int new_template_p
= 0;
5260 /* True if the template is a member template, in the sense of
5262 bool member_template_p
= false;
5264 if (decl
== error_mark_node
|| !current_template_parms
)
5265 return error_mark_node
;
5267 /* See if this is a partial specialization. */
5268 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5269 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5270 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5272 && DECL_LANG_SPECIFIC (decl
)
5273 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5274 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5276 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5280 /* For a friend, we want the context of the friend function, not
5281 the type of which it is a friend. */
5282 ctx
= CP_DECL_CONTEXT (decl
);
5283 else if (CP_DECL_CONTEXT (decl
)
5284 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5285 /* In the case of a virtual function, we want the class in which
5287 ctx
= CP_DECL_CONTEXT (decl
);
5289 /* Otherwise, if we're currently defining some class, the DECL
5290 is assumed to be a member of the class. */
5291 ctx
= current_scope ();
5293 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5296 if (!DECL_CONTEXT (decl
))
5297 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5299 /* See if this is a primary template. */
5300 if (is_friend
&& ctx
5301 && uses_template_parms_level (ctx
, processing_template_decl
))
5302 /* A friend template that specifies a class context, i.e.
5303 template <typename T> friend void A<T>::f();
5306 else if (TREE_CODE (decl
) == TYPE_DECL
5307 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5310 is_primary
= template_parm_scope_p ();
5314 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5316 if (DECL_CLASS_SCOPE_P (decl
))
5317 member_template_p
= true;
5318 if (TREE_CODE (decl
) == TYPE_DECL
5319 && anon_aggrname_p (DECL_NAME (decl
)))
5321 error ("template class without a name");
5322 return error_mark_node
;
5324 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5326 if (member_template_p
)
5328 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5329 error ("member template %qD may not have virt-specifiers", decl
);
5331 if (DECL_DESTRUCTOR_P (decl
))
5335 A destructor shall not be a member template. */
5336 error ("destructor %qD declared as member template", decl
);
5337 return error_mark_node
;
5339 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5340 && (!prototype_p (TREE_TYPE (decl
))
5341 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5342 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5343 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5344 == void_list_node
)))
5346 /* [basic.stc.dynamic.allocation]
5348 An allocation function can be a function
5349 template. ... Template allocation functions shall
5350 have two or more parameters. */
5351 error ("invalid template declaration of %qD", decl
);
5352 return error_mark_node
;
5355 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5356 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5358 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5359 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5360 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5362 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5363 if (TREE_CODE (t
) == TYPE_DECL
)
5365 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5366 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5369 else if (TREE_CODE (decl
) == TYPE_DECL
5370 && TYPE_DECL_ALIAS_P (decl
))
5371 /* alias-declaration */
5372 gcc_assert (!DECL_ARTIFICIAL (decl
));
5373 else if (VAR_P (decl
))
5374 /* C++14 variable template. */;
5377 error ("template declaration of %q#D", decl
);
5378 return error_mark_node
;
5382 /* Check to see that the rules regarding the use of default
5383 arguments are not being violated. */
5384 check_default_tmpl_args (decl
, current_template_parms
,
5385 is_primary
, is_partial
, /*is_friend_decl=*/0);
5387 /* Ensure that there are no parameter packs in the type of this
5388 declaration that have not been expanded. */
5389 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5391 /* Check each of the arguments individually to see if there are
5392 any bare parameter packs. */
5393 tree type
= TREE_TYPE (decl
);
5394 tree arg
= DECL_ARGUMENTS (decl
);
5395 tree argtype
= TYPE_ARG_TYPES (type
);
5397 while (arg
&& argtype
)
5399 if (!DECL_PACK_P (arg
)
5400 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5402 /* This is a PARM_DECL that contains unexpanded parameter
5403 packs. We have already complained about this in the
5404 check_for_bare_parameter_packs call, so just replace
5405 these types with ERROR_MARK_NODE. */
5406 TREE_TYPE (arg
) = error_mark_node
;
5407 TREE_VALUE (argtype
) = error_mark_node
;
5410 arg
= DECL_CHAIN (arg
);
5411 argtype
= TREE_CHAIN (argtype
);
5414 /* Check for bare parameter packs in the return type and the
5415 exception specifiers. */
5416 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5417 /* Errors were already issued, set return type to int
5418 as the frontend doesn't expect error_mark_node as
5420 TREE_TYPE (type
) = integer_type_node
;
5421 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5422 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5424 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5425 && TYPE_DECL_ALIAS_P (decl
))
5426 ? DECL_ORIGINAL_TYPE (decl
)
5427 : TREE_TYPE (decl
)))
5429 TREE_TYPE (decl
) = error_mark_node
;
5430 return error_mark_node
;
5434 return process_partial_specialization (decl
);
5436 args
= current_template_args ();
5439 || TREE_CODE (ctx
) == FUNCTION_DECL
5440 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5441 || (TREE_CODE (decl
) == TYPE_DECL
5442 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5443 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5445 if (DECL_LANG_SPECIFIC (decl
)
5446 && DECL_TEMPLATE_INFO (decl
)
5447 && DECL_TI_TEMPLATE (decl
))
5448 tmpl
= DECL_TI_TEMPLATE (decl
);
5449 /* If DECL is a TYPE_DECL for a class-template, then there won't
5450 be DECL_LANG_SPECIFIC. The information equivalent to
5451 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5452 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5453 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5454 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5456 /* Since a template declaration already existed for this
5457 class-type, we must be redeclaring it here. Make sure
5458 that the redeclaration is valid. */
5459 redeclare_class_template (TREE_TYPE (decl
),
5460 current_template_parms
,
5461 current_template_constraints ());
5462 /* We don't need to create a new TEMPLATE_DECL; just use the
5463 one we already had. */
5464 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5468 tmpl
= build_template_decl (decl
, current_template_parms
,
5472 if (DECL_LANG_SPECIFIC (decl
)
5473 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5475 /* A specialization of a member template of a template
5477 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5478 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5479 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5485 tree a
, t
, current
, parms
;
5487 tree tinfo
= get_template_info (decl
);
5491 error ("template definition of non-template %q#D", decl
);
5492 return error_mark_node
;
5495 tmpl
= TI_TEMPLATE (tinfo
);
5497 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5498 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5499 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5500 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5504 /* The declaration is a specialization of a member
5505 template, declared outside the class. Therefore, the
5506 innermost template arguments will be NULL, so we
5507 replace them with the arguments determined by the
5508 earlier call to check_explicit_specialization. */
5509 args
= DECL_TI_ARGS (decl
);
5512 = build_template_decl (decl
, current_template_parms
,
5514 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5515 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5516 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5517 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5518 DECL_TEMPLATE_INFO (new_tmpl
)
5519 = build_template_info (tmpl
, args
);
5521 register_specialization (new_tmpl
,
5522 most_general_template (tmpl
),
5528 /* Make sure the template headers we got make sense. */
5530 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5531 i
= TMPL_PARMS_DEPTH (parms
);
5532 if (TMPL_ARGS_DEPTH (args
) != i
)
5534 error ("expected %d levels of template parms for %q#D, got %d",
5535 i
, decl
, TMPL_ARGS_DEPTH (args
));
5536 DECL_INTERFACE_KNOWN (decl
) = 1;
5537 return error_mark_node
;
5540 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5542 a
= TMPL_ARGS_LEVEL (args
, i
);
5543 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5545 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5547 if (current
== decl
)
5548 error ("got %d template parameters for %q#D",
5549 TREE_VEC_LENGTH (a
), decl
);
5551 error ("got %d template parameters for %q#T",
5552 TREE_VEC_LENGTH (a
), current
);
5553 error (" but %d required", TREE_VEC_LENGTH (t
));
5554 /* Avoid crash in import_export_decl. */
5555 DECL_INTERFACE_KNOWN (decl
) = 1;
5556 return error_mark_node
;
5559 if (current
== decl
)
5561 else if (current
== NULL_TREE
)
5562 /* Can happen in erroneous input. */
5565 current
= get_containing_scope (current
);
5568 /* Check that the parms are used in the appropriate qualifying scopes
5569 in the declarator. */
5570 if (!comp_template_args
5572 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5574 error ("template arguments to %qD do not match original "
5575 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5576 if (!uses_template_parms (TI_ARGS (tinfo
)))
5577 inform (input_location
, "use %<template<>%> for"
5578 " an explicit specialization");
5579 /* Avoid crash in import_export_decl. */
5580 DECL_INTERFACE_KNOWN (decl
) = 1;
5581 return error_mark_node
;
5585 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5586 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5588 /* Push template declarations for global functions and types. Note
5589 that we do not try to push a global template friend declared in a
5590 template class; such a thing may well depend on the template
5591 parameters of the class. */
5592 if (new_template_p
&& !ctx
5593 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5595 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5596 if (tmpl
== error_mark_node
)
5597 return error_mark_node
;
5599 /* Hide template friend classes that haven't been declared yet. */
5600 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5602 DECL_ANTICIPATED (tmpl
) = 1;
5603 DECL_FRIEND_P (tmpl
) = 1;
5609 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5611 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5613 /* Give template template parms a DECL_CONTEXT of the template
5614 for which they are a parameter. */
5615 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5616 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5618 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5619 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5620 DECL_CONTEXT (parm
) = tmpl
;
5623 if (TREE_CODE (decl
) == TYPE_DECL
5624 && TYPE_DECL_ALIAS_P (decl
)
5625 && complex_alias_template_p (tmpl
))
5626 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5629 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5630 back to its most general template. If TMPL is a specialization,
5631 ARGS may only have the innermost set of arguments. Add the missing
5632 argument levels if necessary. */
5633 if (DECL_TEMPLATE_INFO (tmpl
))
5634 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5636 info
= build_template_info (tmpl
, args
);
5638 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5639 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5643 retrofit_lang_decl (decl
);
5644 if (DECL_LANG_SPECIFIC (decl
))
5645 DECL_TEMPLATE_INFO (decl
) = info
;
5648 if (flag_implicit_templates
5650 && TREE_PUBLIC (decl
)
5651 && VAR_OR_FUNCTION_DECL_P (decl
))
5652 /* Set DECL_COMDAT on template instantiations; if we force
5653 them to be emitted by explicit instantiation or -frepo,
5654 mark_needed will tell cgraph to do the right thing. */
5655 DECL_COMDAT (decl
) = true;
5657 return DECL_TEMPLATE_RESULT (tmpl
);
5661 push_template_decl (tree decl
)
5663 return push_template_decl_real (decl
, false);
5666 /* FN is an inheriting constructor that inherits from the constructor
5667 template INHERITED; turn FN into a constructor template with a matching
5671 add_inherited_template_parms (tree fn
, tree inherited
)
5674 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5675 inner_parms
= copy_node (inner_parms
);
5677 = tree_cons (size_int (processing_template_decl
+ 1),
5678 inner_parms
, current_template_parms
);
5679 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5680 tree args
= template_parms_to_args (parms
);
5681 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5682 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5683 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5684 DECL_ARTIFICIAL (tmpl
) = true;
5685 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5689 /* Called when a class template TYPE is redeclared with the indicated
5690 template PARMS, e.g.:
5692 template <class T> struct S;
5693 template <class T> struct S {}; */
5696 redeclare_class_template (tree type
, tree parms
, tree cons
)
5702 if (!TYPE_TEMPLATE_INFO (type
))
5704 error ("%qT is not a template type", type
);
5708 tmpl
= TYPE_TI_TEMPLATE (type
);
5709 if (!PRIMARY_TEMPLATE_P (tmpl
))
5710 /* The type is nested in some template class. Nothing to worry
5711 about here; there are no new template parameters for the nested
5717 error ("template specifiers not specified in declaration of %qD",
5722 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5723 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5725 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5727 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5728 "redeclared with %d template parameter",
5729 "redeclared with %d template parameters",
5730 TREE_VEC_LENGTH (parms
));
5731 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5732 "previous declaration %qD used %d template parameter",
5733 "previous declaration %qD used %d template parameters",
5734 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5738 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5745 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5746 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5749 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5750 if (error_operand_p (tmpl_parm
))
5753 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5754 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5755 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5757 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5759 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5760 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5761 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5762 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5763 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5764 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5765 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5766 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5767 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5769 error ("template parameter %q+#D", tmpl_parm
);
5770 error ("redeclared here as %q#D", parm
);
5774 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5776 /* We have in [temp.param]:
5778 A template-parameter may not be given default arguments
5779 by two different declarations in the same scope. */
5780 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5781 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5782 "original definition appeared here");
5786 if (parm_default
!= NULL_TREE
)
5787 /* Update the previous template parameters (which are the ones
5788 that will really count) with the new default value. */
5789 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5790 else if (tmpl_default
!= NULL_TREE
)
5791 /* Update the new parameters, too; they'll be used as the
5792 parameters for any members. */
5793 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5795 /* Give each template template parm in this redeclaration a
5796 DECL_CONTEXT of the template for which they are a parameter. */
5797 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5799 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5800 DECL_CONTEXT (parm
) = tmpl
;
5803 if (TREE_CODE (parm
) == TYPE_DECL
)
5804 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5807 // Cannot redeclare a class template with a different set of constraints.
5808 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5810 error_at (input_location
, "redeclaration %q#D with different "
5811 "constraints", tmpl
);
5812 inform (DECL_SOURCE_LOCATION (tmpl
),
5813 "original declaration appeared here");
5819 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5820 to be used when the caller has already checked
5821 (processing_template_decl
5822 && !instantiation_dependent_expression_p (expr)
5823 && potential_constant_expression (expr))
5824 and cleared processing_template_decl. */
5827 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5829 return tsubst_copy_and_build (expr
,
5832 /*in_decl=*/NULL_TREE
,
5833 /*function_p=*/false,
5834 /*integral_constant_expression_p=*/true);
5837 /* Simplify EXPR if it is a non-dependent expression. Returns the
5838 (possibly simplified) expression. */
5841 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5843 if (expr
== NULL_TREE
)
5846 /* If we're in a template, but EXPR isn't value dependent, simplify
5847 it. We're supposed to treat:
5849 template <typename T> void f(T[1 + 1]);
5850 template <typename T> void f(T[2]);
5852 as two declarations of the same function, for example. */
5853 if (processing_template_decl
5854 && is_nondependent_constant_expression (expr
))
5856 processing_template_decl_sentinel s
;
5857 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5863 instantiate_non_dependent_expr (tree expr
)
5865 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5868 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5869 an uninstantiated expression. */
5872 instantiate_non_dependent_or_null (tree expr
)
5874 if (expr
== NULL_TREE
)
5876 if (processing_template_decl
)
5878 if (!is_nondependent_constant_expression (expr
))
5882 processing_template_decl_sentinel s
;
5883 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5889 /* True iff T is a specialization of a variable template. */
5892 variable_template_specialization_p (tree t
)
5894 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5896 tree tmpl
= DECL_TI_TEMPLATE (t
);
5897 return variable_template_p (tmpl
);
5900 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5901 template declaration, or a TYPE_DECL for an alias declaration. */
5904 alias_type_or_template_p (tree t
)
5908 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5911 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5912 || DECL_ALIAS_TEMPLATE_P (t
));
5915 /* Return TRUE iff T is a specialization of an alias template. */
5918 alias_template_specialization_p (const_tree t
)
5920 /* It's an alias template specialization if it's an alias and its
5921 TYPE_NAME is a specialization of a primary template. */
5922 if (TYPE_ALIAS_P (t
))
5923 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5924 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5929 /* An alias template is complex from a SFINAE perspective if a template-id
5930 using that alias can be ill-formed when the expansion is not, as with
5931 the void_t template. We determine this by checking whether the
5932 expansion for the alias template uses all its template parameters. */
5934 struct uses_all_template_parms_data
5941 uses_all_template_parms_r (tree t
, void *data_
)
5943 struct uses_all_template_parms_data
&data
5944 = *(struct uses_all_template_parms_data
*)data_
;
5945 tree idx
= get_template_parm_index (t
);
5947 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5948 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5953 complex_alias_template_p (const_tree tmpl
)
5955 struct uses_all_template_parms_data data
;
5956 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5957 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5958 data
.level
= TMPL_PARMS_DEPTH (parms
);
5959 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5960 data
.seen
= XALLOCAVEC (bool, len
);
5961 for (int i
= 0; i
< len
; ++i
)
5962 data
.seen
[i
] = false;
5964 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5965 for (int i
= 0; i
< len
; ++i
)
5971 /* Return TRUE iff T is a specialization of a complex alias template with
5972 dependent template-arguments. */
5975 dependent_alias_template_spec_p (const_tree t
)
5977 if (!alias_template_specialization_p (t
))
5980 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5981 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5984 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5985 if (!any_dependent_template_arguments_p (args
))
5991 /* Return the number of innermost template parameters in TMPL. */
5994 num_innermost_template_parms (tree tmpl
)
5996 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5997 return TREE_VEC_LENGTH (parms
);
6000 /* Return either TMPL or another template that it is equivalent to under DR
6001 1286: An alias that just changes the name of a template is equivalent to
6002 the other template. */
6005 get_underlying_template (tree tmpl
)
6007 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6008 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6010 /* Determine if the alias is equivalent to an underlying template. */
6011 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6012 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6016 tree underlying
= TI_TEMPLATE (tinfo
);
6017 if (!PRIMARY_TEMPLATE_P (underlying
)
6018 || (num_innermost_template_parms (tmpl
)
6019 != num_innermost_template_parms (underlying
)))
6022 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6023 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6024 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6027 /* Alias is equivalent. Strip it and repeat. */
6034 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6035 must be a reference-to-function or a pointer-to-function type, as specified
6036 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6037 and check that the resulting function has external linkage. */
6040 convert_nontype_argument_function (tree type
, tree expr
,
6041 tsubst_flags_t complain
)
6045 linkage_kind linkage
;
6047 fn
= instantiate_type (type
, fns
, tf_none
);
6048 if (fn
== error_mark_node
)
6049 return error_mark_node
;
6051 if (value_dependent_expression_p (fn
))
6054 fn_no_ptr
= strip_fnptr_conv (fn
);
6055 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6056 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6057 if (BASELINK_P (fn_no_ptr
))
6058 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6060 /* [temp.arg.nontype]/1
6062 A template-argument for a non-type, non-template template-parameter
6065 -- the address of an object or function with external [C++11: or
6066 internal] linkage. */
6068 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6070 if (complain
& tf_error
)
6072 error ("%qE is not a valid template argument for type %qT",
6074 if (TYPE_PTR_P (type
))
6075 inform (input_location
, "it must be the address of a function "
6076 "with external linkage");
6078 inform (input_location
, "it must be the name of a function with "
6079 "external linkage");
6084 linkage
= decl_linkage (fn_no_ptr
);
6085 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6087 if (complain
& tf_error
)
6089 if (cxx_dialect
>= cxx11
)
6090 error ("%qE is not a valid template argument for type %qT "
6091 "because %qD has no linkage",
6092 expr
, type
, fn_no_ptr
);
6094 error ("%qE is not a valid template argument for type %qT "
6095 "because %qD does not have external linkage",
6096 expr
, type
, fn_no_ptr
);
6102 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6103 fn
= build_address (fn
);
6104 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6105 fn
= build_nop (type
, fn
);
6110 /* Subroutine of convert_nontype_argument.
6111 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6112 Emit an error otherwise. */
6115 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6116 tsubst_flags_t complain
)
6118 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6119 tree orig_expr
= expr
;
6121 if (null_ptr_cst_p (expr
))
6123 if (TREE_CODE (expr
) == PTRMEM_CST
6124 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6125 PTRMEM_CST_CLASS (expr
)))
6127 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6129 if (processing_template_decl
6130 && TREE_CODE (expr
) == ADDR_EXPR
6131 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6133 if (complain
& tf_error
)
6135 error_at (loc
, "%qE is not a valid template argument for type %qT",
6137 if (TREE_CODE (expr
) != PTRMEM_CST
)
6138 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6140 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6145 /* Returns TRUE iff the address of OP is value-dependent.
6147 14.6.2.4 [temp.dep.temp]:
6148 A non-integral non-type template-argument is dependent if its type is
6149 dependent or it has either of the following forms
6152 and contains a nested-name-specifier which specifies a class-name that
6153 names a dependent type.
6155 We generalize this to just say that the address of a member of a
6156 dependent class is value-dependent; the above doesn't cover the
6157 address of a static data member named with an unqualified-id. */
6160 has_value_dependent_address (tree op
)
6162 /* We could use get_inner_reference here, but there's no need;
6163 this is only relevant for template non-type arguments, which
6164 can only be expressed as &id-expression. */
6167 tree ctx
= CP_DECL_CONTEXT (op
);
6168 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6175 /* The next set of functions are used for providing helpful explanatory
6176 diagnostics for failed overload resolution. Their messages should be
6177 indented by two spaces for consistency with the messages in
6181 unify_success (bool /*explain_p*/)
6186 /* Other failure functions should call this one, to provide a single function
6187 for setting a breakpoint on. */
6190 unify_invalid (bool /*explain_p*/)
6196 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6199 inform (input_location
,
6200 " couldn't deduce template parameter %qD", parm
);
6201 return unify_invalid (explain_p
);
6205 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6208 inform (input_location
,
6209 " types %qT and %qT have incompatible cv-qualifiers",
6211 return unify_invalid (explain_p
);
6215 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6218 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6219 return unify_invalid (explain_p
);
6223 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6226 inform (input_location
,
6227 " template parameter %qD is not a parameter pack, but "
6230 return unify_invalid (explain_p
);
6234 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6237 inform (input_location
,
6238 " template argument %qE does not match "
6239 "pointer-to-member constant %qE",
6241 return unify_invalid (explain_p
);
6245 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6248 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6249 return unify_invalid (explain_p
);
6253 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6256 inform (input_location
,
6257 " inconsistent parameter pack deduction with %qT and %qT",
6259 return unify_invalid (explain_p
);
6263 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6268 inform (input_location
,
6269 " deduced conflicting types for parameter %qT (%qT and %qT)",
6270 parm
, first
, second
);
6272 inform (input_location
,
6273 " deduced conflicting values for non-type parameter "
6274 "%qE (%qE and %qE)", parm
, first
, second
);
6276 return unify_invalid (explain_p
);
6280 unify_vla_arg (bool explain_p
, tree arg
)
6283 inform (input_location
,
6284 " variable-sized array type %qT is not "
6285 "a valid template argument",
6287 return unify_invalid (explain_p
);
6291 unify_method_type_error (bool explain_p
, tree arg
)
6294 inform (input_location
,
6295 " member function type %qT is not a valid template argument",
6297 return unify_invalid (explain_p
);
6301 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6306 inform_n (input_location
, wanted
,
6307 " candidate expects at least %d argument, %d provided",
6308 " candidate expects at least %d arguments, %d provided",
6311 inform_n (input_location
, wanted
,
6312 " candidate expects %d argument, %d provided",
6313 " candidate expects %d arguments, %d provided",
6316 return unify_invalid (explain_p
);
6320 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6322 return unify_arity (explain_p
, have
, wanted
);
6326 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6327 bool least_p
= false)
6329 return unify_arity (explain_p
, have
, wanted
, least_p
);
6333 unify_arg_conversion (bool explain_p
, tree to_type
,
6334 tree from_type
, tree arg
)
6337 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6338 " cannot convert %qE (type %qT) to type %qT",
6339 arg
, from_type
, to_type
);
6340 return unify_invalid (explain_p
);
6344 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6345 tree parm
, tree arg
)
6350 case tbr_ambiguous_baseclass
:
6351 inform (input_location
, " %qT is an ambiguous base class of %qT",
6355 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6358 return unify_invalid (explain_p
);
6362 unify_inconsistent_template_template_parameters (bool explain_p
)
6365 inform (input_location
,
6366 " template parameters of a template template argument are "
6367 "inconsistent with other deduced template arguments");
6368 return unify_invalid (explain_p
);
6372 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6375 inform (input_location
,
6376 " can't deduce a template for %qT from non-template type %qT",
6378 return unify_invalid (explain_p
);
6382 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6385 inform (input_location
,
6386 " template argument %qE does not match %qE", arg
, parm
);
6387 return unify_invalid (explain_p
);
6390 /* Attempt to convert the non-type template parameter EXPR to the
6391 indicated TYPE. If the conversion is successful, return the
6392 converted value. If the conversion is unsuccessful, return
6393 NULL_TREE if we issued an error message, or error_mark_node if we
6394 did not. We issue error messages for out-and-out bad template
6395 parameters, but not simply because the conversion failed, since we
6396 might be just trying to do argument deduction. Both TYPE and EXPR
6397 must be non-dependent.
6399 The conversion follows the special rules described in
6400 [temp.arg.nontype], and it is much more strict than an implicit
6403 This function is called twice for each template argument (see
6404 lookup_template_class for a more accurate description of this
6405 problem). This means that we need to handle expressions which
6406 are not valid in a C++ source, but can be created from the
6407 first call (for instance, casts to perform conversions). These
6408 hacks can go away after we fix the double coercion problem. */
6411 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6414 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6415 tree orig_expr
= expr
;
6417 /* Detect immediately string literals as invalid non-type argument.
6418 This special-case is not needed for correctness (we would easily
6419 catch this later), but only to provide better diagnostic for this
6420 common user mistake. As suggested by DR 100, we do not mention
6421 linkage issues in the diagnostic as this is not the point. */
6422 /* FIXME we're making this OK. */
6423 if (TREE_CODE (expr
) == STRING_CST
)
6425 if (complain
& tf_error
)
6426 error ("%qE is not a valid template argument for type %qT "
6427 "because string literals can never be used in this context",
6432 /* Add the ADDR_EXPR now for the benefit of
6433 value_dependent_expression_p. */
6434 if (TYPE_PTROBV_P (type
)
6435 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6437 expr
= decay_conversion (expr
, complain
);
6438 if (expr
== error_mark_node
)
6439 return error_mark_node
;
6442 /* If we are in a template, EXPR may be non-dependent, but still
6443 have a syntactic, rather than semantic, form. For example, EXPR
6444 might be a SCOPE_REF, rather than the VAR_DECL to which the
6445 SCOPE_REF refers. Preserving the qualifying scope is necessary
6446 so that access checking can be performed when the template is
6447 instantiated -- but here we need the resolved form so that we can
6448 convert the argument. */
6449 bool non_dep
= false;
6450 if (TYPE_REF_OBJ_P (type
)
6451 && has_value_dependent_address (expr
))
6452 /* If we want the address and it's value-dependent, don't fold. */;
6453 else if (processing_template_decl
6454 && is_nondependent_constant_expression (expr
))
6456 if (error_operand_p (expr
))
6457 return error_mark_node
;
6458 expr_type
= TREE_TYPE (expr
);
6460 /* If the argument is non-dependent, perform any conversions in
6461 non-dependent context as well. */
6462 processing_template_decl_sentinel
s (non_dep
);
6464 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6466 if (value_dependent_expression_p (expr
))
6467 expr
= canonicalize_expr_argument (expr
, complain
);
6469 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6470 to a non-type argument of "nullptr". */
6471 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6472 expr
= fold_simple (convert (type
, expr
));
6474 /* In C++11, integral or enumeration non-type template arguments can be
6475 arbitrary constant expressions. Pointer and pointer to
6476 member arguments can be general constant expressions that evaluate
6477 to a null value, but otherwise still need to be of a specific form. */
6478 if (cxx_dialect
>= cxx11
)
6480 if (TREE_CODE (expr
) == PTRMEM_CST
)
6481 /* A PTRMEM_CST is already constant, and a valid template
6482 argument for a parameter of pointer to member type, we just want
6483 to leave it in that form rather than lower it to a
6485 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6486 || cxx_dialect
>= cxx17
)
6488 /* C++17: A template-argument for a non-type template-parameter shall
6489 be a converted constant expression (8.20) of the type of the
6490 template-parameter. */
6491 expr
= build_converted_constant_expr (type
, expr
, complain
);
6492 if (expr
== error_mark_node
)
6493 return error_mark_node
;
6494 expr
= maybe_constant_value (expr
);
6495 expr
= convert_from_reference (expr
);
6497 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6499 tree folded
= maybe_constant_value (expr
);
6500 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6501 : null_member_pointer_value_p (folded
))
6506 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6507 expr
= mark_lvalue_use (expr
);
6509 expr
= mark_rvalue_use (expr
);
6511 /* HACK: Due to double coercion, we can get a
6512 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6513 which is the tree that we built on the first call (see
6514 below when coercing to reference to object or to reference to
6515 function). We just strip everything and get to the arg.
6516 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6518 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6520 tree probe_type
, probe
= expr
;
6521 if (REFERENCE_REF_P (probe
))
6522 probe
= TREE_OPERAND (probe
, 0);
6523 probe_type
= TREE_TYPE (probe
);
6524 if (TREE_CODE (probe
) == NOP_EXPR
)
6526 /* ??? Maybe we could use convert_from_reference here, but we
6527 would need to relax its constraints because the NOP_EXPR
6528 could actually change the type to something more cv-qualified,
6529 and this is not folded by convert_from_reference. */
6530 tree addr
= TREE_OPERAND (probe
, 0);
6531 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6532 && TREE_CODE (addr
) == ADDR_EXPR
6533 && TYPE_PTR_P (TREE_TYPE (addr
))
6534 && (same_type_ignoring_top_level_qualifiers_p
6535 (TREE_TYPE (probe_type
),
6536 TREE_TYPE (TREE_TYPE (addr
)))))
6538 expr
= TREE_OPERAND (addr
, 0);
6539 expr_type
= TREE_TYPE (probe_type
);
6544 /* [temp.arg.nontype]/5, bullet 1
6546 For a non-type template-parameter of integral or enumeration type,
6547 integral promotions (_conv.prom_) and integral conversions
6548 (_conv.integral_) are applied. */
6549 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6551 if (cxx_dialect
< cxx11
)
6553 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6554 t
= maybe_constant_value (t
);
6555 if (t
!= error_mark_node
)
6559 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6560 return error_mark_node
;
6562 /* Notice that there are constant expressions like '4 % 0' which
6563 do not fold into integer constants. */
6564 if (TREE_CODE (expr
) != INTEGER_CST
6565 && !value_dependent_expression_p (expr
))
6567 if (complain
& tf_error
)
6569 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6570 if (!require_potential_constant_expression (expr
))
6571 expr
= error_mark_node
;
6573 expr
= cxx_constant_value (expr
);
6574 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6575 inform (loc
, "in template argument for type %qT ", type
);
6576 if (expr
== error_mark_node
)
6578 /* else cxx_constant_value complained but gave us
6579 a real constant, so go ahead. */
6580 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6586 /* Avoid typedef problems. */
6587 if (TREE_TYPE (expr
) != type
)
6588 expr
= fold_convert (type
, expr
);
6590 /* [temp.arg.nontype]/5, bullet 2
6592 For a non-type template-parameter of type pointer to object,
6593 qualification conversions (_conv.qual_) and the array-to-pointer
6594 conversion (_conv.array_) are applied. */
6595 else if (TYPE_PTROBV_P (type
))
6597 tree decayed
= expr
;
6599 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6600 decay_conversion or an explicit cast. If it's a problematic cast,
6601 we'll complain about it below. */
6602 if (TREE_CODE (expr
) == NOP_EXPR
)
6606 if (TREE_CODE (probe
) == ADDR_EXPR
6607 && TYPE_PTR_P (TREE_TYPE (probe
)))
6610 expr_type
= TREE_TYPE (expr
);
6614 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6616 A template-argument for a non-type, non-template template-parameter
6617 shall be one of: [...]
6619 -- the name of a non-type template-parameter;
6620 -- the address of an object or function with external linkage, [...]
6621 expressed as "& id-expression" where the & is optional if the name
6622 refers to a function or array, or if the corresponding
6623 template-parameter is a reference.
6625 Here, we do not care about functions, as they are invalid anyway
6626 for a parameter of type pointer-to-object. */
6628 if (value_dependent_expression_p (expr
))
6629 /* Non-type template parameters are OK. */
6631 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6632 /* Null pointer values are OK in C++11. */;
6633 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6637 if (complain
& tf_error
)
6638 error ("%qD is not a valid template argument "
6639 "because %qD is a variable, not the address of "
6640 "a variable", orig_expr
, expr
);
6643 if (POINTER_TYPE_P (expr_type
))
6645 if (complain
& tf_error
)
6646 error ("%qE is not a valid template argument for %qT "
6647 "because it is not the address of a variable",
6651 /* Other values, like integer constants, might be valid
6652 non-type arguments of some other type. */
6653 return error_mark_node
;
6657 tree decl
= TREE_OPERAND (expr
, 0);
6661 if (complain
& tf_error
)
6662 error ("%qE is not a valid template argument of type %qT "
6663 "because %qE is not a variable", orig_expr
, type
, decl
);
6666 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6668 if (complain
& tf_error
)
6669 error ("%qE is not a valid template argument of type %qT "
6670 "because %qD does not have external linkage",
6671 orig_expr
, type
, decl
);
6674 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6675 && decl_linkage (decl
) == lk_none
)
6677 if (complain
& tf_error
)
6678 error ("%qE is not a valid template argument of type %qT "
6679 "because %qD has no linkage", orig_expr
, type
, decl
);
6682 /* C++17: For a non-type template-parameter of reference or pointer
6683 type, the value of the constant expression shall not refer to (or
6684 for a pointer type, shall not be the address of):
6685 * a subobject (4.5),
6686 * a temporary object (15.2),
6687 * a string literal (5.13.5),
6688 * the result of a typeid expression (8.2.8), or
6689 * a predefined __func__ variable (11.4.1). */
6690 else if (DECL_ARTIFICIAL (decl
))
6692 if (complain
& tf_error
)
6693 error ("the address of %qD is not a valid template argument",
6697 else if (!same_type_ignoring_top_level_qualifiers_p
6698 (strip_array_types (TREE_TYPE (type
)),
6699 strip_array_types (TREE_TYPE (decl
))))
6701 if (complain
& tf_error
)
6702 error ("the address of the %qT subobject of %qD is not a "
6703 "valid template argument", TREE_TYPE (type
), decl
);
6706 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6708 if (complain
& tf_error
)
6709 error ("the address of %qD is not a valid template argument "
6710 "because it does not have static storage duration",
6718 expr
= perform_qualification_conversions (type
, expr
);
6719 if (expr
== error_mark_node
)
6720 return error_mark_node
;
6722 /* [temp.arg.nontype]/5, bullet 3
6724 For a non-type template-parameter of type reference to object, no
6725 conversions apply. The type referred to by the reference may be more
6726 cv-qualified than the (otherwise identical) type of the
6727 template-argument. The template-parameter is bound directly to the
6728 template-argument, which must be an lvalue. */
6729 else if (TYPE_REF_OBJ_P (type
))
6731 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6733 return error_mark_node
;
6735 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6737 if (complain
& tf_error
)
6738 error ("%qE is not a valid template argument for type %qT "
6739 "because of conflicts in cv-qualification", expr
, type
);
6743 if (!lvalue_p (expr
))
6745 if (complain
& tf_error
)
6746 error ("%qE is not a valid template argument for type %qT "
6747 "because it is not an lvalue", expr
, type
);
6751 /* [temp.arg.nontype]/1
6753 A template-argument for a non-type, non-template template-parameter
6754 shall be one of: [...]
6756 -- the address of an object or function with external linkage. */
6757 if (INDIRECT_REF_P (expr
)
6758 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6760 expr
= TREE_OPERAND (expr
, 0);
6763 if (complain
& tf_error
)
6764 error ("%q#D is not a valid template argument for type %qT "
6765 "because a reference variable does not have a constant "
6766 "address", expr
, type
);
6771 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6772 && value_dependent_expression_p (expr
))
6773 /* OK, dependent reference. We don't want to ask whether a DECL is
6774 itself value-dependent, since what we want here is its address. */;
6779 if (complain
& tf_error
)
6780 error ("%qE is not a valid template argument for type %qT "
6781 "because it is not an object with linkage",
6786 /* DR 1155 allows internal linkage in C++11 and up. */
6787 linkage_kind linkage
= decl_linkage (expr
);
6788 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6790 if (complain
& tf_error
)
6791 error ("%qE is not a valid template argument for type %qT "
6792 "because object %qD does not have linkage",
6797 expr
= build_address (expr
);
6800 if (!same_type_p (type
, TREE_TYPE (expr
)))
6801 expr
= build_nop (type
, expr
);
6803 /* [temp.arg.nontype]/5, bullet 4
6805 For a non-type template-parameter of type pointer to function, only
6806 the function-to-pointer conversion (_conv.func_) is applied. If the
6807 template-argument represents a set of overloaded functions (or a
6808 pointer to such), the matching function is selected from the set
6810 else if (TYPE_PTRFN_P (type
))
6812 /* If the argument is a template-id, we might not have enough
6813 context information to decay the pointer. */
6814 if (!type_unknown_p (expr_type
))
6816 expr
= decay_conversion (expr
, complain
);
6817 if (expr
== error_mark_node
)
6818 return error_mark_node
;
6821 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6822 /* Null pointer values are OK in C++11. */
6823 return perform_qualification_conversions (type
, expr
);
6825 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6826 if (!expr
|| expr
== error_mark_node
)
6829 /* [temp.arg.nontype]/5, bullet 5
6831 For a non-type template-parameter of type reference to function, no
6832 conversions apply. If the template-argument represents a set of
6833 overloaded functions, the matching function is selected from the set
6835 else if (TYPE_REFFN_P (type
))
6837 if (TREE_CODE (expr
) == ADDR_EXPR
)
6839 if (complain
& tf_error
)
6841 error ("%qE is not a valid template argument for type %qT "
6842 "because it is a pointer", expr
, type
);
6843 inform (input_location
, "try using %qE instead",
6844 TREE_OPERAND (expr
, 0));
6849 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6850 if (!expr
|| expr
== error_mark_node
)
6853 /* [temp.arg.nontype]/5, bullet 6
6855 For a non-type template-parameter of type pointer to member function,
6856 no conversions apply. If the template-argument represents a set of
6857 overloaded member functions, the matching member function is selected
6858 from the set (_over.over_). */
6859 else if (TYPE_PTRMEMFUNC_P (type
))
6861 expr
= instantiate_type (type
, expr
, tf_none
);
6862 if (expr
== error_mark_node
)
6863 return error_mark_node
;
6865 /* [temp.arg.nontype] bullet 1 says the pointer to member
6866 expression must be a pointer-to-member constant. */
6867 if (!value_dependent_expression_p (expr
)
6868 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6871 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6872 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6873 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6874 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6876 /* [temp.arg.nontype]/5, bullet 7
6878 For a non-type template-parameter of type pointer to data member,
6879 qualification conversions (_conv.qual_) are applied. */
6880 else if (TYPE_PTRDATAMEM_P (type
))
6882 /* [temp.arg.nontype] bullet 1 says the pointer to member
6883 expression must be a pointer-to-member constant. */
6884 if (!value_dependent_expression_p (expr
)
6885 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6888 expr
= perform_qualification_conversions (type
, expr
);
6889 if (expr
== error_mark_node
)
6892 else if (NULLPTR_TYPE_P (type
))
6894 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6896 if (complain
& tf_error
)
6897 error ("%qE is not a valid template argument for type %qT "
6898 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6903 /* A template non-type parameter must be one of the above. */
6907 /* Sanity check: did we actually convert the argument to the
6909 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6910 (type
, TREE_TYPE (expr
)));
6911 return convert_from_reference (expr
);
6914 /* Subroutine of coerce_template_template_parms, which returns 1 if
6915 PARM_PARM and ARG_PARM match using the rule for the template
6916 parameters of template template parameters. Both PARM and ARG are
6917 template parameters; the rest of the arguments are the same as for
6918 coerce_template_template_parms.
6921 coerce_template_template_parm (tree parm
,
6923 tsubst_flags_t complain
,
6927 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6928 || parm
== NULL_TREE
|| error_operand_p (parm
))
6931 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6934 switch (TREE_CODE (parm
))
6937 /* We encounter instantiations of templates like
6938 template <template <template <class> class> class TT>
6941 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6942 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6944 if (!coerce_template_template_parms
6945 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6951 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6952 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6953 /* Argument is a parameter pack but parameter is not. */
6958 /* The tsubst call is used to handle cases such as
6960 template <int> class C {};
6961 template <class T, template <T> class TT> class D {};
6964 i.e. the parameter list of TT depends on earlier parameters. */
6965 if (!uses_template_parms (TREE_TYPE (arg
)))
6967 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6968 if (!uses_template_parms (t
)
6969 && !same_type_p (t
, TREE_TYPE (arg
)))
6973 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6974 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6975 /* Argument is a parameter pack but parameter is not. */
6987 /* Coerce template argument list ARGLIST for use with template
6988 template-parameter TEMPL. */
6991 coerce_template_args_for_ttp (tree templ
, tree arglist
,
6992 tsubst_flags_t complain
)
6994 /* Consider an example where a template template parameter declared as
6996 template <class T, class U = std::allocator<T> > class TT
6998 The template parameter level of T and U are one level larger than
6999 of TT. To proper process the default argument of U, say when an
7000 instantiation `TT<int>' is seen, we need to build the full
7001 arguments containing {int} as the innermost level. Outer levels,
7002 available when not appearing as default template argument, can be
7003 obtained from the arguments of the enclosing template.
7005 Suppose that TT is later substituted with std::vector. The above
7006 instantiation is `TT<int, std::allocator<T> >' with TT at
7007 level 1, and T at level 2, while the template arguments at level 1
7008 becomes {std::vector} and the inner level 2 is {int}. */
7010 tree outer
= DECL_CONTEXT (templ
);
7013 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7014 /* We want arguments for the partial specialization, not arguments for
7015 the primary template. */
7016 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7018 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7020 else if (current_template_parms
)
7022 /* This is an argument of the current template, so we haven't set
7023 DECL_CONTEXT yet. */
7024 tree relevant_template_parms
;
7026 /* Parameter levels that are greater than the level of the given
7027 template template parm are irrelevant. */
7028 relevant_template_parms
= current_template_parms
;
7029 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7030 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7031 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7033 outer
= template_parms_to_args (relevant_template_parms
);
7037 arglist
= add_to_template_args (outer
, arglist
);
7039 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7040 return coerce_template_parms (parmlist
, arglist
, templ
,
7042 /*require_all_args=*/true,
7043 /*use_default_args=*/true);
7046 /* A cache of template template parameters with match-all default
7048 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7050 store_defaulted_ttp (tree v
, tree t
)
7052 if (!defaulted_ttp_cache
)
7053 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7054 defaulted_ttp_cache
->put (v
, t
);
7057 lookup_defaulted_ttp (tree v
)
7059 if (defaulted_ttp_cache
)
7060 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7065 /* T is a bound template template-parameter. Copy its arguments into default
7066 arguments of the template template-parameter's template parameters. */
7069 add_defaults_to_ttp (tree otmpl
)
7071 if (tree c
= lookup_defaulted_ttp (otmpl
))
7074 tree ntmpl
= copy_node (otmpl
);
7076 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7077 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7078 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7079 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7080 TYPE_NAME (ntype
) = ntmpl
;
7081 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7083 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7084 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7085 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7086 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7088 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7089 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7090 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7091 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7092 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7094 tree o
= TREE_VEC_ELT (vec
, i
);
7095 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7097 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7098 TREE_PURPOSE (n
) = any_targ_node
;
7102 store_defaulted_ttp (otmpl
, ntmpl
);
7106 /* ARG is a bound potential template template-argument, and PARGS is a list
7107 of arguments for the corresponding template template-parameter. Adjust
7108 PARGS as appropriate for application to ARG's template, and if ARG is a
7109 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7110 arguments to the template template parameter. */
7113 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7115 ++processing_template_decl
;
7116 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7117 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7119 /* When comparing two template template-parameters in partial ordering,
7120 rewrite the one currently being used as an argument to have default
7121 arguments for all parameters. */
7122 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7123 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7124 if (pargs
!= error_mark_node
)
7125 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7126 TYPE_TI_ARGS (arg
));
7131 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7132 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7133 /*require_all*/true,
7134 /*use_default*/true);
7136 --processing_template_decl
;
7140 /* Subroutine of unify for the case when PARM is a
7141 BOUND_TEMPLATE_TEMPLATE_PARM. */
7144 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7147 tree parmvec
= TYPE_TI_ARGS (parm
);
7148 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7150 /* The template template parm might be variadic and the argument
7151 not, so flatten both argument lists. */
7152 parmvec
= expand_template_argument_pack (parmvec
);
7153 argvec
= expand_template_argument_pack (argvec
);
7157 /* In keeping with P0522R0, adjust P's template arguments
7158 to apply to A's template; then flatten it again. */
7159 tree nparmvec
= parmvec
;
7160 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7161 nparmvec
= expand_template_argument_pack (nparmvec
);
7163 if (unify (tparms
, targs
, nparmvec
, argvec
,
7164 UNIFY_ALLOW_NONE
, explain_p
))
7167 /* If the P0522 adjustment eliminated a pack expansion, deduce
7170 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7171 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7172 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7177 /* Deduce arguments T, i from TT<T> or TT<i>.
7178 We check each element of PARMVEC and ARGVEC individually
7179 rather than the whole TREE_VEC since they can have
7180 different number of elements, which is allowed under N2555. */
7182 int len
= TREE_VEC_LENGTH (parmvec
);
7184 /* Check if the parameters end in a pack, making them
7186 int parm_variadic_p
= 0;
7188 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7189 parm_variadic_p
= 1;
7191 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7192 /* If the template argument list of P contains a pack
7193 expansion that is not the last template argument, the
7194 entire template argument list is a non-deduced
7196 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7197 return unify_success (explain_p
);
7199 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7200 return unify_too_few_arguments (explain_p
,
7201 TREE_VEC_LENGTH (argvec
), len
);
7203 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7204 if (unify (tparms
, targs
,
7205 TREE_VEC_ELT (parmvec
, i
),
7206 TREE_VEC_ELT (argvec
, i
),
7207 UNIFY_ALLOW_NONE
, explain_p
))
7211 && unify_pack_expansion (tparms
, targs
,
7214 /*subr=*/true, explain_p
))
7221 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7222 template template parameters. Both PARM_PARMS and ARG_PARMS are
7223 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7226 Consider the example:
7227 template <class T> class A;
7228 template<template <class U> class TT> class B;
7230 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7231 the parameters to A, and OUTER_ARGS contains A. */
7234 coerce_template_template_parms (tree parm_parms
,
7236 tsubst_flags_t complain
,
7240 int nparms
, nargs
, i
;
7244 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7245 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7247 nparms
= TREE_VEC_LENGTH (parm_parms
);
7248 nargs
= TREE_VEC_LENGTH (arg_parms
);
7252 /* P0522R0: A template template-parameter P is at least as specialized as
7253 a template template-argument A if, given the following rewrite to two
7254 function templates, the function template corresponding to P is at
7255 least as specialized as the function template corresponding to A
7256 according to the partial ordering rules for function templates
7257 ([temp.func.order]). Given an invented class template X with the
7258 template parameter list of A (including default arguments):
7260 * Each of the two function templates has the same template parameters,
7261 respectively, as P or A.
7263 * Each function template has a single function parameter whose type is
7264 a specialization of X with template arguments corresponding to the
7265 template parameters from the respective function template where, for
7266 each template parameter PP in the template parameter list of the
7267 function template, a corresponding template argument AA is formed. If
7268 PP declares a parameter pack, then AA is the pack expansion
7269 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7271 If the rewrite produces an invalid type, then P is not at least as
7272 specialized as A. */
7274 /* So coerce P's args to apply to A's parms, and then deduce between A's
7275 args and the converted args. If that succeeds, A is at least as
7276 specialized as P, so they match.*/
7277 tree pargs
= template_parms_level_to_args (parm_parms
);
7278 ++processing_template_decl
;
7279 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7280 /*require_all*/true, /*use_default*/true);
7281 --processing_template_decl
;
7282 if (pargs
!= error_mark_node
)
7284 tree targs
= make_tree_vec (nargs
);
7285 tree aargs
= template_parms_level_to_args (arg_parms
);
7286 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7292 /* Determine whether we have a parameter pack at the end of the
7293 template template parameter's template parameter list. */
7294 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7296 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7298 if (error_operand_p (parm
))
7301 switch (TREE_CODE (parm
))
7305 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7310 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7320 && !(variadic_p
&& nargs
>= nparms
- 1))
7323 /* Check all of the template parameters except the parameter pack at
7324 the end (if any). */
7325 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7327 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7328 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7331 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7332 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7334 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7342 /* Check each of the template parameters in the template
7343 argument against the template parameter pack at the end of
7344 the template template parameter. */
7345 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7348 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7350 for (; i
< nargs
; ++i
)
7352 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7355 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7357 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7366 /* Verifies that the deduced template arguments (in TARGS) for the
7367 template template parameters (in TPARMS) represent valid bindings,
7368 by comparing the template parameter list of each template argument
7369 to the template parameter list of its corresponding template
7370 template parameter, in accordance with DR150. This
7371 routine can only be called after all template arguments have been
7372 deduced. It will return TRUE if all of the template template
7373 parameter bindings are okay, FALSE otherwise. */
7375 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7377 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7380 /* We're dealing with template parms in this process. */
7381 ++processing_template_decl
;
7383 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7385 for (i
= 0; i
< ntparms
; ++i
)
7387 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7388 tree targ
= TREE_VEC_ELT (targs
, i
);
7390 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7392 tree packed_args
= NULL_TREE
;
7395 if (ARGUMENT_PACK_P (targ
))
7397 /* Look inside the argument pack. */
7398 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7399 len
= TREE_VEC_LENGTH (packed_args
);
7402 for (idx
= 0; idx
< len
; ++idx
)
7404 tree targ_parms
= NULL_TREE
;
7407 /* Extract the next argument from the argument
7409 targ
= TREE_VEC_ELT (packed_args
, idx
);
7411 if (PACK_EXPANSION_P (targ
))
7412 /* Look at the pattern of the pack expansion. */
7413 targ
= PACK_EXPANSION_PATTERN (targ
);
7415 /* Extract the template parameters from the template
7417 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7418 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7419 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7420 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7422 /* Verify that we can coerce the template template
7423 parameters from the template argument to the template
7424 parameter. This requires an exact match. */
7426 && !coerce_template_template_parms
7427 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7442 --processing_template_decl
;
7446 /* Since type attributes aren't mangled, we need to strip them from
7447 template type arguments. */
7450 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7452 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7454 bool removed_attributes
= false;
7455 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7456 if (removed_attributes
7457 && (complain
& tf_warning
))
7458 warning (OPT_Wignored_attributes
,
7459 "ignoring attributes on template argument %qT", arg
);
7463 /* And from inside dependent non-type arguments like sizeof(Type). */
7466 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7468 if (!arg
|| arg
== error_mark_node
)
7470 bool removed_attributes
= false;
7471 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7472 if (removed_attributes
7473 && (complain
& tf_warning
))
7474 warning (OPT_Wignored_attributes
,
7475 "ignoring attributes in template argument %qE", arg
);
7479 // A template declaration can be substituted for a constrained
7480 // template template parameter only when the argument is more
7481 // constrained than the parameter.
7483 is_compatible_template_arg (tree parm
, tree arg
)
7485 tree parm_cons
= get_constraints (parm
);
7487 /* For now, allow constrained template template arguments
7488 and unconstrained template template parameters. */
7489 if (parm_cons
== NULL_TREE
)
7492 tree arg_cons
= get_constraints (arg
);
7494 // If the template parameter is constrained, we need to rewrite its
7495 // constraints in terms of the ARG's template parameters. This ensures
7496 // that all of the template parameter types will have the same depth.
7498 // Note that this is only valid when coerce_template_template_parm is
7499 // true for the innermost template parameters of PARM and ARG. In other
7500 // words, because coercion is successful, this conversion will be valid.
7503 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7504 parm_cons
= tsubst_constraint_info (parm_cons
,
7505 INNERMOST_TEMPLATE_ARGS (args
),
7506 tf_none
, NULL_TREE
);
7507 if (parm_cons
== error_mark_node
)
7511 return subsumes (parm_cons
, arg_cons
);
7514 // Convert a placeholder argument into a binding to the original
7515 // parameter. The original parameter is saved as the TREE_TYPE of
7518 convert_wildcard_argument (tree parm
, tree arg
)
7520 TREE_TYPE (arg
) = parm
;
7524 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7525 because one of them is dependent. But we need to represent the
7526 conversion for the benefit of cp_tree_equal. */
7529 maybe_convert_nontype_argument (tree type
, tree arg
)
7531 /* Auto parms get no conversion. */
7532 if (type_uses_auto (type
))
7534 /* We don't need or want to add this conversion now if we're going to use the
7535 argument for deduction. */
7536 if (value_dependent_expression_p (arg
))
7539 type
= cv_unqualified (type
);
7540 tree argtype
= TREE_TYPE (arg
);
7541 if (same_type_p (type
, argtype
))
7544 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7545 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7549 /* Convert the indicated template ARG as necessary to match the
7550 indicated template PARM. Returns the converted ARG, or
7551 error_mark_node if the conversion was unsuccessful. Error and
7552 warning messages are issued under control of COMPLAIN. This
7553 conversion is for the Ith parameter in the parameter list. ARGS is
7554 the full set of template arguments deduced so far. */
7557 convert_template_argument (tree parm
,
7560 tsubst_flags_t complain
,
7566 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7568 if (parm
== error_mark_node
)
7569 return error_mark_node
;
7571 /* Trivially convert placeholders. */
7572 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7573 return convert_wildcard_argument (parm
, arg
);
7575 if (arg
== any_targ_node
)
7578 if (TREE_CODE (arg
) == TREE_LIST
7579 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7581 /* The template argument was the name of some
7582 member function. That's usually
7583 invalid, but static members are OK. In any
7584 case, grab the underlying fields/functions
7585 and issue an error later if required. */
7586 orig_arg
= TREE_VALUE (arg
);
7587 TREE_TYPE (arg
) = unknown_type_node
;
7592 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7593 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7594 || requires_tmpl_type
);
7596 /* When determining whether an argument pack expansion is a template,
7597 look at the pattern. */
7598 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7599 arg
= PACK_EXPANSION_PATTERN (arg
);
7601 /* Deal with an injected-class-name used as a template template arg. */
7602 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7604 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7605 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7607 if (cxx_dialect
>= cxx11
)
7608 /* OK under DR 1004. */;
7609 else if (complain
& tf_warning_or_error
)
7610 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7611 " used as template template argument", TYPE_NAME (arg
));
7612 else if (flag_pedantic_errors
)
7620 ((TREE_CODE (arg
) == TEMPLATE_DECL
7621 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7622 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7623 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7624 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7627 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7628 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7629 arg
= TYPE_STUB_DECL (arg
);
7631 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7633 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7634 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7636 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7638 if (complain
& tf_error
)
7639 error ("invalid use of destructor %qE as a type", orig_arg
);
7640 return error_mark_node
;
7643 permerror (input_location
,
7644 "to refer to a type member of a template parameter, "
7645 "use %<typename %E%>", orig_arg
);
7647 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7648 TREE_OPERAND (arg
, 1),
7654 if (is_type
!= requires_type
)
7658 if (complain
& tf_error
)
7660 error ("type/value mismatch at argument %d in template "
7661 "parameter list for %qD",
7664 inform (input_location
,
7665 " expected a constant of type %qT, got %qT",
7667 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7668 else if (requires_tmpl_type
)
7669 inform (input_location
,
7670 " expected a class template, got %qE", orig_arg
);
7672 inform (input_location
,
7673 " expected a type, got %qE", orig_arg
);
7676 return error_mark_node
;
7678 if (is_tmpl_type
^ requires_tmpl_type
)
7680 if (in_decl
&& (complain
& tf_error
))
7682 error ("type/value mismatch at argument %d in template "
7683 "parameter list for %qD",
7686 inform (input_location
,
7687 " expected a type, got %qT", DECL_NAME (arg
));
7689 inform (input_location
,
7690 " expected a class template, got %qT", orig_arg
);
7692 return error_mark_node
;
7695 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7696 /* We already did the appropriate conversion when packing args. */
7700 if (requires_tmpl_type
)
7702 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7703 /* The number of argument required is not known yet.
7704 Just accept it for now. */
7708 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7711 /* Strip alias templates that are equivalent to another
7713 arg
= get_underlying_template (arg
);
7714 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7716 if (coerce_template_template_parms (parmparm
, argparm
,
7722 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7724 if (val
!= error_mark_node
)
7726 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7727 val
= TREE_TYPE (val
);
7728 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7729 val
= make_pack_expansion (val
, complain
);
7734 if (in_decl
&& (complain
& tf_error
))
7736 error ("type/value mismatch at argument %d in "
7737 "template parameter list for %qD",
7739 inform (input_location
,
7740 " expected a template of type %qD, got %qT",
7744 val
= error_mark_node
;
7747 // Check that the constraints are compatible before allowing the
7749 if (val
!= error_mark_node
)
7750 if (!is_compatible_template_arg (parm
, arg
))
7752 if (in_decl
&& (complain
& tf_error
))
7754 error ("constraint mismatch at argument %d in "
7755 "template parameter list for %qD",
7757 inform (input_location
, " expected %qD but got %qD",
7760 val
= error_mark_node
;
7766 /* We only form one instance of each template specialization.
7767 Therefore, if we use a non-canonical variant (i.e., a
7768 typedef), any future messages referring to the type will use
7769 the typedef, which is confusing if those future uses do not
7770 themselves also use the typedef. */
7772 val
= canonicalize_type_argument (val
, complain
);
7776 tree t
= TREE_TYPE (parm
);
7778 if (tree a
= type_uses_auto (t
))
7780 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7781 if (t
== error_mark_node
)
7782 return error_mark_node
;
7785 t
= tsubst (t
, args
, complain
, in_decl
);
7787 if (invalid_nontype_parm_type_p (t
, complain
))
7788 return error_mark_node
;
7790 if (!type_dependent_expression_p (orig_arg
)
7791 && !uses_template_parms (t
))
7792 /* We used to call digest_init here. However, digest_init
7793 will report errors, which we don't want when complain
7794 is zero. More importantly, digest_init will try too
7795 hard to convert things: for example, `0' should not be
7796 converted to pointer type at this point according to
7797 the standard. Accepting this is not merely an
7798 extension, since deciding whether or not these
7799 conversions can occur is part of determining which
7800 function template to call, or whether a given explicit
7801 argument specification is valid. */
7802 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7805 val
= canonicalize_expr_argument (orig_arg
, complain
);
7806 val
= maybe_convert_nontype_argument (t
, val
);
7810 if (val
== NULL_TREE
)
7811 val
= error_mark_node
;
7812 else if (val
== error_mark_node
&& (complain
& tf_error
))
7813 error ("could not convert template argument %qE from %qT to %qT",
7814 orig_arg
, TREE_TYPE (orig_arg
), t
);
7816 if (INDIRECT_REF_P (val
))
7818 /* Reject template arguments that are references to built-in
7819 functions with no library fallbacks. */
7820 const_tree inner
= TREE_OPERAND (val
, 0);
7821 const_tree innertype
= TREE_TYPE (inner
);
7823 && TREE_CODE (innertype
) == REFERENCE_TYPE
7824 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7825 && 0 < TREE_OPERAND_LENGTH (inner
)
7826 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7827 return error_mark_node
;
7830 if (TREE_CODE (val
) == SCOPE_REF
)
7832 /* Strip typedefs from the SCOPE_REF. */
7833 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7834 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7836 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7837 QUALIFIED_NAME_IS_TEMPLATE (val
));
7844 /* Coerces the remaining template arguments in INNER_ARGS (from
7845 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7846 Returns the coerced argument pack. PARM_IDX is the position of this
7847 parameter in the template parameter list. ARGS is the original
7848 template argument list. */
7850 coerce_template_parameter_pack (tree parms
,
7858 tsubst_flags_t complain
)
7860 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7861 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7864 tree packed_parms
= NULL_TREE
;
7866 if (arg_idx
> nargs
)
7869 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7871 /* When the template parameter is a non-type template parameter pack
7872 or template template parameter pack whose type or template
7873 parameters use parameter packs, we know exactly how many arguments
7874 we are looking for. Build a vector of the instantiated decls for
7875 these template parameters in PACKED_PARMS. */
7876 /* We can't use make_pack_expansion here because it would interpret a
7877 _DECL as a use rather than a declaration. */
7878 tree decl
= TREE_VALUE (parm
);
7879 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7880 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7881 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7882 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7884 TREE_VEC_LENGTH (args
)--;
7885 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7886 TREE_VEC_LENGTH (args
)++;
7888 if (packed_parms
== error_mark_node
)
7889 return error_mark_node
;
7891 /* If we're doing a partial instantiation of a member template,
7892 verify that all of the types used for the non-type
7893 template parameter pack are, in fact, valid for non-type
7894 template parameters. */
7896 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7898 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7899 for (j
= 0; j
< len
; ++j
)
7901 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7902 if (invalid_nontype_parm_type_p (t
, complain
))
7903 return error_mark_node
;
7905 /* We don't know how many args we have yet, just
7906 use the unconverted ones for now. */
7910 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7912 /* Check if we have a placeholder pack, which indicates we're
7913 in the context of a introduction list. In that case we want
7914 to match this pack to the single placeholder. */
7915 else if (arg_idx
< nargs
7916 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7917 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7919 nargs
= arg_idx
+ 1;
7920 packed_args
= make_tree_vec (1);
7923 packed_args
= make_tree_vec (nargs
- arg_idx
);
7925 /* Convert the remaining arguments, which will be a part of the
7926 parameter pack "parm". */
7927 int first_pack_arg
= arg_idx
;
7928 for (; arg_idx
< nargs
; ++arg_idx
)
7930 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7931 tree actual_parm
= TREE_VALUE (parm
);
7932 int pack_idx
= arg_idx
- first_pack_arg
;
7936 /* Once we've packed as many args as we have types, stop. */
7937 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7939 else if (PACK_EXPANSION_P (arg
))
7940 /* We don't know how many args we have yet, just
7941 use the unconverted ones for now. */
7944 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7947 if (arg
== error_mark_node
)
7949 if (complain
& tf_error
)
7950 error ("template argument %d is invalid", arg_idx
+ 1);
7953 arg
= convert_template_argument (actual_parm
,
7954 arg
, new_args
, complain
, parm_idx
,
7956 if (arg
== error_mark_node
)
7958 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7961 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7962 && TREE_VEC_LENGTH (packed_args
) > 0)
7964 if (complain
& tf_error
)
7965 error ("wrong number of template arguments (%d, should be %d)",
7966 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7967 return error_mark_node
;
7970 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7971 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7972 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7975 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7976 TREE_CONSTANT (argument_pack
) = 1;
7979 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7981 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7982 TREE_VEC_LENGTH (packed_args
));
7983 return argument_pack
;
7986 /* Returns the number of pack expansions in the template argument vector
7990 pack_expansion_args_count (tree args
)
7995 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7997 tree elt
= TREE_VEC_ELT (args
, i
);
7998 if (elt
&& PACK_EXPANSION_P (elt
))
8004 /* Convert all template arguments to their appropriate types, and
8005 return a vector containing the innermost resulting template
8006 arguments. If any error occurs, return error_mark_node. Error and
8007 warning messages are issued under control of COMPLAIN.
8009 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8010 for arguments not specified in ARGS. Otherwise, if
8011 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8012 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8013 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8017 coerce_template_parms (tree parms
,
8020 tsubst_flags_t complain
,
8021 bool require_all_args
,
8022 bool use_default_args
)
8024 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8025 tree orig_inner_args
;
8028 tree new_inner_args
;
8029 int saved_unevaluated_operand
;
8030 int saved_inhibit_evaluation_warnings
;
8032 /* When used as a boolean value, indicates whether this is a
8033 variadic template parameter list. Since it's an int, we can also
8034 subtract it from nparms to get the number of non-variadic
8037 int variadic_args_p
= 0;
8038 int post_variadic_parms
= 0;
8040 /* Likewise for parameters with default arguments. */
8043 if (args
== error_mark_node
)
8044 return error_mark_node
;
8046 nparms
= TREE_VEC_LENGTH (parms
);
8048 /* Determine if there are any parameter packs or default arguments. */
8049 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8051 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8053 ++post_variadic_parms
;
8054 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8056 if (TREE_PURPOSE (parm
))
8060 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8061 /* If there are no parameters that follow a parameter pack, we need to
8062 expand any argument packs so that we can deduce a parameter pack from
8063 some non-packed args followed by an argument pack, as in variadic85.C.
8064 If there are such parameters, we need to leave argument packs intact
8065 so the arguments are assigned properly. This can happen when dealing
8066 with a nested class inside a partial specialization of a class
8067 template, as in variadic92.C, or when deducing a template parameter pack
8068 from a sub-declarator, as in variadic114.C. */
8069 if (!post_variadic_parms
)
8070 inner_args
= expand_template_argument_pack (inner_args
);
8072 /* Count any pack expansion args. */
8073 variadic_args_p
= pack_expansion_args_count (inner_args
);
8075 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8076 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8077 || (nargs
< nparms
- variadic_p
8080 && (!use_default_args
8081 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8082 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8084 if (complain
& tf_error
)
8086 if (variadic_p
|| default_p
)
8088 nparms
-= variadic_p
+ default_p
;
8089 error ("wrong number of template arguments "
8090 "(%d, should be at least %d)", nargs
, nparms
);
8093 error ("wrong number of template arguments "
8094 "(%d, should be %d)", nargs
, nparms
);
8097 inform (DECL_SOURCE_LOCATION (in_decl
),
8098 "provided for %qD", in_decl
);
8101 return error_mark_node
;
8103 /* We can't pass a pack expansion to a non-pack parameter of an alias
8104 template (DR 1430). */
8106 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8107 || concept_template_p (in_decl
))
8109 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8111 if (complain
& tf_error
)
8113 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8115 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8116 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8118 if (PACK_EXPANSION_P (arg
)
8119 && !template_parameter_pack_p (parm
))
8121 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8122 error_at (location_of (arg
),
8123 "pack expansion argument for non-pack parameter "
8124 "%qD of alias template %qD", parm
, in_decl
);
8126 error_at (location_of (arg
),
8127 "pack expansion argument for non-pack parameter "
8128 "%qD of concept %qD", parm
, in_decl
);
8129 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8136 return error_mark_node
;
8139 /* We need to evaluate the template arguments, even though this
8140 template-id may be nested within a "sizeof". */
8141 saved_unevaluated_operand
= cp_unevaluated_operand
;
8142 cp_unevaluated_operand
= 0;
8143 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8144 c_inhibit_evaluation_warnings
= 0;
8145 new_inner_args
= make_tree_vec (nparms
);
8146 new_args
= add_outermost_template_args (args
, new_inner_args
);
8147 int pack_adjust
= 0;
8148 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8153 /* Get the Ith template parameter. */
8154 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8156 if (parm
== error_mark_node
)
8158 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8162 /* Calculate the next argument. */
8163 if (arg_idx
< nargs
)
8164 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8168 if (template_parameter_pack_p (TREE_VALUE (parm
))
8169 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8171 /* Some arguments will be placed in the
8172 template parameter pack PARM. */
8173 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8174 inner_args
, arg_idx
,
8178 if (arg
== NULL_TREE
)
8180 /* We don't know how many args we have yet, just use the
8181 unconverted (and still packed) ones for now. */
8182 new_inner_args
= orig_inner_args
;
8187 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8189 /* Store this argument. */
8190 if (arg
== error_mark_node
)
8193 /* We are done with all of the arguments. */
8198 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8199 arg_idx
+= pack_adjust
;
8206 if (PACK_EXPANSION_P (arg
))
8208 /* "If every valid specialization of a variadic template
8209 requires an empty template parameter pack, the template is
8210 ill-formed, no diagnostic required." So check that the
8211 pattern works with this parameter. */
8212 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8213 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8217 if (conv
== error_mark_node
)
8219 if (complain
& tf_error
)
8220 inform (input_location
, "so any instantiation with a "
8221 "non-empty parameter pack would be ill-formed");
8224 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8225 /* Recover from missing typename. */
8226 TREE_VEC_ELT (inner_args
, arg_idx
)
8227 = make_pack_expansion (conv
, complain
);
8229 /* We don't know how many args we have yet, just
8230 use the unconverted ones for now. */
8231 new_inner_args
= inner_args
;
8236 else if (require_all_args
)
8238 /* There must be a default arg in this case. */
8239 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8241 /* The position of the first default template argument,
8242 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8244 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8245 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8246 arg_idx
- pack_adjust
);
8251 if (arg
== error_mark_node
)
8253 if (complain
& tf_error
)
8254 error ("template argument %d is invalid", arg_idx
+ 1);
8257 /* This only occurs if there was an error in the template
8258 parameter list itself (which we would already have
8259 reported) that we are trying to recover from, e.g., a class
8260 template with a parameter list such as
8261 template<typename..., typename>. */
8264 arg
= convert_template_argument (TREE_VALUE (parm
),
8265 arg
, new_args
, complain
,
8268 if (arg
== error_mark_node
)
8270 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8272 cp_unevaluated_operand
= saved_unevaluated_operand
;
8273 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8275 if (variadic_p
&& arg_idx
< nargs
)
8277 if (complain
& tf_error
)
8279 error ("wrong number of template arguments "
8280 "(%d, should be %d)", nargs
, arg_idx
);
8282 error ("provided for %q+D", in_decl
);
8284 return error_mark_node
;
8288 return error_mark_node
;
8290 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8291 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8292 TREE_VEC_LENGTH (new_inner_args
));
8294 return new_inner_args
;
8297 /* Convert all template arguments to their appropriate types, and
8298 return a vector containing the innermost resulting template
8299 arguments. If any error occurs, return error_mark_node. Error and
8300 warning messages are not issued.
8302 Note that no function argument deduction is performed, and default
8303 arguments are used to fill in unspecified arguments. */
8305 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8307 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8310 /* Convert all template arguments to their appropriate type, and
8311 instantiate default arguments as needed. This returns a vector
8312 containing the innermost resulting template arguments, or
8313 error_mark_node if unsuccessful. */
8315 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8316 tsubst_flags_t complain
)
8318 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8321 /* Like coerce_template_parms. If PARMS represents all template
8322 parameters levels, this function returns a vector of vectors
8323 representing all the resulting argument levels. Note that in this
8324 case, only the innermost arguments are coerced because the
8325 outermost ones are supposed to have been coerced already.
8327 Otherwise, if PARMS represents only (the innermost) vector of
8328 parameters, this function returns a vector containing just the
8329 innermost resulting arguments. */
8332 coerce_innermost_template_parms (tree parms
,
8335 tsubst_flags_t complain
,
8336 bool require_all_args
,
8337 bool use_default_args
)
8339 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8340 int args_depth
= TMPL_ARGS_DEPTH (args
);
8343 if (parms_depth
> 1)
8345 coerced_args
= make_tree_vec (parms_depth
);
8349 for (level
= parms
, cur_depth
= parms_depth
;
8350 parms_depth
> 0 && level
!= NULL_TREE
;
8351 level
= TREE_CHAIN (level
), --cur_depth
)
8354 if (cur_depth
== args_depth
)
8355 l
= coerce_template_parms (TREE_VALUE (level
),
8356 args
, in_decl
, complain
,
8360 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8362 if (l
== error_mark_node
)
8363 return error_mark_node
;
8365 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8369 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8370 args
, in_decl
, complain
,
8373 return coerced_args
;
8376 /* Returns 1 if template args OT and NT are equivalent. */
8379 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8383 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8385 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8388 if (TREE_CODE (nt
) == TREE_VEC
)
8389 /* For member templates */
8390 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8391 else if (PACK_EXPANSION_P (ot
))
8392 return (PACK_EXPANSION_P (nt
)
8393 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8394 PACK_EXPANSION_PATTERN (nt
))
8395 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8396 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8397 else if (ARGUMENT_PACK_P (ot
))
8402 if (!ARGUMENT_PACK_P (nt
))
8405 opack
= ARGUMENT_PACK_ARGS (ot
);
8406 npack
= ARGUMENT_PACK_ARGS (nt
);
8407 len
= TREE_VEC_LENGTH (opack
);
8408 if (TREE_VEC_LENGTH (npack
) != len
)
8410 for (i
= 0; i
< len
; ++i
)
8411 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8412 TREE_VEC_ELT (npack
, i
)))
8416 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8418 else if (TYPE_P (nt
))
8422 /* Don't treat an alias template specialization with dependent
8423 arguments as equivalent to its underlying type when used as a
8424 template argument; we need them to be distinct so that we
8425 substitute into the specialization arguments at instantiation
8426 time. And aliases can't be equivalent without being ==, so
8427 we don't need to look any deeper.
8429 During partial ordering, however, we need to treat them normally so
8430 that we can order uses of the same alias with different
8431 cv-qualification (79960). */
8433 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8436 return same_type_p (ot
, nt
);
8438 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8442 /* Try to treat a template non-type argument that has been converted
8443 to the parameter type as equivalent to one that hasn't yet. */
8444 for (enum tree_code code1
= TREE_CODE (ot
);
8445 CONVERT_EXPR_CODE_P (code1
)
8446 || code1
== NON_LVALUE_EXPR
;
8447 code1
= TREE_CODE (ot
))
8448 ot
= TREE_OPERAND (ot
, 0);
8449 for (enum tree_code code2
= TREE_CODE (nt
);
8450 CONVERT_EXPR_CODE_P (code2
)
8451 || code2
== NON_LVALUE_EXPR
;
8452 code2
= TREE_CODE (nt
))
8453 nt
= TREE_OPERAND (nt
, 0);
8455 return cp_tree_equal (ot
, nt
);
8459 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8460 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8461 NEWARG_PTR with the offending arguments if they are non-NULL. */
8464 comp_template_args (tree oldargs
, tree newargs
,
8465 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8470 if (oldargs
== newargs
)
8473 if (!oldargs
|| !newargs
)
8476 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8479 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8481 tree nt
= TREE_VEC_ELT (newargs
, i
);
8482 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8484 if (! template_args_equal (ot
, nt
, partial_order
))
8486 if (oldarg_ptr
!= NULL
)
8488 if (newarg_ptr
!= NULL
)
8497 comp_template_args_porder (tree oargs
, tree nargs
)
8499 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8503 add_pending_template (tree d
)
8505 tree ti
= (TYPE_P (d
)
8506 ? CLASSTYPE_TEMPLATE_INFO (d
)
8507 : DECL_TEMPLATE_INFO (d
));
8508 struct pending_template
*pt
;
8511 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8514 /* We are called both from instantiate_decl, where we've already had a
8515 tinst_level pushed, and instantiate_template, where we haven't.
8517 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8520 push_tinst_level (d
);
8522 pt
= ggc_alloc
<pending_template
> ();
8524 pt
->tinst
= current_tinst_level
;
8525 if (last_pending_template
)
8526 last_pending_template
->next
= pt
;
8528 pending_templates
= pt
;
8530 last_pending_template
= pt
;
8532 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8539 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8540 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8541 documentation for TEMPLATE_ID_EXPR. */
8544 lookup_template_function (tree fns
, tree arglist
)
8548 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8549 return error_mark_node
;
8551 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8553 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8555 error ("%q#D is not a function template", fns
);
8556 return error_mark_node
;
8559 if (BASELINK_P (fns
))
8561 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8563 BASELINK_FUNCTIONS (fns
),
8568 type
= TREE_TYPE (fns
);
8569 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8570 type
= unknown_type_node
;
8572 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8575 /* Within the scope of a template class S<T>, the name S gets bound
8576 (in build_self_reference) to a TYPE_DECL for the class, not a
8577 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8578 or one of its enclosing classes, and that type is a template,
8579 return the associated TEMPLATE_DECL. Otherwise, the original
8582 Also handle the case when DECL is a TREE_LIST of ambiguous
8583 injected-class-names from different bases. */
8586 maybe_get_template_decl_from_type_decl (tree decl
)
8588 if (decl
== NULL_TREE
)
8591 /* DR 176: A lookup that finds an injected-class-name (10.2
8592 [class.member.lookup]) can result in an ambiguity in certain cases
8593 (for example, if it is found in more than one base class). If all of
8594 the injected-class-names that are found refer to specializations of
8595 the same class template, and if the name is followed by a
8596 template-argument-list, the reference refers to the class template
8597 itself and not a specialization thereof, and is not ambiguous. */
8598 if (TREE_CODE (decl
) == TREE_LIST
)
8600 tree t
, tmpl
= NULL_TREE
;
8601 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8603 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8606 else if (tmpl
!= elt
)
8609 if (tmpl
&& t
== NULL_TREE
)
8615 return (decl
!= NULL_TREE
8616 && DECL_SELF_REFERENCE_P (decl
)
8617 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8618 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8621 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8622 parameters, find the desired type.
8624 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8626 IN_DECL, if non-NULL, is the template declaration we are trying to
8629 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8630 the class we are looking up.
8632 Issue error and warning messages under control of COMPLAIN.
8634 If the template class is really a local class in a template
8635 function, then the FUNCTION_CONTEXT is the function in which it is
8638 ??? Note that this function is currently called *twice* for each
8639 template-id: the first time from the parser, while creating the
8640 incomplete type (finish_template_type), and the second type during the
8641 real instantiation (instantiate_template_class). This is surely something
8642 that we want to avoid. It also causes some problems with argument
8643 coercion (see convert_nontype_argument for more information on this). */
8646 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8647 int entering_scope
, tsubst_flags_t complain
)
8649 tree templ
= NULL_TREE
, parmlist
;
8656 if (identifier_p (d1
))
8658 tree value
= innermost_non_namespace_value (d1
);
8659 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8664 push_decl_namespace (context
);
8665 templ
= lookup_name (d1
);
8666 templ
= maybe_get_template_decl_from_type_decl (templ
);
8668 pop_decl_namespace ();
8671 context
= DECL_CONTEXT (templ
);
8673 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8675 tree type
= TREE_TYPE (d1
);
8677 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8678 an implicit typename for the second A. Deal with it. */
8679 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8680 type
= TREE_TYPE (type
);
8682 if (CLASSTYPE_TEMPLATE_INFO (type
))
8684 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8685 d1
= DECL_NAME (templ
);
8688 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8689 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8691 templ
= TYPE_TI_TEMPLATE (d1
);
8692 d1
= DECL_NAME (templ
);
8694 else if (DECL_TYPE_TEMPLATE_P (d1
))
8697 d1
= DECL_NAME (templ
);
8698 context
= DECL_CONTEXT (templ
);
8700 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8703 d1
= DECL_NAME (templ
);
8706 /* Issue an error message if we didn't find a template. */
8709 if (complain
& tf_error
)
8710 error ("%qT is not a template", d1
);
8711 return error_mark_node
;
8714 if (TREE_CODE (templ
) != TEMPLATE_DECL
8715 /* Make sure it's a user visible template, if it was named by
8717 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8718 && !PRIMARY_TEMPLATE_P (templ
)))
8720 if (complain
& tf_error
)
8722 error ("non-template type %qT used as a template", d1
);
8724 error ("for template declaration %q+D", in_decl
);
8726 return error_mark_node
;
8729 complain
&= ~tf_user
;
8731 /* An alias that just changes the name of a template is equivalent to the
8732 other template, so if any of the arguments are pack expansions, strip
8733 the alias to avoid problems with a pack expansion passed to a non-pack
8734 alias template parameter (DR 1430). */
8735 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8736 templ
= get_underlying_template (templ
);
8738 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8741 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8742 if (arglist2
== error_mark_node
8743 || (!uses_template_parms (arglist2
)
8744 && check_instantiated_args (templ
, arglist2
, complain
)))
8745 return error_mark_node
;
8747 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8752 tree template_type
= TREE_TYPE (templ
);
8755 tree found
= NULL_TREE
;
8758 int is_dependent_type
;
8759 int use_partial_inst_tmpl
= false;
8761 if (template_type
== error_mark_node
)
8762 /* An error occurred while building the template TEMPL, and a
8763 diagnostic has most certainly been emitted for that
8764 already. Let's propagate that error. */
8765 return error_mark_node
;
8767 gen_tmpl
= most_general_template (templ
);
8768 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8769 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8770 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8772 if (arg_depth
== 1 && parm_depth
> 1)
8774 /* We've been given an incomplete set of template arguments.
8777 template <class T> struct S1 {
8778 template <class U> struct S2 {};
8779 template <class U> struct S2<U*> {};
8782 we will be called with an ARGLIST of `U*', but the
8783 TEMPLATE will be `template <class T> template
8784 <class U> struct S1<T>::S2'. We must fill in the missing
8786 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8787 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8788 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8791 /* Now we should have enough arguments. */
8792 gcc_assert (parm_depth
== arg_depth
);
8794 /* From here on, we're only interested in the most general
8797 /* Calculate the BOUND_ARGS. These will be the args that are
8798 actually tsubst'd into the definition to create the
8800 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8802 /*require_all_args=*/true,
8803 /*use_default_args=*/true);
8805 if (arglist
== error_mark_node
)
8806 /* We were unable to bind the arguments. */
8807 return error_mark_node
;
8809 /* In the scope of a template class, explicit references to the
8810 template class refer to the type of the template, not any
8811 instantiation of it. For example, in:
8813 template <class T> class C { void f(C<T>); }
8815 the `C<T>' is just the same as `C'. Outside of the
8816 class, however, such a reference is an instantiation. */
8818 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8819 || currently_open_class (template_type
))
8821 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8823 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8824 return template_type
;
8827 /* If we already have this specialization, return it. */
8828 elt
.tmpl
= gen_tmpl
;
8830 elt
.spec
= NULL_TREE
;
8831 hash
= spec_hasher::hash (&elt
);
8832 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8837 /* If the the template's constraints are not satisfied,
8838 then we cannot form a valid type.
8840 Note that the check is deferred until after the hash
8841 lookup. This prevents redundant checks on previously
8842 instantiated specializations. */
8843 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8845 if (complain
& tf_error
)
8847 error ("template constraint failure");
8848 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8850 return error_mark_node
;
8853 is_dependent_type
= uses_template_parms (arglist
);
8855 /* If the deduced arguments are invalid, then the binding
8857 if (!is_dependent_type
8858 && check_instantiated_args (gen_tmpl
,
8859 INNERMOST_TEMPLATE_ARGS (arglist
),
8861 return error_mark_node
;
8863 if (!is_dependent_type
8864 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8865 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8866 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8868 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8869 DECL_NAME (gen_tmpl
),
8870 /*tag_scope=*/ts_global
);
8874 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8876 if (context
== error_mark_node
)
8877 return error_mark_node
;
8880 context
= global_namespace
;
8882 /* Create the type. */
8883 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8885 /* The user referred to a specialization of an alias
8886 template represented by GEN_TMPL.
8888 [temp.alias]/2 says:
8890 When a template-id refers to the specialization of an
8891 alias template, it is equivalent to the associated
8892 type obtained by substitution of its
8893 template-arguments for the template-parameters in the
8894 type-id of the alias template. */
8896 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8897 /* Note that the call above (by indirectly calling
8898 register_specialization in tsubst_decl) registers the
8899 TYPE_DECL representing the specialization of the alias
8900 template. So next time someone substitutes ARGLIST for
8901 the template parms into the alias template (GEN_TMPL),
8902 she'll get that TYPE_DECL back. */
8904 if (t
== error_mark_node
)
8907 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8909 if (!is_dependent_type
)
8911 set_current_access_from_decl (TYPE_NAME (template_type
));
8912 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8913 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8914 arglist
, complain
, in_decl
),
8915 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8916 arglist
, complain
, in_decl
),
8917 SCOPED_ENUM_P (template_type
), NULL
);
8919 if (t
== error_mark_node
)
8924 /* We don't want to call start_enum for this type, since
8925 the values for the enumeration constants may involve
8926 template parameters. And, no one should be interested
8927 in the enumeration constants for such a type. */
8928 t
= cxx_make_type (ENUMERAL_TYPE
);
8929 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8931 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8932 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8933 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8935 else if (CLASS_TYPE_P (template_type
))
8937 t
= make_class_type (TREE_CODE (template_type
));
8938 CLASSTYPE_DECLARED_CLASS (t
)
8939 = CLASSTYPE_DECLARED_CLASS (template_type
);
8940 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8942 /* A local class. Make sure the decl gets registered properly. */
8943 if (context
== current_function_decl
)
8944 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8946 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8947 /* This instantiation is another name for the primary
8948 template type. Set the TYPE_CANONICAL field
8950 TYPE_CANONICAL (t
) = template_type
;
8951 else if (any_template_arguments_need_structural_equality_p (arglist
))
8952 /* Some of the template arguments require structural
8953 equality testing, so this template class requires
8954 structural equality testing. */
8955 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8960 /* If we called start_enum or pushtag above, this information
8961 will already be set up. */
8964 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8966 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8967 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8968 DECL_SOURCE_LOCATION (type_decl
)
8969 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8972 type_decl
= TYPE_NAME (t
);
8974 if (CLASS_TYPE_P (template_type
))
8976 TREE_PRIVATE (type_decl
)
8977 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8978 TREE_PROTECTED (type_decl
)
8979 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8980 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8982 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8983 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8987 if (OVERLOAD_TYPE_P (t
)
8988 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8990 static const char *tags
[] = {"abi_tag", "may_alias"};
8992 for (unsigned ix
= 0; ix
!= 2; ix
++)
8995 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8999 = tree_cons (TREE_PURPOSE (attributes
),
9000 TREE_VALUE (attributes
),
9001 TYPE_ATTRIBUTES (t
));
9005 /* Let's consider the explicit specialization of a member
9006 of a class template specialization that is implicitly instantiated,
9011 template<class U> struct M {}; //#0
9016 struct S<int>::M<char> //#1
9020 [temp.expl.spec]/4 says this is valid.
9022 In this case, when we write:
9025 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9028 When we encounter #1, we want to store the partial instantiation
9029 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9031 For all cases other than this "explicit specialization of member of a
9032 class template", we just want to store the most general template into
9033 the CLASSTYPE_TI_TEMPLATE of M.
9035 This case of "explicit specialization of member of a class template"
9037 1/ the enclosing class is an instantiation of, and therefore not
9038 the same as, the context of the most general template, and
9039 2/ we aren't looking at the partial instantiation itself, i.e.
9040 the innermost arguments are not the same as the innermost parms of
9041 the most general template.
9043 So it's only when 1/ and 2/ happens that we want to use the partial
9044 instantiation of the member template in lieu of its most general
9047 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9048 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9049 /* the enclosing class must be an instantiation... */
9050 && CLASS_TYPE_P (context
)
9051 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9053 TREE_VEC_LENGTH (arglist
)--;
9054 ++processing_template_decl
;
9055 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9056 tree partial_inst_args
=
9057 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9058 arglist
, complain
, NULL_TREE
);
9059 --processing_template_decl
;
9060 TREE_VEC_LENGTH (arglist
)++;
9061 if (partial_inst_args
== error_mark_node
)
9062 return error_mark_node
;
9063 use_partial_inst_tmpl
=
9064 /*...and we must not be looking at the partial instantiation
9066 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9070 if (!use_partial_inst_tmpl
)
9071 /* This case is easy; there are no member templates involved. */
9075 /* This is a full instantiation of a member template. Find
9076 the partial instantiation of which this is an instance. */
9078 /* Temporarily reduce by one the number of levels in the ARGLIST
9079 so as to avoid comparing the last set of arguments. */
9080 TREE_VEC_LENGTH (arglist
)--;
9081 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9082 TREE_VEC_LENGTH (arglist
)++;
9083 /* FOUND is either a proper class type, or an alias
9084 template specialization. In the later case, it's a
9085 TYPE_DECL, resulting from the substituting of arguments
9086 for parameters in the TYPE_DECL of the alias template
9087 done earlier. So be careful while getting the template
9089 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9091 : (TREE_CODE (found
) == TYPE_DECL
9092 ? DECL_TI_TEMPLATE (found
)
9093 : CLASSTYPE_TI_TEMPLATE (found
)));
9096 // Build template info for the new specialization.
9097 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9100 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9101 entry
= ggc_alloc
<spec_entry
> ();
9105 /* Note this use of the partial instantiation so we can check it
9106 later in maybe_process_partial_specialization. */
9107 DECL_TEMPLATE_INSTANTIATIONS (found
)
9108 = tree_cons (arglist
, t
,
9109 DECL_TEMPLATE_INSTANTIATIONS (found
));
9111 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9112 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9113 /* Now that the type has been registered on the instantiations
9114 list, we set up the enumerators. Because the enumeration
9115 constants may involve the enumeration type itself, we make
9116 sure to register the type first, and then create the
9117 constants. That way, doing tsubst_expr for the enumeration
9118 constants won't result in recursive calls here; we'll find
9119 the instantiation and exit above. */
9120 tsubst_enum (template_type
, t
, arglist
);
9122 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9123 /* If the type makes use of template parameters, the
9124 code that generates debugging information will crash. */
9125 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9127 /* Possibly limit visibility based on template args. */
9128 TREE_PUBLIC (type_decl
) = 1;
9129 determine_visibility (type_decl
);
9131 inherit_targ_abi_tags (t
);
9137 /* Wrapper for lookup_template_class_1. */
9140 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9141 int entering_scope
, tsubst_flags_t complain
)
9144 timevar_push (TV_TEMPLATE_INST
);
9145 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9146 entering_scope
, complain
);
9147 timevar_pop (TV_TEMPLATE_INST
);
9151 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9154 lookup_template_variable (tree templ
, tree arglist
)
9156 /* The type of the expression is NULL_TREE since the template-id could refer
9157 to an explicit or partial specialization. */
9158 tree type
= NULL_TREE
;
9159 if (flag_concepts
&& variable_concept_p (templ
))
9160 /* Except that concepts are always bool. */
9161 type
= boolean_type_node
;
9162 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9165 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9168 finish_template_variable (tree var
, tsubst_flags_t complain
)
9170 tree templ
= TREE_OPERAND (var
, 0);
9171 tree arglist
= TREE_OPERAND (var
, 1);
9173 /* We never want to return a VAR_DECL for a variable concept, since they
9174 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9175 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9176 if (concept_p
&& processing_template_decl
)
9179 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9180 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9182 templ
= most_general_template (templ
);
9183 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9184 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9186 /*use_default*/true);
9188 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9190 if (complain
& tf_error
)
9192 error ("use of invalid variable template %qE", var
);
9193 diagnose_constraints (location_of (var
), templ
, arglist
);
9195 return error_mark_node
;
9198 /* If a template-id refers to a specialization of a variable
9199 concept, then the expression is true if and only if the
9200 concept's constraints are satisfied by the given template
9203 NOTE: This is an extension of Concepts Lite TS that
9204 allows constraints to be used in expressions. */
9207 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9208 return evaluate_variable_concept (decl
, arglist
);
9211 return instantiate_template (templ
, arglist
, complain
);
9214 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9215 TARGS template args, and instantiate it if it's not dependent. */
9218 lookup_and_finish_template_variable (tree templ
, tree targs
,
9219 tsubst_flags_t complain
)
9221 templ
= lookup_template_variable (templ
, targs
);
9222 if (!any_dependent_template_arguments_p (targs
))
9224 templ
= finish_template_variable (templ
, complain
);
9228 return convert_from_reference (templ
);
9237 /* True when we should also visit template parameters that occur in
9238 non-deduced contexts. */
9239 bool include_nondeduced_p
;
9240 hash_set
<tree
> *visited
;
9243 /* Called from for_each_template_parm via walk_tree. */
9246 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9249 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9250 tree_fn_t fn
= pfd
->fn
;
9251 void *data
= pfd
->data
;
9252 tree result
= NULL_TREE
;
9254 #define WALK_SUBTREE(NODE) \
9257 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9258 pfd->include_nondeduced_p, \
9260 if (result) goto out; \
9264 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9268 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9269 WALK_SUBTREE (TYPE_CONTEXT (t
));
9271 switch (TREE_CODE (t
))
9274 if (TYPE_PTRMEMFUNC_P (t
))
9280 if (!TYPE_TEMPLATE_INFO (t
))
9283 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9287 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9288 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9292 /* Since we're not going to walk subtrees, we have to do this
9294 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9298 /* Check the return type. */
9299 WALK_SUBTREE (TREE_TYPE (t
));
9301 /* Check the parameter types. Since default arguments are not
9302 instantiated until they are needed, the TYPE_ARG_TYPES may
9303 contain expressions that involve template parameters. But,
9304 no-one should be looking at them yet. And, once they're
9305 instantiated, they don't contain template parameters, so
9306 there's no point in looking at them then, either. */
9310 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9311 WALK_SUBTREE (TREE_VALUE (parm
));
9313 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9314 want walk_tree walking into them itself. */
9318 if (flag_noexcept_type
)
9320 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9322 WALK_SUBTREE (TREE_PURPOSE (spec
));
9327 case UNDERLYING_TYPE
:
9328 if (pfd
->include_nondeduced_p
9329 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9331 pfd
->include_nondeduced_p
,
9333 return error_mark_node
;
9338 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9339 WALK_SUBTREE (DECL_TI_ARGS (t
));
9344 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9345 WALK_SUBTREE (DECL_INITIAL (t
));
9346 if (DECL_CONTEXT (t
)
9347 && pfd
->include_nondeduced_p
)
9348 WALK_SUBTREE (DECL_CONTEXT (t
));
9351 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9352 /* Record template parameters such as `T' inside `TT<T>'. */
9353 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9356 case TEMPLATE_TEMPLATE_PARM
:
9357 case TEMPLATE_TYPE_PARM
:
9358 case TEMPLATE_PARM_INDEX
:
9359 if (fn
&& (*fn
)(t
, data
))
9366 /* A template template parameter is encountered. */
9367 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9368 WALK_SUBTREE (TREE_TYPE (t
));
9370 /* Already substituted template template parameter */
9375 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9376 partial instantiation. */
9377 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9381 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9382 && pfd
->include_nondeduced_p
)
9383 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9388 /* If there's no type, then this thing must be some expression
9389 involving template parameters. */
9390 if (!fn
&& !TREE_TYPE (t
))
9391 return error_mark_node
;
9396 case IMPLICIT_CONV_EXPR
:
9397 case REINTERPRET_CAST_EXPR
:
9398 case CONST_CAST_EXPR
:
9399 case STATIC_CAST_EXPR
:
9400 case DYNAMIC_CAST_EXPR
:
9404 case PSEUDO_DTOR_EXPR
:
9406 return error_mark_node
;
9415 /* We didn't find any template parameters we liked. */
9420 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9421 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9422 call FN with the parameter and the DATA.
9423 If FN returns nonzero, the iteration is terminated, and
9424 for_each_template_parm returns 1. Otherwise, the iteration
9425 continues. If FN never returns a nonzero value, the value
9426 returned by for_each_template_parm is 0. If FN is NULL, it is
9427 considered to be the function which always returns 1.
9429 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9430 parameters that occur in non-deduced contexts. When false, only
9431 visits those template parameters that can be deduced. */
9434 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9435 hash_set
<tree
> *visited
,
9436 bool include_nondeduced_p
,
9439 struct pair_fn_data pfd
;
9444 pfd
.any_fn
= any_fn
;
9446 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9448 /* Walk the tree. (Conceptually, we would like to walk without
9449 duplicates, but for_each_template_parm_r recursively calls
9450 for_each_template_parm, so we would need to reorganize a fair
9451 bit to use walk_tree_without_duplicates, so we keep our own
9454 pfd
.visited
= visited
;
9456 pfd
.visited
= new hash_set
<tree
>;
9457 result
= cp_walk_tree (&t
,
9458 for_each_template_parm_r
,
9472 /* Returns true if T depends on any template parameter. */
9475 uses_template_parms (tree t
)
9481 int saved_processing_template_decl
;
9483 saved_processing_template_decl
= processing_template_decl
;
9484 if (!saved_processing_template_decl
)
9485 processing_template_decl
= 1;
9487 dependent_p
= dependent_type_p (t
);
9488 else if (TREE_CODE (t
) == TREE_VEC
)
9489 dependent_p
= any_dependent_template_arguments_p (t
);
9490 else if (TREE_CODE (t
) == TREE_LIST
)
9491 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9492 || uses_template_parms (TREE_CHAIN (t
)));
9493 else if (TREE_CODE (t
) == TYPE_DECL
)
9494 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9497 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9498 || TREE_CODE (t
) == OVERLOAD
9501 || TREE_CODE (t
) == TRAIT_EXPR
9502 || TREE_CODE (t
) == CONSTRUCTOR
9503 || CONSTANT_CLASS_P (t
))
9504 dependent_p
= (type_dependent_expression_p (t
)
9505 || value_dependent_expression_p (t
));
9508 gcc_assert (t
== error_mark_node
);
9509 dependent_p
= false;
9512 processing_template_decl
= saved_processing_template_decl
;
9517 /* Returns true iff current_function_decl is an incompletely instantiated
9518 template. Useful instead of processing_template_decl because the latter
9519 is set to 0 during instantiate_non_dependent_expr. */
9522 in_template_function (void)
9524 tree fn
= current_function_decl
;
9526 ++processing_template_decl
;
9527 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9528 && DECL_TEMPLATE_INFO (fn
)
9529 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9530 --processing_template_decl
;
9534 /* Returns true if T depends on any template parameter with level LEVEL. */
9537 uses_template_parms_level (tree t
, int level
)
9539 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9540 /*include_nondeduced_p=*/true);
9543 /* Returns true if the signature of DECL depends on any template parameter from
9544 its enclosing class. */
9547 uses_outer_template_parms (tree decl
)
9549 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9552 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9553 &depth
, NULL
, /*include_nondeduced_p=*/true))
9555 if (PRIMARY_TEMPLATE_P (decl
)
9556 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9557 (DECL_TEMPLATE_PARMS (decl
)),
9558 template_parm_outer_level
,
9559 &depth
, NULL
, /*include_nondeduced_p=*/true))
9561 tree ci
= get_constraints (decl
);
9563 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9564 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9565 &depth
, NULL
, /*nondeduced*/true))
9570 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9571 ill-formed translation unit, i.e. a variable or function that isn't
9572 usable in a constant expression. */
9575 neglectable_inst_p (tree d
)
9578 && !undeduced_auto_decl (d
)
9579 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9580 : decl_maybe_constant_var_p (d
)));
9583 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9584 neglectable and instantiated from within an erroneous instantiation. */
9587 limit_bad_template_recursion (tree decl
)
9589 struct tinst_level
*lev
= current_tinst_level
;
9590 int errs
= errorcount
+ sorrycount
;
9591 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9594 for (; lev
; lev
= lev
->next
)
9595 if (neglectable_inst_p (lev
->decl
))
9598 return (lev
&& errs
> lev
->errors
);
9601 static int tinst_depth
;
9602 extern int max_tinst_depth
;
9605 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9607 /* We're starting to instantiate D; record the template instantiation context
9608 for diagnostics and to restore it later. */
9611 push_tinst_level (tree d
)
9613 return push_tinst_level_loc (d
, input_location
);
9616 /* We're starting to instantiate D; record the template instantiation context
9617 at LOC for diagnostics and to restore it later. */
9620 push_tinst_level_loc (tree d
, location_t loc
)
9622 struct tinst_level
*new_level
;
9624 if (tinst_depth
>= max_tinst_depth
)
9626 /* Tell error.c not to try to instantiate any templates. */
9628 fatal_error (input_location
,
9629 "template instantiation depth exceeds maximum of %d"
9630 " (use -ftemplate-depth= to increase the maximum)",
9635 /* If the current instantiation caused problems, don't let it instantiate
9636 anything else. Do allow deduction substitution and decls usable in
9637 constant expressions. */
9638 if (limit_bad_template_recursion (d
))
9641 /* When not -quiet, dump template instantiations other than functions, since
9642 announce_function will take care of those. */
9644 && TREE_CODE (d
) != TREE_LIST
9645 && TREE_CODE (d
) != FUNCTION_DECL
)
9646 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9648 new_level
= ggc_alloc
<tinst_level
> ();
9649 new_level
->decl
= d
;
9650 new_level
->locus
= loc
;
9651 new_level
->errors
= errorcount
+sorrycount
;
9652 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9653 new_level
->next
= current_tinst_level
;
9654 current_tinst_level
= new_level
;
9657 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9658 depth_reached
= tinst_depth
;
9663 /* We're done instantiating this template; return to the instantiation
9667 pop_tinst_level (void)
9669 /* Restore the filename and line number stashed away when we started
9670 this instantiation. */
9671 input_location
= current_tinst_level
->locus
;
9672 current_tinst_level
= current_tinst_level
->next
;
9676 /* We're instantiating a deferred template; restore the template
9677 instantiation context in which the instantiation was requested, which
9678 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9681 reopen_tinst_level (struct tinst_level
*level
)
9683 struct tinst_level
*t
;
9686 for (t
= level
; t
; t
= t
->next
)
9689 current_tinst_level
= level
;
9691 if (current_tinst_level
)
9692 current_tinst_level
->errors
= errorcount
+sorrycount
;
9696 /* Returns the TINST_LEVEL which gives the original instantiation
9699 struct tinst_level
*
9700 outermost_tinst_level (void)
9702 struct tinst_level
*level
= current_tinst_level
;
9705 level
= level
->next
;
9709 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9710 vector of template arguments, as for tsubst.
9712 Returns an appropriate tsubst'd friend declaration. */
9715 tsubst_friend_function (tree decl
, tree args
)
9719 if (TREE_CODE (decl
) == FUNCTION_DECL
9720 && DECL_TEMPLATE_INSTANTIATION (decl
)
9721 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9722 /* This was a friend declared with an explicit template
9723 argument list, e.g.:
9727 to indicate that f was a template instantiation, not a new
9728 function declaration. Now, we have to figure out what
9729 instantiation of what template. */
9731 tree template_id
, arglist
, fns
;
9734 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9736 /* Friend functions are looked up in the containing namespace scope.
9737 We must enter that scope, to avoid finding member functions of the
9738 current class with same name. */
9739 push_nested_namespace (ns
);
9740 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9741 tf_warning_or_error
, NULL_TREE
,
9742 /*integral_constant_expression_p=*/false);
9743 pop_nested_namespace (ns
);
9744 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9745 tf_warning_or_error
, NULL_TREE
);
9746 template_id
= lookup_template_function (fns
, arglist
);
9748 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9749 tmpl
= determine_specialization (template_id
, new_friend
,
9751 /*need_member_template=*/0,
9752 TREE_VEC_LENGTH (args
),
9754 return instantiate_template (tmpl
, new_args
, tf_error
);
9757 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9759 /* The NEW_FRIEND will look like an instantiation, to the
9760 compiler, but is not an instantiation from the point of view of
9761 the language. For example, we might have had:
9763 template <class T> struct S {
9764 template <class U> friend void f(T, U);
9767 Then, in S<int>, template <class U> void f(int, U) is not an
9768 instantiation of anything. */
9769 if (new_friend
== error_mark_node
)
9770 return error_mark_node
;
9772 DECL_USE_TEMPLATE (new_friend
) = 0;
9773 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9775 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9776 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9777 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9780 /* The mangled name for the NEW_FRIEND is incorrect. The function
9781 is not a template instantiation and should not be mangled like
9782 one. Therefore, we forget the mangling here; we'll recompute it
9783 later if we need it. */
9784 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9786 SET_DECL_RTL (new_friend
, NULL
);
9787 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9790 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9793 tree new_friend_template_info
;
9794 tree new_friend_result_template_info
;
9796 int new_friend_is_defn
;
9798 /* We must save some information from NEW_FRIEND before calling
9799 duplicate decls since that function will free NEW_FRIEND if
9801 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9802 new_friend_is_defn
=
9803 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9804 (template_for_substitution (new_friend
)))
9806 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9808 /* This declaration is a `primary' template. */
9809 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9811 new_friend_result_template_info
9812 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9815 new_friend_result_template_info
= NULL_TREE
;
9817 /* Inside pushdecl_namespace_level, we will push into the
9818 current namespace. However, the friend function should go
9819 into the namespace of the template. */
9820 ns
= decl_namespace_context (new_friend
);
9821 push_nested_namespace (ns
);
9822 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9823 pop_nested_namespace (ns
);
9825 if (old_decl
== error_mark_node
)
9826 return error_mark_node
;
9828 if (old_decl
!= new_friend
)
9830 /* This new friend declaration matched an existing
9831 declaration. For example, given:
9833 template <class T> void f(T);
9834 template <class U> class C {
9835 template <class T> friend void f(T) {}
9838 the friend declaration actually provides the definition
9839 of `f', once C has been instantiated for some type. So,
9840 old_decl will be the out-of-class template declaration,
9841 while new_friend is the in-class definition.
9843 But, if `f' was called before this point, the
9844 instantiation of `f' will have DECL_TI_ARGS corresponding
9845 to `T' but not to `U', references to which might appear
9846 in the definition of `f'. Previously, the most general
9847 template for an instantiation of `f' was the out-of-class
9848 version; now it is the in-class version. Therefore, we
9849 run through all specialization of `f', adding to their
9850 DECL_TI_ARGS appropriately. In particular, they need a
9851 new set of outer arguments, corresponding to the
9852 arguments for this class instantiation.
9854 The same situation can arise with something like this:
9857 template <class T> class C {
9861 when `C<int>' is instantiated. Now, `f(int)' is defined
9864 if (!new_friend_is_defn
)
9865 /* On the other hand, if the in-class declaration does
9866 *not* provide a definition, then we don't want to alter
9867 existing definitions. We can just leave everything
9872 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9873 tree new_args
= TI_ARGS (new_friend_template_info
);
9875 /* Overwrite whatever template info was there before, if
9876 any, with the new template information pertaining to
9878 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9880 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9882 /* We should have called reregister_specialization in
9884 gcc_assert (retrieve_specialization (new_template
,
9888 /* Instantiate it if the global has already been used. */
9889 if (DECL_ODR_USED (old_decl
))
9890 instantiate_decl (old_decl
, /*defer_ok=*/true,
9891 /*expl_inst_class_mem_p=*/false);
9897 /* Indicate that the old function template is a partial
9899 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9900 = new_friend_result_template_info
;
9902 gcc_assert (new_template
9903 == most_general_template (new_template
));
9904 gcc_assert (new_template
!= old_decl
);
9906 /* Reassign any specializations already in the hash table
9907 to the new more general template, and add the
9908 additional template args. */
9909 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9913 tree spec
= TREE_VALUE (t
);
9916 elt
.tmpl
= old_decl
;
9917 elt
.args
= DECL_TI_ARGS (spec
);
9918 elt
.spec
= NULL_TREE
;
9920 decl_specializations
->remove_elt (&elt
);
9923 = add_outermost_template_args (new_args
,
9924 DECL_TI_ARGS (spec
));
9926 register_specialization
9927 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9930 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9934 /* The information from NEW_FRIEND has been merged into OLD_DECL
9935 by duplicate_decls. */
9936 new_friend
= old_decl
;
9941 tree context
= DECL_CONTEXT (new_friend
);
9945 template <class T> class C {
9946 template <class U> friend void C1<U>::f (); // case 1
9947 friend void C2<T>::f (); // case 2
9949 we only need to make sure CONTEXT is a complete type for
9950 case 2. To distinguish between the two cases, we note that
9951 CONTEXT of case 1 remains dependent type after tsubst while
9952 this isn't true for case 2. */
9953 ++processing_template_decl
;
9954 dependent_p
= dependent_type_p (context
);
9955 --processing_template_decl
;
9958 && !complete_type_or_else (context
, NULL_TREE
))
9959 return error_mark_node
;
9961 if (COMPLETE_TYPE_P (context
))
9963 tree fn
= new_friend
;
9964 /* do_friend adds the TEMPLATE_DECL for any member friend
9965 template even if it isn't a member template, i.e.
9966 template <class T> friend A<T>::f();
9967 Look through it in that case. */
9968 if (TREE_CODE (fn
) == TEMPLATE_DECL
9969 && !PRIMARY_TEMPLATE_P (fn
))
9970 fn
= DECL_TEMPLATE_RESULT (fn
);
9971 /* Check to see that the declaration is really present, and,
9972 possibly obtain an improved declaration. */
9973 fn
= check_classfn (context
, fn
, NULL_TREE
);
9983 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9984 template arguments, as for tsubst.
9986 Returns an appropriate tsubst'd friend type or error_mark_node on
9990 tsubst_friend_class (tree friend_tmpl
, tree args
)
9996 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9998 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9999 return TREE_TYPE (t
);
10002 context
= CP_DECL_CONTEXT (friend_tmpl
);
10004 if (context
!= global_namespace
)
10006 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10007 push_nested_namespace (context
);
10009 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
10012 /* Look for a class template declaration. We look for hidden names
10013 because two friend declarations of the same template are the
10014 same. For example, in:
10017 template <typename> friend class F;
10019 template <typename> struct B {
10020 template <typename> friend class F;
10023 both F templates are the same. */
10024 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
10025 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
10027 /* But, if we don't find one, it might be because we're in a
10028 situation like this:
10036 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10037 for `S<int>', not the TEMPLATE_DECL. */
10038 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
10040 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
10041 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
10044 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10046 /* The friend template has already been declared. Just
10047 check to see that the declarations match, and install any new
10048 default parameters. We must tsubst the default parameters,
10049 of course. We only need the innermost template parameters
10050 because that is all that redeclare_class_template will look
10052 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10053 > TMPL_ARGS_DEPTH (args
))
10056 location_t saved_input_location
;
10057 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10058 args
, tf_warning_or_error
);
10060 saved_input_location
= input_location
;
10061 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10062 tree cons
= get_constraints (tmpl
);
10063 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10064 input_location
= saved_input_location
;
10068 friend_type
= TREE_TYPE (tmpl
);
10072 /* The friend template has not already been declared. In this
10073 case, the instantiation of the template class will cause the
10074 injection of this template into the global scope. */
10075 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10076 if (tmpl
== error_mark_node
)
10077 return error_mark_node
;
10079 /* The new TMPL is not an instantiation of anything, so we
10080 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10081 the new type because that is supposed to be the corresponding
10082 template decl, i.e., TMPL. */
10083 DECL_USE_TEMPLATE (tmpl
) = 0;
10084 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10085 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10086 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10087 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10089 /* Inject this template into the global scope. */
10090 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
, true));
10093 if (context
!= global_namespace
)
10095 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10096 pop_nested_namespace (context
);
10098 pop_nested_class ();
10101 return friend_type
;
10104 /* Returns zero if TYPE cannot be completed later due to circularity.
10105 Otherwise returns one. */
10108 can_complete_type_without_circularity (tree type
)
10110 if (type
== NULL_TREE
|| type
== error_mark_node
)
10112 else if (COMPLETE_TYPE_P (type
))
10114 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10115 return can_complete_type_without_circularity (TREE_TYPE (type
));
10116 else if (CLASS_TYPE_P (type
)
10117 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10123 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10124 tsubst_flags_t
, tree
);
10126 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10127 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10130 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10131 tsubst_flags_t complain
, tree in_decl
)
10133 gcc_assert (ATTR_IS_DEPENDENT (t
));
10135 tree val
= TREE_VALUE (t
);
10136 if (val
== NULL_TREE
)
10137 /* Nothing to do. */;
10138 else if ((flag_openmp
|| flag_openmp_simd
)
10139 && is_attribute_p ("omp declare simd",
10140 get_attribute_name (t
)))
10142 tree clauses
= TREE_VALUE (val
);
10143 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10144 complain
, in_decl
);
10145 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10146 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10147 tree parms
= DECL_ARGUMENTS (*decl_p
);
10149 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10151 val
= build_tree_list (NULL_TREE
, clauses
);
10155 /* If the first attribute argument is an identifier, don't
10156 pass it through tsubst. Attributes like mode, format,
10157 cleanup and several target specific attributes expect it
10159 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10162 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10163 /*integral_constant_expression_p=*/false);
10164 if (chain
!= TREE_CHAIN (val
))
10165 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10167 else if (PACK_EXPANSION_P (val
))
10169 /* An attribute pack expansion. */
10170 tree purp
= TREE_PURPOSE (t
);
10171 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10172 if (pack
== error_mark_node
)
10173 return error_mark_node
;
10174 int len
= TREE_VEC_LENGTH (pack
);
10175 tree list
= NULL_TREE
;
10177 for (int i
= 0; i
< len
; ++i
)
10179 tree elt
= TREE_VEC_ELT (pack
, i
);
10180 *q
= build_tree_list (purp
, elt
);
10181 q
= &TREE_CHAIN (*q
);
10186 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10187 /*integral_constant_expression_p=*/false);
10189 if (val
!= TREE_VALUE (t
))
10190 return build_tree_list (TREE_PURPOSE (t
), val
);
10194 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10195 unchanged or a new TREE_LIST chain. */
10198 tsubst_attributes (tree attributes
, tree args
,
10199 tsubst_flags_t complain
, tree in_decl
)
10201 tree last_dep
= NULL_TREE
;
10203 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10204 if (ATTR_IS_DEPENDENT (t
))
10207 attributes
= copy_list (attributes
);
10212 for (tree
*p
= &attributes
; *p
; )
10215 if (ATTR_IS_DEPENDENT (t
))
10217 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10222 p
= &TREE_CHAIN (*p
);
10224 *p
= TREE_CHAIN (t
);
10228 p
= &TREE_CHAIN (*p
);
10234 /* Apply any attributes which had to be deferred until instantiation
10235 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10236 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10239 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10240 tree args
, tsubst_flags_t complain
, tree in_decl
)
10242 tree last_dep
= NULL_TREE
;
10246 if (attributes
== NULL_TREE
)
10249 if (DECL_P (*decl_p
))
10251 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10253 p
= &DECL_ATTRIBUTES (*decl_p
);
10254 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10255 to our attributes parameter. */
10256 gcc_assert (*p
== attributes
);
10260 p
= &TYPE_ATTRIBUTES (*decl_p
);
10261 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10262 lookup_template_class_1, and should be preserved. */
10263 gcc_assert (*p
!= attributes
);
10265 p
= &TREE_CHAIN (*p
);
10268 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10269 if (ATTR_IS_DEPENDENT (t
))
10272 attributes
= copy_list (attributes
);
10279 tree late_attrs
= NULL_TREE
;
10280 tree
*q
= &late_attrs
;
10285 if (ATTR_IS_DEPENDENT (t
))
10287 *p
= TREE_CHAIN (t
);
10288 TREE_CHAIN (t
) = NULL_TREE
;
10289 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10291 q
= &TREE_CHAIN (*q
);
10295 p
= &TREE_CHAIN (t
);
10298 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10302 /* Perform (or defer) access check for typedefs that were referenced
10303 from within the template TMPL code.
10304 This is a subroutine of instantiate_decl and instantiate_class_template.
10305 TMPL is the template to consider and TARGS is the list of arguments of
10309 perform_typedefs_access_check (tree tmpl
, tree targs
)
10311 location_t saved_location
;
10313 qualified_typedef_usage_t
*iter
;
10316 || (!CLASS_TYPE_P (tmpl
)
10317 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10320 saved_location
= input_location
;
10321 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10323 tree type_decl
= iter
->typedef_decl
;
10324 tree type_scope
= iter
->context
;
10326 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10329 if (uses_template_parms (type_decl
))
10330 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10331 if (uses_template_parms (type_scope
))
10332 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10334 /* Make access check error messages point to the location
10335 of the use of the typedef. */
10336 input_location
= iter
->locus
;
10337 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10338 type_decl
, type_decl
,
10339 tf_warning_or_error
);
10341 input_location
= saved_location
;
10345 instantiate_class_template_1 (tree type
)
10347 tree templ
, args
, pattern
, t
, member
;
10351 unsigned int saved_maximum_field_alignment
;
10354 if (type
== error_mark_node
)
10355 return error_mark_node
;
10357 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10358 || uses_template_parms (type
))
10361 /* Figure out which template is being instantiated. */
10362 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10363 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10365 /* Determine what specialization of the original template to
10367 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10368 if (t
== error_mark_node
)
10370 TYPE_BEING_DEFINED (type
) = 1;
10371 return error_mark_node
;
10375 /* This TYPE is actually an instantiation of a partial
10376 specialization. We replace the innermost set of ARGS with
10377 the arguments appropriate for substitution. For example,
10380 template <class T> struct S {};
10381 template <class T> struct S<T*> {};
10383 and supposing that we are instantiating S<int*>, ARGS will
10384 presently be {int*} -- but we need {int}. */
10385 pattern
= TREE_TYPE (t
);
10386 args
= TREE_PURPOSE (t
);
10390 pattern
= TREE_TYPE (templ
);
10391 args
= CLASSTYPE_TI_ARGS (type
);
10394 /* If the template we're instantiating is incomplete, then clearly
10395 there's nothing we can do. */
10396 if (!COMPLETE_TYPE_P (pattern
))
10399 /* If we've recursively instantiated too many templates, stop. */
10400 if (! push_tinst_level (type
))
10403 /* Now we're really doing the instantiation. Mark the type as in
10404 the process of being defined. */
10405 TYPE_BEING_DEFINED (type
) = 1;
10407 /* We may be in the middle of deferred access check. Disable
10409 push_deferring_access_checks (dk_no_deferred
);
10411 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10412 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10414 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10415 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10416 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10417 fn_context
= error_mark_node
;
10419 push_to_top_level ();
10422 cp_unevaluated_operand
= 0;
10423 c_inhibit_evaluation_warnings
= 0;
10425 /* Use #pragma pack from the template context. */
10426 saved_maximum_field_alignment
= maximum_field_alignment
;
10427 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10429 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10431 /* Set the input location to the most specialized template definition.
10432 This is needed if tsubsting causes an error. */
10433 typedecl
= TYPE_MAIN_DECL (pattern
);
10434 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10435 DECL_SOURCE_LOCATION (typedecl
);
10437 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10438 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10439 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10440 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10441 if (ANON_AGGR_TYPE_P (pattern
))
10442 SET_ANON_AGGR_TYPE_P (type
);
10443 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10445 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10446 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10447 /* Adjust visibility for template arguments. */
10448 determine_visibility (TYPE_MAIN_DECL (type
));
10450 if (CLASS_TYPE_P (type
))
10451 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10453 pbinfo
= TYPE_BINFO (pattern
);
10455 /* We should never instantiate a nested class before its enclosing
10456 class; we need to look up the nested class by name before we can
10457 instantiate it, and that lookup should instantiate the enclosing
10459 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10460 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10462 base_list
= NULL_TREE
;
10463 if (BINFO_N_BASE_BINFOS (pbinfo
))
10469 /* We must enter the scope containing the type, as that is where
10470 the accessibility of types named in dependent bases are
10472 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10474 /* Substitute into each of the bases to determine the actual
10476 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10479 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10480 tree expanded_bases
= NULL_TREE
;
10483 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10486 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10487 args
, tf_error
, NULL_TREE
);
10488 if (expanded_bases
== error_mark_node
)
10491 len
= TREE_VEC_LENGTH (expanded_bases
);
10494 for (idx
= 0; idx
< len
; idx
++)
10496 if (expanded_bases
)
10497 /* Extract the already-expanded base class. */
10498 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10500 /* Substitute to figure out the base class. */
10501 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10504 if (base
== error_mark_node
)
10507 base_list
= tree_cons (access
, base
, base_list
);
10508 if (BINFO_VIRTUAL_P (pbase_binfo
))
10509 TREE_TYPE (base_list
) = integer_type_node
;
10513 /* The list is now in reverse order; correct that. */
10514 base_list
= nreverse (base_list
);
10517 pop_scope (pushed_scope
);
10519 /* Now call xref_basetypes to set up all the base-class
10521 xref_basetypes (type
, base_list
);
10523 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10524 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10525 args
, tf_error
, NULL_TREE
);
10526 fixup_attribute_variants (type
);
10528 /* Now that our base classes are set up, enter the scope of the
10529 class, so that name lookups into base classes, etc. will work
10530 correctly. This is precisely analogous to what we do in
10531 begin_class_definition when defining an ordinary non-template
10532 class, except we also need to push the enclosing classes. */
10533 push_nested_class (type
);
10535 /* Now members are processed in the order of declaration. */
10536 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10537 member
; member
= TREE_CHAIN (member
))
10539 tree t
= TREE_VALUE (member
);
10541 if (TREE_PURPOSE (member
))
10545 /* Build new CLASSTYPE_NESTED_UTDS. */
10548 bool class_template_p
;
10550 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10551 && TYPE_LANG_SPECIFIC (t
)
10552 && CLASSTYPE_IS_TEMPLATE (t
));
10553 /* If the member is a class template, then -- even after
10554 substitution -- there may be dependent types in the
10555 template argument list for the class. We increment
10556 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10557 that function will assume that no types are dependent
10558 when outside of a template. */
10559 if (class_template_p
)
10560 ++processing_template_decl
;
10561 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10562 if (class_template_p
)
10563 --processing_template_decl
;
10564 if (newtag
== error_mark_node
)
10567 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10569 tree name
= TYPE_IDENTIFIER (t
);
10571 if (class_template_p
)
10572 /* Unfortunately, lookup_template_class sets
10573 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10574 instantiation (i.e., for the type of a member
10575 template class nested within a template class.)
10576 This behavior is required for
10577 maybe_process_partial_specialization to work
10578 correctly, but is not accurate in this case;
10579 the TAG is not an instantiation of anything.
10580 (The corresponding TEMPLATE_DECL is an
10581 instantiation, but the TYPE is not.) */
10582 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10584 /* Now, we call pushtag to put this NEWTAG into the scope of
10585 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10586 pushtag calling push_template_decl. We don't have to do
10587 this for enums because it will already have been done in
10590 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10591 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10594 else if (DECL_DECLARES_FUNCTION_P (t
))
10598 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10599 ++processing_template_decl
;
10600 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10601 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10602 --processing_template_decl
;
10603 set_current_access_from_decl (r
);
10604 finish_member_declaration (r
);
10605 /* Instantiate members marked with attribute used. */
10606 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10608 if (TREE_CODE (r
) == FUNCTION_DECL
10609 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10610 cp_check_omp_declare_reduction (r
);
10612 else if (DECL_CLASS_TEMPLATE_P (t
)
10613 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10614 /* A closure type for a lambda in a default argument for a
10615 member template. Ignore it; it will be instantiated with
10616 the default argument. */;
10619 /* Build new TYPE_FIELDS. */
10620 if (TREE_CODE (t
) == STATIC_ASSERT
)
10624 ++c_inhibit_evaluation_warnings
;
10626 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10627 tf_warning_or_error
, NULL_TREE
,
10628 /*integral_constant_expression_p=*/true);
10629 --c_inhibit_evaluation_warnings
;
10631 finish_static_assert (condition
,
10632 STATIC_ASSERT_MESSAGE (t
),
10633 STATIC_ASSERT_SOURCE_LOCATION (t
),
10634 /*member_p=*/true);
10636 else if (TREE_CODE (t
) != CONST_DECL
)
10639 tree vec
= NULL_TREE
;
10642 /* The file and line for this declaration, to
10643 assist in error message reporting. Since we
10644 called push_tinst_level above, we don't need to
10646 input_location
= DECL_SOURCE_LOCATION (t
);
10648 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10649 ++processing_template_decl
;
10650 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10651 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10652 --processing_template_decl
;
10654 if (TREE_CODE (r
) == TREE_VEC
)
10656 /* A capture pack became multiple fields. */
10658 len
= TREE_VEC_LENGTH (vec
);
10661 for (int i
= 0; i
< len
; ++i
)
10664 r
= TREE_VEC_ELT (vec
, i
);
10669 [t]he initialization (and any associated
10670 side-effects) of a static data member does
10671 not occur unless the static data member is
10672 itself used in a way that requires the
10673 definition of the static data member to
10676 Therefore, we do not substitute into the
10677 initialized for the static data member here. */
10678 finish_static_data_member_decl
10680 /*init=*/NULL_TREE
,
10681 /*init_const_expr_p=*/false,
10682 /*asmspec_tree=*/NULL_TREE
,
10684 /* Instantiate members marked with attribute used. */
10685 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10688 else if (TREE_CODE (r
) == FIELD_DECL
)
10690 /* Determine whether R has a valid type and can be
10691 completed later. If R is invalid, then its type
10692 is replaced by error_mark_node. */
10693 tree rtype
= TREE_TYPE (r
);
10694 if (can_complete_type_without_circularity (rtype
))
10695 complete_type (rtype
);
10697 if (!complete_or_array_type_p (rtype
))
10699 /* If R's type couldn't be completed and
10700 it isn't a flexible array member (whose
10701 type is incomplete by definition) give
10703 cxx_incomplete_type_error (r
, rtype
);
10704 TREE_TYPE (r
) = error_mark_node
;
10708 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10709 such a thing will already have been added to the field
10710 list by tsubst_enum in finish_member_declaration in the
10711 CLASSTYPE_NESTED_UTDS case above. */
10712 if (!(TREE_CODE (r
) == TYPE_DECL
10713 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10714 && DECL_ARTIFICIAL (r
)))
10716 set_current_access_from_decl (r
);
10717 finish_member_declaration (r
);
10725 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10726 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10728 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10730 tree friend_type
= t
;
10731 bool adjust_processing_template_decl
= false;
10733 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10735 /* template <class T> friend class C; */
10736 friend_type
= tsubst_friend_class (friend_type
, args
);
10737 adjust_processing_template_decl
= true;
10739 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10741 /* template <class T> friend class C::D; */
10742 friend_type
= tsubst (friend_type
, args
,
10743 tf_warning_or_error
, NULL_TREE
);
10744 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10745 friend_type
= TREE_TYPE (friend_type
);
10746 adjust_processing_template_decl
= true;
10748 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10749 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10751 /* This could be either
10755 when dependent_type_p is false or
10757 template <class U> friend class T::C;
10760 /* Bump processing_template_decl in case this is something like
10761 template <class T> friend struct A<T>::B. */
10762 ++processing_template_decl
;
10763 friend_type
= tsubst (friend_type
, args
,
10764 tf_warning_or_error
, NULL_TREE
);
10765 if (dependent_type_p (friend_type
))
10766 adjust_processing_template_decl
= true;
10767 --processing_template_decl
;
10769 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10770 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10771 && TYPE_HIDDEN_P (friend_type
))
10775 where C hasn't been declared yet. Let's lookup name
10776 from namespace scope directly, bypassing any name that
10777 come from dependent base class. */
10778 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10780 /* The call to xref_tag_from_type does injection for friend
10782 push_nested_namespace (ns
);
10784 xref_tag_from_type (friend_type
, NULL_TREE
,
10785 /*tag_scope=*/ts_current
);
10786 pop_nested_namespace (ns
);
10788 else if (uses_template_parms (friend_type
))
10789 /* friend class C<T>; */
10790 friend_type
= tsubst (friend_type
, args
,
10791 tf_warning_or_error
, NULL_TREE
);
10796 where C is already declared or
10798 friend class C<int>;
10800 We don't have to do anything in these cases. */
10802 if (adjust_processing_template_decl
)
10803 /* Trick make_friend_class into realizing that the friend
10804 we're adding is a template, not an ordinary class. It's
10805 important that we use make_friend_class since it will
10806 perform some error-checking and output cross-reference
10808 ++processing_template_decl
;
10810 if (friend_type
!= error_mark_node
)
10811 make_friend_class (type
, friend_type
, /*complain=*/false);
10813 if (adjust_processing_template_decl
)
10814 --processing_template_decl
;
10818 /* Build new DECL_FRIENDLIST. */
10821 /* The file and line for this declaration, to
10822 assist in error message reporting. Since we
10823 called push_tinst_level above, we don't need to
10825 input_location
= DECL_SOURCE_LOCATION (t
);
10827 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10829 ++processing_template_decl
;
10830 push_deferring_access_checks (dk_no_check
);
10833 r
= tsubst_friend_function (t
, args
);
10834 add_friend (type
, r
, /*complain=*/false);
10835 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10837 pop_deferring_access_checks ();
10838 --processing_template_decl
;
10846 /* Restore these before substituting into the lambda capture
10848 cp_unevaluated_operand
= saved_unevaluated_operand
;
10849 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10852 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10854 tree decl
= lambda_function (type
);
10857 if (cxx_dialect
>= cxx17
)
10858 CLASSTYPE_LITERAL_P (type
) = true;
10860 if (!DECL_TEMPLATE_INFO (decl
)
10861 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10863 /* Set function_depth to avoid garbage collection. */
10865 instantiate_decl (decl
, /*defer_ok=*/false, false);
10869 /* We need to instantiate the capture list from the template
10870 after we've instantiated the closure members, but before we
10871 consider adding the conversion op. Also keep any captures
10872 that may have been added during instantiation of the op(). */
10873 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10875 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10876 args
, tf_warning_or_error
, NULL_TREE
,
10879 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10880 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10882 maybe_add_lambda_conv_op (type
);
10885 gcc_assert (errorcount
);
10888 /* Set the file and line number information to whatever is given for
10889 the class itself. This puts error messages involving generated
10890 implicit functions at a predictable point, and the same point
10891 that would be used for non-template classes. */
10892 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10894 unreverse_member_declarations (type
);
10895 finish_struct_1 (type
);
10896 TYPE_BEING_DEFINED (type
) = 0;
10898 /* We don't instantiate default arguments for member functions. 14.7.1:
10900 The implicit instantiation of a class template specialization causes
10901 the implicit instantiation of the declarations, but not of the
10902 definitions or default arguments, of the class member functions,
10903 member classes, static data members and member templates.... */
10905 /* Some typedefs referenced from within the template code need to be access
10906 checked at template instantiation time, i.e now. These types were
10907 added to the template at parsing time. Let's get those and perform
10908 the access checks then. */
10909 perform_typedefs_access_check (pattern
, args
);
10910 perform_deferred_access_checks (tf_warning_or_error
);
10911 pop_nested_class ();
10912 maximum_field_alignment
= saved_maximum_field_alignment
;
10914 pop_from_top_level ();
10915 pop_deferring_access_checks ();
10916 pop_tinst_level ();
10918 /* The vtable for a template class can be emitted in any translation
10919 unit in which the class is instantiated. When there is no key
10920 method, however, finish_struct_1 will already have added TYPE to
10921 the keyed_classes. */
10922 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10923 vec_safe_push (keyed_classes
, type
);
10928 /* Wrapper for instantiate_class_template_1. */
10931 instantiate_class_template (tree type
)
10934 timevar_push (TV_TEMPLATE_INST
);
10935 ret
= instantiate_class_template_1 (type
);
10936 timevar_pop (TV_TEMPLATE_INST
);
10941 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10947 else if (TYPE_P (t
))
10948 r
= tsubst (t
, args
, complain
, in_decl
);
10951 if (!(complain
& tf_warning
))
10952 ++c_inhibit_evaluation_warnings
;
10953 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10954 /*integral_constant_expression_p=*/true);
10955 if (!(complain
& tf_warning
))
10956 --c_inhibit_evaluation_warnings
;
10961 /* Given a function parameter pack TMPL_PARM and some function parameters
10962 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10963 and set *SPEC_P to point at the next point in the list. */
10966 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10968 /* Collect all of the extra "packed" parameters into an
10971 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10972 tree spec_parm
= *spec_p
;
10975 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10977 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10980 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10981 parmvec
= make_tree_vec (len
);
10982 spec_parm
= *spec_p
;
10983 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10984 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10986 /* Build the argument packs. */
10987 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10988 *spec_p
= spec_parm
;
10993 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10994 NONTYPE_ARGUMENT_PACK. */
10997 make_fnparm_pack (tree spec_parm
)
10999 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11002 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11003 pack expansion with no extra args, 2 if it has extra args, or 0
11004 if it is not a pack expansion. */
11007 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11009 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11010 if (i
>= TREE_VEC_LENGTH (vec
))
11012 tree elt
= TREE_VEC_ELT (vec
, i
);
11014 /* A decl pack is itself an expansion. */
11015 elt
= TREE_TYPE (elt
);
11016 if (!PACK_EXPANSION_P (elt
))
11018 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11024 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11027 make_argument_pack_select (tree arg_pack
, unsigned index
)
11029 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11031 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11032 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11037 /* This is a subroutine of tsubst_pack_expansion.
11039 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11040 mechanism to store the (non complete list of) arguments of the
11041 substitution and return a non substituted pack expansion, in order
11042 to wait for when we have enough arguments to really perform the
11046 use_pack_expansion_extra_args_p (tree parm_packs
,
11048 bool has_empty_arg
)
11050 /* If one pack has an expansion and another pack has a normal
11051 argument or if one pack has an empty argument and an another
11052 one hasn't then tsubst_pack_expansion cannot perform the
11053 substitution and need to fall back on the
11054 PACK_EXPANSION_EXTRA mechanism. */
11055 if (parm_packs
== NULL_TREE
)
11057 else if (has_empty_arg
)
11060 bool has_expansion_arg
= false;
11061 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11063 bool has_non_expansion_arg
= false;
11064 for (tree parm_pack
= parm_packs
;
11066 parm_pack
= TREE_CHAIN (parm_pack
))
11068 tree arg
= TREE_VALUE (parm_pack
);
11070 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11072 /* We can't substitute a pack expansion with extra args into
11076 has_expansion_arg
= true;
11078 has_non_expansion_arg
= true;
11081 if (has_expansion_arg
&& has_non_expansion_arg
)
11087 /* [temp.variadic]/6 says that:
11089 The instantiation of a pack expansion [...]
11090 produces a list E1,E2, ..., En, where N is the number of elements
11091 in the pack expansion parameters.
11093 This subroutine of tsubst_pack_expansion produces one of these Ei.
11095 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11096 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11097 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11098 INDEX is the index 'i' of the element Ei to produce. ARGS,
11099 COMPLAIN, and IN_DECL are the same parameters as for the
11100 tsubst_pack_expansion function.
11102 The function returns the resulting Ei upon successful completion,
11103 or error_mark_node.
11105 Note that this function possibly modifies the ARGS parameter, so
11106 it's the responsibility of the caller to restore it. */
11109 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11112 tree args
/* This parm gets
11114 tsubst_flags_t complain
,
11118 bool ith_elem_is_expansion
= false;
11120 /* For each parameter pack, change the substitution of the parameter
11121 pack to the ith argument in its argument pack, then expand the
11123 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11125 tree parm
= TREE_PURPOSE (pack
);
11126 tree arg_pack
= TREE_VALUE (pack
);
11127 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11129 ith_elem_is_expansion
|=
11130 argument_pack_element_is_expansion_p (arg_pack
, index
);
11132 /* Select the Ith argument from the pack. */
11133 if (TREE_CODE (parm
) == PARM_DECL
11134 || TREE_CODE (parm
) == FIELD_DECL
)
11138 aps
= make_argument_pack_select (arg_pack
, index
);
11139 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11140 return error_mark_node
;
11141 register_local_specialization (aps
, parm
);
11144 aps
= retrieve_local_specialization (parm
);
11149 template_parm_level_and_index (parm
, &level
, &idx
);
11153 aps
= make_argument_pack_select (arg_pack
, index
);
11154 /* Update the corresponding argument. */
11155 TMPL_ARG (args
, level
, idx
) = aps
;
11158 /* Re-use the ARGUMENT_PACK_SELECT. */
11159 aps
= TMPL_ARG (args
, level
, idx
);
11161 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11164 /* Substitute into the PATTERN with the (possibly altered)
11166 if (pattern
== in_decl
)
11167 /* Expanding a fixed parameter pack from
11168 coerce_template_parameter_pack. */
11169 t
= tsubst_decl (pattern
, args
, complain
);
11170 else if (pattern
== error_mark_node
)
11171 t
= error_mark_node
;
11172 else if (constraint_p (pattern
))
11174 if (processing_template_decl
)
11175 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11177 t
= (constraints_satisfied_p (pattern
, args
)
11178 ? boolean_true_node
: boolean_false_node
);
11180 else if (!TYPE_P (pattern
))
11181 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11182 /*integral_constant_expression_p=*/false);
11184 t
= tsubst (pattern
, args
, complain
, in_decl
);
11186 /* If the Ith argument pack element is a pack expansion, then
11187 the Ith element resulting from the substituting is going to
11188 be a pack expansion as well. */
11189 if (ith_elem_is_expansion
)
11190 t
= make_pack_expansion (t
, complain
);
11195 /* When the unexpanded parameter pack in a fold expression expands to an empty
11196 sequence, the value of the expression is as follows; the program is
11197 ill-formed if the operator is not listed in this table.
11204 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11206 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11207 if (!FOLD_EXPR_MODIFY_P (t
))
11210 case TRUTH_ANDIF_EXPR
:
11211 return boolean_true_node
;
11212 case TRUTH_ORIF_EXPR
:
11213 return boolean_false_node
;
11214 case COMPOUND_EXPR
:
11220 if (complain
& tf_error
)
11221 error_at (location_of (t
),
11222 "fold of empty expansion over %O", code
);
11223 return error_mark_node
;
11226 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11227 form an expression that combines the two terms using the
11231 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11233 tree op
= FOLD_EXPR_OP (t
);
11234 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11236 // Handle compound assignment operators.
11237 if (FOLD_EXPR_MODIFY_P (t
))
11238 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11242 case COMPOUND_EXPR
:
11243 return build_x_compound_expr (input_location
, left
, right
, complain
);
11245 return build_m_component_ref (left
, right
, complain
);
11247 return build_x_binary_op (input_location
, code
,
11248 left
, TREE_CODE (left
),
11249 right
, TREE_CODE (right
),
11255 /* Substitute ARGS into the pack of a fold expression T. */
11258 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11260 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11263 /* Substitute ARGS into the pack of a fold expression T. */
11266 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11268 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11271 /* Expand a PACK of arguments into a grouped as left fold.
11272 Given a pack containing elements A0, A1, ..., An and an
11273 operator @, this builds the expression:
11275 ((A0 @ A1) @ A2) ... @ An
11277 Note that PACK must not be empty.
11279 The operator is defined by the original fold expression T. */
11282 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11284 tree left
= TREE_VEC_ELT (pack
, 0);
11285 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11287 tree right
= TREE_VEC_ELT (pack
, i
);
11288 left
= fold_expression (t
, left
, right
, complain
);
11293 /* Substitute into a unary left fold expression. */
11296 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11299 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11300 if (pack
== error_mark_node
)
11301 return error_mark_node
;
11302 if (PACK_EXPANSION_P (pack
))
11304 tree r
= copy_node (t
);
11305 FOLD_EXPR_PACK (r
) = pack
;
11308 if (TREE_VEC_LENGTH (pack
) == 0)
11309 return expand_empty_fold (t
, complain
);
11311 return expand_left_fold (t
, pack
, complain
);
11314 /* Substitute into a binary left fold expression.
11316 Do ths by building a single (non-empty) vector of argumnts and
11317 building the expression from those elements. */
11320 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11323 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11324 if (pack
== error_mark_node
)
11325 return error_mark_node
;
11326 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11327 if (init
== error_mark_node
)
11328 return error_mark_node
;
11330 if (PACK_EXPANSION_P (pack
))
11332 tree r
= copy_node (t
);
11333 FOLD_EXPR_PACK (r
) = pack
;
11334 FOLD_EXPR_INIT (r
) = init
;
11338 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11339 TREE_VEC_ELT (vec
, 0) = init
;
11340 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11341 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11343 return expand_left_fold (t
, vec
, complain
);
11346 /* Expand a PACK of arguments into a grouped as right fold.
11347 Given a pack containing elementns A0, A1, ..., and an
11348 operator @, this builds the expression:
11350 A0@ ... (An-2 @ (An-1 @ An))
11352 Note that PACK must not be empty.
11354 The operator is defined by the original fold expression T. */
11357 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11359 // Build the expression.
11360 int n
= TREE_VEC_LENGTH (pack
);
11361 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11362 for (--n
; n
!= 0; --n
)
11364 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11365 right
= fold_expression (t
, left
, right
, complain
);
11370 /* Substitute into a unary right fold expression. */
11373 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11376 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11377 if (pack
== error_mark_node
)
11378 return error_mark_node
;
11379 if (PACK_EXPANSION_P (pack
))
11381 tree r
= copy_node (t
);
11382 FOLD_EXPR_PACK (r
) = pack
;
11385 if (TREE_VEC_LENGTH (pack
) == 0)
11386 return expand_empty_fold (t
, complain
);
11388 return expand_right_fold (t
, pack
, complain
);
11391 /* Substitute into a binary right fold expression.
11393 Do ths by building a single (non-empty) vector of arguments and
11394 building the expression from those elements. */
11397 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11400 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11401 if (pack
== error_mark_node
)
11402 return error_mark_node
;
11403 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11404 if (init
== error_mark_node
)
11405 return error_mark_node
;
11407 if (PACK_EXPANSION_P (pack
))
11409 tree r
= copy_node (t
);
11410 FOLD_EXPR_PACK (r
) = pack
;
11411 FOLD_EXPR_INIT (r
) = init
;
11415 int n
= TREE_VEC_LENGTH (pack
);
11416 tree vec
= make_tree_vec (n
+ 1);
11417 for (int i
= 0; i
< n
; ++i
)
11418 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11419 TREE_VEC_ELT (vec
, n
) = init
;
11421 return expand_right_fold (t
, vec
, complain
);
11425 /* Substitute ARGS into T, which is an pack expansion
11426 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11427 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11428 (if only a partial substitution could be performed) or
11429 ERROR_MARK_NODE if there was an error. */
11431 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11435 tree pack
, packs
= NULL_TREE
;
11436 bool unsubstituted_packs
= false;
11439 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11440 bool need_local_specializations
= false;
11443 gcc_assert (PACK_EXPANSION_P (t
));
11444 pattern
= PACK_EXPANSION_PATTERN (t
);
11446 /* Add in any args remembered from an earlier partial instantiation. */
11447 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11449 levels
= TMPL_ARGS_DEPTH (args
);
11451 /* Determine the argument packs that will instantiate the parameter
11452 packs used in the expansion expression. While we're at it,
11453 compute the number of arguments to be expanded and make sure it
11455 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11456 pack
= TREE_CHAIN (pack
))
11458 tree parm_pack
= TREE_VALUE (pack
);
11459 tree arg_pack
= NULL_TREE
;
11460 tree orig_arg
= NULL_TREE
;
11463 if (TREE_CODE (parm_pack
) == BASES
)
11465 gcc_assert (parm_pack
== pattern
);
11466 if (BASES_DIRECT (parm_pack
))
11467 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11468 args
, complain
, in_decl
, false));
11470 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11471 args
, complain
, in_decl
, false));
11473 else if (builtin_pack_call_p (parm_pack
))
11475 /* ??? Support use in other patterns. */
11476 gcc_assert (parm_pack
== pattern
);
11477 return expand_builtin_pack_call (parm_pack
, args
,
11478 complain
, in_decl
);
11480 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11482 /* We know we have correct local_specializations if this
11483 expansion is at function scope, or if we're dealing with a
11484 local parameter in a requires expression; for the latter,
11485 tsubst_requires_expr set it up appropriately. */
11486 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11487 arg_pack
= retrieve_local_specialization (parm_pack
);
11489 /* We can't rely on local_specializations for a parameter
11490 name used later in a function declaration (such as in a
11491 late-specified return type). Even if it exists, it might
11492 have the wrong value for a recursive call. */
11493 need_local_specializations
= true;
11497 /* This parameter pack was used in an unevaluated context. Just
11498 make a dummy decl, since it's only used for its type. */
11499 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11500 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11501 /* Partial instantiation of the parm_pack, we can't build
11502 up an argument pack yet. */
11503 arg_pack
= NULL_TREE
;
11505 arg_pack
= make_fnparm_pack (arg_pack
);
11508 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11509 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11513 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11515 if (level
<= levels
)
11516 arg_pack
= TMPL_ARG (args
, level
, idx
);
11519 orig_arg
= arg_pack
;
11520 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11521 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11523 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11524 /* This can only happen if we forget to expand an argument
11525 pack somewhere else. Just return an error, silently. */
11527 result
= make_tree_vec (1);
11528 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11535 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11537 /* Don't bother trying to do a partial substitution with
11538 incomplete packs; we'll try again after deduction. */
11539 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11544 else if (len
!= my_len
)
11546 if (!(complain
& tf_error
))
11547 /* Fail quietly. */;
11548 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11549 error ("mismatched argument pack lengths while expanding %qT",
11552 error ("mismatched argument pack lengths while expanding %qE",
11554 return error_mark_node
;
11557 /* Keep track of the parameter packs and their corresponding
11559 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11560 TREE_TYPE (packs
) = orig_arg
;
11564 /* We can't substitute for this parameter pack. We use a flag as
11565 well as the missing_level counter because function parameter
11566 packs don't have a level. */
11567 gcc_assert (processing_template_decl
);
11568 unsubstituted_packs
= true;
11572 /* If the expansion is just T..., return the matching argument pack, unless
11573 we need to call convert_from_reference on all the elements. This is an
11574 important optimization; see c++/68422. */
11575 if (!unsubstituted_packs
11576 && TREE_PURPOSE (packs
) == pattern
)
11578 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11579 /* Types need no adjustment, nor does sizeof..., and if we still have
11580 some pack expansion args we won't do anything yet. */
11581 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11582 || PACK_EXPANSION_SIZEOF_P (t
)
11583 || pack_expansion_args_count (args
))
11585 /* Also optimize expression pack expansions if we can tell that the
11586 elements won't have reference type. */
11587 tree type
= TREE_TYPE (pattern
);
11588 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11589 && !PACK_EXPANSION_P (type
)
11590 && !WILDCARD_TYPE_P (type
))
11592 /* Otherwise use the normal path so we get convert_from_reference. */
11595 /* We cannot expand this expansion expression, because we don't have
11596 all of the argument packs we need. */
11597 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11599 /* We got some full packs, but we can't substitute them in until we
11600 have values for all the packs. So remember these until then. */
11602 t
= make_pack_expansion (pattern
, complain
);
11603 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11606 else if (unsubstituted_packs
)
11608 /* There were no real arguments, we're just replacing a parameter
11609 pack with another version of itself. Substitute into the
11610 pattern and return a PACK_EXPANSION_*. The caller will need to
11612 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11613 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11614 /*integral_constant_expression_p=*/false);
11616 t
= tsubst (pattern
, args
, complain
, in_decl
);
11617 t
= make_pack_expansion (t
, complain
);
11621 gcc_assert (len
>= 0);
11623 if (need_local_specializations
)
11625 /* We're in a late-specified return type, so create our own local
11626 specializations map; the current map is either NULL or (in the
11627 case of recursive unification) might have bindings that we don't
11628 want to use or alter. */
11629 saved_local_specializations
= local_specializations
;
11630 local_specializations
= new hash_map
<tree
, tree
>;
11633 /* For each argument in each argument pack, substitute into the
11635 result
= make_tree_vec (len
);
11636 tree elem_args
= copy_template_args (args
);
11637 for (i
= 0; i
< len
; ++i
)
11639 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11641 elem_args
, complain
,
11643 TREE_VEC_ELT (result
, i
) = t
;
11644 if (t
== error_mark_node
)
11646 result
= error_mark_node
;
11651 /* Update ARGS to restore the substitution from parameter packs to
11652 their argument packs. */
11653 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11655 tree parm
= TREE_PURPOSE (pack
);
11657 if (TREE_CODE (parm
) == PARM_DECL
11658 || TREE_CODE (parm
) == FIELD_DECL
)
11659 register_local_specialization (TREE_TYPE (pack
), parm
);
11664 if (TREE_VALUE (pack
) == NULL_TREE
)
11667 template_parm_level_and_index (parm
, &level
, &idx
);
11669 /* Update the corresponding argument. */
11670 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11671 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11674 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11678 if (need_local_specializations
)
11680 delete local_specializations
;
11681 local_specializations
= saved_local_specializations
;
11684 /* If the dependent pack arguments were such that we end up with only a
11685 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11686 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11687 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11688 return TREE_VEC_ELT (result
, 0);
11693 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11694 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11695 parameter packs; all parms generated from a function parameter pack will
11696 have the same DECL_PARM_INDEX. */
11699 get_pattern_parm (tree parm
, tree tmpl
)
11701 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11704 if (DECL_ARTIFICIAL (parm
))
11706 for (patparm
= DECL_ARGUMENTS (pattern
);
11707 patparm
; patparm
= DECL_CHAIN (patparm
))
11708 if (DECL_ARTIFICIAL (patparm
)
11709 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11714 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11715 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11716 gcc_assert (DECL_PARM_INDEX (patparm
)
11717 == DECL_PARM_INDEX (parm
));
11723 /* Make an argument pack out of the TREE_VEC VEC. */
11726 make_argument_pack (tree vec
)
11729 tree elt
= TREE_VEC_ELT (vec
, 0);
11731 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11734 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11735 TREE_CONSTANT (pack
) = 1;
11737 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11741 /* Return an exact copy of template args T that can be modified
11745 copy_template_args (tree t
)
11747 if (t
== error_mark_node
)
11750 int len
= TREE_VEC_LENGTH (t
);
11751 tree new_vec
= make_tree_vec (len
);
11753 for (int i
= 0; i
< len
; ++i
)
11755 tree elt
= TREE_VEC_ELT (t
, i
);
11756 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11757 elt
= copy_template_args (elt
);
11758 TREE_VEC_ELT (new_vec
, i
) = elt
;
11761 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11762 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11767 /* Substitute ARGS into the vector or list of template arguments T. */
11770 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11773 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11776 if (t
== error_mark_node
)
11777 return error_mark_node
;
11779 len
= TREE_VEC_LENGTH (t
);
11780 elts
= XALLOCAVEC (tree
, len
);
11782 for (i
= 0; i
< len
; i
++)
11784 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11787 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11788 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11789 else if (PACK_EXPANSION_P (orig_arg
))
11791 /* Substitute into an expansion expression. */
11792 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11794 if (TREE_CODE (new_arg
) == TREE_VEC
)
11795 /* Add to the expanded length adjustment the number of
11796 expanded arguments. We subtract one from this
11797 measurement, because the argument pack expression
11798 itself is already counted as 1 in
11799 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11800 the argument pack is empty. */
11801 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11803 else if (ARGUMENT_PACK_P (orig_arg
))
11805 /* Substitute into each of the arguments. */
11806 new_arg
= TYPE_P (orig_arg
)
11807 ? cxx_make_type (TREE_CODE (orig_arg
))
11808 : make_node (TREE_CODE (orig_arg
));
11810 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11811 args
, complain
, in_decl
);
11812 if (pack_args
== error_mark_node
)
11813 new_arg
= error_mark_node
;
11815 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11817 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11818 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11821 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11823 if (new_arg
== error_mark_node
)
11824 return error_mark_node
;
11827 if (new_arg
!= orig_arg
)
11834 /* Make space for the expanded arguments coming from template
11836 t
= make_tree_vec (len
+ expanded_len_adjust
);
11837 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11838 arguments for a member template.
11839 In that case each TREE_VEC in ORIG_T represents a level of template
11840 arguments, and ORIG_T won't carry any non defaulted argument count.
11841 It will rather be the nested TREE_VECs that will carry one.
11842 In other words, ORIG_T carries a non defaulted argument count only
11843 if it doesn't contain any nested TREE_VEC. */
11844 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11846 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11847 count
+= expanded_len_adjust
;
11848 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11850 for (i
= 0, out
= 0; i
< len
; i
++)
11852 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11853 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11854 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11858 /* Now expand the template argument pack "in place". */
11859 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11860 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11864 TREE_VEC_ELT (t
, out
) = elts
[i
];
11872 /* Substitute ARGS into one level PARMS of template parameters. */
11875 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11877 if (parms
== error_mark_node
)
11878 return error_mark_node
;
11880 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11882 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11884 tree tuple
= TREE_VEC_ELT (parms
, i
);
11886 if (tuple
== error_mark_node
)
11889 TREE_VEC_ELT (new_vec
, i
) =
11890 tsubst_template_parm (tuple
, args
, complain
);
11896 /* Return the result of substituting ARGS into the template parameters
11897 given by PARMS. If there are m levels of ARGS and m + n levels of
11898 PARMS, then the result will contain n levels of PARMS. For
11899 example, if PARMS is `template <class T> template <class U>
11900 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11901 result will be `template <int*, double, class V>'. */
11904 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11906 tree r
= NULL_TREE
;
11909 /* When substituting into a template, we must set
11910 PROCESSING_TEMPLATE_DECL as the template parameters may be
11911 dependent if they are based on one-another, and the dependency
11912 predicates are short-circuit outside of templates. */
11913 ++processing_template_decl
;
11915 for (new_parms
= &r
;
11916 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11917 new_parms
= &(TREE_CHAIN (*new_parms
)),
11918 parms
= TREE_CHAIN (parms
))
11920 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11923 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11924 - TMPL_ARGS_DEPTH (args
)),
11925 new_vec
, NULL_TREE
);
11928 --processing_template_decl
;
11933 /* Return the result of substituting ARGS into one template parameter
11934 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11935 parameter and which TREE_PURPOSE is the default argument of the
11936 template parameter. */
11939 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11941 tree default_value
, parm_decl
;
11943 if (args
== NULL_TREE
11945 || t
== error_mark_node
)
11948 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11950 default_value
= TREE_PURPOSE (t
);
11951 parm_decl
= TREE_VALUE (t
);
11953 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11954 if (TREE_CODE (parm_decl
) == PARM_DECL
11955 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11956 parm_decl
= error_mark_node
;
11957 default_value
= tsubst_template_arg (default_value
, args
,
11958 complain
, NULL_TREE
);
11960 return build_tree_list (default_value
, parm_decl
);
11963 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11964 type T. If T is not an aggregate or enumeration type, it is
11965 handled as if by tsubst. IN_DECL is as for tsubst. If
11966 ENTERING_SCOPE is nonzero, T is the context for a template which
11967 we are presently tsubst'ing. Return the substituted value. */
11970 tsubst_aggr_type (tree t
,
11972 tsubst_flags_t complain
,
11974 int entering_scope
)
11976 if (t
== NULL_TREE
)
11979 switch (TREE_CODE (t
))
11982 if (TYPE_PTRMEMFUNC_P (t
))
11983 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11985 /* Fall through. */
11986 case ENUMERAL_TYPE
:
11988 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11993 int saved_unevaluated_operand
;
11994 int saved_inhibit_evaluation_warnings
;
11996 /* In "sizeof(X<I>)" we need to evaluate "I". */
11997 saved_unevaluated_operand
= cp_unevaluated_operand
;
11998 cp_unevaluated_operand
= 0;
11999 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12000 c_inhibit_evaluation_warnings
= 0;
12002 /* First, determine the context for the type we are looking
12004 context
= TYPE_CONTEXT (t
);
12005 if (context
&& TYPE_P (context
))
12007 context
= tsubst_aggr_type (context
, args
, complain
,
12008 in_decl
, /*entering_scope=*/1);
12009 /* If context is a nested class inside a class template,
12010 it may still need to be instantiated (c++/33959). */
12011 context
= complete_type (context
);
12014 /* Then, figure out what arguments are appropriate for the
12015 type we are trying to find. For example, given:
12017 template <class T> struct S;
12018 template <class T, class U> void f(T, U) { S<U> su; }
12020 and supposing that we are instantiating f<int, double>,
12021 then our ARGS will be {int, double}, but, when looking up
12022 S we only want {double}. */
12023 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12024 complain
, in_decl
);
12025 if (argvec
== error_mark_node
)
12026 r
= error_mark_node
;
12029 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12030 entering_scope
, complain
);
12031 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12034 cp_unevaluated_operand
= saved_unevaluated_operand
;
12035 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12040 /* This is not a template type, so there's nothing to do. */
12044 return tsubst (t
, args
, complain
, in_decl
);
12048 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12050 /* Substitute into the default argument ARG (a default argument for
12051 FN), which has the indicated TYPE. */
12054 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12055 tsubst_flags_t complain
)
12057 tree saved_class_ptr
= NULL_TREE
;
12058 tree saved_class_ref
= NULL_TREE
;
12059 int errs
= errorcount
+ sorrycount
;
12061 /* This can happen in invalid code. */
12062 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12065 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12066 parm
= chain_index (parmnum
, parm
);
12067 tree parmtype
= TREE_TYPE (parm
);
12068 if (DECL_BY_REFERENCE (parm
))
12069 parmtype
= TREE_TYPE (parmtype
);
12070 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12073 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12076 /* This default argument came from a template. Instantiate the
12077 default argument here, not in tsubst. In the case of
12086 we must be careful to do name lookup in the scope of S<T>,
12087 rather than in the current class. */
12088 push_access_scope (fn
);
12089 /* The "this" pointer is not valid in a default argument. */
12092 saved_class_ptr
= current_class_ptr
;
12093 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12094 saved_class_ref
= current_class_ref
;
12095 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12098 start_lambda_scope (parm
);
12100 push_deferring_access_checks(dk_no_deferred
);
12101 /* The default argument expression may cause implicitly defined
12102 member functions to be synthesized, which will result in garbage
12103 collection. We must treat this situation as if we were within
12104 the body of function so as to avoid collecting live data on the
12107 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12108 complain
, NULL_TREE
,
12109 /*integral_constant_expression_p=*/false);
12111 pop_deferring_access_checks();
12113 finish_lambda_scope ();
12115 /* Restore the "this" pointer. */
12118 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12119 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12122 if (errorcount
+sorrycount
> errs
12123 && (complain
& tf_warning_or_error
))
12124 inform (input_location
,
12125 " when instantiating default argument for call to %qD", fn
);
12127 /* Make sure the default argument is reasonable. */
12128 arg
= check_default_argument (type
, arg
, complain
);
12130 pop_access_scope (fn
);
12132 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12135 defarg_inst
= tree_cache_map::create_ggc (37);
12136 defarg_inst
->put (parm
, arg
);
12142 /* Substitute into all the default arguments for FN. */
12145 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12150 tmpl_args
= DECL_TI_ARGS (fn
);
12152 /* If this function is not yet instantiated, we certainly don't need
12153 its default arguments. */
12154 if (uses_template_parms (tmpl_args
))
12156 /* Don't do this again for clones. */
12157 if (DECL_CLONED_FUNCTION_P (fn
))
12161 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12163 arg
= TREE_CHAIN (arg
), ++i
)
12164 if (TREE_PURPOSE (arg
))
12165 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12167 TREE_PURPOSE (arg
),
12171 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12174 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12175 tree lambda_fntype
)
12177 tree gen_tmpl
, argvec
;
12178 hashval_t hash
= 0;
12181 /* Nobody should be tsubst'ing into non-template functions. */
12182 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12184 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12186 /* If T is not dependent, just return it. */
12187 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12190 /* Calculate the most general template of which R is a
12191 specialization, and the complete set of arguments used to
12193 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12194 argvec
= tsubst_template_args (DECL_TI_ARGS
12195 (DECL_TEMPLATE_RESULT
12196 (DECL_TI_TEMPLATE (t
))),
12197 args
, complain
, in_decl
);
12198 if (argvec
== error_mark_node
)
12199 return error_mark_node
;
12201 /* Check to see if we already have this specialization. */
12202 if (!lambda_fntype
)
12204 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12205 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12209 /* We can see more levels of arguments than parameters if
12210 there was a specialization of a member template, like
12213 template <class T> struct S { template <class U> void f(); }
12214 template <> template <class U> void S<int>::f(U);
12216 Here, we'll be substituting into the specialization,
12217 because that's where we can find the code we actually
12218 want to generate, but we'll have enough arguments for
12219 the most general template.
12221 We also deal with the peculiar case:
12223 template <class T> struct S {
12224 template <class U> friend void f();
12226 template <class U> void f() {}
12228 template void f<double>();
12230 Here, the ARGS for the instantiation of will be {int,
12231 double}. But, we only need as many ARGS as there are
12232 levels of template parameters in CODE_PATTERN. We are
12233 careful not to get fooled into reducing the ARGS in
12236 template <class T> struct S { template <class U> void f(U); }
12237 template <class T> template <> void S<T>::f(int) {}
12239 which we can spot because the pattern will be a
12240 specialization in this case. */
12241 int args_depth
= TMPL_ARGS_DEPTH (args
);
12243 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12245 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12246 args
= get_innermost_template_args (args
, parms_depth
);
12250 /* This special case arises when we have something like this:
12252 template <class T> struct S {
12253 friend void f<int>(int, double);
12256 Here, the DECL_TI_TEMPLATE for the friend declaration
12257 will be an IDENTIFIER_NODE. We are being called from
12258 tsubst_friend_function, and we want only to create a
12259 new decl (R) with appropriate types so that we can call
12260 determine_specialization. */
12261 gen_tmpl
= NULL_TREE
;
12262 argvec
= NULL_TREE
;
12265 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12267 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12268 bool member
= ctx
&& TYPE_P (ctx
);
12270 if (member
&& !closure
)
12271 ctx
= tsubst_aggr_type (ctx
, args
,
12272 complain
, t
, /*entering_scope=*/1);
12274 tree type
= (lambda_fntype
? lambda_fntype
12275 : tsubst (TREE_TYPE (t
), args
,
12276 complain
| tf_fndecl_type
, in_decl
));
12277 if (type
== error_mark_node
)
12278 return error_mark_node
;
12280 /* If we hit excessive deduction depth, the type is bogus even if
12281 it isn't error_mark_node, so don't build a decl. */
12282 if (excessive_deduction_depth
)
12283 return error_mark_node
;
12285 /* We do NOT check for matching decls pushed separately at this
12286 point, as they may not represent instantiations of this
12287 template, and in any case are considered separate under the
12289 tree r
= copy_decl (t
);
12290 DECL_USE_TEMPLATE (r
) = 0;
12291 TREE_TYPE (r
) = type
;
12292 /* Clear out the mangled name and RTL for the instantiation. */
12293 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12294 SET_DECL_RTL (r
, NULL
);
12295 /* Leave DECL_INITIAL set on deleted instantiations. */
12296 if (!DECL_DELETED_FN (r
))
12297 DECL_INITIAL (r
) = NULL_TREE
;
12298 DECL_CONTEXT (r
) = ctx
;
12300 /* OpenMP UDRs have the only argument a reference to the declared
12301 type. We want to diagnose if the declared type is a reference,
12302 which is invalid, but as references to references are usually
12303 quietly merged, diagnose it here. */
12304 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12307 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12308 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12309 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12310 error_at (DECL_SOURCE_LOCATION (t
),
12311 "reference type %qT in "
12312 "%<#pragma omp declare reduction%>", argtype
);
12313 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12314 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12318 if (member
&& DECL_CONV_FN_P (r
))
12319 /* Type-conversion operator. Reconstruct the name, in
12320 case it's the name of one of the template's parameters. */
12321 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12323 tree parms
= DECL_ARGUMENTS (t
);
12325 parms
= DECL_CHAIN (parms
);
12326 parms
= tsubst (parms
, args
, complain
, t
);
12327 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12328 DECL_CONTEXT (parm
) = r
;
12331 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12332 DECL_CHAIN (tparm
) = parms
;
12335 DECL_ARGUMENTS (r
) = parms
;
12336 DECL_RESULT (r
) = NULL_TREE
;
12338 TREE_STATIC (r
) = 0;
12339 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12340 DECL_EXTERNAL (r
) = 1;
12341 /* If this is an instantiation of a function with internal
12342 linkage, we already know what object file linkage will be
12343 assigned to the instantiation. */
12344 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12345 DECL_DEFER_OUTPUT (r
) = 0;
12346 DECL_CHAIN (r
) = NULL_TREE
;
12347 DECL_PENDING_INLINE_INFO (r
) = 0;
12348 DECL_PENDING_INLINE_P (r
) = 0;
12349 DECL_SAVED_TREE (r
) = NULL_TREE
;
12350 DECL_STRUCT_FUNCTION (r
) = NULL
;
12352 /* We'll re-clone as appropriate in instantiate_template. */
12353 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12355 /* If we aren't complaining now, return on error before we register
12356 the specialization so that we'll complain eventually. */
12357 if ((complain
& tf_error
) == 0
12358 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12359 && !grok_op_properties (r
, /*complain=*/false))
12360 return error_mark_node
;
12362 /* When instantiating a constrained member, substitute
12363 into the constraints to create a new constraint. */
12364 if (tree ci
= get_constraints (t
))
12367 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12368 set_constraints (r
, ci
);
12371 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12372 this in the special friend case mentioned above where
12373 GEN_TMPL is NULL. */
12374 if (gen_tmpl
&& !closure
)
12376 DECL_TEMPLATE_INFO (r
)
12377 = build_template_info (gen_tmpl
, argvec
);
12378 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12381 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12383 /* We instantiated this while substituting into
12384 the type earlier (template/friend54.C). */
12387 /* We're not supposed to instantiate default arguments
12388 until they are called, for a template. But, for a
12391 template <class T> void f ()
12392 { extern void g(int i = T()); }
12394 we should do the substitution when the template is
12395 instantiated. We handle the member function case in
12396 instantiate_class_template since the default arguments
12397 might refer to other members of the class. */
12399 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12400 && !uses_template_parms (argvec
))
12401 tsubst_default_arguments (r
, complain
);
12404 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12406 /* Copy the list of befriending classes. */
12407 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12409 friends
= &TREE_CHAIN (*friends
))
12411 *friends
= copy_node (*friends
);
12412 TREE_VALUE (*friends
)
12413 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12416 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12418 maybe_retrofit_in_chrg (r
);
12419 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12420 return error_mark_node
;
12421 /* If this is an instantiation of a member template, clone it.
12422 If it isn't, that'll be handled by
12423 clone_constructors_and_destructors. */
12424 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12425 clone_function_decl (r
, /*update_methods=*/false);
12427 else if ((complain
& tf_error
) != 0
12428 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12429 && !grok_op_properties (r
, /*complain=*/true))
12430 return error_mark_node
;
12432 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12433 SET_DECL_FRIEND_CONTEXT (r
,
12434 tsubst (DECL_FRIEND_CONTEXT (t
),
12435 args
, complain
, in_decl
));
12437 /* Possibly limit visibility based on template args. */
12438 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12439 if (DECL_VISIBILITY_SPECIFIED (t
))
12441 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12442 DECL_ATTRIBUTES (r
)
12443 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12445 determine_visibility (r
);
12446 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12447 && !processing_template_decl
)
12448 defaulted_late_check (r
);
12450 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12451 args
, complain
, in_decl
);
12455 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12458 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12459 tree lambda_fntype
)
12461 /* We can get here when processing a member function template,
12462 member class template, or template template parameter. */
12463 tree decl
= DECL_TEMPLATE_RESULT (t
);
12469 hashval_t hash
= 0;
12471 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12473 /* Template template parameter is treated here. */
12474 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12475 if (new_type
== error_mark_node
)
12476 r
= error_mark_node
;
12477 /* If we get a real template back, return it. This can happen in
12478 the context of most_specialized_partial_spec. */
12479 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12482 /* The new TEMPLATE_DECL was built in
12483 reduce_template_parm_level. */
12484 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12488 if (!lambda_fntype
)
12490 /* We might already have an instance of this template.
12491 The ARGS are for the surrounding class type, so the
12492 full args contain the tsubst'd args for the context,
12493 plus the innermost args from the template decl. */
12494 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12495 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12496 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12497 /* Because this is a template, the arguments will still be
12498 dependent, even after substitution. If
12499 PROCESSING_TEMPLATE_DECL is not set, the dependency
12500 predicates will short-circuit. */
12501 ++processing_template_decl
;
12502 full_args
= tsubst_template_args (tmpl_args
, args
,
12503 complain
, in_decl
);
12504 --processing_template_decl
;
12505 if (full_args
== error_mark_node
)
12506 return error_mark_node
;
12508 /* If this is a default template template argument,
12509 tsubst might not have changed anything. */
12510 if (full_args
== tmpl_args
)
12513 hash
= hash_tmpl_and_args (t
, full_args
);
12514 spec
= retrieve_specialization (t
, full_args
, hash
);
12515 if (spec
!= NULL_TREE
)
12519 /* Make a new template decl. It will be similar to the
12520 original, but will record the current template arguments.
12521 We also create a new function declaration, which is just
12522 like the old one, but points to this new template, rather
12523 than the old one. */
12525 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12526 DECL_CHAIN (r
) = NULL_TREE
;
12528 // Build new template info linking to the original template decl.
12529 if (!lambda_fntype
)
12531 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12532 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12535 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12537 /* The template parameters for this new template are all the
12538 template parameters for the old template, except the
12539 outermost level of parameters. */
12540 DECL_TEMPLATE_PARMS (r
)
12541 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12544 if (TREE_CODE (decl
) == TYPE_DECL
12545 && !TYPE_DECL_ALIAS_P (decl
))
12548 ++processing_template_decl
;
12549 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12550 --processing_template_decl
;
12551 if (new_type
== error_mark_node
)
12552 return error_mark_node
;
12554 TREE_TYPE (r
) = new_type
;
12555 /* For a partial specialization, we need to keep pointing to
12556 the primary template. */
12557 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12558 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12559 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12560 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12561 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12566 ++processing_template_decl
;
12567 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12568 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12570 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12571 --processing_template_decl
;
12572 if (new_decl
== error_mark_node
)
12573 return error_mark_node
;
12575 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12576 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12577 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12580 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12581 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12585 DECL_TI_TEMPLATE (new_decl
) = r
;
12586 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12590 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12591 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12593 if (PRIMARY_TEMPLATE_P (t
))
12594 DECL_PRIMARY_TEMPLATE (r
) = r
;
12596 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12598 /* Record this non-type partial instantiation. */
12599 register_specialization (r
, t
,
12600 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12606 /* True if FN is the op() for a lambda in an uninstantiated template. */
12609 lambda_fn_in_template_p (tree fn
)
12611 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12613 tree closure
= DECL_CONTEXT (fn
);
12614 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12617 /* True if FN is the op() for a lambda regenerated from a lambda in an
12618 uninstantiated template. */
12621 regenerated_lambda_fn_p (tree fn
)
12623 return (LAMBDA_FUNCTION_P (fn
)
12624 && !DECL_TEMPLATE_INSTANTIATION (fn
));
12627 /* We're instantiating a variable from template function TCTX. Return the
12628 corresponding current enclosing scope. This gets complicated because lambda
12629 functions in templates are regenerated rather than instantiated, but generic
12630 lambda functions are subsequently instantiated. */
12633 enclosing_instantiation_of (tree tctx
)
12635 tree fn
= current_function_decl
;
12636 int lambda_count
= 0;
12638 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12639 tctx
= decl_function_context (tctx
))
12641 for (; fn
; fn
= decl_function_context (fn
))
12644 int flambda_count
= 0;
12645 for (; fn
&& regenerated_lambda_fn_p (fn
);
12646 fn
= decl_function_context (fn
))
12648 if (DECL_TEMPLATE_INFO (fn
)
12649 ? most_general_template (fn
) != most_general_template (tctx
)
12655 while (flambda_count
-- > lambda_count
)
12656 fn
= decl_function_context (fn
);
12660 gcc_unreachable ();
12663 /* Substitute the ARGS into the T, which is a _DECL. Return the
12664 result of the substitution. Issue error and warning messages under
12665 control of COMPLAIN. */
12668 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12670 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12671 location_t saved_loc
;
12672 tree r
= NULL_TREE
;
12674 hashval_t hash
= 0;
12676 /* Set the filename and linenumber to improve error-reporting. */
12677 saved_loc
= input_location
;
12678 input_location
= DECL_SOURCE_LOCATION (t
);
12680 switch (TREE_CODE (t
))
12682 case TEMPLATE_DECL
:
12683 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12686 case FUNCTION_DECL
:
12687 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12692 tree type
= NULL_TREE
;
12694 tree expanded_types
= NULL_TREE
;
12695 tree prev_r
= NULL_TREE
;
12696 tree first_r
= NULL_TREE
;
12698 if (DECL_PACK_P (t
))
12700 /* If there is a local specialization that isn't a
12701 parameter pack, it means that we're doing a "simple"
12702 substitution from inside tsubst_pack_expansion. Just
12703 return the local specialization (which will be a single
12705 tree spec
= retrieve_local_specialization (t
);
12707 && TREE_CODE (spec
) == PARM_DECL
12708 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12711 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12712 the parameters in this function parameter pack. */
12713 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12714 complain
, in_decl
);
12715 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12717 len
= TREE_VEC_LENGTH (expanded_types
);
12719 /* Zero-length parameter packs are boring. Just substitute
12722 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12727 /* All we did was update the type. Make a note of that. */
12728 type
= expanded_types
;
12729 expanded_types
= NULL_TREE
;
12733 /* Loop through all of the parameters we'll build. When T is
12734 a function parameter pack, LEN is the number of expanded
12735 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12737 for (i
= 0; i
< len
; ++i
)
12741 if (DECL_TEMPLATE_PARM_P (t
))
12742 SET_DECL_TEMPLATE_PARM_P (r
);
12744 if (expanded_types
)
12745 /* We're on the Ith parameter of the function parameter
12748 /* Get the Ith type. */
12749 type
= TREE_VEC_ELT (expanded_types
, i
);
12751 /* Rename the parameter to include the index. */
12753 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12756 /* We're dealing with a normal parameter. */
12757 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12759 type
= type_decays_to (type
);
12760 TREE_TYPE (r
) = type
;
12761 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12763 if (DECL_INITIAL (r
))
12765 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12766 DECL_INITIAL (r
) = TREE_TYPE (r
);
12768 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12769 complain
, in_decl
);
12772 DECL_CONTEXT (r
) = NULL_TREE
;
12774 if (!DECL_TEMPLATE_PARM_P (r
))
12775 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12777 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12778 args
, complain
, in_decl
);
12780 /* Keep track of the first new parameter we
12781 generate. That's what will be returned to the
12786 /* Build a proper chain of parameters when substituting
12787 into a function parameter pack. */
12789 DECL_CHAIN (prev_r
) = r
;
12792 /* If cp_unevaluated_operand is set, we're just looking for a
12793 single dummy parameter, so don't keep going. */
12794 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12795 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12796 complain
, DECL_CHAIN (t
));
12798 /* FIRST_R contains the start of the chain we've built. */
12805 tree type
= NULL_TREE
;
12806 tree vec
= NULL_TREE
;
12807 tree expanded_types
= NULL_TREE
;
12810 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12812 /* This field is a lambda capture pack. Return a TREE_VEC of
12813 the expanded fields to instantiate_class_template_1 and
12814 store them in the specializations hash table as a
12815 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12816 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12817 complain
, in_decl
);
12818 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12820 len
= TREE_VEC_LENGTH (expanded_types
);
12821 vec
= make_tree_vec (len
);
12825 /* All we did was update the type. Make a note of that. */
12826 type
= expanded_types
;
12827 expanded_types
= NULL_TREE
;
12831 for (int i
= 0; i
< len
; ++i
)
12834 if (expanded_types
)
12836 type
= TREE_VEC_ELT (expanded_types
, i
);
12838 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12841 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12843 if (type
== error_mark_node
)
12844 RETURN (error_mark_node
);
12845 TREE_TYPE (r
) = type
;
12846 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12848 if (DECL_C_BIT_FIELD (r
))
12849 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12851 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12852 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12854 /*integral_constant_expression_p=*/true);
12855 if (DECL_INITIAL (t
))
12857 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12858 NSDMI in perform_member_init. Still set DECL_INITIAL
12859 so that we know there is one. */
12860 DECL_INITIAL (r
) = void_node
;
12861 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12862 retrofit_lang_decl (r
);
12863 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12865 /* We don't have to set DECL_CONTEXT here; it is set by
12866 finish_member_declaration. */
12867 DECL_CHAIN (r
) = NULL_TREE
;
12869 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12870 args
, complain
, in_decl
);
12873 TREE_VEC_ELT (vec
, i
) = r
;
12879 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12880 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12881 register_specialization (pack
, t
, args
, false, 0);
12887 /* We reach here only for member using decls. We also need to check
12888 uses_template_parms because DECL_DEPENDENT_P is not set for a
12889 using-declaration that designates a member of the current
12890 instantiation (c++/53549). */
12891 if (DECL_DEPENDENT_P (t
)
12892 || uses_template_parms (USING_DECL_SCOPE (t
)))
12894 tree scope
= USING_DECL_SCOPE (t
);
12895 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12896 if (PACK_EXPANSION_P (scope
))
12898 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12899 int len
= TREE_VEC_LENGTH (vec
);
12900 r
= make_tree_vec (len
);
12901 for (int i
= 0; i
< len
; ++i
)
12903 tree escope
= TREE_VEC_ELT (vec
, i
);
12904 tree elt
= do_class_using_decl (escope
, name
);
12907 r
= error_mark_node
;
12912 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12913 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12915 TREE_VEC_ELT (r
, i
) = elt
;
12920 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12921 complain
, in_decl
);
12922 r
= do_class_using_decl (inst_scope
, name
);
12924 r
= error_mark_node
;
12927 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12928 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12935 DECL_CHAIN (r
) = NULL_TREE
;
12942 tree argvec
= NULL_TREE
;
12943 tree gen_tmpl
= NULL_TREE
;
12945 tree tmpl
= NULL_TREE
;
12947 tree type
= NULL_TREE
;
12950 if (TREE_TYPE (t
) == error_mark_node
)
12951 RETURN (error_mark_node
);
12953 if (TREE_CODE (t
) == TYPE_DECL
12954 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12956 /* If this is the canonical decl, we don't have to
12957 mess with instantiations, and often we can't (for
12958 typename, template type parms and such). Note that
12959 TYPE_NAME is not correct for the above test if
12960 we've copied the type for a typedef. */
12961 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12962 if (type
== error_mark_node
)
12963 RETURN (error_mark_node
);
12964 r
= TYPE_NAME (type
);
12968 /* Check to see if we already have the specialization we
12971 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12973 /* T is a static data member or namespace-scope entity.
12974 We have to substitute into namespace-scope variables
12975 (not just variable templates) because of cases like:
12977 template <class T> void f() { extern T t; }
12979 where the entity referenced is not known until
12980 instantiation time. */
12982 ctx
= DECL_CONTEXT (t
);
12983 if (DECL_CLASS_SCOPE_P (t
))
12985 ctx
= tsubst_aggr_type (ctx
, args
,
12987 in_decl
, /*entering_scope=*/1);
12988 /* If CTX is unchanged, then T is in fact the
12989 specialization we want. That situation occurs when
12990 referencing a static data member within in its own
12991 class. We can use pointer equality, rather than
12992 same_type_p, because DECL_CONTEXT is always
12994 if (ctx
== DECL_CONTEXT (t
)
12995 /* ... unless T is a member template; in which
12996 case our caller can be willing to create a
12997 specialization of that template represented
12999 && !(DECL_TI_TEMPLATE (t
)
13000 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13006 tmpl
= DECL_TI_TEMPLATE (t
);
13007 gen_tmpl
= most_general_template (tmpl
);
13008 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13009 if (argvec
!= error_mark_node
)
13010 argvec
= (coerce_innermost_template_parms
13011 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13012 argvec
, t
, complain
,
13013 /*all*/true, /*defarg*/true));
13014 if (argvec
== error_mark_node
)
13015 RETURN (error_mark_node
);
13016 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13017 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13022 /* A local variable. */
13024 /* Subsequent calls to pushdecl will fill this in. */
13026 /* Unless this is a reference to a static variable from an
13027 enclosing function, in which case we need to fill it in now. */
13028 if (TREE_STATIC (t
))
13030 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13031 if (fn
!= current_function_decl
)
13034 spec
= retrieve_local_specialization (t
);
13036 /* If we already have the specialization we need, there is
13037 nothing more to do. */
13044 /* Create a new node for the specialization we need. */
13046 if (type
== NULL_TREE
)
13048 if (is_typedef_decl (t
))
13049 type
= DECL_ORIGINAL_TYPE (t
);
13051 type
= TREE_TYPE (t
);
13053 && VAR_HAD_UNKNOWN_BOUND (t
)
13054 && type
!= error_mark_node
)
13055 type
= strip_array_domain (type
);
13056 tree sub_args
= args
;
13057 if (tree auto_node
= type_uses_auto (type
))
13059 /* Mask off any template args past the variable's context so we
13060 don't replace the auto with an unrelated argument. */
13061 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13062 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13064 /* This should never happen with the new lambda instantiation
13065 model, but keep the handling just in case. */
13066 gcc_assert (!CHECKING_P
),
13067 sub_args
= strip_innermost_template_args (args
, extra
);
13069 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13073 /* Even if the original location is out of scope, the
13074 newly substituted one is not. */
13075 DECL_DEAD_FOR_LOCAL (r
) = 0;
13076 DECL_INITIALIZED_P (r
) = 0;
13077 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13078 if (type
== error_mark_node
)
13079 RETURN (error_mark_node
);
13080 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13082 /* It may seem that this case cannot occur, since:
13087 declares a function, not a variable. However:
13090 template <typename T> void g() { T t; }
13091 template void g<f>();
13093 is an attempt to declare a variable with function
13095 error ("variable %qD has function type",
13096 /* R is not yet sufficiently initialized, so we
13097 just use its name. */
13099 RETURN (error_mark_node
);
13101 type
= complete_type (type
);
13102 /* Wait until cp_finish_decl to set this again, to handle
13103 circular dependency (template/instantiate6.C). */
13104 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13105 type
= check_var_type (DECL_NAME (r
), type
);
13107 if (DECL_HAS_VALUE_EXPR_P (t
))
13109 tree ve
= DECL_VALUE_EXPR (t
);
13110 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13111 /*constant_expression_p=*/false);
13112 if (REFERENCE_REF_P (ve
))
13114 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13115 ve
= TREE_OPERAND (ve
, 0);
13117 SET_DECL_VALUE_EXPR (r
, ve
);
13119 if (CP_DECL_THREAD_LOCAL_P (r
)
13120 && !processing_template_decl
)
13121 set_decl_tls_model (r
, decl_default_tls_model (r
));
13123 else if (DECL_SELF_REFERENCE_P (t
))
13124 SET_DECL_SELF_REFERENCE_P (r
);
13125 TREE_TYPE (r
) = type
;
13126 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13127 DECL_CONTEXT (r
) = ctx
;
13128 /* Clear out the mangled name and RTL for the instantiation. */
13129 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13130 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13131 SET_DECL_RTL (r
, NULL
);
13132 /* The initializer must not be expanded until it is required;
13133 see [temp.inst]. */
13134 DECL_INITIAL (r
) = NULL_TREE
;
13135 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13138 if (DECL_LANG_SPECIFIC (r
))
13139 SET_DECL_DEPENDENT_INIT_P (r
, false);
13141 SET_DECL_MODE (r
, VOIDmode
);
13143 /* Possibly limit visibility based on template args. */
13144 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13145 if (DECL_VISIBILITY_SPECIFIED (t
))
13147 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13148 DECL_ATTRIBUTES (r
)
13149 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13151 determine_visibility (r
);
13156 /* A static data member declaration is always marked
13157 external when it is declared in-class, even if an
13158 initializer is present. We mimic the non-template
13159 processing here. */
13160 DECL_EXTERNAL (r
) = 1;
13161 if (DECL_NAMESPACE_SCOPE_P (t
))
13162 DECL_NOT_REALLY_EXTERN (r
) = 1;
13164 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13165 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13166 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13170 if (DECL_LANG_SPECIFIC (r
))
13171 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13172 if (!cp_unevaluated_operand
)
13173 register_local_specialization (r
, t
);
13176 DECL_CHAIN (r
) = NULL_TREE
;
13178 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13180 args
, complain
, in_decl
);
13182 /* Preserve a typedef that names a type. */
13183 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13185 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13186 set_underlying_type (r
);
13187 if (TYPE_DECL_ALIAS_P (r
))
13188 /* An alias template specialization can be dependent
13189 even if its underlying type is not. */
13190 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13193 layout_decl (r
, 0);
13198 gcc_unreachable ();
13203 /* Restore the file and line information. */
13204 input_location
= saved_loc
;
13209 /* Substitute into the ARG_TYPES of a function type.
13210 If END is a TREE_CHAIN, leave it and any following types
13214 tsubst_arg_types (tree arg_types
,
13217 tsubst_flags_t complain
,
13220 tree remaining_arg_types
;
13221 tree type
= NULL_TREE
;
13223 tree expanded_args
= NULL_TREE
;
13226 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13229 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13230 args
, end
, complain
, in_decl
);
13231 if (remaining_arg_types
== error_mark_node
)
13232 return error_mark_node
;
13234 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13236 /* For a pack expansion, perform substitution on the
13237 entire expression. Later on, we'll handle the arguments
13239 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13240 args
, complain
, in_decl
);
13242 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13243 /* So that we'll spin through the parameters, one by one. */
13244 i
= TREE_VEC_LENGTH (expanded_args
);
13247 /* We only partially substituted into the parameter
13248 pack. Our type is TYPE_PACK_EXPANSION. */
13249 type
= expanded_args
;
13250 expanded_args
= NULL_TREE
;
13258 type
= TREE_VEC_ELT (expanded_args
, i
);
13260 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13262 if (type
== error_mark_node
)
13263 return error_mark_node
;
13264 if (VOID_TYPE_P (type
))
13266 if (complain
& tf_error
)
13268 error ("invalid parameter type %qT", type
);
13270 error ("in declaration %q+D", in_decl
);
13272 return error_mark_node
;
13275 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13276 return error_mark_node
;
13278 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13279 top-level qualifiers as required. */
13280 type
= cv_unqualified (type_decays_to (type
));
13282 /* We do not substitute into default arguments here. The standard
13283 mandates that they be instantiated only when needed, which is
13284 done in build_over_call. */
13285 default_arg
= TREE_PURPOSE (arg_types
);
13287 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13288 since the new op() won't have any associated template arguments for us
13289 to refer to later. */
13290 if (lambda_fn_in_template_p (in_decl
))
13291 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13292 false/*fn*/, false/*constexpr*/);
13294 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13296 /* We've instantiated a template before its default arguments
13297 have been parsed. This can happen for a nested template
13298 class, and is not an error unless we require the default
13299 argument in a call of this function. */
13300 remaining_arg_types
=
13301 tree_cons (default_arg
, type
, remaining_arg_types
);
13302 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13305 remaining_arg_types
=
13306 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13309 return remaining_arg_types
;
13312 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13313 *not* handle the exception-specification for FNTYPE, because the
13314 initial substitution of explicitly provided template parameters
13315 during argument deduction forbids substitution into the
13316 exception-specification:
13320 All references in the function type of the function template to the
13321 corresponding template parameters are replaced by the specified tem-
13322 plate argument values. If a substitution in a template parameter or
13323 in the function type of the function template results in an invalid
13324 type, type deduction fails. [Note: The equivalent substitution in
13325 exception specifications is done only when the function is instanti-
13326 ated, at which point a program is ill-formed if the substitution
13327 results in an invalid type.] */
13330 tsubst_function_type (tree t
,
13332 tsubst_flags_t complain
,
13336 tree arg_types
= NULL_TREE
;
13339 /* The TYPE_CONTEXT is not used for function/method types. */
13340 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13342 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13344 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13346 if (late_return_type_p
)
13348 /* Substitute the argument types. */
13349 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13350 complain
, in_decl
);
13351 if (arg_types
== error_mark_node
)
13352 return error_mark_node
;
13354 tree save_ccp
= current_class_ptr
;
13355 tree save_ccr
= current_class_ref
;
13356 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13357 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13358 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13361 /* DR 1207: 'this' is in scope in the trailing return type. */
13362 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13365 /* Substitute the return type. */
13366 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13370 current_class_ptr
= save_ccp
;
13371 current_class_ref
= save_ccr
;
13375 /* Substitute the return type. */
13376 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13378 if (return_type
== error_mark_node
)
13379 return error_mark_node
;
13380 /* DR 486 clarifies that creation of a function type with an
13381 invalid return type is a deduction failure. */
13382 if (TREE_CODE (return_type
) == ARRAY_TYPE
13383 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13385 if (complain
& tf_error
)
13387 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13388 error ("function returning an array");
13390 error ("function returning a function");
13392 return error_mark_node
;
13395 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13396 return error_mark_node
;
13398 if (!late_return_type_p
)
13400 /* Substitute the argument types. */
13401 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13402 complain
, in_decl
);
13403 if (arg_types
== error_mark_node
)
13404 return error_mark_node
;
13407 /* Construct a new type node and return it. */
13408 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13410 fntype
= build_function_type (return_type
, arg_types
);
13411 fntype
= apply_memfn_quals (fntype
,
13412 type_memfn_quals (t
),
13413 type_memfn_rqual (t
));
13417 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13418 /* Don't pick up extra function qualifiers from the basetype. */
13419 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13420 if (! MAYBE_CLASS_TYPE_P (r
))
13424 Type deduction may fail for any of the following
13427 -- Attempting to create "pointer to member of T" when T
13428 is not a class type. */
13429 if (complain
& tf_error
)
13430 error ("creating pointer to member function of non-class type %qT",
13432 return error_mark_node
;
13435 fntype
= build_method_type_directly (r
, return_type
,
13436 TREE_CHAIN (arg_types
));
13437 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13439 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13441 if (late_return_type_p
)
13442 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13447 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13448 ARGS into that specification, and return the substituted
13449 specification. If there is no specification, return NULL_TREE. */
13452 tsubst_exception_specification (tree fntype
,
13454 tsubst_flags_t complain
,
13461 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13462 new_specs
= NULL_TREE
;
13463 if (specs
&& TREE_PURPOSE (specs
))
13465 /* A noexcept-specifier. */
13466 tree expr
= TREE_PURPOSE (specs
);
13467 if (TREE_CODE (expr
) == INTEGER_CST
)
13471 /* Defer instantiation of noexcept-specifiers to avoid
13472 excessive instantiations (c++/49107). */
13473 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13474 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13476 /* We already partially instantiated this member template,
13477 so combine the new args with the old. */
13478 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13479 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13480 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13481 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13485 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13486 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13490 new_specs
= tsubst_copy_and_build
13491 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13492 /*integral_constant_expression_p=*/true);
13493 new_specs
= build_noexcept_spec (new_specs
, complain
);
13497 if (! TREE_VALUE (specs
))
13504 tree expanded_specs
= NULL_TREE
;
13506 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13508 /* Expand the pack expansion type. */
13509 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13513 if (expanded_specs
== error_mark_node
)
13514 return error_mark_node
;
13515 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13516 len
= TREE_VEC_LENGTH (expanded_specs
);
13519 /* We're substituting into a member template, so
13520 we got a TYPE_PACK_EXPANSION back. Add that
13521 expansion and move on. */
13522 gcc_assert (TREE_CODE (expanded_specs
)
13523 == TYPE_PACK_EXPANSION
);
13524 new_specs
= add_exception_specifier (new_specs
,
13527 specs
= TREE_CHAIN (specs
);
13532 for (i
= 0; i
< len
; ++i
)
13534 if (expanded_specs
)
13535 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13537 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13538 if (spec
== error_mark_node
)
13540 new_specs
= add_exception_specifier (new_specs
, spec
,
13544 specs
= TREE_CHAIN (specs
);
13550 /* Take the tree structure T and replace template parameters used
13551 therein with the argument vector ARGS. IN_DECL is an associated
13552 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13553 Issue error and warning messages under control of COMPLAIN. Note
13554 that we must be relatively non-tolerant of extensions here, in
13555 order to preserve conformance; if we allow substitutions that
13556 should not be allowed, we may allow argument deductions that should
13557 not succeed, and therefore report ambiguous overload situations
13558 where there are none. In theory, we could allow the substitution,
13559 but indicate that it should have failed, and allow our caller to
13560 make sure that the right thing happens, but we don't try to do this
13563 This function is used for dealing with types, decls and the like;
13564 for expressions, use tsubst_expr or tsubst_copy. */
13567 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13569 enum tree_code code
;
13570 tree type
, r
= NULL_TREE
;
13572 if (t
== NULL_TREE
|| t
== error_mark_node
13573 || t
== integer_type_node
13574 || t
== void_type_node
13575 || t
== char_type_node
13576 || t
== unknown_type_node
13577 || TREE_CODE (t
) == NAMESPACE_DECL
13578 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13582 return tsubst_decl (t
, args
, complain
);
13584 if (args
== NULL_TREE
)
13587 code
= TREE_CODE (t
);
13589 if (code
== IDENTIFIER_NODE
)
13590 type
= IDENTIFIER_TYPE_VALUE (t
);
13592 type
= TREE_TYPE (t
);
13594 gcc_assert (type
!= unknown_type_node
);
13596 /* Reuse typedefs. We need to do this to handle dependent attributes,
13597 such as attribute aligned. */
13599 && typedef_variant_p (t
))
13601 tree decl
= TYPE_NAME (t
);
13603 if (alias_template_specialization_p (t
))
13605 /* DECL represents an alias template and we want to
13607 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13608 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13609 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13611 else if (DECL_CLASS_SCOPE_P (decl
)
13612 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13613 && uses_template_parms (DECL_CONTEXT (decl
)))
13615 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13616 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13617 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13619 else if (DECL_FUNCTION_SCOPE_P (decl
)
13620 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13621 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13622 r
= retrieve_local_specialization (decl
);
13624 /* The typedef is from a non-template context. */
13630 r
= cp_build_qualified_type_real
13631 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13632 complain
| tf_ignore_bad_quals
);
13637 /* We don't have an instantiation yet, so drop the typedef. */
13638 int quals
= cp_type_quals (t
);
13639 t
= DECL_ORIGINAL_TYPE (decl
);
13640 t
= cp_build_qualified_type_real (t
, quals
,
13641 complain
| tf_ignore_bad_quals
);
13645 bool fndecl_type
= (complain
& tf_fndecl_type
);
13646 complain
&= ~tf_fndecl_type
;
13649 && code
!= TYPENAME_TYPE
13650 && code
!= TEMPLATE_TYPE_PARM
13651 && code
!= TEMPLATE_PARM_INDEX
13652 && code
!= IDENTIFIER_NODE
13653 && code
!= FUNCTION_TYPE
13654 && code
!= METHOD_TYPE
)
13655 type
= tsubst (type
, args
, complain
, in_decl
);
13656 if (type
== error_mark_node
)
13657 return error_mark_node
;
13663 case ENUMERAL_TYPE
:
13664 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13665 /*entering_scope=*/0);
13668 case IDENTIFIER_NODE
:
13679 if (t
== integer_type_node
)
13682 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13683 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13687 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13689 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13690 /*integral_constant_expression_p=*/false);
13692 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13694 if (TREE_CODE (max
) == NOP_EXPR
13695 && TREE_SIDE_EFFECTS (omax
)
13696 && !TREE_TYPE (max
))
13697 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13699 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13700 with TREE_SIDE_EFFECTS that indicates this is not an integral
13701 constant expression. */
13702 if (processing_template_decl
13703 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13705 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13706 TREE_SIDE_EFFECTS (max
) = 1;
13709 return compute_array_index_type (NULL_TREE
, max
, complain
);
13712 case TEMPLATE_TYPE_PARM
:
13713 case TEMPLATE_TEMPLATE_PARM
:
13714 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13715 case TEMPLATE_PARM_INDEX
:
13720 tree arg
= NULL_TREE
;
13722 /* Early in template argument deduction substitution, we don't
13723 want to reduce the level of 'auto', or it will be confused
13724 with a normal template parm in subsequent deduction. */
13725 if (is_auto (t
) && (complain
& tf_partial
))
13730 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13731 template_parm_level_and_index (t
, &level
, &idx
);
13733 levels
= TMPL_ARGS_DEPTH (args
);
13734 if (level
<= levels
13735 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13737 arg
= TMPL_ARG (args
, level
, idx
);
13739 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13741 /* See through ARGUMENT_PACK_SELECT arguments. */
13742 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13743 /* If the selected argument is an expansion E, that most
13744 likely means we were called from
13745 gen_elem_of_pack_expansion_instantiation during the
13746 substituting of pack an argument pack (which Ith
13747 element is a pack expansion, where I is
13748 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13749 In this case, the Ith element resulting from this
13750 substituting is going to be a pack expansion, which
13751 pattern is the pattern of E. Let's return the
13753 gen_elem_of_pack_expansion_instantiation will
13754 build the resulting pack expansion from it. */
13755 if (PACK_EXPANSION_P (arg
))
13757 /* Make sure we aren't throwing away arg info. */
13758 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13759 arg
= PACK_EXPANSION_PATTERN (arg
);
13764 if (arg
== error_mark_node
)
13765 return error_mark_node
;
13766 else if (arg
!= NULL_TREE
)
13768 if (ARGUMENT_PACK_P (arg
))
13769 /* If ARG is an argument pack, we don't actually want to
13770 perform a substitution here, because substitutions
13771 for argument packs are only done
13772 element-by-element. We can get to this point when
13773 substituting the type of a non-type template
13774 parameter pack, when that type actually contains
13775 template parameter packs from an outer template, e.g.,
13777 template<typename... Types> struct A {
13778 template<Types... Values> struct B { };
13782 if (code
== TEMPLATE_TYPE_PARM
)
13785 gcc_assert (TYPE_P (arg
));
13787 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13789 return cp_build_qualified_type_real
13790 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13792 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13794 /* We are processing a type constructed from a
13795 template template parameter. */
13796 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13797 args
, complain
, in_decl
);
13798 if (argvec
== error_mark_node
)
13799 return error_mark_node
;
13801 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13802 || TREE_CODE (arg
) == TEMPLATE_DECL
13803 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13805 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13806 /* Consider this code:
13808 template <template <class> class Template>
13810 template <class Arg> using Bind = Template<Arg>;
13813 template <template <class> class Template, class Arg>
13814 using Instantiate = Template<Arg>; //#0
13816 template <template <class> class Template,
13819 Instantiate<Internal<Template>::template Bind,
13822 When #1 is parsed, the
13823 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13824 parameter `Template' in #0 matches the
13825 UNBOUND_CLASS_TEMPLATE representing the argument
13826 `Internal<Template>::template Bind'; We then want
13827 to assemble the type `Bind<Argument>' that can't
13828 be fully created right now, because
13829 `Internal<Template>' not being complete, the Bind
13830 template cannot be looked up in that context. So
13831 we need to "store" `Bind<Argument>' for later
13832 when the context of Bind becomes complete. Let's
13833 store that in a TYPENAME_TYPE. */
13834 return make_typename_type (TYPE_CONTEXT (arg
),
13835 build_nt (TEMPLATE_ID_EXPR
,
13836 TYPE_IDENTIFIER (arg
),
13841 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13842 are resolving nested-types in the signature of a
13843 member function templates. Otherwise ARG is a
13844 TEMPLATE_DECL and is the real template to be
13846 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13847 arg
= TYPE_NAME (arg
);
13849 r
= lookup_template_class (arg
,
13851 DECL_CONTEXT (arg
),
13852 /*entering_scope=*/0,
13854 return cp_build_qualified_type_real
13855 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13857 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13860 /* TEMPLATE_PARM_INDEX. */
13861 return convert_from_reference (unshare_expr (arg
));
13865 /* This can happen during the attempted tsubst'ing in
13866 unify. This means that we don't yet have any information
13867 about the template parameter in question. */
13870 /* If we get here, we must have been looking at a parm for a
13871 more deeply nested template. Make a new version of this
13872 template parameter, but with a lower level. */
13875 case TEMPLATE_TYPE_PARM
:
13876 case TEMPLATE_TEMPLATE_PARM
:
13877 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13878 if (cp_type_quals (t
))
13880 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13881 r
= cp_build_qualified_type_real
13882 (r
, cp_type_quals (t
),
13883 complain
| (code
== TEMPLATE_TYPE_PARM
13884 ? tf_ignore_bad_quals
: 0));
13886 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13887 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13888 && (r
= (TEMPLATE_PARM_DESCENDANTS
13889 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13890 && (r
= TREE_TYPE (r
))
13891 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13892 /* Break infinite recursion when substituting the constraints
13893 of a constrained placeholder. */;
13897 TEMPLATE_TYPE_PARM_INDEX (r
)
13898 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13899 r
, levels
, args
, complain
);
13900 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13901 TYPE_MAIN_VARIANT (r
) = r
;
13902 TYPE_POINTER_TO (r
) = NULL_TREE
;
13903 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13905 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13907 /* Propagate constraints on placeholders. */
13908 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13909 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13910 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13911 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13913 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13914 pl
= tsubst (pl
, args
, complain
, in_decl
);
13915 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13919 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13920 /* We have reduced the level of the template
13921 template parameter, but not the levels of its
13922 template parameters, so canonical_type_parameter
13923 will not be able to find the canonical template
13924 template parameter for this level. Thus, we
13925 require structural equality checking to compare
13926 TEMPLATE_TEMPLATE_PARMs. */
13927 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13928 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13929 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13931 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13933 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13935 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13936 /* We might need to substitute into the types of non-type
13937 template parameters. */
13938 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13939 complain
, in_decl
);
13940 if (tmpl
== error_mark_node
)
13941 return error_mark_node
;
13942 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13943 complain
, in_decl
);
13944 if (argvec
== error_mark_node
)
13945 return error_mark_node
;
13947 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13948 = build_template_info (tmpl
, argvec
);
13953 case TEMPLATE_PARM_INDEX
:
13954 /* OK, now substitute the type of the non-type parameter. We
13955 couldn't do it earlier because it might be an auto parameter,
13956 and we wouldn't need to if we had an argument. */
13957 type
= tsubst (type
, args
, complain
, in_decl
);
13958 if (type
== error_mark_node
)
13959 return error_mark_node
;
13960 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13964 gcc_unreachable ();
13972 tree purpose
, value
, chain
;
13974 if (t
== void_list_node
)
13977 purpose
= TREE_PURPOSE (t
);
13980 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13981 if (purpose
== error_mark_node
)
13982 return error_mark_node
;
13984 value
= TREE_VALUE (t
);
13987 value
= tsubst (value
, args
, complain
, in_decl
);
13988 if (value
== error_mark_node
)
13989 return error_mark_node
;
13991 chain
= TREE_CHAIN (t
);
13992 if (chain
&& chain
!= void_type_node
)
13994 chain
= tsubst (chain
, args
, complain
, in_decl
);
13995 if (chain
== error_mark_node
)
13996 return error_mark_node
;
13998 if (purpose
== TREE_PURPOSE (t
)
13999 && value
== TREE_VALUE (t
)
14000 && chain
== TREE_CHAIN (t
))
14002 return hash_tree_cons (purpose
, value
, chain
);
14006 /* We should never be tsubsting a binfo. */
14007 gcc_unreachable ();
14010 /* A vector of template arguments. */
14011 gcc_assert (!type
);
14012 return tsubst_template_args (t
, args
, complain
, in_decl
);
14015 case REFERENCE_TYPE
:
14017 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14022 Type deduction may fail for any of the following
14025 -- Attempting to create a pointer to reference type.
14026 -- Attempting to create a reference to a reference type or
14027 a reference to void.
14029 Core issue 106 says that creating a reference to a reference
14030 during instantiation is no longer a cause for failure. We
14031 only enforce this check in strict C++98 mode. */
14032 if ((TREE_CODE (type
) == REFERENCE_TYPE
14033 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14034 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14036 static location_t last_loc
;
14038 /* We keep track of the last time we issued this error
14039 message to avoid spewing a ton of messages during a
14040 single bad template instantiation. */
14041 if (complain
& tf_error
14042 && last_loc
!= input_location
)
14044 if (VOID_TYPE_P (type
))
14045 error ("forming reference to void");
14046 else if (code
== POINTER_TYPE
)
14047 error ("forming pointer to reference type %qT", type
);
14049 error ("forming reference to reference type %qT", type
);
14050 last_loc
= input_location
;
14053 return error_mark_node
;
14055 else if (TREE_CODE (type
) == FUNCTION_TYPE
14056 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14057 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14059 if (complain
& tf_error
)
14061 if (code
== POINTER_TYPE
)
14062 error ("forming pointer to qualified function type %qT",
14065 error ("forming reference to qualified function type %qT",
14068 return error_mark_node
;
14070 else if (code
== POINTER_TYPE
)
14072 r
= build_pointer_type (type
);
14073 if (TREE_CODE (type
) == METHOD_TYPE
)
14074 r
= build_ptrmemfunc_type (r
);
14076 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14077 /* In C++0x, during template argument substitution, when there is an
14078 attempt to create a reference to a reference type, reference
14079 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14081 "If a template-argument for a template-parameter T names a type
14082 that is a reference to a type A, an attempt to create the type
14083 'lvalue reference to cv T' creates the type 'lvalue reference to
14084 A,' while an attempt to create the type type rvalue reference to
14085 cv T' creates the type T"
14087 r
= cp_build_reference_type
14089 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14091 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14092 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14094 if (r
!= error_mark_node
)
14095 /* Will this ever be needed for TYPE_..._TO values? */
14102 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14103 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14107 Type deduction may fail for any of the following
14110 -- Attempting to create "pointer to member of T" when T
14111 is not a class type. */
14112 if (complain
& tf_error
)
14113 error ("creating pointer to member of non-class type %qT", r
);
14114 return error_mark_node
;
14116 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14118 if (complain
& tf_error
)
14119 error ("creating pointer to member reference type %qT", type
);
14120 return error_mark_node
;
14122 if (VOID_TYPE_P (type
))
14124 if (complain
& tf_error
)
14125 error ("creating pointer to member of type void");
14126 return error_mark_node
;
14128 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14129 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14131 /* The type of the implicit object parameter gets its
14132 cv-qualifiers from the FUNCTION_TYPE. */
14135 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14136 type_memfn_rqual (type
));
14137 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14138 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14142 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14146 case FUNCTION_TYPE
:
14151 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14152 if (fntype
== error_mark_node
)
14153 return error_mark_node
;
14155 /* Substitute the exception specification. */
14156 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14157 /*defer_ok*/fndecl_type
);
14158 if (specs
== error_mark_node
)
14159 return error_mark_node
;
14161 fntype
= build_exception_variant (fntype
, specs
);
14166 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14167 if (domain
== error_mark_node
)
14168 return error_mark_node
;
14170 /* As an optimization, we avoid regenerating the array type if
14171 it will obviously be the same as T. */
14172 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14175 /* These checks should match the ones in create_array_type_for_decl.
14179 The deduction may fail for any of the following reasons:
14181 -- Attempting to create an array with an element type that
14182 is void, a function type, or a reference type, or [DR337]
14183 an abstract class type. */
14184 if (VOID_TYPE_P (type
)
14185 || TREE_CODE (type
) == FUNCTION_TYPE
14186 || (TREE_CODE (type
) == ARRAY_TYPE
14187 && TYPE_DOMAIN (type
) == NULL_TREE
)
14188 || TREE_CODE (type
) == REFERENCE_TYPE
)
14190 if (complain
& tf_error
)
14191 error ("creating array of %qT", type
);
14192 return error_mark_node
;
14195 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14196 return error_mark_node
;
14198 r
= build_cplus_array_type (type
, domain
);
14200 if (TYPE_USER_ALIGN (t
))
14202 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14203 TYPE_USER_ALIGN (r
) = 1;
14209 case TYPENAME_TYPE
:
14211 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14212 in_decl
, /*entering_scope=*/1);
14213 if (ctx
== error_mark_node
)
14214 return error_mark_node
;
14216 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14217 complain
, in_decl
);
14218 if (f
== error_mark_node
)
14219 return error_mark_node
;
14221 if (!MAYBE_CLASS_TYPE_P (ctx
))
14223 if (complain
& tf_error
)
14224 error ("%qT is not a class, struct, or union type", ctx
);
14225 return error_mark_node
;
14227 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14229 /* Normally, make_typename_type does not require that the CTX
14230 have complete type in order to allow things like:
14232 template <class T> struct S { typename S<T>::X Y; };
14234 But, such constructs have already been resolved by this
14235 point, so here CTX really should have complete type, unless
14236 it's a partial instantiation. */
14237 ctx
= complete_type (ctx
);
14238 if (!COMPLETE_TYPE_P (ctx
))
14240 if (complain
& tf_error
)
14241 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14242 return error_mark_node
;
14246 f
= make_typename_type (ctx
, f
, typename_type
,
14247 complain
| tf_keep_type_decl
);
14248 if (f
== error_mark_node
)
14250 if (TREE_CODE (f
) == TYPE_DECL
)
14252 complain
|= tf_ignore_bad_quals
;
14256 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14258 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14260 if (complain
& tf_error
)
14261 error ("%qT resolves to %qT, which is not an enumeration type",
14264 return error_mark_node
;
14266 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14268 if (complain
& tf_error
)
14269 error ("%qT resolves to %qT, which is is not a class type",
14272 return error_mark_node
;
14276 return cp_build_qualified_type_real
14277 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14280 case UNBOUND_CLASS_TEMPLATE
:
14282 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14283 in_decl
, /*entering_scope=*/1);
14284 tree name
= TYPE_IDENTIFIER (t
);
14285 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14287 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14288 return error_mark_node
;
14291 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14292 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14299 ++cp_unevaluated_operand
;
14300 ++c_inhibit_evaluation_warnings
;
14302 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14304 /*integral_constant_expression_p=*/false);
14306 --cp_unevaluated_operand
;
14307 --c_inhibit_evaluation_warnings
;
14309 type
= finish_typeof (type
);
14310 return cp_build_qualified_type_real (type
,
14312 | cp_type_quals (type
),
14316 case DECLTYPE_TYPE
:
14320 ++cp_unevaluated_operand
;
14321 ++c_inhibit_evaluation_warnings
;
14323 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14324 complain
|tf_decltype
, in_decl
,
14325 /*function_p*/false,
14326 /*integral_constant_expression*/false);
14328 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14330 if (type
== NULL_TREE
)
14332 if (complain
& tf_error
)
14333 error ("empty initializer in lambda init-capture");
14334 type
= error_mark_node
;
14336 else if (TREE_CODE (type
) == TREE_LIST
)
14337 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14340 --cp_unevaluated_operand
;
14341 --c_inhibit_evaluation_warnings
;
14343 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14344 type
= lambda_capture_field_type (type
,
14345 DECLTYPE_FOR_INIT_CAPTURE (t
),
14346 DECLTYPE_FOR_REF_CAPTURE (t
));
14347 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14348 type
= lambda_proxy_type (type
);
14351 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14352 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14354 /* In a template ~id could be either a complement expression
14355 or an unqualified-id naming a destructor; if instantiating
14356 it produces an expression, it's not an id-expression or
14359 type
= finish_decltype_type (type
, id
, complain
);
14361 return cp_build_qualified_type_real (type
,
14363 | cp_type_quals (type
),
14364 complain
| tf_ignore_bad_quals
);
14367 case UNDERLYING_TYPE
:
14369 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14370 complain
, in_decl
);
14371 return finish_underlying_type (type
);
14374 case TYPE_ARGUMENT_PACK
:
14375 case NONTYPE_ARGUMENT_PACK
:
14379 if (code
== NONTYPE_ARGUMENT_PACK
)
14380 r
= make_node (code
);
14382 r
= cxx_make_type (code
);
14384 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14385 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14386 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14404 /* We should use one of the expression tsubsts for these codes. */
14405 gcc_unreachable ();
14408 sorry ("use of %qs in template", get_tree_code_name (code
));
14409 return error_mark_node
;
14413 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14414 expression on the left-hand side of the "." or "->" operator. We
14415 only do the lookup if we had a dependent BASELINK. Otherwise we
14416 adjust it onto the instantiated heirarchy. */
14419 tsubst_baselink (tree baselink
, tree object_type
,
14420 tree args
, tsubst_flags_t complain
, tree in_decl
)
14422 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14423 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14424 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14426 tree optype
= BASELINK_OPTYPE (baselink
);
14427 optype
= tsubst (optype
, args
, complain
, in_decl
);
14429 tree template_args
= NULL_TREE
;
14430 bool template_id_p
= false;
14431 tree fns
= BASELINK_FUNCTIONS (baselink
);
14432 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14434 template_id_p
= true;
14435 template_args
= TREE_OPERAND (fns
, 1);
14436 fns
= TREE_OPERAND (fns
, 0);
14438 template_args
= tsubst_template_args (template_args
, args
,
14439 complain
, in_decl
);
14442 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14443 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14444 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14448 tree name
= OVL_NAME (fns
);
14449 if (IDENTIFIER_CONV_OP_P (name
))
14450 name
= make_conv_op_name (optype
);
14452 if (name
== complete_dtor_identifier
)
14453 /* Treat as-if non-dependent below. */
14454 dependent_p
= false;
14456 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14459 if ((complain
& tf_error
)
14460 && constructor_name_p (name
, qualifying_scope
))
14461 error ("cannot call constructor %<%T::%D%> directly",
14462 qualifying_scope
, name
);
14463 return error_mark_node
;
14466 if (BASELINK_P (baselink
))
14467 fns
= BASELINK_FUNCTIONS (baselink
);
14471 gcc_assert (optype
== BASELINK_OPTYPE (baselink
));
14472 /* We're going to overwrite pieces below, make a duplicate. */
14473 baselink
= copy_node (baselink
);
14476 /* If lookup found a single function, mark it as used at this point.
14477 (If lookup found multiple functions the one selected later by
14478 overload resolution will be marked as used at that point.) */
14479 if (!template_id_p
&& !really_overloaded_fn (fns
)
14480 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14481 return error_mark_node
;
14483 if (BASELINK_P (baselink
))
14485 /* Add back the template arguments, if present. */
14487 BASELINK_FUNCTIONS (baselink
)
14488 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14490 /* Update the conversion operator type. */
14491 BASELINK_OPTYPE (baselink
) = optype
;
14495 object_type
= current_class_type
;
14497 if (qualified_p
|| !dependent_p
)
14499 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14503 /* We need to call adjust_result_of_qualified_name_lookup in case the
14504 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14505 so that we still get virtual function binding. */
14506 BASELINK_QUALIFIED_P (baselink
) = false;
14512 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14513 true if the qualified-id will be a postfix-expression in-and-of
14514 itself; false if more of the postfix-expression follows the
14515 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14519 tsubst_qualified_id (tree qualified_id
, tree args
,
14520 tsubst_flags_t complain
, tree in_decl
,
14521 bool done
, bool address_p
)
14527 tree template_args
;
14528 location_t loc
= UNKNOWN_LOCATION
;
14530 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14532 /* Figure out what name to look up. */
14533 name
= TREE_OPERAND (qualified_id
, 1);
14534 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14536 is_template
= true;
14537 loc
= EXPR_LOCATION (name
);
14538 template_args
= TREE_OPERAND (name
, 1);
14540 template_args
= tsubst_template_args (template_args
, args
,
14541 complain
, in_decl
);
14542 if (template_args
== error_mark_node
)
14543 return error_mark_node
;
14544 name
= TREE_OPERAND (name
, 0);
14548 is_template
= false;
14549 template_args
= NULL_TREE
;
14552 /* Substitute into the qualifying scope. When there are no ARGS, we
14553 are just trying to simplify a non-dependent expression. In that
14554 case the qualifying scope may be dependent, and, in any case,
14555 substituting will not help. */
14556 scope
= TREE_OPERAND (qualified_id
, 0);
14559 scope
= tsubst (scope
, args
, complain
, in_decl
);
14560 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14565 if (dependent_scope_p (scope
))
14568 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14569 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14570 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14571 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14575 if (!BASELINK_P (name
) && !DECL_P (expr
))
14577 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14579 /* A BIT_NOT_EXPR is used to represent a destructor. */
14580 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14582 error ("qualifying type %qT does not match destructor name ~%qT",
14583 scope
, TREE_OPERAND (expr
, 0));
14584 expr
= error_mark_node
;
14587 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14588 /*is_type_p=*/0, false);
14591 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14592 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14593 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14595 if (complain
& tf_error
)
14597 error ("dependent-name %qE is parsed as a non-type, but "
14598 "instantiation yields a type", qualified_id
);
14599 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14601 return error_mark_node
;
14607 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14609 /* Remember that there was a reference to this entity. */
14610 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14611 return error_mark_node
;
14614 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14616 if (complain
& tf_error
)
14617 qualified_name_lookup_error (scope
,
14618 TREE_OPERAND (qualified_id
, 1),
14619 expr
, input_location
);
14620 return error_mark_node
;
14625 if (variable_template_p (expr
))
14626 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14629 expr
= lookup_template_function (expr
, template_args
);
14632 if (expr
== error_mark_node
&& complain
& tf_error
)
14633 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14634 expr
, input_location
);
14635 else if (TYPE_P (scope
))
14637 expr
= (adjust_result_of_qualified_name_lookup
14638 (expr
, scope
, current_nonlambda_class_type ()));
14639 expr
= (finish_qualified_id_expr
14640 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14641 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14642 /*template_arg_p=*/false, complain
));
14645 /* Expressions do not generally have reference type. */
14646 if (TREE_CODE (expr
) != SCOPE_REF
14647 /* However, if we're about to form a pointer-to-member, we just
14648 want the referenced member referenced. */
14649 && TREE_CODE (expr
) != OFFSET_REF
)
14650 expr
= convert_from_reference (expr
);
14652 if (REF_PARENTHESIZED_P (qualified_id
))
14653 expr
= force_paren_expr (expr
);
14658 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14659 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14663 tsubst_init (tree init
, tree decl
, tree args
,
14664 tsubst_flags_t complain
, tree in_decl
)
14669 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14671 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14673 /* If we had an initializer but it
14674 instantiated to nothing,
14675 value-initialize the object. This will
14676 only occur when the initializer was a
14677 pack expansion where the parameter packs
14678 used in that expansion were of length
14680 init
= build_value_init (TREE_TYPE (decl
),
14682 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14683 init
= get_target_expr_sfinae (init
, complain
);
14684 if (TREE_CODE (init
) == TARGET_EXPR
)
14685 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14691 /* Like tsubst, but deals with expressions. This function just replaces
14692 template parms; to finish processing the resultant expression, use
14693 tsubst_copy_and_build or tsubst_expr. */
14696 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14698 enum tree_code code
;
14701 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14704 code
= TREE_CODE (t
);
14709 r
= retrieve_local_specialization (t
);
14711 if (r
== NULL_TREE
)
14713 /* We get here for a use of 'this' in an NSDMI. */
14714 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14715 return current_class_ptr
;
14717 /* This can happen for a parameter name used later in a function
14718 declaration (such as in a late-specified return type). Just
14719 make a dummy decl, since it's only used for its type. */
14720 gcc_assert (cp_unevaluated_operand
!= 0);
14721 r
= tsubst_decl (t
, args
, complain
);
14722 /* Give it the template pattern as its context; its true context
14723 hasn't been instantiated yet and this is good enough for
14725 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14728 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14729 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14730 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14731 return error_mark_node
;
14739 if (DECL_TEMPLATE_PARM_P (t
))
14740 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14741 /* There is no need to substitute into namespace-scope
14743 if (DECL_NAMESPACE_SCOPE_P (t
))
14745 /* If ARGS is NULL, then T is known to be non-dependent. */
14746 if (args
== NULL_TREE
)
14747 return scalar_constant_value (t
);
14749 /* Unfortunately, we cannot just call lookup_name here.
14752 template <int I> int f() {
14754 struct S { void g() { E e = a; } };
14757 When we instantiate f<7>::S::g(), say, lookup_name is not
14758 clever enough to find f<7>::a. */
14760 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14761 /*entering_scope=*/0);
14763 for (v
= TYPE_VALUES (enum_type
);
14765 v
= TREE_CHAIN (v
))
14766 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14767 return TREE_VALUE (v
);
14769 /* We didn't find the name. That should never happen; if
14770 name-lookup found it during preliminary parsing, we
14771 should find it again here during instantiation. */
14772 gcc_unreachable ();
14777 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14779 /* Check for a local specialization set up by
14780 tsubst_pack_expansion. */
14781 if (tree r
= retrieve_local_specialization (t
))
14783 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14784 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14788 /* When retrieving a capture pack from a generic lambda, remove the
14789 lambda call op's own template argument list from ARGS. Only the
14790 template arguments active for the closure type should be used to
14791 retrieve the pack specialization. */
14792 if (LAMBDA_FUNCTION_P (current_function_decl
)
14793 && (template_class_depth (DECL_CONTEXT (t
))
14794 != TMPL_ARGS_DEPTH (args
)))
14795 args
= strip_innermost_template_args (args
, 1);
14797 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14798 tsubst_decl put in the hash table. */
14799 return retrieve_specialization (t
, args
, 0);
14802 if (DECL_CONTEXT (t
))
14806 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14807 /*entering_scope=*/1);
14808 if (ctx
!= DECL_CONTEXT (t
))
14810 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14813 if (complain
& tf_error
)
14814 error ("using invalid field %qD", t
);
14815 return error_mark_node
;
14824 case FUNCTION_DECL
:
14825 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14826 r
= tsubst (t
, args
, complain
, in_decl
);
14827 else if (local_variable_p (t
)
14828 && uses_template_parms (DECL_CONTEXT (t
)))
14830 r
= retrieve_local_specialization (t
);
14831 if (r
== NULL_TREE
)
14833 /* First try name lookup to find the instantiation. */
14834 r
= lookup_name (DECL_NAME (t
));
14835 if (r
&& !is_capture_proxy (r
))
14837 /* Make sure that the one we found is the one we want. */
14838 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14839 if (ctx
!= DECL_CONTEXT (r
))
14847 /* This can happen for a variable used in a
14848 late-specified return type of a local lambda, or for a
14849 local static or constant. Building a new VAR_DECL
14850 should be OK in all those cases. */
14851 r
= tsubst_decl (t
, args
, complain
);
14852 if (local_specializations
)
14853 /* Avoid infinite recursion (79640). */
14854 register_local_specialization (r
, t
);
14855 if (decl_maybe_constant_var_p (r
))
14857 /* We can't call cp_finish_decl, so handle the
14858 initializer by hand. */
14859 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14860 complain
, in_decl
);
14861 if (!processing_template_decl
)
14862 init
= maybe_constant_init (init
);
14863 if (processing_template_decl
14864 ? potential_constant_expression (init
)
14865 : reduced_constant_expression_p (init
))
14866 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14867 = TREE_CONSTANT (r
) = true;
14868 DECL_INITIAL (r
) = init
;
14869 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14871 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14872 complain
, adc_variable_type
);
14874 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14875 || decl_constant_var_p (r
)
14876 || errorcount
|| sorrycount
);
14877 if (!processing_template_decl
14878 && !TREE_STATIC (r
))
14879 r
= process_outer_var_ref (r
, complain
);
14881 /* Remember this for subsequent uses. */
14882 if (local_specializations
)
14883 register_local_specialization (r
, t
);
14888 if (!mark_used (r
, complain
))
14889 return error_mark_node
;
14892 case NAMESPACE_DECL
:
14896 /* An OVERLOAD will always be a non-dependent overload set; an
14897 overload set from function scope will just be represented with an
14898 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14899 gcc_assert (!uses_template_parms (t
));
14900 /* We must have marked any lookups as persistent. */
14901 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14905 return tsubst_baselink (t
, current_nonlambda_class_type (),
14906 args
, complain
, in_decl
);
14908 case TEMPLATE_DECL
:
14909 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14910 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14911 args
, complain
, in_decl
);
14912 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14913 return tsubst (t
, args
, complain
, in_decl
);
14914 else if (DECL_CLASS_SCOPE_P (t
)
14915 && uses_template_parms (DECL_CONTEXT (t
)))
14917 /* Template template argument like the following example need
14920 template <template <class> class TT> struct C {};
14921 template <class T> struct D {
14922 template <class U> struct E {};
14927 We are processing the template argument `E' in #1 for
14928 the template instantiation #2. Originally, `E' is a
14929 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14930 have to substitute this with one having context `D<int>'. */
14932 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14933 if (dependent_scope_p (context
))
14935 /* When rewriting a constructor into a deduction guide, a
14936 non-dependent name can become dependent, so memtmpl<args>
14937 becomes context::template memtmpl<args>. */
14938 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14939 return build_qualified_name (type
, context
, DECL_NAME (t
),
14942 return lookup_field (context
, DECL_NAME(t
), 0, false);
14945 /* Ordinary template template argument. */
14949 case REINTERPRET_CAST_EXPR
:
14950 case CONST_CAST_EXPR
:
14951 case STATIC_CAST_EXPR
:
14952 case DYNAMIC_CAST_EXPR
:
14953 case IMPLICIT_CONV_EXPR
:
14957 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14958 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14959 return build1 (code
, type
, op0
);
14963 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14964 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14966 tree expanded
, op
= TREE_OPERAND (t
, 0);
14969 if (SIZEOF_EXPR_TYPE_P (t
))
14970 op
= TREE_TYPE (op
);
14972 ++cp_unevaluated_operand
;
14973 ++c_inhibit_evaluation_warnings
;
14974 /* We only want to compute the number of arguments. */
14975 if (PACK_EXPANSION_P (op
))
14976 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14978 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14979 args
, complain
, in_decl
);
14980 --cp_unevaluated_operand
;
14981 --c_inhibit_evaluation_warnings
;
14983 if (TREE_CODE (expanded
) == TREE_VEC
)
14985 len
= TREE_VEC_LENGTH (expanded
);
14986 /* Set TREE_USED for the benefit of -Wunused. */
14987 for (int i
= 0; i
< len
; i
++)
14988 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14989 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14992 if (expanded
== error_mark_node
)
14993 return error_mark_node
;
14994 else if (PACK_EXPANSION_P (expanded
)
14995 || (TREE_CODE (expanded
) == TREE_VEC
14996 && pack_expansion_args_count (expanded
)))
14999 if (PACK_EXPANSION_P (expanded
))
15001 else if (TREE_VEC_LENGTH (expanded
) == 1)
15002 expanded
= TREE_VEC_ELT (expanded
, 0);
15004 expanded
= make_argument_pack (expanded
);
15006 if (TYPE_P (expanded
))
15007 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15008 complain
& tf_error
);
15010 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15011 complain
& tf_error
);
15014 return build_int_cst (size_type_node
, len
);
15016 if (SIZEOF_EXPR_TYPE_P (t
))
15018 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15019 args
, complain
, in_decl
);
15020 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15021 r
= build1 (SIZEOF_EXPR
,
15022 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15023 SIZEOF_EXPR_TYPE_P (r
) = 1;
15030 case TRUTH_NOT_EXPR
:
15033 case UNARY_PLUS_EXPR
: /* Unary + */
15035 case AT_ENCODE_EXPR
:
15039 case REALPART_EXPR
:
15040 case IMAGPART_EXPR
:
15043 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15044 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15045 return build1 (code
, type
, op0
);
15048 case COMPONENT_REF
:
15053 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15054 name
= TREE_OPERAND (t
, 1);
15055 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15057 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15058 complain
, in_decl
);
15059 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15061 else if (TREE_CODE (name
) == SCOPE_REF
15062 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15064 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15065 complain
, in_decl
);
15066 name
= TREE_OPERAND (name
, 1);
15067 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15068 complain
, in_decl
);
15069 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15070 name
= build_qualified_name (/*type=*/NULL_TREE
,
15072 /*template_p=*/false);
15074 else if (BASELINK_P (name
))
15075 name
= tsubst_baselink (name
,
15076 non_reference (TREE_TYPE (object
)),
15080 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15081 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15087 case TRUNC_DIV_EXPR
:
15088 case CEIL_DIV_EXPR
:
15089 case FLOOR_DIV_EXPR
:
15090 case ROUND_DIV_EXPR
:
15091 case EXACT_DIV_EXPR
:
15095 case TRUNC_MOD_EXPR
:
15096 case FLOOR_MOD_EXPR
:
15097 case TRUTH_ANDIF_EXPR
:
15098 case TRUTH_ORIF_EXPR
:
15099 case TRUTH_AND_EXPR
:
15100 case TRUTH_OR_EXPR
:
15113 case COMPOUND_EXPR
:
15116 case PREDECREMENT_EXPR
:
15117 case PREINCREMENT_EXPR
:
15118 case POSTDECREMENT_EXPR
:
15119 case POSTINCREMENT_EXPR
:
15121 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15122 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15123 return build_nt (code
, op0
, op1
);
15128 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15129 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15130 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15131 QUALIFIED_NAME_IS_TEMPLATE (t
));
15136 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15137 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15138 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15143 int n
= VL_EXP_OPERAND_LENGTH (t
);
15144 tree result
= build_vl_exp (CALL_EXPR
, n
);
15146 for (i
= 0; i
< n
; i
++)
15147 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15148 complain
, in_decl
);
15154 case PSEUDO_DTOR_EXPR
:
15155 case VEC_PERM_EXPR
:
15157 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15158 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15159 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15160 r
= build_nt (code
, op0
, op1
, op2
);
15161 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15167 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15168 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15169 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15170 r
= build_nt (code
, op0
, op1
, op2
);
15171 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15177 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15178 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15179 r
= build_nt (code
, op0
, op1
);
15180 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15181 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15185 case TEMPLATE_ID_EXPR
:
15187 /* Substituted template arguments */
15188 tree fn
= TREE_OPERAND (t
, 0);
15189 tree targs
= TREE_OPERAND (t
, 1);
15191 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15193 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15195 return lookup_template_function (fn
, targs
);
15200 tree purpose
, value
, chain
;
15202 if (t
== void_list_node
)
15205 purpose
= TREE_PURPOSE (t
);
15207 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15208 value
= TREE_VALUE (t
);
15210 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15211 chain
= TREE_CHAIN (t
);
15212 if (chain
&& chain
!= void_type_node
)
15213 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15214 if (purpose
== TREE_PURPOSE (t
)
15215 && value
== TREE_VALUE (t
)
15216 && chain
== TREE_CHAIN (t
))
15218 return tree_cons (purpose
, value
, chain
);
15223 case ENUMERAL_TYPE
:
15225 case TEMPLATE_TYPE_PARM
:
15226 case TEMPLATE_TEMPLATE_PARM
:
15227 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15228 case TEMPLATE_PARM_INDEX
:
15230 case REFERENCE_TYPE
:
15232 case FUNCTION_TYPE
:
15235 case TYPENAME_TYPE
:
15236 case UNBOUND_CLASS_TEMPLATE
:
15238 case DECLTYPE_TYPE
:
15240 return tsubst (t
, args
, complain
, in_decl
);
15244 /* Fall through. */
15245 case IDENTIFIER_NODE
:
15246 if (IDENTIFIER_CONV_OP_P (t
))
15248 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15249 return make_conv_op_name (new_type
);
15255 /* This is handled by tsubst_copy_and_build. */
15256 gcc_unreachable ();
15260 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15261 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15262 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15265 case CLEANUP_POINT_EXPR
:
15266 /* We shouldn't have built any of these during initial template
15267 generation. Instead, they should be built during instantiation
15268 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15269 gcc_unreachable ();
15273 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15274 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15275 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15276 r
= build2 (code
, type
, op0
, op1
);
15277 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15278 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15279 && !(complain
& tf_error
))
15280 return error_mark_node
;
15284 case EXPR_PACK_EXPANSION
:
15285 error ("invalid use of pack expansion expression");
15286 return error_mark_node
;
15288 case NONTYPE_ARGUMENT_PACK
:
15289 error ("use %<...%> to expand argument pack");
15290 return error_mark_node
;
15293 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15301 /* Instantiate any typedefs in the type. */
15302 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15303 r
= fold_convert (type
, t
);
15304 gcc_assert (TREE_CODE (r
) == code
);
15309 /* These can sometimes show up in a partial instantiation, but never
15310 involve template parms. */
15311 gcc_assert (!uses_template_parms (t
));
15314 case UNARY_LEFT_FOLD_EXPR
:
15315 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15316 case UNARY_RIGHT_FOLD_EXPR
:
15317 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15318 case BINARY_LEFT_FOLD_EXPR
:
15319 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15320 case BINARY_RIGHT_FOLD_EXPR
:
15321 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15325 case DEBUG_BEGIN_STMT
:
15326 /* ??? There's no point in copying it for now, but maybe some
15327 day it will contain more information, such as a pointer back
15328 to the containing function, inlined copy or so. */
15332 /* We shouldn't get here, but keep going if !flag_checking. */
15334 gcc_unreachable ();
15339 /* Helper function for tsubst_omp_clauses, used for instantiation of
15340 OMP_CLAUSE_DECL of clauses. */
15343 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15346 if (decl
== NULL_TREE
)
15349 /* Handle an OpenMP array section represented as a TREE_LIST (or
15350 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15351 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15352 TREE_LIST. We can handle it exactly the same as an array section
15353 (purpose, value, and a chain), even though the nomenclature
15354 (low_bound, length, etc) is different. */
15355 if (TREE_CODE (decl
) == TREE_LIST
)
15358 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15359 /*integral_constant_expression_p=*/false);
15360 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15361 /*integral_constant_expression_p=*/false);
15362 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15364 if (TREE_PURPOSE (decl
) == low_bound
15365 && TREE_VALUE (decl
) == length
15366 && TREE_CHAIN (decl
) == chain
)
15368 tree ret
= tree_cons (low_bound
, length
, chain
);
15369 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15370 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15373 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15374 /*integral_constant_expression_p=*/false);
15375 /* Undo convert_from_reference tsubst_expr could have called. */
15377 && REFERENCE_REF_P (ret
)
15378 && !REFERENCE_REF_P (decl
))
15379 ret
= TREE_OPERAND (ret
, 0);
15383 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15386 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15387 tree args
, tsubst_flags_t complain
, tree in_decl
)
15389 tree new_clauses
= NULL_TREE
, nc
, oc
;
15390 tree linear_no_step
= NULL_TREE
;
15392 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15394 nc
= copy_node (oc
);
15395 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15398 switch (OMP_CLAUSE_CODE (nc
))
15400 case OMP_CLAUSE_LASTPRIVATE
:
15401 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15403 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15404 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15405 in_decl
, /*integral_constant_expression_p=*/false);
15406 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15407 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15410 case OMP_CLAUSE_PRIVATE
:
15411 case OMP_CLAUSE_SHARED
:
15412 case OMP_CLAUSE_FIRSTPRIVATE
:
15413 case OMP_CLAUSE_COPYIN
:
15414 case OMP_CLAUSE_COPYPRIVATE
:
15415 case OMP_CLAUSE_UNIFORM
:
15416 case OMP_CLAUSE_DEPEND
:
15417 case OMP_CLAUSE_FROM
:
15418 case OMP_CLAUSE_TO
:
15419 case OMP_CLAUSE_MAP
:
15420 case OMP_CLAUSE_USE_DEVICE_PTR
:
15421 case OMP_CLAUSE_IS_DEVICE_PTR
:
15422 OMP_CLAUSE_DECL (nc
)
15423 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15426 case OMP_CLAUSE_TILE
:
15427 case OMP_CLAUSE_IF
:
15428 case OMP_CLAUSE_NUM_THREADS
:
15429 case OMP_CLAUSE_SCHEDULE
:
15430 case OMP_CLAUSE_COLLAPSE
:
15431 case OMP_CLAUSE_FINAL
:
15432 case OMP_CLAUSE_DEVICE
:
15433 case OMP_CLAUSE_DIST_SCHEDULE
:
15434 case OMP_CLAUSE_NUM_TEAMS
:
15435 case OMP_CLAUSE_THREAD_LIMIT
:
15436 case OMP_CLAUSE_SAFELEN
:
15437 case OMP_CLAUSE_SIMDLEN
:
15438 case OMP_CLAUSE_NUM_TASKS
:
15439 case OMP_CLAUSE_GRAINSIZE
:
15440 case OMP_CLAUSE_PRIORITY
:
15441 case OMP_CLAUSE_ORDERED
:
15442 case OMP_CLAUSE_HINT
:
15443 case OMP_CLAUSE_NUM_GANGS
:
15444 case OMP_CLAUSE_NUM_WORKERS
:
15445 case OMP_CLAUSE_VECTOR_LENGTH
:
15446 case OMP_CLAUSE_WORKER
:
15447 case OMP_CLAUSE_VECTOR
:
15448 case OMP_CLAUSE_ASYNC
:
15449 case OMP_CLAUSE_WAIT
:
15450 OMP_CLAUSE_OPERAND (nc
, 0)
15451 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15452 in_decl
, /*integral_constant_expression_p=*/false);
15454 case OMP_CLAUSE_REDUCTION
:
15455 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15457 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15458 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15460 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15461 complain
, in_decl
);
15462 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15463 = build_qualified_name (NULL_TREE
, scope
,
15464 TREE_OPERAND (placeholder
, 1),
15468 gcc_assert (identifier_p (placeholder
));
15470 OMP_CLAUSE_DECL (nc
)
15471 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15474 case OMP_CLAUSE_GANG
:
15475 case OMP_CLAUSE_ALIGNED
:
15476 OMP_CLAUSE_DECL (nc
)
15477 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15479 OMP_CLAUSE_OPERAND (nc
, 1)
15480 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15481 in_decl
, /*integral_constant_expression_p=*/false);
15483 case OMP_CLAUSE_LINEAR
:
15484 OMP_CLAUSE_DECL (nc
)
15485 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15487 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15489 gcc_assert (!linear_no_step
);
15490 linear_no_step
= nc
;
15492 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15493 OMP_CLAUSE_LINEAR_STEP (nc
)
15494 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15495 complain
, in_decl
);
15497 OMP_CLAUSE_LINEAR_STEP (nc
)
15498 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15500 /*integral_constant_expression_p=*/false);
15502 case OMP_CLAUSE_NOWAIT
:
15503 case OMP_CLAUSE_DEFAULT
:
15504 case OMP_CLAUSE_UNTIED
:
15505 case OMP_CLAUSE_MERGEABLE
:
15506 case OMP_CLAUSE_INBRANCH
:
15507 case OMP_CLAUSE_NOTINBRANCH
:
15508 case OMP_CLAUSE_PROC_BIND
:
15509 case OMP_CLAUSE_FOR
:
15510 case OMP_CLAUSE_PARALLEL
:
15511 case OMP_CLAUSE_SECTIONS
:
15512 case OMP_CLAUSE_TASKGROUP
:
15513 case OMP_CLAUSE_NOGROUP
:
15514 case OMP_CLAUSE_THREADS
:
15515 case OMP_CLAUSE_SIMD
:
15516 case OMP_CLAUSE_DEFAULTMAP
:
15517 case OMP_CLAUSE_INDEPENDENT
:
15518 case OMP_CLAUSE_AUTO
:
15519 case OMP_CLAUSE_SEQ
:
15522 gcc_unreachable ();
15524 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15525 switch (OMP_CLAUSE_CODE (nc
))
15527 case OMP_CLAUSE_SHARED
:
15528 case OMP_CLAUSE_PRIVATE
:
15529 case OMP_CLAUSE_FIRSTPRIVATE
:
15530 case OMP_CLAUSE_LASTPRIVATE
:
15531 case OMP_CLAUSE_COPYPRIVATE
:
15532 case OMP_CLAUSE_LINEAR
:
15533 case OMP_CLAUSE_REDUCTION
:
15534 case OMP_CLAUSE_USE_DEVICE_PTR
:
15535 case OMP_CLAUSE_IS_DEVICE_PTR
:
15536 /* tsubst_expr on SCOPE_REF results in returning
15537 finish_non_static_data_member result. Undo that here. */
15538 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15539 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15540 == IDENTIFIER_NODE
))
15542 tree t
= OMP_CLAUSE_DECL (nc
);
15545 switch (TREE_CODE (v
))
15547 case COMPONENT_REF
:
15551 case POINTER_PLUS_EXPR
:
15552 v
= TREE_OPERAND (v
, 0);
15555 if (DECL_CONTEXT (v
) == current_function_decl
15556 && DECL_ARTIFICIAL (v
)
15557 && DECL_NAME (v
) == this_identifier
)
15558 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15565 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15566 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15567 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15568 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15569 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15571 tree decl
= OMP_CLAUSE_DECL (nc
);
15574 retrofit_lang_decl (decl
);
15575 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15584 new_clauses
= nreverse (new_clauses
);
15585 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15587 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15588 if (linear_no_step
)
15589 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15590 if (nc
== linear_no_step
)
15592 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15596 return new_clauses
;
15599 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15602 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15605 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15607 tree purpose
, value
, chain
;
15612 if (TREE_CODE (t
) != TREE_LIST
)
15613 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15614 /*function_p=*/false,
15615 /*integral_constant_expression_p=*/false);
15617 if (t
== void_list_node
)
15620 purpose
= TREE_PURPOSE (t
);
15622 purpose
= RECUR (purpose
);
15623 value
= TREE_VALUE (t
);
15626 if (TREE_CODE (value
) != LABEL_DECL
)
15627 value
= RECUR (value
);
15630 value
= lookup_label (DECL_NAME (value
));
15631 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15632 TREE_USED (value
) = 1;
15635 chain
= TREE_CHAIN (t
);
15636 if (chain
&& chain
!= void_type_node
)
15637 chain
= RECUR (chain
);
15638 return tree_cons (purpose
, value
, chain
);
15642 /* Used to temporarily communicate the list of #pragma omp parallel
15643 clauses to #pragma omp for instantiation if they are combined
15646 static tree
*omp_parallel_combined_clauses
;
15648 /* Substitute one OMP_FOR iterator. */
15651 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15652 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15653 tree args
, tsubst_flags_t complain
, tree in_decl
,
15654 bool integral_constant_expression_p
)
15656 #define RECUR(NODE) \
15657 tsubst_expr ((NODE), args, complain, in_decl, \
15658 integral_constant_expression_p)
15659 tree decl
, init
, cond
, incr
;
15661 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15662 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15664 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15666 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15667 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15670 decl
= TREE_OPERAND (init
, 0);
15671 init
= TREE_OPERAND (init
, 1);
15672 tree decl_expr
= NULL_TREE
;
15673 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15675 /* We need to jump through some hoops to handle declarations in the
15676 init-statement, since we might need to handle auto deduction,
15677 but we need to keep control of initialization. */
15679 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15680 decl
= tsubst_decl (decl
, args
, complain
);
15684 if (TREE_CODE (decl
) == SCOPE_REF
)
15686 decl
= RECUR (decl
);
15687 if (TREE_CODE (decl
) == COMPONENT_REF
)
15691 switch (TREE_CODE (v
))
15693 case COMPONENT_REF
:
15697 case POINTER_PLUS_EXPR
:
15698 v
= TREE_OPERAND (v
, 0);
15701 if (DECL_CONTEXT (v
) == current_function_decl
15702 && DECL_ARTIFICIAL (v
)
15703 && DECL_NAME (v
) == this_identifier
)
15705 decl
= TREE_OPERAND (decl
, 1);
15706 decl
= omp_privatize_field (decl
, false);
15716 decl
= RECUR (decl
);
15718 init
= RECUR (init
);
15720 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15721 if (auto_node
&& init
)
15723 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15725 gcc_assert (!type_dependent_expression_p (decl
));
15727 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15731 /* Declare the variable, but don't let that initialize it. */
15732 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15733 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15735 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15738 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15739 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15740 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15742 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15743 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15744 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15745 NOP_EXPR
, rhs
, complain
);
15748 incr
= RECUR (incr
);
15749 TREE_VEC_ELT (declv
, i
) = decl
;
15750 TREE_VEC_ELT (initv
, i
) = init
;
15751 TREE_VEC_ELT (condv
, i
) = cond
;
15752 TREE_VEC_ELT (incrv
, i
) = incr
;
15758 /* Declare and initialize the variable. */
15766 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15768 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15770 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15771 && OMP_CLAUSE_DECL (*pc
) == decl
)
15773 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15774 && OMP_CLAUSE_DECL (*pc
) == decl
)
15778 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15780 *pc
= OMP_CLAUSE_CHAIN (c
);
15781 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15784 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15785 && OMP_CLAUSE_DECL (*pc
) == decl
)
15787 error ("iteration variable %qD should not be firstprivate",
15789 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15791 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15792 && OMP_CLAUSE_DECL (*pc
) == decl
)
15794 error ("iteration variable %qD should not be reduction",
15796 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15799 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15804 if (*pc
== NULL_TREE
)
15806 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15807 OMP_CLAUSE_DECL (c
) = decl
;
15808 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15811 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15816 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15817 if (COMPARISON_CLASS_P (cond
))
15819 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15820 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15821 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15824 cond
= RECUR (cond
);
15825 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15826 switch (TREE_CODE (incr
))
15828 case PREINCREMENT_EXPR
:
15829 case PREDECREMENT_EXPR
:
15830 case POSTINCREMENT_EXPR
:
15831 case POSTDECREMENT_EXPR
:
15832 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15833 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15836 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15837 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15839 tree rhs
= TREE_OPERAND (incr
, 1);
15840 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15841 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15842 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15843 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15844 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15848 incr
= RECUR (incr
);
15851 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15852 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15854 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15855 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15856 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15857 TREE_TYPE (decl
), lhs
,
15858 RECUR (TREE_OPERAND (incr
, 2))));
15860 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15861 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15862 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15864 tree rhs
= TREE_OPERAND (incr
, 2);
15865 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15866 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15867 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15868 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15869 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15873 incr
= RECUR (incr
);
15876 incr
= RECUR (incr
);
15880 TREE_VEC_ELT (declv
, i
) = decl
;
15881 TREE_VEC_ELT (initv
, i
) = init
;
15882 TREE_VEC_ELT (condv
, i
) = cond
;
15883 TREE_VEC_ELT (incrv
, i
) = incr
;
15887 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15888 of OMP_TARGET's body. */
15891 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15893 *walk_subtrees
= 0;
15894 switch (TREE_CODE (*tp
))
15899 case STATEMENT_LIST
:
15900 *walk_subtrees
= 1;
15908 /* Helper function for tsubst_expr. For decomposition declaration
15909 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15910 also the corresponding decls representing the identifiers
15911 of the decomposition declaration. Return DECL if successful
15912 or error_mark_node otherwise, set *FIRST to the first decl
15913 in the list chained through DECL_CHAIN and *CNT to the number
15917 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15918 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15921 tree decl2
, decl3
, prev
= decl
;
15923 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15924 for (decl2
= DECL_CHAIN (pattern_decl
);
15927 && DECL_DECOMPOSITION_P (decl2
)
15928 && DECL_NAME (decl2
);
15929 decl2
= DECL_CHAIN (decl2
))
15931 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15933 gcc_assert (errorcount
);
15934 return error_mark_node
;
15937 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
15938 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15939 tree v
= DECL_VALUE_EXPR (decl2
);
15940 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15941 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15942 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15943 SET_DECL_VALUE_EXPR (decl2
, v
);
15944 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15946 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15947 maybe_push_decl (decl3
);
15948 if (error_operand_p (decl3
))
15949 decl
= error_mark_node
;
15950 else if (decl
!= error_mark_node
15951 && DECL_CHAIN (decl3
) != prev
)
15953 gcc_assert (errorcount
);
15954 decl
= error_mark_node
;
15963 /* Like tsubst_copy for expressions, etc. but also does semantic
15967 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15968 bool integral_constant_expression_p
)
15970 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15971 #define RECUR(NODE) \
15972 tsubst_expr ((NODE), args, complain, in_decl, \
15973 integral_constant_expression_p)
15979 if (t
== NULL_TREE
|| t
== error_mark_node
)
15982 loc
= input_location
;
15983 if (EXPR_HAS_LOCATION (t
))
15984 input_location
= EXPR_LOCATION (t
);
15985 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15986 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15988 switch (TREE_CODE (t
))
15990 case STATEMENT_LIST
:
15992 tree_stmt_iterator i
;
15993 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15994 RECUR (tsi_stmt (i
));
15998 case CTOR_INITIALIZER
:
15999 finish_mem_initializers (tsubst_initializer_list
16000 (TREE_OPERAND (t
, 0), args
));
16004 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16008 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16009 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16010 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16012 finish_expr_stmt (tmp
);
16016 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16017 /*attribs=*/NULL_TREE
);
16022 tree decl
, pattern_decl
;
16025 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16026 if (TREE_CODE (decl
) == LABEL_DECL
)
16027 finish_label_decl (DECL_NAME (decl
));
16028 else if (TREE_CODE (decl
) == USING_DECL
)
16030 tree scope
= USING_DECL_SCOPE (decl
);
16031 tree name
= DECL_NAME (decl
);
16033 scope
= tsubst (scope
, args
, complain
, in_decl
);
16034 decl
= lookup_qualified_name (scope
, name
,
16035 /*is_type_p=*/false,
16036 /*complain=*/false);
16037 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16038 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16040 finish_local_using_decl (decl
, scope
, name
);
16042 else if (DECL_PACK_P (decl
))
16044 /* Don't build up decls for a variadic capture proxy, we'll
16045 instantiate the elements directly as needed. */
16048 else if (is_capture_proxy (decl
)
16049 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16051 /* We're in tsubst_lambda_expr, we've already inserted a new
16052 capture proxy, so look it up and register it. */
16053 tree inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16054 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16055 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16056 register_local_specialization (inst
, decl
);
16059 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16060 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16061 /* Don't copy the old closure; we'll create a new one in
16062 tsubst_lambda_expr. */
16066 init
= DECL_INITIAL (decl
);
16067 decl
= tsubst (decl
, args
, complain
, in_decl
);
16068 if (decl
!= error_mark_node
)
16070 /* By marking the declaration as instantiated, we avoid
16071 trying to instantiate it. Since instantiate_decl can't
16072 handle local variables, and since we've already done
16073 all that needs to be done, that's the right thing to
16076 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16078 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16079 /* Anonymous aggregates are a special case. */
16080 finish_anon_union (decl
);
16081 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16083 DECL_CONTEXT (decl
) = current_function_decl
;
16084 if (DECL_NAME (decl
) == this_identifier
)
16086 tree lam
= DECL_CONTEXT (current_function_decl
);
16087 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16088 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16090 insert_capture_proxy (decl
);
16092 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16093 /* We already did a pushtag. */;
16094 else if (TREE_CODE (decl
) == FUNCTION_DECL
16095 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16096 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16098 DECL_CONTEXT (decl
) = NULL_TREE
;
16100 DECL_CONTEXT (decl
) = current_function_decl
;
16101 cp_check_omp_declare_reduction (decl
);
16105 int const_init
= false;
16106 maybe_push_decl (decl
);
16108 && DECL_PRETTY_FUNCTION_P (decl
))
16110 /* For __PRETTY_FUNCTION__ we have to adjust the
16112 const char *const name
16113 = cxx_printable_name (current_function_decl
, 2);
16114 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16117 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16120 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16122 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16124 && DECL_DECOMPOSITION_P (decl
)
16125 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16129 decl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
16130 complain
, in_decl
, &first
,
16132 if (decl
!= error_mark_node
)
16133 cp_finish_decomp (decl
, first
, cnt
);
16143 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16144 RECUR (FOR_INIT_STMT (t
));
16145 finish_init_stmt (stmt
);
16146 tmp
= RECUR (FOR_COND (t
));
16147 finish_for_cond (tmp
, stmt
, false);
16148 tmp
= RECUR (FOR_EXPR (t
));
16149 finish_for_expr (tmp
, stmt
);
16151 bool prev
= note_iteration_stmt_body_start ();
16152 RECUR (FOR_BODY (t
));
16153 note_iteration_stmt_body_end (prev
);
16155 finish_for_stmt (stmt
);
16158 case RANGE_FOR_STMT
:
16161 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16162 decl
= RANGE_FOR_DECL (t
);
16163 decl
= tsubst (decl
, args
, complain
, in_decl
);
16164 maybe_push_decl (decl
);
16165 expr
= RECUR (RANGE_FOR_EXPR (t
));
16166 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16170 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16171 complain
, in_decl
, &first
, &cnt
);
16172 stmt
= cp_convert_range_for (stmt
, decl
, expr
, first
, cnt
,
16173 RANGE_FOR_IVDEP (t
));
16176 stmt
= cp_convert_range_for (stmt
, decl
, expr
, NULL_TREE
, 0,
16177 RANGE_FOR_IVDEP (t
));
16178 bool prev
= note_iteration_stmt_body_start ();
16179 RECUR (RANGE_FOR_BODY (t
));
16180 note_iteration_stmt_body_end (prev
);
16181 finish_for_stmt (stmt
);
16186 stmt
= begin_while_stmt ();
16187 tmp
= RECUR (WHILE_COND (t
));
16188 finish_while_stmt_cond (tmp
, stmt
, false);
16190 bool prev
= note_iteration_stmt_body_start ();
16191 RECUR (WHILE_BODY (t
));
16192 note_iteration_stmt_body_end (prev
);
16194 finish_while_stmt (stmt
);
16198 stmt
= begin_do_stmt ();
16200 bool prev
= note_iteration_stmt_body_start ();
16201 RECUR (DO_BODY (t
));
16202 note_iteration_stmt_body_end (prev
);
16204 finish_do_body (stmt
);
16205 tmp
= RECUR (DO_COND (t
));
16206 finish_do_stmt (tmp
, stmt
, false);
16210 stmt
= begin_if_stmt ();
16211 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16212 tmp
= RECUR (IF_COND (t
));
16213 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16214 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16215 /* Don't instantiate the THEN_CLAUSE. */;
16218 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16220 ++c_inhibit_evaluation_warnings
;
16221 RECUR (THEN_CLAUSE (t
));
16223 --c_inhibit_evaluation_warnings
;
16225 finish_then_clause (stmt
);
16227 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16228 /* Don't instantiate the ELSE_CLAUSE. */;
16229 else if (ELSE_CLAUSE (t
))
16231 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16232 begin_else_clause (stmt
);
16234 ++c_inhibit_evaluation_warnings
;
16235 RECUR (ELSE_CLAUSE (t
));
16237 --c_inhibit_evaluation_warnings
;
16238 finish_else_clause (stmt
);
16241 finish_if_stmt (stmt
);
16245 if (BIND_EXPR_BODY_BLOCK (t
))
16246 stmt
= begin_function_body ();
16248 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16249 ? BCS_TRY_BLOCK
: 0);
16251 RECUR (BIND_EXPR_BODY (t
));
16253 if (BIND_EXPR_BODY_BLOCK (t
))
16254 finish_function_body (stmt
);
16256 finish_compound_stmt (stmt
);
16260 finish_break_stmt ();
16263 case CONTINUE_STMT
:
16264 finish_continue_stmt ();
16268 stmt
= begin_switch_stmt ();
16269 tmp
= RECUR (SWITCH_STMT_COND (t
));
16270 finish_switch_cond (tmp
, stmt
);
16271 RECUR (SWITCH_STMT_BODY (t
));
16272 finish_switch_stmt (stmt
);
16275 case CASE_LABEL_EXPR
:
16277 tree low
= RECUR (CASE_LOW (t
));
16278 tree high
= RECUR (CASE_HIGH (t
));
16279 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16280 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16281 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16282 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16288 tree decl
= LABEL_EXPR_LABEL (t
);
16291 label
= finish_label_stmt (DECL_NAME (decl
));
16292 if (TREE_CODE (label
) == LABEL_DECL
)
16293 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16294 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16295 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16300 tmp
= GOTO_DESTINATION (t
);
16301 if (TREE_CODE (tmp
) != LABEL_DECL
)
16302 /* Computed goto's must be tsubst'd into. On the other hand,
16303 non-computed gotos must not be; the identifier in question
16304 will have no binding. */
16307 tmp
= DECL_NAME (tmp
);
16308 finish_goto_stmt (tmp
);
16313 tree string
= RECUR (ASM_STRING (t
));
16314 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16315 complain
, in_decl
);
16316 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16317 complain
, in_decl
);
16318 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16319 complain
, in_decl
);
16320 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16321 complain
, in_decl
);
16322 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16324 tree asm_expr
= tmp
;
16325 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16326 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16327 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16334 stmt
= begin_try_block ();
16335 RECUR (TRY_STMTS (t
));
16336 finish_cleanup_try_block (stmt
);
16337 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16341 tree compound_stmt
= NULL_TREE
;
16343 if (FN_TRY_BLOCK_P (t
))
16344 stmt
= begin_function_try_block (&compound_stmt
);
16346 stmt
= begin_try_block ();
16348 RECUR (TRY_STMTS (t
));
16350 if (FN_TRY_BLOCK_P (t
))
16351 finish_function_try_block (stmt
);
16353 finish_try_block (stmt
);
16355 RECUR (TRY_HANDLERS (t
));
16356 if (FN_TRY_BLOCK_P (t
))
16357 finish_function_handler_sequence (stmt
, compound_stmt
);
16359 finish_handler_sequence (stmt
);
16365 tree decl
= HANDLER_PARMS (t
);
16369 decl
= tsubst (decl
, args
, complain
, in_decl
);
16370 /* Prevent instantiate_decl from trying to instantiate
16371 this variable. We've already done all that needs to be
16373 if (decl
!= error_mark_node
)
16374 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16376 stmt
= begin_handler ();
16377 finish_handler_parms (decl
, stmt
);
16378 RECUR (HANDLER_BODY (t
));
16379 finish_handler (stmt
);
16384 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16385 if (CLASS_TYPE_P (tmp
))
16387 /* Local classes are not independent templates; they are
16388 instantiated along with their containing function. And this
16389 way we don't have to deal with pushing out of one local class
16390 to instantiate a member of another local class. */
16391 /* Closures are handled by the LAMBDA_EXPR. */
16392 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16393 complete_type (tmp
);
16394 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16396 || (TREE_CODE (fld
) == FUNCTION_DECL
16397 && !DECL_ARTIFICIAL (fld
)))
16398 && DECL_TEMPLATE_INSTANTIATION (fld
))
16399 instantiate_decl (fld
, /*defer_ok=*/false,
16400 /*expl_inst_class=*/false);
16404 case STATIC_ASSERT
:
16408 ++c_inhibit_evaluation_warnings
;
16410 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16413 /*integral_constant_expression_p=*/true);
16414 --c_inhibit_evaluation_warnings
;
16416 finish_static_assert (condition
,
16417 STATIC_ASSERT_MESSAGE (t
),
16418 STATIC_ASSERT_SOURCE_LOCATION (t
),
16419 /*member_p=*/false);
16424 case OACC_PARALLEL
:
16425 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16427 stmt
= begin_omp_parallel ();
16428 RECUR (OMP_BODY (t
));
16429 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16433 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16434 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16435 complain
, in_decl
);
16436 if (OMP_PARALLEL_COMBINED (t
))
16437 omp_parallel_combined_clauses
= &tmp
;
16438 stmt
= begin_omp_parallel ();
16439 RECUR (OMP_PARALLEL_BODY (t
));
16440 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16441 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16442 = OMP_PARALLEL_COMBINED (t
);
16443 pop_omp_privatization_clauses (r
);
16447 r
= push_omp_privatization_clauses (false);
16448 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16449 complain
, in_decl
);
16450 stmt
= begin_omp_task ();
16451 RECUR (OMP_TASK_BODY (t
));
16452 finish_omp_task (tmp
, stmt
);
16453 pop_omp_privatization_clauses (r
);
16458 case OMP_DISTRIBUTE
:
16462 tree clauses
, body
, pre_body
;
16463 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16464 tree orig_declv
= NULL_TREE
;
16465 tree incrv
= NULL_TREE
;
16466 enum c_omp_region_type ort
= C_ORT_OMP
;
16469 if (TREE_CODE (t
) == OACC_LOOP
)
16472 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16473 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16475 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16477 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16478 if (OMP_FOR_ORIG_DECLS (t
))
16479 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16480 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16481 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16482 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16485 stmt
= begin_omp_structured_block ();
16487 pre_body
= push_stmt_list ();
16488 RECUR (OMP_FOR_PRE_BODY (t
));
16489 pre_body
= pop_stmt_list (pre_body
);
16491 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16492 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16493 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16494 incrv
, &clauses
, args
, complain
, in_decl
,
16495 integral_constant_expression_p
);
16496 omp_parallel_combined_clauses
= NULL
;
16498 body
= push_stmt_list ();
16499 RECUR (OMP_FOR_BODY (t
));
16500 body
= pop_stmt_list (body
);
16502 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16503 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16504 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16508 t
= make_node (TREE_CODE (t
));
16509 TREE_TYPE (t
) = void_type_node
;
16510 OMP_FOR_BODY (t
) = body
;
16511 OMP_FOR_PRE_BODY (t
) = pre_body
;
16512 OMP_FOR_CLAUSES (t
) = clauses
;
16513 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16517 add_stmt (finish_omp_structured_block (stmt
));
16518 pop_omp_privatization_clauses (r
);
16523 omp_parallel_combined_clauses
= NULL
;
16528 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16529 && OMP_TEAMS_COMBINED (t
));
16530 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16532 stmt
= push_stmt_list ();
16533 RECUR (OMP_BODY (t
));
16534 stmt
= pop_stmt_list (stmt
);
16537 OMP_BODY (t
) = stmt
;
16538 OMP_CLAUSES (t
) = tmp
;
16540 pop_omp_privatization_clauses (r
);
16544 case OMP_TARGET_DATA
:
16546 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16547 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16549 keep_next_level (true);
16550 stmt
= begin_omp_structured_block ();
16552 RECUR (OMP_BODY (t
));
16553 stmt
= finish_omp_structured_block (stmt
);
16556 OMP_BODY (t
) = stmt
;
16557 OMP_CLAUSES (t
) = tmp
;
16558 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16560 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16563 /* For combined target teams, ensure the num_teams and
16564 thread_limit clause expressions are evaluated on the host,
16565 before entering the target construct. */
16567 for (c
= OMP_TEAMS_CLAUSES (teams
);
16568 c
; c
= OMP_CLAUSE_CHAIN (c
))
16569 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16570 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16571 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16573 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16574 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16575 if (expr
== error_mark_node
)
16577 tmp
= TARGET_EXPR_SLOT (expr
);
16579 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16580 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16581 OMP_CLAUSE_FIRSTPRIVATE
);
16582 OMP_CLAUSE_DECL (tc
) = tmp
;
16583 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16584 OMP_TARGET_CLAUSES (t
) = tc
;
16593 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16594 complain
, in_decl
);
16595 OACC_DECLARE_CLAUSES (t
) = tmp
;
16599 case OMP_TARGET_UPDATE
:
16600 case OMP_TARGET_ENTER_DATA
:
16601 case OMP_TARGET_EXIT_DATA
:
16602 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16603 complain
, in_decl
);
16605 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16609 case OACC_ENTER_DATA
:
16610 case OACC_EXIT_DATA
:
16612 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16613 complain
, in_decl
);
16615 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16620 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16621 complain
, in_decl
);
16622 stmt
= push_stmt_list ();
16623 RECUR (OMP_BODY (t
));
16624 stmt
= pop_stmt_list (stmt
);
16627 OMP_BODY (t
) = stmt
;
16628 OMP_ORDERED_CLAUSES (t
) = tmp
;
16634 case OMP_TASKGROUP
:
16635 stmt
= push_stmt_list ();
16636 RECUR (OMP_BODY (t
));
16637 stmt
= pop_stmt_list (stmt
);
16640 OMP_BODY (t
) = stmt
;
16645 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16646 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16648 tree op1
= TREE_OPERAND (t
, 1);
16649 tree rhs1
= NULL_TREE
;
16651 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16653 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16654 op1
= TREE_OPERAND (op1
, 1);
16656 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16657 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16658 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16659 NULL_TREE
, NULL_TREE
, rhs1
,
16660 OMP_ATOMIC_SEQ_CST (t
));
16664 tree op1
= TREE_OPERAND (t
, 1);
16665 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16666 tree rhs1
= NULL_TREE
;
16667 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16668 enum tree_code opcode
= NOP_EXPR
;
16669 if (code
== OMP_ATOMIC_READ
)
16671 v
= RECUR (TREE_OPERAND (op1
, 0));
16672 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16674 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16675 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16677 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16678 v
= RECUR (TREE_OPERAND (op1
, 0));
16679 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16680 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16682 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16683 op11
= TREE_OPERAND (op11
, 1);
16685 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16686 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16687 opcode
= TREE_CODE (op11
);
16688 if (opcode
== MODIFY_EXPR
)
16694 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16695 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16697 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16698 OMP_ATOMIC_SEQ_CST (t
));
16702 case TRANSACTION_EXPR
:
16705 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16706 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16708 if (TRANSACTION_EXPR_IS_STMT (t
))
16710 tree body
= TRANSACTION_EXPR_BODY (t
);
16711 tree noex
= NULL_TREE
;
16712 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16714 noex
= MUST_NOT_THROW_COND (body
);
16715 if (noex
== NULL_TREE
)
16716 noex
= boolean_true_node
;
16717 body
= TREE_OPERAND (body
, 0);
16719 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16721 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16725 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16726 RECUR (TRANSACTION_EXPR_BODY (t
)),
16733 case MUST_NOT_THROW_EXPR
:
16735 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16736 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16737 RETURN (build_must_not_throw_expr (op0
, cond
));
16740 case EXPR_PACK_EXPANSION
:
16741 error ("invalid use of pack expansion expression");
16742 RETURN (error_mark_node
);
16744 case NONTYPE_ARGUMENT_PACK
:
16745 error ("use %<...%> to expand argument pack");
16746 RETURN (error_mark_node
);
16748 case COMPOUND_EXPR
:
16749 tmp
= RECUR (TREE_OPERAND (t
, 0));
16750 if (tmp
== NULL_TREE
)
16751 /* If the first operand was a statement, we're done with it. */
16752 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16753 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16754 RECUR (TREE_OPERAND (t
, 1)),
16757 case ANNOTATE_EXPR
:
16758 tmp
= RECUR (TREE_OPERAND (t
, 0));
16759 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16760 TREE_TYPE (tmp
), tmp
,
16761 RECUR (TREE_OPERAND (t
, 1)),
16762 RECUR (TREE_OPERAND (t
, 2))));
16765 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16767 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16768 /*function_p=*/false,
16769 integral_constant_expression_p
));
16772 RETURN (NULL_TREE
);
16774 input_location
= loc
;
16780 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16781 function. For description of the body see comment above
16782 cp_parser_omp_declare_reduction_exprs. */
16785 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16787 if (t
== NULL_TREE
|| t
== error_mark_node
)
16790 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16792 tree_stmt_iterator tsi
;
16795 memset (stmts
, 0, sizeof stmts
);
16796 for (i
= 0, tsi
= tsi_start (t
);
16797 i
< 7 && !tsi_end_p (tsi
);
16798 i
++, tsi_next (&tsi
))
16799 stmts
[i
] = tsi_stmt (tsi
);
16800 gcc_assert (tsi_end_p (tsi
));
16804 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16805 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16806 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16807 args
, complain
, in_decl
);
16808 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16809 args
, complain
, in_decl
);
16810 DECL_CONTEXT (omp_out
) = current_function_decl
;
16811 DECL_CONTEXT (omp_in
) = current_function_decl
;
16812 keep_next_level (true);
16813 tree block
= begin_omp_structured_block ();
16814 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16815 block
= finish_omp_structured_block (block
);
16816 block
= maybe_cleanup_point_expr_void (block
);
16817 add_decl_expr (omp_out
);
16818 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16819 TREE_NO_WARNING (omp_out
) = 1;
16820 add_decl_expr (omp_in
);
16821 finish_expr_stmt (block
);
16825 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16826 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16827 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16828 args
, complain
, in_decl
);
16829 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16830 args
, complain
, in_decl
);
16831 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16832 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16833 keep_next_level (true);
16834 tree block
= begin_omp_structured_block ();
16835 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16836 block
= finish_omp_structured_block (block
);
16837 block
= maybe_cleanup_point_expr_void (block
);
16838 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16839 add_decl_expr (omp_priv
);
16840 add_decl_expr (omp_orig
);
16841 finish_expr_stmt (block
);
16843 add_decl_expr (omp_orig
);
16847 /* T is a postfix-expression that is not being used in a function
16848 call. Return the substituted version of T. */
16851 tsubst_non_call_postfix_expression (tree t
, tree args
,
16852 tsubst_flags_t complain
,
16855 if (TREE_CODE (t
) == SCOPE_REF
)
16856 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16857 /*done=*/false, /*address_p=*/false);
16859 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16860 /*function_p=*/false,
16861 /*integral_constant_expression_p=*/false);
16866 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16867 instantiation context. Instantiating a pack expansion containing a lambda
16868 might result in multiple lambdas all based on the same lambda in the
16872 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16874 tree oldfn
= lambda_function (t
);
16877 tree r
= build_lambda_expr ();
16879 LAMBDA_EXPR_LOCATION (r
)
16880 = LAMBDA_EXPR_LOCATION (t
);
16881 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16882 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16883 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16885 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16886 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16888 record_lambda_scope (r
);
16890 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16891 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16893 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16894 cap
= TREE_CHAIN (cap
))
16896 tree field
= TREE_PURPOSE (cap
);
16897 if (PACK_EXPANSION_P (field
))
16898 field
= PACK_EXPANSION_PATTERN (field
);
16899 field
= tsubst_decl (field
, args
, complain
);
16901 if (field
== error_mark_node
)
16902 return error_mark_node
;
16904 tree init
= TREE_VALUE (cap
);
16905 if (PACK_EXPANSION_P (init
))
16906 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
16908 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
16909 /*fn*/false, /*constexpr*/false);
16911 if (TREE_CODE (field
) == TREE_VEC
)
16913 int len
= TREE_VEC_LENGTH (field
);
16914 gcc_assert (TREE_CODE (init
) == TREE_VEC
16915 && TREE_VEC_LENGTH (init
) == len
);
16916 for (int i
= 0; i
< len
; ++i
)
16917 LAMBDA_EXPR_CAPTURE_LIST (r
)
16918 = tree_cons (TREE_VEC_ELT (field
, i
),
16919 TREE_VEC_ELT (init
, i
),
16920 LAMBDA_EXPR_CAPTURE_LIST (r
));
16924 LAMBDA_EXPR_CAPTURE_LIST (r
)
16925 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
16927 if (id_equal (DECL_NAME (field
), "__this"))
16928 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
16932 tree type
= begin_lambda_type (r
);
16934 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16935 determine_visibility (TYPE_NAME (type
));
16937 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
16939 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
16940 ? DECL_TI_TEMPLATE (oldfn
)
16943 tree fntype
= static_fn_type (oldfn
);
16945 ++processing_template_decl
;
16946 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
16948 --processing_template_decl
;
16950 if (fntype
== error_mark_node
)
16951 r
= error_mark_node
;
16954 /* Fix the type of 'this'. */
16955 fntype
= build_memfn_type (fntype
, type
,
16956 type_memfn_quals (fntype
),
16957 type_memfn_rqual (fntype
));
16961 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
16962 fn
= DECL_TEMPLATE_RESULT (tmpl
);
16963 finish_member_declaration (tmpl
);
16968 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
16969 finish_member_declaration (fn
);
16972 /* Let finish_function set this. */
16973 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
16975 bool nested
= cfun
;
16977 push_function_context ();
16979 local_specialization_stack
s (lss_copy
);
16981 tree body
= start_lambda_function (fn
, r
);
16983 register_parameter_specializations (oldfn
, fn
);
16985 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
16986 /*constexpr*/false);
16988 finish_lambda_function (body
);
16991 pop_function_context ();
16993 /* The capture list was built up in reverse order; fix that now. */
16994 LAMBDA_EXPR_CAPTURE_LIST (r
)
16995 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
16997 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
16999 maybe_add_lambda_conv_op (type
);
17002 finish_struct (type
, /*attr*/NULL_TREE
);
17004 insert_pending_capture_proxies ();
17009 /* Like tsubst but deals with expressions and performs semantic
17010 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17013 tsubst_copy_and_build (tree t
,
17015 tsubst_flags_t complain
,
17018 bool integral_constant_expression_p
)
17020 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17021 #define RECUR(NODE) \
17022 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17023 /*function_p=*/false, \
17024 integral_constant_expression_p)
17029 if (t
== NULL_TREE
|| t
== error_mark_node
)
17032 loc
= input_location
;
17033 if (EXPR_HAS_LOCATION (t
))
17034 input_location
= EXPR_LOCATION (t
);
17036 /* N3276 decltype magic only applies to calls at the top level or on the
17037 right side of a comma. */
17038 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17039 complain
&= ~tf_decltype
;
17041 switch (TREE_CODE (t
))
17045 /* Fall through. */
17046 case IDENTIFIER_NODE
:
17050 bool non_integral_constant_expression_p
;
17051 const char *error_msg
;
17053 if (IDENTIFIER_CONV_OP_P (t
))
17055 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17056 t
= make_conv_op_name (new_type
);
17059 /* Look up the name. */
17060 decl
= lookup_name (t
);
17062 /* By convention, expressions use ERROR_MARK_NODE to indicate
17063 failure, not NULL_TREE. */
17064 if (decl
== NULL_TREE
)
17065 decl
= error_mark_node
;
17067 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17069 integral_constant_expression_p
,
17070 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17071 &non_integral_constant_expression_p
,
17072 /*template_p=*/false,
17074 /*address_p=*/false,
17075 /*template_arg_p=*/false,
17080 if (!function_p
&& identifier_p (decl
))
17082 if (complain
& tf_error
)
17083 unqualified_name_lookup_error (decl
);
17084 decl
= error_mark_node
;
17089 case TEMPLATE_ID_EXPR
:
17092 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17093 tree targs
= TREE_OPERAND (t
, 1);
17096 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17097 if (targs
== error_mark_node
)
17098 return error_mark_node
;
17100 if (TREE_CODE (templ
) == SCOPE_REF
)
17102 tree name
= TREE_OPERAND (templ
, 1);
17103 tree tid
= lookup_template_function (name
, targs
);
17104 TREE_OPERAND (templ
, 1) = tid
;
17108 if (variable_template_p (templ
))
17109 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17111 if (TREE_CODE (templ
) == COMPONENT_REF
)
17113 object
= TREE_OPERAND (templ
, 0);
17114 templ
= TREE_OPERAND (templ
, 1);
17117 object
= NULL_TREE
;
17118 templ
= lookup_template_function (templ
, targs
);
17121 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17122 object
, templ
, NULL_TREE
));
17124 RETURN (baselink_for_fns (templ
));
17129 tree r
= RECUR (TREE_OPERAND (t
, 0));
17131 if (REFERENCE_REF_P (t
))
17133 /* A type conversion to reference type will be enclosed in
17134 such an indirect ref, but the substitution of the cast
17135 will have also added such an indirect ref. */
17136 r
= convert_from_reference (r
);
17139 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17140 complain
|decltype_flag
);
17142 if (TREE_CODE (r
) == INDIRECT_REF
)
17143 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17150 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17151 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17152 RETURN (build_nop (type
, op0
));
17155 case IMPLICIT_CONV_EXPR
:
17157 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17158 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17159 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17161 retval
= copy_node (t
);
17162 TREE_TYPE (retval
) = type
;
17163 TREE_OPERAND (retval
, 0) = expr
;
17166 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17167 /* We'll pass this to convert_nontype_argument again, we don't need
17168 to actually perform any conversion here. */
17170 int flags
= LOOKUP_IMPLICIT
;
17171 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17172 flags
= LOOKUP_NORMAL
;
17173 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17179 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17180 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17181 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17185 case REINTERPRET_CAST_EXPR
:
17186 case CONST_CAST_EXPR
:
17187 case DYNAMIC_CAST_EXPR
:
17188 case STATIC_CAST_EXPR
:
17191 tree op
, r
= NULL_TREE
;
17193 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17194 if (integral_constant_expression_p
17195 && !cast_valid_in_integral_constant_expression_p (type
))
17197 if (complain
& tf_error
)
17198 error ("a cast to a type other than an integral or "
17199 "enumeration type cannot appear in a constant-expression");
17200 RETURN (error_mark_node
);
17203 op
= RECUR (TREE_OPERAND (t
, 0));
17205 warning_sentinel
s(warn_useless_cast
);
17206 warning_sentinel
s2(warn_ignored_qualifiers
);
17207 switch (TREE_CODE (t
))
17210 r
= build_functional_cast (type
, op
, complain
);
17212 case REINTERPRET_CAST_EXPR
:
17213 r
= build_reinterpret_cast (type
, op
, complain
);
17215 case CONST_CAST_EXPR
:
17216 r
= build_const_cast (type
, op
, complain
);
17218 case DYNAMIC_CAST_EXPR
:
17219 r
= build_dynamic_cast (type
, op
, complain
);
17221 case STATIC_CAST_EXPR
:
17222 r
= build_static_cast (type
, op
, complain
);
17225 gcc_unreachable ();
17231 case POSTDECREMENT_EXPR
:
17232 case POSTINCREMENT_EXPR
:
17233 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17234 args
, complain
, in_decl
);
17235 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17236 complain
|decltype_flag
));
17238 case PREDECREMENT_EXPR
:
17239 case PREINCREMENT_EXPR
:
17243 case TRUTH_NOT_EXPR
:
17244 case UNARY_PLUS_EXPR
: /* Unary + */
17245 case REALPART_EXPR
:
17246 case IMAGPART_EXPR
:
17247 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17248 RECUR (TREE_OPERAND (t
, 0)),
17249 complain
|decltype_flag
));
17251 case FIX_TRUNC_EXPR
:
17252 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17256 op1
= TREE_OPERAND (t
, 0);
17257 if (TREE_CODE (op1
) == LABEL_DECL
)
17258 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17259 EXPR_LOCATION (op1
)));
17260 if (TREE_CODE (op1
) == SCOPE_REF
)
17261 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17262 /*done=*/true, /*address_p=*/true);
17264 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17266 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17267 complain
|decltype_flag
));
17272 case TRUNC_DIV_EXPR
:
17273 case CEIL_DIV_EXPR
:
17274 case FLOOR_DIV_EXPR
:
17275 case ROUND_DIV_EXPR
:
17276 case EXACT_DIV_EXPR
:
17280 case TRUNC_MOD_EXPR
:
17281 case FLOOR_MOD_EXPR
:
17282 case TRUTH_ANDIF_EXPR
:
17283 case TRUTH_ORIF_EXPR
:
17284 case TRUTH_AND_EXPR
:
17285 case TRUTH_OR_EXPR
:
17301 warning_sentinel
s1(warn_type_limits
);
17302 warning_sentinel
s2(warn_div_by_zero
);
17303 warning_sentinel
s3(warn_logical_op
);
17304 warning_sentinel
s4(warn_tautological_compare
);
17305 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17306 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17307 tree r
= build_x_binary_op
17308 (input_location
, TREE_CODE (t
),
17310 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17312 : TREE_CODE (TREE_OPERAND (t
, 0))),
17314 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17316 : TREE_CODE (TREE_OPERAND (t
, 1))),
17318 complain
|decltype_flag
);
17319 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17320 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17325 case POINTER_PLUS_EXPR
:
17327 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17328 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17329 return fold_build_pointer_plus (op0
, op1
);
17333 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17334 /*address_p=*/false));
17336 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17337 args
, complain
, in_decl
);
17338 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17339 RECUR (TREE_OPERAND (t
, 1)),
17340 complain
|decltype_flag
));
17343 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17344 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17345 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17352 op1
= TREE_OPERAND (t
, 0);
17353 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17354 op1
= TREE_TYPE (op1
);
17357 /* When there are no ARGS, we are trying to evaluate a
17358 non-dependent expression from the parser. Trying to do
17359 the substitutions may not work. */
17361 op1
= TREE_TYPE (op1
);
17365 ++cp_unevaluated_operand
;
17366 ++c_inhibit_evaluation_warnings
;
17368 op1
= tsubst (op1
, args
, complain
, in_decl
);
17370 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17371 /*function_p=*/false,
17372 /*integral_constant_expression_p=*/
17374 --cp_unevaluated_operand
;
17375 --c_inhibit_evaluation_warnings
;
17378 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17379 complain
& tf_error
);
17381 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17382 complain
& tf_error
);
17383 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17385 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17387 if (!processing_template_decl
&& TYPE_P (op1
))
17389 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17390 build1 (NOP_EXPR
, op1
, error_mark_node
));
17391 SIZEOF_EXPR_TYPE_P (r
) = 1;
17394 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17395 TREE_SIDE_EFFECTS (r
) = 0;
17396 TREE_READONLY (r
) = 1;
17398 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17403 case AT_ENCODE_EXPR
:
17405 op1
= TREE_OPERAND (t
, 0);
17406 ++cp_unevaluated_operand
;
17407 ++c_inhibit_evaluation_warnings
;
17408 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17409 /*function_p=*/false,
17410 /*integral_constant_expression_p=*/false);
17411 --cp_unevaluated_operand
;
17412 --c_inhibit_evaluation_warnings
;
17413 RETURN (objc_build_encode_expr (op1
));
17416 case NOEXCEPT_EXPR
:
17417 op1
= TREE_OPERAND (t
, 0);
17418 ++cp_unevaluated_operand
;
17419 ++c_inhibit_evaluation_warnings
;
17420 ++cp_noexcept_operand
;
17421 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17422 /*function_p=*/false,
17423 /*integral_constant_expression_p=*/false);
17424 --cp_unevaluated_operand
;
17425 --c_inhibit_evaluation_warnings
;
17426 --cp_noexcept_operand
;
17427 RETURN (finish_noexcept_expr (op1
, complain
));
17431 warning_sentinel
s(warn_div_by_zero
);
17432 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17433 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17434 tree r
= build_x_modify_expr
17435 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17436 complain
|decltype_flag
);
17437 /* TREE_NO_WARNING must be set if either the expression was
17438 parenthesized or it uses an operator such as >>= rather
17439 than plain assignment. In the former case, it was already
17440 set and must be copied. In the latter case,
17441 build_x_modify_expr sets it and it must not be reset
17443 if (TREE_NO_WARNING (t
))
17444 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17450 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17451 args
, complain
, in_decl
);
17452 /* Remember that there was a reference to this entity. */
17454 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17455 RETURN (error_mark_node
);
17456 RETURN (build_x_arrow (input_location
, op1
, complain
));
17460 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17461 tree init
= RECUR (TREE_OPERAND (t
, 3));
17462 vec
<tree
, va_gc
> *placement_vec
;
17463 vec
<tree
, va_gc
> *init_vec
;
17466 if (placement
== NULL_TREE
)
17467 placement_vec
= NULL
;
17470 placement_vec
= make_tree_vector ();
17471 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17472 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17475 /* If there was an initializer in the original tree, but it
17476 instantiated to an empty list, then we should pass a
17477 non-NULL empty vector to tell build_new that it was an
17478 empty initializer() rather than no initializer. This can
17479 only happen when the initializer is a pack expansion whose
17480 parameter packs are of length zero. */
17481 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17485 init_vec
= make_tree_vector ();
17486 if (init
== void_node
)
17487 gcc_assert (init_vec
!= NULL
);
17490 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17491 vec_safe_push (init_vec
, TREE_VALUE (init
));
17495 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17496 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17497 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17498 NEW_EXPR_USE_GLOBAL (t
),
17501 if (placement_vec
!= NULL
)
17502 release_tree_vector (placement_vec
);
17503 if (init_vec
!= NULL
)
17504 release_tree_vector (init_vec
);
17511 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17512 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17513 RETURN (delete_sanity (op0
, op1
,
17514 DELETE_EXPR_USE_VEC (t
),
17515 DELETE_EXPR_USE_GLOBAL (t
),
17519 case COMPOUND_EXPR
:
17521 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17522 complain
& ~tf_decltype
, in_decl
,
17523 /*function_p=*/false,
17524 integral_constant_expression_p
);
17525 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17527 RECUR (TREE_OPERAND (t
, 1)),
17528 complain
|decltype_flag
));
17534 vec
<tree
, va_gc
> *call_args
;
17535 unsigned int nargs
, i
;
17540 function
= CALL_EXPR_FN (t
);
17541 /* Internal function with no arguments. */
17542 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17545 /* When we parsed the expression, we determined whether or
17546 not Koenig lookup should be performed. */
17547 koenig_p
= KOENIG_LOOKUP_P (t
);
17548 if (function
== NULL_TREE
)
17551 qualified_p
= false;
17553 else if (TREE_CODE (function
) == SCOPE_REF
)
17555 qualified_p
= true;
17556 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17558 /*address_p=*/false);
17560 else if (koenig_p
&& identifier_p (function
))
17562 /* Do nothing; calling tsubst_copy_and_build on an identifier
17563 would incorrectly perform unqualified lookup again.
17565 Note that we can also have an IDENTIFIER_NODE if the earlier
17566 unqualified lookup found a member function; in that case
17567 koenig_p will be false and we do want to do the lookup
17568 again to find the instantiated member function.
17570 FIXME but doing that causes c++/15272, so we need to stop
17571 using IDENTIFIER_NODE in that situation. */
17572 qualified_p
= false;
17576 if (TREE_CODE (function
) == COMPONENT_REF
)
17578 tree op
= TREE_OPERAND (function
, 1);
17580 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17581 || (BASELINK_P (op
)
17582 && BASELINK_QUALIFIED_P (op
)));
17585 qualified_p
= false;
17587 if (TREE_CODE (function
) == ADDR_EXPR
17588 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17589 /* Avoid error about taking the address of a constructor. */
17590 function
= TREE_OPERAND (function
, 0);
17592 function
= tsubst_copy_and_build (function
, args
, complain
,
17595 integral_constant_expression_p
);
17597 if (BASELINK_P (function
))
17598 qualified_p
= true;
17601 nargs
= call_expr_nargs (t
);
17602 call_args
= make_tree_vector ();
17603 for (i
= 0; i
< nargs
; ++i
)
17605 tree arg
= CALL_EXPR_ARG (t
, i
);
17607 if (!PACK_EXPANSION_P (arg
))
17608 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17611 /* Expand the pack expansion and push each entry onto
17613 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17614 if (TREE_CODE (arg
) == TREE_VEC
)
17616 unsigned int len
, j
;
17618 len
= TREE_VEC_LENGTH (arg
);
17619 for (j
= 0; j
< len
; ++j
)
17621 tree value
= TREE_VEC_ELT (arg
, j
);
17622 if (value
!= NULL_TREE
)
17623 value
= convert_from_reference (value
);
17624 vec_safe_push (call_args
, value
);
17629 /* A partial substitution. Add one entry. */
17630 vec_safe_push (call_args
, arg
);
17635 /* We do not perform argument-dependent lookup if normal
17636 lookup finds a non-function, in accordance with the
17637 expected resolution of DR 218. */
17639 && ((is_overloaded_fn (function
)
17640 /* If lookup found a member function, the Koenig lookup is
17641 not appropriate, even if an unqualified-name was used
17642 to denote the function. */
17643 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17644 || identifier_p (function
))
17645 /* Only do this when substitution turns a dependent call
17646 into a non-dependent call. */
17647 && type_dependent_expression_p_push (t
)
17648 && !any_type_dependent_arguments_p (call_args
))
17649 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17651 if (function
!= NULL_TREE
17652 && identifier_p (function
)
17653 && !any_type_dependent_arguments_p (call_args
))
17655 if (koenig_p
&& (complain
& tf_warning_or_error
))
17657 /* For backwards compatibility and good diagnostics, try
17658 the unqualified lookup again if we aren't in SFINAE
17660 tree unq
= (tsubst_copy_and_build
17661 (function
, args
, complain
, in_decl
, true,
17662 integral_constant_expression_p
));
17663 if (unq
== error_mark_node
)
17665 release_tree_vector (call_args
);
17666 RETURN (error_mark_node
);
17669 if (unq
!= function
)
17671 /* In a lambda fn, we have to be careful to not
17672 introduce new this captures. Legacy code can't
17673 be using lambdas anyway, so it's ok to be
17675 bool in_lambda
= (current_class_type
17676 && LAMBDA_TYPE_P (current_class_type
));
17677 char const *const msg
17678 = G_("%qD was not declared in this scope, "
17679 "and no declarations were found by "
17680 "argument-dependent lookup at the point "
17681 "of instantiation");
17685 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17688 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17694 if (INDIRECT_REF_P (fn
))
17695 fn
= TREE_OPERAND (fn
, 0);
17696 if (is_overloaded_fn (fn
))
17697 fn
= get_first_fn (fn
);
17700 /* Can't say anything more. */;
17701 else if (DECL_CLASS_SCOPE_P (fn
))
17703 location_t loc
= EXPR_LOC_OR_LOC (t
,
17706 "declarations in dependent base %qT are "
17707 "not found by unqualified lookup",
17708 DECL_CLASS_CONTEXT (fn
));
17709 if (current_class_ptr
)
17711 "use %<this->%D%> instead", function
);
17714 "use %<%T::%D%> instead",
17715 current_class_name
, function
);
17718 inform (DECL_SOURCE_LOCATION (fn
),
17719 "%qD declared here, later in the "
17720 "translation unit", fn
);
17723 release_tree_vector (call_args
);
17724 RETURN (error_mark_node
);
17731 if (identifier_p (function
))
17733 if (complain
& tf_error
)
17734 unqualified_name_lookup_error (function
);
17735 release_tree_vector (call_args
);
17736 RETURN (error_mark_node
);
17740 /* Remember that there was a reference to this entity. */
17741 if (function
!= NULL_TREE
17742 && DECL_P (function
)
17743 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17745 release_tree_vector (call_args
);
17746 RETURN (error_mark_node
);
17749 /* Put back tf_decltype for the actual call. */
17750 complain
|= decltype_flag
;
17752 if (function
== NULL_TREE
)
17753 switch (CALL_EXPR_IFN (t
))
17756 gcc_assert (nargs
== 1);
17757 if (vec_safe_length (call_args
) != 1)
17759 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17760 "wrong number of arguments to "
17761 "%<__builtin_launder%>");
17762 ret
= error_mark_node
;
17765 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17767 (*call_args
)[0], complain
);
17771 /* Unsupported internal function with arguments. */
17772 gcc_unreachable ();
17774 else if (TREE_CODE (function
) == OFFSET_REF
)
17775 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17777 else if (TREE_CODE (function
) == COMPONENT_REF
)
17779 tree instance
= TREE_OPERAND (function
, 0);
17780 tree fn
= TREE_OPERAND (function
, 1);
17782 if (processing_template_decl
17783 && (type_dependent_expression_p (instance
)
17784 || (!BASELINK_P (fn
)
17785 && TREE_CODE (fn
) != FIELD_DECL
)
17786 || type_dependent_expression_p (fn
)
17787 || any_type_dependent_arguments_p (call_args
)))
17788 ret
= build_min_nt_call_vec (function
, call_args
);
17789 else if (!BASELINK_P (fn
))
17790 ret
= finish_call_expr (function
, &call_args
,
17791 /*disallow_virtual=*/false,
17792 /*koenig_p=*/false,
17795 ret
= (build_new_method_call
17797 &call_args
, NULL_TREE
,
17798 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17803 ret
= finish_call_expr (function
, &call_args
,
17804 /*disallow_virtual=*/qualified_p
,
17808 release_tree_vector (call_args
);
17810 if (ret
!= error_mark_node
)
17812 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17813 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17814 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17815 bool thk
= CALL_FROM_THUNK_P (t
);
17816 if (op
|| ord
|| rev
|| thk
)
17818 function
= extract_call_expr (ret
);
17819 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17820 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17821 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17824 CALL_FROM_THUNK_P (function
) = true;
17825 /* The thunk location is not interesting. */
17826 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17836 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17837 tree folded_cond
= fold_non_dependent_expr (cond
);
17840 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17842 if (integer_zerop (folded_cond
))
17844 ++c_inhibit_evaluation_warnings
;
17845 exp1
= RECUR (TREE_OPERAND (t
, 1));
17846 --c_inhibit_evaluation_warnings
;
17847 exp2
= RECUR (TREE_OPERAND (t
, 2));
17851 exp1
= RECUR (TREE_OPERAND (t
, 1));
17852 ++c_inhibit_evaluation_warnings
;
17853 exp2
= RECUR (TREE_OPERAND (t
, 2));
17854 --c_inhibit_evaluation_warnings
;
17856 cond
= folded_cond
;
17860 exp1
= RECUR (TREE_OPERAND (t
, 1));
17861 exp2
= RECUR (TREE_OPERAND (t
, 2));
17864 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17865 cond
, exp1
, exp2
, complain
));
17868 case PSEUDO_DTOR_EXPR
:
17870 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17871 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17872 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17873 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17879 tree purpose
, value
, chain
;
17881 if (t
== void_list_node
)
17884 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17885 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17887 /* We have pack expansions, so expand those and
17888 create a new list out of it. */
17889 tree purposevec
= NULL_TREE
;
17890 tree valuevec
= NULL_TREE
;
17894 /* Expand the argument expressions. */
17895 if (TREE_PURPOSE (t
))
17896 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17897 complain
, in_decl
);
17898 if (TREE_VALUE (t
))
17899 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17900 complain
, in_decl
);
17902 /* Build the rest of the list. */
17903 chain
= TREE_CHAIN (t
);
17904 if (chain
&& chain
!= void_type_node
)
17905 chain
= RECUR (chain
);
17907 /* Determine the number of arguments. */
17908 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17910 len
= TREE_VEC_LENGTH (purposevec
);
17911 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17913 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17914 len
= TREE_VEC_LENGTH (valuevec
);
17917 /* Since we only performed a partial substitution into
17918 the argument pack, we only RETURN (a single list
17920 if (purposevec
== TREE_PURPOSE (t
)
17921 && valuevec
== TREE_VALUE (t
)
17922 && chain
== TREE_CHAIN (t
))
17925 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17928 /* Convert the argument vectors into a TREE_LIST */
17932 /* Grab the Ith values. */
17934 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17937 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17940 /* Build the list (backwards). */
17941 chain
= tree_cons (purpose
, value
, chain
);
17947 purpose
= TREE_PURPOSE (t
);
17949 purpose
= RECUR (purpose
);
17950 value
= TREE_VALUE (t
);
17952 value
= RECUR (value
);
17953 chain
= TREE_CHAIN (t
);
17954 if (chain
&& chain
!= void_type_node
)
17955 chain
= RECUR (chain
);
17956 if (purpose
== TREE_PURPOSE (t
)
17957 && value
== TREE_VALUE (t
)
17958 && chain
== TREE_CHAIN (t
))
17960 RETURN (tree_cons (purpose
, value
, chain
));
17963 case COMPONENT_REF
:
17970 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17971 args
, complain
, in_decl
);
17972 /* Remember that there was a reference to this entity. */
17973 if (DECL_P (object
)
17974 && !mark_used (object
, complain
) && !(complain
& tf_error
))
17975 RETURN (error_mark_node
);
17976 object_type
= TREE_TYPE (object
);
17978 member
= TREE_OPERAND (t
, 1);
17979 if (BASELINK_P (member
))
17980 member
= tsubst_baselink (member
,
17981 non_reference (TREE_TYPE (object
)),
17982 args
, complain
, in_decl
);
17984 member
= tsubst_copy (member
, args
, complain
, in_decl
);
17985 if (member
== error_mark_node
)
17986 RETURN (error_mark_node
);
17988 if (TREE_CODE (member
) == FIELD_DECL
)
17990 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
17991 if (TREE_CODE (r
) == COMPONENT_REF
)
17992 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17995 else if (type_dependent_expression_p (object
))
17996 /* We can't do much here. */;
17997 else if (!CLASS_TYPE_P (object_type
))
17999 if (scalarish_type_p (object_type
))
18001 tree s
= NULL_TREE
;
18002 tree dtor
= member
;
18004 if (TREE_CODE (dtor
) == SCOPE_REF
)
18006 s
= TREE_OPERAND (dtor
, 0);
18007 dtor
= TREE_OPERAND (dtor
, 1);
18009 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18011 dtor
= TREE_OPERAND (dtor
, 0);
18013 RETURN (finish_pseudo_destructor_expr
18014 (object
, s
, dtor
, input_location
));
18018 else if (TREE_CODE (member
) == SCOPE_REF
18019 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18021 /* Lookup the template functions now that we know what the
18023 tree scope
= TREE_OPERAND (member
, 0);
18024 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18025 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18026 member
= lookup_qualified_name (scope
, tmpl
,
18027 /*is_type_p=*/false,
18028 /*complain=*/false);
18029 if (BASELINK_P (member
))
18031 BASELINK_FUNCTIONS (member
)
18032 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18034 member
= (adjust_result_of_qualified_name_lookup
18035 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18040 qualified_name_lookup_error (scope
, tmpl
, member
,
18042 RETURN (error_mark_node
);
18045 else if (TREE_CODE (member
) == SCOPE_REF
18046 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18047 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18049 if (complain
& tf_error
)
18051 if (TYPE_P (TREE_OPERAND (member
, 0)))
18052 error ("%qT is not a class or namespace",
18053 TREE_OPERAND (member
, 0));
18055 error ("%qD is not a class or namespace",
18056 TREE_OPERAND (member
, 0));
18058 RETURN (error_mark_node
);
18061 r
= finish_class_member_access_expr (object
, member
,
18062 /*template_p=*/false,
18064 if (TREE_CODE (r
) == COMPONENT_REF
)
18065 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18070 RETURN (build_throw
18071 (RECUR (TREE_OPERAND (t
, 0))));
18075 vec
<constructor_elt
, va_gc
> *n
;
18076 constructor_elt
*ce
;
18077 unsigned HOST_WIDE_INT idx
;
18078 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18079 bool process_index_p
;
18081 bool need_copy_p
= false;
18084 if (type
== error_mark_node
)
18085 RETURN (error_mark_node
);
18087 /* digest_init will do the wrong thing if we let it. */
18088 if (type
&& TYPE_PTRMEMFUNC_P (type
))
18091 /* We do not want to process the index of aggregate
18092 initializers as they are identifier nodes which will be
18093 looked up by digest_init. */
18094 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18096 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18097 newlen
= vec_safe_length (n
);
18098 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18100 if (ce
->index
&& process_index_p
18101 /* An identifier index is looked up in the type
18102 being initialized, not the current scope. */
18103 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18104 ce
->index
= RECUR (ce
->index
);
18106 if (PACK_EXPANSION_P (ce
->value
))
18108 /* Substitute into the pack expansion. */
18109 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18112 if (ce
->value
== error_mark_node
18113 || PACK_EXPANSION_P (ce
->value
))
18115 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18116 /* Just move the argument into place. */
18117 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18120 /* Update the length of the final CONSTRUCTOR
18121 arguments vector, and note that we will need to
18123 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18124 need_copy_p
= true;
18128 ce
->value
= RECUR (ce
->value
);
18133 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18135 vec_alloc (n
, newlen
);
18136 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18138 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18140 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18141 for (i
= 0; i
< len
; ++i
)
18142 CONSTRUCTOR_APPEND_ELT (n
, 0,
18143 TREE_VEC_ELT (ce
->value
, i
));
18146 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18150 r
= build_constructor (init_list_type_node
, n
);
18151 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18153 if (TREE_HAS_CONSTRUCTOR (t
))
18155 fcl_t cl
= fcl_functional
;
18156 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18158 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18161 TREE_TYPE (r
) = type
;
18167 tree operand_0
= TREE_OPERAND (t
, 0);
18168 if (TYPE_P (operand_0
))
18170 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18171 RETURN (get_typeid (operand_0
, complain
));
18175 operand_0
= RECUR (operand_0
);
18176 RETURN (build_typeid (operand_0
, complain
));
18183 else if (DECL_PACK_P (t
))
18185 /* We don't build decls for an instantiation of a
18186 variadic capture proxy, we instantiate the elements
18188 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
18189 return RECUR (DECL_VALUE_EXPR (t
));
18195 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18196 /* ??? We're doing a subset of finish_id_expression here. */
18198 && !processing_template_decl
18199 && !cp_unevaluated_operand
18200 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18201 && CP_DECL_THREAD_LOCAL_P (r
))
18203 if (tree wrap
= get_tls_wrapper_fn (r
))
18204 /* Replace an evaluated use of the thread_local variable with
18205 a call to its wrapper. */
18206 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18208 else if (outer_automatic_var_p (r
))
18209 r
= process_outer_var_ref (r
, complain
);
18211 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18212 /* If the original type was a reference, we'll be wrapped in
18213 the appropriate INDIRECT_REF. */
18214 r
= convert_from_reference (r
);
18220 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18221 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18222 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18225 case OFFSETOF_EXPR
:
18228 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18229 in_decl
, /*function_p=*/false,
18230 /*integral_constant_expression_p=*/false);
18231 RETURN (finish_offsetof (object_ptr
,
18232 RECUR (TREE_OPERAND (t
, 0)),
18233 EXPR_LOCATION (t
)));
18236 case ADDRESSOF_EXPR
:
18237 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18238 RECUR (TREE_OPERAND (t
, 0)), complain
));
18242 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18243 complain
, in_decl
);
18245 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18246 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18247 type2
= RECUR (type2
);
18249 type2
= tsubst (type2
, args
, complain
, in_decl
);
18251 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18256 tree old_stmt_expr
= cur_stmt_expr
;
18257 tree stmt_expr
= begin_stmt_expr ();
18259 cur_stmt_expr
= stmt_expr
;
18260 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18261 integral_constant_expression_p
);
18262 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18263 cur_stmt_expr
= old_stmt_expr
;
18265 /* If the resulting list of expression statement is empty,
18266 fold it further into void_node. */
18267 if (empty_expr_stmt_p (stmt_expr
))
18268 stmt_expr
= void_node
;
18270 RETURN (stmt_expr
);
18275 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18277 RETURN (build_lambda_object (r
));
18281 /* We can get here for a constant initializer of non-dependent type.
18282 FIXME stop folding in cp_parser_initializer_clause. */
18284 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18289 case TRANSACTION_EXPR
:
18290 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18291 integral_constant_expression_p
));
18294 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18296 case VEC_PERM_EXPR
:
18298 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18299 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18300 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18301 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18305 case REQUIRES_EXPR
:
18306 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18309 /* Handle Objective-C++ constructs, if appropriate. */
18312 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18313 in_decl
, /*function_p=*/false);
18317 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18323 input_location
= loc
;
18327 /* Verify that the instantiated ARGS are valid. For type arguments,
18328 make sure that the type's linkage is ok. For non-type arguments,
18329 make sure they are constants if they are integral or enumerations.
18330 Emit an error under control of COMPLAIN, and return TRUE on error. */
18333 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18335 if (dependent_template_arg_p (t
))
18337 if (ARGUMENT_PACK_P (t
))
18339 tree vec
= ARGUMENT_PACK_ARGS (t
);
18340 int len
= TREE_VEC_LENGTH (vec
);
18341 bool result
= false;
18344 for (i
= 0; i
< len
; ++i
)
18345 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18349 else if (TYPE_P (t
))
18351 /* [basic.link]: A name with no linkage (notably, the name
18352 of a class or enumeration declared in a local scope)
18353 shall not be used to declare an entity with linkage.
18354 This implies that names with no linkage cannot be used as
18357 DR 757 relaxes this restriction for C++0x. */
18358 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18359 : no_linkage_check (t
, /*relaxed_p=*/false));
18363 /* DR 488 makes use of a type with no linkage cause
18364 type deduction to fail. */
18365 if (complain
& tf_error
)
18367 if (TYPE_UNNAMED_P (nt
))
18368 error ("%qT is/uses unnamed type", t
);
18370 error ("template argument for %qD uses local type %qT",
18375 /* In order to avoid all sorts of complications, we do not
18376 allow variably-modified types as template arguments. */
18377 else if (variably_modified_type_p (t
, NULL_TREE
))
18379 if (complain
& tf_error
)
18380 error ("%qT is a variably modified type", t
);
18384 /* Class template and alias template arguments should be OK. */
18385 else if (DECL_TYPE_TEMPLATE_P (t
))
18387 /* A non-type argument of integral or enumerated type must be a
18389 else if (TREE_TYPE (t
)
18390 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18391 && !REFERENCE_REF_P (t
)
18392 && !TREE_CONSTANT (t
))
18394 if (complain
& tf_error
)
18395 error ("integral expression %qE is not constant", t
);
18402 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18404 int ix
, len
= DECL_NTPARMS (tmpl
);
18405 bool result
= false;
18407 for (ix
= 0; ix
!= len
; ix
++)
18409 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18412 if (result
&& (complain
& tf_error
))
18413 error (" trying to instantiate %qD", tmpl
);
18417 /* We're out of SFINAE context now, so generate diagnostics for the access
18418 errors we saw earlier when instantiating D from TMPL and ARGS. */
18421 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18423 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18424 tree type
= TREE_TYPE (pattern
);
18425 location_t loc
= input_location
;
18427 push_access_scope (d
);
18428 push_deferring_access_checks (dk_no_deferred
);
18429 input_location
= DECL_SOURCE_LOCATION (pattern
);
18430 tsubst (type
, args
, tf_warning_or_error
, d
);
18431 input_location
= loc
;
18432 pop_deferring_access_checks ();
18433 pop_access_scope (d
);
18436 /* Instantiate the indicated variable, function, or alias template TMPL with
18437 the template arguments in TARG_PTR. */
18440 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18442 tree targ_ptr
= orig_args
;
18446 bool access_ok
= true;
18448 if (tmpl
== error_mark_node
)
18449 return error_mark_node
;
18451 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18453 /* If this function is a clone, handle it specially. */
18454 if (DECL_CLONED_FUNCTION_P (tmpl
))
18459 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18460 DECL_CLONED_FUNCTION. */
18461 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18462 targ_ptr
, complain
);
18463 if (spec
== error_mark_node
)
18464 return error_mark_node
;
18466 /* Look for the clone. */
18467 FOR_EACH_CLONE (clone
, spec
)
18468 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18470 /* We should always have found the clone by now. */
18471 gcc_unreachable ();
18475 if (targ_ptr
== error_mark_node
)
18476 return error_mark_node
;
18478 /* Check to see if we already have this specialization. */
18479 gen_tmpl
= most_general_template (tmpl
);
18480 if (TMPL_ARGS_DEPTH (targ_ptr
)
18481 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18482 /* targ_ptr only has the innermost template args, so add the outer ones
18483 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18484 the case of a non-dependent call within a template definition). */
18485 targ_ptr
= (add_outermost_template_args
18486 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18489 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18490 but it doesn't seem to be on the hot path. */
18491 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18493 gcc_assert (tmpl
== gen_tmpl
18494 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18496 || fndecl
== NULL_TREE
);
18498 if (spec
!= NULL_TREE
)
18500 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18502 if (complain
& tf_error
)
18503 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18504 return error_mark_node
;
18509 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18511 return error_mark_node
;
18513 /* We are building a FUNCTION_DECL, during which the access of its
18514 parameters and return types have to be checked. However this
18515 FUNCTION_DECL which is the desired context for access checking
18516 is not built yet. We solve this chicken-and-egg problem by
18517 deferring all checks until we have the FUNCTION_DECL. */
18518 push_deferring_access_checks (dk_deferred
);
18520 /* Instantiation of the function happens in the context of the function
18521 template, not the context of the overload resolution we're doing. */
18522 push_to_top_level ();
18523 /* If there are dependent arguments, e.g. because we're doing partial
18524 ordering, make sure processing_template_decl stays set. */
18525 if (uses_template_parms (targ_ptr
))
18526 ++processing_template_decl
;
18527 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18529 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18530 complain
, gen_tmpl
, true);
18531 push_nested_class (ctx
);
18534 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18536 fndecl
= NULL_TREE
;
18537 if (VAR_P (pattern
))
18539 /* We need to determine if we're using a partial or explicit
18540 specialization now, because the type of the variable could be
18542 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18543 tree elt
= most_specialized_partial_spec (tid
, complain
);
18544 if (elt
== error_mark_node
)
18545 pattern
= error_mark_node
;
18548 tree partial_tmpl
= TREE_VALUE (elt
);
18549 tree partial_args
= TREE_PURPOSE (elt
);
18550 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18551 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18555 /* Substitute template parameters to obtain the specialization. */
18556 if (fndecl
== NULL_TREE
)
18557 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18558 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18559 pop_nested_class ();
18560 pop_from_top_level ();
18562 if (fndecl
== error_mark_node
)
18564 pop_deferring_access_checks ();
18565 return error_mark_node
;
18568 /* The DECL_TI_TEMPLATE should always be the immediate parent
18569 template, not the most general template. */
18570 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18571 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18573 /* Now we know the specialization, compute access previously
18574 deferred. Do no access control for inheriting constructors,
18575 as we already checked access for the inherited constructor. */
18576 if (!(flag_new_inheriting_ctors
18577 && DECL_INHERITED_CTOR (fndecl
)))
18579 push_access_scope (fndecl
);
18580 if (!perform_deferred_access_checks (complain
))
18582 pop_access_scope (fndecl
);
18584 pop_deferring_access_checks ();
18586 /* If we've just instantiated the main entry point for a function,
18587 instantiate all the alternate entry points as well. We do this
18588 by cloning the instantiation of the main entry point, not by
18589 instantiating the template clones. */
18590 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18591 clone_function_decl (fndecl
, /*update_methods=*/false);
18595 if (!(complain
& tf_error
))
18597 /* Remember to reinstantiate when we're out of SFINAE so the user
18598 can see the errors. */
18599 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18601 return error_mark_node
;
18606 /* Wrapper for instantiate_template_1. */
18609 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18612 timevar_push (TV_TEMPLATE_INST
);
18613 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18614 timevar_pop (TV_TEMPLATE_INST
);
18618 /* Instantiate the alias template TMPL with ARGS. Also push a template
18619 instantiation level, which instantiate_template doesn't do because
18620 functions and variables have sufficient context established by the
18624 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18626 struct pending_template
*old_last_pend
= last_pending_template
;
18627 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18628 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18629 return error_mark_node
;
18630 tree tinst
= build_tree_list (tmpl
, args
);
18631 if (!push_tinst_level (tinst
))
18634 return error_mark_node
;
18638 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18639 args
, tmpl
, complain
,
18640 /*require_all_args=*/true,
18641 /*use_default_args=*/true);
18643 tree r
= instantiate_template (tmpl
, args
, complain
);
18644 pop_tinst_level ();
18645 /* We can't free this if a pending_template entry or last_error_tinst_level
18646 is pointing at it. */
18647 if (last_pending_template
== old_last_pend
18648 && last_error_tinst_level
== old_error_tinst
)
18654 /* PARM is a template parameter pack for FN. Returns true iff
18655 PARM is used in a deducible way in the argument list of FN. */
18658 pack_deducible_p (tree parm
, tree fn
)
18660 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18661 for (; t
; t
= TREE_CHAIN (t
))
18663 tree type
= TREE_VALUE (t
);
18665 if (!PACK_EXPANSION_P (type
))
18667 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18668 packs
; packs
= TREE_CHAIN (packs
))
18669 if (template_args_equal (TREE_VALUE (packs
), parm
))
18671 /* The template parameter pack is used in a function parameter
18672 pack. If this is the end of the parameter list, the
18673 template parameter pack is deducible. */
18674 if (TREE_CHAIN (t
) == void_list_node
)
18677 /* Otherwise, not. Well, it could be deduced from
18678 a non-pack parameter, but doing so would end up with
18679 a deduction mismatch, so don't bother. */
18683 /* The template parameter pack isn't used in any function parameter
18684 packs, but it might be used deeper, e.g. tuple<Args...>. */
18688 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18689 NARGS elements of the arguments that are being used when calling
18690 it. TARGS is a vector into which the deduced template arguments
18693 Returns either a FUNCTION_DECL for the matching specialization of FN or
18694 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18695 true, diagnostics will be printed to explain why it failed.
18697 If FN is a conversion operator, or we are trying to produce a specific
18698 specialization, RETURN_TYPE is the return type desired.
18700 The EXPLICIT_TARGS are explicit template arguments provided via a
18703 The parameter STRICT is one of:
18706 We are deducing arguments for a function call, as in
18707 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18708 deducing arguments for a call to the result of a conversion
18709 function template, as in [over.call.object].
18712 We are deducing arguments for a conversion function, as in
18713 [temp.deduct.conv].
18716 We are deducing arguments when doing an explicit instantiation
18717 as in [temp.explicit], when determining an explicit specialization
18718 as in [temp.expl.spec], or when taking the address of a function
18719 template, as in [temp.deduct.funcaddr]. */
18722 fn_type_unification (tree fn
,
18723 tree explicit_targs
,
18726 unsigned int nargs
,
18728 unification_kind_t strict
,
18735 tree decl
= NULL_TREE
;
18736 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18738 static int deduction_depth
;
18739 struct pending_template
*old_last_pend
= last_pending_template
;
18740 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18743 if (flag_new_inheriting_ctors
)
18744 fn
= strip_inheriting_ctors (fn
);
18746 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18748 tree r
= error_mark_node
;
18750 tree full_targs
= targs
;
18751 if (TMPL_ARGS_DEPTH (targs
)
18752 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18753 full_targs
= (add_outermost_template_args
18754 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18758 complain
|= tf_decltype
;
18760 /* In C++0x, it's possible to have a function template whose type depends
18761 on itself recursively. This is most obvious with decltype, but can also
18762 occur with enumeration scope (c++/48969). So we need to catch infinite
18763 recursion and reject the substitution at deduction time; this function
18764 will return error_mark_node for any repeated substitution.
18766 This also catches excessive recursion such as when f<N> depends on
18767 f<N-1> across all integers, and returns error_mark_node for all the
18768 substitutions back up to the initial one.
18770 This is, of course, not reentrant. */
18771 if (excessive_deduction_depth
)
18772 return error_mark_node
;
18773 tinst
= build_tree_list (fn
, NULL_TREE
);
18776 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18778 fntype
= TREE_TYPE (fn
);
18779 if (explicit_targs
)
18783 The specified template arguments must match the template
18784 parameters in kind (i.e., type, nontype, template), and there
18785 must not be more arguments than there are parameters;
18786 otherwise type deduction fails.
18788 Nontype arguments must match the types of the corresponding
18789 nontype template parameters, or must be convertible to the
18790 types of the corresponding nontype parameters as specified in
18791 _temp.arg.nontype_, otherwise type deduction fails.
18793 All references in the function type of the function template
18794 to the corresponding template parameters are replaced by the
18795 specified template argument values. If a substitution in a
18796 template parameter or in the function type of the function
18797 template results in an invalid type, type deduction fails. */
18798 int i
, len
= TREE_VEC_LENGTH (tparms
);
18799 location_t loc
= input_location
;
18800 bool incomplete
= false;
18802 if (explicit_targs
== error_mark_node
)
18805 if (TMPL_ARGS_DEPTH (explicit_targs
)
18806 < TMPL_ARGS_DEPTH (full_targs
))
18807 explicit_targs
= add_outermost_template_args (full_targs
,
18810 /* Adjust any explicit template arguments before entering the
18811 substitution context. */
18813 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18815 /*require_all_args=*/false,
18816 /*use_default_args=*/false));
18817 if (explicit_targs
== error_mark_node
)
18820 /* Substitute the explicit args into the function type. This is
18821 necessary so that, for instance, explicitly declared function
18822 arguments can match null pointed constants. If we were given
18823 an incomplete set of explicit args, we must not do semantic
18824 processing during substitution as we could create partial
18826 for (i
= 0; i
< len
; i
++)
18828 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18829 bool parameter_pack
= false;
18830 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18832 /* Dig out the actual parm. */
18833 if (TREE_CODE (parm
) == TYPE_DECL
18834 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18836 parm
= TREE_TYPE (parm
);
18837 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18839 else if (TREE_CODE (parm
) == PARM_DECL
)
18841 parm
= DECL_INITIAL (parm
);
18842 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18845 if (!parameter_pack
&& targ
== NULL_TREE
)
18846 /* No explicit argument for this template parameter. */
18849 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18851 /* Mark the argument pack as "incomplete". We could
18852 still deduce more arguments during unification.
18853 We remove this mark in type_unification_real. */
18856 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18857 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18858 = ARGUMENT_PACK_ARGS (targ
);
18861 /* We have some incomplete argument packs. */
18866 TREE_VALUE (tinst
) = explicit_targs
;
18867 if (!push_tinst_level (tinst
))
18869 excessive_deduction_depth
= true;
18872 processing_template_decl
+= incomplete
;
18873 input_location
= DECL_SOURCE_LOCATION (fn
);
18874 /* Ignore any access checks; we'll see them again in
18875 instantiate_template and they might have the wrong
18876 access path at this point. */
18877 push_deferring_access_checks (dk_deferred
);
18878 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18879 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18880 pop_deferring_access_checks ();
18881 input_location
= loc
;
18882 processing_template_decl
-= incomplete
;
18883 pop_tinst_level ();
18885 if (fntype
== error_mark_node
)
18888 /* Place the explicitly specified arguments in TARGS. */
18889 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18890 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18891 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18894 /* Never do unification on the 'this' parameter. */
18895 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18897 if (return_type
&& strict
== DEDUCE_CALL
)
18899 /* We're deducing for a call to the result of a template conversion
18900 function. The parms we really want are in return_type. */
18901 if (POINTER_TYPE_P (return_type
))
18902 return_type
= TREE_TYPE (return_type
);
18903 parms
= TYPE_ARG_TYPES (return_type
);
18905 else if (return_type
)
18909 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18910 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18911 new_args
[0] = return_type
;
18912 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18917 /* We allow incomplete unification without an error message here
18918 because the standard doesn't seem to explicitly prohibit it. Our
18919 callers must be ready to deal with unification failures in any
18922 TREE_VALUE (tinst
) = targs
;
18923 /* If we aren't explaining yet, push tinst context so we can see where
18924 any errors (e.g. from class instantiations triggered by instantiation
18925 of default template arguments) come from. If we are explaining, this
18926 context is redundant. */
18927 if (!explain_p
&& !push_tinst_level (tinst
))
18929 excessive_deduction_depth
= true;
18933 /* type_unification_real will pass back any access checks from default
18934 template argument substitution. */
18935 vec
<deferred_access_check
, va_gc
> *checks
;
18938 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18939 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18940 strict
, flags
, &checks
, explain_p
);
18942 pop_tinst_level ();
18946 /* Now that we have bindings for all of the template arguments,
18947 ensure that the arguments deduced for the template template
18948 parameters have compatible template parameter lists. We cannot
18949 check this property before we have deduced all template
18950 arguments, because the template parameter types of a template
18951 template parameter might depend on prior template parameters
18952 deduced after the template template parameter. The following
18953 ill-formed example illustrates this issue:
18955 template<typename T, template<T> class C> void f(C<5>, T);
18957 template<int N> struct X {};
18960 f(X<5>(), 5l); // error: template argument deduction fails
18963 The template parameter list of 'C' depends on the template type
18964 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18965 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18966 time that we deduce 'C'. */
18967 if (!template_template_parm_bindings_ok_p
18968 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
18970 unify_inconsistent_template_template_parameters (explain_p
);
18974 /* All is well so far. Now, check:
18978 When all template arguments have been deduced, all uses of
18979 template parameters in nondeduced contexts are replaced with
18980 the corresponding deduced argument values. If the
18981 substitution results in an invalid type, as described above,
18982 type deduction fails. */
18983 TREE_VALUE (tinst
) = targs
;
18984 if (!push_tinst_level (tinst
))
18986 excessive_deduction_depth
= true;
18990 /* Also collect access checks from the instantiation. */
18991 reopen_deferring_access_checks (checks
);
18993 decl
= instantiate_template (fn
, targs
, complain
);
18995 checks
= get_deferred_access_checks ();
18996 pop_deferring_access_checks ();
18998 pop_tinst_level ();
19000 if (decl
== error_mark_node
)
19003 /* Now perform any access checks encountered during substitution. */
19004 push_access_scope (decl
);
19005 ok
= perform_access_checks (checks
, complain
);
19006 pop_access_scope (decl
);
19010 /* If we're looking for an exact match, check that what we got
19011 is indeed an exact match. It might not be if some template
19012 parameters are used in non-deduced contexts. But don't check
19013 for an exact match if we have dependent template arguments;
19014 in that case we're doing partial ordering, and we already know
19015 that we have two candidates that will provide the actual type. */
19016 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19018 tree substed
= TREE_TYPE (decl
);
19022 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19024 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19025 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19026 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19028 unify_type_mismatch (explain_p
, args
[i
],
19029 TREE_VALUE (sarg
));
19034 /* After doing deduction with the inherited constructor, actually return an
19035 instantiation of the inheriting constructor. */
19037 decl
= instantiate_template (orig_fn
, targs
, complain
);
19043 if (excessive_deduction_depth
)
19045 if (deduction_depth
== 0)
19046 /* Reset once we're all the way out. */
19047 excessive_deduction_depth
= false;
19050 /* We can't free this if a pending_template entry or last_error_tinst_level
19051 is pointing at it. */
19052 if (last_pending_template
== old_last_pend
19053 && last_error_tinst_level
== old_error_tinst
)
19059 /* Adjust types before performing type deduction, as described in
19060 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19061 sections are symmetric. PARM is the type of a function parameter
19062 or the return type of the conversion function. ARG is the type of
19063 the argument passed to the call, or the type of the value
19064 initialized with the result of the conversion function.
19065 ARG_EXPR is the original argument expression, which may be null. */
19068 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19081 /* Swap PARM and ARG throughout the remainder of this
19082 function; the handling is precisely symmetric since PARM
19083 will initialize ARG rather than vice versa. */
19084 std::swap (parm
, arg
);
19088 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19089 too, but here handle it by stripping the reference from PARM
19090 rather than by adding it to ARG. */
19091 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19092 && TYPE_REF_IS_RVALUE (*parm
)
19093 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19094 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19095 && TREE_CODE (*arg
) == REFERENCE_TYPE
19096 && !TYPE_REF_IS_RVALUE (*arg
))
19097 *parm
= TREE_TYPE (*parm
);
19098 /* Nothing else to do in this case. */
19102 gcc_unreachable ();
19105 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19107 /* [temp.deduct.call]
19109 If P is not a reference type:
19111 --If A is an array type, the pointer type produced by the
19112 array-to-pointer standard conversion (_conv.array_) is
19113 used in place of A for type deduction; otherwise,
19115 --If A is a function type, the pointer type produced by
19116 the function-to-pointer standard conversion
19117 (_conv.func_) is used in place of A for type deduction;
19120 --If A is a cv-qualified type, the top level
19121 cv-qualifiers of A's type are ignored for type
19123 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19124 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19125 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19126 *arg
= build_pointer_type (*arg
);
19128 *arg
= TYPE_MAIN_VARIANT (*arg
);
19131 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19132 reference to a cv-unqualified template parameter that does not represent a
19133 template parameter of a class template (during class template argument
19134 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19135 an lvalue, the type "lvalue reference to A" is used in place of A for type
19137 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19138 && TYPE_REF_IS_RVALUE (*parm
)
19139 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19140 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19141 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19142 && (arg_expr
? lvalue_p (arg_expr
)
19143 /* try_one_overload doesn't provide an arg_expr, but
19144 functions are always lvalues. */
19145 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19146 *arg
= build_reference_type (*arg
);
19148 /* [temp.deduct.call]
19150 If P is a cv-qualified type, the top level cv-qualifiers
19151 of P's type are ignored for type deduction. If P is a
19152 reference type, the type referred to by P is used for
19154 *parm
= TYPE_MAIN_VARIANT (*parm
);
19155 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19157 *parm
= TREE_TYPE (*parm
);
19158 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19161 /* DR 322. For conversion deduction, remove a reference type on parm
19162 too (which has been swapped into ARG). */
19163 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19164 *arg
= TREE_TYPE (*arg
);
19169 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19170 template which does contain any deducible template parameters; check if
19171 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19172 unify_one_argument. */
19175 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19176 int flags
, bool explain_p
)
19181 type
= TREE_TYPE (arg
);
19185 if (same_type_p (parm
, type
))
19186 return unify_success (explain_p
);
19188 if (strict
== DEDUCE_CONV
)
19190 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19191 explain_p
? tf_warning_or_error
: tf_none
))
19192 return unify_success (explain_p
);
19194 else if (strict
!= DEDUCE_EXACT
)
19196 if (can_convert_arg (parm
, type
,
19197 TYPE_P (arg
) ? NULL_TREE
: arg
,
19198 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19199 return unify_success (explain_p
);
19202 if (strict
== DEDUCE_EXACT
)
19203 return unify_type_mismatch (explain_p
, parm
, arg
);
19205 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19208 static bool uses_deducible_template_parms (tree type
);
19210 /* Returns true iff the expression EXPR is one from which a template
19211 argument can be deduced. In other words, if it's an undecorated
19212 use of a template non-type parameter. */
19215 deducible_expression (tree expr
)
19217 /* Strip implicit conversions. */
19218 while (CONVERT_EXPR_P (expr
))
19219 expr
= TREE_OPERAND (expr
, 0);
19220 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19223 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19224 deducible way; that is, if it has a max value of <PARM> - 1. */
19227 deducible_array_bound (tree domain
)
19229 if (domain
== NULL_TREE
)
19232 tree max
= TYPE_MAX_VALUE (domain
);
19233 if (TREE_CODE (max
) != MINUS_EXPR
)
19236 return deducible_expression (TREE_OPERAND (max
, 0));
19239 /* Returns true iff the template arguments ARGS use a template parameter
19240 in a deducible way. */
19243 deducible_template_args (tree args
)
19245 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19248 tree elt
= TREE_VEC_ELT (args
, i
);
19249 if (ARGUMENT_PACK_P (elt
))
19250 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19253 if (PACK_EXPANSION_P (elt
))
19254 elt
= PACK_EXPANSION_PATTERN (elt
);
19255 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19257 else if (TYPE_P (elt
))
19258 deducible
= uses_deducible_template_parms (elt
);
19260 deducible
= deducible_expression (elt
);
19268 /* Returns true iff TYPE contains any deducible references to template
19269 parameters, as per 14.8.2.5. */
19272 uses_deducible_template_parms (tree type
)
19274 if (PACK_EXPANSION_P (type
))
19275 type
= PACK_EXPANSION_PATTERN (type
);
19282 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19283 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19289 if (POINTER_TYPE_P (type
))
19290 return uses_deducible_template_parms (TREE_TYPE (type
));
19292 /* T[integer-constant ]
19294 if (TREE_CODE (type
) == ARRAY_TYPE
)
19295 return (uses_deducible_template_parms (TREE_TYPE (type
))
19296 || deducible_array_bound (TYPE_DOMAIN (type
)));
19308 if (TYPE_PTRMEM_P (type
))
19309 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19310 || (uses_deducible_template_parms
19311 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19313 /* template-name <T> (where template-name refers to a class template)
19314 template-name <i> (where template-name refers to a class template) */
19315 if (CLASS_TYPE_P (type
)
19316 && CLASSTYPE_TEMPLATE_INFO (type
)
19317 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19318 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19319 (CLASSTYPE_TI_ARGS (type
)));
19324 if (TREE_CODE (type
) == FUNCTION_TYPE
19325 || TREE_CODE (type
) == METHOD_TYPE
)
19327 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19329 tree parm
= TYPE_ARG_TYPES (type
);
19330 if (TREE_CODE (type
) == METHOD_TYPE
)
19331 parm
= TREE_CHAIN (parm
);
19332 for (; parm
; parm
= TREE_CHAIN (parm
))
19333 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19340 /* Subroutine of type_unification_real and unify_pack_expansion to
19341 handle unification of a single P/A pair. Parameters are as
19342 for those functions. */
19345 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19346 int subr
, unification_kind_t strict
,
19349 tree arg_expr
= NULL_TREE
;
19352 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19353 return unify_invalid (explain_p
);
19354 if (arg
== unknown_type_node
)
19355 /* We can't deduce anything from this, but we might get all the
19356 template args from other function args. */
19357 return unify_success (explain_p
);
19359 /* Implicit conversions (Clause 4) will be performed on a function
19360 argument to convert it to the type of the corresponding function
19361 parameter if the parameter type contains no template-parameters that
19362 participate in template argument deduction. */
19363 if (strict
!= DEDUCE_EXACT
19364 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19365 /* For function parameters with no deducible template parameters,
19366 just return. We'll check non-dependent conversions later. */
19367 return unify_success (explain_p
);
19372 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19373 | UNIFY_ALLOW_MORE_CV_QUAL
19374 | UNIFY_ALLOW_DERIVED
);
19378 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19382 arg_strict
= UNIFY_ALLOW_NONE
;
19386 gcc_unreachable ();
19389 /* We only do these transformations if this is the top-level
19390 parameter_type_list in a call or declaration matching; in other
19391 situations (nested function declarators, template argument lists) we
19392 won't be comparing a type to an expression, and we don't do any type
19398 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19399 if (type_unknown_p (arg
))
19401 /* [temp.deduct.type] A template-argument can be
19402 deduced from a pointer to function or pointer
19403 to member function argument if the set of
19404 overloaded functions does not contain function
19405 templates and at most one of a set of
19406 overloaded functions provides a unique
19408 resolve_overloaded_unification (tparms
, targs
, parm
,
19410 arg_strict
, explain_p
);
19411 /* If a unique match was not found, this is a
19412 non-deduced context, so we still succeed. */
19413 return unify_success (explain_p
);
19417 arg
= unlowered_expr_type (arg
);
19418 if (arg
== error_mark_node
)
19419 return unify_invalid (explain_p
);
19423 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19426 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19427 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19428 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19430 /* For deduction from an init-list we need the actual list. */
19431 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19433 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19436 /* for_each_template_parm callback that always returns 0. */
19439 zero_r (tree
, void *)
19444 /* for_each_template_parm any_fn callback to handle deduction of a template
19445 type argument from the type of an array bound. */
19448 array_deduction_r (tree t
, void *data
)
19450 tree_pair_p d
= (tree_pair_p
)data
;
19451 tree
&tparms
= d
->purpose
;
19452 tree
&targs
= d
->value
;
19454 if (TREE_CODE (t
) == ARRAY_TYPE
)
19455 if (tree dom
= TYPE_DOMAIN (t
))
19456 if (tree max
= TYPE_MAX_VALUE (dom
))
19458 if (TREE_CODE (max
) == MINUS_EXPR
)
19459 max
= TREE_OPERAND (max
, 0);
19460 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19461 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19462 UNIFY_ALLOW_NONE
, /*explain*/false);
19465 /* Keep walking. */
19469 /* Try to deduce any not-yet-deduced template type arguments from the type of
19470 an array bound. This is handled separately from unify because 14.8.2.5 says
19471 "The type of a type parameter is only deduced from an array bound if it is
19472 not otherwise deduced." */
19475 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19477 tree_pair_s data
= { tparms
, targs
};
19478 hash_set
<tree
> visited
;
19479 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19480 /*nondeduced*/false, array_deduction_r
);
19483 /* Most parms like fn_type_unification.
19485 If SUBR is 1, we're being called recursively (to unify the
19486 arguments of a function or method parameter of a function
19489 CHECKS is a pointer to a vector of access checks encountered while
19490 substituting default template arguments. */
19493 type_unification_real (tree tparms
,
19497 unsigned int xnargs
,
19499 unification_kind_t strict
,
19501 vec
<deferred_access_check
, va_gc
> **checks
,
19506 int ntparms
= TREE_VEC_LENGTH (tparms
);
19507 int saw_undeduced
= 0;
19510 unsigned int nargs
;
19513 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19514 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19515 gcc_assert (ntparms
> 0);
19517 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19519 /* Reset the number of non-defaulted template arguments contained
19521 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19529 while (parms
&& parms
!= void_list_node
19532 parm
= TREE_VALUE (parms
);
19534 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19535 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19536 /* For a function parameter pack that occurs at the end of the
19537 parameter-declaration-list, the type A of each remaining
19538 argument of the call is compared with the type P of the
19539 declarator-id of the function parameter pack. */
19542 parms
= TREE_CHAIN (parms
);
19544 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19545 /* For a function parameter pack that does not occur at the
19546 end of the parameter-declaration-list, the type of the
19547 parameter pack is a non-deduced context. */
19553 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19559 && parms
!= void_list_node
19560 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19562 /* Unify the remaining arguments with the pack expansion type. */
19564 tree parmvec
= make_tree_vec (1);
19566 /* Allocate a TREE_VEC and copy in all of the arguments */
19567 argvec
= make_tree_vec (nargs
- ia
);
19568 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19569 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19571 /* Copy the parameter into parmvec. */
19572 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19573 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19574 /*subr=*/subr
, explain_p
))
19577 /* Advance to the end of the list of parameters. */
19578 parms
= TREE_CHAIN (parms
);
19581 /* Fail if we've reached the end of the parm list, and more args
19582 are present, and the parm list isn't variadic. */
19583 if (ia
< nargs
&& parms
== void_list_node
)
19584 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19585 /* Fail if parms are left and they don't have default values and
19586 they aren't all deduced as empty packs (c++/57397). This is
19587 consistent with sufficient_parms_p. */
19588 if (parms
&& parms
!= void_list_node
19589 && TREE_PURPOSE (parms
) == NULL_TREE
)
19591 unsigned int count
= nargs
;
19596 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19599 p
= TREE_CHAIN (p
);
19601 while (p
&& p
!= void_list_node
);
19602 if (count
!= nargs
)
19603 return unify_too_few_arguments (explain_p
, ia
, count
,
19609 tsubst_flags_t complain
= (explain_p
19610 ? tf_warning_or_error
19612 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19614 for (i
= 0; i
< ntparms
; i
++)
19616 tree targ
= TREE_VEC_ELT (targs
, i
);
19617 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19619 /* Clear the "incomplete" flags on all argument packs now so that
19620 substituting them into later default arguments works. */
19621 if (targ
&& ARGUMENT_PACK_P (targ
))
19623 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19624 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19627 if (targ
|| tparm
== error_mark_node
)
19629 tparm
= TREE_VALUE (tparm
);
19631 if (TREE_CODE (tparm
) == TYPE_DECL
19632 && !tried_array_deduction
)
19634 try_array_deduction (tparms
, targs
, xparms
);
19635 tried_array_deduction
= true;
19636 if (TREE_VEC_ELT (targs
, i
))
19640 /* If this is an undeduced nontype parameter that depends on
19641 a type parameter, try another pass; its type may have been
19642 deduced from a later argument than the one from which
19643 this parameter can be deduced. */
19644 if (TREE_CODE (tparm
) == PARM_DECL
19645 && uses_template_parms (TREE_TYPE (tparm
))
19646 && saw_undeduced
< 2)
19652 /* Core issue #226 (C++0x) [temp.deduct]:
19654 If a template argument has not been deduced, its
19655 default template argument, if any, is used.
19657 When we are in C++98 mode, TREE_PURPOSE will either
19658 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19659 to explicitly check cxx_dialect here. */
19660 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19661 /* OK, there is a default argument. Wait until after the
19662 conversion check to do substitution. */
19665 /* If the type parameter is a parameter pack, then it will
19666 be deduced to an empty parameter pack. */
19667 if (template_parameter_pack_p (tparm
))
19671 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19673 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19674 TREE_CONSTANT (arg
) = 1;
19677 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19679 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19681 TREE_VEC_ELT (targs
, i
) = arg
;
19685 return unify_parameter_deduction_failure (explain_p
, tparm
);
19688 /* DR 1391: All parameters have args, now check non-dependent parms for
19690 if (saw_undeduced
< 2)
19691 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19692 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19694 parm
= TREE_VALUE (parms
);
19696 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19697 && (!TREE_CHAIN (parms
)
19698 || TREE_CHAIN (parms
) == void_list_node
))
19699 /* For a function parameter pack that occurs at the end of the
19700 parameter-declaration-list, the type A of each remaining
19701 argument of the call is compared with the type P of the
19702 declarator-id of the function parameter pack. */
19705 parms
= TREE_CHAIN (parms
);
19707 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19708 /* For a function parameter pack that does not occur at the
19709 end of the parameter-declaration-list, the type of the
19710 parameter pack is a non-deduced context. */
19716 if (uses_template_parms (parm
))
19718 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19723 /* Now substitute into the default template arguments. */
19724 for (i
= 0; i
< ntparms
; i
++)
19726 tree targ
= TREE_VEC_ELT (targs
, i
);
19727 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19729 if (targ
|| tparm
== error_mark_node
)
19731 tree parm
= TREE_VALUE (tparm
);
19733 if (TREE_CODE (parm
) == PARM_DECL
19734 && uses_template_parms (TREE_TYPE (parm
))
19735 && saw_undeduced
< 2)
19738 tree arg
= TREE_PURPOSE (tparm
);
19739 reopen_deferring_access_checks (*checks
);
19740 location_t save_loc
= input_location
;
19742 input_location
= DECL_SOURCE_LOCATION (parm
);
19743 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19744 if (!uses_template_parms (arg
))
19745 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19747 else if (saw_undeduced
< 2)
19750 arg
= error_mark_node
;
19751 input_location
= save_loc
;
19752 *checks
= get_deferred_access_checks ();
19753 pop_deferring_access_checks ();
19754 if (arg
== error_mark_node
)
19758 TREE_VEC_ELT (targs
, i
) = arg
;
19759 /* The position of the first default template argument,
19760 is also the number of non-defaulted arguments in TARGS.
19762 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19763 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19767 if (saw_undeduced
++ == 1)
19771 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19772 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19774 return unify_success (explain_p
);
19777 /* Subroutine of type_unification_real. Args are like the variables
19778 at the call site. ARG is an overloaded function (or template-id);
19779 we try deducing template args from each of the overloads, and if
19780 only one succeeds, we go with that. Modifies TARGS and returns
19781 true on success. */
19784 resolve_overloaded_unification (tree tparms
,
19788 unification_kind_t strict
,
19792 tree tempargs
= copy_node (targs
);
19794 tree goodfn
= NULL_TREE
;
19797 if (TREE_CODE (arg
) == ADDR_EXPR
)
19799 arg
= TREE_OPERAND (arg
, 0);
19805 if (TREE_CODE (arg
) == COMPONENT_REF
)
19806 /* Handle `&x' where `x' is some static or non-static member
19808 arg
= TREE_OPERAND (arg
, 1);
19810 if (TREE_CODE (arg
) == OFFSET_REF
)
19811 arg
= TREE_OPERAND (arg
, 1);
19813 /* Strip baselink information. */
19814 if (BASELINK_P (arg
))
19815 arg
= BASELINK_FUNCTIONS (arg
);
19817 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19819 /* If we got some explicit template args, we need to plug them into
19820 the affected templates before we try to unify, in case the
19821 explicit args will completely resolve the templates in question. */
19824 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19825 arg
= TREE_OPERAND (arg
, 0);
19827 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19830 tree subargs
, elem
;
19832 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19835 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19836 expl_subargs
, NULL_TREE
, tf_none
,
19837 /*require_all_args=*/true,
19838 /*use_default_args=*/true);
19839 if (subargs
!= error_mark_node
19840 && !any_dependent_template_arguments_p (subargs
))
19842 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19843 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19844 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19845 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19854 /* If no templates (or more than one) are fully resolved by the
19855 explicit arguments, this template-id is a non-deduced context; it
19856 could still be OK if we deduce all template arguments for the
19857 enclosing call through other arguments. */
19861 else if (TREE_CODE (arg
) != OVERLOAD
19862 && TREE_CODE (arg
) != FUNCTION_DECL
)
19863 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19864 -- but the deduction does not succeed because the expression is
19865 not just the function on its own. */
19868 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19871 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19872 strict
, sub_strict
, addr_p
, explain_p
)
19873 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19880 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19881 to function or pointer to member function argument if the set of
19882 overloaded functions does not contain function templates and at most
19883 one of a set of overloaded functions provides a unique match.
19885 So if we found multiple possibilities, we return success but don't
19886 deduce anything. */
19890 int i
= TREE_VEC_LENGTH (targs
);
19892 if (TREE_VEC_ELT (tempargs
, i
))
19894 tree old
= TREE_VEC_ELT (targs
, i
);
19895 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19896 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19897 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19898 /* Don't forget explicit template arguments in a pack. */
19899 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19900 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19901 TREE_VEC_ELT (targs
, i
) = new_
;
19910 /* Core DR 115: In contexts where deduction is done and fails, or in
19911 contexts where deduction is not done, if a template argument list is
19912 specified and it, along with any default template arguments, identifies
19913 a single function template specialization, then the template-id is an
19914 lvalue for the function template specialization. */
19917 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19919 tree expr
, offset
, baselink
;
19922 if (!type_unknown_p (orig_expr
))
19927 offset
= NULL_TREE
;
19928 baselink
= NULL_TREE
;
19930 if (TREE_CODE (expr
) == ADDR_EXPR
)
19932 expr
= TREE_OPERAND (expr
, 0);
19935 if (TREE_CODE (expr
) == OFFSET_REF
)
19938 expr
= TREE_OPERAND (expr
, 1);
19940 if (BASELINK_P (expr
))
19943 expr
= BASELINK_FUNCTIONS (expr
);
19946 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19949 tree goodfn
= NULL_TREE
;
19951 /* If we got some explicit template args, we need to plug them into
19952 the affected templates before we try to unify, in case the
19953 explicit args will completely resolve the templates in question. */
19955 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19956 tree arg
= TREE_OPERAND (expr
, 0);
19957 tree badfn
= NULL_TREE
;
19958 tree badargs
= NULL_TREE
;
19960 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19963 tree subargs
, elem
;
19965 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19968 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19969 expl_subargs
, NULL_TREE
, tf_none
,
19970 /*require_all_args=*/true,
19971 /*use_default_args=*/true);
19972 if (subargs
!= error_mark_node
19973 && !any_dependent_template_arguments_p (subargs
))
19975 elem
= instantiate_template (fn
, subargs
, tf_none
);
19976 if (elem
== error_mark_node
)
19981 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
19990 mark_used (goodfn
);
19993 expr
= build_baselink (BASELINK_BINFO (baselink
),
19994 BASELINK_ACCESS_BINFO (baselink
),
19995 expr
, BASELINK_OPTYPE (baselink
));
19999 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20000 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20003 expr
= cp_build_addr_expr (expr
, complain
);
20006 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20007 /* There were no good options and at least one bad one, so let the
20008 user know what the problem is. */
20009 instantiate_template (badfn
, badargs
, complain
);
20014 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20015 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20016 different overloads deduce different arguments for a given parm.
20017 ADDR_P is true if the expression for which deduction is being
20018 performed was of the form "& fn" rather than simply "fn".
20020 Returns 1 on success. */
20023 try_one_overload (tree tparms
,
20028 unification_kind_t strict
,
20037 if (arg
== error_mark_node
)
20040 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20041 to function or pointer to member function argument if the set of
20042 overloaded functions does not contain function templates and at most
20043 one of a set of overloaded functions provides a unique match.
20045 So if this is a template, just return success. */
20047 if (uses_template_parms (arg
))
20050 if (TREE_CODE (arg
) == METHOD_TYPE
)
20051 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20053 arg
= build_pointer_type (arg
);
20055 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20057 /* We don't copy orig_targs for this because if we have already deduced
20058 some template args from previous args, unify would complain when we
20059 try to deduce a template parameter for the same argument, even though
20060 there isn't really a conflict. */
20061 nargs
= TREE_VEC_LENGTH (targs
);
20062 tempargs
= make_tree_vec (nargs
);
20064 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20067 /* First make sure we didn't deduce anything that conflicts with
20068 explicitly specified args. */
20069 for (i
= nargs
; i
--; )
20071 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20072 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20076 else if (uses_template_parms (elt
))
20077 /* Since we're unifying against ourselves, we will fill in
20078 template args used in the function parm list with our own
20079 template parms. Discard them. */
20080 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20081 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20083 /* Check that the argument at each index of the deduced argument pack
20084 is equivalent to the corresponding explicitly specified argument.
20085 We may have deduced more arguments than were explicitly specified,
20088 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20089 that's wrong if we deduce the same argument pack from multiple
20090 function arguments: it's only incomplete the first time. */
20092 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20093 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20095 if (TREE_VEC_LENGTH (deduced_pack
)
20096 < TREE_VEC_LENGTH (explicit_pack
))
20099 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20100 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20101 TREE_VEC_ELT (deduced_pack
, j
)))
20104 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20108 for (i
= nargs
; i
--; )
20110 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20113 TREE_VEC_ELT (targs
, i
) = elt
;
20119 /* PARM is a template class (perhaps with unbound template
20120 parameters). ARG is a fully instantiated type. If ARG can be
20121 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20122 TARGS are as for unify. */
20125 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20128 tree copy_of_targs
;
20130 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20132 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20133 /* Matches anything. */;
20134 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20135 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20138 /* We need to make a new template argument vector for the call to
20139 unify. If we used TARGS, we'd clutter it up with the result of
20140 the attempted unification, even if this class didn't work out.
20141 We also don't want to commit ourselves to all the unifications
20142 we've already done, since unification is supposed to be done on
20143 an argument-by-argument basis. In other words, consider the
20144 following pathological case:
20146 template <int I, int J, int K>
20149 template <int I, int J>
20150 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20152 template <int I, int J, int K>
20153 void f(S<I, J, K>, S<I, I, I>);
20162 Now, by the time we consider the unification involving `s2', we
20163 already know that we must have `f<0, 0, 0>'. But, even though
20164 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20165 because there are two ways to unify base classes of S<0, 1, 2>
20166 with S<I, I, I>. If we kept the already deduced knowledge, we
20167 would reject the possibility I=1. */
20168 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20170 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20172 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20177 /* If unification failed, we're done. */
20178 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20179 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20185 /* Given a template type PARM and a class type ARG, find the unique
20186 base type in ARG that is an instance of PARM. We do not examine
20187 ARG itself; only its base-classes. If there is not exactly one
20188 appropriate base class, return NULL_TREE. PARM may be the type of
20189 a partial specialization, as well as a plain template type. Used
20192 static enum template_base_result
20193 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20194 bool explain_p
, tree
*result
)
20196 tree rval
= NULL_TREE
;
20199 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20201 binfo
= TYPE_BINFO (complete_type (arg
));
20204 /* The type could not be completed. */
20205 *result
= NULL_TREE
;
20206 return tbr_incomplete_type
;
20209 /* Walk in inheritance graph order. The search order is not
20210 important, and this avoids multiple walks of virtual bases. */
20211 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20213 tree r
= try_class_unification (tparms
, targs
, parm
,
20214 BINFO_TYPE (binfo
), explain_p
);
20218 /* If there is more than one satisfactory baseclass, then:
20222 If they yield more than one possible deduced A, the type
20226 if (rval
&& !same_type_p (r
, rval
))
20228 *result
= NULL_TREE
;
20229 return tbr_ambiguous_baseclass
;
20237 return tbr_success
;
20240 /* Returns the level of DECL, which declares a template parameter. */
20243 template_decl_level (tree decl
)
20245 switch (TREE_CODE (decl
))
20248 case TEMPLATE_DECL
:
20249 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20252 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20255 gcc_unreachable ();
20260 /* Decide whether ARG can be unified with PARM, considering only the
20261 cv-qualifiers of each type, given STRICT as documented for unify.
20262 Returns nonzero iff the unification is OK on that basis. */
20265 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20267 int arg_quals
= cp_type_quals (arg
);
20268 int parm_quals
= cp_type_quals (parm
);
20270 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20271 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20273 /* Although a CVR qualifier is ignored when being applied to a
20274 substituted template parameter ([8.3.2]/1 for example), that
20275 does not allow us to unify "const T" with "int&" because both
20276 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20277 It is ok when we're allowing additional CV qualifiers
20278 at the outer level [14.8.2.1]/3,1st bullet. */
20279 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20280 || TREE_CODE (arg
) == FUNCTION_TYPE
20281 || TREE_CODE (arg
) == METHOD_TYPE
)
20282 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20285 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20286 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20290 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20291 && (arg_quals
& parm_quals
) != parm_quals
)
20294 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20295 && (parm_quals
& arg_quals
) != arg_quals
)
20301 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20303 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20305 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20306 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20307 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20309 *index
= TEMPLATE_TYPE_IDX (parm
);
20310 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20314 *index
= TEMPLATE_PARM_IDX (parm
);
20315 *level
= TEMPLATE_PARM_LEVEL (parm
);
20319 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20321 if (unify (TP, TA, P, A, S, EP)) \
20325 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20326 expansion at the end of PACKED_PARMS. Returns 0 if the type
20327 deduction succeeds, 1 otherwise. STRICT is the same as in
20328 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20329 function call argument list. We'll need to adjust the arguments to make them
20330 types. SUBR tells us if this is from a recursive call to
20331 type_unification_real, or for comparing two template argument
20335 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20336 tree packed_args
, unification_kind_t strict
,
20337 bool subr
, bool explain_p
)
20340 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20341 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20342 tree pack
, packs
= NULL_TREE
;
20343 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20345 /* Add in any args remembered from an earlier partial instantiation. */
20346 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20348 packed_args
= expand_template_argument_pack (packed_args
);
20350 int len
= TREE_VEC_LENGTH (packed_args
);
20352 /* Determine the parameter packs we will be deducing from the
20353 pattern, and record their current deductions. */
20354 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20355 pack
; pack
= TREE_CHAIN (pack
))
20357 tree parm_pack
= TREE_VALUE (pack
);
20360 /* Determine the index and level of this parameter pack. */
20361 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20363 /* Keep track of the parameter packs and their corresponding
20365 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20366 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20369 /* Loop through all of the arguments that have not yet been
20370 unified and unify each with the pattern. */
20371 for (i
= start
; i
< len
; i
++)
20374 bool any_explicit
= false;
20375 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20377 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20378 or the element of its argument pack at the current index if
20379 this argument was explicitly specified. */
20380 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20384 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20387 if (TREE_VALUE (pack
)
20388 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20389 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20391 any_explicit
= true;
20392 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20394 TMPL_ARG (targs
, level
, idx
) = arg
;
20397 /* If we had explicit template arguments, substitute them into the
20398 pattern before deduction. */
20401 /* Some arguments might still be unspecified or dependent. */
20403 ++processing_template_decl
;
20404 dependent
= any_dependent_template_arguments_p (targs
);
20406 --processing_template_decl
;
20407 parm
= tsubst (pattern
, targs
,
20408 explain_p
? tf_warning_or_error
: tf_none
,
20411 --processing_template_decl
;
20412 if (parm
== error_mark_node
)
20418 /* Unify the pattern with the current argument. */
20419 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20423 /* For each parameter pack, collect the deduced value. */
20424 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20427 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20429 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20430 TMPL_ARG (targs
, level
, idx
);
20434 /* Verify that the results of unification with the parameter packs
20435 produce results consistent with what we've seen before, and make
20436 the deduced argument packs available. */
20437 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20439 tree old_pack
= TREE_VALUE (pack
);
20440 tree new_args
= TREE_TYPE (pack
);
20441 int i
, len
= TREE_VEC_LENGTH (new_args
);
20443 bool nondeduced_p
= false;
20445 /* By default keep the original deduced argument pack.
20446 If necessary, more specific code is going to update the
20447 resulting deduced argument later down in this function. */
20448 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20449 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20451 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20452 actually deduce anything. */
20453 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20454 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20455 nondeduced_p
= true;
20459 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20461 /* If we had fewer function args than explicit template args,
20462 just use the explicits. */
20463 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20464 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20465 if (len
< explicit_len
)
20466 new_args
= explicit_args
;
20472 /* Build the deduced *_ARGUMENT_PACK. */
20473 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20475 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20476 TREE_CONSTANT (result
) = 1;
20479 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20481 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20483 /* Note the deduced argument packs for this parameter
20485 TMPL_ARG (targs
, level
, idx
) = result
;
20487 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20488 && (ARGUMENT_PACK_ARGS (old_pack
)
20489 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20491 /* We only had the explicitly-provided arguments before, but
20492 now we have a complete set of arguments. */
20493 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20495 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20496 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20497 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20501 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20502 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20504 if (!comp_template_args (old_args
, new_args
,
20505 &bad_old_arg
, &bad_new_arg
))
20506 /* Inconsistent unification of this parameter pack. */
20507 return unify_parameter_pack_inconsistent (explain_p
,
20513 return unify_success (explain_p
);
20516 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20517 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20518 parameters and return value are as for unify. */
20521 unify_array_domain (tree tparms
, tree targs
,
20522 tree parm_dom
, tree arg_dom
,
20530 /* Our representation of array types uses "N - 1" as the
20531 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20532 not an integer constant. We cannot unify arbitrarily
20533 complex expressions, so we eliminate the MINUS_EXPRs
20535 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20536 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20539 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20540 parm_max
= TREE_OPERAND (parm_max
, 0);
20542 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20543 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20546 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20547 trying to unify the type of a variable with the type
20548 of a template parameter. For example:
20550 template <unsigned int N>
20551 void f (char (&) [N]);
20558 Here, the type of the ARG will be "int [g(i)]", and
20559 may be a SAVE_EXPR, etc. */
20560 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20561 return unify_vla_arg (explain_p
, arg_dom
);
20562 arg_max
= TREE_OPERAND (arg_max
, 0);
20565 /* If only one of the bounds used a MINUS_EXPR, compensate
20566 by adding one to the other bound. */
20567 if (parm_cst
&& !arg_cst
)
20568 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20572 else if (arg_cst
&& !parm_cst
)
20573 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20578 return unify (tparms
, targs
, parm_max
, arg_max
,
20579 UNIFY_ALLOW_INTEGER
, explain_p
);
20582 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20584 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20589 if (PACK_EXPANSION_P (t
))
20590 t
= PACK_EXPANSION_PATTERN (t
);
20591 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20592 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20593 || DECL_TYPE_TEMPLATE_P (t
))
20595 else if (TYPE_P (t
))
20601 /* Deduce the value of template parameters. TPARMS is the (innermost)
20602 set of template parameters to a template. TARGS is the bindings
20603 for those template parameters, as determined thus far; TARGS may
20604 include template arguments for outer levels of template parameters
20605 as well. PARM is a parameter to a template function, or a
20606 subcomponent of that parameter; ARG is the corresponding argument.
20607 This function attempts to match PARM with ARG in a manner
20608 consistent with the existing assignments in TARGS. If more values
20609 are deduced, then TARGS is updated.
20611 Returns 0 if the type deduction succeeds, 1 otherwise. The
20612 parameter STRICT is a bitwise or of the following flags:
20615 Require an exact match between PARM and ARG.
20616 UNIFY_ALLOW_MORE_CV_QUAL:
20617 Allow the deduced ARG to be more cv-qualified (by qualification
20618 conversion) than ARG.
20619 UNIFY_ALLOW_LESS_CV_QUAL:
20620 Allow the deduced ARG to be less cv-qualified than ARG.
20621 UNIFY_ALLOW_DERIVED:
20622 Allow the deduced ARG to be a template base class of ARG,
20623 or a pointer to a template base class of the type pointed to by
20625 UNIFY_ALLOW_INTEGER:
20626 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20627 case for more information.
20628 UNIFY_ALLOW_OUTER_LEVEL:
20629 This is the outermost level of a deduction. Used to determine validity
20630 of qualification conversions. A valid qualification conversion must
20631 have const qualified pointers leading up to the inner type which
20632 requires additional CV quals, except at the outer level, where const
20633 is not required [conv.qual]. It would be normal to set this flag in
20634 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20635 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20636 This is the outermost level of a deduction, and PARM can be more CV
20637 qualified at this point.
20638 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20639 This is the outermost level of a deduction, and PARM can be less CV
20640 qualified at this point. */
20643 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20649 int strict_in
= strict
;
20650 tsubst_flags_t complain
= (explain_p
20651 ? tf_warning_or_error
20654 /* I don't think this will do the right thing with respect to types.
20655 But the only case I've seen it in so far has been array bounds, where
20656 signedness is the only information lost, and I think that will be
20658 while (CONVERT_EXPR_P (parm
))
20659 parm
= TREE_OPERAND (parm
, 0);
20661 if (arg
== error_mark_node
)
20662 return unify_invalid (explain_p
);
20663 if (arg
== unknown_type_node
20664 || arg
== init_list_type_node
)
20665 /* We can't deduce anything from this, but we might get all the
20666 template args from other function args. */
20667 return unify_success (explain_p
);
20669 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20670 return unify_success (explain_p
);
20672 /* If PARM uses template parameters, then we can't bail out here,
20673 even if ARG == PARM, since we won't record unifications for the
20674 template parameters. We might need them if we're trying to
20675 figure out which of two things is more specialized. */
20676 if (arg
== parm
&& !uses_template_parms (parm
))
20677 return unify_success (explain_p
);
20679 /* Handle init lists early, so the rest of the function can assume
20680 we're dealing with a type. */
20681 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20685 tree orig_parm
= parm
;
20687 /* Replace T with std::initializer_list<T> for deduction. */
20688 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20689 && flag_deduce_init_list
)
20690 parm
= listify (parm
);
20692 if (!is_std_init_list (parm
)
20693 && TREE_CODE (parm
) != ARRAY_TYPE
)
20694 /* We can only deduce from an initializer list argument if the
20695 parameter is std::initializer_list or an array; otherwise this
20696 is a non-deduced context. */
20697 return unify_success (explain_p
);
20699 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20700 elttype
= TREE_TYPE (parm
);
20703 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20704 /* Deduction is defined in terms of a single type, so just punt
20705 on the (bizarre) std::initializer_list<T...>. */
20706 if (PACK_EXPANSION_P (elttype
))
20707 return unify_success (explain_p
);
20710 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20712 int elt_strict
= strict
;
20714 if (elt
== error_mark_node
)
20715 return unify_invalid (explain_p
);
20717 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20719 tree type
= TREE_TYPE (elt
);
20720 if (type
== error_mark_node
)
20721 return unify_invalid (explain_p
);
20722 /* It should only be possible to get here for a call. */
20723 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20724 elt_strict
|= maybe_adjust_types_for_deduction
20725 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20729 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20733 if (TREE_CODE (parm
) == ARRAY_TYPE
20734 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20736 /* Also deduce from the length of the initializer list. */
20737 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20738 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20739 if (idx
== error_mark_node
)
20740 return unify_invalid (explain_p
);
20741 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20745 /* If the std::initializer_list<T> deduction worked, replace the
20746 deduced A with std::initializer_list<A>. */
20747 if (orig_parm
!= parm
)
20749 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20750 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20751 targ
= listify (targ
);
20752 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20754 return unify_success (explain_p
);
20757 /* If parm and arg aren't the same kind of thing (template, type, or
20758 expression), fail early. */
20759 if (pa_kind (parm
) != pa_kind (arg
))
20760 return unify_invalid (explain_p
);
20762 /* Immediately reject some pairs that won't unify because of
20763 cv-qualification mismatches. */
20764 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20766 /* It is the elements of the array which hold the cv quals of an array
20767 type, and the elements might be template type parms. We'll check
20768 when we recurse. */
20769 && TREE_CODE (arg
) != ARRAY_TYPE
20770 /* We check the cv-qualifiers when unifying with template type
20771 parameters below. We want to allow ARG `const T' to unify with
20772 PARM `T' for example, when computing which of two templates
20773 is more specialized, for example. */
20774 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20775 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20776 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20778 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20779 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20780 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20781 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20782 strict
&= ~UNIFY_ALLOW_DERIVED
;
20783 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20784 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20786 switch (TREE_CODE (parm
))
20788 case TYPENAME_TYPE
:
20790 case UNBOUND_CLASS_TEMPLATE
:
20791 /* In a type which contains a nested-name-specifier, template
20792 argument values cannot be deduced for template parameters used
20793 within the nested-name-specifier. */
20794 return unify_success (explain_p
);
20796 case TEMPLATE_TYPE_PARM
:
20797 case TEMPLATE_TEMPLATE_PARM
:
20798 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20799 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20800 if (error_operand_p (tparm
))
20801 return unify_invalid (explain_p
);
20803 if (TEMPLATE_TYPE_LEVEL (parm
)
20804 != template_decl_level (tparm
))
20805 /* The PARM is not one we're trying to unify. Just check
20806 to see if it matches ARG. */
20808 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20809 && (is_auto (parm
) ? is_auto (arg
)
20810 : same_type_p (parm
, arg
)))
20811 return unify_success (explain_p
);
20813 return unify_type_mismatch (explain_p
, parm
, arg
);
20815 idx
= TEMPLATE_TYPE_IDX (parm
);
20816 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20817 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20818 if (error_operand_p (tparm
))
20819 return unify_invalid (explain_p
);
20821 /* Check for mixed types and values. */
20822 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20823 && TREE_CODE (tparm
) != TYPE_DECL
)
20824 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20825 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20826 gcc_unreachable ();
20828 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20830 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20831 && CLASS_TYPE_P (arg
))
20833 /* First try to match ARG directly. */
20834 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20838 /* Otherwise, look for a suitable base of ARG, as below. */
20839 enum template_base_result r
;
20840 r
= get_template_base (tparms
, targs
, parm
, arg
,
20843 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20847 /* ARG must be constructed from a template class or a template
20848 template parameter. */
20849 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20850 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20851 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20853 /* Deduce arguments T, i from TT<T> or TT<i>. */
20854 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20857 arg
= TYPE_TI_TEMPLATE (arg
);
20859 /* Fall through to deduce template name. */
20862 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20863 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20865 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20867 /* Simple cases: Value already set, does match or doesn't. */
20868 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20869 return unify_success (explain_p
);
20871 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20875 /* If PARM is `const T' and ARG is only `int', we don't have
20876 a match unless we are allowing additional qualification.
20877 If ARG is `const int' and PARM is just `T' that's OK;
20878 that binds `const int' to `T'. */
20879 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20881 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20883 /* Consider the case where ARG is `const volatile int' and
20884 PARM is `const T'. Then, T should be `volatile int'. */
20885 arg
= cp_build_qualified_type_real
20886 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20887 if (arg
== error_mark_node
)
20888 return unify_invalid (explain_p
);
20890 /* Simple cases: Value already set, does match or doesn't. */
20891 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20892 return unify_success (explain_p
);
20894 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20896 /* Make sure that ARG is not a variable-sized array. (Note
20897 that were talking about variable-sized arrays (like
20898 `int[n]'), rather than arrays of unknown size (like
20899 `int[]').) We'll get very confused by such a type since
20900 the bound of the array is not constant, and therefore
20901 not mangleable. Besides, such types are not allowed in
20902 ISO C++, so we can do as we please here. We do allow
20903 them for 'auto' deduction, since that isn't ABI-exposed. */
20904 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20905 return unify_vla_arg (explain_p
, arg
);
20907 /* Strip typedefs as in convert_template_argument. */
20908 arg
= canonicalize_type_argument (arg
, tf_none
);
20911 /* If ARG is a parameter pack or an expansion, we cannot unify
20912 against it unless PARM is also a parameter pack. */
20913 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20914 && !template_parameter_pack_p (parm
))
20915 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20917 /* If the argument deduction results is a METHOD_TYPE,
20918 then there is a problem.
20919 METHOD_TYPE doesn't map to any real C++ type the result of
20920 the deduction can not be of that type. */
20921 if (TREE_CODE (arg
) == METHOD_TYPE
)
20922 return unify_method_type_error (explain_p
, arg
);
20924 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20925 return unify_success (explain_p
);
20927 case TEMPLATE_PARM_INDEX
:
20928 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20929 if (error_operand_p (tparm
))
20930 return unify_invalid (explain_p
);
20932 if (TEMPLATE_PARM_LEVEL (parm
)
20933 != template_decl_level (tparm
))
20935 /* The PARM is not one we're trying to unify. Just check
20936 to see if it matches ARG. */
20937 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20938 && cp_tree_equal (parm
, arg
));
20940 unify_expression_unequal (explain_p
, parm
, arg
);
20944 idx
= TEMPLATE_PARM_IDX (parm
);
20945 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20949 if ((strict
& UNIFY_ALLOW_INTEGER
)
20950 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
20951 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
20952 /* We're deducing from an array bound, the type doesn't matter. */
20953 arg
= fold_convert (TREE_TYPE (targ
), arg
);
20954 int x
= !cp_tree_equal (targ
, arg
);
20956 unify_inconsistency (explain_p
, parm
, targ
, arg
);
20960 /* [temp.deduct.type] If, in the declaration of a function template
20961 with a non-type template-parameter, the non-type
20962 template-parameter is used in an expression in the function
20963 parameter-list and, if the corresponding template-argument is
20964 deduced, the template-argument type shall match the type of the
20965 template-parameter exactly, except that a template-argument
20966 deduced from an array bound may be of any integral type.
20967 The non-type parameter might use already deduced type parameters. */
20968 ++processing_template_decl
;
20969 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
20970 --processing_template_decl
;
20971 if (tree a
= type_uses_auto (tparm
))
20973 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
20974 if (tparm
== error_mark_node
)
20978 if (!TREE_TYPE (arg
))
20979 /* Template-parameter dependent expression. Just accept it for now.
20980 It will later be processed in convert_template_argument. */
20982 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
20983 non_reference (tparm
)))
20985 else if ((strict
& UNIFY_ALLOW_INTEGER
)
20986 && CP_INTEGRAL_TYPE_P (tparm
))
20987 /* Convert the ARG to the type of PARM; the deduced non-type
20988 template argument must exactly match the types of the
20989 corresponding parameter. */
20990 arg
= fold (build_nop (tparm
, arg
));
20991 else if (uses_template_parms (tparm
))
20993 /* We haven't deduced the type of this parameter yet. */
20994 if (cxx_dialect
>= cxx17
20995 /* We deduce from array bounds in try_array_deduction. */
20996 && !(strict
& UNIFY_ALLOW_INTEGER
))
20998 /* Deduce it from the non-type argument. */
20999 tree atype
= TREE_TYPE (arg
);
21000 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21002 UNIFY_ALLOW_NONE
, explain_p
);
21005 /* Try again later. */
21006 return unify_success (explain_p
);
21009 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21011 /* If ARG is a parameter pack or an expansion, we cannot unify
21012 against it unless PARM is also a parameter pack. */
21013 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21014 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21015 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21018 bool removed_attr
= false;
21019 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21021 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21022 return unify_success (explain_p
);
21026 /* A pointer-to-member constant can be unified only with
21027 another constant. */
21028 if (TREE_CODE (arg
) != PTRMEM_CST
)
21029 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21031 /* Just unify the class member. It would be useless (and possibly
21032 wrong, depending on the strict flags) to unify also
21033 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21034 arg refer to the same variable, even if through different
21035 classes. For instance:
21037 struct A { int x; };
21040 Unification of &A::x and &B::x must succeed. */
21041 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21042 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21047 if (!TYPE_PTR_P (arg
))
21048 return unify_type_mismatch (explain_p
, parm
, arg
);
21050 /* [temp.deduct.call]
21052 A can be another pointer or pointer to member type that can
21053 be converted to the deduced A via a qualification
21054 conversion (_conv.qual_).
21056 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21057 This will allow for additional cv-qualification of the
21058 pointed-to types if appropriate. */
21060 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21061 /* The derived-to-base conversion only persists through one
21062 level of pointers. */
21063 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21065 return unify (tparms
, targs
, TREE_TYPE (parm
),
21066 TREE_TYPE (arg
), strict
, explain_p
);
21069 case REFERENCE_TYPE
:
21070 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21071 return unify_type_mismatch (explain_p
, parm
, arg
);
21072 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21073 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21076 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21077 return unify_type_mismatch (explain_p
, parm
, arg
);
21078 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21079 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21080 return unify_type_mismatch (explain_p
, parm
, arg
);
21081 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21082 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21083 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21084 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21085 TYPE_DOMAIN (arg
), explain_p
);
21086 return unify_success (explain_p
);
21093 case ENUMERAL_TYPE
:
21096 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21097 return unify_type_mismatch (explain_p
, parm
, arg
);
21099 /* We have already checked cv-qualification at the top of the
21101 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21102 return unify_type_mismatch (explain_p
, parm
, arg
);
21104 /* As far as unification is concerned, this wins. Later checks
21105 will invalidate it if necessary. */
21106 return unify_success (explain_p
);
21108 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21109 /* Type INTEGER_CST can come from ordinary constant template args. */
21111 while (CONVERT_EXPR_P (arg
))
21112 arg
= TREE_OPERAND (arg
, 0);
21114 if (TREE_CODE (arg
) != INTEGER_CST
)
21115 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21116 return (tree_int_cst_equal (parm
, arg
)
21117 ? unify_success (explain_p
)
21118 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21122 int i
, len
, argslen
;
21123 int parm_variadic_p
= 0;
21125 if (TREE_CODE (arg
) != TREE_VEC
)
21126 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21128 len
= TREE_VEC_LENGTH (parm
);
21129 argslen
= TREE_VEC_LENGTH (arg
);
21131 /* Check for pack expansions in the parameters. */
21132 for (i
= 0; i
< len
; ++i
)
21134 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21137 /* We can unify against something with a trailing
21139 parm_variadic_p
= 1;
21141 /* [temp.deduct.type]/9: If the template argument list of
21142 P contains a pack expansion that is not the last
21143 template argument, the entire template argument list
21144 is a non-deduced context. */
21145 return unify_success (explain_p
);
21149 /* If we don't have enough arguments to satisfy the parameters
21150 (not counting the pack expression at the end), or we have
21151 too many arguments for a parameter list that doesn't end in
21152 a pack expression, we can't unify. */
21153 if (parm_variadic_p
21154 ? argslen
< len
- parm_variadic_p
21156 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21158 /* Unify all of the parameters that precede the (optional)
21159 pack expression. */
21160 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21162 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21163 TREE_VEC_ELT (parm
, i
),
21164 TREE_VEC_ELT (arg
, i
),
21165 UNIFY_ALLOW_NONE
, explain_p
);
21167 if (parm_variadic_p
)
21168 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21170 /*subr=*/true, explain_p
);
21171 return unify_success (explain_p
);
21176 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21177 return unify_type_mismatch (explain_p
, parm
, arg
);
21179 if (TYPE_PTRMEMFUNC_P (parm
))
21181 if (!TYPE_PTRMEMFUNC_P (arg
))
21182 return unify_type_mismatch (explain_p
, parm
, arg
);
21184 return unify (tparms
, targs
,
21185 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21186 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21187 strict
, explain_p
);
21189 else if (TYPE_PTRMEMFUNC_P (arg
))
21190 return unify_type_mismatch (explain_p
, parm
, arg
);
21192 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21194 tree t
= NULL_TREE
;
21196 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21198 /* First, we try to unify the PARM and ARG directly. */
21199 t
= try_class_unification (tparms
, targs
,
21200 parm
, arg
, explain_p
);
21204 /* Fallback to the special case allowed in
21205 [temp.deduct.call]:
21207 If P is a class, and P has the form
21208 template-id, then A can be a derived class of
21209 the deduced A. Likewise, if P is a pointer to
21210 a class of the form template-id, A can be a
21211 pointer to a derived class pointed to by the
21213 enum template_base_result r
;
21214 r
= get_template_base (tparms
, targs
, parm
, arg
,
21219 /* Don't give the derived diagnostic if we're
21220 already dealing with the same template. */
21222 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21223 && (CLASSTYPE_TI_TEMPLATE (parm
)
21224 == CLASSTYPE_TI_TEMPLATE (arg
)));
21225 return unify_no_common_base (explain_p
&& !same_template
,
21230 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21231 && (CLASSTYPE_TI_TEMPLATE (parm
)
21232 == CLASSTYPE_TI_TEMPLATE (arg
)))
21233 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21234 Then, we should unify `int' and `U'. */
21237 /* There's no chance of unification succeeding. */
21238 return unify_type_mismatch (explain_p
, parm
, arg
);
21240 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21241 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21243 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21244 return unify_type_mismatch (explain_p
, parm
, arg
);
21245 return unify_success (explain_p
);
21248 case FUNCTION_TYPE
:
21250 unsigned int nargs
;
21255 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21256 return unify_type_mismatch (explain_p
, parm
, arg
);
21258 /* CV qualifications for methods can never be deduced, they must
21259 match exactly. We need to check them explicitly here,
21260 because type_unification_real treats them as any other
21261 cv-qualified parameter. */
21262 if (TREE_CODE (parm
) == METHOD_TYPE
21263 && (!check_cv_quals_for_unify
21265 class_of_this_parm (arg
),
21266 class_of_this_parm (parm
))))
21267 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21268 if (TREE_CODE (arg
) == FUNCTION_TYPE
21269 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21270 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21271 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21272 return unify_type_mismatch (explain_p
, parm
, arg
);
21274 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21275 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21277 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21278 args
= XALLOCAVEC (tree
, nargs
);
21279 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21280 a
!= NULL_TREE
&& a
!= void_list_node
;
21281 a
= TREE_CHAIN (a
), ++i
)
21282 args
[i
] = TREE_VALUE (a
);
21285 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21286 args
, nargs
, 1, DEDUCE_EXACT
,
21287 LOOKUP_NORMAL
, NULL
, explain_p
))
21290 if (flag_noexcept_type
)
21292 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21293 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21294 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21295 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21296 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21297 && uses_template_parms (TREE_PURPOSE (pspec
)))
21298 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21299 TREE_PURPOSE (aspec
),
21300 UNIFY_ALLOW_NONE
, explain_p
);
21301 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21302 return unify_type_mismatch (explain_p
, parm
, arg
);
21309 /* Unify a pointer to member with a pointer to member function, which
21310 deduces the type of the member as a function type. */
21311 if (TYPE_PTRMEMFUNC_P (arg
))
21313 /* Check top-level cv qualifiers */
21314 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21315 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21317 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21318 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21319 UNIFY_ALLOW_NONE
, explain_p
);
21321 /* Determine the type of the function we are unifying against. */
21322 tree fntype
= static_fn_type (arg
);
21324 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21327 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21328 return unify_type_mismatch (explain_p
, parm
, arg
);
21329 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21330 TYPE_OFFSET_BASETYPE (arg
),
21331 UNIFY_ALLOW_NONE
, explain_p
);
21332 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21333 strict
, explain_p
);
21336 if (DECL_TEMPLATE_PARM_P (parm
))
21337 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21338 if (arg
!= scalar_constant_value (parm
))
21339 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21340 return unify_success (explain_p
);
21343 case TEMPLATE_DECL
:
21344 /* Matched cases are handled by the ARG == PARM test above. */
21345 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21348 /* We might get a variable as a non-type template argument in parm if the
21349 corresponding parameter is type-dependent. Make any necessary
21350 adjustments based on whether arg is a reference. */
21351 if (CONSTANT_CLASS_P (arg
))
21352 parm
= fold_non_dependent_expr (parm
);
21353 else if (REFERENCE_REF_P (arg
))
21355 tree sub
= TREE_OPERAND (arg
, 0);
21357 if (TREE_CODE (sub
) == ADDR_EXPR
)
21358 arg
= TREE_OPERAND (sub
, 0);
21360 /* Now use the normal expression code to check whether they match. */
21363 case TYPE_ARGUMENT_PACK
:
21364 case NONTYPE_ARGUMENT_PACK
:
21365 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21366 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21369 case DECLTYPE_TYPE
:
21370 case UNDERLYING_TYPE
:
21371 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21372 or UNDERLYING_TYPE nodes. */
21373 return unify_success (explain_p
);
21376 /* Unification fails if we hit an error node. */
21377 return unify_invalid (explain_p
);
21380 if (REFERENCE_REF_P (parm
))
21382 bool pexp
= PACK_EXPANSION_P (arg
);
21384 arg
= PACK_EXPANSION_PATTERN (arg
);
21385 if (REFERENCE_REF_P (arg
))
21386 arg
= TREE_OPERAND (arg
, 0);
21388 arg
= make_pack_expansion (arg
, complain
);
21389 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21390 strict
, explain_p
);
21395 /* An unresolved overload is a nondeduced context. */
21396 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21397 return unify_success (explain_p
);
21398 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21400 /* We must be looking at an expression. This can happen with
21404 void foo(S<I>, S<I + 2>);
21406 This is a "nondeduced context":
21410 The nondeduced contexts are:
21412 --A type that is a template-id in which one or more of
21413 the template-arguments is an expression that references
21414 a template-parameter.
21416 In these cases, we assume deduction succeeded, but don't
21417 actually infer any unifications. */
21419 if (!uses_template_parms (parm
)
21420 && !template_args_equal (parm
, arg
))
21421 return unify_expression_unequal (explain_p
, parm
, arg
);
21423 return unify_success (explain_p
);
21426 #undef RECUR_AND_CHECK_FAILURE
21428 /* Note that DECL can be defined in this translation unit, if
21432 mark_definable (tree decl
)
21435 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21436 FOR_EACH_CLONE (clone
, decl
)
21437 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21440 /* Called if RESULT is explicitly instantiated, or is a member of an
21441 explicitly instantiated class. */
21444 mark_decl_instantiated (tree result
, int extern_p
)
21446 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21448 /* If this entity has already been written out, it's too late to
21449 make any modifications. */
21450 if (TREE_ASM_WRITTEN (result
))
21453 /* For anonymous namespace we don't need to do anything. */
21454 if (decl_anon_ns_mem_p (result
))
21456 gcc_assert (!TREE_PUBLIC (result
));
21460 if (TREE_CODE (result
) != FUNCTION_DECL
)
21461 /* The TREE_PUBLIC flag for function declarations will have been
21462 set correctly by tsubst. */
21463 TREE_PUBLIC (result
) = 1;
21465 /* This might have been set by an earlier implicit instantiation. */
21466 DECL_COMDAT (result
) = 0;
21469 DECL_NOT_REALLY_EXTERN (result
) = 0;
21472 mark_definable (result
);
21473 mark_needed (result
);
21474 /* Always make artificials weak. */
21475 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21476 comdat_linkage (result
);
21477 /* For WIN32 we also want to put explicit instantiations in
21478 linkonce sections. */
21479 else if (TREE_PUBLIC (result
))
21480 maybe_make_one_only (result
);
21483 /* If EXTERN_P, then this function will not be emitted -- unless
21484 followed by an explicit instantiation, at which point its linkage
21485 will be adjusted. If !EXTERN_P, then this function will be
21486 emitted here. In neither circumstance do we want
21487 import_export_decl to adjust the linkage. */
21488 DECL_INTERFACE_KNOWN (result
) = 1;
21491 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21492 important template arguments. If any are missing, we check whether
21493 they're important by using error_mark_node for substituting into any
21494 args that were used for partial ordering (the ones between ARGS and END)
21495 and seeing if it bubbles up. */
21498 check_undeduced_parms (tree targs
, tree args
, tree end
)
21500 bool found
= false;
21502 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21503 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21506 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21510 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21511 if (substed
== error_mark_node
)
21517 /* Given two function templates PAT1 and PAT2, return:
21519 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21520 -1 if PAT2 is more specialized than PAT1.
21521 0 if neither is more specialized.
21523 LEN indicates the number of parameters we should consider
21524 (defaulted parameters should not be considered).
21526 The 1998 std underspecified function template partial ordering, and
21527 DR214 addresses the issue. We take pairs of arguments, one from
21528 each of the templates, and deduce them against each other. One of
21529 the templates will be more specialized if all the *other*
21530 template's arguments deduce against its arguments and at least one
21531 of its arguments *does* *not* deduce against the other template's
21532 corresponding argument. Deduction is done as for class templates.
21533 The arguments used in deduction have reference and top level cv
21534 qualifiers removed. Iff both arguments were originally reference
21535 types *and* deduction succeeds in both directions, an lvalue reference
21536 wins against an rvalue reference and otherwise the template
21537 with the more cv-qualified argument wins for that pairing (if
21538 neither is more cv-qualified, they both are equal). Unlike regular
21539 deduction, after all the arguments have been deduced in this way,
21540 we do *not* verify the deduced template argument values can be
21541 substituted into non-deduced contexts.
21543 The logic can be a bit confusing here, because we look at deduce1 and
21544 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21545 can find template arguments for pat1 to make arg1 look like arg2, that
21546 means that arg2 is at least as specialized as arg1. */
21549 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21551 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21552 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21553 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21554 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21555 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21556 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21557 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21558 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21559 tree origs1
, origs2
;
21560 bool lose1
= false;
21561 bool lose2
= false;
21563 /* Remove the this parameter from non-static member functions. If
21564 one is a non-static member function and the other is not a static
21565 member function, remove the first parameter from that function
21566 also. This situation occurs for operator functions where we
21567 locate both a member function (with this pointer) and non-member
21568 operator (with explicit first operand). */
21569 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21571 len
--; /* LEN is the number of significant arguments for DECL1 */
21572 args1
= TREE_CHAIN (args1
);
21573 if (!DECL_STATIC_FUNCTION_P (decl2
))
21574 args2
= TREE_CHAIN (args2
);
21576 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21578 args2
= TREE_CHAIN (args2
);
21579 if (!DECL_STATIC_FUNCTION_P (decl1
))
21582 args1
= TREE_CHAIN (args1
);
21586 /* If only one is a conversion operator, they are unordered. */
21587 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21590 /* Consider the return type for a conversion function */
21591 if (DECL_CONV_FN_P (decl1
))
21593 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21594 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21598 processing_template_decl
++;
21604 /* Stop when an ellipsis is seen. */
21605 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21607 tree arg1
= TREE_VALUE (args1
);
21608 tree arg2
= TREE_VALUE (args2
);
21609 int deduce1
, deduce2
;
21615 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21616 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21618 /* When both arguments are pack expansions, we need only
21619 unify the patterns themselves. */
21620 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21621 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21623 /* This is the last comparison we need to do. */
21627 /* DR 1847: If a particular P contains no template-parameters that
21628 participate in template argument deduction, that P is not used to
21629 determine the ordering. */
21630 if (!uses_deducible_template_parms (arg1
)
21631 && !uses_deducible_template_parms (arg2
))
21634 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21636 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21637 arg1
= TREE_TYPE (arg1
);
21638 quals1
= cp_type_quals (arg1
);
21641 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21643 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21644 arg2
= TREE_TYPE (arg2
);
21645 quals2
= cp_type_quals (arg2
);
21648 arg1
= TYPE_MAIN_VARIANT (arg1
);
21649 arg2
= TYPE_MAIN_VARIANT (arg2
);
21651 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21653 int i
, len2
= remaining_arguments (args2
);
21654 tree parmvec
= make_tree_vec (1);
21655 tree argvec
= make_tree_vec (len2
);
21658 /* Setup the parameter vector, which contains only ARG1. */
21659 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21661 /* Setup the argument vector, which contains the remaining
21663 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21664 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21666 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21667 argvec
, DEDUCE_EXACT
,
21668 /*subr=*/true, /*explain_p=*/false)
21671 /* We cannot deduce in the other direction, because ARG1 is
21672 a pack expansion but ARG2 is not. */
21675 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21677 int i
, len1
= remaining_arguments (args1
);
21678 tree parmvec
= make_tree_vec (1);
21679 tree argvec
= make_tree_vec (len1
);
21682 /* Setup the parameter vector, which contains only ARG1. */
21683 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21685 /* Setup the argument vector, which contains the remaining
21687 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21688 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21690 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21691 argvec
, DEDUCE_EXACT
,
21692 /*subr=*/true, /*explain_p=*/false)
21695 /* We cannot deduce in the other direction, because ARG2 is
21696 a pack expansion but ARG1 is not.*/
21702 /* The normal case, where neither argument is a pack
21704 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21705 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21707 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21708 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21712 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21713 arg2, then arg2 is not as specialized as arg1. */
21719 /* "If, for a given type, deduction succeeds in both directions
21720 (i.e., the types are identical after the transformations above)
21721 and both P and A were reference types (before being replaced with
21722 the type referred to above):
21723 - if the type from the argument template was an lvalue reference and
21724 the type from the parameter template was not, the argument type is
21725 considered to be more specialized than the other; otherwise,
21726 - if the type from the argument template is more cv-qualified
21727 than the type from the parameter template (as described above),
21728 the argument type is considered to be more specialized than the other;
21730 - neither type is more specialized than the other." */
21732 if (deduce1
&& deduce2
)
21734 if (ref1
&& ref2
&& ref1
!= ref2
)
21741 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21743 if ((quals1
& quals2
) == quals2
)
21745 if ((quals1
& quals2
) == quals1
)
21750 if (lose1
&& lose2
)
21751 /* We've failed to deduce something in either direction.
21752 These must be unordered. */
21757 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21758 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21759 /* We have already processed all of the arguments in our
21760 handing of the pack expansion type. */
21763 args1
= TREE_CHAIN (args1
);
21764 args2
= TREE_CHAIN (args2
);
21767 /* "In most cases, all template parameters must have values in order for
21768 deduction to succeed, but for partial ordering purposes a template
21769 parameter may remain without a value provided it is not used in the
21770 types being used for partial ordering."
21772 Thus, if we are missing any of the targs1 we need to substitute into
21773 origs1, then pat2 is not as specialized as pat1. This can happen when
21774 there is a nondeduced context. */
21775 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21777 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21780 processing_template_decl
--;
21782 /* If both deductions succeed, the partial ordering selects the more
21783 constrained template. */
21784 if (!lose1
&& !lose2
)
21786 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21787 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21788 lose1
= !subsumes_constraints (c1
, c2
);
21789 lose2
= !subsumes_constraints (c2
, c1
);
21792 /* All things being equal, if the next argument is a pack expansion
21793 for one function but not for the other, prefer the
21794 non-variadic function. FIXME this is bogus; see c++/41958. */
21796 && args1
&& TREE_VALUE (args1
)
21797 && args2
&& TREE_VALUE (args2
))
21799 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21800 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21803 if (lose1
== lose2
)
21811 /* Determine which of two partial specializations of TMPL is more
21814 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21815 to the first partial specialization. The TREE_PURPOSE is the
21816 innermost set of template parameters for the partial
21817 specialization. PAT2 is similar, but for the second template.
21819 Return 1 if the first partial specialization is more specialized;
21820 -1 if the second is more specialized; 0 if neither is more
21823 See [temp.class.order] for information about determining which of
21824 two templates is more specialized. */
21827 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21831 bool any_deductions
= false;
21833 tree tmpl1
= TREE_VALUE (pat1
);
21834 tree tmpl2
= TREE_VALUE (pat2
);
21835 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21836 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21838 /* Just like what happens for functions, if we are ordering between
21839 different template specializations, we may encounter dependent
21840 types in the arguments, and we need our dependency check functions
21841 to behave correctly. */
21842 ++processing_template_decl
;
21843 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21847 any_deductions
= true;
21850 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21854 any_deductions
= true;
21856 --processing_template_decl
;
21858 /* If both deductions succeed, the partial ordering selects the more
21859 constrained template. */
21860 if (!winner
&& any_deductions
)
21861 return more_constrained (tmpl1
, tmpl2
);
21863 /* In the case of a tie where at least one of the templates
21864 has a parameter pack at the end, the template with the most
21865 non-packed parameters wins. */
21868 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21869 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21871 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21872 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21873 int len1
= TREE_VEC_LENGTH (args1
);
21874 int len2
= TREE_VEC_LENGTH (args2
);
21876 /* We don't count the pack expansion at the end. */
21877 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21879 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21884 else if (len1
< len2
)
21891 /* Return the template arguments that will produce the function signature
21892 DECL from the function template FN, with the explicit template
21893 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21894 also match. Return NULL_TREE if no satisfactory arguments could be
21898 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21900 int ntparms
= DECL_NTPARMS (fn
);
21901 tree targs
= make_tree_vec (ntparms
);
21902 tree decl_type
= TREE_TYPE (decl
);
21903 tree decl_arg_types
;
21905 unsigned int nargs
, ix
;
21908 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21910 /* Never do unification on the 'this' parameter. */
21911 decl_arg_types
= skip_artificial_parms_for (decl
,
21912 TYPE_ARG_TYPES (decl_type
));
21914 nargs
= list_length (decl_arg_types
);
21915 args
= XALLOCAVEC (tree
, nargs
);
21916 for (arg
= decl_arg_types
, ix
= 0;
21917 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21918 arg
= TREE_CHAIN (arg
), ++ix
)
21919 args
[ix
] = TREE_VALUE (arg
);
21921 if (fn_type_unification (fn
, explicit_args
, targs
,
21923 (check_rettype
|| DECL_CONV_FN_P (fn
)
21924 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21925 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21927 == error_mark_node
)
21933 /* Return the innermost template arguments that, when applied to a partial
21934 specialization SPEC_TMPL of TMPL, yield the ARGS.
21936 For example, suppose we have:
21938 template <class T, class U> struct S {};
21939 template <class T> struct S<T*, int> {};
21941 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21942 partial specialization and the ARGS will be {double*, int}. The resulting
21943 vector will be {double}, indicating that `T' is bound to `double'. */
21946 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21948 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21950 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21951 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21953 tree innermost_deduced_args
;
21955 innermost_deduced_args
= make_tree_vec (ntparms
);
21956 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21958 deduced_args
= copy_node (args
);
21959 SET_TMPL_ARGS_LEVEL (deduced_args
,
21960 TMPL_ARGS_DEPTH (deduced_args
),
21961 innermost_deduced_args
);
21964 deduced_args
= innermost_deduced_args
;
21966 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21968 if (unify (tparms
, deduced_args
,
21969 INNERMOST_TEMPLATE_ARGS (spec_args
),
21970 INNERMOST_TEMPLATE_ARGS (args
),
21971 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
21974 for (i
= 0; i
< ntparms
; ++i
)
21975 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
21977 if (!tried_array_deduction
)
21979 try_array_deduction (tparms
, innermost_deduced_args
,
21980 INNERMOST_TEMPLATE_ARGS (spec_args
));
21981 tried_array_deduction
= true;
21982 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
21988 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
21989 if (!push_tinst_level (tinst
))
21991 excessive_deduction_depth
= true;
21995 /* Verify that nondeduced template arguments agree with the type
21996 obtained from argument deduction.
22000 struct A { typedef int X; };
22001 template <class T, class U> struct C {};
22002 template <class T> struct C<T, typename T::X> {};
22004 Then with the instantiation `C<A, int>', we can deduce that
22005 `T' is `A' but unify () does not check whether `typename T::X'
22007 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22009 if (spec_args
!= error_mark_node
)
22010 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22011 INNERMOST_TEMPLATE_ARGS (spec_args
),
22012 tmpl
, tf_none
, false, false);
22014 pop_tinst_level ();
22016 if (spec_args
== error_mark_node
22017 /* We only need to check the innermost arguments; the other
22018 arguments will always agree. */
22019 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22020 INNERMOST_TEMPLATE_ARGS (args
)))
22023 /* Now that we have bindings for all of the template arguments,
22024 ensure that the arguments deduced for the template template
22025 parameters have compatible template parameter lists. See the use
22026 of template_template_parm_bindings_ok_p in fn_type_unification
22027 for more information. */
22028 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22031 return deduced_args
;
22034 // Compare two function templates T1 and T2 by deducing bindings
22035 // from one against the other. If both deductions succeed, compare
22036 // constraints to see which is more constrained.
22038 more_specialized_inst (tree t1
, tree t2
)
22043 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22049 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22055 // If both deductions succeed, then one may be more constrained.
22056 if (count
== 2 && fate
== 0)
22057 fate
= more_constrained (t1
, t2
);
22062 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22063 Return the TREE_LIST node with the most specialized template, if
22064 any. If there is no most specialized template, the error_mark_node
22067 Note that this function does not look at, or modify, the
22068 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22069 returned is one of the elements of INSTANTIATIONS, callers may
22070 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22071 and retrieve it from the value returned. */
22074 most_specialized_instantiation (tree templates
)
22078 ++processing_template_decl
;
22081 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22083 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22084 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22089 /* Equally specialized, move to next function. If there
22090 is no next function, nothing's most specialized. */
22091 fn
= TREE_CHAIN (fn
);
22099 /* Now verify that champ is better than everything earlier in the
22100 instantiation list. */
22101 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22102 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22109 processing_template_decl
--;
22112 return error_mark_node
;
22117 /* If DECL is a specialization of some template, return the most
22118 general such template. Otherwise, returns NULL_TREE.
22120 For example, given:
22122 template <class T> struct S { template <class U> void f(U); };
22124 if TMPL is `template <class U> void S<int>::f(U)' this will return
22125 the full template. This function will not trace past partial
22126 specializations, however. For example, given in addition:
22128 template <class T> struct S<T*> { template <class U> void f(U); };
22130 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22131 `template <class T> template <class U> S<T*>::f(U)'. */
22134 most_general_template (tree decl
)
22136 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22138 if (tree tinfo
= get_template_info (decl
))
22139 decl
= TI_TEMPLATE (tinfo
);
22140 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22141 template friend, or a FIELD_DECL for a capture pack. */
22142 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22146 /* Look for more and more general templates. */
22147 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22149 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22150 (See cp-tree.h for details.) */
22151 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22154 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22155 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22156 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22159 /* Stop if we run into an explicitly specialized class template. */
22160 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22161 && DECL_CONTEXT (decl
)
22162 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22165 decl
= DECL_TI_TEMPLATE (decl
);
22171 /* Return the most specialized of the template partial specializations
22172 which can produce TARGET, a specialization of some class or variable
22173 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22174 a TEMPLATE_DECL node corresponding to the partial specialization, while
22175 the TREE_PURPOSE is the set of template arguments that must be
22176 substituted into the template pattern in order to generate TARGET.
22178 If the choice of partial specialization is ambiguous, a diagnostic
22179 is issued, and the error_mark_node is returned. If there are no
22180 partial specializations matching TARGET, then NULL_TREE is
22181 returned, indicating that the primary template should be used. */
22184 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22186 tree list
= NULL_TREE
;
22191 tree outer_args
= NULL_TREE
;
22194 if (TYPE_P (target
))
22196 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22197 tmpl
= TI_TEMPLATE (tinfo
);
22198 args
= TI_ARGS (tinfo
);
22200 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22202 tmpl
= TREE_OPERAND (target
, 0);
22203 args
= TREE_OPERAND (target
, 1);
22205 else if (VAR_P (target
))
22207 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22208 tmpl
= TI_TEMPLATE (tinfo
);
22209 args
= TI_ARGS (tinfo
);
22212 gcc_unreachable ();
22214 tree main_tmpl
= most_general_template (tmpl
);
22216 /* For determining which partial specialization to use, only the
22217 innermost args are interesting. */
22218 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22220 outer_args
= strip_innermost_template_args (args
, 1);
22221 args
= INNERMOST_TEMPLATE_ARGS (args
);
22224 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22227 tree spec_tmpl
= TREE_VALUE (t
);
22231 /* Substitute in the template args from the enclosing class. */
22232 ++processing_template_decl
;
22233 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22234 --processing_template_decl
;
22237 if (spec_tmpl
== error_mark_node
)
22238 return error_mark_node
;
22240 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22244 spec_args
= add_to_template_args (outer_args
, spec_args
);
22246 /* Keep the candidate only if the constraints are satisfied,
22247 or if we're not compiling with concepts. */
22249 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22251 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22252 TREE_TYPE (list
) = TREE_TYPE (t
);
22260 ambiguous_p
= false;
22263 t
= TREE_CHAIN (t
);
22264 for (; t
; t
= TREE_CHAIN (t
))
22266 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22273 t
= TREE_CHAIN (t
);
22276 ambiguous_p
= true;
22285 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22287 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22290 ambiguous_p
= true;
22298 char *spaces
= NULL
;
22299 if (!(complain
& tf_error
))
22300 return error_mark_node
;
22301 if (TYPE_P (target
))
22302 error ("ambiguous template instantiation for %q#T", target
);
22304 error ("ambiguous template instantiation for %q#D", target
);
22305 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22306 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22308 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22309 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22310 "%s %#qS", spaces
? spaces
: str
, subst
);
22311 spaces
= spaces
? spaces
: get_spaces (str
);
22314 return error_mark_node
;
22320 /* Explicitly instantiate DECL. */
22323 do_decl_instantiation (tree decl
, tree storage
)
22325 tree result
= NULL_TREE
;
22328 if (!decl
|| decl
== error_mark_node
)
22329 /* An error occurred, for which grokdeclarator has already issued
22330 an appropriate message. */
22332 else if (! DECL_LANG_SPECIFIC (decl
))
22334 error ("explicit instantiation of non-template %q#D", decl
);
22338 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22339 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22341 if (VAR_P (decl
) && !var_templ
)
22343 /* There is an asymmetry here in the way VAR_DECLs and
22344 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22345 the latter, the DECL we get back will be marked as a
22346 template instantiation, and the appropriate
22347 DECL_TEMPLATE_INFO will be set up. This does not happen for
22348 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22349 should handle VAR_DECLs as it currently handles
22351 if (!DECL_CLASS_SCOPE_P (decl
))
22353 error ("%qD is not a static data member of a class template", decl
);
22356 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22357 if (!result
|| !VAR_P (result
))
22359 error ("no matching template for %qD found", decl
);
22362 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22364 error ("type %qT for explicit instantiation %qD does not match "
22365 "declared type %qT", TREE_TYPE (result
), decl
,
22370 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22372 error ("explicit instantiation of %q#D", decl
);
22378 /* Check for various error cases. Note that if the explicit
22379 instantiation is valid the RESULT will currently be marked as an
22380 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22381 until we get here. */
22383 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22385 /* DR 259 [temp.spec].
22387 Both an explicit instantiation and a declaration of an explicit
22388 specialization shall not appear in a program unless the explicit
22389 instantiation follows a declaration of the explicit specialization.
22391 For a given set of template parameters, if an explicit
22392 instantiation of a template appears after a declaration of an
22393 explicit specialization for that template, the explicit
22394 instantiation has no effect. */
22397 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22401 No program shall explicitly instantiate any template more
22404 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22405 the first instantiation was `extern' and the second is not,
22406 and EXTERN_P for the opposite case. */
22407 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22408 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22409 /* If an "extern" explicit instantiation follows an ordinary
22410 explicit instantiation, the template is instantiated. */
22414 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22416 error ("no matching template for %qD found", result
);
22419 else if (!DECL_TEMPLATE_INFO (result
))
22421 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22425 if (storage
== NULL_TREE
)
22427 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22429 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22430 pedwarn (input_location
, OPT_Wpedantic
,
22431 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22436 error ("storage class %qD applied to template instantiation", storage
);
22438 check_explicit_instantiation_namespace (result
);
22439 mark_decl_instantiated (result
, extern_p
);
22441 instantiate_decl (result
, /*defer_ok=*/true,
22442 /*expl_inst_class_mem_p=*/false);
22446 mark_class_instantiated (tree t
, int extern_p
)
22448 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22449 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22450 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22451 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22454 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22455 rest_of_type_compilation (t
, 1);
22459 /* Called from do_type_instantiation through binding_table_foreach to
22460 do recursive instantiation for the type bound in ENTRY. */
22462 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22464 tree storage
= *(tree
*) data
;
22466 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22467 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22468 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22471 /* Perform an explicit instantiation of template class T. STORAGE, if
22472 non-null, is the RID for extern, inline or static. COMPLAIN is
22473 nonzero if this is called from the parser, zero if called recursively,
22474 since the standard is unclear (as detailed below). */
22477 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22482 int previous_instantiation_extern_p
= 0;
22484 if (TREE_CODE (t
) == TYPE_DECL
)
22487 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22490 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22492 error ("explicit instantiation of non-class template %qD", tmpl
);
22494 error ("explicit instantiation of non-template type %qT", t
);
22500 if (!COMPLETE_TYPE_P (t
))
22502 if (complain
& tf_error
)
22503 error ("explicit instantiation of %q#T before definition of template",
22508 if (storage
!= NULL_TREE
)
22510 if (!in_system_header_at (input_location
))
22512 if (storage
== ridpointers
[(int) RID_EXTERN
])
22514 if (cxx_dialect
== cxx98
)
22515 pedwarn (input_location
, OPT_Wpedantic
,
22516 "ISO C++ 1998 forbids the use of %<extern%> on "
22517 "explicit instantiations");
22520 pedwarn (input_location
, OPT_Wpedantic
,
22521 "ISO C++ forbids the use of %qE"
22522 " on explicit instantiations", storage
);
22525 if (storage
== ridpointers
[(int) RID_INLINE
])
22527 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22529 else if (storage
== ridpointers
[(int) RID_STATIC
])
22533 error ("storage class %qD applied to template instantiation",
22539 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22541 /* DR 259 [temp.spec].
22543 Both an explicit instantiation and a declaration of an explicit
22544 specialization shall not appear in a program unless the explicit
22545 instantiation follows a declaration of the explicit specialization.
22547 For a given set of template parameters, if an explicit
22548 instantiation of a template appears after a declaration of an
22549 explicit specialization for that template, the explicit
22550 instantiation has no effect. */
22553 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22557 No program shall explicitly instantiate any template more
22560 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22561 instantiation was `extern'. If EXTERN_P then the second is.
22562 These cases are OK. */
22563 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22565 if (!previous_instantiation_extern_p
&& !extern_p
22566 && (complain
& tf_error
))
22567 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22569 /* If we've already instantiated the template, just return now. */
22570 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22574 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22575 mark_class_instantiated (t
, extern_p
);
22580 /* In contrast to implicit instantiation, where only the
22581 declarations, and not the definitions, of members are
22582 instantiated, we have here:
22586 The explicit instantiation of a class template specialization
22587 implies the instantiation of all of its members not
22588 previously explicitly specialized in the translation unit
22589 containing the explicit instantiation.
22591 Of course, we can't instantiate member template classes, since we
22592 don't have any arguments for them. Note that the standard is
22593 unclear on whether the instantiation of the members are
22594 *explicit* instantiations or not. However, the most natural
22595 interpretation is that it should be an explicit
22597 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22599 || (TREE_CODE (fld
) == FUNCTION_DECL
22601 && user_provided_p (fld
)))
22602 && DECL_TEMPLATE_INSTANTIATION (fld
))
22604 mark_decl_instantiated (fld
, extern_p
);
22606 instantiate_decl (fld
, /*defer_ok=*/true,
22607 /*expl_inst_class_mem_p=*/true);
22610 if (CLASSTYPE_NESTED_UTDS (t
))
22611 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22612 bt_instantiate_type_proc
, &storage
);
22615 /* Given a function DECL, which is a specialization of TMPL, modify
22616 DECL to be a re-instantiation of TMPL with the same template
22617 arguments. TMPL should be the template into which tsubst'ing
22618 should occur for DECL, not the most general template.
22620 One reason for doing this is a scenario like this:
22623 void f(const T&, int i);
22625 void g() { f(3, 7); }
22628 void f(const T& t, const int i) { }
22630 Note that when the template is first instantiated, with
22631 instantiate_template, the resulting DECL will have no name for the
22632 first parameter, and the wrong type for the second. So, when we go
22633 to instantiate the DECL, we regenerate it. */
22636 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22638 /* The arguments used to instantiate DECL, from the most general
22642 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22644 /* Make sure that we can see identifiers, and compute access
22646 push_access_scope (decl
);
22648 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22656 args_depth
= TMPL_ARGS_DEPTH (args
);
22657 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22658 if (args_depth
> parms_depth
)
22659 args
= get_innermost_template_args (args
, parms_depth
);
22661 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22662 args
, tf_error
, NULL_TREE
,
22663 /*defer_ok*/false);
22664 if (specs
&& specs
!= error_mark_node
)
22665 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22668 /* Merge parameter declarations. */
22669 decl_parm
= skip_artificial_parms_for (decl
,
22670 DECL_ARGUMENTS (decl
));
22672 = skip_artificial_parms_for (code_pattern
,
22673 DECL_ARGUMENTS (code_pattern
));
22674 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22679 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22680 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22681 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22683 parm_type
= type_decays_to (parm_type
);
22684 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22685 TREE_TYPE (decl_parm
) = parm_type
;
22686 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22687 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22689 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22690 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22692 decl_parm
= DECL_CHAIN (decl_parm
);
22693 pattern_parm
= DECL_CHAIN (pattern_parm
);
22695 /* Merge any parameters that match with the function parameter
22697 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22700 tree expanded_types
;
22701 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22702 the parameters in this function parameter pack. */
22703 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22704 args
, tf_error
, NULL_TREE
);
22705 len
= TREE_VEC_LENGTH (expanded_types
);
22706 for (i
= 0; i
< len
; i
++)
22711 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22712 /* Rename the parameter to include the index. */
22713 DECL_NAME (decl_parm
) =
22714 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22715 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22716 parm_type
= type_decays_to (parm_type
);
22717 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22718 TREE_TYPE (decl_parm
) = parm_type
;
22719 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22720 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22722 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22723 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22725 decl_parm
= DECL_CHAIN (decl_parm
);
22728 /* Merge additional specifiers from the CODE_PATTERN. */
22729 if (DECL_DECLARED_INLINE_P (code_pattern
)
22730 && !DECL_DECLARED_INLINE_P (decl
))
22731 DECL_DECLARED_INLINE_P (decl
) = 1;
22733 else if (VAR_P (decl
))
22735 start_lambda_scope (decl
);
22736 DECL_INITIAL (decl
) =
22737 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22738 tf_error
, DECL_TI_TEMPLATE (decl
),
22739 /*integral_constant_expression_p=*/false);
22740 finish_lambda_scope ();
22741 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22742 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22743 tf_error
, DECL_TI_TEMPLATE (decl
));
22746 gcc_unreachable ();
22748 pop_access_scope (decl
);
22751 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22752 substituted to get DECL. */
22755 template_for_substitution (tree decl
)
22757 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22759 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22760 for the instantiation. This is not always the most general
22761 template. Consider, for example:
22764 struct S { template <class U> void f();
22765 template <> void f<int>(); };
22767 and an instantiation of S<double>::f<int>. We want TD to be the
22768 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22769 while (/* An instantiation cannot have a definition, so we need a
22770 more general template. */
22771 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22772 /* We must also deal with friend templates. Given:
22774 template <class T> struct S {
22775 template <class U> friend void f() {};
22778 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22779 so far as the language is concerned, but that's still
22780 where we get the pattern for the instantiation from. On
22781 other hand, if the definition comes outside the class, say:
22783 template <class T> struct S {
22784 template <class U> friend void f();
22786 template <class U> friend void f() {}
22788 we don't need to look any further. That's what the check for
22789 DECL_INITIAL is for. */
22790 || (TREE_CODE (decl
) == FUNCTION_DECL
22791 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22792 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22794 /* The present template, TD, should not be a definition. If it
22795 were a definition, we should be using it! Note that we
22796 cannot restructure the loop to just keep going until we find
22797 a template with a definition, since that might go too far if
22798 a specialization was declared, but not defined. */
22800 /* Fetch the more general template. */
22801 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22807 /* Returns true if we need to instantiate this template instance even if we
22808 know we aren't going to emit it. */
22811 always_instantiate_p (tree decl
)
22813 /* We always instantiate inline functions so that we can inline them. An
22814 explicit instantiation declaration prohibits implicit instantiation of
22815 non-inline functions. With high levels of optimization, we would
22816 normally inline non-inline functions -- but we're not allowed to do
22817 that for "extern template" functions. Therefore, we check
22818 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22819 return ((TREE_CODE (decl
) == FUNCTION_DECL
22820 && (DECL_DECLARED_INLINE_P (decl
)
22821 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22822 /* And we need to instantiate static data members so that
22823 their initializers are available in integral constant
22826 && decl_maybe_constant_var_p (decl
)));
22829 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22830 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22831 error, true otherwise. */
22834 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22836 tree fntype
, spec
, noex
, clone
;
22838 /* Don't instantiate a noexcept-specification from template context. */
22839 if (processing_template_decl
)
22842 if (DECL_CLONED_FUNCTION_P (fn
))
22843 fn
= DECL_CLONED_FUNCTION (fn
);
22844 fntype
= TREE_TYPE (fn
);
22845 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22847 if (!spec
|| !TREE_PURPOSE (spec
))
22850 noex
= TREE_PURPOSE (spec
);
22852 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22854 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22855 bool added
= false;
22856 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22857 spec
= get_defaulted_eh_spec (fn
, complain
);
22858 else if (!(added
= !fns
->add (fn
)))
22860 /* If hash_set::add returns true, the element was already there. */
22861 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22862 DECL_SOURCE_LOCATION (fn
));
22864 "exception specification of %qD depends on itself",
22866 spec
= noexcept_false_spec
;
22868 else if (push_tinst_level (fn
))
22870 push_access_scope (fn
);
22871 push_deferring_access_checks (dk_no_deferred
);
22872 input_location
= DECL_SOURCE_LOCATION (fn
);
22873 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22874 DEFERRED_NOEXCEPT_ARGS (noex
),
22875 tf_warning_or_error
, fn
,
22876 /*function_p=*/false,
22877 /*integral_constant_expression_p=*/true);
22878 pop_deferring_access_checks ();
22879 pop_access_scope (fn
);
22880 pop_tinst_level ();
22881 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22882 if (spec
== error_mark_node
)
22883 spec
= noexcept_false_spec
;
22886 spec
= noexcept_false_spec
;
22891 if (spec
== error_mark_node
)
22894 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22897 FOR_EACH_CLONE (clone
, fn
)
22899 if (TREE_TYPE (clone
) == fntype
)
22900 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22902 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22908 /* We're starting to process the function INST, an instantiation of PATTERN;
22909 add their parameters to local_specializations. */
22912 register_parameter_specializations (tree pattern
, tree inst
)
22914 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
22915 tree spec_parm
= DECL_ARGUMENTS (inst
);
22916 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
22918 register_local_specialization (spec_parm
, tmpl_parm
);
22919 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
22920 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
22922 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22924 if (!DECL_PACK_P (tmpl_parm
))
22926 register_local_specialization (spec_parm
, tmpl_parm
);
22927 spec_parm
= DECL_CHAIN (spec_parm
);
22931 /* Register the (value) argument pack as a specialization of
22932 TMPL_PARM, then move on. */
22933 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22934 register_local_specialization (argpack
, tmpl_parm
);
22937 gcc_assert (!spec_parm
);
22940 /* Produce the definition of D, a _DECL generated from a template. If
22941 DEFER_OK is true, then we don't have to actually do the
22942 instantiation now; we just have to do it sometime. Normally it is
22943 an error if this is an explicit instantiation but D is undefined.
22944 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22945 instantiated class template. */
22948 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
22950 tree tmpl
= DECL_TI_TEMPLATE (d
);
22957 bool pattern_defined
;
22958 location_t saved_loc
= input_location
;
22959 int saved_unevaluated_operand
= cp_unevaluated_operand
;
22960 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22964 /* This function should only be used to instantiate templates for
22965 functions and static member variables. */
22966 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
22968 /* A concept is never instantiated. */
22969 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
22971 /* Variables are never deferred; if instantiation is required, they
22972 are instantiated right away. That allows for better code in the
22973 case that an expression refers to the value of the variable --
22974 if the variable has a constant value the referring expression can
22975 take advantage of that fact. */
22979 /* Don't instantiate cloned functions. Instead, instantiate the
22980 functions they cloned. */
22981 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
22982 d
= DECL_CLONED_FUNCTION (d
);
22984 if (DECL_TEMPLATE_INSTANTIATED (d
)
22985 || (TREE_CODE (d
) == FUNCTION_DECL
22986 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
22987 || DECL_TEMPLATE_SPECIALIZATION (d
))
22988 /* D has already been instantiated or explicitly specialized, so
22989 there's nothing for us to do here.
22991 It might seem reasonable to check whether or not D is an explicit
22992 instantiation, and, if so, stop here. But when an explicit
22993 instantiation is deferred until the end of the compilation,
22994 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22995 the instantiation. */
22998 /* Check to see whether we know that this template will be
22999 instantiated in some other file, as with "extern template"
23001 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23003 /* In general, we do not instantiate such templates. */
23004 if (external_p
&& !always_instantiate_p (d
))
23007 gen_tmpl
= most_general_template (tmpl
);
23008 gen_args
= DECL_TI_ARGS (d
);
23010 if (tmpl
!= gen_tmpl
)
23011 /* We should already have the extra args. */
23012 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23013 == TMPL_ARGS_DEPTH (gen_args
));
23014 /* And what's in the hash table should match D. */
23015 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23016 || spec
== NULL_TREE
);
23018 /* This needs to happen before any tsubsting. */
23019 if (! push_tinst_level (d
))
23022 timevar_push (TV_TEMPLATE_INST
);
23024 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23025 for the instantiation. */
23026 td
= template_for_substitution (d
);
23031 /* Look up an explicit specialization, if any. */
23032 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23033 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23034 if (elt
&& elt
!= error_mark_node
)
23036 td
= TREE_VALUE (elt
);
23037 args
= TREE_PURPOSE (elt
);
23041 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23043 /* We should never be trying to instantiate a member of a class
23044 template or partial specialization. */
23045 gcc_assert (d
!= code_pattern
);
23047 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23048 || DECL_TEMPLATE_SPECIALIZATION (td
))
23049 /* In the case of a friend template whose definition is provided
23050 outside the class, we may have too many arguments. Drop the
23051 ones we don't need. The same is true for specializations. */
23052 args
= get_innermost_template_args
23053 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23055 if (TREE_CODE (d
) == FUNCTION_DECL
)
23057 deleted_p
= DECL_DELETED_FN (code_pattern
);
23058 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23059 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23060 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23066 if (DECL_CLASS_SCOPE_P (code_pattern
))
23067 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23068 || DECL_INLINE_VAR_P (code_pattern
));
23070 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23073 /* We may be in the middle of deferred access check. Disable it now. */
23074 push_deferring_access_checks (dk_no_deferred
);
23076 /* Unless an explicit instantiation directive has already determined
23077 the linkage of D, remember that a definition is available for
23079 if (pattern_defined
23080 && !DECL_INTERFACE_KNOWN (d
)
23081 && !DECL_NOT_REALLY_EXTERN (d
))
23082 mark_definable (d
);
23084 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23085 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23086 input_location
= DECL_SOURCE_LOCATION (d
);
23088 /* If D is a member of an explicitly instantiated class template,
23089 and no definition is available, treat it like an implicit
23091 if (!pattern_defined
&& expl_inst_class_mem_p
23092 && DECL_EXPLICIT_INSTANTIATION (d
))
23094 /* Leave linkage flags alone on instantiations with anonymous
23096 if (TREE_PUBLIC (d
))
23098 DECL_NOT_REALLY_EXTERN (d
) = 0;
23099 DECL_INTERFACE_KNOWN (d
) = 0;
23101 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23104 /* Defer all other templates, unless we have been explicitly
23105 forbidden from doing so. */
23106 if (/* If there is no definition, we cannot instantiate the
23109 /* If it's OK to postpone instantiation, do so. */
23111 /* If this is a static data member that will be defined
23112 elsewhere, we don't want to instantiate the entire data
23113 member, but we do want to instantiate the initializer so that
23114 we can substitute that elsewhere. */
23115 || (external_p
&& VAR_P (d
))
23116 /* Handle here a deleted function too, avoid generating
23117 its body (c++/61080). */
23120 /* The definition of the static data member is now required so
23121 we must substitute the initializer. */
23123 && !DECL_INITIAL (d
)
23124 && DECL_INITIAL (code_pattern
))
23128 bool const_init
= false;
23129 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23131 ns
= decl_namespace_context (d
);
23132 push_nested_namespace (ns
);
23134 push_nested_class (DECL_CONTEXT (d
));
23135 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23137 tf_warning_or_error
, NULL_TREE
,
23138 /*integral_constant_expression_p=*/false);
23139 /* If instantiating the initializer involved instantiating this
23140 again, don't call cp_finish_decl twice. */
23141 if (!DECL_INITIAL (d
))
23143 /* Make sure the initializer is still constant, in case of
23144 circular dependency (template/instantiate6.C). */
23146 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23147 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23148 /*asmspec_tree=*/NULL_TREE
,
23149 LOOKUP_ONLYCONVERTING
);
23152 pop_nested_class ();
23153 pop_nested_namespace (ns
);
23156 /* We restore the source position here because it's used by
23157 add_pending_template. */
23158 input_location
= saved_loc
;
23160 if (at_eof
&& !pattern_defined
23161 && DECL_EXPLICIT_INSTANTIATION (d
)
23162 && DECL_NOT_REALLY_EXTERN (d
))
23165 The definition of a non-exported function template, a
23166 non-exported member function template, or a non-exported
23167 member function or static data member of a class template
23168 shall be present in every translation unit in which it is
23169 explicitly instantiated. */
23170 permerror (input_location
, "explicit instantiation of %qD "
23171 "but no definition available", d
);
23173 /* If we're in unevaluated context, we just wanted to get the
23174 constant value; this isn't an odr use, so don't queue
23175 a full instantiation. */
23176 if (cp_unevaluated_operand
!= 0)
23178 /* ??? Historically, we have instantiated inline functions, even
23179 when marked as "extern template". */
23180 if (!(external_p
&& VAR_P (d
)))
23181 add_pending_template (d
);
23184 /* Tell the repository that D is available in this translation unit
23185 -- and see if it is supposed to be instantiated here. */
23186 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23188 /* In a PCH file, despite the fact that the repository hasn't
23189 requested instantiation in the PCH it is still possible that
23190 an instantiation will be required in a file that includes the
23193 add_pending_template (d
);
23194 /* Instantiate inline functions so that the inliner can do its
23195 job, even though we'll not be emitting a copy of this
23197 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23201 bool push_to_top
, nested
;
23203 fn_context
= decl_function_context (d
);
23204 nested
= current_function_decl
!= NULL_TREE
;
23205 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23207 vec
<tree
> omp_privatization_save
;
23209 save_omp_privatization_clauses (omp_privatization_save
);
23212 push_to_top_level ();
23215 push_function_context ();
23216 cp_unevaluated_operand
= 0;
23217 c_inhibit_evaluation_warnings
= 0;
23220 /* Mark D as instantiated so that recursive calls to
23221 instantiate_decl do not try to instantiate it again. */
23222 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23224 /* Regenerate the declaration in case the template has been modified
23225 by a subsequent redeclaration. */
23226 regenerate_decl_from_template (d
, td
, args
);
23228 /* We already set the file and line above. Reset them now in case
23229 they changed as a result of calling regenerate_decl_from_template. */
23230 input_location
= DECL_SOURCE_LOCATION (d
);
23235 bool const_init
= false;
23237 /* Clear out DECL_RTL; whatever was there before may not be right
23238 since we've reset the type of the declaration. */
23239 SET_DECL_RTL (d
, NULL
);
23240 DECL_IN_AGGR_P (d
) = 0;
23242 /* The initializer is placed in DECL_INITIAL by
23243 regenerate_decl_from_template so we don't need to
23244 push/pop_access_scope again here. Pull it out so that
23245 cp_finish_decl can process it. */
23246 init
= DECL_INITIAL (d
);
23247 DECL_INITIAL (d
) = NULL_TREE
;
23248 DECL_INITIALIZED_P (d
) = 0;
23250 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23251 initializer. That function will defer actual emission until
23252 we have a chance to determine linkage. */
23253 DECL_EXTERNAL (d
) = 0;
23255 /* Enter the scope of D so that access-checking works correctly. */
23256 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23258 push_nested_class (DECL_CONTEXT (d
));
23260 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23261 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23264 pop_nested_class ();
23266 if (variable_template_p (gen_tmpl
))
23267 note_variable_template_instantiation (d
);
23269 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23270 synthesize_method (d
);
23271 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23273 /* Set up the list of local specializations. */
23274 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23275 tree block
= NULL_TREE
;
23277 /* Set up context. */
23278 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23279 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23280 block
= push_stmt_list ();
23282 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23284 /* Some typedefs referenced from within the template code need to be
23285 access checked at template instantiation time, i.e now. These
23286 types were added to the template at parsing time. Let's get those
23287 and perform the access checks then. */
23288 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23291 /* Create substitution entries for the parameters. */
23292 register_parameter_specializations (code_pattern
, d
);
23294 /* Substitute into the body of the function. */
23295 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23296 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23297 tf_warning_or_error
, tmpl
);
23300 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23301 tf_warning_or_error
, tmpl
,
23302 /*integral_constant_expression_p=*/false);
23304 /* Set the current input_location to the end of the function
23305 so that finish_function knows where we are. */
23307 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23309 /* Remember if we saw an infinite loop in the template. */
23310 current_function_infinite_loop
23311 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23314 /* Finish the function. */
23315 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23316 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23317 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23320 d
= finish_function (/*inline_p=*/false);
23321 expand_or_defer_fn (d
);
23324 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23325 cp_check_omp_declare_reduction (d
);
23328 /* We're not deferring instantiation any more. */
23329 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23332 pop_from_top_level ();
23334 pop_function_context ();
23337 restore_omp_privatization_clauses (omp_privatization_save
);
23340 pop_deferring_access_checks ();
23341 timevar_pop (TV_TEMPLATE_INST
);
23342 pop_tinst_level ();
23343 input_location
= saved_loc
;
23344 cp_unevaluated_operand
= saved_unevaluated_operand
;
23345 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23350 /* Run through the list of templates that we wish we could
23351 instantiate, and instantiate any we can. RETRIES is the
23352 number of times we retry pending template instantiation. */
23355 instantiate_pending_templates (int retries
)
23358 location_t saved_loc
= input_location
;
23360 /* Instantiating templates may trigger vtable generation. This in turn
23361 may require further template instantiations. We place a limit here
23362 to avoid infinite loop. */
23363 if (pending_templates
&& retries
>= max_tinst_depth
)
23365 tree decl
= pending_templates
->tinst
->decl
;
23367 fatal_error (input_location
,
23368 "template instantiation depth exceeds maximum of %d"
23369 " instantiating %q+D, possibly from virtual table generation"
23370 " (use -ftemplate-depth= to increase the maximum)",
23371 max_tinst_depth
, decl
);
23372 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23373 /* Pretend that we defined it. */
23374 DECL_INITIAL (decl
) = error_mark_node
;
23380 struct pending_template
**t
= &pending_templates
;
23381 struct pending_template
*last
= NULL
;
23385 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23386 bool complete
= false;
23388 if (TYPE_P (instantiation
))
23390 if (!COMPLETE_TYPE_P (instantiation
))
23392 instantiate_class_template (instantiation
);
23393 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23394 for (tree fld
= TYPE_FIELDS (instantiation
);
23395 fld
; fld
= TREE_CHAIN (fld
))
23397 || (TREE_CODE (fld
) == FUNCTION_DECL
23398 && !DECL_ARTIFICIAL (fld
)))
23399 && DECL_TEMPLATE_INSTANTIATION (fld
))
23400 instantiate_decl (fld
,
23401 /*defer_ok=*/false,
23402 /*expl_inst_class_mem_p=*/false);
23404 if (COMPLETE_TYPE_P (instantiation
))
23408 complete
= COMPLETE_TYPE_P (instantiation
);
23412 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23413 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23416 = instantiate_decl (instantiation
,
23417 /*defer_ok=*/false,
23418 /*expl_inst_class_mem_p=*/false);
23419 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23423 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23424 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23428 /* If INSTANTIATION has been instantiated, then we don't
23429 need to consider it again in the future. */
23437 current_tinst_level
= NULL
;
23439 last_pending_template
= last
;
23441 while (reconsider
);
23443 input_location
= saved_loc
;
23446 /* Substitute ARGVEC into T, which is a list of initializers for
23447 either base class or a non-static data member. The TREE_PURPOSEs
23448 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23449 instantiate_decl. */
23452 tsubst_initializer_list (tree t
, tree argvec
)
23454 tree inits
= NULL_TREE
;
23456 for (; t
; t
= TREE_CHAIN (t
))
23460 tree expanded_bases
= NULL_TREE
;
23461 tree expanded_arguments
= NULL_TREE
;
23464 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23469 /* Expand the base class expansion type into separate base
23471 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23472 tf_warning_or_error
,
23474 if (expanded_bases
== error_mark_node
)
23477 /* We'll be building separate TREE_LISTs of arguments for
23479 len
= TREE_VEC_LENGTH (expanded_bases
);
23480 expanded_arguments
= make_tree_vec (len
);
23481 for (i
= 0; i
< len
; i
++)
23482 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23484 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23485 expand each argument in the TREE_VALUE of t. */
23486 expr
= make_node (EXPR_PACK_EXPANSION
);
23487 PACK_EXPANSION_LOCAL_P (expr
) = true;
23488 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23489 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23491 if (TREE_VALUE (t
) == void_type_node
)
23492 /* VOID_TYPE_NODE is used to indicate
23493 value-initialization. */
23495 for (i
= 0; i
< len
; i
++)
23496 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23500 /* Substitute parameter packs into each argument in the
23502 in_base_initializer
= 1;
23503 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23505 tree expanded_exprs
;
23507 /* Expand the argument. */
23508 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23510 = tsubst_pack_expansion (expr
, argvec
,
23511 tf_warning_or_error
,
23513 if (expanded_exprs
== error_mark_node
)
23516 /* Prepend each of the expanded expressions to the
23517 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23518 for (i
= 0; i
< len
; i
++)
23520 TREE_VEC_ELT (expanded_arguments
, i
) =
23521 tree_cons (NULL_TREE
,
23522 TREE_VEC_ELT (expanded_exprs
, i
),
23523 TREE_VEC_ELT (expanded_arguments
, i
));
23526 in_base_initializer
= 0;
23528 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23529 since we built them backwards. */
23530 for (i
= 0; i
< len
; i
++)
23532 TREE_VEC_ELT (expanded_arguments
, i
) =
23533 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23538 for (i
= 0; i
< len
; ++i
)
23540 if (expanded_bases
)
23542 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23543 decl
= expand_member_init (decl
);
23544 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23549 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23550 tf_warning_or_error
, NULL_TREE
);
23552 decl
= expand_member_init (decl
);
23553 if (decl
&& !DECL_P (decl
))
23554 in_base_initializer
= 1;
23556 init
= TREE_VALUE (t
);
23558 if (init
!= void_type_node
)
23559 init
= tsubst_expr (init
, argvec
,
23560 tf_warning_or_error
, NULL_TREE
,
23561 /*integral_constant_expression_p=*/false);
23562 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23563 /* If we had an initializer but it instantiated to nothing,
23564 value-initialize the object. This will only occur when
23565 the initializer was a pack expansion where the parameter
23566 packs used in that expansion were of length zero. */
23567 init
= void_type_node
;
23568 in_base_initializer
= 0;
23573 init
= build_tree_list (decl
, init
);
23574 TREE_CHAIN (init
) = inits
;
23582 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23585 set_current_access_from_decl (tree decl
)
23587 if (TREE_PRIVATE (decl
))
23588 current_access_specifier
= access_private_node
;
23589 else if (TREE_PROTECTED (decl
))
23590 current_access_specifier
= access_protected_node
;
23592 current_access_specifier
= access_public_node
;
23595 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23596 is the instantiation (which should have been created with
23597 start_enum) and ARGS are the template arguments to use. */
23600 tsubst_enum (tree tag
, tree newtag
, tree args
)
23604 if (SCOPED_ENUM_P (newtag
))
23605 begin_scope (sk_scoped_enum
, newtag
);
23607 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23612 decl
= TREE_VALUE (e
);
23613 /* Note that in a template enum, the TREE_VALUE is the
23614 CONST_DECL, not the corresponding INTEGER_CST. */
23615 value
= tsubst_expr (DECL_INITIAL (decl
),
23616 args
, tf_warning_or_error
, NULL_TREE
,
23617 /*integral_constant_expression_p=*/true);
23619 /* Give this enumeration constant the correct access. */
23620 set_current_access_from_decl (decl
);
23622 /* Actually build the enumerator itself. Here we're assuming that
23623 enumerators can't have dependent attributes. */
23624 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23625 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23628 if (SCOPED_ENUM_P (newtag
))
23631 finish_enum_value_list (newtag
);
23632 finish_enum (newtag
);
23634 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23635 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23638 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23639 its type -- but without substituting the innermost set of template
23640 arguments. So, innermost set of template parameters will appear in
23644 get_mostly_instantiated_function_type (tree decl
)
23646 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23647 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23650 /* Return truthvalue if we're processing a template different from
23651 the last one involved in diagnostics. */
23653 problematic_instantiation_changed (void)
23655 return current_tinst_level
!= last_error_tinst_level
;
23658 /* Remember current template involved in diagnostics. */
23660 record_last_problematic_instantiation (void)
23662 last_error_tinst_level
= current_tinst_level
;
23665 struct tinst_level
*
23666 current_instantiation (void)
23668 return current_tinst_level
;
23671 /* Return TRUE if current_function_decl is being instantiated, false
23675 instantiating_current_function_p (void)
23677 return (current_instantiation ()
23678 && current_instantiation ()->decl
== current_function_decl
);
23681 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23682 type. Return false for ok, true for disallowed. Issue error and
23683 inform messages under control of COMPLAIN. */
23686 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23688 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23690 else if (POINTER_TYPE_P (type
))
23692 else if (TYPE_PTRMEM_P (type
))
23694 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23696 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23698 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23700 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23702 /* A bound template template parm could later be instantiated to have a valid
23703 nontype parm type via an alias template. */
23704 else if (cxx_dialect
>= cxx11
23705 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23708 if (complain
& tf_error
)
23710 if (type
== error_mark_node
)
23711 inform (input_location
, "invalid template non-type parameter");
23713 error ("%q#T is not a valid type for a template non-type parameter",
23719 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23720 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23723 dependent_type_p_r (tree type
)
23729 A type is dependent if it is:
23731 -- a template parameter. Template template parameters are types
23732 for us (since TYPE_P holds true for them) so we handle
23734 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23735 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23737 /* -- a qualified-id with a nested-name-specifier which contains a
23738 class-name that names a dependent type or whose unqualified-id
23739 names a dependent type. */
23740 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23743 /* An alias template specialization can be dependent even if the
23744 resulting type is not. */
23745 if (dependent_alias_template_spec_p (type
))
23748 /* -- a cv-qualified type where the cv-unqualified type is
23750 No code is necessary for this bullet; the code below handles
23751 cv-qualified types, and we don't want to strip aliases with
23752 TYPE_MAIN_VARIANT because of DR 1558. */
23753 /* -- a compound type constructed from any dependent type. */
23754 if (TYPE_PTRMEM_P (type
))
23755 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23756 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23758 else if (TYPE_PTR_P (type
)
23759 || TREE_CODE (type
) == REFERENCE_TYPE
)
23760 return dependent_type_p (TREE_TYPE (type
));
23761 else if (TREE_CODE (type
) == FUNCTION_TYPE
23762 || TREE_CODE (type
) == METHOD_TYPE
)
23766 if (dependent_type_p (TREE_TYPE (type
)))
23768 for (arg_type
= TYPE_ARG_TYPES (type
);
23770 arg_type
= TREE_CHAIN (arg_type
))
23771 if (dependent_type_p (TREE_VALUE (arg_type
)))
23773 if (cxx_dialect
>= cxx17
)
23774 /* A value-dependent noexcept-specifier makes the type dependent. */
23775 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
23776 if (tree noex
= TREE_PURPOSE (spec
))
23777 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23778 affect overload resolution and treating it as dependent breaks
23780 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
23781 && value_dependent_expression_p (noex
))
23785 /* -- an array type constructed from any dependent type or whose
23786 size is specified by a constant expression that is
23789 We checked for type- and value-dependence of the bounds in
23790 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23791 if (TREE_CODE (type
) == ARRAY_TYPE
)
23793 if (TYPE_DOMAIN (type
)
23794 && dependent_type_p (TYPE_DOMAIN (type
)))
23796 return dependent_type_p (TREE_TYPE (type
));
23799 /* -- a template-id in which either the template name is a template
23801 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23803 /* ... or any of the template arguments is a dependent type or
23804 an expression that is type-dependent or value-dependent. */
23805 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23806 && (any_dependent_template_arguments_p
23807 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23810 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23811 dependent; if the argument of the `typeof' expression is not
23812 type-dependent, then it should already been have resolved. */
23813 if (TREE_CODE (type
) == TYPEOF_TYPE
23814 || TREE_CODE (type
) == DECLTYPE_TYPE
23815 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23818 /* A template argument pack is dependent if any of its packed
23820 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23822 tree args
= ARGUMENT_PACK_ARGS (type
);
23823 int i
, len
= TREE_VEC_LENGTH (args
);
23824 for (i
= 0; i
< len
; ++i
)
23825 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23829 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23830 be template parameters. */
23831 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23834 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23837 /* The standard does not specifically mention types that are local
23838 to template functions or local classes, but they should be
23839 considered dependent too. For example:
23841 template <int I> void f() {
23846 The size of `E' cannot be known until the value of `I' has been
23847 determined. Therefore, `E' must be considered dependent. */
23848 scope
= TYPE_CONTEXT (type
);
23849 if (scope
&& TYPE_P (scope
))
23850 return dependent_type_p (scope
);
23851 /* Don't use type_dependent_expression_p here, as it can lead
23852 to infinite recursion trying to determine whether a lambda
23853 nested in a lambda is dependent (c++/47687). */
23854 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23855 && DECL_LANG_SPECIFIC (scope
)
23856 && DECL_TEMPLATE_INFO (scope
)
23857 && (any_dependent_template_arguments_p
23858 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23861 /* Other types are non-dependent. */
23865 /* Returns TRUE if TYPE is dependent, in the sense of
23866 [temp.dep.type]. Note that a NULL type is considered dependent. */
23869 dependent_type_p (tree type
)
23871 /* If there are no template parameters in scope, then there can't be
23872 any dependent types. */
23873 if (!processing_template_decl
)
23875 /* If we are not processing a template, then nobody should be
23876 providing us with a dependent type. */
23878 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23882 /* If the type is NULL, we have not computed a type for the entity
23883 in question; in that case, the type is dependent. */
23887 /* Erroneous types can be considered non-dependent. */
23888 if (type
== error_mark_node
)
23891 /* Getting here with global_type_node means we improperly called this
23892 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23893 gcc_checking_assert (type
!= global_type_node
);
23895 /* If we have not already computed the appropriate value for TYPE,
23897 if (!TYPE_DEPENDENT_P_VALID (type
))
23899 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23900 TYPE_DEPENDENT_P_VALID (type
) = 1;
23903 return TYPE_DEPENDENT_P (type
);
23906 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23907 lookup. In other words, a dependent type that is not the current
23911 dependent_scope_p (tree scope
)
23913 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23914 && !currently_open_class (scope
));
23917 /* T is a SCOPE_REF; return whether we need to consider it
23918 instantiation-dependent so that we can check access at instantiation
23919 time even though we know which member it resolves to. */
23922 instantiation_dependent_scope_ref_p (tree t
)
23924 if (DECL_P (TREE_OPERAND (t
, 1))
23925 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23926 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23927 TREE_OPERAND (t
, 1)))
23933 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23934 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23937 /* Note that this predicate is not appropriate for general expressions;
23938 only constant expressions (that satisfy potential_constant_expression)
23939 can be tested for value dependence. */
23942 value_dependent_expression_p (tree expression
)
23944 if (!processing_template_decl
|| expression
== NULL_TREE
)
23947 /* A type-dependent expression is also value-dependent. */
23948 if (type_dependent_expression_p (expression
))
23951 switch (TREE_CODE (expression
))
23954 /* A dependent member function of the current instantiation. */
23955 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
23957 case FUNCTION_DECL
:
23958 /* A dependent member function of the current instantiation. */
23959 if (DECL_CLASS_SCOPE_P (expression
)
23960 && dependent_type_p (DECL_CONTEXT (expression
)))
23964 case IDENTIFIER_NODE
:
23965 /* A name that has not been looked up -- must be dependent. */
23968 case TEMPLATE_PARM_INDEX
:
23969 /* A non-type template parm. */
23973 /* A non-type template parm. */
23974 if (DECL_TEMPLATE_PARM_P (expression
))
23976 return value_dependent_expression_p (DECL_INITIAL (expression
));
23979 /* A constant with literal type and is initialized
23980 with an expression that is value-dependent. */
23981 if (DECL_DEPENDENT_INIT_P (expression
)
23982 /* FIXME cp_finish_decl doesn't fold reference initializers. */
23983 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
23985 if (DECL_HAS_VALUE_EXPR_P (expression
))
23987 tree value_expr
= DECL_VALUE_EXPR (expression
);
23988 if (value_dependent_expression_p (value_expr
))
23993 case DYNAMIC_CAST_EXPR
:
23994 case STATIC_CAST_EXPR
:
23995 case CONST_CAST_EXPR
:
23996 case REINTERPRET_CAST_EXPR
:
23998 case IMPLICIT_CONV_EXPR
:
23999 /* These expressions are value-dependent if the type to which
24000 the cast occurs is dependent or the expression being casted
24001 is value-dependent. */
24003 tree type
= TREE_TYPE (expression
);
24005 if (dependent_type_p (type
))
24008 /* A functional cast has a list of operands. */
24009 expression
= TREE_OPERAND (expression
, 0);
24012 /* If there are no operands, it must be an expression such
24013 as "int()". This should not happen for aggregate types
24014 because it would form non-constant expressions. */
24015 gcc_assert (cxx_dialect
>= cxx11
24016 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24021 if (TREE_CODE (expression
) == TREE_LIST
)
24022 return any_value_dependent_elements_p (expression
);
24024 return value_dependent_expression_p (expression
);
24028 if (SIZEOF_EXPR_TYPE_P (expression
))
24029 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24033 /* A `sizeof' expression is value-dependent if the operand is
24034 type-dependent or is a pack expansion. */
24035 expression
= TREE_OPERAND (expression
, 0);
24036 if (PACK_EXPANSION_P (expression
))
24038 else if (TYPE_P (expression
))
24039 return dependent_type_p (expression
);
24040 return instantiation_dependent_uneval_expression_p (expression
);
24042 case AT_ENCODE_EXPR
:
24043 /* An 'encode' expression is value-dependent if the operand is
24045 expression
= TREE_OPERAND (expression
, 0);
24046 return dependent_type_p (expression
);
24048 case NOEXCEPT_EXPR
:
24049 expression
= TREE_OPERAND (expression
, 0);
24050 return instantiation_dependent_uneval_expression_p (expression
);
24053 /* All instantiation-dependent expressions should also be considered
24054 value-dependent. */
24055 return instantiation_dependent_scope_ref_p (expression
);
24057 case COMPONENT_REF
:
24058 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24059 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24061 case NONTYPE_ARGUMENT_PACK
:
24062 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24063 is value-dependent. */
24065 tree values
= ARGUMENT_PACK_ARGS (expression
);
24066 int i
, len
= TREE_VEC_LENGTH (values
);
24068 for (i
= 0; i
< len
; ++i
)
24069 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24077 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24079 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24085 if (TREE_CODE (type2
) != TREE_LIST
)
24086 return dependent_type_p (type2
);
24088 for (; type2
; type2
= TREE_CHAIN (type2
))
24089 if (dependent_type_p (TREE_VALUE (type2
)))
24096 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24097 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24100 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24101 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24105 tree op
= TREE_OPERAND (expression
, 0);
24106 return (value_dependent_expression_p (op
)
24107 || has_value_dependent_address (op
));
24110 case REQUIRES_EXPR
:
24111 /* Treat all requires-expressions as value-dependent so
24112 we don't try to fold them. */
24116 return dependent_type_p (TREE_OPERAND (expression
, 0));
24120 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24122 tree fn
= get_callee_fndecl (expression
);
24124 nargs
= call_expr_nargs (expression
);
24125 for (i
= 0; i
< nargs
; ++i
)
24127 tree op
= CALL_EXPR_ARG (expression
, i
);
24128 /* In a call to a constexpr member function, look through the
24129 implicit ADDR_EXPR on the object argument so that it doesn't
24130 cause the call to be considered value-dependent. We also
24131 look through it in potential_constant_expression. */
24132 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24133 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24134 && TREE_CODE (op
) == ADDR_EXPR
)
24135 op
= TREE_OPERAND (op
, 0);
24136 if (value_dependent_expression_p (op
))
24142 case TEMPLATE_ID_EXPR
:
24143 return variable_concept_p (TREE_OPERAND (expression
, 0));
24149 if (dependent_type_p (TREE_TYPE (expression
)))
24151 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24152 if (value_dependent_expression_p (val
))
24158 /* Treat a GNU statement expression as dependent to avoid crashing
24159 under instantiate_non_dependent_expr; it can't be constant. */
24163 /* A constant expression is value-dependent if any subexpression is
24164 value-dependent. */
24165 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24167 case tcc_reference
:
24169 case tcc_comparison
:
24171 case tcc_expression
:
24174 int i
, len
= cp_tree_operand_length (expression
);
24176 for (i
= 0; i
< len
; i
++)
24178 tree t
= TREE_OPERAND (expression
, i
);
24180 /* In some cases, some of the operands may be missing.
24181 (For example, in the case of PREDECREMENT_EXPR, the
24182 amount to increment by may be missing.) That doesn't
24183 make the expression dependent. */
24184 if (t
&& value_dependent_expression_p (t
))
24195 /* The expression is not value-dependent. */
24199 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24200 [temp.dep.expr]. Note that an expression with no type is
24201 considered dependent. Other parts of the compiler arrange for an
24202 expression with type-dependent subexpressions to have no type, so
24203 this function doesn't have to be fully recursive. */
24206 type_dependent_expression_p (tree expression
)
24208 if (!processing_template_decl
)
24211 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24214 /* An unresolved name is always dependent. */
24215 if (identifier_p (expression
)
24216 || TREE_CODE (expression
) == USING_DECL
24217 || TREE_CODE (expression
) == WILDCARD_DECL
)
24220 /* A fold expression is type-dependent. */
24221 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24222 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24223 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24224 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24227 /* Some expression forms are never type-dependent. */
24228 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24229 || TREE_CODE (expression
) == SIZEOF_EXPR
24230 || TREE_CODE (expression
) == ALIGNOF_EXPR
24231 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24232 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24233 || TREE_CODE (expression
) == TRAIT_EXPR
24234 || TREE_CODE (expression
) == TYPEID_EXPR
24235 || TREE_CODE (expression
) == DELETE_EXPR
24236 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24237 || TREE_CODE (expression
) == THROW_EXPR
24238 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24241 /* The types of these expressions depends only on the type to which
24242 the cast occurs. */
24243 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24244 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24245 || TREE_CODE (expression
) == CONST_CAST_EXPR
24246 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24247 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24248 || TREE_CODE (expression
) == CAST_EXPR
)
24249 return dependent_type_p (TREE_TYPE (expression
));
24251 /* The types of these expressions depends only on the type created
24252 by the expression. */
24253 if (TREE_CODE (expression
) == NEW_EXPR
24254 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24256 /* For NEW_EXPR tree nodes created inside a template, either
24257 the object type itself or a TREE_LIST may appear as the
24259 tree type
= TREE_OPERAND (expression
, 1);
24260 if (TREE_CODE (type
) == TREE_LIST
)
24261 /* This is an array type. We need to check array dimensions
24263 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24264 || value_dependent_expression_p
24265 (TREE_OPERAND (TREE_VALUE (type
), 1));
24267 return dependent_type_p (type
);
24270 if (TREE_CODE (expression
) == SCOPE_REF
)
24272 tree scope
= TREE_OPERAND (expression
, 0);
24273 tree name
= TREE_OPERAND (expression
, 1);
24275 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24276 contains an identifier associated by name lookup with one or more
24277 declarations declared with a dependent type, or...a
24278 nested-name-specifier or qualified-id that names a member of an
24279 unknown specialization. */
24280 return (type_dependent_expression_p (name
)
24281 || dependent_scope_p (scope
));
24284 if (TREE_CODE (expression
) == TEMPLATE_DECL
24285 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24286 return uses_outer_template_parms (expression
);
24288 if (TREE_CODE (expression
) == STMT_EXPR
)
24289 expression
= stmt_expr_value_expr (expression
);
24291 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24296 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24298 if (type_dependent_expression_p (elt
))
24304 /* A static data member of the current instantiation with incomplete
24305 array type is type-dependent, as the definition and specializations
24306 can have different bounds. */
24307 if (VAR_P (expression
)
24308 && DECL_CLASS_SCOPE_P (expression
)
24309 && dependent_type_p (DECL_CONTEXT (expression
))
24310 && VAR_HAD_UNKNOWN_BOUND (expression
))
24313 /* An array of unknown bound depending on a variadic parameter, eg:
24315 template<typename... Args>
24316 void foo (Args... args)
24318 int arr[] = { args... };
24321 template<int... vals>
24324 int arr[] = { vals... };
24327 If the array has no length and has an initializer, it must be that
24328 we couldn't determine its length in cp_complete_array_type because
24329 it is dependent. */
24330 if (VAR_P (expression
)
24331 && TREE_TYPE (expression
) != NULL_TREE
24332 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24333 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24334 && DECL_INITIAL (expression
))
24337 /* A function or variable template-id is type-dependent if it has any
24338 dependent template arguments. */
24339 if (VAR_OR_FUNCTION_DECL_P (expression
)
24340 && DECL_LANG_SPECIFIC (expression
)
24341 && DECL_TEMPLATE_INFO (expression
))
24343 /* Consider the innermost template arguments, since those are the ones
24344 that come from the template-id; the template arguments for the
24345 enclosing class do not make it type-dependent unless they are used in
24346 the type of the decl. */
24347 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24348 && (any_dependent_template_arguments_p
24349 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24353 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24354 type-dependent. Checking this is important for functions with auto return
24355 type, which looks like a dependent type. */
24356 if (TREE_CODE (expression
) == FUNCTION_DECL
24357 && !(DECL_CLASS_SCOPE_P (expression
)
24358 && dependent_type_p (DECL_CONTEXT (expression
)))
24359 && !(DECL_FRIEND_P (expression
)
24360 && (!DECL_FRIEND_CONTEXT (expression
)
24361 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24362 && !DECL_LOCAL_FUNCTION_P (expression
))
24364 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24365 || undeduced_auto_decl (expression
));
24369 /* Always dependent, on the number of arguments if nothing else. */
24370 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24373 if (TREE_TYPE (expression
) == unknown_type_node
)
24375 if (TREE_CODE (expression
) == ADDR_EXPR
)
24376 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24377 if (TREE_CODE (expression
) == COMPONENT_REF
24378 || TREE_CODE (expression
) == OFFSET_REF
)
24380 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24382 expression
= TREE_OPERAND (expression
, 1);
24383 if (identifier_p (expression
))
24386 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24387 if (TREE_CODE (expression
) == SCOPE_REF
)
24390 if (BASELINK_P (expression
))
24392 if (BASELINK_OPTYPE (expression
)
24393 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24395 expression
= BASELINK_FUNCTIONS (expression
);
24398 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24400 if (any_dependent_template_arguments_p
24401 (TREE_OPERAND (expression
, 1)))
24403 expression
= TREE_OPERAND (expression
, 0);
24404 if (identifier_p (expression
))
24408 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24409 || TREE_CODE (expression
) == FUNCTION_DECL
);
24411 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24412 if (type_dependent_expression_p (*iter
))
24418 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24420 /* Dependent type attributes might not have made it from the decl to
24422 if (DECL_P (expression
)
24423 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24426 return (dependent_type_p (TREE_TYPE (expression
)));
24429 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24430 type-dependent if the expression refers to a member of the current
24431 instantiation and the type of the referenced member is dependent, or the
24432 class member access expression refers to a member of an unknown
24435 This function returns true if the OBJECT in such a class member access
24436 expression is of an unknown specialization. */
24439 type_dependent_object_expression_p (tree object
)
24441 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24443 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24445 tree scope
= TREE_TYPE (object
);
24446 return (!scope
|| dependent_scope_p (scope
));
24449 /* walk_tree callback function for instantiation_dependent_expression_p,
24450 below. Returns non-zero if a dependent subexpression is found. */
24453 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24458 /* We don't have to worry about decltype currently because decltype
24459 of an instantiation-dependent expr is a dependent type. This
24460 might change depending on the resolution of DR 1172. */
24461 *walk_subtrees
= false;
24464 enum tree_code code
= TREE_CODE (*tp
);
24467 /* Don't treat an argument list as dependent just because it has no
24473 case TEMPLATE_PARM_INDEX
:
24476 /* Handle expressions with type operands. */
24480 case AT_ENCODE_EXPR
:
24482 tree op
= TREE_OPERAND (*tp
, 0);
24483 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24484 op
= TREE_TYPE (op
);
24487 if (dependent_type_p (op
))
24491 *walk_subtrees
= false;
24498 case COMPONENT_REF
:
24499 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24500 /* In a template, finish_class_member_access_expr creates a
24501 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24502 type-dependent, so that we can check access control at
24503 instantiation time (PR 42277). See also Core issue 1273. */
24508 if (instantiation_dependent_scope_ref_p (*tp
))
24513 /* Treat statement-expressions as dependent. */
24517 /* Treat requires-expressions as dependent. */
24518 case REQUIRES_EXPR
:
24522 /* Treat calls to function concepts as dependent. */
24523 if (function_concept_check_p (*tp
))
24527 case TEMPLATE_ID_EXPR
:
24528 /* And variable concepts. */
24529 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24537 if (type_dependent_expression_p (*tp
))
24543 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24544 sense defined by the ABI:
24546 "An expression is instantiation-dependent if it is type-dependent
24547 or value-dependent, or it has a subexpression that is type-dependent
24548 or value-dependent."
24550 Except don't actually check value-dependence for unevaluated expressions,
24551 because in sizeof(i) we don't care about the value of i. Checking
24552 type-dependence will in turn check value-dependence of array bounds/template
24553 arguments as needed. */
24556 instantiation_dependent_uneval_expression_p (tree expression
)
24560 if (!processing_template_decl
)
24563 if (expression
== error_mark_node
)
24566 result
= cp_walk_tree_without_duplicates (&expression
,
24567 instantiation_dependent_r
, NULL
);
24568 return result
!= NULL_TREE
;
24571 /* As above, but also check value-dependence of the expression as a whole. */
24574 instantiation_dependent_expression_p (tree expression
)
24576 return (instantiation_dependent_uneval_expression_p (expression
)
24577 || value_dependent_expression_p (expression
));
24580 /* Like type_dependent_expression_p, but it also works while not processing
24581 a template definition, i.e. during substitution or mangling. */
24584 type_dependent_expression_p_push (tree expr
)
24587 ++processing_template_decl
;
24588 b
= type_dependent_expression_p (expr
);
24589 --processing_template_decl
;
24593 /* Returns TRUE if ARGS contains a type-dependent expression. */
24596 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24601 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24603 if (type_dependent_expression_p (arg
))
24609 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24610 expressions) contains any type-dependent expressions. */
24613 any_type_dependent_elements_p (const_tree list
)
24615 for (; list
; list
= TREE_CHAIN (list
))
24616 if (type_dependent_expression_p (TREE_VALUE (list
)))
24622 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24623 expressions) contains any value-dependent expressions. */
24626 any_value_dependent_elements_p (const_tree list
)
24628 for (; list
; list
= TREE_CHAIN (list
))
24629 if (value_dependent_expression_p (TREE_VALUE (list
)))
24635 /* Returns TRUE if the ARG (a template argument) is dependent. */
24638 dependent_template_arg_p (tree arg
)
24640 if (!processing_template_decl
)
24643 /* Assume a template argument that was wrongly written by the user
24644 is dependent. This is consistent with what
24645 any_dependent_template_arguments_p [that calls this function]
24647 if (!arg
|| arg
== error_mark_node
)
24650 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24651 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24653 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24655 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24657 if (DECL_TEMPLATE_PARM_P (arg
))
24659 /* A member template of a dependent class is not necessarily
24660 type-dependent, but it is a dependent template argument because it
24661 will be a member of an unknown specialization to that template. */
24662 tree scope
= CP_DECL_CONTEXT (arg
);
24663 return TYPE_P (scope
) && dependent_type_p (scope
);
24665 else if (ARGUMENT_PACK_P (arg
))
24667 tree args
= ARGUMENT_PACK_ARGS (arg
);
24668 int i
, len
= TREE_VEC_LENGTH (args
);
24669 for (i
= 0; i
< len
; ++i
)
24671 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24677 else if (TYPE_P (arg
))
24678 return dependent_type_p (arg
);
24680 return (type_dependent_expression_p (arg
)
24681 || value_dependent_expression_p (arg
));
24684 /* Returns true if ARGS (a collection of template arguments) contains
24685 any types that require structural equality testing. */
24688 any_template_arguments_need_structural_equality_p (tree args
)
24695 if (args
== error_mark_node
)
24698 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24700 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24701 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24703 tree arg
= TREE_VEC_ELT (level
, j
);
24704 tree packed_args
= NULL_TREE
;
24707 if (ARGUMENT_PACK_P (arg
))
24709 /* Look inside the argument pack. */
24710 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24711 len
= TREE_VEC_LENGTH (packed_args
);
24714 for (k
= 0; k
< len
; ++k
)
24717 arg
= TREE_VEC_ELT (packed_args
, k
);
24719 if (error_operand_p (arg
))
24721 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24723 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24725 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24726 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24735 /* Returns true if ARGS (a collection of template arguments) contains
24736 any dependent arguments. */
24739 any_dependent_template_arguments_p (const_tree args
)
24746 if (args
== error_mark_node
)
24749 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24751 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24752 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24753 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24760 /* Returns TRUE if the template TMPL is type-dependent. */
24763 dependent_template_p (tree tmpl
)
24765 if (TREE_CODE (tmpl
) == OVERLOAD
)
24767 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24768 if (dependent_template_p (*iter
))
24773 /* Template template parameters are dependent. */
24774 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24775 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24777 /* So are names that have not been looked up. */
24778 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24783 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24786 dependent_template_id_p (tree tmpl
, tree args
)
24788 return (dependent_template_p (tmpl
)
24789 || any_dependent_template_arguments_p (args
));
24792 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24796 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24800 if (!processing_template_decl
)
24803 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24805 tree decl
= TREE_VEC_ELT (declv
, i
);
24806 tree init
= TREE_VEC_ELT (initv
, i
);
24807 tree cond
= TREE_VEC_ELT (condv
, i
);
24808 tree incr
= TREE_VEC_ELT (incrv
, i
);
24810 if (type_dependent_expression_p (decl
)
24811 || TREE_CODE (decl
) == SCOPE_REF
)
24814 if (init
&& type_dependent_expression_p (init
))
24817 if (type_dependent_expression_p (cond
))
24820 if (COMPARISON_CLASS_P (cond
)
24821 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24822 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24825 if (TREE_CODE (incr
) == MODOP_EXPR
)
24827 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24828 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24831 else if (type_dependent_expression_p (incr
))
24833 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24835 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24837 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24839 tree t
= TREE_OPERAND (incr
, 1);
24840 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24841 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24850 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24851 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24852 no such TYPE can be found. Note that this function peers inside
24853 uninstantiated templates and therefore should be used only in
24854 extremely limited situations. ONLY_CURRENT_P restricts this
24855 peering to the currently open classes hierarchy (which is required
24856 when comparing types). */
24859 resolve_typename_type (tree type
, bool only_current_p
)
24868 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24870 scope
= TYPE_CONTEXT (type
);
24871 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24872 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24873 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24874 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24875 identifier of the TYPENAME_TYPE anymore.
24876 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24877 TYPENAME_TYPE instead, we avoid messing up with a possible
24878 typedef variant case. */
24879 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24881 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24882 it first before we can figure out what NAME refers to. */
24883 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24885 if (TYPENAME_IS_RESOLVING_P (scope
))
24886 /* Given a class template A with a dependent base with nested type C,
24887 typedef typename A::C::C C will land us here, as trying to resolve
24888 the initial A::C leads to the local C typedef, which leads back to
24889 A::C::C. So we break the recursion now. */
24892 scope
= resolve_typename_type (scope
, only_current_p
);
24894 /* If we don't know what SCOPE refers to, then we cannot resolve the
24896 if (!CLASS_TYPE_P (scope
))
24898 /* If this is a typedef, we don't want to look inside (c++/11987). */
24899 if (typedef_variant_p (type
))
24901 /* If SCOPE isn't the template itself, it will not have a valid
24902 TYPE_FIELDS list. */
24903 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24904 /* scope is either the template itself or a compatible instantiation
24905 like X<T>, so look up the name in the original template. */
24906 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24907 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24908 gcc_checking_assert (uses_template_parms (scope
));
24909 /* If scope has no fields, it can't be a current instantiation. Check this
24910 before currently_open_class to avoid infinite recursion (71515). */
24911 if (!TYPE_FIELDS (scope
))
24913 /* If the SCOPE is not the current instantiation, there's no reason
24914 to look inside it. */
24915 if (only_current_p
&& !currently_open_class (scope
))
24917 /* Enter the SCOPE so that name lookup will be resolved as if we
24918 were in the class definition. In particular, SCOPE will no
24919 longer be considered a dependent type. */
24920 pushed_scope
= push_scope (scope
);
24921 /* Look up the declaration. */
24922 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24923 tf_warning_or_error
);
24925 result
= NULL_TREE
;
24927 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24928 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24929 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
24932 else if (identifier_p (fullname
)
24933 && TREE_CODE (decl
) == TYPE_DECL
)
24935 result
= TREE_TYPE (decl
);
24936 if (result
== error_mark_node
)
24937 result
= NULL_TREE
;
24939 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
24940 && DECL_CLASS_TEMPLATE_P (decl
))
24942 /* Obtain the template and the arguments. */
24943 tree tmpl
= TREE_OPERAND (fullname
, 0);
24944 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
24946 /* We get here with a plain identifier because a previous tentative
24947 parse of the nested-name-specifier as part of a ptr-operator saw
24948 ::template X<A>. The use of ::template is necessary in a
24949 ptr-operator, but wrong in a declarator-id.
24951 [temp.names]: In a qualified-id of a declarator-id, the keyword
24952 template shall not appear at the top level. */
24953 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
24954 "keyword %<template%> not allowed in declarator-id");
24957 tree args
= TREE_OPERAND (fullname
, 1);
24958 /* Instantiate the template. */
24959 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
24960 /*entering_scope=*/true,
24961 tf_error
| tf_user
);
24962 if (result
== error_mark_node
)
24963 result
= NULL_TREE
;
24966 /* Leave the SCOPE. */
24968 pop_scope (pushed_scope
);
24970 /* If we failed to resolve it, return the original typename. */
24974 /* If lookup found a typename type, resolve that too. */
24975 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
24977 /* Ill-formed programs can cause infinite recursion here, so we
24978 must catch that. */
24979 TYPENAME_IS_RESOLVING_P (result
) = 1;
24980 result
= resolve_typename_type (result
, only_current_p
);
24981 TYPENAME_IS_RESOLVING_P (result
) = 0;
24984 /* Qualify the resulting type. */
24985 quals
= cp_type_quals (type
);
24987 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
24992 /* EXPR is an expression which is not type-dependent. Return a proxy
24993 for EXPR that can be used to compute the types of larger
24994 expressions containing EXPR. */
24997 build_non_dependent_expr (tree expr
)
25001 /* When checking, try to get a constant value for all non-dependent
25002 expressions in order to expose bugs in *_dependent_expression_p
25003 and constexpr. This can affect code generation, see PR70704, so
25004 only do this for -fchecking=2. */
25005 if (flag_checking
> 1
25006 && cxx_dialect
>= cxx11
25007 /* Don't do this during nsdmi parsing as it can lead to
25008 unexpected recursive instantiations. */
25009 && !parsing_nsdmi ()
25010 /* Don't do this during concept expansion either and for
25011 the same reason. */
25012 && !expanding_concept ())
25013 fold_non_dependent_expr (expr
);
25015 /* Preserve OVERLOADs; the functions must be available to resolve
25018 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25019 inner_expr
= stmt_expr_value_expr (inner_expr
);
25020 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25021 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25022 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25023 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25024 if (is_overloaded_fn (inner_expr
)
25025 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25027 /* There is no need to return a proxy for a variable. */
25030 /* Preserve string constants; conversions from string constants to
25031 "char *" are allowed, even though normally a "const char *"
25032 cannot be used to initialize a "char *". */
25033 if (TREE_CODE (expr
) == STRING_CST
)
25035 /* Preserve void and arithmetic constants, as an optimization -- there is no
25036 reason to create a new node. */
25037 if (TREE_CODE (expr
) == VOID_CST
25038 || TREE_CODE (expr
) == INTEGER_CST
25039 || TREE_CODE (expr
) == REAL_CST
)
25041 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25042 There is at least one place where we want to know that a
25043 particular expression is a throw-expression: when checking a ?:
25044 expression, there are special rules if the second or third
25045 argument is a throw-expression. */
25046 if (TREE_CODE (expr
) == THROW_EXPR
)
25049 /* Don't wrap an initializer list, we need to be able to look inside. */
25050 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25053 /* Don't wrap a dummy object, we need to be able to test for it. */
25054 if (is_dummy_object (expr
))
25057 if (TREE_CODE (expr
) == COND_EXPR
)
25058 return build3 (COND_EXPR
,
25060 TREE_OPERAND (expr
, 0),
25061 (TREE_OPERAND (expr
, 1)
25062 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25063 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25064 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25065 if (TREE_CODE (expr
) == COMPOUND_EXPR
25066 && !COMPOUND_EXPR_OVERLOADED (expr
))
25067 return build2 (COMPOUND_EXPR
,
25069 TREE_OPERAND (expr
, 0),
25070 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25072 /* If the type is unknown, it can't really be non-dependent */
25073 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25075 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25076 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25079 /* ARGS is a vector of expressions as arguments to a function call.
25080 Replace the arguments with equivalent non-dependent expressions.
25081 This modifies ARGS in place. */
25084 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25089 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25091 tree newarg
= build_non_dependent_expr (arg
);
25093 (*args
)[ix
] = newarg
;
25097 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25098 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25099 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25102 make_auto_1 (tree name
, bool set_canonical
)
25104 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25105 TYPE_NAME (au
) = build_decl (input_location
,
25106 TYPE_DECL
, name
, au
);
25107 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25108 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25109 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25110 TYPE_NAME (au
), NULL_TREE
);
25112 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25113 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25114 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25120 make_decltype_auto (void)
25122 return make_auto_1 (decltype_auto_identifier
, true);
25128 return make_auto_1 (auto_identifier
, true);
25131 /* Return a C++17 deduction placeholder for class template TMPL. */
25134 make_template_placeholder (tree tmpl
)
25136 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25137 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25141 /* True iff T is a C++17 class template deduction placeholder. */
25144 template_placeholder_p (tree t
)
25146 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25149 /* Make a "constrained auto" type-specifier. This is an
25150 auto type with constraints that must be associated after
25151 deduction. The constraint is formed from the given
25152 CONC and its optional sequence of arguments, which are
25153 non-null if written as partial-concept-id. */
25156 make_constrained_auto (tree con
, tree args
)
25158 tree type
= make_auto_1 (auto_identifier
, false);
25160 /* Build the constraint. */
25161 tree tmpl
= DECL_TI_TEMPLATE (con
);
25162 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25163 expr
= build_concept_check (expr
, type
, args
);
25165 tree constr
= normalize_expression (expr
);
25166 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25168 /* Our canonical type depends on the constraint. */
25169 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25171 /* Attach the constraint to the type declaration. */
25172 tree decl
= TYPE_NAME (type
);
25176 /* Given type ARG, return std::initializer_list<ARG>. */
25181 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25183 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25185 gcc_rich_location
richloc (input_location
);
25186 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25187 error_at (&richloc
,
25188 "deducing from brace-enclosed initializer list"
25189 " requires %<#include <initializer_list>%>");
25191 return error_mark_node
;
25193 tree argvec
= make_tree_vec (1);
25194 TREE_VEC_ELT (argvec
, 0) = arg
;
25196 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25197 NULL_TREE
, 0, tf_warning_or_error
);
25200 /* Replace auto in TYPE with std::initializer_list<auto>. */
25203 listify_autos (tree type
, tree auto_node
)
25205 tree init_auto
= listify (auto_node
);
25206 tree argvec
= make_tree_vec (1);
25207 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25208 if (processing_template_decl
)
25209 argvec
= add_to_template_args (current_template_args (), argvec
);
25210 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25213 /* Hash traits for hashing possibly constrained 'auto'
25214 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25216 struct auto_hash
: default_hash_traits
<tree
>
25218 static inline hashval_t
hash (tree
);
25219 static inline bool equal (tree
, tree
);
25222 /* Hash the 'auto' T. */
25225 auto_hash::hash (tree t
)
25227 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25228 /* Matching constrained-type-specifiers denote the same template
25229 parameter, so hash the constraint. */
25230 return hash_placeholder_constraint (c
);
25232 /* But unconstrained autos are all separate, so just hash the pointer. */
25233 return iterative_hash_object (t
, 0);
25236 /* Compare two 'auto's. */
25239 auto_hash::equal (tree t1
, tree t2
)
25244 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25245 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25247 /* Two unconstrained autos are distinct. */
25251 return equivalent_placeholder_constraints (c1
, c2
);
25254 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25255 constrained) auto, add it to the vector. */
25258 extract_autos_r (tree t
, void *data
)
25260 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25263 /* All the autos were built with index 0; fix that up now. */
25264 tree
*p
= hash
.find_slot (t
, INSERT
);
25267 /* If this is a repeated constrained-type-specifier, use the index we
25269 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25272 /* Otherwise this is new, so use the current count. */
25274 idx
= hash
.elements () - 1;
25276 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25279 /* Always keep walking. */
25283 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25284 says they can appear anywhere in the type. */
25287 extract_autos (tree type
)
25289 hash_set
<tree
> visited
;
25290 hash_table
<auto_hash
> hash (2);
25292 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25294 tree tree_vec
= make_tree_vec (hash
.elements());
25295 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25296 iter
!= hash
.end(); ++iter
)
25299 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25300 TREE_VEC_ELT (tree_vec
, i
)
25301 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25307 /* The stem for deduction guide names. */
25308 const char *const dguide_base
= "__dguide_";
25310 /* Return the name for a deduction guide for class template TMPL. */
25313 dguide_name (tree tmpl
)
25315 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25316 tree tname
= TYPE_IDENTIFIER (type
);
25317 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25318 + IDENTIFIER_LENGTH (tname
));
25319 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25320 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25321 IDENTIFIER_LENGTH (tname
) + 1);
25322 tree dname
= get_identifier (buf
);
25323 TREE_TYPE (dname
) = type
;
25327 /* True if NAME is the name of a deduction guide. */
25330 dguide_name_p (tree name
)
25332 return (TREE_TYPE (name
)
25333 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25334 strlen (dguide_base
)));
25337 /* True if FN is a deduction guide. */
25340 deduction_guide_p (const_tree fn
)
25343 if (tree name
= DECL_NAME (fn
))
25344 return dguide_name_p (name
);
25348 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25351 copy_guide_p (const_tree fn
)
25353 gcc_assert (deduction_guide_p (fn
));
25354 if (!DECL_ARTIFICIAL (fn
))
25356 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25357 return (TREE_CHAIN (parms
) == void_list_node
25358 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25361 /* True if FN is a guide generated from a constructor template. */
25364 template_guide_p (const_tree fn
)
25366 gcc_assert (deduction_guide_p (fn
));
25367 if (!DECL_ARTIFICIAL (fn
))
25369 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25370 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25371 return PRIMARY_TEMPLATE_P (org
);
25375 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25376 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25377 template parameter types. Note that the handling of template template
25378 parameters relies on current_template_parms being set appropriately for the
25382 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25383 tree tsubst_args
, tsubst_flags_t complain
)
25385 tree oldidx
= get_template_parm_index (olddecl
);
25388 if (TREE_CODE (olddecl
) == TYPE_DECL
25389 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25391 tree oldtype
= TREE_TYPE (olddecl
);
25392 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25393 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25394 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25395 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25396 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25399 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25400 complain
, NULL_TREE
);
25403 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25404 DECL_NAME (olddecl
), newtype
);
25405 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25408 if (TREE_CODE (olddecl
) == TYPE_DECL
25409 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25411 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25412 = build_template_parm_index (index
, level
, level
,
25414 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25415 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25416 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25417 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25419 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25421 DECL_TEMPLATE_RESULT (newdecl
)
25422 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25423 DECL_NAME (olddecl
), newtype
);
25424 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25425 // First create a copy (ttargs) of tsubst_args with an
25426 // additional level for the template template parameter's own
25427 // template parameters (ttparms).
25428 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25429 (DECL_TEMPLATE_PARMS (olddecl
)));
25430 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25431 tree ttargs
= make_tree_vec (depth
+ 1);
25432 for (int i
= 0; i
< depth
; ++i
)
25433 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25434 TREE_VEC_ELT (ttargs
, depth
)
25435 = template_parms_level_to_args (ttparms
);
25436 // Substitute ttargs into ttparms to fix references to
25437 // other template parameters.
25438 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25440 // Now substitute again with args based on tparms, to reduce
25441 // the level of the ttparms.
25442 ttargs
= current_template_args ();
25443 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25445 // Finally, tack the adjusted parms onto tparms.
25446 ttparms
= tree_cons (size_int (depth
), ttparms
,
25447 current_template_parms
);
25448 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25453 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25455 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25456 TREE_CODE (oldconst
),
25457 DECL_NAME (oldconst
), newtype
);
25458 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25459 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25460 SET_DECL_TEMPLATE_PARM_P (newconst
);
25461 newidx
= build_template_parm_index (index
, level
, level
,
25462 newconst
, newtype
);
25463 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25464 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25465 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25471 /* Returns a C++17 class deduction guide template based on the constructor
25472 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25473 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25476 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25478 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25479 bool memtmpl
= false;
25482 tree fn_tmpl
= NULL_TREE
;
25487 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25490 type
= TREE_TYPE (type
);
25491 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25494 fparms
= void_list_node
;
25496 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25497 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25498 targs
= CLASSTYPE_TI_ARGS (type
);
25501 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25502 explicit_p
= false;
25506 ++processing_template_decl
;
25509 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25510 : DECL_TI_TEMPLATE (ctor
));
25512 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25513 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25515 type
= DECL_CONTEXT (ctor
);
25517 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25518 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25519 fully specialized args for the enclosing class. Strip those off, as
25520 the deduction guide won't have those template parameters. */
25521 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25522 TMPL_PARMS_DEPTH (tparms
));
25523 /* Discard the 'this' parameter. */
25524 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25525 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25526 ci
= get_constraints (ctor
);
25527 loc
= DECL_SOURCE_LOCATION (ctor
);
25528 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25530 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25534 /* For a member template constructor, we need to flatten the two
25535 template parameter lists into one, and then adjust the function
25536 signature accordingly. This gets...complicated. */
25537 tree save_parms
= current_template_parms
;
25539 /* For a member template we should have two levels of parms/args, one
25540 for the class and one for the constructor. We stripped
25541 specialized args for further enclosing classes above. */
25542 const int depth
= 2;
25543 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25545 /* Template args for translating references to the two-level template
25546 parameters into references to the one-level template parameters we
25548 tree tsubst_args
= copy_node (targs
);
25549 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25550 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25552 /* Template parms for the constructor template. */
25553 tree ftparms
= TREE_VALUE (tparms
);
25554 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25555 /* Template parms for the class template. */
25556 tparms
= TREE_CHAIN (tparms
);
25557 tree ctparms
= TREE_VALUE (tparms
);
25558 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25559 /* Template parms for the deduction guide start as a copy of the
25560 template parms for the class. We set current_template_parms for
25561 lookup_template_class_1. */
25562 current_template_parms
= tparms
= copy_node (tparms
);
25563 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25564 for (unsigned i
= 0; i
< clen
; ++i
)
25565 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25567 /* Now we need to rewrite the constructor parms to append them to the
25569 for (unsigned i
= 0; i
< flen
; ++i
)
25571 unsigned index
= i
+ clen
;
25572 unsigned level
= 1;
25573 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25574 tree olddecl
= TREE_VALUE (oldelt
);
25575 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25576 tsubst_args
, complain
);
25577 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25578 tsubst_args
, complain
, ctor
);
25579 tree list
= build_tree_list (newdef
, newdecl
);
25580 TEMPLATE_PARM_CONSTRAINTS (list
)
25581 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25582 tsubst_args
, complain
, ctor
);
25583 TREE_VEC_ELT (new_vec
, index
) = list
;
25584 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25587 /* Now we have a final set of template parms to substitute into the
25588 function signature. */
25589 targs
= template_parms_to_args (tparms
);
25590 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25592 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25594 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25596 current_template_parms
= save_parms
;
25598 --processing_template_decl
;
25603 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25604 tparms
= copy_node (tparms
);
25605 INNERMOST_TEMPLATE_PARMS (tparms
)
25606 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25609 tree fntype
= build_function_type (type
, fparms
);
25610 tree ded_fn
= build_lang_decl_loc (loc
,
25612 dguide_name (type
), fntype
);
25613 DECL_ARGUMENTS (ded_fn
) = fargs
;
25614 DECL_ARTIFICIAL (ded_fn
) = true;
25615 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25616 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25617 DECL_ARTIFICIAL (ded_tmpl
) = true;
25618 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25619 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25620 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25621 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25623 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25625 set_constraints (ded_tmpl
, ci
);
25630 /* Deduce template arguments for the class template placeholder PTYPE for
25631 template TMPL based on the initializer INIT, and return the resulting
25635 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25636 tsubst_flags_t complain
)
25638 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25640 /* We should have handled this in the caller. */
25641 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25643 if (complain
& tf_error
)
25644 error ("non-class template %qT used without template arguments", tmpl
);
25645 return error_mark_node
;
25648 tree type
= TREE_TYPE (tmpl
);
25650 bool try_list_ctor
= false;
25652 vec
<tree
,va_gc
> *args
;
25653 if (init
== NULL_TREE
25654 || TREE_CODE (init
) == TREE_LIST
)
25655 args
= make_tree_vector_from_list (init
);
25656 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25658 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25659 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25661 /* As an exception, the first phase in 16.3.1.7 (considering the
25662 initializer list as a single argument) is omitted if the
25663 initializer list consists of a single expression of type cv U,
25664 where U is a specialization of C or a class derived from a
25665 specialization of C. */
25666 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25667 tree etype
= TREE_TYPE (elt
);
25669 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25670 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25671 int err
= unify (tparms
, targs
, type
, etype
,
25672 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25674 try_list_ctor
= false;
25677 if (try_list_ctor
|| is_std_init_list (type
))
25678 args
= make_tree_vector_single (init
);
25680 args
= make_tree_vector_from_ctor (init
);
25683 args
= make_tree_vector_single (init
);
25685 tree dname
= dguide_name (tmpl
);
25686 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25687 /*type*/false, /*complain*/false,
25689 bool elided
= false;
25690 if (cands
== error_mark_node
)
25693 /* Prune explicit deduction guides in copy-initialization context. */
25694 if (flags
& LOOKUP_ONLYCONVERTING
)
25696 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25697 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25702 /* Found a nonconverting guide, prune the candidates. */
25703 tree pruned
= NULL_TREE
;
25704 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25705 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25706 pruned
= lookup_add (*iter
, pruned
);
25712 tree outer_args
= NULL_TREE
;
25713 if (DECL_CLASS_SCOPE_P (tmpl
)
25714 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25716 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25717 type
= TREE_TYPE (most_general_template (tmpl
));
25720 bool saw_ctor
= false;
25721 // FIXME cache artificial deduction guides
25722 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25724 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25725 if ((flags
& LOOKUP_ONLYCONVERTING
)
25726 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25729 cands
= lookup_add (guide
, cands
);
25734 tree call
= error_mark_node
;
25736 /* If this is list-initialization and the class has a list constructor, first
25737 try deducing from the list as a single argument, as [over.match.list]. */
25738 tree list_cands
= NULL_TREE
;
25739 if (try_list_ctor
&& cands
)
25740 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25743 if (is_list_ctor (dg
))
25744 list_cands
= lookup_add (dg
, list_cands
);
25748 ++cp_unevaluated_operand
;
25749 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25750 --cp_unevaluated_operand
;
25752 if (call
== error_mark_node
)
25754 /* That didn't work, now try treating the list as a sequence of
25756 release_tree_vector (args
);
25757 args
= make_tree_vector_from_ctor (init
);
25761 /* Maybe generate an implicit deduction guide. */
25762 if (call
== error_mark_node
&& args
->length () < 2)
25764 tree gtype
= NULL_TREE
;
25766 if (args
->length () == 1)
25767 /* Generate a copy guide. */
25768 gtype
= build_reference_type (type
);
25769 else if (!saw_ctor
)
25770 /* Generate a default guide. */
25775 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25776 cands
= lookup_add (guide
, cands
);
25780 if (elided
&& !cands
)
25782 error ("cannot deduce template arguments for copy-initialization"
25783 " of %qT, as it has no non-explicit deduction guides or "
25784 "user-declared constructors", type
);
25785 return error_mark_node
;
25787 else if (!cands
&& call
== error_mark_node
)
25789 error ("cannot deduce template arguments of %qT, as it has no viable "
25790 "deduction guides", type
);
25791 return error_mark_node
;
25794 if (call
== error_mark_node
)
25796 ++cp_unevaluated_operand
;
25797 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25798 --cp_unevaluated_operand
;
25801 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25803 error ("class template argument deduction failed:");
25805 ++cp_unevaluated_operand
;
25806 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25807 --cp_unevaluated_operand
;
25810 inform (input_location
, "explicit deduction guides not considered "
25811 "for copy-initialization");
25814 release_tree_vector (args
);
25816 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25819 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25820 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25823 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25825 return do_auto_deduction (type
, init
, auto_node
,
25826 tf_warning_or_error
,
25830 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25831 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25832 The CONTEXT determines the context in which auto deduction is performed
25833 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25834 OUTER_TARGS are used during template argument deduction
25835 (context == adc_unify) to properly substitute the result, and is ignored
25838 For partial-concept-ids, extra args may be appended to the list of deduced
25839 template arguments prior to determining constraint satisfaction. */
25842 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25843 tsubst_flags_t complain
, auto_deduction_context context
,
25844 tree outer_targs
, int flags
)
25848 if (init
== error_mark_node
)
25849 return error_mark_node
;
25851 if (init
&& type_dependent_expression_p (init
)
25852 && context
!= adc_unify
)
25853 /* Defining a subset of type-dependent expressions that we can deduce
25854 from ahead of time isn't worth the trouble. */
25857 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25858 /* C++17 class template argument deduction. */
25859 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25861 if (TREE_TYPE (init
) == NULL_TREE
)
25862 /* Nothing we can do with this, even in deduction context. */
25865 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25866 with either a new invented type template parameter U or, if the
25867 initializer is a braced-init-list (8.5.4), with
25868 std::initializer_list<U>. */
25869 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25871 if (!DIRECT_LIST_INIT_P (init
))
25872 type
= listify_autos (type
, auto_node
);
25873 else if (CONSTRUCTOR_NELTS (init
) == 1)
25874 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25877 if (complain
& tf_warning_or_error
)
25879 if (permerror (input_location
, "direct-list-initialization of "
25880 "%<auto%> requires exactly one element"))
25881 inform (input_location
,
25882 "for deduction to %<std::initializer_list%>, use copy-"
25883 "list-initialization (i.e. add %<=%> before the %<{%>)");
25885 type
= listify_autos (type
, auto_node
);
25889 if (type
== error_mark_node
)
25890 return error_mark_node
;
25892 init
= resolve_nondeduced_context (init
, complain
);
25894 if (context
== adc_decomp_type
25895 && auto_node
== type
25896 && init
!= error_mark_node
25897 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
25898 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25899 and initializer has array type, deduce cv-qualified array type. */
25900 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
25902 else if (AUTO_IS_DECLTYPE (auto_node
))
25904 bool id
= (DECL_P (init
)
25905 || ((TREE_CODE (init
) == COMPONENT_REF
25906 || TREE_CODE (init
) == SCOPE_REF
)
25907 && !REF_PARENTHESIZED_P (init
)));
25908 targs
= make_tree_vec (1);
25909 TREE_VEC_ELT (targs
, 0)
25910 = finish_decltype_type (init
, id
, tf_warning_or_error
);
25911 if (type
!= auto_node
)
25913 if (complain
& tf_error
)
25914 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
25915 return error_mark_node
;
25920 tree parms
= build_tree_list (NULL_TREE
, type
);
25924 tparms
= extract_autos (type
);
25927 tparms
= make_tree_vec (1);
25928 TREE_VEC_ELT (tparms
, 0)
25929 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
25932 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25933 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25934 DEDUCE_CALL
, LOOKUP_NORMAL
,
25935 NULL
, /*explain_p=*/false);
25938 if (processing_template_decl
)
25939 /* Try again at instantiation time. */
25941 if (type
&& type
!= error_mark_node
25942 && (complain
& tf_error
))
25943 /* If type is error_mark_node a diagnostic must have been
25944 emitted by now. Also, having a mention to '<type error>'
25945 in the diagnostic is not really useful to the user. */
25947 if (cfun
&& auto_node
== current_function_auto_return_pattern
25948 && LAMBDA_FUNCTION_P (current_function_decl
))
25949 error ("unable to deduce lambda return type from %qE", init
);
25951 error ("unable to deduce %qT from %qE", type
, init
);
25952 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25953 DEDUCE_CALL
, LOOKUP_NORMAL
,
25954 NULL
, /*explain_p=*/true);
25956 return error_mark_node
;
25960 /* Check any placeholder constraints against the deduced type. */
25961 if (flag_concepts
&& !processing_template_decl
)
25962 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
25964 /* Use the deduced type to check the associated constraints. If we
25965 have a partial-concept-id, rebuild the argument list so that
25966 we check using the extra arguments. */
25967 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
25968 tree cargs
= CHECK_CONSTR_ARGS (constr
);
25969 if (TREE_VEC_LENGTH (cargs
) > 1)
25971 cargs
= copy_node (cargs
);
25972 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
25976 if (!constraints_satisfied_p (constr
, cargs
))
25978 if (complain
& tf_warning_or_error
)
25982 case adc_unspecified
:
25984 error("placeholder constraints not satisfied");
25986 case adc_variable_type
:
25987 case adc_decomp_type
:
25988 error ("deduced initializer does not satisfy "
25989 "placeholder constraints");
25991 case adc_return_type
:
25992 error ("deduced return type does not satisfy "
25993 "placeholder constraints");
25995 case adc_requirement
:
25996 error ("deduced expression type does not satisfy "
25997 "placeholder constraints");
26000 diagnose_constraints (input_location
, constr
, targs
);
26002 return error_mark_node
;
26006 if (processing_template_decl
&& context
!= adc_unify
)
26007 outer_targs
= current_template_args ();
26008 targs
= add_to_template_args (outer_targs
, targs
);
26009 return tsubst (type
, targs
, complain
, NULL_TREE
);
26012 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26016 splice_late_return_type (tree type
, tree late_return_type
)
26018 if (is_auto (type
))
26020 if (late_return_type
)
26021 return late_return_type
;
26023 tree idx
= get_template_parm_index (type
);
26024 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26025 /* In an abbreviated function template we didn't know we were dealing
26026 with a function template when we saw the auto return type, so update
26027 it to have the correct level. */
26028 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26033 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26034 'decltype(auto)' or a deduced class template. */
26037 is_auto (const_tree type
)
26039 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26040 && (TYPE_IDENTIFIER (type
) == auto_identifier
26041 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26042 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26048 /* for_each_template_parm callback for type_uses_auto. */
26051 is_auto_r (tree tp
, void */
*data*/
)
26053 return is_auto (tp
);
26056 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26057 a use of `auto'. Returns NULL_TREE otherwise. */
26060 type_uses_auto (tree type
)
26062 if (type
== NULL_TREE
)
26064 else if (flag_concepts
)
26066 /* The Concepts TS allows multiple autos in one type-specifier; just
26067 return the first one we find, do_auto_deduction will collect all of
26069 if (uses_template_parms (type
))
26070 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26071 /*visited*/NULL
, /*nondeduced*/true);
26076 return find_type_usage (type
, is_auto
);
26079 /* For a given template T, return the vector of typedefs referenced
26080 in T for which access check is needed at T instantiation time.
26081 T is either a FUNCTION_DECL or a RECORD_TYPE.
26082 Those typedefs were added to T by the function
26083 append_type_to_template_for_access_check. */
26085 vec
<qualified_typedef_usage_t
, va_gc
> *
26086 get_types_needing_access_check (tree t
)
26089 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26091 if (!t
|| t
== error_mark_node
)
26094 if (!(ti
= get_template_info (t
)))
26097 if (CLASS_TYPE_P (t
)
26098 || TREE_CODE (t
) == FUNCTION_DECL
)
26100 if (!TI_TEMPLATE (ti
))
26103 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26109 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26110 tied to T. That list of typedefs will be access checked at
26111 T instantiation time.
26112 T is either a FUNCTION_DECL or a RECORD_TYPE.
26113 TYPE_DECL is a TYPE_DECL node representing a typedef.
26114 SCOPE is the scope through which TYPE_DECL is accessed.
26115 LOCATION is the location of the usage point of TYPE_DECL.
26117 This function is a subroutine of
26118 append_type_to_template_for_access_check. */
26121 append_type_to_template_for_access_check_1 (tree t
,
26124 location_t location
)
26126 qualified_typedef_usage_t typedef_usage
;
26129 if (!t
|| t
== error_mark_node
)
26132 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26133 || CLASS_TYPE_P (t
))
26135 && TREE_CODE (type_decl
) == TYPE_DECL
26138 if (!(ti
= get_template_info (t
)))
26141 gcc_assert (TI_TEMPLATE (ti
));
26143 typedef_usage
.typedef_decl
= type_decl
;
26144 typedef_usage
.context
= scope
;
26145 typedef_usage
.locus
= location
;
26147 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26150 /* Append TYPE_DECL to the template TEMPL.
26151 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26152 At TEMPL instanciation time, TYPE_DECL will be checked to see
26153 if it can be accessed through SCOPE.
26154 LOCATION is the location of the usage point of TYPE_DECL.
26156 e.g. consider the following code snippet:
26163 template<class U> struct S
26165 C::myint mi; // <-- usage point of the typedef C::myint
26170 At S<char> instantiation time, we need to check the access of C::myint
26171 In other words, we need to check the access of the myint typedef through
26172 the C scope. For that purpose, this function will add the myint typedef
26173 and the scope C through which its being accessed to a list of typedefs
26174 tied to the template S. That list will be walked at template instantiation
26175 time and access check performed on each typedefs it contains.
26176 Note that this particular code snippet should yield an error because
26177 myint is private to C. */
26180 append_type_to_template_for_access_check (tree templ
,
26183 location_t location
)
26185 qualified_typedef_usage_t
*iter
;
26188 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26190 /* Make sure we don't append the type to the template twice. */
26191 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26192 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26195 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26199 /* Convert the generic type parameters in PARM that match the types given in the
26200 range [START_IDX, END_IDX) from the current_template_parms into generic type
26204 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26206 tree current
= current_template_parms
;
26207 int depth
= TMPL_PARMS_DEPTH (current
);
26208 current
= INNERMOST_TEMPLATE_PARMS (current
);
26209 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26211 for (int i
= 0; i
< start_idx
; ++i
)
26212 TREE_VEC_ELT (replacement
, i
)
26213 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26215 for (int i
= start_idx
; i
< end_idx
; ++i
)
26217 /* Create a distinct parameter pack type from the current parm and add it
26218 to the replacement args to tsubst below into the generic function
26221 tree o
= TREE_TYPE (TREE_VALUE
26222 (TREE_VEC_ELT (current
, i
)));
26223 tree t
= copy_type (o
);
26224 TEMPLATE_TYPE_PARM_INDEX (t
)
26225 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26227 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26228 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26229 TYPE_MAIN_VARIANT (t
) = t
;
26230 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26231 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26232 TREE_VEC_ELT (replacement
, i
) = t
;
26233 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26236 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26237 TREE_VEC_ELT (replacement
, i
)
26238 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26240 /* If there are more levels then build up the replacement with the outer
26243 replacement
= add_to_template_args (template_parms_to_args
26244 (TREE_CHAIN (current_template_parms
)),
26247 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26250 /* Entries in the decl_constraint hash table. */
26251 struct GTY((for_user
)) constr_entry
26257 /* Hashing function and equality for constraint entries. */
26258 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26260 static hashval_t
hash (constr_entry
*e
)
26262 return (hashval_t
)DECL_UID (e
->decl
);
26265 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26267 return e1
->decl
== e2
->decl
;
26271 /* A mapping from declarations to constraint information. Note that
26272 both templates and their underlying declarations are mapped to the
26273 same constraint information.
26275 FIXME: This is defined in pt.c because garbage collection
26276 code is not being generated for constraint.cc. */
26278 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26280 /* Returns the template constraints of declaration T. If T is not
26281 constrained, return NULL_TREE. Note that T must be non-null. */
26284 get_constraints (tree t
)
26286 if (!flag_concepts
)
26289 gcc_assert (DECL_P (t
));
26290 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26291 t
= DECL_TEMPLATE_RESULT (t
);
26292 constr_entry elt
= { t
, NULL_TREE
};
26293 constr_entry
* found
= decl_constraints
->find (&elt
);
26300 /* Associate the given constraint information CI with the declaration
26301 T. If T is a template, then the constraints are associated with
26302 its underlying declaration. Don't build associations if CI is
26306 set_constraints (tree t
, tree ci
)
26310 gcc_assert (t
&& flag_concepts
);
26311 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26312 t
= DECL_TEMPLATE_RESULT (t
);
26313 gcc_assert (!get_constraints (t
));
26314 constr_entry elt
= {t
, ci
};
26315 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26316 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26321 /* Remove the associated constraints of the declaration T. */
26324 remove_constraints (tree t
)
26326 gcc_assert (DECL_P (t
));
26327 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26328 t
= DECL_TEMPLATE_RESULT (t
);
26330 constr_entry elt
= {t
, NULL_TREE
};
26331 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26333 decl_constraints
->clear_slot (slot
);
26336 /* Memoized satisfaction results for declarations. This
26337 maps the pair (constraint_info, arguments) to the result computed
26338 by constraints_satisfied_p. */
26340 struct GTY((for_user
)) constraint_sat_entry
26347 /* Hashing function and equality for constraint entries. */
26349 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26351 static hashval_t
hash (constraint_sat_entry
*e
)
26353 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26354 return iterative_hash_template_arg (e
->args
, val
);
26357 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26359 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26363 /* Memoized satisfaction results for concept checks. */
26365 struct GTY((for_user
)) concept_spec_entry
26372 /* Hashing function and equality for constraint entries. */
26374 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26376 static hashval_t
hash (concept_spec_entry
*e
)
26378 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26381 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26383 ++comparing_specializations
;
26384 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26385 --comparing_specializations
;
26390 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26391 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26393 /* Search for a memoized satisfaction result. Returns one of the
26394 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26397 lookup_constraint_satisfaction (tree ci
, tree args
)
26399 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26400 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26402 return found
->result
;
26407 /* Memoize the result of a satisfication test. Returns the saved result. */
26410 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26412 constraint_sat_entry elt
= {ci
, args
, result
};
26413 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26414 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26420 /* Search for a memoized satisfaction result for a concept. */
26423 lookup_concept_satisfaction (tree tmpl
, tree args
)
26425 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26426 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26428 return found
->result
;
26433 /* Memoize the result of a concept check. Returns the saved result. */
26436 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26438 concept_spec_entry elt
= {tmpl
, args
, result
};
26439 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26440 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26446 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26448 /* Returns a prior concept specialization. This returns the substituted
26449 and normalized constraints defined by the concept. */
26452 get_concept_expansion (tree tmpl
, tree args
)
26454 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26455 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26457 return found
->result
;
26462 /* Save a concept expansion for later. */
26465 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26467 concept_spec_entry elt
= {tmpl
, args
, def
};
26468 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26469 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26476 hash_subsumption_args (tree t1
, tree t2
)
26478 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26479 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26481 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26482 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26483 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26484 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26488 /* Compare the constraints of two subsumption entries. The LEFT1 and
26489 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26490 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26493 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26495 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26496 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26497 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26498 CHECK_CONSTR_ARGS (right1
)))
26499 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26500 CHECK_CONSTR_ARGS (right2
));
26504 /* Key/value pair for learning and memoizing subsumption results. This
26505 associates a pair of check constraints (including arguments) with
26506 a boolean value indicating the result. */
26508 struct GTY((for_user
)) subsumption_entry
26515 /* Hashing function and equality for constraint entries. */
26517 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26519 static hashval_t
hash (subsumption_entry
*e
)
26521 return hash_subsumption_args (e
->t1
, e
->t2
);
26524 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26526 ++comparing_specializations
;
26527 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26528 --comparing_specializations
;
26533 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26535 /* Search for a previously cached subsumption result. */
26538 lookup_subsumption_result (tree t1
, tree t2
)
26540 subsumption_entry elt
= { t1
, t2
, false };
26541 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26543 return &found
->result
;
26548 /* Save a subsumption result. */
26551 save_subsumption_result (tree t1
, tree t2
, bool result
)
26553 subsumption_entry elt
= {t1
, t2
, result
};
26554 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26555 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26561 /* Set up the hash table for constraint association. */
26564 init_constraint_processing (void)
26566 if (!flag_concepts
)
26569 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26570 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26571 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26572 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26573 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26576 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26580 declare_integer_pack (void)
26582 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26583 build_function_type_list (integer_type_node
,
26586 NULL_TREE
, ECF_CONST
);
26587 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26588 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26591 /* Set up the hash tables for template instantiations. */
26594 init_template_processing (void)
26596 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26597 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26599 if (cxx_dialect
>= cxx11
)
26600 declare_integer_pack ();
26603 /* Print stats about the template hash tables for -fstats. */
26606 print_template_statistics (void)
26608 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26609 "%f collisions\n", (long) decl_specializations
->size (),
26610 (long) decl_specializations
->elements (),
26611 decl_specializations
->collisions ());
26612 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26613 "%f collisions\n", (long) type_specializations
->size (),
26614 (long) type_specializations
->elements (),
26615 type_specializations
->collisions ());
26618 #include "gt-cp-pt.h"