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
)
14013 len
= TREE_VEC_LENGTH (expanded
);
14015 if (expanded
== error_mark_node
)
14016 return error_mark_node
;
14017 else if (PACK_EXPANSION_P (expanded
)
14018 || (TREE_CODE (expanded
) == TREE_VEC
14020 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
14022 if (TREE_CODE (expanded
) == TREE_VEC
)
14023 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
14025 PACK_EXPANSION_SIZEOF_P (expanded
) = true;
14027 if (TYPE_P (expanded
))
14028 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14029 complain
& tf_error
);
14031 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14032 complain
& tf_error
);
14035 return build_int_cst (size_type_node
, len
);
14037 if (SIZEOF_EXPR_TYPE_P (t
))
14039 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14040 args
, complain
, in_decl
);
14041 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14042 r
= build1 (SIZEOF_EXPR
,
14043 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14044 SIZEOF_EXPR_TYPE_P (r
) = 1;
14051 case TRUTH_NOT_EXPR
:
14054 case UNARY_PLUS_EXPR
: /* Unary + */
14056 case AT_ENCODE_EXPR
:
14060 case REALPART_EXPR
:
14061 case IMAGPART_EXPR
:
14064 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14065 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14066 return build1 (code
, type
, op0
);
14069 case COMPONENT_REF
:
14074 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14075 name
= TREE_OPERAND (t
, 1);
14076 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14078 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14079 complain
, in_decl
);
14080 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14082 else if (TREE_CODE (name
) == SCOPE_REF
14083 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14085 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14086 complain
, in_decl
);
14087 name
= TREE_OPERAND (name
, 1);
14088 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14089 complain
, in_decl
);
14090 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14091 name
= build_qualified_name (/*type=*/NULL_TREE
,
14093 /*template_p=*/false);
14095 else if (BASELINK_P (name
))
14096 name
= tsubst_baselink (name
,
14097 non_reference (TREE_TYPE (object
)),
14101 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14102 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14108 case TRUNC_DIV_EXPR
:
14109 case CEIL_DIV_EXPR
:
14110 case FLOOR_DIV_EXPR
:
14111 case ROUND_DIV_EXPR
:
14112 case EXACT_DIV_EXPR
:
14116 case TRUNC_MOD_EXPR
:
14117 case FLOOR_MOD_EXPR
:
14118 case TRUTH_ANDIF_EXPR
:
14119 case TRUTH_ORIF_EXPR
:
14120 case TRUTH_AND_EXPR
:
14121 case TRUTH_OR_EXPR
:
14134 case COMPOUND_EXPR
:
14137 case PREDECREMENT_EXPR
:
14138 case PREINCREMENT_EXPR
:
14139 case POSTDECREMENT_EXPR
:
14140 case POSTINCREMENT_EXPR
:
14142 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14143 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14144 return build_nt (code
, op0
, op1
);
14149 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14150 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14151 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14152 QUALIFIED_NAME_IS_TEMPLATE (t
));
14157 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14158 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14159 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14164 int n
= VL_EXP_OPERAND_LENGTH (t
);
14165 tree result
= build_vl_exp (CALL_EXPR
, n
);
14167 for (i
= 0; i
< n
; i
++)
14168 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14169 complain
, in_decl
);
14175 case PSEUDO_DTOR_EXPR
:
14176 case VEC_PERM_EXPR
:
14178 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14179 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14180 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14181 r
= build_nt (code
, op0
, op1
, op2
);
14182 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14188 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14189 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14190 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14191 r
= build_nt (code
, op0
, op1
, op2
);
14192 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14198 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14199 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14200 r
= build_nt (code
, op0
, op1
);
14201 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14202 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14206 case TEMPLATE_ID_EXPR
:
14208 /* Substituted template arguments */
14209 tree fn
= TREE_OPERAND (t
, 0);
14210 tree targs
= TREE_OPERAND (t
, 1);
14212 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14214 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14216 return lookup_template_function (fn
, targs
);
14221 tree purpose
, value
, chain
;
14223 if (t
== void_list_node
)
14226 purpose
= TREE_PURPOSE (t
);
14228 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
14229 value
= TREE_VALUE (t
);
14231 value
= tsubst_copy (value
, args
, complain
, in_decl
);
14232 chain
= TREE_CHAIN (t
);
14233 if (chain
&& chain
!= void_type_node
)
14234 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
14235 if (purpose
== TREE_PURPOSE (t
)
14236 && value
== TREE_VALUE (t
)
14237 && chain
== TREE_CHAIN (t
))
14239 return tree_cons (purpose
, value
, chain
);
14244 case ENUMERAL_TYPE
:
14246 case TEMPLATE_TYPE_PARM
:
14247 case TEMPLATE_TEMPLATE_PARM
:
14248 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14249 case TEMPLATE_PARM_INDEX
:
14251 case REFERENCE_TYPE
:
14253 case FUNCTION_TYPE
:
14256 case TYPENAME_TYPE
:
14257 case UNBOUND_CLASS_TEMPLATE
:
14259 case DECLTYPE_TYPE
:
14261 return tsubst (t
, args
, complain
, in_decl
);
14265 /* Fall through. */
14266 case IDENTIFIER_NODE
:
14267 if (IDENTIFIER_TYPENAME_P (t
))
14269 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14270 return mangle_conv_op_name_for_type (new_type
);
14276 /* This is handled by tsubst_copy_and_build. */
14277 gcc_unreachable ();
14281 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14282 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14283 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
14286 case CLEANUP_POINT_EXPR
:
14287 /* We shouldn't have built any of these during initial template
14288 generation. Instead, they should be built during instantiation
14289 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14290 gcc_unreachable ();
14294 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14295 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14296 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14297 r
= build2 (code
, type
, op0
, op1
);
14298 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
14299 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
14300 && !(complain
& tf_error
))
14301 return error_mark_node
;
14305 case EXPR_PACK_EXPANSION
:
14306 error ("invalid use of pack expansion expression");
14307 return error_mark_node
;
14309 case NONTYPE_ARGUMENT_PACK
:
14310 error ("use %<...%> to expand argument pack");
14311 return error_mark_node
;
14314 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
14322 /* Instantiate any typedefs in the type. */
14323 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14324 r
= fold_convert (type
, t
);
14325 gcc_assert (TREE_CODE (r
) == code
);
14330 /* These can sometimes show up in a partial instantiation, but never
14331 involve template parms. */
14332 gcc_assert (!uses_template_parms (t
));
14335 case UNARY_LEFT_FOLD_EXPR
:
14336 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
14337 case UNARY_RIGHT_FOLD_EXPR
:
14338 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
14339 case BINARY_LEFT_FOLD_EXPR
:
14340 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
14341 case BINARY_RIGHT_FOLD_EXPR
:
14342 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
14345 /* We shouldn't get here, but keep going if !flag_checking. */
14347 gcc_unreachable ();
14352 /* Helper function for tsubst_omp_clauses, used for instantiation of
14353 OMP_CLAUSE_DECL of clauses. */
14356 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
14359 if (decl
== NULL_TREE
)
14362 /* Handle an OpenMP array section represented as a TREE_LIST (or
14363 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14364 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14365 TREE_LIST. We can handle it exactly the same as an array section
14366 (purpose, value, and a chain), even though the nomenclature
14367 (low_bound, length, etc) is different. */
14368 if (TREE_CODE (decl
) == TREE_LIST
)
14371 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
14372 /*integral_constant_expression_p=*/false);
14373 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
14374 /*integral_constant_expression_p=*/false);
14375 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
14377 if (TREE_PURPOSE (decl
) == low_bound
14378 && TREE_VALUE (decl
) == length
14379 && TREE_CHAIN (decl
) == chain
)
14381 tree ret
= tree_cons (low_bound
, length
, chain
);
14382 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
14383 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
14386 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
14387 /*integral_constant_expression_p=*/false);
14388 /* Undo convert_from_reference tsubst_expr could have called. */
14390 && REFERENCE_REF_P (ret
)
14391 && !REFERENCE_REF_P (decl
))
14392 ret
= TREE_OPERAND (ret
, 0);
14396 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14399 tsubst_omp_clauses (tree clauses
, bool declare_simd
, bool allow_fields
,
14400 tree args
, tsubst_flags_t complain
, tree in_decl
)
14402 tree new_clauses
= NULL_TREE
, nc
, oc
;
14403 tree linear_no_step
= NULL_TREE
;
14405 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
14407 nc
= copy_node (oc
);
14408 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
14411 switch (OMP_CLAUSE_CODE (nc
))
14413 case OMP_CLAUSE_LASTPRIVATE
:
14414 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
14416 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
14417 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
14418 in_decl
, /*integral_constant_expression_p=*/false);
14419 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
14420 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
14423 case OMP_CLAUSE_PRIVATE
:
14424 case OMP_CLAUSE_SHARED
:
14425 case OMP_CLAUSE_FIRSTPRIVATE
:
14426 case OMP_CLAUSE_COPYIN
:
14427 case OMP_CLAUSE_COPYPRIVATE
:
14428 case OMP_CLAUSE_UNIFORM
:
14429 case OMP_CLAUSE_DEPEND
:
14430 case OMP_CLAUSE_FROM
:
14431 case OMP_CLAUSE_TO
:
14432 case OMP_CLAUSE_MAP
:
14433 case OMP_CLAUSE_USE_DEVICE_PTR
:
14434 case OMP_CLAUSE_IS_DEVICE_PTR
:
14435 OMP_CLAUSE_DECL (nc
)
14436 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14439 case OMP_CLAUSE_IF
:
14440 case OMP_CLAUSE_NUM_THREADS
:
14441 case OMP_CLAUSE_SCHEDULE
:
14442 case OMP_CLAUSE_COLLAPSE
:
14443 case OMP_CLAUSE_FINAL
:
14444 case OMP_CLAUSE_DEVICE
:
14445 case OMP_CLAUSE_DIST_SCHEDULE
:
14446 case OMP_CLAUSE_NUM_TEAMS
:
14447 case OMP_CLAUSE_THREAD_LIMIT
:
14448 case OMP_CLAUSE_SAFELEN
:
14449 case OMP_CLAUSE_SIMDLEN
:
14450 case OMP_CLAUSE_NUM_TASKS
:
14451 case OMP_CLAUSE_GRAINSIZE
:
14452 case OMP_CLAUSE_PRIORITY
:
14453 case OMP_CLAUSE_ORDERED
:
14454 case OMP_CLAUSE_HINT
:
14455 case OMP_CLAUSE_NUM_GANGS
:
14456 case OMP_CLAUSE_NUM_WORKERS
:
14457 case OMP_CLAUSE_VECTOR_LENGTH
:
14458 case OMP_CLAUSE_WORKER
:
14459 case OMP_CLAUSE_VECTOR
:
14460 case OMP_CLAUSE_ASYNC
:
14461 case OMP_CLAUSE_WAIT
:
14462 OMP_CLAUSE_OPERAND (nc
, 0)
14463 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14464 in_decl
, /*integral_constant_expression_p=*/false);
14466 case OMP_CLAUSE_REDUCTION
:
14467 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
14469 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
14470 if (TREE_CODE (placeholder
) == SCOPE_REF
)
14472 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
14473 complain
, in_decl
);
14474 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
14475 = build_qualified_name (NULL_TREE
, scope
,
14476 TREE_OPERAND (placeholder
, 1),
14480 gcc_assert (identifier_p (placeholder
));
14482 OMP_CLAUSE_DECL (nc
)
14483 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14486 case OMP_CLAUSE_GANG
:
14487 case OMP_CLAUSE_ALIGNED
:
14488 OMP_CLAUSE_DECL (nc
)
14489 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14491 OMP_CLAUSE_OPERAND (nc
, 1)
14492 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
14493 in_decl
, /*integral_constant_expression_p=*/false);
14495 case OMP_CLAUSE_LINEAR
:
14496 OMP_CLAUSE_DECL (nc
)
14497 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14499 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
14501 gcc_assert (!linear_no_step
);
14502 linear_no_step
= nc
;
14504 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
14505 OMP_CLAUSE_LINEAR_STEP (nc
)
14506 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
14507 complain
, in_decl
);
14509 OMP_CLAUSE_LINEAR_STEP (nc
)
14510 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
14512 /*integral_constant_expression_p=*/false);
14514 case OMP_CLAUSE_NOWAIT
:
14515 case OMP_CLAUSE_DEFAULT
:
14516 case OMP_CLAUSE_UNTIED
:
14517 case OMP_CLAUSE_MERGEABLE
:
14518 case OMP_CLAUSE_INBRANCH
:
14519 case OMP_CLAUSE_NOTINBRANCH
:
14520 case OMP_CLAUSE_PROC_BIND
:
14521 case OMP_CLAUSE_FOR
:
14522 case OMP_CLAUSE_PARALLEL
:
14523 case OMP_CLAUSE_SECTIONS
:
14524 case OMP_CLAUSE_TASKGROUP
:
14525 case OMP_CLAUSE_NOGROUP
:
14526 case OMP_CLAUSE_THREADS
:
14527 case OMP_CLAUSE_SIMD
:
14528 case OMP_CLAUSE_DEFAULTMAP
:
14529 case OMP_CLAUSE_INDEPENDENT
:
14530 case OMP_CLAUSE_AUTO
:
14531 case OMP_CLAUSE_SEQ
:
14533 case OMP_CLAUSE_TILE
:
14536 for (lnc
= OMP_CLAUSE_TILE_LIST (nc
),
14537 loc
= OMP_CLAUSE_TILE_LIST (oc
);
14539 loc
= TREE_CHAIN (loc
), lnc
= TREE_CHAIN (lnc
))
14541 TREE_VALUE (lnc
) = tsubst_expr (TREE_VALUE (loc
), args
,
14542 complain
, in_decl
, false);
14547 gcc_unreachable ();
14550 switch (OMP_CLAUSE_CODE (nc
))
14552 case OMP_CLAUSE_SHARED
:
14553 case OMP_CLAUSE_PRIVATE
:
14554 case OMP_CLAUSE_FIRSTPRIVATE
:
14555 case OMP_CLAUSE_LASTPRIVATE
:
14556 case OMP_CLAUSE_COPYPRIVATE
:
14557 case OMP_CLAUSE_LINEAR
:
14558 case OMP_CLAUSE_REDUCTION
:
14559 case OMP_CLAUSE_USE_DEVICE_PTR
:
14560 case OMP_CLAUSE_IS_DEVICE_PTR
:
14561 /* tsubst_expr on SCOPE_REF results in returning
14562 finish_non_static_data_member result. Undo that here. */
14563 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
14564 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
14565 == IDENTIFIER_NODE
))
14567 tree t
= OMP_CLAUSE_DECL (nc
);
14570 switch (TREE_CODE (v
))
14572 case COMPONENT_REF
:
14576 case POINTER_PLUS_EXPR
:
14577 v
= TREE_OPERAND (v
, 0);
14580 if (DECL_CONTEXT (v
) == current_function_decl
14581 && DECL_ARTIFICIAL (v
)
14582 && DECL_NAME (v
) == this_identifier
)
14583 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
14590 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
14591 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
14592 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
14593 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
14594 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
14596 tree decl
= OMP_CLAUSE_DECL (nc
);
14599 if (!DECL_LANG_SPECIFIC (decl
))
14600 retrofit_lang_decl (decl
);
14601 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
14610 new_clauses
= nreverse (new_clauses
);
14613 new_clauses
= finish_omp_clauses (new_clauses
, allow_fields
);
14614 if (linear_no_step
)
14615 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
14616 if (nc
== linear_no_step
)
14618 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
14622 return new_clauses
;
14625 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14628 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
14631 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14633 tree purpose
, value
, chain
;
14638 if (TREE_CODE (t
) != TREE_LIST
)
14639 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14640 /*function_p=*/false,
14641 /*integral_constant_expression_p=*/false);
14643 if (t
== void_list_node
)
14646 purpose
= TREE_PURPOSE (t
);
14648 purpose
= RECUR (purpose
);
14649 value
= TREE_VALUE (t
);
14652 if (TREE_CODE (value
) != LABEL_DECL
)
14653 value
= RECUR (value
);
14656 value
= lookup_label (DECL_NAME (value
));
14657 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
14658 TREE_USED (value
) = 1;
14661 chain
= TREE_CHAIN (t
);
14662 if (chain
&& chain
!= void_type_node
)
14663 chain
= RECUR (chain
);
14664 return tree_cons (purpose
, value
, chain
);
14668 /* Used to temporarily communicate the list of #pragma omp parallel
14669 clauses to #pragma omp for instantiation if they are combined
14672 static tree
*omp_parallel_combined_clauses
;
14674 /* Substitute one OMP_FOR iterator. */
14677 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
14678 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
14679 tree args
, tsubst_flags_t complain
, tree in_decl
,
14680 bool integral_constant_expression_p
)
14682 #define RECUR(NODE) \
14683 tsubst_expr ((NODE), args, complain, in_decl, \
14684 integral_constant_expression_p)
14685 tree decl
, init
, cond
, incr
;
14687 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
14688 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
14690 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
14692 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
14693 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
14696 decl
= TREE_OPERAND (init
, 0);
14697 init
= TREE_OPERAND (init
, 1);
14698 tree decl_expr
= NULL_TREE
;
14699 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
14701 /* We need to jump through some hoops to handle declarations in the
14702 for-init-statement, since we might need to handle auto deduction,
14703 but we need to keep control of initialization. */
14705 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
14706 decl
= tsubst_decl (decl
, args
, complain
);
14710 if (TREE_CODE (decl
) == SCOPE_REF
)
14712 decl
= RECUR (decl
);
14713 if (TREE_CODE (decl
) == COMPONENT_REF
)
14717 switch (TREE_CODE (v
))
14719 case COMPONENT_REF
:
14723 case POINTER_PLUS_EXPR
:
14724 v
= TREE_OPERAND (v
, 0);
14727 if (DECL_CONTEXT (v
) == current_function_decl
14728 && DECL_ARTIFICIAL (v
)
14729 && DECL_NAME (v
) == this_identifier
)
14731 decl
= TREE_OPERAND (decl
, 1);
14732 decl
= omp_privatize_field (decl
, false);
14742 decl
= RECUR (decl
);
14744 init
= RECUR (init
);
14746 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
14747 if (auto_node
&& init
)
14749 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
14751 gcc_assert (!type_dependent_expression_p (decl
));
14753 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14757 /* Declare the variable, but don't let that initialize it. */
14758 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
14759 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
14761 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
14764 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
14765 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14766 if (TREE_CODE (incr
) == MODIFY_EXPR
)
14768 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14769 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
14770 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
14771 NOP_EXPR
, rhs
, complain
);
14774 incr
= RECUR (incr
);
14775 TREE_VEC_ELT (declv
, i
) = decl
;
14776 TREE_VEC_ELT (initv
, i
) = init
;
14777 TREE_VEC_ELT (condv
, i
) = cond
;
14778 TREE_VEC_ELT (incrv
, i
) = incr
;
14784 /* Declare and initialize the variable. */
14792 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
14794 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
14796 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
14797 && OMP_CLAUSE_DECL (*pc
) == decl
)
14799 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
14800 && OMP_CLAUSE_DECL (*pc
) == decl
)
14804 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14806 *pc
= OMP_CLAUSE_CHAIN (c
);
14807 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14810 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
14811 && OMP_CLAUSE_DECL (*pc
) == decl
)
14813 error ("iteration variable %qD should not be firstprivate",
14815 *pc
= OMP_CLAUSE_CHAIN (*pc
);
14817 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
14818 && OMP_CLAUSE_DECL (*pc
) == decl
)
14820 error ("iteration variable %qD should not be reduction",
14822 *pc
= OMP_CLAUSE_CHAIN (*pc
);
14825 pc
= &OMP_CLAUSE_CHAIN (*pc
);
14830 if (*pc
== NULL_TREE
)
14832 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
14833 OMP_CLAUSE_DECL (c
) = decl
;
14834 c
= finish_omp_clauses (c
, true);
14837 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14842 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
14843 if (COMPARISON_CLASS_P (cond
))
14845 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
14846 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
14847 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
14850 cond
= RECUR (cond
);
14851 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14852 switch (TREE_CODE (incr
))
14854 case PREINCREMENT_EXPR
:
14855 case PREDECREMENT_EXPR
:
14856 case POSTINCREMENT_EXPR
:
14857 case POSTDECREMENT_EXPR
:
14858 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
14859 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
14862 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14863 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14865 tree rhs
= TREE_OPERAND (incr
, 1);
14866 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14867 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14868 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14869 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14870 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14874 incr
= RECUR (incr
);
14877 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14878 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14880 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14881 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14882 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
14883 TREE_TYPE (decl
), lhs
,
14884 RECUR (TREE_OPERAND (incr
, 2))));
14886 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
14887 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
14888 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
14890 tree rhs
= TREE_OPERAND (incr
, 2);
14891 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14892 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14893 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14894 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14895 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14899 incr
= RECUR (incr
);
14902 incr
= RECUR (incr
);
14906 TREE_VEC_ELT (declv
, i
) = decl
;
14907 TREE_VEC_ELT (initv
, i
) = init
;
14908 TREE_VEC_ELT (condv
, i
) = cond
;
14909 TREE_VEC_ELT (incrv
, i
) = incr
;
14913 /* Helper function of tsubst_expr, find OMP_TEAMS inside
14914 of OMP_TARGET's body. */
14917 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
14919 *walk_subtrees
= 0;
14920 switch (TREE_CODE (*tp
))
14925 case STATEMENT_LIST
:
14926 *walk_subtrees
= 1;
14934 /* Like tsubst_copy for expressions, etc. but also does semantic
14938 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
14939 bool integral_constant_expression_p
)
14941 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14942 #define RECUR(NODE) \
14943 tsubst_expr ((NODE), args, complain, in_decl, \
14944 integral_constant_expression_p)
14950 if (t
== NULL_TREE
|| t
== error_mark_node
)
14953 loc
= input_location
;
14954 if (EXPR_HAS_LOCATION (t
))
14955 input_location
= EXPR_LOCATION (t
);
14956 if (STATEMENT_CODE_P (TREE_CODE (t
)))
14957 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
14959 switch (TREE_CODE (t
))
14961 case STATEMENT_LIST
:
14963 tree_stmt_iterator i
;
14964 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
14965 RECUR (tsi_stmt (i
));
14969 case CTOR_INITIALIZER
:
14970 finish_mem_initializers (tsubst_initializer_list
14971 (TREE_OPERAND (t
, 0), args
));
14975 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
14979 tmp
= RECUR (EXPR_STMT_EXPR (t
));
14980 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
14981 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
14983 finish_expr_stmt (tmp
);
14987 do_using_directive (USING_STMT_NAMESPACE (t
));
14992 tree decl
, pattern_decl
;
14995 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
14996 if (TREE_CODE (decl
) == LABEL_DECL
)
14997 finish_label_decl (DECL_NAME (decl
));
14998 else if (TREE_CODE (decl
) == USING_DECL
)
15000 tree scope
= USING_DECL_SCOPE (decl
);
15001 tree name
= DECL_NAME (decl
);
15004 scope
= tsubst (scope
, args
, complain
, in_decl
);
15005 decl
= lookup_qualified_name (scope
, name
,
15006 /*is_type_p=*/false,
15007 /*complain=*/false);
15008 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15009 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15011 do_local_using_decl (decl
, scope
, name
);
15013 else if (DECL_PACK_P (decl
))
15015 /* Don't build up decls for a variadic capture proxy, we'll
15016 instantiate the elements directly as needed. */
15021 init
= DECL_INITIAL (decl
);
15022 decl
= tsubst (decl
, args
, complain
, in_decl
);
15023 if (decl
!= error_mark_node
)
15025 /* By marking the declaration as instantiated, we avoid
15026 trying to instantiate it. Since instantiate_decl can't
15027 handle local variables, and since we've already done
15028 all that needs to be done, that's the right thing to
15031 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15033 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15034 /* Anonymous aggregates are a special case. */
15035 finish_anon_union (decl
);
15036 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15038 DECL_CONTEXT (decl
) = current_function_decl
;
15039 if (DECL_NAME (decl
) == this_identifier
)
15041 tree lam
= DECL_CONTEXT (current_function_decl
);
15042 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15043 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15045 insert_capture_proxy (decl
);
15047 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15048 /* We already did a pushtag. */;
15049 else if (TREE_CODE (decl
) == FUNCTION_DECL
15050 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15051 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15053 DECL_CONTEXT (decl
) = NULL_TREE
;
15055 DECL_CONTEXT (decl
) = current_function_decl
;
15056 cp_check_omp_declare_reduction (decl
);
15060 int const_init
= false;
15061 maybe_push_decl (decl
);
15063 && DECL_PRETTY_FUNCTION_P (decl
))
15065 /* For __PRETTY_FUNCTION__ we have to adjust the
15067 const char *const name
15068 = cxx_printable_name (current_function_decl
, 2);
15069 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15072 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15075 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15077 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15086 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15087 RECUR (FOR_INIT_STMT (t
));
15088 finish_for_init_stmt (stmt
);
15089 tmp
= RECUR (FOR_COND (t
));
15090 finish_for_cond (tmp
, stmt
, false);
15091 tmp
= RECUR (FOR_EXPR (t
));
15092 finish_for_expr (tmp
, stmt
);
15093 RECUR (FOR_BODY (t
));
15094 finish_for_stmt (stmt
);
15097 case RANGE_FOR_STMT
:
15100 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15101 decl
= RANGE_FOR_DECL (t
);
15102 decl
= tsubst (decl
, args
, complain
, in_decl
);
15103 maybe_push_decl (decl
);
15104 expr
= RECUR (RANGE_FOR_EXPR (t
));
15105 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
15106 RECUR (RANGE_FOR_BODY (t
));
15107 finish_for_stmt (stmt
);
15112 stmt
= begin_while_stmt ();
15113 tmp
= RECUR (WHILE_COND (t
));
15114 finish_while_stmt_cond (tmp
, stmt
, false);
15115 RECUR (WHILE_BODY (t
));
15116 finish_while_stmt (stmt
);
15120 stmt
= begin_do_stmt ();
15121 RECUR (DO_BODY (t
));
15122 finish_do_body (stmt
);
15123 tmp
= RECUR (DO_COND (t
));
15124 finish_do_stmt (tmp
, stmt
, false);
15128 stmt
= begin_if_stmt ();
15129 tmp
= RECUR (IF_COND (t
));
15130 finish_if_stmt_cond (tmp
, stmt
);
15131 RECUR (THEN_CLAUSE (t
));
15132 finish_then_clause (stmt
);
15134 if (ELSE_CLAUSE (t
))
15136 begin_else_clause (stmt
);
15137 RECUR (ELSE_CLAUSE (t
));
15138 finish_else_clause (stmt
);
15141 finish_if_stmt (stmt
);
15145 if (BIND_EXPR_BODY_BLOCK (t
))
15146 stmt
= begin_function_body ();
15148 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
15149 ? BCS_TRY_BLOCK
: 0);
15151 RECUR (BIND_EXPR_BODY (t
));
15153 if (BIND_EXPR_BODY_BLOCK (t
))
15154 finish_function_body (stmt
);
15156 finish_compound_stmt (stmt
);
15160 finish_break_stmt ();
15163 case CONTINUE_STMT
:
15164 finish_continue_stmt ();
15168 stmt
= begin_switch_stmt ();
15169 tmp
= RECUR (SWITCH_STMT_COND (t
));
15170 finish_switch_cond (tmp
, stmt
);
15171 RECUR (SWITCH_STMT_BODY (t
));
15172 finish_switch_stmt (stmt
);
15175 case CASE_LABEL_EXPR
:
15177 tree low
= RECUR (CASE_LOW (t
));
15178 tree high
= RECUR (CASE_HIGH (t
));
15179 finish_case_label (EXPR_LOCATION (t
), low
, high
);
15185 tree decl
= LABEL_EXPR_LABEL (t
);
15188 label
= finish_label_stmt (DECL_NAME (decl
));
15189 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
15190 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
15195 tmp
= GOTO_DESTINATION (t
);
15196 if (TREE_CODE (tmp
) != LABEL_DECL
)
15197 /* Computed goto's must be tsubst'd into. On the other hand,
15198 non-computed gotos must not be; the identifier in question
15199 will have no binding. */
15202 tmp
= DECL_NAME (tmp
);
15203 finish_goto_stmt (tmp
);
15208 tree string
= RECUR (ASM_STRING (t
));
15209 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
15210 complain
, in_decl
);
15211 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
15212 complain
, in_decl
);
15213 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
15214 complain
, in_decl
);
15215 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
15216 complain
, in_decl
);
15217 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
15219 tree asm_expr
= tmp
;
15220 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
15221 asm_expr
= TREE_OPERAND (asm_expr
, 0);
15222 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
15229 stmt
= begin_try_block ();
15230 RECUR (TRY_STMTS (t
));
15231 finish_cleanup_try_block (stmt
);
15232 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
15236 tree compound_stmt
= NULL_TREE
;
15238 if (FN_TRY_BLOCK_P (t
))
15239 stmt
= begin_function_try_block (&compound_stmt
);
15241 stmt
= begin_try_block ();
15243 RECUR (TRY_STMTS (t
));
15245 if (FN_TRY_BLOCK_P (t
))
15246 finish_function_try_block (stmt
);
15248 finish_try_block (stmt
);
15250 RECUR (TRY_HANDLERS (t
));
15251 if (FN_TRY_BLOCK_P (t
))
15252 finish_function_handler_sequence (stmt
, compound_stmt
);
15254 finish_handler_sequence (stmt
);
15260 tree decl
= HANDLER_PARMS (t
);
15264 decl
= tsubst (decl
, args
, complain
, in_decl
);
15265 /* Prevent instantiate_decl from trying to instantiate
15266 this variable. We've already done all that needs to be
15268 if (decl
!= error_mark_node
)
15269 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15271 stmt
= begin_handler ();
15272 finish_handler_parms (decl
, stmt
);
15273 RECUR (HANDLER_BODY (t
));
15274 finish_handler (stmt
);
15279 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
15280 if (CLASS_TYPE_P (tmp
))
15282 /* Local classes are not independent templates; they are
15283 instantiated along with their containing function. And this
15284 way we don't have to deal with pushing out of one local class
15285 to instantiate a member of another local class. */
15287 /* Closures are handled by the LAMBDA_EXPR. */
15288 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
15289 complete_type (tmp
);
15290 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
15291 if (!DECL_ARTIFICIAL (fn
))
15292 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
15296 case STATIC_ASSERT
:
15300 ++c_inhibit_evaluation_warnings
;
15302 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
15305 /*integral_constant_expression_p=*/true);
15306 --c_inhibit_evaluation_warnings
;
15308 finish_static_assert (condition
,
15309 STATIC_ASSERT_MESSAGE (t
),
15310 STATIC_ASSERT_SOURCE_LOCATION (t
),
15311 /*member_p=*/false);
15316 case OACC_PARALLEL
:
15317 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false, false, args
, complain
,
15319 stmt
= begin_omp_parallel ();
15320 RECUR (OMP_BODY (t
));
15321 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
15325 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
15326 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false, true,
15327 args
, complain
, in_decl
);
15328 if (OMP_PARALLEL_COMBINED (t
))
15329 omp_parallel_combined_clauses
= &tmp
;
15330 stmt
= begin_omp_parallel ();
15331 RECUR (OMP_PARALLEL_BODY (t
));
15332 gcc_assert (omp_parallel_combined_clauses
== NULL
);
15333 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
15334 = OMP_PARALLEL_COMBINED (t
);
15335 pop_omp_privatization_clauses (r
);
15339 r
= push_omp_privatization_clauses (false);
15340 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false, true,
15341 args
, complain
, in_decl
);
15342 stmt
= begin_omp_task ();
15343 RECUR (OMP_TASK_BODY (t
));
15344 finish_omp_task (tmp
, stmt
);
15345 pop_omp_privatization_clauses (r
);
15352 case OMP_DISTRIBUTE
:
15356 tree clauses
, body
, pre_body
;
15357 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
15358 tree orig_declv
= NULL_TREE
;
15359 tree incrv
= NULL_TREE
;
15362 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
15363 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
15364 TREE_CODE (t
) != OACC_LOOP
,
15365 args
, complain
, in_decl
);
15366 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15368 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15369 if (OMP_FOR_ORIG_DECLS (t
))
15370 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15371 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15372 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15373 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15376 stmt
= begin_omp_structured_block ();
15378 pre_body
= push_stmt_list ();
15379 RECUR (OMP_FOR_PRE_BODY (t
));
15380 pre_body
= pop_stmt_list (pre_body
);
15382 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15383 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
15384 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
15385 incrv
, &clauses
, args
, complain
, in_decl
,
15386 integral_constant_expression_p
);
15387 omp_parallel_combined_clauses
= NULL
;
15389 body
= push_stmt_list ();
15390 RECUR (OMP_FOR_BODY (t
));
15391 body
= pop_stmt_list (body
);
15393 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15394 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
15395 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
15399 t
= make_node (TREE_CODE (t
));
15400 TREE_TYPE (t
) = void_type_node
;
15401 OMP_FOR_BODY (t
) = body
;
15402 OMP_FOR_PRE_BODY (t
) = pre_body
;
15403 OMP_FOR_CLAUSES (t
) = clauses
;
15404 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
15408 add_stmt (finish_omp_structured_block (stmt
));
15409 pop_omp_privatization_clauses (r
);
15414 omp_parallel_combined_clauses
= NULL
;
15419 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
15420 && OMP_TEAMS_COMBINED (t
));
15421 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false, true,
15422 args
, complain
, in_decl
);
15423 stmt
= push_stmt_list ();
15424 RECUR (OMP_BODY (t
));
15425 stmt
= pop_stmt_list (stmt
);
15428 OMP_BODY (t
) = stmt
;
15429 OMP_CLAUSES (t
) = tmp
;
15431 pop_omp_privatization_clauses (r
);
15435 case OMP_TARGET_DATA
:
15437 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
15438 TREE_CODE (t
) != OACC_DATA
,
15439 args
, complain
, in_decl
);
15440 keep_next_level (true);
15441 stmt
= begin_omp_structured_block ();
15443 RECUR (OMP_BODY (t
));
15444 stmt
= finish_omp_structured_block (stmt
);
15447 OMP_BODY (t
) = stmt
;
15448 OMP_CLAUSES (t
) = tmp
;
15449 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
15451 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
15454 /* For combined target teams, ensure the num_teams and
15455 thread_limit clause expressions are evaluated on the host,
15456 before entering the target construct. */
15458 for (c
= OMP_TEAMS_CLAUSES (teams
);
15459 c
; c
= OMP_CLAUSE_CHAIN (c
))
15460 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
15461 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
15462 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
15464 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
15465 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
15466 if (expr
== error_mark_node
)
15468 tmp
= TARGET_EXPR_SLOT (expr
);
15470 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
15471 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
15472 OMP_CLAUSE_FIRSTPRIVATE
);
15473 OMP_CLAUSE_DECL (tc
) = tmp
;
15474 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
15475 OMP_TARGET_CLAUSES (t
) = tc
;
15484 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), false, false,
15485 args
, complain
, in_decl
);
15486 OACC_DECLARE_CLAUSES (t
) = tmp
;
15490 case OMP_TARGET_UPDATE
:
15491 case OMP_TARGET_ENTER_DATA
:
15492 case OMP_TARGET_EXIT_DATA
:
15493 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), false, true,
15494 args
, complain
, in_decl
);
15496 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15500 case OACC_ENTER_DATA
:
15501 case OACC_EXIT_DATA
:
15503 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), false, false,
15504 args
, complain
, in_decl
);
15506 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15511 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), false, true,
15512 args
, complain
, in_decl
);
15513 stmt
= push_stmt_list ();
15514 RECUR (OMP_BODY (t
));
15515 stmt
= pop_stmt_list (stmt
);
15518 OMP_BODY (t
) = stmt
;
15519 OMP_ORDERED_CLAUSES (t
) = tmp
;
15525 case OMP_TASKGROUP
:
15526 stmt
= push_stmt_list ();
15527 RECUR (OMP_BODY (t
));
15528 stmt
= pop_stmt_list (stmt
);
15531 OMP_BODY (t
) = stmt
;
15536 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
15537 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
15539 tree op1
= TREE_OPERAND (t
, 1);
15540 tree rhs1
= NULL_TREE
;
15542 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
15544 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
15545 op1
= TREE_OPERAND (op1
, 1);
15547 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15548 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15549 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
15550 NULL_TREE
, NULL_TREE
, rhs1
,
15551 OMP_ATOMIC_SEQ_CST (t
));
15555 tree op1
= TREE_OPERAND (t
, 1);
15556 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
15557 tree rhs1
= NULL_TREE
;
15558 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
15559 enum tree_code opcode
= NOP_EXPR
;
15560 if (code
== OMP_ATOMIC_READ
)
15562 v
= RECUR (TREE_OPERAND (op1
, 0));
15563 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15565 else if (code
== OMP_ATOMIC_CAPTURE_OLD
15566 || code
== OMP_ATOMIC_CAPTURE_NEW
)
15568 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
15569 v
= RECUR (TREE_OPERAND (op1
, 0));
15570 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15571 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
15573 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
15574 op11
= TREE_OPERAND (op11
, 1);
15576 lhs
= RECUR (TREE_OPERAND (op11
, 0));
15577 rhs
= RECUR (TREE_OPERAND (op11
, 1));
15578 opcode
= TREE_CODE (op11
);
15579 if (opcode
== MODIFY_EXPR
)
15585 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15586 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15588 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
15589 OMP_ATOMIC_SEQ_CST (t
));
15593 case TRANSACTION_EXPR
:
15596 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
15597 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
15599 if (TRANSACTION_EXPR_IS_STMT (t
))
15601 tree body
= TRANSACTION_EXPR_BODY (t
);
15602 tree noex
= NULL_TREE
;
15603 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
15605 noex
= MUST_NOT_THROW_COND (body
);
15606 if (noex
== NULL_TREE
)
15607 noex
= boolean_true_node
;
15608 body
= TREE_OPERAND (body
, 0);
15610 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
15612 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
15616 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
15617 RECUR (TRANSACTION_EXPR_BODY (t
)),
15624 case MUST_NOT_THROW_EXPR
:
15626 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15627 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
15628 RETURN (build_must_not_throw_expr (op0
, cond
));
15631 case EXPR_PACK_EXPANSION
:
15632 error ("invalid use of pack expansion expression");
15633 RETURN (error_mark_node
);
15635 case NONTYPE_ARGUMENT_PACK
:
15636 error ("use %<...%> to expand argument pack");
15637 RETURN (error_mark_node
);
15639 case CILK_SPAWN_STMT
:
15640 cfun
->calls_cilk_spawn
= 1;
15641 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
15643 case CILK_SYNC_STMT
:
15644 RETURN (build_cilk_sync ());
15646 case COMPOUND_EXPR
:
15647 tmp
= RECUR (TREE_OPERAND (t
, 0));
15648 if (tmp
== NULL_TREE
)
15649 /* If the first operand was a statement, we're done with it. */
15650 RETURN (RECUR (TREE_OPERAND (t
, 1)));
15651 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
15652 RECUR (TREE_OPERAND (t
, 1)),
15655 case ANNOTATE_EXPR
:
15656 tmp
= RECUR (TREE_OPERAND (t
, 0));
15657 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
15658 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
15661 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
15663 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15664 /*function_p=*/false,
15665 integral_constant_expression_p
));
15668 RETURN (NULL_TREE
);
15670 input_location
= loc
;
15676 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15677 function. For description of the body see comment above
15678 cp_parser_omp_declare_reduction_exprs. */
15681 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15683 if (t
== NULL_TREE
|| t
== error_mark_node
)
15686 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
15688 tree_stmt_iterator tsi
;
15691 memset (stmts
, 0, sizeof stmts
);
15692 for (i
= 0, tsi
= tsi_start (t
);
15693 i
< 7 && !tsi_end_p (tsi
);
15694 i
++, tsi_next (&tsi
))
15695 stmts
[i
] = tsi_stmt (tsi
);
15696 gcc_assert (tsi_end_p (tsi
));
15700 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
15701 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
15702 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
15703 args
, complain
, in_decl
);
15704 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
15705 args
, complain
, in_decl
);
15706 DECL_CONTEXT (omp_out
) = current_function_decl
;
15707 DECL_CONTEXT (omp_in
) = current_function_decl
;
15708 keep_next_level (true);
15709 tree block
= begin_omp_structured_block ();
15710 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
15711 block
= finish_omp_structured_block (block
);
15712 block
= maybe_cleanup_point_expr_void (block
);
15713 add_decl_expr (omp_out
);
15714 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
15715 TREE_NO_WARNING (omp_out
) = 1;
15716 add_decl_expr (omp_in
);
15717 finish_expr_stmt (block
);
15721 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
15722 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
15723 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
15724 args
, complain
, in_decl
);
15725 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
15726 args
, complain
, in_decl
);
15727 DECL_CONTEXT (omp_priv
) = current_function_decl
;
15728 DECL_CONTEXT (omp_orig
) = current_function_decl
;
15729 keep_next_level (true);
15730 tree block
= begin_omp_structured_block ();
15731 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
15732 block
= finish_omp_structured_block (block
);
15733 block
= maybe_cleanup_point_expr_void (block
);
15734 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
15735 add_decl_expr (omp_priv
);
15736 add_decl_expr (omp_orig
);
15737 finish_expr_stmt (block
);
15739 add_decl_expr (omp_orig
);
15743 /* T is a postfix-expression that is not being used in a function
15744 call. Return the substituted version of T. */
15747 tsubst_non_call_postfix_expression (tree t
, tree args
,
15748 tsubst_flags_t complain
,
15751 if (TREE_CODE (t
) == SCOPE_REF
)
15752 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
15753 /*done=*/false, /*address_p=*/false);
15755 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15756 /*function_p=*/false,
15757 /*integral_constant_expression_p=*/false);
15762 /* Like tsubst but deals with expressions and performs semantic
15763 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15766 tsubst_copy_and_build (tree t
,
15768 tsubst_flags_t complain
,
15771 bool integral_constant_expression_p
)
15773 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15774 #define RECUR(NODE) \
15775 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15776 /*function_p=*/false, \
15777 integral_constant_expression_p)
15782 if (t
== NULL_TREE
|| t
== error_mark_node
)
15785 loc
= input_location
;
15786 if (EXPR_HAS_LOCATION (t
))
15787 input_location
= EXPR_LOCATION (t
);
15789 /* N3276 decltype magic only applies to calls at the top level or on the
15790 right side of a comma. */
15791 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
15792 complain
&= ~tf_decltype
;
15794 switch (TREE_CODE (t
))
15798 /* Fall through. */
15799 case IDENTIFIER_NODE
:
15803 bool non_integral_constant_expression_p
;
15804 const char *error_msg
;
15806 if (IDENTIFIER_TYPENAME_P (t
))
15808 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15809 t
= mangle_conv_op_name_for_type (new_type
);
15812 /* Look up the name. */
15813 decl
= lookup_name (t
);
15815 /* By convention, expressions use ERROR_MARK_NODE to indicate
15816 failure, not NULL_TREE. */
15817 if (decl
== NULL_TREE
)
15818 decl
= error_mark_node
;
15820 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
15822 integral_constant_expression_p
,
15823 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
15824 &non_integral_constant_expression_p
,
15825 /*template_p=*/false,
15827 /*address_p=*/false,
15828 /*template_arg_p=*/false,
15833 if (!function_p
&& identifier_p (decl
))
15835 if (complain
& tf_error
)
15836 unqualified_name_lookup_error (decl
);
15837 decl
= error_mark_node
;
15842 case TEMPLATE_ID_EXPR
:
15845 tree templ
= RECUR (TREE_OPERAND (t
, 0));
15846 tree targs
= TREE_OPERAND (t
, 1);
15849 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15850 if (targs
== error_mark_node
)
15851 return error_mark_node
;
15853 if (variable_template_p (templ
))
15855 templ
= lookup_template_variable (templ
, targs
);
15856 if (!any_dependent_template_arguments_p (targs
))
15858 templ
= finish_template_variable (templ
, complain
);
15861 RETURN (convert_from_reference (templ
));
15864 if (TREE_CODE (templ
) == COMPONENT_REF
)
15866 object
= TREE_OPERAND (templ
, 0);
15867 templ
= TREE_OPERAND (templ
, 1);
15870 object
= NULL_TREE
;
15871 templ
= lookup_template_function (templ
, targs
);
15874 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
15875 object
, templ
, NULL_TREE
));
15877 RETURN (baselink_for_fns (templ
));
15882 tree r
= RECUR (TREE_OPERAND (t
, 0));
15884 if (REFERENCE_REF_P (t
))
15886 /* A type conversion to reference type will be enclosed in
15887 such an indirect ref, but the substitution of the cast
15888 will have also added such an indirect ref. */
15889 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
15890 r
= convert_from_reference (r
);
15893 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
15894 complain
|decltype_flag
);
15900 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15901 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15902 RETURN (build_nop (type
, op0
));
15905 case IMPLICIT_CONV_EXPR
:
15907 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15908 tree expr
= RECUR (TREE_OPERAND (t
, 0));
15909 int flags
= LOOKUP_IMPLICIT
;
15910 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
15911 flags
= LOOKUP_NORMAL
;
15912 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
15918 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15919 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15920 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
15924 case REINTERPRET_CAST_EXPR
:
15925 case CONST_CAST_EXPR
:
15926 case DYNAMIC_CAST_EXPR
:
15927 case STATIC_CAST_EXPR
:
15930 tree op
, r
= NULL_TREE
;
15932 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15933 if (integral_constant_expression_p
15934 && !cast_valid_in_integral_constant_expression_p (type
))
15936 if (complain
& tf_error
)
15937 error ("a cast to a type other than an integral or "
15938 "enumeration type cannot appear in a constant-expression");
15939 RETURN (error_mark_node
);
15942 op
= RECUR (TREE_OPERAND (t
, 0));
15944 warning_sentinel
s(warn_useless_cast
);
15945 switch (TREE_CODE (t
))
15948 r
= build_functional_cast (type
, op
, complain
);
15950 case REINTERPRET_CAST_EXPR
:
15951 r
= build_reinterpret_cast (type
, op
, complain
);
15953 case CONST_CAST_EXPR
:
15954 r
= build_const_cast (type
, op
, complain
);
15956 case DYNAMIC_CAST_EXPR
:
15957 r
= build_dynamic_cast (type
, op
, complain
);
15959 case STATIC_CAST_EXPR
:
15960 r
= build_static_cast (type
, op
, complain
);
15963 gcc_unreachable ();
15969 case POSTDECREMENT_EXPR
:
15970 case POSTINCREMENT_EXPR
:
15971 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15972 args
, complain
, in_decl
);
15973 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
15974 complain
|decltype_flag
));
15976 case PREDECREMENT_EXPR
:
15977 case PREINCREMENT_EXPR
:
15981 case TRUTH_NOT_EXPR
:
15982 case UNARY_PLUS_EXPR
: /* Unary + */
15983 case REALPART_EXPR
:
15984 case IMAGPART_EXPR
:
15985 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
15986 RECUR (TREE_OPERAND (t
, 0)),
15987 complain
|decltype_flag
));
15989 case FIX_TRUNC_EXPR
:
15990 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
15994 op1
= TREE_OPERAND (t
, 0);
15995 if (TREE_CODE (op1
) == LABEL_DECL
)
15996 RETURN (finish_label_address_expr (DECL_NAME (op1
),
15997 EXPR_LOCATION (op1
)));
15998 if (TREE_CODE (op1
) == SCOPE_REF
)
15999 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
16000 /*done=*/true, /*address_p=*/true);
16002 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
16004 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16005 complain
|decltype_flag
));
16010 case TRUNC_DIV_EXPR
:
16011 case CEIL_DIV_EXPR
:
16012 case FLOOR_DIV_EXPR
:
16013 case ROUND_DIV_EXPR
:
16014 case EXACT_DIV_EXPR
:
16018 case TRUNC_MOD_EXPR
:
16019 case FLOOR_MOD_EXPR
:
16020 case TRUTH_ANDIF_EXPR
:
16021 case TRUTH_ORIF_EXPR
:
16022 case TRUTH_AND_EXPR
:
16023 case TRUTH_OR_EXPR
:
16039 warning_sentinel
s1(warn_type_limits
);
16040 warning_sentinel
s2(warn_div_by_zero
);
16041 warning_sentinel
s3(warn_logical_op
);
16042 warning_sentinel
s4(warn_tautological_compare
);
16043 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16044 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16045 tree r
= build_x_binary_op
16046 (input_location
, TREE_CODE (t
),
16048 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16050 : TREE_CODE (TREE_OPERAND (t
, 0))),
16052 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16054 : TREE_CODE (TREE_OPERAND (t
, 1))),
16056 complain
|decltype_flag
);
16057 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16058 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16063 case POINTER_PLUS_EXPR
:
16065 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16066 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16067 return fold_build_pointer_plus (op0
, op1
);
16071 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16072 /*address_p=*/false));
16074 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16075 args
, complain
, in_decl
);
16076 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16077 RECUR (TREE_OPERAND (t
, 1)),
16078 complain
|decltype_flag
));
16080 case ARRAY_NOTATION_REF
:
16082 tree start_index
, length
, stride
;
16083 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16084 args
, complain
, in_decl
);
16085 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16086 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16087 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16088 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16089 length
, stride
, TREE_TYPE (op1
)));
16092 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
16093 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16100 op1
= TREE_OPERAND (t
, 0);
16101 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16102 op1
= TREE_TYPE (op1
);
16105 /* When there are no ARGS, we are trying to evaluate a
16106 non-dependent expression from the parser. Trying to do
16107 the substitutions may not work. */
16109 op1
= TREE_TYPE (op1
);
16113 ++cp_unevaluated_operand
;
16114 ++c_inhibit_evaluation_warnings
;
16116 op1
= tsubst (op1
, args
, complain
, in_decl
);
16118 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16119 /*function_p=*/false,
16120 /*integral_constant_expression_p=*/
16122 --cp_unevaluated_operand
;
16123 --c_inhibit_evaluation_warnings
;
16126 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16127 complain
& tf_error
);
16129 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
16130 complain
& tf_error
);
16131 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
16133 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
16135 if (!processing_template_decl
&& TYPE_P (op1
))
16137 r
= build_min (SIZEOF_EXPR
, size_type_node
,
16138 build1 (NOP_EXPR
, op1
, error_mark_node
));
16139 SIZEOF_EXPR_TYPE_P (r
) = 1;
16142 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
16143 TREE_SIDE_EFFECTS (r
) = 0;
16144 TREE_READONLY (r
) = 1;
16146 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16151 case AT_ENCODE_EXPR
:
16153 op1
= TREE_OPERAND (t
, 0);
16154 ++cp_unevaluated_operand
;
16155 ++c_inhibit_evaluation_warnings
;
16156 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16157 /*function_p=*/false,
16158 /*integral_constant_expression_p=*/false);
16159 --cp_unevaluated_operand
;
16160 --c_inhibit_evaluation_warnings
;
16161 RETURN (objc_build_encode_expr (op1
));
16164 case NOEXCEPT_EXPR
:
16165 op1
= TREE_OPERAND (t
, 0);
16166 ++cp_unevaluated_operand
;
16167 ++c_inhibit_evaluation_warnings
;
16168 ++cp_noexcept_operand
;
16169 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16170 /*function_p=*/false,
16171 /*integral_constant_expression_p=*/false);
16172 --cp_unevaluated_operand
;
16173 --c_inhibit_evaluation_warnings
;
16174 --cp_noexcept_operand
;
16175 RETURN (finish_noexcept_expr (op1
, complain
));
16179 warning_sentinel
s(warn_div_by_zero
);
16180 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
16181 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
16182 tree r
= build_x_modify_expr
16183 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
16184 complain
|decltype_flag
);
16185 /* TREE_NO_WARNING must be set if either the expression was
16186 parenthesized or it uses an operator such as >>= rather
16187 than plain assignment. In the former case, it was already
16188 set and must be copied. In the latter case,
16189 build_x_modify_expr sets it and it must not be reset
16191 if (TREE_NO_WARNING (t
))
16192 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16198 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16199 args
, complain
, in_decl
);
16200 /* Remember that there was a reference to this entity. */
16202 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
16203 RETURN (error_mark_node
);
16204 RETURN (build_x_arrow (input_location
, op1
, complain
));
16208 tree placement
= RECUR (TREE_OPERAND (t
, 0));
16209 tree init
= RECUR (TREE_OPERAND (t
, 3));
16210 vec
<tree
, va_gc
> *placement_vec
;
16211 vec
<tree
, va_gc
> *init_vec
;
16214 if (placement
== NULL_TREE
)
16215 placement_vec
= NULL
;
16218 placement_vec
= make_tree_vector ();
16219 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
16220 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
16223 /* If there was an initializer in the original tree, but it
16224 instantiated to an empty list, then we should pass a
16225 non-NULL empty vector to tell build_new that it was an
16226 empty initializer() rather than no initializer. This can
16227 only happen when the initializer is a pack expansion whose
16228 parameter packs are of length zero. */
16229 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
16233 init_vec
= make_tree_vector ();
16234 if (init
== void_node
)
16235 gcc_assert (init_vec
!= NULL
);
16238 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
16239 vec_safe_push (init_vec
, TREE_VALUE (init
));
16243 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16244 tree op2
= RECUR (TREE_OPERAND (t
, 2));
16245 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
16246 NEW_EXPR_USE_GLOBAL (t
),
16249 if (placement_vec
!= NULL
)
16250 release_tree_vector (placement_vec
);
16251 if (init_vec
!= NULL
)
16252 release_tree_vector (init_vec
);
16259 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16260 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16261 RETURN (delete_sanity (op0
, op1
,
16262 DELETE_EXPR_USE_VEC (t
),
16263 DELETE_EXPR_USE_GLOBAL (t
),
16267 case COMPOUND_EXPR
:
16269 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
16270 complain
& ~tf_decltype
, in_decl
,
16271 /*function_p=*/false,
16272 integral_constant_expression_p
);
16273 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
16275 RECUR (TREE_OPERAND (t
, 1)),
16276 complain
|decltype_flag
));
16282 vec
<tree
, va_gc
> *call_args
;
16283 unsigned int nargs
, i
;
16288 function
= CALL_EXPR_FN (t
);
16289 /* When we parsed the expression, we determined whether or
16290 not Koenig lookup should be performed. */
16291 koenig_p
= KOENIG_LOOKUP_P (t
);
16292 if (TREE_CODE (function
) == SCOPE_REF
)
16294 qualified_p
= true;
16295 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
16297 /*address_p=*/false);
16299 else if (koenig_p
&& identifier_p (function
))
16301 /* Do nothing; calling tsubst_copy_and_build on an identifier
16302 would incorrectly perform unqualified lookup again.
16304 Note that we can also have an IDENTIFIER_NODE if the earlier
16305 unqualified lookup found a member function; in that case
16306 koenig_p will be false and we do want to do the lookup
16307 again to find the instantiated member function.
16309 FIXME but doing that causes c++/15272, so we need to stop
16310 using IDENTIFIER_NODE in that situation. */
16311 qualified_p
= false;
16315 if (TREE_CODE (function
) == COMPONENT_REF
)
16317 tree op
= TREE_OPERAND (function
, 1);
16319 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
16320 || (BASELINK_P (op
)
16321 && BASELINK_QUALIFIED_P (op
)));
16324 qualified_p
= false;
16326 if (TREE_CODE (function
) == ADDR_EXPR
16327 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
16328 /* Avoid error about taking the address of a constructor. */
16329 function
= TREE_OPERAND (function
, 0);
16331 function
= tsubst_copy_and_build (function
, args
, complain
,
16334 integral_constant_expression_p
);
16336 if (BASELINK_P (function
))
16337 qualified_p
= true;
16340 nargs
= call_expr_nargs (t
);
16341 call_args
= make_tree_vector ();
16342 for (i
= 0; i
< nargs
; ++i
)
16344 tree arg
= CALL_EXPR_ARG (t
, i
);
16346 if (!PACK_EXPANSION_P (arg
))
16347 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
16350 /* Expand the pack expansion and push each entry onto
16352 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
16353 if (TREE_CODE (arg
) == TREE_VEC
)
16355 unsigned int len
, j
;
16357 len
= TREE_VEC_LENGTH (arg
);
16358 for (j
= 0; j
< len
; ++j
)
16360 tree value
= TREE_VEC_ELT (arg
, j
);
16361 if (value
!= NULL_TREE
)
16362 value
= convert_from_reference (value
);
16363 vec_safe_push (call_args
, value
);
16368 /* A partial substitution. Add one entry. */
16369 vec_safe_push (call_args
, arg
);
16374 /* We do not perform argument-dependent lookup if normal
16375 lookup finds a non-function, in accordance with the
16376 expected resolution of DR 218. */
16378 && ((is_overloaded_fn (function
)
16379 /* If lookup found a member function, the Koenig lookup is
16380 not appropriate, even if an unqualified-name was used
16381 to denote the function. */
16382 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
16383 || identifier_p (function
))
16384 /* Only do this when substitution turns a dependent call
16385 into a non-dependent call. */
16386 && type_dependent_expression_p_push (t
)
16387 && !any_type_dependent_arguments_p (call_args
))
16388 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
16390 if (identifier_p (function
)
16391 && !any_type_dependent_arguments_p (call_args
))
16393 if (koenig_p
&& (complain
& tf_warning_or_error
))
16395 /* For backwards compatibility and good diagnostics, try
16396 the unqualified lookup again if we aren't in SFINAE
16398 tree unq
= (tsubst_copy_and_build
16399 (function
, args
, complain
, in_decl
, true,
16400 integral_constant_expression_p
));
16401 if (unq
== error_mark_node
)
16402 RETURN (error_mark_node
);
16404 if (unq
!= function
)
16407 if (INDIRECT_REF_P (fn
))
16408 fn
= TREE_OPERAND (fn
, 0);
16409 if (TREE_CODE (fn
) == COMPONENT_REF
)
16410 fn
= TREE_OPERAND (fn
, 1);
16411 if (is_overloaded_fn (fn
))
16412 fn
= get_first_fn (fn
);
16413 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
16414 "%qD was not declared in this scope, "
16415 "and no declarations were found by "
16416 "argument-dependent lookup at the point "
16417 "of instantiation", function
))
16420 /* Can't say anything more. */;
16421 else if (DECL_CLASS_SCOPE_P (fn
))
16423 location_t loc
= EXPR_LOC_OR_LOC (t
,
16426 "declarations in dependent base %qT are "
16427 "not found by unqualified lookup",
16428 DECL_CLASS_CONTEXT (fn
));
16429 if (current_class_ptr
)
16431 "use %<this->%D%> instead", function
);
16434 "use %<%T::%D%> instead",
16435 current_class_name
, function
);
16438 inform (DECL_SOURCE_LOCATION (fn
),
16439 "%qD declared here, later in the "
16440 "translation unit", fn
);
16445 if (identifier_p (function
))
16447 if (complain
& tf_error
)
16448 unqualified_name_lookup_error (function
);
16449 release_tree_vector (call_args
);
16450 RETURN (error_mark_node
);
16454 /* Remember that there was a reference to this entity. */
16455 if (DECL_P (function
)
16456 && !mark_used (function
, complain
) && !(complain
& tf_error
))
16457 RETURN (error_mark_node
);
16459 /* Put back tf_decltype for the actual call. */
16460 complain
|= decltype_flag
;
16462 if (TREE_CODE (function
) == OFFSET_REF
)
16463 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
16465 else if (TREE_CODE (function
) == COMPONENT_REF
)
16467 tree instance
= TREE_OPERAND (function
, 0);
16468 tree fn
= TREE_OPERAND (function
, 1);
16470 if (processing_template_decl
16471 && (type_dependent_expression_p (instance
)
16472 || (!BASELINK_P (fn
)
16473 && TREE_CODE (fn
) != FIELD_DECL
)
16474 || type_dependent_expression_p (fn
)
16475 || any_type_dependent_arguments_p (call_args
)))
16476 ret
= build_nt_call_vec (function
, call_args
);
16477 else if (!BASELINK_P (fn
))
16478 ret
= finish_call_expr (function
, &call_args
,
16479 /*disallow_virtual=*/false,
16480 /*koenig_p=*/false,
16483 ret
= (build_new_method_call
16485 &call_args
, NULL_TREE
,
16486 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
16491 ret
= finish_call_expr (function
, &call_args
,
16492 /*disallow_virtual=*/qualified_p
,
16496 release_tree_vector (call_args
);
16503 tree cond
= RECUR (TREE_OPERAND (t
, 0));
16504 tree folded_cond
= fold_non_dependent_expr (cond
);
16507 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
16509 if (integer_zerop (folded_cond
))
16511 ++c_inhibit_evaluation_warnings
;
16512 exp1
= RECUR (TREE_OPERAND (t
, 1));
16513 --c_inhibit_evaluation_warnings
;
16514 exp2
= RECUR (TREE_OPERAND (t
, 2));
16518 exp1
= RECUR (TREE_OPERAND (t
, 1));
16519 ++c_inhibit_evaluation_warnings
;
16520 exp2
= RECUR (TREE_OPERAND (t
, 2));
16521 --c_inhibit_evaluation_warnings
;
16523 cond
= folded_cond
;
16527 exp1
= RECUR (TREE_OPERAND (t
, 1));
16528 exp2
= RECUR (TREE_OPERAND (t
, 2));
16531 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
16532 cond
, exp1
, exp2
, complain
));
16535 case PSEUDO_DTOR_EXPR
:
16537 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16538 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16539 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16540 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
16546 tree purpose
, value
, chain
;
16548 if (t
== void_list_node
)
16551 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
16552 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
16554 /* We have pack expansions, so expand those and
16555 create a new list out of it. */
16556 tree purposevec
= NULL_TREE
;
16557 tree valuevec
= NULL_TREE
;
16561 /* Expand the argument expressions. */
16562 if (TREE_PURPOSE (t
))
16563 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
16564 complain
, in_decl
);
16565 if (TREE_VALUE (t
))
16566 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
16567 complain
, in_decl
);
16569 /* Build the rest of the list. */
16570 chain
= TREE_CHAIN (t
);
16571 if (chain
&& chain
!= void_type_node
)
16572 chain
= RECUR (chain
);
16574 /* Determine the number of arguments. */
16575 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
16577 len
= TREE_VEC_LENGTH (purposevec
);
16578 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
16580 else if (TREE_CODE (valuevec
) == TREE_VEC
)
16581 len
= TREE_VEC_LENGTH (valuevec
);
16584 /* Since we only performed a partial substitution into
16585 the argument pack, we only RETURN (a single list
16587 if (purposevec
== TREE_PURPOSE (t
)
16588 && valuevec
== TREE_VALUE (t
)
16589 && chain
== TREE_CHAIN (t
))
16592 RETURN (tree_cons (purposevec
, valuevec
, chain
));
16595 /* Convert the argument vectors into a TREE_LIST */
16599 /* Grab the Ith values. */
16601 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
16604 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
16607 /* Build the list (backwards). */
16608 chain
= tree_cons (purpose
, value
, chain
);
16614 purpose
= TREE_PURPOSE (t
);
16616 purpose
= RECUR (purpose
);
16617 value
= TREE_VALUE (t
);
16619 value
= RECUR (value
);
16620 chain
= TREE_CHAIN (t
);
16621 if (chain
&& chain
!= void_type_node
)
16622 chain
= RECUR (chain
);
16623 if (purpose
== TREE_PURPOSE (t
)
16624 && value
== TREE_VALUE (t
)
16625 && chain
== TREE_CHAIN (t
))
16627 RETURN (tree_cons (purpose
, value
, chain
));
16630 case COMPONENT_REF
:
16637 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16638 args
, complain
, in_decl
);
16639 /* Remember that there was a reference to this entity. */
16640 if (DECL_P (object
)
16641 && !mark_used (object
, complain
) && !(complain
& tf_error
))
16642 RETURN (error_mark_node
);
16643 object_type
= TREE_TYPE (object
);
16645 member
= TREE_OPERAND (t
, 1);
16646 if (BASELINK_P (member
))
16647 member
= tsubst_baselink (member
,
16648 non_reference (TREE_TYPE (object
)),
16649 args
, complain
, in_decl
);
16651 member
= tsubst_copy (member
, args
, complain
, in_decl
);
16652 if (member
== error_mark_node
)
16653 RETURN (error_mark_node
);
16655 if (type_dependent_expression_p (object
))
16656 /* We can't do much here. */;
16657 else if (!CLASS_TYPE_P (object_type
))
16659 if (scalarish_type_p (object_type
))
16661 tree s
= NULL_TREE
;
16662 tree dtor
= member
;
16664 if (TREE_CODE (dtor
) == SCOPE_REF
)
16666 s
= TREE_OPERAND (dtor
, 0);
16667 dtor
= TREE_OPERAND (dtor
, 1);
16669 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
16671 dtor
= TREE_OPERAND (dtor
, 0);
16673 RETURN (finish_pseudo_destructor_expr
16674 (object
, s
, dtor
, input_location
));
16678 else if (TREE_CODE (member
) == SCOPE_REF
16679 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
16681 /* Lookup the template functions now that we know what the
16683 tree scope
= TREE_OPERAND (member
, 0);
16684 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
16685 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
16686 member
= lookup_qualified_name (scope
, tmpl
,
16687 /*is_type_p=*/false,
16688 /*complain=*/false);
16689 if (BASELINK_P (member
))
16691 BASELINK_FUNCTIONS (member
)
16692 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
16694 member
= (adjust_result_of_qualified_name_lookup
16695 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
16700 qualified_name_lookup_error (scope
, tmpl
, member
,
16702 RETURN (error_mark_node
);
16705 else if (TREE_CODE (member
) == SCOPE_REF
16706 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
16707 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
16709 if (complain
& tf_error
)
16711 if (TYPE_P (TREE_OPERAND (member
, 0)))
16712 error ("%qT is not a class or namespace",
16713 TREE_OPERAND (member
, 0));
16715 error ("%qD is not a class or namespace",
16716 TREE_OPERAND (member
, 0));
16718 RETURN (error_mark_node
);
16720 else if (TREE_CODE (member
) == FIELD_DECL
)
16722 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
16723 if (TREE_CODE (r
) == COMPONENT_REF
)
16724 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16728 r
= finish_class_member_access_expr (object
, member
,
16729 /*template_p=*/false,
16731 if (TREE_CODE (r
) == COMPONENT_REF
)
16732 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16737 RETURN (build_throw
16738 (RECUR (TREE_OPERAND (t
, 0))));
16742 vec
<constructor_elt
, va_gc
> *n
;
16743 constructor_elt
*ce
;
16744 unsigned HOST_WIDE_INT idx
;
16745 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16746 bool process_index_p
;
16748 bool need_copy_p
= false;
16751 if (type
== error_mark_node
)
16752 RETURN (error_mark_node
);
16754 /* digest_init will do the wrong thing if we let it. */
16755 if (type
&& TYPE_PTRMEMFUNC_P (type
))
16758 /* We do not want to process the index of aggregate
16759 initializers as they are identifier nodes which will be
16760 looked up by digest_init. */
16761 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
16763 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
16764 newlen
= vec_safe_length (n
);
16765 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
16767 if (ce
->index
&& process_index_p
16768 /* An identifier index is looked up in the type
16769 being initialized, not the current scope. */
16770 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
16771 ce
->index
= RECUR (ce
->index
);
16773 if (PACK_EXPANSION_P (ce
->value
))
16775 /* Substitute into the pack expansion. */
16776 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
16779 if (ce
->value
== error_mark_node
16780 || PACK_EXPANSION_P (ce
->value
))
16782 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
16783 /* Just move the argument into place. */
16784 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
16787 /* Update the length of the final CONSTRUCTOR
16788 arguments vector, and note that we will need to
16790 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
16791 need_copy_p
= true;
16795 ce
->value
= RECUR (ce
->value
);
16800 vec
<constructor_elt
, va_gc
> *old_n
= n
;
16802 vec_alloc (n
, newlen
);
16803 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
16805 if (TREE_CODE (ce
->value
) == TREE_VEC
)
16807 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
16808 for (i
= 0; i
< len
; ++i
)
16809 CONSTRUCTOR_APPEND_ELT (n
, 0,
16810 TREE_VEC_ELT (ce
->value
, i
));
16813 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
16817 r
= build_constructor (init_list_type_node
, n
);
16818 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
16820 if (TREE_HAS_CONSTRUCTOR (t
))
16821 RETURN (finish_compound_literal (type
, r
, complain
));
16823 TREE_TYPE (r
) = type
;
16829 tree operand_0
= TREE_OPERAND (t
, 0);
16830 if (TYPE_P (operand_0
))
16832 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
16833 RETURN (get_typeid (operand_0
, complain
));
16837 operand_0
= RECUR (operand_0
);
16838 RETURN (build_typeid (operand_0
, complain
));
16845 else if (DECL_PACK_P (t
))
16847 /* We don't build decls for an instantiation of a
16848 variadic capture proxy, we instantiate the elements
16850 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
16851 return RECUR (DECL_VALUE_EXPR (t
));
16857 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
16858 /* ??? We're doing a subset of finish_id_expression here. */
16860 && !processing_template_decl
16861 && !cp_unevaluated_operand
16862 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
16863 && CP_DECL_THREAD_LOCAL_P (r
))
16865 if (tree wrap
= get_tls_wrapper_fn (r
))
16866 /* Replace an evaluated use of the thread_local variable with
16867 a call to its wrapper. */
16868 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
16870 else if (outer_automatic_var_p (r
))
16872 r
= process_outer_var_ref (r
, complain
);
16873 if (is_capture_proxy (r
))
16874 register_local_specialization (r
, t
);
16877 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
16878 /* If the original type was a reference, we'll be wrapped in
16879 the appropriate INDIRECT_REF. */
16880 r
= convert_from_reference (r
);
16886 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16887 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16888 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
16891 case OFFSETOF_EXPR
:
16892 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
16893 EXPR_LOCATION (t
)));
16897 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
16898 complain
, in_decl
);
16900 tree type2
= TRAIT_EXPR_TYPE2 (t
);
16901 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
16902 type2
= RECUR (type2
);
16904 type2
= tsubst (type2
, args
, complain
, in_decl
);
16906 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
16911 tree old_stmt_expr
= cur_stmt_expr
;
16912 tree stmt_expr
= begin_stmt_expr ();
16914 cur_stmt_expr
= stmt_expr
;
16915 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
16916 integral_constant_expression_p
);
16917 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
16918 cur_stmt_expr
= old_stmt_expr
;
16920 /* If the resulting list of expression statement is empty,
16921 fold it further into void_node. */
16922 if (empty_expr_stmt_p (stmt_expr
))
16923 stmt_expr
= void_node
;
16925 RETURN (stmt_expr
);
16930 tree r
= build_lambda_expr ();
16932 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
16933 LAMBDA_EXPR_CLOSURE (r
) = type
;
16934 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
16936 LAMBDA_EXPR_LOCATION (r
)
16937 = LAMBDA_EXPR_LOCATION (t
);
16938 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16939 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16940 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16941 LAMBDA_EXPR_DISCRIMINATOR (r
)
16942 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
16943 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
16945 /* No substitution needed. */;
16946 else if (VAR_OR_FUNCTION_DECL_P (scope
))
16947 /* For a function or variable scope, we want to use tsubst so that we
16948 don't complain about referring to an auto before deduction. */
16949 scope
= tsubst (scope
, args
, complain
, in_decl
);
16950 else if (TREE_CODE (scope
) == PARM_DECL
)
16952 /* Look up the parameter we want directly, as tsubst_copy
16953 doesn't do what we need. */
16954 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
16955 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
16956 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
16957 parm
= DECL_CHAIN (parm
);
16959 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16960 if (DECL_CONTEXT (scope
) == NULL_TREE
)
16961 DECL_CONTEXT (scope
) = fn
;
16963 else if (TREE_CODE (scope
) == FIELD_DECL
)
16964 /* For a field, use tsubst_copy so that we look up the existing field
16965 rather than build a new one. */
16966 scope
= RECUR (scope
);
16968 gcc_unreachable ();
16969 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
16970 LAMBDA_EXPR_RETURN_TYPE (r
)
16971 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
16973 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16974 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16976 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16977 determine_visibility (TYPE_NAME (type
));
16978 /* Now that we know visibility, instantiate the type so we have a
16979 declaration of the op() for later calls to lambda_function. */
16980 complete_type (type
);
16982 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
16984 insert_pending_capture_proxies ();
16986 RETURN (build_lambda_object (r
));
16990 /* We can get here for a constant initializer of non-dependent type.
16991 FIXME stop folding in cp_parser_initializer_clause. */
16993 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
16998 case TRANSACTION_EXPR
:
16999 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
17000 integral_constant_expression_p
));
17003 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17005 case VEC_PERM_EXPR
:
17007 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17008 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17009 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17010 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17014 case REQUIRES_EXPR
:
17015 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17018 /* Handle Objective-C++ constructs, if appropriate. */
17021 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17022 in_decl
, /*function_p=*/false);
17026 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17032 input_location
= loc
;
17036 /* Verify that the instantiated ARGS are valid. For type arguments,
17037 make sure that the type's linkage is ok. For non-type arguments,
17038 make sure they are constants if they are integral or enumerations.
17039 Emit an error under control of COMPLAIN, and return TRUE on error. */
17042 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
17044 if (dependent_template_arg_p (t
))
17046 if (ARGUMENT_PACK_P (t
))
17048 tree vec
= ARGUMENT_PACK_ARGS (t
);
17049 int len
= TREE_VEC_LENGTH (vec
);
17050 bool result
= false;
17053 for (i
= 0; i
< len
; ++i
)
17054 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
17058 else if (TYPE_P (t
))
17060 /* [basic.link]: A name with no linkage (notably, the name
17061 of a class or enumeration declared in a local scope)
17062 shall not be used to declare an entity with linkage.
17063 This implies that names with no linkage cannot be used as
17066 DR 757 relaxes this restriction for C++0x. */
17067 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
17068 : no_linkage_check (t
, /*relaxed_p=*/false));
17072 /* DR 488 makes use of a type with no linkage cause
17073 type deduction to fail. */
17074 if (complain
& tf_error
)
17076 if (TYPE_ANONYMOUS_P (nt
))
17077 error ("%qT is/uses anonymous type", t
);
17079 error ("template argument for %qD uses local type %qT",
17084 /* In order to avoid all sorts of complications, we do not
17085 allow variably-modified types as template arguments. */
17086 else if (variably_modified_type_p (t
, NULL_TREE
))
17088 if (complain
& tf_error
)
17089 error ("%qT is a variably modified type", t
);
17093 /* Class template and alias template arguments should be OK. */
17094 else if (DECL_TYPE_TEMPLATE_P (t
))
17096 /* A non-type argument of integral or enumerated type must be a
17098 else if (TREE_TYPE (t
)
17099 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
17100 && !REFERENCE_REF_P (t
)
17101 && !TREE_CONSTANT (t
))
17103 if (complain
& tf_error
)
17104 error ("integral expression %qE is not constant", t
);
17111 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
17113 int ix
, len
= DECL_NTPARMS (tmpl
);
17114 bool result
= false;
17116 for (ix
= 0; ix
!= len
; ix
++)
17118 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
17121 if (result
&& (complain
& tf_error
))
17122 error (" trying to instantiate %qD", tmpl
);
17126 /* We're out of SFINAE context now, so generate diagnostics for the access
17127 errors we saw earlier when instantiating D from TMPL and ARGS. */
17130 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
17132 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17133 tree type
= TREE_TYPE (pattern
);
17134 location_t loc
= input_location
;
17136 push_access_scope (d
);
17137 push_deferring_access_checks (dk_no_deferred
);
17138 input_location
= DECL_SOURCE_LOCATION (pattern
);
17139 tsubst (type
, args
, tf_warning_or_error
, d
);
17140 input_location
= loc
;
17141 pop_deferring_access_checks ();
17142 pop_access_scope (d
);
17145 /* Instantiate the indicated variable, function, or alias template TMPL with
17146 the template arguments in TARG_PTR. */
17149 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17151 tree targ_ptr
= orig_args
;
17155 bool access_ok
= true;
17157 if (tmpl
== error_mark_node
)
17158 return error_mark_node
;
17160 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
17162 /* If this function is a clone, handle it specially. */
17163 if (DECL_CLONED_FUNCTION_P (tmpl
))
17168 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17169 DECL_CLONED_FUNCTION. */
17170 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
17171 targ_ptr
, complain
);
17172 if (spec
== error_mark_node
)
17173 return error_mark_node
;
17175 /* Look for the clone. */
17176 FOR_EACH_CLONE (clone
, spec
)
17177 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
17179 /* We should always have found the clone by now. */
17180 gcc_unreachable ();
17184 if (targ_ptr
== error_mark_node
)
17185 return error_mark_node
;
17187 /* Check to see if we already have this specialization. */
17188 gen_tmpl
= most_general_template (tmpl
);
17189 if (tmpl
!= gen_tmpl
)
17190 /* The TMPL is a partial instantiation. To get a full set of
17191 arguments we must add the arguments used to perform the
17192 partial instantiation. */
17193 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
17196 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17197 but it doesn't seem to be on the hot path. */
17198 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
17200 gcc_assert (tmpl
== gen_tmpl
17201 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
17203 || fndecl
== NULL_TREE
);
17205 if (spec
!= NULL_TREE
)
17207 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
17209 if (complain
& tf_error
)
17210 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
17211 return error_mark_node
;
17216 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
17218 return error_mark_node
;
17220 /* We are building a FUNCTION_DECL, during which the access of its
17221 parameters and return types have to be checked. However this
17222 FUNCTION_DECL which is the desired context for access checking
17223 is not built yet. We solve this chicken-and-egg problem by
17224 deferring all checks until we have the FUNCTION_DECL. */
17225 push_deferring_access_checks (dk_deferred
);
17227 /* Instantiation of the function happens in the context of the function
17228 template, not the context of the overload resolution we're doing. */
17229 push_to_top_level ();
17230 /* If there are dependent arguments, e.g. because we're doing partial
17231 ordering, make sure processing_template_decl stays set. */
17232 if (uses_template_parms (targ_ptr
))
17233 ++processing_template_decl
;
17234 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17236 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
17237 complain
, gen_tmpl
, true);
17238 push_nested_class (ctx
);
17241 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17243 if (VAR_P (pattern
))
17245 /* We need to determine if we're using a partial or explicit
17246 specialization now, because the type of the variable could be
17248 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
17249 tree elt
= most_specialized_partial_spec (tid
, complain
);
17250 if (elt
== error_mark_node
)
17251 pattern
= error_mark_node
;
17254 tmpl
= TREE_VALUE (elt
);
17255 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17256 targ_ptr
= TREE_PURPOSE (elt
);
17260 /* Substitute template parameters to obtain the specialization. */
17261 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
17262 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17263 pop_nested_class ();
17264 pop_from_top_level ();
17266 if (fndecl
== error_mark_node
)
17268 pop_deferring_access_checks ();
17269 return error_mark_node
;
17272 /* The DECL_TI_TEMPLATE should always be the immediate parent
17273 template, not the most general template. */
17274 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
17275 DECL_TI_ARGS (fndecl
) = targ_ptr
;
17277 /* Now we know the specialization, compute access previously
17279 push_access_scope (fndecl
);
17280 if (!perform_deferred_access_checks (complain
))
17282 pop_access_scope (fndecl
);
17283 pop_deferring_access_checks ();
17285 /* If we've just instantiated the main entry point for a function,
17286 instantiate all the alternate entry points as well. We do this
17287 by cloning the instantiation of the main entry point, not by
17288 instantiating the template clones. */
17289 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
17290 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
17294 if (!(complain
& tf_error
))
17296 /* Remember to reinstantiate when we're out of SFINAE so the user
17297 can see the errors. */
17298 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
17300 return error_mark_node
;
17305 /* Wrapper for instantiate_template_1. */
17308 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17311 timevar_push (TV_TEMPLATE_INST
);
17312 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
17313 timevar_pop (TV_TEMPLATE_INST
);
17317 /* Instantiate the alias template TMPL with ARGS. Also push a template
17318 instantiation level, which instantiate_template doesn't do because
17319 functions and variables have sufficient context established by the
17323 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
17325 struct pending_template
*old_last_pend
= last_pending_template
;
17326 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17327 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
17328 return error_mark_node
;
17329 tree tinst
= build_tree_list (tmpl
, args
);
17330 if (!push_tinst_level (tinst
))
17333 return error_mark_node
;
17337 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
17338 args
, tmpl
, complain
,
17339 /*require_all_args=*/true,
17340 /*use_default_args=*/true);
17342 tree r
= instantiate_template (tmpl
, args
, complain
);
17343 pop_tinst_level ();
17344 /* We can't free this if a pending_template entry or last_error_tinst_level
17345 is pointing at it. */
17346 if (last_pending_template
== old_last_pend
17347 && last_error_tinst_level
== old_error_tinst
)
17353 /* PARM is a template parameter pack for FN. Returns true iff
17354 PARM is used in a deducible way in the argument list of FN. */
17357 pack_deducible_p (tree parm
, tree fn
)
17359 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
17360 for (; t
; t
= TREE_CHAIN (t
))
17362 tree type
= TREE_VALUE (t
);
17364 if (!PACK_EXPANSION_P (type
))
17366 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
17367 packs
; packs
= TREE_CHAIN (packs
))
17368 if (template_args_equal (TREE_VALUE (packs
), parm
))
17370 /* The template parameter pack is used in a function parameter
17371 pack. If this is the end of the parameter list, the
17372 template parameter pack is deducible. */
17373 if (TREE_CHAIN (t
) == void_list_node
)
17376 /* Otherwise, not. Well, it could be deduced from
17377 a non-pack parameter, but doing so would end up with
17378 a deduction mismatch, so don't bother. */
17382 /* The template parameter pack isn't used in any function parameter
17383 packs, but it might be used deeper, e.g. tuple<Args...>. */
17387 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17388 NARGS elements of the arguments that are being used when calling
17389 it. TARGS is a vector into which the deduced template arguments
17392 Returns either a FUNCTION_DECL for the matching specialization of FN or
17393 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17394 true, diagnostics will be printed to explain why it failed.
17396 If FN is a conversion operator, or we are trying to produce a specific
17397 specialization, RETURN_TYPE is the return type desired.
17399 The EXPLICIT_TARGS are explicit template arguments provided via a
17402 The parameter STRICT is one of:
17405 We are deducing arguments for a function call, as in
17406 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17407 deducing arguments for a call to the result of a conversion
17408 function template, as in [over.call.object].
17411 We are deducing arguments for a conversion function, as in
17412 [temp.deduct.conv].
17415 We are deducing arguments when doing an explicit instantiation
17416 as in [temp.explicit], when determining an explicit specialization
17417 as in [temp.expl.spec], or when taking the address of a function
17418 template, as in [temp.deduct.funcaddr]. */
17421 fn_type_unification (tree fn
,
17422 tree explicit_targs
,
17425 unsigned int nargs
,
17427 unification_kind_t strict
,
17434 tree decl
= NULL_TREE
;
17435 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
17437 static int deduction_depth
;
17438 struct pending_template
*old_last_pend
= last_pending_template
;
17439 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17440 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
17442 tree r
= error_mark_node
;
17445 complain
|= tf_decltype
;
17447 /* In C++0x, it's possible to have a function template whose type depends
17448 on itself recursively. This is most obvious with decltype, but can also
17449 occur with enumeration scope (c++/48969). So we need to catch infinite
17450 recursion and reject the substitution at deduction time; this function
17451 will return error_mark_node for any repeated substitution.
17453 This also catches excessive recursion such as when f<N> depends on
17454 f<N-1> across all integers, and returns error_mark_node for all the
17455 substitutions back up to the initial one.
17457 This is, of course, not reentrant. */
17458 if (excessive_deduction_depth
)
17459 return error_mark_node
;
17460 tinst
= build_tree_list (fn
, NULL_TREE
);
17463 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
17465 fntype
= TREE_TYPE (fn
);
17466 if (explicit_targs
)
17470 The specified template arguments must match the template
17471 parameters in kind (i.e., type, nontype, template), and there
17472 must not be more arguments than there are parameters;
17473 otherwise type deduction fails.
17475 Nontype arguments must match the types of the corresponding
17476 nontype template parameters, or must be convertible to the
17477 types of the corresponding nontype parameters as specified in
17478 _temp.arg.nontype_, otherwise type deduction fails.
17480 All references in the function type of the function template
17481 to the corresponding template parameters are replaced by the
17482 specified template argument values. If a substitution in a
17483 template parameter or in the function type of the function
17484 template results in an invalid type, type deduction fails. */
17485 int i
, len
= TREE_VEC_LENGTH (tparms
);
17486 location_t loc
= input_location
;
17487 bool incomplete
= false;
17489 /* Adjust any explicit template arguments before entering the
17490 substitution context. */
17492 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
17494 /*require_all_args=*/false,
17495 /*use_default_args=*/false));
17496 if (explicit_targs
== error_mark_node
)
17499 /* Substitute the explicit args into the function type. This is
17500 necessary so that, for instance, explicitly declared function
17501 arguments can match null pointed constants. If we were given
17502 an incomplete set of explicit args, we must not do semantic
17503 processing during substitution as we could create partial
17505 for (i
= 0; i
< len
; i
++)
17507 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
17508 bool parameter_pack
= false;
17509 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
17511 /* Dig out the actual parm. */
17512 if (TREE_CODE (parm
) == TYPE_DECL
17513 || TREE_CODE (parm
) == TEMPLATE_DECL
)
17515 parm
= TREE_TYPE (parm
);
17516 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
17518 else if (TREE_CODE (parm
) == PARM_DECL
)
17520 parm
= DECL_INITIAL (parm
);
17521 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
17524 if (!parameter_pack
&& targ
== NULL_TREE
)
17525 /* No explicit argument for this template parameter. */
17528 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
17530 /* Mark the argument pack as "incomplete". We could
17531 still deduce more arguments during unification.
17532 We remove this mark in type_unification_real. */
17535 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
17536 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
17537 = ARGUMENT_PACK_ARGS (targ
);
17540 /* We have some incomplete argument packs. */
17545 TREE_VALUE (tinst
) = explicit_targs
;
17546 if (!push_tinst_level (tinst
))
17548 excessive_deduction_depth
= true;
17551 processing_template_decl
+= incomplete
;
17552 input_location
= DECL_SOURCE_LOCATION (fn
);
17553 /* Ignore any access checks; we'll see them again in
17554 instantiate_template and they might have the wrong
17555 access path at this point. */
17556 push_deferring_access_checks (dk_deferred
);
17557 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
17558 complain
| tf_partial
, NULL_TREE
);
17559 pop_deferring_access_checks ();
17560 input_location
= loc
;
17561 processing_template_decl
-= incomplete
;
17562 pop_tinst_level ();
17564 if (fntype
== error_mark_node
)
17567 /* Place the explicitly specified arguments in TARGS. */
17568 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
17569 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
17572 /* Never do unification on the 'this' parameter. */
17573 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
17575 if (return_type
&& strict
== DEDUCE_CALL
)
17577 /* We're deducing for a call to the result of a template conversion
17578 function. The parms we really want are in return_type. */
17579 if (POINTER_TYPE_P (return_type
))
17580 return_type
= TREE_TYPE (return_type
);
17581 parms
= TYPE_ARG_TYPES (return_type
);
17583 else if (return_type
)
17587 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
17588 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
17589 new_args
[0] = return_type
;
17590 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
17595 /* We allow incomplete unification without an error message here
17596 because the standard doesn't seem to explicitly prohibit it. Our
17597 callers must be ready to deal with unification failures in any
17600 TREE_VALUE (tinst
) = targs
;
17601 /* If we aren't explaining yet, push tinst context so we can see where
17602 any errors (e.g. from class instantiations triggered by instantiation
17603 of default template arguments) come from. If we are explaining, this
17604 context is redundant. */
17605 if (!explain_p
&& !push_tinst_level (tinst
))
17607 excessive_deduction_depth
= true;
17611 /* type_unification_real will pass back any access checks from default
17612 template argument substitution. */
17613 vec
<deferred_access_check
, va_gc
> *checks
;
17616 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17617 targs
, parms
, args
, nargs
, /*subr=*/0,
17618 strict
, flags
, &checks
, explain_p
);
17620 pop_tinst_level ();
17624 /* Now that we have bindings for all of the template arguments,
17625 ensure that the arguments deduced for the template template
17626 parameters have compatible template parameter lists. We cannot
17627 check this property before we have deduced all template
17628 arguments, because the template parameter types of a template
17629 template parameter might depend on prior template parameters
17630 deduced after the template template parameter. The following
17631 ill-formed example illustrates this issue:
17633 template<typename T, template<T> class C> void f(C<5>, T);
17635 template<int N> struct X {};
17638 f(X<5>(), 5l); // error: template argument deduction fails
17641 The template parameter list of 'C' depends on the template type
17642 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17643 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17644 time that we deduce 'C'. */
17645 if (!template_template_parm_bindings_ok_p
17646 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
17648 unify_inconsistent_template_template_parameters (explain_p
);
17652 /* All is well so far. Now, check:
17656 When all template arguments have been deduced, all uses of
17657 template parameters in nondeduced contexts are replaced with
17658 the corresponding deduced argument values. If the
17659 substitution results in an invalid type, as described above,
17660 type deduction fails. */
17661 TREE_VALUE (tinst
) = targs
;
17662 if (!push_tinst_level (tinst
))
17664 excessive_deduction_depth
= true;
17668 /* Also collect access checks from the instantiation. */
17669 reopen_deferring_access_checks (checks
);
17671 decl
= instantiate_template (fn
, targs
, complain
);
17673 checks
= get_deferred_access_checks ();
17674 pop_deferring_access_checks ();
17676 pop_tinst_level ();
17678 if (decl
== error_mark_node
)
17681 /* Now perform any access checks encountered during substitution. */
17682 push_access_scope (decl
);
17683 ok
= perform_access_checks (checks
, complain
);
17684 pop_access_scope (decl
);
17688 /* If we're looking for an exact match, check that what we got
17689 is indeed an exact match. It might not be if some template
17690 parameters are used in non-deduced contexts. But don't check
17691 for an exact match if we have dependent template arguments;
17692 in that case we're doing partial ordering, and we already know
17693 that we have two candidates that will provide the actual type. */
17694 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
17696 tree substed
= TREE_TYPE (decl
);
17700 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
17702 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
17703 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
17704 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
17706 unify_type_mismatch (explain_p
, args
[i
],
17707 TREE_VALUE (sarg
));
17716 if (excessive_deduction_depth
)
17718 if (deduction_depth
== 0)
17719 /* Reset once we're all the way out. */
17720 excessive_deduction_depth
= false;
17723 /* We can't free this if a pending_template entry or last_error_tinst_level
17724 is pointing at it. */
17725 if (last_pending_template
== old_last_pend
17726 && last_error_tinst_level
== old_error_tinst
)
17732 /* Adjust types before performing type deduction, as described in
17733 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17734 sections are symmetric. PARM is the type of a function parameter
17735 or the return type of the conversion function. ARG is the type of
17736 the argument passed to the call, or the type of the value
17737 initialized with the result of the conversion function.
17738 ARG_EXPR is the original argument expression, which may be null. */
17741 maybe_adjust_types_for_deduction (unification_kind_t strict
,
17754 /* Swap PARM and ARG throughout the remainder of this
17755 function; the handling is precisely symmetric since PARM
17756 will initialize ARG rather than vice versa. */
17757 std::swap (parm
, arg
);
17761 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17762 too, but here handle it by stripping the reference from PARM
17763 rather than by adding it to ARG. */
17764 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17765 && TYPE_REF_IS_RVALUE (*parm
)
17766 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17767 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17768 && TREE_CODE (*arg
) == REFERENCE_TYPE
17769 && !TYPE_REF_IS_RVALUE (*arg
))
17770 *parm
= TREE_TYPE (*parm
);
17771 /* Nothing else to do in this case. */
17775 gcc_unreachable ();
17778 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
17780 /* [temp.deduct.call]
17782 If P is not a reference type:
17784 --If A is an array type, the pointer type produced by the
17785 array-to-pointer standard conversion (_conv.array_) is
17786 used in place of A for type deduction; otherwise,
17788 --If A is a function type, the pointer type produced by
17789 the function-to-pointer standard conversion
17790 (_conv.func_) is used in place of A for type deduction;
17793 --If A is a cv-qualified type, the top level
17794 cv-qualifiers of A's type are ignored for type
17796 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
17797 *arg
= build_pointer_type (TREE_TYPE (*arg
));
17798 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
17799 *arg
= build_pointer_type (*arg
);
17801 *arg
= TYPE_MAIN_VARIANT (*arg
);
17804 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17805 of the form T&&, where T is a template parameter, and the argument
17806 is an lvalue, T is deduced as A& */
17807 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17808 && TYPE_REF_IS_RVALUE (*parm
)
17809 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17810 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17811 && (arg_expr
? real_lvalue_p (arg_expr
)
17812 /* try_one_overload doesn't provide an arg_expr, but
17813 functions are always lvalues. */
17814 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
17815 *arg
= build_reference_type (*arg
);
17817 /* [temp.deduct.call]
17819 If P is a cv-qualified type, the top level cv-qualifiers
17820 of P's type are ignored for type deduction. If P is a
17821 reference type, the type referred to by P is used for
17823 *parm
= TYPE_MAIN_VARIANT (*parm
);
17824 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
17826 *parm
= TREE_TYPE (*parm
);
17827 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17830 /* DR 322. For conversion deduction, remove a reference type on parm
17831 too (which has been swapped into ARG). */
17832 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
17833 *arg
= TREE_TYPE (*arg
);
17838 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17839 template which does contain any deducible template parameters; check if
17840 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17841 unify_one_argument. */
17844 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
17845 int flags
, bool explain_p
)
17850 type
= TREE_TYPE (arg
);
17854 if (same_type_p (parm
, type
))
17855 return unify_success (explain_p
);
17857 if (strict
== DEDUCE_CONV
)
17859 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
17860 explain_p
? tf_warning_or_error
: tf_none
))
17861 return unify_success (explain_p
);
17863 else if (strict
!= DEDUCE_EXACT
)
17865 if (can_convert_arg (parm
, type
,
17866 TYPE_P (arg
) ? NULL_TREE
: arg
,
17867 flags
, explain_p
? tf_warning_or_error
: tf_none
))
17868 return unify_success (explain_p
);
17871 if (strict
== DEDUCE_EXACT
)
17872 return unify_type_mismatch (explain_p
, parm
, arg
);
17874 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
17877 static bool uses_deducible_template_parms (tree type
);
17879 /* Returns true iff the expression EXPR is one from which a template
17880 argument can be deduced. In other words, if it's an undecorated
17881 use of a template non-type parameter. */
17884 deducible_expression (tree expr
)
17886 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
17889 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17890 deducible way; that is, if it has a max value of <PARM> - 1. */
17893 deducible_array_bound (tree domain
)
17895 if (domain
== NULL_TREE
)
17898 tree max
= TYPE_MAX_VALUE (domain
);
17899 if (TREE_CODE (max
) != MINUS_EXPR
)
17902 return deducible_expression (TREE_OPERAND (max
, 0));
17905 /* Returns true iff the template arguments ARGS use a template parameter
17906 in a deducible way. */
17909 deducible_template_args (tree args
)
17911 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
17914 tree elt
= TREE_VEC_ELT (args
, i
);
17915 if (ARGUMENT_PACK_P (elt
))
17916 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
17919 if (PACK_EXPANSION_P (elt
))
17920 elt
= PACK_EXPANSION_PATTERN (elt
);
17921 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
17923 else if (TYPE_P (elt
))
17924 deducible
= uses_deducible_template_parms (elt
);
17926 deducible
= deducible_expression (elt
);
17934 /* Returns true iff TYPE contains any deducible references to template
17935 parameters, as per 14.8.2.5. */
17938 uses_deducible_template_parms (tree type
)
17940 if (PACK_EXPANSION_P (type
))
17941 type
= PACK_EXPANSION_PATTERN (type
);
17948 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17949 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17955 if (POINTER_TYPE_P (type
))
17956 return uses_deducible_template_parms (TREE_TYPE (type
));
17958 /* T[integer-constant ]
17960 if (TREE_CODE (type
) == ARRAY_TYPE
)
17961 return (uses_deducible_template_parms (TREE_TYPE (type
))
17962 || deducible_array_bound (TYPE_DOMAIN (type
)));
17974 if (TYPE_PTRMEM_P (type
))
17975 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
17976 || (uses_deducible_template_parms
17977 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
17979 /* template-name <T> (where template-name refers to a class template)
17980 template-name <i> (where template-name refers to a class template) */
17981 if (CLASS_TYPE_P (type
)
17982 && CLASSTYPE_TEMPLATE_INFO (type
)
17983 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
17984 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17985 (CLASSTYPE_TI_ARGS (type
)));
17990 if (TREE_CODE (type
) == FUNCTION_TYPE
17991 || TREE_CODE (type
) == METHOD_TYPE
)
17993 if (uses_deducible_template_parms (TREE_TYPE (type
)))
17995 tree parm
= TYPE_ARG_TYPES (type
);
17996 if (TREE_CODE (type
) == METHOD_TYPE
)
17997 parm
= TREE_CHAIN (parm
);
17998 for (; parm
; parm
= TREE_CHAIN (parm
))
17999 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
18006 /* Subroutine of type_unification_real and unify_pack_expansion to
18007 handle unification of a single P/A pair. Parameters are as
18008 for those functions. */
18011 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
18012 int subr
, unification_kind_t strict
,
18015 tree arg_expr
= NULL_TREE
;
18018 if (arg
== error_mark_node
|| parm
== error_mark_node
)
18019 return unify_invalid (explain_p
);
18020 if (arg
== unknown_type_node
)
18021 /* We can't deduce anything from this, but we might get all the
18022 template args from other function args. */
18023 return unify_success (explain_p
);
18025 /* Implicit conversions (Clause 4) will be performed on a function
18026 argument to convert it to the type of the corresponding function
18027 parameter if the parameter type contains no template-parameters that
18028 participate in template argument deduction. */
18029 if (strict
!= DEDUCE_EXACT
18030 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
18031 /* For function parameters with no deducible template parameters,
18032 just return. We'll check non-dependent conversions later. */
18033 return unify_success (explain_p
);
18038 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
18039 | UNIFY_ALLOW_MORE_CV_QUAL
18040 | UNIFY_ALLOW_DERIVED
);
18044 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
18048 arg_strict
= UNIFY_ALLOW_NONE
;
18052 gcc_unreachable ();
18055 /* We only do these transformations if this is the top-level
18056 parameter_type_list in a call or declaration matching; in other
18057 situations (nested function declarators, template argument lists) we
18058 won't be comparing a type to an expression, and we don't do any type
18064 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
18065 if (type_unknown_p (arg
))
18067 /* [temp.deduct.type] A template-argument can be
18068 deduced from a pointer to function or pointer
18069 to member function argument if the set of
18070 overloaded functions does not contain function
18071 templates and at most one of a set of
18072 overloaded functions provides a unique
18075 if (resolve_overloaded_unification
18076 (tparms
, targs
, parm
, arg
, strict
,
18077 arg_strict
, explain_p
))
18078 return unify_success (explain_p
);
18079 return unify_overload_resolution_failure (explain_p
, arg
);
18083 arg
= unlowered_expr_type (arg
);
18084 if (arg
== error_mark_node
)
18085 return unify_invalid (explain_p
);
18089 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
18092 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
18093 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
18094 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18096 /* For deduction from an init-list we need the actual list. */
18097 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
18099 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
18102 /* Most parms like fn_type_unification.
18104 If SUBR is 1, we're being called recursively (to unify the
18105 arguments of a function or method parameter of a function
18108 CHECKS is a pointer to a vector of access checks encountered while
18109 substituting default template arguments. */
18112 type_unification_real (tree tparms
,
18116 unsigned int xnargs
,
18118 unification_kind_t strict
,
18120 vec
<deferred_access_check
, va_gc
> **checks
,
18125 int ntparms
= TREE_VEC_LENGTH (tparms
);
18126 int saw_undeduced
= 0;
18129 unsigned int nargs
;
18132 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
18133 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
18134 gcc_assert (ntparms
> 0);
18136 /* Reset the number of non-defaulted template arguments contained
18138 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
18146 while (parms
&& parms
!= void_list_node
18149 parm
= TREE_VALUE (parms
);
18151 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18152 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
18153 /* For a function parameter pack that occurs at the end of the
18154 parameter-declaration-list, the type A of each remaining
18155 argument of the call is compared with the type P of the
18156 declarator-id of the function parameter pack. */
18159 parms
= TREE_CHAIN (parms
);
18161 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18162 /* For a function parameter pack that does not occur at the
18163 end of the parameter-declaration-list, the type of the
18164 parameter pack is a non-deduced context. */
18170 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
18176 && parms
!= void_list_node
18177 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
18179 /* Unify the remaining arguments with the pack expansion type. */
18181 tree parmvec
= make_tree_vec (1);
18183 /* Allocate a TREE_VEC and copy in all of the arguments */
18184 argvec
= make_tree_vec (nargs
- ia
);
18185 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
18186 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
18188 /* Copy the parameter into parmvec. */
18189 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
18190 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
18191 /*subr=*/subr
, explain_p
))
18194 /* Advance to the end of the list of parameters. */
18195 parms
= TREE_CHAIN (parms
);
18198 /* Fail if we've reached the end of the parm list, and more args
18199 are present, and the parm list isn't variadic. */
18200 if (ia
< nargs
&& parms
== void_list_node
)
18201 return unify_too_many_arguments (explain_p
, nargs
, ia
);
18202 /* Fail if parms are left and they don't have default values and
18203 they aren't all deduced as empty packs (c++/57397). This is
18204 consistent with sufficient_parms_p. */
18205 if (parms
&& parms
!= void_list_node
18206 && TREE_PURPOSE (parms
) == NULL_TREE
)
18208 unsigned int count
= nargs
;
18213 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
18216 p
= TREE_CHAIN (p
);
18218 while (p
&& p
!= void_list_node
);
18219 if (count
!= nargs
)
18220 return unify_too_few_arguments (explain_p
, ia
, count
,
18226 tsubst_flags_t complain
= (explain_p
18227 ? tf_warning_or_error
18230 for (i
= 0; i
< ntparms
; i
++)
18232 tree targ
= TREE_VEC_ELT (targs
, i
);
18233 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18235 /* Clear the "incomplete" flags on all argument packs now so that
18236 substituting them into later default arguments works. */
18237 if (targ
&& ARGUMENT_PACK_P (targ
))
18239 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
18240 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
18243 if (targ
|| tparm
== error_mark_node
)
18245 tparm
= TREE_VALUE (tparm
);
18247 /* If this is an undeduced nontype parameter that depends on
18248 a type parameter, try another pass; its type may have been
18249 deduced from a later argument than the one from which
18250 this parameter can be deduced. */
18251 if (TREE_CODE (tparm
) == PARM_DECL
18252 && uses_template_parms (TREE_TYPE (tparm
))
18253 && saw_undeduced
< 2)
18259 /* Core issue #226 (C++0x) [temp.deduct]:
18261 If a template argument has not been deduced, its
18262 default template argument, if any, is used.
18264 When we are in C++98 mode, TREE_PURPOSE will either
18265 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18266 to explicitly check cxx_dialect here. */
18267 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
18268 /* OK, there is a default argument. Wait until after the
18269 conversion check to do substitution. */
18272 /* If the type parameter is a parameter pack, then it will
18273 be deduced to an empty parameter pack. */
18274 if (template_parameter_pack_p (tparm
))
18278 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
18280 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
18281 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
18282 TREE_CONSTANT (arg
) = 1;
18285 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
18287 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
18289 TREE_VEC_ELT (targs
, i
) = arg
;
18293 return unify_parameter_deduction_failure (explain_p
, tparm
);
18296 /* DR 1391: All parameters have args, now check non-dependent parms for
18298 if (saw_undeduced
< 2)
18299 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
18300 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
18302 parm
= TREE_VALUE (parms
);
18304 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18305 && (!TREE_CHAIN (parms
)
18306 || TREE_CHAIN (parms
) == void_list_node
))
18307 /* For a function parameter pack that occurs at the end of the
18308 parameter-declaration-list, the type A of each remaining
18309 argument of the call is compared with the type P of the
18310 declarator-id of the function parameter pack. */
18313 parms
= TREE_CHAIN (parms
);
18315 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18316 /* For a function parameter pack that does not occur at the
18317 end of the parameter-declaration-list, the type of the
18318 parameter pack is a non-deduced context. */
18324 if (uses_template_parms (parm
))
18326 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
18331 /* Now substitute into the default template arguments. */
18332 for (i
= 0; i
< ntparms
; i
++)
18334 tree targ
= TREE_VEC_ELT (targs
, i
);
18335 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18337 if (targ
|| tparm
== error_mark_node
)
18339 tree parm
= TREE_VALUE (tparm
);
18341 if (TREE_CODE (parm
) == PARM_DECL
18342 && uses_template_parms (TREE_TYPE (parm
))
18343 && saw_undeduced
< 2)
18346 tree arg
= TREE_PURPOSE (tparm
);
18347 reopen_deferring_access_checks (*checks
);
18348 location_t save_loc
= input_location
;
18350 input_location
= DECL_SOURCE_LOCATION (parm
);
18351 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
18352 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
18354 input_location
= save_loc
;
18355 *checks
= get_deferred_access_checks ();
18356 pop_deferring_access_checks ();
18357 if (arg
== error_mark_node
)
18361 TREE_VEC_ELT (targs
, i
) = arg
;
18362 /* The position of the first default template argument,
18363 is also the number of non-defaulted arguments in TARGS.
18365 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18366 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
18371 if (saw_undeduced
++ == 1)
18375 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18376 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
18378 return unify_success (explain_p
);
18381 /* Subroutine of type_unification_real. Args are like the variables
18382 at the call site. ARG is an overloaded function (or template-id);
18383 we try deducing template args from each of the overloads, and if
18384 only one succeeds, we go with that. Modifies TARGS and returns
18385 true on success. */
18388 resolve_overloaded_unification (tree tparms
,
18392 unification_kind_t strict
,
18396 tree tempargs
= copy_node (targs
);
18398 tree goodfn
= NULL_TREE
;
18401 if (TREE_CODE (arg
) == ADDR_EXPR
)
18403 arg
= TREE_OPERAND (arg
, 0);
18409 if (TREE_CODE (arg
) == COMPONENT_REF
)
18410 /* Handle `&x' where `x' is some static or non-static member
18412 arg
= TREE_OPERAND (arg
, 1);
18414 if (TREE_CODE (arg
) == OFFSET_REF
)
18415 arg
= TREE_OPERAND (arg
, 1);
18417 /* Strip baselink information. */
18418 if (BASELINK_P (arg
))
18419 arg
= BASELINK_FUNCTIONS (arg
);
18421 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
18423 /* If we got some explicit template args, we need to plug them into
18424 the affected templates before we try to unify, in case the
18425 explicit args will completely resolve the templates in question. */
18428 tree expl_subargs
= TREE_OPERAND (arg
, 1);
18429 arg
= TREE_OPERAND (arg
, 0);
18431 for (; arg
; arg
= OVL_NEXT (arg
))
18433 tree fn
= OVL_CURRENT (arg
);
18434 tree subargs
, elem
;
18436 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18439 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18440 expl_subargs
, NULL_TREE
, tf_none
,
18441 /*require_all_args=*/true,
18442 /*use_default_args=*/true);
18443 if (subargs
!= error_mark_node
18444 && !any_dependent_template_arguments_p (subargs
))
18446 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
18447 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18448 elem
, strict
, sub_strict
, addr_p
, explain_p
)
18449 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
18458 /* If no templates (or more than one) are fully resolved by the
18459 explicit arguments, this template-id is a non-deduced context; it
18460 could still be OK if we deduce all template arguments for the
18461 enclosing call through other arguments. */
18465 else if (TREE_CODE (arg
) != OVERLOAD
18466 && TREE_CODE (arg
) != FUNCTION_DECL
)
18467 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18468 -- but the deduction does not succeed because the expression is
18469 not just the function on its own. */
18472 for (; arg
; arg
= OVL_NEXT (arg
))
18473 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18474 TREE_TYPE (OVL_CURRENT (arg
)),
18475 strict
, sub_strict
, addr_p
, explain_p
)
18476 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
18478 goodfn
= OVL_CURRENT (arg
);
18482 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18483 to function or pointer to member function argument if the set of
18484 overloaded functions does not contain function templates and at most
18485 one of a set of overloaded functions provides a unique match.
18487 So if we found multiple possibilities, we return success but don't
18488 deduce anything. */
18492 int i
= TREE_VEC_LENGTH (targs
);
18494 if (TREE_VEC_ELT (tempargs
, i
))
18496 tree old
= TREE_VEC_ELT (targs
, i
);
18497 tree new_
= TREE_VEC_ELT (tempargs
, i
);
18498 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
18499 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
18500 /* Don't forget explicit template arguments in a pack. */
18501 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
18502 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
18503 TREE_VEC_ELT (targs
, i
) = new_
;
18512 /* Core DR 115: In contexts where deduction is done and fails, or in
18513 contexts where deduction is not done, if a template argument list is
18514 specified and it, along with any default template arguments, identifies
18515 a single function template specialization, then the template-id is an
18516 lvalue for the function template specialization. */
18519 resolve_nondeduced_context (tree orig_expr
)
18521 tree expr
, offset
, baselink
;
18524 if (!type_unknown_p (orig_expr
))
18529 offset
= NULL_TREE
;
18530 baselink
= NULL_TREE
;
18532 if (TREE_CODE (expr
) == ADDR_EXPR
)
18534 expr
= TREE_OPERAND (expr
, 0);
18537 if (TREE_CODE (expr
) == OFFSET_REF
)
18540 expr
= TREE_OPERAND (expr
, 1);
18542 if (BASELINK_P (expr
))
18545 expr
= BASELINK_FUNCTIONS (expr
);
18548 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
18551 tree goodfn
= NULL_TREE
;
18553 /* If we got some explicit template args, we need to plug them into
18554 the affected templates before we try to unify, in case the
18555 explicit args will completely resolve the templates in question. */
18557 tree expl_subargs
= TREE_OPERAND (expr
, 1);
18558 tree arg
= TREE_OPERAND (expr
, 0);
18559 tree badfn
= NULL_TREE
;
18560 tree badargs
= NULL_TREE
;
18562 for (; arg
; arg
= OVL_NEXT (arg
))
18564 tree fn
= OVL_CURRENT (arg
);
18565 tree subargs
, elem
;
18567 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18570 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18571 expl_subargs
, NULL_TREE
, tf_none
,
18572 /*require_all_args=*/true,
18573 /*use_default_args=*/true);
18574 if (subargs
!= error_mark_node
18575 && !any_dependent_template_arguments_p (subargs
))
18577 elem
= instantiate_template (fn
, subargs
, tf_none
);
18578 if (elem
== error_mark_node
)
18583 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
18592 mark_used (goodfn
);
18595 expr
= build_baselink (BASELINK_BINFO (baselink
),
18596 BASELINK_ACCESS_BINFO (baselink
),
18597 expr
, BASELINK_OPTYPE (baselink
));
18601 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
18602 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
18605 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
18608 else if (good
== 0 && badargs
)
18609 /* There were no good options and at least one bad one, so let the
18610 user know what the problem is. */
18611 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
18616 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18617 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18618 different overloads deduce different arguments for a given parm.
18619 ADDR_P is true if the expression for which deduction is being
18620 performed was of the form "& fn" rather than simply "fn".
18622 Returns 1 on success. */
18625 try_one_overload (tree tparms
,
18630 unification_kind_t strict
,
18639 if (arg
== error_mark_node
)
18642 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18643 to function or pointer to member function argument if the set of
18644 overloaded functions does not contain function templates and at most
18645 one of a set of overloaded functions provides a unique match.
18647 So if this is a template, just return success. */
18649 if (uses_template_parms (arg
))
18652 if (TREE_CODE (arg
) == METHOD_TYPE
)
18653 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
18655 arg
= build_pointer_type (arg
);
18657 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
18659 /* We don't copy orig_targs for this because if we have already deduced
18660 some template args from previous args, unify would complain when we
18661 try to deduce a template parameter for the same argument, even though
18662 there isn't really a conflict. */
18663 nargs
= TREE_VEC_LENGTH (targs
);
18664 tempargs
= make_tree_vec (nargs
);
18666 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
18669 /* First make sure we didn't deduce anything that conflicts with
18670 explicitly specified args. */
18671 for (i
= nargs
; i
--; )
18673 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18674 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
18678 else if (uses_template_parms (elt
))
18679 /* Since we're unifying against ourselves, we will fill in
18680 template args used in the function parm list with our own
18681 template parms. Discard them. */
18682 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
18683 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
18687 for (i
= nargs
; i
--; )
18689 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18692 TREE_VEC_ELT (targs
, i
) = elt
;
18698 /* PARM is a template class (perhaps with unbound template
18699 parameters). ARG is a fully instantiated type. If ARG can be
18700 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18701 TARGS are as for unify. */
18704 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
18707 tree copy_of_targs
;
18709 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
18710 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
18711 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
18714 /* We need to make a new template argument vector for the call to
18715 unify. If we used TARGS, we'd clutter it up with the result of
18716 the attempted unification, even if this class didn't work out.
18717 We also don't want to commit ourselves to all the unifications
18718 we've already done, since unification is supposed to be done on
18719 an argument-by-argument basis. In other words, consider the
18720 following pathological case:
18722 template <int I, int J, int K>
18725 template <int I, int J>
18726 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18728 template <int I, int J, int K>
18729 void f(S<I, J, K>, S<I, I, I>);
18738 Now, by the time we consider the unification involving `s2', we
18739 already know that we must have `f<0, 0, 0>'. But, even though
18740 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18741 because there are two ways to unify base classes of S<0, 1, 2>
18742 with S<I, I, I>. If we kept the already deduced knowledge, we
18743 would reject the possibility I=1. */
18744 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18746 /* If unification failed, we're done. */
18747 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
18748 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
18754 /* Given a template type PARM and a class type ARG, find the unique
18755 base type in ARG that is an instance of PARM. We do not examine
18756 ARG itself; only its base-classes. If there is not exactly one
18757 appropriate base class, return NULL_TREE. PARM may be the type of
18758 a partial specialization, as well as a plain template type. Used
18761 static enum template_base_result
18762 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
18763 bool explain_p
, tree
*result
)
18765 tree rval
= NULL_TREE
;
18768 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
18770 binfo
= TYPE_BINFO (complete_type (arg
));
18773 /* The type could not be completed. */
18774 *result
= NULL_TREE
;
18775 return tbr_incomplete_type
;
18778 /* Walk in inheritance graph order. The search order is not
18779 important, and this avoids multiple walks of virtual bases. */
18780 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
18782 tree r
= try_class_unification (tparms
, targs
, parm
,
18783 BINFO_TYPE (binfo
), explain_p
);
18787 /* If there is more than one satisfactory baseclass, then:
18791 If they yield more than one possible deduced A, the type
18795 if (rval
&& !same_type_p (r
, rval
))
18797 *result
= NULL_TREE
;
18798 return tbr_ambiguous_baseclass
;
18806 return tbr_success
;
18809 /* Returns the level of DECL, which declares a template parameter. */
18812 template_decl_level (tree decl
)
18814 switch (TREE_CODE (decl
))
18817 case TEMPLATE_DECL
:
18818 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
18821 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
18824 gcc_unreachable ();
18829 /* Decide whether ARG can be unified with PARM, considering only the
18830 cv-qualifiers of each type, given STRICT as documented for unify.
18831 Returns nonzero iff the unification is OK on that basis. */
18834 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
18836 int arg_quals
= cp_type_quals (arg
);
18837 int parm_quals
= cp_type_quals (parm
);
18839 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18840 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18842 /* Although a CVR qualifier is ignored when being applied to a
18843 substituted template parameter ([8.3.2]/1 for example), that
18844 does not allow us to unify "const T" with "int&" because both
18845 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18846 It is ok when we're allowing additional CV qualifiers
18847 at the outer level [14.8.2.1]/3,1st bullet. */
18848 if ((TREE_CODE (arg
) == REFERENCE_TYPE
18849 || TREE_CODE (arg
) == FUNCTION_TYPE
18850 || TREE_CODE (arg
) == METHOD_TYPE
)
18851 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
18854 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
18855 && (parm_quals
& TYPE_QUAL_RESTRICT
))
18859 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18860 && (arg_quals
& parm_quals
) != parm_quals
)
18863 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
18864 && (parm_quals
& arg_quals
) != arg_quals
)
18870 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18872 template_parm_level_and_index (tree parm
, int* level
, int* index
)
18874 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18875 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18876 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18878 *index
= TEMPLATE_TYPE_IDX (parm
);
18879 *level
= TEMPLATE_TYPE_LEVEL (parm
);
18883 *index
= TEMPLATE_PARM_IDX (parm
);
18884 *level
= TEMPLATE_PARM_LEVEL (parm
);
18888 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18890 if (unify (TP, TA, P, A, S, EP)) \
18894 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18895 expansion at the end of PACKED_PARMS. Returns 0 if the type
18896 deduction succeeds, 1 otherwise. STRICT is the same as in
18897 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18898 call argument list. We'll need to adjust the arguments to make them
18899 types. SUBR tells us if this is from a recursive call to
18900 type_unification_real, or for comparing two template argument
18904 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
18905 tree packed_args
, unification_kind_t strict
,
18906 bool subr
, bool explain_p
)
18909 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
18910 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
18911 tree pack
, packs
= NULL_TREE
;
18912 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
18914 packed_args
= expand_template_argument_pack (packed_args
);
18916 int len
= TREE_VEC_LENGTH (packed_args
);
18918 /* Determine the parameter packs we will be deducing from the
18919 pattern, and record their current deductions. */
18920 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
18921 pack
; pack
= TREE_CHAIN (pack
))
18923 tree parm_pack
= TREE_VALUE (pack
);
18926 /* Determine the index and level of this parameter pack. */
18927 template_parm_level_and_index (parm_pack
, &level
, &idx
);
18929 /* Keep track of the parameter packs and their corresponding
18931 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
18932 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
18935 /* Loop through all of the arguments that have not yet been
18936 unified and unify each with the pattern. */
18937 for (i
= start
; i
< len
; i
++)
18940 bool any_explicit
= false;
18941 tree arg
= TREE_VEC_ELT (packed_args
, i
);
18943 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18944 or the element of its argument pack at the current index if
18945 this argument was explicitly specified. */
18946 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18950 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18953 if (TREE_VALUE (pack
)
18954 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
18955 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
18957 any_explicit
= true;
18958 arg
= TREE_VEC_ELT (pargs
, i
- start
);
18960 TMPL_ARG (targs
, level
, idx
) = arg
;
18963 /* If we had explicit template arguments, substitute them into the
18964 pattern before deduction. */
18967 /* Some arguments might still be unspecified or dependent. */
18969 ++processing_template_decl
;
18970 dependent
= any_dependent_template_arguments_p (targs
);
18972 --processing_template_decl
;
18973 parm
= tsubst (pattern
, targs
,
18974 explain_p
? tf_warning_or_error
: tf_none
,
18977 --processing_template_decl
;
18978 if (parm
== error_mark_node
)
18984 /* Unify the pattern with the current argument. */
18985 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
18989 /* For each parameter pack, collect the deduced value. */
18990 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18993 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18995 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
18996 TMPL_ARG (targs
, level
, idx
);
19000 /* Verify that the results of unification with the parameter packs
19001 produce results consistent with what we've seen before, and make
19002 the deduced argument packs available. */
19003 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19005 tree old_pack
= TREE_VALUE (pack
);
19006 tree new_args
= TREE_TYPE (pack
);
19007 int i
, len
= TREE_VEC_LENGTH (new_args
);
19009 bool nondeduced_p
= false;
19011 /* By default keep the original deduced argument pack.
19012 If necessary, more specific code is going to update the
19013 resulting deduced argument later down in this function. */
19014 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19015 TMPL_ARG (targs
, level
, idx
) = old_pack
;
19017 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19018 actually deduce anything. */
19019 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
19020 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
19021 nondeduced_p
= true;
19025 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
19027 /* If we had fewer function args than explicit template args,
19028 just use the explicits. */
19029 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19030 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
19031 if (len
< explicit_len
)
19032 new_args
= explicit_args
;
19038 /* Build the deduced *_ARGUMENT_PACK. */
19039 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
19041 result
= make_node (NONTYPE_ARGUMENT_PACK
);
19042 TREE_TYPE (result
) =
19043 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
19044 TREE_CONSTANT (result
) = 1;
19047 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19049 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
19051 /* Note the deduced argument packs for this parameter
19053 TMPL_ARG (targs
, level
, idx
) = result
;
19055 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
19056 && (ARGUMENT_PACK_ARGS (old_pack
)
19057 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
19059 /* We only had the explicitly-provided arguments before, but
19060 now we have a complete set of arguments. */
19061 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19063 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
19064 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
19065 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
19069 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
19070 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
19072 if (!comp_template_args (old_args
, new_args
,
19073 &bad_old_arg
, &bad_new_arg
))
19074 /* Inconsistent unification of this parameter pack. */
19075 return unify_parameter_pack_inconsistent (explain_p
,
19081 return unify_success (explain_p
);
19084 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19085 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19086 parameters and return value are as for unify. */
19089 unify_array_domain (tree tparms
, tree targs
,
19090 tree parm_dom
, tree arg_dom
,
19098 /* Our representation of array types uses "N - 1" as the
19099 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19100 not an integer constant. We cannot unify arbitrarily
19101 complex expressions, so we eliminate the MINUS_EXPRs
19103 parm_max
= TYPE_MAX_VALUE (parm_dom
);
19104 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
19107 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
19108 parm_max
= TREE_OPERAND (parm_max
, 0);
19110 arg_max
= TYPE_MAX_VALUE (arg_dom
);
19111 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
19114 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19115 trying to unify the type of a variable with the type
19116 of a template parameter. For example:
19118 template <unsigned int N>
19119 void f (char (&) [N]);
19126 Here, the type of the ARG will be "int [g(i)]", and
19127 may be a SAVE_EXPR, etc. */
19128 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
19129 return unify_vla_arg (explain_p
, arg_dom
);
19130 arg_max
= TREE_OPERAND (arg_max
, 0);
19133 /* If only one of the bounds used a MINUS_EXPR, compensate
19134 by adding one to the other bound. */
19135 if (parm_cst
&& !arg_cst
)
19136 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19140 else if (arg_cst
&& !parm_cst
)
19141 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19146 return unify (tparms
, targs
, parm_max
, arg_max
,
19147 UNIFY_ALLOW_INTEGER
, explain_p
);
19150 /* Deduce the value of template parameters. TPARMS is the (innermost)
19151 set of template parameters to a template. TARGS is the bindings
19152 for those template parameters, as determined thus far; TARGS may
19153 include template arguments for outer levels of template parameters
19154 as well. PARM is a parameter to a template function, or a
19155 subcomponent of that parameter; ARG is the corresponding argument.
19156 This function attempts to match PARM with ARG in a manner
19157 consistent with the existing assignments in TARGS. If more values
19158 are deduced, then TARGS is updated.
19160 Returns 0 if the type deduction succeeds, 1 otherwise. The
19161 parameter STRICT is a bitwise or of the following flags:
19164 Require an exact match between PARM and ARG.
19165 UNIFY_ALLOW_MORE_CV_QUAL:
19166 Allow the deduced ARG to be more cv-qualified (by qualification
19167 conversion) than ARG.
19168 UNIFY_ALLOW_LESS_CV_QUAL:
19169 Allow the deduced ARG to be less cv-qualified than ARG.
19170 UNIFY_ALLOW_DERIVED:
19171 Allow the deduced ARG to be a template base class of ARG,
19172 or a pointer to a template base class of the type pointed to by
19174 UNIFY_ALLOW_INTEGER:
19175 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19176 case for more information.
19177 UNIFY_ALLOW_OUTER_LEVEL:
19178 This is the outermost level of a deduction. Used to determine validity
19179 of qualification conversions. A valid qualification conversion must
19180 have const qualified pointers leading up to the inner type which
19181 requires additional CV quals, except at the outer level, where const
19182 is not required [conv.qual]. It would be normal to set this flag in
19183 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19184 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19185 This is the outermost level of a deduction, and PARM can be more CV
19186 qualified at this point.
19187 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19188 This is the outermost level of a deduction, and PARM can be less CV
19189 qualified at this point. */
19192 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
19198 int strict_in
= strict
;
19200 /* I don't think this will do the right thing with respect to types.
19201 But the only case I've seen it in so far has been array bounds, where
19202 signedness is the only information lost, and I think that will be
19204 while (TREE_CODE (parm
) == NOP_EXPR
)
19205 parm
= TREE_OPERAND (parm
, 0);
19207 if (arg
== error_mark_node
)
19208 return unify_invalid (explain_p
);
19209 if (arg
== unknown_type_node
19210 || arg
== init_list_type_node
)
19211 /* We can't deduce anything from this, but we might get all the
19212 template args from other function args. */
19213 return unify_success (explain_p
);
19215 /* If PARM uses template parameters, then we can't bail out here,
19216 even if ARG == PARM, since we won't record unifications for the
19217 template parameters. We might need them if we're trying to
19218 figure out which of two things is more specialized. */
19219 if (arg
== parm
&& !uses_template_parms (parm
))
19220 return unify_success (explain_p
);
19222 /* Handle init lists early, so the rest of the function can assume
19223 we're dealing with a type. */
19224 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
19228 tree orig_parm
= parm
;
19230 /* Replace T with std::initializer_list<T> for deduction. */
19231 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19232 && flag_deduce_init_list
)
19233 parm
= listify (parm
);
19235 if (!is_std_init_list (parm
)
19236 && TREE_CODE (parm
) != ARRAY_TYPE
)
19237 /* We can only deduce from an initializer list argument if the
19238 parameter is std::initializer_list or an array; otherwise this
19239 is a non-deduced context. */
19240 return unify_success (explain_p
);
19242 if (TREE_CODE (parm
) == ARRAY_TYPE
)
19243 elttype
= TREE_TYPE (parm
);
19246 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
19247 /* Deduction is defined in terms of a single type, so just punt
19248 on the (bizarre) std::initializer_list<T...>. */
19249 if (PACK_EXPANSION_P (elttype
))
19250 return unify_success (explain_p
);
19253 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
19255 int elt_strict
= strict
;
19257 if (elt
== error_mark_node
)
19258 return unify_invalid (explain_p
);
19260 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
19262 tree type
= TREE_TYPE (elt
);
19263 if (type
== error_mark_node
)
19264 return unify_invalid (explain_p
);
19265 /* It should only be possible to get here for a call. */
19266 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
19267 elt_strict
|= maybe_adjust_types_for_deduction
19268 (DEDUCE_CALL
, &elttype
, &type
, elt
);
19272 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
19276 if (TREE_CODE (parm
) == ARRAY_TYPE
19277 && deducible_array_bound (TYPE_DOMAIN (parm
)))
19279 /* Also deduce from the length of the initializer list. */
19280 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
19281 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
19282 if (idx
== error_mark_node
)
19283 return unify_invalid (explain_p
);
19284 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19288 /* If the std::initializer_list<T> deduction worked, replace the
19289 deduced A with std::initializer_list<A>. */
19290 if (orig_parm
!= parm
)
19292 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
19293 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19294 targ
= listify (targ
);
19295 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
19297 return unify_success (explain_p
);
19300 /* Immediately reject some pairs that won't unify because of
19301 cv-qualification mismatches. */
19302 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19304 /* It is the elements of the array which hold the cv quals of an array
19305 type, and the elements might be template type parms. We'll check
19306 when we recurse. */
19307 && TREE_CODE (arg
) != ARRAY_TYPE
19308 /* We check the cv-qualifiers when unifying with template type
19309 parameters below. We want to allow ARG `const T' to unify with
19310 PARM `T' for example, when computing which of two templates
19311 is more specialized, for example. */
19312 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
19313 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
19314 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19316 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
19317 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
19318 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
19319 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
19320 strict
&= ~UNIFY_ALLOW_DERIVED
;
19321 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19322 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
19324 switch (TREE_CODE (parm
))
19326 case TYPENAME_TYPE
:
19328 case UNBOUND_CLASS_TEMPLATE
:
19329 /* In a type which contains a nested-name-specifier, template
19330 argument values cannot be deduced for template parameters used
19331 within the nested-name-specifier. */
19332 return unify_success (explain_p
);
19334 case TEMPLATE_TYPE_PARM
:
19335 case TEMPLATE_TEMPLATE_PARM
:
19336 case BOUND_TEMPLATE_TEMPLATE_PARM
:
19337 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19338 if (error_operand_p (tparm
))
19339 return unify_invalid (explain_p
);
19341 if (TEMPLATE_TYPE_LEVEL (parm
)
19342 != template_decl_level (tparm
))
19343 /* The PARM is not one we're trying to unify. Just check
19344 to see if it matches ARG. */
19346 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19347 && (is_auto (parm
) ? is_auto (arg
)
19348 : same_type_p (parm
, arg
)))
19349 return unify_success (explain_p
);
19351 return unify_type_mismatch (explain_p
, parm
, arg
);
19353 idx
= TEMPLATE_TYPE_IDX (parm
);
19354 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19355 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
19356 if (error_operand_p (tparm
))
19357 return unify_invalid (explain_p
);
19359 /* Check for mixed types and values. */
19360 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19361 && TREE_CODE (tparm
) != TYPE_DECL
)
19362 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19363 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
19364 gcc_unreachable ();
19366 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19368 /* ARG must be constructed from a template class or a template
19369 template parameter. */
19370 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
19371 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
19372 return unify_template_deduction_failure (explain_p
, parm
, arg
);
19374 tree parmvec
= TYPE_TI_ARGS (parm
);
19375 /* An alias template name is never deduced. */
19376 if (TYPE_ALIAS_P (arg
))
19377 arg
= strip_typedefs (arg
);
19378 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
19379 tree full_argvec
= add_to_template_args (targs
, argvec
);
19381 = DECL_INNERMOST_TEMPLATE_PARMS
19382 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
19384 int parm_variadic_p
= 0;
19386 /* The resolution to DR150 makes clear that default
19387 arguments for an N-argument may not be used to bind T
19388 to a template template parameter with fewer than N
19389 parameters. It is not safe to permit the binding of
19390 default arguments as an extension, as that may change
19391 the meaning of a conforming program. Consider:
19393 struct Dense { static const unsigned int dim = 1; };
19395 template <template <typename> class View,
19397 void operator+(float, View<Block> const&);
19399 template <typename Block,
19400 unsigned int Dim = Block::dim>
19401 struct Lvalue_proxy { operator float() const; };
19405 Lvalue_proxy<Dense> p;
19410 Here, if Lvalue_proxy is permitted to bind to View, then
19411 the global operator+ will be used; if they are not, the
19412 Lvalue_proxy will be converted to float. */
19413 if (coerce_template_parms (parm_parms
,
19415 TYPE_TI_TEMPLATE (parm
),
19417 ? tf_warning_or_error
19419 /*require_all_args=*/true,
19420 /*use_default_args=*/false)
19421 == error_mark_node
)
19424 /* Deduce arguments T, i from TT<T> or TT<i>.
19425 We check each element of PARMVEC and ARGVEC individually
19426 rather than the whole TREE_VEC since they can have
19427 different number of elements. */
19429 parmvec
= expand_template_argument_pack (parmvec
);
19430 argvec
= expand_template_argument_pack (argvec
);
19432 len
= TREE_VEC_LENGTH (parmvec
);
19434 /* Check if the parameters end in a pack, making them
19437 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
19438 parm_variadic_p
= 1;
19440 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19441 /* If the template argument list of P contains a pack
19442 expansion that is not the last template argument, the
19443 entire template argument list is a non-deduced
19445 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
19446 return unify_success (explain_p
);
19448 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
19449 return unify_too_few_arguments (explain_p
,
19450 TREE_VEC_LENGTH (argvec
), len
);
19452 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19454 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19455 TREE_VEC_ELT (parmvec
, i
),
19456 TREE_VEC_ELT (argvec
, i
),
19457 UNIFY_ALLOW_NONE
, explain_p
);
19460 if (parm_variadic_p
19461 && unify_pack_expansion (tparms
, targs
,
19464 /*subr=*/true, explain_p
))
19467 arg
= TYPE_TI_TEMPLATE (arg
);
19469 /* Fall through to deduce template name. */
19472 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19473 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19475 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19477 /* Simple cases: Value already set, does match or doesn't. */
19478 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
19479 return unify_success (explain_p
);
19481 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19485 /* If PARM is `const T' and ARG is only `int', we don't have
19486 a match unless we are allowing additional qualification.
19487 If ARG is `const int' and PARM is just `T' that's OK;
19488 that binds `const int' to `T'. */
19489 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
19491 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19493 /* Consider the case where ARG is `const volatile int' and
19494 PARM is `const T'. Then, T should be `volatile int'. */
19495 arg
= cp_build_qualified_type_real
19496 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
19497 if (arg
== error_mark_node
)
19498 return unify_invalid (explain_p
);
19500 /* Simple cases: Value already set, does match or doesn't. */
19501 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
19502 return unify_success (explain_p
);
19504 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19506 /* Make sure that ARG is not a variable-sized array. (Note
19507 that were talking about variable-sized arrays (like
19508 `int[n]'), rather than arrays of unknown size (like
19509 `int[]').) We'll get very confused by such a type since
19510 the bound of the array is not constant, and therefore
19511 not mangleable. Besides, such types are not allowed in
19512 ISO C++, so we can do as we please here. We do allow
19513 them for 'auto' deduction, since that isn't ABI-exposed. */
19514 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
19515 return unify_vla_arg (explain_p
, arg
);
19517 /* Strip typedefs as in convert_template_argument. */
19518 arg
= canonicalize_type_argument (arg
, tf_none
);
19521 /* If ARG is a parameter pack or an expansion, we cannot unify
19522 against it unless PARM is also a parameter pack. */
19523 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19524 && !template_parameter_pack_p (parm
))
19525 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19527 /* If the argument deduction results is a METHOD_TYPE,
19528 then there is a problem.
19529 METHOD_TYPE doesn't map to any real C++ type the result of
19530 the deduction can not be of that type. */
19531 if (TREE_CODE (arg
) == METHOD_TYPE
)
19532 return unify_method_type_error (explain_p
, arg
);
19534 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19535 return unify_success (explain_p
);
19537 case TEMPLATE_PARM_INDEX
:
19538 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19539 if (error_operand_p (tparm
))
19540 return unify_invalid (explain_p
);
19542 if (TEMPLATE_PARM_LEVEL (parm
)
19543 != template_decl_level (tparm
))
19545 /* The PARM is not one we're trying to unify. Just check
19546 to see if it matches ARG. */
19547 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
19548 && cp_tree_equal (parm
, arg
));
19550 unify_expression_unequal (explain_p
, parm
, arg
);
19554 idx
= TEMPLATE_PARM_IDX (parm
);
19555 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19559 int x
= !cp_tree_equal (targ
, arg
);
19561 unify_inconsistency (explain_p
, parm
, targ
, arg
);
19565 /* [temp.deduct.type] If, in the declaration of a function template
19566 with a non-type template-parameter, the non-type
19567 template-parameter is used in an expression in the function
19568 parameter-list and, if the corresponding template-argument is
19569 deduced, the template-argument type shall match the type of the
19570 template-parameter exactly, except that a template-argument
19571 deduced from an array bound may be of any integral type.
19572 The non-type parameter might use already deduced type parameters. */
19573 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
19574 if (!TREE_TYPE (arg
))
19575 /* Template-parameter dependent expression. Just accept it for now.
19576 It will later be processed in convert_template_argument. */
19578 else if (same_type_p (TREE_TYPE (arg
), tparm
))
19580 else if ((strict
& UNIFY_ALLOW_INTEGER
)
19581 && CP_INTEGRAL_TYPE_P (tparm
))
19582 /* Convert the ARG to the type of PARM; the deduced non-type
19583 template argument must exactly match the types of the
19584 corresponding parameter. */
19585 arg
= fold (build_nop (tparm
, arg
));
19586 else if (uses_template_parms (tparm
))
19587 /* We haven't deduced the type of this parameter yet. Try again
19589 return unify_success (explain_p
);
19591 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
19593 /* If ARG is a parameter pack or an expansion, we cannot unify
19594 against it unless PARM is also a parameter pack. */
19595 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19596 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
19597 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19600 bool removed_attr
= false;
19601 arg
= strip_typedefs_expr (arg
, &removed_attr
);
19603 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19604 return unify_success (explain_p
);
19608 /* A pointer-to-member constant can be unified only with
19609 another constant. */
19610 if (TREE_CODE (arg
) != PTRMEM_CST
)
19611 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
19613 /* Just unify the class member. It would be useless (and possibly
19614 wrong, depending on the strict flags) to unify also
19615 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19616 arg refer to the same variable, even if through different
19617 classes. For instance:
19619 struct A { int x; };
19622 Unification of &A::x and &B::x must succeed. */
19623 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
19624 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
19629 if (!TYPE_PTR_P (arg
))
19630 return unify_type_mismatch (explain_p
, parm
, arg
);
19632 /* [temp.deduct.call]
19634 A can be another pointer or pointer to member type that can
19635 be converted to the deduced A via a qualification
19636 conversion (_conv.qual_).
19638 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19639 This will allow for additional cv-qualification of the
19640 pointed-to types if appropriate. */
19642 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
19643 /* The derived-to-base conversion only persists through one
19644 level of pointers. */
19645 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
19647 return unify (tparms
, targs
, TREE_TYPE (parm
),
19648 TREE_TYPE (arg
), strict
, explain_p
);
19651 case REFERENCE_TYPE
:
19652 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
19653 return unify_type_mismatch (explain_p
, parm
, arg
);
19654 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19655 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19658 if (TREE_CODE (arg
) != ARRAY_TYPE
)
19659 return unify_type_mismatch (explain_p
, parm
, arg
);
19660 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
19661 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
19662 return unify_type_mismatch (explain_p
, parm
, arg
);
19663 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19664 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19665 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
19666 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19667 TYPE_DOMAIN (arg
), explain_p
);
19668 return unify_success (explain_p
);
19675 case ENUMERAL_TYPE
:
19678 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19679 return unify_type_mismatch (explain_p
, parm
, arg
);
19681 /* We have already checked cv-qualification at the top of the
19683 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
19684 return unify_type_mismatch (explain_p
, parm
, arg
);
19686 /* As far as unification is concerned, this wins. Later checks
19687 will invalidate it if necessary. */
19688 return unify_success (explain_p
);
19690 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19691 /* Type INTEGER_CST can come from ordinary constant template args. */
19693 while (TREE_CODE (arg
) == NOP_EXPR
)
19694 arg
= TREE_OPERAND (arg
, 0);
19696 if (TREE_CODE (arg
) != INTEGER_CST
)
19697 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19698 return (tree_int_cst_equal (parm
, arg
)
19699 ? unify_success (explain_p
)
19700 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
19704 int i
, len
, argslen
;
19705 int parm_variadic_p
= 0;
19707 if (TREE_CODE (arg
) != TREE_VEC
)
19708 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19710 len
= TREE_VEC_LENGTH (parm
);
19711 argslen
= TREE_VEC_LENGTH (arg
);
19713 /* Check for pack expansions in the parameters. */
19714 for (i
= 0; i
< len
; ++i
)
19716 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
19719 /* We can unify against something with a trailing
19721 parm_variadic_p
= 1;
19723 /* [temp.deduct.type]/9: If the template argument list of
19724 P contains a pack expansion that is not the last
19725 template argument, the entire template argument list
19726 is a non-deduced context. */
19727 return unify_success (explain_p
);
19731 /* If we don't have enough arguments to satisfy the parameters
19732 (not counting the pack expression at the end), or we have
19733 too many arguments for a parameter list that doesn't end in
19734 a pack expression, we can't unify. */
19735 if (parm_variadic_p
19736 ? argslen
< len
- parm_variadic_p
19738 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
19740 /* Unify all of the parameters that precede the (optional)
19741 pack expression. */
19742 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19744 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19745 TREE_VEC_ELT (parm
, i
),
19746 TREE_VEC_ELT (arg
, i
),
19747 UNIFY_ALLOW_NONE
, explain_p
);
19749 if (parm_variadic_p
)
19750 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
19752 /*subr=*/true, explain_p
);
19753 return unify_success (explain_p
);
19758 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19759 return unify_type_mismatch (explain_p
, parm
, arg
);
19761 if (TYPE_PTRMEMFUNC_P (parm
))
19763 if (!TYPE_PTRMEMFUNC_P (arg
))
19764 return unify_type_mismatch (explain_p
, parm
, arg
);
19766 return unify (tparms
, targs
,
19767 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
19768 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
19769 strict
, explain_p
);
19771 else if (TYPE_PTRMEMFUNC_P (arg
))
19772 return unify_type_mismatch (explain_p
, parm
, arg
);
19774 if (CLASSTYPE_TEMPLATE_INFO (parm
))
19776 tree t
= NULL_TREE
;
19778 if (strict_in
& UNIFY_ALLOW_DERIVED
)
19780 /* First, we try to unify the PARM and ARG directly. */
19781 t
= try_class_unification (tparms
, targs
,
19782 parm
, arg
, explain_p
);
19786 /* Fallback to the special case allowed in
19787 [temp.deduct.call]:
19789 If P is a class, and P has the form
19790 template-id, then A can be a derived class of
19791 the deduced A. Likewise, if P is a pointer to
19792 a class of the form template-id, A can be a
19793 pointer to a derived class pointed to by the
19795 enum template_base_result r
;
19796 r
= get_template_base (tparms
, targs
, parm
, arg
,
19801 /* Don't give the derived diagnostic if we're
19802 already dealing with the same template. */
19804 = (CLASSTYPE_TEMPLATE_INFO (arg
)
19805 && (CLASSTYPE_TI_TEMPLATE (parm
)
19806 == CLASSTYPE_TI_TEMPLATE (arg
)));
19807 return unify_no_common_base (explain_p
&& !same_template
,
19812 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
19813 && (CLASSTYPE_TI_TEMPLATE (parm
)
19814 == CLASSTYPE_TI_TEMPLATE (arg
)))
19815 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19816 Then, we should unify `int' and `U'. */
19819 /* There's no chance of unification succeeding. */
19820 return unify_type_mismatch (explain_p
, parm
, arg
);
19822 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
19823 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
19825 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
19826 return unify_type_mismatch (explain_p
, parm
, arg
);
19827 return unify_success (explain_p
);
19830 case FUNCTION_TYPE
:
19832 unsigned int nargs
;
19837 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19838 return unify_type_mismatch (explain_p
, parm
, arg
);
19840 /* CV qualifications for methods can never be deduced, they must
19841 match exactly. We need to check them explicitly here,
19842 because type_unification_real treats them as any other
19843 cv-qualified parameter. */
19844 if (TREE_CODE (parm
) == METHOD_TYPE
19845 && (!check_cv_quals_for_unify
19847 class_of_this_parm (arg
),
19848 class_of_this_parm (parm
))))
19849 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19851 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
19852 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
19854 nargs
= list_length (TYPE_ARG_TYPES (arg
));
19855 args
= XALLOCAVEC (tree
, nargs
);
19856 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
19857 a
!= NULL_TREE
&& a
!= void_list_node
;
19858 a
= TREE_CHAIN (a
), ++i
)
19859 args
[i
] = TREE_VALUE (a
);
19862 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
19863 args
, nargs
, 1, DEDUCE_EXACT
,
19864 LOOKUP_NORMAL
, NULL
, explain_p
);
19868 /* Unify a pointer to member with a pointer to member function, which
19869 deduces the type of the member as a function type. */
19870 if (TYPE_PTRMEMFUNC_P (arg
))
19872 /* Check top-level cv qualifiers */
19873 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
19874 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19876 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19877 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
19878 UNIFY_ALLOW_NONE
, explain_p
);
19880 /* Determine the type of the function we are unifying against. */
19881 tree fntype
= static_fn_type (arg
);
19883 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
19886 if (TREE_CODE (arg
) != OFFSET_TYPE
)
19887 return unify_type_mismatch (explain_p
, parm
, arg
);
19888 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19889 TYPE_OFFSET_BASETYPE (arg
),
19890 UNIFY_ALLOW_NONE
, explain_p
);
19891 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19892 strict
, explain_p
);
19895 if (DECL_TEMPLATE_PARM_P (parm
))
19896 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
19897 if (arg
!= scalar_constant_value (parm
))
19898 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19899 return unify_success (explain_p
);
19902 case TEMPLATE_DECL
:
19903 /* Matched cases are handled by the ARG == PARM test above. */
19904 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19907 /* A non-type template parameter that is a variable should be a
19908 an integral constant, in which case, it whould have been
19909 folded into its (constant) value. So we should not be getting
19910 a variable here. */
19911 gcc_unreachable ();
19913 case TYPE_ARGUMENT_PACK
:
19914 case NONTYPE_ARGUMENT_PACK
:
19915 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
19916 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
19919 case DECLTYPE_TYPE
:
19920 case UNDERLYING_TYPE
:
19921 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19922 or UNDERLYING_TYPE nodes. */
19923 return unify_success (explain_p
);
19926 /* Unification fails if we hit an error node. */
19927 return unify_invalid (explain_p
);
19930 if (REFERENCE_REF_P (parm
))
19932 if (REFERENCE_REF_P (arg
))
19933 arg
= TREE_OPERAND (arg
, 0);
19934 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
19935 strict
, explain_p
);
19940 /* An unresolved overload is a nondeduced context. */
19941 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
19942 return unify_success (explain_p
);
19943 gcc_assert (EXPR_P (parm
));
19945 /* We must be looking at an expression. This can happen with
19949 void foo(S<I>, S<I + 2>);
19951 This is a "nondeduced context":
19955 The nondeduced contexts are:
19957 --A type that is a template-id in which one or more of
19958 the template-arguments is an expression that references
19959 a template-parameter.
19961 In these cases, we assume deduction succeeded, but don't
19962 actually infer any unifications. */
19964 if (!uses_template_parms (parm
)
19965 && !template_args_equal (parm
, arg
))
19966 return unify_expression_unequal (explain_p
, parm
, arg
);
19968 return unify_success (explain_p
);
19971 #undef RECUR_AND_CHECK_FAILURE
19973 /* Note that DECL can be defined in this translation unit, if
19977 mark_definable (tree decl
)
19980 DECL_NOT_REALLY_EXTERN (decl
) = 1;
19981 FOR_EACH_CLONE (clone
, decl
)
19982 DECL_NOT_REALLY_EXTERN (clone
) = 1;
19985 /* Called if RESULT is explicitly instantiated, or is a member of an
19986 explicitly instantiated class. */
19989 mark_decl_instantiated (tree result
, int extern_p
)
19991 SET_DECL_EXPLICIT_INSTANTIATION (result
);
19993 /* If this entity has already been written out, it's too late to
19994 make any modifications. */
19995 if (TREE_ASM_WRITTEN (result
))
19998 /* For anonymous namespace we don't need to do anything. */
19999 if (decl_anon_ns_mem_p (result
))
20001 gcc_assert (!TREE_PUBLIC (result
));
20005 if (TREE_CODE (result
) != FUNCTION_DECL
)
20006 /* The TREE_PUBLIC flag for function declarations will have been
20007 set correctly by tsubst. */
20008 TREE_PUBLIC (result
) = 1;
20010 /* This might have been set by an earlier implicit instantiation. */
20011 DECL_COMDAT (result
) = 0;
20014 DECL_NOT_REALLY_EXTERN (result
) = 0;
20017 mark_definable (result
);
20018 mark_needed (result
);
20019 /* Always make artificials weak. */
20020 if (DECL_ARTIFICIAL (result
) && flag_weak
)
20021 comdat_linkage (result
);
20022 /* For WIN32 we also want to put explicit instantiations in
20023 linkonce sections. */
20024 else if (TREE_PUBLIC (result
))
20025 maybe_make_one_only (result
);
20028 /* If EXTERN_P, then this function will not be emitted -- unless
20029 followed by an explicit instantiation, at which point its linkage
20030 will be adjusted. If !EXTERN_P, then this function will be
20031 emitted here. In neither circumstance do we want
20032 import_export_decl to adjust the linkage. */
20033 DECL_INTERFACE_KNOWN (result
) = 1;
20036 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20037 important template arguments. If any are missing, we check whether
20038 they're important by using error_mark_node for substituting into any
20039 args that were used for partial ordering (the ones between ARGS and END)
20040 and seeing if it bubbles up. */
20043 check_undeduced_parms (tree targs
, tree args
, tree end
)
20045 bool found
= false;
20047 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
20048 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
20051 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
20055 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
20056 if (substed
== error_mark_node
)
20062 /* Given two function templates PAT1 and PAT2, return:
20064 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20065 -1 if PAT2 is more specialized than PAT1.
20066 0 if neither is more specialized.
20068 LEN indicates the number of parameters we should consider
20069 (defaulted parameters should not be considered).
20071 The 1998 std underspecified function template partial ordering, and
20072 DR214 addresses the issue. We take pairs of arguments, one from
20073 each of the templates, and deduce them against each other. One of
20074 the templates will be more specialized if all the *other*
20075 template's arguments deduce against its arguments and at least one
20076 of its arguments *does* *not* deduce against the other template's
20077 corresponding argument. Deduction is done as for class templates.
20078 The arguments used in deduction have reference and top level cv
20079 qualifiers removed. Iff both arguments were originally reference
20080 types *and* deduction succeeds in both directions, an lvalue reference
20081 wins against an rvalue reference and otherwise the template
20082 with the more cv-qualified argument wins for that pairing (if
20083 neither is more cv-qualified, they both are equal). Unlike regular
20084 deduction, after all the arguments have been deduced in this way,
20085 we do *not* verify the deduced template argument values can be
20086 substituted into non-deduced contexts.
20088 The logic can be a bit confusing here, because we look at deduce1 and
20089 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20090 can find template arguments for pat1 to make arg1 look like arg2, that
20091 means that arg2 is at least as specialized as arg1. */
20094 more_specialized_fn (tree pat1
, tree pat2
, int len
)
20096 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
20097 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
20098 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
20099 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
20100 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
20101 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
20102 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
20103 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
20104 tree origs1
, origs2
;
20105 bool lose1
= false;
20106 bool lose2
= false;
20108 /* Remove the this parameter from non-static member functions. If
20109 one is a non-static member function and the other is not a static
20110 member function, remove the first parameter from that function
20111 also. This situation occurs for operator functions where we
20112 locate both a member function (with this pointer) and non-member
20113 operator (with explicit first operand). */
20114 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
20116 len
--; /* LEN is the number of significant arguments for DECL1 */
20117 args1
= TREE_CHAIN (args1
);
20118 if (!DECL_STATIC_FUNCTION_P (decl2
))
20119 args2
= TREE_CHAIN (args2
);
20121 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
20123 args2
= TREE_CHAIN (args2
);
20124 if (!DECL_STATIC_FUNCTION_P (decl1
))
20127 args1
= TREE_CHAIN (args1
);
20131 /* If only one is a conversion operator, they are unordered. */
20132 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
20135 /* Consider the return type for a conversion function */
20136 if (DECL_CONV_FN_P (decl1
))
20138 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
20139 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
20143 processing_template_decl
++;
20149 /* Stop when an ellipsis is seen. */
20150 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
20152 tree arg1
= TREE_VALUE (args1
);
20153 tree arg2
= TREE_VALUE (args2
);
20154 int deduce1
, deduce2
;
20160 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20161 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20163 /* When both arguments are pack expansions, we need only
20164 unify the patterns themselves. */
20165 arg1
= PACK_EXPANSION_PATTERN (arg1
);
20166 arg2
= PACK_EXPANSION_PATTERN (arg2
);
20168 /* This is the last comparison we need to do. */
20172 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
20174 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
20175 arg1
= TREE_TYPE (arg1
);
20176 quals1
= cp_type_quals (arg1
);
20179 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
20181 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
20182 arg2
= TREE_TYPE (arg2
);
20183 quals2
= cp_type_quals (arg2
);
20186 arg1
= TYPE_MAIN_VARIANT (arg1
);
20187 arg2
= TYPE_MAIN_VARIANT (arg2
);
20189 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
20191 int i
, len2
= list_length (args2
);
20192 tree parmvec
= make_tree_vec (1);
20193 tree argvec
= make_tree_vec (len2
);
20196 /* Setup the parameter vector, which contains only ARG1. */
20197 TREE_VEC_ELT (parmvec
, 0) = arg1
;
20199 /* Setup the argument vector, which contains the remaining
20201 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
20202 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20204 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
20205 argvec
, DEDUCE_EXACT
,
20206 /*subr=*/true, /*explain_p=*/false)
20209 /* We cannot deduce in the other direction, because ARG1 is
20210 a pack expansion but ARG2 is not. */
20213 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20215 int i
, len1
= list_length (args1
);
20216 tree parmvec
= make_tree_vec (1);
20217 tree argvec
= make_tree_vec (len1
);
20220 /* Setup the parameter vector, which contains only ARG1. */
20221 TREE_VEC_ELT (parmvec
, 0) = arg2
;
20223 /* Setup the argument vector, which contains the remaining
20225 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
20226 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20228 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
20229 argvec
, DEDUCE_EXACT
,
20230 /*subr=*/true, /*explain_p=*/false)
20233 /* We cannot deduce in the other direction, because ARG2 is
20234 a pack expansion but ARG1 is not.*/
20240 /* The normal case, where neither argument is a pack
20242 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
20243 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20245 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
20246 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20250 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20251 arg2, then arg2 is not as specialized as arg1. */
20257 /* "If, for a given type, deduction succeeds in both directions
20258 (i.e., the types are identical after the transformations above)
20259 and both P and A were reference types (before being replaced with
20260 the type referred to above):
20261 - if the type from the argument template was an lvalue reference and
20262 the type from the parameter template was not, the argument type is
20263 considered to be more specialized than the other; otherwise,
20264 - if the type from the argument template is more cv-qualified
20265 than the type from the parameter template (as described above),
20266 the argument type is considered to be more specialized than the other;
20268 - neither type is more specialized than the other." */
20270 if (deduce1
&& deduce2
)
20272 if (ref1
&& ref2
&& ref1
!= ref2
)
20279 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
20281 if ((quals1
& quals2
) == quals2
)
20283 if ((quals1
& quals2
) == quals1
)
20288 if (lose1
&& lose2
)
20289 /* We've failed to deduce something in either direction.
20290 These must be unordered. */
20293 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20294 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20295 /* We have already processed all of the arguments in our
20296 handing of the pack expansion type. */
20299 args1
= TREE_CHAIN (args1
);
20300 args2
= TREE_CHAIN (args2
);
20303 /* "In most cases, all template parameters must have values in order for
20304 deduction to succeed, but for partial ordering purposes a template
20305 parameter may remain without a value provided it is not used in the
20306 types being used for partial ordering."
20308 Thus, if we are missing any of the targs1 we need to substitute into
20309 origs1, then pat2 is not as specialized as pat1. This can happen when
20310 there is a nondeduced context. */
20311 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
20313 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
20316 processing_template_decl
--;
20318 /* If both deductions succeed, the partial ordering selects the more
20319 constrained template. */
20320 if (!lose1
&& !lose2
)
20322 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
20323 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
20324 lose1
= !subsumes_constraints (c1
, c2
);
20325 lose2
= !subsumes_constraints (c2
, c1
);
20328 /* All things being equal, if the next argument is a pack expansion
20329 for one function but not for the other, prefer the
20330 non-variadic function. FIXME this is bogus; see c++/41958. */
20332 && args1
&& TREE_VALUE (args1
)
20333 && args2
&& TREE_VALUE (args2
))
20335 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
20336 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
20339 if (lose1
== lose2
)
20347 /* Determine which of two partial specializations of TMPL is more
20350 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20351 to the first partial specialization. The TREE_PURPOSE is the
20352 innermost set of template parameters for the partial
20353 specialization. PAT2 is similar, but for the second template.
20355 Return 1 if the first partial specialization is more specialized;
20356 -1 if the second is more specialized; 0 if neither is more
20359 See [temp.class.order] for information about determining which of
20360 two templates is more specialized. */
20363 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
20367 bool any_deductions
= false;
20369 tree tmpl1
= TREE_VALUE (pat1
);
20370 tree tmpl2
= TREE_VALUE (pat2
);
20371 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
20372 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
20373 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
20374 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
20376 /* Just like what happens for functions, if we are ordering between
20377 different template specializations, we may encounter dependent
20378 types in the arguments, and we need our dependency check functions
20379 to behave correctly. */
20380 ++processing_template_decl
;
20381 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
20385 any_deductions
= true;
20388 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
20392 any_deductions
= true;
20394 --processing_template_decl
;
20396 /* If both deductions succeed, the partial ordering selects the more
20397 constrained template. */
20398 if (!winner
&& any_deductions
)
20399 return more_constrained (tmpl1
, tmpl2
);
20401 /* In the case of a tie where at least one of the templates
20402 has a parameter pack at the end, the template with the most
20403 non-packed parameters wins. */
20406 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
20407 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
20409 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
20410 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
20411 int len1
= TREE_VEC_LENGTH (args1
);
20412 int len2
= TREE_VEC_LENGTH (args2
);
20414 /* We don't count the pack expansion at the end. */
20415 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
20417 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
20422 else if (len1
< len2
)
20429 /* Return the template arguments that will produce the function signature
20430 DECL from the function template FN, with the explicit template
20431 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20432 also match. Return NULL_TREE if no satisfactory arguments could be
20436 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
20438 int ntparms
= DECL_NTPARMS (fn
);
20439 tree targs
= make_tree_vec (ntparms
);
20440 tree decl_type
= TREE_TYPE (decl
);
20441 tree decl_arg_types
;
20443 unsigned int nargs
, ix
;
20446 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
20448 /* Never do unification on the 'this' parameter. */
20449 decl_arg_types
= skip_artificial_parms_for (decl
,
20450 TYPE_ARG_TYPES (decl_type
));
20452 nargs
= list_length (decl_arg_types
);
20453 args
= XALLOCAVEC (tree
, nargs
);
20454 for (arg
= decl_arg_types
, ix
= 0;
20455 arg
!= NULL_TREE
&& arg
!= void_list_node
;
20456 arg
= TREE_CHAIN (arg
), ++ix
)
20457 args
[ix
] = TREE_VALUE (arg
);
20459 if (fn_type_unification (fn
, explicit_args
, targs
,
20461 (check_rettype
|| DECL_CONV_FN_P (fn
)
20462 ? TREE_TYPE (decl_type
) : NULL_TREE
),
20463 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
20465 == error_mark_node
)
20471 /* Return the innermost template arguments that, when applied to a partial
20472 specialization of TMPL whose innermost template parameters are
20473 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20476 For example, suppose we have:
20478 template <class T, class U> struct S {};
20479 template <class T> struct S<T*, int> {};
20481 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20482 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20483 int}. The resulting vector will be {double}, indicating that `T'
20484 is bound to `double'. */
20487 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
20489 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
20491 tree innermost_deduced_args
;
20493 innermost_deduced_args
= make_tree_vec (ntparms
);
20494 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20496 deduced_args
= copy_node (args
);
20497 SET_TMPL_ARGS_LEVEL (deduced_args
,
20498 TMPL_ARGS_DEPTH (deduced_args
),
20499 innermost_deduced_args
);
20502 deduced_args
= innermost_deduced_args
;
20504 if (unify (tparms
, deduced_args
,
20505 INNERMOST_TEMPLATE_ARGS (spec_args
),
20506 INNERMOST_TEMPLATE_ARGS (args
),
20507 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
20510 for (i
= 0; i
< ntparms
; ++i
)
20511 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
20514 /* Verify that nondeduced template arguments agree with the type
20515 obtained from argument deduction.
20519 struct A { typedef int X; };
20520 template <class T, class U> struct C {};
20521 template <class T> struct C<T, typename T::X> {};
20523 Then with the instantiation `C<A, int>', we can deduce that
20524 `T' is `A' but unify () does not check whether `typename T::X'
20526 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
20527 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20529 tf_none
, false, false);
20530 if (spec_args
== error_mark_node
20531 /* We only need to check the innermost arguments; the other
20532 arguments will always agree. */
20533 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
20534 INNERMOST_TEMPLATE_ARGS (args
)))
20537 /* Now that we have bindings for all of the template arguments,
20538 ensure that the arguments deduced for the template template
20539 parameters have compatible template parameter lists. See the use
20540 of template_template_parm_bindings_ok_p in fn_type_unification
20541 for more information. */
20542 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
20545 return deduced_args
;
20548 // Compare two function templates T1 and T2 by deducing bindings
20549 // from one against the other. If both deductions succeed, compare
20550 // constraints to see which is more constrained.
20552 more_specialized_inst (tree t1
, tree t2
)
20557 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
20563 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
20569 // If both deductions succeed, then one may be more constrained.
20570 if (count
== 2 && fate
== 0)
20571 fate
= more_constrained (t1
, t2
);
20576 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20577 Return the TREE_LIST node with the most specialized template, if
20578 any. If there is no most specialized template, the error_mark_node
20581 Note that this function does not look at, or modify, the
20582 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20583 returned is one of the elements of INSTANTIATIONS, callers may
20584 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20585 and retrieve it from the value returned. */
20588 most_specialized_instantiation (tree templates
)
20592 ++processing_template_decl
;
20595 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
20597 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
20602 /* Equally specialized, move to next function. If there
20603 is no next function, nothing's most specialized. */
20604 fn
= TREE_CHAIN (fn
);
20612 /* Now verify that champ is better than everything earlier in the
20613 instantiation list. */
20614 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
20615 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
20622 processing_template_decl
--;
20625 return error_mark_node
;
20630 /* If DECL is a specialization of some template, return the most
20631 general such template. Otherwise, returns NULL_TREE.
20633 For example, given:
20635 template <class T> struct S { template <class U> void f(U); };
20637 if TMPL is `template <class U> void S<int>::f(U)' this will return
20638 the full template. This function will not trace past partial
20639 specializations, however. For example, given in addition:
20641 template <class T> struct S<T*> { template <class U> void f(U); };
20643 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20644 `template <class T> template <class U> S<T*>::f(U)'. */
20647 most_general_template (tree decl
)
20649 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20651 if (tree tinfo
= get_template_info (decl
))
20652 decl
= TI_TEMPLATE (tinfo
);
20653 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20654 template friend, or a FIELD_DECL for a capture pack. */
20655 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20659 /* Look for more and more general templates. */
20660 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
20662 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20663 (See cp-tree.h for details.) */
20664 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
20667 if (CLASS_TYPE_P (TREE_TYPE (decl
))
20668 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
20669 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
20672 /* Stop if we run into an explicitly specialized class template. */
20673 if (!DECL_NAMESPACE_SCOPE_P (decl
)
20674 && DECL_CONTEXT (decl
)
20675 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
20678 decl
= DECL_TI_TEMPLATE (decl
);
20684 /* Return the most specialized of the template partial specializations
20685 which can produce TARGET, a specialization of some class or variable
20686 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20687 a TEMPLATE_DECL node corresponding to the partial specialization, while
20688 the TREE_PURPOSE is the set of template arguments that must be
20689 substituted into the template pattern in order to generate TARGET.
20691 If the choice of partial specialization is ambiguous, a diagnostic
20692 is issued, and the error_mark_node is returned. If there are no
20693 partial specializations matching TARGET, then NULL_TREE is
20694 returned, indicating that the primary template should be used. */
20697 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
20699 tree list
= NULL_TREE
;
20704 tree outer_args
= NULL_TREE
;
20707 if (TYPE_P (target
))
20709 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
20710 tmpl
= TI_TEMPLATE (tinfo
);
20711 args
= TI_ARGS (tinfo
);
20713 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
20715 tmpl
= TREE_OPERAND (target
, 0);
20716 args
= TREE_OPERAND (target
, 1);
20718 else if (VAR_P (target
))
20720 tree tinfo
= DECL_TEMPLATE_INFO (target
);
20721 tmpl
= TI_TEMPLATE (tinfo
);
20722 args
= TI_ARGS (tinfo
);
20725 gcc_unreachable ();
20727 tree main_tmpl
= most_general_template (tmpl
);
20729 /* For determining which partial specialization to use, only the
20730 innermost args are interesting. */
20731 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20733 outer_args
= strip_innermost_template_args (args
, 1);
20734 args
= INNERMOST_TEMPLATE_ARGS (args
);
20737 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
20739 tree partial_spec_args
;
20741 tree spec_tmpl
= TREE_VALUE (t
);
20743 partial_spec_args
= TREE_PURPOSE (t
);
20745 ++processing_template_decl
;
20749 /* Discard the outer levels of args, and then substitute in the
20750 template args from the enclosing class. */
20751 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
20752 partial_spec_args
= tsubst_template_args
20753 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
20755 /* And the same for the partial specialization TEMPLATE_DECL. */
20756 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
20759 partial_spec_args
=
20760 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20763 /*require_all_args=*/true,
20764 /*use_default_args=*/true);
20766 --processing_template_decl
;
20768 if (partial_spec_args
== error_mark_node
)
20769 return error_mark_node
;
20770 if (spec_tmpl
== error_mark_node
)
20771 return error_mark_node
;
20773 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
20774 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
20780 spec_args
= add_to_template_args (outer_args
, spec_args
);
20782 /* Keep the candidate only if the constraints are satisfied,
20783 or if we're not compiling with concepts. */
20785 || constraints_satisfied_p (spec_tmpl
, spec_args
))
20787 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
20788 TREE_TYPE (list
) = TREE_TYPE (t
);
20796 ambiguous_p
= false;
20799 t
= TREE_CHAIN (t
);
20800 for (; t
; t
= TREE_CHAIN (t
))
20802 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20809 t
= TREE_CHAIN (t
);
20812 ambiguous_p
= true;
20821 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
20823 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20826 ambiguous_p
= true;
20834 char *spaces
= NULL
;
20835 if (!(complain
& tf_error
))
20836 return error_mark_node
;
20837 if (TYPE_P (target
))
20838 error ("ambiguous template instantiation for %q#T", target
);
20840 error ("ambiguous template instantiation for %q#D", target
);
20841 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
20842 for (t
= list
; t
; t
= TREE_CHAIN (t
))
20844 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
20845 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
20846 "%s %#S", spaces
? spaces
: str
, subst
);
20847 spaces
= spaces
? spaces
: get_spaces (str
);
20850 return error_mark_node
;
20856 /* Explicitly instantiate DECL. */
20859 do_decl_instantiation (tree decl
, tree storage
)
20861 tree result
= NULL_TREE
;
20864 if (!decl
|| decl
== error_mark_node
)
20865 /* An error occurred, for which grokdeclarator has already issued
20866 an appropriate message. */
20868 else if (! DECL_LANG_SPECIFIC (decl
))
20870 error ("explicit instantiation of non-template %q#D", decl
);
20874 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
20875 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
20877 if (VAR_P (decl
) && !var_templ
)
20879 /* There is an asymmetry here in the way VAR_DECLs and
20880 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20881 the latter, the DECL we get back will be marked as a
20882 template instantiation, and the appropriate
20883 DECL_TEMPLATE_INFO will be set up. This does not happen for
20884 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20885 should handle VAR_DECLs as it currently handles
20887 if (!DECL_CLASS_SCOPE_P (decl
))
20889 error ("%qD is not a static data member of a class template", decl
);
20892 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
20893 if (!result
|| !VAR_P (result
))
20895 error ("no matching template for %qD found", decl
);
20898 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
20900 error ("type %qT for explicit instantiation %qD does not match "
20901 "declared type %qT", TREE_TYPE (result
), decl
,
20906 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
20908 error ("explicit instantiation of %q#D", decl
);
20914 /* Check for various error cases. Note that if the explicit
20915 instantiation is valid the RESULT will currently be marked as an
20916 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20917 until we get here. */
20919 if (DECL_TEMPLATE_SPECIALIZATION (result
))
20921 /* DR 259 [temp.spec].
20923 Both an explicit instantiation and a declaration of an explicit
20924 specialization shall not appear in a program unless the explicit
20925 instantiation follows a declaration of the explicit specialization.
20927 For a given set of template parameters, if an explicit
20928 instantiation of a template appears after a declaration of an
20929 explicit specialization for that template, the explicit
20930 instantiation has no effect. */
20933 else if (DECL_EXPLICIT_INSTANTIATION (result
))
20937 No program shall explicitly instantiate any template more
20940 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20941 the first instantiation was `extern' and the second is not,
20942 and EXTERN_P for the opposite case. */
20943 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
20944 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
20945 /* If an "extern" explicit instantiation follows an ordinary
20946 explicit instantiation, the template is instantiated. */
20950 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
20952 error ("no matching template for %qD found", result
);
20955 else if (!DECL_TEMPLATE_INFO (result
))
20957 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
20961 if (storage
== NULL_TREE
)
20963 else if (storage
== ridpointers
[(int) RID_EXTERN
])
20965 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
20966 pedwarn (input_location
, OPT_Wpedantic
,
20967 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20972 error ("storage class %qD applied to template instantiation", storage
);
20974 check_explicit_instantiation_namespace (result
);
20975 mark_decl_instantiated (result
, extern_p
);
20977 instantiate_decl (result
, /*defer_ok=*/1,
20978 /*expl_inst_class_mem_p=*/false);
20982 mark_class_instantiated (tree t
, int extern_p
)
20984 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
20985 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
20986 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
20987 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
20990 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
20991 rest_of_type_compilation (t
, 1);
20995 /* Called from do_type_instantiation through binding_table_foreach to
20996 do recursive instantiation for the type bound in ENTRY. */
20998 bt_instantiate_type_proc (binding_entry entry
, void *data
)
21000 tree storage
= *(tree
*) data
;
21002 if (MAYBE_CLASS_TYPE_P (entry
->type
)
21003 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
21004 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
21007 /* Called from do_type_instantiation to instantiate a member
21008 (a member function or a static member variable) of an
21009 explicitly instantiated class template. */
21011 instantiate_class_member (tree decl
, int extern_p
)
21013 mark_decl_instantiated (decl
, extern_p
);
21015 instantiate_decl (decl
, /*defer_ok=*/1,
21016 /*expl_inst_class_mem_p=*/true);
21019 /* Perform an explicit instantiation of template class T. STORAGE, if
21020 non-null, is the RID for extern, inline or static. COMPLAIN is
21021 nonzero if this is called from the parser, zero if called recursively,
21022 since the standard is unclear (as detailed below). */
21025 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
21030 int previous_instantiation_extern_p
= 0;
21032 if (TREE_CODE (t
) == TYPE_DECL
)
21035 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
21038 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
21040 error ("explicit instantiation of non-class template %qD", tmpl
);
21042 error ("explicit instantiation of non-template type %qT", t
);
21048 if (!COMPLETE_TYPE_P (t
))
21050 if (complain
& tf_error
)
21051 error ("explicit instantiation of %q#T before definition of template",
21056 if (storage
!= NULL_TREE
)
21058 if (!in_system_header_at (input_location
))
21060 if (storage
== ridpointers
[(int) RID_EXTERN
])
21062 if (cxx_dialect
== cxx98
)
21063 pedwarn (input_location
, OPT_Wpedantic
,
21064 "ISO C++ 1998 forbids the use of %<extern%> on "
21065 "explicit instantiations");
21068 pedwarn (input_location
, OPT_Wpedantic
,
21069 "ISO C++ forbids the use of %qE"
21070 " on explicit instantiations", storage
);
21073 if (storage
== ridpointers
[(int) RID_INLINE
])
21075 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21077 else if (storage
== ridpointers
[(int) RID_STATIC
])
21081 error ("storage class %qD applied to template instantiation",
21087 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
21089 /* DR 259 [temp.spec].
21091 Both an explicit instantiation and a declaration of an explicit
21092 specialization shall not appear in a program unless the explicit
21093 instantiation follows a declaration of the explicit specialization.
21095 For a given set of template parameters, if an explicit
21096 instantiation of a template appears after a declaration of an
21097 explicit specialization for that template, the explicit
21098 instantiation has no effect. */
21101 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
21105 No program shall explicitly instantiate any template more
21108 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21109 instantiation was `extern'. If EXTERN_P then the second is.
21110 These cases are OK. */
21111 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
21113 if (!previous_instantiation_extern_p
&& !extern_p
21114 && (complain
& tf_error
))
21115 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
21117 /* If we've already instantiated the template, just return now. */
21118 if (!CLASSTYPE_INTERFACE_ONLY (t
))
21122 check_explicit_instantiation_namespace (TYPE_NAME (t
));
21123 mark_class_instantiated (t
, extern_p
);
21131 /* In contrast to implicit instantiation, where only the
21132 declarations, and not the definitions, of members are
21133 instantiated, we have here:
21137 The explicit instantiation of a class template specialization
21138 implies the instantiation of all of its members not
21139 previously explicitly specialized in the translation unit
21140 containing the explicit instantiation.
21142 Of course, we can't instantiate member template classes, since
21143 we don't have any arguments for them. Note that the standard
21144 is unclear on whether the instantiation of the members are
21145 *explicit* instantiations or not. However, the most natural
21146 interpretation is that it should be an explicit instantiation. */
21149 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21150 if (TREE_CODE (tmp
) == FUNCTION_DECL
21151 && DECL_TEMPLATE_INSTANTIATION (tmp
))
21152 instantiate_class_member (tmp
, extern_p
);
21154 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21155 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
21156 instantiate_class_member (tmp
, extern_p
);
21158 if (CLASSTYPE_NESTED_UTDS (t
))
21159 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
21160 bt_instantiate_type_proc
, &storage
);
21164 /* Given a function DECL, which is a specialization of TMPL, modify
21165 DECL to be a re-instantiation of TMPL with the same template
21166 arguments. TMPL should be the template into which tsubst'ing
21167 should occur for DECL, not the most general template.
21169 One reason for doing this is a scenario like this:
21172 void f(const T&, int i);
21174 void g() { f(3, 7); }
21177 void f(const T& t, const int i) { }
21179 Note that when the template is first instantiated, with
21180 instantiate_template, the resulting DECL will have no name for the
21181 first parameter, and the wrong type for the second. So, when we go
21182 to instantiate the DECL, we regenerate it. */
21185 regenerate_decl_from_template (tree decl
, tree tmpl
)
21187 /* The arguments used to instantiate DECL, from the most general
21192 args
= DECL_TI_ARGS (decl
);
21193 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21195 /* Make sure that we can see identifiers, and compute access
21197 push_access_scope (decl
);
21199 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21207 args_depth
= TMPL_ARGS_DEPTH (args
);
21208 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
21209 if (args_depth
> parms_depth
)
21210 args
= get_innermost_template_args (args
, parms_depth
);
21212 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
21213 args
, tf_error
, NULL_TREE
,
21214 /*defer_ok*/false);
21215 if (specs
&& specs
!= error_mark_node
)
21216 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
21219 /* Merge parameter declarations. */
21220 decl_parm
= skip_artificial_parms_for (decl
,
21221 DECL_ARGUMENTS (decl
));
21223 = skip_artificial_parms_for (code_pattern
,
21224 DECL_ARGUMENTS (code_pattern
));
21225 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
21230 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21231 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
21232 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
21234 parm_type
= type_decays_to (parm_type
);
21235 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21236 TREE_TYPE (decl_parm
) = parm_type
;
21237 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21238 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21240 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21241 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21243 decl_parm
= DECL_CHAIN (decl_parm
);
21244 pattern_parm
= DECL_CHAIN (pattern_parm
);
21246 /* Merge any parameters that match with the function parameter
21248 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
21251 tree expanded_types
;
21252 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21253 the parameters in this function parameter pack. */
21254 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
21255 args
, tf_error
, NULL_TREE
);
21256 len
= TREE_VEC_LENGTH (expanded_types
);
21257 for (i
= 0; i
< len
; i
++)
21262 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21263 /* Rename the parameter to include the index. */
21264 DECL_NAME (decl_parm
) =
21265 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
21266 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
21267 parm_type
= type_decays_to (parm_type
);
21268 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21269 TREE_TYPE (decl_parm
) = parm_type
;
21270 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21271 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21273 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21274 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21276 decl_parm
= DECL_CHAIN (decl_parm
);
21279 /* Merge additional specifiers from the CODE_PATTERN. */
21280 if (DECL_DECLARED_INLINE_P (code_pattern
)
21281 && !DECL_DECLARED_INLINE_P (decl
))
21282 DECL_DECLARED_INLINE_P (decl
) = 1;
21284 else if (VAR_P (decl
))
21286 DECL_INITIAL (decl
) =
21287 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
21288 tf_error
, DECL_TI_TEMPLATE (decl
),
21289 /*integral_constant_expression_p=*/false);
21290 if (VAR_HAD_UNKNOWN_BOUND (decl
))
21291 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
21292 tf_error
, DECL_TI_TEMPLATE (decl
));
21295 gcc_unreachable ();
21297 pop_access_scope (decl
);
21300 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21301 substituted to get DECL. */
21304 template_for_substitution (tree decl
)
21306 tree tmpl
= DECL_TI_TEMPLATE (decl
);
21308 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21309 for the instantiation. This is not always the most general
21310 template. Consider, for example:
21313 struct S { template <class U> void f();
21314 template <> void f<int>(); };
21316 and an instantiation of S<double>::f<int>. We want TD to be the
21317 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21318 while (/* An instantiation cannot have a definition, so we need a
21319 more general template. */
21320 DECL_TEMPLATE_INSTANTIATION (tmpl
)
21321 /* We must also deal with friend templates. Given:
21323 template <class T> struct S {
21324 template <class U> friend void f() {};
21327 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21328 so far as the language is concerned, but that's still
21329 where we get the pattern for the instantiation from. On
21330 other hand, if the definition comes outside the class, say:
21332 template <class T> struct S {
21333 template <class U> friend void f();
21335 template <class U> friend void f() {}
21337 we don't need to look any further. That's what the check for
21338 DECL_INITIAL is for. */
21339 || (TREE_CODE (decl
) == FUNCTION_DECL
21340 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
21341 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
21343 /* The present template, TD, should not be a definition. If it
21344 were a definition, we should be using it! Note that we
21345 cannot restructure the loop to just keep going until we find
21346 a template with a definition, since that might go too far if
21347 a specialization was declared, but not defined. */
21349 /* Fetch the more general template. */
21350 tmpl
= DECL_TI_TEMPLATE (tmpl
);
21356 /* Returns true if we need to instantiate this template instance even if we
21357 know we aren't going to emit it. */
21360 always_instantiate_p (tree decl
)
21362 /* We always instantiate inline functions so that we can inline them. An
21363 explicit instantiation declaration prohibits implicit instantiation of
21364 non-inline functions. With high levels of optimization, we would
21365 normally inline non-inline functions -- but we're not allowed to do
21366 that for "extern template" functions. Therefore, we check
21367 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21368 return ((TREE_CODE (decl
) == FUNCTION_DECL
21369 && (DECL_DECLARED_INLINE_P (decl
)
21370 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
21371 /* And we need to instantiate static data members so that
21372 their initializers are available in integral constant
21375 && decl_maybe_constant_var_p (decl
)));
21378 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21379 instantiate it now, modifying TREE_TYPE (fn). */
21382 maybe_instantiate_noexcept (tree fn
)
21384 tree fntype
, spec
, noex
, clone
;
21386 /* Don't instantiate a noexcept-specification from template context. */
21387 if (processing_template_decl
)
21390 if (DECL_CLONED_FUNCTION_P (fn
))
21391 fn
= DECL_CLONED_FUNCTION (fn
);
21392 fntype
= TREE_TYPE (fn
);
21393 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
21395 if (!spec
|| !TREE_PURPOSE (spec
))
21398 noex
= TREE_PURPOSE (spec
);
21400 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
21402 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
21403 spec
= get_defaulted_eh_spec (fn
);
21404 else if (push_tinst_level (fn
))
21406 push_access_scope (fn
);
21407 push_deferring_access_checks (dk_no_deferred
);
21408 input_location
= DECL_SOURCE_LOCATION (fn
);
21409 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
21410 DEFERRED_NOEXCEPT_ARGS (noex
),
21411 tf_warning_or_error
, fn
,
21412 /*function_p=*/false,
21413 /*integral_constant_expression_p=*/true);
21414 pop_deferring_access_checks ();
21415 pop_access_scope (fn
);
21416 pop_tinst_level ();
21417 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
21418 if (spec
== error_mark_node
)
21419 spec
= noexcept_false_spec
;
21422 spec
= noexcept_false_spec
;
21424 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
21427 FOR_EACH_CLONE (clone
, fn
)
21429 if (TREE_TYPE (clone
) == fntype
)
21430 TREE_TYPE (clone
) = TREE_TYPE (fn
);
21432 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
21436 /* Produce the definition of D, a _DECL generated from a template. If
21437 DEFER_OK is nonzero, then we don't have to actually do the
21438 instantiation now; we just have to do it sometime. Normally it is
21439 an error if this is an explicit instantiation but D is undefined.
21440 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21441 explicitly instantiated class template. */
21444 instantiate_decl (tree d
, int defer_ok
,
21445 bool expl_inst_class_mem_p
)
21447 tree tmpl
= DECL_TI_TEMPLATE (d
);
21454 bool pattern_defined
;
21455 location_t saved_loc
= input_location
;
21456 int saved_unevaluated_operand
= cp_unevaluated_operand
;
21457 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
21461 bool nested
= false;
21463 /* This function should only be used to instantiate templates for
21464 functions and static member variables. */
21465 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
21467 /* A concept is never instantiated. */
21468 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
21470 /* Variables are never deferred; if instantiation is required, they
21471 are instantiated right away. That allows for better code in the
21472 case that an expression refers to the value of the variable --
21473 if the variable has a constant value the referring expression can
21474 take advantage of that fact. */
21476 || DECL_DECLARED_CONSTEXPR_P (d
))
21479 /* Don't instantiate cloned functions. Instead, instantiate the
21480 functions they cloned. */
21481 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
21482 d
= DECL_CLONED_FUNCTION (d
);
21484 if (DECL_TEMPLATE_INSTANTIATED (d
)
21485 || (TREE_CODE (d
) == FUNCTION_DECL
21486 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
21487 || DECL_TEMPLATE_SPECIALIZATION (d
))
21488 /* D has already been instantiated or explicitly specialized, so
21489 there's nothing for us to do here.
21491 It might seem reasonable to check whether or not D is an explicit
21492 instantiation, and, if so, stop here. But when an explicit
21493 instantiation is deferred until the end of the compilation,
21494 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21495 the instantiation. */
21498 /* Check to see whether we know that this template will be
21499 instantiated in some other file, as with "extern template"
21501 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
21503 /* In general, we do not instantiate such templates. */
21504 if (external_p
&& !always_instantiate_p (d
))
21507 gen_tmpl
= most_general_template (tmpl
);
21508 gen_args
= DECL_TI_ARGS (d
);
21510 if (tmpl
!= gen_tmpl
)
21511 /* We should already have the extra args. */
21512 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
21513 == TMPL_ARGS_DEPTH (gen_args
));
21514 /* And what's in the hash table should match D. */
21515 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
21516 || spec
== NULL_TREE
);
21518 /* This needs to happen before any tsubsting. */
21519 if (! push_tinst_level (d
))
21522 timevar_push (TV_TEMPLATE_INST
);
21524 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21525 for the instantiation. */
21526 td
= template_for_substitution (d
);
21527 code_pattern
= DECL_TEMPLATE_RESULT (td
);
21529 /* We should never be trying to instantiate a member of a class
21530 template or partial specialization. */
21531 gcc_assert (d
!= code_pattern
);
21533 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
21534 || DECL_TEMPLATE_SPECIALIZATION (td
))
21535 /* In the case of a friend template whose definition is provided
21536 outside the class, we may have too many arguments. Drop the
21537 ones we don't need. The same is true for specializations. */
21538 args
= get_innermost_template_args
21539 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
21543 if (TREE_CODE (d
) == FUNCTION_DECL
)
21545 deleted_p
= DECL_DELETED_FN (code_pattern
);
21546 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
21547 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
21553 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
21556 /* We may be in the middle of deferred access check. Disable it now. */
21557 push_deferring_access_checks (dk_no_deferred
);
21559 /* Unless an explicit instantiation directive has already determined
21560 the linkage of D, remember that a definition is available for
21562 if (pattern_defined
21563 && !DECL_INTERFACE_KNOWN (d
)
21564 && !DECL_NOT_REALLY_EXTERN (d
))
21565 mark_definable (d
);
21567 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
21568 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
21569 input_location
= DECL_SOURCE_LOCATION (d
);
21571 /* If D is a member of an explicitly instantiated class template,
21572 and no definition is available, treat it like an implicit
21574 if (!pattern_defined
&& expl_inst_class_mem_p
21575 && DECL_EXPLICIT_INSTANTIATION (d
))
21577 /* Leave linkage flags alone on instantiations with anonymous
21579 if (TREE_PUBLIC (d
))
21581 DECL_NOT_REALLY_EXTERN (d
) = 0;
21582 DECL_INTERFACE_KNOWN (d
) = 0;
21584 SET_DECL_IMPLICIT_INSTANTIATION (d
);
21587 /* Defer all other templates, unless we have been explicitly
21588 forbidden from doing so. */
21589 if (/* If there is no definition, we cannot instantiate the
21592 /* If it's OK to postpone instantiation, do so. */
21594 /* If this is a static data member that will be defined
21595 elsewhere, we don't want to instantiate the entire data
21596 member, but we do want to instantiate the initializer so that
21597 we can substitute that elsewhere. */
21598 || (external_p
&& VAR_P (d
))
21599 /* Handle here a deleted function too, avoid generating
21600 its body (c++/61080). */
21603 /* The definition of the static data member is now required so
21604 we must substitute the initializer. */
21606 && !DECL_INITIAL (d
)
21607 && DECL_INITIAL (code_pattern
))
21611 bool const_init
= false;
21612 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21614 ns
= decl_namespace_context (d
);
21615 push_nested_namespace (ns
);
21617 push_nested_class (DECL_CONTEXT (d
));
21618 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
21620 tf_warning_or_error
, NULL_TREE
,
21621 /*integral_constant_expression_p=*/false);
21622 /* If instantiating the initializer involved instantiating this
21623 again, don't call cp_finish_decl twice. */
21624 if (!DECL_INITIAL (d
))
21626 /* Make sure the initializer is still constant, in case of
21627 circular dependency (template/instantiate6.C). */
21629 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21630 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
21631 /*asmspec_tree=*/NULL_TREE
,
21632 LOOKUP_ONLYCONVERTING
);
21635 pop_nested_class ();
21636 pop_nested_namespace (ns
);
21639 /* We restore the source position here because it's used by
21640 add_pending_template. */
21641 input_location
= saved_loc
;
21643 if (at_eof
&& !pattern_defined
21644 && DECL_EXPLICIT_INSTANTIATION (d
)
21645 && DECL_NOT_REALLY_EXTERN (d
))
21648 The definition of a non-exported function template, a
21649 non-exported member function template, or a non-exported
21650 member function or static data member of a class template
21651 shall be present in every translation unit in which it is
21652 explicitly instantiated. */
21653 permerror (input_location
, "explicit instantiation of %qD "
21654 "but no definition available", d
);
21656 /* If we're in unevaluated context, we just wanted to get the
21657 constant value; this isn't an odr use, so don't queue
21658 a full instantiation. */
21659 if (cp_unevaluated_operand
!= 0)
21661 /* ??? Historically, we have instantiated inline functions, even
21662 when marked as "extern template". */
21663 if (!(external_p
&& VAR_P (d
)))
21664 add_pending_template (d
);
21667 /* Tell the repository that D is available in this translation unit
21668 -- and see if it is supposed to be instantiated here. */
21669 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
21671 /* In a PCH file, despite the fact that the repository hasn't
21672 requested instantiation in the PCH it is still possible that
21673 an instantiation will be required in a file that includes the
21676 add_pending_template (d
);
21677 /* Instantiate inline functions so that the inliner can do its
21678 job, even though we'll not be emitting a copy of this
21680 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
21684 fn_context
= decl_function_context (d
);
21685 nested
= (current_function_decl
!= NULL_TREE
);
21686 vec
<tree
> omp_privatization_save
;
21688 save_omp_privatization_clauses (omp_privatization_save
);
21691 push_to_top_level ();
21695 push_function_context ();
21696 cp_unevaluated_operand
= 0;
21697 c_inhibit_evaluation_warnings
= 0;
21700 /* Mark D as instantiated so that recursive calls to
21701 instantiate_decl do not try to instantiate it again. */
21702 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
21704 /* Regenerate the declaration in case the template has been modified
21705 by a subsequent redeclaration. */
21706 regenerate_decl_from_template (d
, td
);
21708 /* We already set the file and line above. Reset them now in case
21709 they changed as a result of calling regenerate_decl_from_template. */
21710 input_location
= DECL_SOURCE_LOCATION (d
);
21715 bool const_init
= false;
21717 /* Clear out DECL_RTL; whatever was there before may not be right
21718 since we've reset the type of the declaration. */
21719 SET_DECL_RTL (d
, NULL
);
21720 DECL_IN_AGGR_P (d
) = 0;
21722 /* The initializer is placed in DECL_INITIAL by
21723 regenerate_decl_from_template so we don't need to
21724 push/pop_access_scope again here. Pull it out so that
21725 cp_finish_decl can process it. */
21726 init
= DECL_INITIAL (d
);
21727 DECL_INITIAL (d
) = NULL_TREE
;
21728 DECL_INITIALIZED_P (d
) = 0;
21730 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21731 initializer. That function will defer actual emission until
21732 we have a chance to determine linkage. */
21733 DECL_EXTERNAL (d
) = 0;
21735 /* Enter the scope of D so that access-checking works correctly. */
21736 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21738 push_nested_class (DECL_CONTEXT (d
));
21740 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21741 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
21744 pop_nested_class ();
21746 if (variable_template_p (td
))
21747 note_variable_template_instantiation (d
);
21749 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
21750 synthesize_method (d
);
21751 else if (TREE_CODE (d
) == FUNCTION_DECL
)
21753 hash_map
<tree
, tree
> *saved_local_specializations
;
21757 tree block
= NULL_TREE
;
21759 /* Save away the current list, in case we are instantiating one
21760 template from within the body of another. */
21761 saved_local_specializations
= local_specializations
;
21763 /* Set up the list of local specializations. */
21764 local_specializations
= new hash_map
<tree
, tree
>;
21766 /* Set up context. */
21767 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21768 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21769 block
= push_stmt_list ();
21771 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
21773 /* Some typedefs referenced from within the template code need to be
21774 access checked at template instantiation time, i.e now. These
21775 types were added to the template at parsing time. Let's get those
21776 and perform the access checks then. */
21777 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
21780 /* Create substitution entries for the parameters. */
21781 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
21782 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
21783 spec_parm
= DECL_ARGUMENTS (d
);
21784 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
21786 register_local_specialization (spec_parm
, tmpl_parm
);
21787 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
21788 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
21790 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
21792 if (!DECL_PACK_P (tmpl_parm
))
21794 register_local_specialization (spec_parm
, tmpl_parm
);
21795 spec_parm
= DECL_CHAIN (spec_parm
);
21799 /* Register the (value) argument pack as a specialization of
21800 TMPL_PARM, then move on. */
21801 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
21802 register_local_specialization (argpack
, tmpl_parm
);
21805 gcc_assert (!spec_parm
);
21807 /* Substitute into the body of the function. */
21808 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21809 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
21810 tf_warning_or_error
, tmpl
);
21813 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
21814 tf_warning_or_error
, tmpl
,
21815 /*integral_constant_expression_p=*/false);
21817 /* Set the current input_location to the end of the function
21818 so that finish_function knows where we are. */
21820 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
21822 /* Remember if we saw an infinite loop in the template. */
21823 current_function_infinite_loop
21824 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
21827 /* We don't need the local specializations any more. */
21828 delete local_specializations
;
21829 local_specializations
= saved_local_specializations
;
21831 /* Finish the function. */
21832 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21833 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21834 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
21837 d
= finish_function (0);
21838 expand_or_defer_fn (d
);
21841 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21842 cp_check_omp_declare_reduction (d
);
21845 /* We're not deferring instantiation any more. */
21846 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
21849 pop_from_top_level ();
21851 pop_function_context ();
21854 input_location
= saved_loc
;
21855 cp_unevaluated_operand
= saved_unevaluated_operand
;
21856 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
21857 pop_deferring_access_checks ();
21858 pop_tinst_level ();
21860 restore_omp_privatization_clauses (omp_privatization_save
);
21862 timevar_pop (TV_TEMPLATE_INST
);
21867 /* Run through the list of templates that we wish we could
21868 instantiate, and instantiate any we can. RETRIES is the
21869 number of times we retry pending template instantiation. */
21872 instantiate_pending_templates (int retries
)
21875 location_t saved_loc
= input_location
;
21877 /* Instantiating templates may trigger vtable generation. This in turn
21878 may require further template instantiations. We place a limit here
21879 to avoid infinite loop. */
21880 if (pending_templates
&& retries
>= max_tinst_depth
)
21882 tree decl
= pending_templates
->tinst
->decl
;
21884 fatal_error (input_location
,
21885 "template instantiation depth exceeds maximum of %d"
21886 " instantiating %q+D, possibly from virtual table generation"
21887 " (use -ftemplate-depth= to increase the maximum)",
21888 max_tinst_depth
, decl
);
21889 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21890 /* Pretend that we defined it. */
21891 DECL_INITIAL (decl
) = error_mark_node
;
21897 struct pending_template
**t
= &pending_templates
;
21898 struct pending_template
*last
= NULL
;
21902 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
21903 bool complete
= false;
21905 if (TYPE_P (instantiation
))
21909 if (!COMPLETE_TYPE_P (instantiation
))
21911 instantiate_class_template (instantiation
);
21912 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
21913 for (fn
= TYPE_METHODS (instantiation
);
21915 fn
= TREE_CHAIN (fn
))
21916 if (! DECL_ARTIFICIAL (fn
))
21917 instantiate_decl (fn
,
21919 /*expl_inst_class_mem_p=*/false);
21920 if (COMPLETE_TYPE_P (instantiation
))
21924 complete
= COMPLETE_TYPE_P (instantiation
);
21928 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21929 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
21932 = instantiate_decl (instantiation
,
21934 /*expl_inst_class_mem_p=*/false);
21935 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
21939 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21940 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
21944 /* If INSTANTIATION has been instantiated, then we don't
21945 need to consider it again in the future. */
21953 current_tinst_level
= NULL
;
21955 last_pending_template
= last
;
21957 while (reconsider
);
21959 input_location
= saved_loc
;
21962 /* Substitute ARGVEC into T, which is a list of initializers for
21963 either base class or a non-static data member. The TREE_PURPOSEs
21964 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21965 instantiate_decl. */
21968 tsubst_initializer_list (tree t
, tree argvec
)
21970 tree inits
= NULL_TREE
;
21972 for (; t
; t
= TREE_CHAIN (t
))
21976 tree expanded_bases
= NULL_TREE
;
21977 tree expanded_arguments
= NULL_TREE
;
21980 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
21985 /* Expand the base class expansion type into separate base
21987 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
21988 tf_warning_or_error
,
21990 if (expanded_bases
== error_mark_node
)
21993 /* We'll be building separate TREE_LISTs of arguments for
21995 len
= TREE_VEC_LENGTH (expanded_bases
);
21996 expanded_arguments
= make_tree_vec (len
);
21997 for (i
= 0; i
< len
; i
++)
21998 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
22000 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22001 expand each argument in the TREE_VALUE of t. */
22002 expr
= make_node (EXPR_PACK_EXPANSION
);
22003 PACK_EXPANSION_LOCAL_P (expr
) = true;
22004 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
22005 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
22007 if (TREE_VALUE (t
) == void_type_node
)
22008 /* VOID_TYPE_NODE is used to indicate
22009 value-initialization. */
22011 for (i
= 0; i
< len
; i
++)
22012 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
22016 /* Substitute parameter packs into each argument in the
22018 in_base_initializer
= 1;
22019 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
22021 tree expanded_exprs
;
22023 /* Expand the argument. */
22024 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
22026 = tsubst_pack_expansion (expr
, argvec
,
22027 tf_warning_or_error
,
22029 if (expanded_exprs
== error_mark_node
)
22032 /* Prepend each of the expanded expressions to the
22033 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22034 for (i
= 0; i
< len
; i
++)
22036 TREE_VEC_ELT (expanded_arguments
, i
) =
22037 tree_cons (NULL_TREE
,
22038 TREE_VEC_ELT (expanded_exprs
, i
),
22039 TREE_VEC_ELT (expanded_arguments
, i
));
22042 in_base_initializer
= 0;
22044 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22045 since we built them backwards. */
22046 for (i
= 0; i
< len
; i
++)
22048 TREE_VEC_ELT (expanded_arguments
, i
) =
22049 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
22054 for (i
= 0; i
< len
; ++i
)
22056 if (expanded_bases
)
22058 decl
= TREE_VEC_ELT (expanded_bases
, i
);
22059 decl
= expand_member_init (decl
);
22060 init
= TREE_VEC_ELT (expanded_arguments
, i
);
22065 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
22066 tf_warning_or_error
, NULL_TREE
);
22068 decl
= expand_member_init (decl
);
22069 if (decl
&& !DECL_P (decl
))
22070 in_base_initializer
= 1;
22072 init
= TREE_VALUE (t
);
22074 if (init
!= void_type_node
)
22075 init
= tsubst_expr (init
, argvec
,
22076 tf_warning_or_error
, NULL_TREE
,
22077 /*integral_constant_expression_p=*/false);
22078 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
22079 /* If we had an initializer but it instantiated to nothing,
22080 value-initialize the object. This will only occur when
22081 the initializer was a pack expansion where the parameter
22082 packs used in that expansion were of length zero. */
22083 init
= void_type_node
;
22084 in_base_initializer
= 0;
22089 init
= build_tree_list (decl
, init
);
22090 TREE_CHAIN (init
) = inits
;
22098 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22101 set_current_access_from_decl (tree decl
)
22103 if (TREE_PRIVATE (decl
))
22104 current_access_specifier
= access_private_node
;
22105 else if (TREE_PROTECTED (decl
))
22106 current_access_specifier
= access_protected_node
;
22108 current_access_specifier
= access_public_node
;
22111 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22112 is the instantiation (which should have been created with
22113 start_enum) and ARGS are the template arguments to use. */
22116 tsubst_enum (tree tag
, tree newtag
, tree args
)
22120 if (SCOPED_ENUM_P (newtag
))
22121 begin_scope (sk_scoped_enum
, newtag
);
22123 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
22128 decl
= TREE_VALUE (e
);
22129 /* Note that in a template enum, the TREE_VALUE is the
22130 CONST_DECL, not the corresponding INTEGER_CST. */
22131 value
= tsubst_expr (DECL_INITIAL (decl
),
22132 args
, tf_warning_or_error
, NULL_TREE
,
22133 /*integral_constant_expression_p=*/true);
22135 /* Give this enumeration constant the correct access. */
22136 set_current_access_from_decl (decl
);
22138 /* Actually build the enumerator itself. Here we're assuming that
22139 enumerators can't have dependent attributes. */
22140 build_enumerator (DECL_NAME (decl
), value
, newtag
,
22141 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
22144 if (SCOPED_ENUM_P (newtag
))
22147 finish_enum_value_list (newtag
);
22148 finish_enum (newtag
);
22150 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
22151 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
22154 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22155 its type -- but without substituting the innermost set of template
22156 arguments. So, innermost set of template parameters will appear in
22160 get_mostly_instantiated_function_type (tree decl
)
22162 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22163 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
22166 /* Return truthvalue if we're processing a template different from
22167 the last one involved in diagnostics. */
22169 problematic_instantiation_changed (void)
22171 return current_tinst_level
!= last_error_tinst_level
;
22174 /* Remember current template involved in diagnostics. */
22176 record_last_problematic_instantiation (void)
22178 last_error_tinst_level
= current_tinst_level
;
22181 struct tinst_level
*
22182 current_instantiation (void)
22184 return current_tinst_level
;
22187 /* Return TRUE if current_function_decl is being instantiated, false
22191 instantiating_current_function_p (void)
22193 return (current_instantiation ()
22194 && current_instantiation ()->decl
== current_function_decl
);
22197 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22198 type. Return zero for ok, nonzero for disallowed. Issue error and
22199 warning messages under control of COMPLAIN. */
22202 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
22204 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
22206 else if (POINTER_TYPE_P (type
))
22208 else if (TYPE_PTRMEM_P (type
))
22210 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
22212 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
22214 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
22216 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
22218 /* A bound template template parm could later be instantiated to have a valid
22219 nontype parm type via an alias template. */
22220 else if (cxx_dialect
>= cxx11
22221 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22224 if (complain
& tf_error
)
22226 if (type
== error_mark_node
)
22227 inform (input_location
, "invalid template non-type parameter");
22229 error ("%q#T is not a valid type for a template non-type parameter",
22235 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22236 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22239 dependent_type_p_r (tree type
)
22245 A type is dependent if it is:
22247 -- a template parameter. Template template parameters are types
22248 for us (since TYPE_P holds true for them) so we handle
22250 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22251 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
22253 /* -- a qualified-id with a nested-name-specifier which contains a
22254 class-name that names a dependent type or whose unqualified-id
22255 names a dependent type. */
22256 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22259 /* An alias template specialization can be dependent even if the
22260 resulting type is not. */
22261 if (dependent_alias_template_spec_p (type
))
22264 /* -- a cv-qualified type where the cv-unqualified type is
22266 No code is necessary for this bullet; the code below handles
22267 cv-qualified types, and we don't want to strip aliases with
22268 TYPE_MAIN_VARIANT because of DR 1558. */
22269 /* -- a compound type constructed from any dependent type. */
22270 if (TYPE_PTRMEM_P (type
))
22271 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
22272 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22274 else if (TYPE_PTR_P (type
)
22275 || TREE_CODE (type
) == REFERENCE_TYPE
)
22276 return dependent_type_p (TREE_TYPE (type
));
22277 else if (TREE_CODE (type
) == FUNCTION_TYPE
22278 || TREE_CODE (type
) == METHOD_TYPE
)
22282 if (dependent_type_p (TREE_TYPE (type
)))
22284 for (arg_type
= TYPE_ARG_TYPES (type
);
22286 arg_type
= TREE_CHAIN (arg_type
))
22287 if (dependent_type_p (TREE_VALUE (arg_type
)))
22291 /* -- an array type constructed from any dependent type or whose
22292 size is specified by a constant expression that is
22295 We checked for type- and value-dependence of the bounds in
22296 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22297 if (TREE_CODE (type
) == ARRAY_TYPE
)
22299 if (TYPE_DOMAIN (type
)
22300 && dependent_type_p (TYPE_DOMAIN (type
)))
22302 return dependent_type_p (TREE_TYPE (type
));
22305 /* -- a template-id in which either the template name is a template
22307 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22309 /* ... or any of the template arguments is a dependent type or
22310 an expression that is type-dependent or value-dependent. */
22311 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
22312 && (any_dependent_template_arguments_p
22313 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
22316 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22317 dependent; if the argument of the `typeof' expression is not
22318 type-dependent, then it should already been have resolved. */
22319 if (TREE_CODE (type
) == TYPEOF_TYPE
22320 || TREE_CODE (type
) == DECLTYPE_TYPE
22321 || TREE_CODE (type
) == UNDERLYING_TYPE
)
22324 /* A template argument pack is dependent if any of its packed
22326 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
22328 tree args
= ARGUMENT_PACK_ARGS (type
);
22329 int i
, len
= TREE_VEC_LENGTH (args
);
22330 for (i
= 0; i
< len
; ++i
)
22331 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
22335 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22336 be template parameters. */
22337 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
22340 /* The standard does not specifically mention types that are local
22341 to template functions or local classes, but they should be
22342 considered dependent too. For example:
22344 template <int I> void f() {
22349 The size of `E' cannot be known until the value of `I' has been
22350 determined. Therefore, `E' must be considered dependent. */
22351 scope
= TYPE_CONTEXT (type
);
22352 if (scope
&& TYPE_P (scope
))
22353 return dependent_type_p (scope
);
22354 /* Don't use type_dependent_expression_p here, as it can lead
22355 to infinite recursion trying to determine whether a lambda
22356 nested in a lambda is dependent (c++/47687). */
22357 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
22358 && DECL_LANG_SPECIFIC (scope
)
22359 && DECL_TEMPLATE_INFO (scope
)
22360 && (any_dependent_template_arguments_p
22361 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
22364 /* Other types are non-dependent. */
22368 /* Returns TRUE if TYPE is dependent, in the sense of
22369 [temp.dep.type]. Note that a NULL type is considered dependent. */
22372 dependent_type_p (tree type
)
22374 /* If there are no template parameters in scope, then there can't be
22375 any dependent types. */
22376 if (!processing_template_decl
)
22378 /* If we are not processing a template, then nobody should be
22379 providing us with a dependent type. */
22381 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
22385 /* If the type is NULL, we have not computed a type for the entity
22386 in question; in that case, the type is dependent. */
22390 /* Erroneous types can be considered non-dependent. */
22391 if (type
== error_mark_node
)
22394 /* If we have not already computed the appropriate value for TYPE,
22396 if (!TYPE_DEPENDENT_P_VALID (type
))
22398 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
22399 TYPE_DEPENDENT_P_VALID (type
) = 1;
22402 return TYPE_DEPENDENT_P (type
);
22405 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22406 lookup. In other words, a dependent type that is not the current
22410 dependent_scope_p (tree scope
)
22412 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
22413 && !currently_open_class (scope
));
22416 /* T is a SCOPE_REF; return whether we need to consider it
22417 instantiation-dependent so that we can check access at instantiation
22418 time even though we know which member it resolves to. */
22421 instantiation_dependent_scope_ref_p (tree t
)
22423 if (DECL_P (TREE_OPERAND (t
, 1))
22424 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
22425 && accessible_in_template_p (TREE_OPERAND (t
, 0),
22426 TREE_OPERAND (t
, 1)))
22432 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22433 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22436 /* Note that this predicate is not appropriate for general expressions;
22437 only constant expressions (that satisfy potential_constant_expression)
22438 can be tested for value dependence. */
22441 value_dependent_expression_p (tree expression
)
22443 if (!processing_template_decl
)
22446 /* A name declared with a dependent type. */
22447 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
22450 switch (TREE_CODE (expression
))
22452 case IDENTIFIER_NODE
:
22453 /* A name that has not been looked up -- must be dependent. */
22456 case TEMPLATE_PARM_INDEX
:
22457 /* A non-type template parm. */
22461 /* A non-type template parm. */
22462 if (DECL_TEMPLATE_PARM_P (expression
))
22464 return value_dependent_expression_p (DECL_INITIAL (expression
));
22467 /* A constant with literal type and is initialized
22468 with an expression that is value-dependent.
22470 Note that a non-dependent parenthesized initializer will have
22471 already been replaced with its constant value, so if we see
22472 a TREE_LIST it must be dependent. */
22473 if (DECL_INITIAL (expression
)
22474 && decl_constant_var_p (expression
)
22475 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
22476 /* cp_finish_decl doesn't fold reference initializers. */
22477 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
22478 || value_dependent_expression_p (DECL_INITIAL (expression
))))
22482 case DYNAMIC_CAST_EXPR
:
22483 case STATIC_CAST_EXPR
:
22484 case CONST_CAST_EXPR
:
22485 case REINTERPRET_CAST_EXPR
:
22487 /* These expressions are value-dependent if the type to which
22488 the cast occurs is dependent or the expression being casted
22489 is value-dependent. */
22491 tree type
= TREE_TYPE (expression
);
22493 if (dependent_type_p (type
))
22496 /* A functional cast has a list of operands. */
22497 expression
= TREE_OPERAND (expression
, 0);
22500 /* If there are no operands, it must be an expression such
22501 as "int()". This should not happen for aggregate types
22502 because it would form non-constant expressions. */
22503 gcc_assert (cxx_dialect
>= cxx11
22504 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
22509 if (TREE_CODE (expression
) == TREE_LIST
)
22510 return any_value_dependent_elements_p (expression
);
22512 return value_dependent_expression_p (expression
);
22516 if (SIZEOF_EXPR_TYPE_P (expression
))
22517 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
22521 /* A `sizeof' expression is value-dependent if the operand is
22522 type-dependent or is a pack expansion. */
22523 expression
= TREE_OPERAND (expression
, 0);
22524 if (PACK_EXPANSION_P (expression
))
22526 else if (TYPE_P (expression
))
22527 return dependent_type_p (expression
);
22528 return instantiation_dependent_expression_p (expression
);
22530 case AT_ENCODE_EXPR
:
22531 /* An 'encode' expression is value-dependent if the operand is
22533 expression
= TREE_OPERAND (expression
, 0);
22534 return dependent_type_p (expression
);
22536 case NOEXCEPT_EXPR
:
22537 expression
= TREE_OPERAND (expression
, 0);
22538 return instantiation_dependent_expression_p (expression
);
22541 /* All instantiation-dependent expressions should also be considered
22542 value-dependent. */
22543 return instantiation_dependent_scope_ref_p (expression
);
22545 case COMPONENT_REF
:
22546 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
22547 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
22549 case NONTYPE_ARGUMENT_PACK
:
22550 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22551 is value-dependent. */
22553 tree values
= ARGUMENT_PACK_ARGS (expression
);
22554 int i
, len
= TREE_VEC_LENGTH (values
);
22556 for (i
= 0; i
< len
; ++i
)
22557 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
22565 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
22566 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
22567 || (type2
? dependent_type_p (type2
) : false));
22571 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22572 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
22575 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22576 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
22580 tree op
= TREE_OPERAND (expression
, 0);
22581 return (value_dependent_expression_p (op
)
22582 || has_value_dependent_address (op
));
22585 case REQUIRES_EXPR
:
22586 /* Treat all requires-expressions as value-dependent so
22587 we don't try to fold them. */
22591 return dependent_type_p (TREE_OPERAND (expression
, 0));
22595 tree fn
= get_callee_fndecl (expression
);
22597 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
22599 nargs
= call_expr_nargs (expression
);
22600 for (i
= 0; i
< nargs
; ++i
)
22602 tree op
= CALL_EXPR_ARG (expression
, i
);
22603 /* In a call to a constexpr member function, look through the
22604 implicit ADDR_EXPR on the object argument so that it doesn't
22605 cause the call to be considered value-dependent. We also
22606 look through it in potential_constant_expression. */
22607 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
22608 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
22609 && TREE_CODE (op
) == ADDR_EXPR
)
22610 op
= TREE_OPERAND (op
, 0);
22611 if (value_dependent_expression_p (op
))
22617 case TEMPLATE_ID_EXPR
:
22618 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22620 return type_dependent_expression_p (expression
)
22621 || variable_concept_p (TREE_OPERAND (expression
, 0));
22627 if (dependent_type_p (TREE_TYPE (expression
)))
22629 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
22630 if (value_dependent_expression_p (val
))
22636 /* Treat a GNU statement expression as dependent to avoid crashing
22637 under instantiate_non_dependent_expr; it can't be constant. */
22641 /* A constant expression is value-dependent if any subexpression is
22642 value-dependent. */
22643 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
22645 case tcc_reference
:
22647 case tcc_comparison
:
22649 case tcc_expression
:
22652 int i
, len
= cp_tree_operand_length (expression
);
22654 for (i
= 0; i
< len
; i
++)
22656 tree t
= TREE_OPERAND (expression
, i
);
22658 /* In some cases, some of the operands may be missing.l
22659 (For example, in the case of PREDECREMENT_EXPR, the
22660 amount to increment by may be missing.) That doesn't
22661 make the expression dependent. */
22662 if (t
&& value_dependent_expression_p (t
))
22673 /* The expression is not value-dependent. */
22677 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22678 [temp.dep.expr]. Note that an expression with no type is
22679 considered dependent. Other parts of the compiler arrange for an
22680 expression with type-dependent subexpressions to have no type, so
22681 this function doesn't have to be fully recursive. */
22684 type_dependent_expression_p (tree expression
)
22686 if (!processing_template_decl
)
22689 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
22692 /* An unresolved name is always dependent. */
22693 if (identifier_p (expression
)
22694 || TREE_CODE (expression
) == USING_DECL
22695 || TREE_CODE (expression
) == WILDCARD_DECL
)
22698 /* A fold expression is type-dependent. */
22699 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
22700 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
22701 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
22702 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
22705 /* Some expression forms are never type-dependent. */
22706 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
22707 || TREE_CODE (expression
) == SIZEOF_EXPR
22708 || TREE_CODE (expression
) == ALIGNOF_EXPR
22709 || TREE_CODE (expression
) == AT_ENCODE_EXPR
22710 || TREE_CODE (expression
) == NOEXCEPT_EXPR
22711 || TREE_CODE (expression
) == TRAIT_EXPR
22712 || TREE_CODE (expression
) == TYPEID_EXPR
22713 || TREE_CODE (expression
) == DELETE_EXPR
22714 || TREE_CODE (expression
) == VEC_DELETE_EXPR
22715 || TREE_CODE (expression
) == THROW_EXPR
22716 || TREE_CODE (expression
) == REQUIRES_EXPR
)
22719 /* The types of these expressions depends only on the type to which
22720 the cast occurs. */
22721 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
22722 || TREE_CODE (expression
) == STATIC_CAST_EXPR
22723 || TREE_CODE (expression
) == CONST_CAST_EXPR
22724 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
22725 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
22726 || TREE_CODE (expression
) == CAST_EXPR
)
22727 return dependent_type_p (TREE_TYPE (expression
));
22729 /* The types of these expressions depends only on the type created
22730 by the expression. */
22731 if (TREE_CODE (expression
) == NEW_EXPR
22732 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
22734 /* For NEW_EXPR tree nodes created inside a template, either
22735 the object type itself or a TREE_LIST may appear as the
22737 tree type
= TREE_OPERAND (expression
, 1);
22738 if (TREE_CODE (type
) == TREE_LIST
)
22739 /* This is an array type. We need to check array dimensions
22741 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
22742 || value_dependent_expression_p
22743 (TREE_OPERAND (TREE_VALUE (type
), 1));
22745 return dependent_type_p (type
);
22748 if (TREE_CODE (expression
) == SCOPE_REF
)
22750 tree scope
= TREE_OPERAND (expression
, 0);
22751 tree name
= TREE_OPERAND (expression
, 1);
22753 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22754 contains an identifier associated by name lookup with one or more
22755 declarations declared with a dependent type, or...a
22756 nested-name-specifier or qualified-id that names a member of an
22757 unknown specialization. */
22758 return (type_dependent_expression_p (name
)
22759 || dependent_scope_p (scope
));
22762 if (TREE_CODE (expression
) == FUNCTION_DECL
22763 && DECL_LANG_SPECIFIC (expression
)
22764 && DECL_TEMPLATE_INFO (expression
)
22765 && (any_dependent_template_arguments_p
22766 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
22769 if (TREE_CODE (expression
) == TEMPLATE_DECL
22770 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
22773 if (TREE_CODE (expression
) == STMT_EXPR
)
22774 expression
= stmt_expr_value_expr (expression
);
22776 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
22781 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
22783 if (type_dependent_expression_p (elt
))
22789 /* A static data member of the current instantiation with incomplete
22790 array type is type-dependent, as the definition and specializations
22791 can have different bounds. */
22792 if (VAR_P (expression
)
22793 && DECL_CLASS_SCOPE_P (expression
)
22794 && dependent_type_p (DECL_CONTEXT (expression
))
22795 && VAR_HAD_UNKNOWN_BOUND (expression
))
22798 /* An array of unknown bound depending on a variadic parameter, eg:
22800 template<typename... Args>
22801 void foo (Args... args)
22803 int arr[] = { args... };
22806 template<int... vals>
22809 int arr[] = { vals... };
22812 If the array has no length and has an initializer, it must be that
22813 we couldn't determine its length in cp_complete_array_type because
22814 it is dependent. */
22815 if (VAR_P (expression
)
22816 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
22817 && !TYPE_DOMAIN (TREE_TYPE (expression
))
22818 && DECL_INITIAL (expression
))
22821 /* A variable template specialization is type-dependent if it has any
22822 dependent template arguments. */
22823 if (VAR_P (expression
)
22824 && DECL_LANG_SPECIFIC (expression
)
22825 && DECL_TEMPLATE_INFO (expression
)
22826 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
22827 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
22829 /* Always dependent, on the number of arguments if nothing else. */
22830 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
22833 if (TREE_TYPE (expression
) == unknown_type_node
)
22835 if (TREE_CODE (expression
) == ADDR_EXPR
)
22836 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
22837 if (TREE_CODE (expression
) == COMPONENT_REF
22838 || TREE_CODE (expression
) == OFFSET_REF
)
22840 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22842 expression
= TREE_OPERAND (expression
, 1);
22843 if (identifier_p (expression
))
22846 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22847 if (TREE_CODE (expression
) == SCOPE_REF
)
22850 if (BASELINK_P (expression
))
22852 if (BASELINK_OPTYPE (expression
)
22853 && dependent_type_p (BASELINK_OPTYPE (expression
)))
22855 expression
= BASELINK_FUNCTIONS (expression
);
22858 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
22860 if (any_dependent_template_arguments_p
22861 (TREE_OPERAND (expression
, 1)))
22863 expression
= TREE_OPERAND (expression
, 0);
22864 if (identifier_p (expression
))
22868 gcc_assert (TREE_CODE (expression
) == OVERLOAD
22869 || TREE_CODE (expression
) == FUNCTION_DECL
);
22873 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
22875 expression
= OVL_NEXT (expression
);
22880 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
22882 return (dependent_type_p (TREE_TYPE (expression
)));
22885 /* walk_tree callback function for instantiation_dependent_expression_p,
22886 below. Returns non-zero if a dependent subexpression is found. */
22889 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
22894 /* We don't have to worry about decltype currently because decltype
22895 of an instantiation-dependent expr is a dependent type. This
22896 might change depending on the resolution of DR 1172. */
22897 *walk_subtrees
= false;
22900 enum tree_code code
= TREE_CODE (*tp
);
22903 /* Don't treat an argument list as dependent just because it has no
22911 /* A constant with a dependent initializer is dependent. */
22912 if (value_dependent_expression_p (*tp
))
22916 case TEMPLATE_PARM_INDEX
:
22919 /* Handle expressions with type operands. */
22923 case AT_ENCODE_EXPR
:
22925 tree op
= TREE_OPERAND (*tp
, 0);
22926 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
22927 op
= TREE_TYPE (op
);
22930 if (dependent_type_p (op
))
22934 *walk_subtrees
= false;
22942 if (value_dependent_expression_p (*tp
))
22944 *walk_subtrees
= false;
22947 case COMPONENT_REF
:
22948 if (identifier_p (TREE_OPERAND (*tp
, 1)))
22949 /* In a template, finish_class_member_access_expr creates a
22950 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22951 type-dependent, so that we can check access control at
22952 instantiation time (PR 42277). See also Core issue 1273. */
22957 if (instantiation_dependent_scope_ref_p (*tp
))
22962 /* Treat statement-expressions as dependent. */
22966 /* Treat requires-expressions as dependent. */
22967 case REQUIRES_EXPR
:
22971 /* Treat calls to function concepts as dependent. */
22972 if (function_concept_check_p (*tp
))
22976 case TEMPLATE_ID_EXPR
:
22977 /* And variable concepts. */
22978 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
22986 if (type_dependent_expression_p (*tp
))
22992 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22993 sense defined by the ABI:
22995 "An expression is instantiation-dependent if it is type-dependent
22996 or value-dependent, or it has a subexpression that is type-dependent
22997 or value-dependent." */
23000 instantiation_dependent_expression_p (tree expression
)
23004 if (!processing_template_decl
)
23007 if (expression
== error_mark_node
)
23010 result
= cp_walk_tree_without_duplicates (&expression
,
23011 instantiation_dependent_r
, NULL
);
23012 return result
!= NULL_TREE
;
23015 /* Like type_dependent_expression_p, but it also works while not processing
23016 a template definition, i.e. during substitution or mangling. */
23019 type_dependent_expression_p_push (tree expr
)
23022 ++processing_template_decl
;
23023 b
= type_dependent_expression_p (expr
);
23024 --processing_template_decl
;
23028 /* Returns TRUE if ARGS contains a type-dependent expression. */
23031 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
23036 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
23038 if (type_dependent_expression_p (arg
))
23044 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23045 expressions) contains any type-dependent expressions. */
23048 any_type_dependent_elements_p (const_tree list
)
23050 for (; list
; list
= TREE_CHAIN (list
))
23051 if (type_dependent_expression_p (TREE_VALUE (list
)))
23057 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23058 expressions) contains any value-dependent expressions. */
23061 any_value_dependent_elements_p (const_tree list
)
23063 for (; list
; list
= TREE_CHAIN (list
))
23064 if (value_dependent_expression_p (TREE_VALUE (list
)))
23070 /* Returns TRUE if the ARG (a template argument) is dependent. */
23073 dependent_template_arg_p (tree arg
)
23075 if (!processing_template_decl
)
23078 /* Assume a template argument that was wrongly written by the user
23079 is dependent. This is consistent with what
23080 any_dependent_template_arguments_p [that calls this function]
23082 if (!arg
|| arg
== error_mark_node
)
23085 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
23086 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
23088 if (TREE_CODE (arg
) == TEMPLATE_DECL
23089 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
23090 return dependent_template_p (arg
);
23091 else if (ARGUMENT_PACK_P (arg
))
23093 tree args
= ARGUMENT_PACK_ARGS (arg
);
23094 int i
, len
= TREE_VEC_LENGTH (args
);
23095 for (i
= 0; i
< len
; ++i
)
23097 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23103 else if (TYPE_P (arg
))
23104 return dependent_type_p (arg
);
23106 return (type_dependent_expression_p (arg
)
23107 || value_dependent_expression_p (arg
));
23110 /* Returns true if ARGS (a collection of template arguments) contains
23111 any types that require structural equality testing. */
23114 any_template_arguments_need_structural_equality_p (tree args
)
23121 if (args
== error_mark_node
)
23124 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23126 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23127 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23129 tree arg
= TREE_VEC_ELT (level
, j
);
23130 tree packed_args
= NULL_TREE
;
23133 if (ARGUMENT_PACK_P (arg
))
23135 /* Look inside the argument pack. */
23136 packed_args
= ARGUMENT_PACK_ARGS (arg
);
23137 len
= TREE_VEC_LENGTH (packed_args
);
23140 for (k
= 0; k
< len
; ++k
)
23143 arg
= TREE_VEC_ELT (packed_args
, k
);
23145 if (error_operand_p (arg
))
23147 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23149 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
23151 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
23152 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
23161 /* Returns true if ARGS (a collection of template arguments) contains
23162 any dependent arguments. */
23165 any_dependent_template_arguments_p (const_tree args
)
23172 if (args
== error_mark_node
)
23175 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23177 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23178 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23179 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
23186 /* Returns TRUE if the template TMPL is dependent. */
23189 dependent_template_p (tree tmpl
)
23191 if (TREE_CODE (tmpl
) == OVERLOAD
)
23195 if (dependent_template_p (OVL_CURRENT (tmpl
)))
23197 tmpl
= OVL_NEXT (tmpl
);
23202 /* Template template parameters are dependent. */
23203 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
23204 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
23206 /* So are names that have not been looked up. */
23207 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
23209 /* So are member templates of dependent classes. */
23210 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
23211 return dependent_type_p (DECL_CONTEXT (tmpl
));
23215 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23218 dependent_template_id_p (tree tmpl
, tree args
)
23220 return (dependent_template_p (tmpl
)
23221 || any_dependent_template_arguments_p (args
));
23224 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23228 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
23232 if (!processing_template_decl
)
23235 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
23237 tree decl
= TREE_VEC_ELT (declv
, i
);
23238 tree init
= TREE_VEC_ELT (initv
, i
);
23239 tree cond
= TREE_VEC_ELT (condv
, i
);
23240 tree incr
= TREE_VEC_ELT (incrv
, i
);
23242 if (type_dependent_expression_p (decl
)
23243 || TREE_CODE (decl
) == SCOPE_REF
)
23246 if (init
&& type_dependent_expression_p (init
))
23249 if (type_dependent_expression_p (cond
))
23252 if (COMPARISON_CLASS_P (cond
)
23253 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
23254 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
23257 if (TREE_CODE (incr
) == MODOP_EXPR
)
23259 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
23260 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
23263 else if (type_dependent_expression_p (incr
))
23265 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
23267 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
23269 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
23271 tree t
= TREE_OPERAND (incr
, 1);
23272 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
23273 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
23282 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23283 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23284 no such TYPE can be found. Note that this function peers inside
23285 uninstantiated templates and therefore should be used only in
23286 extremely limited situations. ONLY_CURRENT_P restricts this
23287 peering to the currently open classes hierarchy (which is required
23288 when comparing types). */
23291 resolve_typename_type (tree type
, bool only_current_p
)
23300 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
23302 scope
= TYPE_CONTEXT (type
);
23303 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23304 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23305 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23306 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23307 identifier of the TYPENAME_TYPE anymore.
23308 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23309 TYPENAME_TYPE instead, we avoid messing up with a possible
23310 typedef variant case. */
23311 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
23313 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23314 it first before we can figure out what NAME refers to. */
23315 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23317 if (TYPENAME_IS_RESOLVING_P (scope
))
23318 /* Given a class template A with a dependent base with nested type C,
23319 typedef typename A::C::C C will land us here, as trying to resolve
23320 the initial A::C leads to the local C typedef, which leads back to
23321 A::C::C. So we break the recursion now. */
23324 scope
= resolve_typename_type (scope
, only_current_p
);
23326 /* If we don't know what SCOPE refers to, then we cannot resolve the
23328 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23330 /* If the SCOPE is a template type parameter, we have no way of
23331 resolving the name. */
23332 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
23334 /* If the SCOPE is not the current instantiation, there's no reason
23335 to look inside it. */
23336 if (only_current_p
&& !currently_open_class (scope
))
23338 /* If this is a typedef, we don't want to look inside (c++/11987). */
23339 if (typedef_variant_p (type
))
23341 /* If SCOPE isn't the template itself, it will not have a valid
23342 TYPE_FIELDS list. */
23343 if (CLASS_TYPE_P (scope
)
23344 && same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
23345 /* scope is either the template itself or a compatible instantiation
23346 like X<T>, so look up the name in the original template. */
23347 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
23349 /* scope is a partial instantiation, so we can't do the lookup or we
23350 will lose the template arguments. */
23352 /* Enter the SCOPE so that name lookup will be resolved as if we
23353 were in the class definition. In particular, SCOPE will no
23354 longer be considered a dependent type. */
23355 pushed_scope
= push_scope (scope
);
23356 /* Look up the declaration. */
23357 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
23358 tf_warning_or_error
);
23360 result
= NULL_TREE
;
23362 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23363 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23366 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
23367 && TREE_CODE (decl
) == TYPE_DECL
)
23369 result
= TREE_TYPE (decl
);
23370 if (result
== error_mark_node
)
23371 result
= NULL_TREE
;
23373 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
23374 && DECL_CLASS_TEMPLATE_P (decl
))
23378 /* Obtain the template and the arguments. */
23379 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
23380 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
23381 /* Instantiate the template. */
23382 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
23383 /*entering_scope=*/0,
23384 tf_error
| tf_user
);
23385 if (result
== error_mark_node
)
23386 result
= NULL_TREE
;
23389 /* Leave the SCOPE. */
23391 pop_scope (pushed_scope
);
23393 /* If we failed to resolve it, return the original typename. */
23397 /* If lookup found a typename type, resolve that too. */
23398 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
23400 /* Ill-formed programs can cause infinite recursion here, so we
23401 must catch that. */
23402 TYPENAME_IS_RESOLVING_P (type
) = 1;
23403 result
= resolve_typename_type (result
, only_current_p
);
23404 TYPENAME_IS_RESOLVING_P (type
) = 0;
23407 /* Qualify the resulting type. */
23408 quals
= cp_type_quals (type
);
23410 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
23415 /* EXPR is an expression which is not type-dependent. Return a proxy
23416 for EXPR that can be used to compute the types of larger
23417 expressions containing EXPR. */
23420 build_non_dependent_expr (tree expr
)
23424 /* When checking, try to get a constant value for all non-dependent
23425 expressions in order to expose bugs in *_dependent_expression_p
23427 if (flag_checking
&& cxx_dialect
>= cxx11
23428 /* Don't do this during nsdmi parsing as it can lead to
23429 unexpected recursive instantiations. */
23430 && !parsing_nsdmi ())
23431 fold_non_dependent_expr (expr
);
23433 /* Preserve OVERLOADs; the functions must be available to resolve
23436 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
23437 inner_expr
= stmt_expr_value_expr (inner_expr
);
23438 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
23439 inner_expr
= TREE_OPERAND (inner_expr
, 0);
23440 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
23441 inner_expr
= TREE_OPERAND (inner_expr
, 1);
23442 if (is_overloaded_fn (inner_expr
)
23443 || TREE_CODE (inner_expr
) == OFFSET_REF
)
23445 /* There is no need to return a proxy for a variable. */
23448 /* Preserve string constants; conversions from string constants to
23449 "char *" are allowed, even though normally a "const char *"
23450 cannot be used to initialize a "char *". */
23451 if (TREE_CODE (expr
) == STRING_CST
)
23453 /* Preserve void and arithmetic constants, as an optimization -- there is no
23454 reason to create a new node. */
23455 if (TREE_CODE (expr
) == VOID_CST
23456 || TREE_CODE (expr
) == INTEGER_CST
23457 || TREE_CODE (expr
) == REAL_CST
)
23459 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23460 There is at least one place where we want to know that a
23461 particular expression is a throw-expression: when checking a ?:
23462 expression, there are special rules if the second or third
23463 argument is a throw-expression. */
23464 if (TREE_CODE (expr
) == THROW_EXPR
)
23467 /* Don't wrap an initializer list, we need to be able to look inside. */
23468 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
23471 /* Don't wrap a dummy object, we need to be able to test for it. */
23472 if (is_dummy_object (expr
))
23475 if (TREE_CODE (expr
) == COND_EXPR
)
23476 return build3 (COND_EXPR
,
23478 TREE_OPERAND (expr
, 0),
23479 (TREE_OPERAND (expr
, 1)
23480 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
23481 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
23482 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
23483 if (TREE_CODE (expr
) == COMPOUND_EXPR
23484 && !COMPOUND_EXPR_OVERLOADED (expr
))
23485 return build2 (COMPOUND_EXPR
,
23487 TREE_OPERAND (expr
, 0),
23488 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
23490 /* If the type is unknown, it can't really be non-dependent */
23491 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
23493 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23494 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
23497 /* ARGS is a vector of expressions as arguments to a function call.
23498 Replace the arguments with equivalent non-dependent expressions.
23499 This modifies ARGS in place. */
23502 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
23507 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
23509 tree newarg
= build_non_dependent_expr (arg
);
23511 (*args
)[ix
] = newarg
;
23515 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23516 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23517 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23520 make_auto_1 (tree name
, bool set_canonical
)
23522 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
23523 TYPE_NAME (au
) = build_decl (input_location
,
23524 TYPE_DECL
, name
, au
);
23525 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
23526 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
23527 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
23528 TYPE_NAME (au
), NULL_TREE
);
23530 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
23531 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
23532 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
23538 make_decltype_auto (void)
23540 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23546 return make_auto_1 (get_identifier ("auto"), true);
23549 /* Make a "constrained auto" type-specifier. This is an
23550 auto type with constraints that must be associated after
23551 deduction. The constraint is formed from the given
23552 CONC and its optional sequence of arguments, which are
23553 non-null if written as partial-concept-id. */
23556 make_constrained_auto (tree con
, tree args
)
23558 tree type
= make_auto_1 (get_identifier ("auto"), false);
23560 /* Build the constraint. */
23561 tree tmpl
= DECL_TI_TEMPLATE (con
);
23564 expr
= build_concept_check (tmpl
, type
, args
);
23566 expr
= build_concept_check (build_overload (tmpl
, NULL_TREE
), type
, args
);
23568 tree constr
= make_predicate_constraint (expr
);
23569 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
23571 /* Our canonical type depends on the constraint. */
23572 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
23574 /* Attach the constraint to the type declaration. */
23575 tree decl
= TYPE_NAME (type
);
23579 /* Given type ARG, return std::initializer_list<ARG>. */
23584 tree std_init_list
= namespace_binding
23585 (get_identifier ("initializer_list"), std_node
);
23587 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
23589 error ("deducing from brace-enclosed initializer list requires "
23590 "#include <initializer_list>");
23591 return error_mark_node
;
23593 argvec
= make_tree_vec (1);
23594 TREE_VEC_ELT (argvec
, 0) = arg
;
23595 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
23596 NULL_TREE
, 0, tf_warning_or_error
);
23599 /* Replace auto in TYPE with std::initializer_list<auto>. */
23602 listify_autos (tree type
, tree auto_node
)
23604 tree init_auto
= listify (auto_node
);
23605 tree argvec
= make_tree_vec (1);
23606 TREE_VEC_ELT (argvec
, 0) = init_auto
;
23607 if (processing_template_decl
)
23608 argvec
= add_to_template_args (current_template_args (), argvec
);
23609 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
23612 /* Hash traits for hashing possibly constrained 'auto'
23613 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23615 struct auto_hash
: default_hash_traits
<tree
>
23617 static inline hashval_t
hash (tree
);
23618 static inline bool equal (tree
, tree
);
23621 /* Hash the 'auto' T. */
23624 auto_hash::hash (tree t
)
23626 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
23627 /* Matching constrained-type-specifiers denote the same template
23628 parameter, so hash the constraint. */
23629 return hash_placeholder_constraint (c
);
23631 /* But unconstrained autos are all separate, so just hash the pointer. */
23632 return iterative_hash_object (t
, 0);
23635 /* Compare two 'auto's. */
23638 auto_hash::equal (tree t1
, tree t2
)
23643 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
23644 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
23646 /* Two unconstrained autos are distinct. */
23650 return equivalent_placeholder_constraints (c1
, c2
);
23653 /* for_each_template_parm callback for extract_autos: if t is a (possibly
23654 constrained) auto, add it to the vector. */
23657 extract_autos_r (tree t
, void *data
)
23659 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
23660 if (is_auto_or_concept (t
))
23662 /* All the autos were built with index 0; fix that up now. */
23663 tree
*p
= hash
.find_slot (t
, INSERT
);
23666 /* If this is a repeated constrained-type-specifier, use the index we
23668 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
23671 /* Otherwise this is new, so use the current count. */
23673 idx
= hash
.elements () - 1;
23675 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
23678 /* Always keep walking. */
23682 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
23683 says they can appear anywhere in the type. */
23686 extract_autos (tree type
)
23688 hash_set
<tree
> visited
;
23689 hash_table
<auto_hash
> hash (2);
23691 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
23693 tree tree_vec
= make_tree_vec (hash
.elements());
23694 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
23695 iter
!= hash
.end(); ++iter
)
23698 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
23699 TREE_VEC_ELT (tree_vec
, i
)
23700 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
23706 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23707 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23710 do_auto_deduction (tree type
, tree init
, tree auto_node
)
23712 return do_auto_deduction (type
, init
, auto_node
,
23713 tf_warning_or_error
,
23717 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23718 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23719 The CONTEXT determines the context in which auto deduction is performed
23720 and is used to control error diagnostics. */
23723 do_auto_deduction (tree type
, tree init
, tree auto_node
,
23724 tsubst_flags_t complain
, auto_deduction_context context
)
23728 if (init
== error_mark_node
)
23729 return error_mark_node
;
23731 if (type_dependent_expression_p (init
))
23732 /* Defining a subset of type-dependent expressions that we can deduce
23733 from ahead of time isn't worth the trouble. */
23736 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23737 with either a new invented type template parameter U or, if the
23738 initializer is a braced-init-list (8.5.4), with
23739 std::initializer_list<U>. */
23740 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
23742 if (!DIRECT_LIST_INIT_P (init
))
23743 type
= listify_autos (type
, auto_node
);
23744 else if (CONSTRUCTOR_NELTS (init
) == 1)
23745 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
23748 if (complain
& tf_warning_or_error
)
23750 if (permerror (input_location
, "direct-list-initialization of "
23751 "%<auto%> requires exactly one element"))
23752 inform (input_location
,
23753 "for deduction to %<std::initializer_list%>, use copy-"
23754 "list-initialization (i.e. add %<=%> before the %<{%>)");
23756 type
= listify_autos (type
, auto_node
);
23760 if (type
== error_mark_node
)
23761 return error_mark_node
;
23763 init
= resolve_nondeduced_context (init
);
23765 if (AUTO_IS_DECLTYPE (auto_node
))
23767 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
23768 && !REF_PARENTHESIZED_P (init
)));
23769 targs
= make_tree_vec (1);
23770 TREE_VEC_ELT (targs
, 0)
23771 = finish_decltype_type (init
, id
, tf_warning_or_error
);
23772 if (type
!= auto_node
)
23774 if (complain
& tf_error
)
23775 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
23776 return error_mark_node
;
23781 tree parms
= build_tree_list (NULL_TREE
, type
);
23785 tparms
= extract_autos (type
);
23788 tparms
= make_tree_vec (1);
23789 TREE_VEC_ELT (tparms
, 0)
23790 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
23793 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
23794 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
23795 DEDUCE_CALL
, LOOKUP_NORMAL
,
23796 NULL
, /*explain_p=*/false);
23799 if (processing_template_decl
)
23800 /* Try again at instantiation time. */
23802 if (type
&& type
!= error_mark_node
23803 && (complain
& tf_error
))
23804 /* If type is error_mark_node a diagnostic must have been
23805 emitted by now. Also, having a mention to '<type error>'
23806 in the diagnostic is not really useful to the user. */
23808 if (cfun
&& auto_node
== current_function_auto_return_pattern
23809 && LAMBDA_FUNCTION_P (current_function_decl
))
23810 error ("unable to deduce lambda return type from %qE", init
);
23812 error ("unable to deduce %qT from %qE", type
, init
);
23813 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
23814 DEDUCE_CALL
, LOOKUP_NORMAL
,
23815 NULL
, /*explain_p=*/true);
23817 return error_mark_node
;
23821 /* Check any placeholder constraints against the deduced type. */
23822 if (flag_concepts
&& !processing_template_decl
)
23823 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
23825 /* Use the deduced type to check the associated constraints. */
23826 if (!constraints_satisfied_p (constr
, targs
))
23828 if (complain
& tf_warning_or_error
)
23832 case adc_unspecified
:
23833 error("placeholder constraints not satisfied");
23835 case adc_variable_type
:
23836 error ("deduced initializer does not satisfy "
23837 "placeholder constraints");
23839 case adc_return_type
:
23840 error ("deduced return type does not satisfy "
23841 "placeholder constraints");
23843 case adc_requirement
:
23844 error ("deduced expression type does not saatisy "
23845 "placeholder constraints");
23848 diagnose_constraints (input_location
, constr
, targs
);
23850 return error_mark_node
;
23854 if (processing_template_decl
)
23855 targs
= add_to_template_args (current_template_args (), targs
);
23856 return tsubst (type
, targs
, complain
, NULL_TREE
);
23859 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23863 splice_late_return_type (tree type
, tree late_return_type
)
23865 if (is_auto (type
))
23867 if (late_return_type
)
23868 return late_return_type
;
23870 tree idx
= get_template_parm_index (type
);
23871 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
23872 /* In an abbreviated function template we didn't know we were dealing
23873 with a function template when we saw the auto return type, so update
23874 it to have the correct level. */
23875 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
23880 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23881 'decltype(auto)'. */
23884 is_auto (const_tree type
)
23886 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23887 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
23888 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
23894 /* for_each_template_parm callback for type_uses_auto. */
23897 is_auto_r (tree tp
, void */
*data*/
)
23899 return is_auto_or_concept (tp
);
23902 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23903 a use of `auto'. Returns NULL_TREE otherwise. */
23906 type_uses_auto (tree type
)
23908 if (type
== NULL_TREE
)
23910 else if (flag_concepts
)
23912 /* The Concepts TS allows multiple autos in one type-specifier; just
23913 return the first one we find, do_auto_deduction will collect all of
23915 if (uses_template_parms (type
))
23916 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
23917 /*visited*/NULL
, /*nondeduced*/true);
23922 return find_type_usage (type
, is_auto
);
23925 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23926 'decltype(auto)' or a concept. */
23929 is_auto_or_concept (const_tree type
)
23931 return is_auto (type
); // or concept
23934 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23935 a concept identifier) iff TYPE contains a use of a generic type. Returns
23936 NULL_TREE otherwise. */
23939 type_uses_auto_or_concept (tree type
)
23941 return find_type_usage (type
, is_auto_or_concept
);
23945 /* For a given template T, return the vector of typedefs referenced
23946 in T for which access check is needed at T instantiation time.
23947 T is either a FUNCTION_DECL or a RECORD_TYPE.
23948 Those typedefs were added to T by the function
23949 append_type_to_template_for_access_check. */
23951 vec
<qualified_typedef_usage_t
, va_gc
> *
23952 get_types_needing_access_check (tree t
)
23955 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
23957 if (!t
|| t
== error_mark_node
)
23960 if (!(ti
= get_template_info (t
)))
23963 if (CLASS_TYPE_P (t
)
23964 || TREE_CODE (t
) == FUNCTION_DECL
)
23966 if (!TI_TEMPLATE (ti
))
23969 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
23975 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
23976 tied to T. That list of typedefs will be access checked at
23977 T instantiation time.
23978 T is either a FUNCTION_DECL or a RECORD_TYPE.
23979 TYPE_DECL is a TYPE_DECL node representing a typedef.
23980 SCOPE is the scope through which TYPE_DECL is accessed.
23981 LOCATION is the location of the usage point of TYPE_DECL.
23983 This function is a subroutine of
23984 append_type_to_template_for_access_check. */
23987 append_type_to_template_for_access_check_1 (tree t
,
23990 location_t location
)
23992 qualified_typedef_usage_t typedef_usage
;
23995 if (!t
|| t
== error_mark_node
)
23998 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
23999 || CLASS_TYPE_P (t
))
24001 && TREE_CODE (type_decl
) == TYPE_DECL
24004 if (!(ti
= get_template_info (t
)))
24007 gcc_assert (TI_TEMPLATE (ti
));
24009 typedef_usage
.typedef_decl
= type_decl
;
24010 typedef_usage
.context
= scope
;
24011 typedef_usage
.locus
= location
;
24013 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
24016 /* Append TYPE_DECL to the template TEMPL.
24017 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24018 At TEMPL instanciation time, TYPE_DECL will be checked to see
24019 if it can be accessed through SCOPE.
24020 LOCATION is the location of the usage point of TYPE_DECL.
24022 e.g. consider the following code snippet:
24029 template<class U> struct S
24031 C::myint mi; // <-- usage point of the typedef C::myint
24036 At S<char> instantiation time, we need to check the access of C::myint
24037 In other words, we need to check the access of the myint typedef through
24038 the C scope. For that purpose, this function will add the myint typedef
24039 and the scope C through which its being accessed to a list of typedefs
24040 tied to the template S. That list will be walked at template instantiation
24041 time and access check performed on each typedefs it contains.
24042 Note that this particular code snippet should yield an error because
24043 myint is private to C. */
24046 append_type_to_template_for_access_check (tree templ
,
24049 location_t location
)
24051 qualified_typedef_usage_t
*iter
;
24054 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
24056 /* Make sure we don't append the type to the template twice. */
24057 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
24058 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
24061 append_type_to_template_for_access_check_1 (templ
, type_decl
,
24065 /* Convert the generic type parameters in PARM that match the types given in the
24066 range [START_IDX, END_IDX) from the current_template_parms into generic type
24070 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
24072 tree current
= current_template_parms
;
24073 int depth
= TMPL_PARMS_DEPTH (current
);
24074 current
= INNERMOST_TEMPLATE_PARMS (current
);
24075 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
24077 for (int i
= 0; i
< start_idx
; ++i
)
24078 TREE_VEC_ELT (replacement
, i
)
24079 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24081 for (int i
= start_idx
; i
< end_idx
; ++i
)
24083 /* Create a distinct parameter pack type from the current parm and add it
24084 to the replacement args to tsubst below into the generic function
24087 tree o
= TREE_TYPE (TREE_VALUE
24088 (TREE_VEC_ELT (current
, i
)));
24089 tree t
= copy_type (o
);
24090 TEMPLATE_TYPE_PARM_INDEX (t
)
24091 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
24093 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
24094 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
24095 TYPE_MAIN_VARIANT (t
) = t
;
24096 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
24097 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
24098 TREE_VEC_ELT (replacement
, i
) = t
;
24099 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
24102 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
24103 TREE_VEC_ELT (replacement
, i
)
24104 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24106 /* If there are more levels then build up the replacement with the outer
24109 replacement
= add_to_template_args (template_parms_to_args
24110 (TREE_CHAIN (current_template_parms
)),
24113 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
24116 /* Entries in the decl_constraint hash table. */
24117 struct GTY((for_user
)) constr_entry
24123 /* Hashing function and equality for constraint entries. */
24124 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
24126 static hashval_t
hash (constr_entry
*e
)
24128 return (hashval_t
)DECL_UID (e
->decl
);
24131 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
24133 return e1
->decl
== e2
->decl
;
24137 /* A mapping from declarations to constraint information. Note that
24138 both templates and their underlying declarations are mapped to the
24139 same constraint information.
24141 FIXME: This is defined in pt.c because garbage collection
24142 code is not being generated for constraint.cc. */
24144 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
24146 /* Returns true iff cinfo contains a valid set of constraints.
24147 This is the case when the associated requirements have been
24148 successfully decomposed into lists of atomic constraints.
24149 That is, when the saved assumptions are not error_mark_node. */
24152 valid_constraints_p (tree cinfo
)
24154 gcc_assert (cinfo
);
24155 return CI_ASSUMPTIONS (cinfo
) != error_mark_node
;
24158 /* Returns the template constraints of declaration T. If T is not
24159 constrained, return NULL_TREE. Note that T must be non-null. */
24162 get_constraints (tree t
)
24164 gcc_assert (DECL_P (t
));
24165 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24166 t
= DECL_TEMPLATE_RESULT (t
);
24167 constr_entry elt
= { t
, NULL_TREE
};
24168 constr_entry
* found
= decl_constraints
->find (&elt
);
24175 /* Associate the given constraint information CI with the declaration
24176 T. If T is a template, then the constraints are associated with
24177 its underlying declaration. Don't build associations if CI is
24181 set_constraints (tree t
, tree ci
)
24186 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24187 t
= DECL_TEMPLATE_RESULT (t
);
24188 gcc_assert (!get_constraints (t
));
24189 constr_entry elt
= {t
, ci
};
24190 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
24191 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
24196 /* Remove the associated constraints of the declaration T. */
24199 remove_constraints (tree t
)
24201 gcc_assert (DECL_P (t
));
24202 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24203 t
= DECL_TEMPLATE_RESULT (t
);
24205 constr_entry elt
= {t
, NULL_TREE
};
24206 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
24208 decl_constraints
->clear_slot (slot
);
24211 /* Set up the hash table for constraint association. */
24214 init_constraint_processing (void)
24216 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
24219 /* Set up the hash tables for template instantiations. */
24222 init_template_processing (void)
24224 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24225 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24228 /* Print stats about the template hash tables for -fstats. */
24231 print_template_statistics (void)
24233 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
24234 "%f collisions\n", (long) decl_specializations
->size (),
24235 (long) decl_specializations
->elements (),
24236 decl_specializations
->collisions ());
24237 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
24238 "%f collisions\n", (long) type_specializations
->size (),
24239 (long) type_specializations
->elements (),
24240 type_specializations
->collisions ());
24243 #include "gt-cp-pt.h"