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
);
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
copy_template_args (tree
);
182 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
183 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
184 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
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 void tsubst_default_arguments (tree
, tsubst_flags_t
);
199 static tree
for_each_template_parm_r (tree
*, int *, void *);
200 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
201 static void copy_default_args_to_explicit_spec (tree
);
202 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
203 static bool dependent_template_arg_p (tree
);
204 static bool any_template_arguments_need_structural_equality_p (tree
);
205 static bool dependent_type_p_r (tree
);
206 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
207 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
208 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
209 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
211 static tree
listify (tree
);
212 static tree
listify_autos (tree
, tree
);
213 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
214 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
215 static bool complex_alias_template_p (const_tree tmpl
);
216 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
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
332 || TREE_CODE (t
) == PARM_DECL
)
335 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
336 tinfo
= DECL_TEMPLATE_INFO (t
);
338 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
341 if (OVERLOAD_TYPE_P (t
))
342 tinfo
= TYPE_TEMPLATE_INFO (t
);
343 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
344 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
349 /* Returns the template nesting level of the indicated class TYPE.
359 A<T>::B<U> has depth two, while A<T> has depth one.
360 Both A<T>::B<int> and A<int>::B<U> have depth one, if
361 they are instantiations, not specializations.
363 This function is guaranteed to return 0 if passed NULL_TREE so
364 that, for example, `template_class_depth (current_class_type)' is
368 template_class_depth (tree type
)
372 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
374 tree tinfo
= get_template_info (type
);
376 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
377 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
381 type
= CP_DECL_CONTEXT (type
);
382 else if (LAMBDA_TYPE_P (type
))
383 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
385 type
= CP_TYPE_CONTEXT (type
);
391 /* Subroutine of maybe_begin_member_template_processing.
392 Returns true if processing DECL needs us to push template parms. */
395 inline_needs_template_parms (tree decl
, bool nsdmi
)
397 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
400 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
401 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
404 /* Subroutine of maybe_begin_member_template_processing.
405 Push the template parms in PARMS, starting from LEVELS steps into the
406 chain, and ending at the beginning, since template parms are listed
410 push_inline_template_parms_recursive (tree parmlist
, int levels
)
412 tree parms
= TREE_VALUE (parmlist
);
416 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
418 ++processing_template_decl
;
419 current_template_parms
420 = tree_cons (size_int (processing_template_decl
),
421 parms
, current_template_parms
);
422 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
424 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
426 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
428 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
430 if (error_operand_p (parm
))
433 gcc_assert (DECL_P (parm
));
435 switch (TREE_CODE (parm
))
443 /* Push the CONST_DECL. */
444 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
453 /* Restore the template parameter context for a member template, a
454 friend template defined in a class definition, or a non-template
455 member of template class. */
458 maybe_begin_member_template_processing (tree decl
)
462 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
466 tree ctx
= DECL_CONTEXT (decl
);
467 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
468 /* Disregard full specializations (c++/60999). */
469 && uses_template_parms (ctx
)
470 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
473 if (inline_needs_template_parms (decl
, nsdmi
))
475 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
476 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
478 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
481 parms
= TREE_CHAIN (parms
);
484 push_inline_template_parms_recursive (parms
, levels
);
487 /* Remember how many levels of template parameters we pushed so that
488 we can pop them later. */
489 inline_parm_levels
.safe_push (levels
);
492 /* Undo the effects of maybe_begin_member_template_processing. */
495 maybe_end_member_template_processing (void)
500 if (inline_parm_levels
.length () == 0)
503 last
= inline_parm_levels
.pop ();
504 for (i
= 0; i
< last
; ++i
)
506 --processing_template_decl
;
507 current_template_parms
= TREE_CHAIN (current_template_parms
);
512 /* Return a new template argument vector which contains all of ARGS,
513 but has as its innermost set of arguments the EXTRA_ARGS. */
516 add_to_template_args (tree args
, tree extra_args
)
523 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
526 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
527 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
529 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
530 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
532 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
533 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
538 /* Like add_to_template_args, but only the outermost ARGS are added to
539 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
540 (EXTRA_ARGS) levels are added. This function is used to combine
541 the template arguments from a partial instantiation with the
542 template arguments used to attain the full instantiation from the
543 partial instantiation. */
546 add_outermost_template_args (tree args
, tree extra_args
)
550 /* If there are more levels of EXTRA_ARGS than there are ARGS,
551 something very fishy is going on. */
552 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
554 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
559 /* For the moment, we make ARGS look like it contains fewer levels. */
560 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
562 new_args
= add_to_template_args (args
, extra_args
);
564 /* Now, we restore ARGS to its full dimensions. */
565 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
570 /* Return the N levels of innermost template arguments from the ARGS. */
573 get_innermost_template_args (tree args
, int n
)
581 /* If N is 1, just return the innermost set of template arguments. */
583 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
585 /* If we're not removing anything, just return the arguments we were
587 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
588 gcc_assert (extra_levels
>= 0);
589 if (extra_levels
== 0)
592 /* Make a new set of arguments, not containing the outer arguments. */
593 new_args
= make_tree_vec (n
);
594 for (i
= 1; i
<= n
; ++i
)
595 SET_TMPL_ARGS_LEVEL (new_args
, i
,
596 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
601 /* The inverse of get_innermost_template_args: Return all but the innermost
602 EXTRA_LEVELS levels of template arguments from the ARGS. */
605 strip_innermost_template_args (tree args
, int extra_levels
)
608 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
613 /* If N is 1, just return the outermost set of template arguments. */
615 return TMPL_ARGS_LEVEL (args
, 1);
617 /* If we're not removing anything, just return the arguments we were
619 gcc_assert (extra_levels
>= 0);
620 if (extra_levels
== 0)
623 /* Make a new set of arguments, not containing the inner arguments. */
624 new_args
= make_tree_vec (n
);
625 for (i
= 1; i
<= n
; ++i
)
626 SET_TMPL_ARGS_LEVEL (new_args
, i
,
627 TMPL_ARGS_LEVEL (args
, i
));
632 /* We've got a template header coming up; push to a new level for storing
636 begin_template_parm_list (void)
638 /* We use a non-tag-transparent scope here, which causes pushtag to
639 put tags in this scope, rather than in the enclosing class or
640 namespace scope. This is the right thing, since we want
641 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
642 global template class, push_template_decl handles putting the
643 TEMPLATE_DECL into top-level scope. For a nested template class,
646 template <class T> struct S1 {
647 template <class T> struct S2 {};
650 pushtag contains special code to call pushdecl_with_scope on the
651 TEMPLATE_DECL for S2. */
652 begin_scope (sk_template_parms
, NULL
);
653 ++processing_template_decl
;
654 ++processing_template_parmlist
;
655 note_template_header (0);
657 /* Add a dummy parameter level while we process the parameter list. */
658 current_template_parms
659 = tree_cons (size_int (processing_template_decl
),
661 current_template_parms
);
664 /* This routine is called when a specialization is declared. If it is
665 invalid to declare a specialization here, an error is reported and
666 false is returned, otherwise this routine will return true. */
669 check_specialization_scope (void)
671 tree scope
= current_scope ();
675 An explicit specialization shall be declared in the namespace of
676 which the template is a member, or, for member templates, in the
677 namespace of which the enclosing class or enclosing class
678 template is a member. An explicit specialization of a member
679 function, member class or static data member of a class template
680 shall be declared in the namespace of which the class template
682 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
684 error ("explicit specialization in non-namespace scope %qD", scope
);
690 In an explicit specialization declaration for a member of a class
691 template or a member template that appears in namespace scope,
692 the member template and some of its enclosing class templates may
693 remain unspecialized, except that the declaration shall not
694 explicitly specialize a class member template if its enclosing
695 class templates are not explicitly specialized as well. */
696 if (current_template_parms
)
698 error ("enclosing class templates are not explicitly specialized");
705 /* We've just seen template <>. */
708 begin_specialization (void)
710 begin_scope (sk_template_spec
, NULL
);
711 note_template_header (1);
712 return check_specialization_scope ();
715 /* Called at then end of processing a declaration preceded by
719 end_specialization (void)
722 reset_specialization ();
725 /* Any template <>'s that we have seen thus far are not referring to a
726 function specialization. */
729 reset_specialization (void)
731 processing_specialization
= 0;
732 template_header_count
= 0;
735 /* We've just seen a template header. If SPECIALIZATION is nonzero,
736 it was of the form template <>. */
739 note_template_header (int specialization
)
741 processing_specialization
= specialization
;
742 template_header_count
++;
745 /* We're beginning an explicit instantiation. */
748 begin_explicit_instantiation (void)
750 gcc_assert (!processing_explicit_instantiation
);
751 processing_explicit_instantiation
= true;
756 end_explicit_instantiation (void)
758 gcc_assert (processing_explicit_instantiation
);
759 processing_explicit_instantiation
= false;
762 /* An explicit specialization or partial specialization of TMPL is being
763 declared. Check that the namespace in which the specialization is
764 occurring is permissible. Returns false iff it is invalid to
765 specialize TMPL in the current namespace. */
768 check_specialization_namespace (tree tmpl
)
770 tree tpl_ns
= decl_namespace_context (tmpl
);
774 An explicit specialization shall be declared in the namespace of
775 which the template is a member, or, for member templates, in the
776 namespace of which the enclosing class or enclosing class
777 template is a member. An explicit specialization of a member
778 function, member class or static data member of a class template
779 shall be declared in the namespace of which the class template is
781 if (current_scope() != DECL_CONTEXT (tmpl
)
782 && !at_namespace_scope_p ())
784 error ("specialization of %qD must appear at namespace scope", tmpl
);
787 if (is_associated_namespace (current_namespace
, tpl_ns
))
788 /* Same or super-using namespace. */
792 permerror (input_location
,
793 "specialization of %qD in different namespace", tmpl
);
794 permerror (DECL_SOURCE_LOCATION (tmpl
),
795 " from definition of %q#D", tmpl
);
800 /* SPEC is an explicit instantiation. Check that it is valid to
801 perform this explicit instantiation in the current namespace. */
804 check_explicit_instantiation_namespace (tree spec
)
808 /* DR 275: An explicit instantiation shall appear in an enclosing
809 namespace of its template. */
810 ns
= decl_namespace_context (spec
);
811 if (!is_ancestor (current_namespace
, ns
))
812 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
813 "(which does not enclose namespace %qD)",
814 spec
, current_namespace
, ns
);
817 // Returns the type of a template specialization only if that
818 // specialization needs to be defined. Otherwise (e.g., if the type has
819 // already been defined), the function returns NULL_TREE.
821 maybe_new_partial_specialization (tree type
)
823 // An implicit instantiation of an incomplete type implies
824 // the definition of a new class template.
826 // template<typename T>
829 // template<typename T>
832 // Here, S<T*> is an implicit instantiation of S whose type
834 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
837 // It can also be the case that TYPE is a completed specialization.
838 // Continuing the previous example, suppose we also declare:
840 // template<typename T>
841 // requires Integral<T>
844 // Here, S<T*> refers to the specialization S<T*> defined
845 // above. However, we need to differentiate definitions because
846 // we intend to define a new partial specialization. In this case,
847 // we rely on the fact that the constraints are different for
848 // this declaration than that above.
850 // Note that we also get here for injected class names and
851 // late-parsed template definitions. We must ensure that we
852 // do not create new type declarations for those cases.
853 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
855 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
856 tree args
= CLASSTYPE_TI_ARGS (type
);
858 // If there are no template parameters, this cannot be a new
859 // partial template specializtion?
860 if (!current_template_parms
)
863 // The injected-class-name is not a new partial specialization.
864 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
867 // If the constraints are not the same as those of the primary
868 // then, we can probably create a new specialization.
869 tree type_constr
= current_template_constraints ();
871 if (type
== TREE_TYPE (tmpl
))
873 tree main_constr
= get_constraints (tmpl
);
874 if (equivalent_constraints (type_constr
, main_constr
))
878 // Also, if there's a pre-existing specialization with matching
879 // constraints, then this also isn't new.
880 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
883 tree spec_tmpl
= TREE_VALUE (specs
);
884 tree spec_args
= TREE_PURPOSE (specs
);
885 tree spec_constr
= get_constraints (spec_tmpl
);
886 if (comp_template_args (args
, spec_args
)
887 && equivalent_constraints (type_constr
, spec_constr
))
889 specs
= TREE_CHAIN (specs
);
892 // Create a new type node (and corresponding type decl)
893 // for the newly declared specialization.
894 tree t
= make_class_type (TREE_CODE (type
));
895 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
896 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (type
);
897 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
899 /* We only need a separate type node for storing the definition of this
900 partial specialization; uses of S<T*> are unconstrained, so all are
901 equivalent. So keep TYPE_CANONICAL the same. */
902 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
904 // Build the corresponding type decl.
905 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
906 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
907 DECL_SOURCE_LOCATION (d
) = input_location
;
915 /* The TYPE is being declared. If it is a template type, that means it
916 is a partial specialization. Do appropriate error-checking. */
919 maybe_process_partial_specialization (tree type
)
923 if (type
== error_mark_node
)
924 return error_mark_node
;
926 /* A lambda that appears in specialization context is not itself a
928 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
931 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
933 error ("name of class shadows template template parameter %qD",
935 return error_mark_node
;
938 context
= TYPE_CONTEXT (type
);
940 if (TYPE_ALIAS_P (type
))
942 if (TYPE_TEMPLATE_INFO (type
)
943 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
944 error ("specialization of alias template %qD",
945 TYPE_TI_TEMPLATE (type
));
947 error ("explicit specialization of non-template %qT", type
);
948 return error_mark_node
;
950 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
952 /* This is for ordinary explicit specialization and partial
953 specialization of a template class such as:
955 template <> class C<int>;
959 template <class T> class C<T*>;
961 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
963 if (tree t
= maybe_new_partial_specialization (type
))
965 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
966 && !at_namespace_scope_p ())
967 return error_mark_node
;
968 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
969 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
970 if (processing_template_decl
)
972 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
973 if (decl
== error_mark_node
)
974 return error_mark_node
;
975 return TREE_TYPE (decl
);
978 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
979 error ("specialization of %qT after instantiation", type
);
980 else if (errorcount
&& !processing_specialization
981 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
982 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
983 /* Trying to define a specialization either without a template<> header
984 or in an inappropriate place. We've already given an error, so just
985 bail now so we don't actually define the specialization. */
986 return error_mark_node
;
988 else if (CLASS_TYPE_P (type
)
989 && !CLASSTYPE_USE_TEMPLATE (type
)
990 && CLASSTYPE_TEMPLATE_INFO (type
)
991 && context
&& CLASS_TYPE_P (context
)
992 && CLASSTYPE_TEMPLATE_INFO (context
))
994 /* This is for an explicit specialization of member class
995 template according to [temp.expl.spec/18]:
997 template <> template <class U> class C<int>::D;
999 The context `C<int>' must be an implicit instantiation.
1000 Otherwise this is just a member class template declared
1003 template <> class C<int> { template <class U> class D; };
1004 template <> template <class U> class C<int>::D;
1006 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1007 while in the second case, `C<int>::D' is a primary template
1008 and `C<T>::D' may not exist. */
1010 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1011 && !COMPLETE_TYPE_P (type
))
1014 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1016 if (current_namespace
1017 != decl_namespace_context (tmpl
))
1019 permerror (input_location
,
1020 "specializing %q#T in different namespace", type
);
1021 permerror (DECL_SOURCE_LOCATION (tmpl
),
1022 " from definition of %q#D", tmpl
);
1025 /* Check for invalid specialization after instantiation:
1027 template <> template <> class C<int>::D<int>;
1028 template <> template <class U> class C<int>::D; */
1030 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1031 t
; t
= TREE_CHAIN (t
))
1033 tree inst
= TREE_VALUE (t
);
1034 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1035 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1037 /* We already have a full specialization of this partial
1038 instantiation, or a full specialization has been
1039 looked up but not instantiated. Reassign it to the
1040 new member specialization template. */
1044 elt
.tmpl
= most_general_template (tmpl
);
1045 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1048 type_specializations
->remove_elt (&elt
);
1051 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1054 = type_specializations
->find_slot (&elt
, INSERT
);
1055 entry
= ggc_alloc
<spec_entry
> ();
1060 /* But if we've had an implicit instantiation, that's a
1061 problem ([temp.expl.spec]/6). */
1062 error ("specialization %qT after instantiation %qT",
1066 /* Mark TYPE as a specialization. And as a result, we only
1067 have one level of template argument for the innermost
1069 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1070 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1071 CLASSTYPE_TI_ARGS (type
)
1072 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1075 else if (processing_specialization
)
1077 /* Someday C++0x may allow for enum template specialization. */
1078 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1079 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1080 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1081 "of %qD not allowed by ISO C++", type
);
1084 error ("explicit specialization of non-template %qT", type
);
1085 return error_mark_node
;
1092 /* Returns nonzero if we can optimize the retrieval of specializations
1093 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1094 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1097 optimize_specialization_lookup_p (tree tmpl
)
1099 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1100 && DECL_CLASS_SCOPE_P (tmpl
)
1101 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1103 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1104 /* The optimized lookup depends on the fact that the
1105 template arguments for the member function template apply
1106 purely to the containing class, which is not true if the
1107 containing class is an explicit or partial
1109 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1110 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1111 && !DECL_CONV_FN_P (tmpl
)
1112 /* It is possible to have a template that is not a member
1113 template and is not a member of a template class:
1115 template <typename T>
1116 struct S { friend A::f(); };
1118 Here, the friend function is a template, but the context does
1119 not have template information. The optimized lookup relies
1120 on having ARGS be the template arguments for both the class
1121 and the function template. */
1122 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1125 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1126 gone through coerce_template_parms by now. */
1129 verify_unstripped_args (tree args
)
1131 ++processing_template_decl
;
1132 if (!any_dependent_template_arguments_p (args
))
1134 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1135 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1137 tree arg
= TREE_VEC_ELT (inner
, i
);
1138 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1140 else if (TYPE_P (arg
))
1141 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1142 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1143 /* Allow typedefs on the type of a non-type argument, since a
1144 parameter can have them. */;
1146 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1149 --processing_template_decl
;
1152 /* Retrieve the specialization (in the sense of [temp.spec] - a
1153 specialization is either an instantiation or an explicit
1154 specialization) of TMPL for the given template ARGS. If there is
1155 no such specialization, return NULL_TREE. The ARGS are a vector of
1156 arguments, or a vector of vectors of arguments, in the case of
1157 templates with more than one level of parameters.
1159 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1160 then we search for a partial specialization matching ARGS. This
1161 parameter is ignored if TMPL is not a class template.
1163 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1164 result is a NONTYPE_ARGUMENT_PACK. */
1167 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1169 if (tmpl
== NULL_TREE
)
1172 if (args
== error_mark_node
)
1175 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1176 || TREE_CODE (tmpl
) == FIELD_DECL
);
1178 /* There should be as many levels of arguments as there are
1179 levels of parameters. */
1180 gcc_assert (TMPL_ARGS_DEPTH (args
)
1181 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1182 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1183 : template_class_depth (DECL_CONTEXT (tmpl
))));
1186 verify_unstripped_args (args
);
1188 if (optimize_specialization_lookup_p (tmpl
))
1190 tree class_template
;
1191 tree class_specialization
;
1192 vec
<tree
, va_gc
> *methods
;
1196 /* The template arguments actually apply to the containing
1197 class. Find the class specialization with those
1199 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1200 class_specialization
1201 = retrieve_specialization (class_template
, args
, 0);
1202 if (!class_specialization
)
1204 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1205 for the specialization. */
1206 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1209 /* Iterate through the methods with the indicated name, looking
1210 for the one that has an instance of TMPL. */
1211 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1212 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1214 tree fn
= OVL_CURRENT (fns
);
1215 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn
) == class_specialization
)
1227 hash_table
<spec_hasher
> *specializations
;
1231 elt
.spec
= NULL_TREE
;
1233 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1234 specializations
= type_specializations
;
1236 specializations
= decl_specializations
;
1239 hash
= spec_hasher::hash (&elt
);
1240 found
= specializations
->find_with_hash (&elt
, hash
);
1248 /* Like retrieve_specialization, but for local declarations. */
1251 retrieve_local_specialization (tree tmpl
)
1253 if (local_specializations
== NULL
)
1256 tree
*slot
= local_specializations
->get (tmpl
);
1257 return slot
? *slot
: NULL_TREE
;
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263 is_specialization_of (tree decl
, tree tmpl
)
1267 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1271 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1277 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1279 for (t
= TREE_TYPE (decl
);
1281 t
= CLASSTYPE_USE_TEMPLATE (t
)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1283 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1294 is_specialization_of_friend (tree decl
, tree friend_decl
)
1296 bool need_template
= true;
1299 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1300 || TREE_CODE (decl
) == TYPE_DECL
);
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1305 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl
)
1307 && !DECL_USE_TEMPLATE (friend_decl
))
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1311 need_template
= false;
1313 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl
))
1315 need_template
= false;
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1321 if (is_specialization_of (decl
, friend_decl
))
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1330 For example, given a template friend declaration
1332 template <class T> friend void A<T>::f();
1334 the member function below is considered a friend
1336 template <> struct A<int> {
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1344 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1346 && DECL_CLASS_SCOPE_P (decl
)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1366 tree context
= DECL_CONTEXT (decl
);
1367 tree args
= NULL_TREE
;
1368 int current_depth
= 0;
1370 while (current_depth
< template_depth
)
1372 if (CLASSTYPE_TEMPLATE_INFO (context
))
1374 if (current_depth
== 0)
1375 args
= TYPE_TI_ARGS (context
);
1377 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1380 context
= TYPE_CONTEXT (context
);
1383 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1388 tree friend_args_type
;
1389 tree decl_args_type
;
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1393 is_template
= DECL_TEMPLATE_INFO (decl
)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1395 if (need_template
^ is_template
)
1397 else if (is_template
)
1399 /* If both are templates, check template parameter list. */
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1408 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1411 decl_type
= TREE_TYPE (decl
);
1413 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1414 tf_none
, NULL_TREE
);
1415 if (friend_type
== error_mark_node
)
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1425 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1427 friend_args_type
= TREE_CHAIN (friend_args_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1429 decl_args_type
= TREE_CHAIN (decl_args_type
);
1431 return compparms (decl_args_type
, friend_args_type
);
1435 /* DECL is a TYPE_DECL */
1437 tree decl_type
= TREE_TYPE (decl
);
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1445 if (need_template
^ is_template
)
1447 else if (is_template
)
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1453 != DECL_NAME (friend_decl
))
1456 /* Now check template parameter list. */
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1465 return (DECL_NAME (decl
)
1466 == DECL_NAME (friend_decl
));
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1481 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1485 spec_entry
**slot
= NULL
;
1488 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1489 || (TREE_CODE (tmpl
) == FIELD_DECL
1490 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1492 if (TREE_CODE (spec
) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec
)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1501 template <class T> void f(T t1, T t = T())
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1507 if (optimize_specialization_lookup_p (tmpl
))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn
= retrieve_specialization (tmpl
, args
, 0);
1518 hash
= spec_hasher::hash (&elt
);
1521 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1523 fn
= ((spec_entry
*) *slot
)->spec
;
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1535 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1537 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1539 if (DECL_ODR_USED (fn
)
1540 || DECL_EXPLICIT_INSTANTIATION (fn
))
1542 error ("specialization of %qD after instantiation",
1544 return error_mark_node
;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn
) = NULL_TREE
;
1573 duplicate_decls (spec
, fn
, is_friend
);
1574 /* The call to duplicate_decls will have applied
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone
, fn
)
1586 DECL_DECLARED_INLINE_P (clone
)
1587 = DECL_DECLARED_INLINE_P (fn
);
1588 DECL_SOURCE_LOCATION (clone
)
1589 = DECL_SOURCE_LOCATION (fn
);
1590 DECL_DELETED_FN (clone
)
1591 = DECL_DELETED_FN (fn
);
1593 check_specialization_namespace (tmpl
);
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1600 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1601 /* Dup decl failed, but this is a new definition. Set the
1602 line number so any errors match this new
1604 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1610 return duplicate_decls (spec
, fn
, is_friend
);
1612 /* A specialization must be declared in the same namespace as the
1613 template it is specializing. */
1614 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1615 && !check_specialization_namespace (tmpl
))
1616 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1618 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1620 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1621 gcc_assert (tmpl
&& args
&& spec
);
1624 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1625 && PRIMARY_TEMPLATE_P (tmpl
)
1626 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1627 || variable_template_p (tmpl
))
1628 /* If TMPL is a forward declaration of a template function, keep a list
1629 of all specializations in case we need to reassign them to a friend
1630 template later in tsubst_friend_function.
1632 Also keep a list of all variable template instantiations so that
1633 process_partial_specialization can check whether a later partial
1634 specialization would have used it. */
1635 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1636 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1642 /* Returns true iff two spec_entry nodes are equivalent. */
1644 int comparing_specializations
;
1647 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1651 ++comparing_specializations
;
1652 equal
= (e1
->tmpl
== e2
->tmpl
1653 && comp_template_args (e1
->args
, e2
->args
));
1654 if (equal
&& flag_concepts
1655 /* tmpl could be a FIELD_DECL for a capture pack. */
1656 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1657 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1658 && uses_template_parms (e1
->args
))
1660 /* Partial specializations of a variable template can be distinguished by
1662 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1663 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1664 equal
= equivalent_constraints (c1
, c2
);
1666 --comparing_specializations
;
1671 /* Returns a hash for a template TMPL and template arguments ARGS. */
1674 hash_tmpl_and_args (tree tmpl
, tree args
)
1676 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1677 return iterative_hash_template_arg (args
, val
);
1680 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1684 spec_hasher::hash (spec_entry
*e
)
1686 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1689 /* Recursively calculate a hash value for a template argument ARG, for use
1690 in the hash tables of template specializations. */
1693 iterative_hash_template_arg (tree arg
, hashval_t val
)
1695 unsigned HOST_WIDE_INT i
;
1696 enum tree_code code
;
1699 if (arg
== NULL_TREE
)
1700 return iterative_hash_object (arg
, val
);
1705 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1708 code
= TREE_CODE (arg
);
1709 tclass
= TREE_CODE_CLASS (code
);
1711 val
= iterative_hash_object (code
, val
);
1718 case IDENTIFIER_NODE
:
1719 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1723 int i
, len
= TREE_VEC_LENGTH (arg
);
1724 for (i
= 0; i
< len
; ++i
)
1725 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1729 case TYPE_PACK_EXPANSION
:
1730 case EXPR_PACK_EXPANSION
:
1731 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1732 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1734 case TYPE_ARGUMENT_PACK
:
1735 case NONTYPE_ARGUMENT_PACK
:
1736 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1739 for (; arg
; arg
= TREE_CHAIN (arg
))
1740 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1744 for (; arg
; arg
= OVL_NEXT (arg
))
1745 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1751 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1752 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1754 val
= iterative_hash_template_arg (field
, val
);
1755 val
= iterative_hash_template_arg (value
, val
);
1761 if (!DECL_ARTIFICIAL (arg
))
1763 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1764 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1766 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1769 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1772 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1773 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1775 case TEMPLATE_PARM_INDEX
:
1776 val
= iterative_hash_template_arg
1777 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1778 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1779 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1782 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1783 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1784 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1787 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1789 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1793 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1794 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1795 val
= iterative_hash_object (code
, val
);
1796 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1799 /* A lambda can't appear in a template arg, but don't crash on
1801 gcc_assert (seen_error ());
1805 case IMPLICIT_CONV_EXPR
:
1806 case STATIC_CAST_EXPR
:
1807 case REINTERPRET_CAST_EXPR
:
1808 case CONST_CAST_EXPR
:
1809 case DYNAMIC_CAST_EXPR
:
1811 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1812 /* Now hash operands as usual. */
1822 if (alias_template_specialization_p (arg
))
1824 // We want an alias specialization that survived strip_typedefs
1825 // to hash differently from its TYPE_CANONICAL, to avoid hash
1826 // collisions that compare as different in template_args_equal.
1827 // These could be dependent specializations that strip_typedefs
1828 // left alone, or untouched specializations because
1829 // coerce_template_parms returns the unconverted template
1830 // arguments if it sees incomplete argument packs.
1831 tree ti
= TYPE_TEMPLATE_INFO (arg
);
1832 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1834 if (TYPE_CANONICAL (arg
))
1835 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1837 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1838 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1839 /* Otherwise just compare the types during lookup. */
1842 case tcc_declaration
:
1844 return iterative_hash_expr (arg
, val
);
1847 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1849 unsigned n
= cp_tree_operand_length (arg
);
1850 for (i
= 0; i
< n
; ++i
)
1851 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1859 /* Unregister the specialization SPEC as a specialization of TMPL.
1860 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1861 if the SPEC was listed as a specialization of TMPL.
1863 Note that SPEC has been ggc_freed, so we can't look inside it. */
1866 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1871 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1872 elt
.args
= TI_ARGS (tinfo
);
1873 elt
.spec
= NULL_TREE
;
1875 entry
= decl_specializations
->find (&elt
);
1878 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1879 gcc_assert (new_spec
!= NULL_TREE
);
1880 entry
->spec
= new_spec
;
1887 /* Like register_specialization, but for local declarations. We are
1888 registering SPEC, an instantiation of TMPL. */
1891 register_local_specialization (tree spec
, tree tmpl
)
1893 local_specializations
->put (tmpl
, spec
);
1896 /* TYPE is a class type. Returns true if TYPE is an explicitly
1897 specialized class. */
1900 explicit_class_specialization_p (tree type
)
1902 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1904 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1907 /* Print the list of functions at FNS, going through all the overloads
1908 for each element of the list. Alternatively, FNS can not be a
1909 TREE_LIST, in which case it will be printed together with all the
1912 MORE and *STR should respectively be FALSE and NULL when the function
1913 is called from the outside. They are used internally on recursive
1914 calls. print_candidates manages the two parameters and leaves NULL
1915 in *STR when it ends. */
1918 print_candidates_1 (tree fns
, bool more
, const char **str
)
1921 char *spaces
= NULL
;
1923 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1924 if (TREE_CODE (fn
) == TREE_LIST
)
1926 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1927 print_candidates_1 (TREE_VALUE (fn2
),
1928 TREE_CHAIN (fn2
) || more
, str
);
1932 tree cand
= OVL_CURRENT (fn
);
1935 /* Pick the prefix string. */
1936 if (!more
&& !OVL_NEXT (fns
))
1938 inform (DECL_SOURCE_LOCATION (cand
),
1939 "candidate is: %#D", cand
);
1943 *str
= _("candidates are:");
1944 spaces
= get_spaces (*str
);
1946 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1947 *str
= spaces
? spaces
: *str
;
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1961 print_candidates (tree fns
)
1963 const char *str
= NULL
;
1964 print_candidates_1 (fns
, false, &str
);
1965 gcc_assert (str
== NULL
);
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1971 get_template_for_ordering (tree list
)
1973 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1974 tree f
= TREE_VALUE (list
);
1975 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1976 return TI_TEMPLATE (ti
);
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1989 most_constrained_function (tree candidates
)
1991 // Try to find the best candidate in a first pass.
1992 tree champ
= candidates
;
1993 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1995 int winner
= more_constrained (get_template_for_ordering (champ
),
1996 get_template_for_ordering (c
));
1998 champ
= c
; // The candidate is more constrained
1999 else if (winner
== 0)
2000 return NULL_TREE
; // Neither is more constrained
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2005 if (!more_constrained (get_template_for_ordering (champ
),
2006 get_template_for_ordering (c
)))
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2039 determine_specialization (tree template_id
,
2042 int need_member_template
,
2048 tree explicit_targs
;
2049 tree candidates
= NULL_TREE
;
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates
= NULL_TREE
;
2058 cp_binding_level
*b
;
2060 *targs_out
= NULL_TREE
;
2062 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2063 return error_mark_node
;
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2068 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2069 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2071 gcc_assert (errorcount
);
2072 return error_mark_node
;
2075 fns
= TREE_OPERAND (template_id
, 0);
2076 explicit_targs
= TREE_OPERAND (template_id
, 1);
2078 if (fns
== error_mark_node
)
2079 return error_mark_node
;
2081 /* Check for baselinks. */
2082 if (BASELINK_P (fns
))
2083 fns
= BASELINK_FUNCTIONS (fns
);
2085 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2087 error ("%qD is not a function template", fns
);
2088 return error_mark_node
;
2090 else if (VAR_P (decl
) && !variable_template_p (fns
))
2092 error ("%qD is not a variable template", fns
);
2093 return error_mark_node
;
2096 /* Count the number of template headers specified for this
2099 for (b
= current_binding_level
;
2100 b
->kind
== sk_template_parms
;
2104 tree orig_fns
= fns
;
2106 if (variable_template_p (fns
))
2108 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2109 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2110 tf_warning_or_error
,
2111 /*req_all*/true, /*use_defarg*/true);
2112 if (targs
!= error_mark_node
)
2113 templates
= tree_cons (targs
, fns
, templates
);
2115 else for (; fns
; fns
= OVL_NEXT (fns
))
2117 tree fn
= OVL_CURRENT (fns
);
2119 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2121 tree decl_arg_types
;
2125 /* In case of explicit specialization, we need to check if
2126 the number of template headers appearing in the specialization
2127 is correct. This is usually done in check_explicit_specialization,
2128 but the check done there cannot be exhaustive when specializing
2129 member functions. Consider the following code:
2131 template <> void A<int>::f(int);
2132 template <> template <> void A<int>::f(int);
2134 Assuming that A<int> is not itself an explicit specialization
2135 already, the first line specializes "f" which is a non-template
2136 member function, whilst the second line specializes "f" which
2137 is a template member function. So both lines are syntactically
2138 correct, and check_explicit_specialization does not reject
2141 Here, we can do better, as we are matching the specialization
2142 against the declarations. We count the number of template
2143 headers, and we check if they match TEMPLATE_COUNT + 1
2144 (TEMPLATE_COUNT is the number of qualifying template classes,
2145 plus there must be another header for the member template
2148 Notice that if header_count is zero, this is not a
2149 specialization but rather a template instantiation, so there
2150 is no check we can perform here. */
2151 if (header_count
&& header_count
!= template_count
+ 1)
2154 /* Check that the number of template arguments at the
2155 innermost level for DECL is the same as for FN. */
2156 if (current_binding_level
->kind
== sk_template_parms
2157 && !current_binding_level
->explicit_spec_p
2158 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2159 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2160 (current_template_parms
))))
2163 /* DECL might be a specialization of FN. */
2164 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2165 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2167 /* For a non-static member function, we need to make sure
2168 that the const qualification is the same. Since
2169 get_bindings does not try to merge the "this" parameter,
2170 we must do the comparison explicitly. */
2171 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2172 && !same_type_p (TREE_VALUE (fn_arg_types
),
2173 TREE_VALUE (decl_arg_types
)))
2176 /* Skip the "this" parameter and, for constructors of
2177 classes with virtual bases, the VTT parameter. A
2178 full specialization of a constructor will have a VTT
2179 parameter, but a template never will. */
2181 = skip_artificial_parms_for (decl
, decl_arg_types
);
2183 = skip_artificial_parms_for (fn
, fn_arg_types
);
2185 /* Function templates cannot be specializations; there are
2186 no partial specializations of functions. Therefore, if
2187 the type of DECL does not match FN, there is no
2190 Note that it should never be the case that we have both
2191 candidates added here, and for regular member functions
2193 if (tsk
== tsk_template
)
2195 if (compparms (fn_arg_types
, decl_arg_types
))
2196 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2200 /* See whether this function might be a specialization of this
2201 template. Suppress access control because we might be trying
2202 to make this specialization a friend, and we have already done
2203 access control for the declaration of the specialization. */
2204 push_deferring_access_checks (dk_no_check
);
2205 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2206 pop_deferring_access_checks ();
2209 /* We cannot deduce template arguments that when used to
2210 specialize TMPL will produce DECL. */
2213 /* Remove, from the set of candidates, all those functions
2214 whose constraints are not satisfied. */
2215 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2218 // Then, try to form the new function type.
2219 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
2220 if (insttype
== error_mark_node
)
2223 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2224 if (!compparms (fn_arg_types
, decl_arg_types
))
2227 /* Save this template, and the arguments deduced. */
2228 templates
= tree_cons (targs
, fn
, templates
);
2230 else if (need_member_template
)
2231 /* FN is an ordinary member function, and we need a
2232 specialization of a member template. */
2234 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2235 /* We can get IDENTIFIER_NODEs here in certain erroneous
2238 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2239 /* This is just an ordinary non-member function. Nothing can
2240 be a specialization of that. */
2242 else if (DECL_ARTIFICIAL (fn
))
2243 /* Cannot specialize functions that are created implicitly. */
2247 tree decl_arg_types
;
2249 /* This is an ordinary member function. However, since
2250 we're here, we can assume its enclosing class is a
2251 template class. For example,
2253 template <typename T> struct S { void f(); };
2254 template <> void S<int>::f() {}
2256 Here, S<int>::f is a non-template, but S<int> is a
2257 template class. If FN has the same type as DECL, we
2258 might be in business. */
2260 if (!DECL_TEMPLATE_INFO (fn
))
2261 /* Its enclosing class is an explicit specialization
2262 of a template class. This is not a candidate. */
2265 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2266 TREE_TYPE (TREE_TYPE (fn
))))
2267 /* The return types differ. */
2270 /* Adjust the type of DECL in case FN is a static member. */
2271 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2272 if (DECL_STATIC_FUNCTION_P (fn
)
2273 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2274 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2276 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2280 // If the deduced arguments do not satisfy the constraints,
2281 // this is not a candidate.
2282 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2285 // Add the candidate.
2286 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2290 if (templates
&& TREE_CHAIN (templates
))
2296 It is possible for a specialization with a given function
2297 signature to be instantiated from more than one function
2298 template. In such cases, explicit specification of the
2299 template arguments must be used to uniquely identify the
2300 function template specialization being specialized.
2302 Note that here, there's no suggestion that we're supposed to
2303 determine which of the candidate templates is most
2304 specialized. However, we, also have:
2308 Partial ordering of overloaded function template
2309 declarations is used in the following contexts to select
2310 the function template to which a function template
2311 specialization refers:
2313 -- when an explicit specialization refers to a function
2316 So, we do use the partial ordering rules, at least for now.
2317 This extension can only serve to make invalid programs valid,
2318 so it's safe. And, there is strong anecdotal evidence that
2319 the committee intended the partial ordering rules to apply;
2320 the EDG front end has that behavior, and John Spicer claims
2321 that the committee simply forgot to delete the wording in
2322 [temp.expl.spec]. */
2323 tree tmpl
= most_specialized_instantiation (templates
);
2324 if (tmpl
!= error_mark_node
)
2327 TREE_CHAIN (templates
) = NULL_TREE
;
2331 // Concepts allows multiple declarations of member functions
2332 // with the same signature. Like above, we need to rely on
2333 // on the partial ordering of those candidates to determine which
2335 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2337 if (tree cand
= most_constrained_function (candidates
))
2340 TREE_CHAIN (cand
) = NULL_TREE
;
2344 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2346 error ("template-id %qD for %q+D does not match any template "
2347 "declaration", template_id
, decl
);
2348 if (header_count
&& header_count
!= template_count
+ 1)
2349 inform (input_location
, "saw %d %<template<>%>, need %d for "
2350 "specializing a member function template",
2351 header_count
, template_count
+ 1);
2353 print_candidates (orig_fns
);
2354 return error_mark_node
;
2356 else if ((templates
&& TREE_CHAIN (templates
))
2357 || (candidates
&& TREE_CHAIN (candidates
))
2358 || (templates
&& candidates
))
2360 error ("ambiguous template specialization %qD for %q+D",
2362 candidates
= chainon (candidates
, templates
);
2363 print_candidates (candidates
);
2364 return error_mark_node
;
2367 /* We have one, and exactly one, match. */
2370 tree fn
= TREE_VALUE (candidates
);
2371 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2373 // Propagate the candidate's constraints to the declaration.
2374 set_constraints (decl
, get_constraints (fn
));
2376 /* DECL is a re-declaration or partial instantiation of a template
2378 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2380 /* It was a specialization of an ordinary member function in a
2382 return DECL_TI_TEMPLATE (fn
);
2385 /* It was a specialization of a template. */
2386 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2387 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2389 *targs_out
= copy_node (targs
);
2390 SET_TMPL_ARGS_LEVEL (*targs_out
,
2391 TMPL_ARGS_DEPTH (*targs_out
),
2392 TREE_PURPOSE (templates
));
2395 *targs_out
= TREE_PURPOSE (templates
);
2396 return TREE_VALUE (templates
);
2399 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2400 but with the default argument values filled in from those in the
2404 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2407 tree new_spec_types
;
2412 if (spec_types
== void_list_node
)
2413 return void_list_node
;
2415 /* Substitute into the rest of the list. */
2417 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2418 TREE_CHAIN (tmpl_types
));
2420 /* Add the default argument for this parameter. */
2421 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2422 TREE_VALUE (spec_types
),
2426 /* DECL is an explicit specialization. Replicate default arguments
2427 from the template it specializes. (That way, code like:
2429 template <class T> void f(T = 3);
2430 template <> void f(double);
2433 works, as required.) An alternative approach would be to look up
2434 the correct default arguments at the call-site, but this approach
2435 is consistent with how implicit instantiations are handled. */
2438 copy_default_args_to_explicit_spec (tree decl
)
2443 tree new_spec_types
;
2447 tree object_type
= NULL_TREE
;
2448 tree in_charge
= NULL_TREE
;
2449 tree vtt
= NULL_TREE
;
2451 /* See if there's anything we need to do. */
2452 tmpl
= DECL_TI_TEMPLATE (decl
);
2453 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2454 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2455 if (TREE_PURPOSE (t
))
2460 old_type
= TREE_TYPE (decl
);
2461 spec_types
= TYPE_ARG_TYPES (old_type
);
2463 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2465 /* Remove the this pointer, but remember the object's type for
2467 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2468 spec_types
= TREE_CHAIN (spec_types
);
2469 tmpl_types
= TREE_CHAIN (tmpl_types
);
2471 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2473 /* DECL may contain more parameters than TMPL due to the extra
2474 in-charge parameter in constructors and destructors. */
2475 in_charge
= spec_types
;
2476 spec_types
= TREE_CHAIN (spec_types
);
2478 if (DECL_HAS_VTT_PARM_P (decl
))
2481 spec_types
= TREE_CHAIN (spec_types
);
2485 /* Compute the merged default arguments. */
2487 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2489 /* Compute the new FUNCTION_TYPE. */
2493 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2498 /* Put the in-charge parameter back. */
2499 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2500 TREE_VALUE (in_charge
),
2503 new_type
= build_method_type_directly (object_type
,
2504 TREE_TYPE (old_type
),
2508 new_type
= build_function_type (TREE_TYPE (old_type
),
2510 new_type
= cp_build_type_attribute_variant (new_type
,
2511 TYPE_ATTRIBUTES (old_type
));
2512 new_type
= build_exception_variant (new_type
,
2513 TYPE_RAISES_EXCEPTIONS (old_type
));
2515 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2516 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2518 TREE_TYPE (decl
) = new_type
;
2521 /* Return the number of template headers we expect to see for a definition
2522 or specialization of CTYPE or one of its non-template members. */
2525 num_template_headers_for_class (tree ctype
)
2527 int num_templates
= 0;
2529 while (ctype
&& CLASS_TYPE_P (ctype
))
2531 /* You're supposed to have one `template <...>' for every
2532 template class, but you don't need one for a full
2533 specialization. For example:
2535 template <class T> struct S{};
2536 template <> struct S<int> { void f(); };
2537 void S<int>::f () {}
2539 is correct; there shouldn't be a `template <>' for the
2540 definition of `S<int>::f'. */
2541 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2542 /* If CTYPE does not have template information of any
2543 kind, then it is not a template, nor is it nested
2544 within a template. */
2546 if (explicit_class_specialization_p (ctype
))
2548 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2551 ctype
= TYPE_CONTEXT (ctype
);
2554 return num_templates
;
2557 /* Do a simple sanity check on the template headers that precede the
2558 variable declaration DECL. */
2561 check_template_variable (tree decl
)
2563 tree ctx
= CP_DECL_CONTEXT (decl
);
2564 int wanted
= num_template_headers_for_class (ctx
);
2565 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2566 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2568 if (cxx_dialect
< cxx14
)
2569 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2570 "variable templates only available with "
2571 "-std=c++14 or -std=gnu++14");
2573 // Namespace-scope variable templates should have a template header.
2576 if (template_header_count
> wanted
)
2578 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2579 "too many template headers for %D (should be %d)",
2581 if (warned
&& CLASS_TYPE_P (ctx
)
2582 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2583 inform (DECL_SOURCE_LOCATION (decl
),
2584 "members of an explicitly specialized class are defined "
2585 "without a template header");
2589 /* Check to see if the function just declared, as indicated in
2590 DECLARATOR, and in DECL, is a specialization of a function
2591 template. We may also discover that the declaration is an explicit
2592 instantiation at this point.
2594 Returns DECL, or an equivalent declaration that should be used
2595 instead if all goes well. Issues an error message if something is
2596 amiss. Returns error_mark_node if the error is not easily
2599 FLAGS is a bitmask consisting of the following flags:
2601 2: The function has a definition.
2602 4: The function is a friend.
2604 The TEMPLATE_COUNT is the number of references to qualifying
2605 template classes that appeared in the name of the function. For
2608 template <class T> struct S { void f(); };
2611 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2612 classes are not counted in the TEMPLATE_COUNT, so that in
2614 template <class T> struct S {};
2615 template <> struct S<int> { void f(); }
2616 template <> void S<int>::f();
2618 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2619 invalid; there should be no template <>.)
2621 If the function is a specialization, it is marked as such via
2622 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2623 is set up correctly, and it is added to the list of specializations
2624 for that template. */
2627 check_explicit_specialization (tree declarator
,
2632 int have_def
= flags
& 2;
2633 int is_friend
= flags
& 4;
2634 bool is_concept
= flags
& 8;
2635 int specialization
= 0;
2636 int explicit_instantiation
= 0;
2637 int member_specialization
= 0;
2638 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2639 tree dname
= DECL_NAME (decl
);
2644 if (!processing_specialization
)
2647 tsk
= tsk_excessive_parms
;
2650 tsk
= current_tmpl_spec_kind (template_count
);
2655 if (processing_specialization
&& !VAR_P (decl
))
2658 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2660 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2663 /* This could be something like:
2665 template <class T> void f(T);
2666 class S { friend void f<>(int); } */
2670 /* This case handles bogus declarations like template <>
2671 template <class T> void f<int>(); */
2673 error ("template-id %qD in declaration of primary template",
2680 case tsk_invalid_member_spec
:
2681 /* The error has already been reported in
2682 check_specialization_scope. */
2683 return error_mark_node
;
2685 case tsk_invalid_expl_inst
:
2686 error ("template parameter list used in explicit instantiation");
2692 error ("definition provided for explicit instantiation");
2694 explicit_instantiation
= 1;
2697 case tsk_excessive_parms
:
2698 case tsk_insufficient_parms
:
2699 if (tsk
== tsk_excessive_parms
)
2700 error ("too many template parameter lists in declaration of %qD",
2702 else if (template_header_count
)
2703 error("too few template parameter lists in declaration of %qD", decl
);
2705 error("explicit specialization of %qD must be introduced by "
2706 "%<template <>%>", decl
);
2711 error ("explicit specialization declared %<concept%>");
2713 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2714 /* In cases like template<> constexpr bool v = true;
2715 We'll give an error in check_template_variable. */
2718 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2720 member_specialization
= 1;
2726 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2728 /* This case handles bogus declarations like template <>
2729 template <class T> void f<int>(); */
2731 if (!uses_template_parms (declarator
))
2732 error ("template-id %qD in declaration of primary template",
2734 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2736 /* Partial specialization of variable template. */
2737 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2741 else if (cxx_dialect
< cxx14
)
2742 error ("non-type partial specialization %qD "
2743 "is not allowed", declarator
);
2745 error ("non-class, non-variable partial specialization %qD "
2746 "is not allowed", declarator
);
2751 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2752 /* This is a specialization of a member template, without
2753 specialization the containing class. Something like:
2755 template <class T> struct S {
2756 template <class U> void f (U);
2758 template <> template <class U> void S<int>::f(U) {}
2760 That's a specialization -- but of the entire template. */
2768 if ((specialization
|| member_specialization
)
2769 /* This doesn't apply to variable templates. */
2770 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2771 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2773 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2774 for (; t
; t
= TREE_CHAIN (t
))
2775 if (TREE_PURPOSE (t
))
2777 permerror (input_location
,
2778 "default argument specified in explicit specialization");
2783 if (specialization
|| member_specialization
|| explicit_instantiation
)
2785 tree tmpl
= NULL_TREE
;
2786 tree targs
= NULL_TREE
;
2787 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2789 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2790 if (!was_template_id
)
2794 gcc_assert (identifier_p (declarator
));
2799 /* If there is no class context, the explicit instantiation
2800 must be at namespace scope. */
2801 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2803 /* Find the namespace binding, using the declaration
2805 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2807 if (fns
== error_mark_node
)
2808 /* If lookup fails, look for a friend declaration so we can
2809 give a better diagnostic. */
2810 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2811 /*type*/false, /*complain*/true,
2814 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2816 error ("%qD is not a template function", dname
);
2817 fns
= error_mark_node
;
2821 declarator
= lookup_template_function (fns
, NULL_TREE
);
2824 if (declarator
== error_mark_node
)
2825 return error_mark_node
;
2827 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2829 if (!explicit_instantiation
)
2830 /* A specialization in class scope. This is invalid,
2831 but the error will already have been flagged by
2832 check_specialization_scope. */
2833 return error_mark_node
;
2836 /* It's not valid to write an explicit instantiation in
2839 class C { template void f(); }
2841 This case is caught by the parser. However, on
2844 template class C { void f(); };
2846 (which is invalid) we can get here. The error will be
2853 else if (ctype
!= NULL_TREE
2854 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2856 // We'll match variable templates in start_decl.
2860 /* Find the list of functions in ctype that have the same
2861 name as the declared function. */
2862 tree name
= TREE_OPERAND (declarator
, 0);
2863 tree fns
= NULL_TREE
;
2866 if (constructor_name_p (name
, ctype
))
2868 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2870 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2871 : !CLASSTYPE_DESTRUCTORS (ctype
))
2873 /* From [temp.expl.spec]:
2875 If such an explicit specialization for the member
2876 of a class template names an implicitly-declared
2877 special member function (clause _special_), the
2878 program is ill-formed.
2880 Similar language is found in [temp.explicit]. */
2881 error ("specialization of implicitly-declared special member function");
2882 return error_mark_node
;
2885 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2888 if (!DECL_CONV_FN_P (decl
))
2890 idx
= lookup_fnfields_1 (ctype
, name
);
2892 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2896 vec
<tree
, va_gc
> *methods
;
2899 /* For a type-conversion operator, we cannot do a
2900 name-based lookup. We might be looking for `operator
2901 int' which will be a specialization of `operator T'.
2902 So, we find *all* the conversion operators, and then
2903 select from them. */
2906 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2908 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2909 methods
->iterate (idx
, &ovl
);
2912 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2913 /* There are no more conversion functions. */
2916 /* Glue all these conversion functions together
2917 with those we already have. */
2918 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2919 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2923 if (fns
== NULL_TREE
)
2925 error ("no member function %qD declared in %qT", name
, ctype
);
2926 return error_mark_node
;
2929 TREE_OPERAND (declarator
, 0) = fns
;
2932 /* Figure out what exactly is being specialized at this point.
2933 Note that for an explicit instantiation, even one for a
2934 member function, we cannot tell apriori whether the
2935 instantiation is for a member template, or just a member
2936 function of a template class. Even if a member template is
2937 being instantiated, the member template arguments may be
2938 elided if they can be deduced from the rest of the
2940 tmpl
= determine_specialization (declarator
, decl
,
2942 member_specialization
,
2946 if (!tmpl
|| tmpl
== error_mark_node
)
2947 /* We couldn't figure out what this declaration was
2949 return error_mark_node
;
2952 if (!ctype
&& !was_template_id
2953 && (specialization
|| member_specialization
2954 || explicit_instantiation
)
2955 && !is_associated_namespace (CP_DECL_CONTEXT (decl
),
2956 CP_DECL_CONTEXT (tmpl
)))
2957 error ("%qD is not declared in %qD",
2958 tmpl
, current_namespace
);
2959 else if (TREE_CODE (decl
) == FUNCTION_DECL
2960 && DECL_HIDDEN_FRIEND_P (tmpl
))
2962 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2963 "friend declaration %qD is not visible to "
2964 "explicit specialization", tmpl
))
2965 inform (DECL_SOURCE_LOCATION (tmpl
),
2966 "friend declaration here");
2969 tree gen_tmpl
= most_general_template (tmpl
);
2971 if (explicit_instantiation
)
2973 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2974 is done by do_decl_instantiation later. */
2976 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2977 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2979 if (arg_depth
> parm_depth
)
2981 /* If TMPL is not the most general template (for
2982 example, if TMPL is a friend template that is
2983 injected into namespace scope), then there will
2984 be too many levels of TARGS. Remove some of them
2989 new_targs
= make_tree_vec (parm_depth
);
2990 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2991 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2992 = TREE_VEC_ELT (targs
, i
);
2996 return instantiate_template (tmpl
, targs
, tf_error
);
2999 /* If we thought that the DECL was a member function, but it
3000 turns out to be specializing a static member function,
3001 make DECL a static member function as well. */
3002 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3003 && DECL_STATIC_FUNCTION_P (tmpl
)
3004 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3005 revert_static_member_fn (decl
);
3007 /* If this is a specialization of a member template of a
3008 template class, we want to return the TEMPLATE_DECL, not
3009 the specialization of it. */
3010 if (tsk
== tsk_template
&& !was_template_id
)
3012 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3013 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3014 DECL_INITIAL (result
) = NULL_TREE
;
3018 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3019 DECL_SOURCE_LOCATION (result
)
3020 = DECL_SOURCE_LOCATION (decl
);
3021 /* We want to use the argument list specified in the
3022 definition, not in the original declaration. */
3023 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3024 for (parm
= DECL_ARGUMENTS (result
); parm
;
3025 parm
= DECL_CHAIN (parm
))
3026 DECL_CONTEXT (parm
) = result
;
3028 return register_specialization (tmpl
, gen_tmpl
, targs
,
3032 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3033 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3035 if (was_template_id
)
3036 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3038 /* Inherit default function arguments from the template
3039 DECL is specializing. */
3040 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3041 copy_default_args_to_explicit_spec (decl
);
3043 /* This specialization has the same protection as the
3044 template it specializes. */
3045 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3046 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3048 /* 7.1.1-1 [dcl.stc]
3050 A storage-class-specifier shall not be specified in an
3051 explicit specialization...
3053 The parser rejects these, so unless action is taken here,
3054 explicit function specializations will always appear with
3057 The action recommended by the C++ CWG in response to C++
3058 defect report 605 is to make the storage class and linkage
3059 of the explicit specialization match the templated function:
3061 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3063 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3065 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3066 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3068 /* A concept cannot be specialized. */
3069 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3071 error ("explicit specialization of function concept %qD",
3073 return error_mark_node
;
3076 /* This specialization has the same linkage and visibility as
3077 the function template it specializes. */
3078 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3079 if (! TREE_PUBLIC (decl
))
3081 DECL_INTERFACE_KNOWN (decl
) = 1;
3082 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3084 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3085 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3087 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3088 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3092 /* If DECL is a friend declaration, declared using an
3093 unqualified name, the namespace associated with DECL may
3094 have been set incorrectly. For example, in:
3096 template <typename T> void f(T);
3098 struct S { friend void f<int>(int); }
3101 we will have set the DECL_CONTEXT for the friend
3102 declaration to N, rather than to the global namespace. */
3103 if (DECL_NAMESPACE_SCOPE_P (decl
))
3104 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3106 if (is_friend
&& !have_def
)
3107 /* This is not really a declaration of a specialization.
3108 It's just the name of an instantiation. But, it's not
3109 a request for an instantiation, either. */
3110 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3111 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3112 /* A specialization is not necessarily COMDAT. */
3113 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3114 && DECL_DECLARED_INLINE_P (decl
));
3115 else if (VAR_P (decl
))
3116 DECL_COMDAT (decl
) = false;
3118 /* If this is a full specialization, register it so that we can find
3119 it again. Partial specializations will be registered in
3120 process_partial_specialization. */
3121 if (!processing_template_decl
)
3122 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3125 /* A 'structor should already have clones. */
3126 gcc_assert (decl
== error_mark_node
3127 || variable_template_p (tmpl
)
3128 || !(DECL_CONSTRUCTOR_P (decl
)
3129 || DECL_DESTRUCTOR_P (decl
))
3130 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3137 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3138 parameters. These are represented in the same format used for
3139 DECL_TEMPLATE_PARMS. */
3142 comp_template_parms (const_tree parms1
, const_tree parms2
)
3147 if (parms1
== parms2
)
3150 for (p1
= parms1
, p2
= parms2
;
3151 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3152 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3154 tree t1
= TREE_VALUE (p1
);
3155 tree t2
= TREE_VALUE (p2
);
3158 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3159 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3161 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3164 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3166 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3167 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3169 /* If either of the template parameters are invalid, assume
3170 they match for the sake of error recovery. */
3171 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3174 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3177 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3178 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3179 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3181 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3186 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3187 /* One set of parameters has more parameters lists than the
3194 /* Determine whether PARM is a parameter pack. */
3197 template_parameter_pack_p (const_tree parm
)
3199 /* Determine if we have a non-type template parameter pack. */
3200 if (TREE_CODE (parm
) == PARM_DECL
)
3201 return (DECL_TEMPLATE_PARM_P (parm
)
3202 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3203 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3204 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3206 /* If this is a list of template parameters, we could get a
3207 TYPE_DECL or a TEMPLATE_DECL. */
3208 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3209 parm
= TREE_TYPE (parm
);
3211 /* Otherwise it must be a type template parameter. */
3212 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3213 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3214 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3217 /* Determine if T is a function parameter pack. */
3220 function_parameter_pack_p (const_tree t
)
3222 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3223 return DECL_PACK_P (t
);
3227 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3228 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3231 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3233 if (! primary_func_tmpl_inst
3234 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3235 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3238 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3241 /* Return true iff the function parameter PARAM_DECL was expanded
3242 from the function parameter pack PACK. */
3245 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3247 if (DECL_ARTIFICIAL (param_decl
)
3248 || !function_parameter_pack_p (pack
))
3251 /* The parameter pack and its pack arguments have the same
3253 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3256 /* Determine whether ARGS describes a variadic template args list,
3257 i.e., one that is terminated by a template argument pack. */
3260 template_args_variadic_p (tree args
)
3265 if (args
== NULL_TREE
)
3268 args
= INNERMOST_TEMPLATE_ARGS (args
);
3269 nargs
= TREE_VEC_LENGTH (args
);
3274 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3276 return ARGUMENT_PACK_P (last_parm
);
3279 /* Generate a new name for the parameter pack name NAME (an
3280 IDENTIFIER_NODE) that incorporates its */
3283 make_ith_pack_parameter_name (tree name
, int i
)
3285 /* Munge the name to include the parameter index. */
3286 #define NUMBUF_LEN 128
3287 char numbuf
[NUMBUF_LEN
];
3291 if (name
== NULL_TREE
)
3293 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3294 newname_len
= IDENTIFIER_LENGTH (name
)
3295 + strlen (numbuf
) + 2;
3296 newname
= (char*)alloca (newname_len
);
3297 snprintf (newname
, newname_len
,
3298 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3299 return get_identifier (newname
);
3302 /* Return true if T is a primary function, class or alias template
3306 primary_template_instantiation_p (const_tree t
)
3311 if (TREE_CODE (t
) == FUNCTION_DECL
)
3312 return DECL_LANG_SPECIFIC (t
)
3313 && DECL_TEMPLATE_INSTANTIATION (t
)
3314 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3315 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3316 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3317 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3318 else if (alias_template_specialization_p (t
))
3323 /* Return true if PARM is a template template parameter. */
3326 template_template_parameter_p (const_tree parm
)
3328 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3331 /* Return true iff PARM is a DECL representing a type template
3335 template_type_parameter_p (const_tree parm
)
3338 && (TREE_CODE (parm
) == TYPE_DECL
3339 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3340 && DECL_TEMPLATE_PARM_P (parm
));
3343 /* Return the template parameters of T if T is a
3344 primary template instantiation, NULL otherwise. */
3347 get_primary_template_innermost_parameters (const_tree t
)
3349 tree parms
= NULL
, template_info
= NULL
;
3351 if ((template_info
= get_template_info (t
))
3352 && primary_template_instantiation_p (t
))
3353 parms
= INNERMOST_TEMPLATE_PARMS
3354 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3359 /* Return the template parameters of the LEVELth level from the full list
3360 of template parameters PARMS. */
3363 get_template_parms_at_level (tree parms
, int level
)
3367 || TREE_CODE (parms
) != TREE_LIST
3368 || level
> TMPL_PARMS_DEPTH (parms
))
3371 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3372 if (TMPL_PARMS_DEPTH (p
) == level
)
3378 /* Returns the template arguments of T if T is a template instantiation,
3382 get_template_innermost_arguments (const_tree t
)
3384 tree args
= NULL
, template_info
= NULL
;
3386 if ((template_info
= get_template_info (t
))
3387 && TI_ARGS (template_info
))
3388 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3393 /* Return the argument pack elements of T if T is a template argument pack,
3397 get_template_argument_pack_elems (const_tree t
)
3399 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3400 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3403 return ARGUMENT_PACK_ARGS (t
);
3406 /* Structure used to track the progress of find_parameter_packs_r. */
3407 struct find_parameter_pack_data
3409 /* TREE_LIST that will contain all of the parameter packs found by
3411 tree
* parameter_packs
;
3413 /* Set of AST nodes that have been visited by the traversal. */
3414 hash_set
<tree
> *visited
;
3416 /* True iff we're making a type pack expansion. */
3417 bool type_pack_expansion_p
;
3420 /* Identifies all of the argument packs that occur in a template
3421 argument and appends them to the TREE_LIST inside DATA, which is a
3422 find_parameter_pack_data structure. This is a subroutine of
3423 make_pack_expansion and uses_parameter_packs. */
3425 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3428 struct find_parameter_pack_data
* ppd
=
3429 (struct find_parameter_pack_data
*)data
;
3430 bool parameter_pack_p
= false;
3432 /* Handle type aliases/typedefs. */
3433 if (TYPE_ALIAS_P (t
))
3435 if (TYPE_TEMPLATE_INFO (t
))
3436 cp_walk_tree (&TYPE_TI_ARGS (t
),
3437 &find_parameter_packs_r
,
3443 /* Identify whether this is a parameter pack or not. */
3444 switch (TREE_CODE (t
))
3446 case TEMPLATE_PARM_INDEX
:
3447 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3448 parameter_pack_p
= true;
3451 case TEMPLATE_TYPE_PARM
:
3452 t
= TYPE_MAIN_VARIANT (t
);
3453 case TEMPLATE_TEMPLATE_PARM
:
3454 /* If the placeholder appears in the decl-specifier-seq of a function
3455 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3456 is a pack expansion, the invented template parameter is a template
3458 if (ppd
->type_pack_expansion_p
&& is_auto_or_concept (t
))
3459 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3460 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3461 parameter_pack_p
= true;
3466 if (DECL_PACK_P (t
))
3468 /* We don't want to walk into the type of a PARM_DECL,
3469 because we don't want to see the type parameter pack. */
3471 parameter_pack_p
= true;
3475 /* Look through a lambda capture proxy to the field pack. */
3477 if (DECL_HAS_VALUE_EXPR_P (t
))
3479 tree v
= DECL_VALUE_EXPR (t
);
3481 &find_parameter_packs_r
,
3485 else if (variable_template_specialization_p (t
))
3487 cp_walk_tree (&DECL_TI_ARGS (t
),
3488 find_parameter_packs_r
,
3495 parameter_pack_p
= true;
3498 /* Not a parameter pack. */
3502 if (parameter_pack_p
)
3504 /* Add this parameter pack to the list. */
3505 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3509 cp_walk_tree (&TYPE_CONTEXT (t
),
3510 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3512 /* This switch statement will return immediately if we don't find a
3514 switch (TREE_CODE (t
))
3516 case TEMPLATE_PARM_INDEX
:
3519 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3520 /* Check the template itself. */
3521 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3522 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3523 /* Check the template arguments. */
3524 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3529 case TEMPLATE_TYPE_PARM
:
3530 case TEMPLATE_TEMPLATE_PARM
:
3537 if (TYPE_PTRMEMFUNC_P (t
))
3543 if (TYPE_TEMPLATE_INFO (t
))
3544 cp_walk_tree (&TYPE_TI_ARGS (t
),
3545 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3552 cp_walk_tree (&TREE_TYPE (t
),
3553 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3557 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3562 case TYPE_PACK_EXPANSION
:
3563 case EXPR_PACK_EXPANSION
:
3568 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3573 case IDENTIFIER_NODE
:
3574 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3581 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3582 type_pack_expansion_p to false so that any placeholders
3583 within the expression don't get marked as parameter packs. */
3584 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3585 ppd
->type_pack_expansion_p
= false;
3586 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3588 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3600 /* Determines if the expression or type T uses any parameter packs. */
3602 uses_parameter_packs (tree t
)
3604 tree parameter_packs
= NULL_TREE
;
3605 struct find_parameter_pack_data ppd
;
3606 ppd
.parameter_packs
= ¶meter_packs
;
3607 ppd
.visited
= new hash_set
<tree
>;
3608 ppd
.type_pack_expansion_p
= false;
3609 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3611 return parameter_packs
!= NULL_TREE
;
3614 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3615 representation a base-class initializer into a parameter pack
3616 expansion. If all goes well, the resulting node will be an
3617 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3620 make_pack_expansion (tree arg
)
3623 tree parameter_packs
= NULL_TREE
;
3624 bool for_types
= false;
3625 struct find_parameter_pack_data ppd
;
3627 if (!arg
|| arg
== error_mark_node
)
3630 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3632 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3633 class initializer. In this case, the TREE_PURPOSE will be a
3634 _TYPE node (representing the base class expansion we're
3635 initializing) and the TREE_VALUE will be a TREE_LIST
3636 containing the initialization arguments.
3638 The resulting expansion looks somewhat different from most
3639 expansions. Rather than returning just one _EXPANSION, we
3640 return a TREE_LIST whose TREE_PURPOSE is a
3641 TYPE_PACK_EXPANSION containing the bases that will be
3642 initialized. The TREE_VALUE will be identical to the
3643 original TREE_VALUE, which is a list of arguments that will
3644 be passed to each base. We do not introduce any new pack
3645 expansion nodes into the TREE_VALUE (although it is possible
3646 that some already exist), because the TREE_PURPOSE and
3647 TREE_VALUE all need to be expanded together with the same
3648 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3649 resulting TREE_PURPOSE will mention the parameter packs in
3650 both the bases and the arguments to the bases. */
3653 tree parameter_packs
= NULL_TREE
;
3655 /* Determine which parameter packs will be used by the base
3657 ppd
.visited
= new hash_set
<tree
>;
3658 ppd
.parameter_packs
= ¶meter_packs
;
3659 ppd
.type_pack_expansion_p
= true;
3660 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3661 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3664 if (parameter_packs
== NULL_TREE
)
3666 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3668 return error_mark_node
;
3671 if (TREE_VALUE (arg
) != void_type_node
)
3673 /* Collect the sets of parameter packs used in each of the
3674 initialization arguments. */
3675 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3677 /* Determine which parameter packs will be expanded in this
3679 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3686 /* Create the pack expansion type for the base type. */
3687 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3688 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3689 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3691 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3692 they will rarely be compared to anything. */
3693 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3695 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3698 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3701 /* Build the PACK_EXPANSION_* node. */
3703 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3704 : make_node (EXPR_PACK_EXPANSION
);
3705 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3706 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3708 /* Propagate type and const-expression information. */
3709 TREE_TYPE (result
) = TREE_TYPE (arg
);
3710 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3711 /* Mark this read now, since the expansion might be length 0. */
3712 mark_exp_read (arg
);
3715 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3716 they will rarely be compared to anything. */
3717 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3719 /* Determine which parameter packs will be expanded. */
3720 ppd
.parameter_packs
= ¶meter_packs
;
3721 ppd
.visited
= new hash_set
<tree
>;
3722 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3723 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3726 /* Make sure we found some parameter packs. */
3727 if (parameter_packs
== NULL_TREE
)
3730 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3732 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3733 return error_mark_node
;
3735 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3737 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3742 /* Checks T for any "bare" parameter packs, which have not yet been
3743 expanded, and issues an error if any are found. This operation can
3744 only be done on full expressions or types (e.g., an expression
3745 statement, "if" condition, etc.), because we could have expressions like:
3747 foo(f(g(h(args)))...)
3749 where "args" is a parameter pack. check_for_bare_parameter_packs
3750 should not be called for the subexpressions args, h(args),
3751 g(h(args)), or f(g(h(args))), because we would produce erroneous
3754 Returns TRUE and emits an error if there were bare parameter packs,
3755 returns FALSE otherwise. */
3757 check_for_bare_parameter_packs (tree t
)
3759 tree parameter_packs
= NULL_TREE
;
3760 struct find_parameter_pack_data ppd
;
3762 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3765 if (TREE_CODE (t
) == TYPE_DECL
)
3768 ppd
.parameter_packs
= ¶meter_packs
;
3769 ppd
.visited
= new hash_set
<tree
>;
3770 ppd
.type_pack_expansion_p
= false;
3771 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3774 if (parameter_packs
)
3776 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3777 error_at (loc
, "parameter packs not expanded with %<...%>:");
3778 while (parameter_packs
)
3780 tree pack
= TREE_VALUE (parameter_packs
);
3781 tree name
= NULL_TREE
;
3783 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3784 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3785 name
= TYPE_NAME (pack
);
3786 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3787 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3789 name
= DECL_NAME (pack
);
3792 inform (loc
, " %qD", name
);
3794 inform (loc
, " <anonymous>");
3796 parameter_packs
= TREE_CHAIN (parameter_packs
);
3805 /* Expand any parameter packs that occur in the template arguments in
3808 expand_template_argument_pack (tree args
)
3810 tree result_args
= NULL_TREE
;
3811 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3812 int num_result_args
= -1;
3813 int non_default_args_count
= -1;
3815 /* First, determine if we need to expand anything, and the number of
3816 slots we'll need. */
3817 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3819 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3820 if (arg
== NULL_TREE
)
3822 if (ARGUMENT_PACK_P (arg
))
3824 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3825 if (num_result_args
< 0)
3826 num_result_args
= in_arg
+ num_packed
;
3828 num_result_args
+= num_packed
;
3832 if (num_result_args
>= 0)
3837 /* If no expansion is necessary, we're done. */
3838 if (num_result_args
< 0)
3841 /* Expand arguments. */
3842 result_args
= make_tree_vec (num_result_args
);
3843 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3844 non_default_args_count
=
3845 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3846 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3848 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3849 if (ARGUMENT_PACK_P (arg
))
3851 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3852 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3853 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3854 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3855 if (non_default_args_count
> 0)
3856 non_default_args_count
+= num_packed
- 1;
3860 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3864 if (non_default_args_count
>= 0)
3865 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3869 /* Checks if DECL shadows a template parameter.
3871 [temp.local]: A template-parameter shall not be redeclared within its
3872 scope (including nested scopes).
3874 Emits an error and returns TRUE if the DECL shadows a parameter,
3875 returns FALSE otherwise. */
3878 check_template_shadow (tree decl
)
3882 /* If we're not in a template, we can't possibly shadow a template
3884 if (!current_template_parms
)
3887 /* Figure out what we're shadowing. */
3888 if (TREE_CODE (decl
) == OVERLOAD
)
3889 decl
= OVL_CURRENT (decl
);
3890 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3892 /* If there's no previous binding for this name, we're not shadowing
3893 anything, let alone a template parameter. */
3897 /* If we're not shadowing a template parameter, we're done. Note
3898 that OLDDECL might be an OVERLOAD (or perhaps even an
3899 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3901 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3904 /* We check for decl != olddecl to avoid bogus errors for using a
3905 name inside a class. We check TPFI to avoid duplicate errors for
3906 inline member templates. */
3908 || (DECL_TEMPLATE_PARM_P (decl
)
3909 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3912 /* Don't complain about the injected class name, as we've already
3913 complained about the class itself. */
3914 if (DECL_SELF_REFERENCE_P (decl
))
3917 if (DECL_TEMPLATE_PARM_P (decl
))
3918 error ("declaration of template parameter %q+D shadows "
3919 "template parameter", decl
);
3921 error ("declaration of %q+#D shadows template parameter", decl
);
3922 inform (DECL_SOURCE_LOCATION (olddecl
),
3923 "template parameter %qD declared here", olddecl
);
3927 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3928 ORIG_LEVEL, DECL, and TYPE. */
3931 build_template_parm_index (int index
,
3937 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3938 TEMPLATE_PARM_IDX (t
) = index
;
3939 TEMPLATE_PARM_LEVEL (t
) = level
;
3940 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3941 TEMPLATE_PARM_DECL (t
) = decl
;
3942 TREE_TYPE (t
) = type
;
3943 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3944 TREE_READONLY (t
) = TREE_READONLY (decl
);
3949 /* Find the canonical type parameter for the given template type
3950 parameter. Returns the canonical type parameter, which may be TYPE
3951 if no such parameter existed. */
3954 canonical_type_parameter (tree type
)
3957 int idx
= TEMPLATE_TYPE_IDX (type
);
3958 if (!canonical_template_parms
)
3959 vec_alloc (canonical_template_parms
, idx
+1);
3961 while (canonical_template_parms
->length () <= (unsigned)idx
)
3962 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3964 list
= (*canonical_template_parms
)[idx
];
3965 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3966 list
= TREE_CHAIN (list
);
3969 return TREE_VALUE (list
);
3972 (*canonical_template_parms
)[idx
]
3973 = tree_cons (NULL_TREE
, type
,
3974 (*canonical_template_parms
)[idx
]);
3979 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3980 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3981 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3982 new one is created. */
3985 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3986 tsubst_flags_t complain
)
3988 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3989 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3990 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3991 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3993 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3996 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3997 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3998 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3999 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4000 DECL_ARTIFICIAL (decl
) = 1;
4001 SET_DECL_TEMPLATE_PARM_P (decl
);
4003 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4004 TEMPLATE_PARM_LEVEL (index
) - levels
,
4005 TEMPLATE_PARM_ORIG_LEVEL (index
),
4007 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4008 TEMPLATE_PARM_PARAMETER_PACK (t
)
4009 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4011 /* Template template parameters need this. */
4012 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4014 DECL_TEMPLATE_RESULT (decl
)
4015 = build_decl (DECL_SOURCE_LOCATION (decl
),
4016 TYPE_DECL
, DECL_NAME (decl
), type
);
4017 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4018 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4019 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4023 return TEMPLATE_PARM_DESCENDANTS (index
);
4026 /* Process information from new template parameter PARM and append it
4027 to the LIST being built. This new parameter is a non-type
4028 parameter iff IS_NON_TYPE is true. This new parameter is a
4029 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4033 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4034 bool is_non_type
, bool is_parameter_pack
)
4039 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4040 tree defval
= TREE_PURPOSE (parm
);
4041 tree constr
= TREE_TYPE (parm
);
4045 tree p
= tree_last (list
);
4047 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4050 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4051 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4053 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4061 parm
= TREE_VALUE (parm
);
4063 SET_DECL_TEMPLATE_PARM_P (parm
);
4065 if (TREE_TYPE (parm
) != error_mark_node
)
4069 The top-level cv-qualifiers on the template-parameter are
4070 ignored when determining its type. */
4071 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4072 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4073 TREE_TYPE (parm
) = error_mark_node
;
4074 else if (uses_parameter_packs (TREE_TYPE (parm
))
4075 && !is_parameter_pack
4076 /* If we're in a nested template parameter list, the template
4077 template parameter could be a parameter pack. */
4078 && processing_template_parmlist
== 1)
4080 /* This template parameter is not a parameter pack, but it
4081 should be. Complain about "bare" parameter packs. */
4082 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4084 /* Recover by calling this a parameter pack. */
4085 is_parameter_pack
= true;
4089 /* A template parameter is not modifiable. */
4090 TREE_CONSTANT (parm
) = 1;
4091 TREE_READONLY (parm
) = 1;
4092 decl
= build_decl (parm_loc
,
4093 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4094 TREE_CONSTANT (decl
) = 1;
4095 TREE_READONLY (decl
) = 1;
4096 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4097 = build_template_parm_index (idx
, processing_template_decl
,
4098 processing_template_decl
,
4099 decl
, TREE_TYPE (parm
));
4101 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4102 = is_parameter_pack
;
4107 parm
= TREE_VALUE (TREE_VALUE (parm
));
4109 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4111 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4112 /* This is for distinguishing between real templates and template
4113 template parameters */
4114 TREE_TYPE (parm
) = t
;
4115 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4120 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4121 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4122 decl
= build_decl (parm_loc
,
4123 TYPE_DECL
, parm
, t
);
4126 TYPE_NAME (t
) = decl
;
4127 TYPE_STUB_DECL (t
) = decl
;
4129 TEMPLATE_TYPE_PARM_INDEX (t
)
4130 = build_template_parm_index (idx
, processing_template_decl
,
4131 processing_template_decl
,
4132 decl
, TREE_TYPE (parm
));
4133 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4134 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4136 DECL_ARTIFICIAL (decl
) = 1;
4137 SET_DECL_TEMPLATE_PARM_P (decl
);
4139 /* Build requirements for the type/template parameter.
4140 This must be done after SET_DECL_TEMPLATE_PARM_P or
4141 process_template_parm could fail. */
4142 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4146 /* Build the parameter node linking the parameter declaration,
4147 its default argument (if any), and its constraints (if any). */
4148 parm
= build_tree_list (defval
, parm
);
4149 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4151 return chainon (list
, parm
);
4154 /* The end of a template parameter list has been reached. Process the
4155 tree list into a parameter vector, converting each parameter into a more
4156 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4160 end_template_parm_list (tree parms
)
4164 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4166 /* Pop the dummy parameter level and add the real one. */
4167 current_template_parms
= TREE_CHAIN (current_template_parms
);
4169 current_template_parms
4170 = tree_cons (size_int (processing_template_decl
),
4171 saved_parmlist
, current_template_parms
);
4173 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4175 next
= TREE_CHAIN (parm
);
4176 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4177 TREE_CHAIN (parm
) = NULL_TREE
;
4180 --processing_template_parmlist
;
4182 return saved_parmlist
;
4185 // Explicitly indicate the end of the template parameter list. We assume
4186 // that the current template parameters have been constructed and/or
4187 // managed explicitly, as when creating new template template parameters
4188 // from a shorthand constraint.
4190 end_template_parm_list ()
4192 --processing_template_parmlist
;
4195 /* end_template_decl is called after a template declaration is seen. */
4198 end_template_decl (void)
4200 reset_specialization ();
4202 if (! processing_template_decl
)
4205 /* This matches the pushlevel in begin_template_parm_list. */
4208 --processing_template_decl
;
4209 current_template_parms
= TREE_CHAIN (current_template_parms
);
4212 /* Takes a TREE_LIST representing a template parameter and convert it
4213 into an argument suitable to be passed to the type substitution
4214 functions. Note that If the TREE_LIST contains an error_mark
4215 node, the returned argument is error_mark_node. */
4218 template_parm_to_arg (tree t
)
4222 || TREE_CODE (t
) != TREE_LIST
)
4225 if (error_operand_p (TREE_VALUE (t
)))
4226 return error_mark_node
;
4230 if (TREE_CODE (t
) == TYPE_DECL
4231 || TREE_CODE (t
) == TEMPLATE_DECL
)
4235 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4237 /* Turn this argument into a TYPE_ARGUMENT_PACK
4238 with a single element, which expands T. */
4239 tree vec
= make_tree_vec (1);
4241 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4243 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4245 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4246 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4251 t
= DECL_INITIAL (t
);
4253 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4255 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4256 with a single element, which expands T. */
4257 tree vec
= make_tree_vec (1);
4258 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4260 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4262 t
= convert_from_reference (t
);
4263 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4265 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4266 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4267 TREE_TYPE (t
) = type
;
4270 t
= convert_from_reference (t
);
4275 /* Given a set of template parameters, return them as a set of template
4276 arguments. The template parameters are represented as a TREE_VEC, in
4277 the form documented in cp-tree.h for template arguments. */
4280 template_parms_to_args (tree parms
)
4283 tree args
= NULL_TREE
;
4284 int length
= TMPL_PARMS_DEPTH (parms
);
4287 /* If there is only one level of template parameters, we do not
4288 create a TREE_VEC of TREE_VECs. Instead, we return a single
4289 TREE_VEC containing the arguments. */
4291 args
= make_tree_vec (length
);
4293 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4295 tree a
= copy_node (TREE_VALUE (header
));
4298 TREE_TYPE (a
) = NULL_TREE
;
4299 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4300 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4303 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4306 TREE_VEC_ELT (args
, --l
) = a
;
4314 /* Within the declaration of a template, return the currently active
4315 template parameters as an argument TREE_VEC. */
4318 current_template_args (void)
4320 return template_parms_to_args (current_template_parms
);
4323 /* Update the declared TYPE by doing any lookups which were thought to be
4324 dependent, but are not now that we know the SCOPE of the declarator. */
4327 maybe_update_decl_type (tree orig_type
, tree scope
)
4329 tree type
= orig_type
;
4331 if (type
== NULL_TREE
)
4334 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4335 type
= TREE_TYPE (type
);
4337 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4338 && dependent_type_p (type
)
4339 /* Don't bother building up the args in this case. */
4340 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4342 /* tsubst in the args corresponding to the template parameters,
4343 including auto if present. Most things will be unchanged, but
4344 make_typename_type and tsubst_qualified_id will resolve
4345 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4346 tree args
= current_template_args ();
4347 tree auto_node
= type_uses_auto (type
);
4351 tree auto_vec
= make_tree_vec (1);
4352 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4353 args
= add_to_template_args (args
, auto_vec
);
4355 pushed
= push_scope (scope
);
4356 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4361 if (type
== error_mark_node
)
4364 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4366 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4369 type
= TYPE_NAME (type
);
4374 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4375 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4376 the new template is a member template. */
4379 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4381 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4382 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4383 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4384 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4385 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4390 struct template_parm_data
4392 /* The level of the template parameters we are currently
4396 /* The index of the specialization argument we are currently
4400 /* An array whose size is the number of template parameters. The
4401 elements are nonzero if the parameter has been used in any one
4402 of the arguments processed so far. */
4405 /* An array whose size is the number of template arguments. The
4406 elements are nonzero if the argument makes use of template
4407 parameters of this level. */
4408 int* arg_uses_template_parms
;
4411 /* Subroutine of push_template_decl used to see if each template
4412 parameter in a partial specialization is used in the explicit
4413 argument list. If T is of the LEVEL given in DATA (which is
4414 treated as a template_parm_data*), then DATA->PARMS is marked
4418 mark_template_parm (tree t
, void* data
)
4422 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4424 template_parm_level_and_index (t
, &level
, &idx
);
4426 if (level
== tpd
->level
)
4428 tpd
->parms
[idx
] = 1;
4429 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4432 /* Return zero so that for_each_template_parm will continue the
4433 traversal of the tree; we want to mark *every* template parm. */
4437 /* Process the partial specialization DECL. */
4440 process_partial_specialization (tree decl
)
4442 tree type
= TREE_TYPE (decl
);
4443 tree tinfo
= get_template_info (decl
);
4444 tree maintmpl
= TI_TEMPLATE (tinfo
);
4445 tree specargs
= TI_ARGS (tinfo
);
4446 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4447 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4450 int nargs
= TREE_VEC_LENGTH (inner_args
);
4453 bool did_error_intro
= false;
4454 struct template_parm_data tpd
;
4455 struct template_parm_data tpd2
;
4457 gcc_assert (current_template_parms
);
4459 /* A concept cannot be specialized. */
4460 if (flag_concepts
&& variable_concept_p (maintmpl
))
4462 error ("specialization of variable concept %q#D", maintmpl
);
4463 return error_mark_node
;
4466 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4467 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4469 /* We check that each of the template parameters given in the
4470 partial specialization is used in the argument list to the
4471 specialization. For example:
4473 template <class T> struct S;
4474 template <class T> struct S<T*>;
4476 The second declaration is OK because `T*' uses the template
4477 parameter T, whereas
4479 template <class T> struct S<int>;
4481 is no good. Even trickier is:
4492 The S2<T> declaration is actually invalid; it is a
4493 full-specialization. Of course,
4496 struct S2<T (*)(U)>;
4498 or some such would have been OK. */
4499 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4500 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4501 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4503 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4504 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4505 for (i
= 0; i
< nargs
; ++i
)
4507 tpd
.current_arg
= i
;
4508 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4509 &mark_template_parm
,
4512 /*include_nondeduced_p=*/false);
4514 for (i
= 0; i
< ntparms
; ++i
)
4515 if (tpd
.parms
[i
] == 0)
4517 /* One of the template parms was not used in a deduced context in the
4519 if (!did_error_intro
)
4521 error ("template parameters not deducible in "
4522 "partial specialization:");
4523 did_error_intro
= true;
4526 inform (input_location
, " %qD",
4527 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4530 if (did_error_intro
)
4531 return error_mark_node
;
4533 /* [temp.class.spec]
4535 The argument list of the specialization shall not be identical to
4536 the implicit argument list of the primary template. */
4538 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4539 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4541 || !strictly_subsumes (current_template_constraints (),
4542 get_constraints (maintmpl
))))
4545 error ("partial specialization %q+D does not specialize "
4546 "any template arguments", decl
);
4548 error ("partial specialization %q+D does not specialize any "
4549 "template arguments and is not more constrained than", decl
);
4550 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4553 /* A partial specialization that replaces multiple parameters of the
4554 primary template with a pack expansion is less specialized for those
4556 if (nargs
< DECL_NTPARMS (maintmpl
))
4558 error ("partial specialization is not more specialized than the "
4559 "primary template because it replaces multiple parameters "
4560 "with a pack expansion");
4561 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4565 /* [temp.class.spec]
4567 A partially specialized non-type argument expression shall not
4568 involve template parameters of the partial specialization except
4569 when the argument expression is a simple identifier.
4571 The type of a template parameter corresponding to a specialized
4572 non-type argument shall not be dependent on a parameter of the
4575 Also, we verify that pack expansions only occur at the
4576 end of the argument list. */
4577 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4579 for (i
= 0; i
< nargs
; ++i
)
4581 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4582 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4583 tree packed_args
= NULL_TREE
;
4586 if (ARGUMENT_PACK_P (arg
))
4588 /* Extract the arguments from the argument pack. We'll be
4589 iterating over these in the following loop. */
4590 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4591 len
= TREE_VEC_LENGTH (packed_args
);
4594 for (j
= 0; j
< len
; j
++)
4597 /* Get the Jth argument in the parameter pack. */
4598 arg
= TREE_VEC_ELT (packed_args
, j
);
4600 if (PACK_EXPANSION_P (arg
))
4602 /* Pack expansions must come at the end of the
4604 if ((packed_args
&& j
< len
- 1)
4605 || (!packed_args
&& i
< nargs
- 1))
4607 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4608 error ("parameter pack argument %qE must be at the "
4609 "end of the template argument list", arg
);
4611 error ("parameter pack argument %qT must be at the "
4612 "end of the template argument list", arg
);
4616 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4617 /* We only care about the pattern. */
4618 arg
= PACK_EXPANSION_PATTERN (arg
);
4620 if (/* These first two lines are the `non-type' bit. */
4622 && TREE_CODE (arg
) != TEMPLATE_DECL
4623 /* This next two lines are the `argument expression is not just a
4624 simple identifier' condition and also the `specialized
4625 non-type argument' bit. */
4626 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4627 && !(REFERENCE_REF_P (arg
)
4628 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4630 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4631 || (packed_args
&& uses_template_parms (arg
)))
4632 error ("template argument %qE involves template parameter(s)",
4636 /* Look at the corresponding template parameter,
4637 marking which template parameters its type depends
4639 tree type
= TREE_TYPE (parm
);
4643 /* We haven't yet initialized TPD2. Do so now. */
4644 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4645 /* The number of parameters here is the number in the
4646 main template, which, as checked in the assertion
4648 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4650 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4653 /* Mark the template parameters. But this time, we're
4654 looking for the template parameters of the main
4655 template, not in the specialization. */
4656 tpd2
.current_arg
= i
;
4657 tpd2
.arg_uses_template_parms
[i
] = 0;
4658 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4659 for_each_template_parm (type
,
4660 &mark_template_parm
,
4663 /*include_nondeduced_p=*/false);
4665 if (tpd2
.arg_uses_template_parms
[i
])
4667 /* The type depended on some template parameters.
4668 If they are fully specialized in the
4669 specialization, that's OK. */
4672 for (j
= 0; j
< nargs
; ++j
)
4673 if (tpd2
.parms
[j
] != 0
4674 && tpd
.arg_uses_template_parms
[j
])
4677 error_n (input_location
, count
,
4678 "type %qT of template argument %qE depends "
4679 "on a template parameter",
4680 "type %qT of template argument %qE depends "
4681 "on template parameters",
4690 /* We should only get here once. */
4691 if (TREE_CODE (decl
) == TYPE_DECL
)
4692 gcc_assert (!COMPLETE_TYPE_P (type
));
4694 // Build the template decl.
4695 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4696 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4697 TREE_TYPE (tmpl
) = type
;
4698 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4699 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4700 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4701 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4704 /* We didn't register this in check_explicit_specialization so we could
4705 wait until the constraints were set. */
4706 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4708 associate_classtype_constraints (type
);
4710 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4711 = tree_cons (specargs
, tmpl
,
4712 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4713 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4715 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4716 inst
= TREE_CHAIN (inst
))
4718 tree instance
= TREE_VALUE (inst
);
4719 if (TYPE_P (instance
)
4720 ? (COMPLETE_TYPE_P (instance
)
4721 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4722 : DECL_TEMPLATE_INSTANTIATION (instance
))
4724 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4725 tree inst_decl
= (DECL_P (instance
)
4726 ? instance
: TYPE_NAME (instance
));
4729 else if (spec
== error_mark_node
)
4730 permerror (input_location
,
4731 "declaration of %qD ambiguates earlier template "
4732 "instantiation for %qD", decl
, inst_decl
);
4733 else if (TREE_VALUE (spec
) == tmpl
)
4734 permerror (input_location
,
4735 "partial specialization of %qD after instantiation "
4736 "of %qD", decl
, inst_decl
);
4743 /* PARM is a template parameter of some form; return the corresponding
4744 TEMPLATE_PARM_INDEX. */
4747 get_template_parm_index (tree parm
)
4749 if (TREE_CODE (parm
) == PARM_DECL
4750 || TREE_CODE (parm
) == CONST_DECL
)
4751 parm
= DECL_INITIAL (parm
);
4752 else if (TREE_CODE (parm
) == TYPE_DECL
4753 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4754 parm
= TREE_TYPE (parm
);
4755 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4756 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4757 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4758 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4759 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4763 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4764 parameter packs used by the template parameter PARM. */
4767 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4769 /* A type parm can't refer to another parm. */
4770 if (TREE_CODE (parm
) == TYPE_DECL
)
4772 else if (TREE_CODE (parm
) == PARM_DECL
)
4774 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4779 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4781 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4782 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4783 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4786 /* PARM is a template parameter pack. Return any parameter packs used in
4787 its type or the type of any of its template parameters. If there are
4788 any such packs, it will be instantiated into a fixed template parameter
4789 list by partial instantiation rather than be fully deduced. */
4792 fixed_parameter_pack_p (tree parm
)
4794 /* This can only be true in a member template. */
4795 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4797 /* This can only be true for a parameter pack. */
4798 if (!template_parameter_pack_p (parm
))
4800 /* A type parm can't refer to another parm. */
4801 if (TREE_CODE (parm
) == TYPE_DECL
)
4804 tree parameter_packs
= NULL_TREE
;
4805 struct find_parameter_pack_data ppd
;
4806 ppd
.parameter_packs
= ¶meter_packs
;
4807 ppd
.visited
= new hash_set
<tree
>;
4808 ppd
.type_pack_expansion_p
= false;
4810 fixed_parameter_pack_p_1 (parm
, &ppd
);
4813 return parameter_packs
;
4816 /* Check that a template declaration's use of default arguments and
4817 parameter packs is not invalid. Here, PARMS are the template
4818 parameters. IS_PRIMARY is true if DECL is the thing declared by
4819 a primary template. IS_PARTIAL is true if DECL is a partial
4822 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4823 declaration (but not a definition); 1 indicates a declaration, 2
4824 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4825 emitted for extraneous default arguments.
4827 Returns TRUE if there were no errors found, FALSE otherwise. */
4830 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4831 bool is_partial
, int is_friend_decl
)
4834 int last_level_to_check
;
4836 bool no_errors
= true;
4840 A default template-argument shall not be specified in a
4841 function template declaration or a function template definition, nor
4842 in the template-parameter-list of the definition of a member of a
4845 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4846 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4847 /* You can't have a function template declaration in a local
4848 scope, nor you can you define a member of a class template in a
4852 if ((TREE_CODE (decl
) == TYPE_DECL
4854 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4855 || (TREE_CODE (decl
) == FUNCTION_DECL
4856 && LAMBDA_FUNCTION_P (decl
)))
4857 /* A lambda doesn't have an explicit declaration; don't complain
4858 about the parms of the enclosing class. */
4861 if (current_class_type
4862 && !TYPE_BEING_DEFINED (current_class_type
)
4863 && DECL_LANG_SPECIFIC (decl
)
4864 && DECL_DECLARES_FUNCTION_P (decl
)
4865 /* If this is either a friend defined in the scope of the class
4866 or a member function. */
4867 && (DECL_FUNCTION_MEMBER_P (decl
)
4868 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4869 : DECL_FRIEND_CONTEXT (decl
)
4870 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4872 /* And, if it was a member function, it really was defined in
4873 the scope of the class. */
4874 && (!DECL_FUNCTION_MEMBER_P (decl
)
4875 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4876 /* We already checked these parameters when the template was
4877 declared, so there's no need to do it again now. This function
4878 was defined in class scope, but we're processing its body now
4879 that the class is complete. */
4882 /* Core issue 226 (C++0x only): the following only applies to class
4885 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4889 If a template-parameter has a default template-argument, all
4890 subsequent template-parameters shall have a default
4891 template-argument supplied. */
4892 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4894 tree inner_parms
= TREE_VALUE (parm_level
);
4895 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4896 int seen_def_arg_p
= 0;
4899 for (i
= 0; i
< ntparms
; ++i
)
4901 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4903 if (parm
== error_mark_node
)
4906 if (TREE_PURPOSE (parm
))
4908 else if (seen_def_arg_p
4909 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4911 error ("no default argument for %qD", TREE_VALUE (parm
));
4912 /* For better subsequent error-recovery, we indicate that
4913 there should have been a default argument. */
4914 TREE_PURPOSE (parm
) = error_mark_node
;
4917 else if (!is_partial
4919 /* Don't complain about an enclosing partial
4921 && parm_level
== parms
4922 && TREE_CODE (decl
) == TYPE_DECL
4924 && template_parameter_pack_p (TREE_VALUE (parm
))
4925 /* A fixed parameter pack will be partially
4926 instantiated into a fixed length list. */
4927 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4929 /* A primary class template can only have one
4930 parameter pack, at the end of the template
4933 error ("parameter pack %q+D must be at the end of the"
4934 " template parameter list", TREE_VALUE (parm
));
4936 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4944 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4948 /* For an ordinary class template, default template arguments are
4949 allowed at the innermost level, e.g.:
4950 template <class T = int>
4952 but, in a partial specialization, they're not allowed even
4953 there, as we have in [temp.class.spec]:
4955 The template parameter list of a specialization shall not
4956 contain default template argument values.
4958 So, for a partial specialization, or for a function template
4959 (in C++98/C++03), we look at all of them. */
4962 /* But, for a primary class template that is not a partial
4963 specialization we look at all template parameters except the
4965 parms
= TREE_CHAIN (parms
);
4967 /* Figure out what error message to issue. */
4968 if (is_friend_decl
== 2)
4969 msg
= G_("default template arguments may not be used in function template "
4970 "friend re-declaration");
4971 else if (is_friend_decl
)
4972 msg
= G_("default template arguments may not be used in function template "
4973 "friend declarations");
4974 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4975 msg
= G_("default template arguments may not be used in function templates "
4976 "without -std=c++11 or -std=gnu++11");
4977 else if (is_partial
)
4978 msg
= G_("default template arguments may not be used in "
4979 "partial specializations");
4980 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
4981 msg
= G_("default argument for template parameter for class enclosing %qD");
4983 /* Per [temp.param]/9, "A default template-argument shall not be
4984 specified in the template-parameter-lists of the definition of
4985 a member of a class template that appears outside of the member's
4986 class.", thus if we aren't handling a member of a class template
4987 there is no need to examine the parameters. */
4990 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4991 /* If we're inside a class definition, there's no need to
4992 examine the parameters to the class itself. On the one
4993 hand, they will be checked when the class is defined, and,
4994 on the other, default arguments are valid in things like:
4995 template <class T = double>
4996 struct S { template <class U> void f(U); };
4997 Here the default argument for `S' has no bearing on the
4998 declaration of `f'. */
4999 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5001 /* Check everything. */
5002 last_level_to_check
= 0;
5004 for (parm_level
= parms
;
5005 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5006 parm_level
= TREE_CHAIN (parm_level
))
5008 tree inner_parms
= TREE_VALUE (parm_level
);
5012 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5013 for (i
= 0; i
< ntparms
; ++i
)
5015 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5018 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5023 if (is_friend_decl
== 2)
5030 /* Clear out the default argument so that we are not
5032 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5036 /* At this point, if we're still interested in issuing messages,
5037 they must apply to classes surrounding the object declared. */
5039 msg
= G_("default argument for template parameter for class "
5046 /* Worker for push_template_decl_real, called via
5047 for_each_template_parm. DATA is really an int, indicating the
5048 level of the parameters we are interested in. If T is a template
5049 parameter of that level, return nonzero. */
5052 template_parm_this_level_p (tree t
, void* data
)
5054 int this_level
= *(int *)data
;
5057 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5058 level
= TEMPLATE_PARM_LEVEL (t
);
5060 level
= TEMPLATE_TYPE_LEVEL (t
);
5061 return level
== this_level
;
5064 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5065 DATA is really an int, indicating the innermost outer level of parameters.
5066 If T is a template parameter of that level or further out, return
5070 template_parm_outer_level (tree t
, void *data
)
5072 int this_level
= *(int *)data
;
5075 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5076 level
= TEMPLATE_PARM_LEVEL (t
);
5078 level
= TEMPLATE_TYPE_LEVEL (t
);
5079 return level
<= this_level
;
5082 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5083 parameters given by current_template_args, or reuses a
5084 previously existing one, if appropriate. Returns the DECL, or an
5085 equivalent one, if it is replaced via a call to duplicate_decls.
5087 If IS_FRIEND is true, DECL is a friend declaration. */
5090 push_template_decl_real (tree decl
, bool is_friend
)
5098 int new_template_p
= 0;
5099 /* True if the template is a member template, in the sense of
5101 bool member_template_p
= false;
5103 if (decl
== error_mark_node
|| !current_template_parms
)
5104 return error_mark_node
;
5106 /* See if this is a partial specialization. */
5107 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5108 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5109 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5111 && DECL_LANG_SPECIFIC (decl
)
5112 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5113 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5115 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5119 /* For a friend, we want the context of the friend function, not
5120 the type of which it is a friend. */
5121 ctx
= CP_DECL_CONTEXT (decl
);
5122 else if (CP_DECL_CONTEXT (decl
)
5123 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5124 /* In the case of a virtual function, we want the class in which
5126 ctx
= CP_DECL_CONTEXT (decl
);
5128 /* Otherwise, if we're currently defining some class, the DECL
5129 is assumed to be a member of the class. */
5130 ctx
= current_scope ();
5132 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5135 if (!DECL_CONTEXT (decl
))
5136 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5138 /* See if this is a primary template. */
5139 if (is_friend
&& ctx
5140 && uses_template_parms_level (ctx
, processing_template_decl
))
5141 /* A friend template that specifies a class context, i.e.
5142 template <typename T> friend void A<T>::f();
5145 else if (TREE_CODE (decl
) == TYPE_DECL
5146 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5149 is_primary
= template_parm_scope_p ();
5153 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5155 if (DECL_CLASS_SCOPE_P (decl
))
5156 member_template_p
= true;
5157 if (TREE_CODE (decl
) == TYPE_DECL
5158 && anon_aggrname_p (DECL_NAME (decl
)))
5160 error ("template class without a name");
5161 return error_mark_node
;
5163 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5165 if (member_template_p
)
5167 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5168 error ("member template %qD may not have virt-specifiers", decl
);
5170 if (DECL_DESTRUCTOR_P (decl
))
5174 A destructor shall not be a member template. */
5175 error ("destructor %qD declared as member template", decl
);
5176 return error_mark_node
;
5178 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
5179 && (!prototype_p (TREE_TYPE (decl
))
5180 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5181 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5182 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
5183 == void_list_node
)))
5185 /* [basic.stc.dynamic.allocation]
5187 An allocation function can be a function
5188 template. ... Template allocation functions shall
5189 have two or more parameters. */
5190 error ("invalid template declaration of %qD", decl
);
5191 return error_mark_node
;
5194 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5195 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5197 else if (TREE_CODE (decl
) == TYPE_DECL
5198 && TYPE_DECL_ALIAS_P (decl
))
5199 /* alias-declaration */
5200 gcc_assert (!DECL_ARTIFICIAL (decl
));
5201 else if (VAR_P (decl
))
5202 /* C++14 variable template. */;
5205 error ("template declaration of %q#D", decl
);
5206 return error_mark_node
;
5210 /* Check to see that the rules regarding the use of default
5211 arguments are not being violated. */
5212 check_default_tmpl_args (decl
, current_template_parms
,
5213 is_primary
, is_partial
, /*is_friend_decl=*/0);
5215 /* Ensure that there are no parameter packs in the type of this
5216 declaration that have not been expanded. */
5217 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5219 /* Check each of the arguments individually to see if there are
5220 any bare parameter packs. */
5221 tree type
= TREE_TYPE (decl
);
5222 tree arg
= DECL_ARGUMENTS (decl
);
5223 tree argtype
= TYPE_ARG_TYPES (type
);
5225 while (arg
&& argtype
)
5227 if (!DECL_PACK_P (arg
)
5228 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5230 /* This is a PARM_DECL that contains unexpanded parameter
5231 packs. We have already complained about this in the
5232 check_for_bare_parameter_packs call, so just replace
5233 these types with ERROR_MARK_NODE. */
5234 TREE_TYPE (arg
) = error_mark_node
;
5235 TREE_VALUE (argtype
) = error_mark_node
;
5238 arg
= DECL_CHAIN (arg
);
5239 argtype
= TREE_CHAIN (argtype
);
5242 /* Check for bare parameter packs in the return type and the
5243 exception specifiers. */
5244 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5245 /* Errors were already issued, set return type to int
5246 as the frontend doesn't expect error_mark_node as
5248 TREE_TYPE (type
) = integer_type_node
;
5249 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5250 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5252 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5253 && TYPE_DECL_ALIAS_P (decl
))
5254 ? DECL_ORIGINAL_TYPE (decl
)
5255 : TREE_TYPE (decl
)))
5257 TREE_TYPE (decl
) = error_mark_node
;
5258 return error_mark_node
;
5262 return process_partial_specialization (decl
);
5264 args
= current_template_args ();
5267 || TREE_CODE (ctx
) == FUNCTION_DECL
5268 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5269 || (TREE_CODE (decl
) == TYPE_DECL
5270 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5271 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5273 if (DECL_LANG_SPECIFIC (decl
)
5274 && DECL_TEMPLATE_INFO (decl
)
5275 && DECL_TI_TEMPLATE (decl
))
5276 tmpl
= DECL_TI_TEMPLATE (decl
);
5277 /* If DECL is a TYPE_DECL for a class-template, then there won't
5278 be DECL_LANG_SPECIFIC. The information equivalent to
5279 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5280 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5281 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5282 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5284 /* Since a template declaration already existed for this
5285 class-type, we must be redeclaring it here. Make sure
5286 that the redeclaration is valid. */
5287 redeclare_class_template (TREE_TYPE (decl
),
5288 current_template_parms
,
5289 current_template_constraints ());
5290 /* We don't need to create a new TEMPLATE_DECL; just use the
5291 one we already had. */
5292 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5296 tmpl
= build_template_decl (decl
, current_template_parms
,
5300 if (DECL_LANG_SPECIFIC (decl
)
5301 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5303 /* A specialization of a member template of a template
5305 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5306 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5307 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5313 tree a
, t
, current
, parms
;
5315 tree tinfo
= get_template_info (decl
);
5319 error ("template definition of non-template %q#D", decl
);
5320 return error_mark_node
;
5323 tmpl
= TI_TEMPLATE (tinfo
);
5325 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5326 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5327 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5328 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5332 /* The declaration is a specialization of a member
5333 template, declared outside the class. Therefore, the
5334 innermost template arguments will be NULL, so we
5335 replace them with the arguments determined by the
5336 earlier call to check_explicit_specialization. */
5337 args
= DECL_TI_ARGS (decl
);
5340 = build_template_decl (decl
, current_template_parms
,
5342 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5343 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5344 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5345 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5346 DECL_TEMPLATE_INFO (new_tmpl
)
5347 = build_template_info (tmpl
, args
);
5349 register_specialization (new_tmpl
,
5350 most_general_template (tmpl
),
5356 /* Make sure the template headers we got make sense. */
5358 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5359 i
= TMPL_PARMS_DEPTH (parms
);
5360 if (TMPL_ARGS_DEPTH (args
) != i
)
5362 error ("expected %d levels of template parms for %q#D, got %d",
5363 i
, decl
, TMPL_ARGS_DEPTH (args
));
5364 DECL_INTERFACE_KNOWN (decl
) = 1;
5365 return error_mark_node
;
5368 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5370 a
= TMPL_ARGS_LEVEL (args
, i
);
5371 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5373 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5375 if (current
== decl
)
5376 error ("got %d template parameters for %q#D",
5377 TREE_VEC_LENGTH (a
), decl
);
5379 error ("got %d template parameters for %q#T",
5380 TREE_VEC_LENGTH (a
), current
);
5381 error (" but %d required", TREE_VEC_LENGTH (t
));
5382 /* Avoid crash in import_export_decl. */
5383 DECL_INTERFACE_KNOWN (decl
) = 1;
5384 return error_mark_node
;
5387 if (current
== decl
)
5389 else if (current
== NULL_TREE
)
5390 /* Can happen in erroneous input. */
5393 current
= get_containing_scope (current
);
5396 /* Check that the parms are used in the appropriate qualifying scopes
5397 in the declarator. */
5398 if (!comp_template_args
5400 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5403 template arguments to %qD do not match original template %qD",
5404 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5405 if (!uses_template_parms (TI_ARGS (tinfo
)))
5406 inform (input_location
, "use template<> for an explicit specialization");
5407 /* Avoid crash in import_export_decl. */
5408 DECL_INTERFACE_KNOWN (decl
) = 1;
5409 return error_mark_node
;
5413 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5414 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5416 /* Push template declarations for global functions and types. Note
5417 that we do not try to push a global template friend declared in a
5418 template class; such a thing may well depend on the template
5419 parameters of the class. */
5420 if (new_template_p
&& !ctx
5421 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5423 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5424 if (tmpl
== error_mark_node
)
5425 return error_mark_node
;
5427 /* Hide template friend classes that haven't been declared yet. */
5428 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5430 DECL_ANTICIPATED (tmpl
) = 1;
5431 DECL_FRIEND_P (tmpl
) = 1;
5437 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5440 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5441 if (DECL_CONV_FN_P (tmpl
))
5443 int depth
= TMPL_PARMS_DEPTH (parms
);
5445 /* It is a conversion operator. See if the type converted to
5446 depends on innermost template operands. */
5448 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5450 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5453 /* Give template template parms a DECL_CONTEXT of the template
5454 for which they are a parameter. */
5455 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5456 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5458 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5459 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5460 DECL_CONTEXT (parm
) = tmpl
;
5463 if (TREE_CODE (decl
) == TYPE_DECL
5464 && TYPE_DECL_ALIAS_P (decl
)
5465 && complex_alias_template_p (tmpl
))
5466 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5469 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5470 back to its most general template. If TMPL is a specialization,
5471 ARGS may only have the innermost set of arguments. Add the missing
5472 argument levels if necessary. */
5473 if (DECL_TEMPLATE_INFO (tmpl
))
5474 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5476 info
= build_template_info (tmpl
, args
);
5478 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5479 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5482 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5483 retrofit_lang_decl (decl
);
5484 if (DECL_LANG_SPECIFIC (decl
))
5485 DECL_TEMPLATE_INFO (decl
) = info
;
5488 if (flag_implicit_templates
5490 && TREE_PUBLIC (decl
)
5491 && VAR_OR_FUNCTION_DECL_P (decl
))
5492 /* Set DECL_COMDAT on template instantiations; if we force
5493 them to be emitted by explicit instantiation or -frepo,
5494 mark_needed will tell cgraph to do the right thing. */
5495 DECL_COMDAT (decl
) = true;
5497 return DECL_TEMPLATE_RESULT (tmpl
);
5501 push_template_decl (tree decl
)
5503 return push_template_decl_real (decl
, false);
5506 /* FN is an inheriting constructor that inherits from the constructor
5507 template INHERITED; turn FN into a constructor template with a matching
5511 add_inherited_template_parms (tree fn
, tree inherited
)
5514 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5515 inner_parms
= copy_node (inner_parms
);
5517 = tree_cons (size_int (processing_template_decl
+ 1),
5518 inner_parms
, current_template_parms
);
5519 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5520 tree args
= template_parms_to_args (parms
);
5521 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5522 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5523 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5524 DECL_ARTIFICIAL (tmpl
) = true;
5525 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5529 /* Called when a class template TYPE is redeclared with the indicated
5530 template PARMS, e.g.:
5532 template <class T> struct S;
5533 template <class T> struct S {}; */
5536 redeclare_class_template (tree type
, tree parms
, tree cons
)
5542 if (!TYPE_TEMPLATE_INFO (type
))
5544 error ("%qT is not a template type", type
);
5548 tmpl
= TYPE_TI_TEMPLATE (type
);
5549 if (!PRIMARY_TEMPLATE_P (tmpl
))
5550 /* The type is nested in some template class. Nothing to worry
5551 about here; there are no new template parameters for the nested
5557 error ("template specifiers not specified in declaration of %qD",
5562 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5563 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5565 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5567 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5568 "redeclared with %d template parameter",
5569 "redeclared with %d template parameters",
5570 TREE_VEC_LENGTH (parms
));
5571 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5572 "previous declaration %qD used %d template parameter",
5573 "previous declaration %qD used %d template parameters",
5574 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5578 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5585 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5586 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5589 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5590 if (error_operand_p (tmpl_parm
))
5593 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5594 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5595 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5597 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5599 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5600 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5601 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5602 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5603 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5604 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5605 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5606 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5607 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5609 error ("template parameter %q+#D", tmpl_parm
);
5610 error ("redeclared here as %q#D", parm
);
5614 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5616 /* We have in [temp.param]:
5618 A template-parameter may not be given default arguments
5619 by two different declarations in the same scope. */
5620 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5621 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5622 "original definition appeared here");
5626 if (parm_default
!= NULL_TREE
)
5627 /* Update the previous template parameters (which are the ones
5628 that will really count) with the new default value. */
5629 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5630 else if (tmpl_default
!= NULL_TREE
)
5631 /* Update the new parameters, too; they'll be used as the
5632 parameters for any members. */
5633 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5635 /* Give each template template parm in this redeclaration a
5636 DECL_CONTEXT of the template for which they are a parameter. */
5637 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5639 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5640 DECL_CONTEXT (parm
) = tmpl
;
5644 // Cannot redeclare a class template with a different set of constraints.
5645 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5647 error_at (input_location
, "redeclaration %q#D with different "
5648 "constraints", tmpl
);
5649 inform (DECL_SOURCE_LOCATION (tmpl
),
5650 "original declaration appeared here");
5656 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5657 to be used when the caller has already checked
5658 (processing_template_decl
5659 && !instantiation_dependent_expression_p (expr)
5660 && potential_constant_expression (expr))
5661 and cleared processing_template_decl. */
5664 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5666 return tsubst_copy_and_build (expr
,
5669 /*in_decl=*/NULL_TREE
,
5670 /*function_p=*/false,
5671 /*integral_constant_expression_p=*/true);
5674 /* Simplify EXPR if it is a non-dependent expression. Returns the
5675 (possibly simplified) expression. */
5678 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5680 if (expr
== NULL_TREE
)
5683 /* If we're in a template, but EXPR isn't value dependent, simplify
5684 it. We're supposed to treat:
5686 template <typename T> void f(T[1 + 1]);
5687 template <typename T> void f(T[2]);
5689 as two declarations of the same function, for example. */
5690 if (processing_template_decl
5691 && potential_nondependent_constant_expression (expr
))
5693 processing_template_decl_sentinel s
;
5694 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5700 instantiate_non_dependent_expr (tree expr
)
5702 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5705 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5706 an uninstantiated expression. */
5709 instantiate_non_dependent_or_null (tree expr
)
5711 if (expr
== NULL_TREE
)
5713 if (processing_template_decl
)
5715 if (!potential_nondependent_constant_expression (expr
))
5719 processing_template_decl_sentinel s
;
5720 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5726 /* True iff T is a specialization of a variable template. */
5729 variable_template_specialization_p (tree t
)
5731 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5733 tree tmpl
= DECL_TI_TEMPLATE (t
);
5734 return variable_template_p (tmpl
);
5737 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5738 template declaration, or a TYPE_DECL for an alias declaration. */
5741 alias_type_or_template_p (tree t
)
5745 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5748 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5749 || DECL_ALIAS_TEMPLATE_P (t
));
5752 /* Return TRUE iff T is a specialization of an alias template. */
5755 alias_template_specialization_p (const_tree t
)
5757 /* It's an alias template specialization if it's an alias and its
5758 TYPE_NAME is a specialization of a primary template. */
5759 if (TYPE_ALIAS_P (t
))
5761 tree name
= TYPE_NAME (t
);
5762 if (DECL_LANG_SPECIFIC (name
))
5763 if (tree ti
= DECL_TEMPLATE_INFO (name
))
5765 tree tmpl
= TI_TEMPLATE (ti
);
5766 return PRIMARY_TEMPLATE_P (tmpl
);
5772 /* An alias template is complex from a SFINAE perspective if a template-id
5773 using that alias can be ill-formed when the expansion is not, as with
5774 the void_t template. We determine this by checking whether the
5775 expansion for the alias template uses all its template parameters. */
5777 struct uses_all_template_parms_data
5784 uses_all_template_parms_r (tree t
, void *data_
)
5786 struct uses_all_template_parms_data
&data
5787 = *(struct uses_all_template_parms_data
*)data_
;
5788 tree idx
= get_template_parm_index (t
);
5790 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5791 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5796 complex_alias_template_p (const_tree tmpl
)
5798 struct uses_all_template_parms_data data
;
5799 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5800 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5801 data
.level
= TMPL_PARMS_DEPTH (parms
);
5802 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5803 data
.seen
= XALLOCAVEC (bool, len
);
5804 for (int i
= 0; i
< len
; ++i
)
5805 data
.seen
[i
] = false;
5807 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5808 for (int i
= 0; i
< len
; ++i
)
5814 /* Return TRUE iff T is a specialization of a complex alias template with
5815 dependent template-arguments. */
5818 dependent_alias_template_spec_p (const_tree t
)
5820 return (alias_template_specialization_p (t
)
5821 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t
)))
5822 && (any_dependent_template_arguments_p
5823 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t
)))));
5826 /* Return the number of innermost template parameters in TMPL. */
5829 num_innermost_template_parms (tree tmpl
)
5831 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5832 return TREE_VEC_LENGTH (parms
);
5835 /* Return either TMPL or another template that it is equivalent to under DR
5836 1286: An alias that just changes the name of a template is equivalent to
5837 the other template. */
5840 get_underlying_template (tree tmpl
)
5842 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5843 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5845 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5846 if (TYPE_TEMPLATE_INFO (result
))
5848 tree sub
= TYPE_TI_TEMPLATE (result
);
5849 if (PRIMARY_TEMPLATE_P (sub
)
5850 && (num_innermost_template_parms (tmpl
)
5851 == num_innermost_template_parms (sub
)))
5853 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5854 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5855 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5857 /* The alias type is equivalent to the pattern of the
5858 underlying template, so strip the alias. */
5868 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5869 must be a function or a pointer-to-function type, as specified
5870 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5871 and check that the resulting function has external linkage. */
5874 convert_nontype_argument_function (tree type
, tree expr
,
5875 tsubst_flags_t complain
)
5879 linkage_kind linkage
;
5881 fn
= instantiate_type (type
, fns
, tf_none
);
5882 if (fn
== error_mark_node
)
5883 return error_mark_node
;
5886 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5887 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5888 if (BASELINK_P (fn_no_ptr
))
5889 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5891 /* [temp.arg.nontype]/1
5893 A template-argument for a non-type, non-template template-parameter
5896 -- the address of an object or function with external [C++11: or
5897 internal] linkage. */
5899 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5901 if (complain
& tf_error
)
5903 error ("%qE is not a valid template argument for type %qT",
5905 if (TYPE_PTR_P (type
))
5906 error ("it must be the address of a function with "
5907 "external linkage");
5909 error ("it must be the name of a function with "
5910 "external linkage");
5915 linkage
= decl_linkage (fn_no_ptr
);
5916 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5918 if (complain
& tf_error
)
5920 if (cxx_dialect
>= cxx11
)
5921 error ("%qE is not a valid template argument for type %qT "
5922 "because %qD has no linkage",
5923 expr
, type
, fn_no_ptr
);
5925 error ("%qE is not a valid template argument for type %qT "
5926 "because %qD does not have external linkage",
5927 expr
, type
, fn_no_ptr
);
5935 /* Subroutine of convert_nontype_argument.
5936 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5937 Emit an error otherwise. */
5940 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5941 tsubst_flags_t complain
)
5944 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5946 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5948 if (processing_template_decl
5949 && TREE_CODE (expr
) == ADDR_EXPR
5950 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5952 if (complain
& tf_error
)
5954 error ("%qE is not a valid template argument for type %qT",
5956 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5961 /* Returns TRUE iff the address of OP is value-dependent.
5963 14.6.2.4 [temp.dep.temp]:
5964 A non-integral non-type template-argument is dependent if its type is
5965 dependent or it has either of the following forms
5968 and contains a nested-name-specifier which specifies a class-name that
5969 names a dependent type.
5971 We generalize this to just say that the address of a member of a
5972 dependent class is value-dependent; the above doesn't cover the
5973 address of a static data member named with an unqualified-id. */
5976 has_value_dependent_address (tree op
)
5978 /* We could use get_inner_reference here, but there's no need;
5979 this is only relevant for template non-type arguments, which
5980 can only be expressed as &id-expression. */
5983 tree ctx
= CP_DECL_CONTEXT (op
);
5984 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5991 /* The next set of functions are used for providing helpful explanatory
5992 diagnostics for failed overload resolution. Their messages should be
5993 indented by two spaces for consistency with the messages in
5997 unify_success (bool /*explain_p*/)
6003 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6006 inform (input_location
,
6007 " couldn't deduce template parameter %qD", parm
);
6012 unify_invalid (bool /*explain_p*/)
6018 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6021 inform (input_location
,
6022 " types %qT and %qT have incompatible cv-qualifiers",
6028 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6031 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6036 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6039 inform (input_location
,
6040 " template parameter %qD is not a parameter pack, but "
6047 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6050 inform (input_location
,
6051 " template argument %qE does not match "
6052 "pointer-to-member constant %qE",
6058 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6061 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6066 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6069 inform (input_location
,
6070 " inconsistent parameter pack deduction with %qT and %qT",
6076 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6081 inform (input_location
,
6082 " deduced conflicting types for parameter %qT (%qT and %qT)",
6083 parm
, first
, second
);
6085 inform (input_location
,
6086 " deduced conflicting values for non-type parameter "
6087 "%qE (%qE and %qE)", parm
, first
, second
);
6093 unify_vla_arg (bool explain_p
, tree arg
)
6096 inform (input_location
,
6097 " variable-sized array type %qT is not "
6098 "a valid template argument",
6104 unify_method_type_error (bool explain_p
, tree arg
)
6107 inform (input_location
,
6108 " member function type %qT is not a valid template argument",
6114 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6119 inform_n (input_location
, wanted
,
6120 " candidate expects at least %d argument, %d provided",
6121 " candidate expects at least %d arguments, %d provided",
6124 inform_n (input_location
, wanted
,
6125 " candidate expects %d argument, %d provided",
6126 " candidate expects %d arguments, %d provided",
6133 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6135 return unify_arity (explain_p
, have
, wanted
);
6139 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6140 bool least_p
= false)
6142 return unify_arity (explain_p
, have
, wanted
, least_p
);
6146 unify_arg_conversion (bool explain_p
, tree to_type
,
6147 tree from_type
, tree arg
)
6150 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6151 " cannot convert %qE (type %qT) to type %qT",
6152 arg
, from_type
, to_type
);
6157 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6158 tree parm
, tree arg
)
6163 case tbr_ambiguous_baseclass
:
6164 inform (input_location
, " %qT is an ambiguous base class of %qT",
6168 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6175 unify_inconsistent_template_template_parameters (bool explain_p
)
6178 inform (input_location
,
6179 " template parameters of a template template argument are "
6180 "inconsistent with other deduced template arguments");
6185 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6188 inform (input_location
,
6189 " can't deduce a template for %qT from non-template type %qT",
6195 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6198 inform (input_location
,
6199 " template argument %qE does not match %qE", arg
, parm
);
6204 unify_overload_resolution_failure (bool explain_p
, tree arg
)
6207 inform (input_location
,
6208 " could not resolve address from overloaded function %qE",
6213 /* Attempt to convert the non-type template parameter EXPR to the
6214 indicated TYPE. If the conversion is successful, return the
6215 converted value. If the conversion is unsuccessful, return
6216 NULL_TREE if we issued an error message, or error_mark_node if we
6217 did not. We issue error messages for out-and-out bad template
6218 parameters, but not simply because the conversion failed, since we
6219 might be just trying to do argument deduction. Both TYPE and EXPR
6220 must be non-dependent.
6222 The conversion follows the special rules described in
6223 [temp.arg.nontype], and it is much more strict than an implicit
6226 This function is called twice for each template argument (see
6227 lookup_template_class for a more accurate description of this
6228 problem). This means that we need to handle expressions which
6229 are not valid in a C++ source, but can be created from the
6230 first call (for instance, casts to perform conversions). These
6231 hacks can go away after we fix the double coercion problem. */
6234 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6238 /* Detect immediately string literals as invalid non-type argument.
6239 This special-case is not needed for correctness (we would easily
6240 catch this later), but only to provide better diagnostic for this
6241 common user mistake. As suggested by DR 100, we do not mention
6242 linkage issues in the diagnostic as this is not the point. */
6243 /* FIXME we're making this OK. */
6244 if (TREE_CODE (expr
) == STRING_CST
)
6246 if (complain
& tf_error
)
6247 error ("%qE is not a valid template argument for type %qT "
6248 "because string literals can never be used in this context",
6253 /* Add the ADDR_EXPR now for the benefit of
6254 value_dependent_expression_p. */
6255 if (TYPE_PTROBV_P (type
)
6256 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6258 expr
= decay_conversion (expr
, complain
);
6259 if (expr
== error_mark_node
)
6260 return error_mark_node
;
6263 /* If we are in a template, EXPR may be non-dependent, but still
6264 have a syntactic, rather than semantic, form. For example, EXPR
6265 might be a SCOPE_REF, rather than the VAR_DECL to which the
6266 SCOPE_REF refers. Preserving the qualifying scope is necessary
6267 so that access checking can be performed when the template is
6268 instantiated -- but here we need the resolved form so that we can
6269 convert the argument. */
6270 bool non_dep
= false;
6271 if (TYPE_REF_OBJ_P (type
)
6272 && has_value_dependent_address (expr
))
6273 /* If we want the address and it's value-dependent, don't fold. */;
6274 else if (processing_template_decl
6275 && potential_nondependent_constant_expression (expr
))
6277 if (error_operand_p (expr
))
6278 return error_mark_node
;
6279 expr_type
= TREE_TYPE (expr
);
6280 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6281 expr
= mark_lvalue_use (expr
);
6283 expr
= mark_rvalue_use (expr
);
6285 /* If the argument is non-dependent, perform any conversions in
6286 non-dependent context as well. */
6287 processing_template_decl_sentinel
s (non_dep
);
6289 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6291 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6292 to a non-type argument of "nullptr". */
6293 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
6294 expr
= fold_simple (convert (type
, expr
));
6296 /* In C++11, integral or enumeration non-type template arguments can be
6297 arbitrary constant expressions. Pointer and pointer to
6298 member arguments can be general constant expressions that evaluate
6299 to a null value, but otherwise still need to be of a specific form. */
6300 if (cxx_dialect
>= cxx11
)
6302 if (TREE_CODE (expr
) == PTRMEM_CST
)
6303 /* A PTRMEM_CST is already constant, and a valid template
6304 argument for a parameter of pointer to member type, we just want
6305 to leave it in that form rather than lower it to a
6307 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6308 expr
= maybe_constant_value (expr
);
6309 else if (cxx_dialect
>= cxx1z
)
6311 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6312 expr
= maybe_constant_value (expr
);
6313 else if (REFERENCE_REF_P (expr
))
6315 expr
= TREE_OPERAND (expr
, 0);
6316 expr
= maybe_constant_value (expr
);
6317 expr
= convert_from_reference (expr
);
6320 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6322 tree folded
= maybe_constant_value (expr
);
6323 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6324 : null_member_pointer_value_p (folded
))
6329 /* HACK: Due to double coercion, we can get a
6330 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6331 which is the tree that we built on the first call (see
6332 below when coercing to reference to object or to reference to
6333 function). We just strip everything and get to the arg.
6334 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6336 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6338 tree probe_type
, probe
= expr
;
6339 if (REFERENCE_REF_P (probe
))
6340 probe
= TREE_OPERAND (probe
, 0);
6341 probe_type
= TREE_TYPE (probe
);
6342 if (TREE_CODE (probe
) == NOP_EXPR
)
6344 /* ??? Maybe we could use convert_from_reference here, but we
6345 would need to relax its constraints because the NOP_EXPR
6346 could actually change the type to something more cv-qualified,
6347 and this is not folded by convert_from_reference. */
6348 tree addr
= TREE_OPERAND (probe
, 0);
6349 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6350 && TREE_CODE (addr
) == ADDR_EXPR
6351 && TYPE_PTR_P (TREE_TYPE (addr
))
6352 && (same_type_ignoring_top_level_qualifiers_p
6353 (TREE_TYPE (probe_type
),
6354 TREE_TYPE (TREE_TYPE (addr
)))))
6356 expr
= TREE_OPERAND (addr
, 0);
6357 expr_type
= TREE_TYPE (probe_type
);
6362 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6363 parameter is a pointer to object, through decay and
6364 qualification conversion. Let's strip everything. */
6365 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
6369 if (TREE_CODE (probe
) == ADDR_EXPR
6370 && TYPE_PTR_P (TREE_TYPE (probe
)))
6372 /* Skip the ADDR_EXPR only if it is part of the decay for
6373 an array. Otherwise, it is part of the original argument
6374 in the source code. */
6375 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
6376 probe
= TREE_OPERAND (probe
, 0);
6378 expr_type
= TREE_TYPE (expr
);
6382 /* [temp.arg.nontype]/5, bullet 1
6384 For a non-type template-parameter of integral or enumeration type,
6385 integral promotions (_conv.prom_) and integral conversions
6386 (_conv.integral_) are applied. */
6387 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6389 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
6390 t
= maybe_constant_value (t
);
6391 if (t
!= error_mark_node
)
6394 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6395 return error_mark_node
;
6397 /* Notice that there are constant expressions like '4 % 0' which
6398 do not fold into integer constants. */
6399 if (TREE_CODE (expr
) != INTEGER_CST
)
6401 if (complain
& tf_error
)
6403 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6404 if (processing_template_decl
6405 && !require_potential_constant_expression (expr
))
6407 expr
= cxx_constant_value (expr
);
6408 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6409 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6410 "in template argument for type %qT ", type
);
6411 if (expr
== error_mark_node
)
6413 /* else cxx_constant_value complained but gave us
6414 a real constant, so go ahead. */
6415 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6421 /* Avoid typedef problems. */
6422 if (TREE_TYPE (expr
) != type
)
6423 expr
= fold_convert (type
, expr
);
6425 /* [temp.arg.nontype]/5, bullet 2
6427 For a non-type template-parameter of type pointer to object,
6428 qualification conversions (_conv.qual_) and the array-to-pointer
6429 conversion (_conv.array_) are applied. */
6430 else if (TYPE_PTROBV_P (type
))
6432 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6434 A template-argument for a non-type, non-template template-parameter
6435 shall be one of: [...]
6437 -- the name of a non-type template-parameter;
6438 -- the address of an object or function with external linkage, [...]
6439 expressed as "& id-expression" where the & is optional if the name
6440 refers to a function or array, or if the corresponding
6441 template-parameter is a reference.
6443 Here, we do not care about functions, as they are invalid anyway
6444 for a parameter of type pointer-to-object. */
6446 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
6447 /* Non-type template parameters are OK. */
6449 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6450 /* Null pointer values are OK in C++11. */;
6451 else if (TREE_CODE (expr
) != ADDR_EXPR
6452 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6456 if (complain
& tf_error
)
6457 error ("%qD is not a valid template argument "
6458 "because %qD is a variable, not the address of "
6459 "a variable", expr
, expr
);
6462 if (POINTER_TYPE_P (expr_type
))
6464 if (complain
& tf_error
)
6465 error ("%qE is not a valid template argument for %qT "
6466 "because it is not the address of a variable",
6470 /* Other values, like integer constants, might be valid
6471 non-type arguments of some other type. */
6472 return error_mark_node
;
6478 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6479 ? TREE_OPERAND (expr
, 0) : expr
);
6482 if (complain
& tf_error
)
6483 error ("%qE is not a valid template argument of type %qT "
6484 "because %qE is not a variable", expr
, type
, decl
);
6487 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6489 if (complain
& tf_error
)
6490 error ("%qE is not a valid template argument of type %qT "
6491 "because %qD does not have external linkage",
6495 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6497 if (complain
& tf_error
)
6498 error ("%qE is not a valid template argument of type %qT "
6499 "because %qD has no linkage", expr
, type
, decl
);
6504 expr
= decay_conversion (expr
, complain
);
6505 if (expr
== error_mark_node
)
6506 return error_mark_node
;
6508 expr
= perform_qualification_conversions (type
, expr
);
6509 if (expr
== error_mark_node
)
6510 return error_mark_node
;
6512 /* [temp.arg.nontype]/5, bullet 3
6514 For a non-type template-parameter of type reference to object, no
6515 conversions apply. The type referred to by the reference may be more
6516 cv-qualified than the (otherwise identical) type of the
6517 template-argument. The template-parameter is bound directly to the
6518 template-argument, which must be an lvalue. */
6519 else if (TYPE_REF_OBJ_P (type
))
6521 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6523 return error_mark_node
;
6525 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6527 if (complain
& tf_error
)
6528 error ("%qE is not a valid template argument for type %qT "
6529 "because of conflicts in cv-qualification", expr
, type
);
6533 if (!lvalue_p (expr
))
6535 if (complain
& tf_error
)
6536 error ("%qE is not a valid template argument for type %qT "
6537 "because it is not an lvalue", expr
, type
);
6541 /* [temp.arg.nontype]/1
6543 A template-argument for a non-type, non-template template-parameter
6544 shall be one of: [...]
6546 -- the address of an object or function with external linkage. */
6547 if (INDIRECT_REF_P (expr
)
6548 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6550 expr
= TREE_OPERAND (expr
, 0);
6553 if (complain
& tf_error
)
6554 error ("%q#D is not a valid template argument for type %qT "
6555 "because a reference variable does not have a constant "
6556 "address", expr
, type
);
6563 if (complain
& tf_error
)
6564 error ("%qE is not a valid template argument for type %qT "
6565 "because it is not an object with linkage",
6570 /* DR 1155 allows internal linkage in C++11 and up. */
6571 linkage_kind linkage
= decl_linkage (expr
);
6572 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6574 if (complain
& tf_error
)
6575 error ("%qE is not a valid template argument for type %qT "
6576 "because object %qD does not have linkage",
6581 expr
= build_nop (type
, build_address (expr
));
6583 /* [temp.arg.nontype]/5, bullet 4
6585 For a non-type template-parameter of type pointer to function, only
6586 the function-to-pointer conversion (_conv.func_) is applied. If the
6587 template-argument represents a set of overloaded functions (or a
6588 pointer to such), the matching function is selected from the set
6590 else if (TYPE_PTRFN_P (type
))
6592 /* If the argument is a template-id, we might not have enough
6593 context information to decay the pointer. */
6594 if (!type_unknown_p (expr_type
))
6596 expr
= decay_conversion (expr
, complain
);
6597 if (expr
== error_mark_node
)
6598 return error_mark_node
;
6601 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6602 /* Null pointer values are OK in C++11. */
6603 return perform_qualification_conversions (type
, expr
);
6605 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6606 if (!expr
|| expr
== error_mark_node
)
6609 /* [temp.arg.nontype]/5, bullet 5
6611 For a non-type template-parameter of type reference to function, no
6612 conversions apply. If the template-argument represents a set of
6613 overloaded functions, the matching function is selected from the set
6615 else if (TYPE_REFFN_P (type
))
6617 if (TREE_CODE (expr
) == ADDR_EXPR
)
6619 if (complain
& tf_error
)
6621 error ("%qE is not a valid template argument for type %qT "
6622 "because it is a pointer", expr
, type
);
6623 inform (input_location
, "try using %qE instead",
6624 TREE_OPERAND (expr
, 0));
6629 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6630 if (!expr
|| expr
== error_mark_node
)
6633 expr
= build_nop (type
, build_address (expr
));
6635 /* [temp.arg.nontype]/5, bullet 6
6637 For a non-type template-parameter of type pointer to member function,
6638 no conversions apply. If the template-argument represents a set of
6639 overloaded member functions, the matching member function is selected
6640 from the set (_over.over_). */
6641 else if (TYPE_PTRMEMFUNC_P (type
))
6643 expr
= instantiate_type (type
, expr
, tf_none
);
6644 if (expr
== error_mark_node
)
6645 return error_mark_node
;
6647 /* [temp.arg.nontype] bullet 1 says the pointer to member
6648 expression must be a pointer-to-member constant. */
6649 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6650 return error_mark_node
;
6652 /* There is no way to disable standard conversions in
6653 resolve_address_of_overloaded_function (called by
6654 instantiate_type). It is possible that the call succeeded by
6655 converting &B::I to &D::I (where B is a base of D), so we need
6656 to reject this conversion here.
6658 Actually, even if there was a way to disable standard conversions,
6659 it would still be better to reject them here so that we can
6660 provide a superior diagnostic. */
6661 if (!same_type_p (TREE_TYPE (expr
), type
))
6663 if (complain
& tf_error
)
6665 error ("%qE is not a valid template argument for type %qT "
6666 "because it is of type %qT", expr
, type
,
6668 /* If we are just one standard conversion off, explain. */
6669 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6670 inform (input_location
,
6671 "standard conversions are not allowed in this context");
6676 /* [temp.arg.nontype]/5, bullet 7
6678 For a non-type template-parameter of type pointer to data member,
6679 qualification conversions (_conv.qual_) are applied. */
6680 else if (TYPE_PTRDATAMEM_P (type
))
6682 /* [temp.arg.nontype] bullet 1 says the pointer to member
6683 expression must be a pointer-to-member constant. */
6684 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6685 return error_mark_node
;
6687 expr
= perform_qualification_conversions (type
, expr
);
6688 if (expr
== error_mark_node
)
6691 else if (NULLPTR_TYPE_P (type
))
6693 if (expr
!= nullptr_node
)
6695 if (complain
& tf_error
)
6696 error ("%qE is not a valid template argument for type %qT "
6697 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6702 /* A template non-type parameter must be one of the above. */
6706 /* Sanity check: did we actually convert the argument to the
6708 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6709 (type
, TREE_TYPE (expr
)));
6710 return convert_from_reference (expr
);
6713 /* Subroutine of coerce_template_template_parms, which returns 1 if
6714 PARM_PARM and ARG_PARM match using the rule for the template
6715 parameters of template template parameters. Both PARM and ARG are
6716 template parameters; the rest of the arguments are the same as for
6717 coerce_template_template_parms.
6720 coerce_template_template_parm (tree parm
,
6722 tsubst_flags_t complain
,
6726 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6727 || parm
== NULL_TREE
|| error_operand_p (parm
))
6730 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6733 switch (TREE_CODE (parm
))
6736 /* We encounter instantiations of templates like
6737 template <template <template <class> class> class TT>
6740 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6741 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6743 if (!coerce_template_template_parms
6744 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6750 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6751 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6752 /* Argument is a parameter pack but parameter is not. */
6757 /* The tsubst call is used to handle cases such as
6759 template <int> class C {};
6760 template <class T, template <T> class TT> class D {};
6763 i.e. the parameter list of TT depends on earlier parameters. */
6764 if (!uses_template_parms (TREE_TYPE (arg
)))
6766 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6767 if (!uses_template_parms (t
)
6768 && !same_type_p (t
, TREE_TYPE (arg
)))
6772 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6773 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6774 /* Argument is a parameter pack but parameter is not. */
6787 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6788 template template parameters. Both PARM_PARMS and ARG_PARMS are
6789 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6792 Consider the example:
6793 template <class T> class A;
6794 template<template <class U> class TT> class B;
6796 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6797 the parameters to A, and OUTER_ARGS contains A. */
6800 coerce_template_template_parms (tree parm_parms
,
6802 tsubst_flags_t complain
,
6806 int nparms
, nargs
, i
;
6810 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6811 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6813 nparms
= TREE_VEC_LENGTH (parm_parms
);
6814 nargs
= TREE_VEC_LENGTH (arg_parms
);
6816 /* Determine whether we have a parameter pack at the end of the
6817 template template parameter's template parameter list. */
6818 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6820 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6822 if (error_operand_p (parm
))
6825 switch (TREE_CODE (parm
))
6829 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6834 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6844 && !(variadic_p
&& nargs
>= nparms
- 1))
6847 /* Check all of the template parameters except the parameter pack at
6848 the end (if any). */
6849 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6851 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6852 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6855 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6856 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6858 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6866 /* Check each of the template parameters in the template
6867 argument against the template parameter pack at the end of
6868 the template template parameter. */
6869 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6872 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6874 for (; i
< nargs
; ++i
)
6876 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6879 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6881 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6890 /* Verifies that the deduced template arguments (in TARGS) for the
6891 template template parameters (in TPARMS) represent valid bindings,
6892 by comparing the template parameter list of each template argument
6893 to the template parameter list of its corresponding template
6894 template parameter, in accordance with DR150. This
6895 routine can only be called after all template arguments have been
6896 deduced. It will return TRUE if all of the template template
6897 parameter bindings are okay, FALSE otherwise. */
6899 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6901 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6904 /* We're dealing with template parms in this process. */
6905 ++processing_template_decl
;
6907 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6909 for (i
= 0; i
< ntparms
; ++i
)
6911 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6912 tree targ
= TREE_VEC_ELT (targs
, i
);
6914 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6916 tree packed_args
= NULL_TREE
;
6919 if (ARGUMENT_PACK_P (targ
))
6921 /* Look inside the argument pack. */
6922 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6923 len
= TREE_VEC_LENGTH (packed_args
);
6926 for (idx
= 0; idx
< len
; ++idx
)
6928 tree targ_parms
= NULL_TREE
;
6931 /* Extract the next argument from the argument
6933 targ
= TREE_VEC_ELT (packed_args
, idx
);
6935 if (PACK_EXPANSION_P (targ
))
6936 /* Look at the pattern of the pack expansion. */
6937 targ
= PACK_EXPANSION_PATTERN (targ
);
6939 /* Extract the template parameters from the template
6941 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6942 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6943 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6944 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6946 /* Verify that we can coerce the template template
6947 parameters from the template argument to the template
6948 parameter. This requires an exact match. */
6950 && !coerce_template_template_parms
6951 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6966 --processing_template_decl
;
6970 /* Since type attributes aren't mangled, we need to strip them from
6971 template type arguments. */
6974 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6976 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6978 bool removed_attributes
= false;
6979 tree canon
= strip_typedefs (arg
, &removed_attributes
);
6980 if (removed_attributes
6981 && (complain
& tf_warning
))
6982 warning (OPT_Wignored_attributes
,
6983 "ignoring attributes on template argument %qT", arg
);
6987 // A template declaration can be substituted for a constrained
6988 // template template parameter only when the argument is more
6989 // constrained than the parameter.
6991 is_compatible_template_arg (tree parm
, tree arg
)
6993 tree parm_cons
= get_constraints (parm
);
6995 /* For now, allow constrained template template arguments
6996 and unconstrained template template parameters. */
6997 if (parm_cons
== NULL_TREE
)
7000 tree arg_cons
= get_constraints (arg
);
7002 // If the template parameter is constrained, we need to rewrite its
7003 // constraints in terms of the ARG's template parameters. This ensures
7004 // that all of the template parameter types will have the same depth.
7006 // Note that this is only valid when coerce_template_template_parm is
7007 // true for the innermost template parameters of PARM and ARG. In other
7008 // words, because coercion is successful, this conversion will be valid.
7011 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7012 parm_cons
= tsubst_constraint_info (parm_cons
,
7013 INNERMOST_TEMPLATE_ARGS (args
),
7014 tf_none
, NULL_TREE
);
7015 if (parm_cons
== error_mark_node
)
7019 return subsumes (parm_cons
, arg_cons
);
7022 // Convert a placeholder argument into a binding to the original
7023 // parameter. The original parameter is saved as the TREE_TYPE of
7026 convert_wildcard_argument (tree parm
, tree arg
)
7028 TREE_TYPE (arg
) = parm
;
7032 /* Convert the indicated template ARG as necessary to match the
7033 indicated template PARM. Returns the converted ARG, or
7034 error_mark_node if the conversion was unsuccessful. Error and
7035 warning messages are issued under control of COMPLAIN. This
7036 conversion is for the Ith parameter in the parameter list. ARGS is
7037 the full set of template arguments deduced so far. */
7040 convert_template_argument (tree parm
,
7043 tsubst_flags_t complain
,
7049 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7051 if (parm
== error_mark_node
)
7052 return error_mark_node
;
7054 /* Trivially convert placeholders. */
7055 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7056 return convert_wildcard_argument (parm
, arg
);
7058 if (TREE_CODE (arg
) == TREE_LIST
7059 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7061 /* The template argument was the name of some
7062 member function. That's usually
7063 invalid, but static members are OK. In any
7064 case, grab the underlying fields/functions
7065 and issue an error later if required. */
7066 orig_arg
= TREE_VALUE (arg
);
7067 TREE_TYPE (arg
) = unknown_type_node
;
7072 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7073 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7074 || requires_tmpl_type
);
7076 /* When determining whether an argument pack expansion is a template,
7077 look at the pattern. */
7078 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7079 arg
= PACK_EXPANSION_PATTERN (arg
);
7081 /* Deal with an injected-class-name used as a template template arg. */
7082 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7084 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7085 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7087 if (cxx_dialect
>= cxx11
)
7088 /* OK under DR 1004. */;
7089 else if (complain
& tf_warning_or_error
)
7090 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7091 " used as template template argument", TYPE_NAME (arg
));
7092 else if (flag_pedantic_errors
)
7100 ((TREE_CODE (arg
) == TEMPLATE_DECL
7101 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7102 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7103 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7104 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7107 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7108 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7109 arg
= TYPE_STUB_DECL (arg
);
7111 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7113 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7114 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7116 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7118 if (complain
& tf_error
)
7119 error ("invalid use of destructor %qE as a type", orig_arg
);
7120 return error_mark_node
;
7123 permerror (input_location
,
7124 "to refer to a type member of a template parameter, "
7125 "use %<typename %E%>", orig_arg
);
7127 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7128 TREE_OPERAND (arg
, 1),
7134 if (is_type
!= requires_type
)
7138 if (complain
& tf_error
)
7140 error ("type/value mismatch at argument %d in template "
7141 "parameter list for %qD",
7144 inform (input_location
,
7145 " expected a constant of type %qT, got %qT",
7147 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7148 else if (requires_tmpl_type
)
7149 inform (input_location
,
7150 " expected a class template, got %qE", orig_arg
);
7152 inform (input_location
,
7153 " expected a type, got %qE", orig_arg
);
7156 return error_mark_node
;
7158 if (is_tmpl_type
^ requires_tmpl_type
)
7160 if (in_decl
&& (complain
& tf_error
))
7162 error ("type/value mismatch at argument %d in template "
7163 "parameter list for %qD",
7166 inform (input_location
,
7167 " expected a type, got %qT", DECL_NAME (arg
));
7169 inform (input_location
,
7170 " expected a class template, got %qT", orig_arg
);
7172 return error_mark_node
;
7177 if (requires_tmpl_type
)
7179 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7181 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7182 /* The number of argument required is not known yet.
7183 Just accept it for now. */
7184 val
= TREE_TYPE (arg
);
7187 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7190 /* Strip alias templates that are equivalent to another
7192 arg
= get_underlying_template (arg
);
7193 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7195 if (coerce_template_template_parms (parmparm
, argparm
,
7201 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7203 if (val
!= error_mark_node
)
7205 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7206 val
= TREE_TYPE (val
);
7207 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7208 val
= make_pack_expansion (val
);
7213 if (in_decl
&& (complain
& tf_error
))
7215 error ("type/value mismatch at argument %d in "
7216 "template parameter list for %qD",
7218 inform (input_location
,
7219 " expected a template of type %qD, got %qT",
7223 val
= error_mark_node
;
7226 // Check that the constraints are compatible before allowing the
7228 if (val
!= error_mark_node
)
7229 if (!is_compatible_template_arg (parm
, arg
))
7231 if (in_decl
&& (complain
& tf_error
))
7233 error ("constraint mismatch at argument %d in "
7234 "template parameter list for %qD",
7236 inform (input_location
, " expected %qD but got %qD",
7239 val
= error_mark_node
;
7245 /* We only form one instance of each template specialization.
7246 Therefore, if we use a non-canonical variant (i.e., a
7247 typedef), any future messages referring to the type will use
7248 the typedef, which is confusing if those future uses do not
7249 themselves also use the typedef. */
7251 val
= canonicalize_type_argument (val
, complain
);
7255 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
7257 if (invalid_nontype_parm_type_p (t
, complain
))
7258 return error_mark_node
;
7260 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7262 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
7266 /* Not sure if this is reachable, but it doesn't hurt
7268 error ("type mismatch in nontype parameter pack");
7269 val
= error_mark_node
;
7272 else if (!dependent_template_arg_p (orig_arg
)
7273 && !uses_template_parms (t
))
7274 /* We used to call digest_init here. However, digest_init
7275 will report errors, which we don't want when complain
7276 is zero. More importantly, digest_init will try too
7277 hard to convert things: for example, `0' should not be
7278 converted to pointer type at this point according to
7279 the standard. Accepting this is not merely an
7280 extension, since deciding whether or not these
7281 conversions can occur is part of determining which
7282 function template to call, or whether a given explicit
7283 argument specification is valid. */
7284 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7287 bool removed_attr
= false;
7288 val
= strip_typedefs_expr (orig_arg
, &removed_attr
);
7291 if (val
== NULL_TREE
)
7292 val
= error_mark_node
;
7293 else if (val
== error_mark_node
&& (complain
& tf_error
))
7294 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
7296 if (INDIRECT_REF_P (val
))
7298 /* Reject template arguments that are references to built-in
7299 functions with no library fallbacks. */
7300 const_tree inner
= TREE_OPERAND (val
, 0);
7301 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
7302 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner
))) == FUNCTION_TYPE
7303 && TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
7304 && 0 < TREE_OPERAND_LENGTH (inner
)
7305 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7306 return error_mark_node
;
7309 if (TREE_CODE (val
) == SCOPE_REF
)
7311 /* Strip typedefs from the SCOPE_REF. */
7312 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7313 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7315 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7316 QUALIFIED_NAME_IS_TEMPLATE (val
));
7323 /* Coerces the remaining template arguments in INNER_ARGS (from
7324 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7325 Returns the coerced argument pack. PARM_IDX is the position of this
7326 parameter in the template parameter list. ARGS is the original
7327 template argument list. */
7329 coerce_template_parameter_pack (tree parms
,
7337 tsubst_flags_t complain
)
7339 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7340 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7343 tree packed_parms
= NULL_TREE
;
7345 if (arg_idx
> nargs
)
7348 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7350 /* When the template parameter is a non-type template parameter pack
7351 or template template parameter pack whose type or template
7352 parameters use parameter packs, we know exactly how many arguments
7353 we are looking for. Build a vector of the instantiated decls for
7354 these template parameters in PACKED_PARMS. */
7355 /* We can't use make_pack_expansion here because it would interpret a
7356 _DECL as a use rather than a declaration. */
7357 tree decl
= TREE_VALUE (parm
);
7358 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7359 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7360 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7361 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7363 TREE_VEC_LENGTH (args
)--;
7364 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7365 TREE_VEC_LENGTH (args
)++;
7367 if (packed_parms
== error_mark_node
)
7368 return error_mark_node
;
7370 /* If we're doing a partial instantiation of a member template,
7371 verify that all of the types used for the non-type
7372 template parameter pack are, in fact, valid for non-type
7373 template parameters. */
7375 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7377 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7378 for (j
= 0; j
< len
; ++j
)
7380 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7381 if (invalid_nontype_parm_type_p (t
, complain
))
7382 return error_mark_node
;
7384 /* We don't know how many args we have yet, just
7385 use the unconverted ones for now. */
7389 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7391 /* Check if we have a placeholder pack, which indicates we're
7392 in the context of a introduction list. In that case we want
7393 to match this pack to the single placeholder. */
7394 else if (arg_idx
< nargs
7395 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7396 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7398 nargs
= arg_idx
+ 1;
7399 packed_args
= make_tree_vec (1);
7402 packed_args
= make_tree_vec (nargs
- arg_idx
);
7404 /* Convert the remaining arguments, which will be a part of the
7405 parameter pack "parm". */
7406 int first_pack_arg
= arg_idx
;
7407 for (; arg_idx
< nargs
; ++arg_idx
)
7409 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7410 tree actual_parm
= TREE_VALUE (parm
);
7411 int pack_idx
= arg_idx
- first_pack_arg
;
7415 /* Once we've packed as many args as we have types, stop. */
7416 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7418 else if (PACK_EXPANSION_P (arg
))
7419 /* We don't know how many args we have yet, just
7420 use the unconverted ones for now. */
7423 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7426 if (arg
== error_mark_node
)
7428 if (complain
& tf_error
)
7429 error ("template argument %d is invalid", arg_idx
+ 1);
7432 arg
= convert_template_argument (actual_parm
,
7433 arg
, new_args
, complain
, parm_idx
,
7435 if (arg
== error_mark_node
)
7437 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7440 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7441 && TREE_VEC_LENGTH (packed_args
) > 0)
7443 if (complain
& tf_error
)
7444 error ("wrong number of template arguments (%d, should be %d)",
7445 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7446 return error_mark_node
;
7449 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7450 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7451 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7454 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7455 TREE_TYPE (argument_pack
)
7456 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
7457 TREE_CONSTANT (argument_pack
) = 1;
7460 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7462 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7463 TREE_VEC_LENGTH (packed_args
));
7464 return argument_pack
;
7467 /* Returns the number of pack expansions in the template argument vector
7471 pack_expansion_args_count (tree args
)
7476 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7478 tree elt
= TREE_VEC_ELT (args
, i
);
7479 if (elt
&& PACK_EXPANSION_P (elt
))
7485 /* Convert all template arguments to their appropriate types, and
7486 return a vector containing the innermost resulting template
7487 arguments. If any error occurs, return error_mark_node. Error and
7488 warning messages are issued under control of COMPLAIN.
7490 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7491 for arguments not specified in ARGS. Otherwise, if
7492 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7493 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7494 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7498 coerce_template_parms (tree parms
,
7501 tsubst_flags_t complain
,
7502 bool require_all_args
,
7503 bool use_default_args
)
7505 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7506 tree orig_inner_args
;
7509 tree new_inner_args
;
7510 int saved_unevaluated_operand
;
7511 int saved_inhibit_evaluation_warnings
;
7513 /* When used as a boolean value, indicates whether this is a
7514 variadic template parameter list. Since it's an int, we can also
7515 subtract it from nparms to get the number of non-variadic
7518 int variadic_args_p
= 0;
7519 int post_variadic_parms
= 0;
7521 /* Likewise for parameters with default arguments. */
7524 if (args
== error_mark_node
)
7525 return error_mark_node
;
7527 nparms
= TREE_VEC_LENGTH (parms
);
7529 /* Determine if there are any parameter packs or default arguments. */
7530 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7532 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7534 ++post_variadic_parms
;
7535 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7537 if (TREE_PURPOSE (parm
))
7541 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7542 /* If there are no parameters that follow a parameter pack, we need to
7543 expand any argument packs so that we can deduce a parameter pack from
7544 some non-packed args followed by an argument pack, as in variadic85.C.
7545 If there are such parameters, we need to leave argument packs intact
7546 so the arguments are assigned properly. This can happen when dealing
7547 with a nested class inside a partial specialization of a class
7548 template, as in variadic92.C, or when deducing a template parameter pack
7549 from a sub-declarator, as in variadic114.C. */
7550 if (!post_variadic_parms
)
7551 inner_args
= expand_template_argument_pack (inner_args
);
7553 /* Count any pack expansion args. */
7554 variadic_args_p
= pack_expansion_args_count (inner_args
);
7556 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7557 if ((nargs
> nparms
&& !variadic_p
)
7558 || (nargs
< nparms
- variadic_p
7561 && (!use_default_args
7562 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7563 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7565 if (complain
& tf_error
)
7567 if (variadic_p
|| default_p
)
7569 nparms
-= variadic_p
+ default_p
;
7570 error ("wrong number of template arguments "
7571 "(%d, should be at least %d)", nargs
, nparms
);
7574 error ("wrong number of template arguments "
7575 "(%d, should be %d)", nargs
, nparms
);
7578 inform (DECL_SOURCE_LOCATION (in_decl
),
7579 "provided for %qD", in_decl
);
7582 return error_mark_node
;
7584 /* We can't pass a pack expansion to a non-pack parameter of an alias
7585 template (DR 1430). */
7587 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
7588 || concept_template_p (in_decl
))
7590 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7592 if (complain
& tf_error
)
7594 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7596 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7597 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7599 if (PACK_EXPANSION_P (arg
)
7600 && !template_parameter_pack_p (parm
))
7602 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
7603 error_at (location_of (arg
),
7604 "pack expansion argument for non-pack parameter "
7605 "%qD of alias template %qD", parm
, in_decl
);
7607 error_at (location_of (arg
),
7608 "pack expansion argument for non-pack parameter "
7609 "%qD of concept %qD", parm
, in_decl
);
7610 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7617 return error_mark_node
;
7620 /* We need to evaluate the template arguments, even though this
7621 template-id may be nested within a "sizeof". */
7622 saved_unevaluated_operand
= cp_unevaluated_operand
;
7623 cp_unevaluated_operand
= 0;
7624 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7625 c_inhibit_evaluation_warnings
= 0;
7626 new_inner_args
= make_tree_vec (nparms
);
7627 new_args
= add_outermost_template_args (args
, new_inner_args
);
7628 int pack_adjust
= 0;
7629 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7634 /* Get the Ith template parameter. */
7635 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7637 if (parm
== error_mark_node
)
7639 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7643 /* Calculate the next argument. */
7644 if (arg_idx
< nargs
)
7645 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7649 if (template_parameter_pack_p (TREE_VALUE (parm
))
7650 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7652 /* Some arguments will be placed in the
7653 template parameter pack PARM. */
7654 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7655 inner_args
, arg_idx
,
7659 if (arg
== NULL_TREE
)
7661 /* We don't know how many args we have yet, just use the
7662 unconverted (and still packed) ones for now. */
7663 new_inner_args
= orig_inner_args
;
7668 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7670 /* Store this argument. */
7671 if (arg
== error_mark_node
)
7674 /* We are done with all of the arguments. */
7679 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7680 arg_idx
+= pack_adjust
;
7687 if (PACK_EXPANSION_P (arg
))
7689 /* "If every valid specialization of a variadic template
7690 requires an empty template parameter pack, the template is
7691 ill-formed, no diagnostic required." So check that the
7692 pattern works with this parameter. */
7693 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7694 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7698 if (conv
== error_mark_node
)
7700 inform (input_location
, "so any instantiation with a "
7701 "non-empty parameter pack would be ill-formed");
7704 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7705 /* Recover from missing typename. */
7706 TREE_VEC_ELT (inner_args
, arg_idx
)
7707 = make_pack_expansion (conv
);
7709 /* We don't know how many args we have yet, just
7710 use the unconverted ones for now. */
7711 new_inner_args
= inner_args
;
7716 else if (require_all_args
)
7718 /* There must be a default arg in this case. */
7719 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7721 /* The position of the first default template argument,
7722 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7724 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7725 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7726 arg_idx
- pack_adjust
);
7731 if (arg
== error_mark_node
)
7733 if (complain
& tf_error
)
7734 error ("template argument %d is invalid", arg_idx
+ 1);
7737 /* This only occurs if there was an error in the template
7738 parameter list itself (which we would already have
7739 reported) that we are trying to recover from, e.g., a class
7740 template with a parameter list such as
7741 template<typename..., typename>. */
7744 arg
= convert_template_argument (TREE_VALUE (parm
),
7745 arg
, new_args
, complain
,
7748 if (arg
== error_mark_node
)
7750 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7752 cp_unevaluated_operand
= saved_unevaluated_operand
;
7753 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7755 if (variadic_p
&& arg_idx
< nargs
)
7757 if (complain
& tf_error
)
7759 error ("wrong number of template arguments "
7760 "(%d, should be %d)", nargs
, arg_idx
);
7762 error ("provided for %q+D", in_decl
);
7764 return error_mark_node
;
7768 return error_mark_node
;
7770 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7771 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7772 TREE_VEC_LENGTH (new_inner_args
));
7774 return new_inner_args
;
7777 /* Convert all template arguments to their appropriate types, and
7778 return a vector containing the innermost resulting template
7779 arguments. If any error occurs, return error_mark_node. Error and
7780 warning messages are not issued.
7782 Note that no function argument deduction is performed, and default
7783 arguments are used to fill in unspecified arguments. */
7785 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
7787 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
7790 /* Convert all template arguments to their appropriate type, and
7791 instantiate default arguments as needed. This returns a vector
7792 containing the innermost resulting template arguments, or
7793 error_mark_node if unsuccessful. */
7795 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
7796 tsubst_flags_t complain
)
7798 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
7801 /* Like coerce_template_parms. If PARMS represents all template
7802 parameters levels, this function returns a vector of vectors
7803 representing all the resulting argument levels. Note that in this
7804 case, only the innermost arguments are coerced because the
7805 outermost ones are supposed to have been coerced already.
7807 Otherwise, if PARMS represents only (the innermost) vector of
7808 parameters, this function returns a vector containing just the
7809 innermost resulting arguments. */
7812 coerce_innermost_template_parms (tree parms
,
7815 tsubst_flags_t complain
,
7816 bool require_all_args
,
7817 bool use_default_args
)
7819 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7820 int args_depth
= TMPL_ARGS_DEPTH (args
);
7823 if (parms_depth
> 1)
7825 coerced_args
= make_tree_vec (parms_depth
);
7829 for (level
= parms
, cur_depth
= parms_depth
;
7830 parms_depth
> 0 && level
!= NULL_TREE
;
7831 level
= TREE_CHAIN (level
), --cur_depth
)
7834 if (cur_depth
== args_depth
)
7835 l
= coerce_template_parms (TREE_VALUE (level
),
7836 args
, in_decl
, complain
,
7840 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7842 if (l
== error_mark_node
)
7843 return error_mark_node
;
7845 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7849 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7850 args
, in_decl
, complain
,
7853 return coerced_args
;
7856 /* Returns 1 if template args OT and NT are equivalent. */
7859 template_args_equal (tree ot
, tree nt
)
7863 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7866 if (TREE_CODE (nt
) == TREE_VEC
)
7867 /* For member templates */
7868 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7869 else if (PACK_EXPANSION_P (ot
))
7870 return (PACK_EXPANSION_P (nt
)
7871 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7872 PACK_EXPANSION_PATTERN (nt
))
7873 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7874 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7875 else if (ARGUMENT_PACK_P (ot
))
7880 if (!ARGUMENT_PACK_P (nt
))
7883 opack
= ARGUMENT_PACK_ARGS (ot
);
7884 npack
= ARGUMENT_PACK_ARGS (nt
);
7885 len
= TREE_VEC_LENGTH (opack
);
7886 if (TREE_VEC_LENGTH (npack
) != len
)
7888 for (i
= 0; i
< len
; ++i
)
7889 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7890 TREE_VEC_ELT (npack
, i
)))
7894 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7896 else if (TYPE_P (nt
))
7900 /* Don't treat an alias template specialization with dependent
7901 arguments as equivalent to its underlying type when used as a
7902 template argument; we need them to be distinct so that we
7903 substitute into the specialization arguments at instantiation
7904 time. And aliases can't be equivalent without being ==, so
7905 we don't need to look any deeper. */
7906 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7909 return same_type_p (ot
, nt
);
7911 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7915 /* Try to treat a template non-type argument that has been converted
7916 to the parameter type as equivalent to one that hasn't yet. */
7917 for (enum tree_code code1
= TREE_CODE (ot
);
7918 CONVERT_EXPR_CODE_P (code1
)
7919 || code1
== NON_LVALUE_EXPR
;
7920 code1
= TREE_CODE (ot
))
7921 ot
= TREE_OPERAND (ot
, 0);
7922 for (enum tree_code code2
= TREE_CODE (nt
);
7923 CONVERT_EXPR_CODE_P (code2
)
7924 || code2
== NON_LVALUE_EXPR
;
7925 code2
= TREE_CODE (nt
))
7926 nt
= TREE_OPERAND (nt
, 0);
7928 return cp_tree_equal (ot
, nt
);
7932 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7933 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7934 NEWARG_PTR with the offending arguments if they are non-NULL. */
7937 comp_template_args (tree oldargs
, tree newargs
,
7938 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7942 if (oldargs
== newargs
)
7945 if (!oldargs
|| !newargs
)
7948 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7951 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7953 tree nt
= TREE_VEC_ELT (newargs
, i
);
7954 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7956 if (! template_args_equal (ot
, nt
))
7958 if (oldarg_ptr
!= NULL
)
7960 if (newarg_ptr
!= NULL
)
7969 add_pending_template (tree d
)
7971 tree ti
= (TYPE_P (d
)
7972 ? CLASSTYPE_TEMPLATE_INFO (d
)
7973 : DECL_TEMPLATE_INFO (d
));
7974 struct pending_template
*pt
;
7977 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7980 /* We are called both from instantiate_decl, where we've already had a
7981 tinst_level pushed, and instantiate_template, where we haven't.
7983 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7986 push_tinst_level (d
);
7988 pt
= ggc_alloc
<pending_template
> ();
7990 pt
->tinst
= current_tinst_level
;
7991 if (last_pending_template
)
7992 last_pending_template
->next
= pt
;
7994 pending_templates
= pt
;
7996 last_pending_template
= pt
;
7998 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8005 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8006 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8007 documentation for TEMPLATE_ID_EXPR. */
8010 lookup_template_function (tree fns
, tree arglist
)
8014 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8015 return error_mark_node
;
8017 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8019 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8021 error ("%q#D is not a function template", fns
);
8022 return error_mark_node
;
8025 if (BASELINK_P (fns
))
8027 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8029 BASELINK_FUNCTIONS (fns
),
8034 type
= TREE_TYPE (fns
);
8035 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8036 type
= unknown_type_node
;
8038 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8041 /* Within the scope of a template class S<T>, the name S gets bound
8042 (in build_self_reference) to a TYPE_DECL for the class, not a
8043 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8044 or one of its enclosing classes, and that type is a template,
8045 return the associated TEMPLATE_DECL. Otherwise, the original
8048 Also handle the case when DECL is a TREE_LIST of ambiguous
8049 injected-class-names from different bases. */
8052 maybe_get_template_decl_from_type_decl (tree decl
)
8054 if (decl
== NULL_TREE
)
8057 /* DR 176: A lookup that finds an injected-class-name (10.2
8058 [class.member.lookup]) can result in an ambiguity in certain cases
8059 (for example, if it is found in more than one base class). If all of
8060 the injected-class-names that are found refer to specializations of
8061 the same class template, and if the name is followed by a
8062 template-argument-list, the reference refers to the class template
8063 itself and not a specialization thereof, and is not ambiguous. */
8064 if (TREE_CODE (decl
) == TREE_LIST
)
8066 tree t
, tmpl
= NULL_TREE
;
8067 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8069 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8072 else if (tmpl
!= elt
)
8075 if (tmpl
&& t
== NULL_TREE
)
8081 return (decl
!= NULL_TREE
8082 && DECL_SELF_REFERENCE_P (decl
)
8083 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8084 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8087 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8088 parameters, find the desired type.
8090 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8092 IN_DECL, if non-NULL, is the template declaration we are trying to
8095 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8096 the class we are looking up.
8098 Issue error and warning messages under control of COMPLAIN.
8100 If the template class is really a local class in a template
8101 function, then the FUNCTION_CONTEXT is the function in which it is
8104 ??? Note that this function is currently called *twice* for each
8105 template-id: the first time from the parser, while creating the
8106 incomplete type (finish_template_type), and the second type during the
8107 real instantiation (instantiate_template_class). This is surely something
8108 that we want to avoid. It also causes some problems with argument
8109 coercion (see convert_nontype_argument for more information on this). */
8112 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8113 int entering_scope
, tsubst_flags_t complain
)
8115 tree templ
= NULL_TREE
, parmlist
;
8122 if (identifier_p (d1
))
8124 tree value
= innermost_non_namespace_value (d1
);
8125 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8130 push_decl_namespace (context
);
8131 templ
= lookup_name (d1
);
8132 templ
= maybe_get_template_decl_from_type_decl (templ
);
8134 pop_decl_namespace ();
8137 context
= DECL_CONTEXT (templ
);
8139 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8141 tree type
= TREE_TYPE (d1
);
8143 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8144 an implicit typename for the second A. Deal with it. */
8145 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8146 type
= TREE_TYPE (type
);
8148 if (CLASSTYPE_TEMPLATE_INFO (type
))
8150 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8151 d1
= DECL_NAME (templ
);
8154 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8155 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8157 templ
= TYPE_TI_TEMPLATE (d1
);
8158 d1
= DECL_NAME (templ
);
8160 else if (DECL_TYPE_TEMPLATE_P (d1
))
8163 d1
= DECL_NAME (templ
);
8164 context
= DECL_CONTEXT (templ
);
8166 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8169 d1
= DECL_NAME (templ
);
8172 /* Issue an error message if we didn't find a template. */
8175 if (complain
& tf_error
)
8176 error ("%qT is not a template", d1
);
8177 return error_mark_node
;
8180 if (TREE_CODE (templ
) != TEMPLATE_DECL
8181 /* Make sure it's a user visible template, if it was named by
8183 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8184 && !PRIMARY_TEMPLATE_P (templ
)))
8186 if (complain
& tf_error
)
8188 error ("non-template type %qT used as a template", d1
);
8190 error ("for template declaration %q+D", in_decl
);
8192 return error_mark_node
;
8195 complain
&= ~tf_user
;
8197 /* An alias that just changes the name of a template is equivalent to the
8198 other template, so if any of the arguments are pack expansions, strip
8199 the alias to avoid problems with a pack expansion passed to a non-pack
8200 alias template parameter (DR 1430). */
8201 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8202 templ
= get_underlying_template (templ
);
8204 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8206 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8207 template arguments */
8213 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
8215 /* Consider an example where a template template parameter declared as
8217 template <class T, class U = std::allocator<T> > class TT
8219 The template parameter level of T and U are one level larger than
8220 of TT. To proper process the default argument of U, say when an
8221 instantiation `TT<int>' is seen, we need to build the full
8222 arguments containing {int} as the innermost level. Outer levels,
8223 available when not appearing as default template argument, can be
8224 obtained from the arguments of the enclosing template.
8226 Suppose that TT is later substituted with std::vector. The above
8227 instantiation is `TT<int, std::allocator<T> >' with TT at
8228 level 1, and T at level 2, while the template arguments at level 1
8229 becomes {std::vector} and the inner level 2 is {int}. */
8231 outer
= DECL_CONTEXT (templ
);
8233 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
8234 else if (current_template_parms
)
8236 /* This is an argument of the current template, so we haven't set
8237 DECL_CONTEXT yet. */
8238 tree relevant_template_parms
;
8240 /* Parameter levels that are greater than the level of the given
8241 template template parm are irrelevant. */
8242 relevant_template_parms
= current_template_parms
;
8243 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
8244 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
8245 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
8247 outer
= template_parms_to_args (relevant_template_parms
);
8251 arglist
= add_to_template_args (outer
, arglist
);
8253 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
8255 /*require_all_args=*/true,
8256 /*use_default_args=*/true);
8257 if (arglist2
== error_mark_node
8258 || (!uses_template_parms (arglist2
)
8259 && check_instantiated_args (templ
, arglist2
, complain
)))
8260 return error_mark_node
;
8262 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8267 tree template_type
= TREE_TYPE (templ
);
8270 tree found
= NULL_TREE
;
8273 int is_dependent_type
;
8274 int use_partial_inst_tmpl
= false;
8276 if (template_type
== error_mark_node
)
8277 /* An error occurred while building the template TEMPL, and a
8278 diagnostic has most certainly been emitted for that
8279 already. Let's propagate that error. */
8280 return error_mark_node
;
8282 gen_tmpl
= most_general_template (templ
);
8283 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8284 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8285 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8287 if (arg_depth
== 1 && parm_depth
> 1)
8289 /* We've been given an incomplete set of template arguments.
8292 template <class T> struct S1 {
8293 template <class U> struct S2 {};
8294 template <class U> struct S2<U*> {};
8297 we will be called with an ARGLIST of `U*', but the
8298 TEMPLATE will be `template <class T> template
8299 <class U> struct S1<T>::S2'. We must fill in the missing
8302 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
8304 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8307 /* Now we should have enough arguments. */
8308 gcc_assert (parm_depth
== arg_depth
);
8310 /* From here on, we're only interested in the most general
8313 /* Calculate the BOUND_ARGS. These will be the args that are
8314 actually tsubst'd into the definition to create the
8316 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8318 /*require_all_args=*/true,
8319 /*use_default_args=*/true);
8321 if (arglist
== error_mark_node
)
8322 /* We were unable to bind the arguments. */
8323 return error_mark_node
;
8325 /* In the scope of a template class, explicit references to the
8326 template class refer to the type of the template, not any
8327 instantiation of it. For example, in:
8329 template <class T> class C { void f(C<T>); }
8331 the `C<T>' is just the same as `C'. Outside of the
8332 class, however, such a reference is an instantiation. */
8334 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8335 || currently_open_class (template_type
))
8336 /* comp_template_args is expensive, check it last. */
8337 && comp_template_args (TYPE_TI_ARGS (template_type
),
8339 return template_type
;
8341 /* If we already have this specialization, return it. */
8342 elt
.tmpl
= gen_tmpl
;
8344 elt
.spec
= NULL_TREE
;
8345 hash
= spec_hasher::hash (&elt
);
8346 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8351 /* If the the template's constraints are not satisfied,
8352 then we cannot form a valid type.
8354 Note that the check is deferred until after the hash
8355 lookup. This prevents redundant checks on previously
8356 instantiated specializations. */
8357 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8359 if (complain
& tf_error
)
8361 error ("template constraint failure");
8362 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8364 return error_mark_node
;
8367 is_dependent_type
= uses_template_parms (arglist
);
8369 /* If the deduced arguments are invalid, then the binding
8371 if (!is_dependent_type
8372 && check_instantiated_args (gen_tmpl
,
8373 INNERMOST_TEMPLATE_ARGS (arglist
),
8375 return error_mark_node
;
8377 if (!is_dependent_type
8378 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8379 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8380 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8382 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8383 DECL_NAME (gen_tmpl
),
8384 /*tag_scope=*/ts_global
);
8388 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8390 if (context
== error_mark_node
)
8391 return error_mark_node
;
8394 context
= global_namespace
;
8396 /* Create the type. */
8397 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8399 /* The user referred to a specialization of an alias
8400 template represented by GEN_TMPL.
8402 [temp.alias]/2 says:
8404 When a template-id refers to the specialization of an
8405 alias template, it is equivalent to the associated
8406 type obtained by substitution of its
8407 template-arguments for the template-parameters in the
8408 type-id of the alias template. */
8410 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8411 /* Note that the call above (by indirectly calling
8412 register_specialization in tsubst_decl) registers the
8413 TYPE_DECL representing the specialization of the alias
8414 template. So next time someone substitutes ARGLIST for
8415 the template parms into the alias template (GEN_TMPL),
8416 she'll get that TYPE_DECL back. */
8418 if (t
== error_mark_node
)
8421 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8423 if (!is_dependent_type
)
8425 set_current_access_from_decl (TYPE_NAME (template_type
));
8426 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8427 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8428 arglist
, complain
, in_decl
),
8429 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8430 arglist
, complain
, in_decl
),
8431 SCOPED_ENUM_P (template_type
), NULL
);
8433 if (t
== error_mark_node
)
8438 /* We don't want to call start_enum for this type, since
8439 the values for the enumeration constants may involve
8440 template parameters. And, no one should be interested
8441 in the enumeration constants for such a type. */
8442 t
= cxx_make_type (ENUMERAL_TYPE
);
8443 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8445 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8446 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8447 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8449 else if (CLASS_TYPE_P (template_type
))
8451 t
= make_class_type (TREE_CODE (template_type
));
8452 CLASSTYPE_DECLARED_CLASS (t
)
8453 = CLASSTYPE_DECLARED_CLASS (template_type
);
8454 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8455 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
8457 /* A local class. Make sure the decl gets registered properly. */
8458 if (context
== current_function_decl
)
8459 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8461 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8462 /* This instantiation is another name for the primary
8463 template type. Set the TYPE_CANONICAL field
8465 TYPE_CANONICAL (t
) = template_type
;
8466 else if (any_template_arguments_need_structural_equality_p (arglist
))
8467 /* Some of the template arguments require structural
8468 equality testing, so this template class requires
8469 structural equality testing. */
8470 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8475 /* If we called start_enum or pushtag above, this information
8476 will already be set up. */
8479 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8481 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8482 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8483 DECL_SOURCE_LOCATION (type_decl
)
8484 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8487 type_decl
= TYPE_NAME (t
);
8489 if (CLASS_TYPE_P (template_type
))
8491 TREE_PRIVATE (type_decl
)
8492 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8493 TREE_PROTECTED (type_decl
)
8494 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8495 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8497 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8498 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8502 if (OVERLOAD_TYPE_P (t
)
8503 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8505 static const char *tags
[] = {"abi_tag", "may_alias"};
8507 for (unsigned ix
= 0; ix
!= 2; ix
++)
8510 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8514 = tree_cons (TREE_PURPOSE (attributes
),
8515 TREE_VALUE (attributes
),
8516 TYPE_ATTRIBUTES (t
));
8520 /* Let's consider the explicit specialization of a member
8521 of a class template specialization that is implicitly instantiated,
8526 template<class U> struct M {}; //#0
8531 struct S<int>::M<char> //#1
8535 [temp.expl.spec]/4 says this is valid.
8537 In this case, when we write:
8540 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8543 When we encounter #1, we want to store the partial instantiation
8544 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8546 For all cases other than this "explicit specialization of member of a
8547 class template", we just want to store the most general template into
8548 the CLASSTYPE_TI_TEMPLATE of M.
8550 This case of "explicit specialization of member of a class template"
8552 1/ the enclosing class is an instantiation of, and therefore not
8553 the same as, the context of the most general template, and
8554 2/ we aren't looking at the partial instantiation itself, i.e.
8555 the innermost arguments are not the same as the innermost parms of
8556 the most general template.
8558 So it's only when 1/ and 2/ happens that we want to use the partial
8559 instantiation of the member template in lieu of its most general
8562 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8563 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8564 /* the enclosing class must be an instantiation... */
8565 && CLASS_TYPE_P (context
)
8566 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8568 tree partial_inst_args
;
8569 TREE_VEC_LENGTH (arglist
)--;
8570 ++processing_template_decl
;
8572 tsubst (INNERMOST_TEMPLATE_ARGS
8573 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
8574 arglist
, complain
, NULL_TREE
);
8575 --processing_template_decl
;
8576 TREE_VEC_LENGTH (arglist
)++;
8577 if (partial_inst_args
== error_mark_node
)
8578 return error_mark_node
;
8579 use_partial_inst_tmpl
=
8580 /*...and we must not be looking at the partial instantiation
8582 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8586 if (!use_partial_inst_tmpl
)
8587 /* This case is easy; there are no member templates involved. */
8591 /* This is a full instantiation of a member template. Find
8592 the partial instantiation of which this is an instance. */
8594 /* Temporarily reduce by one the number of levels in the ARGLIST
8595 so as to avoid comparing the last set of arguments. */
8596 TREE_VEC_LENGTH (arglist
)--;
8597 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
8598 TREE_VEC_LENGTH (arglist
)++;
8599 /* FOUND is either a proper class type, or an alias
8600 template specialization. In the later case, it's a
8601 TYPE_DECL, resulting from the substituting of arguments
8602 for parameters in the TYPE_DECL of the alias template
8603 done earlier. So be careful while getting the template
8605 found
= TREE_CODE (found
) == TEMPLATE_DECL
8607 : TREE_CODE (found
) == TYPE_DECL
8608 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
8609 : CLASSTYPE_TI_TEMPLATE (found
);
8612 // Build template info for the new specialization.
8613 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8616 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8617 entry
= ggc_alloc
<spec_entry
> ();
8621 /* Note this use of the partial instantiation so we can check it
8622 later in maybe_process_partial_specialization. */
8623 DECL_TEMPLATE_INSTANTIATIONS (found
)
8624 = tree_cons (arglist
, t
,
8625 DECL_TEMPLATE_INSTANTIATIONS (found
));
8627 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8628 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8629 /* Now that the type has been registered on the instantiations
8630 list, we set up the enumerators. Because the enumeration
8631 constants may involve the enumeration type itself, we make
8632 sure to register the type first, and then create the
8633 constants. That way, doing tsubst_expr for the enumeration
8634 constants won't result in recursive calls here; we'll find
8635 the instantiation and exit above. */
8636 tsubst_enum (template_type
, t
, arglist
);
8638 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8639 /* If the type makes use of template parameters, the
8640 code that generates debugging information will crash. */
8641 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8643 /* Possibly limit visibility based on template args. */
8644 TREE_PUBLIC (type_decl
) = 1;
8645 determine_visibility (type_decl
);
8647 inherit_targ_abi_tags (t
);
8653 /* Wrapper for lookup_template_class_1. */
8656 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8657 int entering_scope
, tsubst_flags_t complain
)
8660 timevar_push (TV_TEMPLATE_INST
);
8661 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8662 entering_scope
, complain
);
8663 timevar_pop (TV_TEMPLATE_INST
);
8667 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8670 lookup_template_variable (tree templ
, tree arglist
)
8672 /* The type of the expression is NULL_TREE since the template-id could refer
8673 to an explicit or partial specialization. */
8674 tree type
= NULL_TREE
;
8675 if (flag_concepts
&& variable_concept_p (templ
))
8676 /* Except that concepts are always bool. */
8677 type
= boolean_type_node
;
8678 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8681 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8684 finish_template_variable (tree var
, tsubst_flags_t complain
)
8686 tree templ
= TREE_OPERAND (var
, 0);
8687 tree arglist
= TREE_OPERAND (var
, 1);
8689 /* We never want to return a VAR_DECL for a variable concept, since they
8690 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8691 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
8692 if (concept_p
&& processing_template_decl
)
8695 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8696 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8698 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8699 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8701 /*use_default*/true);
8703 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
8705 if (complain
& tf_error
)
8707 error ("use of invalid variable template %qE", var
);
8708 diagnose_constraints (location_of (var
), templ
, arglist
);
8710 return error_mark_node
;
8713 /* If a template-id refers to a specialization of a variable
8714 concept, then the expression is true if and only if the
8715 concept's constraints are satisfied by the given template
8718 NOTE: This is an extension of Concepts Lite TS that
8719 allows constraints to be used in expressions. */
8722 tree decl
= DECL_TEMPLATE_RESULT (templ
);
8723 return evaluate_variable_concept (decl
, arglist
);
8726 return instantiate_template (templ
, arglist
, complain
);
8729 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
8730 TARGS template args, and instantiate it if it's not dependent. */
8733 lookup_and_finish_template_variable (tree templ
, tree targs
,
8734 tsubst_flags_t complain
)
8736 templ
= lookup_template_variable (templ
, targs
);
8737 if (!any_dependent_template_arguments_p (targs
))
8739 templ
= finish_template_variable (templ
, complain
);
8743 return convert_from_reference (templ
);
8751 /* True when we should also visit template parameters that occur in
8752 non-deduced contexts. */
8753 bool include_nondeduced_p
;
8754 hash_set
<tree
> *visited
;
8757 /* Called from for_each_template_parm via walk_tree. */
8760 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8763 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8764 tree_fn_t fn
= pfd
->fn
;
8765 void *data
= pfd
->data
;
8766 tree result
= NULL_TREE
;
8768 #define WALK_SUBTREE(NODE) \
8771 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8772 pfd->include_nondeduced_p); \
8773 if (result) goto out; \
8778 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
8779 WALK_SUBTREE (TYPE_CONTEXT (t
));
8781 switch (TREE_CODE (t
))
8784 if (TYPE_PTRMEMFUNC_P (t
))
8790 if (!TYPE_TEMPLATE_INFO (t
))
8793 WALK_SUBTREE (TYPE_TI_ARGS (t
));
8797 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
8798 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
8802 /* Since we're not going to walk subtrees, we have to do this
8804 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
8808 /* Check the return type. */
8809 WALK_SUBTREE (TREE_TYPE (t
));
8811 /* Check the parameter types. Since default arguments are not
8812 instantiated until they are needed, the TYPE_ARG_TYPES may
8813 contain expressions that involve template parameters. But,
8814 no-one should be looking at them yet. And, once they're
8815 instantiated, they don't contain template parameters, so
8816 there's no point in looking at them then, either. */
8820 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8821 WALK_SUBTREE (TREE_VALUE (parm
));
8823 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8824 want walk_tree walking into them itself. */
8830 case UNDERLYING_TYPE
:
8831 if (pfd
->include_nondeduced_p
8832 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
8834 pfd
->include_nondeduced_p
))
8835 return error_mark_node
;
8840 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
8841 WALK_SUBTREE (DECL_TI_ARGS (t
));
8846 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
8847 WALK_SUBTREE (DECL_INITIAL (t
));
8848 if (DECL_CONTEXT (t
)
8849 && pfd
->include_nondeduced_p
)
8850 WALK_SUBTREE (DECL_CONTEXT (t
));
8853 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8854 /* Record template parameters such as `T' inside `TT<T>'. */
8855 WALK_SUBTREE (TYPE_TI_ARGS (t
));
8858 case TEMPLATE_TEMPLATE_PARM
:
8859 case TEMPLATE_TYPE_PARM
:
8860 case TEMPLATE_PARM_INDEX
:
8861 if (fn
&& (*fn
)(t
, data
))
8868 /* A template template parameter is encountered. */
8869 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8870 WALK_SUBTREE (TREE_TYPE (t
));
8872 /* Already substituted template template parameter */
8877 /* A template-id in a TYPENAME_TYPE might be a deduced context after
8878 partial instantiation. */
8879 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
8883 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8884 && pfd
->include_nondeduced_p
)
8885 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
8890 /* If there's no type, then this thing must be some expression
8891 involving template parameters. */
8892 if (!fn
&& !TREE_TYPE (t
))
8893 return error_mark_node
;
8898 case IMPLICIT_CONV_EXPR
:
8899 case REINTERPRET_CAST_EXPR
:
8900 case CONST_CAST_EXPR
:
8901 case STATIC_CAST_EXPR
:
8902 case DYNAMIC_CAST_EXPR
:
8906 case PSEUDO_DTOR_EXPR
:
8908 return error_mark_node
;
8917 /* We didn't find any template parameters we liked. */
8922 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8923 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8924 call FN with the parameter and the DATA.
8925 If FN returns nonzero, the iteration is terminated, and
8926 for_each_template_parm returns 1. Otherwise, the iteration
8927 continues. If FN never returns a nonzero value, the value
8928 returned by for_each_template_parm is 0. If FN is NULL, it is
8929 considered to be the function which always returns 1.
8931 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8932 parameters that occur in non-deduced contexts. When false, only
8933 visits those template parameters that can be deduced. */
8936 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8937 hash_set
<tree
> *visited
,
8938 bool include_nondeduced_p
)
8940 struct pair_fn_data pfd
;
8946 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8948 /* Walk the tree. (Conceptually, we would like to walk without
8949 duplicates, but for_each_template_parm_r recursively calls
8950 for_each_template_parm, so we would need to reorganize a fair
8951 bit to use walk_tree_without_duplicates, so we keep our own
8954 pfd
.visited
= visited
;
8956 pfd
.visited
= new hash_set
<tree
>;
8957 result
= cp_walk_tree (&t
,
8958 for_each_template_parm_r
,
8972 /* Returns true if T depends on any template parameter. */
8975 uses_template_parms (tree t
)
8981 int saved_processing_template_decl
;
8983 saved_processing_template_decl
= processing_template_decl
;
8984 if (!saved_processing_template_decl
)
8985 processing_template_decl
= 1;
8987 dependent_p
= dependent_type_p (t
);
8988 else if (TREE_CODE (t
) == TREE_VEC
)
8989 dependent_p
= any_dependent_template_arguments_p (t
);
8990 else if (TREE_CODE (t
) == TREE_LIST
)
8991 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8992 || uses_template_parms (TREE_CHAIN (t
)));
8993 else if (TREE_CODE (t
) == TYPE_DECL
)
8994 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8997 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8998 || TREE_CODE (t
) == OVERLOAD
9001 || TREE_CODE (t
) == TRAIT_EXPR
9002 || TREE_CODE (t
) == CONSTRUCTOR
9003 || CONSTANT_CLASS_P (t
))
9004 dependent_p
= (type_dependent_expression_p (t
)
9005 || value_dependent_expression_p (t
));
9008 gcc_assert (t
== error_mark_node
);
9009 dependent_p
= false;
9012 processing_template_decl
= saved_processing_template_decl
;
9017 /* Returns true iff current_function_decl is an incompletely instantiated
9018 template. Useful instead of processing_template_decl because the latter
9019 is set to 0 during instantiate_non_dependent_expr. */
9022 in_template_function (void)
9024 tree fn
= current_function_decl
;
9026 ++processing_template_decl
;
9027 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9028 && DECL_TEMPLATE_INFO (fn
)
9029 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9030 --processing_template_decl
;
9034 /* Returns true if T depends on any template parameter with level LEVEL. */
9037 uses_template_parms_level (tree t
, int level
)
9039 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9040 /*include_nondeduced_p=*/true);
9043 /* Returns true if the signature of DECL depends on any template parameter from
9044 its enclosing class. */
9047 uses_outer_template_parms (tree decl
)
9049 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9052 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9053 &depth
, NULL
, /*include_nondeduced_p=*/true))
9055 if (PRIMARY_TEMPLATE_P (decl
)
9056 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9057 (DECL_TEMPLATE_PARMS (decl
)),
9058 template_parm_outer_level
,
9059 &depth
, NULL
, /*include_nondeduced_p=*/true))
9061 tree ci
= get_constraints (decl
);
9063 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9064 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9065 &depth
, NULL
, /*nondeduced*/true))
9070 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9071 ill-formed translation unit, i.e. a variable or function that isn't
9072 usable in a constant expression. */
9075 neglectable_inst_p (tree d
)
9078 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9079 : decl_maybe_constant_var_p (d
)));
9082 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9083 neglectable and instantiated from within an erroneous instantiation. */
9086 limit_bad_template_recursion (tree decl
)
9088 struct tinst_level
*lev
= current_tinst_level
;
9089 int errs
= errorcount
+ sorrycount
;
9090 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9093 for (; lev
; lev
= lev
->next
)
9094 if (neglectable_inst_p (lev
->decl
))
9097 return (lev
&& errs
> lev
->errors
);
9100 static int tinst_depth
;
9101 extern int max_tinst_depth
;
9104 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9106 /* We're starting to instantiate D; record the template instantiation context
9107 for diagnostics and to restore it later. */
9110 push_tinst_level (tree d
)
9112 return push_tinst_level_loc (d
, input_location
);
9115 /* We're starting to instantiate D; record the template instantiation context
9116 at LOC for diagnostics and to restore it later. */
9119 push_tinst_level_loc (tree d
, location_t loc
)
9121 struct tinst_level
*new_level
;
9123 if (tinst_depth
>= max_tinst_depth
)
9125 /* Tell error.c not to try to instantiate any templates. */
9127 fatal_error (input_location
,
9128 "template instantiation depth exceeds maximum of %d"
9129 " (use -ftemplate-depth= to increase the maximum)",
9134 /* If the current instantiation caused problems, don't let it instantiate
9135 anything else. Do allow deduction substitution and decls usable in
9136 constant expressions. */
9137 if (limit_bad_template_recursion (d
))
9140 new_level
= ggc_alloc
<tinst_level
> ();
9141 new_level
->decl
= d
;
9142 new_level
->locus
= loc
;
9143 new_level
->errors
= errorcount
+sorrycount
;
9144 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9145 new_level
->next
= current_tinst_level
;
9146 current_tinst_level
= new_level
;
9149 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9150 depth_reached
= tinst_depth
;
9155 /* We're done instantiating this template; return to the instantiation
9159 pop_tinst_level (void)
9161 /* Restore the filename and line number stashed away when we started
9162 this instantiation. */
9163 input_location
= current_tinst_level
->locus
;
9164 current_tinst_level
= current_tinst_level
->next
;
9168 /* We're instantiating a deferred template; restore the template
9169 instantiation context in which the instantiation was requested, which
9170 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9173 reopen_tinst_level (struct tinst_level
*level
)
9175 struct tinst_level
*t
;
9178 for (t
= level
; t
; t
= t
->next
)
9181 current_tinst_level
= level
;
9183 if (current_tinst_level
)
9184 current_tinst_level
->errors
= errorcount
+sorrycount
;
9188 /* Returns the TINST_LEVEL which gives the original instantiation
9191 struct tinst_level
*
9192 outermost_tinst_level (void)
9194 struct tinst_level
*level
= current_tinst_level
;
9197 level
= level
->next
;
9201 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9202 vector of template arguments, as for tsubst.
9204 Returns an appropriate tsubst'd friend declaration. */
9207 tsubst_friend_function (tree decl
, tree args
)
9211 if (TREE_CODE (decl
) == FUNCTION_DECL
9212 && DECL_TEMPLATE_INSTANTIATION (decl
)
9213 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9214 /* This was a friend declared with an explicit template
9215 argument list, e.g.:
9219 to indicate that f was a template instantiation, not a new
9220 function declaration. Now, we have to figure out what
9221 instantiation of what template. */
9223 tree template_id
, arglist
, fns
;
9226 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9228 /* Friend functions are looked up in the containing namespace scope.
9229 We must enter that scope, to avoid finding member functions of the
9230 current class with same name. */
9231 push_nested_namespace (ns
);
9232 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9233 tf_warning_or_error
, NULL_TREE
,
9234 /*integral_constant_expression_p=*/false);
9235 pop_nested_namespace (ns
);
9236 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9237 tf_warning_or_error
, NULL_TREE
);
9238 template_id
= lookup_template_function (fns
, arglist
);
9240 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9241 tmpl
= determine_specialization (template_id
, new_friend
,
9243 /*need_member_template=*/0,
9244 TREE_VEC_LENGTH (args
),
9246 return instantiate_template (tmpl
, new_args
, tf_error
);
9249 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9251 /* The NEW_FRIEND will look like an instantiation, to the
9252 compiler, but is not an instantiation from the point of view of
9253 the language. For example, we might have had:
9255 template <class T> struct S {
9256 template <class U> friend void f(T, U);
9259 Then, in S<int>, template <class U> void f(int, U) is not an
9260 instantiation of anything. */
9261 if (new_friend
== error_mark_node
)
9262 return error_mark_node
;
9264 DECL_USE_TEMPLATE (new_friend
) = 0;
9265 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9267 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9268 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9269 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9272 /* The mangled name for the NEW_FRIEND is incorrect. The function
9273 is not a template instantiation and should not be mangled like
9274 one. Therefore, we forget the mangling here; we'll recompute it
9275 later if we need it. */
9276 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9278 SET_DECL_RTL (new_friend
, NULL
);
9279 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9282 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9285 tree new_friend_template_info
;
9286 tree new_friend_result_template_info
;
9288 int new_friend_is_defn
;
9290 /* We must save some information from NEW_FRIEND before calling
9291 duplicate decls since that function will free NEW_FRIEND if
9293 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9294 new_friend_is_defn
=
9295 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9296 (template_for_substitution (new_friend
)))
9298 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9300 /* This declaration is a `primary' template. */
9301 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9303 new_friend_result_template_info
9304 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9307 new_friend_result_template_info
= NULL_TREE
;
9309 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9310 if (new_friend_is_defn
)
9311 DECL_INITIAL (new_friend
) = error_mark_node
;
9313 /* Inside pushdecl_namespace_level, we will push into the
9314 current namespace. However, the friend function should go
9315 into the namespace of the template. */
9316 ns
= decl_namespace_context (new_friend
);
9317 push_nested_namespace (ns
);
9318 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9319 pop_nested_namespace (ns
);
9321 if (old_decl
== error_mark_node
)
9322 return error_mark_node
;
9324 if (old_decl
!= new_friend
)
9326 /* This new friend declaration matched an existing
9327 declaration. For example, given:
9329 template <class T> void f(T);
9330 template <class U> class C {
9331 template <class T> friend void f(T) {}
9334 the friend declaration actually provides the definition
9335 of `f', once C has been instantiated for some type. So,
9336 old_decl will be the out-of-class template declaration,
9337 while new_friend is the in-class definition.
9339 But, if `f' was called before this point, the
9340 instantiation of `f' will have DECL_TI_ARGS corresponding
9341 to `T' but not to `U', references to which might appear
9342 in the definition of `f'. Previously, the most general
9343 template for an instantiation of `f' was the out-of-class
9344 version; now it is the in-class version. Therefore, we
9345 run through all specialization of `f', adding to their
9346 DECL_TI_ARGS appropriately. In particular, they need a
9347 new set of outer arguments, corresponding to the
9348 arguments for this class instantiation.
9350 The same situation can arise with something like this:
9353 template <class T> class C {
9357 when `C<int>' is instantiated. Now, `f(int)' is defined
9360 if (!new_friend_is_defn
)
9361 /* On the other hand, if the in-class declaration does
9362 *not* provide a definition, then we don't want to alter
9363 existing definitions. We can just leave everything
9368 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9369 tree new_args
= TI_ARGS (new_friend_template_info
);
9371 /* Overwrite whatever template info was there before, if
9372 any, with the new template information pertaining to
9374 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9376 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9378 /* We should have called reregister_specialization in
9380 gcc_assert (retrieve_specialization (new_template
,
9384 /* Instantiate it if the global has already been used. */
9385 if (DECL_ODR_USED (old_decl
))
9386 instantiate_decl (old_decl
, /*defer_ok=*/true,
9387 /*expl_inst_class_mem_p=*/false);
9393 /* Indicate that the old function template is a partial
9395 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9396 = new_friend_result_template_info
;
9398 gcc_assert (new_template
9399 == most_general_template (new_template
));
9400 gcc_assert (new_template
!= old_decl
);
9402 /* Reassign any specializations already in the hash table
9403 to the new more general template, and add the
9404 additional template args. */
9405 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9409 tree spec
= TREE_VALUE (t
);
9412 elt
.tmpl
= old_decl
;
9413 elt
.args
= DECL_TI_ARGS (spec
);
9414 elt
.spec
= NULL_TREE
;
9416 decl_specializations
->remove_elt (&elt
);
9419 = add_outermost_template_args (new_args
,
9420 DECL_TI_ARGS (spec
));
9422 register_specialization
9423 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9426 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9430 /* The information from NEW_FRIEND has been merged into OLD_DECL
9431 by duplicate_decls. */
9432 new_friend
= old_decl
;
9437 tree context
= DECL_CONTEXT (new_friend
);
9441 template <class T> class C {
9442 template <class U> friend void C1<U>::f (); // case 1
9443 friend void C2<T>::f (); // case 2
9445 we only need to make sure CONTEXT is a complete type for
9446 case 2. To distinguish between the two cases, we note that
9447 CONTEXT of case 1 remains dependent type after tsubst while
9448 this isn't true for case 2. */
9449 ++processing_template_decl
;
9450 dependent_p
= dependent_type_p (context
);
9451 --processing_template_decl
;
9454 && !complete_type_or_else (context
, NULL_TREE
))
9455 return error_mark_node
;
9457 if (COMPLETE_TYPE_P (context
))
9459 tree fn
= new_friend
;
9460 /* do_friend adds the TEMPLATE_DECL for any member friend
9461 template even if it isn't a member template, i.e.
9462 template <class T> friend A<T>::f();
9463 Look through it in that case. */
9464 if (TREE_CODE (fn
) == TEMPLATE_DECL
9465 && !PRIMARY_TEMPLATE_P (fn
))
9466 fn
= DECL_TEMPLATE_RESULT (fn
);
9467 /* Check to see that the declaration is really present, and,
9468 possibly obtain an improved declaration. */
9469 fn
= check_classfn (context
, fn
, NULL_TREE
);
9479 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9480 template arguments, as for tsubst.
9482 Returns an appropriate tsubst'd friend type or error_mark_node on
9486 tsubst_friend_class (tree friend_tmpl
, tree args
)
9492 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9494 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9495 return TREE_TYPE (t
);
9498 context
= CP_DECL_CONTEXT (friend_tmpl
);
9500 if (context
!= global_namespace
)
9502 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9503 push_nested_namespace (context
);
9505 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9508 /* Look for a class template declaration. We look for hidden names
9509 because two friend declarations of the same template are the
9510 same. For example, in:
9513 template <typename> friend class F;
9515 template <typename> struct B {
9516 template <typename> friend class F;
9519 both F templates are the same. */
9520 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
9521 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
9523 /* But, if we don't find one, it might be because we're in a
9524 situation like this:
9532 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9533 for `S<int>', not the TEMPLATE_DECL. */
9534 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
9536 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
9537 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
9540 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9542 /* The friend template has already been declared. Just
9543 check to see that the declarations match, and install any new
9544 default parameters. We must tsubst the default parameters,
9545 of course. We only need the innermost template parameters
9546 because that is all that redeclare_class_template will look
9548 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9549 > TMPL_ARGS_DEPTH (args
))
9552 location_t saved_input_location
;
9553 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9554 args
, tf_warning_or_error
);
9556 saved_input_location
= input_location
;
9557 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9558 tree cons
= get_constraints (tmpl
);
9559 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
9560 input_location
= saved_input_location
;
9564 friend_type
= TREE_TYPE (tmpl
);
9568 /* The friend template has not already been declared. In this
9569 case, the instantiation of the template class will cause the
9570 injection of this template into the global scope. */
9571 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
9572 if (tmpl
== error_mark_node
)
9573 return error_mark_node
;
9575 /* The new TMPL is not an instantiation of anything, so we
9576 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9577 the new type because that is supposed to be the corresponding
9578 template decl, i.e., TMPL. */
9579 DECL_USE_TEMPLATE (tmpl
) = 0;
9580 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
9581 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
9582 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
9583 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
9585 /* Inject this template into the global scope. */
9586 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
9589 if (context
!= global_namespace
)
9591 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9592 pop_nested_namespace (context
);
9594 pop_nested_class ();
9600 /* Returns zero if TYPE cannot be completed later due to circularity.
9601 Otherwise returns one. */
9604 can_complete_type_without_circularity (tree type
)
9606 if (type
== NULL_TREE
|| type
== error_mark_node
)
9608 else if (COMPLETE_TYPE_P (type
))
9610 else if (TREE_CODE (type
) == ARRAY_TYPE
)
9611 return can_complete_type_without_circularity (TREE_TYPE (type
));
9612 else if (CLASS_TYPE_P (type
)
9613 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
9619 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
9620 tsubst_flags_t
, tree
);
9622 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9623 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9626 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
9627 tsubst_flags_t complain
, tree in_decl
)
9629 gcc_assert (ATTR_IS_DEPENDENT (t
));
9631 tree val
= TREE_VALUE (t
);
9632 if (val
== NULL_TREE
)
9633 /* Nothing to do. */;
9634 else if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
9635 && is_attribute_p ("omp declare simd",
9636 get_attribute_name (t
)))
9638 tree clauses
= TREE_VALUE (val
);
9639 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
9641 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9642 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
9643 tree parms
= DECL_ARGUMENTS (*decl_p
);
9645 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9647 val
= build_tree_list (NULL_TREE
, clauses
);
9651 /* If the first attribute argument is an identifier, don't
9652 pass it through tsubst. Attributes like mode, format,
9653 cleanup and several target specific attributes expect it
9655 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
9658 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
9659 /*integral_constant_expression_p=*/false);
9660 if (chain
!= TREE_CHAIN (val
))
9661 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
9663 else if (PACK_EXPANSION_P (val
))
9665 /* An attribute pack expansion. */
9666 tree purp
= TREE_PURPOSE (t
);
9667 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
9668 int len
= TREE_VEC_LENGTH (pack
);
9669 tree list
= NULL_TREE
;
9671 for (int i
= 0; i
< len
; ++i
)
9673 tree elt
= TREE_VEC_ELT (pack
, i
);
9674 *q
= build_tree_list (purp
, elt
);
9675 q
= &TREE_CHAIN (*q
);
9680 val
= tsubst_expr (val
, args
, complain
, in_decl
,
9681 /*integral_constant_expression_p=*/false);
9683 if (val
!= TREE_VALUE (t
))
9684 return build_tree_list (TREE_PURPOSE (t
), val
);
9688 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
9689 unchanged or a new TREE_LIST chain. */
9692 tsubst_attributes (tree attributes
, tree args
,
9693 tsubst_flags_t complain
, tree in_decl
)
9695 tree last_dep
= NULL_TREE
;
9697 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
9698 if (ATTR_IS_DEPENDENT (t
))
9701 attributes
= copy_list (attributes
);
9706 for (tree
*p
= &attributes
; *p
; )
9709 if (ATTR_IS_DEPENDENT (t
))
9711 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
9716 p
= &TREE_CHAIN (*p
);
9718 *p
= TREE_CHAIN (t
);
9722 p
= &TREE_CHAIN (*p
);
9728 /* Apply any attributes which had to be deferred until instantiation
9729 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9730 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9733 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
9734 tree args
, tsubst_flags_t complain
, tree in_decl
)
9736 tree last_dep
= NULL_TREE
;
9740 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
9741 if (ATTR_IS_DEPENDENT (t
))
9744 attributes
= copy_list (attributes
);
9748 if (DECL_P (*decl_p
))
9750 if (TREE_TYPE (*decl_p
) == error_mark_node
)
9752 p
= &DECL_ATTRIBUTES (*decl_p
);
9755 p
= &TYPE_ATTRIBUTES (*decl_p
);
9759 tree late_attrs
= NULL_TREE
;
9760 tree
*q
= &late_attrs
;
9762 for (*p
= attributes
; *p
; )
9765 if (ATTR_IS_DEPENDENT (t
))
9767 *p
= TREE_CHAIN (t
);
9768 TREE_CHAIN (t
) = NULL_TREE
;
9769 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
9771 q
= &TREE_CHAIN (*q
);
9775 p
= &TREE_CHAIN (t
);
9778 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9782 /* Perform (or defer) access check for typedefs that were referenced
9783 from within the template TMPL code.
9784 This is a subroutine of instantiate_decl and instantiate_class_template.
9785 TMPL is the template to consider and TARGS is the list of arguments of
9789 perform_typedefs_access_check (tree tmpl
, tree targs
)
9791 location_t saved_location
;
9793 qualified_typedef_usage_t
*iter
;
9796 || (!CLASS_TYPE_P (tmpl
)
9797 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9800 saved_location
= input_location
;
9801 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9803 tree type_decl
= iter
->typedef_decl
;
9804 tree type_scope
= iter
->context
;
9806 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9809 if (uses_template_parms (type_decl
))
9810 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9811 if (uses_template_parms (type_scope
))
9812 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9814 /* Make access check error messages point to the location
9815 of the use of the typedef. */
9816 input_location
= iter
->locus
;
9817 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9818 type_decl
, type_decl
,
9819 tf_warning_or_error
);
9821 input_location
= saved_location
;
9825 instantiate_class_template_1 (tree type
)
9827 tree templ
, args
, pattern
, t
, member
;
9831 unsigned int saved_maximum_field_alignment
;
9834 if (type
== error_mark_node
)
9835 return error_mark_node
;
9837 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9838 || uses_template_parms (type
))
9841 /* Figure out which template is being instantiated. */
9842 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9843 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9845 /* Determine what specialization of the original template to
9847 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9848 if (t
== error_mark_node
)
9850 TYPE_BEING_DEFINED (type
) = 1;
9851 return error_mark_node
;
9855 /* This TYPE is actually an instantiation of a partial
9856 specialization. We replace the innermost set of ARGS with
9857 the arguments appropriate for substitution. For example,
9860 template <class T> struct S {};
9861 template <class T> struct S<T*> {};
9863 and supposing that we are instantiating S<int*>, ARGS will
9864 presently be {int*} -- but we need {int}. */
9865 pattern
= TREE_TYPE (t
);
9866 args
= TREE_PURPOSE (t
);
9870 pattern
= TREE_TYPE (templ
);
9871 args
= CLASSTYPE_TI_ARGS (type
);
9874 /* If the template we're instantiating is incomplete, then clearly
9875 there's nothing we can do. */
9876 if (!COMPLETE_TYPE_P (pattern
))
9879 /* If we've recursively instantiated too many templates, stop. */
9880 if (! push_tinst_level (type
))
9883 /* Now we're really doing the instantiation. Mark the type as in
9884 the process of being defined. */
9885 TYPE_BEING_DEFINED (type
) = 1;
9887 /* We may be in the middle of deferred access check. Disable
9889 push_deferring_access_checks (dk_no_deferred
);
9891 int saved_unevaluated_operand
= cp_unevaluated_operand
;
9892 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9894 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9895 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9896 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
9897 fn_context
= error_mark_node
;
9899 push_to_top_level ();
9902 cp_unevaluated_operand
= 0;
9903 c_inhibit_evaluation_warnings
= 0;
9905 /* Use #pragma pack from the template context. */
9906 saved_maximum_field_alignment
= maximum_field_alignment
;
9907 maximum_field_alignment
= TYPE_PRECISION (pattern
);
9909 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
9911 /* Set the input location to the most specialized template definition.
9912 This is needed if tsubsting causes an error. */
9913 typedecl
= TYPE_MAIN_DECL (pattern
);
9914 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
9915 DECL_SOURCE_LOCATION (typedecl
);
9917 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
9918 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
9919 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
9920 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
9921 if (ANON_AGGR_TYPE_P (pattern
))
9922 SET_ANON_AGGR_TYPE_P (type
);
9923 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
9925 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
9926 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
9927 /* Adjust visibility for template arguments. */
9928 determine_visibility (TYPE_MAIN_DECL (type
));
9930 if (CLASS_TYPE_P (type
))
9931 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
9933 pbinfo
= TYPE_BINFO (pattern
);
9935 /* We should never instantiate a nested class before its enclosing
9936 class; we need to look up the nested class by name before we can
9937 instantiate it, and that lookup should instantiate the enclosing
9939 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
9940 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
9942 base_list
= NULL_TREE
;
9943 if (BINFO_N_BASE_BINFOS (pbinfo
))
9949 /* We must enter the scope containing the type, as that is where
9950 the accessibility of types named in dependent bases are
9952 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
9954 /* Substitute into each of the bases to determine the actual
9956 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
9959 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
9960 tree expanded_bases
= NULL_TREE
;
9963 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
9966 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
9967 args
, tf_error
, NULL_TREE
);
9968 if (expanded_bases
== error_mark_node
)
9971 len
= TREE_VEC_LENGTH (expanded_bases
);
9974 for (idx
= 0; idx
< len
; idx
++)
9977 /* Extract the already-expanded base class. */
9978 base
= TREE_VEC_ELT (expanded_bases
, idx
);
9980 /* Substitute to figure out the base class. */
9981 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
9984 if (base
== error_mark_node
)
9987 base_list
= tree_cons (access
, base
, base_list
);
9988 if (BINFO_VIRTUAL_P (pbase_binfo
))
9989 TREE_TYPE (base_list
) = integer_type_node
;
9993 /* The list is now in reverse order; correct that. */
9994 base_list
= nreverse (base_list
);
9997 pop_scope (pushed_scope
);
9999 /* Now call xref_basetypes to set up all the base-class
10001 xref_basetypes (type
, base_list
);
10003 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10004 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10005 args
, tf_error
, NULL_TREE
);
10006 fixup_attribute_variants (type
);
10008 /* Now that our base classes are set up, enter the scope of the
10009 class, so that name lookups into base classes, etc. will work
10010 correctly. This is precisely analogous to what we do in
10011 begin_class_definition when defining an ordinary non-template
10012 class, except we also need to push the enclosing classes. */
10013 push_nested_class (type
);
10015 /* Now members are processed in the order of declaration. */
10016 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10017 member
; member
= TREE_CHAIN (member
))
10019 tree t
= TREE_VALUE (member
);
10021 if (TREE_PURPOSE (member
))
10025 /* Build new CLASSTYPE_NESTED_UTDS. */
10028 bool class_template_p
;
10030 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10031 && TYPE_LANG_SPECIFIC (t
)
10032 && CLASSTYPE_IS_TEMPLATE (t
));
10033 /* If the member is a class template, then -- even after
10034 substitution -- there may be dependent types in the
10035 template argument list for the class. We increment
10036 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10037 that function will assume that no types are dependent
10038 when outside of a template. */
10039 if (class_template_p
)
10040 ++processing_template_decl
;
10041 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10042 if (class_template_p
)
10043 --processing_template_decl
;
10044 if (newtag
== error_mark_node
)
10047 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10049 tree name
= TYPE_IDENTIFIER (t
);
10051 if (class_template_p
)
10052 /* Unfortunately, lookup_template_class sets
10053 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10054 instantiation (i.e., for the type of a member
10055 template class nested within a template class.)
10056 This behavior is required for
10057 maybe_process_partial_specialization to work
10058 correctly, but is not accurate in this case;
10059 the TAG is not an instantiation of anything.
10060 (The corresponding TEMPLATE_DECL is an
10061 instantiation, but the TYPE is not.) */
10062 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10064 /* Now, we call pushtag to put this NEWTAG into the scope of
10065 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10066 pushtag calling push_template_decl. We don't have to do
10067 this for enums because it will already have been done in
10070 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10071 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10074 else if (DECL_DECLARES_FUNCTION_P (t
))
10076 /* Build new TYPE_METHODS. */
10079 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10080 ++processing_template_decl
;
10081 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10082 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10083 --processing_template_decl
;
10084 set_current_access_from_decl (r
);
10085 finish_member_declaration (r
);
10086 /* Instantiate members marked with attribute used. */
10087 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10089 if (TREE_CODE (r
) == FUNCTION_DECL
10090 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10091 cp_check_omp_declare_reduction (r
);
10093 else if (DECL_CLASS_TEMPLATE_P (t
)
10094 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10095 /* A closure type for a lambda in a default argument for a
10096 member template. Ignore it; it will be instantiated with
10097 the default argument. */;
10100 /* Build new TYPE_FIELDS. */
10101 if (TREE_CODE (t
) == STATIC_ASSERT
)
10105 ++c_inhibit_evaluation_warnings
;
10107 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10108 tf_warning_or_error
, NULL_TREE
,
10109 /*integral_constant_expression_p=*/true);
10110 --c_inhibit_evaluation_warnings
;
10112 finish_static_assert (condition
,
10113 STATIC_ASSERT_MESSAGE (t
),
10114 STATIC_ASSERT_SOURCE_LOCATION (t
),
10115 /*member_p=*/true);
10117 else if (TREE_CODE (t
) != CONST_DECL
)
10120 tree vec
= NULL_TREE
;
10123 /* The file and line for this declaration, to
10124 assist in error message reporting. Since we
10125 called push_tinst_level above, we don't need to
10127 input_location
= DECL_SOURCE_LOCATION (t
);
10129 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10130 ++processing_template_decl
;
10131 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10132 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10133 --processing_template_decl
;
10135 if (TREE_CODE (r
) == TREE_VEC
)
10137 /* A capture pack became multiple fields. */
10139 len
= TREE_VEC_LENGTH (vec
);
10142 for (int i
= 0; i
< len
; ++i
)
10145 r
= TREE_VEC_ELT (vec
, i
);
10150 [t]he initialization (and any associated
10151 side-effects) of a static data member does
10152 not occur unless the static data member is
10153 itself used in a way that requires the
10154 definition of the static data member to
10157 Therefore, we do not substitute into the
10158 initialized for the static data member here. */
10159 finish_static_data_member_decl
10161 /*init=*/NULL_TREE
,
10162 /*init_const_expr_p=*/false,
10163 /*asmspec_tree=*/NULL_TREE
,
10165 /* Instantiate members marked with attribute used. */
10166 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10169 else if (TREE_CODE (r
) == FIELD_DECL
)
10171 /* Determine whether R has a valid type and can be
10172 completed later. If R is invalid, then its type
10173 is replaced by error_mark_node. */
10174 tree rtype
= TREE_TYPE (r
);
10175 if (can_complete_type_without_circularity (rtype
))
10176 complete_type (rtype
);
10178 if (!complete_or_array_type_p (rtype
))
10180 /* If R's type couldn't be completed and
10181 it isn't a flexible array member (whose
10182 type is incomplete by definition) give
10184 cxx_incomplete_type_error (r
, rtype
);
10185 TREE_TYPE (r
) = error_mark_node
;
10189 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10190 such a thing will already have been added to the field
10191 list by tsubst_enum in finish_member_declaration in the
10192 CLASSTYPE_NESTED_UTDS case above. */
10193 if (!(TREE_CODE (r
) == TYPE_DECL
10194 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10195 && DECL_ARTIFICIAL (r
)))
10197 set_current_access_from_decl (r
);
10198 finish_member_declaration (r
);
10206 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10207 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10209 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10211 tree friend_type
= t
;
10212 bool adjust_processing_template_decl
= false;
10214 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10216 /* template <class T> friend class C; */
10217 friend_type
= tsubst_friend_class (friend_type
, args
);
10218 adjust_processing_template_decl
= true;
10220 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10222 /* template <class T> friend class C::D; */
10223 friend_type
= tsubst (friend_type
, args
,
10224 tf_warning_or_error
, NULL_TREE
);
10225 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10226 friend_type
= TREE_TYPE (friend_type
);
10227 adjust_processing_template_decl
= true;
10229 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10230 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10232 /* This could be either
10236 when dependent_type_p is false or
10238 template <class U> friend class T::C;
10241 /* Bump processing_template_decl in case this is something like
10242 template <class T> friend struct A<T>::B. */
10243 ++processing_template_decl
;
10244 friend_type
= tsubst (friend_type
, args
,
10245 tf_warning_or_error
, NULL_TREE
);
10246 if (dependent_type_p (friend_type
))
10247 adjust_processing_template_decl
= true;
10248 --processing_template_decl
;
10250 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
10251 && hidden_name_p (TYPE_NAME (friend_type
)))
10255 where C hasn't been declared yet. Let's lookup name
10256 from namespace scope directly, bypassing any name that
10257 come from dependent base class. */
10258 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10260 /* The call to xref_tag_from_type does injection for friend
10262 push_nested_namespace (ns
);
10264 xref_tag_from_type (friend_type
, NULL_TREE
,
10265 /*tag_scope=*/ts_current
);
10266 pop_nested_namespace (ns
);
10268 else if (uses_template_parms (friend_type
))
10269 /* friend class C<T>; */
10270 friend_type
= tsubst (friend_type
, args
,
10271 tf_warning_or_error
, NULL_TREE
);
10276 where C is already declared or
10278 friend class C<int>;
10280 We don't have to do anything in these cases. */
10282 if (adjust_processing_template_decl
)
10283 /* Trick make_friend_class into realizing that the friend
10284 we're adding is a template, not an ordinary class. It's
10285 important that we use make_friend_class since it will
10286 perform some error-checking and output cross-reference
10288 ++processing_template_decl
;
10290 if (friend_type
!= error_mark_node
)
10291 make_friend_class (type
, friend_type
, /*complain=*/false);
10293 if (adjust_processing_template_decl
)
10294 --processing_template_decl
;
10298 /* Build new DECL_FRIENDLIST. */
10301 /* The file and line for this declaration, to
10302 assist in error message reporting. Since we
10303 called push_tinst_level above, we don't need to
10305 input_location
= DECL_SOURCE_LOCATION (t
);
10307 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10309 ++processing_template_decl
;
10310 push_deferring_access_checks (dk_no_check
);
10313 r
= tsubst_friend_function (t
, args
);
10314 add_friend (type
, r
, /*complain=*/false);
10315 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10317 pop_deferring_access_checks ();
10318 --processing_template_decl
;
10326 /* Restore these before substituting into the lambda capture
10328 cp_unevaluated_operand
= saved_unevaluated_operand
;
10329 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10332 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10334 tree decl
= lambda_function (type
);
10337 if (!DECL_TEMPLATE_INFO (decl
)
10338 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10340 /* Set function_depth to avoid garbage collection. */
10342 instantiate_decl (decl
, false, false);
10346 /* We need to instantiate the capture list from the template
10347 after we've instantiated the closure members, but before we
10348 consider adding the conversion op. Also keep any captures
10349 that may have been added during instantiation of the op(). */
10350 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10352 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10353 args
, tf_warning_or_error
, NULL_TREE
,
10356 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10357 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10359 maybe_add_lambda_conv_op (type
);
10362 gcc_assert (errorcount
);
10365 /* Set the file and line number information to whatever is given for
10366 the class itself. This puts error messages involving generated
10367 implicit functions at a predictable point, and the same point
10368 that would be used for non-template classes. */
10369 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10371 unreverse_member_declarations (type
);
10372 finish_struct_1 (type
);
10373 TYPE_BEING_DEFINED (type
) = 0;
10375 /* We don't instantiate default arguments for member functions. 14.7.1:
10377 The implicit instantiation of a class template specialization causes
10378 the implicit instantiation of the declarations, but not of the
10379 definitions or default arguments, of the class member functions,
10380 member classes, static data members and member templates.... */
10382 /* Some typedefs referenced from within the template code need to be access
10383 checked at template instantiation time, i.e now. These types were
10384 added to the template at parsing time. Let's get those and perform
10385 the access checks then. */
10386 perform_typedefs_access_check (pattern
, args
);
10387 perform_deferred_access_checks (tf_warning_or_error
);
10388 pop_nested_class ();
10389 maximum_field_alignment
= saved_maximum_field_alignment
;
10391 pop_from_top_level ();
10392 pop_deferring_access_checks ();
10393 pop_tinst_level ();
10395 /* The vtable for a template class can be emitted in any translation
10396 unit in which the class is instantiated. When there is no key
10397 method, however, finish_struct_1 will already have added TYPE to
10398 the keyed_classes list. */
10399 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10400 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
10405 /* Wrapper for instantiate_class_template_1. */
10408 instantiate_class_template (tree type
)
10411 timevar_push (TV_TEMPLATE_INST
);
10412 ret
= instantiate_class_template_1 (type
);
10413 timevar_pop (TV_TEMPLATE_INST
);
10418 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10424 else if (TYPE_P (t
))
10425 r
= tsubst (t
, args
, complain
, in_decl
);
10428 if (!(complain
& tf_warning
))
10429 ++c_inhibit_evaluation_warnings
;
10430 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10431 /*integral_constant_expression_p=*/true);
10432 if (!(complain
& tf_warning
))
10433 --c_inhibit_evaluation_warnings
;
10438 /* Given a function parameter pack TMPL_PARM and some function parameters
10439 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10440 and set *SPEC_P to point at the next point in the list. */
10443 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10445 /* Collect all of the extra "packed" parameters into an
10449 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10450 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10451 tree spec_parm
= *spec_p
;
10454 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10456 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10459 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10460 parmvec
= make_tree_vec (len
);
10461 parmtypevec
= make_tree_vec (len
);
10462 spec_parm
= *spec_p
;
10463 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10465 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10466 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
10469 /* Build the argument packs. */
10470 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10471 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
10472 TREE_TYPE (argpack
) = argtypepack
;
10473 *spec_p
= spec_parm
;
10478 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10479 NONTYPE_ARGUMENT_PACK. */
10482 make_fnparm_pack (tree spec_parm
)
10484 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10487 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10488 pack expansion with no extra args, 2 if it has extra args, or 0
10489 if it is not a pack expansion. */
10492 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10494 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10495 if (i
>= TREE_VEC_LENGTH (vec
))
10497 tree elt
= TREE_VEC_ELT (vec
, i
);
10499 /* A decl pack is itself an expansion. */
10500 elt
= TREE_TYPE (elt
);
10501 if (!PACK_EXPANSION_P (elt
))
10503 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
10509 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10512 make_argument_pack_select (tree arg_pack
, unsigned index
)
10514 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
10516 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
10517 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10522 /* This is a subroutine of tsubst_pack_expansion.
10524 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10525 mechanism to store the (non complete list of) arguments of the
10526 substitution and return a non substituted pack expansion, in order
10527 to wait for when we have enough arguments to really perform the
10531 use_pack_expansion_extra_args_p (tree parm_packs
,
10533 bool has_empty_arg
)
10535 /* If one pack has an expansion and another pack has a normal
10536 argument or if one pack has an empty argument and an another
10537 one hasn't then tsubst_pack_expansion cannot perform the
10538 substitution and need to fall back on the
10539 PACK_EXPANSION_EXTRA mechanism. */
10540 if (parm_packs
== NULL_TREE
)
10542 else if (has_empty_arg
)
10545 bool has_expansion_arg
= false;
10546 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
10548 bool has_non_expansion_arg
= false;
10549 for (tree parm_pack
= parm_packs
;
10551 parm_pack
= TREE_CHAIN (parm_pack
))
10553 tree arg
= TREE_VALUE (parm_pack
);
10555 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
10557 /* We can't substitute a pack expansion with extra args into
10561 has_expansion_arg
= true;
10563 has_non_expansion_arg
= true;
10566 if (has_expansion_arg
&& has_non_expansion_arg
)
10572 /* [temp.variadic]/6 says that:
10574 The instantiation of a pack expansion [...]
10575 produces a list E1,E2, ..., En, where N is the number of elements
10576 in the pack expansion parameters.
10578 This subroutine of tsubst_pack_expansion produces one of these Ei.
10580 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10581 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10582 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10583 INDEX is the index 'i' of the element Ei to produce. ARGS,
10584 COMPLAIN, and IN_DECL are the same parameters as for the
10585 tsubst_pack_expansion function.
10587 The function returns the resulting Ei upon successful completion,
10588 or error_mark_node.
10590 Note that this function possibly modifies the ARGS parameter, so
10591 it's the responsibility of the caller to restore it. */
10594 gen_elem_of_pack_expansion_instantiation (tree pattern
,
10597 tree args
/* This parm gets
10599 tsubst_flags_t complain
,
10603 bool ith_elem_is_expansion
= false;
10605 /* For each parameter pack, change the substitution of the parameter
10606 pack to the ith argument in its argument pack, then expand the
10608 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
10610 tree parm
= TREE_PURPOSE (pack
);
10611 tree arg_pack
= TREE_VALUE (pack
);
10612 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
10614 ith_elem_is_expansion
|=
10615 argument_pack_element_is_expansion_p (arg_pack
, index
);
10617 /* Select the Ith argument from the pack. */
10618 if (TREE_CODE (parm
) == PARM_DECL
10619 || TREE_CODE (parm
) == FIELD_DECL
)
10623 aps
= make_argument_pack_select (arg_pack
, index
);
10624 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
10625 return error_mark_node
;
10626 register_local_specialization (aps
, parm
);
10629 aps
= retrieve_local_specialization (parm
);
10634 template_parm_level_and_index (parm
, &level
, &idx
);
10638 aps
= make_argument_pack_select (arg_pack
, index
);
10639 /* Update the corresponding argument. */
10640 TMPL_ARG (args
, level
, idx
) = aps
;
10643 /* Re-use the ARGUMENT_PACK_SELECT. */
10644 aps
= TMPL_ARG (args
, level
, idx
);
10646 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10649 /* Substitute into the PATTERN with the (possibly altered)
10651 if (pattern
== in_decl
)
10652 /* Expanding a fixed parameter pack from
10653 coerce_template_parameter_pack. */
10654 t
= tsubst_decl (pattern
, args
, complain
);
10655 else if (pattern
== error_mark_node
)
10656 t
= error_mark_node
;
10657 else if (constraint_p (pattern
))
10659 if (processing_template_decl
)
10660 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
10662 t
= (constraints_satisfied_p (pattern
, args
)
10663 ? boolean_true_node
: boolean_false_node
);
10665 else if (!TYPE_P (pattern
))
10666 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10667 /*integral_constant_expression_p=*/false);
10669 t
= tsubst (pattern
, args
, complain
, in_decl
);
10671 /* If the Ith argument pack element is a pack expansion, then
10672 the Ith element resulting from the substituting is going to
10673 be a pack expansion as well. */
10674 if (ith_elem_is_expansion
)
10675 t
= make_pack_expansion (t
);
10680 /* When the unexpanded parameter pack in a fold expression expands to an empty
10681 sequence, the value of the expression is as follows; the program is
10682 ill-formed if the operator is not listed in this table.
10689 expand_empty_fold (tree t
, tsubst_flags_t complain
)
10691 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
10692 if (!FOLD_EXPR_MODIFY_P (t
))
10695 case TRUTH_ANDIF_EXPR
:
10696 return boolean_true_node
;
10697 case TRUTH_ORIF_EXPR
:
10698 return boolean_false_node
;
10699 case COMPOUND_EXPR
:
10705 if (complain
& tf_error
)
10706 error_at (location_of (t
),
10707 "fold of empty expansion over %O", code
);
10708 return error_mark_node
;
10711 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10712 form an expression that combines the two terms using the
10716 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
10718 tree op
= FOLD_EXPR_OP (t
);
10719 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
10721 // Handle compound assignment operators.
10722 if (FOLD_EXPR_MODIFY_P (t
))
10723 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
10727 case COMPOUND_EXPR
:
10728 return build_x_compound_expr (input_location
, left
, right
, complain
);
10730 return build_m_component_ref (left
, right
, complain
);
10732 return build_x_binary_op (input_location
, code
,
10733 left
, TREE_CODE (left
),
10734 right
, TREE_CODE (right
),
10740 /* Substitute ARGS into the pack of a fold expression T. */
10743 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10745 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
10748 /* Substitute ARGS into the pack of a fold expression T. */
10751 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10753 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
10756 /* Expand a PACK of arguments into a grouped as left fold.
10757 Given a pack containing elements A0, A1, ..., An and an
10758 operator @, this builds the expression:
10760 ((A0 @ A1) @ A2) ... @ An
10762 Note that PACK must not be empty.
10764 The operator is defined by the original fold expression T. */
10767 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
10769 tree left
= TREE_VEC_ELT (pack
, 0);
10770 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
10772 tree right
= TREE_VEC_ELT (pack
, i
);
10773 left
= fold_expression (t
, left
, right
, complain
);
10778 /* Substitute into a unary left fold expression. */
10781 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
10784 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10785 if (pack
== error_mark_node
)
10786 return error_mark_node
;
10787 if (PACK_EXPANSION_P (pack
))
10789 tree r
= copy_node (t
);
10790 FOLD_EXPR_PACK (r
) = pack
;
10793 if (TREE_VEC_LENGTH (pack
) == 0)
10794 return expand_empty_fold (t
, complain
);
10796 return expand_left_fold (t
, pack
, complain
);
10799 /* Substitute into a binary left fold expression.
10801 Do ths by building a single (non-empty) vector of argumnts and
10802 building the expression from those elements. */
10805 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
10808 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10809 if (pack
== error_mark_node
)
10810 return error_mark_node
;
10811 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
10812 if (init
== error_mark_node
)
10813 return error_mark_node
;
10815 if (PACK_EXPANSION_P (pack
))
10817 tree r
= copy_node (t
);
10818 FOLD_EXPR_PACK (r
) = pack
;
10819 FOLD_EXPR_INIT (r
) = init
;
10823 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
10824 TREE_VEC_ELT (vec
, 0) = init
;
10825 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
10826 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
10828 return expand_left_fold (t
, vec
, complain
);
10831 /* Expand a PACK of arguments into a grouped as right fold.
10832 Given a pack containing elementns A0, A1, ..., and an
10833 operator @, this builds the expression:
10835 A0@ ... (An-2 @ (An-1 @ An))
10837 Note that PACK must not be empty.
10839 The operator is defined by the original fold expression T. */
10842 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
10844 // Build the expression.
10845 int n
= TREE_VEC_LENGTH (pack
);
10846 tree right
= TREE_VEC_ELT (pack
, n
- 1);
10847 for (--n
; n
!= 0; --n
)
10849 tree left
= TREE_VEC_ELT (pack
, n
- 1);
10850 right
= fold_expression (t
, left
, right
, complain
);
10855 /* Substitute into a unary right fold expression. */
10858 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
10861 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10862 if (pack
== error_mark_node
)
10863 return error_mark_node
;
10864 if (PACK_EXPANSION_P (pack
))
10866 tree r
= copy_node (t
);
10867 FOLD_EXPR_PACK (r
) = pack
;
10870 if (TREE_VEC_LENGTH (pack
) == 0)
10871 return expand_empty_fold (t
, complain
);
10873 return expand_right_fold (t
, pack
, complain
);
10876 /* Substitute into a binary right fold expression.
10878 Do ths by building a single (non-empty) vector of arguments and
10879 building the expression from those elements. */
10882 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
10885 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10886 if (pack
== error_mark_node
)
10887 return error_mark_node
;
10888 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
10889 if (init
== error_mark_node
)
10890 return error_mark_node
;
10892 if (PACK_EXPANSION_P (pack
))
10894 tree r
= copy_node (t
);
10895 FOLD_EXPR_PACK (r
) = pack
;
10896 FOLD_EXPR_INIT (r
) = init
;
10900 int n
= TREE_VEC_LENGTH (pack
);
10901 tree vec
= make_tree_vec (n
+ 1);
10902 for (int i
= 0; i
< n
; ++i
)
10903 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
10904 TREE_VEC_ELT (vec
, n
) = init
;
10906 return expand_right_fold (t
, vec
, complain
);
10910 /* Substitute ARGS into T, which is an pack expansion
10911 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10912 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10913 (if only a partial substitution could be performed) or
10914 ERROR_MARK_NODE if there was an error. */
10916 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
10920 tree pack
, packs
= NULL_TREE
;
10921 bool unsubstituted_packs
= false;
10924 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
10925 bool need_local_specializations
= false;
10928 gcc_assert (PACK_EXPANSION_P (t
));
10929 pattern
= PACK_EXPANSION_PATTERN (t
);
10931 /* Add in any args remembered from an earlier partial instantiation. */
10932 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
10934 levels
= TMPL_ARGS_DEPTH (args
);
10936 /* Determine the argument packs that will instantiate the parameter
10937 packs used in the expansion expression. While we're at it,
10938 compute the number of arguments to be expanded and make sure it
10940 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
10941 pack
= TREE_CHAIN (pack
))
10943 tree parm_pack
= TREE_VALUE (pack
);
10944 tree arg_pack
= NULL_TREE
;
10945 tree orig_arg
= NULL_TREE
;
10948 if (TREE_CODE (parm_pack
) == BASES
)
10950 if (BASES_DIRECT (parm_pack
))
10951 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10952 args
, complain
, in_decl
, false));
10954 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10955 args
, complain
, in_decl
, false));
10957 if (TREE_CODE (parm_pack
) == PARM_DECL
)
10959 /* We know we have correct local_specializations if this
10960 expansion is at function scope, or if we're dealing with a
10961 local parameter in a requires expression; for the latter,
10962 tsubst_requires_expr set it up appropriately. */
10963 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
10964 arg_pack
= retrieve_local_specialization (parm_pack
);
10966 /* We can't rely on local_specializations for a parameter
10967 name used later in a function declaration (such as in a
10968 late-specified return type). Even if it exists, it might
10969 have the wrong value for a recursive call. */
10970 need_local_specializations
= true;
10974 /* This parameter pack was used in an unevaluated context. Just
10975 make a dummy decl, since it's only used for its type. */
10976 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
10977 if (arg_pack
&& DECL_PACK_P (arg_pack
))
10978 /* Partial instantiation of the parm_pack, we can't build
10979 up an argument pack yet. */
10980 arg_pack
= NULL_TREE
;
10982 arg_pack
= make_fnparm_pack (arg_pack
);
10985 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
10986 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
10990 template_parm_level_and_index (parm_pack
, &level
, &idx
);
10992 if (level
<= levels
)
10993 arg_pack
= TMPL_ARG (args
, level
, idx
);
10996 orig_arg
= arg_pack
;
10997 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
10998 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11000 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11001 /* This can only happen if we forget to expand an argument
11002 pack somewhere else. Just return an error, silently. */
11004 result
= make_tree_vec (1);
11005 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11012 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11014 /* Don't bother trying to do a partial substitution with
11015 incomplete packs; we'll try again after deduction. */
11016 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11021 else if (len
!= my_len
)
11023 if (!(complain
& tf_error
))
11024 /* Fail quietly. */;
11025 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11026 error ("mismatched argument pack lengths while expanding "
11030 error ("mismatched argument pack lengths while expanding "
11033 return error_mark_node
;
11036 /* Keep track of the parameter packs and their corresponding
11038 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11039 TREE_TYPE (packs
) = orig_arg
;
11043 /* We can't substitute for this parameter pack. We use a flag as
11044 well as the missing_level counter because function parameter
11045 packs don't have a level. */
11046 gcc_assert (processing_template_decl
);
11047 unsubstituted_packs
= true;
11051 /* If the expansion is just T..., return the matching argument pack, unless
11052 we need to call convert_from_reference on all the elements. This is an
11053 important optimization; see c++/68422. */
11054 if (!unsubstituted_packs
11055 && TREE_PURPOSE (packs
) == pattern
)
11057 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11058 /* Types need no adjustment, nor does sizeof..., and if we still have
11059 some pack expansion args we won't do anything yet. */
11060 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11061 || PACK_EXPANSION_SIZEOF_P (t
)
11062 || pack_expansion_args_count (args
))
11064 /* Also optimize expression pack expansions if we can tell that the
11065 elements won't have reference type. */
11066 tree type
= TREE_TYPE (pattern
);
11067 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11068 && !PACK_EXPANSION_P (type
)
11069 && !WILDCARD_TYPE_P (type
))
11071 /* Otherwise use the normal path so we get convert_from_reference. */
11074 /* We cannot expand this expansion expression, because we don't have
11075 all of the argument packs we need. */
11076 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11078 /* We got some full packs, but we can't substitute them in until we
11079 have values for all the packs. So remember these until then. */
11081 t
= make_pack_expansion (pattern
);
11082 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11085 else if (unsubstituted_packs
)
11087 /* There were no real arguments, we're just replacing a parameter
11088 pack with another version of itself. Substitute into the
11089 pattern and return a PACK_EXPANSION_*. The caller will need to
11091 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11092 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11093 /*integral_constant_expression_p=*/false);
11095 t
= tsubst (pattern
, args
, complain
, in_decl
);
11096 t
= make_pack_expansion (t
);
11100 gcc_assert (len
>= 0);
11102 if (need_local_specializations
)
11104 /* We're in a late-specified return type, so create our own local
11105 specializations map; the current map is either NULL or (in the
11106 case of recursive unification) might have bindings that we don't
11107 want to use or alter. */
11108 saved_local_specializations
= local_specializations
;
11109 local_specializations
= new hash_map
<tree
, tree
>;
11112 /* For each argument in each argument pack, substitute into the
11114 result
= make_tree_vec (len
);
11115 tree elem_args
= copy_template_args (args
);
11116 for (i
= 0; i
< len
; ++i
)
11118 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11120 elem_args
, complain
,
11122 TREE_VEC_ELT (result
, i
) = t
;
11123 if (t
== error_mark_node
)
11125 result
= error_mark_node
;
11130 /* Update ARGS to restore the substitution from parameter packs to
11131 their argument packs. */
11132 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11134 tree parm
= TREE_PURPOSE (pack
);
11136 if (TREE_CODE (parm
) == PARM_DECL
11137 || TREE_CODE (parm
) == FIELD_DECL
)
11138 register_local_specialization (TREE_TYPE (pack
), parm
);
11143 if (TREE_VALUE (pack
) == NULL_TREE
)
11146 template_parm_level_and_index (parm
, &level
, &idx
);
11148 /* Update the corresponding argument. */
11149 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11150 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11153 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11157 if (need_local_specializations
)
11159 delete local_specializations
;
11160 local_specializations
= saved_local_specializations
;
11166 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11167 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11168 parameter packs; all parms generated from a function parameter pack will
11169 have the same DECL_PARM_INDEX. */
11172 get_pattern_parm (tree parm
, tree tmpl
)
11174 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11177 if (DECL_ARTIFICIAL (parm
))
11179 for (patparm
= DECL_ARGUMENTS (pattern
);
11180 patparm
; patparm
= DECL_CHAIN (patparm
))
11181 if (DECL_ARTIFICIAL (patparm
)
11182 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11187 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11188 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11189 gcc_assert (DECL_PARM_INDEX (patparm
)
11190 == DECL_PARM_INDEX (parm
));
11196 /* Make an argument pack out of the TREE_VEC VEC. */
11199 make_argument_pack (tree vec
)
11202 tree elt
= TREE_VEC_ELT (vec
, 0);
11204 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11207 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11208 TREE_TYPE (pack
) = TREE_TYPE (elt
);
11209 TREE_CONSTANT (pack
) = 1;
11211 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11215 /* Return an exact copy of template args T that can be modified
11219 copy_template_args (tree t
)
11221 if (t
== error_mark_node
)
11224 int len
= TREE_VEC_LENGTH (t
);
11225 tree new_vec
= make_tree_vec (len
);
11227 for (int i
= 0; i
< len
; ++i
)
11229 tree elt
= TREE_VEC_ELT (t
, i
);
11230 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11231 elt
= copy_template_args (elt
);
11232 TREE_VEC_ELT (new_vec
, i
) = elt
;
11235 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11236 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11241 /* Substitute ARGS into the vector or list of template arguments T. */
11244 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11247 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11250 if (t
== error_mark_node
)
11251 return error_mark_node
;
11253 len
= TREE_VEC_LENGTH (t
);
11254 elts
= XALLOCAVEC (tree
, len
);
11256 for (i
= 0; i
< len
; i
++)
11258 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11261 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11262 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11263 else if (PACK_EXPANSION_P (orig_arg
))
11265 /* Substitute into an expansion expression. */
11266 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11268 if (TREE_CODE (new_arg
) == TREE_VEC
)
11269 /* Add to the expanded length adjustment the number of
11270 expanded arguments. We subtract one from this
11271 measurement, because the argument pack expression
11272 itself is already counted as 1 in
11273 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11274 the argument pack is empty. */
11275 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11277 else if (ARGUMENT_PACK_P (orig_arg
))
11279 /* Substitute into each of the arguments. */
11280 new_arg
= TYPE_P (orig_arg
)
11281 ? cxx_make_type (TREE_CODE (orig_arg
))
11282 : make_node (TREE_CODE (orig_arg
));
11284 SET_ARGUMENT_PACK_ARGS (
11286 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11287 args
, complain
, in_decl
));
11289 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
11290 new_arg
= error_mark_node
;
11292 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
11293 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
11294 complain
, in_decl
);
11295 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11297 if (TREE_TYPE (new_arg
) == error_mark_node
)
11298 new_arg
= error_mark_node
;
11302 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11304 if (new_arg
== error_mark_node
)
11305 return error_mark_node
;
11308 if (new_arg
!= orig_arg
)
11315 /* Make space for the expanded arguments coming from template
11317 t
= make_tree_vec (len
+ expanded_len_adjust
);
11318 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11319 arguments for a member template.
11320 In that case each TREE_VEC in ORIG_T represents a level of template
11321 arguments, and ORIG_T won't carry any non defaulted argument count.
11322 It will rather be the nested TREE_VECs that will carry one.
11323 In other words, ORIG_T carries a non defaulted argument count only
11324 if it doesn't contain any nested TREE_VEC. */
11325 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11327 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11328 count
+= expanded_len_adjust
;
11329 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11331 for (i
= 0, out
= 0; i
< len
; i
++)
11333 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11334 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11335 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11339 /* Now expand the template argument pack "in place". */
11340 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11341 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11345 TREE_VEC_ELT (t
, out
) = elts
[i
];
11353 /* Return the result of substituting ARGS into the template parameters
11354 given by PARMS. If there are m levels of ARGS and m + n levels of
11355 PARMS, then the result will contain n levels of PARMS. For
11356 example, if PARMS is `template <class T> template <class U>
11357 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11358 result will be `template <int*, double, class V>'. */
11361 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11363 tree r
= NULL_TREE
;
11366 /* When substituting into a template, we must set
11367 PROCESSING_TEMPLATE_DECL as the template parameters may be
11368 dependent if they are based on one-another, and the dependency
11369 predicates are short-circuit outside of templates. */
11370 ++processing_template_decl
;
11372 for (new_parms
= &r
;
11373 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11374 new_parms
= &(TREE_CHAIN (*new_parms
)),
11375 parms
= TREE_CHAIN (parms
))
11378 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
11381 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11385 if (parms
== error_mark_node
)
11388 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
11390 if (tuple
== error_mark_node
)
11393 TREE_VEC_ELT (new_vec
, i
) =
11394 tsubst_template_parm (tuple
, args
, complain
);
11398 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11399 - TMPL_ARGS_DEPTH (args
)),
11400 new_vec
, NULL_TREE
);
11403 --processing_template_decl
;
11408 /* Return the result of substituting ARGS into one template parameter
11409 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11410 parameter and which TREE_PURPOSE is the default argument of the
11411 template parameter. */
11414 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11416 tree default_value
, parm_decl
;
11418 if (args
== NULL_TREE
11420 || t
== error_mark_node
)
11423 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11425 default_value
= TREE_PURPOSE (t
);
11426 parm_decl
= TREE_VALUE (t
);
11428 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11429 if (TREE_CODE (parm_decl
) == PARM_DECL
11430 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11431 parm_decl
= error_mark_node
;
11432 default_value
= tsubst_template_arg (default_value
, args
,
11433 complain
, NULL_TREE
);
11435 return build_tree_list (default_value
, parm_decl
);
11438 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11439 type T. If T is not an aggregate or enumeration type, it is
11440 handled as if by tsubst. IN_DECL is as for tsubst. If
11441 ENTERING_SCOPE is nonzero, T is the context for a template which
11442 we are presently tsubst'ing. Return the substituted value. */
11445 tsubst_aggr_type (tree t
,
11447 tsubst_flags_t complain
,
11449 int entering_scope
)
11451 if (t
== NULL_TREE
)
11454 switch (TREE_CODE (t
))
11457 if (TYPE_PTRMEMFUNC_P (t
))
11458 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11460 /* Else fall through. */
11461 case ENUMERAL_TYPE
:
11463 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11468 int saved_unevaluated_operand
;
11469 int saved_inhibit_evaluation_warnings
;
11471 /* In "sizeof(X<I>)" we need to evaluate "I". */
11472 saved_unevaluated_operand
= cp_unevaluated_operand
;
11473 cp_unevaluated_operand
= 0;
11474 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11475 c_inhibit_evaluation_warnings
= 0;
11477 /* First, determine the context for the type we are looking
11479 context
= TYPE_CONTEXT (t
);
11480 if (context
&& TYPE_P (context
))
11482 context
= tsubst_aggr_type (context
, args
, complain
,
11483 in_decl
, /*entering_scope=*/1);
11484 /* If context is a nested class inside a class template,
11485 it may still need to be instantiated (c++/33959). */
11486 context
= complete_type (context
);
11489 /* Then, figure out what arguments are appropriate for the
11490 type we are trying to find. For example, given:
11492 template <class T> struct S;
11493 template <class T, class U> void f(T, U) { S<U> su; }
11495 and supposing that we are instantiating f<int, double>,
11496 then our ARGS will be {int, double}, but, when looking up
11497 S we only want {double}. */
11498 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
11499 complain
, in_decl
);
11500 if (argvec
== error_mark_node
)
11501 r
= error_mark_node
;
11504 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
11505 entering_scope
, complain
);
11506 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11509 cp_unevaluated_operand
= saved_unevaluated_operand
;
11510 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11515 /* This is not a template type, so there's nothing to do. */
11519 return tsubst (t
, args
, complain
, in_decl
);
11523 /* Substitute into the default argument ARG (a default argument for
11524 FN), which has the indicated TYPE. */
11527 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
11529 tree saved_class_ptr
= NULL_TREE
;
11530 tree saved_class_ref
= NULL_TREE
;
11531 int errs
= errorcount
+ sorrycount
;
11533 /* This can happen in invalid code. */
11534 if (TREE_CODE (arg
) == DEFAULT_ARG
)
11537 /* This default argument came from a template. Instantiate the
11538 default argument here, not in tsubst. In the case of
11547 we must be careful to do name lookup in the scope of S<T>,
11548 rather than in the current class. */
11549 push_access_scope (fn
);
11550 /* The "this" pointer is not valid in a default argument. */
11553 saved_class_ptr
= current_class_ptr
;
11554 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
11555 saved_class_ref
= current_class_ref
;
11556 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
11559 push_deferring_access_checks(dk_no_deferred
);
11560 /* The default argument expression may cause implicitly defined
11561 member functions to be synthesized, which will result in garbage
11562 collection. We must treat this situation as if we were within
11563 the body of function so as to avoid collecting live data on the
11566 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
11567 complain
, NULL_TREE
,
11568 /*integral_constant_expression_p=*/false);
11570 pop_deferring_access_checks();
11572 /* Restore the "this" pointer. */
11575 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
11576 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
11579 if (errorcount
+sorrycount
> errs
11580 && (complain
& tf_warning_or_error
))
11581 inform (input_location
,
11582 " when instantiating default argument for call to %D", fn
);
11584 /* Make sure the default argument is reasonable. */
11585 arg
= check_default_argument (type
, arg
, complain
);
11587 pop_access_scope (fn
);
11592 /* Substitute into all the default arguments for FN. */
11595 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
11600 tmpl_args
= DECL_TI_ARGS (fn
);
11602 /* If this function is not yet instantiated, we certainly don't need
11603 its default arguments. */
11604 if (uses_template_parms (tmpl_args
))
11606 /* Don't do this again for clones. */
11607 if (DECL_CLONED_FUNCTION_P (fn
))
11610 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
11612 arg
= TREE_CHAIN (arg
))
11613 if (TREE_PURPOSE (arg
))
11614 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
11616 TREE_PURPOSE (arg
),
11620 /* Substitute the ARGS into the T, which is a _DECL. Return the
11621 result of the substitution. Issue error and warning messages under
11622 control of COMPLAIN. */
11625 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
11627 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11628 location_t saved_loc
;
11629 tree r
= NULL_TREE
;
11631 hashval_t hash
= 0;
11633 /* Set the filename and linenumber to improve error-reporting. */
11634 saved_loc
= input_location
;
11635 input_location
= DECL_SOURCE_LOCATION (t
);
11637 switch (TREE_CODE (t
))
11639 case TEMPLATE_DECL
:
11641 /* We can get here when processing a member function template,
11642 member class template, or template template parameter. */
11643 tree decl
= DECL_TEMPLATE_RESULT (t
);
11648 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11650 /* Template template parameter is treated here. */
11651 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11652 if (new_type
== error_mark_node
)
11653 r
= error_mark_node
;
11654 /* If we get a real template back, return it. This can happen in
11655 the context of most_specialized_partial_spec. */
11656 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
11659 /* The new TEMPLATE_DECL was built in
11660 reduce_template_parm_level. */
11661 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
11665 /* We might already have an instance of this template.
11666 The ARGS are for the surrounding class type, so the
11667 full args contain the tsubst'd args for the context,
11668 plus the innermost args from the template decl. */
11669 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
11670 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
11671 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
11672 /* Because this is a template, the arguments will still be
11673 dependent, even after substitution. If
11674 PROCESSING_TEMPLATE_DECL is not set, the dependency
11675 predicates will short-circuit. */
11676 ++processing_template_decl
;
11677 full_args
= tsubst_template_args (tmpl_args
, args
,
11678 complain
, in_decl
);
11679 --processing_template_decl
;
11680 if (full_args
== error_mark_node
)
11681 RETURN (error_mark_node
);
11683 /* If this is a default template template argument,
11684 tsubst might not have changed anything. */
11685 if (full_args
== tmpl_args
)
11688 hash
= hash_tmpl_and_args (t
, full_args
);
11689 spec
= retrieve_specialization (t
, full_args
, hash
);
11690 if (spec
!= NULL_TREE
)
11696 /* Make a new template decl. It will be similar to the
11697 original, but will record the current template arguments.
11698 We also create a new function declaration, which is just
11699 like the old one, but points to this new template, rather
11700 than the old one. */
11702 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
11703 DECL_CHAIN (r
) = NULL_TREE
;
11705 // Build new template info linking to the original template decl.
11706 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11708 if (TREE_CODE (decl
) == TYPE_DECL
11709 && !TYPE_DECL_ALIAS_P (decl
))
11712 ++processing_template_decl
;
11713 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11714 --processing_template_decl
;
11715 if (new_type
== error_mark_node
)
11716 RETURN (error_mark_node
);
11718 TREE_TYPE (r
) = new_type
;
11719 /* For a partial specialization, we need to keep pointing to
11720 the primary template. */
11721 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
11722 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
11723 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
11724 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
11725 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
11730 ++processing_template_decl
;
11731 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
11732 --processing_template_decl
;
11733 if (new_decl
== error_mark_node
)
11734 RETURN (error_mark_node
);
11736 DECL_TEMPLATE_RESULT (r
) = new_decl
;
11737 DECL_TI_TEMPLATE (new_decl
) = r
;
11738 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
11739 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
11740 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
11743 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11744 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
11745 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
11747 /* The template parameters for this new template are all the
11748 template parameters for the old template, except the
11749 outermost level of parameters. */
11750 DECL_TEMPLATE_PARMS (r
)
11751 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
11754 if (PRIMARY_TEMPLATE_P (t
))
11755 DECL_PRIMARY_TEMPLATE (r
) = r
;
11757 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
))
11758 /* Record this non-type partial instantiation. */
11759 register_specialization (r
, t
,
11760 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
11765 case FUNCTION_DECL
:
11768 tree argvec
= NULL_TREE
;
11776 /* Nobody should be tsubst'ing into non-template functions. */
11777 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
11779 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
11783 /* If T is not dependent, just return it. */
11784 if (!uses_template_parms (DECL_TI_ARGS (t
)))
11787 /* Calculate the most general template of which R is a
11788 specialization, and the complete set of arguments used to
11790 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
11791 argvec
= tsubst_template_args (DECL_TI_ARGS
11792 (DECL_TEMPLATE_RESULT
11793 (DECL_TI_TEMPLATE (t
))),
11794 args
, complain
, in_decl
);
11795 if (argvec
== error_mark_node
)
11796 RETURN (error_mark_node
);
11798 /* Check to see if we already have this specialization. */
11799 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11800 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11808 /* We can see more levels of arguments than parameters if
11809 there was a specialization of a member template, like
11812 template <class T> struct S { template <class U> void f(); }
11813 template <> template <class U> void S<int>::f(U);
11815 Here, we'll be substituting into the specialization,
11816 because that's where we can find the code we actually
11817 want to generate, but we'll have enough arguments for
11818 the most general template.
11820 We also deal with the peculiar case:
11822 template <class T> struct S {
11823 template <class U> friend void f();
11825 template <class U> void f() {}
11827 template void f<double>();
11829 Here, the ARGS for the instantiation of will be {int,
11830 double}. But, we only need as many ARGS as there are
11831 levels of template parameters in CODE_PATTERN. We are
11832 careful not to get fooled into reducing the ARGS in
11835 template <class T> struct S { template <class U> void f(U); }
11836 template <class T> template <> void S<T>::f(int) {}
11838 which we can spot because the pattern will be a
11839 specialization in this case. */
11840 args_depth
= TMPL_ARGS_DEPTH (args
);
11842 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
11843 if (args_depth
> parms_depth
11844 && !DECL_TEMPLATE_SPECIALIZATION (t
))
11845 args
= get_innermost_template_args (args
, parms_depth
);
11849 /* This special case arises when we have something like this:
11851 template <class T> struct S {
11852 friend void f<int>(int, double);
11855 Here, the DECL_TI_TEMPLATE for the friend declaration
11856 will be an IDENTIFIER_NODE. We are being called from
11857 tsubst_friend_function, and we want only to create a
11858 new decl (R) with appropriate types so that we can call
11859 determine_specialization. */
11860 gen_tmpl
= NULL_TREE
;
11863 if (DECL_CLASS_SCOPE_P (t
))
11865 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
11869 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
11870 complain
, t
, /*entering_scope=*/1);
11875 ctx
= DECL_CONTEXT (t
);
11877 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11878 if (type
== error_mark_node
)
11879 RETURN (error_mark_node
);
11881 /* If we hit excessive deduction depth, the type is bogus even if
11882 it isn't error_mark_node, so don't build a decl. */
11883 if (excessive_deduction_depth
)
11884 RETURN (error_mark_node
);
11886 /* We do NOT check for matching decls pushed separately at this
11887 point, as they may not represent instantiations of this
11888 template, and in any case are considered separate under the
11891 DECL_USE_TEMPLATE (r
) = 0;
11892 TREE_TYPE (r
) = type
;
11893 /* Clear out the mangled name and RTL for the instantiation. */
11894 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11895 SET_DECL_RTL (r
, NULL
);
11896 /* Leave DECL_INITIAL set on deleted instantiations. */
11897 if (!DECL_DELETED_FN (r
))
11898 DECL_INITIAL (r
) = NULL_TREE
;
11899 DECL_CONTEXT (r
) = ctx
;
11901 /* OpenMP UDRs have the only argument a reference to the declared
11902 type. We want to diagnose if the declared type is a reference,
11903 which is invalid, but as references to references are usually
11904 quietly merged, diagnose it here. */
11905 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
11908 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
11909 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
11910 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
11911 error_at (DECL_SOURCE_LOCATION (t
),
11912 "reference type %qT in "
11913 "%<#pragma omp declare reduction%>", argtype
);
11914 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
11915 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
11919 if (member
&& DECL_CONV_FN_P (r
))
11920 /* Type-conversion operator. Reconstruct the name, in
11921 case it's the name of one of the template's parameters. */
11922 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
11924 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
11926 DECL_RESULT (r
) = NULL_TREE
;
11928 TREE_STATIC (r
) = 0;
11929 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
11930 DECL_EXTERNAL (r
) = 1;
11931 /* If this is an instantiation of a function with internal
11932 linkage, we already know what object file linkage will be
11933 assigned to the instantiation. */
11934 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
11935 DECL_DEFER_OUTPUT (r
) = 0;
11936 DECL_CHAIN (r
) = NULL_TREE
;
11937 DECL_PENDING_INLINE_INFO (r
) = 0;
11938 DECL_PENDING_INLINE_P (r
) = 0;
11939 DECL_SAVED_TREE (r
) = NULL_TREE
;
11940 DECL_STRUCT_FUNCTION (r
) = NULL
;
11942 /* We'll re-clone as appropriate in instantiate_template. */
11943 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
11945 /* If we aren't complaining now, return on error before we register
11946 the specialization so that we'll complain eventually. */
11947 if ((complain
& tf_error
) == 0
11948 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11949 && !grok_op_properties (r
, /*complain=*/false))
11950 RETURN (error_mark_node
);
11952 /* When instantiating a constrained member, substitute
11953 into the constraints to create a new constraint. */
11954 if (tree ci
= get_constraints (t
))
11957 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
11958 set_constraints (r
, ci
);
11961 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11962 this in the special friend case mentioned above where
11963 GEN_TMPL is NULL. */
11966 DECL_TEMPLATE_INFO (r
)
11967 = build_template_info (gen_tmpl
, argvec
);
11968 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11971 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11973 /* We instantiated this while substituting into
11974 the type earlier (template/friend54.C). */
11977 /* We're not supposed to instantiate default arguments
11978 until they are called, for a template. But, for a
11981 template <class T> void f ()
11982 { extern void g(int i = T()); }
11984 we should do the substitution when the template is
11985 instantiated. We handle the member function case in
11986 instantiate_class_template since the default arguments
11987 might refer to other members of the class. */
11989 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
11990 && !uses_template_parms (argvec
))
11991 tsubst_default_arguments (r
, complain
);
11994 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
11996 /* Copy the list of befriending classes. */
11997 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
11999 friends
= &TREE_CHAIN (*friends
))
12001 *friends
= copy_node (*friends
);
12002 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
12007 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12009 maybe_retrofit_in_chrg (r
);
12010 if (DECL_CONSTRUCTOR_P (r
))
12011 grok_ctor_properties (ctx
, r
);
12012 if (DECL_INHERITED_CTOR_BASE (r
))
12013 deduce_inheriting_ctor (r
);
12014 /* If this is an instantiation of a member template, clone it.
12015 If it isn't, that'll be handled by
12016 clone_constructors_and_destructors. */
12017 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12018 clone_function_decl (r
, /*update_method_vec_p=*/0);
12020 else if ((complain
& tf_error
) != 0
12021 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12022 && !grok_op_properties (r
, /*complain=*/true))
12023 RETURN (error_mark_node
);
12025 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12026 SET_DECL_FRIEND_CONTEXT (r
,
12027 tsubst (DECL_FRIEND_CONTEXT (t
),
12028 args
, complain
, in_decl
));
12030 /* Possibly limit visibility based on template args. */
12031 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12032 if (DECL_VISIBILITY_SPECIFIED (t
))
12034 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12035 DECL_ATTRIBUTES (r
)
12036 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12038 determine_visibility (r
);
12039 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12040 && !processing_template_decl
)
12041 defaulted_late_check (r
);
12043 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12044 args
, complain
, in_decl
);
12050 tree type
= NULL_TREE
;
12052 tree expanded_types
= NULL_TREE
;
12053 tree prev_r
= NULL_TREE
;
12054 tree first_r
= NULL_TREE
;
12056 if (DECL_PACK_P (t
))
12058 /* If there is a local specialization that isn't a
12059 parameter pack, it means that we're doing a "simple"
12060 substitution from inside tsubst_pack_expansion. Just
12061 return the local specialization (which will be a single
12063 tree spec
= retrieve_local_specialization (t
);
12065 && TREE_CODE (spec
) == PARM_DECL
12066 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12069 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12070 the parameters in this function parameter pack. */
12071 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12072 complain
, in_decl
);
12073 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12075 len
= TREE_VEC_LENGTH (expanded_types
);
12077 /* Zero-length parameter packs are boring. Just substitute
12080 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12085 /* All we did was update the type. Make a note of that. */
12086 type
= expanded_types
;
12087 expanded_types
= NULL_TREE
;
12091 /* Loop through all of the parameters we'll build. When T is
12092 a function parameter pack, LEN is the number of expanded
12093 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12095 for (i
= 0; i
< len
; ++i
)
12099 if (DECL_TEMPLATE_PARM_P (t
))
12100 SET_DECL_TEMPLATE_PARM_P (r
);
12102 if (expanded_types
)
12103 /* We're on the Ith parameter of the function parameter
12106 /* Get the Ith type. */
12107 type
= TREE_VEC_ELT (expanded_types
, i
);
12109 /* Rename the parameter to include the index. */
12111 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12114 /* We're dealing with a normal parameter. */
12115 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12117 type
= type_decays_to (type
);
12118 TREE_TYPE (r
) = type
;
12119 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12121 if (DECL_INITIAL (r
))
12123 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12124 DECL_INITIAL (r
) = TREE_TYPE (r
);
12126 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12127 complain
, in_decl
);
12130 DECL_CONTEXT (r
) = NULL_TREE
;
12132 if (!DECL_TEMPLATE_PARM_P (r
))
12133 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12135 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12136 args
, complain
, in_decl
);
12138 /* Keep track of the first new parameter we
12139 generate. That's what will be returned to the
12144 /* Build a proper chain of parameters when substituting
12145 into a function parameter pack. */
12147 DECL_CHAIN (prev_r
) = r
;
12150 /* If cp_unevaluated_operand is set, we're just looking for a
12151 single dummy parameter, so don't keep going. */
12152 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12153 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12154 complain
, DECL_CHAIN (t
));
12156 /* FIRST_R contains the start of the chain we've built. */
12163 tree type
= NULL_TREE
;
12164 tree vec
= NULL_TREE
;
12165 tree expanded_types
= NULL_TREE
;
12168 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12170 /* This field is a lambda capture pack. Return a TREE_VEC of
12171 the expanded fields to instantiate_class_template_1 and
12172 store them in the specializations hash table as a
12173 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12174 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12175 complain
, in_decl
);
12176 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12178 len
= TREE_VEC_LENGTH (expanded_types
);
12179 vec
= make_tree_vec (len
);
12183 /* All we did was update the type. Make a note of that. */
12184 type
= expanded_types
;
12185 expanded_types
= NULL_TREE
;
12189 for (int i
= 0; i
< len
; ++i
)
12192 if (expanded_types
)
12194 type
= TREE_VEC_ELT (expanded_types
, i
);
12196 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12199 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12201 if (type
== error_mark_node
)
12202 RETURN (error_mark_node
);
12203 TREE_TYPE (r
) = type
;
12204 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12206 if (DECL_C_BIT_FIELD (r
))
12207 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12208 non-bit-fields DECL_INITIAL is a non-static data member
12209 initializer, which gets deferred instantiation. */
12211 = tsubst_expr (DECL_INITIAL (t
), args
,
12213 /*integral_constant_expression_p=*/true);
12214 else if (DECL_INITIAL (t
))
12216 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12217 NSDMI in perform_member_init. Still set DECL_INITIAL
12218 so that we know there is one. */
12219 DECL_INITIAL (r
) = void_node
;
12220 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12221 retrofit_lang_decl (r
);
12222 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12224 /* We don't have to set DECL_CONTEXT here; it is set by
12225 finish_member_declaration. */
12226 DECL_CHAIN (r
) = NULL_TREE
;
12228 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12229 args
, complain
, in_decl
);
12232 TREE_VEC_ELT (vec
, i
) = r
;
12238 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12239 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12240 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12241 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
12242 TREE_TYPE (pack
) = tpack
;
12243 register_specialization (pack
, t
, args
, false, 0);
12249 /* We reach here only for member using decls. We also need to check
12250 uses_template_parms because DECL_DEPENDENT_P is not set for a
12251 using-declaration that designates a member of the current
12252 instantiation (c++/53549). */
12253 if (DECL_DEPENDENT_P (t
)
12254 || uses_template_parms (USING_DECL_SCOPE (t
)))
12256 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12257 complain
, in_decl
);
12258 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12259 r
= do_class_using_decl (inst_scope
, name
);
12261 r
= error_mark_node
;
12264 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12265 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12271 DECL_CHAIN (r
) = NULL_TREE
;
12278 tree argvec
= NULL_TREE
;
12279 tree gen_tmpl
= NULL_TREE
;
12281 tree tmpl
= NULL_TREE
;
12283 tree type
= NULL_TREE
;
12286 if (TREE_TYPE (t
) == error_mark_node
)
12287 RETURN (error_mark_node
);
12289 if (TREE_CODE (t
) == TYPE_DECL
12290 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12292 /* If this is the canonical decl, we don't have to
12293 mess with instantiations, and often we can't (for
12294 typename, template type parms and such). Note that
12295 TYPE_NAME is not correct for the above test if
12296 we've copied the type for a typedef. */
12297 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12298 if (type
== error_mark_node
)
12299 RETURN (error_mark_node
);
12300 r
= TYPE_NAME (type
);
12304 /* Check to see if we already have the specialization we
12307 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12309 /* T is a static data member or namespace-scope entity.
12310 We have to substitute into namespace-scope variables
12311 (not just variable templates) because of cases like:
12313 template <class T> void f() { extern T t; }
12315 where the entity referenced is not known until
12316 instantiation time. */
12318 ctx
= DECL_CONTEXT (t
);
12319 if (DECL_CLASS_SCOPE_P (t
))
12321 ctx
= tsubst_aggr_type (ctx
, args
,
12323 in_decl
, /*entering_scope=*/1);
12324 /* If CTX is unchanged, then T is in fact the
12325 specialization we want. That situation occurs when
12326 referencing a static data member within in its own
12327 class. We can use pointer equality, rather than
12328 same_type_p, because DECL_CONTEXT is always
12330 if (ctx
== DECL_CONTEXT (t
)
12331 /* ... unless T is a member template; in which
12332 case our caller can be willing to create a
12333 specialization of that template represented
12335 && !(DECL_TI_TEMPLATE (t
)
12336 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12342 tmpl
= DECL_TI_TEMPLATE (t
);
12343 gen_tmpl
= most_general_template (tmpl
);
12344 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12345 if (argvec
!= error_mark_node
)
12346 argvec
= (coerce_innermost_template_parms
12347 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12348 argvec
, t
, complain
,
12349 /*all*/true, /*defarg*/true));
12350 if (argvec
== error_mark_node
)
12351 RETURN (error_mark_node
);
12352 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12353 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12358 /* A local variable. */
12360 /* Subsequent calls to pushdecl will fill this in. */
12362 /* Unless this is a reference to a static variable from an
12363 enclosing function, in which case we need to fill it in now. */
12364 if (TREE_STATIC (t
))
12366 tree fn
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12367 if (fn
!= current_function_decl
)
12370 spec
= retrieve_local_specialization (t
);
12372 /* If we already have the specialization we need, there is
12373 nothing more to do. */
12380 /* Create a new node for the specialization we need. */
12382 if (type
== NULL_TREE
)
12384 if (is_typedef_decl (t
))
12385 type
= DECL_ORIGINAL_TYPE (t
);
12387 type
= TREE_TYPE (t
);
12389 && VAR_HAD_UNKNOWN_BOUND (t
)
12390 && type
!= error_mark_node
)
12391 type
= strip_array_domain (type
);
12392 type
= tsubst (type
, args
, complain
, in_decl
);
12396 /* Even if the original location is out of scope, the
12397 newly substituted one is not. */
12398 DECL_DEAD_FOR_LOCAL (r
) = 0;
12399 DECL_INITIALIZED_P (r
) = 0;
12400 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
12401 if (type
== error_mark_node
)
12402 RETURN (error_mark_node
);
12403 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12405 /* It may seem that this case cannot occur, since:
12410 declares a function, not a variable. However:
12413 template <typename T> void g() { T t; }
12414 template void g<f>();
12416 is an attempt to declare a variable with function
12418 error ("variable %qD has function type",
12419 /* R is not yet sufficiently initialized, so we
12420 just use its name. */
12422 RETURN (error_mark_node
);
12424 type
= complete_type (type
);
12425 /* Wait until cp_finish_decl to set this again, to handle
12426 circular dependency (template/instantiate6.C). */
12427 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
12428 type
= check_var_type (DECL_NAME (r
), type
);
12430 if (DECL_HAS_VALUE_EXPR_P (t
))
12432 tree ve
= DECL_VALUE_EXPR (t
);
12433 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
12434 /*constant_expression_p=*/false);
12435 if (REFERENCE_REF_P (ve
))
12437 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
12438 ve
= TREE_OPERAND (ve
, 0);
12440 SET_DECL_VALUE_EXPR (r
, ve
);
12442 if (CP_DECL_THREAD_LOCAL_P (r
)
12443 && !processing_template_decl
)
12444 set_decl_tls_model (r
, decl_default_tls_model (r
));
12446 else if (DECL_SELF_REFERENCE_P (t
))
12447 SET_DECL_SELF_REFERENCE_P (r
);
12448 TREE_TYPE (r
) = type
;
12449 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12450 DECL_CONTEXT (r
) = ctx
;
12451 /* Clear out the mangled name and RTL for the instantiation. */
12452 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12453 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12454 SET_DECL_RTL (r
, NULL
);
12455 /* The initializer must not be expanded until it is required;
12456 see [temp.inst]. */
12457 DECL_INITIAL (r
) = NULL_TREE
;
12459 DECL_MODE (r
) = VOIDmode
;
12460 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12461 SET_DECL_RTL (r
, NULL
);
12462 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
12465 /* Possibly limit visibility based on template args. */
12466 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12467 if (DECL_VISIBILITY_SPECIFIED (t
))
12469 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12470 DECL_ATTRIBUTES (r
)
12471 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12473 determine_visibility (r
);
12478 /* A static data member declaration is always marked
12479 external when it is declared in-class, even if an
12480 initializer is present. We mimic the non-template
12481 processing here. */
12482 DECL_EXTERNAL (r
) = 1;
12483 if (DECL_NAMESPACE_SCOPE_P (t
))
12484 DECL_NOT_REALLY_EXTERN (r
) = 1;
12486 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
12487 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12488 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12492 if (DECL_LANG_SPECIFIC (r
))
12493 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12494 if (!cp_unevaluated_operand
)
12495 register_local_specialization (r
, t
);
12498 DECL_CHAIN (r
) = NULL_TREE
;
12500 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12502 args
, complain
, in_decl
);
12504 /* Preserve a typedef that names a type. */
12505 if (is_typedef_decl (r
))
12507 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12508 set_underlying_type (r
);
12509 if (TYPE_DECL_ALIAS_P (r
) && type
!= error_mark_node
)
12510 /* An alias template specialization can be dependent
12511 even if its underlying type is not. */
12512 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
12515 layout_decl (r
, 0);
12520 gcc_unreachable ();
12525 /* Restore the file and line information. */
12526 input_location
= saved_loc
;
12531 /* Substitute into the ARG_TYPES of a function type.
12532 If END is a TREE_CHAIN, leave it and any following types
12536 tsubst_arg_types (tree arg_types
,
12539 tsubst_flags_t complain
,
12542 tree remaining_arg_types
;
12543 tree type
= NULL_TREE
;
12545 tree expanded_args
= NULL_TREE
;
12548 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
12551 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
12552 args
, end
, complain
, in_decl
);
12553 if (remaining_arg_types
== error_mark_node
)
12554 return error_mark_node
;
12556 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
12558 /* For a pack expansion, perform substitution on the
12559 entire expression. Later on, we'll handle the arguments
12561 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
12562 args
, complain
, in_decl
);
12564 if (TREE_CODE (expanded_args
) == TREE_VEC
)
12565 /* So that we'll spin through the parameters, one by one. */
12566 i
= TREE_VEC_LENGTH (expanded_args
);
12569 /* We only partially substituted into the parameter
12570 pack. Our type is TYPE_PACK_EXPANSION. */
12571 type
= expanded_args
;
12572 expanded_args
= NULL_TREE
;
12580 type
= TREE_VEC_ELT (expanded_args
, i
);
12582 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
12584 if (type
== error_mark_node
)
12585 return error_mark_node
;
12586 if (VOID_TYPE_P (type
))
12588 if (complain
& tf_error
)
12590 error ("invalid parameter type %qT", type
);
12592 error ("in declaration %q+D", in_decl
);
12594 return error_mark_node
;
12597 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
12598 return error_mark_node
;
12600 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12601 top-level qualifiers as required. */
12602 type
= cv_unqualified (type_decays_to (type
));
12604 /* We do not substitute into default arguments here. The standard
12605 mandates that they be instantiated only when needed, which is
12606 done in build_over_call. */
12607 default_arg
= TREE_PURPOSE (arg_types
);
12609 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
12611 /* We've instantiated a template before its default arguments
12612 have been parsed. This can happen for a nested template
12613 class, and is not an error unless we require the default
12614 argument in a call of this function. */
12615 remaining_arg_types
=
12616 tree_cons (default_arg
, type
, remaining_arg_types
);
12617 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
12620 remaining_arg_types
=
12621 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
12624 return remaining_arg_types
;
12627 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12628 *not* handle the exception-specification for FNTYPE, because the
12629 initial substitution of explicitly provided template parameters
12630 during argument deduction forbids substitution into the
12631 exception-specification:
12635 All references in the function type of the function template to the
12636 corresponding template parameters are replaced by the specified tem-
12637 plate argument values. If a substitution in a template parameter or
12638 in the function type of the function template results in an invalid
12639 type, type deduction fails. [Note: The equivalent substitution in
12640 exception specifications is done only when the function is instanti-
12641 ated, at which point a program is ill-formed if the substitution
12642 results in an invalid type.] */
12645 tsubst_function_type (tree t
,
12647 tsubst_flags_t complain
,
12651 tree arg_types
= NULL_TREE
;
12654 /* The TYPE_CONTEXT is not used for function/method types. */
12655 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
12657 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12659 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
12661 if (late_return_type_p
)
12663 /* Substitute the argument types. */
12664 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12665 complain
, in_decl
);
12666 if (arg_types
== error_mark_node
)
12667 return error_mark_node
;
12669 tree save_ccp
= current_class_ptr
;
12670 tree save_ccr
= current_class_ref
;
12671 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
12672 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
12673 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
12676 /* DR 1207: 'this' is in scope in the trailing return type. */
12677 inject_this_parameter (this_type
, cp_type_quals (this_type
));
12680 /* Substitute the return type. */
12681 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12685 current_class_ptr
= save_ccp
;
12686 current_class_ref
= save_ccr
;
12690 /* Substitute the return type. */
12691 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12693 if (return_type
== error_mark_node
)
12694 return error_mark_node
;
12695 /* DR 486 clarifies that creation of a function type with an
12696 invalid return type is a deduction failure. */
12697 if (TREE_CODE (return_type
) == ARRAY_TYPE
12698 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
12700 if (complain
& tf_error
)
12702 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
12703 error ("function returning an array");
12705 error ("function returning a function");
12707 return error_mark_node
;
12710 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
12711 return error_mark_node
;
12713 if (!late_return_type_p
)
12715 /* Substitute the argument types. */
12716 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12717 complain
, in_decl
);
12718 if (arg_types
== error_mark_node
)
12719 return error_mark_node
;
12722 /* Construct a new type node and return it. */
12723 if (TREE_CODE (t
) == FUNCTION_TYPE
)
12725 fntype
= build_function_type (return_type
, arg_types
);
12726 fntype
= apply_memfn_quals (fntype
,
12727 type_memfn_quals (t
),
12728 type_memfn_rqual (t
));
12732 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
12733 /* Don't pick up extra function qualifiers from the basetype. */
12734 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
12735 if (! MAYBE_CLASS_TYPE_P (r
))
12739 Type deduction may fail for any of the following
12742 -- Attempting to create "pointer to member of T" when T
12743 is not a class type. */
12744 if (complain
& tf_error
)
12745 error ("creating pointer to member function of non-class type %qT",
12747 return error_mark_node
;
12750 fntype
= build_method_type_directly (r
, return_type
,
12751 TREE_CHAIN (arg_types
));
12752 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
12754 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
12756 if (late_return_type_p
)
12757 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
12762 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12763 ARGS into that specification, and return the substituted
12764 specification. If there is no specification, return NULL_TREE. */
12767 tsubst_exception_specification (tree fntype
,
12769 tsubst_flags_t complain
,
12776 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
12777 new_specs
= NULL_TREE
;
12778 if (specs
&& TREE_PURPOSE (specs
))
12780 /* A noexcept-specifier. */
12781 tree expr
= TREE_PURPOSE (specs
);
12782 if (TREE_CODE (expr
) == INTEGER_CST
)
12786 /* Defer instantiation of noexcept-specifiers to avoid
12787 excessive instantiations (c++/49107). */
12788 new_specs
= make_node (DEFERRED_NOEXCEPT
);
12789 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
12791 /* We already partially instantiated this member template,
12792 so combine the new args with the old. */
12793 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
12794 = DEFERRED_NOEXCEPT_PATTERN (expr
);
12795 DEFERRED_NOEXCEPT_ARGS (new_specs
)
12796 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
12800 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
12801 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
12805 new_specs
= tsubst_copy_and_build
12806 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
12807 /*integral_constant_expression_p=*/true);
12808 new_specs
= build_noexcept_spec (new_specs
, complain
);
12812 if (! TREE_VALUE (specs
))
12819 tree expanded_specs
= NULL_TREE
;
12821 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
12823 /* Expand the pack expansion type. */
12824 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
12828 if (expanded_specs
== error_mark_node
)
12829 return error_mark_node
;
12830 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
12831 len
= TREE_VEC_LENGTH (expanded_specs
);
12834 /* We're substituting into a member template, so
12835 we got a TYPE_PACK_EXPANSION back. Add that
12836 expansion and move on. */
12837 gcc_assert (TREE_CODE (expanded_specs
)
12838 == TYPE_PACK_EXPANSION
);
12839 new_specs
= add_exception_specifier (new_specs
,
12842 specs
= TREE_CHAIN (specs
);
12847 for (i
= 0; i
< len
; ++i
)
12849 if (expanded_specs
)
12850 spec
= TREE_VEC_ELT (expanded_specs
, i
);
12852 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
12853 if (spec
== error_mark_node
)
12855 new_specs
= add_exception_specifier (new_specs
, spec
,
12859 specs
= TREE_CHAIN (specs
);
12865 /* Take the tree structure T and replace template parameters used
12866 therein with the argument vector ARGS. IN_DECL is an associated
12867 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12868 Issue error and warning messages under control of COMPLAIN. Note
12869 that we must be relatively non-tolerant of extensions here, in
12870 order to preserve conformance; if we allow substitutions that
12871 should not be allowed, we may allow argument deductions that should
12872 not succeed, and therefore report ambiguous overload situations
12873 where there are none. In theory, we could allow the substitution,
12874 but indicate that it should have failed, and allow our caller to
12875 make sure that the right thing happens, but we don't try to do this
12878 This function is used for dealing with types, decls and the like;
12879 for expressions, use tsubst_expr or tsubst_copy. */
12882 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12884 enum tree_code code
;
12885 tree type
, r
= NULL_TREE
;
12887 if (t
== NULL_TREE
|| t
== error_mark_node
12888 || t
== integer_type_node
12889 || t
== void_type_node
12890 || t
== char_type_node
12891 || t
== unknown_type_node
12892 || TREE_CODE (t
) == NAMESPACE_DECL
12893 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
12897 return tsubst_decl (t
, args
, complain
);
12899 if (args
== NULL_TREE
)
12902 code
= TREE_CODE (t
);
12904 if (code
== IDENTIFIER_NODE
)
12905 type
= IDENTIFIER_TYPE_VALUE (t
);
12907 type
= TREE_TYPE (t
);
12909 gcc_assert (type
!= unknown_type_node
);
12911 /* Reuse typedefs. We need to do this to handle dependent attributes,
12912 such as attribute aligned. */
12914 && typedef_variant_p (t
))
12916 tree decl
= TYPE_NAME (t
);
12918 if (alias_template_specialization_p (t
))
12920 /* DECL represents an alias template and we want to
12922 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12923 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12924 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
12926 else if (DECL_CLASS_SCOPE_P (decl
)
12927 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12928 && uses_template_parms (DECL_CONTEXT (decl
)))
12930 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12931 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12932 r
= retrieve_specialization (tmpl
, gen_args
, 0);
12934 else if (DECL_FUNCTION_SCOPE_P (decl
)
12935 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12936 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
12937 r
= retrieve_local_specialization (decl
);
12939 /* The typedef is from a non-template context. */
12945 r
= cp_build_qualified_type_real
12946 (r
, cp_type_quals (t
) | cp_type_quals (r
),
12947 complain
| tf_ignore_bad_quals
);
12952 /* We don't have an instantiation yet, so drop the typedef. */
12953 int quals
= cp_type_quals (t
);
12954 t
= DECL_ORIGINAL_TYPE (decl
);
12955 t
= cp_build_qualified_type_real (t
, quals
,
12956 complain
| tf_ignore_bad_quals
);
12961 && code
!= TYPENAME_TYPE
12962 && code
!= TEMPLATE_TYPE_PARM
12963 && code
!= IDENTIFIER_NODE
12964 && code
!= FUNCTION_TYPE
12965 && code
!= METHOD_TYPE
)
12966 type
= tsubst (type
, args
, complain
, in_decl
);
12967 if (type
== error_mark_node
)
12968 return error_mark_node
;
12974 case ENUMERAL_TYPE
:
12975 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
12976 /*entering_scope=*/0);
12979 case IDENTIFIER_NODE
:
12990 if (t
== integer_type_node
)
12993 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
12994 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
12998 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13000 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13001 /*integral_constant_expression_p=*/false);
13003 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13005 if (TREE_CODE (max
) == NOP_EXPR
13006 && TREE_SIDE_EFFECTS (omax
)
13007 && !TREE_TYPE (max
))
13008 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13010 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13011 with TREE_SIDE_EFFECTS that indicates this is not an integral
13012 constant expression. */
13013 if (processing_template_decl
13014 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13016 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13017 TREE_SIDE_EFFECTS (max
) = 1;
13020 return compute_array_index_type (NULL_TREE
, max
, complain
);
13023 case TEMPLATE_TYPE_PARM
:
13024 case TEMPLATE_TEMPLATE_PARM
:
13025 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13026 case TEMPLATE_PARM_INDEX
:
13031 tree arg
= NULL_TREE
;
13033 /* Early in template argument deduction substitution, we don't
13034 want to reduce the level of 'auto', or it will be confused
13035 with a normal template parm in subsequent deduction. */
13036 if (is_auto (t
) && (complain
& tf_partial
))
13041 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13042 template_parm_level_and_index (t
, &level
, &idx
);
13044 levels
= TMPL_ARGS_DEPTH (args
);
13045 if (level
<= levels
13046 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13048 arg
= TMPL_ARG (args
, level
, idx
);
13050 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13052 /* See through ARGUMENT_PACK_SELECT arguments. */
13053 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13054 /* If the selected argument is an expansion E, that most
13055 likely means we were called from
13056 gen_elem_of_pack_expansion_instantiation during the
13057 substituting of pack an argument pack (which Ith
13058 element is a pack expansion, where I is
13059 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13060 In this case, the Ith element resulting from this
13061 substituting is going to be a pack expansion, which
13062 pattern is the pattern of E. Let's return the
13064 gen_elem_of_pack_expansion_instantiation will
13065 build the resulting pack expansion from it. */
13066 if (PACK_EXPANSION_P (arg
))
13068 /* Make sure we aren't throwing away arg info. */
13069 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13070 arg
= PACK_EXPANSION_PATTERN (arg
);
13075 if (arg
== error_mark_node
)
13076 return error_mark_node
;
13077 else if (arg
!= NULL_TREE
)
13079 if (ARGUMENT_PACK_P (arg
))
13080 /* If ARG is an argument pack, we don't actually want to
13081 perform a substitution here, because substitutions
13082 for argument packs are only done
13083 element-by-element. We can get to this point when
13084 substituting the type of a non-type template
13085 parameter pack, when that type actually contains
13086 template parameter packs from an outer template, e.g.,
13088 template<typename... Types> struct A {
13089 template<Types... Values> struct B { };
13093 if (code
== TEMPLATE_TYPE_PARM
)
13096 gcc_assert (TYPE_P (arg
));
13098 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13100 return cp_build_qualified_type_real
13101 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13103 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13105 /* We are processing a type constructed from a
13106 template template parameter. */
13107 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13108 args
, complain
, in_decl
);
13109 if (argvec
== error_mark_node
)
13110 return error_mark_node
;
13112 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13113 || TREE_CODE (arg
) == TEMPLATE_DECL
13114 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13116 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13117 /* Consider this code:
13119 template <template <class> class Template>
13121 template <class Arg> using Bind = Template<Arg>;
13124 template <template <class> class Template, class Arg>
13125 using Instantiate = Template<Arg>; //#0
13127 template <template <class> class Template,
13130 Instantiate<Internal<Template>::template Bind,
13133 When #1 is parsed, the
13134 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13135 parameter `Template' in #0 matches the
13136 UNBOUND_CLASS_TEMPLATE representing the argument
13137 `Internal<Template>::template Bind'; We then want
13138 to assemble the type `Bind<Argument>' that can't
13139 be fully created right now, because
13140 `Internal<Template>' not being complete, the Bind
13141 template cannot be looked up in that context. So
13142 we need to "store" `Bind<Argument>' for later
13143 when the context of Bind becomes complete. Let's
13144 store that in a TYPENAME_TYPE. */
13145 return make_typename_type (TYPE_CONTEXT (arg
),
13146 build_nt (TEMPLATE_ID_EXPR
,
13147 TYPE_IDENTIFIER (arg
),
13152 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13153 are resolving nested-types in the signature of a
13154 member function templates. Otherwise ARG is a
13155 TEMPLATE_DECL and is the real template to be
13157 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13158 arg
= TYPE_NAME (arg
);
13160 r
= lookup_template_class (arg
,
13162 DECL_CONTEXT (arg
),
13163 /*entering_scope=*/0,
13165 return cp_build_qualified_type_real
13166 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13169 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
13170 return convert_from_reference (unshare_expr (arg
));
13174 /* This can happen during the attempted tsubst'ing in
13175 unify. This means that we don't yet have any information
13176 about the template parameter in question. */
13179 /* If we get here, we must have been looking at a parm for a
13180 more deeply nested template. Make a new version of this
13181 template parameter, but with a lower level. */
13184 case TEMPLATE_TYPE_PARM
:
13185 case TEMPLATE_TEMPLATE_PARM
:
13186 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13187 if (cp_type_quals (t
))
13189 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13190 r
= cp_build_qualified_type_real
13191 (r
, cp_type_quals (t
),
13192 complain
| (code
== TEMPLATE_TYPE_PARM
13193 ? tf_ignore_bad_quals
: 0));
13195 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13196 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13197 && (r
= (TEMPLATE_PARM_DESCENDANTS
13198 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13199 && (r
= TREE_TYPE (r
))
13200 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13201 /* Break infinite recursion when substituting the constraints
13202 of a constrained placeholder. */;
13206 TEMPLATE_TYPE_PARM_INDEX (r
)
13207 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13208 r
, levels
, args
, complain
);
13209 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13210 TYPE_MAIN_VARIANT (r
) = r
;
13211 TYPE_POINTER_TO (r
) = NULL_TREE
;
13212 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13214 /* Propagate constraints on placeholders. */
13215 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13216 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13217 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13218 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13220 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13221 /* We have reduced the level of the template
13222 template parameter, but not the levels of its
13223 template parameters, so canonical_type_parameter
13224 will not be able to find the canonical template
13225 template parameter for this level. Thus, we
13226 require structural equality checking to compare
13227 TEMPLATE_TEMPLATE_PARMs. */
13228 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13229 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13230 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13232 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13234 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13236 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13237 /* We might need to substitute into the types of non-type
13238 template parameters. */
13239 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13240 complain
, in_decl
);
13241 if (tmpl
== error_mark_node
)
13242 return error_mark_node
;
13243 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13244 complain
, in_decl
);
13245 if (argvec
== error_mark_node
)
13246 return error_mark_node
;
13248 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13249 = build_template_info (tmpl
, argvec
);
13254 case TEMPLATE_PARM_INDEX
:
13255 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13259 gcc_unreachable ();
13267 tree purpose
, value
, chain
;
13269 if (t
== void_list_node
)
13272 purpose
= TREE_PURPOSE (t
);
13275 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13276 if (purpose
== error_mark_node
)
13277 return error_mark_node
;
13279 value
= TREE_VALUE (t
);
13282 value
= tsubst (value
, args
, complain
, in_decl
);
13283 if (value
== error_mark_node
)
13284 return error_mark_node
;
13286 chain
= TREE_CHAIN (t
);
13287 if (chain
&& chain
!= void_type_node
)
13289 chain
= tsubst (chain
, args
, complain
, in_decl
);
13290 if (chain
== error_mark_node
)
13291 return error_mark_node
;
13293 if (purpose
== TREE_PURPOSE (t
)
13294 && value
== TREE_VALUE (t
)
13295 && chain
== TREE_CHAIN (t
))
13297 return hash_tree_cons (purpose
, value
, chain
);
13301 /* We should never be tsubsting a binfo. */
13302 gcc_unreachable ();
13305 /* A vector of template arguments. */
13306 gcc_assert (!type
);
13307 return tsubst_template_args (t
, args
, complain
, in_decl
);
13310 case REFERENCE_TYPE
:
13312 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13317 Type deduction may fail for any of the following
13320 -- Attempting to create a pointer to reference type.
13321 -- Attempting to create a reference to a reference type or
13322 a reference to void.
13324 Core issue 106 says that creating a reference to a reference
13325 during instantiation is no longer a cause for failure. We
13326 only enforce this check in strict C++98 mode. */
13327 if ((TREE_CODE (type
) == REFERENCE_TYPE
13328 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
13329 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
13331 static location_t last_loc
;
13333 /* We keep track of the last time we issued this error
13334 message to avoid spewing a ton of messages during a
13335 single bad template instantiation. */
13336 if (complain
& tf_error
13337 && last_loc
!= input_location
)
13339 if (VOID_TYPE_P (type
))
13340 error ("forming reference to void");
13341 else if (code
== POINTER_TYPE
)
13342 error ("forming pointer to reference type %qT", type
);
13344 error ("forming reference to reference type %qT", type
);
13345 last_loc
= input_location
;
13348 return error_mark_node
;
13350 else if (TREE_CODE (type
) == FUNCTION_TYPE
13351 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13352 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13354 if (complain
& tf_error
)
13356 if (code
== POINTER_TYPE
)
13357 error ("forming pointer to qualified function type %qT",
13360 error ("forming reference to qualified function type %qT",
13363 return error_mark_node
;
13365 else if (code
== POINTER_TYPE
)
13367 r
= build_pointer_type (type
);
13368 if (TREE_CODE (type
) == METHOD_TYPE
)
13369 r
= build_ptrmemfunc_type (r
);
13371 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
13372 /* In C++0x, during template argument substitution, when there is an
13373 attempt to create a reference to a reference type, reference
13374 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13376 "If a template-argument for a template-parameter T names a type
13377 that is a reference to a type A, an attempt to create the type
13378 'lvalue reference to cv T' creates the type 'lvalue reference to
13379 A,' while an attempt to create the type type rvalue reference to
13380 cv T' creates the type T"
13382 r
= cp_build_reference_type
13384 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
13386 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
13387 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13389 if (r
!= error_mark_node
)
13390 /* Will this ever be needed for TYPE_..._TO values? */
13397 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
13398 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
13402 Type deduction may fail for any of the following
13405 -- Attempting to create "pointer to member of T" when T
13406 is not a class type. */
13407 if (complain
& tf_error
)
13408 error ("creating pointer to member of non-class type %qT", r
);
13409 return error_mark_node
;
13411 if (TREE_CODE (type
) == REFERENCE_TYPE
)
13413 if (complain
& tf_error
)
13414 error ("creating pointer to member reference type %qT", type
);
13415 return error_mark_node
;
13417 if (VOID_TYPE_P (type
))
13419 if (complain
& tf_error
)
13420 error ("creating pointer to member of type void");
13421 return error_mark_node
;
13423 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13424 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13426 /* The type of the implicit object parameter gets its
13427 cv-qualifiers from the FUNCTION_TYPE. */
13430 = build_memfn_type (type
, r
, type_memfn_quals (type
),
13431 type_memfn_rqual (type
));
13432 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
13433 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
13437 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
13441 case FUNCTION_TYPE
:
13446 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
13447 if (fntype
== error_mark_node
)
13448 return error_mark_node
;
13450 /* Substitute the exception specification. */
13451 specs
= tsubst_exception_specification (t
, args
, complain
,
13452 in_decl
, /*defer_ok*/true);
13453 if (specs
== error_mark_node
)
13454 return error_mark_node
;
13456 fntype
= build_exception_variant (fntype
, specs
);
13461 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
13462 if (domain
== error_mark_node
)
13463 return error_mark_node
;
13465 /* As an optimization, we avoid regenerating the array type if
13466 it will obviously be the same as T. */
13467 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
13470 /* These checks should match the ones in create_array_type_for_decl.
13474 The deduction may fail for any of the following reasons:
13476 -- Attempting to create an array with an element type that
13477 is void, a function type, or a reference type, or [DR337]
13478 an abstract class type. */
13479 if (VOID_TYPE_P (type
)
13480 || TREE_CODE (type
) == FUNCTION_TYPE
13481 || (TREE_CODE (type
) == ARRAY_TYPE
13482 && TYPE_DOMAIN (type
) == NULL_TREE
)
13483 || TREE_CODE (type
) == REFERENCE_TYPE
)
13485 if (complain
& tf_error
)
13486 error ("creating array of %qT", type
);
13487 return error_mark_node
;
13490 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
13491 return error_mark_node
;
13493 r
= build_cplus_array_type (type
, domain
);
13495 if (TYPE_USER_ALIGN (t
))
13497 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
13498 TYPE_USER_ALIGN (r
) = 1;
13504 case TYPENAME_TYPE
:
13506 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13507 in_decl
, /*entering_scope=*/1);
13508 if (ctx
== error_mark_node
)
13509 return error_mark_node
;
13511 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
13512 complain
, in_decl
);
13513 if (f
== error_mark_node
)
13514 return error_mark_node
;
13516 if (!MAYBE_CLASS_TYPE_P (ctx
))
13518 if (complain
& tf_error
)
13519 error ("%qT is not a class, struct, or union type", ctx
);
13520 return error_mark_node
;
13522 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
13524 /* Normally, make_typename_type does not require that the CTX
13525 have complete type in order to allow things like:
13527 template <class T> struct S { typename S<T>::X Y; };
13529 But, such constructs have already been resolved by this
13530 point, so here CTX really should have complete type, unless
13531 it's a partial instantiation. */
13532 ctx
= complete_type (ctx
);
13533 if (!COMPLETE_TYPE_P (ctx
))
13535 if (complain
& tf_error
)
13536 cxx_incomplete_type_error (NULL_TREE
, ctx
);
13537 return error_mark_node
;
13541 f
= make_typename_type (ctx
, f
, typename_type
,
13542 complain
| tf_keep_type_decl
);
13543 if (f
== error_mark_node
)
13545 if (TREE_CODE (f
) == TYPE_DECL
)
13547 complain
|= tf_ignore_bad_quals
;
13551 if (TREE_CODE (f
) != TYPENAME_TYPE
)
13553 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
13555 if (complain
& tf_error
)
13556 error ("%qT resolves to %qT, which is not an enumeration type",
13559 return error_mark_node
;
13561 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
13563 if (complain
& tf_error
)
13564 error ("%qT resolves to %qT, which is is not a class type",
13567 return error_mark_node
;
13571 return cp_build_qualified_type_real
13572 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
13575 case UNBOUND_CLASS_TEMPLATE
:
13577 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13578 in_decl
, /*entering_scope=*/1);
13579 tree name
= TYPE_IDENTIFIER (t
);
13580 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
13582 if (ctx
== error_mark_node
|| name
== error_mark_node
)
13583 return error_mark_node
;
13586 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
13587 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
13594 ++cp_unevaluated_operand
;
13595 ++c_inhibit_evaluation_warnings
;
13597 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
13599 /*integral_constant_expression_p=*/false);
13601 --cp_unevaluated_operand
;
13602 --c_inhibit_evaluation_warnings
;
13604 type
= finish_typeof (type
);
13605 return cp_build_qualified_type_real (type
,
13607 | cp_type_quals (type
),
13611 case DECLTYPE_TYPE
:
13615 ++cp_unevaluated_operand
;
13616 ++c_inhibit_evaluation_warnings
;
13618 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
13619 complain
|tf_decltype
, in_decl
,
13620 /*function_p*/false,
13621 /*integral_constant_expression*/false);
13623 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
13625 if (type
== NULL_TREE
)
13627 if (complain
& tf_error
)
13628 error ("empty initializer in lambda init-capture");
13629 type
= error_mark_node
;
13631 else if (TREE_CODE (type
) == TREE_LIST
)
13632 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
13635 --cp_unevaluated_operand
;
13636 --c_inhibit_evaluation_warnings
;
13638 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
13639 type
= lambda_capture_field_type (type
,
13640 DECLTYPE_FOR_INIT_CAPTURE (t
));
13641 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
13642 type
= lambda_proxy_type (type
);
13645 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
13646 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
13648 /* In a template ~id could be either a complement expression
13649 or an unqualified-id naming a destructor; if instantiating
13650 it produces an expression, it's not an id-expression or
13653 type
= finish_decltype_type (type
, id
, complain
);
13655 return cp_build_qualified_type_real (type
,
13657 | cp_type_quals (type
),
13658 complain
| tf_ignore_bad_quals
);
13661 case UNDERLYING_TYPE
:
13663 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
13664 complain
, in_decl
);
13665 return finish_underlying_type (type
);
13668 case TYPE_ARGUMENT_PACK
:
13669 case NONTYPE_ARGUMENT_PACK
:
13671 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
13673 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
13677 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
13679 /* For template nontype argument packs, also substitute into
13681 if (code
== NONTYPE_ARGUMENT_PACK
)
13682 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13701 /* We should use one of the expression tsubsts for these codes. */
13702 gcc_unreachable ();
13705 sorry ("use of %qs in template", get_tree_code_name (code
));
13706 return error_mark_node
;
13710 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13711 type of the expression on the left-hand side of the "." or "->"
13715 tsubst_baselink (tree baselink
, tree object_type
,
13716 tree args
, tsubst_flags_t complain
, tree in_decl
)
13719 tree qualifying_scope
;
13722 tree template_args
= 0;
13723 bool template_id_p
= false;
13724 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
13726 /* A baselink indicates a function from a base class. Both the
13727 BASELINK_ACCESS_BINFO and the base class referenced may
13728 indicate bases of the template class, rather than the
13729 instantiated class. In addition, lookups that were not
13730 ambiguous before may be ambiguous now. Therefore, we perform
13731 the lookup again. */
13732 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
13733 qualifying_scope
= tsubst (qualifying_scope
, args
,
13734 complain
, in_decl
);
13735 fns
= BASELINK_FUNCTIONS (baselink
);
13736 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
13737 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
13739 template_id_p
= true;
13740 template_args
= TREE_OPERAND (fns
, 1);
13741 fns
= TREE_OPERAND (fns
, 0);
13743 template_args
= tsubst_template_args (template_args
, args
,
13744 complain
, in_decl
);
13746 name
= DECL_NAME (get_first_fn (fns
));
13747 if (IDENTIFIER_TYPENAME_P (name
))
13748 name
= mangle_conv_op_name_for_type (optype
);
13749 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
13752 if (constructor_name_p (name
, qualifying_scope
))
13754 if (complain
& tf_error
)
13755 error ("cannot call constructor %<%T::%D%> directly",
13756 qualifying_scope
, name
);
13758 return error_mark_node
;
13761 /* If lookup found a single function, mark it as used at this
13762 point. (If it lookup found multiple functions the one selected
13763 later by overload resolution will be marked as used at that
13765 if (BASELINK_P (baselink
))
13766 fns
= BASELINK_FUNCTIONS (baselink
);
13767 if (!template_id_p
&& !really_overloaded_fn (fns
)
13768 && !mark_used (OVL_CURRENT (fns
), complain
) && !(complain
& tf_error
))
13769 return error_mark_node
;
13771 /* Add back the template arguments, if present. */
13772 if (BASELINK_P (baselink
) && template_id_p
)
13773 BASELINK_FUNCTIONS (baselink
)
13774 = build2 (TEMPLATE_ID_EXPR
,
13776 BASELINK_FUNCTIONS (baselink
),
13778 /* Update the conversion operator type. */
13779 if (BASELINK_P (baselink
))
13780 BASELINK_OPTYPE (baselink
) = optype
;
13783 object_type
= current_class_type
;
13785 if (qualified
|| name
== complete_dtor_identifier
)
13787 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
13791 /* We need to call adjust_result_of_qualified_name_lookup in case the
13792 destructor names a base class, but we unset BASELINK_QUALIFIED_P
13793 so that we still get virtual function binding. */
13794 BASELINK_QUALIFIED_P (baselink
) = false;
13799 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13800 true if the qualified-id will be a postfix-expression in-and-of
13801 itself; false if more of the postfix-expression follows the
13802 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13806 tsubst_qualified_id (tree qualified_id
, tree args
,
13807 tsubst_flags_t complain
, tree in_decl
,
13808 bool done
, bool address_p
)
13814 tree template_args
;
13815 location_t loc
= UNKNOWN_LOCATION
;
13817 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
13819 /* Figure out what name to look up. */
13820 name
= TREE_OPERAND (qualified_id
, 1);
13821 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13823 is_template
= true;
13824 loc
= EXPR_LOCATION (name
);
13825 template_args
= TREE_OPERAND (name
, 1);
13827 template_args
= tsubst_template_args (template_args
, args
,
13828 complain
, in_decl
);
13829 name
= TREE_OPERAND (name
, 0);
13833 is_template
= false;
13834 template_args
= NULL_TREE
;
13837 /* Substitute into the qualifying scope. When there are no ARGS, we
13838 are just trying to simplify a non-dependent expression. In that
13839 case the qualifying scope may be dependent, and, in any case,
13840 substituting will not help. */
13841 scope
= TREE_OPERAND (qualified_id
, 0);
13844 scope
= tsubst (scope
, args
, complain
, in_decl
);
13845 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
13850 if (dependent_scope_p (scope
))
13853 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
13854 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
13855 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
13856 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
13860 if (!BASELINK_P (name
) && !DECL_P (expr
))
13862 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
13864 /* A BIT_NOT_EXPR is used to represent a destructor. */
13865 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
13867 error ("qualifying type %qT does not match destructor name ~%qT",
13868 scope
, TREE_OPERAND (expr
, 0));
13869 expr
= error_mark_node
;
13872 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
13873 /*is_type_p=*/0, false);
13876 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
13877 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
13878 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
13880 if (complain
& tf_error
)
13882 error ("dependent-name %qE is parsed as a non-type, but "
13883 "instantiation yields a type", qualified_id
);
13884 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
13886 return error_mark_node
;
13892 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
13894 /* Remember that there was a reference to this entity. */
13895 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
13896 return error_mark_node
;
13899 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
13901 if (complain
& tf_error
)
13902 qualified_name_lookup_error (scope
,
13903 TREE_OPERAND (qualified_id
, 1),
13904 expr
, input_location
);
13905 return error_mark_node
;
13910 if (variable_template_p (expr
))
13911 expr
= lookup_and_finish_template_variable (expr
, template_args
,
13914 expr
= lookup_template_function (expr
, template_args
);
13917 if (expr
== error_mark_node
&& complain
& tf_error
)
13918 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
13919 expr
, input_location
);
13920 else if (TYPE_P (scope
))
13922 expr
= (adjust_result_of_qualified_name_lookup
13923 (expr
, scope
, current_nonlambda_class_type ()));
13924 expr
= (finish_qualified_id_expr
13925 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
13926 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
13927 /*template_arg_p=*/false, complain
));
13930 /* Expressions do not generally have reference type. */
13931 if (TREE_CODE (expr
) != SCOPE_REF
13932 /* However, if we're about to form a pointer-to-member, we just
13933 want the referenced member referenced. */
13934 && TREE_CODE (expr
) != OFFSET_REF
)
13935 expr
= convert_from_reference (expr
);
13937 if (REF_PARENTHESIZED_P (qualified_id
))
13938 expr
= force_paren_expr (expr
);
13943 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13944 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13948 tsubst_init (tree init
, tree decl
, tree args
,
13949 tsubst_flags_t complain
, tree in_decl
)
13954 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
13958 /* If we had an initializer but it
13959 instantiated to nothing,
13960 value-initialize the object. This will
13961 only occur when the initializer was a
13962 pack expansion where the parameter packs
13963 used in that expansion were of length
13965 init
= build_value_init (TREE_TYPE (decl
),
13967 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13968 init
= get_target_expr_sfinae (init
, complain
);
13974 /* Like tsubst, but deals with expressions. This function just replaces
13975 template parms; to finish processing the resultant expression, use
13976 tsubst_copy_and_build or tsubst_expr. */
13979 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13981 enum tree_code code
;
13984 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
13987 code
= TREE_CODE (t
);
13992 r
= retrieve_local_specialization (t
);
13994 if (r
== NULL_TREE
)
13996 /* We get here for a use of 'this' in an NSDMI as part of a
13997 constructor call or as part of an aggregate initialization. */
13998 if (DECL_NAME (t
) == this_identifier
13999 && ((current_function_decl
14000 && DECL_CONSTRUCTOR_P (current_function_decl
))
14001 || (current_class_ref
14002 && TREE_CODE (current_class_ref
) == PLACEHOLDER_EXPR
)))
14003 return current_class_ptr
;
14005 /* This can happen for a parameter name used later in a function
14006 declaration (such as in a late-specified return type). Just
14007 make a dummy decl, since it's only used for its type. */
14008 gcc_assert (cp_unevaluated_operand
!= 0);
14009 r
= tsubst_decl (t
, args
, complain
);
14010 /* Give it the template pattern as its context; its true context
14011 hasn't been instantiated yet and this is good enough for
14013 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14016 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14017 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14018 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14019 return error_mark_node
;
14027 if (DECL_TEMPLATE_PARM_P (t
))
14028 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14029 /* There is no need to substitute into namespace-scope
14031 if (DECL_NAMESPACE_SCOPE_P (t
))
14033 /* If ARGS is NULL, then T is known to be non-dependent. */
14034 if (args
== NULL_TREE
)
14035 return scalar_constant_value (t
);
14037 /* Unfortunately, we cannot just call lookup_name here.
14040 template <int I> int f() {
14042 struct S { void g() { E e = a; } };
14045 When we instantiate f<7>::S::g(), say, lookup_name is not
14046 clever enough to find f<7>::a. */
14048 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14049 /*entering_scope=*/0);
14051 for (v
= TYPE_VALUES (enum_type
);
14053 v
= TREE_CHAIN (v
))
14054 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14055 return TREE_VALUE (v
);
14057 /* We didn't find the name. That should never happen; if
14058 name-lookup found it during preliminary parsing, we
14059 should find it again here during instantiation. */
14060 gcc_unreachable ();
14065 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14067 /* Check for a local specialization set up by
14068 tsubst_pack_expansion. */
14069 if (tree r
= retrieve_local_specialization (t
))
14071 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14072 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14076 /* When retrieving a capture pack from a generic lambda, remove the
14077 lambda call op's own template argument list from ARGS. Only the
14078 template arguments active for the closure type should be used to
14079 retrieve the pack specialization. */
14080 if (LAMBDA_FUNCTION_P (current_function_decl
)
14081 && (template_class_depth (DECL_CONTEXT (t
))
14082 != TMPL_ARGS_DEPTH (args
)))
14083 args
= strip_innermost_template_args (args
, 1);
14085 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14086 tsubst_decl put in the hash table. */
14087 return retrieve_specialization (t
, args
, 0);
14090 if (DECL_CONTEXT (t
))
14094 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14095 /*entering_scope=*/1);
14096 if (ctx
!= DECL_CONTEXT (t
))
14098 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14101 if (complain
& tf_error
)
14102 error ("using invalid field %qD", t
);
14103 return error_mark_node
;
14112 case FUNCTION_DECL
:
14113 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14114 r
= tsubst (t
, args
, complain
, in_decl
);
14115 else if (local_variable_p (t
)
14116 && uses_template_parms (DECL_CONTEXT (t
)))
14118 r
= retrieve_local_specialization (t
);
14119 if (r
== NULL_TREE
)
14121 /* First try name lookup to find the instantiation. */
14122 r
= lookup_name (DECL_NAME (t
));
14125 /* Make sure that the one we found is the one we want. */
14126 tree ctx
= DECL_CONTEXT (t
);
14127 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
14128 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
14129 if (ctx
!= DECL_CONTEXT (r
))
14137 /* This can happen for a variable used in a
14138 late-specified return type of a local lambda, or for a
14139 local static or constant. Building a new VAR_DECL
14140 should be OK in all those cases. */
14141 r
= tsubst_decl (t
, args
, complain
);
14142 if (decl_maybe_constant_var_p (r
))
14144 /* We can't call cp_finish_decl, so handle the
14145 initializer by hand. */
14146 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14147 complain
, in_decl
);
14148 if (!processing_template_decl
)
14149 init
= maybe_constant_init (init
);
14150 if (processing_template_decl
14151 ? potential_constant_expression (init
)
14152 : reduced_constant_expression_p (init
))
14153 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14154 = TREE_CONSTANT (r
) = true;
14155 DECL_INITIAL (r
) = init
;
14157 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14158 || decl_constant_var_p (r
)
14159 || errorcount
|| sorrycount
);
14160 if (!processing_template_decl
14161 && !TREE_STATIC (r
))
14162 r
= process_outer_var_ref (r
, complain
);
14164 /* Remember this for subsequent uses. */
14165 if (local_specializations
)
14166 register_local_specialization (r
, t
);
14171 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14172 return error_mark_node
;
14175 case NAMESPACE_DECL
:
14179 /* An OVERLOAD will always be a non-dependent overload set; an
14180 overload set from function scope will just be represented with an
14181 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14182 gcc_assert (!uses_template_parms (t
));
14186 return tsubst_baselink (t
, current_nonlambda_class_type (),
14187 args
, complain
, in_decl
);
14189 case TEMPLATE_DECL
:
14190 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14191 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14192 args
, complain
, in_decl
);
14193 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14194 return tsubst (t
, args
, complain
, in_decl
);
14195 else if (DECL_CLASS_SCOPE_P (t
)
14196 && uses_template_parms (DECL_CONTEXT (t
)))
14198 /* Template template argument like the following example need
14201 template <template <class> class TT> struct C {};
14202 template <class T> struct D {
14203 template <class U> struct E {};
14208 We are processing the template argument `E' in #1 for
14209 the template instantiation #2. Originally, `E' is a
14210 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14211 have to substitute this with one having context `D<int>'. */
14213 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14214 return lookup_field (context
, DECL_NAME(t
), 0, false);
14217 /* Ordinary template template argument. */
14221 case REINTERPRET_CAST_EXPR
:
14222 case CONST_CAST_EXPR
:
14223 case STATIC_CAST_EXPR
:
14224 case DYNAMIC_CAST_EXPR
:
14225 case IMPLICIT_CONV_EXPR
:
14229 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14230 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14231 return build1 (code
, type
, op0
);
14235 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14236 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14238 tree expanded
, op
= TREE_OPERAND (t
, 0);
14241 if (SIZEOF_EXPR_TYPE_P (t
))
14242 op
= TREE_TYPE (op
);
14244 ++cp_unevaluated_operand
;
14245 ++c_inhibit_evaluation_warnings
;
14246 /* We only want to compute the number of arguments. */
14247 if (PACK_EXPANSION_P (op
))
14248 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14250 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14251 args
, complain
, in_decl
);
14252 --cp_unevaluated_operand
;
14253 --c_inhibit_evaluation_warnings
;
14255 if (TREE_CODE (expanded
) == TREE_VEC
)
14257 len
= TREE_VEC_LENGTH (expanded
);
14258 /* Set TREE_USED for the benefit of -Wunused. */
14259 for (int i
= 0; i
< len
; i
++)
14260 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14261 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14264 if (expanded
== error_mark_node
)
14265 return error_mark_node
;
14266 else if (PACK_EXPANSION_P (expanded
)
14267 || (TREE_CODE (expanded
) == TREE_VEC
14268 && pack_expansion_args_count (expanded
)))
14271 if (PACK_EXPANSION_P (expanded
))
14273 else if (TREE_VEC_LENGTH (expanded
) == 1)
14274 expanded
= TREE_VEC_ELT (expanded
, 0);
14276 expanded
= make_argument_pack (expanded
);
14278 if (TYPE_P (expanded
))
14279 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14280 complain
& tf_error
);
14282 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14283 complain
& tf_error
);
14286 return build_int_cst (size_type_node
, len
);
14288 if (SIZEOF_EXPR_TYPE_P (t
))
14290 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14291 args
, complain
, in_decl
);
14292 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14293 r
= build1 (SIZEOF_EXPR
,
14294 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14295 SIZEOF_EXPR_TYPE_P (r
) = 1;
14302 case TRUTH_NOT_EXPR
:
14305 case UNARY_PLUS_EXPR
: /* Unary + */
14307 case AT_ENCODE_EXPR
:
14311 case REALPART_EXPR
:
14312 case IMAGPART_EXPR
:
14315 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14316 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14317 return build1 (code
, type
, op0
);
14320 case COMPONENT_REF
:
14325 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14326 name
= TREE_OPERAND (t
, 1);
14327 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14329 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14330 complain
, in_decl
);
14331 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14333 else if (TREE_CODE (name
) == SCOPE_REF
14334 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14336 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14337 complain
, in_decl
);
14338 name
= TREE_OPERAND (name
, 1);
14339 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14340 complain
, in_decl
);
14341 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14342 name
= build_qualified_name (/*type=*/NULL_TREE
,
14344 /*template_p=*/false);
14346 else if (BASELINK_P (name
))
14347 name
= tsubst_baselink (name
,
14348 non_reference (TREE_TYPE (object
)),
14352 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14353 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14359 case TRUNC_DIV_EXPR
:
14360 case CEIL_DIV_EXPR
:
14361 case FLOOR_DIV_EXPR
:
14362 case ROUND_DIV_EXPR
:
14363 case EXACT_DIV_EXPR
:
14367 case TRUNC_MOD_EXPR
:
14368 case FLOOR_MOD_EXPR
:
14369 case TRUTH_ANDIF_EXPR
:
14370 case TRUTH_ORIF_EXPR
:
14371 case TRUTH_AND_EXPR
:
14372 case TRUTH_OR_EXPR
:
14385 case COMPOUND_EXPR
:
14388 case PREDECREMENT_EXPR
:
14389 case PREINCREMENT_EXPR
:
14390 case POSTDECREMENT_EXPR
:
14391 case POSTINCREMENT_EXPR
:
14393 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14394 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14395 return build_nt (code
, op0
, op1
);
14400 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14401 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14402 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14403 QUALIFIED_NAME_IS_TEMPLATE (t
));
14408 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14409 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14410 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14415 int n
= VL_EXP_OPERAND_LENGTH (t
);
14416 tree result
= build_vl_exp (CALL_EXPR
, n
);
14418 for (i
= 0; i
< n
; i
++)
14419 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14420 complain
, in_decl
);
14426 case PSEUDO_DTOR_EXPR
:
14427 case VEC_PERM_EXPR
:
14429 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14430 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14431 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14432 r
= build_nt (code
, op0
, op1
, op2
);
14433 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14439 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14440 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14441 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14442 r
= build_nt (code
, op0
, op1
, op2
);
14443 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14449 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14450 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14451 r
= build_nt (code
, op0
, op1
);
14452 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14453 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14457 case TEMPLATE_ID_EXPR
:
14459 /* Substituted template arguments */
14460 tree fn
= TREE_OPERAND (t
, 0);
14461 tree targs
= TREE_OPERAND (t
, 1);
14463 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14465 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14467 return lookup_template_function (fn
, targs
);
14472 tree purpose
, value
, chain
;
14474 if (t
== void_list_node
)
14477 purpose
= TREE_PURPOSE (t
);
14479 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
14480 value
= TREE_VALUE (t
);
14482 value
= tsubst_copy (value
, args
, complain
, in_decl
);
14483 chain
= TREE_CHAIN (t
);
14484 if (chain
&& chain
!= void_type_node
)
14485 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
14486 if (purpose
== TREE_PURPOSE (t
)
14487 && value
== TREE_VALUE (t
)
14488 && chain
== TREE_CHAIN (t
))
14490 return tree_cons (purpose
, value
, chain
);
14495 case ENUMERAL_TYPE
:
14497 case TEMPLATE_TYPE_PARM
:
14498 case TEMPLATE_TEMPLATE_PARM
:
14499 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14500 case TEMPLATE_PARM_INDEX
:
14502 case REFERENCE_TYPE
:
14504 case FUNCTION_TYPE
:
14507 case TYPENAME_TYPE
:
14508 case UNBOUND_CLASS_TEMPLATE
:
14510 case DECLTYPE_TYPE
:
14512 return tsubst (t
, args
, complain
, in_decl
);
14516 /* Fall through. */
14517 case IDENTIFIER_NODE
:
14518 if (IDENTIFIER_TYPENAME_P (t
))
14520 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14521 return mangle_conv_op_name_for_type (new_type
);
14527 /* This is handled by tsubst_copy_and_build. */
14528 gcc_unreachable ();
14532 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14533 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14534 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
14537 case CLEANUP_POINT_EXPR
:
14538 /* We shouldn't have built any of these during initial template
14539 generation. Instead, they should be built during instantiation
14540 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14541 gcc_unreachable ();
14545 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14546 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14547 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14548 r
= build2 (code
, type
, op0
, op1
);
14549 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
14550 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
14551 && !(complain
& tf_error
))
14552 return error_mark_node
;
14556 case EXPR_PACK_EXPANSION
:
14557 error ("invalid use of pack expansion expression");
14558 return error_mark_node
;
14560 case NONTYPE_ARGUMENT_PACK
:
14561 error ("use %<...%> to expand argument pack");
14562 return error_mark_node
;
14565 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
14573 /* Instantiate any typedefs in the type. */
14574 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14575 r
= fold_convert (type
, t
);
14576 gcc_assert (TREE_CODE (r
) == code
);
14581 /* These can sometimes show up in a partial instantiation, but never
14582 involve template parms. */
14583 gcc_assert (!uses_template_parms (t
));
14586 case UNARY_LEFT_FOLD_EXPR
:
14587 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
14588 case UNARY_RIGHT_FOLD_EXPR
:
14589 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
14590 case BINARY_LEFT_FOLD_EXPR
:
14591 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
14592 case BINARY_RIGHT_FOLD_EXPR
:
14593 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
14596 /* We shouldn't get here, but keep going if !flag_checking. */
14598 gcc_unreachable ();
14603 /* Helper function for tsubst_omp_clauses, used for instantiation of
14604 OMP_CLAUSE_DECL of clauses. */
14607 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
14610 if (decl
== NULL_TREE
)
14613 /* Handle an OpenMP array section represented as a TREE_LIST (or
14614 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14615 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14616 TREE_LIST. We can handle it exactly the same as an array section
14617 (purpose, value, and a chain), even though the nomenclature
14618 (low_bound, length, etc) is different. */
14619 if (TREE_CODE (decl
) == TREE_LIST
)
14622 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
14623 /*integral_constant_expression_p=*/false);
14624 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
14625 /*integral_constant_expression_p=*/false);
14626 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
14628 if (TREE_PURPOSE (decl
) == low_bound
14629 && TREE_VALUE (decl
) == length
14630 && TREE_CHAIN (decl
) == chain
)
14632 tree ret
= tree_cons (low_bound
, length
, chain
);
14633 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
14634 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
14637 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
14638 /*integral_constant_expression_p=*/false);
14639 /* Undo convert_from_reference tsubst_expr could have called. */
14641 && REFERENCE_REF_P (ret
)
14642 && !REFERENCE_REF_P (decl
))
14643 ret
= TREE_OPERAND (ret
, 0);
14647 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14650 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
14651 tree args
, tsubst_flags_t complain
, tree in_decl
)
14653 tree new_clauses
= NULL_TREE
, nc
, oc
;
14654 tree linear_no_step
= NULL_TREE
;
14656 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
14658 nc
= copy_node (oc
);
14659 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
14662 switch (OMP_CLAUSE_CODE (nc
))
14664 case OMP_CLAUSE_LASTPRIVATE
:
14665 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
14667 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
14668 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
14669 in_decl
, /*integral_constant_expression_p=*/false);
14670 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
14671 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
14674 case OMP_CLAUSE_PRIVATE
:
14675 case OMP_CLAUSE_SHARED
:
14676 case OMP_CLAUSE_FIRSTPRIVATE
:
14677 case OMP_CLAUSE_COPYIN
:
14678 case OMP_CLAUSE_COPYPRIVATE
:
14679 case OMP_CLAUSE_UNIFORM
:
14680 case OMP_CLAUSE_DEPEND
:
14681 case OMP_CLAUSE_FROM
:
14682 case OMP_CLAUSE_TO
:
14683 case OMP_CLAUSE_MAP
:
14684 case OMP_CLAUSE_USE_DEVICE_PTR
:
14685 case OMP_CLAUSE_IS_DEVICE_PTR
:
14686 OMP_CLAUSE_DECL (nc
)
14687 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14690 case OMP_CLAUSE_IF
:
14691 case OMP_CLAUSE_NUM_THREADS
:
14692 case OMP_CLAUSE_SCHEDULE
:
14693 case OMP_CLAUSE_COLLAPSE
:
14694 case OMP_CLAUSE_FINAL
:
14695 case OMP_CLAUSE_DEVICE
:
14696 case OMP_CLAUSE_DIST_SCHEDULE
:
14697 case OMP_CLAUSE_NUM_TEAMS
:
14698 case OMP_CLAUSE_THREAD_LIMIT
:
14699 case OMP_CLAUSE_SAFELEN
:
14700 case OMP_CLAUSE_SIMDLEN
:
14701 case OMP_CLAUSE_NUM_TASKS
:
14702 case OMP_CLAUSE_GRAINSIZE
:
14703 case OMP_CLAUSE_PRIORITY
:
14704 case OMP_CLAUSE_ORDERED
:
14705 case OMP_CLAUSE_HINT
:
14706 case OMP_CLAUSE_NUM_GANGS
:
14707 case OMP_CLAUSE_NUM_WORKERS
:
14708 case OMP_CLAUSE_VECTOR_LENGTH
:
14709 case OMP_CLAUSE_WORKER
:
14710 case OMP_CLAUSE_VECTOR
:
14711 case OMP_CLAUSE_ASYNC
:
14712 case OMP_CLAUSE_WAIT
:
14713 OMP_CLAUSE_OPERAND (nc
, 0)
14714 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14715 in_decl
, /*integral_constant_expression_p=*/false);
14717 case OMP_CLAUSE_REDUCTION
:
14718 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
14720 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
14721 if (TREE_CODE (placeholder
) == SCOPE_REF
)
14723 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
14724 complain
, in_decl
);
14725 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
14726 = build_qualified_name (NULL_TREE
, scope
,
14727 TREE_OPERAND (placeholder
, 1),
14731 gcc_assert (identifier_p (placeholder
));
14733 OMP_CLAUSE_DECL (nc
)
14734 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14737 case OMP_CLAUSE_GANG
:
14738 case OMP_CLAUSE_ALIGNED
:
14739 OMP_CLAUSE_DECL (nc
)
14740 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14742 OMP_CLAUSE_OPERAND (nc
, 1)
14743 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
14744 in_decl
, /*integral_constant_expression_p=*/false);
14746 case OMP_CLAUSE_LINEAR
:
14747 OMP_CLAUSE_DECL (nc
)
14748 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14750 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
14752 gcc_assert (!linear_no_step
);
14753 linear_no_step
= nc
;
14755 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
14756 OMP_CLAUSE_LINEAR_STEP (nc
)
14757 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
14758 complain
, in_decl
);
14760 OMP_CLAUSE_LINEAR_STEP (nc
)
14761 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
14763 /*integral_constant_expression_p=*/false);
14765 case OMP_CLAUSE_NOWAIT
:
14766 case OMP_CLAUSE_DEFAULT
:
14767 case OMP_CLAUSE_UNTIED
:
14768 case OMP_CLAUSE_MERGEABLE
:
14769 case OMP_CLAUSE_INBRANCH
:
14770 case OMP_CLAUSE_NOTINBRANCH
:
14771 case OMP_CLAUSE_PROC_BIND
:
14772 case OMP_CLAUSE_FOR
:
14773 case OMP_CLAUSE_PARALLEL
:
14774 case OMP_CLAUSE_SECTIONS
:
14775 case OMP_CLAUSE_TASKGROUP
:
14776 case OMP_CLAUSE_NOGROUP
:
14777 case OMP_CLAUSE_THREADS
:
14778 case OMP_CLAUSE_SIMD
:
14779 case OMP_CLAUSE_DEFAULTMAP
:
14780 case OMP_CLAUSE_INDEPENDENT
:
14781 case OMP_CLAUSE_AUTO
:
14782 case OMP_CLAUSE_SEQ
:
14784 case OMP_CLAUSE_TILE
:
14787 for (lnc
= OMP_CLAUSE_TILE_LIST (nc
),
14788 loc
= OMP_CLAUSE_TILE_LIST (oc
);
14790 loc
= TREE_CHAIN (loc
), lnc
= TREE_CHAIN (lnc
))
14792 TREE_VALUE (lnc
) = tsubst_expr (TREE_VALUE (loc
), args
,
14793 complain
, in_decl
, false);
14798 gcc_unreachable ();
14800 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
14801 switch (OMP_CLAUSE_CODE (nc
))
14803 case OMP_CLAUSE_SHARED
:
14804 case OMP_CLAUSE_PRIVATE
:
14805 case OMP_CLAUSE_FIRSTPRIVATE
:
14806 case OMP_CLAUSE_LASTPRIVATE
:
14807 case OMP_CLAUSE_COPYPRIVATE
:
14808 case OMP_CLAUSE_LINEAR
:
14809 case OMP_CLAUSE_REDUCTION
:
14810 case OMP_CLAUSE_USE_DEVICE_PTR
:
14811 case OMP_CLAUSE_IS_DEVICE_PTR
:
14812 /* tsubst_expr on SCOPE_REF results in returning
14813 finish_non_static_data_member result. Undo that here. */
14814 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
14815 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
14816 == IDENTIFIER_NODE
))
14818 tree t
= OMP_CLAUSE_DECL (nc
);
14821 switch (TREE_CODE (v
))
14823 case COMPONENT_REF
:
14827 case POINTER_PLUS_EXPR
:
14828 v
= TREE_OPERAND (v
, 0);
14831 if (DECL_CONTEXT (v
) == current_function_decl
14832 && DECL_ARTIFICIAL (v
)
14833 && DECL_NAME (v
) == this_identifier
)
14834 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
14841 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
14842 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
14843 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
14844 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
14845 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
14847 tree decl
= OMP_CLAUSE_DECL (nc
);
14850 if (!DECL_LANG_SPECIFIC (decl
))
14851 retrofit_lang_decl (decl
);
14852 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
14861 new_clauses
= nreverse (new_clauses
);
14862 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
14864 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
14865 if (linear_no_step
)
14866 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
14867 if (nc
== linear_no_step
)
14869 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
14873 return new_clauses
;
14876 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14879 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
14882 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14884 tree purpose
, value
, chain
;
14889 if (TREE_CODE (t
) != TREE_LIST
)
14890 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14891 /*function_p=*/false,
14892 /*integral_constant_expression_p=*/false);
14894 if (t
== void_list_node
)
14897 purpose
= TREE_PURPOSE (t
);
14899 purpose
= RECUR (purpose
);
14900 value
= TREE_VALUE (t
);
14903 if (TREE_CODE (value
) != LABEL_DECL
)
14904 value
= RECUR (value
);
14907 value
= lookup_label (DECL_NAME (value
));
14908 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
14909 TREE_USED (value
) = 1;
14912 chain
= TREE_CHAIN (t
);
14913 if (chain
&& chain
!= void_type_node
)
14914 chain
= RECUR (chain
);
14915 return tree_cons (purpose
, value
, chain
);
14919 /* Used to temporarily communicate the list of #pragma omp parallel
14920 clauses to #pragma omp for instantiation if they are combined
14923 static tree
*omp_parallel_combined_clauses
;
14925 /* Substitute one OMP_FOR iterator. */
14928 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
14929 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
14930 tree args
, tsubst_flags_t complain
, tree in_decl
,
14931 bool integral_constant_expression_p
)
14933 #define RECUR(NODE) \
14934 tsubst_expr ((NODE), args, complain, in_decl, \
14935 integral_constant_expression_p)
14936 tree decl
, init
, cond
, incr
;
14938 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
14939 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
14941 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
14943 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
14944 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
14947 decl
= TREE_OPERAND (init
, 0);
14948 init
= TREE_OPERAND (init
, 1);
14949 tree decl_expr
= NULL_TREE
;
14950 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
14952 /* We need to jump through some hoops to handle declarations in the
14953 for-init-statement, since we might need to handle auto deduction,
14954 but we need to keep control of initialization. */
14956 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
14957 decl
= tsubst_decl (decl
, args
, complain
);
14961 if (TREE_CODE (decl
) == SCOPE_REF
)
14963 decl
= RECUR (decl
);
14964 if (TREE_CODE (decl
) == COMPONENT_REF
)
14968 switch (TREE_CODE (v
))
14970 case COMPONENT_REF
:
14974 case POINTER_PLUS_EXPR
:
14975 v
= TREE_OPERAND (v
, 0);
14978 if (DECL_CONTEXT (v
) == current_function_decl
14979 && DECL_ARTIFICIAL (v
)
14980 && DECL_NAME (v
) == this_identifier
)
14982 decl
= TREE_OPERAND (decl
, 1);
14983 decl
= omp_privatize_field (decl
, false);
14993 decl
= RECUR (decl
);
14995 init
= RECUR (init
);
14997 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
14998 if (auto_node
&& init
)
15000 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15002 gcc_assert (!type_dependent_expression_p (decl
));
15004 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15008 /* Declare the variable, but don't let that initialize it. */
15009 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15010 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15012 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15015 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15016 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15017 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15019 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15020 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15021 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15022 NOP_EXPR
, rhs
, complain
);
15025 incr
= RECUR (incr
);
15026 TREE_VEC_ELT (declv
, i
) = decl
;
15027 TREE_VEC_ELT (initv
, i
) = init
;
15028 TREE_VEC_ELT (condv
, i
) = cond
;
15029 TREE_VEC_ELT (incrv
, i
) = incr
;
15035 /* Declare and initialize the variable. */
15043 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15045 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15047 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15048 && OMP_CLAUSE_DECL (*pc
) == decl
)
15050 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15051 && OMP_CLAUSE_DECL (*pc
) == decl
)
15055 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15057 *pc
= OMP_CLAUSE_CHAIN (c
);
15058 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15061 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15062 && OMP_CLAUSE_DECL (*pc
) == decl
)
15064 error ("iteration variable %qD should not be firstprivate",
15066 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15068 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15069 && OMP_CLAUSE_DECL (*pc
) == decl
)
15071 error ("iteration variable %qD should not be reduction",
15073 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15076 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15081 if (*pc
== NULL_TREE
)
15083 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15084 OMP_CLAUSE_DECL (c
) = decl
;
15085 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15088 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15093 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15094 if (COMPARISON_CLASS_P (cond
))
15096 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15097 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15098 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15101 cond
= RECUR (cond
);
15102 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15103 switch (TREE_CODE (incr
))
15105 case PREINCREMENT_EXPR
:
15106 case PREDECREMENT_EXPR
:
15107 case POSTINCREMENT_EXPR
:
15108 case POSTDECREMENT_EXPR
:
15109 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15110 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15113 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15114 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15116 tree rhs
= TREE_OPERAND (incr
, 1);
15117 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15118 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15119 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15120 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15121 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15125 incr
= RECUR (incr
);
15128 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15129 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15131 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15132 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15133 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15134 TREE_TYPE (decl
), lhs
,
15135 RECUR (TREE_OPERAND (incr
, 2))));
15137 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15138 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15139 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15141 tree rhs
= TREE_OPERAND (incr
, 2);
15142 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15143 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15144 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15145 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15146 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15150 incr
= RECUR (incr
);
15153 incr
= RECUR (incr
);
15157 TREE_VEC_ELT (declv
, i
) = decl
;
15158 TREE_VEC_ELT (initv
, i
) = init
;
15159 TREE_VEC_ELT (condv
, i
) = cond
;
15160 TREE_VEC_ELT (incrv
, i
) = incr
;
15164 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15165 of OMP_TARGET's body. */
15168 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15170 *walk_subtrees
= 0;
15171 switch (TREE_CODE (*tp
))
15176 case STATEMENT_LIST
:
15177 *walk_subtrees
= 1;
15185 /* Like tsubst_copy for expressions, etc. but also does semantic
15189 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15190 bool integral_constant_expression_p
)
15192 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15193 #define RECUR(NODE) \
15194 tsubst_expr ((NODE), args, complain, in_decl, \
15195 integral_constant_expression_p)
15201 if (t
== NULL_TREE
|| t
== error_mark_node
)
15204 loc
= input_location
;
15205 if (EXPR_HAS_LOCATION (t
))
15206 input_location
= EXPR_LOCATION (t
);
15207 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15208 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15210 switch (TREE_CODE (t
))
15212 case STATEMENT_LIST
:
15214 tree_stmt_iterator i
;
15215 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15216 RECUR (tsi_stmt (i
));
15220 case CTOR_INITIALIZER
:
15221 finish_mem_initializers (tsubst_initializer_list
15222 (TREE_OPERAND (t
, 0), args
));
15226 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
15230 tmp
= RECUR (EXPR_STMT_EXPR (t
));
15231 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
15232 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
15234 finish_expr_stmt (tmp
);
15238 do_using_directive (USING_STMT_NAMESPACE (t
));
15243 tree decl
, pattern_decl
;
15246 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
15247 if (TREE_CODE (decl
) == LABEL_DECL
)
15248 finish_label_decl (DECL_NAME (decl
));
15249 else if (TREE_CODE (decl
) == USING_DECL
)
15251 tree scope
= USING_DECL_SCOPE (decl
);
15252 tree name
= DECL_NAME (decl
);
15254 scope
= tsubst (scope
, args
, complain
, in_decl
);
15255 decl
= lookup_qualified_name (scope
, name
,
15256 /*is_type_p=*/false,
15257 /*complain=*/false);
15258 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15259 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15261 do_local_using_decl (decl
, scope
, name
);
15263 else if (DECL_PACK_P (decl
))
15265 /* Don't build up decls for a variadic capture proxy, we'll
15266 instantiate the elements directly as needed. */
15271 init
= DECL_INITIAL (decl
);
15272 decl
= tsubst (decl
, args
, complain
, in_decl
);
15273 if (decl
!= error_mark_node
)
15275 /* By marking the declaration as instantiated, we avoid
15276 trying to instantiate it. Since instantiate_decl can't
15277 handle local variables, and since we've already done
15278 all that needs to be done, that's the right thing to
15281 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15283 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15284 /* Anonymous aggregates are a special case. */
15285 finish_anon_union (decl
);
15286 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15288 DECL_CONTEXT (decl
) = current_function_decl
;
15289 if (DECL_NAME (decl
) == this_identifier
)
15291 tree lam
= DECL_CONTEXT (current_function_decl
);
15292 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15293 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15295 insert_capture_proxy (decl
);
15297 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15298 /* We already did a pushtag. */;
15299 else if (TREE_CODE (decl
) == FUNCTION_DECL
15300 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15301 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15303 DECL_CONTEXT (decl
) = NULL_TREE
;
15305 DECL_CONTEXT (decl
) = current_function_decl
;
15306 cp_check_omp_declare_reduction (decl
);
15310 int const_init
= false;
15311 maybe_push_decl (decl
);
15313 && DECL_PRETTY_FUNCTION_P (decl
))
15315 /* For __PRETTY_FUNCTION__ we have to adjust the
15317 const char *const name
15318 = cxx_printable_name (current_function_decl
, 2);
15319 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15322 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15325 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15327 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15336 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15337 RECUR (FOR_INIT_STMT (t
));
15338 finish_for_init_stmt (stmt
);
15339 tmp
= RECUR (FOR_COND (t
));
15340 finish_for_cond (tmp
, stmt
, false);
15341 tmp
= RECUR (FOR_EXPR (t
));
15342 finish_for_expr (tmp
, stmt
);
15343 RECUR (FOR_BODY (t
));
15344 finish_for_stmt (stmt
);
15347 case RANGE_FOR_STMT
:
15350 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15351 decl
= RANGE_FOR_DECL (t
);
15352 decl
= tsubst (decl
, args
, complain
, in_decl
);
15353 maybe_push_decl (decl
);
15354 expr
= RECUR (RANGE_FOR_EXPR (t
));
15355 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
15356 RECUR (RANGE_FOR_BODY (t
));
15357 finish_for_stmt (stmt
);
15362 stmt
= begin_while_stmt ();
15363 tmp
= RECUR (WHILE_COND (t
));
15364 finish_while_stmt_cond (tmp
, stmt
, false);
15365 RECUR (WHILE_BODY (t
));
15366 finish_while_stmt (stmt
);
15370 stmt
= begin_do_stmt ();
15371 RECUR (DO_BODY (t
));
15372 finish_do_body (stmt
);
15373 tmp
= RECUR (DO_COND (t
));
15374 finish_do_stmt (tmp
, stmt
, false);
15378 stmt
= begin_if_stmt ();
15379 tmp
= RECUR (IF_COND (t
));
15380 finish_if_stmt_cond (tmp
, stmt
);
15381 RECUR (THEN_CLAUSE (t
));
15382 finish_then_clause (stmt
);
15384 if (ELSE_CLAUSE (t
))
15386 begin_else_clause (stmt
);
15387 RECUR (ELSE_CLAUSE (t
));
15388 finish_else_clause (stmt
);
15391 finish_if_stmt (stmt
);
15395 if (BIND_EXPR_BODY_BLOCK (t
))
15396 stmt
= begin_function_body ();
15398 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
15399 ? BCS_TRY_BLOCK
: 0);
15401 RECUR (BIND_EXPR_BODY (t
));
15403 if (BIND_EXPR_BODY_BLOCK (t
))
15404 finish_function_body (stmt
);
15406 finish_compound_stmt (stmt
);
15410 finish_break_stmt ();
15413 case CONTINUE_STMT
:
15414 finish_continue_stmt ();
15418 stmt
= begin_switch_stmt ();
15419 tmp
= RECUR (SWITCH_STMT_COND (t
));
15420 finish_switch_cond (tmp
, stmt
);
15421 RECUR (SWITCH_STMT_BODY (t
));
15422 finish_switch_stmt (stmt
);
15425 case CASE_LABEL_EXPR
:
15427 tree low
= RECUR (CASE_LOW (t
));
15428 tree high
= RECUR (CASE_HIGH (t
));
15429 finish_case_label (EXPR_LOCATION (t
), low
, high
);
15435 tree decl
= LABEL_EXPR_LABEL (t
);
15438 label
= finish_label_stmt (DECL_NAME (decl
));
15439 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
15440 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
15445 tmp
= GOTO_DESTINATION (t
);
15446 if (TREE_CODE (tmp
) != LABEL_DECL
)
15447 /* Computed goto's must be tsubst'd into. On the other hand,
15448 non-computed gotos must not be; the identifier in question
15449 will have no binding. */
15452 tmp
= DECL_NAME (tmp
);
15453 finish_goto_stmt (tmp
);
15458 tree string
= RECUR (ASM_STRING (t
));
15459 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
15460 complain
, in_decl
);
15461 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
15462 complain
, in_decl
);
15463 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
15464 complain
, in_decl
);
15465 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
15466 complain
, in_decl
);
15467 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
15469 tree asm_expr
= tmp
;
15470 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
15471 asm_expr
= TREE_OPERAND (asm_expr
, 0);
15472 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
15479 stmt
= begin_try_block ();
15480 RECUR (TRY_STMTS (t
));
15481 finish_cleanup_try_block (stmt
);
15482 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
15486 tree compound_stmt
= NULL_TREE
;
15488 if (FN_TRY_BLOCK_P (t
))
15489 stmt
= begin_function_try_block (&compound_stmt
);
15491 stmt
= begin_try_block ();
15493 RECUR (TRY_STMTS (t
));
15495 if (FN_TRY_BLOCK_P (t
))
15496 finish_function_try_block (stmt
);
15498 finish_try_block (stmt
);
15500 RECUR (TRY_HANDLERS (t
));
15501 if (FN_TRY_BLOCK_P (t
))
15502 finish_function_handler_sequence (stmt
, compound_stmt
);
15504 finish_handler_sequence (stmt
);
15510 tree decl
= HANDLER_PARMS (t
);
15514 decl
= tsubst (decl
, args
, complain
, in_decl
);
15515 /* Prevent instantiate_decl from trying to instantiate
15516 this variable. We've already done all that needs to be
15518 if (decl
!= error_mark_node
)
15519 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15521 stmt
= begin_handler ();
15522 finish_handler_parms (decl
, stmt
);
15523 RECUR (HANDLER_BODY (t
));
15524 finish_handler (stmt
);
15529 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
15530 if (CLASS_TYPE_P (tmp
))
15532 /* Local classes are not independent templates; they are
15533 instantiated along with their containing function. And this
15534 way we don't have to deal with pushing out of one local class
15535 to instantiate a member of another local class. */
15537 /* Closures are handled by the LAMBDA_EXPR. */
15538 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
15539 complete_type (tmp
);
15540 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
15541 if (!DECL_ARTIFICIAL (fn
))
15542 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
15546 case STATIC_ASSERT
:
15550 ++c_inhibit_evaluation_warnings
;
15552 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
15555 /*integral_constant_expression_p=*/true);
15556 --c_inhibit_evaluation_warnings
;
15558 finish_static_assert (condition
,
15559 STATIC_ASSERT_MESSAGE (t
),
15560 STATIC_ASSERT_SOURCE_LOCATION (t
),
15561 /*member_p=*/false);
15566 case OACC_PARALLEL
:
15567 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
15569 stmt
= begin_omp_parallel ();
15570 RECUR (OMP_BODY (t
));
15571 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
15575 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
15576 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
15577 complain
, in_decl
);
15578 if (OMP_PARALLEL_COMBINED (t
))
15579 omp_parallel_combined_clauses
= &tmp
;
15580 stmt
= begin_omp_parallel ();
15581 RECUR (OMP_PARALLEL_BODY (t
));
15582 gcc_assert (omp_parallel_combined_clauses
== NULL
);
15583 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
15584 = OMP_PARALLEL_COMBINED (t
);
15585 pop_omp_privatization_clauses (r
);
15589 r
= push_omp_privatization_clauses (false);
15590 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
15591 complain
, in_decl
);
15592 stmt
= begin_omp_task ();
15593 RECUR (OMP_TASK_BODY (t
));
15594 finish_omp_task (tmp
, stmt
);
15595 pop_omp_privatization_clauses (r
);
15602 case OMP_DISTRIBUTE
:
15606 tree clauses
, body
, pre_body
;
15607 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
15608 tree orig_declv
= NULL_TREE
;
15609 tree incrv
= NULL_TREE
;
15610 enum c_omp_region_type ort
= C_ORT_OMP
;
15613 if (TREE_CODE (t
) == CILK_SIMD
|| TREE_CODE (t
) == CILK_FOR
)
15615 else if (TREE_CODE (t
) == OACC_LOOP
)
15618 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
15619 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
15621 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15623 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15624 if (OMP_FOR_ORIG_DECLS (t
))
15625 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15626 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15627 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15628 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15631 stmt
= begin_omp_structured_block ();
15633 pre_body
= push_stmt_list ();
15634 RECUR (OMP_FOR_PRE_BODY (t
));
15635 pre_body
= pop_stmt_list (pre_body
);
15637 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15638 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
15639 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
15640 incrv
, &clauses
, args
, complain
, in_decl
,
15641 integral_constant_expression_p
);
15642 omp_parallel_combined_clauses
= NULL
;
15644 body
= push_stmt_list ();
15645 RECUR (OMP_FOR_BODY (t
));
15646 body
= pop_stmt_list (body
);
15648 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15649 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
15650 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
15654 t
= make_node (TREE_CODE (t
));
15655 TREE_TYPE (t
) = void_type_node
;
15656 OMP_FOR_BODY (t
) = body
;
15657 OMP_FOR_PRE_BODY (t
) = pre_body
;
15658 OMP_FOR_CLAUSES (t
) = clauses
;
15659 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
15663 add_stmt (finish_omp_structured_block (stmt
));
15664 pop_omp_privatization_clauses (r
);
15669 omp_parallel_combined_clauses
= NULL
;
15674 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
15675 && OMP_TEAMS_COMBINED (t
));
15676 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
15678 stmt
= push_stmt_list ();
15679 RECUR (OMP_BODY (t
));
15680 stmt
= pop_stmt_list (stmt
);
15683 OMP_BODY (t
) = stmt
;
15684 OMP_CLAUSES (t
) = tmp
;
15686 pop_omp_privatization_clauses (r
);
15690 case OMP_TARGET_DATA
:
15692 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
15693 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
15695 keep_next_level (true);
15696 stmt
= begin_omp_structured_block ();
15698 RECUR (OMP_BODY (t
));
15699 stmt
= finish_omp_structured_block (stmt
);
15702 OMP_BODY (t
) = stmt
;
15703 OMP_CLAUSES (t
) = tmp
;
15704 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
15706 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
15709 /* For combined target teams, ensure the num_teams and
15710 thread_limit clause expressions are evaluated on the host,
15711 before entering the target construct. */
15713 for (c
= OMP_TEAMS_CLAUSES (teams
);
15714 c
; c
= OMP_CLAUSE_CHAIN (c
))
15715 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
15716 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
15717 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
15719 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
15720 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
15721 if (expr
== error_mark_node
)
15723 tmp
= TARGET_EXPR_SLOT (expr
);
15725 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
15726 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
15727 OMP_CLAUSE_FIRSTPRIVATE
);
15728 OMP_CLAUSE_DECL (tc
) = tmp
;
15729 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
15730 OMP_TARGET_CLAUSES (t
) = tc
;
15739 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
15740 complain
, in_decl
);
15741 OACC_DECLARE_CLAUSES (t
) = tmp
;
15745 case OMP_TARGET_UPDATE
:
15746 case OMP_TARGET_ENTER_DATA
:
15747 case OMP_TARGET_EXIT_DATA
:
15748 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
15749 complain
, in_decl
);
15751 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15755 case OACC_ENTER_DATA
:
15756 case OACC_EXIT_DATA
:
15758 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
15759 complain
, in_decl
);
15761 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15766 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
15767 complain
, in_decl
);
15768 stmt
= push_stmt_list ();
15769 RECUR (OMP_BODY (t
));
15770 stmt
= pop_stmt_list (stmt
);
15773 OMP_BODY (t
) = stmt
;
15774 OMP_ORDERED_CLAUSES (t
) = tmp
;
15780 case OMP_TASKGROUP
:
15781 stmt
= push_stmt_list ();
15782 RECUR (OMP_BODY (t
));
15783 stmt
= pop_stmt_list (stmt
);
15786 OMP_BODY (t
) = stmt
;
15791 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
15792 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
15794 tree op1
= TREE_OPERAND (t
, 1);
15795 tree rhs1
= NULL_TREE
;
15797 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
15799 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
15800 op1
= TREE_OPERAND (op1
, 1);
15802 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15803 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15804 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
15805 NULL_TREE
, NULL_TREE
, rhs1
,
15806 OMP_ATOMIC_SEQ_CST (t
));
15810 tree op1
= TREE_OPERAND (t
, 1);
15811 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
15812 tree rhs1
= NULL_TREE
;
15813 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
15814 enum tree_code opcode
= NOP_EXPR
;
15815 if (code
== OMP_ATOMIC_READ
)
15817 v
= RECUR (TREE_OPERAND (op1
, 0));
15818 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15820 else if (code
== OMP_ATOMIC_CAPTURE_OLD
15821 || code
== OMP_ATOMIC_CAPTURE_NEW
)
15823 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
15824 v
= RECUR (TREE_OPERAND (op1
, 0));
15825 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
15826 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
15828 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
15829 op11
= TREE_OPERAND (op11
, 1);
15831 lhs
= RECUR (TREE_OPERAND (op11
, 0));
15832 rhs
= RECUR (TREE_OPERAND (op11
, 1));
15833 opcode
= TREE_CODE (op11
);
15834 if (opcode
== MODIFY_EXPR
)
15840 lhs
= RECUR (TREE_OPERAND (op1
, 0));
15841 rhs
= RECUR (TREE_OPERAND (op1
, 1));
15843 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
15844 OMP_ATOMIC_SEQ_CST (t
));
15848 case TRANSACTION_EXPR
:
15851 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
15852 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
15854 if (TRANSACTION_EXPR_IS_STMT (t
))
15856 tree body
= TRANSACTION_EXPR_BODY (t
);
15857 tree noex
= NULL_TREE
;
15858 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
15860 noex
= MUST_NOT_THROW_COND (body
);
15861 if (noex
== NULL_TREE
)
15862 noex
= boolean_true_node
;
15863 body
= TREE_OPERAND (body
, 0);
15865 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
15867 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
15871 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
15872 RECUR (TRANSACTION_EXPR_BODY (t
)),
15879 case MUST_NOT_THROW_EXPR
:
15881 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15882 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
15883 RETURN (build_must_not_throw_expr (op0
, cond
));
15886 case EXPR_PACK_EXPANSION
:
15887 error ("invalid use of pack expansion expression");
15888 RETURN (error_mark_node
);
15890 case NONTYPE_ARGUMENT_PACK
:
15891 error ("use %<...%> to expand argument pack");
15892 RETURN (error_mark_node
);
15894 case CILK_SPAWN_STMT
:
15895 cfun
->calls_cilk_spawn
= 1;
15896 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
15898 case CILK_SYNC_STMT
:
15899 RETURN (build_cilk_sync ());
15901 case COMPOUND_EXPR
:
15902 tmp
= RECUR (TREE_OPERAND (t
, 0));
15903 if (tmp
== NULL_TREE
)
15904 /* If the first operand was a statement, we're done with it. */
15905 RETURN (RECUR (TREE_OPERAND (t
, 1)));
15906 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
15907 RECUR (TREE_OPERAND (t
, 1)),
15910 case ANNOTATE_EXPR
:
15911 tmp
= RECUR (TREE_OPERAND (t
, 0));
15912 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
15913 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
15916 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
15918 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15919 /*function_p=*/false,
15920 integral_constant_expression_p
));
15923 RETURN (NULL_TREE
);
15925 input_location
= loc
;
15931 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15932 function. For description of the body see comment above
15933 cp_parser_omp_declare_reduction_exprs. */
15936 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15938 if (t
== NULL_TREE
|| t
== error_mark_node
)
15941 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
15943 tree_stmt_iterator tsi
;
15946 memset (stmts
, 0, sizeof stmts
);
15947 for (i
= 0, tsi
= tsi_start (t
);
15948 i
< 7 && !tsi_end_p (tsi
);
15949 i
++, tsi_next (&tsi
))
15950 stmts
[i
] = tsi_stmt (tsi
);
15951 gcc_assert (tsi_end_p (tsi
));
15955 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
15956 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
15957 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
15958 args
, complain
, in_decl
);
15959 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
15960 args
, complain
, in_decl
);
15961 DECL_CONTEXT (omp_out
) = current_function_decl
;
15962 DECL_CONTEXT (omp_in
) = current_function_decl
;
15963 keep_next_level (true);
15964 tree block
= begin_omp_structured_block ();
15965 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
15966 block
= finish_omp_structured_block (block
);
15967 block
= maybe_cleanup_point_expr_void (block
);
15968 add_decl_expr (omp_out
);
15969 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
15970 TREE_NO_WARNING (omp_out
) = 1;
15971 add_decl_expr (omp_in
);
15972 finish_expr_stmt (block
);
15976 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
15977 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
15978 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
15979 args
, complain
, in_decl
);
15980 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
15981 args
, complain
, in_decl
);
15982 DECL_CONTEXT (omp_priv
) = current_function_decl
;
15983 DECL_CONTEXT (omp_orig
) = current_function_decl
;
15984 keep_next_level (true);
15985 tree block
= begin_omp_structured_block ();
15986 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
15987 block
= finish_omp_structured_block (block
);
15988 block
= maybe_cleanup_point_expr_void (block
);
15989 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
15990 add_decl_expr (omp_priv
);
15991 add_decl_expr (omp_orig
);
15992 finish_expr_stmt (block
);
15994 add_decl_expr (omp_orig
);
15998 /* T is a postfix-expression that is not being used in a function
15999 call. Return the substituted version of T. */
16002 tsubst_non_call_postfix_expression (tree t
, tree args
,
16003 tsubst_flags_t complain
,
16006 if (TREE_CODE (t
) == SCOPE_REF
)
16007 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16008 /*done=*/false, /*address_p=*/false);
16010 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16011 /*function_p=*/false,
16012 /*integral_constant_expression_p=*/false);
16017 /* Like tsubst but deals with expressions and performs semantic
16018 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16021 tsubst_copy_and_build (tree t
,
16023 tsubst_flags_t complain
,
16026 bool integral_constant_expression_p
)
16028 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16029 #define RECUR(NODE) \
16030 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16031 /*function_p=*/false, \
16032 integral_constant_expression_p)
16037 if (t
== NULL_TREE
|| t
== error_mark_node
)
16040 loc
= input_location
;
16041 if (EXPR_HAS_LOCATION (t
))
16042 input_location
= EXPR_LOCATION (t
);
16044 /* N3276 decltype magic only applies to calls at the top level or on the
16045 right side of a comma. */
16046 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
16047 complain
&= ~tf_decltype
;
16049 switch (TREE_CODE (t
))
16053 /* Fall through. */
16054 case IDENTIFIER_NODE
:
16058 bool non_integral_constant_expression_p
;
16059 const char *error_msg
;
16061 if (IDENTIFIER_TYPENAME_P (t
))
16063 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16064 t
= mangle_conv_op_name_for_type (new_type
);
16067 /* Look up the name. */
16068 decl
= lookup_name (t
);
16070 /* By convention, expressions use ERROR_MARK_NODE to indicate
16071 failure, not NULL_TREE. */
16072 if (decl
== NULL_TREE
)
16073 decl
= error_mark_node
;
16075 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
16077 integral_constant_expression_p
,
16078 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
16079 &non_integral_constant_expression_p
,
16080 /*template_p=*/false,
16082 /*address_p=*/false,
16083 /*template_arg_p=*/false,
16088 if (!function_p
&& identifier_p (decl
))
16090 if (complain
& tf_error
)
16091 unqualified_name_lookup_error (decl
);
16092 decl
= error_mark_node
;
16097 case TEMPLATE_ID_EXPR
:
16100 tree templ
= RECUR (TREE_OPERAND (t
, 0));
16101 tree targs
= TREE_OPERAND (t
, 1);
16104 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16105 if (targs
== error_mark_node
)
16106 return error_mark_node
;
16108 if (variable_template_p (templ
))
16109 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
16111 if (TREE_CODE (templ
) == COMPONENT_REF
)
16113 object
= TREE_OPERAND (templ
, 0);
16114 templ
= TREE_OPERAND (templ
, 1);
16117 object
= NULL_TREE
;
16118 templ
= lookup_template_function (templ
, targs
);
16121 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
16122 object
, templ
, NULL_TREE
));
16124 RETURN (baselink_for_fns (templ
));
16129 tree r
= RECUR (TREE_OPERAND (t
, 0));
16131 if (REFERENCE_REF_P (t
))
16133 /* A type conversion to reference type will be enclosed in
16134 such an indirect ref, but the substitution of the cast
16135 will have also added such an indirect ref. */
16136 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
16137 r
= convert_from_reference (r
);
16140 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
16141 complain
|decltype_flag
);
16143 if (TREE_CODE (r
) == INDIRECT_REF
)
16144 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16151 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16152 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16153 RETURN (build_nop (type
, op0
));
16156 case IMPLICIT_CONV_EXPR
:
16158 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16159 tree expr
= RECUR (TREE_OPERAND (t
, 0));
16160 int flags
= LOOKUP_IMPLICIT
;
16161 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
16162 flags
= LOOKUP_NORMAL
;
16163 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
16169 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16170 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16171 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
16175 case REINTERPRET_CAST_EXPR
:
16176 case CONST_CAST_EXPR
:
16177 case DYNAMIC_CAST_EXPR
:
16178 case STATIC_CAST_EXPR
:
16181 tree op
, r
= NULL_TREE
;
16183 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16184 if (integral_constant_expression_p
16185 && !cast_valid_in_integral_constant_expression_p (type
))
16187 if (complain
& tf_error
)
16188 error ("a cast to a type other than an integral or "
16189 "enumeration type cannot appear in a constant-expression");
16190 RETURN (error_mark_node
);
16193 op
= RECUR (TREE_OPERAND (t
, 0));
16195 warning_sentinel
s(warn_useless_cast
);
16196 switch (TREE_CODE (t
))
16199 r
= build_functional_cast (type
, op
, complain
);
16201 case REINTERPRET_CAST_EXPR
:
16202 r
= build_reinterpret_cast (type
, op
, complain
);
16204 case CONST_CAST_EXPR
:
16205 r
= build_const_cast (type
, op
, complain
);
16207 case DYNAMIC_CAST_EXPR
:
16208 r
= build_dynamic_cast (type
, op
, complain
);
16210 case STATIC_CAST_EXPR
:
16211 r
= build_static_cast (type
, op
, complain
);
16214 gcc_unreachable ();
16220 case POSTDECREMENT_EXPR
:
16221 case POSTINCREMENT_EXPR
:
16222 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16223 args
, complain
, in_decl
);
16224 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
16225 complain
|decltype_flag
));
16227 case PREDECREMENT_EXPR
:
16228 case PREINCREMENT_EXPR
:
16232 case TRUTH_NOT_EXPR
:
16233 case UNARY_PLUS_EXPR
: /* Unary + */
16234 case REALPART_EXPR
:
16235 case IMAGPART_EXPR
:
16236 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
16237 RECUR (TREE_OPERAND (t
, 0)),
16238 complain
|decltype_flag
));
16240 case FIX_TRUNC_EXPR
:
16241 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
16245 op1
= TREE_OPERAND (t
, 0);
16246 if (TREE_CODE (op1
) == LABEL_DECL
)
16247 RETURN (finish_label_address_expr (DECL_NAME (op1
),
16248 EXPR_LOCATION (op1
)));
16249 if (TREE_CODE (op1
) == SCOPE_REF
)
16250 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
16251 /*done=*/true, /*address_p=*/true);
16253 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
16255 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16256 complain
|decltype_flag
));
16261 case TRUNC_DIV_EXPR
:
16262 case CEIL_DIV_EXPR
:
16263 case FLOOR_DIV_EXPR
:
16264 case ROUND_DIV_EXPR
:
16265 case EXACT_DIV_EXPR
:
16269 case TRUNC_MOD_EXPR
:
16270 case FLOOR_MOD_EXPR
:
16271 case TRUTH_ANDIF_EXPR
:
16272 case TRUTH_ORIF_EXPR
:
16273 case TRUTH_AND_EXPR
:
16274 case TRUTH_OR_EXPR
:
16290 warning_sentinel
s1(warn_type_limits
);
16291 warning_sentinel
s2(warn_div_by_zero
);
16292 warning_sentinel
s3(warn_logical_op
);
16293 warning_sentinel
s4(warn_tautological_compare
);
16294 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16295 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16296 tree r
= build_x_binary_op
16297 (input_location
, TREE_CODE (t
),
16299 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16301 : TREE_CODE (TREE_OPERAND (t
, 0))),
16303 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16305 : TREE_CODE (TREE_OPERAND (t
, 1))),
16307 complain
|decltype_flag
);
16308 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16309 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16314 case POINTER_PLUS_EXPR
:
16316 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16317 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16318 return fold_build_pointer_plus (op0
, op1
);
16322 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16323 /*address_p=*/false));
16325 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16326 args
, complain
, in_decl
);
16327 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16328 RECUR (TREE_OPERAND (t
, 1)),
16329 complain
|decltype_flag
));
16331 case ARRAY_NOTATION_REF
:
16333 tree start_index
, length
, stride
;
16334 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16335 args
, complain
, in_decl
);
16336 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16337 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16338 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16339 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16340 length
, stride
, TREE_TYPE (op1
)));
16343 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16344 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16345 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16352 op1
= TREE_OPERAND (t
, 0);
16353 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16354 op1
= TREE_TYPE (op1
);
16357 /* When there are no ARGS, we are trying to evaluate a
16358 non-dependent expression from the parser. Trying to do
16359 the substitutions may not work. */
16361 op1
= TREE_TYPE (op1
);
16365 ++cp_unevaluated_operand
;
16366 ++c_inhibit_evaluation_warnings
;
16368 op1
= tsubst (op1
, args
, complain
, in_decl
);
16370 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16371 /*function_p=*/false,
16372 /*integral_constant_expression_p=*/
16374 --cp_unevaluated_operand
;
16375 --c_inhibit_evaluation_warnings
;
16378 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16379 complain
& tf_error
);
16381 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
16382 complain
& tf_error
);
16383 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
16385 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
16387 if (!processing_template_decl
&& TYPE_P (op1
))
16389 r
= build_min (SIZEOF_EXPR
, size_type_node
,
16390 build1 (NOP_EXPR
, op1
, error_mark_node
));
16391 SIZEOF_EXPR_TYPE_P (r
) = 1;
16394 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
16395 TREE_SIDE_EFFECTS (r
) = 0;
16396 TREE_READONLY (r
) = 1;
16398 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16403 case AT_ENCODE_EXPR
:
16405 op1
= TREE_OPERAND (t
, 0);
16406 ++cp_unevaluated_operand
;
16407 ++c_inhibit_evaluation_warnings
;
16408 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16409 /*function_p=*/false,
16410 /*integral_constant_expression_p=*/false);
16411 --cp_unevaluated_operand
;
16412 --c_inhibit_evaluation_warnings
;
16413 RETURN (objc_build_encode_expr (op1
));
16416 case NOEXCEPT_EXPR
:
16417 op1
= TREE_OPERAND (t
, 0);
16418 ++cp_unevaluated_operand
;
16419 ++c_inhibit_evaluation_warnings
;
16420 ++cp_noexcept_operand
;
16421 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16422 /*function_p=*/false,
16423 /*integral_constant_expression_p=*/false);
16424 --cp_unevaluated_operand
;
16425 --c_inhibit_evaluation_warnings
;
16426 --cp_noexcept_operand
;
16427 RETURN (finish_noexcept_expr (op1
, complain
));
16431 warning_sentinel
s(warn_div_by_zero
);
16432 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
16433 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
16434 tree r
= build_x_modify_expr
16435 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
16436 complain
|decltype_flag
);
16437 /* TREE_NO_WARNING must be set if either the expression was
16438 parenthesized or it uses an operator such as >>= rather
16439 than plain assignment. In the former case, it was already
16440 set and must be copied. In the latter case,
16441 build_x_modify_expr sets it and it must not be reset
16443 if (TREE_NO_WARNING (t
))
16444 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16450 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16451 args
, complain
, in_decl
);
16452 /* Remember that there was a reference to this entity. */
16454 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
16455 RETURN (error_mark_node
);
16456 RETURN (build_x_arrow (input_location
, op1
, complain
));
16460 tree placement
= RECUR (TREE_OPERAND (t
, 0));
16461 tree init
= RECUR (TREE_OPERAND (t
, 3));
16462 vec
<tree
, va_gc
> *placement_vec
;
16463 vec
<tree
, va_gc
> *init_vec
;
16466 if (placement
== NULL_TREE
)
16467 placement_vec
= NULL
;
16470 placement_vec
= make_tree_vector ();
16471 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
16472 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
16475 /* If there was an initializer in the original tree, but it
16476 instantiated to an empty list, then we should pass a
16477 non-NULL empty vector to tell build_new that it was an
16478 empty initializer() rather than no initializer. This can
16479 only happen when the initializer is a pack expansion whose
16480 parameter packs are of length zero. */
16481 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
16485 init_vec
= make_tree_vector ();
16486 if (init
== void_node
)
16487 gcc_assert (init_vec
!= NULL
);
16490 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
16491 vec_safe_push (init_vec
, TREE_VALUE (init
));
16495 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16496 tree op2
= RECUR (TREE_OPERAND (t
, 2));
16497 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
16498 NEW_EXPR_USE_GLOBAL (t
),
16501 if (placement_vec
!= NULL
)
16502 release_tree_vector (placement_vec
);
16503 if (init_vec
!= NULL
)
16504 release_tree_vector (init_vec
);
16511 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16512 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16513 RETURN (delete_sanity (op0
, op1
,
16514 DELETE_EXPR_USE_VEC (t
),
16515 DELETE_EXPR_USE_GLOBAL (t
),
16519 case COMPOUND_EXPR
:
16521 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
16522 complain
& ~tf_decltype
, in_decl
,
16523 /*function_p=*/false,
16524 integral_constant_expression_p
);
16525 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
16527 RECUR (TREE_OPERAND (t
, 1)),
16528 complain
|decltype_flag
));
16534 vec
<tree
, va_gc
> *call_args
;
16535 unsigned int nargs
, i
;
16540 function
= CALL_EXPR_FN (t
);
16541 /* When we parsed the expression, we determined whether or
16542 not Koenig lookup should be performed. */
16543 koenig_p
= KOENIG_LOOKUP_P (t
);
16544 if (TREE_CODE (function
) == SCOPE_REF
)
16546 qualified_p
= true;
16547 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
16549 /*address_p=*/false);
16551 else if (koenig_p
&& identifier_p (function
))
16553 /* Do nothing; calling tsubst_copy_and_build on an identifier
16554 would incorrectly perform unqualified lookup again.
16556 Note that we can also have an IDENTIFIER_NODE if the earlier
16557 unqualified lookup found a member function; in that case
16558 koenig_p will be false and we do want to do the lookup
16559 again to find the instantiated member function.
16561 FIXME but doing that causes c++/15272, so we need to stop
16562 using IDENTIFIER_NODE in that situation. */
16563 qualified_p
= false;
16567 if (TREE_CODE (function
) == COMPONENT_REF
)
16569 tree op
= TREE_OPERAND (function
, 1);
16571 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
16572 || (BASELINK_P (op
)
16573 && BASELINK_QUALIFIED_P (op
)));
16576 qualified_p
= false;
16578 if (TREE_CODE (function
) == ADDR_EXPR
16579 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
16580 /* Avoid error about taking the address of a constructor. */
16581 function
= TREE_OPERAND (function
, 0);
16583 function
= tsubst_copy_and_build (function
, args
, complain
,
16586 integral_constant_expression_p
);
16588 if (BASELINK_P (function
))
16589 qualified_p
= true;
16592 nargs
= call_expr_nargs (t
);
16593 call_args
= make_tree_vector ();
16594 for (i
= 0; i
< nargs
; ++i
)
16596 tree arg
= CALL_EXPR_ARG (t
, i
);
16598 if (!PACK_EXPANSION_P (arg
))
16599 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
16602 /* Expand the pack expansion and push each entry onto
16604 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
16605 if (TREE_CODE (arg
) == TREE_VEC
)
16607 unsigned int len
, j
;
16609 len
= TREE_VEC_LENGTH (arg
);
16610 for (j
= 0; j
< len
; ++j
)
16612 tree value
= TREE_VEC_ELT (arg
, j
);
16613 if (value
!= NULL_TREE
)
16614 value
= convert_from_reference (value
);
16615 vec_safe_push (call_args
, value
);
16620 /* A partial substitution. Add one entry. */
16621 vec_safe_push (call_args
, arg
);
16626 /* We do not perform argument-dependent lookup if normal
16627 lookup finds a non-function, in accordance with the
16628 expected resolution of DR 218. */
16630 && ((is_overloaded_fn (function
)
16631 /* If lookup found a member function, the Koenig lookup is
16632 not appropriate, even if an unqualified-name was used
16633 to denote the function. */
16634 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
16635 || identifier_p (function
))
16636 /* Only do this when substitution turns a dependent call
16637 into a non-dependent call. */
16638 && type_dependent_expression_p_push (t
)
16639 && !any_type_dependent_arguments_p (call_args
))
16640 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
16642 if (identifier_p (function
)
16643 && !any_type_dependent_arguments_p (call_args
))
16645 if (koenig_p
&& (complain
& tf_warning_or_error
))
16647 /* For backwards compatibility and good diagnostics, try
16648 the unqualified lookup again if we aren't in SFINAE
16650 tree unq
= (tsubst_copy_and_build
16651 (function
, args
, complain
, in_decl
, true,
16652 integral_constant_expression_p
));
16653 if (unq
== error_mark_node
)
16654 RETURN (error_mark_node
);
16656 if (unq
!= function
)
16659 if (INDIRECT_REF_P (fn
))
16660 fn
= TREE_OPERAND (fn
, 0);
16661 if (TREE_CODE (fn
) == COMPONENT_REF
)
16662 fn
= TREE_OPERAND (fn
, 1);
16663 if (is_overloaded_fn (fn
))
16664 fn
= get_first_fn (fn
);
16665 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
16666 "%qD was not declared in this scope, "
16667 "and no declarations were found by "
16668 "argument-dependent lookup at the point "
16669 "of instantiation", function
))
16672 /* Can't say anything more. */;
16673 else if (DECL_CLASS_SCOPE_P (fn
))
16675 location_t loc
= EXPR_LOC_OR_LOC (t
,
16678 "declarations in dependent base %qT are "
16679 "not found by unqualified lookup",
16680 DECL_CLASS_CONTEXT (fn
));
16681 if (current_class_ptr
)
16683 "use %<this->%D%> instead", function
);
16686 "use %<%T::%D%> instead",
16687 current_class_name
, function
);
16690 inform (DECL_SOURCE_LOCATION (fn
),
16691 "%qD declared here, later in the "
16692 "translation unit", fn
);
16697 if (identifier_p (function
))
16699 if (complain
& tf_error
)
16700 unqualified_name_lookup_error (function
);
16701 release_tree_vector (call_args
);
16702 RETURN (error_mark_node
);
16706 /* Remember that there was a reference to this entity. */
16707 if (DECL_P (function
)
16708 && !mark_used (function
, complain
) && !(complain
& tf_error
))
16709 RETURN (error_mark_node
);
16711 /* Put back tf_decltype for the actual call. */
16712 complain
|= decltype_flag
;
16714 if (TREE_CODE (function
) == OFFSET_REF
)
16715 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
16717 else if (TREE_CODE (function
) == COMPONENT_REF
)
16719 tree instance
= TREE_OPERAND (function
, 0);
16720 tree fn
= TREE_OPERAND (function
, 1);
16722 if (processing_template_decl
16723 && (type_dependent_expression_p (instance
)
16724 || (!BASELINK_P (fn
)
16725 && TREE_CODE (fn
) != FIELD_DECL
)
16726 || type_dependent_expression_p (fn
)
16727 || any_type_dependent_arguments_p (call_args
)))
16728 ret
= build_nt_call_vec (function
, call_args
);
16729 else if (!BASELINK_P (fn
))
16730 ret
= finish_call_expr (function
, &call_args
,
16731 /*disallow_virtual=*/false,
16732 /*koenig_p=*/false,
16735 ret
= (build_new_method_call
16737 &call_args
, NULL_TREE
,
16738 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
16743 ret
= finish_call_expr (function
, &call_args
,
16744 /*disallow_virtual=*/qualified_p
,
16748 release_tree_vector (call_args
);
16750 if (ret
!= error_mark_node
)
16752 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
16753 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
16754 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
16755 if (op
|| ord
|| rev
)
16757 function
= extract_call_expr (ret
);
16758 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
16759 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
16760 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
16769 tree cond
= RECUR (TREE_OPERAND (t
, 0));
16770 tree folded_cond
= fold_non_dependent_expr (cond
);
16773 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
16775 if (integer_zerop (folded_cond
))
16777 ++c_inhibit_evaluation_warnings
;
16778 exp1
= RECUR (TREE_OPERAND (t
, 1));
16779 --c_inhibit_evaluation_warnings
;
16780 exp2
= RECUR (TREE_OPERAND (t
, 2));
16784 exp1
= RECUR (TREE_OPERAND (t
, 1));
16785 ++c_inhibit_evaluation_warnings
;
16786 exp2
= RECUR (TREE_OPERAND (t
, 2));
16787 --c_inhibit_evaluation_warnings
;
16789 cond
= folded_cond
;
16793 exp1
= RECUR (TREE_OPERAND (t
, 1));
16794 exp2
= RECUR (TREE_OPERAND (t
, 2));
16797 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
16798 cond
, exp1
, exp2
, complain
));
16801 case PSEUDO_DTOR_EXPR
:
16803 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16804 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16805 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16806 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
16812 tree purpose
, value
, chain
;
16814 if (t
== void_list_node
)
16817 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
16818 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
16820 /* We have pack expansions, so expand those and
16821 create a new list out of it. */
16822 tree purposevec
= NULL_TREE
;
16823 tree valuevec
= NULL_TREE
;
16827 /* Expand the argument expressions. */
16828 if (TREE_PURPOSE (t
))
16829 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
16830 complain
, in_decl
);
16831 if (TREE_VALUE (t
))
16832 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
16833 complain
, in_decl
);
16835 /* Build the rest of the list. */
16836 chain
= TREE_CHAIN (t
);
16837 if (chain
&& chain
!= void_type_node
)
16838 chain
= RECUR (chain
);
16840 /* Determine the number of arguments. */
16841 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
16843 len
= TREE_VEC_LENGTH (purposevec
);
16844 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
16846 else if (TREE_CODE (valuevec
) == TREE_VEC
)
16847 len
= TREE_VEC_LENGTH (valuevec
);
16850 /* Since we only performed a partial substitution into
16851 the argument pack, we only RETURN (a single list
16853 if (purposevec
== TREE_PURPOSE (t
)
16854 && valuevec
== TREE_VALUE (t
)
16855 && chain
== TREE_CHAIN (t
))
16858 RETURN (tree_cons (purposevec
, valuevec
, chain
));
16861 /* Convert the argument vectors into a TREE_LIST */
16865 /* Grab the Ith values. */
16867 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
16870 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
16873 /* Build the list (backwards). */
16874 chain
= tree_cons (purpose
, value
, chain
);
16880 purpose
= TREE_PURPOSE (t
);
16882 purpose
= RECUR (purpose
);
16883 value
= TREE_VALUE (t
);
16885 value
= RECUR (value
);
16886 chain
= TREE_CHAIN (t
);
16887 if (chain
&& chain
!= void_type_node
)
16888 chain
= RECUR (chain
);
16889 if (purpose
== TREE_PURPOSE (t
)
16890 && value
== TREE_VALUE (t
)
16891 && chain
== TREE_CHAIN (t
))
16893 RETURN (tree_cons (purpose
, value
, chain
));
16896 case COMPONENT_REF
:
16903 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16904 args
, complain
, in_decl
);
16905 /* Remember that there was a reference to this entity. */
16906 if (DECL_P (object
)
16907 && !mark_used (object
, complain
) && !(complain
& tf_error
))
16908 RETURN (error_mark_node
);
16909 object_type
= TREE_TYPE (object
);
16911 member
= TREE_OPERAND (t
, 1);
16912 if (BASELINK_P (member
))
16913 member
= tsubst_baselink (member
,
16914 non_reference (TREE_TYPE (object
)),
16915 args
, complain
, in_decl
);
16917 member
= tsubst_copy (member
, args
, complain
, in_decl
);
16918 if (member
== error_mark_node
)
16919 RETURN (error_mark_node
);
16921 if (type_dependent_expression_p (object
))
16922 /* We can't do much here. */;
16923 else if (!CLASS_TYPE_P (object_type
))
16925 if (scalarish_type_p (object_type
))
16927 tree s
= NULL_TREE
;
16928 tree dtor
= member
;
16930 if (TREE_CODE (dtor
) == SCOPE_REF
)
16932 s
= TREE_OPERAND (dtor
, 0);
16933 dtor
= TREE_OPERAND (dtor
, 1);
16935 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
16937 dtor
= TREE_OPERAND (dtor
, 0);
16939 RETURN (finish_pseudo_destructor_expr
16940 (object
, s
, dtor
, input_location
));
16944 else if (TREE_CODE (member
) == SCOPE_REF
16945 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
16947 /* Lookup the template functions now that we know what the
16949 tree scope
= TREE_OPERAND (member
, 0);
16950 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
16951 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
16952 member
= lookup_qualified_name (scope
, tmpl
,
16953 /*is_type_p=*/false,
16954 /*complain=*/false);
16955 if (BASELINK_P (member
))
16957 BASELINK_FUNCTIONS (member
)
16958 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
16960 member
= (adjust_result_of_qualified_name_lookup
16961 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
16966 qualified_name_lookup_error (scope
, tmpl
, member
,
16968 RETURN (error_mark_node
);
16971 else if (TREE_CODE (member
) == SCOPE_REF
16972 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
16973 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
16975 if (complain
& tf_error
)
16977 if (TYPE_P (TREE_OPERAND (member
, 0)))
16978 error ("%qT is not a class or namespace",
16979 TREE_OPERAND (member
, 0));
16981 error ("%qD is not a class or namespace",
16982 TREE_OPERAND (member
, 0));
16984 RETURN (error_mark_node
);
16986 else if (TREE_CODE (member
) == FIELD_DECL
)
16988 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
16989 if (TREE_CODE (r
) == COMPONENT_REF
)
16990 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16994 r
= finish_class_member_access_expr (object
, member
,
16995 /*template_p=*/false,
16997 if (TREE_CODE (r
) == COMPONENT_REF
)
16998 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17003 RETURN (build_throw
17004 (RECUR (TREE_OPERAND (t
, 0))));
17008 vec
<constructor_elt
, va_gc
> *n
;
17009 constructor_elt
*ce
;
17010 unsigned HOST_WIDE_INT idx
;
17011 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17012 bool process_index_p
;
17014 bool need_copy_p
= false;
17017 if (type
== error_mark_node
)
17018 RETURN (error_mark_node
);
17020 /* digest_init will do the wrong thing if we let it. */
17021 if (type
&& TYPE_PTRMEMFUNC_P (type
))
17024 /* We do not want to process the index of aggregate
17025 initializers as they are identifier nodes which will be
17026 looked up by digest_init. */
17027 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
17029 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
17030 newlen
= vec_safe_length (n
);
17031 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
17033 if (ce
->index
&& process_index_p
17034 /* An identifier index is looked up in the type
17035 being initialized, not the current scope. */
17036 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
17037 ce
->index
= RECUR (ce
->index
);
17039 if (PACK_EXPANSION_P (ce
->value
))
17041 /* Substitute into the pack expansion. */
17042 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
17045 if (ce
->value
== error_mark_node
17046 || PACK_EXPANSION_P (ce
->value
))
17048 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
17049 /* Just move the argument into place. */
17050 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
17053 /* Update the length of the final CONSTRUCTOR
17054 arguments vector, and note that we will need to
17056 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
17057 need_copy_p
= true;
17061 ce
->value
= RECUR (ce
->value
);
17066 vec
<constructor_elt
, va_gc
> *old_n
= n
;
17068 vec_alloc (n
, newlen
);
17069 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
17071 if (TREE_CODE (ce
->value
) == TREE_VEC
)
17073 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
17074 for (i
= 0; i
< len
; ++i
)
17075 CONSTRUCTOR_APPEND_ELT (n
, 0,
17076 TREE_VEC_ELT (ce
->value
, i
));
17079 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
17083 r
= build_constructor (init_list_type_node
, n
);
17084 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
17086 if (TREE_HAS_CONSTRUCTOR (t
))
17087 RETURN (finish_compound_literal (type
, r
, complain
));
17089 TREE_TYPE (r
) = type
;
17095 tree operand_0
= TREE_OPERAND (t
, 0);
17096 if (TYPE_P (operand_0
))
17098 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
17099 RETURN (get_typeid (operand_0
, complain
));
17103 operand_0
= RECUR (operand_0
);
17104 RETURN (build_typeid (operand_0
, complain
));
17111 else if (DECL_PACK_P (t
))
17113 /* We don't build decls for an instantiation of a
17114 variadic capture proxy, we instantiate the elements
17116 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
17117 return RECUR (DECL_VALUE_EXPR (t
));
17123 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
17124 /* ??? We're doing a subset of finish_id_expression here. */
17126 && !processing_template_decl
17127 && !cp_unevaluated_operand
17128 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
17129 && CP_DECL_THREAD_LOCAL_P (r
))
17131 if (tree wrap
= get_tls_wrapper_fn (r
))
17132 /* Replace an evaluated use of the thread_local variable with
17133 a call to its wrapper. */
17134 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
17136 else if (outer_automatic_var_p (r
))
17138 r
= process_outer_var_ref (r
, complain
);
17139 if (is_capture_proxy (r
))
17140 register_local_specialization (r
, t
);
17143 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
17144 /* If the original type was a reference, we'll be wrapped in
17145 the appropriate INDIRECT_REF. */
17146 r
= convert_from_reference (r
);
17152 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17153 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17154 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
17157 case OFFSETOF_EXPR
:
17158 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
17159 EXPR_LOCATION (t
)));
17163 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
17164 complain
, in_decl
);
17166 tree type2
= TRAIT_EXPR_TYPE2 (t
);
17167 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
17168 type2
= RECUR (type2
);
17170 type2
= tsubst (type2
, args
, complain
, in_decl
);
17172 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
17177 tree old_stmt_expr
= cur_stmt_expr
;
17178 tree stmt_expr
= begin_stmt_expr ();
17180 cur_stmt_expr
= stmt_expr
;
17181 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
17182 integral_constant_expression_p
);
17183 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
17184 cur_stmt_expr
= old_stmt_expr
;
17186 /* If the resulting list of expression statement is empty,
17187 fold it further into void_node. */
17188 if (empty_expr_stmt_p (stmt_expr
))
17189 stmt_expr
= void_node
;
17191 RETURN (stmt_expr
);
17196 tree r
= build_lambda_expr ();
17198 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
17199 LAMBDA_EXPR_CLOSURE (r
) = type
;
17200 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
17202 LAMBDA_EXPR_LOCATION (r
)
17203 = LAMBDA_EXPR_LOCATION (t
);
17204 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17205 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17206 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17207 LAMBDA_EXPR_DISCRIMINATOR (r
)
17208 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
17209 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
17211 /* No substitution needed. */;
17212 else if (VAR_OR_FUNCTION_DECL_P (scope
))
17213 /* For a function or variable scope, we want to use tsubst so that we
17214 don't complain about referring to an auto before deduction. */
17215 scope
= tsubst (scope
, args
, complain
, in_decl
);
17216 else if (TREE_CODE (scope
) == PARM_DECL
)
17218 /* Look up the parameter we want directly, as tsubst_copy
17219 doesn't do what we need. */
17220 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
17221 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
17222 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
17223 parm
= DECL_CHAIN (parm
);
17225 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17226 if (DECL_CONTEXT (scope
) == NULL_TREE
)
17227 DECL_CONTEXT (scope
) = fn
;
17229 else if (TREE_CODE (scope
) == FIELD_DECL
)
17230 /* For a field, use tsubst_copy so that we look up the existing field
17231 rather than build a new one. */
17232 scope
= RECUR (scope
);
17234 gcc_unreachable ();
17235 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
17237 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17238 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17240 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17241 determine_visibility (TYPE_NAME (type
));
17242 /* Now that we know visibility, instantiate the type so we have a
17243 declaration of the op() for later calls to lambda_function. */
17244 complete_type (type
);
17246 if (tree fn
= lambda_function (type
))
17247 LAMBDA_EXPR_RETURN_TYPE (r
) = TREE_TYPE (TREE_TYPE (fn
));
17249 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17251 insert_pending_capture_proxies ();
17253 RETURN (build_lambda_object (r
));
17257 /* We can get here for a constant initializer of non-dependent type.
17258 FIXME stop folding in cp_parser_initializer_clause. */
17260 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
17265 case TRANSACTION_EXPR
:
17266 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
17267 integral_constant_expression_p
));
17270 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17272 case VEC_PERM_EXPR
:
17274 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17275 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17276 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17277 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17281 case REQUIRES_EXPR
:
17282 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17285 /* Handle Objective-C++ constructs, if appropriate. */
17288 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17289 in_decl
, /*function_p=*/false);
17293 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17299 input_location
= loc
;
17303 /* Verify that the instantiated ARGS are valid. For type arguments,
17304 make sure that the type's linkage is ok. For non-type arguments,
17305 make sure they are constants if they are integral or enumerations.
17306 Emit an error under control of COMPLAIN, and return TRUE on error. */
17309 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
17311 if (dependent_template_arg_p (t
))
17313 if (ARGUMENT_PACK_P (t
))
17315 tree vec
= ARGUMENT_PACK_ARGS (t
);
17316 int len
= TREE_VEC_LENGTH (vec
);
17317 bool result
= false;
17320 for (i
= 0; i
< len
; ++i
)
17321 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
17325 else if (TYPE_P (t
))
17327 /* [basic.link]: A name with no linkage (notably, the name
17328 of a class or enumeration declared in a local scope)
17329 shall not be used to declare an entity with linkage.
17330 This implies that names with no linkage cannot be used as
17333 DR 757 relaxes this restriction for C++0x. */
17334 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
17335 : no_linkage_check (t
, /*relaxed_p=*/false));
17339 /* DR 488 makes use of a type with no linkage cause
17340 type deduction to fail. */
17341 if (complain
& tf_error
)
17343 if (TYPE_UNNAMED_P (nt
))
17344 error ("%qT is/uses unnamed type", t
);
17346 error ("template argument for %qD uses local type %qT",
17351 /* In order to avoid all sorts of complications, we do not
17352 allow variably-modified types as template arguments. */
17353 else if (variably_modified_type_p (t
, NULL_TREE
))
17355 if (complain
& tf_error
)
17356 error ("%qT is a variably modified type", t
);
17360 /* Class template and alias template arguments should be OK. */
17361 else if (DECL_TYPE_TEMPLATE_P (t
))
17363 /* A non-type argument of integral or enumerated type must be a
17365 else if (TREE_TYPE (t
)
17366 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
17367 && !REFERENCE_REF_P (t
)
17368 && !TREE_CONSTANT (t
))
17370 if (complain
& tf_error
)
17371 error ("integral expression %qE is not constant", t
);
17378 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
17380 int ix
, len
= DECL_NTPARMS (tmpl
);
17381 bool result
= false;
17383 for (ix
= 0; ix
!= len
; ix
++)
17385 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
17388 if (result
&& (complain
& tf_error
))
17389 error (" trying to instantiate %qD", tmpl
);
17393 /* We're out of SFINAE context now, so generate diagnostics for the access
17394 errors we saw earlier when instantiating D from TMPL and ARGS. */
17397 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
17399 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17400 tree type
= TREE_TYPE (pattern
);
17401 location_t loc
= input_location
;
17403 push_access_scope (d
);
17404 push_deferring_access_checks (dk_no_deferred
);
17405 input_location
= DECL_SOURCE_LOCATION (pattern
);
17406 tsubst (type
, args
, tf_warning_or_error
, d
);
17407 input_location
= loc
;
17408 pop_deferring_access_checks ();
17409 pop_access_scope (d
);
17412 /* Instantiate the indicated variable, function, or alias template TMPL with
17413 the template arguments in TARG_PTR. */
17416 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17418 tree targ_ptr
= orig_args
;
17422 bool access_ok
= true;
17424 if (tmpl
== error_mark_node
)
17425 return error_mark_node
;
17427 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
17429 /* If this function is a clone, handle it specially. */
17430 if (DECL_CLONED_FUNCTION_P (tmpl
))
17435 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17436 DECL_CLONED_FUNCTION. */
17437 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
17438 targ_ptr
, complain
);
17439 if (spec
== error_mark_node
)
17440 return error_mark_node
;
17442 /* Look for the clone. */
17443 FOR_EACH_CLONE (clone
, spec
)
17444 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
17446 /* We should always have found the clone by now. */
17447 gcc_unreachable ();
17451 if (targ_ptr
== error_mark_node
)
17452 return error_mark_node
;
17454 /* Check to see if we already have this specialization. */
17455 gen_tmpl
= most_general_template (tmpl
);
17456 if (TMPL_ARGS_DEPTH (targ_ptr
)
17457 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
17458 /* targ_ptr only has the innermost template args, so add the outer ones
17459 from tmpl, which could be either a partial instantiation or gen_tmpl (in
17460 the case of a non-dependent call within a template definition). */
17461 targ_ptr
= (add_outermost_template_args
17462 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
17465 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17466 but it doesn't seem to be on the hot path. */
17467 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
17469 gcc_assert (tmpl
== gen_tmpl
17470 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
17472 || fndecl
== NULL_TREE
);
17474 if (spec
!= NULL_TREE
)
17476 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
17478 if (complain
& tf_error
)
17479 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
17480 return error_mark_node
;
17485 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
17487 return error_mark_node
;
17489 /* We are building a FUNCTION_DECL, during which the access of its
17490 parameters and return types have to be checked. However this
17491 FUNCTION_DECL which is the desired context for access checking
17492 is not built yet. We solve this chicken-and-egg problem by
17493 deferring all checks until we have the FUNCTION_DECL. */
17494 push_deferring_access_checks (dk_deferred
);
17496 /* Instantiation of the function happens in the context of the function
17497 template, not the context of the overload resolution we're doing. */
17498 push_to_top_level ();
17499 /* If there are dependent arguments, e.g. because we're doing partial
17500 ordering, make sure processing_template_decl stays set. */
17501 if (uses_template_parms (targ_ptr
))
17502 ++processing_template_decl
;
17503 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17505 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
17506 complain
, gen_tmpl
, true);
17507 push_nested_class (ctx
);
17510 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17512 fndecl
= NULL_TREE
;
17513 if (VAR_P (pattern
))
17515 /* We need to determine if we're using a partial or explicit
17516 specialization now, because the type of the variable could be
17518 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
17519 tree elt
= most_specialized_partial_spec (tid
, complain
);
17520 if (elt
== error_mark_node
)
17521 pattern
= error_mark_node
;
17524 tree partial_tmpl
= TREE_VALUE (elt
);
17525 tree partial_args
= TREE_PURPOSE (elt
);
17526 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
17527 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
17531 /* Substitute template parameters to obtain the specialization. */
17532 if (fndecl
== NULL_TREE
)
17533 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
17534 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17535 pop_nested_class ();
17536 pop_from_top_level ();
17538 if (fndecl
== error_mark_node
)
17540 pop_deferring_access_checks ();
17541 return error_mark_node
;
17544 /* The DECL_TI_TEMPLATE should always be the immediate parent
17545 template, not the most general template. */
17546 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
17547 DECL_TI_ARGS (fndecl
) = targ_ptr
;
17549 /* Now we know the specialization, compute access previously
17551 push_access_scope (fndecl
);
17552 if (!perform_deferred_access_checks (complain
))
17554 pop_access_scope (fndecl
);
17555 pop_deferring_access_checks ();
17557 /* If we've just instantiated the main entry point for a function,
17558 instantiate all the alternate entry points as well. We do this
17559 by cloning the instantiation of the main entry point, not by
17560 instantiating the template clones. */
17561 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
17562 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
17566 if (!(complain
& tf_error
))
17568 /* Remember to reinstantiate when we're out of SFINAE so the user
17569 can see the errors. */
17570 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
17572 return error_mark_node
;
17577 /* Wrapper for instantiate_template_1. */
17580 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17583 timevar_push (TV_TEMPLATE_INST
);
17584 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
17585 timevar_pop (TV_TEMPLATE_INST
);
17589 /* Instantiate the alias template TMPL with ARGS. Also push a template
17590 instantiation level, which instantiate_template doesn't do because
17591 functions and variables have sufficient context established by the
17595 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
17597 struct pending_template
*old_last_pend
= last_pending_template
;
17598 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17599 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
17600 return error_mark_node
;
17601 tree tinst
= build_tree_list (tmpl
, args
);
17602 if (!push_tinst_level (tinst
))
17605 return error_mark_node
;
17609 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
17610 args
, tmpl
, complain
,
17611 /*require_all_args=*/true,
17612 /*use_default_args=*/true);
17614 tree r
= instantiate_template (tmpl
, args
, complain
);
17615 pop_tinst_level ();
17616 /* We can't free this if a pending_template entry or last_error_tinst_level
17617 is pointing at it. */
17618 if (last_pending_template
== old_last_pend
17619 && last_error_tinst_level
== old_error_tinst
)
17625 /* PARM is a template parameter pack for FN. Returns true iff
17626 PARM is used in a deducible way in the argument list of FN. */
17629 pack_deducible_p (tree parm
, tree fn
)
17631 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
17632 for (; t
; t
= TREE_CHAIN (t
))
17634 tree type
= TREE_VALUE (t
);
17636 if (!PACK_EXPANSION_P (type
))
17638 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
17639 packs
; packs
= TREE_CHAIN (packs
))
17640 if (template_args_equal (TREE_VALUE (packs
), parm
))
17642 /* The template parameter pack is used in a function parameter
17643 pack. If this is the end of the parameter list, the
17644 template parameter pack is deducible. */
17645 if (TREE_CHAIN (t
) == void_list_node
)
17648 /* Otherwise, not. Well, it could be deduced from
17649 a non-pack parameter, but doing so would end up with
17650 a deduction mismatch, so don't bother. */
17654 /* The template parameter pack isn't used in any function parameter
17655 packs, but it might be used deeper, e.g. tuple<Args...>. */
17659 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17660 NARGS elements of the arguments that are being used when calling
17661 it. TARGS is a vector into which the deduced template arguments
17664 Returns either a FUNCTION_DECL for the matching specialization of FN or
17665 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17666 true, diagnostics will be printed to explain why it failed.
17668 If FN is a conversion operator, or we are trying to produce a specific
17669 specialization, RETURN_TYPE is the return type desired.
17671 The EXPLICIT_TARGS are explicit template arguments provided via a
17674 The parameter STRICT is one of:
17677 We are deducing arguments for a function call, as in
17678 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17679 deducing arguments for a call to the result of a conversion
17680 function template, as in [over.call.object].
17683 We are deducing arguments for a conversion function, as in
17684 [temp.deduct.conv].
17687 We are deducing arguments when doing an explicit instantiation
17688 as in [temp.explicit], when determining an explicit specialization
17689 as in [temp.expl.spec], or when taking the address of a function
17690 template, as in [temp.deduct.funcaddr]. */
17693 fn_type_unification (tree fn
,
17694 tree explicit_targs
,
17697 unsigned int nargs
,
17699 unification_kind_t strict
,
17706 tree decl
= NULL_TREE
;
17707 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
17709 static int deduction_depth
;
17710 struct pending_template
*old_last_pend
= last_pending_template
;
17711 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17712 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
17714 tree r
= error_mark_node
;
17716 tree full_targs
= targs
;
17717 if (TMPL_ARGS_DEPTH (targs
)
17718 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
17719 full_targs
= (add_outermost_template_args
17720 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
17724 complain
|= tf_decltype
;
17726 /* In C++0x, it's possible to have a function template whose type depends
17727 on itself recursively. This is most obvious with decltype, but can also
17728 occur with enumeration scope (c++/48969). So we need to catch infinite
17729 recursion and reject the substitution at deduction time; this function
17730 will return error_mark_node for any repeated substitution.
17732 This also catches excessive recursion such as when f<N> depends on
17733 f<N-1> across all integers, and returns error_mark_node for all the
17734 substitutions back up to the initial one.
17736 This is, of course, not reentrant. */
17737 if (excessive_deduction_depth
)
17738 return error_mark_node
;
17739 tinst
= build_tree_list (fn
, NULL_TREE
);
17742 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
17744 fntype
= TREE_TYPE (fn
);
17745 if (explicit_targs
)
17749 The specified template arguments must match the template
17750 parameters in kind (i.e., type, nontype, template), and there
17751 must not be more arguments than there are parameters;
17752 otherwise type deduction fails.
17754 Nontype arguments must match the types of the corresponding
17755 nontype template parameters, or must be convertible to the
17756 types of the corresponding nontype parameters as specified in
17757 _temp.arg.nontype_, otherwise type deduction fails.
17759 All references in the function type of the function template
17760 to the corresponding template parameters are replaced by the
17761 specified template argument values. If a substitution in a
17762 template parameter or in the function type of the function
17763 template results in an invalid type, type deduction fails. */
17764 int i
, len
= TREE_VEC_LENGTH (tparms
);
17765 location_t loc
= input_location
;
17766 bool incomplete
= false;
17768 if (explicit_targs
== error_mark_node
)
17771 if (TMPL_ARGS_DEPTH (explicit_targs
)
17772 < TMPL_ARGS_DEPTH (full_targs
))
17773 explicit_targs
= add_outermost_template_args (full_targs
,
17776 /* Adjust any explicit template arguments before entering the
17777 substitution context. */
17779 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
17781 /*require_all_args=*/false,
17782 /*use_default_args=*/false));
17783 if (explicit_targs
== error_mark_node
)
17786 /* Substitute the explicit args into the function type. This is
17787 necessary so that, for instance, explicitly declared function
17788 arguments can match null pointed constants. If we were given
17789 an incomplete set of explicit args, we must not do semantic
17790 processing during substitution as we could create partial
17792 for (i
= 0; i
< len
; i
++)
17794 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
17795 bool parameter_pack
= false;
17796 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
17798 /* Dig out the actual parm. */
17799 if (TREE_CODE (parm
) == TYPE_DECL
17800 || TREE_CODE (parm
) == TEMPLATE_DECL
)
17802 parm
= TREE_TYPE (parm
);
17803 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
17805 else if (TREE_CODE (parm
) == PARM_DECL
)
17807 parm
= DECL_INITIAL (parm
);
17808 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
17811 if (!parameter_pack
&& targ
== NULL_TREE
)
17812 /* No explicit argument for this template parameter. */
17815 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
17817 /* Mark the argument pack as "incomplete". We could
17818 still deduce more arguments during unification.
17819 We remove this mark in type_unification_real. */
17822 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
17823 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
17824 = ARGUMENT_PACK_ARGS (targ
);
17827 /* We have some incomplete argument packs. */
17832 TREE_VALUE (tinst
) = explicit_targs
;
17833 if (!push_tinst_level (tinst
))
17835 excessive_deduction_depth
= true;
17838 processing_template_decl
+= incomplete
;
17839 input_location
= DECL_SOURCE_LOCATION (fn
);
17840 /* Ignore any access checks; we'll see them again in
17841 instantiate_template and they might have the wrong
17842 access path at this point. */
17843 push_deferring_access_checks (dk_deferred
);
17844 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
17845 complain
| tf_partial
, NULL_TREE
);
17846 pop_deferring_access_checks ();
17847 input_location
= loc
;
17848 processing_template_decl
-= incomplete
;
17849 pop_tinst_level ();
17851 if (fntype
== error_mark_node
)
17854 /* Place the explicitly specified arguments in TARGS. */
17855 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
17856 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
17857 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
17860 /* Never do unification on the 'this' parameter. */
17861 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
17863 if (return_type
&& strict
== DEDUCE_CALL
)
17865 /* We're deducing for a call to the result of a template conversion
17866 function. The parms we really want are in return_type. */
17867 if (POINTER_TYPE_P (return_type
))
17868 return_type
= TREE_TYPE (return_type
);
17869 parms
= TYPE_ARG_TYPES (return_type
);
17871 else if (return_type
)
17875 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
17876 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
17877 new_args
[0] = return_type
;
17878 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
17883 /* We allow incomplete unification without an error message here
17884 because the standard doesn't seem to explicitly prohibit it. Our
17885 callers must be ready to deal with unification failures in any
17888 TREE_VALUE (tinst
) = targs
;
17889 /* If we aren't explaining yet, push tinst context so we can see where
17890 any errors (e.g. from class instantiations triggered by instantiation
17891 of default template arguments) come from. If we are explaining, this
17892 context is redundant. */
17893 if (!explain_p
&& !push_tinst_level (tinst
))
17895 excessive_deduction_depth
= true;
17899 /* type_unification_real will pass back any access checks from default
17900 template argument substitution. */
17901 vec
<deferred_access_check
, va_gc
> *checks
;
17904 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17905 full_targs
, parms
, args
, nargs
, /*subr=*/0,
17906 strict
, flags
, &checks
, explain_p
);
17908 pop_tinst_level ();
17912 /* Now that we have bindings for all of the template arguments,
17913 ensure that the arguments deduced for the template template
17914 parameters have compatible template parameter lists. We cannot
17915 check this property before we have deduced all template
17916 arguments, because the template parameter types of a template
17917 template parameter might depend on prior template parameters
17918 deduced after the template template parameter. The following
17919 ill-formed example illustrates this issue:
17921 template<typename T, template<T> class C> void f(C<5>, T);
17923 template<int N> struct X {};
17926 f(X<5>(), 5l); // error: template argument deduction fails
17929 The template parameter list of 'C' depends on the template type
17930 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17931 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17932 time that we deduce 'C'. */
17933 if (!template_template_parm_bindings_ok_p
17934 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
17936 unify_inconsistent_template_template_parameters (explain_p
);
17940 /* All is well so far. Now, check:
17944 When all template arguments have been deduced, all uses of
17945 template parameters in nondeduced contexts are replaced with
17946 the corresponding deduced argument values. If the
17947 substitution results in an invalid type, as described above,
17948 type deduction fails. */
17949 TREE_VALUE (tinst
) = targs
;
17950 if (!push_tinst_level (tinst
))
17952 excessive_deduction_depth
= true;
17956 /* Also collect access checks from the instantiation. */
17957 reopen_deferring_access_checks (checks
);
17959 decl
= instantiate_template (fn
, targs
, complain
);
17961 checks
= get_deferred_access_checks ();
17962 pop_deferring_access_checks ();
17964 pop_tinst_level ();
17966 if (decl
== error_mark_node
)
17969 /* Now perform any access checks encountered during substitution. */
17970 push_access_scope (decl
);
17971 ok
= perform_access_checks (checks
, complain
);
17972 pop_access_scope (decl
);
17976 /* If we're looking for an exact match, check that what we got
17977 is indeed an exact match. It might not be if some template
17978 parameters are used in non-deduced contexts. But don't check
17979 for an exact match if we have dependent template arguments;
17980 in that case we're doing partial ordering, and we already know
17981 that we have two candidates that will provide the actual type. */
17982 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
17984 tree substed
= TREE_TYPE (decl
);
17988 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
17990 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
17991 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
17992 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
17994 unify_type_mismatch (explain_p
, args
[i
],
17995 TREE_VALUE (sarg
));
18004 if (excessive_deduction_depth
)
18006 if (deduction_depth
== 0)
18007 /* Reset once we're all the way out. */
18008 excessive_deduction_depth
= false;
18011 /* We can't free this if a pending_template entry or last_error_tinst_level
18012 is pointing at it. */
18013 if (last_pending_template
== old_last_pend
18014 && last_error_tinst_level
== old_error_tinst
)
18020 /* Adjust types before performing type deduction, as described in
18021 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18022 sections are symmetric. PARM is the type of a function parameter
18023 or the return type of the conversion function. ARG is the type of
18024 the argument passed to the call, or the type of the value
18025 initialized with the result of the conversion function.
18026 ARG_EXPR is the original argument expression, which may be null. */
18029 maybe_adjust_types_for_deduction (unification_kind_t strict
,
18042 /* Swap PARM and ARG throughout the remainder of this
18043 function; the handling is precisely symmetric since PARM
18044 will initialize ARG rather than vice versa. */
18045 std::swap (parm
, arg
);
18049 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18050 too, but here handle it by stripping the reference from PARM
18051 rather than by adding it to ARG. */
18052 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18053 && TYPE_REF_IS_RVALUE (*parm
)
18054 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18055 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18056 && TREE_CODE (*arg
) == REFERENCE_TYPE
18057 && !TYPE_REF_IS_RVALUE (*arg
))
18058 *parm
= TREE_TYPE (*parm
);
18059 /* Nothing else to do in this case. */
18063 gcc_unreachable ();
18066 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
18068 /* [temp.deduct.call]
18070 If P is not a reference type:
18072 --If A is an array type, the pointer type produced by the
18073 array-to-pointer standard conversion (_conv.array_) is
18074 used in place of A for type deduction; otherwise,
18076 --If A is a function type, the pointer type produced by
18077 the function-to-pointer standard conversion
18078 (_conv.func_) is used in place of A for type deduction;
18081 --If A is a cv-qualified type, the top level
18082 cv-qualifiers of A's type are ignored for type
18084 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
18085 *arg
= build_pointer_type (TREE_TYPE (*arg
));
18086 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
18087 *arg
= build_pointer_type (*arg
);
18089 *arg
= TYPE_MAIN_VARIANT (*arg
);
18092 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
18093 of the form T&&, where T is a template parameter, and the argument
18094 is an lvalue, T is deduced as A& */
18095 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18096 && TYPE_REF_IS_RVALUE (*parm
)
18097 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18098 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18099 && (arg_expr
? lvalue_p (arg_expr
)
18100 /* try_one_overload doesn't provide an arg_expr, but
18101 functions are always lvalues. */
18102 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
18103 *arg
= build_reference_type (*arg
);
18105 /* [temp.deduct.call]
18107 If P is a cv-qualified type, the top level cv-qualifiers
18108 of P's type are ignored for type deduction. If P is a
18109 reference type, the type referred to by P is used for
18111 *parm
= TYPE_MAIN_VARIANT (*parm
);
18112 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
18114 *parm
= TREE_TYPE (*parm
);
18115 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18118 /* DR 322. For conversion deduction, remove a reference type on parm
18119 too (which has been swapped into ARG). */
18120 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
18121 *arg
= TREE_TYPE (*arg
);
18126 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18127 template which does contain any deducible template parameters; check if
18128 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18129 unify_one_argument. */
18132 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
18133 int flags
, bool explain_p
)
18138 type
= TREE_TYPE (arg
);
18142 if (same_type_p (parm
, type
))
18143 return unify_success (explain_p
);
18145 if (strict
== DEDUCE_CONV
)
18147 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
18148 explain_p
? tf_warning_or_error
: tf_none
))
18149 return unify_success (explain_p
);
18151 else if (strict
!= DEDUCE_EXACT
)
18153 if (can_convert_arg (parm
, type
,
18154 TYPE_P (arg
) ? NULL_TREE
: arg
,
18155 flags
, explain_p
? tf_warning_or_error
: tf_none
))
18156 return unify_success (explain_p
);
18159 if (strict
== DEDUCE_EXACT
)
18160 return unify_type_mismatch (explain_p
, parm
, arg
);
18162 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
18165 static bool uses_deducible_template_parms (tree type
);
18167 /* Returns true iff the expression EXPR is one from which a template
18168 argument can be deduced. In other words, if it's an undecorated
18169 use of a template non-type parameter. */
18172 deducible_expression (tree expr
)
18174 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
18177 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18178 deducible way; that is, if it has a max value of <PARM> - 1. */
18181 deducible_array_bound (tree domain
)
18183 if (domain
== NULL_TREE
)
18186 tree max
= TYPE_MAX_VALUE (domain
);
18187 if (TREE_CODE (max
) != MINUS_EXPR
)
18190 return deducible_expression (TREE_OPERAND (max
, 0));
18193 /* Returns true iff the template arguments ARGS use a template parameter
18194 in a deducible way. */
18197 deducible_template_args (tree args
)
18199 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
18202 tree elt
= TREE_VEC_ELT (args
, i
);
18203 if (ARGUMENT_PACK_P (elt
))
18204 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
18207 if (PACK_EXPANSION_P (elt
))
18208 elt
= PACK_EXPANSION_PATTERN (elt
);
18209 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
18211 else if (TYPE_P (elt
))
18212 deducible
= uses_deducible_template_parms (elt
);
18214 deducible
= deducible_expression (elt
);
18222 /* Returns true iff TYPE contains any deducible references to template
18223 parameters, as per 14.8.2.5. */
18226 uses_deducible_template_parms (tree type
)
18228 if (PACK_EXPANSION_P (type
))
18229 type
= PACK_EXPANSION_PATTERN (type
);
18236 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18237 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18243 if (POINTER_TYPE_P (type
))
18244 return uses_deducible_template_parms (TREE_TYPE (type
));
18246 /* T[integer-constant ]
18248 if (TREE_CODE (type
) == ARRAY_TYPE
)
18249 return (uses_deducible_template_parms (TREE_TYPE (type
))
18250 || deducible_array_bound (TYPE_DOMAIN (type
)));
18262 if (TYPE_PTRMEM_P (type
))
18263 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
18264 || (uses_deducible_template_parms
18265 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
18267 /* template-name <T> (where template-name refers to a class template)
18268 template-name <i> (where template-name refers to a class template) */
18269 if (CLASS_TYPE_P (type
)
18270 && CLASSTYPE_TEMPLATE_INFO (type
)
18271 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
18272 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18273 (CLASSTYPE_TI_ARGS (type
)));
18278 if (TREE_CODE (type
) == FUNCTION_TYPE
18279 || TREE_CODE (type
) == METHOD_TYPE
)
18281 if (uses_deducible_template_parms (TREE_TYPE (type
)))
18283 tree parm
= TYPE_ARG_TYPES (type
);
18284 if (TREE_CODE (type
) == METHOD_TYPE
)
18285 parm
= TREE_CHAIN (parm
);
18286 for (; parm
; parm
= TREE_CHAIN (parm
))
18287 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
18294 /* Subroutine of type_unification_real and unify_pack_expansion to
18295 handle unification of a single P/A pair. Parameters are as
18296 for those functions. */
18299 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
18300 int subr
, unification_kind_t strict
,
18303 tree arg_expr
= NULL_TREE
;
18306 if (arg
== error_mark_node
|| parm
== error_mark_node
)
18307 return unify_invalid (explain_p
);
18308 if (arg
== unknown_type_node
)
18309 /* We can't deduce anything from this, but we might get all the
18310 template args from other function args. */
18311 return unify_success (explain_p
);
18313 /* Implicit conversions (Clause 4) will be performed on a function
18314 argument to convert it to the type of the corresponding function
18315 parameter if the parameter type contains no template-parameters that
18316 participate in template argument deduction. */
18317 if (strict
!= DEDUCE_EXACT
18318 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
18319 /* For function parameters with no deducible template parameters,
18320 just return. We'll check non-dependent conversions later. */
18321 return unify_success (explain_p
);
18326 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
18327 | UNIFY_ALLOW_MORE_CV_QUAL
18328 | UNIFY_ALLOW_DERIVED
);
18332 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
18336 arg_strict
= UNIFY_ALLOW_NONE
;
18340 gcc_unreachable ();
18343 /* We only do these transformations if this is the top-level
18344 parameter_type_list in a call or declaration matching; in other
18345 situations (nested function declarators, template argument lists) we
18346 won't be comparing a type to an expression, and we don't do any type
18352 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
18353 if (type_unknown_p (arg
))
18355 /* [temp.deduct.type] A template-argument can be
18356 deduced from a pointer to function or pointer
18357 to member function argument if the set of
18358 overloaded functions does not contain function
18359 templates and at most one of a set of
18360 overloaded functions provides a unique
18363 if (resolve_overloaded_unification
18364 (tparms
, targs
, parm
, arg
, strict
,
18365 arg_strict
, explain_p
))
18366 return unify_success (explain_p
);
18367 return unify_overload_resolution_failure (explain_p
, arg
);
18371 arg
= unlowered_expr_type (arg
);
18372 if (arg
== error_mark_node
)
18373 return unify_invalid (explain_p
);
18377 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
18380 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
18381 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
18382 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18384 /* For deduction from an init-list we need the actual list. */
18385 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
18387 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
18390 /* Most parms like fn_type_unification.
18392 If SUBR is 1, we're being called recursively (to unify the
18393 arguments of a function or method parameter of a function
18396 CHECKS is a pointer to a vector of access checks encountered while
18397 substituting default template arguments. */
18400 type_unification_real (tree tparms
,
18404 unsigned int xnargs
,
18406 unification_kind_t strict
,
18408 vec
<deferred_access_check
, va_gc
> **checks
,
18413 int ntparms
= TREE_VEC_LENGTH (tparms
);
18414 int saw_undeduced
= 0;
18417 unsigned int nargs
;
18420 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
18421 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
18422 gcc_assert (ntparms
> 0);
18424 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
18426 /* Reset the number of non-defaulted template arguments contained
18428 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
18436 while (parms
&& parms
!= void_list_node
18439 parm
= TREE_VALUE (parms
);
18441 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18442 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
18443 /* For a function parameter pack that occurs at the end of the
18444 parameter-declaration-list, the type A of each remaining
18445 argument of the call is compared with the type P of the
18446 declarator-id of the function parameter pack. */
18449 parms
= TREE_CHAIN (parms
);
18451 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18452 /* For a function parameter pack that does not occur at the
18453 end of the parameter-declaration-list, the type of the
18454 parameter pack is a non-deduced context. */
18460 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
18466 && parms
!= void_list_node
18467 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
18469 /* Unify the remaining arguments with the pack expansion type. */
18471 tree parmvec
= make_tree_vec (1);
18473 /* Allocate a TREE_VEC and copy in all of the arguments */
18474 argvec
= make_tree_vec (nargs
- ia
);
18475 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
18476 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
18478 /* Copy the parameter into parmvec. */
18479 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
18480 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
18481 /*subr=*/subr
, explain_p
))
18484 /* Advance to the end of the list of parameters. */
18485 parms
= TREE_CHAIN (parms
);
18488 /* Fail if we've reached the end of the parm list, and more args
18489 are present, and the parm list isn't variadic. */
18490 if (ia
< nargs
&& parms
== void_list_node
)
18491 return unify_too_many_arguments (explain_p
, nargs
, ia
);
18492 /* Fail if parms are left and they don't have default values and
18493 they aren't all deduced as empty packs (c++/57397). This is
18494 consistent with sufficient_parms_p. */
18495 if (parms
&& parms
!= void_list_node
18496 && TREE_PURPOSE (parms
) == NULL_TREE
)
18498 unsigned int count
= nargs
;
18503 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
18506 p
= TREE_CHAIN (p
);
18508 while (p
&& p
!= void_list_node
);
18509 if (count
!= nargs
)
18510 return unify_too_few_arguments (explain_p
, ia
, count
,
18516 tsubst_flags_t complain
= (explain_p
18517 ? tf_warning_or_error
18520 for (i
= 0; i
< ntparms
; i
++)
18522 tree targ
= TREE_VEC_ELT (targs
, i
);
18523 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18525 /* Clear the "incomplete" flags on all argument packs now so that
18526 substituting them into later default arguments works. */
18527 if (targ
&& ARGUMENT_PACK_P (targ
))
18529 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
18530 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
18533 if (targ
|| tparm
== error_mark_node
)
18535 tparm
= TREE_VALUE (tparm
);
18537 /* If this is an undeduced nontype parameter that depends on
18538 a type parameter, try another pass; its type may have been
18539 deduced from a later argument than the one from which
18540 this parameter can be deduced. */
18541 if (TREE_CODE (tparm
) == PARM_DECL
18542 && uses_template_parms (TREE_TYPE (tparm
))
18543 && saw_undeduced
< 2)
18549 /* Core issue #226 (C++0x) [temp.deduct]:
18551 If a template argument has not been deduced, its
18552 default template argument, if any, is used.
18554 When we are in C++98 mode, TREE_PURPOSE will either
18555 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18556 to explicitly check cxx_dialect here. */
18557 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
18558 /* OK, there is a default argument. Wait until after the
18559 conversion check to do substitution. */
18562 /* If the type parameter is a parameter pack, then it will
18563 be deduced to an empty parameter pack. */
18564 if (template_parameter_pack_p (tparm
))
18568 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
18570 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
18571 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
18572 TREE_CONSTANT (arg
) = 1;
18575 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
18577 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
18579 TREE_VEC_ELT (targs
, i
) = arg
;
18583 return unify_parameter_deduction_failure (explain_p
, tparm
);
18586 /* DR 1391: All parameters have args, now check non-dependent parms for
18588 if (saw_undeduced
< 2)
18589 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
18590 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
18592 parm
= TREE_VALUE (parms
);
18594 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18595 && (!TREE_CHAIN (parms
)
18596 || TREE_CHAIN (parms
) == void_list_node
))
18597 /* For a function parameter pack that occurs at the end of the
18598 parameter-declaration-list, the type A of each remaining
18599 argument of the call is compared with the type P of the
18600 declarator-id of the function parameter pack. */
18603 parms
= TREE_CHAIN (parms
);
18605 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18606 /* For a function parameter pack that does not occur at the
18607 end of the parameter-declaration-list, the type of the
18608 parameter pack is a non-deduced context. */
18614 if (uses_template_parms (parm
))
18616 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
18621 /* Now substitute into the default template arguments. */
18622 for (i
= 0; i
< ntparms
; i
++)
18624 tree targ
= TREE_VEC_ELT (targs
, i
);
18625 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18627 if (targ
|| tparm
== error_mark_node
)
18629 tree parm
= TREE_VALUE (tparm
);
18631 if (TREE_CODE (parm
) == PARM_DECL
18632 && uses_template_parms (TREE_TYPE (parm
))
18633 && saw_undeduced
< 2)
18636 tree arg
= TREE_PURPOSE (tparm
);
18637 reopen_deferring_access_checks (*checks
);
18638 location_t save_loc
= input_location
;
18640 input_location
= DECL_SOURCE_LOCATION (parm
);
18641 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
18642 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
18644 input_location
= save_loc
;
18645 *checks
= get_deferred_access_checks ();
18646 pop_deferring_access_checks ();
18647 if (arg
== error_mark_node
)
18651 TREE_VEC_ELT (targs
, i
) = arg
;
18652 /* The position of the first default template argument,
18653 is also the number of non-defaulted arguments in TARGS.
18655 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18656 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
18661 if (saw_undeduced
++ == 1)
18665 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
18666 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
18668 return unify_success (explain_p
);
18671 /* Subroutine of type_unification_real. Args are like the variables
18672 at the call site. ARG is an overloaded function (or template-id);
18673 we try deducing template args from each of the overloads, and if
18674 only one succeeds, we go with that. Modifies TARGS and returns
18675 true on success. */
18678 resolve_overloaded_unification (tree tparms
,
18682 unification_kind_t strict
,
18686 tree tempargs
= copy_node (targs
);
18688 tree goodfn
= NULL_TREE
;
18691 if (TREE_CODE (arg
) == ADDR_EXPR
)
18693 arg
= TREE_OPERAND (arg
, 0);
18699 if (TREE_CODE (arg
) == COMPONENT_REF
)
18700 /* Handle `&x' where `x' is some static or non-static member
18702 arg
= TREE_OPERAND (arg
, 1);
18704 if (TREE_CODE (arg
) == OFFSET_REF
)
18705 arg
= TREE_OPERAND (arg
, 1);
18707 /* Strip baselink information. */
18708 if (BASELINK_P (arg
))
18709 arg
= BASELINK_FUNCTIONS (arg
);
18711 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
18713 /* If we got some explicit template args, we need to plug them into
18714 the affected templates before we try to unify, in case the
18715 explicit args will completely resolve the templates in question. */
18718 tree expl_subargs
= TREE_OPERAND (arg
, 1);
18719 arg
= TREE_OPERAND (arg
, 0);
18721 for (; arg
; arg
= OVL_NEXT (arg
))
18723 tree fn
= OVL_CURRENT (arg
);
18724 tree subargs
, elem
;
18726 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18729 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18730 expl_subargs
, NULL_TREE
, tf_none
,
18731 /*require_all_args=*/true,
18732 /*use_default_args=*/true);
18733 if (subargs
!= error_mark_node
18734 && !any_dependent_template_arguments_p (subargs
))
18736 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
18737 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18738 elem
, strict
, sub_strict
, addr_p
, explain_p
)
18739 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
18748 /* If no templates (or more than one) are fully resolved by the
18749 explicit arguments, this template-id is a non-deduced context; it
18750 could still be OK if we deduce all template arguments for the
18751 enclosing call through other arguments. */
18755 else if (TREE_CODE (arg
) != OVERLOAD
18756 && TREE_CODE (arg
) != FUNCTION_DECL
)
18757 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18758 -- but the deduction does not succeed because the expression is
18759 not just the function on its own. */
18762 for (; arg
; arg
= OVL_NEXT (arg
))
18763 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
18764 TREE_TYPE (OVL_CURRENT (arg
)),
18765 strict
, sub_strict
, addr_p
, explain_p
)
18766 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
18768 goodfn
= OVL_CURRENT (arg
);
18772 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18773 to function or pointer to member function argument if the set of
18774 overloaded functions does not contain function templates and at most
18775 one of a set of overloaded functions provides a unique match.
18777 So if we found multiple possibilities, we return success but don't
18778 deduce anything. */
18782 int i
= TREE_VEC_LENGTH (targs
);
18784 if (TREE_VEC_ELT (tempargs
, i
))
18786 tree old
= TREE_VEC_ELT (targs
, i
);
18787 tree new_
= TREE_VEC_ELT (tempargs
, i
);
18788 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
18789 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
18790 /* Don't forget explicit template arguments in a pack. */
18791 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
18792 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
18793 TREE_VEC_ELT (targs
, i
) = new_
;
18802 /* Core DR 115: In contexts where deduction is done and fails, or in
18803 contexts where deduction is not done, if a template argument list is
18804 specified and it, along with any default template arguments, identifies
18805 a single function template specialization, then the template-id is an
18806 lvalue for the function template specialization. */
18809 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
18811 tree expr
, offset
, baselink
;
18814 if (!type_unknown_p (orig_expr
))
18819 offset
= NULL_TREE
;
18820 baselink
= NULL_TREE
;
18822 if (TREE_CODE (expr
) == ADDR_EXPR
)
18824 expr
= TREE_OPERAND (expr
, 0);
18827 if (TREE_CODE (expr
) == OFFSET_REF
)
18830 expr
= TREE_OPERAND (expr
, 1);
18832 if (BASELINK_P (expr
))
18835 expr
= BASELINK_FUNCTIONS (expr
);
18838 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
18841 tree goodfn
= NULL_TREE
;
18843 /* If we got some explicit template args, we need to plug them into
18844 the affected templates before we try to unify, in case the
18845 explicit args will completely resolve the templates in question. */
18847 tree expl_subargs
= TREE_OPERAND (expr
, 1);
18848 tree arg
= TREE_OPERAND (expr
, 0);
18849 tree badfn
= NULL_TREE
;
18850 tree badargs
= NULL_TREE
;
18852 for (; arg
; arg
= OVL_NEXT (arg
))
18854 tree fn
= OVL_CURRENT (arg
);
18855 tree subargs
, elem
;
18857 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
18860 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18861 expl_subargs
, NULL_TREE
, tf_none
,
18862 /*require_all_args=*/true,
18863 /*use_default_args=*/true);
18864 if (subargs
!= error_mark_node
18865 && !any_dependent_template_arguments_p (subargs
))
18867 elem
= instantiate_template (fn
, subargs
, tf_none
);
18868 if (elem
== error_mark_node
)
18873 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
18882 mark_used (goodfn
);
18885 expr
= build_baselink (BASELINK_BINFO (baselink
),
18886 BASELINK_ACCESS_BINFO (baselink
),
18887 expr
, BASELINK_OPTYPE (baselink
));
18891 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
18892 expr
= build_offset_ref (base
, expr
, addr
, complain
);
18895 expr
= cp_build_addr_expr (expr
, complain
);
18898 else if (good
== 0 && badargs
&& (complain
& tf_error
))
18899 /* There were no good options and at least one bad one, so let the
18900 user know what the problem is. */
18901 instantiate_template (badfn
, badargs
, complain
);
18906 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18907 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18908 different overloads deduce different arguments for a given parm.
18909 ADDR_P is true if the expression for which deduction is being
18910 performed was of the form "& fn" rather than simply "fn".
18912 Returns 1 on success. */
18915 try_one_overload (tree tparms
,
18920 unification_kind_t strict
,
18929 if (arg
== error_mark_node
)
18932 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18933 to function or pointer to member function argument if the set of
18934 overloaded functions does not contain function templates and at most
18935 one of a set of overloaded functions provides a unique match.
18937 So if this is a template, just return success. */
18939 if (uses_template_parms (arg
))
18942 if (TREE_CODE (arg
) == METHOD_TYPE
)
18943 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
18945 arg
= build_pointer_type (arg
);
18947 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
18949 /* We don't copy orig_targs for this because if we have already deduced
18950 some template args from previous args, unify would complain when we
18951 try to deduce a template parameter for the same argument, even though
18952 there isn't really a conflict. */
18953 nargs
= TREE_VEC_LENGTH (targs
);
18954 tempargs
= make_tree_vec (nargs
);
18956 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
18959 /* First make sure we didn't deduce anything that conflicts with
18960 explicitly specified args. */
18961 for (i
= nargs
; i
--; )
18963 tree elt
= TREE_VEC_ELT (tempargs
, i
);
18964 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
18968 else if (uses_template_parms (elt
))
18969 /* Since we're unifying against ourselves, we will fill in
18970 template args used in the function parm list with our own
18971 template parms. Discard them. */
18972 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
18973 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
18975 /* Check that the argument at each index of the deduced argument pack
18976 is equivalent to the corresponding explicitly specified argument.
18977 We may have deduced more arguments than were explicitly specified,
18979 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt
));
18980 gcc_assert (ARGUMENT_PACK_ARGS (oldelt
)
18981 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt
));
18983 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
18984 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
18986 if (TREE_VEC_LENGTH (deduced_pack
)
18987 < TREE_VEC_LENGTH (explicit_pack
))
18990 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
18991 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
18992 TREE_VEC_ELT (deduced_pack
, j
)))
18995 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
18999 for (i
= nargs
; i
--; )
19001 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19004 TREE_VEC_ELT (targs
, i
) = elt
;
19010 /* PARM is a template class (perhaps with unbound template
19011 parameters). ARG is a fully instantiated type. If ARG can be
19012 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19013 TARGS are as for unify. */
19016 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
19019 tree copy_of_targs
;
19021 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
19022 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
19023 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
19026 /* We need to make a new template argument vector for the call to
19027 unify. If we used TARGS, we'd clutter it up with the result of
19028 the attempted unification, even if this class didn't work out.
19029 We also don't want to commit ourselves to all the unifications
19030 we've already done, since unification is supposed to be done on
19031 an argument-by-argument basis. In other words, consider the
19032 following pathological case:
19034 template <int I, int J, int K>
19037 template <int I, int J>
19038 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19040 template <int I, int J, int K>
19041 void f(S<I, J, K>, S<I, I, I>);
19050 Now, by the time we consider the unification involving `s2', we
19051 already know that we must have `f<0, 0, 0>'. But, even though
19052 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19053 because there are two ways to unify base classes of S<0, 1, 2>
19054 with S<I, I, I>. If we kept the already deduced knowledge, we
19055 would reject the possibility I=1. */
19056 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19058 /* If unification failed, we're done. */
19059 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
19060 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
19066 /* Given a template type PARM and a class type ARG, find the unique
19067 base type in ARG that is an instance of PARM. We do not examine
19068 ARG itself; only its base-classes. If there is not exactly one
19069 appropriate base class, return NULL_TREE. PARM may be the type of
19070 a partial specialization, as well as a plain template type. Used
19073 static enum template_base_result
19074 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
19075 bool explain_p
, tree
*result
)
19077 tree rval
= NULL_TREE
;
19080 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
19082 binfo
= TYPE_BINFO (complete_type (arg
));
19085 /* The type could not be completed. */
19086 *result
= NULL_TREE
;
19087 return tbr_incomplete_type
;
19090 /* Walk in inheritance graph order. The search order is not
19091 important, and this avoids multiple walks of virtual bases. */
19092 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
19094 tree r
= try_class_unification (tparms
, targs
, parm
,
19095 BINFO_TYPE (binfo
), explain_p
);
19099 /* If there is more than one satisfactory baseclass, then:
19103 If they yield more than one possible deduced A, the type
19107 if (rval
&& !same_type_p (r
, rval
))
19109 *result
= NULL_TREE
;
19110 return tbr_ambiguous_baseclass
;
19118 return tbr_success
;
19121 /* Returns the level of DECL, which declares a template parameter. */
19124 template_decl_level (tree decl
)
19126 switch (TREE_CODE (decl
))
19129 case TEMPLATE_DECL
:
19130 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
19133 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
19136 gcc_unreachable ();
19141 /* Decide whether ARG can be unified with PARM, considering only the
19142 cv-qualifiers of each type, given STRICT as documented for unify.
19143 Returns nonzero iff the unification is OK on that basis. */
19146 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
19148 int arg_quals
= cp_type_quals (arg
);
19149 int parm_quals
= cp_type_quals (parm
);
19151 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19152 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19154 /* Although a CVR qualifier is ignored when being applied to a
19155 substituted template parameter ([8.3.2]/1 for example), that
19156 does not allow us to unify "const T" with "int&" because both
19157 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19158 It is ok when we're allowing additional CV qualifiers
19159 at the outer level [14.8.2.1]/3,1st bullet. */
19160 if ((TREE_CODE (arg
) == REFERENCE_TYPE
19161 || TREE_CODE (arg
) == FUNCTION_TYPE
19162 || TREE_CODE (arg
) == METHOD_TYPE
)
19163 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
19166 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
19167 && (parm_quals
& TYPE_QUAL_RESTRICT
))
19171 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19172 && (arg_quals
& parm_quals
) != parm_quals
)
19175 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
19176 && (parm_quals
& arg_quals
) != arg_quals
)
19182 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19184 template_parm_level_and_index (tree parm
, int* level
, int* index
)
19186 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19187 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19188 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19190 *index
= TEMPLATE_TYPE_IDX (parm
);
19191 *level
= TEMPLATE_TYPE_LEVEL (parm
);
19195 *index
= TEMPLATE_PARM_IDX (parm
);
19196 *level
= TEMPLATE_PARM_LEVEL (parm
);
19200 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19202 if (unify (TP, TA, P, A, S, EP)) \
19206 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19207 expansion at the end of PACKED_PARMS. Returns 0 if the type
19208 deduction succeeds, 1 otherwise. STRICT is the same as in
19209 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
19210 call argument list. We'll need to adjust the arguments to make them
19211 types. SUBR tells us if this is from a recursive call to
19212 type_unification_real, or for comparing two template argument
19216 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
19217 tree packed_args
, unification_kind_t strict
,
19218 bool subr
, bool explain_p
)
19221 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
19222 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
19223 tree pack
, packs
= NULL_TREE
;
19224 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
19226 packed_args
= expand_template_argument_pack (packed_args
);
19228 int len
= TREE_VEC_LENGTH (packed_args
);
19230 /* Determine the parameter packs we will be deducing from the
19231 pattern, and record their current deductions. */
19232 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
19233 pack
; pack
= TREE_CHAIN (pack
))
19235 tree parm_pack
= TREE_VALUE (pack
);
19238 /* Determine the index and level of this parameter pack. */
19239 template_parm_level_and_index (parm_pack
, &level
, &idx
);
19241 /* Keep track of the parameter packs and their corresponding
19243 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
19244 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
19247 /* Loop through all of the arguments that have not yet been
19248 unified and unify each with the pattern. */
19249 for (i
= start
; i
< len
; i
++)
19252 bool any_explicit
= false;
19253 tree arg
= TREE_VEC_ELT (packed_args
, i
);
19255 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19256 or the element of its argument pack at the current index if
19257 this argument was explicitly specified. */
19258 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19262 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19265 if (TREE_VALUE (pack
)
19266 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
19267 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
19269 any_explicit
= true;
19270 arg
= TREE_VEC_ELT (pargs
, i
- start
);
19272 TMPL_ARG (targs
, level
, idx
) = arg
;
19275 /* If we had explicit template arguments, substitute them into the
19276 pattern before deduction. */
19279 /* Some arguments might still be unspecified or dependent. */
19281 ++processing_template_decl
;
19282 dependent
= any_dependent_template_arguments_p (targs
);
19284 --processing_template_decl
;
19285 parm
= tsubst (pattern
, targs
,
19286 explain_p
? tf_warning_or_error
: tf_none
,
19289 --processing_template_decl
;
19290 if (parm
== error_mark_node
)
19296 /* Unify the pattern with the current argument. */
19297 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
19301 /* For each parameter pack, collect the deduced value. */
19302 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19305 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19307 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
19308 TMPL_ARG (targs
, level
, idx
);
19312 /* Verify that the results of unification with the parameter packs
19313 produce results consistent with what we've seen before, and make
19314 the deduced argument packs available. */
19315 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19317 tree old_pack
= TREE_VALUE (pack
);
19318 tree new_args
= TREE_TYPE (pack
);
19319 int i
, len
= TREE_VEC_LENGTH (new_args
);
19321 bool nondeduced_p
= false;
19323 /* By default keep the original deduced argument pack.
19324 If necessary, more specific code is going to update the
19325 resulting deduced argument later down in this function. */
19326 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19327 TMPL_ARG (targs
, level
, idx
) = old_pack
;
19329 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19330 actually deduce anything. */
19331 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
19332 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
19333 nondeduced_p
= true;
19337 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
19339 /* If we had fewer function args than explicit template args,
19340 just use the explicits. */
19341 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19342 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
19343 if (len
< explicit_len
)
19344 new_args
= explicit_args
;
19350 /* Build the deduced *_ARGUMENT_PACK. */
19351 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
19353 result
= make_node (NONTYPE_ARGUMENT_PACK
);
19354 TREE_TYPE (result
) =
19355 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
19356 TREE_CONSTANT (result
) = 1;
19359 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19361 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
19363 /* Note the deduced argument packs for this parameter
19365 TMPL_ARG (targs
, level
, idx
) = result
;
19367 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
19368 && (ARGUMENT_PACK_ARGS (old_pack
)
19369 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
19371 /* We only had the explicitly-provided arguments before, but
19372 now we have a complete set of arguments. */
19373 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19375 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
19376 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
19377 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
19381 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
19382 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
19384 if (!comp_template_args (old_args
, new_args
,
19385 &bad_old_arg
, &bad_new_arg
))
19386 /* Inconsistent unification of this parameter pack. */
19387 return unify_parameter_pack_inconsistent (explain_p
,
19393 return unify_success (explain_p
);
19396 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19397 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19398 parameters and return value are as for unify. */
19401 unify_array_domain (tree tparms
, tree targs
,
19402 tree parm_dom
, tree arg_dom
,
19410 /* Our representation of array types uses "N - 1" as the
19411 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19412 not an integer constant. We cannot unify arbitrarily
19413 complex expressions, so we eliminate the MINUS_EXPRs
19415 parm_max
= TYPE_MAX_VALUE (parm_dom
);
19416 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
19419 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
19420 parm_max
= TREE_OPERAND (parm_max
, 0);
19422 arg_max
= TYPE_MAX_VALUE (arg_dom
);
19423 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
19426 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19427 trying to unify the type of a variable with the type
19428 of a template parameter. For example:
19430 template <unsigned int N>
19431 void f (char (&) [N]);
19438 Here, the type of the ARG will be "int [g(i)]", and
19439 may be a SAVE_EXPR, etc. */
19440 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
19441 return unify_vla_arg (explain_p
, arg_dom
);
19442 arg_max
= TREE_OPERAND (arg_max
, 0);
19445 /* If only one of the bounds used a MINUS_EXPR, compensate
19446 by adding one to the other bound. */
19447 if (parm_cst
&& !arg_cst
)
19448 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19452 else if (arg_cst
&& !parm_cst
)
19453 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19458 return unify (tparms
, targs
, parm_max
, arg_max
,
19459 UNIFY_ALLOW_INTEGER
, explain_p
);
19462 /* Deduce the value of template parameters. TPARMS is the (innermost)
19463 set of template parameters to a template. TARGS is the bindings
19464 for those template parameters, as determined thus far; TARGS may
19465 include template arguments for outer levels of template parameters
19466 as well. PARM is a parameter to a template function, or a
19467 subcomponent of that parameter; ARG is the corresponding argument.
19468 This function attempts to match PARM with ARG in a manner
19469 consistent with the existing assignments in TARGS. If more values
19470 are deduced, then TARGS is updated.
19472 Returns 0 if the type deduction succeeds, 1 otherwise. The
19473 parameter STRICT is a bitwise or of the following flags:
19476 Require an exact match between PARM and ARG.
19477 UNIFY_ALLOW_MORE_CV_QUAL:
19478 Allow the deduced ARG to be more cv-qualified (by qualification
19479 conversion) than ARG.
19480 UNIFY_ALLOW_LESS_CV_QUAL:
19481 Allow the deduced ARG to be less cv-qualified than ARG.
19482 UNIFY_ALLOW_DERIVED:
19483 Allow the deduced ARG to be a template base class of ARG,
19484 or a pointer to a template base class of the type pointed to by
19486 UNIFY_ALLOW_INTEGER:
19487 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19488 case for more information.
19489 UNIFY_ALLOW_OUTER_LEVEL:
19490 This is the outermost level of a deduction. Used to determine validity
19491 of qualification conversions. A valid qualification conversion must
19492 have const qualified pointers leading up to the inner type which
19493 requires additional CV quals, except at the outer level, where const
19494 is not required [conv.qual]. It would be normal to set this flag in
19495 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19496 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19497 This is the outermost level of a deduction, and PARM can be more CV
19498 qualified at this point.
19499 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19500 This is the outermost level of a deduction, and PARM can be less CV
19501 qualified at this point. */
19504 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
19510 int strict_in
= strict
;
19512 /* I don't think this will do the right thing with respect to types.
19513 But the only case I've seen it in so far has been array bounds, where
19514 signedness is the only information lost, and I think that will be
19516 while (TREE_CODE (parm
) == NOP_EXPR
)
19517 parm
= TREE_OPERAND (parm
, 0);
19519 if (arg
== error_mark_node
)
19520 return unify_invalid (explain_p
);
19521 if (arg
== unknown_type_node
19522 || arg
== init_list_type_node
)
19523 /* We can't deduce anything from this, but we might get all the
19524 template args from other function args. */
19525 return unify_success (explain_p
);
19527 /* If PARM uses template parameters, then we can't bail out here,
19528 even if ARG == PARM, since we won't record unifications for the
19529 template parameters. We might need them if we're trying to
19530 figure out which of two things is more specialized. */
19531 if (arg
== parm
&& !uses_template_parms (parm
))
19532 return unify_success (explain_p
);
19534 /* Handle init lists early, so the rest of the function can assume
19535 we're dealing with a type. */
19536 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
19540 tree orig_parm
= parm
;
19542 /* Replace T with std::initializer_list<T> for deduction. */
19543 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19544 && flag_deduce_init_list
)
19545 parm
= listify (parm
);
19547 if (!is_std_init_list (parm
)
19548 && TREE_CODE (parm
) != ARRAY_TYPE
)
19549 /* We can only deduce from an initializer list argument if the
19550 parameter is std::initializer_list or an array; otherwise this
19551 is a non-deduced context. */
19552 return unify_success (explain_p
);
19554 if (TREE_CODE (parm
) == ARRAY_TYPE
)
19555 elttype
= TREE_TYPE (parm
);
19558 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
19559 /* Deduction is defined in terms of a single type, so just punt
19560 on the (bizarre) std::initializer_list<T...>. */
19561 if (PACK_EXPANSION_P (elttype
))
19562 return unify_success (explain_p
);
19565 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
19567 int elt_strict
= strict
;
19569 if (elt
== error_mark_node
)
19570 return unify_invalid (explain_p
);
19572 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
19574 tree type
= TREE_TYPE (elt
);
19575 if (type
== error_mark_node
)
19576 return unify_invalid (explain_p
);
19577 /* It should only be possible to get here for a call. */
19578 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
19579 elt_strict
|= maybe_adjust_types_for_deduction
19580 (DEDUCE_CALL
, &elttype
, &type
, elt
);
19584 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
19588 if (TREE_CODE (parm
) == ARRAY_TYPE
19589 && deducible_array_bound (TYPE_DOMAIN (parm
)))
19591 /* Also deduce from the length of the initializer list. */
19592 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
19593 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
19594 if (idx
== error_mark_node
)
19595 return unify_invalid (explain_p
);
19596 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19600 /* If the std::initializer_list<T> deduction worked, replace the
19601 deduced A with std::initializer_list<A>. */
19602 if (orig_parm
!= parm
)
19604 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
19605 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19606 targ
= listify (targ
);
19607 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
19609 return unify_success (explain_p
);
19612 /* Immediately reject some pairs that won't unify because of
19613 cv-qualification mismatches. */
19614 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19616 /* It is the elements of the array which hold the cv quals of an array
19617 type, and the elements might be template type parms. We'll check
19618 when we recurse. */
19619 && TREE_CODE (arg
) != ARRAY_TYPE
19620 /* We check the cv-qualifiers when unifying with template type
19621 parameters below. We want to allow ARG `const T' to unify with
19622 PARM `T' for example, when computing which of two templates
19623 is more specialized, for example. */
19624 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
19625 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
19626 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19628 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
19629 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
19630 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
19631 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
19632 strict
&= ~UNIFY_ALLOW_DERIVED
;
19633 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19634 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
19636 switch (TREE_CODE (parm
))
19638 case TYPENAME_TYPE
:
19640 case UNBOUND_CLASS_TEMPLATE
:
19641 /* In a type which contains a nested-name-specifier, template
19642 argument values cannot be deduced for template parameters used
19643 within the nested-name-specifier. */
19644 return unify_success (explain_p
);
19646 case TEMPLATE_TYPE_PARM
:
19647 case TEMPLATE_TEMPLATE_PARM
:
19648 case BOUND_TEMPLATE_TEMPLATE_PARM
:
19649 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19650 if (error_operand_p (tparm
))
19651 return unify_invalid (explain_p
);
19653 if (TEMPLATE_TYPE_LEVEL (parm
)
19654 != template_decl_level (tparm
))
19655 /* The PARM is not one we're trying to unify. Just check
19656 to see if it matches ARG. */
19658 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19659 && (is_auto (parm
) ? is_auto (arg
)
19660 : same_type_p (parm
, arg
)))
19661 return unify_success (explain_p
);
19663 return unify_type_mismatch (explain_p
, parm
, arg
);
19665 idx
= TEMPLATE_TYPE_IDX (parm
);
19666 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19667 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
19668 if (error_operand_p (tparm
))
19669 return unify_invalid (explain_p
);
19671 /* Check for mixed types and values. */
19672 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19673 && TREE_CODE (tparm
) != TYPE_DECL
)
19674 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19675 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
19676 gcc_unreachable ();
19678 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19680 /* ARG must be constructed from a template class or a template
19681 template parameter. */
19682 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
19683 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
19684 return unify_template_deduction_failure (explain_p
, parm
, arg
);
19686 tree parmvec
= TYPE_TI_ARGS (parm
);
19687 /* An alias template name is never deduced. */
19688 if (TYPE_ALIAS_P (arg
))
19689 arg
= strip_typedefs (arg
);
19690 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
19691 tree full_argvec
= add_to_template_args (targs
, argvec
);
19693 = DECL_INNERMOST_TEMPLATE_PARMS
19694 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
19696 int parm_variadic_p
= 0;
19698 /* The resolution to DR150 makes clear that default
19699 arguments for an N-argument may not be used to bind T
19700 to a template template parameter with fewer than N
19701 parameters. It is not safe to permit the binding of
19702 default arguments as an extension, as that may change
19703 the meaning of a conforming program. Consider:
19705 struct Dense { static const unsigned int dim = 1; };
19707 template <template <typename> class View,
19709 void operator+(float, View<Block> const&);
19711 template <typename Block,
19712 unsigned int Dim = Block::dim>
19713 struct Lvalue_proxy { operator float() const; };
19717 Lvalue_proxy<Dense> p;
19722 Here, if Lvalue_proxy is permitted to bind to View, then
19723 the global operator+ will be used; if they are not, the
19724 Lvalue_proxy will be converted to float. */
19725 if (coerce_template_parms (parm_parms
,
19727 TYPE_TI_TEMPLATE (parm
),
19729 ? tf_warning_or_error
19731 /*require_all_args=*/true,
19732 /*use_default_args=*/false)
19733 == error_mark_node
)
19736 /* Deduce arguments T, i from TT<T> or TT<i>.
19737 We check each element of PARMVEC and ARGVEC individually
19738 rather than the whole TREE_VEC since they can have
19739 different number of elements. */
19741 parmvec
= expand_template_argument_pack (parmvec
);
19742 argvec
= expand_template_argument_pack (argvec
);
19744 len
= TREE_VEC_LENGTH (parmvec
);
19746 /* Check if the parameters end in a pack, making them
19749 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
19750 parm_variadic_p
= 1;
19752 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19753 /* If the template argument list of P contains a pack
19754 expansion that is not the last template argument, the
19755 entire template argument list is a non-deduced
19757 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
19758 return unify_success (explain_p
);
19760 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
19761 return unify_too_few_arguments (explain_p
,
19762 TREE_VEC_LENGTH (argvec
), len
);
19764 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
19766 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
19767 TREE_VEC_ELT (parmvec
, i
),
19768 TREE_VEC_ELT (argvec
, i
),
19769 UNIFY_ALLOW_NONE
, explain_p
);
19772 if (parm_variadic_p
19773 && unify_pack_expansion (tparms
, targs
,
19776 /*subr=*/true, explain_p
))
19779 arg
= TYPE_TI_TEMPLATE (arg
);
19781 /* Fall through to deduce template name. */
19784 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19785 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19787 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19789 /* Simple cases: Value already set, does match or doesn't. */
19790 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
19791 return unify_success (explain_p
);
19793 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19797 /* If PARM is `const T' and ARG is only `int', we don't have
19798 a match unless we are allowing additional qualification.
19799 If ARG is `const int' and PARM is just `T' that's OK;
19800 that binds `const int' to `T'. */
19801 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
19803 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19805 /* Consider the case where ARG is `const volatile int' and
19806 PARM is `const T'. Then, T should be `volatile int'. */
19807 arg
= cp_build_qualified_type_real
19808 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
19809 if (arg
== error_mark_node
)
19810 return unify_invalid (explain_p
);
19812 /* Simple cases: Value already set, does match or doesn't. */
19813 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
19814 return unify_success (explain_p
);
19816 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
19818 /* Make sure that ARG is not a variable-sized array. (Note
19819 that were talking about variable-sized arrays (like
19820 `int[n]'), rather than arrays of unknown size (like
19821 `int[]').) We'll get very confused by such a type since
19822 the bound of the array is not constant, and therefore
19823 not mangleable. Besides, such types are not allowed in
19824 ISO C++, so we can do as we please here. We do allow
19825 them for 'auto' deduction, since that isn't ABI-exposed. */
19826 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
19827 return unify_vla_arg (explain_p
, arg
);
19829 /* Strip typedefs as in convert_template_argument. */
19830 arg
= canonicalize_type_argument (arg
, tf_none
);
19833 /* If ARG is a parameter pack or an expansion, we cannot unify
19834 against it unless PARM is also a parameter pack. */
19835 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19836 && !template_parameter_pack_p (parm
))
19837 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19839 /* If the argument deduction results is a METHOD_TYPE,
19840 then there is a problem.
19841 METHOD_TYPE doesn't map to any real C++ type the result of
19842 the deduction can not be of that type. */
19843 if (TREE_CODE (arg
) == METHOD_TYPE
)
19844 return unify_method_type_error (explain_p
, arg
);
19846 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19847 return unify_success (explain_p
);
19849 case TEMPLATE_PARM_INDEX
:
19850 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
19851 if (error_operand_p (tparm
))
19852 return unify_invalid (explain_p
);
19854 if (TEMPLATE_PARM_LEVEL (parm
)
19855 != template_decl_level (tparm
))
19857 /* The PARM is not one we're trying to unify. Just check
19858 to see if it matches ARG. */
19859 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
19860 && cp_tree_equal (parm
, arg
));
19862 unify_expression_unequal (explain_p
, parm
, arg
);
19866 idx
= TEMPLATE_PARM_IDX (parm
);
19867 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19871 int x
= !cp_tree_equal (targ
, arg
);
19873 unify_inconsistency (explain_p
, parm
, targ
, arg
);
19877 /* [temp.deduct.type] If, in the declaration of a function template
19878 with a non-type template-parameter, the non-type
19879 template-parameter is used in an expression in the function
19880 parameter-list and, if the corresponding template-argument is
19881 deduced, the template-argument type shall match the type of the
19882 template-parameter exactly, except that a template-argument
19883 deduced from an array bound may be of any integral type.
19884 The non-type parameter might use already deduced type parameters. */
19885 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
19886 if (!TREE_TYPE (arg
))
19887 /* Template-parameter dependent expression. Just accept it for now.
19888 It will later be processed in convert_template_argument. */
19890 else if (same_type_p (TREE_TYPE (arg
), tparm
))
19892 else if ((strict
& UNIFY_ALLOW_INTEGER
)
19893 && CP_INTEGRAL_TYPE_P (tparm
))
19894 /* Convert the ARG to the type of PARM; the deduced non-type
19895 template argument must exactly match the types of the
19896 corresponding parameter. */
19897 arg
= fold (build_nop (tparm
, arg
));
19898 else if (uses_template_parms (tparm
))
19899 /* We haven't deduced the type of this parameter yet. Try again
19901 return unify_success (explain_p
);
19903 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
19905 /* If ARG is a parameter pack or an expansion, we cannot unify
19906 against it unless PARM is also a parameter pack. */
19907 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
19908 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
19909 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
19912 bool removed_attr
= false;
19913 arg
= strip_typedefs_expr (arg
, &removed_attr
);
19915 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
19916 return unify_success (explain_p
);
19920 /* A pointer-to-member constant can be unified only with
19921 another constant. */
19922 if (TREE_CODE (arg
) != PTRMEM_CST
)
19923 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
19925 /* Just unify the class member. It would be useless (and possibly
19926 wrong, depending on the strict flags) to unify also
19927 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19928 arg refer to the same variable, even if through different
19929 classes. For instance:
19931 struct A { int x; };
19934 Unification of &A::x and &B::x must succeed. */
19935 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
19936 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
19941 if (!TYPE_PTR_P (arg
))
19942 return unify_type_mismatch (explain_p
, parm
, arg
);
19944 /* [temp.deduct.call]
19946 A can be another pointer or pointer to member type that can
19947 be converted to the deduced A via a qualification
19948 conversion (_conv.qual_).
19950 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19951 This will allow for additional cv-qualification of the
19952 pointed-to types if appropriate. */
19954 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
19955 /* The derived-to-base conversion only persists through one
19956 level of pointers. */
19957 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
19959 return unify (tparms
, targs
, TREE_TYPE (parm
),
19960 TREE_TYPE (arg
), strict
, explain_p
);
19963 case REFERENCE_TYPE
:
19964 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
19965 return unify_type_mismatch (explain_p
, parm
, arg
);
19966 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19967 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19970 if (TREE_CODE (arg
) != ARRAY_TYPE
)
19971 return unify_type_mismatch (explain_p
, parm
, arg
);
19972 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
19973 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
19974 return unify_type_mismatch (explain_p
, parm
, arg
);
19975 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19976 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
19977 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
19978 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19979 TYPE_DOMAIN (arg
), explain_p
);
19980 return unify_success (explain_p
);
19987 case ENUMERAL_TYPE
:
19990 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19991 return unify_type_mismatch (explain_p
, parm
, arg
);
19993 /* We have already checked cv-qualification at the top of the
19995 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
19996 return unify_type_mismatch (explain_p
, parm
, arg
);
19998 /* As far as unification is concerned, this wins. Later checks
19999 will invalidate it if necessary. */
20000 return unify_success (explain_p
);
20002 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20003 /* Type INTEGER_CST can come from ordinary constant template args. */
20005 while (TREE_CODE (arg
) == NOP_EXPR
)
20006 arg
= TREE_OPERAND (arg
, 0);
20008 if (TREE_CODE (arg
) != INTEGER_CST
)
20009 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20010 return (tree_int_cst_equal (parm
, arg
)
20011 ? unify_success (explain_p
)
20012 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
20016 int i
, len
, argslen
;
20017 int parm_variadic_p
= 0;
20019 if (TREE_CODE (arg
) != TREE_VEC
)
20020 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20022 len
= TREE_VEC_LENGTH (parm
);
20023 argslen
= TREE_VEC_LENGTH (arg
);
20025 /* Check for pack expansions in the parameters. */
20026 for (i
= 0; i
< len
; ++i
)
20028 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
20031 /* We can unify against something with a trailing
20033 parm_variadic_p
= 1;
20035 /* [temp.deduct.type]/9: If the template argument list of
20036 P contains a pack expansion that is not the last
20037 template argument, the entire template argument list
20038 is a non-deduced context. */
20039 return unify_success (explain_p
);
20043 /* If we don't have enough arguments to satisfy the parameters
20044 (not counting the pack expression at the end), or we have
20045 too many arguments for a parameter list that doesn't end in
20046 a pack expression, we can't unify. */
20047 if (parm_variadic_p
20048 ? argslen
< len
- parm_variadic_p
20050 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
20052 /* Unify all of the parameters that precede the (optional)
20053 pack expression. */
20054 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
20056 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20057 TREE_VEC_ELT (parm
, i
),
20058 TREE_VEC_ELT (arg
, i
),
20059 UNIFY_ALLOW_NONE
, explain_p
);
20061 if (parm_variadic_p
)
20062 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
20064 /*subr=*/true, explain_p
);
20065 return unify_success (explain_p
);
20070 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20071 return unify_type_mismatch (explain_p
, parm
, arg
);
20073 if (TYPE_PTRMEMFUNC_P (parm
))
20075 if (!TYPE_PTRMEMFUNC_P (arg
))
20076 return unify_type_mismatch (explain_p
, parm
, arg
);
20078 return unify (tparms
, targs
,
20079 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
20080 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
20081 strict
, explain_p
);
20083 else if (TYPE_PTRMEMFUNC_P (arg
))
20084 return unify_type_mismatch (explain_p
, parm
, arg
);
20086 if (CLASSTYPE_TEMPLATE_INFO (parm
))
20088 tree t
= NULL_TREE
;
20090 if (strict_in
& UNIFY_ALLOW_DERIVED
)
20092 /* First, we try to unify the PARM and ARG directly. */
20093 t
= try_class_unification (tparms
, targs
,
20094 parm
, arg
, explain_p
);
20098 /* Fallback to the special case allowed in
20099 [temp.deduct.call]:
20101 If P is a class, and P has the form
20102 template-id, then A can be a derived class of
20103 the deduced A. Likewise, if P is a pointer to
20104 a class of the form template-id, A can be a
20105 pointer to a derived class pointed to by the
20107 enum template_base_result r
;
20108 r
= get_template_base (tparms
, targs
, parm
, arg
,
20113 /* Don't give the derived diagnostic if we're
20114 already dealing with the same template. */
20116 = (CLASSTYPE_TEMPLATE_INFO (arg
)
20117 && (CLASSTYPE_TI_TEMPLATE (parm
)
20118 == CLASSTYPE_TI_TEMPLATE (arg
)));
20119 return unify_no_common_base (explain_p
&& !same_template
,
20124 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
20125 && (CLASSTYPE_TI_TEMPLATE (parm
)
20126 == CLASSTYPE_TI_TEMPLATE (arg
)))
20127 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20128 Then, we should unify `int' and `U'. */
20131 /* There's no chance of unification succeeding. */
20132 return unify_type_mismatch (explain_p
, parm
, arg
);
20134 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
20135 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
20137 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
20138 return unify_type_mismatch (explain_p
, parm
, arg
);
20139 return unify_success (explain_p
);
20142 case FUNCTION_TYPE
:
20144 unsigned int nargs
;
20149 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20150 return unify_type_mismatch (explain_p
, parm
, arg
);
20152 /* CV qualifications for methods can never be deduced, they must
20153 match exactly. We need to check them explicitly here,
20154 because type_unification_real treats them as any other
20155 cv-qualified parameter. */
20156 if (TREE_CODE (parm
) == METHOD_TYPE
20157 && (!check_cv_quals_for_unify
20159 class_of_this_parm (arg
),
20160 class_of_this_parm (parm
))))
20161 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20163 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
20164 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
20166 nargs
= list_length (TYPE_ARG_TYPES (arg
));
20167 args
= XALLOCAVEC (tree
, nargs
);
20168 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
20169 a
!= NULL_TREE
&& a
!= void_list_node
;
20170 a
= TREE_CHAIN (a
), ++i
)
20171 args
[i
] = TREE_VALUE (a
);
20174 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
20175 args
, nargs
, 1, DEDUCE_EXACT
,
20176 LOOKUP_NORMAL
, NULL
, explain_p
);
20180 /* Unify a pointer to member with a pointer to member function, which
20181 deduces the type of the member as a function type. */
20182 if (TYPE_PTRMEMFUNC_P (arg
))
20184 /* Check top-level cv qualifiers */
20185 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
20186 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20188 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20189 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
20190 UNIFY_ALLOW_NONE
, explain_p
);
20192 /* Determine the type of the function we are unifying against. */
20193 tree fntype
= static_fn_type (arg
);
20195 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
20198 if (TREE_CODE (arg
) != OFFSET_TYPE
)
20199 return unify_type_mismatch (explain_p
, parm
, arg
);
20200 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20201 TYPE_OFFSET_BASETYPE (arg
),
20202 UNIFY_ALLOW_NONE
, explain_p
);
20203 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20204 strict
, explain_p
);
20207 if (DECL_TEMPLATE_PARM_P (parm
))
20208 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
20209 if (arg
!= scalar_constant_value (parm
))
20210 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20211 return unify_success (explain_p
);
20214 case TEMPLATE_DECL
:
20215 /* Matched cases are handled by the ARG == PARM test above. */
20216 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20219 /* We might get a variable as a non-type template argument in parm if the
20220 corresponding parameter is type-dependent. Make any necessary
20221 adjustments based on whether arg is a reference. */
20222 if (CONSTANT_CLASS_P (arg
))
20223 parm
= fold_non_dependent_expr (parm
);
20224 else if (REFERENCE_REF_P (arg
))
20226 tree sub
= TREE_OPERAND (arg
, 0);
20228 if (TREE_CODE (sub
) == ADDR_EXPR
)
20229 arg
= TREE_OPERAND (sub
, 0);
20231 /* Now use the normal expression code to check whether they match. */
20234 case TYPE_ARGUMENT_PACK
:
20235 case NONTYPE_ARGUMENT_PACK
:
20236 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
20237 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
20240 case DECLTYPE_TYPE
:
20241 case UNDERLYING_TYPE
:
20242 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20243 or UNDERLYING_TYPE nodes. */
20244 return unify_success (explain_p
);
20247 /* Unification fails if we hit an error node. */
20248 return unify_invalid (explain_p
);
20251 if (REFERENCE_REF_P (parm
))
20253 if (REFERENCE_REF_P (arg
))
20254 arg
= TREE_OPERAND (arg
, 0);
20255 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
20256 strict
, explain_p
);
20261 /* An unresolved overload is a nondeduced context. */
20262 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
20263 return unify_success (explain_p
);
20264 gcc_assert (EXPR_P (parm
));
20266 /* We must be looking at an expression. This can happen with
20270 void foo(S<I>, S<I + 2>);
20272 This is a "nondeduced context":
20276 The nondeduced contexts are:
20278 --A type that is a template-id in which one or more of
20279 the template-arguments is an expression that references
20280 a template-parameter.
20282 In these cases, we assume deduction succeeded, but don't
20283 actually infer any unifications. */
20285 if (!uses_template_parms (parm
)
20286 && !template_args_equal (parm
, arg
))
20287 return unify_expression_unequal (explain_p
, parm
, arg
);
20289 return unify_success (explain_p
);
20292 #undef RECUR_AND_CHECK_FAILURE
20294 /* Note that DECL can be defined in this translation unit, if
20298 mark_definable (tree decl
)
20301 DECL_NOT_REALLY_EXTERN (decl
) = 1;
20302 FOR_EACH_CLONE (clone
, decl
)
20303 DECL_NOT_REALLY_EXTERN (clone
) = 1;
20306 /* Called if RESULT is explicitly instantiated, or is a member of an
20307 explicitly instantiated class. */
20310 mark_decl_instantiated (tree result
, int extern_p
)
20312 SET_DECL_EXPLICIT_INSTANTIATION (result
);
20314 /* If this entity has already been written out, it's too late to
20315 make any modifications. */
20316 if (TREE_ASM_WRITTEN (result
))
20319 /* For anonymous namespace we don't need to do anything. */
20320 if (decl_anon_ns_mem_p (result
))
20322 gcc_assert (!TREE_PUBLIC (result
));
20326 if (TREE_CODE (result
) != FUNCTION_DECL
)
20327 /* The TREE_PUBLIC flag for function declarations will have been
20328 set correctly by tsubst. */
20329 TREE_PUBLIC (result
) = 1;
20331 /* This might have been set by an earlier implicit instantiation. */
20332 DECL_COMDAT (result
) = 0;
20335 DECL_NOT_REALLY_EXTERN (result
) = 0;
20338 mark_definable (result
);
20339 mark_needed (result
);
20340 /* Always make artificials weak. */
20341 if (DECL_ARTIFICIAL (result
) && flag_weak
)
20342 comdat_linkage (result
);
20343 /* For WIN32 we also want to put explicit instantiations in
20344 linkonce sections. */
20345 else if (TREE_PUBLIC (result
))
20346 maybe_make_one_only (result
);
20349 /* If EXTERN_P, then this function will not be emitted -- unless
20350 followed by an explicit instantiation, at which point its linkage
20351 will be adjusted. If !EXTERN_P, then this function will be
20352 emitted here. In neither circumstance do we want
20353 import_export_decl to adjust the linkage. */
20354 DECL_INTERFACE_KNOWN (result
) = 1;
20357 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20358 important template arguments. If any are missing, we check whether
20359 they're important by using error_mark_node for substituting into any
20360 args that were used for partial ordering (the ones between ARGS and END)
20361 and seeing if it bubbles up. */
20364 check_undeduced_parms (tree targs
, tree args
, tree end
)
20366 bool found
= false;
20368 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
20369 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
20372 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
20376 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
20377 if (substed
== error_mark_node
)
20383 /* Given two function templates PAT1 and PAT2, return:
20385 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20386 -1 if PAT2 is more specialized than PAT1.
20387 0 if neither is more specialized.
20389 LEN indicates the number of parameters we should consider
20390 (defaulted parameters should not be considered).
20392 The 1998 std underspecified function template partial ordering, and
20393 DR214 addresses the issue. We take pairs of arguments, one from
20394 each of the templates, and deduce them against each other. One of
20395 the templates will be more specialized if all the *other*
20396 template's arguments deduce against its arguments and at least one
20397 of its arguments *does* *not* deduce against the other template's
20398 corresponding argument. Deduction is done as for class templates.
20399 The arguments used in deduction have reference and top level cv
20400 qualifiers removed. Iff both arguments were originally reference
20401 types *and* deduction succeeds in both directions, an lvalue reference
20402 wins against an rvalue reference and otherwise the template
20403 with the more cv-qualified argument wins for that pairing (if
20404 neither is more cv-qualified, they both are equal). Unlike regular
20405 deduction, after all the arguments have been deduced in this way,
20406 we do *not* verify the deduced template argument values can be
20407 substituted into non-deduced contexts.
20409 The logic can be a bit confusing here, because we look at deduce1 and
20410 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20411 can find template arguments for pat1 to make arg1 look like arg2, that
20412 means that arg2 is at least as specialized as arg1. */
20415 more_specialized_fn (tree pat1
, tree pat2
, int len
)
20417 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
20418 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
20419 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
20420 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
20421 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
20422 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
20423 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
20424 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
20425 tree origs1
, origs2
;
20426 bool lose1
= false;
20427 bool lose2
= false;
20429 /* Remove the this parameter from non-static member functions. If
20430 one is a non-static member function and the other is not a static
20431 member function, remove the first parameter from that function
20432 also. This situation occurs for operator functions where we
20433 locate both a member function (with this pointer) and non-member
20434 operator (with explicit first operand). */
20435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
20437 len
--; /* LEN is the number of significant arguments for DECL1 */
20438 args1
= TREE_CHAIN (args1
);
20439 if (!DECL_STATIC_FUNCTION_P (decl2
))
20440 args2
= TREE_CHAIN (args2
);
20442 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
20444 args2
= TREE_CHAIN (args2
);
20445 if (!DECL_STATIC_FUNCTION_P (decl1
))
20448 args1
= TREE_CHAIN (args1
);
20452 /* If only one is a conversion operator, they are unordered. */
20453 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
20456 /* Consider the return type for a conversion function */
20457 if (DECL_CONV_FN_P (decl1
))
20459 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
20460 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
20464 processing_template_decl
++;
20470 /* Stop when an ellipsis is seen. */
20471 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
20473 tree arg1
= TREE_VALUE (args1
);
20474 tree arg2
= TREE_VALUE (args2
);
20475 int deduce1
, deduce2
;
20481 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20482 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20484 /* When both arguments are pack expansions, we need only
20485 unify the patterns themselves. */
20486 arg1
= PACK_EXPANSION_PATTERN (arg1
);
20487 arg2
= PACK_EXPANSION_PATTERN (arg2
);
20489 /* This is the last comparison we need to do. */
20493 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
20495 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
20496 arg1
= TREE_TYPE (arg1
);
20497 quals1
= cp_type_quals (arg1
);
20500 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
20502 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
20503 arg2
= TREE_TYPE (arg2
);
20504 quals2
= cp_type_quals (arg2
);
20507 arg1
= TYPE_MAIN_VARIANT (arg1
);
20508 arg2
= TYPE_MAIN_VARIANT (arg2
);
20510 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
20512 int i
, len2
= remaining_arguments (args2
);
20513 tree parmvec
= make_tree_vec (1);
20514 tree argvec
= make_tree_vec (len2
);
20517 /* Setup the parameter vector, which contains only ARG1. */
20518 TREE_VEC_ELT (parmvec
, 0) = arg1
;
20520 /* Setup the argument vector, which contains the remaining
20522 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
20523 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20525 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
20526 argvec
, DEDUCE_EXACT
,
20527 /*subr=*/true, /*explain_p=*/false)
20530 /* We cannot deduce in the other direction, because ARG1 is
20531 a pack expansion but ARG2 is not. */
20534 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20536 int i
, len1
= remaining_arguments (args1
);
20537 tree parmvec
= make_tree_vec (1);
20538 tree argvec
= make_tree_vec (len1
);
20541 /* Setup the parameter vector, which contains only ARG1. */
20542 TREE_VEC_ELT (parmvec
, 0) = arg2
;
20544 /* Setup the argument vector, which contains the remaining
20546 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
20547 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20549 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
20550 argvec
, DEDUCE_EXACT
,
20551 /*subr=*/true, /*explain_p=*/false)
20554 /* We cannot deduce in the other direction, because ARG2 is
20555 a pack expansion but ARG1 is not.*/
20561 /* The normal case, where neither argument is a pack
20563 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
20564 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20566 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
20567 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20571 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20572 arg2, then arg2 is not as specialized as arg1. */
20578 /* "If, for a given type, deduction succeeds in both directions
20579 (i.e., the types are identical after the transformations above)
20580 and both P and A were reference types (before being replaced with
20581 the type referred to above):
20582 - if the type from the argument template was an lvalue reference and
20583 the type from the parameter template was not, the argument type is
20584 considered to be more specialized than the other; otherwise,
20585 - if the type from the argument template is more cv-qualified
20586 than the type from the parameter template (as described above),
20587 the argument type is considered to be more specialized than the other;
20589 - neither type is more specialized than the other." */
20591 if (deduce1
&& deduce2
)
20593 if (ref1
&& ref2
&& ref1
!= ref2
)
20600 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
20602 if ((quals1
& quals2
) == quals2
)
20604 if ((quals1
& quals2
) == quals1
)
20609 if (lose1
&& lose2
)
20610 /* We've failed to deduce something in either direction.
20611 These must be unordered. */
20614 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20615 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20616 /* We have already processed all of the arguments in our
20617 handing of the pack expansion type. */
20620 args1
= TREE_CHAIN (args1
);
20621 args2
= TREE_CHAIN (args2
);
20624 /* "In most cases, all template parameters must have values in order for
20625 deduction to succeed, but for partial ordering purposes a template
20626 parameter may remain without a value provided it is not used in the
20627 types being used for partial ordering."
20629 Thus, if we are missing any of the targs1 we need to substitute into
20630 origs1, then pat2 is not as specialized as pat1. This can happen when
20631 there is a nondeduced context. */
20632 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
20634 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
20637 processing_template_decl
--;
20639 /* If both deductions succeed, the partial ordering selects the more
20640 constrained template. */
20641 if (!lose1
&& !lose2
)
20643 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
20644 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
20645 lose1
= !subsumes_constraints (c1
, c2
);
20646 lose2
= !subsumes_constraints (c2
, c1
);
20649 /* All things being equal, if the next argument is a pack expansion
20650 for one function but not for the other, prefer the
20651 non-variadic function. FIXME this is bogus; see c++/41958. */
20653 && args1
&& TREE_VALUE (args1
)
20654 && args2
&& TREE_VALUE (args2
))
20656 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
20657 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
20660 if (lose1
== lose2
)
20668 /* Determine which of two partial specializations of TMPL is more
20671 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20672 to the first partial specialization. The TREE_PURPOSE is the
20673 innermost set of template parameters for the partial
20674 specialization. PAT2 is similar, but for the second template.
20676 Return 1 if the first partial specialization is more specialized;
20677 -1 if the second is more specialized; 0 if neither is more
20680 See [temp.class.order] for information about determining which of
20681 two templates is more specialized. */
20684 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
20688 bool any_deductions
= false;
20690 tree tmpl1
= TREE_VALUE (pat1
);
20691 tree tmpl2
= TREE_VALUE (pat2
);
20692 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
20693 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
20695 /* Just like what happens for functions, if we are ordering between
20696 different template specializations, we may encounter dependent
20697 types in the arguments, and we need our dependency check functions
20698 to behave correctly. */
20699 ++processing_template_decl
;
20700 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
20704 any_deductions
= true;
20707 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
20711 any_deductions
= true;
20713 --processing_template_decl
;
20715 /* If both deductions succeed, the partial ordering selects the more
20716 constrained template. */
20717 if (!winner
&& any_deductions
)
20718 return more_constrained (tmpl1
, tmpl2
);
20720 /* In the case of a tie where at least one of the templates
20721 has a parameter pack at the end, the template with the most
20722 non-packed parameters wins. */
20725 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
20726 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
20728 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
20729 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
20730 int len1
= TREE_VEC_LENGTH (args1
);
20731 int len2
= TREE_VEC_LENGTH (args2
);
20733 /* We don't count the pack expansion at the end. */
20734 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
20736 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
20741 else if (len1
< len2
)
20748 /* Return the template arguments that will produce the function signature
20749 DECL from the function template FN, with the explicit template
20750 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20751 also match. Return NULL_TREE if no satisfactory arguments could be
20755 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
20757 int ntparms
= DECL_NTPARMS (fn
);
20758 tree targs
= make_tree_vec (ntparms
);
20759 tree decl_type
= TREE_TYPE (decl
);
20760 tree decl_arg_types
;
20762 unsigned int nargs
, ix
;
20765 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
20767 /* Never do unification on the 'this' parameter. */
20768 decl_arg_types
= skip_artificial_parms_for (decl
,
20769 TYPE_ARG_TYPES (decl_type
));
20771 nargs
= list_length (decl_arg_types
);
20772 args
= XALLOCAVEC (tree
, nargs
);
20773 for (arg
= decl_arg_types
, ix
= 0;
20774 arg
!= NULL_TREE
&& arg
!= void_list_node
;
20775 arg
= TREE_CHAIN (arg
), ++ix
)
20776 args
[ix
] = TREE_VALUE (arg
);
20778 if (fn_type_unification (fn
, explicit_args
, targs
,
20780 (check_rettype
|| DECL_CONV_FN_P (fn
)
20781 ? TREE_TYPE (decl_type
) : NULL_TREE
),
20782 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
20784 == error_mark_node
)
20790 /* Return the innermost template arguments that, when applied to a partial
20791 specialization SPEC_TMPL of TMPL, yield the ARGS.
20793 For example, suppose we have:
20795 template <class T, class U> struct S {};
20796 template <class T> struct S<T*, int> {};
20798 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
20799 partial specialization and the ARGS will be {double*, int}. The resulting
20800 vector will be {double}, indicating that `T' is bound to `double'. */
20803 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
20805 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
20807 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
20808 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
20810 tree innermost_deduced_args
;
20812 innermost_deduced_args
= make_tree_vec (ntparms
);
20813 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
20815 deduced_args
= copy_node (args
);
20816 SET_TMPL_ARGS_LEVEL (deduced_args
,
20817 TMPL_ARGS_DEPTH (deduced_args
),
20818 innermost_deduced_args
);
20821 deduced_args
= innermost_deduced_args
;
20823 if (unify (tparms
, deduced_args
,
20824 INNERMOST_TEMPLATE_ARGS (spec_args
),
20825 INNERMOST_TEMPLATE_ARGS (args
),
20826 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
20829 for (i
= 0; i
< ntparms
; ++i
)
20830 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
20833 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
20834 if (!push_tinst_level (tinst
))
20836 excessive_deduction_depth
= true;
20840 /* Verify that nondeduced template arguments agree with the type
20841 obtained from argument deduction.
20845 struct A { typedef int X; };
20846 template <class T, class U> struct C {};
20847 template <class T> struct C<T, typename T::X> {};
20849 Then with the instantiation `C<A, int>', we can deduce that
20850 `T' is `A' but unify () does not check whether `typename T::X'
20852 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
20853 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
20855 tf_none
, false, false);
20857 pop_tinst_level ();
20859 if (spec_args
== error_mark_node
20860 /* We only need to check the innermost arguments; the other
20861 arguments will always agree. */
20862 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
20863 INNERMOST_TEMPLATE_ARGS (args
)))
20866 /* Now that we have bindings for all of the template arguments,
20867 ensure that the arguments deduced for the template template
20868 parameters have compatible template parameter lists. See the use
20869 of template_template_parm_bindings_ok_p in fn_type_unification
20870 for more information. */
20871 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
20874 return deduced_args
;
20877 // Compare two function templates T1 and T2 by deducing bindings
20878 // from one against the other. If both deductions succeed, compare
20879 // constraints to see which is more constrained.
20881 more_specialized_inst (tree t1
, tree t2
)
20886 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
20892 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
20898 // If both deductions succeed, then one may be more constrained.
20899 if (count
== 2 && fate
== 0)
20900 fate
= more_constrained (t1
, t2
);
20905 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20906 Return the TREE_LIST node with the most specialized template, if
20907 any. If there is no most specialized template, the error_mark_node
20910 Note that this function does not look at, or modify, the
20911 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20912 returned is one of the elements of INSTANTIATIONS, callers may
20913 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20914 and retrieve it from the value returned. */
20917 most_specialized_instantiation (tree templates
)
20921 ++processing_template_decl
;
20924 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
20926 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
20931 /* Equally specialized, move to next function. If there
20932 is no next function, nothing's most specialized. */
20933 fn
= TREE_CHAIN (fn
);
20941 /* Now verify that champ is better than everything earlier in the
20942 instantiation list. */
20943 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
20944 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
20951 processing_template_decl
--;
20954 return error_mark_node
;
20959 /* If DECL is a specialization of some template, return the most
20960 general such template. Otherwise, returns NULL_TREE.
20962 For example, given:
20964 template <class T> struct S { template <class U> void f(U); };
20966 if TMPL is `template <class U> void S<int>::f(U)' this will return
20967 the full template. This function will not trace past partial
20968 specializations, however. For example, given in addition:
20970 template <class T> struct S<T*> { template <class U> void f(U); };
20972 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20973 `template <class T> template <class U> S<T*>::f(U)'. */
20976 most_general_template (tree decl
)
20978 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20980 if (tree tinfo
= get_template_info (decl
))
20981 decl
= TI_TEMPLATE (tinfo
);
20982 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20983 template friend, or a FIELD_DECL for a capture pack. */
20984 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
20988 /* Look for more and more general templates. */
20989 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
20991 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20992 (See cp-tree.h for details.) */
20993 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
20996 if (CLASS_TYPE_P (TREE_TYPE (decl
))
20997 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
20998 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
21001 /* Stop if we run into an explicitly specialized class template. */
21002 if (!DECL_NAMESPACE_SCOPE_P (decl
)
21003 && DECL_CONTEXT (decl
)
21004 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
21007 decl
= DECL_TI_TEMPLATE (decl
);
21013 /* Return the most specialized of the template partial specializations
21014 which can produce TARGET, a specialization of some class or variable
21015 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21016 a TEMPLATE_DECL node corresponding to the partial specialization, while
21017 the TREE_PURPOSE is the set of template arguments that must be
21018 substituted into the template pattern in order to generate TARGET.
21020 If the choice of partial specialization is ambiguous, a diagnostic
21021 is issued, and the error_mark_node is returned. If there are no
21022 partial specializations matching TARGET, then NULL_TREE is
21023 returned, indicating that the primary template should be used. */
21026 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
21028 tree list
= NULL_TREE
;
21033 tree outer_args
= NULL_TREE
;
21036 if (TYPE_P (target
))
21038 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
21039 tmpl
= TI_TEMPLATE (tinfo
);
21040 args
= TI_ARGS (tinfo
);
21042 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
21044 tmpl
= TREE_OPERAND (target
, 0);
21045 args
= TREE_OPERAND (target
, 1);
21047 else if (VAR_P (target
))
21049 tree tinfo
= DECL_TEMPLATE_INFO (target
);
21050 tmpl
= TI_TEMPLATE (tinfo
);
21051 args
= TI_ARGS (tinfo
);
21054 gcc_unreachable ();
21056 tree main_tmpl
= most_general_template (tmpl
);
21058 /* For determining which partial specialization to use, only the
21059 innermost args are interesting. */
21060 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21062 outer_args
= strip_innermost_template_args (args
, 1);
21063 args
= INNERMOST_TEMPLATE_ARGS (args
);
21066 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
21069 tree spec_tmpl
= TREE_VALUE (t
);
21073 /* Substitute in the template args from the enclosing class. */
21074 ++processing_template_decl
;
21075 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
21076 --processing_template_decl
;
21079 if (spec_tmpl
== error_mark_node
)
21080 return error_mark_node
;
21082 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
21086 spec_args
= add_to_template_args (outer_args
, spec_args
);
21088 /* Keep the candidate only if the constraints are satisfied,
21089 or if we're not compiling with concepts. */
21091 || constraints_satisfied_p (spec_tmpl
, spec_args
))
21093 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
21094 TREE_TYPE (list
) = TREE_TYPE (t
);
21102 ambiguous_p
= false;
21105 t
= TREE_CHAIN (t
);
21106 for (; t
; t
= TREE_CHAIN (t
))
21108 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21115 t
= TREE_CHAIN (t
);
21118 ambiguous_p
= true;
21127 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
21129 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21132 ambiguous_p
= true;
21140 char *spaces
= NULL
;
21141 if (!(complain
& tf_error
))
21142 return error_mark_node
;
21143 if (TYPE_P (target
))
21144 error ("ambiguous template instantiation for %q#T", target
);
21146 error ("ambiguous template instantiation for %q#D", target
);
21147 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
21148 for (t
= list
; t
; t
= TREE_CHAIN (t
))
21150 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
21151 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
21152 "%s %#S", spaces
? spaces
: str
, subst
);
21153 spaces
= spaces
? spaces
: get_spaces (str
);
21156 return error_mark_node
;
21162 /* Explicitly instantiate DECL. */
21165 do_decl_instantiation (tree decl
, tree storage
)
21167 tree result
= NULL_TREE
;
21170 if (!decl
|| decl
== error_mark_node
)
21171 /* An error occurred, for which grokdeclarator has already issued
21172 an appropriate message. */
21174 else if (! DECL_LANG_SPECIFIC (decl
))
21176 error ("explicit instantiation of non-template %q#D", decl
);
21180 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
21181 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
21183 if (VAR_P (decl
) && !var_templ
)
21185 /* There is an asymmetry here in the way VAR_DECLs and
21186 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21187 the latter, the DECL we get back will be marked as a
21188 template instantiation, and the appropriate
21189 DECL_TEMPLATE_INFO will be set up. This does not happen for
21190 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21191 should handle VAR_DECLs as it currently handles
21193 if (!DECL_CLASS_SCOPE_P (decl
))
21195 error ("%qD is not a static data member of a class template", decl
);
21198 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
21199 if (!result
|| !VAR_P (result
))
21201 error ("no matching template for %qD found", decl
);
21204 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
21206 error ("type %qT for explicit instantiation %qD does not match "
21207 "declared type %qT", TREE_TYPE (result
), decl
,
21212 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
21214 error ("explicit instantiation of %q#D", decl
);
21220 /* Check for various error cases. Note that if the explicit
21221 instantiation is valid the RESULT will currently be marked as an
21222 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21223 until we get here. */
21225 if (DECL_TEMPLATE_SPECIALIZATION (result
))
21227 /* DR 259 [temp.spec].
21229 Both an explicit instantiation and a declaration of an explicit
21230 specialization shall not appear in a program unless the explicit
21231 instantiation follows a declaration of the explicit specialization.
21233 For a given set of template parameters, if an explicit
21234 instantiation of a template appears after a declaration of an
21235 explicit specialization for that template, the explicit
21236 instantiation has no effect. */
21239 else if (DECL_EXPLICIT_INSTANTIATION (result
))
21243 No program shall explicitly instantiate any template more
21246 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21247 the first instantiation was `extern' and the second is not,
21248 and EXTERN_P for the opposite case. */
21249 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
21250 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
21251 /* If an "extern" explicit instantiation follows an ordinary
21252 explicit instantiation, the template is instantiated. */
21256 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
21258 error ("no matching template for %qD found", result
);
21261 else if (!DECL_TEMPLATE_INFO (result
))
21263 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
21267 if (storage
== NULL_TREE
)
21269 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21271 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
21272 pedwarn (input_location
, OPT_Wpedantic
,
21273 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21278 error ("storage class %qD applied to template instantiation", storage
);
21280 check_explicit_instantiation_namespace (result
);
21281 mark_decl_instantiated (result
, extern_p
);
21283 instantiate_decl (result
, /*defer_ok=*/1,
21284 /*expl_inst_class_mem_p=*/false);
21288 mark_class_instantiated (tree t
, int extern_p
)
21290 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
21291 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
21292 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
21293 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
21296 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
21297 rest_of_type_compilation (t
, 1);
21301 /* Called from do_type_instantiation through binding_table_foreach to
21302 do recursive instantiation for the type bound in ENTRY. */
21304 bt_instantiate_type_proc (binding_entry entry
, void *data
)
21306 tree storage
= *(tree
*) data
;
21308 if (MAYBE_CLASS_TYPE_P (entry
->type
)
21309 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
21310 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
21313 /* Called from do_type_instantiation to instantiate a member
21314 (a member function or a static member variable) of an
21315 explicitly instantiated class template. */
21317 instantiate_class_member (tree decl
, int extern_p
)
21319 mark_decl_instantiated (decl
, extern_p
);
21321 instantiate_decl (decl
, /*defer_ok=*/1,
21322 /*expl_inst_class_mem_p=*/true);
21325 /* Perform an explicit instantiation of template class T. STORAGE, if
21326 non-null, is the RID for extern, inline or static. COMPLAIN is
21327 nonzero if this is called from the parser, zero if called recursively,
21328 since the standard is unclear (as detailed below). */
21331 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
21336 int previous_instantiation_extern_p
= 0;
21338 if (TREE_CODE (t
) == TYPE_DECL
)
21341 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
21344 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
21346 error ("explicit instantiation of non-class template %qD", tmpl
);
21348 error ("explicit instantiation of non-template type %qT", t
);
21354 if (!COMPLETE_TYPE_P (t
))
21356 if (complain
& tf_error
)
21357 error ("explicit instantiation of %q#T before definition of template",
21362 if (storage
!= NULL_TREE
)
21364 if (!in_system_header_at (input_location
))
21366 if (storage
== ridpointers
[(int) RID_EXTERN
])
21368 if (cxx_dialect
== cxx98
)
21369 pedwarn (input_location
, OPT_Wpedantic
,
21370 "ISO C++ 1998 forbids the use of %<extern%> on "
21371 "explicit instantiations");
21374 pedwarn (input_location
, OPT_Wpedantic
,
21375 "ISO C++ forbids the use of %qE"
21376 " on explicit instantiations", storage
);
21379 if (storage
== ridpointers
[(int) RID_INLINE
])
21381 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21383 else if (storage
== ridpointers
[(int) RID_STATIC
])
21387 error ("storage class %qD applied to template instantiation",
21393 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
21395 /* DR 259 [temp.spec].
21397 Both an explicit instantiation and a declaration of an explicit
21398 specialization shall not appear in a program unless the explicit
21399 instantiation follows a declaration of the explicit specialization.
21401 For a given set of template parameters, if an explicit
21402 instantiation of a template appears after a declaration of an
21403 explicit specialization for that template, the explicit
21404 instantiation has no effect. */
21407 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
21411 No program shall explicitly instantiate any template more
21414 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21415 instantiation was `extern'. If EXTERN_P then the second is.
21416 These cases are OK. */
21417 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
21419 if (!previous_instantiation_extern_p
&& !extern_p
21420 && (complain
& tf_error
))
21421 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
21423 /* If we've already instantiated the template, just return now. */
21424 if (!CLASSTYPE_INTERFACE_ONLY (t
))
21428 check_explicit_instantiation_namespace (TYPE_NAME (t
));
21429 mark_class_instantiated (t
, extern_p
);
21437 /* In contrast to implicit instantiation, where only the
21438 declarations, and not the definitions, of members are
21439 instantiated, we have here:
21443 The explicit instantiation of a class template specialization
21444 implies the instantiation of all of its members not
21445 previously explicitly specialized in the translation unit
21446 containing the explicit instantiation.
21448 Of course, we can't instantiate member template classes, since
21449 we don't have any arguments for them. Note that the standard
21450 is unclear on whether the instantiation of the members are
21451 *explicit* instantiations or not. However, the most natural
21452 interpretation is that it should be an explicit instantiation. */
21455 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21456 if (TREE_CODE (tmp
) == FUNCTION_DECL
21457 && DECL_TEMPLATE_INSTANTIATION (tmp
))
21458 instantiate_class_member (tmp
, extern_p
);
21460 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21461 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
21462 instantiate_class_member (tmp
, extern_p
);
21464 if (CLASSTYPE_NESTED_UTDS (t
))
21465 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
21466 bt_instantiate_type_proc
, &storage
);
21470 /* Given a function DECL, which is a specialization of TMPL, modify
21471 DECL to be a re-instantiation of TMPL with the same template
21472 arguments. TMPL should be the template into which tsubst'ing
21473 should occur for DECL, not the most general template.
21475 One reason for doing this is a scenario like this:
21478 void f(const T&, int i);
21480 void g() { f(3, 7); }
21483 void f(const T& t, const int i) { }
21485 Note that when the template is first instantiated, with
21486 instantiate_template, the resulting DECL will have no name for the
21487 first parameter, and the wrong type for the second. So, when we go
21488 to instantiate the DECL, we regenerate it. */
21491 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
21493 /* The arguments used to instantiate DECL, from the most general
21497 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21499 /* Make sure that we can see identifiers, and compute access
21501 push_access_scope (decl
);
21503 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21511 args_depth
= TMPL_ARGS_DEPTH (args
);
21512 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
21513 if (args_depth
> parms_depth
)
21514 args
= get_innermost_template_args (args
, parms_depth
);
21516 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
21517 args
, tf_error
, NULL_TREE
,
21518 /*defer_ok*/false);
21519 if (specs
&& specs
!= error_mark_node
)
21520 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
21523 /* Merge parameter declarations. */
21524 decl_parm
= skip_artificial_parms_for (decl
,
21525 DECL_ARGUMENTS (decl
));
21527 = skip_artificial_parms_for (code_pattern
,
21528 DECL_ARGUMENTS (code_pattern
));
21529 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
21534 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21535 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
21536 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
21538 parm_type
= type_decays_to (parm_type
);
21539 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21540 TREE_TYPE (decl_parm
) = parm_type
;
21541 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21542 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21544 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21545 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21547 decl_parm
= DECL_CHAIN (decl_parm
);
21548 pattern_parm
= DECL_CHAIN (pattern_parm
);
21550 /* Merge any parameters that match with the function parameter
21552 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
21555 tree expanded_types
;
21556 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21557 the parameters in this function parameter pack. */
21558 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
21559 args
, tf_error
, NULL_TREE
);
21560 len
= TREE_VEC_LENGTH (expanded_types
);
21561 for (i
= 0; i
< len
; i
++)
21566 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21567 /* Rename the parameter to include the index. */
21568 DECL_NAME (decl_parm
) =
21569 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
21570 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
21571 parm_type
= type_decays_to (parm_type
);
21572 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21573 TREE_TYPE (decl_parm
) = parm_type
;
21574 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21575 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21577 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21578 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21580 decl_parm
= DECL_CHAIN (decl_parm
);
21583 /* Merge additional specifiers from the CODE_PATTERN. */
21584 if (DECL_DECLARED_INLINE_P (code_pattern
)
21585 && !DECL_DECLARED_INLINE_P (decl
))
21586 DECL_DECLARED_INLINE_P (decl
) = 1;
21588 else if (VAR_P (decl
))
21590 DECL_INITIAL (decl
) =
21591 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
21592 tf_error
, DECL_TI_TEMPLATE (decl
),
21593 /*integral_constant_expression_p=*/false);
21594 if (VAR_HAD_UNKNOWN_BOUND (decl
))
21595 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
21596 tf_error
, DECL_TI_TEMPLATE (decl
));
21599 gcc_unreachable ();
21601 pop_access_scope (decl
);
21604 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21605 substituted to get DECL. */
21608 template_for_substitution (tree decl
)
21610 tree tmpl
= DECL_TI_TEMPLATE (decl
);
21612 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21613 for the instantiation. This is not always the most general
21614 template. Consider, for example:
21617 struct S { template <class U> void f();
21618 template <> void f<int>(); };
21620 and an instantiation of S<double>::f<int>. We want TD to be the
21621 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21622 while (/* An instantiation cannot have a definition, so we need a
21623 more general template. */
21624 DECL_TEMPLATE_INSTANTIATION (tmpl
)
21625 /* We must also deal with friend templates. Given:
21627 template <class T> struct S {
21628 template <class U> friend void f() {};
21631 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21632 so far as the language is concerned, but that's still
21633 where we get the pattern for the instantiation from. On
21634 other hand, if the definition comes outside the class, say:
21636 template <class T> struct S {
21637 template <class U> friend void f();
21639 template <class U> friend void f() {}
21641 we don't need to look any further. That's what the check for
21642 DECL_INITIAL is for. */
21643 || (TREE_CODE (decl
) == FUNCTION_DECL
21644 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
21645 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
21647 /* The present template, TD, should not be a definition. If it
21648 were a definition, we should be using it! Note that we
21649 cannot restructure the loop to just keep going until we find
21650 a template with a definition, since that might go too far if
21651 a specialization was declared, but not defined. */
21653 /* Fetch the more general template. */
21654 tmpl
= DECL_TI_TEMPLATE (tmpl
);
21660 /* Returns true if we need to instantiate this template instance even if we
21661 know we aren't going to emit it. */
21664 always_instantiate_p (tree decl
)
21666 /* We always instantiate inline functions so that we can inline them. An
21667 explicit instantiation declaration prohibits implicit instantiation of
21668 non-inline functions. With high levels of optimization, we would
21669 normally inline non-inline functions -- but we're not allowed to do
21670 that for "extern template" functions. Therefore, we check
21671 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21672 return ((TREE_CODE (decl
) == FUNCTION_DECL
21673 && (DECL_DECLARED_INLINE_P (decl
)
21674 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
21675 /* And we need to instantiate static data members so that
21676 their initializers are available in integral constant
21679 && decl_maybe_constant_var_p (decl
)));
21682 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21683 instantiate it now, modifying TREE_TYPE (fn). */
21686 maybe_instantiate_noexcept (tree fn
)
21688 tree fntype
, spec
, noex
, clone
;
21690 /* Don't instantiate a noexcept-specification from template context. */
21691 if (processing_template_decl
)
21694 if (DECL_CLONED_FUNCTION_P (fn
))
21695 fn
= DECL_CLONED_FUNCTION (fn
);
21696 fntype
= TREE_TYPE (fn
);
21697 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
21699 if (!spec
|| !TREE_PURPOSE (spec
))
21702 noex
= TREE_PURPOSE (spec
);
21704 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
21706 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
21707 spec
= get_defaulted_eh_spec (fn
);
21708 else if (push_tinst_level (fn
))
21710 push_access_scope (fn
);
21711 push_deferring_access_checks (dk_no_deferred
);
21712 input_location
= DECL_SOURCE_LOCATION (fn
);
21713 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
21714 DEFERRED_NOEXCEPT_ARGS (noex
),
21715 tf_warning_or_error
, fn
,
21716 /*function_p=*/false,
21717 /*integral_constant_expression_p=*/true);
21718 pop_deferring_access_checks ();
21719 pop_access_scope (fn
);
21720 pop_tinst_level ();
21721 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
21722 if (spec
== error_mark_node
)
21723 spec
= noexcept_false_spec
;
21726 spec
= noexcept_false_spec
;
21728 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
21731 FOR_EACH_CLONE (clone
, fn
)
21733 if (TREE_TYPE (clone
) == fntype
)
21734 TREE_TYPE (clone
) = TREE_TYPE (fn
);
21736 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
21740 /* Produce the definition of D, a _DECL generated from a template. If
21741 DEFER_OK is nonzero, then we don't have to actually do the
21742 instantiation now; we just have to do it sometime. Normally it is
21743 an error if this is an explicit instantiation but D is undefined.
21744 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21745 explicitly instantiated class template. */
21748 instantiate_decl (tree d
, int defer_ok
,
21749 bool expl_inst_class_mem_p
)
21751 tree tmpl
= DECL_TI_TEMPLATE (d
);
21758 bool pattern_defined
;
21759 location_t saved_loc
= input_location
;
21760 int saved_unevaluated_operand
= cp_unevaluated_operand
;
21761 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
21765 bool nested
= false;
21767 /* This function should only be used to instantiate templates for
21768 functions and static member variables. */
21769 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
21771 /* A concept is never instantiated. */
21772 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
21774 /* Variables are never deferred; if instantiation is required, they
21775 are instantiated right away. That allows for better code in the
21776 case that an expression refers to the value of the variable --
21777 if the variable has a constant value the referring expression can
21778 take advantage of that fact. */
21780 || DECL_DECLARED_CONSTEXPR_P (d
))
21783 /* Don't instantiate cloned functions. Instead, instantiate the
21784 functions they cloned. */
21785 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
21786 d
= DECL_CLONED_FUNCTION (d
);
21788 if (DECL_TEMPLATE_INSTANTIATED (d
)
21789 || (TREE_CODE (d
) == FUNCTION_DECL
21790 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
21791 || DECL_TEMPLATE_SPECIALIZATION (d
))
21792 /* D has already been instantiated or explicitly specialized, so
21793 there's nothing for us to do here.
21795 It might seem reasonable to check whether or not D is an explicit
21796 instantiation, and, if so, stop here. But when an explicit
21797 instantiation is deferred until the end of the compilation,
21798 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21799 the instantiation. */
21802 /* Check to see whether we know that this template will be
21803 instantiated in some other file, as with "extern template"
21805 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
21807 /* In general, we do not instantiate such templates. */
21808 if (external_p
&& !always_instantiate_p (d
))
21811 gen_tmpl
= most_general_template (tmpl
);
21812 gen_args
= DECL_TI_ARGS (d
);
21814 if (tmpl
!= gen_tmpl
)
21815 /* We should already have the extra args. */
21816 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
21817 == TMPL_ARGS_DEPTH (gen_args
));
21818 /* And what's in the hash table should match D. */
21819 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
21820 || spec
== NULL_TREE
);
21822 /* This needs to happen before any tsubsting. */
21823 if (! push_tinst_level (d
))
21826 timevar_push (TV_TEMPLATE_INST
);
21828 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21829 for the instantiation. */
21830 td
= template_for_substitution (d
);
21835 /* Look up an explicit specialization, if any. */
21836 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
21837 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
21838 if (elt
&& elt
!= error_mark_node
)
21840 td
= TREE_VALUE (elt
);
21841 args
= TREE_PURPOSE (elt
);
21845 code_pattern
= DECL_TEMPLATE_RESULT (td
);
21847 /* We should never be trying to instantiate a member of a class
21848 template or partial specialization. */
21849 gcc_assert (d
!= code_pattern
);
21851 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
21852 || DECL_TEMPLATE_SPECIALIZATION (td
))
21853 /* In the case of a friend template whose definition is provided
21854 outside the class, we may have too many arguments. Drop the
21855 ones we don't need. The same is true for specializations. */
21856 args
= get_innermost_template_args
21857 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
21859 if (TREE_CODE (d
) == FUNCTION_DECL
)
21861 deleted_p
= DECL_DELETED_FN (code_pattern
);
21862 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
21863 && DECL_INITIAL (code_pattern
) != error_mark_node
)
21864 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
21870 if (DECL_CLASS_SCOPE_P (code_pattern
))
21871 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
21873 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
21876 /* We may be in the middle of deferred access check. Disable it now. */
21877 push_deferring_access_checks (dk_no_deferred
);
21879 /* Unless an explicit instantiation directive has already determined
21880 the linkage of D, remember that a definition is available for
21882 if (pattern_defined
21883 && !DECL_INTERFACE_KNOWN (d
)
21884 && !DECL_NOT_REALLY_EXTERN (d
))
21885 mark_definable (d
);
21887 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
21888 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
21889 input_location
= DECL_SOURCE_LOCATION (d
);
21891 /* If D is a member of an explicitly instantiated class template,
21892 and no definition is available, treat it like an implicit
21894 if (!pattern_defined
&& expl_inst_class_mem_p
21895 && DECL_EXPLICIT_INSTANTIATION (d
))
21897 /* Leave linkage flags alone on instantiations with anonymous
21899 if (TREE_PUBLIC (d
))
21901 DECL_NOT_REALLY_EXTERN (d
) = 0;
21902 DECL_INTERFACE_KNOWN (d
) = 0;
21904 SET_DECL_IMPLICIT_INSTANTIATION (d
);
21907 /* Defer all other templates, unless we have been explicitly
21908 forbidden from doing so. */
21909 if (/* If there is no definition, we cannot instantiate the
21912 /* If it's OK to postpone instantiation, do so. */
21914 /* If this is a static data member that will be defined
21915 elsewhere, we don't want to instantiate the entire data
21916 member, but we do want to instantiate the initializer so that
21917 we can substitute that elsewhere. */
21918 || (external_p
&& VAR_P (d
))
21919 /* Handle here a deleted function too, avoid generating
21920 its body (c++/61080). */
21923 /* The definition of the static data member is now required so
21924 we must substitute the initializer. */
21926 && !DECL_INITIAL (d
)
21927 && DECL_INITIAL (code_pattern
))
21931 bool const_init
= false;
21932 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
21934 ns
= decl_namespace_context (d
);
21935 push_nested_namespace (ns
);
21937 push_nested_class (DECL_CONTEXT (d
));
21938 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
21940 tf_warning_or_error
, NULL_TREE
,
21941 /*integral_constant_expression_p=*/false);
21942 /* If instantiating the initializer involved instantiating this
21943 again, don't call cp_finish_decl twice. */
21944 if (!DECL_INITIAL (d
))
21946 /* Make sure the initializer is still constant, in case of
21947 circular dependency (template/instantiate6.C). */
21949 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
21950 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
21951 /*asmspec_tree=*/NULL_TREE
,
21952 LOOKUP_ONLYCONVERTING
);
21955 pop_nested_class ();
21956 pop_nested_namespace (ns
);
21959 /* We restore the source position here because it's used by
21960 add_pending_template. */
21961 input_location
= saved_loc
;
21963 if (at_eof
&& !pattern_defined
21964 && DECL_EXPLICIT_INSTANTIATION (d
)
21965 && DECL_NOT_REALLY_EXTERN (d
))
21968 The definition of a non-exported function template, a
21969 non-exported member function template, or a non-exported
21970 member function or static data member of a class template
21971 shall be present in every translation unit in which it is
21972 explicitly instantiated. */
21973 permerror (input_location
, "explicit instantiation of %qD "
21974 "but no definition available", d
);
21976 /* If we're in unevaluated context, we just wanted to get the
21977 constant value; this isn't an odr use, so don't queue
21978 a full instantiation. */
21979 if (cp_unevaluated_operand
!= 0)
21981 /* ??? Historically, we have instantiated inline functions, even
21982 when marked as "extern template". */
21983 if (!(external_p
&& VAR_P (d
)))
21984 add_pending_template (d
);
21987 /* Tell the repository that D is available in this translation unit
21988 -- and see if it is supposed to be instantiated here. */
21989 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
21991 /* In a PCH file, despite the fact that the repository hasn't
21992 requested instantiation in the PCH it is still possible that
21993 an instantiation will be required in a file that includes the
21996 add_pending_template (d
);
21997 /* Instantiate inline functions so that the inliner can do its
21998 job, even though we'll not be emitting a copy of this
22000 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
22004 fn_context
= decl_function_context (d
);
22005 nested
= (current_function_decl
!= NULL_TREE
);
22006 vec
<tree
> omp_privatization_save
;
22008 save_omp_privatization_clauses (omp_privatization_save
);
22011 push_to_top_level ();
22015 push_function_context ();
22016 cp_unevaluated_operand
= 0;
22017 c_inhibit_evaluation_warnings
= 0;
22020 /* Mark D as instantiated so that recursive calls to
22021 instantiate_decl do not try to instantiate it again. */
22022 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
22024 /* Regenerate the declaration in case the template has been modified
22025 by a subsequent redeclaration. */
22026 regenerate_decl_from_template (d
, td
, args
);
22028 /* We already set the file and line above. Reset them now in case
22029 they changed as a result of calling regenerate_decl_from_template. */
22030 input_location
= DECL_SOURCE_LOCATION (d
);
22035 bool const_init
= false;
22037 /* Clear out DECL_RTL; whatever was there before may not be right
22038 since we've reset the type of the declaration. */
22039 SET_DECL_RTL (d
, NULL
);
22040 DECL_IN_AGGR_P (d
) = 0;
22042 /* The initializer is placed in DECL_INITIAL by
22043 regenerate_decl_from_template so we don't need to
22044 push/pop_access_scope again here. Pull it out so that
22045 cp_finish_decl can process it. */
22046 init
= DECL_INITIAL (d
);
22047 DECL_INITIAL (d
) = NULL_TREE
;
22048 DECL_INITIALIZED_P (d
) = 0;
22050 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22051 initializer. That function will defer actual emission until
22052 we have a chance to determine linkage. */
22053 DECL_EXTERNAL (d
) = 0;
22055 /* Enter the scope of D so that access-checking works correctly. */
22056 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22058 push_nested_class (DECL_CONTEXT (d
));
22060 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22061 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
22064 pop_nested_class ();
22066 if (variable_template_p (gen_tmpl
))
22067 note_variable_template_instantiation (d
);
22069 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
22070 synthesize_method (d
);
22071 else if (TREE_CODE (d
) == FUNCTION_DECL
)
22073 hash_map
<tree
, tree
> *saved_local_specializations
;
22076 tree block
= NULL_TREE
;
22078 /* Save away the current list, in case we are instantiating one
22079 template from within the body of another. */
22080 saved_local_specializations
= local_specializations
;
22082 /* Set up the list of local specializations. */
22083 local_specializations
= new hash_map
<tree
, tree
>;
22085 /* Set up context. */
22086 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22087 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22088 block
= push_stmt_list ();
22090 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
22092 /* Some typedefs referenced from within the template code need to be
22093 access checked at template instantiation time, i.e now. These
22094 types were added to the template at parsing time. Let's get those
22095 and perform the access checks then. */
22096 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
22099 /* Create substitution entries for the parameters. */
22100 tmpl_parm
= DECL_ARGUMENTS (code_pattern
);
22101 spec_parm
= DECL_ARGUMENTS (d
);
22102 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
22104 register_local_specialization (spec_parm
, tmpl_parm
);
22105 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
22106 tmpl_parm
= skip_artificial_parms_for (code_pattern
, tmpl_parm
);
22108 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22110 if (!DECL_PACK_P (tmpl_parm
))
22112 register_local_specialization (spec_parm
, tmpl_parm
);
22113 spec_parm
= DECL_CHAIN (spec_parm
);
22117 /* Register the (value) argument pack as a specialization of
22118 TMPL_PARM, then move on. */
22119 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22120 register_local_specialization (argpack
, tmpl_parm
);
22123 gcc_assert (!spec_parm
);
22125 /* Substitute into the body of the function. */
22126 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22127 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
22128 tf_warning_or_error
, tmpl
);
22131 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
22132 tf_warning_or_error
, tmpl
,
22133 /*integral_constant_expression_p=*/false);
22135 /* Set the current input_location to the end of the function
22136 so that finish_function knows where we are. */
22138 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
22140 /* Remember if we saw an infinite loop in the template. */
22141 current_function_infinite_loop
22142 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
22145 /* We don't need the local specializations any more. */
22146 delete local_specializations
;
22147 local_specializations
= saved_local_specializations
;
22149 /* Finish the function. */
22150 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22151 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22152 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
22155 d
= finish_function (0);
22156 expand_or_defer_fn (d
);
22159 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22160 cp_check_omp_declare_reduction (d
);
22163 /* We're not deferring instantiation any more. */
22164 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
22167 pop_from_top_level ();
22169 pop_function_context ();
22172 input_location
= saved_loc
;
22173 cp_unevaluated_operand
= saved_unevaluated_operand
;
22174 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
22175 pop_deferring_access_checks ();
22176 pop_tinst_level ();
22178 restore_omp_privatization_clauses (omp_privatization_save
);
22180 timevar_pop (TV_TEMPLATE_INST
);
22185 /* Run through the list of templates that we wish we could
22186 instantiate, and instantiate any we can. RETRIES is the
22187 number of times we retry pending template instantiation. */
22190 instantiate_pending_templates (int retries
)
22193 location_t saved_loc
= input_location
;
22195 /* Instantiating templates may trigger vtable generation. This in turn
22196 may require further template instantiations. We place a limit here
22197 to avoid infinite loop. */
22198 if (pending_templates
&& retries
>= max_tinst_depth
)
22200 tree decl
= pending_templates
->tinst
->decl
;
22202 fatal_error (input_location
,
22203 "template instantiation depth exceeds maximum of %d"
22204 " instantiating %q+D, possibly from virtual table generation"
22205 " (use -ftemplate-depth= to increase the maximum)",
22206 max_tinst_depth
, decl
);
22207 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22208 /* Pretend that we defined it. */
22209 DECL_INITIAL (decl
) = error_mark_node
;
22215 struct pending_template
**t
= &pending_templates
;
22216 struct pending_template
*last
= NULL
;
22220 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
22221 bool complete
= false;
22223 if (TYPE_P (instantiation
))
22227 if (!COMPLETE_TYPE_P (instantiation
))
22229 instantiate_class_template (instantiation
);
22230 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
22231 for (fn
= TYPE_METHODS (instantiation
);
22233 fn
= TREE_CHAIN (fn
))
22234 if (! DECL_ARTIFICIAL (fn
))
22235 instantiate_decl (fn
,
22237 /*expl_inst_class_mem_p=*/false);
22238 if (COMPLETE_TYPE_P (instantiation
))
22242 complete
= COMPLETE_TYPE_P (instantiation
);
22246 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
22247 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
22250 = instantiate_decl (instantiation
,
22252 /*expl_inst_class_mem_p=*/false);
22253 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
22257 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
22258 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
22262 /* If INSTANTIATION has been instantiated, then we don't
22263 need to consider it again in the future. */
22271 current_tinst_level
= NULL
;
22273 last_pending_template
= last
;
22275 while (reconsider
);
22277 input_location
= saved_loc
;
22280 /* Substitute ARGVEC into T, which is a list of initializers for
22281 either base class or a non-static data member. The TREE_PURPOSEs
22282 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22283 instantiate_decl. */
22286 tsubst_initializer_list (tree t
, tree argvec
)
22288 tree inits
= NULL_TREE
;
22290 for (; t
; t
= TREE_CHAIN (t
))
22294 tree expanded_bases
= NULL_TREE
;
22295 tree expanded_arguments
= NULL_TREE
;
22298 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
22303 /* Expand the base class expansion type into separate base
22305 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
22306 tf_warning_or_error
,
22308 if (expanded_bases
== error_mark_node
)
22311 /* We'll be building separate TREE_LISTs of arguments for
22313 len
= TREE_VEC_LENGTH (expanded_bases
);
22314 expanded_arguments
= make_tree_vec (len
);
22315 for (i
= 0; i
< len
; i
++)
22316 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
22318 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22319 expand each argument in the TREE_VALUE of t. */
22320 expr
= make_node (EXPR_PACK_EXPANSION
);
22321 PACK_EXPANSION_LOCAL_P (expr
) = true;
22322 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
22323 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
22325 if (TREE_VALUE (t
) == void_type_node
)
22326 /* VOID_TYPE_NODE is used to indicate
22327 value-initialization. */
22329 for (i
= 0; i
< len
; i
++)
22330 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
22334 /* Substitute parameter packs into each argument in the
22336 in_base_initializer
= 1;
22337 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
22339 tree expanded_exprs
;
22341 /* Expand the argument. */
22342 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
22344 = tsubst_pack_expansion (expr
, argvec
,
22345 tf_warning_or_error
,
22347 if (expanded_exprs
== error_mark_node
)
22350 /* Prepend each of the expanded expressions to the
22351 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22352 for (i
= 0; i
< len
; i
++)
22354 TREE_VEC_ELT (expanded_arguments
, i
) =
22355 tree_cons (NULL_TREE
,
22356 TREE_VEC_ELT (expanded_exprs
, i
),
22357 TREE_VEC_ELT (expanded_arguments
, i
));
22360 in_base_initializer
= 0;
22362 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22363 since we built them backwards. */
22364 for (i
= 0; i
< len
; i
++)
22366 TREE_VEC_ELT (expanded_arguments
, i
) =
22367 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
22372 for (i
= 0; i
< len
; ++i
)
22374 if (expanded_bases
)
22376 decl
= TREE_VEC_ELT (expanded_bases
, i
);
22377 decl
= expand_member_init (decl
);
22378 init
= TREE_VEC_ELT (expanded_arguments
, i
);
22383 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
22384 tf_warning_or_error
, NULL_TREE
);
22386 decl
= expand_member_init (decl
);
22387 if (decl
&& !DECL_P (decl
))
22388 in_base_initializer
= 1;
22390 init
= TREE_VALUE (t
);
22392 if (init
!= void_type_node
)
22393 init
= tsubst_expr (init
, argvec
,
22394 tf_warning_or_error
, NULL_TREE
,
22395 /*integral_constant_expression_p=*/false);
22396 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
22397 /* If we had an initializer but it instantiated to nothing,
22398 value-initialize the object. This will only occur when
22399 the initializer was a pack expansion where the parameter
22400 packs used in that expansion were of length zero. */
22401 init
= void_type_node
;
22402 in_base_initializer
= 0;
22407 init
= build_tree_list (decl
, init
);
22408 TREE_CHAIN (init
) = inits
;
22416 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22419 set_current_access_from_decl (tree decl
)
22421 if (TREE_PRIVATE (decl
))
22422 current_access_specifier
= access_private_node
;
22423 else if (TREE_PROTECTED (decl
))
22424 current_access_specifier
= access_protected_node
;
22426 current_access_specifier
= access_public_node
;
22429 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22430 is the instantiation (which should have been created with
22431 start_enum) and ARGS are the template arguments to use. */
22434 tsubst_enum (tree tag
, tree newtag
, tree args
)
22438 if (SCOPED_ENUM_P (newtag
))
22439 begin_scope (sk_scoped_enum
, newtag
);
22441 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
22446 decl
= TREE_VALUE (e
);
22447 /* Note that in a template enum, the TREE_VALUE is the
22448 CONST_DECL, not the corresponding INTEGER_CST. */
22449 value
= tsubst_expr (DECL_INITIAL (decl
),
22450 args
, tf_warning_or_error
, NULL_TREE
,
22451 /*integral_constant_expression_p=*/true);
22453 /* Give this enumeration constant the correct access. */
22454 set_current_access_from_decl (decl
);
22456 /* Actually build the enumerator itself. Here we're assuming that
22457 enumerators can't have dependent attributes. */
22458 build_enumerator (DECL_NAME (decl
), value
, newtag
,
22459 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
22462 if (SCOPED_ENUM_P (newtag
))
22465 finish_enum_value_list (newtag
);
22466 finish_enum (newtag
);
22468 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
22469 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
22472 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22473 its type -- but without substituting the innermost set of template
22474 arguments. So, innermost set of template parameters will appear in
22478 get_mostly_instantiated_function_type (tree decl
)
22480 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22481 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
22484 /* Return truthvalue if we're processing a template different from
22485 the last one involved in diagnostics. */
22487 problematic_instantiation_changed (void)
22489 return current_tinst_level
!= last_error_tinst_level
;
22492 /* Remember current template involved in diagnostics. */
22494 record_last_problematic_instantiation (void)
22496 last_error_tinst_level
= current_tinst_level
;
22499 struct tinst_level
*
22500 current_instantiation (void)
22502 return current_tinst_level
;
22505 /* Return TRUE if current_function_decl is being instantiated, false
22509 instantiating_current_function_p (void)
22511 return (current_instantiation ()
22512 && current_instantiation ()->decl
== current_function_decl
);
22515 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22516 type. Return zero for ok, nonzero for disallowed. Issue error and
22517 warning messages under control of COMPLAIN. */
22520 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
22522 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
22524 else if (POINTER_TYPE_P (type
))
22526 else if (TYPE_PTRMEM_P (type
))
22528 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
22530 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
22532 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
22534 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
22536 /* A bound template template parm could later be instantiated to have a valid
22537 nontype parm type via an alias template. */
22538 else if (cxx_dialect
>= cxx11
22539 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22542 if (complain
& tf_error
)
22544 if (type
== error_mark_node
)
22545 inform (input_location
, "invalid template non-type parameter");
22547 error ("%q#T is not a valid type for a template non-type parameter",
22553 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22554 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22557 dependent_type_p_r (tree type
)
22563 A type is dependent if it is:
22565 -- a template parameter. Template template parameters are types
22566 for us (since TYPE_P holds true for them) so we handle
22568 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22569 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
22571 /* -- a qualified-id with a nested-name-specifier which contains a
22572 class-name that names a dependent type or whose unqualified-id
22573 names a dependent type. */
22574 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22577 /* An alias template specialization can be dependent even if the
22578 resulting type is not. */
22579 if (dependent_alias_template_spec_p (type
))
22582 /* -- a cv-qualified type where the cv-unqualified type is
22584 No code is necessary for this bullet; the code below handles
22585 cv-qualified types, and we don't want to strip aliases with
22586 TYPE_MAIN_VARIANT because of DR 1558. */
22587 /* -- a compound type constructed from any dependent type. */
22588 if (TYPE_PTRMEM_P (type
))
22589 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
22590 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22592 else if (TYPE_PTR_P (type
)
22593 || TREE_CODE (type
) == REFERENCE_TYPE
)
22594 return dependent_type_p (TREE_TYPE (type
));
22595 else if (TREE_CODE (type
) == FUNCTION_TYPE
22596 || TREE_CODE (type
) == METHOD_TYPE
)
22600 if (dependent_type_p (TREE_TYPE (type
)))
22602 for (arg_type
= TYPE_ARG_TYPES (type
);
22604 arg_type
= TREE_CHAIN (arg_type
))
22605 if (dependent_type_p (TREE_VALUE (arg_type
)))
22609 /* -- an array type constructed from any dependent type or whose
22610 size is specified by a constant expression that is
22613 We checked for type- and value-dependence of the bounds in
22614 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22615 if (TREE_CODE (type
) == ARRAY_TYPE
)
22617 if (TYPE_DOMAIN (type
)
22618 && dependent_type_p (TYPE_DOMAIN (type
)))
22620 return dependent_type_p (TREE_TYPE (type
));
22623 /* -- a template-id in which either the template name is a template
22625 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22627 /* ... or any of the template arguments is a dependent type or
22628 an expression that is type-dependent or value-dependent. */
22629 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
22630 && (any_dependent_template_arguments_p
22631 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
22634 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22635 dependent; if the argument of the `typeof' expression is not
22636 type-dependent, then it should already been have resolved. */
22637 if (TREE_CODE (type
) == TYPEOF_TYPE
22638 || TREE_CODE (type
) == DECLTYPE_TYPE
22639 || TREE_CODE (type
) == UNDERLYING_TYPE
)
22642 /* A template argument pack is dependent if any of its packed
22644 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
22646 tree args
= ARGUMENT_PACK_ARGS (type
);
22647 int i
, len
= TREE_VEC_LENGTH (args
);
22648 for (i
= 0; i
< len
; ++i
)
22649 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
22653 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22654 be template parameters. */
22655 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
22658 /* The standard does not specifically mention types that are local
22659 to template functions or local classes, but they should be
22660 considered dependent too. For example:
22662 template <int I> void f() {
22667 The size of `E' cannot be known until the value of `I' has been
22668 determined. Therefore, `E' must be considered dependent. */
22669 scope
= TYPE_CONTEXT (type
);
22670 if (scope
&& TYPE_P (scope
))
22671 return dependent_type_p (scope
);
22672 /* Don't use type_dependent_expression_p here, as it can lead
22673 to infinite recursion trying to determine whether a lambda
22674 nested in a lambda is dependent (c++/47687). */
22675 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
22676 && DECL_LANG_SPECIFIC (scope
)
22677 && DECL_TEMPLATE_INFO (scope
)
22678 && (any_dependent_template_arguments_p
22679 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
22682 /* Other types are non-dependent. */
22686 /* Returns TRUE if TYPE is dependent, in the sense of
22687 [temp.dep.type]. Note that a NULL type is considered dependent. */
22690 dependent_type_p (tree type
)
22692 /* If there are no template parameters in scope, then there can't be
22693 any dependent types. */
22694 if (!processing_template_decl
)
22696 /* If we are not processing a template, then nobody should be
22697 providing us with a dependent type. */
22699 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
22703 /* If the type is NULL, we have not computed a type for the entity
22704 in question; in that case, the type is dependent. */
22708 /* Erroneous types can be considered non-dependent. */
22709 if (type
== error_mark_node
)
22712 /* If we have not already computed the appropriate value for TYPE,
22714 if (!TYPE_DEPENDENT_P_VALID (type
))
22716 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
22717 TYPE_DEPENDENT_P_VALID (type
) = 1;
22720 return TYPE_DEPENDENT_P (type
);
22723 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22724 lookup. In other words, a dependent type that is not the current
22728 dependent_scope_p (tree scope
)
22730 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
22731 && !currently_open_class (scope
));
22734 /* T is a SCOPE_REF; return whether we need to consider it
22735 instantiation-dependent so that we can check access at instantiation
22736 time even though we know which member it resolves to. */
22739 instantiation_dependent_scope_ref_p (tree t
)
22741 if (DECL_P (TREE_OPERAND (t
, 1))
22742 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
22743 && accessible_in_template_p (TREE_OPERAND (t
, 0),
22744 TREE_OPERAND (t
, 1)))
22750 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22751 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22754 /* Note that this predicate is not appropriate for general expressions;
22755 only constant expressions (that satisfy potential_constant_expression)
22756 can be tested for value dependence. */
22759 value_dependent_expression_p (tree expression
)
22761 if (!processing_template_decl
)
22764 /* A name declared with a dependent type. */
22765 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
22768 switch (TREE_CODE (expression
))
22771 /* A dependent member function of the current instantiation. */
22772 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
22774 case FUNCTION_DECL
:
22775 /* A dependent member function of the current instantiation. */
22776 if (DECL_CLASS_SCOPE_P (expression
)
22777 && dependent_type_p (DECL_CONTEXT (expression
)))
22781 case IDENTIFIER_NODE
:
22782 /* A name that has not been looked up -- must be dependent. */
22785 case TEMPLATE_PARM_INDEX
:
22786 /* A non-type template parm. */
22790 /* A non-type template parm. */
22791 if (DECL_TEMPLATE_PARM_P (expression
))
22793 return value_dependent_expression_p (DECL_INITIAL (expression
));
22796 /* A constant with literal type and is initialized
22797 with an expression that is value-dependent.
22799 Note that a non-dependent parenthesized initializer will have
22800 already been replaced with its constant value, so if we see
22801 a TREE_LIST it must be dependent. */
22802 if (DECL_INITIAL (expression
)
22803 && decl_constant_var_p (expression
)
22804 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
22805 /* cp_finish_decl doesn't fold reference initializers. */
22806 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
22807 || type_dependent_expression_p (DECL_INITIAL (expression
))
22808 || value_dependent_expression_p (DECL_INITIAL (expression
))))
22812 case DYNAMIC_CAST_EXPR
:
22813 case STATIC_CAST_EXPR
:
22814 case CONST_CAST_EXPR
:
22815 case REINTERPRET_CAST_EXPR
:
22817 /* These expressions are value-dependent if the type to which
22818 the cast occurs is dependent or the expression being casted
22819 is value-dependent. */
22821 tree type
= TREE_TYPE (expression
);
22823 if (dependent_type_p (type
))
22826 /* A functional cast has a list of operands. */
22827 expression
= TREE_OPERAND (expression
, 0);
22830 /* If there are no operands, it must be an expression such
22831 as "int()". This should not happen for aggregate types
22832 because it would form non-constant expressions. */
22833 gcc_assert (cxx_dialect
>= cxx11
22834 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
22839 if (TREE_CODE (expression
) == TREE_LIST
)
22840 return any_value_dependent_elements_p (expression
);
22842 return value_dependent_expression_p (expression
);
22846 if (SIZEOF_EXPR_TYPE_P (expression
))
22847 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
22851 /* A `sizeof' expression is value-dependent if the operand is
22852 type-dependent or is a pack expansion. */
22853 expression
= TREE_OPERAND (expression
, 0);
22854 if (PACK_EXPANSION_P (expression
))
22856 else if (TYPE_P (expression
))
22857 return dependent_type_p (expression
);
22858 return instantiation_dependent_uneval_expression_p (expression
);
22860 case AT_ENCODE_EXPR
:
22861 /* An 'encode' expression is value-dependent if the operand is
22863 expression
= TREE_OPERAND (expression
, 0);
22864 return dependent_type_p (expression
);
22866 case NOEXCEPT_EXPR
:
22867 expression
= TREE_OPERAND (expression
, 0);
22868 return instantiation_dependent_uneval_expression_p (expression
);
22871 /* All instantiation-dependent expressions should also be considered
22872 value-dependent. */
22873 return instantiation_dependent_scope_ref_p (expression
);
22875 case COMPONENT_REF
:
22876 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
22877 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
22879 case NONTYPE_ARGUMENT_PACK
:
22880 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22881 is value-dependent. */
22883 tree values
= ARGUMENT_PACK_ARGS (expression
);
22884 int i
, len
= TREE_VEC_LENGTH (values
);
22886 for (i
= 0; i
< len
; ++i
)
22887 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
22895 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
22896 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
22897 || (type2
? dependent_type_p (type2
) : false));
22901 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22902 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
22905 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22906 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
22910 tree op
= TREE_OPERAND (expression
, 0);
22911 return (value_dependent_expression_p (op
)
22912 || has_value_dependent_address (op
));
22915 case REQUIRES_EXPR
:
22916 /* Treat all requires-expressions as value-dependent so
22917 we don't try to fold them. */
22921 return dependent_type_p (TREE_OPERAND (expression
, 0));
22925 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
22927 tree fn
= get_callee_fndecl (expression
);
22929 nargs
= call_expr_nargs (expression
);
22930 for (i
= 0; i
< nargs
; ++i
)
22932 tree op
= CALL_EXPR_ARG (expression
, i
);
22933 /* In a call to a constexpr member function, look through the
22934 implicit ADDR_EXPR on the object argument so that it doesn't
22935 cause the call to be considered value-dependent. We also
22936 look through it in potential_constant_expression. */
22937 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
22938 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
22939 && TREE_CODE (op
) == ADDR_EXPR
)
22940 op
= TREE_OPERAND (op
, 0);
22941 if (value_dependent_expression_p (op
))
22947 case TEMPLATE_ID_EXPR
:
22948 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22950 return type_dependent_expression_p (expression
)
22951 || variable_concept_p (TREE_OPERAND (expression
, 0));
22957 if (dependent_type_p (TREE_TYPE (expression
)))
22959 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
22960 if (value_dependent_expression_p (val
))
22966 /* Treat a GNU statement expression as dependent to avoid crashing
22967 under instantiate_non_dependent_expr; it can't be constant. */
22971 /* A constant expression is value-dependent if any subexpression is
22972 value-dependent. */
22973 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
22975 case tcc_reference
:
22977 case tcc_comparison
:
22979 case tcc_expression
:
22982 int i
, len
= cp_tree_operand_length (expression
);
22984 for (i
= 0; i
< len
; i
++)
22986 tree t
= TREE_OPERAND (expression
, i
);
22988 /* In some cases, some of the operands may be missing.l
22989 (For example, in the case of PREDECREMENT_EXPR, the
22990 amount to increment by may be missing.) That doesn't
22991 make the expression dependent. */
22992 if (t
&& value_dependent_expression_p (t
))
23003 /* The expression is not value-dependent. */
23007 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23008 [temp.dep.expr]. Note that an expression with no type is
23009 considered dependent. Other parts of the compiler arrange for an
23010 expression with type-dependent subexpressions to have no type, so
23011 this function doesn't have to be fully recursive. */
23014 type_dependent_expression_p (tree expression
)
23016 if (!processing_template_decl
)
23019 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
23022 /* An unresolved name is always dependent. */
23023 if (identifier_p (expression
)
23024 || TREE_CODE (expression
) == USING_DECL
23025 || TREE_CODE (expression
) == WILDCARD_DECL
)
23028 /* A fold expression is type-dependent. */
23029 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
23030 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
23031 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
23032 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
23035 /* Some expression forms are never type-dependent. */
23036 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
23037 || TREE_CODE (expression
) == SIZEOF_EXPR
23038 || TREE_CODE (expression
) == ALIGNOF_EXPR
23039 || TREE_CODE (expression
) == AT_ENCODE_EXPR
23040 || TREE_CODE (expression
) == NOEXCEPT_EXPR
23041 || TREE_CODE (expression
) == TRAIT_EXPR
23042 || TREE_CODE (expression
) == TYPEID_EXPR
23043 || TREE_CODE (expression
) == DELETE_EXPR
23044 || TREE_CODE (expression
) == VEC_DELETE_EXPR
23045 || TREE_CODE (expression
) == THROW_EXPR
23046 || TREE_CODE (expression
) == REQUIRES_EXPR
)
23049 /* The types of these expressions depends only on the type to which
23050 the cast occurs. */
23051 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
23052 || TREE_CODE (expression
) == STATIC_CAST_EXPR
23053 || TREE_CODE (expression
) == CONST_CAST_EXPR
23054 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
23055 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
23056 || TREE_CODE (expression
) == CAST_EXPR
)
23057 return dependent_type_p (TREE_TYPE (expression
));
23059 /* The types of these expressions depends only on the type created
23060 by the expression. */
23061 if (TREE_CODE (expression
) == NEW_EXPR
23062 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
23064 /* For NEW_EXPR tree nodes created inside a template, either
23065 the object type itself or a TREE_LIST may appear as the
23067 tree type
= TREE_OPERAND (expression
, 1);
23068 if (TREE_CODE (type
) == TREE_LIST
)
23069 /* This is an array type. We need to check array dimensions
23071 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
23072 || value_dependent_expression_p
23073 (TREE_OPERAND (TREE_VALUE (type
), 1));
23075 return dependent_type_p (type
);
23078 if (TREE_CODE (expression
) == SCOPE_REF
)
23080 tree scope
= TREE_OPERAND (expression
, 0);
23081 tree name
= TREE_OPERAND (expression
, 1);
23083 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23084 contains an identifier associated by name lookup with one or more
23085 declarations declared with a dependent type, or...a
23086 nested-name-specifier or qualified-id that names a member of an
23087 unknown specialization. */
23088 return (type_dependent_expression_p (name
)
23089 || dependent_scope_p (scope
));
23092 if (TREE_CODE (expression
) == TEMPLATE_DECL
23093 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
23094 return uses_outer_template_parms (expression
);
23096 if (TREE_CODE (expression
) == STMT_EXPR
)
23097 expression
= stmt_expr_value_expr (expression
);
23099 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
23104 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
23106 if (type_dependent_expression_p (elt
))
23112 /* A static data member of the current instantiation with incomplete
23113 array type is type-dependent, as the definition and specializations
23114 can have different bounds. */
23115 if (VAR_P (expression
)
23116 && DECL_CLASS_SCOPE_P (expression
)
23117 && dependent_type_p (DECL_CONTEXT (expression
))
23118 && VAR_HAD_UNKNOWN_BOUND (expression
))
23121 /* An array of unknown bound depending on a variadic parameter, eg:
23123 template<typename... Args>
23124 void foo (Args... args)
23126 int arr[] = { args... };
23129 template<int... vals>
23132 int arr[] = { vals... };
23135 If the array has no length and has an initializer, it must be that
23136 we couldn't determine its length in cp_complete_array_type because
23137 it is dependent. */
23138 if (VAR_P (expression
)
23139 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
23140 && !TYPE_DOMAIN (TREE_TYPE (expression
))
23141 && DECL_INITIAL (expression
))
23144 /* A function or variable template-id is type-dependent if it has any
23145 dependent template arguments. Note that we only consider the innermost
23146 template arguments here, since those are the ones that come from the
23147 template-id; the template arguments for the enclosing class do not make it
23148 type-dependent, they only make a member function value-dependent. */
23149 if (VAR_OR_FUNCTION_DECL_P (expression
)
23150 && DECL_LANG_SPECIFIC (expression
)
23151 && DECL_TEMPLATE_INFO (expression
)
23152 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
23153 && (any_dependent_template_arguments_p
23154 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
23157 /* Always dependent, on the number of arguments if nothing else. */
23158 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
23161 if (TREE_TYPE (expression
) == unknown_type_node
)
23163 if (TREE_CODE (expression
) == ADDR_EXPR
)
23164 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
23165 if (TREE_CODE (expression
) == COMPONENT_REF
23166 || TREE_CODE (expression
) == OFFSET_REF
)
23168 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23170 expression
= TREE_OPERAND (expression
, 1);
23171 if (identifier_p (expression
))
23174 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23175 if (TREE_CODE (expression
) == SCOPE_REF
)
23178 if (BASELINK_P (expression
))
23180 if (BASELINK_OPTYPE (expression
)
23181 && dependent_type_p (BASELINK_OPTYPE (expression
)))
23183 expression
= BASELINK_FUNCTIONS (expression
);
23186 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
23188 if (any_dependent_template_arguments_p
23189 (TREE_OPERAND (expression
, 1)))
23191 expression
= TREE_OPERAND (expression
, 0);
23192 if (identifier_p (expression
))
23196 gcc_assert (TREE_CODE (expression
) == OVERLOAD
23197 || TREE_CODE (expression
) == FUNCTION_DECL
);
23201 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
23203 expression
= OVL_NEXT (expression
);
23208 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
23210 return (dependent_type_p (TREE_TYPE (expression
)));
23213 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23214 type-dependent if the expression refers to a member of the current
23215 instantiation and the type of the referenced member is dependent, or the
23216 class member access expression refers to a member of an unknown
23219 This function returns true if the OBJECT in such a class member access
23220 expression is of an unknown specialization. */
23223 type_dependent_object_expression_p (tree object
)
23225 tree scope
= TREE_TYPE (object
);
23226 return (!scope
|| dependent_scope_p (scope
));
23229 /* walk_tree callback function for instantiation_dependent_expression_p,
23230 below. Returns non-zero if a dependent subexpression is found. */
23233 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
23238 /* We don't have to worry about decltype currently because decltype
23239 of an instantiation-dependent expr is a dependent type. This
23240 might change depending on the resolution of DR 1172. */
23241 *walk_subtrees
= false;
23244 enum tree_code code
= TREE_CODE (*tp
);
23247 /* Don't treat an argument list as dependent just because it has no
23253 case TEMPLATE_PARM_INDEX
:
23256 /* Handle expressions with type operands. */
23260 case AT_ENCODE_EXPR
:
23262 tree op
= TREE_OPERAND (*tp
, 0);
23263 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
23264 op
= TREE_TYPE (op
);
23267 if (dependent_type_p (op
))
23271 *walk_subtrees
= false;
23278 case COMPONENT_REF
:
23279 if (identifier_p (TREE_OPERAND (*tp
, 1)))
23280 /* In a template, finish_class_member_access_expr creates a
23281 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23282 type-dependent, so that we can check access control at
23283 instantiation time (PR 42277). See also Core issue 1273. */
23288 if (instantiation_dependent_scope_ref_p (*tp
))
23293 /* Treat statement-expressions as dependent. */
23297 /* Treat requires-expressions as dependent. */
23298 case REQUIRES_EXPR
:
23302 /* Treat calls to function concepts as dependent. */
23303 if (function_concept_check_p (*tp
))
23307 case TEMPLATE_ID_EXPR
:
23308 /* And variable concepts. */
23309 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
23317 if (type_dependent_expression_p (*tp
))
23323 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23324 sense defined by the ABI:
23326 "An expression is instantiation-dependent if it is type-dependent
23327 or value-dependent, or it has a subexpression that is type-dependent
23328 or value-dependent."
23330 Except don't actually check value-dependence for unevaluated expressions,
23331 because in sizeof(i) we don't care about the value of i. Checking
23332 type-dependence will in turn check value-dependence of array bounds/template
23333 arguments as needed. */
23336 instantiation_dependent_uneval_expression_p (tree expression
)
23340 if (!processing_template_decl
)
23343 if (expression
== error_mark_node
)
23346 result
= cp_walk_tree_without_duplicates (&expression
,
23347 instantiation_dependent_r
, NULL
);
23348 return result
!= NULL_TREE
;
23351 /* As above, but also check value-dependence of the expression as a whole. */
23354 instantiation_dependent_expression_p (tree expression
)
23356 return (instantiation_dependent_uneval_expression_p (expression
)
23357 || value_dependent_expression_p (expression
));
23360 /* Like type_dependent_expression_p, but it also works while not processing
23361 a template definition, i.e. during substitution or mangling. */
23364 type_dependent_expression_p_push (tree expr
)
23367 ++processing_template_decl
;
23368 b
= type_dependent_expression_p (expr
);
23369 --processing_template_decl
;
23373 /* Returns TRUE if ARGS contains a type-dependent expression. */
23376 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
23381 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
23383 if (type_dependent_expression_p (arg
))
23389 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23390 expressions) contains any type-dependent expressions. */
23393 any_type_dependent_elements_p (const_tree list
)
23395 for (; list
; list
= TREE_CHAIN (list
))
23396 if (type_dependent_expression_p (TREE_VALUE (list
)))
23402 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23403 expressions) contains any value-dependent expressions. */
23406 any_value_dependent_elements_p (const_tree list
)
23408 for (; list
; list
= TREE_CHAIN (list
))
23409 if (value_dependent_expression_p (TREE_VALUE (list
)))
23415 /* Returns TRUE if the ARG (a template argument) is dependent. */
23418 dependent_template_arg_p (tree arg
)
23420 if (!processing_template_decl
)
23423 /* Assume a template argument that was wrongly written by the user
23424 is dependent. This is consistent with what
23425 any_dependent_template_arguments_p [that calls this function]
23427 if (!arg
|| arg
== error_mark_node
)
23430 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
23431 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
23433 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
23435 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23437 if (DECL_TEMPLATE_PARM_P (arg
))
23439 /* A member template of a dependent class is not necessarily
23440 type-dependent, but it is a dependent template argument because it
23441 will be a member of an unknown specialization to that template. */
23442 tree scope
= CP_DECL_CONTEXT (arg
);
23443 return TYPE_P (scope
) && dependent_type_p (scope
);
23445 else if (ARGUMENT_PACK_P (arg
))
23447 tree args
= ARGUMENT_PACK_ARGS (arg
);
23448 int i
, len
= TREE_VEC_LENGTH (args
);
23449 for (i
= 0; i
< len
; ++i
)
23451 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23457 else if (TYPE_P (arg
))
23458 return dependent_type_p (arg
);
23460 return (type_dependent_expression_p (arg
)
23461 || value_dependent_expression_p (arg
));
23464 /* Returns true if ARGS (a collection of template arguments) contains
23465 any types that require structural equality testing. */
23468 any_template_arguments_need_structural_equality_p (tree args
)
23475 if (args
== error_mark_node
)
23478 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23480 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23481 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23483 tree arg
= TREE_VEC_ELT (level
, j
);
23484 tree packed_args
= NULL_TREE
;
23487 if (ARGUMENT_PACK_P (arg
))
23489 /* Look inside the argument pack. */
23490 packed_args
= ARGUMENT_PACK_ARGS (arg
);
23491 len
= TREE_VEC_LENGTH (packed_args
);
23494 for (k
= 0; k
< len
; ++k
)
23497 arg
= TREE_VEC_ELT (packed_args
, k
);
23499 if (error_operand_p (arg
))
23501 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23503 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
23505 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
23506 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
23515 /* Returns true if ARGS (a collection of template arguments) contains
23516 any dependent arguments. */
23519 any_dependent_template_arguments_p (const_tree args
)
23526 if (args
== error_mark_node
)
23529 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23531 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23532 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23533 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
23540 /* Returns TRUE if the template TMPL is type-dependent. */
23543 dependent_template_p (tree tmpl
)
23545 if (TREE_CODE (tmpl
) == OVERLOAD
)
23549 if (dependent_template_p (OVL_CURRENT (tmpl
)))
23551 tmpl
= OVL_NEXT (tmpl
);
23556 /* Template template parameters are dependent. */
23557 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
23558 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
23560 /* So are names that have not been looked up. */
23561 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
23566 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23569 dependent_template_id_p (tree tmpl
, tree args
)
23571 return (dependent_template_p (tmpl
)
23572 || any_dependent_template_arguments_p (args
));
23575 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23579 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
23583 if (!processing_template_decl
)
23586 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
23588 tree decl
= TREE_VEC_ELT (declv
, i
);
23589 tree init
= TREE_VEC_ELT (initv
, i
);
23590 tree cond
= TREE_VEC_ELT (condv
, i
);
23591 tree incr
= TREE_VEC_ELT (incrv
, i
);
23593 if (type_dependent_expression_p (decl
)
23594 || TREE_CODE (decl
) == SCOPE_REF
)
23597 if (init
&& type_dependent_expression_p (init
))
23600 if (type_dependent_expression_p (cond
))
23603 if (COMPARISON_CLASS_P (cond
)
23604 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
23605 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
23608 if (TREE_CODE (incr
) == MODOP_EXPR
)
23610 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
23611 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
23614 else if (type_dependent_expression_p (incr
))
23616 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
23618 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
23620 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
23622 tree t
= TREE_OPERAND (incr
, 1);
23623 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
23624 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
23633 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23634 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23635 no such TYPE can be found. Note that this function peers inside
23636 uninstantiated templates and therefore should be used only in
23637 extremely limited situations. ONLY_CURRENT_P restricts this
23638 peering to the currently open classes hierarchy (which is required
23639 when comparing types). */
23642 resolve_typename_type (tree type
, bool only_current_p
)
23651 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
23653 scope
= TYPE_CONTEXT (type
);
23654 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23655 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23656 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23657 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23658 identifier of the TYPENAME_TYPE anymore.
23659 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23660 TYPENAME_TYPE instead, we avoid messing up with a possible
23661 typedef variant case. */
23662 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
23664 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23665 it first before we can figure out what NAME refers to. */
23666 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
23668 if (TYPENAME_IS_RESOLVING_P (scope
))
23669 /* Given a class template A with a dependent base with nested type C,
23670 typedef typename A::C::C C will land us here, as trying to resolve
23671 the initial A::C leads to the local C typedef, which leads back to
23672 A::C::C. So we break the recursion now. */
23675 scope
= resolve_typename_type (scope
, only_current_p
);
23677 /* If we don't know what SCOPE refers to, then we cannot resolve the
23679 if (!CLASS_TYPE_P (scope
))
23681 /* If this is a typedef, we don't want to look inside (c++/11987). */
23682 if (typedef_variant_p (type
))
23684 /* If SCOPE isn't the template itself, it will not have a valid
23685 TYPE_FIELDS list. */
23686 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
23687 /* scope is either the template itself or a compatible instantiation
23688 like X<T>, so look up the name in the original template. */
23689 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
23690 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
23691 gcc_checking_assert (uses_template_parms (scope
));
23692 /* If scope has no fields, it can't be a current instantiation. Check this
23693 before currently_open_class to avoid infinite recursion (71515). */
23694 if (!TYPE_FIELDS (scope
))
23696 /* If the SCOPE is not the current instantiation, there's no reason
23697 to look inside it. */
23698 if (only_current_p
&& !currently_open_class (scope
))
23700 /* Enter the SCOPE so that name lookup will be resolved as if we
23701 were in the class definition. In particular, SCOPE will no
23702 longer be considered a dependent type. */
23703 pushed_scope
= push_scope (scope
);
23704 /* Look up the declaration. */
23705 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
23706 tf_warning_or_error
);
23708 result
= NULL_TREE
;
23710 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23711 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23714 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
23715 && TREE_CODE (decl
) == TYPE_DECL
)
23717 result
= TREE_TYPE (decl
);
23718 if (result
== error_mark_node
)
23719 result
= NULL_TREE
;
23721 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
23722 && DECL_CLASS_TEMPLATE_P (decl
))
23726 /* Obtain the template and the arguments. */
23727 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
23728 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
23729 /* Instantiate the template. */
23730 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
23731 /*entering_scope=*/0,
23732 tf_error
| tf_user
);
23733 if (result
== error_mark_node
)
23734 result
= NULL_TREE
;
23737 /* Leave the SCOPE. */
23739 pop_scope (pushed_scope
);
23741 /* If we failed to resolve it, return the original typename. */
23745 /* If lookup found a typename type, resolve that too. */
23746 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
23748 /* Ill-formed programs can cause infinite recursion here, so we
23749 must catch that. */
23750 TYPENAME_IS_RESOLVING_P (result
) = 1;
23751 result
= resolve_typename_type (result
, only_current_p
);
23752 TYPENAME_IS_RESOLVING_P (result
) = 0;
23755 /* Qualify the resulting type. */
23756 quals
= cp_type_quals (type
);
23758 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
23763 /* EXPR is an expression which is not type-dependent. Return a proxy
23764 for EXPR that can be used to compute the types of larger
23765 expressions containing EXPR. */
23768 build_non_dependent_expr (tree expr
)
23772 /* When checking, try to get a constant value for all non-dependent
23773 expressions in order to expose bugs in *_dependent_expression_p
23774 and constexpr. This can affect code generation, see PR70704, so
23775 only do this for -fchecking=2. */
23776 if (flag_checking
> 1
23777 && cxx_dialect
>= cxx11
23778 /* Don't do this during nsdmi parsing as it can lead to
23779 unexpected recursive instantiations. */
23780 && !parsing_nsdmi ()
23781 /* Don't do this during concept expansion either and for
23782 the same reason. */
23783 && !expanding_concept ())
23784 fold_non_dependent_expr (expr
);
23786 /* Preserve OVERLOADs; the functions must be available to resolve
23789 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
23790 inner_expr
= stmt_expr_value_expr (inner_expr
);
23791 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
23792 inner_expr
= TREE_OPERAND (inner_expr
, 0);
23793 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
23794 inner_expr
= TREE_OPERAND (inner_expr
, 1);
23795 if (is_overloaded_fn (inner_expr
)
23796 || TREE_CODE (inner_expr
) == OFFSET_REF
)
23798 /* There is no need to return a proxy for a variable. */
23801 /* Preserve string constants; conversions from string constants to
23802 "char *" are allowed, even though normally a "const char *"
23803 cannot be used to initialize a "char *". */
23804 if (TREE_CODE (expr
) == STRING_CST
)
23806 /* Preserve void and arithmetic constants, as an optimization -- there is no
23807 reason to create a new node. */
23808 if (TREE_CODE (expr
) == VOID_CST
23809 || TREE_CODE (expr
) == INTEGER_CST
23810 || TREE_CODE (expr
) == REAL_CST
)
23812 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23813 There is at least one place where we want to know that a
23814 particular expression is a throw-expression: when checking a ?:
23815 expression, there are special rules if the second or third
23816 argument is a throw-expression. */
23817 if (TREE_CODE (expr
) == THROW_EXPR
)
23820 /* Don't wrap an initializer list, we need to be able to look inside. */
23821 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
23824 /* Don't wrap a dummy object, we need to be able to test for it. */
23825 if (is_dummy_object (expr
))
23828 if (TREE_CODE (expr
) == COND_EXPR
)
23829 return build3 (COND_EXPR
,
23831 TREE_OPERAND (expr
, 0),
23832 (TREE_OPERAND (expr
, 1)
23833 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
23834 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
23835 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
23836 if (TREE_CODE (expr
) == COMPOUND_EXPR
23837 && !COMPOUND_EXPR_OVERLOADED (expr
))
23838 return build2 (COMPOUND_EXPR
,
23840 TREE_OPERAND (expr
, 0),
23841 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
23843 /* If the type is unknown, it can't really be non-dependent */
23844 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
23846 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23847 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
23850 /* ARGS is a vector of expressions as arguments to a function call.
23851 Replace the arguments with equivalent non-dependent expressions.
23852 This modifies ARGS in place. */
23855 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
23860 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
23862 tree newarg
= build_non_dependent_expr (arg
);
23864 (*args
)[ix
] = newarg
;
23868 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23869 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23870 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23873 make_auto_1 (tree name
, bool set_canonical
)
23875 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
23876 TYPE_NAME (au
) = build_decl (input_location
,
23877 TYPE_DECL
, name
, au
);
23878 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
23879 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
23880 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
23881 TYPE_NAME (au
), NULL_TREE
);
23883 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
23884 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
23885 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
23891 make_decltype_auto (void)
23893 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23899 return make_auto_1 (get_identifier ("auto"), true);
23902 /* Make a "constrained auto" type-specifier. This is an
23903 auto type with constraints that must be associated after
23904 deduction. The constraint is formed from the given
23905 CONC and its optional sequence of arguments, which are
23906 non-null if written as partial-concept-id. */
23909 make_constrained_auto (tree con
, tree args
)
23911 tree type
= make_auto_1 (get_identifier ("auto"), false);
23913 /* Build the constraint. */
23914 tree tmpl
= DECL_TI_TEMPLATE (con
);
23917 expr
= build_concept_check (tmpl
, type
, args
);
23919 expr
= build_concept_check (build_overload (tmpl
, NULL_TREE
), type
, args
);
23921 tree constr
= normalize_expression (expr
);
23922 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
23924 /* Our canonical type depends on the constraint. */
23925 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
23927 /* Attach the constraint to the type declaration. */
23928 tree decl
= TYPE_NAME (type
);
23932 /* Given type ARG, return std::initializer_list<ARG>. */
23937 tree std_init_list
= namespace_binding
23938 (get_identifier ("initializer_list"), std_node
);
23940 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
23942 error ("deducing from brace-enclosed initializer list requires "
23943 "#include <initializer_list>");
23944 return error_mark_node
;
23946 argvec
= make_tree_vec (1);
23947 TREE_VEC_ELT (argvec
, 0) = arg
;
23948 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
23949 NULL_TREE
, 0, tf_warning_or_error
);
23952 /* Replace auto in TYPE with std::initializer_list<auto>. */
23955 listify_autos (tree type
, tree auto_node
)
23957 tree init_auto
= listify (auto_node
);
23958 tree argvec
= make_tree_vec (1);
23959 TREE_VEC_ELT (argvec
, 0) = init_auto
;
23960 if (processing_template_decl
)
23961 argvec
= add_to_template_args (current_template_args (), argvec
);
23962 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
23965 /* Hash traits for hashing possibly constrained 'auto'
23966 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23968 struct auto_hash
: default_hash_traits
<tree
>
23970 static inline hashval_t
hash (tree
);
23971 static inline bool equal (tree
, tree
);
23974 /* Hash the 'auto' T. */
23977 auto_hash::hash (tree t
)
23979 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
23980 /* Matching constrained-type-specifiers denote the same template
23981 parameter, so hash the constraint. */
23982 return hash_placeholder_constraint (c
);
23984 /* But unconstrained autos are all separate, so just hash the pointer. */
23985 return iterative_hash_object (t
, 0);
23988 /* Compare two 'auto's. */
23991 auto_hash::equal (tree t1
, tree t2
)
23996 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
23997 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
23999 /* Two unconstrained autos are distinct. */
24003 return equivalent_placeholder_constraints (c1
, c2
);
24006 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24007 constrained) auto, add it to the vector. */
24010 extract_autos_r (tree t
, void *data
)
24012 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
24013 if (is_auto_or_concept (t
))
24015 /* All the autos were built with index 0; fix that up now. */
24016 tree
*p
= hash
.find_slot (t
, INSERT
);
24019 /* If this is a repeated constrained-type-specifier, use the index we
24021 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
24024 /* Otherwise this is new, so use the current count. */
24026 idx
= hash
.elements () - 1;
24028 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
24031 /* Always keep walking. */
24035 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24036 says they can appear anywhere in the type. */
24039 extract_autos (tree type
)
24041 hash_set
<tree
> visited
;
24042 hash_table
<auto_hash
> hash (2);
24044 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
24046 tree tree_vec
= make_tree_vec (hash
.elements());
24047 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
24048 iter
!= hash
.end(); ++iter
)
24051 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
24052 TREE_VEC_ELT (tree_vec
, i
)
24053 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
24059 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24060 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
24063 do_auto_deduction (tree type
, tree init
, tree auto_node
)
24065 return do_auto_deduction (type
, init
, auto_node
,
24066 tf_warning_or_error
,
24070 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24071 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
24072 The CONTEXT determines the context in which auto deduction is performed
24073 and is used to control error diagnostics.
24075 For partial-concept-ids, extra args may be appended to the list of deduced
24076 template arguments prior to determining constraint satisfaction. */
24079 do_auto_deduction (tree type
, tree init
, tree auto_node
,
24080 tsubst_flags_t complain
, auto_deduction_context context
)
24084 if (init
== error_mark_node
)
24085 return error_mark_node
;
24087 if (type_dependent_expression_p (init
))
24088 /* Defining a subset of type-dependent expressions that we can deduce
24089 from ahead of time isn't worth the trouble. */
24092 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
24093 with either a new invented type template parameter U or, if the
24094 initializer is a braced-init-list (8.5.4), with
24095 std::initializer_list<U>. */
24096 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
24098 if (!DIRECT_LIST_INIT_P (init
))
24099 type
= listify_autos (type
, auto_node
);
24100 else if (CONSTRUCTOR_NELTS (init
) == 1)
24101 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
24104 if (complain
& tf_warning_or_error
)
24106 if (permerror (input_location
, "direct-list-initialization of "
24107 "%<auto%> requires exactly one element"))
24108 inform (input_location
,
24109 "for deduction to %<std::initializer_list%>, use copy-"
24110 "list-initialization (i.e. add %<=%> before the %<{%>)");
24112 type
= listify_autos (type
, auto_node
);
24116 if (type
== error_mark_node
)
24117 return error_mark_node
;
24119 init
= resolve_nondeduced_context (init
, complain
);
24121 if (AUTO_IS_DECLTYPE (auto_node
))
24123 bool id
= (DECL_P (init
)
24124 || ((TREE_CODE (init
) == COMPONENT_REF
24125 || TREE_CODE (init
) == SCOPE_REF
)
24126 && !REF_PARENTHESIZED_P (init
)));
24127 targs
= make_tree_vec (1);
24128 TREE_VEC_ELT (targs
, 0)
24129 = finish_decltype_type (init
, id
, tf_warning_or_error
);
24130 if (type
!= auto_node
)
24132 if (complain
& tf_error
)
24133 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
24134 return error_mark_node
;
24139 tree parms
= build_tree_list (NULL_TREE
, type
);
24143 tparms
= extract_autos (type
);
24146 tparms
= make_tree_vec (1);
24147 TREE_VEC_ELT (tparms
, 0)
24148 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
24151 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
24152 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
24153 DEDUCE_CALL
, LOOKUP_NORMAL
,
24154 NULL
, /*explain_p=*/false);
24157 if (processing_template_decl
)
24158 /* Try again at instantiation time. */
24160 if (type
&& type
!= error_mark_node
24161 && (complain
& tf_error
))
24162 /* If type is error_mark_node a diagnostic must have been
24163 emitted by now. Also, having a mention to '<type error>'
24164 in the diagnostic is not really useful to the user. */
24166 if (cfun
&& auto_node
== current_function_auto_return_pattern
24167 && LAMBDA_FUNCTION_P (current_function_decl
))
24168 error ("unable to deduce lambda return type from %qE", init
);
24170 error ("unable to deduce %qT from %qE", type
, init
);
24171 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
24172 DEDUCE_CALL
, LOOKUP_NORMAL
,
24173 NULL
, /*explain_p=*/true);
24175 return error_mark_node
;
24179 /* Check any placeholder constraints against the deduced type. */
24180 if (flag_concepts
&& !processing_template_decl
)
24181 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
24183 /* Use the deduced type to check the associated constraints. If we
24184 have a partial-concept-id, rebuild the argument list so that
24185 we check using the extra arguments. */
24186 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
24187 tree cargs
= CHECK_CONSTR_ARGS (constr
);
24188 if (TREE_VEC_LENGTH (cargs
) > 1)
24190 cargs
= copy_node (cargs
);
24191 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
24195 if (!constraints_satisfied_p (constr
, cargs
))
24197 if (complain
& tf_warning_or_error
)
24201 case adc_unspecified
:
24202 error("placeholder constraints not satisfied");
24204 case adc_variable_type
:
24205 error ("deduced initializer does not satisfy "
24206 "placeholder constraints");
24208 case adc_return_type
:
24209 error ("deduced return type does not satisfy "
24210 "placeholder constraints");
24212 case adc_requirement
:
24213 error ("deduced expression type does not saatisy "
24214 "placeholder constraints");
24217 diagnose_constraints (input_location
, constr
, targs
);
24219 return error_mark_node
;
24223 if (processing_template_decl
)
24224 targs
= add_to_template_args (current_template_args (), targs
);
24225 return tsubst (type
, targs
, complain
, NULL_TREE
);
24228 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
24232 splice_late_return_type (tree type
, tree late_return_type
)
24234 if (is_auto (type
))
24236 if (late_return_type
)
24237 return late_return_type
;
24239 tree idx
= get_template_parm_index (type
);
24240 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
24241 /* In an abbreviated function template we didn't know we were dealing
24242 with a function template when we saw the auto return type, so update
24243 it to have the correct level. */
24244 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
24249 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
24250 'decltype(auto)'. */
24253 is_auto (const_tree type
)
24255 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
24256 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
24257 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
24263 /* for_each_template_parm callback for type_uses_auto. */
24266 is_auto_r (tree tp
, void */
*data*/
)
24268 return is_auto_or_concept (tp
);
24271 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
24272 a use of `auto'. Returns NULL_TREE otherwise. */
24275 type_uses_auto (tree type
)
24277 if (type
== NULL_TREE
)
24279 else if (flag_concepts
)
24281 /* The Concepts TS allows multiple autos in one type-specifier; just
24282 return the first one we find, do_auto_deduction will collect all of
24284 if (uses_template_parms (type
))
24285 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
24286 /*visited*/NULL
, /*nondeduced*/true);
24291 return find_type_usage (type
, is_auto
);
24294 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
24295 'decltype(auto)' or a concept. */
24298 is_auto_or_concept (const_tree type
)
24300 return is_auto (type
); // or concept
24303 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
24304 a concept identifier) iff TYPE contains a use of a generic type. Returns
24305 NULL_TREE otherwise. */
24308 type_uses_auto_or_concept (tree type
)
24310 return find_type_usage (type
, is_auto_or_concept
);
24314 /* For a given template T, return the vector of typedefs referenced
24315 in T for which access check is needed at T instantiation time.
24316 T is either a FUNCTION_DECL or a RECORD_TYPE.
24317 Those typedefs were added to T by the function
24318 append_type_to_template_for_access_check. */
24320 vec
<qualified_typedef_usage_t
, va_gc
> *
24321 get_types_needing_access_check (tree t
)
24324 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
24326 if (!t
|| t
== error_mark_node
)
24329 if (!(ti
= get_template_info (t
)))
24332 if (CLASS_TYPE_P (t
)
24333 || TREE_CODE (t
) == FUNCTION_DECL
)
24335 if (!TI_TEMPLATE (ti
))
24338 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
24344 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
24345 tied to T. That list of typedefs will be access checked at
24346 T instantiation time.
24347 T is either a FUNCTION_DECL or a RECORD_TYPE.
24348 TYPE_DECL is a TYPE_DECL node representing a typedef.
24349 SCOPE is the scope through which TYPE_DECL is accessed.
24350 LOCATION is the location of the usage point of TYPE_DECL.
24352 This function is a subroutine of
24353 append_type_to_template_for_access_check. */
24356 append_type_to_template_for_access_check_1 (tree t
,
24359 location_t location
)
24361 qualified_typedef_usage_t typedef_usage
;
24364 if (!t
|| t
== error_mark_node
)
24367 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
24368 || CLASS_TYPE_P (t
))
24370 && TREE_CODE (type_decl
) == TYPE_DECL
24373 if (!(ti
= get_template_info (t
)))
24376 gcc_assert (TI_TEMPLATE (ti
));
24378 typedef_usage
.typedef_decl
= type_decl
;
24379 typedef_usage
.context
= scope
;
24380 typedef_usage
.locus
= location
;
24382 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
24385 /* Append TYPE_DECL to the template TEMPL.
24386 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24387 At TEMPL instanciation time, TYPE_DECL will be checked to see
24388 if it can be accessed through SCOPE.
24389 LOCATION is the location of the usage point of TYPE_DECL.
24391 e.g. consider the following code snippet:
24398 template<class U> struct S
24400 C::myint mi; // <-- usage point of the typedef C::myint
24405 At S<char> instantiation time, we need to check the access of C::myint
24406 In other words, we need to check the access of the myint typedef through
24407 the C scope. For that purpose, this function will add the myint typedef
24408 and the scope C through which its being accessed to a list of typedefs
24409 tied to the template S. That list will be walked at template instantiation
24410 time and access check performed on each typedefs it contains.
24411 Note that this particular code snippet should yield an error because
24412 myint is private to C. */
24415 append_type_to_template_for_access_check (tree templ
,
24418 location_t location
)
24420 qualified_typedef_usage_t
*iter
;
24423 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
24425 /* Make sure we don't append the type to the template twice. */
24426 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
24427 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
24430 append_type_to_template_for_access_check_1 (templ
, type_decl
,
24434 /* Convert the generic type parameters in PARM that match the types given in the
24435 range [START_IDX, END_IDX) from the current_template_parms into generic type
24439 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
24441 tree current
= current_template_parms
;
24442 int depth
= TMPL_PARMS_DEPTH (current
);
24443 current
= INNERMOST_TEMPLATE_PARMS (current
);
24444 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
24446 for (int i
= 0; i
< start_idx
; ++i
)
24447 TREE_VEC_ELT (replacement
, i
)
24448 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24450 for (int i
= start_idx
; i
< end_idx
; ++i
)
24452 /* Create a distinct parameter pack type from the current parm and add it
24453 to the replacement args to tsubst below into the generic function
24456 tree o
= TREE_TYPE (TREE_VALUE
24457 (TREE_VEC_ELT (current
, i
)));
24458 tree t
= copy_type (o
);
24459 TEMPLATE_TYPE_PARM_INDEX (t
)
24460 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
24462 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
24463 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
24464 TYPE_MAIN_VARIANT (t
) = t
;
24465 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
24466 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
24467 TREE_VEC_ELT (replacement
, i
) = t
;
24468 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
24471 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
24472 TREE_VEC_ELT (replacement
, i
)
24473 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
24475 /* If there are more levels then build up the replacement with the outer
24478 replacement
= add_to_template_args (template_parms_to_args
24479 (TREE_CHAIN (current_template_parms
)),
24482 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
24485 /* Entries in the decl_constraint hash table. */
24486 struct GTY((for_user
)) constr_entry
24492 /* Hashing function and equality for constraint entries. */
24493 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
24495 static hashval_t
hash (constr_entry
*e
)
24497 return (hashval_t
)DECL_UID (e
->decl
);
24500 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
24502 return e1
->decl
== e2
->decl
;
24506 /* A mapping from declarations to constraint information. Note that
24507 both templates and their underlying declarations are mapped to the
24508 same constraint information.
24510 FIXME: This is defined in pt.c because garbage collection
24511 code is not being generated for constraint.cc. */
24513 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
24515 /* Returns the template constraints of declaration T. If T is not
24516 constrained, return NULL_TREE. Note that T must be non-null. */
24519 get_constraints (tree t
)
24521 if (!flag_concepts
)
24524 gcc_assert (DECL_P (t
));
24525 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24526 t
= DECL_TEMPLATE_RESULT (t
);
24527 constr_entry elt
= { t
, NULL_TREE
};
24528 constr_entry
* found
= decl_constraints
->find (&elt
);
24535 /* Associate the given constraint information CI with the declaration
24536 T. If T is a template, then the constraints are associated with
24537 its underlying declaration. Don't build associations if CI is
24541 set_constraints (tree t
, tree ci
)
24545 gcc_assert (t
&& flag_concepts
);
24546 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24547 t
= DECL_TEMPLATE_RESULT (t
);
24548 gcc_assert (!get_constraints (t
));
24549 constr_entry elt
= {t
, ci
};
24550 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
24551 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
24556 /* Remove the associated constraints of the declaration T. */
24559 remove_constraints (tree t
)
24561 gcc_assert (DECL_P (t
));
24562 if (TREE_CODE (t
) == TEMPLATE_DECL
)
24563 t
= DECL_TEMPLATE_RESULT (t
);
24565 constr_entry elt
= {t
, NULL_TREE
};
24566 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
24568 decl_constraints
->clear_slot (slot
);
24571 /* Memoized satisfaction results for declarations. This
24572 maps the pair (constraint_info, arguments) to the result computed
24573 by constraints_satisfied_p. */
24575 struct GTY((for_user
)) constraint_sat_entry
24582 /* Hashing function and equality for constraint entries. */
24584 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
24586 static hashval_t
hash (constraint_sat_entry
*e
)
24588 hashval_t val
= iterative_hash_object(e
->ci
, 0);
24589 return iterative_hash_template_arg (e
->args
, val
);
24592 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
24594 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
24598 /* Memoized satisfaction results for concept checks. */
24600 struct GTY((for_user
)) concept_spec_entry
24607 /* Hashing function and equality for constraint entries. */
24609 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
24611 static hashval_t
hash (concept_spec_entry
*e
)
24613 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
24616 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
24618 ++comparing_specializations
;
24619 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
24620 --comparing_specializations
;
24625 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
24626 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
24628 /* Search for a memoized satisfaction result. Returns one of the
24629 truth value nodes if previously memoized, or NULL_TREE otherwise. */
24632 lookup_constraint_satisfaction (tree ci
, tree args
)
24634 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
24635 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
24637 return found
->result
;
24642 /* Memoize the result of a satisfication test. Returns the saved result. */
24645 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
24647 constraint_sat_entry elt
= {ci
, args
, result
};
24648 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
24649 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
24655 /* Search for a memoized satisfaction result for a concept. */
24658 lookup_concept_satisfaction (tree tmpl
, tree args
)
24660 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
24661 concept_spec_entry
* found
= concept_memos
->find (&elt
);
24663 return found
->result
;
24668 /* Memoize the result of a concept check. Returns the saved result. */
24671 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
24673 concept_spec_entry elt
= {tmpl
, args
, result
};
24674 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
24675 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
24681 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
24683 /* Returns a prior concept specialization. This returns the substituted
24684 and normalized constraints defined by the concept. */
24687 get_concept_expansion (tree tmpl
, tree args
)
24689 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
24690 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
24692 return found
->result
;
24697 /* Save a concept expansion for later. */
24700 save_concept_expansion (tree tmpl
, tree args
, tree def
)
24702 concept_spec_entry elt
= {tmpl
, args
, def
};
24703 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
24704 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
24711 hash_subsumption_args (tree t1
, tree t2
)
24713 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
24714 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
24716 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
24717 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
24718 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
24719 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
24723 /* Compare the constraints of two subsumption entries. The LEFT1 and
24724 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
24725 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
24728 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
24730 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
24731 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
24732 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
24733 CHECK_CONSTR_ARGS (right1
)))
24734 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
24735 CHECK_CONSTR_ARGS (right2
));
24739 /* Key/value pair for learning and memoizing subsumption results. This
24740 associates a pair of check constraints (including arguments) with
24741 a boolean value indicating the result. */
24743 struct GTY((for_user
)) subsumption_entry
24750 /* Hashing function and equality for constraint entries. */
24752 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
24754 static hashval_t
hash (subsumption_entry
*e
)
24756 return hash_subsumption_args (e
->t1
, e
->t2
);
24759 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
24761 ++comparing_specializations
;
24762 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
24763 --comparing_specializations
;
24768 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
24770 /* Search for a previously cached subsumption result. */
24773 lookup_subsumption_result (tree t1
, tree t2
)
24775 subsumption_entry elt
= { t1
, t2
, false };
24776 subsumption_entry
* found
= subsumption_table
->find (&elt
);
24778 return &found
->result
;
24783 /* Save a subsumption result. */
24786 save_subsumption_result (tree t1
, tree t2
, bool result
)
24788 subsumption_entry elt
= {t1
, t2
, result
};
24789 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
24790 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
24796 /* Set up the hash table for constraint association. */
24799 init_constraint_processing (void)
24801 if (!flag_concepts
)
24804 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
24805 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
24806 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
24807 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
24808 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
24811 /* Set up the hash tables for template instantiations. */
24814 init_template_processing (void)
24816 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24817 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
24820 /* Print stats about the template hash tables for -fstats. */
24823 print_template_statistics (void)
24825 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
24826 "%f collisions\n", (long) decl_specializations
->size (),
24827 (long) decl_specializations
->elements (),
24828 decl_specializations
->collisions ());
24829 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
24830 "%f collisions\n", (long) type_specializations
->size (),
24831 (long) type_specializations
->elements (),
24832 type_specializations
->collisions ());
24835 #include "gt-cp-pt.h"