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
);
12295 else if (!cp_unevaluated_operand
)
12296 register_local_specialization (r
, t
);
12298 DECL_CHAIN (r
) = NULL_TREE
;
12300 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12302 args
, complain
, in_decl
);
12304 /* Preserve a typedef that names a type. */
12305 if (is_typedef_decl (r
))
12307 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12308 set_underlying_type (r
);
12309 if (TYPE_DECL_ALIAS_P (r
) && type
!= error_mark_node
)
12310 /* An alias template specialization can be dependent
12311 even if its underlying type is not. */
12312 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
12315 layout_decl (r
, 0);
12320 gcc_unreachable ();
12325 /* Restore the file and line information. */
12326 input_location
= saved_loc
;
12331 /* Substitute into the ARG_TYPES of a function type.
12332 If END is a TREE_CHAIN, leave it and any following types
12336 tsubst_arg_types (tree arg_types
,
12339 tsubst_flags_t complain
,
12342 tree remaining_arg_types
;
12343 tree type
= NULL_TREE
;
12345 tree expanded_args
= NULL_TREE
;
12348 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
12351 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
12352 args
, end
, complain
, in_decl
);
12353 if (remaining_arg_types
== error_mark_node
)
12354 return error_mark_node
;
12356 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
12358 /* For a pack expansion, perform substitution on the
12359 entire expression. Later on, we'll handle the arguments
12361 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
12362 args
, complain
, in_decl
);
12364 if (TREE_CODE (expanded_args
) == TREE_VEC
)
12365 /* So that we'll spin through the parameters, one by one. */
12366 i
= TREE_VEC_LENGTH (expanded_args
);
12369 /* We only partially substituted into the parameter
12370 pack. Our type is TYPE_PACK_EXPANSION. */
12371 type
= expanded_args
;
12372 expanded_args
= NULL_TREE
;
12380 type
= TREE_VEC_ELT (expanded_args
, i
);
12382 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
12384 if (type
== error_mark_node
)
12385 return error_mark_node
;
12386 if (VOID_TYPE_P (type
))
12388 if (complain
& tf_error
)
12390 error ("invalid parameter type %qT", type
);
12392 error ("in declaration %q+D", in_decl
);
12394 return error_mark_node
;
12397 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
12398 return error_mark_node
;
12400 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12401 top-level qualifiers as required. */
12402 type
= cv_unqualified (type_decays_to (type
));
12404 /* We do not substitute into default arguments here. The standard
12405 mandates that they be instantiated only when needed, which is
12406 done in build_over_call. */
12407 default_arg
= TREE_PURPOSE (arg_types
);
12409 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
12411 /* We've instantiated a template before its default arguments
12412 have been parsed. This can happen for a nested template
12413 class, and is not an error unless we require the default
12414 argument in a call of this function. */
12415 remaining_arg_types
=
12416 tree_cons (default_arg
, type
, remaining_arg_types
);
12417 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
12420 remaining_arg_types
=
12421 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
12424 return remaining_arg_types
;
12427 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12428 *not* handle the exception-specification for FNTYPE, because the
12429 initial substitution of explicitly provided template parameters
12430 during argument deduction forbids substitution into the
12431 exception-specification:
12435 All references in the function type of the function template to the
12436 corresponding template parameters are replaced by the specified tem-
12437 plate argument values. If a substitution in a template parameter or
12438 in the function type of the function template results in an invalid
12439 type, type deduction fails. [Note: The equivalent substitution in
12440 exception specifications is done only when the function is instanti-
12441 ated, at which point a program is ill-formed if the substitution
12442 results in an invalid type.] */
12445 tsubst_function_type (tree t
,
12447 tsubst_flags_t complain
,
12451 tree arg_types
= NULL_TREE
;
12454 /* The TYPE_CONTEXT is not used for function/method types. */
12455 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
12457 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12459 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
12461 if (late_return_type_p
)
12463 /* Substitute the argument types. */
12464 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12465 complain
, in_decl
);
12466 if (arg_types
== error_mark_node
)
12467 return error_mark_node
;
12469 tree save_ccp
= current_class_ptr
;
12470 tree save_ccr
= current_class_ref
;
12471 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
12472 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
12473 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
12476 /* DR 1207: 'this' is in scope in the trailing return type. */
12477 inject_this_parameter (this_type
, cp_type_quals (this_type
));
12480 /* Substitute the return type. */
12481 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12485 current_class_ptr
= save_ccp
;
12486 current_class_ref
= save_ccr
;
12490 /* Substitute the return type. */
12491 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12493 if (return_type
== error_mark_node
)
12494 return error_mark_node
;
12495 /* DR 486 clarifies that creation of a function type with an
12496 invalid return type is a deduction failure. */
12497 if (TREE_CODE (return_type
) == ARRAY_TYPE
12498 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
12500 if (complain
& tf_error
)
12502 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
12503 error ("function returning an array");
12505 error ("function returning a function");
12507 return error_mark_node
;
12510 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
12511 return error_mark_node
;
12513 if (!late_return_type_p
)
12515 /* Substitute the argument types. */
12516 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12517 complain
, in_decl
);
12518 if (arg_types
== error_mark_node
)
12519 return error_mark_node
;
12522 /* Construct a new type node and return it. */
12523 if (TREE_CODE (t
) == FUNCTION_TYPE
)
12525 fntype
= build_function_type (return_type
, arg_types
);
12526 fntype
= apply_memfn_quals (fntype
,
12527 type_memfn_quals (t
),
12528 type_memfn_rqual (t
));
12532 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
12533 /* Don't pick up extra function qualifiers from the basetype. */
12534 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
12535 if (! MAYBE_CLASS_TYPE_P (r
))
12539 Type deduction may fail for any of the following
12542 -- Attempting to create "pointer to member of T" when T
12543 is not a class type. */
12544 if (complain
& tf_error
)
12545 error ("creating pointer to member function of non-class type %qT",
12547 return error_mark_node
;
12550 fntype
= build_method_type_directly (r
, return_type
,
12551 TREE_CHAIN (arg_types
));
12552 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
12554 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
12556 if (late_return_type_p
)
12557 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
12562 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12563 ARGS into that specification, and return the substituted
12564 specification. If there is no specification, return NULL_TREE. */
12567 tsubst_exception_specification (tree fntype
,
12569 tsubst_flags_t complain
,
12576 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
12577 new_specs
= NULL_TREE
;
12578 if (specs
&& TREE_PURPOSE (specs
))
12580 /* A noexcept-specifier. */
12581 tree expr
= TREE_PURPOSE (specs
);
12582 if (TREE_CODE (expr
) == INTEGER_CST
)
12586 /* Defer instantiation of noexcept-specifiers to avoid
12587 excessive instantiations (c++/49107). */
12588 new_specs
= make_node (DEFERRED_NOEXCEPT
);
12589 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
12591 /* We already partially instantiated this member template,
12592 so combine the new args with the old. */
12593 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
12594 = DEFERRED_NOEXCEPT_PATTERN (expr
);
12595 DEFERRED_NOEXCEPT_ARGS (new_specs
)
12596 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
12600 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
12601 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
12605 new_specs
= tsubst_copy_and_build
12606 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
12607 /*integral_constant_expression_p=*/true);
12608 new_specs
= build_noexcept_spec (new_specs
, complain
);
12612 if (! TREE_VALUE (specs
))
12619 tree expanded_specs
= NULL_TREE
;
12621 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
12623 /* Expand the pack expansion type. */
12624 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
12628 if (expanded_specs
== error_mark_node
)
12629 return error_mark_node
;
12630 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
12631 len
= TREE_VEC_LENGTH (expanded_specs
);
12634 /* We're substituting into a member template, so
12635 we got a TYPE_PACK_EXPANSION back. Add that
12636 expansion and move on. */
12637 gcc_assert (TREE_CODE (expanded_specs
)
12638 == TYPE_PACK_EXPANSION
);
12639 new_specs
= add_exception_specifier (new_specs
,
12642 specs
= TREE_CHAIN (specs
);
12647 for (i
= 0; i
< len
; ++i
)
12649 if (expanded_specs
)
12650 spec
= TREE_VEC_ELT (expanded_specs
, i
);
12652 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
12653 if (spec
== error_mark_node
)
12655 new_specs
= add_exception_specifier (new_specs
, spec
,
12659 specs
= TREE_CHAIN (specs
);
12665 /* Take the tree structure T and replace template parameters used
12666 therein with the argument vector ARGS. IN_DECL is an associated
12667 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12668 Issue error and warning messages under control of COMPLAIN. Note
12669 that we must be relatively non-tolerant of extensions here, in
12670 order to preserve conformance; if we allow substitutions that
12671 should not be allowed, we may allow argument deductions that should
12672 not succeed, and therefore report ambiguous overload situations
12673 where there are none. In theory, we could allow the substitution,
12674 but indicate that it should have failed, and allow our caller to
12675 make sure that the right thing happens, but we don't try to do this
12678 This function is used for dealing with types, decls and the like;
12679 for expressions, use tsubst_expr or tsubst_copy. */
12682 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12684 enum tree_code code
;
12685 tree type
, r
= NULL_TREE
;
12687 if (t
== NULL_TREE
|| t
== error_mark_node
12688 || t
== integer_type_node
12689 || t
== void_type_node
12690 || t
== char_type_node
12691 || t
== unknown_type_node
12692 || TREE_CODE (t
) == NAMESPACE_DECL
12693 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
12697 return tsubst_decl (t
, args
, complain
);
12699 if (args
== NULL_TREE
)
12702 code
= TREE_CODE (t
);
12704 if (code
== IDENTIFIER_NODE
)
12705 type
= IDENTIFIER_TYPE_VALUE (t
);
12707 type
= TREE_TYPE (t
);
12709 gcc_assert (type
!= unknown_type_node
);
12711 /* Reuse typedefs. We need to do this to handle dependent attributes,
12712 such as attribute aligned. */
12714 && typedef_variant_p (t
))
12716 tree decl
= TYPE_NAME (t
);
12718 if (alias_template_specialization_p (t
))
12720 /* DECL represents an alias template and we want to
12722 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12723 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12724 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
12726 else if (DECL_CLASS_SCOPE_P (decl
)
12727 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12728 && uses_template_parms (DECL_CONTEXT (decl
)))
12730 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12731 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12732 r
= retrieve_specialization (tmpl
, gen_args
, 0);
12734 else if (DECL_FUNCTION_SCOPE_P (decl
)
12735 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12736 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
12737 r
= retrieve_local_specialization (decl
);
12739 /* The typedef is from a non-template context. */
12745 r
= cp_build_qualified_type_real
12746 (r
, cp_type_quals (t
) | cp_type_quals (r
),
12747 complain
| tf_ignore_bad_quals
);
12752 /* We don't have an instantiation yet, so drop the typedef. */
12753 int quals
= cp_type_quals (t
);
12754 t
= DECL_ORIGINAL_TYPE (decl
);
12755 t
= cp_build_qualified_type_real (t
, quals
,
12756 complain
| tf_ignore_bad_quals
);
12761 && code
!= TYPENAME_TYPE
12762 && code
!= TEMPLATE_TYPE_PARM
12763 && code
!= IDENTIFIER_NODE
12764 && code
!= FUNCTION_TYPE
12765 && code
!= METHOD_TYPE
)
12766 type
= tsubst (type
, args
, complain
, in_decl
);
12767 if (type
== error_mark_node
)
12768 return error_mark_node
;
12774 case ENUMERAL_TYPE
:
12775 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
12776 /*entering_scope=*/0);
12779 case IDENTIFIER_NODE
:
12790 if (t
== integer_type_node
)
12793 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
)
12795 if (!TYPE_MAX_VALUE (t
))
12796 return compute_array_index_type (NULL_TREE
, NULL_TREE
, complain
);
12798 if (TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
12803 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
12805 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
12806 /*integral_constant_expression_p=*/false);
12808 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12810 if (TREE_CODE (max
) == NOP_EXPR
12811 && TREE_SIDE_EFFECTS (omax
)
12812 && !TREE_TYPE (max
))
12813 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
12815 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12816 with TREE_SIDE_EFFECTS that indicates this is not an integral
12817 constant expression. */
12818 if (processing_template_decl
12819 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
12821 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
12822 TREE_SIDE_EFFECTS (max
) = 1;
12825 return compute_array_index_type (NULL_TREE
, max
, complain
);
12828 case TEMPLATE_TYPE_PARM
:
12829 case TEMPLATE_TEMPLATE_PARM
:
12830 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12831 case TEMPLATE_PARM_INDEX
:
12836 tree arg
= NULL_TREE
;
12838 /* Early in template argument deduction substitution, we don't
12839 want to reduce the level of 'auto', or it will be confused
12840 with a normal template parm in subsequent deduction. */
12841 if (is_auto (t
) && (complain
& tf_partial
))
12846 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
12847 template_parm_level_and_index (t
, &level
, &idx
);
12849 levels
= TMPL_ARGS_DEPTH (args
);
12850 if (level
<= levels
12851 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
12853 arg
= TMPL_ARG (args
, level
, idx
);
12855 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
12857 /* See through ARGUMENT_PACK_SELECT arguments. */
12858 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
12859 /* If the selected argument is an expansion E, that most
12860 likely means we were called from
12861 gen_elem_of_pack_expansion_instantiation during the
12862 substituting of pack an argument pack (which Ith
12863 element is a pack expansion, where I is
12864 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12865 In this case, the Ith element resulting from this
12866 substituting is going to be a pack expansion, which
12867 pattern is the pattern of E. Let's return the
12869 gen_elem_of_pack_expansion_instantiation will
12870 build the resulting pack expansion from it. */
12871 if (PACK_EXPANSION_P (arg
))
12873 /* Make sure we aren't throwing away arg info. */
12874 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
12875 arg
= PACK_EXPANSION_PATTERN (arg
);
12880 if (arg
== error_mark_node
)
12881 return error_mark_node
;
12882 else if (arg
!= NULL_TREE
)
12884 if (ARGUMENT_PACK_P (arg
))
12885 /* If ARG is an argument pack, we don't actually want to
12886 perform a substitution here, because substitutions
12887 for argument packs are only done
12888 element-by-element. We can get to this point when
12889 substituting the type of a non-type template
12890 parameter pack, when that type actually contains
12891 template parameter packs from an outer template, e.g.,
12893 template<typename... Types> struct A {
12894 template<Types... Values> struct B { };
12898 if (code
== TEMPLATE_TYPE_PARM
)
12901 gcc_assert (TYPE_P (arg
));
12903 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
12905 return cp_build_qualified_type_real
12906 (arg
, quals
, complain
| tf_ignore_bad_quals
);
12908 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12910 /* We are processing a type constructed from a
12911 template template parameter. */
12912 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
12913 args
, complain
, in_decl
);
12914 if (argvec
== error_mark_node
)
12915 return error_mark_node
;
12917 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
12918 || TREE_CODE (arg
) == TEMPLATE_DECL
12919 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
12921 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
12922 /* Consider this code:
12924 template <template <class> class Template>
12926 template <class Arg> using Bind = Template<Arg>;
12929 template <template <class> class Template, class Arg>
12930 using Instantiate = Template<Arg>; //#0
12932 template <template <class> class Template,
12935 Instantiate<Internal<Template>::template Bind,
12938 When #1 is parsed, the
12939 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12940 parameter `Template' in #0 matches the
12941 UNBOUND_CLASS_TEMPLATE representing the argument
12942 `Internal<Template>::template Bind'; We then want
12943 to assemble the type `Bind<Argument>' that can't
12944 be fully created right now, because
12945 `Internal<Template>' not being complete, the Bind
12946 template cannot be looked up in that context. So
12947 we need to "store" `Bind<Argument>' for later
12948 when the context of Bind becomes complete. Let's
12949 store that in a TYPENAME_TYPE. */
12950 return make_typename_type (TYPE_CONTEXT (arg
),
12951 build_nt (TEMPLATE_ID_EXPR
,
12952 TYPE_IDENTIFIER (arg
),
12957 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12958 are resolving nested-types in the signature of a
12959 member function templates. Otherwise ARG is a
12960 TEMPLATE_DECL and is the real template to be
12962 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
12963 arg
= TYPE_NAME (arg
);
12965 r
= lookup_template_class (arg
,
12967 DECL_CONTEXT (arg
),
12968 /*entering_scope=*/0,
12970 return cp_build_qualified_type_real
12971 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
12974 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12975 return convert_from_reference (unshare_expr (arg
));
12979 /* This can happen during the attempted tsubst'ing in
12980 unify. This means that we don't yet have any information
12981 about the template parameter in question. */
12984 /* If we get here, we must have been looking at a parm for a
12985 more deeply nested template. Make a new version of this
12986 template parameter, but with a lower level. */
12989 case TEMPLATE_TYPE_PARM
:
12990 case TEMPLATE_TEMPLATE_PARM
:
12991 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12992 if (cp_type_quals (t
))
12994 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
12995 r
= cp_build_qualified_type_real
12996 (r
, cp_type_quals (t
),
12997 complain
| (code
== TEMPLATE_TYPE_PARM
12998 ? tf_ignore_bad_quals
: 0));
13000 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13001 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13002 && (r
= (TEMPLATE_PARM_DESCENDANTS
13003 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13004 && (r
= TREE_TYPE (r
))
13005 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13006 /* Break infinite recursion when substituting the constraints
13007 of a constrained placeholder. */;
13011 TEMPLATE_TYPE_PARM_INDEX (r
)
13012 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13013 r
, levels
, args
, complain
);
13014 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13015 TYPE_MAIN_VARIANT (r
) = r
;
13016 TYPE_POINTER_TO (r
) = NULL_TREE
;
13017 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13019 /* Propagate constraints on placeholders. */
13020 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13021 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13022 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13023 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13025 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13026 /* We have reduced the level of the template
13027 template parameter, but not the levels of its
13028 template parameters, so canonical_type_parameter
13029 will not be able to find the canonical template
13030 template parameter for this level. Thus, we
13031 require structural equality checking to compare
13032 TEMPLATE_TEMPLATE_PARMs. */
13033 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13034 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13035 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13037 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13039 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13041 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
13042 complain
, in_decl
);
13043 if (argvec
== error_mark_node
)
13044 return error_mark_node
;
13046 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13047 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
13052 case TEMPLATE_PARM_INDEX
:
13053 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13057 gcc_unreachable ();
13065 tree purpose
, value
, chain
;
13067 if (t
== void_list_node
)
13070 purpose
= TREE_PURPOSE (t
);
13073 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13074 if (purpose
== error_mark_node
)
13075 return error_mark_node
;
13077 value
= TREE_VALUE (t
);
13080 value
= tsubst (value
, args
, complain
, in_decl
);
13081 if (value
== error_mark_node
)
13082 return error_mark_node
;
13084 chain
= TREE_CHAIN (t
);
13085 if (chain
&& chain
!= void_type_node
)
13087 chain
= tsubst (chain
, args
, complain
, in_decl
);
13088 if (chain
== error_mark_node
)
13089 return error_mark_node
;
13091 if (purpose
== TREE_PURPOSE (t
)
13092 && value
== TREE_VALUE (t
)
13093 && chain
== TREE_CHAIN (t
))
13095 return hash_tree_cons (purpose
, value
, chain
);
13099 /* We should never be tsubsting a binfo. */
13100 gcc_unreachable ();
13103 /* A vector of template arguments. */
13104 gcc_assert (!type
);
13105 return tsubst_template_args (t
, args
, complain
, in_decl
);
13108 case REFERENCE_TYPE
:
13110 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13115 Type deduction may fail for any of the following
13118 -- Attempting to create a pointer to reference type.
13119 -- Attempting to create a reference to a reference type or
13120 a reference to void.
13122 Core issue 106 says that creating a reference to a reference
13123 during instantiation is no longer a cause for failure. We
13124 only enforce this check in strict C++98 mode. */
13125 if ((TREE_CODE (type
) == REFERENCE_TYPE
13126 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
13127 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
13129 static location_t last_loc
;
13131 /* We keep track of the last time we issued this error
13132 message to avoid spewing a ton of messages during a
13133 single bad template instantiation. */
13134 if (complain
& tf_error
13135 && last_loc
!= input_location
)
13137 if (VOID_TYPE_P (type
))
13138 error ("forming reference to void");
13139 else if (code
== POINTER_TYPE
)
13140 error ("forming pointer to reference type %qT", type
);
13142 error ("forming reference to reference type %qT", type
);
13143 last_loc
= input_location
;
13146 return error_mark_node
;
13148 else if (TREE_CODE (type
) == FUNCTION_TYPE
13149 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13150 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13152 if (complain
& tf_error
)
13154 if (code
== POINTER_TYPE
)
13155 error ("forming pointer to qualified function type %qT",
13158 error ("forming reference to qualified function type %qT",
13161 return error_mark_node
;
13163 else if (code
== POINTER_TYPE
)
13165 r
= build_pointer_type (type
);
13166 if (TREE_CODE (type
) == METHOD_TYPE
)
13167 r
= build_ptrmemfunc_type (r
);
13169 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
13170 /* In C++0x, during template argument substitution, when there is an
13171 attempt to create a reference to a reference type, reference
13172 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13174 "If a template-argument for a template-parameter T names a type
13175 that is a reference to a type A, an attempt to create the type
13176 'lvalue reference to cv T' creates the type 'lvalue reference to
13177 A,' while an attempt to create the type type rvalue reference to
13178 cv T' creates the type T"
13180 r
= cp_build_reference_type
13182 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
13184 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
13185 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13187 if (r
!= error_mark_node
)
13188 /* Will this ever be needed for TYPE_..._TO values? */
13195 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
13196 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
13200 Type deduction may fail for any of the following
13203 -- Attempting to create "pointer to member of T" when T
13204 is not a class type. */
13205 if (complain
& tf_error
)
13206 error ("creating pointer to member of non-class type %qT", r
);
13207 return error_mark_node
;
13209 if (TREE_CODE (type
) == REFERENCE_TYPE
)
13211 if (complain
& tf_error
)
13212 error ("creating pointer to member reference type %qT", type
);
13213 return error_mark_node
;
13215 if (VOID_TYPE_P (type
))
13217 if (complain
& tf_error
)
13218 error ("creating pointer to member of type void");
13219 return error_mark_node
;
13221 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13222 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13224 /* The type of the implicit object parameter gets its
13225 cv-qualifiers from the FUNCTION_TYPE. */
13228 = build_memfn_type (type
, r
, type_memfn_quals (type
),
13229 type_memfn_rqual (type
));
13230 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
13231 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
13235 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
13239 case FUNCTION_TYPE
:
13244 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
13245 if (fntype
== error_mark_node
)
13246 return error_mark_node
;
13248 /* Substitute the exception specification. */
13249 specs
= tsubst_exception_specification (t
, args
, complain
,
13250 in_decl
, /*defer_ok*/true);
13251 if (specs
== error_mark_node
)
13252 return error_mark_node
;
13254 fntype
= build_exception_variant (fntype
, specs
);
13259 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
13260 if (domain
== error_mark_node
)
13261 return error_mark_node
;
13263 /* As an optimization, we avoid regenerating the array type if
13264 it will obviously be the same as T. */
13265 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
13268 /* These checks should match the ones in create_array_type_for_decl.
13272 The deduction may fail for any of the following reasons:
13274 -- Attempting to create an array with an element type that
13275 is void, a function type, or a reference type, or [DR337]
13276 an abstract class type. */
13277 if (VOID_TYPE_P (type
)
13278 || TREE_CODE (type
) == FUNCTION_TYPE
13279 || (TREE_CODE (type
) == ARRAY_TYPE
13280 && TYPE_DOMAIN (type
) == NULL_TREE
)
13281 || TREE_CODE (type
) == REFERENCE_TYPE
)
13283 if (complain
& tf_error
)
13284 error ("creating array of %qT", type
);
13285 return error_mark_node
;
13288 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
13289 return error_mark_node
;
13291 r
= build_cplus_array_type (type
, domain
);
13293 if (TYPE_USER_ALIGN (t
))
13295 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
13296 TYPE_USER_ALIGN (r
) = 1;
13302 case TYPENAME_TYPE
:
13304 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13305 in_decl
, /*entering_scope=*/1);
13306 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
13307 complain
, in_decl
);
13309 if (ctx
== error_mark_node
|| f
== error_mark_node
)
13310 return error_mark_node
;
13312 if (!MAYBE_CLASS_TYPE_P (ctx
))
13314 if (complain
& tf_error
)
13315 error ("%qT is not a class, struct, or union type", ctx
);
13316 return error_mark_node
;
13318 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
13320 /* Normally, make_typename_type does not require that the CTX
13321 have complete type in order to allow things like:
13323 template <class T> struct S { typename S<T>::X Y; };
13325 But, such constructs have already been resolved by this
13326 point, so here CTX really should have complete type, unless
13327 it's a partial instantiation. */
13328 ctx
= complete_type (ctx
);
13329 if (!COMPLETE_TYPE_P (ctx
))
13331 if (complain
& tf_error
)
13332 cxx_incomplete_type_error (NULL_TREE
, ctx
);
13333 return error_mark_node
;
13337 f
= make_typename_type (ctx
, f
, typename_type
,
13338 complain
| tf_keep_type_decl
);
13339 if (f
== error_mark_node
)
13341 if (TREE_CODE (f
) == TYPE_DECL
)
13343 complain
|= tf_ignore_bad_quals
;
13347 if (TREE_CODE (f
) != TYPENAME_TYPE
)
13349 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
13351 if (complain
& tf_error
)
13352 error ("%qT resolves to %qT, which is not an enumeration type",
13355 return error_mark_node
;
13357 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
13359 if (complain
& tf_error
)
13360 error ("%qT resolves to %qT, which is is not a class type",
13363 return error_mark_node
;
13367 return cp_build_qualified_type_real
13368 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
13371 case UNBOUND_CLASS_TEMPLATE
:
13373 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13374 in_decl
, /*entering_scope=*/1);
13375 tree name
= TYPE_IDENTIFIER (t
);
13376 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
13378 if (ctx
== error_mark_node
|| name
== error_mark_node
)
13379 return error_mark_node
;
13382 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
13383 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
13390 ++cp_unevaluated_operand
;
13391 ++c_inhibit_evaluation_warnings
;
13393 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
13395 /*integral_constant_expression_p=*/false);
13397 --cp_unevaluated_operand
;
13398 --c_inhibit_evaluation_warnings
;
13400 type
= finish_typeof (type
);
13401 return cp_build_qualified_type_real (type
,
13403 | cp_type_quals (type
),
13407 case DECLTYPE_TYPE
:
13411 ++cp_unevaluated_operand
;
13412 ++c_inhibit_evaluation_warnings
;
13414 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
13415 complain
|tf_decltype
, in_decl
,
13416 /*function_p*/false,
13417 /*integral_constant_expression*/false);
13419 --cp_unevaluated_operand
;
13420 --c_inhibit_evaluation_warnings
;
13422 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
13423 type
= lambda_capture_field_type (type
,
13424 DECLTYPE_FOR_INIT_CAPTURE (t
));
13425 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
13426 type
= lambda_proxy_type (type
);
13429 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
13430 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
13432 /* In a template ~id could be either a complement expression
13433 or an unqualified-id naming a destructor; if instantiating
13434 it produces an expression, it's not an id-expression or
13437 type
= finish_decltype_type (type
, id
, complain
);
13439 return cp_build_qualified_type_real (type
,
13441 | cp_type_quals (type
),
13442 complain
| tf_ignore_bad_quals
);
13445 case UNDERLYING_TYPE
:
13447 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
13448 complain
, in_decl
);
13449 return finish_underlying_type (type
);
13452 case TYPE_ARGUMENT_PACK
:
13453 case NONTYPE_ARGUMENT_PACK
:
13455 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
13457 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
13461 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
13463 /* For template nontype argument packs, also substitute into
13465 if (code
== NONTYPE_ARGUMENT_PACK
)
13466 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13485 /* We should use one of the expression tsubsts for these codes. */
13486 gcc_unreachable ();
13489 sorry ("use of %qs in template", get_tree_code_name (code
));
13490 return error_mark_node
;
13494 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13495 type of the expression on the left-hand side of the "." or "->"
13499 tsubst_baselink (tree baselink
, tree object_type
,
13500 tree args
, tsubst_flags_t complain
, tree in_decl
)
13503 tree qualifying_scope
;
13506 tree template_args
= 0;
13507 bool template_id_p
= false;
13508 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
13510 /* A baselink indicates a function from a base class. Both the
13511 BASELINK_ACCESS_BINFO and the base class referenced may
13512 indicate bases of the template class, rather than the
13513 instantiated class. In addition, lookups that were not
13514 ambiguous before may be ambiguous now. Therefore, we perform
13515 the lookup again. */
13516 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
13517 qualifying_scope
= tsubst (qualifying_scope
, args
,
13518 complain
, in_decl
);
13519 fns
= BASELINK_FUNCTIONS (baselink
);
13520 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
13521 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
13523 template_id_p
= true;
13524 template_args
= TREE_OPERAND (fns
, 1);
13525 fns
= TREE_OPERAND (fns
, 0);
13527 template_args
= tsubst_template_args (template_args
, args
,
13528 complain
, in_decl
);
13530 name
= DECL_NAME (get_first_fn (fns
));
13531 if (IDENTIFIER_TYPENAME_P (name
))
13532 name
= mangle_conv_op_name_for_type (optype
);
13533 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
13535 return error_mark_node
;
13537 /* If lookup found a single function, mark it as used at this
13538 point. (If it lookup found multiple functions the one selected
13539 later by overload resolution will be marked as used at that
13541 if (BASELINK_P (baselink
))
13542 fns
= BASELINK_FUNCTIONS (baselink
);
13543 if (!template_id_p
&& !really_overloaded_fn (fns
)
13544 && !mark_used (OVL_CURRENT (fns
), complain
) && !(complain
& tf_error
))
13545 return error_mark_node
;
13547 /* Add back the template arguments, if present. */
13548 if (BASELINK_P (baselink
) && template_id_p
)
13549 BASELINK_FUNCTIONS (baselink
)
13550 = build_nt (TEMPLATE_ID_EXPR
,
13551 BASELINK_FUNCTIONS (baselink
),
13553 /* Update the conversion operator type. */
13554 BASELINK_OPTYPE (baselink
) = optype
;
13557 object_type
= current_class_type
;
13560 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
13566 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13567 true if the qualified-id will be a postfix-expression in-and-of
13568 itself; false if more of the postfix-expression follows the
13569 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13573 tsubst_qualified_id (tree qualified_id
, tree args
,
13574 tsubst_flags_t complain
, tree in_decl
,
13575 bool done
, bool address_p
)
13581 tree template_args
;
13582 location_t loc
= UNKNOWN_LOCATION
;
13584 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
13586 /* Figure out what name to look up. */
13587 name
= TREE_OPERAND (qualified_id
, 1);
13588 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13590 is_template
= true;
13591 loc
= EXPR_LOCATION (name
);
13592 template_args
= TREE_OPERAND (name
, 1);
13594 template_args
= tsubst_template_args (template_args
, args
,
13595 complain
, in_decl
);
13596 name
= TREE_OPERAND (name
, 0);
13600 is_template
= false;
13601 template_args
= NULL_TREE
;
13604 /* Substitute into the qualifying scope. When there are no ARGS, we
13605 are just trying to simplify a non-dependent expression. In that
13606 case the qualifying scope may be dependent, and, in any case,
13607 substituting will not help. */
13608 scope
= TREE_OPERAND (qualified_id
, 0);
13611 scope
= tsubst (scope
, args
, complain
, in_decl
);
13612 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
13617 if (dependent_scope_p (scope
))
13620 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
13621 return build_qualified_name (NULL_TREE
, scope
, expr
,
13622 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
13625 if (!BASELINK_P (name
) && !DECL_P (expr
))
13627 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
13629 /* A BIT_NOT_EXPR is used to represent a destructor. */
13630 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
13632 error ("qualifying type %qT does not match destructor name ~%qT",
13633 scope
, TREE_OPERAND (expr
, 0));
13634 expr
= error_mark_node
;
13637 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
13638 /*is_type_p=*/0, false);
13641 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
13642 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
13643 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
13645 if (complain
& tf_error
)
13647 error ("dependent-name %qE is parsed as a non-type, but "
13648 "instantiation yields a type", qualified_id
);
13649 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
13651 return error_mark_node
;
13657 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
13659 /* Remember that there was a reference to this entity. */
13660 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
13661 return error_mark_node
;
13664 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
13666 if (complain
& tf_error
)
13667 qualified_name_lookup_error (scope
,
13668 TREE_OPERAND (qualified_id
, 1),
13669 expr
, input_location
);
13670 return error_mark_node
;
13674 expr
= lookup_template_function (expr
, template_args
);
13676 if (expr
== error_mark_node
&& complain
& tf_error
)
13677 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
13678 expr
, input_location
);
13679 else if (TYPE_P (scope
))
13681 expr
= (adjust_result_of_qualified_name_lookup
13682 (expr
, scope
, current_nonlambda_class_type ()));
13683 expr
= (finish_qualified_id_expr
13684 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
13685 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
13686 /*template_arg_p=*/false, complain
));
13689 /* Expressions do not generally have reference type. */
13690 if (TREE_CODE (expr
) != SCOPE_REF
13691 /* However, if we're about to form a pointer-to-member, we just
13692 want the referenced member referenced. */
13693 && TREE_CODE (expr
) != OFFSET_REF
)
13694 expr
= convert_from_reference (expr
);
13699 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13700 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13704 tsubst_init (tree init
, tree decl
, tree args
,
13705 tsubst_flags_t complain
, tree in_decl
)
13710 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
13714 /* If we had an initializer but it
13715 instantiated to nothing,
13716 value-initialize the object. This will
13717 only occur when the initializer was a
13718 pack expansion where the parameter packs
13719 used in that expansion were of length
13721 init
= build_value_init (TREE_TYPE (decl
),
13723 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13724 init
= get_target_expr_sfinae (init
, complain
);
13730 /* Like tsubst, but deals with expressions. This function just replaces
13731 template parms; to finish processing the resultant expression, use
13732 tsubst_copy_and_build or tsubst_expr. */
13735 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13737 enum tree_code code
;
13740 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
13743 code
= TREE_CODE (t
);
13748 r
= retrieve_local_specialization (t
);
13750 if (r
== NULL_TREE
)
13752 /* We get here for a use of 'this' in an NSDMI. */
13753 if (DECL_NAME (t
) == this_identifier
13754 && current_function_decl
13755 && DECL_CONSTRUCTOR_P (current_function_decl
))
13756 return current_class_ptr
;
13758 /* This can happen for a parameter name used later in a function
13759 declaration (such as in a late-specified return type). Just
13760 make a dummy decl, since it's only used for its type. */
13761 gcc_assert (cp_unevaluated_operand
!= 0);
13762 r
= tsubst_decl (t
, args
, complain
);
13763 /* Give it the template pattern as its context; its true context
13764 hasn't been instantiated yet and this is good enough for
13766 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
13769 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13770 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13771 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13772 return error_mark_node
;
13780 if (DECL_TEMPLATE_PARM_P (t
))
13781 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
13782 /* There is no need to substitute into namespace-scope
13784 if (DECL_NAMESPACE_SCOPE_P (t
))
13786 /* If ARGS is NULL, then T is known to be non-dependent. */
13787 if (args
== NULL_TREE
)
13788 return scalar_constant_value (t
);
13790 /* Unfortunately, we cannot just call lookup_name here.
13793 template <int I> int f() {
13795 struct S { void g() { E e = a; } };
13798 When we instantiate f<7>::S::g(), say, lookup_name is not
13799 clever enough to find f<7>::a. */
13801 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13802 /*entering_scope=*/0);
13804 for (v
= TYPE_VALUES (enum_type
);
13806 v
= TREE_CHAIN (v
))
13807 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
13808 return TREE_VALUE (v
);
13810 /* We didn't find the name. That should never happen; if
13811 name-lookup found it during preliminary parsing, we
13812 should find it again here during instantiation. */
13813 gcc_unreachable ();
13818 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13820 /* Check for a local specialization set up by
13821 tsubst_pack_expansion. */
13822 if (tree r
= retrieve_local_specialization (t
))
13824 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13825 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13829 /* When retrieving a capture pack from a generic lambda, remove the
13830 lambda call op's own template argument list from ARGS. Only the
13831 template arguments active for the closure type should be used to
13832 retrieve the pack specialization. */
13833 if (LAMBDA_FUNCTION_P (current_function_decl
)
13834 && (template_class_depth (DECL_CONTEXT (t
))
13835 != TMPL_ARGS_DEPTH (args
)))
13836 args
= strip_innermost_template_args (args
, 1);
13838 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13839 tsubst_decl put in the hash table. */
13840 return retrieve_specialization (t
, args
, 0);
13843 if (DECL_CONTEXT (t
))
13847 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13848 /*entering_scope=*/1);
13849 if (ctx
!= DECL_CONTEXT (t
))
13851 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
13854 if (complain
& tf_error
)
13855 error ("using invalid field %qD", t
);
13856 return error_mark_node
;
13865 case FUNCTION_DECL
:
13866 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
13867 r
= tsubst (t
, args
, complain
, in_decl
);
13868 else if (local_variable_p (t
))
13870 r
= retrieve_local_specialization (t
);
13871 if (r
== NULL_TREE
)
13873 /* First try name lookup to find the instantiation. */
13874 r
= lookup_name (DECL_NAME (t
));
13877 /* Make sure that the one we found is the one we want. */
13878 tree ctx
= DECL_CONTEXT (t
);
13879 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
13880 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
13881 if (ctx
!= DECL_CONTEXT (r
))
13889 /* This can happen for a variable used in a
13890 late-specified return type of a local lambda, or for a
13891 local static or constant. Building a new VAR_DECL
13892 should be OK in all those cases. */
13893 r
= tsubst_decl (t
, args
, complain
);
13894 if (decl_maybe_constant_var_p (r
))
13896 /* We can't call cp_finish_decl, so handle the
13897 initializer by hand. */
13898 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
13899 complain
, in_decl
);
13900 if (!processing_template_decl
)
13901 init
= maybe_constant_init (init
);
13902 if (processing_template_decl
13903 ? potential_constant_expression (init
)
13904 : reduced_constant_expression_p (init
))
13905 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
13906 = TREE_CONSTANT (r
) = true;
13907 DECL_INITIAL (r
) = init
;
13909 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
13910 || decl_constant_var_p (r
)
13911 || errorcount
|| sorrycount
);
13912 if (!processing_template_decl
)
13914 if (TREE_STATIC (r
))
13915 rest_of_decl_compilation (r
, toplevel_bindings_p (),
13918 r
= process_outer_var_ref (r
, complain
);
13921 /* Remember this for subsequent uses. */
13922 if (local_specializations
)
13923 register_local_specialization (r
, t
);
13928 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13929 return error_mark_node
;
13932 case NAMESPACE_DECL
:
13936 /* An OVERLOAD will always be a non-dependent overload set; an
13937 overload set from function scope will just be represented with an
13938 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13939 gcc_assert (!uses_template_parms (t
));
13943 return tsubst_baselink (t
, current_nonlambda_class_type (),
13944 args
, complain
, in_decl
);
13946 case TEMPLATE_DECL
:
13947 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13948 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
13949 args
, complain
, in_decl
);
13950 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
13951 return tsubst (t
, args
, complain
, in_decl
);
13952 else if (DECL_CLASS_SCOPE_P (t
)
13953 && uses_template_parms (DECL_CONTEXT (t
)))
13955 /* Template template argument like the following example need
13958 template <template <class> class TT> struct C {};
13959 template <class T> struct D {
13960 template <class U> struct E {};
13965 We are processing the template argument `E' in #1 for
13966 the template instantiation #2. Originally, `E' is a
13967 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13968 have to substitute this with one having context `D<int>'. */
13970 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
13971 return lookup_field (context
, DECL_NAME(t
), 0, false);
13974 /* Ordinary template template argument. */
13978 case REINTERPRET_CAST_EXPR
:
13979 case CONST_CAST_EXPR
:
13980 case STATIC_CAST_EXPR
:
13981 case DYNAMIC_CAST_EXPR
:
13982 case IMPLICIT_CONV_EXPR
:
13986 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13987 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13988 return build1 (code
, type
, op0
);
13992 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13994 tree expanded
, op
= TREE_OPERAND (t
, 0);
13997 if (SIZEOF_EXPR_TYPE_P (t
))
13998 op
= TREE_TYPE (op
);
14000 ++cp_unevaluated_operand
;
14001 ++c_inhibit_evaluation_warnings
;
14002 /* We only want to compute the number of arguments. */
14003 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14004 --cp_unevaluated_operand
;
14005 --c_inhibit_evaluation_warnings
;
14007 if (TREE_CODE (expanded
) == TREE_VEC
)
14008 len
= TREE_VEC_LENGTH (expanded
);
14010 if (expanded
== error_mark_node
)
14011 return error_mark_node
;
14012 else if (PACK_EXPANSION_P (expanded
)
14013 || (TREE_CODE (expanded
) == TREE_VEC
14015 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
14017 if (TREE_CODE (expanded
) == TREE_VEC
)
14018 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
14020 PACK_EXPANSION_SIZEOF_P (expanded
) = true;
14022 if (TYPE_P (expanded
))
14023 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14024 complain
& tf_error
);
14026 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14027 complain
& tf_error
);
14030 return build_int_cst (size_type_node
, len
);
14032 if (SIZEOF_EXPR_TYPE_P (t
))
14034 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14035 args
, complain
, in_decl
);
14036 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14037 r
= build1 (SIZEOF_EXPR
,
14038 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14039 SIZEOF_EXPR_TYPE_P (r
) = 1;
14046 case TRUTH_NOT_EXPR
:
14049 case UNARY_PLUS_EXPR
: /* Unary + */
14051 case AT_ENCODE_EXPR
:
14055 case REALPART_EXPR
:
14056 case IMAGPART_EXPR
:
14059 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14060 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14061 return build1 (code
, type
, op0
);
14064 case COMPONENT_REF
:
14069 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14070 name
= TREE_OPERAND (t
, 1);
14071 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14073 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14074 complain
, in_decl
);
14075 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14077 else if (TREE_CODE (name
) == SCOPE_REF
14078 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14080 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14081 complain
, in_decl
);
14082 name
= TREE_OPERAND (name
, 1);
14083 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14084 complain
, in_decl
);
14085 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14086 name
= build_qualified_name (/*type=*/NULL_TREE
,
14088 /*template_p=*/false);
14090 else if (BASELINK_P (name
))
14091 name
= tsubst_baselink (name
,
14092 non_reference (TREE_TYPE (object
)),
14096 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14097 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14103 case TRUNC_DIV_EXPR
:
14104 case CEIL_DIV_EXPR
:
14105 case FLOOR_DIV_EXPR
:
14106 case ROUND_DIV_EXPR
:
14107 case EXACT_DIV_EXPR
:
14111 case TRUNC_MOD_EXPR
:
14112 case FLOOR_MOD_EXPR
:
14113 case TRUTH_ANDIF_EXPR
:
14114 case TRUTH_ORIF_EXPR
:
14115 case TRUTH_AND_EXPR
:
14116 case TRUTH_OR_EXPR
:
14129 case COMPOUND_EXPR
:
14132 case PREDECREMENT_EXPR
:
14133 case PREINCREMENT_EXPR
:
14134 case POSTDECREMENT_EXPR
:
14135 case POSTINCREMENT_EXPR
:
14137 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14138 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14139 return build_nt (code
, op0
, op1
);
14144 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14145 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14146 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14147 QUALIFIED_NAME_IS_TEMPLATE (t
));
14152 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14153 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14154 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14159 int n
= VL_EXP_OPERAND_LENGTH (t
);
14160 tree result
= build_vl_exp (CALL_EXPR
, n
);
14162 for (i
= 0; i
< n
; i
++)
14163 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14164 complain
, in_decl
);
14170 case PSEUDO_DTOR_EXPR
:
14171 case VEC_PERM_EXPR
:
14173 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14174 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14175 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14176 r
= build_nt (code
, op0
, op1
, op2
);
14177 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14183 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14184 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14185 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14186 r
= build_nt (code
, op0
, op1
, op2
);
14187 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14193 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14194 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14195 r
= build_nt (code
, op0
, op1
);
14196 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14197 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14201 case TEMPLATE_ID_EXPR
:
14203 /* Substituted template arguments */
14204 tree fn
= TREE_OPERAND (t
, 0);
14205 tree targs
= TREE_OPERAND (t
, 1);
14207 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14209 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14211 return lookup_template_function (fn
, targs
);
14216 tree purpose
, value
, chain
;
14218 if (t
== void_list_node
)
14221 purpose
= TREE_PURPOSE (t
);
14223 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
14224 value
= TREE_VALUE (t
);
14226 value
= tsubst_copy (value
, args
, complain
, in_decl
);
14227 chain
= TREE_CHAIN (t
);
14228 if (chain
&& chain
!= void_type_node
)
14229 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
14230 if (purpose
== TREE_PURPOSE (t
)
14231 && value
== TREE_VALUE (t
)
14232 && chain
== TREE_CHAIN (t
))
14234 return tree_cons (purpose
, value
, chain
);
14239 case ENUMERAL_TYPE
:
14241 case TEMPLATE_TYPE_PARM
:
14242 case TEMPLATE_TEMPLATE_PARM
:
14243 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14244 case TEMPLATE_PARM_INDEX
:
14246 case REFERENCE_TYPE
:
14248 case FUNCTION_TYPE
:
14251 case TYPENAME_TYPE
:
14252 case UNBOUND_CLASS_TEMPLATE
:
14254 case DECLTYPE_TYPE
:
14256 return tsubst (t
, args
, complain
, in_decl
);
14260 /* Fall through. */
14261 case IDENTIFIER_NODE
:
14262 if (IDENTIFIER_TYPENAME_P (t
))
14264 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14265 return mangle_conv_op_name_for_type (new_type
);
14271 /* This is handled by tsubst_copy_and_build. */
14272 gcc_unreachable ();
14276 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14277 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14278 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
14281 case CLEANUP_POINT_EXPR
:
14282 /* We shouldn't have built any of these during initial template
14283 generation. Instead, they should be built during instantiation
14284 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14285 gcc_unreachable ();
14289 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14290 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14291 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14292 r
= build2 (code
, type
, op0
, op1
);
14293 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
14294 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
14295 && !(complain
& tf_error
))
14296 return error_mark_node
;
14300 case EXPR_PACK_EXPANSION
:
14301 error ("invalid use of pack expansion expression");
14302 return error_mark_node
;
14304 case NONTYPE_ARGUMENT_PACK
:
14305 error ("use %<...%> to expand argument pack");
14306 return error_mark_node
;
14309 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
14317 /* Instantiate any typedefs in the type. */
14318 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14319 r
= fold_convert (type
, t
);
14320 gcc_assert (TREE_CODE (r
) == code
);
14325 /* These can sometimes show up in a partial instantiation, but never
14326 involve template parms. */
14327 gcc_assert (!uses_template_parms (t
));
14330 case UNARY_LEFT_FOLD_EXPR
:
14331 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
14332 case UNARY_RIGHT_FOLD_EXPR
:
14333 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
14334 case BINARY_LEFT_FOLD_EXPR
:
14335 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
14336 case BINARY_RIGHT_FOLD_EXPR
:
14337 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
14340 /* We shouldn't get here, but keep going if !flag_checking. */
14342 gcc_unreachable ();
14347 /* Helper function for tsubst_omp_clauses, used for instantiation of
14348 OMP_CLAUSE_DECL of clauses. */
14351 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
14354 if (decl
== NULL_TREE
)
14357 /* Handle an OpenMP array section represented as a TREE_LIST (or
14358 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14359 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14360 TREE_LIST. We can handle it exactly the same as an array section
14361 (purpose, value, and a chain), even though the nomenclature
14362 (low_bound, length, etc) is different. */
14363 if (TREE_CODE (decl
) == TREE_LIST
)
14366 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
14367 /*integral_constant_expression_p=*/false);
14368 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
14369 /*integral_constant_expression_p=*/false);
14370 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
14372 if (TREE_PURPOSE (decl
) == low_bound
14373 && TREE_VALUE (decl
) == length
14374 && TREE_CHAIN (decl
) == chain
)
14376 tree ret
= tree_cons (low_bound
, length
, chain
);
14377 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
14378 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
14381 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
14382 /*integral_constant_expression_p=*/false);
14383 /* Undo convert_from_reference tsubst_expr could have called. */
14385 && REFERENCE_REF_P (ret
)
14386 && !REFERENCE_REF_P (decl
))
14387 ret
= TREE_OPERAND (ret
, 0);
14391 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14394 tsubst_omp_clauses (tree clauses
, bool declare_simd
, bool allow_fields
,
14395 tree args
, tsubst_flags_t complain
, tree in_decl
)
14397 tree new_clauses
= NULL_TREE
, nc
, oc
;
14398 tree linear_no_step
= NULL_TREE
;
14400 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
14402 nc
= copy_node (oc
);
14403 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
14406 switch (OMP_CLAUSE_CODE (nc
))
14408 case OMP_CLAUSE_LASTPRIVATE
:
14409 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
14411 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
14412 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
14413 in_decl
, /*integral_constant_expression_p=*/false);
14414 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
14415 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
14418 case OMP_CLAUSE_PRIVATE
:
14419 case OMP_CLAUSE_SHARED
:
14420 case OMP_CLAUSE_FIRSTPRIVATE
:
14421 case OMP_CLAUSE_COPYIN
:
14422 case OMP_CLAUSE_COPYPRIVATE
:
14423 case OMP_CLAUSE_UNIFORM
:
14424 case OMP_CLAUSE_DEPEND
:
14425 case OMP_CLAUSE_FROM
:
14426 case OMP_CLAUSE_TO
:
14427 case OMP_CLAUSE_MAP
:
14428 case OMP_CLAUSE_USE_DEVICE_PTR
:
14429 case OMP_CLAUSE_IS_DEVICE_PTR
:
14430 OMP_CLAUSE_DECL (nc
)
14431 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14434 case OMP_CLAUSE_IF
:
14435 case OMP_CLAUSE_NUM_THREADS
:
14436 case OMP_CLAUSE_SCHEDULE
:
14437 case OMP_CLAUSE_COLLAPSE
:
14438 case OMP_CLAUSE_FINAL
:
14439 case OMP_CLAUSE_DEVICE
:
14440 case OMP_CLAUSE_DIST_SCHEDULE
:
14441 case OMP_CLAUSE_NUM_TEAMS
:
14442 case OMP_CLAUSE_THREAD_LIMIT
:
14443 case OMP_CLAUSE_SAFELEN
:
14444 case OMP_CLAUSE_SIMDLEN
:
14445 case OMP_CLAUSE_NUM_TASKS
:
14446 case OMP_CLAUSE_GRAINSIZE
:
14447 case OMP_CLAUSE_PRIORITY
:
14448 case OMP_CLAUSE_ORDERED
:
14449 case OMP_CLAUSE_HINT
:
14450 case OMP_CLAUSE_NUM_GANGS
:
14451 case OMP_CLAUSE_NUM_WORKERS
:
14452 case OMP_CLAUSE_VECTOR_LENGTH
:
14453 case OMP_CLAUSE_WORKER
:
14454 case OMP_CLAUSE_VECTOR
:
14455 case OMP_CLAUSE_ASYNC
:
14456 case OMP_CLAUSE_WAIT
:
14457 OMP_CLAUSE_OPERAND (nc
, 0)
14458 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14459 in_decl
, /*integral_constant_expression_p=*/false);
14461 case OMP_CLAUSE_REDUCTION
:
14462 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
14464 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
14465 if (TREE_CODE (placeholder
) == SCOPE_REF
)
14467 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
14468 complain
, in_decl
);
14469 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
14470 = build_qualified_name (NULL_TREE
, scope
,
14471 TREE_OPERAND (placeholder
, 1),
14475 gcc_assert (identifier_p (placeholder
));
14477 OMP_CLAUSE_DECL (nc
)
14478 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14481 case OMP_CLAUSE_GANG
:
14482 case OMP_CLAUSE_ALIGNED
:
14483 OMP_CLAUSE_DECL (nc
)
14484 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14486 OMP_CLAUSE_OPERAND (nc
, 1)
14487 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
14488 in_decl
, /*integral_constant_expression_p=*/false);
14490 case OMP_CLAUSE_LINEAR
:
14491 OMP_CLAUSE_DECL (nc
)
14492 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14494 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
14496 gcc_assert (!linear_no_step
);
14497 linear_no_step
= nc
;
14499 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
14500 OMP_CLAUSE_LINEAR_STEP (nc
)
14501 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
14502 complain
, in_decl
);
14504 OMP_CLAUSE_LINEAR_STEP (nc
)
14505 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
14507 /*integral_constant_expression_p=*/false);
14509 case OMP_CLAUSE_NOWAIT
:
14510 case OMP_CLAUSE_DEFAULT
:
14511 case OMP_CLAUSE_UNTIED
:
14512 case OMP_CLAUSE_MERGEABLE
:
14513 case OMP_CLAUSE_INBRANCH
:
14514 case OMP_CLAUSE_NOTINBRANCH
:
14515 case OMP_CLAUSE_PROC_BIND
:
14516 case OMP_CLAUSE_FOR
:
14517 case OMP_CLAUSE_PARALLEL
:
14518 case OMP_CLAUSE_SECTIONS
:
14519 case OMP_CLAUSE_TASKGROUP
:
14520 case OMP_CLAUSE_NOGROUP
:
14521 case OMP_CLAUSE_THREADS
:
14522 case OMP_CLAUSE_SIMD
:
14523 case OMP_CLAUSE_DEFAULTMAP
:
14524 case OMP_CLAUSE_INDEPENDENT
:
14525 case OMP_CLAUSE_AUTO
:
14526 case OMP_CLAUSE_SEQ
:
14528 case OMP_CLAUSE_TILE
:
14531 for (lnc
= OMP_CLAUSE_TILE_LIST (nc
),
14532 loc
= OMP_CLAUSE_TILE_LIST (oc
);
14534 loc
= TREE_CHAIN (loc
), lnc
= TREE_CHAIN (lnc
))
14536 TREE_VALUE (lnc
) = tsubst_expr (TREE_VALUE (loc
), args
,
14537 complain
, in_decl
, false);
14542 gcc_unreachable ();
14545 switch (OMP_CLAUSE_CODE (nc
))
14547 case OMP_CLAUSE_SHARED
:
14548 case OMP_CLAUSE_PRIVATE
:
14549 case OMP_CLAUSE_FIRSTPRIVATE
:
14550 case OMP_CLAUSE_LASTPRIVATE
:
14551 case OMP_CLAUSE_COPYPRIVATE
:
14552 case OMP_CLAUSE_LINEAR
:
14553 case OMP_CLAUSE_REDUCTION
:
14554 case OMP_CLAUSE_USE_DEVICE_PTR
:
14555 case OMP_CLAUSE_IS_DEVICE_PTR
:
14556 /* tsubst_expr on SCOPE_REF results in returning
14557 finish_non_static_data_member result. Undo that here. */
14558 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
14559 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
14560 == IDENTIFIER_NODE
))
14562 tree t
= OMP_CLAUSE_DECL (nc
);
14565 switch (TREE_CODE (v
))
14567 case COMPONENT_REF
:
14571 case POINTER_PLUS_EXPR
:
14572 v
= TREE_OPERAND (v
, 0);
14575 if (DECL_CONTEXT (v
) == current_function_decl
14576 && DECL_ARTIFICIAL (v
)
14577 && DECL_NAME (v
) == this_identifier
)
14578 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
14585 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
14586 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
14587 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
14588 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
14589 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
14591 tree decl
= OMP_CLAUSE_DECL (nc
);
14594 if (!DECL_LANG_SPECIFIC (decl
))
14595 retrofit_lang_decl (decl
);
14596 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
14605 new_clauses
= nreverse (new_clauses
);
14608 new_clauses
= finish_omp_clauses (new_clauses
, allow_fields
);
14609 if (linear_no_step
)
14610 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
14611 if (nc
== linear_no_step
)
14613 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
14617 return new_clauses
;
14620 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14623 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
14626 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14628 tree purpose
, value
, chain
;
14633 if (TREE_CODE (t
) != TREE_LIST
)
14634 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14635 /*function_p=*/false,
14636 /*integral_constant_expression_p=*/false);
14638 if (t
== void_list_node
)
14641 purpose
= TREE_PURPOSE (t
);
14643 purpose
= RECUR (purpose
);
14644 value
= TREE_VALUE (t
);
14647 if (TREE_CODE (value
) != LABEL_DECL
)
14648 value
= RECUR (value
);
14651 value
= lookup_label (DECL_NAME (value
));
14652 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
14653 TREE_USED (value
) = 1;
14656 chain
= TREE_CHAIN (t
);
14657 if (chain
&& chain
!= void_type_node
)
14658 chain
= RECUR (chain
);
14659 return tree_cons (purpose
, value
, chain
);
14663 /* Used to temporarily communicate the list of #pragma omp parallel
14664 clauses to #pragma omp for instantiation if they are combined
14667 static tree
*omp_parallel_combined_clauses
;
14669 /* Substitute one OMP_FOR iterator. */
14672 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
14673 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
14674 tree args
, tsubst_flags_t complain
, tree in_decl
,
14675 bool integral_constant_expression_p
)
14677 #define RECUR(NODE) \
14678 tsubst_expr ((NODE), args, complain, in_decl, \
14679 integral_constant_expression_p)
14680 tree decl
, init
, cond
, incr
;
14682 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
14683 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
14685 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
14687 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
14688 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
14691 decl
= TREE_OPERAND (init
, 0);
14692 init
= TREE_OPERAND (init
, 1);
14693 tree decl_expr
= NULL_TREE
;
14694 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
14696 /* We need to jump through some hoops to handle declarations in the
14697 for-init-statement, since we might need to handle auto deduction,
14698 but we need to keep control of initialization. */
14700 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
14701 decl
= tsubst_decl (decl
, args
, complain
);
14705 if (TREE_CODE (decl
) == SCOPE_REF
)
14707 decl
= RECUR (decl
);
14708 if (TREE_CODE (decl
) == COMPONENT_REF
)
14712 switch (TREE_CODE (v
))
14714 case COMPONENT_REF
:
14718 case POINTER_PLUS_EXPR
:
14719 v
= TREE_OPERAND (v
, 0);
14722 if (DECL_CONTEXT (v
) == current_function_decl
14723 && DECL_ARTIFICIAL (v
)
14724 && DECL_NAME (v
) == this_identifier
)
14726 decl
= TREE_OPERAND (decl
, 1);
14727 decl
= omp_privatize_field (decl
, false);
14737 decl
= RECUR (decl
);
14739 init
= RECUR (init
);
14741 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
14742 if (auto_node
&& init
)
14744 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
14746 gcc_assert (!type_dependent_expression_p (decl
));
14748 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14752 /* Declare the variable, but don't let that initialize it. */
14753 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
14754 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
14756 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
14759 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
14760 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14761 if (TREE_CODE (incr
) == MODIFY_EXPR
)
14763 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14764 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
14765 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
14766 NOP_EXPR
, rhs
, complain
);
14769 incr
= RECUR (incr
);
14770 TREE_VEC_ELT (declv
, i
) = decl
;
14771 TREE_VEC_ELT (initv
, i
) = init
;
14772 TREE_VEC_ELT (condv
, i
) = cond
;
14773 TREE_VEC_ELT (incrv
, i
) = incr
;
14779 /* Declare and initialize the variable. */
14787 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
14789 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
14791 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
14792 && OMP_CLAUSE_DECL (*pc
) == decl
)
14794 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
14795 && OMP_CLAUSE_DECL (*pc
) == decl
)
14799 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14801 *pc
= OMP_CLAUSE_CHAIN (c
);
14802 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14805 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
14806 && OMP_CLAUSE_DECL (*pc
) == decl
)
14808 error ("iteration variable %qD should not be firstprivate",
14810 *pc
= OMP_CLAUSE_CHAIN (*pc
);
14812 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
14813 && OMP_CLAUSE_DECL (*pc
) == decl
)
14815 error ("iteration variable %qD should not be reduction",
14817 *pc
= OMP_CLAUSE_CHAIN (*pc
);
14820 pc
= &OMP_CLAUSE_CHAIN (*pc
);
14825 if (*pc
== NULL_TREE
)
14827 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
14828 OMP_CLAUSE_DECL (c
) = decl
;
14829 c
= finish_omp_clauses (c
, true);
14832 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14837 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
14838 if (COMPARISON_CLASS_P (cond
))
14840 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
14841 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
14842 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
14845 cond
= RECUR (cond
);
14846 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14847 switch (TREE_CODE (incr
))
14849 case PREINCREMENT_EXPR
:
14850 case PREDECREMENT_EXPR
:
14851 case POSTINCREMENT_EXPR
:
14852 case POSTDECREMENT_EXPR
:
14853 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
14854 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
14857 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14858 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14860 tree rhs
= TREE_OPERAND (incr
, 1);
14861 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14862 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14863 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14864 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14865 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14869 incr
= RECUR (incr
);
14872 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14873 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14875 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14876 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14877 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
14878 TREE_TYPE (decl
), lhs
,
14879 RECUR (TREE_OPERAND (incr
, 2))));
14881 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
14882 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
14883 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
14885 tree rhs
= TREE_OPERAND (incr
, 2);
14886 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14887 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14888 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14889 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14890 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14894 incr
= RECUR (incr
);
14897 incr
= RECUR (incr
);
14901 TREE_VEC_ELT (declv
, i
) = decl
;
14902 TREE_VEC_ELT (initv
, i
) = init
;
14903 TREE_VEC_ELT (condv
, i
) = cond
;
14904 TREE_VEC_ELT (incrv
, i
) = incr
;
14908 /* Helper function of tsubst_expr, find OMP_TEAMS inside
14909 of OMP_TARGET's body. */
14912 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
14914 *walk_subtrees
= 0;
14915 switch (TREE_CODE (*tp
))
14920 case STATEMENT_LIST
:
14921 *walk_subtrees
= 1;
14929 /* Like tsubst_copy for expressions, etc. but also does semantic
14933 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
14934 bool integral_constant_expression_p
)
14936 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14937 #define RECUR(NODE) \
14938 tsubst_expr ((NODE), args, complain, in_decl, \
14939 integral_constant_expression_p)
14945 if (t
== NULL_TREE
|| t
== error_mark_node
)
14948 loc
= input_location
;
14949 if (EXPR_HAS_LOCATION (t
))
14950 input_location
= EXPR_LOCATION (t
);
14951 if (STATEMENT_CODE_P (TREE_CODE (t
)))
14952 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
14954 switch (TREE_CODE (t
))
14956 case STATEMENT_LIST
:
14958 tree_stmt_iterator i
;
14959 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
14960 RECUR (tsi_stmt (i
));
14964 case CTOR_INITIALIZER
:
14965 finish_mem_initializers (tsubst_initializer_list
14966 (TREE_OPERAND (t
, 0), args
));
14970 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
14974 tmp
= RECUR (EXPR_STMT_EXPR (t
));
14975 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
14976 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
14978 finish_expr_stmt (tmp
);
14982 do_using_directive (USING_STMT_NAMESPACE (t
));
14987 tree decl
, pattern_decl
;
14990 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
14991 if (TREE_CODE (decl
) == LABEL_DECL
)
14992 finish_label_decl (DECL_NAME (decl
));
14993 else if (TREE_CODE (decl
) == USING_DECL
)
14995 tree scope
= USING_DECL_SCOPE (decl
);
14996 tree name
= DECL_NAME (decl
);
14999 scope
= tsubst (scope
, args
, complain
, in_decl
);
15000 decl
= lookup_qualified_name (scope
, name
,
15001 /*is_type_p=*/false,
15002 /*complain=*/false);
15003 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15004 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15006 do_local_using_decl (decl
, scope
, name
);
15008 else if (DECL_PACK_P (decl
))
15010 /* Don't build up decls for a variadic capture proxy, we'll
15011 instantiate the elements directly as needed. */
15016 init
= DECL_INITIAL (decl
);
15017 decl
= tsubst (decl
, args
, complain
, in_decl
);
15018 if (decl
!= error_mark_node
)
15020 /* By marking the declaration as instantiated, we avoid
15021 trying to instantiate it. Since instantiate_decl can't
15022 handle local variables, and since we've already done
15023 all that needs to be done, that's the right thing to
15026 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15028 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15029 /* Anonymous aggregates are a special case. */
15030 finish_anon_union (decl
);
15031 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15033 DECL_CONTEXT (decl
) = current_function_decl
;
15034 if (DECL_NAME (decl
) == this_identifier
)
15036 tree lam
= DECL_CONTEXT (current_function_decl
);
15037 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15038 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15040 insert_capture_proxy (decl
);
15042 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15043 /* We already did a pushtag. */;
15044 else if (TREE_CODE (decl
) == FUNCTION_DECL
15045 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15046 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15048 DECL_CONTEXT (decl
) = NULL_TREE
;
15050 DECL_CONTEXT (decl
) = current_function_decl
;
15051 cp_check_omp_declare_reduction (decl
);
15055 int const_init
= false;
15056 maybe_push_decl (decl
);
15058 && DECL_PRETTY_FUNCTION_P (decl
))
15060 /* For __PRETTY_FUNCTION__ we have to adjust the
15062 const char *const name
15063 = cxx_printable_name (current_function_decl
, 2);
15064 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15067 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15070 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15072 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15081 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15082 RECUR (FOR_INIT_STMT (t
));
15083 finish_for_init_stmt (stmt
);
15084 tmp
= RECUR (FOR_COND (t
));
15085 finish_for_cond (tmp
, stmt
, false);
15086 tmp
= RECUR (FOR_EXPR (t
));
15087 finish_for_expr (tmp
, stmt
);
15088 RECUR (FOR_BODY (t
));
15089 finish_for_stmt (stmt
);
15092 case RANGE_FOR_STMT
:
15095 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15096 decl
= RANGE_FOR_DECL (t
);
15097 decl
= tsubst (decl
, args
, complain
, in_decl
);
15098 maybe_push_decl (decl
);
15099 expr
= RECUR (RANGE_FOR_EXPR (t
));
15100 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
15101 RECUR (RANGE_FOR_BODY (t
));
15102 finish_for_stmt (stmt
);
15107 stmt
= begin_while_stmt ();
15108 tmp
= RECUR (WHILE_COND (t
));
15109 finish_while_stmt_cond (tmp
, stmt
, false);
15110 RECUR (WHILE_BODY (t
));
15111 finish_while_stmt (stmt
);
15115 stmt
= begin_do_stmt ();
15116 RECUR (DO_BODY (t
));
15117 finish_do_body (stmt
);
15118 tmp
= RECUR (DO_COND (t
));
15119 finish_do_stmt (tmp
, stmt
, false);
15123 stmt
= begin_if_stmt ();
15124 tmp
= RECUR (IF_COND (t
));
15125 finish_if_stmt_cond (tmp
, stmt
);
15126 RECUR (THEN_CLAUSE (t
));
15127 finish_then_clause (stmt
);
15129 if (ELSE_CLAUSE (t
))
15131 begin_else_clause (stmt
);
15132 RECUR (ELSE_CLAUSE (t
));
15133 finish_else_clause (stmt
);
15136 finish_if_stmt (stmt
);
15140 if (BIND_EXPR_BODY_BLOCK (t
))
15141 stmt
= begin_function_body ();
15143 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
15144 ? BCS_TRY_BLOCK
: 0);
15146 RECUR (BIND_EXPR_BODY (t
));
15148 if (BIND_EXPR_BODY_BLOCK (t
))
15149 finish_function_body (stmt
);
15151 finish_compound_stmt (stmt
);
15155 finish_break_stmt ();
15158 case CONTINUE_STMT
:
15159 finish_continue_stmt ();
15163 stmt
= begin_switch_stmt ();
15164 tmp
= RECUR (SWITCH_STMT_COND (t
));
15165 finish_switch_cond (tmp
, stmt
);
15166 RECUR (SWITCH_STMT_BODY (t
));
15167 finish_switch_stmt (stmt
);
15170 case CASE_LABEL_EXPR
:
15172 tree low
= RECUR (CASE_LOW (t
));
15173 tree high
= RECUR (CASE_HIGH (t
));
15174 finish_case_label (EXPR_LOCATION (t
), low
, high
);
15180 tree decl
= LABEL_EXPR_LABEL (t
);
15183 label
= finish_label_stmt (DECL_NAME (decl
));
15184 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
15185 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
15190 tmp
= GOTO_DESTINATION (t
);
15191 if (TREE_CODE (tmp
) != LABEL_DECL
)
15192 /* Computed goto's must be tsubst'd into. On the other hand,
15193 non-computed gotos must not be; the identifier in question
15194 will have no binding. */
15197 tmp
= DECL_NAME (tmp
);
15198 finish_goto_stmt (tmp
);
15203 tree string
= RECUR (ASM_STRING (t
));
15204 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
15205 complain
, in_decl
);
15206 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
15207 complain
, in_decl
);
15208 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
15209 complain
, in_decl
);
15210 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
15211 complain
, in_decl
);
15212 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
15214 tree asm_expr
= tmp
;
15215 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
15216 asm_expr
= TREE_OPERAND (asm_expr
, 0);
15217 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
15224 stmt
= begin_try_block ();
15225 RECUR (TRY_STMTS (t
));
15226 finish_cleanup_try_block (stmt
);
15227 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
15231 tree compound_stmt
= NULL_TREE
;
15233 if (FN_TRY_BLOCK_P (t
))
15234 stmt
= begin_function_try_block (&compound_stmt
);
15236 stmt
= begin_try_block ();
15238 RECUR (TRY_STMTS (t
));
15240 if (FN_TRY_BLOCK_P (t
))
15241 finish_function_try_block (stmt
);
15243 finish_try_block (stmt
);
15245 RECUR (TRY_HANDLERS (t
));
15246 if (FN_TRY_BLOCK_P (t
))
15247 finish_function_handler_sequence (stmt
, compound_stmt
);
15249 finish_handler_sequence (stmt
);
15255 tree decl
= HANDLER_PARMS (t
);
15259 decl
= tsubst (decl
, args
, complain
, in_decl
);
15260 /* Prevent instantiate_decl from trying to instantiate
15261 this variable. We've already done all that needs to be
15263 if (decl
!= error_mark_node
)
15264 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15266 stmt
= begin_handler ();
15267 finish_handler_parms (decl
, stmt
);
15268 RECUR (HANDLER_BODY (t
));
15269 finish_handler (stmt
);
15274 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
15275 if (CLASS_TYPE_P (tmp
))
15277 /* Local classes are not independent templates; they are
15278 instantiated along with their containing function. And this
15279 way we don't have to deal with pushing out of one local class
15280 to instantiate a member of another local class. */
15282 /* Closures are handled by the LAMBDA_EXPR. */
15283 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
15284 complete_type (tmp
);
15285 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
15286 if (!DECL_ARTIFICIAL (fn
))
15287 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
15291 case STATIC_ASSERT
:
15295 ++c_inhibit_evaluation_warnings
;
15297 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
15300 /*integral_constant_expression_p=*/true);
15301 --c_inhibit_evaluation_warnings
;
15303 finish_static_assert (condition
,
15304 STATIC_ASSERT_MESSAGE (t
),
15305 STATIC_ASSERT_SOURCE_LOCATION (t
),
15306 /*member_p=*/false);
15311 case OACC_PARALLEL
:
15312 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false, false, args
, complain
,
15314 stmt
= begin_omp_parallel ();
15315 RECUR (OMP_BODY (t
));
15316 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
15320 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
15321 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false, true,
15322 args
, complain
, in_decl
);
15323 if (OMP_PARALLEL_COMBINED (t
))
15324 omp_parallel_combined_clauses
= &tmp
;
15325 stmt
= begin_omp_parallel ();
15326 RECUR (OMP_PARALLEL_BODY (t
));
15327 gcc_assert (omp_parallel_combined_clauses
== NULL
);
15328 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
15329 = OMP_PARALLEL_COMBINED (t
);
15330 pop_omp_privatization_clauses (r
);
15334 r
= push_omp_privatization_clauses (false);
15335 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false, true,
15336 args
, complain
, in_decl
);
15337 stmt
= begin_omp_task ();
15338 RECUR (OMP_TASK_BODY (t
));
15339 finish_omp_task (tmp
, stmt
);
15340 pop_omp_privatization_clauses (r
);
15347 case OMP_DISTRIBUTE
:
15351 tree clauses
, body
, pre_body
;
15352 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
15353 tree orig_declv
= NULL_TREE
;
15354 tree incrv
= NULL_TREE
;
15357 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
15358 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
15359 TREE_CODE (t
) != OACC_LOOP
,
15360 args
, complain
, in_decl
);
15361 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15363 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15364 if (OMP_FOR_ORIG_DECLS (t
))
15365 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15366 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15367 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15368 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15371 stmt
= begin_omp_structured_block ();
15373 pre_body
= push_stmt_list ();
15374 RECUR (OMP_FOR_PRE_BODY (t
));
15375 pre_body
= pop_stmt_list (pre_body
);
15377 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15378 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
15379 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
15380 incrv
, &clauses
, args
, complain
, in_decl
,
15381 integral_constant_expression_p
);
15382 omp_parallel_combined_clauses
= NULL
;
15384 body
= push_stmt_list ();
15385 RECUR (OMP_FOR_BODY (t
));
15386 body
= pop_stmt_list (body
);
15388 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15389 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
15390 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
15394 t
= make_node (TREE_CODE (t
));
15395 TREE_TYPE (t
) = void_type_node
;
15396 OMP_FOR_BODY (t
) = body
;
15397 OMP_FOR_PRE_BODY (t
) = pre_body
;
15398 OMP_FOR_CLAUSES (t
) = clauses
;
15399 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
15403 add_stmt (finish_omp_structured_block (stmt
));
15404 pop_omp_privatization_clauses (r
);
15409 omp_parallel_combined_clauses
= NULL
;
15414 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
15415 && OMP_TEAMS_COMBINED (t
));
15416 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false, true,
15417 args
, complain
, in_decl
);
15418 stmt
= push_stmt_list ();
15419 RECUR (OMP_BODY (t
));
15420 stmt
= pop_stmt_list (stmt
);
15423 OMP_BODY (t
) = stmt
;
15424 OMP_CLAUSES (t
) = tmp
;
15426 pop_omp_privatization_clauses (r
);
15430 case OMP_TARGET_DATA
:
15432 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
15433 TREE_CODE (t
) != OACC_DATA
,
15434 args
, complain
, in_decl
);
15435 keep_next_level (true);
15436 stmt
= begin_omp_structured_block ();
15438 RECUR (OMP_BODY (t
));
15439 stmt
= finish_omp_structured_block (stmt
);
15442 OMP_BODY (t
) = stmt
;
15443 OMP_CLAUSES (t
) = tmp
;
15444 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
15446 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
15449 /* For combined target teams, ensure the num_teams and
15450 thread_limit clause expressions are evaluated on the host,
15451 before entering the target construct. */
15453 for (c
= OMP_TEAMS_CLAUSES (teams
);
15454 c
; c
= OMP_CLAUSE_CHAIN (c
))
15455 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
15456 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
15457 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
15459 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
15460 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
15461 if (expr
== error_mark_node
)
15463 tmp
= TARGET_EXPR_SLOT (expr
);
15465 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
15466 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
15467 OMP_CLAUSE_FIRSTPRIVATE
);
15468 OMP_CLAUSE_DECL (tc
) = tmp
;
15469 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
15470 OMP_TARGET_CLAUSES (t
) = tc
;
15479 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), false, false,
15480 args
, complain
, in_decl
);
15481 OACC_DECLARE_CLAUSES (t
) = tmp
;
15485 case OMP_TARGET_UPDATE
:
15486 case OMP_TARGET_ENTER_DATA
:
15487 case OMP_TARGET_EXIT_DATA
:
15488 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), false, true,
15489 args
, complain
, in_decl
);
15491 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15495 case OACC_ENTER_DATA
:
15496 case OACC_EXIT_DATA
:
15498 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), false, false,
15499 args
, complain
, in_decl
);
15501 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15506 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), false, true,
15507 args
, complain
, in_decl
);
15508 stmt
= push_stmt_list ();
15509 RECUR (OMP_BODY (t
));
15510 stmt
= pop_stmt_list (stmt
);
15513 OMP_BODY (t
) = stmt
;
15514 OMP_ORDERED_CLAUSES (t
) = tmp
;
15520 case OMP_TASKGROUP
:
15521 stmt
= push_stmt_list ();
15522 RECUR (OMP_BODY (t
));
15523 stmt
= pop_stmt_list (stmt
);
15526 OMP_BODY (t
) = stmt
;
15531 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
15532 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
15534 tree op1
= TREE_OPERAND (t
, 1);
15535 tree rhs1
= NULL_TREE
;
15537 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
15539 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
15540 op1
= TREE_OPERAND (op1
, 1);
15542 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15543 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15544 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
15545 NULL_TREE
, NULL_TREE
, rhs1
,
15546 OMP_ATOMIC_SEQ_CST (t
));
15550 tree op1
= TREE_OPERAND (t
, 1);
15551 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
15552 tree rhs1
= NULL_TREE
;
15553 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
15554 enum tree_code opcode
= NOP_EXPR
;
15555 if (code
== OMP_ATOMIC_READ
)
15557 v
= RECUR (TREE_OPERAND (op1
, 0));
15558 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15560 else if (code
== OMP_ATOMIC_CAPTURE_OLD
15561 || code
== OMP_ATOMIC_CAPTURE_NEW
)
15563 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
15564 v
= RECUR (TREE_OPERAND (op1
, 0));
15565 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15566 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
15568 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
15569 op11
= TREE_OPERAND (op11
, 1);
15571 lhs
= RECUR (TREE_OPERAND (op11
, 0));
15572 rhs
= RECUR (TREE_OPERAND (op11
, 1));
15573 opcode
= TREE_CODE (op11
);
15574 if (opcode
== MODIFY_EXPR
)
15580 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15581 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15583 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
15584 OMP_ATOMIC_SEQ_CST (t
));
15588 case TRANSACTION_EXPR
:
15591 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
15592 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
15594 if (TRANSACTION_EXPR_IS_STMT (t
))
15596 tree body
= TRANSACTION_EXPR_BODY (t
);
15597 tree noex
= NULL_TREE
;
15598 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
15600 noex
= MUST_NOT_THROW_COND (body
);
15601 if (noex
== NULL_TREE
)
15602 noex
= boolean_true_node
;
15603 body
= TREE_OPERAND (body
, 0);
15605 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
15607 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
15611 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
15612 RECUR (TRANSACTION_EXPR_BODY (t
)),
15619 case MUST_NOT_THROW_EXPR
:
15621 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15622 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
15623 RETURN (build_must_not_throw_expr (op0
, cond
));
15626 case EXPR_PACK_EXPANSION
:
15627 error ("invalid use of pack expansion expression");
15628 RETURN (error_mark_node
);
15630 case NONTYPE_ARGUMENT_PACK
:
15631 error ("use %<...%> to expand argument pack");
15632 RETURN (error_mark_node
);
15634 case CILK_SPAWN_STMT
:
15635 cfun
->calls_cilk_spawn
= 1;
15636 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
15638 case CILK_SYNC_STMT
:
15639 RETURN (build_cilk_sync ());
15641 case COMPOUND_EXPR
:
15642 tmp
= RECUR (TREE_OPERAND (t
, 0));
15643 if (tmp
== NULL_TREE
)
15644 /* If the first operand was a statement, we're done with it. */
15645 RETURN (RECUR (TREE_OPERAND (t
, 1)));
15646 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
15647 RECUR (TREE_OPERAND (t
, 1)),
15650 case ANNOTATE_EXPR
:
15651 tmp
= RECUR (TREE_OPERAND (t
, 0));
15652 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
15653 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
15656 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
15658 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15659 /*function_p=*/false,
15660 integral_constant_expression_p
));
15663 RETURN (NULL_TREE
);
15665 input_location
= loc
;
15671 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15672 function. For description of the body see comment above
15673 cp_parser_omp_declare_reduction_exprs. */
15676 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15678 if (t
== NULL_TREE
|| t
== error_mark_node
)
15681 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
15683 tree_stmt_iterator tsi
;
15686 memset (stmts
, 0, sizeof stmts
);
15687 for (i
= 0, tsi
= tsi_start (t
);
15688 i
< 7 && !tsi_end_p (tsi
);
15689 i
++, tsi_next (&tsi
))
15690 stmts
[i
] = tsi_stmt (tsi
);
15691 gcc_assert (tsi_end_p (tsi
));
15695 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
15696 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
15697 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
15698 args
, complain
, in_decl
);
15699 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
15700 args
, complain
, in_decl
);
15701 DECL_CONTEXT (omp_out
) = current_function_decl
;
15702 DECL_CONTEXT (omp_in
) = current_function_decl
;
15703 keep_next_level (true);
15704 tree block
= begin_omp_structured_block ();
15705 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
15706 block
= finish_omp_structured_block (block
);
15707 block
= maybe_cleanup_point_expr_void (block
);
15708 add_decl_expr (omp_out
);
15709 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
15710 TREE_NO_WARNING (omp_out
) = 1;
15711 add_decl_expr (omp_in
);
15712 finish_expr_stmt (block
);
15716 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
15717 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
15718 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
15719 args
, complain
, in_decl
);
15720 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
15721 args
, complain
, in_decl
);
15722 DECL_CONTEXT (omp_priv
) = current_function_decl
;
15723 DECL_CONTEXT (omp_orig
) = current_function_decl
;
15724 keep_next_level (true);
15725 tree block
= begin_omp_structured_block ();
15726 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
15727 block
= finish_omp_structured_block (block
);
15728 block
= maybe_cleanup_point_expr_void (block
);
15729 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
15730 add_decl_expr (omp_priv
);
15731 add_decl_expr (omp_orig
);
15732 finish_expr_stmt (block
);
15734 add_decl_expr (omp_orig
);
15738 /* T is a postfix-expression that is not being used in a function
15739 call. Return the substituted version of T. */
15742 tsubst_non_call_postfix_expression (tree t
, tree args
,
15743 tsubst_flags_t complain
,
15746 if (TREE_CODE (t
) == SCOPE_REF
)
15747 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
15748 /*done=*/false, /*address_p=*/false);
15750 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15751 /*function_p=*/false,
15752 /*integral_constant_expression_p=*/false);
15757 /* Like tsubst but deals with expressions and performs semantic
15758 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15761 tsubst_copy_and_build (tree t
,
15763 tsubst_flags_t complain
,
15766 bool integral_constant_expression_p
)
15768 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15769 #define RECUR(NODE) \
15770 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15771 /*function_p=*/false, \
15772 integral_constant_expression_p)
15777 if (t
== NULL_TREE
|| t
== error_mark_node
)
15780 loc
= input_location
;
15781 if (EXPR_HAS_LOCATION (t
))
15782 input_location
= EXPR_LOCATION (t
);
15784 /* N3276 decltype magic only applies to calls at the top level or on the
15785 right side of a comma. */
15786 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
15787 complain
&= ~tf_decltype
;
15789 switch (TREE_CODE (t
))
15793 /* Fall through. */
15794 case IDENTIFIER_NODE
:
15798 bool non_integral_constant_expression_p
;
15799 const char *error_msg
;
15801 if (IDENTIFIER_TYPENAME_P (t
))
15803 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15804 t
= mangle_conv_op_name_for_type (new_type
);
15807 /* Look up the name. */
15808 decl
= lookup_name (t
);
15810 /* By convention, expressions use ERROR_MARK_NODE to indicate
15811 failure, not NULL_TREE. */
15812 if (decl
== NULL_TREE
)
15813 decl
= error_mark_node
;
15815 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
15817 integral_constant_expression_p
,
15818 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
15819 &non_integral_constant_expression_p
,
15820 /*template_p=*/false,
15822 /*address_p=*/false,
15823 /*template_arg_p=*/false,
15828 if (!function_p
&& identifier_p (decl
))
15830 if (complain
& tf_error
)
15831 unqualified_name_lookup_error (decl
);
15832 decl
= error_mark_node
;
15837 case TEMPLATE_ID_EXPR
:
15840 tree templ
= RECUR (TREE_OPERAND (t
, 0));
15841 tree targs
= TREE_OPERAND (t
, 1);
15844 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15845 if (targs
== error_mark_node
)
15846 return error_mark_node
;
15848 if (variable_template_p (templ
))
15850 templ
= lookup_template_variable (templ
, targs
);
15851 if (!any_dependent_template_arguments_p (targs
))
15853 templ
= finish_template_variable (templ
, complain
);
15856 RETURN (convert_from_reference (templ
));
15859 if (TREE_CODE (templ
) == COMPONENT_REF
)
15861 object
= TREE_OPERAND (templ
, 0);
15862 templ
= TREE_OPERAND (templ
, 1);
15865 object
= NULL_TREE
;
15866 templ
= lookup_template_function (templ
, targs
);
15869 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
15870 object
, templ
, NULL_TREE
));
15872 RETURN (baselink_for_fns (templ
));
15877 tree r
= RECUR (TREE_OPERAND (t
, 0));
15879 if (REFERENCE_REF_P (t
))
15881 /* A type conversion to reference type will be enclosed in
15882 such an indirect ref, but the substitution of the cast
15883 will have also added such an indirect ref. */
15884 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
15885 r
= convert_from_reference (r
);
15888 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
15889 complain
|decltype_flag
);
15895 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15896 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15897 RETURN (build_nop (type
, op0
));
15900 case IMPLICIT_CONV_EXPR
:
15902 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15903 tree expr
= RECUR (TREE_OPERAND (t
, 0));
15904 int flags
= LOOKUP_IMPLICIT
;
15905 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
15906 flags
= LOOKUP_NORMAL
;
15907 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
15913 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15914 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15915 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
15919 case REINTERPRET_CAST_EXPR
:
15920 case CONST_CAST_EXPR
:
15921 case DYNAMIC_CAST_EXPR
:
15922 case STATIC_CAST_EXPR
:
15925 tree op
, r
= NULL_TREE
;
15927 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15928 if (integral_constant_expression_p
15929 && !cast_valid_in_integral_constant_expression_p (type
))
15931 if (complain
& tf_error
)
15932 error ("a cast to a type other than an integral or "
15933 "enumeration type cannot appear in a constant-expression");
15934 RETURN (error_mark_node
);
15937 op
= RECUR (TREE_OPERAND (t
, 0));
15939 warning_sentinel
s(warn_useless_cast
);
15940 switch (TREE_CODE (t
))
15943 r
= build_functional_cast (type
, op
, complain
);
15945 case REINTERPRET_CAST_EXPR
:
15946 r
= build_reinterpret_cast (type
, op
, complain
);
15948 case CONST_CAST_EXPR
:
15949 r
= build_const_cast (type
, op
, complain
);
15951 case DYNAMIC_CAST_EXPR
:
15952 r
= build_dynamic_cast (type
, op
, complain
);
15954 case STATIC_CAST_EXPR
:
15955 r
= build_static_cast (type
, op
, complain
);
15958 gcc_unreachable ();
15964 case POSTDECREMENT_EXPR
:
15965 case POSTINCREMENT_EXPR
:
15966 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15967 args
, complain
, in_decl
);
15968 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
15969 complain
|decltype_flag
));
15971 case PREDECREMENT_EXPR
:
15972 case PREINCREMENT_EXPR
:
15976 case TRUTH_NOT_EXPR
:
15977 case UNARY_PLUS_EXPR
: /* Unary + */
15978 case REALPART_EXPR
:
15979 case IMAGPART_EXPR
:
15980 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
15981 RECUR (TREE_OPERAND (t
, 0)),
15982 complain
|decltype_flag
));
15984 case FIX_TRUNC_EXPR
:
15985 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
15989 op1
= TREE_OPERAND (t
, 0);
15990 if (TREE_CODE (op1
) == LABEL_DECL
)
15991 RETURN (finish_label_address_expr (DECL_NAME (op1
),
15992 EXPR_LOCATION (op1
)));
15993 if (TREE_CODE (op1
) == SCOPE_REF
)
15994 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
15995 /*done=*/true, /*address_p=*/true);
15997 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
15999 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16000 complain
|decltype_flag
));
16005 case TRUNC_DIV_EXPR
:
16006 case CEIL_DIV_EXPR
:
16007 case FLOOR_DIV_EXPR
:
16008 case ROUND_DIV_EXPR
:
16009 case EXACT_DIV_EXPR
:
16013 case TRUNC_MOD_EXPR
:
16014 case FLOOR_MOD_EXPR
:
16015 case TRUTH_ANDIF_EXPR
:
16016 case TRUTH_ORIF_EXPR
:
16017 case TRUTH_AND_EXPR
:
16018 case TRUTH_OR_EXPR
:
16034 warning_sentinel
s1(warn_type_limits
);
16035 warning_sentinel
s2(warn_div_by_zero
);
16036 warning_sentinel
s3(warn_logical_op
);
16037 warning_sentinel
s4(warn_tautological_compare
);
16038 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16039 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16040 tree r
= build_x_binary_op
16041 (input_location
, TREE_CODE (t
),
16043 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16045 : TREE_CODE (TREE_OPERAND (t
, 0))),
16047 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16049 : TREE_CODE (TREE_OPERAND (t
, 1))),
16051 complain
|decltype_flag
);
16052 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16053 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16058 case POINTER_PLUS_EXPR
:
16060 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16061 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16062 return fold_build_pointer_plus (op0
, op1
);
16066 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16067 /*address_p=*/false));
16069 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16070 args
, complain
, in_decl
);
16071 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16072 RECUR (TREE_OPERAND (t
, 1)),
16073 complain
|decltype_flag
));
16075 case ARRAY_NOTATION_REF
:
16077 tree start_index
, length
, stride
;
16078 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16079 args
, complain
, in_decl
);
16080 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16081 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16082 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16083 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16084 length
, stride
, TREE_TYPE (op1
)));
16087 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
16088 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16095 op1
= TREE_OPERAND (t
, 0);
16096 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16097 op1
= TREE_TYPE (op1
);
16100 /* When there are no ARGS, we are trying to evaluate a
16101 non-dependent expression from the parser. Trying to do
16102 the substitutions may not work. */
16104 op1
= TREE_TYPE (op1
);
16108 ++cp_unevaluated_operand
;
16109 ++c_inhibit_evaluation_warnings
;
16111 op1
= tsubst (op1
, args
, complain
, in_decl
);
16113 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16114 /*function_p=*/false,
16115 /*integral_constant_expression_p=*/
16117 --cp_unevaluated_operand
;
16118 --c_inhibit_evaluation_warnings
;
16121 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16122 complain
& tf_error
);
16124 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
16125 complain
& tf_error
);
16126 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
16128 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
16130 if (!processing_template_decl
&& TYPE_P (op1
))
16132 r
= build_min (SIZEOF_EXPR
, size_type_node
,
16133 build1 (NOP_EXPR
, op1
, error_mark_node
));
16134 SIZEOF_EXPR_TYPE_P (r
) = 1;
16137 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
16138 TREE_SIDE_EFFECTS (r
) = 0;
16139 TREE_READONLY (r
) = 1;
16141 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16146 case AT_ENCODE_EXPR
:
16148 op1
= TREE_OPERAND (t
, 0);
16149 ++cp_unevaluated_operand
;
16150 ++c_inhibit_evaluation_warnings
;
16151 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16152 /*function_p=*/false,
16153 /*integral_constant_expression_p=*/false);
16154 --cp_unevaluated_operand
;
16155 --c_inhibit_evaluation_warnings
;
16156 RETURN (objc_build_encode_expr (op1
));
16159 case NOEXCEPT_EXPR
:
16160 op1
= TREE_OPERAND (t
, 0);
16161 ++cp_unevaluated_operand
;
16162 ++c_inhibit_evaluation_warnings
;
16163 ++cp_noexcept_operand
;
16164 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16165 /*function_p=*/false,
16166 /*integral_constant_expression_p=*/false);
16167 --cp_unevaluated_operand
;
16168 --c_inhibit_evaluation_warnings
;
16169 --cp_noexcept_operand
;
16170 RETURN (finish_noexcept_expr (op1
, complain
));
16174 warning_sentinel
s(warn_div_by_zero
);
16175 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
16176 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
16177 tree r
= build_x_modify_expr
16178 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
16179 complain
|decltype_flag
);
16180 /* TREE_NO_WARNING must be set if either the expression was
16181 parenthesized or it uses an operator such as >>= rather
16182 than plain assignment. In the former case, it was already
16183 set and must be copied. In the latter case,
16184 build_x_modify_expr sets it and it must not be reset
16186 if (TREE_NO_WARNING (t
))
16187 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16193 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16194 args
, complain
, in_decl
);
16195 /* Remember that there was a reference to this entity. */
16197 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
16198 RETURN (error_mark_node
);
16199 RETURN (build_x_arrow (input_location
, op1
, complain
));
16203 tree placement
= RECUR (TREE_OPERAND (t
, 0));
16204 tree init
= RECUR (TREE_OPERAND (t
, 3));
16205 vec
<tree
, va_gc
> *placement_vec
;
16206 vec
<tree
, va_gc
> *init_vec
;
16209 if (placement
== NULL_TREE
)
16210 placement_vec
= NULL
;
16213 placement_vec
= make_tree_vector ();
16214 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
16215 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
16218 /* If there was an initializer in the original tree, but it
16219 instantiated to an empty list, then we should pass a
16220 non-NULL empty vector to tell build_new that it was an
16221 empty initializer() rather than no initializer. This can
16222 only happen when the initializer is a pack expansion whose
16223 parameter packs are of length zero. */
16224 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
16228 init_vec
= make_tree_vector ();
16229 if (init
== void_node
)
16230 gcc_assert (init_vec
!= NULL
);
16233 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
16234 vec_safe_push (init_vec
, TREE_VALUE (init
));
16238 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16239 tree op2
= RECUR (TREE_OPERAND (t
, 2));
16240 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
16241 NEW_EXPR_USE_GLOBAL (t
),
16244 if (placement_vec
!= NULL
)
16245 release_tree_vector (placement_vec
);
16246 if (init_vec
!= NULL
)
16247 release_tree_vector (init_vec
);
16254 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16255 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16256 RETURN (delete_sanity (op0
, op1
,
16257 DELETE_EXPR_USE_VEC (t
),
16258 DELETE_EXPR_USE_GLOBAL (t
),
16262 case COMPOUND_EXPR
:
16264 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
16265 complain
& ~tf_decltype
, in_decl
,
16266 /*function_p=*/false,
16267 integral_constant_expression_p
);
16268 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
16270 RECUR (TREE_OPERAND (t
, 1)),
16271 complain
|decltype_flag
));
16277 vec
<tree
, va_gc
> *call_args
;
16278 unsigned int nargs
, i
;
16283 function
= CALL_EXPR_FN (t
);
16284 /* When we parsed the expression, we determined whether or
16285 not Koenig lookup should be performed. */
16286 koenig_p
= KOENIG_LOOKUP_P (t
);
16287 if (TREE_CODE (function
) == SCOPE_REF
)
16289 qualified_p
= true;
16290 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
16292 /*address_p=*/false);
16294 else if (koenig_p
&& identifier_p (function
))
16296 /* Do nothing; calling tsubst_copy_and_build on an identifier
16297 would incorrectly perform unqualified lookup again.
16299 Note that we can also have an IDENTIFIER_NODE if the earlier
16300 unqualified lookup found a member function; in that case
16301 koenig_p will be false and we do want to do the lookup
16302 again to find the instantiated member function.
16304 FIXME but doing that causes c++/15272, so we need to stop
16305 using IDENTIFIER_NODE in that situation. */
16306 qualified_p
= false;
16310 if (TREE_CODE (function
) == COMPONENT_REF
)
16312 tree op
= TREE_OPERAND (function
, 1);
16314 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
16315 || (BASELINK_P (op
)
16316 && BASELINK_QUALIFIED_P (op
)));
16319 qualified_p
= false;
16321 if (TREE_CODE (function
) == ADDR_EXPR
16322 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
16323 /* Avoid error about taking the address of a constructor. */
16324 function
= TREE_OPERAND (function
, 0);
16326 function
= tsubst_copy_and_build (function
, args
, complain
,
16329 integral_constant_expression_p
);
16331 if (BASELINK_P (function
))
16332 qualified_p
= true;
16335 nargs
= call_expr_nargs (t
);
16336 call_args
= make_tree_vector ();
16337 for (i
= 0; i
< nargs
; ++i
)
16339 tree arg
= CALL_EXPR_ARG (t
, i
);
16341 if (!PACK_EXPANSION_P (arg
))
16342 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
16345 /* Expand the pack expansion and push each entry onto
16347 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
16348 if (TREE_CODE (arg
) == TREE_VEC
)
16350 unsigned int len
, j
;
16352 len
= TREE_VEC_LENGTH (arg
);
16353 for (j
= 0; j
< len
; ++j
)
16355 tree value
= TREE_VEC_ELT (arg
, j
);
16356 if (value
!= NULL_TREE
)
16357 value
= convert_from_reference (value
);
16358 vec_safe_push (call_args
, value
);
16363 /* A partial substitution. Add one entry. */
16364 vec_safe_push (call_args
, arg
);
16369 /* We do not perform argument-dependent lookup if normal
16370 lookup finds a non-function, in accordance with the
16371 expected resolution of DR 218. */
16373 && ((is_overloaded_fn (function
)
16374 /* If lookup found a member function, the Koenig lookup is
16375 not appropriate, even if an unqualified-name was used
16376 to denote the function. */
16377 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
16378 || identifier_p (function
))
16379 /* Only do this when substitution turns a dependent call
16380 into a non-dependent call. */
16381 && type_dependent_expression_p_push (t
)
16382 && !any_type_dependent_arguments_p (call_args
))
16383 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
16385 if (identifier_p (function
)
16386 && !any_type_dependent_arguments_p (call_args
))
16388 if (koenig_p
&& (complain
& tf_warning_or_error
))
16390 /* For backwards compatibility and good diagnostics, try
16391 the unqualified lookup again if we aren't in SFINAE
16393 tree unq
= (tsubst_copy_and_build
16394 (function
, args
, complain
, in_decl
, true,
16395 integral_constant_expression_p
));
16396 if (unq
== error_mark_node
)
16397 RETURN (error_mark_node
);
16399 if (unq
!= function
)
16402 if (INDIRECT_REF_P (fn
))
16403 fn
= TREE_OPERAND (fn
, 0);
16404 if (TREE_CODE (fn
) == COMPONENT_REF
)
16405 fn
= TREE_OPERAND (fn
, 1);
16406 if (is_overloaded_fn (fn
))
16407 fn
= get_first_fn (fn
);
16408 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
16409 "%qD was not declared in this scope, "
16410 "and no declarations were found by "
16411 "argument-dependent lookup at the point "
16412 "of instantiation", function
))
16415 /* Can't say anything more. */;
16416 else if (DECL_CLASS_SCOPE_P (fn
))
16418 location_t loc
= EXPR_LOC_OR_LOC (t
,
16421 "declarations in dependent base %qT are "
16422 "not found by unqualified lookup",
16423 DECL_CLASS_CONTEXT (fn
));
16424 if (current_class_ptr
)
16426 "use %<this->%D%> instead", function
);
16429 "use %<%T::%D%> instead",
16430 current_class_name
, function
);
16433 inform (DECL_SOURCE_LOCATION (fn
),
16434 "%qD declared here, later in the "
16435 "translation unit", fn
);
16440 if (identifier_p (function
))
16442 if (complain
& tf_error
)
16443 unqualified_name_lookup_error (function
);
16444 release_tree_vector (call_args
);
16445 RETURN (error_mark_node
);
16449 /* Remember that there was a reference to this entity. */
16450 if (DECL_P (function
)
16451 && !mark_used (function
, complain
) && !(complain
& tf_error
))
16452 RETURN (error_mark_node
);
16454 /* Put back tf_decltype for the actual call. */
16455 complain
|= decltype_flag
;
16457 if (TREE_CODE (function
) == OFFSET_REF
)
16458 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
16460 else if (TREE_CODE (function
) == COMPONENT_REF
)
16462 tree instance
= TREE_OPERAND (function
, 0);
16463 tree fn
= TREE_OPERAND (function
, 1);
16465 if (processing_template_decl
16466 && (type_dependent_expression_p (instance
)
16467 || (!BASELINK_P (fn
)
16468 && TREE_CODE (fn
) != FIELD_DECL
)
16469 || type_dependent_expression_p (fn
)
16470 || any_type_dependent_arguments_p (call_args
)))
16471 ret
= build_nt_call_vec (function
, call_args
);
16472 else if (!BASELINK_P (fn
))
16473 ret
= finish_call_expr (function
, &call_args
,
16474 /*disallow_virtual=*/false,
16475 /*koenig_p=*/false,
16478 ret
= (build_new_method_call
16480 &call_args
, NULL_TREE
,
16481 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
16486 ret
= finish_call_expr (function
, &call_args
,
16487 /*disallow_virtual=*/qualified_p
,
16491 release_tree_vector (call_args
);
16498 tree cond
= RECUR (TREE_OPERAND (t
, 0));
16499 tree folded_cond
= fold_non_dependent_expr (cond
);
16502 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
16504 if (integer_zerop (folded_cond
))
16506 ++c_inhibit_evaluation_warnings
;
16507 exp1
= RECUR (TREE_OPERAND (t
, 1));
16508 --c_inhibit_evaluation_warnings
;
16509 exp2
= RECUR (TREE_OPERAND (t
, 2));
16513 exp1
= RECUR (TREE_OPERAND (t
, 1));
16514 ++c_inhibit_evaluation_warnings
;
16515 exp2
= RECUR (TREE_OPERAND (t
, 2));
16516 --c_inhibit_evaluation_warnings
;
16518 cond
= folded_cond
;
16522 exp1
= RECUR (TREE_OPERAND (t
, 1));
16523 exp2
= RECUR (TREE_OPERAND (t
, 2));
16526 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
16527 cond
, exp1
, exp2
, complain
));
16530 case PSEUDO_DTOR_EXPR
:
16532 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16533 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16534 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16535 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
16541 tree purpose
, value
, chain
;
16543 if (t
== void_list_node
)
16546 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
16547 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
16549 /* We have pack expansions, so expand those and
16550 create a new list out of it. */
16551 tree purposevec
= NULL_TREE
;
16552 tree valuevec
= NULL_TREE
;
16556 /* Expand the argument expressions. */
16557 if (TREE_PURPOSE (t
))
16558 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
16559 complain
, in_decl
);
16560 if (TREE_VALUE (t
))
16561 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
16562 complain
, in_decl
);
16564 /* Build the rest of the list. */
16565 chain
= TREE_CHAIN (t
);
16566 if (chain
&& chain
!= void_type_node
)
16567 chain
= RECUR (chain
);
16569 /* Determine the number of arguments. */
16570 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
16572 len
= TREE_VEC_LENGTH (purposevec
);
16573 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
16575 else if (TREE_CODE (valuevec
) == TREE_VEC
)
16576 len
= TREE_VEC_LENGTH (valuevec
);
16579 /* Since we only performed a partial substitution into
16580 the argument pack, we only RETURN (a single list
16582 if (purposevec
== TREE_PURPOSE (t
)
16583 && valuevec
== TREE_VALUE (t
)
16584 && chain
== TREE_CHAIN (t
))
16587 RETURN (tree_cons (purposevec
, valuevec
, chain
));
16590 /* Convert the argument vectors into a TREE_LIST */
16594 /* Grab the Ith values. */
16596 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
16599 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
16602 /* Build the list (backwards). */
16603 chain
= tree_cons (purpose
, value
, chain
);
16609 purpose
= TREE_PURPOSE (t
);
16611 purpose
= RECUR (purpose
);
16612 value
= TREE_VALUE (t
);
16614 value
= RECUR (value
);
16615 chain
= TREE_CHAIN (t
);
16616 if (chain
&& chain
!= void_type_node
)
16617 chain
= RECUR (chain
);
16618 if (purpose
== TREE_PURPOSE (t
)
16619 && value
== TREE_VALUE (t
)
16620 && chain
== TREE_CHAIN (t
))
16622 RETURN (tree_cons (purpose
, value
, chain
));
16625 case COMPONENT_REF
:
16632 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16633 args
, complain
, in_decl
);
16634 /* Remember that there was a reference to this entity. */
16635 if (DECL_P (object
)
16636 && !mark_used (object
, complain
) && !(complain
& tf_error
))
16637 RETURN (error_mark_node
);
16638 object_type
= TREE_TYPE (object
);
16640 member
= TREE_OPERAND (t
, 1);
16641 if (BASELINK_P (member
))
16642 member
= tsubst_baselink (member
,
16643 non_reference (TREE_TYPE (object
)),
16644 args
, complain
, in_decl
);
16646 member
= tsubst_copy (member
, args
, complain
, in_decl
);
16647 if (member
== error_mark_node
)
16648 RETURN (error_mark_node
);
16650 if (type_dependent_expression_p (object
))
16651 /* We can't do much here. */;
16652 else if (!CLASS_TYPE_P (object_type
))
16654 if (scalarish_type_p (object_type
))
16656 tree s
= NULL_TREE
;
16657 tree dtor
= member
;
16659 if (TREE_CODE (dtor
) == SCOPE_REF
)
16661 s
= TREE_OPERAND (dtor
, 0);
16662 dtor
= TREE_OPERAND (dtor
, 1);
16664 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
16666 dtor
= TREE_OPERAND (dtor
, 0);
16668 RETURN (finish_pseudo_destructor_expr
16669 (object
, s
, dtor
, input_location
));
16673 else if (TREE_CODE (member
) == SCOPE_REF
16674 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
16676 /* Lookup the template functions now that we know what the
16678 tree scope
= TREE_OPERAND (member
, 0);
16679 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
16680 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
16681 member
= lookup_qualified_name (scope
, tmpl
,
16682 /*is_type_p=*/false,
16683 /*complain=*/false);
16684 if (BASELINK_P (member
))
16686 BASELINK_FUNCTIONS (member
)
16687 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
16689 member
= (adjust_result_of_qualified_name_lookup
16690 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
16695 qualified_name_lookup_error (scope
, tmpl
, member
,
16697 RETURN (error_mark_node
);
16700 else if (TREE_CODE (member
) == SCOPE_REF
16701 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
16702 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
16704 if (complain
& tf_error
)
16706 if (TYPE_P (TREE_OPERAND (member
, 0)))
16707 error ("%qT is not a class or namespace",
16708 TREE_OPERAND (member
, 0));
16710 error ("%qD is not a class or namespace",
16711 TREE_OPERAND (member
, 0));
16713 RETURN (error_mark_node
);
16715 else if (TREE_CODE (member
) == FIELD_DECL
)
16717 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
16718 if (TREE_CODE (r
) == COMPONENT_REF
)
16719 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16723 r
= finish_class_member_access_expr (object
, member
,
16724 /*template_p=*/false,
16726 if (TREE_CODE (r
) == COMPONENT_REF
)
16727 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16732 RETURN (build_throw
16733 (RECUR (TREE_OPERAND (t
, 0))));
16737 vec
<constructor_elt
, va_gc
> *n
;
16738 constructor_elt
*ce
;
16739 unsigned HOST_WIDE_INT idx
;
16740 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16741 bool process_index_p
;
16743 bool need_copy_p
= false;
16746 if (type
== error_mark_node
)
16747 RETURN (error_mark_node
);
16749 /* digest_init will do the wrong thing if we let it. */
16750 if (type
&& TYPE_PTRMEMFUNC_P (type
))
16753 /* We do not want to process the index of aggregate
16754 initializers as they are identifier nodes which will be
16755 looked up by digest_init. */
16756 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
16758 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
16759 newlen
= vec_safe_length (n
);
16760 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
16762 if (ce
->index
&& process_index_p
16763 /* An identifier index is looked up in the type
16764 being initialized, not the current scope. */
16765 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
16766 ce
->index
= RECUR (ce
->index
);
16768 if (PACK_EXPANSION_P (ce
->value
))
16770 /* Substitute into the pack expansion. */
16771 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
16774 if (ce
->value
== error_mark_node
16775 || PACK_EXPANSION_P (ce
->value
))
16777 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
16778 /* Just move the argument into place. */
16779 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
16782 /* Update the length of the final CONSTRUCTOR
16783 arguments vector, and note that we will need to
16785 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
16786 need_copy_p
= true;
16790 ce
->value
= RECUR (ce
->value
);
16795 vec
<constructor_elt
, va_gc
> *old_n
= n
;
16797 vec_alloc (n
, newlen
);
16798 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
16800 if (TREE_CODE (ce
->value
) == TREE_VEC
)
16802 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
16803 for (i
= 0; i
< len
; ++i
)
16804 CONSTRUCTOR_APPEND_ELT (n
, 0,
16805 TREE_VEC_ELT (ce
->value
, i
));
16808 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
16812 r
= build_constructor (init_list_type_node
, n
);
16813 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
16815 if (TREE_HAS_CONSTRUCTOR (t
))
16816 RETURN (finish_compound_literal (type
, r
, complain
));
16818 TREE_TYPE (r
) = type
;
16824 tree operand_0
= TREE_OPERAND (t
, 0);
16825 if (TYPE_P (operand_0
))
16827 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
16828 RETURN (get_typeid (operand_0
, complain
));
16832 operand_0
= RECUR (operand_0
);
16833 RETURN (build_typeid (operand_0
, complain
));
16840 else if (DECL_PACK_P (t
))
16842 /* We don't build decls for an instantiation of a
16843 variadic capture proxy, we instantiate the elements
16845 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
16846 return RECUR (DECL_VALUE_EXPR (t
));
16852 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
16853 /* ??? We're doing a subset of finish_id_expression here. */
16855 && !processing_template_decl
16856 && !cp_unevaluated_operand
16857 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
16858 && CP_DECL_THREAD_LOCAL_P (r
))
16860 if (tree wrap
= get_tls_wrapper_fn (r
))
16861 /* Replace an evaluated use of the thread_local variable with
16862 a call to its wrapper. */
16863 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
16865 else if (outer_automatic_var_p (r
))
16867 r
= process_outer_var_ref (r
, complain
);
16868 if (is_capture_proxy (r
))
16869 register_local_specialization (r
, t
);
16872 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
16873 /* If the original type was a reference, we'll be wrapped in
16874 the appropriate INDIRECT_REF. */
16875 r
= convert_from_reference (r
);
16881 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16882 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16883 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
16886 case OFFSETOF_EXPR
:
16887 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
16888 EXPR_LOCATION (t
)));
16892 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
16893 complain
, in_decl
);
16895 tree type2
= TRAIT_EXPR_TYPE2 (t
);
16896 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
16897 type2
= RECUR (type2
);
16899 type2
= tsubst (type2
, args
, complain
, in_decl
);
16901 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
16906 tree old_stmt_expr
= cur_stmt_expr
;
16907 tree stmt_expr
= begin_stmt_expr ();
16909 cur_stmt_expr
= stmt_expr
;
16910 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
16911 integral_constant_expression_p
);
16912 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
16913 cur_stmt_expr
= old_stmt_expr
;
16915 /* If the resulting list of expression statement is empty,
16916 fold it further into void_node. */
16917 if (empty_expr_stmt_p (stmt_expr
))
16918 stmt_expr
= void_node
;
16920 RETURN (stmt_expr
);
16925 tree r
= build_lambda_expr ();
16927 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
16928 LAMBDA_EXPR_CLOSURE (r
) = type
;
16929 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
16931 LAMBDA_EXPR_LOCATION (r
)
16932 = LAMBDA_EXPR_LOCATION (t
);
16933 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16934 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16935 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16936 LAMBDA_EXPR_DISCRIMINATOR (r
)
16937 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
16938 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
16940 /* No substitution needed. */;
16941 else if (VAR_OR_FUNCTION_DECL_P (scope
))
16942 /* For a function or variable scope, we want to use tsubst so that we
16943 don't complain about referring to an auto before deduction. */
16944 scope
= tsubst (scope
, args
, complain
, in_decl
);
16945 else if (TREE_CODE (scope
) == PARM_DECL
)
16947 /* Look up the parameter we want directly, as tsubst_copy
16948 doesn't do what we need. */
16949 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
16950 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
16951 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
16952 parm
= DECL_CHAIN (parm
);
16954 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16955 if (DECL_CONTEXT (scope
) == NULL_TREE
)
16956 DECL_CONTEXT (scope
) = fn
;
16958 else if (TREE_CODE (scope
) == FIELD_DECL
)
16959 /* For a field, use tsubst_copy so that we look up the existing field
16960 rather than build a new one. */
16961 scope
= RECUR (scope
);
16963 gcc_unreachable ();
16964 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
16965 LAMBDA_EXPR_RETURN_TYPE (r
)
16966 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
16968 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16969 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16971 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16972 determine_visibility (TYPE_NAME (type
));
16973 /* Now that we know visibility, instantiate the type so we have a
16974 declaration of the op() for later calls to lambda_function. */
16975 complete_type (type
);
16977 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
16979 insert_pending_capture_proxies ();
16981 RETURN (build_lambda_object (r
));
16985 /* We can get here for a constant initializer of non-dependent type.
16986 FIXME stop folding in cp_parser_initializer_clause. */
16988 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
16993 case TRANSACTION_EXPR
:
16994 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
16995 integral_constant_expression_p
));
16998 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17000 case VEC_PERM_EXPR
:
17002 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17003 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17004 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17005 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17009 case REQUIRES_EXPR
:
17010 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17013 /* Handle Objective-C++ constructs, if appropriate. */
17016 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17017 in_decl
, /*function_p=*/false);
17021 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17027 input_location
= loc
;
17031 /* Verify that the instantiated ARGS are valid. For type arguments,
17032 make sure that the type's linkage is ok. For non-type arguments,
17033 make sure they are constants if they are integral or enumerations.
17034 Emit an error under control of COMPLAIN, and return TRUE on error. */
17037 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
17039 if (dependent_template_arg_p (t
))
17041 if (ARGUMENT_PACK_P (t
))
17043 tree vec
= ARGUMENT_PACK_ARGS (t
);
17044 int len
= TREE_VEC_LENGTH (vec
);
17045 bool result
= false;
17048 for (i
= 0; i
< len
; ++i
)
17049 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
17053 else if (TYPE_P (t
))
17055 /* [basic.link]: A name with no linkage (notably, the name
17056 of a class or enumeration declared in a local scope)
17057 shall not be used to declare an entity with linkage.
17058 This implies that names with no linkage cannot be used as
17061 DR 757 relaxes this restriction for C++0x. */
17062 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
17063 : no_linkage_check (t
, /*relaxed_p=*/false));
17067 /* DR 488 makes use of a type with no linkage cause
17068 type deduction to fail. */
17069 if (complain
& tf_error
)
17071 if (TYPE_ANONYMOUS_P (nt
))
17072 error ("%qT is/uses anonymous type", t
);
17074 error ("template argument for %qD uses local type %qT",
17079 /* In order to avoid all sorts of complications, we do not
17080 allow variably-modified types as template arguments. */
17081 else if (variably_modified_type_p (t
, NULL_TREE
))
17083 if (complain
& tf_error
)
17084 error ("%qT is a variably modified type", t
);
17088 /* Class template and alias template arguments should be OK. */
17089 else if (DECL_TYPE_TEMPLATE_P (t
))
17091 /* A non-type argument of integral or enumerated type must be a
17093 else if (TREE_TYPE (t
)
17094 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
17095 && !REFERENCE_REF_P (t
)
17096 && !TREE_CONSTANT (t
))
17098 if (complain
& tf_error
)
17099 error ("integral expression %qE is not constant", t
);
17106 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
17108 int ix
, len
= DECL_NTPARMS (tmpl
);
17109 bool result
= false;
17111 for (ix
= 0; ix
!= len
; ix
++)
17113 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
17116 if (result
&& (complain
& tf_error
))
17117 error (" trying to instantiate %qD", tmpl
);
17121 /* We're out of SFINAE context now, so generate diagnostics for the access
17122 errors we saw earlier when instantiating D from TMPL and ARGS. */
17125 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
17127 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17128 tree type
= TREE_TYPE (pattern
);
17129 location_t loc
= input_location
;
17131 push_access_scope (d
);
17132 push_deferring_access_checks (dk_no_deferred
);
17133 input_location
= DECL_SOURCE_LOCATION (pattern
);
17134 tsubst (type
, args
, tf_warning_or_error
, d
);
17135 input_location
= loc
;
17136 pop_deferring_access_checks ();
17137 pop_access_scope (d
);
17140 /* Instantiate the indicated variable, function, or alias template TMPL with
17141 the template arguments in TARG_PTR. */
17144 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17146 tree targ_ptr
= orig_args
;
17150 bool access_ok
= true;
17152 if (tmpl
== error_mark_node
)
17153 return error_mark_node
;
17155 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
17157 /* If this function is a clone, handle it specially. */
17158 if (DECL_CLONED_FUNCTION_P (tmpl
))
17163 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17164 DECL_CLONED_FUNCTION. */
17165 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
17166 targ_ptr
, complain
);
17167 if (spec
== error_mark_node
)
17168 return error_mark_node
;
17170 /* Look for the clone. */
17171 FOR_EACH_CLONE (clone
, spec
)
17172 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
17174 /* We should always have found the clone by now. */
17175 gcc_unreachable ();
17179 if (targ_ptr
== error_mark_node
)
17180 return error_mark_node
;
17182 /* Check to see if we already have this specialization. */
17183 gen_tmpl
= most_general_template (tmpl
);
17184 if (tmpl
!= gen_tmpl
)
17185 /* The TMPL is a partial instantiation. To get a full set of
17186 arguments we must add the arguments used to perform the
17187 partial instantiation. */
17188 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
17191 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17192 but it doesn't seem to be on the hot path. */
17193 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
17195 gcc_assert (tmpl
== gen_tmpl
17196 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
17198 || fndecl
== NULL_TREE
);
17200 if (spec
!= NULL_TREE
)
17202 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
17204 if (complain
& tf_error
)
17205 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
17206 return error_mark_node
;
17211 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
17213 return error_mark_node
;
17215 /* We are building a FUNCTION_DECL, during which the access of its
17216 parameters and return types have to be checked. However this
17217 FUNCTION_DECL which is the desired context for access checking
17218 is not built yet. We solve this chicken-and-egg problem by
17219 deferring all checks until we have the FUNCTION_DECL. */
17220 push_deferring_access_checks (dk_deferred
);
17222 /* Instantiation of the function happens in the context of the function
17223 template, not the context of the overload resolution we're doing. */
17224 push_to_top_level ();
17225 /* If there are dependent arguments, e.g. because we're doing partial
17226 ordering, make sure processing_template_decl stays set. */
17227 if (uses_template_parms (targ_ptr
))
17228 ++processing_template_decl
;
17229 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17231 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
17232 complain
, gen_tmpl
, true);
17233 push_nested_class (ctx
);
17236 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17238 if (VAR_P (pattern
))
17240 /* We need to determine if we're using a partial or explicit
17241 specialization now, because the type of the variable could be
17243 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
17244 tree elt
= most_specialized_partial_spec (tid
, complain
);
17245 if (elt
== error_mark_node
)
17246 pattern
= error_mark_node
;
17249 tmpl
= TREE_VALUE (elt
);
17250 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17251 targ_ptr
= TREE_PURPOSE (elt
);
17255 /* Substitute template parameters to obtain the specialization. */
17256 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
17257 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17258 pop_nested_class ();
17259 pop_from_top_level ();
17261 if (fndecl
== error_mark_node
)
17263 pop_deferring_access_checks ();
17264 return error_mark_node
;
17267 /* The DECL_TI_TEMPLATE should always be the immediate parent
17268 template, not the most general template. */
17269 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
17270 DECL_TI_ARGS (fndecl
) = targ_ptr
;
17272 /* Now we know the specialization, compute access previously
17274 push_access_scope (fndecl
);
17275 if (!perform_deferred_access_checks (complain
))
17277 pop_access_scope (fndecl
);
17278 pop_deferring_access_checks ();
17280 /* If we've just instantiated the main entry point for a function,
17281 instantiate all the alternate entry points as well. We do this
17282 by cloning the instantiation of the main entry point, not by
17283 instantiating the template clones. */
17284 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
17285 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
17289 if (!(complain
& tf_error
))
17291 /* Remember to reinstantiate when we're out of SFINAE so the user
17292 can see the errors. */
17293 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
17295 return error_mark_node
;
17300 /* Wrapper for instantiate_template_1. */
17303 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17306 timevar_push (TV_TEMPLATE_INST
);
17307 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
17308 timevar_pop (TV_TEMPLATE_INST
);
17312 /* Instantiate the alias template TMPL with ARGS. Also push a template
17313 instantiation level, which instantiate_template doesn't do because
17314 functions and variables have sufficient context established by the
17318 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
17320 struct pending_template
*old_last_pend
= last_pending_template
;
17321 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17322 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
17323 return error_mark_node
;
17324 tree tinst
= build_tree_list (tmpl
, args
);
17325 if (!push_tinst_level (tinst
))
17328 return error_mark_node
;
17332 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
17333 args
, tmpl
, complain
,
17334 /*require_all_args=*/true,
17335 /*use_default_args=*/true);
17337 tree r
= instantiate_template (tmpl
, args
, complain
);
17338 pop_tinst_level ();
17339 /* We can't free this if a pending_template entry or last_error_tinst_level
17340 is pointing at it. */
17341 if (last_pending_template
== old_last_pend
17342 && last_error_tinst_level
== old_error_tinst
)
17348 /* PARM is a template parameter pack for FN. Returns true iff
17349 PARM is used in a deducible way in the argument list of FN. */
17352 pack_deducible_p (tree parm
, tree fn
)
17354 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
17355 for (; t
; t
= TREE_CHAIN (t
))
17357 tree type
= TREE_VALUE (t
);
17359 if (!PACK_EXPANSION_P (type
))
17361 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
17362 packs
; packs
= TREE_CHAIN (packs
))
17363 if (template_args_equal (TREE_VALUE (packs
), parm
))
17365 /* The template parameter pack is used in a function parameter
17366 pack. If this is the end of the parameter list, the
17367 template parameter pack is deducible. */
17368 if (TREE_CHAIN (t
) == void_list_node
)
17371 /* Otherwise, not. Well, it could be deduced from
17372 a non-pack parameter, but doing so would end up with
17373 a deduction mismatch, so don't bother. */
17377 /* The template parameter pack isn't used in any function parameter
17378 packs, but it might be used deeper, e.g. tuple<Args...>. */
17382 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17383 NARGS elements of the arguments that are being used when calling
17384 it. TARGS is a vector into which the deduced template arguments
17387 Returns either a FUNCTION_DECL for the matching specialization of FN or
17388 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17389 true, diagnostics will be printed to explain why it failed.
17391 If FN is a conversion operator, or we are trying to produce a specific
17392 specialization, RETURN_TYPE is the return type desired.
17394 The EXPLICIT_TARGS are explicit template arguments provided via a
17397 The parameter STRICT is one of:
17400 We are deducing arguments for a function call, as in
17401 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17402 deducing arguments for a call to the result of a conversion
17403 function template, as in [over.call.object].
17406 We are deducing arguments for a conversion function, as in
17407 [temp.deduct.conv].
17410 We are deducing arguments when doing an explicit instantiation
17411 as in [temp.explicit], when determining an explicit specialization
17412 as in [temp.expl.spec], or when taking the address of a function
17413 template, as in [temp.deduct.funcaddr]. */
17416 fn_type_unification (tree fn
,
17417 tree explicit_targs
,
17420 unsigned int nargs
,
17422 unification_kind_t strict
,
17429 tree decl
= NULL_TREE
;
17430 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
17432 static int deduction_depth
;
17433 struct pending_template
*old_last_pend
= last_pending_template
;
17434 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17435 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
17437 tree r
= error_mark_node
;
17440 complain
|= tf_decltype
;
17442 /* In C++0x, it's possible to have a function template whose type depends
17443 on itself recursively. This is most obvious with decltype, but can also
17444 occur with enumeration scope (c++/48969). So we need to catch infinite
17445 recursion and reject the substitution at deduction time; this function
17446 will return error_mark_node for any repeated substitution.
17448 This also catches excessive recursion such as when f<N> depends on
17449 f<N-1> across all integers, and returns error_mark_node for all the
17450 substitutions back up to the initial one.
17452 This is, of course, not reentrant. */
17453 if (excessive_deduction_depth
)
17454 return error_mark_node
;
17455 tinst
= build_tree_list (fn
, NULL_TREE
);
17458 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
17460 fntype
= TREE_TYPE (fn
);
17461 if (explicit_targs
)
17465 The specified template arguments must match the template
17466 parameters in kind (i.e., type, nontype, template), and there
17467 must not be more arguments than there are parameters;
17468 otherwise type deduction fails.
17470 Nontype arguments must match the types of the corresponding
17471 nontype template parameters, or must be convertible to the
17472 types of the corresponding nontype parameters as specified in
17473 _temp.arg.nontype_, otherwise type deduction fails.
17475 All references in the function type of the function template
17476 to the corresponding template parameters are replaced by the
17477 specified template argument values. If a substitution in a
17478 template parameter or in the function type of the function
17479 template results in an invalid type, type deduction fails. */
17480 int i
, len
= TREE_VEC_LENGTH (tparms
);
17481 location_t loc
= input_location
;
17482 bool incomplete
= false;
17484 /* Adjust any explicit template arguments before entering the
17485 substitution context. */
17487 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
17489 /*require_all_args=*/false,
17490 /*use_default_args=*/false));
17491 if (explicit_targs
== error_mark_node
)
17494 /* Substitute the explicit args into the function type. This is
17495 necessary so that, for instance, explicitly declared function
17496 arguments can match null pointed constants. If we were given
17497 an incomplete set of explicit args, we must not do semantic
17498 processing during substitution as we could create partial
17500 for (i
= 0; i
< len
; i
++)
17502 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
17503 bool parameter_pack
= false;
17504 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
17506 /* Dig out the actual parm. */
17507 if (TREE_CODE (parm
) == TYPE_DECL
17508 || TREE_CODE (parm
) == TEMPLATE_DECL
)
17510 parm
= TREE_TYPE (parm
);
17511 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
17513 else if (TREE_CODE (parm
) == PARM_DECL
)
17515 parm
= DECL_INITIAL (parm
);
17516 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
17519 if (!parameter_pack
&& targ
== NULL_TREE
)
17520 /* No explicit argument for this template parameter. */
17523 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
17525 /* Mark the argument pack as "incomplete". We could
17526 still deduce more arguments during unification.
17527 We remove this mark in type_unification_real. */
17530 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
17531 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
17532 = ARGUMENT_PACK_ARGS (targ
);
17535 /* We have some incomplete argument packs. */
17540 TREE_VALUE (tinst
) = explicit_targs
;
17541 if (!push_tinst_level (tinst
))
17543 excessive_deduction_depth
= true;
17546 processing_template_decl
+= incomplete
;
17547 input_location
= DECL_SOURCE_LOCATION (fn
);
17548 /* Ignore any access checks; we'll see them again in
17549 instantiate_template and they might have the wrong
17550 access path at this point. */
17551 push_deferring_access_checks (dk_deferred
);
17552 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
17553 complain
| tf_partial
, NULL_TREE
);
17554 pop_deferring_access_checks ();
17555 input_location
= loc
;
17556 processing_template_decl
-= incomplete
;
17557 pop_tinst_level ();
17559 if (fntype
== error_mark_node
)
17562 /* Place the explicitly specified arguments in TARGS. */
17563 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
17564 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
17567 /* Never do unification on the 'this' parameter. */
17568 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
17570 if (return_type
&& strict
== DEDUCE_CALL
)
17572 /* We're deducing for a call to the result of a template conversion
17573 function. The parms we really want are in return_type. */
17574 if (POINTER_TYPE_P (return_type
))
17575 return_type
= TREE_TYPE (return_type
);
17576 parms
= TYPE_ARG_TYPES (return_type
);
17578 else if (return_type
)
17582 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
17583 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
17584 new_args
[0] = return_type
;
17585 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
17590 /* We allow incomplete unification without an error message here
17591 because the standard doesn't seem to explicitly prohibit it. Our
17592 callers must be ready to deal with unification failures in any
17595 TREE_VALUE (tinst
) = targs
;
17596 /* If we aren't explaining yet, push tinst context so we can see where
17597 any errors (e.g. from class instantiations triggered by instantiation
17598 of default template arguments) come from. If we are explaining, this
17599 context is redundant. */
17600 if (!explain_p
&& !push_tinst_level (tinst
))
17602 excessive_deduction_depth
= true;
17606 /* type_unification_real will pass back any access checks from default
17607 template argument substitution. */
17608 vec
<deferred_access_check
, va_gc
> *checks
;
17611 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17612 targs
, parms
, args
, nargs
, /*subr=*/0,
17613 strict
, flags
, &checks
, explain_p
);
17615 pop_tinst_level ();
17619 /* Now that we have bindings for all of the template arguments,
17620 ensure that the arguments deduced for the template template
17621 parameters have compatible template parameter lists. We cannot
17622 check this property before we have deduced all template
17623 arguments, because the template parameter types of a template
17624 template parameter might depend on prior template parameters
17625 deduced after the template template parameter. The following
17626 ill-formed example illustrates this issue:
17628 template<typename T, template<T> class C> void f(C<5>, T);
17630 template<int N> struct X {};
17633 f(X<5>(), 5l); // error: template argument deduction fails
17636 The template parameter list of 'C' depends on the template type
17637 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17638 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17639 time that we deduce 'C'. */
17640 if (!template_template_parm_bindings_ok_p
17641 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
17643 unify_inconsistent_template_template_parameters (explain_p
);
17647 /* All is well so far. Now, check:
17651 When all template arguments have been deduced, all uses of
17652 template parameters in nondeduced contexts are replaced with
17653 the corresponding deduced argument values. If the
17654 substitution results in an invalid type, as described above,
17655 type deduction fails. */
17656 TREE_VALUE (tinst
) = targs
;
17657 if (!push_tinst_level (tinst
))
17659 excessive_deduction_depth
= true;
17663 /* Also collect access checks from the instantiation. */
17664 reopen_deferring_access_checks (checks
);
17666 decl
= instantiate_template (fn
, targs
, complain
);
17668 checks
= get_deferred_access_checks ();
17669 pop_deferring_access_checks ();
17671 pop_tinst_level ();
17673 if (decl
== error_mark_node
)
17676 /* Now perform any access checks encountered during substitution. */
17677 push_access_scope (decl
);
17678 ok
= perform_access_checks (checks
, complain
);
17679 pop_access_scope (decl
);
17683 /* If we're looking for an exact match, check that what we got
17684 is indeed an exact match. It might not be if some template
17685 parameters are used in non-deduced contexts. But don't check
17686 for an exact match if we have dependent template arguments;
17687 in that case we're doing partial ordering, and we already know
17688 that we have two candidates that will provide the actual type. */
17689 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
17691 tree substed
= TREE_TYPE (decl
);
17695 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
17697 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
17698 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
17699 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
17701 unify_type_mismatch (explain_p
, args
[i
],
17702 TREE_VALUE (sarg
));
17711 if (excessive_deduction_depth
)
17713 if (deduction_depth
== 0)
17714 /* Reset once we're all the way out. */
17715 excessive_deduction_depth
= false;
17718 /* We can't free this if a pending_template entry or last_error_tinst_level
17719 is pointing at it. */
17720 if (last_pending_template
== old_last_pend
17721 && last_error_tinst_level
== old_error_tinst
)
17727 /* Adjust types before performing type deduction, as described in
17728 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17729 sections are symmetric. PARM is the type of a function parameter
17730 or the return type of the conversion function. ARG is the type of
17731 the argument passed to the call, or the type of the value
17732 initialized with the result of the conversion function.
17733 ARG_EXPR is the original argument expression, which may be null. */
17736 maybe_adjust_types_for_deduction (unification_kind_t strict
,
17749 /* Swap PARM and ARG throughout the remainder of this
17750 function; the handling is precisely symmetric since PARM
17751 will initialize ARG rather than vice versa. */
17752 std::swap (parm
, arg
);
17756 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17757 too, but here handle it by stripping the reference from PARM
17758 rather than by adding it to ARG. */
17759 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17760 && TYPE_REF_IS_RVALUE (*parm
)
17761 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17762 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17763 && TREE_CODE (*arg
) == REFERENCE_TYPE
17764 && !TYPE_REF_IS_RVALUE (*arg
))
17765 *parm
= TREE_TYPE (*parm
);
17766 /* Nothing else to do in this case. */
17770 gcc_unreachable ();
17773 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
17775 /* [temp.deduct.call]
17777 If P is not a reference type:
17779 --If A is an array type, the pointer type produced by the
17780 array-to-pointer standard conversion (_conv.array_) is
17781 used in place of A for type deduction; otherwise,
17783 --If A is a function type, the pointer type produced by
17784 the function-to-pointer standard conversion
17785 (_conv.func_) is used in place of A for type deduction;
17788 --If A is a cv-qualified type, the top level
17789 cv-qualifiers of A's type are ignored for type
17791 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
17792 *arg
= build_pointer_type (TREE_TYPE (*arg
));
17793 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
17794 *arg
= build_pointer_type (*arg
);
17796 *arg
= TYPE_MAIN_VARIANT (*arg
);
17799 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17800 of the form T&&, where T is a template parameter, and the argument
17801 is an lvalue, T is deduced as A& */
17802 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17803 && TYPE_REF_IS_RVALUE (*parm
)
17804 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17805 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17806 && (arg_expr
? real_lvalue_p (arg_expr
)
17807 /* try_one_overload doesn't provide an arg_expr, but
17808 functions are always lvalues. */
17809 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
17810 *arg
= build_reference_type (*arg
);
17812 /* [temp.deduct.call]
17814 If P is a cv-qualified type, the top level cv-qualifiers
17815 of P's type are ignored for type deduction. If P is a
17816 reference type, the type referred to by P is used for
17818 *parm
= TYPE_MAIN_VARIANT (*parm
);
17819 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
17821 *parm
= TREE_TYPE (*parm
);
17822 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17825 /* DR 322. For conversion deduction, remove a reference type on parm
17826 too (which has been swapped into ARG). */
17827 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
17828 *arg
= TREE_TYPE (*arg
);
17833 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17834 template which does contain any deducible template parameters; check if
17835 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17836 unify_one_argument. */
17839 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
17840 int flags
, bool explain_p
)
17845 type
= TREE_TYPE (arg
);
17849 if (same_type_p (parm
, type
))
17850 return unify_success (explain_p
);
17852 if (strict
== DEDUCE_CONV
)
17854 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
17855 explain_p
? tf_warning_or_error
: tf_none
))
17856 return unify_success (explain_p
);
17858 else if (strict
!= DEDUCE_EXACT
)
17860 if (can_convert_arg (parm
, type
,
17861 TYPE_P (arg
) ? NULL_TREE
: arg
,
17862 flags
, explain_p
? tf_warning_or_error
: tf_none
))
17863 return unify_success (explain_p
);
17866 if (strict
== DEDUCE_EXACT
)
17867 return unify_type_mismatch (explain_p
, parm
, arg
);
17869 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
17872 static bool uses_deducible_template_parms (tree type
);
17874 /* Returns true iff the expression EXPR is one from which a template
17875 argument can be deduced. In other words, if it's an undecorated
17876 use of a template non-type parameter. */
17879 deducible_expression (tree expr
)
17881 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
17884 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17885 deducible way; that is, if it has a max value of <PARM> - 1. */
17888 deducible_array_bound (tree domain
)
17890 if (domain
== NULL_TREE
)
17893 tree max
= TYPE_MAX_VALUE (domain
);
17894 if (TREE_CODE (max
) != MINUS_EXPR
)
17897 return deducible_expression (TREE_OPERAND (max
, 0));
17900 /* Returns true iff the template arguments ARGS use a template parameter
17901 in a deducible way. */
17904 deducible_template_args (tree args
)
17906 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
17909 tree elt
= TREE_VEC_ELT (args
, i
);
17910 if (ARGUMENT_PACK_P (elt
))
17911 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
17914 if (PACK_EXPANSION_P (elt
))
17915 elt
= PACK_EXPANSION_PATTERN (elt
);
17916 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
17918 else if (TYPE_P (elt
))
17919 deducible
= uses_deducible_template_parms (elt
);
17921 deducible
= deducible_expression (elt
);
17929 /* Returns true iff TYPE contains any deducible references to template
17930 parameters, as per 14.8.2.5. */
17933 uses_deducible_template_parms (tree type
)
17935 if (PACK_EXPANSION_P (type
))
17936 type
= PACK_EXPANSION_PATTERN (type
);
17943 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17944 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17950 if (POINTER_TYPE_P (type
))
17951 return uses_deducible_template_parms (TREE_TYPE (type
));
17953 /* T[integer-constant ]
17955 if (TREE_CODE (type
) == ARRAY_TYPE
)
17956 return (uses_deducible_template_parms (TREE_TYPE (type
))
17957 || deducible_array_bound (TYPE_DOMAIN (type
)));
17969 if (TYPE_PTRMEM_P (type
))
17970 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
17971 || (uses_deducible_template_parms
17972 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
17974 /* template-name <T> (where template-name refers to a class template)
17975 template-name <i> (where template-name refers to a class template) */
17976 if (CLASS_TYPE_P (type
)
17977 && CLASSTYPE_TEMPLATE_INFO (type
)
17978 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
17979 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17980 (CLASSTYPE_TI_ARGS (type
)));
17985 if (TREE_CODE (type
) == FUNCTION_TYPE
17986 || TREE_CODE (type
) == METHOD_TYPE
)
17988 if (uses_deducible_template_parms (TREE_TYPE (type
)))
17990 tree parm
= TYPE_ARG_TYPES (type
);
17991 if (TREE_CODE (type
) == METHOD_TYPE
)
17992 parm
= TREE_CHAIN (parm
);
17993 for (; parm
; parm
= TREE_CHAIN (parm
))
17994 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
18001 /* Subroutine of type_unification_real and unify_pack_expansion to
18002 handle unification of a single P/A pair. Parameters are as
18003 for those functions. */
18006 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
18007 int subr
, unification_kind_t strict
,
18010 tree arg_expr
= NULL_TREE
;
18013 if (arg
== error_mark_node
|| parm
== error_mark_node
)
18014 return unify_invalid (explain_p
);
18015 if (arg
== unknown_type_node
)
18016 /* We can't deduce anything from this, but we might get all the
18017 template args from other function args. */
18018 return unify_success (explain_p
);
18020 /* Implicit conversions (Clause 4) will be performed on a function
18021 argument to convert it to the type of the corresponding function
18022 parameter if the parameter type contains no template-parameters that
18023 participate in template argument deduction. */
18024 if (strict
!= DEDUCE_EXACT
18025 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
18026 /* For function parameters with no deducible template parameters,
18027 just return. We'll check non-dependent conversions later. */
18028 return unify_success (explain_p
);
18033 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
18034 | UNIFY_ALLOW_MORE_CV_QUAL
18035 | UNIFY_ALLOW_DERIVED
);
18039 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
18043 arg_strict
= UNIFY_ALLOW_NONE
;
18047 gcc_unreachable ();
18050 /* We only do these transformations if this is the top-level
18051 parameter_type_list in a call or declaration matching; in other
18052 situations (nested function declarators, template argument lists) we
18053 won't be comparing a type to an expression, and we don't do any type
18059 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
18060 if (type_unknown_p (arg
))
18062 /* [temp.deduct.type] A template-argument can be
18063 deduced from a pointer to function or pointer
18064 to member function argument if the set of
18065 overloaded functions does not contain function
18066 templates and at most one of a set of
18067 overloaded functions provides a unique
18070 if (resolve_overloaded_unification
18071 (tparms
, targs
, parm
, arg
, strict
,
18072 arg_strict
, explain_p
))
18073 return unify_success (explain_p
);
18074 return unify_overload_resolution_failure (explain_p
, arg
);
18078 arg
= unlowered_expr_type (arg
);
18079 if (arg
== error_mark_node
)
18080 return unify_invalid (explain_p
);
18084 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
18087 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
18088 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
18089 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18091 /* For deduction from an init-list we need the actual list. */
18092 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
18094 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
18097 /* Most parms like fn_type_unification.
18099 If SUBR is 1, we're being called recursively (to unify the
18100 arguments of a function or method parameter of a function
18103 CHECKS is a pointer to a vector of access checks encountered while
18104 substituting default template arguments. */
18107 type_unification_real (tree tparms
,
18111 unsigned int xnargs
,
18113 unification_kind_t strict
,
18115 vec
<deferred_access_check
, va_gc
> **checks
,
18120 int ntparms
= TREE_VEC_LENGTH (tparms
);
18121 int saw_undeduced
= 0;
18124 unsigned int nargs
;
18127 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
18128 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
18129 gcc_assert (ntparms
> 0);
18131 /* Reset the number of non-defaulted template arguments contained
18133 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
18141 while (parms
&& parms
!= void_list_node
18144 parm
= TREE_VALUE (parms
);
18146 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18147 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
18148 /* For a function parameter pack that occurs at the end of the
18149 parameter-declaration-list, the type A of each remaining
18150 argument of the call is compared with the type P of the
18151 declarator-id of the function parameter pack. */
18154 parms
= TREE_CHAIN (parms
);
18156 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18157 /* For a function parameter pack that does not occur at the
18158 end of the parameter-declaration-list, the type of the
18159 parameter pack is a non-deduced context. */
18165 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
18171 && parms
!= void_list_node
18172 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
18174 /* Unify the remaining arguments with the pack expansion type. */
18176 tree parmvec
= make_tree_vec (1);
18178 /* Allocate a TREE_VEC and copy in all of the arguments */
18179 argvec
= make_tree_vec (nargs
- ia
);
18180 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
18181 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
18183 /* Copy the parameter into parmvec. */
18184 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
18185 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
18186 /*subr=*/subr
, explain_p
))
18189 /* Advance to the end of the list of parameters. */
18190 parms
= TREE_CHAIN (parms
);
18193 /* Fail if we've reached the end of the parm list, and more args
18194 are present, and the parm list isn't variadic. */
18195 if (ia
< nargs
&& parms
== void_list_node
)
18196 return unify_too_many_arguments (explain_p
, nargs
, ia
);
18197 /* Fail if parms are left and they don't have default values and
18198 they aren't all deduced as empty packs (c++/57397). This is
18199 consistent with sufficient_parms_p. */
18200 if (parms
&& parms
!= void_list_node
18201 && TREE_PURPOSE (parms
) == NULL_TREE
)
18203 unsigned int count
= nargs
;
18208 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
18211 p
= TREE_CHAIN (p
);
18213 while (p
&& p
!= void_list_node
);
18214 if (count
!= nargs
)
18215 return unify_too_few_arguments (explain_p
, ia
, count
,
18221 tsubst_flags_t complain
= (explain_p
18222 ? tf_warning_or_error
18225 for (i
= 0; i
< ntparms
; i
++)
18227 tree targ
= TREE_VEC_ELT (targs
, i
);
18228 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18230 /* Clear the "incomplete" flags on all argument packs now so that
18231 substituting them into later default arguments works. */
18232 if (targ
&& ARGUMENT_PACK_P (targ
))
18234 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
18235 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
18238 if (targ
|| tparm
== error_mark_node
)
18240 tparm
= TREE_VALUE (tparm
);
18242 /* If this is an undeduced nontype parameter that depends on
18243 a type parameter, try another pass; its type may have been
18244 deduced from a later argument than the one from which
18245 this parameter can be deduced. */
18246 if (TREE_CODE (tparm
) == PARM_DECL
18247 && uses_template_parms (TREE_TYPE (tparm
))
18248 && saw_undeduced
< 2)
18254 /* Core issue #226 (C++0x) [temp.deduct]:
18256 If a template argument has not been deduced, its
18257 default template argument, if any, is used.
18259 When we are in C++98 mode, TREE_PURPOSE will either
18260 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18261 to explicitly check cxx_dialect here. */
18262 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
18263 /* OK, there is a default argument. Wait until after the
18264 conversion check to do substitution. */
18267 /* If the type parameter is a parameter pack, then it will
18268 be deduced to an empty parameter pack. */
18269 if (template_parameter_pack_p (tparm
))
18273 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
18275 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
18276 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
18277 TREE_CONSTANT (arg
) = 1;
18280 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
18282 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
18284 TREE_VEC_ELT (targs
, i
) = arg
;
18288 return unify_parameter_deduction_failure (explain_p
, tparm
);
18291 /* DR 1391: All parameters have args, now check non-dependent parms for
18293 if (saw_undeduced
< 2)
18294 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
18295 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
18297 parm
= TREE_VALUE (parms
);
18299 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18300 && (!TREE_CHAIN (parms
)
18301 || TREE_CHAIN (parms
) == void_list_node
))
18302 /* For a function parameter pack that occurs at the end of the
18303 parameter-declaration-list, the type A of each remaining
18304 argument of the call is compared with the type P of the
18305 declarator-id of the function parameter pack. */
18308 parms
= TREE_CHAIN (parms
);
18310 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18311 /* For a function parameter pack that does not occur at the
18312 end of the parameter-declaration-list, the type of the
18313 parameter pack is a non-deduced context. */
18319 if (uses_template_parms (parm
))
18321 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
18326 /* Now substitute into the default template arguments. */
18327 for (i
= 0; i
< ntparms
; i
++)
18329 tree targ
= TREE_VEC_ELT (targs
, i
);
18330 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18332 if (targ
|| tparm
== error_mark_node
)
18334 tree parm
= TREE_VALUE (tparm
);
18336 if (TREE_CODE (parm
) == PARM_DECL
18337 && uses_template_parms (TREE_TYPE (parm
))
18338 && saw_undeduced
< 2)
18341 tree arg
= TREE_PURPOSE (tparm
);
18342 reopen_deferring_access_checks (*checks
);
18343 location_t save_loc
= input_location
;
18345 input_location
= DECL_SOURCE_LOCATION (parm
);
18346 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
18347 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
18349 input_location
= save_loc
;
18350 *checks
= get_deferred_access_checks ();
18351 pop_deferring_access_checks ();
18352 if (arg
== error_mark_node
)
18356 TREE_VEC_ELT (targs
, i
) = arg
;
18357 /* The position of the first default template argument,
18358 is also the number of non-defaulted arguments in TARGS.
18360 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18361 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
18366 if (saw_undeduced
++ == 1)
18370 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18371 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
18373 return unify_success (explain_p
);
18376 /* Subroutine of type_unification_real. Args are like the variables
18377 at the call site. ARG is an overloaded function (or template-id);
18378 we try deducing template args from each of the overloads, and if
18379 only one succeeds, we go with that. Modifies TARGS and returns
18380 true on success. */
18383 resolve_overloaded_unification (tree tparms
,
18387 unification_kind_t strict
,
18391 tree tempargs
= copy_node (targs
);
18393 tree goodfn
= NULL_TREE
;
18396 if (TREE_CODE (arg
) == ADDR_EXPR
)
18398 arg
= TREE_OPERAND (arg
, 0);
18404 if (TREE_CODE (arg
) == COMPONENT_REF
)
18405 /* Handle `&x' where `x' is some static or non-static member
18407 arg
= TREE_OPERAND (arg
, 1);
18409 if (TREE_CODE (arg
) == OFFSET_REF
)
18410 arg
= TREE_OPERAND (arg
, 1);
18412 /* Strip baselink information. */
18413 if (BASELINK_P (arg
))
18414 arg
= BASELINK_FUNCTIONS (arg
);
18416 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
18418 /* If we got some explicit template args, we need to plug them into
18419 the affected templates before we try to unify, in case the
18420 explicit args will completely resolve the templates in question. */
18423 tree expl_subargs
= TREE_OPERAND (arg
, 1);
18424 arg
= TREE_OPERAND (arg
, 0);
18426 for (; arg
; arg
= OVL_NEXT (arg
))
18428 tree fn
= OVL_CURRENT (arg
);
18429 tree subargs
, elem
;
18431 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18434 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18435 expl_subargs
, NULL_TREE
, tf_none
,
18436 /*require_all_args=*/true,
18437 /*use_default_args=*/true);
18438 if (subargs
!= error_mark_node
18439 && !any_dependent_template_arguments_p (subargs
))
18441 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
18442 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18443 elem
, strict
, sub_strict
, addr_p
, explain_p
)
18444 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
18453 /* If no templates (or more than one) are fully resolved by the
18454 explicit arguments, this template-id is a non-deduced context; it
18455 could still be OK if we deduce all template arguments for the
18456 enclosing call through other arguments. */
18460 else if (TREE_CODE (arg
) != OVERLOAD
18461 && TREE_CODE (arg
) != FUNCTION_DECL
)
18462 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18463 -- but the deduction does not succeed because the expression is
18464 not just the function on its own. */
18467 for (; arg
; arg
= OVL_NEXT (arg
))
18468 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18469 TREE_TYPE (OVL_CURRENT (arg
)),
18470 strict
, sub_strict
, addr_p
, explain_p
)
18471 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
18473 goodfn
= OVL_CURRENT (arg
);
18477 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18478 to function or pointer to member function argument if the set of
18479 overloaded functions does not contain function templates and at most
18480 one of a set of overloaded functions provides a unique match.
18482 So if we found multiple possibilities, we return success but don't
18483 deduce anything. */
18487 int i
= TREE_VEC_LENGTH (targs
);
18489 if (TREE_VEC_ELT (tempargs
, i
))
18491 tree old
= TREE_VEC_ELT (targs
, i
);
18492 tree new_
= TREE_VEC_ELT (tempargs
, i
);
18493 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
18494 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
18495 /* Don't forget explicit template arguments in a pack. */
18496 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
18497 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
18498 TREE_VEC_ELT (targs
, i
) = new_
;
18507 /* Core DR 115: In contexts where deduction is done and fails, or in
18508 contexts where deduction is not done, if a template argument list is
18509 specified and it, along with any default template arguments, identifies
18510 a single function template specialization, then the template-id is an
18511 lvalue for the function template specialization. */
18514 resolve_nondeduced_context (tree orig_expr
)
18516 tree expr
, offset
, baselink
;
18519 if (!type_unknown_p (orig_expr
))
18524 offset
= NULL_TREE
;
18525 baselink
= NULL_TREE
;
18527 if (TREE_CODE (expr
) == ADDR_EXPR
)
18529 expr
= TREE_OPERAND (expr
, 0);
18532 if (TREE_CODE (expr
) == OFFSET_REF
)
18535 expr
= TREE_OPERAND (expr
, 1);
18537 if (BASELINK_P (expr
))
18540 expr
= BASELINK_FUNCTIONS (expr
);
18543 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
18546 tree goodfn
= NULL_TREE
;
18548 /* If we got some explicit template args, we need to plug them into
18549 the affected templates before we try to unify, in case the
18550 explicit args will completely resolve the templates in question. */
18552 tree expl_subargs
= TREE_OPERAND (expr
, 1);
18553 tree arg
= TREE_OPERAND (expr
, 0);
18554 tree badfn
= NULL_TREE
;
18555 tree badargs
= NULL_TREE
;
18557 for (; arg
; arg
= OVL_NEXT (arg
))
18559 tree fn
= OVL_CURRENT (arg
);
18560 tree subargs
, elem
;
18562 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18565 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18566 expl_subargs
, NULL_TREE
, tf_none
,
18567 /*require_all_args=*/true,
18568 /*use_default_args=*/true);
18569 if (subargs
!= error_mark_node
18570 && !any_dependent_template_arguments_p (subargs
))
18572 elem
= instantiate_template (fn
, subargs
, tf_none
);
18573 if (elem
== error_mark_node
)
18578 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
18587 mark_used (goodfn
);
18590 expr
= build_baselink (BASELINK_BINFO (baselink
),
18591 BASELINK_ACCESS_BINFO (baselink
),
18592 expr
, BASELINK_OPTYPE (baselink
));
18596 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
18597 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
18600 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
18603 else if (good
== 0 && badargs
)
18604 /* There were no good options and at least one bad one, so let the
18605 user know what the problem is. */
18606 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
18611 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18612 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18613 different overloads deduce different arguments for a given parm.
18614 ADDR_P is true if the expression for which deduction is being
18615 performed was of the form "& fn" rather than simply "fn".
18617 Returns 1 on success. */
18620 try_one_overload (tree tparms
,
18625 unification_kind_t strict
,
18634 if (arg
== error_mark_node
)
18637 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18638 to function or pointer to member function argument if the set of
18639 overloaded functions does not contain function templates and at most
18640 one of a set of overloaded functions provides a unique match.
18642 So if this is a template, just return success. */
18644 if (uses_template_parms (arg
))
18647 if (TREE_CODE (arg
) == METHOD_TYPE
)
18648 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
18650 arg
= build_pointer_type (arg
);
18652 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
18654 /* We don't copy orig_targs for this because if we have already deduced
18655 some template args from previous args, unify would complain when we
18656 try to deduce a template parameter for the same argument, even though
18657 there isn't really a conflict. */
18658 nargs
= TREE_VEC_LENGTH (targs
);
18659 tempargs
= make_tree_vec (nargs
);
18661 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
18664 /* First make sure we didn't deduce anything that conflicts with
18665 explicitly specified args. */
18666 for (i
= nargs
; i
--; )
18668 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18669 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
18673 else if (uses_template_parms (elt
))
18674 /* Since we're unifying against ourselves, we will fill in
18675 template args used in the function parm list with our own
18676 template parms. Discard them. */
18677 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
18678 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
18682 for (i
= nargs
; i
--; )
18684 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18687 TREE_VEC_ELT (targs
, i
) = elt
;
18693 /* PARM is a template class (perhaps with unbound template
18694 parameters). ARG is a fully instantiated type. If ARG can be
18695 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18696 TARGS are as for unify. */
18699 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
18702 tree copy_of_targs
;
18704 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
18705 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
18706 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
18709 /* We need to make a new template argument vector for the call to
18710 unify. If we used TARGS, we'd clutter it up with the result of
18711 the attempted unification, even if this class didn't work out.
18712 We also don't want to commit ourselves to all the unifications
18713 we've already done, since unification is supposed to be done on
18714 an argument-by-argument basis. In other words, consider the
18715 following pathological case:
18717 template <int I, int J, int K>
18720 template <int I, int J>
18721 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18723 template <int I, int J, int K>
18724 void f(S<I, J, K>, S<I, I, I>);
18733 Now, by the time we consider the unification involving `s2', we
18734 already know that we must have `f<0, 0, 0>'. But, even though
18735 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18736 because there are two ways to unify base classes of S<0, 1, 2>
18737 with S<I, I, I>. If we kept the already deduced knowledge, we
18738 would reject the possibility I=1. */
18739 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18741 /* If unification failed, we're done. */
18742 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
18743 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
18749 /* Given a template type PARM and a class type ARG, find the unique
18750 base type in ARG that is an instance of PARM. We do not examine
18751 ARG itself; only its base-classes. If there is not exactly one
18752 appropriate base class, return NULL_TREE. PARM may be the type of
18753 a partial specialization, as well as a plain template type. Used
18756 static enum template_base_result
18757 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
18758 bool explain_p
, tree
*result
)
18760 tree rval
= NULL_TREE
;
18763 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
18765 binfo
= TYPE_BINFO (complete_type (arg
));
18768 /* The type could not be completed. */
18769 *result
= NULL_TREE
;
18770 return tbr_incomplete_type
;
18773 /* Walk in inheritance graph order. The search order is not
18774 important, and this avoids multiple walks of virtual bases. */
18775 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
18777 tree r
= try_class_unification (tparms
, targs
, parm
,
18778 BINFO_TYPE (binfo
), explain_p
);
18782 /* If there is more than one satisfactory baseclass, then:
18786 If they yield more than one possible deduced A, the type
18790 if (rval
&& !same_type_p (r
, rval
))
18792 *result
= NULL_TREE
;
18793 return tbr_ambiguous_baseclass
;
18801 return tbr_success
;
18804 /* Returns the level of DECL, which declares a template parameter. */
18807 template_decl_level (tree decl
)
18809 switch (TREE_CODE (decl
))
18812 case TEMPLATE_DECL
:
18813 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
18816 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
18819 gcc_unreachable ();
18824 /* Decide whether ARG can be unified with PARM, considering only the
18825 cv-qualifiers of each type, given STRICT as documented for unify.
18826 Returns nonzero iff the unification is OK on that basis. */
18829 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
18831 int arg_quals
= cp_type_quals (arg
);
18832 int parm_quals
= cp_type_quals (parm
);
18834 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18835 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18837 /* Although a CVR qualifier is ignored when being applied to a
18838 substituted template parameter ([8.3.2]/1 for example), that
18839 does not allow us to unify "const T" with "int&" because both
18840 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18841 It is ok when we're allowing additional CV qualifiers
18842 at the outer level [14.8.2.1]/3,1st bullet. */
18843 if ((TREE_CODE (arg
) == REFERENCE_TYPE
18844 || TREE_CODE (arg
) == FUNCTION_TYPE
18845 || TREE_CODE (arg
) == METHOD_TYPE
)
18846 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
18849 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
18850 && (parm_quals
& TYPE_QUAL_RESTRICT
))
18854 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18855 && (arg_quals
& parm_quals
) != parm_quals
)
18858 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
18859 && (parm_quals
& arg_quals
) != arg_quals
)
18865 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18867 template_parm_level_and_index (tree parm
, int* level
, int* index
)
18869 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18870 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18871 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18873 *index
= TEMPLATE_TYPE_IDX (parm
);
18874 *level
= TEMPLATE_TYPE_LEVEL (parm
);
18878 *index
= TEMPLATE_PARM_IDX (parm
);
18879 *level
= TEMPLATE_PARM_LEVEL (parm
);
18883 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18885 if (unify (TP, TA, P, A, S, EP)) \
18889 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18890 expansion at the end of PACKED_PARMS. Returns 0 if the type
18891 deduction succeeds, 1 otherwise. STRICT is the same as in
18892 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18893 call argument list. We'll need to adjust the arguments to make them
18894 types. SUBR tells us if this is from a recursive call to
18895 type_unification_real, or for comparing two template argument
18899 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
18900 tree packed_args
, unification_kind_t strict
,
18901 bool subr
, bool explain_p
)
18904 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
18905 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
18906 tree pack
, packs
= NULL_TREE
;
18907 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
18909 packed_args
= expand_template_argument_pack (packed_args
);
18911 int len
= TREE_VEC_LENGTH (packed_args
);
18913 /* Determine the parameter packs we will be deducing from the
18914 pattern, and record their current deductions. */
18915 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
18916 pack
; pack
= TREE_CHAIN (pack
))
18918 tree parm_pack
= TREE_VALUE (pack
);
18921 /* Determine the index and level of this parameter pack. */
18922 template_parm_level_and_index (parm_pack
, &level
, &idx
);
18924 /* Keep track of the parameter packs and their corresponding
18926 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
18927 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
18930 /* Loop through all of the arguments that have not yet been
18931 unified and unify each with the pattern. */
18932 for (i
= start
; i
< len
; i
++)
18935 bool any_explicit
= false;
18936 tree arg
= TREE_VEC_ELT (packed_args
, i
);
18938 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18939 or the element of its argument pack at the current index if
18940 this argument was explicitly specified. */
18941 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18945 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18948 if (TREE_VALUE (pack
)
18949 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
18950 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
18952 any_explicit
= true;
18953 arg
= TREE_VEC_ELT (pargs
, i
- start
);
18955 TMPL_ARG (targs
, level
, idx
) = arg
;
18958 /* If we had explicit template arguments, substitute them into the
18959 pattern before deduction. */
18962 /* Some arguments might still be unspecified or dependent. */
18964 ++processing_template_decl
;
18965 dependent
= any_dependent_template_arguments_p (targs
);
18967 --processing_template_decl
;
18968 parm
= tsubst (pattern
, targs
,
18969 explain_p
? tf_warning_or_error
: tf_none
,
18972 --processing_template_decl
;
18973 if (parm
== error_mark_node
)
18979 /* Unify the pattern with the current argument. */
18980 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
18984 /* For each parameter pack, collect the deduced value. */
18985 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18988 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18990 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
18991 TMPL_ARG (targs
, level
, idx
);
18995 /* Verify that the results of unification with the parameter packs
18996 produce results consistent with what we've seen before, and make
18997 the deduced argument packs available. */
18998 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19000 tree old_pack
= TREE_VALUE (pack
);
19001 tree new_args
= TREE_TYPE (pack
);
19002 int i
, len
= TREE_VEC_LENGTH (new_args
);
19004 bool nondeduced_p
= false;
19006 /* By default keep the original deduced argument pack.
19007 If necessary, more specific code is going to update the
19008 resulting deduced argument later down in this function. */
19009 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19010 TMPL_ARG (targs
, level
, idx
) = old_pack
;
19012 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19013 actually deduce anything. */
19014 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
19015 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
19016 nondeduced_p
= true;
19020 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
19022 /* If we had fewer function args than explicit template args,
19023 just use the explicits. */
19024 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19025 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
19026 if (len
< explicit_len
)
19027 new_args
= explicit_args
;
19033 /* Build the deduced *_ARGUMENT_PACK. */
19034 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
19036 result
= make_node (NONTYPE_ARGUMENT_PACK
);
19037 TREE_TYPE (result
) =
19038 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
19039 TREE_CONSTANT (result
) = 1;
19042 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19044 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
19046 /* Note the deduced argument packs for this parameter
19048 TMPL_ARG (targs
, level
, idx
) = result
;
19050 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
19051 && (ARGUMENT_PACK_ARGS (old_pack
)
19052 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
19054 /* We only had the explicitly-provided arguments before, but
19055 now we have a complete set of arguments. */
19056 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19058 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
19059 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
19060 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
19064 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
19065 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
19067 if (!comp_template_args (old_args
, new_args
,
19068 &bad_old_arg
, &bad_new_arg
))
19069 /* Inconsistent unification of this parameter pack. */
19070 return unify_parameter_pack_inconsistent (explain_p
,
19076 return unify_success (explain_p
);
19079 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19080 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19081 parameters and return value are as for unify. */
19084 unify_array_domain (tree tparms
, tree targs
,
19085 tree parm_dom
, tree arg_dom
,
19093 /* Our representation of array types uses "N - 1" as the
19094 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19095 not an integer constant. We cannot unify arbitrarily
19096 complex expressions, so we eliminate the MINUS_EXPRs
19098 parm_max
= TYPE_MAX_VALUE (parm_dom
);
19099 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
19102 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
19103 parm_max
= TREE_OPERAND (parm_max
, 0);
19105 arg_max
= TYPE_MAX_VALUE (arg_dom
);
19106 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
19109 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19110 trying to unify the type of a variable with the type
19111 of a template parameter. For example:
19113 template <unsigned int N>
19114 void f (char (&) [N]);
19121 Here, the type of the ARG will be "int [g(i)]", and
19122 may be a SAVE_EXPR, etc. */
19123 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
19124 return unify_vla_arg (explain_p
, arg_dom
);
19125 arg_max
= TREE_OPERAND (arg_max
, 0);
19128 /* If only one of the bounds used a MINUS_EXPR, compensate
19129 by adding one to the other bound. */
19130 if (parm_cst
&& !arg_cst
)
19131 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19135 else if (arg_cst
&& !parm_cst
)
19136 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19141 return unify (tparms
, targs
, parm_max
, arg_max
,
19142 UNIFY_ALLOW_INTEGER
, explain_p
);
19145 /* Deduce the value of template parameters. TPARMS is the (innermost)
19146 set of template parameters to a template. TARGS is the bindings
19147 for those template parameters, as determined thus far; TARGS may
19148 include template arguments for outer levels of template parameters
19149 as well. PARM is a parameter to a template function, or a
19150 subcomponent of that parameter; ARG is the corresponding argument.
19151 This function attempts to match PARM with ARG in a manner
19152 consistent with the existing assignments in TARGS. If more values
19153 are deduced, then TARGS is updated.
19155 Returns 0 if the type deduction succeeds, 1 otherwise. The
19156 parameter STRICT is a bitwise or of the following flags:
19159 Require an exact match between PARM and ARG.
19160 UNIFY_ALLOW_MORE_CV_QUAL:
19161 Allow the deduced ARG to be more cv-qualified (by qualification
19162 conversion) than ARG.
19163 UNIFY_ALLOW_LESS_CV_QUAL:
19164 Allow the deduced ARG to be less cv-qualified than ARG.
19165 UNIFY_ALLOW_DERIVED:
19166 Allow the deduced ARG to be a template base class of ARG,
19167 or a pointer to a template base class of the type pointed to by
19169 UNIFY_ALLOW_INTEGER:
19170 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19171 case for more information.
19172 UNIFY_ALLOW_OUTER_LEVEL:
19173 This is the outermost level of a deduction. Used to determine validity
19174 of qualification conversions. A valid qualification conversion must
19175 have const qualified pointers leading up to the inner type which
19176 requires additional CV quals, except at the outer level, where const
19177 is not required [conv.qual]. It would be normal to set this flag in
19178 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19179 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19180 This is the outermost level of a deduction, and PARM can be more CV
19181 qualified at this point.
19182 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19183 This is the outermost level of a deduction, and PARM can be less CV
19184 qualified at this point. */
19187 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
19193 int strict_in
= strict
;
19195 /* I don't think this will do the right thing with respect to types.
19196 But the only case I've seen it in so far has been array bounds, where
19197 signedness is the only information lost, and I think that will be
19199 while (TREE_CODE (parm
) == NOP_EXPR
)
19200 parm
= TREE_OPERAND (parm
, 0);
19202 if (arg
== error_mark_node
)
19203 return unify_invalid (explain_p
);
19204 if (arg
== unknown_type_node
19205 || arg
== init_list_type_node
)
19206 /* We can't deduce anything from this, but we might get all the
19207 template args from other function args. */
19208 return unify_success (explain_p
);
19210 /* If PARM uses template parameters, then we can't bail out here,
19211 even if ARG == PARM, since we won't record unifications for the
19212 template parameters. We might need them if we're trying to
19213 figure out which of two things is more specialized. */
19214 if (arg
== parm
&& !uses_template_parms (parm
))
19215 return unify_success (explain_p
);
19217 /* Handle init lists early, so the rest of the function can assume
19218 we're dealing with a type. */
19219 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
19223 tree orig_parm
= parm
;
19225 /* Replace T with std::initializer_list<T> for deduction. */
19226 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19227 && flag_deduce_init_list
)
19228 parm
= listify (parm
);
19230 if (!is_std_init_list (parm
)
19231 && TREE_CODE (parm
) != ARRAY_TYPE
)
19232 /* We can only deduce from an initializer list argument if the
19233 parameter is std::initializer_list or an array; otherwise this
19234 is a non-deduced context. */
19235 return unify_success (explain_p
);
19237 if (TREE_CODE (parm
) == ARRAY_TYPE
)
19238 elttype
= TREE_TYPE (parm
);
19241 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
19242 /* Deduction is defined in terms of a single type, so just punt
19243 on the (bizarre) std::initializer_list<T...>. */
19244 if (PACK_EXPANSION_P (elttype
))
19245 return unify_success (explain_p
);
19248 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
19250 int elt_strict
= strict
;
19252 if (elt
== error_mark_node
)
19253 return unify_invalid (explain_p
);
19255 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
19257 tree type
= TREE_TYPE (elt
);
19258 if (type
== error_mark_node
)
19259 return unify_invalid (explain_p
);
19260 /* It should only be possible to get here for a call. */
19261 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
19262 elt_strict
|= maybe_adjust_types_for_deduction
19263 (DEDUCE_CALL
, &elttype
, &type
, elt
);
19267 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
19271 if (TREE_CODE (parm
) == ARRAY_TYPE
19272 && deducible_array_bound (TYPE_DOMAIN (parm
)))
19274 /* Also deduce from the length of the initializer list. */
19275 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
19276 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
19277 if (idx
== error_mark_node
)
19278 return unify_invalid (explain_p
);
19279 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19283 /* If the std::initializer_list<T> deduction worked, replace the
19284 deduced A with std::initializer_list<A>. */
19285 if (orig_parm
!= parm
)
19287 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
19288 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19289 targ
= listify (targ
);
19290 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
19292 return unify_success (explain_p
);
19295 /* Immediately reject some pairs that won't unify because of
19296 cv-qualification mismatches. */
19297 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19299 /* It is the elements of the array which hold the cv quals of an array
19300 type, and the elements might be template type parms. We'll check
19301 when we recurse. */
19302 && TREE_CODE (arg
) != ARRAY_TYPE
19303 /* We check the cv-qualifiers when unifying with template type
19304 parameters below. We want to allow ARG `const T' to unify with
19305 PARM `T' for example, when computing which of two templates
19306 is more specialized, for example. */
19307 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
19308 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
19309 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19311 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
19312 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
19313 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
19314 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
19315 strict
&= ~UNIFY_ALLOW_DERIVED
;
19316 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19317 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
19319 switch (TREE_CODE (parm
))
19321 case TYPENAME_TYPE
:
19323 case UNBOUND_CLASS_TEMPLATE
:
19324 /* In a type which contains a nested-name-specifier, template
19325 argument values cannot be deduced for template parameters used
19326 within the nested-name-specifier. */
19327 return unify_success (explain_p
);
19329 case TEMPLATE_TYPE_PARM
:
19330 case TEMPLATE_TEMPLATE_PARM
:
19331 case BOUND_TEMPLATE_TEMPLATE_PARM
:
19332 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19333 if (error_operand_p (tparm
))
19334 return unify_invalid (explain_p
);
19336 if (TEMPLATE_TYPE_LEVEL (parm
)
19337 != template_decl_level (tparm
))
19338 /* The PARM is not one we're trying to unify. Just check
19339 to see if it matches ARG. */
19341 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19342 && (is_auto (parm
) ? is_auto (arg
)
19343 : same_type_p (parm
, arg
)))
19344 return unify_success (explain_p
);
19346 return unify_type_mismatch (explain_p
, parm
, arg
);
19348 idx
= TEMPLATE_TYPE_IDX (parm
);
19349 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19350 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
19351 if (error_operand_p (tparm
))
19352 return unify_invalid (explain_p
);
19354 /* Check for mixed types and values. */
19355 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19356 && TREE_CODE (tparm
) != TYPE_DECL
)
19357 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19358 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
19359 gcc_unreachable ();
19361 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19363 /* ARG must be constructed from a template class or a template
19364 template parameter. */
19365 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
19366 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
19367 return unify_template_deduction_failure (explain_p
, parm
, arg
);
19369 tree parmvec
= TYPE_TI_ARGS (parm
);
19370 /* An alias template name is never deduced. */
19371 if (TYPE_ALIAS_P (arg
))
19372 arg
= strip_typedefs (arg
);
19373 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
19374 tree full_argvec
= add_to_template_args (targs
, argvec
);
19376 = DECL_INNERMOST_TEMPLATE_PARMS
19377 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
19379 int parm_variadic_p
= 0;
19381 /* The resolution to DR150 makes clear that default
19382 arguments for an N-argument may not be used to bind T
19383 to a template template parameter with fewer than N
19384 parameters. It is not safe to permit the binding of
19385 default arguments as an extension, as that may change
19386 the meaning of a conforming program. Consider:
19388 struct Dense { static const unsigned int dim = 1; };
19390 template <template <typename> class View,
19392 void operator+(float, View<Block> const&);
19394 template <typename Block,
19395 unsigned int Dim = Block::dim>
19396 struct Lvalue_proxy { operator float() const; };
19400 Lvalue_proxy<Dense> p;
19405 Here, if Lvalue_proxy is permitted to bind to View, then
19406 the global operator+ will be used; if they are not, the
19407 Lvalue_proxy will be converted to float. */
19408 if (coerce_template_parms (parm_parms
,
19410 TYPE_TI_TEMPLATE (parm
),
19412 ? tf_warning_or_error
19414 /*require_all_args=*/true,
19415 /*use_default_args=*/false)
19416 == error_mark_node
)
19419 /* Deduce arguments T, i from TT<T> or TT<i>.
19420 We check each element of PARMVEC and ARGVEC individually
19421 rather than the whole TREE_VEC since they can have
19422 different number of elements. */
19424 parmvec
= expand_template_argument_pack (parmvec
);
19425 argvec
= expand_template_argument_pack (argvec
);
19427 len
= TREE_VEC_LENGTH (parmvec
);
19429 /* Check if the parameters end in a pack, making them
19432 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
19433 parm_variadic_p
= 1;
19435 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19436 /* If the template argument list of P contains a pack
19437 expansion that is not the last template argument, the
19438 entire template argument list is a non-deduced
19440 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
19441 return unify_success (explain_p
);
19443 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
19444 return unify_too_few_arguments (explain_p
,
19445 TREE_VEC_LENGTH (argvec
), len
);
19447 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19449 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19450 TREE_VEC_ELT (parmvec
, i
),
19451 TREE_VEC_ELT (argvec
, i
),
19452 UNIFY_ALLOW_NONE
, explain_p
);
19455 if (parm_variadic_p
19456 && unify_pack_expansion (tparms
, targs
,
19459 /*subr=*/true, explain_p
))
19462 arg
= TYPE_TI_TEMPLATE (arg
);
19464 /* Fall through to deduce template name. */
19467 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19468 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19470 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19472 /* Simple cases: Value already set, does match or doesn't. */
19473 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
19474 return unify_success (explain_p
);
19476 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19480 /* If PARM is `const T' and ARG is only `int', we don't have
19481 a match unless we are allowing additional qualification.
19482 If ARG is `const int' and PARM is just `T' that's OK;
19483 that binds `const int' to `T'. */
19484 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
19486 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19488 /* Consider the case where ARG is `const volatile int' and
19489 PARM is `const T'. Then, T should be `volatile int'. */
19490 arg
= cp_build_qualified_type_real
19491 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
19492 if (arg
== error_mark_node
)
19493 return unify_invalid (explain_p
);
19495 /* Simple cases: Value already set, does match or doesn't. */
19496 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
19497 return unify_success (explain_p
);
19499 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19501 /* Make sure that ARG is not a variable-sized array. (Note
19502 that were talking about variable-sized arrays (like
19503 `int[n]'), rather than arrays of unknown size (like
19504 `int[]').) We'll get very confused by such a type since
19505 the bound of the array is not constant, and therefore
19506 not mangleable. Besides, such types are not allowed in
19507 ISO C++, so we can do as we please here. We do allow
19508 them for 'auto' deduction, since that isn't ABI-exposed. */
19509 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
19510 return unify_vla_arg (explain_p
, arg
);
19512 /* Strip typedefs as in convert_template_argument. */
19513 arg
= canonicalize_type_argument (arg
, tf_none
);
19516 /* If ARG is a parameter pack or an expansion, we cannot unify
19517 against it unless PARM is also a parameter pack. */
19518 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19519 && !template_parameter_pack_p (parm
))
19520 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19522 /* If the argument deduction results is a METHOD_TYPE,
19523 then there is a problem.
19524 METHOD_TYPE doesn't map to any real C++ type the result of
19525 the deduction can not be of that type. */
19526 if (TREE_CODE (arg
) == METHOD_TYPE
)
19527 return unify_method_type_error (explain_p
, arg
);
19529 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19530 return unify_success (explain_p
);
19532 case TEMPLATE_PARM_INDEX
:
19533 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19534 if (error_operand_p (tparm
))
19535 return unify_invalid (explain_p
);
19537 if (TEMPLATE_PARM_LEVEL (parm
)
19538 != template_decl_level (tparm
))
19540 /* The PARM is not one we're trying to unify. Just check
19541 to see if it matches ARG. */
19542 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
19543 && cp_tree_equal (parm
, arg
));
19545 unify_expression_unequal (explain_p
, parm
, arg
);
19549 idx
= TEMPLATE_PARM_IDX (parm
);
19550 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19554 int x
= !cp_tree_equal (targ
, arg
);
19556 unify_inconsistency (explain_p
, parm
, targ
, arg
);
19560 /* [temp.deduct.type] If, in the declaration of a function template
19561 with a non-type template-parameter, the non-type
19562 template-parameter is used in an expression in the function
19563 parameter-list and, if the corresponding template-argument is
19564 deduced, the template-argument type shall match the type of the
19565 template-parameter exactly, except that a template-argument
19566 deduced from an array bound may be of any integral type.
19567 The non-type parameter might use already deduced type parameters. */
19568 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
19569 if (!TREE_TYPE (arg
))
19570 /* Template-parameter dependent expression. Just accept it for now.
19571 It will later be processed in convert_template_argument. */
19573 else if (same_type_p (TREE_TYPE (arg
), tparm
))
19575 else if ((strict
& UNIFY_ALLOW_INTEGER
)
19576 && CP_INTEGRAL_TYPE_P (tparm
))
19577 /* Convert the ARG to the type of PARM; the deduced non-type
19578 template argument must exactly match the types of the
19579 corresponding parameter. */
19580 arg
= fold (build_nop (tparm
, arg
));
19581 else if (uses_template_parms (tparm
))
19582 /* We haven't deduced the type of this parameter yet. Try again
19584 return unify_success (explain_p
);
19586 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
19588 /* If ARG is a parameter pack or an expansion, we cannot unify
19589 against it unless PARM is also a parameter pack. */
19590 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19591 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
19592 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19595 bool removed_attr
= false;
19596 arg
= strip_typedefs_expr (arg
, &removed_attr
);
19598 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19599 return unify_success (explain_p
);
19603 /* A pointer-to-member constant can be unified only with
19604 another constant. */
19605 if (TREE_CODE (arg
) != PTRMEM_CST
)
19606 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
19608 /* Just unify the class member. It would be useless (and possibly
19609 wrong, depending on the strict flags) to unify also
19610 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19611 arg refer to the same variable, even if through different
19612 classes. For instance:
19614 struct A { int x; };
19617 Unification of &A::x and &B::x must succeed. */
19618 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
19619 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
19624 if (!TYPE_PTR_P (arg
))
19625 return unify_type_mismatch (explain_p
, parm
, arg
);
19627 /* [temp.deduct.call]
19629 A can be another pointer or pointer to member type that can
19630 be converted to the deduced A via a qualification
19631 conversion (_conv.qual_).
19633 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19634 This will allow for additional cv-qualification of the
19635 pointed-to types if appropriate. */
19637 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
19638 /* The derived-to-base conversion only persists through one
19639 level of pointers. */
19640 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
19642 return unify (tparms
, targs
, TREE_TYPE (parm
),
19643 TREE_TYPE (arg
), strict
, explain_p
);
19646 case REFERENCE_TYPE
:
19647 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
19648 return unify_type_mismatch (explain_p
, parm
, arg
);
19649 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19650 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19653 if (TREE_CODE (arg
) != ARRAY_TYPE
)
19654 return unify_type_mismatch (explain_p
, parm
, arg
);
19655 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
19656 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
19657 return unify_type_mismatch (explain_p
, parm
, arg
);
19658 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19659 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19660 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
19661 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19662 TYPE_DOMAIN (arg
), explain_p
);
19663 return unify_success (explain_p
);
19670 case ENUMERAL_TYPE
:
19673 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19674 return unify_type_mismatch (explain_p
, parm
, arg
);
19676 /* We have already checked cv-qualification at the top of the
19678 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
19679 return unify_type_mismatch (explain_p
, parm
, arg
);
19681 /* As far as unification is concerned, this wins. Later checks
19682 will invalidate it if necessary. */
19683 return unify_success (explain_p
);
19685 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19686 /* Type INTEGER_CST can come from ordinary constant template args. */
19688 while (TREE_CODE (arg
) == NOP_EXPR
)
19689 arg
= TREE_OPERAND (arg
, 0);
19691 if (TREE_CODE (arg
) != INTEGER_CST
)
19692 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19693 return (tree_int_cst_equal (parm
, arg
)
19694 ? unify_success (explain_p
)
19695 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
19699 int i
, len
, argslen
;
19700 int parm_variadic_p
= 0;
19702 if (TREE_CODE (arg
) != TREE_VEC
)
19703 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19705 len
= TREE_VEC_LENGTH (parm
);
19706 argslen
= TREE_VEC_LENGTH (arg
);
19708 /* Check for pack expansions in the parameters. */
19709 for (i
= 0; i
< len
; ++i
)
19711 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
19714 /* We can unify against something with a trailing
19716 parm_variadic_p
= 1;
19718 /* [temp.deduct.type]/9: If the template argument list of
19719 P contains a pack expansion that is not the last
19720 template argument, the entire template argument list
19721 is a non-deduced context. */
19722 return unify_success (explain_p
);
19726 /* If we don't have enough arguments to satisfy the parameters
19727 (not counting the pack expression at the end), or we have
19728 too many arguments for a parameter list that doesn't end in
19729 a pack expression, we can't unify. */
19730 if (parm_variadic_p
19731 ? argslen
< len
- parm_variadic_p
19733 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
19735 /* Unify all of the parameters that precede the (optional)
19736 pack expression. */
19737 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19739 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19740 TREE_VEC_ELT (parm
, i
),
19741 TREE_VEC_ELT (arg
, i
),
19742 UNIFY_ALLOW_NONE
, explain_p
);
19744 if (parm_variadic_p
)
19745 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
19747 /*subr=*/true, explain_p
);
19748 return unify_success (explain_p
);
19753 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19754 return unify_type_mismatch (explain_p
, parm
, arg
);
19756 if (TYPE_PTRMEMFUNC_P (parm
))
19758 if (!TYPE_PTRMEMFUNC_P (arg
))
19759 return unify_type_mismatch (explain_p
, parm
, arg
);
19761 return unify (tparms
, targs
,
19762 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
19763 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
19764 strict
, explain_p
);
19766 else if (TYPE_PTRMEMFUNC_P (arg
))
19767 return unify_type_mismatch (explain_p
, parm
, arg
);
19769 if (CLASSTYPE_TEMPLATE_INFO (parm
))
19771 tree t
= NULL_TREE
;
19773 if (strict_in
& UNIFY_ALLOW_DERIVED
)
19775 /* First, we try to unify the PARM and ARG directly. */
19776 t
= try_class_unification (tparms
, targs
,
19777 parm
, arg
, explain_p
);
19781 /* Fallback to the special case allowed in
19782 [temp.deduct.call]:
19784 If P is a class, and P has the form
19785 template-id, then A can be a derived class of
19786 the deduced A. Likewise, if P is a pointer to
19787 a class of the form template-id, A can be a
19788 pointer to a derived class pointed to by the
19790 enum template_base_result r
;
19791 r
= get_template_base (tparms
, targs
, parm
, arg
,
19796 /* Don't give the derived diagnostic if we're
19797 already dealing with the same template. */
19799 = (CLASSTYPE_TEMPLATE_INFO (arg
)
19800 && (CLASSTYPE_TI_TEMPLATE (parm
)
19801 == CLASSTYPE_TI_TEMPLATE (arg
)));
19802 return unify_no_common_base (explain_p
&& !same_template
,
19807 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
19808 && (CLASSTYPE_TI_TEMPLATE (parm
)
19809 == CLASSTYPE_TI_TEMPLATE (arg
)))
19810 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19811 Then, we should unify `int' and `U'. */
19814 /* There's no chance of unification succeeding. */
19815 return unify_type_mismatch (explain_p
, parm
, arg
);
19817 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
19818 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
19820 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
19821 return unify_type_mismatch (explain_p
, parm
, arg
);
19822 return unify_success (explain_p
);
19825 case FUNCTION_TYPE
:
19827 unsigned int nargs
;
19832 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19833 return unify_type_mismatch (explain_p
, parm
, arg
);
19835 /* CV qualifications for methods can never be deduced, they must
19836 match exactly. We need to check them explicitly here,
19837 because type_unification_real treats them as any other
19838 cv-qualified parameter. */
19839 if (TREE_CODE (parm
) == METHOD_TYPE
19840 && (!check_cv_quals_for_unify
19842 class_of_this_parm (arg
),
19843 class_of_this_parm (parm
))))
19844 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19846 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
19847 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
19849 nargs
= list_length (TYPE_ARG_TYPES (arg
));
19850 args
= XALLOCAVEC (tree
, nargs
);
19851 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
19852 a
!= NULL_TREE
&& a
!= void_list_node
;
19853 a
= TREE_CHAIN (a
), ++i
)
19854 args
[i
] = TREE_VALUE (a
);
19857 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
19858 args
, nargs
, 1, DEDUCE_EXACT
,
19859 LOOKUP_NORMAL
, NULL
, explain_p
);
19863 /* Unify a pointer to member with a pointer to member function, which
19864 deduces the type of the member as a function type. */
19865 if (TYPE_PTRMEMFUNC_P (arg
))
19867 /* Check top-level cv qualifiers */
19868 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
19869 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19871 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19872 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
19873 UNIFY_ALLOW_NONE
, explain_p
);
19875 /* Determine the type of the function we are unifying against. */
19876 tree fntype
= static_fn_type (arg
);
19878 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
19881 if (TREE_CODE (arg
) != OFFSET_TYPE
)
19882 return unify_type_mismatch (explain_p
, parm
, arg
);
19883 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19884 TYPE_OFFSET_BASETYPE (arg
),
19885 UNIFY_ALLOW_NONE
, explain_p
);
19886 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19887 strict
, explain_p
);
19890 if (DECL_TEMPLATE_PARM_P (parm
))
19891 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
19892 if (arg
!= scalar_constant_value (parm
))
19893 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19894 return unify_success (explain_p
);
19897 case TEMPLATE_DECL
:
19898 /* Matched cases are handled by the ARG == PARM test above. */
19899 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19902 /* A non-type template parameter that is a variable should be a
19903 an integral constant, in which case, it whould have been
19904 folded into its (constant) value. So we should not be getting
19905 a variable here. */
19906 gcc_unreachable ();
19908 case TYPE_ARGUMENT_PACK
:
19909 case NONTYPE_ARGUMENT_PACK
:
19910 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
19911 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
19914 case DECLTYPE_TYPE
:
19915 case UNDERLYING_TYPE
:
19916 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19917 or UNDERLYING_TYPE nodes. */
19918 return unify_success (explain_p
);
19921 /* Unification fails if we hit an error node. */
19922 return unify_invalid (explain_p
);
19925 if (REFERENCE_REF_P (parm
))
19927 if (REFERENCE_REF_P (arg
))
19928 arg
= TREE_OPERAND (arg
, 0);
19929 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
19930 strict
, explain_p
);
19935 /* An unresolved overload is a nondeduced context. */
19936 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
19937 return unify_success (explain_p
);
19938 gcc_assert (EXPR_P (parm
));
19940 /* We must be looking at an expression. This can happen with
19944 void foo(S<I>, S<I + 2>);
19946 This is a "nondeduced context":
19950 The nondeduced contexts are:
19952 --A type that is a template-id in which one or more of
19953 the template-arguments is an expression that references
19954 a template-parameter.
19956 In these cases, we assume deduction succeeded, but don't
19957 actually infer any unifications. */
19959 if (!uses_template_parms (parm
)
19960 && !template_args_equal (parm
, arg
))
19961 return unify_expression_unequal (explain_p
, parm
, arg
);
19963 return unify_success (explain_p
);
19966 #undef RECUR_AND_CHECK_FAILURE
19968 /* Note that DECL can be defined in this translation unit, if
19972 mark_definable (tree decl
)
19975 DECL_NOT_REALLY_EXTERN (decl
) = 1;
19976 FOR_EACH_CLONE (clone
, decl
)
19977 DECL_NOT_REALLY_EXTERN (clone
) = 1;
19980 /* Called if RESULT is explicitly instantiated, or is a member of an
19981 explicitly instantiated class. */
19984 mark_decl_instantiated (tree result
, int extern_p
)
19986 SET_DECL_EXPLICIT_INSTANTIATION (result
);
19988 /* If this entity has already been written out, it's too late to
19989 make any modifications. */
19990 if (TREE_ASM_WRITTEN (result
))
19993 /* For anonymous namespace we don't need to do anything. */
19994 if (decl_anon_ns_mem_p (result
))
19996 gcc_assert (!TREE_PUBLIC (result
));
20000 if (TREE_CODE (result
) != FUNCTION_DECL
)
20001 /* The TREE_PUBLIC flag for function declarations will have been
20002 set correctly by tsubst. */
20003 TREE_PUBLIC (result
) = 1;
20005 /* This might have been set by an earlier implicit instantiation. */
20006 DECL_COMDAT (result
) = 0;
20009 DECL_NOT_REALLY_EXTERN (result
) = 0;
20012 mark_definable (result
);
20013 mark_needed (result
);
20014 /* Always make artificials weak. */
20015 if (DECL_ARTIFICIAL (result
) && flag_weak
)
20016 comdat_linkage (result
);
20017 /* For WIN32 we also want to put explicit instantiations in
20018 linkonce sections. */
20019 else if (TREE_PUBLIC (result
))
20020 maybe_make_one_only (result
);
20023 /* If EXTERN_P, then this function will not be emitted -- unless
20024 followed by an explicit instantiation, at which point its linkage
20025 will be adjusted. If !EXTERN_P, then this function will be
20026 emitted here. In neither circumstance do we want
20027 import_export_decl to adjust the linkage. */
20028 DECL_INTERFACE_KNOWN (result
) = 1;
20031 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20032 important template arguments. If any are missing, we check whether
20033 they're important by using error_mark_node for substituting into any
20034 args that were used for partial ordering (the ones between ARGS and END)
20035 and seeing if it bubbles up. */
20038 check_undeduced_parms (tree targs
, tree args
, tree end
)
20040 bool found
= false;
20042 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
20043 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
20046 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
20050 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
20051 if (substed
== error_mark_node
)
20057 /* Given two function templates PAT1 and PAT2, return:
20059 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20060 -1 if PAT2 is more specialized than PAT1.
20061 0 if neither is more specialized.
20063 LEN indicates the number of parameters we should consider
20064 (defaulted parameters should not be considered).
20066 The 1998 std underspecified function template partial ordering, and
20067 DR214 addresses the issue. We take pairs of arguments, one from
20068 each of the templates, and deduce them against each other. One of
20069 the templates will be more specialized if all the *other*
20070 template's arguments deduce against its arguments and at least one
20071 of its arguments *does* *not* deduce against the other template's
20072 corresponding argument. Deduction is done as for class templates.
20073 The arguments used in deduction have reference and top level cv
20074 qualifiers removed. Iff both arguments were originally reference
20075 types *and* deduction succeeds in both directions, an lvalue reference
20076 wins against an rvalue reference and otherwise the template
20077 with the more cv-qualified argument wins for that pairing (if
20078 neither is more cv-qualified, they both are equal). Unlike regular
20079 deduction, after all the arguments have been deduced in this way,
20080 we do *not* verify the deduced template argument values can be
20081 substituted into non-deduced contexts.
20083 The logic can be a bit confusing here, because we look at deduce1 and
20084 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20085 can find template arguments for pat1 to make arg1 look like arg2, that
20086 means that arg2 is at least as specialized as arg1. */
20089 more_specialized_fn (tree pat1
, tree pat2
, int len
)
20091 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
20092 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
20093 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
20094 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
20095 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
20096 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
20097 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
20098 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
20099 tree origs1
, origs2
;
20100 bool lose1
= false;
20101 bool lose2
= false;
20103 /* Remove the this parameter from non-static member functions. If
20104 one is a non-static member function and the other is not a static
20105 member function, remove the first parameter from that function
20106 also. This situation occurs for operator functions where we
20107 locate both a member function (with this pointer) and non-member
20108 operator (with explicit first operand). */
20109 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
20111 len
--; /* LEN is the number of significant arguments for DECL1 */
20112 args1
= TREE_CHAIN (args1
);
20113 if (!DECL_STATIC_FUNCTION_P (decl2
))
20114 args2
= TREE_CHAIN (args2
);
20116 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
20118 args2
= TREE_CHAIN (args2
);
20119 if (!DECL_STATIC_FUNCTION_P (decl1
))
20122 args1
= TREE_CHAIN (args1
);
20126 /* If only one is a conversion operator, they are unordered. */
20127 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
20130 /* Consider the return type for a conversion function */
20131 if (DECL_CONV_FN_P (decl1
))
20133 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
20134 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
20138 processing_template_decl
++;
20144 /* Stop when an ellipsis is seen. */
20145 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
20147 tree arg1
= TREE_VALUE (args1
);
20148 tree arg2
= TREE_VALUE (args2
);
20149 int deduce1
, deduce2
;
20155 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20156 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20158 /* When both arguments are pack expansions, we need only
20159 unify the patterns themselves. */
20160 arg1
= PACK_EXPANSION_PATTERN (arg1
);
20161 arg2
= PACK_EXPANSION_PATTERN (arg2
);
20163 /* This is the last comparison we need to do. */
20167 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
20169 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
20170 arg1
= TREE_TYPE (arg1
);
20171 quals1
= cp_type_quals (arg1
);
20174 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
20176 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
20177 arg2
= TREE_TYPE (arg2
);
20178 quals2
= cp_type_quals (arg2
);
20181 arg1
= TYPE_MAIN_VARIANT (arg1
);
20182 arg2
= TYPE_MAIN_VARIANT (arg2
);
20184 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
20186 int i
, len2
= list_length (args2
);
20187 tree parmvec
= make_tree_vec (1);
20188 tree argvec
= make_tree_vec (len2
);
20191 /* Setup the parameter vector, which contains only ARG1. */
20192 TREE_VEC_ELT (parmvec
, 0) = arg1
;
20194 /* Setup the argument vector, which contains the remaining
20196 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
20197 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20199 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
20200 argvec
, DEDUCE_EXACT
,
20201 /*subr=*/true, /*explain_p=*/false)
20204 /* We cannot deduce in the other direction, because ARG1 is
20205 a pack expansion but ARG2 is not. */
20208 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20210 int i
, len1
= list_length (args1
);
20211 tree parmvec
= make_tree_vec (1);
20212 tree argvec
= make_tree_vec (len1
);
20215 /* Setup the parameter vector, which contains only ARG1. */
20216 TREE_VEC_ELT (parmvec
, 0) = arg2
;
20218 /* Setup the argument vector, which contains the remaining
20220 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
20221 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20223 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
20224 argvec
, DEDUCE_EXACT
,
20225 /*subr=*/true, /*explain_p=*/false)
20228 /* We cannot deduce in the other direction, because ARG2 is
20229 a pack expansion but ARG1 is not.*/
20235 /* The normal case, where neither argument is a pack
20237 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
20238 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20240 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
20241 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20245 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20246 arg2, then arg2 is not as specialized as arg1. */
20252 /* "If, for a given type, deduction succeeds in both directions
20253 (i.e., the types are identical after the transformations above)
20254 and both P and A were reference types (before being replaced with
20255 the type referred to above):
20256 - if the type from the argument template was an lvalue reference and
20257 the type from the parameter template was not, the argument type is
20258 considered to be more specialized than the other; otherwise,
20259 - if the type from the argument template is more cv-qualified
20260 than the type from the parameter template (as described above),
20261 the argument type is considered to be more specialized than the other;
20263 - neither type is more specialized than the other." */
20265 if (deduce1
&& deduce2
)
20267 if (ref1
&& ref2
&& ref1
!= ref2
)
20274 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
20276 if ((quals1
& quals2
) == quals2
)
20278 if ((quals1
& quals2
) == quals1
)
20283 if (lose1
&& lose2
)
20284 /* We've failed to deduce something in either direction.
20285 These must be unordered. */
20288 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20289 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20290 /* We have already processed all of the arguments in our
20291 handing of the pack expansion type. */
20294 args1
= TREE_CHAIN (args1
);
20295 args2
= TREE_CHAIN (args2
);
20298 /* "In most cases, all template parameters must have values in order for
20299 deduction to succeed, but for partial ordering purposes a template
20300 parameter may remain without a value provided it is not used in the
20301 types being used for partial ordering."
20303 Thus, if we are missing any of the targs1 we need to substitute into
20304 origs1, then pat2 is not as specialized as pat1. This can happen when
20305 there is a nondeduced context. */
20306 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
20308 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
20311 processing_template_decl
--;
20313 /* If both deductions succeed, the partial ordering selects the more
20314 constrained template. */
20315 if (!lose1
&& !lose2
)
20317 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
20318 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
20319 lose1
= !subsumes_constraints (c1
, c2
);
20320 lose2
= !subsumes_constraints (c2
, c1
);
20323 /* All things being equal, if the next argument is a pack expansion
20324 for one function but not for the other, prefer the
20325 non-variadic function. FIXME this is bogus; see c++/41958. */
20327 && args1
&& TREE_VALUE (args1
)
20328 && args2
&& TREE_VALUE (args2
))
20330 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
20331 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
20334 if (lose1
== lose2
)
20342 /* Determine which of two partial specializations of TMPL is more
20345 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20346 to the first partial specialization. The TREE_PURPOSE is the
20347 innermost set of template parameters for the partial
20348 specialization. PAT2 is similar, but for the second template.
20350 Return 1 if the first partial specialization is more specialized;
20351 -1 if the second is more specialized; 0 if neither is more
20354 See [temp.class.order] for information about determining which of
20355 two templates is more specialized. */
20358 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
20362 bool any_deductions
= false;
20364 tree tmpl1
= TREE_VALUE (pat1
);
20365 tree tmpl2
= TREE_VALUE (pat2
);
20366 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
20367 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
20368 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
20369 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
20371 /* Just like what happens for functions, if we are ordering between
20372 different template specializations, we may encounter dependent
20373 types in the arguments, and we need our dependency check functions
20374 to behave correctly. */
20375 ++processing_template_decl
;
20376 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
20380 any_deductions
= true;
20383 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
20387 any_deductions
= true;
20389 --processing_template_decl
;
20391 /* If both deductions succeed, the partial ordering selects the more
20392 constrained template. */
20393 if (!winner
&& any_deductions
)
20394 return more_constrained (tmpl1
, tmpl2
);
20396 /* In the case of a tie where at least one of the templates
20397 has a parameter pack at the end, the template with the most
20398 non-packed parameters wins. */
20401 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
20402 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
20404 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
20405 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
20406 int len1
= TREE_VEC_LENGTH (args1
);
20407 int len2
= TREE_VEC_LENGTH (args2
);
20409 /* We don't count the pack expansion at the end. */
20410 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
20412 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
20417 else if (len1
< len2
)
20424 /* Return the template arguments that will produce the function signature
20425 DECL from the function template FN, with the explicit template
20426 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20427 also match. Return NULL_TREE if no satisfactory arguments could be
20431 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
20433 int ntparms
= DECL_NTPARMS (fn
);
20434 tree targs
= make_tree_vec (ntparms
);
20435 tree decl_type
= TREE_TYPE (decl
);
20436 tree decl_arg_types
;
20438 unsigned int nargs
, ix
;
20441 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
20443 /* Never do unification on the 'this' parameter. */
20444 decl_arg_types
= skip_artificial_parms_for (decl
,
20445 TYPE_ARG_TYPES (decl_type
));
20447 nargs
= list_length (decl_arg_types
);
20448 args
= XALLOCAVEC (tree
, nargs
);
20449 for (arg
= decl_arg_types
, ix
= 0;
20450 arg
!= NULL_TREE
&& arg
!= void_list_node
;
20451 arg
= TREE_CHAIN (arg
), ++ix
)
20452 args
[ix
] = TREE_VALUE (arg
);
20454 if (fn_type_unification (fn
, explicit_args
, targs
,
20456 (check_rettype
|| DECL_CONV_FN_P (fn
)
20457 ? TREE_TYPE (decl_type
) : NULL_TREE
),
20458 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
20460 == error_mark_node
)
20466 /* Return the innermost template arguments that, when applied to a partial
20467 specialization of TMPL whose innermost template parameters are
20468 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20471 For example, suppose we have:
20473 template <class T, class U> struct S {};
20474 template <class T> struct S<T*, int> {};
20476 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20477 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20478 int}. The resulting vector will be {double}, indicating that `T'
20479 is bound to `double'. */
20482 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
20484 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
20486 tree innermost_deduced_args
;
20488 innermost_deduced_args
= make_tree_vec (ntparms
);
20489 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20491 deduced_args
= copy_node (args
);
20492 SET_TMPL_ARGS_LEVEL (deduced_args
,
20493 TMPL_ARGS_DEPTH (deduced_args
),
20494 innermost_deduced_args
);
20497 deduced_args
= innermost_deduced_args
;
20499 if (unify (tparms
, deduced_args
,
20500 INNERMOST_TEMPLATE_ARGS (spec_args
),
20501 INNERMOST_TEMPLATE_ARGS (args
),
20502 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
20505 for (i
= 0; i
< ntparms
; ++i
)
20506 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
20509 /* Verify that nondeduced template arguments agree with the type
20510 obtained from argument deduction.
20514 struct A { typedef int X; };
20515 template <class T, class U> struct C {};
20516 template <class T> struct C<T, typename T::X> {};
20518 Then with the instantiation `C<A, int>', we can deduce that
20519 `T' is `A' but unify () does not check whether `typename T::X'
20521 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
20522 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20524 tf_none
, false, false);
20525 if (spec_args
== error_mark_node
20526 /* We only need to check the innermost arguments; the other
20527 arguments will always agree. */
20528 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
20529 INNERMOST_TEMPLATE_ARGS (args
)))
20532 /* Now that we have bindings for all of the template arguments,
20533 ensure that the arguments deduced for the template template
20534 parameters have compatible template parameter lists. See the use
20535 of template_template_parm_bindings_ok_p in fn_type_unification
20536 for more information. */
20537 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
20540 return deduced_args
;
20543 // Compare two function templates T1 and T2 by deducing bindings
20544 // from one against the other. If both deductions succeed, compare
20545 // constraints to see which is more constrained.
20547 more_specialized_inst (tree t1
, tree t2
)
20552 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
20558 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
20564 // If both deductions succeed, then one may be more constrained.
20565 if (count
== 2 && fate
== 0)
20566 fate
= more_constrained (t1
, t2
);
20571 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20572 Return the TREE_LIST node with the most specialized template, if
20573 any. If there is no most specialized template, the error_mark_node
20576 Note that this function does not look at, or modify, the
20577 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20578 returned is one of the elements of INSTANTIATIONS, callers may
20579 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20580 and retrieve it from the value returned. */
20583 most_specialized_instantiation (tree templates
)
20587 ++processing_template_decl
;
20590 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
20592 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
20597 /* Equally specialized, move to next function. If there
20598 is no next function, nothing's most specialized. */
20599 fn
= TREE_CHAIN (fn
);
20607 /* Now verify that champ is better than everything earlier in the
20608 instantiation list. */
20609 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
20610 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
20617 processing_template_decl
--;
20620 return error_mark_node
;
20625 /* If DECL is a specialization of some template, return the most
20626 general such template. Otherwise, returns NULL_TREE.
20628 For example, given:
20630 template <class T> struct S { template <class U> void f(U); };
20632 if TMPL is `template <class U> void S<int>::f(U)' this will return
20633 the full template. This function will not trace past partial
20634 specializations, however. For example, given in addition:
20636 template <class T> struct S<T*> { template <class U> void f(U); };
20638 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20639 `template <class T> template <class U> S<T*>::f(U)'. */
20642 most_general_template (tree decl
)
20644 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20646 if (tree tinfo
= get_template_info (decl
))
20647 decl
= TI_TEMPLATE (tinfo
);
20648 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20649 template friend, or a FIELD_DECL for a capture pack. */
20650 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20654 /* Look for more and more general templates. */
20655 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
20657 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20658 (See cp-tree.h for details.) */
20659 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
20662 if (CLASS_TYPE_P (TREE_TYPE (decl
))
20663 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
20664 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
20667 /* Stop if we run into an explicitly specialized class template. */
20668 if (!DECL_NAMESPACE_SCOPE_P (decl
)
20669 && DECL_CONTEXT (decl
)
20670 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
20673 decl
= DECL_TI_TEMPLATE (decl
);
20679 /* Return the most specialized of the template partial specializations
20680 which can produce TARGET, a specialization of some class or variable
20681 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20682 a TEMPLATE_DECL node corresponding to the partial specialization, while
20683 the TREE_PURPOSE is the set of template arguments that must be
20684 substituted into the template pattern in order to generate TARGET.
20686 If the choice of partial specialization is ambiguous, a diagnostic
20687 is issued, and the error_mark_node is returned. If there are no
20688 partial specializations matching TARGET, then NULL_TREE is
20689 returned, indicating that the primary template should be used. */
20692 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
20694 tree list
= NULL_TREE
;
20699 tree outer_args
= NULL_TREE
;
20702 if (TYPE_P (target
))
20704 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
20705 tmpl
= TI_TEMPLATE (tinfo
);
20706 args
= TI_ARGS (tinfo
);
20708 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
20710 tmpl
= TREE_OPERAND (target
, 0);
20711 args
= TREE_OPERAND (target
, 1);
20713 else if (VAR_P (target
))
20715 tree tinfo
= DECL_TEMPLATE_INFO (target
);
20716 tmpl
= TI_TEMPLATE (tinfo
);
20717 args
= TI_ARGS (tinfo
);
20720 gcc_unreachable ();
20722 tree main_tmpl
= most_general_template (tmpl
);
20724 /* For determining which partial specialization to use, only the
20725 innermost args are interesting. */
20726 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20728 outer_args
= strip_innermost_template_args (args
, 1);
20729 args
= INNERMOST_TEMPLATE_ARGS (args
);
20732 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
20734 tree partial_spec_args
;
20736 tree spec_tmpl
= TREE_VALUE (t
);
20738 partial_spec_args
= TREE_PURPOSE (t
);
20740 ++processing_template_decl
;
20744 /* Discard the outer levels of args, and then substitute in the
20745 template args from the enclosing class. */
20746 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
20747 partial_spec_args
= tsubst_template_args
20748 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
20750 /* And the same for the partial specialization TEMPLATE_DECL. */
20751 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
20754 partial_spec_args
=
20755 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20758 /*require_all_args=*/true,
20759 /*use_default_args=*/true);
20761 --processing_template_decl
;
20763 if (partial_spec_args
== error_mark_node
)
20764 return error_mark_node
;
20765 if (spec_tmpl
== error_mark_node
)
20766 return error_mark_node
;
20768 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
20769 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
20775 spec_args
= add_to_template_args (outer_args
, spec_args
);
20777 /* Keep the candidate only if the constraints are satisfied,
20778 or if we're not compiling with concepts. */
20780 || constraints_satisfied_p (spec_tmpl
, spec_args
))
20782 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
20783 TREE_TYPE (list
) = TREE_TYPE (t
);
20791 ambiguous_p
= false;
20794 t
= TREE_CHAIN (t
);
20795 for (; t
; t
= TREE_CHAIN (t
))
20797 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20804 t
= TREE_CHAIN (t
);
20807 ambiguous_p
= true;
20816 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
20818 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20821 ambiguous_p
= true;
20829 char *spaces
= NULL
;
20830 if (!(complain
& tf_error
))
20831 return error_mark_node
;
20832 if (TYPE_P (target
))
20833 error ("ambiguous template instantiation for %q#T", target
);
20835 error ("ambiguous template instantiation for %q#D", target
);
20836 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
20837 for (t
= list
; t
; t
= TREE_CHAIN (t
))
20839 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
20840 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
20841 "%s %#S", spaces
? spaces
: str
, subst
);
20842 spaces
= spaces
? spaces
: get_spaces (str
);
20845 return error_mark_node
;
20851 /* Explicitly instantiate DECL. */
20854 do_decl_instantiation (tree decl
, tree storage
)
20856 tree result
= NULL_TREE
;
20859 if (!decl
|| decl
== error_mark_node
)
20860 /* An error occurred, for which grokdeclarator has already issued
20861 an appropriate message. */
20863 else if (! DECL_LANG_SPECIFIC (decl
))
20865 error ("explicit instantiation of non-template %q#D", decl
);
20869 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
20870 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
20872 if (VAR_P (decl
) && !var_templ
)
20874 /* There is an asymmetry here in the way VAR_DECLs and
20875 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20876 the latter, the DECL we get back will be marked as a
20877 template instantiation, and the appropriate
20878 DECL_TEMPLATE_INFO will be set up. This does not happen for
20879 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20880 should handle VAR_DECLs as it currently handles
20882 if (!DECL_CLASS_SCOPE_P (decl
))
20884 error ("%qD is not a static data member of a class template", decl
);
20887 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
20888 if (!result
|| !VAR_P (result
))
20890 error ("no matching template for %qD found", decl
);
20893 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
20895 error ("type %qT for explicit instantiation %qD does not match "
20896 "declared type %qT", TREE_TYPE (result
), decl
,
20901 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
20903 error ("explicit instantiation of %q#D", decl
);
20909 /* Check for various error cases. Note that if the explicit
20910 instantiation is valid the RESULT will currently be marked as an
20911 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20912 until we get here. */
20914 if (DECL_TEMPLATE_SPECIALIZATION (result
))
20916 /* DR 259 [temp.spec].
20918 Both an explicit instantiation and a declaration of an explicit
20919 specialization shall not appear in a program unless the explicit
20920 instantiation follows a declaration of the explicit specialization.
20922 For a given set of template parameters, if an explicit
20923 instantiation of a template appears after a declaration of an
20924 explicit specialization for that template, the explicit
20925 instantiation has no effect. */
20928 else if (DECL_EXPLICIT_INSTANTIATION (result
))
20932 No program shall explicitly instantiate any template more
20935 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20936 the first instantiation was `extern' and the second is not,
20937 and EXTERN_P for the opposite case. */
20938 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
20939 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
20940 /* If an "extern" explicit instantiation follows an ordinary
20941 explicit instantiation, the template is instantiated. */
20945 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
20947 error ("no matching template for %qD found", result
);
20950 else if (!DECL_TEMPLATE_INFO (result
))
20952 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
20956 if (storage
== NULL_TREE
)
20958 else if (storage
== ridpointers
[(int) RID_EXTERN
])
20960 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
20961 pedwarn (input_location
, OPT_Wpedantic
,
20962 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20967 error ("storage class %qD applied to template instantiation", storage
);
20969 check_explicit_instantiation_namespace (result
);
20970 mark_decl_instantiated (result
, extern_p
);
20972 instantiate_decl (result
, /*defer_ok=*/1,
20973 /*expl_inst_class_mem_p=*/false);
20977 mark_class_instantiated (tree t
, int extern_p
)
20979 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
20980 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
20981 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
20982 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
20985 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
20986 rest_of_type_compilation (t
, 1);
20990 /* Called from do_type_instantiation through binding_table_foreach to
20991 do recursive instantiation for the type bound in ENTRY. */
20993 bt_instantiate_type_proc (binding_entry entry
, void *data
)
20995 tree storage
= *(tree
*) data
;
20997 if (MAYBE_CLASS_TYPE_P (entry
->type
)
20998 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
20999 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
21002 /* Called from do_type_instantiation to instantiate a member
21003 (a member function or a static member variable) of an
21004 explicitly instantiated class template. */
21006 instantiate_class_member (tree decl
, int extern_p
)
21008 mark_decl_instantiated (decl
, extern_p
);
21010 instantiate_decl (decl
, /*defer_ok=*/1,
21011 /*expl_inst_class_mem_p=*/true);
21014 /* Perform an explicit instantiation of template class T. STORAGE, if
21015 non-null, is the RID for extern, inline or static. COMPLAIN is
21016 nonzero if this is called from the parser, zero if called recursively,
21017 since the standard is unclear (as detailed below). */
21020 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
21025 int previous_instantiation_extern_p
= 0;
21027 if (TREE_CODE (t
) == TYPE_DECL
)
21030 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
21033 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
21035 error ("explicit instantiation of non-class template %qD", tmpl
);
21037 error ("explicit instantiation of non-template type %qT", t
);
21043 if (!COMPLETE_TYPE_P (t
))
21045 if (complain
& tf_error
)
21046 error ("explicit instantiation of %q#T before definition of template",
21051 if (storage
!= NULL_TREE
)
21053 if (!in_system_header_at (input_location
))
21055 if (storage
== ridpointers
[(int) RID_EXTERN
])
21057 if (cxx_dialect
== cxx98
)
21058 pedwarn (input_location
, OPT_Wpedantic
,
21059 "ISO C++ 1998 forbids the use of %<extern%> on "
21060 "explicit instantiations");
21063 pedwarn (input_location
, OPT_Wpedantic
,
21064 "ISO C++ forbids the use of %qE"
21065 " on explicit instantiations", storage
);
21068 if (storage
== ridpointers
[(int) RID_INLINE
])
21070 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21072 else if (storage
== ridpointers
[(int) RID_STATIC
])
21076 error ("storage class %qD applied to template instantiation",
21082 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
21084 /* DR 259 [temp.spec].
21086 Both an explicit instantiation and a declaration of an explicit
21087 specialization shall not appear in a program unless the explicit
21088 instantiation follows a declaration of the explicit specialization.
21090 For a given set of template parameters, if an explicit
21091 instantiation of a template appears after a declaration of an
21092 explicit specialization for that template, the explicit
21093 instantiation has no effect. */
21096 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
21100 No program shall explicitly instantiate any template more
21103 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21104 instantiation was `extern'. If EXTERN_P then the second is.
21105 These cases are OK. */
21106 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
21108 if (!previous_instantiation_extern_p
&& !extern_p
21109 && (complain
& tf_error
))
21110 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
21112 /* If we've already instantiated the template, just return now. */
21113 if (!CLASSTYPE_INTERFACE_ONLY (t
))
21117 check_explicit_instantiation_namespace (TYPE_NAME (t
));
21118 mark_class_instantiated (t
, extern_p
);
21126 /* In contrast to implicit instantiation, where only the
21127 declarations, and not the definitions, of members are
21128 instantiated, we have here:
21132 The explicit instantiation of a class template specialization
21133 implies the instantiation of all of its members not
21134 previously explicitly specialized in the translation unit
21135 containing the explicit instantiation.
21137 Of course, we can't instantiate member template classes, since
21138 we don't have any arguments for them. Note that the standard
21139 is unclear on whether the instantiation of the members are
21140 *explicit* instantiations or not. However, the most natural
21141 interpretation is that it should be an explicit instantiation. */
21144 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21145 if (TREE_CODE (tmp
) == FUNCTION_DECL
21146 && DECL_TEMPLATE_INSTANTIATION (tmp
))
21147 instantiate_class_member (tmp
, extern_p
);
21149 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21150 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
21151 instantiate_class_member (tmp
, extern_p
);
21153 if (CLASSTYPE_NESTED_UTDS (t
))
21154 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
21155 bt_instantiate_type_proc
, &storage
);
21159 /* Given a function DECL, which is a specialization of TMPL, modify
21160 DECL to be a re-instantiation of TMPL with the same template
21161 arguments. TMPL should be the template into which tsubst'ing
21162 should occur for DECL, not the most general template.
21164 One reason for doing this is a scenario like this:
21167 void f(const T&, int i);
21169 void g() { f(3, 7); }
21172 void f(const T& t, const int i) { }
21174 Note that when the template is first instantiated, with
21175 instantiate_template, the resulting DECL will have no name for the
21176 first parameter, and the wrong type for the second. So, when we go
21177 to instantiate the DECL, we regenerate it. */
21180 regenerate_decl_from_template (tree decl
, tree tmpl
)
21182 /* The arguments used to instantiate DECL, from the most general
21187 args
= DECL_TI_ARGS (decl
);
21188 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21190 /* Make sure that we can see identifiers, and compute access
21192 push_access_scope (decl
);
21194 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21202 args_depth
= TMPL_ARGS_DEPTH (args
);
21203 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
21204 if (args_depth
> parms_depth
)
21205 args
= get_innermost_template_args (args
, parms_depth
);
21207 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
21208 args
, tf_error
, NULL_TREE
,
21209 /*defer_ok*/false);
21210 if (specs
&& specs
!= error_mark_node
)
21211 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
21214 /* Merge parameter declarations. */
21215 decl_parm
= skip_artificial_parms_for (decl
,
21216 DECL_ARGUMENTS (decl
));
21218 = skip_artificial_parms_for (code_pattern
,
21219 DECL_ARGUMENTS (code_pattern
));
21220 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
21225 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21226 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
21227 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
21229 parm_type
= type_decays_to (parm_type
);
21230 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21231 TREE_TYPE (decl_parm
) = parm_type
;
21232 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21233 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21235 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21236 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21238 decl_parm
= DECL_CHAIN (decl_parm
);
21239 pattern_parm
= DECL_CHAIN (pattern_parm
);
21241 /* Merge any parameters that match with the function parameter
21243 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
21246 tree expanded_types
;
21247 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21248 the parameters in this function parameter pack. */
21249 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
21250 args
, tf_error
, NULL_TREE
);
21251 len
= TREE_VEC_LENGTH (expanded_types
);
21252 for (i
= 0; i
< len
; i
++)
21257 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21258 /* Rename the parameter to include the index. */
21259 DECL_NAME (decl_parm
) =
21260 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
21261 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
21262 parm_type
= type_decays_to (parm_type
);
21263 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21264 TREE_TYPE (decl_parm
) = parm_type
;
21265 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21266 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21268 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21269 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21271 decl_parm
= DECL_CHAIN (decl_parm
);
21274 /* Merge additional specifiers from the CODE_PATTERN. */
21275 if (DECL_DECLARED_INLINE_P (code_pattern
)
21276 && !DECL_DECLARED_INLINE_P (decl
))
21277 DECL_DECLARED_INLINE_P (decl
) = 1;
21279 else if (VAR_P (decl
))
21281 DECL_INITIAL (decl
) =
21282 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
21283 tf_error
, DECL_TI_TEMPLATE (decl
),
21284 /*integral_constant_expression_p=*/false);
21285 if (VAR_HAD_UNKNOWN_BOUND (decl
))
21286 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
21287 tf_error
, DECL_TI_TEMPLATE (decl
));
21290 gcc_unreachable ();
21292 pop_access_scope (decl
);
21295 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21296 substituted to get DECL. */
21299 template_for_substitution (tree decl
)
21301 tree tmpl
= DECL_TI_TEMPLATE (decl
);
21303 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21304 for the instantiation. This is not always the most general
21305 template. Consider, for example:
21308 struct S { template <class U> void f();
21309 template <> void f<int>(); };
21311 and an instantiation of S<double>::f<int>. We want TD to be the
21312 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21313 while (/* An instantiation cannot have a definition, so we need a
21314 more general template. */
21315 DECL_TEMPLATE_INSTANTIATION (tmpl
)
21316 /* We must also deal with friend templates. Given:
21318 template <class T> struct S {
21319 template <class U> friend void f() {};
21322 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21323 so far as the language is concerned, but that's still
21324 where we get the pattern for the instantiation from. On
21325 other hand, if the definition comes outside the class, say:
21327 template <class T> struct S {
21328 template <class U> friend void f();
21330 template <class U> friend void f() {}
21332 we don't need to look any further. That's what the check for
21333 DECL_INITIAL is for. */
21334 || (TREE_CODE (decl
) == FUNCTION_DECL
21335 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
21336 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
21338 /* The present template, TD, should not be a definition. If it
21339 were a definition, we should be using it! Note that we
21340 cannot restructure the loop to just keep going until we find
21341 a template with a definition, since that might go too far if
21342 a specialization was declared, but not defined. */
21344 /* Fetch the more general template. */
21345 tmpl
= DECL_TI_TEMPLATE (tmpl
);
21351 /* Returns true if we need to instantiate this template instance even if we
21352 know we aren't going to emit it. */
21355 always_instantiate_p (tree decl
)
21357 /* We always instantiate inline functions so that we can inline them. An
21358 explicit instantiation declaration prohibits implicit instantiation of
21359 non-inline functions. With high levels of optimization, we would
21360 normally inline non-inline functions -- but we're not allowed to do
21361 that for "extern template" functions. Therefore, we check
21362 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21363 return ((TREE_CODE (decl
) == FUNCTION_DECL
21364 && (DECL_DECLARED_INLINE_P (decl
)
21365 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
21366 /* And we need to instantiate static data members so that
21367 their initializers are available in integral constant
21370 && decl_maybe_constant_var_p (decl
)));
21373 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21374 instantiate it now, modifying TREE_TYPE (fn). */
21377 maybe_instantiate_noexcept (tree fn
)
21379 tree fntype
, spec
, noex
, clone
;
21381 /* Don't instantiate a noexcept-specification from template context. */
21382 if (processing_template_decl
)
21385 if (DECL_CLONED_FUNCTION_P (fn
))
21386 fn
= DECL_CLONED_FUNCTION (fn
);
21387 fntype
= TREE_TYPE (fn
);
21388 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
21390 if (!spec
|| !TREE_PURPOSE (spec
))
21393 noex
= TREE_PURPOSE (spec
);
21395 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
21397 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
21398 spec
= get_defaulted_eh_spec (fn
);
21399 else if (push_tinst_level (fn
))
21401 push_access_scope (fn
);
21402 push_deferring_access_checks (dk_no_deferred
);
21403 input_location
= DECL_SOURCE_LOCATION (fn
);
21404 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
21405 DEFERRED_NOEXCEPT_ARGS (noex
),
21406 tf_warning_or_error
, fn
,
21407 /*function_p=*/false,
21408 /*integral_constant_expression_p=*/true);
21409 pop_deferring_access_checks ();
21410 pop_access_scope (fn
);
21411 pop_tinst_level ();
21412 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
21413 if (spec
== error_mark_node
)
21414 spec
= noexcept_false_spec
;
21417 spec
= noexcept_false_spec
;
21419 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
21422 FOR_EACH_CLONE (clone
, fn
)
21424 if (TREE_TYPE (clone
) == fntype
)
21425 TREE_TYPE (clone
) = TREE_TYPE (fn
);
21427 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
21431 /* Produce the definition of D, a _DECL generated from a template. If
21432 DEFER_OK is nonzero, then we don't have to actually do the
21433 instantiation now; we just have to do it sometime. Normally it is
21434 an error if this is an explicit instantiation but D is undefined.
21435 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21436 explicitly instantiated class template. */
21439 instantiate_decl (tree d
, int defer_ok
,
21440 bool expl_inst_class_mem_p
)
21442 tree tmpl
= DECL_TI_TEMPLATE (d
);
21449 bool pattern_defined
;
21450 location_t saved_loc
= input_location
;
21451 int saved_unevaluated_operand
= cp_unevaluated_operand
;
21452 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
21456 bool nested
= false;
21458 /* This function should only be used to instantiate templates for
21459 functions and static member variables. */
21460 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
21462 /* A concept is never instantiated. */
21463 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
21465 /* Variables are never deferred; if instantiation is required, they
21466 are instantiated right away. That allows for better code in the
21467 case that an expression refers to the value of the variable --
21468 if the variable has a constant value the referring expression can
21469 take advantage of that fact. */
21471 || DECL_DECLARED_CONSTEXPR_P (d
))
21474 /* Don't instantiate cloned functions. Instead, instantiate the
21475 functions they cloned. */
21476 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
21477 d
= DECL_CLONED_FUNCTION (d
);
21479 if (DECL_TEMPLATE_INSTANTIATED (d
)
21480 || (TREE_CODE (d
) == FUNCTION_DECL
21481 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
21482 || DECL_TEMPLATE_SPECIALIZATION (d
))
21483 /* D has already been instantiated or explicitly specialized, so
21484 there's nothing for us to do here.
21486 It might seem reasonable to check whether or not D is an explicit
21487 instantiation, and, if so, stop here. But when an explicit
21488 instantiation is deferred until the end of the compilation,
21489 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21490 the instantiation. */
21493 /* Check to see whether we know that this template will be
21494 instantiated in some other file, as with "extern template"
21496 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
21498 /* In general, we do not instantiate such templates. */
21499 if (external_p
&& !always_instantiate_p (d
))
21502 gen_tmpl
= most_general_template (tmpl
);
21503 gen_args
= DECL_TI_ARGS (d
);
21505 if (tmpl
!= gen_tmpl
)
21506 /* We should already have the extra args. */
21507 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
21508 == TMPL_ARGS_DEPTH (gen_args
));
21509 /* And what's in the hash table should match D. */
21510 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
21511 || spec
== NULL_TREE
);
21513 /* This needs to happen before any tsubsting. */
21514 if (! push_tinst_level (d
))
21517 timevar_push (TV_TEMPLATE_INST
);
21519 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21520 for the instantiation. */
21521 td
= template_for_substitution (d
);
21522 code_pattern
= DECL_TEMPLATE_RESULT (td
);
21524 /* We should never be trying to instantiate a member of a class
21525 template or partial specialization. */
21526 gcc_assert (d
!= code_pattern
);
21528 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
21529 || DECL_TEMPLATE_SPECIALIZATION (td
))
21530 /* In the case of a friend template whose definition is provided
21531 outside the class, we may have too many arguments. Drop the
21532 ones we don't need. The same is true for specializations. */
21533 args
= get_innermost_template_args
21534 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
21538 if (TREE_CODE (d
) == FUNCTION_DECL
)
21540 deleted_p
= DECL_DELETED_FN (code_pattern
);
21541 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
21542 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
21548 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
21551 /* We may be in the middle of deferred access check. Disable it now. */
21552 push_deferring_access_checks (dk_no_deferred
);
21554 /* Unless an explicit instantiation directive has already determined
21555 the linkage of D, remember that a definition is available for
21557 if (pattern_defined
21558 && !DECL_INTERFACE_KNOWN (d
)
21559 && !DECL_NOT_REALLY_EXTERN (d
))
21560 mark_definable (d
);
21562 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
21563 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
21564 input_location
= DECL_SOURCE_LOCATION (d
);
21566 /* If D is a member of an explicitly instantiated class template,
21567 and no definition is available, treat it like an implicit
21569 if (!pattern_defined
&& expl_inst_class_mem_p
21570 && DECL_EXPLICIT_INSTANTIATION (d
))
21572 /* Leave linkage flags alone on instantiations with anonymous
21574 if (TREE_PUBLIC (d
))
21576 DECL_NOT_REALLY_EXTERN (d
) = 0;
21577 DECL_INTERFACE_KNOWN (d
) = 0;
21579 SET_DECL_IMPLICIT_INSTANTIATION (d
);
21582 /* Defer all other templates, unless we have been explicitly
21583 forbidden from doing so. */
21584 if (/* If there is no definition, we cannot instantiate the
21587 /* If it's OK to postpone instantiation, do so. */
21589 /* If this is a static data member that will be defined
21590 elsewhere, we don't want to instantiate the entire data
21591 member, but we do want to instantiate the initializer so that
21592 we can substitute that elsewhere. */
21593 || (external_p
&& VAR_P (d
))
21594 /* Handle here a deleted function too, avoid generating
21595 its body (c++/61080). */
21598 /* The definition of the static data member is now required so
21599 we must substitute the initializer. */
21601 && !DECL_INITIAL (d
)
21602 && DECL_INITIAL (code_pattern
))
21606 bool const_init
= false;
21607 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21609 ns
= decl_namespace_context (d
);
21610 push_nested_namespace (ns
);
21612 push_nested_class (DECL_CONTEXT (d
));
21613 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
21615 tf_warning_or_error
, NULL_TREE
,
21616 /*integral_constant_expression_p=*/false);
21617 /* If instantiating the initializer involved instantiating this
21618 again, don't call cp_finish_decl twice. */
21619 if (!DECL_INITIAL (d
))
21621 /* Make sure the initializer is still constant, in case of
21622 circular dependency (template/instantiate6.C). */
21624 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21625 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
21626 /*asmspec_tree=*/NULL_TREE
,
21627 LOOKUP_ONLYCONVERTING
);
21630 pop_nested_class ();
21631 pop_nested_namespace (ns
);
21634 /* We restore the source position here because it's used by
21635 add_pending_template. */
21636 input_location
= saved_loc
;
21638 if (at_eof
&& !pattern_defined
21639 && DECL_EXPLICIT_INSTANTIATION (d
)
21640 && DECL_NOT_REALLY_EXTERN (d
))
21643 The definition of a non-exported function template, a
21644 non-exported member function template, or a non-exported
21645 member function or static data member of a class template
21646 shall be present in every translation unit in which it is
21647 explicitly instantiated. */
21648 permerror (input_location
, "explicit instantiation of %qD "
21649 "but no definition available", d
);
21651 /* If we're in unevaluated context, we just wanted to get the
21652 constant value; this isn't an odr use, so don't queue
21653 a full instantiation. */
21654 if (cp_unevaluated_operand
!= 0)
21656 /* ??? Historically, we have instantiated inline functions, even
21657 when marked as "extern template". */
21658 if (!(external_p
&& VAR_P (d
)))
21659 add_pending_template (d
);
21662 /* Tell the repository that D is available in this translation unit
21663 -- and see if it is supposed to be instantiated here. */
21664 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
21666 /* In a PCH file, despite the fact that the repository hasn't
21667 requested instantiation in the PCH it is still possible that
21668 an instantiation will be required in a file that includes the
21671 add_pending_template (d
);
21672 /* Instantiate inline functions so that the inliner can do its
21673 job, even though we'll not be emitting a copy of this
21675 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
21679 fn_context
= decl_function_context (d
);
21680 nested
= (current_function_decl
!= NULL_TREE
);
21681 vec
<tree
> omp_privatization_save
;
21683 save_omp_privatization_clauses (omp_privatization_save
);
21686 push_to_top_level ();
21690 push_function_context ();
21691 cp_unevaluated_operand
= 0;
21692 c_inhibit_evaluation_warnings
= 0;
21695 /* Mark D as instantiated so that recursive calls to
21696 instantiate_decl do not try to instantiate it again. */
21697 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
21699 /* Regenerate the declaration in case the template has been modified
21700 by a subsequent redeclaration. */
21701 regenerate_decl_from_template (d
, td
);
21703 /* We already set the file and line above. Reset them now in case
21704 they changed as a result of calling regenerate_decl_from_template. */
21705 input_location
= DECL_SOURCE_LOCATION (d
);
21710 bool const_init
= false;
21712 /* Clear out DECL_RTL; whatever was there before may not be right
21713 since we've reset the type of the declaration. */
21714 SET_DECL_RTL (d
, NULL
);
21715 DECL_IN_AGGR_P (d
) = 0;
21717 /* The initializer is placed in DECL_INITIAL by
21718 regenerate_decl_from_template so we don't need to
21719 push/pop_access_scope again here. Pull it out so that
21720 cp_finish_decl can process it. */
21721 init
= DECL_INITIAL (d
);
21722 DECL_INITIAL (d
) = NULL_TREE
;
21723 DECL_INITIALIZED_P (d
) = 0;
21725 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21726 initializer. That function will defer actual emission until
21727 we have a chance to determine linkage. */
21728 DECL_EXTERNAL (d
) = 0;
21730 /* Enter the scope of D so that access-checking works correctly. */
21731 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21733 push_nested_class (DECL_CONTEXT (d
));
21735 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21736 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
21739 pop_nested_class ();
21741 if (variable_template_p (td
))
21742 note_variable_template_instantiation (d
);
21744 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
21745 synthesize_method (d
);
21746 else if (TREE_CODE (d
) == FUNCTION_DECL
)
21748 hash_map
<tree
, tree
> *saved_local_specializations
;
21752 tree block
= NULL_TREE
;
21754 /* Save away the current list, in case we are instantiating one
21755 template from within the body of another. */
21756 saved_local_specializations
= local_specializations
;
21758 /* Set up the list of local specializations. */
21759 local_specializations
= new hash_map
<tree
, tree
>;
21761 /* Set up context. */
21762 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21763 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21764 block
= push_stmt_list ();
21766 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
21768 /* Some typedefs referenced from within the template code need to be
21769 access checked at template instantiation time, i.e now. These
21770 types were added to the template at parsing time. Let's get those
21771 and perform the access checks then. */
21772 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
21775 /* Create substitution entries for the parameters. */
21776 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
21777 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
21778 spec_parm
= DECL_ARGUMENTS (d
);
21779 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
21781 register_local_specialization (spec_parm
, tmpl_parm
);
21782 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
21783 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
21785 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
21787 if (!DECL_PACK_P (tmpl_parm
))
21789 register_local_specialization (spec_parm
, tmpl_parm
);
21790 spec_parm
= DECL_CHAIN (spec_parm
);
21794 /* Register the (value) argument pack as a specialization of
21795 TMPL_PARM, then move on. */
21796 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
21797 register_local_specialization (argpack
, tmpl_parm
);
21800 gcc_assert (!spec_parm
);
21802 /* Substitute into the body of the function. */
21803 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21804 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
21805 tf_warning_or_error
, tmpl
);
21808 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
21809 tf_warning_or_error
, tmpl
,
21810 /*integral_constant_expression_p=*/false);
21812 /* Set the current input_location to the end of the function
21813 so that finish_function knows where we are. */
21815 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
21817 /* Remember if we saw an infinite loop in the template. */
21818 current_function_infinite_loop
21819 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
21822 /* We don't need the local specializations any more. */
21823 delete local_specializations
;
21824 local_specializations
= saved_local_specializations
;
21826 /* Finish the function. */
21827 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21828 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21829 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
21832 d
= finish_function (0);
21833 expand_or_defer_fn (d
);
21836 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21837 cp_check_omp_declare_reduction (d
);
21840 /* We're not deferring instantiation any more. */
21841 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
21844 pop_from_top_level ();
21846 pop_function_context ();
21849 input_location
= saved_loc
;
21850 cp_unevaluated_operand
= saved_unevaluated_operand
;
21851 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
21852 pop_deferring_access_checks ();
21853 pop_tinst_level ();
21855 restore_omp_privatization_clauses (omp_privatization_save
);
21857 timevar_pop (TV_TEMPLATE_INST
);
21862 /* Run through the list of templates that we wish we could
21863 instantiate, and instantiate any we can. RETRIES is the
21864 number of times we retry pending template instantiation. */
21867 instantiate_pending_templates (int retries
)
21870 location_t saved_loc
= input_location
;
21872 /* Instantiating templates may trigger vtable generation. This in turn
21873 may require further template instantiations. We place a limit here
21874 to avoid infinite loop. */
21875 if (pending_templates
&& retries
>= max_tinst_depth
)
21877 tree decl
= pending_templates
->tinst
->decl
;
21879 fatal_error (input_location
,
21880 "template instantiation depth exceeds maximum of %d"
21881 " instantiating %q+D, possibly from virtual table generation"
21882 " (use -ftemplate-depth= to increase the maximum)",
21883 max_tinst_depth
, decl
);
21884 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21885 /* Pretend that we defined it. */
21886 DECL_INITIAL (decl
) = error_mark_node
;
21892 struct pending_template
**t
= &pending_templates
;
21893 struct pending_template
*last
= NULL
;
21897 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
21898 bool complete
= false;
21900 if (TYPE_P (instantiation
))
21904 if (!COMPLETE_TYPE_P (instantiation
))
21906 instantiate_class_template (instantiation
);
21907 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
21908 for (fn
= TYPE_METHODS (instantiation
);
21910 fn
= TREE_CHAIN (fn
))
21911 if (! DECL_ARTIFICIAL (fn
))
21912 instantiate_decl (fn
,
21914 /*expl_inst_class_mem_p=*/false);
21915 if (COMPLETE_TYPE_P (instantiation
))
21919 complete
= COMPLETE_TYPE_P (instantiation
);
21923 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21924 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
21927 = instantiate_decl (instantiation
,
21929 /*expl_inst_class_mem_p=*/false);
21930 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
21934 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21935 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
21939 /* If INSTANTIATION has been instantiated, then we don't
21940 need to consider it again in the future. */
21948 current_tinst_level
= NULL
;
21950 last_pending_template
= last
;
21952 while (reconsider
);
21954 input_location
= saved_loc
;
21957 /* Substitute ARGVEC into T, which is a list of initializers for
21958 either base class or a non-static data member. The TREE_PURPOSEs
21959 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21960 instantiate_decl. */
21963 tsubst_initializer_list (tree t
, tree argvec
)
21965 tree inits
= NULL_TREE
;
21967 for (; t
; t
= TREE_CHAIN (t
))
21971 tree expanded_bases
= NULL_TREE
;
21972 tree expanded_arguments
= NULL_TREE
;
21975 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
21980 /* Expand the base class expansion type into separate base
21982 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
21983 tf_warning_or_error
,
21985 if (expanded_bases
== error_mark_node
)
21988 /* We'll be building separate TREE_LISTs of arguments for
21990 len
= TREE_VEC_LENGTH (expanded_bases
);
21991 expanded_arguments
= make_tree_vec (len
);
21992 for (i
= 0; i
< len
; i
++)
21993 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
21995 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
21996 expand each argument in the TREE_VALUE of t. */
21997 expr
= make_node (EXPR_PACK_EXPANSION
);
21998 PACK_EXPANSION_LOCAL_P (expr
) = true;
21999 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
22000 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
22002 if (TREE_VALUE (t
) == void_type_node
)
22003 /* VOID_TYPE_NODE is used to indicate
22004 value-initialization. */
22006 for (i
= 0; i
< len
; i
++)
22007 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
22011 /* Substitute parameter packs into each argument in the
22013 in_base_initializer
= 1;
22014 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
22016 tree expanded_exprs
;
22018 /* Expand the argument. */
22019 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
22021 = tsubst_pack_expansion (expr
, argvec
,
22022 tf_warning_or_error
,
22024 if (expanded_exprs
== error_mark_node
)
22027 /* Prepend each of the expanded expressions to the
22028 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22029 for (i
= 0; i
< len
; i
++)
22031 TREE_VEC_ELT (expanded_arguments
, i
) =
22032 tree_cons (NULL_TREE
,
22033 TREE_VEC_ELT (expanded_exprs
, i
),
22034 TREE_VEC_ELT (expanded_arguments
, i
));
22037 in_base_initializer
= 0;
22039 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22040 since we built them backwards. */
22041 for (i
= 0; i
< len
; i
++)
22043 TREE_VEC_ELT (expanded_arguments
, i
) =
22044 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
22049 for (i
= 0; i
< len
; ++i
)
22051 if (expanded_bases
)
22053 decl
= TREE_VEC_ELT (expanded_bases
, i
);
22054 decl
= expand_member_init (decl
);
22055 init
= TREE_VEC_ELT (expanded_arguments
, i
);
22060 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
22061 tf_warning_or_error
, NULL_TREE
);
22063 decl
= expand_member_init (decl
);
22064 if (decl
&& !DECL_P (decl
))
22065 in_base_initializer
= 1;
22067 init
= TREE_VALUE (t
);
22069 if (init
!= void_type_node
)
22070 init
= tsubst_expr (init
, argvec
,
22071 tf_warning_or_error
, NULL_TREE
,
22072 /*integral_constant_expression_p=*/false);
22073 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
22074 /* If we had an initializer but it instantiated to nothing,
22075 value-initialize the object. This will only occur when
22076 the initializer was a pack expansion where the parameter
22077 packs used in that expansion were of length zero. */
22078 init
= void_type_node
;
22079 in_base_initializer
= 0;
22084 init
= build_tree_list (decl
, init
);
22085 TREE_CHAIN (init
) = inits
;
22093 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22096 set_current_access_from_decl (tree decl
)
22098 if (TREE_PRIVATE (decl
))
22099 current_access_specifier
= access_private_node
;
22100 else if (TREE_PROTECTED (decl
))
22101 current_access_specifier
= access_protected_node
;
22103 current_access_specifier
= access_public_node
;
22106 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22107 is the instantiation (which should have been created with
22108 start_enum) and ARGS are the template arguments to use. */
22111 tsubst_enum (tree tag
, tree newtag
, tree args
)
22115 if (SCOPED_ENUM_P (newtag
))
22116 begin_scope (sk_scoped_enum
, newtag
);
22118 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
22123 decl
= TREE_VALUE (e
);
22124 /* Note that in a template enum, the TREE_VALUE is the
22125 CONST_DECL, not the corresponding INTEGER_CST. */
22126 value
= tsubst_expr (DECL_INITIAL (decl
),
22127 args
, tf_warning_or_error
, NULL_TREE
,
22128 /*integral_constant_expression_p=*/true);
22130 /* Give this enumeration constant the correct access. */
22131 set_current_access_from_decl (decl
);
22133 /* Actually build the enumerator itself. Here we're assuming that
22134 enumerators can't have dependent attributes. */
22135 build_enumerator (DECL_NAME (decl
), value
, newtag
,
22136 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
22139 if (SCOPED_ENUM_P (newtag
))
22142 finish_enum_value_list (newtag
);
22143 finish_enum (newtag
);
22145 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
22146 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
22149 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22150 its type -- but without substituting the innermost set of template
22151 arguments. So, innermost set of template parameters will appear in
22155 get_mostly_instantiated_function_type (tree decl
)
22157 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22158 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
22161 /* Return truthvalue if we're processing a template different from
22162 the last one involved in diagnostics. */
22164 problematic_instantiation_changed (void)
22166 return current_tinst_level
!= last_error_tinst_level
;
22169 /* Remember current template involved in diagnostics. */
22171 record_last_problematic_instantiation (void)
22173 last_error_tinst_level
= current_tinst_level
;
22176 struct tinst_level
*
22177 current_instantiation (void)
22179 return current_tinst_level
;
22182 /* Return TRUE if current_function_decl is being instantiated, false
22186 instantiating_current_function_p (void)
22188 return (current_instantiation ()
22189 && current_instantiation ()->decl
== current_function_decl
);
22192 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22193 type. Return zero for ok, nonzero for disallowed. Issue error and
22194 warning messages under control of COMPLAIN. */
22197 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
22199 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
22201 else if (POINTER_TYPE_P (type
))
22203 else if (TYPE_PTRMEM_P (type
))
22205 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
22207 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
22209 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
22211 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
22213 /* A bound template template parm could later be instantiated to have a valid
22214 nontype parm type via an alias template. */
22215 else if (cxx_dialect
>= cxx11
22216 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22219 if (complain
& tf_error
)
22221 if (type
== error_mark_node
)
22222 inform (input_location
, "invalid template non-type parameter");
22224 error ("%q#T is not a valid type for a template non-type parameter",
22230 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22231 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22234 dependent_type_p_r (tree type
)
22240 A type is dependent if it is:
22242 -- a template parameter. Template template parameters are types
22243 for us (since TYPE_P holds true for them) so we handle
22245 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22246 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
22248 /* -- a qualified-id with a nested-name-specifier which contains a
22249 class-name that names a dependent type or whose unqualified-id
22250 names a dependent type. */
22251 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22254 /* An alias template specialization can be dependent even if the
22255 resulting type is not. */
22256 if (dependent_alias_template_spec_p (type
))
22259 /* -- a cv-qualified type where the cv-unqualified type is
22261 No code is necessary for this bullet; the code below handles
22262 cv-qualified types, and we don't want to strip aliases with
22263 TYPE_MAIN_VARIANT because of DR 1558. */
22264 /* -- a compound type constructed from any dependent type. */
22265 if (TYPE_PTRMEM_P (type
))
22266 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
22267 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22269 else if (TYPE_PTR_P (type
)
22270 || TREE_CODE (type
) == REFERENCE_TYPE
)
22271 return dependent_type_p (TREE_TYPE (type
));
22272 else if (TREE_CODE (type
) == FUNCTION_TYPE
22273 || TREE_CODE (type
) == METHOD_TYPE
)
22277 if (dependent_type_p (TREE_TYPE (type
)))
22279 for (arg_type
= TYPE_ARG_TYPES (type
);
22281 arg_type
= TREE_CHAIN (arg_type
))
22282 if (dependent_type_p (TREE_VALUE (arg_type
)))
22286 /* -- an array type constructed from any dependent type or whose
22287 size is specified by a constant expression that is
22290 We checked for type- and value-dependence of the bounds in
22291 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22292 if (TREE_CODE (type
) == ARRAY_TYPE
)
22294 if (TYPE_DOMAIN (type
)
22295 && dependent_type_p (TYPE_DOMAIN (type
)))
22297 return dependent_type_p (TREE_TYPE (type
));
22300 /* -- a template-id in which either the template name is a template
22302 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22304 /* ... or any of the template arguments is a dependent type or
22305 an expression that is type-dependent or value-dependent. */
22306 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
22307 && (any_dependent_template_arguments_p
22308 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
22311 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22312 dependent; if the argument of the `typeof' expression is not
22313 type-dependent, then it should already been have resolved. */
22314 if (TREE_CODE (type
) == TYPEOF_TYPE
22315 || TREE_CODE (type
) == DECLTYPE_TYPE
22316 || TREE_CODE (type
) == UNDERLYING_TYPE
)
22319 /* A template argument pack is dependent if any of its packed
22321 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
22323 tree args
= ARGUMENT_PACK_ARGS (type
);
22324 int i
, len
= TREE_VEC_LENGTH (args
);
22325 for (i
= 0; i
< len
; ++i
)
22326 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
22330 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22331 be template parameters. */
22332 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
22335 /* The standard does not specifically mention types that are local
22336 to template functions or local classes, but they should be
22337 considered dependent too. For example:
22339 template <int I> void f() {
22344 The size of `E' cannot be known until the value of `I' has been
22345 determined. Therefore, `E' must be considered dependent. */
22346 scope
= TYPE_CONTEXT (type
);
22347 if (scope
&& TYPE_P (scope
))
22348 return dependent_type_p (scope
);
22349 /* Don't use type_dependent_expression_p here, as it can lead
22350 to infinite recursion trying to determine whether a lambda
22351 nested in a lambda is dependent (c++/47687). */
22352 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
22353 && DECL_LANG_SPECIFIC (scope
)
22354 && DECL_TEMPLATE_INFO (scope
)
22355 && (any_dependent_template_arguments_p
22356 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
22359 /* Other types are non-dependent. */
22363 /* Returns TRUE if TYPE is dependent, in the sense of
22364 [temp.dep.type]. Note that a NULL type is considered dependent. */
22367 dependent_type_p (tree type
)
22369 /* If there are no template parameters in scope, then there can't be
22370 any dependent types. */
22371 if (!processing_template_decl
)
22373 /* If we are not processing a template, then nobody should be
22374 providing us with a dependent type. */
22376 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
22380 /* If the type is NULL, we have not computed a type for the entity
22381 in question; in that case, the type is dependent. */
22385 /* Erroneous types can be considered non-dependent. */
22386 if (type
== error_mark_node
)
22389 /* If we have not already computed the appropriate value for TYPE,
22391 if (!TYPE_DEPENDENT_P_VALID (type
))
22393 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
22394 TYPE_DEPENDENT_P_VALID (type
) = 1;
22397 return TYPE_DEPENDENT_P (type
);
22400 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22401 lookup. In other words, a dependent type that is not the current
22405 dependent_scope_p (tree scope
)
22407 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
22408 && !currently_open_class (scope
));
22411 /* T is a SCOPE_REF; return whether we need to consider it
22412 instantiation-dependent so that we can check access at instantiation
22413 time even though we know which member it resolves to. */
22416 instantiation_dependent_scope_ref_p (tree t
)
22418 if (DECL_P (TREE_OPERAND (t
, 1))
22419 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
22420 && accessible_in_template_p (TREE_OPERAND (t
, 0),
22421 TREE_OPERAND (t
, 1)))
22427 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22428 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22431 /* Note that this predicate is not appropriate for general expressions;
22432 only constant expressions (that satisfy potential_constant_expression)
22433 can be tested for value dependence. */
22436 value_dependent_expression_p (tree expression
)
22438 if (!processing_template_decl
)
22441 /* A name declared with a dependent type. */
22442 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
22445 switch (TREE_CODE (expression
))
22447 case IDENTIFIER_NODE
:
22448 /* A name that has not been looked up -- must be dependent. */
22451 case TEMPLATE_PARM_INDEX
:
22452 /* A non-type template parm. */
22456 /* A non-type template parm. */
22457 if (DECL_TEMPLATE_PARM_P (expression
))
22459 return value_dependent_expression_p (DECL_INITIAL (expression
));
22462 /* A constant with literal type and is initialized
22463 with an expression that is value-dependent.
22465 Note that a non-dependent parenthesized initializer will have
22466 already been replaced with its constant value, so if we see
22467 a TREE_LIST it must be dependent. */
22468 if (DECL_INITIAL (expression
)
22469 && decl_constant_var_p (expression
)
22470 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
22471 /* cp_finish_decl doesn't fold reference initializers. */
22472 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
22473 || value_dependent_expression_p (DECL_INITIAL (expression
))))
22477 case DYNAMIC_CAST_EXPR
:
22478 case STATIC_CAST_EXPR
:
22479 case CONST_CAST_EXPR
:
22480 case REINTERPRET_CAST_EXPR
:
22482 /* These expressions are value-dependent if the type to which
22483 the cast occurs is dependent or the expression being casted
22484 is value-dependent. */
22486 tree type
= TREE_TYPE (expression
);
22488 if (dependent_type_p (type
))
22491 /* A functional cast has a list of operands. */
22492 expression
= TREE_OPERAND (expression
, 0);
22495 /* If there are no operands, it must be an expression such
22496 as "int()". This should not happen for aggregate types
22497 because it would form non-constant expressions. */
22498 gcc_assert (cxx_dialect
>= cxx11
22499 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
22504 if (TREE_CODE (expression
) == TREE_LIST
)
22505 return any_value_dependent_elements_p (expression
);
22507 return value_dependent_expression_p (expression
);
22511 if (SIZEOF_EXPR_TYPE_P (expression
))
22512 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
22516 /* A `sizeof' expression is value-dependent if the operand is
22517 type-dependent or is a pack expansion. */
22518 expression
= TREE_OPERAND (expression
, 0);
22519 if (PACK_EXPANSION_P (expression
))
22521 else if (TYPE_P (expression
))
22522 return dependent_type_p (expression
);
22523 return instantiation_dependent_expression_p (expression
);
22525 case AT_ENCODE_EXPR
:
22526 /* An 'encode' expression is value-dependent if the operand is
22528 expression
= TREE_OPERAND (expression
, 0);
22529 return dependent_type_p (expression
);
22531 case NOEXCEPT_EXPR
:
22532 expression
= TREE_OPERAND (expression
, 0);
22533 return instantiation_dependent_expression_p (expression
);
22536 /* All instantiation-dependent expressions should also be considered
22537 value-dependent. */
22538 return instantiation_dependent_scope_ref_p (expression
);
22540 case COMPONENT_REF
:
22541 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
22542 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
22544 case NONTYPE_ARGUMENT_PACK
:
22545 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22546 is value-dependent. */
22548 tree values
= ARGUMENT_PACK_ARGS (expression
);
22549 int i
, len
= TREE_VEC_LENGTH (values
);
22551 for (i
= 0; i
< len
; ++i
)
22552 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
22560 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
22561 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
22562 || (type2
? dependent_type_p (type2
) : false));
22566 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22567 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
22570 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22571 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
22575 tree op
= TREE_OPERAND (expression
, 0);
22576 return (value_dependent_expression_p (op
)
22577 || has_value_dependent_address (op
));
22580 case REQUIRES_EXPR
:
22581 /* Treat all requires-expressions as value-dependent so
22582 we don't try to fold them. */
22586 return dependent_type_p (TREE_OPERAND (expression
, 0));
22590 tree fn
= get_callee_fndecl (expression
);
22592 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
22594 nargs
= call_expr_nargs (expression
);
22595 for (i
= 0; i
< nargs
; ++i
)
22597 tree op
= CALL_EXPR_ARG (expression
, i
);
22598 /* In a call to a constexpr member function, look through the
22599 implicit ADDR_EXPR on the object argument so that it doesn't
22600 cause the call to be considered value-dependent. We also
22601 look through it in potential_constant_expression. */
22602 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
22603 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
22604 && TREE_CODE (op
) == ADDR_EXPR
)
22605 op
= TREE_OPERAND (op
, 0);
22606 if (value_dependent_expression_p (op
))
22612 case TEMPLATE_ID_EXPR
:
22613 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22615 return type_dependent_expression_p (expression
)
22616 || variable_concept_p (TREE_OPERAND (expression
, 0));
22622 if (dependent_type_p (TREE_TYPE (expression
)))
22624 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
22625 if (value_dependent_expression_p (val
))
22631 /* Treat a GNU statement expression as dependent to avoid crashing
22632 under instantiate_non_dependent_expr; it can't be constant. */
22636 /* A constant expression is value-dependent if any subexpression is
22637 value-dependent. */
22638 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
22640 case tcc_reference
:
22642 case tcc_comparison
:
22644 case tcc_expression
:
22647 int i
, len
= cp_tree_operand_length (expression
);
22649 for (i
= 0; i
< len
; i
++)
22651 tree t
= TREE_OPERAND (expression
, i
);
22653 /* In some cases, some of the operands may be missing.l
22654 (For example, in the case of PREDECREMENT_EXPR, the
22655 amount to increment by may be missing.) That doesn't
22656 make the expression dependent. */
22657 if (t
&& value_dependent_expression_p (t
))
22668 /* The expression is not value-dependent. */
22672 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22673 [temp.dep.expr]. Note that an expression with no type is
22674 considered dependent. Other parts of the compiler arrange for an
22675 expression with type-dependent subexpressions to have no type, so
22676 this function doesn't have to be fully recursive. */
22679 type_dependent_expression_p (tree expression
)
22681 if (!processing_template_decl
)
22684 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
22687 /* An unresolved name is always dependent. */
22688 if (identifier_p (expression
)
22689 || TREE_CODE (expression
) == USING_DECL
22690 || TREE_CODE (expression
) == WILDCARD_DECL
)
22693 /* A fold expression is type-dependent. */
22694 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
22695 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
22696 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
22697 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
22700 /* Some expression forms are never type-dependent. */
22701 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
22702 || TREE_CODE (expression
) == SIZEOF_EXPR
22703 || TREE_CODE (expression
) == ALIGNOF_EXPR
22704 || TREE_CODE (expression
) == AT_ENCODE_EXPR
22705 || TREE_CODE (expression
) == NOEXCEPT_EXPR
22706 || TREE_CODE (expression
) == TRAIT_EXPR
22707 || TREE_CODE (expression
) == TYPEID_EXPR
22708 || TREE_CODE (expression
) == DELETE_EXPR
22709 || TREE_CODE (expression
) == VEC_DELETE_EXPR
22710 || TREE_CODE (expression
) == THROW_EXPR
22711 || TREE_CODE (expression
) == REQUIRES_EXPR
)
22714 /* The types of these expressions depends only on the type to which
22715 the cast occurs. */
22716 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
22717 || TREE_CODE (expression
) == STATIC_CAST_EXPR
22718 || TREE_CODE (expression
) == CONST_CAST_EXPR
22719 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
22720 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
22721 || TREE_CODE (expression
) == CAST_EXPR
)
22722 return dependent_type_p (TREE_TYPE (expression
));
22724 /* The types of these expressions depends only on the type created
22725 by the expression. */
22726 if (TREE_CODE (expression
) == NEW_EXPR
22727 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
22729 /* For NEW_EXPR tree nodes created inside a template, either
22730 the object type itself or a TREE_LIST may appear as the
22732 tree type
= TREE_OPERAND (expression
, 1);
22733 if (TREE_CODE (type
) == TREE_LIST
)
22734 /* This is an array type. We need to check array dimensions
22736 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
22737 || value_dependent_expression_p
22738 (TREE_OPERAND (TREE_VALUE (type
), 1));
22740 return dependent_type_p (type
);
22743 if (TREE_CODE (expression
) == SCOPE_REF
)
22745 tree scope
= TREE_OPERAND (expression
, 0);
22746 tree name
= TREE_OPERAND (expression
, 1);
22748 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22749 contains an identifier associated by name lookup with one or more
22750 declarations declared with a dependent type, or...a
22751 nested-name-specifier or qualified-id that names a member of an
22752 unknown specialization. */
22753 return (type_dependent_expression_p (name
)
22754 || dependent_scope_p (scope
));
22757 if (TREE_CODE (expression
) == FUNCTION_DECL
22758 && DECL_LANG_SPECIFIC (expression
)
22759 && DECL_TEMPLATE_INFO (expression
)
22760 && (any_dependent_template_arguments_p
22761 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
22764 if (TREE_CODE (expression
) == TEMPLATE_DECL
22765 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
22768 if (TREE_CODE (expression
) == STMT_EXPR
)
22769 expression
= stmt_expr_value_expr (expression
);
22771 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
22776 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
22778 if (type_dependent_expression_p (elt
))
22784 /* A static data member of the current instantiation with incomplete
22785 array type is type-dependent, as the definition and specializations
22786 can have different bounds. */
22787 if (VAR_P (expression
)
22788 && DECL_CLASS_SCOPE_P (expression
)
22789 && dependent_type_p (DECL_CONTEXT (expression
))
22790 && VAR_HAD_UNKNOWN_BOUND (expression
))
22793 /* An array of unknown bound depending on a variadic parameter, eg:
22795 template<typename... Args>
22796 void foo (Args... args)
22798 int arr[] = { args... };
22801 template<int... vals>
22804 int arr[] = { vals... };
22807 If the array has no length and has an initializer, it must be that
22808 we couldn't determine its length in cp_complete_array_type because
22809 it is dependent. */
22810 if (VAR_P (expression
)
22811 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
22812 && !TYPE_DOMAIN (TREE_TYPE (expression
))
22813 && DECL_INITIAL (expression
))
22816 /* A variable template specialization is type-dependent if it has any
22817 dependent template arguments. */
22818 if (VAR_P (expression
)
22819 && DECL_LANG_SPECIFIC (expression
)
22820 && DECL_TEMPLATE_INFO (expression
)
22821 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
22822 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
22824 /* Always dependent, on the number of arguments if nothing else. */
22825 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
22828 if (TREE_TYPE (expression
) == unknown_type_node
)
22830 if (TREE_CODE (expression
) == ADDR_EXPR
)
22831 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
22832 if (TREE_CODE (expression
) == COMPONENT_REF
22833 || TREE_CODE (expression
) == OFFSET_REF
)
22835 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22837 expression
= TREE_OPERAND (expression
, 1);
22838 if (identifier_p (expression
))
22841 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22842 if (TREE_CODE (expression
) == SCOPE_REF
)
22845 if (BASELINK_P (expression
))
22847 if (BASELINK_OPTYPE (expression
)
22848 && dependent_type_p (BASELINK_OPTYPE (expression
)))
22850 expression
= BASELINK_FUNCTIONS (expression
);
22853 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
22855 if (any_dependent_template_arguments_p
22856 (TREE_OPERAND (expression
, 1)))
22858 expression
= TREE_OPERAND (expression
, 0);
22859 if (identifier_p (expression
))
22863 gcc_assert (TREE_CODE (expression
) == OVERLOAD
22864 || TREE_CODE (expression
) == FUNCTION_DECL
);
22868 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
22870 expression
= OVL_NEXT (expression
);
22875 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
22877 return (dependent_type_p (TREE_TYPE (expression
)));
22880 /* walk_tree callback function for instantiation_dependent_expression_p,
22881 below. Returns non-zero if a dependent subexpression is found. */
22884 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
22889 /* We don't have to worry about decltype currently because decltype
22890 of an instantiation-dependent expr is a dependent type. This
22891 might change depending on the resolution of DR 1172. */
22892 *walk_subtrees
= false;
22895 enum tree_code code
= TREE_CODE (*tp
);
22898 /* Don't treat an argument list as dependent just because it has no
22906 /* A constant with a dependent initializer is dependent. */
22907 if (value_dependent_expression_p (*tp
))
22911 case TEMPLATE_PARM_INDEX
:
22914 /* Handle expressions with type operands. */
22918 case AT_ENCODE_EXPR
:
22920 tree op
= TREE_OPERAND (*tp
, 0);
22921 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
22922 op
= TREE_TYPE (op
);
22925 if (dependent_type_p (op
))
22929 *walk_subtrees
= false;
22937 if (value_dependent_expression_p (*tp
))
22939 *walk_subtrees
= false;
22942 case COMPONENT_REF
:
22943 if (identifier_p (TREE_OPERAND (*tp
, 1)))
22944 /* In a template, finish_class_member_access_expr creates a
22945 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22946 type-dependent, so that we can check access control at
22947 instantiation time (PR 42277). See also Core issue 1273. */
22952 if (instantiation_dependent_scope_ref_p (*tp
))
22957 /* Treat statement-expressions as dependent. */
22961 /* Treat requires-expressions as dependent. */
22962 case REQUIRES_EXPR
:
22966 /* Treat calls to function concepts as dependent. */
22967 if (function_concept_check_p (*tp
))
22971 case TEMPLATE_ID_EXPR
:
22972 /* And variable concepts. */
22973 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
22981 if (type_dependent_expression_p (*tp
))
22987 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22988 sense defined by the ABI:
22990 "An expression is instantiation-dependent if it is type-dependent
22991 or value-dependent, or it has a subexpression that is type-dependent
22992 or value-dependent." */
22995 instantiation_dependent_expression_p (tree expression
)
22999 if (!processing_template_decl
)
23002 if (expression
== error_mark_node
)
23005 result
= cp_walk_tree_without_duplicates (&expression
,
23006 instantiation_dependent_r
, NULL
);
23007 return result
!= NULL_TREE
;
23010 /* Like type_dependent_expression_p, but it also works while not processing
23011 a template definition, i.e. during substitution or mangling. */
23014 type_dependent_expression_p_push (tree expr
)
23017 ++processing_template_decl
;
23018 b
= type_dependent_expression_p (expr
);
23019 --processing_template_decl
;
23023 /* Returns TRUE if ARGS contains a type-dependent expression. */
23026 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
23031 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
23033 if (type_dependent_expression_p (arg
))
23039 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23040 expressions) contains any type-dependent expressions. */
23043 any_type_dependent_elements_p (const_tree list
)
23045 for (; list
; list
= TREE_CHAIN (list
))
23046 if (type_dependent_expression_p (TREE_VALUE (list
)))
23052 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23053 expressions) contains any value-dependent expressions. */
23056 any_value_dependent_elements_p (const_tree list
)
23058 for (; list
; list
= TREE_CHAIN (list
))
23059 if (value_dependent_expression_p (TREE_VALUE (list
)))
23065 /* Returns TRUE if the ARG (a template argument) is dependent. */
23068 dependent_template_arg_p (tree arg
)
23070 if (!processing_template_decl
)
23073 /* Assume a template argument that was wrongly written by the user
23074 is dependent. This is consistent with what
23075 any_dependent_template_arguments_p [that calls this function]
23077 if (!arg
|| arg
== error_mark_node
)
23080 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
23081 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
23083 if (TREE_CODE (arg
) == TEMPLATE_DECL
23084 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
23085 return dependent_template_p (arg
);
23086 else if (ARGUMENT_PACK_P (arg
))
23088 tree args
= ARGUMENT_PACK_ARGS (arg
);
23089 int i
, len
= TREE_VEC_LENGTH (args
);
23090 for (i
= 0; i
< len
; ++i
)
23092 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23098 else if (TYPE_P (arg
))
23099 return dependent_type_p (arg
);
23101 return (type_dependent_expression_p (arg
)
23102 || value_dependent_expression_p (arg
));
23105 /* Returns true if ARGS (a collection of template arguments) contains
23106 any types that require structural equality testing. */
23109 any_template_arguments_need_structural_equality_p (tree args
)
23116 if (args
== error_mark_node
)
23119 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23121 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23122 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23124 tree arg
= TREE_VEC_ELT (level
, j
);
23125 tree packed_args
= NULL_TREE
;
23128 if (ARGUMENT_PACK_P (arg
))
23130 /* Look inside the argument pack. */
23131 packed_args
= ARGUMENT_PACK_ARGS (arg
);
23132 len
= TREE_VEC_LENGTH (packed_args
);
23135 for (k
= 0; k
< len
; ++k
)
23138 arg
= TREE_VEC_ELT (packed_args
, k
);
23140 if (error_operand_p (arg
))
23142 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23144 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
23146 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
23147 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
23156 /* Returns true if ARGS (a collection of template arguments) contains
23157 any dependent arguments. */
23160 any_dependent_template_arguments_p (const_tree args
)
23167 if (args
== error_mark_node
)
23170 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23172 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23173 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23174 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
23181 /* Returns TRUE if the template TMPL is dependent. */
23184 dependent_template_p (tree tmpl
)
23186 if (TREE_CODE (tmpl
) == OVERLOAD
)
23190 if (dependent_template_p (OVL_CURRENT (tmpl
)))
23192 tmpl
= OVL_NEXT (tmpl
);
23197 /* Template template parameters are dependent. */
23198 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
23199 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
23201 /* So are names that have not been looked up. */
23202 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
23204 /* So are member templates of dependent classes. */
23205 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
23206 return dependent_type_p (DECL_CONTEXT (tmpl
));
23210 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23213 dependent_template_id_p (tree tmpl
, tree args
)
23215 return (dependent_template_p (tmpl
)
23216 || any_dependent_template_arguments_p (args
));
23219 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23223 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
23227 if (!processing_template_decl
)
23230 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
23232 tree decl
= TREE_VEC_ELT (declv
, i
);
23233 tree init
= TREE_VEC_ELT (initv
, i
);
23234 tree cond
= TREE_VEC_ELT (condv
, i
);
23235 tree incr
= TREE_VEC_ELT (incrv
, i
);
23237 if (type_dependent_expression_p (decl
)
23238 || TREE_CODE (decl
) == SCOPE_REF
)
23241 if (init
&& type_dependent_expression_p (init
))
23244 if (type_dependent_expression_p (cond
))
23247 if (COMPARISON_CLASS_P (cond
)
23248 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
23249 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
23252 if (TREE_CODE (incr
) == MODOP_EXPR
)
23254 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
23255 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
23258 else if (type_dependent_expression_p (incr
))
23260 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
23262 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
23264 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
23266 tree t
= TREE_OPERAND (incr
, 1);
23267 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
23268 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
23277 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23278 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23279 no such TYPE can be found. Note that this function peers inside
23280 uninstantiated templates and therefore should be used only in
23281 extremely limited situations. ONLY_CURRENT_P restricts this
23282 peering to the currently open classes hierarchy (which is required
23283 when comparing types). */
23286 resolve_typename_type (tree type
, bool only_current_p
)
23295 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
23297 scope
= TYPE_CONTEXT (type
);
23298 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23299 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23300 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23301 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23302 identifier of the TYPENAME_TYPE anymore.
23303 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23304 TYPENAME_TYPE instead, we avoid messing up with a possible
23305 typedef variant case. */
23306 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
23308 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23309 it first before we can figure out what NAME refers to. */
23310 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23312 if (TYPENAME_IS_RESOLVING_P (scope
))
23313 /* Given a class template A with a dependent base with nested type C,
23314 typedef typename A::C::C C will land us here, as trying to resolve
23315 the initial A::C leads to the local C typedef, which leads back to
23316 A::C::C. So we break the recursion now. */
23319 scope
= resolve_typename_type (scope
, only_current_p
);
23321 /* If we don't know what SCOPE refers to, then we cannot resolve the
23323 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23325 /* If the SCOPE is a template type parameter, we have no way of
23326 resolving the name. */
23327 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
23329 /* If the SCOPE is not the current instantiation, there's no reason
23330 to look inside it. */
23331 if (only_current_p
&& !currently_open_class (scope
))
23333 /* If this is a typedef, we don't want to look inside (c++/11987). */
23334 if (typedef_variant_p (type
))
23336 /* If SCOPE isn't the template itself, it will not have a valid
23337 TYPE_FIELDS list. */
23338 if (CLASS_TYPE_P (scope
)
23339 && same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
23340 /* scope is either the template itself or a compatible instantiation
23341 like X<T>, so look up the name in the original template. */
23342 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
23344 /* scope is a partial instantiation, so we can't do the lookup or we
23345 will lose the template arguments. */
23347 /* Enter the SCOPE so that name lookup will be resolved as if we
23348 were in the class definition. In particular, SCOPE will no
23349 longer be considered a dependent type. */
23350 pushed_scope
= push_scope (scope
);
23351 /* Look up the declaration. */
23352 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
23353 tf_warning_or_error
);
23355 result
= NULL_TREE
;
23357 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23358 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23361 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
23362 && TREE_CODE (decl
) == TYPE_DECL
)
23364 result
= TREE_TYPE (decl
);
23365 if (result
== error_mark_node
)
23366 result
= NULL_TREE
;
23368 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
23369 && DECL_CLASS_TEMPLATE_P (decl
))
23373 /* Obtain the template and the arguments. */
23374 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
23375 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
23376 /* Instantiate the template. */
23377 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
23378 /*entering_scope=*/0,
23379 tf_error
| tf_user
);
23380 if (result
== error_mark_node
)
23381 result
= NULL_TREE
;
23384 /* Leave the SCOPE. */
23386 pop_scope (pushed_scope
);
23388 /* If we failed to resolve it, return the original typename. */
23392 /* If lookup found a typename type, resolve that too. */
23393 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
23395 /* Ill-formed programs can cause infinite recursion here, so we
23396 must catch that. */
23397 TYPENAME_IS_RESOLVING_P (type
) = 1;
23398 result
= resolve_typename_type (result
, only_current_p
);
23399 TYPENAME_IS_RESOLVING_P (type
) = 0;
23402 /* Qualify the resulting type. */
23403 quals
= cp_type_quals (type
);
23405 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
23410 /* EXPR is an expression which is not type-dependent. Return a proxy
23411 for EXPR that can be used to compute the types of larger
23412 expressions containing EXPR. */
23415 build_non_dependent_expr (tree expr
)
23419 /* Try to get a constant value for all non-dependent expressions in
23420 order to expose bugs in *_dependent_expression_p and constexpr. */
23421 if (flag_checking
&& cxx_dialect
>= cxx11
)
23422 fold_non_dependent_expr (expr
);
23424 /* Preserve OVERLOADs; the functions must be available to resolve
23427 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
23428 inner_expr
= stmt_expr_value_expr (inner_expr
);
23429 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
23430 inner_expr
= TREE_OPERAND (inner_expr
, 0);
23431 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
23432 inner_expr
= TREE_OPERAND (inner_expr
, 1);
23433 if (is_overloaded_fn (inner_expr
)
23434 || TREE_CODE (inner_expr
) == OFFSET_REF
)
23436 /* There is no need to return a proxy for a variable. */
23439 /* Preserve string constants; conversions from string constants to
23440 "char *" are allowed, even though normally a "const char *"
23441 cannot be used to initialize a "char *". */
23442 if (TREE_CODE (expr
) == STRING_CST
)
23444 /* Preserve void and arithmetic constants, as an optimization -- there is no
23445 reason to create a new node. */
23446 if (TREE_CODE (expr
) == VOID_CST
23447 || TREE_CODE (expr
) == INTEGER_CST
23448 || TREE_CODE (expr
) == REAL_CST
)
23450 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23451 There is at least one place where we want to know that a
23452 particular expression is a throw-expression: when checking a ?:
23453 expression, there are special rules if the second or third
23454 argument is a throw-expression. */
23455 if (TREE_CODE (expr
) == THROW_EXPR
)
23458 /* Don't wrap an initializer list, we need to be able to look inside. */
23459 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
23462 /* Don't wrap a dummy object, we need to be able to test for it. */
23463 if (is_dummy_object (expr
))
23466 if (TREE_CODE (expr
) == COND_EXPR
)
23467 return build3 (COND_EXPR
,
23469 TREE_OPERAND (expr
, 0),
23470 (TREE_OPERAND (expr
, 1)
23471 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
23472 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
23473 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
23474 if (TREE_CODE (expr
) == COMPOUND_EXPR
23475 && !COMPOUND_EXPR_OVERLOADED (expr
))
23476 return build2 (COMPOUND_EXPR
,
23478 TREE_OPERAND (expr
, 0),
23479 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
23481 /* If the type is unknown, it can't really be non-dependent */
23482 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
23484 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23485 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
23488 /* ARGS is a vector of expressions as arguments to a function call.
23489 Replace the arguments with equivalent non-dependent expressions.
23490 This modifies ARGS in place. */
23493 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
23498 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
23500 tree newarg
= build_non_dependent_expr (arg
);
23502 (*args
)[ix
] = newarg
;
23506 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23507 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23508 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23511 make_auto_1 (tree name
, bool set_canonical
)
23513 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
23514 TYPE_NAME (au
) = build_decl (input_location
,
23515 TYPE_DECL
, name
, au
);
23516 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
23517 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
23518 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
23519 TYPE_NAME (au
), NULL_TREE
);
23521 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
23522 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
23523 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
23529 make_decltype_auto (void)
23531 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23537 return make_auto_1 (get_identifier ("auto"), true);
23540 /* Make a "constrained auto" type-specifier. This is an
23541 auto type with constraints that must be associated after
23542 deduction. The constraint is formed from the given
23543 CONC and its optional sequence of arguments, which are
23544 non-null if written as partial-concept-id. */
23547 make_constrained_auto (tree con
, tree args
)
23549 tree type
= make_auto_1 (get_identifier ("auto"), false);
23551 /* Build the constraint. */
23552 tree tmpl
= DECL_TI_TEMPLATE (con
);
23555 expr
= build_concept_check (tmpl
, type
, args
);
23557 expr
= build_concept_check (build_overload (tmpl
, NULL_TREE
), type
, args
);
23559 tree constr
= make_predicate_constraint (expr
);
23560 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
23562 /* Our canonical type depends on the constraint. */
23563 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
23565 /* Attach the constraint to the type declaration. */
23566 tree decl
= TYPE_NAME (type
);
23570 /* Given type ARG, return std::initializer_list<ARG>. */
23575 tree std_init_list
= namespace_binding
23576 (get_identifier ("initializer_list"), std_node
);
23578 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
23580 error ("deducing from brace-enclosed initializer list requires "
23581 "#include <initializer_list>");
23582 return error_mark_node
;
23584 argvec
= make_tree_vec (1);
23585 TREE_VEC_ELT (argvec
, 0) = arg
;
23586 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
23587 NULL_TREE
, 0, tf_warning_or_error
);
23590 /* Replace auto in TYPE with std::initializer_list<auto>. */
23593 listify_autos (tree type
, tree auto_node
)
23595 tree init_auto
= listify (auto_node
);
23596 tree argvec
= make_tree_vec (1);
23597 TREE_VEC_ELT (argvec
, 0) = init_auto
;
23598 if (processing_template_decl
)
23599 argvec
= add_to_template_args (current_template_args (), argvec
);
23600 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
23603 /* Hash traits for hashing possibly constrained 'auto'
23604 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23606 struct auto_hash
: default_hash_traits
<tree
>
23608 static inline hashval_t
hash (tree
);
23609 static inline bool equal (tree
, tree
);
23612 /* Hash the 'auto' T. */
23615 auto_hash::hash (tree t
)
23617 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
23618 /* Matching constrained-type-specifiers denote the same template
23619 parameter, so hash the constraint. */
23620 return hash_placeholder_constraint (c
);
23622 /* But unconstrained autos are all separate, so just hash the pointer. */
23623 return iterative_hash_object (t
, 0);
23626 /* Compare two 'auto's. */
23629 auto_hash::equal (tree t1
, tree t2
)
23634 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
23635 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
23637 /* Two unconstrained autos are distinct. */
23641 return equivalent_placeholder_constraints (c1
, c2
);
23644 /* for_each_template_parm callback for extract_autos: if t is a (possibly
23645 constrained) auto, add it to the vector. */
23648 extract_autos_r (tree t
, void *data
)
23650 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
23651 if (is_auto_or_concept (t
))
23653 /* All the autos were built with index 0; fix that up now. */
23654 tree
*p
= hash
.find_slot (t
, INSERT
);
23657 /* If this is a repeated constrained-type-specifier, use the index we
23659 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
23662 /* Otherwise this is new, so use the current count. */
23664 idx
= hash
.elements () - 1;
23666 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
23669 /* Always keep walking. */
23673 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
23674 says they can appear anywhere in the type. */
23677 extract_autos (tree type
)
23679 hash_set
<tree
> visited
;
23680 hash_table
<auto_hash
> hash (2);
23682 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
23684 tree tree_vec
= make_tree_vec (hash
.elements());
23685 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
23686 iter
!= hash
.end(); ++iter
)
23689 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
23690 TREE_VEC_ELT (tree_vec
, i
)
23691 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
23697 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23698 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23701 do_auto_deduction (tree type
, tree init
, tree auto_node
)
23703 return do_auto_deduction (type
, init
, auto_node
,
23704 tf_warning_or_error
,
23708 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23709 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23710 The CONTEXT determines the context in which auto deduction is performed
23711 and is used to control error diagnostics. */
23714 do_auto_deduction (tree type
, tree init
, tree auto_node
,
23715 tsubst_flags_t complain
, auto_deduction_context context
)
23719 if (init
== error_mark_node
)
23720 return error_mark_node
;
23722 if (type_dependent_expression_p (init
))
23723 /* Defining a subset of type-dependent expressions that we can deduce
23724 from ahead of time isn't worth the trouble. */
23727 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23728 with either a new invented type template parameter U or, if the
23729 initializer is a braced-init-list (8.5.4), with
23730 std::initializer_list<U>. */
23731 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
23733 if (!DIRECT_LIST_INIT_P (init
))
23734 type
= listify_autos (type
, auto_node
);
23735 else if (CONSTRUCTOR_NELTS (init
) == 1)
23736 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
23739 if (complain
& tf_warning_or_error
)
23741 if (permerror (input_location
, "direct-list-initialization of "
23742 "%<auto%> requires exactly one element"))
23743 inform (input_location
,
23744 "for deduction to %<std::initializer_list%>, use copy-"
23745 "list-initialization (i.e. add %<=%> before the %<{%>)");
23747 type
= listify_autos (type
, auto_node
);
23751 if (type
== error_mark_node
)
23752 return error_mark_node
;
23754 init
= resolve_nondeduced_context (init
);
23756 if (AUTO_IS_DECLTYPE (auto_node
))
23758 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
23759 && !REF_PARENTHESIZED_P (init
)));
23760 targs
= make_tree_vec (1);
23761 TREE_VEC_ELT (targs
, 0)
23762 = finish_decltype_type (init
, id
, tf_warning_or_error
);
23763 if (type
!= auto_node
)
23765 if (complain
& tf_error
)
23766 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
23767 return error_mark_node
;
23772 tree parms
= build_tree_list (NULL_TREE
, type
);
23776 tparms
= extract_autos (type
);
23779 tparms
= make_tree_vec (1);
23780 TREE_VEC_ELT (tparms
, 0)
23781 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
23784 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
23785 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
23786 DEDUCE_CALL
, LOOKUP_NORMAL
,
23787 NULL
, /*explain_p=*/false);
23790 if (processing_template_decl
)
23791 /* Try again at instantiation time. */
23793 if (type
&& type
!= error_mark_node
23794 && (complain
& tf_error
))
23795 /* If type is error_mark_node a diagnostic must have been
23796 emitted by now. Also, having a mention to '<type error>'
23797 in the diagnostic is not really useful to the user. */
23799 if (cfun
&& auto_node
== current_function_auto_return_pattern
23800 && LAMBDA_FUNCTION_P (current_function_decl
))
23801 error ("unable to deduce lambda return type from %qE", init
);
23803 error ("unable to deduce %qT from %qE", type
, init
);
23804 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
23805 DEDUCE_CALL
, LOOKUP_NORMAL
,
23806 NULL
, /*explain_p=*/true);
23808 return error_mark_node
;
23812 /* Check any placeholder constraints against the deduced type. */
23813 if (flag_concepts
&& !processing_template_decl
)
23814 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
23816 /* Use the deduced type to check the associated constraints. */
23817 if (!constraints_satisfied_p (constr
, targs
))
23819 if (complain
& tf_warning_or_error
)
23823 case adc_unspecified
:
23824 error("placeholder constraints not satisfied");
23826 case adc_variable_type
:
23827 error ("deduced initializer does not satisfy "
23828 "placeholder constraints");
23830 case adc_return_type
:
23831 error ("deduced return type does not satisfy "
23832 "placeholder constraints");
23834 case adc_requirement
:
23835 error ("deduced expression type does not saatisy "
23836 "placeholder constraints");
23839 diagnose_constraints (input_location
, constr
, targs
);
23841 return error_mark_node
;
23845 if (processing_template_decl
)
23846 targs
= add_to_template_args (current_template_args (), targs
);
23847 return tsubst (type
, targs
, complain
, NULL_TREE
);
23850 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23854 splice_late_return_type (tree type
, tree late_return_type
)
23856 if (is_auto (type
))
23858 if (late_return_type
)
23859 return late_return_type
;
23861 tree idx
= get_template_parm_index (type
);
23862 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
23863 /* In an abbreviated function template we didn't know we were dealing
23864 with a function template when we saw the auto return type, so update
23865 it to have the correct level. */
23866 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
23871 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23872 'decltype(auto)'. */
23875 is_auto (const_tree type
)
23877 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23878 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
23879 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
23885 /* for_each_template_parm callback for type_uses_auto. */
23888 is_auto_r (tree tp
, void */
*data*/
)
23890 return is_auto_or_concept (tp
);
23893 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23894 a use of `auto'. Returns NULL_TREE otherwise. */
23897 type_uses_auto (tree type
)
23899 if (type
== NULL_TREE
)
23901 else if (flag_concepts
)
23903 /* The Concepts TS allows multiple autos in one type-specifier; just
23904 return the first one we find, do_auto_deduction will collect all of
23906 if (uses_template_parms (type
))
23907 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
23908 /*visited*/NULL
, /*nondeduced*/true);
23913 return find_type_usage (type
, is_auto
);
23916 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23917 'decltype(auto)' or a concept. */
23920 is_auto_or_concept (const_tree type
)
23922 return is_auto (type
); // or concept
23925 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23926 a concept identifier) iff TYPE contains a use of a generic type. Returns
23927 NULL_TREE otherwise. */
23930 type_uses_auto_or_concept (tree type
)
23932 return find_type_usage (type
, is_auto_or_concept
);
23936 /* For a given template T, return the vector of typedefs referenced
23937 in T for which access check is needed at T instantiation time.
23938 T is either a FUNCTION_DECL or a RECORD_TYPE.
23939 Those typedefs were added to T by the function
23940 append_type_to_template_for_access_check. */
23942 vec
<qualified_typedef_usage_t
, va_gc
> *
23943 get_types_needing_access_check (tree t
)
23946 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
23948 if (!t
|| t
== error_mark_node
)
23951 if (!(ti
= get_template_info (t
)))
23954 if (CLASS_TYPE_P (t
)
23955 || TREE_CODE (t
) == FUNCTION_DECL
)
23957 if (!TI_TEMPLATE (ti
))
23960 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
23966 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
23967 tied to T. That list of typedefs will be access checked at
23968 T instantiation time.
23969 T is either a FUNCTION_DECL or a RECORD_TYPE.
23970 TYPE_DECL is a TYPE_DECL node representing a typedef.
23971 SCOPE is the scope through which TYPE_DECL is accessed.
23972 LOCATION is the location of the usage point of TYPE_DECL.
23974 This function is a subroutine of
23975 append_type_to_template_for_access_check. */
23978 append_type_to_template_for_access_check_1 (tree t
,
23981 location_t location
)
23983 qualified_typedef_usage_t typedef_usage
;
23986 if (!t
|| t
== error_mark_node
)
23989 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
23990 || CLASS_TYPE_P (t
))
23992 && TREE_CODE (type_decl
) == TYPE_DECL
23995 if (!(ti
= get_template_info (t
)))
23998 gcc_assert (TI_TEMPLATE (ti
));
24000 typedef_usage
.typedef_decl
= type_decl
;
24001 typedef_usage
.context
= scope
;
24002 typedef_usage
.locus
= location
;
24004 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
24007 /* Append TYPE_DECL to the template TEMPL.
24008 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24009 At TEMPL instanciation time, TYPE_DECL will be checked to see
24010 if it can be accessed through SCOPE.
24011 LOCATION is the location of the usage point of TYPE_DECL.
24013 e.g. consider the following code snippet:
24020 template<class U> struct S
24022 C::myint mi; // <-- usage point of the typedef C::myint
24027 At S<char> instantiation time, we need to check the access of C::myint
24028 In other words, we need to check the access of the myint typedef through
24029 the C scope. For that purpose, this function will add the myint typedef
24030 and the scope C through which its being accessed to a list of typedefs
24031 tied to the template S. That list will be walked at template instantiation
24032 time and access check performed on each typedefs it contains.
24033 Note that this particular code snippet should yield an error because
24034 myint is private to C. */
24037 append_type_to_template_for_access_check (tree templ
,
24040 location_t location
)
24042 qualified_typedef_usage_t
*iter
;
24045 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
24047 /* Make sure we don't append the type to the template twice. */
24048 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
24049 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
24052 append_type_to_template_for_access_check_1 (templ
, type_decl
,
24056 /* Convert the generic type parameters in PARM that match the types given in the
24057 range [START_IDX, END_IDX) from the current_template_parms into generic type
24061 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
24063 tree current
= current_template_parms
;
24064 int depth
= TMPL_PARMS_DEPTH (current
);
24065 current
= INNERMOST_TEMPLATE_PARMS (current
);
24066 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
24068 for (int i
= 0; i
< start_idx
; ++i
)
24069 TREE_VEC_ELT (replacement
, i
)
24070 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24072 for (int i
= start_idx
; i
< end_idx
; ++i
)
24074 /* Create a distinct parameter pack type from the current parm and add it
24075 to the replacement args to tsubst below into the generic function
24078 tree o
= TREE_TYPE (TREE_VALUE
24079 (TREE_VEC_ELT (current
, i
)));
24080 tree t
= copy_type (o
);
24081 TEMPLATE_TYPE_PARM_INDEX (t
)
24082 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
24084 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
24085 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
24086 TYPE_MAIN_VARIANT (t
) = t
;
24087 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
24088 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
24089 TREE_VEC_ELT (replacement
, i
) = t
;
24090 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
24093 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
24094 TREE_VEC_ELT (replacement
, i
)
24095 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24097 /* If there are more levels then build up the replacement with the outer
24100 replacement
= add_to_template_args (template_parms_to_args
24101 (TREE_CHAIN (current_template_parms
)),
24104 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
24107 /* Entries in the decl_constraint hash table. */
24108 struct GTY((for_user
)) constr_entry
24114 /* Hashing function and equality for constraint entries. */
24115 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
24117 static hashval_t
hash (constr_entry
*e
)
24119 return (hashval_t
)DECL_UID (e
->decl
);
24122 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
24124 return e1
->decl
== e2
->decl
;
24128 /* A mapping from declarations to constraint information. Note that
24129 both templates and their underlying declarations are mapped to the
24130 same constraint information.
24132 FIXME: This is defined in pt.c because garbage collection
24133 code is not being generated for constraint.cc. */
24135 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
24137 /* Returns true iff cinfo contains a valid set of constraints.
24138 This is the case when the associated requirements have been
24139 successfully decomposed into lists of atomic constraints.
24140 That is, when the saved assumptions are not error_mark_node. */
24143 valid_constraints_p (tree cinfo
)
24145 gcc_assert (cinfo
);
24146 return CI_ASSUMPTIONS (cinfo
) != error_mark_node
;
24149 /* Returns the template constraints of declaration T. If T is not
24150 constrained, return NULL_TREE. Note that T must be non-null. */
24153 get_constraints (tree t
)
24155 gcc_assert (DECL_P (t
));
24156 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24157 t
= DECL_TEMPLATE_RESULT (t
);
24158 constr_entry elt
= { t
, NULL_TREE
};
24159 constr_entry
* found
= decl_constraints
->find (&elt
);
24166 /* Associate the given constraint information CI with the declaration
24167 T. If T is a template, then the constraints are associated with
24168 its underlying declaration. Don't build associations if CI is
24172 set_constraints (tree t
, tree ci
)
24177 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24178 t
= DECL_TEMPLATE_RESULT (t
);
24179 gcc_assert (!get_constraints (t
));
24180 constr_entry elt
= {t
, ci
};
24181 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
24182 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
24187 /* Remove the associated constraints of the declaration T. */
24190 remove_constraints (tree t
)
24192 gcc_assert (DECL_P (t
));
24193 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24194 t
= DECL_TEMPLATE_RESULT (t
);
24196 constr_entry elt
= {t
, NULL_TREE
};
24197 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
24199 decl_constraints
->clear_slot (slot
);
24202 /* Set up the hash table for constraint association. */
24205 init_constraint_processing (void)
24207 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
24210 /* Set up the hash tables for template instantiations. */
24213 init_template_processing (void)
24215 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24216 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24219 /* Print stats about the template hash tables for -fstats. */
24222 print_template_statistics (void)
24224 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
24225 "%f collisions\n", (long) decl_specializations
->size (),
24226 (long) decl_specializations
->elements (),
24227 decl_specializations
->collisions ());
24228 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
24229 "%f collisions\n", (long) type_specializations
->size (),
24230 (long) type_specializations
->elements (),
24231 type_specializations
->collisions ());
24234 #include "gt-cp-pt.h"