1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
81 local_specialization_stack::local_specialization_stack (lss_policy policy
)
82 : saved (local_specializations
)
84 if (policy
== lss_blank
|| !saved
)
85 local_specializations
= new hash_map
<tree
, tree
>;
87 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations
;
93 local_specializations
= saved
;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth
;
99 struct GTY((for_user
)) spec_entry
106 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
108 static hashval_t
hash (spec_entry
*);
109 static bool equal (spec_entry
*, spec_entry
*);
112 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
114 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result
{
133 tbr_ambiguous_baseclass
,
137 static void push_access_scope (tree
);
138 static void pop_access_scope (tree
);
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
153 static void tsubst_enum (tree
, tree
, tree
);
154 static tree
add_to_template_args (tree
, tree
);
155 static tree
add_outermost_template_args (tree
, tree
);
156 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
157 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
, int,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static void template_parm_level_and_index (tree
, int*, int*);
184 static int unify_pack_expansion (tree
, tree
, tree
,
185 tree
, unification_kind_t
, bool, bool);
186 static tree
copy_template_args (tree
);
187 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
189 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
190 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
191 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
192 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
193 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
194 static bool check_specialization_scope (void);
195 static tree
process_partial_specialization (tree
);
196 static void set_current_access_from_decl (tree
);
197 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
199 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
200 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
202 static bool template_template_parm_bindings_ok_p (tree
, tree
);
203 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
204 static tree
for_each_template_parm_r (tree
*, int *, void *);
205 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
206 static void copy_default_args_to_explicit_spec (tree
);
207 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
208 static bool dependent_template_arg_p (tree
);
209 static bool any_template_arguments_need_structural_equality_p (tree
);
210 static bool dependent_type_p_r (tree
);
211 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
212 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
213 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
214 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
216 static tree
listify (tree
);
217 static tree
listify_autos (tree
, tree
);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
220 static bool complex_alias_template_p (const_tree tmpl
);
221 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
222 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
223 static tree
make_argument_pack (tree
);
224 static void register_parameter_specializations (tree
, tree
);
226 /* Make the current scope suitable for access checking when we are
227 processing T. T can be FUNCTION_DECL for instantiated function
228 template, VAR_DECL for static member variable, or TYPE_DECL for
229 alias template (needed by instantiate_decl). */
232 push_access_scope (tree t
)
234 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
235 || TREE_CODE (t
) == TYPE_DECL
);
237 if (DECL_FRIEND_CONTEXT (t
))
238 push_nested_class (DECL_FRIEND_CONTEXT (t
));
239 else if (DECL_CLASS_SCOPE_P (t
))
240 push_nested_class (DECL_CONTEXT (t
));
242 push_to_top_level ();
244 if (TREE_CODE (t
) == FUNCTION_DECL
)
246 saved_access_scope
= tree_cons
247 (NULL_TREE
, current_function_decl
, saved_access_scope
);
248 current_function_decl
= t
;
252 /* Restore the scope set up by push_access_scope. T is the node we
256 pop_access_scope (tree t
)
258 if (TREE_CODE (t
) == FUNCTION_DECL
)
260 current_function_decl
= TREE_VALUE (saved_access_scope
);
261 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
264 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
267 pop_from_top_level ();
270 /* Do any processing required when DECL (a member template
271 declaration) is finished. Returns the TEMPLATE_DECL corresponding
272 to DECL, unless it is a specialization, in which case the DECL
273 itself is returned. */
276 finish_member_template_decl (tree decl
)
278 if (decl
== error_mark_node
)
279 return error_mark_node
;
281 gcc_assert (DECL_P (decl
));
283 if (TREE_CODE (decl
) == TYPE_DECL
)
287 type
= TREE_TYPE (decl
);
288 if (type
== error_mark_node
)
289 return error_mark_node
;
290 if (MAYBE_CLASS_TYPE_P (type
)
291 && CLASSTYPE_TEMPLATE_INFO (type
)
292 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
294 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
295 check_member_template (tmpl
);
300 else if (TREE_CODE (decl
) == FIELD_DECL
)
301 error ("data member %qD cannot be a member template", decl
);
302 else if (DECL_TEMPLATE_INFO (decl
))
304 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
306 check_member_template (DECL_TI_TEMPLATE (decl
));
307 return DECL_TI_TEMPLATE (decl
);
313 error ("invalid member template declaration %qD", decl
);
315 return error_mark_node
;
318 /* Create a template info node. */
321 build_template_info (tree template_decl
, tree template_args
)
323 tree result
= make_node (TEMPLATE_INFO
);
324 TI_TEMPLATE (result
) = template_decl
;
325 TI_ARGS (result
) = template_args
;
329 /* Return the template info node corresponding to T, whatever T is. */
332 get_template_info (const_tree t
)
334 tree tinfo
= NULL_TREE
;
336 if (!t
|| t
== error_mark_node
)
339 if (TREE_CODE (t
) == NAMESPACE_DECL
340 || TREE_CODE (t
) == PARM_DECL
)
343 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
344 tinfo
= DECL_TEMPLATE_INFO (t
);
346 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
349 if (OVERLOAD_TYPE_P (t
))
350 tinfo
= TYPE_TEMPLATE_INFO (t
);
351 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
352 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
357 /* Returns the template nesting level of the indicated class TYPE.
367 A<T>::B<U> has depth two, while A<T> has depth one.
368 Both A<T>::B<int> and A<int>::B<U> have depth one, if
369 they are instantiations, not specializations.
371 This function is guaranteed to return 0 if passed NULL_TREE so
372 that, for example, `template_class_depth (current_class_type)' is
376 template_class_depth (tree type
)
380 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
382 tree tinfo
= get_template_info (type
);
384 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
385 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
389 type
= CP_DECL_CONTEXT (type
);
390 else if (LAMBDA_TYPE_P (type
))
391 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
393 type
= CP_TYPE_CONTEXT (type
);
399 /* Subroutine of maybe_begin_member_template_processing.
400 Returns true if processing DECL needs us to push template parms. */
403 inline_needs_template_parms (tree decl
, bool nsdmi
)
405 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
408 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
409 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
412 /* Subroutine of maybe_begin_member_template_processing.
413 Push the template parms in PARMS, starting from LEVELS steps into the
414 chain, and ending at the beginning, since template parms are listed
418 push_inline_template_parms_recursive (tree parmlist
, int levels
)
420 tree parms
= TREE_VALUE (parmlist
);
424 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
426 ++processing_template_decl
;
427 current_template_parms
428 = tree_cons (size_int (processing_template_decl
),
429 parms
, current_template_parms
);
430 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
432 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
434 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
436 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
438 if (error_operand_p (parm
))
441 gcc_assert (DECL_P (parm
));
443 switch (TREE_CODE (parm
))
451 /* Push the CONST_DECL. */
452 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
461 /* Restore the template parameter context for a member template, a
462 friend template defined in a class definition, or a non-template
463 member of template class. */
466 maybe_begin_member_template_processing (tree decl
)
470 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
474 tree ctx
= DECL_CONTEXT (decl
);
475 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
476 /* Disregard full specializations (c++/60999). */
477 && uses_template_parms (ctx
)
478 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
481 if (inline_needs_template_parms (decl
, nsdmi
))
483 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
484 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
486 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
489 parms
= TREE_CHAIN (parms
);
492 push_inline_template_parms_recursive (parms
, levels
);
495 /* Remember how many levels of template parameters we pushed so that
496 we can pop them later. */
497 inline_parm_levels
.safe_push (levels
);
500 /* Undo the effects of maybe_begin_member_template_processing. */
503 maybe_end_member_template_processing (void)
508 if (inline_parm_levels
.length () == 0)
511 last
= inline_parm_levels
.pop ();
512 for (i
= 0; i
< last
; ++i
)
514 --processing_template_decl
;
515 current_template_parms
= TREE_CHAIN (current_template_parms
);
520 /* Return a new template argument vector which contains all of ARGS,
521 but has as its innermost set of arguments the EXTRA_ARGS. */
524 add_to_template_args (tree args
, tree extra_args
)
531 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
534 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
535 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
537 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
538 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
540 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
541 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
546 /* Like add_to_template_args, but only the outermost ARGS are added to
547 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
548 (EXTRA_ARGS) levels are added. This function is used to combine
549 the template arguments from a partial instantiation with the
550 template arguments used to attain the full instantiation from the
551 partial instantiation. */
554 add_outermost_template_args (tree args
, tree extra_args
)
558 /* If there are more levels of EXTRA_ARGS than there are ARGS,
559 something very fishy is going on. */
560 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
562 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
567 /* For the moment, we make ARGS look like it contains fewer levels. */
568 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
570 new_args
= add_to_template_args (args
, extra_args
);
572 /* Now, we restore ARGS to its full dimensions. */
573 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
578 /* Return the N levels of innermost template arguments from the ARGS. */
581 get_innermost_template_args (tree args
, int n
)
589 /* If N is 1, just return the innermost set of template arguments. */
591 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
593 /* If we're not removing anything, just return the arguments we were
595 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
596 gcc_assert (extra_levels
>= 0);
597 if (extra_levels
== 0)
600 /* Make a new set of arguments, not containing the outer arguments. */
601 new_args
= make_tree_vec (n
);
602 for (i
= 1; i
<= n
; ++i
)
603 SET_TMPL_ARGS_LEVEL (new_args
, i
,
604 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
609 /* The inverse of get_innermost_template_args: Return all but the innermost
610 EXTRA_LEVELS levels of template arguments from the ARGS. */
613 strip_innermost_template_args (tree args
, int extra_levels
)
616 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
621 /* If N is 1, just return the outermost set of template arguments. */
623 return TMPL_ARGS_LEVEL (args
, 1);
625 /* If we're not removing anything, just return the arguments we were
627 gcc_assert (extra_levels
>= 0);
628 if (extra_levels
== 0)
631 /* Make a new set of arguments, not containing the inner arguments. */
632 new_args
= make_tree_vec (n
);
633 for (i
= 1; i
<= n
; ++i
)
634 SET_TMPL_ARGS_LEVEL (new_args
, i
,
635 TMPL_ARGS_LEVEL (args
, i
));
640 /* We've got a template header coming up; push to a new level for storing
644 begin_template_parm_list (void)
646 /* We use a non-tag-transparent scope here, which causes pushtag to
647 put tags in this scope, rather than in the enclosing class or
648 namespace scope. This is the right thing, since we want
649 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
650 global template class, push_template_decl handles putting the
651 TEMPLATE_DECL into top-level scope. For a nested template class,
654 template <class T> struct S1 {
655 template <class T> struct S2 {};
658 pushtag contains special code to insert the TEMPLATE_DECL for S2
659 at the right scope. */
660 begin_scope (sk_template_parms
, NULL
);
661 ++processing_template_decl
;
662 ++processing_template_parmlist
;
663 note_template_header (0);
665 /* Add a dummy parameter level while we process the parameter list. */
666 current_template_parms
667 = tree_cons (size_int (processing_template_decl
),
669 current_template_parms
);
672 /* This routine is called when a specialization is declared. If it is
673 invalid to declare a specialization here, an error is reported and
674 false is returned, otherwise this routine will return true. */
677 check_specialization_scope (void)
679 tree scope
= current_scope ();
683 An explicit specialization shall be declared in the namespace of
684 which the template is a member, or, for member templates, in the
685 namespace of which the enclosing class or enclosing class
686 template is a member. An explicit specialization of a member
687 function, member class or static data member of a class template
688 shall be declared in the namespace of which the class template
690 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
692 error ("explicit specialization in non-namespace scope %qD", scope
);
698 In an explicit specialization declaration for a member of a class
699 template or a member template that appears in namespace scope,
700 the member template and some of its enclosing class templates may
701 remain unspecialized, except that the declaration shall not
702 explicitly specialize a class member template if its enclosing
703 class templates are not explicitly specialized as well. */
704 if (current_template_parms
)
706 error ("enclosing class templates are not explicitly specialized");
713 /* We've just seen template <>. */
716 begin_specialization (void)
718 begin_scope (sk_template_spec
, NULL
);
719 note_template_header (1);
720 return check_specialization_scope ();
723 /* Called at then end of processing a declaration preceded by
727 end_specialization (void)
730 reset_specialization ();
733 /* Any template <>'s that we have seen thus far are not referring to a
734 function specialization. */
737 reset_specialization (void)
739 processing_specialization
= 0;
740 template_header_count
= 0;
743 /* We've just seen a template header. If SPECIALIZATION is nonzero,
744 it was of the form template <>. */
747 note_template_header (int specialization
)
749 processing_specialization
= specialization
;
750 template_header_count
++;
753 /* We're beginning an explicit instantiation. */
756 begin_explicit_instantiation (void)
758 gcc_assert (!processing_explicit_instantiation
);
759 processing_explicit_instantiation
= true;
764 end_explicit_instantiation (void)
766 gcc_assert (processing_explicit_instantiation
);
767 processing_explicit_instantiation
= false;
770 /* An explicit specialization or partial specialization of TMPL is being
771 declared. Check that the namespace in which the specialization is
772 occurring is permissible. Returns false iff it is invalid to
773 specialize TMPL in the current namespace. */
776 check_specialization_namespace (tree tmpl
)
778 tree tpl_ns
= decl_namespace_context (tmpl
);
782 An explicit specialization shall be declared in a namespace enclosing the
783 specialized template. An explicit specialization whose declarator-id is
784 not qualified shall be declared in the nearest enclosing namespace of the
785 template, or, if the namespace is inline (7.3.1), any namespace from its
786 enclosing namespace set. */
787 if (current_scope() != DECL_CONTEXT (tmpl
)
788 && !at_namespace_scope_p ())
790 error ("specialization of %qD must appear at namespace scope", tmpl
);
794 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
795 /* Same or enclosing namespace. */
799 permerror (input_location
,
800 "specialization of %qD in different namespace", tmpl
);
801 inform (DECL_SOURCE_LOCATION (tmpl
),
802 " from definition of %q#D", tmpl
);
807 /* SPEC is an explicit instantiation. Check that it is valid to
808 perform this explicit instantiation in the current namespace. */
811 check_explicit_instantiation_namespace (tree spec
)
815 /* DR 275: An explicit instantiation shall appear in an enclosing
816 namespace of its template. */
817 ns
= decl_namespace_context (spec
);
818 if (!is_nested_namespace (current_namespace
, ns
))
819 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
820 "(which does not enclose namespace %qD)",
821 spec
, current_namespace
, ns
);
824 // Returns the type of a template specialization only if that
825 // specialization needs to be defined. Otherwise (e.g., if the type has
826 // already been defined), the function returns NULL_TREE.
828 maybe_new_partial_specialization (tree type
)
830 // An implicit instantiation of an incomplete type implies
831 // the definition of a new class template.
833 // template<typename T>
836 // template<typename T>
839 // Here, S<T*> is an implicit instantiation of S whose type
841 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
844 // It can also be the case that TYPE is a completed specialization.
845 // Continuing the previous example, suppose we also declare:
847 // template<typename T>
848 // requires Integral<T>
851 // Here, S<T*> refers to the specialization S<T*> defined
852 // above. However, we need to differentiate definitions because
853 // we intend to define a new partial specialization. In this case,
854 // we rely on the fact that the constraints are different for
855 // this declaration than that above.
857 // Note that we also get here for injected class names and
858 // late-parsed template definitions. We must ensure that we
859 // do not create new type declarations for those cases.
860 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
862 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
863 tree args
= CLASSTYPE_TI_ARGS (type
);
865 // If there are no template parameters, this cannot be a new
866 // partial template specializtion?
867 if (!current_template_parms
)
870 // The injected-class-name is not a new partial specialization.
871 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
874 // If the constraints are not the same as those of the primary
875 // then, we can probably create a new specialization.
876 tree type_constr
= current_template_constraints ();
878 if (type
== TREE_TYPE (tmpl
))
880 tree main_constr
= get_constraints (tmpl
);
881 if (equivalent_constraints (type_constr
, main_constr
))
885 // Also, if there's a pre-existing specialization with matching
886 // constraints, then this also isn't new.
887 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
890 tree spec_tmpl
= TREE_VALUE (specs
);
891 tree spec_args
= TREE_PURPOSE (specs
);
892 tree spec_constr
= get_constraints (spec_tmpl
);
893 if (comp_template_args (args
, spec_args
)
894 && equivalent_constraints (type_constr
, spec_constr
))
896 specs
= TREE_CHAIN (specs
);
899 // Create a new type node (and corresponding type decl)
900 // for the newly declared specialization.
901 tree t
= make_class_type (TREE_CODE (type
));
902 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
903 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
905 /* We only need a separate type node for storing the definition of this
906 partial specialization; uses of S<T*> are unconstrained, so all are
907 equivalent. So keep TYPE_CANONICAL the same. */
908 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
910 // Build the corresponding type decl.
911 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
912 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
913 DECL_SOURCE_LOCATION (d
) = input_location
;
921 /* The TYPE is being declared. If it is a template type, that means it
922 is a partial specialization. Do appropriate error-checking. */
925 maybe_process_partial_specialization (tree type
)
929 if (type
== error_mark_node
)
930 return error_mark_node
;
932 /* A lambda that appears in specialization context is not itself a
934 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
937 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
939 error ("name of class shadows template template parameter %qD",
941 return error_mark_node
;
944 context
= TYPE_CONTEXT (type
);
946 if (TYPE_ALIAS_P (type
))
948 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
950 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
951 error ("specialization of alias template %qD",
952 TI_TEMPLATE (tinfo
));
954 error ("explicit specialization of non-template %qT", type
);
955 return error_mark_node
;
957 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
959 /* This is for ordinary explicit specialization and partial
960 specialization of a template class such as:
962 template <> class C<int>;
966 template <class T> class C<T*>;
968 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
970 if (tree t
= maybe_new_partial_specialization (type
))
972 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
973 && !at_namespace_scope_p ())
974 return error_mark_node
;
975 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
976 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
977 if (processing_template_decl
)
979 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
980 if (decl
== error_mark_node
)
981 return error_mark_node
;
982 return TREE_TYPE (decl
);
985 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
986 error ("specialization of %qT after instantiation", type
);
987 else if (errorcount
&& !processing_specialization
988 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
989 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
990 /* Trying to define a specialization either without a template<> header
991 or in an inappropriate place. We've already given an error, so just
992 bail now so we don't actually define the specialization. */
993 return error_mark_node
;
995 else if (CLASS_TYPE_P (type
)
996 && !CLASSTYPE_USE_TEMPLATE (type
)
997 && CLASSTYPE_TEMPLATE_INFO (type
)
998 && context
&& CLASS_TYPE_P (context
)
999 && CLASSTYPE_TEMPLATE_INFO (context
))
1001 /* This is for an explicit specialization of member class
1002 template according to [temp.expl.spec/18]:
1004 template <> template <class U> class C<int>::D;
1006 The context `C<int>' must be an implicit instantiation.
1007 Otherwise this is just a member class template declared
1010 template <> class C<int> { template <class U> class D; };
1011 template <> template <class U> class C<int>::D;
1013 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1014 while in the second case, `C<int>::D' is a primary template
1015 and `C<T>::D' may not exist. */
1017 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1018 && !COMPLETE_TYPE_P (type
))
1021 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1023 if (current_namespace
1024 != decl_namespace_context (tmpl
))
1026 permerror (input_location
,
1027 "specializing %q#T in different namespace", type
);
1028 permerror (DECL_SOURCE_LOCATION (tmpl
),
1029 " from definition of %q#D", tmpl
);
1032 /* Check for invalid specialization after instantiation:
1034 template <> template <> class C<int>::D<int>;
1035 template <> template <class U> class C<int>::D; */
1037 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1038 t
; t
= TREE_CHAIN (t
))
1040 tree inst
= TREE_VALUE (t
);
1041 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1042 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1044 /* We already have a full specialization of this partial
1045 instantiation, or a full specialization has been
1046 looked up but not instantiated. Reassign it to the
1047 new member specialization template. */
1051 elt
.tmpl
= most_general_template (tmpl
);
1052 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1055 type_specializations
->remove_elt (&elt
);
1058 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1061 = type_specializations
->find_slot (&elt
, INSERT
);
1062 entry
= ggc_alloc
<spec_entry
> ();
1067 /* But if we've had an implicit instantiation, that's a
1068 problem ([temp.expl.spec]/6). */
1069 error ("specialization %qT after instantiation %qT",
1073 /* Mark TYPE as a specialization. And as a result, we only
1074 have one level of template argument for the innermost
1076 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1077 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1078 CLASSTYPE_TI_ARGS (type
)
1079 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1082 else if (processing_specialization
)
1084 /* Someday C++0x may allow for enum template specialization. */
1085 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1086 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1087 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1088 "of %qD not allowed by ISO C++", type
);
1091 error ("explicit specialization of non-template %qT", type
);
1092 return error_mark_node
;
1099 /* Returns nonzero if we can optimize the retrieval of specializations
1100 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1101 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1104 optimize_specialization_lookup_p (tree tmpl
)
1106 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1107 && DECL_CLASS_SCOPE_P (tmpl
)
1108 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1111 /* The optimized lookup depends on the fact that the
1112 template arguments for the member function template apply
1113 purely to the containing class, which is not true if the
1114 containing class is an explicit or partial
1116 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1117 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1118 && !DECL_CONV_FN_P (tmpl
)
1119 /* It is possible to have a template that is not a member
1120 template and is not a member of a template class:
1122 template <typename T>
1123 struct S { friend A::f(); };
1125 Here, the friend function is a template, but the context does
1126 not have template information. The optimized lookup relies
1127 on having ARGS be the template arguments for both the class
1128 and the function template. */
1129 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1132 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1133 gone through coerce_template_parms by now. */
1136 verify_unstripped_args (tree args
)
1138 ++processing_template_decl
;
1139 if (!any_dependent_template_arguments_p (args
))
1141 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1142 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1144 tree arg
= TREE_VEC_ELT (inner
, i
);
1145 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1147 else if (TYPE_P (arg
))
1148 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1149 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1150 /* Allow typedefs on the type of a non-type argument, since a
1151 parameter can have them. */;
1153 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1156 --processing_template_decl
;
1159 /* Retrieve the specialization (in the sense of [temp.spec] - a
1160 specialization is either an instantiation or an explicit
1161 specialization) of TMPL for the given template ARGS. If there is
1162 no such specialization, return NULL_TREE. The ARGS are a vector of
1163 arguments, or a vector of vectors of arguments, in the case of
1164 templates with more than one level of parameters.
1166 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1167 then we search for a partial specialization matching ARGS. This
1168 parameter is ignored if TMPL is not a class template.
1170 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1171 result is a NONTYPE_ARGUMENT_PACK. */
1174 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1176 if (tmpl
== NULL_TREE
)
1179 if (args
== error_mark_node
)
1182 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1183 || TREE_CODE (tmpl
) == FIELD_DECL
);
1185 /* There should be as many levels of arguments as there are
1186 levels of parameters. */
1187 gcc_assert (TMPL_ARGS_DEPTH (args
)
1188 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1189 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1190 : template_class_depth (DECL_CONTEXT (tmpl
))));
1193 verify_unstripped_args (args
);
1195 /* Lambda functions in templates aren't instantiated normally, but through
1196 tsubst_lambda_expr. */
1197 if (lambda_fn_in_template_p (tmpl
))
1200 if (optimize_specialization_lookup_p (tmpl
))
1202 /* The template arguments actually apply to the containing
1203 class. Find the class specialization with those
1205 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1206 tree class_specialization
1207 = retrieve_specialization (class_template
, args
, 0);
1208 if (!class_specialization
)
1211 /* Find the instance of TMPL. */
1212 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1213 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1216 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1217 /* using-declarations can add base methods to the method vec,
1218 and we don't want those here. */
1219 && DECL_CONTEXT (fn
) == class_specialization
)
1228 hash_table
<spec_hasher
> *specializations
;
1232 elt
.spec
= NULL_TREE
;
1234 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1235 specializations
= type_specializations
;
1237 specializations
= decl_specializations
;
1240 hash
= spec_hasher::hash (&elt
);
1241 found
= specializations
->find_with_hash (&elt
, hash
);
1249 /* Like retrieve_specialization, but for local declarations. */
1252 retrieve_local_specialization (tree tmpl
)
1254 if (local_specializations
== NULL
)
1257 tree
*slot
= local_specializations
->get (tmpl
);
1258 return slot
? *slot
: NULL_TREE
;
1261 /* Returns nonzero iff DECL is a specialization of TMPL. */
1264 is_specialization_of (tree decl
, tree tmpl
)
1268 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1272 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1278 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1280 for (t
= TREE_TYPE (decl
);
1282 t
= CLASSTYPE_USE_TEMPLATE (t
)
1283 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1284 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1291 /* Returns nonzero iff DECL is a specialization of friend declaration
1292 FRIEND_DECL according to [temp.friend]. */
1295 is_specialization_of_friend (tree decl
, tree friend_decl
)
1297 bool need_template
= true;
1300 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1301 || TREE_CODE (decl
) == TYPE_DECL
);
1303 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1304 of a template class, we want to check if DECL is a specialization
1306 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1307 && DECL_TEMPLATE_INFO (friend_decl
)
1308 && !DECL_USE_TEMPLATE (friend_decl
))
1310 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1311 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1312 need_template
= false;
1314 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1315 && !PRIMARY_TEMPLATE_P (friend_decl
))
1316 need_template
= false;
1318 /* There is nothing to do if this is not a template friend. */
1319 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1322 if (is_specialization_of (decl
, friend_decl
))
1326 A member of a class template may be declared to be a friend of a
1327 non-template class. In this case, the corresponding member of
1328 every specialization of the class template is a friend of the
1329 class granting friendship.
1331 For example, given a template friend declaration
1333 template <class T> friend void A<T>::f();
1335 the member function below is considered a friend
1337 template <> struct A<int> {
1341 For this type of template friend, TEMPLATE_DEPTH below will be
1342 nonzero. To determine if DECL is a friend of FRIEND, we first
1343 check if the enclosing class is a specialization of another. */
1345 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1347 && DECL_CLASS_SCOPE_P (decl
)
1348 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1349 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1351 /* Next, we check the members themselves. In order to handle
1352 a few tricky cases, such as when FRIEND_DECL's are
1354 template <class T> friend void A<T>::g(T t);
1355 template <class T> template <T t> friend void A<T>::h();
1359 void A<int>::g(int);
1360 template <int> void A<int>::h();
1362 we need to figure out ARGS, the template arguments from
1363 the context of DECL. This is required for template substitution
1364 of `T' in the function parameter of `g' and template parameter
1365 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367 tree context
= DECL_CONTEXT (decl
);
1368 tree args
= NULL_TREE
;
1369 int current_depth
= 0;
1371 while (current_depth
< template_depth
)
1373 if (CLASSTYPE_TEMPLATE_INFO (context
))
1375 if (current_depth
== 0)
1376 args
= TYPE_TI_ARGS (context
);
1378 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1381 context
= TYPE_CONTEXT (context
);
1384 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1389 tree friend_args_type
;
1390 tree decl_args_type
;
1392 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 is_template
= DECL_TEMPLATE_INFO (decl
)
1395 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1396 if (need_template
^ is_template
)
1398 else if (is_template
)
1400 /* If both are templates, check template parameter list. */
1402 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1404 if (!comp_template_parms
1405 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1409 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1412 decl_type
= TREE_TYPE (decl
);
1414 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1415 tf_none
, NULL_TREE
);
1416 if (friend_type
== error_mark_node
)
1419 /* Check if return types match. */
1420 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1423 /* Check if function parameter types match, ignoring the
1424 `this' parameter. */
1425 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1426 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1427 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1428 friend_args_type
= TREE_CHAIN (friend_args_type
);
1429 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1430 decl_args_type
= TREE_CHAIN (decl_args_type
);
1432 return compparms (decl_args_type
, friend_args_type
);
1436 /* DECL is a TYPE_DECL */
1438 tree decl_type
= TREE_TYPE (decl
);
1440 /* Make sure that both DECL and FRIEND_DECL are templates or
1443 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1444 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1446 if (need_template
^ is_template
)
1448 else if (is_template
)
1451 /* If both are templates, check the name of the two
1452 TEMPLATE_DECL's first because is_friend didn't. */
1453 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1454 != DECL_NAME (friend_decl
))
1457 /* Now check template parameter list. */
1459 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1461 return comp_template_parms
1462 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1466 return (DECL_NAME (decl
)
1467 == DECL_NAME (friend_decl
));
1473 /* Register the specialization SPEC as a specialization of TMPL with
1474 the indicated ARGS. IS_FRIEND indicates whether the specialization
1475 is actually just a friend declaration. Returns SPEC, or an
1476 equivalent prior declaration, if available.
1478 We also store instantiations of field packs in the hash table, even
1479 though they are not themselves templates, to make lookup easier. */
1482 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1486 spec_entry
**slot
= NULL
;
1489 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1490 || (TREE_CODE (tmpl
) == FIELD_DECL
1491 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1493 if (TREE_CODE (spec
) == FUNCTION_DECL
1494 && uses_template_parms (DECL_TI_ARGS (spec
)))
1495 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1496 register it; we want the corresponding TEMPLATE_DECL instead.
1497 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1498 the more obvious `uses_template_parms (spec)' to avoid problems
1499 with default function arguments. In particular, given
1500 something like this:
1502 template <class T> void f(T t1, T t = T())
1504 the default argument expression is not substituted for in an
1505 instantiation unless and until it is actually needed. */
1508 if (optimize_specialization_lookup_p (tmpl
))
1509 /* We don't put these specializations in the hash table, but we might
1510 want to give an error about a mismatch. */
1511 fn
= retrieve_specialization (tmpl
, args
, 0);
1519 hash
= spec_hasher::hash (&elt
);
1522 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1524 fn
= ((spec_entry
*) *slot
)->spec
;
1529 /* We can sometimes try to re-register a specialization that we've
1530 already got. In particular, regenerate_decl_from_template calls
1531 duplicate_decls which will update the specialization list. But,
1532 we'll still get called again here anyhow. It's more convenient
1533 to simply allow this than to try to prevent it. */
1536 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1538 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1540 if (DECL_ODR_USED (fn
)
1541 || DECL_EXPLICIT_INSTANTIATION (fn
))
1543 error ("specialization of %qD after instantiation",
1545 return error_mark_node
;
1550 /* This situation should occur only if the first
1551 specialization is an implicit instantiation, the
1552 second is an explicit specialization, and the
1553 implicit instantiation has not yet been used. That
1554 situation can occur if we have implicitly
1555 instantiated a member function and then specialized
1558 We can also wind up here if a friend declaration that
1559 looked like an instantiation turns out to be a
1562 template <class T> void foo(T);
1563 class S { friend void foo<>(int) };
1564 template <> void foo(int);
1566 We transform the existing DECL in place so that any
1567 pointers to it become pointers to the updated
1570 If there was a definition for the template, but not
1571 for the specialization, we want this to look as if
1572 there were no definition, and vice versa. */
1573 DECL_INITIAL (fn
) = NULL_TREE
;
1574 duplicate_decls (spec
, fn
, is_friend
);
1575 /* The call to duplicate_decls will have applied
1578 An explicit specialization of a function template
1579 is inline only if it is explicitly declared to be,
1580 and independently of whether its function template
1583 to the primary function; now copy the inline bits to
1584 the various clones. */
1585 FOR_EACH_CLONE (clone
, fn
)
1587 DECL_DECLARED_INLINE_P (clone
)
1588 = DECL_DECLARED_INLINE_P (fn
);
1589 DECL_SOURCE_LOCATION (clone
)
1590 = DECL_SOURCE_LOCATION (fn
);
1591 DECL_DELETED_FN (clone
)
1592 = DECL_DELETED_FN (fn
);
1594 check_specialization_namespace (tmpl
);
1599 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1601 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1602 if (dd
== error_mark_node
)
1603 /* We've already complained in duplicate_decls. */
1604 return error_mark_node
;
1606 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1607 /* Dup decl failed, but this is a new definition. Set the
1608 line number so any errors match this new
1610 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1616 return duplicate_decls (spec
, fn
, is_friend
);
1618 /* A specialization must be declared in the same namespace as the
1619 template it is specializing. */
1620 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1621 && !check_specialization_namespace (tmpl
))
1622 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1624 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1626 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1627 gcc_assert (tmpl
&& args
&& spec
);
1630 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1631 && PRIMARY_TEMPLATE_P (tmpl
)
1632 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1633 || variable_template_p (tmpl
))
1634 /* If TMPL is a forward declaration of a template function, keep a list
1635 of all specializations in case we need to reassign them to a friend
1636 template later in tsubst_friend_function.
1638 Also keep a list of all variable template instantiations so that
1639 process_partial_specialization can check whether a later partial
1640 specialization would have used it. */
1641 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1642 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1648 /* Returns true iff two spec_entry nodes are equivalent. */
1650 int comparing_specializations
;
1653 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1657 ++comparing_specializations
;
1658 equal
= (e1
->tmpl
== e2
->tmpl
1659 && comp_template_args (e1
->args
, e2
->args
));
1660 if (equal
&& flag_concepts
1661 /* tmpl could be a FIELD_DECL for a capture pack. */
1662 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1663 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1664 && uses_template_parms (e1
->args
))
1666 /* Partial specializations of a variable template can be distinguished by
1668 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1669 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1670 equal
= equivalent_constraints (c1
, c2
);
1672 --comparing_specializations
;
1677 /* Returns a hash for a template TMPL and template arguments ARGS. */
1680 hash_tmpl_and_args (tree tmpl
, tree args
)
1682 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1683 return iterative_hash_template_arg (args
, val
);
1686 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1690 spec_hasher::hash (spec_entry
*e
)
1692 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1695 /* Recursively calculate a hash value for a template argument ARG, for use
1696 in the hash tables of template specializations. */
1699 iterative_hash_template_arg (tree arg
, hashval_t val
)
1701 unsigned HOST_WIDE_INT i
;
1702 enum tree_code code
;
1705 if (arg
== NULL_TREE
)
1706 return iterative_hash_object (arg
, val
);
1711 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1714 code
= TREE_CODE (arg
);
1715 tclass
= TREE_CODE_CLASS (code
);
1717 val
= iterative_hash_object (code
, val
);
1724 case IDENTIFIER_NODE
:
1725 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1729 int i
, len
= TREE_VEC_LENGTH (arg
);
1730 for (i
= 0; i
< len
; ++i
)
1731 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1735 case TYPE_PACK_EXPANSION
:
1736 case EXPR_PACK_EXPANSION
:
1737 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1738 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1740 case TYPE_ARGUMENT_PACK
:
1741 case NONTYPE_ARGUMENT_PACK
:
1742 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1745 for (; arg
; arg
= TREE_CHAIN (arg
))
1746 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1750 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1751 val
= iterative_hash_template_arg (*iter
, val
);
1757 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1758 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1760 val
= iterative_hash_template_arg (field
, val
);
1761 val
= iterative_hash_template_arg (value
, val
);
1767 if (!DECL_ARTIFICIAL (arg
))
1769 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1770 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1772 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1775 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1778 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1779 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1781 case TEMPLATE_PARM_INDEX
:
1782 val
= iterative_hash_template_arg
1783 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1784 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1785 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1788 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1789 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1790 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1793 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1795 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1799 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1800 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1801 val
= iterative_hash_object (code
, val
);
1802 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1805 /* A lambda can't appear in a template arg, but don't crash on
1807 gcc_assert (seen_error ());
1811 case IMPLICIT_CONV_EXPR
:
1812 case STATIC_CAST_EXPR
:
1813 case REINTERPRET_CAST_EXPR
:
1814 case CONST_CAST_EXPR
:
1815 case DYNAMIC_CAST_EXPR
:
1817 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1818 /* Now hash operands as usual. */
1828 if (alias_template_specialization_p (arg
))
1830 // We want an alias specialization that survived strip_typedefs
1831 // to hash differently from its TYPE_CANONICAL, to avoid hash
1832 // collisions that compare as different in template_args_equal.
1833 // These could be dependent specializations that strip_typedefs
1834 // left alone, or untouched specializations because
1835 // coerce_template_parms returns the unconverted template
1836 // arguments if it sees incomplete argument packs.
1837 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1838 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1840 if (TYPE_CANONICAL (arg
))
1841 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1843 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1844 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1845 /* Otherwise just compare the types during lookup. */
1848 case tcc_declaration
:
1850 return iterative_hash_expr (arg
, val
);
1853 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1855 unsigned n
= cp_tree_operand_length (arg
);
1856 for (i
= 0; i
< n
; ++i
)
1857 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1865 /* Unregister the specialization SPEC as a specialization of TMPL.
1866 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1867 if the SPEC was listed as a specialization of TMPL.
1869 Note that SPEC has been ggc_freed, so we can't look inside it. */
1872 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1877 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1878 elt
.args
= TI_ARGS (tinfo
);
1879 elt
.spec
= NULL_TREE
;
1881 entry
= decl_specializations
->find (&elt
);
1884 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1885 gcc_assert (new_spec
!= NULL_TREE
);
1886 entry
->spec
= new_spec
;
1893 /* Like register_specialization, but for local declarations. We are
1894 registering SPEC, an instantiation of TMPL. */
1897 register_local_specialization (tree spec
, tree tmpl
)
1899 gcc_assert (tmpl
!= spec
);
1900 local_specializations
->put (tmpl
, spec
);
1903 /* TYPE is a class type. Returns true if TYPE is an explicitly
1904 specialized class. */
1907 explicit_class_specialization_p (tree type
)
1909 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1911 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1914 /* Print the list of functions at FNS, going through all the overloads
1915 for each element of the list. Alternatively, FNS can not be a
1916 TREE_LIST, in which case it will be printed together with all the
1919 MORE and *STR should respectively be FALSE and NULL when the function
1920 is called from the outside. They are used internally on recursive
1921 calls. print_candidates manages the two parameters and leaves NULL
1922 in *STR when it ends. */
1925 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1927 if (TREE_CODE (fns
) == TREE_LIST
)
1928 for (; fns
; fns
= TREE_CHAIN (fns
))
1929 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1931 for (lkp_iterator
iter (fns
); iter
;)
1936 const char *pfx
= *str
;
1940 pfx
= _("candidates are:");
1942 pfx
= _("candidate is:");
1943 *str
= get_spaces (pfx
);
1945 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1949 /* Print the list of candidate FNS in an error message. FNS can also
1950 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1953 print_candidates (tree fns
)
1956 print_candidates_1 (fns
, &str
);
1960 /* Get a (possibly) constrained template declaration for the
1961 purpose of ordering candidates. */
1963 get_template_for_ordering (tree list
)
1965 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1966 tree f
= TREE_VALUE (list
);
1967 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1968 return TI_TEMPLATE (ti
);
1972 /* Among candidates having the same signature, return the
1973 most constrained or NULL_TREE if there is no best candidate.
1974 If the signatures of candidates vary (e.g., template
1975 specialization vs. member function), then there can be no
1978 Note that we don't compare constraints on the functions
1979 themselves, but rather those of their templates. */
1981 most_constrained_function (tree candidates
)
1983 // Try to find the best candidate in a first pass.
1984 tree champ
= candidates
;
1985 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1987 int winner
= more_constrained (get_template_for_ordering (champ
),
1988 get_template_for_ordering (c
));
1990 champ
= c
; // The candidate is more constrained
1991 else if (winner
== 0)
1992 return NULL_TREE
; // Neither is more constrained
1995 // Verify that the champ is better than previous candidates.
1996 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
1997 if (!more_constrained (get_template_for_ordering (champ
),
1998 get_template_for_ordering (c
)))
2006 /* Returns the template (one of the functions given by TEMPLATE_ID)
2007 which can be specialized to match the indicated DECL with the
2008 explicit template args given in TEMPLATE_ID. The DECL may be
2009 NULL_TREE if none is available. In that case, the functions in
2010 TEMPLATE_ID are non-members.
2012 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2013 specialization of a member template.
2015 The TEMPLATE_COUNT is the number of references to qualifying
2016 template classes that appeared in the name of the function. See
2017 check_explicit_specialization for a more accurate description.
2019 TSK indicates what kind of template declaration (if any) is being
2020 declared. TSK_TEMPLATE indicates that the declaration given by
2021 DECL, though a FUNCTION_DECL, has template parameters, and is
2022 therefore a template function.
2024 The template args (those explicitly specified and those deduced)
2025 are output in a newly created vector *TARGS_OUT.
2027 If it is impossible to determine the result, an error message is
2028 issued. The error_mark_node is returned to indicate failure. */
2031 determine_specialization (tree template_id
,
2034 int need_member_template
,
2040 tree explicit_targs
;
2041 tree candidates
= NULL_TREE
;
2043 /* A TREE_LIST of templates of which DECL may be a specialization.
2044 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2045 corresponding TREE_PURPOSE is the set of template arguments that,
2046 when used to instantiate the template, would produce a function
2047 with the signature of DECL. */
2048 tree templates
= NULL_TREE
;
2050 cp_binding_level
*b
;
2052 *targs_out
= NULL_TREE
;
2054 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2055 return error_mark_node
;
2057 /* We shouldn't be specializing a member template of an
2058 unspecialized class template; we already gave an error in
2059 check_specialization_scope, now avoid crashing. */
2060 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2061 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2063 gcc_assert (errorcount
);
2064 return error_mark_node
;
2067 fns
= TREE_OPERAND (template_id
, 0);
2068 explicit_targs
= TREE_OPERAND (template_id
, 1);
2070 if (fns
== error_mark_node
)
2071 return error_mark_node
;
2073 /* Check for baselinks. */
2074 if (BASELINK_P (fns
))
2075 fns
= BASELINK_FUNCTIONS (fns
);
2077 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2079 error ("%qD is not a function template", fns
);
2080 return error_mark_node
;
2082 else if (VAR_P (decl
) && !variable_template_p (fns
))
2084 error ("%qD is not a variable template", fns
);
2085 return error_mark_node
;
2088 /* Count the number of template headers specified for this
2091 for (b
= current_binding_level
;
2092 b
->kind
== sk_template_parms
;
2096 tree orig_fns
= fns
;
2098 if (variable_template_p (fns
))
2100 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2101 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2102 tf_warning_or_error
,
2103 /*req_all*/true, /*use_defarg*/true);
2104 if (targs
!= error_mark_node
)
2105 templates
= tree_cons (targs
, fns
, templates
);
2107 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2111 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2113 tree decl_arg_types
;
2117 /* In case of explicit specialization, we need to check if
2118 the number of template headers appearing in the specialization
2119 is correct. This is usually done in check_explicit_specialization,
2120 but the check done there cannot be exhaustive when specializing
2121 member functions. Consider the following code:
2123 template <> void A<int>::f(int);
2124 template <> template <> void A<int>::f(int);
2126 Assuming that A<int> is not itself an explicit specialization
2127 already, the first line specializes "f" which is a non-template
2128 member function, whilst the second line specializes "f" which
2129 is a template member function. So both lines are syntactically
2130 correct, and check_explicit_specialization does not reject
2133 Here, we can do better, as we are matching the specialization
2134 against the declarations. We count the number of template
2135 headers, and we check if they match TEMPLATE_COUNT + 1
2136 (TEMPLATE_COUNT is the number of qualifying template classes,
2137 plus there must be another header for the member template
2140 Notice that if header_count is zero, this is not a
2141 specialization but rather a template instantiation, so there
2142 is no check we can perform here. */
2143 if (header_count
&& header_count
!= template_count
+ 1)
2146 /* Check that the number of template arguments at the
2147 innermost level for DECL is the same as for FN. */
2148 if (current_binding_level
->kind
== sk_template_parms
2149 && !current_binding_level
->explicit_spec_p
2150 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2151 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2152 (current_template_parms
))))
2155 /* DECL might be a specialization of FN. */
2156 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2157 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2159 /* For a non-static member function, we need to make sure
2160 that the const qualification is the same. Since
2161 get_bindings does not try to merge the "this" parameter,
2162 we must do the comparison explicitly. */
2163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2164 && !same_type_p (TREE_VALUE (fn_arg_types
),
2165 TREE_VALUE (decl_arg_types
)))
2168 /* Skip the "this" parameter and, for constructors of
2169 classes with virtual bases, the VTT parameter. A
2170 full specialization of a constructor will have a VTT
2171 parameter, but a template never will. */
2173 = skip_artificial_parms_for (decl
, decl_arg_types
);
2175 = skip_artificial_parms_for (fn
, fn_arg_types
);
2177 /* Function templates cannot be specializations; there are
2178 no partial specializations of functions. Therefore, if
2179 the type of DECL does not match FN, there is no
2182 Note that it should never be the case that we have both
2183 candidates added here, and for regular member functions
2185 if (tsk
== tsk_template
)
2187 if (compparms (fn_arg_types
, decl_arg_types
))
2188 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2192 /* See whether this function might be a specialization of this
2193 template. Suppress access control because we might be trying
2194 to make this specialization a friend, and we have already done
2195 access control for the declaration of the specialization. */
2196 push_deferring_access_checks (dk_no_check
);
2197 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2198 pop_deferring_access_checks ();
2201 /* We cannot deduce template arguments that when used to
2202 specialize TMPL will produce DECL. */
2205 /* Remove, from the set of candidates, all those functions
2206 whose constraints are not satisfied. */
2207 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2210 // Then, try to form the new function type.
2211 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2212 if (insttype
== error_mark_node
)
2215 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2216 if (!compparms (fn_arg_types
, decl_arg_types
))
2219 /* Save this template, and the arguments deduced. */
2220 templates
= tree_cons (targs
, fn
, templates
);
2222 else if (need_member_template
)
2223 /* FN is an ordinary member function, and we need a
2224 specialization of a member template. */
2226 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2227 /* We can get IDENTIFIER_NODEs here in certain erroneous
2230 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2231 /* This is just an ordinary non-member function. Nothing can
2232 be a specialization of that. */
2234 else if (DECL_ARTIFICIAL (fn
))
2235 /* Cannot specialize functions that are created implicitly. */
2239 tree decl_arg_types
;
2241 /* This is an ordinary member function. However, since
2242 we're here, we can assume its enclosing class is a
2243 template class. For example,
2245 template <typename T> struct S { void f(); };
2246 template <> void S<int>::f() {}
2248 Here, S<int>::f is a non-template, but S<int> is a
2249 template class. If FN has the same type as DECL, we
2250 might be in business. */
2252 if (!DECL_TEMPLATE_INFO (fn
))
2253 /* Its enclosing class is an explicit specialization
2254 of a template class. This is not a candidate. */
2257 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2258 TREE_TYPE (TREE_TYPE (fn
))))
2259 /* The return types differ. */
2262 /* Adjust the type of DECL in case FN is a static member. */
2263 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2264 if (DECL_STATIC_FUNCTION_P (fn
)
2265 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2266 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2268 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2272 // If the deduced arguments do not satisfy the constraints,
2273 // this is not a candidate.
2274 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2277 // Add the candidate.
2278 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2282 if (templates
&& TREE_CHAIN (templates
))
2288 It is possible for a specialization with a given function
2289 signature to be instantiated from more than one function
2290 template. In such cases, explicit specification of the
2291 template arguments must be used to uniquely identify the
2292 function template specialization being specialized.
2294 Note that here, there's no suggestion that we're supposed to
2295 determine which of the candidate templates is most
2296 specialized. However, we, also have:
2300 Partial ordering of overloaded function template
2301 declarations is used in the following contexts to select
2302 the function template to which a function template
2303 specialization refers:
2305 -- when an explicit specialization refers to a function
2308 So, we do use the partial ordering rules, at least for now.
2309 This extension can only serve to make invalid programs valid,
2310 so it's safe. And, there is strong anecdotal evidence that
2311 the committee intended the partial ordering rules to apply;
2312 the EDG front end has that behavior, and John Spicer claims
2313 that the committee simply forgot to delete the wording in
2314 [temp.expl.spec]. */
2315 tree tmpl
= most_specialized_instantiation (templates
);
2316 if (tmpl
!= error_mark_node
)
2319 TREE_CHAIN (templates
) = NULL_TREE
;
2323 // Concepts allows multiple declarations of member functions
2324 // with the same signature. Like above, we need to rely on
2325 // on the partial ordering of those candidates to determine which
2327 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2329 if (tree cand
= most_constrained_function (candidates
))
2332 TREE_CHAIN (cand
) = NULL_TREE
;
2336 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2338 error ("template-id %qD for %q+D does not match any template "
2339 "declaration", template_id
, decl
);
2340 if (header_count
&& header_count
!= template_count
+ 1)
2341 inform (input_location
, "saw %d %<template<>%>, need %d for "
2342 "specializing a member function template",
2343 header_count
, template_count
+ 1);
2345 print_candidates (orig_fns
);
2346 return error_mark_node
;
2348 else if ((templates
&& TREE_CHAIN (templates
))
2349 || (candidates
&& TREE_CHAIN (candidates
))
2350 || (templates
&& candidates
))
2352 error ("ambiguous template specialization %qD for %q+D",
2354 candidates
= chainon (candidates
, templates
);
2355 print_candidates (candidates
);
2356 return error_mark_node
;
2359 /* We have one, and exactly one, match. */
2362 tree fn
= TREE_VALUE (candidates
);
2363 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2365 // Propagate the candidate's constraints to the declaration.
2366 set_constraints (decl
, get_constraints (fn
));
2368 /* DECL is a re-declaration or partial instantiation of a template
2370 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2372 /* It was a specialization of an ordinary member function in a
2374 return DECL_TI_TEMPLATE (fn
);
2377 /* It was a specialization of a template. */
2378 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2379 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2381 *targs_out
= copy_node (targs
);
2382 SET_TMPL_ARGS_LEVEL (*targs_out
,
2383 TMPL_ARGS_DEPTH (*targs_out
),
2384 TREE_PURPOSE (templates
));
2387 *targs_out
= TREE_PURPOSE (templates
);
2388 return TREE_VALUE (templates
);
2391 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2392 but with the default argument values filled in from those in the
2396 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2399 tree new_spec_types
;
2404 if (spec_types
== void_list_node
)
2405 return void_list_node
;
2407 /* Substitute into the rest of the list. */
2409 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2410 TREE_CHAIN (tmpl_types
));
2412 /* Add the default argument for this parameter. */
2413 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2414 TREE_VALUE (spec_types
),
2418 /* DECL is an explicit specialization. Replicate default arguments
2419 from the template it specializes. (That way, code like:
2421 template <class T> void f(T = 3);
2422 template <> void f(double);
2425 works, as required.) An alternative approach would be to look up
2426 the correct default arguments at the call-site, but this approach
2427 is consistent with how implicit instantiations are handled. */
2430 copy_default_args_to_explicit_spec (tree decl
)
2435 tree new_spec_types
;
2439 tree object_type
= NULL_TREE
;
2440 tree in_charge
= NULL_TREE
;
2441 tree vtt
= NULL_TREE
;
2443 /* See if there's anything we need to do. */
2444 tmpl
= DECL_TI_TEMPLATE (decl
);
2445 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2446 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2447 if (TREE_PURPOSE (t
))
2452 old_type
= TREE_TYPE (decl
);
2453 spec_types
= TYPE_ARG_TYPES (old_type
);
2455 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2457 /* Remove the this pointer, but remember the object's type for
2459 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2460 spec_types
= TREE_CHAIN (spec_types
);
2461 tmpl_types
= TREE_CHAIN (tmpl_types
);
2463 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2465 /* DECL may contain more parameters than TMPL due to the extra
2466 in-charge parameter in constructors and destructors. */
2467 in_charge
= spec_types
;
2468 spec_types
= TREE_CHAIN (spec_types
);
2470 if (DECL_HAS_VTT_PARM_P (decl
))
2473 spec_types
= TREE_CHAIN (spec_types
);
2477 /* Compute the merged default arguments. */
2479 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2481 /* Compute the new FUNCTION_TYPE. */
2485 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2490 /* Put the in-charge parameter back. */
2491 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2492 TREE_VALUE (in_charge
),
2495 new_type
= build_method_type_directly (object_type
,
2496 TREE_TYPE (old_type
),
2500 new_type
= build_function_type (TREE_TYPE (old_type
),
2502 new_type
= cp_build_type_attribute_variant (new_type
,
2503 TYPE_ATTRIBUTES (old_type
));
2504 new_type
= build_exception_variant (new_type
,
2505 TYPE_RAISES_EXCEPTIONS (old_type
));
2507 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2508 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2510 TREE_TYPE (decl
) = new_type
;
2513 /* Return the number of template headers we expect to see for a definition
2514 or specialization of CTYPE or one of its non-template members. */
2517 num_template_headers_for_class (tree ctype
)
2519 int num_templates
= 0;
2521 while (ctype
&& CLASS_TYPE_P (ctype
))
2523 /* You're supposed to have one `template <...>' for every
2524 template class, but you don't need one for a full
2525 specialization. For example:
2527 template <class T> struct S{};
2528 template <> struct S<int> { void f(); };
2529 void S<int>::f () {}
2531 is correct; there shouldn't be a `template <>' for the
2532 definition of `S<int>::f'. */
2533 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2534 /* If CTYPE does not have template information of any
2535 kind, then it is not a template, nor is it nested
2536 within a template. */
2538 if (explicit_class_specialization_p (ctype
))
2540 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2543 ctype
= TYPE_CONTEXT (ctype
);
2546 return num_templates
;
2549 /* Do a simple sanity check on the template headers that precede the
2550 variable declaration DECL. */
2553 check_template_variable (tree decl
)
2555 tree ctx
= CP_DECL_CONTEXT (decl
);
2556 int wanted
= num_template_headers_for_class (ctx
);
2557 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2558 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2560 if (cxx_dialect
< cxx14
)
2561 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2562 "variable templates only available with "
2563 "-std=c++14 or -std=gnu++14");
2565 // Namespace-scope variable templates should have a template header.
2568 if (template_header_count
> wanted
)
2570 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2571 "too many template headers for %qD "
2574 if (warned
&& CLASS_TYPE_P (ctx
)
2575 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2576 inform (DECL_SOURCE_LOCATION (decl
),
2577 "members of an explicitly specialized class are defined "
2578 "without a template header");
2582 /* An explicit specialization whose declarator-id or class-head-name is not
2583 qualified shall be declared in the nearest enclosing namespace of the
2584 template, or, if the namespace is inline (7.3.1), any namespace from its
2585 enclosing namespace set.
2587 If the name declared in the explicit instantiation is an unqualified name,
2588 the explicit instantiation shall appear in the namespace where its template
2589 is declared or, if that namespace is inline (7.3.1), any namespace from its
2590 enclosing namespace set. */
2593 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2595 tree tmpl
= most_general_template (t
);
2596 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2597 && !is_nested_namespace (current_namespace
,
2598 CP_DECL_CONTEXT (tmpl
), true))
2600 if (processing_specialization
)
2601 permerror (loc
, "explicit specialization of %qD outside its "
2602 "namespace must use a nested-name-specifier", tmpl
);
2603 else if (processing_explicit_instantiation
2604 && cxx_dialect
>= cxx11
)
2605 /* This was allowed in C++98, so only pedwarn. */
2606 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2607 "outside its namespace must use a nested-name-"
2612 /* Check to see if the function just declared, as indicated in
2613 DECLARATOR, and in DECL, is a specialization of a function
2614 template. We may also discover that the declaration is an explicit
2615 instantiation at this point.
2617 Returns DECL, or an equivalent declaration that should be used
2618 instead if all goes well. Issues an error message if something is
2619 amiss. Returns error_mark_node if the error is not easily
2622 FLAGS is a bitmask consisting of the following flags:
2624 2: The function has a definition.
2625 4: The function is a friend.
2627 The TEMPLATE_COUNT is the number of references to qualifying
2628 template classes that appeared in the name of the function. For
2631 template <class T> struct S { void f(); };
2634 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2635 classes are not counted in the TEMPLATE_COUNT, so that in
2637 template <class T> struct S {};
2638 template <> struct S<int> { void f(); }
2639 template <> void S<int>::f();
2641 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2642 invalid; there should be no template <>.)
2644 If the function is a specialization, it is marked as such via
2645 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2646 is set up correctly, and it is added to the list of specializations
2647 for that template. */
2650 check_explicit_specialization (tree declarator
,
2655 int have_def
= flags
& 2;
2656 int is_friend
= flags
& 4;
2657 bool is_concept
= flags
& 8;
2658 int specialization
= 0;
2659 int explicit_instantiation
= 0;
2660 int member_specialization
= 0;
2661 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2662 tree dname
= DECL_NAME (decl
);
2667 if (!processing_specialization
)
2670 tsk
= tsk_excessive_parms
;
2673 tsk
= current_tmpl_spec_kind (template_count
);
2678 if (processing_specialization
&& !VAR_P (decl
))
2681 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2683 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2686 /* This could be something like:
2688 template <class T> void f(T);
2689 class S { friend void f<>(int); } */
2693 /* This case handles bogus declarations like template <>
2694 template <class T> void f<int>(); */
2696 error ("template-id %qD in declaration of primary template",
2703 case tsk_invalid_member_spec
:
2704 /* The error has already been reported in
2705 check_specialization_scope. */
2706 return error_mark_node
;
2708 case tsk_invalid_expl_inst
:
2709 error ("template parameter list used in explicit instantiation");
2715 error ("definition provided for explicit instantiation");
2717 explicit_instantiation
= 1;
2720 case tsk_excessive_parms
:
2721 case tsk_insufficient_parms
:
2722 if (tsk
== tsk_excessive_parms
)
2723 error ("too many template parameter lists in declaration of %qD",
2725 else if (template_header_count
)
2726 error("too few template parameter lists in declaration of %qD", decl
);
2728 error("explicit specialization of %qD must be introduced by "
2729 "%<template <>%>", decl
);
2734 error ("explicit specialization declared %<concept%>");
2736 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2737 /* In cases like template<> constexpr bool v = true;
2738 We'll give an error in check_template_variable. */
2741 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2743 member_specialization
= 1;
2749 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2751 /* This case handles bogus declarations like template <>
2752 template <class T> void f<int>(); */
2754 if (!uses_template_parms (declarator
))
2755 error ("template-id %qD in declaration of primary template",
2757 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2759 /* Partial specialization of variable template. */
2760 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2764 else if (cxx_dialect
< cxx14
)
2765 error ("non-type partial specialization %qD "
2766 "is not allowed", declarator
);
2768 error ("non-class, non-variable partial specialization %qD "
2769 "is not allowed", declarator
);
2774 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2775 /* This is a specialization of a member template, without
2776 specialization the containing class. Something like:
2778 template <class T> struct S {
2779 template <class U> void f (U);
2781 template <> template <class U> void S<int>::f(U) {}
2783 That's a specialization -- but of the entire template. */
2791 if ((specialization
|| member_specialization
)
2792 /* This doesn't apply to variable templates. */
2793 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2794 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2796 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2797 for (; t
; t
= TREE_CHAIN (t
))
2798 if (TREE_PURPOSE (t
))
2800 permerror (input_location
,
2801 "default argument specified in explicit specialization");
2806 if (specialization
|| member_specialization
|| explicit_instantiation
)
2808 tree tmpl
= NULL_TREE
;
2809 tree targs
= NULL_TREE
;
2810 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2812 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2813 if (!was_template_id
)
2817 gcc_assert (identifier_p (declarator
));
2822 /* If there is no class context, the explicit instantiation
2823 must be at namespace scope. */
2824 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2826 /* Find the namespace binding, using the declaration
2828 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2830 if (fns
== error_mark_node
)
2831 /* If lookup fails, look for a friend declaration so we can
2832 give a better diagnostic. */
2833 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2834 /*type*/false, /*complain*/true,
2837 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2839 error ("%qD is not a template function", dname
);
2840 fns
= error_mark_node
;
2844 declarator
= lookup_template_function (fns
, NULL_TREE
);
2847 if (declarator
== error_mark_node
)
2848 return error_mark_node
;
2850 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2852 if (!explicit_instantiation
)
2853 /* A specialization in class scope. This is invalid,
2854 but the error will already have been flagged by
2855 check_specialization_scope. */
2856 return error_mark_node
;
2859 /* It's not valid to write an explicit instantiation in
2862 class C { template void f(); }
2864 This case is caught by the parser. However, on
2867 template class C { void f(); };
2869 (which is invalid) we can get here. The error will be
2876 else if (ctype
!= NULL_TREE
2877 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2879 // We'll match variable templates in start_decl.
2883 /* Find the list of functions in ctype that have the same
2884 name as the declared function. */
2885 tree name
= TREE_OPERAND (declarator
, 0);
2887 if (constructor_name_p (name
, ctype
))
2889 if (DECL_CONSTRUCTOR_P (decl
)
2890 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2891 : !CLASSTYPE_DESTRUCTOR (ctype
))
2893 /* From [temp.expl.spec]:
2895 If such an explicit specialization for the member
2896 of a class template names an implicitly-declared
2897 special member function (clause _special_), the
2898 program is ill-formed.
2900 Similar language is found in [temp.explicit]. */
2901 error ("specialization of implicitly-declared special member function");
2902 return error_mark_node
;
2905 name
= DECL_NAME (decl
);
2908 /* For a type-conversion operator, We might be looking for
2909 `operator int' which will be a specialization of
2910 `operator T'. Grab all the conversion operators, and
2911 then select from them. */
2912 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2913 ? conv_op_identifier
: name
);
2915 if (fns
== NULL_TREE
)
2917 error ("no member function %qD declared in %qT", name
, ctype
);
2918 return error_mark_node
;
2921 TREE_OPERAND (declarator
, 0) = fns
;
2924 /* Figure out what exactly is being specialized at this point.
2925 Note that for an explicit instantiation, even one for a
2926 member function, we cannot tell a priori whether the
2927 instantiation is for a member template, or just a member
2928 function of a template class. Even if a member template is
2929 being instantiated, the member template arguments may be
2930 elided if they can be deduced from the rest of the
2932 tmpl
= determine_specialization (declarator
, decl
,
2934 member_specialization
,
2938 if (!tmpl
|| tmpl
== error_mark_node
)
2939 /* We couldn't figure out what this declaration was
2941 return error_mark_node
;
2944 if (TREE_CODE (decl
) == FUNCTION_DECL
2945 && DECL_HIDDEN_FRIEND_P (tmpl
))
2947 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2948 "friend declaration %qD is not visible to "
2949 "explicit specialization", tmpl
))
2950 inform (DECL_SOURCE_LOCATION (tmpl
),
2951 "friend declaration here");
2953 else if (!ctype
&& !is_friend
2954 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2955 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2957 tree gen_tmpl
= most_general_template (tmpl
);
2959 if (explicit_instantiation
)
2961 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2962 is done by do_decl_instantiation later. */
2964 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2965 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2967 if (arg_depth
> parm_depth
)
2969 /* If TMPL is not the most general template (for
2970 example, if TMPL is a friend template that is
2971 injected into namespace scope), then there will
2972 be too many levels of TARGS. Remove some of them
2977 new_targs
= make_tree_vec (parm_depth
);
2978 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2979 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2980 = TREE_VEC_ELT (targs
, i
);
2984 return instantiate_template (tmpl
, targs
, tf_error
);
2987 /* If we thought that the DECL was a member function, but it
2988 turns out to be specializing a static member function,
2989 make DECL a static member function as well. */
2990 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2991 && DECL_STATIC_FUNCTION_P (tmpl
)
2992 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2993 revert_static_member_fn (decl
);
2995 /* If this is a specialization of a member template of a
2996 template class, we want to return the TEMPLATE_DECL, not
2997 the specialization of it. */
2998 if (tsk
== tsk_template
&& !was_template_id
)
3000 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3001 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3002 DECL_INITIAL (result
) = NULL_TREE
;
3006 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3007 DECL_SOURCE_LOCATION (result
)
3008 = DECL_SOURCE_LOCATION (decl
);
3009 /* We want to use the argument list specified in the
3010 definition, not in the original declaration. */
3011 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3012 for (parm
= DECL_ARGUMENTS (result
); parm
;
3013 parm
= DECL_CHAIN (parm
))
3014 DECL_CONTEXT (parm
) = result
;
3016 return register_specialization (tmpl
, gen_tmpl
, targs
,
3020 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3021 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3023 if (was_template_id
)
3024 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3026 /* Inherit default function arguments from the template
3027 DECL is specializing. */
3028 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3029 copy_default_args_to_explicit_spec (decl
);
3031 /* This specialization has the same protection as the
3032 template it specializes. */
3033 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3034 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3036 /* 7.1.1-1 [dcl.stc]
3038 A storage-class-specifier shall not be specified in an
3039 explicit specialization...
3041 The parser rejects these, so unless action is taken here,
3042 explicit function specializations will always appear with
3045 The action recommended by the C++ CWG in response to C++
3046 defect report 605 is to make the storage class and linkage
3047 of the explicit specialization match the templated function:
3049 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3051 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3053 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3054 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3056 /* A concept cannot be specialized. */
3057 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3059 error ("explicit specialization of function concept %qD",
3061 return error_mark_node
;
3064 /* This specialization has the same linkage and visibility as
3065 the function template it specializes. */
3066 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3067 if (! TREE_PUBLIC (decl
))
3069 DECL_INTERFACE_KNOWN (decl
) = 1;
3070 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3072 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3073 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3075 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3076 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3080 /* If DECL is a friend declaration, declared using an
3081 unqualified name, the namespace associated with DECL may
3082 have been set incorrectly. For example, in:
3084 template <typename T> void f(T);
3086 struct S { friend void f<int>(int); }
3089 we will have set the DECL_CONTEXT for the friend
3090 declaration to N, rather than to the global namespace. */
3091 if (DECL_NAMESPACE_SCOPE_P (decl
))
3092 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3094 if (is_friend
&& !have_def
)
3095 /* This is not really a declaration of a specialization.
3096 It's just the name of an instantiation. But, it's not
3097 a request for an instantiation, either. */
3098 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3099 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3100 /* A specialization is not necessarily COMDAT. */
3101 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3102 && DECL_DECLARED_INLINE_P (decl
));
3103 else if (VAR_P (decl
))
3104 DECL_COMDAT (decl
) = false;
3106 /* If this is a full specialization, register it so that we can find
3107 it again. Partial specializations will be registered in
3108 process_partial_specialization. */
3109 if (!processing_template_decl
)
3110 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3113 /* A 'structor should already have clones. */
3114 gcc_assert (decl
== error_mark_node
3115 || variable_template_p (tmpl
)
3116 || !(DECL_CONSTRUCTOR_P (decl
)
3117 || DECL_DESTRUCTOR_P (decl
))
3118 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3125 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3126 parameters. These are represented in the same format used for
3127 DECL_TEMPLATE_PARMS. */
3130 comp_template_parms (const_tree parms1
, const_tree parms2
)
3135 if (parms1
== parms2
)
3138 for (p1
= parms1
, p2
= parms2
;
3139 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3140 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3142 tree t1
= TREE_VALUE (p1
);
3143 tree t2
= TREE_VALUE (p2
);
3146 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3147 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3149 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3152 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3154 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3155 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3157 /* If either of the template parameters are invalid, assume
3158 they match for the sake of error recovery. */
3159 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3162 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3165 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3166 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3167 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3169 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3174 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3175 /* One set of parameters has more parameters lists than the
3182 /* Determine whether PARM is a parameter pack. */
3185 template_parameter_pack_p (const_tree parm
)
3187 /* Determine if we have a non-type template parameter pack. */
3188 if (TREE_CODE (parm
) == PARM_DECL
)
3189 return (DECL_TEMPLATE_PARM_P (parm
)
3190 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3191 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3192 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3194 /* If this is a list of template parameters, we could get a
3195 TYPE_DECL or a TEMPLATE_DECL. */
3196 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3197 parm
= TREE_TYPE (parm
);
3199 /* Otherwise it must be a type template parameter. */
3200 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3201 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3202 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3205 /* Determine if T is a function parameter pack. */
3208 function_parameter_pack_p (const_tree t
)
3210 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3211 return DECL_PACK_P (t
);
3215 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3216 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3219 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3221 if (! primary_func_tmpl_inst
3222 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3223 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3226 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3229 /* Return true iff the function parameter PARAM_DECL was expanded
3230 from the function parameter pack PACK. */
3233 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3235 if (DECL_ARTIFICIAL (param_decl
)
3236 || !function_parameter_pack_p (pack
))
3239 /* The parameter pack and its pack arguments have the same
3241 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3244 /* Determine whether ARGS describes a variadic template args list,
3245 i.e., one that is terminated by a template argument pack. */
3248 template_args_variadic_p (tree args
)
3253 if (args
== NULL_TREE
)
3256 args
= INNERMOST_TEMPLATE_ARGS (args
);
3257 nargs
= TREE_VEC_LENGTH (args
);
3262 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3264 return ARGUMENT_PACK_P (last_parm
);
3267 /* Generate a new name for the parameter pack name NAME (an
3268 IDENTIFIER_NODE) that incorporates its */
3271 make_ith_pack_parameter_name (tree name
, int i
)
3273 /* Munge the name to include the parameter index. */
3274 #define NUMBUF_LEN 128
3275 char numbuf
[NUMBUF_LEN
];
3279 if (name
== NULL_TREE
)
3281 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3282 newname_len
= IDENTIFIER_LENGTH (name
)
3283 + strlen (numbuf
) + 2;
3284 newname
= (char*)alloca (newname_len
);
3285 snprintf (newname
, newname_len
,
3286 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3287 return get_identifier (newname
);
3290 /* Return true if T is a primary function, class or alias template
3291 specialization, not including the template pattern. */
3294 primary_template_specialization_p (const_tree t
)
3299 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3300 return (DECL_LANG_SPECIFIC (t
)
3301 && DECL_USE_TEMPLATE (t
)
3302 && DECL_TEMPLATE_INFO (t
)
3303 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3304 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3305 return (CLASSTYPE_TEMPLATE_INFO (t
)
3306 && CLASSTYPE_USE_TEMPLATE (t
)
3307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3308 else if (alias_template_specialization_p (t
))
3313 /* Return true if PARM is a template template parameter. */
3316 template_template_parameter_p (const_tree parm
)
3318 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3321 /* Return true iff PARM is a DECL representing a type template
3325 template_type_parameter_p (const_tree parm
)
3328 && (TREE_CODE (parm
) == TYPE_DECL
3329 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3330 && DECL_TEMPLATE_PARM_P (parm
));
3333 /* Return the template parameters of T if T is a
3334 primary template instantiation, NULL otherwise. */
3337 get_primary_template_innermost_parameters (const_tree t
)
3339 tree parms
= NULL
, template_info
= NULL
;
3341 if ((template_info
= get_template_info (t
))
3342 && primary_template_specialization_p (t
))
3343 parms
= INNERMOST_TEMPLATE_PARMS
3344 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3349 /* Return the template parameters of the LEVELth level from the full list
3350 of template parameters PARMS. */
3353 get_template_parms_at_level (tree parms
, int level
)
3357 || TREE_CODE (parms
) != TREE_LIST
3358 || level
> TMPL_PARMS_DEPTH (parms
))
3361 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3362 if (TMPL_PARMS_DEPTH (p
) == level
)
3368 /* Returns the template arguments of T if T is a template instantiation,
3372 get_template_innermost_arguments (const_tree t
)
3374 tree args
= NULL
, template_info
= NULL
;
3376 if ((template_info
= get_template_info (t
))
3377 && TI_ARGS (template_info
))
3378 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3383 /* Return the argument pack elements of T if T is a template argument pack,
3387 get_template_argument_pack_elems (const_tree t
)
3389 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3390 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3393 return ARGUMENT_PACK_ARGS (t
);
3396 /* True iff FN is a function representing a built-in variadic parameter
3400 builtin_pack_fn_p (tree fn
)
3403 || TREE_CODE (fn
) != FUNCTION_DECL
3404 || !DECL_IS_BUILTIN (fn
))
3407 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3413 /* True iff CALL is a call to a function representing a built-in variadic
3417 builtin_pack_call_p (tree call
)
3419 if (TREE_CODE (call
) != CALL_EXPR
)
3421 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3424 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3427 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3430 tree ohi
= CALL_EXPR_ARG (call
, 0);
3431 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3432 false/*fn*/, true/*int_cst*/);
3434 if (value_dependent_expression_p (hi
))
3438 call
= copy_node (call
);
3439 CALL_EXPR_ARG (call
, 0) = hi
;
3441 tree ex
= make_pack_expansion (call
, complain
);
3442 tree vec
= make_tree_vec (1);
3443 TREE_VEC_ELT (vec
, 0) = ex
;
3448 hi
= cxx_constant_value (hi
);
3449 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3451 /* Calculate the largest value of len that won't make the size of the vec
3452 overflow an int. The compiler will exceed resource limits long before
3453 this, but it seems a decent place to diagnose. */
3454 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3456 if (len
< 0 || len
> max
)
3458 if ((complain
& tf_error
)
3459 && hi
!= error_mark_node
)
3460 error ("argument to __integer_pack must be between 0 and %d", max
);
3461 return error_mark_node
;
3464 tree vec
= make_tree_vec (len
);
3466 for (int i
= 0; i
< len
; ++i
)
3467 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3473 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3477 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3480 if (!builtin_pack_call_p (call
))
3483 tree fn
= CALL_EXPR_FN (call
);
3485 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3486 return expand_integer_pack (call
, args
, complain
, in_decl
);
3491 /* Structure used to track the progress of find_parameter_packs_r. */
3492 struct find_parameter_pack_data
3494 /* TREE_LIST that will contain all of the parameter packs found by
3496 tree
* parameter_packs
;
3498 /* Set of AST nodes that have been visited by the traversal. */
3499 hash_set
<tree
> *visited
;
3501 /* True iff we're making a type pack expansion. */
3502 bool type_pack_expansion_p
;
3505 /* Identifies all of the argument packs that occur in a template
3506 argument and appends them to the TREE_LIST inside DATA, which is a
3507 find_parameter_pack_data structure. This is a subroutine of
3508 make_pack_expansion and uses_parameter_packs. */
3510 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3513 struct find_parameter_pack_data
* ppd
=
3514 (struct find_parameter_pack_data
*)data
;
3515 bool parameter_pack_p
= false;
3517 /* Handle type aliases/typedefs. */
3518 if (TYPE_ALIAS_P (t
))
3520 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3521 cp_walk_tree (&TI_ARGS (tinfo
),
3522 &find_parameter_packs_r
,
3528 /* Identify whether this is a parameter pack or not. */
3529 switch (TREE_CODE (t
))
3531 case TEMPLATE_PARM_INDEX
:
3532 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3533 parameter_pack_p
= true;
3536 case TEMPLATE_TYPE_PARM
:
3537 t
= TYPE_MAIN_VARIANT (t
);
3539 case TEMPLATE_TEMPLATE_PARM
:
3540 /* If the placeholder appears in the decl-specifier-seq of a function
3541 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3542 is a pack expansion, the invented template parameter is a template
3544 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3545 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3546 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3547 parameter_pack_p
= true;
3552 if (DECL_PACK_P (t
))
3554 /* We don't want to walk into the type of a PARM_DECL,
3555 because we don't want to see the type parameter pack. */
3557 parameter_pack_p
= true;
3561 /* Look through a lambda capture proxy to the field pack. */
3563 if (DECL_HAS_VALUE_EXPR_P (t
))
3565 tree v
= DECL_VALUE_EXPR (t
);
3567 &find_parameter_packs_r
,
3571 else if (variable_template_specialization_p (t
))
3573 cp_walk_tree (&DECL_TI_ARGS (t
),
3574 find_parameter_packs_r
,
3581 if (builtin_pack_call_p (t
))
3582 parameter_pack_p
= true;
3586 parameter_pack_p
= true;
3589 /* Not a parameter pack. */
3593 if (parameter_pack_p
)
3595 /* Add this parameter pack to the list. */
3596 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3600 cp_walk_tree (&TYPE_CONTEXT (t
),
3601 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3603 /* This switch statement will return immediately if we don't find a
3605 switch (TREE_CODE (t
))
3607 case TEMPLATE_PARM_INDEX
:
3610 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3611 /* Check the template itself. */
3612 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3613 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3614 /* Check the template arguments. */
3615 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3620 case TEMPLATE_TYPE_PARM
:
3621 case TEMPLATE_TEMPLATE_PARM
:
3628 /* Ignore the declaration of a capture proxy for a parameter pack. */
3629 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3634 if (TYPE_PTRMEMFUNC_P (t
))
3640 if (TYPE_TEMPLATE_INFO (t
))
3641 cp_walk_tree (&TYPE_TI_ARGS (t
),
3642 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3648 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3653 cp_walk_tree (&TREE_TYPE (t
),
3654 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3658 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3663 case TYPE_PACK_EXPANSION
:
3664 case EXPR_PACK_EXPANSION
:
3669 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3674 case IDENTIFIER_NODE
:
3675 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3682 tree fn
= lambda_function (t
);
3683 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3691 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3692 type_pack_expansion_p to false so that any placeholders
3693 within the expression don't get marked as parameter packs. */
3694 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3695 ppd
->type_pack_expansion_p
= false;
3696 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3698 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3710 /* Determines if the expression or type T uses any parameter packs. */
3712 uses_parameter_packs (tree t
)
3714 tree parameter_packs
= NULL_TREE
;
3715 struct find_parameter_pack_data ppd
;
3716 ppd
.parameter_packs
= ¶meter_packs
;
3717 ppd
.visited
= new hash_set
<tree
>;
3718 ppd
.type_pack_expansion_p
= false;
3719 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3721 return parameter_packs
!= NULL_TREE
;
3724 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3725 representation a base-class initializer into a parameter pack
3726 expansion. If all goes well, the resulting node will be an
3727 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3730 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3733 tree parameter_packs
= NULL_TREE
;
3734 bool for_types
= false;
3735 struct find_parameter_pack_data ppd
;
3737 if (!arg
|| arg
== error_mark_node
)
3740 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3742 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3743 class initializer. In this case, the TREE_PURPOSE will be a
3744 _TYPE node (representing the base class expansion we're
3745 initializing) and the TREE_VALUE will be a TREE_LIST
3746 containing the initialization arguments.
3748 The resulting expansion looks somewhat different from most
3749 expansions. Rather than returning just one _EXPANSION, we
3750 return a TREE_LIST whose TREE_PURPOSE is a
3751 TYPE_PACK_EXPANSION containing the bases that will be
3752 initialized. The TREE_VALUE will be identical to the
3753 original TREE_VALUE, which is a list of arguments that will
3754 be passed to each base. We do not introduce any new pack
3755 expansion nodes into the TREE_VALUE (although it is possible
3756 that some already exist), because the TREE_PURPOSE and
3757 TREE_VALUE all need to be expanded together with the same
3758 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3759 resulting TREE_PURPOSE will mention the parameter packs in
3760 both the bases and the arguments to the bases. */
3763 tree parameter_packs
= NULL_TREE
;
3765 /* Determine which parameter packs will be used by the base
3767 ppd
.visited
= new hash_set
<tree
>;
3768 ppd
.parameter_packs
= ¶meter_packs
;
3769 ppd
.type_pack_expansion_p
= true;
3770 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3771 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3774 if (parameter_packs
== NULL_TREE
)
3776 if (complain
& tf_error
)
3777 error ("base initializer expansion %qT contains no parameter packs",
3780 return error_mark_node
;
3783 if (TREE_VALUE (arg
) != void_type_node
)
3785 /* Collect the sets of parameter packs used in each of the
3786 initialization arguments. */
3787 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3789 /* Determine which parameter packs will be expanded in this
3791 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3798 /* Create the pack expansion type for the base type. */
3799 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3800 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3801 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3802 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3804 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3805 they will rarely be compared to anything. */
3806 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3808 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3811 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3814 /* Build the PACK_EXPANSION_* node. */
3816 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3817 : make_node (EXPR_PACK_EXPANSION
);
3818 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3819 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3821 /* Propagate type and const-expression information. */
3822 TREE_TYPE (result
) = TREE_TYPE (arg
);
3823 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3824 /* Mark this read now, since the expansion might be length 0. */
3825 mark_exp_read (arg
);
3828 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3829 they will rarely be compared to anything. */
3830 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3832 /* Determine which parameter packs will be expanded. */
3833 ppd
.parameter_packs
= ¶meter_packs
;
3834 ppd
.visited
= new hash_set
<tree
>;
3835 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3836 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3839 /* Make sure we found some parameter packs. */
3840 if (parameter_packs
== NULL_TREE
)
3842 if (complain
& tf_error
)
3845 error ("expansion pattern %qT contains no argument packs", arg
);
3847 error ("expansion pattern %qE contains no argument packs", arg
);
3849 return error_mark_node
;
3851 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3853 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3858 /* Checks T for any "bare" parameter packs, which have not yet been
3859 expanded, and issues an error if any are found. This operation can
3860 only be done on full expressions or types (e.g., an expression
3861 statement, "if" condition, etc.), because we could have expressions like:
3863 foo(f(g(h(args)))...)
3865 where "args" is a parameter pack. check_for_bare_parameter_packs
3866 should not be called for the subexpressions args, h(args),
3867 g(h(args)), or f(g(h(args))), because we would produce erroneous
3870 Returns TRUE and emits an error if there were bare parameter packs,
3871 returns FALSE otherwise. */
3873 check_for_bare_parameter_packs (tree t
)
3875 tree parameter_packs
= NULL_TREE
;
3876 struct find_parameter_pack_data ppd
;
3878 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3881 /* A lambda might use a parameter pack from the containing context. */
3882 if (current_function_decl
&& LAMBDA_FUNCTION_P (current_function_decl
))
3885 if (TREE_CODE (t
) == TYPE_DECL
)
3888 ppd
.parameter_packs
= ¶meter_packs
;
3889 ppd
.visited
= new hash_set
<tree
>;
3890 ppd
.type_pack_expansion_p
= false;
3891 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3894 if (parameter_packs
)
3896 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3897 error_at (loc
, "parameter packs not expanded with %<...%>:");
3898 while (parameter_packs
)
3900 tree pack
= TREE_VALUE (parameter_packs
);
3901 tree name
= NULL_TREE
;
3903 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3904 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3905 name
= TYPE_NAME (pack
);
3906 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3907 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3908 else if (TREE_CODE (pack
) == CALL_EXPR
)
3909 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3911 name
= DECL_NAME (pack
);
3914 inform (loc
, " %qD", name
);
3916 inform (loc
, " <anonymous>");
3918 parameter_packs
= TREE_CHAIN (parameter_packs
);
3927 /* Expand any parameter packs that occur in the template arguments in
3930 expand_template_argument_pack (tree args
)
3932 if (args
== error_mark_node
)
3933 return error_mark_node
;
3935 tree result_args
= NULL_TREE
;
3936 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3937 int num_result_args
= -1;
3938 int non_default_args_count
= -1;
3940 /* First, determine if we need to expand anything, and the number of
3941 slots we'll need. */
3942 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3944 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3945 if (arg
== NULL_TREE
)
3947 if (ARGUMENT_PACK_P (arg
))
3949 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3950 if (num_result_args
< 0)
3951 num_result_args
= in_arg
+ num_packed
;
3953 num_result_args
+= num_packed
;
3957 if (num_result_args
>= 0)
3962 /* If no expansion is necessary, we're done. */
3963 if (num_result_args
< 0)
3966 /* Expand arguments. */
3967 result_args
= make_tree_vec (num_result_args
);
3968 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3969 non_default_args_count
=
3970 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3971 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3973 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3974 if (ARGUMENT_PACK_P (arg
))
3976 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3977 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3978 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3979 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3980 if (non_default_args_count
> 0)
3981 non_default_args_count
+= num_packed
- 1;
3985 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3989 if (non_default_args_count
>= 0)
3990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3994 /* Checks if DECL shadows a template parameter.
3996 [temp.local]: A template-parameter shall not be redeclared within its
3997 scope (including nested scopes).
3999 Emits an error and returns TRUE if the DECL shadows a parameter,
4000 returns FALSE otherwise. */
4003 check_template_shadow (tree decl
)
4007 /* If we're not in a template, we can't possibly shadow a template
4009 if (!current_template_parms
)
4012 /* Figure out what we're shadowing. */
4013 decl
= OVL_FIRST (decl
);
4014 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4016 /* If there's no previous binding for this name, we're not shadowing
4017 anything, let alone a template parameter. */
4021 /* If we're not shadowing a template parameter, we're done. Note
4022 that OLDDECL might be an OVERLOAD (or perhaps even an
4023 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4025 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4028 /* We check for decl != olddecl to avoid bogus errors for using a
4029 name inside a class. We check TPFI to avoid duplicate errors for
4030 inline member templates. */
4032 || (DECL_TEMPLATE_PARM_P (decl
)
4033 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4036 /* Don't complain about the injected class name, as we've already
4037 complained about the class itself. */
4038 if (DECL_SELF_REFERENCE_P (decl
))
4041 if (DECL_TEMPLATE_PARM_P (decl
))
4042 error ("declaration of template parameter %q+D shadows "
4043 "template parameter", decl
);
4045 error ("declaration of %q+#D shadows template parameter", decl
);
4046 inform (DECL_SOURCE_LOCATION (olddecl
),
4047 "template parameter %qD declared here", olddecl
);
4051 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4052 ORIG_LEVEL, DECL, and TYPE. */
4055 build_template_parm_index (int index
,
4061 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4062 TEMPLATE_PARM_IDX (t
) = index
;
4063 TEMPLATE_PARM_LEVEL (t
) = level
;
4064 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4065 TEMPLATE_PARM_DECL (t
) = decl
;
4066 TREE_TYPE (t
) = type
;
4067 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4068 TREE_READONLY (t
) = TREE_READONLY (decl
);
4073 /* Find the canonical type parameter for the given template type
4074 parameter. Returns the canonical type parameter, which may be TYPE
4075 if no such parameter existed. */
4078 canonical_type_parameter (tree type
)
4081 int idx
= TEMPLATE_TYPE_IDX (type
);
4082 if (!canonical_template_parms
)
4083 vec_alloc (canonical_template_parms
, idx
+ 1);
4085 if (canonical_template_parms
->length () <= (unsigned) idx
)
4086 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4088 list
= (*canonical_template_parms
)[idx
];
4089 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4090 list
= TREE_CHAIN (list
);
4093 return TREE_VALUE (list
);
4096 (*canonical_template_parms
)[idx
]
4097 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4102 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4103 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4104 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4105 new one is created. */
4108 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4109 tsubst_flags_t complain
)
4111 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4112 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4113 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4114 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4116 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4119 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4120 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4121 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4122 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4123 DECL_ARTIFICIAL (decl
) = 1;
4124 SET_DECL_TEMPLATE_PARM_P (decl
);
4126 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4127 TEMPLATE_PARM_LEVEL (index
) - levels
,
4128 TEMPLATE_PARM_ORIG_LEVEL (index
),
4130 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4131 TEMPLATE_PARM_PARAMETER_PACK (t
)
4132 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4134 /* Template template parameters need this. */
4135 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4137 DECL_TEMPLATE_RESULT (decl
)
4138 = build_decl (DECL_SOURCE_LOCATION (decl
),
4139 TYPE_DECL
, DECL_NAME (decl
), type
);
4140 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4141 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4142 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4146 return TEMPLATE_PARM_DESCENDANTS (index
);
4149 /* Process information from new template parameter PARM and append it
4150 to the LIST being built. This new parameter is a non-type
4151 parameter iff IS_NON_TYPE is true. This new parameter is a
4152 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4156 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4157 bool is_non_type
, bool is_parameter_pack
)
4162 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4163 tree defval
= TREE_PURPOSE (parm
);
4164 tree constr
= TREE_TYPE (parm
);
4168 tree p
= tree_last (list
);
4170 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4173 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4174 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4176 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4184 parm
= TREE_VALUE (parm
);
4186 SET_DECL_TEMPLATE_PARM_P (parm
);
4188 if (TREE_TYPE (parm
) != error_mark_node
)
4192 The top-level cv-qualifiers on the template-parameter are
4193 ignored when determining its type. */
4194 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4195 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4196 TREE_TYPE (parm
) = error_mark_node
;
4197 else if (uses_parameter_packs (TREE_TYPE (parm
))
4198 && !is_parameter_pack
4199 /* If we're in a nested template parameter list, the template
4200 template parameter could be a parameter pack. */
4201 && processing_template_parmlist
== 1)
4203 /* This template parameter is not a parameter pack, but it
4204 should be. Complain about "bare" parameter packs. */
4205 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4207 /* Recover by calling this a parameter pack. */
4208 is_parameter_pack
= true;
4212 /* A template parameter is not modifiable. */
4213 TREE_CONSTANT (parm
) = 1;
4214 TREE_READONLY (parm
) = 1;
4215 decl
= build_decl (parm_loc
,
4216 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4217 TREE_CONSTANT (decl
) = 1;
4218 TREE_READONLY (decl
) = 1;
4219 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4220 = build_template_parm_index (idx
, processing_template_decl
,
4221 processing_template_decl
,
4222 decl
, TREE_TYPE (parm
));
4224 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4225 = is_parameter_pack
;
4230 parm
= TREE_VALUE (TREE_VALUE (parm
));
4232 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4234 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4235 /* This is for distinguishing between real templates and template
4236 template parameters */
4237 TREE_TYPE (parm
) = t
;
4238 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4243 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4244 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4245 decl
= build_decl (parm_loc
,
4246 TYPE_DECL
, parm
, t
);
4249 TYPE_NAME (t
) = decl
;
4250 TYPE_STUB_DECL (t
) = decl
;
4252 TEMPLATE_TYPE_PARM_INDEX (t
)
4253 = build_template_parm_index (idx
, processing_template_decl
,
4254 processing_template_decl
,
4255 decl
, TREE_TYPE (parm
));
4256 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4257 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4259 DECL_ARTIFICIAL (decl
) = 1;
4260 SET_DECL_TEMPLATE_PARM_P (decl
);
4262 /* Build requirements for the type/template parameter.
4263 This must be done after SET_DECL_TEMPLATE_PARM_P or
4264 process_template_parm could fail. */
4265 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4269 /* Build the parameter node linking the parameter declaration,
4270 its default argument (if any), and its constraints (if any). */
4271 parm
= build_tree_list (defval
, parm
);
4272 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4274 return chainon (list
, parm
);
4277 /* The end of a template parameter list has been reached. Process the
4278 tree list into a parameter vector, converting each parameter into a more
4279 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4283 end_template_parm_list (tree parms
)
4287 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4289 /* Pop the dummy parameter level and add the real one. */
4290 current_template_parms
= TREE_CHAIN (current_template_parms
);
4292 current_template_parms
4293 = tree_cons (size_int (processing_template_decl
),
4294 saved_parmlist
, current_template_parms
);
4296 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4298 next
= TREE_CHAIN (parm
);
4299 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4300 TREE_CHAIN (parm
) = NULL_TREE
;
4303 --processing_template_parmlist
;
4305 return saved_parmlist
;
4308 // Explicitly indicate the end of the template parameter list. We assume
4309 // that the current template parameters have been constructed and/or
4310 // managed explicitly, as when creating new template template parameters
4311 // from a shorthand constraint.
4313 end_template_parm_list ()
4315 --processing_template_parmlist
;
4318 /* end_template_decl is called after a template declaration is seen. */
4321 end_template_decl (void)
4323 reset_specialization ();
4325 if (! processing_template_decl
)
4328 /* This matches the pushlevel in begin_template_parm_list. */
4331 --processing_template_decl
;
4332 current_template_parms
= TREE_CHAIN (current_template_parms
);
4335 /* Takes a TREE_LIST representing a template parameter and convert it
4336 into an argument suitable to be passed to the type substitution
4337 functions. Note that If the TREE_LIST contains an error_mark
4338 node, the returned argument is error_mark_node. */
4341 template_parm_to_arg (tree t
)
4345 || TREE_CODE (t
) != TREE_LIST
)
4348 if (error_operand_p (TREE_VALUE (t
)))
4349 return error_mark_node
;
4353 if (TREE_CODE (t
) == TYPE_DECL
4354 || TREE_CODE (t
) == TEMPLATE_DECL
)
4358 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4360 /* Turn this argument into a TYPE_ARGUMENT_PACK
4361 with a single element, which expands T. */
4362 tree vec
= make_tree_vec (1);
4364 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4366 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4368 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4369 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4374 t
= DECL_INITIAL (t
);
4376 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4378 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4379 with a single element, which expands T. */
4380 tree vec
= make_tree_vec (1);
4382 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4384 t
= convert_from_reference (t
);
4385 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4387 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4388 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4391 t
= convert_from_reference (t
);
4396 /* Given a single level of template parameters (a TREE_VEC), return it
4397 as a set of template arguments. */
4400 template_parms_level_to_args (tree parms
)
4402 tree a
= copy_node (parms
);
4403 TREE_TYPE (a
) = NULL_TREE
;
4404 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4405 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4408 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4413 /* Given a set of template parameters, return them as a set of template
4414 arguments. The template parameters are represented as a TREE_VEC, in
4415 the form documented in cp-tree.h for template arguments. */
4418 template_parms_to_args (tree parms
)
4421 tree args
= NULL_TREE
;
4422 int length
= TMPL_PARMS_DEPTH (parms
);
4425 /* If there is only one level of template parameters, we do not
4426 create a TREE_VEC of TREE_VECs. Instead, we return a single
4427 TREE_VEC containing the arguments. */
4429 args
= make_tree_vec (length
);
4431 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4433 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4436 TREE_VEC_ELT (args
, --l
) = a
;
4444 /* Within the declaration of a template, return the currently active
4445 template parameters as an argument TREE_VEC. */
4448 current_template_args (void)
4450 return template_parms_to_args (current_template_parms
);
4453 /* Update the declared TYPE by doing any lookups which were thought to be
4454 dependent, but are not now that we know the SCOPE of the declarator. */
4457 maybe_update_decl_type (tree orig_type
, tree scope
)
4459 tree type
= orig_type
;
4461 if (type
== NULL_TREE
)
4464 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4465 type
= TREE_TYPE (type
);
4467 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4468 && dependent_type_p (type
)
4469 /* Don't bother building up the args in this case. */
4470 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4472 /* tsubst in the args corresponding to the template parameters,
4473 including auto if present. Most things will be unchanged, but
4474 make_typename_type and tsubst_qualified_id will resolve
4475 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4476 tree args
= current_template_args ();
4477 tree auto_node
= type_uses_auto (type
);
4481 tree auto_vec
= make_tree_vec (1);
4482 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4483 args
= add_to_template_args (args
, auto_vec
);
4485 pushed
= push_scope (scope
);
4486 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4491 if (type
== error_mark_node
)
4494 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4496 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4499 type
= TYPE_NAME (type
);
4504 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4505 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4506 the new template is a member template. */
4509 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4511 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4512 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4513 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4514 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4515 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4520 struct template_parm_data
4522 /* The level of the template parameters we are currently
4526 /* The index of the specialization argument we are currently
4530 /* An array whose size is the number of template parameters. The
4531 elements are nonzero if the parameter has been used in any one
4532 of the arguments processed so far. */
4535 /* An array whose size is the number of template arguments. The
4536 elements are nonzero if the argument makes use of template
4537 parameters of this level. */
4538 int* arg_uses_template_parms
;
4541 /* Subroutine of push_template_decl used to see if each template
4542 parameter in a partial specialization is used in the explicit
4543 argument list. If T is of the LEVEL given in DATA (which is
4544 treated as a template_parm_data*), then DATA->PARMS is marked
4548 mark_template_parm (tree t
, void* data
)
4552 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4554 template_parm_level_and_index (t
, &level
, &idx
);
4556 if (level
== tpd
->level
)
4558 tpd
->parms
[idx
] = 1;
4559 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4562 /* In C++17 the type of a non-type argument is a deduced context. */
4563 if (cxx_dialect
>= cxx17
4564 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4565 for_each_template_parm (TREE_TYPE (t
),
4566 &mark_template_parm
,
4569 /*include_nondeduced_p=*/false);
4571 /* Return zero so that for_each_template_parm will continue the
4572 traversal of the tree; we want to mark *every* template parm. */
4576 /* Process the partial specialization DECL. */
4579 process_partial_specialization (tree decl
)
4581 tree type
= TREE_TYPE (decl
);
4582 tree tinfo
= get_template_info (decl
);
4583 tree maintmpl
= TI_TEMPLATE (tinfo
);
4584 tree specargs
= TI_ARGS (tinfo
);
4585 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4586 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4589 int nargs
= TREE_VEC_LENGTH (inner_args
);
4592 bool did_error_intro
= false;
4593 struct template_parm_data tpd
;
4594 struct template_parm_data tpd2
;
4596 gcc_assert (current_template_parms
);
4598 /* A concept cannot be specialized. */
4599 if (flag_concepts
&& variable_concept_p (maintmpl
))
4601 error ("specialization of variable concept %q#D", maintmpl
);
4602 return error_mark_node
;
4605 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4606 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4608 /* We check that each of the template parameters given in the
4609 partial specialization is used in the argument list to the
4610 specialization. For example:
4612 template <class T> struct S;
4613 template <class T> struct S<T*>;
4615 The second declaration is OK because `T*' uses the template
4616 parameter T, whereas
4618 template <class T> struct S<int>;
4620 is no good. Even trickier is:
4631 The S2<T> declaration is actually invalid; it is a
4632 full-specialization. Of course,
4635 struct S2<T (*)(U)>;
4637 or some such would have been OK. */
4638 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4639 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4640 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4642 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4643 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4644 for (i
= 0; i
< nargs
; ++i
)
4646 tpd
.current_arg
= i
;
4647 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4648 &mark_template_parm
,
4651 /*include_nondeduced_p=*/false);
4653 for (i
= 0; i
< ntparms
; ++i
)
4654 if (tpd
.parms
[i
] == 0)
4656 /* One of the template parms was not used in a deduced context in the
4658 if (!did_error_intro
)
4660 error ("template parameters not deducible in "
4661 "partial specialization:");
4662 did_error_intro
= true;
4665 inform (input_location
, " %qD",
4666 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4669 if (did_error_intro
)
4670 return error_mark_node
;
4672 /* [temp.class.spec]
4674 The argument list of the specialization shall not be identical to
4675 the implicit argument list of the primary template. */
4677 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4678 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4680 || !strictly_subsumes (current_template_constraints (),
4681 get_constraints (maintmpl
))))
4684 error ("partial specialization %q+D does not specialize "
4685 "any template arguments", decl
);
4687 error ("partial specialization %q+D does not specialize any "
4688 "template arguments and is not more constrained than", decl
);
4689 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4692 /* A partial specialization that replaces multiple parameters of the
4693 primary template with a pack expansion is less specialized for those
4695 if (nargs
< DECL_NTPARMS (maintmpl
))
4697 error ("partial specialization is not more specialized than the "
4698 "primary template because it replaces multiple parameters "
4699 "with a pack expansion");
4700 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4701 /* Avoid crash in process_partial_specialization. */
4705 /* If we aren't in a dependent class, we can actually try deduction. */
4706 else if (tpd
.level
== 1
4707 /* FIXME we should be able to handle a partial specialization of a
4708 partial instantiation, but currently we can't (c++/41727). */
4709 && TMPL_ARGS_DEPTH (specargs
) == 1
4710 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4712 if (permerror (input_location
, "partial specialization %qD is not "
4713 "more specialized than", decl
))
4714 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4718 /* [temp.class.spec]
4720 A partially specialized non-type argument expression shall not
4721 involve template parameters of the partial specialization except
4722 when the argument expression is a simple identifier.
4724 The type of a template parameter corresponding to a specialized
4725 non-type argument shall not be dependent on a parameter of the
4728 Also, we verify that pack expansions only occur at the
4729 end of the argument list. */
4730 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4732 for (i
= 0; i
< nargs
; ++i
)
4734 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4735 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4736 tree packed_args
= NULL_TREE
;
4739 if (ARGUMENT_PACK_P (arg
))
4741 /* Extract the arguments from the argument pack. We'll be
4742 iterating over these in the following loop. */
4743 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4744 len
= TREE_VEC_LENGTH (packed_args
);
4747 for (j
= 0; j
< len
; j
++)
4750 /* Get the Jth argument in the parameter pack. */
4751 arg
= TREE_VEC_ELT (packed_args
, j
);
4753 if (PACK_EXPANSION_P (arg
))
4755 /* Pack expansions must come at the end of the
4757 if ((packed_args
&& j
< len
- 1)
4758 || (!packed_args
&& i
< nargs
- 1))
4760 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4761 error ("parameter pack argument %qE must be at the "
4762 "end of the template argument list", arg
);
4764 error ("parameter pack argument %qT must be at the "
4765 "end of the template argument list", arg
);
4769 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4770 /* We only care about the pattern. */
4771 arg
= PACK_EXPANSION_PATTERN (arg
);
4773 if (/* These first two lines are the `non-type' bit. */
4775 && TREE_CODE (arg
) != TEMPLATE_DECL
4776 /* This next two lines are the `argument expression is not just a
4777 simple identifier' condition and also the `specialized
4778 non-type argument' bit. */
4779 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4780 && !(REFERENCE_REF_P (arg
)
4781 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4783 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4784 || (packed_args
&& uses_template_parms (arg
)))
4785 error ("template argument %qE involves template parameter(s)",
4789 /* Look at the corresponding template parameter,
4790 marking which template parameters its type depends
4792 tree type
= TREE_TYPE (parm
);
4796 /* We haven't yet initialized TPD2. Do so now. */
4797 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4798 /* The number of parameters here is the number in the
4799 main template, which, as checked in the assertion
4801 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4803 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4806 /* Mark the template parameters. But this time, we're
4807 looking for the template parameters of the main
4808 template, not in the specialization. */
4809 tpd2
.current_arg
= i
;
4810 tpd2
.arg_uses_template_parms
[i
] = 0;
4811 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4812 for_each_template_parm (type
,
4813 &mark_template_parm
,
4816 /*include_nondeduced_p=*/false);
4818 if (tpd2
.arg_uses_template_parms
[i
])
4820 /* The type depended on some template parameters.
4821 If they are fully specialized in the
4822 specialization, that's OK. */
4825 for (j
= 0; j
< nargs
; ++j
)
4826 if (tpd2
.parms
[j
] != 0
4827 && tpd
.arg_uses_template_parms
[j
])
4830 error_n (input_location
, count
,
4831 "type %qT of template argument %qE depends "
4832 "on a template parameter",
4833 "type %qT of template argument %qE depends "
4834 "on template parameters",
4843 /* We should only get here once. */
4844 if (TREE_CODE (decl
) == TYPE_DECL
)
4845 gcc_assert (!COMPLETE_TYPE_P (type
));
4847 // Build the template decl.
4848 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4849 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4850 TREE_TYPE (tmpl
) = type
;
4851 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4853 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4854 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4856 /* Give template template parms a DECL_CONTEXT of the template
4857 for which they are a parameter. */
4858 for (i
= 0; i
< ntparms
; ++i
)
4860 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4861 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4862 DECL_CONTEXT (parm
) = tmpl
;
4866 /* We didn't register this in check_explicit_specialization so we could
4867 wait until the constraints were set. */
4868 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4870 associate_classtype_constraints (type
);
4872 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4873 = tree_cons (specargs
, tmpl
,
4874 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4875 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4877 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4878 inst
= TREE_CHAIN (inst
))
4880 tree instance
= TREE_VALUE (inst
);
4881 if (TYPE_P (instance
)
4882 ? (COMPLETE_TYPE_P (instance
)
4883 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4884 : DECL_TEMPLATE_INSTANTIATION (instance
))
4886 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4887 tree inst_decl
= (DECL_P (instance
)
4888 ? instance
: TYPE_NAME (instance
));
4891 else if (spec
== error_mark_node
)
4892 permerror (input_location
,
4893 "declaration of %qD ambiguates earlier template "
4894 "instantiation for %qD", decl
, inst_decl
);
4895 else if (TREE_VALUE (spec
) == tmpl
)
4896 permerror (input_location
,
4897 "partial specialization of %qD after instantiation "
4898 "of %qD", decl
, inst_decl
);
4905 /* PARM is a template parameter of some form; return the corresponding
4906 TEMPLATE_PARM_INDEX. */
4909 get_template_parm_index (tree parm
)
4911 if (TREE_CODE (parm
) == PARM_DECL
4912 || TREE_CODE (parm
) == CONST_DECL
)
4913 parm
= DECL_INITIAL (parm
);
4914 else if (TREE_CODE (parm
) == TYPE_DECL
4915 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4916 parm
= TREE_TYPE (parm
);
4917 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4918 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4919 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4920 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4921 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4925 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4926 parameter packs used by the template parameter PARM. */
4929 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4931 /* A type parm can't refer to another parm. */
4932 if (TREE_CODE (parm
) == TYPE_DECL
)
4934 else if (TREE_CODE (parm
) == PARM_DECL
)
4936 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4941 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4943 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4944 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4945 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4948 /* PARM is a template parameter pack. Return any parameter packs used in
4949 its type or the type of any of its template parameters. If there are
4950 any such packs, it will be instantiated into a fixed template parameter
4951 list by partial instantiation rather than be fully deduced. */
4954 fixed_parameter_pack_p (tree parm
)
4956 /* This can only be true in a member template. */
4957 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4959 /* This can only be true for a parameter pack. */
4960 if (!template_parameter_pack_p (parm
))
4962 /* A type parm can't refer to another parm. */
4963 if (TREE_CODE (parm
) == TYPE_DECL
)
4966 tree parameter_packs
= NULL_TREE
;
4967 struct find_parameter_pack_data ppd
;
4968 ppd
.parameter_packs
= ¶meter_packs
;
4969 ppd
.visited
= new hash_set
<tree
>;
4970 ppd
.type_pack_expansion_p
= false;
4972 fixed_parameter_pack_p_1 (parm
, &ppd
);
4975 return parameter_packs
;
4978 /* Check that a template declaration's use of default arguments and
4979 parameter packs is not invalid. Here, PARMS are the template
4980 parameters. IS_PRIMARY is true if DECL is the thing declared by
4981 a primary template. IS_PARTIAL is true if DECL is a partial
4984 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
4985 function template declaration or a friend class template
4986 declaration. In the function case, 1 indicates a declaration, 2
4987 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4988 emitted for extraneous default arguments.
4990 Returns TRUE if there were no errors found, FALSE otherwise. */
4993 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4994 bool is_partial
, int is_friend_decl
)
4997 int last_level_to_check
;
4999 bool no_errors
= true;
5003 A default template-argument shall not be specified in a
5004 function template declaration or a function template definition, nor
5005 in the template-parameter-list of the definition of a member of a
5008 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5009 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5010 /* You can't have a function template declaration in a local
5011 scope, nor you can you define a member of a class template in a
5015 if ((TREE_CODE (decl
) == TYPE_DECL
5017 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5018 || (TREE_CODE (decl
) == FUNCTION_DECL
5019 && LAMBDA_FUNCTION_P (decl
)))
5020 /* A lambda doesn't have an explicit declaration; don't complain
5021 about the parms of the enclosing class. */
5024 if (current_class_type
5025 && !TYPE_BEING_DEFINED (current_class_type
)
5026 && DECL_LANG_SPECIFIC (decl
)
5027 && DECL_DECLARES_FUNCTION_P (decl
)
5028 /* If this is either a friend defined in the scope of the class
5029 or a member function. */
5030 && (DECL_FUNCTION_MEMBER_P (decl
)
5031 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5032 : DECL_FRIEND_CONTEXT (decl
)
5033 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5035 /* And, if it was a member function, it really was defined in
5036 the scope of the class. */
5037 && (!DECL_FUNCTION_MEMBER_P (decl
)
5038 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5039 /* We already checked these parameters when the template was
5040 declared, so there's no need to do it again now. This function
5041 was defined in class scope, but we're processing its body now
5042 that the class is complete. */
5045 /* Core issue 226 (C++0x only): the following only applies to class
5048 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5052 If a template-parameter has a default template-argument, all
5053 subsequent template-parameters shall have a default
5054 template-argument supplied. */
5055 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5057 tree inner_parms
= TREE_VALUE (parm_level
);
5058 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5059 int seen_def_arg_p
= 0;
5062 for (i
= 0; i
< ntparms
; ++i
)
5064 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5066 if (parm
== error_mark_node
)
5069 if (TREE_PURPOSE (parm
))
5071 else if (seen_def_arg_p
5072 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5074 error ("no default argument for %qD", TREE_VALUE (parm
));
5075 /* For better subsequent error-recovery, we indicate that
5076 there should have been a default argument. */
5077 TREE_PURPOSE (parm
) = error_mark_node
;
5080 else if (!is_partial
5082 /* Don't complain about an enclosing partial
5084 && parm_level
== parms
5085 && TREE_CODE (decl
) == TYPE_DECL
5087 && template_parameter_pack_p (TREE_VALUE (parm
))
5088 /* A fixed parameter pack will be partially
5089 instantiated into a fixed length list. */
5090 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5092 /* A primary class template can only have one
5093 parameter pack, at the end of the template
5096 error ("parameter pack %q+D must be at the end of the"
5097 " template parameter list", TREE_VALUE (parm
));
5099 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5107 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5111 /* For an ordinary class template, default template arguments are
5112 allowed at the innermost level, e.g.:
5113 template <class T = int>
5115 but, in a partial specialization, they're not allowed even
5116 there, as we have in [temp.class.spec]:
5118 The template parameter list of a specialization shall not
5119 contain default template argument values.
5121 So, for a partial specialization, or for a function template
5122 (in C++98/C++03), we look at all of them. */
5125 /* But, for a primary class template that is not a partial
5126 specialization we look at all template parameters except the
5128 parms
= TREE_CHAIN (parms
);
5130 /* Figure out what error message to issue. */
5131 if (is_friend_decl
== 2)
5132 msg
= G_("default template arguments may not be used in function template "
5133 "friend re-declaration");
5134 else if (is_friend_decl
)
5135 msg
= G_("default template arguments may not be used in template "
5136 "friend declarations");
5137 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5138 msg
= G_("default template arguments may not be used in function templates "
5139 "without -std=c++11 or -std=gnu++11");
5140 else if (is_partial
)
5141 msg
= G_("default template arguments may not be used in "
5142 "partial specializations");
5143 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5144 msg
= G_("default argument for template parameter for class enclosing %qD");
5146 /* Per [temp.param]/9, "A default template-argument shall not be
5147 specified in the template-parameter-lists of the definition of
5148 a member of a class template that appears outside of the member's
5149 class.", thus if we aren't handling a member of a class template
5150 there is no need to examine the parameters. */
5153 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5154 /* If we're inside a class definition, there's no need to
5155 examine the parameters to the class itself. On the one
5156 hand, they will be checked when the class is defined, and,
5157 on the other, default arguments are valid in things like:
5158 template <class T = double>
5159 struct S { template <class U> void f(U); };
5160 Here the default argument for `S' has no bearing on the
5161 declaration of `f'. */
5162 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5164 /* Check everything. */
5165 last_level_to_check
= 0;
5167 for (parm_level
= parms
;
5168 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5169 parm_level
= TREE_CHAIN (parm_level
))
5171 tree inner_parms
= TREE_VALUE (parm_level
);
5175 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5176 for (i
= 0; i
< ntparms
; ++i
)
5178 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5181 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5186 if (is_friend_decl
== 2)
5193 /* Clear out the default argument so that we are not
5195 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5199 /* At this point, if we're still interested in issuing messages,
5200 they must apply to classes surrounding the object declared. */
5202 msg
= G_("default argument for template parameter for class "
5209 /* Worker for push_template_decl_real, called via
5210 for_each_template_parm. DATA is really an int, indicating the
5211 level of the parameters we are interested in. If T is a template
5212 parameter of that level, return nonzero. */
5215 template_parm_this_level_p (tree t
, void* data
)
5217 int this_level
= *(int *)data
;
5220 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5221 level
= TEMPLATE_PARM_LEVEL (t
);
5223 level
= TEMPLATE_TYPE_LEVEL (t
);
5224 return level
== this_level
;
5227 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5228 DATA is really an int, indicating the innermost outer level of parameters.
5229 If T is a template parameter of that level or further out, return
5233 template_parm_outer_level (tree t
, void *data
)
5235 int this_level
= *(int *)data
;
5238 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5239 level
= TEMPLATE_PARM_LEVEL (t
);
5241 level
= TEMPLATE_TYPE_LEVEL (t
);
5242 return level
<= this_level
;
5245 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5246 parameters given by current_template_args, or reuses a
5247 previously existing one, if appropriate. Returns the DECL, or an
5248 equivalent one, if it is replaced via a call to duplicate_decls.
5250 If IS_FRIEND is true, DECL is a friend declaration. */
5253 push_template_decl_real (tree decl
, bool is_friend
)
5261 int new_template_p
= 0;
5262 /* True if the template is a member template, in the sense of
5264 bool member_template_p
= false;
5266 if (decl
== error_mark_node
|| !current_template_parms
)
5267 return error_mark_node
;
5269 /* See if this is a partial specialization. */
5270 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5271 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5272 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5274 && DECL_LANG_SPECIFIC (decl
)
5275 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5276 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5278 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5282 /* For a friend, we want the context of the friend, not
5283 the type of which it is a friend. */
5284 ctx
= CP_DECL_CONTEXT (decl
);
5285 else if (CP_DECL_CONTEXT (decl
)
5286 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5287 /* In the case of a virtual function, we want the class in which
5289 ctx
= CP_DECL_CONTEXT (decl
);
5291 /* Otherwise, if we're currently defining some class, the DECL
5292 is assumed to be a member of the class. */
5293 ctx
= current_scope ();
5295 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5298 if (!DECL_CONTEXT (decl
))
5299 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5301 /* See if this is a primary template. */
5302 if (is_friend
&& ctx
5303 && uses_template_parms_level (ctx
, processing_template_decl
))
5304 /* A friend template that specifies a class context, i.e.
5305 template <typename T> friend void A<T>::f();
5308 else if (TREE_CODE (decl
) == TYPE_DECL
5309 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5312 is_primary
= template_parm_scope_p ();
5316 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5318 if (DECL_CLASS_SCOPE_P (decl
))
5319 member_template_p
= true;
5320 if (TREE_CODE (decl
) == TYPE_DECL
5321 && anon_aggrname_p (DECL_NAME (decl
)))
5323 error ("template class without a name");
5324 return error_mark_node
;
5326 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5328 if (member_template_p
)
5330 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5331 error ("member template %qD may not have virt-specifiers", decl
);
5333 if (DECL_DESTRUCTOR_P (decl
))
5337 A destructor shall not be a member template. */
5338 error ("destructor %qD declared as member template", decl
);
5339 return error_mark_node
;
5341 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5342 && (!prototype_p (TREE_TYPE (decl
))
5343 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5344 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5345 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5346 == void_list_node
)))
5348 /* [basic.stc.dynamic.allocation]
5350 An allocation function can be a function
5351 template. ... Template allocation functions shall
5352 have two or more parameters. */
5353 error ("invalid template declaration of %qD", decl
);
5354 return error_mark_node
;
5357 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5358 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5360 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5361 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5362 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5364 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5365 if (TREE_CODE (t
) == TYPE_DECL
)
5367 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5368 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5371 else if (TREE_CODE (decl
) == TYPE_DECL
5372 && TYPE_DECL_ALIAS_P (decl
))
5373 /* alias-declaration */
5374 gcc_assert (!DECL_ARTIFICIAL (decl
));
5375 else if (VAR_P (decl
))
5376 /* C++14 variable template. */;
5379 error ("template declaration of %q#D", decl
);
5380 return error_mark_node
;
5384 /* Check to see that the rules regarding the use of default
5385 arguments are not being violated. We check args for a friend
5386 functions when we know whether it's a definition, introducing
5387 declaration or re-declaration. */
5388 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5389 check_default_tmpl_args (decl
, current_template_parms
,
5390 is_primary
, is_partial
, is_friend
);
5392 /* Ensure that there are no parameter packs in the type of this
5393 declaration that have not been expanded. */
5394 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5396 /* Check each of the arguments individually to see if there are
5397 any bare parameter packs. */
5398 tree type
= TREE_TYPE (decl
);
5399 tree arg
= DECL_ARGUMENTS (decl
);
5400 tree argtype
= TYPE_ARG_TYPES (type
);
5402 while (arg
&& argtype
)
5404 if (!DECL_PACK_P (arg
)
5405 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5407 /* This is a PARM_DECL that contains unexpanded parameter
5408 packs. We have already complained about this in the
5409 check_for_bare_parameter_packs call, so just replace
5410 these types with ERROR_MARK_NODE. */
5411 TREE_TYPE (arg
) = error_mark_node
;
5412 TREE_VALUE (argtype
) = error_mark_node
;
5415 arg
= DECL_CHAIN (arg
);
5416 argtype
= TREE_CHAIN (argtype
);
5419 /* Check for bare parameter packs in the return type and the
5420 exception specifiers. */
5421 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5422 /* Errors were already issued, set return type to int
5423 as the frontend doesn't expect error_mark_node as
5425 TREE_TYPE (type
) = integer_type_node
;
5426 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5427 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5429 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5430 && TYPE_DECL_ALIAS_P (decl
))
5431 ? DECL_ORIGINAL_TYPE (decl
)
5432 : TREE_TYPE (decl
)))
5434 TREE_TYPE (decl
) = error_mark_node
;
5435 return error_mark_node
;
5439 return process_partial_specialization (decl
);
5441 args
= current_template_args ();
5444 || TREE_CODE (ctx
) == FUNCTION_DECL
5445 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5446 || (TREE_CODE (decl
) == TYPE_DECL
5447 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5448 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5450 if (DECL_LANG_SPECIFIC (decl
)
5451 && DECL_TEMPLATE_INFO (decl
)
5452 && DECL_TI_TEMPLATE (decl
))
5453 tmpl
= DECL_TI_TEMPLATE (decl
);
5454 /* If DECL is a TYPE_DECL for a class-template, then there won't
5455 be DECL_LANG_SPECIFIC. The information equivalent to
5456 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5457 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5458 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5459 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5461 /* Since a template declaration already existed for this
5462 class-type, we must be redeclaring it here. Make sure
5463 that the redeclaration is valid. */
5464 redeclare_class_template (TREE_TYPE (decl
),
5465 current_template_parms
,
5466 current_template_constraints ());
5467 /* We don't need to create a new TEMPLATE_DECL; just use the
5468 one we already had. */
5469 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5473 tmpl
= build_template_decl (decl
, current_template_parms
,
5477 if (DECL_LANG_SPECIFIC (decl
)
5478 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5480 /* A specialization of a member template of a template
5482 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5483 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5484 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5490 tree a
, t
, current
, parms
;
5492 tree tinfo
= get_template_info (decl
);
5496 error ("template definition of non-template %q#D", decl
);
5497 return error_mark_node
;
5500 tmpl
= TI_TEMPLATE (tinfo
);
5502 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5503 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5504 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5505 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5509 /* The declaration is a specialization of a member
5510 template, declared outside the class. Therefore, the
5511 innermost template arguments will be NULL, so we
5512 replace them with the arguments determined by the
5513 earlier call to check_explicit_specialization. */
5514 args
= DECL_TI_ARGS (decl
);
5517 = build_template_decl (decl
, current_template_parms
,
5519 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5520 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5521 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5522 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5523 DECL_TEMPLATE_INFO (new_tmpl
)
5524 = build_template_info (tmpl
, args
);
5526 register_specialization (new_tmpl
,
5527 most_general_template (tmpl
),
5533 /* Make sure the template headers we got make sense. */
5535 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5536 i
= TMPL_PARMS_DEPTH (parms
);
5537 if (TMPL_ARGS_DEPTH (args
) != i
)
5539 error ("expected %d levels of template parms for %q#D, got %d",
5540 i
, decl
, TMPL_ARGS_DEPTH (args
));
5541 DECL_INTERFACE_KNOWN (decl
) = 1;
5542 return error_mark_node
;
5545 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5547 a
= TMPL_ARGS_LEVEL (args
, i
);
5548 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5550 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5552 if (current
== decl
)
5553 error ("got %d template parameters for %q#D",
5554 TREE_VEC_LENGTH (a
), decl
);
5556 error ("got %d template parameters for %q#T",
5557 TREE_VEC_LENGTH (a
), current
);
5558 error (" but %d required", TREE_VEC_LENGTH (t
));
5559 /* Avoid crash in import_export_decl. */
5560 DECL_INTERFACE_KNOWN (decl
) = 1;
5561 return error_mark_node
;
5564 if (current
== decl
)
5566 else if (current
== NULL_TREE
)
5567 /* Can happen in erroneous input. */
5570 current
= get_containing_scope (current
);
5573 /* Check that the parms are used in the appropriate qualifying scopes
5574 in the declarator. */
5575 if (!comp_template_args
5577 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5579 error ("template arguments to %qD do not match original "
5580 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5581 if (!uses_template_parms (TI_ARGS (tinfo
)))
5582 inform (input_location
, "use %<template<>%> for"
5583 " an explicit specialization");
5584 /* Avoid crash in import_export_decl. */
5585 DECL_INTERFACE_KNOWN (decl
) = 1;
5586 return error_mark_node
;
5590 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5591 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5593 /* Push template declarations for global functions and types. Note
5594 that we do not try to push a global template friend declared in a
5595 template class; such a thing may well depend on the template
5596 parameters of the class. */
5597 if (new_template_p
&& !ctx
5598 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5600 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5601 if (tmpl
== error_mark_node
)
5602 return error_mark_node
;
5604 /* Hide template friend classes that haven't been declared yet. */
5605 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5607 DECL_ANTICIPATED (tmpl
) = 1;
5608 DECL_FRIEND_P (tmpl
) = 1;
5614 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5616 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5618 /* Give template template parms a DECL_CONTEXT of the template
5619 for which they are a parameter. */
5620 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5621 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5623 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5624 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5625 DECL_CONTEXT (parm
) = tmpl
;
5628 if (TREE_CODE (decl
) == TYPE_DECL
5629 && TYPE_DECL_ALIAS_P (decl
)
5630 && complex_alias_template_p (tmpl
))
5631 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5634 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5635 back to its most general template. If TMPL is a specialization,
5636 ARGS may only have the innermost set of arguments. Add the missing
5637 argument levels if necessary. */
5638 if (DECL_TEMPLATE_INFO (tmpl
))
5639 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5641 info
= build_template_info (tmpl
, args
);
5643 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5644 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5648 retrofit_lang_decl (decl
);
5649 if (DECL_LANG_SPECIFIC (decl
))
5650 DECL_TEMPLATE_INFO (decl
) = info
;
5653 if (flag_implicit_templates
5655 && TREE_PUBLIC (decl
)
5656 && VAR_OR_FUNCTION_DECL_P (decl
))
5657 /* Set DECL_COMDAT on template instantiations; if we force
5658 them to be emitted by explicit instantiation or -frepo,
5659 mark_needed will tell cgraph to do the right thing. */
5660 DECL_COMDAT (decl
) = true;
5662 return DECL_TEMPLATE_RESULT (tmpl
);
5666 push_template_decl (tree decl
)
5668 return push_template_decl_real (decl
, false);
5671 /* FN is an inheriting constructor that inherits from the constructor
5672 template INHERITED; turn FN into a constructor template with a matching
5676 add_inherited_template_parms (tree fn
, tree inherited
)
5679 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5680 inner_parms
= copy_node (inner_parms
);
5682 = tree_cons (size_int (processing_template_decl
+ 1),
5683 inner_parms
, current_template_parms
);
5684 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5685 tree args
= template_parms_to_args (parms
);
5686 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5687 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5688 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5689 DECL_ARTIFICIAL (tmpl
) = true;
5690 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5694 /* Called when a class template TYPE is redeclared with the indicated
5695 template PARMS, e.g.:
5697 template <class T> struct S;
5698 template <class T> struct S {}; */
5701 redeclare_class_template (tree type
, tree parms
, tree cons
)
5707 if (!TYPE_TEMPLATE_INFO (type
))
5709 error ("%qT is not a template type", type
);
5713 tmpl
= TYPE_TI_TEMPLATE (type
);
5714 if (!PRIMARY_TEMPLATE_P (tmpl
))
5715 /* The type is nested in some template class. Nothing to worry
5716 about here; there are no new template parameters for the nested
5722 error ("template specifiers not specified in declaration of %qD",
5727 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5728 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5730 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5732 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5733 "redeclared with %d template parameter",
5734 "redeclared with %d template parameters",
5735 TREE_VEC_LENGTH (parms
));
5736 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5737 "previous declaration %qD used %d template parameter",
5738 "previous declaration %qD used %d template parameters",
5739 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5743 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5750 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5751 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5754 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5755 if (error_operand_p (tmpl_parm
))
5758 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5759 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5760 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5762 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5764 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5765 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5766 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5767 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5768 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5769 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5770 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5771 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5772 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5774 error ("template parameter %q+#D", tmpl_parm
);
5775 error ("redeclared here as %q#D", parm
);
5779 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5781 /* We have in [temp.param]:
5783 A template-parameter may not be given default arguments
5784 by two different declarations in the same scope. */
5785 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5786 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5787 "original definition appeared here");
5791 if (parm_default
!= NULL_TREE
)
5792 /* Update the previous template parameters (which are the ones
5793 that will really count) with the new default value. */
5794 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5795 else if (tmpl_default
!= NULL_TREE
)
5796 /* Update the new parameters, too; they'll be used as the
5797 parameters for any members. */
5798 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5800 /* Give each template template parm in this redeclaration a
5801 DECL_CONTEXT of the template for which they are a parameter. */
5802 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5804 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5805 DECL_CONTEXT (parm
) = tmpl
;
5808 if (TREE_CODE (parm
) == TYPE_DECL
)
5809 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5812 // Cannot redeclare a class template with a different set of constraints.
5813 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5815 error_at (input_location
, "redeclaration %q#D with different "
5816 "constraints", tmpl
);
5817 inform (DECL_SOURCE_LOCATION (tmpl
),
5818 "original declaration appeared here");
5824 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5825 to be used when the caller has already checked
5826 (processing_template_decl
5827 && !instantiation_dependent_expression_p (expr)
5828 && potential_constant_expression (expr))
5829 and cleared processing_template_decl. */
5832 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5834 return tsubst_copy_and_build (expr
,
5837 /*in_decl=*/NULL_TREE
,
5838 /*function_p=*/false,
5839 /*integral_constant_expression_p=*/true);
5842 /* Simplify EXPR if it is a non-dependent expression. Returns the
5843 (possibly simplified) expression. */
5846 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5848 if (expr
== NULL_TREE
)
5851 /* If we're in a template, but EXPR isn't value dependent, simplify
5852 it. We're supposed to treat:
5854 template <typename T> void f(T[1 + 1]);
5855 template <typename T> void f(T[2]);
5857 as two declarations of the same function, for example. */
5858 if (processing_template_decl
5859 && is_nondependent_constant_expression (expr
))
5861 processing_template_decl_sentinel s
;
5862 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5868 instantiate_non_dependent_expr (tree expr
)
5870 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5873 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5874 an uninstantiated expression. */
5877 instantiate_non_dependent_or_null (tree expr
)
5879 if (expr
== NULL_TREE
)
5881 if (processing_template_decl
)
5883 if (!is_nondependent_constant_expression (expr
))
5887 processing_template_decl_sentinel s
;
5888 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5894 /* True iff T is a specialization of a variable template. */
5897 variable_template_specialization_p (tree t
)
5899 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5901 tree tmpl
= DECL_TI_TEMPLATE (t
);
5902 return variable_template_p (tmpl
);
5905 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5906 template declaration, or a TYPE_DECL for an alias declaration. */
5909 alias_type_or_template_p (tree t
)
5913 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5916 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5917 || DECL_ALIAS_TEMPLATE_P (t
));
5920 /* Return TRUE iff T is a specialization of an alias template. */
5923 alias_template_specialization_p (const_tree t
)
5925 /* It's an alias template specialization if it's an alias and its
5926 TYPE_NAME is a specialization of a primary template. */
5927 if (TYPE_ALIAS_P (t
))
5928 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5929 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5934 /* An alias template is complex from a SFINAE perspective if a template-id
5935 using that alias can be ill-formed when the expansion is not, as with
5936 the void_t template. We determine this by checking whether the
5937 expansion for the alias template uses all its template parameters. */
5939 struct uses_all_template_parms_data
5946 uses_all_template_parms_r (tree t
, void *data_
)
5948 struct uses_all_template_parms_data
&data
5949 = *(struct uses_all_template_parms_data
*)data_
;
5950 tree idx
= get_template_parm_index (t
);
5952 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5953 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5958 complex_alias_template_p (const_tree tmpl
)
5960 struct uses_all_template_parms_data data
;
5961 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5962 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5963 data
.level
= TMPL_PARMS_DEPTH (parms
);
5964 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5965 data
.seen
= XALLOCAVEC (bool, len
);
5966 for (int i
= 0; i
< len
; ++i
)
5967 data
.seen
[i
] = false;
5969 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5970 for (int i
= 0; i
< len
; ++i
)
5976 /* Return TRUE iff T is a specialization of a complex alias template with
5977 dependent template-arguments. */
5980 dependent_alias_template_spec_p (const_tree t
)
5982 if (!alias_template_specialization_p (t
))
5985 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5986 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5989 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5990 if (!any_dependent_template_arguments_p (args
))
5996 /* Return the number of innermost template parameters in TMPL. */
5999 num_innermost_template_parms (tree tmpl
)
6001 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6002 return TREE_VEC_LENGTH (parms
);
6005 /* Return either TMPL or another template that it is equivalent to under DR
6006 1286: An alias that just changes the name of a template is equivalent to
6007 the other template. */
6010 get_underlying_template (tree tmpl
)
6012 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6013 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6015 /* Determine if the alias is equivalent to an underlying template. */
6016 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6017 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6021 tree underlying
= TI_TEMPLATE (tinfo
);
6022 if (!PRIMARY_TEMPLATE_P (underlying
)
6023 || (num_innermost_template_parms (tmpl
)
6024 != num_innermost_template_parms (underlying
)))
6027 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6028 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6029 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6032 /* Alias is equivalent. Strip it and repeat. */
6039 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6040 must be a reference-to-function or a pointer-to-function type, as specified
6041 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6042 and check that the resulting function has external linkage. */
6045 convert_nontype_argument_function (tree type
, tree expr
,
6046 tsubst_flags_t complain
)
6050 linkage_kind linkage
;
6052 fn
= instantiate_type (type
, fns
, tf_none
);
6053 if (fn
== error_mark_node
)
6054 return error_mark_node
;
6056 if (value_dependent_expression_p (fn
))
6059 fn_no_ptr
= strip_fnptr_conv (fn
);
6060 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6061 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6062 if (BASELINK_P (fn_no_ptr
))
6063 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6065 /* [temp.arg.nontype]/1
6067 A template-argument for a non-type, non-template template-parameter
6070 -- the address of an object or function with external [C++11: or
6071 internal] linkage. */
6073 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6075 if (complain
& tf_error
)
6077 error ("%qE is not a valid template argument for type %qT",
6079 if (TYPE_PTR_P (type
))
6080 inform (input_location
, "it must be the address of a function "
6081 "with external linkage");
6083 inform (input_location
, "it must be the name of a function with "
6084 "external linkage");
6089 linkage
= decl_linkage (fn_no_ptr
);
6090 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6092 if (complain
& tf_error
)
6094 if (cxx_dialect
>= cxx11
)
6095 error ("%qE is not a valid template argument for type %qT "
6096 "because %qD has no linkage",
6097 expr
, type
, fn_no_ptr
);
6099 error ("%qE is not a valid template argument for type %qT "
6100 "because %qD does not have external linkage",
6101 expr
, type
, fn_no_ptr
);
6107 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6108 fn
= build_address (fn
);
6109 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6110 fn
= build_nop (type
, fn
);
6115 /* Subroutine of convert_nontype_argument.
6116 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6117 Emit an error otherwise. */
6120 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6121 tsubst_flags_t complain
)
6123 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6124 tree orig_expr
= expr
;
6126 if (null_ptr_cst_p (expr
))
6128 if (TREE_CODE (expr
) == PTRMEM_CST
6129 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6130 PTRMEM_CST_CLASS (expr
)))
6132 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6134 if (processing_template_decl
6135 && TREE_CODE (expr
) == ADDR_EXPR
6136 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6138 if (complain
& tf_error
)
6140 error_at (loc
, "%qE is not a valid template argument for type %qT",
6142 if (TREE_CODE (expr
) != PTRMEM_CST
)
6143 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6145 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6150 /* Returns TRUE iff the address of OP is value-dependent.
6152 14.6.2.4 [temp.dep.temp]:
6153 A non-integral non-type template-argument is dependent if its type is
6154 dependent or it has either of the following forms
6157 and contains a nested-name-specifier which specifies a class-name that
6158 names a dependent type.
6160 We generalize this to just say that the address of a member of a
6161 dependent class is value-dependent; the above doesn't cover the
6162 address of a static data member named with an unqualified-id. */
6165 has_value_dependent_address (tree op
)
6167 /* We could use get_inner_reference here, but there's no need;
6168 this is only relevant for template non-type arguments, which
6169 can only be expressed as &id-expression. */
6172 tree ctx
= CP_DECL_CONTEXT (op
);
6173 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6180 /* The next set of functions are used for providing helpful explanatory
6181 diagnostics for failed overload resolution. Their messages should be
6182 indented by two spaces for consistency with the messages in
6186 unify_success (bool /*explain_p*/)
6191 /* Other failure functions should call this one, to provide a single function
6192 for setting a breakpoint on. */
6195 unify_invalid (bool /*explain_p*/)
6201 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6204 inform (input_location
,
6205 " couldn't deduce template parameter %qD", parm
);
6206 return unify_invalid (explain_p
);
6210 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6213 inform (input_location
,
6214 " types %qT and %qT have incompatible cv-qualifiers",
6216 return unify_invalid (explain_p
);
6220 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6223 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6224 return unify_invalid (explain_p
);
6228 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6231 inform (input_location
,
6232 " template parameter %qD is not a parameter pack, but "
6235 return unify_invalid (explain_p
);
6239 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6242 inform (input_location
,
6243 " template argument %qE does not match "
6244 "pointer-to-member constant %qE",
6246 return unify_invalid (explain_p
);
6250 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6253 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6254 return unify_invalid (explain_p
);
6258 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6261 inform (input_location
,
6262 " inconsistent parameter pack deduction with %qT and %qT",
6264 return unify_invalid (explain_p
);
6268 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6273 inform (input_location
,
6274 " deduced conflicting types for parameter %qT (%qT and %qT)",
6275 parm
, first
, second
);
6277 inform (input_location
,
6278 " deduced conflicting values for non-type parameter "
6279 "%qE (%qE and %qE)", parm
, first
, second
);
6281 return unify_invalid (explain_p
);
6285 unify_vla_arg (bool explain_p
, tree arg
)
6288 inform (input_location
,
6289 " variable-sized array type %qT is not "
6290 "a valid template argument",
6292 return unify_invalid (explain_p
);
6296 unify_method_type_error (bool explain_p
, tree arg
)
6299 inform (input_location
,
6300 " member function type %qT is not a valid template argument",
6302 return unify_invalid (explain_p
);
6306 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6311 inform_n (input_location
, wanted
,
6312 " candidate expects at least %d argument, %d provided",
6313 " candidate expects at least %d arguments, %d provided",
6316 inform_n (input_location
, wanted
,
6317 " candidate expects %d argument, %d provided",
6318 " candidate expects %d arguments, %d provided",
6321 return unify_invalid (explain_p
);
6325 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6327 return unify_arity (explain_p
, have
, wanted
);
6331 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6332 bool least_p
= false)
6334 return unify_arity (explain_p
, have
, wanted
, least_p
);
6338 unify_arg_conversion (bool explain_p
, tree to_type
,
6339 tree from_type
, tree arg
)
6342 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6343 " cannot convert %qE (type %qT) to type %qT",
6344 arg
, from_type
, to_type
);
6345 return unify_invalid (explain_p
);
6349 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6350 tree parm
, tree arg
)
6355 case tbr_ambiguous_baseclass
:
6356 inform (input_location
, " %qT is an ambiguous base class of %qT",
6360 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6363 return unify_invalid (explain_p
);
6367 unify_inconsistent_template_template_parameters (bool explain_p
)
6370 inform (input_location
,
6371 " template parameters of a template template argument are "
6372 "inconsistent with other deduced template arguments");
6373 return unify_invalid (explain_p
);
6377 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6380 inform (input_location
,
6381 " can't deduce a template for %qT from non-template type %qT",
6383 return unify_invalid (explain_p
);
6387 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6390 inform (input_location
,
6391 " template argument %qE does not match %qE", arg
, parm
);
6392 return unify_invalid (explain_p
);
6395 /* Attempt to convert the non-type template parameter EXPR to the
6396 indicated TYPE. If the conversion is successful, return the
6397 converted value. If the conversion is unsuccessful, return
6398 NULL_TREE if we issued an error message, or error_mark_node if we
6399 did not. We issue error messages for out-and-out bad template
6400 parameters, but not simply because the conversion failed, since we
6401 might be just trying to do argument deduction. Both TYPE and EXPR
6402 must be non-dependent.
6404 The conversion follows the special rules described in
6405 [temp.arg.nontype], and it is much more strict than an implicit
6408 This function is called twice for each template argument (see
6409 lookup_template_class for a more accurate description of this
6410 problem). This means that we need to handle expressions which
6411 are not valid in a C++ source, but can be created from the
6412 first call (for instance, casts to perform conversions). These
6413 hacks can go away after we fix the double coercion problem. */
6416 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6419 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6420 tree orig_expr
= expr
;
6422 /* Detect immediately string literals as invalid non-type argument.
6423 This special-case is not needed for correctness (we would easily
6424 catch this later), but only to provide better diagnostic for this
6425 common user mistake. As suggested by DR 100, we do not mention
6426 linkage issues in the diagnostic as this is not the point. */
6427 /* FIXME we're making this OK. */
6428 if (TREE_CODE (expr
) == STRING_CST
)
6430 if (complain
& tf_error
)
6431 error ("%qE is not a valid template argument for type %qT "
6432 "because string literals can never be used in this context",
6437 /* Add the ADDR_EXPR now for the benefit of
6438 value_dependent_expression_p. */
6439 if (TYPE_PTROBV_P (type
)
6440 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6442 expr
= decay_conversion (expr
, complain
);
6443 if (expr
== error_mark_node
)
6444 return error_mark_node
;
6447 /* If we are in a template, EXPR may be non-dependent, but still
6448 have a syntactic, rather than semantic, form. For example, EXPR
6449 might be a SCOPE_REF, rather than the VAR_DECL to which the
6450 SCOPE_REF refers. Preserving the qualifying scope is necessary
6451 so that access checking can be performed when the template is
6452 instantiated -- but here we need the resolved form so that we can
6453 convert the argument. */
6454 bool non_dep
= false;
6455 if (TYPE_REF_OBJ_P (type
)
6456 && has_value_dependent_address (expr
))
6457 /* If we want the address and it's value-dependent, don't fold. */;
6458 else if (processing_template_decl
6459 && is_nondependent_constant_expression (expr
))
6461 if (error_operand_p (expr
))
6462 return error_mark_node
;
6463 expr_type
= TREE_TYPE (expr
);
6465 /* If the argument is non-dependent, perform any conversions in
6466 non-dependent context as well. */
6467 processing_template_decl_sentinel
s (non_dep
);
6469 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6471 if (value_dependent_expression_p (expr
))
6472 expr
= canonicalize_expr_argument (expr
, complain
);
6474 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6475 to a non-type argument of "nullptr". */
6476 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6477 expr
= fold_simple (convert (type
, expr
));
6479 /* In C++11, integral or enumeration non-type template arguments can be
6480 arbitrary constant expressions. Pointer and pointer to
6481 member arguments can be general constant expressions that evaluate
6482 to a null value, but otherwise still need to be of a specific form. */
6483 if (cxx_dialect
>= cxx11
)
6485 if (TREE_CODE (expr
) == PTRMEM_CST
)
6486 /* A PTRMEM_CST is already constant, and a valid template
6487 argument for a parameter of pointer to member type, we just want
6488 to leave it in that form rather than lower it to a
6490 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6491 || cxx_dialect
>= cxx17
)
6493 /* C++17: A template-argument for a non-type template-parameter shall
6494 be a converted constant expression (8.20) of the type of the
6495 template-parameter. */
6496 expr
= build_converted_constant_expr (type
, expr
, complain
);
6497 if (expr
== error_mark_node
)
6498 return error_mark_node
;
6499 expr
= maybe_constant_value (expr
);
6500 expr
= convert_from_reference (expr
);
6502 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6504 tree folded
= maybe_constant_value (expr
);
6505 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6506 : null_member_pointer_value_p (folded
))
6511 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6512 expr
= mark_lvalue_use (expr
);
6514 expr
= mark_rvalue_use (expr
);
6516 /* HACK: Due to double coercion, we can get a
6517 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6518 which is the tree that we built on the first call (see
6519 below when coercing to reference to object or to reference to
6520 function). We just strip everything and get to the arg.
6521 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6523 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6525 tree probe_type
, probe
= expr
;
6526 if (REFERENCE_REF_P (probe
))
6527 probe
= TREE_OPERAND (probe
, 0);
6528 probe_type
= TREE_TYPE (probe
);
6529 if (TREE_CODE (probe
) == NOP_EXPR
)
6531 /* ??? Maybe we could use convert_from_reference here, but we
6532 would need to relax its constraints because the NOP_EXPR
6533 could actually change the type to something more cv-qualified,
6534 and this is not folded by convert_from_reference. */
6535 tree addr
= TREE_OPERAND (probe
, 0);
6536 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6537 && TREE_CODE (addr
) == ADDR_EXPR
6538 && TYPE_PTR_P (TREE_TYPE (addr
))
6539 && (same_type_ignoring_top_level_qualifiers_p
6540 (TREE_TYPE (probe_type
),
6541 TREE_TYPE (TREE_TYPE (addr
)))))
6543 expr
= TREE_OPERAND (addr
, 0);
6544 expr_type
= TREE_TYPE (probe_type
);
6549 /* [temp.arg.nontype]/5, bullet 1
6551 For a non-type template-parameter of integral or enumeration type,
6552 integral promotions (_conv.prom_) and integral conversions
6553 (_conv.integral_) are applied. */
6554 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6556 if (cxx_dialect
< cxx11
)
6558 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6559 t
= maybe_constant_value (t
);
6560 if (t
!= error_mark_node
)
6564 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6565 return error_mark_node
;
6567 /* Notice that there are constant expressions like '4 % 0' which
6568 do not fold into integer constants. */
6569 if (TREE_CODE (expr
) != INTEGER_CST
6570 && !value_dependent_expression_p (expr
))
6572 if (complain
& tf_error
)
6574 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6575 if (!require_potential_constant_expression (expr
))
6576 expr
= error_mark_node
;
6578 expr
= cxx_constant_value (expr
);
6579 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6580 inform (loc
, "in template argument for type %qT ", type
);
6581 if (expr
== error_mark_node
)
6583 /* else cxx_constant_value complained but gave us
6584 a real constant, so go ahead. */
6585 if (TREE_CODE (expr
) != INTEGER_CST
)
6587 /* Some assemble time constant expressions like
6588 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6589 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6590 as we can emit them into .rodata initializers of
6591 variables, yet they can't fold into an INTEGER_CST at
6592 compile time. Refuse them here. */
6593 gcc_checking_assert (reduced_constant_expression_p (expr
));
6594 error_at (loc
, "template argument %qE for type %qT not "
6595 "a constant integer", expr
, type
);
6603 /* Avoid typedef problems. */
6604 if (TREE_TYPE (expr
) != type
)
6605 expr
= fold_convert (type
, expr
);
6607 /* [temp.arg.nontype]/5, bullet 2
6609 For a non-type template-parameter of type pointer to object,
6610 qualification conversions (_conv.qual_) and the array-to-pointer
6611 conversion (_conv.array_) are applied. */
6612 else if (TYPE_PTROBV_P (type
))
6614 tree decayed
= expr
;
6616 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6617 decay_conversion or an explicit cast. If it's a problematic cast,
6618 we'll complain about it below. */
6619 if (TREE_CODE (expr
) == NOP_EXPR
)
6623 if (TREE_CODE (probe
) == ADDR_EXPR
6624 && TYPE_PTR_P (TREE_TYPE (probe
)))
6627 expr_type
= TREE_TYPE (expr
);
6631 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6633 A template-argument for a non-type, non-template template-parameter
6634 shall be one of: [...]
6636 -- the name of a non-type template-parameter;
6637 -- the address of an object or function with external linkage, [...]
6638 expressed as "& id-expression" where the & is optional if the name
6639 refers to a function or array, or if the corresponding
6640 template-parameter is a reference.
6642 Here, we do not care about functions, as they are invalid anyway
6643 for a parameter of type pointer-to-object. */
6645 if (value_dependent_expression_p (expr
))
6646 /* Non-type template parameters are OK. */
6648 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6649 /* Null pointer values are OK in C++11. */;
6650 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6654 if (complain
& tf_error
)
6655 error ("%qD is not a valid template argument "
6656 "because %qD is a variable, not the address of "
6657 "a variable", orig_expr
, expr
);
6660 if (POINTER_TYPE_P (expr_type
))
6662 if (complain
& tf_error
)
6663 error ("%qE is not a valid template argument for %qT "
6664 "because it is not the address of a variable",
6668 /* Other values, like integer constants, might be valid
6669 non-type arguments of some other type. */
6670 return error_mark_node
;
6674 tree decl
= TREE_OPERAND (expr
, 0);
6678 if (complain
& tf_error
)
6679 error ("%qE is not a valid template argument of type %qT "
6680 "because %qE is not a variable", orig_expr
, type
, decl
);
6683 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6685 if (complain
& tf_error
)
6686 error ("%qE is not a valid template argument of type %qT "
6687 "because %qD does not have external linkage",
6688 orig_expr
, type
, decl
);
6691 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6692 && decl_linkage (decl
) == lk_none
)
6694 if (complain
& tf_error
)
6695 error ("%qE is not a valid template argument of type %qT "
6696 "because %qD has no linkage", orig_expr
, type
, decl
);
6699 /* C++17: For a non-type template-parameter of reference or pointer
6700 type, the value of the constant expression shall not refer to (or
6701 for a pointer type, shall not be the address of):
6702 * a subobject (4.5),
6703 * a temporary object (15.2),
6704 * a string literal (5.13.5),
6705 * the result of a typeid expression (8.2.8), or
6706 * a predefined __func__ variable (11.4.1). */
6707 else if (DECL_ARTIFICIAL (decl
))
6709 if (complain
& tf_error
)
6710 error ("the address of %qD is not a valid template argument",
6714 else if (!same_type_ignoring_top_level_qualifiers_p
6715 (strip_array_types (TREE_TYPE (type
)),
6716 strip_array_types (TREE_TYPE (decl
))))
6718 if (complain
& tf_error
)
6719 error ("the address of the %qT subobject of %qD is not a "
6720 "valid template argument", TREE_TYPE (type
), decl
);
6723 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6725 if (complain
& tf_error
)
6726 error ("the address of %qD is not a valid template argument "
6727 "because it does not have static storage duration",
6735 expr
= perform_qualification_conversions (type
, expr
);
6736 if (expr
== error_mark_node
)
6737 return error_mark_node
;
6739 /* [temp.arg.nontype]/5, bullet 3
6741 For a non-type template-parameter of type reference to object, no
6742 conversions apply. The type referred to by the reference may be more
6743 cv-qualified than the (otherwise identical) type of the
6744 template-argument. The template-parameter is bound directly to the
6745 template-argument, which must be an lvalue. */
6746 else if (TYPE_REF_OBJ_P (type
))
6748 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6750 return error_mark_node
;
6752 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6754 if (complain
& tf_error
)
6755 error ("%qE is not a valid template argument for type %qT "
6756 "because of conflicts in cv-qualification", expr
, type
);
6760 if (!lvalue_p (expr
))
6762 if (complain
& tf_error
)
6763 error ("%qE is not a valid template argument for type %qT "
6764 "because it is not an lvalue", expr
, type
);
6768 /* [temp.arg.nontype]/1
6770 A template-argument for a non-type, non-template template-parameter
6771 shall be one of: [...]
6773 -- the address of an object or function with external linkage. */
6774 if (INDIRECT_REF_P (expr
)
6775 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6777 expr
= TREE_OPERAND (expr
, 0);
6780 if (complain
& tf_error
)
6781 error ("%q#D is not a valid template argument for type %qT "
6782 "because a reference variable does not have a constant "
6783 "address", expr
, type
);
6788 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6789 && value_dependent_expression_p (expr
))
6790 /* OK, dependent reference. We don't want to ask whether a DECL is
6791 itself value-dependent, since what we want here is its address. */;
6796 if (complain
& tf_error
)
6797 error ("%qE is not a valid template argument for type %qT "
6798 "because it is not an object with linkage",
6803 /* DR 1155 allows internal linkage in C++11 and up. */
6804 linkage_kind linkage
= decl_linkage (expr
);
6805 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6807 if (complain
& tf_error
)
6808 error ("%qE is not a valid template argument for type %qT "
6809 "because object %qD does not have linkage",
6814 expr
= build_address (expr
);
6817 if (!same_type_p (type
, TREE_TYPE (expr
)))
6818 expr
= build_nop (type
, expr
);
6820 /* [temp.arg.nontype]/5, bullet 4
6822 For a non-type template-parameter of type pointer to function, only
6823 the function-to-pointer conversion (_conv.func_) is applied. If the
6824 template-argument represents a set of overloaded functions (or a
6825 pointer to such), the matching function is selected from the set
6827 else if (TYPE_PTRFN_P (type
))
6829 /* If the argument is a template-id, we might not have enough
6830 context information to decay the pointer. */
6831 if (!type_unknown_p (expr_type
))
6833 expr
= decay_conversion (expr
, complain
);
6834 if (expr
== error_mark_node
)
6835 return error_mark_node
;
6838 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6839 /* Null pointer values are OK in C++11. */
6840 return perform_qualification_conversions (type
, expr
);
6842 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6843 if (!expr
|| expr
== error_mark_node
)
6846 /* [temp.arg.nontype]/5, bullet 5
6848 For a non-type template-parameter of type reference to function, no
6849 conversions apply. If the template-argument represents a set of
6850 overloaded functions, the matching function is selected from the set
6852 else if (TYPE_REFFN_P (type
))
6854 if (TREE_CODE (expr
) == ADDR_EXPR
)
6856 if (complain
& tf_error
)
6858 error ("%qE is not a valid template argument for type %qT "
6859 "because it is a pointer", expr
, type
);
6860 inform (input_location
, "try using %qE instead",
6861 TREE_OPERAND (expr
, 0));
6866 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6867 if (!expr
|| expr
== error_mark_node
)
6870 /* [temp.arg.nontype]/5, bullet 6
6872 For a non-type template-parameter of type pointer to member function,
6873 no conversions apply. If the template-argument represents a set of
6874 overloaded member functions, the matching member function is selected
6875 from the set (_over.over_). */
6876 else if (TYPE_PTRMEMFUNC_P (type
))
6878 expr
= instantiate_type (type
, expr
, tf_none
);
6879 if (expr
== error_mark_node
)
6880 return error_mark_node
;
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 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6889 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6890 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6891 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6893 /* [temp.arg.nontype]/5, bullet 7
6895 For a non-type template-parameter of type pointer to data member,
6896 qualification conversions (_conv.qual_) are applied. */
6897 else if (TYPE_PTRDATAMEM_P (type
))
6899 /* [temp.arg.nontype] bullet 1 says the pointer to member
6900 expression must be a pointer-to-member constant. */
6901 if (!value_dependent_expression_p (expr
)
6902 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6905 expr
= perform_qualification_conversions (type
, expr
);
6906 if (expr
== error_mark_node
)
6909 else if (NULLPTR_TYPE_P (type
))
6911 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6913 if (complain
& tf_error
)
6914 error ("%qE is not a valid template argument for type %qT "
6915 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6920 /* A template non-type parameter must be one of the above. */
6924 /* Sanity check: did we actually convert the argument to the
6926 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6927 (type
, TREE_TYPE (expr
)));
6928 return convert_from_reference (expr
);
6931 /* Subroutine of coerce_template_template_parms, which returns 1 if
6932 PARM_PARM and ARG_PARM match using the rule for the template
6933 parameters of template template parameters. Both PARM and ARG are
6934 template parameters; the rest of the arguments are the same as for
6935 coerce_template_template_parms.
6938 coerce_template_template_parm (tree parm
,
6940 tsubst_flags_t complain
,
6944 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6945 || parm
== NULL_TREE
|| error_operand_p (parm
))
6948 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6951 switch (TREE_CODE (parm
))
6954 /* We encounter instantiations of templates like
6955 template <template <template <class> class> class TT>
6958 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6959 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6961 if (!coerce_template_template_parms
6962 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6968 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6969 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6970 /* Argument is a parameter pack but parameter is not. */
6975 /* The tsubst call is used to handle cases such as
6977 template <int> class C {};
6978 template <class T, template <T> class TT> class D {};
6981 i.e. the parameter list of TT depends on earlier parameters. */
6982 if (!uses_template_parms (TREE_TYPE (arg
)))
6984 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6985 if (!uses_template_parms (t
)
6986 && !same_type_p (t
, TREE_TYPE (arg
)))
6990 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6991 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6992 /* Argument is a parameter pack but parameter is not. */
7004 /* Coerce template argument list ARGLIST for use with template
7005 template-parameter TEMPL. */
7008 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7009 tsubst_flags_t complain
)
7011 /* Consider an example where a template template parameter declared as
7013 template <class T, class U = std::allocator<T> > class TT
7015 The template parameter level of T and U are one level larger than
7016 of TT. To proper process the default argument of U, say when an
7017 instantiation `TT<int>' is seen, we need to build the full
7018 arguments containing {int} as the innermost level. Outer levels,
7019 available when not appearing as default template argument, can be
7020 obtained from the arguments of the enclosing template.
7022 Suppose that TT is later substituted with std::vector. The above
7023 instantiation is `TT<int, std::allocator<T> >' with TT at
7024 level 1, and T at level 2, while the template arguments at level 1
7025 becomes {std::vector} and the inner level 2 is {int}. */
7027 tree outer
= DECL_CONTEXT (templ
);
7030 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7031 /* We want arguments for the partial specialization, not arguments for
7032 the primary template. */
7033 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7035 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7037 else if (current_template_parms
)
7039 /* This is an argument of the current template, so we haven't set
7040 DECL_CONTEXT yet. */
7041 tree relevant_template_parms
;
7043 /* Parameter levels that are greater than the level of the given
7044 template template parm are irrelevant. */
7045 relevant_template_parms
= current_template_parms
;
7046 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7047 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7048 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7050 outer
= template_parms_to_args (relevant_template_parms
);
7054 arglist
= add_to_template_args (outer
, arglist
);
7056 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7057 return coerce_template_parms (parmlist
, arglist
, templ
,
7059 /*require_all_args=*/true,
7060 /*use_default_args=*/true);
7063 /* A cache of template template parameters with match-all default
7065 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7067 store_defaulted_ttp (tree v
, tree t
)
7069 if (!defaulted_ttp_cache
)
7070 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7071 defaulted_ttp_cache
->put (v
, t
);
7074 lookup_defaulted_ttp (tree v
)
7076 if (defaulted_ttp_cache
)
7077 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7082 /* T is a bound template template-parameter. Copy its arguments into default
7083 arguments of the template template-parameter's template parameters. */
7086 add_defaults_to_ttp (tree otmpl
)
7088 if (tree c
= lookup_defaulted_ttp (otmpl
))
7091 tree ntmpl
= copy_node (otmpl
);
7093 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7094 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7095 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7096 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7097 TYPE_NAME (ntype
) = ntmpl
;
7098 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7100 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7101 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7102 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7103 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7105 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7106 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7107 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7108 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7109 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7111 tree o
= TREE_VEC_ELT (vec
, i
);
7112 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7114 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7115 TREE_PURPOSE (n
) = any_targ_node
;
7119 store_defaulted_ttp (otmpl
, ntmpl
);
7123 /* ARG is a bound potential template template-argument, and PARGS is a list
7124 of arguments for the corresponding template template-parameter. Adjust
7125 PARGS as appropriate for application to ARG's template, and if ARG is a
7126 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7127 arguments to the template template parameter. */
7130 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7132 ++processing_template_decl
;
7133 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7134 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7136 /* When comparing two template template-parameters in partial ordering,
7137 rewrite the one currently being used as an argument to have default
7138 arguments for all parameters. */
7139 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7140 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7141 if (pargs
!= error_mark_node
)
7142 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7143 TYPE_TI_ARGS (arg
));
7148 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7149 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7150 /*require_all*/true,
7151 /*use_default*/true);
7153 --processing_template_decl
;
7157 /* Subroutine of unify for the case when PARM is a
7158 BOUND_TEMPLATE_TEMPLATE_PARM. */
7161 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7164 tree parmvec
= TYPE_TI_ARGS (parm
);
7165 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7167 /* The template template parm might be variadic and the argument
7168 not, so flatten both argument lists. */
7169 parmvec
= expand_template_argument_pack (parmvec
);
7170 argvec
= expand_template_argument_pack (argvec
);
7174 /* In keeping with P0522R0, adjust P's template arguments
7175 to apply to A's template; then flatten it again. */
7176 tree nparmvec
= parmvec
;
7177 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7178 nparmvec
= expand_template_argument_pack (nparmvec
);
7180 if (unify (tparms
, targs
, nparmvec
, argvec
,
7181 UNIFY_ALLOW_NONE
, explain_p
))
7184 /* If the P0522 adjustment eliminated a pack expansion, deduce
7187 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7188 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7189 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7194 /* Deduce arguments T, i from TT<T> or TT<i>.
7195 We check each element of PARMVEC and ARGVEC individually
7196 rather than the whole TREE_VEC since they can have
7197 different number of elements, which is allowed under N2555. */
7199 int len
= TREE_VEC_LENGTH (parmvec
);
7201 /* Check if the parameters end in a pack, making them
7203 int parm_variadic_p
= 0;
7205 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7206 parm_variadic_p
= 1;
7208 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7209 /* If the template argument list of P contains a pack
7210 expansion that is not the last template argument, the
7211 entire template argument list is a non-deduced
7213 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7214 return unify_success (explain_p
);
7216 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7217 return unify_too_few_arguments (explain_p
,
7218 TREE_VEC_LENGTH (argvec
), len
);
7220 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7221 if (unify (tparms
, targs
,
7222 TREE_VEC_ELT (parmvec
, i
),
7223 TREE_VEC_ELT (argvec
, i
),
7224 UNIFY_ALLOW_NONE
, explain_p
))
7228 && unify_pack_expansion (tparms
, targs
,
7231 /*subr=*/true, explain_p
))
7238 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7239 template template parameters. Both PARM_PARMS and ARG_PARMS are
7240 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7243 Consider the example:
7244 template <class T> class A;
7245 template<template <class U> class TT> class B;
7247 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7248 the parameters to A, and OUTER_ARGS contains A. */
7251 coerce_template_template_parms (tree parm_parms
,
7253 tsubst_flags_t complain
,
7257 int nparms
, nargs
, i
;
7261 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7262 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7264 nparms
= TREE_VEC_LENGTH (parm_parms
);
7265 nargs
= TREE_VEC_LENGTH (arg_parms
);
7269 /* P0522R0: A template template-parameter P is at least as specialized as
7270 a template template-argument A if, given the following rewrite to two
7271 function templates, the function template corresponding to P is at
7272 least as specialized as the function template corresponding to A
7273 according to the partial ordering rules for function templates
7274 ([temp.func.order]). Given an invented class template X with the
7275 template parameter list of A (including default arguments):
7277 * Each of the two function templates has the same template parameters,
7278 respectively, as P or A.
7280 * Each function template has a single function parameter whose type is
7281 a specialization of X with template arguments corresponding to the
7282 template parameters from the respective function template where, for
7283 each template parameter PP in the template parameter list of the
7284 function template, a corresponding template argument AA is formed. If
7285 PP declares a parameter pack, then AA is the pack expansion
7286 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7288 If the rewrite produces an invalid type, then P is not at least as
7289 specialized as A. */
7291 /* So coerce P's args to apply to A's parms, and then deduce between A's
7292 args and the converted args. If that succeeds, A is at least as
7293 specialized as P, so they match.*/
7294 tree pargs
= template_parms_level_to_args (parm_parms
);
7295 ++processing_template_decl
;
7296 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7297 /*require_all*/true, /*use_default*/true);
7298 --processing_template_decl
;
7299 if (pargs
!= error_mark_node
)
7301 tree targs
= make_tree_vec (nargs
);
7302 tree aargs
= template_parms_level_to_args (arg_parms
);
7303 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7309 /* Determine whether we have a parameter pack at the end of the
7310 template template parameter's template parameter list. */
7311 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7313 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7315 if (error_operand_p (parm
))
7318 switch (TREE_CODE (parm
))
7322 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7327 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7337 && !(variadic_p
&& nargs
>= nparms
- 1))
7340 /* Check all of the template parameters except the parameter pack at
7341 the end (if any). */
7342 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7344 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7345 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7348 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7349 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7351 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7359 /* Check each of the template parameters in the template
7360 argument against the template parameter pack at the end of
7361 the template template parameter. */
7362 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7365 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7367 for (; i
< nargs
; ++i
)
7369 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7372 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7374 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7383 /* Verifies that the deduced template arguments (in TARGS) for the
7384 template template parameters (in TPARMS) represent valid bindings,
7385 by comparing the template parameter list of each template argument
7386 to the template parameter list of its corresponding template
7387 template parameter, in accordance with DR150. This
7388 routine can only be called after all template arguments have been
7389 deduced. It will return TRUE if all of the template template
7390 parameter bindings are okay, FALSE otherwise. */
7392 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7394 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7397 /* We're dealing with template parms in this process. */
7398 ++processing_template_decl
;
7400 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7402 for (i
= 0; i
< ntparms
; ++i
)
7404 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7405 tree targ
= TREE_VEC_ELT (targs
, i
);
7407 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7409 tree packed_args
= NULL_TREE
;
7412 if (ARGUMENT_PACK_P (targ
))
7414 /* Look inside the argument pack. */
7415 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7416 len
= TREE_VEC_LENGTH (packed_args
);
7419 for (idx
= 0; idx
< len
; ++idx
)
7421 tree targ_parms
= NULL_TREE
;
7424 /* Extract the next argument from the argument
7426 targ
= TREE_VEC_ELT (packed_args
, idx
);
7428 if (PACK_EXPANSION_P (targ
))
7429 /* Look at the pattern of the pack expansion. */
7430 targ
= PACK_EXPANSION_PATTERN (targ
);
7432 /* Extract the template parameters from the template
7434 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7435 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7436 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7437 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7439 /* Verify that we can coerce the template template
7440 parameters from the template argument to the template
7441 parameter. This requires an exact match. */
7443 && !coerce_template_template_parms
7444 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7459 --processing_template_decl
;
7463 /* Since type attributes aren't mangled, we need to strip them from
7464 template type arguments. */
7467 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7469 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7471 bool removed_attributes
= false;
7472 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7473 if (removed_attributes
7474 && (complain
& tf_warning
))
7475 warning (OPT_Wignored_attributes
,
7476 "ignoring attributes on template argument %qT", arg
);
7480 /* And from inside dependent non-type arguments like sizeof(Type). */
7483 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7485 if (!arg
|| arg
== error_mark_node
)
7487 bool removed_attributes
= false;
7488 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7489 if (removed_attributes
7490 && (complain
& tf_warning
))
7491 warning (OPT_Wignored_attributes
,
7492 "ignoring attributes in template argument %qE", arg
);
7496 // A template declaration can be substituted for a constrained
7497 // template template parameter only when the argument is more
7498 // constrained than the parameter.
7500 is_compatible_template_arg (tree parm
, tree arg
)
7502 tree parm_cons
= get_constraints (parm
);
7504 /* For now, allow constrained template template arguments
7505 and unconstrained template template parameters. */
7506 if (parm_cons
== NULL_TREE
)
7509 tree arg_cons
= get_constraints (arg
);
7511 // If the template parameter is constrained, we need to rewrite its
7512 // constraints in terms of the ARG's template parameters. This ensures
7513 // that all of the template parameter types will have the same depth.
7515 // Note that this is only valid when coerce_template_template_parm is
7516 // true for the innermost template parameters of PARM and ARG. In other
7517 // words, because coercion is successful, this conversion will be valid.
7520 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7521 parm_cons
= tsubst_constraint_info (parm_cons
,
7522 INNERMOST_TEMPLATE_ARGS (args
),
7523 tf_none
, NULL_TREE
);
7524 if (parm_cons
== error_mark_node
)
7528 return subsumes (parm_cons
, arg_cons
);
7531 // Convert a placeholder argument into a binding to the original
7532 // parameter. The original parameter is saved as the TREE_TYPE of
7535 convert_wildcard_argument (tree parm
, tree arg
)
7537 TREE_TYPE (arg
) = parm
;
7541 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7542 because one of them is dependent. But we need to represent the
7543 conversion for the benefit of cp_tree_equal. */
7546 maybe_convert_nontype_argument (tree type
, tree arg
)
7548 /* Auto parms get no conversion. */
7549 if (type_uses_auto (type
))
7551 /* We don't need or want to add this conversion now if we're going to use the
7552 argument for deduction. */
7553 if (value_dependent_expression_p (arg
))
7556 type
= cv_unqualified (type
);
7557 tree argtype
= TREE_TYPE (arg
);
7558 if (same_type_p (type
, argtype
))
7561 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7562 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7566 /* Convert the indicated template ARG as necessary to match the
7567 indicated template PARM. Returns the converted ARG, or
7568 error_mark_node if the conversion was unsuccessful. Error and
7569 warning messages are issued under control of COMPLAIN. This
7570 conversion is for the Ith parameter in the parameter list. ARGS is
7571 the full set of template arguments deduced so far. */
7574 convert_template_argument (tree parm
,
7577 tsubst_flags_t complain
,
7583 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7585 if (parm
== error_mark_node
)
7586 return error_mark_node
;
7588 /* Trivially convert placeholders. */
7589 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7590 return convert_wildcard_argument (parm
, arg
);
7592 if (arg
== any_targ_node
)
7595 if (TREE_CODE (arg
) == TREE_LIST
7596 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7598 /* The template argument was the name of some
7599 member function. That's usually
7600 invalid, but static members are OK. In any
7601 case, grab the underlying fields/functions
7602 and issue an error later if required. */
7603 orig_arg
= TREE_VALUE (arg
);
7604 TREE_TYPE (arg
) = unknown_type_node
;
7609 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7610 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7611 || requires_tmpl_type
);
7613 /* When determining whether an argument pack expansion is a template,
7614 look at the pattern. */
7615 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7616 arg
= PACK_EXPANSION_PATTERN (arg
);
7618 /* Deal with an injected-class-name used as a template template arg. */
7619 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7621 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7622 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7624 if (cxx_dialect
>= cxx11
)
7625 /* OK under DR 1004. */;
7626 else if (complain
& tf_warning_or_error
)
7627 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7628 " used as template template argument", TYPE_NAME (arg
));
7629 else if (flag_pedantic_errors
)
7637 ((TREE_CODE (arg
) == TEMPLATE_DECL
7638 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7639 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7640 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7641 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7644 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7645 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7646 arg
= TYPE_STUB_DECL (arg
);
7648 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7650 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7651 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7653 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7655 if (complain
& tf_error
)
7656 error ("invalid use of destructor %qE as a type", orig_arg
);
7657 return error_mark_node
;
7660 permerror (input_location
,
7661 "to refer to a type member of a template parameter, "
7662 "use %<typename %E%>", orig_arg
);
7664 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7665 TREE_OPERAND (arg
, 1),
7671 if (is_type
!= requires_type
)
7675 if (complain
& tf_error
)
7677 error ("type/value mismatch at argument %d in template "
7678 "parameter list for %qD",
7681 inform (input_location
,
7682 " expected a constant of type %qT, got %qT",
7684 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7685 else if (requires_tmpl_type
)
7686 inform (input_location
,
7687 " expected a class template, got %qE", orig_arg
);
7689 inform (input_location
,
7690 " expected a type, got %qE", orig_arg
);
7693 return error_mark_node
;
7695 if (is_tmpl_type
^ requires_tmpl_type
)
7697 if (in_decl
&& (complain
& tf_error
))
7699 error ("type/value mismatch at argument %d in template "
7700 "parameter list for %qD",
7703 inform (input_location
,
7704 " expected a type, got %qT", DECL_NAME (arg
));
7706 inform (input_location
,
7707 " expected a class template, got %qT", orig_arg
);
7709 return error_mark_node
;
7712 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7713 /* We already did the appropriate conversion when packing args. */
7717 if (requires_tmpl_type
)
7719 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7720 /* The number of argument required is not known yet.
7721 Just accept it for now. */
7725 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7728 /* Strip alias templates that are equivalent to another
7730 arg
= get_underlying_template (arg
);
7731 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7733 if (coerce_template_template_parms (parmparm
, argparm
,
7739 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7741 if (val
!= error_mark_node
)
7743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7744 val
= TREE_TYPE (val
);
7745 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7746 val
= make_pack_expansion (val
, complain
);
7751 if (in_decl
&& (complain
& tf_error
))
7753 error ("type/value mismatch at argument %d in "
7754 "template parameter list for %qD",
7756 inform (input_location
,
7757 " expected a template of type %qD, got %qT",
7761 val
= error_mark_node
;
7764 // Check that the constraints are compatible before allowing the
7766 if (val
!= error_mark_node
)
7767 if (!is_compatible_template_arg (parm
, arg
))
7769 if (in_decl
&& (complain
& tf_error
))
7771 error ("constraint mismatch at argument %d in "
7772 "template parameter list for %qD",
7774 inform (input_location
, " expected %qD but got %qD",
7777 val
= error_mark_node
;
7783 /* We only form one instance of each template specialization.
7784 Therefore, if we use a non-canonical variant (i.e., a
7785 typedef), any future messages referring to the type will use
7786 the typedef, which is confusing if those future uses do not
7787 themselves also use the typedef. */
7789 val
= canonicalize_type_argument (val
, complain
);
7793 tree t
= TREE_TYPE (parm
);
7795 if (tree a
= type_uses_auto (t
))
7797 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7798 if (t
== error_mark_node
)
7799 return error_mark_node
;
7802 t
= tsubst (t
, args
, complain
, in_decl
);
7804 if (invalid_nontype_parm_type_p (t
, complain
))
7805 return error_mark_node
;
7807 if (!type_dependent_expression_p (orig_arg
)
7808 && !uses_template_parms (t
))
7809 /* We used to call digest_init here. However, digest_init
7810 will report errors, which we don't want when complain
7811 is zero. More importantly, digest_init will try too
7812 hard to convert things: for example, `0' should not be
7813 converted to pointer type at this point according to
7814 the standard. Accepting this is not merely an
7815 extension, since deciding whether or not these
7816 conversions can occur is part of determining which
7817 function template to call, or whether a given explicit
7818 argument specification is valid. */
7819 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7822 val
= canonicalize_expr_argument (orig_arg
, complain
);
7823 val
= maybe_convert_nontype_argument (t
, val
);
7827 if (val
== NULL_TREE
)
7828 val
= error_mark_node
;
7829 else if (val
== error_mark_node
&& (complain
& tf_error
))
7830 error ("could not convert template argument %qE from %qT to %qT",
7831 orig_arg
, TREE_TYPE (orig_arg
), t
);
7833 if (INDIRECT_REF_P (val
))
7835 /* Reject template arguments that are references to built-in
7836 functions with no library fallbacks. */
7837 const_tree inner
= TREE_OPERAND (val
, 0);
7838 const_tree innertype
= TREE_TYPE (inner
);
7840 && TREE_CODE (innertype
) == REFERENCE_TYPE
7841 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7842 && TREE_OPERAND_LENGTH (inner
) > 0
7843 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7844 return error_mark_node
;
7847 if (TREE_CODE (val
) == SCOPE_REF
)
7849 /* Strip typedefs from the SCOPE_REF. */
7850 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7851 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7853 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7854 QUALIFIED_NAME_IS_TEMPLATE (val
));
7861 /* Coerces the remaining template arguments in INNER_ARGS (from
7862 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7863 Returns the coerced argument pack. PARM_IDX is the position of this
7864 parameter in the template parameter list. ARGS is the original
7865 template argument list. */
7867 coerce_template_parameter_pack (tree parms
,
7875 tsubst_flags_t complain
)
7877 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7878 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7881 tree packed_parms
= NULL_TREE
;
7883 if (arg_idx
> nargs
)
7886 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7888 /* When the template parameter is a non-type template parameter pack
7889 or template template parameter pack whose type or template
7890 parameters use parameter packs, we know exactly how many arguments
7891 we are looking for. Build a vector of the instantiated decls for
7892 these template parameters in PACKED_PARMS. */
7893 /* We can't use make_pack_expansion here because it would interpret a
7894 _DECL as a use rather than a declaration. */
7895 tree decl
= TREE_VALUE (parm
);
7896 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7897 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7898 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7899 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7901 TREE_VEC_LENGTH (args
)--;
7902 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7903 TREE_VEC_LENGTH (args
)++;
7905 if (packed_parms
== error_mark_node
)
7906 return error_mark_node
;
7908 /* If we're doing a partial instantiation of a member template,
7909 verify that all of the types used for the non-type
7910 template parameter pack are, in fact, valid for non-type
7911 template parameters. */
7913 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7915 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7916 for (j
= 0; j
< len
; ++j
)
7918 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7919 if (invalid_nontype_parm_type_p (t
, complain
))
7920 return error_mark_node
;
7922 /* We don't know how many args we have yet, just
7923 use the unconverted ones for now. */
7927 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7929 /* Check if we have a placeholder pack, which indicates we're
7930 in the context of a introduction list. In that case we want
7931 to match this pack to the single placeholder. */
7932 else if (arg_idx
< nargs
7933 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7934 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7936 nargs
= arg_idx
+ 1;
7937 packed_args
= make_tree_vec (1);
7940 packed_args
= make_tree_vec (nargs
- arg_idx
);
7942 /* Convert the remaining arguments, which will be a part of the
7943 parameter pack "parm". */
7944 int first_pack_arg
= arg_idx
;
7945 for (; arg_idx
< nargs
; ++arg_idx
)
7947 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7948 tree actual_parm
= TREE_VALUE (parm
);
7949 int pack_idx
= arg_idx
- first_pack_arg
;
7953 /* Once we've packed as many args as we have types, stop. */
7954 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7956 else if (PACK_EXPANSION_P (arg
))
7957 /* We don't know how many args we have yet, just
7958 use the unconverted ones for now. */
7961 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7964 if (arg
== error_mark_node
)
7966 if (complain
& tf_error
)
7967 error ("template argument %d is invalid", arg_idx
+ 1);
7970 arg
= convert_template_argument (actual_parm
,
7971 arg
, new_args
, complain
, parm_idx
,
7973 if (arg
== error_mark_node
)
7975 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7978 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7979 && TREE_VEC_LENGTH (packed_args
) > 0)
7981 if (complain
& tf_error
)
7982 error ("wrong number of template arguments (%d, should be %d)",
7983 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7984 return error_mark_node
;
7987 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7988 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7989 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7992 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7993 TREE_CONSTANT (argument_pack
) = 1;
7996 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7998 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7999 TREE_VEC_LENGTH (packed_args
));
8000 return argument_pack
;
8003 /* Returns the number of pack expansions in the template argument vector
8007 pack_expansion_args_count (tree args
)
8012 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8014 tree elt
= TREE_VEC_ELT (args
, i
);
8015 if (elt
&& PACK_EXPANSION_P (elt
))
8021 /* Convert all template arguments to their appropriate types, and
8022 return a vector containing the innermost resulting template
8023 arguments. If any error occurs, return error_mark_node. Error and
8024 warning messages are issued under control of COMPLAIN.
8026 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8027 for arguments not specified in ARGS. Otherwise, if
8028 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8029 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8030 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8034 coerce_template_parms (tree parms
,
8037 tsubst_flags_t complain
,
8038 bool require_all_args
,
8039 bool use_default_args
)
8041 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8042 tree orig_inner_args
;
8045 tree new_inner_args
;
8046 int saved_unevaluated_operand
;
8047 int saved_inhibit_evaluation_warnings
;
8049 /* When used as a boolean value, indicates whether this is a
8050 variadic template parameter list. Since it's an int, we can also
8051 subtract it from nparms to get the number of non-variadic
8054 int variadic_args_p
= 0;
8055 int post_variadic_parms
= 0;
8057 /* Likewise for parameters with default arguments. */
8060 if (args
== error_mark_node
)
8061 return error_mark_node
;
8063 nparms
= TREE_VEC_LENGTH (parms
);
8065 /* Determine if there are any parameter packs or default arguments. */
8066 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8068 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8070 ++post_variadic_parms
;
8071 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8073 if (TREE_PURPOSE (parm
))
8077 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8078 /* If there are no parameters that follow a parameter pack, we need to
8079 expand any argument packs so that we can deduce a parameter pack from
8080 some non-packed args followed by an argument pack, as in variadic85.C.
8081 If there are such parameters, we need to leave argument packs intact
8082 so the arguments are assigned properly. This can happen when dealing
8083 with a nested class inside a partial specialization of a class
8084 template, as in variadic92.C, or when deducing a template parameter pack
8085 from a sub-declarator, as in variadic114.C. */
8086 if (!post_variadic_parms
)
8087 inner_args
= expand_template_argument_pack (inner_args
);
8089 /* Count any pack expansion args. */
8090 variadic_args_p
= pack_expansion_args_count (inner_args
);
8092 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8093 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8094 || (nargs
< nparms
- variadic_p
8097 && (!use_default_args
8098 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8099 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8101 if (complain
& tf_error
)
8103 if (variadic_p
|| default_p
)
8105 nparms
-= variadic_p
+ default_p
;
8106 error ("wrong number of template arguments "
8107 "(%d, should be at least %d)", nargs
, nparms
);
8110 error ("wrong number of template arguments "
8111 "(%d, should be %d)", nargs
, nparms
);
8114 inform (DECL_SOURCE_LOCATION (in_decl
),
8115 "provided for %qD", in_decl
);
8118 return error_mark_node
;
8120 /* We can't pass a pack expansion to a non-pack parameter of an alias
8121 template (DR 1430). */
8123 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8124 || concept_template_p (in_decl
))
8126 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8128 if (complain
& tf_error
)
8130 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8132 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8133 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8135 if (PACK_EXPANSION_P (arg
)
8136 && !template_parameter_pack_p (parm
))
8138 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8139 error_at (location_of (arg
),
8140 "pack expansion argument for non-pack parameter "
8141 "%qD of alias template %qD", parm
, in_decl
);
8143 error_at (location_of (arg
),
8144 "pack expansion argument for non-pack parameter "
8145 "%qD of concept %qD", parm
, in_decl
);
8146 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8153 return error_mark_node
;
8156 /* We need to evaluate the template arguments, even though this
8157 template-id may be nested within a "sizeof". */
8158 saved_unevaluated_operand
= cp_unevaluated_operand
;
8159 cp_unevaluated_operand
= 0;
8160 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8161 c_inhibit_evaluation_warnings
= 0;
8162 new_inner_args
= make_tree_vec (nparms
);
8163 new_args
= add_outermost_template_args (args
, new_inner_args
);
8164 int pack_adjust
= 0;
8165 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8170 /* Get the Ith template parameter. */
8171 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8173 if (parm
== error_mark_node
)
8175 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8179 /* Calculate the next argument. */
8180 if (arg_idx
< nargs
)
8181 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8185 if (template_parameter_pack_p (TREE_VALUE (parm
))
8186 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8188 /* Some arguments will be placed in the
8189 template parameter pack PARM. */
8190 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8191 inner_args
, arg_idx
,
8195 if (arg
== NULL_TREE
)
8197 /* We don't know how many args we have yet, just use the
8198 unconverted (and still packed) ones for now. */
8199 new_inner_args
= orig_inner_args
;
8204 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8206 /* Store this argument. */
8207 if (arg
== error_mark_node
)
8210 /* We are done with all of the arguments. */
8215 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8216 arg_idx
+= pack_adjust
;
8223 if (PACK_EXPANSION_P (arg
))
8225 /* "If every valid specialization of a variadic template
8226 requires an empty template parameter pack, the template is
8227 ill-formed, no diagnostic required." So check that the
8228 pattern works with this parameter. */
8229 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8230 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8234 if (conv
== error_mark_node
)
8236 if (complain
& tf_error
)
8237 inform (input_location
, "so any instantiation with a "
8238 "non-empty parameter pack would be ill-formed");
8241 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8242 /* Recover from missing typename. */
8243 TREE_VEC_ELT (inner_args
, arg_idx
)
8244 = make_pack_expansion (conv
, complain
);
8246 /* We don't know how many args we have yet, just
8247 use the unconverted ones for now. */
8248 new_inner_args
= inner_args
;
8253 else if (require_all_args
)
8255 /* There must be a default arg in this case. */
8256 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8258 /* The position of the first default template argument,
8259 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8261 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8262 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8263 arg_idx
- pack_adjust
);
8268 if (arg
== error_mark_node
)
8270 if (complain
& tf_error
)
8271 error ("template argument %d is invalid", arg_idx
+ 1);
8274 /* This only occurs if there was an error in the template
8275 parameter list itself (which we would already have
8276 reported) that we are trying to recover from, e.g., a class
8277 template with a parameter list such as
8278 template<typename..., typename>. */
8281 arg
= convert_template_argument (TREE_VALUE (parm
),
8282 arg
, new_args
, complain
,
8285 if (arg
== error_mark_node
)
8287 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8289 cp_unevaluated_operand
= saved_unevaluated_operand
;
8290 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8292 if (variadic_p
&& arg_idx
< nargs
)
8294 if (complain
& tf_error
)
8296 error ("wrong number of template arguments "
8297 "(%d, should be %d)", nargs
, arg_idx
);
8299 error ("provided for %q+D", in_decl
);
8301 return error_mark_node
;
8305 return error_mark_node
;
8307 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8309 TREE_VEC_LENGTH (new_inner_args
));
8311 return new_inner_args
;
8314 /* Convert all template arguments to their appropriate types, and
8315 return a vector containing the innermost resulting template
8316 arguments. If any error occurs, return error_mark_node. Error and
8317 warning messages are not issued.
8319 Note that no function argument deduction is performed, and default
8320 arguments are used to fill in unspecified arguments. */
8322 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8324 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8327 /* Convert all template arguments to their appropriate type, and
8328 instantiate default arguments as needed. This returns a vector
8329 containing the innermost resulting template arguments, or
8330 error_mark_node if unsuccessful. */
8332 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8333 tsubst_flags_t complain
)
8335 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8338 /* Like coerce_template_parms. If PARMS represents all template
8339 parameters levels, this function returns a vector of vectors
8340 representing all the resulting argument levels. Note that in this
8341 case, only the innermost arguments are coerced because the
8342 outermost ones are supposed to have been coerced already.
8344 Otherwise, if PARMS represents only (the innermost) vector of
8345 parameters, this function returns a vector containing just the
8346 innermost resulting arguments. */
8349 coerce_innermost_template_parms (tree parms
,
8352 tsubst_flags_t complain
,
8353 bool require_all_args
,
8354 bool use_default_args
)
8356 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8357 int args_depth
= TMPL_ARGS_DEPTH (args
);
8360 if (parms_depth
> 1)
8362 coerced_args
= make_tree_vec (parms_depth
);
8366 for (level
= parms
, cur_depth
= parms_depth
;
8367 parms_depth
> 0 && level
!= NULL_TREE
;
8368 level
= TREE_CHAIN (level
), --cur_depth
)
8371 if (cur_depth
== args_depth
)
8372 l
= coerce_template_parms (TREE_VALUE (level
),
8373 args
, in_decl
, complain
,
8377 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8379 if (l
== error_mark_node
)
8380 return error_mark_node
;
8382 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8386 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8387 args
, in_decl
, complain
,
8390 return coerced_args
;
8393 /* Returns 1 if template args OT and NT are equivalent. */
8396 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8400 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8402 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8405 if (TREE_CODE (nt
) == TREE_VEC
)
8406 /* For member templates */
8407 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8408 else if (PACK_EXPANSION_P (ot
))
8409 return (PACK_EXPANSION_P (nt
)
8410 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8411 PACK_EXPANSION_PATTERN (nt
))
8412 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8413 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8414 else if (ARGUMENT_PACK_P (ot
))
8419 if (!ARGUMENT_PACK_P (nt
))
8422 opack
= ARGUMENT_PACK_ARGS (ot
);
8423 npack
= ARGUMENT_PACK_ARGS (nt
);
8424 len
= TREE_VEC_LENGTH (opack
);
8425 if (TREE_VEC_LENGTH (npack
) != len
)
8427 for (i
= 0; i
< len
; ++i
)
8428 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8429 TREE_VEC_ELT (npack
, i
)))
8433 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8435 else if (TYPE_P (nt
))
8439 /* Don't treat an alias template specialization with dependent
8440 arguments as equivalent to its underlying type when used as a
8441 template argument; we need them to be distinct so that we
8442 substitute into the specialization arguments at instantiation
8443 time. And aliases can't be equivalent without being ==, so
8444 we don't need to look any deeper.
8446 During partial ordering, however, we need to treat them normally so
8447 that we can order uses of the same alias with different
8448 cv-qualification (79960). */
8450 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8453 return same_type_p (ot
, nt
);
8455 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8459 /* Try to treat a template non-type argument that has been converted
8460 to the parameter type as equivalent to one that hasn't yet. */
8461 for (enum tree_code code1
= TREE_CODE (ot
);
8462 CONVERT_EXPR_CODE_P (code1
)
8463 || code1
== NON_LVALUE_EXPR
;
8464 code1
= TREE_CODE (ot
))
8465 ot
= TREE_OPERAND (ot
, 0);
8466 for (enum tree_code code2
= TREE_CODE (nt
);
8467 CONVERT_EXPR_CODE_P (code2
)
8468 || code2
== NON_LVALUE_EXPR
;
8469 code2
= TREE_CODE (nt
))
8470 nt
= TREE_OPERAND (nt
, 0);
8472 return cp_tree_equal (ot
, nt
);
8476 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8477 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8478 NEWARG_PTR with the offending arguments if they are non-NULL. */
8481 comp_template_args (tree oldargs
, tree newargs
,
8482 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8487 if (oldargs
== newargs
)
8490 if (!oldargs
|| !newargs
)
8493 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8496 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8498 tree nt
= TREE_VEC_ELT (newargs
, i
);
8499 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8501 if (! template_args_equal (ot
, nt
, partial_order
))
8503 if (oldarg_ptr
!= NULL
)
8505 if (newarg_ptr
!= NULL
)
8514 comp_template_args_porder (tree oargs
, tree nargs
)
8516 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8520 add_pending_template (tree d
)
8522 tree ti
= (TYPE_P (d
)
8523 ? CLASSTYPE_TEMPLATE_INFO (d
)
8524 : DECL_TEMPLATE_INFO (d
));
8525 struct pending_template
*pt
;
8528 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8531 /* We are called both from instantiate_decl, where we've already had a
8532 tinst_level pushed, and instantiate_template, where we haven't.
8534 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8537 push_tinst_level (d
);
8539 pt
= ggc_alloc
<pending_template
> ();
8541 pt
->tinst
= current_tinst_level
;
8542 if (last_pending_template
)
8543 last_pending_template
->next
= pt
;
8545 pending_templates
= pt
;
8547 last_pending_template
= pt
;
8549 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8556 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8557 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8558 documentation for TEMPLATE_ID_EXPR. */
8561 lookup_template_function (tree fns
, tree arglist
)
8565 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8566 return error_mark_node
;
8568 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8570 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8572 error ("%q#D is not a function template", fns
);
8573 return error_mark_node
;
8576 if (BASELINK_P (fns
))
8578 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8580 BASELINK_FUNCTIONS (fns
),
8585 type
= TREE_TYPE (fns
);
8586 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8587 type
= unknown_type_node
;
8589 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8592 /* Within the scope of a template class S<T>, the name S gets bound
8593 (in build_self_reference) to a TYPE_DECL for the class, not a
8594 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8595 or one of its enclosing classes, and that type is a template,
8596 return the associated TEMPLATE_DECL. Otherwise, the original
8599 Also handle the case when DECL is a TREE_LIST of ambiguous
8600 injected-class-names from different bases. */
8603 maybe_get_template_decl_from_type_decl (tree decl
)
8605 if (decl
== NULL_TREE
)
8608 /* DR 176: A lookup that finds an injected-class-name (10.2
8609 [class.member.lookup]) can result in an ambiguity in certain cases
8610 (for example, if it is found in more than one base class). If all of
8611 the injected-class-names that are found refer to specializations of
8612 the same class template, and if the name is followed by a
8613 template-argument-list, the reference refers to the class template
8614 itself and not a specialization thereof, and is not ambiguous. */
8615 if (TREE_CODE (decl
) == TREE_LIST
)
8617 tree t
, tmpl
= NULL_TREE
;
8618 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8620 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8623 else if (tmpl
!= elt
)
8626 if (tmpl
&& t
== NULL_TREE
)
8632 return (decl
!= NULL_TREE
8633 && DECL_SELF_REFERENCE_P (decl
)
8634 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8635 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8638 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8639 parameters, find the desired type.
8641 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8643 IN_DECL, if non-NULL, is the template declaration we are trying to
8646 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8647 the class we are looking up.
8649 Issue error and warning messages under control of COMPLAIN.
8651 If the template class is really a local class in a template
8652 function, then the FUNCTION_CONTEXT is the function in which it is
8655 ??? Note that this function is currently called *twice* for each
8656 template-id: the first time from the parser, while creating the
8657 incomplete type (finish_template_type), and the second type during the
8658 real instantiation (instantiate_template_class). This is surely something
8659 that we want to avoid. It also causes some problems with argument
8660 coercion (see convert_nontype_argument for more information on this). */
8663 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8664 int entering_scope
, tsubst_flags_t complain
)
8666 tree templ
= NULL_TREE
, parmlist
;
8673 if (identifier_p (d1
))
8675 tree value
= innermost_non_namespace_value (d1
);
8676 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8681 push_decl_namespace (context
);
8682 templ
= lookup_name (d1
);
8683 templ
= maybe_get_template_decl_from_type_decl (templ
);
8685 pop_decl_namespace ();
8688 context
= DECL_CONTEXT (templ
);
8690 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8692 tree type
= TREE_TYPE (d1
);
8694 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8695 an implicit typename for the second A. Deal with it. */
8696 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8697 type
= TREE_TYPE (type
);
8699 if (CLASSTYPE_TEMPLATE_INFO (type
))
8701 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8702 d1
= DECL_NAME (templ
);
8705 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8706 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8708 templ
= TYPE_TI_TEMPLATE (d1
);
8709 d1
= DECL_NAME (templ
);
8711 else if (DECL_TYPE_TEMPLATE_P (d1
))
8714 d1
= DECL_NAME (templ
);
8715 context
= DECL_CONTEXT (templ
);
8717 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8720 d1
= DECL_NAME (templ
);
8723 /* Issue an error message if we didn't find a template. */
8726 if (complain
& tf_error
)
8727 error ("%qT is not a template", d1
);
8728 return error_mark_node
;
8731 if (TREE_CODE (templ
) != TEMPLATE_DECL
8732 /* Make sure it's a user visible template, if it was named by
8734 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8735 && !PRIMARY_TEMPLATE_P (templ
)))
8737 if (complain
& tf_error
)
8739 error ("non-template type %qT used as a template", d1
);
8741 error ("for template declaration %q+D", in_decl
);
8743 return error_mark_node
;
8746 complain
&= ~tf_user
;
8748 /* An alias that just changes the name of a template is equivalent to the
8749 other template, so if any of the arguments are pack expansions, strip
8750 the alias to avoid problems with a pack expansion passed to a non-pack
8751 alias template parameter (DR 1430). */
8752 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8753 templ
= get_underlying_template (templ
);
8755 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8758 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8759 if (arglist2
== error_mark_node
8760 || (!uses_template_parms (arglist2
)
8761 && check_instantiated_args (templ
, arglist2
, complain
)))
8762 return error_mark_node
;
8764 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8769 tree template_type
= TREE_TYPE (templ
);
8772 tree found
= NULL_TREE
;
8775 int is_dependent_type
;
8776 int use_partial_inst_tmpl
= false;
8778 if (template_type
== error_mark_node
)
8779 /* An error occurred while building the template TEMPL, and a
8780 diagnostic has most certainly been emitted for that
8781 already. Let's propagate that error. */
8782 return error_mark_node
;
8784 gen_tmpl
= most_general_template (templ
);
8785 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8786 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8787 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8789 if (arg_depth
== 1 && parm_depth
> 1)
8791 /* We've been given an incomplete set of template arguments.
8794 template <class T> struct S1 {
8795 template <class U> struct S2 {};
8796 template <class U> struct S2<U*> {};
8799 we will be called with an ARGLIST of `U*', but the
8800 TEMPLATE will be `template <class T> template
8801 <class U> struct S1<T>::S2'. We must fill in the missing
8803 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8804 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8805 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8808 /* Now we should have enough arguments. */
8809 gcc_assert (parm_depth
== arg_depth
);
8811 /* From here on, we're only interested in the most general
8814 /* Calculate the BOUND_ARGS. These will be the args that are
8815 actually tsubst'd into the definition to create the
8817 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8819 /*require_all_args=*/true,
8820 /*use_default_args=*/true);
8822 if (arglist
== error_mark_node
)
8823 /* We were unable to bind the arguments. */
8824 return error_mark_node
;
8826 /* In the scope of a template class, explicit references to the
8827 template class refer to the type of the template, not any
8828 instantiation of it. For example, in:
8830 template <class T> class C { void f(C<T>); }
8832 the `C<T>' is just the same as `C'. Outside of the
8833 class, however, such a reference is an instantiation. */
8835 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8836 || currently_open_class (template_type
))
8838 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8840 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8841 return template_type
;
8844 /* If we already have this specialization, return it. */
8845 elt
.tmpl
= gen_tmpl
;
8847 elt
.spec
= NULL_TREE
;
8848 hash
= spec_hasher::hash (&elt
);
8849 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8854 /* If the the template's constraints are not satisfied,
8855 then we cannot form a valid type.
8857 Note that the check is deferred until after the hash
8858 lookup. This prevents redundant checks on previously
8859 instantiated specializations. */
8860 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8862 if (complain
& tf_error
)
8864 error ("template constraint failure");
8865 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8867 return error_mark_node
;
8870 is_dependent_type
= uses_template_parms (arglist
);
8872 /* If the deduced arguments are invalid, then the binding
8874 if (!is_dependent_type
8875 && check_instantiated_args (gen_tmpl
,
8876 INNERMOST_TEMPLATE_ARGS (arglist
),
8878 return error_mark_node
;
8880 if (!is_dependent_type
8881 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8882 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8883 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8885 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8886 DECL_NAME (gen_tmpl
),
8887 /*tag_scope=*/ts_global
);
8891 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8893 if (context
== error_mark_node
)
8894 return error_mark_node
;
8897 context
= global_namespace
;
8899 /* Create the type. */
8900 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8902 /* The user referred to a specialization of an alias
8903 template represented by GEN_TMPL.
8905 [temp.alias]/2 says:
8907 When a template-id refers to the specialization of an
8908 alias template, it is equivalent to the associated
8909 type obtained by substitution of its
8910 template-arguments for the template-parameters in the
8911 type-id of the alias template. */
8913 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8914 /* Note that the call above (by indirectly calling
8915 register_specialization in tsubst_decl) registers the
8916 TYPE_DECL representing the specialization of the alias
8917 template. So next time someone substitutes ARGLIST for
8918 the template parms into the alias template (GEN_TMPL),
8919 she'll get that TYPE_DECL back. */
8921 if (t
== error_mark_node
)
8924 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8926 if (!is_dependent_type
)
8928 set_current_access_from_decl (TYPE_NAME (template_type
));
8929 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8930 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8931 arglist
, complain
, in_decl
),
8932 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8933 arglist
, complain
, in_decl
),
8934 SCOPED_ENUM_P (template_type
), NULL
);
8936 if (t
== error_mark_node
)
8941 /* We don't want to call start_enum for this type, since
8942 the values for the enumeration constants may involve
8943 template parameters. And, no one should be interested
8944 in the enumeration constants for such a type. */
8945 t
= cxx_make_type (ENUMERAL_TYPE
);
8946 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8948 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8949 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8950 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8952 else if (CLASS_TYPE_P (template_type
))
8954 t
= make_class_type (TREE_CODE (template_type
));
8955 CLASSTYPE_DECLARED_CLASS (t
)
8956 = CLASSTYPE_DECLARED_CLASS (template_type
);
8957 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8959 /* A local class. Make sure the decl gets registered properly. */
8960 if (context
== current_function_decl
)
8961 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8963 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8964 /* This instantiation is another name for the primary
8965 template type. Set the TYPE_CANONICAL field
8967 TYPE_CANONICAL (t
) = template_type
;
8968 else if (any_template_arguments_need_structural_equality_p (arglist
))
8969 /* Some of the template arguments require structural
8970 equality testing, so this template class requires
8971 structural equality testing. */
8972 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8977 /* If we called start_enum or pushtag above, this information
8978 will already be set up. */
8981 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8983 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8984 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8985 DECL_SOURCE_LOCATION (type_decl
)
8986 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8989 type_decl
= TYPE_NAME (t
);
8991 if (CLASS_TYPE_P (template_type
))
8993 TREE_PRIVATE (type_decl
)
8994 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8995 TREE_PROTECTED (type_decl
)
8996 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8997 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8999 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9000 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9004 if (OVERLOAD_TYPE_P (t
)
9005 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9007 static const char *tags
[] = {"abi_tag", "may_alias"};
9009 for (unsigned ix
= 0; ix
!= 2; ix
++)
9012 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9016 = tree_cons (TREE_PURPOSE (attributes
),
9017 TREE_VALUE (attributes
),
9018 TYPE_ATTRIBUTES (t
));
9022 /* Let's consider the explicit specialization of a member
9023 of a class template specialization that is implicitly instantiated,
9028 template<class U> struct M {}; //#0
9033 struct S<int>::M<char> //#1
9037 [temp.expl.spec]/4 says this is valid.
9039 In this case, when we write:
9042 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9045 When we encounter #1, we want to store the partial instantiation
9046 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9048 For all cases other than this "explicit specialization of member of a
9049 class template", we just want to store the most general template into
9050 the CLASSTYPE_TI_TEMPLATE of M.
9052 This case of "explicit specialization of member of a class template"
9054 1/ the enclosing class is an instantiation of, and therefore not
9055 the same as, the context of the most general template, and
9056 2/ we aren't looking at the partial instantiation itself, i.e.
9057 the innermost arguments are not the same as the innermost parms of
9058 the most general template.
9060 So it's only when 1/ and 2/ happens that we want to use the partial
9061 instantiation of the member template in lieu of its most general
9064 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9065 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9066 /* the enclosing class must be an instantiation... */
9067 && CLASS_TYPE_P (context
)
9068 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9070 TREE_VEC_LENGTH (arglist
)--;
9071 ++processing_template_decl
;
9072 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9073 tree partial_inst_args
=
9074 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9075 arglist
, complain
, NULL_TREE
);
9076 --processing_template_decl
;
9077 TREE_VEC_LENGTH (arglist
)++;
9078 if (partial_inst_args
== error_mark_node
)
9079 return error_mark_node
;
9080 use_partial_inst_tmpl
=
9081 /*...and we must not be looking at the partial instantiation
9083 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9087 if (!use_partial_inst_tmpl
)
9088 /* This case is easy; there are no member templates involved. */
9092 /* This is a full instantiation of a member template. Find
9093 the partial instantiation of which this is an instance. */
9095 /* Temporarily reduce by one the number of levels in the ARGLIST
9096 so as to avoid comparing the last set of arguments. */
9097 TREE_VEC_LENGTH (arglist
)--;
9098 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9099 TREE_VEC_LENGTH (arglist
)++;
9100 /* FOUND is either a proper class type, or an alias
9101 template specialization. In the later case, it's a
9102 TYPE_DECL, resulting from the substituting of arguments
9103 for parameters in the TYPE_DECL of the alias template
9104 done earlier. So be careful while getting the template
9106 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9108 : (TREE_CODE (found
) == TYPE_DECL
9109 ? DECL_TI_TEMPLATE (found
)
9110 : CLASSTYPE_TI_TEMPLATE (found
)));
9113 // Build template info for the new specialization.
9114 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9117 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9118 entry
= ggc_alloc
<spec_entry
> ();
9122 /* Note this use of the partial instantiation so we can check it
9123 later in maybe_process_partial_specialization. */
9124 DECL_TEMPLATE_INSTANTIATIONS (found
)
9125 = tree_cons (arglist
, t
,
9126 DECL_TEMPLATE_INSTANTIATIONS (found
));
9128 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9129 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9130 /* Now that the type has been registered on the instantiations
9131 list, we set up the enumerators. Because the enumeration
9132 constants may involve the enumeration type itself, we make
9133 sure to register the type first, and then create the
9134 constants. That way, doing tsubst_expr for the enumeration
9135 constants won't result in recursive calls here; we'll find
9136 the instantiation and exit above. */
9137 tsubst_enum (template_type
, t
, arglist
);
9139 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9140 /* If the type makes use of template parameters, the
9141 code that generates debugging information will crash. */
9142 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9144 /* Possibly limit visibility based on template args. */
9145 TREE_PUBLIC (type_decl
) = 1;
9146 determine_visibility (type_decl
);
9148 inherit_targ_abi_tags (t
);
9154 /* Wrapper for lookup_template_class_1. */
9157 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9158 int entering_scope
, tsubst_flags_t complain
)
9161 timevar_push (TV_TEMPLATE_INST
);
9162 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9163 entering_scope
, complain
);
9164 timevar_pop (TV_TEMPLATE_INST
);
9168 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9171 lookup_template_variable (tree templ
, tree arglist
)
9173 /* The type of the expression is NULL_TREE since the template-id could refer
9174 to an explicit or partial specialization. */
9175 tree type
= NULL_TREE
;
9176 if (flag_concepts
&& variable_concept_p (templ
))
9177 /* Except that concepts are always bool. */
9178 type
= boolean_type_node
;
9179 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9182 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9185 finish_template_variable (tree var
, tsubst_flags_t complain
)
9187 tree templ
= TREE_OPERAND (var
, 0);
9188 tree arglist
= TREE_OPERAND (var
, 1);
9190 /* We never want to return a VAR_DECL for a variable concept, since they
9191 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9192 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9193 if (concept_p
&& processing_template_decl
)
9196 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9197 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9199 templ
= most_general_template (templ
);
9200 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9201 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9203 /*use_default*/true);
9205 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9207 if (complain
& tf_error
)
9209 error ("use of invalid variable template %qE", var
);
9210 diagnose_constraints (location_of (var
), templ
, arglist
);
9212 return error_mark_node
;
9215 /* If a template-id refers to a specialization of a variable
9216 concept, then the expression is true if and only if the
9217 concept's constraints are satisfied by the given template
9220 NOTE: This is an extension of Concepts Lite TS that
9221 allows constraints to be used in expressions. */
9224 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9225 return evaluate_variable_concept (decl
, arglist
);
9228 return instantiate_template (templ
, arglist
, complain
);
9231 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9232 TARGS template args, and instantiate it if it's not dependent. */
9235 lookup_and_finish_template_variable (tree templ
, tree targs
,
9236 tsubst_flags_t complain
)
9238 templ
= lookup_template_variable (templ
, targs
);
9239 if (!any_dependent_template_arguments_p (targs
))
9241 templ
= finish_template_variable (templ
, complain
);
9245 return convert_from_reference (templ
);
9254 /* True when we should also visit template parameters that occur in
9255 non-deduced contexts. */
9256 bool include_nondeduced_p
;
9257 hash_set
<tree
> *visited
;
9260 /* Called from for_each_template_parm via walk_tree. */
9263 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9266 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9267 tree_fn_t fn
= pfd
->fn
;
9268 void *data
= pfd
->data
;
9269 tree result
= NULL_TREE
;
9271 #define WALK_SUBTREE(NODE) \
9274 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9275 pfd->include_nondeduced_p, \
9277 if (result) goto out; \
9281 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9285 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9286 WALK_SUBTREE (TYPE_CONTEXT (t
));
9288 switch (TREE_CODE (t
))
9291 if (TYPE_PTRMEMFUNC_P (t
))
9297 if (!TYPE_TEMPLATE_INFO (t
))
9300 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9304 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9305 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9309 /* Since we're not going to walk subtrees, we have to do this
9311 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9315 /* Check the return type. */
9316 WALK_SUBTREE (TREE_TYPE (t
));
9318 /* Check the parameter types. Since default arguments are not
9319 instantiated until they are needed, the TYPE_ARG_TYPES may
9320 contain expressions that involve template parameters. But,
9321 no-one should be looking at them yet. And, once they're
9322 instantiated, they don't contain template parameters, so
9323 there's no point in looking at them then, either. */
9327 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9328 WALK_SUBTREE (TREE_VALUE (parm
));
9330 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9331 want walk_tree walking into them itself. */
9335 if (flag_noexcept_type
)
9337 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9339 WALK_SUBTREE (TREE_PURPOSE (spec
));
9344 case UNDERLYING_TYPE
:
9345 if (pfd
->include_nondeduced_p
9346 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9348 pfd
->include_nondeduced_p
,
9350 return error_mark_node
;
9355 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9356 WALK_SUBTREE (DECL_TI_ARGS (t
));
9361 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9362 WALK_SUBTREE (DECL_INITIAL (t
));
9363 if (DECL_CONTEXT (t
)
9364 && pfd
->include_nondeduced_p
)
9365 WALK_SUBTREE (DECL_CONTEXT (t
));
9368 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9369 /* Record template parameters such as `T' inside `TT<T>'. */
9370 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9373 case TEMPLATE_TEMPLATE_PARM
:
9374 case TEMPLATE_TYPE_PARM
:
9375 case TEMPLATE_PARM_INDEX
:
9376 if (fn
&& (*fn
)(t
, data
))
9383 /* A template template parameter is encountered. */
9384 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9385 WALK_SUBTREE (TREE_TYPE (t
));
9387 /* Already substituted template template parameter */
9392 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9393 partial instantiation. */
9394 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9398 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9399 && pfd
->include_nondeduced_p
)
9400 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9405 /* If there's no type, then this thing must be some expression
9406 involving template parameters. */
9407 if (!fn
&& !TREE_TYPE (t
))
9408 return error_mark_node
;
9413 case IMPLICIT_CONV_EXPR
:
9414 case REINTERPRET_CAST_EXPR
:
9415 case CONST_CAST_EXPR
:
9416 case STATIC_CAST_EXPR
:
9417 case DYNAMIC_CAST_EXPR
:
9421 case PSEUDO_DTOR_EXPR
:
9423 return error_mark_node
;
9432 /* We didn't find any template parameters we liked. */
9437 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9438 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9439 call FN with the parameter and the DATA.
9440 If FN returns nonzero, the iteration is terminated, and
9441 for_each_template_parm returns 1. Otherwise, the iteration
9442 continues. If FN never returns a nonzero value, the value
9443 returned by for_each_template_parm is 0. If FN is NULL, it is
9444 considered to be the function which always returns 1.
9446 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9447 parameters that occur in non-deduced contexts. When false, only
9448 visits those template parameters that can be deduced. */
9451 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9452 hash_set
<tree
> *visited
,
9453 bool include_nondeduced_p
,
9456 struct pair_fn_data pfd
;
9461 pfd
.any_fn
= any_fn
;
9463 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9465 /* Walk the tree. (Conceptually, we would like to walk without
9466 duplicates, but for_each_template_parm_r recursively calls
9467 for_each_template_parm, so we would need to reorganize a fair
9468 bit to use walk_tree_without_duplicates, so we keep our own
9471 pfd
.visited
= visited
;
9473 pfd
.visited
= new hash_set
<tree
>;
9474 result
= cp_walk_tree (&t
,
9475 for_each_template_parm_r
,
9489 /* Returns true if T depends on any template parameter. */
9492 uses_template_parms (tree t
)
9498 int saved_processing_template_decl
;
9500 saved_processing_template_decl
= processing_template_decl
;
9501 if (!saved_processing_template_decl
)
9502 processing_template_decl
= 1;
9504 dependent_p
= dependent_type_p (t
);
9505 else if (TREE_CODE (t
) == TREE_VEC
)
9506 dependent_p
= any_dependent_template_arguments_p (t
);
9507 else if (TREE_CODE (t
) == TREE_LIST
)
9508 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9509 || uses_template_parms (TREE_CHAIN (t
)));
9510 else if (TREE_CODE (t
) == TYPE_DECL
)
9511 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9514 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9515 || TREE_CODE (t
) == OVERLOAD
9518 || TREE_CODE (t
) == TRAIT_EXPR
9519 || TREE_CODE (t
) == CONSTRUCTOR
9520 || CONSTANT_CLASS_P (t
))
9521 dependent_p
= (type_dependent_expression_p (t
)
9522 || value_dependent_expression_p (t
));
9525 gcc_assert (t
== error_mark_node
);
9526 dependent_p
= false;
9529 processing_template_decl
= saved_processing_template_decl
;
9534 /* Returns true iff current_function_decl is an incompletely instantiated
9535 template. Useful instead of processing_template_decl because the latter
9536 is set to 0 during instantiate_non_dependent_expr. */
9539 in_template_function (void)
9541 tree fn
= current_function_decl
;
9543 ++processing_template_decl
;
9544 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9545 && DECL_TEMPLATE_INFO (fn
)
9546 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9547 --processing_template_decl
;
9551 /* Returns true if T depends on any template parameter with level LEVEL. */
9554 uses_template_parms_level (tree t
, int level
)
9556 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9557 /*include_nondeduced_p=*/true);
9560 /* Returns true if the signature of DECL depends on any template parameter from
9561 its enclosing class. */
9564 uses_outer_template_parms (tree decl
)
9566 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9569 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9570 &depth
, NULL
, /*include_nondeduced_p=*/true))
9572 if (PRIMARY_TEMPLATE_P (decl
)
9573 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9574 (DECL_TEMPLATE_PARMS (decl
)),
9575 template_parm_outer_level
,
9576 &depth
, NULL
, /*include_nondeduced_p=*/true))
9578 tree ci
= get_constraints (decl
);
9580 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9581 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9582 &depth
, NULL
, /*nondeduced*/true))
9587 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9588 ill-formed translation unit, i.e. a variable or function that isn't
9589 usable in a constant expression. */
9592 neglectable_inst_p (tree d
)
9595 && !undeduced_auto_decl (d
)
9596 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9597 : decl_maybe_constant_var_p (d
)));
9600 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9601 neglectable and instantiated from within an erroneous instantiation. */
9604 limit_bad_template_recursion (tree decl
)
9606 struct tinst_level
*lev
= current_tinst_level
;
9607 int errs
= errorcount
+ sorrycount
;
9608 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9611 for (; lev
; lev
= lev
->next
)
9612 if (neglectable_inst_p (lev
->decl
))
9615 return (lev
&& errs
> lev
->errors
);
9618 static int tinst_depth
;
9619 extern int max_tinst_depth
;
9622 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9624 /* We're starting to instantiate D; record the template instantiation context
9625 for diagnostics and to restore it later. */
9628 push_tinst_level (tree d
)
9630 return push_tinst_level_loc (d
, input_location
);
9633 /* We're starting to instantiate D; record the template instantiation context
9634 at LOC for diagnostics and to restore it later. */
9637 push_tinst_level_loc (tree d
, location_t loc
)
9639 struct tinst_level
*new_level
;
9641 if (tinst_depth
>= max_tinst_depth
)
9643 /* Tell error.c not to try to instantiate any templates. */
9645 fatal_error (input_location
,
9646 "template instantiation depth exceeds maximum of %d"
9647 " (use -ftemplate-depth= to increase the maximum)",
9652 /* If the current instantiation caused problems, don't let it instantiate
9653 anything else. Do allow deduction substitution and decls usable in
9654 constant expressions. */
9655 if (limit_bad_template_recursion (d
))
9658 /* When not -quiet, dump template instantiations other than functions, since
9659 announce_function will take care of those. */
9661 && TREE_CODE (d
) != TREE_LIST
9662 && TREE_CODE (d
) != FUNCTION_DECL
)
9663 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9665 new_level
= ggc_alloc
<tinst_level
> ();
9666 new_level
->decl
= d
;
9667 new_level
->locus
= loc
;
9668 new_level
->errors
= errorcount
+sorrycount
;
9669 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9670 new_level
->next
= current_tinst_level
;
9671 current_tinst_level
= new_level
;
9674 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9675 depth_reached
= tinst_depth
;
9680 /* We're done instantiating this template; return to the instantiation
9684 pop_tinst_level (void)
9686 /* Restore the filename and line number stashed away when we started
9687 this instantiation. */
9688 input_location
= current_tinst_level
->locus
;
9689 current_tinst_level
= current_tinst_level
->next
;
9693 /* We're instantiating a deferred template; restore the template
9694 instantiation context in which the instantiation was requested, which
9695 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9698 reopen_tinst_level (struct tinst_level
*level
)
9700 struct tinst_level
*t
;
9703 for (t
= level
; t
; t
= t
->next
)
9706 current_tinst_level
= level
;
9708 if (current_tinst_level
)
9709 current_tinst_level
->errors
= errorcount
+sorrycount
;
9713 /* Returns the TINST_LEVEL which gives the original instantiation
9716 struct tinst_level
*
9717 outermost_tinst_level (void)
9719 struct tinst_level
*level
= current_tinst_level
;
9722 level
= level
->next
;
9726 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9727 vector of template arguments, as for tsubst.
9729 Returns an appropriate tsubst'd friend declaration. */
9732 tsubst_friend_function (tree decl
, tree args
)
9736 if (TREE_CODE (decl
) == FUNCTION_DECL
9737 && DECL_TEMPLATE_INSTANTIATION (decl
)
9738 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9739 /* This was a friend declared with an explicit template
9740 argument list, e.g.:
9744 to indicate that f was a template instantiation, not a new
9745 function declaration. Now, we have to figure out what
9746 instantiation of what template. */
9748 tree template_id
, arglist
, fns
;
9751 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9753 /* Friend functions are looked up in the containing namespace scope.
9754 We must enter that scope, to avoid finding member functions of the
9755 current class with same name. */
9756 push_nested_namespace (ns
);
9757 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9758 tf_warning_or_error
, NULL_TREE
,
9759 /*integral_constant_expression_p=*/false);
9760 pop_nested_namespace (ns
);
9761 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9762 tf_warning_or_error
, NULL_TREE
);
9763 template_id
= lookup_template_function (fns
, arglist
);
9765 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9766 tmpl
= determine_specialization (template_id
, new_friend
,
9768 /*need_member_template=*/0,
9769 TREE_VEC_LENGTH (args
),
9771 return instantiate_template (tmpl
, new_args
, tf_error
);
9774 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9776 /* The NEW_FRIEND will look like an instantiation, to the
9777 compiler, but is not an instantiation from the point of view of
9778 the language. For example, we might have had:
9780 template <class T> struct S {
9781 template <class U> friend void f(T, U);
9784 Then, in S<int>, template <class U> void f(int, U) is not an
9785 instantiation of anything. */
9786 if (new_friend
== error_mark_node
)
9787 return error_mark_node
;
9789 DECL_USE_TEMPLATE (new_friend
) = 0;
9790 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9792 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9793 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9794 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9797 /* The mangled name for the NEW_FRIEND is incorrect. The function
9798 is not a template instantiation and should not be mangled like
9799 one. Therefore, we forget the mangling here; we'll recompute it
9800 later if we need it. */
9801 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9803 SET_DECL_RTL (new_friend
, NULL
);
9804 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9807 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9810 tree new_friend_template_info
;
9811 tree new_friend_result_template_info
;
9813 int new_friend_is_defn
;
9815 /* We must save some information from NEW_FRIEND before calling
9816 duplicate decls since that function will free NEW_FRIEND if
9818 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9819 new_friend_is_defn
=
9820 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9821 (template_for_substitution (new_friend
)))
9823 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9825 /* This declaration is a `primary' template. */
9826 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9828 new_friend_result_template_info
9829 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9832 new_friend_result_template_info
= NULL_TREE
;
9834 /* Inside pushdecl_namespace_level, we will push into the
9835 current namespace. However, the friend function should go
9836 into the namespace of the template. */
9837 ns
= decl_namespace_context (new_friend
);
9838 push_nested_namespace (ns
);
9839 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9840 pop_nested_namespace (ns
);
9842 if (old_decl
== error_mark_node
)
9843 return error_mark_node
;
9845 if (old_decl
!= new_friend
)
9847 /* This new friend declaration matched an existing
9848 declaration. For example, given:
9850 template <class T> void f(T);
9851 template <class U> class C {
9852 template <class T> friend void f(T) {}
9855 the friend declaration actually provides the definition
9856 of `f', once C has been instantiated for some type. So,
9857 old_decl will be the out-of-class template declaration,
9858 while new_friend is the in-class definition.
9860 But, if `f' was called before this point, the
9861 instantiation of `f' will have DECL_TI_ARGS corresponding
9862 to `T' but not to `U', references to which might appear
9863 in the definition of `f'. Previously, the most general
9864 template for an instantiation of `f' was the out-of-class
9865 version; now it is the in-class version. Therefore, we
9866 run through all specialization of `f', adding to their
9867 DECL_TI_ARGS appropriately. In particular, they need a
9868 new set of outer arguments, corresponding to the
9869 arguments for this class instantiation.
9871 The same situation can arise with something like this:
9874 template <class T> class C {
9878 when `C<int>' is instantiated. Now, `f(int)' is defined
9881 if (!new_friend_is_defn
)
9882 /* On the other hand, if the in-class declaration does
9883 *not* provide a definition, then we don't want to alter
9884 existing definitions. We can just leave everything
9889 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9890 tree new_args
= TI_ARGS (new_friend_template_info
);
9892 /* Overwrite whatever template info was there before, if
9893 any, with the new template information pertaining to
9895 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9897 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9899 /* We should have called reregister_specialization in
9901 gcc_assert (retrieve_specialization (new_template
,
9905 /* Instantiate it if the global has already been used. */
9906 if (DECL_ODR_USED (old_decl
))
9907 instantiate_decl (old_decl
, /*defer_ok=*/true,
9908 /*expl_inst_class_mem_p=*/false);
9914 /* Indicate that the old function template is a partial
9916 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9917 = new_friend_result_template_info
;
9919 gcc_assert (new_template
9920 == most_general_template (new_template
));
9921 gcc_assert (new_template
!= old_decl
);
9923 /* Reassign any specializations already in the hash table
9924 to the new more general template, and add the
9925 additional template args. */
9926 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9930 tree spec
= TREE_VALUE (t
);
9933 elt
.tmpl
= old_decl
;
9934 elt
.args
= DECL_TI_ARGS (spec
);
9935 elt
.spec
= NULL_TREE
;
9937 decl_specializations
->remove_elt (&elt
);
9940 = add_outermost_template_args (new_args
,
9941 DECL_TI_ARGS (spec
));
9943 register_specialization
9944 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9947 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9951 /* The information from NEW_FRIEND has been merged into OLD_DECL
9952 by duplicate_decls. */
9953 new_friend
= old_decl
;
9958 tree context
= DECL_CONTEXT (new_friend
);
9962 template <class T> class C {
9963 template <class U> friend void C1<U>::f (); // case 1
9964 friend void C2<T>::f (); // case 2
9966 we only need to make sure CONTEXT is a complete type for
9967 case 2. To distinguish between the two cases, we note that
9968 CONTEXT of case 1 remains dependent type after tsubst while
9969 this isn't true for case 2. */
9970 ++processing_template_decl
;
9971 dependent_p
= dependent_type_p (context
);
9972 --processing_template_decl
;
9975 && !complete_type_or_else (context
, NULL_TREE
))
9976 return error_mark_node
;
9978 if (COMPLETE_TYPE_P (context
))
9980 tree fn
= new_friend
;
9981 /* do_friend adds the TEMPLATE_DECL for any member friend
9982 template even if it isn't a member template, i.e.
9983 template <class T> friend A<T>::f();
9984 Look through it in that case. */
9985 if (TREE_CODE (fn
) == TEMPLATE_DECL
9986 && !PRIMARY_TEMPLATE_P (fn
))
9987 fn
= DECL_TEMPLATE_RESULT (fn
);
9988 /* Check to see that the declaration is really present, and,
9989 possibly obtain an improved declaration. */
9990 fn
= check_classfn (context
, fn
, NULL_TREE
);
10000 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10001 template arguments, as for tsubst.
10003 Returns an appropriate tsubst'd friend type or error_mark_node on
10007 tsubst_friend_class (tree friend_tmpl
, tree args
)
10011 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10013 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10014 return TREE_TYPE (tmpl
);
10017 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10018 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10019 push_nested_namespace (context
);
10021 push_nested_class (context
);
10023 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10024 /*non_class=*/false, /*block_p=*/false,
10025 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10027 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10029 /* The friend template has already been declared. Just
10030 check to see that the declarations match, and install any new
10031 default parameters. We must tsubst the default parameters,
10032 of course. We only need the innermost template parameters
10033 because that is all that redeclare_class_template will look
10035 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10036 > TMPL_ARGS_DEPTH (args
))
10038 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10039 args
, tf_warning_or_error
);
10040 location_t saved_input_location
= input_location
;
10041 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10042 tree cons
= get_constraints (tmpl
);
10043 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10044 input_location
= saved_input_location
;
10049 /* The friend template has not already been declared. In this
10050 case, the instantiation of the template class will cause the
10051 injection of this template into the namespace scope. */
10052 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10054 if (tmpl
!= error_mark_node
)
10056 /* The new TMPL is not an instantiation of anything, so we
10057 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10058 for the new type because that is supposed to be the
10059 corresponding template decl, i.e., TMPL. */
10060 DECL_USE_TEMPLATE (tmpl
) = 0;
10061 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10062 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10063 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10064 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10066 /* It is hidden. */
10067 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10068 DECL_ANTICIPATED (tmpl
)
10069 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10071 /* Inject this template into the enclosing namspace scope. */
10072 tmpl
= pushdecl_namespace_level (tmpl
, true);
10076 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10077 pop_nested_namespace (context
);
10079 pop_nested_class ();
10081 return TREE_TYPE (tmpl
);
10084 /* Returns zero if TYPE cannot be completed later due to circularity.
10085 Otherwise returns one. */
10088 can_complete_type_without_circularity (tree type
)
10090 if (type
== NULL_TREE
|| type
== error_mark_node
)
10092 else if (COMPLETE_TYPE_P (type
))
10094 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10095 return can_complete_type_without_circularity (TREE_TYPE (type
));
10096 else if (CLASS_TYPE_P (type
)
10097 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10103 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10104 tsubst_flags_t
, tree
);
10106 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10107 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10110 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10111 tsubst_flags_t complain
, tree in_decl
)
10113 gcc_assert (ATTR_IS_DEPENDENT (t
));
10115 tree val
= TREE_VALUE (t
);
10116 if (val
== NULL_TREE
)
10117 /* Nothing to do. */;
10118 else if ((flag_openmp
|| flag_openmp_simd
)
10119 && is_attribute_p ("omp declare simd",
10120 get_attribute_name (t
)))
10122 tree clauses
= TREE_VALUE (val
);
10123 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10124 complain
, in_decl
);
10125 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10126 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10127 tree parms
= DECL_ARGUMENTS (*decl_p
);
10129 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10131 val
= build_tree_list (NULL_TREE
, clauses
);
10135 /* If the first attribute argument is an identifier, don't
10136 pass it through tsubst. Attributes like mode, format,
10137 cleanup and several target specific attributes expect it
10139 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10142 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10143 /*integral_constant_expression_p=*/false);
10144 if (chain
!= TREE_CHAIN (val
))
10145 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10147 else if (PACK_EXPANSION_P (val
))
10149 /* An attribute pack expansion. */
10150 tree purp
= TREE_PURPOSE (t
);
10151 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10152 if (pack
== error_mark_node
)
10153 return error_mark_node
;
10154 int len
= TREE_VEC_LENGTH (pack
);
10155 tree list
= NULL_TREE
;
10157 for (int i
= 0; i
< len
; ++i
)
10159 tree elt
= TREE_VEC_ELT (pack
, i
);
10160 *q
= build_tree_list (purp
, elt
);
10161 q
= &TREE_CHAIN (*q
);
10166 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10167 /*integral_constant_expression_p=*/false);
10169 if (val
!= TREE_VALUE (t
))
10170 return build_tree_list (TREE_PURPOSE (t
), val
);
10174 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10175 unchanged or a new TREE_LIST chain. */
10178 tsubst_attributes (tree attributes
, tree args
,
10179 tsubst_flags_t complain
, tree in_decl
)
10181 tree last_dep
= NULL_TREE
;
10183 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10184 if (ATTR_IS_DEPENDENT (t
))
10187 attributes
= copy_list (attributes
);
10192 for (tree
*p
= &attributes
; *p
; )
10195 if (ATTR_IS_DEPENDENT (t
))
10197 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10202 p
= &TREE_CHAIN (*p
);
10204 *p
= TREE_CHAIN (t
);
10208 p
= &TREE_CHAIN (*p
);
10214 /* Apply any attributes which had to be deferred until instantiation
10215 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10216 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10219 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10220 tree args
, tsubst_flags_t complain
, tree in_decl
)
10222 tree last_dep
= NULL_TREE
;
10226 if (attributes
== NULL_TREE
)
10229 if (DECL_P (*decl_p
))
10231 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10233 p
= &DECL_ATTRIBUTES (*decl_p
);
10234 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10235 to our attributes parameter. */
10236 gcc_assert (*p
== attributes
);
10240 p
= &TYPE_ATTRIBUTES (*decl_p
);
10241 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10242 lookup_template_class_1, and should be preserved. */
10243 gcc_assert (*p
!= attributes
);
10245 p
= &TREE_CHAIN (*p
);
10248 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10249 if (ATTR_IS_DEPENDENT (t
))
10252 attributes
= copy_list (attributes
);
10259 tree late_attrs
= NULL_TREE
;
10260 tree
*q
= &late_attrs
;
10265 if (ATTR_IS_DEPENDENT (t
))
10267 *p
= TREE_CHAIN (t
);
10268 TREE_CHAIN (t
) = NULL_TREE
;
10269 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10271 q
= &TREE_CHAIN (*q
);
10275 p
= &TREE_CHAIN (t
);
10278 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10282 /* Perform (or defer) access check for typedefs that were referenced
10283 from within the template TMPL code.
10284 This is a subroutine of instantiate_decl and instantiate_class_template.
10285 TMPL is the template to consider and TARGS is the list of arguments of
10289 perform_typedefs_access_check (tree tmpl
, tree targs
)
10291 location_t saved_location
;
10293 qualified_typedef_usage_t
*iter
;
10296 || (!CLASS_TYPE_P (tmpl
)
10297 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10300 saved_location
= input_location
;
10301 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10303 tree type_decl
= iter
->typedef_decl
;
10304 tree type_scope
= iter
->context
;
10306 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10309 if (uses_template_parms (type_decl
))
10310 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10311 if (uses_template_parms (type_scope
))
10312 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10314 /* Make access check error messages point to the location
10315 of the use of the typedef. */
10316 input_location
= iter
->locus
;
10317 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10318 type_decl
, type_decl
,
10319 tf_warning_or_error
);
10321 input_location
= saved_location
;
10325 instantiate_class_template_1 (tree type
)
10327 tree templ
, args
, pattern
, t
, member
;
10331 unsigned int saved_maximum_field_alignment
;
10334 if (type
== error_mark_node
)
10335 return error_mark_node
;
10337 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10338 || uses_template_parms (type
))
10341 /* Figure out which template is being instantiated. */
10342 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10343 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10345 /* Determine what specialization of the original template to
10347 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10348 if (t
== error_mark_node
)
10350 TYPE_BEING_DEFINED (type
) = 1;
10351 return error_mark_node
;
10355 /* This TYPE is actually an instantiation of a partial
10356 specialization. We replace the innermost set of ARGS with
10357 the arguments appropriate for substitution. For example,
10360 template <class T> struct S {};
10361 template <class T> struct S<T*> {};
10363 and supposing that we are instantiating S<int*>, ARGS will
10364 presently be {int*} -- but we need {int}. */
10365 pattern
= TREE_TYPE (t
);
10366 args
= TREE_PURPOSE (t
);
10370 pattern
= TREE_TYPE (templ
);
10371 args
= CLASSTYPE_TI_ARGS (type
);
10374 /* If the template we're instantiating is incomplete, then clearly
10375 there's nothing we can do. */
10376 if (!COMPLETE_TYPE_P (pattern
))
10379 /* If we've recursively instantiated too many templates, stop. */
10380 if (! push_tinst_level (type
))
10383 /* Now we're really doing the instantiation. Mark the type as in
10384 the process of being defined. */
10385 TYPE_BEING_DEFINED (type
) = 1;
10387 /* We may be in the middle of deferred access check. Disable
10389 push_deferring_access_checks (dk_no_deferred
);
10391 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10392 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10394 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10395 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10396 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10397 fn_context
= error_mark_node
;
10399 push_to_top_level ();
10402 cp_unevaluated_operand
= 0;
10403 c_inhibit_evaluation_warnings
= 0;
10405 /* Use #pragma pack from the template context. */
10406 saved_maximum_field_alignment
= maximum_field_alignment
;
10407 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10409 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10411 /* Set the input location to the most specialized template definition.
10412 This is needed if tsubsting causes an error. */
10413 typedecl
= TYPE_MAIN_DECL (pattern
);
10414 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10415 DECL_SOURCE_LOCATION (typedecl
);
10417 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10418 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10419 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10420 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10421 if (ANON_AGGR_TYPE_P (pattern
))
10422 SET_ANON_AGGR_TYPE_P (type
);
10423 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10425 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10426 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10427 /* Adjust visibility for template arguments. */
10428 determine_visibility (TYPE_MAIN_DECL (type
));
10430 if (CLASS_TYPE_P (type
))
10431 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10433 pbinfo
= TYPE_BINFO (pattern
);
10435 /* We should never instantiate a nested class before its enclosing
10436 class; we need to look up the nested class by name before we can
10437 instantiate it, and that lookup should instantiate the enclosing
10439 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10440 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10442 base_list
= NULL_TREE
;
10443 if (BINFO_N_BASE_BINFOS (pbinfo
))
10449 /* We must enter the scope containing the type, as that is where
10450 the accessibility of types named in dependent bases are
10452 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10454 /* Substitute into each of the bases to determine the actual
10456 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10459 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10460 tree expanded_bases
= NULL_TREE
;
10463 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10466 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10467 args
, tf_error
, NULL_TREE
);
10468 if (expanded_bases
== error_mark_node
)
10471 len
= TREE_VEC_LENGTH (expanded_bases
);
10474 for (idx
= 0; idx
< len
; idx
++)
10476 if (expanded_bases
)
10477 /* Extract the already-expanded base class. */
10478 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10480 /* Substitute to figure out the base class. */
10481 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10484 if (base
== error_mark_node
)
10487 base_list
= tree_cons (access
, base
, base_list
);
10488 if (BINFO_VIRTUAL_P (pbase_binfo
))
10489 TREE_TYPE (base_list
) = integer_type_node
;
10493 /* The list is now in reverse order; correct that. */
10494 base_list
= nreverse (base_list
);
10497 pop_scope (pushed_scope
);
10499 /* Now call xref_basetypes to set up all the base-class
10501 xref_basetypes (type
, base_list
);
10503 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10504 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10505 args
, tf_error
, NULL_TREE
);
10506 fixup_attribute_variants (type
);
10508 /* Now that our base classes are set up, enter the scope of the
10509 class, so that name lookups into base classes, etc. will work
10510 correctly. This is precisely analogous to what we do in
10511 begin_class_definition when defining an ordinary non-template
10512 class, except we also need to push the enclosing classes. */
10513 push_nested_class (type
);
10515 /* Now members are processed in the order of declaration. */
10516 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10517 member
; member
= TREE_CHAIN (member
))
10519 tree t
= TREE_VALUE (member
);
10521 if (TREE_PURPOSE (member
))
10525 /* Build new CLASSTYPE_NESTED_UTDS. */
10528 bool class_template_p
;
10530 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10531 && TYPE_LANG_SPECIFIC (t
)
10532 && CLASSTYPE_IS_TEMPLATE (t
));
10533 /* If the member is a class template, then -- even after
10534 substitution -- there may be dependent types in the
10535 template argument list for the class. We increment
10536 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10537 that function will assume that no types are dependent
10538 when outside of a template. */
10539 if (class_template_p
)
10540 ++processing_template_decl
;
10541 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10542 if (class_template_p
)
10543 --processing_template_decl
;
10544 if (newtag
== error_mark_node
)
10547 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10549 tree name
= TYPE_IDENTIFIER (t
);
10551 if (class_template_p
)
10552 /* Unfortunately, lookup_template_class sets
10553 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10554 instantiation (i.e., for the type of a member
10555 template class nested within a template class.)
10556 This behavior is required for
10557 maybe_process_partial_specialization to work
10558 correctly, but is not accurate in this case;
10559 the TAG is not an instantiation of anything.
10560 (The corresponding TEMPLATE_DECL is an
10561 instantiation, but the TYPE is not.) */
10562 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10564 /* Now, we call pushtag to put this NEWTAG into the scope of
10565 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10566 pushtag calling push_template_decl. We don't have to do
10567 this for enums because it will already have been done in
10570 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10571 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10574 else if (DECL_DECLARES_FUNCTION_P (t
))
10578 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10579 ++processing_template_decl
;
10580 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10581 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10582 --processing_template_decl
;
10583 set_current_access_from_decl (r
);
10584 finish_member_declaration (r
);
10585 /* Instantiate members marked with attribute used. */
10586 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10588 if (TREE_CODE (r
) == FUNCTION_DECL
10589 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10590 cp_check_omp_declare_reduction (r
);
10592 else if (DECL_CLASS_TEMPLATE_P (t
)
10593 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10594 /* A closure type for a lambda in a default argument for a
10595 member template. Ignore it; it will be instantiated with
10596 the default argument. */;
10599 /* Build new TYPE_FIELDS. */
10600 if (TREE_CODE (t
) == STATIC_ASSERT
)
10604 ++c_inhibit_evaluation_warnings
;
10606 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10607 tf_warning_or_error
, NULL_TREE
,
10608 /*integral_constant_expression_p=*/true);
10609 --c_inhibit_evaluation_warnings
;
10611 finish_static_assert (condition
,
10612 STATIC_ASSERT_MESSAGE (t
),
10613 STATIC_ASSERT_SOURCE_LOCATION (t
),
10614 /*member_p=*/true);
10616 else if (TREE_CODE (t
) != CONST_DECL
)
10619 tree vec
= NULL_TREE
;
10622 /* The file and line for this declaration, to
10623 assist in error message reporting. Since we
10624 called push_tinst_level above, we don't need to
10626 input_location
= DECL_SOURCE_LOCATION (t
);
10628 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10629 ++processing_template_decl
;
10630 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10631 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10632 --processing_template_decl
;
10634 if (TREE_CODE (r
) == TREE_VEC
)
10636 /* A capture pack became multiple fields. */
10638 len
= TREE_VEC_LENGTH (vec
);
10641 for (int i
= 0; i
< len
; ++i
)
10644 r
= TREE_VEC_ELT (vec
, i
);
10649 [t]he initialization (and any associated
10650 side-effects) of a static data member does
10651 not occur unless the static data member is
10652 itself used in a way that requires the
10653 definition of the static data member to
10656 Therefore, we do not substitute into the
10657 initialized for the static data member here. */
10658 finish_static_data_member_decl
10660 /*init=*/NULL_TREE
,
10661 /*init_const_expr_p=*/false,
10662 /*asmspec_tree=*/NULL_TREE
,
10664 /* Instantiate members marked with attribute used. */
10665 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10668 else if (TREE_CODE (r
) == FIELD_DECL
)
10670 /* Determine whether R has a valid type and can be
10671 completed later. If R is invalid, then its type
10672 is replaced by error_mark_node. */
10673 tree rtype
= TREE_TYPE (r
);
10674 if (can_complete_type_without_circularity (rtype
))
10675 complete_type (rtype
);
10677 if (!complete_or_array_type_p (rtype
))
10679 /* If R's type couldn't be completed and
10680 it isn't a flexible array member (whose
10681 type is incomplete by definition) give
10683 cxx_incomplete_type_error (r
, rtype
);
10684 TREE_TYPE (r
) = error_mark_node
;
10688 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10689 such a thing will already have been added to the field
10690 list by tsubst_enum in finish_member_declaration in the
10691 CLASSTYPE_NESTED_UTDS case above. */
10692 if (!(TREE_CODE (r
) == TYPE_DECL
10693 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10694 && DECL_ARTIFICIAL (r
)))
10696 set_current_access_from_decl (r
);
10697 finish_member_declaration (r
);
10705 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10706 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10708 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10710 tree friend_type
= t
;
10711 bool adjust_processing_template_decl
= false;
10713 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10715 /* template <class T> friend class C; */
10716 friend_type
= tsubst_friend_class (friend_type
, args
);
10717 adjust_processing_template_decl
= true;
10719 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10721 /* template <class T> friend class C::D; */
10722 friend_type
= tsubst (friend_type
, args
,
10723 tf_warning_or_error
, NULL_TREE
);
10724 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10725 friend_type
= TREE_TYPE (friend_type
);
10726 adjust_processing_template_decl
= true;
10728 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10729 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10731 /* This could be either
10735 when dependent_type_p is false or
10737 template <class U> friend class T::C;
10740 /* Bump processing_template_decl in case this is something like
10741 template <class T> friend struct A<T>::B. */
10742 ++processing_template_decl
;
10743 friend_type
= tsubst (friend_type
, args
,
10744 tf_warning_or_error
, NULL_TREE
);
10745 if (dependent_type_p (friend_type
))
10746 adjust_processing_template_decl
= true;
10747 --processing_template_decl
;
10749 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10750 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10751 && TYPE_HIDDEN_P (friend_type
))
10755 where C hasn't been declared yet. Let's lookup name
10756 from namespace scope directly, bypassing any name that
10757 come from dependent base class. */
10758 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10760 /* The call to xref_tag_from_type does injection for friend
10762 push_nested_namespace (ns
);
10764 xref_tag_from_type (friend_type
, NULL_TREE
,
10765 /*tag_scope=*/ts_current
);
10766 pop_nested_namespace (ns
);
10768 else if (uses_template_parms (friend_type
))
10769 /* friend class C<T>; */
10770 friend_type
= tsubst (friend_type
, args
,
10771 tf_warning_or_error
, NULL_TREE
);
10776 where C is already declared or
10778 friend class C<int>;
10780 We don't have to do anything in these cases. */
10782 if (adjust_processing_template_decl
)
10783 /* Trick make_friend_class into realizing that the friend
10784 we're adding is a template, not an ordinary class. It's
10785 important that we use make_friend_class since it will
10786 perform some error-checking and output cross-reference
10788 ++processing_template_decl
;
10790 if (friend_type
!= error_mark_node
)
10791 make_friend_class (type
, friend_type
, /*complain=*/false);
10793 if (adjust_processing_template_decl
)
10794 --processing_template_decl
;
10798 /* Build new DECL_FRIENDLIST. */
10801 /* The file and line for this declaration, to
10802 assist in error message reporting. Since we
10803 called push_tinst_level above, we don't need to
10805 input_location
= DECL_SOURCE_LOCATION (t
);
10807 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10809 ++processing_template_decl
;
10810 push_deferring_access_checks (dk_no_check
);
10813 r
= tsubst_friend_function (t
, args
);
10814 add_friend (type
, r
, /*complain=*/false);
10815 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10817 pop_deferring_access_checks ();
10818 --processing_template_decl
;
10826 /* Restore these before substituting into the lambda capture
10828 cp_unevaluated_operand
= saved_unevaluated_operand
;
10829 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10832 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10834 tree decl
= lambda_function (type
);
10837 if (cxx_dialect
>= cxx17
)
10838 CLASSTYPE_LITERAL_P (type
) = true;
10840 if (!DECL_TEMPLATE_INFO (decl
)
10841 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10843 /* Set function_depth to avoid garbage collection. */
10845 instantiate_decl (decl
, /*defer_ok=*/false, false);
10849 /* We need to instantiate the capture list from the template
10850 after we've instantiated the closure members, but before we
10851 consider adding the conversion op. Also keep any captures
10852 that may have been added during instantiation of the op(). */
10853 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10855 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10856 args
, tf_warning_or_error
, NULL_TREE
,
10859 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10860 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10862 maybe_add_lambda_conv_op (type
);
10865 gcc_assert (errorcount
);
10868 /* Set the file and line number information to whatever is given for
10869 the class itself. This puts error messages involving generated
10870 implicit functions at a predictable point, and the same point
10871 that would be used for non-template classes. */
10872 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10874 unreverse_member_declarations (type
);
10875 finish_struct_1 (type
);
10876 TYPE_BEING_DEFINED (type
) = 0;
10878 /* We don't instantiate default arguments for member functions. 14.7.1:
10880 The implicit instantiation of a class template specialization causes
10881 the implicit instantiation of the declarations, but not of the
10882 definitions or default arguments, of the class member functions,
10883 member classes, static data members and member templates.... */
10885 /* Some typedefs referenced from within the template code need to be access
10886 checked at template instantiation time, i.e now. These types were
10887 added to the template at parsing time. Let's get those and perform
10888 the access checks then. */
10889 perform_typedefs_access_check (pattern
, args
);
10890 perform_deferred_access_checks (tf_warning_or_error
);
10891 pop_nested_class ();
10892 maximum_field_alignment
= saved_maximum_field_alignment
;
10894 pop_from_top_level ();
10895 pop_deferring_access_checks ();
10896 pop_tinst_level ();
10898 /* The vtable for a template class can be emitted in any translation
10899 unit in which the class is instantiated. When there is no key
10900 method, however, finish_struct_1 will already have added TYPE to
10901 the keyed_classes. */
10902 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10903 vec_safe_push (keyed_classes
, type
);
10908 /* Wrapper for instantiate_class_template_1. */
10911 instantiate_class_template (tree type
)
10914 timevar_push (TV_TEMPLATE_INST
);
10915 ret
= instantiate_class_template_1 (type
);
10916 timevar_pop (TV_TEMPLATE_INST
);
10921 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10927 else if (TYPE_P (t
))
10928 r
= tsubst (t
, args
, complain
, in_decl
);
10931 if (!(complain
& tf_warning
))
10932 ++c_inhibit_evaluation_warnings
;
10933 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10934 /*integral_constant_expression_p=*/true);
10935 if (!(complain
& tf_warning
))
10936 --c_inhibit_evaluation_warnings
;
10941 /* Given a function parameter pack TMPL_PARM and some function parameters
10942 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10943 and set *SPEC_P to point at the next point in the list. */
10946 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10948 /* Collect all of the extra "packed" parameters into an
10951 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10952 tree spec_parm
= *spec_p
;
10955 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10957 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10960 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10961 parmvec
= make_tree_vec (len
);
10962 spec_parm
= *spec_p
;
10963 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10964 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10966 /* Build the argument packs. */
10967 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10968 *spec_p
= spec_parm
;
10973 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10974 NONTYPE_ARGUMENT_PACK. */
10977 make_fnparm_pack (tree spec_parm
)
10979 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10982 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10983 pack expansion with no extra args, 2 if it has extra args, or 0
10984 if it is not a pack expansion. */
10987 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10989 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10990 if (i
>= TREE_VEC_LENGTH (vec
))
10992 tree elt
= TREE_VEC_ELT (vec
, i
);
10994 /* A decl pack is itself an expansion. */
10995 elt
= TREE_TYPE (elt
);
10996 if (!PACK_EXPANSION_P (elt
))
10998 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11004 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11007 make_argument_pack_select (tree arg_pack
, unsigned index
)
11009 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11011 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11012 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11017 /* This is a subroutine of tsubst_pack_expansion.
11019 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11020 mechanism to store the (non complete list of) arguments of the
11021 substitution and return a non substituted pack expansion, in order
11022 to wait for when we have enough arguments to really perform the
11026 use_pack_expansion_extra_args_p (tree parm_packs
,
11028 bool has_empty_arg
)
11030 /* If one pack has an expansion and another pack has a normal
11031 argument or if one pack has an empty argument and an another
11032 one hasn't then tsubst_pack_expansion cannot perform the
11033 substitution and need to fall back on the
11034 PACK_EXPANSION_EXTRA mechanism. */
11035 if (parm_packs
== NULL_TREE
)
11037 else if (has_empty_arg
)
11040 bool has_expansion_arg
= false;
11041 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11043 bool has_non_expansion_arg
= false;
11044 for (tree parm_pack
= parm_packs
;
11046 parm_pack
= TREE_CHAIN (parm_pack
))
11048 tree arg
= TREE_VALUE (parm_pack
);
11050 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11052 /* We can't substitute a pack expansion with extra args into
11056 has_expansion_arg
= true;
11058 has_non_expansion_arg
= true;
11061 if (has_expansion_arg
&& has_non_expansion_arg
)
11067 /* [temp.variadic]/6 says that:
11069 The instantiation of a pack expansion [...]
11070 produces a list E1,E2, ..., En, where N is the number of elements
11071 in the pack expansion parameters.
11073 This subroutine of tsubst_pack_expansion produces one of these Ei.
11075 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11076 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11077 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11078 INDEX is the index 'i' of the element Ei to produce. ARGS,
11079 COMPLAIN, and IN_DECL are the same parameters as for the
11080 tsubst_pack_expansion function.
11082 The function returns the resulting Ei upon successful completion,
11083 or error_mark_node.
11085 Note that this function possibly modifies the ARGS parameter, so
11086 it's the responsibility of the caller to restore it. */
11089 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11092 tree args
/* This parm gets
11094 tsubst_flags_t complain
,
11098 bool ith_elem_is_expansion
= false;
11100 /* For each parameter pack, change the substitution of the parameter
11101 pack to the ith argument in its argument pack, then expand the
11103 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11105 tree parm
= TREE_PURPOSE (pack
);
11106 tree arg_pack
= TREE_VALUE (pack
);
11107 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11109 ith_elem_is_expansion
|=
11110 argument_pack_element_is_expansion_p (arg_pack
, index
);
11112 /* Select the Ith argument from the pack. */
11113 if (TREE_CODE (parm
) == PARM_DECL
11114 || TREE_CODE (parm
) == FIELD_DECL
)
11118 aps
= make_argument_pack_select (arg_pack
, index
);
11119 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11120 return error_mark_node
;
11121 register_local_specialization (aps
, parm
);
11124 aps
= retrieve_local_specialization (parm
);
11129 template_parm_level_and_index (parm
, &level
, &idx
);
11133 aps
= make_argument_pack_select (arg_pack
, index
);
11134 /* Update the corresponding argument. */
11135 TMPL_ARG (args
, level
, idx
) = aps
;
11138 /* Re-use the ARGUMENT_PACK_SELECT. */
11139 aps
= TMPL_ARG (args
, level
, idx
);
11141 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11144 /* Substitute into the PATTERN with the (possibly altered)
11146 if (pattern
== in_decl
)
11147 /* Expanding a fixed parameter pack from
11148 coerce_template_parameter_pack. */
11149 t
= tsubst_decl (pattern
, args
, complain
);
11150 else if (pattern
== error_mark_node
)
11151 t
= error_mark_node
;
11152 else if (constraint_p (pattern
))
11154 if (processing_template_decl
)
11155 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11157 t
= (constraints_satisfied_p (pattern
, args
)
11158 ? boolean_true_node
: boolean_false_node
);
11160 else if (!TYPE_P (pattern
))
11161 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11162 /*integral_constant_expression_p=*/false);
11164 t
= tsubst (pattern
, args
, complain
, in_decl
);
11166 /* If the Ith argument pack element is a pack expansion, then
11167 the Ith element resulting from the substituting is going to
11168 be a pack expansion as well. */
11169 if (ith_elem_is_expansion
)
11170 t
= make_pack_expansion (t
, complain
);
11175 /* When the unexpanded parameter pack in a fold expression expands to an empty
11176 sequence, the value of the expression is as follows; the program is
11177 ill-formed if the operator is not listed in this table.
11184 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11186 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11187 if (!FOLD_EXPR_MODIFY_P (t
))
11190 case TRUTH_ANDIF_EXPR
:
11191 return boolean_true_node
;
11192 case TRUTH_ORIF_EXPR
:
11193 return boolean_false_node
;
11194 case COMPOUND_EXPR
:
11200 if (complain
& tf_error
)
11201 error_at (location_of (t
),
11202 "fold of empty expansion over %O", code
);
11203 return error_mark_node
;
11206 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11207 form an expression that combines the two terms using the
11211 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11213 tree op
= FOLD_EXPR_OP (t
);
11214 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11216 // Handle compound assignment operators.
11217 if (FOLD_EXPR_MODIFY_P (t
))
11218 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11222 case COMPOUND_EXPR
:
11223 return build_x_compound_expr (input_location
, left
, right
, complain
);
11225 return build_m_component_ref (left
, right
, complain
);
11227 return build_x_binary_op (input_location
, code
,
11228 left
, TREE_CODE (left
),
11229 right
, TREE_CODE (right
),
11235 /* Substitute ARGS into the pack of a fold expression T. */
11238 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11240 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11243 /* Substitute ARGS into the pack of a fold expression T. */
11246 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11248 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11251 /* Expand a PACK of arguments into a grouped as left fold.
11252 Given a pack containing elements A0, A1, ..., An and an
11253 operator @, this builds the expression:
11255 ((A0 @ A1) @ A2) ... @ An
11257 Note that PACK must not be empty.
11259 The operator is defined by the original fold expression T. */
11262 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11264 tree left
= TREE_VEC_ELT (pack
, 0);
11265 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11267 tree right
= TREE_VEC_ELT (pack
, i
);
11268 left
= fold_expression (t
, left
, right
, complain
);
11273 /* Substitute into a unary left fold expression. */
11276 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11279 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11280 if (pack
== error_mark_node
)
11281 return error_mark_node
;
11282 if (PACK_EXPANSION_P (pack
))
11284 tree r
= copy_node (t
);
11285 FOLD_EXPR_PACK (r
) = pack
;
11288 if (TREE_VEC_LENGTH (pack
) == 0)
11289 return expand_empty_fold (t
, complain
);
11291 return expand_left_fold (t
, pack
, complain
);
11294 /* Substitute into a binary left fold expression.
11296 Do ths by building a single (non-empty) vector of argumnts and
11297 building the expression from those elements. */
11300 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11303 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11304 if (pack
== error_mark_node
)
11305 return error_mark_node
;
11306 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11307 if (init
== error_mark_node
)
11308 return error_mark_node
;
11310 if (PACK_EXPANSION_P (pack
))
11312 tree r
= copy_node (t
);
11313 FOLD_EXPR_PACK (r
) = pack
;
11314 FOLD_EXPR_INIT (r
) = init
;
11318 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11319 TREE_VEC_ELT (vec
, 0) = init
;
11320 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11321 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11323 return expand_left_fold (t
, vec
, complain
);
11326 /* Expand a PACK of arguments into a grouped as right fold.
11327 Given a pack containing elementns A0, A1, ..., and an
11328 operator @, this builds the expression:
11330 A0@ ... (An-2 @ (An-1 @ An))
11332 Note that PACK must not be empty.
11334 The operator is defined by the original fold expression T. */
11337 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11339 // Build the expression.
11340 int n
= TREE_VEC_LENGTH (pack
);
11341 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11342 for (--n
; n
!= 0; --n
)
11344 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11345 right
= fold_expression (t
, left
, right
, complain
);
11350 /* Substitute into a unary right fold expression. */
11353 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11356 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11357 if (pack
== error_mark_node
)
11358 return error_mark_node
;
11359 if (PACK_EXPANSION_P (pack
))
11361 tree r
= copy_node (t
);
11362 FOLD_EXPR_PACK (r
) = pack
;
11365 if (TREE_VEC_LENGTH (pack
) == 0)
11366 return expand_empty_fold (t
, complain
);
11368 return expand_right_fold (t
, pack
, complain
);
11371 /* Substitute into a binary right fold expression.
11373 Do ths by building a single (non-empty) vector of arguments and
11374 building the expression from those elements. */
11377 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11380 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11381 if (pack
== error_mark_node
)
11382 return error_mark_node
;
11383 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11384 if (init
== error_mark_node
)
11385 return error_mark_node
;
11387 if (PACK_EXPANSION_P (pack
))
11389 tree r
= copy_node (t
);
11390 FOLD_EXPR_PACK (r
) = pack
;
11391 FOLD_EXPR_INIT (r
) = init
;
11395 int n
= TREE_VEC_LENGTH (pack
);
11396 tree vec
= make_tree_vec (n
+ 1);
11397 for (int i
= 0; i
< n
; ++i
)
11398 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11399 TREE_VEC_ELT (vec
, n
) = init
;
11401 return expand_right_fold (t
, vec
, complain
);
11405 /* Substitute ARGS into T, which is an pack expansion
11406 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11407 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11408 (if only a partial substitution could be performed) or
11409 ERROR_MARK_NODE if there was an error. */
11411 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11415 tree pack
, packs
= NULL_TREE
;
11416 bool unsubstituted_packs
= false;
11419 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11420 bool need_local_specializations
= false;
11423 gcc_assert (PACK_EXPANSION_P (t
));
11424 pattern
= PACK_EXPANSION_PATTERN (t
);
11426 /* Add in any args remembered from an earlier partial instantiation. */
11427 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11429 levels
= TMPL_ARGS_DEPTH (args
);
11431 /* Determine the argument packs that will instantiate the parameter
11432 packs used in the expansion expression. While we're at it,
11433 compute the number of arguments to be expanded and make sure it
11435 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11436 pack
= TREE_CHAIN (pack
))
11438 tree parm_pack
= TREE_VALUE (pack
);
11439 tree arg_pack
= NULL_TREE
;
11440 tree orig_arg
= NULL_TREE
;
11443 if (TREE_CODE (parm_pack
) == BASES
)
11445 gcc_assert (parm_pack
== pattern
);
11446 if (BASES_DIRECT (parm_pack
))
11447 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11448 args
, complain
, in_decl
, false));
11450 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11451 args
, complain
, in_decl
, false));
11453 else if (builtin_pack_call_p (parm_pack
))
11455 /* ??? Support use in other patterns. */
11456 gcc_assert (parm_pack
== pattern
);
11457 return expand_builtin_pack_call (parm_pack
, args
,
11458 complain
, in_decl
);
11460 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11462 /* We know we have correct local_specializations if this
11463 expansion is at function scope, or if we're dealing with a
11464 local parameter in a requires expression; for the latter,
11465 tsubst_requires_expr set it up appropriately. */
11466 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11467 arg_pack
= retrieve_local_specialization (parm_pack
);
11469 /* We can't rely on local_specializations for a parameter
11470 name used later in a function declaration (such as in a
11471 late-specified return type). Even if it exists, it might
11472 have the wrong value for a recursive call. */
11473 need_local_specializations
= true;
11477 /* This parameter pack was used in an unevaluated context. Just
11478 make a dummy decl, since it's only used for its type. */
11479 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11480 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11481 /* Partial instantiation of the parm_pack, we can't build
11482 up an argument pack yet. */
11483 arg_pack
= NULL_TREE
;
11485 arg_pack
= make_fnparm_pack (arg_pack
);
11488 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11489 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11493 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11495 if (level
<= levels
)
11496 arg_pack
= TMPL_ARG (args
, level
, idx
);
11499 orig_arg
= arg_pack
;
11500 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11501 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11503 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11504 /* This can only happen if we forget to expand an argument
11505 pack somewhere else. Just return an error, silently. */
11507 result
= make_tree_vec (1);
11508 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11515 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11517 /* Don't bother trying to do a partial substitution with
11518 incomplete packs; we'll try again after deduction. */
11519 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11524 else if (len
!= my_len
)
11526 if (!(complain
& tf_error
))
11527 /* Fail quietly. */;
11528 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11529 error ("mismatched argument pack lengths while expanding %qT",
11532 error ("mismatched argument pack lengths while expanding %qE",
11534 return error_mark_node
;
11537 /* Keep track of the parameter packs and their corresponding
11539 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11540 TREE_TYPE (packs
) = orig_arg
;
11544 /* We can't substitute for this parameter pack. We use a flag as
11545 well as the missing_level counter because function parameter
11546 packs don't have a level. */
11547 gcc_assert (processing_template_decl
);
11548 unsubstituted_packs
= true;
11552 /* If the expansion is just T..., return the matching argument pack, unless
11553 we need to call convert_from_reference on all the elements. This is an
11554 important optimization; see c++/68422. */
11555 if (!unsubstituted_packs
11556 && TREE_PURPOSE (packs
) == pattern
)
11558 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11559 /* Types need no adjustment, nor does sizeof..., and if we still have
11560 some pack expansion args we won't do anything yet. */
11561 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11562 || PACK_EXPANSION_SIZEOF_P (t
)
11563 || pack_expansion_args_count (args
))
11565 /* Also optimize expression pack expansions if we can tell that the
11566 elements won't have reference type. */
11567 tree type
= TREE_TYPE (pattern
);
11568 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11569 && !PACK_EXPANSION_P (type
)
11570 && !WILDCARD_TYPE_P (type
))
11572 /* Otherwise use the normal path so we get convert_from_reference. */
11575 /* We cannot expand this expansion expression, because we don't have
11576 all of the argument packs we need. */
11577 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11579 /* We got some full packs, but we can't substitute them in until we
11580 have values for all the packs. So remember these until then. */
11582 t
= make_pack_expansion (pattern
, complain
);
11583 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11586 else if (unsubstituted_packs
)
11588 /* There were no real arguments, we're just replacing a parameter
11589 pack with another version of itself. Substitute into the
11590 pattern and return a PACK_EXPANSION_*. The caller will need to
11592 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11593 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11594 /*integral_constant_expression_p=*/false);
11596 t
= tsubst (pattern
, args
, complain
, in_decl
);
11597 t
= make_pack_expansion (t
, complain
);
11601 gcc_assert (len
>= 0);
11603 if (need_local_specializations
)
11605 /* We're in a late-specified return type, so create our own local
11606 specializations map; the current map is either NULL or (in the
11607 case of recursive unification) might have bindings that we don't
11608 want to use or alter. */
11609 saved_local_specializations
= local_specializations
;
11610 local_specializations
= new hash_map
<tree
, tree
>;
11613 /* For each argument in each argument pack, substitute into the
11615 result
= make_tree_vec (len
);
11616 tree elem_args
= copy_template_args (args
);
11617 for (i
= 0; i
< len
; ++i
)
11619 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11621 elem_args
, complain
,
11623 TREE_VEC_ELT (result
, i
) = t
;
11624 if (t
== error_mark_node
)
11626 result
= error_mark_node
;
11631 /* Update ARGS to restore the substitution from parameter packs to
11632 their argument packs. */
11633 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11635 tree parm
= TREE_PURPOSE (pack
);
11637 if (TREE_CODE (parm
) == PARM_DECL
11638 || TREE_CODE (parm
) == FIELD_DECL
)
11639 register_local_specialization (TREE_TYPE (pack
), parm
);
11644 if (TREE_VALUE (pack
) == NULL_TREE
)
11647 template_parm_level_and_index (parm
, &level
, &idx
);
11649 /* Update the corresponding argument. */
11650 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11651 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11654 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11658 if (need_local_specializations
)
11660 delete local_specializations
;
11661 local_specializations
= saved_local_specializations
;
11664 /* If the dependent pack arguments were such that we end up with only a
11665 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11666 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11667 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11668 return TREE_VEC_ELT (result
, 0);
11673 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11674 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11675 parameter packs; all parms generated from a function parameter pack will
11676 have the same DECL_PARM_INDEX. */
11679 get_pattern_parm (tree parm
, tree tmpl
)
11681 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11684 if (DECL_ARTIFICIAL (parm
))
11686 for (patparm
= DECL_ARGUMENTS (pattern
);
11687 patparm
; patparm
= DECL_CHAIN (patparm
))
11688 if (DECL_ARTIFICIAL (patparm
)
11689 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11694 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11695 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11696 gcc_assert (DECL_PARM_INDEX (patparm
)
11697 == DECL_PARM_INDEX (parm
));
11703 /* Make an argument pack out of the TREE_VEC VEC. */
11706 make_argument_pack (tree vec
)
11709 tree elt
= TREE_VEC_ELT (vec
, 0);
11711 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11714 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11715 TREE_CONSTANT (pack
) = 1;
11717 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11721 /* Return an exact copy of template args T that can be modified
11725 copy_template_args (tree t
)
11727 if (t
== error_mark_node
)
11730 int len
= TREE_VEC_LENGTH (t
);
11731 tree new_vec
= make_tree_vec (len
);
11733 for (int i
= 0; i
< len
; ++i
)
11735 tree elt
= TREE_VEC_ELT (t
, i
);
11736 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11737 elt
= copy_template_args (elt
);
11738 TREE_VEC_ELT (new_vec
, i
) = elt
;
11741 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11742 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11747 /* Substitute ARGS into the vector or list of template arguments T. */
11750 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11753 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11756 if (t
== error_mark_node
)
11757 return error_mark_node
;
11759 len
= TREE_VEC_LENGTH (t
);
11760 elts
= XALLOCAVEC (tree
, len
);
11762 for (i
= 0; i
< len
; i
++)
11764 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11767 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11768 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11769 else if (PACK_EXPANSION_P (orig_arg
))
11771 /* Substitute into an expansion expression. */
11772 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11774 if (TREE_CODE (new_arg
) == TREE_VEC
)
11775 /* Add to the expanded length adjustment the number of
11776 expanded arguments. We subtract one from this
11777 measurement, because the argument pack expression
11778 itself is already counted as 1 in
11779 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11780 the argument pack is empty. */
11781 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11783 else if (ARGUMENT_PACK_P (orig_arg
))
11785 /* Substitute into each of the arguments. */
11786 new_arg
= TYPE_P (orig_arg
)
11787 ? cxx_make_type (TREE_CODE (orig_arg
))
11788 : make_node (TREE_CODE (orig_arg
));
11790 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11791 args
, complain
, in_decl
);
11792 if (pack_args
== error_mark_node
)
11793 new_arg
= error_mark_node
;
11795 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11797 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11798 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11801 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11803 if (new_arg
== error_mark_node
)
11804 return error_mark_node
;
11807 if (new_arg
!= orig_arg
)
11814 /* Make space for the expanded arguments coming from template
11816 t
= make_tree_vec (len
+ expanded_len_adjust
);
11817 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11818 arguments for a member template.
11819 In that case each TREE_VEC in ORIG_T represents a level of template
11820 arguments, and ORIG_T won't carry any non defaulted argument count.
11821 It will rather be the nested TREE_VECs that will carry one.
11822 In other words, ORIG_T carries a non defaulted argument count only
11823 if it doesn't contain any nested TREE_VEC. */
11824 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11826 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11827 count
+= expanded_len_adjust
;
11828 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11830 for (i
= 0, out
= 0; i
< len
; i
++)
11832 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11833 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11834 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11838 /* Now expand the template argument pack "in place". */
11839 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11840 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11844 TREE_VEC_ELT (t
, out
) = elts
[i
];
11852 /* Substitute ARGS into one level PARMS of template parameters. */
11855 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11857 if (parms
== error_mark_node
)
11858 return error_mark_node
;
11860 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11862 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11864 tree tuple
= TREE_VEC_ELT (parms
, i
);
11866 if (tuple
== error_mark_node
)
11869 TREE_VEC_ELT (new_vec
, i
) =
11870 tsubst_template_parm (tuple
, args
, complain
);
11876 /* Return the result of substituting ARGS into the template parameters
11877 given by PARMS. If there are m levels of ARGS and m + n levels of
11878 PARMS, then the result will contain n levels of PARMS. For
11879 example, if PARMS is `template <class T> template <class U>
11880 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11881 result will be `template <int*, double, class V>'. */
11884 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11886 tree r
= NULL_TREE
;
11889 /* When substituting into a template, we must set
11890 PROCESSING_TEMPLATE_DECL as the template parameters may be
11891 dependent if they are based on one-another, and the dependency
11892 predicates are short-circuit outside of templates. */
11893 ++processing_template_decl
;
11895 for (new_parms
= &r
;
11896 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11897 new_parms
= &(TREE_CHAIN (*new_parms
)),
11898 parms
= TREE_CHAIN (parms
))
11900 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11903 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11904 - TMPL_ARGS_DEPTH (args
)),
11905 new_vec
, NULL_TREE
);
11908 --processing_template_decl
;
11913 /* Return the result of substituting ARGS into one template parameter
11914 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11915 parameter and which TREE_PURPOSE is the default argument of the
11916 template parameter. */
11919 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11921 tree default_value
, parm_decl
;
11923 if (args
== NULL_TREE
11925 || t
== error_mark_node
)
11928 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11930 default_value
= TREE_PURPOSE (t
);
11931 parm_decl
= TREE_VALUE (t
);
11933 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11934 if (TREE_CODE (parm_decl
) == PARM_DECL
11935 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11936 parm_decl
= error_mark_node
;
11937 default_value
= tsubst_template_arg (default_value
, args
,
11938 complain
, NULL_TREE
);
11940 return build_tree_list (default_value
, parm_decl
);
11943 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11944 type T. If T is not an aggregate or enumeration type, it is
11945 handled as if by tsubst. IN_DECL is as for tsubst. If
11946 ENTERING_SCOPE is nonzero, T is the context for a template which
11947 we are presently tsubst'ing. Return the substituted value. */
11950 tsubst_aggr_type (tree t
,
11952 tsubst_flags_t complain
,
11954 int entering_scope
)
11956 if (t
== NULL_TREE
)
11959 switch (TREE_CODE (t
))
11962 if (TYPE_PTRMEMFUNC_P (t
))
11963 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11965 /* Fall through. */
11966 case ENUMERAL_TYPE
:
11968 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11973 int saved_unevaluated_operand
;
11974 int saved_inhibit_evaluation_warnings
;
11976 /* In "sizeof(X<I>)" we need to evaluate "I". */
11977 saved_unevaluated_operand
= cp_unevaluated_operand
;
11978 cp_unevaluated_operand
= 0;
11979 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11980 c_inhibit_evaluation_warnings
= 0;
11982 /* First, determine the context for the type we are looking
11984 context
= TYPE_CONTEXT (t
);
11985 if (context
&& TYPE_P (context
))
11987 context
= tsubst_aggr_type (context
, args
, complain
,
11988 in_decl
, /*entering_scope=*/1);
11989 /* If context is a nested class inside a class template,
11990 it may still need to be instantiated (c++/33959). */
11991 context
= complete_type (context
);
11994 /* Then, figure out what arguments are appropriate for the
11995 type we are trying to find. For example, given:
11997 template <class T> struct S;
11998 template <class T, class U> void f(T, U) { S<U> su; }
12000 and supposing that we are instantiating f<int, double>,
12001 then our ARGS will be {int, double}, but, when looking up
12002 S we only want {double}. */
12003 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12004 complain
, in_decl
);
12005 if (argvec
== error_mark_node
)
12006 r
= error_mark_node
;
12009 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12010 entering_scope
, complain
);
12011 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12014 cp_unevaluated_operand
= saved_unevaluated_operand
;
12015 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12020 /* This is not a template type, so there's nothing to do. */
12024 return tsubst (t
, args
, complain
, in_decl
);
12028 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12030 /* Substitute into the default argument ARG (a default argument for
12031 FN), which has the indicated TYPE. */
12034 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12035 tsubst_flags_t complain
)
12037 tree saved_class_ptr
= NULL_TREE
;
12038 tree saved_class_ref
= NULL_TREE
;
12039 int errs
= errorcount
+ sorrycount
;
12041 /* This can happen in invalid code. */
12042 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12045 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12046 parm
= chain_index (parmnum
, parm
);
12047 tree parmtype
= TREE_TYPE (parm
);
12048 if (DECL_BY_REFERENCE (parm
))
12049 parmtype
= TREE_TYPE (parmtype
);
12050 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12053 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12056 /* This default argument came from a template. Instantiate the
12057 default argument here, not in tsubst. In the case of
12066 we must be careful to do name lookup in the scope of S<T>,
12067 rather than in the current class. */
12068 push_access_scope (fn
);
12069 /* The "this" pointer is not valid in a default argument. */
12072 saved_class_ptr
= current_class_ptr
;
12073 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12074 saved_class_ref
= current_class_ref
;
12075 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12078 start_lambda_scope (parm
);
12080 push_deferring_access_checks(dk_no_deferred
);
12081 /* The default argument expression may cause implicitly defined
12082 member functions to be synthesized, which will result in garbage
12083 collection. We must treat this situation as if we were within
12084 the body of function so as to avoid collecting live data on the
12087 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12088 complain
, NULL_TREE
,
12089 /*integral_constant_expression_p=*/false);
12091 pop_deferring_access_checks();
12093 finish_lambda_scope ();
12095 /* Restore the "this" pointer. */
12098 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12099 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12102 if (errorcount
+sorrycount
> errs
12103 && (complain
& tf_warning_or_error
))
12104 inform (input_location
,
12105 " when instantiating default argument for call to %qD", fn
);
12107 /* Make sure the default argument is reasonable. */
12108 arg
= check_default_argument (type
, arg
, complain
);
12110 pop_access_scope (fn
);
12112 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12115 defarg_inst
= tree_cache_map::create_ggc (37);
12116 defarg_inst
->put (parm
, arg
);
12122 /* Substitute into all the default arguments for FN. */
12125 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12130 tmpl_args
= DECL_TI_ARGS (fn
);
12132 /* If this function is not yet instantiated, we certainly don't need
12133 its default arguments. */
12134 if (uses_template_parms (tmpl_args
))
12136 /* Don't do this again for clones. */
12137 if (DECL_CLONED_FUNCTION_P (fn
))
12141 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12143 arg
= TREE_CHAIN (arg
), ++i
)
12144 if (TREE_PURPOSE (arg
))
12145 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12147 TREE_PURPOSE (arg
),
12151 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12154 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12155 tree lambda_fntype
)
12157 tree gen_tmpl
, argvec
;
12158 hashval_t hash
= 0;
12161 /* Nobody should be tsubst'ing into non-template functions. */
12162 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12164 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12166 /* If T is not dependent, just return it. */
12167 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12170 /* Calculate the most general template of which R is a
12171 specialization, and the complete set of arguments used to
12173 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12174 argvec
= tsubst_template_args (DECL_TI_ARGS
12175 (DECL_TEMPLATE_RESULT
12176 (DECL_TI_TEMPLATE (t
))),
12177 args
, complain
, in_decl
);
12178 if (argvec
== error_mark_node
)
12179 return error_mark_node
;
12181 /* Check to see if we already have this specialization. */
12182 if (!lambda_fntype
)
12184 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12185 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12189 /* We can see more levels of arguments than parameters if
12190 there was a specialization of a member template, like
12193 template <class T> struct S { template <class U> void f(); }
12194 template <> template <class U> void S<int>::f(U);
12196 Here, we'll be substituting into the specialization,
12197 because that's where we can find the code we actually
12198 want to generate, but we'll have enough arguments for
12199 the most general template.
12201 We also deal with the peculiar case:
12203 template <class T> struct S {
12204 template <class U> friend void f();
12206 template <class U> void f() {}
12208 template void f<double>();
12210 Here, the ARGS for the instantiation of will be {int,
12211 double}. But, we only need as many ARGS as there are
12212 levels of template parameters in CODE_PATTERN. We are
12213 careful not to get fooled into reducing the ARGS in
12216 template <class T> struct S { template <class U> void f(U); }
12217 template <class T> template <> void S<T>::f(int) {}
12219 which we can spot because the pattern will be a
12220 specialization in this case. */
12221 int args_depth
= TMPL_ARGS_DEPTH (args
);
12223 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12225 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12226 args
= get_innermost_template_args (args
, parms_depth
);
12230 /* This special case arises when we have something like this:
12232 template <class T> struct S {
12233 friend void f<int>(int, double);
12236 Here, the DECL_TI_TEMPLATE for the friend declaration
12237 will be an IDENTIFIER_NODE. We are being called from
12238 tsubst_friend_function, and we want only to create a
12239 new decl (R) with appropriate types so that we can call
12240 determine_specialization. */
12241 gen_tmpl
= NULL_TREE
;
12242 argvec
= NULL_TREE
;
12245 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12247 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12248 bool member
= ctx
&& TYPE_P (ctx
);
12250 if (member
&& !closure
)
12251 ctx
= tsubst_aggr_type (ctx
, args
,
12252 complain
, t
, /*entering_scope=*/1);
12254 tree type
= (lambda_fntype
? lambda_fntype
12255 : tsubst (TREE_TYPE (t
), args
,
12256 complain
| tf_fndecl_type
, in_decl
));
12257 if (type
== error_mark_node
)
12258 return error_mark_node
;
12260 /* If we hit excessive deduction depth, the type is bogus even if
12261 it isn't error_mark_node, so don't build a decl. */
12262 if (excessive_deduction_depth
)
12263 return error_mark_node
;
12265 /* We do NOT check for matching decls pushed separately at this
12266 point, as they may not represent instantiations of this
12267 template, and in any case are considered separate under the
12269 tree r
= copy_decl (t
);
12270 DECL_USE_TEMPLATE (r
) = 0;
12271 TREE_TYPE (r
) = type
;
12272 /* Clear out the mangled name and RTL for the instantiation. */
12273 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12274 SET_DECL_RTL (r
, NULL
);
12275 /* Leave DECL_INITIAL set on deleted instantiations. */
12276 if (!DECL_DELETED_FN (r
))
12277 DECL_INITIAL (r
) = NULL_TREE
;
12278 DECL_CONTEXT (r
) = ctx
;
12280 /* OpenMP UDRs have the only argument a reference to the declared
12281 type. We want to diagnose if the declared type is a reference,
12282 which is invalid, but as references to references are usually
12283 quietly merged, diagnose it here. */
12284 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12287 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12288 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12289 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12290 error_at (DECL_SOURCE_LOCATION (t
),
12291 "reference type %qT in "
12292 "%<#pragma omp declare reduction%>", argtype
);
12293 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12294 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12298 if (member
&& DECL_CONV_FN_P (r
))
12299 /* Type-conversion operator. Reconstruct the name, in
12300 case it's the name of one of the template's parameters. */
12301 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12303 tree parms
= DECL_ARGUMENTS (t
);
12305 parms
= DECL_CHAIN (parms
);
12306 parms
= tsubst (parms
, args
, complain
, t
);
12307 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12308 DECL_CONTEXT (parm
) = r
;
12311 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12312 DECL_CHAIN (tparm
) = parms
;
12315 DECL_ARGUMENTS (r
) = parms
;
12316 DECL_RESULT (r
) = NULL_TREE
;
12318 TREE_STATIC (r
) = 0;
12319 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12320 DECL_EXTERNAL (r
) = 1;
12321 /* If this is an instantiation of a function with internal
12322 linkage, we already know what object file linkage will be
12323 assigned to the instantiation. */
12324 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12325 DECL_DEFER_OUTPUT (r
) = 0;
12326 DECL_CHAIN (r
) = NULL_TREE
;
12327 DECL_PENDING_INLINE_INFO (r
) = 0;
12328 DECL_PENDING_INLINE_P (r
) = 0;
12329 DECL_SAVED_TREE (r
) = NULL_TREE
;
12330 DECL_STRUCT_FUNCTION (r
) = NULL
;
12332 /* We'll re-clone as appropriate in instantiate_template. */
12333 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12335 /* If we aren't complaining now, return on error before we register
12336 the specialization so that we'll complain eventually. */
12337 if ((complain
& tf_error
) == 0
12338 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12339 && !grok_op_properties (r
, /*complain=*/false))
12340 return error_mark_node
;
12342 /* When instantiating a constrained member, substitute
12343 into the constraints to create a new constraint. */
12344 if (tree ci
= get_constraints (t
))
12347 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12348 set_constraints (r
, ci
);
12351 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12352 this in the special friend case mentioned above where
12353 GEN_TMPL is NULL. */
12354 if (gen_tmpl
&& !closure
)
12356 DECL_TEMPLATE_INFO (r
)
12357 = build_template_info (gen_tmpl
, argvec
);
12358 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12361 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12363 /* We instantiated this while substituting into
12364 the type earlier (template/friend54.C). */
12367 /* We're not supposed to instantiate default arguments
12368 until they are called, for a template. But, for a
12371 template <class T> void f ()
12372 { extern void g(int i = T()); }
12374 we should do the substitution when the template is
12375 instantiated. We handle the member function case in
12376 instantiate_class_template since the default arguments
12377 might refer to other members of the class. */
12379 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12380 && !uses_template_parms (argvec
))
12381 tsubst_default_arguments (r
, complain
);
12384 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12386 /* Copy the list of befriending classes. */
12387 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12389 friends
= &TREE_CHAIN (*friends
))
12391 *friends
= copy_node (*friends
);
12392 TREE_VALUE (*friends
)
12393 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12396 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12398 maybe_retrofit_in_chrg (r
);
12399 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12400 return error_mark_node
;
12401 /* If this is an instantiation of a member template, clone it.
12402 If it isn't, that'll be handled by
12403 clone_constructors_and_destructors. */
12404 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12405 clone_function_decl (r
, /*update_methods=*/false);
12407 else if ((complain
& tf_error
) != 0
12408 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12409 && !grok_op_properties (r
, /*complain=*/true))
12410 return error_mark_node
;
12412 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12413 SET_DECL_FRIEND_CONTEXT (r
,
12414 tsubst (DECL_FRIEND_CONTEXT (t
),
12415 args
, complain
, in_decl
));
12417 /* Possibly limit visibility based on template args. */
12418 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12419 if (DECL_VISIBILITY_SPECIFIED (t
))
12421 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12422 DECL_ATTRIBUTES (r
)
12423 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12425 determine_visibility (r
);
12426 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12427 && !processing_template_decl
)
12428 defaulted_late_check (r
);
12430 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12431 args
, complain
, in_decl
);
12435 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12438 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12439 tree lambda_fntype
)
12441 /* We can get here when processing a member function template,
12442 member class template, or template template parameter. */
12443 tree decl
= DECL_TEMPLATE_RESULT (t
);
12449 hashval_t hash
= 0;
12451 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12453 /* Template template parameter is treated here. */
12454 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12455 if (new_type
== error_mark_node
)
12456 r
= error_mark_node
;
12457 /* If we get a real template back, return it. This can happen in
12458 the context of most_specialized_partial_spec. */
12459 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12462 /* The new TEMPLATE_DECL was built in
12463 reduce_template_parm_level. */
12464 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12468 if (!lambda_fntype
)
12470 /* We might already have an instance of this template.
12471 The ARGS are for the surrounding class type, so the
12472 full args contain the tsubst'd args for the context,
12473 plus the innermost args from the template decl. */
12474 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12475 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12476 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12477 /* Because this is a template, the arguments will still be
12478 dependent, even after substitution. If
12479 PROCESSING_TEMPLATE_DECL is not set, the dependency
12480 predicates will short-circuit. */
12481 ++processing_template_decl
;
12482 full_args
= tsubst_template_args (tmpl_args
, args
,
12483 complain
, in_decl
);
12484 --processing_template_decl
;
12485 if (full_args
== error_mark_node
)
12486 return error_mark_node
;
12488 /* If this is a default template template argument,
12489 tsubst might not have changed anything. */
12490 if (full_args
== tmpl_args
)
12493 hash
= hash_tmpl_and_args (t
, full_args
);
12494 spec
= retrieve_specialization (t
, full_args
, hash
);
12495 if (spec
!= NULL_TREE
)
12499 /* Make a new template decl. It will be similar to the
12500 original, but will record the current template arguments.
12501 We also create a new function declaration, which is just
12502 like the old one, but points to this new template, rather
12503 than the old one. */
12505 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12506 DECL_CHAIN (r
) = NULL_TREE
;
12508 // Build new template info linking to the original template decl.
12509 if (!lambda_fntype
)
12511 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12512 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12515 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12517 /* The template parameters for this new template are all the
12518 template parameters for the old template, except the
12519 outermost level of parameters. */
12520 DECL_TEMPLATE_PARMS (r
)
12521 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12524 if (TREE_CODE (decl
) == TYPE_DECL
12525 && !TYPE_DECL_ALIAS_P (decl
))
12528 ++processing_template_decl
;
12529 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12530 --processing_template_decl
;
12531 if (new_type
== error_mark_node
)
12532 return error_mark_node
;
12534 TREE_TYPE (r
) = new_type
;
12535 /* For a partial specialization, we need to keep pointing to
12536 the primary template. */
12537 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12538 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12539 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12540 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12541 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12546 ++processing_template_decl
;
12547 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12548 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12550 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12551 --processing_template_decl
;
12552 if (new_decl
== error_mark_node
)
12553 return error_mark_node
;
12555 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12556 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12557 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12560 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12561 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12565 DECL_TI_TEMPLATE (new_decl
) = r
;
12566 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12570 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12571 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12573 if (PRIMARY_TEMPLATE_P (t
))
12574 DECL_PRIMARY_TEMPLATE (r
) = r
;
12576 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12578 /* Record this non-type partial instantiation. */
12579 register_specialization (r
, t
,
12580 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12586 /* True if FN is the op() for a lambda in an uninstantiated template. */
12589 lambda_fn_in_template_p (tree fn
)
12591 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12593 tree closure
= DECL_CONTEXT (fn
);
12594 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12597 /* True if FN is the op() for a lambda regenerated from a lambda in an
12598 uninstantiated template. */
12601 regenerated_lambda_fn_p (tree fn
)
12603 return (LAMBDA_FUNCTION_P (fn
)
12604 && !DECL_TEMPLATE_INSTANTIATION (fn
));
12607 /* We're instantiating a variable from template function TCTX. Return the
12608 corresponding current enclosing scope. This gets complicated because lambda
12609 functions in templates are regenerated rather than instantiated, but generic
12610 lambda functions are subsequently instantiated. */
12613 enclosing_instantiation_of (tree tctx
)
12615 tree fn
= current_function_decl
;
12616 int lambda_count
= 0;
12618 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12619 tctx
= decl_function_context (tctx
))
12621 for (; fn
; fn
= decl_function_context (fn
))
12624 int flambda_count
= 0;
12625 for (; fn
&& regenerated_lambda_fn_p (fn
);
12626 fn
= decl_function_context (fn
))
12628 if (DECL_TEMPLATE_INFO (fn
)
12629 ? most_general_template (fn
) != most_general_template (tctx
)
12635 while (flambda_count
-- > lambda_count
)
12636 fn
= decl_function_context (fn
);
12640 gcc_unreachable ();
12643 /* Substitute the ARGS into the T, which is a _DECL. Return the
12644 result of the substitution. Issue error and warning messages under
12645 control of COMPLAIN. */
12648 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12650 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12651 location_t saved_loc
;
12652 tree r
= NULL_TREE
;
12654 hashval_t hash
= 0;
12656 /* Set the filename and linenumber to improve error-reporting. */
12657 saved_loc
= input_location
;
12658 input_location
= DECL_SOURCE_LOCATION (t
);
12660 switch (TREE_CODE (t
))
12662 case TEMPLATE_DECL
:
12663 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12666 case FUNCTION_DECL
:
12667 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12672 tree type
= NULL_TREE
;
12674 tree expanded_types
= NULL_TREE
;
12675 tree prev_r
= NULL_TREE
;
12676 tree first_r
= NULL_TREE
;
12678 if (DECL_PACK_P (t
))
12680 /* If there is a local specialization that isn't a
12681 parameter pack, it means that we're doing a "simple"
12682 substitution from inside tsubst_pack_expansion. Just
12683 return the local specialization (which will be a single
12685 tree spec
= retrieve_local_specialization (t
);
12687 && TREE_CODE (spec
) == PARM_DECL
12688 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12691 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12692 the parameters in this function parameter pack. */
12693 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12694 complain
, in_decl
);
12695 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12697 len
= TREE_VEC_LENGTH (expanded_types
);
12699 /* Zero-length parameter packs are boring. Just substitute
12702 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12707 /* All we did was update the type. Make a note of that. */
12708 type
= expanded_types
;
12709 expanded_types
= NULL_TREE
;
12713 /* Loop through all of the parameters we'll build. When T is
12714 a function parameter pack, LEN is the number of expanded
12715 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12717 for (i
= 0; i
< len
; ++i
)
12721 if (DECL_TEMPLATE_PARM_P (t
))
12722 SET_DECL_TEMPLATE_PARM_P (r
);
12724 if (expanded_types
)
12725 /* We're on the Ith parameter of the function parameter
12728 /* Get the Ith type. */
12729 type
= TREE_VEC_ELT (expanded_types
, i
);
12731 /* Rename the parameter to include the index. */
12733 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12736 /* We're dealing with a normal parameter. */
12737 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12739 type
= type_decays_to (type
);
12740 TREE_TYPE (r
) = type
;
12741 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12743 if (DECL_INITIAL (r
))
12745 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12746 DECL_INITIAL (r
) = TREE_TYPE (r
);
12748 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12749 complain
, in_decl
);
12752 DECL_CONTEXT (r
) = NULL_TREE
;
12754 if (!DECL_TEMPLATE_PARM_P (r
))
12755 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12757 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12758 args
, complain
, in_decl
);
12760 /* Keep track of the first new parameter we
12761 generate. That's what will be returned to the
12766 /* Build a proper chain of parameters when substituting
12767 into a function parameter pack. */
12769 DECL_CHAIN (prev_r
) = r
;
12772 /* If cp_unevaluated_operand is set, we're just looking for a
12773 single dummy parameter, so don't keep going. */
12774 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12775 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12776 complain
, DECL_CHAIN (t
));
12778 /* FIRST_R contains the start of the chain we've built. */
12785 tree type
= NULL_TREE
;
12786 tree vec
= NULL_TREE
;
12787 tree expanded_types
= NULL_TREE
;
12790 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12792 /* This field is a lambda capture pack. Return a TREE_VEC of
12793 the expanded fields to instantiate_class_template_1 and
12794 store them in the specializations hash table as a
12795 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12796 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12797 complain
, in_decl
);
12798 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12800 len
= TREE_VEC_LENGTH (expanded_types
);
12801 vec
= make_tree_vec (len
);
12805 /* All we did was update the type. Make a note of that. */
12806 type
= expanded_types
;
12807 expanded_types
= NULL_TREE
;
12811 for (int i
= 0; i
< len
; ++i
)
12814 if (expanded_types
)
12816 type
= TREE_VEC_ELT (expanded_types
, i
);
12818 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12821 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12823 if (type
== error_mark_node
)
12824 RETURN (error_mark_node
);
12825 TREE_TYPE (r
) = type
;
12826 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12828 if (DECL_C_BIT_FIELD (r
))
12829 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12831 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12832 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12834 /*integral_constant_expression_p=*/true);
12835 if (DECL_INITIAL (t
))
12837 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12838 NSDMI in perform_member_init. Still set DECL_INITIAL
12839 so that we know there is one. */
12840 DECL_INITIAL (r
) = void_node
;
12841 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12842 retrofit_lang_decl (r
);
12843 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12845 /* We don't have to set DECL_CONTEXT here; it is set by
12846 finish_member_declaration. */
12847 DECL_CHAIN (r
) = NULL_TREE
;
12849 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12850 args
, complain
, in_decl
);
12853 TREE_VEC_ELT (vec
, i
) = r
;
12859 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12860 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12861 register_specialization (pack
, t
, args
, false, 0);
12867 /* We reach here only for member using decls. We also need to check
12868 uses_template_parms because DECL_DEPENDENT_P is not set for a
12869 using-declaration that designates a member of the current
12870 instantiation (c++/53549). */
12871 if (DECL_DEPENDENT_P (t
)
12872 || uses_template_parms (USING_DECL_SCOPE (t
)))
12874 tree scope
= USING_DECL_SCOPE (t
);
12875 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12876 if (PACK_EXPANSION_P (scope
))
12878 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12879 int len
= TREE_VEC_LENGTH (vec
);
12880 r
= make_tree_vec (len
);
12881 for (int i
= 0; i
< len
; ++i
)
12883 tree escope
= TREE_VEC_ELT (vec
, i
);
12884 tree elt
= do_class_using_decl (escope
, name
);
12887 r
= error_mark_node
;
12892 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12893 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12895 TREE_VEC_ELT (r
, i
) = elt
;
12900 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12901 complain
, in_decl
);
12902 r
= do_class_using_decl (inst_scope
, name
);
12904 r
= error_mark_node
;
12907 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12908 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12915 DECL_CHAIN (r
) = NULL_TREE
;
12922 tree argvec
= NULL_TREE
;
12923 tree gen_tmpl
= NULL_TREE
;
12925 tree tmpl
= NULL_TREE
;
12927 tree type
= NULL_TREE
;
12930 if (TREE_TYPE (t
) == error_mark_node
)
12931 RETURN (error_mark_node
);
12933 if (TREE_CODE (t
) == TYPE_DECL
12934 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12936 /* If this is the canonical decl, we don't have to
12937 mess with instantiations, and often we can't (for
12938 typename, template type parms and such). Note that
12939 TYPE_NAME is not correct for the above test if
12940 we've copied the type for a typedef. */
12941 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12942 if (type
== error_mark_node
)
12943 RETURN (error_mark_node
);
12944 r
= TYPE_NAME (type
);
12948 /* Check to see if we already have the specialization we
12951 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12953 /* T is a static data member or namespace-scope entity.
12954 We have to substitute into namespace-scope variables
12955 (not just variable templates) because of cases like:
12957 template <class T> void f() { extern T t; }
12959 where the entity referenced is not known until
12960 instantiation time. */
12962 ctx
= DECL_CONTEXT (t
);
12963 if (DECL_CLASS_SCOPE_P (t
))
12965 ctx
= tsubst_aggr_type (ctx
, args
,
12967 in_decl
, /*entering_scope=*/1);
12968 /* If CTX is unchanged, then T is in fact the
12969 specialization we want. That situation occurs when
12970 referencing a static data member within in its own
12971 class. We can use pointer equality, rather than
12972 same_type_p, because DECL_CONTEXT is always
12974 if (ctx
== DECL_CONTEXT (t
)
12975 /* ... unless T is a member template; in which
12976 case our caller can be willing to create a
12977 specialization of that template represented
12979 && !(DECL_TI_TEMPLATE (t
)
12980 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12986 tmpl
= DECL_TI_TEMPLATE (t
);
12987 gen_tmpl
= most_general_template (tmpl
);
12988 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12989 if (argvec
!= error_mark_node
)
12990 argvec
= (coerce_innermost_template_parms
12991 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12992 argvec
, t
, complain
,
12993 /*all*/true, /*defarg*/true));
12994 if (argvec
== error_mark_node
)
12995 RETURN (error_mark_node
);
12996 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12997 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13002 /* A local variable. */
13004 /* Subsequent calls to pushdecl will fill this in. */
13006 /* Unless this is a reference to a static variable from an
13007 enclosing function, in which case we need to fill it in now. */
13008 if (TREE_STATIC (t
))
13010 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13011 if (fn
!= current_function_decl
)
13014 spec
= retrieve_local_specialization (t
);
13016 /* If we already have the specialization we need, there is
13017 nothing more to do. */
13024 /* Create a new node for the specialization we need. */
13026 if (type
== NULL_TREE
)
13028 if (is_typedef_decl (t
))
13029 type
= DECL_ORIGINAL_TYPE (t
);
13031 type
= TREE_TYPE (t
);
13033 && VAR_HAD_UNKNOWN_BOUND (t
)
13034 && type
!= error_mark_node
)
13035 type
= strip_array_domain (type
);
13036 tree sub_args
= args
;
13037 if (tree auto_node
= type_uses_auto (type
))
13039 /* Mask off any template args past the variable's context so we
13040 don't replace the auto with an unrelated argument. */
13041 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13042 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13044 /* This should never happen with the new lambda instantiation
13045 model, but keep the handling just in case. */
13046 gcc_assert (!CHECKING_P
),
13047 sub_args
= strip_innermost_template_args (args
, extra
);
13049 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13053 /* Even if the original location is out of scope, the
13054 newly substituted one is not. */
13055 DECL_DEAD_FOR_LOCAL (r
) = 0;
13056 DECL_INITIALIZED_P (r
) = 0;
13057 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13058 if (type
== error_mark_node
)
13059 RETURN (error_mark_node
);
13060 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13062 /* It may seem that this case cannot occur, since:
13067 declares a function, not a variable. However:
13070 template <typename T> void g() { T t; }
13071 template void g<f>();
13073 is an attempt to declare a variable with function
13075 error ("variable %qD has function type",
13076 /* R is not yet sufficiently initialized, so we
13077 just use its name. */
13079 RETURN (error_mark_node
);
13081 type
= complete_type (type
);
13082 /* Wait until cp_finish_decl to set this again, to handle
13083 circular dependency (template/instantiate6.C). */
13084 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13085 type
= check_var_type (DECL_NAME (r
), type
);
13087 if (DECL_HAS_VALUE_EXPR_P (t
))
13089 tree ve
= DECL_VALUE_EXPR (t
);
13090 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13091 /*constant_expression_p=*/false);
13092 if (REFERENCE_REF_P (ve
))
13094 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13095 ve
= TREE_OPERAND (ve
, 0);
13097 SET_DECL_VALUE_EXPR (r
, ve
);
13099 if (CP_DECL_THREAD_LOCAL_P (r
)
13100 && !processing_template_decl
)
13101 set_decl_tls_model (r
, decl_default_tls_model (r
));
13103 else if (DECL_SELF_REFERENCE_P (t
))
13104 SET_DECL_SELF_REFERENCE_P (r
);
13105 TREE_TYPE (r
) = type
;
13106 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13107 DECL_CONTEXT (r
) = ctx
;
13108 /* Clear out the mangled name and RTL for the instantiation. */
13109 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13110 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13111 SET_DECL_RTL (r
, NULL
);
13112 /* The initializer must not be expanded until it is required;
13113 see [temp.inst]. */
13114 DECL_INITIAL (r
) = NULL_TREE
;
13115 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13118 if (DECL_LANG_SPECIFIC (r
))
13119 SET_DECL_DEPENDENT_INIT_P (r
, false);
13121 SET_DECL_MODE (r
, VOIDmode
);
13123 /* Possibly limit visibility based on template args. */
13124 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13125 if (DECL_VISIBILITY_SPECIFIED (t
))
13127 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13128 DECL_ATTRIBUTES (r
)
13129 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13131 determine_visibility (r
);
13136 /* A static data member declaration is always marked
13137 external when it is declared in-class, even if an
13138 initializer is present. We mimic the non-template
13139 processing here. */
13140 DECL_EXTERNAL (r
) = 1;
13141 if (DECL_NAMESPACE_SCOPE_P (t
))
13142 DECL_NOT_REALLY_EXTERN (r
) = 1;
13144 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13145 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13146 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13150 if (DECL_LANG_SPECIFIC (r
))
13151 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13152 if (!cp_unevaluated_operand
)
13153 register_local_specialization (r
, t
);
13156 DECL_CHAIN (r
) = NULL_TREE
;
13158 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13160 args
, complain
, in_decl
);
13162 /* Preserve a typedef that names a type. */
13163 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13165 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13166 set_underlying_type (r
);
13167 if (TYPE_DECL_ALIAS_P (r
))
13168 /* An alias template specialization can be dependent
13169 even if its underlying type is not. */
13170 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13173 layout_decl (r
, 0);
13178 gcc_unreachable ();
13183 /* Restore the file and line information. */
13184 input_location
= saved_loc
;
13189 /* Substitute into the ARG_TYPES of a function type.
13190 If END is a TREE_CHAIN, leave it and any following types
13194 tsubst_arg_types (tree arg_types
,
13197 tsubst_flags_t complain
,
13200 tree remaining_arg_types
;
13201 tree type
= NULL_TREE
;
13203 tree expanded_args
= NULL_TREE
;
13206 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13209 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13210 args
, end
, complain
, in_decl
);
13211 if (remaining_arg_types
== error_mark_node
)
13212 return error_mark_node
;
13214 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13216 /* For a pack expansion, perform substitution on the
13217 entire expression. Later on, we'll handle the arguments
13219 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13220 args
, complain
, in_decl
);
13222 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13223 /* So that we'll spin through the parameters, one by one. */
13224 i
= TREE_VEC_LENGTH (expanded_args
);
13227 /* We only partially substituted into the parameter
13228 pack. Our type is TYPE_PACK_EXPANSION. */
13229 type
= expanded_args
;
13230 expanded_args
= NULL_TREE
;
13238 type
= TREE_VEC_ELT (expanded_args
, i
);
13240 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13242 if (type
== error_mark_node
)
13243 return error_mark_node
;
13244 if (VOID_TYPE_P (type
))
13246 if (complain
& tf_error
)
13248 error ("invalid parameter type %qT", type
);
13250 error ("in declaration %q+D", in_decl
);
13252 return error_mark_node
;
13255 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13256 return error_mark_node
;
13258 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13259 top-level qualifiers as required. */
13260 type
= cv_unqualified (type_decays_to (type
));
13262 /* We do not substitute into default arguments here. The standard
13263 mandates that they be instantiated only when needed, which is
13264 done in build_over_call. */
13265 default_arg
= TREE_PURPOSE (arg_types
);
13267 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13268 since the new op() won't have any associated template arguments for us
13269 to refer to later. */
13270 if (lambda_fn_in_template_p (in_decl
))
13271 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13272 false/*fn*/, false/*constexpr*/);
13274 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13276 /* We've instantiated a template before its default arguments
13277 have been parsed. This can happen for a nested template
13278 class, and is not an error unless we require the default
13279 argument in a call of this function. */
13280 remaining_arg_types
=
13281 tree_cons (default_arg
, type
, remaining_arg_types
);
13282 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13285 remaining_arg_types
=
13286 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13289 return remaining_arg_types
;
13292 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13293 *not* handle the exception-specification for FNTYPE, because the
13294 initial substitution of explicitly provided template parameters
13295 during argument deduction forbids substitution into the
13296 exception-specification:
13300 All references in the function type of the function template to the
13301 corresponding template parameters are replaced by the specified tem-
13302 plate argument values. If a substitution in a template parameter or
13303 in the function type of the function template results in an invalid
13304 type, type deduction fails. [Note: The equivalent substitution in
13305 exception specifications is done only when the function is instanti-
13306 ated, at which point a program is ill-formed if the substitution
13307 results in an invalid type.] */
13310 tsubst_function_type (tree t
,
13312 tsubst_flags_t complain
,
13316 tree arg_types
= NULL_TREE
;
13319 /* The TYPE_CONTEXT is not used for function/method types. */
13320 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13322 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13324 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13326 if (late_return_type_p
)
13328 /* Substitute the argument types. */
13329 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13330 complain
, in_decl
);
13331 if (arg_types
== error_mark_node
)
13332 return error_mark_node
;
13334 tree save_ccp
= current_class_ptr
;
13335 tree save_ccr
= current_class_ref
;
13336 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13337 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13338 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13341 /* DR 1207: 'this' is in scope in the trailing return type. */
13342 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13345 /* Substitute the return type. */
13346 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13350 current_class_ptr
= save_ccp
;
13351 current_class_ref
= save_ccr
;
13355 /* Substitute the return type. */
13356 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13358 if (return_type
== error_mark_node
)
13359 return error_mark_node
;
13360 /* DR 486 clarifies that creation of a function type with an
13361 invalid return type is a deduction failure. */
13362 if (TREE_CODE (return_type
) == ARRAY_TYPE
13363 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13365 if (complain
& tf_error
)
13367 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13368 error ("function returning an array");
13370 error ("function returning a function");
13372 return error_mark_node
;
13375 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13376 return error_mark_node
;
13378 if (!late_return_type_p
)
13380 /* Substitute the argument types. */
13381 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13382 complain
, in_decl
);
13383 if (arg_types
== error_mark_node
)
13384 return error_mark_node
;
13387 /* Construct a new type node and return it. */
13388 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13390 fntype
= build_function_type (return_type
, arg_types
);
13391 fntype
= apply_memfn_quals (fntype
,
13392 type_memfn_quals (t
),
13393 type_memfn_rqual (t
));
13397 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13398 /* Don't pick up extra function qualifiers from the basetype. */
13399 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13400 if (! MAYBE_CLASS_TYPE_P (r
))
13404 Type deduction may fail for any of the following
13407 -- Attempting to create "pointer to member of T" when T
13408 is not a class type. */
13409 if (complain
& tf_error
)
13410 error ("creating pointer to member function of non-class type %qT",
13412 return error_mark_node
;
13415 fntype
= build_method_type_directly (r
, return_type
,
13416 TREE_CHAIN (arg_types
));
13417 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13419 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13421 if (late_return_type_p
)
13422 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13427 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13428 ARGS into that specification, and return the substituted
13429 specification. If there is no specification, return NULL_TREE. */
13432 tsubst_exception_specification (tree fntype
,
13434 tsubst_flags_t complain
,
13441 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13442 new_specs
= NULL_TREE
;
13443 if (specs
&& TREE_PURPOSE (specs
))
13445 /* A noexcept-specifier. */
13446 tree expr
= TREE_PURPOSE (specs
);
13447 if (TREE_CODE (expr
) == INTEGER_CST
)
13451 /* Defer instantiation of noexcept-specifiers to avoid
13452 excessive instantiations (c++/49107). */
13453 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13454 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13456 /* We already partially instantiated this member template,
13457 so combine the new args with the old. */
13458 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13459 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13460 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13461 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13465 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13466 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13470 new_specs
= tsubst_copy_and_build
13471 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13472 /*integral_constant_expression_p=*/true);
13473 new_specs
= build_noexcept_spec (new_specs
, complain
);
13477 if (! TREE_VALUE (specs
))
13484 tree expanded_specs
= NULL_TREE
;
13486 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13488 /* Expand the pack expansion type. */
13489 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13493 if (expanded_specs
== error_mark_node
)
13494 return error_mark_node
;
13495 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13496 len
= TREE_VEC_LENGTH (expanded_specs
);
13499 /* We're substituting into a member template, so
13500 we got a TYPE_PACK_EXPANSION back. Add that
13501 expansion and move on. */
13502 gcc_assert (TREE_CODE (expanded_specs
)
13503 == TYPE_PACK_EXPANSION
);
13504 new_specs
= add_exception_specifier (new_specs
,
13507 specs
= TREE_CHAIN (specs
);
13512 for (i
= 0; i
< len
; ++i
)
13514 if (expanded_specs
)
13515 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13517 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13518 if (spec
== error_mark_node
)
13520 new_specs
= add_exception_specifier (new_specs
, spec
,
13524 specs
= TREE_CHAIN (specs
);
13530 /* Take the tree structure T and replace template parameters used
13531 therein with the argument vector ARGS. IN_DECL is an associated
13532 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13533 Issue error and warning messages under control of COMPLAIN. Note
13534 that we must be relatively non-tolerant of extensions here, in
13535 order to preserve conformance; if we allow substitutions that
13536 should not be allowed, we may allow argument deductions that should
13537 not succeed, and therefore report ambiguous overload situations
13538 where there are none. In theory, we could allow the substitution,
13539 but indicate that it should have failed, and allow our caller to
13540 make sure that the right thing happens, but we don't try to do this
13543 This function is used for dealing with types, decls and the like;
13544 for expressions, use tsubst_expr or tsubst_copy. */
13547 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13549 enum tree_code code
;
13550 tree type
, r
= NULL_TREE
;
13552 if (t
== NULL_TREE
|| t
== error_mark_node
13553 || t
== integer_type_node
13554 || t
== void_type_node
13555 || t
== char_type_node
13556 || t
== unknown_type_node
13557 || TREE_CODE (t
) == NAMESPACE_DECL
13558 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13562 return tsubst_decl (t
, args
, complain
);
13564 if (args
== NULL_TREE
)
13567 code
= TREE_CODE (t
);
13569 if (code
== IDENTIFIER_NODE
)
13570 type
= IDENTIFIER_TYPE_VALUE (t
);
13572 type
= TREE_TYPE (t
);
13574 gcc_assert (type
!= unknown_type_node
);
13576 /* Reuse typedefs. We need to do this to handle dependent attributes,
13577 such as attribute aligned. */
13579 && typedef_variant_p (t
))
13581 tree decl
= TYPE_NAME (t
);
13583 if (alias_template_specialization_p (t
))
13585 /* DECL represents an alias template and we want to
13587 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13588 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13589 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13591 else if (DECL_CLASS_SCOPE_P (decl
)
13592 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13593 && uses_template_parms (DECL_CONTEXT (decl
)))
13595 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13596 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13597 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13599 else if (DECL_FUNCTION_SCOPE_P (decl
)
13600 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13601 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13602 r
= retrieve_local_specialization (decl
);
13604 /* The typedef is from a non-template context. */
13610 r
= cp_build_qualified_type_real
13611 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13612 complain
| tf_ignore_bad_quals
);
13617 /* We don't have an instantiation yet, so drop the typedef. */
13618 int quals
= cp_type_quals (t
);
13619 t
= DECL_ORIGINAL_TYPE (decl
);
13620 t
= cp_build_qualified_type_real (t
, quals
,
13621 complain
| tf_ignore_bad_quals
);
13625 bool fndecl_type
= (complain
& tf_fndecl_type
);
13626 complain
&= ~tf_fndecl_type
;
13629 && code
!= TYPENAME_TYPE
13630 && code
!= TEMPLATE_TYPE_PARM
13631 && code
!= TEMPLATE_PARM_INDEX
13632 && code
!= IDENTIFIER_NODE
13633 && code
!= FUNCTION_TYPE
13634 && code
!= METHOD_TYPE
)
13635 type
= tsubst (type
, args
, complain
, in_decl
);
13636 if (type
== error_mark_node
)
13637 return error_mark_node
;
13643 case ENUMERAL_TYPE
:
13644 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13645 /*entering_scope=*/0);
13648 case IDENTIFIER_NODE
:
13659 if (t
== integer_type_node
)
13662 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13663 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13667 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13669 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13670 /*integral_constant_expression_p=*/false);
13672 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13674 if (TREE_CODE (max
) == NOP_EXPR
13675 && TREE_SIDE_EFFECTS (omax
)
13676 && !TREE_TYPE (max
))
13677 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13679 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13680 with TREE_SIDE_EFFECTS that indicates this is not an integral
13681 constant expression. */
13682 if (processing_template_decl
13683 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13685 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13686 TREE_SIDE_EFFECTS (max
) = 1;
13689 return compute_array_index_type (NULL_TREE
, max
, complain
);
13692 case TEMPLATE_TYPE_PARM
:
13693 case TEMPLATE_TEMPLATE_PARM
:
13694 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13695 case TEMPLATE_PARM_INDEX
:
13700 tree arg
= NULL_TREE
;
13702 /* Early in template argument deduction substitution, we don't
13703 want to reduce the level of 'auto', or it will be confused
13704 with a normal template parm in subsequent deduction. */
13705 if (is_auto (t
) && (complain
& tf_partial
))
13710 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13711 template_parm_level_and_index (t
, &level
, &idx
);
13713 levels
= TMPL_ARGS_DEPTH (args
);
13714 if (level
<= levels
13715 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13717 arg
= TMPL_ARG (args
, level
, idx
);
13719 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13721 /* See through ARGUMENT_PACK_SELECT arguments. */
13722 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13723 /* If the selected argument is an expansion E, that most
13724 likely means we were called from
13725 gen_elem_of_pack_expansion_instantiation during the
13726 substituting of pack an argument pack (which Ith
13727 element is a pack expansion, where I is
13728 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13729 In this case, the Ith element resulting from this
13730 substituting is going to be a pack expansion, which
13731 pattern is the pattern of E. Let's return the
13733 gen_elem_of_pack_expansion_instantiation will
13734 build the resulting pack expansion from it. */
13735 if (PACK_EXPANSION_P (arg
))
13737 /* Make sure we aren't throwing away arg info. */
13738 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13739 arg
= PACK_EXPANSION_PATTERN (arg
);
13744 if (arg
== error_mark_node
)
13745 return error_mark_node
;
13746 else if (arg
!= NULL_TREE
)
13748 if (ARGUMENT_PACK_P (arg
))
13749 /* If ARG is an argument pack, we don't actually want to
13750 perform a substitution here, because substitutions
13751 for argument packs are only done
13752 element-by-element. We can get to this point when
13753 substituting the type of a non-type template
13754 parameter pack, when that type actually contains
13755 template parameter packs from an outer template, e.g.,
13757 template<typename... Types> struct A {
13758 template<Types... Values> struct B { };
13762 if (code
== TEMPLATE_TYPE_PARM
)
13765 gcc_assert (TYPE_P (arg
));
13767 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13769 return cp_build_qualified_type_real
13770 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13772 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13774 /* We are processing a type constructed from a
13775 template template parameter. */
13776 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13777 args
, complain
, in_decl
);
13778 if (argvec
== error_mark_node
)
13779 return error_mark_node
;
13781 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13782 || TREE_CODE (arg
) == TEMPLATE_DECL
13783 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13785 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13786 /* Consider this code:
13788 template <template <class> class Template>
13790 template <class Arg> using Bind = Template<Arg>;
13793 template <template <class> class Template, class Arg>
13794 using Instantiate = Template<Arg>; //#0
13796 template <template <class> class Template,
13799 Instantiate<Internal<Template>::template Bind,
13802 When #1 is parsed, the
13803 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13804 parameter `Template' in #0 matches the
13805 UNBOUND_CLASS_TEMPLATE representing the argument
13806 `Internal<Template>::template Bind'; We then want
13807 to assemble the type `Bind<Argument>' that can't
13808 be fully created right now, because
13809 `Internal<Template>' not being complete, the Bind
13810 template cannot be looked up in that context. So
13811 we need to "store" `Bind<Argument>' for later
13812 when the context of Bind becomes complete. Let's
13813 store that in a TYPENAME_TYPE. */
13814 return make_typename_type (TYPE_CONTEXT (arg
),
13815 build_nt (TEMPLATE_ID_EXPR
,
13816 TYPE_IDENTIFIER (arg
),
13821 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13822 are resolving nested-types in the signature of a
13823 member function templates. Otherwise ARG is a
13824 TEMPLATE_DECL and is the real template to be
13826 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13827 arg
= TYPE_NAME (arg
);
13829 r
= lookup_template_class (arg
,
13831 DECL_CONTEXT (arg
),
13832 /*entering_scope=*/0,
13834 return cp_build_qualified_type_real
13835 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13837 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13840 /* TEMPLATE_PARM_INDEX. */
13841 return convert_from_reference (unshare_expr (arg
));
13845 /* This can happen during the attempted tsubst'ing in
13846 unify. This means that we don't yet have any information
13847 about the template parameter in question. */
13850 /* If we get here, we must have been looking at a parm for a
13851 more deeply nested template. Make a new version of this
13852 template parameter, but with a lower level. */
13855 case TEMPLATE_TYPE_PARM
:
13856 case TEMPLATE_TEMPLATE_PARM
:
13857 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13858 if (cp_type_quals (t
))
13860 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13861 r
= cp_build_qualified_type_real
13862 (r
, cp_type_quals (t
),
13863 complain
| (code
== TEMPLATE_TYPE_PARM
13864 ? tf_ignore_bad_quals
: 0));
13866 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13867 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13868 && (r
= (TEMPLATE_PARM_DESCENDANTS
13869 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13870 && (r
= TREE_TYPE (r
))
13871 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13872 /* Break infinite recursion when substituting the constraints
13873 of a constrained placeholder. */;
13877 TEMPLATE_TYPE_PARM_INDEX (r
)
13878 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13879 r
, levels
, args
, complain
);
13880 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13881 TYPE_MAIN_VARIANT (r
) = r
;
13882 TYPE_POINTER_TO (r
) = NULL_TREE
;
13883 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13885 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13887 /* Propagate constraints on placeholders. */
13888 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13889 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13890 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13891 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13893 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13894 pl
= tsubst (pl
, args
, complain
, in_decl
);
13895 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13899 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13900 /* We have reduced the level of the template
13901 template parameter, but not the levels of its
13902 template parameters, so canonical_type_parameter
13903 will not be able to find the canonical template
13904 template parameter for this level. Thus, we
13905 require structural equality checking to compare
13906 TEMPLATE_TEMPLATE_PARMs. */
13907 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13908 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13909 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13911 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13913 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13915 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13916 /* We might need to substitute into the types of non-type
13917 template parameters. */
13918 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13919 complain
, in_decl
);
13920 if (tmpl
== error_mark_node
)
13921 return error_mark_node
;
13922 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13923 complain
, in_decl
);
13924 if (argvec
== error_mark_node
)
13925 return error_mark_node
;
13927 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13928 = build_template_info (tmpl
, argvec
);
13933 case TEMPLATE_PARM_INDEX
:
13934 /* OK, now substitute the type of the non-type parameter. We
13935 couldn't do it earlier because it might be an auto parameter,
13936 and we wouldn't need to if we had an argument. */
13937 type
= tsubst (type
, args
, complain
, in_decl
);
13938 if (type
== error_mark_node
)
13939 return error_mark_node
;
13940 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13944 gcc_unreachable ();
13952 tree purpose
, value
, chain
;
13954 if (t
== void_list_node
)
13957 purpose
= TREE_PURPOSE (t
);
13960 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13961 if (purpose
== error_mark_node
)
13962 return error_mark_node
;
13964 value
= TREE_VALUE (t
);
13967 value
= tsubst (value
, args
, complain
, in_decl
);
13968 if (value
== error_mark_node
)
13969 return error_mark_node
;
13971 chain
= TREE_CHAIN (t
);
13972 if (chain
&& chain
!= void_type_node
)
13974 chain
= tsubst (chain
, args
, complain
, in_decl
);
13975 if (chain
== error_mark_node
)
13976 return error_mark_node
;
13978 if (purpose
== TREE_PURPOSE (t
)
13979 && value
== TREE_VALUE (t
)
13980 && chain
== TREE_CHAIN (t
))
13982 return hash_tree_cons (purpose
, value
, chain
);
13986 /* We should never be tsubsting a binfo. */
13987 gcc_unreachable ();
13990 /* A vector of template arguments. */
13991 gcc_assert (!type
);
13992 return tsubst_template_args (t
, args
, complain
, in_decl
);
13995 case REFERENCE_TYPE
:
13997 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14002 Type deduction may fail for any of the following
14005 -- Attempting to create a pointer to reference type.
14006 -- Attempting to create a reference to a reference type or
14007 a reference to void.
14009 Core issue 106 says that creating a reference to a reference
14010 during instantiation is no longer a cause for failure. We
14011 only enforce this check in strict C++98 mode. */
14012 if ((TREE_CODE (type
) == REFERENCE_TYPE
14013 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14014 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14016 static location_t last_loc
;
14018 /* We keep track of the last time we issued this error
14019 message to avoid spewing a ton of messages during a
14020 single bad template instantiation. */
14021 if (complain
& tf_error
14022 && last_loc
!= input_location
)
14024 if (VOID_TYPE_P (type
))
14025 error ("forming reference to void");
14026 else if (code
== POINTER_TYPE
)
14027 error ("forming pointer to reference type %qT", type
);
14029 error ("forming reference to reference type %qT", type
);
14030 last_loc
= input_location
;
14033 return error_mark_node
;
14035 else if (TREE_CODE (type
) == FUNCTION_TYPE
14036 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14037 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14039 if (complain
& tf_error
)
14041 if (code
== POINTER_TYPE
)
14042 error ("forming pointer to qualified function type %qT",
14045 error ("forming reference to qualified function type %qT",
14048 return error_mark_node
;
14050 else if (code
== POINTER_TYPE
)
14052 r
= build_pointer_type (type
);
14053 if (TREE_CODE (type
) == METHOD_TYPE
)
14054 r
= build_ptrmemfunc_type (r
);
14056 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14057 /* In C++0x, during template argument substitution, when there is an
14058 attempt to create a reference to a reference type, reference
14059 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14061 "If a template-argument for a template-parameter T names a type
14062 that is a reference to a type A, an attempt to create the type
14063 'lvalue reference to cv T' creates the type 'lvalue reference to
14064 A,' while an attempt to create the type type rvalue reference to
14065 cv T' creates the type T"
14067 r
= cp_build_reference_type
14069 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14071 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14072 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14074 if (r
!= error_mark_node
)
14075 /* Will this ever be needed for TYPE_..._TO values? */
14082 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14083 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14087 Type deduction may fail for any of the following
14090 -- Attempting to create "pointer to member of T" when T
14091 is not a class type. */
14092 if (complain
& tf_error
)
14093 error ("creating pointer to member of non-class type %qT", r
);
14094 return error_mark_node
;
14096 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14098 if (complain
& tf_error
)
14099 error ("creating pointer to member reference type %qT", type
);
14100 return error_mark_node
;
14102 if (VOID_TYPE_P (type
))
14104 if (complain
& tf_error
)
14105 error ("creating pointer to member of type void");
14106 return error_mark_node
;
14108 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14109 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14111 /* The type of the implicit object parameter gets its
14112 cv-qualifiers from the FUNCTION_TYPE. */
14115 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14116 type_memfn_rqual (type
));
14117 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14118 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14122 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14126 case FUNCTION_TYPE
:
14131 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14132 if (fntype
== error_mark_node
)
14133 return error_mark_node
;
14135 /* Substitute the exception specification. */
14136 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14137 /*defer_ok*/fndecl_type
);
14138 if (specs
== error_mark_node
)
14139 return error_mark_node
;
14141 fntype
= build_exception_variant (fntype
, specs
);
14146 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14147 if (domain
== error_mark_node
)
14148 return error_mark_node
;
14150 /* As an optimization, we avoid regenerating the array type if
14151 it will obviously be the same as T. */
14152 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14155 /* These checks should match the ones in create_array_type_for_decl.
14159 The deduction may fail for any of the following reasons:
14161 -- Attempting to create an array with an element type that
14162 is void, a function type, or a reference type, or [DR337]
14163 an abstract class type. */
14164 if (VOID_TYPE_P (type
)
14165 || TREE_CODE (type
) == FUNCTION_TYPE
14166 || (TREE_CODE (type
) == ARRAY_TYPE
14167 && TYPE_DOMAIN (type
) == NULL_TREE
)
14168 || TREE_CODE (type
) == REFERENCE_TYPE
)
14170 if (complain
& tf_error
)
14171 error ("creating array of %qT", type
);
14172 return error_mark_node
;
14175 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14176 return error_mark_node
;
14178 r
= build_cplus_array_type (type
, domain
);
14180 if (TYPE_USER_ALIGN (t
))
14182 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14183 TYPE_USER_ALIGN (r
) = 1;
14189 case TYPENAME_TYPE
:
14191 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14192 in_decl
, /*entering_scope=*/1);
14193 if (ctx
== error_mark_node
)
14194 return error_mark_node
;
14196 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14197 complain
, in_decl
);
14198 if (f
== error_mark_node
)
14199 return error_mark_node
;
14201 if (!MAYBE_CLASS_TYPE_P (ctx
))
14203 if (complain
& tf_error
)
14204 error ("%qT is not a class, struct, or union type", ctx
);
14205 return error_mark_node
;
14207 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14209 /* Normally, make_typename_type does not require that the CTX
14210 have complete type in order to allow things like:
14212 template <class T> struct S { typename S<T>::X Y; };
14214 But, such constructs have already been resolved by this
14215 point, so here CTX really should have complete type, unless
14216 it's a partial instantiation. */
14217 ctx
= complete_type (ctx
);
14218 if (!COMPLETE_TYPE_P (ctx
))
14220 if (complain
& tf_error
)
14221 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14222 return error_mark_node
;
14226 f
= make_typename_type (ctx
, f
, typename_type
,
14227 complain
| tf_keep_type_decl
);
14228 if (f
== error_mark_node
)
14230 if (TREE_CODE (f
) == TYPE_DECL
)
14232 complain
|= tf_ignore_bad_quals
;
14236 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14238 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14240 if (complain
& tf_error
)
14241 error ("%qT resolves to %qT, which is not an enumeration type",
14244 return error_mark_node
;
14246 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14248 if (complain
& tf_error
)
14249 error ("%qT resolves to %qT, which is is not a class type",
14252 return error_mark_node
;
14256 return cp_build_qualified_type_real
14257 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14260 case UNBOUND_CLASS_TEMPLATE
:
14262 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14263 in_decl
, /*entering_scope=*/1);
14264 tree name
= TYPE_IDENTIFIER (t
);
14265 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14267 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14268 return error_mark_node
;
14271 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14272 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14279 ++cp_unevaluated_operand
;
14280 ++c_inhibit_evaluation_warnings
;
14282 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14284 /*integral_constant_expression_p=*/false);
14286 --cp_unevaluated_operand
;
14287 --c_inhibit_evaluation_warnings
;
14289 type
= finish_typeof (type
);
14290 return cp_build_qualified_type_real (type
,
14292 | cp_type_quals (type
),
14296 case DECLTYPE_TYPE
:
14300 ++cp_unevaluated_operand
;
14301 ++c_inhibit_evaluation_warnings
;
14303 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14304 complain
|tf_decltype
, in_decl
,
14305 /*function_p*/false,
14306 /*integral_constant_expression*/false);
14308 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14310 if (type
== NULL_TREE
)
14312 if (complain
& tf_error
)
14313 error ("empty initializer in lambda init-capture");
14314 type
= error_mark_node
;
14316 else if (TREE_CODE (type
) == TREE_LIST
)
14317 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14320 --cp_unevaluated_operand
;
14321 --c_inhibit_evaluation_warnings
;
14323 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14324 type
= lambda_capture_field_type (type
,
14325 DECLTYPE_FOR_INIT_CAPTURE (t
),
14326 DECLTYPE_FOR_REF_CAPTURE (t
));
14327 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14328 type
= lambda_proxy_type (type
);
14331 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14332 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14334 /* In a template ~id could be either a complement expression
14335 or an unqualified-id naming a destructor; if instantiating
14336 it produces an expression, it's not an id-expression or
14339 type
= finish_decltype_type (type
, id
, complain
);
14341 return cp_build_qualified_type_real (type
,
14343 | cp_type_quals (type
),
14344 complain
| tf_ignore_bad_quals
);
14347 case UNDERLYING_TYPE
:
14349 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14350 complain
, in_decl
);
14351 return finish_underlying_type (type
);
14354 case TYPE_ARGUMENT_PACK
:
14355 case NONTYPE_ARGUMENT_PACK
:
14359 if (code
== NONTYPE_ARGUMENT_PACK
)
14360 r
= make_node (code
);
14362 r
= cxx_make_type (code
);
14364 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14365 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14366 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14384 /* We should use one of the expression tsubsts for these codes. */
14385 gcc_unreachable ();
14388 sorry ("use of %qs in template", get_tree_code_name (code
));
14389 return error_mark_node
;
14393 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14394 expression on the left-hand side of the "." or "->" operator. We
14395 only do the lookup if we had a dependent BASELINK. Otherwise we
14396 adjust it onto the instantiated heirarchy. */
14399 tsubst_baselink (tree baselink
, tree object_type
,
14400 tree args
, tsubst_flags_t complain
, tree in_decl
)
14402 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14403 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14404 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14406 tree optype
= BASELINK_OPTYPE (baselink
);
14407 optype
= tsubst (optype
, args
, complain
, in_decl
);
14409 tree template_args
= NULL_TREE
;
14410 bool template_id_p
= false;
14411 tree fns
= BASELINK_FUNCTIONS (baselink
);
14412 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14414 template_id_p
= true;
14415 template_args
= TREE_OPERAND (fns
, 1);
14416 fns
= TREE_OPERAND (fns
, 0);
14418 template_args
= tsubst_template_args (template_args
, args
,
14419 complain
, in_decl
);
14422 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14423 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14424 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14428 tree name
= OVL_NAME (fns
);
14429 if (IDENTIFIER_CONV_OP_P (name
))
14430 name
= make_conv_op_name (optype
);
14432 if (name
== complete_dtor_identifier
)
14433 /* Treat as-if non-dependent below. */
14434 dependent_p
= false;
14436 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14439 if ((complain
& tf_error
)
14440 && constructor_name_p (name
, qualifying_scope
))
14441 error ("cannot call constructor %<%T::%D%> directly",
14442 qualifying_scope
, name
);
14443 return error_mark_node
;
14446 if (BASELINK_P (baselink
))
14447 fns
= BASELINK_FUNCTIONS (baselink
);
14451 gcc_assert (optype
== BASELINK_OPTYPE (baselink
));
14452 /* We're going to overwrite pieces below, make a duplicate. */
14453 baselink
= copy_node (baselink
);
14456 /* If lookup found a single function, mark it as used at this point.
14457 (If lookup found multiple functions the one selected later by
14458 overload resolution will be marked as used at that point.) */
14459 if (!template_id_p
&& !really_overloaded_fn (fns
)
14460 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14461 return error_mark_node
;
14463 if (BASELINK_P (baselink
))
14465 /* Add back the template arguments, if present. */
14467 BASELINK_FUNCTIONS (baselink
)
14468 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14470 /* Update the conversion operator type. */
14471 BASELINK_OPTYPE (baselink
) = optype
;
14475 object_type
= current_class_type
;
14477 if (qualified_p
|| !dependent_p
)
14479 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14483 /* We need to call adjust_result_of_qualified_name_lookup in case the
14484 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14485 so that we still get virtual function binding. */
14486 BASELINK_QUALIFIED_P (baselink
) = false;
14492 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14493 true if the qualified-id will be a postfix-expression in-and-of
14494 itself; false if more of the postfix-expression follows the
14495 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14499 tsubst_qualified_id (tree qualified_id
, tree args
,
14500 tsubst_flags_t complain
, tree in_decl
,
14501 bool done
, bool address_p
)
14507 tree template_args
;
14508 location_t loc
= UNKNOWN_LOCATION
;
14510 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14512 /* Figure out what name to look up. */
14513 name
= TREE_OPERAND (qualified_id
, 1);
14514 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14516 is_template
= true;
14517 loc
= EXPR_LOCATION (name
);
14518 template_args
= TREE_OPERAND (name
, 1);
14520 template_args
= tsubst_template_args (template_args
, args
,
14521 complain
, in_decl
);
14522 if (template_args
== error_mark_node
)
14523 return error_mark_node
;
14524 name
= TREE_OPERAND (name
, 0);
14528 is_template
= false;
14529 template_args
= NULL_TREE
;
14532 /* Substitute into the qualifying scope. When there are no ARGS, we
14533 are just trying to simplify a non-dependent expression. In that
14534 case the qualifying scope may be dependent, and, in any case,
14535 substituting will not help. */
14536 scope
= TREE_OPERAND (qualified_id
, 0);
14539 scope
= tsubst (scope
, args
, complain
, in_decl
);
14540 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14545 if (dependent_scope_p (scope
))
14548 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14549 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14550 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14551 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14555 if (!BASELINK_P (name
) && !DECL_P (expr
))
14557 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14559 /* A BIT_NOT_EXPR is used to represent a destructor. */
14560 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14562 error ("qualifying type %qT does not match destructor name ~%qT",
14563 scope
, TREE_OPERAND (expr
, 0));
14564 expr
= error_mark_node
;
14567 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14568 /*is_type_p=*/0, false);
14571 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14572 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14573 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14575 if (complain
& tf_error
)
14577 error ("dependent-name %qE is parsed as a non-type, but "
14578 "instantiation yields a type", qualified_id
);
14579 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14581 return error_mark_node
;
14587 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14589 /* Remember that there was a reference to this entity. */
14590 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14591 return error_mark_node
;
14594 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14596 if (complain
& tf_error
)
14597 qualified_name_lookup_error (scope
,
14598 TREE_OPERAND (qualified_id
, 1),
14599 expr
, input_location
);
14600 return error_mark_node
;
14605 if (variable_template_p (expr
))
14606 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14609 expr
= lookup_template_function (expr
, template_args
);
14612 if (expr
== error_mark_node
&& complain
& tf_error
)
14613 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14614 expr
, input_location
);
14615 else if (TYPE_P (scope
))
14617 expr
= (adjust_result_of_qualified_name_lookup
14618 (expr
, scope
, current_nonlambda_class_type ()));
14619 expr
= (finish_qualified_id_expr
14620 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14621 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14622 /*template_arg_p=*/false, complain
));
14625 /* Expressions do not generally have reference type. */
14626 if (TREE_CODE (expr
) != SCOPE_REF
14627 /* However, if we're about to form a pointer-to-member, we just
14628 want the referenced member referenced. */
14629 && TREE_CODE (expr
) != OFFSET_REF
)
14630 expr
= convert_from_reference (expr
);
14632 if (REF_PARENTHESIZED_P (qualified_id
))
14633 expr
= force_paren_expr (expr
);
14638 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14639 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14643 tsubst_init (tree init
, tree decl
, tree args
,
14644 tsubst_flags_t complain
, tree in_decl
)
14649 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14651 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14653 /* If we had an initializer but it
14654 instantiated to nothing,
14655 value-initialize the object. This will
14656 only occur when the initializer was a
14657 pack expansion where the parameter packs
14658 used in that expansion were of length
14660 init
= build_value_init (TREE_TYPE (decl
),
14662 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14663 init
= get_target_expr_sfinae (init
, complain
);
14664 if (TREE_CODE (init
) == TARGET_EXPR
)
14665 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14671 /* Like tsubst, but deals with expressions. This function just replaces
14672 template parms; to finish processing the resultant expression, use
14673 tsubst_copy_and_build or tsubst_expr. */
14676 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14678 enum tree_code code
;
14681 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14684 code
= TREE_CODE (t
);
14689 r
= retrieve_local_specialization (t
);
14691 if (r
== NULL_TREE
)
14693 /* We get here for a use of 'this' in an NSDMI. */
14694 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14695 return current_class_ptr
;
14697 /* This can happen for a parameter name used later in a function
14698 declaration (such as in a late-specified return type). Just
14699 make a dummy decl, since it's only used for its type. */
14700 gcc_assert (cp_unevaluated_operand
!= 0);
14701 r
= tsubst_decl (t
, args
, complain
);
14702 /* Give it the template pattern as its context; its true context
14703 hasn't been instantiated yet and this is good enough for
14705 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14708 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14709 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14710 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14711 return error_mark_node
;
14719 if (DECL_TEMPLATE_PARM_P (t
))
14720 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14721 /* There is no need to substitute into namespace-scope
14723 if (DECL_NAMESPACE_SCOPE_P (t
))
14725 /* If ARGS is NULL, then T is known to be non-dependent. */
14726 if (args
== NULL_TREE
)
14727 return scalar_constant_value (t
);
14729 /* Unfortunately, we cannot just call lookup_name here.
14732 template <int I> int f() {
14734 struct S { void g() { E e = a; } };
14737 When we instantiate f<7>::S::g(), say, lookup_name is not
14738 clever enough to find f<7>::a. */
14740 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14741 /*entering_scope=*/0);
14743 for (v
= TYPE_VALUES (enum_type
);
14745 v
= TREE_CHAIN (v
))
14746 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14747 return TREE_VALUE (v
);
14749 /* We didn't find the name. That should never happen; if
14750 name-lookup found it during preliminary parsing, we
14751 should find it again here during instantiation. */
14752 gcc_unreachable ();
14757 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14759 /* Check for a local specialization set up by
14760 tsubst_pack_expansion. */
14761 if (tree r
= retrieve_local_specialization (t
))
14763 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14764 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14768 /* When retrieving a capture pack from a generic lambda, remove the
14769 lambda call op's own template argument list from ARGS. Only the
14770 template arguments active for the closure type should be used to
14771 retrieve the pack specialization. */
14772 if (LAMBDA_FUNCTION_P (current_function_decl
)
14773 && (template_class_depth (DECL_CONTEXT (t
))
14774 != TMPL_ARGS_DEPTH (args
)))
14775 args
= strip_innermost_template_args (args
, 1);
14777 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14778 tsubst_decl put in the hash table. */
14779 return retrieve_specialization (t
, args
, 0);
14782 if (DECL_CONTEXT (t
))
14786 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14787 /*entering_scope=*/1);
14788 if (ctx
!= DECL_CONTEXT (t
))
14790 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14793 if (complain
& tf_error
)
14794 error ("using invalid field %qD", t
);
14795 return error_mark_node
;
14804 case FUNCTION_DECL
:
14805 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14806 r
= tsubst (t
, args
, complain
, in_decl
);
14807 else if (local_variable_p (t
)
14808 && uses_template_parms (DECL_CONTEXT (t
)))
14810 r
= retrieve_local_specialization (t
);
14811 if (r
== NULL_TREE
)
14813 /* First try name lookup to find the instantiation. */
14814 r
= lookup_name (DECL_NAME (t
));
14815 if (r
&& !is_capture_proxy (r
))
14817 /* Make sure that the one we found is the one we want. */
14818 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14819 if (ctx
!= DECL_CONTEXT (r
))
14827 /* This can happen for a variable used in a
14828 late-specified return type of a local lambda, or for a
14829 local static or constant. Building a new VAR_DECL
14830 should be OK in all those cases. */
14831 r
= tsubst_decl (t
, args
, complain
);
14832 if (local_specializations
)
14833 /* Avoid infinite recursion (79640). */
14834 register_local_specialization (r
, t
);
14835 if (decl_maybe_constant_var_p (r
))
14837 /* We can't call cp_finish_decl, so handle the
14838 initializer by hand. */
14839 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14840 complain
, in_decl
);
14841 if (!processing_template_decl
)
14842 init
= maybe_constant_init (init
);
14843 if (processing_template_decl
14844 ? potential_constant_expression (init
)
14845 : reduced_constant_expression_p (init
))
14846 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14847 = TREE_CONSTANT (r
) = true;
14848 DECL_INITIAL (r
) = init
;
14849 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14851 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14852 complain
, adc_variable_type
);
14854 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14855 || decl_constant_var_p (r
)
14856 || errorcount
|| sorrycount
);
14857 if (!processing_template_decl
14858 && !TREE_STATIC (r
))
14859 r
= process_outer_var_ref (r
, complain
);
14861 /* Remember this for subsequent uses. */
14862 if (local_specializations
)
14863 register_local_specialization (r
, t
);
14868 if (!mark_used (r
, complain
))
14869 return error_mark_node
;
14872 case NAMESPACE_DECL
:
14876 /* An OVERLOAD will always be a non-dependent overload set; an
14877 overload set from function scope will just be represented with an
14878 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14879 gcc_assert (!uses_template_parms (t
));
14880 /* We must have marked any lookups as persistent. */
14881 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14885 return tsubst_baselink (t
, current_nonlambda_class_type (),
14886 args
, complain
, in_decl
);
14888 case TEMPLATE_DECL
:
14889 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14890 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14891 args
, complain
, in_decl
);
14892 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14893 return tsubst (t
, args
, complain
, in_decl
);
14894 else if (DECL_CLASS_SCOPE_P (t
)
14895 && uses_template_parms (DECL_CONTEXT (t
)))
14897 /* Template template argument like the following example need
14900 template <template <class> class TT> struct C {};
14901 template <class T> struct D {
14902 template <class U> struct E {};
14907 We are processing the template argument `E' in #1 for
14908 the template instantiation #2. Originally, `E' is a
14909 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14910 have to substitute this with one having context `D<int>'. */
14912 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14913 if (dependent_scope_p (context
))
14915 /* When rewriting a constructor into a deduction guide, a
14916 non-dependent name can become dependent, so memtmpl<args>
14917 becomes context::template memtmpl<args>. */
14918 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14919 return build_qualified_name (type
, context
, DECL_NAME (t
),
14922 return lookup_field (context
, DECL_NAME(t
), 0, false);
14925 /* Ordinary template template argument. */
14928 case NON_LVALUE_EXPR
:
14929 case VIEW_CONVERT_EXPR
:
14931 /* Handle location wrappers by substituting the wrapped node
14932 first, *then* reusing the resulting type. Doing the type
14933 first ensures that we handle template parameters and
14934 parameter pack expansions. */
14935 gcc_assert (location_wrapper_p (t
));
14936 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14937 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
14941 case REINTERPRET_CAST_EXPR
:
14942 case CONST_CAST_EXPR
:
14943 case STATIC_CAST_EXPR
:
14944 case DYNAMIC_CAST_EXPR
:
14945 case IMPLICIT_CONV_EXPR
:
14949 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14950 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14951 return build1 (code
, type
, op0
);
14955 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14956 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14958 tree expanded
, op
= TREE_OPERAND (t
, 0);
14961 if (SIZEOF_EXPR_TYPE_P (t
))
14962 op
= TREE_TYPE (op
);
14964 ++cp_unevaluated_operand
;
14965 ++c_inhibit_evaluation_warnings
;
14966 /* We only want to compute the number of arguments. */
14967 if (PACK_EXPANSION_P (op
))
14968 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14970 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14971 args
, complain
, in_decl
);
14972 --cp_unevaluated_operand
;
14973 --c_inhibit_evaluation_warnings
;
14975 if (TREE_CODE (expanded
) == TREE_VEC
)
14977 len
= TREE_VEC_LENGTH (expanded
);
14978 /* Set TREE_USED for the benefit of -Wunused. */
14979 for (int i
= 0; i
< len
; i
++)
14980 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14981 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14984 if (expanded
== error_mark_node
)
14985 return error_mark_node
;
14986 else if (PACK_EXPANSION_P (expanded
)
14987 || (TREE_CODE (expanded
) == TREE_VEC
14988 && pack_expansion_args_count (expanded
)))
14991 if (PACK_EXPANSION_P (expanded
))
14993 else if (TREE_VEC_LENGTH (expanded
) == 1)
14994 expanded
= TREE_VEC_ELT (expanded
, 0);
14996 expanded
= make_argument_pack (expanded
);
14998 if (TYPE_P (expanded
))
14999 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15000 complain
& tf_error
);
15002 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15003 complain
& tf_error
);
15006 return build_int_cst (size_type_node
, len
);
15008 if (SIZEOF_EXPR_TYPE_P (t
))
15010 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15011 args
, complain
, in_decl
);
15012 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15013 r
= build1 (SIZEOF_EXPR
,
15014 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15015 SIZEOF_EXPR_TYPE_P (r
) = 1;
15022 case TRUTH_NOT_EXPR
:
15025 case UNARY_PLUS_EXPR
: /* Unary + */
15027 case AT_ENCODE_EXPR
:
15031 case REALPART_EXPR
:
15032 case IMAGPART_EXPR
:
15035 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15036 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15037 return build1 (code
, type
, op0
);
15040 case COMPONENT_REF
:
15045 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15046 name
= TREE_OPERAND (t
, 1);
15047 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15049 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15050 complain
, in_decl
);
15051 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15053 else if (TREE_CODE (name
) == SCOPE_REF
15054 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15056 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15057 complain
, in_decl
);
15058 name
= TREE_OPERAND (name
, 1);
15059 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15060 complain
, in_decl
);
15061 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15062 name
= build_qualified_name (/*type=*/NULL_TREE
,
15064 /*template_p=*/false);
15066 else if (BASELINK_P (name
))
15067 name
= tsubst_baselink (name
,
15068 non_reference (TREE_TYPE (object
)),
15072 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15073 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15079 case TRUNC_DIV_EXPR
:
15080 case CEIL_DIV_EXPR
:
15081 case FLOOR_DIV_EXPR
:
15082 case ROUND_DIV_EXPR
:
15083 case EXACT_DIV_EXPR
:
15087 case TRUNC_MOD_EXPR
:
15088 case FLOOR_MOD_EXPR
:
15089 case TRUTH_ANDIF_EXPR
:
15090 case TRUTH_ORIF_EXPR
:
15091 case TRUTH_AND_EXPR
:
15092 case TRUTH_OR_EXPR
:
15105 case COMPOUND_EXPR
:
15108 case PREDECREMENT_EXPR
:
15109 case PREINCREMENT_EXPR
:
15110 case POSTDECREMENT_EXPR
:
15111 case POSTINCREMENT_EXPR
:
15113 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15114 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15115 return build_nt (code
, op0
, op1
);
15120 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15121 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15122 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15123 QUALIFIED_NAME_IS_TEMPLATE (t
));
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_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15135 int n
= VL_EXP_OPERAND_LENGTH (t
);
15136 tree result
= build_vl_exp (CALL_EXPR
, n
);
15138 for (i
= 0; i
< n
; i
++)
15139 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15140 complain
, in_decl
);
15146 case PSEUDO_DTOR_EXPR
:
15147 case VEC_PERM_EXPR
:
15149 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15150 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15151 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15152 r
= build_nt (code
, op0
, op1
, op2
);
15153 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15159 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15160 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15161 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15162 r
= build_nt (code
, op0
, op1
, op2
);
15163 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15169 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15170 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15171 r
= build_nt (code
, op0
, op1
);
15172 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15173 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15177 case TEMPLATE_ID_EXPR
:
15179 /* Substituted template arguments */
15180 tree fn
= TREE_OPERAND (t
, 0);
15181 tree targs
= TREE_OPERAND (t
, 1);
15183 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15185 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15187 return lookup_template_function (fn
, targs
);
15192 tree purpose
, value
, chain
;
15194 if (t
== void_list_node
)
15197 purpose
= TREE_PURPOSE (t
);
15199 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15200 value
= TREE_VALUE (t
);
15202 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15203 chain
= TREE_CHAIN (t
);
15204 if (chain
&& chain
!= void_type_node
)
15205 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15206 if (purpose
== TREE_PURPOSE (t
)
15207 && value
== TREE_VALUE (t
)
15208 && chain
== TREE_CHAIN (t
))
15210 return tree_cons (purpose
, value
, chain
);
15215 case ENUMERAL_TYPE
:
15217 case TEMPLATE_TYPE_PARM
:
15218 case TEMPLATE_TEMPLATE_PARM
:
15219 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15220 case TEMPLATE_PARM_INDEX
:
15222 case REFERENCE_TYPE
:
15224 case FUNCTION_TYPE
:
15227 case TYPENAME_TYPE
:
15228 case UNBOUND_CLASS_TEMPLATE
:
15230 case DECLTYPE_TYPE
:
15232 return tsubst (t
, args
, complain
, in_decl
);
15236 /* Fall through. */
15237 case IDENTIFIER_NODE
:
15238 if (IDENTIFIER_CONV_OP_P (t
))
15240 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15241 return make_conv_op_name (new_type
);
15247 /* This is handled by tsubst_copy_and_build. */
15248 gcc_unreachable ();
15252 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15253 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15254 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15257 case CLEANUP_POINT_EXPR
:
15258 /* We shouldn't have built any of these during initial template
15259 generation. Instead, they should be built during instantiation
15260 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15261 gcc_unreachable ();
15265 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15266 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15267 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15268 r
= build2 (code
, type
, op0
, op1
);
15269 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15270 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15271 && !(complain
& tf_error
))
15272 return error_mark_node
;
15276 case EXPR_PACK_EXPANSION
:
15277 error ("invalid use of pack expansion expression");
15278 return error_mark_node
;
15280 case NONTYPE_ARGUMENT_PACK
:
15281 error ("use %<...%> to expand argument pack");
15282 return error_mark_node
;
15285 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15293 /* Instantiate any typedefs in the type. */
15294 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15295 r
= fold_convert (type
, t
);
15296 gcc_assert (TREE_CODE (r
) == code
);
15301 /* These can sometimes show up in a partial instantiation, but never
15302 involve template parms. */
15303 gcc_assert (!uses_template_parms (t
));
15306 case UNARY_LEFT_FOLD_EXPR
:
15307 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15308 case UNARY_RIGHT_FOLD_EXPR
:
15309 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15310 case BINARY_LEFT_FOLD_EXPR
:
15311 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15312 case BINARY_RIGHT_FOLD_EXPR
:
15313 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15317 case DEBUG_BEGIN_STMT
:
15318 /* ??? There's no point in copying it for now, but maybe some
15319 day it will contain more information, such as a pointer back
15320 to the containing function, inlined copy or so. */
15324 /* We shouldn't get here, but keep going if !flag_checking. */
15326 gcc_unreachable ();
15331 /* Helper function for tsubst_omp_clauses, used for instantiation of
15332 OMP_CLAUSE_DECL of clauses. */
15335 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15338 if (decl
== NULL_TREE
)
15341 /* Handle an OpenMP array section represented as a TREE_LIST (or
15342 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15343 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15344 TREE_LIST. We can handle it exactly the same as an array section
15345 (purpose, value, and a chain), even though the nomenclature
15346 (low_bound, length, etc) is different. */
15347 if (TREE_CODE (decl
) == TREE_LIST
)
15350 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15351 /*integral_constant_expression_p=*/false);
15352 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15353 /*integral_constant_expression_p=*/false);
15354 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15356 if (TREE_PURPOSE (decl
) == low_bound
15357 && TREE_VALUE (decl
) == length
15358 && TREE_CHAIN (decl
) == chain
)
15360 tree ret
= tree_cons (low_bound
, length
, chain
);
15361 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15362 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15365 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15366 /*integral_constant_expression_p=*/false);
15367 /* Undo convert_from_reference tsubst_expr could have called. */
15369 && REFERENCE_REF_P (ret
)
15370 && !REFERENCE_REF_P (decl
))
15371 ret
= TREE_OPERAND (ret
, 0);
15375 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15378 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15379 tree args
, tsubst_flags_t complain
, tree in_decl
)
15381 tree new_clauses
= NULL_TREE
, nc
, oc
;
15382 tree linear_no_step
= NULL_TREE
;
15384 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15386 nc
= copy_node (oc
);
15387 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15390 switch (OMP_CLAUSE_CODE (nc
))
15392 case OMP_CLAUSE_LASTPRIVATE
:
15393 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15395 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15396 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15397 in_decl
, /*integral_constant_expression_p=*/false);
15398 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15399 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15402 case OMP_CLAUSE_PRIVATE
:
15403 case OMP_CLAUSE_SHARED
:
15404 case OMP_CLAUSE_FIRSTPRIVATE
:
15405 case OMP_CLAUSE_COPYIN
:
15406 case OMP_CLAUSE_COPYPRIVATE
:
15407 case OMP_CLAUSE_UNIFORM
:
15408 case OMP_CLAUSE_DEPEND
:
15409 case OMP_CLAUSE_FROM
:
15410 case OMP_CLAUSE_TO
:
15411 case OMP_CLAUSE_MAP
:
15412 case OMP_CLAUSE_USE_DEVICE_PTR
:
15413 case OMP_CLAUSE_IS_DEVICE_PTR
:
15414 OMP_CLAUSE_DECL (nc
)
15415 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15418 case OMP_CLAUSE_TILE
:
15419 case OMP_CLAUSE_IF
:
15420 case OMP_CLAUSE_NUM_THREADS
:
15421 case OMP_CLAUSE_SCHEDULE
:
15422 case OMP_CLAUSE_COLLAPSE
:
15423 case OMP_CLAUSE_FINAL
:
15424 case OMP_CLAUSE_DEVICE
:
15425 case OMP_CLAUSE_DIST_SCHEDULE
:
15426 case OMP_CLAUSE_NUM_TEAMS
:
15427 case OMP_CLAUSE_THREAD_LIMIT
:
15428 case OMP_CLAUSE_SAFELEN
:
15429 case OMP_CLAUSE_SIMDLEN
:
15430 case OMP_CLAUSE_NUM_TASKS
:
15431 case OMP_CLAUSE_GRAINSIZE
:
15432 case OMP_CLAUSE_PRIORITY
:
15433 case OMP_CLAUSE_ORDERED
:
15434 case OMP_CLAUSE_HINT
:
15435 case OMP_CLAUSE_NUM_GANGS
:
15436 case OMP_CLAUSE_NUM_WORKERS
:
15437 case OMP_CLAUSE_VECTOR_LENGTH
:
15438 case OMP_CLAUSE_WORKER
:
15439 case OMP_CLAUSE_VECTOR
:
15440 case OMP_CLAUSE_ASYNC
:
15441 case OMP_CLAUSE_WAIT
:
15442 OMP_CLAUSE_OPERAND (nc
, 0)
15443 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15444 in_decl
, /*integral_constant_expression_p=*/false);
15446 case OMP_CLAUSE_REDUCTION
:
15447 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15449 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15450 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15452 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15453 complain
, in_decl
);
15454 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15455 = build_qualified_name (NULL_TREE
, scope
,
15456 TREE_OPERAND (placeholder
, 1),
15460 gcc_assert (identifier_p (placeholder
));
15462 OMP_CLAUSE_DECL (nc
)
15463 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15466 case OMP_CLAUSE_GANG
:
15467 case OMP_CLAUSE_ALIGNED
:
15468 OMP_CLAUSE_DECL (nc
)
15469 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15471 OMP_CLAUSE_OPERAND (nc
, 1)
15472 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15473 in_decl
, /*integral_constant_expression_p=*/false);
15475 case OMP_CLAUSE_LINEAR
:
15476 OMP_CLAUSE_DECL (nc
)
15477 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15479 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15481 gcc_assert (!linear_no_step
);
15482 linear_no_step
= nc
;
15484 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15485 OMP_CLAUSE_LINEAR_STEP (nc
)
15486 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15487 complain
, in_decl
);
15489 OMP_CLAUSE_LINEAR_STEP (nc
)
15490 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15492 /*integral_constant_expression_p=*/false);
15494 case OMP_CLAUSE_NOWAIT
:
15495 case OMP_CLAUSE_DEFAULT
:
15496 case OMP_CLAUSE_UNTIED
:
15497 case OMP_CLAUSE_MERGEABLE
:
15498 case OMP_CLAUSE_INBRANCH
:
15499 case OMP_CLAUSE_NOTINBRANCH
:
15500 case OMP_CLAUSE_PROC_BIND
:
15501 case OMP_CLAUSE_FOR
:
15502 case OMP_CLAUSE_PARALLEL
:
15503 case OMP_CLAUSE_SECTIONS
:
15504 case OMP_CLAUSE_TASKGROUP
:
15505 case OMP_CLAUSE_NOGROUP
:
15506 case OMP_CLAUSE_THREADS
:
15507 case OMP_CLAUSE_SIMD
:
15508 case OMP_CLAUSE_DEFAULTMAP
:
15509 case OMP_CLAUSE_INDEPENDENT
:
15510 case OMP_CLAUSE_AUTO
:
15511 case OMP_CLAUSE_SEQ
:
15514 gcc_unreachable ();
15516 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15517 switch (OMP_CLAUSE_CODE (nc
))
15519 case OMP_CLAUSE_SHARED
:
15520 case OMP_CLAUSE_PRIVATE
:
15521 case OMP_CLAUSE_FIRSTPRIVATE
:
15522 case OMP_CLAUSE_LASTPRIVATE
:
15523 case OMP_CLAUSE_COPYPRIVATE
:
15524 case OMP_CLAUSE_LINEAR
:
15525 case OMP_CLAUSE_REDUCTION
:
15526 case OMP_CLAUSE_USE_DEVICE_PTR
:
15527 case OMP_CLAUSE_IS_DEVICE_PTR
:
15528 /* tsubst_expr on SCOPE_REF results in returning
15529 finish_non_static_data_member result. Undo that here. */
15530 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15531 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15532 == IDENTIFIER_NODE
))
15534 tree t
= OMP_CLAUSE_DECL (nc
);
15537 switch (TREE_CODE (v
))
15539 case COMPONENT_REF
:
15543 case POINTER_PLUS_EXPR
:
15544 v
= TREE_OPERAND (v
, 0);
15547 if (DECL_CONTEXT (v
) == current_function_decl
15548 && DECL_ARTIFICIAL (v
)
15549 && DECL_NAME (v
) == this_identifier
)
15550 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15557 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15558 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15559 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15560 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15561 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15563 tree decl
= OMP_CLAUSE_DECL (nc
);
15566 retrofit_lang_decl (decl
);
15567 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15576 new_clauses
= nreverse (new_clauses
);
15577 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15579 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15580 if (linear_no_step
)
15581 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15582 if (nc
== linear_no_step
)
15584 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15588 return new_clauses
;
15591 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15594 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15597 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15599 tree purpose
, value
, chain
;
15604 if (TREE_CODE (t
) != TREE_LIST
)
15605 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15606 /*function_p=*/false,
15607 /*integral_constant_expression_p=*/false);
15609 if (t
== void_list_node
)
15612 purpose
= TREE_PURPOSE (t
);
15614 purpose
= RECUR (purpose
);
15615 value
= TREE_VALUE (t
);
15618 if (TREE_CODE (value
) != LABEL_DECL
)
15619 value
= RECUR (value
);
15622 value
= lookup_label (DECL_NAME (value
));
15623 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15624 TREE_USED (value
) = 1;
15627 chain
= TREE_CHAIN (t
);
15628 if (chain
&& chain
!= void_type_node
)
15629 chain
= RECUR (chain
);
15630 return tree_cons (purpose
, value
, chain
);
15634 /* Used to temporarily communicate the list of #pragma omp parallel
15635 clauses to #pragma omp for instantiation if they are combined
15638 static tree
*omp_parallel_combined_clauses
;
15640 /* Substitute one OMP_FOR iterator. */
15643 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15644 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15645 tree args
, tsubst_flags_t complain
, tree in_decl
,
15646 bool integral_constant_expression_p
)
15648 #define RECUR(NODE) \
15649 tsubst_expr ((NODE), args, complain, in_decl, \
15650 integral_constant_expression_p)
15651 tree decl
, init
, cond
, incr
;
15653 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15654 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15656 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15658 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15659 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15662 decl
= TREE_OPERAND (init
, 0);
15663 init
= TREE_OPERAND (init
, 1);
15664 tree decl_expr
= NULL_TREE
;
15665 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15667 /* We need to jump through some hoops to handle declarations in the
15668 init-statement, since we might need to handle auto deduction,
15669 but we need to keep control of initialization. */
15671 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15672 decl
= tsubst_decl (decl
, args
, complain
);
15676 if (TREE_CODE (decl
) == SCOPE_REF
)
15678 decl
= RECUR (decl
);
15679 if (TREE_CODE (decl
) == COMPONENT_REF
)
15683 switch (TREE_CODE (v
))
15685 case COMPONENT_REF
:
15689 case POINTER_PLUS_EXPR
:
15690 v
= TREE_OPERAND (v
, 0);
15693 if (DECL_CONTEXT (v
) == current_function_decl
15694 && DECL_ARTIFICIAL (v
)
15695 && DECL_NAME (v
) == this_identifier
)
15697 decl
= TREE_OPERAND (decl
, 1);
15698 decl
= omp_privatize_field (decl
, false);
15708 decl
= RECUR (decl
);
15710 init
= RECUR (init
);
15712 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15713 if (auto_node
&& init
)
15715 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15717 gcc_assert (!type_dependent_expression_p (decl
));
15719 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15723 /* Declare the variable, but don't let that initialize it. */
15724 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15725 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15727 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15730 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15731 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15732 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15734 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15735 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15736 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15737 NOP_EXPR
, rhs
, complain
);
15740 incr
= RECUR (incr
);
15741 TREE_VEC_ELT (declv
, i
) = decl
;
15742 TREE_VEC_ELT (initv
, i
) = init
;
15743 TREE_VEC_ELT (condv
, i
) = cond
;
15744 TREE_VEC_ELT (incrv
, i
) = incr
;
15750 /* Declare and initialize the variable. */
15758 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15760 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15762 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15763 && OMP_CLAUSE_DECL (*pc
) == decl
)
15765 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15766 && OMP_CLAUSE_DECL (*pc
) == decl
)
15770 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15772 *pc
= OMP_CLAUSE_CHAIN (c
);
15773 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15776 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15777 && OMP_CLAUSE_DECL (*pc
) == decl
)
15779 error ("iteration variable %qD should not be firstprivate",
15781 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15783 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15784 && OMP_CLAUSE_DECL (*pc
) == decl
)
15786 error ("iteration variable %qD should not be reduction",
15788 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15791 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15796 if (*pc
== NULL_TREE
)
15798 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15799 OMP_CLAUSE_DECL (c
) = decl
;
15800 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15803 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15808 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15809 if (COMPARISON_CLASS_P (cond
))
15811 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15812 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15813 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15816 cond
= RECUR (cond
);
15817 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15818 switch (TREE_CODE (incr
))
15820 case PREINCREMENT_EXPR
:
15821 case PREDECREMENT_EXPR
:
15822 case POSTINCREMENT_EXPR
:
15823 case POSTDECREMENT_EXPR
:
15824 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15825 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15828 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15829 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15831 tree rhs
= TREE_OPERAND (incr
, 1);
15832 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15833 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15834 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15835 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15836 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15840 incr
= RECUR (incr
);
15843 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15844 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15846 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15847 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15848 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15849 TREE_TYPE (decl
), lhs
,
15850 RECUR (TREE_OPERAND (incr
, 2))));
15852 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15853 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15854 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15856 tree rhs
= TREE_OPERAND (incr
, 2);
15857 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15858 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15859 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15860 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15861 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15865 incr
= RECUR (incr
);
15868 incr
= RECUR (incr
);
15872 TREE_VEC_ELT (declv
, i
) = decl
;
15873 TREE_VEC_ELT (initv
, i
) = init
;
15874 TREE_VEC_ELT (condv
, i
) = cond
;
15875 TREE_VEC_ELT (incrv
, i
) = incr
;
15879 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15880 of OMP_TARGET's body. */
15883 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15885 *walk_subtrees
= 0;
15886 switch (TREE_CODE (*tp
))
15891 case STATEMENT_LIST
:
15892 *walk_subtrees
= 1;
15900 /* Helper function for tsubst_expr. For decomposition declaration
15901 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15902 also the corresponding decls representing the identifiers
15903 of the decomposition declaration. Return DECL if successful
15904 or error_mark_node otherwise, set *FIRST to the first decl
15905 in the list chained through DECL_CHAIN and *CNT to the number
15909 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15910 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15913 tree decl2
, decl3
, prev
= decl
;
15915 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15916 for (decl2
= DECL_CHAIN (pattern_decl
);
15919 && DECL_DECOMPOSITION_P (decl2
)
15920 && DECL_NAME (decl2
);
15921 decl2
= DECL_CHAIN (decl2
))
15923 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15925 gcc_assert (errorcount
);
15926 return error_mark_node
;
15929 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
15930 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15931 tree v
= DECL_VALUE_EXPR (decl2
);
15932 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15933 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15934 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15935 SET_DECL_VALUE_EXPR (decl2
, v
);
15936 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15938 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15939 maybe_push_decl (decl3
);
15940 if (error_operand_p (decl3
))
15941 decl
= error_mark_node
;
15942 else if (decl
!= error_mark_node
15943 && DECL_CHAIN (decl3
) != prev
)
15945 gcc_assert (errorcount
);
15946 decl
= error_mark_node
;
15955 /* Like tsubst_copy for expressions, etc. but also does semantic
15959 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15960 bool integral_constant_expression_p
)
15962 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15963 #define RECUR(NODE) \
15964 tsubst_expr ((NODE), args, complain, in_decl, \
15965 integral_constant_expression_p)
15971 if (t
== NULL_TREE
|| t
== error_mark_node
)
15974 loc
= input_location
;
15975 if (EXPR_HAS_LOCATION (t
))
15976 input_location
= EXPR_LOCATION (t
);
15977 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15978 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15980 switch (TREE_CODE (t
))
15982 case STATEMENT_LIST
:
15984 tree_stmt_iterator i
;
15985 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15986 RECUR (tsi_stmt (i
));
15990 case CTOR_INITIALIZER
:
15991 finish_mem_initializers (tsubst_initializer_list
15992 (TREE_OPERAND (t
, 0), args
));
15996 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16000 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16001 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16002 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16004 finish_expr_stmt (tmp
);
16008 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16009 /*attribs=*/NULL_TREE
);
16014 tree decl
, pattern_decl
;
16017 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16018 if (TREE_CODE (decl
) == LABEL_DECL
)
16019 finish_label_decl (DECL_NAME (decl
));
16020 else if (TREE_CODE (decl
) == USING_DECL
)
16022 tree scope
= USING_DECL_SCOPE (decl
);
16023 tree name
= DECL_NAME (decl
);
16025 scope
= tsubst (scope
, args
, complain
, in_decl
);
16026 decl
= lookup_qualified_name (scope
, name
,
16027 /*is_type_p=*/false,
16028 /*complain=*/false);
16029 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16030 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16032 finish_local_using_decl (decl
, scope
, name
);
16034 else if (DECL_PACK_P (decl
))
16036 /* Don't build up decls for a variadic capture proxy, we'll
16037 instantiate the elements directly as needed. */
16040 else if (is_capture_proxy (decl
)
16041 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16043 /* We're in tsubst_lambda_expr, we've already inserted a new
16044 capture proxy, so look it up and register it. */
16045 tree inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16046 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16047 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16048 register_local_specialization (inst
, decl
);
16051 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16052 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16053 /* Don't copy the old closure; we'll create a new one in
16054 tsubst_lambda_expr. */
16058 init
= DECL_INITIAL (decl
);
16059 decl
= tsubst (decl
, args
, complain
, in_decl
);
16060 if (decl
!= error_mark_node
)
16062 /* By marking the declaration as instantiated, we avoid
16063 trying to instantiate it. Since instantiate_decl can't
16064 handle local variables, and since we've already done
16065 all that needs to be done, that's the right thing to
16068 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16070 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16071 /* Anonymous aggregates are a special case. */
16072 finish_anon_union (decl
);
16073 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16075 DECL_CONTEXT (decl
) = current_function_decl
;
16076 if (DECL_NAME (decl
) == this_identifier
)
16078 tree lam
= DECL_CONTEXT (current_function_decl
);
16079 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16080 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16082 insert_capture_proxy (decl
);
16084 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16085 /* We already did a pushtag. */;
16086 else if (TREE_CODE (decl
) == FUNCTION_DECL
16087 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16088 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16090 DECL_CONTEXT (decl
) = NULL_TREE
;
16092 DECL_CONTEXT (decl
) = current_function_decl
;
16093 cp_check_omp_declare_reduction (decl
);
16097 int const_init
= false;
16098 maybe_push_decl (decl
);
16100 && DECL_PRETTY_FUNCTION_P (decl
))
16102 /* For __PRETTY_FUNCTION__ we have to adjust the
16104 const char *const name
16105 = cxx_printable_name (current_function_decl
, 2);
16106 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16109 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16112 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16115 && DECL_DECOMPOSITION_P (decl
)
16116 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16121 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16122 complain
, in_decl
, &first
, &cnt
);
16123 if (ndecl
!= error_mark_node
)
16124 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16125 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16126 if (ndecl
!= error_mark_node
)
16127 cp_finish_decomp (ndecl
, first
, cnt
);
16130 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16139 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16140 RECUR (FOR_INIT_STMT (t
));
16141 finish_init_stmt (stmt
);
16142 tmp
= RECUR (FOR_COND (t
));
16143 finish_for_cond (tmp
, stmt
, false, 0);
16144 tmp
= RECUR (FOR_EXPR (t
));
16145 finish_for_expr (tmp
, stmt
);
16147 bool prev
= note_iteration_stmt_body_start ();
16148 RECUR (FOR_BODY (t
));
16149 note_iteration_stmt_body_end (prev
);
16151 finish_for_stmt (stmt
);
16154 case RANGE_FOR_STMT
:
16156 /* Construct another range_for, if this is not a final
16157 substitution (for inside inside a generic lambda of a
16158 template). Otherwise convert to a regular for. */
16160 stmt
= (processing_template_decl
16161 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16162 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16163 decl
= RANGE_FOR_DECL (t
);
16164 decl
= tsubst (decl
, args
, complain
, in_decl
);
16165 maybe_push_decl (decl
);
16166 expr
= RECUR (RANGE_FOR_EXPR (t
));
16168 tree decomp_first
= NULL_TREE
;
16169 unsigned decomp_cnt
= 0;
16170 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16171 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16173 &decomp_first
, &decomp_cnt
);
16175 if (processing_template_decl
)
16177 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16178 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16179 finish_range_for_decl (stmt
, decl
, expr
);
16183 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16184 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16185 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16186 decomp_first
, decomp_cnt
,
16187 RANGE_FOR_IVDEP (t
), unroll
);
16190 bool prev
= note_iteration_stmt_body_start ();
16191 RECUR (RANGE_FOR_BODY (t
));
16192 note_iteration_stmt_body_end (prev
);
16193 finish_for_stmt (stmt
);
16198 stmt
= begin_while_stmt ();
16199 tmp
= RECUR (WHILE_COND (t
));
16200 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16202 bool prev
= note_iteration_stmt_body_start ();
16203 RECUR (WHILE_BODY (t
));
16204 note_iteration_stmt_body_end (prev
);
16206 finish_while_stmt (stmt
);
16210 stmt
= begin_do_stmt ();
16212 bool prev
= note_iteration_stmt_body_start ();
16213 RECUR (DO_BODY (t
));
16214 note_iteration_stmt_body_end (prev
);
16216 finish_do_body (stmt
);
16217 tmp
= RECUR (DO_COND (t
));
16218 finish_do_stmt (tmp
, stmt
, false, 0);
16222 stmt
= begin_if_stmt ();
16223 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16224 tmp
= RECUR (IF_COND (t
));
16225 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16226 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16227 /* Don't instantiate the THEN_CLAUSE. */;
16230 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16232 ++c_inhibit_evaluation_warnings
;
16233 RECUR (THEN_CLAUSE (t
));
16235 --c_inhibit_evaluation_warnings
;
16237 finish_then_clause (stmt
);
16239 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16240 /* Don't instantiate the ELSE_CLAUSE. */;
16241 else if (ELSE_CLAUSE (t
))
16243 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16244 begin_else_clause (stmt
);
16246 ++c_inhibit_evaluation_warnings
;
16247 RECUR (ELSE_CLAUSE (t
));
16249 --c_inhibit_evaluation_warnings
;
16250 finish_else_clause (stmt
);
16253 finish_if_stmt (stmt
);
16257 if (BIND_EXPR_BODY_BLOCK (t
))
16258 stmt
= begin_function_body ();
16260 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16261 ? BCS_TRY_BLOCK
: 0);
16263 RECUR (BIND_EXPR_BODY (t
));
16265 if (BIND_EXPR_BODY_BLOCK (t
))
16266 finish_function_body (stmt
);
16268 finish_compound_stmt (stmt
);
16272 finish_break_stmt ();
16275 case CONTINUE_STMT
:
16276 finish_continue_stmt ();
16280 stmt
= begin_switch_stmt ();
16281 tmp
= RECUR (SWITCH_STMT_COND (t
));
16282 finish_switch_cond (tmp
, stmt
);
16283 RECUR (SWITCH_STMT_BODY (t
));
16284 finish_switch_stmt (stmt
);
16287 case CASE_LABEL_EXPR
:
16289 tree low
= RECUR (CASE_LOW (t
));
16290 tree high
= RECUR (CASE_HIGH (t
));
16291 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16292 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16293 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16294 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16300 tree decl
= LABEL_EXPR_LABEL (t
);
16303 label
= finish_label_stmt (DECL_NAME (decl
));
16304 if (TREE_CODE (label
) == LABEL_DECL
)
16305 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16306 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16307 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16312 tmp
= GOTO_DESTINATION (t
);
16313 if (TREE_CODE (tmp
) != LABEL_DECL
)
16314 /* Computed goto's must be tsubst'd into. On the other hand,
16315 non-computed gotos must not be; the identifier in question
16316 will have no binding. */
16319 tmp
= DECL_NAME (tmp
);
16320 finish_goto_stmt (tmp
);
16325 tree string
= RECUR (ASM_STRING (t
));
16326 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16327 complain
, in_decl
);
16328 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16329 complain
, in_decl
);
16330 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16331 complain
, in_decl
);
16332 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16333 complain
, in_decl
);
16334 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16336 tree asm_expr
= tmp
;
16337 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16338 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16339 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16346 stmt
= begin_try_block ();
16347 RECUR (TRY_STMTS (t
));
16348 finish_cleanup_try_block (stmt
);
16349 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16353 tree compound_stmt
= NULL_TREE
;
16355 if (FN_TRY_BLOCK_P (t
))
16356 stmt
= begin_function_try_block (&compound_stmt
);
16358 stmt
= begin_try_block ();
16360 RECUR (TRY_STMTS (t
));
16362 if (FN_TRY_BLOCK_P (t
))
16363 finish_function_try_block (stmt
);
16365 finish_try_block (stmt
);
16367 RECUR (TRY_HANDLERS (t
));
16368 if (FN_TRY_BLOCK_P (t
))
16369 finish_function_handler_sequence (stmt
, compound_stmt
);
16371 finish_handler_sequence (stmt
);
16377 tree decl
= HANDLER_PARMS (t
);
16381 decl
= tsubst (decl
, args
, complain
, in_decl
);
16382 /* Prevent instantiate_decl from trying to instantiate
16383 this variable. We've already done all that needs to be
16385 if (decl
!= error_mark_node
)
16386 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16388 stmt
= begin_handler ();
16389 finish_handler_parms (decl
, stmt
);
16390 RECUR (HANDLER_BODY (t
));
16391 finish_handler (stmt
);
16396 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16397 if (CLASS_TYPE_P (tmp
))
16399 /* Local classes are not independent templates; they are
16400 instantiated along with their containing function. And this
16401 way we don't have to deal with pushing out of one local class
16402 to instantiate a member of another local class. */
16403 /* Closures are handled by the LAMBDA_EXPR. */
16404 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16405 complete_type (tmp
);
16406 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16408 || (TREE_CODE (fld
) == FUNCTION_DECL
16409 && !DECL_ARTIFICIAL (fld
)))
16410 && DECL_TEMPLATE_INSTANTIATION (fld
))
16411 instantiate_decl (fld
, /*defer_ok=*/false,
16412 /*expl_inst_class=*/false);
16416 case STATIC_ASSERT
:
16420 ++c_inhibit_evaluation_warnings
;
16422 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16425 /*integral_constant_expression_p=*/true);
16426 --c_inhibit_evaluation_warnings
;
16428 finish_static_assert (condition
,
16429 STATIC_ASSERT_MESSAGE (t
),
16430 STATIC_ASSERT_SOURCE_LOCATION (t
),
16431 /*member_p=*/false);
16436 case OACC_PARALLEL
:
16437 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16439 stmt
= begin_omp_parallel ();
16440 RECUR (OMP_BODY (t
));
16441 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16445 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16446 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16447 complain
, in_decl
);
16448 if (OMP_PARALLEL_COMBINED (t
))
16449 omp_parallel_combined_clauses
= &tmp
;
16450 stmt
= begin_omp_parallel ();
16451 RECUR (OMP_PARALLEL_BODY (t
));
16452 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16453 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16454 = OMP_PARALLEL_COMBINED (t
);
16455 pop_omp_privatization_clauses (r
);
16459 r
= push_omp_privatization_clauses (false);
16460 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16461 complain
, in_decl
);
16462 stmt
= begin_omp_task ();
16463 RECUR (OMP_TASK_BODY (t
));
16464 finish_omp_task (tmp
, stmt
);
16465 pop_omp_privatization_clauses (r
);
16470 case OMP_DISTRIBUTE
:
16474 tree clauses
, body
, pre_body
;
16475 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16476 tree orig_declv
= NULL_TREE
;
16477 tree incrv
= NULL_TREE
;
16478 enum c_omp_region_type ort
= C_ORT_OMP
;
16481 if (TREE_CODE (t
) == OACC_LOOP
)
16484 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16485 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16487 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16489 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16490 if (OMP_FOR_ORIG_DECLS (t
))
16491 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16492 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16493 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16494 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16497 stmt
= begin_omp_structured_block ();
16499 pre_body
= push_stmt_list ();
16500 RECUR (OMP_FOR_PRE_BODY (t
));
16501 pre_body
= pop_stmt_list (pre_body
);
16503 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16504 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16505 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16506 incrv
, &clauses
, args
, complain
, in_decl
,
16507 integral_constant_expression_p
);
16508 omp_parallel_combined_clauses
= NULL
;
16510 body
= push_stmt_list ();
16511 RECUR (OMP_FOR_BODY (t
));
16512 body
= pop_stmt_list (body
);
16514 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16515 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16516 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16520 t
= make_node (TREE_CODE (t
));
16521 TREE_TYPE (t
) = void_type_node
;
16522 OMP_FOR_BODY (t
) = body
;
16523 OMP_FOR_PRE_BODY (t
) = pre_body
;
16524 OMP_FOR_CLAUSES (t
) = clauses
;
16525 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16529 add_stmt (finish_omp_structured_block (stmt
));
16530 pop_omp_privatization_clauses (r
);
16535 omp_parallel_combined_clauses
= NULL
;
16540 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16541 && OMP_TEAMS_COMBINED (t
));
16542 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16544 stmt
= push_stmt_list ();
16545 RECUR (OMP_BODY (t
));
16546 stmt
= pop_stmt_list (stmt
);
16549 OMP_BODY (t
) = stmt
;
16550 OMP_CLAUSES (t
) = tmp
;
16552 pop_omp_privatization_clauses (r
);
16556 case OMP_TARGET_DATA
:
16558 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16559 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16561 keep_next_level (true);
16562 stmt
= begin_omp_structured_block ();
16564 RECUR (OMP_BODY (t
));
16565 stmt
= finish_omp_structured_block (stmt
);
16568 OMP_BODY (t
) = stmt
;
16569 OMP_CLAUSES (t
) = tmp
;
16570 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16572 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16575 /* For combined target teams, ensure the num_teams and
16576 thread_limit clause expressions are evaluated on the host,
16577 before entering the target construct. */
16579 for (c
= OMP_TEAMS_CLAUSES (teams
);
16580 c
; c
= OMP_CLAUSE_CHAIN (c
))
16581 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16582 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16583 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16585 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16586 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16587 if (expr
== error_mark_node
)
16589 tmp
= TARGET_EXPR_SLOT (expr
);
16591 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16592 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16593 OMP_CLAUSE_FIRSTPRIVATE
);
16594 OMP_CLAUSE_DECL (tc
) = tmp
;
16595 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16596 OMP_TARGET_CLAUSES (t
) = tc
;
16605 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16606 complain
, in_decl
);
16607 OACC_DECLARE_CLAUSES (t
) = tmp
;
16611 case OMP_TARGET_UPDATE
:
16612 case OMP_TARGET_ENTER_DATA
:
16613 case OMP_TARGET_EXIT_DATA
:
16614 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16615 complain
, in_decl
);
16617 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16621 case OACC_ENTER_DATA
:
16622 case OACC_EXIT_DATA
:
16624 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16625 complain
, in_decl
);
16627 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16632 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16633 complain
, in_decl
);
16634 stmt
= push_stmt_list ();
16635 RECUR (OMP_BODY (t
));
16636 stmt
= pop_stmt_list (stmt
);
16639 OMP_BODY (t
) = stmt
;
16640 OMP_ORDERED_CLAUSES (t
) = tmp
;
16646 case OMP_TASKGROUP
:
16647 stmt
= push_stmt_list ();
16648 RECUR (OMP_BODY (t
));
16649 stmt
= pop_stmt_list (stmt
);
16652 OMP_BODY (t
) = stmt
;
16657 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16658 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16660 tree op1
= TREE_OPERAND (t
, 1);
16661 tree rhs1
= NULL_TREE
;
16663 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16665 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16666 op1
= TREE_OPERAND (op1
, 1);
16668 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16669 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16670 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16671 NULL_TREE
, NULL_TREE
, rhs1
,
16672 OMP_ATOMIC_SEQ_CST (t
));
16676 tree op1
= TREE_OPERAND (t
, 1);
16677 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16678 tree rhs1
= NULL_TREE
;
16679 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16680 enum tree_code opcode
= NOP_EXPR
;
16681 if (code
== OMP_ATOMIC_READ
)
16683 v
= RECUR (TREE_OPERAND (op1
, 0));
16684 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16686 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16687 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16689 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16690 v
= RECUR (TREE_OPERAND (op1
, 0));
16691 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16692 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16694 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16695 op11
= TREE_OPERAND (op11
, 1);
16697 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16698 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16699 opcode
= TREE_CODE (op11
);
16700 if (opcode
== MODIFY_EXPR
)
16706 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16707 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16709 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16710 OMP_ATOMIC_SEQ_CST (t
));
16714 case TRANSACTION_EXPR
:
16717 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16718 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16720 if (TRANSACTION_EXPR_IS_STMT (t
))
16722 tree body
= TRANSACTION_EXPR_BODY (t
);
16723 tree noex
= NULL_TREE
;
16724 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16726 noex
= MUST_NOT_THROW_COND (body
);
16727 if (noex
== NULL_TREE
)
16728 noex
= boolean_true_node
;
16729 body
= TREE_OPERAND (body
, 0);
16731 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16733 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16737 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16738 RECUR (TRANSACTION_EXPR_BODY (t
)),
16745 case MUST_NOT_THROW_EXPR
:
16747 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16748 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16749 RETURN (build_must_not_throw_expr (op0
, cond
));
16752 case EXPR_PACK_EXPANSION
:
16753 error ("invalid use of pack expansion expression");
16754 RETURN (error_mark_node
);
16756 case NONTYPE_ARGUMENT_PACK
:
16757 error ("use %<...%> to expand argument pack");
16758 RETURN (error_mark_node
);
16760 case COMPOUND_EXPR
:
16761 tmp
= RECUR (TREE_OPERAND (t
, 0));
16762 if (tmp
== NULL_TREE
)
16763 /* If the first operand was a statement, we're done with it. */
16764 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16765 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16766 RECUR (TREE_OPERAND (t
, 1)),
16769 case ANNOTATE_EXPR
:
16770 tmp
= RECUR (TREE_OPERAND (t
, 0));
16771 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16772 TREE_TYPE (tmp
), tmp
,
16773 RECUR (TREE_OPERAND (t
, 1)),
16774 RECUR (TREE_OPERAND (t
, 2))));
16777 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16779 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16780 /*function_p=*/false,
16781 integral_constant_expression_p
));
16784 RETURN (NULL_TREE
);
16786 input_location
= loc
;
16792 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16793 function. For description of the body see comment above
16794 cp_parser_omp_declare_reduction_exprs. */
16797 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16799 if (t
== NULL_TREE
|| t
== error_mark_node
)
16802 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16804 tree_stmt_iterator tsi
;
16807 memset (stmts
, 0, sizeof stmts
);
16808 for (i
= 0, tsi
= tsi_start (t
);
16809 i
< 7 && !tsi_end_p (tsi
);
16810 i
++, tsi_next (&tsi
))
16811 stmts
[i
] = tsi_stmt (tsi
);
16812 gcc_assert (tsi_end_p (tsi
));
16816 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16817 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16818 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16819 args
, complain
, in_decl
);
16820 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16821 args
, complain
, in_decl
);
16822 DECL_CONTEXT (omp_out
) = current_function_decl
;
16823 DECL_CONTEXT (omp_in
) = current_function_decl
;
16824 keep_next_level (true);
16825 tree block
= begin_omp_structured_block ();
16826 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16827 block
= finish_omp_structured_block (block
);
16828 block
= maybe_cleanup_point_expr_void (block
);
16829 add_decl_expr (omp_out
);
16830 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16831 TREE_NO_WARNING (omp_out
) = 1;
16832 add_decl_expr (omp_in
);
16833 finish_expr_stmt (block
);
16837 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16838 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16839 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16840 args
, complain
, in_decl
);
16841 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16842 args
, complain
, in_decl
);
16843 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16844 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16845 keep_next_level (true);
16846 tree block
= begin_omp_structured_block ();
16847 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16848 block
= finish_omp_structured_block (block
);
16849 block
= maybe_cleanup_point_expr_void (block
);
16850 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16851 add_decl_expr (omp_priv
);
16852 add_decl_expr (omp_orig
);
16853 finish_expr_stmt (block
);
16855 add_decl_expr (omp_orig
);
16859 /* T is a postfix-expression that is not being used in a function
16860 call. Return the substituted version of T. */
16863 tsubst_non_call_postfix_expression (tree t
, tree args
,
16864 tsubst_flags_t complain
,
16867 if (TREE_CODE (t
) == SCOPE_REF
)
16868 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16869 /*done=*/false, /*address_p=*/false);
16871 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16872 /*function_p=*/false,
16873 /*integral_constant_expression_p=*/false);
16878 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16879 instantiation context. Instantiating a pack expansion containing a lambda
16880 might result in multiple lambdas all based on the same lambda in the
16884 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16886 tree oldfn
= lambda_function (t
);
16889 tree r
= build_lambda_expr ();
16891 LAMBDA_EXPR_LOCATION (r
)
16892 = LAMBDA_EXPR_LOCATION (t
);
16893 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16894 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16895 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16897 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16898 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16900 record_lambda_scope (r
);
16902 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16903 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16905 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16906 cap
= TREE_CHAIN (cap
))
16908 tree field
= TREE_PURPOSE (cap
);
16909 if (PACK_EXPANSION_P (field
))
16910 field
= PACK_EXPANSION_PATTERN (field
);
16911 field
= tsubst_decl (field
, args
, complain
);
16913 if (field
== error_mark_node
)
16914 return error_mark_node
;
16916 tree init
= TREE_VALUE (cap
);
16917 if (PACK_EXPANSION_P (init
))
16918 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
16920 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
16921 /*fn*/false, /*constexpr*/false);
16923 if (TREE_CODE (field
) == TREE_VEC
)
16925 int len
= TREE_VEC_LENGTH (field
);
16926 gcc_assert (TREE_CODE (init
) == TREE_VEC
16927 && TREE_VEC_LENGTH (init
) == len
);
16928 for (int i
= 0; i
< len
; ++i
)
16929 LAMBDA_EXPR_CAPTURE_LIST (r
)
16930 = tree_cons (TREE_VEC_ELT (field
, i
),
16931 TREE_VEC_ELT (init
, i
),
16932 LAMBDA_EXPR_CAPTURE_LIST (r
));
16936 LAMBDA_EXPR_CAPTURE_LIST (r
)
16937 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
16939 if (id_equal (DECL_NAME (field
), "__this"))
16940 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
16944 tree type
= begin_lambda_type (r
);
16946 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16947 determine_visibility (TYPE_NAME (type
));
16949 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
16951 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
16952 ? DECL_TI_TEMPLATE (oldfn
)
16955 tree fntype
= static_fn_type (oldfn
);
16957 ++processing_template_decl
;
16958 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
16960 --processing_template_decl
;
16962 if (fntype
== error_mark_node
)
16963 r
= error_mark_node
;
16966 /* Fix the type of 'this'. */
16967 fntype
= build_memfn_type (fntype
, type
,
16968 type_memfn_quals (fntype
),
16969 type_memfn_rqual (fntype
));
16973 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
16974 fn
= DECL_TEMPLATE_RESULT (tmpl
);
16975 finish_member_declaration (tmpl
);
16980 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
16981 finish_member_declaration (fn
);
16984 /* Let finish_function set this. */
16985 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
16987 bool nested
= cfun
;
16989 push_function_context ();
16991 local_specialization_stack
s (lss_copy
);
16993 tree body
= start_lambda_function (fn
, r
);
16995 register_parameter_specializations (oldfn
, fn
);
16997 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
16998 /*constexpr*/false);
17000 finish_lambda_function (body
);
17003 pop_function_context ();
17005 /* The capture list was built up in reverse order; fix that now. */
17006 LAMBDA_EXPR_CAPTURE_LIST (r
)
17007 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17009 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17011 maybe_add_lambda_conv_op (type
);
17014 finish_struct (type
, /*attr*/NULL_TREE
);
17016 insert_pending_capture_proxies ();
17021 /* Like tsubst but deals with expressions and performs semantic
17022 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17025 tsubst_copy_and_build (tree t
,
17027 tsubst_flags_t complain
,
17030 bool integral_constant_expression_p
)
17032 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17033 #define RECUR(NODE) \
17034 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17035 /*function_p=*/false, \
17036 integral_constant_expression_p)
17041 if (t
== NULL_TREE
|| t
== error_mark_node
)
17044 loc
= input_location
;
17045 if (EXPR_HAS_LOCATION (t
))
17046 input_location
= EXPR_LOCATION (t
);
17048 /* N3276 decltype magic only applies to calls at the top level or on the
17049 right side of a comma. */
17050 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17051 complain
&= ~tf_decltype
;
17053 switch (TREE_CODE (t
))
17057 /* Fall through. */
17058 case IDENTIFIER_NODE
:
17062 bool non_integral_constant_expression_p
;
17063 const char *error_msg
;
17065 if (IDENTIFIER_CONV_OP_P (t
))
17067 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17068 t
= make_conv_op_name (new_type
);
17071 /* Look up the name. */
17072 decl
= lookup_name (t
);
17074 /* By convention, expressions use ERROR_MARK_NODE to indicate
17075 failure, not NULL_TREE. */
17076 if (decl
== NULL_TREE
)
17077 decl
= error_mark_node
;
17079 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17081 integral_constant_expression_p
,
17082 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17083 &non_integral_constant_expression_p
,
17084 /*template_p=*/false,
17086 /*address_p=*/false,
17087 /*template_arg_p=*/false,
17092 if (!function_p
&& identifier_p (decl
))
17094 if (complain
& tf_error
)
17095 unqualified_name_lookup_error (decl
);
17096 decl
= error_mark_node
;
17101 case TEMPLATE_ID_EXPR
:
17104 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17105 tree targs
= TREE_OPERAND (t
, 1);
17108 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17109 if (targs
== error_mark_node
)
17110 return error_mark_node
;
17112 if (TREE_CODE (templ
) == SCOPE_REF
)
17114 tree name
= TREE_OPERAND (templ
, 1);
17115 tree tid
= lookup_template_function (name
, targs
);
17116 TREE_OPERAND (templ
, 1) = tid
;
17120 if (variable_template_p (templ
))
17121 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17123 if (TREE_CODE (templ
) == COMPONENT_REF
)
17125 object
= TREE_OPERAND (templ
, 0);
17126 templ
= TREE_OPERAND (templ
, 1);
17129 object
= NULL_TREE
;
17130 templ
= lookup_template_function (templ
, targs
);
17133 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17134 object
, templ
, NULL_TREE
));
17136 RETURN (baselink_for_fns (templ
));
17141 tree r
= RECUR (TREE_OPERAND (t
, 0));
17143 if (REFERENCE_REF_P (t
))
17145 /* A type conversion to reference type will be enclosed in
17146 such an indirect ref, but the substitution of the cast
17147 will have also added such an indirect ref. */
17148 r
= convert_from_reference (r
);
17151 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17152 complain
|decltype_flag
);
17154 if (TREE_CODE (r
) == INDIRECT_REF
)
17155 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17162 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17163 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17164 RETURN (build_nop (type
, op0
));
17167 case IMPLICIT_CONV_EXPR
:
17169 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17170 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17171 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17173 retval
= copy_node (t
);
17174 TREE_TYPE (retval
) = type
;
17175 TREE_OPERAND (retval
, 0) = expr
;
17178 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17179 /* We'll pass this to convert_nontype_argument again, we don't need
17180 to actually perform any conversion here. */
17182 int flags
= LOOKUP_IMPLICIT
;
17183 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17184 flags
= LOOKUP_NORMAL
;
17185 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17191 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17192 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17193 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17197 case REINTERPRET_CAST_EXPR
:
17198 case CONST_CAST_EXPR
:
17199 case DYNAMIC_CAST_EXPR
:
17200 case STATIC_CAST_EXPR
:
17203 tree op
, r
= NULL_TREE
;
17205 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17206 if (integral_constant_expression_p
17207 && !cast_valid_in_integral_constant_expression_p (type
))
17209 if (complain
& tf_error
)
17210 error ("a cast to a type other than an integral or "
17211 "enumeration type cannot appear in a constant-expression");
17212 RETURN (error_mark_node
);
17215 op
= RECUR (TREE_OPERAND (t
, 0));
17217 warning_sentinel
s(warn_useless_cast
);
17218 warning_sentinel
s2(warn_ignored_qualifiers
);
17219 switch (TREE_CODE (t
))
17222 r
= build_functional_cast (type
, op
, complain
);
17224 case REINTERPRET_CAST_EXPR
:
17225 r
= build_reinterpret_cast (type
, op
, complain
);
17227 case CONST_CAST_EXPR
:
17228 r
= build_const_cast (type
, op
, complain
);
17230 case DYNAMIC_CAST_EXPR
:
17231 r
= build_dynamic_cast (type
, op
, complain
);
17233 case STATIC_CAST_EXPR
:
17234 r
= build_static_cast (type
, op
, complain
);
17237 gcc_unreachable ();
17243 case POSTDECREMENT_EXPR
:
17244 case POSTINCREMENT_EXPR
:
17245 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17246 args
, complain
, in_decl
);
17247 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17248 complain
|decltype_flag
));
17250 case PREDECREMENT_EXPR
:
17251 case PREINCREMENT_EXPR
:
17255 case TRUTH_NOT_EXPR
:
17256 case UNARY_PLUS_EXPR
: /* Unary + */
17257 case REALPART_EXPR
:
17258 case IMAGPART_EXPR
:
17259 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17260 RECUR (TREE_OPERAND (t
, 0)),
17261 complain
|decltype_flag
));
17263 case FIX_TRUNC_EXPR
:
17264 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17268 op1
= TREE_OPERAND (t
, 0);
17269 if (TREE_CODE (op1
) == LABEL_DECL
)
17270 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17271 EXPR_LOCATION (op1
)));
17272 if (TREE_CODE (op1
) == SCOPE_REF
)
17273 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17274 /*done=*/true, /*address_p=*/true);
17276 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17278 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17279 complain
|decltype_flag
));
17284 case TRUNC_DIV_EXPR
:
17285 case CEIL_DIV_EXPR
:
17286 case FLOOR_DIV_EXPR
:
17287 case ROUND_DIV_EXPR
:
17288 case EXACT_DIV_EXPR
:
17292 case TRUNC_MOD_EXPR
:
17293 case FLOOR_MOD_EXPR
:
17294 case TRUTH_ANDIF_EXPR
:
17295 case TRUTH_ORIF_EXPR
:
17296 case TRUTH_AND_EXPR
:
17297 case TRUTH_OR_EXPR
:
17313 warning_sentinel
s1(warn_type_limits
);
17314 warning_sentinel
s2(warn_div_by_zero
);
17315 warning_sentinel
s3(warn_logical_op
);
17316 warning_sentinel
s4(warn_tautological_compare
);
17317 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17318 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17319 tree r
= build_x_binary_op
17320 (input_location
, TREE_CODE (t
),
17322 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17324 : TREE_CODE (TREE_OPERAND (t
, 0))),
17326 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17328 : TREE_CODE (TREE_OPERAND (t
, 1))),
17330 complain
|decltype_flag
);
17331 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17332 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17337 case POINTER_PLUS_EXPR
:
17339 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17340 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17341 return fold_build_pointer_plus (op0
, op1
);
17345 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17346 /*address_p=*/false));
17348 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17349 args
, complain
, in_decl
);
17350 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17351 RECUR (TREE_OPERAND (t
, 1)),
17352 complain
|decltype_flag
));
17355 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17356 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17357 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17364 op1
= TREE_OPERAND (t
, 0);
17365 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17366 op1
= TREE_TYPE (op1
);
17369 /* When there are no ARGS, we are trying to evaluate a
17370 non-dependent expression from the parser. Trying to do
17371 the substitutions may not work. */
17373 op1
= TREE_TYPE (op1
);
17377 ++cp_unevaluated_operand
;
17378 ++c_inhibit_evaluation_warnings
;
17380 op1
= tsubst (op1
, args
, complain
, in_decl
);
17382 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17383 /*function_p=*/false,
17384 /*integral_constant_expression_p=*/
17386 --cp_unevaluated_operand
;
17387 --c_inhibit_evaluation_warnings
;
17390 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17391 complain
& tf_error
);
17393 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17394 complain
& tf_error
);
17395 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17397 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17399 if (!processing_template_decl
&& TYPE_P (op1
))
17401 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17402 build1 (NOP_EXPR
, op1
, error_mark_node
));
17403 SIZEOF_EXPR_TYPE_P (r
) = 1;
17406 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17407 TREE_SIDE_EFFECTS (r
) = 0;
17408 TREE_READONLY (r
) = 1;
17410 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17415 case AT_ENCODE_EXPR
:
17417 op1
= TREE_OPERAND (t
, 0);
17418 ++cp_unevaluated_operand
;
17419 ++c_inhibit_evaluation_warnings
;
17420 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17421 /*function_p=*/false,
17422 /*integral_constant_expression_p=*/false);
17423 --cp_unevaluated_operand
;
17424 --c_inhibit_evaluation_warnings
;
17425 RETURN (objc_build_encode_expr (op1
));
17428 case NOEXCEPT_EXPR
:
17429 op1
= TREE_OPERAND (t
, 0);
17430 ++cp_unevaluated_operand
;
17431 ++c_inhibit_evaluation_warnings
;
17432 ++cp_noexcept_operand
;
17433 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17434 /*function_p=*/false,
17435 /*integral_constant_expression_p=*/false);
17436 --cp_unevaluated_operand
;
17437 --c_inhibit_evaluation_warnings
;
17438 --cp_noexcept_operand
;
17439 RETURN (finish_noexcept_expr (op1
, complain
));
17443 warning_sentinel
s(warn_div_by_zero
);
17444 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17445 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17446 tree r
= build_x_modify_expr
17447 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17448 complain
|decltype_flag
);
17449 /* TREE_NO_WARNING must be set if either the expression was
17450 parenthesized or it uses an operator such as >>= rather
17451 than plain assignment. In the former case, it was already
17452 set and must be copied. In the latter case,
17453 build_x_modify_expr sets it and it must not be reset
17455 if (TREE_NO_WARNING (t
))
17456 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17462 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17463 args
, complain
, in_decl
);
17464 /* Remember that there was a reference to this entity. */
17466 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17467 RETURN (error_mark_node
);
17468 RETURN (build_x_arrow (input_location
, op1
, complain
));
17472 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17473 tree init
= RECUR (TREE_OPERAND (t
, 3));
17474 vec
<tree
, va_gc
> *placement_vec
;
17475 vec
<tree
, va_gc
> *init_vec
;
17478 if (placement
== NULL_TREE
)
17479 placement_vec
= NULL
;
17482 placement_vec
= make_tree_vector ();
17483 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17484 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17487 /* If there was an initializer in the original tree, but it
17488 instantiated to an empty list, then we should pass a
17489 non-NULL empty vector to tell build_new that it was an
17490 empty initializer() rather than no initializer. This can
17491 only happen when the initializer is a pack expansion whose
17492 parameter packs are of length zero. */
17493 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17497 init_vec
= make_tree_vector ();
17498 if (init
== void_node
)
17499 gcc_assert (init_vec
!= NULL
);
17502 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17503 vec_safe_push (init_vec
, TREE_VALUE (init
));
17507 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17508 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17509 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17510 NEW_EXPR_USE_GLOBAL (t
),
17513 if (placement_vec
!= NULL
)
17514 release_tree_vector (placement_vec
);
17515 if (init_vec
!= NULL
)
17516 release_tree_vector (init_vec
);
17523 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17524 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17525 RETURN (delete_sanity (op0
, op1
,
17526 DELETE_EXPR_USE_VEC (t
),
17527 DELETE_EXPR_USE_GLOBAL (t
),
17531 case COMPOUND_EXPR
:
17533 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17534 complain
& ~tf_decltype
, in_decl
,
17535 /*function_p=*/false,
17536 integral_constant_expression_p
);
17537 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17539 RECUR (TREE_OPERAND (t
, 1)),
17540 complain
|decltype_flag
));
17546 vec
<tree
, va_gc
> *call_args
;
17547 unsigned int nargs
, i
;
17552 function
= CALL_EXPR_FN (t
);
17553 /* Internal function with no arguments. */
17554 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17557 /* When we parsed the expression, we determined whether or
17558 not Koenig lookup should be performed. */
17559 koenig_p
= KOENIG_LOOKUP_P (t
);
17560 if (function
== NULL_TREE
)
17563 qualified_p
= false;
17565 else if (TREE_CODE (function
) == SCOPE_REF
)
17567 qualified_p
= true;
17568 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17570 /*address_p=*/false);
17572 else if (koenig_p
&& identifier_p (function
))
17574 /* Do nothing; calling tsubst_copy_and_build on an identifier
17575 would incorrectly perform unqualified lookup again.
17577 Note that we can also have an IDENTIFIER_NODE if the earlier
17578 unqualified lookup found a member function; in that case
17579 koenig_p will be false and we do want to do the lookup
17580 again to find the instantiated member function.
17582 FIXME but doing that causes c++/15272, so we need to stop
17583 using IDENTIFIER_NODE in that situation. */
17584 qualified_p
= false;
17588 if (TREE_CODE (function
) == COMPONENT_REF
)
17590 tree op
= TREE_OPERAND (function
, 1);
17592 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17593 || (BASELINK_P (op
)
17594 && BASELINK_QUALIFIED_P (op
)));
17597 qualified_p
= false;
17599 if (TREE_CODE (function
) == ADDR_EXPR
17600 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17601 /* Avoid error about taking the address of a constructor. */
17602 function
= TREE_OPERAND (function
, 0);
17604 function
= tsubst_copy_and_build (function
, args
, complain
,
17607 integral_constant_expression_p
);
17609 if (BASELINK_P (function
))
17610 qualified_p
= true;
17613 nargs
= call_expr_nargs (t
);
17614 call_args
= make_tree_vector ();
17615 for (i
= 0; i
< nargs
; ++i
)
17617 tree arg
= CALL_EXPR_ARG (t
, i
);
17619 if (!PACK_EXPANSION_P (arg
))
17620 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17623 /* Expand the pack expansion and push each entry onto
17625 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17626 if (TREE_CODE (arg
) == TREE_VEC
)
17628 unsigned int len
, j
;
17630 len
= TREE_VEC_LENGTH (arg
);
17631 for (j
= 0; j
< len
; ++j
)
17633 tree value
= TREE_VEC_ELT (arg
, j
);
17634 if (value
!= NULL_TREE
)
17635 value
= convert_from_reference (value
);
17636 vec_safe_push (call_args
, value
);
17641 /* A partial substitution. Add one entry. */
17642 vec_safe_push (call_args
, arg
);
17647 /* We do not perform argument-dependent lookup if normal
17648 lookup finds a non-function, in accordance with the
17649 expected resolution of DR 218. */
17651 && ((is_overloaded_fn (function
)
17652 /* If lookup found a member function, the Koenig lookup is
17653 not appropriate, even if an unqualified-name was used
17654 to denote the function. */
17655 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17656 || identifier_p (function
))
17657 /* Only do this when substitution turns a dependent call
17658 into a non-dependent call. */
17659 && type_dependent_expression_p_push (t
)
17660 && !any_type_dependent_arguments_p (call_args
))
17661 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17663 if (function
!= NULL_TREE
17664 && identifier_p (function
)
17665 && !any_type_dependent_arguments_p (call_args
))
17667 if (koenig_p
&& (complain
& tf_warning_or_error
))
17669 /* For backwards compatibility and good diagnostics, try
17670 the unqualified lookup again if we aren't in SFINAE
17672 tree unq
= (tsubst_copy_and_build
17673 (function
, args
, complain
, in_decl
, true,
17674 integral_constant_expression_p
));
17675 if (unq
== error_mark_node
)
17677 release_tree_vector (call_args
);
17678 RETURN (error_mark_node
);
17681 if (unq
!= function
)
17683 /* In a lambda fn, we have to be careful to not
17684 introduce new this captures. Legacy code can't
17685 be using lambdas anyway, so it's ok to be
17687 bool in_lambda
= (current_class_type
17688 && LAMBDA_TYPE_P (current_class_type
));
17689 char const *const msg
17690 = G_("%qD was not declared in this scope, "
17691 "and no declarations were found by "
17692 "argument-dependent lookup at the point "
17693 "of instantiation");
17697 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17700 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17706 if (INDIRECT_REF_P (fn
))
17707 fn
= TREE_OPERAND (fn
, 0);
17708 if (is_overloaded_fn (fn
))
17709 fn
= get_first_fn (fn
);
17712 /* Can't say anything more. */;
17713 else if (DECL_CLASS_SCOPE_P (fn
))
17715 location_t loc
= EXPR_LOC_OR_LOC (t
,
17718 "declarations in dependent base %qT are "
17719 "not found by unqualified lookup",
17720 DECL_CLASS_CONTEXT (fn
));
17721 if (current_class_ptr
)
17723 "use %<this->%D%> instead", function
);
17726 "use %<%T::%D%> instead",
17727 current_class_name
, function
);
17730 inform (DECL_SOURCE_LOCATION (fn
),
17731 "%qD declared here, later in the "
17732 "translation unit", fn
);
17735 release_tree_vector (call_args
);
17736 RETURN (error_mark_node
);
17743 if (identifier_p (function
))
17745 if (complain
& tf_error
)
17746 unqualified_name_lookup_error (function
);
17747 release_tree_vector (call_args
);
17748 RETURN (error_mark_node
);
17752 /* Remember that there was a reference to this entity. */
17753 if (function
!= NULL_TREE
17754 && DECL_P (function
)
17755 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17757 release_tree_vector (call_args
);
17758 RETURN (error_mark_node
);
17761 /* Put back tf_decltype for the actual call. */
17762 complain
|= decltype_flag
;
17764 if (function
== NULL_TREE
)
17765 switch (CALL_EXPR_IFN (t
))
17768 gcc_assert (nargs
== 1);
17769 if (vec_safe_length (call_args
) != 1)
17771 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17772 "wrong number of arguments to "
17773 "%<__builtin_launder%>");
17774 ret
= error_mark_node
;
17777 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17779 (*call_args
)[0], complain
);
17783 /* Unsupported internal function with arguments. */
17784 gcc_unreachable ();
17786 else if (TREE_CODE (function
) == OFFSET_REF
)
17787 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17789 else if (TREE_CODE (function
) == COMPONENT_REF
)
17791 tree instance
= TREE_OPERAND (function
, 0);
17792 tree fn
= TREE_OPERAND (function
, 1);
17794 if (processing_template_decl
17795 && (type_dependent_expression_p (instance
)
17796 || (!BASELINK_P (fn
)
17797 && TREE_CODE (fn
) != FIELD_DECL
)
17798 || type_dependent_expression_p (fn
)
17799 || any_type_dependent_arguments_p (call_args
)))
17800 ret
= build_min_nt_call_vec (function
, call_args
);
17801 else if (!BASELINK_P (fn
))
17802 ret
= finish_call_expr (function
, &call_args
,
17803 /*disallow_virtual=*/false,
17804 /*koenig_p=*/false,
17807 ret
= (build_new_method_call
17809 &call_args
, NULL_TREE
,
17810 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17815 ret
= finish_call_expr (function
, &call_args
,
17816 /*disallow_virtual=*/qualified_p
,
17820 release_tree_vector (call_args
);
17822 if (ret
!= error_mark_node
)
17824 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17825 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17826 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17827 bool thk
= CALL_FROM_THUNK_P (t
);
17828 if (op
|| ord
|| rev
|| thk
)
17830 function
= extract_call_expr (ret
);
17831 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17832 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17833 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17836 if (TREE_CODE (function
) == CALL_EXPR
)
17837 CALL_FROM_THUNK_P (function
) = true;
17839 AGGR_INIT_FROM_THUNK_P (function
) = true;
17840 /* The thunk location is not interesting. */
17841 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17851 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17852 tree folded_cond
= fold_non_dependent_expr (cond
);
17855 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17857 if (integer_zerop (folded_cond
))
17859 ++c_inhibit_evaluation_warnings
;
17860 exp1
= RECUR (TREE_OPERAND (t
, 1));
17861 --c_inhibit_evaluation_warnings
;
17862 exp2
= RECUR (TREE_OPERAND (t
, 2));
17866 exp1
= RECUR (TREE_OPERAND (t
, 1));
17867 ++c_inhibit_evaluation_warnings
;
17868 exp2
= RECUR (TREE_OPERAND (t
, 2));
17869 --c_inhibit_evaluation_warnings
;
17871 cond
= folded_cond
;
17875 exp1
= RECUR (TREE_OPERAND (t
, 1));
17876 exp2
= RECUR (TREE_OPERAND (t
, 2));
17879 warning_sentinel
s(warn_duplicated_branches
);
17880 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17881 cond
, exp1
, exp2
, complain
));
17884 case PSEUDO_DTOR_EXPR
:
17886 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17887 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17888 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17889 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17895 tree purpose
, value
, chain
;
17897 if (t
== void_list_node
)
17900 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17901 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17903 /* We have pack expansions, so expand those and
17904 create a new list out of it. */
17905 tree purposevec
= NULL_TREE
;
17906 tree valuevec
= NULL_TREE
;
17910 /* Expand the argument expressions. */
17911 if (TREE_PURPOSE (t
))
17912 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17913 complain
, in_decl
);
17914 if (TREE_VALUE (t
))
17915 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17916 complain
, in_decl
);
17918 /* Build the rest of the list. */
17919 chain
= TREE_CHAIN (t
);
17920 if (chain
&& chain
!= void_type_node
)
17921 chain
= RECUR (chain
);
17923 /* Determine the number of arguments. */
17924 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17926 len
= TREE_VEC_LENGTH (purposevec
);
17927 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17929 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17930 len
= TREE_VEC_LENGTH (valuevec
);
17933 /* Since we only performed a partial substitution into
17934 the argument pack, we only RETURN (a single list
17936 if (purposevec
== TREE_PURPOSE (t
)
17937 && valuevec
== TREE_VALUE (t
)
17938 && chain
== TREE_CHAIN (t
))
17941 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17944 /* Convert the argument vectors into a TREE_LIST */
17948 /* Grab the Ith values. */
17950 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17953 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17956 /* Build the list (backwards). */
17957 chain
= tree_cons (purpose
, value
, chain
);
17963 purpose
= TREE_PURPOSE (t
);
17965 purpose
= RECUR (purpose
);
17966 value
= TREE_VALUE (t
);
17968 value
= RECUR (value
);
17969 chain
= TREE_CHAIN (t
);
17970 if (chain
&& chain
!= void_type_node
)
17971 chain
= RECUR (chain
);
17972 if (purpose
== TREE_PURPOSE (t
)
17973 && value
== TREE_VALUE (t
)
17974 && chain
== TREE_CHAIN (t
))
17976 RETURN (tree_cons (purpose
, value
, chain
));
17979 case COMPONENT_REF
:
17986 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17987 args
, complain
, in_decl
);
17988 /* Remember that there was a reference to this entity. */
17989 if (DECL_P (object
)
17990 && !mark_used (object
, complain
) && !(complain
& tf_error
))
17991 RETURN (error_mark_node
);
17992 object_type
= TREE_TYPE (object
);
17994 member
= TREE_OPERAND (t
, 1);
17995 if (BASELINK_P (member
))
17996 member
= tsubst_baselink (member
,
17997 non_reference (TREE_TYPE (object
)),
17998 args
, complain
, in_decl
);
18000 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18001 if (member
== error_mark_node
)
18002 RETURN (error_mark_node
);
18004 if (TREE_CODE (member
) == FIELD_DECL
)
18006 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18007 if (TREE_CODE (r
) == COMPONENT_REF
)
18008 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18011 else if (type_dependent_expression_p (object
))
18012 /* We can't do much here. */;
18013 else if (!CLASS_TYPE_P (object_type
))
18015 if (scalarish_type_p (object_type
))
18017 tree s
= NULL_TREE
;
18018 tree dtor
= member
;
18020 if (TREE_CODE (dtor
) == SCOPE_REF
)
18022 s
= TREE_OPERAND (dtor
, 0);
18023 dtor
= TREE_OPERAND (dtor
, 1);
18025 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18027 dtor
= TREE_OPERAND (dtor
, 0);
18029 RETURN (finish_pseudo_destructor_expr
18030 (object
, s
, dtor
, input_location
));
18034 else if (TREE_CODE (member
) == SCOPE_REF
18035 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18037 /* Lookup the template functions now that we know what the
18039 tree scope
= TREE_OPERAND (member
, 0);
18040 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18041 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18042 member
= lookup_qualified_name (scope
, tmpl
,
18043 /*is_type_p=*/false,
18044 /*complain=*/false);
18045 if (BASELINK_P (member
))
18047 BASELINK_FUNCTIONS (member
)
18048 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18050 member
= (adjust_result_of_qualified_name_lookup
18051 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18056 qualified_name_lookup_error (scope
, tmpl
, member
,
18058 RETURN (error_mark_node
);
18061 else if (TREE_CODE (member
) == SCOPE_REF
18062 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18063 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18065 if (complain
& tf_error
)
18067 if (TYPE_P (TREE_OPERAND (member
, 0)))
18068 error ("%qT is not a class or namespace",
18069 TREE_OPERAND (member
, 0));
18071 error ("%qD is not a class or namespace",
18072 TREE_OPERAND (member
, 0));
18074 RETURN (error_mark_node
);
18077 r
= finish_class_member_access_expr (object
, member
,
18078 /*template_p=*/false,
18080 if (TREE_CODE (r
) == COMPONENT_REF
)
18081 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18086 RETURN (build_throw
18087 (RECUR (TREE_OPERAND (t
, 0))));
18091 vec
<constructor_elt
, va_gc
> *n
;
18092 constructor_elt
*ce
;
18093 unsigned HOST_WIDE_INT idx
;
18094 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18095 bool process_index_p
;
18097 bool need_copy_p
= false;
18100 if (type
== error_mark_node
)
18101 RETURN (error_mark_node
);
18103 /* digest_init will do the wrong thing if we let it. */
18104 if (type
&& TYPE_PTRMEMFUNC_P (type
))
18107 /* We do not want to process the index of aggregate
18108 initializers as they are identifier nodes which will be
18109 looked up by digest_init. */
18110 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18112 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18113 newlen
= vec_safe_length (n
);
18114 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18116 if (ce
->index
&& process_index_p
18117 /* An identifier index is looked up in the type
18118 being initialized, not the current scope. */
18119 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18120 ce
->index
= RECUR (ce
->index
);
18122 if (PACK_EXPANSION_P (ce
->value
))
18124 /* Substitute into the pack expansion. */
18125 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18128 if (ce
->value
== error_mark_node
18129 || PACK_EXPANSION_P (ce
->value
))
18131 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18132 /* Just move the argument into place. */
18133 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18136 /* Update the length of the final CONSTRUCTOR
18137 arguments vector, and note that we will need to
18139 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18140 need_copy_p
= true;
18144 ce
->value
= RECUR (ce
->value
);
18149 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18151 vec_alloc (n
, newlen
);
18152 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18154 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18156 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18157 for (i
= 0; i
< len
; ++i
)
18158 CONSTRUCTOR_APPEND_ELT (n
, 0,
18159 TREE_VEC_ELT (ce
->value
, i
));
18162 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18166 r
= build_constructor (init_list_type_node
, n
);
18167 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18169 if (TREE_HAS_CONSTRUCTOR (t
))
18171 fcl_t cl
= fcl_functional
;
18172 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18174 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18177 TREE_TYPE (r
) = type
;
18183 tree operand_0
= TREE_OPERAND (t
, 0);
18184 if (TYPE_P (operand_0
))
18186 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18187 RETURN (get_typeid (operand_0
, complain
));
18191 operand_0
= RECUR (operand_0
);
18192 RETURN (build_typeid (operand_0
, complain
));
18199 else if (DECL_PACK_P (t
))
18201 /* We don't build decls for an instantiation of a
18202 variadic capture proxy, we instantiate the elements
18204 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
18205 return RECUR (DECL_VALUE_EXPR (t
));
18211 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18212 /* ??? We're doing a subset of finish_id_expression here. */
18214 && !processing_template_decl
18215 && !cp_unevaluated_operand
18216 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18217 && CP_DECL_THREAD_LOCAL_P (r
))
18219 if (tree wrap
= get_tls_wrapper_fn (r
))
18220 /* Replace an evaluated use of the thread_local variable with
18221 a call to its wrapper. */
18222 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18224 else if (outer_automatic_var_p (r
))
18225 r
= process_outer_var_ref (r
, complain
);
18227 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18228 /* If the original type was a reference, we'll be wrapped in
18229 the appropriate INDIRECT_REF. */
18230 r
= convert_from_reference (r
);
18236 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18237 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18238 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18241 case OFFSETOF_EXPR
:
18244 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18245 in_decl
, /*function_p=*/false,
18246 /*integral_constant_expression_p=*/false);
18247 RETURN (finish_offsetof (object_ptr
,
18248 RECUR (TREE_OPERAND (t
, 0)),
18249 EXPR_LOCATION (t
)));
18252 case ADDRESSOF_EXPR
:
18253 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18254 RECUR (TREE_OPERAND (t
, 0)), complain
));
18258 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18259 complain
, in_decl
);
18261 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18262 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18263 type2
= RECUR (type2
);
18265 type2
= tsubst (type2
, args
, complain
, in_decl
);
18267 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18272 tree old_stmt_expr
= cur_stmt_expr
;
18273 tree stmt_expr
= begin_stmt_expr ();
18275 cur_stmt_expr
= stmt_expr
;
18276 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18277 integral_constant_expression_p
);
18278 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18279 cur_stmt_expr
= old_stmt_expr
;
18281 /* If the resulting list of expression statement is empty,
18282 fold it further into void_node. */
18283 if (empty_expr_stmt_p (stmt_expr
))
18284 stmt_expr
= void_node
;
18286 RETURN (stmt_expr
);
18291 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18293 RETURN (build_lambda_object (r
));
18297 /* We can get here for a constant initializer of non-dependent type.
18298 FIXME stop folding in cp_parser_initializer_clause. */
18300 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18305 case TRANSACTION_EXPR
:
18306 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18307 integral_constant_expression_p
));
18310 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18312 case VEC_PERM_EXPR
:
18314 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18315 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18316 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18317 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18321 case REQUIRES_EXPR
:
18322 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18324 case NON_LVALUE_EXPR
:
18325 case VIEW_CONVERT_EXPR
:
18326 /* We should only see these for location wrapper nodes, or within
18327 instantiate_non_dependent_expr (when args is NULL_TREE). */
18328 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18329 if (location_wrapper_p (t
))
18330 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18331 EXPR_LOCATION (t
)));
18335 /* Handle Objective-C++ constructs, if appropriate. */
18338 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18339 in_decl
, /*function_p=*/false);
18343 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18349 input_location
= loc
;
18353 /* Verify that the instantiated ARGS are valid. For type arguments,
18354 make sure that the type's linkage is ok. For non-type arguments,
18355 make sure they are constants if they are integral or enumerations.
18356 Emit an error under control of COMPLAIN, and return TRUE on error. */
18359 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18361 if (dependent_template_arg_p (t
))
18363 if (ARGUMENT_PACK_P (t
))
18365 tree vec
= ARGUMENT_PACK_ARGS (t
);
18366 int len
= TREE_VEC_LENGTH (vec
);
18367 bool result
= false;
18370 for (i
= 0; i
< len
; ++i
)
18371 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18375 else if (TYPE_P (t
))
18377 /* [basic.link]: A name with no linkage (notably, the name
18378 of a class or enumeration declared in a local scope)
18379 shall not be used to declare an entity with linkage.
18380 This implies that names with no linkage cannot be used as
18383 DR 757 relaxes this restriction for C++0x. */
18384 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18385 : no_linkage_check (t
, /*relaxed_p=*/false));
18389 /* DR 488 makes use of a type with no linkage cause
18390 type deduction to fail. */
18391 if (complain
& tf_error
)
18393 if (TYPE_UNNAMED_P (nt
))
18394 error ("%qT is/uses unnamed type", t
);
18396 error ("template argument for %qD uses local type %qT",
18401 /* In order to avoid all sorts of complications, we do not
18402 allow variably-modified types as template arguments. */
18403 else if (variably_modified_type_p (t
, NULL_TREE
))
18405 if (complain
& tf_error
)
18406 error ("%qT is a variably modified type", t
);
18410 /* Class template and alias template arguments should be OK. */
18411 else if (DECL_TYPE_TEMPLATE_P (t
))
18413 /* A non-type argument of integral or enumerated type must be a
18415 else if (TREE_TYPE (t
)
18416 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18417 && !REFERENCE_REF_P (t
)
18418 && !TREE_CONSTANT (t
))
18420 if (complain
& tf_error
)
18421 error ("integral expression %qE is not constant", t
);
18428 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18430 int ix
, len
= DECL_NTPARMS (tmpl
);
18431 bool result
= false;
18433 for (ix
= 0; ix
!= len
; ix
++)
18435 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18438 if (result
&& (complain
& tf_error
))
18439 error (" trying to instantiate %qD", tmpl
);
18443 /* We're out of SFINAE context now, so generate diagnostics for the access
18444 errors we saw earlier when instantiating D from TMPL and ARGS. */
18447 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18449 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18450 tree type
= TREE_TYPE (pattern
);
18451 location_t loc
= input_location
;
18453 push_access_scope (d
);
18454 push_deferring_access_checks (dk_no_deferred
);
18455 input_location
= DECL_SOURCE_LOCATION (pattern
);
18456 tsubst (type
, args
, tf_warning_or_error
, d
);
18457 input_location
= loc
;
18458 pop_deferring_access_checks ();
18459 pop_access_scope (d
);
18462 /* Instantiate the indicated variable, function, or alias template TMPL with
18463 the template arguments in TARG_PTR. */
18466 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18468 tree targ_ptr
= orig_args
;
18472 bool access_ok
= true;
18474 if (tmpl
== error_mark_node
)
18475 return error_mark_node
;
18477 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18479 /* If this function is a clone, handle it specially. */
18480 if (DECL_CLONED_FUNCTION_P (tmpl
))
18485 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18486 DECL_CLONED_FUNCTION. */
18487 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18488 targ_ptr
, complain
);
18489 if (spec
== error_mark_node
)
18490 return error_mark_node
;
18492 /* Look for the clone. */
18493 FOR_EACH_CLONE (clone
, spec
)
18494 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18496 /* We should always have found the clone by now. */
18497 gcc_unreachable ();
18501 if (targ_ptr
== error_mark_node
)
18502 return error_mark_node
;
18504 /* Check to see if we already have this specialization. */
18505 gen_tmpl
= most_general_template (tmpl
);
18506 if (TMPL_ARGS_DEPTH (targ_ptr
)
18507 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18508 /* targ_ptr only has the innermost template args, so add the outer ones
18509 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18510 the case of a non-dependent call within a template definition). */
18511 targ_ptr
= (add_outermost_template_args
18512 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18515 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18516 but it doesn't seem to be on the hot path. */
18517 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18519 gcc_assert (tmpl
== gen_tmpl
18520 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18522 || fndecl
== NULL_TREE
);
18524 if (spec
!= NULL_TREE
)
18526 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18528 if (complain
& tf_error
)
18529 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18530 return error_mark_node
;
18535 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18537 return error_mark_node
;
18539 /* We are building a FUNCTION_DECL, during which the access of its
18540 parameters and return types have to be checked. However this
18541 FUNCTION_DECL which is the desired context for access checking
18542 is not built yet. We solve this chicken-and-egg problem by
18543 deferring all checks until we have the FUNCTION_DECL. */
18544 push_deferring_access_checks (dk_deferred
);
18546 /* Instantiation of the function happens in the context of the function
18547 template, not the context of the overload resolution we're doing. */
18548 push_to_top_level ();
18549 /* If there are dependent arguments, e.g. because we're doing partial
18550 ordering, make sure processing_template_decl stays set. */
18551 if (uses_template_parms (targ_ptr
))
18552 ++processing_template_decl
;
18553 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18555 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18556 complain
, gen_tmpl
, true);
18557 push_nested_class (ctx
);
18560 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18562 fndecl
= NULL_TREE
;
18563 if (VAR_P (pattern
))
18565 /* We need to determine if we're using a partial or explicit
18566 specialization now, because the type of the variable could be
18568 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18569 tree elt
= most_specialized_partial_spec (tid
, complain
);
18570 if (elt
== error_mark_node
)
18571 pattern
= error_mark_node
;
18574 tree partial_tmpl
= TREE_VALUE (elt
);
18575 tree partial_args
= TREE_PURPOSE (elt
);
18576 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18577 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18581 /* Substitute template parameters to obtain the specialization. */
18582 if (fndecl
== NULL_TREE
)
18583 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18584 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18585 pop_nested_class ();
18586 pop_from_top_level ();
18588 if (fndecl
== error_mark_node
)
18590 pop_deferring_access_checks ();
18591 return error_mark_node
;
18594 /* The DECL_TI_TEMPLATE should always be the immediate parent
18595 template, not the most general template. */
18596 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18597 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18599 /* Now we know the specialization, compute access previously
18600 deferred. Do no access control for inheriting constructors,
18601 as we already checked access for the inherited constructor. */
18602 if (!(flag_new_inheriting_ctors
18603 && DECL_INHERITED_CTOR (fndecl
)))
18605 push_access_scope (fndecl
);
18606 if (!perform_deferred_access_checks (complain
))
18608 pop_access_scope (fndecl
);
18610 pop_deferring_access_checks ();
18612 /* If we've just instantiated the main entry point for a function,
18613 instantiate all the alternate entry points as well. We do this
18614 by cloning the instantiation of the main entry point, not by
18615 instantiating the template clones. */
18616 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18617 clone_function_decl (fndecl
, /*update_methods=*/false);
18621 if (!(complain
& tf_error
))
18623 /* Remember to reinstantiate when we're out of SFINAE so the user
18624 can see the errors. */
18625 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18627 return error_mark_node
;
18632 /* Wrapper for instantiate_template_1. */
18635 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18638 timevar_push (TV_TEMPLATE_INST
);
18639 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18640 timevar_pop (TV_TEMPLATE_INST
);
18644 /* Instantiate the alias template TMPL with ARGS. Also push a template
18645 instantiation level, which instantiate_template doesn't do because
18646 functions and variables have sufficient context established by the
18650 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18652 struct pending_template
*old_last_pend
= last_pending_template
;
18653 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18654 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18655 return error_mark_node
;
18656 tree tinst
= build_tree_list (tmpl
, args
);
18657 if (!push_tinst_level (tinst
))
18660 return error_mark_node
;
18664 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18665 args
, tmpl
, complain
,
18666 /*require_all_args=*/true,
18667 /*use_default_args=*/true);
18669 tree r
= instantiate_template (tmpl
, args
, complain
);
18670 pop_tinst_level ();
18671 /* We can't free this if a pending_template entry or last_error_tinst_level
18672 is pointing at it. */
18673 if (last_pending_template
== old_last_pend
18674 && last_error_tinst_level
== old_error_tinst
)
18680 /* PARM is a template parameter pack for FN. Returns true iff
18681 PARM is used in a deducible way in the argument list of FN. */
18684 pack_deducible_p (tree parm
, tree fn
)
18686 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18687 for (; t
; t
= TREE_CHAIN (t
))
18689 tree type
= TREE_VALUE (t
);
18691 if (!PACK_EXPANSION_P (type
))
18693 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18694 packs
; packs
= TREE_CHAIN (packs
))
18695 if (template_args_equal (TREE_VALUE (packs
), parm
))
18697 /* The template parameter pack is used in a function parameter
18698 pack. If this is the end of the parameter list, the
18699 template parameter pack is deducible. */
18700 if (TREE_CHAIN (t
) == void_list_node
)
18703 /* Otherwise, not. Well, it could be deduced from
18704 a non-pack parameter, but doing so would end up with
18705 a deduction mismatch, so don't bother. */
18709 /* The template parameter pack isn't used in any function parameter
18710 packs, but it might be used deeper, e.g. tuple<Args...>. */
18714 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18715 NARGS elements of the arguments that are being used when calling
18716 it. TARGS is a vector into which the deduced template arguments
18719 Returns either a FUNCTION_DECL for the matching specialization of FN or
18720 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18721 true, diagnostics will be printed to explain why it failed.
18723 If FN is a conversion operator, or we are trying to produce a specific
18724 specialization, RETURN_TYPE is the return type desired.
18726 The EXPLICIT_TARGS are explicit template arguments provided via a
18729 The parameter STRICT is one of:
18732 We are deducing arguments for a function call, as in
18733 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18734 deducing arguments for a call to the result of a conversion
18735 function template, as in [over.call.object].
18738 We are deducing arguments for a conversion function, as in
18739 [temp.deduct.conv].
18742 We are deducing arguments when doing an explicit instantiation
18743 as in [temp.explicit], when determining an explicit specialization
18744 as in [temp.expl.spec], or when taking the address of a function
18745 template, as in [temp.deduct.funcaddr]. */
18748 fn_type_unification (tree fn
,
18749 tree explicit_targs
,
18752 unsigned int nargs
,
18754 unification_kind_t strict
,
18761 tree decl
= NULL_TREE
;
18762 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18764 static int deduction_depth
;
18765 struct pending_template
*old_last_pend
= last_pending_template
;
18766 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18769 if (flag_new_inheriting_ctors
)
18770 fn
= strip_inheriting_ctors (fn
);
18772 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18774 tree r
= error_mark_node
;
18776 tree full_targs
= targs
;
18777 if (TMPL_ARGS_DEPTH (targs
)
18778 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18779 full_targs
= (add_outermost_template_args
18780 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18784 complain
|= tf_decltype
;
18786 /* In C++0x, it's possible to have a function template whose type depends
18787 on itself recursively. This is most obvious with decltype, but can also
18788 occur with enumeration scope (c++/48969). So we need to catch infinite
18789 recursion and reject the substitution at deduction time; this function
18790 will return error_mark_node for any repeated substitution.
18792 This also catches excessive recursion such as when f<N> depends on
18793 f<N-1> across all integers, and returns error_mark_node for all the
18794 substitutions back up to the initial one.
18796 This is, of course, not reentrant. */
18797 if (excessive_deduction_depth
)
18798 return error_mark_node
;
18799 tinst
= build_tree_list (fn
, NULL_TREE
);
18802 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18804 fntype
= TREE_TYPE (fn
);
18805 if (explicit_targs
)
18809 The specified template arguments must match the template
18810 parameters in kind (i.e., type, nontype, template), and there
18811 must not be more arguments than there are parameters;
18812 otherwise type deduction fails.
18814 Nontype arguments must match the types of the corresponding
18815 nontype template parameters, or must be convertible to the
18816 types of the corresponding nontype parameters as specified in
18817 _temp.arg.nontype_, otherwise type deduction fails.
18819 All references in the function type of the function template
18820 to the corresponding template parameters are replaced by the
18821 specified template argument values. If a substitution in a
18822 template parameter or in the function type of the function
18823 template results in an invalid type, type deduction fails. */
18824 int i
, len
= TREE_VEC_LENGTH (tparms
);
18825 location_t loc
= input_location
;
18826 bool incomplete
= false;
18828 if (explicit_targs
== error_mark_node
)
18831 if (TMPL_ARGS_DEPTH (explicit_targs
)
18832 < TMPL_ARGS_DEPTH (full_targs
))
18833 explicit_targs
= add_outermost_template_args (full_targs
,
18836 /* Adjust any explicit template arguments before entering the
18837 substitution context. */
18839 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18841 /*require_all_args=*/false,
18842 /*use_default_args=*/false));
18843 if (explicit_targs
== error_mark_node
)
18846 /* Substitute the explicit args into the function type. This is
18847 necessary so that, for instance, explicitly declared function
18848 arguments can match null pointed constants. If we were given
18849 an incomplete set of explicit args, we must not do semantic
18850 processing during substitution as we could create partial
18852 for (i
= 0; i
< len
; i
++)
18854 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18855 bool parameter_pack
= false;
18856 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18858 /* Dig out the actual parm. */
18859 if (TREE_CODE (parm
) == TYPE_DECL
18860 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18862 parm
= TREE_TYPE (parm
);
18863 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18865 else if (TREE_CODE (parm
) == PARM_DECL
)
18867 parm
= DECL_INITIAL (parm
);
18868 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18871 if (!parameter_pack
&& targ
== NULL_TREE
)
18872 /* No explicit argument for this template parameter. */
18875 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18877 /* Mark the argument pack as "incomplete". We could
18878 still deduce more arguments during unification.
18879 We remove this mark in type_unification_real. */
18882 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18883 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18884 = ARGUMENT_PACK_ARGS (targ
);
18887 /* We have some incomplete argument packs. */
18892 TREE_VALUE (tinst
) = explicit_targs
;
18893 if (!push_tinst_level (tinst
))
18895 excessive_deduction_depth
= true;
18898 processing_template_decl
+= incomplete
;
18899 input_location
= DECL_SOURCE_LOCATION (fn
);
18900 /* Ignore any access checks; we'll see them again in
18901 instantiate_template and they might have the wrong
18902 access path at this point. */
18903 push_deferring_access_checks (dk_deferred
);
18904 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18905 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18906 pop_deferring_access_checks ();
18907 input_location
= loc
;
18908 processing_template_decl
-= incomplete
;
18909 pop_tinst_level ();
18911 if (fntype
== error_mark_node
)
18914 /* Place the explicitly specified arguments in TARGS. */
18915 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18916 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18917 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18920 /* Never do unification on the 'this' parameter. */
18921 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18923 if (return_type
&& strict
== DEDUCE_CALL
)
18925 /* We're deducing for a call to the result of a template conversion
18926 function. The parms we really want are in return_type. */
18927 if (POINTER_TYPE_P (return_type
))
18928 return_type
= TREE_TYPE (return_type
);
18929 parms
= TYPE_ARG_TYPES (return_type
);
18931 else if (return_type
)
18935 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18936 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18937 new_args
[0] = return_type
;
18938 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18943 /* We allow incomplete unification without an error message here
18944 because the standard doesn't seem to explicitly prohibit it. Our
18945 callers must be ready to deal with unification failures in any
18948 TREE_VALUE (tinst
) = targs
;
18949 /* If we aren't explaining yet, push tinst context so we can see where
18950 any errors (e.g. from class instantiations triggered by instantiation
18951 of default template arguments) come from. If we are explaining, this
18952 context is redundant. */
18953 if (!explain_p
&& !push_tinst_level (tinst
))
18955 excessive_deduction_depth
= true;
18959 /* type_unification_real will pass back any access checks from default
18960 template argument substitution. */
18961 vec
<deferred_access_check
, va_gc
> *checks
;
18964 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18965 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18966 strict
, flags
, &checks
, explain_p
);
18968 pop_tinst_level ();
18972 /* Now that we have bindings for all of the template arguments,
18973 ensure that the arguments deduced for the template template
18974 parameters have compatible template parameter lists. We cannot
18975 check this property before we have deduced all template
18976 arguments, because the template parameter types of a template
18977 template parameter might depend on prior template parameters
18978 deduced after the template template parameter. The following
18979 ill-formed example illustrates this issue:
18981 template<typename T, template<T> class C> void f(C<5>, T);
18983 template<int N> struct X {};
18986 f(X<5>(), 5l); // error: template argument deduction fails
18989 The template parameter list of 'C' depends on the template type
18990 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18991 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18992 time that we deduce 'C'. */
18993 if (!template_template_parm_bindings_ok_p
18994 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
18996 unify_inconsistent_template_template_parameters (explain_p
);
19000 /* All is well so far. Now, check:
19004 When all template arguments have been deduced, all uses of
19005 template parameters in nondeduced contexts are replaced with
19006 the corresponding deduced argument values. If the
19007 substitution results in an invalid type, as described above,
19008 type deduction fails. */
19009 TREE_VALUE (tinst
) = targs
;
19010 if (!push_tinst_level (tinst
))
19012 excessive_deduction_depth
= true;
19016 /* Also collect access checks from the instantiation. */
19017 reopen_deferring_access_checks (checks
);
19019 decl
= instantiate_template (fn
, targs
, complain
);
19021 checks
= get_deferred_access_checks ();
19022 pop_deferring_access_checks ();
19024 pop_tinst_level ();
19026 if (decl
== error_mark_node
)
19029 /* Now perform any access checks encountered during substitution. */
19030 push_access_scope (decl
);
19031 ok
= perform_access_checks (checks
, complain
);
19032 pop_access_scope (decl
);
19036 /* If we're looking for an exact match, check that what we got
19037 is indeed an exact match. It might not be if some template
19038 parameters are used in non-deduced contexts. But don't check
19039 for an exact match if we have dependent template arguments;
19040 in that case we're doing partial ordering, and we already know
19041 that we have two candidates that will provide the actual type. */
19042 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19044 tree substed
= TREE_TYPE (decl
);
19048 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19050 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19051 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19052 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19054 unify_type_mismatch (explain_p
, args
[i
],
19055 TREE_VALUE (sarg
));
19060 /* After doing deduction with the inherited constructor, actually return an
19061 instantiation of the inheriting constructor. */
19063 decl
= instantiate_template (orig_fn
, targs
, complain
);
19069 if (excessive_deduction_depth
)
19071 if (deduction_depth
== 0)
19072 /* Reset once we're all the way out. */
19073 excessive_deduction_depth
= false;
19076 /* We can't free this if a pending_template entry or last_error_tinst_level
19077 is pointing at it. */
19078 if (last_pending_template
== old_last_pend
19079 && last_error_tinst_level
== old_error_tinst
)
19085 /* Adjust types before performing type deduction, as described in
19086 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19087 sections are symmetric. PARM is the type of a function parameter
19088 or the return type of the conversion function. ARG is the type of
19089 the argument passed to the call, or the type of the value
19090 initialized with the result of the conversion function.
19091 ARG_EXPR is the original argument expression, which may be null. */
19094 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19107 /* Swap PARM and ARG throughout the remainder of this
19108 function; the handling is precisely symmetric since PARM
19109 will initialize ARG rather than vice versa. */
19110 std::swap (parm
, arg
);
19114 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19115 too, but here handle it by stripping the reference from PARM
19116 rather than by adding it to ARG. */
19117 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19118 && TYPE_REF_IS_RVALUE (*parm
)
19119 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19120 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19121 && TREE_CODE (*arg
) == REFERENCE_TYPE
19122 && !TYPE_REF_IS_RVALUE (*arg
))
19123 *parm
= TREE_TYPE (*parm
);
19124 /* Nothing else to do in this case. */
19128 gcc_unreachable ();
19131 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19133 /* [temp.deduct.call]
19135 If P is not a reference type:
19137 --If A is an array type, the pointer type produced by the
19138 array-to-pointer standard conversion (_conv.array_) is
19139 used in place of A for type deduction; otherwise,
19141 --If A is a function type, the pointer type produced by
19142 the function-to-pointer standard conversion
19143 (_conv.func_) is used in place of A for type deduction;
19146 --If A is a cv-qualified type, the top level
19147 cv-qualifiers of A's type are ignored for type
19149 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19150 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19151 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19152 *arg
= build_pointer_type (*arg
);
19154 *arg
= TYPE_MAIN_VARIANT (*arg
);
19157 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19158 reference to a cv-unqualified template parameter that does not represent a
19159 template parameter of a class template (during class template argument
19160 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19161 an lvalue, the type "lvalue reference to A" is used in place of A for type
19163 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19164 && TYPE_REF_IS_RVALUE (*parm
)
19165 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19166 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19167 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19168 && (arg_expr
? lvalue_p (arg_expr
)
19169 /* try_one_overload doesn't provide an arg_expr, but
19170 functions are always lvalues. */
19171 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19172 *arg
= build_reference_type (*arg
);
19174 /* [temp.deduct.call]
19176 If P is a cv-qualified type, the top level cv-qualifiers
19177 of P's type are ignored for type deduction. If P is a
19178 reference type, the type referred to by P is used for
19180 *parm
= TYPE_MAIN_VARIANT (*parm
);
19181 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19183 *parm
= TREE_TYPE (*parm
);
19184 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19187 /* DR 322. For conversion deduction, remove a reference type on parm
19188 too (which has been swapped into ARG). */
19189 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19190 *arg
= TREE_TYPE (*arg
);
19195 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19196 template which does contain any deducible template parameters; check if
19197 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19198 unify_one_argument. */
19201 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19202 int flags
, bool explain_p
)
19207 type
= TREE_TYPE (arg
);
19211 if (same_type_p (parm
, type
))
19212 return unify_success (explain_p
);
19214 if (strict
== DEDUCE_CONV
)
19216 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19217 explain_p
? tf_warning_or_error
: tf_none
))
19218 return unify_success (explain_p
);
19220 else if (strict
!= DEDUCE_EXACT
)
19222 if (can_convert_arg (parm
, type
,
19223 TYPE_P (arg
) ? NULL_TREE
: arg
,
19224 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19225 return unify_success (explain_p
);
19228 if (strict
== DEDUCE_EXACT
)
19229 return unify_type_mismatch (explain_p
, parm
, arg
);
19231 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19234 static bool uses_deducible_template_parms (tree type
);
19236 /* Returns true iff the expression EXPR is one from which a template
19237 argument can be deduced. In other words, if it's an undecorated
19238 use of a template non-type parameter. */
19241 deducible_expression (tree expr
)
19243 /* Strip implicit conversions. */
19244 while (CONVERT_EXPR_P (expr
))
19245 expr
= TREE_OPERAND (expr
, 0);
19246 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19249 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19250 deducible way; that is, if it has a max value of <PARM> - 1. */
19253 deducible_array_bound (tree domain
)
19255 if (domain
== NULL_TREE
)
19258 tree max
= TYPE_MAX_VALUE (domain
);
19259 if (TREE_CODE (max
) != MINUS_EXPR
)
19262 return deducible_expression (TREE_OPERAND (max
, 0));
19265 /* Returns true iff the template arguments ARGS use a template parameter
19266 in a deducible way. */
19269 deducible_template_args (tree args
)
19271 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19274 tree elt
= TREE_VEC_ELT (args
, i
);
19275 if (ARGUMENT_PACK_P (elt
))
19276 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19279 if (PACK_EXPANSION_P (elt
))
19280 elt
= PACK_EXPANSION_PATTERN (elt
);
19281 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19283 else if (TYPE_P (elt
))
19284 deducible
= uses_deducible_template_parms (elt
);
19286 deducible
= deducible_expression (elt
);
19294 /* Returns true iff TYPE contains any deducible references to template
19295 parameters, as per 14.8.2.5. */
19298 uses_deducible_template_parms (tree type
)
19300 if (PACK_EXPANSION_P (type
))
19301 type
= PACK_EXPANSION_PATTERN (type
);
19308 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19309 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19315 if (POINTER_TYPE_P (type
))
19316 return uses_deducible_template_parms (TREE_TYPE (type
));
19318 /* T[integer-constant ]
19320 if (TREE_CODE (type
) == ARRAY_TYPE
)
19321 return (uses_deducible_template_parms (TREE_TYPE (type
))
19322 || deducible_array_bound (TYPE_DOMAIN (type
)));
19334 if (TYPE_PTRMEM_P (type
))
19335 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19336 || (uses_deducible_template_parms
19337 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19339 /* template-name <T> (where template-name refers to a class template)
19340 template-name <i> (where template-name refers to a class template) */
19341 if (CLASS_TYPE_P (type
)
19342 && CLASSTYPE_TEMPLATE_INFO (type
)
19343 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19344 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19345 (CLASSTYPE_TI_ARGS (type
)));
19350 if (TREE_CODE (type
) == FUNCTION_TYPE
19351 || TREE_CODE (type
) == METHOD_TYPE
)
19353 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19355 tree parm
= TYPE_ARG_TYPES (type
);
19356 if (TREE_CODE (type
) == METHOD_TYPE
)
19357 parm
= TREE_CHAIN (parm
);
19358 for (; parm
; parm
= TREE_CHAIN (parm
))
19359 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19366 /* Subroutine of type_unification_real and unify_pack_expansion to
19367 handle unification of a single P/A pair. Parameters are as
19368 for those functions. */
19371 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19372 int subr
, unification_kind_t strict
,
19375 tree arg_expr
= NULL_TREE
;
19378 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19379 return unify_invalid (explain_p
);
19380 if (arg
== unknown_type_node
)
19381 /* We can't deduce anything from this, but we might get all the
19382 template args from other function args. */
19383 return unify_success (explain_p
);
19385 /* Implicit conversions (Clause 4) will be performed on a function
19386 argument to convert it to the type of the corresponding function
19387 parameter if the parameter type contains no template-parameters that
19388 participate in template argument deduction. */
19389 if (strict
!= DEDUCE_EXACT
19390 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19391 /* For function parameters with no deducible template parameters,
19392 just return. We'll check non-dependent conversions later. */
19393 return unify_success (explain_p
);
19398 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19399 | UNIFY_ALLOW_MORE_CV_QUAL
19400 | UNIFY_ALLOW_DERIVED
);
19404 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19408 arg_strict
= UNIFY_ALLOW_NONE
;
19412 gcc_unreachable ();
19415 /* We only do these transformations if this is the top-level
19416 parameter_type_list in a call or declaration matching; in other
19417 situations (nested function declarators, template argument lists) we
19418 won't be comparing a type to an expression, and we don't do any type
19424 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19425 if (type_unknown_p (arg
))
19427 /* [temp.deduct.type] A template-argument can be
19428 deduced from a pointer to function or pointer
19429 to member function argument if the set of
19430 overloaded functions does not contain function
19431 templates and at most one of a set of
19432 overloaded functions provides a unique
19434 resolve_overloaded_unification (tparms
, targs
, parm
,
19436 arg_strict
, explain_p
);
19437 /* If a unique match was not found, this is a
19438 non-deduced context, so we still succeed. */
19439 return unify_success (explain_p
);
19443 arg
= unlowered_expr_type (arg
);
19444 if (arg
== error_mark_node
)
19445 return unify_invalid (explain_p
);
19449 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19452 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19453 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19454 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19456 /* For deduction from an init-list we need the actual list. */
19457 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19459 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19462 /* for_each_template_parm callback that always returns 0. */
19465 zero_r (tree
, void *)
19470 /* for_each_template_parm any_fn callback to handle deduction of a template
19471 type argument from the type of an array bound. */
19474 array_deduction_r (tree t
, void *data
)
19476 tree_pair_p d
= (tree_pair_p
)data
;
19477 tree
&tparms
= d
->purpose
;
19478 tree
&targs
= d
->value
;
19480 if (TREE_CODE (t
) == ARRAY_TYPE
)
19481 if (tree dom
= TYPE_DOMAIN (t
))
19482 if (tree max
= TYPE_MAX_VALUE (dom
))
19484 if (TREE_CODE (max
) == MINUS_EXPR
)
19485 max
= TREE_OPERAND (max
, 0);
19486 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19487 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19488 UNIFY_ALLOW_NONE
, /*explain*/false);
19491 /* Keep walking. */
19495 /* Try to deduce any not-yet-deduced template type arguments from the type of
19496 an array bound. This is handled separately from unify because 14.8.2.5 says
19497 "The type of a type parameter is only deduced from an array bound if it is
19498 not otherwise deduced." */
19501 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19503 tree_pair_s data
= { tparms
, targs
};
19504 hash_set
<tree
> visited
;
19505 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19506 /*nondeduced*/false, array_deduction_r
);
19509 /* Most parms like fn_type_unification.
19511 If SUBR is 1, we're being called recursively (to unify the
19512 arguments of a function or method parameter of a function
19515 CHECKS is a pointer to a vector of access checks encountered while
19516 substituting default template arguments. */
19519 type_unification_real (tree tparms
,
19523 unsigned int xnargs
,
19525 unification_kind_t strict
,
19527 vec
<deferred_access_check
, va_gc
> **checks
,
19532 int ntparms
= TREE_VEC_LENGTH (tparms
);
19533 int saw_undeduced
= 0;
19536 unsigned int nargs
;
19539 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19540 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19541 gcc_assert (ntparms
> 0);
19543 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19545 /* Reset the number of non-defaulted template arguments contained
19547 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19555 while (parms
&& parms
!= void_list_node
19558 parm
= TREE_VALUE (parms
);
19560 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19561 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19562 /* For a function parameter pack that occurs at the end of the
19563 parameter-declaration-list, the type A of each remaining
19564 argument of the call is compared with the type P of the
19565 declarator-id of the function parameter pack. */
19568 parms
= TREE_CHAIN (parms
);
19570 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19571 /* For a function parameter pack that does not occur at the
19572 end of the parameter-declaration-list, the type of the
19573 parameter pack is a non-deduced context. */
19579 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19585 && parms
!= void_list_node
19586 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19588 /* Unify the remaining arguments with the pack expansion type. */
19590 tree parmvec
= make_tree_vec (1);
19592 /* Allocate a TREE_VEC and copy in all of the arguments */
19593 argvec
= make_tree_vec (nargs
- ia
);
19594 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19595 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19597 /* Copy the parameter into parmvec. */
19598 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19599 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19600 /*subr=*/subr
, explain_p
))
19603 /* Advance to the end of the list of parameters. */
19604 parms
= TREE_CHAIN (parms
);
19607 /* Fail if we've reached the end of the parm list, and more args
19608 are present, and the parm list isn't variadic. */
19609 if (ia
< nargs
&& parms
== void_list_node
)
19610 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19611 /* Fail if parms are left and they don't have default values and
19612 they aren't all deduced as empty packs (c++/57397). This is
19613 consistent with sufficient_parms_p. */
19614 if (parms
&& parms
!= void_list_node
19615 && TREE_PURPOSE (parms
) == NULL_TREE
)
19617 unsigned int count
= nargs
;
19622 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19625 p
= TREE_CHAIN (p
);
19627 while (p
&& p
!= void_list_node
);
19628 if (count
!= nargs
)
19629 return unify_too_few_arguments (explain_p
, ia
, count
,
19635 tsubst_flags_t complain
= (explain_p
19636 ? tf_warning_or_error
19638 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19640 for (i
= 0; i
< ntparms
; i
++)
19642 tree targ
= TREE_VEC_ELT (targs
, i
);
19643 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19645 /* Clear the "incomplete" flags on all argument packs now so that
19646 substituting them into later default arguments works. */
19647 if (targ
&& ARGUMENT_PACK_P (targ
))
19649 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19650 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19653 if (targ
|| tparm
== error_mark_node
)
19655 tparm
= TREE_VALUE (tparm
);
19657 if (TREE_CODE (tparm
) == TYPE_DECL
19658 && !tried_array_deduction
)
19660 try_array_deduction (tparms
, targs
, xparms
);
19661 tried_array_deduction
= true;
19662 if (TREE_VEC_ELT (targs
, i
))
19666 /* If this is an undeduced nontype parameter that depends on
19667 a type parameter, try another pass; its type may have been
19668 deduced from a later argument than the one from which
19669 this parameter can be deduced. */
19670 if (TREE_CODE (tparm
) == PARM_DECL
19671 && uses_template_parms (TREE_TYPE (tparm
))
19672 && saw_undeduced
< 2)
19678 /* Core issue #226 (C++0x) [temp.deduct]:
19680 If a template argument has not been deduced, its
19681 default template argument, if any, is used.
19683 When we are in C++98 mode, TREE_PURPOSE will either
19684 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19685 to explicitly check cxx_dialect here. */
19686 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19687 /* OK, there is a default argument. Wait until after the
19688 conversion check to do substitution. */
19691 /* If the type parameter is a parameter pack, then it will
19692 be deduced to an empty parameter pack. */
19693 if (template_parameter_pack_p (tparm
))
19697 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19699 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19700 TREE_CONSTANT (arg
) = 1;
19703 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19705 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19707 TREE_VEC_ELT (targs
, i
) = arg
;
19711 return unify_parameter_deduction_failure (explain_p
, tparm
);
19714 /* DR 1391: All parameters have args, now check non-dependent parms for
19716 if (saw_undeduced
< 2)
19717 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19718 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19720 parm
= TREE_VALUE (parms
);
19722 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19723 && (!TREE_CHAIN (parms
)
19724 || TREE_CHAIN (parms
) == void_list_node
))
19725 /* For a function parameter pack that occurs at the end of the
19726 parameter-declaration-list, the type A of each remaining
19727 argument of the call is compared with the type P of the
19728 declarator-id of the function parameter pack. */
19731 parms
= TREE_CHAIN (parms
);
19733 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19734 /* For a function parameter pack that does not occur at the
19735 end of the parameter-declaration-list, the type of the
19736 parameter pack is a non-deduced context. */
19742 if (uses_template_parms (parm
))
19744 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19749 /* Now substitute into the default template arguments. */
19750 for (i
= 0; i
< ntparms
; i
++)
19752 tree targ
= TREE_VEC_ELT (targs
, i
);
19753 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19755 if (targ
|| tparm
== error_mark_node
)
19757 tree parm
= TREE_VALUE (tparm
);
19759 if (TREE_CODE (parm
) == PARM_DECL
19760 && uses_template_parms (TREE_TYPE (parm
))
19761 && saw_undeduced
< 2)
19764 tree arg
= TREE_PURPOSE (tparm
);
19765 reopen_deferring_access_checks (*checks
);
19766 location_t save_loc
= input_location
;
19768 input_location
= DECL_SOURCE_LOCATION (parm
);
19769 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19770 if (!uses_template_parms (arg
))
19771 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19773 else if (saw_undeduced
< 2)
19776 arg
= error_mark_node
;
19777 input_location
= save_loc
;
19778 *checks
= get_deferred_access_checks ();
19779 pop_deferring_access_checks ();
19780 if (arg
== error_mark_node
)
19784 TREE_VEC_ELT (targs
, i
) = arg
;
19785 /* The position of the first default template argument,
19786 is also the number of non-defaulted arguments in TARGS.
19788 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19789 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19793 if (saw_undeduced
++ == 1)
19797 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19798 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19800 return unify_success (explain_p
);
19803 /* Subroutine of type_unification_real. Args are like the variables
19804 at the call site. ARG is an overloaded function (or template-id);
19805 we try deducing template args from each of the overloads, and if
19806 only one succeeds, we go with that. Modifies TARGS and returns
19807 true on success. */
19810 resolve_overloaded_unification (tree tparms
,
19814 unification_kind_t strict
,
19818 tree tempargs
= copy_node (targs
);
19820 tree goodfn
= NULL_TREE
;
19823 if (TREE_CODE (arg
) == ADDR_EXPR
)
19825 arg
= TREE_OPERAND (arg
, 0);
19831 if (TREE_CODE (arg
) == COMPONENT_REF
)
19832 /* Handle `&x' where `x' is some static or non-static member
19834 arg
= TREE_OPERAND (arg
, 1);
19836 if (TREE_CODE (arg
) == OFFSET_REF
)
19837 arg
= TREE_OPERAND (arg
, 1);
19839 /* Strip baselink information. */
19840 if (BASELINK_P (arg
))
19841 arg
= BASELINK_FUNCTIONS (arg
);
19843 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19845 /* If we got some explicit template args, we need to plug them into
19846 the affected templates before we try to unify, in case the
19847 explicit args will completely resolve the templates in question. */
19850 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19851 arg
= TREE_OPERAND (arg
, 0);
19853 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19856 tree subargs
, elem
;
19858 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19861 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19862 expl_subargs
, NULL_TREE
, tf_none
,
19863 /*require_all_args=*/true,
19864 /*use_default_args=*/true);
19865 if (subargs
!= error_mark_node
19866 && !any_dependent_template_arguments_p (subargs
))
19868 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19869 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19870 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19871 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19880 /* If no templates (or more than one) are fully resolved by the
19881 explicit arguments, this template-id is a non-deduced context; it
19882 could still be OK if we deduce all template arguments for the
19883 enclosing call through other arguments. */
19887 else if (TREE_CODE (arg
) != OVERLOAD
19888 && TREE_CODE (arg
) != FUNCTION_DECL
)
19889 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19890 -- but the deduction does not succeed because the expression is
19891 not just the function on its own. */
19894 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19897 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19898 strict
, sub_strict
, addr_p
, explain_p
)
19899 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19906 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19907 to function or pointer to member function argument if the set of
19908 overloaded functions does not contain function templates and at most
19909 one of a set of overloaded functions provides a unique match.
19911 So if we found multiple possibilities, we return success but don't
19912 deduce anything. */
19916 int i
= TREE_VEC_LENGTH (targs
);
19918 if (TREE_VEC_ELT (tempargs
, i
))
19920 tree old
= TREE_VEC_ELT (targs
, i
);
19921 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19922 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19923 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19924 /* Don't forget explicit template arguments in a pack. */
19925 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19926 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19927 TREE_VEC_ELT (targs
, i
) = new_
;
19936 /* Core DR 115: In contexts where deduction is done and fails, or in
19937 contexts where deduction is not done, if a template argument list is
19938 specified and it, along with any default template arguments, identifies
19939 a single function template specialization, then the template-id is an
19940 lvalue for the function template specialization. */
19943 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19945 tree expr
, offset
, baselink
;
19948 if (!type_unknown_p (orig_expr
))
19953 offset
= NULL_TREE
;
19954 baselink
= NULL_TREE
;
19956 if (TREE_CODE (expr
) == ADDR_EXPR
)
19958 expr
= TREE_OPERAND (expr
, 0);
19961 if (TREE_CODE (expr
) == OFFSET_REF
)
19964 expr
= TREE_OPERAND (expr
, 1);
19966 if (BASELINK_P (expr
))
19969 expr
= BASELINK_FUNCTIONS (expr
);
19972 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19975 tree goodfn
= NULL_TREE
;
19977 /* If we got some explicit template args, we need to plug them into
19978 the affected templates before we try to unify, in case the
19979 explicit args will completely resolve the templates in question. */
19981 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19982 tree arg
= TREE_OPERAND (expr
, 0);
19983 tree badfn
= NULL_TREE
;
19984 tree badargs
= NULL_TREE
;
19986 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19989 tree subargs
, elem
;
19991 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19994 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19995 expl_subargs
, NULL_TREE
, tf_none
,
19996 /*require_all_args=*/true,
19997 /*use_default_args=*/true);
19998 if (subargs
!= error_mark_node
19999 && !any_dependent_template_arguments_p (subargs
))
20001 elem
= instantiate_template (fn
, subargs
, tf_none
);
20002 if (elem
== error_mark_node
)
20007 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20016 mark_used (goodfn
);
20019 expr
= build_baselink (BASELINK_BINFO (baselink
),
20020 BASELINK_ACCESS_BINFO (baselink
),
20021 expr
, BASELINK_OPTYPE (baselink
));
20025 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20026 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20029 expr
= cp_build_addr_expr (expr
, complain
);
20032 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20033 /* There were no good options and at least one bad one, so let the
20034 user know what the problem is. */
20035 instantiate_template (badfn
, badargs
, complain
);
20040 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20041 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20042 different overloads deduce different arguments for a given parm.
20043 ADDR_P is true if the expression for which deduction is being
20044 performed was of the form "& fn" rather than simply "fn".
20046 Returns 1 on success. */
20049 try_one_overload (tree tparms
,
20054 unification_kind_t strict
,
20063 if (arg
== error_mark_node
)
20066 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20067 to function or pointer to member function argument if the set of
20068 overloaded functions does not contain function templates and at most
20069 one of a set of overloaded functions provides a unique match.
20071 So if this is a template, just return success. */
20073 if (uses_template_parms (arg
))
20076 if (TREE_CODE (arg
) == METHOD_TYPE
)
20077 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20079 arg
= build_pointer_type (arg
);
20081 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20083 /* We don't copy orig_targs for this because if we have already deduced
20084 some template args from previous args, unify would complain when we
20085 try to deduce a template parameter for the same argument, even though
20086 there isn't really a conflict. */
20087 nargs
= TREE_VEC_LENGTH (targs
);
20088 tempargs
= make_tree_vec (nargs
);
20090 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20093 /* First make sure we didn't deduce anything that conflicts with
20094 explicitly specified args. */
20095 for (i
= nargs
; i
--; )
20097 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20098 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20102 else if (uses_template_parms (elt
))
20103 /* Since we're unifying against ourselves, we will fill in
20104 template args used in the function parm list with our own
20105 template parms. Discard them. */
20106 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20107 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20109 /* Check that the argument at each index of the deduced argument pack
20110 is equivalent to the corresponding explicitly specified argument.
20111 We may have deduced more arguments than were explicitly specified,
20114 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20115 that's wrong if we deduce the same argument pack from multiple
20116 function arguments: it's only incomplete the first time. */
20118 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20119 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20121 if (TREE_VEC_LENGTH (deduced_pack
)
20122 < TREE_VEC_LENGTH (explicit_pack
))
20125 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20126 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20127 TREE_VEC_ELT (deduced_pack
, j
)))
20130 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20134 for (i
= nargs
; i
--; )
20136 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20139 TREE_VEC_ELT (targs
, i
) = elt
;
20145 /* PARM is a template class (perhaps with unbound template
20146 parameters). ARG is a fully instantiated type. If ARG can be
20147 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20148 TARGS are as for unify. */
20151 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20154 tree copy_of_targs
;
20156 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20158 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20159 /* Matches anything. */;
20160 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20161 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20164 /* We need to make a new template argument vector for the call to
20165 unify. If we used TARGS, we'd clutter it up with the result of
20166 the attempted unification, even if this class didn't work out.
20167 We also don't want to commit ourselves to all the unifications
20168 we've already done, since unification is supposed to be done on
20169 an argument-by-argument basis. In other words, consider the
20170 following pathological case:
20172 template <int I, int J, int K>
20175 template <int I, int J>
20176 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20178 template <int I, int J, int K>
20179 void f(S<I, J, K>, S<I, I, I>);
20188 Now, by the time we consider the unification involving `s2', we
20189 already know that we must have `f<0, 0, 0>'. But, even though
20190 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20191 because there are two ways to unify base classes of S<0, 1, 2>
20192 with S<I, I, I>. If we kept the already deduced knowledge, we
20193 would reject the possibility I=1. */
20194 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20196 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20198 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20203 /* If unification failed, we're done. */
20204 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20205 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20211 /* Given a template type PARM and a class type ARG, find the unique
20212 base type in ARG that is an instance of PARM. We do not examine
20213 ARG itself; only its base-classes. If there is not exactly one
20214 appropriate base class, return NULL_TREE. PARM may be the type of
20215 a partial specialization, as well as a plain template type. Used
20218 static enum template_base_result
20219 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20220 bool explain_p
, tree
*result
)
20222 tree rval
= NULL_TREE
;
20225 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20227 binfo
= TYPE_BINFO (complete_type (arg
));
20230 /* The type could not be completed. */
20231 *result
= NULL_TREE
;
20232 return tbr_incomplete_type
;
20235 /* Walk in inheritance graph order. The search order is not
20236 important, and this avoids multiple walks of virtual bases. */
20237 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20239 tree r
= try_class_unification (tparms
, targs
, parm
,
20240 BINFO_TYPE (binfo
), explain_p
);
20244 /* If there is more than one satisfactory baseclass, then:
20248 If they yield more than one possible deduced A, the type
20252 if (rval
&& !same_type_p (r
, rval
))
20254 *result
= NULL_TREE
;
20255 return tbr_ambiguous_baseclass
;
20263 return tbr_success
;
20266 /* Returns the level of DECL, which declares a template parameter. */
20269 template_decl_level (tree decl
)
20271 switch (TREE_CODE (decl
))
20274 case TEMPLATE_DECL
:
20275 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20278 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20281 gcc_unreachable ();
20286 /* Decide whether ARG can be unified with PARM, considering only the
20287 cv-qualifiers of each type, given STRICT as documented for unify.
20288 Returns nonzero iff the unification is OK on that basis. */
20291 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20293 int arg_quals
= cp_type_quals (arg
);
20294 int parm_quals
= cp_type_quals (parm
);
20296 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20297 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20299 /* Although a CVR qualifier is ignored when being applied to a
20300 substituted template parameter ([8.3.2]/1 for example), that
20301 does not allow us to unify "const T" with "int&" because both
20302 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20303 It is ok when we're allowing additional CV qualifiers
20304 at the outer level [14.8.2.1]/3,1st bullet. */
20305 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20306 || TREE_CODE (arg
) == FUNCTION_TYPE
20307 || TREE_CODE (arg
) == METHOD_TYPE
)
20308 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20311 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20312 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20316 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20317 && (arg_quals
& parm_quals
) != parm_quals
)
20320 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20321 && (parm_quals
& arg_quals
) != arg_quals
)
20327 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20329 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20331 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20332 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20333 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20335 *index
= TEMPLATE_TYPE_IDX (parm
);
20336 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20340 *index
= TEMPLATE_PARM_IDX (parm
);
20341 *level
= TEMPLATE_PARM_LEVEL (parm
);
20345 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20347 if (unify (TP, TA, P, A, S, EP)) \
20351 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20352 expansion at the end of PACKED_PARMS. Returns 0 if the type
20353 deduction succeeds, 1 otherwise. STRICT is the same as in
20354 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20355 function call argument list. We'll need to adjust the arguments to make them
20356 types. SUBR tells us if this is from a recursive call to
20357 type_unification_real, or for comparing two template argument
20361 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20362 tree packed_args
, unification_kind_t strict
,
20363 bool subr
, bool explain_p
)
20366 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20367 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20368 tree pack
, packs
= NULL_TREE
;
20369 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20371 /* Add in any args remembered from an earlier partial instantiation. */
20372 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20373 int levels
= TMPL_ARGS_DEPTH (targs
);
20375 packed_args
= expand_template_argument_pack (packed_args
);
20377 int len
= TREE_VEC_LENGTH (packed_args
);
20379 /* Determine the parameter packs we will be deducing from the
20380 pattern, and record their current deductions. */
20381 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20382 pack
; pack
= TREE_CHAIN (pack
))
20384 tree parm_pack
= TREE_VALUE (pack
);
20387 /* Determine the index and level of this parameter pack. */
20388 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20389 if (level
< levels
)
20392 /* Keep track of the parameter packs and their corresponding
20394 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20395 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20398 /* Loop through all of the arguments that have not yet been
20399 unified and unify each with the pattern. */
20400 for (i
= start
; i
< len
; i
++)
20403 bool any_explicit
= false;
20404 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20406 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20407 or the element of its argument pack at the current index if
20408 this argument was explicitly specified. */
20409 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20413 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20416 if (TREE_VALUE (pack
)
20417 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20418 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20420 any_explicit
= true;
20421 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20423 TMPL_ARG (targs
, level
, idx
) = arg
;
20426 /* If we had explicit template arguments, substitute them into the
20427 pattern before deduction. */
20430 /* Some arguments might still be unspecified or dependent. */
20432 ++processing_template_decl
;
20433 dependent
= any_dependent_template_arguments_p (targs
);
20435 --processing_template_decl
;
20436 parm
= tsubst (pattern
, targs
,
20437 explain_p
? tf_warning_or_error
: tf_none
,
20440 --processing_template_decl
;
20441 if (parm
== error_mark_node
)
20447 /* Unify the pattern with the current argument. */
20448 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20452 /* For each parameter pack, collect the deduced value. */
20453 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20456 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20458 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20459 TMPL_ARG (targs
, level
, idx
);
20463 /* Verify that the results of unification with the parameter packs
20464 produce results consistent with what we've seen before, and make
20465 the deduced argument packs available. */
20466 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20468 tree old_pack
= TREE_VALUE (pack
);
20469 tree new_args
= TREE_TYPE (pack
);
20470 int i
, len
= TREE_VEC_LENGTH (new_args
);
20472 bool nondeduced_p
= false;
20474 /* By default keep the original deduced argument pack.
20475 If necessary, more specific code is going to update the
20476 resulting deduced argument later down in this function. */
20477 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20478 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20480 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20481 actually deduce anything. */
20482 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20483 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20484 nondeduced_p
= true;
20488 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20490 /* If we had fewer function args than explicit template args,
20491 just use the explicits. */
20492 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20493 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20494 if (len
< explicit_len
)
20495 new_args
= explicit_args
;
20501 /* Build the deduced *_ARGUMENT_PACK. */
20502 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20504 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20505 TREE_CONSTANT (result
) = 1;
20508 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20510 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20512 /* Note the deduced argument packs for this parameter
20514 TMPL_ARG (targs
, level
, idx
) = result
;
20516 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20517 && (ARGUMENT_PACK_ARGS (old_pack
)
20518 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20520 /* We only had the explicitly-provided arguments before, but
20521 now we have a complete set of arguments. */
20522 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20524 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20525 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20526 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20530 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20531 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20533 if (!comp_template_args (old_args
, new_args
,
20534 &bad_old_arg
, &bad_new_arg
))
20535 /* Inconsistent unification of this parameter pack. */
20536 return unify_parameter_pack_inconsistent (explain_p
,
20542 return unify_success (explain_p
);
20545 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20546 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20547 parameters and return value are as for unify. */
20550 unify_array_domain (tree tparms
, tree targs
,
20551 tree parm_dom
, tree arg_dom
,
20559 /* Our representation of array types uses "N - 1" as the
20560 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20561 not an integer constant. We cannot unify arbitrarily
20562 complex expressions, so we eliminate the MINUS_EXPRs
20564 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20565 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20568 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20569 parm_max
= TREE_OPERAND (parm_max
, 0);
20571 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20572 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20575 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20576 trying to unify the type of a variable with the type
20577 of a template parameter. For example:
20579 template <unsigned int N>
20580 void f (char (&) [N]);
20587 Here, the type of the ARG will be "int [g(i)]", and
20588 may be a SAVE_EXPR, etc. */
20589 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20590 return unify_vla_arg (explain_p
, arg_dom
);
20591 arg_max
= TREE_OPERAND (arg_max
, 0);
20594 /* If only one of the bounds used a MINUS_EXPR, compensate
20595 by adding one to the other bound. */
20596 if (parm_cst
&& !arg_cst
)
20597 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20601 else if (arg_cst
&& !parm_cst
)
20602 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20607 return unify (tparms
, targs
, parm_max
, arg_max
,
20608 UNIFY_ALLOW_INTEGER
, explain_p
);
20611 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20613 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20618 if (PACK_EXPANSION_P (t
))
20619 t
= PACK_EXPANSION_PATTERN (t
);
20620 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20621 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20622 || DECL_TYPE_TEMPLATE_P (t
))
20624 else if (TYPE_P (t
))
20630 /* Deduce the value of template parameters. TPARMS is the (innermost)
20631 set of template parameters to a template. TARGS is the bindings
20632 for those template parameters, as determined thus far; TARGS may
20633 include template arguments for outer levels of template parameters
20634 as well. PARM is a parameter to a template function, or a
20635 subcomponent of that parameter; ARG is the corresponding argument.
20636 This function attempts to match PARM with ARG in a manner
20637 consistent with the existing assignments in TARGS. If more values
20638 are deduced, then TARGS is updated.
20640 Returns 0 if the type deduction succeeds, 1 otherwise. The
20641 parameter STRICT is a bitwise or of the following flags:
20644 Require an exact match between PARM and ARG.
20645 UNIFY_ALLOW_MORE_CV_QUAL:
20646 Allow the deduced ARG to be more cv-qualified (by qualification
20647 conversion) than ARG.
20648 UNIFY_ALLOW_LESS_CV_QUAL:
20649 Allow the deduced ARG to be less cv-qualified than ARG.
20650 UNIFY_ALLOW_DERIVED:
20651 Allow the deduced ARG to be a template base class of ARG,
20652 or a pointer to a template base class of the type pointed to by
20654 UNIFY_ALLOW_INTEGER:
20655 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20656 case for more information.
20657 UNIFY_ALLOW_OUTER_LEVEL:
20658 This is the outermost level of a deduction. Used to determine validity
20659 of qualification conversions. A valid qualification conversion must
20660 have const qualified pointers leading up to the inner type which
20661 requires additional CV quals, except at the outer level, where const
20662 is not required [conv.qual]. It would be normal to set this flag in
20663 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20664 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20665 This is the outermost level of a deduction, and PARM can be more CV
20666 qualified at this point.
20667 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20668 This is the outermost level of a deduction, and PARM can be less CV
20669 qualified at this point. */
20672 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20678 int strict_in
= strict
;
20679 tsubst_flags_t complain
= (explain_p
20680 ? tf_warning_or_error
20683 /* I don't think this will do the right thing with respect to types.
20684 But the only case I've seen it in so far has been array bounds, where
20685 signedness is the only information lost, and I think that will be
20687 while (CONVERT_EXPR_P (parm
))
20688 parm
= TREE_OPERAND (parm
, 0);
20690 if (arg
== error_mark_node
)
20691 return unify_invalid (explain_p
);
20692 if (arg
== unknown_type_node
20693 || arg
== init_list_type_node
)
20694 /* We can't deduce anything from this, but we might get all the
20695 template args from other function args. */
20696 return unify_success (explain_p
);
20698 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20699 return unify_success (explain_p
);
20701 /* If PARM uses template parameters, then we can't bail out here,
20702 even if ARG == PARM, since we won't record unifications for the
20703 template parameters. We might need them if we're trying to
20704 figure out which of two things is more specialized. */
20705 if (arg
== parm
&& !uses_template_parms (parm
))
20706 return unify_success (explain_p
);
20708 /* Handle init lists early, so the rest of the function can assume
20709 we're dealing with a type. */
20710 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20714 tree orig_parm
= parm
;
20716 /* Replace T with std::initializer_list<T> for deduction. */
20717 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20718 && flag_deduce_init_list
)
20719 parm
= listify (parm
);
20721 if (!is_std_init_list (parm
)
20722 && TREE_CODE (parm
) != ARRAY_TYPE
)
20723 /* We can only deduce from an initializer list argument if the
20724 parameter is std::initializer_list or an array; otherwise this
20725 is a non-deduced context. */
20726 return unify_success (explain_p
);
20728 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20729 elttype
= TREE_TYPE (parm
);
20732 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20733 /* Deduction is defined in terms of a single type, so just punt
20734 on the (bizarre) std::initializer_list<T...>. */
20735 if (PACK_EXPANSION_P (elttype
))
20736 return unify_success (explain_p
);
20739 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20741 int elt_strict
= strict
;
20743 if (elt
== error_mark_node
)
20744 return unify_invalid (explain_p
);
20746 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20748 tree type
= TREE_TYPE (elt
);
20749 if (type
== error_mark_node
)
20750 return unify_invalid (explain_p
);
20751 /* It should only be possible to get here for a call. */
20752 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20753 elt_strict
|= maybe_adjust_types_for_deduction
20754 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20758 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20762 if (TREE_CODE (parm
) == ARRAY_TYPE
20763 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20765 /* Also deduce from the length of the initializer list. */
20766 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20767 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20768 if (idx
== error_mark_node
)
20769 return unify_invalid (explain_p
);
20770 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20774 /* If the std::initializer_list<T> deduction worked, replace the
20775 deduced A with std::initializer_list<A>. */
20776 if (orig_parm
!= parm
)
20778 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20779 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20780 targ
= listify (targ
);
20781 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20783 return unify_success (explain_p
);
20786 /* If parm and arg aren't the same kind of thing (template, type, or
20787 expression), fail early. */
20788 if (pa_kind (parm
) != pa_kind (arg
))
20789 return unify_invalid (explain_p
);
20791 /* Immediately reject some pairs that won't unify because of
20792 cv-qualification mismatches. */
20793 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20795 /* It is the elements of the array which hold the cv quals of an array
20796 type, and the elements might be template type parms. We'll check
20797 when we recurse. */
20798 && TREE_CODE (arg
) != ARRAY_TYPE
20799 /* We check the cv-qualifiers when unifying with template type
20800 parameters below. We want to allow ARG `const T' to unify with
20801 PARM `T' for example, when computing which of two templates
20802 is more specialized, for example. */
20803 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20804 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20805 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20807 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20808 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20809 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20810 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20811 strict
&= ~UNIFY_ALLOW_DERIVED
;
20812 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20813 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20815 switch (TREE_CODE (parm
))
20817 case TYPENAME_TYPE
:
20819 case UNBOUND_CLASS_TEMPLATE
:
20820 /* In a type which contains a nested-name-specifier, template
20821 argument values cannot be deduced for template parameters used
20822 within the nested-name-specifier. */
20823 return unify_success (explain_p
);
20825 case TEMPLATE_TYPE_PARM
:
20826 case TEMPLATE_TEMPLATE_PARM
:
20827 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20828 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20829 if (error_operand_p (tparm
))
20830 return unify_invalid (explain_p
);
20832 if (TEMPLATE_TYPE_LEVEL (parm
)
20833 != template_decl_level (tparm
))
20834 /* The PARM is not one we're trying to unify. Just check
20835 to see if it matches ARG. */
20837 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20838 && (is_auto (parm
) ? is_auto (arg
)
20839 : same_type_p (parm
, arg
)))
20840 return unify_success (explain_p
);
20842 return unify_type_mismatch (explain_p
, parm
, arg
);
20844 idx
= TEMPLATE_TYPE_IDX (parm
);
20845 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20846 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20847 if (error_operand_p (tparm
))
20848 return unify_invalid (explain_p
);
20850 /* Check for mixed types and values. */
20851 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20852 && TREE_CODE (tparm
) != TYPE_DECL
)
20853 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20854 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20855 gcc_unreachable ();
20857 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20859 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20860 && CLASS_TYPE_P (arg
))
20862 /* First try to match ARG directly. */
20863 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20867 /* Otherwise, look for a suitable base of ARG, as below. */
20868 enum template_base_result r
;
20869 r
= get_template_base (tparms
, targs
, parm
, arg
,
20872 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20876 /* ARG must be constructed from a template class or a template
20877 template parameter. */
20878 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20879 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20880 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20882 /* Deduce arguments T, i from TT<T> or TT<i>. */
20883 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20886 arg
= TYPE_TI_TEMPLATE (arg
);
20888 /* Fall through to deduce template name. */
20891 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20892 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20894 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20896 /* Simple cases: Value already set, does match or doesn't. */
20897 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20898 return unify_success (explain_p
);
20900 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20904 /* If PARM is `const T' and ARG is only `int', we don't have
20905 a match unless we are allowing additional qualification.
20906 If ARG is `const int' and PARM is just `T' that's OK;
20907 that binds `const int' to `T'. */
20908 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20910 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20912 /* Consider the case where ARG is `const volatile int' and
20913 PARM is `const T'. Then, T should be `volatile int'. */
20914 arg
= cp_build_qualified_type_real
20915 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20916 if (arg
== error_mark_node
)
20917 return unify_invalid (explain_p
);
20919 /* Simple cases: Value already set, does match or doesn't. */
20920 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20921 return unify_success (explain_p
);
20923 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20925 /* Make sure that ARG is not a variable-sized array. (Note
20926 that were talking about variable-sized arrays (like
20927 `int[n]'), rather than arrays of unknown size (like
20928 `int[]').) We'll get very confused by such a type since
20929 the bound of the array is not constant, and therefore
20930 not mangleable. Besides, such types are not allowed in
20931 ISO C++, so we can do as we please here. We do allow
20932 them for 'auto' deduction, since that isn't ABI-exposed. */
20933 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20934 return unify_vla_arg (explain_p
, arg
);
20936 /* Strip typedefs as in convert_template_argument. */
20937 arg
= canonicalize_type_argument (arg
, tf_none
);
20940 /* If ARG is a parameter pack or an expansion, we cannot unify
20941 against it unless PARM is also a parameter pack. */
20942 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20943 && !template_parameter_pack_p (parm
))
20944 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20946 /* If the argument deduction results is a METHOD_TYPE,
20947 then there is a problem.
20948 METHOD_TYPE doesn't map to any real C++ type the result of
20949 the deduction can not be of that type. */
20950 if (TREE_CODE (arg
) == METHOD_TYPE
)
20951 return unify_method_type_error (explain_p
, arg
);
20953 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20954 return unify_success (explain_p
);
20956 case TEMPLATE_PARM_INDEX
:
20957 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20958 if (error_operand_p (tparm
))
20959 return unify_invalid (explain_p
);
20961 if (TEMPLATE_PARM_LEVEL (parm
)
20962 != template_decl_level (tparm
))
20964 /* The PARM is not one we're trying to unify. Just check
20965 to see if it matches ARG. */
20966 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20967 && cp_tree_equal (parm
, arg
));
20969 unify_expression_unequal (explain_p
, parm
, arg
);
20973 idx
= TEMPLATE_PARM_IDX (parm
);
20974 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20978 if ((strict
& UNIFY_ALLOW_INTEGER
)
20979 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
20980 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
20981 /* We're deducing from an array bound, the type doesn't matter. */
20982 arg
= fold_convert (TREE_TYPE (targ
), arg
);
20983 int x
= !cp_tree_equal (targ
, arg
);
20985 unify_inconsistency (explain_p
, parm
, targ
, arg
);
20989 /* [temp.deduct.type] If, in the declaration of a function template
20990 with a non-type template-parameter, the non-type
20991 template-parameter is used in an expression in the function
20992 parameter-list and, if the corresponding template-argument is
20993 deduced, the template-argument type shall match the type of the
20994 template-parameter exactly, except that a template-argument
20995 deduced from an array bound may be of any integral type.
20996 The non-type parameter might use already deduced type parameters. */
20997 ++processing_template_decl
;
20998 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
20999 --processing_template_decl
;
21000 if (tree a
= type_uses_auto (tparm
))
21002 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21003 if (tparm
== error_mark_node
)
21007 if (!TREE_TYPE (arg
))
21008 /* Template-parameter dependent expression. Just accept it for now.
21009 It will later be processed in convert_template_argument. */
21011 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21012 non_reference (tparm
)))
21014 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21015 && CP_INTEGRAL_TYPE_P (tparm
))
21016 /* Convert the ARG to the type of PARM; the deduced non-type
21017 template argument must exactly match the types of the
21018 corresponding parameter. */
21019 arg
= fold (build_nop (tparm
, arg
));
21020 else if (uses_template_parms (tparm
))
21022 /* We haven't deduced the type of this parameter yet. */
21023 if (cxx_dialect
>= cxx17
21024 /* We deduce from array bounds in try_array_deduction. */
21025 && !(strict
& UNIFY_ALLOW_INTEGER
))
21027 /* Deduce it from the non-type argument. */
21028 tree atype
= TREE_TYPE (arg
);
21029 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21031 UNIFY_ALLOW_NONE
, explain_p
);
21034 /* Try again later. */
21035 return unify_success (explain_p
);
21038 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21040 /* If ARG is a parameter pack or an expansion, we cannot unify
21041 against it unless PARM is also a parameter pack. */
21042 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21043 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21044 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21047 bool removed_attr
= false;
21048 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21050 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21051 return unify_success (explain_p
);
21055 /* A pointer-to-member constant can be unified only with
21056 another constant. */
21057 if (TREE_CODE (arg
) != PTRMEM_CST
)
21058 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21060 /* Just unify the class member. It would be useless (and possibly
21061 wrong, depending on the strict flags) to unify also
21062 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21063 arg refer to the same variable, even if through different
21064 classes. For instance:
21066 struct A { int x; };
21069 Unification of &A::x and &B::x must succeed. */
21070 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21071 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21076 if (!TYPE_PTR_P (arg
))
21077 return unify_type_mismatch (explain_p
, parm
, arg
);
21079 /* [temp.deduct.call]
21081 A can be another pointer or pointer to member type that can
21082 be converted to the deduced A via a qualification
21083 conversion (_conv.qual_).
21085 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21086 This will allow for additional cv-qualification of the
21087 pointed-to types if appropriate. */
21089 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21090 /* The derived-to-base conversion only persists through one
21091 level of pointers. */
21092 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21094 return unify (tparms
, targs
, TREE_TYPE (parm
),
21095 TREE_TYPE (arg
), strict
, explain_p
);
21098 case REFERENCE_TYPE
:
21099 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21100 return unify_type_mismatch (explain_p
, parm
, arg
);
21101 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21102 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21105 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21106 return unify_type_mismatch (explain_p
, parm
, arg
);
21107 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21108 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21109 return unify_type_mismatch (explain_p
, parm
, arg
);
21110 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21111 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21112 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21113 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21114 TYPE_DOMAIN (arg
), explain_p
);
21115 return unify_success (explain_p
);
21122 case ENUMERAL_TYPE
:
21125 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21126 return unify_type_mismatch (explain_p
, parm
, arg
);
21128 /* We have already checked cv-qualification at the top of the
21130 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21131 return unify_type_mismatch (explain_p
, parm
, arg
);
21133 /* As far as unification is concerned, this wins. Later checks
21134 will invalidate it if necessary. */
21135 return unify_success (explain_p
);
21137 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21138 /* Type INTEGER_CST can come from ordinary constant template args. */
21140 while (CONVERT_EXPR_P (arg
))
21141 arg
= TREE_OPERAND (arg
, 0);
21143 if (TREE_CODE (arg
) != INTEGER_CST
)
21144 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21145 return (tree_int_cst_equal (parm
, arg
)
21146 ? unify_success (explain_p
)
21147 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21151 int i
, len
, argslen
;
21152 int parm_variadic_p
= 0;
21154 if (TREE_CODE (arg
) != TREE_VEC
)
21155 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21157 len
= TREE_VEC_LENGTH (parm
);
21158 argslen
= TREE_VEC_LENGTH (arg
);
21160 /* Check for pack expansions in the parameters. */
21161 for (i
= 0; i
< len
; ++i
)
21163 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21166 /* We can unify against something with a trailing
21168 parm_variadic_p
= 1;
21170 /* [temp.deduct.type]/9: If the template argument list of
21171 P contains a pack expansion that is not the last
21172 template argument, the entire template argument list
21173 is a non-deduced context. */
21174 return unify_success (explain_p
);
21178 /* If we don't have enough arguments to satisfy the parameters
21179 (not counting the pack expression at the end), or we have
21180 too many arguments for a parameter list that doesn't end in
21181 a pack expression, we can't unify. */
21182 if (parm_variadic_p
21183 ? argslen
< len
- parm_variadic_p
21185 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21187 /* Unify all of the parameters that precede the (optional)
21188 pack expression. */
21189 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21191 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21192 TREE_VEC_ELT (parm
, i
),
21193 TREE_VEC_ELT (arg
, i
),
21194 UNIFY_ALLOW_NONE
, explain_p
);
21196 if (parm_variadic_p
)
21197 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21199 /*subr=*/true, explain_p
);
21200 return unify_success (explain_p
);
21205 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21206 return unify_type_mismatch (explain_p
, parm
, arg
);
21208 if (TYPE_PTRMEMFUNC_P (parm
))
21210 if (!TYPE_PTRMEMFUNC_P (arg
))
21211 return unify_type_mismatch (explain_p
, parm
, arg
);
21213 return unify (tparms
, targs
,
21214 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21215 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21216 strict
, explain_p
);
21218 else if (TYPE_PTRMEMFUNC_P (arg
))
21219 return unify_type_mismatch (explain_p
, parm
, arg
);
21221 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21223 tree t
= NULL_TREE
;
21225 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21227 /* First, we try to unify the PARM and ARG directly. */
21228 t
= try_class_unification (tparms
, targs
,
21229 parm
, arg
, explain_p
);
21233 /* Fallback to the special case allowed in
21234 [temp.deduct.call]:
21236 If P is a class, and P has the form
21237 template-id, then A can be a derived class of
21238 the deduced A. Likewise, if P is a pointer to
21239 a class of the form template-id, A can be a
21240 pointer to a derived class pointed to by the
21242 enum template_base_result r
;
21243 r
= get_template_base (tparms
, targs
, parm
, arg
,
21248 /* Don't give the derived diagnostic if we're
21249 already dealing with the same template. */
21251 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21252 && (CLASSTYPE_TI_TEMPLATE (parm
)
21253 == CLASSTYPE_TI_TEMPLATE (arg
)));
21254 return unify_no_common_base (explain_p
&& !same_template
,
21259 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21260 && (CLASSTYPE_TI_TEMPLATE (parm
)
21261 == CLASSTYPE_TI_TEMPLATE (arg
)))
21262 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21263 Then, we should unify `int' and `U'. */
21266 /* There's no chance of unification succeeding. */
21267 return unify_type_mismatch (explain_p
, parm
, arg
);
21269 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21270 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21272 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21273 return unify_type_mismatch (explain_p
, parm
, arg
);
21274 return unify_success (explain_p
);
21277 case FUNCTION_TYPE
:
21279 unsigned int nargs
;
21284 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21285 return unify_type_mismatch (explain_p
, parm
, arg
);
21287 /* CV qualifications for methods can never be deduced, they must
21288 match exactly. We need to check them explicitly here,
21289 because type_unification_real treats them as any other
21290 cv-qualified parameter. */
21291 if (TREE_CODE (parm
) == METHOD_TYPE
21292 && (!check_cv_quals_for_unify
21294 class_of_this_parm (arg
),
21295 class_of_this_parm (parm
))))
21296 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21297 if (TREE_CODE (arg
) == FUNCTION_TYPE
21298 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21299 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21300 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21301 return unify_type_mismatch (explain_p
, parm
, arg
);
21303 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21304 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21306 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21307 args
= XALLOCAVEC (tree
, nargs
);
21308 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21309 a
!= NULL_TREE
&& a
!= void_list_node
;
21310 a
= TREE_CHAIN (a
), ++i
)
21311 args
[i
] = TREE_VALUE (a
);
21314 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21315 args
, nargs
, 1, DEDUCE_EXACT
,
21316 LOOKUP_NORMAL
, NULL
, explain_p
))
21319 if (flag_noexcept_type
)
21321 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21322 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21323 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21324 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21325 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21326 && uses_template_parms (TREE_PURPOSE (pspec
)))
21327 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21328 TREE_PURPOSE (aspec
),
21329 UNIFY_ALLOW_NONE
, explain_p
);
21330 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21331 return unify_type_mismatch (explain_p
, parm
, arg
);
21338 /* Unify a pointer to member with a pointer to member function, which
21339 deduces the type of the member as a function type. */
21340 if (TYPE_PTRMEMFUNC_P (arg
))
21342 /* Check top-level cv qualifiers */
21343 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21344 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21346 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21347 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21348 UNIFY_ALLOW_NONE
, explain_p
);
21350 /* Determine the type of the function we are unifying against. */
21351 tree fntype
= static_fn_type (arg
);
21353 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21356 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21357 return unify_type_mismatch (explain_p
, parm
, arg
);
21358 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21359 TYPE_OFFSET_BASETYPE (arg
),
21360 UNIFY_ALLOW_NONE
, explain_p
);
21361 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21362 strict
, explain_p
);
21365 if (DECL_TEMPLATE_PARM_P (parm
))
21366 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21367 if (arg
!= scalar_constant_value (parm
))
21368 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21369 return unify_success (explain_p
);
21372 case TEMPLATE_DECL
:
21373 /* Matched cases are handled by the ARG == PARM test above. */
21374 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21377 /* We might get a variable as a non-type template argument in parm if the
21378 corresponding parameter is type-dependent. Make any necessary
21379 adjustments based on whether arg is a reference. */
21380 if (CONSTANT_CLASS_P (arg
))
21381 parm
= fold_non_dependent_expr (parm
);
21382 else if (REFERENCE_REF_P (arg
))
21384 tree sub
= TREE_OPERAND (arg
, 0);
21386 if (TREE_CODE (sub
) == ADDR_EXPR
)
21387 arg
= TREE_OPERAND (sub
, 0);
21389 /* Now use the normal expression code to check whether they match. */
21392 case TYPE_ARGUMENT_PACK
:
21393 case NONTYPE_ARGUMENT_PACK
:
21394 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21395 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21398 case DECLTYPE_TYPE
:
21399 case UNDERLYING_TYPE
:
21400 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21401 or UNDERLYING_TYPE nodes. */
21402 return unify_success (explain_p
);
21405 /* Unification fails if we hit an error node. */
21406 return unify_invalid (explain_p
);
21409 if (REFERENCE_REF_P (parm
))
21411 bool pexp
= PACK_EXPANSION_P (arg
);
21413 arg
= PACK_EXPANSION_PATTERN (arg
);
21414 if (REFERENCE_REF_P (arg
))
21415 arg
= TREE_OPERAND (arg
, 0);
21417 arg
= make_pack_expansion (arg
, complain
);
21418 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21419 strict
, explain_p
);
21424 /* An unresolved overload is a nondeduced context. */
21425 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21426 return unify_success (explain_p
);
21427 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21429 /* We must be looking at an expression. This can happen with
21433 void foo(S<I>, S<I + 2>);
21435 This is a "nondeduced context":
21439 The nondeduced contexts are:
21441 --A type that is a template-id in which one or more of
21442 the template-arguments is an expression that references
21443 a template-parameter.
21445 In these cases, we assume deduction succeeded, but don't
21446 actually infer any unifications. */
21448 if (!uses_template_parms (parm
)
21449 && !template_args_equal (parm
, arg
))
21450 return unify_expression_unequal (explain_p
, parm
, arg
);
21452 return unify_success (explain_p
);
21455 #undef RECUR_AND_CHECK_FAILURE
21457 /* Note that DECL can be defined in this translation unit, if
21461 mark_definable (tree decl
)
21464 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21465 FOR_EACH_CLONE (clone
, decl
)
21466 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21469 /* Called if RESULT is explicitly instantiated, or is a member of an
21470 explicitly instantiated class. */
21473 mark_decl_instantiated (tree result
, int extern_p
)
21475 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21477 /* If this entity has already been written out, it's too late to
21478 make any modifications. */
21479 if (TREE_ASM_WRITTEN (result
))
21482 /* For anonymous namespace we don't need to do anything. */
21483 if (decl_anon_ns_mem_p (result
))
21485 gcc_assert (!TREE_PUBLIC (result
));
21489 if (TREE_CODE (result
) != FUNCTION_DECL
)
21490 /* The TREE_PUBLIC flag for function declarations will have been
21491 set correctly by tsubst. */
21492 TREE_PUBLIC (result
) = 1;
21494 /* This might have been set by an earlier implicit instantiation. */
21495 DECL_COMDAT (result
) = 0;
21498 DECL_NOT_REALLY_EXTERN (result
) = 0;
21501 mark_definable (result
);
21502 mark_needed (result
);
21503 /* Always make artificials weak. */
21504 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21505 comdat_linkage (result
);
21506 /* For WIN32 we also want to put explicit instantiations in
21507 linkonce sections. */
21508 else if (TREE_PUBLIC (result
))
21509 maybe_make_one_only (result
);
21512 /* If EXTERN_P, then this function will not be emitted -- unless
21513 followed by an explicit instantiation, at which point its linkage
21514 will be adjusted. If !EXTERN_P, then this function will be
21515 emitted here. In neither circumstance do we want
21516 import_export_decl to adjust the linkage. */
21517 DECL_INTERFACE_KNOWN (result
) = 1;
21520 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21521 important template arguments. If any are missing, we check whether
21522 they're important by using error_mark_node for substituting into any
21523 args that were used for partial ordering (the ones between ARGS and END)
21524 and seeing if it bubbles up. */
21527 check_undeduced_parms (tree targs
, tree args
, tree end
)
21529 bool found
= false;
21531 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21532 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21535 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21539 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21540 if (substed
== error_mark_node
)
21546 /* Given two function templates PAT1 and PAT2, return:
21548 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21549 -1 if PAT2 is more specialized than PAT1.
21550 0 if neither is more specialized.
21552 LEN indicates the number of parameters we should consider
21553 (defaulted parameters should not be considered).
21555 The 1998 std underspecified function template partial ordering, and
21556 DR214 addresses the issue. We take pairs of arguments, one from
21557 each of the templates, and deduce them against each other. One of
21558 the templates will be more specialized if all the *other*
21559 template's arguments deduce against its arguments and at least one
21560 of its arguments *does* *not* deduce against the other template's
21561 corresponding argument. Deduction is done as for class templates.
21562 The arguments used in deduction have reference and top level cv
21563 qualifiers removed. Iff both arguments were originally reference
21564 types *and* deduction succeeds in both directions, an lvalue reference
21565 wins against an rvalue reference and otherwise the template
21566 with the more cv-qualified argument wins for that pairing (if
21567 neither is more cv-qualified, they both are equal). Unlike regular
21568 deduction, after all the arguments have been deduced in this way,
21569 we do *not* verify the deduced template argument values can be
21570 substituted into non-deduced contexts.
21572 The logic can be a bit confusing here, because we look at deduce1 and
21573 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21574 can find template arguments for pat1 to make arg1 look like arg2, that
21575 means that arg2 is at least as specialized as arg1. */
21578 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21580 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21581 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21582 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21583 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21584 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21585 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21586 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21587 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21588 tree origs1
, origs2
;
21589 bool lose1
= false;
21590 bool lose2
= false;
21592 /* Remove the this parameter from non-static member functions. If
21593 one is a non-static member function and the other is not a static
21594 member function, remove the first parameter from that function
21595 also. This situation occurs for operator functions where we
21596 locate both a member function (with this pointer) and non-member
21597 operator (with explicit first operand). */
21598 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21600 len
--; /* LEN is the number of significant arguments for DECL1 */
21601 args1
= TREE_CHAIN (args1
);
21602 if (!DECL_STATIC_FUNCTION_P (decl2
))
21603 args2
= TREE_CHAIN (args2
);
21605 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21607 args2
= TREE_CHAIN (args2
);
21608 if (!DECL_STATIC_FUNCTION_P (decl1
))
21611 args1
= TREE_CHAIN (args1
);
21615 /* If only one is a conversion operator, they are unordered. */
21616 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21619 /* Consider the return type for a conversion function */
21620 if (DECL_CONV_FN_P (decl1
))
21622 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21623 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21627 processing_template_decl
++;
21633 /* Stop when an ellipsis is seen. */
21634 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21636 tree arg1
= TREE_VALUE (args1
);
21637 tree arg2
= TREE_VALUE (args2
);
21638 int deduce1
, deduce2
;
21644 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21645 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21647 /* When both arguments are pack expansions, we need only
21648 unify the patterns themselves. */
21649 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21650 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21652 /* This is the last comparison we need to do. */
21656 /* DR 1847: If a particular P contains no template-parameters that
21657 participate in template argument deduction, that P is not used to
21658 determine the ordering. */
21659 if (!uses_deducible_template_parms (arg1
)
21660 && !uses_deducible_template_parms (arg2
))
21663 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21665 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21666 arg1
= TREE_TYPE (arg1
);
21667 quals1
= cp_type_quals (arg1
);
21670 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21672 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21673 arg2
= TREE_TYPE (arg2
);
21674 quals2
= cp_type_quals (arg2
);
21677 arg1
= TYPE_MAIN_VARIANT (arg1
);
21678 arg2
= TYPE_MAIN_VARIANT (arg2
);
21680 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21682 int i
, len2
= remaining_arguments (args2
);
21683 tree parmvec
= make_tree_vec (1);
21684 tree argvec
= make_tree_vec (len2
);
21687 /* Setup the parameter vector, which contains only ARG1. */
21688 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21690 /* Setup the argument vector, which contains the remaining
21692 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21693 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21695 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21696 argvec
, DEDUCE_EXACT
,
21697 /*subr=*/true, /*explain_p=*/false)
21700 /* We cannot deduce in the other direction, because ARG1 is
21701 a pack expansion but ARG2 is not. */
21704 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21706 int i
, len1
= remaining_arguments (args1
);
21707 tree parmvec
= make_tree_vec (1);
21708 tree argvec
= make_tree_vec (len1
);
21711 /* Setup the parameter vector, which contains only ARG1. */
21712 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21714 /* Setup the argument vector, which contains the remaining
21716 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21717 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21719 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21720 argvec
, DEDUCE_EXACT
,
21721 /*subr=*/true, /*explain_p=*/false)
21724 /* We cannot deduce in the other direction, because ARG2 is
21725 a pack expansion but ARG1 is not.*/
21731 /* The normal case, where neither argument is a pack
21733 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21734 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21736 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21737 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21741 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21742 arg2, then arg2 is not as specialized as arg1. */
21748 /* "If, for a given type, deduction succeeds in both directions
21749 (i.e., the types are identical after the transformations above)
21750 and both P and A were reference types (before being replaced with
21751 the type referred to above):
21752 - if the type from the argument template was an lvalue reference and
21753 the type from the parameter template was not, the argument type is
21754 considered to be more specialized than the other; otherwise,
21755 - if the type from the argument template is more cv-qualified
21756 than the type from the parameter template (as described above),
21757 the argument type is considered to be more specialized than the other;
21759 - neither type is more specialized than the other." */
21761 if (deduce1
&& deduce2
)
21763 if (ref1
&& ref2
&& ref1
!= ref2
)
21770 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21772 if ((quals1
& quals2
) == quals2
)
21774 if ((quals1
& quals2
) == quals1
)
21779 if (lose1
&& lose2
)
21780 /* We've failed to deduce something in either direction.
21781 These must be unordered. */
21786 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21787 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21788 /* We have already processed all of the arguments in our
21789 handing of the pack expansion type. */
21792 args1
= TREE_CHAIN (args1
);
21793 args2
= TREE_CHAIN (args2
);
21796 /* "In most cases, all template parameters must have values in order for
21797 deduction to succeed, but for partial ordering purposes a template
21798 parameter may remain without a value provided it is not used in the
21799 types being used for partial ordering."
21801 Thus, if we are missing any of the targs1 we need to substitute into
21802 origs1, then pat2 is not as specialized as pat1. This can happen when
21803 there is a nondeduced context. */
21804 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21806 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21809 processing_template_decl
--;
21811 /* If both deductions succeed, the partial ordering selects the more
21812 constrained template. */
21813 if (!lose1
&& !lose2
)
21815 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21816 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21817 lose1
= !subsumes_constraints (c1
, c2
);
21818 lose2
= !subsumes_constraints (c2
, c1
);
21821 /* All things being equal, if the next argument is a pack expansion
21822 for one function but not for the other, prefer the
21823 non-variadic function. FIXME this is bogus; see c++/41958. */
21825 && args1
&& TREE_VALUE (args1
)
21826 && args2
&& TREE_VALUE (args2
))
21828 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21829 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21832 if (lose1
== lose2
)
21840 /* Determine which of two partial specializations of TMPL is more
21843 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21844 to the first partial specialization. The TREE_PURPOSE is the
21845 innermost set of template parameters for the partial
21846 specialization. PAT2 is similar, but for the second template.
21848 Return 1 if the first partial specialization is more specialized;
21849 -1 if the second is more specialized; 0 if neither is more
21852 See [temp.class.order] for information about determining which of
21853 two templates is more specialized. */
21856 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21860 bool any_deductions
= false;
21862 tree tmpl1
= TREE_VALUE (pat1
);
21863 tree tmpl2
= TREE_VALUE (pat2
);
21864 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21865 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21867 /* Just like what happens for functions, if we are ordering between
21868 different template specializations, we may encounter dependent
21869 types in the arguments, and we need our dependency check functions
21870 to behave correctly. */
21871 ++processing_template_decl
;
21872 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21876 any_deductions
= true;
21879 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21883 any_deductions
= true;
21885 --processing_template_decl
;
21887 /* If both deductions succeed, the partial ordering selects the more
21888 constrained template. */
21889 if (!winner
&& any_deductions
)
21890 return more_constrained (tmpl1
, tmpl2
);
21892 /* In the case of a tie where at least one of the templates
21893 has a parameter pack at the end, the template with the most
21894 non-packed parameters wins. */
21897 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21898 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21900 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21901 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21902 int len1
= TREE_VEC_LENGTH (args1
);
21903 int len2
= TREE_VEC_LENGTH (args2
);
21905 /* We don't count the pack expansion at the end. */
21906 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21908 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21913 else if (len1
< len2
)
21920 /* Return the template arguments that will produce the function signature
21921 DECL from the function template FN, with the explicit template
21922 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21923 also match. Return NULL_TREE if no satisfactory arguments could be
21927 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21929 int ntparms
= DECL_NTPARMS (fn
);
21930 tree targs
= make_tree_vec (ntparms
);
21931 tree decl_type
= TREE_TYPE (decl
);
21932 tree decl_arg_types
;
21934 unsigned int nargs
, ix
;
21937 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21939 /* Never do unification on the 'this' parameter. */
21940 decl_arg_types
= skip_artificial_parms_for (decl
,
21941 TYPE_ARG_TYPES (decl_type
));
21943 nargs
= list_length (decl_arg_types
);
21944 args
= XALLOCAVEC (tree
, nargs
);
21945 for (arg
= decl_arg_types
, ix
= 0;
21946 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21947 arg
= TREE_CHAIN (arg
), ++ix
)
21948 args
[ix
] = TREE_VALUE (arg
);
21950 if (fn_type_unification (fn
, explicit_args
, targs
,
21952 (check_rettype
|| DECL_CONV_FN_P (fn
)
21953 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21954 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21956 == error_mark_node
)
21962 /* Return the innermost template arguments that, when applied to a partial
21963 specialization SPEC_TMPL of TMPL, yield the ARGS.
21965 For example, suppose we have:
21967 template <class T, class U> struct S {};
21968 template <class T> struct S<T*, int> {};
21970 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21971 partial specialization and the ARGS will be {double*, int}. The resulting
21972 vector will be {double}, indicating that `T' is bound to `double'. */
21975 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21977 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21979 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21980 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21982 tree innermost_deduced_args
;
21984 innermost_deduced_args
= make_tree_vec (ntparms
);
21985 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21987 deduced_args
= copy_node (args
);
21988 SET_TMPL_ARGS_LEVEL (deduced_args
,
21989 TMPL_ARGS_DEPTH (deduced_args
),
21990 innermost_deduced_args
);
21993 deduced_args
= innermost_deduced_args
;
21995 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21997 if (unify (tparms
, deduced_args
,
21998 INNERMOST_TEMPLATE_ARGS (spec_args
),
21999 INNERMOST_TEMPLATE_ARGS (args
),
22000 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22003 for (i
= 0; i
< ntparms
; ++i
)
22004 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22006 if (!tried_array_deduction
)
22008 try_array_deduction (tparms
, innermost_deduced_args
,
22009 INNERMOST_TEMPLATE_ARGS (spec_args
));
22010 tried_array_deduction
= true;
22011 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22017 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22018 if (!push_tinst_level (tinst
))
22020 excessive_deduction_depth
= true;
22024 /* Verify that nondeduced template arguments agree with the type
22025 obtained from argument deduction.
22029 struct A { typedef int X; };
22030 template <class T, class U> struct C {};
22031 template <class T> struct C<T, typename T::X> {};
22033 Then with the instantiation `C<A, int>', we can deduce that
22034 `T' is `A' but unify () does not check whether `typename T::X'
22036 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22038 if (spec_args
!= error_mark_node
)
22039 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22040 INNERMOST_TEMPLATE_ARGS (spec_args
),
22041 tmpl
, tf_none
, false, false);
22043 pop_tinst_level ();
22045 if (spec_args
== error_mark_node
22046 /* We only need to check the innermost arguments; the other
22047 arguments will always agree. */
22048 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22049 INNERMOST_TEMPLATE_ARGS (args
)))
22052 /* Now that we have bindings for all of the template arguments,
22053 ensure that the arguments deduced for the template template
22054 parameters have compatible template parameter lists. See the use
22055 of template_template_parm_bindings_ok_p in fn_type_unification
22056 for more information. */
22057 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22060 return deduced_args
;
22063 // Compare two function templates T1 and T2 by deducing bindings
22064 // from one against the other. If both deductions succeed, compare
22065 // constraints to see which is more constrained.
22067 more_specialized_inst (tree t1
, tree t2
)
22072 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22078 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22084 // If both deductions succeed, then one may be more constrained.
22085 if (count
== 2 && fate
== 0)
22086 fate
= more_constrained (t1
, t2
);
22091 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22092 Return the TREE_LIST node with the most specialized template, if
22093 any. If there is no most specialized template, the error_mark_node
22096 Note that this function does not look at, or modify, the
22097 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22098 returned is one of the elements of INSTANTIATIONS, callers may
22099 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22100 and retrieve it from the value returned. */
22103 most_specialized_instantiation (tree templates
)
22107 ++processing_template_decl
;
22110 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22112 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22113 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22118 /* Equally specialized, move to next function. If there
22119 is no next function, nothing's most specialized. */
22120 fn
= TREE_CHAIN (fn
);
22128 /* Now verify that champ is better than everything earlier in the
22129 instantiation list. */
22130 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22131 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22138 processing_template_decl
--;
22141 return error_mark_node
;
22146 /* If DECL is a specialization of some template, return the most
22147 general such template. Otherwise, returns NULL_TREE.
22149 For example, given:
22151 template <class T> struct S { template <class U> void f(U); };
22153 if TMPL is `template <class U> void S<int>::f(U)' this will return
22154 the full template. This function will not trace past partial
22155 specializations, however. For example, given in addition:
22157 template <class T> struct S<T*> { template <class U> void f(U); };
22159 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22160 `template <class T> template <class U> S<T*>::f(U)'. */
22163 most_general_template (tree decl
)
22165 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22167 if (tree tinfo
= get_template_info (decl
))
22168 decl
= TI_TEMPLATE (tinfo
);
22169 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22170 template friend, or a FIELD_DECL for a capture pack. */
22171 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22175 /* Look for more and more general templates. */
22176 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22178 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22179 (See cp-tree.h for details.) */
22180 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22183 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22184 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22185 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22188 /* Stop if we run into an explicitly specialized class template. */
22189 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22190 && DECL_CONTEXT (decl
)
22191 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22194 decl
= DECL_TI_TEMPLATE (decl
);
22200 /* Return the most specialized of the template partial specializations
22201 which can produce TARGET, a specialization of some class or variable
22202 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22203 a TEMPLATE_DECL node corresponding to the partial specialization, while
22204 the TREE_PURPOSE is the set of template arguments that must be
22205 substituted into the template pattern in order to generate TARGET.
22207 If the choice of partial specialization is ambiguous, a diagnostic
22208 is issued, and the error_mark_node is returned. If there are no
22209 partial specializations matching TARGET, then NULL_TREE is
22210 returned, indicating that the primary template should be used. */
22213 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22215 tree list
= NULL_TREE
;
22220 tree outer_args
= NULL_TREE
;
22223 if (TYPE_P (target
))
22225 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22226 tmpl
= TI_TEMPLATE (tinfo
);
22227 args
= TI_ARGS (tinfo
);
22229 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22231 tmpl
= TREE_OPERAND (target
, 0);
22232 args
= TREE_OPERAND (target
, 1);
22234 else if (VAR_P (target
))
22236 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22237 tmpl
= TI_TEMPLATE (tinfo
);
22238 args
= TI_ARGS (tinfo
);
22241 gcc_unreachable ();
22243 tree main_tmpl
= most_general_template (tmpl
);
22245 /* For determining which partial specialization to use, only the
22246 innermost args are interesting. */
22247 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22249 outer_args
= strip_innermost_template_args (args
, 1);
22250 args
= INNERMOST_TEMPLATE_ARGS (args
);
22253 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22256 tree spec_tmpl
= TREE_VALUE (t
);
22260 /* Substitute in the template args from the enclosing class. */
22261 ++processing_template_decl
;
22262 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22263 --processing_template_decl
;
22266 if (spec_tmpl
== error_mark_node
)
22267 return error_mark_node
;
22269 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22273 spec_args
= add_to_template_args (outer_args
, spec_args
);
22275 /* Keep the candidate only if the constraints are satisfied,
22276 or if we're not compiling with concepts. */
22278 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22280 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22281 TREE_TYPE (list
) = TREE_TYPE (t
);
22289 ambiguous_p
= false;
22292 t
= TREE_CHAIN (t
);
22293 for (; t
; t
= TREE_CHAIN (t
))
22295 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22302 t
= TREE_CHAIN (t
);
22305 ambiguous_p
= true;
22314 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22316 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22319 ambiguous_p
= true;
22327 char *spaces
= NULL
;
22328 if (!(complain
& tf_error
))
22329 return error_mark_node
;
22330 if (TYPE_P (target
))
22331 error ("ambiguous template instantiation for %q#T", target
);
22333 error ("ambiguous template instantiation for %q#D", target
);
22334 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22335 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22337 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22338 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22339 "%s %#qS", spaces
? spaces
: str
, subst
);
22340 spaces
= spaces
? spaces
: get_spaces (str
);
22343 return error_mark_node
;
22349 /* Explicitly instantiate DECL. */
22352 do_decl_instantiation (tree decl
, tree storage
)
22354 tree result
= NULL_TREE
;
22357 if (!decl
|| decl
== error_mark_node
)
22358 /* An error occurred, for which grokdeclarator has already issued
22359 an appropriate message. */
22361 else if (! DECL_LANG_SPECIFIC (decl
))
22363 error ("explicit instantiation of non-template %q#D", decl
);
22367 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22368 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22370 if (VAR_P (decl
) && !var_templ
)
22372 /* There is an asymmetry here in the way VAR_DECLs and
22373 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22374 the latter, the DECL we get back will be marked as a
22375 template instantiation, and the appropriate
22376 DECL_TEMPLATE_INFO will be set up. This does not happen for
22377 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22378 should handle VAR_DECLs as it currently handles
22380 if (!DECL_CLASS_SCOPE_P (decl
))
22382 error ("%qD is not a static data member of a class template", decl
);
22385 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22386 if (!result
|| !VAR_P (result
))
22388 error ("no matching template for %qD found", decl
);
22391 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22393 error ("type %qT for explicit instantiation %qD does not match "
22394 "declared type %qT", TREE_TYPE (result
), decl
,
22399 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22401 error ("explicit instantiation of %q#D", decl
);
22407 /* Check for various error cases. Note that if the explicit
22408 instantiation is valid the RESULT will currently be marked as an
22409 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22410 until we get here. */
22412 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22414 /* DR 259 [temp.spec].
22416 Both an explicit instantiation and a declaration of an explicit
22417 specialization shall not appear in a program unless the explicit
22418 instantiation follows a declaration of the explicit specialization.
22420 For a given set of template parameters, if an explicit
22421 instantiation of a template appears after a declaration of an
22422 explicit specialization for that template, the explicit
22423 instantiation has no effect. */
22426 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22430 No program shall explicitly instantiate any template more
22433 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22434 the first instantiation was `extern' and the second is not,
22435 and EXTERN_P for the opposite case. */
22436 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22437 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22438 /* If an "extern" explicit instantiation follows an ordinary
22439 explicit instantiation, the template is instantiated. */
22443 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22445 error ("no matching template for %qD found", result
);
22448 else if (!DECL_TEMPLATE_INFO (result
))
22450 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22454 if (storage
== NULL_TREE
)
22456 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22458 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22459 pedwarn (input_location
, OPT_Wpedantic
,
22460 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22465 error ("storage class %qD applied to template instantiation", storage
);
22467 check_explicit_instantiation_namespace (result
);
22468 mark_decl_instantiated (result
, extern_p
);
22470 instantiate_decl (result
, /*defer_ok=*/true,
22471 /*expl_inst_class_mem_p=*/false);
22475 mark_class_instantiated (tree t
, int extern_p
)
22477 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22478 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22479 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22480 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22483 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22484 rest_of_type_compilation (t
, 1);
22488 /* Called from do_type_instantiation through binding_table_foreach to
22489 do recursive instantiation for the type bound in ENTRY. */
22491 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22493 tree storage
= *(tree
*) data
;
22495 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22496 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22497 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22500 /* Perform an explicit instantiation of template class T. STORAGE, if
22501 non-null, is the RID for extern, inline or static. COMPLAIN is
22502 nonzero if this is called from the parser, zero if called recursively,
22503 since the standard is unclear (as detailed below). */
22506 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22511 int previous_instantiation_extern_p
= 0;
22513 if (TREE_CODE (t
) == TYPE_DECL
)
22516 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22519 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22521 error ("explicit instantiation of non-class template %qD", tmpl
);
22523 error ("explicit instantiation of non-template type %qT", t
);
22529 if (!COMPLETE_TYPE_P (t
))
22531 if (complain
& tf_error
)
22532 error ("explicit instantiation of %q#T before definition of template",
22537 if (storage
!= NULL_TREE
)
22539 if (!in_system_header_at (input_location
))
22541 if (storage
== ridpointers
[(int) RID_EXTERN
])
22543 if (cxx_dialect
== cxx98
)
22544 pedwarn (input_location
, OPT_Wpedantic
,
22545 "ISO C++ 1998 forbids the use of %<extern%> on "
22546 "explicit instantiations");
22549 pedwarn (input_location
, OPT_Wpedantic
,
22550 "ISO C++ forbids the use of %qE"
22551 " on explicit instantiations", storage
);
22554 if (storage
== ridpointers
[(int) RID_INLINE
])
22556 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22558 else if (storage
== ridpointers
[(int) RID_STATIC
])
22562 error ("storage class %qD applied to template instantiation",
22568 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22570 /* DR 259 [temp.spec].
22572 Both an explicit instantiation and a declaration of an explicit
22573 specialization shall not appear in a program unless the explicit
22574 instantiation follows a declaration of the explicit specialization.
22576 For a given set of template parameters, if an explicit
22577 instantiation of a template appears after a declaration of an
22578 explicit specialization for that template, the explicit
22579 instantiation has no effect. */
22582 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22586 No program shall explicitly instantiate any template more
22589 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22590 instantiation was `extern'. If EXTERN_P then the second is.
22591 These cases are OK. */
22592 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22594 if (!previous_instantiation_extern_p
&& !extern_p
22595 && (complain
& tf_error
))
22596 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22598 /* If we've already instantiated the template, just return now. */
22599 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22603 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22604 mark_class_instantiated (t
, extern_p
);
22609 /* In contrast to implicit instantiation, where only the
22610 declarations, and not the definitions, of members are
22611 instantiated, we have here:
22615 The explicit instantiation of a class template specialization
22616 implies the instantiation of all of its members not
22617 previously explicitly specialized in the translation unit
22618 containing the explicit instantiation.
22620 Of course, we can't instantiate member template classes, since we
22621 don't have any arguments for them. Note that the standard is
22622 unclear on whether the instantiation of the members are
22623 *explicit* instantiations or not. However, the most natural
22624 interpretation is that it should be an explicit
22626 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22628 || (TREE_CODE (fld
) == FUNCTION_DECL
22630 && user_provided_p (fld
)))
22631 && DECL_TEMPLATE_INSTANTIATION (fld
))
22633 mark_decl_instantiated (fld
, extern_p
);
22635 instantiate_decl (fld
, /*defer_ok=*/true,
22636 /*expl_inst_class_mem_p=*/true);
22639 if (CLASSTYPE_NESTED_UTDS (t
))
22640 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22641 bt_instantiate_type_proc
, &storage
);
22644 /* Given a function DECL, which is a specialization of TMPL, modify
22645 DECL to be a re-instantiation of TMPL with the same template
22646 arguments. TMPL should be the template into which tsubst'ing
22647 should occur for DECL, not the most general template.
22649 One reason for doing this is a scenario like this:
22652 void f(const T&, int i);
22654 void g() { f(3, 7); }
22657 void f(const T& t, const int i) { }
22659 Note that when the template is first instantiated, with
22660 instantiate_template, the resulting DECL will have no name for the
22661 first parameter, and the wrong type for the second. So, when we go
22662 to instantiate the DECL, we regenerate it. */
22665 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22667 /* The arguments used to instantiate DECL, from the most general
22671 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22673 /* Make sure that we can see identifiers, and compute access
22675 push_access_scope (decl
);
22677 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22685 args_depth
= TMPL_ARGS_DEPTH (args
);
22686 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22687 if (args_depth
> parms_depth
)
22688 args
= get_innermost_template_args (args
, parms_depth
);
22690 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22691 args
, tf_error
, NULL_TREE
,
22692 /*defer_ok*/false);
22693 if (specs
&& specs
!= error_mark_node
)
22694 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22697 /* Merge parameter declarations. */
22698 decl_parm
= skip_artificial_parms_for (decl
,
22699 DECL_ARGUMENTS (decl
));
22701 = skip_artificial_parms_for (code_pattern
,
22702 DECL_ARGUMENTS (code_pattern
));
22703 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22708 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22709 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22710 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22712 parm_type
= type_decays_to (parm_type
);
22713 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22714 TREE_TYPE (decl_parm
) = parm_type
;
22715 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22716 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22718 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22719 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22721 decl_parm
= DECL_CHAIN (decl_parm
);
22722 pattern_parm
= DECL_CHAIN (pattern_parm
);
22724 /* Merge any parameters that match with the function parameter
22726 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22729 tree expanded_types
;
22730 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22731 the parameters in this function parameter pack. */
22732 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22733 args
, tf_error
, NULL_TREE
);
22734 len
= TREE_VEC_LENGTH (expanded_types
);
22735 for (i
= 0; i
< len
; i
++)
22740 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22741 /* Rename the parameter to include the index. */
22742 DECL_NAME (decl_parm
) =
22743 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22744 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22745 parm_type
= type_decays_to (parm_type
);
22746 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22747 TREE_TYPE (decl_parm
) = parm_type
;
22748 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22749 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22751 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22752 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22754 decl_parm
= DECL_CHAIN (decl_parm
);
22757 /* Merge additional specifiers from the CODE_PATTERN. */
22758 if (DECL_DECLARED_INLINE_P (code_pattern
)
22759 && !DECL_DECLARED_INLINE_P (decl
))
22760 DECL_DECLARED_INLINE_P (decl
) = 1;
22762 else if (VAR_P (decl
))
22764 start_lambda_scope (decl
);
22765 DECL_INITIAL (decl
) =
22766 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22767 tf_error
, DECL_TI_TEMPLATE (decl
),
22768 /*integral_constant_expression_p=*/false);
22769 finish_lambda_scope ();
22770 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22771 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22772 tf_error
, DECL_TI_TEMPLATE (decl
));
22775 gcc_unreachable ();
22777 pop_access_scope (decl
);
22780 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22781 substituted to get DECL. */
22784 template_for_substitution (tree decl
)
22786 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22788 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22789 for the instantiation. This is not always the most general
22790 template. Consider, for example:
22793 struct S { template <class U> void f();
22794 template <> void f<int>(); };
22796 and an instantiation of S<double>::f<int>. We want TD to be the
22797 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22798 while (/* An instantiation cannot have a definition, so we need a
22799 more general template. */
22800 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22801 /* We must also deal with friend templates. Given:
22803 template <class T> struct S {
22804 template <class U> friend void f() {};
22807 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22808 so far as the language is concerned, but that's still
22809 where we get the pattern for the instantiation from. On
22810 other hand, if the definition comes outside the class, say:
22812 template <class T> struct S {
22813 template <class U> friend void f();
22815 template <class U> friend void f() {}
22817 we don't need to look any further. That's what the check for
22818 DECL_INITIAL is for. */
22819 || (TREE_CODE (decl
) == FUNCTION_DECL
22820 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22821 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22823 /* The present template, TD, should not be a definition. If it
22824 were a definition, we should be using it! Note that we
22825 cannot restructure the loop to just keep going until we find
22826 a template with a definition, since that might go too far if
22827 a specialization was declared, but not defined. */
22829 /* Fetch the more general template. */
22830 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22836 /* Returns true if we need to instantiate this template instance even if we
22837 know we aren't going to emit it. */
22840 always_instantiate_p (tree decl
)
22842 /* We always instantiate inline functions so that we can inline them. An
22843 explicit instantiation declaration prohibits implicit instantiation of
22844 non-inline functions. With high levels of optimization, we would
22845 normally inline non-inline functions -- but we're not allowed to do
22846 that for "extern template" functions. Therefore, we check
22847 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22848 return ((TREE_CODE (decl
) == FUNCTION_DECL
22849 && (DECL_DECLARED_INLINE_P (decl
)
22850 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22851 /* And we need to instantiate static data members so that
22852 their initializers are available in integral constant
22855 && decl_maybe_constant_var_p (decl
)));
22858 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22859 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22860 error, true otherwise. */
22863 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22865 tree fntype
, spec
, noex
, clone
;
22867 /* Don't instantiate a noexcept-specification from template context. */
22868 if (processing_template_decl
)
22871 if (DECL_CLONED_FUNCTION_P (fn
))
22872 fn
= DECL_CLONED_FUNCTION (fn
);
22873 fntype
= TREE_TYPE (fn
);
22874 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22876 if (!spec
|| !TREE_PURPOSE (spec
))
22879 noex
= TREE_PURPOSE (spec
);
22881 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22883 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22884 bool added
= false;
22885 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22886 spec
= get_defaulted_eh_spec (fn
, complain
);
22887 else if (!(added
= !fns
->add (fn
)))
22889 /* If hash_set::add returns true, the element was already there. */
22890 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22891 DECL_SOURCE_LOCATION (fn
));
22893 "exception specification of %qD depends on itself",
22895 spec
= noexcept_false_spec
;
22897 else if (push_tinst_level (fn
))
22899 push_access_scope (fn
);
22900 push_deferring_access_checks (dk_no_deferred
);
22901 input_location
= DECL_SOURCE_LOCATION (fn
);
22902 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22903 DEFERRED_NOEXCEPT_ARGS (noex
),
22904 tf_warning_or_error
, fn
,
22905 /*function_p=*/false,
22906 /*integral_constant_expression_p=*/true);
22907 pop_deferring_access_checks ();
22908 pop_access_scope (fn
);
22909 pop_tinst_level ();
22910 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22911 if (spec
== error_mark_node
)
22912 spec
= noexcept_false_spec
;
22915 spec
= noexcept_false_spec
;
22920 if (spec
== error_mark_node
)
22923 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22926 FOR_EACH_CLONE (clone
, fn
)
22928 if (TREE_TYPE (clone
) == fntype
)
22929 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22931 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22937 /* We're starting to process the function INST, an instantiation of PATTERN;
22938 add their parameters to local_specializations. */
22941 register_parameter_specializations (tree pattern
, tree inst
)
22943 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
22944 tree spec_parm
= DECL_ARGUMENTS (inst
);
22945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
22947 register_local_specialization (spec_parm
, tmpl_parm
);
22948 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
22949 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
22951 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22953 if (!DECL_PACK_P (tmpl_parm
))
22955 register_local_specialization (spec_parm
, tmpl_parm
);
22956 spec_parm
= DECL_CHAIN (spec_parm
);
22960 /* Register the (value) argument pack as a specialization of
22961 TMPL_PARM, then move on. */
22962 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22963 register_local_specialization (argpack
, tmpl_parm
);
22966 gcc_assert (!spec_parm
);
22969 /* Produce the definition of D, a _DECL generated from a template. If
22970 DEFER_OK is true, then we don't have to actually do the
22971 instantiation now; we just have to do it sometime. Normally it is
22972 an error if this is an explicit instantiation but D is undefined.
22973 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22974 instantiated class template. */
22977 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
22979 tree tmpl
= DECL_TI_TEMPLATE (d
);
22986 bool pattern_defined
;
22987 location_t saved_loc
= input_location
;
22988 int saved_unevaluated_operand
= cp_unevaluated_operand
;
22989 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22993 /* This function should only be used to instantiate templates for
22994 functions and static member variables. */
22995 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
22997 /* A concept is never instantiated. */
22998 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23000 /* Variables are never deferred; if instantiation is required, they
23001 are instantiated right away. That allows for better code in the
23002 case that an expression refers to the value of the variable --
23003 if the variable has a constant value the referring expression can
23004 take advantage of that fact. */
23008 /* Don't instantiate cloned functions. Instead, instantiate the
23009 functions they cloned. */
23010 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23011 d
= DECL_CLONED_FUNCTION (d
);
23013 if (DECL_TEMPLATE_INSTANTIATED (d
)
23014 || (TREE_CODE (d
) == FUNCTION_DECL
23015 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23016 || DECL_TEMPLATE_SPECIALIZATION (d
))
23017 /* D has already been instantiated or explicitly specialized, so
23018 there's nothing for us to do here.
23020 It might seem reasonable to check whether or not D is an explicit
23021 instantiation, and, if so, stop here. But when an explicit
23022 instantiation is deferred until the end of the compilation,
23023 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23024 the instantiation. */
23027 /* Check to see whether we know that this template will be
23028 instantiated in some other file, as with "extern template"
23030 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23032 /* In general, we do not instantiate such templates. */
23033 if (external_p
&& !always_instantiate_p (d
))
23036 gen_tmpl
= most_general_template (tmpl
);
23037 gen_args
= DECL_TI_ARGS (d
);
23039 if (tmpl
!= gen_tmpl
)
23040 /* We should already have the extra args. */
23041 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23042 == TMPL_ARGS_DEPTH (gen_args
));
23043 /* And what's in the hash table should match D. */
23044 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23045 || spec
== NULL_TREE
);
23047 /* This needs to happen before any tsubsting. */
23048 if (! push_tinst_level (d
))
23051 timevar_push (TV_TEMPLATE_INST
);
23053 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23054 for the instantiation. */
23055 td
= template_for_substitution (d
);
23060 /* Look up an explicit specialization, if any. */
23061 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23062 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23063 if (elt
&& elt
!= error_mark_node
)
23065 td
= TREE_VALUE (elt
);
23066 args
= TREE_PURPOSE (elt
);
23070 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23072 /* We should never be trying to instantiate a member of a class
23073 template or partial specialization. */
23074 gcc_assert (d
!= code_pattern
);
23076 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23077 || DECL_TEMPLATE_SPECIALIZATION (td
))
23078 /* In the case of a friend template whose definition is provided
23079 outside the class, we may have too many arguments. Drop the
23080 ones we don't need. The same is true for specializations. */
23081 args
= get_innermost_template_args
23082 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23084 if (TREE_CODE (d
) == FUNCTION_DECL
)
23086 deleted_p
= DECL_DELETED_FN (code_pattern
);
23087 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23088 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23089 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23095 if (DECL_CLASS_SCOPE_P (code_pattern
))
23096 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23097 || DECL_INLINE_VAR_P (code_pattern
));
23099 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23102 /* We may be in the middle of deferred access check. Disable it now. */
23103 push_deferring_access_checks (dk_no_deferred
);
23105 /* Unless an explicit instantiation directive has already determined
23106 the linkage of D, remember that a definition is available for
23108 if (pattern_defined
23109 && !DECL_INTERFACE_KNOWN (d
)
23110 && !DECL_NOT_REALLY_EXTERN (d
))
23111 mark_definable (d
);
23113 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23114 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23115 input_location
= DECL_SOURCE_LOCATION (d
);
23117 /* If D is a member of an explicitly instantiated class template,
23118 and no definition is available, treat it like an implicit
23120 if (!pattern_defined
&& expl_inst_class_mem_p
23121 && DECL_EXPLICIT_INSTANTIATION (d
))
23123 /* Leave linkage flags alone on instantiations with anonymous
23125 if (TREE_PUBLIC (d
))
23127 DECL_NOT_REALLY_EXTERN (d
) = 0;
23128 DECL_INTERFACE_KNOWN (d
) = 0;
23130 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23133 /* Defer all other templates, unless we have been explicitly
23134 forbidden from doing so. */
23135 if (/* If there is no definition, we cannot instantiate the
23138 /* If it's OK to postpone instantiation, do so. */
23140 /* If this is a static data member that will be defined
23141 elsewhere, we don't want to instantiate the entire data
23142 member, but we do want to instantiate the initializer so that
23143 we can substitute that elsewhere. */
23144 || (external_p
&& VAR_P (d
))
23145 /* Handle here a deleted function too, avoid generating
23146 its body (c++/61080). */
23149 /* The definition of the static data member is now required so
23150 we must substitute the initializer. */
23152 && !DECL_INITIAL (d
)
23153 && DECL_INITIAL (code_pattern
))
23157 bool const_init
= false;
23158 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23160 ns
= decl_namespace_context (d
);
23161 push_nested_namespace (ns
);
23163 push_nested_class (DECL_CONTEXT (d
));
23164 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23166 tf_warning_or_error
, NULL_TREE
,
23167 /*integral_constant_expression_p=*/false);
23168 /* If instantiating the initializer involved instantiating this
23169 again, don't call cp_finish_decl twice. */
23170 if (!DECL_INITIAL (d
))
23172 /* Make sure the initializer is still constant, in case of
23173 circular dependency (template/instantiate6.C). */
23175 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23176 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23177 /*asmspec_tree=*/NULL_TREE
,
23178 LOOKUP_ONLYCONVERTING
);
23181 pop_nested_class ();
23182 pop_nested_namespace (ns
);
23185 /* We restore the source position here because it's used by
23186 add_pending_template. */
23187 input_location
= saved_loc
;
23189 if (at_eof
&& !pattern_defined
23190 && DECL_EXPLICIT_INSTANTIATION (d
)
23191 && DECL_NOT_REALLY_EXTERN (d
))
23194 The definition of a non-exported function template, a
23195 non-exported member function template, or a non-exported
23196 member function or static data member of a class template
23197 shall be present in every translation unit in which it is
23198 explicitly instantiated. */
23199 permerror (input_location
, "explicit instantiation of %qD "
23200 "but no definition available", d
);
23202 /* If we're in unevaluated context, we just wanted to get the
23203 constant value; this isn't an odr use, so don't queue
23204 a full instantiation. */
23205 if (cp_unevaluated_operand
!= 0)
23207 /* ??? Historically, we have instantiated inline functions, even
23208 when marked as "extern template". */
23209 if (!(external_p
&& VAR_P (d
)))
23210 add_pending_template (d
);
23213 /* Tell the repository that D is available in this translation unit
23214 -- and see if it is supposed to be instantiated here. */
23215 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23217 /* In a PCH file, despite the fact that the repository hasn't
23218 requested instantiation in the PCH it is still possible that
23219 an instantiation will be required in a file that includes the
23222 add_pending_template (d
);
23223 /* Instantiate inline functions so that the inliner can do its
23224 job, even though we'll not be emitting a copy of this
23226 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23230 bool push_to_top
, nested
;
23232 fn_context
= decl_function_context (d
);
23233 nested
= current_function_decl
!= NULL_TREE
;
23234 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23236 vec
<tree
> omp_privatization_save
;
23238 save_omp_privatization_clauses (omp_privatization_save
);
23241 push_to_top_level ();
23244 push_function_context ();
23245 cp_unevaluated_operand
= 0;
23246 c_inhibit_evaluation_warnings
= 0;
23249 /* Mark D as instantiated so that recursive calls to
23250 instantiate_decl do not try to instantiate it again. */
23251 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23253 /* Regenerate the declaration in case the template has been modified
23254 by a subsequent redeclaration. */
23255 regenerate_decl_from_template (d
, td
, args
);
23257 /* We already set the file and line above. Reset them now in case
23258 they changed as a result of calling regenerate_decl_from_template. */
23259 input_location
= DECL_SOURCE_LOCATION (d
);
23264 bool const_init
= false;
23266 /* Clear out DECL_RTL; whatever was there before may not be right
23267 since we've reset the type of the declaration. */
23268 SET_DECL_RTL (d
, NULL
);
23269 DECL_IN_AGGR_P (d
) = 0;
23271 /* The initializer is placed in DECL_INITIAL by
23272 regenerate_decl_from_template so we don't need to
23273 push/pop_access_scope again here. Pull it out so that
23274 cp_finish_decl can process it. */
23275 init
= DECL_INITIAL (d
);
23276 DECL_INITIAL (d
) = NULL_TREE
;
23277 DECL_INITIALIZED_P (d
) = 0;
23279 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23280 initializer. That function will defer actual emission until
23281 we have a chance to determine linkage. */
23282 DECL_EXTERNAL (d
) = 0;
23284 /* Enter the scope of D so that access-checking works correctly. */
23285 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23287 push_nested_class (DECL_CONTEXT (d
));
23289 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23290 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23293 pop_nested_class ();
23295 if (variable_template_p (gen_tmpl
))
23296 note_variable_template_instantiation (d
);
23298 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23299 synthesize_method (d
);
23300 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23302 /* Set up the list of local specializations. */
23303 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23304 tree block
= NULL_TREE
;
23306 /* Set up context. */
23307 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23308 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23309 block
= push_stmt_list ();
23311 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23313 /* Some typedefs referenced from within the template code need to be
23314 access checked at template instantiation time, i.e now. These
23315 types were added to the template at parsing time. Let's get those
23316 and perform the access checks then. */
23317 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23320 /* Create substitution entries for the parameters. */
23321 register_parameter_specializations (code_pattern
, d
);
23323 /* Substitute into the body of the function. */
23324 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23325 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23326 tf_warning_or_error
, tmpl
);
23329 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23330 tf_warning_or_error
, tmpl
,
23331 /*integral_constant_expression_p=*/false);
23333 /* Set the current input_location to the end of the function
23334 so that finish_function knows where we are. */
23336 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23338 /* Remember if we saw an infinite loop in the template. */
23339 current_function_infinite_loop
23340 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23343 /* Finish the function. */
23344 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23345 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23346 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23349 d
= finish_function (/*inline_p=*/false);
23350 expand_or_defer_fn (d
);
23353 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23354 cp_check_omp_declare_reduction (d
);
23357 /* We're not deferring instantiation any more. */
23358 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23361 pop_from_top_level ();
23363 pop_function_context ();
23366 restore_omp_privatization_clauses (omp_privatization_save
);
23369 pop_deferring_access_checks ();
23370 timevar_pop (TV_TEMPLATE_INST
);
23371 pop_tinst_level ();
23372 input_location
= saved_loc
;
23373 cp_unevaluated_operand
= saved_unevaluated_operand
;
23374 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23379 /* Run through the list of templates that we wish we could
23380 instantiate, and instantiate any we can. RETRIES is the
23381 number of times we retry pending template instantiation. */
23384 instantiate_pending_templates (int retries
)
23387 location_t saved_loc
= input_location
;
23389 /* Instantiating templates may trigger vtable generation. This in turn
23390 may require further template instantiations. We place a limit here
23391 to avoid infinite loop. */
23392 if (pending_templates
&& retries
>= max_tinst_depth
)
23394 tree decl
= pending_templates
->tinst
->decl
;
23396 fatal_error (input_location
,
23397 "template instantiation depth exceeds maximum of %d"
23398 " instantiating %q+D, possibly from virtual table generation"
23399 " (use -ftemplate-depth= to increase the maximum)",
23400 max_tinst_depth
, decl
);
23401 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23402 /* Pretend that we defined it. */
23403 DECL_INITIAL (decl
) = error_mark_node
;
23409 struct pending_template
**t
= &pending_templates
;
23410 struct pending_template
*last
= NULL
;
23414 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23415 bool complete
= false;
23417 if (TYPE_P (instantiation
))
23419 if (!COMPLETE_TYPE_P (instantiation
))
23421 instantiate_class_template (instantiation
);
23422 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23423 for (tree fld
= TYPE_FIELDS (instantiation
);
23424 fld
; fld
= TREE_CHAIN (fld
))
23426 || (TREE_CODE (fld
) == FUNCTION_DECL
23427 && !DECL_ARTIFICIAL (fld
)))
23428 && DECL_TEMPLATE_INSTANTIATION (fld
))
23429 instantiate_decl (fld
,
23430 /*defer_ok=*/false,
23431 /*expl_inst_class_mem_p=*/false);
23433 if (COMPLETE_TYPE_P (instantiation
))
23437 complete
= COMPLETE_TYPE_P (instantiation
);
23441 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23442 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23445 = instantiate_decl (instantiation
,
23446 /*defer_ok=*/false,
23447 /*expl_inst_class_mem_p=*/false);
23448 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23452 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23453 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23457 /* If INSTANTIATION has been instantiated, then we don't
23458 need to consider it again in the future. */
23466 current_tinst_level
= NULL
;
23468 last_pending_template
= last
;
23470 while (reconsider
);
23472 input_location
= saved_loc
;
23475 /* Substitute ARGVEC into T, which is a list of initializers for
23476 either base class or a non-static data member. The TREE_PURPOSEs
23477 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23478 instantiate_decl. */
23481 tsubst_initializer_list (tree t
, tree argvec
)
23483 tree inits
= NULL_TREE
;
23485 for (; t
; t
= TREE_CHAIN (t
))
23489 tree expanded_bases
= NULL_TREE
;
23490 tree expanded_arguments
= NULL_TREE
;
23493 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23498 /* Expand the base class expansion type into separate base
23500 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23501 tf_warning_or_error
,
23503 if (expanded_bases
== error_mark_node
)
23506 /* We'll be building separate TREE_LISTs of arguments for
23508 len
= TREE_VEC_LENGTH (expanded_bases
);
23509 expanded_arguments
= make_tree_vec (len
);
23510 for (i
= 0; i
< len
; i
++)
23511 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23513 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23514 expand each argument in the TREE_VALUE of t. */
23515 expr
= make_node (EXPR_PACK_EXPANSION
);
23516 PACK_EXPANSION_LOCAL_P (expr
) = true;
23517 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23518 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23520 if (TREE_VALUE (t
) == void_type_node
)
23521 /* VOID_TYPE_NODE is used to indicate
23522 value-initialization. */
23524 for (i
= 0; i
< len
; i
++)
23525 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23529 /* Substitute parameter packs into each argument in the
23531 in_base_initializer
= 1;
23532 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23534 tree expanded_exprs
;
23536 /* Expand the argument. */
23537 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23539 = tsubst_pack_expansion (expr
, argvec
,
23540 tf_warning_or_error
,
23542 if (expanded_exprs
== error_mark_node
)
23545 /* Prepend each of the expanded expressions to the
23546 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23547 for (i
= 0; i
< len
; i
++)
23549 TREE_VEC_ELT (expanded_arguments
, i
) =
23550 tree_cons (NULL_TREE
,
23551 TREE_VEC_ELT (expanded_exprs
, i
),
23552 TREE_VEC_ELT (expanded_arguments
, i
));
23555 in_base_initializer
= 0;
23557 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23558 since we built them backwards. */
23559 for (i
= 0; i
< len
; i
++)
23561 TREE_VEC_ELT (expanded_arguments
, i
) =
23562 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23567 for (i
= 0; i
< len
; ++i
)
23569 if (expanded_bases
)
23571 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23572 decl
= expand_member_init (decl
);
23573 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23578 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23579 tf_warning_or_error
, NULL_TREE
);
23581 decl
= expand_member_init (decl
);
23582 if (decl
&& !DECL_P (decl
))
23583 in_base_initializer
= 1;
23585 init
= TREE_VALUE (t
);
23587 if (init
!= void_type_node
)
23588 init
= tsubst_expr (init
, argvec
,
23589 tf_warning_or_error
, NULL_TREE
,
23590 /*integral_constant_expression_p=*/false);
23591 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23592 /* If we had an initializer but it instantiated to nothing,
23593 value-initialize the object. This will only occur when
23594 the initializer was a pack expansion where the parameter
23595 packs used in that expansion were of length zero. */
23596 init
= void_type_node
;
23597 in_base_initializer
= 0;
23602 init
= build_tree_list (decl
, init
);
23603 TREE_CHAIN (init
) = inits
;
23611 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23614 set_current_access_from_decl (tree decl
)
23616 if (TREE_PRIVATE (decl
))
23617 current_access_specifier
= access_private_node
;
23618 else if (TREE_PROTECTED (decl
))
23619 current_access_specifier
= access_protected_node
;
23621 current_access_specifier
= access_public_node
;
23624 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23625 is the instantiation (which should have been created with
23626 start_enum) and ARGS are the template arguments to use. */
23629 tsubst_enum (tree tag
, tree newtag
, tree args
)
23633 if (SCOPED_ENUM_P (newtag
))
23634 begin_scope (sk_scoped_enum
, newtag
);
23636 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23641 decl
= TREE_VALUE (e
);
23642 /* Note that in a template enum, the TREE_VALUE is the
23643 CONST_DECL, not the corresponding INTEGER_CST. */
23644 value
= tsubst_expr (DECL_INITIAL (decl
),
23645 args
, tf_warning_or_error
, NULL_TREE
,
23646 /*integral_constant_expression_p=*/true);
23648 /* Give this enumeration constant the correct access. */
23649 set_current_access_from_decl (decl
);
23651 /* Actually build the enumerator itself. Here we're assuming that
23652 enumerators can't have dependent attributes. */
23653 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23654 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23657 if (SCOPED_ENUM_P (newtag
))
23660 finish_enum_value_list (newtag
);
23661 finish_enum (newtag
);
23663 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23664 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23667 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23668 its type -- but without substituting the innermost set of template
23669 arguments. So, innermost set of template parameters will appear in
23673 get_mostly_instantiated_function_type (tree decl
)
23675 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23676 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23679 /* Return truthvalue if we're processing a template different from
23680 the last one involved in diagnostics. */
23682 problematic_instantiation_changed (void)
23684 return current_tinst_level
!= last_error_tinst_level
;
23687 /* Remember current template involved in diagnostics. */
23689 record_last_problematic_instantiation (void)
23691 last_error_tinst_level
= current_tinst_level
;
23694 struct tinst_level
*
23695 current_instantiation (void)
23697 return current_tinst_level
;
23700 /* Return TRUE if current_function_decl is being instantiated, false
23704 instantiating_current_function_p (void)
23706 return (current_instantiation ()
23707 && current_instantiation ()->decl
== current_function_decl
);
23710 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23711 type. Return false for ok, true for disallowed. Issue error and
23712 inform messages under control of COMPLAIN. */
23715 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23717 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23719 else if (POINTER_TYPE_P (type
))
23721 else if (TYPE_PTRMEM_P (type
))
23723 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23725 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23727 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23729 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23731 /* A bound template template parm could later be instantiated to have a valid
23732 nontype parm type via an alias template. */
23733 else if (cxx_dialect
>= cxx11
23734 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23737 if (complain
& tf_error
)
23739 if (type
== error_mark_node
)
23740 inform (input_location
, "invalid template non-type parameter");
23742 error ("%q#T is not a valid type for a template non-type parameter",
23748 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23749 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23752 dependent_type_p_r (tree type
)
23758 A type is dependent if it is:
23760 -- a template parameter. Template template parameters are types
23761 for us (since TYPE_P holds true for them) so we handle
23763 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23764 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23766 /* -- a qualified-id with a nested-name-specifier which contains a
23767 class-name that names a dependent type or whose unqualified-id
23768 names a dependent type. */
23769 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23772 /* An alias template specialization can be dependent even if the
23773 resulting type is not. */
23774 if (dependent_alias_template_spec_p (type
))
23777 /* -- a cv-qualified type where the cv-unqualified type is
23779 No code is necessary for this bullet; the code below handles
23780 cv-qualified types, and we don't want to strip aliases with
23781 TYPE_MAIN_VARIANT because of DR 1558. */
23782 /* -- a compound type constructed from any dependent type. */
23783 if (TYPE_PTRMEM_P (type
))
23784 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23785 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23787 else if (TYPE_PTR_P (type
)
23788 || TREE_CODE (type
) == REFERENCE_TYPE
)
23789 return dependent_type_p (TREE_TYPE (type
));
23790 else if (TREE_CODE (type
) == FUNCTION_TYPE
23791 || TREE_CODE (type
) == METHOD_TYPE
)
23795 if (dependent_type_p (TREE_TYPE (type
)))
23797 for (arg_type
= TYPE_ARG_TYPES (type
);
23799 arg_type
= TREE_CHAIN (arg_type
))
23800 if (dependent_type_p (TREE_VALUE (arg_type
)))
23802 if (cxx_dialect
>= cxx17
)
23803 /* A value-dependent noexcept-specifier makes the type dependent. */
23804 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
23805 if (tree noex
= TREE_PURPOSE (spec
))
23806 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23807 affect overload resolution and treating it as dependent breaks
23809 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
23810 && value_dependent_expression_p (noex
))
23814 /* -- an array type constructed from any dependent type or whose
23815 size is specified by a constant expression that is
23818 We checked for type- and value-dependence of the bounds in
23819 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23820 if (TREE_CODE (type
) == ARRAY_TYPE
)
23822 if (TYPE_DOMAIN (type
)
23823 && dependent_type_p (TYPE_DOMAIN (type
)))
23825 return dependent_type_p (TREE_TYPE (type
));
23828 /* -- a template-id in which either the template name is a template
23830 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23832 /* ... or any of the template arguments is a dependent type or
23833 an expression that is type-dependent or value-dependent. */
23834 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23835 && (any_dependent_template_arguments_p
23836 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23839 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23840 dependent; if the argument of the `typeof' expression is not
23841 type-dependent, then it should already been have resolved. */
23842 if (TREE_CODE (type
) == TYPEOF_TYPE
23843 || TREE_CODE (type
) == DECLTYPE_TYPE
23844 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23847 /* A template argument pack is dependent if any of its packed
23849 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23851 tree args
= ARGUMENT_PACK_ARGS (type
);
23852 int i
, len
= TREE_VEC_LENGTH (args
);
23853 for (i
= 0; i
< len
; ++i
)
23854 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23858 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23859 be template parameters. */
23860 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23863 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23866 /* The standard does not specifically mention types that are local
23867 to template functions or local classes, but they should be
23868 considered dependent too. For example:
23870 template <int I> void f() {
23875 The size of `E' cannot be known until the value of `I' has been
23876 determined. Therefore, `E' must be considered dependent. */
23877 scope
= TYPE_CONTEXT (type
);
23878 if (scope
&& TYPE_P (scope
))
23879 return dependent_type_p (scope
);
23880 /* Don't use type_dependent_expression_p here, as it can lead
23881 to infinite recursion trying to determine whether a lambda
23882 nested in a lambda is dependent (c++/47687). */
23883 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23884 && DECL_LANG_SPECIFIC (scope
)
23885 && DECL_TEMPLATE_INFO (scope
)
23886 && (any_dependent_template_arguments_p
23887 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23890 /* Other types are non-dependent. */
23894 /* Returns TRUE if TYPE is dependent, in the sense of
23895 [temp.dep.type]. Note that a NULL type is considered dependent. */
23898 dependent_type_p (tree type
)
23900 /* If there are no template parameters in scope, then there can't be
23901 any dependent types. */
23902 if (!processing_template_decl
)
23904 /* If we are not processing a template, then nobody should be
23905 providing us with a dependent type. */
23907 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23911 /* If the type is NULL, we have not computed a type for the entity
23912 in question; in that case, the type is dependent. */
23916 /* Erroneous types can be considered non-dependent. */
23917 if (type
== error_mark_node
)
23920 /* Getting here with global_type_node means we improperly called this
23921 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23922 gcc_checking_assert (type
!= global_type_node
);
23924 /* If we have not already computed the appropriate value for TYPE,
23926 if (!TYPE_DEPENDENT_P_VALID (type
))
23928 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23929 TYPE_DEPENDENT_P_VALID (type
) = 1;
23932 return TYPE_DEPENDENT_P (type
);
23935 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23936 lookup. In other words, a dependent type that is not the current
23940 dependent_scope_p (tree scope
)
23942 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23943 && !currently_open_class (scope
));
23946 /* T is a SCOPE_REF; return whether we need to consider it
23947 instantiation-dependent so that we can check access at instantiation
23948 time even though we know which member it resolves to. */
23951 instantiation_dependent_scope_ref_p (tree t
)
23953 if (DECL_P (TREE_OPERAND (t
, 1))
23954 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23955 /* A dependent base could make a member inaccessible in the current
23957 && !any_dependent_bases_p ()
23958 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23959 TREE_OPERAND (t
, 1)))
23965 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23966 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23969 /* Note that this predicate is not appropriate for general expressions;
23970 only constant expressions (that satisfy potential_constant_expression)
23971 can be tested for value dependence. */
23974 value_dependent_expression_p (tree expression
)
23976 if (!processing_template_decl
|| expression
== NULL_TREE
)
23979 /* A type-dependent expression is also value-dependent. */
23980 if (type_dependent_expression_p (expression
))
23983 switch (TREE_CODE (expression
))
23986 /* A dependent member function of the current instantiation. */
23987 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
23989 case FUNCTION_DECL
:
23990 /* A dependent member function of the current instantiation. */
23991 if (DECL_CLASS_SCOPE_P (expression
)
23992 && dependent_type_p (DECL_CONTEXT (expression
)))
23996 case IDENTIFIER_NODE
:
23997 /* A name that has not been looked up -- must be dependent. */
24000 case TEMPLATE_PARM_INDEX
:
24001 /* A non-type template parm. */
24005 /* A non-type template parm. */
24006 if (DECL_TEMPLATE_PARM_P (expression
))
24008 return value_dependent_expression_p (DECL_INITIAL (expression
));
24011 /* A constant with literal type and is initialized
24012 with an expression that is value-dependent. */
24013 if (DECL_DEPENDENT_INIT_P (expression
)
24014 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24015 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24017 if (DECL_HAS_VALUE_EXPR_P (expression
))
24019 tree value_expr
= DECL_VALUE_EXPR (expression
);
24020 if (value_dependent_expression_p (value_expr
))
24025 case DYNAMIC_CAST_EXPR
:
24026 case STATIC_CAST_EXPR
:
24027 case CONST_CAST_EXPR
:
24028 case REINTERPRET_CAST_EXPR
:
24030 case IMPLICIT_CONV_EXPR
:
24031 /* These expressions are value-dependent if the type to which
24032 the cast occurs is dependent or the expression being casted
24033 is value-dependent. */
24035 tree type
= TREE_TYPE (expression
);
24037 if (dependent_type_p (type
))
24040 /* A functional cast has a list of operands. */
24041 expression
= TREE_OPERAND (expression
, 0);
24044 /* If there are no operands, it must be an expression such
24045 as "int()". This should not happen for aggregate types
24046 because it would form non-constant expressions. */
24047 gcc_assert (cxx_dialect
>= cxx11
24048 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24053 if (TREE_CODE (expression
) == TREE_LIST
)
24054 return any_value_dependent_elements_p (expression
);
24056 return value_dependent_expression_p (expression
);
24060 if (SIZEOF_EXPR_TYPE_P (expression
))
24061 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24065 /* A `sizeof' expression is value-dependent if the operand is
24066 type-dependent or is a pack expansion. */
24067 expression
= TREE_OPERAND (expression
, 0);
24068 if (PACK_EXPANSION_P (expression
))
24070 else if (TYPE_P (expression
))
24071 return dependent_type_p (expression
);
24072 return instantiation_dependent_uneval_expression_p (expression
);
24074 case AT_ENCODE_EXPR
:
24075 /* An 'encode' expression is value-dependent if the operand is
24077 expression
= TREE_OPERAND (expression
, 0);
24078 return dependent_type_p (expression
);
24080 case NOEXCEPT_EXPR
:
24081 expression
= TREE_OPERAND (expression
, 0);
24082 return instantiation_dependent_uneval_expression_p (expression
);
24085 /* All instantiation-dependent expressions should also be considered
24086 value-dependent. */
24087 return instantiation_dependent_scope_ref_p (expression
);
24089 case COMPONENT_REF
:
24090 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24091 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24093 case NONTYPE_ARGUMENT_PACK
:
24094 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24095 is value-dependent. */
24097 tree values
= ARGUMENT_PACK_ARGS (expression
);
24098 int i
, len
= TREE_VEC_LENGTH (values
);
24100 for (i
= 0; i
< len
; ++i
)
24101 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24109 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24111 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24117 if (TREE_CODE (type2
) != TREE_LIST
)
24118 return dependent_type_p (type2
);
24120 for (; type2
; type2
= TREE_CHAIN (type2
))
24121 if (dependent_type_p (TREE_VALUE (type2
)))
24128 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24129 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24132 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24133 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24137 tree op
= TREE_OPERAND (expression
, 0);
24138 return (value_dependent_expression_p (op
)
24139 || has_value_dependent_address (op
));
24142 case REQUIRES_EXPR
:
24143 /* Treat all requires-expressions as value-dependent so
24144 we don't try to fold them. */
24148 return dependent_type_p (TREE_OPERAND (expression
, 0));
24152 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24154 tree fn
= get_callee_fndecl (expression
);
24156 nargs
= call_expr_nargs (expression
);
24157 for (i
= 0; i
< nargs
; ++i
)
24159 tree op
= CALL_EXPR_ARG (expression
, i
);
24160 /* In a call to a constexpr member function, look through the
24161 implicit ADDR_EXPR on the object argument so that it doesn't
24162 cause the call to be considered value-dependent. We also
24163 look through it in potential_constant_expression. */
24164 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24165 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24166 && TREE_CODE (op
) == ADDR_EXPR
)
24167 op
= TREE_OPERAND (op
, 0);
24168 if (value_dependent_expression_p (op
))
24174 case TEMPLATE_ID_EXPR
:
24175 return variable_concept_p (TREE_OPERAND (expression
, 0));
24181 if (dependent_type_p (TREE_TYPE (expression
)))
24183 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24184 if (value_dependent_expression_p (val
))
24190 /* Treat a GNU statement expression as dependent to avoid crashing
24191 under instantiate_non_dependent_expr; it can't be constant. */
24195 /* A constant expression is value-dependent if any subexpression is
24196 value-dependent. */
24197 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24199 case tcc_reference
:
24201 case tcc_comparison
:
24203 case tcc_expression
:
24206 int i
, len
= cp_tree_operand_length (expression
);
24208 for (i
= 0; i
< len
; i
++)
24210 tree t
= TREE_OPERAND (expression
, i
);
24212 /* In some cases, some of the operands may be missing.
24213 (For example, in the case of PREDECREMENT_EXPR, the
24214 amount to increment by may be missing.) That doesn't
24215 make the expression dependent. */
24216 if (t
&& value_dependent_expression_p (t
))
24227 /* The expression is not value-dependent. */
24231 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24232 [temp.dep.expr]. Note that an expression with no type is
24233 considered dependent. Other parts of the compiler arrange for an
24234 expression with type-dependent subexpressions to have no type, so
24235 this function doesn't have to be fully recursive. */
24238 type_dependent_expression_p (tree expression
)
24240 if (!processing_template_decl
)
24243 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24246 STRIP_ANY_LOCATION_WRAPPER (expression
);
24248 /* An unresolved name is always dependent. */
24249 if (identifier_p (expression
)
24250 || TREE_CODE (expression
) == USING_DECL
24251 || TREE_CODE (expression
) == WILDCARD_DECL
)
24254 /* A fold expression is type-dependent. */
24255 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24256 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24257 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24258 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24261 /* Some expression forms are never type-dependent. */
24262 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24263 || TREE_CODE (expression
) == SIZEOF_EXPR
24264 || TREE_CODE (expression
) == ALIGNOF_EXPR
24265 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24266 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24267 || TREE_CODE (expression
) == TRAIT_EXPR
24268 || TREE_CODE (expression
) == TYPEID_EXPR
24269 || TREE_CODE (expression
) == DELETE_EXPR
24270 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24271 || TREE_CODE (expression
) == THROW_EXPR
24272 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24275 /* The types of these expressions depends only on the type to which
24276 the cast occurs. */
24277 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24278 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24279 || TREE_CODE (expression
) == CONST_CAST_EXPR
24280 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24281 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24282 || TREE_CODE (expression
) == CAST_EXPR
)
24283 return dependent_type_p (TREE_TYPE (expression
));
24285 /* The types of these expressions depends only on the type created
24286 by the expression. */
24287 if (TREE_CODE (expression
) == NEW_EXPR
24288 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24290 /* For NEW_EXPR tree nodes created inside a template, either
24291 the object type itself or a TREE_LIST may appear as the
24293 tree type
= TREE_OPERAND (expression
, 1);
24294 if (TREE_CODE (type
) == TREE_LIST
)
24295 /* This is an array type. We need to check array dimensions
24297 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24298 || value_dependent_expression_p
24299 (TREE_OPERAND (TREE_VALUE (type
), 1));
24301 return dependent_type_p (type
);
24304 if (TREE_CODE (expression
) == SCOPE_REF
)
24306 tree scope
= TREE_OPERAND (expression
, 0);
24307 tree name
= TREE_OPERAND (expression
, 1);
24309 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24310 contains an identifier associated by name lookup with one or more
24311 declarations declared with a dependent type, or...a
24312 nested-name-specifier or qualified-id that names a member of an
24313 unknown specialization. */
24314 return (type_dependent_expression_p (name
)
24315 || dependent_scope_p (scope
));
24318 if (TREE_CODE (expression
) == TEMPLATE_DECL
24319 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24320 return uses_outer_template_parms (expression
);
24322 if (TREE_CODE (expression
) == STMT_EXPR
)
24323 expression
= stmt_expr_value_expr (expression
);
24325 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24330 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24332 if (type_dependent_expression_p (elt
))
24338 /* A static data member of the current instantiation with incomplete
24339 array type is type-dependent, as the definition and specializations
24340 can have different bounds. */
24341 if (VAR_P (expression
)
24342 && DECL_CLASS_SCOPE_P (expression
)
24343 && dependent_type_p (DECL_CONTEXT (expression
))
24344 && VAR_HAD_UNKNOWN_BOUND (expression
))
24347 /* An array of unknown bound depending on a variadic parameter, eg:
24349 template<typename... Args>
24350 void foo (Args... args)
24352 int arr[] = { args... };
24355 template<int... vals>
24358 int arr[] = { vals... };
24361 If the array has no length and has an initializer, it must be that
24362 we couldn't determine its length in cp_complete_array_type because
24363 it is dependent. */
24364 if (VAR_P (expression
)
24365 && TREE_TYPE (expression
) != NULL_TREE
24366 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24367 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24368 && DECL_INITIAL (expression
))
24371 /* A function or variable template-id is type-dependent if it has any
24372 dependent template arguments. */
24373 if (VAR_OR_FUNCTION_DECL_P (expression
)
24374 && DECL_LANG_SPECIFIC (expression
)
24375 && DECL_TEMPLATE_INFO (expression
))
24377 /* Consider the innermost template arguments, since those are the ones
24378 that come from the template-id; the template arguments for the
24379 enclosing class do not make it type-dependent unless they are used in
24380 the type of the decl. */
24381 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24382 && (any_dependent_template_arguments_p
24383 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24387 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24388 type-dependent. Checking this is important for functions with auto return
24389 type, which looks like a dependent type. */
24390 if (TREE_CODE (expression
) == FUNCTION_DECL
24391 && !(DECL_CLASS_SCOPE_P (expression
)
24392 && dependent_type_p (DECL_CONTEXT (expression
)))
24393 && !(DECL_FRIEND_P (expression
)
24394 && (!DECL_FRIEND_CONTEXT (expression
)
24395 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24396 && !DECL_LOCAL_FUNCTION_P (expression
))
24398 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24399 || undeduced_auto_decl (expression
));
24403 /* Always dependent, on the number of arguments if nothing else. */
24404 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24407 if (TREE_TYPE (expression
) == unknown_type_node
)
24409 if (TREE_CODE (expression
) == ADDR_EXPR
)
24410 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24411 if (TREE_CODE (expression
) == COMPONENT_REF
24412 || TREE_CODE (expression
) == OFFSET_REF
)
24414 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24416 expression
= TREE_OPERAND (expression
, 1);
24417 if (identifier_p (expression
))
24420 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24421 if (TREE_CODE (expression
) == SCOPE_REF
)
24424 if (BASELINK_P (expression
))
24426 if (BASELINK_OPTYPE (expression
)
24427 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24429 expression
= BASELINK_FUNCTIONS (expression
);
24432 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24434 if (any_dependent_template_arguments_p
24435 (TREE_OPERAND (expression
, 1)))
24437 expression
= TREE_OPERAND (expression
, 0);
24438 if (identifier_p (expression
))
24442 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24443 || TREE_CODE (expression
) == FUNCTION_DECL
);
24445 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24446 if (type_dependent_expression_p (*iter
))
24452 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24454 /* Dependent type attributes might not have made it from the decl to
24456 if (DECL_P (expression
)
24457 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24460 return (dependent_type_p (TREE_TYPE (expression
)));
24463 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24464 type-dependent if the expression refers to a member of the current
24465 instantiation and the type of the referenced member is dependent, or the
24466 class member access expression refers to a member of an unknown
24469 This function returns true if the OBJECT in such a class member access
24470 expression is of an unknown specialization. */
24473 type_dependent_object_expression_p (tree object
)
24475 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24477 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24479 tree scope
= TREE_TYPE (object
);
24480 return (!scope
|| dependent_scope_p (scope
));
24483 /* walk_tree callback function for instantiation_dependent_expression_p,
24484 below. Returns non-zero if a dependent subexpression is found. */
24487 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24492 /* We don't have to worry about decltype currently because decltype
24493 of an instantiation-dependent expr is a dependent type. This
24494 might change depending on the resolution of DR 1172. */
24495 *walk_subtrees
= false;
24498 enum tree_code code
= TREE_CODE (*tp
);
24501 /* Don't treat an argument list as dependent just because it has no
24507 case TEMPLATE_PARM_INDEX
:
24510 /* Handle expressions with type operands. */
24514 case AT_ENCODE_EXPR
:
24516 tree op
= TREE_OPERAND (*tp
, 0);
24517 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24518 op
= TREE_TYPE (op
);
24521 if (dependent_type_p (op
))
24525 *walk_subtrees
= false;
24532 case COMPONENT_REF
:
24533 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24534 /* In a template, finish_class_member_access_expr creates a
24535 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24536 type-dependent, so that we can check access control at
24537 instantiation time (PR 42277). See also Core issue 1273. */
24542 if (instantiation_dependent_scope_ref_p (*tp
))
24547 /* Treat statement-expressions as dependent. */
24551 /* Treat requires-expressions as dependent. */
24552 case REQUIRES_EXPR
:
24556 /* Treat calls to function concepts as dependent. */
24557 if (function_concept_check_p (*tp
))
24561 case TEMPLATE_ID_EXPR
:
24562 /* And variable concepts. */
24563 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24571 if (type_dependent_expression_p (*tp
))
24577 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24578 sense defined by the ABI:
24580 "An expression is instantiation-dependent if it is type-dependent
24581 or value-dependent, or it has a subexpression that is type-dependent
24582 or value-dependent."
24584 Except don't actually check value-dependence for unevaluated expressions,
24585 because in sizeof(i) we don't care about the value of i. Checking
24586 type-dependence will in turn check value-dependence of array bounds/template
24587 arguments as needed. */
24590 instantiation_dependent_uneval_expression_p (tree expression
)
24594 if (!processing_template_decl
)
24597 if (expression
== error_mark_node
)
24600 result
= cp_walk_tree_without_duplicates (&expression
,
24601 instantiation_dependent_r
, NULL
);
24602 return result
!= NULL_TREE
;
24605 /* As above, but also check value-dependence of the expression as a whole. */
24608 instantiation_dependent_expression_p (tree expression
)
24610 return (instantiation_dependent_uneval_expression_p (expression
)
24611 || value_dependent_expression_p (expression
));
24614 /* Like type_dependent_expression_p, but it also works while not processing
24615 a template definition, i.e. during substitution or mangling. */
24618 type_dependent_expression_p_push (tree expr
)
24621 ++processing_template_decl
;
24622 b
= type_dependent_expression_p (expr
);
24623 --processing_template_decl
;
24627 /* Returns TRUE if ARGS contains a type-dependent expression. */
24630 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24635 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24637 if (type_dependent_expression_p (arg
))
24643 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24644 expressions) contains any type-dependent expressions. */
24647 any_type_dependent_elements_p (const_tree list
)
24649 for (; list
; list
= TREE_CHAIN (list
))
24650 if (type_dependent_expression_p (TREE_VALUE (list
)))
24656 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24657 expressions) contains any value-dependent expressions. */
24660 any_value_dependent_elements_p (const_tree list
)
24662 for (; list
; list
= TREE_CHAIN (list
))
24663 if (value_dependent_expression_p (TREE_VALUE (list
)))
24669 /* Returns TRUE if the ARG (a template argument) is dependent. */
24672 dependent_template_arg_p (tree arg
)
24674 if (!processing_template_decl
)
24677 /* Assume a template argument that was wrongly written by the user
24678 is dependent. This is consistent with what
24679 any_dependent_template_arguments_p [that calls this function]
24681 if (!arg
|| arg
== error_mark_node
)
24684 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24685 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24687 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24689 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24691 if (DECL_TEMPLATE_PARM_P (arg
))
24693 /* A member template of a dependent class is not necessarily
24694 type-dependent, but it is a dependent template argument because it
24695 will be a member of an unknown specialization to that template. */
24696 tree scope
= CP_DECL_CONTEXT (arg
);
24697 return TYPE_P (scope
) && dependent_type_p (scope
);
24699 else if (ARGUMENT_PACK_P (arg
))
24701 tree args
= ARGUMENT_PACK_ARGS (arg
);
24702 int i
, len
= TREE_VEC_LENGTH (args
);
24703 for (i
= 0; i
< len
; ++i
)
24705 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24711 else if (TYPE_P (arg
))
24712 return dependent_type_p (arg
);
24714 return (type_dependent_expression_p (arg
)
24715 || value_dependent_expression_p (arg
));
24718 /* Returns true if ARGS (a collection of template arguments) contains
24719 any types that require structural equality testing. */
24722 any_template_arguments_need_structural_equality_p (tree args
)
24729 if (args
== error_mark_node
)
24732 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24734 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24735 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24737 tree arg
= TREE_VEC_ELT (level
, j
);
24738 tree packed_args
= NULL_TREE
;
24741 if (ARGUMENT_PACK_P (arg
))
24743 /* Look inside the argument pack. */
24744 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24745 len
= TREE_VEC_LENGTH (packed_args
);
24748 for (k
= 0; k
< len
; ++k
)
24751 arg
= TREE_VEC_ELT (packed_args
, k
);
24753 if (error_operand_p (arg
))
24755 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24757 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24759 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24760 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24769 /* Returns true if ARGS (a collection of template arguments) contains
24770 any dependent arguments. */
24773 any_dependent_template_arguments_p (const_tree args
)
24780 if (args
== error_mark_node
)
24783 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24785 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24786 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24787 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24794 /* Returns TRUE if the template TMPL is type-dependent. */
24797 dependent_template_p (tree tmpl
)
24799 if (TREE_CODE (tmpl
) == OVERLOAD
)
24801 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24802 if (dependent_template_p (*iter
))
24807 /* Template template parameters are dependent. */
24808 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24809 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24811 /* So are names that have not been looked up. */
24812 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24817 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24820 dependent_template_id_p (tree tmpl
, tree args
)
24822 return (dependent_template_p (tmpl
)
24823 || any_dependent_template_arguments_p (args
));
24826 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24830 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24834 if (!processing_template_decl
)
24837 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24839 tree decl
= TREE_VEC_ELT (declv
, i
);
24840 tree init
= TREE_VEC_ELT (initv
, i
);
24841 tree cond
= TREE_VEC_ELT (condv
, i
);
24842 tree incr
= TREE_VEC_ELT (incrv
, i
);
24844 if (type_dependent_expression_p (decl
)
24845 || TREE_CODE (decl
) == SCOPE_REF
)
24848 if (init
&& type_dependent_expression_p (init
))
24851 if (type_dependent_expression_p (cond
))
24854 if (COMPARISON_CLASS_P (cond
)
24855 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24856 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24859 if (TREE_CODE (incr
) == MODOP_EXPR
)
24861 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24862 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24865 else if (type_dependent_expression_p (incr
))
24867 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24869 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24871 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24873 tree t
= TREE_OPERAND (incr
, 1);
24874 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24875 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24884 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24885 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24886 no such TYPE can be found. Note that this function peers inside
24887 uninstantiated templates and therefore should be used only in
24888 extremely limited situations. ONLY_CURRENT_P restricts this
24889 peering to the currently open classes hierarchy (which is required
24890 when comparing types). */
24893 resolve_typename_type (tree type
, bool only_current_p
)
24902 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24904 scope
= TYPE_CONTEXT (type
);
24905 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24906 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24907 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24908 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24909 identifier of the TYPENAME_TYPE anymore.
24910 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24911 TYPENAME_TYPE instead, we avoid messing up with a possible
24912 typedef variant case. */
24913 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24915 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24916 it first before we can figure out what NAME refers to. */
24917 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24919 if (TYPENAME_IS_RESOLVING_P (scope
))
24920 /* Given a class template A with a dependent base with nested type C,
24921 typedef typename A::C::C C will land us here, as trying to resolve
24922 the initial A::C leads to the local C typedef, which leads back to
24923 A::C::C. So we break the recursion now. */
24926 scope
= resolve_typename_type (scope
, only_current_p
);
24928 /* If we don't know what SCOPE refers to, then we cannot resolve the
24930 if (!CLASS_TYPE_P (scope
))
24932 /* If this is a typedef, we don't want to look inside (c++/11987). */
24933 if (typedef_variant_p (type
))
24935 /* If SCOPE isn't the template itself, it will not have a valid
24936 TYPE_FIELDS list. */
24937 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24938 /* scope is either the template itself or a compatible instantiation
24939 like X<T>, so look up the name in the original template. */
24940 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24941 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24942 gcc_checking_assert (uses_template_parms (scope
));
24943 /* If scope has no fields, it can't be a current instantiation. Check this
24944 before currently_open_class to avoid infinite recursion (71515). */
24945 if (!TYPE_FIELDS (scope
))
24947 /* If the SCOPE is not the current instantiation, there's no reason
24948 to look inside it. */
24949 if (only_current_p
&& !currently_open_class (scope
))
24951 /* Enter the SCOPE so that name lookup will be resolved as if we
24952 were in the class definition. In particular, SCOPE will no
24953 longer be considered a dependent type. */
24954 pushed_scope
= push_scope (scope
);
24955 /* Look up the declaration. */
24956 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24957 tf_warning_or_error
);
24959 result
= NULL_TREE
;
24961 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24962 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24963 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
24966 else if (identifier_p (fullname
)
24967 && TREE_CODE (decl
) == TYPE_DECL
)
24969 result
= TREE_TYPE (decl
);
24970 if (result
== error_mark_node
)
24971 result
= NULL_TREE
;
24973 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
24974 && DECL_CLASS_TEMPLATE_P (decl
))
24976 /* Obtain the template and the arguments. */
24977 tree tmpl
= TREE_OPERAND (fullname
, 0);
24978 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
24980 /* We get here with a plain identifier because a previous tentative
24981 parse of the nested-name-specifier as part of a ptr-operator saw
24982 ::template X<A>. The use of ::template is necessary in a
24983 ptr-operator, but wrong in a declarator-id.
24985 [temp.names]: In a qualified-id of a declarator-id, the keyword
24986 template shall not appear at the top level. */
24987 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
24988 "keyword %<template%> not allowed in declarator-id");
24991 tree args
= TREE_OPERAND (fullname
, 1);
24992 /* Instantiate the template. */
24993 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
24994 /*entering_scope=*/true,
24995 tf_error
| tf_user
);
24996 if (result
== error_mark_node
)
24997 result
= NULL_TREE
;
25000 /* Leave the SCOPE. */
25002 pop_scope (pushed_scope
);
25004 /* If we failed to resolve it, return the original typename. */
25008 /* If lookup found a typename type, resolve that too. */
25009 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25011 /* Ill-formed programs can cause infinite recursion here, so we
25012 must catch that. */
25013 TYPENAME_IS_RESOLVING_P (result
) = 1;
25014 result
= resolve_typename_type (result
, only_current_p
);
25015 TYPENAME_IS_RESOLVING_P (result
) = 0;
25018 /* Qualify the resulting type. */
25019 quals
= cp_type_quals (type
);
25021 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25026 /* EXPR is an expression which is not type-dependent. Return a proxy
25027 for EXPR that can be used to compute the types of larger
25028 expressions containing EXPR. */
25031 build_non_dependent_expr (tree expr
)
25033 tree orig_expr
= expr
;
25036 /* When checking, try to get a constant value for all non-dependent
25037 expressions in order to expose bugs in *_dependent_expression_p
25038 and constexpr. This can affect code generation, see PR70704, so
25039 only do this for -fchecking=2. */
25040 if (flag_checking
> 1
25041 && cxx_dialect
>= cxx11
25042 /* Don't do this during nsdmi parsing as it can lead to
25043 unexpected recursive instantiations. */
25044 && !parsing_nsdmi ()
25045 /* Don't do this during concept expansion either and for
25046 the same reason. */
25047 && !expanding_concept ())
25048 fold_non_dependent_expr (expr
);
25050 STRIP_ANY_LOCATION_WRAPPER (expr
);
25052 /* Preserve OVERLOADs; the functions must be available to resolve
25055 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25056 inner_expr
= stmt_expr_value_expr (inner_expr
);
25057 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25058 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25059 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25060 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25061 if (is_overloaded_fn (inner_expr
)
25062 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25064 /* There is no need to return a proxy for a variable. */
25067 /* Preserve string constants; conversions from string constants to
25068 "char *" are allowed, even though normally a "const char *"
25069 cannot be used to initialize a "char *". */
25070 if (TREE_CODE (expr
) == STRING_CST
)
25072 /* Preserve void and arithmetic constants, as an optimization -- there is no
25073 reason to create a new node. */
25074 if (TREE_CODE (expr
) == VOID_CST
25075 || TREE_CODE (expr
) == INTEGER_CST
25076 || TREE_CODE (expr
) == REAL_CST
)
25078 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25079 There is at least one place where we want to know that a
25080 particular expression is a throw-expression: when checking a ?:
25081 expression, there are special rules if the second or third
25082 argument is a throw-expression. */
25083 if (TREE_CODE (expr
) == THROW_EXPR
)
25086 /* Don't wrap an initializer list, we need to be able to look inside. */
25087 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25090 /* Don't wrap a dummy object, we need to be able to test for it. */
25091 if (is_dummy_object (expr
))
25094 if (TREE_CODE (expr
) == COND_EXPR
)
25095 return build3 (COND_EXPR
,
25097 TREE_OPERAND (expr
, 0),
25098 (TREE_OPERAND (expr
, 1)
25099 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25100 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25101 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25102 if (TREE_CODE (expr
) == COMPOUND_EXPR
25103 && !COMPOUND_EXPR_OVERLOADED (expr
))
25104 return build2 (COMPOUND_EXPR
,
25106 TREE_OPERAND (expr
, 0),
25107 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25109 /* If the type is unknown, it can't really be non-dependent */
25110 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25112 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25113 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25116 /* ARGS is a vector of expressions as arguments to a function call.
25117 Replace the arguments with equivalent non-dependent expressions.
25118 This modifies ARGS in place. */
25121 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25126 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25128 tree newarg
= build_non_dependent_expr (arg
);
25130 (*args
)[ix
] = newarg
;
25134 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25135 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25136 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25139 make_auto_1 (tree name
, bool set_canonical
)
25141 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25142 TYPE_NAME (au
) = build_decl (input_location
,
25143 TYPE_DECL
, name
, au
);
25144 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25145 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25146 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25147 TYPE_NAME (au
), NULL_TREE
);
25149 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25150 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25151 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25157 make_decltype_auto (void)
25159 return make_auto_1 (decltype_auto_identifier
, true);
25165 return make_auto_1 (auto_identifier
, true);
25168 /* Return a C++17 deduction placeholder for class template TMPL. */
25171 make_template_placeholder (tree tmpl
)
25173 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25174 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25178 /* True iff T is a C++17 class template deduction placeholder. */
25181 template_placeholder_p (tree t
)
25183 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25186 /* Make a "constrained auto" type-specifier. This is an
25187 auto type with constraints that must be associated after
25188 deduction. The constraint is formed from the given
25189 CONC and its optional sequence of arguments, which are
25190 non-null if written as partial-concept-id. */
25193 make_constrained_auto (tree con
, tree args
)
25195 tree type
= make_auto_1 (auto_identifier
, false);
25197 /* Build the constraint. */
25198 tree tmpl
= DECL_TI_TEMPLATE (con
);
25199 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25200 expr
= build_concept_check (expr
, type
, args
);
25202 tree constr
= normalize_expression (expr
);
25203 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25205 /* Our canonical type depends on the constraint. */
25206 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25208 /* Attach the constraint to the type declaration. */
25209 tree decl
= TYPE_NAME (type
);
25213 /* Given type ARG, return std::initializer_list<ARG>. */
25218 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25220 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25222 gcc_rich_location
richloc (input_location
);
25223 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25224 error_at (&richloc
,
25225 "deducing from brace-enclosed initializer list"
25226 " requires %<#include <initializer_list>%>");
25228 return error_mark_node
;
25230 tree argvec
= make_tree_vec (1);
25231 TREE_VEC_ELT (argvec
, 0) = arg
;
25233 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25234 NULL_TREE
, 0, tf_warning_or_error
);
25237 /* Replace auto in TYPE with std::initializer_list<auto>. */
25240 listify_autos (tree type
, tree auto_node
)
25242 tree init_auto
= listify (auto_node
);
25243 tree argvec
= make_tree_vec (1);
25244 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25245 if (processing_template_decl
)
25246 argvec
= add_to_template_args (current_template_args (), argvec
);
25247 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25250 /* Hash traits for hashing possibly constrained 'auto'
25251 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25253 struct auto_hash
: default_hash_traits
<tree
>
25255 static inline hashval_t
hash (tree
);
25256 static inline bool equal (tree
, tree
);
25259 /* Hash the 'auto' T. */
25262 auto_hash::hash (tree t
)
25264 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25265 /* Matching constrained-type-specifiers denote the same template
25266 parameter, so hash the constraint. */
25267 return hash_placeholder_constraint (c
);
25269 /* But unconstrained autos are all separate, so just hash the pointer. */
25270 return iterative_hash_object (t
, 0);
25273 /* Compare two 'auto's. */
25276 auto_hash::equal (tree t1
, tree t2
)
25281 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25282 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25284 /* Two unconstrained autos are distinct. */
25288 return equivalent_placeholder_constraints (c1
, c2
);
25291 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25292 constrained) auto, add it to the vector. */
25295 extract_autos_r (tree t
, void *data
)
25297 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25300 /* All the autos were built with index 0; fix that up now. */
25301 tree
*p
= hash
.find_slot (t
, INSERT
);
25304 /* If this is a repeated constrained-type-specifier, use the index we
25306 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25309 /* Otherwise this is new, so use the current count. */
25311 idx
= hash
.elements () - 1;
25313 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25316 /* Always keep walking. */
25320 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25321 says they can appear anywhere in the type. */
25324 extract_autos (tree type
)
25326 hash_set
<tree
> visited
;
25327 hash_table
<auto_hash
> hash (2);
25329 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25331 tree tree_vec
= make_tree_vec (hash
.elements());
25332 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25333 iter
!= hash
.end(); ++iter
)
25336 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25337 TREE_VEC_ELT (tree_vec
, i
)
25338 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25344 /* The stem for deduction guide names. */
25345 const char *const dguide_base
= "__dguide_";
25347 /* Return the name for a deduction guide for class template TMPL. */
25350 dguide_name (tree tmpl
)
25352 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25353 tree tname
= TYPE_IDENTIFIER (type
);
25354 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25355 + IDENTIFIER_LENGTH (tname
));
25356 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25357 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25358 IDENTIFIER_LENGTH (tname
) + 1);
25359 tree dname
= get_identifier (buf
);
25360 TREE_TYPE (dname
) = type
;
25364 /* True if NAME is the name of a deduction guide. */
25367 dguide_name_p (tree name
)
25369 return (TREE_TYPE (name
)
25370 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25371 strlen (dguide_base
)));
25374 /* True if FN is a deduction guide. */
25377 deduction_guide_p (const_tree fn
)
25380 if (tree name
= DECL_NAME (fn
))
25381 return dguide_name_p (name
);
25385 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25388 copy_guide_p (const_tree fn
)
25390 gcc_assert (deduction_guide_p (fn
));
25391 if (!DECL_ARTIFICIAL (fn
))
25393 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25394 return (TREE_CHAIN (parms
) == void_list_node
25395 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25398 /* True if FN is a guide generated from a constructor template. */
25401 template_guide_p (const_tree fn
)
25403 gcc_assert (deduction_guide_p (fn
));
25404 if (!DECL_ARTIFICIAL (fn
))
25406 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25407 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25408 return PRIMARY_TEMPLATE_P (org
);
25412 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25413 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25414 template parameter types. Note that the handling of template template
25415 parameters relies on current_template_parms being set appropriately for the
25419 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25420 tree tsubst_args
, tsubst_flags_t complain
)
25422 tree oldidx
= get_template_parm_index (olddecl
);
25425 if (TREE_CODE (olddecl
) == TYPE_DECL
25426 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25428 tree oldtype
= TREE_TYPE (olddecl
);
25429 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25430 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25431 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25432 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25433 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25436 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25437 complain
, NULL_TREE
);
25440 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25441 DECL_NAME (olddecl
), newtype
);
25442 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25445 if (TREE_CODE (olddecl
) == TYPE_DECL
25446 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25448 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25449 = build_template_parm_index (index
, level
, level
,
25451 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25452 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25453 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25454 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25456 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25458 DECL_TEMPLATE_RESULT (newdecl
)
25459 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25460 DECL_NAME (olddecl
), newtype
);
25461 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25462 // First create a copy (ttargs) of tsubst_args with an
25463 // additional level for the template template parameter's own
25464 // template parameters (ttparms).
25465 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25466 (DECL_TEMPLATE_PARMS (olddecl
)));
25467 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25468 tree ttargs
= make_tree_vec (depth
+ 1);
25469 for (int i
= 0; i
< depth
; ++i
)
25470 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25471 TREE_VEC_ELT (ttargs
, depth
)
25472 = template_parms_level_to_args (ttparms
);
25473 // Substitute ttargs into ttparms to fix references to
25474 // other template parameters.
25475 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25477 // Now substitute again with args based on tparms, to reduce
25478 // the level of the ttparms.
25479 ttargs
= current_template_args ();
25480 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25482 // Finally, tack the adjusted parms onto tparms.
25483 ttparms
= tree_cons (size_int (depth
), ttparms
,
25484 current_template_parms
);
25485 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25490 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25492 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25493 TREE_CODE (oldconst
),
25494 DECL_NAME (oldconst
), newtype
);
25495 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25496 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25497 SET_DECL_TEMPLATE_PARM_P (newconst
);
25498 newidx
= build_template_parm_index (index
, level
, level
,
25499 newconst
, newtype
);
25500 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25501 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25502 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25508 /* Returns a C++17 class deduction guide template based on the constructor
25509 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25510 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25513 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25515 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25516 bool memtmpl
= false;
25519 tree fn_tmpl
= NULL_TREE
;
25524 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25527 type
= TREE_TYPE (type
);
25528 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25531 fparms
= void_list_node
;
25533 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25534 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25535 targs
= CLASSTYPE_TI_ARGS (type
);
25538 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25539 explicit_p
= false;
25543 ++processing_template_decl
;
25546 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25547 : DECL_TI_TEMPLATE (ctor
));
25549 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25550 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25552 type
= DECL_CONTEXT (ctor
);
25554 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25555 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25556 fully specialized args for the enclosing class. Strip those off, as
25557 the deduction guide won't have those template parameters. */
25558 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25559 TMPL_PARMS_DEPTH (tparms
));
25560 /* Discard the 'this' parameter. */
25561 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25562 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25563 ci
= get_constraints (ctor
);
25564 loc
= DECL_SOURCE_LOCATION (ctor
);
25565 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25567 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25571 /* For a member template constructor, we need to flatten the two
25572 template parameter lists into one, and then adjust the function
25573 signature accordingly. This gets...complicated. */
25574 tree save_parms
= current_template_parms
;
25576 /* For a member template we should have two levels of parms/args, one
25577 for the class and one for the constructor. We stripped
25578 specialized args for further enclosing classes above. */
25579 const int depth
= 2;
25580 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25582 /* Template args for translating references to the two-level template
25583 parameters into references to the one-level template parameters we
25585 tree tsubst_args
= copy_node (targs
);
25586 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25587 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25589 /* Template parms for the constructor template. */
25590 tree ftparms
= TREE_VALUE (tparms
);
25591 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25592 /* Template parms for the class template. */
25593 tparms
= TREE_CHAIN (tparms
);
25594 tree ctparms
= TREE_VALUE (tparms
);
25595 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25596 /* Template parms for the deduction guide start as a copy of the
25597 template parms for the class. We set current_template_parms for
25598 lookup_template_class_1. */
25599 current_template_parms
= tparms
= copy_node (tparms
);
25600 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25601 for (unsigned i
= 0; i
< clen
; ++i
)
25602 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25604 /* Now we need to rewrite the constructor parms to append them to the
25606 for (unsigned i
= 0; i
< flen
; ++i
)
25608 unsigned index
= i
+ clen
;
25609 unsigned level
= 1;
25610 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25611 tree olddecl
= TREE_VALUE (oldelt
);
25612 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25613 tsubst_args
, complain
);
25614 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25615 tsubst_args
, complain
, ctor
);
25616 tree list
= build_tree_list (newdef
, newdecl
);
25617 TEMPLATE_PARM_CONSTRAINTS (list
)
25618 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25619 tsubst_args
, complain
, ctor
);
25620 TREE_VEC_ELT (new_vec
, index
) = list
;
25621 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25624 /* Now we have a final set of template parms to substitute into the
25625 function signature. */
25626 targs
= template_parms_to_args (tparms
);
25627 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25629 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25631 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25633 current_template_parms
= save_parms
;
25635 --processing_template_decl
;
25640 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25641 tparms
= copy_node (tparms
);
25642 INNERMOST_TEMPLATE_PARMS (tparms
)
25643 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25646 tree fntype
= build_function_type (type
, fparms
);
25647 tree ded_fn
= build_lang_decl_loc (loc
,
25649 dguide_name (type
), fntype
);
25650 DECL_ARGUMENTS (ded_fn
) = fargs
;
25651 DECL_ARTIFICIAL (ded_fn
) = true;
25652 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25653 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25654 DECL_ARTIFICIAL (ded_tmpl
) = true;
25655 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25656 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25657 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25658 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25660 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25662 set_constraints (ded_tmpl
, ci
);
25667 /* Deduce template arguments for the class template placeholder PTYPE for
25668 template TMPL based on the initializer INIT, and return the resulting
25672 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25673 tsubst_flags_t complain
)
25675 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25677 /* We should have handled this in the caller. */
25678 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25680 if (complain
& tf_error
)
25681 error ("non-class template %qT used without template arguments", tmpl
);
25682 return error_mark_node
;
25685 tree type
= TREE_TYPE (tmpl
);
25687 bool try_list_ctor
= false;
25689 vec
<tree
,va_gc
> *args
;
25690 if (init
== NULL_TREE
25691 || TREE_CODE (init
) == TREE_LIST
)
25692 args
= make_tree_vector_from_list (init
);
25693 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25695 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25696 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25698 /* As an exception, the first phase in 16.3.1.7 (considering the
25699 initializer list as a single argument) is omitted if the
25700 initializer list consists of a single expression of type cv U,
25701 where U is a specialization of C or a class derived from a
25702 specialization of C. */
25703 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25704 tree etype
= TREE_TYPE (elt
);
25706 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25707 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25708 int err
= unify (tparms
, targs
, type
, etype
,
25709 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25711 try_list_ctor
= false;
25714 if (try_list_ctor
|| is_std_init_list (type
))
25715 args
= make_tree_vector_single (init
);
25717 args
= make_tree_vector_from_ctor (init
);
25720 args
= make_tree_vector_single (init
);
25722 tree dname
= dguide_name (tmpl
);
25723 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25724 /*type*/false, /*complain*/false,
25726 bool elided
= false;
25727 if (cands
== error_mark_node
)
25730 /* Prune explicit deduction guides in copy-initialization context. */
25731 if (flags
& LOOKUP_ONLYCONVERTING
)
25733 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25734 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25739 /* Found a nonconverting guide, prune the candidates. */
25740 tree pruned
= NULL_TREE
;
25741 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25742 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25743 pruned
= lookup_add (*iter
, pruned
);
25749 tree outer_args
= NULL_TREE
;
25750 if (DECL_CLASS_SCOPE_P (tmpl
)
25751 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25753 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25754 type
= TREE_TYPE (most_general_template (tmpl
));
25757 bool saw_ctor
= false;
25758 // FIXME cache artificial deduction guides
25759 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25761 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25762 if ((flags
& LOOKUP_ONLYCONVERTING
)
25763 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25766 cands
= lookup_add (guide
, cands
);
25771 tree call
= error_mark_node
;
25773 /* If this is list-initialization and the class has a list constructor, first
25774 try deducing from the list as a single argument, as [over.match.list]. */
25775 tree list_cands
= NULL_TREE
;
25776 if (try_list_ctor
&& cands
)
25777 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25780 if (is_list_ctor (dg
))
25781 list_cands
= lookup_add (dg
, list_cands
);
25785 ++cp_unevaluated_operand
;
25786 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25787 --cp_unevaluated_operand
;
25789 if (call
== error_mark_node
)
25791 /* That didn't work, now try treating the list as a sequence of
25793 release_tree_vector (args
);
25794 args
= make_tree_vector_from_ctor (init
);
25798 /* Maybe generate an implicit deduction guide. */
25799 if (call
== error_mark_node
&& args
->length () < 2)
25801 tree gtype
= NULL_TREE
;
25803 if (args
->length () == 1)
25804 /* Generate a copy guide. */
25805 gtype
= build_reference_type (type
);
25806 else if (!saw_ctor
)
25807 /* Generate a default guide. */
25812 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25813 cands
= lookup_add (guide
, cands
);
25817 if (elided
&& !cands
)
25819 error ("cannot deduce template arguments for copy-initialization"
25820 " of %qT, as it has no non-explicit deduction guides or "
25821 "user-declared constructors", type
);
25822 return error_mark_node
;
25824 else if (!cands
&& call
== error_mark_node
)
25826 error ("cannot deduce template arguments of %qT, as it has no viable "
25827 "deduction guides", type
);
25828 return error_mark_node
;
25831 if (call
== error_mark_node
)
25833 ++cp_unevaluated_operand
;
25834 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25835 --cp_unevaluated_operand
;
25838 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25840 error ("class template argument deduction failed:");
25842 ++cp_unevaluated_operand
;
25843 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25844 --cp_unevaluated_operand
;
25847 inform (input_location
, "explicit deduction guides not considered "
25848 "for copy-initialization");
25851 release_tree_vector (args
);
25853 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25856 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25857 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25860 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25862 return do_auto_deduction (type
, init
, auto_node
,
25863 tf_warning_or_error
,
25867 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25868 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25869 The CONTEXT determines the context in which auto deduction is performed
25870 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25871 OUTER_TARGS are used during template argument deduction
25872 (context == adc_unify) to properly substitute the result, and is ignored
25875 For partial-concept-ids, extra args may be appended to the list of deduced
25876 template arguments prior to determining constraint satisfaction. */
25879 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25880 tsubst_flags_t complain
, auto_deduction_context context
,
25881 tree outer_targs
, int flags
)
25885 if (init
== error_mark_node
)
25886 return error_mark_node
;
25888 if (init
&& type_dependent_expression_p (init
)
25889 && context
!= adc_unify
)
25890 /* Defining a subset of type-dependent expressions that we can deduce
25891 from ahead of time isn't worth the trouble. */
25894 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25895 /* C++17 class template argument deduction. */
25896 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25898 if (TREE_TYPE (init
) == NULL_TREE
)
25899 /* Nothing we can do with this, even in deduction context. */
25902 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25903 with either a new invented type template parameter U or, if the
25904 initializer is a braced-init-list (8.5.4), with
25905 std::initializer_list<U>. */
25906 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25908 if (!DIRECT_LIST_INIT_P (init
))
25909 type
= listify_autos (type
, auto_node
);
25910 else if (CONSTRUCTOR_NELTS (init
) == 1)
25911 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25914 if (complain
& tf_warning_or_error
)
25916 if (permerror (input_location
, "direct-list-initialization of "
25917 "%<auto%> requires exactly one element"))
25918 inform (input_location
,
25919 "for deduction to %<std::initializer_list%>, use copy-"
25920 "list-initialization (i.e. add %<=%> before the %<{%>)");
25922 type
= listify_autos (type
, auto_node
);
25926 if (type
== error_mark_node
)
25927 return error_mark_node
;
25929 init
= resolve_nondeduced_context (init
, complain
);
25931 if (context
== adc_decomp_type
25932 && auto_node
== type
25933 && init
!= error_mark_node
25934 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
25935 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25936 and initializer has array type, deduce cv-qualified array type. */
25937 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
25939 else if (AUTO_IS_DECLTYPE (auto_node
))
25941 bool id
= (DECL_P (init
)
25942 || ((TREE_CODE (init
) == COMPONENT_REF
25943 || TREE_CODE (init
) == SCOPE_REF
)
25944 && !REF_PARENTHESIZED_P (init
)));
25945 targs
= make_tree_vec (1);
25946 TREE_VEC_ELT (targs
, 0)
25947 = finish_decltype_type (init
, id
, tf_warning_or_error
);
25948 if (type
!= auto_node
)
25950 if (complain
& tf_error
)
25951 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
25952 return error_mark_node
;
25957 tree parms
= build_tree_list (NULL_TREE
, type
);
25961 tparms
= extract_autos (type
);
25964 tparms
= make_tree_vec (1);
25965 TREE_VEC_ELT (tparms
, 0)
25966 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
25969 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25970 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25971 DEDUCE_CALL
, LOOKUP_NORMAL
,
25972 NULL
, /*explain_p=*/false);
25975 if (processing_template_decl
)
25976 /* Try again at instantiation time. */
25978 if (type
&& type
!= error_mark_node
25979 && (complain
& tf_error
))
25980 /* If type is error_mark_node a diagnostic must have been
25981 emitted by now. Also, having a mention to '<type error>'
25982 in the diagnostic is not really useful to the user. */
25984 if (cfun
&& auto_node
== current_function_auto_return_pattern
25985 && LAMBDA_FUNCTION_P (current_function_decl
))
25986 error ("unable to deduce lambda return type from %qE", init
);
25988 error ("unable to deduce %qT from %qE", type
, init
);
25989 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25990 DEDUCE_CALL
, LOOKUP_NORMAL
,
25991 NULL
, /*explain_p=*/true);
25993 return error_mark_node
;
25997 /* Check any placeholder constraints against the deduced type. */
25998 if (flag_concepts
&& !processing_template_decl
)
25999 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26001 /* Use the deduced type to check the associated constraints. If we
26002 have a partial-concept-id, rebuild the argument list so that
26003 we check using the extra arguments. */
26004 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26005 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26006 if (TREE_VEC_LENGTH (cargs
) > 1)
26008 cargs
= copy_node (cargs
);
26009 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26013 if (!constraints_satisfied_p (constr
, cargs
))
26015 if (complain
& tf_warning_or_error
)
26019 case adc_unspecified
:
26021 error("placeholder constraints not satisfied");
26023 case adc_variable_type
:
26024 case adc_decomp_type
:
26025 error ("deduced initializer does not satisfy "
26026 "placeholder constraints");
26028 case adc_return_type
:
26029 error ("deduced return type does not satisfy "
26030 "placeholder constraints");
26032 case adc_requirement
:
26033 error ("deduced expression type does not satisfy "
26034 "placeholder constraints");
26037 diagnose_constraints (input_location
, constr
, targs
);
26039 return error_mark_node
;
26043 if (processing_template_decl
&& context
!= adc_unify
)
26044 outer_targs
= current_template_args ();
26045 targs
= add_to_template_args (outer_targs
, targs
);
26046 return tsubst (type
, targs
, complain
, NULL_TREE
);
26049 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26053 splice_late_return_type (tree type
, tree late_return_type
)
26055 if (is_auto (type
))
26057 if (late_return_type
)
26058 return late_return_type
;
26060 tree idx
= get_template_parm_index (type
);
26061 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26062 /* In an abbreviated function template we didn't know we were dealing
26063 with a function template when we saw the auto return type, so update
26064 it to have the correct level. */
26065 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26070 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26071 'decltype(auto)' or a deduced class template. */
26074 is_auto (const_tree type
)
26076 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26077 && (TYPE_IDENTIFIER (type
) == auto_identifier
26078 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26079 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26085 /* for_each_template_parm callback for type_uses_auto. */
26088 is_auto_r (tree tp
, void */
*data*/
)
26090 return is_auto (tp
);
26093 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26094 a use of `auto'. Returns NULL_TREE otherwise. */
26097 type_uses_auto (tree type
)
26099 if (type
== NULL_TREE
)
26101 else if (flag_concepts
)
26103 /* The Concepts TS allows multiple autos in one type-specifier; just
26104 return the first one we find, do_auto_deduction will collect all of
26106 if (uses_template_parms (type
))
26107 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26108 /*visited*/NULL
, /*nondeduced*/true);
26113 return find_type_usage (type
, is_auto
);
26116 /* For a given template T, return the vector of typedefs referenced
26117 in T for which access check is needed at T instantiation time.
26118 T is either a FUNCTION_DECL or a RECORD_TYPE.
26119 Those typedefs were added to T by the function
26120 append_type_to_template_for_access_check. */
26122 vec
<qualified_typedef_usage_t
, va_gc
> *
26123 get_types_needing_access_check (tree t
)
26126 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26128 if (!t
|| t
== error_mark_node
)
26131 if (!(ti
= get_template_info (t
)))
26134 if (CLASS_TYPE_P (t
)
26135 || TREE_CODE (t
) == FUNCTION_DECL
)
26137 if (!TI_TEMPLATE (ti
))
26140 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26146 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26147 tied to T. That list of typedefs will be access checked at
26148 T instantiation time.
26149 T is either a FUNCTION_DECL or a RECORD_TYPE.
26150 TYPE_DECL is a TYPE_DECL node representing a typedef.
26151 SCOPE is the scope through which TYPE_DECL is accessed.
26152 LOCATION is the location of the usage point of TYPE_DECL.
26154 This function is a subroutine of
26155 append_type_to_template_for_access_check. */
26158 append_type_to_template_for_access_check_1 (tree t
,
26161 location_t location
)
26163 qualified_typedef_usage_t typedef_usage
;
26166 if (!t
|| t
== error_mark_node
)
26169 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26170 || CLASS_TYPE_P (t
))
26172 && TREE_CODE (type_decl
) == TYPE_DECL
26175 if (!(ti
= get_template_info (t
)))
26178 gcc_assert (TI_TEMPLATE (ti
));
26180 typedef_usage
.typedef_decl
= type_decl
;
26181 typedef_usage
.context
= scope
;
26182 typedef_usage
.locus
= location
;
26184 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26187 /* Append TYPE_DECL to the template TEMPL.
26188 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26189 At TEMPL instanciation time, TYPE_DECL will be checked to see
26190 if it can be accessed through SCOPE.
26191 LOCATION is the location of the usage point of TYPE_DECL.
26193 e.g. consider the following code snippet:
26200 template<class U> struct S
26202 C::myint mi; // <-- usage point of the typedef C::myint
26207 At S<char> instantiation time, we need to check the access of C::myint
26208 In other words, we need to check the access of the myint typedef through
26209 the C scope. For that purpose, this function will add the myint typedef
26210 and the scope C through which its being accessed to a list of typedefs
26211 tied to the template S. That list will be walked at template instantiation
26212 time and access check performed on each typedefs it contains.
26213 Note that this particular code snippet should yield an error because
26214 myint is private to C. */
26217 append_type_to_template_for_access_check (tree templ
,
26220 location_t location
)
26222 qualified_typedef_usage_t
*iter
;
26225 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26227 /* Make sure we don't append the type to the template twice. */
26228 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26229 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26232 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26236 /* Convert the generic type parameters in PARM that match the types given in the
26237 range [START_IDX, END_IDX) from the current_template_parms into generic type
26241 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26243 tree current
= current_template_parms
;
26244 int depth
= TMPL_PARMS_DEPTH (current
);
26245 current
= INNERMOST_TEMPLATE_PARMS (current
);
26246 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26248 for (int i
= 0; i
< start_idx
; ++i
)
26249 TREE_VEC_ELT (replacement
, i
)
26250 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26252 for (int i
= start_idx
; i
< end_idx
; ++i
)
26254 /* Create a distinct parameter pack type from the current parm and add it
26255 to the replacement args to tsubst below into the generic function
26258 tree o
= TREE_TYPE (TREE_VALUE
26259 (TREE_VEC_ELT (current
, i
)));
26260 tree t
= copy_type (o
);
26261 TEMPLATE_TYPE_PARM_INDEX (t
)
26262 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26264 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26265 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26266 TYPE_MAIN_VARIANT (t
) = t
;
26267 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26268 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26269 TREE_VEC_ELT (replacement
, i
) = t
;
26270 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26273 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26274 TREE_VEC_ELT (replacement
, i
)
26275 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26277 /* If there are more levels then build up the replacement with the outer
26280 replacement
= add_to_template_args (template_parms_to_args
26281 (TREE_CHAIN (current_template_parms
)),
26284 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26287 /* Entries in the decl_constraint hash table. */
26288 struct GTY((for_user
)) constr_entry
26294 /* Hashing function and equality for constraint entries. */
26295 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26297 static hashval_t
hash (constr_entry
*e
)
26299 return (hashval_t
)DECL_UID (e
->decl
);
26302 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26304 return e1
->decl
== e2
->decl
;
26308 /* A mapping from declarations to constraint information. Note that
26309 both templates and their underlying declarations are mapped to the
26310 same constraint information.
26312 FIXME: This is defined in pt.c because garbage collection
26313 code is not being generated for constraint.cc. */
26315 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26317 /* Returns the template constraints of declaration T. If T is not
26318 constrained, return NULL_TREE. Note that T must be non-null. */
26321 get_constraints (tree t
)
26323 if (!flag_concepts
)
26326 gcc_assert (DECL_P (t
));
26327 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26328 t
= DECL_TEMPLATE_RESULT (t
);
26329 constr_entry elt
= { t
, NULL_TREE
};
26330 constr_entry
* found
= decl_constraints
->find (&elt
);
26337 /* Associate the given constraint information CI with the declaration
26338 T. If T is a template, then the constraints are associated with
26339 its underlying declaration. Don't build associations if CI is
26343 set_constraints (tree t
, tree ci
)
26347 gcc_assert (t
&& flag_concepts
);
26348 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26349 t
= DECL_TEMPLATE_RESULT (t
);
26350 gcc_assert (!get_constraints (t
));
26351 constr_entry elt
= {t
, ci
};
26352 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26353 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26358 /* Remove the associated constraints of the declaration T. */
26361 remove_constraints (tree t
)
26363 gcc_assert (DECL_P (t
));
26364 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26365 t
= DECL_TEMPLATE_RESULT (t
);
26367 constr_entry elt
= {t
, NULL_TREE
};
26368 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26370 decl_constraints
->clear_slot (slot
);
26373 /* Memoized satisfaction results for declarations. This
26374 maps the pair (constraint_info, arguments) to the result computed
26375 by constraints_satisfied_p. */
26377 struct GTY((for_user
)) constraint_sat_entry
26384 /* Hashing function and equality for constraint entries. */
26386 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26388 static hashval_t
hash (constraint_sat_entry
*e
)
26390 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26391 return iterative_hash_template_arg (e
->args
, val
);
26394 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26396 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26400 /* Memoized satisfaction results for concept checks. */
26402 struct GTY((for_user
)) concept_spec_entry
26409 /* Hashing function and equality for constraint entries. */
26411 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26413 static hashval_t
hash (concept_spec_entry
*e
)
26415 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26418 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26420 ++comparing_specializations
;
26421 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26422 --comparing_specializations
;
26427 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26428 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26430 /* Search for a memoized satisfaction result. Returns one of the
26431 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26434 lookup_constraint_satisfaction (tree ci
, tree args
)
26436 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26437 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26439 return found
->result
;
26444 /* Memoize the result of a satisfication test. Returns the saved result. */
26447 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26449 constraint_sat_entry elt
= {ci
, args
, result
};
26450 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26451 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26457 /* Search for a memoized satisfaction result for a concept. */
26460 lookup_concept_satisfaction (tree tmpl
, tree args
)
26462 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26463 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26465 return found
->result
;
26470 /* Memoize the result of a concept check. Returns the saved result. */
26473 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26475 concept_spec_entry elt
= {tmpl
, args
, result
};
26476 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26477 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26483 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26485 /* Returns a prior concept specialization. This returns the substituted
26486 and normalized constraints defined by the concept. */
26489 get_concept_expansion (tree tmpl
, tree args
)
26491 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26492 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26494 return found
->result
;
26499 /* Save a concept expansion for later. */
26502 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26504 concept_spec_entry elt
= {tmpl
, args
, def
};
26505 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26506 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26513 hash_subsumption_args (tree t1
, tree t2
)
26515 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26516 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26518 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26519 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26520 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26521 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26525 /* Compare the constraints of two subsumption entries. The LEFT1 and
26526 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26527 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26530 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26532 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26533 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26534 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26535 CHECK_CONSTR_ARGS (right1
)))
26536 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26537 CHECK_CONSTR_ARGS (right2
));
26541 /* Key/value pair for learning and memoizing subsumption results. This
26542 associates a pair of check constraints (including arguments) with
26543 a boolean value indicating the result. */
26545 struct GTY((for_user
)) subsumption_entry
26552 /* Hashing function and equality for constraint entries. */
26554 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26556 static hashval_t
hash (subsumption_entry
*e
)
26558 return hash_subsumption_args (e
->t1
, e
->t2
);
26561 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26563 ++comparing_specializations
;
26564 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26565 --comparing_specializations
;
26570 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26572 /* Search for a previously cached subsumption result. */
26575 lookup_subsumption_result (tree t1
, tree t2
)
26577 subsumption_entry elt
= { t1
, t2
, false };
26578 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26580 return &found
->result
;
26585 /* Save a subsumption result. */
26588 save_subsumption_result (tree t1
, tree t2
, bool result
)
26590 subsumption_entry elt
= {t1
, t2
, result
};
26591 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26592 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26598 /* Set up the hash table for constraint association. */
26601 init_constraint_processing (void)
26603 if (!flag_concepts
)
26606 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26607 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26608 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26609 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26610 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26613 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26617 declare_integer_pack (void)
26619 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26620 build_function_type_list (integer_type_node
,
26623 NULL_TREE
, ECF_CONST
);
26624 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26625 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26628 /* Set up the hash tables for template instantiations. */
26631 init_template_processing (void)
26633 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26634 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26636 if (cxx_dialect
>= cxx11
)
26637 declare_integer_pack ();
26640 /* Print stats about the template hash tables for -fstats. */
26643 print_template_statistics (void)
26645 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26646 "%f collisions\n", (long) decl_specializations
->size (),
26647 (long) decl_specializations
->elements (),
26648 decl_specializations
->collisions ());
26649 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26650 "%f collisions\n", (long) type_specializations
->size (),
26651 (long) type_specializations
->elements (),
26652 type_specializations
->collisions ());
26657 namespace selftest
{
26659 /* Verify that build_non_dependent_expr () works, for various expressions,
26660 and that location wrappers don't affect the results. */
26663 test_build_non_dependent_expr ()
26665 location_t loc
= BUILTINS_LOCATION
;
26667 /* Verify constants, without and with location wrappers. */
26668 tree int_cst
= build_int_cst (integer_type_node
, 42);
26669 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26671 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26672 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26673 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26675 tree string_lit
= build_string (4, "foo");
26676 TREE_TYPE (string_lit
) = char_array_type_node
;
26677 string_lit
= fix_string_type (string_lit
);
26678 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26680 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26681 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26682 ASSERT_EQ (wrapped_string_lit
,
26683 build_non_dependent_expr (wrapped_string_lit
));
26686 /* Verify that type_dependent_expression_p () works correctly, even
26687 in the presence of location wrapper nodes. */
26690 test_type_dependent_expression_p ()
26692 location_t loc
= BUILTINS_LOCATION
;
26694 tree name
= get_identifier ("foo");
26696 /* If no templates are involved, nothing is type-dependent. */
26697 gcc_assert (!processing_template_decl
);
26698 ASSERT_FALSE (type_dependent_expression_p (name
));
26700 ++processing_template_decl
;
26702 /* Within a template, an unresolved name is always type-dependent. */
26703 ASSERT_TRUE (type_dependent_expression_p (name
));
26705 /* Ensure it copes with NULL_TREE and errors. */
26706 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26707 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26709 /* A USING_DECL in a template should be type-dependent, even if wrapped
26710 with a location wrapper (PR c++/83799). */
26711 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26712 TREE_TYPE (using_decl
) = integer_type_node
;
26713 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26714 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26715 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26716 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26718 --processing_template_decl
;
26721 /* Run all of the selftests within this file. */
26726 test_build_non_dependent_expr ();
26727 test_type_dependent_expression_p ();
26730 } // namespace selftest
26732 #endif /* #if CHECKING_P */
26734 #include "gt-cp-pt.h"