1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2016 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"
44 /* The type of functions taking a tree, and some additional data, and
46 typedef int (*tree_fn_t
) (tree
, void*);
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template
{
52 struct pending_template
*next
;
53 struct tinst_level
*tinst
;
56 static GTY(()) struct pending_template
*pending_templates
;
57 static GTY(()) struct pending_template
*last_pending_template
;
59 int processing_template_parmlist
;
60 static int template_header_count
;
62 static GTY(()) tree saved_trees
;
63 static vec
<int> inline_parm_levels
;
65 static GTY(()) struct tinst_level
*current_tinst_level
;
67 static GTY(()) tree saved_access_scope
;
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr
;
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
77 // Implementation of the RAII helper for creating new local
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations
)
82 local_specializations
= new hash_map
<tree
, tree
>;
85 local_specialization_stack::~local_specialization_stack ()
87 delete local_specializations
;
88 local_specializations
= saved
;
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth
;
94 struct GTY((for_user
)) spec_entry
101 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
103 static hashval_t
hash (spec_entry
*);
104 static bool equal (spec_entry
*, spec_entry
*);
107 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
109 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
126 enum template_base_result
{
128 tbr_ambiguous_baseclass
,
132 static void push_access_scope (tree
);
133 static void pop_access_scope (tree
);
134 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
135 unification_kind_t
, int,
137 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
138 unification_kind_t
, int, bool, bool);
139 static int unify (tree
, tree
, tree
, tree
, int, bool);
140 static void add_pending_template (tree
);
141 static tree
reopen_tinst_level (struct tinst_level
*);
142 static tree
tsubst_initializer_list (tree
, tree
);
143 static tree
get_partial_spec_bindings (tree
, tree
, tree
, tree
);
144 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
146 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
148 static void tsubst_enum (tree
, tree
, tree
);
149 static tree
add_to_template_args (tree
, tree
);
150 static tree
add_outermost_template_args (tree
, tree
);
151 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
152 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
154 static int type_unification_real (tree
, tree
, tree
, const tree
*,
155 unsigned int, int, unification_kind_t
, int,
156 vec
<deferred_access_check
, va_gc
> **,
158 static void note_template_header (int);
159 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
160 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
161 static tree
convert_template_argument (tree
, tree
, tree
,
162 tsubst_flags_t
, int, tree
);
163 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
164 hash_set
<tree
> *, bool);
165 static tree
expand_template_argument_pack (tree
);
166 static tree
build_template_parm_index (int, int, int, tree
, tree
);
167 static bool inline_needs_template_parms (tree
, bool);
168 static void push_inline_template_parms_recursive (tree
, int);
169 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
170 static int mark_template_parm (tree
, void *);
171 static int template_parm_this_level_p (tree
, void *);
172 static tree
tsubst_friend_function (tree
, tree
);
173 static tree
tsubst_friend_class (tree
, tree
);
174 static int can_complete_type_without_circularity (tree
);
175 static tree
get_bindings (tree
, tree
, tree
, bool);
176 static int template_decl_level (tree
);
177 static int check_cv_quals_for_unify (int, tree
, tree
);
178 static void template_parm_level_and_index (tree
, int*, int*);
179 static int unify_pack_expansion (tree
, tree
, tree
,
180 tree
, unification_kind_t
, bool, bool);
181 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
182 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
183 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
184 static void regenerate_decl_from_template (tree
, tree
);
185 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
186 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
187 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
189 static bool check_specialization_scope (void);
190 static tree
process_partial_specialization (tree
);
191 static void set_current_access_from_decl (tree
);
192 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
194 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
195 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
197 static bool template_template_parm_bindings_ok_p (tree
, tree
);
198 static int template_args_equal (tree
, tree
);
199 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
200 static tree
for_each_template_parm_r (tree
*, int *, void *);
201 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
202 static void copy_default_args_to_explicit_spec (tree
);
203 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
204 static bool dependent_template_arg_p (tree
);
205 static bool any_template_arguments_need_structural_equality_p (tree
);
206 static bool dependent_type_p_r (tree
);
207 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
208 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
209 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
210 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
212 static tree
listify (tree
);
213 static tree
listify_autos (tree
, tree
);
214 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
215 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
216 static bool complex_alias_template_p (const_tree tmpl
);
218 /* Make the current scope suitable for access checking when we are
219 processing T. T can be FUNCTION_DECL for instantiated function
220 template, VAR_DECL for static member variable, or TYPE_DECL for
221 alias template (needed by instantiate_decl). */
224 push_access_scope (tree t
)
226 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
227 || TREE_CODE (t
) == TYPE_DECL
);
229 if (DECL_FRIEND_CONTEXT (t
))
230 push_nested_class (DECL_FRIEND_CONTEXT (t
));
231 else if (DECL_CLASS_SCOPE_P (t
))
232 push_nested_class (DECL_CONTEXT (t
));
234 push_to_top_level ();
236 if (TREE_CODE (t
) == FUNCTION_DECL
)
238 saved_access_scope
= tree_cons
239 (NULL_TREE
, current_function_decl
, saved_access_scope
);
240 current_function_decl
= t
;
244 /* Restore the scope set up by push_access_scope. T is the node we
248 pop_access_scope (tree t
)
250 if (TREE_CODE (t
) == FUNCTION_DECL
)
252 current_function_decl
= TREE_VALUE (saved_access_scope
);
253 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
256 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
259 pop_from_top_level ();
262 /* Do any processing required when DECL (a member template
263 declaration) is finished. Returns the TEMPLATE_DECL corresponding
264 to DECL, unless it is a specialization, in which case the DECL
265 itself is returned. */
268 finish_member_template_decl (tree decl
)
270 if (decl
== error_mark_node
)
271 return error_mark_node
;
273 gcc_assert (DECL_P (decl
));
275 if (TREE_CODE (decl
) == TYPE_DECL
)
279 type
= TREE_TYPE (decl
);
280 if (type
== error_mark_node
)
281 return error_mark_node
;
282 if (MAYBE_CLASS_TYPE_P (type
)
283 && CLASSTYPE_TEMPLATE_INFO (type
)
284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
286 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
287 check_member_template (tmpl
);
292 else if (TREE_CODE (decl
) == FIELD_DECL
)
293 error ("data member %qD cannot be a member template", decl
);
294 else if (DECL_TEMPLATE_INFO (decl
))
296 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
298 check_member_template (DECL_TI_TEMPLATE (decl
));
299 return DECL_TI_TEMPLATE (decl
);
305 error ("invalid member template declaration %qD", decl
);
307 return error_mark_node
;
310 /* Create a template info node. */
313 build_template_info (tree template_decl
, tree template_args
)
315 tree result
= make_node (TEMPLATE_INFO
);
316 TI_TEMPLATE (result
) = template_decl
;
317 TI_ARGS (result
) = template_args
;
321 /* Return the template info node corresponding to T, whatever T is. */
324 get_template_info (const_tree t
)
326 tree tinfo
= NULL_TREE
;
328 if (!t
|| t
== error_mark_node
)
331 if (TREE_CODE (t
) == NAMESPACE_DECL
)
334 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
335 tinfo
= DECL_TEMPLATE_INFO (t
);
337 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
340 if (OVERLOAD_TYPE_P (t
))
341 tinfo
= TYPE_TEMPLATE_INFO (t
);
342 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
343 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
348 /* Returns the template nesting level of the indicated class TYPE.
358 A<T>::B<U> has depth two, while A<T> has depth one.
359 Both A<T>::B<int> and A<int>::B<U> have depth one, if
360 they are instantiations, not specializations.
362 This function is guaranteed to return 0 if passed NULL_TREE so
363 that, for example, `template_class_depth (current_class_type)' is
367 template_class_depth (tree type
)
372 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
373 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
374 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
376 tree tinfo
= get_template_info (type
);
378 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
379 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
386 /* Subroutine of maybe_begin_member_template_processing.
387 Returns true if processing DECL needs us to push template parms. */
390 inline_needs_template_parms (tree decl
, bool nsdmi
)
392 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
395 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
396 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
399 /* Subroutine of maybe_begin_member_template_processing.
400 Push the template parms in PARMS, starting from LEVELS steps into the
401 chain, and ending at the beginning, since template parms are listed
405 push_inline_template_parms_recursive (tree parmlist
, int levels
)
407 tree parms
= TREE_VALUE (parmlist
);
411 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
413 ++processing_template_decl
;
414 current_template_parms
415 = tree_cons (size_int (processing_template_decl
),
416 parms
, current_template_parms
);
417 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
419 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
421 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
423 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
425 if (error_operand_p (parm
))
428 gcc_assert (DECL_P (parm
));
430 switch (TREE_CODE (parm
))
438 /* Push the CONST_DECL. */
439 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
448 /* Restore the template parameter context for a member template, a
449 friend template defined in a class definition, or a non-template
450 member of template class. */
453 maybe_begin_member_template_processing (tree decl
)
457 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
461 tree ctx
= DECL_CONTEXT (decl
);
462 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
463 /* Disregard full specializations (c++/60999). */
464 && uses_template_parms (ctx
)
465 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
468 if (inline_needs_template_parms (decl
, nsdmi
))
470 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
471 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
473 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
476 parms
= TREE_CHAIN (parms
);
479 push_inline_template_parms_recursive (parms
, levels
);
482 /* Remember how many levels of template parameters we pushed so that
483 we can pop them later. */
484 inline_parm_levels
.safe_push (levels
);
487 /* Undo the effects of maybe_begin_member_template_processing. */
490 maybe_end_member_template_processing (void)
495 if (inline_parm_levels
.length () == 0)
498 last
= inline_parm_levels
.pop ();
499 for (i
= 0; i
< last
; ++i
)
501 --processing_template_decl
;
502 current_template_parms
= TREE_CHAIN (current_template_parms
);
507 /* Return a new template argument vector which contains all of ARGS,
508 but has as its innermost set of arguments the EXTRA_ARGS. */
511 add_to_template_args (tree args
, tree extra_args
)
518 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
521 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
522 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
524 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
525 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
527 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
528 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
533 /* Like add_to_template_args, but only the outermost ARGS are added to
534 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
535 (EXTRA_ARGS) levels are added. This function is used to combine
536 the template arguments from a partial instantiation with the
537 template arguments used to attain the full instantiation from the
538 partial instantiation. */
541 add_outermost_template_args (tree args
, tree extra_args
)
545 /* If there are more levels of EXTRA_ARGS than there are ARGS,
546 something very fishy is going on. */
547 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
549 /* If *all* the new arguments will be the EXTRA_ARGS, just return
551 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
554 /* For the moment, we make ARGS look like it contains fewer levels. */
555 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
557 new_args
= add_to_template_args (args
, extra_args
);
559 /* Now, we restore ARGS to its full dimensions. */
560 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
565 /* Return the N levels of innermost template arguments from the ARGS. */
568 get_innermost_template_args (tree args
, int n
)
576 /* If N is 1, just return the innermost set of template arguments. */
578 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
580 /* If we're not removing anything, just return the arguments we were
582 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
583 gcc_assert (extra_levels
>= 0);
584 if (extra_levels
== 0)
587 /* Make a new set of arguments, not containing the outer arguments. */
588 new_args
= make_tree_vec (n
);
589 for (i
= 1; i
<= n
; ++i
)
590 SET_TMPL_ARGS_LEVEL (new_args
, i
,
591 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
596 /* The inverse of get_innermost_template_args: Return all but the innermost
597 EXTRA_LEVELS levels of template arguments from the ARGS. */
600 strip_innermost_template_args (tree args
, int extra_levels
)
603 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
608 /* If N is 1, just return the outermost set of template arguments. */
610 return TMPL_ARGS_LEVEL (args
, 1);
612 /* If we're not removing anything, just return the arguments we were
614 gcc_assert (extra_levels
>= 0);
615 if (extra_levels
== 0)
618 /* Make a new set of arguments, not containing the inner arguments. */
619 new_args
= make_tree_vec (n
);
620 for (i
= 1; i
<= n
; ++i
)
621 SET_TMPL_ARGS_LEVEL (new_args
, i
,
622 TMPL_ARGS_LEVEL (args
, i
));
627 /* We've got a template header coming up; push to a new level for storing
631 begin_template_parm_list (void)
633 /* We use a non-tag-transparent scope here, which causes pushtag to
634 put tags in this scope, rather than in the enclosing class or
635 namespace scope. This is the right thing, since we want
636 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
637 global template class, push_template_decl handles putting the
638 TEMPLATE_DECL into top-level scope. For a nested template class,
641 template <class T> struct S1 {
642 template <class T> struct S2 {};
645 pushtag contains special code to call pushdecl_with_scope on the
646 TEMPLATE_DECL for S2. */
647 begin_scope (sk_template_parms
, NULL
);
648 ++processing_template_decl
;
649 ++processing_template_parmlist
;
650 note_template_header (0);
652 /* Add a dummy parameter level while we process the parameter list. */
653 current_template_parms
654 = tree_cons (size_int (processing_template_decl
),
656 current_template_parms
);
659 /* This routine is called when a specialization is declared. If it is
660 invalid to declare a specialization here, an error is reported and
661 false is returned, otherwise this routine will return true. */
664 check_specialization_scope (void)
666 tree scope
= current_scope ();
670 An explicit specialization shall be declared in the namespace of
671 which the template is a member, or, for member templates, in the
672 namespace of which the enclosing class or enclosing class
673 template is a member. An explicit specialization of a member
674 function, member class or static data member of a class template
675 shall be declared in the namespace of which the class template
677 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
679 error ("explicit specialization in non-namespace scope %qD", scope
);
685 In an explicit specialization declaration for a member of a class
686 template or a member template that appears in namespace scope,
687 the member template and some of its enclosing class templates may
688 remain unspecialized, except that the declaration shall not
689 explicitly specialize a class member template if its enclosing
690 class templates are not explicitly specialized as well. */
691 if (current_template_parms
)
693 error ("enclosing class templates are not explicitly specialized");
700 /* We've just seen template <>. */
703 begin_specialization (void)
705 begin_scope (sk_template_spec
, NULL
);
706 note_template_header (1);
707 return check_specialization_scope ();
710 /* Called at then end of processing a declaration preceded by
714 end_specialization (void)
717 reset_specialization ();
720 /* Any template <>'s that we have seen thus far are not referring to a
721 function specialization. */
724 reset_specialization (void)
726 processing_specialization
= 0;
727 template_header_count
= 0;
730 /* We've just seen a template header. If SPECIALIZATION is nonzero,
731 it was of the form template <>. */
734 note_template_header (int specialization
)
736 processing_specialization
= specialization
;
737 template_header_count
++;
740 /* We're beginning an explicit instantiation. */
743 begin_explicit_instantiation (void)
745 gcc_assert (!processing_explicit_instantiation
);
746 processing_explicit_instantiation
= true;
751 end_explicit_instantiation (void)
753 gcc_assert (processing_explicit_instantiation
);
754 processing_explicit_instantiation
= false;
757 /* An explicit specialization or partial specialization of TMPL is being
758 declared. Check that the namespace in which the specialization is
759 occurring is permissible. Returns false iff it is invalid to
760 specialize TMPL in the current namespace. */
763 check_specialization_namespace (tree tmpl
)
765 tree tpl_ns
= decl_namespace_context (tmpl
);
769 An explicit specialization shall be declared in the namespace of
770 which the template is a member, or, for member templates, in the
771 namespace of which the enclosing class or enclosing class
772 template is a member. An explicit specialization of a member
773 function, member class or static data member of a class template
774 shall be declared in the namespace of which the class template is
776 if (current_scope() != DECL_CONTEXT (tmpl
)
777 && !at_namespace_scope_p ())
779 error ("specialization of %qD must appear at namespace scope", tmpl
);
782 if (is_associated_namespace (current_namespace
, tpl_ns
))
783 /* Same or super-using namespace. */
787 permerror (input_location
,
788 "specialization of %qD in different namespace", tmpl
);
789 permerror (DECL_SOURCE_LOCATION (tmpl
),
790 " from definition of %q#D", tmpl
);
795 /* SPEC is an explicit instantiation. Check that it is valid to
796 perform this explicit instantiation in the current namespace. */
799 check_explicit_instantiation_namespace (tree spec
)
803 /* DR 275: An explicit instantiation shall appear in an enclosing
804 namespace of its template. */
805 ns
= decl_namespace_context (spec
);
806 if (!is_ancestor (current_namespace
, ns
))
807 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
808 "(which does not enclose namespace %qD)",
809 spec
, current_namespace
, ns
);
812 // Returns the type of a template specialization only if that
813 // specialization needs to be defined. Otherwise (e.g., if the type has
814 // already been defined), the function returns NULL_TREE.
816 maybe_new_partial_specialization (tree type
)
818 // An implicit instantiation of an incomplete type implies
819 // the definition of a new class template.
821 // template<typename T>
824 // template<typename T>
827 // Here, S<T*> is an implicit instantiation of S whose type
829 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
832 // It can also be the case that TYPE is a completed specialization.
833 // Continuing the previous example, suppose we also declare:
835 // template<typename T>
836 // requires Integral<T>
839 // Here, S<T*> refers to the specialization S<T*> defined
840 // above. However, we need to differentiate definitions because
841 // we intend to define a new partial specialization. In this case,
842 // we rely on the fact that the constraints are different for
843 // this declaration than that above.
845 // Note that we also get here for injected class names and
846 // late-parsed template definitions. We must ensure that we
847 // do not create new type declarations for those cases.
848 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
850 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
851 tree args
= CLASSTYPE_TI_ARGS (type
);
853 // If there are no template parameters, this cannot be a new
854 // partial template specializtion?
855 if (!current_template_parms
)
858 // The injected-class-name is not a new partial specialization.
859 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
862 // If the constraints are not the same as those of the primary
863 // then, we can probably create a new specialization.
864 tree type_constr
= current_template_constraints ();
866 if (type
== TREE_TYPE (tmpl
))
868 tree main_constr
= get_constraints (tmpl
);
869 if (equivalent_constraints (type_constr
, main_constr
))
873 // Also, if there's a pre-existing specialization with matching
874 // constraints, then this also isn't new.
875 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
878 tree spec_tmpl
= TREE_VALUE (specs
);
879 tree spec_args
= TREE_PURPOSE (specs
);
880 tree spec_constr
= get_constraints (spec_tmpl
);
881 if (comp_template_args (args
, spec_args
)
882 && equivalent_constraints (type_constr
, spec_constr
))
884 specs
= TREE_CHAIN (specs
);
887 // Create a new type node (and corresponding type decl)
888 // for the newly declared specialization.
889 tree t
= make_class_type (TREE_CODE (type
));
890 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
891 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (type
);
892 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
894 /* We only need a separate type node for storing the definition of this
895 partial specialization; uses of S<T*> are unconstrained, so all are
896 equivalent. So keep TYPE_CANONICAL the same. */
897 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
899 // Build the corresponding type decl.
900 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
901 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
902 DECL_SOURCE_LOCATION (d
) = input_location
;
910 /* The TYPE is being declared. If it is a template type, that means it
911 is a partial specialization. Do appropriate error-checking. */
914 maybe_process_partial_specialization (tree type
)
918 if (type
== error_mark_node
)
919 return error_mark_node
;
921 /* A lambda that appears in specialization context is not itself a
923 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
926 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
928 error ("name of class shadows template template parameter %qD",
930 return error_mark_node
;
933 context
= TYPE_CONTEXT (type
);
935 if (TYPE_ALIAS_P (type
))
937 if (TYPE_TEMPLATE_INFO (type
)
938 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
939 error ("specialization of alias template %qD",
940 TYPE_TI_TEMPLATE (type
));
942 error ("explicit specialization of non-template %qT", type
);
943 return error_mark_node
;
945 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
947 /* This is for ordinary explicit specialization and partial
948 specialization of a template class such as:
950 template <> class C<int>;
954 template <class T> class C<T*>;
956 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
958 if (tree t
= maybe_new_partial_specialization (type
))
960 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
961 && !at_namespace_scope_p ())
962 return error_mark_node
;
963 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
964 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
965 if (processing_template_decl
)
967 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
968 if (decl
== error_mark_node
)
969 return error_mark_node
;
970 return TREE_TYPE (decl
);
973 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
974 error ("specialization of %qT after instantiation", type
);
975 else if (errorcount
&& !processing_specialization
976 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
977 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
978 /* Trying to define a specialization either without a template<> header
979 or in an inappropriate place. We've already given an error, so just
980 bail now so we don't actually define the specialization. */
981 return error_mark_node
;
983 else if (CLASS_TYPE_P (type
)
984 && !CLASSTYPE_USE_TEMPLATE (type
)
985 && CLASSTYPE_TEMPLATE_INFO (type
)
986 && context
&& CLASS_TYPE_P (context
)
987 && CLASSTYPE_TEMPLATE_INFO (context
))
989 /* This is for an explicit specialization of member class
990 template according to [temp.expl.spec/18]:
992 template <> template <class U> class C<int>::D;
994 The context `C<int>' must be an implicit instantiation.
995 Otherwise this is just a member class template declared
998 template <> class C<int> { template <class U> class D; };
999 template <> template <class U> class C<int>::D;
1001 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1002 while in the second case, `C<int>::D' is a primary template
1003 and `C<T>::D' may not exist. */
1005 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1006 && !COMPLETE_TYPE_P (type
))
1009 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1011 if (current_namespace
1012 != decl_namespace_context (tmpl
))
1014 permerror (input_location
,
1015 "specializing %q#T in different namespace", type
);
1016 permerror (DECL_SOURCE_LOCATION (tmpl
),
1017 " from definition of %q#D", tmpl
);
1020 /* Check for invalid specialization after instantiation:
1022 template <> template <> class C<int>::D<int>;
1023 template <> template <class U> class C<int>::D; */
1025 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1026 t
; t
= TREE_CHAIN (t
))
1028 tree inst
= TREE_VALUE (t
);
1029 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1030 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1032 /* We already have a full specialization of this partial
1033 instantiation, or a full specialization has been
1034 looked up but not instantiated. Reassign it to the
1035 new member specialization template. */
1039 elt
.tmpl
= most_general_template (tmpl
);
1040 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1043 type_specializations
->remove_elt (&elt
);
1046 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1049 = type_specializations
->find_slot (&elt
, INSERT
);
1050 entry
= ggc_alloc
<spec_entry
> ();
1055 /* But if we've had an implicit instantiation, that's a
1056 problem ([temp.expl.spec]/6). */
1057 error ("specialization %qT after instantiation %qT",
1061 /* Mark TYPE as a specialization. And as a result, we only
1062 have one level of template argument for the innermost
1064 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1065 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1066 CLASSTYPE_TI_ARGS (type
)
1067 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1070 else if (processing_specialization
)
1072 /* Someday C++0x may allow for enum template specialization. */
1073 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1074 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1075 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1076 "of %qD not allowed by ISO C++", type
);
1079 error ("explicit specialization of non-template %qT", type
);
1080 return error_mark_node
;
1087 /* Returns nonzero if we can optimize the retrieval of specializations
1088 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1089 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1092 optimize_specialization_lookup_p (tree tmpl
)
1094 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1095 && DECL_CLASS_SCOPE_P (tmpl
)
1096 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1098 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1099 /* The optimized lookup depends on the fact that the
1100 template arguments for the member function template apply
1101 purely to the containing class, which is not true if the
1102 containing class is an explicit or partial
1104 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1105 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1106 && !DECL_CONV_FN_P (tmpl
)
1107 /* It is possible to have a template that is not a member
1108 template and is not a member of a template class:
1110 template <typename T>
1111 struct S { friend A::f(); };
1113 Here, the friend function is a template, but the context does
1114 not have template information. The optimized lookup relies
1115 on having ARGS be the template arguments for both the class
1116 and the function template. */
1117 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1120 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1121 gone through coerce_template_parms by now. */
1124 verify_unstripped_args (tree args
)
1126 ++processing_template_decl
;
1127 if (!any_dependent_template_arguments_p (args
))
1129 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1130 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1132 tree arg
= TREE_VEC_ELT (inner
, i
);
1133 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1135 else if (TYPE_P (arg
))
1136 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1137 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1138 /* Allow typedefs on the type of a non-type argument, since a
1139 parameter can have them. */;
1141 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1144 --processing_template_decl
;
1147 /* Retrieve the specialization (in the sense of [temp.spec] - a
1148 specialization is either an instantiation or an explicit
1149 specialization) of TMPL for the given template ARGS. If there is
1150 no such specialization, return NULL_TREE. The ARGS are a vector of
1151 arguments, or a vector of vectors of arguments, in the case of
1152 templates with more than one level of parameters.
1154 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1155 then we search for a partial specialization matching ARGS. This
1156 parameter is ignored if TMPL is not a class template.
1158 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1159 result is a NONTYPE_ARGUMENT_PACK. */
1162 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1164 if (tmpl
== NULL_TREE
)
1167 if (args
== error_mark_node
)
1170 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1171 || TREE_CODE (tmpl
) == FIELD_DECL
);
1173 /* There should be as many levels of arguments as there are
1174 levels of parameters. */
1175 gcc_assert (TMPL_ARGS_DEPTH (args
)
1176 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1177 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1178 : template_class_depth (DECL_CONTEXT (tmpl
))));
1181 verify_unstripped_args (args
);
1183 if (optimize_specialization_lookup_p (tmpl
))
1185 tree class_template
;
1186 tree class_specialization
;
1187 vec
<tree
, va_gc
> *methods
;
1191 /* The template arguments actually apply to the containing
1192 class. Find the class specialization with those
1194 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1195 class_specialization
1196 = retrieve_specialization (class_template
, args
, 0);
1197 if (!class_specialization
)
1199 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1200 for the specialization. */
1201 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1204 /* Iterate through the methods with the indicated name, looking
1205 for the one that has an instance of TMPL. */
1206 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1207 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1209 tree fn
= OVL_CURRENT (fns
);
1210 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1211 /* using-declarations can add base methods to the method vec,
1212 and we don't want those here. */
1213 && DECL_CONTEXT (fn
) == class_specialization
)
1222 hash_table
<spec_hasher
> *specializations
;
1226 elt
.spec
= NULL_TREE
;
1228 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1229 specializations
= type_specializations
;
1231 specializations
= decl_specializations
;
1234 hash
= spec_hasher::hash (&elt
);
1235 found
= specializations
->find_with_hash (&elt
, hash
);
1243 /* Like retrieve_specialization, but for local declarations. */
1246 retrieve_local_specialization (tree tmpl
)
1248 if (local_specializations
== NULL
)
1251 tree
*slot
= local_specializations
->get (tmpl
);
1252 return slot
? *slot
: NULL_TREE
;
1255 /* Returns nonzero iff DECL is a specialization of TMPL. */
1258 is_specialization_of (tree decl
, tree tmpl
)
1262 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1266 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1272 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1274 for (t
= TREE_TYPE (decl
);
1276 t
= CLASSTYPE_USE_TEMPLATE (t
)
1277 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1278 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1285 /* Returns nonzero iff DECL is a specialization of friend declaration
1286 FRIEND_DECL according to [temp.friend]. */
1289 is_specialization_of_friend (tree decl
, tree friend_decl
)
1291 bool need_template
= true;
1294 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1295 || TREE_CODE (decl
) == TYPE_DECL
);
1297 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1298 of a template class, we want to check if DECL is a specialization
1300 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1301 && DECL_TEMPLATE_INFO (friend_decl
)
1302 && !DECL_USE_TEMPLATE (friend_decl
))
1304 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1305 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1306 need_template
= false;
1308 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1309 && !PRIMARY_TEMPLATE_P (friend_decl
))
1310 need_template
= false;
1312 /* There is nothing to do if this is not a template friend. */
1313 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1316 if (is_specialization_of (decl
, friend_decl
))
1320 A member of a class template may be declared to be a friend of a
1321 non-template class. In this case, the corresponding member of
1322 every specialization of the class template is a friend of the
1323 class granting friendship.
1325 For example, given a template friend declaration
1327 template <class T> friend void A<T>::f();
1329 the member function below is considered a friend
1331 template <> struct A<int> {
1335 For this type of template friend, TEMPLATE_DEPTH below will be
1336 nonzero. To determine if DECL is a friend of FRIEND, we first
1337 check if the enclosing class is a specialization of another. */
1339 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1341 && DECL_CLASS_SCOPE_P (decl
)
1342 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1343 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1345 /* Next, we check the members themselves. In order to handle
1346 a few tricky cases, such as when FRIEND_DECL's are
1348 template <class T> friend void A<T>::g(T t);
1349 template <class T> template <T t> friend void A<T>::h();
1353 void A<int>::g(int);
1354 template <int> void A<int>::h();
1356 we need to figure out ARGS, the template arguments from
1357 the context of DECL. This is required for template substitution
1358 of `T' in the function parameter of `g' and template parameter
1359 of `h' in the above examples. Here ARGS corresponds to `int'. */
1361 tree context
= DECL_CONTEXT (decl
);
1362 tree args
= NULL_TREE
;
1363 int current_depth
= 0;
1365 while (current_depth
< template_depth
)
1367 if (CLASSTYPE_TEMPLATE_INFO (context
))
1369 if (current_depth
== 0)
1370 args
= TYPE_TI_ARGS (context
);
1372 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1375 context
= TYPE_CONTEXT (context
);
1378 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1383 tree friend_args_type
;
1384 tree decl_args_type
;
1386 /* Make sure that both DECL and FRIEND_DECL are templates or
1388 is_template
= DECL_TEMPLATE_INFO (decl
)
1389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1390 if (need_template
^ is_template
)
1392 else if (is_template
)
1394 /* If both are templates, check template parameter list. */
1396 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1398 if (!comp_template_parms
1399 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1403 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1406 decl_type
= TREE_TYPE (decl
);
1408 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1409 tf_none
, NULL_TREE
);
1410 if (friend_type
== error_mark_node
)
1413 /* Check if return types match. */
1414 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1417 /* Check if function parameter types match, ignoring the
1418 `this' parameter. */
1419 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1420 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1422 friend_args_type
= TREE_CHAIN (friend_args_type
);
1423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1424 decl_args_type
= TREE_CHAIN (decl_args_type
);
1426 return compparms (decl_args_type
, friend_args_type
);
1430 /* DECL is a TYPE_DECL */
1432 tree decl_type
= TREE_TYPE (decl
);
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1437 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1440 if (need_template
^ is_template
)
1442 else if (is_template
)
1445 /* If both are templates, check the name of the two
1446 TEMPLATE_DECL's first because is_friend didn't. */
1447 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1448 != DECL_NAME (friend_decl
))
1451 /* Now check template parameter list. */
1453 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1455 return comp_template_parms
1456 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1460 return (DECL_NAME (decl
)
1461 == DECL_NAME (friend_decl
));
1467 /* Register the specialization SPEC as a specialization of TMPL with
1468 the indicated ARGS. IS_FRIEND indicates whether the specialization
1469 is actually just a friend declaration. Returns SPEC, or an
1470 equivalent prior declaration, if available.
1472 We also store instantiations of field packs in the hash table, even
1473 though they are not themselves templates, to make lookup easier. */
1476 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1480 spec_entry
**slot
= NULL
;
1483 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1484 || (TREE_CODE (tmpl
) == FIELD_DECL
1485 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1487 if (TREE_CODE (spec
) == FUNCTION_DECL
1488 && uses_template_parms (DECL_TI_ARGS (spec
)))
1489 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1490 register it; we want the corresponding TEMPLATE_DECL instead.
1491 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1492 the more obvious `uses_template_parms (spec)' to avoid problems
1493 with default function arguments. In particular, given
1494 something like this:
1496 template <class T> void f(T t1, T t = T())
1498 the default argument expression is not substituted for in an
1499 instantiation unless and until it is actually needed. */
1502 if (optimize_specialization_lookup_p (tmpl
))
1503 /* We don't put these specializations in the hash table, but we might
1504 want to give an error about a mismatch. */
1505 fn
= retrieve_specialization (tmpl
, args
, 0);
1513 hash
= spec_hasher::hash (&elt
);
1516 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1518 fn
= ((spec_entry
*) *slot
)->spec
;
1523 /* We can sometimes try to re-register a specialization that we've
1524 already got. In particular, regenerate_decl_from_template calls
1525 duplicate_decls which will update the specialization list. But,
1526 we'll still get called again here anyhow. It's more convenient
1527 to simply allow this than to try to prevent it. */
1530 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1532 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1534 if (DECL_ODR_USED (fn
)
1535 || DECL_EXPLICIT_INSTANTIATION (fn
))
1537 error ("specialization of %qD after instantiation",
1539 return error_mark_node
;
1544 /* This situation should occur only if the first
1545 specialization is an implicit instantiation, the
1546 second is an explicit specialization, and the
1547 implicit instantiation has not yet been used. That
1548 situation can occur if we have implicitly
1549 instantiated a member function and then specialized
1552 We can also wind up here if a friend declaration that
1553 looked like an instantiation turns out to be a
1556 template <class T> void foo(T);
1557 class S { friend void foo<>(int) };
1558 template <> void foo(int);
1560 We transform the existing DECL in place so that any
1561 pointers to it become pointers to the updated
1564 If there was a definition for the template, but not
1565 for the specialization, we want this to look as if
1566 there were no definition, and vice versa. */
1567 DECL_INITIAL (fn
) = NULL_TREE
;
1568 duplicate_decls (spec
, fn
, is_friend
);
1569 /* The call to duplicate_decls will have applied
1572 An explicit specialization of a function template
1573 is inline only if it is explicitly declared to be,
1574 and independently of whether its function template
1577 to the primary function; now copy the inline bits to
1578 the various clones. */
1579 FOR_EACH_CLONE (clone
, fn
)
1581 DECL_DECLARED_INLINE_P (clone
)
1582 = DECL_DECLARED_INLINE_P (fn
);
1583 DECL_SOURCE_LOCATION (clone
)
1584 = DECL_SOURCE_LOCATION (fn
);
1585 DECL_DELETED_FN (clone
)
1586 = DECL_DELETED_FN (fn
);
1588 check_specialization_namespace (tmpl
);
1593 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1595 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1596 /* Dup decl failed, but this is a new definition. Set the
1597 line number so any errors match this new
1599 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1605 return duplicate_decls (spec
, fn
, is_friend
);
1607 /* A specialization must be declared in the same namespace as the
1608 template it is specializing. */
1609 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1610 && !check_specialization_namespace (tmpl
))
1611 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1613 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1615 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1616 gcc_assert (tmpl
&& args
&& spec
);
1619 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1620 && PRIMARY_TEMPLATE_P (tmpl
)
1621 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1622 || variable_template_p (tmpl
))
1623 /* If TMPL is a forward declaration of a template function, keep a list
1624 of all specializations in case we need to reassign them to a friend
1625 template later in tsubst_friend_function.
1627 Also keep a list of all variable template instantiations so that
1628 process_partial_specialization can check whether a later partial
1629 specialization would have used it. */
1630 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1631 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1637 /* Returns true iff two spec_entry nodes are equivalent. */
1639 int comparing_specializations
;
1642 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1646 ++comparing_specializations
;
1647 equal
= (e1
->tmpl
== e2
->tmpl
1648 && comp_template_args (e1
->args
, e2
->args
));
1649 if (equal
&& flag_concepts
1650 /* tmpl could be a FIELD_DECL for a capture pack. */
1651 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1652 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1653 && uses_template_parms (e1
->args
))
1655 /* Partial specializations of a variable template can be distinguished by
1657 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1658 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1659 equal
= equivalent_constraints (c1
, c2
);
1661 --comparing_specializations
;
1666 /* Returns a hash for a template TMPL and template arguments ARGS. */
1669 hash_tmpl_and_args (tree tmpl
, tree args
)
1671 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1672 return iterative_hash_template_arg (args
, val
);
1675 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1679 spec_hasher::hash (spec_entry
*e
)
1681 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1684 /* Recursively calculate a hash value for a template argument ARG, for use
1685 in the hash tables of template specializations. */
1688 iterative_hash_template_arg (tree arg
, hashval_t val
)
1690 unsigned HOST_WIDE_INT i
;
1691 enum tree_code code
;
1694 if (arg
== NULL_TREE
)
1695 return iterative_hash_object (arg
, val
);
1700 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1701 /* We can get one of these when re-hashing a previous entry in the middle
1702 of substituting into a pack expansion. Just look through it. */
1703 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1705 code
= TREE_CODE (arg
);
1706 tclass
= TREE_CODE_CLASS (code
);
1708 val
= iterative_hash_object (code
, val
);
1715 case IDENTIFIER_NODE
:
1716 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1720 int i
, len
= TREE_VEC_LENGTH (arg
);
1721 for (i
= 0; i
< len
; ++i
)
1722 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1726 case TYPE_PACK_EXPANSION
:
1727 case EXPR_PACK_EXPANSION
:
1728 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1729 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1731 case TYPE_ARGUMENT_PACK
:
1732 case NONTYPE_ARGUMENT_PACK
:
1733 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1736 for (; arg
; arg
= TREE_CHAIN (arg
))
1737 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1741 for (; arg
; arg
= OVL_NEXT (arg
))
1742 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1748 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1749 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1751 val
= iterative_hash_template_arg (field
, val
);
1752 val
= iterative_hash_template_arg (value
, val
);
1758 if (!DECL_ARTIFICIAL (arg
))
1760 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1761 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1763 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1766 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1769 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1770 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1772 case TEMPLATE_PARM_INDEX
:
1773 val
= iterative_hash_template_arg
1774 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1775 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1776 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1779 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1780 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1781 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1784 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1786 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1790 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1791 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1792 val
= iterative_hash_object (code
, val
);
1793 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1796 /* A lambda can't appear in a template arg, but don't crash on
1798 gcc_assert (seen_error ());
1802 case IMPLICIT_CONV_EXPR
:
1803 case STATIC_CAST_EXPR
:
1804 case REINTERPRET_CAST_EXPR
:
1805 case CONST_CAST_EXPR
:
1806 case DYNAMIC_CAST_EXPR
:
1808 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1809 /* Now hash operands as usual. */
1819 if (alias_template_specialization_p (arg
))
1821 // We want an alias specialization that survived strip_typedefs
1822 // to hash differently from its TYPE_CANONICAL, to avoid hash
1823 // collisions that compare as different in template_args_equal.
1824 // These could be dependent specializations that strip_typedefs
1825 // left alone, or untouched specializations because
1826 // coerce_template_parms returns the unconverted template
1827 // arguments if it sees incomplete argument packs.
1828 tree ti
= TYPE_TEMPLATE_INFO (arg
);
1829 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1831 if (TYPE_CANONICAL (arg
))
1832 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1834 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1835 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1836 /* Otherwise just compare the types during lookup. */
1839 case tcc_declaration
:
1841 return iterative_hash_expr (arg
, val
);
1844 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1846 unsigned n
= cp_tree_operand_length (arg
);
1847 for (i
= 0; i
< n
; ++i
)
1848 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1856 /* Unregister the specialization SPEC as a specialization of TMPL.
1857 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1858 if the SPEC was listed as a specialization of TMPL.
1860 Note that SPEC has been ggc_freed, so we can't look inside it. */
1863 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1868 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1869 elt
.args
= TI_ARGS (tinfo
);
1870 elt
.spec
= NULL_TREE
;
1872 entry
= decl_specializations
->find (&elt
);
1875 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1876 gcc_assert (new_spec
!= NULL_TREE
);
1877 entry
->spec
= new_spec
;
1884 /* Like register_specialization, but for local declarations. We are
1885 registering SPEC, an instantiation of TMPL. */
1888 register_local_specialization (tree spec
, tree tmpl
)
1890 local_specializations
->put (tmpl
, spec
);
1893 /* TYPE is a class type. Returns true if TYPE is an explicitly
1894 specialized class. */
1897 explicit_class_specialization_p (tree type
)
1899 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1901 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1904 /* Print the list of functions at FNS, going through all the overloads
1905 for each element of the list. Alternatively, FNS can not be a
1906 TREE_LIST, in which case it will be printed together with all the
1909 MORE and *STR should respectively be FALSE and NULL when the function
1910 is called from the outside. They are used internally on recursive
1911 calls. print_candidates manages the two parameters and leaves NULL
1912 in *STR when it ends. */
1915 print_candidates_1 (tree fns
, bool more
, const char **str
)
1918 char *spaces
= NULL
;
1920 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1921 if (TREE_CODE (fn
) == TREE_LIST
)
1923 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1924 print_candidates_1 (TREE_VALUE (fn2
),
1925 TREE_CHAIN (fn2
) || more
, str
);
1929 tree cand
= OVL_CURRENT (fn
);
1932 /* Pick the prefix string. */
1933 if (!more
&& !OVL_NEXT (fns
))
1935 inform (DECL_SOURCE_LOCATION (cand
),
1936 "candidate is: %#D", cand
);
1940 *str
= _("candidates are:");
1941 spaces
= get_spaces (*str
);
1943 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1944 *str
= spaces
? spaces
: *str
;
1954 /* Print the list of candidate FNS in an error message. FNS can also
1955 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1958 print_candidates (tree fns
)
1960 const char *str
= NULL
;
1961 print_candidates_1 (fns
, false, &str
);
1962 gcc_assert (str
== NULL
);
1965 /* Get a (possibly) constrained template declaration for the
1966 purpose of ordering candidates. */
1968 get_template_for_ordering (tree list
)
1970 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1971 tree f
= TREE_VALUE (list
);
1972 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1973 return TI_TEMPLATE (ti
);
1977 /* Among candidates having the same signature, return the
1978 most constrained or NULL_TREE if there is no best candidate.
1979 If the signatures of candidates vary (e.g., template
1980 specialization vs. member function), then there can be no
1983 Note that we don't compare constraints on the functions
1984 themselves, but rather those of their templates. */
1986 most_constrained_function (tree candidates
)
1988 // Try to find the best candidate in a first pass.
1989 tree champ
= candidates
;
1990 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1992 int winner
= more_constrained (get_template_for_ordering (champ
),
1993 get_template_for_ordering (c
));
1995 champ
= c
; // The candidate is more constrained
1996 else if (winner
== 0)
1997 return NULL_TREE
; // Neither is more constrained
2000 // Verify that the champ is better than previous candidates.
2001 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2002 if (!more_constrained (get_template_for_ordering (champ
),
2003 get_template_for_ordering (c
)))
2011 /* Returns the template (one of the functions given by TEMPLATE_ID)
2012 which can be specialized to match the indicated DECL with the
2013 explicit template args given in TEMPLATE_ID. The DECL may be
2014 NULL_TREE if none is available. In that case, the functions in
2015 TEMPLATE_ID are non-members.
2017 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2018 specialization of a member template.
2020 The TEMPLATE_COUNT is the number of references to qualifying
2021 template classes that appeared in the name of the function. See
2022 check_explicit_specialization for a more accurate description.
2024 TSK indicates what kind of template declaration (if any) is being
2025 declared. TSK_TEMPLATE indicates that the declaration given by
2026 DECL, though a FUNCTION_DECL, has template parameters, and is
2027 therefore a template function.
2029 The template args (those explicitly specified and those deduced)
2030 are output in a newly created vector *TARGS_OUT.
2032 If it is impossible to determine the result, an error message is
2033 issued. The error_mark_node is returned to indicate failure. */
2036 determine_specialization (tree template_id
,
2039 int need_member_template
,
2045 tree explicit_targs
;
2046 tree candidates
= NULL_TREE
;
2048 /* A TREE_LIST of templates of which DECL may be a specialization.
2049 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2050 corresponding TREE_PURPOSE is the set of template arguments that,
2051 when used to instantiate the template, would produce a function
2052 with the signature of DECL. */
2053 tree templates
= NULL_TREE
;
2055 cp_binding_level
*b
;
2057 *targs_out
= NULL_TREE
;
2059 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2060 return error_mark_node
;
2062 /* We shouldn't be specializing a member template of an
2063 unspecialized class template; we already gave an error in
2064 check_specialization_scope, now avoid crashing. */
2065 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2066 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2068 gcc_assert (errorcount
);
2069 return error_mark_node
;
2072 fns
= TREE_OPERAND (template_id
, 0);
2073 explicit_targs
= TREE_OPERAND (template_id
, 1);
2075 if (fns
== error_mark_node
)
2076 return error_mark_node
;
2078 /* Check for baselinks. */
2079 if (BASELINK_P (fns
))
2080 fns
= BASELINK_FUNCTIONS (fns
);
2082 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2084 error ("%qD is not a function template", fns
);
2085 return error_mark_node
;
2087 else if (VAR_P (decl
) && !variable_template_p (fns
))
2089 error ("%qD is not a variable template", fns
);
2090 return error_mark_node
;
2093 /* Count the number of template headers specified for this
2096 for (b
= current_binding_level
;
2097 b
->kind
== sk_template_parms
;
2101 tree orig_fns
= fns
;
2103 if (variable_template_p (fns
))
2105 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2106 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2107 tf_warning_or_error
,
2108 /*req_all*/true, /*use_defarg*/true);
2109 if (targs
!= error_mark_node
)
2110 templates
= tree_cons (targs
, fns
, templates
);
2112 else for (; fns
; fns
= OVL_NEXT (fns
))
2114 tree fn
= OVL_CURRENT (fns
);
2116 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2118 tree decl_arg_types
;
2122 /* In case of explicit specialization, we need to check if
2123 the number of template headers appearing in the specialization
2124 is correct. This is usually done in check_explicit_specialization,
2125 but the check done there cannot be exhaustive when specializing
2126 member functions. Consider the following code:
2128 template <> void A<int>::f(int);
2129 template <> template <> void A<int>::f(int);
2131 Assuming that A<int> is not itself an explicit specialization
2132 already, the first line specializes "f" which is a non-template
2133 member function, whilst the second line specializes "f" which
2134 is a template member function. So both lines are syntactically
2135 correct, and check_explicit_specialization does not reject
2138 Here, we can do better, as we are matching the specialization
2139 against the declarations. We count the number of template
2140 headers, and we check if they match TEMPLATE_COUNT + 1
2141 (TEMPLATE_COUNT is the number of qualifying template classes,
2142 plus there must be another header for the member template
2145 Notice that if header_count is zero, this is not a
2146 specialization but rather a template instantiation, so there
2147 is no check we can perform here. */
2148 if (header_count
&& header_count
!= template_count
+ 1)
2151 /* Check that the number of template arguments at the
2152 innermost level for DECL is the same as for FN. */
2153 if (current_binding_level
->kind
== sk_template_parms
2154 && !current_binding_level
->explicit_spec_p
2155 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2156 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2157 (current_template_parms
))))
2160 /* DECL might be a specialization of FN. */
2161 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2162 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2164 /* For a non-static member function, we need to make sure
2165 that the const qualification is the same. Since
2166 get_bindings does not try to merge the "this" parameter,
2167 we must do the comparison explicitly. */
2168 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2169 && !same_type_p (TREE_VALUE (fn_arg_types
),
2170 TREE_VALUE (decl_arg_types
)))
2173 /* Skip the "this" parameter and, for constructors of
2174 classes with virtual bases, the VTT parameter. A
2175 full specialization of a constructor will have a VTT
2176 parameter, but a template never will. */
2178 = skip_artificial_parms_for (decl
, decl_arg_types
);
2180 = skip_artificial_parms_for (fn
, fn_arg_types
);
2182 /* Function templates cannot be specializations; there are
2183 no partial specializations of functions. Therefore, if
2184 the type of DECL does not match FN, there is no
2187 Note that it should never be the case that we have both
2188 candidates added here, and for regular member functions
2190 if (tsk
== tsk_template
)
2192 if (compparms (fn_arg_types
, decl_arg_types
))
2193 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2197 /* See whether this function might be a specialization of this
2198 template. Suppress access control because we might be trying
2199 to make this specialization a friend, and we have already done
2200 access control for the declaration of the specialization. */
2201 push_deferring_access_checks (dk_no_check
);
2202 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2203 pop_deferring_access_checks ();
2206 /* We cannot deduce template arguments that when used to
2207 specialize TMPL will produce DECL. */
2210 /* Remove, from the set of candidates, all those functions
2211 whose constraints are not satisfied. */
2212 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2215 // Then, try to form the new function type.
2216 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
2217 if (insttype
== error_mark_node
)
2220 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2221 if (!compparms (fn_arg_types
, decl_arg_types
))
2224 /* Save this template, and the arguments deduced. */
2225 templates
= tree_cons (targs
, fn
, templates
);
2227 else if (need_member_template
)
2228 /* FN is an ordinary member function, and we need a
2229 specialization of a member template. */
2231 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2232 /* We can get IDENTIFIER_NODEs here in certain erroneous
2235 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2236 /* This is just an ordinary non-member function. Nothing can
2237 be a specialization of that. */
2239 else if (DECL_ARTIFICIAL (fn
))
2240 /* Cannot specialize functions that are created implicitly. */
2244 tree decl_arg_types
;
2246 /* This is an ordinary member function. However, since
2247 we're here, we can assume its enclosing class is a
2248 template class. For example,
2250 template <typename T> struct S { void f(); };
2251 template <> void S<int>::f() {}
2253 Here, S<int>::f is a non-template, but S<int> is a
2254 template class. If FN has the same type as DECL, we
2255 might be in business. */
2257 if (!DECL_TEMPLATE_INFO (fn
))
2258 /* Its enclosing class is an explicit specialization
2259 of a template class. This is not a candidate. */
2262 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2263 TREE_TYPE (TREE_TYPE (fn
))))
2264 /* The return types differ. */
2267 /* Adjust the type of DECL in case FN is a static member. */
2268 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2269 if (DECL_STATIC_FUNCTION_P (fn
)
2270 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2271 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2273 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2277 // If the deduced arguments do not satisfy the constraints,
2278 // this is not a candidate.
2279 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2282 // Add the candidate.
2283 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2287 if (templates
&& TREE_CHAIN (templates
))
2293 It is possible for a specialization with a given function
2294 signature to be instantiated from more than one function
2295 template. In such cases, explicit specification of the
2296 template arguments must be used to uniquely identify the
2297 function template specialization being specialized.
2299 Note that here, there's no suggestion that we're supposed to
2300 determine which of the candidate templates is most
2301 specialized. However, we, also have:
2305 Partial ordering of overloaded function template
2306 declarations is used in the following contexts to select
2307 the function template to which a function template
2308 specialization refers:
2310 -- when an explicit specialization refers to a function
2313 So, we do use the partial ordering rules, at least for now.
2314 This extension can only serve to make invalid programs valid,
2315 so it's safe. And, there is strong anecdotal evidence that
2316 the committee intended the partial ordering rules to apply;
2317 the EDG front end has that behavior, and John Spicer claims
2318 that the committee simply forgot to delete the wording in
2319 [temp.expl.spec]. */
2320 tree tmpl
= most_specialized_instantiation (templates
);
2321 if (tmpl
!= error_mark_node
)
2324 TREE_CHAIN (templates
) = NULL_TREE
;
2328 // Concepts allows multiple declarations of member functions
2329 // with the same signature. Like above, we need to rely on
2330 // on the partial ordering of those candidates to determine which
2332 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2334 if (tree cand
= most_constrained_function (candidates
))
2337 TREE_CHAIN (cand
) = NULL_TREE
;
2341 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2343 error ("template-id %qD for %q+D does not match any template "
2344 "declaration", template_id
, decl
);
2345 if (header_count
&& header_count
!= template_count
+ 1)
2346 inform (input_location
, "saw %d %<template<>%>, need %d for "
2347 "specializing a member function template",
2348 header_count
, template_count
+ 1);
2350 print_candidates (orig_fns
);
2351 return error_mark_node
;
2353 else if ((templates
&& TREE_CHAIN (templates
))
2354 || (candidates
&& TREE_CHAIN (candidates
))
2355 || (templates
&& candidates
))
2357 error ("ambiguous template specialization %qD for %q+D",
2359 candidates
= chainon (candidates
, templates
);
2360 print_candidates (candidates
);
2361 return error_mark_node
;
2364 /* We have one, and exactly one, match. */
2367 tree fn
= TREE_VALUE (candidates
);
2368 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2370 // Propagate the candidate's constraints to the declaration.
2371 set_constraints (decl
, get_constraints (fn
));
2373 /* DECL is a re-declaration or partial instantiation of a template
2375 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2377 /* It was a specialization of an ordinary member function in a
2379 return DECL_TI_TEMPLATE (fn
);
2382 /* It was a specialization of a template. */
2383 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2384 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2386 *targs_out
= copy_node (targs
);
2387 SET_TMPL_ARGS_LEVEL (*targs_out
,
2388 TMPL_ARGS_DEPTH (*targs_out
),
2389 TREE_PURPOSE (templates
));
2392 *targs_out
= TREE_PURPOSE (templates
);
2393 return TREE_VALUE (templates
);
2396 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2397 but with the default argument values filled in from those in the
2401 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2404 tree new_spec_types
;
2409 if (spec_types
== void_list_node
)
2410 return void_list_node
;
2412 /* Substitute into the rest of the list. */
2414 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2415 TREE_CHAIN (tmpl_types
));
2417 /* Add the default argument for this parameter. */
2418 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2419 TREE_VALUE (spec_types
),
2423 /* DECL is an explicit specialization. Replicate default arguments
2424 from the template it specializes. (That way, code like:
2426 template <class T> void f(T = 3);
2427 template <> void f(double);
2430 works, as required.) An alternative approach would be to look up
2431 the correct default arguments at the call-site, but this approach
2432 is consistent with how implicit instantiations are handled. */
2435 copy_default_args_to_explicit_spec (tree decl
)
2440 tree new_spec_types
;
2444 tree object_type
= NULL_TREE
;
2445 tree in_charge
= NULL_TREE
;
2446 tree vtt
= NULL_TREE
;
2448 /* See if there's anything we need to do. */
2449 tmpl
= DECL_TI_TEMPLATE (decl
);
2450 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2451 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2452 if (TREE_PURPOSE (t
))
2457 old_type
= TREE_TYPE (decl
);
2458 spec_types
= TYPE_ARG_TYPES (old_type
);
2460 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2462 /* Remove the this pointer, but remember the object's type for
2464 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2465 spec_types
= TREE_CHAIN (spec_types
);
2466 tmpl_types
= TREE_CHAIN (tmpl_types
);
2468 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2470 /* DECL may contain more parameters than TMPL due to the extra
2471 in-charge parameter in constructors and destructors. */
2472 in_charge
= spec_types
;
2473 spec_types
= TREE_CHAIN (spec_types
);
2475 if (DECL_HAS_VTT_PARM_P (decl
))
2478 spec_types
= TREE_CHAIN (spec_types
);
2482 /* Compute the merged default arguments. */
2484 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2486 /* Compute the new FUNCTION_TYPE. */
2490 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2495 /* Put the in-charge parameter back. */
2496 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2497 TREE_VALUE (in_charge
),
2500 new_type
= build_method_type_directly (object_type
,
2501 TREE_TYPE (old_type
),
2505 new_type
= build_function_type (TREE_TYPE (old_type
),
2507 new_type
= cp_build_type_attribute_variant (new_type
,
2508 TYPE_ATTRIBUTES (old_type
));
2509 new_type
= build_exception_variant (new_type
,
2510 TYPE_RAISES_EXCEPTIONS (old_type
));
2512 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2513 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2515 TREE_TYPE (decl
) = new_type
;
2518 /* Return the number of template headers we expect to see for a definition
2519 or specialization of CTYPE or one of its non-template members. */
2522 num_template_headers_for_class (tree ctype
)
2524 int num_templates
= 0;
2526 while (ctype
&& CLASS_TYPE_P (ctype
))
2528 /* You're supposed to have one `template <...>' for every
2529 template class, but you don't need one for a full
2530 specialization. For example:
2532 template <class T> struct S{};
2533 template <> struct S<int> { void f(); };
2534 void S<int>::f () {}
2536 is correct; there shouldn't be a `template <>' for the
2537 definition of `S<int>::f'. */
2538 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2539 /* If CTYPE does not have template information of any
2540 kind, then it is not a template, nor is it nested
2541 within a template. */
2543 if (explicit_class_specialization_p (ctype
))
2545 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2548 ctype
= TYPE_CONTEXT (ctype
);
2551 return num_templates
;
2554 /* Do a simple sanity check on the template headers that precede the
2555 variable declaration DECL. */
2558 check_template_variable (tree decl
)
2560 tree ctx
= CP_DECL_CONTEXT (decl
);
2561 int wanted
= num_template_headers_for_class (ctx
);
2562 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2563 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2565 if (cxx_dialect
< cxx14
)
2566 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2567 "variable templates only available with "
2568 "-std=c++14 or -std=gnu++14");
2570 // Namespace-scope variable templates should have a template header.
2573 if (template_header_count
> wanted
)
2575 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2576 "too many template headers for %D (should be %d)",
2578 if (warned
&& CLASS_TYPE_P (ctx
)
2579 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2580 inform (DECL_SOURCE_LOCATION (decl
),
2581 "members of an explicitly specialized class are defined "
2582 "without a template header");
2586 /* Check to see if the function just declared, as indicated in
2587 DECLARATOR, and in DECL, is a specialization of a function
2588 template. We may also discover that the declaration is an explicit
2589 instantiation at this point.
2591 Returns DECL, or an equivalent declaration that should be used
2592 instead if all goes well. Issues an error message if something is
2593 amiss. Returns error_mark_node if the error is not easily
2596 FLAGS is a bitmask consisting of the following flags:
2598 2: The function has a definition.
2599 4: The function is a friend.
2601 The TEMPLATE_COUNT is the number of references to qualifying
2602 template classes that appeared in the name of the function. For
2605 template <class T> struct S { void f(); };
2608 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2609 classes are not counted in the TEMPLATE_COUNT, so that in
2611 template <class T> struct S {};
2612 template <> struct S<int> { void f(); }
2613 template <> void S<int>::f();
2615 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2616 invalid; there should be no template <>.)
2618 If the function is a specialization, it is marked as such via
2619 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2620 is set up correctly, and it is added to the list of specializations
2621 for that template. */
2624 check_explicit_specialization (tree declarator
,
2629 int have_def
= flags
& 2;
2630 int is_friend
= flags
& 4;
2631 bool is_concept
= flags
& 8;
2632 int specialization
= 0;
2633 int explicit_instantiation
= 0;
2634 int member_specialization
= 0;
2635 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2636 tree dname
= DECL_NAME (decl
);
2641 if (!processing_specialization
)
2644 tsk
= tsk_excessive_parms
;
2647 tsk
= current_tmpl_spec_kind (template_count
);
2652 if (processing_specialization
&& !VAR_P (decl
))
2655 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2657 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2660 /* This could be something like:
2662 template <class T> void f(T);
2663 class S { friend void f<>(int); } */
2667 /* This case handles bogus declarations like template <>
2668 template <class T> void f<int>(); */
2670 error ("template-id %qD in declaration of primary template",
2677 case tsk_invalid_member_spec
:
2678 /* The error has already been reported in
2679 check_specialization_scope. */
2680 return error_mark_node
;
2682 case tsk_invalid_expl_inst
:
2683 error ("template parameter list used in explicit instantiation");
2689 error ("definition provided for explicit instantiation");
2691 explicit_instantiation
= 1;
2694 case tsk_excessive_parms
:
2695 case tsk_insufficient_parms
:
2696 if (tsk
== tsk_excessive_parms
)
2697 error ("too many template parameter lists in declaration of %qD",
2699 else if (template_header_count
)
2700 error("too few template parameter lists in declaration of %qD", decl
);
2702 error("explicit specialization of %qD must be introduced by "
2703 "%<template <>%>", decl
);
2708 error ("explicit specialization declared %<concept%>");
2710 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2711 /* In cases like template<> constexpr bool v = true;
2712 We'll give an error in check_template_variable. */
2715 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2717 member_specialization
= 1;
2723 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2725 /* This case handles bogus declarations like template <>
2726 template <class T> void f<int>(); */
2728 if (!uses_template_parms (declarator
))
2729 error ("template-id %qD in declaration of primary template",
2731 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2733 /* Partial specialization of variable template. */
2734 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2738 else if (cxx_dialect
< cxx14
)
2739 error ("non-type partial specialization %qD "
2740 "is not allowed", declarator
);
2742 error ("non-class, non-variable partial specialization %qD "
2743 "is not allowed", declarator
);
2748 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2749 /* This is a specialization of a member template, without
2750 specialization the containing class. Something like:
2752 template <class T> struct S {
2753 template <class U> void f (U);
2755 template <> template <class U> void S<int>::f(U) {}
2757 That's a specialization -- but of the entire template. */
2765 if ((specialization
|| member_specialization
)
2766 /* This doesn't apply to variable templates. */
2767 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2768 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2770 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2771 for (; t
; t
= TREE_CHAIN (t
))
2772 if (TREE_PURPOSE (t
))
2774 permerror (input_location
,
2775 "default argument specified in explicit specialization");
2780 if (specialization
|| member_specialization
|| explicit_instantiation
)
2782 tree tmpl
= NULL_TREE
;
2783 tree targs
= NULL_TREE
;
2784 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2786 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2787 if (!was_template_id
)
2791 gcc_assert (identifier_p (declarator
));
2796 /* If there is no class context, the explicit instantiation
2797 must be at namespace scope. */
2798 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2800 /* Find the namespace binding, using the declaration
2802 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2804 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2806 error ("%qD is not a template function", dname
);
2807 fns
= error_mark_node
;
2811 declarator
= lookup_template_function (fns
, NULL_TREE
);
2814 if (declarator
== error_mark_node
)
2815 return error_mark_node
;
2817 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2819 if (!explicit_instantiation
)
2820 /* A specialization in class scope. This is invalid,
2821 but the error will already have been flagged by
2822 check_specialization_scope. */
2823 return error_mark_node
;
2826 /* It's not valid to write an explicit instantiation in
2829 class C { template void f(); }
2831 This case is caught by the parser. However, on
2834 template class C { void f(); };
2836 (which is invalid) we can get here. The error will be
2843 else if (ctype
!= NULL_TREE
2844 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2846 // We'll match variable templates in start_decl.
2850 /* Find the list of functions in ctype that have the same
2851 name as the declared function. */
2852 tree name
= TREE_OPERAND (declarator
, 0);
2853 tree fns
= NULL_TREE
;
2856 if (constructor_name_p (name
, ctype
))
2858 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2860 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2861 : !CLASSTYPE_DESTRUCTORS (ctype
))
2863 /* From [temp.expl.spec]:
2865 If such an explicit specialization for the member
2866 of a class template names an implicitly-declared
2867 special member function (clause _special_), the
2868 program is ill-formed.
2870 Similar language is found in [temp.explicit]. */
2871 error ("specialization of implicitly-declared special member function");
2872 return error_mark_node
;
2875 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2878 if (!DECL_CONV_FN_P (decl
))
2880 idx
= lookup_fnfields_1 (ctype
, name
);
2882 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2886 vec
<tree
, va_gc
> *methods
;
2889 /* For a type-conversion operator, we cannot do a
2890 name-based lookup. We might be looking for `operator
2891 int' which will be a specialization of `operator T'.
2892 So, we find *all* the conversion operators, and then
2893 select from them. */
2896 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2898 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2899 methods
->iterate (idx
, &ovl
);
2902 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2903 /* There are no more conversion functions. */
2906 /* Glue all these conversion functions together
2907 with those we already have. */
2908 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2909 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2913 if (fns
== NULL_TREE
)
2915 error ("no member function %qD declared in %qT", name
, ctype
);
2916 return error_mark_node
;
2919 TREE_OPERAND (declarator
, 0) = fns
;
2922 /* Figure out what exactly is being specialized at this point.
2923 Note that for an explicit instantiation, even one for a
2924 member function, we cannot tell apriori whether the
2925 instantiation is for a member template, or just a member
2926 function of a template class. Even if a member template is
2927 being instantiated, the member template arguments may be
2928 elided if they can be deduced from the rest of the
2930 tmpl
= determine_specialization (declarator
, decl
,
2932 member_specialization
,
2936 if (!tmpl
|| tmpl
== error_mark_node
)
2937 /* We couldn't figure out what this declaration was
2939 return error_mark_node
;
2942 if (!ctype
&& !was_template_id
2943 && (specialization
|| member_specialization
2944 || explicit_instantiation
)
2945 && !is_associated_namespace (CP_DECL_CONTEXT (decl
),
2946 CP_DECL_CONTEXT (tmpl
)))
2947 error ("%qD is not declared in %qD",
2948 tmpl
, current_namespace
);
2950 tree gen_tmpl
= most_general_template (tmpl
);
2952 if (explicit_instantiation
)
2954 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2955 is done by do_decl_instantiation later. */
2957 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2958 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2960 if (arg_depth
> parm_depth
)
2962 /* If TMPL is not the most general template (for
2963 example, if TMPL is a friend template that is
2964 injected into namespace scope), then there will
2965 be too many levels of TARGS. Remove some of them
2970 new_targs
= make_tree_vec (parm_depth
);
2971 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2972 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2973 = TREE_VEC_ELT (targs
, i
);
2977 return instantiate_template (tmpl
, targs
, tf_error
);
2980 /* If we thought that the DECL was a member function, but it
2981 turns out to be specializing a static member function,
2982 make DECL a static member function as well. */
2983 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2984 && DECL_STATIC_FUNCTION_P (tmpl
)
2985 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2986 revert_static_member_fn (decl
);
2988 /* If this is a specialization of a member template of a
2989 template class, we want to return the TEMPLATE_DECL, not
2990 the specialization of it. */
2991 if (tsk
== tsk_template
&& !was_template_id
)
2993 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2994 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2995 DECL_INITIAL (result
) = NULL_TREE
;
2999 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3000 DECL_SOURCE_LOCATION (result
)
3001 = DECL_SOURCE_LOCATION (decl
);
3002 /* We want to use the argument list specified in the
3003 definition, not in the original declaration. */
3004 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3005 for (parm
= DECL_ARGUMENTS (result
); parm
;
3006 parm
= DECL_CHAIN (parm
))
3007 DECL_CONTEXT (parm
) = result
;
3009 return register_specialization (tmpl
, gen_tmpl
, targs
,
3013 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3014 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3016 if (was_template_id
)
3017 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3019 /* Inherit default function arguments from the template
3020 DECL is specializing. */
3021 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3022 copy_default_args_to_explicit_spec (decl
);
3024 /* This specialization has the same protection as the
3025 template it specializes. */
3026 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3027 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3029 /* 7.1.1-1 [dcl.stc]
3031 A storage-class-specifier shall not be specified in an
3032 explicit specialization...
3034 The parser rejects these, so unless action is taken here,
3035 explicit function specializations will always appear with
3038 The action recommended by the C++ CWG in response to C++
3039 defect report 605 is to make the storage class and linkage
3040 of the explicit specialization match the templated function:
3042 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3044 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3046 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3047 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3049 /* A concept cannot be specialized. */
3050 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3052 error ("explicit specialization of function concept %qD",
3054 return error_mark_node
;
3057 /* This specialization has the same linkage and visibility as
3058 the function template it specializes. */
3059 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3060 if (! TREE_PUBLIC (decl
))
3062 DECL_INTERFACE_KNOWN (decl
) = 1;
3063 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3065 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3066 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3068 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3069 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3073 /* If DECL is a friend declaration, declared using an
3074 unqualified name, the namespace associated with DECL may
3075 have been set incorrectly. For example, in:
3077 template <typename T> void f(T);
3079 struct S { friend void f<int>(int); }
3082 we will have set the DECL_CONTEXT for the friend
3083 declaration to N, rather than to the global namespace. */
3084 if (DECL_NAMESPACE_SCOPE_P (decl
))
3085 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3087 if (is_friend
&& !have_def
)
3088 /* This is not really a declaration of a specialization.
3089 It's just the name of an instantiation. But, it's not
3090 a request for an instantiation, either. */
3091 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3092 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3093 /* A specialization is not necessarily COMDAT. */
3094 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3095 && DECL_DECLARED_INLINE_P (decl
));
3096 else if (VAR_P (decl
))
3097 DECL_COMDAT (decl
) = false;
3099 /* If this is a full specialization, register it so that we can find
3100 it again. Partial specializations will be registered in
3101 process_partial_specialization. */
3102 if (!processing_template_decl
)
3103 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3106 /* A 'structor should already have clones. */
3107 gcc_assert (decl
== error_mark_node
3108 || variable_template_p (tmpl
)
3109 || !(DECL_CONSTRUCTOR_P (decl
)
3110 || DECL_DESTRUCTOR_P (decl
))
3111 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3118 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3119 parameters. These are represented in the same format used for
3120 DECL_TEMPLATE_PARMS. */
3123 comp_template_parms (const_tree parms1
, const_tree parms2
)
3128 if (parms1
== parms2
)
3131 for (p1
= parms1
, p2
= parms2
;
3132 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3133 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3135 tree t1
= TREE_VALUE (p1
);
3136 tree t2
= TREE_VALUE (p2
);
3139 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3140 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3142 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3145 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3147 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3148 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3150 /* If either of the template parameters are invalid, assume
3151 they match for the sake of error recovery. */
3152 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3155 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3158 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3159 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3160 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3162 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3167 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3168 /* One set of parameters has more parameters lists than the
3175 /* Determine whether PARM is a parameter pack. */
3178 template_parameter_pack_p (const_tree parm
)
3180 /* Determine if we have a non-type template parameter pack. */
3181 if (TREE_CODE (parm
) == PARM_DECL
)
3182 return (DECL_TEMPLATE_PARM_P (parm
)
3183 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3184 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3185 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3187 /* If this is a list of template parameters, we could get a
3188 TYPE_DECL or a TEMPLATE_DECL. */
3189 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3190 parm
= TREE_TYPE (parm
);
3192 /* Otherwise it must be a type template parameter. */
3193 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3194 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3195 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3198 /* Determine if T is a function parameter pack. */
3201 function_parameter_pack_p (const_tree t
)
3203 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3204 return DECL_PACK_P (t
);
3208 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3209 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3212 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3214 if (! primary_func_tmpl_inst
3215 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3216 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3219 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3222 /* Return true iff the function parameter PARAM_DECL was expanded
3223 from the function parameter pack PACK. */
3226 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3228 if (DECL_ARTIFICIAL (param_decl
)
3229 || !function_parameter_pack_p (pack
))
3232 /* The parameter pack and its pack arguments have the same
3234 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3237 /* Determine whether ARGS describes a variadic template args list,
3238 i.e., one that is terminated by a template argument pack. */
3241 template_args_variadic_p (tree args
)
3246 if (args
== NULL_TREE
)
3249 args
= INNERMOST_TEMPLATE_ARGS (args
);
3250 nargs
= TREE_VEC_LENGTH (args
);
3255 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3257 return ARGUMENT_PACK_P (last_parm
);
3260 /* Generate a new name for the parameter pack name NAME (an
3261 IDENTIFIER_NODE) that incorporates its */
3264 make_ith_pack_parameter_name (tree name
, int i
)
3266 /* Munge the name to include the parameter index. */
3267 #define NUMBUF_LEN 128
3268 char numbuf
[NUMBUF_LEN
];
3272 if (name
== NULL_TREE
)
3274 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3275 newname_len
= IDENTIFIER_LENGTH (name
)
3276 + strlen (numbuf
) + 2;
3277 newname
= (char*)alloca (newname_len
);
3278 snprintf (newname
, newname_len
,
3279 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3280 return get_identifier (newname
);
3283 /* Return true if T is a primary function, class or alias template
3287 primary_template_instantiation_p (const_tree t
)
3292 if (TREE_CODE (t
) == FUNCTION_DECL
)
3293 return DECL_LANG_SPECIFIC (t
)
3294 && DECL_TEMPLATE_INSTANTIATION (t
)
3295 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3296 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3297 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3299 else if (alias_template_specialization_p (t
))
3304 /* Return true if PARM is a template template parameter. */
3307 template_template_parameter_p (const_tree parm
)
3309 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3312 /* Return true iff PARM is a DECL representing a type template
3316 template_type_parameter_p (const_tree parm
)
3319 && (TREE_CODE (parm
) == TYPE_DECL
3320 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3321 && DECL_TEMPLATE_PARM_P (parm
));
3324 /* Return the template parameters of T if T is a
3325 primary template instantiation, NULL otherwise. */
3328 get_primary_template_innermost_parameters (const_tree t
)
3330 tree parms
= NULL
, template_info
= NULL
;
3332 if ((template_info
= get_template_info (t
))
3333 && primary_template_instantiation_p (t
))
3334 parms
= INNERMOST_TEMPLATE_PARMS
3335 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3340 /* Return the template parameters of the LEVELth level from the full list
3341 of template parameters PARMS. */
3344 get_template_parms_at_level (tree parms
, int level
)
3348 || TREE_CODE (parms
) != TREE_LIST
3349 || level
> TMPL_PARMS_DEPTH (parms
))
3352 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3353 if (TMPL_PARMS_DEPTH (p
) == level
)
3359 /* Returns the template arguments of T if T is a template instantiation,
3363 get_template_innermost_arguments (const_tree t
)
3365 tree args
= NULL
, template_info
= NULL
;
3367 if ((template_info
= get_template_info (t
))
3368 && TI_ARGS (template_info
))
3369 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3374 /* Return the argument pack elements of T if T is a template argument pack,
3378 get_template_argument_pack_elems (const_tree t
)
3380 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3381 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3384 return ARGUMENT_PACK_ARGS (t
);
3387 /* Structure used to track the progress of find_parameter_packs_r. */
3388 struct find_parameter_pack_data
3390 /* TREE_LIST that will contain all of the parameter packs found by
3392 tree
* parameter_packs
;
3394 /* Set of AST nodes that have been visited by the traversal. */
3395 hash_set
<tree
> *visited
;
3397 /* True iff we're making a type pack expansion. */
3398 bool type_pack_expansion_p
;
3401 /* Identifies all of the argument packs that occur in a template
3402 argument and appends them to the TREE_LIST inside DATA, which is a
3403 find_parameter_pack_data structure. This is a subroutine of
3404 make_pack_expansion and uses_parameter_packs. */
3406 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3409 struct find_parameter_pack_data
* ppd
=
3410 (struct find_parameter_pack_data
*)data
;
3411 bool parameter_pack_p
= false;
3413 /* Handle type aliases/typedefs. */
3414 if (TYPE_ALIAS_P (t
))
3416 if (TYPE_TEMPLATE_INFO (t
))
3417 cp_walk_tree (&TYPE_TI_ARGS (t
),
3418 &find_parameter_packs_r
,
3424 /* Identify whether this is a parameter pack or not. */
3425 switch (TREE_CODE (t
))
3427 case TEMPLATE_PARM_INDEX
:
3428 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3429 parameter_pack_p
= true;
3432 case TEMPLATE_TYPE_PARM
:
3433 t
= TYPE_MAIN_VARIANT (t
);
3434 case TEMPLATE_TEMPLATE_PARM
:
3435 /* If the placeholder appears in the decl-specifier-seq of a function
3436 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3437 is a pack expansion, the invented template parameter is a template
3439 if (ppd
->type_pack_expansion_p
&& is_auto_or_concept (t
))
3440 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3441 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3442 parameter_pack_p
= true;
3447 if (DECL_PACK_P (t
))
3449 /* We don't want to walk into the type of a PARM_DECL,
3450 because we don't want to see the type parameter pack. */
3452 parameter_pack_p
= true;
3456 /* Look through a lambda capture proxy to the field pack. */
3458 if (DECL_HAS_VALUE_EXPR_P (t
))
3460 tree v
= DECL_VALUE_EXPR (t
);
3462 &find_parameter_packs_r
,
3466 else if (variable_template_specialization_p (t
))
3468 cp_walk_tree (&DECL_TI_ARGS (t
),
3469 find_parameter_packs_r
,
3476 parameter_pack_p
= true;
3479 /* Not a parameter pack. */
3483 if (parameter_pack_p
)
3485 /* Add this parameter pack to the list. */
3486 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3490 cp_walk_tree (&TYPE_CONTEXT (t
),
3491 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3493 /* This switch statement will return immediately if we don't find a
3495 switch (TREE_CODE (t
))
3497 case TEMPLATE_PARM_INDEX
:
3500 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3501 /* Check the template itself. */
3502 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3503 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3504 /* Check the template arguments. */
3505 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3510 case TEMPLATE_TYPE_PARM
:
3511 case TEMPLATE_TEMPLATE_PARM
:
3518 if (TYPE_PTRMEMFUNC_P (t
))
3524 if (TYPE_TEMPLATE_INFO (t
))
3525 cp_walk_tree (&TYPE_TI_ARGS (t
),
3526 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3533 cp_walk_tree (&TREE_TYPE (t
),
3534 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3538 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3543 case TYPE_PACK_EXPANSION
:
3544 case EXPR_PACK_EXPANSION
:
3549 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3554 case IDENTIFIER_NODE
:
3555 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3562 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3563 type_pack_expansion_p to false so that any placeholders
3564 within the expression don't get marked as parameter packs. */
3565 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3566 ppd
->type_pack_expansion_p
= false;
3567 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3569 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3581 /* Determines if the expression or type T uses any parameter packs. */
3583 uses_parameter_packs (tree t
)
3585 tree parameter_packs
= NULL_TREE
;
3586 struct find_parameter_pack_data ppd
;
3587 ppd
.parameter_packs
= ¶meter_packs
;
3588 ppd
.visited
= new hash_set
<tree
>;
3589 ppd
.type_pack_expansion_p
= false;
3590 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3592 return parameter_packs
!= NULL_TREE
;
3595 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3596 representation a base-class initializer into a parameter pack
3597 expansion. If all goes well, the resulting node will be an
3598 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3601 make_pack_expansion (tree arg
)
3604 tree parameter_packs
= NULL_TREE
;
3605 bool for_types
= false;
3606 struct find_parameter_pack_data ppd
;
3608 if (!arg
|| arg
== error_mark_node
)
3611 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3613 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3614 class initializer. In this case, the TREE_PURPOSE will be a
3615 _TYPE node (representing the base class expansion we're
3616 initializing) and the TREE_VALUE will be a TREE_LIST
3617 containing the initialization arguments.
3619 The resulting expansion looks somewhat different from most
3620 expansions. Rather than returning just one _EXPANSION, we
3621 return a TREE_LIST whose TREE_PURPOSE is a
3622 TYPE_PACK_EXPANSION containing the bases that will be
3623 initialized. The TREE_VALUE will be identical to the
3624 original TREE_VALUE, which is a list of arguments that will
3625 be passed to each base. We do not introduce any new pack
3626 expansion nodes into the TREE_VALUE (although it is possible
3627 that some already exist), because the TREE_PURPOSE and
3628 TREE_VALUE all need to be expanded together with the same
3629 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3630 resulting TREE_PURPOSE will mention the parameter packs in
3631 both the bases and the arguments to the bases. */
3634 tree parameter_packs
= NULL_TREE
;
3636 /* Determine which parameter packs will be used by the base
3638 ppd
.visited
= new hash_set
<tree
>;
3639 ppd
.parameter_packs
= ¶meter_packs
;
3640 ppd
.type_pack_expansion_p
= true;
3641 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3642 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3645 if (parameter_packs
== NULL_TREE
)
3647 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3649 return error_mark_node
;
3652 if (TREE_VALUE (arg
) != void_type_node
)
3654 /* Collect the sets of parameter packs used in each of the
3655 initialization arguments. */
3656 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3658 /* Determine which parameter packs will be expanded in this
3660 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3667 /* Create the pack expansion type for the base type. */
3668 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3669 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3670 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3672 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3673 they will rarely be compared to anything. */
3674 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3676 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3679 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3682 /* Build the PACK_EXPANSION_* node. */
3684 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3685 : make_node (EXPR_PACK_EXPANSION
);
3686 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3687 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3689 /* Propagate type and const-expression information. */
3690 TREE_TYPE (result
) = TREE_TYPE (arg
);
3691 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3694 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3695 they will rarely be compared to anything. */
3696 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3698 /* Determine which parameter packs will be expanded. */
3699 ppd
.parameter_packs
= ¶meter_packs
;
3700 ppd
.visited
= new hash_set
<tree
>;
3701 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3702 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3705 /* Make sure we found some parameter packs. */
3706 if (parameter_packs
== NULL_TREE
)
3709 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3711 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3712 return error_mark_node
;
3714 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3716 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3721 /* Checks T for any "bare" parameter packs, which have not yet been
3722 expanded, and issues an error if any are found. This operation can
3723 only be done on full expressions or types (e.g., an expression
3724 statement, "if" condition, etc.), because we could have expressions like:
3726 foo(f(g(h(args)))...)
3728 where "args" is a parameter pack. check_for_bare_parameter_packs
3729 should not be called for the subexpressions args, h(args),
3730 g(h(args)), or f(g(h(args))), because we would produce erroneous
3733 Returns TRUE and emits an error if there were bare parameter packs,
3734 returns FALSE otherwise. */
3736 check_for_bare_parameter_packs (tree t
)
3738 tree parameter_packs
= NULL_TREE
;
3739 struct find_parameter_pack_data ppd
;
3741 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3744 if (TREE_CODE (t
) == TYPE_DECL
)
3747 ppd
.parameter_packs
= ¶meter_packs
;
3748 ppd
.visited
= new hash_set
<tree
>;
3749 ppd
.type_pack_expansion_p
= false;
3750 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3753 if (parameter_packs
)
3755 error ("parameter packs not expanded with %<...%>:");
3756 while (parameter_packs
)
3758 tree pack
= TREE_VALUE (parameter_packs
);
3759 tree name
= NULL_TREE
;
3761 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3762 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3763 name
= TYPE_NAME (pack
);
3764 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3765 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3767 name
= DECL_NAME (pack
);
3770 inform (input_location
, " %qD", name
);
3772 inform (input_location
, " <anonymous>");
3774 parameter_packs
= TREE_CHAIN (parameter_packs
);
3783 /* Expand any parameter packs that occur in the template arguments in
3786 expand_template_argument_pack (tree args
)
3788 tree result_args
= NULL_TREE
;
3789 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3790 int num_result_args
= -1;
3791 int non_default_args_count
= -1;
3793 /* First, determine if we need to expand anything, and the number of
3794 slots we'll need. */
3795 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3797 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3798 if (arg
== NULL_TREE
)
3800 if (ARGUMENT_PACK_P (arg
))
3802 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3803 if (num_result_args
< 0)
3804 num_result_args
= in_arg
+ num_packed
;
3806 num_result_args
+= num_packed
;
3810 if (num_result_args
>= 0)
3815 /* If no expansion is necessary, we're done. */
3816 if (num_result_args
< 0)
3819 /* Expand arguments. */
3820 result_args
= make_tree_vec (num_result_args
);
3821 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3822 non_default_args_count
=
3823 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3824 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3826 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3827 if (ARGUMENT_PACK_P (arg
))
3829 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3830 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3831 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3832 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3833 if (non_default_args_count
> 0)
3834 non_default_args_count
+= num_packed
- 1;
3838 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3842 if (non_default_args_count
>= 0)
3843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3847 /* Checks if DECL shadows a template parameter.
3849 [temp.local]: A template-parameter shall not be redeclared within its
3850 scope (including nested scopes).
3852 Emits an error and returns TRUE if the DECL shadows a parameter,
3853 returns FALSE otherwise. */
3856 check_template_shadow (tree decl
)
3860 /* If we're not in a template, we can't possibly shadow a template
3862 if (!current_template_parms
)
3865 /* Figure out what we're shadowing. */
3866 if (TREE_CODE (decl
) == OVERLOAD
)
3867 decl
= OVL_CURRENT (decl
);
3868 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3870 /* If there's no previous binding for this name, we're not shadowing
3871 anything, let alone a template parameter. */
3875 /* If we're not shadowing a template parameter, we're done. Note
3876 that OLDDECL might be an OVERLOAD (or perhaps even an
3877 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3879 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3882 /* We check for decl != olddecl to avoid bogus errors for using a
3883 name inside a class. We check TPFI to avoid duplicate errors for
3884 inline member templates. */
3886 || (DECL_TEMPLATE_PARM_P (decl
)
3887 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3890 /* Don't complain about the injected class name, as we've already
3891 complained about the class itself. */
3892 if (DECL_SELF_REFERENCE_P (decl
))
3895 if (DECL_TEMPLATE_PARM_P (decl
))
3896 error ("declaration of template parameter %q+D shadows "
3897 "template parameter", decl
);
3899 error ("declaration of %q+#D shadows template parameter", decl
);
3900 inform (DECL_SOURCE_LOCATION (olddecl
),
3901 "template parameter %qD declared here", olddecl
);
3905 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3906 ORIG_LEVEL, DECL, and TYPE. */
3909 build_template_parm_index (int index
,
3915 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3916 TEMPLATE_PARM_IDX (t
) = index
;
3917 TEMPLATE_PARM_LEVEL (t
) = level
;
3918 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3919 TEMPLATE_PARM_DECL (t
) = decl
;
3920 TREE_TYPE (t
) = type
;
3921 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3922 TREE_READONLY (t
) = TREE_READONLY (decl
);
3927 /* Find the canonical type parameter for the given template type
3928 parameter. Returns the canonical type parameter, which may be TYPE
3929 if no such parameter existed. */
3932 canonical_type_parameter (tree type
)
3935 int idx
= TEMPLATE_TYPE_IDX (type
);
3936 if (!canonical_template_parms
)
3937 vec_alloc (canonical_template_parms
, idx
+1);
3939 while (canonical_template_parms
->length () <= (unsigned)idx
)
3940 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3942 list
= (*canonical_template_parms
)[idx
];
3943 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3944 list
= TREE_CHAIN (list
);
3947 return TREE_VALUE (list
);
3950 (*canonical_template_parms
)[idx
]
3951 = tree_cons (NULL_TREE
, type
,
3952 (*canonical_template_parms
)[idx
]);
3957 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3958 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3959 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3960 new one is created. */
3963 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3964 tsubst_flags_t complain
)
3966 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3967 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3968 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3969 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3971 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3974 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3975 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3976 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3977 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3978 DECL_ARTIFICIAL (decl
) = 1;
3979 SET_DECL_TEMPLATE_PARM_P (decl
);
3981 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3982 TEMPLATE_PARM_LEVEL (index
) - levels
,
3983 TEMPLATE_PARM_ORIG_LEVEL (index
),
3985 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3986 TEMPLATE_PARM_PARAMETER_PACK (t
)
3987 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3989 /* Template template parameters need this. */
3990 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3992 DECL_TEMPLATE_RESULT (decl
)
3993 = build_decl (DECL_SOURCE_LOCATION (decl
),
3994 TYPE_DECL
, DECL_NAME (decl
), type
);
3995 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
3996 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3997 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4001 return TEMPLATE_PARM_DESCENDANTS (index
);
4004 /* Process information from new template parameter PARM and append it
4005 to the LIST being built. This new parameter is a non-type
4006 parameter iff IS_NON_TYPE is true. This new parameter is a
4007 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4011 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4012 bool is_non_type
, bool is_parameter_pack
)
4017 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4018 tree defval
= TREE_PURPOSE (parm
);
4019 tree constr
= TREE_TYPE (parm
);
4023 tree p
= tree_last (list
);
4025 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4028 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4029 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4031 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4039 parm
= TREE_VALUE (parm
);
4041 SET_DECL_TEMPLATE_PARM_P (parm
);
4043 if (TREE_TYPE (parm
) != error_mark_node
)
4047 The top-level cv-qualifiers on the template-parameter are
4048 ignored when determining its type. */
4049 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4050 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4051 TREE_TYPE (parm
) = error_mark_node
;
4052 else if (uses_parameter_packs (TREE_TYPE (parm
))
4053 && !is_parameter_pack
4054 /* If we're in a nested template parameter list, the template
4055 template parameter could be a parameter pack. */
4056 && processing_template_parmlist
== 1)
4058 /* This template parameter is not a parameter pack, but it
4059 should be. Complain about "bare" parameter packs. */
4060 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4062 /* Recover by calling this a parameter pack. */
4063 is_parameter_pack
= true;
4067 /* A template parameter is not modifiable. */
4068 TREE_CONSTANT (parm
) = 1;
4069 TREE_READONLY (parm
) = 1;
4070 decl
= build_decl (parm_loc
,
4071 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4072 TREE_CONSTANT (decl
) = 1;
4073 TREE_READONLY (decl
) = 1;
4074 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4075 = build_template_parm_index (idx
, processing_template_decl
,
4076 processing_template_decl
,
4077 decl
, TREE_TYPE (parm
));
4079 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4080 = is_parameter_pack
;
4085 parm
= TREE_VALUE (TREE_VALUE (parm
));
4087 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4089 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4090 /* This is for distinguishing between real templates and template
4091 template parameters */
4092 TREE_TYPE (parm
) = t
;
4093 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4098 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4099 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4100 decl
= build_decl (parm_loc
,
4101 TYPE_DECL
, parm
, t
);
4104 TYPE_NAME (t
) = decl
;
4105 TYPE_STUB_DECL (t
) = decl
;
4107 TEMPLATE_TYPE_PARM_INDEX (t
)
4108 = build_template_parm_index (idx
, processing_template_decl
,
4109 processing_template_decl
,
4110 decl
, TREE_TYPE (parm
));
4111 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4112 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4114 DECL_ARTIFICIAL (decl
) = 1;
4115 SET_DECL_TEMPLATE_PARM_P (decl
);
4117 /* Build requirements for the type/template parameter.
4118 This must be done after SET_DECL_TEMPLATE_PARM_P or
4119 process_template_parm could fail. */
4120 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4124 /* Build the parameter node linking the parameter declaration,
4125 its default argument (if any), and its constraints (if any). */
4126 parm
= build_tree_list (defval
, parm
);
4127 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4129 return chainon (list
, parm
);
4132 /* The end of a template parameter list has been reached. Process the
4133 tree list into a parameter vector, converting each parameter into a more
4134 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4138 end_template_parm_list (tree parms
)
4142 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4144 /* Pop the dummy parameter level and add the real one. */
4145 current_template_parms
= TREE_CHAIN (current_template_parms
);
4147 current_template_parms
4148 = tree_cons (size_int (processing_template_decl
),
4149 saved_parmlist
, current_template_parms
);
4151 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4153 next
= TREE_CHAIN (parm
);
4154 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4155 TREE_CHAIN (parm
) = NULL_TREE
;
4158 --processing_template_parmlist
;
4160 return saved_parmlist
;
4163 // Explicitly indicate the end of the template parameter list. We assume
4164 // that the current template parameters have been constructed and/or
4165 // managed explicitly, as when creating new template template parameters
4166 // from a shorthand constraint.
4168 end_template_parm_list ()
4170 --processing_template_parmlist
;
4173 /* end_template_decl is called after a template declaration is seen. */
4176 end_template_decl (void)
4178 reset_specialization ();
4180 if (! processing_template_decl
)
4183 /* This matches the pushlevel in begin_template_parm_list. */
4186 --processing_template_decl
;
4187 current_template_parms
= TREE_CHAIN (current_template_parms
);
4190 /* Takes a TREE_LIST representing a template parameter and convert it
4191 into an argument suitable to be passed to the type substitution
4192 functions. Note that If the TREE_LIST contains an error_mark
4193 node, the returned argument is error_mark_node. */
4196 template_parm_to_arg (tree t
)
4200 || TREE_CODE (t
) != TREE_LIST
)
4203 if (error_operand_p (TREE_VALUE (t
)))
4204 return error_mark_node
;
4208 if (TREE_CODE (t
) == TYPE_DECL
4209 || TREE_CODE (t
) == TEMPLATE_DECL
)
4213 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4215 /* Turn this argument into a TYPE_ARGUMENT_PACK
4216 with a single element, which expands T. */
4217 tree vec
= make_tree_vec (1);
4219 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4221 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4223 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4224 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4229 t
= DECL_INITIAL (t
);
4231 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4233 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4234 with a single element, which expands T. */
4235 tree vec
= make_tree_vec (1);
4236 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4238 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4240 t
= convert_from_reference (t
);
4241 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4243 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4244 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4245 TREE_TYPE (t
) = type
;
4248 t
= convert_from_reference (t
);
4253 /* Given a set of template parameters, return them as a set of template
4254 arguments. The template parameters are represented as a TREE_VEC, in
4255 the form documented in cp-tree.h for template arguments. */
4258 template_parms_to_args (tree parms
)
4261 tree args
= NULL_TREE
;
4262 int length
= TMPL_PARMS_DEPTH (parms
);
4265 /* If there is only one level of template parameters, we do not
4266 create a TREE_VEC of TREE_VECs. Instead, we return a single
4267 TREE_VEC containing the arguments. */
4269 args
= make_tree_vec (length
);
4271 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4273 tree a
= copy_node (TREE_VALUE (header
));
4276 TREE_TYPE (a
) = NULL_TREE
;
4277 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4278 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4281 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4284 TREE_VEC_ELT (args
, --l
) = a
;
4292 /* Within the declaration of a template, return the currently active
4293 template parameters as an argument TREE_VEC. */
4296 current_template_args (void)
4298 return template_parms_to_args (current_template_parms
);
4301 /* Update the declared TYPE by doing any lookups which were thought to be
4302 dependent, but are not now that we know the SCOPE of the declarator. */
4305 maybe_update_decl_type (tree orig_type
, tree scope
)
4307 tree type
= orig_type
;
4309 if (type
== NULL_TREE
)
4312 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4313 type
= TREE_TYPE (type
);
4315 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4316 && dependent_type_p (type
)
4317 /* Don't bother building up the args in this case. */
4318 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4320 /* tsubst in the args corresponding to the template parameters,
4321 including auto if present. Most things will be unchanged, but
4322 make_typename_type and tsubst_qualified_id will resolve
4323 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4324 tree args
= current_template_args ();
4325 tree auto_node
= type_uses_auto (type
);
4329 tree auto_vec
= make_tree_vec (1);
4330 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4331 args
= add_to_template_args (args
, auto_vec
);
4333 pushed
= push_scope (scope
);
4334 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4339 if (type
== error_mark_node
)
4342 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4344 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4347 type
= TYPE_NAME (type
);
4352 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4353 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4354 the new template is a member template. */
4357 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4359 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4360 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4361 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4362 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4363 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4368 struct template_parm_data
4370 /* The level of the template parameters we are currently
4374 /* The index of the specialization argument we are currently
4378 /* An array whose size is the number of template parameters. The
4379 elements are nonzero if the parameter has been used in any one
4380 of the arguments processed so far. */
4383 /* An array whose size is the number of template arguments. The
4384 elements are nonzero if the argument makes use of template
4385 parameters of this level. */
4386 int* arg_uses_template_parms
;
4389 /* Subroutine of push_template_decl used to see if each template
4390 parameter in a partial specialization is used in the explicit
4391 argument list. If T is of the LEVEL given in DATA (which is
4392 treated as a template_parm_data*), then DATA->PARMS is marked
4396 mark_template_parm (tree t
, void* data
)
4400 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4402 template_parm_level_and_index (t
, &level
, &idx
);
4404 if (level
== tpd
->level
)
4406 tpd
->parms
[idx
] = 1;
4407 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4410 /* Return zero so that for_each_template_parm will continue the
4411 traversal of the tree; we want to mark *every* template parm. */
4415 /* Process the partial specialization DECL. */
4418 process_partial_specialization (tree decl
)
4420 tree type
= TREE_TYPE (decl
);
4421 tree tinfo
= get_template_info (decl
);
4422 tree maintmpl
= TI_TEMPLATE (tinfo
);
4423 tree specargs
= TI_ARGS (tinfo
);
4424 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4425 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4428 int nargs
= TREE_VEC_LENGTH (inner_args
);
4431 bool did_error_intro
= false;
4432 struct template_parm_data tpd
;
4433 struct template_parm_data tpd2
;
4435 gcc_assert (current_template_parms
);
4437 /* A concept cannot be specialized. */
4438 if (flag_concepts
&& variable_concept_p (maintmpl
))
4440 error ("specialization of variable concept %q#D", maintmpl
);
4441 return error_mark_node
;
4444 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4445 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4447 /* We check that each of the template parameters given in the
4448 partial specialization is used in the argument list to the
4449 specialization. For example:
4451 template <class T> struct S;
4452 template <class T> struct S<T*>;
4454 The second declaration is OK because `T*' uses the template
4455 parameter T, whereas
4457 template <class T> struct S<int>;
4459 is no good. Even trickier is:
4470 The S2<T> declaration is actually invalid; it is a
4471 full-specialization. Of course,
4474 struct S2<T (*)(U)>;
4476 or some such would have been OK. */
4477 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4478 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4479 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4481 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4482 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4483 for (i
= 0; i
< nargs
; ++i
)
4485 tpd
.current_arg
= i
;
4486 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4487 &mark_template_parm
,
4490 /*include_nondeduced_p=*/false);
4492 for (i
= 0; i
< ntparms
; ++i
)
4493 if (tpd
.parms
[i
] == 0)
4495 /* One of the template parms was not used in a deduced context in the
4497 if (!did_error_intro
)
4499 error ("template parameters not deducible in "
4500 "partial specialization:");
4501 did_error_intro
= true;
4504 inform (input_location
, " %qD",
4505 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4508 if (did_error_intro
)
4509 return error_mark_node
;
4511 /* [temp.class.spec]
4513 The argument list of the specialization shall not be identical to
4514 the implicit argument list of the primary template. */
4516 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4517 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4519 || !strictly_subsumes (current_template_constraints (),
4520 get_constraints (maintmpl
))))
4523 error ("partial specialization %q+D does not specialize "
4524 "any template arguments", decl
);
4526 error ("partial specialization %q+D does not specialize any "
4527 "template arguments and is not more constrained than", decl
);
4528 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4531 /* A partial specialization that replaces multiple parameters of the
4532 primary template with a pack expansion is less specialized for those
4534 if (nargs
< DECL_NTPARMS (maintmpl
))
4536 error ("partial specialization is not more specialized than the "
4537 "primary template because it replaces multiple parameters "
4538 "with a pack expansion");
4539 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4543 /* [temp.class.spec]
4545 A partially specialized non-type argument expression shall not
4546 involve template parameters of the partial specialization except
4547 when the argument expression is a simple identifier.
4549 The type of a template parameter corresponding to a specialized
4550 non-type argument shall not be dependent on a parameter of the
4553 Also, we verify that pack expansions only occur at the
4554 end of the argument list. */
4555 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4557 for (i
= 0; i
< nargs
; ++i
)
4559 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4560 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4561 tree packed_args
= NULL_TREE
;
4564 if (ARGUMENT_PACK_P (arg
))
4566 /* Extract the arguments from the argument pack. We'll be
4567 iterating over these in the following loop. */
4568 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4569 len
= TREE_VEC_LENGTH (packed_args
);
4572 for (j
= 0; j
< len
; j
++)
4575 /* Get the Jth argument in the parameter pack. */
4576 arg
= TREE_VEC_ELT (packed_args
, j
);
4578 if (PACK_EXPANSION_P (arg
))
4580 /* Pack expansions must come at the end of the
4582 if ((packed_args
&& j
< len
- 1)
4583 || (!packed_args
&& i
< nargs
- 1))
4585 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4586 error ("parameter pack argument %qE must be at the "
4587 "end of the template argument list", arg
);
4589 error ("parameter pack argument %qT must be at the "
4590 "end of the template argument list", arg
);
4594 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4595 /* We only care about the pattern. */
4596 arg
= PACK_EXPANSION_PATTERN (arg
);
4598 if (/* These first two lines are the `non-type' bit. */
4600 && TREE_CODE (arg
) != TEMPLATE_DECL
4601 /* This next two lines are the `argument expression is not just a
4602 simple identifier' condition and also the `specialized
4603 non-type argument' bit. */
4604 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4605 && !(REFERENCE_REF_P (arg
)
4606 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4608 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4609 || (packed_args
&& uses_template_parms (arg
)))
4610 error ("template argument %qE involves template parameter(s)",
4614 /* Look at the corresponding template parameter,
4615 marking which template parameters its type depends
4617 tree type
= TREE_TYPE (parm
);
4621 /* We haven't yet initialized TPD2. Do so now. */
4622 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4623 /* The number of parameters here is the number in the
4624 main template, which, as checked in the assertion
4626 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4628 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4631 /* Mark the template parameters. But this time, we're
4632 looking for the template parameters of the main
4633 template, not in the specialization. */
4634 tpd2
.current_arg
= i
;
4635 tpd2
.arg_uses_template_parms
[i
] = 0;
4636 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4637 for_each_template_parm (type
,
4638 &mark_template_parm
,
4641 /*include_nondeduced_p=*/false);
4643 if (tpd2
.arg_uses_template_parms
[i
])
4645 /* The type depended on some template parameters.
4646 If they are fully specialized in the
4647 specialization, that's OK. */
4650 for (j
= 0; j
< nargs
; ++j
)
4651 if (tpd2
.parms
[j
] != 0
4652 && tpd
.arg_uses_template_parms
[j
])
4655 error_n (input_location
, count
,
4656 "type %qT of template argument %qE depends "
4657 "on a template parameter",
4658 "type %qT of template argument %qE depends "
4659 "on template parameters",
4668 /* We should only get here once. */
4669 if (TREE_CODE (decl
) == TYPE_DECL
)
4670 gcc_assert (!COMPLETE_TYPE_P (type
));
4672 // Build the template decl.
4673 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4674 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4675 TREE_TYPE (tmpl
) = type
;
4676 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4677 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4678 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4679 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4682 /* We didn't register this in check_explicit_specialization so we could
4683 wait until the constraints were set. */
4684 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4686 associate_classtype_constraints (type
);
4688 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4689 = tree_cons (specargs
, tmpl
,
4690 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4691 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4693 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4694 inst
= TREE_CHAIN (inst
))
4696 tree instance
= TREE_VALUE (inst
);
4697 if (TYPE_P (instance
)
4698 ? (COMPLETE_TYPE_P (instance
)
4699 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4700 : DECL_TEMPLATE_INSTANTIATION (instance
))
4702 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4703 tree inst_decl
= (DECL_P (instance
)
4704 ? instance
: TYPE_NAME (instance
));
4707 else if (spec
== error_mark_node
)
4708 permerror (input_location
,
4709 "declaration of %qD ambiguates earlier template "
4710 "instantiation for %qD", decl
, inst_decl
);
4711 else if (TREE_VALUE (spec
) == tmpl
)
4712 permerror (input_location
,
4713 "partial specialization of %qD after instantiation "
4714 "of %qD", decl
, inst_decl
);
4721 /* PARM is a template parameter of some form; return the corresponding
4722 TEMPLATE_PARM_INDEX. */
4725 get_template_parm_index (tree parm
)
4727 if (TREE_CODE (parm
) == PARM_DECL
4728 || TREE_CODE (parm
) == CONST_DECL
)
4729 parm
= DECL_INITIAL (parm
);
4730 else if (TREE_CODE (parm
) == TYPE_DECL
4731 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4732 parm
= TREE_TYPE (parm
);
4733 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4734 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4735 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4736 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4737 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4741 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4742 parameter packs used by the template parameter PARM. */
4745 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4747 /* A type parm can't refer to another parm. */
4748 if (TREE_CODE (parm
) == TYPE_DECL
)
4750 else if (TREE_CODE (parm
) == PARM_DECL
)
4752 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4757 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4759 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4760 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4761 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4764 /* PARM is a template parameter pack. Return any parameter packs used in
4765 its type or the type of any of its template parameters. If there are
4766 any such packs, it will be instantiated into a fixed template parameter
4767 list by partial instantiation rather than be fully deduced. */
4770 fixed_parameter_pack_p (tree parm
)
4772 /* This can only be true in a member template. */
4773 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4775 /* This can only be true for a parameter pack. */
4776 if (!template_parameter_pack_p (parm
))
4778 /* A type parm can't refer to another parm. */
4779 if (TREE_CODE (parm
) == TYPE_DECL
)
4782 tree parameter_packs
= NULL_TREE
;
4783 struct find_parameter_pack_data ppd
;
4784 ppd
.parameter_packs
= ¶meter_packs
;
4785 ppd
.visited
= new hash_set
<tree
>;
4786 ppd
.type_pack_expansion_p
= false;
4788 fixed_parameter_pack_p_1 (parm
, &ppd
);
4791 return parameter_packs
;
4794 /* Check that a template declaration's use of default arguments and
4795 parameter packs is not invalid. Here, PARMS are the template
4796 parameters. IS_PRIMARY is true if DECL is the thing declared by
4797 a primary template. IS_PARTIAL is true if DECL is a partial
4800 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4801 declaration (but not a definition); 1 indicates a declaration, 2
4802 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4803 emitted for extraneous default arguments.
4805 Returns TRUE if there were no errors found, FALSE otherwise. */
4808 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4809 bool is_partial
, int is_friend_decl
)
4812 int last_level_to_check
;
4814 bool no_errors
= true;
4818 A default template-argument shall not be specified in a
4819 function template declaration or a function template definition, nor
4820 in the template-parameter-list of the definition of a member of a
4823 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4824 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4825 /* You can't have a function template declaration in a local
4826 scope, nor you can you define a member of a class template in a
4830 if ((TREE_CODE (decl
) == TYPE_DECL
4832 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4833 || (TREE_CODE (decl
) == FUNCTION_DECL
4834 && LAMBDA_FUNCTION_P (decl
)))
4835 /* A lambda doesn't have an explicit declaration; don't complain
4836 about the parms of the enclosing class. */
4839 if (current_class_type
4840 && !TYPE_BEING_DEFINED (current_class_type
)
4841 && DECL_LANG_SPECIFIC (decl
)
4842 && DECL_DECLARES_FUNCTION_P (decl
)
4843 /* If this is either a friend defined in the scope of the class
4844 or a member function. */
4845 && (DECL_FUNCTION_MEMBER_P (decl
)
4846 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4847 : DECL_FRIEND_CONTEXT (decl
)
4848 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4850 /* And, if it was a member function, it really was defined in
4851 the scope of the class. */
4852 && (!DECL_FUNCTION_MEMBER_P (decl
)
4853 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4854 /* We already checked these parameters when the template was
4855 declared, so there's no need to do it again now. This function
4856 was defined in class scope, but we're processing its body now
4857 that the class is complete. */
4860 /* Core issue 226 (C++0x only): the following only applies to class
4863 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4867 If a template-parameter has a default template-argument, all
4868 subsequent template-parameters shall have a default
4869 template-argument supplied. */
4870 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4872 tree inner_parms
= TREE_VALUE (parm_level
);
4873 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4874 int seen_def_arg_p
= 0;
4877 for (i
= 0; i
< ntparms
; ++i
)
4879 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4881 if (parm
== error_mark_node
)
4884 if (TREE_PURPOSE (parm
))
4886 else if (seen_def_arg_p
4887 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4889 error ("no default argument for %qD", TREE_VALUE (parm
));
4890 /* For better subsequent error-recovery, we indicate that
4891 there should have been a default argument. */
4892 TREE_PURPOSE (parm
) = error_mark_node
;
4895 else if (!is_partial
4897 /* Don't complain about an enclosing partial
4899 && parm_level
== parms
4900 && TREE_CODE (decl
) == TYPE_DECL
4902 && template_parameter_pack_p (TREE_VALUE (parm
))
4903 /* A fixed parameter pack will be partially
4904 instantiated into a fixed length list. */
4905 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4907 /* A primary class template can only have one
4908 parameter pack, at the end of the template
4911 error ("parameter pack %q+D must be at the end of the"
4912 " template parameter list", TREE_VALUE (parm
));
4914 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4922 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4926 /* For an ordinary class template, default template arguments are
4927 allowed at the innermost level, e.g.:
4928 template <class T = int>
4930 but, in a partial specialization, they're not allowed even
4931 there, as we have in [temp.class.spec]:
4933 The template parameter list of a specialization shall not
4934 contain default template argument values.
4936 So, for a partial specialization, or for a function template
4937 (in C++98/C++03), we look at all of them. */
4940 /* But, for a primary class template that is not a partial
4941 specialization we look at all template parameters except the
4943 parms
= TREE_CHAIN (parms
);
4945 /* Figure out what error message to issue. */
4946 if (is_friend_decl
== 2)
4947 msg
= G_("default template arguments may not be used in function template "
4948 "friend re-declaration");
4949 else if (is_friend_decl
)
4950 msg
= G_("default template arguments may not be used in function template "
4951 "friend declarations");
4952 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4953 msg
= G_("default template arguments may not be used in function templates "
4954 "without -std=c++11 or -std=gnu++11");
4955 else if (is_partial
)
4956 msg
= G_("default template arguments may not be used in "
4957 "partial specializations");
4958 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
4959 msg
= G_("default argument for template parameter for class enclosing %qD");
4961 /* Per [temp.param]/9, "A default template-argument shall not be
4962 specified in the template-parameter-lists of the definition of
4963 a member of a class template that appears outside of the member's
4964 class.", thus if we aren't handling a member of a class template
4965 there is no need to examine the parameters. */
4968 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4969 /* If we're inside a class definition, there's no need to
4970 examine the parameters to the class itself. On the one
4971 hand, they will be checked when the class is defined, and,
4972 on the other, default arguments are valid in things like:
4973 template <class T = double>
4974 struct S { template <class U> void f(U); };
4975 Here the default argument for `S' has no bearing on the
4976 declaration of `f'. */
4977 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4979 /* Check everything. */
4980 last_level_to_check
= 0;
4982 for (parm_level
= parms
;
4983 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4984 parm_level
= TREE_CHAIN (parm_level
))
4986 tree inner_parms
= TREE_VALUE (parm_level
);
4990 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4991 for (i
= 0; i
< ntparms
; ++i
)
4993 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4996 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5001 if (is_friend_decl
== 2)
5008 /* Clear out the default argument so that we are not
5010 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5014 /* At this point, if we're still interested in issuing messages,
5015 they must apply to classes surrounding the object declared. */
5017 msg
= G_("default argument for template parameter for class "
5024 /* Worker for push_template_decl_real, called via
5025 for_each_template_parm. DATA is really an int, indicating the
5026 level of the parameters we are interested in. If T is a template
5027 parameter of that level, return nonzero. */
5030 template_parm_this_level_p (tree t
, void* data
)
5032 int this_level
= *(int *)data
;
5035 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5036 level
= TEMPLATE_PARM_LEVEL (t
);
5038 level
= TEMPLATE_TYPE_LEVEL (t
);
5039 return level
== this_level
;
5042 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5043 parameters given by current_template_args, or reuses a
5044 previously existing one, if appropriate. Returns the DECL, or an
5045 equivalent one, if it is replaced via a call to duplicate_decls.
5047 If IS_FRIEND is true, DECL is a friend declaration. */
5050 push_template_decl_real (tree decl
, bool is_friend
)
5058 int new_template_p
= 0;
5059 /* True if the template is a member template, in the sense of
5061 bool member_template_p
= false;
5063 if (decl
== error_mark_node
|| !current_template_parms
)
5064 return error_mark_node
;
5066 /* See if this is a partial specialization. */
5067 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5068 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5069 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5071 && DECL_LANG_SPECIFIC (decl
)
5072 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5073 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5075 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5079 /* For a friend, we want the context of the friend function, not
5080 the type of which it is a friend. */
5081 ctx
= CP_DECL_CONTEXT (decl
);
5082 else if (CP_DECL_CONTEXT (decl
)
5083 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5084 /* In the case of a virtual function, we want the class in which
5086 ctx
= CP_DECL_CONTEXT (decl
);
5088 /* Otherwise, if we're currently defining some class, the DECL
5089 is assumed to be a member of the class. */
5090 ctx
= current_scope ();
5092 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5095 if (!DECL_CONTEXT (decl
))
5096 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5098 /* See if this is a primary template. */
5099 if (is_friend
&& ctx
5100 && uses_template_parms_level (ctx
, processing_template_decl
))
5101 /* A friend template that specifies a class context, i.e.
5102 template <typename T> friend void A<T>::f();
5105 else if (TREE_CODE (decl
) == TYPE_DECL
5106 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5109 is_primary
= template_parm_scope_p ();
5113 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5115 if (DECL_CLASS_SCOPE_P (decl
))
5116 member_template_p
= true;
5117 if (TREE_CODE (decl
) == TYPE_DECL
5118 && anon_aggrname_p (DECL_NAME (decl
)))
5120 error ("template class without a name");
5121 return error_mark_node
;
5123 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5125 if (member_template_p
)
5127 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5128 error ("member template %qD may not have virt-specifiers", decl
);
5130 if (DECL_DESTRUCTOR_P (decl
))
5134 A destructor shall not be a member template. */
5135 error ("destructor %qD declared as member template", decl
);
5136 return error_mark_node
;
5138 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
5139 && (!prototype_p (TREE_TYPE (decl
))
5140 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5141 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5142 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
5143 == void_list_node
)))
5145 /* [basic.stc.dynamic.allocation]
5147 An allocation function can be a function
5148 template. ... Template allocation functions shall
5149 have two or more parameters. */
5150 error ("invalid template declaration of %qD", decl
);
5151 return error_mark_node
;
5154 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5155 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5157 else if (TREE_CODE (decl
) == TYPE_DECL
5158 && TYPE_DECL_ALIAS_P (decl
))
5159 /* alias-declaration */
5160 gcc_assert (!DECL_ARTIFICIAL (decl
));
5161 else if (VAR_P (decl
))
5162 /* C++14 variable template. */;
5165 error ("template declaration of %q#D", decl
);
5166 return error_mark_node
;
5170 /* Check to see that the rules regarding the use of default
5171 arguments are not being violated. */
5172 check_default_tmpl_args (decl
, current_template_parms
,
5173 is_primary
, is_partial
, /*is_friend_decl=*/0);
5175 /* Ensure that there are no parameter packs in the type of this
5176 declaration that have not been expanded. */
5177 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5179 /* Check each of the arguments individually to see if there are
5180 any bare parameter packs. */
5181 tree type
= TREE_TYPE (decl
);
5182 tree arg
= DECL_ARGUMENTS (decl
);
5183 tree argtype
= TYPE_ARG_TYPES (type
);
5185 while (arg
&& argtype
)
5187 if (!DECL_PACK_P (arg
)
5188 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5190 /* This is a PARM_DECL that contains unexpanded parameter
5191 packs. We have already complained about this in the
5192 check_for_bare_parameter_packs call, so just replace
5193 these types with ERROR_MARK_NODE. */
5194 TREE_TYPE (arg
) = error_mark_node
;
5195 TREE_VALUE (argtype
) = error_mark_node
;
5198 arg
= DECL_CHAIN (arg
);
5199 argtype
= TREE_CHAIN (argtype
);
5202 /* Check for bare parameter packs in the return type and the
5203 exception specifiers. */
5204 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5205 /* Errors were already issued, set return type to int
5206 as the frontend doesn't expect error_mark_node as
5208 TREE_TYPE (type
) = integer_type_node
;
5209 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5210 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5212 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5213 && TYPE_DECL_ALIAS_P (decl
))
5214 ? DECL_ORIGINAL_TYPE (decl
)
5215 : TREE_TYPE (decl
)))
5217 TREE_TYPE (decl
) = error_mark_node
;
5218 return error_mark_node
;
5222 return process_partial_specialization (decl
);
5224 args
= current_template_args ();
5227 || TREE_CODE (ctx
) == FUNCTION_DECL
5228 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5229 || (TREE_CODE (decl
) == TYPE_DECL
5230 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5231 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5233 if (DECL_LANG_SPECIFIC (decl
)
5234 && DECL_TEMPLATE_INFO (decl
)
5235 && DECL_TI_TEMPLATE (decl
))
5236 tmpl
= DECL_TI_TEMPLATE (decl
);
5237 /* If DECL is a TYPE_DECL for a class-template, then there won't
5238 be DECL_LANG_SPECIFIC. The information equivalent to
5239 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5240 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5241 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5242 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5244 /* Since a template declaration already existed for this
5245 class-type, we must be redeclaring it here. Make sure
5246 that the redeclaration is valid. */
5247 redeclare_class_template (TREE_TYPE (decl
),
5248 current_template_parms
,
5249 current_template_constraints ());
5250 /* We don't need to create a new TEMPLATE_DECL; just use the
5251 one we already had. */
5252 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5256 tmpl
= build_template_decl (decl
, current_template_parms
,
5260 if (DECL_LANG_SPECIFIC (decl
)
5261 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5263 /* A specialization of a member template of a template
5265 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5266 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5267 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5273 tree a
, t
, current
, parms
;
5275 tree tinfo
= get_template_info (decl
);
5279 error ("template definition of non-template %q#D", decl
);
5280 return error_mark_node
;
5283 tmpl
= TI_TEMPLATE (tinfo
);
5285 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5286 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5287 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5288 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5292 /* The declaration is a specialization of a member
5293 template, declared outside the class. Therefore, the
5294 innermost template arguments will be NULL, so we
5295 replace them with the arguments determined by the
5296 earlier call to check_explicit_specialization. */
5297 args
= DECL_TI_ARGS (decl
);
5300 = build_template_decl (decl
, current_template_parms
,
5302 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5303 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5304 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5305 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5306 DECL_TEMPLATE_INFO (new_tmpl
)
5307 = build_template_info (tmpl
, args
);
5309 register_specialization (new_tmpl
,
5310 most_general_template (tmpl
),
5316 /* Make sure the template headers we got make sense. */
5318 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5319 i
= TMPL_PARMS_DEPTH (parms
);
5320 if (TMPL_ARGS_DEPTH (args
) != i
)
5322 error ("expected %d levels of template parms for %q#D, got %d",
5323 i
, decl
, TMPL_ARGS_DEPTH (args
));
5324 DECL_INTERFACE_KNOWN (decl
) = 1;
5325 return error_mark_node
;
5328 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5330 a
= TMPL_ARGS_LEVEL (args
, i
);
5331 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5333 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5335 if (current
== decl
)
5336 error ("got %d template parameters for %q#D",
5337 TREE_VEC_LENGTH (a
), decl
);
5339 error ("got %d template parameters for %q#T",
5340 TREE_VEC_LENGTH (a
), current
);
5341 error (" but %d required", TREE_VEC_LENGTH (t
));
5342 /* Avoid crash in import_export_decl. */
5343 DECL_INTERFACE_KNOWN (decl
) = 1;
5344 return error_mark_node
;
5347 if (current
== decl
)
5349 else if (current
== NULL_TREE
)
5350 /* Can happen in erroneous input. */
5353 current
= get_containing_scope (current
);
5356 /* Check that the parms are used in the appropriate qualifying scopes
5357 in the declarator. */
5358 if (!comp_template_args
5360 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5363 template arguments to %qD do not match original template %qD",
5364 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5365 if (!uses_template_parms (TI_ARGS (tinfo
)))
5366 inform (input_location
, "use template<> for an explicit specialization");
5367 /* Avoid crash in import_export_decl. */
5368 DECL_INTERFACE_KNOWN (decl
) = 1;
5369 return error_mark_node
;
5373 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5374 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5376 /* Push template declarations for global functions and types. Note
5377 that we do not try to push a global template friend declared in a
5378 template class; such a thing may well depend on the template
5379 parameters of the class. */
5380 if (new_template_p
&& !ctx
5381 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5383 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5384 if (tmpl
== error_mark_node
)
5385 return error_mark_node
;
5387 /* Hide template friend classes that haven't been declared yet. */
5388 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5390 DECL_ANTICIPATED (tmpl
) = 1;
5391 DECL_FRIEND_P (tmpl
) = 1;
5397 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5400 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5401 if (DECL_CONV_FN_P (tmpl
))
5403 int depth
= TMPL_PARMS_DEPTH (parms
);
5405 /* It is a conversion operator. See if the type converted to
5406 depends on innermost template operands. */
5408 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5410 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5413 /* Give template template parms a DECL_CONTEXT of the template
5414 for which they are a parameter. */
5415 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5416 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5418 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5419 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5420 DECL_CONTEXT (parm
) = tmpl
;
5423 if (TREE_CODE (decl
) == TYPE_DECL
5424 && TYPE_DECL_ALIAS_P (decl
)
5425 && complex_alias_template_p (tmpl
))
5426 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5429 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5430 back to its most general template. If TMPL is a specialization,
5431 ARGS may only have the innermost set of arguments. Add the missing
5432 argument levels if necessary. */
5433 if (DECL_TEMPLATE_INFO (tmpl
))
5434 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5436 info
= build_template_info (tmpl
, args
);
5438 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5439 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5442 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5443 retrofit_lang_decl (decl
);
5444 if (DECL_LANG_SPECIFIC (decl
))
5445 DECL_TEMPLATE_INFO (decl
) = info
;
5448 if (flag_implicit_templates
5450 && TREE_PUBLIC (decl
)
5451 && VAR_OR_FUNCTION_DECL_P (decl
))
5452 /* Set DECL_COMDAT on template instantiations; if we force
5453 them to be emitted by explicit instantiation or -frepo,
5454 mark_needed will tell cgraph to do the right thing. */
5455 DECL_COMDAT (decl
) = true;
5457 return DECL_TEMPLATE_RESULT (tmpl
);
5461 push_template_decl (tree decl
)
5463 return push_template_decl_real (decl
, false);
5466 /* FN is an inheriting constructor that inherits from the constructor
5467 template INHERITED; turn FN into a constructor template with a matching
5471 add_inherited_template_parms (tree fn
, tree inherited
)
5474 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5475 inner_parms
= copy_node (inner_parms
);
5477 = tree_cons (size_int (processing_template_decl
+ 1),
5478 inner_parms
, current_template_parms
);
5479 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5480 tree args
= template_parms_to_args (parms
);
5481 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5482 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5483 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5484 DECL_ARTIFICIAL (tmpl
) = true;
5485 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5489 /* Called when a class template TYPE is redeclared with the indicated
5490 template PARMS, e.g.:
5492 template <class T> struct S;
5493 template <class T> struct S {}; */
5496 redeclare_class_template (tree type
, tree parms
, tree cons
)
5502 if (!TYPE_TEMPLATE_INFO (type
))
5504 error ("%qT is not a template type", type
);
5508 tmpl
= TYPE_TI_TEMPLATE (type
);
5509 if (!PRIMARY_TEMPLATE_P (tmpl
))
5510 /* The type is nested in some template class. Nothing to worry
5511 about here; there are no new template parameters for the nested
5517 error ("template specifiers not specified in declaration of %qD",
5522 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5523 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5525 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5527 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5528 "redeclared with %d template parameter",
5529 "redeclared with %d template parameters",
5530 TREE_VEC_LENGTH (parms
));
5531 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5532 "previous declaration %qD used %d template parameter",
5533 "previous declaration %qD used %d template parameters",
5534 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5538 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5545 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5546 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5549 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5550 if (error_operand_p (tmpl_parm
))
5553 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5554 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5555 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5557 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5559 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5560 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5561 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5562 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5563 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5564 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5565 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5566 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5567 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5569 error ("template parameter %q+#D", tmpl_parm
);
5570 error ("redeclared here as %q#D", parm
);
5574 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5576 /* We have in [temp.param]:
5578 A template-parameter may not be given default arguments
5579 by two different declarations in the same scope. */
5580 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5581 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5582 "original definition appeared here");
5586 if (parm_default
!= NULL_TREE
)
5587 /* Update the previous template parameters (which are the ones
5588 that will really count) with the new default value. */
5589 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5590 else if (tmpl_default
!= NULL_TREE
)
5591 /* Update the new parameters, too; they'll be used as the
5592 parameters for any members. */
5593 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5595 /* Give each template template parm in this redeclaration a
5596 DECL_CONTEXT of the template for which they are a parameter. */
5597 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5599 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5600 DECL_CONTEXT (parm
) = tmpl
;
5604 // Cannot redeclare a class template with a different set of constraints.
5605 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5607 error_at (input_location
, "redeclaration %q#D with different "
5608 "constraints", tmpl
);
5609 inform (DECL_SOURCE_LOCATION (tmpl
),
5610 "original declaration appeared here");
5616 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5617 to be used when the caller has already checked
5618 (processing_template_decl
5619 && !instantiation_dependent_expression_p (expr)
5620 && potential_constant_expression (expr))
5621 and cleared processing_template_decl. */
5624 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5626 return tsubst_copy_and_build (expr
,
5629 /*in_decl=*/NULL_TREE
,
5630 /*function_p=*/false,
5631 /*integral_constant_expression_p=*/true);
5634 /* Simplify EXPR if it is a non-dependent expression. Returns the
5635 (possibly simplified) expression. */
5638 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5640 if (expr
== NULL_TREE
)
5643 /* If we're in a template, but EXPR isn't value dependent, simplify
5644 it. We're supposed to treat:
5646 template <typename T> void f(T[1 + 1]);
5647 template <typename T> void f(T[2]);
5649 as two declarations of the same function, for example. */
5650 if (processing_template_decl
5651 && !instantiation_dependent_expression_p (expr
)
5652 && potential_constant_expression (expr
))
5654 processing_template_decl_sentinel s
;
5655 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5661 instantiate_non_dependent_expr (tree expr
)
5663 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5666 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5667 an uninstantiated expression. */
5670 instantiate_non_dependent_or_null (tree expr
)
5672 if (expr
== NULL_TREE
)
5674 if (processing_template_decl
)
5676 if (instantiation_dependent_expression_p (expr
)
5677 || !potential_constant_expression (expr
))
5681 processing_template_decl_sentinel s
;
5682 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5688 /* True iff T is a specialization of a variable template. */
5691 variable_template_specialization_p (tree t
)
5693 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5695 tree tmpl
= DECL_TI_TEMPLATE (t
);
5696 return variable_template_p (tmpl
);
5699 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5700 template declaration, or a TYPE_DECL for an alias declaration. */
5703 alias_type_or_template_p (tree t
)
5707 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5710 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5711 || DECL_ALIAS_TEMPLATE_P (t
));
5714 /* Return TRUE iff T is a specialization of an alias template. */
5717 alias_template_specialization_p (const_tree t
)
5719 /* It's an alias template specialization if it's an alias and its
5720 TYPE_NAME is a specialization of a primary template. */
5721 if (TYPE_ALIAS_P (t
))
5723 tree name
= TYPE_NAME (t
);
5724 if (DECL_LANG_SPECIFIC (name
))
5725 if (tree ti
= DECL_TEMPLATE_INFO (name
))
5727 tree tmpl
= TI_TEMPLATE (ti
);
5728 return PRIMARY_TEMPLATE_P (tmpl
);
5734 /* An alias template is complex from a SFINAE perspective if a template-id
5735 using that alias can be ill-formed when the expansion is not, as with
5736 the void_t template. We determine this by checking whether the
5737 expansion for the alias template uses all its template parameters. */
5739 struct uses_all_template_parms_data
5746 uses_all_template_parms_r (tree t
, void *data_
)
5748 struct uses_all_template_parms_data
&data
5749 = *(struct uses_all_template_parms_data
*)data_
;
5750 tree idx
= get_template_parm_index (t
);
5752 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5753 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5758 complex_alias_template_p (const_tree tmpl
)
5760 struct uses_all_template_parms_data data
;
5761 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5762 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5763 data
.level
= TMPL_PARMS_DEPTH (parms
);
5764 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5765 data
.seen
= XALLOCAVEC (bool, len
);
5766 for (int i
= 0; i
< len
; ++i
)
5767 data
.seen
[i
] = false;
5769 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5770 for (int i
= 0; i
< len
; ++i
)
5776 /* Return TRUE iff T is a specialization of a complex alias template with
5777 dependent template-arguments. */
5780 dependent_alias_template_spec_p (const_tree t
)
5782 return (alias_template_specialization_p (t
)
5783 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t
)))
5784 && (any_dependent_template_arguments_p
5785 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t
)))));
5788 /* Return the number of innermost template parameters in TMPL. */
5791 num_innermost_template_parms (tree tmpl
)
5793 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5794 return TREE_VEC_LENGTH (parms
);
5797 /* Return either TMPL or another template that it is equivalent to under DR
5798 1286: An alias that just changes the name of a template is equivalent to
5799 the other template. */
5802 get_underlying_template (tree tmpl
)
5804 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5805 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5807 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5808 if (TYPE_TEMPLATE_INFO (result
))
5810 tree sub
= TYPE_TI_TEMPLATE (result
);
5811 if (PRIMARY_TEMPLATE_P (sub
)
5812 && (num_innermost_template_parms (tmpl
)
5813 == num_innermost_template_parms (sub
)))
5815 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5816 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5817 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5819 /* The alias type is equivalent to the pattern of the
5820 underlying template, so strip the alias. */
5830 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5831 must be a function or a pointer-to-function type, as specified
5832 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5833 and check that the resulting function has external linkage. */
5836 convert_nontype_argument_function (tree type
, tree expr
,
5837 tsubst_flags_t complain
)
5841 linkage_kind linkage
;
5843 fn
= instantiate_type (type
, fns
, tf_none
);
5844 if (fn
== error_mark_node
)
5845 return error_mark_node
;
5848 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5849 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5850 if (BASELINK_P (fn_no_ptr
))
5851 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5853 /* [temp.arg.nontype]/1
5855 A template-argument for a non-type, non-template template-parameter
5858 -- the address of an object or function with external [C++11: or
5859 internal] linkage. */
5861 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5863 if (complain
& tf_error
)
5865 error ("%qE is not a valid template argument for type %qT",
5867 if (TYPE_PTR_P (type
))
5868 error ("it must be the address of a function with "
5869 "external linkage");
5871 error ("it must be the name of a function with "
5872 "external linkage");
5877 linkage
= decl_linkage (fn_no_ptr
);
5878 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5880 if (complain
& tf_error
)
5882 if (cxx_dialect
>= cxx11
)
5883 error ("%qE is not a valid template argument for type %qT "
5884 "because %qD has no linkage",
5885 expr
, type
, fn_no_ptr
);
5887 error ("%qE is not a valid template argument for type %qT "
5888 "because %qD does not have external linkage",
5889 expr
, type
, fn_no_ptr
);
5897 /* Subroutine of convert_nontype_argument.
5898 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5899 Emit an error otherwise. */
5902 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5903 tsubst_flags_t complain
)
5906 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5908 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5910 if (processing_template_decl
5911 && TREE_CODE (expr
) == ADDR_EXPR
5912 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5914 if (complain
& tf_error
)
5916 error ("%qE is not a valid template argument for type %qT",
5918 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5923 /* Returns TRUE iff the address of OP is value-dependent.
5925 14.6.2.4 [temp.dep.temp]:
5926 A non-integral non-type template-argument is dependent if its type is
5927 dependent or it has either of the following forms
5930 and contains a nested-name-specifier which specifies a class-name that
5931 names a dependent type.
5933 We generalize this to just say that the address of a member of a
5934 dependent class is value-dependent; the above doesn't cover the
5935 address of a static data member named with an unqualified-id. */
5938 has_value_dependent_address (tree op
)
5940 /* We could use get_inner_reference here, but there's no need;
5941 this is only relevant for template non-type arguments, which
5942 can only be expressed as &id-expression. */
5945 tree ctx
= CP_DECL_CONTEXT (op
);
5946 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5953 /* The next set of functions are used for providing helpful explanatory
5954 diagnostics for failed overload resolution. Their messages should be
5955 indented by two spaces for consistency with the messages in
5959 unify_success (bool /*explain_p*/)
5965 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5968 inform (input_location
,
5969 " couldn't deduce template parameter %qD", parm
);
5974 unify_invalid (bool /*explain_p*/)
5980 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5983 inform (input_location
,
5984 " types %qT and %qT have incompatible cv-qualifiers",
5990 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5993 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5998 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6001 inform (input_location
,
6002 " template parameter %qD is not a parameter pack, but "
6009 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6012 inform (input_location
,
6013 " template argument %qE does not match "
6014 "pointer-to-member constant %qE",
6020 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6023 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6028 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6031 inform (input_location
,
6032 " inconsistent parameter pack deduction with %qT and %qT",
6038 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6043 inform (input_location
,
6044 " deduced conflicting types for parameter %qT (%qT and %qT)",
6045 parm
, first
, second
);
6047 inform (input_location
,
6048 " deduced conflicting values for non-type parameter "
6049 "%qE (%qE and %qE)", parm
, first
, second
);
6055 unify_vla_arg (bool explain_p
, tree arg
)
6058 inform (input_location
,
6059 " variable-sized array type %qT is not "
6060 "a valid template argument",
6066 unify_method_type_error (bool explain_p
, tree arg
)
6069 inform (input_location
,
6070 " member function type %qT is not a valid template argument",
6076 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6081 inform_n (input_location
, wanted
,
6082 " candidate expects at least %d argument, %d provided",
6083 " candidate expects at least %d arguments, %d provided",
6086 inform_n (input_location
, wanted
,
6087 " candidate expects %d argument, %d provided",
6088 " candidate expects %d arguments, %d provided",
6095 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6097 return unify_arity (explain_p
, have
, wanted
);
6101 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6102 bool least_p
= false)
6104 return unify_arity (explain_p
, have
, wanted
, least_p
);
6108 unify_arg_conversion (bool explain_p
, tree to_type
,
6109 tree from_type
, tree arg
)
6112 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6113 " cannot convert %qE (type %qT) to type %qT",
6114 arg
, from_type
, to_type
);
6119 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6120 tree parm
, tree arg
)
6125 case tbr_ambiguous_baseclass
:
6126 inform (input_location
, " %qT is an ambiguous base class of %qT",
6130 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6137 unify_inconsistent_template_template_parameters (bool explain_p
)
6140 inform (input_location
,
6141 " template parameters of a template template argument are "
6142 "inconsistent with other deduced template arguments");
6147 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6150 inform (input_location
,
6151 " can't deduce a template for %qT from non-template type %qT",
6157 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6160 inform (input_location
,
6161 " template argument %qE does not match %qD", arg
, parm
);
6166 unify_overload_resolution_failure (bool explain_p
, tree arg
)
6169 inform (input_location
,
6170 " could not resolve address from overloaded function %qE",
6175 /* Attempt to convert the non-type template parameter EXPR to the
6176 indicated TYPE. If the conversion is successful, return the
6177 converted value. If the conversion is unsuccessful, return
6178 NULL_TREE if we issued an error message, or error_mark_node if we
6179 did not. We issue error messages for out-and-out bad template
6180 parameters, but not simply because the conversion failed, since we
6181 might be just trying to do argument deduction. Both TYPE and EXPR
6182 must be non-dependent.
6184 The conversion follows the special rules described in
6185 [temp.arg.nontype], and it is much more strict than an implicit
6188 This function is called twice for each template argument (see
6189 lookup_template_class for a more accurate description of this
6190 problem). This means that we need to handle expressions which
6191 are not valid in a C++ source, but can be created from the
6192 first call (for instance, casts to perform conversions). These
6193 hacks can go away after we fix the double coercion problem. */
6196 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6200 /* Detect immediately string literals as invalid non-type argument.
6201 This special-case is not needed for correctness (we would easily
6202 catch this later), but only to provide better diagnostic for this
6203 common user mistake. As suggested by DR 100, we do not mention
6204 linkage issues in the diagnostic as this is not the point. */
6205 /* FIXME we're making this OK. */
6206 if (TREE_CODE (expr
) == STRING_CST
)
6208 if (complain
& tf_error
)
6209 error ("%qE is not a valid template argument for type %qT "
6210 "because string literals can never be used in this context",
6215 /* Add the ADDR_EXPR now for the benefit of
6216 value_dependent_expression_p. */
6217 if (TYPE_PTROBV_P (type
)
6218 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6220 expr
= decay_conversion (expr
, complain
);
6221 if (expr
== error_mark_node
)
6222 return error_mark_node
;
6225 /* If we are in a template, EXPR may be non-dependent, but still
6226 have a syntactic, rather than semantic, form. For example, EXPR
6227 might be a SCOPE_REF, rather than the VAR_DECL to which the
6228 SCOPE_REF refers. Preserving the qualifying scope is necessary
6229 so that access checking can be performed when the template is
6230 instantiated -- but here we need the resolved form so that we can
6231 convert the argument. */
6232 bool non_dep
= false;
6233 if (TYPE_REF_OBJ_P (type
)
6234 && has_value_dependent_address (expr
))
6235 /* If we want the address and it's value-dependent, don't fold. */;
6236 else if (!type_unknown_p (expr
)
6237 && processing_template_decl
6238 && !instantiation_dependent_expression_p (expr
)
6239 && potential_constant_expression (expr
))
6241 if (error_operand_p (expr
))
6242 return error_mark_node
;
6243 expr_type
= TREE_TYPE (expr
);
6244 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6245 expr
= mark_lvalue_use (expr
);
6247 expr
= mark_rvalue_use (expr
);
6249 /* If the argument is non-dependent, perform any conversions in
6250 non-dependent context as well. */
6251 processing_template_decl_sentinel
s (non_dep
);
6253 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6255 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6256 to a non-type argument of "nullptr". */
6257 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
6258 expr
= fold_simple (convert (type
, expr
));
6260 /* In C++11, integral or enumeration non-type template arguments can be
6261 arbitrary constant expressions. Pointer and pointer to
6262 member arguments can be general constant expressions that evaluate
6263 to a null value, but otherwise still need to be of a specific form. */
6264 if (cxx_dialect
>= cxx11
)
6266 if (TREE_CODE (expr
) == PTRMEM_CST
)
6267 /* A PTRMEM_CST is already constant, and a valid template
6268 argument for a parameter of pointer to member type, we just want
6269 to leave it in that form rather than lower it to a
6271 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6272 expr
= maybe_constant_value (expr
);
6273 else if (cxx_dialect
>= cxx1z
)
6275 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6276 expr
= maybe_constant_value (expr
);
6277 else if (REFERENCE_REF_P (expr
))
6279 expr
= TREE_OPERAND (expr
, 0);
6280 expr
= maybe_constant_value (expr
);
6281 expr
= convert_from_reference (expr
);
6284 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6286 tree folded
= maybe_constant_value (expr
);
6287 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6288 : null_member_pointer_value_p (folded
))
6293 /* HACK: Due to double coercion, we can get a
6294 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6295 which is the tree that we built on the first call (see
6296 below when coercing to reference to object or to reference to
6297 function). We just strip everything and get to the arg.
6298 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6300 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6302 tree probe_type
, probe
= expr
;
6303 if (REFERENCE_REF_P (probe
))
6304 probe
= TREE_OPERAND (probe
, 0);
6305 probe_type
= TREE_TYPE (probe
);
6306 if (TREE_CODE (probe
) == NOP_EXPR
)
6308 /* ??? Maybe we could use convert_from_reference here, but we
6309 would need to relax its constraints because the NOP_EXPR
6310 could actually change the type to something more cv-qualified,
6311 and this is not folded by convert_from_reference. */
6312 tree addr
= TREE_OPERAND (probe
, 0);
6313 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6314 && TREE_CODE (addr
) == ADDR_EXPR
6315 && TYPE_PTR_P (TREE_TYPE (addr
))
6316 && (same_type_ignoring_top_level_qualifiers_p
6317 (TREE_TYPE (probe_type
),
6318 TREE_TYPE (TREE_TYPE (addr
)))))
6320 expr
= TREE_OPERAND (addr
, 0);
6321 expr_type
= TREE_TYPE (probe_type
);
6326 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6327 parameter is a pointer to object, through decay and
6328 qualification conversion. Let's strip everything. */
6329 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
6333 if (TREE_CODE (probe
) == ADDR_EXPR
6334 && TYPE_PTR_P (TREE_TYPE (probe
)))
6336 /* Skip the ADDR_EXPR only if it is part of the decay for
6337 an array. Otherwise, it is part of the original argument
6338 in the source code. */
6339 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
6340 probe
= TREE_OPERAND (probe
, 0);
6342 expr_type
= TREE_TYPE (expr
);
6346 /* [temp.arg.nontype]/5, bullet 1
6348 For a non-type template-parameter of integral or enumeration type,
6349 integral promotions (_conv.prom_) and integral conversions
6350 (_conv.integral_) are applied. */
6351 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6353 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
6354 t
= maybe_constant_value (t
);
6355 if (t
!= error_mark_node
)
6358 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6359 return error_mark_node
;
6361 /* Notice that there are constant expressions like '4 % 0' which
6362 do not fold into integer constants. */
6363 if (TREE_CODE (expr
) != INTEGER_CST
)
6365 if (complain
& tf_error
)
6367 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6368 if (processing_template_decl
6369 && !require_potential_constant_expression (expr
))
6371 expr
= cxx_constant_value (expr
);
6372 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6373 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6374 "in template argument for type %qT ", type
);
6375 if (expr
== error_mark_node
)
6377 /* else cxx_constant_value complained but gave us
6378 a real constant, so go ahead. */
6379 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6385 /* Avoid typedef problems. */
6386 if (TREE_TYPE (expr
) != type
)
6387 expr
= fold_convert (type
, expr
);
6389 /* [temp.arg.nontype]/5, bullet 2
6391 For a non-type template-parameter of type pointer to object,
6392 qualification conversions (_conv.qual_) and the array-to-pointer
6393 conversion (_conv.array_) are applied. */
6394 else if (TYPE_PTROBV_P (type
))
6396 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6398 A template-argument for a non-type, non-template template-parameter
6399 shall be one of: [...]
6401 -- the name of a non-type template-parameter;
6402 -- the address of an object or function with external linkage, [...]
6403 expressed as "& id-expression" where the & is optional if the name
6404 refers to a function or array, or if the corresponding
6405 template-parameter is a reference.
6407 Here, we do not care about functions, as they are invalid anyway
6408 for a parameter of type pointer-to-object. */
6410 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
6411 /* Non-type template parameters are OK. */
6413 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6414 /* Null pointer values are OK in C++11. */;
6415 else if (TREE_CODE (expr
) != ADDR_EXPR
6416 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6420 if (complain
& tf_error
)
6421 error ("%qD is not a valid template argument "
6422 "because %qD is a variable, not the address of "
6423 "a variable", expr
, expr
);
6426 if (POINTER_TYPE_P (expr_type
))
6428 if (complain
& tf_error
)
6429 error ("%qE is not a valid template argument for %qT "
6430 "because it is not the address of a variable",
6434 /* Other values, like integer constants, might be valid
6435 non-type arguments of some other type. */
6436 return error_mark_node
;
6442 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6443 ? TREE_OPERAND (expr
, 0) : expr
);
6446 if (complain
& tf_error
)
6447 error ("%qE is not a valid template argument of type %qT "
6448 "because %qE is not a variable", expr
, type
, decl
);
6451 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6453 if (complain
& tf_error
)
6454 error ("%qE is not a valid template argument of type %qT "
6455 "because %qD does not have external linkage",
6459 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6461 if (complain
& tf_error
)
6462 error ("%qE is not a valid template argument of type %qT "
6463 "because %qD has no linkage", expr
, type
, decl
);
6468 expr
= decay_conversion (expr
, complain
);
6469 if (expr
== error_mark_node
)
6470 return error_mark_node
;
6472 expr
= perform_qualification_conversions (type
, expr
);
6473 if (expr
== error_mark_node
)
6474 return error_mark_node
;
6476 /* [temp.arg.nontype]/5, bullet 3
6478 For a non-type template-parameter of type reference to object, no
6479 conversions apply. The type referred to by the reference may be more
6480 cv-qualified than the (otherwise identical) type of the
6481 template-argument. The template-parameter is bound directly to the
6482 template-argument, which must be an lvalue. */
6483 else if (TYPE_REF_OBJ_P (type
))
6485 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6487 return error_mark_node
;
6489 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6491 if (complain
& tf_error
)
6492 error ("%qE is not a valid template argument for type %qT "
6493 "because of conflicts in cv-qualification", expr
, type
);
6497 if (!real_lvalue_p (expr
))
6499 if (complain
& tf_error
)
6500 error ("%qE is not a valid template argument for type %qT "
6501 "because it is not an lvalue", expr
, type
);
6505 /* [temp.arg.nontype]/1
6507 A template-argument for a non-type, non-template template-parameter
6508 shall be one of: [...]
6510 -- the address of an object or function with external linkage. */
6511 if (INDIRECT_REF_P (expr
)
6512 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6514 expr
= TREE_OPERAND (expr
, 0);
6517 if (complain
& tf_error
)
6518 error ("%q#D is not a valid template argument for type %qT "
6519 "because a reference variable does not have a constant "
6520 "address", expr
, type
);
6527 if (complain
& tf_error
)
6528 error ("%qE is not a valid template argument for type %qT "
6529 "because it is not an object with linkage",
6534 /* DR 1155 allows internal linkage in C++11 and up. */
6535 linkage_kind linkage
= decl_linkage (expr
);
6536 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6538 if (complain
& tf_error
)
6539 error ("%qE is not a valid template argument for type %qT "
6540 "because object %qD does not have linkage",
6545 expr
= build_nop (type
, build_address (expr
));
6547 /* [temp.arg.nontype]/5, bullet 4
6549 For a non-type template-parameter of type pointer to function, only
6550 the function-to-pointer conversion (_conv.func_) is applied. If the
6551 template-argument represents a set of overloaded functions (or a
6552 pointer to such), the matching function is selected from the set
6554 else if (TYPE_PTRFN_P (type
))
6556 /* If the argument is a template-id, we might not have enough
6557 context information to decay the pointer. */
6558 if (!type_unknown_p (expr_type
))
6560 expr
= decay_conversion (expr
, complain
);
6561 if (expr
== error_mark_node
)
6562 return error_mark_node
;
6565 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6566 /* Null pointer values are OK in C++11. */
6567 return perform_qualification_conversions (type
, expr
);
6569 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6570 if (!expr
|| expr
== error_mark_node
)
6573 /* [temp.arg.nontype]/5, bullet 5
6575 For a non-type template-parameter of type reference to function, no
6576 conversions apply. If the template-argument represents a set of
6577 overloaded functions, the matching function is selected from the set
6579 else if (TYPE_REFFN_P (type
))
6581 if (TREE_CODE (expr
) == ADDR_EXPR
)
6583 if (complain
& tf_error
)
6585 error ("%qE is not a valid template argument for type %qT "
6586 "because it is a pointer", expr
, type
);
6587 inform (input_location
, "try using %qE instead",
6588 TREE_OPERAND (expr
, 0));
6593 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6594 if (!expr
|| expr
== error_mark_node
)
6597 expr
= build_nop (type
, build_address (expr
));
6599 /* [temp.arg.nontype]/5, bullet 6
6601 For a non-type template-parameter of type pointer to member function,
6602 no conversions apply. If the template-argument represents a set of
6603 overloaded member functions, the matching member function is selected
6604 from the set (_over.over_). */
6605 else if (TYPE_PTRMEMFUNC_P (type
))
6607 expr
= instantiate_type (type
, expr
, tf_none
);
6608 if (expr
== error_mark_node
)
6609 return error_mark_node
;
6611 /* [temp.arg.nontype] bullet 1 says the pointer to member
6612 expression must be a pointer-to-member constant. */
6613 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6614 return error_mark_node
;
6616 /* There is no way to disable standard conversions in
6617 resolve_address_of_overloaded_function (called by
6618 instantiate_type). It is possible that the call succeeded by
6619 converting &B::I to &D::I (where B is a base of D), so we need
6620 to reject this conversion here.
6622 Actually, even if there was a way to disable standard conversions,
6623 it would still be better to reject them here so that we can
6624 provide a superior diagnostic. */
6625 if (!same_type_p (TREE_TYPE (expr
), type
))
6627 if (complain
& tf_error
)
6629 error ("%qE is not a valid template argument for type %qT "
6630 "because it is of type %qT", expr
, type
,
6632 /* If we are just one standard conversion off, explain. */
6633 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6634 inform (input_location
,
6635 "standard conversions are not allowed in this context");
6640 /* [temp.arg.nontype]/5, bullet 7
6642 For a non-type template-parameter of type pointer to data member,
6643 qualification conversions (_conv.qual_) are applied. */
6644 else if (TYPE_PTRDATAMEM_P (type
))
6646 /* [temp.arg.nontype] bullet 1 says the pointer to member
6647 expression must be a pointer-to-member constant. */
6648 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6649 return error_mark_node
;
6651 expr
= perform_qualification_conversions (type
, expr
);
6652 if (expr
== error_mark_node
)
6655 else if (NULLPTR_TYPE_P (type
))
6657 if (expr
!= nullptr_node
)
6659 if (complain
& tf_error
)
6660 error ("%qE is not a valid template argument for type %qT "
6661 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6666 /* A template non-type parameter must be one of the above. */
6670 /* Sanity check: did we actually convert the argument to the
6672 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6673 (type
, TREE_TYPE (expr
)));
6674 return convert_from_reference (expr
);
6677 /* Subroutine of coerce_template_template_parms, which returns 1 if
6678 PARM_PARM and ARG_PARM match using the rule for the template
6679 parameters of template template parameters. Both PARM and ARG are
6680 template parameters; the rest of the arguments are the same as for
6681 coerce_template_template_parms.
6684 coerce_template_template_parm (tree parm
,
6686 tsubst_flags_t complain
,
6690 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6691 || parm
== NULL_TREE
|| error_operand_p (parm
))
6694 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6697 switch (TREE_CODE (parm
))
6700 /* We encounter instantiations of templates like
6701 template <template <template <class> class> class TT>
6704 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6705 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6707 if (!coerce_template_template_parms
6708 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6714 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6715 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6716 /* Argument is a parameter pack but parameter is not. */
6721 /* The tsubst call is used to handle cases such as
6723 template <int> class C {};
6724 template <class T, template <T> class TT> class D {};
6727 i.e. the parameter list of TT depends on earlier parameters. */
6728 if (!uses_template_parms (TREE_TYPE (arg
)))
6730 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6731 if (!uses_template_parms (t
)
6732 && !same_type_p (t
, TREE_TYPE (arg
)))
6736 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6737 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6738 /* Argument is a parameter pack but parameter is not. */
6751 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6752 template template parameters. Both PARM_PARMS and ARG_PARMS are
6753 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6756 Consider the example:
6757 template <class T> class A;
6758 template<template <class U> class TT> class B;
6760 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6761 the parameters to A, and OUTER_ARGS contains A. */
6764 coerce_template_template_parms (tree parm_parms
,
6766 tsubst_flags_t complain
,
6770 int nparms
, nargs
, i
;
6774 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6775 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6777 nparms
= TREE_VEC_LENGTH (parm_parms
);
6778 nargs
= TREE_VEC_LENGTH (arg_parms
);
6780 /* Determine whether we have a parameter pack at the end of the
6781 template template parameter's template parameter list. */
6782 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6784 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6786 if (error_operand_p (parm
))
6789 switch (TREE_CODE (parm
))
6793 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6798 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6808 && !(variadic_p
&& nargs
>= nparms
- 1))
6811 /* Check all of the template parameters except the parameter pack at
6812 the end (if any). */
6813 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6815 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6816 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6819 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6820 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6822 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6830 /* Check each of the template parameters in the template
6831 argument against the template parameter pack at the end of
6832 the template template parameter. */
6833 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6836 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6838 for (; i
< nargs
; ++i
)
6840 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6843 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6845 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6854 /* Verifies that the deduced template arguments (in TARGS) for the
6855 template template parameters (in TPARMS) represent valid bindings,
6856 by comparing the template parameter list of each template argument
6857 to the template parameter list of its corresponding template
6858 template parameter, in accordance with DR150. This
6859 routine can only be called after all template arguments have been
6860 deduced. It will return TRUE if all of the template template
6861 parameter bindings are okay, FALSE otherwise. */
6863 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6865 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6868 /* We're dealing with template parms in this process. */
6869 ++processing_template_decl
;
6871 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6873 for (i
= 0; i
< ntparms
; ++i
)
6875 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6876 tree targ
= TREE_VEC_ELT (targs
, i
);
6878 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6880 tree packed_args
= NULL_TREE
;
6883 if (ARGUMENT_PACK_P (targ
))
6885 /* Look inside the argument pack. */
6886 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6887 len
= TREE_VEC_LENGTH (packed_args
);
6890 for (idx
= 0; idx
< len
; ++idx
)
6892 tree targ_parms
= NULL_TREE
;
6895 /* Extract the next argument from the argument
6897 targ
= TREE_VEC_ELT (packed_args
, idx
);
6899 if (PACK_EXPANSION_P (targ
))
6900 /* Look at the pattern of the pack expansion. */
6901 targ
= PACK_EXPANSION_PATTERN (targ
);
6903 /* Extract the template parameters from the template
6905 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6906 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6907 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6908 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6910 /* Verify that we can coerce the template template
6911 parameters from the template argument to the template
6912 parameter. This requires an exact match. */
6914 && !coerce_template_template_parms
6915 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6930 --processing_template_decl
;
6934 /* Since type attributes aren't mangled, we need to strip them from
6935 template type arguments. */
6938 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6940 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6942 bool removed_attributes
= false;
6943 tree canon
= strip_typedefs (arg
, &removed_attributes
);
6944 if (removed_attributes
6945 && (complain
& tf_warning
))
6946 warning (0, "ignoring attributes on template argument %qT", arg
);
6950 // A template declaration can be substituted for a constrained
6951 // template template parameter only when the argument is more
6952 // constrained than the parameter.
6954 is_compatible_template_arg (tree parm
, tree arg
)
6956 tree parm_cons
= get_constraints (parm
);
6958 /* For now, allow constrained template template arguments
6959 and unconstrained template template parameters. */
6960 if (parm_cons
== NULL_TREE
)
6963 tree arg_cons
= get_constraints (arg
);
6965 // If the template parameter is constrained, we need to rewrite its
6966 // constraints in terms of the ARG's template parameters. This ensures
6967 // that all of the template parameter types will have the same depth.
6969 // Note that this is only valid when coerce_template_template_parm is
6970 // true for the innermost template parameters of PARM and ARG. In other
6971 // words, because coercion is successful, this conversion will be valid.
6974 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
6975 parm_cons
= tsubst_constraint_info (parm_cons
,
6976 INNERMOST_TEMPLATE_ARGS (args
),
6977 tf_none
, NULL_TREE
);
6978 if (parm_cons
== error_mark_node
)
6982 return subsumes (parm_cons
, arg_cons
);
6985 // Convert a placeholder argument into a binding to the original
6986 // parameter. The original parameter is saved as the TREE_TYPE of
6989 convert_wildcard_argument (tree parm
, tree arg
)
6991 TREE_TYPE (arg
) = parm
;
6995 /* Convert the indicated template ARG as necessary to match the
6996 indicated template PARM. Returns the converted ARG, or
6997 error_mark_node if the conversion was unsuccessful. Error and
6998 warning messages are issued under control of COMPLAIN. This
6999 conversion is for the Ith parameter in the parameter list. ARGS is
7000 the full set of template arguments deduced so far. */
7003 convert_template_argument (tree parm
,
7006 tsubst_flags_t complain
,
7012 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7014 if (parm
== error_mark_node
)
7015 return error_mark_node
;
7017 /* Trivially convert placeholders. */
7018 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7019 return convert_wildcard_argument (parm
, arg
);
7021 if (TREE_CODE (arg
) == TREE_LIST
7022 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7024 /* The template argument was the name of some
7025 member function. That's usually
7026 invalid, but static members are OK. In any
7027 case, grab the underlying fields/functions
7028 and issue an error later if required. */
7029 orig_arg
= TREE_VALUE (arg
);
7030 TREE_TYPE (arg
) = unknown_type_node
;
7035 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7036 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7037 || requires_tmpl_type
);
7039 /* When determining whether an argument pack expansion is a template,
7040 look at the pattern. */
7041 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7042 arg
= PACK_EXPANSION_PATTERN (arg
);
7044 /* Deal with an injected-class-name used as a template template arg. */
7045 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7047 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7048 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7050 if (cxx_dialect
>= cxx11
)
7051 /* OK under DR 1004. */;
7052 else if (complain
& tf_warning_or_error
)
7053 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7054 " used as template template argument", TYPE_NAME (arg
));
7055 else if (flag_pedantic_errors
)
7063 ((TREE_CODE (arg
) == TEMPLATE_DECL
7064 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7065 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7066 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7067 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7070 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7071 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7072 arg
= TYPE_STUB_DECL (arg
);
7074 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7076 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7077 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7079 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7081 if (complain
& tf_error
)
7082 error ("invalid use of destructor %qE as a type", orig_arg
);
7083 return error_mark_node
;
7086 permerror (input_location
,
7087 "to refer to a type member of a template parameter, "
7088 "use %<typename %E%>", orig_arg
);
7090 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7091 TREE_OPERAND (arg
, 1),
7097 if (is_type
!= requires_type
)
7101 if (complain
& tf_error
)
7103 error ("type/value mismatch at argument %d in template "
7104 "parameter list for %qD",
7107 inform (input_location
,
7108 " expected a constant of type %qT, got %qT",
7110 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7111 else if (requires_tmpl_type
)
7112 inform (input_location
,
7113 " expected a class template, got %qE", orig_arg
);
7115 inform (input_location
,
7116 " expected a type, got %qE", orig_arg
);
7119 return error_mark_node
;
7121 if (is_tmpl_type
^ requires_tmpl_type
)
7123 if (in_decl
&& (complain
& tf_error
))
7125 error ("type/value mismatch at argument %d in template "
7126 "parameter list for %qD",
7129 inform (input_location
,
7130 " expected a type, got %qT", DECL_NAME (arg
));
7132 inform (input_location
,
7133 " expected a class template, got %qT", orig_arg
);
7135 return error_mark_node
;
7140 if (requires_tmpl_type
)
7142 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7144 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7145 /* The number of argument required is not known yet.
7146 Just accept it for now. */
7147 val
= TREE_TYPE (arg
);
7150 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7153 /* Strip alias templates that are equivalent to another
7155 arg
= get_underlying_template (arg
);
7156 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7158 if (coerce_template_template_parms (parmparm
, argparm
,
7164 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7166 if (val
!= error_mark_node
)
7168 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7169 val
= TREE_TYPE (val
);
7170 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7171 val
= make_pack_expansion (val
);
7176 if (in_decl
&& (complain
& tf_error
))
7178 error ("type/value mismatch at argument %d in "
7179 "template parameter list for %qD",
7181 inform (input_location
,
7182 " expected a template of type %qD, got %qT",
7186 val
= error_mark_node
;
7189 // Check that the constraints are compatible before allowing the
7191 if (val
!= error_mark_node
)
7192 if (!is_compatible_template_arg (parm
, arg
))
7194 if (in_decl
&& (complain
& tf_error
))
7196 error ("constraint mismatch at argument %d in "
7197 "template parameter list for %qD",
7199 inform (input_location
, " expected %qD but got %qD",
7202 val
= error_mark_node
;
7208 /* We only form one instance of each template specialization.
7209 Therefore, if we use a non-canonical variant (i.e., a
7210 typedef), any future messages referring to the type will use
7211 the typedef, which is confusing if those future uses do not
7212 themselves also use the typedef. */
7214 val
= canonicalize_type_argument (val
, complain
);
7218 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
7220 if (invalid_nontype_parm_type_p (t
, complain
))
7221 return error_mark_node
;
7223 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7225 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
7229 /* Not sure if this is reachable, but it doesn't hurt
7231 error ("type mismatch in nontype parameter pack");
7232 val
= error_mark_node
;
7235 else if (!dependent_template_arg_p (orig_arg
)
7236 && !uses_template_parms (t
))
7237 /* We used to call digest_init here. However, digest_init
7238 will report errors, which we don't want when complain
7239 is zero. More importantly, digest_init will try too
7240 hard to convert things: for example, `0' should not be
7241 converted to pointer type at this point according to
7242 the standard. Accepting this is not merely an
7243 extension, since deciding whether or not these
7244 conversions can occur is part of determining which
7245 function template to call, or whether a given explicit
7246 argument specification is valid. */
7247 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7250 bool removed_attr
= false;
7251 val
= strip_typedefs_expr (orig_arg
, &removed_attr
);
7254 if (val
== NULL_TREE
)
7255 val
= error_mark_node
;
7256 else if (val
== error_mark_node
&& (complain
& tf_error
))
7257 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
7259 if (INDIRECT_REF_P (val
))
7261 /* Reject template arguments that are references to built-in
7262 functions with no library fallbacks. */
7263 const_tree inner
= TREE_OPERAND (val
, 0);
7264 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
7265 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner
))) == FUNCTION_TYPE
7266 && TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
7267 && 0 < TREE_OPERAND_LENGTH (inner
)
7268 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7269 return error_mark_node
;
7272 if (TREE_CODE (val
) == SCOPE_REF
)
7274 /* Strip typedefs from the SCOPE_REF. */
7275 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7276 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7278 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7279 QUALIFIED_NAME_IS_TEMPLATE (val
));
7286 /* Coerces the remaining template arguments in INNER_ARGS (from
7287 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7288 Returns the coerced argument pack. PARM_IDX is the position of this
7289 parameter in the template parameter list. ARGS is the original
7290 template argument list. */
7292 coerce_template_parameter_pack (tree parms
,
7300 tsubst_flags_t complain
)
7302 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7303 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7306 tree packed_parms
= NULL_TREE
;
7308 if (arg_idx
> nargs
)
7311 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7313 /* When the template parameter is a non-type template parameter pack
7314 or template template parameter pack whose type or template
7315 parameters use parameter packs, we know exactly how many arguments
7316 we are looking for. Build a vector of the instantiated decls for
7317 these template parameters in PACKED_PARMS. */
7318 /* We can't use make_pack_expansion here because it would interpret a
7319 _DECL as a use rather than a declaration. */
7320 tree decl
= TREE_VALUE (parm
);
7321 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7322 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7323 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7324 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7326 TREE_VEC_LENGTH (args
)--;
7327 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7328 TREE_VEC_LENGTH (args
)++;
7330 if (packed_parms
== error_mark_node
)
7331 return error_mark_node
;
7333 /* If we're doing a partial instantiation of a member template,
7334 verify that all of the types used for the non-type
7335 template parameter pack are, in fact, valid for non-type
7336 template parameters. */
7338 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7340 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7341 for (j
= 0; j
< len
; ++j
)
7343 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7344 if (invalid_nontype_parm_type_p (t
, complain
))
7345 return error_mark_node
;
7347 /* We don't know how many args we have yet, just
7348 use the unconverted ones for now. */
7352 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7354 /* Check if we have a placeholder pack, which indicates we're
7355 in the context of a introduction list. In that case we want
7356 to match this pack to the single placeholder. */
7357 else if (arg_idx
< nargs
7358 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7359 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7361 nargs
= arg_idx
+ 1;
7362 packed_args
= make_tree_vec (1);
7365 packed_args
= make_tree_vec (nargs
- arg_idx
);
7367 /* Convert the remaining arguments, which will be a part of the
7368 parameter pack "parm". */
7369 for (; arg_idx
< nargs
; ++arg_idx
)
7371 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7372 tree actual_parm
= TREE_VALUE (parm
);
7373 int pack_idx
= arg_idx
- parm_idx
;
7377 /* Once we've packed as many args as we have types, stop. */
7378 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7380 else if (PACK_EXPANSION_P (arg
))
7381 /* We don't know how many args we have yet, just
7382 use the unconverted ones for now. */
7385 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7388 if (arg
== error_mark_node
)
7390 if (complain
& tf_error
)
7391 error ("template argument %d is invalid", arg_idx
+ 1);
7394 arg
= convert_template_argument (actual_parm
,
7395 arg
, new_args
, complain
, parm_idx
,
7397 if (arg
== error_mark_node
)
7399 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7402 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
7403 && TREE_VEC_LENGTH (packed_args
) > 0)
7405 if (complain
& tf_error
)
7406 error ("wrong number of template arguments (%d, should be %d)",
7407 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
7408 return error_mark_node
;
7411 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7412 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7413 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7416 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7417 TREE_TYPE (argument_pack
)
7418 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
7419 TREE_CONSTANT (argument_pack
) = 1;
7422 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7424 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7425 TREE_VEC_LENGTH (packed_args
));
7426 return argument_pack
;
7429 /* Returns the number of pack expansions in the template argument vector
7433 pack_expansion_args_count (tree args
)
7438 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7440 tree elt
= TREE_VEC_ELT (args
, i
);
7441 if (elt
&& PACK_EXPANSION_P (elt
))
7447 /* Convert all template arguments to their appropriate types, and
7448 return a vector containing the innermost resulting template
7449 arguments. If any error occurs, return error_mark_node. Error and
7450 warning messages are issued under control of COMPLAIN.
7452 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7453 for arguments not specified in ARGS. Otherwise, if
7454 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7455 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7456 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7460 coerce_template_parms (tree parms
,
7463 tsubst_flags_t complain
,
7464 bool require_all_args
,
7465 bool use_default_args
)
7467 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7468 tree orig_inner_args
;
7471 tree new_inner_args
;
7472 int saved_unevaluated_operand
;
7473 int saved_inhibit_evaluation_warnings
;
7475 /* When used as a boolean value, indicates whether this is a
7476 variadic template parameter list. Since it's an int, we can also
7477 subtract it from nparms to get the number of non-variadic
7480 int variadic_args_p
= 0;
7481 int post_variadic_parms
= 0;
7483 /* Likewise for parameters with default arguments. */
7486 if (args
== error_mark_node
)
7487 return error_mark_node
;
7489 nparms
= TREE_VEC_LENGTH (parms
);
7491 /* Determine if there are any parameter packs or default arguments. */
7492 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7494 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7496 ++post_variadic_parms
;
7497 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7499 if (TREE_PURPOSE (parm
))
7503 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7504 /* If there are no parameters that follow a parameter pack, we need to
7505 expand any argument packs so that we can deduce a parameter pack from
7506 some non-packed args followed by an argument pack, as in variadic85.C.
7507 If there are such parameters, we need to leave argument packs intact
7508 so the arguments are assigned properly. This can happen when dealing
7509 with a nested class inside a partial specialization of a class
7510 template, as in variadic92.C, or when deducing a template parameter pack
7511 from a sub-declarator, as in variadic114.C. */
7512 if (!post_variadic_parms
)
7513 inner_args
= expand_template_argument_pack (inner_args
);
7515 /* Count any pack expansion args. */
7516 variadic_args_p
= pack_expansion_args_count (inner_args
);
7518 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7519 if ((nargs
> nparms
&& !variadic_p
)
7520 || (nargs
< nparms
- variadic_p
7523 && (!use_default_args
7524 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7525 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7527 if (complain
& tf_error
)
7529 if (variadic_p
|| default_p
)
7531 nparms
-= variadic_p
+ default_p
;
7532 error ("wrong number of template arguments "
7533 "(%d, should be at least %d)", nargs
, nparms
);
7536 error ("wrong number of template arguments "
7537 "(%d, should be %d)", nargs
, nparms
);
7540 inform (DECL_SOURCE_LOCATION (in_decl
),
7541 "provided for %qD", in_decl
);
7544 return error_mark_node
;
7546 /* We can't pass a pack expansion to a non-pack parameter of an alias
7547 template (DR 1430). */
7549 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
7550 || concept_template_p (in_decl
))
7552 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7554 if (complain
& tf_error
)
7556 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7558 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7559 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7561 if (PACK_EXPANSION_P (arg
)
7562 && !template_parameter_pack_p (parm
))
7564 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
7565 error_at (location_of (arg
),
7566 "pack expansion argument for non-pack parameter "
7567 "%qD of alias template %qD", parm
, in_decl
);
7569 error_at (location_of (arg
),
7570 "pack expansion argument for non-pack parameter "
7571 "%qD of concept %qD", parm
, in_decl
);
7572 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7579 return error_mark_node
;
7582 /* We need to evaluate the template arguments, even though this
7583 template-id may be nested within a "sizeof". */
7584 saved_unevaluated_operand
= cp_unevaluated_operand
;
7585 cp_unevaluated_operand
= 0;
7586 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7587 c_inhibit_evaluation_warnings
= 0;
7588 new_inner_args
= make_tree_vec (nparms
);
7589 new_args
= add_outermost_template_args (args
, new_inner_args
);
7590 int pack_adjust
= 0;
7591 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7596 /* Get the Ith template parameter. */
7597 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7599 if (parm
== error_mark_node
)
7601 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7605 /* Calculate the next argument. */
7606 if (arg_idx
< nargs
)
7607 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7611 if (template_parameter_pack_p (TREE_VALUE (parm
))
7612 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7614 /* Some arguments will be placed in the
7615 template parameter pack PARM. */
7616 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7617 inner_args
, arg_idx
,
7621 if (arg
== NULL_TREE
)
7623 /* We don't know how many args we have yet, just use the
7624 unconverted (and still packed) ones for now. */
7625 new_inner_args
= orig_inner_args
;
7630 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7632 /* Store this argument. */
7633 if (arg
== error_mark_node
)
7636 /* We are done with all of the arguments. */
7641 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7642 arg_idx
+= pack_adjust
;
7649 if (PACK_EXPANSION_P (arg
))
7651 /* "If every valid specialization of a variadic template
7652 requires an empty template parameter pack, the template is
7653 ill-formed, no diagnostic required." So check that the
7654 pattern works with this parameter. */
7655 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7656 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7660 if (conv
== error_mark_node
)
7662 inform (input_location
, "so any instantiation with a "
7663 "non-empty parameter pack would be ill-formed");
7666 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7667 /* Recover from missing typename. */
7668 TREE_VEC_ELT (inner_args
, arg_idx
)
7669 = make_pack_expansion (conv
);
7671 /* We don't know how many args we have yet, just
7672 use the unconverted ones for now. */
7673 new_inner_args
= inner_args
;
7678 else if (require_all_args
)
7680 /* There must be a default arg in this case. */
7681 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7683 /* The position of the first default template argument,
7684 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7686 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7687 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7688 arg_idx
- pack_adjust
);
7693 if (arg
== error_mark_node
)
7695 if (complain
& tf_error
)
7696 error ("template argument %d is invalid", arg_idx
+ 1);
7699 /* This only occurs if there was an error in the template
7700 parameter list itself (which we would already have
7701 reported) that we are trying to recover from, e.g., a class
7702 template with a parameter list such as
7703 template<typename..., typename>. */
7706 arg
= convert_template_argument (TREE_VALUE (parm
),
7707 arg
, new_args
, complain
,
7710 if (arg
== error_mark_node
)
7712 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7714 cp_unevaluated_operand
= saved_unevaluated_operand
;
7715 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7717 if (variadic_p
&& arg_idx
< nargs
)
7719 if (complain
& tf_error
)
7721 error ("wrong number of template arguments "
7722 "(%d, should be %d)", nargs
, arg_idx
);
7724 error ("provided for %q+D", in_decl
);
7726 return error_mark_node
;
7730 return error_mark_node
;
7732 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7733 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7734 TREE_VEC_LENGTH (new_inner_args
));
7736 return new_inner_args
;
7739 /* Convert all template arguments to their appropriate types, and
7740 return a vector containing the innermost resulting template
7741 arguments. If any error occurs, return error_mark_node. Error and
7742 warning messages are not issued.
7744 Note that no function argument deduction is performed, and default
7745 arguments are used to fill in unspecified arguments. */
7747 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
7749 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
7752 /* Convert all template arguments to their appropriate type, and
7753 instantiate default arguments as needed. This returns a vector
7754 containing the innermost resulting template arguments, or
7755 error_mark_node if unsuccessful. */
7757 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
7758 tsubst_flags_t complain
)
7760 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
7763 /* Like coerce_template_parms. If PARMS represents all template
7764 parameters levels, this function returns a vector of vectors
7765 representing all the resulting argument levels. Note that in this
7766 case, only the innermost arguments are coerced because the
7767 outermost ones are supposed to have been coerced already.
7769 Otherwise, if PARMS represents only (the innermost) vector of
7770 parameters, this function returns a vector containing just the
7771 innermost resulting arguments. */
7774 coerce_innermost_template_parms (tree parms
,
7777 tsubst_flags_t complain
,
7778 bool require_all_args
,
7779 bool use_default_args
)
7781 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7782 int args_depth
= TMPL_ARGS_DEPTH (args
);
7785 if (parms_depth
> 1)
7787 coerced_args
= make_tree_vec (parms_depth
);
7791 for (level
= parms
, cur_depth
= parms_depth
;
7792 parms_depth
> 0 && level
!= NULL_TREE
;
7793 level
= TREE_CHAIN (level
), --cur_depth
)
7796 if (cur_depth
== args_depth
)
7797 l
= coerce_template_parms (TREE_VALUE (level
),
7798 args
, in_decl
, complain
,
7802 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7804 if (l
== error_mark_node
)
7805 return error_mark_node
;
7807 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7811 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7812 args
, in_decl
, complain
,
7815 return coerced_args
;
7818 /* Returns 1 if template args OT and NT are equivalent. */
7821 template_args_equal (tree ot
, tree nt
)
7825 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7828 if (TREE_CODE (nt
) == TREE_VEC
)
7829 /* For member templates */
7830 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7831 else if (PACK_EXPANSION_P (ot
))
7832 return (PACK_EXPANSION_P (nt
)
7833 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7834 PACK_EXPANSION_PATTERN (nt
))
7835 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7836 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7837 else if (ARGUMENT_PACK_P (ot
))
7842 if (!ARGUMENT_PACK_P (nt
))
7845 opack
= ARGUMENT_PACK_ARGS (ot
);
7846 npack
= ARGUMENT_PACK_ARGS (nt
);
7847 len
= TREE_VEC_LENGTH (opack
);
7848 if (TREE_VEC_LENGTH (npack
) != len
)
7850 for (i
= 0; i
< len
; ++i
)
7851 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7852 TREE_VEC_ELT (npack
, i
)))
7856 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7858 /* We get here probably because we are in the middle of substituting
7859 into the pattern of a pack expansion. In that case the
7860 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7861 interested in. So we want to use the initial pack argument for
7863 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7864 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7865 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7866 return template_args_equal (ot
, nt
);
7868 else if (TYPE_P (nt
))
7872 /* Don't treat an alias template specialization with dependent
7873 arguments as equivalent to its underlying type when used as a
7874 template argument; we need them to be distinct so that we
7875 substitute into the specialization arguments at instantiation
7876 time. And aliases can't be equivalent without being ==, so
7877 we don't need to look any deeper. */
7878 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7881 return same_type_p (ot
, nt
);
7883 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7887 /* Try to treat a template non-type argument that has been converted
7888 to the parameter type as equivalent to one that hasn't yet. */
7889 for (enum tree_code code1
= TREE_CODE (ot
);
7890 CONVERT_EXPR_CODE_P (code1
)
7891 || code1
== NON_LVALUE_EXPR
;
7892 code1
= TREE_CODE (ot
))
7893 ot
= TREE_OPERAND (ot
, 0);
7894 for (enum tree_code code2
= TREE_CODE (nt
);
7895 CONVERT_EXPR_CODE_P (code2
)
7896 || code2
== NON_LVALUE_EXPR
;
7897 code2
= TREE_CODE (nt
))
7898 nt
= TREE_OPERAND (nt
, 0);
7900 return cp_tree_equal (ot
, nt
);
7904 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7905 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7906 NEWARG_PTR with the offending arguments if they are non-NULL. */
7909 comp_template_args (tree oldargs
, tree newargs
,
7910 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7914 if (oldargs
== newargs
)
7917 if (!oldargs
|| !newargs
)
7920 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7923 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7925 tree nt
= TREE_VEC_ELT (newargs
, i
);
7926 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7928 if (! template_args_equal (ot
, nt
))
7930 if (oldarg_ptr
!= NULL
)
7932 if (newarg_ptr
!= NULL
)
7941 add_pending_template (tree d
)
7943 tree ti
= (TYPE_P (d
)
7944 ? CLASSTYPE_TEMPLATE_INFO (d
)
7945 : DECL_TEMPLATE_INFO (d
));
7946 struct pending_template
*pt
;
7949 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7952 /* We are called both from instantiate_decl, where we've already had a
7953 tinst_level pushed, and instantiate_template, where we haven't.
7955 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7958 push_tinst_level (d
);
7960 pt
= ggc_alloc
<pending_template
> ();
7962 pt
->tinst
= current_tinst_level
;
7963 if (last_pending_template
)
7964 last_pending_template
->next
= pt
;
7966 pending_templates
= pt
;
7968 last_pending_template
= pt
;
7970 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7977 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7978 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7979 documentation for TEMPLATE_ID_EXPR. */
7982 lookup_template_function (tree fns
, tree arglist
)
7986 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7987 return error_mark_node
;
7989 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7991 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7993 error ("%q#D is not a function template", fns
);
7994 return error_mark_node
;
7997 if (BASELINK_P (fns
))
7999 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8001 BASELINK_FUNCTIONS (fns
),
8006 type
= TREE_TYPE (fns
);
8007 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8008 type
= unknown_type_node
;
8010 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8013 /* Within the scope of a template class S<T>, the name S gets bound
8014 (in build_self_reference) to a TYPE_DECL for the class, not a
8015 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8016 or one of its enclosing classes, and that type is a template,
8017 return the associated TEMPLATE_DECL. Otherwise, the original
8020 Also handle the case when DECL is a TREE_LIST of ambiguous
8021 injected-class-names from different bases. */
8024 maybe_get_template_decl_from_type_decl (tree decl
)
8026 if (decl
== NULL_TREE
)
8029 /* DR 176: A lookup that finds an injected-class-name (10.2
8030 [class.member.lookup]) can result in an ambiguity in certain cases
8031 (for example, if it is found in more than one base class). If all of
8032 the injected-class-names that are found refer to specializations of
8033 the same class template, and if the name is followed by a
8034 template-argument-list, the reference refers to the class template
8035 itself and not a specialization thereof, and is not ambiguous. */
8036 if (TREE_CODE (decl
) == TREE_LIST
)
8038 tree t
, tmpl
= NULL_TREE
;
8039 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8041 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8044 else if (tmpl
!= elt
)
8047 if (tmpl
&& t
== NULL_TREE
)
8053 return (decl
!= NULL_TREE
8054 && DECL_SELF_REFERENCE_P (decl
)
8055 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8056 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8059 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8060 parameters, find the desired type.
8062 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8064 IN_DECL, if non-NULL, is the template declaration we are trying to
8067 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8068 the class we are looking up.
8070 Issue error and warning messages under control of COMPLAIN.
8072 If the template class is really a local class in a template
8073 function, then the FUNCTION_CONTEXT is the function in which it is
8076 ??? Note that this function is currently called *twice* for each
8077 template-id: the first time from the parser, while creating the
8078 incomplete type (finish_template_type), and the second type during the
8079 real instantiation (instantiate_template_class). This is surely something
8080 that we want to avoid. It also causes some problems with argument
8081 coercion (see convert_nontype_argument for more information on this). */
8084 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8085 int entering_scope
, tsubst_flags_t complain
)
8087 tree templ
= NULL_TREE
, parmlist
;
8094 if (identifier_p (d1
))
8096 tree value
= innermost_non_namespace_value (d1
);
8097 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8102 push_decl_namespace (context
);
8103 templ
= lookup_name (d1
);
8104 templ
= maybe_get_template_decl_from_type_decl (templ
);
8106 pop_decl_namespace ();
8109 context
= DECL_CONTEXT (templ
);
8111 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8113 tree type
= TREE_TYPE (d1
);
8115 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8116 an implicit typename for the second A. Deal with it. */
8117 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8118 type
= TREE_TYPE (type
);
8120 if (CLASSTYPE_TEMPLATE_INFO (type
))
8122 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8123 d1
= DECL_NAME (templ
);
8126 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8127 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8129 templ
= TYPE_TI_TEMPLATE (d1
);
8130 d1
= DECL_NAME (templ
);
8132 else if (DECL_TYPE_TEMPLATE_P (d1
))
8135 d1
= DECL_NAME (templ
);
8136 context
= DECL_CONTEXT (templ
);
8138 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8141 d1
= DECL_NAME (templ
);
8144 /* Issue an error message if we didn't find a template. */
8147 if (complain
& tf_error
)
8148 error ("%qT is not a template", d1
);
8149 return error_mark_node
;
8152 if (TREE_CODE (templ
) != TEMPLATE_DECL
8153 /* Make sure it's a user visible template, if it was named by
8155 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8156 && !PRIMARY_TEMPLATE_P (templ
)))
8158 if (complain
& tf_error
)
8160 error ("non-template type %qT used as a template", d1
);
8162 error ("for template declaration %q+D", in_decl
);
8164 return error_mark_node
;
8167 complain
&= ~tf_user
;
8169 /* An alias that just changes the name of a template is equivalent to the
8170 other template, so if any of the arguments are pack expansions, strip
8171 the alias to avoid problems with a pack expansion passed to a non-pack
8172 alias template parameter (DR 1430). */
8173 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8174 templ
= get_underlying_template (templ
);
8176 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8178 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8179 template arguments */
8185 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
8187 /* Consider an example where a template template parameter declared as
8189 template <class T, class U = std::allocator<T> > class TT
8191 The template parameter level of T and U are one level larger than
8192 of TT. To proper process the default argument of U, say when an
8193 instantiation `TT<int>' is seen, we need to build the full
8194 arguments containing {int} as the innermost level. Outer levels,
8195 available when not appearing as default template argument, can be
8196 obtained from the arguments of the enclosing template.
8198 Suppose that TT is later substituted with std::vector. The above
8199 instantiation is `TT<int, std::allocator<T> >' with TT at
8200 level 1, and T at level 2, while the template arguments at level 1
8201 becomes {std::vector} and the inner level 2 is {int}. */
8203 outer
= DECL_CONTEXT (templ
);
8205 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
8206 else if (current_template_parms
)
8208 /* This is an argument of the current template, so we haven't set
8209 DECL_CONTEXT yet. */
8210 tree relevant_template_parms
;
8212 /* Parameter levels that are greater than the level of the given
8213 template template parm are irrelevant. */
8214 relevant_template_parms
= current_template_parms
;
8215 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
8216 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
8217 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
8219 outer
= template_parms_to_args (relevant_template_parms
);
8223 arglist
= add_to_template_args (outer
, arglist
);
8225 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
8227 /*require_all_args=*/true,
8228 /*use_default_args=*/true);
8229 if (arglist2
== error_mark_node
8230 || (!uses_template_parms (arglist2
)
8231 && check_instantiated_args (templ
, arglist2
, complain
)))
8232 return error_mark_node
;
8234 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8239 tree template_type
= TREE_TYPE (templ
);
8242 tree found
= NULL_TREE
;
8245 int is_dependent_type
;
8246 int use_partial_inst_tmpl
= false;
8248 if (template_type
== error_mark_node
)
8249 /* An error occurred while building the template TEMPL, and a
8250 diagnostic has most certainly been emitted for that
8251 already. Let's propagate that error. */
8252 return error_mark_node
;
8254 gen_tmpl
= most_general_template (templ
);
8255 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8256 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8257 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8259 if (arg_depth
== 1 && parm_depth
> 1)
8261 /* We've been given an incomplete set of template arguments.
8264 template <class T> struct S1 {
8265 template <class U> struct S2 {};
8266 template <class U> struct S2<U*> {};
8269 we will be called with an ARGLIST of `U*', but the
8270 TEMPLATE will be `template <class T> template
8271 <class U> struct S1<T>::S2'. We must fill in the missing
8274 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
8276 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8279 /* Now we should have enough arguments. */
8280 gcc_assert (parm_depth
== arg_depth
);
8282 /* From here on, we're only interested in the most general
8285 /* Calculate the BOUND_ARGS. These will be the args that are
8286 actually tsubst'd into the definition to create the
8288 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8290 /*require_all_args=*/true,
8291 /*use_default_args=*/true);
8293 if (arglist
== error_mark_node
)
8294 /* We were unable to bind the arguments. */
8295 return error_mark_node
;
8297 /* In the scope of a template class, explicit references to the
8298 template class refer to the type of the template, not any
8299 instantiation of it. For example, in:
8301 template <class T> class C { void f(C<T>); }
8303 the `C<T>' is just the same as `C'. Outside of the
8304 class, however, such a reference is an instantiation. */
8306 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8307 || currently_open_class (template_type
))
8308 /* comp_template_args is expensive, check it last. */
8309 && comp_template_args (TYPE_TI_ARGS (template_type
),
8311 return template_type
;
8313 /* If we already have this specialization, return it. */
8314 elt
.tmpl
= gen_tmpl
;
8316 elt
.spec
= NULL_TREE
;
8317 hash
= spec_hasher::hash (&elt
);
8318 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8323 /* If the the template's constraints are not satisfied,
8324 then we cannot form a valid type.
8326 Note that the check is deferred until after the hash
8327 lookup. This prevents redundant checks on previously
8328 instantiated specializations. */
8329 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8331 if (complain
& tf_error
)
8333 error ("template constraint failure");
8334 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8336 return error_mark_node
;
8339 is_dependent_type
= uses_template_parms (arglist
);
8341 /* If the deduced arguments are invalid, then the binding
8343 if (!is_dependent_type
8344 && check_instantiated_args (gen_tmpl
,
8345 INNERMOST_TEMPLATE_ARGS (arglist
),
8347 return error_mark_node
;
8349 if (!is_dependent_type
8350 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8351 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8352 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8354 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8355 DECL_NAME (gen_tmpl
),
8356 /*tag_scope=*/ts_global
);
8360 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8362 if (context
== error_mark_node
)
8363 return error_mark_node
;
8366 context
= global_namespace
;
8368 /* Create the type. */
8369 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8371 /* The user referred to a specialization of an alias
8372 template represented by GEN_TMPL.
8374 [temp.alias]/2 says:
8376 When a template-id refers to the specialization of an
8377 alias template, it is equivalent to the associated
8378 type obtained by substitution of its
8379 template-arguments for the template-parameters in the
8380 type-id of the alias template. */
8382 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8383 /* Note that the call above (by indirectly calling
8384 register_specialization in tsubst_decl) registers the
8385 TYPE_DECL representing the specialization of the alias
8386 template. So next time someone substitutes ARGLIST for
8387 the template parms into the alias template (GEN_TMPL),
8388 she'll get that TYPE_DECL back. */
8390 if (t
== error_mark_node
)
8393 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8395 if (!is_dependent_type
)
8397 set_current_access_from_decl (TYPE_NAME (template_type
));
8398 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8399 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8400 arglist
, complain
, in_decl
),
8401 SCOPED_ENUM_P (template_type
), NULL
);
8403 if (t
== error_mark_node
)
8408 /* We don't want to call start_enum for this type, since
8409 the values for the enumeration constants may involve
8410 template parameters. And, no one should be interested
8411 in the enumeration constants for such a type. */
8412 t
= cxx_make_type (ENUMERAL_TYPE
);
8413 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8415 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8416 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8417 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8419 else if (CLASS_TYPE_P (template_type
))
8421 t
= make_class_type (TREE_CODE (template_type
));
8422 CLASSTYPE_DECLARED_CLASS (t
)
8423 = CLASSTYPE_DECLARED_CLASS (template_type
);
8424 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8425 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
8427 /* A local class. Make sure the decl gets registered properly. */
8428 if (context
== current_function_decl
)
8429 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8431 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8432 /* This instantiation is another name for the primary
8433 template type. Set the TYPE_CANONICAL field
8435 TYPE_CANONICAL (t
) = template_type
;
8436 else if (any_template_arguments_need_structural_equality_p (arglist
))
8437 /* Some of the template arguments require structural
8438 equality testing, so this template class requires
8439 structural equality testing. */
8440 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8445 /* If we called start_enum or pushtag above, this information
8446 will already be set up. */
8449 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8451 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8452 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8453 DECL_SOURCE_LOCATION (type_decl
)
8454 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8457 type_decl
= TYPE_NAME (t
);
8459 if (CLASS_TYPE_P (template_type
))
8461 TREE_PRIVATE (type_decl
)
8462 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8463 TREE_PROTECTED (type_decl
)
8464 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8465 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8467 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8468 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8472 if (OVERLOAD_TYPE_P (t
)
8473 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8475 static const char *tags
[] = {"abi_tag", "may_alias"};
8477 for (unsigned ix
= 0; ix
!= 2; ix
++)
8480 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8484 else if (!TREE_CHAIN (attributes
) && !TYPE_ATTRIBUTES (t
))
8485 TYPE_ATTRIBUTES (t
) = attributes
;
8488 = tree_cons (TREE_PURPOSE (attributes
),
8489 TREE_VALUE (attributes
),
8490 TYPE_ATTRIBUTES (t
));
8494 /* Let's consider the explicit specialization of a member
8495 of a class template specialization that is implicitly instantiated,
8500 template<class U> struct M {}; //#0
8505 struct S<int>::M<char> //#1
8509 [temp.expl.spec]/4 says this is valid.
8511 In this case, when we write:
8514 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8517 When we encounter #1, we want to store the partial instantiation
8518 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8520 For all cases other than this "explicit specialization of member of a
8521 class template", we just want to store the most general template into
8522 the CLASSTYPE_TI_TEMPLATE of M.
8524 This case of "explicit specialization of member of a class template"
8526 1/ the enclosing class is an instantiation of, and therefore not
8527 the same as, the context of the most general template, and
8528 2/ we aren't looking at the partial instantiation itself, i.e.
8529 the innermost arguments are not the same as the innermost parms of
8530 the most general template.
8532 So it's only when 1/ and 2/ happens that we want to use the partial
8533 instantiation of the member template in lieu of its most general
8536 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8537 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8538 /* the enclosing class must be an instantiation... */
8539 && CLASS_TYPE_P (context
)
8540 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8542 tree partial_inst_args
;
8543 TREE_VEC_LENGTH (arglist
)--;
8544 ++processing_template_decl
;
8546 tsubst (INNERMOST_TEMPLATE_ARGS
8547 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
8548 arglist
, complain
, NULL_TREE
);
8549 --processing_template_decl
;
8550 TREE_VEC_LENGTH (arglist
)++;
8551 use_partial_inst_tmpl
=
8552 /*...and we must not be looking at the partial instantiation
8554 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8558 if (!use_partial_inst_tmpl
)
8559 /* This case is easy; there are no member templates involved. */
8563 /* This is a full instantiation of a member template. Find
8564 the partial instantiation of which this is an instance. */
8566 /* Temporarily reduce by one the number of levels in the ARGLIST
8567 so as to avoid comparing the last set of arguments. */
8568 TREE_VEC_LENGTH (arglist
)--;
8569 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
8570 TREE_VEC_LENGTH (arglist
)++;
8571 /* FOUND is either a proper class type, or an alias
8572 template specialization. In the later case, it's a
8573 TYPE_DECL, resulting from the substituting of arguments
8574 for parameters in the TYPE_DECL of the alias template
8575 done earlier. So be careful while getting the template
8577 found
= TREE_CODE (found
) == TYPE_DECL
8578 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
8579 : CLASSTYPE_TI_TEMPLATE (found
);
8582 // Build template info for the new specialization.
8583 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8586 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8587 entry
= ggc_alloc
<spec_entry
> ();
8591 /* Note this use of the partial instantiation so we can check it
8592 later in maybe_process_partial_specialization. */
8593 DECL_TEMPLATE_INSTANTIATIONS (found
)
8594 = tree_cons (arglist
, t
,
8595 DECL_TEMPLATE_INSTANTIATIONS (found
));
8597 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8598 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8599 /* Now that the type has been registered on the instantiations
8600 list, we set up the enumerators. Because the enumeration
8601 constants may involve the enumeration type itself, we make
8602 sure to register the type first, and then create the
8603 constants. That way, doing tsubst_expr for the enumeration
8604 constants won't result in recursive calls here; we'll find
8605 the instantiation and exit above. */
8606 tsubst_enum (template_type
, t
, arglist
);
8608 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8609 /* If the type makes use of template parameters, the
8610 code that generates debugging information will crash. */
8611 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8613 /* Possibly limit visibility based on template args. */
8614 TREE_PUBLIC (type_decl
) = 1;
8615 determine_visibility (type_decl
);
8617 inherit_targ_abi_tags (t
);
8623 /* Wrapper for lookup_template_class_1. */
8626 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8627 int entering_scope
, tsubst_flags_t complain
)
8630 timevar_push (TV_TEMPLATE_INST
);
8631 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8632 entering_scope
, complain
);
8633 timevar_pop (TV_TEMPLATE_INST
);
8637 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8640 lookup_template_variable (tree templ
, tree arglist
)
8642 /* The type of the expression is NULL_TREE since the template-id could refer
8643 to an explicit or partial specialization. */
8644 tree type
= NULL_TREE
;
8645 if (flag_concepts
&& variable_concept_p (templ
))
8646 /* Except that concepts are always bool. */
8647 type
= boolean_type_node
;
8648 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8651 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8654 finish_template_variable (tree var
, tsubst_flags_t complain
)
8656 tree templ
= TREE_OPERAND (var
, 0);
8657 tree arglist
= TREE_OPERAND (var
, 1);
8659 /* We never want to return a VAR_DECL for a variable concept, since they
8660 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8661 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
8662 if (concept_p
&& processing_template_decl
)
8665 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8666 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8668 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8669 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8671 /*use_default*/true);
8673 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
8675 if (complain
& tf_error
)
8677 error ("constraints for %qD not satisfied", templ
);
8678 diagnose_constraints (location_of (var
), templ
, arglist
);
8680 return error_mark_node
;
8683 /* If a template-id refers to a specialization of a variable
8684 concept, then the expression is true if and only if the
8685 concept's constraints are satisfied by the given template
8688 NOTE: This is an extension of Concepts Lite TS that
8689 allows constraints to be used in expressions. */
8692 tree decl
= DECL_TEMPLATE_RESULT (templ
);
8693 return evaluate_variable_concept (decl
, arglist
);
8696 return instantiate_template (templ
, arglist
, complain
);
8703 /* True when we should also visit template parameters that occur in
8704 non-deduced contexts. */
8705 bool include_nondeduced_p
;
8706 hash_set
<tree
> *visited
;
8709 /* Called from for_each_template_parm via walk_tree. */
8712 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8715 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8716 tree_fn_t fn
= pfd
->fn
;
8717 void *data
= pfd
->data
;
8718 tree result
= NULL_TREE
;
8720 #define WALK_SUBTREE(NODE) \
8723 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8724 pfd->include_nondeduced_p); \
8725 if (result) goto out; \
8730 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
8731 WALK_SUBTREE (TYPE_CONTEXT (t
));
8733 switch (TREE_CODE (t
))
8736 if (TYPE_PTRMEMFUNC_P (t
))
8742 if (!TYPE_TEMPLATE_INFO (t
))
8745 WALK_SUBTREE (TYPE_TI_ARGS (t
));
8749 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
8750 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
8754 /* Since we're not going to walk subtrees, we have to do this
8756 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
8760 /* Check the return type. */
8761 WALK_SUBTREE (TREE_TYPE (t
));
8763 /* Check the parameter types. Since default arguments are not
8764 instantiated until they are needed, the TYPE_ARG_TYPES may
8765 contain expressions that involve template parameters. But,
8766 no-one should be looking at them yet. And, once they're
8767 instantiated, they don't contain template parameters, so
8768 there's no point in looking at them then, either. */
8772 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8773 WALK_SUBTREE (TREE_VALUE (parm
));
8775 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8776 want walk_tree walking into them itself. */
8782 case UNDERLYING_TYPE
:
8783 if (pfd
->include_nondeduced_p
8784 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
8786 pfd
->include_nondeduced_p
))
8787 return error_mark_node
;
8792 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
8793 WALK_SUBTREE (DECL_TI_ARGS (t
));
8798 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
8799 WALK_SUBTREE (DECL_INITIAL (t
));
8800 if (DECL_CONTEXT (t
)
8801 && pfd
->include_nondeduced_p
)
8802 WALK_SUBTREE (DECL_CONTEXT (t
));
8805 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8806 /* Record template parameters such as `T' inside `TT<T>'. */
8807 WALK_SUBTREE (TYPE_TI_ARGS (t
));
8810 case TEMPLATE_TEMPLATE_PARM
:
8811 case TEMPLATE_TYPE_PARM
:
8812 case TEMPLATE_PARM_INDEX
:
8813 if (fn
&& (*fn
)(t
, data
))
8820 /* A template template parameter is encountered. */
8821 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8822 WALK_SUBTREE (TREE_TYPE (t
));
8824 /* Already substituted template template parameter */
8830 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
8834 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8835 && pfd
->include_nondeduced_p
)
8836 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
8841 /* If there's no type, then this thing must be some expression
8842 involving template parameters. */
8843 if (!fn
&& !TREE_TYPE (t
))
8844 return error_mark_node
;
8849 case IMPLICIT_CONV_EXPR
:
8850 case REINTERPRET_CAST_EXPR
:
8851 case CONST_CAST_EXPR
:
8852 case STATIC_CAST_EXPR
:
8853 case DYNAMIC_CAST_EXPR
:
8857 case PSEUDO_DTOR_EXPR
:
8859 return error_mark_node
;
8868 /* We didn't find any template parameters we liked. */
8873 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8874 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8875 call FN with the parameter and the DATA.
8876 If FN returns nonzero, the iteration is terminated, and
8877 for_each_template_parm returns 1. Otherwise, the iteration
8878 continues. If FN never returns a nonzero value, the value
8879 returned by for_each_template_parm is 0. If FN is NULL, it is
8880 considered to be the function which always returns 1.
8882 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8883 parameters that occur in non-deduced contexts. When false, only
8884 visits those template parameters that can be deduced. */
8887 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8888 hash_set
<tree
> *visited
,
8889 bool include_nondeduced_p
)
8891 struct pair_fn_data pfd
;
8897 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8899 /* Walk the tree. (Conceptually, we would like to walk without
8900 duplicates, but for_each_template_parm_r recursively calls
8901 for_each_template_parm, so we would need to reorganize a fair
8902 bit to use walk_tree_without_duplicates, so we keep our own
8905 pfd
.visited
= visited
;
8907 pfd
.visited
= new hash_set
<tree
>;
8908 result
= cp_walk_tree (&t
,
8909 for_each_template_parm_r
,
8923 /* Returns true if T depends on any template parameter. */
8926 uses_template_parms (tree t
)
8932 int saved_processing_template_decl
;
8934 saved_processing_template_decl
= processing_template_decl
;
8935 if (!saved_processing_template_decl
)
8936 processing_template_decl
= 1;
8938 dependent_p
= dependent_type_p (t
);
8939 else if (TREE_CODE (t
) == TREE_VEC
)
8940 dependent_p
= any_dependent_template_arguments_p (t
);
8941 else if (TREE_CODE (t
) == TREE_LIST
)
8942 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8943 || uses_template_parms (TREE_CHAIN (t
)));
8944 else if (TREE_CODE (t
) == TYPE_DECL
)
8945 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8948 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8949 || TREE_CODE (t
) == OVERLOAD
8952 || TREE_CODE (t
) == TRAIT_EXPR
8953 || TREE_CODE (t
) == CONSTRUCTOR
8954 || CONSTANT_CLASS_P (t
))
8955 dependent_p
= (type_dependent_expression_p (t
)
8956 || value_dependent_expression_p (t
));
8959 gcc_assert (t
== error_mark_node
);
8960 dependent_p
= false;
8963 processing_template_decl
= saved_processing_template_decl
;
8968 /* Returns true iff current_function_decl is an incompletely instantiated
8969 template. Useful instead of processing_template_decl because the latter
8970 is set to 0 during instantiate_non_dependent_expr. */
8973 in_template_function (void)
8975 tree fn
= current_function_decl
;
8977 ++processing_template_decl
;
8978 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8979 && DECL_TEMPLATE_INFO (fn
)
8980 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8981 --processing_template_decl
;
8985 /* Returns true if T depends on any template parameter with level LEVEL. */
8988 uses_template_parms_level (tree t
, int level
)
8990 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8991 /*include_nondeduced_p=*/true);
8994 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8995 ill-formed translation unit, i.e. a variable or function that isn't
8996 usable in a constant expression. */
8999 neglectable_inst_p (tree d
)
9002 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9003 : decl_maybe_constant_var_p (d
)));
9006 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9007 neglectable and instantiated from within an erroneous instantiation. */
9010 limit_bad_template_recursion (tree decl
)
9012 struct tinst_level
*lev
= current_tinst_level
;
9013 int errs
= errorcount
+ sorrycount
;
9014 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9017 for (; lev
; lev
= lev
->next
)
9018 if (neglectable_inst_p (lev
->decl
))
9021 return (lev
&& errs
> lev
->errors
);
9024 static int tinst_depth
;
9025 extern int max_tinst_depth
;
9028 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9030 /* We're starting to instantiate D; record the template instantiation context
9031 for diagnostics and to restore it later. */
9034 push_tinst_level (tree d
)
9036 return push_tinst_level_loc (d
, input_location
);
9039 /* We're starting to instantiate D; record the template instantiation context
9040 at LOC for diagnostics and to restore it later. */
9043 push_tinst_level_loc (tree d
, location_t loc
)
9045 struct tinst_level
*new_level
;
9047 if (tinst_depth
>= max_tinst_depth
)
9049 fatal_error (input_location
,
9050 "template instantiation depth exceeds maximum of %d"
9051 " (use -ftemplate-depth= to increase the maximum)",
9056 /* If the current instantiation caused problems, don't let it instantiate
9057 anything else. Do allow deduction substitution and decls usable in
9058 constant expressions. */
9059 if (limit_bad_template_recursion (d
))
9062 new_level
= ggc_alloc
<tinst_level
> ();
9063 new_level
->decl
= d
;
9064 new_level
->locus
= loc
;
9065 new_level
->errors
= errorcount
+sorrycount
;
9066 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9067 new_level
->next
= current_tinst_level
;
9068 current_tinst_level
= new_level
;
9071 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9072 depth_reached
= tinst_depth
;
9077 /* We're done instantiating this template; return to the instantiation
9081 pop_tinst_level (void)
9083 /* Restore the filename and line number stashed away when we started
9084 this instantiation. */
9085 input_location
= current_tinst_level
->locus
;
9086 current_tinst_level
= current_tinst_level
->next
;
9090 /* We're instantiating a deferred template; restore the template
9091 instantiation context in which the instantiation was requested, which
9092 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9095 reopen_tinst_level (struct tinst_level
*level
)
9097 struct tinst_level
*t
;
9100 for (t
= level
; t
; t
= t
->next
)
9103 current_tinst_level
= level
;
9105 if (current_tinst_level
)
9106 current_tinst_level
->errors
= errorcount
+sorrycount
;
9110 /* Returns the TINST_LEVEL which gives the original instantiation
9113 struct tinst_level
*
9114 outermost_tinst_level (void)
9116 struct tinst_level
*level
= current_tinst_level
;
9119 level
= level
->next
;
9123 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9124 vector of template arguments, as for tsubst.
9126 Returns an appropriate tsubst'd friend declaration. */
9129 tsubst_friend_function (tree decl
, tree args
)
9133 if (TREE_CODE (decl
) == FUNCTION_DECL
9134 && DECL_TEMPLATE_INSTANTIATION (decl
)
9135 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9136 /* This was a friend declared with an explicit template
9137 argument list, e.g.:
9141 to indicate that f was a template instantiation, not a new
9142 function declaration. Now, we have to figure out what
9143 instantiation of what template. */
9145 tree template_id
, arglist
, fns
;
9148 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9150 /* Friend functions are looked up in the containing namespace scope.
9151 We must enter that scope, to avoid finding member functions of the
9152 current class with same name. */
9153 push_nested_namespace (ns
);
9154 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9155 tf_warning_or_error
, NULL_TREE
,
9156 /*integral_constant_expression_p=*/false);
9157 pop_nested_namespace (ns
);
9158 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9159 tf_warning_or_error
, NULL_TREE
);
9160 template_id
= lookup_template_function (fns
, arglist
);
9162 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9163 tmpl
= determine_specialization (template_id
, new_friend
,
9165 /*need_member_template=*/0,
9166 TREE_VEC_LENGTH (args
),
9168 return instantiate_template (tmpl
, new_args
, tf_error
);
9171 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9173 /* The NEW_FRIEND will look like an instantiation, to the
9174 compiler, but is not an instantiation from the point of view of
9175 the language. For example, we might have had:
9177 template <class T> struct S {
9178 template <class U> friend void f(T, U);
9181 Then, in S<int>, template <class U> void f(int, U) is not an
9182 instantiation of anything. */
9183 if (new_friend
== error_mark_node
)
9184 return error_mark_node
;
9186 DECL_USE_TEMPLATE (new_friend
) = 0;
9187 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9189 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9190 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9191 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9194 /* The mangled name for the NEW_FRIEND is incorrect. The function
9195 is not a template instantiation and should not be mangled like
9196 one. Therefore, we forget the mangling here; we'll recompute it
9197 later if we need it. */
9198 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9200 SET_DECL_RTL (new_friend
, NULL
);
9201 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9204 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9207 tree new_friend_template_info
;
9208 tree new_friend_result_template_info
;
9210 int new_friend_is_defn
;
9212 /* We must save some information from NEW_FRIEND before calling
9213 duplicate decls since that function will free NEW_FRIEND if
9215 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9216 new_friend_is_defn
=
9217 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9218 (template_for_substitution (new_friend
)))
9220 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9222 /* This declaration is a `primary' template. */
9223 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9225 new_friend_result_template_info
9226 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9229 new_friend_result_template_info
= NULL_TREE
;
9231 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9232 if (new_friend_is_defn
)
9233 DECL_INITIAL (new_friend
) = error_mark_node
;
9235 /* Inside pushdecl_namespace_level, we will push into the
9236 current namespace. However, the friend function should go
9237 into the namespace of the template. */
9238 ns
= decl_namespace_context (new_friend
);
9239 push_nested_namespace (ns
);
9240 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9241 pop_nested_namespace (ns
);
9243 if (old_decl
== error_mark_node
)
9244 return error_mark_node
;
9246 if (old_decl
!= new_friend
)
9248 /* This new friend declaration matched an existing
9249 declaration. For example, given:
9251 template <class T> void f(T);
9252 template <class U> class C {
9253 template <class T> friend void f(T) {}
9256 the friend declaration actually provides the definition
9257 of `f', once C has been instantiated for some type. So,
9258 old_decl will be the out-of-class template declaration,
9259 while new_friend is the in-class definition.
9261 But, if `f' was called before this point, the
9262 instantiation of `f' will have DECL_TI_ARGS corresponding
9263 to `T' but not to `U', references to which might appear
9264 in the definition of `f'. Previously, the most general
9265 template for an instantiation of `f' was the out-of-class
9266 version; now it is the in-class version. Therefore, we
9267 run through all specialization of `f', adding to their
9268 DECL_TI_ARGS appropriately. In particular, they need a
9269 new set of outer arguments, corresponding to the
9270 arguments for this class instantiation.
9272 The same situation can arise with something like this:
9275 template <class T> class C {
9279 when `C<int>' is instantiated. Now, `f(int)' is defined
9282 if (!new_friend_is_defn
)
9283 /* On the other hand, if the in-class declaration does
9284 *not* provide a definition, then we don't want to alter
9285 existing definitions. We can just leave everything
9290 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9291 tree new_args
= TI_ARGS (new_friend_template_info
);
9293 /* Overwrite whatever template info was there before, if
9294 any, with the new template information pertaining to
9296 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9298 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9300 /* We should have called reregister_specialization in
9302 gcc_assert (retrieve_specialization (new_template
,
9306 /* Instantiate it if the global has already been used. */
9307 if (DECL_ODR_USED (old_decl
))
9308 instantiate_decl (old_decl
, /*defer_ok=*/true,
9309 /*expl_inst_class_mem_p=*/false);
9315 /* Indicate that the old function template is a partial
9317 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9318 = new_friend_result_template_info
;
9320 gcc_assert (new_template
9321 == most_general_template (new_template
));
9322 gcc_assert (new_template
!= old_decl
);
9324 /* Reassign any specializations already in the hash table
9325 to the new more general template, and add the
9326 additional template args. */
9327 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9331 tree spec
= TREE_VALUE (t
);
9334 elt
.tmpl
= old_decl
;
9335 elt
.args
= DECL_TI_ARGS (spec
);
9336 elt
.spec
= NULL_TREE
;
9338 decl_specializations
->remove_elt (&elt
);
9341 = add_outermost_template_args (new_args
,
9342 DECL_TI_ARGS (spec
));
9344 register_specialization
9345 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9348 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9352 /* The information from NEW_FRIEND has been merged into OLD_DECL
9353 by duplicate_decls. */
9354 new_friend
= old_decl
;
9359 tree context
= DECL_CONTEXT (new_friend
);
9363 template <class T> class C {
9364 template <class U> friend void C1<U>::f (); // case 1
9365 friend void C2<T>::f (); // case 2
9367 we only need to make sure CONTEXT is a complete type for
9368 case 2. To distinguish between the two cases, we note that
9369 CONTEXT of case 1 remains dependent type after tsubst while
9370 this isn't true for case 2. */
9371 ++processing_template_decl
;
9372 dependent_p
= dependent_type_p (context
);
9373 --processing_template_decl
;
9376 && !complete_type_or_else (context
, NULL_TREE
))
9377 return error_mark_node
;
9379 if (COMPLETE_TYPE_P (context
))
9381 tree fn
= new_friend
;
9382 /* do_friend adds the TEMPLATE_DECL for any member friend
9383 template even if it isn't a member template, i.e.
9384 template <class T> friend A<T>::f();
9385 Look through it in that case. */
9386 if (TREE_CODE (fn
) == TEMPLATE_DECL
9387 && !PRIMARY_TEMPLATE_P (fn
))
9388 fn
= DECL_TEMPLATE_RESULT (fn
);
9389 /* Check to see that the declaration is really present, and,
9390 possibly obtain an improved declaration. */
9391 fn
= check_classfn (context
, fn
, NULL_TREE
);
9401 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9402 template arguments, as for tsubst.
9404 Returns an appropriate tsubst'd friend type or error_mark_node on
9408 tsubst_friend_class (tree friend_tmpl
, tree args
)
9414 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9416 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9417 return TREE_TYPE (t
);
9420 context
= CP_DECL_CONTEXT (friend_tmpl
);
9422 if (context
!= global_namespace
)
9424 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9425 push_nested_namespace (context
);
9427 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9430 /* Look for a class template declaration. We look for hidden names
9431 because two friend declarations of the same template are the
9432 same. For example, in:
9435 template <typename> friend class F;
9437 template <typename> struct B {
9438 template <typename> friend class F;
9441 both F templates are the same. */
9442 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
9443 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
9445 /* But, if we don't find one, it might be because we're in a
9446 situation like this:
9454 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9455 for `S<int>', not the TEMPLATE_DECL. */
9456 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
9458 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
9459 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
9462 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9464 /* The friend template has already been declared. Just
9465 check to see that the declarations match, and install any new
9466 default parameters. We must tsubst the default parameters,
9467 of course. We only need the innermost template parameters
9468 because that is all that redeclare_class_template will look
9470 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9471 > TMPL_ARGS_DEPTH (args
))
9474 location_t saved_input_location
;
9475 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9476 args
, tf_warning_or_error
);
9478 saved_input_location
= input_location
;
9479 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9480 tree cons
= get_constraints (tmpl
);
9481 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
9482 input_location
= saved_input_location
;
9486 friend_type
= TREE_TYPE (tmpl
);
9490 /* The friend template has not already been declared. In this
9491 case, the instantiation of the template class will cause the
9492 injection of this template into the global scope. */
9493 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
9494 if (tmpl
== error_mark_node
)
9495 return error_mark_node
;
9497 /* The new TMPL is not an instantiation of anything, so we
9498 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9499 the new type because that is supposed to be the corresponding
9500 template decl, i.e., TMPL. */
9501 DECL_USE_TEMPLATE (tmpl
) = 0;
9502 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
9503 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
9504 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
9505 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
9507 /* Inject this template into the global scope. */
9508 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
9511 if (context
!= global_namespace
)
9513 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9514 pop_nested_namespace (context
);
9516 pop_nested_class ();
9522 /* Returns zero if TYPE cannot be completed later due to circularity.
9523 Otherwise returns one. */
9526 can_complete_type_without_circularity (tree type
)
9528 if (type
== NULL_TREE
|| type
== error_mark_node
)
9530 else if (COMPLETE_TYPE_P (type
))
9532 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
9533 return can_complete_type_without_circularity (TREE_TYPE (type
));
9534 else if (CLASS_TYPE_P (type
)
9535 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
9541 static tree
tsubst_omp_clauses (tree
, bool, bool, tree
, tsubst_flags_t
, tree
);
9543 /* Apply any attributes which had to be deferred until instantiation
9544 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9545 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9548 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
9549 tree args
, tsubst_flags_t complain
, tree in_decl
)
9551 tree last_dep
= NULL_TREE
;
9555 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
9556 if (ATTR_IS_DEPENDENT (t
))
9559 attributes
= copy_list (attributes
);
9563 if (DECL_P (*decl_p
))
9565 if (TREE_TYPE (*decl_p
) == error_mark_node
)
9567 p
= &DECL_ATTRIBUTES (*decl_p
);
9570 p
= &TYPE_ATTRIBUTES (*decl_p
);
9574 tree late_attrs
= NULL_TREE
;
9575 tree
*q
= &late_attrs
;
9577 for (*p
= attributes
; *p
; )
9580 if (ATTR_IS_DEPENDENT (t
))
9582 *p
= TREE_CHAIN (t
);
9583 TREE_CHAIN (t
) = NULL_TREE
;
9584 if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
9585 && is_attribute_p ("omp declare simd",
9586 get_attribute_name (t
))
9589 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
9590 clauses
= tsubst_omp_clauses (clauses
, true, false, args
,
9592 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9593 clauses
= finish_omp_clauses (clauses
, false, true);
9594 tree parms
= DECL_ARGUMENTS (*decl_p
);
9596 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9598 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
9600 TREE_VALUE (t
) = NULL_TREE
;
9602 /* If the first attribute argument is an identifier, don't
9603 pass it through tsubst. Attributes like mode, format,
9604 cleanup and several target specific attributes expect it
9606 else if (attribute_takes_identifier_p (get_attribute_name (t
))
9610 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
9612 /*integral_constant_expression_p=*/false);
9613 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
9615 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
9618 else if (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
)))
9620 /* An attribute pack expansion. */
9621 tree purp
= TREE_PURPOSE (t
);
9622 tree pack
= (tsubst_pack_expansion
9623 (TREE_VALUE (t
), args
, complain
, in_decl
));
9624 int len
= TREE_VEC_LENGTH (pack
);
9625 for (int i
= 0; i
< len
; ++i
)
9627 tree elt
= TREE_VEC_ELT (pack
, i
);
9628 *q
= build_tree_list (purp
, elt
);
9629 q
= &TREE_CHAIN (*q
);
9635 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
9636 /*integral_constant_expression_p=*/false);
9638 q
= &TREE_CHAIN (t
);
9641 p
= &TREE_CHAIN (t
);
9644 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9648 /* Perform (or defer) access check for typedefs that were referenced
9649 from within the template TMPL code.
9650 This is a subroutine of instantiate_decl and instantiate_class_template.
9651 TMPL is the template to consider and TARGS is the list of arguments of
9655 perform_typedefs_access_check (tree tmpl
, tree targs
)
9657 location_t saved_location
;
9659 qualified_typedef_usage_t
*iter
;
9662 || (!CLASS_TYPE_P (tmpl
)
9663 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9666 saved_location
= input_location
;
9667 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9669 tree type_decl
= iter
->typedef_decl
;
9670 tree type_scope
= iter
->context
;
9672 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9675 if (uses_template_parms (type_decl
))
9676 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9677 if (uses_template_parms (type_scope
))
9678 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9680 /* Make access check error messages point to the location
9681 of the use of the typedef. */
9682 input_location
= iter
->locus
;
9683 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9684 type_decl
, type_decl
,
9685 tf_warning_or_error
);
9687 input_location
= saved_location
;
9691 instantiate_class_template_1 (tree type
)
9693 tree templ
, args
, pattern
, t
, member
;
9697 unsigned int saved_maximum_field_alignment
;
9700 if (type
== error_mark_node
)
9701 return error_mark_node
;
9703 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9704 || uses_template_parms (type
))
9707 /* Figure out which template is being instantiated. */
9708 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9709 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9711 /* Determine what specialization of the original template to
9713 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9714 if (t
== error_mark_node
)
9716 TYPE_BEING_DEFINED (type
) = 1;
9717 return error_mark_node
;
9721 /* This TYPE is actually an instantiation of a partial
9722 specialization. We replace the innermost set of ARGS with
9723 the arguments appropriate for substitution. For example,
9726 template <class T> struct S {};
9727 template <class T> struct S<T*> {};
9729 and supposing that we are instantiating S<int*>, ARGS will
9730 presently be {int*} -- but we need {int}. */
9731 pattern
= TREE_TYPE (t
);
9732 args
= TREE_PURPOSE (t
);
9736 pattern
= TREE_TYPE (templ
);
9737 args
= CLASSTYPE_TI_ARGS (type
);
9740 /* If the template we're instantiating is incomplete, then clearly
9741 there's nothing we can do. */
9742 if (!COMPLETE_TYPE_P (pattern
))
9745 /* If we've recursively instantiated too many templates, stop. */
9746 if (! push_tinst_level (type
))
9749 /* Now we're really doing the instantiation. Mark the type as in
9750 the process of being defined. */
9751 TYPE_BEING_DEFINED (type
) = 1;
9753 /* We may be in the middle of deferred access check. Disable
9755 push_deferring_access_checks (dk_no_deferred
);
9757 int saved_unevaluated_operand
= cp_unevaluated_operand
;
9758 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9760 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9761 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9762 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
9763 fn_context
= error_mark_node
;
9765 push_to_top_level ();
9768 cp_unevaluated_operand
= 0;
9769 c_inhibit_evaluation_warnings
= 0;
9771 /* Use #pragma pack from the template context. */
9772 saved_maximum_field_alignment
= maximum_field_alignment
;
9773 maximum_field_alignment
= TYPE_PRECISION (pattern
);
9775 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
9777 /* Set the input location to the most specialized template definition.
9778 This is needed if tsubsting causes an error. */
9779 typedecl
= TYPE_MAIN_DECL (pattern
);
9780 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
9781 DECL_SOURCE_LOCATION (typedecl
);
9783 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
9784 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
9785 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
9786 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
9787 if (ANON_AGGR_TYPE_P (pattern
))
9788 SET_ANON_AGGR_TYPE_P (type
);
9789 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
9791 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
9792 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
9793 /* Adjust visibility for template arguments. */
9794 determine_visibility (TYPE_MAIN_DECL (type
));
9796 if (CLASS_TYPE_P (type
))
9797 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
9799 pbinfo
= TYPE_BINFO (pattern
);
9801 /* We should never instantiate a nested class before its enclosing
9802 class; we need to look up the nested class by name before we can
9803 instantiate it, and that lookup should instantiate the enclosing
9805 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
9806 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
9808 base_list
= NULL_TREE
;
9809 if (BINFO_N_BASE_BINFOS (pbinfo
))
9815 /* We must enter the scope containing the type, as that is where
9816 the accessibility of types named in dependent bases are
9818 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
9820 /* Substitute into each of the bases to determine the actual
9822 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
9825 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
9826 tree expanded_bases
= NULL_TREE
;
9829 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
9832 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
9833 args
, tf_error
, NULL_TREE
);
9834 if (expanded_bases
== error_mark_node
)
9837 len
= TREE_VEC_LENGTH (expanded_bases
);
9840 for (idx
= 0; idx
< len
; idx
++)
9843 /* Extract the already-expanded base class. */
9844 base
= TREE_VEC_ELT (expanded_bases
, idx
);
9846 /* Substitute to figure out the base class. */
9847 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
9850 if (base
== error_mark_node
)
9853 base_list
= tree_cons (access
, base
, base_list
);
9854 if (BINFO_VIRTUAL_P (pbase_binfo
))
9855 TREE_TYPE (base_list
) = integer_type_node
;
9859 /* The list is now in reverse order; correct that. */
9860 base_list
= nreverse (base_list
);
9863 pop_scope (pushed_scope
);
9865 /* Now call xref_basetypes to set up all the base-class
9867 xref_basetypes (type
, base_list
);
9869 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9870 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9871 args
, tf_error
, NULL_TREE
);
9872 fixup_attribute_variants (type
);
9874 /* Now that our base classes are set up, enter the scope of the
9875 class, so that name lookups into base classes, etc. will work
9876 correctly. This is precisely analogous to what we do in
9877 begin_class_definition when defining an ordinary non-template
9878 class, except we also need to push the enclosing classes. */
9879 push_nested_class (type
);
9881 /* Now members are processed in the order of declaration. */
9882 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9883 member
; member
= TREE_CHAIN (member
))
9885 tree t
= TREE_VALUE (member
);
9887 if (TREE_PURPOSE (member
))
9891 /* Build new CLASSTYPE_NESTED_UTDS. */
9894 bool class_template_p
;
9896 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9897 && TYPE_LANG_SPECIFIC (t
)
9898 && CLASSTYPE_IS_TEMPLATE (t
));
9899 /* If the member is a class template, then -- even after
9900 substitution -- there may be dependent types in the
9901 template argument list for the class. We increment
9902 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9903 that function will assume that no types are dependent
9904 when outside of a template. */
9905 if (class_template_p
)
9906 ++processing_template_decl
;
9907 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9908 if (class_template_p
)
9909 --processing_template_decl
;
9910 if (newtag
== error_mark_node
)
9913 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9915 tree name
= TYPE_IDENTIFIER (t
);
9917 if (class_template_p
)
9918 /* Unfortunately, lookup_template_class sets
9919 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9920 instantiation (i.e., for the type of a member
9921 template class nested within a template class.)
9922 This behavior is required for
9923 maybe_process_partial_specialization to work
9924 correctly, but is not accurate in this case;
9925 the TAG is not an instantiation of anything.
9926 (The corresponding TEMPLATE_DECL is an
9927 instantiation, but the TYPE is not.) */
9928 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9930 /* Now, we call pushtag to put this NEWTAG into the scope of
9931 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9932 pushtag calling push_template_decl. We don't have to do
9933 this for enums because it will already have been done in
9936 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9937 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9940 else if (DECL_DECLARES_FUNCTION_P (t
))
9942 /* Build new TYPE_METHODS. */
9945 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9946 ++processing_template_decl
;
9947 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9948 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9949 --processing_template_decl
;
9950 set_current_access_from_decl (r
);
9951 finish_member_declaration (r
);
9952 /* Instantiate members marked with attribute used. */
9953 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9955 if (TREE_CODE (r
) == FUNCTION_DECL
9956 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9957 cp_check_omp_declare_reduction (r
);
9959 else if (DECL_CLASS_TEMPLATE_P (t
)
9960 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
9961 /* A closure type for a lambda in a default argument for a
9962 member template. Ignore it; it will be instantiated with
9963 the default argument. */;
9966 /* Build new TYPE_FIELDS. */
9967 if (TREE_CODE (t
) == STATIC_ASSERT
)
9971 ++c_inhibit_evaluation_warnings
;
9973 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9974 tf_warning_or_error
, NULL_TREE
,
9975 /*integral_constant_expression_p=*/true);
9976 --c_inhibit_evaluation_warnings
;
9978 finish_static_assert (condition
,
9979 STATIC_ASSERT_MESSAGE (t
),
9980 STATIC_ASSERT_SOURCE_LOCATION (t
),
9983 else if (TREE_CODE (t
) != CONST_DECL
)
9986 tree vec
= NULL_TREE
;
9989 /* The file and line for this declaration, to
9990 assist in error message reporting. Since we
9991 called push_tinst_level above, we don't need to
9993 input_location
= DECL_SOURCE_LOCATION (t
);
9995 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9996 ++processing_template_decl
;
9997 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9998 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9999 --processing_template_decl
;
10001 if (TREE_CODE (r
) == TREE_VEC
)
10003 /* A capture pack became multiple fields. */
10005 len
= TREE_VEC_LENGTH (vec
);
10008 for (int i
= 0; i
< len
; ++i
)
10011 r
= TREE_VEC_ELT (vec
, i
);
10016 [t]he initialization (and any associated
10017 side-effects) of a static data member does
10018 not occur unless the static data member is
10019 itself used in a way that requires the
10020 definition of the static data member to
10023 Therefore, we do not substitute into the
10024 initialized for the static data member here. */
10025 finish_static_data_member_decl
10027 /*init=*/NULL_TREE
,
10028 /*init_const_expr_p=*/false,
10029 /*asmspec_tree=*/NULL_TREE
,
10031 /* Instantiate members marked with attribute used. */
10032 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10035 else if (TREE_CODE (r
) == FIELD_DECL
)
10037 /* Determine whether R has a valid type and can be
10038 completed later. If R is invalid, then its type
10039 is replaced by error_mark_node. */
10040 tree rtype
= TREE_TYPE (r
);
10041 if (can_complete_type_without_circularity (rtype
))
10042 complete_type (rtype
);
10044 if (TREE_CODE (r
) == FIELD_DECL
10045 && TREE_CODE (rtype
) == ARRAY_TYPE
10046 && COMPLETE_TYPE_P (TREE_TYPE (rtype
))
10047 && !COMPLETE_TYPE_P (rtype
))
10049 /* Flexible array mmembers of elements
10050 of complete type have an incomplete type
10051 and that's okay. */
10053 else if (!COMPLETE_TYPE_P (rtype
))
10055 cxx_incomplete_type_error (r
, rtype
);
10056 TREE_TYPE (r
) = error_mark_node
;
10060 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10061 such a thing will already have been added to the field
10062 list by tsubst_enum in finish_member_declaration in the
10063 CLASSTYPE_NESTED_UTDS case above. */
10064 if (!(TREE_CODE (r
) == TYPE_DECL
10065 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10066 && DECL_ARTIFICIAL (r
)))
10068 set_current_access_from_decl (r
);
10069 finish_member_declaration (r
);
10077 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10078 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10080 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10082 tree friend_type
= t
;
10083 bool adjust_processing_template_decl
= false;
10085 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10087 /* template <class T> friend class C; */
10088 friend_type
= tsubst_friend_class (friend_type
, args
);
10089 adjust_processing_template_decl
= true;
10091 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10093 /* template <class T> friend class C::D; */
10094 friend_type
= tsubst (friend_type
, args
,
10095 tf_warning_or_error
, NULL_TREE
);
10096 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10097 friend_type
= TREE_TYPE (friend_type
);
10098 adjust_processing_template_decl
= true;
10100 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10101 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10103 /* This could be either
10107 when dependent_type_p is false or
10109 template <class U> friend class T::C;
10112 friend_type
= tsubst (friend_type
, args
,
10113 tf_warning_or_error
, NULL_TREE
);
10114 /* Bump processing_template_decl for correct
10115 dependent_type_p calculation. */
10116 ++processing_template_decl
;
10117 if (dependent_type_p (friend_type
))
10118 adjust_processing_template_decl
= true;
10119 --processing_template_decl
;
10121 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
10122 && hidden_name_p (TYPE_NAME (friend_type
)))
10126 where C hasn't been declared yet. Let's lookup name
10127 from namespace scope directly, bypassing any name that
10128 come from dependent base class. */
10129 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10131 /* The call to xref_tag_from_type does injection for friend
10133 push_nested_namespace (ns
);
10135 xref_tag_from_type (friend_type
, NULL_TREE
,
10136 /*tag_scope=*/ts_current
);
10137 pop_nested_namespace (ns
);
10139 else if (uses_template_parms (friend_type
))
10140 /* friend class C<T>; */
10141 friend_type
= tsubst (friend_type
, args
,
10142 tf_warning_or_error
, NULL_TREE
);
10147 where C is already declared or
10149 friend class C<int>;
10151 We don't have to do anything in these cases. */
10153 if (adjust_processing_template_decl
)
10154 /* Trick make_friend_class into realizing that the friend
10155 we're adding is a template, not an ordinary class. It's
10156 important that we use make_friend_class since it will
10157 perform some error-checking and output cross-reference
10159 ++processing_template_decl
;
10161 if (friend_type
!= error_mark_node
)
10162 make_friend_class (type
, friend_type
, /*complain=*/false);
10164 if (adjust_processing_template_decl
)
10165 --processing_template_decl
;
10169 /* Build new DECL_FRIENDLIST. */
10172 /* The file and line for this declaration, to
10173 assist in error message reporting. Since we
10174 called push_tinst_level above, we don't need to
10176 input_location
= DECL_SOURCE_LOCATION (t
);
10178 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10180 ++processing_template_decl
;
10181 push_deferring_access_checks (dk_no_check
);
10184 r
= tsubst_friend_function (t
, args
);
10185 add_friend (type
, r
, /*complain=*/false);
10186 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10188 pop_deferring_access_checks ();
10189 --processing_template_decl
;
10197 /* Restore these before substituting into the lambda capture
10199 cp_unevaluated_operand
= saved_unevaluated_operand
;
10200 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10203 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10205 tree decl
= lambda_function (type
);
10208 if (!DECL_TEMPLATE_INFO (decl
)
10209 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10211 /* Set function_depth to avoid garbage collection. */
10213 instantiate_decl (decl
, false, false);
10217 /* We need to instantiate the capture list from the template
10218 after we've instantiated the closure members, but before we
10219 consider adding the conversion op. Also keep any captures
10220 that may have been added during instantiation of the op(). */
10221 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10223 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10224 args
, tf_warning_or_error
, NULL_TREE
,
10227 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10228 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10230 maybe_add_lambda_conv_op (type
);
10233 gcc_assert (errorcount
);
10236 /* Set the file and line number information to whatever is given for
10237 the class itself. This puts error messages involving generated
10238 implicit functions at a predictable point, and the same point
10239 that would be used for non-template classes. */
10240 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10242 unreverse_member_declarations (type
);
10243 finish_struct_1 (type
);
10244 TYPE_BEING_DEFINED (type
) = 0;
10246 /* We don't instantiate default arguments for member functions. 14.7.1:
10248 The implicit instantiation of a class template specialization causes
10249 the implicit instantiation of the declarations, but not of the
10250 definitions or default arguments, of the class member functions,
10251 member classes, static data members and member templates.... */
10253 /* Some typedefs referenced from within the template code need to be access
10254 checked at template instantiation time, i.e now. These types were
10255 added to the template at parsing time. Let's get those and perform
10256 the access checks then. */
10257 perform_typedefs_access_check (pattern
, args
);
10258 perform_deferred_access_checks (tf_warning_or_error
);
10259 pop_nested_class ();
10260 maximum_field_alignment
= saved_maximum_field_alignment
;
10262 pop_from_top_level ();
10263 pop_deferring_access_checks ();
10264 pop_tinst_level ();
10266 /* The vtable for a template class can be emitted in any translation
10267 unit in which the class is instantiated. When there is no key
10268 method, however, finish_struct_1 will already have added TYPE to
10269 the keyed_classes list. */
10270 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10271 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
10276 /* Wrapper for instantiate_class_template_1. */
10279 instantiate_class_template (tree type
)
10282 timevar_push (TV_TEMPLATE_INST
);
10283 ret
= instantiate_class_template_1 (type
);
10284 timevar_pop (TV_TEMPLATE_INST
);
10289 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10295 else if (TYPE_P (t
))
10296 r
= tsubst (t
, args
, complain
, in_decl
);
10299 if (!(complain
& tf_warning
))
10300 ++c_inhibit_evaluation_warnings
;
10301 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10302 /*integral_constant_expression_p=*/true);
10303 if (!(complain
& tf_warning
))
10304 --c_inhibit_evaluation_warnings
;
10309 /* Given a function parameter pack TMPL_PARM and some function parameters
10310 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10311 and set *SPEC_P to point at the next point in the list. */
10314 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10316 /* Collect all of the extra "packed" parameters into an
10320 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10321 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10322 tree spec_parm
= *spec_p
;
10325 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10327 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10330 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10331 parmvec
= make_tree_vec (len
);
10332 parmtypevec
= make_tree_vec (len
);
10333 spec_parm
= *spec_p
;
10334 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10336 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10337 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
10340 /* Build the argument packs. */
10341 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10342 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
10343 TREE_TYPE (argpack
) = argtypepack
;
10344 *spec_p
= spec_parm
;
10349 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10350 NONTYPE_ARGUMENT_PACK. */
10353 make_fnparm_pack (tree spec_parm
)
10355 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10358 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10359 pack expansion with no extra args, 2 if it has extra args, or 0
10360 if it is not a pack expansion. */
10363 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10365 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10366 if (i
>= TREE_VEC_LENGTH (vec
))
10368 tree elt
= TREE_VEC_ELT (vec
, i
);
10370 /* A decl pack is itself an expansion. */
10371 elt
= TREE_TYPE (elt
);
10372 if (!PACK_EXPANSION_P (elt
))
10374 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
10380 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10383 make_argument_pack_select (tree arg_pack
, unsigned index
)
10385 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
10387 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
10388 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10393 /* This is a subroutine of tsubst_pack_expansion.
10395 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10396 mechanism to store the (non complete list of) arguments of the
10397 substitution and return a non substituted pack expansion, in order
10398 to wait for when we have enough arguments to really perform the
10402 use_pack_expansion_extra_args_p (tree parm_packs
,
10404 bool has_empty_arg
)
10406 /* If one pack has an expansion and another pack has a normal
10407 argument or if one pack has an empty argument and an another
10408 one hasn't then tsubst_pack_expansion cannot perform the
10409 substitution and need to fall back on the
10410 PACK_EXPANSION_EXTRA mechanism. */
10411 if (parm_packs
== NULL_TREE
)
10413 else if (has_empty_arg
)
10416 bool has_expansion_arg
= false;
10417 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
10419 bool has_non_expansion_arg
= false;
10420 for (tree parm_pack
= parm_packs
;
10422 parm_pack
= TREE_CHAIN (parm_pack
))
10424 tree arg
= TREE_VALUE (parm_pack
);
10426 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
10428 /* We can't substitute a pack expansion with extra args into
10432 has_expansion_arg
= true;
10434 has_non_expansion_arg
= true;
10437 if (has_expansion_arg
&& has_non_expansion_arg
)
10443 /* [temp.variadic]/6 says that:
10445 The instantiation of a pack expansion [...]
10446 produces a list E1,E2, ..., En, where N is the number of elements
10447 in the pack expansion parameters.
10449 This subroutine of tsubst_pack_expansion produces one of these Ei.
10451 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10452 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10453 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10454 INDEX is the index 'i' of the element Ei to produce. ARGS,
10455 COMPLAIN, and IN_DECL are the same parameters as for the
10456 tsubst_pack_expansion function.
10458 The function returns the resulting Ei upon successful completion,
10459 or error_mark_node.
10461 Note that this function possibly modifies the ARGS parameter, so
10462 it's the responsibility of the caller to restore it. */
10465 gen_elem_of_pack_expansion_instantiation (tree pattern
,
10468 tree args
/* This parm gets
10470 tsubst_flags_t complain
,
10474 bool ith_elem_is_expansion
= false;
10476 /* For each parameter pack, change the substitution of the parameter
10477 pack to the ith argument in its argument pack, then expand the
10479 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
10481 tree parm
= TREE_PURPOSE (pack
);
10482 tree arg_pack
= TREE_VALUE (pack
);
10483 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
10485 ith_elem_is_expansion
|=
10486 argument_pack_element_is_expansion_p (arg_pack
, index
);
10488 /* Select the Ith argument from the pack. */
10489 if (TREE_CODE (parm
) == PARM_DECL
10490 || TREE_CODE (parm
) == FIELD_DECL
)
10494 aps
= make_argument_pack_select (arg_pack
, index
);
10495 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
10496 return error_mark_node
;
10497 register_local_specialization (aps
, parm
);
10500 aps
= retrieve_local_specialization (parm
);
10505 template_parm_level_and_index (parm
, &level
, &idx
);
10509 aps
= make_argument_pack_select (arg_pack
, index
);
10510 /* Update the corresponding argument. */
10511 TMPL_ARG (args
, level
, idx
) = aps
;
10514 /* Re-use the ARGUMENT_PACK_SELECT. */
10515 aps
= TMPL_ARG (args
, level
, idx
);
10517 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10520 /* Substitute into the PATTERN with the (possibly altered)
10522 if (pattern
== in_decl
)
10523 /* Expanding a fixed parameter pack from
10524 coerce_template_parameter_pack. */
10525 t
= tsubst_decl (pattern
, args
, complain
);
10526 else if (pattern
== error_mark_node
)
10527 t
= error_mark_node
;
10528 else if (constraint_p (pattern
))
10530 if (processing_template_decl
)
10531 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
10533 t
= (constraints_satisfied_p (pattern
, args
)
10534 ? boolean_true_node
: boolean_false_node
);
10536 else if (!TYPE_P (pattern
))
10537 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10538 /*integral_constant_expression_p=*/false);
10540 t
= tsubst (pattern
, args
, complain
, in_decl
);
10542 /* If the Ith argument pack element is a pack expansion, then
10543 the Ith element resulting from the substituting is going to
10544 be a pack expansion as well. */
10545 if (ith_elem_is_expansion
)
10546 t
= make_pack_expansion (t
);
10551 /* When the unexpanded parameter pack in a fold expression expands to an empty
10552 sequence, the value of the expression is as follows; the program is
10553 ill-formed if the operator is not listed in this table.
10564 expand_empty_fold (tree t
, tsubst_flags_t complain
)
10566 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
10567 if (!FOLD_EXPR_MODIFY_P (t
))
10571 return integer_one_node
;
10573 return integer_zero_node
;
10575 return integer_minus_one_node
;
10577 return integer_zero_node
;
10578 case TRUTH_ANDIF_EXPR
:
10579 return boolean_true_node
;
10580 case TRUTH_ORIF_EXPR
:
10581 return boolean_false_node
;
10582 case COMPOUND_EXPR
:
10588 if (complain
& tf_error
)
10589 error_at (location_of (t
),
10590 "fold of empty expansion over %O", code
);
10591 return error_mark_node
;
10594 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10595 form an expression that combines the two terms using the
10599 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
10601 tree op
= FOLD_EXPR_OP (t
);
10602 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
10604 // Handle compound assignment operators.
10605 if (FOLD_EXPR_MODIFY_P (t
))
10606 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
10610 case COMPOUND_EXPR
:
10611 return build_x_compound_expr (input_location
, left
, right
, complain
);
10613 return build_m_component_ref (left
, right
, complain
);
10615 return build_x_binary_op (input_location
, code
,
10616 left
, TREE_CODE (left
),
10617 right
, TREE_CODE (right
),
10623 /* Substitute ARGS into the pack of a fold expression T. */
10626 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10628 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
10631 /* Substitute ARGS into the pack of a fold expression T. */
10634 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10636 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
10639 /* Expand a PACK of arguments into a grouped as left fold.
10640 Given a pack containing elements A0, A1, ..., An and an
10641 operator @, this builds the expression:
10643 ((A0 @ A1) @ A2) ... @ An
10645 Note that PACK must not be empty.
10647 The operator is defined by the original fold expression T. */
10650 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
10652 tree left
= TREE_VEC_ELT (pack
, 0);
10653 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
10655 tree right
= TREE_VEC_ELT (pack
, i
);
10656 left
= fold_expression (t
, left
, right
, complain
);
10661 /* Substitute into a unary left fold expression. */
10664 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
10667 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10668 if (pack
== error_mark_node
)
10669 return error_mark_node
;
10670 if (TREE_VEC_LENGTH (pack
) == 0)
10671 return expand_empty_fold (t
, complain
);
10673 return expand_left_fold (t
, pack
, complain
);
10676 /* Substitute into a binary left fold expression.
10678 Do ths by building a single (non-empty) vector of argumnts and
10679 building the expression from those elements. */
10682 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
10685 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10686 if (pack
== error_mark_node
)
10687 return error_mark_node
;
10688 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
10689 if (init
== error_mark_node
)
10690 return error_mark_node
;
10692 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
10693 TREE_VEC_ELT (vec
, 0) = init
;
10694 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
10695 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
10697 return expand_left_fold (t
, vec
, complain
);
10700 /* Expand a PACK of arguments into a grouped as right fold.
10701 Given a pack containing elementns A0, A1, ..., and an
10702 operator @, this builds the expression:
10704 A0@ ... (An-2 @ (An-1 @ An))
10706 Note that PACK must not be empty.
10708 The operator is defined by the original fold expression T. */
10711 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
10713 // Build the expression.
10714 int n
= TREE_VEC_LENGTH (pack
);
10715 tree right
= TREE_VEC_ELT (pack
, n
- 1);
10716 for (--n
; n
!= 0; --n
)
10718 tree left
= TREE_VEC_ELT (pack
, n
- 1);
10719 right
= fold_expression (t
, left
, right
, complain
);
10724 /* Substitute into a unary right fold expression. */
10727 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
10730 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10731 if (pack
== error_mark_node
)
10732 return error_mark_node
;
10733 if (TREE_VEC_LENGTH (pack
) == 0)
10734 return expand_empty_fold (t
, complain
);
10736 return expand_right_fold (t
, pack
, complain
);
10739 /* Substitute into a binary right fold expression.
10741 Do ths by building a single (non-empty) vector of arguments and
10742 building the expression from those elements. */
10745 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
10748 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10749 if (pack
== error_mark_node
)
10750 return error_mark_node
;
10751 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
10752 if (init
== error_mark_node
)
10753 return error_mark_node
;
10755 int n
= TREE_VEC_LENGTH (pack
);
10756 tree vec
= make_tree_vec (n
+ 1);
10757 for (int i
= 0; i
< n
; ++i
)
10758 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
10759 TREE_VEC_ELT (vec
, n
) = init
;
10761 return expand_right_fold (t
, vec
, complain
);
10765 /* Substitute ARGS into T, which is an pack expansion
10766 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10767 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10768 (if only a partial substitution could be performed) or
10769 ERROR_MARK_NODE if there was an error. */
10771 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
10775 tree pack
, packs
= NULL_TREE
;
10776 bool unsubstituted_packs
= false;
10779 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
10780 bool need_local_specializations
= false;
10783 gcc_assert (PACK_EXPANSION_P (t
));
10784 pattern
= PACK_EXPANSION_PATTERN (t
);
10786 /* Add in any args remembered from an earlier partial instantiation. */
10787 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
10789 levels
= TMPL_ARGS_DEPTH (args
);
10791 /* Determine the argument packs that will instantiate the parameter
10792 packs used in the expansion expression. While we're at it,
10793 compute the number of arguments to be expanded and make sure it
10795 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
10796 pack
= TREE_CHAIN (pack
))
10798 tree parm_pack
= TREE_VALUE (pack
);
10799 tree arg_pack
= NULL_TREE
;
10800 tree orig_arg
= NULL_TREE
;
10803 if (TREE_CODE (parm_pack
) == BASES
)
10805 if (BASES_DIRECT (parm_pack
))
10806 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10807 args
, complain
, in_decl
, false));
10809 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10810 args
, complain
, in_decl
, false));
10812 if (TREE_CODE (parm_pack
) == PARM_DECL
)
10814 /* We know we have correct local_specializations if this
10815 expansion is at function scope, or if we're dealing with a
10816 local parameter in a requires expression; for the latter,
10817 tsubst_requires_expr set it up appropriately. */
10818 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
10819 arg_pack
= retrieve_local_specialization (parm_pack
);
10821 /* We can't rely on local_specializations for a parameter
10822 name used later in a function declaration (such as in a
10823 late-specified return type). Even if it exists, it might
10824 have the wrong value for a recursive call. */
10825 need_local_specializations
= true;
10829 /* This parameter pack was used in an unevaluated context. Just
10830 make a dummy decl, since it's only used for its type. */
10831 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
10832 if (arg_pack
&& DECL_PACK_P (arg_pack
))
10833 /* Partial instantiation of the parm_pack, we can't build
10834 up an argument pack yet. */
10835 arg_pack
= NULL_TREE
;
10837 arg_pack
= make_fnparm_pack (arg_pack
);
10840 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
10841 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
10845 template_parm_level_and_index (parm_pack
, &level
, &idx
);
10847 if (level
<= levels
)
10848 arg_pack
= TMPL_ARG (args
, level
, idx
);
10851 orig_arg
= arg_pack
;
10852 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
10853 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
10855 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
10856 /* This can only happen if we forget to expand an argument
10857 pack somewhere else. Just return an error, silently. */
10859 result
= make_tree_vec (1);
10860 TREE_VEC_ELT (result
, 0) = error_mark_node
;
10867 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
10869 /* Don't bother trying to do a partial substitution with
10870 incomplete packs; we'll try again after deduction. */
10871 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
10876 else if (len
!= my_len
)
10878 if (!(complain
& tf_error
))
10879 /* Fail quietly. */;
10880 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
10881 error ("mismatched argument pack lengths while expanding "
10885 error ("mismatched argument pack lengths while expanding "
10888 return error_mark_node
;
10891 /* Keep track of the parameter packs and their corresponding
10893 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
10894 TREE_TYPE (packs
) = orig_arg
;
10898 /* We can't substitute for this parameter pack. We use a flag as
10899 well as the missing_level counter because function parameter
10900 packs don't have a level. */
10901 unsubstituted_packs
= true;
10905 /* If the expansion is just T..., return the matching argument pack, unless
10906 we need to call convert_from_reference on all the elements. This is an
10907 important optimization; see c++/68422. */
10908 if (!unsubstituted_packs
10909 && TREE_PURPOSE (packs
) == pattern
)
10911 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
10912 /* Types need no adjustment, nor does sizeof..., and if we still have
10913 some pack expansion args we won't do anything yet. */
10914 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
10915 || PACK_EXPANSION_SIZEOF_P (t
)
10916 || pack_expansion_args_count (args
))
10918 /* Also optimize expression pack expansions if we can tell that the
10919 elements won't have reference type. */
10920 tree type
= TREE_TYPE (pattern
);
10921 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
10922 && !PACK_EXPANSION_P (type
)
10923 && !WILDCARD_TYPE_P (type
))
10925 /* Otherwise use the normal path so we get convert_from_reference. */
10928 /* We cannot expand this expansion expression, because we don't have
10929 all of the argument packs we need. */
10930 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
10932 /* We got some full packs, but we can't substitute them in until we
10933 have values for all the packs. So remember these until then. */
10935 t
= make_pack_expansion (pattern
);
10936 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
10939 else if (unsubstituted_packs
)
10941 /* There were no real arguments, we're just replacing a parameter
10942 pack with another version of itself. Substitute into the
10943 pattern and return a PACK_EXPANSION_*. The caller will need to
10945 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
10946 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10947 /*integral_constant_expression_p=*/false);
10949 t
= tsubst (pattern
, args
, complain
, in_decl
);
10950 t
= make_pack_expansion (t
);
10954 gcc_assert (len
>= 0);
10956 if (need_local_specializations
)
10958 /* We're in a late-specified return type, so create our own local
10959 specializations map; the current map is either NULL or (in the
10960 case of recursive unification) might have bindings that we don't
10961 want to use or alter. */
10962 saved_local_specializations
= local_specializations
;
10963 local_specializations
= new hash_map
<tree
, tree
>;
10966 /* For each argument in each argument pack, substitute into the
10968 result
= make_tree_vec (len
);
10969 for (i
= 0; i
< len
; ++i
)
10971 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
10975 TREE_VEC_ELT (result
, i
) = t
;
10976 if (t
== error_mark_node
)
10978 result
= error_mark_node
;
10983 /* Update ARGS to restore the substitution from parameter packs to
10984 their argument packs. */
10985 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
10987 tree parm
= TREE_PURPOSE (pack
);
10989 if (TREE_CODE (parm
) == PARM_DECL
10990 || TREE_CODE (parm
) == FIELD_DECL
)
10991 register_local_specialization (TREE_TYPE (pack
), parm
);
10996 if (TREE_VALUE (pack
) == NULL_TREE
)
10999 template_parm_level_and_index (parm
, &level
, &idx
);
11001 /* Update the corresponding argument. */
11002 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11003 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11006 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11010 if (need_local_specializations
)
11012 delete local_specializations
;
11013 local_specializations
= saved_local_specializations
;
11019 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11020 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11021 parameter packs; all parms generated from a function parameter pack will
11022 have the same DECL_PARM_INDEX. */
11025 get_pattern_parm (tree parm
, tree tmpl
)
11027 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11030 if (DECL_ARTIFICIAL (parm
))
11032 for (patparm
= DECL_ARGUMENTS (pattern
);
11033 patparm
; patparm
= DECL_CHAIN (patparm
))
11034 if (DECL_ARTIFICIAL (patparm
)
11035 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11040 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11041 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11042 gcc_assert (DECL_PARM_INDEX (patparm
)
11043 == DECL_PARM_INDEX (parm
));
11049 /* Substitute ARGS into the vector or list of template arguments T. */
11052 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11055 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11058 if (t
== error_mark_node
)
11059 return error_mark_node
;
11061 len
= TREE_VEC_LENGTH (t
);
11062 elts
= XALLOCAVEC (tree
, len
);
11064 for (i
= 0; i
< len
; i
++)
11066 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11069 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11070 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11071 else if (PACK_EXPANSION_P (orig_arg
))
11073 /* Substitute into an expansion expression. */
11074 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11076 if (TREE_CODE (new_arg
) == TREE_VEC
)
11077 /* Add to the expanded length adjustment the number of
11078 expanded arguments. We subtract one from this
11079 measurement, because the argument pack expression
11080 itself is already counted as 1 in
11081 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11082 the argument pack is empty. */
11083 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11085 else if (ARGUMENT_PACK_P (orig_arg
))
11087 /* Substitute into each of the arguments. */
11088 new_arg
= TYPE_P (orig_arg
)
11089 ? cxx_make_type (TREE_CODE (orig_arg
))
11090 : make_node (TREE_CODE (orig_arg
));
11092 SET_ARGUMENT_PACK_ARGS (
11094 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11095 args
, complain
, in_decl
));
11097 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
11098 new_arg
= error_mark_node
;
11100 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
11101 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
11102 complain
, in_decl
);
11103 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11105 if (TREE_TYPE (new_arg
) == error_mark_node
)
11106 new_arg
= error_mark_node
;
11110 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11112 if (new_arg
== error_mark_node
)
11113 return error_mark_node
;
11116 if (new_arg
!= orig_arg
)
11123 /* Make space for the expanded arguments coming from template
11125 t
= make_tree_vec (len
+ expanded_len_adjust
);
11126 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11127 arguments for a member template.
11128 In that case each TREE_VEC in ORIG_T represents a level of template
11129 arguments, and ORIG_T won't carry any non defaulted argument count.
11130 It will rather be the nested TREE_VECs that will carry one.
11131 In other words, ORIG_T carries a non defaulted argument count only
11132 if it doesn't contain any nested TREE_VEC. */
11133 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11135 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11136 count
+= expanded_len_adjust
;
11137 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11139 for (i
= 0, out
= 0; i
< len
; i
++)
11141 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11142 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11143 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11147 /* Now expand the template argument pack "in place". */
11148 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11149 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11153 TREE_VEC_ELT (t
, out
) = elts
[i
];
11161 /* Return the result of substituting ARGS into the template parameters
11162 given by PARMS. If there are m levels of ARGS and m + n levels of
11163 PARMS, then the result will contain n levels of PARMS. For
11164 example, if PARMS is `template <class T> template <class U>
11165 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11166 result will be `template <int*, double, class V>'. */
11169 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11171 tree r
= NULL_TREE
;
11174 /* When substituting into a template, we must set
11175 PROCESSING_TEMPLATE_DECL as the template parameters may be
11176 dependent if they are based on one-another, and the dependency
11177 predicates are short-circuit outside of templates. */
11178 ++processing_template_decl
;
11180 for (new_parms
= &r
;
11181 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11182 new_parms
= &(TREE_CHAIN (*new_parms
)),
11183 parms
= TREE_CHAIN (parms
))
11186 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
11189 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11193 if (parms
== error_mark_node
)
11196 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
11198 if (tuple
== error_mark_node
)
11201 TREE_VEC_ELT (new_vec
, i
) =
11202 tsubst_template_parm (tuple
, args
, complain
);
11206 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11207 - TMPL_ARGS_DEPTH (args
)),
11208 new_vec
, NULL_TREE
);
11211 --processing_template_decl
;
11216 /* Return the result of substituting ARGS into one template parameter
11217 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11218 parameter and which TREE_PURPOSE is the default argument of the
11219 template parameter. */
11222 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11224 tree default_value
, parm_decl
;
11226 if (args
== NULL_TREE
11228 || t
== error_mark_node
)
11231 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11233 default_value
= TREE_PURPOSE (t
);
11234 parm_decl
= TREE_VALUE (t
);
11236 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11237 if (TREE_CODE (parm_decl
) == PARM_DECL
11238 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11239 parm_decl
= error_mark_node
;
11240 default_value
= tsubst_template_arg (default_value
, args
,
11241 complain
, NULL_TREE
);
11243 return build_tree_list (default_value
, parm_decl
);
11246 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11247 type T. If T is not an aggregate or enumeration type, it is
11248 handled as if by tsubst. IN_DECL is as for tsubst. If
11249 ENTERING_SCOPE is nonzero, T is the context for a template which
11250 we are presently tsubst'ing. Return the substituted value. */
11253 tsubst_aggr_type (tree t
,
11255 tsubst_flags_t complain
,
11257 int entering_scope
)
11259 if (t
== NULL_TREE
)
11262 switch (TREE_CODE (t
))
11265 if (TYPE_PTRMEMFUNC_P (t
))
11266 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11268 /* Else fall through. */
11269 case ENUMERAL_TYPE
:
11271 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11276 int saved_unevaluated_operand
;
11277 int saved_inhibit_evaluation_warnings
;
11279 /* In "sizeof(X<I>)" we need to evaluate "I". */
11280 saved_unevaluated_operand
= cp_unevaluated_operand
;
11281 cp_unevaluated_operand
= 0;
11282 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11283 c_inhibit_evaluation_warnings
= 0;
11285 /* First, determine the context for the type we are looking
11287 context
= TYPE_CONTEXT (t
);
11288 if (context
&& TYPE_P (context
))
11290 context
= tsubst_aggr_type (context
, args
, complain
,
11291 in_decl
, /*entering_scope=*/1);
11292 /* If context is a nested class inside a class template,
11293 it may still need to be instantiated (c++/33959). */
11294 context
= complete_type (context
);
11297 /* Then, figure out what arguments are appropriate for the
11298 type we are trying to find. For example, given:
11300 template <class T> struct S;
11301 template <class T, class U> void f(T, U) { S<U> su; }
11303 and supposing that we are instantiating f<int, double>,
11304 then our ARGS will be {int, double}, but, when looking up
11305 S we only want {double}. */
11306 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
11307 complain
, in_decl
);
11308 if (argvec
== error_mark_node
)
11309 r
= error_mark_node
;
11312 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
11313 entering_scope
, complain
);
11314 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11317 cp_unevaluated_operand
= saved_unevaluated_operand
;
11318 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11323 /* This is not a template type, so there's nothing to do. */
11327 return tsubst (t
, args
, complain
, in_decl
);
11331 /* Substitute into the default argument ARG (a default argument for
11332 FN), which has the indicated TYPE. */
11335 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
11337 tree saved_class_ptr
= NULL_TREE
;
11338 tree saved_class_ref
= NULL_TREE
;
11339 int errs
= errorcount
+ sorrycount
;
11341 /* This can happen in invalid code. */
11342 if (TREE_CODE (arg
) == DEFAULT_ARG
)
11345 /* This default argument came from a template. Instantiate the
11346 default argument here, not in tsubst. In the case of
11355 we must be careful to do name lookup in the scope of S<T>,
11356 rather than in the current class. */
11357 push_access_scope (fn
);
11358 /* The "this" pointer is not valid in a default argument. */
11361 saved_class_ptr
= current_class_ptr
;
11362 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
11363 saved_class_ref
= current_class_ref
;
11364 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
11367 push_deferring_access_checks(dk_no_deferred
);
11368 /* The default argument expression may cause implicitly defined
11369 member functions to be synthesized, which will result in garbage
11370 collection. We must treat this situation as if we were within
11371 the body of function so as to avoid collecting live data on the
11374 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
11375 complain
, NULL_TREE
,
11376 /*integral_constant_expression_p=*/false);
11378 pop_deferring_access_checks();
11380 /* Restore the "this" pointer. */
11383 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
11384 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
11387 if (errorcount
+sorrycount
> errs
11388 && (complain
& tf_warning_or_error
))
11389 inform (input_location
,
11390 " when instantiating default argument for call to %D", fn
);
11392 /* Make sure the default argument is reasonable. */
11393 arg
= check_default_argument (type
, arg
, complain
);
11395 pop_access_scope (fn
);
11400 /* Substitute into all the default arguments for FN. */
11403 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
11408 tmpl_args
= DECL_TI_ARGS (fn
);
11410 /* If this function is not yet instantiated, we certainly don't need
11411 its default arguments. */
11412 if (uses_template_parms (tmpl_args
))
11414 /* Don't do this again for clones. */
11415 if (DECL_CLONED_FUNCTION_P (fn
))
11418 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
11420 arg
= TREE_CHAIN (arg
))
11421 if (TREE_PURPOSE (arg
))
11422 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
11424 TREE_PURPOSE (arg
),
11428 /* Substitute the ARGS into the T, which is a _DECL. Return the
11429 result of the substitution. Issue error and warning messages under
11430 control of COMPLAIN. */
11433 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
11435 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11436 location_t saved_loc
;
11437 tree r
= NULL_TREE
;
11439 hashval_t hash
= 0;
11441 /* Set the filename and linenumber to improve error-reporting. */
11442 saved_loc
= input_location
;
11443 input_location
= DECL_SOURCE_LOCATION (t
);
11445 switch (TREE_CODE (t
))
11447 case TEMPLATE_DECL
:
11449 /* We can get here when processing a member function template,
11450 member class template, or template template parameter. */
11451 tree decl
= DECL_TEMPLATE_RESULT (t
);
11456 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11458 /* Template template parameter is treated here. */
11459 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11460 if (new_type
== error_mark_node
)
11461 r
= error_mark_node
;
11462 /* If we get a real template back, return it. This can happen in
11463 the context of most_specialized_partial_spec. */
11464 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
11467 /* The new TEMPLATE_DECL was built in
11468 reduce_template_parm_level. */
11469 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
11473 /* We might already have an instance of this template.
11474 The ARGS are for the surrounding class type, so the
11475 full args contain the tsubst'd args for the context,
11476 plus the innermost args from the template decl. */
11477 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
11478 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
11479 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
11480 /* Because this is a template, the arguments will still be
11481 dependent, even after substitution. If
11482 PROCESSING_TEMPLATE_DECL is not set, the dependency
11483 predicates will short-circuit. */
11484 ++processing_template_decl
;
11485 full_args
= tsubst_template_args (tmpl_args
, args
,
11486 complain
, in_decl
);
11487 --processing_template_decl
;
11488 if (full_args
== error_mark_node
)
11489 RETURN (error_mark_node
);
11491 /* If this is a default template template argument,
11492 tsubst might not have changed anything. */
11493 if (full_args
== tmpl_args
)
11496 hash
= hash_tmpl_and_args (t
, full_args
);
11497 spec
= retrieve_specialization (t
, full_args
, hash
);
11498 if (spec
!= NULL_TREE
)
11504 /* Make a new template decl. It will be similar to the
11505 original, but will record the current template arguments.
11506 We also create a new function declaration, which is just
11507 like the old one, but points to this new template, rather
11508 than the old one. */
11510 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
11511 DECL_CHAIN (r
) = NULL_TREE
;
11513 // Build new template info linking to the original template decl.
11514 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11516 if (TREE_CODE (decl
) == TYPE_DECL
11517 && !TYPE_DECL_ALIAS_P (decl
))
11520 ++processing_template_decl
;
11521 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11522 --processing_template_decl
;
11523 if (new_type
== error_mark_node
)
11524 RETURN (error_mark_node
);
11526 TREE_TYPE (r
) = new_type
;
11527 /* For a partial specialization, we need to keep pointing to
11528 the primary template. */
11529 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
11530 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
11531 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
11532 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
11533 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
11538 ++processing_template_decl
;
11539 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
11540 --processing_template_decl
;
11541 if (new_decl
== error_mark_node
)
11542 RETURN (error_mark_node
);
11544 DECL_TEMPLATE_RESULT (r
) = new_decl
;
11545 DECL_TI_TEMPLATE (new_decl
) = r
;
11546 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
11547 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
11548 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
11551 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11552 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
11553 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
11555 /* The template parameters for this new template are all the
11556 template parameters for the old template, except the
11557 outermost level of parameters. */
11558 DECL_TEMPLATE_PARMS (r
)
11559 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
11562 if (PRIMARY_TEMPLATE_P (t
))
11563 DECL_PRIMARY_TEMPLATE (r
) = r
;
11565 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
))
11566 /* Record this non-type partial instantiation. */
11567 register_specialization (r
, t
,
11568 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
11573 case FUNCTION_DECL
:
11576 tree argvec
= NULL_TREE
;
11584 /* Nobody should be tsubst'ing into non-template functions. */
11585 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
11587 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
11592 /* If T is not dependent, just return it. We have to
11593 increment PROCESSING_TEMPLATE_DECL because
11594 value_dependent_expression_p assumes that nothing is
11595 dependent when PROCESSING_TEMPLATE_DECL is zero. */
11596 ++processing_template_decl
;
11597 dependent_p
= value_dependent_expression_p (t
);
11598 --processing_template_decl
;
11602 /* Calculate the most general template of which R is a
11603 specialization, and the complete set of arguments used to
11605 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
11606 argvec
= tsubst_template_args (DECL_TI_ARGS
11607 (DECL_TEMPLATE_RESULT
11608 (DECL_TI_TEMPLATE (t
))),
11609 args
, complain
, in_decl
);
11610 if (argvec
== error_mark_node
)
11611 RETURN (error_mark_node
);
11613 /* Check to see if we already have this specialization. */
11614 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11615 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11623 /* We can see more levels of arguments than parameters if
11624 there was a specialization of a member template, like
11627 template <class T> struct S { template <class U> void f(); }
11628 template <> template <class U> void S<int>::f(U);
11630 Here, we'll be substituting into the specialization,
11631 because that's where we can find the code we actually
11632 want to generate, but we'll have enough arguments for
11633 the most general template.
11635 We also deal with the peculiar case:
11637 template <class T> struct S {
11638 template <class U> friend void f();
11640 template <class U> void f() {}
11642 template void f<double>();
11644 Here, the ARGS for the instantiation of will be {int,
11645 double}. But, we only need as many ARGS as there are
11646 levels of template parameters in CODE_PATTERN. We are
11647 careful not to get fooled into reducing the ARGS in
11650 template <class T> struct S { template <class U> void f(U); }
11651 template <class T> template <> void S<T>::f(int) {}
11653 which we can spot because the pattern will be a
11654 specialization in this case. */
11655 args_depth
= TMPL_ARGS_DEPTH (args
);
11657 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
11658 if (args_depth
> parms_depth
11659 && !DECL_TEMPLATE_SPECIALIZATION (t
))
11660 args
= get_innermost_template_args (args
, parms_depth
);
11664 /* This special case arises when we have something like this:
11666 template <class T> struct S {
11667 friend void f<int>(int, double);
11670 Here, the DECL_TI_TEMPLATE for the friend declaration
11671 will be an IDENTIFIER_NODE. We are being called from
11672 tsubst_friend_function, and we want only to create a
11673 new decl (R) with appropriate types so that we can call
11674 determine_specialization. */
11675 gen_tmpl
= NULL_TREE
;
11678 if (DECL_CLASS_SCOPE_P (t
))
11680 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
11684 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
11685 complain
, t
, /*entering_scope=*/1);
11690 ctx
= DECL_CONTEXT (t
);
11692 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11693 if (type
== error_mark_node
)
11694 RETURN (error_mark_node
);
11696 /* If we hit excessive deduction depth, the type is bogus even if
11697 it isn't error_mark_node, so don't build a decl. */
11698 if (excessive_deduction_depth
)
11699 RETURN (error_mark_node
);
11701 /* We do NOT check for matching decls pushed separately at this
11702 point, as they may not represent instantiations of this
11703 template, and in any case are considered separate under the
11706 DECL_USE_TEMPLATE (r
) = 0;
11707 TREE_TYPE (r
) = type
;
11708 /* Clear out the mangled name and RTL for the instantiation. */
11709 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11710 SET_DECL_RTL (r
, NULL
);
11711 /* Leave DECL_INITIAL set on deleted instantiations. */
11712 if (!DECL_DELETED_FN (r
))
11713 DECL_INITIAL (r
) = NULL_TREE
;
11714 DECL_CONTEXT (r
) = ctx
;
11716 /* OpenMP UDRs have the only argument a reference to the declared
11717 type. We want to diagnose if the declared type is a reference,
11718 which is invalid, but as references to references are usually
11719 quietly merged, diagnose it here. */
11720 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
11723 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
11724 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
11725 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
11726 error_at (DECL_SOURCE_LOCATION (t
),
11727 "reference type %qT in "
11728 "%<#pragma omp declare reduction%>", argtype
);
11729 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
11730 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
11734 if (member
&& DECL_CONV_FN_P (r
))
11735 /* Type-conversion operator. Reconstruct the name, in
11736 case it's the name of one of the template's parameters. */
11737 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
11739 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
11741 DECL_RESULT (r
) = NULL_TREE
;
11743 TREE_STATIC (r
) = 0;
11744 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
11745 DECL_EXTERNAL (r
) = 1;
11746 /* If this is an instantiation of a function with internal
11747 linkage, we already know what object file linkage will be
11748 assigned to the instantiation. */
11749 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
11750 DECL_DEFER_OUTPUT (r
) = 0;
11751 DECL_CHAIN (r
) = NULL_TREE
;
11752 DECL_PENDING_INLINE_INFO (r
) = 0;
11753 DECL_PENDING_INLINE_P (r
) = 0;
11754 DECL_SAVED_TREE (r
) = NULL_TREE
;
11755 DECL_STRUCT_FUNCTION (r
) = NULL
;
11757 /* We'll re-clone as appropriate in instantiate_template. */
11758 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
11760 /* If we aren't complaining now, return on error before we register
11761 the specialization so that we'll complain eventually. */
11762 if ((complain
& tf_error
) == 0
11763 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11764 && !grok_op_properties (r
, /*complain=*/false))
11765 RETURN (error_mark_node
);
11767 /* When instantiating a constrained member, substitute
11768 into the constraints to create a new constraint. */
11769 if (tree ci
= get_constraints (t
))
11772 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
11773 set_constraints (r
, ci
);
11776 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11777 this in the special friend case mentioned above where
11778 GEN_TMPL is NULL. */
11781 DECL_TEMPLATE_INFO (r
)
11782 = build_template_info (gen_tmpl
, argvec
);
11783 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11786 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11788 /* We instantiated this while substituting into
11789 the type earlier (template/friend54.C). */
11792 /* We're not supposed to instantiate default arguments
11793 until they are called, for a template. But, for a
11796 template <class T> void f ()
11797 { extern void g(int i = T()); }
11799 we should do the substitution when the template is
11800 instantiated. We handle the member function case in
11801 instantiate_class_template since the default arguments
11802 might refer to other members of the class. */
11804 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
11805 && !uses_template_parms (argvec
))
11806 tsubst_default_arguments (r
, complain
);
11809 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
11811 /* Copy the list of befriending classes. */
11812 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
11814 friends
= &TREE_CHAIN (*friends
))
11816 *friends
= copy_node (*friends
);
11817 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
11822 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
11824 maybe_retrofit_in_chrg (r
);
11825 if (DECL_CONSTRUCTOR_P (r
))
11826 grok_ctor_properties (ctx
, r
);
11827 if (DECL_INHERITED_CTOR_BASE (r
))
11828 deduce_inheriting_ctor (r
);
11829 /* If this is an instantiation of a member template, clone it.
11830 If it isn't, that'll be handled by
11831 clone_constructors_and_destructors. */
11832 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
11833 clone_function_decl (r
, /*update_method_vec_p=*/0);
11835 else if ((complain
& tf_error
) != 0
11836 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11837 && !grok_op_properties (r
, /*complain=*/true))
11838 RETURN (error_mark_node
);
11840 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
11841 SET_DECL_FRIEND_CONTEXT (r
,
11842 tsubst (DECL_FRIEND_CONTEXT (t
),
11843 args
, complain
, in_decl
));
11845 /* Possibly limit visibility based on template args. */
11846 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11847 if (DECL_VISIBILITY_SPECIFIED (t
))
11849 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11850 DECL_ATTRIBUTES (r
)
11851 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11853 determine_visibility (r
);
11854 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
11855 && !processing_template_decl
)
11856 defaulted_late_check (r
);
11858 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11859 args
, complain
, in_decl
);
11865 tree type
= NULL_TREE
;
11867 tree expanded_types
= NULL_TREE
;
11868 tree prev_r
= NULL_TREE
;
11869 tree first_r
= NULL_TREE
;
11871 if (DECL_PACK_P (t
))
11873 /* If there is a local specialization that isn't a
11874 parameter pack, it means that we're doing a "simple"
11875 substitution from inside tsubst_pack_expansion. Just
11876 return the local specialization (which will be a single
11878 tree spec
= retrieve_local_specialization (t
);
11880 && TREE_CODE (spec
) == PARM_DECL
11881 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
11884 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11885 the parameters in this function parameter pack. */
11886 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11887 complain
, in_decl
);
11888 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11890 len
= TREE_VEC_LENGTH (expanded_types
);
11892 /* Zero-length parameter packs are boring. Just substitute
11895 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
11900 /* All we did was update the type. Make a note of that. */
11901 type
= expanded_types
;
11902 expanded_types
= NULL_TREE
;
11906 /* Loop through all of the parameters we'll build. When T is
11907 a function parameter pack, LEN is the number of expanded
11908 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11910 for (i
= 0; i
< len
; ++i
)
11914 if (DECL_TEMPLATE_PARM_P (t
))
11915 SET_DECL_TEMPLATE_PARM_P (r
);
11917 if (expanded_types
)
11918 /* We're on the Ith parameter of the function parameter
11921 /* Get the Ith type. */
11922 type
= TREE_VEC_ELT (expanded_types
, i
);
11924 /* Rename the parameter to include the index. */
11926 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11929 /* We're dealing with a normal parameter. */
11930 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11932 type
= type_decays_to (type
);
11933 TREE_TYPE (r
) = type
;
11934 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11936 if (DECL_INITIAL (r
))
11938 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
11939 DECL_INITIAL (r
) = TREE_TYPE (r
);
11941 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
11942 complain
, in_decl
);
11945 DECL_CONTEXT (r
) = NULL_TREE
;
11947 if (!DECL_TEMPLATE_PARM_P (r
))
11948 DECL_ARG_TYPE (r
) = type_passed_as (type
);
11950 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11951 args
, complain
, in_decl
);
11953 /* Keep track of the first new parameter we
11954 generate. That's what will be returned to the
11959 /* Build a proper chain of parameters when substituting
11960 into a function parameter pack. */
11962 DECL_CHAIN (prev_r
) = r
;
11965 /* If cp_unevaluated_operand is set, we're just looking for a
11966 single dummy parameter, so don't keep going. */
11967 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
11968 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
11969 complain
, DECL_CHAIN (t
));
11971 /* FIRST_R contains the start of the chain we've built. */
11978 tree type
= NULL_TREE
;
11979 tree vec
= NULL_TREE
;
11980 tree expanded_types
= NULL_TREE
;
11983 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
11985 /* This field is a lambda capture pack. Return a TREE_VEC of
11986 the expanded fields to instantiate_class_template_1 and
11987 store them in the specializations hash table as a
11988 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11989 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11990 complain
, in_decl
);
11991 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11993 len
= TREE_VEC_LENGTH (expanded_types
);
11994 vec
= make_tree_vec (len
);
11998 /* All we did was update the type. Make a note of that. */
11999 type
= expanded_types
;
12000 expanded_types
= NULL_TREE
;
12004 for (int i
= 0; i
< len
; ++i
)
12007 if (expanded_types
)
12009 type
= TREE_VEC_ELT (expanded_types
, i
);
12011 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12014 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12016 if (type
== error_mark_node
)
12017 RETURN (error_mark_node
);
12018 TREE_TYPE (r
) = type
;
12019 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12021 if (DECL_C_BIT_FIELD (r
))
12022 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12023 non-bit-fields DECL_INITIAL is a non-static data member
12024 initializer, which gets deferred instantiation. */
12026 = tsubst_expr (DECL_INITIAL (t
), args
,
12028 /*integral_constant_expression_p=*/true);
12029 else if (DECL_INITIAL (t
))
12031 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12032 NSDMI in perform_member_init. Still set DECL_INITIAL
12033 so that we know there is one. */
12034 DECL_INITIAL (r
) = void_node
;
12035 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12036 retrofit_lang_decl (r
);
12037 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12039 /* We don't have to set DECL_CONTEXT here; it is set by
12040 finish_member_declaration. */
12041 DECL_CHAIN (r
) = NULL_TREE
;
12043 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12044 args
, complain
, in_decl
);
12047 TREE_VEC_ELT (vec
, i
) = r
;
12053 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12054 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12055 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12056 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
12057 TREE_TYPE (pack
) = tpack
;
12058 register_specialization (pack
, t
, args
, false, 0);
12064 /* We reach here only for member using decls. We also need to check
12065 uses_template_parms because DECL_DEPENDENT_P is not set for a
12066 using-declaration that designates a member of the current
12067 instantiation (c++/53549). */
12068 if (DECL_DEPENDENT_P (t
)
12069 || uses_template_parms (USING_DECL_SCOPE (t
)))
12071 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12072 complain
, in_decl
);
12073 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12074 r
= do_class_using_decl (inst_scope
, name
);
12076 r
= error_mark_node
;
12079 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12080 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12086 DECL_CHAIN (r
) = NULL_TREE
;
12093 tree argvec
= NULL_TREE
;
12094 tree gen_tmpl
= NULL_TREE
;
12096 tree tmpl
= NULL_TREE
;
12098 tree type
= NULL_TREE
;
12101 if (TREE_TYPE (t
) == error_mark_node
)
12102 RETURN (error_mark_node
);
12104 if (TREE_CODE (t
) == TYPE_DECL
12105 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12107 /* If this is the canonical decl, we don't have to
12108 mess with instantiations, and often we can't (for
12109 typename, template type parms and such). Note that
12110 TYPE_NAME is not correct for the above test if
12111 we've copied the type for a typedef. */
12112 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12113 if (type
== error_mark_node
)
12114 RETURN (error_mark_node
);
12115 r
= TYPE_NAME (type
);
12119 /* Check to see if we already have the specialization we
12122 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12124 /* T is a static data member or namespace-scope entity.
12125 We have to substitute into namespace-scope variables
12126 (not just variable templates) because of cases like:
12128 template <class T> void f() { extern T t; }
12130 where the entity referenced is not known until
12131 instantiation time. */
12133 ctx
= DECL_CONTEXT (t
);
12134 if (DECL_CLASS_SCOPE_P (t
))
12136 ctx
= tsubst_aggr_type (ctx
, args
,
12138 in_decl
, /*entering_scope=*/1);
12139 /* If CTX is unchanged, then T is in fact the
12140 specialization we want. That situation occurs when
12141 referencing a static data member within in its own
12142 class. We can use pointer equality, rather than
12143 same_type_p, because DECL_CONTEXT is always
12145 if (ctx
== DECL_CONTEXT (t
)
12146 /* ... unless T is a member template; in which
12147 case our caller can be willing to create a
12148 specialization of that template represented
12150 && !(DECL_TI_TEMPLATE (t
)
12151 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12157 tmpl
= DECL_TI_TEMPLATE (t
);
12158 gen_tmpl
= most_general_template (tmpl
);
12159 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12160 if (argvec
!= error_mark_node
)
12161 argvec
= (coerce_innermost_template_parms
12162 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12163 argvec
, t
, complain
,
12164 /*all*/true, /*defarg*/true));
12165 if (argvec
== error_mark_node
)
12166 RETURN (error_mark_node
);
12167 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12168 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12173 /* A local variable. */
12175 /* Subsequent calls to pushdecl will fill this in. */
12177 spec
= retrieve_local_specialization (t
);
12179 /* If we already have the specialization we need, there is
12180 nothing more to do. */
12187 /* Create a new node for the specialization we need. */
12189 if (type
== NULL_TREE
)
12191 if (is_typedef_decl (t
))
12192 type
= DECL_ORIGINAL_TYPE (t
);
12194 type
= TREE_TYPE (t
);
12196 && VAR_HAD_UNKNOWN_BOUND (t
)
12197 && type
!= error_mark_node
)
12198 type
= strip_array_domain (type
);
12199 type
= tsubst (type
, args
, complain
, in_decl
);
12203 /* Even if the original location is out of scope, the
12204 newly substituted one is not. */
12205 DECL_DEAD_FOR_LOCAL (r
) = 0;
12206 DECL_INITIALIZED_P (r
) = 0;
12207 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
12208 if (type
== error_mark_node
)
12209 RETURN (error_mark_node
);
12210 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12212 /* It may seem that this case cannot occur, since:
12217 declares a function, not a variable. However:
12220 template <typename T> void g() { T t; }
12221 template void g<f>();
12223 is an attempt to declare a variable with function
12225 error ("variable %qD has function type",
12226 /* R is not yet sufficiently initialized, so we
12227 just use its name. */
12229 RETURN (error_mark_node
);
12231 type
= complete_type (type
);
12232 /* Wait until cp_finish_decl to set this again, to handle
12233 circular dependency (template/instantiate6.C). */
12234 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
12235 type
= check_var_type (DECL_NAME (r
), type
);
12237 if (DECL_HAS_VALUE_EXPR_P (t
))
12239 tree ve
= DECL_VALUE_EXPR (t
);
12240 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
12241 /*constant_expression_p=*/false);
12242 if (REFERENCE_REF_P (ve
))
12244 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
12245 ve
= TREE_OPERAND (ve
, 0);
12247 SET_DECL_VALUE_EXPR (r
, ve
);
12249 if (CP_DECL_THREAD_LOCAL_P (r
)
12250 && !processing_template_decl
)
12251 set_decl_tls_model (r
, decl_default_tls_model (r
));
12253 else if (DECL_SELF_REFERENCE_P (t
))
12254 SET_DECL_SELF_REFERENCE_P (r
);
12255 TREE_TYPE (r
) = type
;
12256 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12257 DECL_CONTEXT (r
) = ctx
;
12258 /* Clear out the mangled name and RTL for the instantiation. */
12259 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12260 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12261 SET_DECL_RTL (r
, NULL
);
12262 /* The initializer must not be expanded until it is required;
12263 see [temp.inst]. */
12264 DECL_INITIAL (r
) = NULL_TREE
;
12265 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12266 SET_DECL_RTL (r
, NULL
);
12267 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
12270 /* Possibly limit visibility based on template args. */
12271 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12272 if (DECL_VISIBILITY_SPECIFIED (t
))
12274 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12275 DECL_ATTRIBUTES (r
)
12276 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12278 determine_visibility (r
);
12283 /* A static data member declaration is always marked
12284 external when it is declared in-class, even if an
12285 initializer is present. We mimic the non-template
12286 processing here. */
12287 DECL_EXTERNAL (r
) = 1;
12288 if (DECL_NAMESPACE_SCOPE_P (t
))
12289 DECL_NOT_REALLY_EXTERN (r
) = 1;
12291 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
12292 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12293 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12297 if (DECL_LANG_SPECIFIC (r
))
12298 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12299 if (!cp_unevaluated_operand
)
12300 register_local_specialization (r
, t
);
12303 DECL_CHAIN (r
) = NULL_TREE
;
12305 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12307 args
, complain
, in_decl
);
12309 /* Preserve a typedef that names a type. */
12310 if (is_typedef_decl (r
))
12312 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12313 set_underlying_type (r
);
12314 if (TYPE_DECL_ALIAS_P (r
) && type
!= error_mark_node
)
12315 /* An alias template specialization can be dependent
12316 even if its underlying type is not. */
12317 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
12320 layout_decl (r
, 0);
12325 gcc_unreachable ();
12330 /* Restore the file and line information. */
12331 input_location
= saved_loc
;
12336 /* Substitute into the ARG_TYPES of a function type.
12337 If END is a TREE_CHAIN, leave it and any following types
12341 tsubst_arg_types (tree arg_types
,
12344 tsubst_flags_t complain
,
12347 tree remaining_arg_types
;
12348 tree type
= NULL_TREE
;
12350 tree expanded_args
= NULL_TREE
;
12353 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
12356 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
12357 args
, end
, complain
, in_decl
);
12358 if (remaining_arg_types
== error_mark_node
)
12359 return error_mark_node
;
12361 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
12363 /* For a pack expansion, perform substitution on the
12364 entire expression. Later on, we'll handle the arguments
12366 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
12367 args
, complain
, in_decl
);
12369 if (TREE_CODE (expanded_args
) == TREE_VEC
)
12370 /* So that we'll spin through the parameters, one by one. */
12371 i
= TREE_VEC_LENGTH (expanded_args
);
12374 /* We only partially substituted into the parameter
12375 pack. Our type is TYPE_PACK_EXPANSION. */
12376 type
= expanded_args
;
12377 expanded_args
= NULL_TREE
;
12385 type
= TREE_VEC_ELT (expanded_args
, i
);
12387 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
12389 if (type
== error_mark_node
)
12390 return error_mark_node
;
12391 if (VOID_TYPE_P (type
))
12393 if (complain
& tf_error
)
12395 error ("invalid parameter type %qT", type
);
12397 error ("in declaration %q+D", in_decl
);
12399 return error_mark_node
;
12402 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
12403 return error_mark_node
;
12405 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12406 top-level qualifiers as required. */
12407 type
= cv_unqualified (type_decays_to (type
));
12409 /* We do not substitute into default arguments here. The standard
12410 mandates that they be instantiated only when needed, which is
12411 done in build_over_call. */
12412 default_arg
= TREE_PURPOSE (arg_types
);
12414 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
12416 /* We've instantiated a template before its default arguments
12417 have been parsed. This can happen for a nested template
12418 class, and is not an error unless we require the default
12419 argument in a call of this function. */
12420 remaining_arg_types
=
12421 tree_cons (default_arg
, type
, remaining_arg_types
);
12422 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
12425 remaining_arg_types
=
12426 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
12429 return remaining_arg_types
;
12432 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12433 *not* handle the exception-specification for FNTYPE, because the
12434 initial substitution of explicitly provided template parameters
12435 during argument deduction forbids substitution into the
12436 exception-specification:
12440 All references in the function type of the function template to the
12441 corresponding template parameters are replaced by the specified tem-
12442 plate argument values. If a substitution in a template parameter or
12443 in the function type of the function template results in an invalid
12444 type, type deduction fails. [Note: The equivalent substitution in
12445 exception specifications is done only when the function is instanti-
12446 ated, at which point a program is ill-formed if the substitution
12447 results in an invalid type.] */
12450 tsubst_function_type (tree t
,
12452 tsubst_flags_t complain
,
12456 tree arg_types
= NULL_TREE
;
12459 /* The TYPE_CONTEXT is not used for function/method types. */
12460 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
12462 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12464 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
12466 if (late_return_type_p
)
12468 /* Substitute the argument types. */
12469 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12470 complain
, in_decl
);
12471 if (arg_types
== error_mark_node
)
12472 return error_mark_node
;
12474 tree save_ccp
= current_class_ptr
;
12475 tree save_ccr
= current_class_ref
;
12476 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
12477 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
12478 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
12481 /* DR 1207: 'this' is in scope in the trailing return type. */
12482 inject_this_parameter (this_type
, cp_type_quals (this_type
));
12485 /* Substitute the return type. */
12486 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12490 current_class_ptr
= save_ccp
;
12491 current_class_ref
= save_ccr
;
12495 /* Substitute the return type. */
12496 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12498 if (return_type
== error_mark_node
)
12499 return error_mark_node
;
12500 /* DR 486 clarifies that creation of a function type with an
12501 invalid return type is a deduction failure. */
12502 if (TREE_CODE (return_type
) == ARRAY_TYPE
12503 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
12505 if (complain
& tf_error
)
12507 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
12508 error ("function returning an array");
12510 error ("function returning a function");
12512 return error_mark_node
;
12515 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
12516 return error_mark_node
;
12518 if (!late_return_type_p
)
12520 /* Substitute the argument types. */
12521 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12522 complain
, in_decl
);
12523 if (arg_types
== error_mark_node
)
12524 return error_mark_node
;
12527 /* Construct a new type node and return it. */
12528 if (TREE_CODE (t
) == FUNCTION_TYPE
)
12530 fntype
= build_function_type (return_type
, arg_types
);
12531 fntype
= apply_memfn_quals (fntype
,
12532 type_memfn_quals (t
),
12533 type_memfn_rqual (t
));
12537 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
12538 /* Don't pick up extra function qualifiers from the basetype. */
12539 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
12540 if (! MAYBE_CLASS_TYPE_P (r
))
12544 Type deduction may fail for any of the following
12547 -- Attempting to create "pointer to member of T" when T
12548 is not a class type. */
12549 if (complain
& tf_error
)
12550 error ("creating pointer to member function of non-class type %qT",
12552 return error_mark_node
;
12555 fntype
= build_method_type_directly (r
, return_type
,
12556 TREE_CHAIN (arg_types
));
12557 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
12559 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
12561 if (late_return_type_p
)
12562 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
12567 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12568 ARGS into that specification, and return the substituted
12569 specification. If there is no specification, return NULL_TREE. */
12572 tsubst_exception_specification (tree fntype
,
12574 tsubst_flags_t complain
,
12581 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
12582 new_specs
= NULL_TREE
;
12583 if (specs
&& TREE_PURPOSE (specs
))
12585 /* A noexcept-specifier. */
12586 tree expr
= TREE_PURPOSE (specs
);
12587 if (TREE_CODE (expr
) == INTEGER_CST
)
12591 /* Defer instantiation of noexcept-specifiers to avoid
12592 excessive instantiations (c++/49107). */
12593 new_specs
= make_node (DEFERRED_NOEXCEPT
);
12594 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
12596 /* We already partially instantiated this member template,
12597 so combine the new args with the old. */
12598 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
12599 = DEFERRED_NOEXCEPT_PATTERN (expr
);
12600 DEFERRED_NOEXCEPT_ARGS (new_specs
)
12601 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
12605 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
12606 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
12610 new_specs
= tsubst_copy_and_build
12611 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
12612 /*integral_constant_expression_p=*/true);
12613 new_specs
= build_noexcept_spec (new_specs
, complain
);
12617 if (! TREE_VALUE (specs
))
12624 tree expanded_specs
= NULL_TREE
;
12626 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
12628 /* Expand the pack expansion type. */
12629 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
12633 if (expanded_specs
== error_mark_node
)
12634 return error_mark_node
;
12635 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
12636 len
= TREE_VEC_LENGTH (expanded_specs
);
12639 /* We're substituting into a member template, so
12640 we got a TYPE_PACK_EXPANSION back. Add that
12641 expansion and move on. */
12642 gcc_assert (TREE_CODE (expanded_specs
)
12643 == TYPE_PACK_EXPANSION
);
12644 new_specs
= add_exception_specifier (new_specs
,
12647 specs
= TREE_CHAIN (specs
);
12652 for (i
= 0; i
< len
; ++i
)
12654 if (expanded_specs
)
12655 spec
= TREE_VEC_ELT (expanded_specs
, i
);
12657 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
12658 if (spec
== error_mark_node
)
12660 new_specs
= add_exception_specifier (new_specs
, spec
,
12664 specs
= TREE_CHAIN (specs
);
12670 /* Take the tree structure T and replace template parameters used
12671 therein with the argument vector ARGS. IN_DECL is an associated
12672 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12673 Issue error and warning messages under control of COMPLAIN. Note
12674 that we must be relatively non-tolerant of extensions here, in
12675 order to preserve conformance; if we allow substitutions that
12676 should not be allowed, we may allow argument deductions that should
12677 not succeed, and therefore report ambiguous overload situations
12678 where there are none. In theory, we could allow the substitution,
12679 but indicate that it should have failed, and allow our caller to
12680 make sure that the right thing happens, but we don't try to do this
12683 This function is used for dealing with types, decls and the like;
12684 for expressions, use tsubst_expr or tsubst_copy. */
12687 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12689 enum tree_code code
;
12690 tree type
, r
= NULL_TREE
;
12692 if (t
== NULL_TREE
|| t
== error_mark_node
12693 || t
== integer_type_node
12694 || t
== void_type_node
12695 || t
== char_type_node
12696 || t
== unknown_type_node
12697 || TREE_CODE (t
) == NAMESPACE_DECL
12698 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
12702 return tsubst_decl (t
, args
, complain
);
12704 if (args
== NULL_TREE
)
12707 code
= TREE_CODE (t
);
12709 if (code
== IDENTIFIER_NODE
)
12710 type
= IDENTIFIER_TYPE_VALUE (t
);
12712 type
= TREE_TYPE (t
);
12714 gcc_assert (type
!= unknown_type_node
);
12716 /* Reuse typedefs. We need to do this to handle dependent attributes,
12717 such as attribute aligned. */
12719 && typedef_variant_p (t
))
12721 tree decl
= TYPE_NAME (t
);
12723 if (alias_template_specialization_p (t
))
12725 /* DECL represents an alias template and we want to
12727 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12728 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12729 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
12731 else if (DECL_CLASS_SCOPE_P (decl
)
12732 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12733 && uses_template_parms (DECL_CONTEXT (decl
)))
12735 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12736 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12737 r
= retrieve_specialization (tmpl
, gen_args
, 0);
12739 else if (DECL_FUNCTION_SCOPE_P (decl
)
12740 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12741 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
12742 r
= retrieve_local_specialization (decl
);
12744 /* The typedef is from a non-template context. */
12750 r
= cp_build_qualified_type_real
12751 (r
, cp_type_quals (t
) | cp_type_quals (r
),
12752 complain
| tf_ignore_bad_quals
);
12757 /* We don't have an instantiation yet, so drop the typedef. */
12758 int quals
= cp_type_quals (t
);
12759 t
= DECL_ORIGINAL_TYPE (decl
);
12760 t
= cp_build_qualified_type_real (t
, quals
,
12761 complain
| tf_ignore_bad_quals
);
12766 && code
!= TYPENAME_TYPE
12767 && code
!= TEMPLATE_TYPE_PARM
12768 && code
!= IDENTIFIER_NODE
12769 && code
!= FUNCTION_TYPE
12770 && code
!= METHOD_TYPE
)
12771 type
= tsubst (type
, args
, complain
, in_decl
);
12772 if (type
== error_mark_node
)
12773 return error_mark_node
;
12779 case ENUMERAL_TYPE
:
12780 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
12781 /*entering_scope=*/0);
12784 case IDENTIFIER_NODE
:
12795 if (t
== integer_type_node
)
12798 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
)
12800 if (!TYPE_MAX_VALUE (t
))
12801 return compute_array_index_type (NULL_TREE
, NULL_TREE
, complain
);
12803 if (TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
12808 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
12810 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
12811 /*integral_constant_expression_p=*/false);
12813 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12815 if (TREE_CODE (max
) == NOP_EXPR
12816 && TREE_SIDE_EFFECTS (omax
)
12817 && !TREE_TYPE (max
))
12818 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
12820 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12821 with TREE_SIDE_EFFECTS that indicates this is not an integral
12822 constant expression. */
12823 if (processing_template_decl
12824 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
12826 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
12827 TREE_SIDE_EFFECTS (max
) = 1;
12830 return compute_array_index_type (NULL_TREE
, max
, complain
);
12833 case TEMPLATE_TYPE_PARM
:
12834 case TEMPLATE_TEMPLATE_PARM
:
12835 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12836 case TEMPLATE_PARM_INDEX
:
12841 tree arg
= NULL_TREE
;
12843 /* Early in template argument deduction substitution, we don't
12844 want to reduce the level of 'auto', or it will be confused
12845 with a normal template parm in subsequent deduction. */
12846 if (is_auto (t
) && (complain
& tf_partial
))
12851 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
12852 template_parm_level_and_index (t
, &level
, &idx
);
12854 levels
= TMPL_ARGS_DEPTH (args
);
12855 if (level
<= levels
12856 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
12858 arg
= TMPL_ARG (args
, level
, idx
);
12860 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
12862 /* See through ARGUMENT_PACK_SELECT arguments. */
12863 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
12864 /* If the selected argument is an expansion E, that most
12865 likely means we were called from
12866 gen_elem_of_pack_expansion_instantiation during the
12867 substituting of pack an argument pack (which Ith
12868 element is a pack expansion, where I is
12869 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12870 In this case, the Ith element resulting from this
12871 substituting is going to be a pack expansion, which
12872 pattern is the pattern of E. Let's return the
12874 gen_elem_of_pack_expansion_instantiation will
12875 build the resulting pack expansion from it. */
12876 if (PACK_EXPANSION_P (arg
))
12878 /* Make sure we aren't throwing away arg info. */
12879 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
12880 arg
= PACK_EXPANSION_PATTERN (arg
);
12885 if (arg
== error_mark_node
)
12886 return error_mark_node
;
12887 else if (arg
!= NULL_TREE
)
12889 if (ARGUMENT_PACK_P (arg
))
12890 /* If ARG is an argument pack, we don't actually want to
12891 perform a substitution here, because substitutions
12892 for argument packs are only done
12893 element-by-element. We can get to this point when
12894 substituting the type of a non-type template
12895 parameter pack, when that type actually contains
12896 template parameter packs from an outer template, e.g.,
12898 template<typename... Types> struct A {
12899 template<Types... Values> struct B { };
12903 if (code
== TEMPLATE_TYPE_PARM
)
12906 gcc_assert (TYPE_P (arg
));
12908 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
12910 return cp_build_qualified_type_real
12911 (arg
, quals
, complain
| tf_ignore_bad_quals
);
12913 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12915 /* We are processing a type constructed from a
12916 template template parameter. */
12917 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
12918 args
, complain
, in_decl
);
12919 if (argvec
== error_mark_node
)
12920 return error_mark_node
;
12922 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
12923 || TREE_CODE (arg
) == TEMPLATE_DECL
12924 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
12926 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
12927 /* Consider this code:
12929 template <template <class> class Template>
12931 template <class Arg> using Bind = Template<Arg>;
12934 template <template <class> class Template, class Arg>
12935 using Instantiate = Template<Arg>; //#0
12937 template <template <class> class Template,
12940 Instantiate<Internal<Template>::template Bind,
12943 When #1 is parsed, the
12944 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12945 parameter `Template' in #0 matches the
12946 UNBOUND_CLASS_TEMPLATE representing the argument
12947 `Internal<Template>::template Bind'; We then want
12948 to assemble the type `Bind<Argument>' that can't
12949 be fully created right now, because
12950 `Internal<Template>' not being complete, the Bind
12951 template cannot be looked up in that context. So
12952 we need to "store" `Bind<Argument>' for later
12953 when the context of Bind becomes complete. Let's
12954 store that in a TYPENAME_TYPE. */
12955 return make_typename_type (TYPE_CONTEXT (arg
),
12956 build_nt (TEMPLATE_ID_EXPR
,
12957 TYPE_IDENTIFIER (arg
),
12962 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12963 are resolving nested-types in the signature of a
12964 member function templates. Otherwise ARG is a
12965 TEMPLATE_DECL and is the real template to be
12967 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
12968 arg
= TYPE_NAME (arg
);
12970 r
= lookup_template_class (arg
,
12972 DECL_CONTEXT (arg
),
12973 /*entering_scope=*/0,
12975 return cp_build_qualified_type_real
12976 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
12979 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12980 return convert_from_reference (unshare_expr (arg
));
12984 /* This can happen during the attempted tsubst'ing in
12985 unify. This means that we don't yet have any information
12986 about the template parameter in question. */
12989 /* If we get here, we must have been looking at a parm for a
12990 more deeply nested template. Make a new version of this
12991 template parameter, but with a lower level. */
12994 case TEMPLATE_TYPE_PARM
:
12995 case TEMPLATE_TEMPLATE_PARM
:
12996 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12997 if (cp_type_quals (t
))
12999 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13000 r
= cp_build_qualified_type_real
13001 (r
, cp_type_quals (t
),
13002 complain
| (code
== TEMPLATE_TYPE_PARM
13003 ? tf_ignore_bad_quals
: 0));
13005 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13006 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13007 && (r
= (TEMPLATE_PARM_DESCENDANTS
13008 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13009 && (r
= TREE_TYPE (r
))
13010 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13011 /* Break infinite recursion when substituting the constraints
13012 of a constrained placeholder. */;
13016 TEMPLATE_TYPE_PARM_INDEX (r
)
13017 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13018 r
, levels
, args
, complain
);
13019 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13020 TYPE_MAIN_VARIANT (r
) = r
;
13021 TYPE_POINTER_TO (r
) = NULL_TREE
;
13022 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13024 /* Propagate constraints on placeholders. */
13025 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13026 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13027 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13028 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13030 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13031 /* We have reduced the level of the template
13032 template parameter, but not the levels of its
13033 template parameters, so canonical_type_parameter
13034 will not be able to find the canonical template
13035 template parameter for this level. Thus, we
13036 require structural equality checking to compare
13037 TEMPLATE_TEMPLATE_PARMs. */
13038 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13039 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13040 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13042 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13044 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13046 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
13047 complain
, in_decl
);
13048 if (argvec
== error_mark_node
)
13049 return error_mark_node
;
13051 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13052 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
13057 case TEMPLATE_PARM_INDEX
:
13058 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13062 gcc_unreachable ();
13070 tree purpose
, value
, chain
;
13072 if (t
== void_list_node
)
13075 purpose
= TREE_PURPOSE (t
);
13078 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13079 if (purpose
== error_mark_node
)
13080 return error_mark_node
;
13082 value
= TREE_VALUE (t
);
13085 value
= tsubst (value
, args
, complain
, in_decl
);
13086 if (value
== error_mark_node
)
13087 return error_mark_node
;
13089 chain
= TREE_CHAIN (t
);
13090 if (chain
&& chain
!= void_type_node
)
13092 chain
= tsubst (chain
, args
, complain
, in_decl
);
13093 if (chain
== error_mark_node
)
13094 return error_mark_node
;
13096 if (purpose
== TREE_PURPOSE (t
)
13097 && value
== TREE_VALUE (t
)
13098 && chain
== TREE_CHAIN (t
))
13100 return hash_tree_cons (purpose
, value
, chain
);
13104 /* We should never be tsubsting a binfo. */
13105 gcc_unreachable ();
13108 /* A vector of template arguments. */
13109 gcc_assert (!type
);
13110 return tsubst_template_args (t
, args
, complain
, in_decl
);
13113 case REFERENCE_TYPE
:
13115 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13120 Type deduction may fail for any of the following
13123 -- Attempting to create a pointer to reference type.
13124 -- Attempting to create a reference to a reference type or
13125 a reference to void.
13127 Core issue 106 says that creating a reference to a reference
13128 during instantiation is no longer a cause for failure. We
13129 only enforce this check in strict C++98 mode. */
13130 if ((TREE_CODE (type
) == REFERENCE_TYPE
13131 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
13132 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
13134 static location_t last_loc
;
13136 /* We keep track of the last time we issued this error
13137 message to avoid spewing a ton of messages during a
13138 single bad template instantiation. */
13139 if (complain
& tf_error
13140 && last_loc
!= input_location
)
13142 if (VOID_TYPE_P (type
))
13143 error ("forming reference to void");
13144 else if (code
== POINTER_TYPE
)
13145 error ("forming pointer to reference type %qT", type
);
13147 error ("forming reference to reference type %qT", type
);
13148 last_loc
= input_location
;
13151 return error_mark_node
;
13153 else if (TREE_CODE (type
) == FUNCTION_TYPE
13154 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13155 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13157 if (complain
& tf_error
)
13159 if (code
== POINTER_TYPE
)
13160 error ("forming pointer to qualified function type %qT",
13163 error ("forming reference to qualified function type %qT",
13166 return error_mark_node
;
13168 else if (code
== POINTER_TYPE
)
13170 r
= build_pointer_type (type
);
13171 if (TREE_CODE (type
) == METHOD_TYPE
)
13172 r
= build_ptrmemfunc_type (r
);
13174 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
13175 /* In C++0x, during template argument substitution, when there is an
13176 attempt to create a reference to a reference type, reference
13177 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13179 "If a template-argument for a template-parameter T names a type
13180 that is a reference to a type A, an attempt to create the type
13181 'lvalue reference to cv T' creates the type 'lvalue reference to
13182 A,' while an attempt to create the type type rvalue reference to
13183 cv T' creates the type T"
13185 r
= cp_build_reference_type
13187 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
13189 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
13190 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13192 if (r
!= error_mark_node
)
13193 /* Will this ever be needed for TYPE_..._TO values? */
13200 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
13201 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
13205 Type deduction may fail for any of the following
13208 -- Attempting to create "pointer to member of T" when T
13209 is not a class type. */
13210 if (complain
& tf_error
)
13211 error ("creating pointer to member of non-class type %qT", r
);
13212 return error_mark_node
;
13214 if (TREE_CODE (type
) == REFERENCE_TYPE
)
13216 if (complain
& tf_error
)
13217 error ("creating pointer to member reference type %qT", type
);
13218 return error_mark_node
;
13220 if (VOID_TYPE_P (type
))
13222 if (complain
& tf_error
)
13223 error ("creating pointer to member of type void");
13224 return error_mark_node
;
13226 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13227 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13229 /* The type of the implicit object parameter gets its
13230 cv-qualifiers from the FUNCTION_TYPE. */
13233 = build_memfn_type (type
, r
, type_memfn_quals (type
),
13234 type_memfn_rqual (type
));
13235 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
13236 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
13240 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
13244 case FUNCTION_TYPE
:
13249 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
13250 if (fntype
== error_mark_node
)
13251 return error_mark_node
;
13253 /* Substitute the exception specification. */
13254 specs
= tsubst_exception_specification (t
, args
, complain
,
13255 in_decl
, /*defer_ok*/true);
13256 if (specs
== error_mark_node
)
13257 return error_mark_node
;
13259 fntype
= build_exception_variant (fntype
, specs
);
13264 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
13265 if (domain
== error_mark_node
)
13266 return error_mark_node
;
13268 /* As an optimization, we avoid regenerating the array type if
13269 it will obviously be the same as T. */
13270 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
13273 /* These checks should match the ones in create_array_type_for_decl.
13277 The deduction may fail for any of the following reasons:
13279 -- Attempting to create an array with an element type that
13280 is void, a function type, or a reference type, or [DR337]
13281 an abstract class type. */
13282 if (VOID_TYPE_P (type
)
13283 || TREE_CODE (type
) == FUNCTION_TYPE
13284 || (TREE_CODE (type
) == ARRAY_TYPE
13285 && TYPE_DOMAIN (type
) == NULL_TREE
)
13286 || TREE_CODE (type
) == REFERENCE_TYPE
)
13288 if (complain
& tf_error
)
13289 error ("creating array of %qT", type
);
13290 return error_mark_node
;
13293 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
13294 return error_mark_node
;
13296 r
= build_cplus_array_type (type
, domain
);
13298 if (TYPE_USER_ALIGN (t
))
13300 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
13301 TYPE_USER_ALIGN (r
) = 1;
13307 case TYPENAME_TYPE
:
13309 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13310 in_decl
, /*entering_scope=*/1);
13311 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
13312 complain
, in_decl
);
13314 if (ctx
== error_mark_node
|| f
== error_mark_node
)
13315 return error_mark_node
;
13317 if (!MAYBE_CLASS_TYPE_P (ctx
))
13319 if (complain
& tf_error
)
13320 error ("%qT is not a class, struct, or union type", ctx
);
13321 return error_mark_node
;
13323 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
13325 /* Normally, make_typename_type does not require that the CTX
13326 have complete type in order to allow things like:
13328 template <class T> struct S { typename S<T>::X Y; };
13330 But, such constructs have already been resolved by this
13331 point, so here CTX really should have complete type, unless
13332 it's a partial instantiation. */
13333 ctx
= complete_type (ctx
);
13334 if (!COMPLETE_TYPE_P (ctx
))
13336 if (complain
& tf_error
)
13337 cxx_incomplete_type_error (NULL_TREE
, ctx
);
13338 return error_mark_node
;
13342 f
= make_typename_type (ctx
, f
, typename_type
,
13343 complain
| tf_keep_type_decl
);
13344 if (f
== error_mark_node
)
13346 if (TREE_CODE (f
) == TYPE_DECL
)
13348 complain
|= tf_ignore_bad_quals
;
13352 if (TREE_CODE (f
) != TYPENAME_TYPE
)
13354 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
13356 if (complain
& tf_error
)
13357 error ("%qT resolves to %qT, which is not an enumeration type",
13360 return error_mark_node
;
13362 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
13364 if (complain
& tf_error
)
13365 error ("%qT resolves to %qT, which is is not a class type",
13368 return error_mark_node
;
13372 return cp_build_qualified_type_real
13373 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
13376 case UNBOUND_CLASS_TEMPLATE
:
13378 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13379 in_decl
, /*entering_scope=*/1);
13380 tree name
= TYPE_IDENTIFIER (t
);
13381 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
13383 if (ctx
== error_mark_node
|| name
== error_mark_node
)
13384 return error_mark_node
;
13387 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
13388 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
13395 ++cp_unevaluated_operand
;
13396 ++c_inhibit_evaluation_warnings
;
13398 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
13400 /*integral_constant_expression_p=*/false);
13402 --cp_unevaluated_operand
;
13403 --c_inhibit_evaluation_warnings
;
13405 type
= finish_typeof (type
);
13406 return cp_build_qualified_type_real (type
,
13408 | cp_type_quals (type
),
13412 case DECLTYPE_TYPE
:
13416 ++cp_unevaluated_operand
;
13417 ++c_inhibit_evaluation_warnings
;
13419 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
13420 complain
|tf_decltype
, in_decl
,
13421 /*function_p*/false,
13422 /*integral_constant_expression*/false);
13424 --cp_unevaluated_operand
;
13425 --c_inhibit_evaluation_warnings
;
13427 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
13428 type
= lambda_capture_field_type (type
,
13429 DECLTYPE_FOR_INIT_CAPTURE (t
));
13430 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
13431 type
= lambda_proxy_type (type
);
13434 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
13435 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
13437 /* In a template ~id could be either a complement expression
13438 or an unqualified-id naming a destructor; if instantiating
13439 it produces an expression, it's not an id-expression or
13442 type
= finish_decltype_type (type
, id
, complain
);
13444 return cp_build_qualified_type_real (type
,
13446 | cp_type_quals (type
),
13447 complain
| tf_ignore_bad_quals
);
13450 case UNDERLYING_TYPE
:
13452 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
13453 complain
, in_decl
);
13454 return finish_underlying_type (type
);
13457 case TYPE_ARGUMENT_PACK
:
13458 case NONTYPE_ARGUMENT_PACK
:
13460 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
13462 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
13466 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
13468 /* For template nontype argument packs, also substitute into
13470 if (code
== NONTYPE_ARGUMENT_PACK
)
13471 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13490 /* We should use one of the expression tsubsts for these codes. */
13491 gcc_unreachable ();
13494 sorry ("use of %qs in template", get_tree_code_name (code
));
13495 return error_mark_node
;
13499 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13500 type of the expression on the left-hand side of the "." or "->"
13504 tsubst_baselink (tree baselink
, tree object_type
,
13505 tree args
, tsubst_flags_t complain
, tree in_decl
)
13508 tree qualifying_scope
;
13511 tree template_args
= 0;
13512 bool template_id_p
= false;
13513 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
13515 /* A baselink indicates a function from a base class. Both the
13516 BASELINK_ACCESS_BINFO and the base class referenced may
13517 indicate bases of the template class, rather than the
13518 instantiated class. In addition, lookups that were not
13519 ambiguous before may be ambiguous now. Therefore, we perform
13520 the lookup again. */
13521 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
13522 qualifying_scope
= tsubst (qualifying_scope
, args
,
13523 complain
, in_decl
);
13524 fns
= BASELINK_FUNCTIONS (baselink
);
13525 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
13526 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
13528 template_id_p
= true;
13529 template_args
= TREE_OPERAND (fns
, 1);
13530 fns
= TREE_OPERAND (fns
, 0);
13532 template_args
= tsubst_template_args (template_args
, args
,
13533 complain
, in_decl
);
13535 name
= DECL_NAME (get_first_fn (fns
));
13536 if (IDENTIFIER_TYPENAME_P (name
))
13537 name
= mangle_conv_op_name_for_type (optype
);
13538 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
13540 return error_mark_node
;
13542 /* If lookup found a single function, mark it as used at this
13543 point. (If it lookup found multiple functions the one selected
13544 later by overload resolution will be marked as used at that
13546 if (BASELINK_P (baselink
))
13547 fns
= BASELINK_FUNCTIONS (baselink
);
13548 if (!template_id_p
&& !really_overloaded_fn (fns
)
13549 && !mark_used (OVL_CURRENT (fns
), complain
) && !(complain
& tf_error
))
13550 return error_mark_node
;
13552 /* Add back the template arguments, if present. */
13553 if (BASELINK_P (baselink
) && template_id_p
)
13554 BASELINK_FUNCTIONS (baselink
)
13555 = build_nt (TEMPLATE_ID_EXPR
,
13556 BASELINK_FUNCTIONS (baselink
),
13558 /* Update the conversion operator type. */
13559 BASELINK_OPTYPE (baselink
) = optype
;
13562 object_type
= current_class_type
;
13565 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
13571 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13572 true if the qualified-id will be a postfix-expression in-and-of
13573 itself; false if more of the postfix-expression follows the
13574 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13578 tsubst_qualified_id (tree qualified_id
, tree args
,
13579 tsubst_flags_t complain
, tree in_decl
,
13580 bool done
, bool address_p
)
13586 tree template_args
;
13587 location_t loc
= UNKNOWN_LOCATION
;
13589 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
13591 /* Figure out what name to look up. */
13592 name
= TREE_OPERAND (qualified_id
, 1);
13593 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13595 is_template
= true;
13596 loc
= EXPR_LOCATION (name
);
13597 template_args
= TREE_OPERAND (name
, 1);
13599 template_args
= tsubst_template_args (template_args
, args
,
13600 complain
, in_decl
);
13601 name
= TREE_OPERAND (name
, 0);
13605 is_template
= false;
13606 template_args
= NULL_TREE
;
13609 /* Substitute into the qualifying scope. When there are no ARGS, we
13610 are just trying to simplify a non-dependent expression. In that
13611 case the qualifying scope may be dependent, and, in any case,
13612 substituting will not help. */
13613 scope
= TREE_OPERAND (qualified_id
, 0);
13616 scope
= tsubst (scope
, args
, complain
, in_decl
);
13617 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
13622 if (dependent_scope_p (scope
))
13625 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
13626 return build_qualified_name (NULL_TREE
, scope
, expr
,
13627 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
13630 if (!BASELINK_P (name
) && !DECL_P (expr
))
13632 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
13634 /* A BIT_NOT_EXPR is used to represent a destructor. */
13635 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
13637 error ("qualifying type %qT does not match destructor name ~%qT",
13638 scope
, TREE_OPERAND (expr
, 0));
13639 expr
= error_mark_node
;
13642 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
13643 /*is_type_p=*/0, false);
13646 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
13647 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
13648 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
13650 if (complain
& tf_error
)
13652 error ("dependent-name %qE is parsed as a non-type, but "
13653 "instantiation yields a type", qualified_id
);
13654 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
13656 return error_mark_node
;
13662 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
13664 /* Remember that there was a reference to this entity. */
13665 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
13666 return error_mark_node
;
13669 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
13671 if (complain
& tf_error
)
13672 qualified_name_lookup_error (scope
,
13673 TREE_OPERAND (qualified_id
, 1),
13674 expr
, input_location
);
13675 return error_mark_node
;
13679 expr
= lookup_template_function (expr
, template_args
);
13681 if (expr
== error_mark_node
&& complain
& tf_error
)
13682 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
13683 expr
, input_location
);
13684 else if (TYPE_P (scope
))
13686 expr
= (adjust_result_of_qualified_name_lookup
13687 (expr
, scope
, current_nonlambda_class_type ()));
13688 expr
= (finish_qualified_id_expr
13689 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
13690 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
13691 /*template_arg_p=*/false, complain
));
13694 /* Expressions do not generally have reference type. */
13695 if (TREE_CODE (expr
) != SCOPE_REF
13696 /* However, if we're about to form a pointer-to-member, we just
13697 want the referenced member referenced. */
13698 && TREE_CODE (expr
) != OFFSET_REF
)
13699 expr
= convert_from_reference (expr
);
13704 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13705 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13709 tsubst_init (tree init
, tree decl
, tree args
,
13710 tsubst_flags_t complain
, tree in_decl
)
13715 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
13719 /* If we had an initializer but it
13720 instantiated to nothing,
13721 value-initialize the object. This will
13722 only occur when the initializer was a
13723 pack expansion where the parameter packs
13724 used in that expansion were of length
13726 init
= build_value_init (TREE_TYPE (decl
),
13728 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13729 init
= get_target_expr_sfinae (init
, complain
);
13735 /* Like tsubst, but deals with expressions. This function just replaces
13736 template parms; to finish processing the resultant expression, use
13737 tsubst_copy_and_build or tsubst_expr. */
13740 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13742 enum tree_code code
;
13745 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
13748 code
= TREE_CODE (t
);
13753 r
= retrieve_local_specialization (t
);
13755 if (r
== NULL_TREE
)
13757 /* We get here for a use of 'this' in an NSDMI. */
13758 if (DECL_NAME (t
) == this_identifier
13759 && current_function_decl
13760 && DECL_CONSTRUCTOR_P (current_function_decl
))
13761 return current_class_ptr
;
13763 /* This can happen for a parameter name used later in a function
13764 declaration (such as in a late-specified return type). Just
13765 make a dummy decl, since it's only used for its type. */
13766 gcc_assert (cp_unevaluated_operand
!= 0);
13767 r
= tsubst_decl (t
, args
, complain
);
13768 /* Give it the template pattern as its context; its true context
13769 hasn't been instantiated yet and this is good enough for
13771 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
13774 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13775 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13776 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13777 return error_mark_node
;
13785 if (DECL_TEMPLATE_PARM_P (t
))
13786 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
13787 /* There is no need to substitute into namespace-scope
13789 if (DECL_NAMESPACE_SCOPE_P (t
))
13791 /* If ARGS is NULL, then T is known to be non-dependent. */
13792 if (args
== NULL_TREE
)
13793 return scalar_constant_value (t
);
13795 /* Unfortunately, we cannot just call lookup_name here.
13798 template <int I> int f() {
13800 struct S { void g() { E e = a; } };
13803 When we instantiate f<7>::S::g(), say, lookup_name is not
13804 clever enough to find f<7>::a. */
13806 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13807 /*entering_scope=*/0);
13809 for (v
= TYPE_VALUES (enum_type
);
13811 v
= TREE_CHAIN (v
))
13812 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
13813 return TREE_VALUE (v
);
13815 /* We didn't find the name. That should never happen; if
13816 name-lookup found it during preliminary parsing, we
13817 should find it again here during instantiation. */
13818 gcc_unreachable ();
13823 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13825 /* Check for a local specialization set up by
13826 tsubst_pack_expansion. */
13827 if (tree r
= retrieve_local_specialization (t
))
13829 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13830 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13834 /* When retrieving a capture pack from a generic lambda, remove the
13835 lambda call op's own template argument list from ARGS. Only the
13836 template arguments active for the closure type should be used to
13837 retrieve the pack specialization. */
13838 if (LAMBDA_FUNCTION_P (current_function_decl
)
13839 && (template_class_depth (DECL_CONTEXT (t
))
13840 != TMPL_ARGS_DEPTH (args
)))
13841 args
= strip_innermost_template_args (args
, 1);
13843 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13844 tsubst_decl put in the hash table. */
13845 return retrieve_specialization (t
, args
, 0);
13848 if (DECL_CONTEXT (t
))
13852 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13853 /*entering_scope=*/1);
13854 if (ctx
!= DECL_CONTEXT (t
))
13856 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
13859 if (complain
& tf_error
)
13860 error ("using invalid field %qD", t
);
13861 return error_mark_node
;
13870 case FUNCTION_DECL
:
13871 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
13872 r
= tsubst (t
, args
, complain
, in_decl
);
13873 else if (local_variable_p (t
))
13875 r
= retrieve_local_specialization (t
);
13876 if (r
== NULL_TREE
)
13878 /* First try name lookup to find the instantiation. */
13879 r
= lookup_name (DECL_NAME (t
));
13882 /* Make sure that the one we found is the one we want. */
13883 tree ctx
= DECL_CONTEXT (t
);
13884 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
13885 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
13886 if (ctx
!= DECL_CONTEXT (r
))
13894 /* This can happen for a variable used in a
13895 late-specified return type of a local lambda, or for a
13896 local static or constant. Building a new VAR_DECL
13897 should be OK in all those cases. */
13898 r
= tsubst_decl (t
, args
, complain
);
13899 if (decl_maybe_constant_var_p (r
))
13901 /* We can't call cp_finish_decl, so handle the
13902 initializer by hand. */
13903 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
13904 complain
, in_decl
);
13905 if (!processing_template_decl
)
13906 init
= maybe_constant_init (init
);
13907 if (processing_template_decl
13908 ? potential_constant_expression (init
)
13909 : reduced_constant_expression_p (init
))
13910 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
13911 = TREE_CONSTANT (r
) = true;
13912 DECL_INITIAL (r
) = init
;
13914 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
13915 || decl_constant_var_p (r
)
13916 || errorcount
|| sorrycount
);
13917 if (!processing_template_decl
)
13919 if (TREE_STATIC (r
))
13920 rest_of_decl_compilation (r
, toplevel_bindings_p (),
13923 r
= process_outer_var_ref (r
, complain
);
13926 /* Remember this for subsequent uses. */
13927 if (local_specializations
)
13928 register_local_specialization (r
, t
);
13933 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13934 return error_mark_node
;
13937 case NAMESPACE_DECL
:
13941 /* An OVERLOAD will always be a non-dependent overload set; an
13942 overload set from function scope will just be represented with an
13943 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13944 gcc_assert (!uses_template_parms (t
));
13948 return tsubst_baselink (t
, current_nonlambda_class_type (),
13949 args
, complain
, in_decl
);
13951 case TEMPLATE_DECL
:
13952 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13953 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
13954 args
, complain
, in_decl
);
13955 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
13956 return tsubst (t
, args
, complain
, in_decl
);
13957 else if (DECL_CLASS_SCOPE_P (t
)
13958 && uses_template_parms (DECL_CONTEXT (t
)))
13960 /* Template template argument like the following example need
13963 template <template <class> class TT> struct C {};
13964 template <class T> struct D {
13965 template <class U> struct E {};
13970 We are processing the template argument `E' in #1 for
13971 the template instantiation #2. Originally, `E' is a
13972 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13973 have to substitute this with one having context `D<int>'. */
13975 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
13976 return lookup_field (context
, DECL_NAME(t
), 0, false);
13979 /* Ordinary template template argument. */
13983 case REINTERPRET_CAST_EXPR
:
13984 case CONST_CAST_EXPR
:
13985 case STATIC_CAST_EXPR
:
13986 case DYNAMIC_CAST_EXPR
:
13987 case IMPLICIT_CONV_EXPR
:
13991 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13992 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13993 return build1 (code
, type
, op0
);
13997 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13999 tree expanded
, op
= TREE_OPERAND (t
, 0);
14002 if (SIZEOF_EXPR_TYPE_P (t
))
14003 op
= TREE_TYPE (op
);
14005 ++cp_unevaluated_operand
;
14006 ++c_inhibit_evaluation_warnings
;
14007 /* We only want to compute the number of arguments. */
14008 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14009 --cp_unevaluated_operand
;
14010 --c_inhibit_evaluation_warnings
;
14012 if (TREE_CODE (expanded
) == TREE_VEC
)
14014 len
= TREE_VEC_LENGTH (expanded
);
14015 /* Set TREE_USED for the benefit of -Wunused. */
14016 for (int i
= 0; i
< len
; i
++)
14017 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14020 if (expanded
== error_mark_node
)
14021 return error_mark_node
;
14022 else if (PACK_EXPANSION_P (expanded
)
14023 || (TREE_CODE (expanded
) == TREE_VEC
14025 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
14027 if (TREE_CODE (expanded
) == TREE_VEC
)
14028 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
14030 PACK_EXPANSION_SIZEOF_P (expanded
) = true;
14032 if (TYPE_P (expanded
))
14033 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14034 complain
& tf_error
);
14036 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14037 complain
& tf_error
);
14040 return build_int_cst (size_type_node
, len
);
14042 if (SIZEOF_EXPR_TYPE_P (t
))
14044 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14045 args
, complain
, in_decl
);
14046 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14047 r
= build1 (SIZEOF_EXPR
,
14048 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14049 SIZEOF_EXPR_TYPE_P (r
) = 1;
14056 case TRUTH_NOT_EXPR
:
14059 case UNARY_PLUS_EXPR
: /* Unary + */
14061 case AT_ENCODE_EXPR
:
14065 case REALPART_EXPR
:
14066 case IMAGPART_EXPR
:
14069 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14070 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14071 return build1 (code
, type
, op0
);
14074 case COMPONENT_REF
:
14079 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14080 name
= TREE_OPERAND (t
, 1);
14081 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14083 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14084 complain
, in_decl
);
14085 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14087 else if (TREE_CODE (name
) == SCOPE_REF
14088 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14090 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14091 complain
, in_decl
);
14092 name
= TREE_OPERAND (name
, 1);
14093 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14094 complain
, in_decl
);
14095 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14096 name
= build_qualified_name (/*type=*/NULL_TREE
,
14098 /*template_p=*/false);
14100 else if (BASELINK_P (name
))
14101 name
= tsubst_baselink (name
,
14102 non_reference (TREE_TYPE (object
)),
14106 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14107 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14113 case TRUNC_DIV_EXPR
:
14114 case CEIL_DIV_EXPR
:
14115 case FLOOR_DIV_EXPR
:
14116 case ROUND_DIV_EXPR
:
14117 case EXACT_DIV_EXPR
:
14121 case TRUNC_MOD_EXPR
:
14122 case FLOOR_MOD_EXPR
:
14123 case TRUTH_ANDIF_EXPR
:
14124 case TRUTH_ORIF_EXPR
:
14125 case TRUTH_AND_EXPR
:
14126 case TRUTH_OR_EXPR
:
14139 case COMPOUND_EXPR
:
14142 case PREDECREMENT_EXPR
:
14143 case PREINCREMENT_EXPR
:
14144 case POSTDECREMENT_EXPR
:
14145 case POSTINCREMENT_EXPR
:
14147 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14148 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14149 return build_nt (code
, op0
, op1
);
14154 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14155 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14156 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14157 QUALIFIED_NAME_IS_TEMPLATE (t
));
14162 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14163 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14164 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14169 int n
= VL_EXP_OPERAND_LENGTH (t
);
14170 tree result
= build_vl_exp (CALL_EXPR
, n
);
14172 for (i
= 0; i
< n
; i
++)
14173 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14174 complain
, in_decl
);
14180 case PSEUDO_DTOR_EXPR
:
14181 case VEC_PERM_EXPR
:
14183 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14184 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14185 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14186 r
= build_nt (code
, op0
, op1
, op2
);
14187 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14193 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14194 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14195 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14196 r
= build_nt (code
, op0
, op1
, op2
);
14197 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14203 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14204 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14205 r
= build_nt (code
, op0
, op1
);
14206 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14207 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14211 case TEMPLATE_ID_EXPR
:
14213 /* Substituted template arguments */
14214 tree fn
= TREE_OPERAND (t
, 0);
14215 tree targs
= TREE_OPERAND (t
, 1);
14217 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14219 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14221 return lookup_template_function (fn
, targs
);
14226 tree purpose
, value
, chain
;
14228 if (t
== void_list_node
)
14231 purpose
= TREE_PURPOSE (t
);
14233 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
14234 value
= TREE_VALUE (t
);
14236 value
= tsubst_copy (value
, args
, complain
, in_decl
);
14237 chain
= TREE_CHAIN (t
);
14238 if (chain
&& chain
!= void_type_node
)
14239 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
14240 if (purpose
== TREE_PURPOSE (t
)
14241 && value
== TREE_VALUE (t
)
14242 && chain
== TREE_CHAIN (t
))
14244 return tree_cons (purpose
, value
, chain
);
14249 case ENUMERAL_TYPE
:
14251 case TEMPLATE_TYPE_PARM
:
14252 case TEMPLATE_TEMPLATE_PARM
:
14253 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14254 case TEMPLATE_PARM_INDEX
:
14256 case REFERENCE_TYPE
:
14258 case FUNCTION_TYPE
:
14261 case TYPENAME_TYPE
:
14262 case UNBOUND_CLASS_TEMPLATE
:
14264 case DECLTYPE_TYPE
:
14266 return tsubst (t
, args
, complain
, in_decl
);
14270 /* Fall through. */
14271 case IDENTIFIER_NODE
:
14272 if (IDENTIFIER_TYPENAME_P (t
))
14274 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14275 return mangle_conv_op_name_for_type (new_type
);
14281 /* This is handled by tsubst_copy_and_build. */
14282 gcc_unreachable ();
14286 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14287 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14288 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
14291 case CLEANUP_POINT_EXPR
:
14292 /* We shouldn't have built any of these during initial template
14293 generation. Instead, they should be built during instantiation
14294 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14295 gcc_unreachable ();
14299 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14300 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14301 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14302 r
= build2 (code
, type
, op0
, op1
);
14303 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
14304 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
14305 && !(complain
& tf_error
))
14306 return error_mark_node
;
14310 case EXPR_PACK_EXPANSION
:
14311 error ("invalid use of pack expansion expression");
14312 return error_mark_node
;
14314 case NONTYPE_ARGUMENT_PACK
:
14315 error ("use %<...%> to expand argument pack");
14316 return error_mark_node
;
14319 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
14327 /* Instantiate any typedefs in the type. */
14328 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14329 r
= fold_convert (type
, t
);
14330 gcc_assert (TREE_CODE (r
) == code
);
14335 /* These can sometimes show up in a partial instantiation, but never
14336 involve template parms. */
14337 gcc_assert (!uses_template_parms (t
));
14340 case UNARY_LEFT_FOLD_EXPR
:
14341 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
14342 case UNARY_RIGHT_FOLD_EXPR
:
14343 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
14344 case BINARY_LEFT_FOLD_EXPR
:
14345 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
14346 case BINARY_RIGHT_FOLD_EXPR
:
14347 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
14350 /* We shouldn't get here, but keep going if !flag_checking. */
14352 gcc_unreachable ();
14357 /* Helper function for tsubst_omp_clauses, used for instantiation of
14358 OMP_CLAUSE_DECL of clauses. */
14361 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
14364 if (decl
== NULL_TREE
)
14367 /* Handle an OpenMP array section represented as a TREE_LIST (or
14368 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14369 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14370 TREE_LIST. We can handle it exactly the same as an array section
14371 (purpose, value, and a chain), even though the nomenclature
14372 (low_bound, length, etc) is different. */
14373 if (TREE_CODE (decl
) == TREE_LIST
)
14376 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
14377 /*integral_constant_expression_p=*/false);
14378 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
14379 /*integral_constant_expression_p=*/false);
14380 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
14382 if (TREE_PURPOSE (decl
) == low_bound
14383 && TREE_VALUE (decl
) == length
14384 && TREE_CHAIN (decl
) == chain
)
14386 tree ret
= tree_cons (low_bound
, length
, chain
);
14387 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
14388 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
14391 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
14392 /*integral_constant_expression_p=*/false);
14393 /* Undo convert_from_reference tsubst_expr could have called. */
14395 && REFERENCE_REF_P (ret
)
14396 && !REFERENCE_REF_P (decl
))
14397 ret
= TREE_OPERAND (ret
, 0);
14401 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14404 tsubst_omp_clauses (tree clauses
, bool declare_simd
, bool allow_fields
,
14405 tree args
, tsubst_flags_t complain
, tree in_decl
)
14407 tree new_clauses
= NULL_TREE
, nc
, oc
;
14408 tree linear_no_step
= NULL_TREE
;
14410 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
14412 nc
= copy_node (oc
);
14413 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
14416 switch (OMP_CLAUSE_CODE (nc
))
14418 case OMP_CLAUSE_LASTPRIVATE
:
14419 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
14421 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
14422 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
14423 in_decl
, /*integral_constant_expression_p=*/false);
14424 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
14425 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
14428 case OMP_CLAUSE_PRIVATE
:
14429 case OMP_CLAUSE_SHARED
:
14430 case OMP_CLAUSE_FIRSTPRIVATE
:
14431 case OMP_CLAUSE_COPYIN
:
14432 case OMP_CLAUSE_COPYPRIVATE
:
14433 case OMP_CLAUSE_UNIFORM
:
14434 case OMP_CLAUSE_DEPEND
:
14435 case OMP_CLAUSE_FROM
:
14436 case OMP_CLAUSE_TO
:
14437 case OMP_CLAUSE_MAP
:
14438 case OMP_CLAUSE_USE_DEVICE_PTR
:
14439 case OMP_CLAUSE_IS_DEVICE_PTR
:
14440 OMP_CLAUSE_DECL (nc
)
14441 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14444 case OMP_CLAUSE_IF
:
14445 case OMP_CLAUSE_NUM_THREADS
:
14446 case OMP_CLAUSE_SCHEDULE
:
14447 case OMP_CLAUSE_COLLAPSE
:
14448 case OMP_CLAUSE_FINAL
:
14449 case OMP_CLAUSE_DEVICE
:
14450 case OMP_CLAUSE_DIST_SCHEDULE
:
14451 case OMP_CLAUSE_NUM_TEAMS
:
14452 case OMP_CLAUSE_THREAD_LIMIT
:
14453 case OMP_CLAUSE_SAFELEN
:
14454 case OMP_CLAUSE_SIMDLEN
:
14455 case OMP_CLAUSE_NUM_TASKS
:
14456 case OMP_CLAUSE_GRAINSIZE
:
14457 case OMP_CLAUSE_PRIORITY
:
14458 case OMP_CLAUSE_ORDERED
:
14459 case OMP_CLAUSE_HINT
:
14460 case OMP_CLAUSE_NUM_GANGS
:
14461 case OMP_CLAUSE_NUM_WORKERS
:
14462 case OMP_CLAUSE_VECTOR_LENGTH
:
14463 case OMP_CLAUSE_WORKER
:
14464 case OMP_CLAUSE_VECTOR
:
14465 case OMP_CLAUSE_ASYNC
:
14466 case OMP_CLAUSE_WAIT
:
14467 OMP_CLAUSE_OPERAND (nc
, 0)
14468 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14469 in_decl
, /*integral_constant_expression_p=*/false);
14471 case OMP_CLAUSE_REDUCTION
:
14472 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
14474 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
14475 if (TREE_CODE (placeholder
) == SCOPE_REF
)
14477 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
14478 complain
, in_decl
);
14479 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
14480 = build_qualified_name (NULL_TREE
, scope
,
14481 TREE_OPERAND (placeholder
, 1),
14485 gcc_assert (identifier_p (placeholder
));
14487 OMP_CLAUSE_DECL (nc
)
14488 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14491 case OMP_CLAUSE_GANG
:
14492 case OMP_CLAUSE_ALIGNED
:
14493 OMP_CLAUSE_DECL (nc
)
14494 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14496 OMP_CLAUSE_OPERAND (nc
, 1)
14497 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
14498 in_decl
, /*integral_constant_expression_p=*/false);
14500 case OMP_CLAUSE_LINEAR
:
14501 OMP_CLAUSE_DECL (nc
)
14502 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14504 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
14506 gcc_assert (!linear_no_step
);
14507 linear_no_step
= nc
;
14509 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
14510 OMP_CLAUSE_LINEAR_STEP (nc
)
14511 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
14512 complain
, in_decl
);
14514 OMP_CLAUSE_LINEAR_STEP (nc
)
14515 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
14517 /*integral_constant_expression_p=*/false);
14519 case OMP_CLAUSE_NOWAIT
:
14520 case OMP_CLAUSE_DEFAULT
:
14521 case OMP_CLAUSE_UNTIED
:
14522 case OMP_CLAUSE_MERGEABLE
:
14523 case OMP_CLAUSE_INBRANCH
:
14524 case OMP_CLAUSE_NOTINBRANCH
:
14525 case OMP_CLAUSE_PROC_BIND
:
14526 case OMP_CLAUSE_FOR
:
14527 case OMP_CLAUSE_PARALLEL
:
14528 case OMP_CLAUSE_SECTIONS
:
14529 case OMP_CLAUSE_TASKGROUP
:
14530 case OMP_CLAUSE_NOGROUP
:
14531 case OMP_CLAUSE_THREADS
:
14532 case OMP_CLAUSE_SIMD
:
14533 case OMP_CLAUSE_DEFAULTMAP
:
14534 case OMP_CLAUSE_INDEPENDENT
:
14535 case OMP_CLAUSE_AUTO
:
14536 case OMP_CLAUSE_SEQ
:
14538 case OMP_CLAUSE_TILE
:
14541 for (lnc
= OMP_CLAUSE_TILE_LIST (nc
),
14542 loc
= OMP_CLAUSE_TILE_LIST (oc
);
14544 loc
= TREE_CHAIN (loc
), lnc
= TREE_CHAIN (lnc
))
14546 TREE_VALUE (lnc
) = tsubst_expr (TREE_VALUE (loc
), args
,
14547 complain
, in_decl
, false);
14552 gcc_unreachable ();
14555 switch (OMP_CLAUSE_CODE (nc
))
14557 case OMP_CLAUSE_SHARED
:
14558 case OMP_CLAUSE_PRIVATE
:
14559 case OMP_CLAUSE_FIRSTPRIVATE
:
14560 case OMP_CLAUSE_LASTPRIVATE
:
14561 case OMP_CLAUSE_COPYPRIVATE
:
14562 case OMP_CLAUSE_LINEAR
:
14563 case OMP_CLAUSE_REDUCTION
:
14564 case OMP_CLAUSE_USE_DEVICE_PTR
:
14565 case OMP_CLAUSE_IS_DEVICE_PTR
:
14566 /* tsubst_expr on SCOPE_REF results in returning
14567 finish_non_static_data_member result. Undo that here. */
14568 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
14569 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
14570 == IDENTIFIER_NODE
))
14572 tree t
= OMP_CLAUSE_DECL (nc
);
14575 switch (TREE_CODE (v
))
14577 case COMPONENT_REF
:
14581 case POINTER_PLUS_EXPR
:
14582 v
= TREE_OPERAND (v
, 0);
14585 if (DECL_CONTEXT (v
) == current_function_decl
14586 && DECL_ARTIFICIAL (v
)
14587 && DECL_NAME (v
) == this_identifier
)
14588 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
14595 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
14596 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
14597 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
14598 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
14599 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
14601 tree decl
= OMP_CLAUSE_DECL (nc
);
14604 if (!DECL_LANG_SPECIFIC (decl
))
14605 retrofit_lang_decl (decl
);
14606 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
14615 new_clauses
= nreverse (new_clauses
);
14618 new_clauses
= finish_omp_clauses (new_clauses
, allow_fields
);
14619 if (linear_no_step
)
14620 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
14621 if (nc
== linear_no_step
)
14623 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
14627 return new_clauses
;
14630 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14633 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
14636 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14638 tree purpose
, value
, chain
;
14643 if (TREE_CODE (t
) != TREE_LIST
)
14644 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14645 /*function_p=*/false,
14646 /*integral_constant_expression_p=*/false);
14648 if (t
== void_list_node
)
14651 purpose
= TREE_PURPOSE (t
);
14653 purpose
= RECUR (purpose
);
14654 value
= TREE_VALUE (t
);
14657 if (TREE_CODE (value
) != LABEL_DECL
)
14658 value
= RECUR (value
);
14661 value
= lookup_label (DECL_NAME (value
));
14662 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
14663 TREE_USED (value
) = 1;
14666 chain
= TREE_CHAIN (t
);
14667 if (chain
&& chain
!= void_type_node
)
14668 chain
= RECUR (chain
);
14669 return tree_cons (purpose
, value
, chain
);
14673 /* Used to temporarily communicate the list of #pragma omp parallel
14674 clauses to #pragma omp for instantiation if they are combined
14677 static tree
*omp_parallel_combined_clauses
;
14679 /* Substitute one OMP_FOR iterator. */
14682 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
14683 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
14684 tree args
, tsubst_flags_t complain
, tree in_decl
,
14685 bool integral_constant_expression_p
)
14687 #define RECUR(NODE) \
14688 tsubst_expr ((NODE), args, complain, in_decl, \
14689 integral_constant_expression_p)
14690 tree decl
, init
, cond
, incr
;
14692 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
14693 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
14695 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
14697 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
14698 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
14701 decl
= TREE_OPERAND (init
, 0);
14702 init
= TREE_OPERAND (init
, 1);
14703 tree decl_expr
= NULL_TREE
;
14704 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
14706 /* We need to jump through some hoops to handle declarations in the
14707 for-init-statement, since we might need to handle auto deduction,
14708 but we need to keep control of initialization. */
14710 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
14711 decl
= tsubst_decl (decl
, args
, complain
);
14715 if (TREE_CODE (decl
) == SCOPE_REF
)
14717 decl
= RECUR (decl
);
14718 if (TREE_CODE (decl
) == COMPONENT_REF
)
14722 switch (TREE_CODE (v
))
14724 case COMPONENT_REF
:
14728 case POINTER_PLUS_EXPR
:
14729 v
= TREE_OPERAND (v
, 0);
14732 if (DECL_CONTEXT (v
) == current_function_decl
14733 && DECL_ARTIFICIAL (v
)
14734 && DECL_NAME (v
) == this_identifier
)
14736 decl
= TREE_OPERAND (decl
, 1);
14737 decl
= omp_privatize_field (decl
, false);
14747 decl
= RECUR (decl
);
14749 init
= RECUR (init
);
14751 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
14752 if (auto_node
&& init
)
14754 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
14756 gcc_assert (!type_dependent_expression_p (decl
));
14758 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14762 /* Declare the variable, but don't let that initialize it. */
14763 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
14764 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
14766 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
14769 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
14770 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14771 if (TREE_CODE (incr
) == MODIFY_EXPR
)
14773 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14774 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
14775 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
14776 NOP_EXPR
, rhs
, complain
);
14779 incr
= RECUR (incr
);
14780 TREE_VEC_ELT (declv
, i
) = decl
;
14781 TREE_VEC_ELT (initv
, i
) = init
;
14782 TREE_VEC_ELT (condv
, i
) = cond
;
14783 TREE_VEC_ELT (incrv
, i
) = incr
;
14789 /* Declare and initialize the variable. */
14797 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
14799 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
14801 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
14802 && OMP_CLAUSE_DECL (*pc
) == decl
)
14804 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
14805 && OMP_CLAUSE_DECL (*pc
) == decl
)
14809 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14811 *pc
= OMP_CLAUSE_CHAIN (c
);
14812 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14815 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
14816 && OMP_CLAUSE_DECL (*pc
) == decl
)
14818 error ("iteration variable %qD should not be firstprivate",
14820 *pc
= OMP_CLAUSE_CHAIN (*pc
);
14822 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
14823 && OMP_CLAUSE_DECL (*pc
) == decl
)
14825 error ("iteration variable %qD should not be reduction",
14827 *pc
= OMP_CLAUSE_CHAIN (*pc
);
14830 pc
= &OMP_CLAUSE_CHAIN (*pc
);
14835 if (*pc
== NULL_TREE
)
14837 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
14838 OMP_CLAUSE_DECL (c
) = decl
;
14839 c
= finish_omp_clauses (c
, true);
14842 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14847 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
14848 if (COMPARISON_CLASS_P (cond
))
14850 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
14851 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
14852 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
14855 cond
= RECUR (cond
);
14856 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14857 switch (TREE_CODE (incr
))
14859 case PREINCREMENT_EXPR
:
14860 case PREDECREMENT_EXPR
:
14861 case POSTINCREMENT_EXPR
:
14862 case POSTDECREMENT_EXPR
:
14863 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
14864 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
14867 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14868 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14870 tree rhs
= TREE_OPERAND (incr
, 1);
14871 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14872 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14873 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14874 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14875 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14879 incr
= RECUR (incr
);
14882 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14883 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14885 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14886 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14887 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
14888 TREE_TYPE (decl
), lhs
,
14889 RECUR (TREE_OPERAND (incr
, 2))));
14891 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
14892 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
14893 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
14895 tree rhs
= TREE_OPERAND (incr
, 2);
14896 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14897 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14898 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14899 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14900 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14904 incr
= RECUR (incr
);
14907 incr
= RECUR (incr
);
14911 TREE_VEC_ELT (declv
, i
) = decl
;
14912 TREE_VEC_ELT (initv
, i
) = init
;
14913 TREE_VEC_ELT (condv
, i
) = cond
;
14914 TREE_VEC_ELT (incrv
, i
) = incr
;
14918 /* Helper function of tsubst_expr, find OMP_TEAMS inside
14919 of OMP_TARGET's body. */
14922 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
14924 *walk_subtrees
= 0;
14925 switch (TREE_CODE (*tp
))
14930 case STATEMENT_LIST
:
14931 *walk_subtrees
= 1;
14939 /* Like tsubst_copy for expressions, etc. but also does semantic
14943 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
14944 bool integral_constant_expression_p
)
14946 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14947 #define RECUR(NODE) \
14948 tsubst_expr ((NODE), args, complain, in_decl, \
14949 integral_constant_expression_p)
14955 if (t
== NULL_TREE
|| t
== error_mark_node
)
14958 loc
= input_location
;
14959 if (EXPR_HAS_LOCATION (t
))
14960 input_location
= EXPR_LOCATION (t
);
14961 if (STATEMENT_CODE_P (TREE_CODE (t
)))
14962 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
14964 switch (TREE_CODE (t
))
14966 case STATEMENT_LIST
:
14968 tree_stmt_iterator i
;
14969 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
14970 RECUR (tsi_stmt (i
));
14974 case CTOR_INITIALIZER
:
14975 finish_mem_initializers (tsubst_initializer_list
14976 (TREE_OPERAND (t
, 0), args
));
14980 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
14984 tmp
= RECUR (EXPR_STMT_EXPR (t
));
14985 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
14986 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
14988 finish_expr_stmt (tmp
);
14992 do_using_directive (USING_STMT_NAMESPACE (t
));
14997 tree decl
, pattern_decl
;
15000 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
15001 if (TREE_CODE (decl
) == LABEL_DECL
)
15002 finish_label_decl (DECL_NAME (decl
));
15003 else if (TREE_CODE (decl
) == USING_DECL
)
15005 tree scope
= USING_DECL_SCOPE (decl
);
15006 tree name
= DECL_NAME (decl
);
15009 scope
= tsubst (scope
, args
, complain
, in_decl
);
15010 decl
= lookup_qualified_name (scope
, name
,
15011 /*is_type_p=*/false,
15012 /*complain=*/false);
15013 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15014 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15016 do_local_using_decl (decl
, scope
, name
);
15018 else if (DECL_PACK_P (decl
))
15020 /* Don't build up decls for a variadic capture proxy, we'll
15021 instantiate the elements directly as needed. */
15026 init
= DECL_INITIAL (decl
);
15027 decl
= tsubst (decl
, args
, complain
, in_decl
);
15028 if (decl
!= error_mark_node
)
15030 /* By marking the declaration as instantiated, we avoid
15031 trying to instantiate it. Since instantiate_decl can't
15032 handle local variables, and since we've already done
15033 all that needs to be done, that's the right thing to
15036 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15038 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15039 /* Anonymous aggregates are a special case. */
15040 finish_anon_union (decl
);
15041 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15043 DECL_CONTEXT (decl
) = current_function_decl
;
15044 if (DECL_NAME (decl
) == this_identifier
)
15046 tree lam
= DECL_CONTEXT (current_function_decl
);
15047 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15048 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15050 insert_capture_proxy (decl
);
15052 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15053 /* We already did a pushtag. */;
15054 else if (TREE_CODE (decl
) == FUNCTION_DECL
15055 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15056 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15058 DECL_CONTEXT (decl
) = NULL_TREE
;
15060 DECL_CONTEXT (decl
) = current_function_decl
;
15061 cp_check_omp_declare_reduction (decl
);
15065 int const_init
= false;
15066 maybe_push_decl (decl
);
15068 && DECL_PRETTY_FUNCTION_P (decl
))
15070 /* For __PRETTY_FUNCTION__ we have to adjust the
15072 const char *const name
15073 = cxx_printable_name (current_function_decl
, 2);
15074 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15077 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15080 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15082 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15091 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15092 RECUR (FOR_INIT_STMT (t
));
15093 finish_for_init_stmt (stmt
);
15094 tmp
= RECUR (FOR_COND (t
));
15095 finish_for_cond (tmp
, stmt
, false);
15096 tmp
= RECUR (FOR_EXPR (t
));
15097 finish_for_expr (tmp
, stmt
);
15098 RECUR (FOR_BODY (t
));
15099 finish_for_stmt (stmt
);
15102 case RANGE_FOR_STMT
:
15105 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15106 decl
= RANGE_FOR_DECL (t
);
15107 decl
= tsubst (decl
, args
, complain
, in_decl
);
15108 maybe_push_decl (decl
);
15109 expr
= RECUR (RANGE_FOR_EXPR (t
));
15110 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
15111 RECUR (RANGE_FOR_BODY (t
));
15112 finish_for_stmt (stmt
);
15117 stmt
= begin_while_stmt ();
15118 tmp
= RECUR (WHILE_COND (t
));
15119 finish_while_stmt_cond (tmp
, stmt
, false);
15120 RECUR (WHILE_BODY (t
));
15121 finish_while_stmt (stmt
);
15125 stmt
= begin_do_stmt ();
15126 RECUR (DO_BODY (t
));
15127 finish_do_body (stmt
);
15128 tmp
= RECUR (DO_COND (t
));
15129 finish_do_stmt (tmp
, stmt
, false);
15133 stmt
= begin_if_stmt ();
15134 tmp
= RECUR (IF_COND (t
));
15135 finish_if_stmt_cond (tmp
, stmt
);
15136 RECUR (THEN_CLAUSE (t
));
15137 finish_then_clause (stmt
);
15139 if (ELSE_CLAUSE (t
))
15141 begin_else_clause (stmt
);
15142 RECUR (ELSE_CLAUSE (t
));
15143 finish_else_clause (stmt
);
15146 finish_if_stmt (stmt
);
15150 if (BIND_EXPR_BODY_BLOCK (t
))
15151 stmt
= begin_function_body ();
15153 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
15154 ? BCS_TRY_BLOCK
: 0);
15156 RECUR (BIND_EXPR_BODY (t
));
15158 if (BIND_EXPR_BODY_BLOCK (t
))
15159 finish_function_body (stmt
);
15161 finish_compound_stmt (stmt
);
15165 finish_break_stmt ();
15168 case CONTINUE_STMT
:
15169 finish_continue_stmt ();
15173 stmt
= begin_switch_stmt ();
15174 tmp
= RECUR (SWITCH_STMT_COND (t
));
15175 finish_switch_cond (tmp
, stmt
);
15176 RECUR (SWITCH_STMT_BODY (t
));
15177 finish_switch_stmt (stmt
);
15180 case CASE_LABEL_EXPR
:
15182 tree low
= RECUR (CASE_LOW (t
));
15183 tree high
= RECUR (CASE_HIGH (t
));
15184 finish_case_label (EXPR_LOCATION (t
), low
, high
);
15190 tree decl
= LABEL_EXPR_LABEL (t
);
15193 label
= finish_label_stmt (DECL_NAME (decl
));
15194 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
15195 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
15200 tmp
= GOTO_DESTINATION (t
);
15201 if (TREE_CODE (tmp
) != LABEL_DECL
)
15202 /* Computed goto's must be tsubst'd into. On the other hand,
15203 non-computed gotos must not be; the identifier in question
15204 will have no binding. */
15207 tmp
= DECL_NAME (tmp
);
15208 finish_goto_stmt (tmp
);
15213 tree string
= RECUR (ASM_STRING (t
));
15214 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
15215 complain
, in_decl
);
15216 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
15217 complain
, in_decl
);
15218 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
15219 complain
, in_decl
);
15220 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
15221 complain
, in_decl
);
15222 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
15224 tree asm_expr
= tmp
;
15225 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
15226 asm_expr
= TREE_OPERAND (asm_expr
, 0);
15227 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
15234 stmt
= begin_try_block ();
15235 RECUR (TRY_STMTS (t
));
15236 finish_cleanup_try_block (stmt
);
15237 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
15241 tree compound_stmt
= NULL_TREE
;
15243 if (FN_TRY_BLOCK_P (t
))
15244 stmt
= begin_function_try_block (&compound_stmt
);
15246 stmt
= begin_try_block ();
15248 RECUR (TRY_STMTS (t
));
15250 if (FN_TRY_BLOCK_P (t
))
15251 finish_function_try_block (stmt
);
15253 finish_try_block (stmt
);
15255 RECUR (TRY_HANDLERS (t
));
15256 if (FN_TRY_BLOCK_P (t
))
15257 finish_function_handler_sequence (stmt
, compound_stmt
);
15259 finish_handler_sequence (stmt
);
15265 tree decl
= HANDLER_PARMS (t
);
15269 decl
= tsubst (decl
, args
, complain
, in_decl
);
15270 /* Prevent instantiate_decl from trying to instantiate
15271 this variable. We've already done all that needs to be
15273 if (decl
!= error_mark_node
)
15274 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15276 stmt
= begin_handler ();
15277 finish_handler_parms (decl
, stmt
);
15278 RECUR (HANDLER_BODY (t
));
15279 finish_handler (stmt
);
15284 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
15285 if (CLASS_TYPE_P (tmp
))
15287 /* Local classes are not independent templates; they are
15288 instantiated along with their containing function. And this
15289 way we don't have to deal with pushing out of one local class
15290 to instantiate a member of another local class. */
15292 /* Closures are handled by the LAMBDA_EXPR. */
15293 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
15294 complete_type (tmp
);
15295 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
15296 if (!DECL_ARTIFICIAL (fn
))
15297 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
15301 case STATIC_ASSERT
:
15305 ++c_inhibit_evaluation_warnings
;
15307 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
15310 /*integral_constant_expression_p=*/true);
15311 --c_inhibit_evaluation_warnings
;
15313 finish_static_assert (condition
,
15314 STATIC_ASSERT_MESSAGE (t
),
15315 STATIC_ASSERT_SOURCE_LOCATION (t
),
15316 /*member_p=*/false);
15321 case OACC_PARALLEL
:
15322 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false, false, args
, complain
,
15324 stmt
= begin_omp_parallel ();
15325 RECUR (OMP_BODY (t
));
15326 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
15330 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
15331 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false, true,
15332 args
, complain
, in_decl
);
15333 if (OMP_PARALLEL_COMBINED (t
))
15334 omp_parallel_combined_clauses
= &tmp
;
15335 stmt
= begin_omp_parallel ();
15336 RECUR (OMP_PARALLEL_BODY (t
));
15337 gcc_assert (omp_parallel_combined_clauses
== NULL
);
15338 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
15339 = OMP_PARALLEL_COMBINED (t
);
15340 pop_omp_privatization_clauses (r
);
15344 r
= push_omp_privatization_clauses (false);
15345 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false, true,
15346 args
, complain
, in_decl
);
15347 stmt
= begin_omp_task ();
15348 RECUR (OMP_TASK_BODY (t
));
15349 finish_omp_task (tmp
, stmt
);
15350 pop_omp_privatization_clauses (r
);
15357 case OMP_DISTRIBUTE
:
15361 tree clauses
, body
, pre_body
;
15362 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
15363 tree orig_declv
= NULL_TREE
;
15364 tree incrv
= NULL_TREE
;
15367 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
15368 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
15369 TREE_CODE (t
) != OACC_LOOP
,
15370 args
, complain
, in_decl
);
15371 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15373 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15374 if (OMP_FOR_ORIG_DECLS (t
))
15375 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15376 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15377 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15378 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15381 stmt
= begin_omp_structured_block ();
15383 pre_body
= push_stmt_list ();
15384 RECUR (OMP_FOR_PRE_BODY (t
));
15385 pre_body
= pop_stmt_list (pre_body
);
15387 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15388 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
15389 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
15390 incrv
, &clauses
, args
, complain
, in_decl
,
15391 integral_constant_expression_p
);
15392 omp_parallel_combined_clauses
= NULL
;
15394 body
= push_stmt_list ();
15395 RECUR (OMP_FOR_BODY (t
));
15396 body
= pop_stmt_list (body
);
15398 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15399 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
15400 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
15404 t
= make_node (TREE_CODE (t
));
15405 TREE_TYPE (t
) = void_type_node
;
15406 OMP_FOR_BODY (t
) = body
;
15407 OMP_FOR_PRE_BODY (t
) = pre_body
;
15408 OMP_FOR_CLAUSES (t
) = clauses
;
15409 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
15413 add_stmt (finish_omp_structured_block (stmt
));
15414 pop_omp_privatization_clauses (r
);
15419 omp_parallel_combined_clauses
= NULL
;
15424 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
15425 && OMP_TEAMS_COMBINED (t
));
15426 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false, true,
15427 args
, complain
, in_decl
);
15428 stmt
= push_stmt_list ();
15429 RECUR (OMP_BODY (t
));
15430 stmt
= pop_stmt_list (stmt
);
15433 OMP_BODY (t
) = stmt
;
15434 OMP_CLAUSES (t
) = tmp
;
15436 pop_omp_privatization_clauses (r
);
15440 case OMP_TARGET_DATA
:
15442 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
15443 TREE_CODE (t
) != OACC_DATA
,
15444 args
, complain
, in_decl
);
15445 keep_next_level (true);
15446 stmt
= begin_omp_structured_block ();
15448 RECUR (OMP_BODY (t
));
15449 stmt
= finish_omp_structured_block (stmt
);
15452 OMP_BODY (t
) = stmt
;
15453 OMP_CLAUSES (t
) = tmp
;
15454 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
15456 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
15459 /* For combined target teams, ensure the num_teams and
15460 thread_limit clause expressions are evaluated on the host,
15461 before entering the target construct. */
15463 for (c
= OMP_TEAMS_CLAUSES (teams
);
15464 c
; c
= OMP_CLAUSE_CHAIN (c
))
15465 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
15466 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
15467 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
15469 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
15470 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
15471 if (expr
== error_mark_node
)
15473 tmp
= TARGET_EXPR_SLOT (expr
);
15475 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
15476 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
15477 OMP_CLAUSE_FIRSTPRIVATE
);
15478 OMP_CLAUSE_DECL (tc
) = tmp
;
15479 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
15480 OMP_TARGET_CLAUSES (t
) = tc
;
15489 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), false, false,
15490 args
, complain
, in_decl
);
15491 OACC_DECLARE_CLAUSES (t
) = tmp
;
15495 case OMP_TARGET_UPDATE
:
15496 case OMP_TARGET_ENTER_DATA
:
15497 case OMP_TARGET_EXIT_DATA
:
15498 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), false, true,
15499 args
, complain
, in_decl
);
15501 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15505 case OACC_ENTER_DATA
:
15506 case OACC_EXIT_DATA
:
15508 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), false, false,
15509 args
, complain
, in_decl
);
15511 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15516 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), false, true,
15517 args
, complain
, in_decl
);
15518 stmt
= push_stmt_list ();
15519 RECUR (OMP_BODY (t
));
15520 stmt
= pop_stmt_list (stmt
);
15523 OMP_BODY (t
) = stmt
;
15524 OMP_ORDERED_CLAUSES (t
) = tmp
;
15530 case OMP_TASKGROUP
:
15531 stmt
= push_stmt_list ();
15532 RECUR (OMP_BODY (t
));
15533 stmt
= pop_stmt_list (stmt
);
15536 OMP_BODY (t
) = stmt
;
15541 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
15542 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
15544 tree op1
= TREE_OPERAND (t
, 1);
15545 tree rhs1
= NULL_TREE
;
15547 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
15549 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
15550 op1
= TREE_OPERAND (op1
, 1);
15552 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15553 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15554 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
15555 NULL_TREE
, NULL_TREE
, rhs1
,
15556 OMP_ATOMIC_SEQ_CST (t
));
15560 tree op1
= TREE_OPERAND (t
, 1);
15561 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
15562 tree rhs1
= NULL_TREE
;
15563 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
15564 enum tree_code opcode
= NOP_EXPR
;
15565 if (code
== OMP_ATOMIC_READ
)
15567 v
= RECUR (TREE_OPERAND (op1
, 0));
15568 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15570 else if (code
== OMP_ATOMIC_CAPTURE_OLD
15571 || code
== OMP_ATOMIC_CAPTURE_NEW
)
15573 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
15574 v
= RECUR (TREE_OPERAND (op1
, 0));
15575 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15576 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
15578 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
15579 op11
= TREE_OPERAND (op11
, 1);
15581 lhs
= RECUR (TREE_OPERAND (op11
, 0));
15582 rhs
= RECUR (TREE_OPERAND (op11
, 1));
15583 opcode
= TREE_CODE (op11
);
15584 if (opcode
== MODIFY_EXPR
)
15590 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15591 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15593 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
15594 OMP_ATOMIC_SEQ_CST (t
));
15598 case TRANSACTION_EXPR
:
15601 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
15602 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
15604 if (TRANSACTION_EXPR_IS_STMT (t
))
15606 tree body
= TRANSACTION_EXPR_BODY (t
);
15607 tree noex
= NULL_TREE
;
15608 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
15610 noex
= MUST_NOT_THROW_COND (body
);
15611 if (noex
== NULL_TREE
)
15612 noex
= boolean_true_node
;
15613 body
= TREE_OPERAND (body
, 0);
15615 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
15617 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
15621 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
15622 RECUR (TRANSACTION_EXPR_BODY (t
)),
15629 case MUST_NOT_THROW_EXPR
:
15631 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15632 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
15633 RETURN (build_must_not_throw_expr (op0
, cond
));
15636 case EXPR_PACK_EXPANSION
:
15637 error ("invalid use of pack expansion expression");
15638 RETURN (error_mark_node
);
15640 case NONTYPE_ARGUMENT_PACK
:
15641 error ("use %<...%> to expand argument pack");
15642 RETURN (error_mark_node
);
15644 case CILK_SPAWN_STMT
:
15645 cfun
->calls_cilk_spawn
= 1;
15646 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
15648 case CILK_SYNC_STMT
:
15649 RETURN (build_cilk_sync ());
15651 case COMPOUND_EXPR
:
15652 tmp
= RECUR (TREE_OPERAND (t
, 0));
15653 if (tmp
== NULL_TREE
)
15654 /* If the first operand was a statement, we're done with it. */
15655 RETURN (RECUR (TREE_OPERAND (t
, 1)));
15656 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
15657 RECUR (TREE_OPERAND (t
, 1)),
15660 case ANNOTATE_EXPR
:
15661 tmp
= RECUR (TREE_OPERAND (t
, 0));
15662 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
15663 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
15666 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
15668 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15669 /*function_p=*/false,
15670 integral_constant_expression_p
));
15673 RETURN (NULL_TREE
);
15675 input_location
= loc
;
15681 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15682 function. For description of the body see comment above
15683 cp_parser_omp_declare_reduction_exprs. */
15686 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15688 if (t
== NULL_TREE
|| t
== error_mark_node
)
15691 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
15693 tree_stmt_iterator tsi
;
15696 memset (stmts
, 0, sizeof stmts
);
15697 for (i
= 0, tsi
= tsi_start (t
);
15698 i
< 7 && !tsi_end_p (tsi
);
15699 i
++, tsi_next (&tsi
))
15700 stmts
[i
] = tsi_stmt (tsi
);
15701 gcc_assert (tsi_end_p (tsi
));
15705 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
15706 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
15707 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
15708 args
, complain
, in_decl
);
15709 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
15710 args
, complain
, in_decl
);
15711 DECL_CONTEXT (omp_out
) = current_function_decl
;
15712 DECL_CONTEXT (omp_in
) = current_function_decl
;
15713 keep_next_level (true);
15714 tree block
= begin_omp_structured_block ();
15715 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
15716 block
= finish_omp_structured_block (block
);
15717 block
= maybe_cleanup_point_expr_void (block
);
15718 add_decl_expr (omp_out
);
15719 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
15720 TREE_NO_WARNING (omp_out
) = 1;
15721 add_decl_expr (omp_in
);
15722 finish_expr_stmt (block
);
15726 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
15727 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
15728 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
15729 args
, complain
, in_decl
);
15730 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
15731 args
, complain
, in_decl
);
15732 DECL_CONTEXT (omp_priv
) = current_function_decl
;
15733 DECL_CONTEXT (omp_orig
) = current_function_decl
;
15734 keep_next_level (true);
15735 tree block
= begin_omp_structured_block ();
15736 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
15737 block
= finish_omp_structured_block (block
);
15738 block
= maybe_cleanup_point_expr_void (block
);
15739 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
15740 add_decl_expr (omp_priv
);
15741 add_decl_expr (omp_orig
);
15742 finish_expr_stmt (block
);
15744 add_decl_expr (omp_orig
);
15748 /* T is a postfix-expression that is not being used in a function
15749 call. Return the substituted version of T. */
15752 tsubst_non_call_postfix_expression (tree t
, tree args
,
15753 tsubst_flags_t complain
,
15756 if (TREE_CODE (t
) == SCOPE_REF
)
15757 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
15758 /*done=*/false, /*address_p=*/false);
15760 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15761 /*function_p=*/false,
15762 /*integral_constant_expression_p=*/false);
15767 /* Like tsubst but deals with expressions and performs semantic
15768 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15771 tsubst_copy_and_build (tree t
,
15773 tsubst_flags_t complain
,
15776 bool integral_constant_expression_p
)
15778 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15779 #define RECUR(NODE) \
15780 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15781 /*function_p=*/false, \
15782 integral_constant_expression_p)
15787 if (t
== NULL_TREE
|| t
== error_mark_node
)
15790 loc
= input_location
;
15791 if (EXPR_HAS_LOCATION (t
))
15792 input_location
= EXPR_LOCATION (t
);
15794 /* N3276 decltype magic only applies to calls at the top level or on the
15795 right side of a comma. */
15796 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
15797 complain
&= ~tf_decltype
;
15799 switch (TREE_CODE (t
))
15803 /* Fall through. */
15804 case IDENTIFIER_NODE
:
15808 bool non_integral_constant_expression_p
;
15809 const char *error_msg
;
15811 if (IDENTIFIER_TYPENAME_P (t
))
15813 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15814 t
= mangle_conv_op_name_for_type (new_type
);
15817 /* Look up the name. */
15818 decl
= lookup_name (t
);
15820 /* By convention, expressions use ERROR_MARK_NODE to indicate
15821 failure, not NULL_TREE. */
15822 if (decl
== NULL_TREE
)
15823 decl
= error_mark_node
;
15825 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
15827 integral_constant_expression_p
,
15828 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
15829 &non_integral_constant_expression_p
,
15830 /*template_p=*/false,
15832 /*address_p=*/false,
15833 /*template_arg_p=*/false,
15838 if (!function_p
&& identifier_p (decl
))
15840 if (complain
& tf_error
)
15841 unqualified_name_lookup_error (decl
);
15842 decl
= error_mark_node
;
15847 case TEMPLATE_ID_EXPR
:
15850 tree templ
= RECUR (TREE_OPERAND (t
, 0));
15851 tree targs
= TREE_OPERAND (t
, 1);
15854 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15855 if (targs
== error_mark_node
)
15856 return error_mark_node
;
15858 if (variable_template_p (templ
))
15860 templ
= lookup_template_variable (templ
, targs
);
15861 if (!any_dependent_template_arguments_p (targs
))
15863 templ
= finish_template_variable (templ
, complain
);
15866 RETURN (convert_from_reference (templ
));
15869 if (TREE_CODE (templ
) == COMPONENT_REF
)
15871 object
= TREE_OPERAND (templ
, 0);
15872 templ
= TREE_OPERAND (templ
, 1);
15875 object
= NULL_TREE
;
15876 templ
= lookup_template_function (templ
, targs
);
15879 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
15880 object
, templ
, NULL_TREE
));
15882 RETURN (baselink_for_fns (templ
));
15887 tree r
= RECUR (TREE_OPERAND (t
, 0));
15889 if (REFERENCE_REF_P (t
))
15891 /* A type conversion to reference type will be enclosed in
15892 such an indirect ref, but the substitution of the cast
15893 will have also added such an indirect ref. */
15894 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
15895 r
= convert_from_reference (r
);
15898 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
15899 complain
|decltype_flag
);
15905 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15906 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15907 RETURN (build_nop (type
, op0
));
15910 case IMPLICIT_CONV_EXPR
:
15912 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15913 tree expr
= RECUR (TREE_OPERAND (t
, 0));
15914 int flags
= LOOKUP_IMPLICIT
;
15915 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
15916 flags
= LOOKUP_NORMAL
;
15917 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
15923 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15924 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15925 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
15929 case REINTERPRET_CAST_EXPR
:
15930 case CONST_CAST_EXPR
:
15931 case DYNAMIC_CAST_EXPR
:
15932 case STATIC_CAST_EXPR
:
15935 tree op
, r
= NULL_TREE
;
15937 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15938 if (integral_constant_expression_p
15939 && !cast_valid_in_integral_constant_expression_p (type
))
15941 if (complain
& tf_error
)
15942 error ("a cast to a type other than an integral or "
15943 "enumeration type cannot appear in a constant-expression");
15944 RETURN (error_mark_node
);
15947 op
= RECUR (TREE_OPERAND (t
, 0));
15949 warning_sentinel
s(warn_useless_cast
);
15950 switch (TREE_CODE (t
))
15953 r
= build_functional_cast (type
, op
, complain
);
15955 case REINTERPRET_CAST_EXPR
:
15956 r
= build_reinterpret_cast (type
, op
, complain
);
15958 case CONST_CAST_EXPR
:
15959 r
= build_const_cast (type
, op
, complain
);
15961 case DYNAMIC_CAST_EXPR
:
15962 r
= build_dynamic_cast (type
, op
, complain
);
15964 case STATIC_CAST_EXPR
:
15965 r
= build_static_cast (type
, op
, complain
);
15968 gcc_unreachable ();
15974 case POSTDECREMENT_EXPR
:
15975 case POSTINCREMENT_EXPR
:
15976 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15977 args
, complain
, in_decl
);
15978 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
15979 complain
|decltype_flag
));
15981 case PREDECREMENT_EXPR
:
15982 case PREINCREMENT_EXPR
:
15986 case TRUTH_NOT_EXPR
:
15987 case UNARY_PLUS_EXPR
: /* Unary + */
15988 case REALPART_EXPR
:
15989 case IMAGPART_EXPR
:
15990 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
15991 RECUR (TREE_OPERAND (t
, 0)),
15992 complain
|decltype_flag
));
15994 case FIX_TRUNC_EXPR
:
15995 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
15999 op1
= TREE_OPERAND (t
, 0);
16000 if (TREE_CODE (op1
) == LABEL_DECL
)
16001 RETURN (finish_label_address_expr (DECL_NAME (op1
),
16002 EXPR_LOCATION (op1
)));
16003 if (TREE_CODE (op1
) == SCOPE_REF
)
16004 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
16005 /*done=*/true, /*address_p=*/true);
16007 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
16009 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16010 complain
|decltype_flag
));
16015 case TRUNC_DIV_EXPR
:
16016 case CEIL_DIV_EXPR
:
16017 case FLOOR_DIV_EXPR
:
16018 case ROUND_DIV_EXPR
:
16019 case EXACT_DIV_EXPR
:
16023 case TRUNC_MOD_EXPR
:
16024 case FLOOR_MOD_EXPR
:
16025 case TRUTH_ANDIF_EXPR
:
16026 case TRUTH_ORIF_EXPR
:
16027 case TRUTH_AND_EXPR
:
16028 case TRUTH_OR_EXPR
:
16044 warning_sentinel
s1(warn_type_limits
);
16045 warning_sentinel
s2(warn_div_by_zero
);
16046 warning_sentinel
s3(warn_logical_op
);
16047 warning_sentinel
s4(warn_tautological_compare
);
16048 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16049 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16050 tree r
= build_x_binary_op
16051 (input_location
, TREE_CODE (t
),
16053 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16055 : TREE_CODE (TREE_OPERAND (t
, 0))),
16057 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16059 : TREE_CODE (TREE_OPERAND (t
, 1))),
16061 complain
|decltype_flag
);
16062 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16063 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16068 case POINTER_PLUS_EXPR
:
16070 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16071 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16072 return fold_build_pointer_plus (op0
, op1
);
16076 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16077 /*address_p=*/false));
16079 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16080 args
, complain
, in_decl
);
16081 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16082 RECUR (TREE_OPERAND (t
, 1)),
16083 complain
|decltype_flag
));
16085 case ARRAY_NOTATION_REF
:
16087 tree start_index
, length
, stride
;
16088 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16089 args
, complain
, in_decl
);
16090 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16091 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16092 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16093 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16094 length
, stride
, TREE_TYPE (op1
)));
16097 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
16098 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16105 op1
= TREE_OPERAND (t
, 0);
16106 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16107 op1
= TREE_TYPE (op1
);
16110 /* When there are no ARGS, we are trying to evaluate a
16111 non-dependent expression from the parser. Trying to do
16112 the substitutions may not work. */
16114 op1
= TREE_TYPE (op1
);
16118 ++cp_unevaluated_operand
;
16119 ++c_inhibit_evaluation_warnings
;
16121 op1
= tsubst (op1
, args
, complain
, in_decl
);
16123 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16124 /*function_p=*/false,
16125 /*integral_constant_expression_p=*/
16127 --cp_unevaluated_operand
;
16128 --c_inhibit_evaluation_warnings
;
16131 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16132 complain
& tf_error
);
16134 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
16135 complain
& tf_error
);
16136 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
16138 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
16140 if (!processing_template_decl
&& TYPE_P (op1
))
16142 r
= build_min (SIZEOF_EXPR
, size_type_node
,
16143 build1 (NOP_EXPR
, op1
, error_mark_node
));
16144 SIZEOF_EXPR_TYPE_P (r
) = 1;
16147 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
16148 TREE_SIDE_EFFECTS (r
) = 0;
16149 TREE_READONLY (r
) = 1;
16151 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16156 case AT_ENCODE_EXPR
:
16158 op1
= TREE_OPERAND (t
, 0);
16159 ++cp_unevaluated_operand
;
16160 ++c_inhibit_evaluation_warnings
;
16161 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16162 /*function_p=*/false,
16163 /*integral_constant_expression_p=*/false);
16164 --cp_unevaluated_operand
;
16165 --c_inhibit_evaluation_warnings
;
16166 RETURN (objc_build_encode_expr (op1
));
16169 case NOEXCEPT_EXPR
:
16170 op1
= TREE_OPERAND (t
, 0);
16171 ++cp_unevaluated_operand
;
16172 ++c_inhibit_evaluation_warnings
;
16173 ++cp_noexcept_operand
;
16174 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16175 /*function_p=*/false,
16176 /*integral_constant_expression_p=*/false);
16177 --cp_unevaluated_operand
;
16178 --c_inhibit_evaluation_warnings
;
16179 --cp_noexcept_operand
;
16180 RETURN (finish_noexcept_expr (op1
, complain
));
16184 warning_sentinel
s(warn_div_by_zero
);
16185 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
16186 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
16187 tree r
= build_x_modify_expr
16188 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
16189 complain
|decltype_flag
);
16190 /* TREE_NO_WARNING must be set if either the expression was
16191 parenthesized or it uses an operator such as >>= rather
16192 than plain assignment. In the former case, it was already
16193 set and must be copied. In the latter case,
16194 build_x_modify_expr sets it and it must not be reset
16196 if (TREE_NO_WARNING (t
))
16197 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16203 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16204 args
, complain
, in_decl
);
16205 /* Remember that there was a reference to this entity. */
16207 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
16208 RETURN (error_mark_node
);
16209 RETURN (build_x_arrow (input_location
, op1
, complain
));
16213 tree placement
= RECUR (TREE_OPERAND (t
, 0));
16214 tree init
= RECUR (TREE_OPERAND (t
, 3));
16215 vec
<tree
, va_gc
> *placement_vec
;
16216 vec
<tree
, va_gc
> *init_vec
;
16219 if (placement
== NULL_TREE
)
16220 placement_vec
= NULL
;
16223 placement_vec
= make_tree_vector ();
16224 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
16225 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
16228 /* If there was an initializer in the original tree, but it
16229 instantiated to an empty list, then we should pass a
16230 non-NULL empty vector to tell build_new that it was an
16231 empty initializer() rather than no initializer. This can
16232 only happen when the initializer is a pack expansion whose
16233 parameter packs are of length zero. */
16234 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
16238 init_vec
= make_tree_vector ();
16239 if (init
== void_node
)
16240 gcc_assert (init_vec
!= NULL
);
16243 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
16244 vec_safe_push (init_vec
, TREE_VALUE (init
));
16248 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16249 tree op2
= RECUR (TREE_OPERAND (t
, 2));
16250 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
16251 NEW_EXPR_USE_GLOBAL (t
),
16254 if (placement_vec
!= NULL
)
16255 release_tree_vector (placement_vec
);
16256 if (init_vec
!= NULL
)
16257 release_tree_vector (init_vec
);
16264 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16265 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16266 RETURN (delete_sanity (op0
, op1
,
16267 DELETE_EXPR_USE_VEC (t
),
16268 DELETE_EXPR_USE_GLOBAL (t
),
16272 case COMPOUND_EXPR
:
16274 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
16275 complain
& ~tf_decltype
, in_decl
,
16276 /*function_p=*/false,
16277 integral_constant_expression_p
);
16278 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
16280 RECUR (TREE_OPERAND (t
, 1)),
16281 complain
|decltype_flag
));
16287 vec
<tree
, va_gc
> *call_args
;
16288 unsigned int nargs
, i
;
16293 function
= CALL_EXPR_FN (t
);
16294 /* When we parsed the expression, we determined whether or
16295 not Koenig lookup should be performed. */
16296 koenig_p
= KOENIG_LOOKUP_P (t
);
16297 if (TREE_CODE (function
) == SCOPE_REF
)
16299 qualified_p
= true;
16300 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
16302 /*address_p=*/false);
16304 else if (koenig_p
&& identifier_p (function
))
16306 /* Do nothing; calling tsubst_copy_and_build on an identifier
16307 would incorrectly perform unqualified lookup again.
16309 Note that we can also have an IDENTIFIER_NODE if the earlier
16310 unqualified lookup found a member function; in that case
16311 koenig_p will be false and we do want to do the lookup
16312 again to find the instantiated member function.
16314 FIXME but doing that causes c++/15272, so we need to stop
16315 using IDENTIFIER_NODE in that situation. */
16316 qualified_p
= false;
16320 if (TREE_CODE (function
) == COMPONENT_REF
)
16322 tree op
= TREE_OPERAND (function
, 1);
16324 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
16325 || (BASELINK_P (op
)
16326 && BASELINK_QUALIFIED_P (op
)));
16329 qualified_p
= false;
16331 if (TREE_CODE (function
) == ADDR_EXPR
16332 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
16333 /* Avoid error about taking the address of a constructor. */
16334 function
= TREE_OPERAND (function
, 0);
16336 function
= tsubst_copy_and_build (function
, args
, complain
,
16339 integral_constant_expression_p
);
16341 if (BASELINK_P (function
))
16342 qualified_p
= true;
16345 nargs
= call_expr_nargs (t
);
16346 call_args
= make_tree_vector ();
16347 for (i
= 0; i
< nargs
; ++i
)
16349 tree arg
= CALL_EXPR_ARG (t
, i
);
16351 if (!PACK_EXPANSION_P (arg
))
16352 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
16355 /* Expand the pack expansion and push each entry onto
16357 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
16358 if (TREE_CODE (arg
) == TREE_VEC
)
16360 unsigned int len
, j
;
16362 len
= TREE_VEC_LENGTH (arg
);
16363 for (j
= 0; j
< len
; ++j
)
16365 tree value
= TREE_VEC_ELT (arg
, j
);
16366 if (value
!= NULL_TREE
)
16367 value
= convert_from_reference (value
);
16368 vec_safe_push (call_args
, value
);
16373 /* A partial substitution. Add one entry. */
16374 vec_safe_push (call_args
, arg
);
16379 /* We do not perform argument-dependent lookup if normal
16380 lookup finds a non-function, in accordance with the
16381 expected resolution of DR 218. */
16383 && ((is_overloaded_fn (function
)
16384 /* If lookup found a member function, the Koenig lookup is
16385 not appropriate, even if an unqualified-name was used
16386 to denote the function. */
16387 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
16388 || identifier_p (function
))
16389 /* Only do this when substitution turns a dependent call
16390 into a non-dependent call. */
16391 && type_dependent_expression_p_push (t
)
16392 && !any_type_dependent_arguments_p (call_args
))
16393 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
16395 if (identifier_p (function
)
16396 && !any_type_dependent_arguments_p (call_args
))
16398 if (koenig_p
&& (complain
& tf_warning_or_error
))
16400 /* For backwards compatibility and good diagnostics, try
16401 the unqualified lookup again if we aren't in SFINAE
16403 tree unq
= (tsubst_copy_and_build
16404 (function
, args
, complain
, in_decl
, true,
16405 integral_constant_expression_p
));
16406 if (unq
== error_mark_node
)
16407 RETURN (error_mark_node
);
16409 if (unq
!= function
)
16412 if (INDIRECT_REF_P (fn
))
16413 fn
= TREE_OPERAND (fn
, 0);
16414 if (TREE_CODE (fn
) == COMPONENT_REF
)
16415 fn
= TREE_OPERAND (fn
, 1);
16416 if (is_overloaded_fn (fn
))
16417 fn
= get_first_fn (fn
);
16418 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
16419 "%qD was not declared in this scope, "
16420 "and no declarations were found by "
16421 "argument-dependent lookup at the point "
16422 "of instantiation", function
))
16425 /* Can't say anything more. */;
16426 else if (DECL_CLASS_SCOPE_P (fn
))
16428 location_t loc
= EXPR_LOC_OR_LOC (t
,
16431 "declarations in dependent base %qT are "
16432 "not found by unqualified lookup",
16433 DECL_CLASS_CONTEXT (fn
));
16434 if (current_class_ptr
)
16436 "use %<this->%D%> instead", function
);
16439 "use %<%T::%D%> instead",
16440 current_class_name
, function
);
16443 inform (DECL_SOURCE_LOCATION (fn
),
16444 "%qD declared here, later in the "
16445 "translation unit", fn
);
16450 if (identifier_p (function
))
16452 if (complain
& tf_error
)
16453 unqualified_name_lookup_error (function
);
16454 release_tree_vector (call_args
);
16455 RETURN (error_mark_node
);
16459 /* Remember that there was a reference to this entity. */
16460 if (DECL_P (function
)
16461 && !mark_used (function
, complain
) && !(complain
& tf_error
))
16462 RETURN (error_mark_node
);
16464 /* Put back tf_decltype for the actual call. */
16465 complain
|= decltype_flag
;
16467 if (TREE_CODE (function
) == OFFSET_REF
)
16468 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
16470 else if (TREE_CODE (function
) == COMPONENT_REF
)
16472 tree instance
= TREE_OPERAND (function
, 0);
16473 tree fn
= TREE_OPERAND (function
, 1);
16475 if (processing_template_decl
16476 && (type_dependent_expression_p (instance
)
16477 || (!BASELINK_P (fn
)
16478 && TREE_CODE (fn
) != FIELD_DECL
)
16479 || type_dependent_expression_p (fn
)
16480 || any_type_dependent_arguments_p (call_args
)))
16481 ret
= build_nt_call_vec (function
, call_args
);
16482 else if (!BASELINK_P (fn
))
16483 ret
= finish_call_expr (function
, &call_args
,
16484 /*disallow_virtual=*/false,
16485 /*koenig_p=*/false,
16488 ret
= (build_new_method_call
16490 &call_args
, NULL_TREE
,
16491 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
16496 ret
= finish_call_expr (function
, &call_args
,
16497 /*disallow_virtual=*/qualified_p
,
16501 release_tree_vector (call_args
);
16508 tree cond
= RECUR (TREE_OPERAND (t
, 0));
16509 tree folded_cond
= fold_non_dependent_expr (cond
);
16512 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
16514 if (integer_zerop (folded_cond
))
16516 ++c_inhibit_evaluation_warnings
;
16517 exp1
= RECUR (TREE_OPERAND (t
, 1));
16518 --c_inhibit_evaluation_warnings
;
16519 exp2
= RECUR (TREE_OPERAND (t
, 2));
16523 exp1
= RECUR (TREE_OPERAND (t
, 1));
16524 ++c_inhibit_evaluation_warnings
;
16525 exp2
= RECUR (TREE_OPERAND (t
, 2));
16526 --c_inhibit_evaluation_warnings
;
16528 cond
= folded_cond
;
16532 exp1
= RECUR (TREE_OPERAND (t
, 1));
16533 exp2
= RECUR (TREE_OPERAND (t
, 2));
16536 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
16537 cond
, exp1
, exp2
, complain
));
16540 case PSEUDO_DTOR_EXPR
:
16542 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16543 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16544 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16545 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
16551 tree purpose
, value
, chain
;
16553 if (t
== void_list_node
)
16556 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
16557 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
16559 /* We have pack expansions, so expand those and
16560 create a new list out of it. */
16561 tree purposevec
= NULL_TREE
;
16562 tree valuevec
= NULL_TREE
;
16566 /* Expand the argument expressions. */
16567 if (TREE_PURPOSE (t
))
16568 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
16569 complain
, in_decl
);
16570 if (TREE_VALUE (t
))
16571 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
16572 complain
, in_decl
);
16574 /* Build the rest of the list. */
16575 chain
= TREE_CHAIN (t
);
16576 if (chain
&& chain
!= void_type_node
)
16577 chain
= RECUR (chain
);
16579 /* Determine the number of arguments. */
16580 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
16582 len
= TREE_VEC_LENGTH (purposevec
);
16583 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
16585 else if (TREE_CODE (valuevec
) == TREE_VEC
)
16586 len
= TREE_VEC_LENGTH (valuevec
);
16589 /* Since we only performed a partial substitution into
16590 the argument pack, we only RETURN (a single list
16592 if (purposevec
== TREE_PURPOSE (t
)
16593 && valuevec
== TREE_VALUE (t
)
16594 && chain
== TREE_CHAIN (t
))
16597 RETURN (tree_cons (purposevec
, valuevec
, chain
));
16600 /* Convert the argument vectors into a TREE_LIST */
16604 /* Grab the Ith values. */
16606 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
16609 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
16612 /* Build the list (backwards). */
16613 chain
= tree_cons (purpose
, value
, chain
);
16619 purpose
= TREE_PURPOSE (t
);
16621 purpose
= RECUR (purpose
);
16622 value
= TREE_VALUE (t
);
16624 value
= RECUR (value
);
16625 chain
= TREE_CHAIN (t
);
16626 if (chain
&& chain
!= void_type_node
)
16627 chain
= RECUR (chain
);
16628 if (purpose
== TREE_PURPOSE (t
)
16629 && value
== TREE_VALUE (t
)
16630 && chain
== TREE_CHAIN (t
))
16632 RETURN (tree_cons (purpose
, value
, chain
));
16635 case COMPONENT_REF
:
16642 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16643 args
, complain
, in_decl
);
16644 /* Remember that there was a reference to this entity. */
16645 if (DECL_P (object
)
16646 && !mark_used (object
, complain
) && !(complain
& tf_error
))
16647 RETURN (error_mark_node
);
16648 object_type
= TREE_TYPE (object
);
16650 member
= TREE_OPERAND (t
, 1);
16651 if (BASELINK_P (member
))
16652 member
= tsubst_baselink (member
,
16653 non_reference (TREE_TYPE (object
)),
16654 args
, complain
, in_decl
);
16656 member
= tsubst_copy (member
, args
, complain
, in_decl
);
16657 if (member
== error_mark_node
)
16658 RETURN (error_mark_node
);
16660 if (type_dependent_expression_p (object
))
16661 /* We can't do much here. */;
16662 else if (!CLASS_TYPE_P (object_type
))
16664 if (scalarish_type_p (object_type
))
16666 tree s
= NULL_TREE
;
16667 tree dtor
= member
;
16669 if (TREE_CODE (dtor
) == SCOPE_REF
)
16671 s
= TREE_OPERAND (dtor
, 0);
16672 dtor
= TREE_OPERAND (dtor
, 1);
16674 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
16676 dtor
= TREE_OPERAND (dtor
, 0);
16678 RETURN (finish_pseudo_destructor_expr
16679 (object
, s
, dtor
, input_location
));
16683 else if (TREE_CODE (member
) == SCOPE_REF
16684 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
16686 /* Lookup the template functions now that we know what the
16688 tree scope
= TREE_OPERAND (member
, 0);
16689 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
16690 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
16691 member
= lookup_qualified_name (scope
, tmpl
,
16692 /*is_type_p=*/false,
16693 /*complain=*/false);
16694 if (BASELINK_P (member
))
16696 BASELINK_FUNCTIONS (member
)
16697 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
16699 member
= (adjust_result_of_qualified_name_lookup
16700 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
16705 qualified_name_lookup_error (scope
, tmpl
, member
,
16707 RETURN (error_mark_node
);
16710 else if (TREE_CODE (member
) == SCOPE_REF
16711 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
16712 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
16714 if (complain
& tf_error
)
16716 if (TYPE_P (TREE_OPERAND (member
, 0)))
16717 error ("%qT is not a class or namespace",
16718 TREE_OPERAND (member
, 0));
16720 error ("%qD is not a class or namespace",
16721 TREE_OPERAND (member
, 0));
16723 RETURN (error_mark_node
);
16725 else if (TREE_CODE (member
) == FIELD_DECL
)
16727 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
16728 if (TREE_CODE (r
) == COMPONENT_REF
)
16729 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16733 r
= finish_class_member_access_expr (object
, member
,
16734 /*template_p=*/false,
16736 if (TREE_CODE (r
) == COMPONENT_REF
)
16737 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16742 RETURN (build_throw
16743 (RECUR (TREE_OPERAND (t
, 0))));
16747 vec
<constructor_elt
, va_gc
> *n
;
16748 constructor_elt
*ce
;
16749 unsigned HOST_WIDE_INT idx
;
16750 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16751 bool process_index_p
;
16753 bool need_copy_p
= false;
16756 if (type
== error_mark_node
)
16757 RETURN (error_mark_node
);
16759 /* digest_init will do the wrong thing if we let it. */
16760 if (type
&& TYPE_PTRMEMFUNC_P (type
))
16763 /* We do not want to process the index of aggregate
16764 initializers as they are identifier nodes which will be
16765 looked up by digest_init. */
16766 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
16768 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
16769 newlen
= vec_safe_length (n
);
16770 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
16772 if (ce
->index
&& process_index_p
16773 /* An identifier index is looked up in the type
16774 being initialized, not the current scope. */
16775 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
16776 ce
->index
= RECUR (ce
->index
);
16778 if (PACK_EXPANSION_P (ce
->value
))
16780 /* Substitute into the pack expansion. */
16781 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
16784 if (ce
->value
== error_mark_node
16785 || PACK_EXPANSION_P (ce
->value
))
16787 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
16788 /* Just move the argument into place. */
16789 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
16792 /* Update the length of the final CONSTRUCTOR
16793 arguments vector, and note that we will need to
16795 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
16796 need_copy_p
= true;
16800 ce
->value
= RECUR (ce
->value
);
16805 vec
<constructor_elt
, va_gc
> *old_n
= n
;
16807 vec_alloc (n
, newlen
);
16808 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
16810 if (TREE_CODE (ce
->value
) == TREE_VEC
)
16812 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
16813 for (i
= 0; i
< len
; ++i
)
16814 CONSTRUCTOR_APPEND_ELT (n
, 0,
16815 TREE_VEC_ELT (ce
->value
, i
));
16818 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
16822 r
= build_constructor (init_list_type_node
, n
);
16823 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
16825 if (TREE_HAS_CONSTRUCTOR (t
))
16826 RETURN (finish_compound_literal (type
, r
, complain
));
16828 TREE_TYPE (r
) = type
;
16834 tree operand_0
= TREE_OPERAND (t
, 0);
16835 if (TYPE_P (operand_0
))
16837 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
16838 RETURN (get_typeid (operand_0
, complain
));
16842 operand_0
= RECUR (operand_0
);
16843 RETURN (build_typeid (operand_0
, complain
));
16850 else if (DECL_PACK_P (t
))
16852 /* We don't build decls for an instantiation of a
16853 variadic capture proxy, we instantiate the elements
16855 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
16856 return RECUR (DECL_VALUE_EXPR (t
));
16862 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
16863 /* ??? We're doing a subset of finish_id_expression here. */
16865 && !processing_template_decl
16866 && !cp_unevaluated_operand
16867 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
16868 && CP_DECL_THREAD_LOCAL_P (r
))
16870 if (tree wrap
= get_tls_wrapper_fn (r
))
16871 /* Replace an evaluated use of the thread_local variable with
16872 a call to its wrapper. */
16873 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
16875 else if (outer_automatic_var_p (r
))
16877 r
= process_outer_var_ref (r
, complain
);
16878 if (is_capture_proxy (r
))
16879 register_local_specialization (r
, t
);
16882 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
16883 /* If the original type was a reference, we'll be wrapped in
16884 the appropriate INDIRECT_REF. */
16885 r
= convert_from_reference (r
);
16891 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16892 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16893 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
16896 case OFFSETOF_EXPR
:
16897 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
16898 EXPR_LOCATION (t
)));
16902 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
16903 complain
, in_decl
);
16905 tree type2
= TRAIT_EXPR_TYPE2 (t
);
16906 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
16907 type2
= RECUR (type2
);
16909 type2
= tsubst (type2
, args
, complain
, in_decl
);
16911 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
16916 tree old_stmt_expr
= cur_stmt_expr
;
16917 tree stmt_expr
= begin_stmt_expr ();
16919 cur_stmt_expr
= stmt_expr
;
16920 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
16921 integral_constant_expression_p
);
16922 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
16923 cur_stmt_expr
= old_stmt_expr
;
16925 /* If the resulting list of expression statement is empty,
16926 fold it further into void_node. */
16927 if (empty_expr_stmt_p (stmt_expr
))
16928 stmt_expr
= void_node
;
16930 RETURN (stmt_expr
);
16935 tree r
= build_lambda_expr ();
16937 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
16938 LAMBDA_EXPR_CLOSURE (r
) = type
;
16939 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
16941 LAMBDA_EXPR_LOCATION (r
)
16942 = LAMBDA_EXPR_LOCATION (t
);
16943 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16944 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16945 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16946 LAMBDA_EXPR_DISCRIMINATOR (r
)
16947 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
16948 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
16950 /* No substitution needed. */;
16951 else if (VAR_OR_FUNCTION_DECL_P (scope
))
16952 /* For a function or variable scope, we want to use tsubst so that we
16953 don't complain about referring to an auto before deduction. */
16954 scope
= tsubst (scope
, args
, complain
, in_decl
);
16955 else if (TREE_CODE (scope
) == PARM_DECL
)
16957 /* Look up the parameter we want directly, as tsubst_copy
16958 doesn't do what we need. */
16959 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
16960 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
16961 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
16962 parm
= DECL_CHAIN (parm
);
16964 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16965 if (DECL_CONTEXT (scope
) == NULL_TREE
)
16966 DECL_CONTEXT (scope
) = fn
;
16968 else if (TREE_CODE (scope
) == FIELD_DECL
)
16969 /* For a field, use tsubst_copy so that we look up the existing field
16970 rather than build a new one. */
16971 scope
= RECUR (scope
);
16973 gcc_unreachable ();
16974 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
16975 LAMBDA_EXPR_RETURN_TYPE (r
)
16976 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
16978 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16979 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16981 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16982 determine_visibility (TYPE_NAME (type
));
16983 /* Now that we know visibility, instantiate the type so we have a
16984 declaration of the op() for later calls to lambda_function. */
16985 complete_type (type
);
16987 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
16989 insert_pending_capture_proxies ();
16991 RETURN (build_lambda_object (r
));
16995 /* We can get here for a constant initializer of non-dependent type.
16996 FIXME stop folding in cp_parser_initializer_clause. */
16998 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
17003 case TRANSACTION_EXPR
:
17004 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
17005 integral_constant_expression_p
));
17008 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17010 case VEC_PERM_EXPR
:
17012 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17013 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17014 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17015 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17019 case REQUIRES_EXPR
:
17020 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17023 /* Handle Objective-C++ constructs, if appropriate. */
17026 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17027 in_decl
, /*function_p=*/false);
17031 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17037 input_location
= loc
;
17041 /* Verify that the instantiated ARGS are valid. For type arguments,
17042 make sure that the type's linkage is ok. For non-type arguments,
17043 make sure they are constants if they are integral or enumerations.
17044 Emit an error under control of COMPLAIN, and return TRUE on error. */
17047 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
17049 if (dependent_template_arg_p (t
))
17051 if (ARGUMENT_PACK_P (t
))
17053 tree vec
= ARGUMENT_PACK_ARGS (t
);
17054 int len
= TREE_VEC_LENGTH (vec
);
17055 bool result
= false;
17058 for (i
= 0; i
< len
; ++i
)
17059 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
17063 else if (TYPE_P (t
))
17065 /* [basic.link]: A name with no linkage (notably, the name
17066 of a class or enumeration declared in a local scope)
17067 shall not be used to declare an entity with linkage.
17068 This implies that names with no linkage cannot be used as
17071 DR 757 relaxes this restriction for C++0x. */
17072 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
17073 : no_linkage_check (t
, /*relaxed_p=*/false));
17077 /* DR 488 makes use of a type with no linkage cause
17078 type deduction to fail. */
17079 if (complain
& tf_error
)
17081 if (TYPE_ANONYMOUS_P (nt
))
17082 error ("%qT is/uses anonymous type", t
);
17084 error ("template argument for %qD uses local type %qT",
17089 /* In order to avoid all sorts of complications, we do not
17090 allow variably-modified types as template arguments. */
17091 else if (variably_modified_type_p (t
, NULL_TREE
))
17093 if (complain
& tf_error
)
17094 error ("%qT is a variably modified type", t
);
17098 /* Class template and alias template arguments should be OK. */
17099 else if (DECL_TYPE_TEMPLATE_P (t
))
17101 /* A non-type argument of integral or enumerated type must be a
17103 else if (TREE_TYPE (t
)
17104 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
17105 && !REFERENCE_REF_P (t
)
17106 && !TREE_CONSTANT (t
))
17108 if (complain
& tf_error
)
17109 error ("integral expression %qE is not constant", t
);
17116 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
17118 int ix
, len
= DECL_NTPARMS (tmpl
);
17119 bool result
= false;
17121 for (ix
= 0; ix
!= len
; ix
++)
17123 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
17126 if (result
&& (complain
& tf_error
))
17127 error (" trying to instantiate %qD", tmpl
);
17131 /* We're out of SFINAE context now, so generate diagnostics for the access
17132 errors we saw earlier when instantiating D from TMPL and ARGS. */
17135 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
17137 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17138 tree type
= TREE_TYPE (pattern
);
17139 location_t loc
= input_location
;
17141 push_access_scope (d
);
17142 push_deferring_access_checks (dk_no_deferred
);
17143 input_location
= DECL_SOURCE_LOCATION (pattern
);
17144 tsubst (type
, args
, tf_warning_or_error
, d
);
17145 input_location
= loc
;
17146 pop_deferring_access_checks ();
17147 pop_access_scope (d
);
17150 /* Instantiate the indicated variable, function, or alias template TMPL with
17151 the template arguments in TARG_PTR. */
17154 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17156 tree targ_ptr
= orig_args
;
17160 bool access_ok
= true;
17162 if (tmpl
== error_mark_node
)
17163 return error_mark_node
;
17165 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
17167 /* If this function is a clone, handle it specially. */
17168 if (DECL_CLONED_FUNCTION_P (tmpl
))
17173 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17174 DECL_CLONED_FUNCTION. */
17175 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
17176 targ_ptr
, complain
);
17177 if (spec
== error_mark_node
)
17178 return error_mark_node
;
17180 /* Look for the clone. */
17181 FOR_EACH_CLONE (clone
, spec
)
17182 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
17184 /* We should always have found the clone by now. */
17185 gcc_unreachable ();
17189 if (targ_ptr
== error_mark_node
)
17190 return error_mark_node
;
17192 /* Check to see if we already have this specialization. */
17193 gen_tmpl
= most_general_template (tmpl
);
17194 if (tmpl
!= gen_tmpl
)
17195 /* The TMPL is a partial instantiation. To get a full set of
17196 arguments we must add the arguments used to perform the
17197 partial instantiation. */
17198 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
17201 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17202 but it doesn't seem to be on the hot path. */
17203 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
17205 gcc_assert (tmpl
== gen_tmpl
17206 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
17208 || fndecl
== NULL_TREE
);
17210 if (spec
!= NULL_TREE
)
17212 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
17214 if (complain
& tf_error
)
17215 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
17216 return error_mark_node
;
17221 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
17223 return error_mark_node
;
17225 /* We are building a FUNCTION_DECL, during which the access of its
17226 parameters and return types have to be checked. However this
17227 FUNCTION_DECL which is the desired context for access checking
17228 is not built yet. We solve this chicken-and-egg problem by
17229 deferring all checks until we have the FUNCTION_DECL. */
17230 push_deferring_access_checks (dk_deferred
);
17232 /* Instantiation of the function happens in the context of the function
17233 template, not the context of the overload resolution we're doing. */
17234 push_to_top_level ();
17235 /* If there are dependent arguments, e.g. because we're doing partial
17236 ordering, make sure processing_template_decl stays set. */
17237 if (uses_template_parms (targ_ptr
))
17238 ++processing_template_decl
;
17239 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17241 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
17242 complain
, gen_tmpl
, true);
17243 push_nested_class (ctx
);
17246 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17248 if (VAR_P (pattern
))
17250 /* We need to determine if we're using a partial or explicit
17251 specialization now, because the type of the variable could be
17253 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
17254 tree elt
= most_specialized_partial_spec (tid
, complain
);
17255 if (elt
== error_mark_node
)
17256 pattern
= error_mark_node
;
17259 tmpl
= TREE_VALUE (elt
);
17260 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17261 targ_ptr
= TREE_PURPOSE (elt
);
17265 /* Substitute template parameters to obtain the specialization. */
17266 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
17267 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17268 pop_nested_class ();
17269 pop_from_top_level ();
17271 if (fndecl
== error_mark_node
)
17273 pop_deferring_access_checks ();
17274 return error_mark_node
;
17277 /* The DECL_TI_TEMPLATE should always be the immediate parent
17278 template, not the most general template. */
17279 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
17280 DECL_TI_ARGS (fndecl
) = targ_ptr
;
17282 /* Now we know the specialization, compute access previously
17284 push_access_scope (fndecl
);
17285 if (!perform_deferred_access_checks (complain
))
17287 pop_access_scope (fndecl
);
17288 pop_deferring_access_checks ();
17290 /* If we've just instantiated the main entry point for a function,
17291 instantiate all the alternate entry points as well. We do this
17292 by cloning the instantiation of the main entry point, not by
17293 instantiating the template clones. */
17294 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
17295 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
17299 if (!(complain
& tf_error
))
17301 /* Remember to reinstantiate when we're out of SFINAE so the user
17302 can see the errors. */
17303 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
17305 return error_mark_node
;
17310 /* Wrapper for instantiate_template_1. */
17313 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17316 timevar_push (TV_TEMPLATE_INST
);
17317 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
17318 timevar_pop (TV_TEMPLATE_INST
);
17322 /* Instantiate the alias template TMPL with ARGS. Also push a template
17323 instantiation level, which instantiate_template doesn't do because
17324 functions and variables have sufficient context established by the
17328 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
17330 struct pending_template
*old_last_pend
= last_pending_template
;
17331 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17332 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
17333 return error_mark_node
;
17334 tree tinst
= build_tree_list (tmpl
, args
);
17335 if (!push_tinst_level (tinst
))
17338 return error_mark_node
;
17342 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
17343 args
, tmpl
, complain
,
17344 /*require_all_args=*/true,
17345 /*use_default_args=*/true);
17347 tree r
= instantiate_template (tmpl
, args
, complain
);
17348 pop_tinst_level ();
17349 /* We can't free this if a pending_template entry or last_error_tinst_level
17350 is pointing at it. */
17351 if (last_pending_template
== old_last_pend
17352 && last_error_tinst_level
== old_error_tinst
)
17358 /* PARM is a template parameter pack for FN. Returns true iff
17359 PARM is used in a deducible way in the argument list of FN. */
17362 pack_deducible_p (tree parm
, tree fn
)
17364 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
17365 for (; t
; t
= TREE_CHAIN (t
))
17367 tree type
= TREE_VALUE (t
);
17369 if (!PACK_EXPANSION_P (type
))
17371 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
17372 packs
; packs
= TREE_CHAIN (packs
))
17373 if (template_args_equal (TREE_VALUE (packs
), parm
))
17375 /* The template parameter pack is used in a function parameter
17376 pack. If this is the end of the parameter list, the
17377 template parameter pack is deducible. */
17378 if (TREE_CHAIN (t
) == void_list_node
)
17381 /* Otherwise, not. Well, it could be deduced from
17382 a non-pack parameter, but doing so would end up with
17383 a deduction mismatch, so don't bother. */
17387 /* The template parameter pack isn't used in any function parameter
17388 packs, but it might be used deeper, e.g. tuple<Args...>. */
17392 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17393 NARGS elements of the arguments that are being used when calling
17394 it. TARGS is a vector into which the deduced template arguments
17397 Returns either a FUNCTION_DECL for the matching specialization of FN or
17398 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17399 true, diagnostics will be printed to explain why it failed.
17401 If FN is a conversion operator, or we are trying to produce a specific
17402 specialization, RETURN_TYPE is the return type desired.
17404 The EXPLICIT_TARGS are explicit template arguments provided via a
17407 The parameter STRICT is one of:
17410 We are deducing arguments for a function call, as in
17411 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17412 deducing arguments for a call to the result of a conversion
17413 function template, as in [over.call.object].
17416 We are deducing arguments for a conversion function, as in
17417 [temp.deduct.conv].
17420 We are deducing arguments when doing an explicit instantiation
17421 as in [temp.explicit], when determining an explicit specialization
17422 as in [temp.expl.spec], or when taking the address of a function
17423 template, as in [temp.deduct.funcaddr]. */
17426 fn_type_unification (tree fn
,
17427 tree explicit_targs
,
17430 unsigned int nargs
,
17432 unification_kind_t strict
,
17439 tree decl
= NULL_TREE
;
17440 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
17442 static int deduction_depth
;
17443 struct pending_template
*old_last_pend
= last_pending_template
;
17444 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17445 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
17447 tree r
= error_mark_node
;
17450 complain
|= tf_decltype
;
17452 /* In C++0x, it's possible to have a function template whose type depends
17453 on itself recursively. This is most obvious with decltype, but can also
17454 occur with enumeration scope (c++/48969). So we need to catch infinite
17455 recursion and reject the substitution at deduction time; this function
17456 will return error_mark_node for any repeated substitution.
17458 This also catches excessive recursion such as when f<N> depends on
17459 f<N-1> across all integers, and returns error_mark_node for all the
17460 substitutions back up to the initial one.
17462 This is, of course, not reentrant. */
17463 if (excessive_deduction_depth
)
17464 return error_mark_node
;
17465 tinst
= build_tree_list (fn
, NULL_TREE
);
17468 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
17470 fntype
= TREE_TYPE (fn
);
17471 if (explicit_targs
)
17475 The specified template arguments must match the template
17476 parameters in kind (i.e., type, nontype, template), and there
17477 must not be more arguments than there are parameters;
17478 otherwise type deduction fails.
17480 Nontype arguments must match the types of the corresponding
17481 nontype template parameters, or must be convertible to the
17482 types of the corresponding nontype parameters as specified in
17483 _temp.arg.nontype_, otherwise type deduction fails.
17485 All references in the function type of the function template
17486 to the corresponding template parameters are replaced by the
17487 specified template argument values. If a substitution in a
17488 template parameter or in the function type of the function
17489 template results in an invalid type, type deduction fails. */
17490 int i
, len
= TREE_VEC_LENGTH (tparms
);
17491 location_t loc
= input_location
;
17492 bool incomplete
= false;
17494 /* Adjust any explicit template arguments before entering the
17495 substitution context. */
17497 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
17499 /*require_all_args=*/false,
17500 /*use_default_args=*/false));
17501 if (explicit_targs
== error_mark_node
)
17504 /* Substitute the explicit args into the function type. This is
17505 necessary so that, for instance, explicitly declared function
17506 arguments can match null pointed constants. If we were given
17507 an incomplete set of explicit args, we must not do semantic
17508 processing during substitution as we could create partial
17510 for (i
= 0; i
< len
; i
++)
17512 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
17513 bool parameter_pack
= false;
17514 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
17516 /* Dig out the actual parm. */
17517 if (TREE_CODE (parm
) == TYPE_DECL
17518 || TREE_CODE (parm
) == TEMPLATE_DECL
)
17520 parm
= TREE_TYPE (parm
);
17521 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
17523 else if (TREE_CODE (parm
) == PARM_DECL
)
17525 parm
= DECL_INITIAL (parm
);
17526 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
17529 if (!parameter_pack
&& targ
== NULL_TREE
)
17530 /* No explicit argument for this template parameter. */
17533 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
17535 /* Mark the argument pack as "incomplete". We could
17536 still deduce more arguments during unification.
17537 We remove this mark in type_unification_real. */
17540 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
17541 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
17542 = ARGUMENT_PACK_ARGS (targ
);
17545 /* We have some incomplete argument packs. */
17550 TREE_VALUE (tinst
) = explicit_targs
;
17551 if (!push_tinst_level (tinst
))
17553 excessive_deduction_depth
= true;
17556 processing_template_decl
+= incomplete
;
17557 input_location
= DECL_SOURCE_LOCATION (fn
);
17558 /* Ignore any access checks; we'll see them again in
17559 instantiate_template and they might have the wrong
17560 access path at this point. */
17561 push_deferring_access_checks (dk_deferred
);
17562 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
17563 complain
| tf_partial
, NULL_TREE
);
17564 pop_deferring_access_checks ();
17565 input_location
= loc
;
17566 processing_template_decl
-= incomplete
;
17567 pop_tinst_level ();
17569 if (fntype
== error_mark_node
)
17572 /* Place the explicitly specified arguments in TARGS. */
17573 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
17574 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
17577 /* Never do unification on the 'this' parameter. */
17578 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
17580 if (return_type
&& strict
== DEDUCE_CALL
)
17582 /* We're deducing for a call to the result of a template conversion
17583 function. The parms we really want are in return_type. */
17584 if (POINTER_TYPE_P (return_type
))
17585 return_type
= TREE_TYPE (return_type
);
17586 parms
= TYPE_ARG_TYPES (return_type
);
17588 else if (return_type
)
17592 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
17593 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
17594 new_args
[0] = return_type
;
17595 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
17600 /* We allow incomplete unification without an error message here
17601 because the standard doesn't seem to explicitly prohibit it. Our
17602 callers must be ready to deal with unification failures in any
17605 TREE_VALUE (tinst
) = targs
;
17606 /* If we aren't explaining yet, push tinst context so we can see where
17607 any errors (e.g. from class instantiations triggered by instantiation
17608 of default template arguments) come from. If we are explaining, this
17609 context is redundant. */
17610 if (!explain_p
&& !push_tinst_level (tinst
))
17612 excessive_deduction_depth
= true;
17616 /* type_unification_real will pass back any access checks from default
17617 template argument substitution. */
17618 vec
<deferred_access_check
, va_gc
> *checks
;
17621 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17622 targs
, parms
, args
, nargs
, /*subr=*/0,
17623 strict
, flags
, &checks
, explain_p
);
17625 pop_tinst_level ();
17629 /* Now that we have bindings for all of the template arguments,
17630 ensure that the arguments deduced for the template template
17631 parameters have compatible template parameter lists. We cannot
17632 check this property before we have deduced all template
17633 arguments, because the template parameter types of a template
17634 template parameter might depend on prior template parameters
17635 deduced after the template template parameter. The following
17636 ill-formed example illustrates this issue:
17638 template<typename T, template<T> class C> void f(C<5>, T);
17640 template<int N> struct X {};
17643 f(X<5>(), 5l); // error: template argument deduction fails
17646 The template parameter list of 'C' depends on the template type
17647 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17648 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17649 time that we deduce 'C'. */
17650 if (!template_template_parm_bindings_ok_p
17651 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
17653 unify_inconsistent_template_template_parameters (explain_p
);
17657 /* All is well so far. Now, check:
17661 When all template arguments have been deduced, all uses of
17662 template parameters in nondeduced contexts are replaced with
17663 the corresponding deduced argument values. If the
17664 substitution results in an invalid type, as described above,
17665 type deduction fails. */
17666 TREE_VALUE (tinst
) = targs
;
17667 if (!push_tinst_level (tinst
))
17669 excessive_deduction_depth
= true;
17673 /* Also collect access checks from the instantiation. */
17674 reopen_deferring_access_checks (checks
);
17676 decl
= instantiate_template (fn
, targs
, complain
);
17678 checks
= get_deferred_access_checks ();
17679 pop_deferring_access_checks ();
17681 pop_tinst_level ();
17683 if (decl
== error_mark_node
)
17686 /* Now perform any access checks encountered during substitution. */
17687 push_access_scope (decl
);
17688 ok
= perform_access_checks (checks
, complain
);
17689 pop_access_scope (decl
);
17693 /* If we're looking for an exact match, check that what we got
17694 is indeed an exact match. It might not be if some template
17695 parameters are used in non-deduced contexts. But don't check
17696 for an exact match if we have dependent template arguments;
17697 in that case we're doing partial ordering, and we already know
17698 that we have two candidates that will provide the actual type. */
17699 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
17701 tree substed
= TREE_TYPE (decl
);
17705 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
17707 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
17708 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
17709 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
17711 unify_type_mismatch (explain_p
, args
[i
],
17712 TREE_VALUE (sarg
));
17721 if (excessive_deduction_depth
)
17723 if (deduction_depth
== 0)
17724 /* Reset once we're all the way out. */
17725 excessive_deduction_depth
= false;
17728 /* We can't free this if a pending_template entry or last_error_tinst_level
17729 is pointing at it. */
17730 if (last_pending_template
== old_last_pend
17731 && last_error_tinst_level
== old_error_tinst
)
17737 /* TYPE is the type of a function parameter. If TYPE is a (dependent)
17738 ARRAY_TYPE, return the corresponding POINTER_TYPE to which it decays.
17739 Otherwise return TYPE. (We shouldn't see non-dependent ARRAY_TYPE
17740 parameters because they get decayed as soon as they are declared.) */
17743 decay_dependent_array_parm_type (tree type
)
17745 if (TREE_CODE (type
) == ARRAY_TYPE
)
17747 gcc_assert (uses_template_parms (type
));
17748 return type_decays_to (type
);
17754 /* Adjust types before performing type deduction, as described in
17755 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17756 sections are symmetric. PARM is the type of a function parameter
17757 or the return type of the conversion function. ARG is the type of
17758 the argument passed to the call, or the type of the value
17759 initialized with the result of the conversion function.
17760 ARG_EXPR is the original argument expression, which may be null. */
17763 maybe_adjust_types_for_deduction (unification_kind_t strict
,
17776 /* Swap PARM and ARG throughout the remainder of this
17777 function; the handling is precisely symmetric since PARM
17778 will initialize ARG rather than vice versa. */
17779 std::swap (parm
, arg
);
17783 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17784 too, but here handle it by stripping the reference from PARM
17785 rather than by adding it to ARG. */
17786 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17787 && TYPE_REF_IS_RVALUE (*parm
)
17788 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17789 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17790 && TREE_CODE (*arg
) == REFERENCE_TYPE
17791 && !TYPE_REF_IS_RVALUE (*arg
))
17792 *parm
= TREE_TYPE (*parm
);
17793 /* Nothing else to do in this case. */
17797 gcc_unreachable ();
17800 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
17802 /* [temp.deduct.call]
17804 If P is not a reference type:
17806 --If A is an array type, the pointer type produced by the
17807 array-to-pointer standard conversion (_conv.array_) is
17808 used in place of A for type deduction; otherwise,
17810 --If A is a function type, the pointer type produced by
17811 the function-to-pointer standard conversion
17812 (_conv.func_) is used in place of A for type deduction;
17815 --If A is a cv-qualified type, the top level
17816 cv-qualifiers of A's type are ignored for type
17818 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
17819 *arg
= build_pointer_type (TREE_TYPE (*arg
));
17820 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
17821 *arg
= build_pointer_type (*arg
);
17823 *arg
= TYPE_MAIN_VARIANT (*arg
);
17826 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17827 of the form T&&, where T is a template parameter, and the argument
17828 is an lvalue, T is deduced as A& */
17829 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17830 && TYPE_REF_IS_RVALUE (*parm
)
17831 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17832 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17833 && (arg_expr
? real_lvalue_p (arg_expr
)
17834 /* try_one_overload doesn't provide an arg_expr, but
17835 functions are always lvalues. */
17836 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
17837 *arg
= build_reference_type (*arg
);
17839 /* [temp.deduct.call]
17841 If P is a cv-qualified type, the top level cv-qualifiers
17842 of P's type are ignored for type deduction. If P is a
17843 reference type, the type referred to by P is used for
17845 *parm
= TYPE_MAIN_VARIANT (*parm
);
17846 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
17848 *parm
= TREE_TYPE (*parm
);
17849 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17852 /* DR 322. For conversion deduction, remove a reference type on parm
17853 too (which has been swapped into ARG). */
17854 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
17855 *arg
= TREE_TYPE (*arg
);
17860 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17861 template which does contain any deducible template parameters; check if
17862 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17863 unify_one_argument. */
17866 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
17867 int flags
, bool explain_p
)
17872 type
= TREE_TYPE (arg
);
17876 if (same_type_p (parm
, type
))
17877 return unify_success (explain_p
);
17879 if (strict
== DEDUCE_CONV
)
17881 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
17882 explain_p
? tf_warning_or_error
: tf_none
))
17883 return unify_success (explain_p
);
17885 else if (strict
!= DEDUCE_EXACT
)
17887 if (can_convert_arg (parm
, type
,
17888 TYPE_P (arg
) ? NULL_TREE
: arg
,
17889 flags
, explain_p
? tf_warning_or_error
: tf_none
))
17890 return unify_success (explain_p
);
17893 if (strict
== DEDUCE_EXACT
)
17894 return unify_type_mismatch (explain_p
, parm
, arg
);
17896 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
17899 static bool uses_deducible_template_parms (tree type
);
17901 /* Returns true iff the expression EXPR is one from which a template
17902 argument can be deduced. In other words, if it's an undecorated
17903 use of a template non-type parameter. */
17906 deducible_expression (tree expr
)
17908 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
17911 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17912 deducible way; that is, if it has a max value of <PARM> - 1. */
17915 deducible_array_bound (tree domain
)
17917 if (domain
== NULL_TREE
)
17920 tree max
= TYPE_MAX_VALUE (domain
);
17921 if (TREE_CODE (max
) != MINUS_EXPR
)
17924 return deducible_expression (TREE_OPERAND (max
, 0));
17927 /* Returns true iff the template arguments ARGS use a template parameter
17928 in a deducible way. */
17931 deducible_template_args (tree args
)
17933 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
17936 tree elt
= TREE_VEC_ELT (args
, i
);
17937 if (ARGUMENT_PACK_P (elt
))
17938 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
17941 if (PACK_EXPANSION_P (elt
))
17942 elt
= PACK_EXPANSION_PATTERN (elt
);
17943 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
17945 else if (TYPE_P (elt
))
17946 deducible
= uses_deducible_template_parms (elt
);
17948 deducible
= deducible_expression (elt
);
17956 /* Returns true iff TYPE contains any deducible references to template
17957 parameters, as per 14.8.2.5. */
17960 uses_deducible_template_parms (tree type
)
17962 if (PACK_EXPANSION_P (type
))
17963 type
= PACK_EXPANSION_PATTERN (type
);
17970 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17971 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17977 if (POINTER_TYPE_P (type
))
17978 return uses_deducible_template_parms (TREE_TYPE (type
));
17980 /* T[integer-constant ]
17982 if (TREE_CODE (type
) == ARRAY_TYPE
)
17983 return (uses_deducible_template_parms (TREE_TYPE (type
))
17984 || deducible_array_bound (TYPE_DOMAIN (type
)));
17996 if (TYPE_PTRMEM_P (type
))
17997 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
17998 || (uses_deducible_template_parms
17999 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
18001 /* template-name <T> (where template-name refers to a class template)
18002 template-name <i> (where template-name refers to a class template) */
18003 if (CLASS_TYPE_P (type
)
18004 && CLASSTYPE_TEMPLATE_INFO (type
)
18005 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
18006 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18007 (CLASSTYPE_TI_ARGS (type
)));
18012 if (TREE_CODE (type
) == FUNCTION_TYPE
18013 || TREE_CODE (type
) == METHOD_TYPE
)
18015 if (uses_deducible_template_parms (TREE_TYPE (type
)))
18017 tree parm
= TYPE_ARG_TYPES (type
);
18018 if (TREE_CODE (type
) == METHOD_TYPE
)
18019 parm
= TREE_CHAIN (parm
);
18020 for (; parm
; parm
= TREE_CHAIN (parm
))
18021 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
18028 /* Subroutine of type_unification_real and unify_pack_expansion to
18029 handle unification of a single P/A pair. Parameters are as
18030 for those functions. */
18033 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
18034 int subr
, unification_kind_t strict
,
18037 tree arg_expr
= NULL_TREE
;
18040 if (arg
== error_mark_node
|| parm
== error_mark_node
)
18041 return unify_invalid (explain_p
);
18042 if (arg
== unknown_type_node
)
18043 /* We can't deduce anything from this, but we might get all the
18044 template args from other function args. */
18045 return unify_success (explain_p
);
18047 /* Implicit conversions (Clause 4) will be performed on a function
18048 argument to convert it to the type of the corresponding function
18049 parameter if the parameter type contains no template-parameters that
18050 participate in template argument deduction. */
18051 if (strict
!= DEDUCE_EXACT
18052 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
18053 /* For function parameters with no deducible template parameters,
18054 just return. We'll check non-dependent conversions later. */
18055 return unify_success (explain_p
);
18060 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
18061 | UNIFY_ALLOW_MORE_CV_QUAL
18062 | UNIFY_ALLOW_DERIVED
);
18066 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
18070 arg_strict
= UNIFY_ALLOW_NONE
;
18074 gcc_unreachable ();
18077 /* We only do these transformations if this is the top-level
18078 parameter_type_list in a call or declaration matching; in other
18079 situations (nested function declarators, template argument lists) we
18080 won't be comparing a type to an expression, and we don't do any type
18086 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
18087 if (type_unknown_p (arg
))
18089 /* [temp.deduct.type] A template-argument can be
18090 deduced from a pointer to function or pointer
18091 to member function argument if the set of
18092 overloaded functions does not contain function
18093 templates and at most one of a set of
18094 overloaded functions provides a unique
18097 if (resolve_overloaded_unification
18098 (tparms
, targs
, parm
, arg
, strict
,
18099 arg_strict
, explain_p
))
18100 return unify_success (explain_p
);
18101 return unify_overload_resolution_failure (explain_p
, arg
);
18105 arg
= unlowered_expr_type (arg
);
18106 if (arg
== error_mark_node
)
18107 return unify_invalid (explain_p
);
18111 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
18114 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
18115 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
18116 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18118 /* For deduction from an init-list we need the actual list. */
18119 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
18121 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
18124 /* Most parms like fn_type_unification.
18126 If SUBR is 1, we're being called recursively (to unify the
18127 arguments of a function or method parameter of a function
18130 CHECKS is a pointer to a vector of access checks encountered while
18131 substituting default template arguments. */
18134 type_unification_real (tree tparms
,
18138 unsigned int xnargs
,
18140 unification_kind_t strict
,
18142 vec
<deferred_access_check
, va_gc
> **checks
,
18147 int ntparms
= TREE_VEC_LENGTH (tparms
);
18148 int saw_undeduced
= 0;
18151 unsigned int nargs
;
18154 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
18155 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
18156 gcc_assert (ntparms
> 0);
18158 /* Reset the number of non-defaulted template arguments contained
18160 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
18168 while (parms
&& parms
!= void_list_node
18171 parm
= TREE_VALUE (parms
);
18173 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18174 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
18175 /* For a function parameter pack that occurs at the end of the
18176 parameter-declaration-list, the type A of each remaining
18177 argument of the call is compared with the type P of the
18178 declarator-id of the function parameter pack. */
18181 parms
= TREE_CHAIN (parms
);
18183 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18184 /* For a function parameter pack that does not occur at the
18185 end of the parameter-declaration-list, the type of the
18186 parameter pack is a non-deduced context. */
18192 parm
= decay_dependent_array_parm_type (parm
);
18194 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
18200 && parms
!= void_list_node
18201 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
18203 /* Unify the remaining arguments with the pack expansion type. */
18205 tree parmvec
= make_tree_vec (1);
18207 /* Allocate a TREE_VEC and copy in all of the arguments */
18208 argvec
= make_tree_vec (nargs
- ia
);
18209 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
18210 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
18212 /* Copy the parameter into parmvec. */
18213 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
18214 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
18215 /*subr=*/subr
, explain_p
))
18218 /* Advance to the end of the list of parameters. */
18219 parms
= TREE_CHAIN (parms
);
18222 /* Fail if we've reached the end of the parm list, and more args
18223 are present, and the parm list isn't variadic. */
18224 if (ia
< nargs
&& parms
== void_list_node
)
18225 return unify_too_many_arguments (explain_p
, nargs
, ia
);
18226 /* Fail if parms are left and they don't have default values and
18227 they aren't all deduced as empty packs (c++/57397). This is
18228 consistent with sufficient_parms_p. */
18229 if (parms
&& parms
!= void_list_node
18230 && TREE_PURPOSE (parms
) == NULL_TREE
)
18232 unsigned int count
= nargs
;
18237 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
18240 p
= TREE_CHAIN (p
);
18242 while (p
&& p
!= void_list_node
);
18243 if (count
!= nargs
)
18244 return unify_too_few_arguments (explain_p
, ia
, count
,
18250 tsubst_flags_t complain
= (explain_p
18251 ? tf_warning_or_error
18254 for (i
= 0; i
< ntparms
; i
++)
18256 tree targ
= TREE_VEC_ELT (targs
, i
);
18257 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18259 /* Clear the "incomplete" flags on all argument packs now so that
18260 substituting them into later default arguments works. */
18261 if (targ
&& ARGUMENT_PACK_P (targ
))
18263 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
18264 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
18267 if (targ
|| tparm
== error_mark_node
)
18269 tparm
= TREE_VALUE (tparm
);
18271 /* If this is an undeduced nontype parameter that depends on
18272 a type parameter, try another pass; its type may have been
18273 deduced from a later argument than the one from which
18274 this parameter can be deduced. */
18275 if (TREE_CODE (tparm
) == PARM_DECL
18276 && uses_template_parms (TREE_TYPE (tparm
))
18277 && saw_undeduced
< 2)
18283 /* Core issue #226 (C++0x) [temp.deduct]:
18285 If a template argument has not been deduced, its
18286 default template argument, if any, is used.
18288 When we are in C++98 mode, TREE_PURPOSE will either
18289 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18290 to explicitly check cxx_dialect here. */
18291 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
18292 /* OK, there is a default argument. Wait until after the
18293 conversion check to do substitution. */
18296 /* If the type parameter is a parameter pack, then it will
18297 be deduced to an empty parameter pack. */
18298 if (template_parameter_pack_p (tparm
))
18302 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
18304 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
18305 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
18306 TREE_CONSTANT (arg
) = 1;
18309 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
18311 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
18313 TREE_VEC_ELT (targs
, i
) = arg
;
18317 return unify_parameter_deduction_failure (explain_p
, tparm
);
18320 /* DR 1391: All parameters have args, now check non-dependent parms for
18322 if (saw_undeduced
< 2)
18323 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
18324 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
18326 parm
= TREE_VALUE (parms
);
18328 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18329 && (!TREE_CHAIN (parms
)
18330 || TREE_CHAIN (parms
) == void_list_node
))
18331 /* For a function parameter pack that occurs at the end of the
18332 parameter-declaration-list, the type A of each remaining
18333 argument of the call is compared with the type P of the
18334 declarator-id of the function parameter pack. */
18337 parms
= TREE_CHAIN (parms
);
18339 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18340 /* For a function parameter pack that does not occur at the
18341 end of the parameter-declaration-list, the type of the
18342 parameter pack is a non-deduced context. */
18348 if (uses_template_parms (parm
))
18350 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
18355 /* Now substitute into the default template arguments. */
18356 for (i
= 0; i
< ntparms
; i
++)
18358 tree targ
= TREE_VEC_ELT (targs
, i
);
18359 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18361 if (targ
|| tparm
== error_mark_node
)
18363 tree parm
= TREE_VALUE (tparm
);
18365 if (TREE_CODE (parm
) == PARM_DECL
18366 && uses_template_parms (TREE_TYPE (parm
))
18367 && saw_undeduced
< 2)
18370 tree arg
= TREE_PURPOSE (tparm
);
18371 reopen_deferring_access_checks (*checks
);
18372 location_t save_loc
= input_location
;
18374 input_location
= DECL_SOURCE_LOCATION (parm
);
18375 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
18376 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
18378 input_location
= save_loc
;
18379 *checks
= get_deferred_access_checks ();
18380 pop_deferring_access_checks ();
18381 if (arg
== error_mark_node
)
18385 TREE_VEC_ELT (targs
, i
) = arg
;
18386 /* The position of the first default template argument,
18387 is also the number of non-defaulted arguments in TARGS.
18389 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18390 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
18395 if (saw_undeduced
++ == 1)
18399 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18400 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
18402 return unify_success (explain_p
);
18405 /* Subroutine of type_unification_real. Args are like the variables
18406 at the call site. ARG is an overloaded function (or template-id);
18407 we try deducing template args from each of the overloads, and if
18408 only one succeeds, we go with that. Modifies TARGS and returns
18409 true on success. */
18412 resolve_overloaded_unification (tree tparms
,
18416 unification_kind_t strict
,
18420 tree tempargs
= copy_node (targs
);
18422 tree goodfn
= NULL_TREE
;
18425 if (TREE_CODE (arg
) == ADDR_EXPR
)
18427 arg
= TREE_OPERAND (arg
, 0);
18433 if (TREE_CODE (arg
) == COMPONENT_REF
)
18434 /* Handle `&x' where `x' is some static or non-static member
18436 arg
= TREE_OPERAND (arg
, 1);
18438 if (TREE_CODE (arg
) == OFFSET_REF
)
18439 arg
= TREE_OPERAND (arg
, 1);
18441 /* Strip baselink information. */
18442 if (BASELINK_P (arg
))
18443 arg
= BASELINK_FUNCTIONS (arg
);
18445 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
18447 /* If we got some explicit template args, we need to plug them into
18448 the affected templates before we try to unify, in case the
18449 explicit args will completely resolve the templates in question. */
18452 tree expl_subargs
= TREE_OPERAND (arg
, 1);
18453 arg
= TREE_OPERAND (arg
, 0);
18455 for (; arg
; arg
= OVL_NEXT (arg
))
18457 tree fn
= OVL_CURRENT (arg
);
18458 tree subargs
, elem
;
18460 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18463 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18464 expl_subargs
, NULL_TREE
, tf_none
,
18465 /*require_all_args=*/true,
18466 /*use_default_args=*/true);
18467 if (subargs
!= error_mark_node
18468 && !any_dependent_template_arguments_p (subargs
))
18470 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
18471 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18472 elem
, strict
, sub_strict
, addr_p
, explain_p
)
18473 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
18482 /* If no templates (or more than one) are fully resolved by the
18483 explicit arguments, this template-id is a non-deduced context; it
18484 could still be OK if we deduce all template arguments for the
18485 enclosing call through other arguments. */
18489 else if (TREE_CODE (arg
) != OVERLOAD
18490 && TREE_CODE (arg
) != FUNCTION_DECL
)
18491 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18492 -- but the deduction does not succeed because the expression is
18493 not just the function on its own. */
18496 for (; arg
; arg
= OVL_NEXT (arg
))
18497 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18498 TREE_TYPE (OVL_CURRENT (arg
)),
18499 strict
, sub_strict
, addr_p
, explain_p
)
18500 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
18502 goodfn
= OVL_CURRENT (arg
);
18506 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18507 to function or pointer to member function argument if the set of
18508 overloaded functions does not contain function templates and at most
18509 one of a set of overloaded functions provides a unique match.
18511 So if we found multiple possibilities, we return success but don't
18512 deduce anything. */
18516 int i
= TREE_VEC_LENGTH (targs
);
18518 if (TREE_VEC_ELT (tempargs
, i
))
18520 tree old
= TREE_VEC_ELT (targs
, i
);
18521 tree new_
= TREE_VEC_ELT (tempargs
, i
);
18522 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
18523 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
18524 /* Don't forget explicit template arguments in a pack. */
18525 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
18526 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
18527 TREE_VEC_ELT (targs
, i
) = new_
;
18536 /* Core DR 115: In contexts where deduction is done and fails, or in
18537 contexts where deduction is not done, if a template argument list is
18538 specified and it, along with any default template arguments, identifies
18539 a single function template specialization, then the template-id is an
18540 lvalue for the function template specialization. */
18543 resolve_nondeduced_context (tree orig_expr
)
18545 tree expr
, offset
, baselink
;
18548 if (!type_unknown_p (orig_expr
))
18553 offset
= NULL_TREE
;
18554 baselink
= NULL_TREE
;
18556 if (TREE_CODE (expr
) == ADDR_EXPR
)
18558 expr
= TREE_OPERAND (expr
, 0);
18561 if (TREE_CODE (expr
) == OFFSET_REF
)
18564 expr
= TREE_OPERAND (expr
, 1);
18566 if (BASELINK_P (expr
))
18569 expr
= BASELINK_FUNCTIONS (expr
);
18572 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
18575 tree goodfn
= NULL_TREE
;
18577 /* If we got some explicit template args, we need to plug them into
18578 the affected templates before we try to unify, in case the
18579 explicit args will completely resolve the templates in question. */
18581 tree expl_subargs
= TREE_OPERAND (expr
, 1);
18582 tree arg
= TREE_OPERAND (expr
, 0);
18583 tree badfn
= NULL_TREE
;
18584 tree badargs
= NULL_TREE
;
18586 for (; arg
; arg
= OVL_NEXT (arg
))
18588 tree fn
= OVL_CURRENT (arg
);
18589 tree subargs
, elem
;
18591 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18594 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18595 expl_subargs
, NULL_TREE
, tf_none
,
18596 /*require_all_args=*/true,
18597 /*use_default_args=*/true);
18598 if (subargs
!= error_mark_node
18599 && !any_dependent_template_arguments_p (subargs
))
18601 elem
= instantiate_template (fn
, subargs
, tf_none
);
18602 if (elem
== error_mark_node
)
18607 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
18616 mark_used (goodfn
);
18619 expr
= build_baselink (BASELINK_BINFO (baselink
),
18620 BASELINK_ACCESS_BINFO (baselink
),
18621 expr
, BASELINK_OPTYPE (baselink
));
18625 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
18626 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
18629 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
18632 else if (good
== 0 && badargs
)
18633 /* There were no good options and at least one bad one, so let the
18634 user know what the problem is. */
18635 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
18640 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18641 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18642 different overloads deduce different arguments for a given parm.
18643 ADDR_P is true if the expression for which deduction is being
18644 performed was of the form "& fn" rather than simply "fn".
18646 Returns 1 on success. */
18649 try_one_overload (tree tparms
,
18654 unification_kind_t strict
,
18663 if (arg
== error_mark_node
)
18666 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18667 to function or pointer to member function argument if the set of
18668 overloaded functions does not contain function templates and at most
18669 one of a set of overloaded functions provides a unique match.
18671 So if this is a template, just return success. */
18673 if (uses_template_parms (arg
))
18676 if (TREE_CODE (arg
) == METHOD_TYPE
)
18677 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
18679 arg
= build_pointer_type (arg
);
18681 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
18683 /* We don't copy orig_targs for this because if we have already deduced
18684 some template args from previous args, unify would complain when we
18685 try to deduce a template parameter for the same argument, even though
18686 there isn't really a conflict. */
18687 nargs
= TREE_VEC_LENGTH (targs
);
18688 tempargs
= make_tree_vec (nargs
);
18690 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
18693 /* First make sure we didn't deduce anything that conflicts with
18694 explicitly specified args. */
18695 for (i
= nargs
; i
--; )
18697 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18698 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
18702 else if (uses_template_parms (elt
))
18703 /* Since we're unifying against ourselves, we will fill in
18704 template args used in the function parm list with our own
18705 template parms. Discard them. */
18706 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
18707 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
18711 for (i
= nargs
; i
--; )
18713 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18716 TREE_VEC_ELT (targs
, i
) = elt
;
18722 /* PARM is a template class (perhaps with unbound template
18723 parameters). ARG is a fully instantiated type. If ARG can be
18724 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18725 TARGS are as for unify. */
18728 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
18731 tree copy_of_targs
;
18733 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
18734 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
18735 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
18738 /* We need to make a new template argument vector for the call to
18739 unify. If we used TARGS, we'd clutter it up with the result of
18740 the attempted unification, even if this class didn't work out.
18741 We also don't want to commit ourselves to all the unifications
18742 we've already done, since unification is supposed to be done on
18743 an argument-by-argument basis. In other words, consider the
18744 following pathological case:
18746 template <int I, int J, int K>
18749 template <int I, int J>
18750 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18752 template <int I, int J, int K>
18753 void f(S<I, J, K>, S<I, I, I>);
18762 Now, by the time we consider the unification involving `s2', we
18763 already know that we must have `f<0, 0, 0>'. But, even though
18764 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18765 because there are two ways to unify base classes of S<0, 1, 2>
18766 with S<I, I, I>. If we kept the already deduced knowledge, we
18767 would reject the possibility I=1. */
18768 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18770 /* If unification failed, we're done. */
18771 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
18772 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
18778 /* Given a template type PARM and a class type ARG, find the unique
18779 base type in ARG that is an instance of PARM. We do not examine
18780 ARG itself; only its base-classes. If there is not exactly one
18781 appropriate base class, return NULL_TREE. PARM may be the type of
18782 a partial specialization, as well as a plain template type. Used
18785 static enum template_base_result
18786 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
18787 bool explain_p
, tree
*result
)
18789 tree rval
= NULL_TREE
;
18792 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
18794 binfo
= TYPE_BINFO (complete_type (arg
));
18797 /* The type could not be completed. */
18798 *result
= NULL_TREE
;
18799 return tbr_incomplete_type
;
18802 /* Walk in inheritance graph order. The search order is not
18803 important, and this avoids multiple walks of virtual bases. */
18804 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
18806 tree r
= try_class_unification (tparms
, targs
, parm
,
18807 BINFO_TYPE (binfo
), explain_p
);
18811 /* If there is more than one satisfactory baseclass, then:
18815 If they yield more than one possible deduced A, the type
18819 if (rval
&& !same_type_p (r
, rval
))
18821 *result
= NULL_TREE
;
18822 return tbr_ambiguous_baseclass
;
18830 return tbr_success
;
18833 /* Returns the level of DECL, which declares a template parameter. */
18836 template_decl_level (tree decl
)
18838 switch (TREE_CODE (decl
))
18841 case TEMPLATE_DECL
:
18842 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
18845 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
18848 gcc_unreachable ();
18853 /* Decide whether ARG can be unified with PARM, considering only the
18854 cv-qualifiers of each type, given STRICT as documented for unify.
18855 Returns nonzero iff the unification is OK on that basis. */
18858 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
18860 int arg_quals
= cp_type_quals (arg
);
18861 int parm_quals
= cp_type_quals (parm
);
18863 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18864 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18866 /* Although a CVR qualifier is ignored when being applied to a
18867 substituted template parameter ([8.3.2]/1 for example), that
18868 does not allow us to unify "const T" with "int&" because both
18869 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18870 It is ok when we're allowing additional CV qualifiers
18871 at the outer level [14.8.2.1]/3,1st bullet. */
18872 if ((TREE_CODE (arg
) == REFERENCE_TYPE
18873 || TREE_CODE (arg
) == FUNCTION_TYPE
18874 || TREE_CODE (arg
) == METHOD_TYPE
)
18875 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
18878 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
18879 && (parm_quals
& TYPE_QUAL_RESTRICT
))
18883 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18884 && (arg_quals
& parm_quals
) != parm_quals
)
18887 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
18888 && (parm_quals
& arg_quals
) != arg_quals
)
18894 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18896 template_parm_level_and_index (tree parm
, int* level
, int* index
)
18898 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18899 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18900 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18902 *index
= TEMPLATE_TYPE_IDX (parm
);
18903 *level
= TEMPLATE_TYPE_LEVEL (parm
);
18907 *index
= TEMPLATE_PARM_IDX (parm
);
18908 *level
= TEMPLATE_PARM_LEVEL (parm
);
18912 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18914 if (unify (TP, TA, P, A, S, EP)) \
18918 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18919 expansion at the end of PACKED_PARMS. Returns 0 if the type
18920 deduction succeeds, 1 otherwise. STRICT is the same as in
18921 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18922 call argument list. We'll need to adjust the arguments to make them
18923 types. SUBR tells us if this is from a recursive call to
18924 type_unification_real, or for comparing two template argument
18928 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
18929 tree packed_args
, unification_kind_t strict
,
18930 bool subr
, bool explain_p
)
18933 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
18934 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
18935 tree pack
, packs
= NULL_TREE
;
18936 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
18938 packed_args
= expand_template_argument_pack (packed_args
);
18940 int len
= TREE_VEC_LENGTH (packed_args
);
18942 /* Determine the parameter packs we will be deducing from the
18943 pattern, and record their current deductions. */
18944 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
18945 pack
; pack
= TREE_CHAIN (pack
))
18947 tree parm_pack
= TREE_VALUE (pack
);
18950 /* Determine the index and level of this parameter pack. */
18951 template_parm_level_and_index (parm_pack
, &level
, &idx
);
18953 /* Keep track of the parameter packs and their corresponding
18955 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
18956 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
18959 /* Loop through all of the arguments that have not yet been
18960 unified and unify each with the pattern. */
18961 for (i
= start
; i
< len
; i
++)
18964 bool any_explicit
= false;
18965 tree arg
= TREE_VEC_ELT (packed_args
, i
);
18967 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18968 or the element of its argument pack at the current index if
18969 this argument was explicitly specified. */
18970 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18974 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18977 if (TREE_VALUE (pack
)
18978 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
18979 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
18981 any_explicit
= true;
18982 arg
= TREE_VEC_ELT (pargs
, i
- start
);
18984 TMPL_ARG (targs
, level
, idx
) = arg
;
18987 /* If we had explicit template arguments, substitute them into the
18988 pattern before deduction. */
18991 /* Some arguments might still be unspecified or dependent. */
18993 ++processing_template_decl
;
18994 dependent
= any_dependent_template_arguments_p (targs
);
18996 --processing_template_decl
;
18997 parm
= tsubst (pattern
, targs
,
18998 explain_p
? tf_warning_or_error
: tf_none
,
19001 --processing_template_decl
;
19002 if (parm
== error_mark_node
)
19008 /* Unify the pattern with the current argument. */
19009 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
19013 /* For each parameter pack, collect the deduced value. */
19014 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19017 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19019 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
19020 TMPL_ARG (targs
, level
, idx
);
19024 /* Verify that the results of unification with the parameter packs
19025 produce results consistent with what we've seen before, and make
19026 the deduced argument packs available. */
19027 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19029 tree old_pack
= TREE_VALUE (pack
);
19030 tree new_args
= TREE_TYPE (pack
);
19031 int i
, len
= TREE_VEC_LENGTH (new_args
);
19033 bool nondeduced_p
= false;
19035 /* By default keep the original deduced argument pack.
19036 If necessary, more specific code is going to update the
19037 resulting deduced argument later down in this function. */
19038 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19039 TMPL_ARG (targs
, level
, idx
) = old_pack
;
19041 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19042 actually deduce anything. */
19043 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
19044 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
19045 nondeduced_p
= true;
19049 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
19051 /* If we had fewer function args than explicit template args,
19052 just use the explicits. */
19053 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19054 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
19055 if (len
< explicit_len
)
19056 new_args
= explicit_args
;
19062 /* Build the deduced *_ARGUMENT_PACK. */
19063 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
19065 result
= make_node (NONTYPE_ARGUMENT_PACK
);
19066 TREE_TYPE (result
) =
19067 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
19068 TREE_CONSTANT (result
) = 1;
19071 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19073 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
19075 /* Note the deduced argument packs for this parameter
19077 TMPL_ARG (targs
, level
, idx
) = result
;
19079 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
19080 && (ARGUMENT_PACK_ARGS (old_pack
)
19081 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
19083 /* We only had the explicitly-provided arguments before, but
19084 now we have a complete set of arguments. */
19085 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19087 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
19088 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
19089 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
19093 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
19094 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
19096 if (!comp_template_args (old_args
, new_args
,
19097 &bad_old_arg
, &bad_new_arg
))
19098 /* Inconsistent unification of this parameter pack. */
19099 return unify_parameter_pack_inconsistent (explain_p
,
19105 return unify_success (explain_p
);
19108 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19109 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19110 parameters and return value are as for unify. */
19113 unify_array_domain (tree tparms
, tree targs
,
19114 tree parm_dom
, tree arg_dom
,
19122 /* Our representation of array types uses "N - 1" as the
19123 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19124 not an integer constant. We cannot unify arbitrarily
19125 complex expressions, so we eliminate the MINUS_EXPRs
19127 parm_max
= TYPE_MAX_VALUE (parm_dom
);
19128 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
19131 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
19132 parm_max
= TREE_OPERAND (parm_max
, 0);
19134 arg_max
= TYPE_MAX_VALUE (arg_dom
);
19135 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
19138 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19139 trying to unify the type of a variable with the type
19140 of a template parameter. For example:
19142 template <unsigned int N>
19143 void f (char (&) [N]);
19150 Here, the type of the ARG will be "int [g(i)]", and
19151 may be a SAVE_EXPR, etc. */
19152 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
19153 return unify_vla_arg (explain_p
, arg_dom
);
19154 arg_max
= TREE_OPERAND (arg_max
, 0);
19157 /* If only one of the bounds used a MINUS_EXPR, compensate
19158 by adding one to the other bound. */
19159 if (parm_cst
&& !arg_cst
)
19160 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19164 else if (arg_cst
&& !parm_cst
)
19165 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19170 return unify (tparms
, targs
, parm_max
, arg_max
,
19171 UNIFY_ALLOW_INTEGER
, explain_p
);
19174 /* Deduce the value of template parameters. TPARMS is the (innermost)
19175 set of template parameters to a template. TARGS is the bindings
19176 for those template parameters, as determined thus far; TARGS may
19177 include template arguments for outer levels of template parameters
19178 as well. PARM is a parameter to a template function, or a
19179 subcomponent of that parameter; ARG is the corresponding argument.
19180 This function attempts to match PARM with ARG in a manner
19181 consistent with the existing assignments in TARGS. If more values
19182 are deduced, then TARGS is updated.
19184 Returns 0 if the type deduction succeeds, 1 otherwise. The
19185 parameter STRICT is a bitwise or of the following flags:
19188 Require an exact match between PARM and ARG.
19189 UNIFY_ALLOW_MORE_CV_QUAL:
19190 Allow the deduced ARG to be more cv-qualified (by qualification
19191 conversion) than ARG.
19192 UNIFY_ALLOW_LESS_CV_QUAL:
19193 Allow the deduced ARG to be less cv-qualified than ARG.
19194 UNIFY_ALLOW_DERIVED:
19195 Allow the deduced ARG to be a template base class of ARG,
19196 or a pointer to a template base class of the type pointed to by
19198 UNIFY_ALLOW_INTEGER:
19199 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19200 case for more information.
19201 UNIFY_ALLOW_OUTER_LEVEL:
19202 This is the outermost level of a deduction. Used to determine validity
19203 of qualification conversions. A valid qualification conversion must
19204 have const qualified pointers leading up to the inner type which
19205 requires additional CV quals, except at the outer level, where const
19206 is not required [conv.qual]. It would be normal to set this flag in
19207 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19208 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19209 This is the outermost level of a deduction, and PARM can be more CV
19210 qualified at this point.
19211 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19212 This is the outermost level of a deduction, and PARM can be less CV
19213 qualified at this point. */
19216 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
19222 int strict_in
= strict
;
19224 /* I don't think this will do the right thing with respect to types.
19225 But the only case I've seen it in so far has been array bounds, where
19226 signedness is the only information lost, and I think that will be
19228 while (TREE_CODE (parm
) == NOP_EXPR
)
19229 parm
= TREE_OPERAND (parm
, 0);
19231 if (arg
== error_mark_node
)
19232 return unify_invalid (explain_p
);
19233 if (arg
== unknown_type_node
19234 || arg
== init_list_type_node
)
19235 /* We can't deduce anything from this, but we might get all the
19236 template args from other function args. */
19237 return unify_success (explain_p
);
19239 /* If PARM uses template parameters, then we can't bail out here,
19240 even if ARG == PARM, since we won't record unifications for the
19241 template parameters. We might need them if we're trying to
19242 figure out which of two things is more specialized. */
19243 if (arg
== parm
&& !uses_template_parms (parm
))
19244 return unify_success (explain_p
);
19246 /* Handle init lists early, so the rest of the function can assume
19247 we're dealing with a type. */
19248 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
19252 tree orig_parm
= parm
;
19254 /* Replace T with std::initializer_list<T> for deduction. */
19255 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19256 && flag_deduce_init_list
)
19257 parm
= listify (parm
);
19259 if (!is_std_init_list (parm
)
19260 && TREE_CODE (parm
) != ARRAY_TYPE
)
19261 /* We can only deduce from an initializer list argument if the
19262 parameter is std::initializer_list or an array; otherwise this
19263 is a non-deduced context. */
19264 return unify_success (explain_p
);
19266 if (TREE_CODE (parm
) == ARRAY_TYPE
)
19267 elttype
= TREE_TYPE (parm
);
19270 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
19271 /* Deduction is defined in terms of a single type, so just punt
19272 on the (bizarre) std::initializer_list<T...>. */
19273 if (PACK_EXPANSION_P (elttype
))
19274 return unify_success (explain_p
);
19277 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
19279 int elt_strict
= strict
;
19281 if (elt
== error_mark_node
)
19282 return unify_invalid (explain_p
);
19284 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
19286 tree type
= TREE_TYPE (elt
);
19287 if (type
== error_mark_node
)
19288 return unify_invalid (explain_p
);
19289 /* It should only be possible to get here for a call. */
19290 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
19291 elt_strict
|= maybe_adjust_types_for_deduction
19292 (DEDUCE_CALL
, &elttype
, &type
, elt
);
19296 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
19300 if (TREE_CODE (parm
) == ARRAY_TYPE
19301 && deducible_array_bound (TYPE_DOMAIN (parm
)))
19303 /* Also deduce from the length of the initializer list. */
19304 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
19305 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
19306 if (idx
== error_mark_node
)
19307 return unify_invalid (explain_p
);
19308 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19312 /* If the std::initializer_list<T> deduction worked, replace the
19313 deduced A with std::initializer_list<A>. */
19314 if (orig_parm
!= parm
)
19316 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
19317 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19318 targ
= listify (targ
);
19319 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
19321 return unify_success (explain_p
);
19324 /* Immediately reject some pairs that won't unify because of
19325 cv-qualification mismatches. */
19326 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19328 /* It is the elements of the array which hold the cv quals of an array
19329 type, and the elements might be template type parms. We'll check
19330 when we recurse. */
19331 && TREE_CODE (arg
) != ARRAY_TYPE
19332 /* We check the cv-qualifiers when unifying with template type
19333 parameters below. We want to allow ARG `const T' to unify with
19334 PARM `T' for example, when computing which of two templates
19335 is more specialized, for example. */
19336 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
19337 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
19338 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19340 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
19341 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
19342 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
19343 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
19344 strict
&= ~UNIFY_ALLOW_DERIVED
;
19345 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19346 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
19348 switch (TREE_CODE (parm
))
19350 case TYPENAME_TYPE
:
19352 case UNBOUND_CLASS_TEMPLATE
:
19353 /* In a type which contains a nested-name-specifier, template
19354 argument values cannot be deduced for template parameters used
19355 within the nested-name-specifier. */
19356 return unify_success (explain_p
);
19358 case TEMPLATE_TYPE_PARM
:
19359 case TEMPLATE_TEMPLATE_PARM
:
19360 case BOUND_TEMPLATE_TEMPLATE_PARM
:
19361 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19362 if (error_operand_p (tparm
))
19363 return unify_invalid (explain_p
);
19365 if (TEMPLATE_TYPE_LEVEL (parm
)
19366 != template_decl_level (tparm
))
19367 /* The PARM is not one we're trying to unify. Just check
19368 to see if it matches ARG. */
19370 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19371 && (is_auto (parm
) ? is_auto (arg
)
19372 : same_type_p (parm
, arg
)))
19373 return unify_success (explain_p
);
19375 return unify_type_mismatch (explain_p
, parm
, arg
);
19377 idx
= TEMPLATE_TYPE_IDX (parm
);
19378 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19379 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
19380 if (error_operand_p (tparm
))
19381 return unify_invalid (explain_p
);
19383 /* Check for mixed types and values. */
19384 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19385 && TREE_CODE (tparm
) != TYPE_DECL
)
19386 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19387 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
19388 gcc_unreachable ();
19390 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19392 /* ARG must be constructed from a template class or a template
19393 template parameter. */
19394 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
19395 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
19396 return unify_template_deduction_failure (explain_p
, parm
, arg
);
19398 tree parmvec
= TYPE_TI_ARGS (parm
);
19399 /* An alias template name is never deduced. */
19400 if (TYPE_ALIAS_P (arg
))
19401 arg
= strip_typedefs (arg
);
19402 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
19403 tree full_argvec
= add_to_template_args (targs
, argvec
);
19405 = DECL_INNERMOST_TEMPLATE_PARMS
19406 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
19408 int parm_variadic_p
= 0;
19410 /* The resolution to DR150 makes clear that default
19411 arguments for an N-argument may not be used to bind T
19412 to a template template parameter with fewer than N
19413 parameters. It is not safe to permit the binding of
19414 default arguments as an extension, as that may change
19415 the meaning of a conforming program. Consider:
19417 struct Dense { static const unsigned int dim = 1; };
19419 template <template <typename> class View,
19421 void operator+(float, View<Block> const&);
19423 template <typename Block,
19424 unsigned int Dim = Block::dim>
19425 struct Lvalue_proxy { operator float() const; };
19429 Lvalue_proxy<Dense> p;
19434 Here, if Lvalue_proxy is permitted to bind to View, then
19435 the global operator+ will be used; if they are not, the
19436 Lvalue_proxy will be converted to float. */
19437 if (coerce_template_parms (parm_parms
,
19439 TYPE_TI_TEMPLATE (parm
),
19441 ? tf_warning_or_error
19443 /*require_all_args=*/true,
19444 /*use_default_args=*/false)
19445 == error_mark_node
)
19448 /* Deduce arguments T, i from TT<T> or TT<i>.
19449 We check each element of PARMVEC and ARGVEC individually
19450 rather than the whole TREE_VEC since they can have
19451 different number of elements. */
19453 parmvec
= expand_template_argument_pack (parmvec
);
19454 argvec
= expand_template_argument_pack (argvec
);
19456 len
= TREE_VEC_LENGTH (parmvec
);
19458 /* Check if the parameters end in a pack, making them
19461 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
19462 parm_variadic_p
= 1;
19464 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19465 /* If the template argument list of P contains a pack
19466 expansion that is not the last template argument, the
19467 entire template argument list is a non-deduced
19469 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
19470 return unify_success (explain_p
);
19472 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
19473 return unify_too_few_arguments (explain_p
,
19474 TREE_VEC_LENGTH (argvec
), len
);
19476 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19478 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19479 TREE_VEC_ELT (parmvec
, i
),
19480 TREE_VEC_ELT (argvec
, i
),
19481 UNIFY_ALLOW_NONE
, explain_p
);
19484 if (parm_variadic_p
19485 && unify_pack_expansion (tparms
, targs
,
19488 /*subr=*/true, explain_p
))
19491 arg
= TYPE_TI_TEMPLATE (arg
);
19493 /* Fall through to deduce template name. */
19496 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19497 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19499 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19501 /* Simple cases: Value already set, does match or doesn't. */
19502 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
19503 return unify_success (explain_p
);
19505 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19509 /* If PARM is `const T' and ARG is only `int', we don't have
19510 a match unless we are allowing additional qualification.
19511 If ARG is `const int' and PARM is just `T' that's OK;
19512 that binds `const int' to `T'. */
19513 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
19515 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19517 /* Consider the case where ARG is `const volatile int' and
19518 PARM is `const T'. Then, T should be `volatile int'. */
19519 arg
= cp_build_qualified_type_real
19520 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
19521 if (arg
== error_mark_node
)
19522 return unify_invalid (explain_p
);
19524 /* Simple cases: Value already set, does match or doesn't. */
19525 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
19526 return unify_success (explain_p
);
19528 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19530 /* Make sure that ARG is not a variable-sized array. (Note
19531 that were talking about variable-sized arrays (like
19532 `int[n]'), rather than arrays of unknown size (like
19533 `int[]').) We'll get very confused by such a type since
19534 the bound of the array is not constant, and therefore
19535 not mangleable. Besides, such types are not allowed in
19536 ISO C++, so we can do as we please here. We do allow
19537 them for 'auto' deduction, since that isn't ABI-exposed. */
19538 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
19539 return unify_vla_arg (explain_p
, arg
);
19541 /* Strip typedefs as in convert_template_argument. */
19542 arg
= canonicalize_type_argument (arg
, tf_none
);
19545 /* If ARG is a parameter pack or an expansion, we cannot unify
19546 against it unless PARM is also a parameter pack. */
19547 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19548 && !template_parameter_pack_p (parm
))
19549 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19551 /* If the argument deduction results is a METHOD_TYPE,
19552 then there is a problem.
19553 METHOD_TYPE doesn't map to any real C++ type the result of
19554 the deduction can not be of that type. */
19555 if (TREE_CODE (arg
) == METHOD_TYPE
)
19556 return unify_method_type_error (explain_p
, arg
);
19558 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19559 return unify_success (explain_p
);
19561 case TEMPLATE_PARM_INDEX
:
19562 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19563 if (error_operand_p (tparm
))
19564 return unify_invalid (explain_p
);
19566 if (TEMPLATE_PARM_LEVEL (parm
)
19567 != template_decl_level (tparm
))
19569 /* The PARM is not one we're trying to unify. Just check
19570 to see if it matches ARG. */
19571 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
19572 && cp_tree_equal (parm
, arg
));
19574 unify_expression_unequal (explain_p
, parm
, arg
);
19578 idx
= TEMPLATE_PARM_IDX (parm
);
19579 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19583 int x
= !cp_tree_equal (targ
, arg
);
19585 unify_inconsistency (explain_p
, parm
, targ
, arg
);
19589 /* [temp.deduct.type] If, in the declaration of a function template
19590 with a non-type template-parameter, the non-type
19591 template-parameter is used in an expression in the function
19592 parameter-list and, if the corresponding template-argument is
19593 deduced, the template-argument type shall match the type of the
19594 template-parameter exactly, except that a template-argument
19595 deduced from an array bound may be of any integral type.
19596 The non-type parameter might use already deduced type parameters. */
19597 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
19598 if (!TREE_TYPE (arg
))
19599 /* Template-parameter dependent expression. Just accept it for now.
19600 It will later be processed in convert_template_argument. */
19602 else if (same_type_p (TREE_TYPE (arg
), tparm
))
19604 else if ((strict
& UNIFY_ALLOW_INTEGER
)
19605 && CP_INTEGRAL_TYPE_P (tparm
))
19606 /* Convert the ARG to the type of PARM; the deduced non-type
19607 template argument must exactly match the types of the
19608 corresponding parameter. */
19609 arg
= fold (build_nop (tparm
, arg
));
19610 else if (uses_template_parms (tparm
))
19611 /* We haven't deduced the type of this parameter yet. Try again
19613 return unify_success (explain_p
);
19615 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
19617 /* If ARG is a parameter pack or an expansion, we cannot unify
19618 against it unless PARM is also a parameter pack. */
19619 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19620 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
19621 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19624 bool removed_attr
= false;
19625 arg
= strip_typedefs_expr (arg
, &removed_attr
);
19627 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19628 return unify_success (explain_p
);
19632 /* A pointer-to-member constant can be unified only with
19633 another constant. */
19634 if (TREE_CODE (arg
) != PTRMEM_CST
)
19635 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
19637 /* Just unify the class member. It would be useless (and possibly
19638 wrong, depending on the strict flags) to unify also
19639 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19640 arg refer to the same variable, even if through different
19641 classes. For instance:
19643 struct A { int x; };
19646 Unification of &A::x and &B::x must succeed. */
19647 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
19648 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
19653 if (!TYPE_PTR_P (arg
))
19654 return unify_type_mismatch (explain_p
, parm
, arg
);
19656 /* [temp.deduct.call]
19658 A can be another pointer or pointer to member type that can
19659 be converted to the deduced A via a qualification
19660 conversion (_conv.qual_).
19662 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19663 This will allow for additional cv-qualification of the
19664 pointed-to types if appropriate. */
19666 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
19667 /* The derived-to-base conversion only persists through one
19668 level of pointers. */
19669 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
19671 return unify (tparms
, targs
, TREE_TYPE (parm
),
19672 TREE_TYPE (arg
), strict
, explain_p
);
19675 case REFERENCE_TYPE
:
19676 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
19677 return unify_type_mismatch (explain_p
, parm
, arg
);
19678 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19679 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19682 if (TREE_CODE (arg
) != ARRAY_TYPE
)
19683 return unify_type_mismatch (explain_p
, parm
, arg
);
19684 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
19685 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
19686 return unify_type_mismatch (explain_p
, parm
, arg
);
19687 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19688 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19689 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
19690 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19691 TYPE_DOMAIN (arg
), explain_p
);
19692 return unify_success (explain_p
);
19699 case ENUMERAL_TYPE
:
19702 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19703 return unify_type_mismatch (explain_p
, parm
, arg
);
19705 /* We have already checked cv-qualification at the top of the
19707 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
19708 return unify_type_mismatch (explain_p
, parm
, arg
);
19710 /* As far as unification is concerned, this wins. Later checks
19711 will invalidate it if necessary. */
19712 return unify_success (explain_p
);
19714 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19715 /* Type INTEGER_CST can come from ordinary constant template args. */
19717 while (TREE_CODE (arg
) == NOP_EXPR
)
19718 arg
= TREE_OPERAND (arg
, 0);
19720 if (TREE_CODE (arg
) != INTEGER_CST
)
19721 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19722 return (tree_int_cst_equal (parm
, arg
)
19723 ? unify_success (explain_p
)
19724 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
19728 int i
, len
, argslen
;
19729 int parm_variadic_p
= 0;
19731 if (TREE_CODE (arg
) != TREE_VEC
)
19732 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19734 len
= TREE_VEC_LENGTH (parm
);
19735 argslen
= TREE_VEC_LENGTH (arg
);
19737 /* Check for pack expansions in the parameters. */
19738 for (i
= 0; i
< len
; ++i
)
19740 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
19743 /* We can unify against something with a trailing
19745 parm_variadic_p
= 1;
19747 /* [temp.deduct.type]/9: If the template argument list of
19748 P contains a pack expansion that is not the last
19749 template argument, the entire template argument list
19750 is a non-deduced context. */
19751 return unify_success (explain_p
);
19755 /* If we don't have enough arguments to satisfy the parameters
19756 (not counting the pack expression at the end), or we have
19757 too many arguments for a parameter list that doesn't end in
19758 a pack expression, we can't unify. */
19759 if (parm_variadic_p
19760 ? argslen
< len
- parm_variadic_p
19762 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
19764 /* Unify all of the parameters that precede the (optional)
19765 pack expression. */
19766 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19768 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19769 TREE_VEC_ELT (parm
, i
),
19770 TREE_VEC_ELT (arg
, i
),
19771 UNIFY_ALLOW_NONE
, explain_p
);
19773 if (parm_variadic_p
)
19774 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
19776 /*subr=*/true, explain_p
);
19777 return unify_success (explain_p
);
19782 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19783 return unify_type_mismatch (explain_p
, parm
, arg
);
19785 if (TYPE_PTRMEMFUNC_P (parm
))
19787 if (!TYPE_PTRMEMFUNC_P (arg
))
19788 return unify_type_mismatch (explain_p
, parm
, arg
);
19790 return unify (tparms
, targs
,
19791 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
19792 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
19793 strict
, explain_p
);
19795 else if (TYPE_PTRMEMFUNC_P (arg
))
19796 return unify_type_mismatch (explain_p
, parm
, arg
);
19798 if (CLASSTYPE_TEMPLATE_INFO (parm
))
19800 tree t
= NULL_TREE
;
19802 if (strict_in
& UNIFY_ALLOW_DERIVED
)
19804 /* First, we try to unify the PARM and ARG directly. */
19805 t
= try_class_unification (tparms
, targs
,
19806 parm
, arg
, explain_p
);
19810 /* Fallback to the special case allowed in
19811 [temp.deduct.call]:
19813 If P is a class, and P has the form
19814 template-id, then A can be a derived class of
19815 the deduced A. Likewise, if P is a pointer to
19816 a class of the form template-id, A can be a
19817 pointer to a derived class pointed to by the
19819 enum template_base_result r
;
19820 r
= get_template_base (tparms
, targs
, parm
, arg
,
19825 /* Don't give the derived diagnostic if we're
19826 already dealing with the same template. */
19828 = (CLASSTYPE_TEMPLATE_INFO (arg
)
19829 && (CLASSTYPE_TI_TEMPLATE (parm
)
19830 == CLASSTYPE_TI_TEMPLATE (arg
)));
19831 return unify_no_common_base (explain_p
&& !same_template
,
19836 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
19837 && (CLASSTYPE_TI_TEMPLATE (parm
)
19838 == CLASSTYPE_TI_TEMPLATE (arg
)))
19839 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19840 Then, we should unify `int' and `U'. */
19843 /* There's no chance of unification succeeding. */
19844 return unify_type_mismatch (explain_p
, parm
, arg
);
19846 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
19847 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
19849 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
19850 return unify_type_mismatch (explain_p
, parm
, arg
);
19851 return unify_success (explain_p
);
19854 case FUNCTION_TYPE
:
19856 unsigned int nargs
;
19861 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19862 return unify_type_mismatch (explain_p
, parm
, arg
);
19864 /* CV qualifications for methods can never be deduced, they must
19865 match exactly. We need to check them explicitly here,
19866 because type_unification_real treats them as any other
19867 cv-qualified parameter. */
19868 if (TREE_CODE (parm
) == METHOD_TYPE
19869 && (!check_cv_quals_for_unify
19871 class_of_this_parm (arg
),
19872 class_of_this_parm (parm
))))
19873 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19875 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
19876 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
19878 nargs
= list_length (TYPE_ARG_TYPES (arg
));
19879 args
= XALLOCAVEC (tree
, nargs
);
19880 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
19881 a
!= NULL_TREE
&& a
!= void_list_node
;
19882 a
= TREE_CHAIN (a
), ++i
)
19883 args
[i
] = TREE_VALUE (a
);
19886 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
19887 args
, nargs
, 1, DEDUCE_EXACT
,
19888 LOOKUP_NORMAL
, NULL
, explain_p
);
19892 /* Unify a pointer to member with a pointer to member function, which
19893 deduces the type of the member as a function type. */
19894 if (TYPE_PTRMEMFUNC_P (arg
))
19896 /* Check top-level cv qualifiers */
19897 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
19898 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19900 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19901 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
19902 UNIFY_ALLOW_NONE
, explain_p
);
19904 /* Determine the type of the function we are unifying against. */
19905 tree fntype
= static_fn_type (arg
);
19907 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
19910 if (TREE_CODE (arg
) != OFFSET_TYPE
)
19911 return unify_type_mismatch (explain_p
, parm
, arg
);
19912 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19913 TYPE_OFFSET_BASETYPE (arg
),
19914 UNIFY_ALLOW_NONE
, explain_p
);
19915 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19916 strict
, explain_p
);
19919 if (DECL_TEMPLATE_PARM_P (parm
))
19920 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
19921 if (arg
!= scalar_constant_value (parm
))
19922 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19923 return unify_success (explain_p
);
19926 case TEMPLATE_DECL
:
19927 /* Matched cases are handled by the ARG == PARM test above. */
19928 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19931 /* A non-type template parameter that is a variable should be a
19932 an integral constant, in which case, it whould have been
19933 folded into its (constant) value. So we should not be getting
19934 a variable here. */
19935 gcc_unreachable ();
19937 case TYPE_ARGUMENT_PACK
:
19938 case NONTYPE_ARGUMENT_PACK
:
19939 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
19940 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
19943 case DECLTYPE_TYPE
:
19944 case UNDERLYING_TYPE
:
19945 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19946 or UNDERLYING_TYPE nodes. */
19947 return unify_success (explain_p
);
19950 /* Unification fails if we hit an error node. */
19951 return unify_invalid (explain_p
);
19954 if (REFERENCE_REF_P (parm
))
19956 if (REFERENCE_REF_P (arg
))
19957 arg
= TREE_OPERAND (arg
, 0);
19958 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
19959 strict
, explain_p
);
19964 /* An unresolved overload is a nondeduced context. */
19965 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
19966 return unify_success (explain_p
);
19967 gcc_assert (EXPR_P (parm
));
19969 /* We must be looking at an expression. This can happen with
19973 void foo(S<I>, S<I + 2>);
19975 This is a "nondeduced context":
19979 The nondeduced contexts are:
19981 --A type that is a template-id in which one or more of
19982 the template-arguments is an expression that references
19983 a template-parameter.
19985 In these cases, we assume deduction succeeded, but don't
19986 actually infer any unifications. */
19988 if (!uses_template_parms (parm
)
19989 && !template_args_equal (parm
, arg
))
19990 return unify_expression_unequal (explain_p
, parm
, arg
);
19992 return unify_success (explain_p
);
19995 #undef RECUR_AND_CHECK_FAILURE
19997 /* Note that DECL can be defined in this translation unit, if
20001 mark_definable (tree decl
)
20004 DECL_NOT_REALLY_EXTERN (decl
) = 1;
20005 FOR_EACH_CLONE (clone
, decl
)
20006 DECL_NOT_REALLY_EXTERN (clone
) = 1;
20009 /* Called if RESULT is explicitly instantiated, or is a member of an
20010 explicitly instantiated class. */
20013 mark_decl_instantiated (tree result
, int extern_p
)
20015 SET_DECL_EXPLICIT_INSTANTIATION (result
);
20017 /* If this entity has already been written out, it's too late to
20018 make any modifications. */
20019 if (TREE_ASM_WRITTEN (result
))
20022 /* For anonymous namespace we don't need to do anything. */
20023 if (decl_anon_ns_mem_p (result
))
20025 gcc_assert (!TREE_PUBLIC (result
));
20029 if (TREE_CODE (result
) != FUNCTION_DECL
)
20030 /* The TREE_PUBLIC flag for function declarations will have been
20031 set correctly by tsubst. */
20032 TREE_PUBLIC (result
) = 1;
20034 /* This might have been set by an earlier implicit instantiation. */
20035 DECL_COMDAT (result
) = 0;
20038 DECL_NOT_REALLY_EXTERN (result
) = 0;
20041 mark_definable (result
);
20042 mark_needed (result
);
20043 /* Always make artificials weak. */
20044 if (DECL_ARTIFICIAL (result
) && flag_weak
)
20045 comdat_linkage (result
);
20046 /* For WIN32 we also want to put explicit instantiations in
20047 linkonce sections. */
20048 else if (TREE_PUBLIC (result
))
20049 maybe_make_one_only (result
);
20052 /* If EXTERN_P, then this function will not be emitted -- unless
20053 followed by an explicit instantiation, at which point its linkage
20054 will be adjusted. If !EXTERN_P, then this function will be
20055 emitted here. In neither circumstance do we want
20056 import_export_decl to adjust the linkage. */
20057 DECL_INTERFACE_KNOWN (result
) = 1;
20060 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20061 important template arguments. If any are missing, we check whether
20062 they're important by using error_mark_node for substituting into any
20063 args that were used for partial ordering (the ones between ARGS and END)
20064 and seeing if it bubbles up. */
20067 check_undeduced_parms (tree targs
, tree args
, tree end
)
20069 bool found
= false;
20071 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
20072 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
20075 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
20079 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
20080 if (substed
== error_mark_node
)
20086 /* Given two function templates PAT1 and PAT2, return:
20088 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20089 -1 if PAT2 is more specialized than PAT1.
20090 0 if neither is more specialized.
20092 LEN indicates the number of parameters we should consider
20093 (defaulted parameters should not be considered).
20095 The 1998 std underspecified function template partial ordering, and
20096 DR214 addresses the issue. We take pairs of arguments, one from
20097 each of the templates, and deduce them against each other. One of
20098 the templates will be more specialized if all the *other*
20099 template's arguments deduce against its arguments and at least one
20100 of its arguments *does* *not* deduce against the other template's
20101 corresponding argument. Deduction is done as for class templates.
20102 The arguments used in deduction have reference and top level cv
20103 qualifiers removed. Iff both arguments were originally reference
20104 types *and* deduction succeeds in both directions, an lvalue reference
20105 wins against an rvalue reference and otherwise the template
20106 with the more cv-qualified argument wins for that pairing (if
20107 neither is more cv-qualified, they both are equal). Unlike regular
20108 deduction, after all the arguments have been deduced in this way,
20109 we do *not* verify the deduced template argument values can be
20110 substituted into non-deduced contexts.
20112 The logic can be a bit confusing here, because we look at deduce1 and
20113 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20114 can find template arguments for pat1 to make arg1 look like arg2, that
20115 means that arg2 is at least as specialized as arg1. */
20118 more_specialized_fn (tree pat1
, tree pat2
, int len
)
20120 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
20121 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
20122 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
20123 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
20124 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
20125 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
20126 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
20127 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
20128 tree origs1
, origs2
;
20129 bool lose1
= false;
20130 bool lose2
= false;
20132 /* Remove the this parameter from non-static member functions. If
20133 one is a non-static member function and the other is not a static
20134 member function, remove the first parameter from that function
20135 also. This situation occurs for operator functions where we
20136 locate both a member function (with this pointer) and non-member
20137 operator (with explicit first operand). */
20138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
20140 len
--; /* LEN is the number of significant arguments for DECL1 */
20141 args1
= TREE_CHAIN (args1
);
20142 if (!DECL_STATIC_FUNCTION_P (decl2
))
20143 args2
= TREE_CHAIN (args2
);
20145 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
20147 args2
= TREE_CHAIN (args2
);
20148 if (!DECL_STATIC_FUNCTION_P (decl1
))
20151 args1
= TREE_CHAIN (args1
);
20155 /* If only one is a conversion operator, they are unordered. */
20156 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
20159 /* Consider the return type for a conversion function */
20160 if (DECL_CONV_FN_P (decl1
))
20162 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
20163 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
20167 processing_template_decl
++;
20173 /* Stop when an ellipsis is seen. */
20174 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
20176 tree arg1
= TREE_VALUE (args1
);
20177 tree arg2
= TREE_VALUE (args2
);
20178 int deduce1
, deduce2
;
20184 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20185 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20187 /* When both arguments are pack expansions, we need only
20188 unify the patterns themselves. */
20189 arg1
= PACK_EXPANSION_PATTERN (arg1
);
20190 arg2
= PACK_EXPANSION_PATTERN (arg2
);
20192 /* This is the last comparison we need to do. */
20196 arg1
= decay_dependent_array_parm_type (arg1
);
20197 arg2
= decay_dependent_array_parm_type (arg2
);
20199 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
20201 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
20202 arg1
= TREE_TYPE (arg1
);
20203 quals1
= cp_type_quals (arg1
);
20206 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
20208 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
20209 arg2
= TREE_TYPE (arg2
);
20210 quals2
= cp_type_quals (arg2
);
20213 arg1
= TYPE_MAIN_VARIANT (arg1
);
20214 arg2
= TYPE_MAIN_VARIANT (arg2
);
20216 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
20218 int i
, len2
= list_length (args2
);
20219 tree parmvec
= make_tree_vec (1);
20220 tree argvec
= make_tree_vec (len2
);
20223 /* Setup the parameter vector, which contains only ARG1. */
20224 TREE_VEC_ELT (parmvec
, 0) = arg1
;
20226 /* Setup the argument vector, which contains the remaining
20228 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
20229 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20231 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
20232 argvec
, DEDUCE_EXACT
,
20233 /*subr=*/true, /*explain_p=*/false)
20236 /* We cannot deduce in the other direction, because ARG1 is
20237 a pack expansion but ARG2 is not. */
20240 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20242 int i
, len1
= list_length (args1
);
20243 tree parmvec
= make_tree_vec (1);
20244 tree argvec
= make_tree_vec (len1
);
20247 /* Setup the parameter vector, which contains only ARG1. */
20248 TREE_VEC_ELT (parmvec
, 0) = arg2
;
20250 /* Setup the argument vector, which contains the remaining
20252 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
20253 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20255 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
20256 argvec
, DEDUCE_EXACT
,
20257 /*subr=*/true, /*explain_p=*/false)
20260 /* We cannot deduce in the other direction, because ARG2 is
20261 a pack expansion but ARG1 is not.*/
20267 /* The normal case, where neither argument is a pack
20269 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
20270 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20272 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
20273 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20277 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20278 arg2, then arg2 is not as specialized as arg1. */
20284 /* "If, for a given type, deduction succeeds in both directions
20285 (i.e., the types are identical after the transformations above)
20286 and both P and A were reference types (before being replaced with
20287 the type referred to above):
20288 - if the type from the argument template was an lvalue reference and
20289 the type from the parameter template was not, the argument type is
20290 considered to be more specialized than the other; otherwise,
20291 - if the type from the argument template is more cv-qualified
20292 than the type from the parameter template (as described above),
20293 the argument type is considered to be more specialized than the other;
20295 - neither type is more specialized than the other." */
20297 if (deduce1
&& deduce2
)
20299 if (ref1
&& ref2
&& ref1
!= ref2
)
20306 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
20308 if ((quals1
& quals2
) == quals2
)
20310 if ((quals1
& quals2
) == quals1
)
20315 if (lose1
&& lose2
)
20316 /* We've failed to deduce something in either direction.
20317 These must be unordered. */
20320 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20321 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20322 /* We have already processed all of the arguments in our
20323 handing of the pack expansion type. */
20326 args1
= TREE_CHAIN (args1
);
20327 args2
= TREE_CHAIN (args2
);
20330 /* "In most cases, all template parameters must have values in order for
20331 deduction to succeed, but for partial ordering purposes a template
20332 parameter may remain without a value provided it is not used in the
20333 types being used for partial ordering."
20335 Thus, if we are missing any of the targs1 we need to substitute into
20336 origs1, then pat2 is not as specialized as pat1. This can happen when
20337 there is a nondeduced context. */
20338 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
20340 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
20343 processing_template_decl
--;
20345 /* If both deductions succeed, the partial ordering selects the more
20346 constrained template. */
20347 if (!lose1
&& !lose2
)
20349 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
20350 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
20351 lose1
= !subsumes_constraints (c1
, c2
);
20352 lose2
= !subsumes_constraints (c2
, c1
);
20355 /* All things being equal, if the next argument is a pack expansion
20356 for one function but not for the other, prefer the
20357 non-variadic function. FIXME this is bogus; see c++/41958. */
20359 && args1
&& TREE_VALUE (args1
)
20360 && args2
&& TREE_VALUE (args2
))
20362 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
20363 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
20366 if (lose1
== lose2
)
20374 /* Determine which of two partial specializations of TMPL is more
20377 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20378 to the first partial specialization. The TREE_PURPOSE is the
20379 innermost set of template parameters for the partial
20380 specialization. PAT2 is similar, but for the second template.
20382 Return 1 if the first partial specialization is more specialized;
20383 -1 if the second is more specialized; 0 if neither is more
20386 See [temp.class.order] for information about determining which of
20387 two templates is more specialized. */
20390 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
20394 bool any_deductions
= false;
20396 tree tmpl1
= TREE_VALUE (pat1
);
20397 tree tmpl2
= TREE_VALUE (pat2
);
20398 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
20399 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
20400 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
20401 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
20403 /* Just like what happens for functions, if we are ordering between
20404 different template specializations, we may encounter dependent
20405 types in the arguments, and we need our dependency check functions
20406 to behave correctly. */
20407 ++processing_template_decl
;
20408 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
20412 any_deductions
= true;
20415 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
20419 any_deductions
= true;
20421 --processing_template_decl
;
20423 /* If both deductions succeed, the partial ordering selects the more
20424 constrained template. */
20425 if (!winner
&& any_deductions
)
20426 return more_constrained (tmpl1
, tmpl2
);
20428 /* In the case of a tie where at least one of the templates
20429 has a parameter pack at the end, the template with the most
20430 non-packed parameters wins. */
20433 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
20434 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
20436 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
20437 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
20438 int len1
= TREE_VEC_LENGTH (args1
);
20439 int len2
= TREE_VEC_LENGTH (args2
);
20441 /* We don't count the pack expansion at the end. */
20442 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
20444 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
20449 else if (len1
< len2
)
20456 /* Return the template arguments that will produce the function signature
20457 DECL from the function template FN, with the explicit template
20458 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20459 also match. Return NULL_TREE if no satisfactory arguments could be
20463 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
20465 int ntparms
= DECL_NTPARMS (fn
);
20466 tree targs
= make_tree_vec (ntparms
);
20467 tree decl_type
= TREE_TYPE (decl
);
20468 tree decl_arg_types
;
20470 unsigned int nargs
, ix
;
20473 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
20475 /* Never do unification on the 'this' parameter. */
20476 decl_arg_types
= skip_artificial_parms_for (decl
,
20477 TYPE_ARG_TYPES (decl_type
));
20479 nargs
= list_length (decl_arg_types
);
20480 args
= XALLOCAVEC (tree
, nargs
);
20481 for (arg
= decl_arg_types
, ix
= 0;
20482 arg
!= NULL_TREE
&& arg
!= void_list_node
;
20483 arg
= TREE_CHAIN (arg
), ++ix
)
20485 args
[ix
] = TREE_VALUE (arg
);
20486 args
[ix
] = decay_dependent_array_parm_type (args
[ix
]);
20489 if (fn_type_unification (fn
, explicit_args
, targs
,
20491 (check_rettype
|| DECL_CONV_FN_P (fn
)
20492 ? TREE_TYPE (decl_type
) : NULL_TREE
),
20493 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
20495 == error_mark_node
)
20501 /* Return the innermost template arguments that, when applied to a partial
20502 specialization of TMPL whose innermost template parameters are
20503 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20506 For example, suppose we have:
20508 template <class T, class U> struct S {};
20509 template <class T> struct S<T*, int> {};
20511 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20512 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20513 int}. The resulting vector will be {double}, indicating that `T'
20514 is bound to `double'. */
20517 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
20519 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
20521 tree innermost_deduced_args
;
20523 innermost_deduced_args
= make_tree_vec (ntparms
);
20524 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20526 deduced_args
= copy_node (args
);
20527 SET_TMPL_ARGS_LEVEL (deduced_args
,
20528 TMPL_ARGS_DEPTH (deduced_args
),
20529 innermost_deduced_args
);
20532 deduced_args
= innermost_deduced_args
;
20534 if (unify (tparms
, deduced_args
,
20535 INNERMOST_TEMPLATE_ARGS (spec_args
),
20536 INNERMOST_TEMPLATE_ARGS (args
),
20537 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
20540 for (i
= 0; i
< ntparms
; ++i
)
20541 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
20544 /* Verify that nondeduced template arguments agree with the type
20545 obtained from argument deduction.
20549 struct A { typedef int X; };
20550 template <class T, class U> struct C {};
20551 template <class T> struct C<T, typename T::X> {};
20553 Then with the instantiation `C<A, int>', we can deduce that
20554 `T' is `A' but unify () does not check whether `typename T::X'
20556 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
20557 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20559 tf_none
, false, false);
20560 if (spec_args
== error_mark_node
20561 /* We only need to check the innermost arguments; the other
20562 arguments will always agree. */
20563 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
20564 INNERMOST_TEMPLATE_ARGS (args
)))
20567 /* Now that we have bindings for all of the template arguments,
20568 ensure that the arguments deduced for the template template
20569 parameters have compatible template parameter lists. See the use
20570 of template_template_parm_bindings_ok_p in fn_type_unification
20571 for more information. */
20572 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
20575 return deduced_args
;
20578 // Compare two function templates T1 and T2 by deducing bindings
20579 // from one against the other. If both deductions succeed, compare
20580 // constraints to see which is more constrained.
20582 more_specialized_inst (tree t1
, tree t2
)
20587 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
20593 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
20599 // If both deductions succeed, then one may be more constrained.
20600 if (count
== 2 && fate
== 0)
20601 fate
= more_constrained (t1
, t2
);
20606 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20607 Return the TREE_LIST node with the most specialized template, if
20608 any. If there is no most specialized template, the error_mark_node
20611 Note that this function does not look at, or modify, the
20612 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20613 returned is one of the elements of INSTANTIATIONS, callers may
20614 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20615 and retrieve it from the value returned. */
20618 most_specialized_instantiation (tree templates
)
20622 ++processing_template_decl
;
20625 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
20627 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
20632 /* Equally specialized, move to next function. If there
20633 is no next function, nothing's most specialized. */
20634 fn
= TREE_CHAIN (fn
);
20642 /* Now verify that champ is better than everything earlier in the
20643 instantiation list. */
20644 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
20645 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
20652 processing_template_decl
--;
20655 return error_mark_node
;
20660 /* If DECL is a specialization of some template, return the most
20661 general such template. Otherwise, returns NULL_TREE.
20663 For example, given:
20665 template <class T> struct S { template <class U> void f(U); };
20667 if TMPL is `template <class U> void S<int>::f(U)' this will return
20668 the full template. This function will not trace past partial
20669 specializations, however. For example, given in addition:
20671 template <class T> struct S<T*> { template <class U> void f(U); };
20673 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20674 `template <class T> template <class U> S<T*>::f(U)'. */
20677 most_general_template (tree decl
)
20679 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20681 if (tree tinfo
= get_template_info (decl
))
20682 decl
= TI_TEMPLATE (tinfo
);
20683 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20684 template friend, or a FIELD_DECL for a capture pack. */
20685 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20689 /* Look for more and more general templates. */
20690 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
20692 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20693 (See cp-tree.h for details.) */
20694 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
20697 if (CLASS_TYPE_P (TREE_TYPE (decl
))
20698 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
20699 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
20702 /* Stop if we run into an explicitly specialized class template. */
20703 if (!DECL_NAMESPACE_SCOPE_P (decl
)
20704 && DECL_CONTEXT (decl
)
20705 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
20708 decl
= DECL_TI_TEMPLATE (decl
);
20714 /* Return the most specialized of the template partial specializations
20715 which can produce TARGET, a specialization of some class or variable
20716 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20717 a TEMPLATE_DECL node corresponding to the partial specialization, while
20718 the TREE_PURPOSE is the set of template arguments that must be
20719 substituted into the template pattern in order to generate TARGET.
20721 If the choice of partial specialization is ambiguous, a diagnostic
20722 is issued, and the error_mark_node is returned. If there are no
20723 partial specializations matching TARGET, then NULL_TREE is
20724 returned, indicating that the primary template should be used. */
20727 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
20729 tree list
= NULL_TREE
;
20734 tree outer_args
= NULL_TREE
;
20737 if (TYPE_P (target
))
20739 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
20740 tmpl
= TI_TEMPLATE (tinfo
);
20741 args
= TI_ARGS (tinfo
);
20743 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
20745 tmpl
= TREE_OPERAND (target
, 0);
20746 args
= TREE_OPERAND (target
, 1);
20748 else if (VAR_P (target
))
20750 tree tinfo
= DECL_TEMPLATE_INFO (target
);
20751 tmpl
= TI_TEMPLATE (tinfo
);
20752 args
= TI_ARGS (tinfo
);
20755 gcc_unreachable ();
20757 tree main_tmpl
= most_general_template (tmpl
);
20759 /* For determining which partial specialization to use, only the
20760 innermost args are interesting. */
20761 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20763 outer_args
= strip_innermost_template_args (args
, 1);
20764 args
= INNERMOST_TEMPLATE_ARGS (args
);
20767 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
20769 tree partial_spec_args
;
20771 tree spec_tmpl
= TREE_VALUE (t
);
20773 partial_spec_args
= TREE_PURPOSE (t
);
20775 ++processing_template_decl
;
20779 /* Discard the outer levels of args, and then substitute in the
20780 template args from the enclosing class. */
20781 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
20782 partial_spec_args
= tsubst_template_args
20783 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
20785 /* And the same for the partial specialization TEMPLATE_DECL. */
20786 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
20789 partial_spec_args
=
20790 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20793 /*require_all_args=*/true,
20794 /*use_default_args=*/true);
20796 --processing_template_decl
;
20798 if (partial_spec_args
== error_mark_node
)
20799 return error_mark_node
;
20800 if (spec_tmpl
== error_mark_node
)
20801 return error_mark_node
;
20803 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
20804 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
20810 spec_args
= add_to_template_args (outer_args
, spec_args
);
20812 /* Keep the candidate only if the constraints are satisfied,
20813 or if we're not compiling with concepts. */
20815 || constraints_satisfied_p (spec_tmpl
, spec_args
))
20817 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
20818 TREE_TYPE (list
) = TREE_TYPE (t
);
20826 ambiguous_p
= false;
20829 t
= TREE_CHAIN (t
);
20830 for (; t
; t
= TREE_CHAIN (t
))
20832 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20839 t
= TREE_CHAIN (t
);
20842 ambiguous_p
= true;
20851 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
20853 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20856 ambiguous_p
= true;
20864 char *spaces
= NULL
;
20865 if (!(complain
& tf_error
))
20866 return error_mark_node
;
20867 if (TYPE_P (target
))
20868 error ("ambiguous template instantiation for %q#T", target
);
20870 error ("ambiguous template instantiation for %q#D", target
);
20871 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
20872 for (t
= list
; t
; t
= TREE_CHAIN (t
))
20874 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
20875 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
20876 "%s %#S", spaces
? spaces
: str
, subst
);
20877 spaces
= spaces
? spaces
: get_spaces (str
);
20880 return error_mark_node
;
20886 /* Explicitly instantiate DECL. */
20889 do_decl_instantiation (tree decl
, tree storage
)
20891 tree result
= NULL_TREE
;
20894 if (!decl
|| decl
== error_mark_node
)
20895 /* An error occurred, for which grokdeclarator has already issued
20896 an appropriate message. */
20898 else if (! DECL_LANG_SPECIFIC (decl
))
20900 error ("explicit instantiation of non-template %q#D", decl
);
20904 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
20905 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
20907 if (VAR_P (decl
) && !var_templ
)
20909 /* There is an asymmetry here in the way VAR_DECLs and
20910 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20911 the latter, the DECL we get back will be marked as a
20912 template instantiation, and the appropriate
20913 DECL_TEMPLATE_INFO will be set up. This does not happen for
20914 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20915 should handle VAR_DECLs as it currently handles
20917 if (!DECL_CLASS_SCOPE_P (decl
))
20919 error ("%qD is not a static data member of a class template", decl
);
20922 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
20923 if (!result
|| !VAR_P (result
))
20925 error ("no matching template for %qD found", decl
);
20928 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
20930 error ("type %qT for explicit instantiation %qD does not match "
20931 "declared type %qT", TREE_TYPE (result
), decl
,
20936 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
20938 error ("explicit instantiation of %q#D", decl
);
20944 /* Check for various error cases. Note that if the explicit
20945 instantiation is valid the RESULT will currently be marked as an
20946 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20947 until we get here. */
20949 if (DECL_TEMPLATE_SPECIALIZATION (result
))
20951 /* DR 259 [temp.spec].
20953 Both an explicit instantiation and a declaration of an explicit
20954 specialization shall not appear in a program unless the explicit
20955 instantiation follows a declaration of the explicit specialization.
20957 For a given set of template parameters, if an explicit
20958 instantiation of a template appears after a declaration of an
20959 explicit specialization for that template, the explicit
20960 instantiation has no effect. */
20963 else if (DECL_EXPLICIT_INSTANTIATION (result
))
20967 No program shall explicitly instantiate any template more
20970 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20971 the first instantiation was `extern' and the second is not,
20972 and EXTERN_P for the opposite case. */
20973 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
20974 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
20975 /* If an "extern" explicit instantiation follows an ordinary
20976 explicit instantiation, the template is instantiated. */
20980 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
20982 error ("no matching template for %qD found", result
);
20985 else if (!DECL_TEMPLATE_INFO (result
))
20987 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
20991 if (storage
== NULL_TREE
)
20993 else if (storage
== ridpointers
[(int) RID_EXTERN
])
20995 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
20996 pedwarn (input_location
, OPT_Wpedantic
,
20997 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21002 error ("storage class %qD applied to template instantiation", storage
);
21004 check_explicit_instantiation_namespace (result
);
21005 mark_decl_instantiated (result
, extern_p
);
21007 instantiate_decl (result
, /*defer_ok=*/1,
21008 /*expl_inst_class_mem_p=*/false);
21012 mark_class_instantiated (tree t
, int extern_p
)
21014 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
21015 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
21016 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
21017 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
21020 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
21021 rest_of_type_compilation (t
, 1);
21025 /* Called from do_type_instantiation through binding_table_foreach to
21026 do recursive instantiation for the type bound in ENTRY. */
21028 bt_instantiate_type_proc (binding_entry entry
, void *data
)
21030 tree storage
= *(tree
*) data
;
21032 if (MAYBE_CLASS_TYPE_P (entry
->type
)
21033 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
21034 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
21037 /* Called from do_type_instantiation to instantiate a member
21038 (a member function or a static member variable) of an
21039 explicitly instantiated class template. */
21041 instantiate_class_member (tree decl
, int extern_p
)
21043 mark_decl_instantiated (decl
, extern_p
);
21045 instantiate_decl (decl
, /*defer_ok=*/1,
21046 /*expl_inst_class_mem_p=*/true);
21049 /* Perform an explicit instantiation of template class T. STORAGE, if
21050 non-null, is the RID for extern, inline or static. COMPLAIN is
21051 nonzero if this is called from the parser, zero if called recursively,
21052 since the standard is unclear (as detailed below). */
21055 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
21060 int previous_instantiation_extern_p
= 0;
21062 if (TREE_CODE (t
) == TYPE_DECL
)
21065 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
21068 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
21070 error ("explicit instantiation of non-class template %qD", tmpl
);
21072 error ("explicit instantiation of non-template type %qT", t
);
21078 if (!COMPLETE_TYPE_P (t
))
21080 if (complain
& tf_error
)
21081 error ("explicit instantiation of %q#T before definition of template",
21086 if (storage
!= NULL_TREE
)
21088 if (!in_system_header_at (input_location
))
21090 if (storage
== ridpointers
[(int) RID_EXTERN
])
21092 if (cxx_dialect
== cxx98
)
21093 pedwarn (input_location
, OPT_Wpedantic
,
21094 "ISO C++ 1998 forbids the use of %<extern%> on "
21095 "explicit instantiations");
21098 pedwarn (input_location
, OPT_Wpedantic
,
21099 "ISO C++ forbids the use of %qE"
21100 " on explicit instantiations", storage
);
21103 if (storage
== ridpointers
[(int) RID_INLINE
])
21105 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21107 else if (storage
== ridpointers
[(int) RID_STATIC
])
21111 error ("storage class %qD applied to template instantiation",
21117 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
21119 /* DR 259 [temp.spec].
21121 Both an explicit instantiation and a declaration of an explicit
21122 specialization shall not appear in a program unless the explicit
21123 instantiation follows a declaration of the explicit specialization.
21125 For a given set of template parameters, if an explicit
21126 instantiation of a template appears after a declaration of an
21127 explicit specialization for that template, the explicit
21128 instantiation has no effect. */
21131 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
21135 No program shall explicitly instantiate any template more
21138 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21139 instantiation was `extern'. If EXTERN_P then the second is.
21140 These cases are OK. */
21141 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
21143 if (!previous_instantiation_extern_p
&& !extern_p
21144 && (complain
& tf_error
))
21145 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
21147 /* If we've already instantiated the template, just return now. */
21148 if (!CLASSTYPE_INTERFACE_ONLY (t
))
21152 check_explicit_instantiation_namespace (TYPE_NAME (t
));
21153 mark_class_instantiated (t
, extern_p
);
21161 /* In contrast to implicit instantiation, where only the
21162 declarations, and not the definitions, of members are
21163 instantiated, we have here:
21167 The explicit instantiation of a class template specialization
21168 implies the instantiation of all of its members not
21169 previously explicitly specialized in the translation unit
21170 containing the explicit instantiation.
21172 Of course, we can't instantiate member template classes, since
21173 we don't have any arguments for them. Note that the standard
21174 is unclear on whether the instantiation of the members are
21175 *explicit* instantiations or not. However, the most natural
21176 interpretation is that it should be an explicit instantiation. */
21179 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21180 if (TREE_CODE (tmp
) == FUNCTION_DECL
21181 && DECL_TEMPLATE_INSTANTIATION (tmp
))
21182 instantiate_class_member (tmp
, extern_p
);
21184 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21185 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
21186 instantiate_class_member (tmp
, extern_p
);
21188 if (CLASSTYPE_NESTED_UTDS (t
))
21189 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
21190 bt_instantiate_type_proc
, &storage
);
21194 /* Given a function DECL, which is a specialization of TMPL, modify
21195 DECL to be a re-instantiation of TMPL with the same template
21196 arguments. TMPL should be the template into which tsubst'ing
21197 should occur for DECL, not the most general template.
21199 One reason for doing this is a scenario like this:
21202 void f(const T&, int i);
21204 void g() { f(3, 7); }
21207 void f(const T& t, const int i) { }
21209 Note that when the template is first instantiated, with
21210 instantiate_template, the resulting DECL will have no name for the
21211 first parameter, and the wrong type for the second. So, when we go
21212 to instantiate the DECL, we regenerate it. */
21215 regenerate_decl_from_template (tree decl
, tree tmpl
)
21217 /* The arguments used to instantiate DECL, from the most general
21222 args
= DECL_TI_ARGS (decl
);
21223 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21225 /* Make sure that we can see identifiers, and compute access
21227 push_access_scope (decl
);
21229 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21237 args_depth
= TMPL_ARGS_DEPTH (args
);
21238 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
21239 if (args_depth
> parms_depth
)
21240 args
= get_innermost_template_args (args
, parms_depth
);
21242 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
21243 args
, tf_error
, NULL_TREE
,
21244 /*defer_ok*/false);
21245 if (specs
&& specs
!= error_mark_node
)
21246 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
21249 /* Merge parameter declarations. */
21250 decl_parm
= skip_artificial_parms_for (decl
,
21251 DECL_ARGUMENTS (decl
));
21253 = skip_artificial_parms_for (code_pattern
,
21254 DECL_ARGUMENTS (code_pattern
));
21255 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
21260 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21261 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
21262 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
21264 parm_type
= type_decays_to (parm_type
);
21265 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21266 TREE_TYPE (decl_parm
) = parm_type
;
21267 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21268 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21270 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21271 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21273 decl_parm
= DECL_CHAIN (decl_parm
);
21274 pattern_parm
= DECL_CHAIN (pattern_parm
);
21276 /* Merge any parameters that match with the function parameter
21278 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
21281 tree expanded_types
;
21282 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21283 the parameters in this function parameter pack. */
21284 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
21285 args
, tf_error
, NULL_TREE
);
21286 len
= TREE_VEC_LENGTH (expanded_types
);
21287 for (i
= 0; i
< len
; i
++)
21292 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21293 /* Rename the parameter to include the index. */
21294 DECL_NAME (decl_parm
) =
21295 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
21296 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
21297 parm_type
= type_decays_to (parm_type
);
21298 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21299 TREE_TYPE (decl_parm
) = parm_type
;
21300 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21301 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21303 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21304 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21306 decl_parm
= DECL_CHAIN (decl_parm
);
21309 /* Merge additional specifiers from the CODE_PATTERN. */
21310 if (DECL_DECLARED_INLINE_P (code_pattern
)
21311 && !DECL_DECLARED_INLINE_P (decl
))
21312 DECL_DECLARED_INLINE_P (decl
) = 1;
21314 else if (VAR_P (decl
))
21316 DECL_INITIAL (decl
) =
21317 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
21318 tf_error
, DECL_TI_TEMPLATE (decl
),
21319 /*integral_constant_expression_p=*/false);
21320 if (VAR_HAD_UNKNOWN_BOUND (decl
))
21321 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
21322 tf_error
, DECL_TI_TEMPLATE (decl
));
21325 gcc_unreachable ();
21327 pop_access_scope (decl
);
21330 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21331 substituted to get DECL. */
21334 template_for_substitution (tree decl
)
21336 tree tmpl
= DECL_TI_TEMPLATE (decl
);
21338 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21339 for the instantiation. This is not always the most general
21340 template. Consider, for example:
21343 struct S { template <class U> void f();
21344 template <> void f<int>(); };
21346 and an instantiation of S<double>::f<int>. We want TD to be the
21347 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21348 while (/* An instantiation cannot have a definition, so we need a
21349 more general template. */
21350 DECL_TEMPLATE_INSTANTIATION (tmpl
)
21351 /* We must also deal with friend templates. Given:
21353 template <class T> struct S {
21354 template <class U> friend void f() {};
21357 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21358 so far as the language is concerned, but that's still
21359 where we get the pattern for the instantiation from. On
21360 other hand, if the definition comes outside the class, say:
21362 template <class T> struct S {
21363 template <class U> friend void f();
21365 template <class U> friend void f() {}
21367 we don't need to look any further. That's what the check for
21368 DECL_INITIAL is for. */
21369 || (TREE_CODE (decl
) == FUNCTION_DECL
21370 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
21371 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
21373 /* The present template, TD, should not be a definition. If it
21374 were a definition, we should be using it! Note that we
21375 cannot restructure the loop to just keep going until we find
21376 a template with a definition, since that might go too far if
21377 a specialization was declared, but not defined. */
21379 /* Fetch the more general template. */
21380 tmpl
= DECL_TI_TEMPLATE (tmpl
);
21386 /* Returns true if we need to instantiate this template instance even if we
21387 know we aren't going to emit it. */
21390 always_instantiate_p (tree decl
)
21392 /* We always instantiate inline functions so that we can inline them. An
21393 explicit instantiation declaration prohibits implicit instantiation of
21394 non-inline functions. With high levels of optimization, we would
21395 normally inline non-inline functions -- but we're not allowed to do
21396 that for "extern template" functions. Therefore, we check
21397 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21398 return ((TREE_CODE (decl
) == FUNCTION_DECL
21399 && (DECL_DECLARED_INLINE_P (decl
)
21400 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
21401 /* And we need to instantiate static data members so that
21402 their initializers are available in integral constant
21405 && decl_maybe_constant_var_p (decl
)));
21408 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21409 instantiate it now, modifying TREE_TYPE (fn). */
21412 maybe_instantiate_noexcept (tree fn
)
21414 tree fntype
, spec
, noex
, clone
;
21416 /* Don't instantiate a noexcept-specification from template context. */
21417 if (processing_template_decl
)
21420 if (DECL_CLONED_FUNCTION_P (fn
))
21421 fn
= DECL_CLONED_FUNCTION (fn
);
21422 fntype
= TREE_TYPE (fn
);
21423 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
21425 if (!spec
|| !TREE_PURPOSE (spec
))
21428 noex
= TREE_PURPOSE (spec
);
21430 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
21432 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
21433 spec
= get_defaulted_eh_spec (fn
);
21434 else if (push_tinst_level (fn
))
21436 push_access_scope (fn
);
21437 push_deferring_access_checks (dk_no_deferred
);
21438 input_location
= DECL_SOURCE_LOCATION (fn
);
21439 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
21440 DEFERRED_NOEXCEPT_ARGS (noex
),
21441 tf_warning_or_error
, fn
,
21442 /*function_p=*/false,
21443 /*integral_constant_expression_p=*/true);
21444 pop_deferring_access_checks ();
21445 pop_access_scope (fn
);
21446 pop_tinst_level ();
21447 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
21448 if (spec
== error_mark_node
)
21449 spec
= noexcept_false_spec
;
21452 spec
= noexcept_false_spec
;
21454 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
21457 FOR_EACH_CLONE (clone
, fn
)
21459 if (TREE_TYPE (clone
) == fntype
)
21460 TREE_TYPE (clone
) = TREE_TYPE (fn
);
21462 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
21466 /* Produce the definition of D, a _DECL generated from a template. If
21467 DEFER_OK is nonzero, then we don't have to actually do the
21468 instantiation now; we just have to do it sometime. Normally it is
21469 an error if this is an explicit instantiation but D is undefined.
21470 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21471 explicitly instantiated class template. */
21474 instantiate_decl (tree d
, int defer_ok
,
21475 bool expl_inst_class_mem_p
)
21477 tree tmpl
= DECL_TI_TEMPLATE (d
);
21484 bool pattern_defined
;
21485 location_t saved_loc
= input_location
;
21486 int saved_unevaluated_operand
= cp_unevaluated_operand
;
21487 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
21491 bool nested
= false;
21493 /* This function should only be used to instantiate templates for
21494 functions and static member variables. */
21495 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
21497 /* A concept is never instantiated. */
21498 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
21500 /* Variables are never deferred; if instantiation is required, they
21501 are instantiated right away. That allows for better code in the
21502 case that an expression refers to the value of the variable --
21503 if the variable has a constant value the referring expression can
21504 take advantage of that fact. */
21506 || DECL_DECLARED_CONSTEXPR_P (d
))
21509 /* Don't instantiate cloned functions. Instead, instantiate the
21510 functions they cloned. */
21511 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
21512 d
= DECL_CLONED_FUNCTION (d
);
21514 if (DECL_TEMPLATE_INSTANTIATED (d
)
21515 || (TREE_CODE (d
) == FUNCTION_DECL
21516 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
21517 || DECL_TEMPLATE_SPECIALIZATION (d
))
21518 /* D has already been instantiated or explicitly specialized, so
21519 there's nothing for us to do here.
21521 It might seem reasonable to check whether or not D is an explicit
21522 instantiation, and, if so, stop here. But when an explicit
21523 instantiation is deferred until the end of the compilation,
21524 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21525 the instantiation. */
21528 /* Check to see whether we know that this template will be
21529 instantiated in some other file, as with "extern template"
21531 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
21533 /* In general, we do not instantiate such templates. */
21534 if (external_p
&& !always_instantiate_p (d
))
21537 gen_tmpl
= most_general_template (tmpl
);
21538 gen_args
= DECL_TI_ARGS (d
);
21540 if (tmpl
!= gen_tmpl
)
21541 /* We should already have the extra args. */
21542 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
21543 == TMPL_ARGS_DEPTH (gen_args
));
21544 /* And what's in the hash table should match D. */
21545 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
21546 || spec
== NULL_TREE
);
21548 /* This needs to happen before any tsubsting. */
21549 if (! push_tinst_level (d
))
21552 timevar_push (TV_TEMPLATE_INST
);
21554 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21555 for the instantiation. */
21556 td
= template_for_substitution (d
);
21557 code_pattern
= DECL_TEMPLATE_RESULT (td
);
21559 /* We should never be trying to instantiate a member of a class
21560 template or partial specialization. */
21561 gcc_assert (d
!= code_pattern
);
21563 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
21564 || DECL_TEMPLATE_SPECIALIZATION (td
))
21565 /* In the case of a friend template whose definition is provided
21566 outside the class, we may have too many arguments. Drop the
21567 ones we don't need. The same is true for specializations. */
21568 args
= get_innermost_template_args
21569 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
21573 if (TREE_CODE (d
) == FUNCTION_DECL
)
21575 deleted_p
= DECL_DELETED_FN (code_pattern
);
21576 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
21577 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
21583 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
21586 /* We may be in the middle of deferred access check. Disable it now. */
21587 push_deferring_access_checks (dk_no_deferred
);
21589 /* Unless an explicit instantiation directive has already determined
21590 the linkage of D, remember that a definition is available for
21592 if (pattern_defined
21593 && !DECL_INTERFACE_KNOWN (d
)
21594 && !DECL_NOT_REALLY_EXTERN (d
))
21595 mark_definable (d
);
21597 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
21598 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
21599 input_location
= DECL_SOURCE_LOCATION (d
);
21601 /* If D is a member of an explicitly instantiated class template,
21602 and no definition is available, treat it like an implicit
21604 if (!pattern_defined
&& expl_inst_class_mem_p
21605 && DECL_EXPLICIT_INSTANTIATION (d
))
21607 /* Leave linkage flags alone on instantiations with anonymous
21609 if (TREE_PUBLIC (d
))
21611 DECL_NOT_REALLY_EXTERN (d
) = 0;
21612 DECL_INTERFACE_KNOWN (d
) = 0;
21614 SET_DECL_IMPLICIT_INSTANTIATION (d
);
21617 /* Defer all other templates, unless we have been explicitly
21618 forbidden from doing so. */
21619 if (/* If there is no definition, we cannot instantiate the
21622 /* If it's OK to postpone instantiation, do so. */
21624 /* If this is a static data member that will be defined
21625 elsewhere, we don't want to instantiate the entire data
21626 member, but we do want to instantiate the initializer so that
21627 we can substitute that elsewhere. */
21628 || (external_p
&& VAR_P (d
))
21629 /* Handle here a deleted function too, avoid generating
21630 its body (c++/61080). */
21633 /* The definition of the static data member is now required so
21634 we must substitute the initializer. */
21636 && !DECL_INITIAL (d
)
21637 && DECL_INITIAL (code_pattern
))
21641 bool const_init
= false;
21642 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21644 ns
= decl_namespace_context (d
);
21645 push_nested_namespace (ns
);
21647 push_nested_class (DECL_CONTEXT (d
));
21648 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
21650 tf_warning_or_error
, NULL_TREE
,
21651 /*integral_constant_expression_p=*/false);
21652 /* If instantiating the initializer involved instantiating this
21653 again, don't call cp_finish_decl twice. */
21654 if (!DECL_INITIAL (d
))
21656 /* Make sure the initializer is still constant, in case of
21657 circular dependency (template/instantiate6.C). */
21659 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21660 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
21661 /*asmspec_tree=*/NULL_TREE
,
21662 LOOKUP_ONLYCONVERTING
);
21665 pop_nested_class ();
21666 pop_nested_namespace (ns
);
21669 /* We restore the source position here because it's used by
21670 add_pending_template. */
21671 input_location
= saved_loc
;
21673 if (at_eof
&& !pattern_defined
21674 && DECL_EXPLICIT_INSTANTIATION (d
)
21675 && DECL_NOT_REALLY_EXTERN (d
))
21678 The definition of a non-exported function template, a
21679 non-exported member function template, or a non-exported
21680 member function or static data member of a class template
21681 shall be present in every translation unit in which it is
21682 explicitly instantiated. */
21683 permerror (input_location
, "explicit instantiation of %qD "
21684 "but no definition available", d
);
21686 /* If we're in unevaluated context, we just wanted to get the
21687 constant value; this isn't an odr use, so don't queue
21688 a full instantiation. */
21689 if (cp_unevaluated_operand
!= 0)
21691 /* ??? Historically, we have instantiated inline functions, even
21692 when marked as "extern template". */
21693 if (!(external_p
&& VAR_P (d
)))
21694 add_pending_template (d
);
21697 /* Tell the repository that D is available in this translation unit
21698 -- and see if it is supposed to be instantiated here. */
21699 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
21701 /* In a PCH file, despite the fact that the repository hasn't
21702 requested instantiation in the PCH it is still possible that
21703 an instantiation will be required in a file that includes the
21706 add_pending_template (d
);
21707 /* Instantiate inline functions so that the inliner can do its
21708 job, even though we'll not be emitting a copy of this
21710 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
21714 fn_context
= decl_function_context (d
);
21715 nested
= (current_function_decl
!= NULL_TREE
);
21716 vec
<tree
> omp_privatization_save
;
21718 save_omp_privatization_clauses (omp_privatization_save
);
21721 push_to_top_level ();
21725 push_function_context ();
21726 cp_unevaluated_operand
= 0;
21727 c_inhibit_evaluation_warnings
= 0;
21730 /* Mark D as instantiated so that recursive calls to
21731 instantiate_decl do not try to instantiate it again. */
21732 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
21734 /* Regenerate the declaration in case the template has been modified
21735 by a subsequent redeclaration. */
21736 regenerate_decl_from_template (d
, td
);
21738 /* We already set the file and line above. Reset them now in case
21739 they changed as a result of calling regenerate_decl_from_template. */
21740 input_location
= DECL_SOURCE_LOCATION (d
);
21745 bool const_init
= false;
21747 /* Clear out DECL_RTL; whatever was there before may not be right
21748 since we've reset the type of the declaration. */
21749 SET_DECL_RTL (d
, NULL
);
21750 DECL_IN_AGGR_P (d
) = 0;
21752 /* The initializer is placed in DECL_INITIAL by
21753 regenerate_decl_from_template so we don't need to
21754 push/pop_access_scope again here. Pull it out so that
21755 cp_finish_decl can process it. */
21756 init
= DECL_INITIAL (d
);
21757 DECL_INITIAL (d
) = NULL_TREE
;
21758 DECL_INITIALIZED_P (d
) = 0;
21760 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21761 initializer. That function will defer actual emission until
21762 we have a chance to determine linkage. */
21763 DECL_EXTERNAL (d
) = 0;
21765 /* Enter the scope of D so that access-checking works correctly. */
21766 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21768 push_nested_class (DECL_CONTEXT (d
));
21770 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21771 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
21774 pop_nested_class ();
21776 if (variable_template_p (td
))
21777 note_variable_template_instantiation (d
);
21779 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
21780 synthesize_method (d
);
21781 else if (TREE_CODE (d
) == FUNCTION_DECL
)
21783 hash_map
<tree
, tree
> *saved_local_specializations
;
21787 tree block
= NULL_TREE
;
21789 /* Save away the current list, in case we are instantiating one
21790 template from within the body of another. */
21791 saved_local_specializations
= local_specializations
;
21793 /* Set up the list of local specializations. */
21794 local_specializations
= new hash_map
<tree
, tree
>;
21796 /* Set up context. */
21797 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21798 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21799 block
= push_stmt_list ();
21801 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
21803 /* Some typedefs referenced from within the template code need to be
21804 access checked at template instantiation time, i.e now. These
21805 types were added to the template at parsing time. Let's get those
21806 and perform the access checks then. */
21807 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
21810 /* Create substitution entries for the parameters. */
21811 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
21812 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
21813 spec_parm
= DECL_ARGUMENTS (d
);
21814 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
21816 register_local_specialization (spec_parm
, tmpl_parm
);
21817 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
21818 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
21820 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
21822 if (!DECL_PACK_P (tmpl_parm
))
21824 register_local_specialization (spec_parm
, tmpl_parm
);
21825 spec_parm
= DECL_CHAIN (spec_parm
);
21829 /* Register the (value) argument pack as a specialization of
21830 TMPL_PARM, then move on. */
21831 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
21832 register_local_specialization (argpack
, tmpl_parm
);
21835 gcc_assert (!spec_parm
);
21837 /* Substitute into the body of the function. */
21838 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21839 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
21840 tf_warning_or_error
, tmpl
);
21843 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
21844 tf_warning_or_error
, tmpl
,
21845 /*integral_constant_expression_p=*/false);
21847 /* Set the current input_location to the end of the function
21848 so that finish_function knows where we are. */
21850 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
21852 /* Remember if we saw an infinite loop in the template. */
21853 current_function_infinite_loop
21854 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
21857 /* We don't need the local specializations any more. */
21858 delete local_specializations
;
21859 local_specializations
= saved_local_specializations
;
21861 /* Finish the function. */
21862 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21863 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21864 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
21867 d
= finish_function (0);
21868 expand_or_defer_fn (d
);
21871 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21872 cp_check_omp_declare_reduction (d
);
21875 /* We're not deferring instantiation any more. */
21876 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
21879 pop_from_top_level ();
21881 pop_function_context ();
21884 input_location
= saved_loc
;
21885 cp_unevaluated_operand
= saved_unevaluated_operand
;
21886 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
21887 pop_deferring_access_checks ();
21888 pop_tinst_level ();
21890 restore_omp_privatization_clauses (omp_privatization_save
);
21892 timevar_pop (TV_TEMPLATE_INST
);
21897 /* Run through the list of templates that we wish we could
21898 instantiate, and instantiate any we can. RETRIES is the
21899 number of times we retry pending template instantiation. */
21902 instantiate_pending_templates (int retries
)
21905 location_t saved_loc
= input_location
;
21907 /* Instantiating templates may trigger vtable generation. This in turn
21908 may require further template instantiations. We place a limit here
21909 to avoid infinite loop. */
21910 if (pending_templates
&& retries
>= max_tinst_depth
)
21912 tree decl
= pending_templates
->tinst
->decl
;
21914 fatal_error (input_location
,
21915 "template instantiation depth exceeds maximum of %d"
21916 " instantiating %q+D, possibly from virtual table generation"
21917 " (use -ftemplate-depth= to increase the maximum)",
21918 max_tinst_depth
, decl
);
21919 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21920 /* Pretend that we defined it. */
21921 DECL_INITIAL (decl
) = error_mark_node
;
21927 struct pending_template
**t
= &pending_templates
;
21928 struct pending_template
*last
= NULL
;
21932 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
21933 bool complete
= false;
21935 if (TYPE_P (instantiation
))
21939 if (!COMPLETE_TYPE_P (instantiation
))
21941 instantiate_class_template (instantiation
);
21942 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
21943 for (fn
= TYPE_METHODS (instantiation
);
21945 fn
= TREE_CHAIN (fn
))
21946 if (! DECL_ARTIFICIAL (fn
))
21947 instantiate_decl (fn
,
21949 /*expl_inst_class_mem_p=*/false);
21950 if (COMPLETE_TYPE_P (instantiation
))
21954 complete
= COMPLETE_TYPE_P (instantiation
);
21958 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21959 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
21962 = instantiate_decl (instantiation
,
21964 /*expl_inst_class_mem_p=*/false);
21965 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
21969 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21970 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
21974 /* If INSTANTIATION has been instantiated, then we don't
21975 need to consider it again in the future. */
21983 current_tinst_level
= NULL
;
21985 last_pending_template
= last
;
21987 while (reconsider
);
21989 input_location
= saved_loc
;
21992 /* Substitute ARGVEC into T, which is a list of initializers for
21993 either base class or a non-static data member. The TREE_PURPOSEs
21994 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21995 instantiate_decl. */
21998 tsubst_initializer_list (tree t
, tree argvec
)
22000 tree inits
= NULL_TREE
;
22002 for (; t
; t
= TREE_CHAIN (t
))
22006 tree expanded_bases
= NULL_TREE
;
22007 tree expanded_arguments
= NULL_TREE
;
22010 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
22015 /* Expand the base class expansion type into separate base
22017 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
22018 tf_warning_or_error
,
22020 if (expanded_bases
== error_mark_node
)
22023 /* We'll be building separate TREE_LISTs of arguments for
22025 len
= TREE_VEC_LENGTH (expanded_bases
);
22026 expanded_arguments
= make_tree_vec (len
);
22027 for (i
= 0; i
< len
; i
++)
22028 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
22030 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22031 expand each argument in the TREE_VALUE of t. */
22032 expr
= make_node (EXPR_PACK_EXPANSION
);
22033 PACK_EXPANSION_LOCAL_P (expr
) = true;
22034 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
22035 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
22037 if (TREE_VALUE (t
) == void_type_node
)
22038 /* VOID_TYPE_NODE is used to indicate
22039 value-initialization. */
22041 for (i
= 0; i
< len
; i
++)
22042 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
22046 /* Substitute parameter packs into each argument in the
22048 in_base_initializer
= 1;
22049 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
22051 tree expanded_exprs
;
22053 /* Expand the argument. */
22054 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
22056 = tsubst_pack_expansion (expr
, argvec
,
22057 tf_warning_or_error
,
22059 if (expanded_exprs
== error_mark_node
)
22062 /* Prepend each of the expanded expressions to the
22063 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22064 for (i
= 0; i
< len
; i
++)
22066 TREE_VEC_ELT (expanded_arguments
, i
) =
22067 tree_cons (NULL_TREE
,
22068 TREE_VEC_ELT (expanded_exprs
, i
),
22069 TREE_VEC_ELT (expanded_arguments
, i
));
22072 in_base_initializer
= 0;
22074 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22075 since we built them backwards. */
22076 for (i
= 0; i
< len
; i
++)
22078 TREE_VEC_ELT (expanded_arguments
, i
) =
22079 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
22084 for (i
= 0; i
< len
; ++i
)
22086 if (expanded_bases
)
22088 decl
= TREE_VEC_ELT (expanded_bases
, i
);
22089 decl
= expand_member_init (decl
);
22090 init
= TREE_VEC_ELT (expanded_arguments
, i
);
22095 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
22096 tf_warning_or_error
, NULL_TREE
);
22098 decl
= expand_member_init (decl
);
22099 if (decl
&& !DECL_P (decl
))
22100 in_base_initializer
= 1;
22102 init
= TREE_VALUE (t
);
22104 if (init
!= void_type_node
)
22105 init
= tsubst_expr (init
, argvec
,
22106 tf_warning_or_error
, NULL_TREE
,
22107 /*integral_constant_expression_p=*/false);
22108 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
22109 /* If we had an initializer but it instantiated to nothing,
22110 value-initialize the object. This will only occur when
22111 the initializer was a pack expansion where the parameter
22112 packs used in that expansion were of length zero. */
22113 init
= void_type_node
;
22114 in_base_initializer
= 0;
22119 init
= build_tree_list (decl
, init
);
22120 TREE_CHAIN (init
) = inits
;
22128 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22131 set_current_access_from_decl (tree decl
)
22133 if (TREE_PRIVATE (decl
))
22134 current_access_specifier
= access_private_node
;
22135 else if (TREE_PROTECTED (decl
))
22136 current_access_specifier
= access_protected_node
;
22138 current_access_specifier
= access_public_node
;
22141 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22142 is the instantiation (which should have been created with
22143 start_enum) and ARGS are the template arguments to use. */
22146 tsubst_enum (tree tag
, tree newtag
, tree args
)
22150 if (SCOPED_ENUM_P (newtag
))
22151 begin_scope (sk_scoped_enum
, newtag
);
22153 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
22158 decl
= TREE_VALUE (e
);
22159 /* Note that in a template enum, the TREE_VALUE is the
22160 CONST_DECL, not the corresponding INTEGER_CST. */
22161 value
= tsubst_expr (DECL_INITIAL (decl
),
22162 args
, tf_warning_or_error
, NULL_TREE
,
22163 /*integral_constant_expression_p=*/true);
22165 /* Give this enumeration constant the correct access. */
22166 set_current_access_from_decl (decl
);
22168 /* Actually build the enumerator itself. Here we're assuming that
22169 enumerators can't have dependent attributes. */
22170 build_enumerator (DECL_NAME (decl
), value
, newtag
,
22171 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
22174 if (SCOPED_ENUM_P (newtag
))
22177 finish_enum_value_list (newtag
);
22178 finish_enum (newtag
);
22180 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
22181 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
22184 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22185 its type -- but without substituting the innermost set of template
22186 arguments. So, innermost set of template parameters will appear in
22190 get_mostly_instantiated_function_type (tree decl
)
22192 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22193 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
22196 /* Return truthvalue if we're processing a template different from
22197 the last one involved in diagnostics. */
22199 problematic_instantiation_changed (void)
22201 return current_tinst_level
!= last_error_tinst_level
;
22204 /* Remember current template involved in diagnostics. */
22206 record_last_problematic_instantiation (void)
22208 last_error_tinst_level
= current_tinst_level
;
22211 struct tinst_level
*
22212 current_instantiation (void)
22214 return current_tinst_level
;
22217 /* Return TRUE if current_function_decl is being instantiated, false
22221 instantiating_current_function_p (void)
22223 return (current_instantiation ()
22224 && current_instantiation ()->decl
== current_function_decl
);
22227 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22228 type. Return zero for ok, nonzero for disallowed. Issue error and
22229 warning messages under control of COMPLAIN. */
22232 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
22234 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
22236 else if (POINTER_TYPE_P (type
))
22238 else if (TYPE_PTRMEM_P (type
))
22240 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
22242 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
22244 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
22246 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
22248 /* A bound template template parm could later be instantiated to have a valid
22249 nontype parm type via an alias template. */
22250 else if (cxx_dialect
>= cxx11
22251 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22254 if (complain
& tf_error
)
22256 if (type
== error_mark_node
)
22257 inform (input_location
, "invalid template non-type parameter");
22259 error ("%q#T is not a valid type for a template non-type parameter",
22265 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22266 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22269 dependent_type_p_r (tree type
)
22275 A type is dependent if it is:
22277 -- a template parameter. Template template parameters are types
22278 for us (since TYPE_P holds true for them) so we handle
22280 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22281 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
22283 /* -- a qualified-id with a nested-name-specifier which contains a
22284 class-name that names a dependent type or whose unqualified-id
22285 names a dependent type. */
22286 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22289 /* An alias template specialization can be dependent even if the
22290 resulting type is not. */
22291 if (dependent_alias_template_spec_p (type
))
22294 /* -- a cv-qualified type where the cv-unqualified type is
22296 No code is necessary for this bullet; the code below handles
22297 cv-qualified types, and we don't want to strip aliases with
22298 TYPE_MAIN_VARIANT because of DR 1558. */
22299 /* -- a compound type constructed from any dependent type. */
22300 if (TYPE_PTRMEM_P (type
))
22301 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
22302 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22304 else if (TYPE_PTR_P (type
)
22305 || TREE_CODE (type
) == REFERENCE_TYPE
)
22306 return dependent_type_p (TREE_TYPE (type
));
22307 else if (TREE_CODE (type
) == FUNCTION_TYPE
22308 || TREE_CODE (type
) == METHOD_TYPE
)
22312 if (dependent_type_p (TREE_TYPE (type
)))
22314 for (arg_type
= TYPE_ARG_TYPES (type
);
22316 arg_type
= TREE_CHAIN (arg_type
))
22317 if (dependent_type_p (TREE_VALUE (arg_type
)))
22321 /* -- an array type constructed from any dependent type or whose
22322 size is specified by a constant expression that is
22325 We checked for type- and value-dependence of the bounds in
22326 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22327 if (TREE_CODE (type
) == ARRAY_TYPE
)
22329 if (TYPE_DOMAIN (type
)
22330 && dependent_type_p (TYPE_DOMAIN (type
)))
22332 return dependent_type_p (TREE_TYPE (type
));
22335 /* -- a template-id in which either the template name is a template
22337 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22339 /* ... or any of the template arguments is a dependent type or
22340 an expression that is type-dependent or value-dependent. */
22341 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
22342 && (any_dependent_template_arguments_p
22343 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
22346 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22347 dependent; if the argument of the `typeof' expression is not
22348 type-dependent, then it should already been have resolved. */
22349 if (TREE_CODE (type
) == TYPEOF_TYPE
22350 || TREE_CODE (type
) == DECLTYPE_TYPE
22351 || TREE_CODE (type
) == UNDERLYING_TYPE
)
22354 /* A template argument pack is dependent if any of its packed
22356 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
22358 tree args
= ARGUMENT_PACK_ARGS (type
);
22359 int i
, len
= TREE_VEC_LENGTH (args
);
22360 for (i
= 0; i
< len
; ++i
)
22361 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
22365 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22366 be template parameters. */
22367 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
22370 /* The standard does not specifically mention types that are local
22371 to template functions or local classes, but they should be
22372 considered dependent too. For example:
22374 template <int I> void f() {
22379 The size of `E' cannot be known until the value of `I' has been
22380 determined. Therefore, `E' must be considered dependent. */
22381 scope
= TYPE_CONTEXT (type
);
22382 if (scope
&& TYPE_P (scope
))
22383 return dependent_type_p (scope
);
22384 /* Don't use type_dependent_expression_p here, as it can lead
22385 to infinite recursion trying to determine whether a lambda
22386 nested in a lambda is dependent (c++/47687). */
22387 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
22388 && DECL_LANG_SPECIFIC (scope
)
22389 && DECL_TEMPLATE_INFO (scope
)
22390 && (any_dependent_template_arguments_p
22391 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
22394 /* Other types are non-dependent. */
22398 /* Returns TRUE if TYPE is dependent, in the sense of
22399 [temp.dep.type]. Note that a NULL type is considered dependent. */
22402 dependent_type_p (tree type
)
22404 /* If there are no template parameters in scope, then there can't be
22405 any dependent types. */
22406 if (!processing_template_decl
)
22408 /* If we are not processing a template, then nobody should be
22409 providing us with a dependent type. */
22411 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
22415 /* If the type is NULL, we have not computed a type for the entity
22416 in question; in that case, the type is dependent. */
22420 /* Erroneous types can be considered non-dependent. */
22421 if (type
== error_mark_node
)
22424 /* If we have not already computed the appropriate value for TYPE,
22426 if (!TYPE_DEPENDENT_P_VALID (type
))
22428 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
22429 TYPE_DEPENDENT_P_VALID (type
) = 1;
22432 return TYPE_DEPENDENT_P (type
);
22435 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22436 lookup. In other words, a dependent type that is not the current
22440 dependent_scope_p (tree scope
)
22442 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
22443 && !currently_open_class (scope
));
22446 /* T is a SCOPE_REF; return whether we need to consider it
22447 instantiation-dependent so that we can check access at instantiation
22448 time even though we know which member it resolves to. */
22451 instantiation_dependent_scope_ref_p (tree t
)
22453 if (DECL_P (TREE_OPERAND (t
, 1))
22454 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
22455 && accessible_in_template_p (TREE_OPERAND (t
, 0),
22456 TREE_OPERAND (t
, 1)))
22462 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22463 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22466 /* Note that this predicate is not appropriate for general expressions;
22467 only constant expressions (that satisfy potential_constant_expression)
22468 can be tested for value dependence. */
22471 value_dependent_expression_p (tree expression
)
22473 if (!processing_template_decl
)
22476 /* A name declared with a dependent type. */
22477 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
22480 switch (TREE_CODE (expression
))
22482 case IDENTIFIER_NODE
:
22483 /* A name that has not been looked up -- must be dependent. */
22486 case TEMPLATE_PARM_INDEX
:
22487 /* A non-type template parm. */
22491 /* A non-type template parm. */
22492 if (DECL_TEMPLATE_PARM_P (expression
))
22494 return value_dependent_expression_p (DECL_INITIAL (expression
));
22497 /* A constant with literal type and is initialized
22498 with an expression that is value-dependent.
22500 Note that a non-dependent parenthesized initializer will have
22501 already been replaced with its constant value, so if we see
22502 a TREE_LIST it must be dependent. */
22503 if (DECL_INITIAL (expression
)
22504 && decl_constant_var_p (expression
)
22505 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
22506 /* cp_finish_decl doesn't fold reference initializers. */
22507 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
22508 || value_dependent_expression_p (DECL_INITIAL (expression
))))
22512 case DYNAMIC_CAST_EXPR
:
22513 case STATIC_CAST_EXPR
:
22514 case CONST_CAST_EXPR
:
22515 case REINTERPRET_CAST_EXPR
:
22517 /* These expressions are value-dependent if the type to which
22518 the cast occurs is dependent or the expression being casted
22519 is value-dependent. */
22521 tree type
= TREE_TYPE (expression
);
22523 if (dependent_type_p (type
))
22526 /* A functional cast has a list of operands. */
22527 expression
= TREE_OPERAND (expression
, 0);
22530 /* If there are no operands, it must be an expression such
22531 as "int()". This should not happen for aggregate types
22532 because it would form non-constant expressions. */
22533 gcc_assert (cxx_dialect
>= cxx11
22534 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
22539 if (TREE_CODE (expression
) == TREE_LIST
)
22540 return any_value_dependent_elements_p (expression
);
22542 return value_dependent_expression_p (expression
);
22546 if (SIZEOF_EXPR_TYPE_P (expression
))
22547 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
22551 /* A `sizeof' expression is value-dependent if the operand is
22552 type-dependent or is a pack expansion. */
22553 expression
= TREE_OPERAND (expression
, 0);
22554 if (PACK_EXPANSION_P (expression
))
22556 else if (TYPE_P (expression
))
22557 return dependent_type_p (expression
);
22558 return instantiation_dependent_expression_p (expression
);
22560 case AT_ENCODE_EXPR
:
22561 /* An 'encode' expression is value-dependent if the operand is
22563 expression
= TREE_OPERAND (expression
, 0);
22564 return dependent_type_p (expression
);
22566 case NOEXCEPT_EXPR
:
22567 expression
= TREE_OPERAND (expression
, 0);
22568 return instantiation_dependent_expression_p (expression
);
22571 /* All instantiation-dependent expressions should also be considered
22572 value-dependent. */
22573 return instantiation_dependent_scope_ref_p (expression
);
22575 case COMPONENT_REF
:
22576 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
22577 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
22579 case NONTYPE_ARGUMENT_PACK
:
22580 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22581 is value-dependent. */
22583 tree values
= ARGUMENT_PACK_ARGS (expression
);
22584 int i
, len
= TREE_VEC_LENGTH (values
);
22586 for (i
= 0; i
< len
; ++i
)
22587 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
22595 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
22596 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
22597 || (type2
? dependent_type_p (type2
) : false));
22601 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22602 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
22605 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22606 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
22610 tree op
= TREE_OPERAND (expression
, 0);
22611 return (value_dependent_expression_p (op
)
22612 || has_value_dependent_address (op
));
22615 case REQUIRES_EXPR
:
22616 /* Treat all requires-expressions as value-dependent so
22617 we don't try to fold them. */
22621 return dependent_type_p (TREE_OPERAND (expression
, 0));
22625 tree fn
= get_callee_fndecl (expression
);
22627 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
22629 nargs
= call_expr_nargs (expression
);
22630 for (i
= 0; i
< nargs
; ++i
)
22632 tree op
= CALL_EXPR_ARG (expression
, i
);
22633 /* In a call to a constexpr member function, look through the
22634 implicit ADDR_EXPR on the object argument so that it doesn't
22635 cause the call to be considered value-dependent. We also
22636 look through it in potential_constant_expression. */
22637 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
22638 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
22639 && TREE_CODE (op
) == ADDR_EXPR
)
22640 op
= TREE_OPERAND (op
, 0);
22641 if (value_dependent_expression_p (op
))
22647 case TEMPLATE_ID_EXPR
:
22648 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22650 return type_dependent_expression_p (expression
)
22651 || variable_concept_p (TREE_OPERAND (expression
, 0));
22657 if (dependent_type_p (TREE_TYPE (expression
)))
22659 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
22660 if (value_dependent_expression_p (val
))
22666 /* Treat a GNU statement expression as dependent to avoid crashing
22667 under instantiate_non_dependent_expr; it can't be constant. */
22671 /* A constant expression is value-dependent if any subexpression is
22672 value-dependent. */
22673 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
22675 case tcc_reference
:
22677 case tcc_comparison
:
22679 case tcc_expression
:
22682 int i
, len
= cp_tree_operand_length (expression
);
22684 for (i
= 0; i
< len
; i
++)
22686 tree t
= TREE_OPERAND (expression
, i
);
22688 /* In some cases, some of the operands may be missing.l
22689 (For example, in the case of PREDECREMENT_EXPR, the
22690 amount to increment by may be missing.) That doesn't
22691 make the expression dependent. */
22692 if (t
&& value_dependent_expression_p (t
))
22703 /* The expression is not value-dependent. */
22707 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22708 [temp.dep.expr]. Note that an expression with no type is
22709 considered dependent. Other parts of the compiler arrange for an
22710 expression with type-dependent subexpressions to have no type, so
22711 this function doesn't have to be fully recursive. */
22714 type_dependent_expression_p (tree expression
)
22716 if (!processing_template_decl
)
22719 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
22722 /* An unresolved name is always dependent. */
22723 if (identifier_p (expression
)
22724 || TREE_CODE (expression
) == USING_DECL
22725 || TREE_CODE (expression
) == WILDCARD_DECL
)
22728 /* A fold expression is type-dependent. */
22729 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
22730 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
22731 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
22732 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
22735 /* Some expression forms are never type-dependent. */
22736 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
22737 || TREE_CODE (expression
) == SIZEOF_EXPR
22738 || TREE_CODE (expression
) == ALIGNOF_EXPR
22739 || TREE_CODE (expression
) == AT_ENCODE_EXPR
22740 || TREE_CODE (expression
) == NOEXCEPT_EXPR
22741 || TREE_CODE (expression
) == TRAIT_EXPR
22742 || TREE_CODE (expression
) == TYPEID_EXPR
22743 || TREE_CODE (expression
) == DELETE_EXPR
22744 || TREE_CODE (expression
) == VEC_DELETE_EXPR
22745 || TREE_CODE (expression
) == THROW_EXPR
22746 || TREE_CODE (expression
) == REQUIRES_EXPR
)
22749 /* The types of these expressions depends only on the type to which
22750 the cast occurs. */
22751 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
22752 || TREE_CODE (expression
) == STATIC_CAST_EXPR
22753 || TREE_CODE (expression
) == CONST_CAST_EXPR
22754 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
22755 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
22756 || TREE_CODE (expression
) == CAST_EXPR
)
22757 return dependent_type_p (TREE_TYPE (expression
));
22759 /* The types of these expressions depends only on the type created
22760 by the expression. */
22761 if (TREE_CODE (expression
) == NEW_EXPR
22762 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
22764 /* For NEW_EXPR tree nodes created inside a template, either
22765 the object type itself or a TREE_LIST may appear as the
22767 tree type
= TREE_OPERAND (expression
, 1);
22768 if (TREE_CODE (type
) == TREE_LIST
)
22769 /* This is an array type. We need to check array dimensions
22771 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
22772 || value_dependent_expression_p
22773 (TREE_OPERAND (TREE_VALUE (type
), 1));
22775 return dependent_type_p (type
);
22778 if (TREE_CODE (expression
) == SCOPE_REF
)
22780 tree scope
= TREE_OPERAND (expression
, 0);
22781 tree name
= TREE_OPERAND (expression
, 1);
22783 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22784 contains an identifier associated by name lookup with one or more
22785 declarations declared with a dependent type, or...a
22786 nested-name-specifier or qualified-id that names a member of an
22787 unknown specialization. */
22788 return (type_dependent_expression_p (name
)
22789 || dependent_scope_p (scope
));
22792 /* A function template specialization is type-dependent if it has any
22793 dependent template arguments. */
22794 if (TREE_CODE (expression
) == FUNCTION_DECL
22795 && DECL_LANG_SPECIFIC (expression
)
22796 && DECL_TEMPLATE_INFO (expression
))
22797 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
22799 if (TREE_CODE (expression
) == TEMPLATE_DECL
22800 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
22803 if (TREE_CODE (expression
) == STMT_EXPR
)
22804 expression
= stmt_expr_value_expr (expression
);
22806 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
22811 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
22813 if (type_dependent_expression_p (elt
))
22819 /* A static data member of the current instantiation with incomplete
22820 array type is type-dependent, as the definition and specializations
22821 can have different bounds. */
22822 if (VAR_P (expression
)
22823 && DECL_CLASS_SCOPE_P (expression
)
22824 && dependent_type_p (DECL_CONTEXT (expression
))
22825 && VAR_HAD_UNKNOWN_BOUND (expression
))
22828 /* An array of unknown bound depending on a variadic parameter, eg:
22830 template<typename... Args>
22831 void foo (Args... args)
22833 int arr[] = { args... };
22836 template<int... vals>
22839 int arr[] = { vals... };
22842 If the array has no length and has an initializer, it must be that
22843 we couldn't determine its length in cp_complete_array_type because
22844 it is dependent. */
22845 if (VAR_P (expression
)
22846 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
22847 && !TYPE_DOMAIN (TREE_TYPE (expression
))
22848 && DECL_INITIAL (expression
))
22851 /* A variable template specialization is type-dependent if it has any
22852 dependent template arguments. */
22853 if (VAR_P (expression
)
22854 && DECL_LANG_SPECIFIC (expression
)
22855 && DECL_TEMPLATE_INFO (expression
)
22856 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
22857 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
22859 /* Always dependent, on the number of arguments if nothing else. */
22860 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
22863 if (TREE_TYPE (expression
) == unknown_type_node
)
22865 if (TREE_CODE (expression
) == ADDR_EXPR
)
22866 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
22867 if (TREE_CODE (expression
) == COMPONENT_REF
22868 || TREE_CODE (expression
) == OFFSET_REF
)
22870 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22872 expression
= TREE_OPERAND (expression
, 1);
22873 if (identifier_p (expression
))
22876 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22877 if (TREE_CODE (expression
) == SCOPE_REF
)
22880 if (BASELINK_P (expression
))
22882 if (BASELINK_OPTYPE (expression
)
22883 && dependent_type_p (BASELINK_OPTYPE (expression
)))
22885 expression
= BASELINK_FUNCTIONS (expression
);
22888 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
22890 if (any_dependent_template_arguments_p
22891 (TREE_OPERAND (expression
, 1)))
22893 expression
= TREE_OPERAND (expression
, 0);
22894 if (identifier_p (expression
))
22898 gcc_assert (TREE_CODE (expression
) == OVERLOAD
22899 || TREE_CODE (expression
) == FUNCTION_DECL
);
22903 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
22905 expression
= OVL_NEXT (expression
);
22910 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
22912 return (dependent_type_p (TREE_TYPE (expression
)));
22915 /* walk_tree callback function for instantiation_dependent_expression_p,
22916 below. Returns non-zero if a dependent subexpression is found. */
22919 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
22924 /* We don't have to worry about decltype currently because decltype
22925 of an instantiation-dependent expr is a dependent type. This
22926 might change depending on the resolution of DR 1172. */
22927 *walk_subtrees
= false;
22930 enum tree_code code
= TREE_CODE (*tp
);
22933 /* Don't treat an argument list as dependent just because it has no
22941 /* A constant with a dependent initializer is dependent. */
22942 if (value_dependent_expression_p (*tp
))
22946 case TEMPLATE_PARM_INDEX
:
22949 /* Handle expressions with type operands. */
22953 case AT_ENCODE_EXPR
:
22955 tree op
= TREE_OPERAND (*tp
, 0);
22956 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
22957 op
= TREE_TYPE (op
);
22960 if (dependent_type_p (op
))
22964 *walk_subtrees
= false;
22972 if (value_dependent_expression_p (*tp
))
22974 *walk_subtrees
= false;
22977 case COMPONENT_REF
:
22978 if (identifier_p (TREE_OPERAND (*tp
, 1)))
22979 /* In a template, finish_class_member_access_expr creates a
22980 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22981 type-dependent, so that we can check access control at
22982 instantiation time (PR 42277). See also Core issue 1273. */
22987 if (instantiation_dependent_scope_ref_p (*tp
))
22992 /* Treat statement-expressions as dependent. */
22996 /* Treat requires-expressions as dependent. */
22997 case REQUIRES_EXPR
:
23001 /* Treat calls to function concepts as dependent. */
23002 if (function_concept_check_p (*tp
))
23006 case TEMPLATE_ID_EXPR
:
23007 /* And variable concepts. */
23008 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
23016 if (type_dependent_expression_p (*tp
))
23022 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23023 sense defined by the ABI:
23025 "An expression is instantiation-dependent if it is type-dependent
23026 or value-dependent, or it has a subexpression that is type-dependent
23027 or value-dependent." */
23030 instantiation_dependent_expression_p (tree expression
)
23034 if (!processing_template_decl
)
23037 if (expression
== error_mark_node
)
23040 result
= cp_walk_tree_without_duplicates (&expression
,
23041 instantiation_dependent_r
, NULL
);
23042 return result
!= NULL_TREE
;
23045 /* Like type_dependent_expression_p, but it also works while not processing
23046 a template definition, i.e. during substitution or mangling. */
23049 type_dependent_expression_p_push (tree expr
)
23052 ++processing_template_decl
;
23053 b
= type_dependent_expression_p (expr
);
23054 --processing_template_decl
;
23058 /* Returns TRUE if ARGS contains a type-dependent expression. */
23061 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
23066 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
23068 if (type_dependent_expression_p (arg
))
23074 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23075 expressions) contains any type-dependent expressions. */
23078 any_type_dependent_elements_p (const_tree list
)
23080 for (; list
; list
= TREE_CHAIN (list
))
23081 if (type_dependent_expression_p (TREE_VALUE (list
)))
23087 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23088 expressions) contains any value-dependent expressions. */
23091 any_value_dependent_elements_p (const_tree list
)
23093 for (; list
; list
= TREE_CHAIN (list
))
23094 if (value_dependent_expression_p (TREE_VALUE (list
)))
23100 /* Returns TRUE if the ARG (a template argument) is dependent. */
23103 dependent_template_arg_p (tree arg
)
23105 if (!processing_template_decl
)
23108 /* Assume a template argument that was wrongly written by the user
23109 is dependent. This is consistent with what
23110 any_dependent_template_arguments_p [that calls this function]
23112 if (!arg
|| arg
== error_mark_node
)
23115 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
23116 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
23118 if (TREE_CODE (arg
) == TEMPLATE_DECL
23119 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
23120 return dependent_template_p (arg
);
23121 else if (ARGUMENT_PACK_P (arg
))
23123 tree args
= ARGUMENT_PACK_ARGS (arg
);
23124 int i
, len
= TREE_VEC_LENGTH (args
);
23125 for (i
= 0; i
< len
; ++i
)
23127 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23133 else if (TYPE_P (arg
))
23134 return dependent_type_p (arg
);
23136 return (type_dependent_expression_p (arg
)
23137 || value_dependent_expression_p (arg
));
23140 /* Returns true if ARGS (a collection of template arguments) contains
23141 any types that require structural equality testing. */
23144 any_template_arguments_need_structural_equality_p (tree args
)
23151 if (args
== error_mark_node
)
23154 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23156 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23157 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23159 tree arg
= TREE_VEC_ELT (level
, j
);
23160 tree packed_args
= NULL_TREE
;
23163 if (ARGUMENT_PACK_P (arg
))
23165 /* Look inside the argument pack. */
23166 packed_args
= ARGUMENT_PACK_ARGS (arg
);
23167 len
= TREE_VEC_LENGTH (packed_args
);
23170 for (k
= 0; k
< len
; ++k
)
23173 arg
= TREE_VEC_ELT (packed_args
, k
);
23175 if (error_operand_p (arg
))
23177 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23179 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
23181 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
23182 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
23191 /* Returns true if ARGS (a collection of template arguments) contains
23192 any dependent arguments. */
23195 any_dependent_template_arguments_p (const_tree args
)
23202 if (args
== error_mark_node
)
23205 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23207 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23208 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23209 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
23216 /* Returns TRUE if the template TMPL is dependent. */
23219 dependent_template_p (tree tmpl
)
23221 if (TREE_CODE (tmpl
) == OVERLOAD
)
23225 if (dependent_template_p (OVL_CURRENT (tmpl
)))
23227 tmpl
= OVL_NEXT (tmpl
);
23232 /* Template template parameters are dependent. */
23233 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
23234 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
23236 /* So are names that have not been looked up. */
23237 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
23239 /* So are member templates of dependent classes. */
23240 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
23241 return dependent_type_p (DECL_CONTEXT (tmpl
));
23245 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23248 dependent_template_id_p (tree tmpl
, tree args
)
23250 return (dependent_template_p (tmpl
)
23251 || any_dependent_template_arguments_p (args
));
23254 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23258 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
23262 if (!processing_template_decl
)
23265 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
23267 tree decl
= TREE_VEC_ELT (declv
, i
);
23268 tree init
= TREE_VEC_ELT (initv
, i
);
23269 tree cond
= TREE_VEC_ELT (condv
, i
);
23270 tree incr
= TREE_VEC_ELT (incrv
, i
);
23272 if (type_dependent_expression_p (decl
)
23273 || TREE_CODE (decl
) == SCOPE_REF
)
23276 if (init
&& type_dependent_expression_p (init
))
23279 if (type_dependent_expression_p (cond
))
23282 if (COMPARISON_CLASS_P (cond
)
23283 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
23284 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
23287 if (TREE_CODE (incr
) == MODOP_EXPR
)
23289 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
23290 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
23293 else if (type_dependent_expression_p (incr
))
23295 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
23297 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
23299 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
23301 tree t
= TREE_OPERAND (incr
, 1);
23302 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
23303 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
23312 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23313 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23314 no such TYPE can be found. Note that this function peers inside
23315 uninstantiated templates and therefore should be used only in
23316 extremely limited situations. ONLY_CURRENT_P restricts this
23317 peering to the currently open classes hierarchy (which is required
23318 when comparing types). */
23321 resolve_typename_type (tree type
, bool only_current_p
)
23330 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
23332 scope
= TYPE_CONTEXT (type
);
23333 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23334 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23335 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23336 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23337 identifier of the TYPENAME_TYPE anymore.
23338 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23339 TYPENAME_TYPE instead, we avoid messing up with a possible
23340 typedef variant case. */
23341 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
23343 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23344 it first before we can figure out what NAME refers to. */
23345 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23347 if (TYPENAME_IS_RESOLVING_P (scope
))
23348 /* Given a class template A with a dependent base with nested type C,
23349 typedef typename A::C::C C will land us here, as trying to resolve
23350 the initial A::C leads to the local C typedef, which leads back to
23351 A::C::C. So we break the recursion now. */
23354 scope
= resolve_typename_type (scope
, only_current_p
);
23356 /* If we don't know what SCOPE refers to, then we cannot resolve the
23358 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23360 /* If the SCOPE is a template type parameter, we have no way of
23361 resolving the name. */
23362 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
23364 /* If the SCOPE is not the current instantiation, there's no reason
23365 to look inside it. */
23366 if (only_current_p
&& !currently_open_class (scope
))
23368 /* If this is a typedef, we don't want to look inside (c++/11987). */
23369 if (typedef_variant_p (type
))
23371 /* If SCOPE isn't the template itself, it will not have a valid
23372 TYPE_FIELDS list. */
23373 if (CLASS_TYPE_P (scope
)
23374 && same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
23375 /* scope is either the template itself or a compatible instantiation
23376 like X<T>, so look up the name in the original template. */
23377 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
23379 /* scope is a partial instantiation, so we can't do the lookup or we
23380 will lose the template arguments. */
23382 /* Enter the SCOPE so that name lookup will be resolved as if we
23383 were in the class definition. In particular, SCOPE will no
23384 longer be considered a dependent type. */
23385 pushed_scope
= push_scope (scope
);
23386 /* Look up the declaration. */
23387 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
23388 tf_warning_or_error
);
23390 result
= NULL_TREE
;
23392 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23393 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23396 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
23397 && TREE_CODE (decl
) == TYPE_DECL
)
23399 result
= TREE_TYPE (decl
);
23400 if (result
== error_mark_node
)
23401 result
= NULL_TREE
;
23403 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
23404 && DECL_CLASS_TEMPLATE_P (decl
))
23408 /* Obtain the template and the arguments. */
23409 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
23410 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
23411 /* Instantiate the template. */
23412 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
23413 /*entering_scope=*/0,
23414 tf_error
| tf_user
);
23415 if (result
== error_mark_node
)
23416 result
= NULL_TREE
;
23419 /* Leave the SCOPE. */
23421 pop_scope (pushed_scope
);
23423 /* If we failed to resolve it, return the original typename. */
23427 /* If lookup found a typename type, resolve that too. */
23428 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
23430 /* Ill-formed programs can cause infinite recursion here, so we
23431 must catch that. */
23432 TYPENAME_IS_RESOLVING_P (type
) = 1;
23433 result
= resolve_typename_type (result
, only_current_p
);
23434 TYPENAME_IS_RESOLVING_P (type
) = 0;
23437 /* Qualify the resulting type. */
23438 quals
= cp_type_quals (type
);
23440 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
23445 /* EXPR is an expression which is not type-dependent. Return a proxy
23446 for EXPR that can be used to compute the types of larger
23447 expressions containing EXPR. */
23450 build_non_dependent_expr (tree expr
)
23454 /* When checking, try to get a constant value for all non-dependent
23455 expressions in order to expose bugs in *_dependent_expression_p
23457 if (flag_checking
&& cxx_dialect
>= cxx11
23458 /* Don't do this during nsdmi parsing as it can lead to
23459 unexpected recursive instantiations. */
23460 && !parsing_nsdmi ())
23461 fold_non_dependent_expr (expr
);
23463 /* Preserve OVERLOADs; the functions must be available to resolve
23466 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
23467 inner_expr
= stmt_expr_value_expr (inner_expr
);
23468 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
23469 inner_expr
= TREE_OPERAND (inner_expr
, 0);
23470 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
23471 inner_expr
= TREE_OPERAND (inner_expr
, 1);
23472 if (is_overloaded_fn (inner_expr
)
23473 || TREE_CODE (inner_expr
) == OFFSET_REF
)
23475 /* There is no need to return a proxy for a variable. */
23478 /* Preserve string constants; conversions from string constants to
23479 "char *" are allowed, even though normally a "const char *"
23480 cannot be used to initialize a "char *". */
23481 if (TREE_CODE (expr
) == STRING_CST
)
23483 /* Preserve void and arithmetic constants, as an optimization -- there is no
23484 reason to create a new node. */
23485 if (TREE_CODE (expr
) == VOID_CST
23486 || TREE_CODE (expr
) == INTEGER_CST
23487 || TREE_CODE (expr
) == REAL_CST
)
23489 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23490 There is at least one place where we want to know that a
23491 particular expression is a throw-expression: when checking a ?:
23492 expression, there are special rules if the second or third
23493 argument is a throw-expression. */
23494 if (TREE_CODE (expr
) == THROW_EXPR
)
23497 /* Don't wrap an initializer list, we need to be able to look inside. */
23498 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
23501 /* Don't wrap a dummy object, we need to be able to test for it. */
23502 if (is_dummy_object (expr
))
23505 if (TREE_CODE (expr
) == COND_EXPR
)
23506 return build3 (COND_EXPR
,
23508 TREE_OPERAND (expr
, 0),
23509 (TREE_OPERAND (expr
, 1)
23510 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
23511 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
23512 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
23513 if (TREE_CODE (expr
) == COMPOUND_EXPR
23514 && !COMPOUND_EXPR_OVERLOADED (expr
))
23515 return build2 (COMPOUND_EXPR
,
23517 TREE_OPERAND (expr
, 0),
23518 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
23520 /* If the type is unknown, it can't really be non-dependent */
23521 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
23523 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23524 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
23527 /* ARGS is a vector of expressions as arguments to a function call.
23528 Replace the arguments with equivalent non-dependent expressions.
23529 This modifies ARGS in place. */
23532 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
23537 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
23539 tree newarg
= build_non_dependent_expr (arg
);
23541 (*args
)[ix
] = newarg
;
23545 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23546 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23547 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23550 make_auto_1 (tree name
, bool set_canonical
)
23552 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
23553 TYPE_NAME (au
) = build_decl (input_location
,
23554 TYPE_DECL
, name
, au
);
23555 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
23556 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
23557 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
23558 TYPE_NAME (au
), NULL_TREE
);
23560 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
23561 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
23562 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
23568 make_decltype_auto (void)
23570 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23576 return make_auto_1 (get_identifier ("auto"), true);
23579 /* Make a "constrained auto" type-specifier. This is an
23580 auto type with constraints that must be associated after
23581 deduction. The constraint is formed from the given
23582 CONC and its optional sequence of arguments, which are
23583 non-null if written as partial-concept-id. */
23586 make_constrained_auto (tree con
, tree args
)
23588 tree type
= make_auto_1 (get_identifier ("auto"), false);
23590 /* Build the constraint. */
23591 tree tmpl
= DECL_TI_TEMPLATE (con
);
23594 expr
= build_concept_check (tmpl
, type
, args
);
23596 expr
= build_concept_check (build_overload (tmpl
, NULL_TREE
), type
, args
);
23598 tree constr
= make_predicate_constraint (expr
);
23599 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
23601 /* Our canonical type depends on the constraint. */
23602 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
23604 /* Attach the constraint to the type declaration. */
23605 tree decl
= TYPE_NAME (type
);
23609 /* Given type ARG, return std::initializer_list<ARG>. */
23614 tree std_init_list
= namespace_binding
23615 (get_identifier ("initializer_list"), std_node
);
23617 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
23619 error ("deducing from brace-enclosed initializer list requires "
23620 "#include <initializer_list>");
23621 return error_mark_node
;
23623 argvec
= make_tree_vec (1);
23624 TREE_VEC_ELT (argvec
, 0) = arg
;
23625 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
23626 NULL_TREE
, 0, tf_warning_or_error
);
23629 /* Replace auto in TYPE with std::initializer_list<auto>. */
23632 listify_autos (tree type
, tree auto_node
)
23634 tree init_auto
= listify (auto_node
);
23635 tree argvec
= make_tree_vec (1);
23636 TREE_VEC_ELT (argvec
, 0) = init_auto
;
23637 if (processing_template_decl
)
23638 argvec
= add_to_template_args (current_template_args (), argvec
);
23639 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
23642 /* Hash traits for hashing possibly constrained 'auto'
23643 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23645 struct auto_hash
: default_hash_traits
<tree
>
23647 static inline hashval_t
hash (tree
);
23648 static inline bool equal (tree
, tree
);
23651 /* Hash the 'auto' T. */
23654 auto_hash::hash (tree t
)
23656 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
23657 /* Matching constrained-type-specifiers denote the same template
23658 parameter, so hash the constraint. */
23659 return hash_placeholder_constraint (c
);
23661 /* But unconstrained autos are all separate, so just hash the pointer. */
23662 return iterative_hash_object (t
, 0);
23665 /* Compare two 'auto's. */
23668 auto_hash::equal (tree t1
, tree t2
)
23673 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
23674 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
23676 /* Two unconstrained autos are distinct. */
23680 return equivalent_placeholder_constraints (c1
, c2
);
23683 /* for_each_template_parm callback for extract_autos: if t is a (possibly
23684 constrained) auto, add it to the vector. */
23687 extract_autos_r (tree t
, void *data
)
23689 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
23690 if (is_auto_or_concept (t
))
23692 /* All the autos were built with index 0; fix that up now. */
23693 tree
*p
= hash
.find_slot (t
, INSERT
);
23696 /* If this is a repeated constrained-type-specifier, use the index we
23698 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
23701 /* Otherwise this is new, so use the current count. */
23703 idx
= hash
.elements () - 1;
23705 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
23708 /* Always keep walking. */
23712 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
23713 says they can appear anywhere in the type. */
23716 extract_autos (tree type
)
23718 hash_set
<tree
> visited
;
23719 hash_table
<auto_hash
> hash (2);
23721 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
23723 tree tree_vec
= make_tree_vec (hash
.elements());
23724 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
23725 iter
!= hash
.end(); ++iter
)
23728 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
23729 TREE_VEC_ELT (tree_vec
, i
)
23730 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
23736 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23737 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23740 do_auto_deduction (tree type
, tree init
, tree auto_node
)
23742 return do_auto_deduction (type
, init
, auto_node
,
23743 tf_warning_or_error
,
23747 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23748 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23749 The CONTEXT determines the context in which auto deduction is performed
23750 and is used to control error diagnostics. */
23753 do_auto_deduction (tree type
, tree init
, tree auto_node
,
23754 tsubst_flags_t complain
, auto_deduction_context context
)
23758 if (init
== error_mark_node
)
23759 return error_mark_node
;
23761 if (type_dependent_expression_p (init
))
23762 /* Defining a subset of type-dependent expressions that we can deduce
23763 from ahead of time isn't worth the trouble. */
23766 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23767 with either a new invented type template parameter U or, if the
23768 initializer is a braced-init-list (8.5.4), with
23769 std::initializer_list<U>. */
23770 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
23772 if (!DIRECT_LIST_INIT_P (init
))
23773 type
= listify_autos (type
, auto_node
);
23774 else if (CONSTRUCTOR_NELTS (init
) == 1)
23775 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
23778 if (complain
& tf_warning_or_error
)
23780 if (permerror (input_location
, "direct-list-initialization of "
23781 "%<auto%> requires exactly one element"))
23782 inform (input_location
,
23783 "for deduction to %<std::initializer_list%>, use copy-"
23784 "list-initialization (i.e. add %<=%> before the %<{%>)");
23786 type
= listify_autos (type
, auto_node
);
23790 if (type
== error_mark_node
)
23791 return error_mark_node
;
23793 init
= resolve_nondeduced_context (init
);
23795 if (AUTO_IS_DECLTYPE (auto_node
))
23797 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
23798 && !REF_PARENTHESIZED_P (init
)));
23799 targs
= make_tree_vec (1);
23800 TREE_VEC_ELT (targs
, 0)
23801 = finish_decltype_type (init
, id
, tf_warning_or_error
);
23802 if (type
!= auto_node
)
23804 if (complain
& tf_error
)
23805 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
23806 return error_mark_node
;
23811 tree parms
= build_tree_list (NULL_TREE
, type
);
23815 tparms
= extract_autos (type
);
23818 tparms
= make_tree_vec (1);
23819 TREE_VEC_ELT (tparms
, 0)
23820 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
23823 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
23824 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
23825 DEDUCE_CALL
, LOOKUP_NORMAL
,
23826 NULL
, /*explain_p=*/false);
23829 if (processing_template_decl
)
23830 /* Try again at instantiation time. */
23832 if (type
&& type
!= error_mark_node
23833 && (complain
& tf_error
))
23834 /* If type is error_mark_node a diagnostic must have been
23835 emitted by now. Also, having a mention to '<type error>'
23836 in the diagnostic is not really useful to the user. */
23838 if (cfun
&& auto_node
== current_function_auto_return_pattern
23839 && LAMBDA_FUNCTION_P (current_function_decl
))
23840 error ("unable to deduce lambda return type from %qE", init
);
23842 error ("unable to deduce %qT from %qE", type
, init
);
23843 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
23844 DEDUCE_CALL
, LOOKUP_NORMAL
,
23845 NULL
, /*explain_p=*/true);
23847 return error_mark_node
;
23851 /* Check any placeholder constraints against the deduced type. */
23852 if (flag_concepts
&& !processing_template_decl
)
23853 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
23855 /* Use the deduced type to check the associated constraints. */
23856 if (!constraints_satisfied_p (constr
, targs
))
23858 if (complain
& tf_warning_or_error
)
23862 case adc_unspecified
:
23863 error("placeholder constraints not satisfied");
23865 case adc_variable_type
:
23866 error ("deduced initializer does not satisfy "
23867 "placeholder constraints");
23869 case adc_return_type
:
23870 error ("deduced return type does not satisfy "
23871 "placeholder constraints");
23873 case adc_requirement
:
23874 error ("deduced expression type does not saatisy "
23875 "placeholder constraints");
23878 diagnose_constraints (input_location
, constr
, targs
);
23880 return error_mark_node
;
23884 if (processing_template_decl
)
23885 targs
= add_to_template_args (current_template_args (), targs
);
23886 return tsubst (type
, targs
, complain
, NULL_TREE
);
23889 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23893 splice_late_return_type (tree type
, tree late_return_type
)
23895 if (is_auto (type
))
23897 if (late_return_type
)
23898 return late_return_type
;
23900 tree idx
= get_template_parm_index (type
);
23901 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
23902 /* In an abbreviated function template we didn't know we were dealing
23903 with a function template when we saw the auto return type, so update
23904 it to have the correct level. */
23905 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
23910 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23911 'decltype(auto)'. */
23914 is_auto (const_tree type
)
23916 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23917 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
23918 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
23924 /* for_each_template_parm callback for type_uses_auto. */
23927 is_auto_r (tree tp
, void */
*data*/
)
23929 return is_auto_or_concept (tp
);
23932 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23933 a use of `auto'. Returns NULL_TREE otherwise. */
23936 type_uses_auto (tree type
)
23938 if (type
== NULL_TREE
)
23940 else if (flag_concepts
)
23942 /* The Concepts TS allows multiple autos in one type-specifier; just
23943 return the first one we find, do_auto_deduction will collect all of
23945 if (uses_template_parms (type
))
23946 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
23947 /*visited*/NULL
, /*nondeduced*/true);
23952 return find_type_usage (type
, is_auto
);
23955 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23956 'decltype(auto)' or a concept. */
23959 is_auto_or_concept (const_tree type
)
23961 return is_auto (type
); // or concept
23964 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23965 a concept identifier) iff TYPE contains a use of a generic type. Returns
23966 NULL_TREE otherwise. */
23969 type_uses_auto_or_concept (tree type
)
23971 return find_type_usage (type
, is_auto_or_concept
);
23975 /* For a given template T, return the vector of typedefs referenced
23976 in T for which access check is needed at T instantiation time.
23977 T is either a FUNCTION_DECL or a RECORD_TYPE.
23978 Those typedefs were added to T by the function
23979 append_type_to_template_for_access_check. */
23981 vec
<qualified_typedef_usage_t
, va_gc
> *
23982 get_types_needing_access_check (tree t
)
23985 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
23987 if (!t
|| t
== error_mark_node
)
23990 if (!(ti
= get_template_info (t
)))
23993 if (CLASS_TYPE_P (t
)
23994 || TREE_CODE (t
) == FUNCTION_DECL
)
23996 if (!TI_TEMPLATE (ti
))
23999 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
24005 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
24006 tied to T. That list of typedefs will be access checked at
24007 T instantiation time.
24008 T is either a FUNCTION_DECL or a RECORD_TYPE.
24009 TYPE_DECL is a TYPE_DECL node representing a typedef.
24010 SCOPE is the scope through which TYPE_DECL is accessed.
24011 LOCATION is the location of the usage point of TYPE_DECL.
24013 This function is a subroutine of
24014 append_type_to_template_for_access_check. */
24017 append_type_to_template_for_access_check_1 (tree t
,
24020 location_t location
)
24022 qualified_typedef_usage_t typedef_usage
;
24025 if (!t
|| t
== error_mark_node
)
24028 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
24029 || CLASS_TYPE_P (t
))
24031 && TREE_CODE (type_decl
) == TYPE_DECL
24034 if (!(ti
= get_template_info (t
)))
24037 gcc_assert (TI_TEMPLATE (ti
));
24039 typedef_usage
.typedef_decl
= type_decl
;
24040 typedef_usage
.context
= scope
;
24041 typedef_usage
.locus
= location
;
24043 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
24046 /* Append TYPE_DECL to the template TEMPL.
24047 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24048 At TEMPL instanciation time, TYPE_DECL will be checked to see
24049 if it can be accessed through SCOPE.
24050 LOCATION is the location of the usage point of TYPE_DECL.
24052 e.g. consider the following code snippet:
24059 template<class U> struct S
24061 C::myint mi; // <-- usage point of the typedef C::myint
24066 At S<char> instantiation time, we need to check the access of C::myint
24067 In other words, we need to check the access of the myint typedef through
24068 the C scope. For that purpose, this function will add the myint typedef
24069 and the scope C through which its being accessed to a list of typedefs
24070 tied to the template S. That list will be walked at template instantiation
24071 time and access check performed on each typedefs it contains.
24072 Note that this particular code snippet should yield an error because
24073 myint is private to C. */
24076 append_type_to_template_for_access_check (tree templ
,
24079 location_t location
)
24081 qualified_typedef_usage_t
*iter
;
24084 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
24086 /* Make sure we don't append the type to the template twice. */
24087 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
24088 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
24091 append_type_to_template_for_access_check_1 (templ
, type_decl
,
24095 /* Convert the generic type parameters in PARM that match the types given in the
24096 range [START_IDX, END_IDX) from the current_template_parms into generic type
24100 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
24102 tree current
= current_template_parms
;
24103 int depth
= TMPL_PARMS_DEPTH (current
);
24104 current
= INNERMOST_TEMPLATE_PARMS (current
);
24105 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
24107 for (int i
= 0; i
< start_idx
; ++i
)
24108 TREE_VEC_ELT (replacement
, i
)
24109 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24111 for (int i
= start_idx
; i
< end_idx
; ++i
)
24113 /* Create a distinct parameter pack type from the current parm and add it
24114 to the replacement args to tsubst below into the generic function
24117 tree o
= TREE_TYPE (TREE_VALUE
24118 (TREE_VEC_ELT (current
, i
)));
24119 tree t
= copy_type (o
);
24120 TEMPLATE_TYPE_PARM_INDEX (t
)
24121 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
24123 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
24124 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
24125 TYPE_MAIN_VARIANT (t
) = t
;
24126 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
24127 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
24128 TREE_VEC_ELT (replacement
, i
) = t
;
24129 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
24132 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
24133 TREE_VEC_ELT (replacement
, i
)
24134 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24136 /* If there are more levels then build up the replacement with the outer
24139 replacement
= add_to_template_args (template_parms_to_args
24140 (TREE_CHAIN (current_template_parms
)),
24143 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
24146 /* Entries in the decl_constraint hash table. */
24147 struct GTY((for_user
)) constr_entry
24153 /* Hashing function and equality for constraint entries. */
24154 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
24156 static hashval_t
hash (constr_entry
*e
)
24158 return (hashval_t
)DECL_UID (e
->decl
);
24161 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
24163 return e1
->decl
== e2
->decl
;
24167 /* A mapping from declarations to constraint information. Note that
24168 both templates and their underlying declarations are mapped to the
24169 same constraint information.
24171 FIXME: This is defined in pt.c because garbage collection
24172 code is not being generated for constraint.cc. */
24174 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
24176 /* Returns true iff cinfo contains a valid set of constraints.
24177 This is the case when the associated requirements have been
24178 successfully decomposed into lists of atomic constraints.
24179 That is, when the saved assumptions are not error_mark_node. */
24182 valid_constraints_p (tree cinfo
)
24184 gcc_assert (cinfo
);
24185 return CI_ASSUMPTIONS (cinfo
) != error_mark_node
;
24188 /* Returns the template constraints of declaration T. If T is not
24189 constrained, return NULL_TREE. Note that T must be non-null. */
24192 get_constraints (tree t
)
24194 gcc_assert (DECL_P (t
));
24195 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24196 t
= DECL_TEMPLATE_RESULT (t
);
24197 constr_entry elt
= { t
, NULL_TREE
};
24198 constr_entry
* found
= decl_constraints
->find (&elt
);
24205 /* Associate the given constraint information CI with the declaration
24206 T. If T is a template, then the constraints are associated with
24207 its underlying declaration. Don't build associations if CI is
24211 set_constraints (tree t
, tree ci
)
24216 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24217 t
= DECL_TEMPLATE_RESULT (t
);
24218 gcc_assert (!get_constraints (t
));
24219 constr_entry elt
= {t
, ci
};
24220 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
24221 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
24226 /* Remove the associated constraints of the declaration T. */
24229 remove_constraints (tree t
)
24231 gcc_assert (DECL_P (t
));
24232 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24233 t
= DECL_TEMPLATE_RESULT (t
);
24235 constr_entry elt
= {t
, NULL_TREE
};
24236 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
24238 decl_constraints
->clear_slot (slot
);
24241 /* Set up the hash table for constraint association. */
24244 init_constraint_processing (void)
24246 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
24249 /* Set up the hash tables for template instantiations. */
24252 init_template_processing (void)
24254 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24255 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24258 /* Print stats about the template hash tables for -fstats. */
24261 print_template_statistics (void)
24263 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
24264 "%f collisions\n", (long) decl_specializations
->size (),
24265 (long) decl_specializations
->elements (),
24266 decl_specializations
->collisions ());
24267 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
24268 "%f collisions\n", (long) type_specializations
->size (),
24269 (long) type_specializations
->elements (),
24270 type_specializations
->collisions ());
24273 #include "gt-cp-pt.h"