1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
30 #include "coretypes.h"
33 #include "stringpool.h"
36 #include "stor-layout.h"
38 #include "c-family/c-objc.h"
39 #include "cp-objcp-common.h"
41 #include "tree-iterator.h"
42 #include "type-utils.h"
44 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
{
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) tree saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
80 // Implementation of the RAII helper for creating new local
82 local_specialization_stack::local_specialization_stack (lss_policy policy
)
83 : saved (local_specializations
)
85 if (policy
== lss_blank
|| !saved
)
86 local_specializations
= new hash_map
<tree
, tree
>;
88 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
91 local_specialization_stack::~local_specialization_stack ()
93 delete local_specializations
;
94 local_specializations
= saved
;
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth
;
100 struct GTY((for_user
)) spec_entry
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
115 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132 enum template_base_result
{
134 tbr_ambiguous_baseclass
,
138 static void push_access_scope (tree
);
139 static void pop_access_scope (tree
);
140 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
141 unification_kind_t
, int,
143 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
144 unification_kind_t
, int, bool, bool);
145 static int unify (tree
, tree
, tree
, tree
, int, bool);
146 static void add_pending_template (tree
);
147 static tree
reopen_tinst_level (struct tinst_level
*);
148 static tree
tsubst_initializer_list (tree
, tree
);
149 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
150 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
154 static void tsubst_enum (tree
, tree
, tree
);
155 static tree
add_to_template_args (tree
, tree
);
156 static tree
add_outermost_template_args (tree
, tree
);
157 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
158 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
160 static int type_unification_real (tree
, tree
, tree
, const tree
*,
161 unsigned int, int, unification_kind_t
, int,
162 vec
<deferred_access_check
, va_gc
> **,
164 static void note_template_header (int);
165 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
167 static tree
convert_template_argument (tree
, tree
, tree
,
168 tsubst_flags_t
, int, tree
);
169 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
170 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
171 static tree
expand_template_argument_pack (tree
);
172 static tree
build_template_parm_index (int, int, int, tree
, tree
);
173 static bool inline_needs_template_parms (tree
, bool);
174 static void push_inline_template_parms_recursive (tree
, int);
175 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
176 static int mark_template_parm (tree
, void *);
177 static int template_parm_this_level_p (tree
, void *);
178 static tree
tsubst_friend_function (tree
, tree
);
179 static tree
tsubst_friend_class (tree
, tree
);
180 static int can_complete_type_without_circularity (tree
);
181 static tree
get_bindings (tree
, tree
, tree
, bool);
182 static int template_decl_level (tree
);
183 static int check_cv_quals_for_unify (int, tree
, tree
);
184 static void template_parm_level_and_index (tree
, int*, int*);
185 static int unify_pack_expansion (tree
, tree
, tree
,
186 tree
, unification_kind_t
, bool, bool);
187 static tree
copy_template_args (tree
);
188 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
189 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
190 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
191 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
192 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
193 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
194 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
195 static bool check_specialization_scope (void);
196 static tree
process_partial_specialization (tree
);
197 static void set_current_access_from_decl (tree
);
198 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
200 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
201 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
203 static bool template_template_parm_bindings_ok_p (tree
, tree
);
204 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
205 static tree
for_each_template_parm_r (tree
*, int *, void *);
206 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
207 static void copy_default_args_to_explicit_spec (tree
);
208 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
209 static bool dependent_template_arg_p (tree
);
210 static bool any_template_arguments_need_structural_equality_p (tree
);
211 static bool dependent_type_p_r (tree
);
212 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
213 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
214 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
215 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
217 static tree
listify (tree
);
218 static tree
listify_autos (tree
, tree
);
219 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
220 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
221 static bool complex_alias_template_p (const_tree tmpl
);
222 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
223 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
224 static tree
make_argument_pack (tree
);
225 static void register_parameter_specializations (tree
, tree
);
226 static tree
enclosing_instantiation_of (tree tctx
);
228 /* Make the current scope suitable for access checking when we are
229 processing T. T can be FUNCTION_DECL for instantiated function
230 template, VAR_DECL for static member variable, or TYPE_DECL for
231 alias template (needed by instantiate_decl). */
234 push_access_scope (tree t
)
236 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
237 || TREE_CODE (t
) == TYPE_DECL
);
239 if (DECL_FRIEND_CONTEXT (t
))
240 push_nested_class (DECL_FRIEND_CONTEXT (t
));
241 else if (DECL_CLASS_SCOPE_P (t
))
242 push_nested_class (DECL_CONTEXT (t
));
244 push_to_top_level ();
246 if (TREE_CODE (t
) == FUNCTION_DECL
)
248 saved_access_scope
= tree_cons
249 (NULL_TREE
, current_function_decl
, saved_access_scope
);
250 current_function_decl
= t
;
254 /* Restore the scope set up by push_access_scope. T is the node we
258 pop_access_scope (tree t
)
260 if (TREE_CODE (t
) == FUNCTION_DECL
)
262 current_function_decl
= TREE_VALUE (saved_access_scope
);
263 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
266 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
269 pop_from_top_level ();
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
278 finish_member_template_decl (tree decl
)
280 if (decl
== error_mark_node
)
281 return error_mark_node
;
283 gcc_assert (DECL_P (decl
));
285 if (TREE_CODE (decl
) == TYPE_DECL
)
289 type
= TREE_TYPE (decl
);
290 if (type
== error_mark_node
)
291 return error_mark_node
;
292 if (MAYBE_CLASS_TYPE_P (type
)
293 && CLASSTYPE_TEMPLATE_INFO (type
)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
296 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
297 check_member_template (tmpl
);
302 else if (TREE_CODE (decl
) == FIELD_DECL
)
303 error ("data member %qD cannot be a member template", decl
);
304 else if (DECL_TEMPLATE_INFO (decl
))
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
308 check_member_template (DECL_TI_TEMPLATE (decl
));
309 return DECL_TI_TEMPLATE (decl
);
315 error ("invalid member template declaration %qD", decl
);
317 return error_mark_node
;
320 /* Create a template info node. */
323 build_template_info (tree template_decl
, tree template_args
)
325 tree result
= make_node (TEMPLATE_INFO
);
326 TI_TEMPLATE (result
) = template_decl
;
327 TI_ARGS (result
) = template_args
;
331 /* Return the template info node corresponding to T, whatever T is. */
334 get_template_info (const_tree t
)
336 tree tinfo
= NULL_TREE
;
338 if (!t
|| t
== error_mark_node
)
341 if (TREE_CODE (t
) == NAMESPACE_DECL
342 || TREE_CODE (t
) == PARM_DECL
)
345 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
346 tinfo
= DECL_TEMPLATE_INFO (t
);
348 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
351 if (OVERLOAD_TYPE_P (t
))
352 tinfo
= TYPE_TEMPLATE_INFO (t
);
353 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
354 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
359 /* Returns the template nesting level of the indicated class TYPE.
369 A<T>::B<U> has depth two, while A<T> has depth one.
370 Both A<T>::B<int> and A<int>::B<U> have depth one, if
371 they are instantiations, not specializations.
373 This function is guaranteed to return 0 if passed NULL_TREE so
374 that, for example, `template_class_depth (current_class_type)' is
378 template_class_depth (tree type
)
382 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
384 tree tinfo
= get_template_info (type
);
386 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
387 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
391 type
= CP_DECL_CONTEXT (type
);
392 else if (LAMBDA_TYPE_P (type
))
393 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
395 type
= CP_TYPE_CONTEXT (type
);
401 /* Subroutine of maybe_begin_member_template_processing.
402 Returns true if processing DECL needs us to push template parms. */
405 inline_needs_template_parms (tree decl
, bool nsdmi
)
407 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
410 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
411 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
414 /* Subroutine of maybe_begin_member_template_processing.
415 Push the template parms in PARMS, starting from LEVELS steps into the
416 chain, and ending at the beginning, since template parms are listed
420 push_inline_template_parms_recursive (tree parmlist
, int levels
)
422 tree parms
= TREE_VALUE (parmlist
);
426 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
428 ++processing_template_decl
;
429 current_template_parms
430 = tree_cons (size_int (processing_template_decl
),
431 parms
, current_template_parms
);
432 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
434 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
436 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
438 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
440 if (error_operand_p (parm
))
443 gcc_assert (DECL_P (parm
));
445 switch (TREE_CODE (parm
))
453 /* Push the CONST_DECL. */
454 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
463 /* Restore the template parameter context for a member template, a
464 friend template defined in a class definition, or a non-template
465 member of template class. */
468 maybe_begin_member_template_processing (tree decl
)
472 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
476 tree ctx
= DECL_CONTEXT (decl
);
477 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
478 /* Disregard full specializations (c++/60999). */
479 && uses_template_parms (ctx
)
480 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
483 if (inline_needs_template_parms (decl
, nsdmi
))
485 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
486 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
488 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
491 parms
= TREE_CHAIN (parms
);
494 push_inline_template_parms_recursive (parms
, levels
);
497 /* Remember how many levels of template parameters we pushed so that
498 we can pop them later. */
499 inline_parm_levels
.safe_push (levels
);
502 /* Undo the effects of maybe_begin_member_template_processing. */
505 maybe_end_member_template_processing (void)
510 if (inline_parm_levels
.length () == 0)
513 last
= inline_parm_levels
.pop ();
514 for (i
= 0; i
< last
; ++i
)
516 --processing_template_decl
;
517 current_template_parms
= TREE_CHAIN (current_template_parms
);
522 /* Return a new template argument vector which contains all of ARGS,
523 but has as its innermost set of arguments the EXTRA_ARGS. */
526 add_to_template_args (tree args
, tree extra_args
)
533 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
536 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
537 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
539 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
540 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
542 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
543 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
548 /* Like add_to_template_args, but only the outermost ARGS are added to
549 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
550 (EXTRA_ARGS) levels are added. This function is used to combine
551 the template arguments from a partial instantiation with the
552 template arguments used to attain the full instantiation from the
553 partial instantiation. */
556 add_outermost_template_args (tree args
, tree extra_args
)
560 /* If there are more levels of EXTRA_ARGS than there are ARGS,
561 something very fishy is going on. */
562 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
564 /* If *all* the new arguments will be the EXTRA_ARGS, just return
566 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
569 /* For the moment, we make ARGS look like it contains fewer levels. */
570 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
572 new_args
= add_to_template_args (args
, extra_args
);
574 /* Now, we restore ARGS to its full dimensions. */
575 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
580 /* Return the N levels of innermost template arguments from the ARGS. */
583 get_innermost_template_args (tree args
, int n
)
591 /* If N is 1, just return the innermost set of template arguments. */
593 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
595 /* If we're not removing anything, just return the arguments we were
597 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
598 gcc_assert (extra_levels
>= 0);
599 if (extra_levels
== 0)
602 /* Make a new set of arguments, not containing the outer arguments. */
603 new_args
= make_tree_vec (n
);
604 for (i
= 1; i
<= n
; ++i
)
605 SET_TMPL_ARGS_LEVEL (new_args
, i
,
606 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
611 /* The inverse of get_innermost_template_args: Return all but the innermost
612 EXTRA_LEVELS levels of template arguments from the ARGS. */
615 strip_innermost_template_args (tree args
, int extra_levels
)
618 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
623 /* If N is 1, just return the outermost set of template arguments. */
625 return TMPL_ARGS_LEVEL (args
, 1);
627 /* If we're not removing anything, just return the arguments we were
629 gcc_assert (extra_levels
>= 0);
630 if (extra_levels
== 0)
633 /* Make a new set of arguments, not containing the inner arguments. */
634 new_args
= make_tree_vec (n
);
635 for (i
= 1; i
<= n
; ++i
)
636 SET_TMPL_ARGS_LEVEL (new_args
, i
,
637 TMPL_ARGS_LEVEL (args
, i
));
642 /* We've got a template header coming up; push to a new level for storing
646 begin_template_parm_list (void)
648 /* We use a non-tag-transparent scope here, which causes pushtag to
649 put tags in this scope, rather than in the enclosing class or
650 namespace scope. This is the right thing, since we want
651 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
652 global template class, push_template_decl handles putting the
653 TEMPLATE_DECL into top-level scope. For a nested template class,
656 template <class T> struct S1 {
657 template <class T> struct S2 {};
660 pushtag contains special code to insert the TEMPLATE_DECL for S2
661 at the right scope. */
662 begin_scope (sk_template_parms
, NULL
);
663 ++processing_template_decl
;
664 ++processing_template_parmlist
;
665 note_template_header (0);
667 /* Add a dummy parameter level while we process the parameter list. */
668 current_template_parms
669 = tree_cons (size_int (processing_template_decl
),
671 current_template_parms
);
674 /* This routine is called when a specialization is declared. If it is
675 invalid to declare a specialization here, an error is reported and
676 false is returned, otherwise this routine will return true. */
679 check_specialization_scope (void)
681 tree scope
= current_scope ();
685 An explicit specialization shall be declared in the namespace of
686 which the template is a member, or, for member templates, in the
687 namespace of which the enclosing class or enclosing class
688 template is a member. An explicit specialization of a member
689 function, member class or static data member of a class template
690 shall be declared in the namespace of which the class template
692 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
694 error ("explicit specialization in non-namespace scope %qD", scope
);
700 In an explicit specialization declaration for a member of a class
701 template or a member template that appears in namespace scope,
702 the member template and some of its enclosing class templates may
703 remain unspecialized, except that the declaration shall not
704 explicitly specialize a class member template if its enclosing
705 class templates are not explicitly specialized as well. */
706 if (current_template_parms
)
708 error ("enclosing class templates are not explicitly specialized");
715 /* We've just seen template <>. */
718 begin_specialization (void)
720 begin_scope (sk_template_spec
, NULL
);
721 note_template_header (1);
722 return check_specialization_scope ();
725 /* Called at then end of processing a declaration preceded by
729 end_specialization (void)
732 reset_specialization ();
735 /* Any template <>'s that we have seen thus far are not referring to a
736 function specialization. */
739 reset_specialization (void)
741 processing_specialization
= 0;
742 template_header_count
= 0;
745 /* We've just seen a template header. If SPECIALIZATION is nonzero,
746 it was of the form template <>. */
749 note_template_header (int specialization
)
751 processing_specialization
= specialization
;
752 template_header_count
++;
755 /* We're beginning an explicit instantiation. */
758 begin_explicit_instantiation (void)
760 gcc_assert (!processing_explicit_instantiation
);
761 processing_explicit_instantiation
= true;
766 end_explicit_instantiation (void)
768 gcc_assert (processing_explicit_instantiation
);
769 processing_explicit_instantiation
= false;
772 /* An explicit specialization or partial specialization of TMPL is being
773 declared. Check that the namespace in which the specialization is
774 occurring is permissible. Returns false iff it is invalid to
775 specialize TMPL in the current namespace. */
778 check_specialization_namespace (tree tmpl
)
780 tree tpl_ns
= decl_namespace_context (tmpl
);
784 An explicit specialization shall be declared in a namespace enclosing the
785 specialized template. An explicit specialization whose declarator-id is
786 not qualified shall be declared in the nearest enclosing namespace of the
787 template, or, if the namespace is inline (7.3.1), any namespace from its
788 enclosing namespace set. */
789 if (current_scope() != DECL_CONTEXT (tmpl
)
790 && !at_namespace_scope_p ())
792 error ("specialization of %qD must appear at namespace scope", tmpl
);
796 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
797 /* Same or enclosing namespace. */
801 permerror (input_location
,
802 "specialization of %qD in different namespace", tmpl
);
803 inform (DECL_SOURCE_LOCATION (tmpl
),
804 " from definition of %q#D", tmpl
);
809 /* SPEC is an explicit instantiation. Check that it is valid to
810 perform this explicit instantiation in the current namespace. */
813 check_explicit_instantiation_namespace (tree spec
)
817 /* DR 275: An explicit instantiation shall appear in an enclosing
818 namespace of its template. */
819 ns
= decl_namespace_context (spec
);
820 if (!is_nested_namespace (current_namespace
, ns
))
821 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
822 "(which does not enclose namespace %qD)",
823 spec
, current_namespace
, ns
);
826 // Returns the type of a template specialization only if that
827 // specialization needs to be defined. Otherwise (e.g., if the type has
828 // already been defined), the function returns NULL_TREE.
830 maybe_new_partial_specialization (tree type
)
832 // An implicit instantiation of an incomplete type implies
833 // the definition of a new class template.
835 // template<typename T>
838 // template<typename T>
841 // Here, S<T*> is an implicit instantiation of S whose type
843 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
846 // It can also be the case that TYPE is a completed specialization.
847 // Continuing the previous example, suppose we also declare:
849 // template<typename T>
850 // requires Integral<T>
853 // Here, S<T*> refers to the specialization S<T*> defined
854 // above. However, we need to differentiate definitions because
855 // we intend to define a new partial specialization. In this case,
856 // we rely on the fact that the constraints are different for
857 // this declaration than that above.
859 // Note that we also get here for injected class names and
860 // late-parsed template definitions. We must ensure that we
861 // do not create new type declarations for those cases.
862 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
864 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
865 tree args
= CLASSTYPE_TI_ARGS (type
);
867 // If there are no template parameters, this cannot be a new
868 // partial template specializtion?
869 if (!current_template_parms
)
872 // The injected-class-name is not a new partial specialization.
873 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
876 // If the constraints are not the same as those of the primary
877 // then, we can probably create a new specialization.
878 tree type_constr
= current_template_constraints ();
880 if (type
== TREE_TYPE (tmpl
))
882 tree main_constr
= get_constraints (tmpl
);
883 if (equivalent_constraints (type_constr
, main_constr
))
887 // Also, if there's a pre-existing specialization with matching
888 // constraints, then this also isn't new.
889 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
892 tree spec_tmpl
= TREE_VALUE (specs
);
893 tree spec_args
= TREE_PURPOSE (specs
);
894 tree spec_constr
= get_constraints (spec_tmpl
);
895 if (comp_template_args (args
, spec_args
)
896 && equivalent_constraints (type_constr
, spec_constr
))
898 specs
= TREE_CHAIN (specs
);
901 // Create a new type node (and corresponding type decl)
902 // for the newly declared specialization.
903 tree t
= make_class_type (TREE_CODE (type
));
904 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
905 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
907 /* We only need a separate type node for storing the definition of this
908 partial specialization; uses of S<T*> are unconstrained, so all are
909 equivalent. So keep TYPE_CANONICAL the same. */
910 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
912 // Build the corresponding type decl.
913 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
914 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
915 DECL_SOURCE_LOCATION (d
) = input_location
;
923 /* The TYPE is being declared. If it is a template type, that means it
924 is a partial specialization. Do appropriate error-checking. */
927 maybe_process_partial_specialization (tree type
)
931 if (type
== error_mark_node
)
932 return error_mark_node
;
934 /* A lambda that appears in specialization context is not itself a
936 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
939 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
941 error ("name of class shadows template template parameter %qD",
943 return error_mark_node
;
946 context
= TYPE_CONTEXT (type
);
948 if (TYPE_ALIAS_P (type
))
950 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
952 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
953 error ("specialization of alias template %qD",
954 TI_TEMPLATE (tinfo
));
956 error ("explicit specialization of non-template %qT", type
);
957 return error_mark_node
;
959 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
961 /* This is for ordinary explicit specialization and partial
962 specialization of a template class such as:
964 template <> class C<int>;
968 template <class T> class C<T*>;
970 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
972 if (tree t
= maybe_new_partial_specialization (type
))
974 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
975 && !at_namespace_scope_p ())
976 return error_mark_node
;
977 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
978 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
979 if (processing_template_decl
)
981 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
982 if (decl
== error_mark_node
)
983 return error_mark_node
;
984 return TREE_TYPE (decl
);
987 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
988 error ("specialization of %qT after instantiation", type
);
989 else if (errorcount
&& !processing_specialization
990 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
991 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
992 /* Trying to define a specialization either without a template<> header
993 or in an inappropriate place. We've already given an error, so just
994 bail now so we don't actually define the specialization. */
995 return error_mark_node
;
997 else if (CLASS_TYPE_P (type
)
998 && !CLASSTYPE_USE_TEMPLATE (type
)
999 && CLASSTYPE_TEMPLATE_INFO (type
)
1000 && context
&& CLASS_TYPE_P (context
)
1001 && CLASSTYPE_TEMPLATE_INFO (context
))
1003 /* This is for an explicit specialization of member class
1004 template according to [temp.expl.spec/18]:
1006 template <> template <class U> class C<int>::D;
1008 The context `C<int>' must be an implicit instantiation.
1009 Otherwise this is just a member class template declared
1012 template <> class C<int> { template <class U> class D; };
1013 template <> template <class U> class C<int>::D;
1015 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1016 while in the second case, `C<int>::D' is a primary template
1017 and `C<T>::D' may not exist. */
1019 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1020 && !COMPLETE_TYPE_P (type
))
1023 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1025 if (current_namespace
1026 != decl_namespace_context (tmpl
))
1028 permerror (input_location
,
1029 "specializing %q#T in different namespace", type
);
1030 permerror (DECL_SOURCE_LOCATION (tmpl
),
1031 " from definition of %q#D", tmpl
);
1034 /* Check for invalid specialization after instantiation:
1036 template <> template <> class C<int>::D<int>;
1037 template <> template <class U> class C<int>::D; */
1039 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1040 t
; t
= TREE_CHAIN (t
))
1042 tree inst
= TREE_VALUE (t
);
1043 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1044 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1046 /* We already have a full specialization of this partial
1047 instantiation, or a full specialization has been
1048 looked up but not instantiated. Reassign it to the
1049 new member specialization template. */
1053 elt
.tmpl
= most_general_template (tmpl
);
1054 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1057 type_specializations
->remove_elt (&elt
);
1060 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1063 = type_specializations
->find_slot (&elt
, INSERT
);
1064 entry
= ggc_alloc
<spec_entry
> ();
1069 /* But if we've had an implicit instantiation, that's a
1070 problem ([temp.expl.spec]/6). */
1071 error ("specialization %qT after instantiation %qT",
1075 /* Mark TYPE as a specialization. And as a result, we only
1076 have one level of template argument for the innermost
1078 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1079 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1080 CLASSTYPE_TI_ARGS (type
)
1081 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1084 else if (processing_specialization
)
1086 /* Someday C++0x may allow for enum template specialization. */
1087 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1088 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1089 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1090 "of %qD not allowed by ISO C++", type
);
1093 error ("explicit specialization of non-template %qT", type
);
1094 return error_mark_node
;
1101 /* Returns nonzero if we can optimize the retrieval of specializations
1102 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1103 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1106 optimize_specialization_lookup_p (tree tmpl
)
1108 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1109 && DECL_CLASS_SCOPE_P (tmpl
)
1110 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1112 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1113 /* The optimized lookup depends on the fact that the
1114 template arguments for the member function template apply
1115 purely to the containing class, which is not true if the
1116 containing class is an explicit or partial
1118 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1119 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1120 && !DECL_CONV_FN_P (tmpl
)
1121 /* It is possible to have a template that is not a member
1122 template and is not a member of a template class:
1124 template <typename T>
1125 struct S { friend A::f(); };
1127 Here, the friend function is a template, but the context does
1128 not have template information. The optimized lookup relies
1129 on having ARGS be the template arguments for both the class
1130 and the function template. */
1131 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1134 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1135 gone through coerce_template_parms by now. */
1138 verify_unstripped_args (tree args
)
1140 ++processing_template_decl
;
1141 if (!any_dependent_template_arguments_p (args
))
1143 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1144 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1146 tree arg
= TREE_VEC_ELT (inner
, i
);
1147 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1149 else if (TYPE_P (arg
))
1150 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1151 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1152 /* Allow typedefs on the type of a non-type argument, since a
1153 parameter can have them. */;
1155 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1158 --processing_template_decl
;
1161 /* Retrieve the specialization (in the sense of [temp.spec] - a
1162 specialization is either an instantiation or an explicit
1163 specialization) of TMPL for the given template ARGS. If there is
1164 no such specialization, return NULL_TREE. The ARGS are a vector of
1165 arguments, or a vector of vectors of arguments, in the case of
1166 templates with more than one level of parameters.
1168 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1169 then we search for a partial specialization matching ARGS. This
1170 parameter is ignored if TMPL is not a class template.
1172 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1173 result is a NONTYPE_ARGUMENT_PACK. */
1176 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1178 if (tmpl
== NULL_TREE
)
1181 if (args
== error_mark_node
)
1184 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1185 || TREE_CODE (tmpl
) == FIELD_DECL
);
1187 /* There should be as many levels of arguments as there are
1188 levels of parameters. */
1189 gcc_assert (TMPL_ARGS_DEPTH (args
)
1190 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1191 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1192 : template_class_depth (DECL_CONTEXT (tmpl
))));
1195 verify_unstripped_args (args
);
1197 /* Lambda functions in templates aren't instantiated normally, but through
1198 tsubst_lambda_expr. */
1199 if (lambda_fn_in_template_p (tmpl
))
1202 if (optimize_specialization_lookup_p (tmpl
))
1204 /* The template arguments actually apply to the containing
1205 class. Find the class specialization with those
1207 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1208 tree class_specialization
1209 = retrieve_specialization (class_template
, args
, 0);
1210 if (!class_specialization
)
1213 /* Find the instance of TMPL. */
1214 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1215 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1218 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1219 /* using-declarations can add base methods to the method vec,
1220 and we don't want those here. */
1221 && DECL_CONTEXT (fn
) == class_specialization
)
1230 hash_table
<spec_hasher
> *specializations
;
1234 elt
.spec
= NULL_TREE
;
1236 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1237 specializations
= type_specializations
;
1239 specializations
= decl_specializations
;
1242 hash
= spec_hasher::hash (&elt
);
1243 found
= specializations
->find_with_hash (&elt
, hash
);
1251 /* Like retrieve_specialization, but for local declarations. */
1254 retrieve_local_specialization (tree tmpl
)
1256 if (local_specializations
== NULL
)
1259 tree
*slot
= local_specializations
->get (tmpl
);
1260 return slot
? *slot
: NULL_TREE
;
1263 /* Returns nonzero iff DECL is a specialization of TMPL. */
1266 is_specialization_of (tree decl
, tree tmpl
)
1270 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1274 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1280 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1282 for (t
= TREE_TYPE (decl
);
1284 t
= CLASSTYPE_USE_TEMPLATE (t
)
1285 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1286 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1293 /* Returns nonzero iff DECL is a specialization of friend declaration
1294 FRIEND_DECL according to [temp.friend]. */
1297 is_specialization_of_friend (tree decl
, tree friend_decl
)
1299 bool need_template
= true;
1302 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1303 || TREE_CODE (decl
) == TYPE_DECL
);
1305 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1306 of a template class, we want to check if DECL is a specialization
1308 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1309 && DECL_TEMPLATE_INFO (friend_decl
)
1310 && !DECL_USE_TEMPLATE (friend_decl
))
1312 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1313 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1314 need_template
= false;
1316 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1317 && !PRIMARY_TEMPLATE_P (friend_decl
))
1318 need_template
= false;
1320 /* There is nothing to do if this is not a template friend. */
1321 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1324 if (is_specialization_of (decl
, friend_decl
))
1328 A member of a class template may be declared to be a friend of a
1329 non-template class. In this case, the corresponding member of
1330 every specialization of the class template is a friend of the
1331 class granting friendship.
1333 For example, given a template friend declaration
1335 template <class T> friend void A<T>::f();
1337 the member function below is considered a friend
1339 template <> struct A<int> {
1343 For this type of template friend, TEMPLATE_DEPTH below will be
1344 nonzero. To determine if DECL is a friend of FRIEND, we first
1345 check if the enclosing class is a specialization of another. */
1347 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1349 && DECL_CLASS_SCOPE_P (decl
)
1350 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1351 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1353 /* Next, we check the members themselves. In order to handle
1354 a few tricky cases, such as when FRIEND_DECL's are
1356 template <class T> friend void A<T>::g(T t);
1357 template <class T> template <T t> friend void A<T>::h();
1361 void A<int>::g(int);
1362 template <int> void A<int>::h();
1364 we need to figure out ARGS, the template arguments from
1365 the context of DECL. This is required for template substitution
1366 of `T' in the function parameter of `g' and template parameter
1367 of `h' in the above examples. Here ARGS corresponds to `int'. */
1369 tree context
= DECL_CONTEXT (decl
);
1370 tree args
= NULL_TREE
;
1371 int current_depth
= 0;
1373 while (current_depth
< template_depth
)
1375 if (CLASSTYPE_TEMPLATE_INFO (context
))
1377 if (current_depth
== 0)
1378 args
= TYPE_TI_ARGS (context
);
1380 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1383 context
= TYPE_CONTEXT (context
);
1386 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1391 tree friend_args_type
;
1392 tree decl_args_type
;
1394 /* Make sure that both DECL and FRIEND_DECL are templates or
1396 is_template
= DECL_TEMPLATE_INFO (decl
)
1397 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1398 if (need_template
^ is_template
)
1400 else if (is_template
)
1402 /* If both are templates, check template parameter list. */
1404 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1406 if (!comp_template_parms
1407 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1411 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1414 decl_type
= TREE_TYPE (decl
);
1416 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1417 tf_none
, NULL_TREE
);
1418 if (friend_type
== error_mark_node
)
1421 /* Check if return types match. */
1422 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1425 /* Check if function parameter types match, ignoring the
1426 `this' parameter. */
1427 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1428 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1429 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1430 friend_args_type
= TREE_CHAIN (friend_args_type
);
1431 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1432 decl_args_type
= TREE_CHAIN (decl_args_type
);
1434 return compparms (decl_args_type
, friend_args_type
);
1438 /* DECL is a TYPE_DECL */
1440 tree decl_type
= TREE_TYPE (decl
);
1442 /* Make sure that both DECL and FRIEND_DECL are templates or
1445 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1446 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1448 if (need_template
^ is_template
)
1450 else if (is_template
)
1453 /* If both are templates, check the name of the two
1454 TEMPLATE_DECL's first because is_friend didn't. */
1455 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1456 != DECL_NAME (friend_decl
))
1459 /* Now check template parameter list. */
1461 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1463 return comp_template_parms
1464 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1468 return (DECL_NAME (decl
)
1469 == DECL_NAME (friend_decl
));
1475 /* Register the specialization SPEC as a specialization of TMPL with
1476 the indicated ARGS. IS_FRIEND indicates whether the specialization
1477 is actually just a friend declaration. ATTRLIST is the list of
1478 attributes that the specialization is declared with or NULL when
1479 it isn't. Returns SPEC, or an equivalent prior declaration, if
1482 We also store instantiations of field packs in the hash table, even
1483 though they are not themselves templates, to make lookup easier. */
1486 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1490 spec_entry
**slot
= NULL
;
1493 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1494 || (TREE_CODE (tmpl
) == FIELD_DECL
1495 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1497 if (TREE_CODE (spec
) == FUNCTION_DECL
1498 && uses_template_parms (DECL_TI_ARGS (spec
)))
1499 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1500 register it; we want the corresponding TEMPLATE_DECL instead.
1501 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1502 the more obvious `uses_template_parms (spec)' to avoid problems
1503 with default function arguments. In particular, given
1504 something like this:
1506 template <class T> void f(T t1, T t = T())
1508 the default argument expression is not substituted for in an
1509 instantiation unless and until it is actually needed. */
1512 if (optimize_specialization_lookup_p (tmpl
))
1513 /* We don't put these specializations in the hash table, but we might
1514 want to give an error about a mismatch. */
1515 fn
= retrieve_specialization (tmpl
, args
, 0);
1523 hash
= spec_hasher::hash (&elt
);
1526 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1528 fn
= ((spec_entry
*) *slot
)->spec
;
1533 /* We can sometimes try to re-register a specialization that we've
1534 already got. In particular, regenerate_decl_from_template calls
1535 duplicate_decls which will update the specialization list. But,
1536 we'll still get called again here anyhow. It's more convenient
1537 to simply allow this than to try to prevent it. */
1540 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1542 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1544 if (DECL_ODR_USED (fn
)
1545 || DECL_EXPLICIT_INSTANTIATION (fn
))
1547 error ("specialization of %qD after instantiation",
1549 return error_mark_node
;
1554 /* This situation should occur only if the first
1555 specialization is an implicit instantiation, the
1556 second is an explicit specialization, and the
1557 implicit instantiation has not yet been used. That
1558 situation can occur if we have implicitly
1559 instantiated a member function and then specialized
1562 We can also wind up here if a friend declaration that
1563 looked like an instantiation turns out to be a
1566 template <class T> void foo(T);
1567 class S { friend void foo<>(int) };
1568 template <> void foo(int);
1570 We transform the existing DECL in place so that any
1571 pointers to it become pointers to the updated
1574 If there was a definition for the template, but not
1575 for the specialization, we want this to look as if
1576 there were no definition, and vice versa. */
1577 DECL_INITIAL (fn
) = NULL_TREE
;
1578 duplicate_decls (spec
, fn
, is_friend
);
1579 /* The call to duplicate_decls will have applied
1582 An explicit specialization of a function template
1583 is inline only if it is explicitly declared to be,
1584 and independently of whether its function template
1587 to the primary function; now copy the inline bits to
1588 the various clones. */
1589 FOR_EACH_CLONE (clone
, fn
)
1591 DECL_DECLARED_INLINE_P (clone
)
1592 = DECL_DECLARED_INLINE_P (fn
);
1593 DECL_SOURCE_LOCATION (clone
)
1594 = DECL_SOURCE_LOCATION (fn
);
1595 DECL_DELETED_FN (clone
)
1596 = DECL_DELETED_FN (fn
);
1598 check_specialization_namespace (tmpl
);
1603 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1605 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1606 if (dd
== error_mark_node
)
1607 /* We've already complained in duplicate_decls. */
1608 return error_mark_node
;
1610 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1611 /* Dup decl failed, but this is a new definition. Set the
1612 line number so any errors match this new
1614 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1620 return duplicate_decls (spec
, fn
, is_friend
);
1622 /* A specialization must be declared in the same namespace as the
1623 template it is specializing. */
1624 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1625 && !check_specialization_namespace (tmpl
))
1626 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1628 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1630 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1631 gcc_assert (tmpl
&& args
&& spec
);
1634 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1635 && PRIMARY_TEMPLATE_P (tmpl
)
1636 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1637 || variable_template_p (tmpl
))
1638 /* If TMPL is a forward declaration of a template function, keep a list
1639 of all specializations in case we need to reassign them to a friend
1640 template later in tsubst_friend_function.
1642 Also keep a list of all variable template instantiations so that
1643 process_partial_specialization can check whether a later partial
1644 specialization would have used it. */
1645 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1646 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1652 /* Returns true iff two spec_entry nodes are equivalent. */
1654 int comparing_specializations
;
1657 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1661 ++comparing_specializations
;
1662 equal
= (e1
->tmpl
== e2
->tmpl
1663 && comp_template_args (e1
->args
, e2
->args
));
1664 if (equal
&& flag_concepts
1665 /* tmpl could be a FIELD_DECL for a capture pack. */
1666 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1667 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1668 && uses_template_parms (e1
->args
))
1670 /* Partial specializations of a variable template can be distinguished by
1672 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1673 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1674 equal
= equivalent_constraints (c1
, c2
);
1676 --comparing_specializations
;
1681 /* Returns a hash for a template TMPL and template arguments ARGS. */
1684 hash_tmpl_and_args (tree tmpl
, tree args
)
1686 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1687 return iterative_hash_template_arg (args
, val
);
1690 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1694 spec_hasher::hash (spec_entry
*e
)
1696 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1699 /* Recursively calculate a hash value for a template argument ARG, for use
1700 in the hash tables of template specializations. */
1703 iterative_hash_template_arg (tree arg
, hashval_t val
)
1705 unsigned HOST_WIDE_INT i
;
1706 enum tree_code code
;
1709 if (arg
== NULL_TREE
)
1710 return iterative_hash_object (arg
, val
);
1715 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1718 code
= TREE_CODE (arg
);
1719 tclass
= TREE_CODE_CLASS (code
);
1721 val
= iterative_hash_object (code
, val
);
1728 case IDENTIFIER_NODE
:
1729 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1733 int i
, len
= TREE_VEC_LENGTH (arg
);
1734 for (i
= 0; i
< len
; ++i
)
1735 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1739 case TYPE_PACK_EXPANSION
:
1740 case EXPR_PACK_EXPANSION
:
1741 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1742 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1744 case TYPE_ARGUMENT_PACK
:
1745 case NONTYPE_ARGUMENT_PACK
:
1746 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1749 for (; arg
; arg
= TREE_CHAIN (arg
))
1750 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1754 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1755 val
= iterative_hash_template_arg (*iter
, val
);
1761 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1762 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1764 val
= iterative_hash_template_arg (field
, val
);
1765 val
= iterative_hash_template_arg (value
, val
);
1771 if (!DECL_ARTIFICIAL (arg
))
1773 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1774 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1776 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1779 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1782 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1783 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1785 case TEMPLATE_PARM_INDEX
:
1786 val
= iterative_hash_template_arg
1787 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1788 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1789 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1792 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1793 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1794 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1797 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1799 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1803 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1804 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1805 val
= iterative_hash_object (code
, val
);
1806 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1809 /* A lambda can't appear in a template arg, but don't crash on
1811 gcc_assert (seen_error ());
1815 case IMPLICIT_CONV_EXPR
:
1816 case STATIC_CAST_EXPR
:
1817 case REINTERPRET_CAST_EXPR
:
1818 case CONST_CAST_EXPR
:
1819 case DYNAMIC_CAST_EXPR
:
1821 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1822 /* Now hash operands as usual. */
1832 if (alias_template_specialization_p (arg
))
1834 // We want an alias specialization that survived strip_typedefs
1835 // to hash differently from its TYPE_CANONICAL, to avoid hash
1836 // collisions that compare as different in template_args_equal.
1837 // These could be dependent specializations that strip_typedefs
1838 // left alone, or untouched specializations because
1839 // coerce_template_parms returns the unconverted template
1840 // arguments if it sees incomplete argument packs.
1841 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1842 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1844 if (TYPE_CANONICAL (arg
))
1845 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1847 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1848 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1849 /* Otherwise just compare the types during lookup. */
1852 case tcc_declaration
:
1854 return iterative_hash_expr (arg
, val
);
1857 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1859 unsigned n
= cp_tree_operand_length (arg
);
1860 for (i
= 0; i
< n
; ++i
)
1861 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1869 /* Unregister the specialization SPEC as a specialization of TMPL.
1870 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1871 if the SPEC was listed as a specialization of TMPL.
1873 Note that SPEC has been ggc_freed, so we can't look inside it. */
1876 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1881 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1882 elt
.args
= TI_ARGS (tinfo
);
1883 elt
.spec
= NULL_TREE
;
1885 entry
= decl_specializations
->find (&elt
);
1888 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1889 gcc_assert (new_spec
!= NULL_TREE
);
1890 entry
->spec
= new_spec
;
1897 /* Like register_specialization, but for local declarations. We are
1898 registering SPEC, an instantiation of TMPL. */
1901 register_local_specialization (tree spec
, tree tmpl
)
1903 gcc_assert (tmpl
!= spec
);
1904 local_specializations
->put (tmpl
, spec
);
1907 /* TYPE is a class type. Returns true if TYPE is an explicitly
1908 specialized class. */
1911 explicit_class_specialization_p (tree type
)
1913 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1915 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1918 /* Print the list of functions at FNS, going through all the overloads
1919 for each element of the list. Alternatively, FNS can not be a
1920 TREE_LIST, in which case it will be printed together with all the
1923 MORE and *STR should respectively be FALSE and NULL when the function
1924 is called from the outside. They are used internally on recursive
1925 calls. print_candidates manages the two parameters and leaves NULL
1926 in *STR when it ends. */
1929 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1931 if (TREE_CODE (fns
) == TREE_LIST
)
1932 for (; fns
; fns
= TREE_CHAIN (fns
))
1933 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1935 for (lkp_iterator
iter (fns
); iter
;)
1940 const char *pfx
= *str
;
1944 pfx
= _("candidates are:");
1946 pfx
= _("candidate is:");
1947 *str
= get_spaces (pfx
);
1949 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1953 /* Print the list of candidate FNS in an error message. FNS can also
1954 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1957 print_candidates (tree fns
)
1960 print_candidates_1 (fns
, &str
);
1964 /* Get a (possibly) constrained template declaration for the
1965 purpose of ordering candidates. */
1967 get_template_for_ordering (tree list
)
1969 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1970 tree f
= TREE_VALUE (list
);
1971 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1972 return TI_TEMPLATE (ti
);
1976 /* Among candidates having the same signature, return the
1977 most constrained or NULL_TREE if there is no best candidate.
1978 If the signatures of candidates vary (e.g., template
1979 specialization vs. member function), then there can be no
1982 Note that we don't compare constraints on the functions
1983 themselves, but rather those of their templates. */
1985 most_constrained_function (tree candidates
)
1987 // Try to find the best candidate in a first pass.
1988 tree champ
= candidates
;
1989 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1991 int winner
= more_constrained (get_template_for_ordering (champ
),
1992 get_template_for_ordering (c
));
1994 champ
= c
; // The candidate is more constrained
1995 else if (winner
== 0)
1996 return NULL_TREE
; // Neither is more constrained
1999 // Verify that the champ is better than previous candidates.
2000 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2001 if (!more_constrained (get_template_for_ordering (champ
),
2002 get_template_for_ordering (c
)))
2010 /* Returns the template (one of the functions given by TEMPLATE_ID)
2011 which can be specialized to match the indicated DECL with the
2012 explicit template args given in TEMPLATE_ID. The DECL may be
2013 NULL_TREE if none is available. In that case, the functions in
2014 TEMPLATE_ID are non-members.
2016 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2017 specialization of a member template.
2019 The TEMPLATE_COUNT is the number of references to qualifying
2020 template classes that appeared in the name of the function. See
2021 check_explicit_specialization for a more accurate description.
2023 TSK indicates what kind of template declaration (if any) is being
2024 declared. TSK_TEMPLATE indicates that the declaration given by
2025 DECL, though a FUNCTION_DECL, has template parameters, and is
2026 therefore a template function.
2028 The template args (those explicitly specified and those deduced)
2029 are output in a newly created vector *TARGS_OUT.
2031 If it is impossible to determine the result, an error message is
2032 issued. The error_mark_node is returned to indicate failure. */
2035 determine_specialization (tree template_id
,
2038 int need_member_template
,
2044 tree explicit_targs
;
2045 tree candidates
= NULL_TREE
;
2047 /* A TREE_LIST of templates of which DECL may be a specialization.
2048 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2049 corresponding TREE_PURPOSE is the set of template arguments that,
2050 when used to instantiate the template, would produce a function
2051 with the signature of DECL. */
2052 tree templates
= NULL_TREE
;
2054 cp_binding_level
*b
;
2056 *targs_out
= NULL_TREE
;
2058 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2059 return error_mark_node
;
2061 /* We shouldn't be specializing a member template of an
2062 unspecialized class template; we already gave an error in
2063 check_specialization_scope, now avoid crashing. */
2064 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2065 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2067 gcc_assert (errorcount
);
2068 return error_mark_node
;
2071 fns
= TREE_OPERAND (template_id
, 0);
2072 explicit_targs
= TREE_OPERAND (template_id
, 1);
2074 if (fns
== error_mark_node
)
2075 return error_mark_node
;
2077 /* Check for baselinks. */
2078 if (BASELINK_P (fns
))
2079 fns
= BASELINK_FUNCTIONS (fns
);
2081 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2083 error ("%qD is not a function template", fns
);
2084 return error_mark_node
;
2086 else if (VAR_P (decl
) && !variable_template_p (fns
))
2088 error ("%qD is not a variable template", fns
);
2089 return error_mark_node
;
2092 /* Count the number of template headers specified for this
2095 for (b
= current_binding_level
;
2096 b
->kind
== sk_template_parms
;
2100 tree orig_fns
= fns
;
2102 if (variable_template_p (fns
))
2104 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2105 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2106 tf_warning_or_error
,
2107 /*req_all*/true, /*use_defarg*/true);
2108 if (targs
!= error_mark_node
)
2109 templates
= tree_cons (targs
, fns
, templates
);
2111 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2115 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2117 tree decl_arg_types
;
2121 /* In case of explicit specialization, we need to check if
2122 the number of template headers appearing in the specialization
2123 is correct. This is usually done in check_explicit_specialization,
2124 but the check done there cannot be exhaustive when specializing
2125 member functions. Consider the following code:
2127 template <> void A<int>::f(int);
2128 template <> template <> void A<int>::f(int);
2130 Assuming that A<int> is not itself an explicit specialization
2131 already, the first line specializes "f" which is a non-template
2132 member function, whilst the second line specializes "f" which
2133 is a template member function. So both lines are syntactically
2134 correct, and check_explicit_specialization does not reject
2137 Here, we can do better, as we are matching the specialization
2138 against the declarations. We count the number of template
2139 headers, and we check if they match TEMPLATE_COUNT + 1
2140 (TEMPLATE_COUNT is the number of qualifying template classes,
2141 plus there must be another header for the member template
2144 Notice that if header_count is zero, this is not a
2145 specialization but rather a template instantiation, so there
2146 is no check we can perform here. */
2147 if (header_count
&& header_count
!= template_count
+ 1)
2150 /* Check that the number of template arguments at the
2151 innermost level for DECL is the same as for FN. */
2152 if (current_binding_level
->kind
== sk_template_parms
2153 && !current_binding_level
->explicit_spec_p
2154 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2155 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2156 (current_template_parms
))))
2159 /* DECL might be a specialization of FN. */
2160 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2161 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2163 /* For a non-static member function, we need to make sure
2164 that the const qualification is the same. Since
2165 get_bindings does not try to merge the "this" parameter,
2166 we must do the comparison explicitly. */
2167 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2168 && !same_type_p (TREE_VALUE (fn_arg_types
),
2169 TREE_VALUE (decl_arg_types
)))
2172 /* Skip the "this" parameter and, for constructors of
2173 classes with virtual bases, the VTT parameter. A
2174 full specialization of a constructor will have a VTT
2175 parameter, but a template never will. */
2177 = skip_artificial_parms_for (decl
, decl_arg_types
);
2179 = skip_artificial_parms_for (fn
, fn_arg_types
);
2181 /* Function templates cannot be specializations; there are
2182 no partial specializations of functions. Therefore, if
2183 the type of DECL does not match FN, there is no
2186 Note that it should never be the case that we have both
2187 candidates added here, and for regular member functions
2189 if (tsk
== tsk_template
)
2191 if (compparms (fn_arg_types
, decl_arg_types
))
2192 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2196 /* See whether this function might be a specialization of this
2197 template. Suppress access control because we might be trying
2198 to make this specialization a friend, and we have already done
2199 access control for the declaration of the specialization. */
2200 push_deferring_access_checks (dk_no_check
);
2201 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2202 pop_deferring_access_checks ();
2205 /* We cannot deduce template arguments that when used to
2206 specialize TMPL will produce DECL. */
2209 if (uses_template_parms (targs
))
2210 /* We deduced something involving 'auto', which isn't a valid
2211 template argument. */
2214 /* Remove, from the set of candidates, all those functions
2215 whose constraints are not satisfied. */
2216 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2219 // Then, try to form the new function type.
2220 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2221 if (insttype
== error_mark_node
)
2224 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2225 if (!compparms (fn_arg_types
, decl_arg_types
))
2228 /* Save this template, and the arguments deduced. */
2229 templates
= tree_cons (targs
, fn
, templates
);
2231 else if (need_member_template
)
2232 /* FN is an ordinary member function, and we need a
2233 specialization of a member template. */
2235 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2236 /* We can get IDENTIFIER_NODEs here in certain erroneous
2239 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2240 /* This is just an ordinary non-member function. Nothing can
2241 be a specialization of that. */
2243 else if (DECL_ARTIFICIAL (fn
))
2244 /* Cannot specialize functions that are created implicitly. */
2248 tree decl_arg_types
;
2250 /* This is an ordinary member function. However, since
2251 we're here, we can assume its enclosing class is a
2252 template class. For example,
2254 template <typename T> struct S { void f(); };
2255 template <> void S<int>::f() {}
2257 Here, S<int>::f is a non-template, but S<int> is a
2258 template class. If FN has the same type as DECL, we
2259 might be in business. */
2261 if (!DECL_TEMPLATE_INFO (fn
))
2262 /* Its enclosing class is an explicit specialization
2263 of a template class. This is not a candidate. */
2266 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2267 TREE_TYPE (TREE_TYPE (fn
))))
2268 /* The return types differ. */
2271 /* Adjust the type of DECL in case FN is a static member. */
2272 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2273 if (DECL_STATIC_FUNCTION_P (fn
)
2274 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2275 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2277 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2281 // If the deduced arguments do not satisfy the constraints,
2282 // this is not a candidate.
2283 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2286 // Add the candidate.
2287 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2291 if (templates
&& TREE_CHAIN (templates
))
2297 It is possible for a specialization with a given function
2298 signature to be instantiated from more than one function
2299 template. In such cases, explicit specification of the
2300 template arguments must be used to uniquely identify the
2301 function template specialization being specialized.
2303 Note that here, there's no suggestion that we're supposed to
2304 determine which of the candidate templates is most
2305 specialized. However, we, also have:
2309 Partial ordering of overloaded function template
2310 declarations is used in the following contexts to select
2311 the function template to which a function template
2312 specialization refers:
2314 -- when an explicit specialization refers to a function
2317 So, we do use the partial ordering rules, at least for now.
2318 This extension can only serve to make invalid programs valid,
2319 so it's safe. And, there is strong anecdotal evidence that
2320 the committee intended the partial ordering rules to apply;
2321 the EDG front end has that behavior, and John Spicer claims
2322 that the committee simply forgot to delete the wording in
2323 [temp.expl.spec]. */
2324 tree tmpl
= most_specialized_instantiation (templates
);
2325 if (tmpl
!= error_mark_node
)
2328 TREE_CHAIN (templates
) = NULL_TREE
;
2332 // Concepts allows multiple declarations of member functions
2333 // with the same signature. Like above, we need to rely on
2334 // on the partial ordering of those candidates to determine which
2336 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2338 if (tree cand
= most_constrained_function (candidates
))
2341 TREE_CHAIN (cand
) = NULL_TREE
;
2345 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2347 error ("template-id %qD for %q+D does not match any template "
2348 "declaration", template_id
, decl
);
2349 if (header_count
&& header_count
!= template_count
+ 1)
2350 inform (input_location
, "saw %d %<template<>%>, need %d for "
2351 "specializing a member function template",
2352 header_count
, template_count
+ 1);
2354 print_candidates (orig_fns
);
2355 return error_mark_node
;
2357 else if ((templates
&& TREE_CHAIN (templates
))
2358 || (candidates
&& TREE_CHAIN (candidates
))
2359 || (templates
&& candidates
))
2361 error ("ambiguous template specialization %qD for %q+D",
2363 candidates
= chainon (candidates
, templates
);
2364 print_candidates (candidates
);
2365 return error_mark_node
;
2368 /* We have one, and exactly one, match. */
2371 tree fn
= TREE_VALUE (candidates
);
2372 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2374 // Propagate the candidate's constraints to the declaration.
2375 set_constraints (decl
, get_constraints (fn
));
2377 /* DECL is a re-declaration or partial instantiation of a template
2379 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2381 /* It was a specialization of an ordinary member function in a
2383 return DECL_TI_TEMPLATE (fn
);
2386 /* It was a specialization of a template. */
2387 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2388 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2390 *targs_out
= copy_node (targs
);
2391 SET_TMPL_ARGS_LEVEL (*targs_out
,
2392 TMPL_ARGS_DEPTH (*targs_out
),
2393 TREE_PURPOSE (templates
));
2396 *targs_out
= TREE_PURPOSE (templates
);
2397 return TREE_VALUE (templates
);
2400 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2401 but with the default argument values filled in from those in the
2405 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2408 tree new_spec_types
;
2413 if (spec_types
== void_list_node
)
2414 return void_list_node
;
2416 /* Substitute into the rest of the list. */
2418 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2419 TREE_CHAIN (tmpl_types
));
2421 /* Add the default argument for this parameter. */
2422 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2423 TREE_VALUE (spec_types
),
2427 /* DECL is an explicit specialization. Replicate default arguments
2428 from the template it specializes. (That way, code like:
2430 template <class T> void f(T = 3);
2431 template <> void f(double);
2434 works, as required.) An alternative approach would be to look up
2435 the correct default arguments at the call-site, but this approach
2436 is consistent with how implicit instantiations are handled. */
2439 copy_default_args_to_explicit_spec (tree decl
)
2444 tree new_spec_types
;
2448 tree object_type
= NULL_TREE
;
2449 tree in_charge
= NULL_TREE
;
2450 tree vtt
= NULL_TREE
;
2452 /* See if there's anything we need to do. */
2453 tmpl
= DECL_TI_TEMPLATE (decl
);
2454 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2455 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2456 if (TREE_PURPOSE (t
))
2461 old_type
= TREE_TYPE (decl
);
2462 spec_types
= TYPE_ARG_TYPES (old_type
);
2464 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2466 /* Remove the this pointer, but remember the object's type for
2468 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2469 spec_types
= TREE_CHAIN (spec_types
);
2470 tmpl_types
= TREE_CHAIN (tmpl_types
);
2472 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2474 /* DECL may contain more parameters than TMPL due to the extra
2475 in-charge parameter in constructors and destructors. */
2476 in_charge
= spec_types
;
2477 spec_types
= TREE_CHAIN (spec_types
);
2479 if (DECL_HAS_VTT_PARM_P (decl
))
2482 spec_types
= TREE_CHAIN (spec_types
);
2486 /* Compute the merged default arguments. */
2488 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2490 /* Compute the new FUNCTION_TYPE. */
2494 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2499 /* Put the in-charge parameter back. */
2500 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2501 TREE_VALUE (in_charge
),
2504 new_type
= build_method_type_directly (object_type
,
2505 TREE_TYPE (old_type
),
2509 new_type
= build_function_type (TREE_TYPE (old_type
),
2511 new_type
= cp_build_type_attribute_variant (new_type
,
2512 TYPE_ATTRIBUTES (old_type
));
2513 new_type
= build_exception_variant (new_type
,
2514 TYPE_RAISES_EXCEPTIONS (old_type
));
2516 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2517 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2519 TREE_TYPE (decl
) = new_type
;
2522 /* Return the number of template headers we expect to see for a definition
2523 or specialization of CTYPE or one of its non-template members. */
2526 num_template_headers_for_class (tree ctype
)
2528 int num_templates
= 0;
2530 while (ctype
&& CLASS_TYPE_P (ctype
))
2532 /* You're supposed to have one `template <...>' for every
2533 template class, but you don't need one for a full
2534 specialization. For example:
2536 template <class T> struct S{};
2537 template <> struct S<int> { void f(); };
2538 void S<int>::f () {}
2540 is correct; there shouldn't be a `template <>' for the
2541 definition of `S<int>::f'. */
2542 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2543 /* If CTYPE does not have template information of any
2544 kind, then it is not a template, nor is it nested
2545 within a template. */
2547 if (explicit_class_specialization_p (ctype
))
2549 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2552 ctype
= TYPE_CONTEXT (ctype
);
2555 return num_templates
;
2558 /* Do a simple sanity check on the template headers that precede the
2559 variable declaration DECL. */
2562 check_template_variable (tree decl
)
2564 tree ctx
= CP_DECL_CONTEXT (decl
);
2565 int wanted
= num_template_headers_for_class (ctx
);
2566 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2567 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2569 if (cxx_dialect
< cxx14
)
2570 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2571 "variable templates only available with "
2572 "-std=c++14 or -std=gnu++14");
2574 // Namespace-scope variable templates should have a template header.
2577 if (template_header_count
> wanted
)
2579 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2580 "too many template headers for %qD "
2583 if (warned
&& CLASS_TYPE_P (ctx
)
2584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2585 inform (DECL_SOURCE_LOCATION (decl
),
2586 "members of an explicitly specialized class are defined "
2587 "without a template header");
2591 /* An explicit specialization whose declarator-id or class-head-name is not
2592 qualified shall be declared in the nearest enclosing namespace of the
2593 template, or, if the namespace is inline (7.3.1), any namespace from its
2594 enclosing namespace set.
2596 If the name declared in the explicit instantiation is an unqualified name,
2597 the explicit instantiation shall appear in the namespace where its template
2598 is declared or, if that namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set. */
2602 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2604 tree tmpl
= most_general_template (t
);
2605 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2606 && !is_nested_namespace (current_namespace
,
2607 CP_DECL_CONTEXT (tmpl
), true))
2609 if (processing_specialization
)
2610 permerror (loc
, "explicit specialization of %qD outside its "
2611 "namespace must use a nested-name-specifier", tmpl
);
2612 else if (processing_explicit_instantiation
2613 && cxx_dialect
>= cxx11
)
2614 /* This was allowed in C++98, so only pedwarn. */
2615 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2616 "outside its namespace must use a nested-name-"
2621 /* Warn for a template specialization SPEC that is missing some of a set
2622 of function or type attributes that the template TEMPL is declared with.
2623 ATTRLIST is a list of additional attributes that SPEC should be taken
2624 to ultimately be declared with. */
2627 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2629 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2630 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2632 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2635 /* Avoid warning if either declaration or its type is deprecated. */
2636 if (TREE_DEPRECATED (tmpl
)
2637 || TREE_DEPRECATED (spec
))
2640 tree tmpl_type
= TREE_TYPE (tmpl
);
2641 tree spec_type
= TREE_TYPE (spec
);
2643 if (TREE_DEPRECATED (tmpl_type
)
2644 || TREE_DEPRECATED (spec_type
)
2645 || TREE_DEPRECATED (TREE_TYPE (tmpl_type
))
2646 || TREE_DEPRECATED (TREE_TYPE (spec_type
)))
2649 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpl_type
) };
2650 tree spec_attrs
[] = { DECL_ATTRIBUTES (spec
), TYPE_ATTRIBUTES (spec_type
) };
2653 spec_attrs
[0] = attrlist
;
2654 else if (!spec_attrs
[1])
2655 spec_attrs
[1] = attrlist
;
2657 /* Avoid warning if the primary has no attributes. */
2658 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2661 /* Avoid warning if either declaration contains an attribute on
2662 the white list below. */
2663 const char* const whitelist
[] = {
2667 for (unsigned i
= 0; i
!= 2; ++i
)
2668 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
2669 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2670 || lookup_attribute (whitelist
[j
], spec_attrs
[i
]))
2673 /* Avoid warning if the difference between the primary and
2674 the specialization is not in one of the attributes below. */
2675 const char* const blacklist
[] = {
2676 "alloc_align", "alloc_size", "assume_aligned", "format",
2677 "format_arg", "malloc", "nonnull"
2680 /* Put together a list of the black listed attributes that the primary
2681 template is declared with that the specialization is not, in case
2682 it's not apparent from the most recent declaration of the primary. */
2683 unsigned nattrs
= 0;
2686 for (unsigned i
= 0; i
!= sizeof blacklist
/ sizeof *blacklist
; ++i
)
2688 for (unsigned j
= 0; j
!= 2; ++j
)
2690 if (!lookup_attribute (blacklist
[i
], tmpl_attrs
[j
]))
2693 for (unsigned k
= 0; k
!= 1 + !!spec_attrs
[1]; ++k
)
2695 if (lookup_attribute (blacklist
[i
], spec_attrs
[k
]))
2701 str
+= blacklist
[i
];
2711 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2712 "explicit specialization %q#D may be missing attributes",
2716 str
= G_("missing primary template attributes ") + str
;
2718 str
= G_("missing primary template attribute ") + str
;
2720 inform (DECL_SOURCE_LOCATION (tmpl
), str
.c_str ());
2725 /* Check to see if the function just declared, as indicated in
2726 DECLARATOR, and in DECL, is a specialization of a function
2727 template. We may also discover that the declaration is an explicit
2728 instantiation at this point.
2730 Returns DECL, or an equivalent declaration that should be used
2731 instead if all goes well. Issues an error message if something is
2732 amiss. Returns error_mark_node if the error is not easily
2735 FLAGS is a bitmask consisting of the following flags:
2737 2: The function has a definition.
2738 4: The function is a friend.
2740 The TEMPLATE_COUNT is the number of references to qualifying
2741 template classes that appeared in the name of the function. For
2744 template <class T> struct S { void f(); };
2747 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2748 classes are not counted in the TEMPLATE_COUNT, so that in
2750 template <class T> struct S {};
2751 template <> struct S<int> { void f(); }
2752 template <> void S<int>::f();
2754 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2755 invalid; there should be no template <>.)
2757 If the function is a specialization, it is marked as such via
2758 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2759 is set up correctly, and it is added to the list of specializations
2760 for that template. */
2763 check_explicit_specialization (tree declarator
,
2769 int have_def
= flags
& 2;
2770 int is_friend
= flags
& 4;
2771 bool is_concept
= flags
& 8;
2772 int specialization
= 0;
2773 int explicit_instantiation
= 0;
2774 int member_specialization
= 0;
2775 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2776 tree dname
= DECL_NAME (decl
);
2781 if (!processing_specialization
)
2784 tsk
= tsk_excessive_parms
;
2787 tsk
= current_tmpl_spec_kind (template_count
);
2792 if (processing_specialization
&& !VAR_P (decl
))
2795 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2797 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2800 /* This could be something like:
2802 template <class T> void f(T);
2803 class S { friend void f<>(int); } */
2807 /* This case handles bogus declarations like template <>
2808 template <class T> void f<int>(); */
2810 error ("template-id %qD in declaration of primary template",
2817 case tsk_invalid_member_spec
:
2818 /* The error has already been reported in
2819 check_specialization_scope. */
2820 return error_mark_node
;
2822 case tsk_invalid_expl_inst
:
2823 error ("template parameter list used in explicit instantiation");
2829 error ("definition provided for explicit instantiation");
2831 explicit_instantiation
= 1;
2834 case tsk_excessive_parms
:
2835 case tsk_insufficient_parms
:
2836 if (tsk
== tsk_excessive_parms
)
2837 error ("too many template parameter lists in declaration of %qD",
2839 else if (template_header_count
)
2840 error("too few template parameter lists in declaration of %qD", decl
);
2842 error("explicit specialization of %qD must be introduced by "
2843 "%<template <>%>", decl
);
2848 error ("explicit specialization declared %<concept%>");
2850 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2851 /* In cases like template<> constexpr bool v = true;
2852 We'll give an error in check_template_variable. */
2855 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2857 member_specialization
= 1;
2863 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2865 /* This case handles bogus declarations like template <>
2866 template <class T> void f<int>(); */
2868 if (!uses_template_parms (declarator
))
2869 error ("template-id %qD in declaration of primary template",
2871 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2873 /* Partial specialization of variable template. */
2874 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2878 else if (cxx_dialect
< cxx14
)
2879 error ("non-type partial specialization %qD "
2880 "is not allowed", declarator
);
2882 error ("non-class, non-variable partial specialization %qD "
2883 "is not allowed", declarator
);
2888 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2889 /* This is a specialization of a member template, without
2890 specialization the containing class. Something like:
2892 template <class T> struct S {
2893 template <class U> void f (U);
2895 template <> template <class U> void S<int>::f(U) {}
2897 That's a specialization -- but of the entire template. */
2905 if ((specialization
|| member_specialization
)
2906 /* This doesn't apply to variable templates. */
2907 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2908 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2910 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2911 for (; t
; t
= TREE_CHAIN (t
))
2912 if (TREE_PURPOSE (t
))
2914 permerror (input_location
,
2915 "default argument specified in explicit specialization");
2920 if (specialization
|| member_specialization
|| explicit_instantiation
)
2922 tree tmpl
= NULL_TREE
;
2923 tree targs
= NULL_TREE
;
2924 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2926 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2927 if (!was_template_id
)
2931 gcc_assert (identifier_p (declarator
));
2936 /* If there is no class context, the explicit instantiation
2937 must be at namespace scope. */
2938 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2940 /* Find the namespace binding, using the declaration
2942 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2944 if (fns
== error_mark_node
)
2945 /* If lookup fails, look for a friend declaration so we can
2946 give a better diagnostic. */
2947 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2948 /*type*/false, /*complain*/true,
2951 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2953 error ("%qD is not a template function", dname
);
2954 fns
= error_mark_node
;
2958 declarator
= lookup_template_function (fns
, NULL_TREE
);
2961 if (declarator
== error_mark_node
)
2962 return error_mark_node
;
2964 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2966 if (!explicit_instantiation
)
2967 /* A specialization in class scope. This is invalid,
2968 but the error will already have been flagged by
2969 check_specialization_scope. */
2970 return error_mark_node
;
2973 /* It's not valid to write an explicit instantiation in
2976 class C { template void f(); }
2978 This case is caught by the parser. However, on
2981 template class C { void f(); };
2983 (which is invalid) we can get here. The error will be
2990 else if (ctype
!= NULL_TREE
2991 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2993 // We'll match variable templates in start_decl.
2997 /* Find the list of functions in ctype that have the same
2998 name as the declared function. */
2999 tree name
= TREE_OPERAND (declarator
, 0);
3001 if (constructor_name_p (name
, ctype
))
3003 if (DECL_CONSTRUCTOR_P (decl
)
3004 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3005 : !CLASSTYPE_DESTRUCTOR (ctype
))
3007 /* From [temp.expl.spec]:
3009 If such an explicit specialization for the member
3010 of a class template names an implicitly-declared
3011 special member function (clause _special_), the
3012 program is ill-formed.
3014 Similar language is found in [temp.explicit]. */
3015 error ("specialization of implicitly-declared special member function");
3016 return error_mark_node
;
3019 name
= DECL_NAME (decl
);
3022 /* For a type-conversion operator, We might be looking for
3023 `operator int' which will be a specialization of
3024 `operator T'. Grab all the conversion operators, and
3025 then select from them. */
3026 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3027 ? conv_op_identifier
: name
);
3029 if (fns
== NULL_TREE
)
3031 error ("no member function %qD declared in %qT", name
, ctype
);
3032 return error_mark_node
;
3035 TREE_OPERAND (declarator
, 0) = fns
;
3038 /* Figure out what exactly is being specialized at this point.
3039 Note that for an explicit instantiation, even one for a
3040 member function, we cannot tell a priori whether the
3041 instantiation is for a member template, or just a member
3042 function of a template class. Even if a member template is
3043 being instantiated, the member template arguments may be
3044 elided if they can be deduced from the rest of the
3046 tmpl
= determine_specialization (declarator
, decl
,
3048 member_specialization
,
3052 if (!tmpl
|| tmpl
== error_mark_node
)
3053 /* We couldn't figure out what this declaration was
3055 return error_mark_node
;
3058 if (TREE_CODE (decl
) == FUNCTION_DECL
3059 && DECL_HIDDEN_FRIEND_P (tmpl
))
3061 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3062 "friend declaration %qD is not visible to "
3063 "explicit specialization", tmpl
))
3064 inform (DECL_SOURCE_LOCATION (tmpl
),
3065 "friend declaration here");
3067 else if (!ctype
&& !is_friend
3068 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3069 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3071 tree gen_tmpl
= most_general_template (tmpl
);
3073 if (explicit_instantiation
)
3075 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3076 is done by do_decl_instantiation later. */
3078 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3079 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3081 if (arg_depth
> parm_depth
)
3083 /* If TMPL is not the most general template (for
3084 example, if TMPL is a friend template that is
3085 injected into namespace scope), then there will
3086 be too many levels of TARGS. Remove some of them
3091 new_targs
= make_tree_vec (parm_depth
);
3092 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3093 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3094 = TREE_VEC_ELT (targs
, i
);
3098 return instantiate_template (tmpl
, targs
, tf_error
);
3101 /* If we thought that the DECL was a member function, but it
3102 turns out to be specializing a static member function,
3103 make DECL a static member function as well. */
3104 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3105 && DECL_STATIC_FUNCTION_P (tmpl
)
3106 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3107 revert_static_member_fn (decl
);
3109 /* If this is a specialization of a member template of a
3110 template class, we want to return the TEMPLATE_DECL, not
3111 the specialization of it. */
3112 if (tsk
== tsk_template
&& !was_template_id
)
3114 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3115 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3116 DECL_INITIAL (result
) = NULL_TREE
;
3120 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3121 DECL_SOURCE_LOCATION (result
)
3122 = DECL_SOURCE_LOCATION (decl
);
3123 /* We want to use the argument list specified in the
3124 definition, not in the original declaration. */
3125 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3126 for (parm
= DECL_ARGUMENTS (result
); parm
;
3127 parm
= DECL_CHAIN (parm
))
3128 DECL_CONTEXT (parm
) = result
;
3130 return register_specialization (tmpl
, gen_tmpl
, targs
,
3134 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3135 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3137 if (was_template_id
)
3138 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3140 /* Inherit default function arguments from the template
3141 DECL is specializing. */
3142 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3143 copy_default_args_to_explicit_spec (decl
);
3145 /* This specialization has the same protection as the
3146 template it specializes. */
3147 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3148 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3150 /* 7.1.1-1 [dcl.stc]
3152 A storage-class-specifier shall not be specified in an
3153 explicit specialization...
3155 The parser rejects these, so unless action is taken here,
3156 explicit function specializations will always appear with
3159 The action recommended by the C++ CWG in response to C++
3160 defect report 605 is to make the storage class and linkage
3161 of the explicit specialization match the templated function:
3163 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3165 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3167 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3168 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3170 /* A concept cannot be specialized. */
3171 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3173 error ("explicit specialization of function concept %qD",
3175 return error_mark_node
;
3178 /* This specialization has the same linkage and visibility as
3179 the function template it specializes. */
3180 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3181 if (! TREE_PUBLIC (decl
))
3183 DECL_INTERFACE_KNOWN (decl
) = 1;
3184 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3186 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3187 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3189 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3190 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3194 /* If DECL is a friend declaration, declared using an
3195 unqualified name, the namespace associated with DECL may
3196 have been set incorrectly. For example, in:
3198 template <typename T> void f(T);
3200 struct S { friend void f<int>(int); }
3203 we will have set the DECL_CONTEXT for the friend
3204 declaration to N, rather than to the global namespace. */
3205 if (DECL_NAMESPACE_SCOPE_P (decl
))
3206 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3208 if (is_friend
&& !have_def
)
3209 /* This is not really a declaration of a specialization.
3210 It's just the name of an instantiation. But, it's not
3211 a request for an instantiation, either. */
3212 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3213 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3214 /* A specialization is not necessarily COMDAT. */
3215 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3216 && DECL_DECLARED_INLINE_P (decl
));
3217 else if (VAR_P (decl
))
3218 DECL_COMDAT (decl
) = false;
3220 /* If this is a full specialization, register it so that we can find
3221 it again. Partial specializations will be registered in
3222 process_partial_specialization. */
3223 if (!processing_template_decl
)
3225 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3227 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3232 /* A 'structor should already have clones. */
3233 gcc_assert (decl
== error_mark_node
3234 || variable_template_p (tmpl
)
3235 || !(DECL_CONSTRUCTOR_P (decl
)
3236 || DECL_DESTRUCTOR_P (decl
))
3237 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3244 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3245 parameters. These are represented in the same format used for
3246 DECL_TEMPLATE_PARMS. */
3249 comp_template_parms (const_tree parms1
, const_tree parms2
)
3254 if (parms1
== parms2
)
3257 for (p1
= parms1
, p2
= parms2
;
3258 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3259 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3261 tree t1
= TREE_VALUE (p1
);
3262 tree t2
= TREE_VALUE (p2
);
3265 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3266 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3268 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3271 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3273 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3274 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3276 /* If either of the template parameters are invalid, assume
3277 they match for the sake of error recovery. */
3278 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3281 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3284 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3285 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3286 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3288 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3293 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3294 /* One set of parameters has more parameters lists than the
3301 /* Determine whether PARM is a parameter pack. */
3304 template_parameter_pack_p (const_tree parm
)
3306 /* Determine if we have a non-type template parameter pack. */
3307 if (TREE_CODE (parm
) == PARM_DECL
)
3308 return (DECL_TEMPLATE_PARM_P (parm
)
3309 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3310 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3311 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3313 /* If this is a list of template parameters, we could get a
3314 TYPE_DECL or a TEMPLATE_DECL. */
3315 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3316 parm
= TREE_TYPE (parm
);
3318 /* Otherwise it must be a type template parameter. */
3319 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3320 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3321 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3324 /* Determine if T is a function parameter pack. */
3327 function_parameter_pack_p (const_tree t
)
3329 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3330 return DECL_PACK_P (t
);
3334 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3335 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3338 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3340 if (! primary_func_tmpl_inst
3341 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3342 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3345 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3348 /* Return true iff the function parameter PARAM_DECL was expanded
3349 from the function parameter pack PACK. */
3352 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3354 if (DECL_ARTIFICIAL (param_decl
)
3355 || !function_parameter_pack_p (pack
))
3358 /* The parameter pack and its pack arguments have the same
3360 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3363 /* Determine whether ARGS describes a variadic template args list,
3364 i.e., one that is terminated by a template argument pack. */
3367 template_args_variadic_p (tree args
)
3372 if (args
== NULL_TREE
)
3375 args
= INNERMOST_TEMPLATE_ARGS (args
);
3376 nargs
= TREE_VEC_LENGTH (args
);
3381 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3383 return ARGUMENT_PACK_P (last_parm
);
3386 /* Generate a new name for the parameter pack name NAME (an
3387 IDENTIFIER_NODE) that incorporates its */
3390 make_ith_pack_parameter_name (tree name
, int i
)
3392 /* Munge the name to include the parameter index. */
3393 #define NUMBUF_LEN 128
3394 char numbuf
[NUMBUF_LEN
];
3398 if (name
== NULL_TREE
)
3400 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3401 newname_len
= IDENTIFIER_LENGTH (name
)
3402 + strlen (numbuf
) + 2;
3403 newname
= (char*)alloca (newname_len
);
3404 snprintf (newname
, newname_len
,
3405 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3406 return get_identifier (newname
);
3409 /* Return true if T is a primary function, class or alias template
3410 specialization, not including the template pattern. */
3413 primary_template_specialization_p (const_tree t
)
3418 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3419 return (DECL_LANG_SPECIFIC (t
)
3420 && DECL_USE_TEMPLATE (t
)
3421 && DECL_TEMPLATE_INFO (t
)
3422 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3423 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3424 return (CLASSTYPE_TEMPLATE_INFO (t
)
3425 && CLASSTYPE_USE_TEMPLATE (t
)
3426 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3427 else if (alias_template_specialization_p (t
))
3432 /* Return true if PARM is a template template parameter. */
3435 template_template_parameter_p (const_tree parm
)
3437 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3440 /* Return true iff PARM is a DECL representing a type template
3444 template_type_parameter_p (const_tree parm
)
3447 && (TREE_CODE (parm
) == TYPE_DECL
3448 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3449 && DECL_TEMPLATE_PARM_P (parm
));
3452 /* Return the template parameters of T if T is a
3453 primary template instantiation, NULL otherwise. */
3456 get_primary_template_innermost_parameters (const_tree t
)
3458 tree parms
= NULL
, template_info
= NULL
;
3460 if ((template_info
= get_template_info (t
))
3461 && primary_template_specialization_p (t
))
3462 parms
= INNERMOST_TEMPLATE_PARMS
3463 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3468 /* Return the template parameters of the LEVELth level from the full list
3469 of template parameters PARMS. */
3472 get_template_parms_at_level (tree parms
, int level
)
3476 || TREE_CODE (parms
) != TREE_LIST
3477 || level
> TMPL_PARMS_DEPTH (parms
))
3480 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3481 if (TMPL_PARMS_DEPTH (p
) == level
)
3487 /* Returns the template arguments of T if T is a template instantiation,
3491 get_template_innermost_arguments (const_tree t
)
3493 tree args
= NULL
, template_info
= NULL
;
3495 if ((template_info
= get_template_info (t
))
3496 && TI_ARGS (template_info
))
3497 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3502 /* Return the argument pack elements of T if T is a template argument pack,
3506 get_template_argument_pack_elems (const_tree t
)
3508 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3509 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3512 return ARGUMENT_PACK_ARGS (t
);
3515 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3516 ARGUMENT_PACK_SELECT represents. */
3519 argument_pack_select_arg (tree t
)
3521 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3522 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3524 /* If the selected argument is an expansion E, that most likely means we were
3525 called from gen_elem_of_pack_expansion_instantiation during the
3526 substituting of an argument pack (of which the Ith element is a pack
3527 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3528 In this case, the Ith element resulting from this substituting is going to
3529 be a pack expansion, which pattern is the pattern of E. Let's return the
3530 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3531 resulting pack expansion from it. */
3532 if (PACK_EXPANSION_P (arg
))
3534 /* Make sure we aren't throwing away arg info. */
3535 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3536 arg
= PACK_EXPANSION_PATTERN (arg
);
3543 /* True iff FN is a function representing a built-in variadic parameter
3547 builtin_pack_fn_p (tree fn
)
3550 || TREE_CODE (fn
) != FUNCTION_DECL
3551 || !DECL_IS_BUILTIN (fn
))
3554 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3560 /* True iff CALL is a call to a function representing a built-in variadic
3564 builtin_pack_call_p (tree call
)
3566 if (TREE_CODE (call
) != CALL_EXPR
)
3568 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3571 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3574 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3577 tree ohi
= CALL_EXPR_ARG (call
, 0);
3578 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3579 false/*fn*/, true/*int_cst*/);
3581 if (value_dependent_expression_p (hi
))
3585 call
= copy_node (call
);
3586 CALL_EXPR_ARG (call
, 0) = hi
;
3588 tree ex
= make_pack_expansion (call
, complain
);
3589 tree vec
= make_tree_vec (1);
3590 TREE_VEC_ELT (vec
, 0) = ex
;
3595 hi
= cxx_constant_value (hi
);
3596 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3598 /* Calculate the largest value of len that won't make the size of the vec
3599 overflow an int. The compiler will exceed resource limits long before
3600 this, but it seems a decent place to diagnose. */
3601 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3603 if (len
< 0 || len
> max
)
3605 if ((complain
& tf_error
)
3606 && hi
!= error_mark_node
)
3607 error ("argument to __integer_pack must be between 0 and %d", max
);
3608 return error_mark_node
;
3611 tree vec
= make_tree_vec (len
);
3613 for (int i
= 0; i
< len
; ++i
)
3614 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3620 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3624 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3627 if (!builtin_pack_call_p (call
))
3630 tree fn
= CALL_EXPR_FN (call
);
3632 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3633 return expand_integer_pack (call
, args
, complain
, in_decl
);
3638 /* Structure used to track the progress of find_parameter_packs_r. */
3639 struct find_parameter_pack_data
3641 /* TREE_LIST that will contain all of the parameter packs found by
3643 tree
* parameter_packs
;
3645 /* Set of AST nodes that have been visited by the traversal. */
3646 hash_set
<tree
> *visited
;
3648 /* True iff we're making a type pack expansion. */
3649 bool type_pack_expansion_p
;
3652 /* Identifies all of the argument packs that occur in a template
3653 argument and appends them to the TREE_LIST inside DATA, which is a
3654 find_parameter_pack_data structure. This is a subroutine of
3655 make_pack_expansion and uses_parameter_packs. */
3657 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3660 struct find_parameter_pack_data
* ppd
=
3661 (struct find_parameter_pack_data
*)data
;
3662 bool parameter_pack_p
= false;
3664 /* Handle type aliases/typedefs. */
3665 if (TYPE_ALIAS_P (t
))
3667 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3668 cp_walk_tree (&TI_ARGS (tinfo
),
3669 &find_parameter_packs_r
,
3675 /* Identify whether this is a parameter pack or not. */
3676 switch (TREE_CODE (t
))
3678 case TEMPLATE_PARM_INDEX
:
3679 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3680 parameter_pack_p
= true;
3683 case TEMPLATE_TYPE_PARM
:
3684 t
= TYPE_MAIN_VARIANT (t
);
3686 case TEMPLATE_TEMPLATE_PARM
:
3687 /* If the placeholder appears in the decl-specifier-seq of a function
3688 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3689 is a pack expansion, the invented template parameter is a template
3691 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3692 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3693 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3694 parameter_pack_p
= true;
3699 if (DECL_PACK_P (t
))
3701 /* We don't want to walk into the type of a PARM_DECL,
3702 because we don't want to see the type parameter pack. */
3704 parameter_pack_p
= true;
3709 if (DECL_PACK_P (t
))
3711 /* We don't want to walk into the type of a variadic capture proxy,
3712 because we don't want to see the type parameter pack. */
3714 parameter_pack_p
= true;
3716 else if (variable_template_specialization_p (t
))
3718 cp_walk_tree (&DECL_TI_ARGS (t
),
3719 find_parameter_packs_r
,
3726 if (builtin_pack_call_p (t
))
3727 parameter_pack_p
= true;
3731 parameter_pack_p
= true;
3734 /* Not a parameter pack. */
3738 if (parameter_pack_p
)
3740 /* Add this parameter pack to the list. */
3741 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3745 cp_walk_tree (&TYPE_CONTEXT (t
),
3746 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3748 /* This switch statement will return immediately if we don't find a
3750 switch (TREE_CODE (t
))
3752 case TEMPLATE_PARM_INDEX
:
3755 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3756 /* Check the template itself. */
3757 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3758 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3759 /* Check the template arguments. */
3760 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3765 case TEMPLATE_TYPE_PARM
:
3766 case TEMPLATE_TEMPLATE_PARM
:
3773 /* Ignore the declaration of a capture proxy for a parameter pack. */
3774 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3779 if (TYPE_PTRMEMFUNC_P (t
))
3785 if (TYPE_TEMPLATE_INFO (t
))
3786 cp_walk_tree (&TYPE_TI_ARGS (t
),
3787 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3793 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3798 cp_walk_tree (&TREE_TYPE (t
),
3799 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3803 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3808 case TYPE_PACK_EXPANSION
:
3809 case EXPR_PACK_EXPANSION
:
3814 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3819 case IDENTIFIER_NODE
:
3820 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3827 /* Look at explicit captures. */
3828 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3829 cap
; cap
= TREE_CHAIN (cap
))
3830 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3832 /* Since we defer implicit capture, look in the body as well. */
3833 tree fn
= lambda_function (t
);
3834 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3842 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3843 type_pack_expansion_p to false so that any placeholders
3844 within the expression don't get marked as parameter packs. */
3845 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3846 ppd
->type_pack_expansion_p
= false;
3847 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3849 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3861 /* Determines if the expression or type T uses any parameter packs. */
3863 uses_parameter_packs (tree t
)
3865 tree parameter_packs
= NULL_TREE
;
3866 struct find_parameter_pack_data ppd
;
3867 ppd
.parameter_packs
= ¶meter_packs
;
3868 ppd
.visited
= new hash_set
<tree
>;
3869 ppd
.type_pack_expansion_p
= false;
3870 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3872 return parameter_packs
!= NULL_TREE
;
3875 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3876 representation a base-class initializer into a parameter pack
3877 expansion. If all goes well, the resulting node will be an
3878 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3881 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3884 tree parameter_packs
= NULL_TREE
;
3885 bool for_types
= false;
3886 struct find_parameter_pack_data ppd
;
3888 if (!arg
|| arg
== error_mark_node
)
3891 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3893 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3894 class initializer. In this case, the TREE_PURPOSE will be a
3895 _TYPE node (representing the base class expansion we're
3896 initializing) and the TREE_VALUE will be a TREE_LIST
3897 containing the initialization arguments.
3899 The resulting expansion looks somewhat different from most
3900 expansions. Rather than returning just one _EXPANSION, we
3901 return a TREE_LIST whose TREE_PURPOSE is a
3902 TYPE_PACK_EXPANSION containing the bases that will be
3903 initialized. The TREE_VALUE will be identical to the
3904 original TREE_VALUE, which is a list of arguments that will
3905 be passed to each base. We do not introduce any new pack
3906 expansion nodes into the TREE_VALUE (although it is possible
3907 that some already exist), because the TREE_PURPOSE and
3908 TREE_VALUE all need to be expanded together with the same
3909 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3910 resulting TREE_PURPOSE will mention the parameter packs in
3911 both the bases and the arguments to the bases. */
3914 tree parameter_packs
= NULL_TREE
;
3916 /* Determine which parameter packs will be used by the base
3918 ppd
.visited
= new hash_set
<tree
>;
3919 ppd
.parameter_packs
= ¶meter_packs
;
3920 ppd
.type_pack_expansion_p
= true;
3921 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3922 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3925 if (parameter_packs
== NULL_TREE
)
3927 if (complain
& tf_error
)
3928 error ("base initializer expansion %qT contains no parameter packs",
3931 return error_mark_node
;
3934 if (TREE_VALUE (arg
) != void_type_node
)
3936 /* Collect the sets of parameter packs used in each of the
3937 initialization arguments. */
3938 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3940 /* Determine which parameter packs will be expanded in this
3942 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3949 /* Create the pack expansion type for the base type. */
3950 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3951 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3952 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3953 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3955 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3956 they will rarely be compared to anything. */
3957 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3959 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3962 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3965 /* Build the PACK_EXPANSION_* node. */
3967 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3968 : make_node (EXPR_PACK_EXPANSION
);
3969 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3970 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3972 /* Propagate type and const-expression information. */
3973 TREE_TYPE (result
) = TREE_TYPE (arg
);
3974 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3975 /* Mark this read now, since the expansion might be length 0. */
3976 mark_exp_read (arg
);
3979 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3980 they will rarely be compared to anything. */
3981 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3983 /* Determine which parameter packs will be expanded. */
3984 ppd
.parameter_packs
= ¶meter_packs
;
3985 ppd
.visited
= new hash_set
<tree
>;
3986 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3987 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3990 /* Make sure we found some parameter packs. */
3991 if (parameter_packs
== NULL_TREE
)
3993 if (complain
& tf_error
)
3996 error ("expansion pattern %qT contains no argument packs", arg
);
3998 error ("expansion pattern %qE contains no argument packs", arg
);
4000 return error_mark_node
;
4002 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4004 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4009 /* Checks T for any "bare" parameter packs, which have not yet been
4010 expanded, and issues an error if any are found. This operation can
4011 only be done on full expressions or types (e.g., an expression
4012 statement, "if" condition, etc.), because we could have expressions like:
4014 foo(f(g(h(args)))...)
4016 where "args" is a parameter pack. check_for_bare_parameter_packs
4017 should not be called for the subexpressions args, h(args),
4018 g(h(args)), or f(g(h(args))), because we would produce erroneous
4021 Returns TRUE and emits an error if there were bare parameter packs,
4022 returns FALSE otherwise. */
4024 check_for_bare_parameter_packs (tree t
)
4026 tree parameter_packs
= NULL_TREE
;
4027 struct find_parameter_pack_data ppd
;
4029 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4032 /* A lambda might use a parameter pack from the containing context. */
4033 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
4036 if (TREE_CODE (t
) == TYPE_DECL
)
4039 ppd
.parameter_packs
= ¶meter_packs
;
4040 ppd
.visited
= new hash_set
<tree
>;
4041 ppd
.type_pack_expansion_p
= false;
4042 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4045 if (parameter_packs
)
4047 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
4048 error_at (loc
, "parameter packs not expanded with %<...%>:");
4049 while (parameter_packs
)
4051 tree pack
= TREE_VALUE (parameter_packs
);
4052 tree name
= NULL_TREE
;
4054 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4055 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4056 name
= TYPE_NAME (pack
);
4057 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4058 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4059 else if (TREE_CODE (pack
) == CALL_EXPR
)
4060 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4062 name
= DECL_NAME (pack
);
4065 inform (loc
, " %qD", name
);
4067 inform (loc
, " <anonymous>");
4069 parameter_packs
= TREE_CHAIN (parameter_packs
);
4078 /* Expand any parameter packs that occur in the template arguments in
4081 expand_template_argument_pack (tree args
)
4083 if (args
== error_mark_node
)
4084 return error_mark_node
;
4086 tree result_args
= NULL_TREE
;
4087 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4088 int num_result_args
= -1;
4089 int non_default_args_count
= -1;
4091 /* First, determine if we need to expand anything, and the number of
4092 slots we'll need. */
4093 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4095 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4096 if (arg
== NULL_TREE
)
4098 if (ARGUMENT_PACK_P (arg
))
4100 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4101 if (num_result_args
< 0)
4102 num_result_args
= in_arg
+ num_packed
;
4104 num_result_args
+= num_packed
;
4108 if (num_result_args
>= 0)
4113 /* If no expansion is necessary, we're done. */
4114 if (num_result_args
< 0)
4117 /* Expand arguments. */
4118 result_args
= make_tree_vec (num_result_args
);
4119 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4120 non_default_args_count
=
4121 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4122 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4124 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4125 if (ARGUMENT_PACK_P (arg
))
4127 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4128 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4129 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4130 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4131 if (non_default_args_count
> 0)
4132 non_default_args_count
+= num_packed
- 1;
4136 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4140 if (non_default_args_count
>= 0)
4141 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4145 /* Checks if DECL shadows a template parameter.
4147 [temp.local]: A template-parameter shall not be redeclared within its
4148 scope (including nested scopes).
4150 Emits an error and returns TRUE if the DECL shadows a parameter,
4151 returns FALSE otherwise. */
4154 check_template_shadow (tree decl
)
4158 /* If we're not in a template, we can't possibly shadow a template
4160 if (!current_template_parms
)
4163 /* Figure out what we're shadowing. */
4164 decl
= OVL_FIRST (decl
);
4165 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4167 /* If there's no previous binding for this name, we're not shadowing
4168 anything, let alone a template parameter. */
4172 /* If we're not shadowing a template parameter, we're done. Note
4173 that OLDDECL might be an OVERLOAD (or perhaps even an
4174 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4176 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4179 /* We check for decl != olddecl to avoid bogus errors for using a
4180 name inside a class. We check TPFI to avoid duplicate errors for
4181 inline member templates. */
4183 || (DECL_TEMPLATE_PARM_P (decl
)
4184 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4187 /* Don't complain about the injected class name, as we've already
4188 complained about the class itself. */
4189 if (DECL_SELF_REFERENCE_P (decl
))
4192 if (DECL_TEMPLATE_PARM_P (decl
))
4193 error ("declaration of template parameter %q+D shadows "
4194 "template parameter", decl
);
4196 error ("declaration of %q+#D shadows template parameter", decl
);
4197 inform (DECL_SOURCE_LOCATION (olddecl
),
4198 "template parameter %qD declared here", olddecl
);
4202 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4203 ORIG_LEVEL, DECL, and TYPE. */
4206 build_template_parm_index (int index
,
4212 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4213 TEMPLATE_PARM_IDX (t
) = index
;
4214 TEMPLATE_PARM_LEVEL (t
) = level
;
4215 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4216 TEMPLATE_PARM_DECL (t
) = decl
;
4217 TREE_TYPE (t
) = type
;
4218 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4219 TREE_READONLY (t
) = TREE_READONLY (decl
);
4224 /* Find the canonical type parameter for the given template type
4225 parameter. Returns the canonical type parameter, which may be TYPE
4226 if no such parameter existed. */
4229 canonical_type_parameter (tree type
)
4232 int idx
= TEMPLATE_TYPE_IDX (type
);
4233 if (!canonical_template_parms
)
4234 vec_alloc (canonical_template_parms
, idx
+ 1);
4236 if (canonical_template_parms
->length () <= (unsigned) idx
)
4237 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4239 list
= (*canonical_template_parms
)[idx
];
4240 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4241 list
= TREE_CHAIN (list
);
4244 return TREE_VALUE (list
);
4247 (*canonical_template_parms
)[idx
]
4248 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4253 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4254 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4255 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4256 new one is created. */
4259 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4260 tsubst_flags_t complain
)
4262 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4263 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4264 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4265 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4267 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4270 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4271 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4272 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4273 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4274 DECL_ARTIFICIAL (decl
) = 1;
4275 SET_DECL_TEMPLATE_PARM_P (decl
);
4277 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4278 TEMPLATE_PARM_LEVEL (index
) - levels
,
4279 TEMPLATE_PARM_ORIG_LEVEL (index
),
4281 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4282 TEMPLATE_PARM_PARAMETER_PACK (t
)
4283 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4285 /* Template template parameters need this. */
4286 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4288 DECL_TEMPLATE_RESULT (decl
)
4289 = build_decl (DECL_SOURCE_LOCATION (decl
),
4290 TYPE_DECL
, DECL_NAME (decl
), type
);
4291 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4292 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4293 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4297 return TEMPLATE_PARM_DESCENDANTS (index
);
4300 /* Process information from new template parameter PARM and append it
4301 to the LIST being built. This new parameter is a non-type
4302 parameter iff IS_NON_TYPE is true. This new parameter is a
4303 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4307 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4308 bool is_non_type
, bool is_parameter_pack
)
4313 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4314 tree defval
= TREE_PURPOSE (parm
);
4315 tree constr
= TREE_TYPE (parm
);
4319 tree p
= tree_last (list
);
4321 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4324 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4325 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4327 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4335 parm
= TREE_VALUE (parm
);
4337 SET_DECL_TEMPLATE_PARM_P (parm
);
4339 if (TREE_TYPE (parm
) != error_mark_node
)
4343 The top-level cv-qualifiers on the template-parameter are
4344 ignored when determining its type. */
4345 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4346 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4347 TREE_TYPE (parm
) = error_mark_node
;
4348 else if (uses_parameter_packs (TREE_TYPE (parm
))
4349 && !is_parameter_pack
4350 /* If we're in a nested template parameter list, the template
4351 template parameter could be a parameter pack. */
4352 && processing_template_parmlist
== 1)
4354 /* This template parameter is not a parameter pack, but it
4355 should be. Complain about "bare" parameter packs. */
4356 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4358 /* Recover by calling this a parameter pack. */
4359 is_parameter_pack
= true;
4363 /* A template parameter is not modifiable. */
4364 TREE_CONSTANT (parm
) = 1;
4365 TREE_READONLY (parm
) = 1;
4366 decl
= build_decl (parm_loc
,
4367 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4368 TREE_CONSTANT (decl
) = 1;
4369 TREE_READONLY (decl
) = 1;
4370 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4371 = build_template_parm_index (idx
, processing_template_decl
,
4372 processing_template_decl
,
4373 decl
, TREE_TYPE (parm
));
4375 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4376 = is_parameter_pack
;
4381 parm
= TREE_VALUE (TREE_VALUE (parm
));
4383 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4385 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4386 /* This is for distinguishing between real templates and template
4387 template parameters */
4388 TREE_TYPE (parm
) = t
;
4389 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4394 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4395 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4396 decl
= build_decl (parm_loc
,
4397 TYPE_DECL
, parm
, t
);
4400 TYPE_NAME (t
) = decl
;
4401 TYPE_STUB_DECL (t
) = decl
;
4403 TEMPLATE_TYPE_PARM_INDEX (t
)
4404 = build_template_parm_index (idx
, processing_template_decl
,
4405 processing_template_decl
,
4406 decl
, TREE_TYPE (parm
));
4407 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4408 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4410 DECL_ARTIFICIAL (decl
) = 1;
4411 SET_DECL_TEMPLATE_PARM_P (decl
);
4413 /* Build requirements for the type/template parameter.
4414 This must be done after SET_DECL_TEMPLATE_PARM_P or
4415 process_template_parm could fail. */
4416 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4420 /* Build the parameter node linking the parameter declaration,
4421 its default argument (if any), and its constraints (if any). */
4422 parm
= build_tree_list (defval
, parm
);
4423 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4425 return chainon (list
, parm
);
4428 /* The end of a template parameter list has been reached. Process the
4429 tree list into a parameter vector, converting each parameter into a more
4430 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4434 end_template_parm_list (tree parms
)
4438 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4440 /* Pop the dummy parameter level and add the real one. */
4441 current_template_parms
= TREE_CHAIN (current_template_parms
);
4443 current_template_parms
4444 = tree_cons (size_int (processing_template_decl
),
4445 saved_parmlist
, current_template_parms
);
4447 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4449 next
= TREE_CHAIN (parm
);
4450 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4451 TREE_CHAIN (parm
) = NULL_TREE
;
4454 --processing_template_parmlist
;
4456 return saved_parmlist
;
4459 // Explicitly indicate the end of the template parameter list. We assume
4460 // that the current template parameters have been constructed and/or
4461 // managed explicitly, as when creating new template template parameters
4462 // from a shorthand constraint.
4464 end_template_parm_list ()
4466 --processing_template_parmlist
;
4469 /* end_template_decl is called after a template declaration is seen. */
4472 end_template_decl (void)
4474 reset_specialization ();
4476 if (! processing_template_decl
)
4479 /* This matches the pushlevel in begin_template_parm_list. */
4482 --processing_template_decl
;
4483 current_template_parms
= TREE_CHAIN (current_template_parms
);
4486 /* Takes a TREE_LIST representing a template parameter and convert it
4487 into an argument suitable to be passed to the type substitution
4488 functions. Note that If the TREE_LIST contains an error_mark
4489 node, the returned argument is error_mark_node. */
4492 template_parm_to_arg (tree t
)
4496 || TREE_CODE (t
) != TREE_LIST
)
4499 if (error_operand_p (TREE_VALUE (t
)))
4500 return error_mark_node
;
4504 if (TREE_CODE (t
) == TYPE_DECL
4505 || TREE_CODE (t
) == TEMPLATE_DECL
)
4509 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4511 /* Turn this argument into a TYPE_ARGUMENT_PACK
4512 with a single element, which expands T. */
4513 tree vec
= make_tree_vec (1);
4515 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4517 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4519 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4520 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4525 t
= DECL_INITIAL (t
);
4527 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4529 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4530 with a single element, which expands T. */
4531 tree vec
= make_tree_vec (1);
4533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4535 t
= convert_from_reference (t
);
4536 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4538 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4539 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4542 t
= convert_from_reference (t
);
4547 /* Given a single level of template parameters (a TREE_VEC), return it
4548 as a set of template arguments. */
4551 template_parms_level_to_args (tree parms
)
4553 tree a
= copy_node (parms
);
4554 TREE_TYPE (a
) = NULL_TREE
;
4555 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4556 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4559 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4564 /* Given a set of template parameters, return them as a set of template
4565 arguments. The template parameters are represented as a TREE_VEC, in
4566 the form documented in cp-tree.h for template arguments. */
4569 template_parms_to_args (tree parms
)
4572 tree args
= NULL_TREE
;
4573 int length
= TMPL_PARMS_DEPTH (parms
);
4576 /* If there is only one level of template parameters, we do not
4577 create a TREE_VEC of TREE_VECs. Instead, we return a single
4578 TREE_VEC containing the arguments. */
4580 args
= make_tree_vec (length
);
4582 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4584 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4587 TREE_VEC_ELT (args
, --l
) = a
;
4595 /* Within the declaration of a template, return the currently active
4596 template parameters as an argument TREE_VEC. */
4599 current_template_args (void)
4601 return template_parms_to_args (current_template_parms
);
4604 /* Update the declared TYPE by doing any lookups which were thought to be
4605 dependent, but are not now that we know the SCOPE of the declarator. */
4608 maybe_update_decl_type (tree orig_type
, tree scope
)
4610 tree type
= orig_type
;
4612 if (type
== NULL_TREE
)
4615 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4616 type
= TREE_TYPE (type
);
4618 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4619 && dependent_type_p (type
)
4620 /* Don't bother building up the args in this case. */
4621 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4623 /* tsubst in the args corresponding to the template parameters,
4624 including auto if present. Most things will be unchanged, but
4625 make_typename_type and tsubst_qualified_id will resolve
4626 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4627 tree args
= current_template_args ();
4628 tree auto_node
= type_uses_auto (type
);
4632 tree auto_vec
= make_tree_vec (1);
4633 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4634 args
= add_to_template_args (args
, auto_vec
);
4636 pushed
= push_scope (scope
);
4637 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4642 if (type
== error_mark_node
)
4645 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4647 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4650 type
= TYPE_NAME (type
);
4655 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4656 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4657 the new template is a member template. */
4660 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4662 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4663 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4664 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4665 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4666 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4671 struct template_parm_data
4673 /* The level of the template parameters we are currently
4677 /* The index of the specialization argument we are currently
4681 /* An array whose size is the number of template parameters. The
4682 elements are nonzero if the parameter has been used in any one
4683 of the arguments processed so far. */
4686 /* An array whose size is the number of template arguments. The
4687 elements are nonzero if the argument makes use of template
4688 parameters of this level. */
4689 int* arg_uses_template_parms
;
4692 /* Subroutine of push_template_decl used to see if each template
4693 parameter in a partial specialization is used in the explicit
4694 argument list. If T is of the LEVEL given in DATA (which is
4695 treated as a template_parm_data*), then DATA->PARMS is marked
4699 mark_template_parm (tree t
, void* data
)
4703 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4705 template_parm_level_and_index (t
, &level
, &idx
);
4707 if (level
== tpd
->level
)
4709 tpd
->parms
[idx
] = 1;
4710 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4713 /* In C++17 the type of a non-type argument is a deduced context. */
4714 if (cxx_dialect
>= cxx17
4715 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4716 for_each_template_parm (TREE_TYPE (t
),
4717 &mark_template_parm
,
4720 /*include_nondeduced_p=*/false);
4722 /* Return zero so that for_each_template_parm will continue the
4723 traversal of the tree; we want to mark *every* template parm. */
4727 /* Process the partial specialization DECL. */
4730 process_partial_specialization (tree decl
)
4732 tree type
= TREE_TYPE (decl
);
4733 tree tinfo
= get_template_info (decl
);
4734 tree maintmpl
= TI_TEMPLATE (tinfo
);
4735 tree specargs
= TI_ARGS (tinfo
);
4736 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4737 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4740 int nargs
= TREE_VEC_LENGTH (inner_args
);
4743 bool did_error_intro
= false;
4744 struct template_parm_data tpd
;
4745 struct template_parm_data tpd2
;
4747 gcc_assert (current_template_parms
);
4749 /* A concept cannot be specialized. */
4750 if (flag_concepts
&& variable_concept_p (maintmpl
))
4752 error ("specialization of variable concept %q#D", maintmpl
);
4753 return error_mark_node
;
4756 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4757 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4759 /* We check that each of the template parameters given in the
4760 partial specialization is used in the argument list to the
4761 specialization. For example:
4763 template <class T> struct S;
4764 template <class T> struct S<T*>;
4766 The second declaration is OK because `T*' uses the template
4767 parameter T, whereas
4769 template <class T> struct S<int>;
4771 is no good. Even trickier is:
4782 The S2<T> declaration is actually invalid; it is a
4783 full-specialization. Of course,
4786 struct S2<T (*)(U)>;
4788 or some such would have been OK. */
4789 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4790 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4791 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4793 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4794 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4795 for (i
= 0; i
< nargs
; ++i
)
4797 tpd
.current_arg
= i
;
4798 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4799 &mark_template_parm
,
4802 /*include_nondeduced_p=*/false);
4804 for (i
= 0; i
< ntparms
; ++i
)
4805 if (tpd
.parms
[i
] == 0)
4807 /* One of the template parms was not used in a deduced context in the
4809 if (!did_error_intro
)
4811 error ("template parameters not deducible in "
4812 "partial specialization:");
4813 did_error_intro
= true;
4816 inform (input_location
, " %qD",
4817 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4820 if (did_error_intro
)
4821 return error_mark_node
;
4823 /* [temp.class.spec]
4825 The argument list of the specialization shall not be identical to
4826 the implicit argument list of the primary template. */
4828 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4829 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4831 || !strictly_subsumes (current_template_constraints (),
4832 get_constraints (maintmpl
))))
4835 error ("partial specialization %q+D does not specialize "
4836 "any template arguments", decl
);
4838 error ("partial specialization %q+D does not specialize any "
4839 "template arguments and is not more constrained than", decl
);
4840 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4843 /* A partial specialization that replaces multiple parameters of the
4844 primary template with a pack expansion is less specialized for those
4846 if (nargs
< DECL_NTPARMS (maintmpl
))
4848 error ("partial specialization is not more specialized than the "
4849 "primary template because it replaces multiple parameters "
4850 "with a pack expansion");
4851 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4852 /* Avoid crash in process_partial_specialization. */
4856 /* If we aren't in a dependent class, we can actually try deduction. */
4857 else if (tpd
.level
== 1
4858 /* FIXME we should be able to handle a partial specialization of a
4859 partial instantiation, but currently we can't (c++/41727). */
4860 && TMPL_ARGS_DEPTH (specargs
) == 1
4861 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4863 if (permerror (input_location
, "partial specialization %qD is not "
4864 "more specialized than", decl
))
4865 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4869 /* [temp.class.spec]
4871 A partially specialized non-type argument expression shall not
4872 involve template parameters of the partial specialization except
4873 when the argument expression is a simple identifier.
4875 The type of a template parameter corresponding to a specialized
4876 non-type argument shall not be dependent on a parameter of the
4879 Also, we verify that pack expansions only occur at the
4880 end of the argument list. */
4881 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4883 for (i
= 0; i
< nargs
; ++i
)
4885 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4886 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4887 tree packed_args
= NULL_TREE
;
4890 if (ARGUMENT_PACK_P (arg
))
4892 /* Extract the arguments from the argument pack. We'll be
4893 iterating over these in the following loop. */
4894 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4895 len
= TREE_VEC_LENGTH (packed_args
);
4898 for (j
= 0; j
< len
; j
++)
4901 /* Get the Jth argument in the parameter pack. */
4902 arg
= TREE_VEC_ELT (packed_args
, j
);
4904 if (PACK_EXPANSION_P (arg
))
4906 /* Pack expansions must come at the end of the
4908 if ((packed_args
&& j
< len
- 1)
4909 || (!packed_args
&& i
< nargs
- 1))
4911 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4912 error ("parameter pack argument %qE must be at the "
4913 "end of the template argument list", arg
);
4915 error ("parameter pack argument %qT must be at the "
4916 "end of the template argument list", arg
);
4920 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4921 /* We only care about the pattern. */
4922 arg
= PACK_EXPANSION_PATTERN (arg
);
4924 if (/* These first two lines are the `non-type' bit. */
4926 && TREE_CODE (arg
) != TEMPLATE_DECL
4927 /* This next two lines are the `argument expression is not just a
4928 simple identifier' condition and also the `specialized
4929 non-type argument' bit. */
4930 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4931 && !(REFERENCE_REF_P (arg
)
4932 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4934 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4935 || (packed_args
&& uses_template_parms (arg
)))
4936 error ("template argument %qE involves template parameter(s)",
4940 /* Look at the corresponding template parameter,
4941 marking which template parameters its type depends
4943 tree type
= TREE_TYPE (parm
);
4947 /* We haven't yet initialized TPD2. Do so now. */
4948 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4949 /* The number of parameters here is the number in the
4950 main template, which, as checked in the assertion
4952 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4954 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4957 /* Mark the template parameters. But this time, we're
4958 looking for the template parameters of the main
4959 template, not in the specialization. */
4960 tpd2
.current_arg
= i
;
4961 tpd2
.arg_uses_template_parms
[i
] = 0;
4962 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4963 for_each_template_parm (type
,
4964 &mark_template_parm
,
4967 /*include_nondeduced_p=*/false);
4969 if (tpd2
.arg_uses_template_parms
[i
])
4971 /* The type depended on some template parameters.
4972 If they are fully specialized in the
4973 specialization, that's OK. */
4976 for (j
= 0; j
< nargs
; ++j
)
4977 if (tpd2
.parms
[j
] != 0
4978 && tpd
.arg_uses_template_parms
[j
])
4981 error_n (input_location
, count
,
4982 "type %qT of template argument %qE depends "
4983 "on a template parameter",
4984 "type %qT of template argument %qE depends "
4985 "on template parameters",
4994 /* We should only get here once. */
4995 if (TREE_CODE (decl
) == TYPE_DECL
)
4996 gcc_assert (!COMPLETE_TYPE_P (type
));
4998 // Build the template decl.
4999 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5000 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5001 TREE_TYPE (tmpl
) = type
;
5002 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5003 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5004 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5005 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5007 /* Give template template parms a DECL_CONTEXT of the template
5008 for which they are a parameter. */
5009 for (i
= 0; i
< ntparms
; ++i
)
5011 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5012 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5013 DECL_CONTEXT (parm
) = tmpl
;
5017 /* We didn't register this in check_explicit_specialization so we could
5018 wait until the constraints were set. */
5019 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5021 associate_classtype_constraints (type
);
5023 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5024 = tree_cons (specargs
, tmpl
,
5025 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5026 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5028 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5029 inst
= TREE_CHAIN (inst
))
5031 tree instance
= TREE_VALUE (inst
);
5032 if (TYPE_P (instance
)
5033 ? (COMPLETE_TYPE_P (instance
)
5034 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5035 : DECL_TEMPLATE_INSTANTIATION (instance
))
5037 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5038 tree inst_decl
= (DECL_P (instance
)
5039 ? instance
: TYPE_NAME (instance
));
5042 else if (spec
== error_mark_node
)
5043 permerror (input_location
,
5044 "declaration of %qD ambiguates earlier template "
5045 "instantiation for %qD", decl
, inst_decl
);
5046 else if (TREE_VALUE (spec
) == tmpl
)
5047 permerror (input_location
,
5048 "partial specialization of %qD after instantiation "
5049 "of %qD", decl
, inst_decl
);
5056 /* PARM is a template parameter of some form; return the corresponding
5057 TEMPLATE_PARM_INDEX. */
5060 get_template_parm_index (tree parm
)
5062 if (TREE_CODE (parm
) == PARM_DECL
5063 || TREE_CODE (parm
) == CONST_DECL
)
5064 parm
= DECL_INITIAL (parm
);
5065 else if (TREE_CODE (parm
) == TYPE_DECL
5066 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5067 parm
= TREE_TYPE (parm
);
5068 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5069 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5070 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5071 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5072 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5076 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5077 parameter packs used by the template parameter PARM. */
5080 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5082 /* A type parm can't refer to another parm. */
5083 if (TREE_CODE (parm
) == TYPE_DECL
)
5085 else if (TREE_CODE (parm
) == PARM_DECL
)
5087 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5092 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5094 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5095 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5096 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5099 /* PARM is a template parameter pack. Return any parameter packs used in
5100 its type or the type of any of its template parameters. If there are
5101 any such packs, it will be instantiated into a fixed template parameter
5102 list by partial instantiation rather than be fully deduced. */
5105 fixed_parameter_pack_p (tree parm
)
5107 /* This can only be true in a member template. */
5108 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5110 /* This can only be true for a parameter pack. */
5111 if (!template_parameter_pack_p (parm
))
5113 /* A type parm can't refer to another parm. */
5114 if (TREE_CODE (parm
) == TYPE_DECL
)
5117 tree parameter_packs
= NULL_TREE
;
5118 struct find_parameter_pack_data ppd
;
5119 ppd
.parameter_packs
= ¶meter_packs
;
5120 ppd
.visited
= new hash_set
<tree
>;
5121 ppd
.type_pack_expansion_p
= false;
5123 fixed_parameter_pack_p_1 (parm
, &ppd
);
5126 return parameter_packs
;
5129 /* Check that a template declaration's use of default arguments and
5130 parameter packs is not invalid. Here, PARMS are the template
5131 parameters. IS_PRIMARY is true if DECL is the thing declared by
5132 a primary template. IS_PARTIAL is true if DECL is a partial
5135 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5136 function template declaration or a friend class template
5137 declaration. In the function case, 1 indicates a declaration, 2
5138 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5139 emitted for extraneous default arguments.
5141 Returns TRUE if there were no errors found, FALSE otherwise. */
5144 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5145 bool is_partial
, int is_friend_decl
)
5148 int last_level_to_check
;
5150 bool no_errors
= true;
5154 A default template-argument shall not be specified in a
5155 function template declaration or a function template definition, nor
5156 in the template-parameter-list of the definition of a member of a
5159 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5160 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5161 /* You can't have a function template declaration in a local
5162 scope, nor you can you define a member of a class template in a
5166 if ((TREE_CODE (decl
) == TYPE_DECL
5168 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5169 || (TREE_CODE (decl
) == FUNCTION_DECL
5170 && LAMBDA_FUNCTION_P (decl
)))
5171 /* A lambda doesn't have an explicit declaration; don't complain
5172 about the parms of the enclosing class. */
5175 if (current_class_type
5176 && !TYPE_BEING_DEFINED (current_class_type
)
5177 && DECL_LANG_SPECIFIC (decl
)
5178 && DECL_DECLARES_FUNCTION_P (decl
)
5179 /* If this is either a friend defined in the scope of the class
5180 or a member function. */
5181 && (DECL_FUNCTION_MEMBER_P (decl
)
5182 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5183 : DECL_FRIEND_CONTEXT (decl
)
5184 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5186 /* And, if it was a member function, it really was defined in
5187 the scope of the class. */
5188 && (!DECL_FUNCTION_MEMBER_P (decl
)
5189 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5190 /* We already checked these parameters when the template was
5191 declared, so there's no need to do it again now. This function
5192 was defined in class scope, but we're processing its body now
5193 that the class is complete. */
5196 /* Core issue 226 (C++0x only): the following only applies to class
5199 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5203 If a template-parameter has a default template-argument, all
5204 subsequent template-parameters shall have a default
5205 template-argument supplied. */
5206 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5208 tree inner_parms
= TREE_VALUE (parm_level
);
5209 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5210 int seen_def_arg_p
= 0;
5213 for (i
= 0; i
< ntparms
; ++i
)
5215 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5217 if (parm
== error_mark_node
)
5220 if (TREE_PURPOSE (parm
))
5222 else if (seen_def_arg_p
5223 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5225 error ("no default argument for %qD", TREE_VALUE (parm
));
5226 /* For better subsequent error-recovery, we indicate that
5227 there should have been a default argument. */
5228 TREE_PURPOSE (parm
) = error_mark_node
;
5231 else if (!is_partial
5233 /* Don't complain about an enclosing partial
5235 && parm_level
== parms
5236 && TREE_CODE (decl
) == TYPE_DECL
5238 && template_parameter_pack_p (TREE_VALUE (parm
))
5239 /* A fixed parameter pack will be partially
5240 instantiated into a fixed length list. */
5241 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5243 /* A primary class template can only have one
5244 parameter pack, at the end of the template
5247 error ("parameter pack %q+D must be at the end of the"
5248 " template parameter list", TREE_VALUE (parm
));
5250 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5258 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5262 /* For an ordinary class template, default template arguments are
5263 allowed at the innermost level, e.g.:
5264 template <class T = int>
5266 but, in a partial specialization, they're not allowed even
5267 there, as we have in [temp.class.spec]:
5269 The template parameter list of a specialization shall not
5270 contain default template argument values.
5272 So, for a partial specialization, or for a function template
5273 (in C++98/C++03), we look at all of them. */
5276 /* But, for a primary class template that is not a partial
5277 specialization we look at all template parameters except the
5279 parms
= TREE_CHAIN (parms
);
5281 /* Figure out what error message to issue. */
5282 if (is_friend_decl
== 2)
5283 msg
= G_("default template arguments may not be used in function template "
5284 "friend re-declaration");
5285 else if (is_friend_decl
)
5286 msg
= G_("default template arguments may not be used in template "
5287 "friend declarations");
5288 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5289 msg
= G_("default template arguments may not be used in function templates "
5290 "without -std=c++11 or -std=gnu++11");
5291 else if (is_partial
)
5292 msg
= G_("default template arguments may not be used in "
5293 "partial specializations");
5294 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5295 msg
= G_("default argument for template parameter for class enclosing %qD");
5297 /* Per [temp.param]/9, "A default template-argument shall not be
5298 specified in the template-parameter-lists of the definition of
5299 a member of a class template that appears outside of the member's
5300 class.", thus if we aren't handling a member of a class template
5301 there is no need to examine the parameters. */
5304 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5305 /* If we're inside a class definition, there's no need to
5306 examine the parameters to the class itself. On the one
5307 hand, they will be checked when the class is defined, and,
5308 on the other, default arguments are valid in things like:
5309 template <class T = double>
5310 struct S { template <class U> void f(U); };
5311 Here the default argument for `S' has no bearing on the
5312 declaration of `f'. */
5313 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5315 /* Check everything. */
5316 last_level_to_check
= 0;
5318 for (parm_level
= parms
;
5319 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5320 parm_level
= TREE_CHAIN (parm_level
))
5322 tree inner_parms
= TREE_VALUE (parm_level
);
5326 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5327 for (i
= 0; i
< ntparms
; ++i
)
5329 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5332 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5337 if (is_friend_decl
== 2)
5344 /* Clear out the default argument so that we are not
5346 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5350 /* At this point, if we're still interested in issuing messages,
5351 they must apply to classes surrounding the object declared. */
5353 msg
= G_("default argument for template parameter for class "
5360 /* Worker for push_template_decl_real, called via
5361 for_each_template_parm. DATA is really an int, indicating the
5362 level of the parameters we are interested in. If T is a template
5363 parameter of that level, return nonzero. */
5366 template_parm_this_level_p (tree t
, void* data
)
5368 int this_level
= *(int *)data
;
5371 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5372 level
= TEMPLATE_PARM_LEVEL (t
);
5374 level
= TEMPLATE_TYPE_LEVEL (t
);
5375 return level
== this_level
;
5378 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5379 DATA is really an int, indicating the innermost outer level of parameters.
5380 If T is a template parameter of that level or further out, return
5384 template_parm_outer_level (tree t
, void *data
)
5386 int this_level
= *(int *)data
;
5389 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5390 level
= TEMPLATE_PARM_LEVEL (t
);
5392 level
= TEMPLATE_TYPE_LEVEL (t
);
5393 return level
<= this_level
;
5396 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5397 parameters given by current_template_args, or reuses a
5398 previously existing one, if appropriate. Returns the DECL, or an
5399 equivalent one, if it is replaced via a call to duplicate_decls.
5401 If IS_FRIEND is true, DECL is a friend declaration. */
5404 push_template_decl_real (tree decl
, bool is_friend
)
5412 int new_template_p
= 0;
5413 /* True if the template is a member template, in the sense of
5415 bool member_template_p
= false;
5417 if (decl
== error_mark_node
|| !current_template_parms
)
5418 return error_mark_node
;
5420 /* See if this is a partial specialization. */
5421 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5422 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5423 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5425 && DECL_LANG_SPECIFIC (decl
)
5426 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5427 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5429 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5433 /* For a friend, we want the context of the friend, not
5434 the type of which it is a friend. */
5435 ctx
= CP_DECL_CONTEXT (decl
);
5436 else if (CP_DECL_CONTEXT (decl
)
5437 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5438 /* In the case of a virtual function, we want the class in which
5440 ctx
= CP_DECL_CONTEXT (decl
);
5442 /* Otherwise, if we're currently defining some class, the DECL
5443 is assumed to be a member of the class. */
5444 ctx
= current_scope ();
5446 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5449 if (!DECL_CONTEXT (decl
))
5450 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5452 /* See if this is a primary template. */
5453 if (is_friend
&& ctx
5454 && uses_template_parms_level (ctx
, processing_template_decl
))
5455 /* A friend template that specifies a class context, i.e.
5456 template <typename T> friend void A<T>::f();
5459 else if (TREE_CODE (decl
) == TYPE_DECL
5460 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5463 is_primary
= template_parm_scope_p ();
5467 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5469 if (DECL_CLASS_SCOPE_P (decl
))
5470 member_template_p
= true;
5471 if (TREE_CODE (decl
) == TYPE_DECL
5472 && anon_aggrname_p (DECL_NAME (decl
)))
5474 error ("template class without a name");
5475 return error_mark_node
;
5477 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5479 if (member_template_p
)
5481 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5482 error ("member template %qD may not have virt-specifiers", decl
);
5484 if (DECL_DESTRUCTOR_P (decl
))
5488 A destructor shall not be a member template. */
5489 error ("destructor %qD declared as member template", decl
);
5490 return error_mark_node
;
5492 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5493 && (!prototype_p (TREE_TYPE (decl
))
5494 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5495 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5496 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5497 == void_list_node
)))
5499 /* [basic.stc.dynamic.allocation]
5501 An allocation function can be a function
5502 template. ... Template allocation functions shall
5503 have two or more parameters. */
5504 error ("invalid template declaration of %qD", decl
);
5505 return error_mark_node
;
5508 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5509 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5511 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5512 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5513 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5515 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5516 if (TREE_CODE (t
) == TYPE_DECL
)
5518 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5519 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5522 else if (TREE_CODE (decl
) == TYPE_DECL
5523 && TYPE_DECL_ALIAS_P (decl
))
5524 /* alias-declaration */
5525 gcc_assert (!DECL_ARTIFICIAL (decl
));
5526 else if (VAR_P (decl
))
5527 /* C++14 variable template. */;
5530 error ("template declaration of %q#D", decl
);
5531 return error_mark_node
;
5535 /* Check to see that the rules regarding the use of default
5536 arguments are not being violated. We check args for a friend
5537 functions when we know whether it's a definition, introducing
5538 declaration or re-declaration. */
5539 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5540 check_default_tmpl_args (decl
, current_template_parms
,
5541 is_primary
, is_partial
, is_friend
);
5543 /* Ensure that there are no parameter packs in the type of this
5544 declaration that have not been expanded. */
5545 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5547 /* Check each of the arguments individually to see if there are
5548 any bare parameter packs. */
5549 tree type
= TREE_TYPE (decl
);
5550 tree arg
= DECL_ARGUMENTS (decl
);
5551 tree argtype
= TYPE_ARG_TYPES (type
);
5553 while (arg
&& argtype
)
5555 if (!DECL_PACK_P (arg
)
5556 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5558 /* This is a PARM_DECL that contains unexpanded parameter
5559 packs. We have already complained about this in the
5560 check_for_bare_parameter_packs call, so just replace
5561 these types with ERROR_MARK_NODE. */
5562 TREE_TYPE (arg
) = error_mark_node
;
5563 TREE_VALUE (argtype
) = error_mark_node
;
5566 arg
= DECL_CHAIN (arg
);
5567 argtype
= TREE_CHAIN (argtype
);
5570 /* Check for bare parameter packs in the return type and the
5571 exception specifiers. */
5572 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5573 /* Errors were already issued, set return type to int
5574 as the frontend doesn't expect error_mark_node as
5576 TREE_TYPE (type
) = integer_type_node
;
5577 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5578 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5580 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5581 && TYPE_DECL_ALIAS_P (decl
))
5582 ? DECL_ORIGINAL_TYPE (decl
)
5583 : TREE_TYPE (decl
)))
5585 TREE_TYPE (decl
) = error_mark_node
;
5586 return error_mark_node
;
5590 return process_partial_specialization (decl
);
5592 args
= current_template_args ();
5595 || TREE_CODE (ctx
) == FUNCTION_DECL
5596 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5597 || (TREE_CODE (decl
) == TYPE_DECL
5598 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5599 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5601 if (DECL_LANG_SPECIFIC (decl
)
5602 && DECL_TEMPLATE_INFO (decl
)
5603 && DECL_TI_TEMPLATE (decl
))
5604 tmpl
= DECL_TI_TEMPLATE (decl
);
5605 /* If DECL is a TYPE_DECL for a class-template, then there won't
5606 be DECL_LANG_SPECIFIC. The information equivalent to
5607 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5608 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5609 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5610 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5612 /* Since a template declaration already existed for this
5613 class-type, we must be redeclaring it here. Make sure
5614 that the redeclaration is valid. */
5615 redeclare_class_template (TREE_TYPE (decl
),
5616 current_template_parms
,
5617 current_template_constraints ());
5618 /* We don't need to create a new TEMPLATE_DECL; just use the
5619 one we already had. */
5620 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5624 tmpl
= build_template_decl (decl
, current_template_parms
,
5628 if (DECL_LANG_SPECIFIC (decl
)
5629 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5631 /* A specialization of a member template of a template
5633 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5634 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5635 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5641 tree a
, t
, current
, parms
;
5643 tree tinfo
= get_template_info (decl
);
5647 error ("template definition of non-template %q#D", decl
);
5648 return error_mark_node
;
5651 tmpl
= TI_TEMPLATE (tinfo
);
5653 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5654 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5655 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5656 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5660 /* The declaration is a specialization of a member
5661 template, declared outside the class. Therefore, the
5662 innermost template arguments will be NULL, so we
5663 replace them with the arguments determined by the
5664 earlier call to check_explicit_specialization. */
5665 args
= DECL_TI_ARGS (decl
);
5668 = build_template_decl (decl
, current_template_parms
,
5670 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5671 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5672 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5673 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5674 DECL_TEMPLATE_INFO (new_tmpl
)
5675 = build_template_info (tmpl
, args
);
5677 register_specialization (new_tmpl
,
5678 most_general_template (tmpl
),
5684 /* Make sure the template headers we got make sense. */
5686 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5687 i
= TMPL_PARMS_DEPTH (parms
);
5688 if (TMPL_ARGS_DEPTH (args
) != i
)
5690 error ("expected %d levels of template parms for %q#D, got %d",
5691 i
, decl
, TMPL_ARGS_DEPTH (args
));
5692 DECL_INTERFACE_KNOWN (decl
) = 1;
5693 return error_mark_node
;
5696 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5698 a
= TMPL_ARGS_LEVEL (args
, i
);
5699 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5701 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5703 if (current
== decl
)
5704 error ("got %d template parameters for %q#D",
5705 TREE_VEC_LENGTH (a
), decl
);
5707 error ("got %d template parameters for %q#T",
5708 TREE_VEC_LENGTH (a
), current
);
5709 error (" but %d required", TREE_VEC_LENGTH (t
));
5710 /* Avoid crash in import_export_decl. */
5711 DECL_INTERFACE_KNOWN (decl
) = 1;
5712 return error_mark_node
;
5715 if (current
== decl
)
5717 else if (current
== NULL_TREE
)
5718 /* Can happen in erroneous input. */
5721 current
= get_containing_scope (current
);
5724 /* Check that the parms are used in the appropriate qualifying scopes
5725 in the declarator. */
5726 if (!comp_template_args
5728 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5730 error ("template arguments to %qD do not match original "
5731 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5732 if (!uses_template_parms (TI_ARGS (tinfo
)))
5733 inform (input_location
, "use %<template<>%> for"
5734 " an explicit specialization");
5735 /* Avoid crash in import_export_decl. */
5736 DECL_INTERFACE_KNOWN (decl
) = 1;
5737 return error_mark_node
;
5741 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5742 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5744 /* Push template declarations for global functions and types. Note
5745 that we do not try to push a global template friend declared in a
5746 template class; such a thing may well depend on the template
5747 parameters of the class. */
5748 if (new_template_p
&& !ctx
5749 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5751 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5752 if (tmpl
== error_mark_node
)
5753 return error_mark_node
;
5755 /* Hide template friend classes that haven't been declared yet. */
5756 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5758 DECL_ANTICIPATED (tmpl
) = 1;
5759 DECL_FRIEND_P (tmpl
) = 1;
5765 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5767 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5769 /* Give template template parms a DECL_CONTEXT of the template
5770 for which they are a parameter. */
5771 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5772 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5774 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5775 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5776 DECL_CONTEXT (parm
) = tmpl
;
5779 if (TREE_CODE (decl
) == TYPE_DECL
5780 && TYPE_DECL_ALIAS_P (decl
)
5781 && complex_alias_template_p (tmpl
))
5782 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5785 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5786 back to its most general template. If TMPL is a specialization,
5787 ARGS may only have the innermost set of arguments. Add the missing
5788 argument levels if necessary. */
5789 if (DECL_TEMPLATE_INFO (tmpl
))
5790 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5792 info
= build_template_info (tmpl
, args
);
5794 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5795 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5799 retrofit_lang_decl (decl
);
5800 if (DECL_LANG_SPECIFIC (decl
))
5801 DECL_TEMPLATE_INFO (decl
) = info
;
5804 if (flag_implicit_templates
5806 && TREE_PUBLIC (decl
)
5807 && VAR_OR_FUNCTION_DECL_P (decl
))
5808 /* Set DECL_COMDAT on template instantiations; if we force
5809 them to be emitted by explicit instantiation or -frepo,
5810 mark_needed will tell cgraph to do the right thing. */
5811 DECL_COMDAT (decl
) = true;
5813 return DECL_TEMPLATE_RESULT (tmpl
);
5817 push_template_decl (tree decl
)
5819 return push_template_decl_real (decl
, false);
5822 /* FN is an inheriting constructor that inherits from the constructor
5823 template INHERITED; turn FN into a constructor template with a matching
5827 add_inherited_template_parms (tree fn
, tree inherited
)
5830 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5831 inner_parms
= copy_node (inner_parms
);
5833 = tree_cons (size_int (processing_template_decl
+ 1),
5834 inner_parms
, current_template_parms
);
5835 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5836 tree args
= template_parms_to_args (parms
);
5837 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5838 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5839 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5840 DECL_ARTIFICIAL (tmpl
) = true;
5841 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5845 /* Called when a class template TYPE is redeclared with the indicated
5846 template PARMS, e.g.:
5848 template <class T> struct S;
5849 template <class T> struct S {}; */
5852 redeclare_class_template (tree type
, tree parms
, tree cons
)
5858 if (!TYPE_TEMPLATE_INFO (type
))
5860 error ("%qT is not a template type", type
);
5864 tmpl
= TYPE_TI_TEMPLATE (type
);
5865 if (!PRIMARY_TEMPLATE_P (tmpl
))
5866 /* The type is nested in some template class. Nothing to worry
5867 about here; there are no new template parameters for the nested
5873 error ("template specifiers not specified in declaration of %qD",
5878 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5879 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5881 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5883 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5884 "redeclared with %d template parameter",
5885 "redeclared with %d template parameters",
5886 TREE_VEC_LENGTH (parms
));
5887 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5888 "previous declaration %qD used %d template parameter",
5889 "previous declaration %qD used %d template parameters",
5890 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5894 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5901 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5902 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5905 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5906 if (error_operand_p (tmpl_parm
))
5909 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5910 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5911 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5913 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5915 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5916 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5917 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5918 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5919 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5920 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5921 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5922 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5923 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5925 error ("template parameter %q+#D", tmpl_parm
);
5926 error ("redeclared here as %q#D", parm
);
5930 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5932 /* We have in [temp.param]:
5934 A template-parameter may not be given default arguments
5935 by two different declarations in the same scope. */
5936 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5937 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5938 "original definition appeared here");
5942 if (parm_default
!= NULL_TREE
)
5943 /* Update the previous template parameters (which are the ones
5944 that will really count) with the new default value. */
5945 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5946 else if (tmpl_default
!= NULL_TREE
)
5947 /* Update the new parameters, too; they'll be used as the
5948 parameters for any members. */
5949 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5951 /* Give each template template parm in this redeclaration a
5952 DECL_CONTEXT of the template for which they are a parameter. */
5953 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5955 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5956 DECL_CONTEXT (parm
) = tmpl
;
5959 if (TREE_CODE (parm
) == TYPE_DECL
)
5960 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5963 // Cannot redeclare a class template with a different set of constraints.
5964 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5966 error_at (input_location
, "redeclaration %q#D with different "
5967 "constraints", tmpl
);
5968 inform (DECL_SOURCE_LOCATION (tmpl
),
5969 "original declaration appeared here");
5975 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5976 to be used when the caller has already checked
5977 (processing_template_decl
5978 && !instantiation_dependent_expression_p (expr)
5979 && potential_constant_expression (expr))
5980 and cleared processing_template_decl. */
5983 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5985 return tsubst_copy_and_build (expr
,
5988 /*in_decl=*/NULL_TREE
,
5989 /*function_p=*/false,
5990 /*integral_constant_expression_p=*/true);
5993 /* Simplify EXPR if it is a non-dependent expression. Returns the
5994 (possibly simplified) expression. */
5997 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5999 if (expr
== NULL_TREE
)
6002 /* If we're in a template, but EXPR isn't value dependent, simplify
6003 it. We're supposed to treat:
6005 template <typename T> void f(T[1 + 1]);
6006 template <typename T> void f(T[2]);
6008 as two declarations of the same function, for example. */
6009 if (processing_template_decl
6010 && is_nondependent_constant_expression (expr
))
6012 processing_template_decl_sentinel s
;
6013 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6019 instantiate_non_dependent_expr (tree expr
)
6021 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6024 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6025 an uninstantiated expression. */
6028 instantiate_non_dependent_or_null (tree expr
)
6030 if (expr
== NULL_TREE
)
6032 if (processing_template_decl
)
6034 if (!is_nondependent_constant_expression (expr
))
6038 processing_template_decl_sentinel s
;
6039 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6045 /* True iff T is a specialization of a variable template. */
6048 variable_template_specialization_p (tree t
)
6050 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6052 tree tmpl
= DECL_TI_TEMPLATE (t
);
6053 return variable_template_p (tmpl
);
6056 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6057 template declaration, or a TYPE_DECL for an alias declaration. */
6060 alias_type_or_template_p (tree t
)
6064 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6067 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6068 || DECL_ALIAS_TEMPLATE_P (t
));
6071 /* Return TRUE iff T is a specialization of an alias template. */
6074 alias_template_specialization_p (const_tree t
)
6076 /* It's an alias template specialization if it's an alias and its
6077 TYPE_NAME is a specialization of a primary template. */
6078 if (TYPE_ALIAS_P (t
))
6079 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6080 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6085 /* An alias template is complex from a SFINAE perspective if a template-id
6086 using that alias can be ill-formed when the expansion is not, as with
6087 the void_t template. We determine this by checking whether the
6088 expansion for the alias template uses all its template parameters. */
6090 struct uses_all_template_parms_data
6097 uses_all_template_parms_r (tree t
, void *data_
)
6099 struct uses_all_template_parms_data
&data
6100 = *(struct uses_all_template_parms_data
*)data_
;
6101 tree idx
= get_template_parm_index (t
);
6103 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6104 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6109 complex_alias_template_p (const_tree tmpl
)
6111 struct uses_all_template_parms_data data
;
6112 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6113 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6114 data
.level
= TMPL_PARMS_DEPTH (parms
);
6115 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6116 data
.seen
= XALLOCAVEC (bool, len
);
6117 for (int i
= 0; i
< len
; ++i
)
6118 data
.seen
[i
] = false;
6120 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6121 for (int i
= 0; i
< len
; ++i
)
6127 /* Return TRUE iff T is a specialization of a complex alias template with
6128 dependent template-arguments. */
6131 dependent_alias_template_spec_p (const_tree t
)
6133 if (!alias_template_specialization_p (t
))
6136 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6137 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6140 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6141 if (!any_dependent_template_arguments_p (args
))
6147 /* Return the number of innermost template parameters in TMPL. */
6150 num_innermost_template_parms (tree tmpl
)
6152 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6153 return TREE_VEC_LENGTH (parms
);
6156 /* Return either TMPL or another template that it is equivalent to under DR
6157 1286: An alias that just changes the name of a template is equivalent to
6158 the other template. */
6161 get_underlying_template (tree tmpl
)
6163 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6164 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6166 /* Determine if the alias is equivalent to an underlying template. */
6167 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6168 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6172 tree underlying
= TI_TEMPLATE (tinfo
);
6173 if (!PRIMARY_TEMPLATE_P (underlying
)
6174 || (num_innermost_template_parms (tmpl
)
6175 != num_innermost_template_parms (underlying
)))
6178 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6179 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6180 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6183 /* Alias is equivalent. Strip it and repeat. */
6190 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6191 must be a reference-to-function or a pointer-to-function type, as specified
6192 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6193 and check that the resulting function has external linkage. */
6196 convert_nontype_argument_function (tree type
, tree expr
,
6197 tsubst_flags_t complain
)
6201 linkage_kind linkage
;
6203 fn
= instantiate_type (type
, fns
, tf_none
);
6204 if (fn
== error_mark_node
)
6205 return error_mark_node
;
6207 if (value_dependent_expression_p (fn
))
6210 fn_no_ptr
= strip_fnptr_conv (fn
);
6211 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6212 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6213 if (BASELINK_P (fn_no_ptr
))
6214 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6216 /* [temp.arg.nontype]/1
6218 A template-argument for a non-type, non-template template-parameter
6221 -- the address of an object or function with external [C++11: or
6222 internal] linkage. */
6224 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6226 if (complain
& tf_error
)
6228 error ("%qE is not a valid template argument for type %qT",
6230 if (TYPE_PTR_P (type
))
6231 inform (input_location
, "it must be the address of a function "
6232 "with external linkage");
6234 inform (input_location
, "it must be the name of a function with "
6235 "external linkage");
6240 linkage
= decl_linkage (fn_no_ptr
);
6241 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6243 if (complain
& tf_error
)
6245 if (cxx_dialect
>= cxx11
)
6246 error ("%qE is not a valid template argument for type %qT "
6247 "because %qD has no linkage",
6248 expr
, type
, fn_no_ptr
);
6250 error ("%qE is not a valid template argument for type %qT "
6251 "because %qD does not have external linkage",
6252 expr
, type
, fn_no_ptr
);
6258 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6260 if (REFERENCE_REF_P (fn
))
6261 fn
= TREE_OPERAND (fn
, 0);
6263 fn
= build_address (fn
);
6265 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6266 fn
= build_nop (type
, fn
);
6271 /* Subroutine of convert_nontype_argument.
6272 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6273 Emit an error otherwise. */
6276 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6277 tsubst_flags_t complain
)
6279 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6280 tree orig_expr
= expr
;
6282 if (null_ptr_cst_p (expr
))
6284 if (TREE_CODE (expr
) == PTRMEM_CST
6285 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6286 PTRMEM_CST_CLASS (expr
)))
6288 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6290 if (processing_template_decl
6291 && TREE_CODE (expr
) == ADDR_EXPR
6292 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6294 if (complain
& tf_error
)
6296 error_at (loc
, "%qE is not a valid template argument for type %qT",
6298 if (TREE_CODE (expr
) != PTRMEM_CST
)
6299 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6301 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6306 /* Returns TRUE iff the address of OP is value-dependent.
6308 14.6.2.4 [temp.dep.temp]:
6309 A non-integral non-type template-argument is dependent if its type is
6310 dependent or it has either of the following forms
6313 and contains a nested-name-specifier which specifies a class-name that
6314 names a dependent type.
6316 We generalize this to just say that the address of a member of a
6317 dependent class is value-dependent; the above doesn't cover the
6318 address of a static data member named with an unqualified-id. */
6321 has_value_dependent_address (tree op
)
6323 /* We could use get_inner_reference here, but there's no need;
6324 this is only relevant for template non-type arguments, which
6325 can only be expressed as &id-expression. */
6328 tree ctx
= CP_DECL_CONTEXT (op
);
6329 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6336 /* The next set of functions are used for providing helpful explanatory
6337 diagnostics for failed overload resolution. Their messages should be
6338 indented by two spaces for consistency with the messages in
6342 unify_success (bool /*explain_p*/)
6347 /* Other failure functions should call this one, to provide a single function
6348 for setting a breakpoint on. */
6351 unify_invalid (bool /*explain_p*/)
6357 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6360 inform (input_location
,
6361 " couldn't deduce template parameter %qD", parm
);
6362 return unify_invalid (explain_p
);
6366 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6369 inform (input_location
,
6370 " types %qT and %qT have incompatible cv-qualifiers",
6372 return unify_invalid (explain_p
);
6376 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6379 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6380 return unify_invalid (explain_p
);
6384 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6387 inform (input_location
,
6388 " template parameter %qD is not a parameter pack, but "
6391 return unify_invalid (explain_p
);
6395 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6398 inform (input_location
,
6399 " template argument %qE does not match "
6400 "pointer-to-member constant %qE",
6402 return unify_invalid (explain_p
);
6406 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6409 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6410 return unify_invalid (explain_p
);
6414 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6417 inform (input_location
,
6418 " inconsistent parameter pack deduction with %qT and %qT",
6420 return unify_invalid (explain_p
);
6424 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6429 inform (input_location
,
6430 " deduced conflicting types for parameter %qT (%qT and %qT)",
6431 parm
, first
, second
);
6433 inform (input_location
,
6434 " deduced conflicting values for non-type parameter "
6435 "%qE (%qE and %qE)", parm
, first
, second
);
6437 return unify_invalid (explain_p
);
6441 unify_vla_arg (bool explain_p
, tree arg
)
6444 inform (input_location
,
6445 " variable-sized array type %qT is not "
6446 "a valid template argument",
6448 return unify_invalid (explain_p
);
6452 unify_method_type_error (bool explain_p
, tree arg
)
6455 inform (input_location
,
6456 " member function type %qT is not a valid template argument",
6458 return unify_invalid (explain_p
);
6462 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6467 inform_n (input_location
, wanted
,
6468 " candidate expects at least %d argument, %d provided",
6469 " candidate expects at least %d arguments, %d provided",
6472 inform_n (input_location
, wanted
,
6473 " candidate expects %d argument, %d provided",
6474 " candidate expects %d arguments, %d provided",
6477 return unify_invalid (explain_p
);
6481 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6483 return unify_arity (explain_p
, have
, wanted
);
6487 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6488 bool least_p
= false)
6490 return unify_arity (explain_p
, have
, wanted
, least_p
);
6494 unify_arg_conversion (bool explain_p
, tree to_type
,
6495 tree from_type
, tree arg
)
6498 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6499 " cannot convert %qE (type %qT) to type %qT",
6500 arg
, from_type
, to_type
);
6501 return unify_invalid (explain_p
);
6505 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6506 tree parm
, tree arg
)
6511 case tbr_ambiguous_baseclass
:
6512 inform (input_location
, " %qT is an ambiguous base class of %qT",
6516 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6519 return unify_invalid (explain_p
);
6523 unify_inconsistent_template_template_parameters (bool explain_p
)
6526 inform (input_location
,
6527 " template parameters of a template template argument are "
6528 "inconsistent with other deduced template arguments");
6529 return unify_invalid (explain_p
);
6533 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6536 inform (input_location
,
6537 " can't deduce a template for %qT from non-template type %qT",
6539 return unify_invalid (explain_p
);
6543 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6546 inform (input_location
,
6547 " template argument %qE does not match %qE", arg
, parm
);
6548 return unify_invalid (explain_p
);
6551 /* Attempt to convert the non-type template parameter EXPR to the
6552 indicated TYPE. If the conversion is successful, return the
6553 converted value. If the conversion is unsuccessful, return
6554 NULL_TREE if we issued an error message, or error_mark_node if we
6555 did not. We issue error messages for out-and-out bad template
6556 parameters, but not simply because the conversion failed, since we
6557 might be just trying to do argument deduction. Both TYPE and EXPR
6558 must be non-dependent.
6560 The conversion follows the special rules described in
6561 [temp.arg.nontype], and it is much more strict than an implicit
6564 This function is called twice for each template argument (see
6565 lookup_template_class for a more accurate description of this
6566 problem). This means that we need to handle expressions which
6567 are not valid in a C++ source, but can be created from the
6568 first call (for instance, casts to perform conversions). These
6569 hacks can go away after we fix the double coercion problem. */
6572 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6575 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6576 tree orig_expr
= expr
;
6578 /* Detect immediately string literals as invalid non-type argument.
6579 This special-case is not needed for correctness (we would easily
6580 catch this later), but only to provide better diagnostic for this
6581 common user mistake. As suggested by DR 100, we do not mention
6582 linkage issues in the diagnostic as this is not the point. */
6583 /* FIXME we're making this OK. */
6584 if (TREE_CODE (expr
) == STRING_CST
)
6586 if (complain
& tf_error
)
6587 error ("%qE is not a valid template argument for type %qT "
6588 "because string literals can never be used in this context",
6593 /* Add the ADDR_EXPR now for the benefit of
6594 value_dependent_expression_p. */
6595 if (TYPE_PTROBV_P (type
)
6596 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6598 expr
= decay_conversion (expr
, complain
);
6599 if (expr
== error_mark_node
)
6600 return error_mark_node
;
6603 /* If we are in a template, EXPR may be non-dependent, but still
6604 have a syntactic, rather than semantic, form. For example, EXPR
6605 might be a SCOPE_REF, rather than the VAR_DECL to which the
6606 SCOPE_REF refers. Preserving the qualifying scope is necessary
6607 so that access checking can be performed when the template is
6608 instantiated -- but here we need the resolved form so that we can
6609 convert the argument. */
6610 bool non_dep
= false;
6611 if (TYPE_REF_OBJ_P (type
)
6612 && has_value_dependent_address (expr
))
6613 /* If we want the address and it's value-dependent, don't fold. */;
6614 else if (processing_template_decl
6615 && is_nondependent_constant_expression (expr
))
6617 if (error_operand_p (expr
))
6618 return error_mark_node
;
6619 expr_type
= TREE_TYPE (expr
);
6621 /* If the argument is non-dependent, perform any conversions in
6622 non-dependent context as well. */
6623 processing_template_decl_sentinel
s (non_dep
);
6625 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6627 if (value_dependent_expression_p (expr
))
6628 expr
= canonicalize_expr_argument (expr
, complain
);
6630 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6631 to a non-type argument of "nullptr". */
6632 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6633 expr
= fold_simple (convert (type
, expr
));
6635 /* In C++11, integral or enumeration non-type template arguments can be
6636 arbitrary constant expressions. Pointer and pointer to
6637 member arguments can be general constant expressions that evaluate
6638 to a null value, but otherwise still need to be of a specific form. */
6639 if (cxx_dialect
>= cxx11
)
6641 if (TREE_CODE (expr
) == PTRMEM_CST
)
6642 /* A PTRMEM_CST is already constant, and a valid template
6643 argument for a parameter of pointer to member type, we just want
6644 to leave it in that form rather than lower it to a
6646 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6647 || cxx_dialect
>= cxx17
)
6649 /* C++17: A template-argument for a non-type template-parameter shall
6650 be a converted constant expression (8.20) of the type of the
6651 template-parameter. */
6652 expr
= build_converted_constant_expr (type
, expr
, complain
);
6653 if (expr
== error_mark_node
)
6654 return error_mark_node
;
6655 expr
= maybe_constant_value (expr
);
6656 expr
= convert_from_reference (expr
);
6658 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6660 tree folded
= maybe_constant_value (expr
);
6661 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6662 : null_member_pointer_value_p (folded
))
6667 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6668 expr
= mark_lvalue_use (expr
);
6670 expr
= mark_rvalue_use (expr
);
6672 /* HACK: Due to double coercion, we can get a
6673 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6674 which is the tree that we built on the first call (see
6675 below when coercing to reference to object or to reference to
6676 function). We just strip everything and get to the arg.
6677 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6679 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6681 tree probe_type
, probe
= expr
;
6682 if (REFERENCE_REF_P (probe
))
6683 probe
= TREE_OPERAND (probe
, 0);
6684 probe_type
= TREE_TYPE (probe
);
6685 if (TREE_CODE (probe
) == NOP_EXPR
)
6687 /* ??? Maybe we could use convert_from_reference here, but we
6688 would need to relax its constraints because the NOP_EXPR
6689 could actually change the type to something more cv-qualified,
6690 and this is not folded by convert_from_reference. */
6691 tree addr
= TREE_OPERAND (probe
, 0);
6692 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6693 && TREE_CODE (addr
) == ADDR_EXPR
6694 && TYPE_PTR_P (TREE_TYPE (addr
))
6695 && (same_type_ignoring_top_level_qualifiers_p
6696 (TREE_TYPE (probe_type
),
6697 TREE_TYPE (TREE_TYPE (addr
)))))
6699 expr
= TREE_OPERAND (addr
, 0);
6700 expr_type
= TREE_TYPE (probe_type
);
6705 /* [temp.arg.nontype]/5, bullet 1
6707 For a non-type template-parameter of integral or enumeration type,
6708 integral promotions (_conv.prom_) and integral conversions
6709 (_conv.integral_) are applied. */
6710 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6712 if (cxx_dialect
< cxx11
)
6714 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6715 t
= maybe_constant_value (t
);
6716 if (t
!= error_mark_node
)
6720 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6721 return error_mark_node
;
6723 /* Notice that there are constant expressions like '4 % 0' which
6724 do not fold into integer constants. */
6725 if (TREE_CODE (expr
) != INTEGER_CST
6726 && !value_dependent_expression_p (expr
))
6728 if (complain
& tf_error
)
6730 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6731 if (!require_potential_constant_expression (expr
))
6732 expr
= error_mark_node
;
6734 expr
= cxx_constant_value (expr
);
6735 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6736 inform (loc
, "in template argument for type %qT ", type
);
6737 if (expr
== error_mark_node
)
6739 /* else cxx_constant_value complained but gave us
6740 a real constant, so go ahead. */
6741 if (TREE_CODE (expr
) != INTEGER_CST
)
6743 /* Some assemble time constant expressions like
6744 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6745 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6746 as we can emit them into .rodata initializers of
6747 variables, yet they can't fold into an INTEGER_CST at
6748 compile time. Refuse them here. */
6749 gcc_checking_assert (reduced_constant_expression_p (expr
));
6750 error_at (loc
, "template argument %qE for type %qT not "
6751 "a constant integer", expr
, type
);
6759 /* Avoid typedef problems. */
6760 if (TREE_TYPE (expr
) != type
)
6761 expr
= fold_convert (type
, expr
);
6763 /* [temp.arg.nontype]/5, bullet 2
6765 For a non-type template-parameter of type pointer to object,
6766 qualification conversions (_conv.qual_) and the array-to-pointer
6767 conversion (_conv.array_) are applied. */
6768 else if (TYPE_PTROBV_P (type
))
6770 tree decayed
= expr
;
6772 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6773 decay_conversion or an explicit cast. If it's a problematic cast,
6774 we'll complain about it below. */
6775 if (TREE_CODE (expr
) == NOP_EXPR
)
6779 if (TREE_CODE (probe
) == ADDR_EXPR
6780 && TYPE_PTR_P (TREE_TYPE (probe
)))
6783 expr_type
= TREE_TYPE (expr
);
6787 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6789 A template-argument for a non-type, non-template template-parameter
6790 shall be one of: [...]
6792 -- the name of a non-type template-parameter;
6793 -- the address of an object or function with external linkage, [...]
6794 expressed as "& id-expression" where the & is optional if the name
6795 refers to a function or array, or if the corresponding
6796 template-parameter is a reference.
6798 Here, we do not care about functions, as they are invalid anyway
6799 for a parameter of type pointer-to-object. */
6801 if (value_dependent_expression_p (expr
))
6802 /* Non-type template parameters are OK. */
6804 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6805 /* Null pointer values are OK in C++11. */;
6806 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6810 if (complain
& tf_error
)
6811 error ("%qD is not a valid template argument "
6812 "because %qD is a variable, not the address of "
6813 "a variable", orig_expr
, expr
);
6816 if (POINTER_TYPE_P (expr_type
))
6818 if (complain
& tf_error
)
6819 error ("%qE is not a valid template argument for %qT "
6820 "because it is not the address of a variable",
6824 /* Other values, like integer constants, might be valid
6825 non-type arguments of some other type. */
6826 return error_mark_node
;
6830 tree decl
= TREE_OPERAND (expr
, 0);
6834 if (complain
& tf_error
)
6835 error ("%qE is not a valid template argument of type %qT "
6836 "because %qE is not a variable", orig_expr
, type
, decl
);
6839 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6841 if (complain
& tf_error
)
6842 error ("%qE is not a valid template argument of type %qT "
6843 "because %qD does not have external linkage",
6844 orig_expr
, type
, decl
);
6847 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6848 && decl_linkage (decl
) == lk_none
)
6850 if (complain
& tf_error
)
6851 error ("%qE is not a valid template argument of type %qT "
6852 "because %qD has no linkage", orig_expr
, type
, decl
);
6855 /* C++17: For a non-type template-parameter of reference or pointer
6856 type, the value of the constant expression shall not refer to (or
6857 for a pointer type, shall not be the address of):
6858 * a subobject (4.5),
6859 * a temporary object (15.2),
6860 * a string literal (5.13.5),
6861 * the result of a typeid expression (8.2.8), or
6862 * a predefined __func__ variable (11.4.1). */
6863 else if (DECL_ARTIFICIAL (decl
))
6865 if (complain
& tf_error
)
6866 error ("the address of %qD is not a valid template argument",
6870 else if (!same_type_ignoring_top_level_qualifiers_p
6871 (strip_array_types (TREE_TYPE (type
)),
6872 strip_array_types (TREE_TYPE (decl
))))
6874 if (complain
& tf_error
)
6875 error ("the address of the %qT subobject of %qD is not a "
6876 "valid template argument", TREE_TYPE (type
), decl
);
6879 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6881 if (complain
& tf_error
)
6882 error ("the address of %qD is not a valid template argument "
6883 "because it does not have static storage duration",
6891 expr
= perform_qualification_conversions (type
, expr
);
6892 if (expr
== error_mark_node
)
6893 return error_mark_node
;
6895 /* [temp.arg.nontype]/5, bullet 3
6897 For a non-type template-parameter of type reference to object, no
6898 conversions apply. The type referred to by the reference may be more
6899 cv-qualified than the (otherwise identical) type of the
6900 template-argument. The template-parameter is bound directly to the
6901 template-argument, which must be an lvalue. */
6902 else if (TYPE_REF_OBJ_P (type
))
6904 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6906 return error_mark_node
;
6908 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6910 if (complain
& tf_error
)
6911 error ("%qE is not a valid template argument for type %qT "
6912 "because of conflicts in cv-qualification", expr
, type
);
6916 if (!lvalue_p (expr
))
6918 if (complain
& tf_error
)
6919 error ("%qE is not a valid template argument for type %qT "
6920 "because it is not an lvalue", expr
, type
);
6924 /* [temp.arg.nontype]/1
6926 A template-argument for a non-type, non-template template-parameter
6927 shall be one of: [...]
6929 -- the address of an object or function with external linkage. */
6930 if (INDIRECT_REF_P (expr
)
6931 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6933 expr
= TREE_OPERAND (expr
, 0);
6936 if (complain
& tf_error
)
6937 error ("%q#D is not a valid template argument for type %qT "
6938 "because a reference variable does not have a constant "
6939 "address", expr
, type
);
6944 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6945 && value_dependent_expression_p (expr
))
6946 /* OK, dependent reference. We don't want to ask whether a DECL is
6947 itself value-dependent, since what we want here is its address. */;
6952 if (complain
& tf_error
)
6953 error ("%qE is not a valid template argument for type %qT "
6954 "because it is not an object with linkage",
6959 /* DR 1155 allows internal linkage in C++11 and up. */
6960 linkage_kind linkage
= decl_linkage (expr
);
6961 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6963 if (complain
& tf_error
)
6964 error ("%qE is not a valid template argument for type %qT "
6965 "because object %qD does not have linkage",
6970 expr
= build_address (expr
);
6973 if (!same_type_p (type
, TREE_TYPE (expr
)))
6974 expr
= build_nop (type
, expr
);
6976 /* [temp.arg.nontype]/5, bullet 4
6978 For a non-type template-parameter of type pointer to function, only
6979 the function-to-pointer conversion (_conv.func_) is applied. If the
6980 template-argument represents a set of overloaded functions (or a
6981 pointer to such), the matching function is selected from the set
6983 else if (TYPE_PTRFN_P (type
))
6985 /* If the argument is a template-id, we might not have enough
6986 context information to decay the pointer. */
6987 if (!type_unknown_p (expr_type
))
6989 expr
= decay_conversion (expr
, complain
);
6990 if (expr
== error_mark_node
)
6991 return error_mark_node
;
6994 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6995 /* Null pointer values are OK in C++11. */
6996 return perform_qualification_conversions (type
, expr
);
6998 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6999 if (!expr
|| expr
== error_mark_node
)
7002 /* [temp.arg.nontype]/5, bullet 5
7004 For a non-type template-parameter of type reference to function, no
7005 conversions apply. If the template-argument represents a set of
7006 overloaded functions, the matching function is selected from the set
7008 else if (TYPE_REFFN_P (type
))
7010 if (TREE_CODE (expr
) == ADDR_EXPR
)
7012 if (complain
& tf_error
)
7014 error ("%qE is not a valid template argument for type %qT "
7015 "because it is a pointer", expr
, type
);
7016 inform (input_location
, "try using %qE instead",
7017 TREE_OPERAND (expr
, 0));
7022 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7023 if (!expr
|| expr
== error_mark_node
)
7026 /* [temp.arg.nontype]/5, bullet 6
7028 For a non-type template-parameter of type pointer to member function,
7029 no conversions apply. If the template-argument represents a set of
7030 overloaded member functions, the matching member function is selected
7031 from the set (_over.over_). */
7032 else if (TYPE_PTRMEMFUNC_P (type
))
7034 expr
= instantiate_type (type
, expr
, tf_none
);
7035 if (expr
== error_mark_node
)
7036 return error_mark_node
;
7038 /* [temp.arg.nontype] bullet 1 says the pointer to member
7039 expression must be a pointer-to-member constant. */
7040 if (!value_dependent_expression_p (expr
)
7041 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7044 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7045 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7046 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7047 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7049 /* [temp.arg.nontype]/5, bullet 7
7051 For a non-type template-parameter of type pointer to data member,
7052 qualification conversions (_conv.qual_) are applied. */
7053 else if (TYPE_PTRDATAMEM_P (type
))
7055 /* [temp.arg.nontype] bullet 1 says the pointer to member
7056 expression must be a pointer-to-member constant. */
7057 if (!value_dependent_expression_p (expr
)
7058 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7061 expr
= perform_qualification_conversions (type
, expr
);
7062 if (expr
== error_mark_node
)
7065 else if (NULLPTR_TYPE_P (type
))
7067 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7069 if (complain
& tf_error
)
7070 error ("%qE is not a valid template argument for type %qT "
7071 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7076 /* A template non-type parameter must be one of the above. */
7080 /* Sanity check: did we actually convert the argument to the
7082 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7083 (type
, TREE_TYPE (expr
)));
7084 return convert_from_reference (expr
);
7087 /* Subroutine of coerce_template_template_parms, which returns 1 if
7088 PARM_PARM and ARG_PARM match using the rule for the template
7089 parameters of template template parameters. Both PARM and ARG are
7090 template parameters; the rest of the arguments are the same as for
7091 coerce_template_template_parms.
7094 coerce_template_template_parm (tree parm
,
7096 tsubst_flags_t complain
,
7100 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7101 || parm
== NULL_TREE
|| error_operand_p (parm
))
7104 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7107 switch (TREE_CODE (parm
))
7110 /* We encounter instantiations of templates like
7111 template <template <template <class> class> class TT>
7114 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7115 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7117 if (!coerce_template_template_parms
7118 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7124 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7125 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7126 /* Argument is a parameter pack but parameter is not. */
7131 /* The tsubst call is used to handle cases such as
7133 template <int> class C {};
7134 template <class T, template <T> class TT> class D {};
7137 i.e. the parameter list of TT depends on earlier parameters. */
7138 if (!uses_template_parms (TREE_TYPE (arg
)))
7140 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7141 if (!uses_template_parms (t
)
7142 && !same_type_p (t
, TREE_TYPE (arg
)))
7146 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7147 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7148 /* Argument is a parameter pack but parameter is not. */
7160 /* Coerce template argument list ARGLIST for use with template
7161 template-parameter TEMPL. */
7164 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7165 tsubst_flags_t complain
)
7167 /* Consider an example where a template template parameter declared as
7169 template <class T, class U = std::allocator<T> > class TT
7171 The template parameter level of T and U are one level larger than
7172 of TT. To proper process the default argument of U, say when an
7173 instantiation `TT<int>' is seen, we need to build the full
7174 arguments containing {int} as the innermost level. Outer levels,
7175 available when not appearing as default template argument, can be
7176 obtained from the arguments of the enclosing template.
7178 Suppose that TT is later substituted with std::vector. The above
7179 instantiation is `TT<int, std::allocator<T> >' with TT at
7180 level 1, and T at level 2, while the template arguments at level 1
7181 becomes {std::vector} and the inner level 2 is {int}. */
7183 tree outer
= DECL_CONTEXT (templ
);
7186 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7187 /* We want arguments for the partial specialization, not arguments for
7188 the primary template. */
7189 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7191 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7193 else if (current_template_parms
)
7195 /* This is an argument of the current template, so we haven't set
7196 DECL_CONTEXT yet. */
7197 tree relevant_template_parms
;
7199 /* Parameter levels that are greater than the level of the given
7200 template template parm are irrelevant. */
7201 relevant_template_parms
= current_template_parms
;
7202 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7203 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7204 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7206 outer
= template_parms_to_args (relevant_template_parms
);
7210 arglist
= add_to_template_args (outer
, arglist
);
7212 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7213 return coerce_template_parms (parmlist
, arglist
, templ
,
7215 /*require_all_args=*/true,
7216 /*use_default_args=*/true);
7219 /* A cache of template template parameters with match-all default
7221 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7223 store_defaulted_ttp (tree v
, tree t
)
7225 if (!defaulted_ttp_cache
)
7226 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7227 defaulted_ttp_cache
->put (v
, t
);
7230 lookup_defaulted_ttp (tree v
)
7232 if (defaulted_ttp_cache
)
7233 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7238 /* T is a bound template template-parameter. Copy its arguments into default
7239 arguments of the template template-parameter's template parameters. */
7242 add_defaults_to_ttp (tree otmpl
)
7244 if (tree c
= lookup_defaulted_ttp (otmpl
))
7247 tree ntmpl
= copy_node (otmpl
);
7249 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7250 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7251 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7252 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7253 TYPE_NAME (ntype
) = ntmpl
;
7254 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7256 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7257 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7258 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7259 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7261 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7262 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7263 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7264 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7265 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7267 tree o
= TREE_VEC_ELT (vec
, i
);
7268 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7270 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7271 TREE_PURPOSE (n
) = any_targ_node
;
7275 store_defaulted_ttp (otmpl
, ntmpl
);
7279 /* ARG is a bound potential template template-argument, and PARGS is a list
7280 of arguments for the corresponding template template-parameter. Adjust
7281 PARGS as appropriate for application to ARG's template, and if ARG is a
7282 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7283 arguments to the template template parameter. */
7286 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7288 ++processing_template_decl
;
7289 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7290 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7292 /* When comparing two template template-parameters in partial ordering,
7293 rewrite the one currently being used as an argument to have default
7294 arguments for all parameters. */
7295 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7296 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7297 if (pargs
!= error_mark_node
)
7298 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7299 TYPE_TI_ARGS (arg
));
7304 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7305 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7306 /*require_all*/true,
7307 /*use_default*/true);
7309 --processing_template_decl
;
7313 /* Subroutine of unify for the case when PARM is a
7314 BOUND_TEMPLATE_TEMPLATE_PARM. */
7317 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7320 tree parmvec
= TYPE_TI_ARGS (parm
);
7321 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7323 /* The template template parm might be variadic and the argument
7324 not, so flatten both argument lists. */
7325 parmvec
= expand_template_argument_pack (parmvec
);
7326 argvec
= expand_template_argument_pack (argvec
);
7330 /* In keeping with P0522R0, adjust P's template arguments
7331 to apply to A's template; then flatten it again. */
7332 tree nparmvec
= parmvec
;
7333 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7334 nparmvec
= expand_template_argument_pack (nparmvec
);
7336 if (unify (tparms
, targs
, nparmvec
, argvec
,
7337 UNIFY_ALLOW_NONE
, explain_p
))
7340 /* If the P0522 adjustment eliminated a pack expansion, deduce
7343 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7344 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7345 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7350 /* Deduce arguments T, i from TT<T> or TT<i>.
7351 We check each element of PARMVEC and ARGVEC individually
7352 rather than the whole TREE_VEC since they can have
7353 different number of elements, which is allowed under N2555. */
7355 int len
= TREE_VEC_LENGTH (parmvec
);
7357 /* Check if the parameters end in a pack, making them
7359 int parm_variadic_p
= 0;
7361 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7362 parm_variadic_p
= 1;
7364 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7365 /* If the template argument list of P contains a pack
7366 expansion that is not the last template argument, the
7367 entire template argument list is a non-deduced
7369 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7370 return unify_success (explain_p
);
7372 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7373 return unify_too_few_arguments (explain_p
,
7374 TREE_VEC_LENGTH (argvec
), len
);
7376 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7377 if (unify (tparms
, targs
,
7378 TREE_VEC_ELT (parmvec
, i
),
7379 TREE_VEC_ELT (argvec
, i
),
7380 UNIFY_ALLOW_NONE
, explain_p
))
7384 && unify_pack_expansion (tparms
, targs
,
7387 /*subr=*/true, explain_p
))
7394 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7395 template template parameters. Both PARM_PARMS and ARG_PARMS are
7396 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7399 Consider the example:
7400 template <class T> class A;
7401 template<template <class U> class TT> class B;
7403 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7404 the parameters to A, and OUTER_ARGS contains A. */
7407 coerce_template_template_parms (tree parm_parms
,
7409 tsubst_flags_t complain
,
7413 int nparms
, nargs
, i
;
7417 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7418 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7420 nparms
= TREE_VEC_LENGTH (parm_parms
);
7421 nargs
= TREE_VEC_LENGTH (arg_parms
);
7425 /* P0522R0: A template template-parameter P is at least as specialized as
7426 a template template-argument A if, given the following rewrite to two
7427 function templates, the function template corresponding to P is at
7428 least as specialized as the function template corresponding to A
7429 according to the partial ordering rules for function templates
7430 ([temp.func.order]). Given an invented class template X with the
7431 template parameter list of A (including default arguments):
7433 * Each of the two function templates has the same template parameters,
7434 respectively, as P or A.
7436 * Each function template has a single function parameter whose type is
7437 a specialization of X with template arguments corresponding to the
7438 template parameters from the respective function template where, for
7439 each template parameter PP in the template parameter list of the
7440 function template, a corresponding template argument AA is formed. If
7441 PP declares a parameter pack, then AA is the pack expansion
7442 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7444 If the rewrite produces an invalid type, then P is not at least as
7445 specialized as A. */
7447 /* So coerce P's args to apply to A's parms, and then deduce between A's
7448 args and the converted args. If that succeeds, A is at least as
7449 specialized as P, so they match.*/
7450 tree pargs
= template_parms_level_to_args (parm_parms
);
7451 ++processing_template_decl
;
7452 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7453 /*require_all*/true, /*use_default*/true);
7454 --processing_template_decl
;
7455 if (pargs
!= error_mark_node
)
7457 tree targs
= make_tree_vec (nargs
);
7458 tree aargs
= template_parms_level_to_args (arg_parms
);
7459 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7465 /* Determine whether we have a parameter pack at the end of the
7466 template template parameter's template parameter list. */
7467 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7469 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7471 if (error_operand_p (parm
))
7474 switch (TREE_CODE (parm
))
7478 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7483 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7493 && !(variadic_p
&& nargs
>= nparms
- 1))
7496 /* Check all of the template parameters except the parameter pack at
7497 the end (if any). */
7498 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7500 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7501 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7504 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7505 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7507 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7515 /* Check each of the template parameters in the template
7516 argument against the template parameter pack at the end of
7517 the template template parameter. */
7518 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7521 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7523 for (; i
< nargs
; ++i
)
7525 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7528 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7530 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7539 /* Verifies that the deduced template arguments (in TARGS) for the
7540 template template parameters (in TPARMS) represent valid bindings,
7541 by comparing the template parameter list of each template argument
7542 to the template parameter list of its corresponding template
7543 template parameter, in accordance with DR150. This
7544 routine can only be called after all template arguments have been
7545 deduced. It will return TRUE if all of the template template
7546 parameter bindings are okay, FALSE otherwise. */
7548 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7550 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7553 /* We're dealing with template parms in this process. */
7554 ++processing_template_decl
;
7556 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7558 for (i
= 0; i
< ntparms
; ++i
)
7560 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7561 tree targ
= TREE_VEC_ELT (targs
, i
);
7563 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7565 tree packed_args
= NULL_TREE
;
7568 if (ARGUMENT_PACK_P (targ
))
7570 /* Look inside the argument pack. */
7571 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7572 len
= TREE_VEC_LENGTH (packed_args
);
7575 for (idx
= 0; idx
< len
; ++idx
)
7577 tree targ_parms
= NULL_TREE
;
7580 /* Extract the next argument from the argument
7582 targ
= TREE_VEC_ELT (packed_args
, idx
);
7584 if (PACK_EXPANSION_P (targ
))
7585 /* Look at the pattern of the pack expansion. */
7586 targ
= PACK_EXPANSION_PATTERN (targ
);
7588 /* Extract the template parameters from the template
7590 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7591 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7592 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7593 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7595 /* Verify that we can coerce the template template
7596 parameters from the template argument to the template
7597 parameter. This requires an exact match. */
7599 && !coerce_template_template_parms
7600 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7615 --processing_template_decl
;
7619 /* Since type attributes aren't mangled, we need to strip them from
7620 template type arguments. */
7623 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7625 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7627 bool removed_attributes
= false;
7628 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7629 if (removed_attributes
7630 && (complain
& tf_warning
))
7631 warning (OPT_Wignored_attributes
,
7632 "ignoring attributes on template argument %qT", arg
);
7636 /* And from inside dependent non-type arguments like sizeof(Type). */
7639 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7641 if (!arg
|| arg
== error_mark_node
)
7643 bool removed_attributes
= false;
7644 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7645 if (removed_attributes
7646 && (complain
& tf_warning
))
7647 warning (OPT_Wignored_attributes
,
7648 "ignoring attributes in template argument %qE", arg
);
7652 // A template declaration can be substituted for a constrained
7653 // template template parameter only when the argument is more
7654 // constrained than the parameter.
7656 is_compatible_template_arg (tree parm
, tree arg
)
7658 tree parm_cons
= get_constraints (parm
);
7660 /* For now, allow constrained template template arguments
7661 and unconstrained template template parameters. */
7662 if (parm_cons
== NULL_TREE
)
7665 tree arg_cons
= get_constraints (arg
);
7667 // If the template parameter is constrained, we need to rewrite its
7668 // constraints in terms of the ARG's template parameters. This ensures
7669 // that all of the template parameter types will have the same depth.
7671 // Note that this is only valid when coerce_template_template_parm is
7672 // true for the innermost template parameters of PARM and ARG. In other
7673 // words, because coercion is successful, this conversion will be valid.
7676 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7677 parm_cons
= tsubst_constraint_info (parm_cons
,
7678 INNERMOST_TEMPLATE_ARGS (args
),
7679 tf_none
, NULL_TREE
);
7680 if (parm_cons
== error_mark_node
)
7684 return subsumes (parm_cons
, arg_cons
);
7687 // Convert a placeholder argument into a binding to the original
7688 // parameter. The original parameter is saved as the TREE_TYPE of
7691 convert_wildcard_argument (tree parm
, tree arg
)
7693 TREE_TYPE (arg
) = parm
;
7697 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7698 because one of them is dependent. But we need to represent the
7699 conversion for the benefit of cp_tree_equal. */
7702 maybe_convert_nontype_argument (tree type
, tree arg
)
7704 /* Auto parms get no conversion. */
7705 if (type_uses_auto (type
))
7707 /* We don't need or want to add this conversion now if we're going to use the
7708 argument for deduction. */
7709 if (value_dependent_expression_p (arg
))
7712 type
= cv_unqualified (type
);
7713 tree argtype
= TREE_TYPE (arg
);
7714 if (same_type_p (type
, argtype
))
7717 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7718 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7722 /* Convert the indicated template ARG as necessary to match the
7723 indicated template PARM. Returns the converted ARG, or
7724 error_mark_node if the conversion was unsuccessful. Error and
7725 warning messages are issued under control of COMPLAIN. This
7726 conversion is for the Ith parameter in the parameter list. ARGS is
7727 the full set of template arguments deduced so far. */
7730 convert_template_argument (tree parm
,
7733 tsubst_flags_t complain
,
7739 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7741 if (parm
== error_mark_node
)
7742 return error_mark_node
;
7744 /* Trivially convert placeholders. */
7745 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7746 return convert_wildcard_argument (parm
, arg
);
7748 if (arg
== any_targ_node
)
7751 if (TREE_CODE (arg
) == TREE_LIST
7752 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7754 /* The template argument was the name of some
7755 member function. That's usually
7756 invalid, but static members are OK. In any
7757 case, grab the underlying fields/functions
7758 and issue an error later if required. */
7759 orig_arg
= TREE_VALUE (arg
);
7760 TREE_TYPE (arg
) = unknown_type_node
;
7765 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7766 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7767 || requires_tmpl_type
);
7769 /* When determining whether an argument pack expansion is a template,
7770 look at the pattern. */
7771 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7772 arg
= PACK_EXPANSION_PATTERN (arg
);
7774 /* Deal with an injected-class-name used as a template template arg. */
7775 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7777 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7778 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7780 if (cxx_dialect
>= cxx11
)
7781 /* OK under DR 1004. */;
7782 else if (complain
& tf_warning_or_error
)
7783 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7784 " used as template template argument", TYPE_NAME (arg
));
7785 else if (flag_pedantic_errors
)
7793 ((TREE_CODE (arg
) == TEMPLATE_DECL
7794 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7795 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7796 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7797 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7800 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7801 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7802 arg
= TYPE_STUB_DECL (arg
);
7804 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7806 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7807 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7809 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7811 if (complain
& tf_error
)
7812 error ("invalid use of destructor %qE as a type", orig_arg
);
7813 return error_mark_node
;
7816 permerror (input_location
,
7817 "to refer to a type member of a template parameter, "
7818 "use %<typename %E%>", orig_arg
);
7820 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7821 TREE_OPERAND (arg
, 1),
7827 if (is_type
!= requires_type
)
7831 if (complain
& tf_error
)
7833 error ("type/value mismatch at argument %d in template "
7834 "parameter list for %qD",
7837 inform (input_location
,
7838 " expected a constant of type %qT, got %qT",
7840 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7841 else if (requires_tmpl_type
)
7842 inform (input_location
,
7843 " expected a class template, got %qE", orig_arg
);
7845 inform (input_location
,
7846 " expected a type, got %qE", orig_arg
);
7849 return error_mark_node
;
7851 if (is_tmpl_type
^ requires_tmpl_type
)
7853 if (in_decl
&& (complain
& tf_error
))
7855 error ("type/value mismatch at argument %d in template "
7856 "parameter list for %qD",
7859 inform (input_location
,
7860 " expected a type, got %qT", DECL_NAME (arg
));
7862 inform (input_location
,
7863 " expected a class template, got %qT", orig_arg
);
7865 return error_mark_node
;
7868 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7869 /* We already did the appropriate conversion when packing args. */
7873 if (requires_tmpl_type
)
7875 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7876 /* The number of argument required is not known yet.
7877 Just accept it for now. */
7881 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7884 /* Strip alias templates that are equivalent to another
7886 arg
= get_underlying_template (arg
);
7887 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7889 if (coerce_template_template_parms (parmparm
, argparm
,
7895 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7897 if (val
!= error_mark_node
)
7899 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7900 val
= TREE_TYPE (val
);
7901 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7902 val
= make_pack_expansion (val
, complain
);
7907 if (in_decl
&& (complain
& tf_error
))
7909 error ("type/value mismatch at argument %d in "
7910 "template parameter list for %qD",
7912 inform (input_location
,
7913 " expected a template of type %qD, got %qT",
7917 val
= error_mark_node
;
7920 // Check that the constraints are compatible before allowing the
7922 if (val
!= error_mark_node
)
7923 if (!is_compatible_template_arg (parm
, arg
))
7925 if (in_decl
&& (complain
& tf_error
))
7927 error ("constraint mismatch at argument %d in "
7928 "template parameter list for %qD",
7930 inform (input_location
, " expected %qD but got %qD",
7933 val
= error_mark_node
;
7939 /* We only form one instance of each template specialization.
7940 Therefore, if we use a non-canonical variant (i.e., a
7941 typedef), any future messages referring to the type will use
7942 the typedef, which is confusing if those future uses do not
7943 themselves also use the typedef. */
7945 val
= canonicalize_type_argument (val
, complain
);
7949 tree t
= TREE_TYPE (parm
);
7951 if (tree a
= type_uses_auto (t
))
7953 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7954 if (t
== error_mark_node
)
7955 return error_mark_node
;
7958 t
= tsubst (t
, args
, complain
, in_decl
);
7960 if (invalid_nontype_parm_type_p (t
, complain
))
7961 return error_mark_node
;
7963 if (!type_dependent_expression_p (orig_arg
)
7964 && !uses_template_parms (t
))
7965 /* We used to call digest_init here. However, digest_init
7966 will report errors, which we don't want when complain
7967 is zero. More importantly, digest_init will try too
7968 hard to convert things: for example, `0' should not be
7969 converted to pointer type at this point according to
7970 the standard. Accepting this is not merely an
7971 extension, since deciding whether or not these
7972 conversions can occur is part of determining which
7973 function template to call, or whether a given explicit
7974 argument specification is valid. */
7975 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7978 val
= canonicalize_expr_argument (orig_arg
, complain
);
7979 val
= maybe_convert_nontype_argument (t
, val
);
7983 if (val
== NULL_TREE
)
7984 val
= error_mark_node
;
7985 else if (val
== error_mark_node
&& (complain
& tf_error
))
7986 error ("could not convert template argument %qE from %qT to %qT",
7987 orig_arg
, TREE_TYPE (orig_arg
), t
);
7989 if (INDIRECT_REF_P (val
))
7991 /* Reject template arguments that are references to built-in
7992 functions with no library fallbacks. */
7993 const_tree inner
= TREE_OPERAND (val
, 0);
7994 const_tree innertype
= TREE_TYPE (inner
);
7996 && TREE_CODE (innertype
) == REFERENCE_TYPE
7997 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7998 && TREE_OPERAND_LENGTH (inner
) > 0
7999 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8000 return error_mark_node
;
8003 if (TREE_CODE (val
) == SCOPE_REF
)
8005 /* Strip typedefs from the SCOPE_REF. */
8006 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8007 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8009 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8010 QUALIFIED_NAME_IS_TEMPLATE (val
));
8017 /* Coerces the remaining template arguments in INNER_ARGS (from
8018 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8019 Returns the coerced argument pack. PARM_IDX is the position of this
8020 parameter in the template parameter list. ARGS is the original
8021 template argument list. */
8023 coerce_template_parameter_pack (tree parms
,
8031 tsubst_flags_t complain
)
8033 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8034 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8037 tree packed_parms
= NULL_TREE
;
8039 if (arg_idx
> nargs
)
8042 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8044 /* When the template parameter is a non-type template parameter pack
8045 or template template parameter pack whose type or template
8046 parameters use parameter packs, we know exactly how many arguments
8047 we are looking for. Build a vector of the instantiated decls for
8048 these template parameters in PACKED_PARMS. */
8049 /* We can't use make_pack_expansion here because it would interpret a
8050 _DECL as a use rather than a declaration. */
8051 tree decl
= TREE_VALUE (parm
);
8052 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8053 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8054 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8055 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8057 TREE_VEC_LENGTH (args
)--;
8058 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8059 TREE_VEC_LENGTH (args
)++;
8061 if (packed_parms
== error_mark_node
)
8062 return error_mark_node
;
8064 /* If we're doing a partial instantiation of a member template,
8065 verify that all of the types used for the non-type
8066 template parameter pack are, in fact, valid for non-type
8067 template parameters. */
8069 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8071 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8072 for (j
= 0; j
< len
; ++j
)
8074 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8075 if (invalid_nontype_parm_type_p (t
, complain
))
8076 return error_mark_node
;
8078 /* We don't know how many args we have yet, just
8079 use the unconverted ones for now. */
8083 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8085 /* Check if we have a placeholder pack, which indicates we're
8086 in the context of a introduction list. In that case we want
8087 to match this pack to the single placeholder. */
8088 else if (arg_idx
< nargs
8089 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8090 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8092 nargs
= arg_idx
+ 1;
8093 packed_args
= make_tree_vec (1);
8096 packed_args
= make_tree_vec (nargs
- arg_idx
);
8098 /* Convert the remaining arguments, which will be a part of the
8099 parameter pack "parm". */
8100 int first_pack_arg
= arg_idx
;
8101 for (; arg_idx
< nargs
; ++arg_idx
)
8103 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8104 tree actual_parm
= TREE_VALUE (parm
);
8105 int pack_idx
= arg_idx
- first_pack_arg
;
8109 /* Once we've packed as many args as we have types, stop. */
8110 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8112 else if (PACK_EXPANSION_P (arg
))
8113 /* We don't know how many args we have yet, just
8114 use the unconverted ones for now. */
8117 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8120 if (arg
== error_mark_node
)
8122 if (complain
& tf_error
)
8123 error ("template argument %d is invalid", arg_idx
+ 1);
8126 arg
= convert_template_argument (actual_parm
,
8127 arg
, new_args
, complain
, parm_idx
,
8129 if (arg
== error_mark_node
)
8131 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8134 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8135 && TREE_VEC_LENGTH (packed_args
) > 0)
8137 if (complain
& tf_error
)
8138 error ("wrong number of template arguments (%d, should be %d)",
8139 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8140 return error_mark_node
;
8143 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8144 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8145 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8148 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8149 TREE_CONSTANT (argument_pack
) = 1;
8152 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8154 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8155 TREE_VEC_LENGTH (packed_args
));
8156 return argument_pack
;
8159 /* Returns the number of pack expansions in the template argument vector
8163 pack_expansion_args_count (tree args
)
8168 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8170 tree elt
= TREE_VEC_ELT (args
, i
);
8171 if (elt
&& PACK_EXPANSION_P (elt
))
8177 /* Convert all template arguments to their appropriate types, and
8178 return a vector containing the innermost resulting template
8179 arguments. If any error occurs, return error_mark_node. Error and
8180 warning messages are issued under control of COMPLAIN.
8182 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8183 for arguments not specified in ARGS. Otherwise, if
8184 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8185 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8186 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8190 coerce_template_parms (tree parms
,
8193 tsubst_flags_t complain
,
8194 bool require_all_args
,
8195 bool use_default_args
)
8197 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8198 tree orig_inner_args
;
8201 tree new_inner_args
;
8202 int saved_unevaluated_operand
;
8203 int saved_inhibit_evaluation_warnings
;
8205 /* When used as a boolean value, indicates whether this is a
8206 variadic template parameter list. Since it's an int, we can also
8207 subtract it from nparms to get the number of non-variadic
8210 int variadic_args_p
= 0;
8211 int post_variadic_parms
= 0;
8213 /* Likewise for parameters with default arguments. */
8216 if (args
== error_mark_node
)
8217 return error_mark_node
;
8219 nparms
= TREE_VEC_LENGTH (parms
);
8221 /* Determine if there are any parameter packs or default arguments. */
8222 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8224 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8226 ++post_variadic_parms
;
8227 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8229 if (TREE_PURPOSE (parm
))
8233 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8234 /* If there are no parameters that follow a parameter pack, we need to
8235 expand any argument packs so that we can deduce a parameter pack from
8236 some non-packed args followed by an argument pack, as in variadic85.C.
8237 If there are such parameters, we need to leave argument packs intact
8238 so the arguments are assigned properly. This can happen when dealing
8239 with a nested class inside a partial specialization of a class
8240 template, as in variadic92.C, or when deducing a template parameter pack
8241 from a sub-declarator, as in variadic114.C. */
8242 if (!post_variadic_parms
)
8243 inner_args
= expand_template_argument_pack (inner_args
);
8245 /* Count any pack expansion args. */
8246 variadic_args_p
= pack_expansion_args_count (inner_args
);
8248 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8249 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8250 || (nargs
< nparms
- variadic_p
8253 && (!use_default_args
8254 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8255 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8257 if (complain
& tf_error
)
8259 if (variadic_p
|| default_p
)
8261 nparms
-= variadic_p
+ default_p
;
8262 error ("wrong number of template arguments "
8263 "(%d, should be at least %d)", nargs
, nparms
);
8266 error ("wrong number of template arguments "
8267 "(%d, should be %d)", nargs
, nparms
);
8270 inform (DECL_SOURCE_LOCATION (in_decl
),
8271 "provided for %qD", in_decl
);
8274 return error_mark_node
;
8276 /* We can't pass a pack expansion to a non-pack parameter of an alias
8277 template (DR 1430). */
8279 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8280 || concept_template_p (in_decl
))
8282 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8284 if (complain
& tf_error
)
8286 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8288 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8289 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8291 if (PACK_EXPANSION_P (arg
)
8292 && !template_parameter_pack_p (parm
))
8294 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8295 error_at (location_of (arg
),
8296 "pack expansion argument for non-pack parameter "
8297 "%qD of alias template %qD", parm
, in_decl
);
8299 error_at (location_of (arg
),
8300 "pack expansion argument for non-pack parameter "
8301 "%qD of concept %qD", parm
, in_decl
);
8302 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8309 return error_mark_node
;
8312 /* We need to evaluate the template arguments, even though this
8313 template-id may be nested within a "sizeof". */
8314 saved_unevaluated_operand
= cp_unevaluated_operand
;
8315 cp_unevaluated_operand
= 0;
8316 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8317 c_inhibit_evaluation_warnings
= 0;
8318 new_inner_args
= make_tree_vec (nparms
);
8319 new_args
= add_outermost_template_args (args
, new_inner_args
);
8320 int pack_adjust
= 0;
8321 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8326 /* Get the Ith template parameter. */
8327 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8329 if (parm
== error_mark_node
)
8331 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8335 /* Calculate the next argument. */
8336 if (arg_idx
< nargs
)
8337 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8341 if (template_parameter_pack_p (TREE_VALUE (parm
))
8342 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8344 /* Some arguments will be placed in the
8345 template parameter pack PARM. */
8346 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8347 inner_args
, arg_idx
,
8351 if (arg
== NULL_TREE
)
8353 /* We don't know how many args we have yet, just use the
8354 unconverted (and still packed) ones for now. */
8355 new_inner_args
= orig_inner_args
;
8360 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8362 /* Store this argument. */
8363 if (arg
== error_mark_node
)
8366 /* We are done with all of the arguments. */
8371 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8372 arg_idx
+= pack_adjust
;
8379 if (PACK_EXPANSION_P (arg
))
8381 /* "If every valid specialization of a variadic template
8382 requires an empty template parameter pack, the template is
8383 ill-formed, no diagnostic required." So check that the
8384 pattern works with this parameter. */
8385 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8386 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8390 if (conv
== error_mark_node
)
8392 if (complain
& tf_error
)
8393 inform (input_location
, "so any instantiation with a "
8394 "non-empty parameter pack would be ill-formed");
8397 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8398 /* Recover from missing typename. */
8399 TREE_VEC_ELT (inner_args
, arg_idx
)
8400 = make_pack_expansion (conv
, complain
);
8402 /* We don't know how many args we have yet, just
8403 use the unconverted ones for now. */
8404 new_inner_args
= inner_args
;
8409 else if (require_all_args
)
8411 /* There must be a default arg in this case. */
8412 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8414 /* The position of the first default template argument,
8415 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8417 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8418 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8419 arg_idx
- pack_adjust
);
8424 if (arg
== error_mark_node
)
8426 if (complain
& tf_error
)
8427 error ("template argument %d is invalid", arg_idx
+ 1);
8430 /* This only occurs if there was an error in the template
8431 parameter list itself (which we would already have
8432 reported) that we are trying to recover from, e.g., a class
8433 template with a parameter list such as
8434 template<typename..., typename>. */
8437 arg
= convert_template_argument (TREE_VALUE (parm
),
8438 arg
, new_args
, complain
,
8441 if (arg
== error_mark_node
)
8443 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8445 cp_unevaluated_operand
= saved_unevaluated_operand
;
8446 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8448 if (variadic_p
&& arg_idx
< nargs
)
8450 if (complain
& tf_error
)
8452 error ("wrong number of template arguments "
8453 "(%d, should be %d)", nargs
, arg_idx
);
8455 error ("provided for %q+D", in_decl
);
8457 return error_mark_node
;
8461 return error_mark_node
;
8463 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8464 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8465 TREE_VEC_LENGTH (new_inner_args
));
8467 return new_inner_args
;
8470 /* Convert all template arguments to their appropriate types, and
8471 return a vector containing the innermost resulting template
8472 arguments. If any error occurs, return error_mark_node. Error and
8473 warning messages are not issued.
8475 Note that no function argument deduction is performed, and default
8476 arguments are used to fill in unspecified arguments. */
8478 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8480 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8483 /* Convert all template arguments to their appropriate type, and
8484 instantiate default arguments as needed. This returns a vector
8485 containing the innermost resulting template arguments, or
8486 error_mark_node if unsuccessful. */
8488 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8489 tsubst_flags_t complain
)
8491 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8494 /* Like coerce_template_parms. If PARMS represents all template
8495 parameters levels, this function returns a vector of vectors
8496 representing all the resulting argument levels. Note that in this
8497 case, only the innermost arguments are coerced because the
8498 outermost ones are supposed to have been coerced already.
8500 Otherwise, if PARMS represents only (the innermost) vector of
8501 parameters, this function returns a vector containing just the
8502 innermost resulting arguments. */
8505 coerce_innermost_template_parms (tree parms
,
8508 tsubst_flags_t complain
,
8509 bool require_all_args
,
8510 bool use_default_args
)
8512 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8513 int args_depth
= TMPL_ARGS_DEPTH (args
);
8516 if (parms_depth
> 1)
8518 coerced_args
= make_tree_vec (parms_depth
);
8522 for (level
= parms
, cur_depth
= parms_depth
;
8523 parms_depth
> 0 && level
!= NULL_TREE
;
8524 level
= TREE_CHAIN (level
), --cur_depth
)
8527 if (cur_depth
== args_depth
)
8528 l
= coerce_template_parms (TREE_VALUE (level
),
8529 args
, in_decl
, complain
,
8533 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8535 if (l
== error_mark_node
)
8536 return error_mark_node
;
8538 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8542 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8543 args
, in_decl
, complain
,
8546 return coerced_args
;
8549 /* Returns 1 if template args OT and NT are equivalent. */
8552 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8556 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8558 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8561 if (TREE_CODE (nt
) == TREE_VEC
)
8562 /* For member templates */
8563 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8564 else if (PACK_EXPANSION_P (ot
))
8565 return (PACK_EXPANSION_P (nt
)
8566 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8567 PACK_EXPANSION_PATTERN (nt
))
8568 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8569 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8570 else if (ARGUMENT_PACK_P (ot
))
8575 if (!ARGUMENT_PACK_P (nt
))
8578 opack
= ARGUMENT_PACK_ARGS (ot
);
8579 npack
= ARGUMENT_PACK_ARGS (nt
);
8580 len
= TREE_VEC_LENGTH (opack
);
8581 if (TREE_VEC_LENGTH (npack
) != len
)
8583 for (i
= 0; i
< len
; ++i
)
8584 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8585 TREE_VEC_ELT (npack
, i
)))
8589 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8591 else if (TYPE_P (nt
))
8595 /* Don't treat an alias template specialization with dependent
8596 arguments as equivalent to its underlying type when used as a
8597 template argument; we need them to be distinct so that we
8598 substitute into the specialization arguments at instantiation
8599 time. And aliases can't be equivalent without being ==, so
8600 we don't need to look any deeper.
8602 During partial ordering, however, we need to treat them normally so
8603 that we can order uses of the same alias with different
8604 cv-qualification (79960). */
8606 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8609 return same_type_p (ot
, nt
);
8611 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8615 /* Try to treat a template non-type argument that has been converted
8616 to the parameter type as equivalent to one that hasn't yet. */
8617 for (enum tree_code code1
= TREE_CODE (ot
);
8618 CONVERT_EXPR_CODE_P (code1
)
8619 || code1
== NON_LVALUE_EXPR
;
8620 code1
= TREE_CODE (ot
))
8621 ot
= TREE_OPERAND (ot
, 0);
8622 for (enum tree_code code2
= TREE_CODE (nt
);
8623 CONVERT_EXPR_CODE_P (code2
)
8624 || code2
== NON_LVALUE_EXPR
;
8625 code2
= TREE_CODE (nt
))
8626 nt
= TREE_OPERAND (nt
, 0);
8628 return cp_tree_equal (ot
, nt
);
8632 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8633 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8634 NEWARG_PTR with the offending arguments if they are non-NULL. */
8637 comp_template_args (tree oldargs
, tree newargs
,
8638 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8643 if (oldargs
== newargs
)
8646 if (!oldargs
|| !newargs
)
8649 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8652 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8654 tree nt
= TREE_VEC_ELT (newargs
, i
);
8655 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8657 if (! template_args_equal (ot
, nt
, partial_order
))
8659 if (oldarg_ptr
!= NULL
)
8661 if (newarg_ptr
!= NULL
)
8670 comp_template_args_porder (tree oargs
, tree nargs
)
8672 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8676 add_pending_template (tree d
)
8678 tree ti
= (TYPE_P (d
)
8679 ? CLASSTYPE_TEMPLATE_INFO (d
)
8680 : DECL_TEMPLATE_INFO (d
));
8681 struct pending_template
*pt
;
8684 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8687 /* We are called both from instantiate_decl, where we've already had a
8688 tinst_level pushed, and instantiate_template, where we haven't.
8690 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8693 push_tinst_level (d
);
8695 pt
= ggc_alloc
<pending_template
> ();
8697 pt
->tinst
= current_tinst_level
;
8698 if (last_pending_template
)
8699 last_pending_template
->next
= pt
;
8701 pending_templates
= pt
;
8703 last_pending_template
= pt
;
8705 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8712 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8713 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8714 documentation for TEMPLATE_ID_EXPR. */
8717 lookup_template_function (tree fns
, tree arglist
)
8721 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8722 return error_mark_node
;
8724 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8726 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8728 error ("%q#D is not a function template", fns
);
8729 return error_mark_node
;
8732 if (BASELINK_P (fns
))
8734 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8736 BASELINK_FUNCTIONS (fns
),
8741 type
= TREE_TYPE (fns
);
8742 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8743 type
= unknown_type_node
;
8745 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8748 /* Within the scope of a template class S<T>, the name S gets bound
8749 (in build_self_reference) to a TYPE_DECL for the class, not a
8750 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8751 or one of its enclosing classes, and that type is a template,
8752 return the associated TEMPLATE_DECL. Otherwise, the original
8755 Also handle the case when DECL is a TREE_LIST of ambiguous
8756 injected-class-names from different bases. */
8759 maybe_get_template_decl_from_type_decl (tree decl
)
8761 if (decl
== NULL_TREE
)
8764 /* DR 176: A lookup that finds an injected-class-name (10.2
8765 [class.member.lookup]) can result in an ambiguity in certain cases
8766 (for example, if it is found in more than one base class). If all of
8767 the injected-class-names that are found refer to specializations of
8768 the same class template, and if the name is followed by a
8769 template-argument-list, the reference refers to the class template
8770 itself and not a specialization thereof, and is not ambiguous. */
8771 if (TREE_CODE (decl
) == TREE_LIST
)
8773 tree t
, tmpl
= NULL_TREE
;
8774 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8776 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8779 else if (tmpl
!= elt
)
8782 if (tmpl
&& t
== NULL_TREE
)
8788 return (decl
!= NULL_TREE
8789 && DECL_SELF_REFERENCE_P (decl
)
8790 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8791 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8794 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8795 parameters, find the desired type.
8797 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8799 IN_DECL, if non-NULL, is the template declaration we are trying to
8802 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8803 the class we are looking up.
8805 Issue error and warning messages under control of COMPLAIN.
8807 If the template class is really a local class in a template
8808 function, then the FUNCTION_CONTEXT is the function in which it is
8811 ??? Note that this function is currently called *twice* for each
8812 template-id: the first time from the parser, while creating the
8813 incomplete type (finish_template_type), and the second type during the
8814 real instantiation (instantiate_template_class). This is surely something
8815 that we want to avoid. It also causes some problems with argument
8816 coercion (see convert_nontype_argument for more information on this). */
8819 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8820 int entering_scope
, tsubst_flags_t complain
)
8822 tree templ
= NULL_TREE
, parmlist
;
8829 if (identifier_p (d1
))
8831 tree value
= innermost_non_namespace_value (d1
);
8832 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8837 push_decl_namespace (context
);
8838 templ
= lookup_name (d1
);
8839 templ
= maybe_get_template_decl_from_type_decl (templ
);
8841 pop_decl_namespace ();
8844 context
= DECL_CONTEXT (templ
);
8846 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8848 tree type
= TREE_TYPE (d1
);
8850 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8851 an implicit typename for the second A. Deal with it. */
8852 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8853 type
= TREE_TYPE (type
);
8855 if (CLASSTYPE_TEMPLATE_INFO (type
))
8857 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8858 d1
= DECL_NAME (templ
);
8861 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8862 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8864 templ
= TYPE_TI_TEMPLATE (d1
);
8865 d1
= DECL_NAME (templ
);
8867 else if (DECL_TYPE_TEMPLATE_P (d1
))
8870 d1
= DECL_NAME (templ
);
8871 context
= DECL_CONTEXT (templ
);
8873 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8876 d1
= DECL_NAME (templ
);
8879 /* Issue an error message if we didn't find a template. */
8882 if (complain
& tf_error
)
8883 error ("%qT is not a template", d1
);
8884 return error_mark_node
;
8887 if (TREE_CODE (templ
) != TEMPLATE_DECL
8888 /* Make sure it's a user visible template, if it was named by
8890 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8891 && !PRIMARY_TEMPLATE_P (templ
)))
8893 if (complain
& tf_error
)
8895 error ("non-template type %qT used as a template", d1
);
8897 error ("for template declaration %q+D", in_decl
);
8899 return error_mark_node
;
8902 complain
&= ~tf_user
;
8904 /* An alias that just changes the name of a template is equivalent to the
8905 other template, so if any of the arguments are pack expansions, strip
8906 the alias to avoid problems with a pack expansion passed to a non-pack
8907 alias template parameter (DR 1430). */
8908 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8909 templ
= get_underlying_template (templ
);
8911 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8914 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8915 if (arglist2
== error_mark_node
8916 || (!uses_template_parms (arglist2
)
8917 && check_instantiated_args (templ
, arglist2
, complain
)))
8918 return error_mark_node
;
8920 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8925 tree template_type
= TREE_TYPE (templ
);
8928 tree found
= NULL_TREE
;
8931 int is_dependent_type
;
8932 int use_partial_inst_tmpl
= false;
8934 if (template_type
== error_mark_node
)
8935 /* An error occurred while building the template TEMPL, and a
8936 diagnostic has most certainly been emitted for that
8937 already. Let's propagate that error. */
8938 return error_mark_node
;
8940 gen_tmpl
= most_general_template (templ
);
8941 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8942 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8943 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8945 if (arg_depth
== 1 && parm_depth
> 1)
8947 /* We've been given an incomplete set of template arguments.
8950 template <class T> struct S1 {
8951 template <class U> struct S2 {};
8952 template <class U> struct S2<U*> {};
8955 we will be called with an ARGLIST of `U*', but the
8956 TEMPLATE will be `template <class T> template
8957 <class U> struct S1<T>::S2'. We must fill in the missing
8959 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8960 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8961 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8964 /* Now we should have enough arguments. */
8965 gcc_assert (parm_depth
== arg_depth
);
8967 /* From here on, we're only interested in the most general
8970 /* Calculate the BOUND_ARGS. These will be the args that are
8971 actually tsubst'd into the definition to create the
8973 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8975 /*require_all_args=*/true,
8976 /*use_default_args=*/true);
8978 if (arglist
== error_mark_node
)
8979 /* We were unable to bind the arguments. */
8980 return error_mark_node
;
8982 /* In the scope of a template class, explicit references to the
8983 template class refer to the type of the template, not any
8984 instantiation of it. For example, in:
8986 template <class T> class C { void f(C<T>); }
8988 the `C<T>' is just the same as `C'. Outside of the
8989 class, however, such a reference is an instantiation. */
8991 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8992 || currently_open_class (template_type
))
8994 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8996 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8997 return template_type
;
9000 /* If we already have this specialization, return it. */
9001 elt
.tmpl
= gen_tmpl
;
9003 elt
.spec
= NULL_TREE
;
9004 hash
= spec_hasher::hash (&elt
);
9005 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9010 /* If the the template's constraints are not satisfied,
9011 then we cannot form a valid type.
9013 Note that the check is deferred until after the hash
9014 lookup. This prevents redundant checks on previously
9015 instantiated specializations. */
9016 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9018 if (complain
& tf_error
)
9020 error ("template constraint failure");
9021 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9023 return error_mark_node
;
9026 is_dependent_type
= uses_template_parms (arglist
);
9028 /* If the deduced arguments are invalid, then the binding
9030 if (!is_dependent_type
9031 && check_instantiated_args (gen_tmpl
,
9032 INNERMOST_TEMPLATE_ARGS (arglist
),
9034 return error_mark_node
;
9036 if (!is_dependent_type
9037 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9038 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9039 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9041 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9042 DECL_NAME (gen_tmpl
),
9043 /*tag_scope=*/ts_global
);
9047 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
9049 if (context
== error_mark_node
)
9050 return error_mark_node
;
9053 context
= global_namespace
;
9055 /* Create the type. */
9056 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9058 /* The user referred to a specialization of an alias
9059 template represented by GEN_TMPL.
9061 [temp.alias]/2 says:
9063 When a template-id refers to the specialization of an
9064 alias template, it is equivalent to the associated
9065 type obtained by substitution of its
9066 template-arguments for the template-parameters in the
9067 type-id of the alias template. */
9069 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9070 /* Note that the call above (by indirectly calling
9071 register_specialization in tsubst_decl) registers the
9072 TYPE_DECL representing the specialization of the alias
9073 template. So next time someone substitutes ARGLIST for
9074 the template parms into the alias template (GEN_TMPL),
9075 she'll get that TYPE_DECL back. */
9077 if (t
== error_mark_node
)
9080 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9082 if (!is_dependent_type
)
9084 set_current_access_from_decl (TYPE_NAME (template_type
));
9085 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9086 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9087 arglist
, complain
, in_decl
),
9088 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9089 arglist
, complain
, in_decl
),
9090 SCOPED_ENUM_P (template_type
), NULL
);
9092 if (t
== error_mark_node
)
9097 /* We don't want to call start_enum for this type, since
9098 the values for the enumeration constants may involve
9099 template parameters. And, no one should be interested
9100 in the enumeration constants for such a type. */
9101 t
= cxx_make_type (ENUMERAL_TYPE
);
9102 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9104 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9105 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9106 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9108 else if (CLASS_TYPE_P (template_type
))
9110 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9111 instantiated here. */
9112 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9114 t
= make_class_type (TREE_CODE (template_type
));
9115 CLASSTYPE_DECLARED_CLASS (t
)
9116 = CLASSTYPE_DECLARED_CLASS (template_type
);
9117 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9119 /* A local class. Make sure the decl gets registered properly. */
9120 if (context
== current_function_decl
)
9121 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
9123 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9124 /* This instantiation is another name for the primary
9125 template type. Set the TYPE_CANONICAL field
9127 TYPE_CANONICAL (t
) = template_type
;
9128 else if (any_template_arguments_need_structural_equality_p (arglist
))
9129 /* Some of the template arguments require structural
9130 equality testing, so this template class requires
9131 structural equality testing. */
9132 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9137 /* If we called start_enum or pushtag above, this information
9138 will already be set up. */
9141 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9143 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9144 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9145 DECL_SOURCE_LOCATION (type_decl
)
9146 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9149 type_decl
= TYPE_NAME (t
);
9151 if (CLASS_TYPE_P (template_type
))
9153 TREE_PRIVATE (type_decl
)
9154 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9155 TREE_PROTECTED (type_decl
)
9156 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9157 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9159 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9160 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9164 if (OVERLOAD_TYPE_P (t
)
9165 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9167 static const char *tags
[] = {"abi_tag", "may_alias"};
9169 for (unsigned ix
= 0; ix
!= 2; ix
++)
9172 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9176 = tree_cons (TREE_PURPOSE (attributes
),
9177 TREE_VALUE (attributes
),
9178 TYPE_ATTRIBUTES (t
));
9182 /* Let's consider the explicit specialization of a member
9183 of a class template specialization that is implicitly instantiated,
9188 template<class U> struct M {}; //#0
9193 struct S<int>::M<char> //#1
9197 [temp.expl.spec]/4 says this is valid.
9199 In this case, when we write:
9202 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9205 When we encounter #1, we want to store the partial instantiation
9206 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9208 For all cases other than this "explicit specialization of member of a
9209 class template", we just want to store the most general template into
9210 the CLASSTYPE_TI_TEMPLATE of M.
9212 This case of "explicit specialization of member of a class template"
9214 1/ the enclosing class is an instantiation of, and therefore not
9215 the same as, the context of the most general template, and
9216 2/ we aren't looking at the partial instantiation itself, i.e.
9217 the innermost arguments are not the same as the innermost parms of
9218 the most general template.
9220 So it's only when 1/ and 2/ happens that we want to use the partial
9221 instantiation of the member template in lieu of its most general
9224 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9225 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9226 /* the enclosing class must be an instantiation... */
9227 && CLASS_TYPE_P (context
)
9228 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9230 TREE_VEC_LENGTH (arglist
)--;
9231 ++processing_template_decl
;
9232 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9233 tree partial_inst_args
=
9234 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9235 arglist
, complain
, NULL_TREE
);
9236 --processing_template_decl
;
9237 TREE_VEC_LENGTH (arglist
)++;
9238 if (partial_inst_args
== error_mark_node
)
9239 return error_mark_node
;
9240 use_partial_inst_tmpl
=
9241 /*...and we must not be looking at the partial instantiation
9243 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9247 if (!use_partial_inst_tmpl
)
9248 /* This case is easy; there are no member templates involved. */
9252 /* This is a full instantiation of a member template. Find
9253 the partial instantiation of which this is an instance. */
9255 /* Temporarily reduce by one the number of levels in the ARGLIST
9256 so as to avoid comparing the last set of arguments. */
9257 TREE_VEC_LENGTH (arglist
)--;
9258 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9259 TREE_VEC_LENGTH (arglist
)++;
9260 /* FOUND is either a proper class type, or an alias
9261 template specialization. In the later case, it's a
9262 TYPE_DECL, resulting from the substituting of arguments
9263 for parameters in the TYPE_DECL of the alias template
9264 done earlier. So be careful while getting the template
9266 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9268 : (TREE_CODE (found
) == TYPE_DECL
9269 ? DECL_TI_TEMPLATE (found
)
9270 : CLASSTYPE_TI_TEMPLATE (found
)));
9273 // Build template info for the new specialization.
9274 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9277 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9278 entry
= ggc_alloc
<spec_entry
> ();
9282 /* Note this use of the partial instantiation so we can check it
9283 later in maybe_process_partial_specialization. */
9284 DECL_TEMPLATE_INSTANTIATIONS (found
)
9285 = tree_cons (arglist
, t
,
9286 DECL_TEMPLATE_INSTANTIATIONS (found
));
9288 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9289 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9290 /* Now that the type has been registered on the instantiations
9291 list, we set up the enumerators. Because the enumeration
9292 constants may involve the enumeration type itself, we make
9293 sure to register the type first, and then create the
9294 constants. That way, doing tsubst_expr for the enumeration
9295 constants won't result in recursive calls here; we'll find
9296 the instantiation and exit above. */
9297 tsubst_enum (template_type
, t
, arglist
);
9299 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9300 /* If the type makes use of template parameters, the
9301 code that generates debugging information will crash. */
9302 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9304 /* Possibly limit visibility based on template args. */
9305 TREE_PUBLIC (type_decl
) = 1;
9306 determine_visibility (type_decl
);
9308 inherit_targ_abi_tags (t
);
9314 /* Wrapper for lookup_template_class_1. */
9317 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9318 int entering_scope
, tsubst_flags_t complain
)
9321 timevar_push (TV_TEMPLATE_INST
);
9322 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9323 entering_scope
, complain
);
9324 timevar_pop (TV_TEMPLATE_INST
);
9328 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9331 lookup_template_variable (tree templ
, tree arglist
)
9333 /* The type of the expression is NULL_TREE since the template-id could refer
9334 to an explicit or partial specialization. */
9335 tree type
= NULL_TREE
;
9336 if (flag_concepts
&& variable_concept_p (templ
))
9337 /* Except that concepts are always bool. */
9338 type
= boolean_type_node
;
9339 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9342 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9345 finish_template_variable (tree var
, tsubst_flags_t complain
)
9347 tree templ
= TREE_OPERAND (var
, 0);
9348 tree arglist
= TREE_OPERAND (var
, 1);
9350 /* We never want to return a VAR_DECL for a variable concept, since they
9351 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9352 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9353 if (concept_p
&& processing_template_decl
)
9356 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9357 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9359 templ
= most_general_template (templ
);
9360 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9361 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9363 /*use_default*/true);
9365 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9367 if (complain
& tf_error
)
9369 error ("use of invalid variable template %qE", var
);
9370 diagnose_constraints (location_of (var
), templ
, arglist
);
9372 return error_mark_node
;
9375 /* If a template-id refers to a specialization of a variable
9376 concept, then the expression is true if and only if the
9377 concept's constraints are satisfied by the given template
9380 NOTE: This is an extension of Concepts Lite TS that
9381 allows constraints to be used in expressions. */
9384 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9385 return evaluate_variable_concept (decl
, arglist
);
9388 return instantiate_template (templ
, arglist
, complain
);
9391 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9392 TARGS template args, and instantiate it if it's not dependent. */
9395 lookup_and_finish_template_variable (tree templ
, tree targs
,
9396 tsubst_flags_t complain
)
9398 templ
= lookup_template_variable (templ
, targs
);
9399 if (!any_dependent_template_arguments_p (targs
))
9401 templ
= finish_template_variable (templ
, complain
);
9405 return convert_from_reference (templ
);
9414 /* True when we should also visit template parameters that occur in
9415 non-deduced contexts. */
9416 bool include_nondeduced_p
;
9417 hash_set
<tree
> *visited
;
9420 /* Called from for_each_template_parm via walk_tree. */
9423 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9426 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9427 tree_fn_t fn
= pfd
->fn
;
9428 void *data
= pfd
->data
;
9429 tree result
= NULL_TREE
;
9431 #define WALK_SUBTREE(NODE) \
9434 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9435 pfd->include_nondeduced_p, \
9437 if (result) goto out; \
9441 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9445 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9446 WALK_SUBTREE (TYPE_CONTEXT (t
));
9448 switch (TREE_CODE (t
))
9451 if (TYPE_PTRMEMFUNC_P (t
))
9457 if (!TYPE_TEMPLATE_INFO (t
))
9460 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9464 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9465 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9469 /* Since we're not going to walk subtrees, we have to do this
9471 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9475 /* Check the return type. */
9476 WALK_SUBTREE (TREE_TYPE (t
));
9478 /* Check the parameter types. Since default arguments are not
9479 instantiated until they are needed, the TYPE_ARG_TYPES may
9480 contain expressions that involve template parameters. But,
9481 no-one should be looking at them yet. And, once they're
9482 instantiated, they don't contain template parameters, so
9483 there's no point in looking at them then, either. */
9487 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9488 WALK_SUBTREE (TREE_VALUE (parm
));
9490 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9491 want walk_tree walking into them itself. */
9495 if (flag_noexcept_type
)
9497 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9499 WALK_SUBTREE (TREE_PURPOSE (spec
));
9504 case UNDERLYING_TYPE
:
9505 if (pfd
->include_nondeduced_p
9506 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9508 pfd
->include_nondeduced_p
,
9510 return error_mark_node
;
9515 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9516 WALK_SUBTREE (DECL_TI_ARGS (t
));
9521 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9522 WALK_SUBTREE (DECL_INITIAL (t
));
9523 if (DECL_CONTEXT (t
)
9524 && pfd
->include_nondeduced_p
)
9525 WALK_SUBTREE (DECL_CONTEXT (t
));
9528 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9529 /* Record template parameters such as `T' inside `TT<T>'. */
9530 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9533 case TEMPLATE_TEMPLATE_PARM
:
9534 case TEMPLATE_TYPE_PARM
:
9535 case TEMPLATE_PARM_INDEX
:
9536 if (fn
&& (*fn
)(t
, data
))
9543 /* A template template parameter is encountered. */
9544 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9545 WALK_SUBTREE (TREE_TYPE (t
));
9547 /* Already substituted template template parameter */
9552 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9553 partial instantiation. */
9554 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9558 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9559 && pfd
->include_nondeduced_p
)
9560 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9565 /* If there's no type, then this thing must be some expression
9566 involving template parameters. */
9567 if (!fn
&& !TREE_TYPE (t
))
9568 return error_mark_node
;
9573 case IMPLICIT_CONV_EXPR
:
9574 case REINTERPRET_CAST_EXPR
:
9575 case CONST_CAST_EXPR
:
9576 case STATIC_CAST_EXPR
:
9577 case DYNAMIC_CAST_EXPR
:
9581 case PSEUDO_DTOR_EXPR
:
9583 return error_mark_node
;
9592 /* We didn't find any template parameters we liked. */
9597 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9598 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9599 call FN with the parameter and the DATA.
9600 If FN returns nonzero, the iteration is terminated, and
9601 for_each_template_parm returns 1. Otherwise, the iteration
9602 continues. If FN never returns a nonzero value, the value
9603 returned by for_each_template_parm is 0. If FN is NULL, it is
9604 considered to be the function which always returns 1.
9606 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9607 parameters that occur in non-deduced contexts. When false, only
9608 visits those template parameters that can be deduced. */
9611 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9612 hash_set
<tree
> *visited
,
9613 bool include_nondeduced_p
,
9616 struct pair_fn_data pfd
;
9621 pfd
.any_fn
= any_fn
;
9623 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9625 /* Walk the tree. (Conceptually, we would like to walk without
9626 duplicates, but for_each_template_parm_r recursively calls
9627 for_each_template_parm, so we would need to reorganize a fair
9628 bit to use walk_tree_without_duplicates, so we keep our own
9631 pfd
.visited
= visited
;
9633 pfd
.visited
= new hash_set
<tree
>;
9634 result
= cp_walk_tree (&t
,
9635 for_each_template_parm_r
,
9649 /* Returns true if T depends on any template parameter. */
9652 uses_template_parms (tree t
)
9658 int saved_processing_template_decl
;
9660 saved_processing_template_decl
= processing_template_decl
;
9661 if (!saved_processing_template_decl
)
9662 processing_template_decl
= 1;
9664 dependent_p
= dependent_type_p (t
);
9665 else if (TREE_CODE (t
) == TREE_VEC
)
9666 dependent_p
= any_dependent_template_arguments_p (t
);
9667 else if (TREE_CODE (t
) == TREE_LIST
)
9668 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9669 || uses_template_parms (TREE_CHAIN (t
)));
9670 else if (TREE_CODE (t
) == TYPE_DECL
)
9671 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9674 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9675 || TREE_CODE (t
) == OVERLOAD
9678 || TREE_CODE (t
) == TRAIT_EXPR
9679 || TREE_CODE (t
) == CONSTRUCTOR
9680 || CONSTANT_CLASS_P (t
))
9681 dependent_p
= (type_dependent_expression_p (t
)
9682 || value_dependent_expression_p (t
));
9685 gcc_assert (t
== error_mark_node
);
9686 dependent_p
= false;
9689 processing_template_decl
= saved_processing_template_decl
;
9694 /* Returns true iff current_function_decl is an incompletely instantiated
9695 template. Useful instead of processing_template_decl because the latter
9696 is set to 0 during instantiate_non_dependent_expr. */
9699 in_template_function (void)
9701 tree fn
= current_function_decl
;
9703 ++processing_template_decl
;
9704 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9705 && DECL_TEMPLATE_INFO (fn
)
9706 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9707 --processing_template_decl
;
9711 /* Returns true if T depends on any template parameter with level LEVEL. */
9714 uses_template_parms_level (tree t
, int level
)
9716 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9717 /*include_nondeduced_p=*/true);
9720 /* Returns true if the signature of DECL depends on any template parameter from
9721 its enclosing class. */
9724 uses_outer_template_parms (tree decl
)
9726 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9729 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9730 &depth
, NULL
, /*include_nondeduced_p=*/true))
9732 if (PRIMARY_TEMPLATE_P (decl
)
9733 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9734 (DECL_TEMPLATE_PARMS (decl
)),
9735 template_parm_outer_level
,
9736 &depth
, NULL
, /*include_nondeduced_p=*/true))
9738 tree ci
= get_constraints (decl
);
9740 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9741 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9742 &depth
, NULL
, /*nondeduced*/true))
9747 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9748 ill-formed translation unit, i.e. a variable or function that isn't
9749 usable in a constant expression. */
9752 neglectable_inst_p (tree d
)
9755 && !undeduced_auto_decl (d
)
9756 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9757 : decl_maybe_constant_var_p (d
)));
9760 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9761 neglectable and instantiated from within an erroneous instantiation. */
9764 limit_bad_template_recursion (tree decl
)
9766 struct tinst_level
*lev
= current_tinst_level
;
9767 int errs
= errorcount
+ sorrycount
;
9768 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9771 for (; lev
; lev
= lev
->next
)
9772 if (neglectable_inst_p (lev
->decl
))
9775 return (lev
&& errs
> lev
->errors
);
9778 static int tinst_depth
;
9779 extern int max_tinst_depth
;
9782 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9784 /* We're starting to instantiate D; record the template instantiation context
9785 for diagnostics and to restore it later. */
9788 push_tinst_level (tree d
)
9790 return push_tinst_level_loc (d
, input_location
);
9793 /* We're starting to instantiate D; record the template instantiation context
9794 at LOC for diagnostics and to restore it later. */
9797 push_tinst_level_loc (tree d
, location_t loc
)
9799 struct tinst_level
*new_level
;
9801 if (tinst_depth
>= max_tinst_depth
)
9803 /* Tell error.c not to try to instantiate any templates. */
9805 fatal_error (input_location
,
9806 "template instantiation depth exceeds maximum of %d"
9807 " (use -ftemplate-depth= to increase the maximum)",
9812 /* If the current instantiation caused problems, don't let it instantiate
9813 anything else. Do allow deduction substitution and decls usable in
9814 constant expressions. */
9815 if (limit_bad_template_recursion (d
))
9818 /* When not -quiet, dump template instantiations other than functions, since
9819 announce_function will take care of those. */
9821 && TREE_CODE (d
) != TREE_LIST
9822 && TREE_CODE (d
) != FUNCTION_DECL
)
9823 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9825 new_level
= ggc_alloc
<tinst_level
> ();
9826 new_level
->decl
= d
;
9827 new_level
->locus
= loc
;
9828 new_level
->errors
= errorcount
+sorrycount
;
9829 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9830 new_level
->next
= current_tinst_level
;
9831 current_tinst_level
= new_level
;
9834 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9835 depth_reached
= tinst_depth
;
9840 /* We're done instantiating this template; return to the instantiation
9844 pop_tinst_level (void)
9846 /* Restore the filename and line number stashed away when we started
9847 this instantiation. */
9848 input_location
= current_tinst_level
->locus
;
9849 current_tinst_level
= current_tinst_level
->next
;
9853 /* We're instantiating a deferred template; restore the template
9854 instantiation context in which the instantiation was requested, which
9855 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9858 reopen_tinst_level (struct tinst_level
*level
)
9860 struct tinst_level
*t
;
9863 for (t
= level
; t
; t
= t
->next
)
9866 current_tinst_level
= level
;
9868 if (current_tinst_level
)
9869 current_tinst_level
->errors
= errorcount
+sorrycount
;
9873 /* Returns the TINST_LEVEL which gives the original instantiation
9876 struct tinst_level
*
9877 outermost_tinst_level (void)
9879 struct tinst_level
*level
= current_tinst_level
;
9882 level
= level
->next
;
9886 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9887 vector of template arguments, as for tsubst.
9889 Returns an appropriate tsubst'd friend declaration. */
9892 tsubst_friend_function (tree decl
, tree args
)
9896 if (TREE_CODE (decl
) == FUNCTION_DECL
9897 && DECL_TEMPLATE_INSTANTIATION (decl
)
9898 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9899 /* This was a friend declared with an explicit template
9900 argument list, e.g.:
9904 to indicate that f was a template instantiation, not a new
9905 function declaration. Now, we have to figure out what
9906 instantiation of what template. */
9908 tree template_id
, arglist
, fns
;
9911 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9913 /* Friend functions are looked up in the containing namespace scope.
9914 We must enter that scope, to avoid finding member functions of the
9915 current class with same name. */
9916 push_nested_namespace (ns
);
9917 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9918 tf_warning_or_error
, NULL_TREE
,
9919 /*integral_constant_expression_p=*/false);
9920 pop_nested_namespace (ns
);
9921 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9922 tf_warning_or_error
, NULL_TREE
);
9923 template_id
= lookup_template_function (fns
, arglist
);
9925 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9926 tmpl
= determine_specialization (template_id
, new_friend
,
9928 /*need_member_template=*/0,
9929 TREE_VEC_LENGTH (args
),
9931 return instantiate_template (tmpl
, new_args
, tf_error
);
9934 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9936 /* The NEW_FRIEND will look like an instantiation, to the
9937 compiler, but is not an instantiation from the point of view of
9938 the language. For example, we might have had:
9940 template <class T> struct S {
9941 template <class U> friend void f(T, U);
9944 Then, in S<int>, template <class U> void f(int, U) is not an
9945 instantiation of anything. */
9946 if (new_friend
== error_mark_node
)
9947 return error_mark_node
;
9949 DECL_USE_TEMPLATE (new_friend
) = 0;
9950 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9952 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9953 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9954 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9957 /* The mangled name for the NEW_FRIEND is incorrect. The function
9958 is not a template instantiation and should not be mangled like
9959 one. Therefore, we forget the mangling here; we'll recompute it
9960 later if we need it. */
9961 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9963 SET_DECL_RTL (new_friend
, NULL
);
9964 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9967 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9970 tree new_friend_template_info
;
9971 tree new_friend_result_template_info
;
9973 int new_friend_is_defn
;
9975 /* We must save some information from NEW_FRIEND before calling
9976 duplicate decls since that function will free NEW_FRIEND if
9978 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9979 new_friend_is_defn
=
9980 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9981 (template_for_substitution (new_friend
)))
9983 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9985 /* This declaration is a `primary' template. */
9986 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9988 new_friend_result_template_info
9989 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9992 new_friend_result_template_info
= NULL_TREE
;
9994 /* Inside pushdecl_namespace_level, we will push into the
9995 current namespace. However, the friend function should go
9996 into the namespace of the template. */
9997 ns
= decl_namespace_context (new_friend
);
9998 push_nested_namespace (ns
);
9999 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10000 pop_nested_namespace (ns
);
10002 if (old_decl
== error_mark_node
)
10003 return error_mark_node
;
10005 if (old_decl
!= new_friend
)
10007 /* This new friend declaration matched an existing
10008 declaration. For example, given:
10010 template <class T> void f(T);
10011 template <class U> class C {
10012 template <class T> friend void f(T) {}
10015 the friend declaration actually provides the definition
10016 of `f', once C has been instantiated for some type. So,
10017 old_decl will be the out-of-class template declaration,
10018 while new_friend is the in-class definition.
10020 But, if `f' was called before this point, the
10021 instantiation of `f' will have DECL_TI_ARGS corresponding
10022 to `T' but not to `U', references to which might appear
10023 in the definition of `f'. Previously, the most general
10024 template for an instantiation of `f' was the out-of-class
10025 version; now it is the in-class version. Therefore, we
10026 run through all specialization of `f', adding to their
10027 DECL_TI_ARGS appropriately. In particular, they need a
10028 new set of outer arguments, corresponding to the
10029 arguments for this class instantiation.
10031 The same situation can arise with something like this:
10033 friend void f(int);
10034 template <class T> class C {
10035 friend void f(T) {}
10038 when `C<int>' is instantiated. Now, `f(int)' is defined
10041 if (!new_friend_is_defn
)
10042 /* On the other hand, if the in-class declaration does
10043 *not* provide a definition, then we don't want to alter
10044 existing definitions. We can just leave everything
10049 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10050 tree new_args
= TI_ARGS (new_friend_template_info
);
10052 /* Overwrite whatever template info was there before, if
10053 any, with the new template information pertaining to
10054 the declaration. */
10055 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10057 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10059 /* We should have called reregister_specialization in
10060 duplicate_decls. */
10061 gcc_assert (retrieve_specialization (new_template
,
10065 /* Instantiate it if the global has already been used. */
10066 if (DECL_ODR_USED (old_decl
))
10067 instantiate_decl (old_decl
, /*defer_ok=*/true,
10068 /*expl_inst_class_mem_p=*/false);
10074 /* Indicate that the old function template is a partial
10076 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10077 = new_friend_result_template_info
;
10079 gcc_assert (new_template
10080 == most_general_template (new_template
));
10081 gcc_assert (new_template
!= old_decl
);
10083 /* Reassign any specializations already in the hash table
10084 to the new more general template, and add the
10085 additional template args. */
10086 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10088 t
= TREE_CHAIN (t
))
10090 tree spec
= TREE_VALUE (t
);
10093 elt
.tmpl
= old_decl
;
10094 elt
.args
= DECL_TI_ARGS (spec
);
10095 elt
.spec
= NULL_TREE
;
10097 decl_specializations
->remove_elt (&elt
);
10099 DECL_TI_ARGS (spec
)
10100 = add_outermost_template_args (new_args
,
10101 DECL_TI_ARGS (spec
));
10103 register_specialization
10104 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10107 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10111 /* The information from NEW_FRIEND has been merged into OLD_DECL
10112 by duplicate_decls. */
10113 new_friend
= old_decl
;
10118 tree context
= DECL_CONTEXT (new_friend
);
10122 template <class T> class C {
10123 template <class U> friend void C1<U>::f (); // case 1
10124 friend void C2<T>::f (); // case 2
10126 we only need to make sure CONTEXT is a complete type for
10127 case 2. To distinguish between the two cases, we note that
10128 CONTEXT of case 1 remains dependent type after tsubst while
10129 this isn't true for case 2. */
10130 ++processing_template_decl
;
10131 dependent_p
= dependent_type_p (context
);
10132 --processing_template_decl
;
10135 && !complete_type_or_else (context
, NULL_TREE
))
10136 return error_mark_node
;
10138 if (COMPLETE_TYPE_P (context
))
10140 tree fn
= new_friend
;
10141 /* do_friend adds the TEMPLATE_DECL for any member friend
10142 template even if it isn't a member template, i.e.
10143 template <class T> friend A<T>::f();
10144 Look through it in that case. */
10145 if (TREE_CODE (fn
) == TEMPLATE_DECL
10146 && !PRIMARY_TEMPLATE_P (fn
))
10147 fn
= DECL_TEMPLATE_RESULT (fn
);
10148 /* Check to see that the declaration is really present, and,
10149 possibly obtain an improved declaration. */
10150 fn
= check_classfn (context
, fn
, NULL_TREE
);
10160 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10161 template arguments, as for tsubst.
10163 Returns an appropriate tsubst'd friend type or error_mark_node on
10167 tsubst_friend_class (tree friend_tmpl
, tree args
)
10171 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10173 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10174 return TREE_TYPE (tmpl
);
10177 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10178 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10179 push_nested_namespace (context
);
10181 push_nested_class (context
);
10183 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10184 /*non_class=*/false, /*block_p=*/false,
10185 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10187 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10189 /* The friend template has already been declared. Just
10190 check to see that the declarations match, and install any new
10191 default parameters. We must tsubst the default parameters,
10192 of course. We only need the innermost template parameters
10193 because that is all that redeclare_class_template will look
10195 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10196 > TMPL_ARGS_DEPTH (args
))
10198 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10199 args
, tf_warning_or_error
);
10200 location_t saved_input_location
= input_location
;
10201 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10202 tree cons
= get_constraints (tmpl
);
10203 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10204 input_location
= saved_input_location
;
10209 /* The friend template has not already been declared. In this
10210 case, the instantiation of the template class will cause the
10211 injection of this template into the namespace scope. */
10212 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10214 if (tmpl
!= error_mark_node
)
10216 /* The new TMPL is not an instantiation of anything, so we
10217 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10218 for the new type because that is supposed to be the
10219 corresponding template decl, i.e., TMPL. */
10220 DECL_USE_TEMPLATE (tmpl
) = 0;
10221 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10222 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10223 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10224 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10226 /* It is hidden. */
10227 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10228 DECL_ANTICIPATED (tmpl
)
10229 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10231 /* Inject this template into the enclosing namspace scope. */
10232 tmpl
= pushdecl_namespace_level (tmpl
, true);
10236 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10237 pop_nested_namespace (context
);
10239 pop_nested_class ();
10241 return TREE_TYPE (tmpl
);
10244 /* Returns zero if TYPE cannot be completed later due to circularity.
10245 Otherwise returns one. */
10248 can_complete_type_without_circularity (tree type
)
10250 if (type
== NULL_TREE
|| type
== error_mark_node
)
10252 else if (COMPLETE_TYPE_P (type
))
10254 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10255 return can_complete_type_without_circularity (TREE_TYPE (type
));
10256 else if (CLASS_TYPE_P (type
)
10257 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10263 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10264 tsubst_flags_t
, tree
);
10266 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10267 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10270 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10271 tsubst_flags_t complain
, tree in_decl
)
10273 gcc_assert (ATTR_IS_DEPENDENT (t
));
10275 tree val
= TREE_VALUE (t
);
10276 if (val
== NULL_TREE
)
10277 /* Nothing to do. */;
10278 else if ((flag_openmp
|| flag_openmp_simd
)
10279 && is_attribute_p ("omp declare simd",
10280 get_attribute_name (t
)))
10282 tree clauses
= TREE_VALUE (val
);
10283 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10284 complain
, in_decl
);
10285 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10286 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10287 tree parms
= DECL_ARGUMENTS (*decl_p
);
10289 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10291 val
= build_tree_list (NULL_TREE
, clauses
);
10295 /* If the first attribute argument is an identifier, don't
10296 pass it through tsubst. Attributes like mode, format,
10297 cleanup and several target specific attributes expect it
10299 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10302 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10303 /*integral_constant_expression_p=*/false);
10304 if (chain
!= TREE_CHAIN (val
))
10305 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10307 else if (PACK_EXPANSION_P (val
))
10309 /* An attribute pack expansion. */
10310 tree purp
= TREE_PURPOSE (t
);
10311 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10312 if (pack
== error_mark_node
)
10313 return error_mark_node
;
10314 int len
= TREE_VEC_LENGTH (pack
);
10315 tree list
= NULL_TREE
;
10317 for (int i
= 0; i
< len
; ++i
)
10319 tree elt
= TREE_VEC_ELT (pack
, i
);
10320 *q
= build_tree_list (purp
, elt
);
10321 q
= &TREE_CHAIN (*q
);
10326 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10327 /*integral_constant_expression_p=*/false);
10329 if (val
!= TREE_VALUE (t
))
10330 return build_tree_list (TREE_PURPOSE (t
), val
);
10334 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10335 unchanged or a new TREE_LIST chain. */
10338 tsubst_attributes (tree attributes
, tree args
,
10339 tsubst_flags_t complain
, tree in_decl
)
10341 tree last_dep
= NULL_TREE
;
10343 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10344 if (ATTR_IS_DEPENDENT (t
))
10347 attributes
= copy_list (attributes
);
10352 for (tree
*p
= &attributes
; *p
; )
10355 if (ATTR_IS_DEPENDENT (t
))
10357 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10362 p
= &TREE_CHAIN (*p
);
10363 *p
= TREE_CHAIN (t
);
10367 p
= &TREE_CHAIN (*p
);
10373 /* Apply any attributes which had to be deferred until instantiation
10374 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10375 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10378 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10379 tree args
, tsubst_flags_t complain
, tree in_decl
)
10381 tree last_dep
= NULL_TREE
;
10385 if (attributes
== NULL_TREE
)
10388 if (DECL_P (*decl_p
))
10390 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10392 p
= &DECL_ATTRIBUTES (*decl_p
);
10393 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10394 to our attributes parameter. */
10395 gcc_assert (*p
== attributes
);
10399 p
= &TYPE_ATTRIBUTES (*decl_p
);
10400 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10401 lookup_template_class_1, and should be preserved. */
10402 gcc_assert (*p
!= attributes
);
10404 p
= &TREE_CHAIN (*p
);
10407 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10408 if (ATTR_IS_DEPENDENT (t
))
10411 attributes
= copy_list (attributes
);
10418 tree late_attrs
= NULL_TREE
;
10419 tree
*q
= &late_attrs
;
10424 if (ATTR_IS_DEPENDENT (t
))
10426 *p
= TREE_CHAIN (t
);
10427 TREE_CHAIN (t
) = NULL_TREE
;
10428 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10430 q
= &TREE_CHAIN (*q
);
10433 p
= &TREE_CHAIN (t
);
10436 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10440 /* Perform (or defer) access check for typedefs that were referenced
10441 from within the template TMPL code.
10442 This is a subroutine of instantiate_decl and instantiate_class_template.
10443 TMPL is the template to consider and TARGS is the list of arguments of
10447 perform_typedefs_access_check (tree tmpl
, tree targs
)
10449 location_t saved_location
;
10451 qualified_typedef_usage_t
*iter
;
10454 || (!CLASS_TYPE_P (tmpl
)
10455 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10458 saved_location
= input_location
;
10459 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10461 tree type_decl
= iter
->typedef_decl
;
10462 tree type_scope
= iter
->context
;
10464 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10467 if (uses_template_parms (type_decl
))
10468 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10469 if (uses_template_parms (type_scope
))
10470 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10472 /* Make access check error messages point to the location
10473 of the use of the typedef. */
10474 input_location
= iter
->locus
;
10475 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10476 type_decl
, type_decl
,
10477 tf_warning_or_error
);
10479 input_location
= saved_location
;
10483 instantiate_class_template_1 (tree type
)
10485 tree templ
, args
, pattern
, t
, member
;
10489 unsigned int saved_maximum_field_alignment
;
10492 if (type
== error_mark_node
)
10493 return error_mark_node
;
10495 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10496 || uses_template_parms (type
))
10499 /* Figure out which template is being instantiated. */
10500 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10501 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10503 /* Mark the type as in the process of being defined. */
10504 TYPE_BEING_DEFINED (type
) = 1;
10506 /* Determine what specialization of the original template to
10508 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10509 if (t
== error_mark_node
)
10510 return error_mark_node
;
10513 /* This TYPE is actually an instantiation of a partial
10514 specialization. We replace the innermost set of ARGS with
10515 the arguments appropriate for substitution. For example,
10518 template <class T> struct S {};
10519 template <class T> struct S<T*> {};
10521 and supposing that we are instantiating S<int*>, ARGS will
10522 presently be {int*} -- but we need {int}. */
10523 pattern
= TREE_TYPE (t
);
10524 args
= TREE_PURPOSE (t
);
10528 pattern
= TREE_TYPE (templ
);
10529 args
= CLASSTYPE_TI_ARGS (type
);
10532 /* If the template we're instantiating is incomplete, then clearly
10533 there's nothing we can do. */
10534 if (!COMPLETE_TYPE_P (pattern
))
10536 /* We can try again later. */
10537 TYPE_BEING_DEFINED (type
) = 0;
10541 /* If we've recursively instantiated too many templates, stop. */
10542 if (! push_tinst_level (type
))
10545 /* We may be in the middle of deferred access check. Disable
10547 push_deferring_access_checks (dk_no_deferred
);
10549 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10550 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10552 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10553 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10554 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10555 fn_context
= error_mark_node
;
10557 push_to_top_level ();
10560 cp_unevaluated_operand
= 0;
10561 c_inhibit_evaluation_warnings
= 0;
10563 /* Use #pragma pack from the template context. */
10564 saved_maximum_field_alignment
= maximum_field_alignment
;
10565 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10567 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10569 /* Set the input location to the most specialized template definition.
10570 This is needed if tsubsting causes an error. */
10571 typedecl
= TYPE_MAIN_DECL (pattern
);
10572 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10573 DECL_SOURCE_LOCATION (typedecl
);
10575 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10576 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10577 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10578 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10579 if (ANON_AGGR_TYPE_P (pattern
))
10580 SET_ANON_AGGR_TYPE_P (type
);
10581 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10583 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10584 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10585 /* Adjust visibility for template arguments. */
10586 determine_visibility (TYPE_MAIN_DECL (type
));
10588 if (CLASS_TYPE_P (type
))
10589 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10591 pbinfo
= TYPE_BINFO (pattern
);
10593 /* We should never instantiate a nested class before its enclosing
10594 class; we need to look up the nested class by name before we can
10595 instantiate it, and that lookup should instantiate the enclosing
10597 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10598 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10600 base_list
= NULL_TREE
;
10601 if (BINFO_N_BASE_BINFOS (pbinfo
))
10607 /* We must enter the scope containing the type, as that is where
10608 the accessibility of types named in dependent bases are
10610 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10612 /* Substitute into each of the bases to determine the actual
10614 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10617 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10618 tree expanded_bases
= NULL_TREE
;
10621 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10624 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10625 args
, tf_error
, NULL_TREE
);
10626 if (expanded_bases
== error_mark_node
)
10629 len
= TREE_VEC_LENGTH (expanded_bases
);
10632 for (idx
= 0; idx
< len
; idx
++)
10634 if (expanded_bases
)
10635 /* Extract the already-expanded base class. */
10636 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10638 /* Substitute to figure out the base class. */
10639 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10642 if (base
== error_mark_node
)
10645 base_list
= tree_cons (access
, base
, base_list
);
10646 if (BINFO_VIRTUAL_P (pbase_binfo
))
10647 TREE_TYPE (base_list
) = integer_type_node
;
10651 /* The list is now in reverse order; correct that. */
10652 base_list
= nreverse (base_list
);
10655 pop_scope (pushed_scope
);
10657 /* Now call xref_basetypes to set up all the base-class
10659 xref_basetypes (type
, base_list
);
10661 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10662 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10663 args
, tf_error
, NULL_TREE
);
10664 fixup_attribute_variants (type
);
10666 /* Now that our base classes are set up, enter the scope of the
10667 class, so that name lookups into base classes, etc. will work
10668 correctly. This is precisely analogous to what we do in
10669 begin_class_definition when defining an ordinary non-template
10670 class, except we also need to push the enclosing classes. */
10671 push_nested_class (type
);
10673 /* Now members are processed in the order of declaration. */
10674 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10675 member
; member
= TREE_CHAIN (member
))
10677 tree t
= TREE_VALUE (member
);
10679 if (TREE_PURPOSE (member
))
10683 if (LAMBDA_TYPE_P (t
))
10684 /* A closure type for a lambda in an NSDMI or default argument.
10685 Ignore it; it will be regenerated when needed. */
10688 /* Build new CLASSTYPE_NESTED_UTDS. */
10691 bool class_template_p
;
10693 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10694 && TYPE_LANG_SPECIFIC (t
)
10695 && CLASSTYPE_IS_TEMPLATE (t
));
10696 /* If the member is a class template, then -- even after
10697 substitution -- there may be dependent types in the
10698 template argument list for the class. We increment
10699 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10700 that function will assume that no types are dependent
10701 when outside of a template. */
10702 if (class_template_p
)
10703 ++processing_template_decl
;
10704 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10705 if (class_template_p
)
10706 --processing_template_decl
;
10707 if (newtag
== error_mark_node
)
10710 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10712 tree name
= TYPE_IDENTIFIER (t
);
10714 if (class_template_p
)
10715 /* Unfortunately, lookup_template_class sets
10716 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10717 instantiation (i.e., for the type of a member
10718 template class nested within a template class.)
10719 This behavior is required for
10720 maybe_process_partial_specialization to work
10721 correctly, but is not accurate in this case;
10722 the TAG is not an instantiation of anything.
10723 (The corresponding TEMPLATE_DECL is an
10724 instantiation, but the TYPE is not.) */
10725 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10727 /* Now, we call pushtag to put this NEWTAG into the scope of
10728 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10729 pushtag calling push_template_decl. We don't have to do
10730 this for enums because it will already have been done in
10733 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10734 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10737 else if (DECL_DECLARES_FUNCTION_P (t
))
10741 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10742 ++processing_template_decl
;
10743 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10744 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10745 --processing_template_decl
;
10746 set_current_access_from_decl (r
);
10747 finish_member_declaration (r
);
10748 /* Instantiate members marked with attribute used. */
10749 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10751 if (TREE_CODE (r
) == FUNCTION_DECL
10752 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10753 cp_check_omp_declare_reduction (r
);
10755 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
10756 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10757 /* A closure type for a lambda in an NSDMI or default argument.
10758 Ignore it; it will be regenerated when needed. */;
10761 /* Build new TYPE_FIELDS. */
10762 if (TREE_CODE (t
) == STATIC_ASSERT
)
10766 ++c_inhibit_evaluation_warnings
;
10768 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10769 tf_warning_or_error
, NULL_TREE
,
10770 /*integral_constant_expression_p=*/true);
10771 --c_inhibit_evaluation_warnings
;
10773 finish_static_assert (condition
,
10774 STATIC_ASSERT_MESSAGE (t
),
10775 STATIC_ASSERT_SOURCE_LOCATION (t
),
10776 /*member_p=*/true);
10778 else if (TREE_CODE (t
) != CONST_DECL
)
10781 tree vec
= NULL_TREE
;
10784 /* The file and line for this declaration, to
10785 assist in error message reporting. Since we
10786 called push_tinst_level above, we don't need to
10788 input_location
= DECL_SOURCE_LOCATION (t
);
10790 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10791 ++processing_template_decl
;
10792 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10793 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10794 --processing_template_decl
;
10796 if (TREE_CODE (r
) == TREE_VEC
)
10798 /* A capture pack became multiple fields. */
10800 len
= TREE_VEC_LENGTH (vec
);
10803 for (int i
= 0; i
< len
; ++i
)
10806 r
= TREE_VEC_ELT (vec
, i
);
10811 [t]he initialization (and any associated
10812 side-effects) of a static data member does
10813 not occur unless the static data member is
10814 itself used in a way that requires the
10815 definition of the static data member to
10818 Therefore, we do not substitute into the
10819 initialized for the static data member here. */
10820 finish_static_data_member_decl
10822 /*init=*/NULL_TREE
,
10823 /*init_const_expr_p=*/false,
10824 /*asmspec_tree=*/NULL_TREE
,
10826 /* Instantiate members marked with attribute used. */
10827 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10830 else if (TREE_CODE (r
) == FIELD_DECL
)
10832 /* Determine whether R has a valid type and can be
10833 completed later. If R is invalid, then its type
10834 is replaced by error_mark_node. */
10835 tree rtype
= TREE_TYPE (r
);
10836 if (can_complete_type_without_circularity (rtype
))
10837 complete_type (rtype
);
10839 if (!complete_or_array_type_p (rtype
))
10841 /* If R's type couldn't be completed and
10842 it isn't a flexible array member (whose
10843 type is incomplete by definition) give
10845 cxx_incomplete_type_error (r
, rtype
);
10846 TREE_TYPE (r
) = error_mark_node
;
10850 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10851 such a thing will already have been added to the field
10852 list by tsubst_enum in finish_member_declaration in the
10853 CLASSTYPE_NESTED_UTDS case above. */
10854 if (!(TREE_CODE (r
) == TYPE_DECL
10855 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10856 && DECL_ARTIFICIAL (r
)))
10858 set_current_access_from_decl (r
);
10859 finish_member_declaration (r
);
10867 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10868 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10870 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10872 tree friend_type
= t
;
10873 bool adjust_processing_template_decl
= false;
10875 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10877 /* template <class T> friend class C; */
10878 friend_type
= tsubst_friend_class (friend_type
, args
);
10879 adjust_processing_template_decl
= true;
10881 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10883 /* template <class T> friend class C::D; */
10884 friend_type
= tsubst (friend_type
, args
,
10885 tf_warning_or_error
, NULL_TREE
);
10886 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10887 friend_type
= TREE_TYPE (friend_type
);
10888 adjust_processing_template_decl
= true;
10890 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10891 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10893 /* This could be either
10897 when dependent_type_p is false or
10899 template <class U> friend class T::C;
10902 /* Bump processing_template_decl in case this is something like
10903 template <class T> friend struct A<T>::B. */
10904 ++processing_template_decl
;
10905 friend_type
= tsubst (friend_type
, args
,
10906 tf_warning_or_error
, NULL_TREE
);
10907 if (dependent_type_p (friend_type
))
10908 adjust_processing_template_decl
= true;
10909 --processing_template_decl
;
10911 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10912 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10913 && TYPE_HIDDEN_P (friend_type
))
10917 where C hasn't been declared yet. Let's lookup name
10918 from namespace scope directly, bypassing any name that
10919 come from dependent base class. */
10920 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10922 /* The call to xref_tag_from_type does injection for friend
10924 push_nested_namespace (ns
);
10926 xref_tag_from_type (friend_type
, NULL_TREE
,
10927 /*tag_scope=*/ts_current
);
10928 pop_nested_namespace (ns
);
10930 else if (uses_template_parms (friend_type
))
10931 /* friend class C<T>; */
10932 friend_type
= tsubst (friend_type
, args
,
10933 tf_warning_or_error
, NULL_TREE
);
10938 where C is already declared or
10940 friend class C<int>;
10942 We don't have to do anything in these cases. */
10944 if (adjust_processing_template_decl
)
10945 /* Trick make_friend_class into realizing that the friend
10946 we're adding is a template, not an ordinary class. It's
10947 important that we use make_friend_class since it will
10948 perform some error-checking and output cross-reference
10950 ++processing_template_decl
;
10952 if (friend_type
!= error_mark_node
)
10953 make_friend_class (type
, friend_type
, /*complain=*/false);
10955 if (adjust_processing_template_decl
)
10956 --processing_template_decl
;
10960 /* Build new DECL_FRIENDLIST. */
10963 /* The file and line for this declaration, to
10964 assist in error message reporting. Since we
10965 called push_tinst_level above, we don't need to
10967 input_location
= DECL_SOURCE_LOCATION (t
);
10969 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10971 ++processing_template_decl
;
10972 push_deferring_access_checks (dk_no_check
);
10975 r
= tsubst_friend_function (t
, args
);
10976 add_friend (type
, r
, /*complain=*/false);
10977 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10979 pop_deferring_access_checks ();
10980 --processing_template_decl
;
10988 /* Restore these before substituting into the lambda capture
10990 cp_unevaluated_operand
= saved_unevaluated_operand
;
10991 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10994 /* Set the file and line number information to whatever is given for
10995 the class itself. This puts error messages involving generated
10996 implicit functions at a predictable point, and the same point
10997 that would be used for non-template classes. */
10998 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11000 unreverse_member_declarations (type
);
11001 finish_struct_1 (type
);
11002 TYPE_BEING_DEFINED (type
) = 0;
11004 /* We don't instantiate default arguments for member functions. 14.7.1:
11006 The implicit instantiation of a class template specialization causes
11007 the implicit instantiation of the declarations, but not of the
11008 definitions or default arguments, of the class member functions,
11009 member classes, static data members and member templates.... */
11011 /* Some typedefs referenced from within the template code need to be access
11012 checked at template instantiation time, i.e now. These types were
11013 added to the template at parsing time. Let's get those and perform
11014 the access checks then. */
11015 perform_typedefs_access_check (pattern
, args
);
11016 perform_deferred_access_checks (tf_warning_or_error
);
11017 pop_nested_class ();
11018 maximum_field_alignment
= saved_maximum_field_alignment
;
11020 pop_from_top_level ();
11021 pop_deferring_access_checks ();
11022 pop_tinst_level ();
11024 /* The vtable for a template class can be emitted in any translation
11025 unit in which the class is instantiated. When there is no key
11026 method, however, finish_struct_1 will already have added TYPE to
11027 the keyed_classes. */
11028 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11029 vec_safe_push (keyed_classes
, type
);
11034 /* Wrapper for instantiate_class_template_1. */
11037 instantiate_class_template (tree type
)
11040 timevar_push (TV_TEMPLATE_INST
);
11041 ret
= instantiate_class_template_1 (type
);
11042 timevar_pop (TV_TEMPLATE_INST
);
11047 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11053 else if (TYPE_P (t
))
11054 r
= tsubst (t
, args
, complain
, in_decl
);
11057 if (!(complain
& tf_warning
))
11058 ++c_inhibit_evaluation_warnings
;
11059 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11060 /*integral_constant_expression_p=*/true);
11061 if (!(complain
& tf_warning
))
11062 --c_inhibit_evaluation_warnings
;
11067 /* Given a function parameter pack TMPL_PARM and some function parameters
11068 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11069 and set *SPEC_P to point at the next point in the list. */
11072 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11074 /* Collect all of the extra "packed" parameters into an
11077 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11078 tree spec_parm
= *spec_p
;
11081 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11083 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11086 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11087 parmvec
= make_tree_vec (len
);
11088 spec_parm
= *spec_p
;
11089 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11091 tree elt
= spec_parm
;
11092 if (DECL_PACK_P (elt
))
11093 elt
= make_pack_expansion (elt
);
11094 TREE_VEC_ELT (parmvec
, i
) = elt
;
11097 /* Build the argument packs. */
11098 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11099 *spec_p
= spec_parm
;
11104 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11105 NONTYPE_ARGUMENT_PACK. */
11108 make_fnparm_pack (tree spec_parm
)
11110 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11113 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11114 pack expansion with no extra args, 2 if it has extra args, or 0
11115 if it is not a pack expansion. */
11118 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11120 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11121 if (i
>= TREE_VEC_LENGTH (vec
))
11123 tree elt
= TREE_VEC_ELT (vec
, i
);
11125 /* A decl pack is itself an expansion. */
11126 elt
= TREE_TYPE (elt
);
11127 if (!PACK_EXPANSION_P (elt
))
11129 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11135 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11138 make_argument_pack_select (tree arg_pack
, unsigned index
)
11140 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11142 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11143 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11148 /* This is a subroutine of tsubst_pack_expansion.
11150 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11151 mechanism to store the (non complete list of) arguments of the
11152 substitution and return a non substituted pack expansion, in order
11153 to wait for when we have enough arguments to really perform the
11157 use_pack_expansion_extra_args_p (tree parm_packs
,
11159 bool has_empty_arg
)
11161 /* If one pack has an expansion and another pack has a normal
11162 argument or if one pack has an empty argument and an another
11163 one hasn't then tsubst_pack_expansion cannot perform the
11164 substitution and need to fall back on the
11165 PACK_EXPANSION_EXTRA mechanism. */
11166 if (parm_packs
== NULL_TREE
)
11168 else if (has_empty_arg
)
11171 bool has_expansion_arg
= false;
11172 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11174 bool has_non_expansion_arg
= false;
11175 for (tree parm_pack
= parm_packs
;
11177 parm_pack
= TREE_CHAIN (parm_pack
))
11179 tree arg
= TREE_VALUE (parm_pack
);
11181 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11183 /* We can't substitute a pack expansion with extra args into
11187 has_expansion_arg
= true;
11189 has_non_expansion_arg
= true;
11192 if (has_expansion_arg
&& has_non_expansion_arg
)
11198 /* [temp.variadic]/6 says that:
11200 The instantiation of a pack expansion [...]
11201 produces a list E1,E2, ..., En, where N is the number of elements
11202 in the pack expansion parameters.
11204 This subroutine of tsubst_pack_expansion produces one of these Ei.
11206 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11207 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11208 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11209 INDEX is the index 'i' of the element Ei to produce. ARGS,
11210 COMPLAIN, and IN_DECL are the same parameters as for the
11211 tsubst_pack_expansion function.
11213 The function returns the resulting Ei upon successful completion,
11214 or error_mark_node.
11216 Note that this function possibly modifies the ARGS parameter, so
11217 it's the responsibility of the caller to restore it. */
11220 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11223 tree args
/* This parm gets
11225 tsubst_flags_t complain
,
11229 bool ith_elem_is_expansion
= false;
11231 /* For each parameter pack, change the substitution of the parameter
11232 pack to the ith argument in its argument pack, then expand the
11234 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11236 tree parm
= TREE_PURPOSE (pack
);
11237 tree arg_pack
= TREE_VALUE (pack
);
11238 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11240 ith_elem_is_expansion
|=
11241 argument_pack_element_is_expansion_p (arg_pack
, index
);
11243 /* Select the Ith argument from the pack. */
11244 if (TREE_CODE (parm
) == PARM_DECL
11246 || TREE_CODE (parm
) == FIELD_DECL
)
11250 aps
= make_argument_pack_select (arg_pack
, index
);
11251 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11252 return error_mark_node
;
11253 register_local_specialization (aps
, parm
);
11256 aps
= retrieve_local_specialization (parm
);
11261 template_parm_level_and_index (parm
, &level
, &idx
);
11265 aps
= make_argument_pack_select (arg_pack
, index
);
11266 /* Update the corresponding argument. */
11267 TMPL_ARG (args
, level
, idx
) = aps
;
11270 /* Re-use the ARGUMENT_PACK_SELECT. */
11271 aps
= TMPL_ARG (args
, level
, idx
);
11273 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11276 /* Substitute into the PATTERN with the (possibly altered)
11278 if (pattern
== in_decl
)
11279 /* Expanding a fixed parameter pack from
11280 coerce_template_parameter_pack. */
11281 t
= tsubst_decl (pattern
, args
, complain
);
11282 else if (pattern
== error_mark_node
)
11283 t
= error_mark_node
;
11284 else if (constraint_p (pattern
))
11286 if (processing_template_decl
)
11287 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11289 t
= (constraints_satisfied_p (pattern
, args
)
11290 ? boolean_true_node
: boolean_false_node
);
11292 else if (!TYPE_P (pattern
))
11293 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11294 /*integral_constant_expression_p=*/false);
11296 t
= tsubst (pattern
, args
, complain
, in_decl
);
11298 /* If the Ith argument pack element is a pack expansion, then
11299 the Ith element resulting from the substituting is going to
11300 be a pack expansion as well. */
11301 if (ith_elem_is_expansion
)
11302 t
= make_pack_expansion (t
, complain
);
11307 /* When the unexpanded parameter pack in a fold expression expands to an empty
11308 sequence, the value of the expression is as follows; the program is
11309 ill-formed if the operator is not listed in this table.
11316 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11318 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11319 if (!FOLD_EXPR_MODIFY_P (t
))
11322 case TRUTH_ANDIF_EXPR
:
11323 return boolean_true_node
;
11324 case TRUTH_ORIF_EXPR
:
11325 return boolean_false_node
;
11326 case COMPOUND_EXPR
:
11332 if (complain
& tf_error
)
11333 error_at (location_of (t
),
11334 "fold of empty expansion over %O", code
);
11335 return error_mark_node
;
11338 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11339 form an expression that combines the two terms using the
11343 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11345 tree op
= FOLD_EXPR_OP (t
);
11346 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11348 // Handle compound assignment operators.
11349 if (FOLD_EXPR_MODIFY_P (t
))
11350 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11354 case COMPOUND_EXPR
:
11355 return build_x_compound_expr (input_location
, left
, right
, complain
);
11357 return build_m_component_ref (left
, right
, complain
);
11359 return build_x_binary_op (input_location
, code
,
11360 left
, TREE_CODE (left
),
11361 right
, TREE_CODE (right
),
11367 /* Substitute ARGS into the pack of a fold expression T. */
11370 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11372 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11375 /* Substitute ARGS into the pack of a fold expression T. */
11378 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11380 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11383 /* Expand a PACK of arguments into a grouped as left fold.
11384 Given a pack containing elements A0, A1, ..., An and an
11385 operator @, this builds the expression:
11387 ((A0 @ A1) @ A2) ... @ An
11389 Note that PACK must not be empty.
11391 The operator is defined by the original fold expression T. */
11394 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11396 tree left
= TREE_VEC_ELT (pack
, 0);
11397 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11399 tree right
= TREE_VEC_ELT (pack
, i
);
11400 left
= fold_expression (t
, left
, right
, complain
);
11405 /* Substitute into a unary left fold expression. */
11408 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11411 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11412 if (pack
== error_mark_node
)
11413 return error_mark_node
;
11414 if (PACK_EXPANSION_P (pack
))
11416 tree r
= copy_node (t
);
11417 FOLD_EXPR_PACK (r
) = pack
;
11420 if (TREE_VEC_LENGTH (pack
) == 0)
11421 return expand_empty_fold (t
, complain
);
11423 return expand_left_fold (t
, pack
, complain
);
11426 /* Substitute into a binary left fold expression.
11428 Do ths by building a single (non-empty) vector of argumnts and
11429 building the expression from those elements. */
11432 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11435 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11436 if (pack
== error_mark_node
)
11437 return error_mark_node
;
11438 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11439 if (init
== error_mark_node
)
11440 return error_mark_node
;
11442 if (PACK_EXPANSION_P (pack
))
11444 tree r
= copy_node (t
);
11445 FOLD_EXPR_PACK (r
) = pack
;
11446 FOLD_EXPR_INIT (r
) = init
;
11450 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11451 TREE_VEC_ELT (vec
, 0) = init
;
11452 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11453 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11455 return expand_left_fold (t
, vec
, complain
);
11458 /* Expand a PACK of arguments into a grouped as right fold.
11459 Given a pack containing elementns A0, A1, ..., and an
11460 operator @, this builds the expression:
11462 A0@ ... (An-2 @ (An-1 @ An))
11464 Note that PACK must not be empty.
11466 The operator is defined by the original fold expression T. */
11469 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11471 // Build the expression.
11472 int n
= TREE_VEC_LENGTH (pack
);
11473 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11474 for (--n
; n
!= 0; --n
)
11476 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11477 right
= fold_expression (t
, left
, right
, complain
);
11482 /* Substitute into a unary right fold expression. */
11485 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11488 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11489 if (pack
== error_mark_node
)
11490 return error_mark_node
;
11491 if (PACK_EXPANSION_P (pack
))
11493 tree r
= copy_node (t
);
11494 FOLD_EXPR_PACK (r
) = pack
;
11497 if (TREE_VEC_LENGTH (pack
) == 0)
11498 return expand_empty_fold (t
, complain
);
11500 return expand_right_fold (t
, pack
, complain
);
11503 /* Substitute into a binary right fold expression.
11505 Do ths by building a single (non-empty) vector of arguments and
11506 building the expression from those elements. */
11509 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11512 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11513 if (pack
== error_mark_node
)
11514 return error_mark_node
;
11515 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11516 if (init
== error_mark_node
)
11517 return error_mark_node
;
11519 if (PACK_EXPANSION_P (pack
))
11521 tree r
= copy_node (t
);
11522 FOLD_EXPR_PACK (r
) = pack
;
11523 FOLD_EXPR_INIT (r
) = init
;
11527 int n
= TREE_VEC_LENGTH (pack
);
11528 tree vec
= make_tree_vec (n
+ 1);
11529 for (int i
= 0; i
< n
; ++i
)
11530 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11531 TREE_VEC_ELT (vec
, n
) = init
;
11533 return expand_right_fold (t
, vec
, complain
);
11536 /* Walk through the pattern of a pack expansion, adding everything in
11537 local_specializations to a list. */
11542 tsubst_flags_t complain
;
11545 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11547 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11548 tree
*extra
= &data
.extra
;
11549 tsubst_flags_t complain
= data
.complain
;
11550 if (tree spec
= retrieve_local_specialization (*tp
))
11552 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11554 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11555 tree args
= ARGUMENT_PACK_ARGS (spec
);
11556 if (TREE_VEC_LENGTH (args
) == 1)
11558 tree elt
= TREE_VEC_ELT (args
, 0);
11559 if (PACK_EXPANSION_P (elt
))
11560 elt
= PACK_EXPANSION_PATTERN (elt
);
11561 if (DECL_PACK_P (elt
))
11564 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11566 /* Handle lambda capture here, since we aren't doing any
11567 substitution now, and so tsubst_copy won't call
11568 process_outer_var_ref. */
11569 tree args
= ARGUMENT_PACK_ARGS (spec
);
11570 int len
= TREE_VEC_LENGTH (args
);
11571 for (int i
= 0; i
< len
; ++i
)
11573 tree arg
= TREE_VEC_ELT (args
, i
);
11575 if (outer_automatic_var_p (arg
))
11576 carg
= process_outer_var_ref (arg
, complain
);
11579 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11583 spec
= copy_node (spec
);
11584 args
= copy_node (args
);
11585 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11586 register_local_specialization (spec
, *tp
);
11588 TREE_VEC_ELT (args
, i
) = carg
;
11593 if (outer_automatic_var_p (spec
))
11594 spec
= process_outer_var_ref (spec
, complain
);
11595 *extra
= tree_cons (*tp
, spec
, *extra
);
11600 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11602 el_data data
= { NULL_TREE
, complain
};
11603 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
11607 /* Substitute ARGS into T, which is an pack expansion
11608 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11609 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11610 (if only a partial substitution could be performed) or
11611 ERROR_MARK_NODE if there was an error. */
11613 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11617 tree pack
, packs
= NULL_TREE
;
11618 bool unsubstituted_packs
= false;
11619 bool unsubstituted_fn_pack
= false;
11622 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11623 bool need_local_specializations
= false;
11626 gcc_assert (PACK_EXPANSION_P (t
));
11627 pattern
= PACK_EXPANSION_PATTERN (t
);
11629 /* Add in any args remembered from an earlier partial instantiation. */
11630 tree extra
= PACK_EXPANSION_EXTRA_ARGS (t
);
11631 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
11633 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
11635 /* The partial instantiation involved local declarations collected in
11636 extract_local_specs; map from the general template to our local
11638 tree gen
= TREE_PURPOSE (elt
);
11639 tree inst
= TREE_VALUE (elt
);
11641 if (tree local
= retrieve_local_specialization (inst
))
11643 /* else inst is already a full instantiation of the pack. */
11644 register_local_specialization (inst
, gen
);
11646 gcc_assert (!TREE_PURPOSE (extra
));
11647 extra
= TREE_VALUE (extra
);
11649 args
= add_to_template_args (extra
, args
);
11651 levels
= TMPL_ARGS_DEPTH (args
);
11653 /* Determine the argument packs that will instantiate the parameter
11654 packs used in the expansion expression. While we're at it,
11655 compute the number of arguments to be expanded and make sure it
11657 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11658 pack
= TREE_CHAIN (pack
))
11660 tree parm_pack
= TREE_VALUE (pack
);
11661 tree arg_pack
= NULL_TREE
;
11662 tree orig_arg
= NULL_TREE
;
11665 if (TREE_CODE (parm_pack
) == BASES
)
11667 gcc_assert (parm_pack
== pattern
);
11668 if (BASES_DIRECT (parm_pack
))
11669 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11670 args
, complain
, in_decl
, false));
11672 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11673 args
, complain
, in_decl
, false));
11675 else if (builtin_pack_call_p (parm_pack
))
11677 /* ??? Support use in other patterns. */
11678 gcc_assert (parm_pack
== pattern
);
11679 return expand_builtin_pack_call (parm_pack
, args
,
11680 complain
, in_decl
);
11682 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11684 /* We know we have correct local_specializations if this
11685 expansion is at function scope, or if we're dealing with a
11686 local parameter in a requires expression; for the latter,
11687 tsubst_requires_expr set it up appropriately. */
11688 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11689 arg_pack
= retrieve_local_specialization (parm_pack
);
11691 /* We can't rely on local_specializations for a parameter
11692 name used later in a function declaration (such as in a
11693 late-specified return type). Even if it exists, it might
11694 have the wrong value for a recursive call. */
11695 need_local_specializations
= true;
11699 /* This parameter pack was used in an unevaluated context. Just
11700 make a dummy decl, since it's only used for its type. */
11701 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11702 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11703 /* Partial instantiation of the parm_pack, we can't build
11704 up an argument pack yet. */
11705 arg_pack
= NULL_TREE
;
11707 arg_pack
= make_fnparm_pack (arg_pack
);
11709 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11710 /* This argument pack isn't fully instantiated yet. We set this
11711 flag rather than clear arg_pack because we do want to do the
11712 optimization below, and we don't want to substitute directly
11713 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11714 where it isn't expected). */
11715 unsubstituted_fn_pack
= true;
11717 else if (is_normal_capture_proxy (parm_pack
))
11719 arg_pack
= retrieve_local_specialization (parm_pack
);
11720 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11721 unsubstituted_fn_pack
= true;
11726 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11728 if (level
<= levels
)
11729 arg_pack
= TMPL_ARG (args
, level
, idx
);
11732 orig_arg
= arg_pack
;
11733 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11734 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11736 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11737 /* This can only happen if we forget to expand an argument
11738 pack somewhere else. Just return an error, silently. */
11740 result
= make_tree_vec (1);
11741 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11748 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11750 /* Don't bother trying to do a partial substitution with
11751 incomplete packs; we'll try again after deduction. */
11752 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11757 else if (len
!= my_len
11758 && !unsubstituted_fn_pack
)
11760 if (!(complain
& tf_error
))
11761 /* Fail quietly. */;
11762 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11763 error ("mismatched argument pack lengths while expanding %qT",
11766 error ("mismatched argument pack lengths while expanding %qE",
11768 return error_mark_node
;
11771 /* Keep track of the parameter packs and their corresponding
11773 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11774 TREE_TYPE (packs
) = orig_arg
;
11778 /* We can't substitute for this parameter pack. We use a flag as
11779 well as the missing_level counter because function parameter
11780 packs don't have a level. */
11781 gcc_assert (processing_template_decl
);
11782 unsubstituted_packs
= true;
11786 /* If the expansion is just T..., return the matching argument pack, unless
11787 we need to call convert_from_reference on all the elements. This is an
11788 important optimization; see c++/68422. */
11789 if (!unsubstituted_packs
11790 && TREE_PURPOSE (packs
) == pattern
)
11792 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11794 /* If the argument pack is a single pack expansion, pull it out. */
11795 if (TREE_VEC_LENGTH (args
) == 1
11796 && pack_expansion_args_count (args
))
11797 return TREE_VEC_ELT (args
, 0);
11799 /* Types need no adjustment, nor does sizeof..., and if we still have
11800 some pack expansion args we won't do anything yet. */
11801 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11802 || PACK_EXPANSION_SIZEOF_P (t
)
11803 || pack_expansion_args_count (args
))
11805 /* Also optimize expression pack expansions if we can tell that the
11806 elements won't have reference type. */
11807 tree type
= TREE_TYPE (pattern
);
11808 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11809 && !PACK_EXPANSION_P (type
)
11810 && !WILDCARD_TYPE_P (type
))
11812 /* Otherwise use the normal path so we get convert_from_reference. */
11815 /* We cannot expand this expansion expression, because we don't have
11816 all of the argument packs we need. */
11817 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11819 /* We got some full packs, but we can't substitute them in until we
11820 have values for all the packs. So remember these until then. */
11822 t
= make_pack_expansion (pattern
, complain
);
11824 if (local_specializations
)
11825 if (tree locals
= extract_local_specs (pattern
, complain
))
11826 extra
= tree_cons (NULL_TREE
, extra
, locals
);
11827 PACK_EXPANSION_EXTRA_ARGS (t
) = extra
;
11830 else if (unsubstituted_packs
)
11832 /* There were no real arguments, we're just replacing a parameter
11833 pack with another version of itself. Substitute into the
11834 pattern and return a PACK_EXPANSION_*. The caller will need to
11836 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11837 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11838 /*integral_constant_expression_p=*/false);
11840 t
= tsubst (pattern
, args
, complain
, in_decl
);
11841 t
= make_pack_expansion (t
, complain
);
11845 gcc_assert (len
>= 0);
11847 if (need_local_specializations
)
11849 /* We're in a late-specified return type, so create our own local
11850 specializations map; the current map is either NULL or (in the
11851 case of recursive unification) might have bindings that we don't
11852 want to use or alter. */
11853 saved_local_specializations
= local_specializations
;
11854 local_specializations
= new hash_map
<tree
, tree
>;
11857 /* For each argument in each argument pack, substitute into the
11859 result
= make_tree_vec (len
);
11860 tree elem_args
= copy_template_args (args
);
11861 for (i
= 0; i
< len
; ++i
)
11863 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11865 elem_args
, complain
,
11867 TREE_VEC_ELT (result
, i
) = t
;
11868 if (t
== error_mark_node
)
11870 result
= error_mark_node
;
11875 /* Update ARGS to restore the substitution from parameter packs to
11876 their argument packs. */
11877 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11879 tree parm
= TREE_PURPOSE (pack
);
11881 if (TREE_CODE (parm
) == PARM_DECL
11883 || TREE_CODE (parm
) == FIELD_DECL
)
11884 register_local_specialization (TREE_TYPE (pack
), parm
);
11889 if (TREE_VALUE (pack
) == NULL_TREE
)
11892 template_parm_level_and_index (parm
, &level
, &idx
);
11894 /* Update the corresponding argument. */
11895 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11896 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11899 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11903 if (need_local_specializations
)
11905 delete local_specializations
;
11906 local_specializations
= saved_local_specializations
;
11909 /* If the dependent pack arguments were such that we end up with only a
11910 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11911 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11912 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11913 return TREE_VEC_ELT (result
, 0);
11918 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11919 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11920 parameter packs; all parms generated from a function parameter pack will
11921 have the same DECL_PARM_INDEX. */
11924 get_pattern_parm (tree parm
, tree tmpl
)
11926 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11929 if (DECL_ARTIFICIAL (parm
))
11931 for (patparm
= DECL_ARGUMENTS (pattern
);
11932 patparm
; patparm
= DECL_CHAIN (patparm
))
11933 if (DECL_ARTIFICIAL (patparm
)
11934 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11939 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11940 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11941 gcc_assert (DECL_PARM_INDEX (patparm
)
11942 == DECL_PARM_INDEX (parm
));
11948 /* Make an argument pack out of the TREE_VEC VEC. */
11951 make_argument_pack (tree vec
)
11954 tree elt
= TREE_VEC_ELT (vec
, 0);
11956 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11959 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11960 TREE_CONSTANT (pack
) = 1;
11962 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11966 /* Return an exact copy of template args T that can be modified
11970 copy_template_args (tree t
)
11972 if (t
== error_mark_node
)
11975 int len
= TREE_VEC_LENGTH (t
);
11976 tree new_vec
= make_tree_vec (len
);
11978 for (int i
= 0; i
< len
; ++i
)
11980 tree elt
= TREE_VEC_ELT (t
, i
);
11981 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11982 elt
= copy_template_args (elt
);
11983 TREE_VEC_ELT (new_vec
, i
) = elt
;
11986 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11987 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11992 /* Substitute ARGS into the vector or list of template arguments T. */
11995 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11998 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12001 if (t
== error_mark_node
)
12002 return error_mark_node
;
12004 len
= TREE_VEC_LENGTH (t
);
12005 elts
= XALLOCAVEC (tree
, len
);
12007 for (i
= 0; i
< len
; i
++)
12009 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12012 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12013 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12014 else if (PACK_EXPANSION_P (orig_arg
))
12016 /* Substitute into an expansion expression. */
12017 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12019 if (TREE_CODE (new_arg
) == TREE_VEC
)
12020 /* Add to the expanded length adjustment the number of
12021 expanded arguments. We subtract one from this
12022 measurement, because the argument pack expression
12023 itself is already counted as 1 in
12024 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12025 the argument pack is empty. */
12026 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12028 else if (ARGUMENT_PACK_P (orig_arg
))
12030 /* Substitute into each of the arguments. */
12031 new_arg
= TYPE_P (orig_arg
)
12032 ? cxx_make_type (TREE_CODE (orig_arg
))
12033 : make_node (TREE_CODE (orig_arg
));
12035 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12036 args
, complain
, in_decl
);
12037 if (pack_args
== error_mark_node
)
12038 new_arg
= error_mark_node
;
12040 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12042 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12043 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12046 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12048 if (new_arg
== error_mark_node
)
12049 return error_mark_node
;
12052 if (new_arg
!= orig_arg
)
12059 /* Make space for the expanded arguments coming from template
12061 t
= make_tree_vec (len
+ expanded_len_adjust
);
12062 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12063 arguments for a member template.
12064 In that case each TREE_VEC in ORIG_T represents a level of template
12065 arguments, and ORIG_T won't carry any non defaulted argument count.
12066 It will rather be the nested TREE_VECs that will carry one.
12067 In other words, ORIG_T carries a non defaulted argument count only
12068 if it doesn't contain any nested TREE_VEC. */
12069 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12071 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12072 count
+= expanded_len_adjust
;
12073 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12075 for (i
= 0, out
= 0; i
< len
; i
++)
12077 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12078 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12079 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12083 /* Now expand the template argument pack "in place". */
12084 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12085 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12089 TREE_VEC_ELT (t
, out
) = elts
[i
];
12097 /* Substitute ARGS into one level PARMS of template parameters. */
12100 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12102 if (parms
== error_mark_node
)
12103 return error_mark_node
;
12105 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12107 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12109 tree tuple
= TREE_VEC_ELT (parms
, i
);
12111 if (tuple
== error_mark_node
)
12114 TREE_VEC_ELT (new_vec
, i
) =
12115 tsubst_template_parm (tuple
, args
, complain
);
12121 /* Return the result of substituting ARGS into the template parameters
12122 given by PARMS. If there are m levels of ARGS and m + n levels of
12123 PARMS, then the result will contain n levels of PARMS. For
12124 example, if PARMS is `template <class T> template <class U>
12125 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12126 result will be `template <int*, double, class V>'. */
12129 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12131 tree r
= NULL_TREE
;
12134 /* When substituting into a template, we must set
12135 PROCESSING_TEMPLATE_DECL as the template parameters may be
12136 dependent if they are based on one-another, and the dependency
12137 predicates are short-circuit outside of templates. */
12138 ++processing_template_decl
;
12140 for (new_parms
= &r
;
12141 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12142 new_parms
= &(TREE_CHAIN (*new_parms
)),
12143 parms
= TREE_CHAIN (parms
))
12145 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12148 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12149 - TMPL_ARGS_DEPTH (args
)),
12150 new_vec
, NULL_TREE
);
12153 --processing_template_decl
;
12158 /* Return the result of substituting ARGS into one template parameter
12159 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12160 parameter and which TREE_PURPOSE is the default argument of the
12161 template parameter. */
12164 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12166 tree default_value
, parm_decl
;
12168 if (args
== NULL_TREE
12170 || t
== error_mark_node
)
12173 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12175 default_value
= TREE_PURPOSE (t
);
12176 parm_decl
= TREE_VALUE (t
);
12178 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12179 if (TREE_CODE (parm_decl
) == PARM_DECL
12180 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12181 parm_decl
= error_mark_node
;
12182 default_value
= tsubst_template_arg (default_value
, args
,
12183 complain
, NULL_TREE
);
12185 return build_tree_list (default_value
, parm_decl
);
12188 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12189 type T. If T is not an aggregate or enumeration type, it is
12190 handled as if by tsubst. IN_DECL is as for tsubst. If
12191 ENTERING_SCOPE is nonzero, T is the context for a template which
12192 we are presently tsubst'ing. Return the substituted value. */
12195 tsubst_aggr_type (tree t
,
12197 tsubst_flags_t complain
,
12199 int entering_scope
)
12201 if (t
== NULL_TREE
)
12204 switch (TREE_CODE (t
))
12207 if (TYPE_PTRMEMFUNC_P (t
))
12208 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12210 /* Fall through. */
12211 case ENUMERAL_TYPE
:
12213 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12218 int saved_unevaluated_operand
;
12219 int saved_inhibit_evaluation_warnings
;
12221 /* In "sizeof(X<I>)" we need to evaluate "I". */
12222 saved_unevaluated_operand
= cp_unevaluated_operand
;
12223 cp_unevaluated_operand
= 0;
12224 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12225 c_inhibit_evaluation_warnings
= 0;
12227 /* First, determine the context for the type we are looking
12229 context
= TYPE_CONTEXT (t
);
12230 if (context
&& TYPE_P (context
))
12232 context
= tsubst_aggr_type (context
, args
, complain
,
12233 in_decl
, /*entering_scope=*/1);
12234 /* If context is a nested class inside a class template,
12235 it may still need to be instantiated (c++/33959). */
12236 context
= complete_type (context
);
12239 /* Then, figure out what arguments are appropriate for the
12240 type we are trying to find. For example, given:
12242 template <class T> struct S;
12243 template <class T, class U> void f(T, U) { S<U> su; }
12245 and supposing that we are instantiating f<int, double>,
12246 then our ARGS will be {int, double}, but, when looking up
12247 S we only want {double}. */
12248 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12249 complain
, in_decl
);
12250 if (argvec
== error_mark_node
)
12251 r
= error_mark_node
;
12254 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12255 entering_scope
, complain
);
12256 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12259 cp_unevaluated_operand
= saved_unevaluated_operand
;
12260 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12265 /* This is not a template type, so there's nothing to do. */
12269 return tsubst (t
, args
, complain
, in_decl
);
12273 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12275 /* Substitute into the default argument ARG (a default argument for
12276 FN), which has the indicated TYPE. */
12279 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12280 tsubst_flags_t complain
)
12282 tree saved_class_ptr
= NULL_TREE
;
12283 tree saved_class_ref
= NULL_TREE
;
12284 int errs
= errorcount
+ sorrycount
;
12286 /* This can happen in invalid code. */
12287 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12290 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12291 parm
= chain_index (parmnum
, parm
);
12292 tree parmtype
= TREE_TYPE (parm
);
12293 if (DECL_BY_REFERENCE (parm
))
12294 parmtype
= TREE_TYPE (parmtype
);
12295 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12298 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12301 /* This default argument came from a template. Instantiate the
12302 default argument here, not in tsubst. In the case of
12311 we must be careful to do name lookup in the scope of S<T>,
12312 rather than in the current class. */
12313 push_access_scope (fn
);
12314 /* The "this" pointer is not valid in a default argument. */
12317 saved_class_ptr
= current_class_ptr
;
12318 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12319 saved_class_ref
= current_class_ref
;
12320 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12323 start_lambda_scope (parm
);
12325 push_deferring_access_checks(dk_no_deferred
);
12326 /* The default argument expression may cause implicitly defined
12327 member functions to be synthesized, which will result in garbage
12328 collection. We must treat this situation as if we were within
12329 the body of function so as to avoid collecting live data on the
12332 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12333 complain
, NULL_TREE
,
12334 /*integral_constant_expression_p=*/false);
12336 pop_deferring_access_checks();
12338 finish_lambda_scope ();
12340 /* Restore the "this" pointer. */
12343 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12344 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12347 if (errorcount
+sorrycount
> errs
12348 && (complain
& tf_warning_or_error
))
12349 inform (input_location
,
12350 " when instantiating default argument for call to %qD", fn
);
12352 /* Make sure the default argument is reasonable. */
12353 arg
= check_default_argument (type
, arg
, complain
);
12355 pop_access_scope (fn
);
12357 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12360 defarg_inst
= tree_cache_map::create_ggc (37);
12361 defarg_inst
->put (parm
, arg
);
12367 /* Substitute into all the default arguments for FN. */
12370 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12375 tmpl_args
= DECL_TI_ARGS (fn
);
12377 /* If this function is not yet instantiated, we certainly don't need
12378 its default arguments. */
12379 if (uses_template_parms (tmpl_args
))
12381 /* Don't do this again for clones. */
12382 if (DECL_CLONED_FUNCTION_P (fn
))
12386 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12388 arg
= TREE_CHAIN (arg
), ++i
)
12389 if (TREE_PURPOSE (arg
))
12390 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12392 TREE_PURPOSE (arg
),
12396 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12399 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12400 tree lambda_fntype
)
12402 tree gen_tmpl
, argvec
;
12403 hashval_t hash
= 0;
12406 /* Nobody should be tsubst'ing into non-template functions. */
12407 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12409 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12411 /* If T is not dependent, just return it. */
12412 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12415 /* Calculate the most general template of which R is a
12417 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12419 /* We're substituting a lambda function under tsubst_lambda_expr but not
12420 directly from it; find the matching function we're already inside.
12421 But don't do this if T is a generic lambda with a single level of
12422 template parms, as in that case we're doing a normal instantiation. */
12423 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12424 && (!generic_lambda_fn_p (t
)
12425 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12426 return enclosing_instantiation_of (t
);
12428 /* Calculate the complete set of arguments used to
12430 argvec
= tsubst_template_args (DECL_TI_ARGS
12431 (DECL_TEMPLATE_RESULT
12432 (DECL_TI_TEMPLATE (t
))),
12433 args
, complain
, in_decl
);
12434 if (argvec
== error_mark_node
)
12435 return error_mark_node
;
12437 /* Check to see if we already have this specialization. */
12438 if (!lambda_fntype
)
12440 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12441 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12445 /* We can see more levels of arguments than parameters if
12446 there was a specialization of a member template, like
12449 template <class T> struct S { template <class U> void f(); }
12450 template <> template <class U> void S<int>::f(U);
12452 Here, we'll be substituting into the specialization,
12453 because that's where we can find the code we actually
12454 want to generate, but we'll have enough arguments for
12455 the most general template.
12457 We also deal with the peculiar case:
12459 template <class T> struct S {
12460 template <class U> friend void f();
12462 template <class U> void f() {}
12464 template void f<double>();
12466 Here, the ARGS for the instantiation of will be {int,
12467 double}. But, we only need as many ARGS as there are
12468 levels of template parameters in CODE_PATTERN. We are
12469 careful not to get fooled into reducing the ARGS in
12472 template <class T> struct S { template <class U> void f(U); }
12473 template <class T> template <> void S<T>::f(int) {}
12475 which we can spot because the pattern will be a
12476 specialization in this case. */
12477 int args_depth
= TMPL_ARGS_DEPTH (args
);
12479 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12481 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12482 args
= get_innermost_template_args (args
, parms_depth
);
12486 /* This special case arises when we have something like this:
12488 template <class T> struct S {
12489 friend void f<int>(int, double);
12492 Here, the DECL_TI_TEMPLATE for the friend declaration
12493 will be an IDENTIFIER_NODE. We are being called from
12494 tsubst_friend_function, and we want only to create a
12495 new decl (R) with appropriate types so that we can call
12496 determine_specialization. */
12497 gen_tmpl
= NULL_TREE
;
12498 argvec
= NULL_TREE
;
12501 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12503 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12504 bool member
= ctx
&& TYPE_P (ctx
);
12506 if (member
&& !closure
)
12507 ctx
= tsubst_aggr_type (ctx
, args
,
12508 complain
, t
, /*entering_scope=*/1);
12510 tree type
= (lambda_fntype
? lambda_fntype
12511 : tsubst (TREE_TYPE (t
), args
,
12512 complain
| tf_fndecl_type
, in_decl
));
12513 if (type
== error_mark_node
)
12514 return error_mark_node
;
12516 /* If we hit excessive deduction depth, the type is bogus even if
12517 it isn't error_mark_node, so don't build a decl. */
12518 if (excessive_deduction_depth
)
12519 return error_mark_node
;
12521 /* We do NOT check for matching decls pushed separately at this
12522 point, as they may not represent instantiations of this
12523 template, and in any case are considered separate under the
12525 tree r
= copy_decl (t
);
12526 DECL_USE_TEMPLATE (r
) = 0;
12527 TREE_TYPE (r
) = type
;
12528 /* Clear out the mangled name and RTL for the instantiation. */
12529 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12530 SET_DECL_RTL (r
, NULL
);
12531 /* Leave DECL_INITIAL set on deleted instantiations. */
12532 if (!DECL_DELETED_FN (r
))
12533 DECL_INITIAL (r
) = NULL_TREE
;
12534 DECL_CONTEXT (r
) = ctx
;
12536 /* OpenMP UDRs have the only argument a reference to the declared
12537 type. We want to diagnose if the declared type is a reference,
12538 which is invalid, but as references to references are usually
12539 quietly merged, diagnose it here. */
12540 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12543 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12544 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12545 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12546 error_at (DECL_SOURCE_LOCATION (t
),
12547 "reference type %qT in "
12548 "%<#pragma omp declare reduction%>", argtype
);
12549 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12550 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12554 if (member
&& DECL_CONV_FN_P (r
))
12555 /* Type-conversion operator. Reconstruct the name, in
12556 case it's the name of one of the template's parameters. */
12557 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12559 tree parms
= DECL_ARGUMENTS (t
);
12561 parms
= DECL_CHAIN (parms
);
12562 parms
= tsubst (parms
, args
, complain
, t
);
12563 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12564 DECL_CONTEXT (parm
) = r
;
12567 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12568 DECL_CHAIN (tparm
) = parms
;
12571 DECL_ARGUMENTS (r
) = parms
;
12572 DECL_RESULT (r
) = NULL_TREE
;
12574 TREE_STATIC (r
) = 0;
12575 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12576 DECL_EXTERNAL (r
) = 1;
12577 /* If this is an instantiation of a function with internal
12578 linkage, we already know what object file linkage will be
12579 assigned to the instantiation. */
12580 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12581 DECL_DEFER_OUTPUT (r
) = 0;
12582 DECL_CHAIN (r
) = NULL_TREE
;
12583 DECL_PENDING_INLINE_INFO (r
) = 0;
12584 DECL_PENDING_INLINE_P (r
) = 0;
12585 DECL_SAVED_TREE (r
) = NULL_TREE
;
12586 DECL_STRUCT_FUNCTION (r
) = NULL
;
12588 /* We'll re-clone as appropriate in instantiate_template. */
12589 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12591 /* If we aren't complaining now, return on error before we register
12592 the specialization so that we'll complain eventually. */
12593 if ((complain
& tf_error
) == 0
12594 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12595 && !grok_op_properties (r
, /*complain=*/false))
12596 return error_mark_node
;
12598 /* When instantiating a constrained member, substitute
12599 into the constraints to create a new constraint. */
12600 if (tree ci
= get_constraints (t
))
12603 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12604 set_constraints (r
, ci
);
12607 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12608 this in the special friend case mentioned above where
12609 GEN_TMPL is NULL. */
12610 if (gen_tmpl
&& !closure
)
12612 DECL_TEMPLATE_INFO (r
)
12613 = build_template_info (gen_tmpl
, argvec
);
12614 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12617 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12619 /* We instantiated this while substituting into
12620 the type earlier (template/friend54.C). */
12623 /* We're not supposed to instantiate default arguments
12624 until they are called, for a template. But, for a
12627 template <class T> void f ()
12628 { extern void g(int i = T()); }
12630 we should do the substitution when the template is
12631 instantiated. We handle the member function case in
12632 instantiate_class_template since the default arguments
12633 might refer to other members of the class. */
12635 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12636 && !uses_template_parms (argvec
))
12637 tsubst_default_arguments (r
, complain
);
12640 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12642 /* Copy the list of befriending classes. */
12643 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12645 friends
= &TREE_CHAIN (*friends
))
12647 *friends
= copy_node (*friends
);
12648 TREE_VALUE (*friends
)
12649 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12652 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12654 maybe_retrofit_in_chrg (r
);
12655 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12656 return error_mark_node
;
12657 /* If this is an instantiation of a member template, clone it.
12658 If it isn't, that'll be handled by
12659 clone_constructors_and_destructors. */
12660 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12661 clone_function_decl (r
, /*update_methods=*/false);
12663 else if ((complain
& tf_error
) != 0
12664 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12665 && !grok_op_properties (r
, /*complain=*/true))
12666 return error_mark_node
;
12668 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12669 SET_DECL_FRIEND_CONTEXT (r
,
12670 tsubst (DECL_FRIEND_CONTEXT (t
),
12671 args
, complain
, in_decl
));
12673 /* Possibly limit visibility based on template args. */
12674 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12675 if (DECL_VISIBILITY_SPECIFIED (t
))
12677 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12678 DECL_ATTRIBUTES (r
)
12679 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12681 determine_visibility (r
);
12682 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12683 && !processing_template_decl
)
12684 defaulted_late_check (r
);
12686 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12687 args
, complain
, in_decl
);
12691 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12694 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12695 tree lambda_fntype
)
12697 /* We can get here when processing a member function template,
12698 member class template, or template template parameter. */
12699 tree decl
= DECL_TEMPLATE_RESULT (t
);
12705 hashval_t hash
= 0;
12707 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12709 /* Template template parameter is treated here. */
12710 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12711 if (new_type
== error_mark_node
)
12712 r
= error_mark_node
;
12713 /* If we get a real template back, return it. This can happen in
12714 the context of most_specialized_partial_spec. */
12715 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12718 /* The new TEMPLATE_DECL was built in
12719 reduce_template_parm_level. */
12720 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12724 if (!lambda_fntype
)
12726 /* We might already have an instance of this template.
12727 The ARGS are for the surrounding class type, so the
12728 full args contain the tsubst'd args for the context,
12729 plus the innermost args from the template decl. */
12730 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12731 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12732 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12733 /* Because this is a template, the arguments will still be
12734 dependent, even after substitution. If
12735 PROCESSING_TEMPLATE_DECL is not set, the dependency
12736 predicates will short-circuit. */
12737 ++processing_template_decl
;
12738 full_args
= tsubst_template_args (tmpl_args
, args
,
12739 complain
, in_decl
);
12740 --processing_template_decl
;
12741 if (full_args
== error_mark_node
)
12742 return error_mark_node
;
12744 /* If this is a default template template argument,
12745 tsubst might not have changed anything. */
12746 if (full_args
== tmpl_args
)
12749 hash
= hash_tmpl_and_args (t
, full_args
);
12750 spec
= retrieve_specialization (t
, full_args
, hash
);
12751 if (spec
!= NULL_TREE
)
12755 /* Make a new template decl. It will be similar to the
12756 original, but will record the current template arguments.
12757 We also create a new function declaration, which is just
12758 like the old one, but points to this new template, rather
12759 than the old one. */
12761 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12762 DECL_CHAIN (r
) = NULL_TREE
;
12764 // Build new template info linking to the original template decl.
12765 if (!lambda_fntype
)
12767 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12768 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12771 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12773 /* The template parameters for this new template are all the
12774 template parameters for the old template, except the
12775 outermost level of parameters. */
12776 DECL_TEMPLATE_PARMS (r
)
12777 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12780 if (TREE_CODE (decl
) == TYPE_DECL
12781 && !TYPE_DECL_ALIAS_P (decl
))
12784 ++processing_template_decl
;
12785 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12786 --processing_template_decl
;
12787 if (new_type
== error_mark_node
)
12788 return error_mark_node
;
12790 TREE_TYPE (r
) = new_type
;
12791 /* For a partial specialization, we need to keep pointing to
12792 the primary template. */
12793 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12794 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12795 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12796 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12797 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12802 ++processing_template_decl
;
12803 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12804 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12806 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12807 --processing_template_decl
;
12808 if (new_decl
== error_mark_node
)
12809 return error_mark_node
;
12811 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12812 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12813 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12816 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12817 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12821 DECL_TI_TEMPLATE (new_decl
) = r
;
12822 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12826 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12827 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12829 if (PRIMARY_TEMPLATE_P (t
))
12830 DECL_PRIMARY_TEMPLATE (r
) = r
;
12832 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12834 /* Record this non-type partial instantiation. */
12835 register_specialization (r
, t
,
12836 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12842 /* True if FN is the op() for a lambda in an uninstantiated template. */
12845 lambda_fn_in_template_p (tree fn
)
12847 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12849 tree closure
= DECL_CONTEXT (fn
);
12850 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12853 /* We're instantiating a variable from template function TCTX. Return the
12854 corresponding current enclosing scope. This gets complicated because lambda
12855 functions in templates are regenerated rather than instantiated, but generic
12856 lambda functions are subsequently instantiated. */
12859 enclosing_instantiation_of (tree otctx
)
12862 tree fn
= current_function_decl
;
12863 int lambda_count
= 0;
12865 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12866 tctx
= decl_function_context (tctx
))
12868 for (; fn
; fn
= decl_function_context (fn
))
12871 int flambda_count
= 0;
12872 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12873 fn
= decl_function_context (fn
))
12875 if (DECL_TEMPLATE_INFO (fn
)
12876 ? most_general_template (fn
) != most_general_template (tctx
)
12879 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12880 || DECL_CONV_FN_P (ofn
));
12883 gcc_unreachable ();
12886 /* Substitute the ARGS into the T, which is a _DECL. Return the
12887 result of the substitution. Issue error and warning messages under
12888 control of COMPLAIN. */
12891 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12893 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12894 location_t saved_loc
;
12895 tree r
= NULL_TREE
;
12897 hashval_t hash
= 0;
12899 /* Set the filename and linenumber to improve error-reporting. */
12900 saved_loc
= input_location
;
12901 input_location
= DECL_SOURCE_LOCATION (t
);
12903 switch (TREE_CODE (t
))
12905 case TEMPLATE_DECL
:
12906 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12909 case FUNCTION_DECL
:
12910 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12915 tree type
= NULL_TREE
;
12917 tree expanded_types
= NULL_TREE
;
12918 tree prev_r
= NULL_TREE
;
12919 tree first_r
= NULL_TREE
;
12921 if (DECL_PACK_P (t
))
12923 /* If there is a local specialization that isn't a
12924 parameter pack, it means that we're doing a "simple"
12925 substitution from inside tsubst_pack_expansion. Just
12926 return the local specialization (which will be a single
12928 tree spec
= retrieve_local_specialization (t
);
12930 && TREE_CODE (spec
) == PARM_DECL
12931 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12934 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12935 the parameters in this function parameter pack. */
12936 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12937 complain
, in_decl
);
12938 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12940 len
= TREE_VEC_LENGTH (expanded_types
);
12942 /* Zero-length parameter packs are boring. Just substitute
12945 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12950 /* All we did was update the type. Make a note of that. */
12951 type
= expanded_types
;
12952 expanded_types
= NULL_TREE
;
12956 /* Loop through all of the parameters we'll build. When T is
12957 a function parameter pack, LEN is the number of expanded
12958 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12960 for (i
= 0; i
< len
; ++i
)
12964 if (DECL_TEMPLATE_PARM_P (t
))
12965 SET_DECL_TEMPLATE_PARM_P (r
);
12967 if (expanded_types
)
12968 /* We're on the Ith parameter of the function parameter
12971 /* Get the Ith type. */
12972 type
= TREE_VEC_ELT (expanded_types
, i
);
12974 /* Rename the parameter to include the index. */
12976 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12979 /* We're dealing with a normal parameter. */
12980 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12982 type
= type_decays_to (type
);
12983 TREE_TYPE (r
) = type
;
12984 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12986 if (DECL_INITIAL (r
))
12988 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12989 DECL_INITIAL (r
) = TREE_TYPE (r
);
12991 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12992 complain
, in_decl
);
12995 DECL_CONTEXT (r
) = NULL_TREE
;
12997 if (!DECL_TEMPLATE_PARM_P (r
))
12998 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13000 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13001 args
, complain
, in_decl
);
13003 /* Keep track of the first new parameter we
13004 generate. That's what will be returned to the
13009 /* Build a proper chain of parameters when substituting
13010 into a function parameter pack. */
13012 DECL_CHAIN (prev_r
) = r
;
13015 /* If cp_unevaluated_operand is set, we're just looking for a
13016 single dummy parameter, so don't keep going. */
13017 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13018 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13019 complain
, DECL_CHAIN (t
));
13021 /* FIRST_R contains the start of the chain we've built. */
13028 tree type
= NULL_TREE
;
13029 tree vec
= NULL_TREE
;
13030 tree expanded_types
= NULL_TREE
;
13033 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13035 /* This field is a lambda capture pack. Return a TREE_VEC of
13036 the expanded fields to instantiate_class_template_1. */
13037 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13038 complain
, in_decl
);
13039 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13041 len
= TREE_VEC_LENGTH (expanded_types
);
13042 vec
= make_tree_vec (len
);
13046 /* All we did was update the type. Make a note of that. */
13047 type
= expanded_types
;
13048 expanded_types
= NULL_TREE
;
13052 for (int i
= 0; i
< len
; ++i
)
13055 if (expanded_types
)
13057 type
= TREE_VEC_ELT (expanded_types
, i
);
13059 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13062 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13064 if (type
== error_mark_node
)
13065 RETURN (error_mark_node
);
13066 TREE_TYPE (r
) = type
;
13067 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13069 if (DECL_C_BIT_FIELD (r
))
13070 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13072 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13073 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13075 /*integral_constant_expression_p=*/true);
13076 if (DECL_INITIAL (t
))
13078 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13079 NSDMI in perform_member_init. Still set DECL_INITIAL
13080 so that we know there is one. */
13081 DECL_INITIAL (r
) = void_node
;
13082 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13083 retrofit_lang_decl (r
);
13084 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13086 /* We don't have to set DECL_CONTEXT here; it is set by
13087 finish_member_declaration. */
13088 DECL_CHAIN (r
) = NULL_TREE
;
13090 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13091 args
, complain
, in_decl
);
13094 TREE_VEC_ELT (vec
, i
) = r
;
13103 /* We reach here only for member using decls. We also need to check
13104 uses_template_parms because DECL_DEPENDENT_P is not set for a
13105 using-declaration that designates a member of the current
13106 instantiation (c++/53549). */
13107 if (DECL_DEPENDENT_P (t
)
13108 || uses_template_parms (USING_DECL_SCOPE (t
)))
13110 tree scope
= USING_DECL_SCOPE (t
);
13111 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13112 if (PACK_EXPANSION_P (scope
))
13114 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13115 int len
= TREE_VEC_LENGTH (vec
);
13116 r
= make_tree_vec (len
);
13117 for (int i
= 0; i
< len
; ++i
)
13119 tree escope
= TREE_VEC_ELT (vec
, i
);
13120 tree elt
= do_class_using_decl (escope
, name
);
13123 r
= error_mark_node
;
13128 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13129 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13131 TREE_VEC_ELT (r
, i
) = elt
;
13136 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13137 complain
, in_decl
);
13138 r
= do_class_using_decl (inst_scope
, name
);
13140 r
= error_mark_node
;
13143 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13144 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13151 DECL_CHAIN (r
) = NULL_TREE
;
13158 tree argvec
= NULL_TREE
;
13159 tree gen_tmpl
= NULL_TREE
;
13161 tree tmpl
= NULL_TREE
;
13163 tree type
= NULL_TREE
;
13166 if (TREE_TYPE (t
) == error_mark_node
)
13167 RETURN (error_mark_node
);
13169 if (TREE_CODE (t
) == TYPE_DECL
13170 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13172 /* If this is the canonical decl, we don't have to
13173 mess with instantiations, and often we can't (for
13174 typename, template type parms and such). Note that
13175 TYPE_NAME is not correct for the above test if
13176 we've copied the type for a typedef. */
13177 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13178 if (type
== error_mark_node
)
13179 RETURN (error_mark_node
);
13180 r
= TYPE_NAME (type
);
13184 /* Check to see if we already have the specialization we
13187 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13189 /* T is a static data member or namespace-scope entity.
13190 We have to substitute into namespace-scope variables
13191 (not just variable templates) because of cases like:
13193 template <class T> void f() { extern T t; }
13195 where the entity referenced is not known until
13196 instantiation time. */
13198 ctx
= DECL_CONTEXT (t
);
13199 if (DECL_CLASS_SCOPE_P (t
))
13201 ctx
= tsubst_aggr_type (ctx
, args
,
13203 in_decl
, /*entering_scope=*/1);
13204 /* If CTX is unchanged, then T is in fact the
13205 specialization we want. That situation occurs when
13206 referencing a static data member within in its own
13207 class. We can use pointer equality, rather than
13208 same_type_p, because DECL_CONTEXT is always
13210 if (ctx
== DECL_CONTEXT (t
)
13211 /* ... unless T is a member template; in which
13212 case our caller can be willing to create a
13213 specialization of that template represented
13215 && !(DECL_TI_TEMPLATE (t
)
13216 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13222 tmpl
= DECL_TI_TEMPLATE (t
);
13223 gen_tmpl
= most_general_template (tmpl
);
13224 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13225 if (argvec
!= error_mark_node
)
13226 argvec
= (coerce_innermost_template_parms
13227 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13228 argvec
, t
, complain
,
13229 /*all*/true, /*defarg*/true));
13230 if (argvec
== error_mark_node
)
13231 RETURN (error_mark_node
);
13232 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13233 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13238 /* A local variable. */
13240 /* Subsequent calls to pushdecl will fill this in. */
13242 /* Unless this is a reference to a static variable from an
13243 enclosing function, in which case we need to fill it in now. */
13244 if (TREE_STATIC (t
))
13246 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13247 if (fn
!= current_function_decl
)
13250 spec
= retrieve_local_specialization (t
);
13252 /* If we already have the specialization we need, there is
13253 nothing more to do. */
13260 /* Create a new node for the specialization we need. */
13262 if (type
== NULL_TREE
)
13264 if (is_typedef_decl (t
))
13265 type
= DECL_ORIGINAL_TYPE (t
);
13267 type
= TREE_TYPE (t
);
13269 && VAR_HAD_UNKNOWN_BOUND (t
)
13270 && type
!= error_mark_node
)
13271 type
= strip_array_domain (type
);
13272 tree sub_args
= args
;
13273 if (tree auto_node
= type_uses_auto (type
))
13275 /* Mask off any template args past the variable's context so we
13276 don't replace the auto with an unrelated argument. */
13277 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13278 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13280 /* This should never happen with the new lambda instantiation
13281 model, but keep the handling just in case. */
13282 gcc_assert (!CHECKING_P
),
13283 sub_args
= strip_innermost_template_args (args
, extra
);
13285 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13289 /* Even if the original location is out of scope, the
13290 newly substituted one is not. */
13291 DECL_DEAD_FOR_LOCAL (r
) = 0;
13292 DECL_INITIALIZED_P (r
) = 0;
13293 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13294 if (type
== error_mark_node
)
13295 RETURN (error_mark_node
);
13296 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13298 /* It may seem that this case cannot occur, since:
13303 declares a function, not a variable. However:
13306 template <typename T> void g() { T t; }
13307 template void g<f>();
13309 is an attempt to declare a variable with function
13311 error ("variable %qD has function type",
13312 /* R is not yet sufficiently initialized, so we
13313 just use its name. */
13315 RETURN (error_mark_node
);
13317 type
= complete_type (type
);
13318 /* Wait until cp_finish_decl to set this again, to handle
13319 circular dependency (template/instantiate6.C). */
13320 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13321 type
= check_var_type (DECL_NAME (r
), type
);
13323 if (DECL_HAS_VALUE_EXPR_P (t
))
13325 tree ve
= DECL_VALUE_EXPR (t
);
13326 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13327 /*constant_expression_p=*/false);
13328 if (REFERENCE_REF_P (ve
))
13330 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13331 ve
= TREE_OPERAND (ve
, 0);
13333 SET_DECL_VALUE_EXPR (r
, ve
);
13335 if (CP_DECL_THREAD_LOCAL_P (r
)
13336 && !processing_template_decl
)
13337 set_decl_tls_model (r
, decl_default_tls_model (r
));
13339 else if (DECL_SELF_REFERENCE_P (t
))
13340 SET_DECL_SELF_REFERENCE_P (r
);
13341 TREE_TYPE (r
) = type
;
13342 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13343 DECL_CONTEXT (r
) = ctx
;
13344 /* Clear out the mangled name and RTL for the instantiation. */
13345 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13346 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13347 SET_DECL_RTL (r
, NULL
);
13348 /* The initializer must not be expanded until it is required;
13349 see [temp.inst]. */
13350 DECL_INITIAL (r
) = NULL_TREE
;
13351 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13354 if (DECL_LANG_SPECIFIC (r
))
13355 SET_DECL_DEPENDENT_INIT_P (r
, false);
13357 SET_DECL_MODE (r
, VOIDmode
);
13359 /* Possibly limit visibility based on template args. */
13360 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13361 if (DECL_VISIBILITY_SPECIFIED (t
))
13363 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13364 DECL_ATTRIBUTES (r
)
13365 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13367 determine_visibility (r
);
13372 /* A static data member declaration is always marked
13373 external when it is declared in-class, even if an
13374 initializer is present. We mimic the non-template
13375 processing here. */
13376 DECL_EXTERNAL (r
) = 1;
13377 if (DECL_NAMESPACE_SCOPE_P (t
))
13378 DECL_NOT_REALLY_EXTERN (r
) = 1;
13380 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13381 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13382 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13386 if (DECL_LANG_SPECIFIC (r
))
13387 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13388 if (!cp_unevaluated_operand
)
13389 register_local_specialization (r
, t
);
13392 DECL_CHAIN (r
) = NULL_TREE
;
13394 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13396 args
, complain
, in_decl
);
13398 /* Preserve a typedef that names a type. */
13399 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13401 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13402 set_underlying_type (r
);
13403 if (TYPE_DECL_ALIAS_P (r
))
13404 /* An alias template specialization can be dependent
13405 even if its underlying type is not. */
13406 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13409 layout_decl (r
, 0);
13414 gcc_unreachable ();
13419 /* Restore the file and line information. */
13420 input_location
= saved_loc
;
13425 /* Substitute into the ARG_TYPES of a function type.
13426 If END is a TREE_CHAIN, leave it and any following types
13430 tsubst_arg_types (tree arg_types
,
13433 tsubst_flags_t complain
,
13436 tree remaining_arg_types
;
13437 tree type
= NULL_TREE
;
13439 tree expanded_args
= NULL_TREE
;
13442 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13445 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13446 args
, end
, complain
, in_decl
);
13447 if (remaining_arg_types
== error_mark_node
)
13448 return error_mark_node
;
13450 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13452 /* For a pack expansion, perform substitution on the
13453 entire expression. Later on, we'll handle the arguments
13455 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13456 args
, complain
, in_decl
);
13458 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13459 /* So that we'll spin through the parameters, one by one. */
13460 i
= TREE_VEC_LENGTH (expanded_args
);
13463 /* We only partially substituted into the parameter
13464 pack. Our type is TYPE_PACK_EXPANSION. */
13465 type
= expanded_args
;
13466 expanded_args
= NULL_TREE
;
13474 type
= TREE_VEC_ELT (expanded_args
, i
);
13476 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13478 if (type
== error_mark_node
)
13479 return error_mark_node
;
13480 if (VOID_TYPE_P (type
))
13482 if (complain
& tf_error
)
13484 error ("invalid parameter type %qT", type
);
13486 error ("in declaration %q+D", in_decl
);
13488 return error_mark_node
;
13491 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13492 return error_mark_node
;
13494 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13495 top-level qualifiers as required. */
13496 type
= cv_unqualified (type_decays_to (type
));
13498 /* We do not substitute into default arguments here. The standard
13499 mandates that they be instantiated only when needed, which is
13500 done in build_over_call. */
13501 default_arg
= TREE_PURPOSE (arg_types
);
13503 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13504 since the new op() won't have any associated template arguments for us
13505 to refer to later. */
13506 if (lambda_fn_in_template_p (in_decl
))
13507 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13508 false/*fn*/, false/*constexpr*/);
13510 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13512 /* We've instantiated a template before its default arguments
13513 have been parsed. This can happen for a nested template
13514 class, and is not an error unless we require the default
13515 argument in a call of this function. */
13516 remaining_arg_types
=
13517 tree_cons (default_arg
, type
, remaining_arg_types
);
13518 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13521 remaining_arg_types
=
13522 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13525 return remaining_arg_types
;
13528 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13529 *not* handle the exception-specification for FNTYPE, because the
13530 initial substitution of explicitly provided template parameters
13531 during argument deduction forbids substitution into the
13532 exception-specification:
13536 All references in the function type of the function template to the
13537 corresponding template parameters are replaced by the specified tem-
13538 plate argument values. If a substitution in a template parameter or
13539 in the function type of the function template results in an invalid
13540 type, type deduction fails. [Note: The equivalent substitution in
13541 exception specifications is done only when the function is instanti-
13542 ated, at which point a program is ill-formed if the substitution
13543 results in an invalid type.] */
13546 tsubst_function_type (tree t
,
13548 tsubst_flags_t complain
,
13552 tree arg_types
= NULL_TREE
;
13555 /* The TYPE_CONTEXT is not used for function/method types. */
13556 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13558 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13560 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13562 if (late_return_type_p
)
13564 /* Substitute the argument types. */
13565 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13566 complain
, in_decl
);
13567 if (arg_types
== error_mark_node
)
13568 return error_mark_node
;
13570 tree save_ccp
= current_class_ptr
;
13571 tree save_ccr
= current_class_ref
;
13572 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13573 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13574 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13577 /* DR 1207: 'this' is in scope in the trailing return type. */
13578 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13581 /* Substitute the return type. */
13582 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13586 current_class_ptr
= save_ccp
;
13587 current_class_ref
= save_ccr
;
13591 /* Substitute the return type. */
13592 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13594 if (return_type
== error_mark_node
)
13595 return error_mark_node
;
13596 /* DR 486 clarifies that creation of a function type with an
13597 invalid return type is a deduction failure. */
13598 if (TREE_CODE (return_type
) == ARRAY_TYPE
13599 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13601 if (complain
& tf_error
)
13603 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13604 error ("function returning an array");
13606 error ("function returning a function");
13608 return error_mark_node
;
13611 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13612 return error_mark_node
;
13614 if (!late_return_type_p
)
13616 /* Substitute the argument types. */
13617 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13618 complain
, in_decl
);
13619 if (arg_types
== error_mark_node
)
13620 return error_mark_node
;
13623 /* Construct a new type node and return it. */
13624 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13626 fntype
= build_function_type (return_type
, arg_types
);
13627 fntype
= apply_memfn_quals (fntype
,
13628 type_memfn_quals (t
),
13629 type_memfn_rqual (t
));
13633 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13634 /* Don't pick up extra function qualifiers from the basetype. */
13635 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13636 if (! MAYBE_CLASS_TYPE_P (r
))
13640 Type deduction may fail for any of the following
13643 -- Attempting to create "pointer to member of T" when T
13644 is not a class type. */
13645 if (complain
& tf_error
)
13646 error ("creating pointer to member function of non-class type %qT",
13648 return error_mark_node
;
13651 fntype
= build_method_type_directly (r
, return_type
,
13652 TREE_CHAIN (arg_types
));
13653 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13655 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13657 if (late_return_type_p
)
13658 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13663 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13664 ARGS into that specification, and return the substituted
13665 specification. If there is no specification, return NULL_TREE. */
13668 tsubst_exception_specification (tree fntype
,
13670 tsubst_flags_t complain
,
13677 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13678 new_specs
= NULL_TREE
;
13679 if (specs
&& TREE_PURPOSE (specs
))
13681 /* A noexcept-specifier. */
13682 tree expr
= TREE_PURPOSE (specs
);
13683 if (TREE_CODE (expr
) == INTEGER_CST
)
13687 /* Defer instantiation of noexcept-specifiers to avoid
13688 excessive instantiations (c++/49107). */
13689 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13690 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13692 /* We already partially instantiated this member template,
13693 so combine the new args with the old. */
13694 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13695 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13696 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13697 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13701 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13702 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13706 new_specs
= tsubst_copy_and_build
13707 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13708 /*integral_constant_expression_p=*/true);
13709 new_specs
= build_noexcept_spec (new_specs
, complain
);
13713 if (! TREE_VALUE (specs
))
13720 tree expanded_specs
= NULL_TREE
;
13722 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13724 /* Expand the pack expansion type. */
13725 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13729 if (expanded_specs
== error_mark_node
)
13730 return error_mark_node
;
13731 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13732 len
= TREE_VEC_LENGTH (expanded_specs
);
13735 /* We're substituting into a member template, so
13736 we got a TYPE_PACK_EXPANSION back. Add that
13737 expansion and move on. */
13738 gcc_assert (TREE_CODE (expanded_specs
)
13739 == TYPE_PACK_EXPANSION
);
13740 new_specs
= add_exception_specifier (new_specs
,
13743 specs
= TREE_CHAIN (specs
);
13748 for (i
= 0; i
< len
; ++i
)
13750 if (expanded_specs
)
13751 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13753 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13754 if (spec
== error_mark_node
)
13756 new_specs
= add_exception_specifier (new_specs
, spec
,
13760 specs
= TREE_CHAIN (specs
);
13766 /* Take the tree structure T and replace template parameters used
13767 therein with the argument vector ARGS. IN_DECL is an associated
13768 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13769 Issue error and warning messages under control of COMPLAIN. Note
13770 that we must be relatively non-tolerant of extensions here, in
13771 order to preserve conformance; if we allow substitutions that
13772 should not be allowed, we may allow argument deductions that should
13773 not succeed, and therefore report ambiguous overload situations
13774 where there are none. In theory, we could allow the substitution,
13775 but indicate that it should have failed, and allow our caller to
13776 make sure that the right thing happens, but we don't try to do this
13779 This function is used for dealing with types, decls and the like;
13780 for expressions, use tsubst_expr or tsubst_copy. */
13783 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13785 enum tree_code code
;
13786 tree type
, r
= NULL_TREE
;
13788 if (t
== NULL_TREE
|| t
== error_mark_node
13789 || t
== integer_type_node
13790 || t
== void_type_node
13791 || t
== char_type_node
13792 || t
== unknown_type_node
13793 || TREE_CODE (t
) == NAMESPACE_DECL
13794 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13798 return tsubst_decl (t
, args
, complain
);
13800 if (args
== NULL_TREE
)
13803 code
= TREE_CODE (t
);
13805 if (code
== IDENTIFIER_NODE
)
13806 type
= IDENTIFIER_TYPE_VALUE (t
);
13808 type
= TREE_TYPE (t
);
13810 gcc_assert (type
!= unknown_type_node
);
13812 /* Reuse typedefs. We need to do this to handle dependent attributes,
13813 such as attribute aligned. */
13815 && typedef_variant_p (t
))
13817 tree decl
= TYPE_NAME (t
);
13819 if (alias_template_specialization_p (t
))
13821 /* DECL represents an alias template and we want to
13823 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13824 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13825 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13827 else if (DECL_CLASS_SCOPE_P (decl
)
13828 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13829 && uses_template_parms (DECL_CONTEXT (decl
)))
13831 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13832 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13833 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13835 else if (DECL_FUNCTION_SCOPE_P (decl
)
13836 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13837 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13838 r
= retrieve_local_specialization (decl
);
13840 /* The typedef is from a non-template context. */
13846 r
= cp_build_qualified_type_real
13847 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13848 complain
| tf_ignore_bad_quals
);
13853 /* We don't have an instantiation yet, so drop the typedef. */
13854 int quals
= cp_type_quals (t
);
13855 t
= DECL_ORIGINAL_TYPE (decl
);
13856 t
= cp_build_qualified_type_real (t
, quals
,
13857 complain
| tf_ignore_bad_quals
);
13861 bool fndecl_type
= (complain
& tf_fndecl_type
);
13862 complain
&= ~tf_fndecl_type
;
13865 && code
!= TYPENAME_TYPE
13866 && code
!= TEMPLATE_TYPE_PARM
13867 && code
!= TEMPLATE_PARM_INDEX
13868 && code
!= IDENTIFIER_NODE
13869 && code
!= FUNCTION_TYPE
13870 && code
!= METHOD_TYPE
)
13871 type
= tsubst (type
, args
, complain
, in_decl
);
13872 if (type
== error_mark_node
)
13873 return error_mark_node
;
13879 case ENUMERAL_TYPE
:
13880 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13881 /*entering_scope=*/0);
13884 case IDENTIFIER_NODE
:
13895 if (t
== integer_type_node
)
13898 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13899 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13903 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13905 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13906 /*integral_constant_expression_p=*/false);
13908 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13910 if (TREE_CODE (max
) == NOP_EXPR
13911 && TREE_SIDE_EFFECTS (omax
)
13912 && !TREE_TYPE (max
))
13913 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13915 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13916 with TREE_SIDE_EFFECTS that indicates this is not an integral
13917 constant expression. */
13918 if (processing_template_decl
13919 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13921 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13922 TREE_SIDE_EFFECTS (max
) = 1;
13925 return compute_array_index_type (NULL_TREE
, max
, complain
);
13928 case TEMPLATE_TYPE_PARM
:
13929 case TEMPLATE_TEMPLATE_PARM
:
13930 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13931 case TEMPLATE_PARM_INDEX
:
13936 tree arg
= NULL_TREE
;
13938 /* Early in template argument deduction substitution, we don't
13939 want to reduce the level of 'auto', or it will be confused
13940 with a normal template parm in subsequent deduction. */
13941 if (is_auto (t
) && (complain
& tf_partial
))
13946 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13947 template_parm_level_and_index (t
, &level
, &idx
);
13949 levels
= TMPL_ARGS_DEPTH (args
);
13950 if (level
<= levels
13951 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13953 arg
= TMPL_ARG (args
, level
, idx
);
13955 /* See through ARGUMENT_PACK_SELECT arguments. */
13956 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13957 arg
= argument_pack_select_arg (arg
);
13960 if (arg
== error_mark_node
)
13961 return error_mark_node
;
13962 else if (arg
!= NULL_TREE
)
13964 if (ARGUMENT_PACK_P (arg
))
13965 /* If ARG is an argument pack, we don't actually want to
13966 perform a substitution here, because substitutions
13967 for argument packs are only done
13968 element-by-element. We can get to this point when
13969 substituting the type of a non-type template
13970 parameter pack, when that type actually contains
13971 template parameter packs from an outer template, e.g.,
13973 template<typename... Types> struct A {
13974 template<Types... Values> struct B { };
13978 if (code
== TEMPLATE_TYPE_PARM
)
13981 gcc_assert (TYPE_P (arg
));
13983 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13985 return cp_build_qualified_type_real
13986 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13988 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13990 /* We are processing a type constructed from a
13991 template template parameter. */
13992 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13993 args
, complain
, in_decl
);
13994 if (argvec
== error_mark_node
)
13995 return error_mark_node
;
13997 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13998 || TREE_CODE (arg
) == TEMPLATE_DECL
13999 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14001 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14002 /* Consider this code:
14004 template <template <class> class Template>
14006 template <class Arg> using Bind = Template<Arg>;
14009 template <template <class> class Template, class Arg>
14010 using Instantiate = Template<Arg>; //#0
14012 template <template <class> class Template,
14015 Instantiate<Internal<Template>::template Bind,
14018 When #1 is parsed, the
14019 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14020 parameter `Template' in #0 matches the
14021 UNBOUND_CLASS_TEMPLATE representing the argument
14022 `Internal<Template>::template Bind'; We then want
14023 to assemble the type `Bind<Argument>' that can't
14024 be fully created right now, because
14025 `Internal<Template>' not being complete, the Bind
14026 template cannot be looked up in that context. So
14027 we need to "store" `Bind<Argument>' for later
14028 when the context of Bind becomes complete. Let's
14029 store that in a TYPENAME_TYPE. */
14030 return make_typename_type (TYPE_CONTEXT (arg
),
14031 build_nt (TEMPLATE_ID_EXPR
,
14032 TYPE_IDENTIFIER (arg
),
14037 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14038 are resolving nested-types in the signature of a
14039 member function templates. Otherwise ARG is a
14040 TEMPLATE_DECL and is the real template to be
14042 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14043 arg
= TYPE_NAME (arg
);
14045 r
= lookup_template_class (arg
,
14047 DECL_CONTEXT (arg
),
14048 /*entering_scope=*/0,
14050 return cp_build_qualified_type_real
14051 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14053 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14056 /* TEMPLATE_PARM_INDEX. */
14057 return convert_from_reference (unshare_expr (arg
));
14061 /* This can happen during the attempted tsubst'ing in
14062 unify. This means that we don't yet have any information
14063 about the template parameter in question. */
14066 /* If we get here, we must have been looking at a parm for a
14067 more deeply nested template. Make a new version of this
14068 template parameter, but with a lower level. */
14071 case TEMPLATE_TYPE_PARM
:
14072 case TEMPLATE_TEMPLATE_PARM
:
14073 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14074 if (cp_type_quals (t
))
14076 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14077 r
= cp_build_qualified_type_real
14078 (r
, cp_type_quals (t
),
14079 complain
| (code
== TEMPLATE_TYPE_PARM
14080 ? tf_ignore_bad_quals
: 0));
14082 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14083 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14084 && (r
= (TEMPLATE_PARM_DESCENDANTS
14085 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14086 && (r
= TREE_TYPE (r
))
14087 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14088 /* Break infinite recursion when substituting the constraints
14089 of a constrained placeholder. */;
14093 TEMPLATE_TYPE_PARM_INDEX (r
)
14094 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14095 r
, levels
, args
, complain
);
14096 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14097 TYPE_MAIN_VARIANT (r
) = r
;
14098 TYPE_POINTER_TO (r
) = NULL_TREE
;
14099 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14101 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14103 /* Propagate constraints on placeholders. */
14104 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14105 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14106 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14107 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
14110 pl
= tsubst (pl
, args
, complain
, in_decl
);
14111 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14115 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14116 /* We have reduced the level of the template
14117 template parameter, but not the levels of its
14118 template parameters, so canonical_type_parameter
14119 will not be able to find the canonical template
14120 template parameter for this level. Thus, we
14121 require structural equality checking to compare
14122 TEMPLATE_TEMPLATE_PARMs. */
14123 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14124 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14125 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14127 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14129 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14131 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14132 /* We might need to substitute into the types of non-type
14133 template parameters. */
14134 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14135 complain
, in_decl
);
14136 if (tmpl
== error_mark_node
)
14137 return error_mark_node
;
14138 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14139 complain
, in_decl
);
14140 if (argvec
== error_mark_node
)
14141 return error_mark_node
;
14143 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14144 = build_template_info (tmpl
, argvec
);
14149 case TEMPLATE_PARM_INDEX
:
14150 /* OK, now substitute the type of the non-type parameter. We
14151 couldn't do it earlier because it might be an auto parameter,
14152 and we wouldn't need to if we had an argument. */
14153 type
= tsubst (type
, args
, complain
, in_decl
);
14154 if (type
== error_mark_node
)
14155 return error_mark_node
;
14156 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14160 gcc_unreachable ();
14168 tree purpose
, value
, chain
;
14170 if (t
== void_list_node
)
14173 purpose
= TREE_PURPOSE (t
);
14176 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14177 if (purpose
== error_mark_node
)
14178 return error_mark_node
;
14180 value
= TREE_VALUE (t
);
14183 value
= tsubst (value
, args
, complain
, in_decl
);
14184 if (value
== error_mark_node
)
14185 return error_mark_node
;
14187 chain
= TREE_CHAIN (t
);
14188 if (chain
&& chain
!= void_type_node
)
14190 chain
= tsubst (chain
, args
, complain
, in_decl
);
14191 if (chain
== error_mark_node
)
14192 return error_mark_node
;
14194 if (purpose
== TREE_PURPOSE (t
)
14195 && value
== TREE_VALUE (t
)
14196 && chain
== TREE_CHAIN (t
))
14198 return hash_tree_cons (purpose
, value
, chain
);
14202 /* We should never be tsubsting a binfo. */
14203 gcc_unreachable ();
14206 /* A vector of template arguments. */
14207 gcc_assert (!type
);
14208 return tsubst_template_args (t
, args
, complain
, in_decl
);
14211 case REFERENCE_TYPE
:
14213 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14218 Type deduction may fail for any of the following
14221 -- Attempting to create a pointer to reference type.
14222 -- Attempting to create a reference to a reference type or
14223 a reference to void.
14225 Core issue 106 says that creating a reference to a reference
14226 during instantiation is no longer a cause for failure. We
14227 only enforce this check in strict C++98 mode. */
14228 if ((TREE_CODE (type
) == REFERENCE_TYPE
14229 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14230 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14232 static location_t last_loc
;
14234 /* We keep track of the last time we issued this error
14235 message to avoid spewing a ton of messages during a
14236 single bad template instantiation. */
14237 if (complain
& tf_error
14238 && last_loc
!= input_location
)
14240 if (VOID_TYPE_P (type
))
14241 error ("forming reference to void");
14242 else if (code
== POINTER_TYPE
)
14243 error ("forming pointer to reference type %qT", type
);
14245 error ("forming reference to reference type %qT", type
);
14246 last_loc
= input_location
;
14249 return error_mark_node
;
14251 else if (TREE_CODE (type
) == FUNCTION_TYPE
14252 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14253 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14255 if (complain
& tf_error
)
14257 if (code
== POINTER_TYPE
)
14258 error ("forming pointer to qualified function type %qT",
14261 error ("forming reference to qualified function type %qT",
14264 return error_mark_node
;
14266 else if (code
== POINTER_TYPE
)
14268 r
= build_pointer_type (type
);
14269 if (TREE_CODE (type
) == METHOD_TYPE
)
14270 r
= build_ptrmemfunc_type (r
);
14272 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14273 /* In C++0x, during template argument substitution, when there is an
14274 attempt to create a reference to a reference type, reference
14275 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14277 "If a template-argument for a template-parameter T names a type
14278 that is a reference to a type A, an attempt to create the type
14279 'lvalue reference to cv T' creates the type 'lvalue reference to
14280 A,' while an attempt to create the type type rvalue reference to
14281 cv T' creates the type T"
14283 r
= cp_build_reference_type
14285 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14287 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14288 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14290 if (r
!= error_mark_node
)
14291 /* Will this ever be needed for TYPE_..._TO values? */
14298 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14299 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14303 Type deduction may fail for any of the following
14306 -- Attempting to create "pointer to member of T" when T
14307 is not a class type. */
14308 if (complain
& tf_error
)
14309 error ("creating pointer to member of non-class type %qT", r
);
14310 return error_mark_node
;
14312 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14314 if (complain
& tf_error
)
14315 error ("creating pointer to member reference type %qT", type
);
14316 return error_mark_node
;
14318 if (VOID_TYPE_P (type
))
14320 if (complain
& tf_error
)
14321 error ("creating pointer to member of type void");
14322 return error_mark_node
;
14324 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14325 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14327 /* The type of the implicit object parameter gets its
14328 cv-qualifiers from the FUNCTION_TYPE. */
14331 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14332 type_memfn_rqual (type
));
14333 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14334 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14338 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14342 case FUNCTION_TYPE
:
14347 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14348 if (fntype
== error_mark_node
)
14349 return error_mark_node
;
14351 /* Substitute the exception specification. */
14352 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14353 /*defer_ok*/fndecl_type
);
14354 if (specs
== error_mark_node
)
14355 return error_mark_node
;
14357 fntype
= build_exception_variant (fntype
, specs
);
14362 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14363 if (domain
== error_mark_node
)
14364 return error_mark_node
;
14366 /* As an optimization, we avoid regenerating the array type if
14367 it will obviously be the same as T. */
14368 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14371 /* These checks should match the ones in create_array_type_for_decl.
14375 The deduction may fail for any of the following reasons:
14377 -- Attempting to create an array with an element type that
14378 is void, a function type, or a reference type, or [DR337]
14379 an abstract class type. */
14380 if (VOID_TYPE_P (type
)
14381 || TREE_CODE (type
) == FUNCTION_TYPE
14382 || (TREE_CODE (type
) == ARRAY_TYPE
14383 && TYPE_DOMAIN (type
) == NULL_TREE
)
14384 || TREE_CODE (type
) == REFERENCE_TYPE
)
14386 if (complain
& tf_error
)
14387 error ("creating array of %qT", type
);
14388 return error_mark_node
;
14391 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14392 return error_mark_node
;
14394 r
= build_cplus_array_type (type
, domain
);
14396 if (TYPE_USER_ALIGN (t
))
14398 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14399 TYPE_USER_ALIGN (r
) = 1;
14405 case TYPENAME_TYPE
:
14407 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14408 in_decl
, /*entering_scope=*/1);
14409 if (ctx
== error_mark_node
)
14410 return error_mark_node
;
14412 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14413 complain
, in_decl
);
14414 if (f
== error_mark_node
)
14415 return error_mark_node
;
14417 if (!MAYBE_CLASS_TYPE_P (ctx
))
14419 if (complain
& tf_error
)
14420 error ("%qT is not a class, struct, or union type", ctx
);
14421 return error_mark_node
;
14423 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14425 /* Normally, make_typename_type does not require that the CTX
14426 have complete type in order to allow things like:
14428 template <class T> struct S { typename S<T>::X Y; };
14430 But, such constructs have already been resolved by this
14431 point, so here CTX really should have complete type, unless
14432 it's a partial instantiation. */
14433 ctx
= complete_type (ctx
);
14434 if (!COMPLETE_TYPE_P (ctx
))
14436 if (complain
& tf_error
)
14437 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14438 return error_mark_node
;
14442 f
= make_typename_type (ctx
, f
, typename_type
,
14443 complain
| tf_keep_type_decl
);
14444 if (f
== error_mark_node
)
14446 if (TREE_CODE (f
) == TYPE_DECL
)
14448 complain
|= tf_ignore_bad_quals
;
14452 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14454 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14456 if (complain
& tf_error
)
14457 error ("%qT resolves to %qT, which is not an enumeration type",
14460 return error_mark_node
;
14462 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14464 if (complain
& tf_error
)
14465 error ("%qT resolves to %qT, which is is not a class type",
14468 return error_mark_node
;
14472 return cp_build_qualified_type_real
14473 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14476 case UNBOUND_CLASS_TEMPLATE
:
14478 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14479 in_decl
, /*entering_scope=*/1);
14480 tree name
= TYPE_IDENTIFIER (t
);
14481 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14483 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14484 return error_mark_node
;
14487 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14488 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14495 ++cp_unevaluated_operand
;
14496 ++c_inhibit_evaluation_warnings
;
14498 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14500 /*integral_constant_expression_p=*/false);
14502 --cp_unevaluated_operand
;
14503 --c_inhibit_evaluation_warnings
;
14505 type
= finish_typeof (type
);
14506 return cp_build_qualified_type_real (type
,
14508 | cp_type_quals (type
),
14512 case DECLTYPE_TYPE
:
14516 ++cp_unevaluated_operand
;
14517 ++c_inhibit_evaluation_warnings
;
14519 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14520 complain
|tf_decltype
, in_decl
,
14521 /*function_p*/false,
14522 /*integral_constant_expression*/false);
14524 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14526 if (type
== NULL_TREE
)
14528 if (complain
& tf_error
)
14529 error ("empty initializer in lambda init-capture");
14530 type
= error_mark_node
;
14532 else if (TREE_CODE (type
) == TREE_LIST
)
14533 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14536 --cp_unevaluated_operand
;
14537 --c_inhibit_evaluation_warnings
;
14539 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14540 type
= lambda_capture_field_type (type
,
14541 DECLTYPE_FOR_INIT_CAPTURE (t
),
14542 DECLTYPE_FOR_REF_CAPTURE (t
));
14543 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14544 type
= lambda_proxy_type (type
);
14547 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14548 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14550 /* In a template ~id could be either a complement expression
14551 or an unqualified-id naming a destructor; if instantiating
14552 it produces an expression, it's not an id-expression or
14555 type
= finish_decltype_type (type
, id
, complain
);
14557 return cp_build_qualified_type_real (type
,
14559 | cp_type_quals (type
),
14560 complain
| tf_ignore_bad_quals
);
14563 case UNDERLYING_TYPE
:
14565 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14566 complain
, in_decl
);
14567 return finish_underlying_type (type
);
14570 case TYPE_ARGUMENT_PACK
:
14571 case NONTYPE_ARGUMENT_PACK
:
14575 if (code
== NONTYPE_ARGUMENT_PACK
)
14576 r
= make_node (code
);
14578 r
= cxx_make_type (code
);
14580 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14581 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14582 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14600 /* We should use one of the expression tsubsts for these codes. */
14601 gcc_unreachable ();
14604 sorry ("use of %qs in template", get_tree_code_name (code
));
14605 return error_mark_node
;
14609 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14610 expression on the left-hand side of the "." or "->" operator. We
14611 only do the lookup if we had a dependent BASELINK. Otherwise we
14612 adjust it onto the instantiated heirarchy. */
14615 tsubst_baselink (tree baselink
, tree object_type
,
14616 tree args
, tsubst_flags_t complain
, tree in_decl
)
14618 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14619 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14620 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14622 tree optype
= BASELINK_OPTYPE (baselink
);
14623 optype
= tsubst (optype
, args
, complain
, in_decl
);
14625 tree template_args
= NULL_TREE
;
14626 bool template_id_p
= false;
14627 tree fns
= BASELINK_FUNCTIONS (baselink
);
14628 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14630 template_id_p
= true;
14631 template_args
= TREE_OPERAND (fns
, 1);
14632 fns
= TREE_OPERAND (fns
, 0);
14634 template_args
= tsubst_template_args (template_args
, args
,
14635 complain
, in_decl
);
14638 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14639 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14640 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14644 tree name
= OVL_NAME (fns
);
14645 if (IDENTIFIER_CONV_OP_P (name
))
14646 name
= make_conv_op_name (optype
);
14648 if (name
== complete_dtor_identifier
)
14649 /* Treat as-if non-dependent below. */
14650 dependent_p
= false;
14652 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14655 if ((complain
& tf_error
)
14656 && constructor_name_p (name
, qualifying_scope
))
14657 error ("cannot call constructor %<%T::%D%> directly",
14658 qualifying_scope
, name
);
14659 return error_mark_node
;
14662 if (BASELINK_P (baselink
))
14663 fns
= BASELINK_FUNCTIONS (baselink
);
14666 /* We're going to overwrite pieces below, make a duplicate. */
14667 baselink
= copy_node (baselink
);
14669 /* If lookup found a single function, mark it as used at this point.
14670 (If lookup found multiple functions the one selected later by
14671 overload resolution will be marked as used at that point.) */
14672 if (!template_id_p
&& !really_overloaded_fn (fns
)
14673 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14674 return error_mark_node
;
14676 if (BASELINK_P (baselink
))
14678 /* Add back the template arguments, if present. */
14680 BASELINK_FUNCTIONS (baselink
)
14681 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14683 /* Update the conversion operator type. */
14684 BASELINK_OPTYPE (baselink
) = optype
;
14688 object_type
= current_class_type
;
14690 if (qualified_p
|| !dependent_p
)
14692 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14696 /* We need to call adjust_result_of_qualified_name_lookup in case the
14697 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14698 so that we still get virtual function binding. */
14699 BASELINK_QUALIFIED_P (baselink
) = false;
14705 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14706 true if the qualified-id will be a postfix-expression in-and-of
14707 itself; false if more of the postfix-expression follows the
14708 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14712 tsubst_qualified_id (tree qualified_id
, tree args
,
14713 tsubst_flags_t complain
, tree in_decl
,
14714 bool done
, bool address_p
)
14720 tree template_args
;
14721 location_t loc
= UNKNOWN_LOCATION
;
14723 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14725 /* Figure out what name to look up. */
14726 name
= TREE_OPERAND (qualified_id
, 1);
14727 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14729 is_template
= true;
14730 loc
= EXPR_LOCATION (name
);
14731 template_args
= TREE_OPERAND (name
, 1);
14733 template_args
= tsubst_template_args (template_args
, args
,
14734 complain
, in_decl
);
14735 if (template_args
== error_mark_node
)
14736 return error_mark_node
;
14737 name
= TREE_OPERAND (name
, 0);
14741 is_template
= false;
14742 template_args
= NULL_TREE
;
14745 /* Substitute into the qualifying scope. When there are no ARGS, we
14746 are just trying to simplify a non-dependent expression. In that
14747 case the qualifying scope may be dependent, and, in any case,
14748 substituting will not help. */
14749 scope
= TREE_OPERAND (qualified_id
, 0);
14752 scope
= tsubst (scope
, args
, complain
, in_decl
);
14753 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14758 if (dependent_scope_p (scope
))
14761 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14762 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14763 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14764 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14768 if (!BASELINK_P (name
) && !DECL_P (expr
))
14770 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14772 /* A BIT_NOT_EXPR is used to represent a destructor. */
14773 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14775 error ("qualifying type %qT does not match destructor name ~%qT",
14776 scope
, TREE_OPERAND (expr
, 0));
14777 expr
= error_mark_node
;
14780 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14781 /*is_type_p=*/0, false);
14784 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14785 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14786 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14788 if (complain
& tf_error
)
14790 error ("dependent-name %qE is parsed as a non-type, but "
14791 "instantiation yields a type", qualified_id
);
14792 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14794 return error_mark_node
;
14800 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14802 /* Remember that there was a reference to this entity. */
14803 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14804 return error_mark_node
;
14807 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14809 if (complain
& tf_error
)
14810 qualified_name_lookup_error (scope
,
14811 TREE_OPERAND (qualified_id
, 1),
14812 expr
, input_location
);
14813 return error_mark_node
;
14818 if (variable_template_p (expr
))
14819 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14822 expr
= lookup_template_function (expr
, template_args
);
14825 if (expr
== error_mark_node
&& complain
& tf_error
)
14826 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14827 expr
, input_location
);
14828 else if (TYPE_P (scope
))
14830 expr
= (adjust_result_of_qualified_name_lookup
14831 (expr
, scope
, current_nonlambda_class_type ()));
14832 expr
= (finish_qualified_id_expr
14833 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14834 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14835 /*template_arg_p=*/false, complain
));
14838 /* Expressions do not generally have reference type. */
14839 if (TREE_CODE (expr
) != SCOPE_REF
14840 /* However, if we're about to form a pointer-to-member, we just
14841 want the referenced member referenced. */
14842 && TREE_CODE (expr
) != OFFSET_REF
)
14843 expr
= convert_from_reference (expr
);
14845 if (REF_PARENTHESIZED_P (qualified_id
))
14846 expr
= force_paren_expr (expr
);
14851 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14852 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14856 tsubst_init (tree init
, tree decl
, tree args
,
14857 tsubst_flags_t complain
, tree in_decl
)
14862 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14864 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14866 /* If we had an initializer but it
14867 instantiated to nothing,
14868 value-initialize the object. This will
14869 only occur when the initializer was a
14870 pack expansion where the parameter packs
14871 used in that expansion were of length
14873 init
= build_value_init (TREE_TYPE (decl
),
14875 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14876 init
= get_target_expr_sfinae (init
, complain
);
14877 if (TREE_CODE (init
) == TARGET_EXPR
)
14878 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14884 /* Like tsubst, but deals with expressions. This function just replaces
14885 template parms; to finish processing the resultant expression, use
14886 tsubst_copy_and_build or tsubst_expr. */
14889 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14891 enum tree_code code
;
14894 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14897 code
= TREE_CODE (t
);
14902 r
= retrieve_local_specialization (t
);
14904 if (r
== NULL_TREE
)
14906 /* We get here for a use of 'this' in an NSDMI. */
14907 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14908 return current_class_ptr
;
14910 /* This can happen for a parameter name used later in a function
14911 declaration (such as in a late-specified return type). Just
14912 make a dummy decl, since it's only used for its type. */
14913 gcc_assert (cp_unevaluated_operand
!= 0);
14914 r
= tsubst_decl (t
, args
, complain
);
14915 /* Give it the template pattern as its context; its true context
14916 hasn't been instantiated yet and this is good enough for
14918 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14921 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14922 r
= argument_pack_select_arg (r
);
14923 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14924 return error_mark_node
;
14932 if (DECL_TEMPLATE_PARM_P (t
))
14933 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14934 /* There is no need to substitute into namespace-scope
14936 if (DECL_NAMESPACE_SCOPE_P (t
))
14938 /* If ARGS is NULL, then T is known to be non-dependent. */
14939 if (args
== NULL_TREE
)
14940 return scalar_constant_value (t
);
14942 /* Unfortunately, we cannot just call lookup_name here.
14945 template <int I> int f() {
14947 struct S { void g() { E e = a; } };
14950 When we instantiate f<7>::S::g(), say, lookup_name is not
14951 clever enough to find f<7>::a. */
14953 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14954 /*entering_scope=*/0);
14956 for (v
= TYPE_VALUES (enum_type
);
14958 v
= TREE_CHAIN (v
))
14959 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14960 return TREE_VALUE (v
);
14962 /* We didn't find the name. That should never happen; if
14963 name-lookup found it during preliminary parsing, we
14964 should find it again here during instantiation. */
14965 gcc_unreachable ();
14970 if (DECL_CONTEXT (t
))
14974 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14975 /*entering_scope=*/1);
14976 if (ctx
!= DECL_CONTEXT (t
))
14978 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14981 if (complain
& tf_error
)
14982 error ("using invalid field %qD", t
);
14983 return error_mark_node
;
14992 case FUNCTION_DECL
:
14993 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14994 r
= tsubst (t
, args
, complain
, in_decl
);
14995 else if (local_variable_p (t
)
14996 && uses_template_parms (DECL_CONTEXT (t
)))
14998 r
= retrieve_local_specialization (t
);
14999 if (r
== NULL_TREE
)
15001 /* First try name lookup to find the instantiation. */
15002 r
= lookup_name (DECL_NAME (t
));
15003 if (r
&& !is_capture_proxy (r
))
15005 /* Make sure that the one we found is the one we want. */
15006 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15007 if (ctx
!= DECL_CONTEXT (r
))
15015 /* This can happen for a variable used in a
15016 late-specified return type of a local lambda, or for a
15017 local static or constant. Building a new VAR_DECL
15018 should be OK in all those cases. */
15019 r
= tsubst_decl (t
, args
, complain
);
15020 if (local_specializations
)
15021 /* Avoid infinite recursion (79640). */
15022 register_local_specialization (r
, t
);
15023 if (decl_maybe_constant_var_p (r
))
15025 /* We can't call cp_finish_decl, so handle the
15026 initializer by hand. */
15027 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15028 complain
, in_decl
);
15029 if (!processing_template_decl
)
15030 init
= maybe_constant_init (init
);
15031 if (processing_template_decl
15032 ? potential_constant_expression (init
)
15033 : reduced_constant_expression_p (init
))
15034 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15035 = TREE_CONSTANT (r
) = true;
15036 DECL_INITIAL (r
) = init
;
15037 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15039 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15040 complain
, adc_variable_type
);
15042 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15043 || decl_constant_var_p (r
)
15044 || errorcount
|| sorrycount
);
15045 if (!processing_template_decl
15046 && !TREE_STATIC (r
))
15047 r
= process_outer_var_ref (r
, complain
);
15049 /* Remember this for subsequent uses. */
15050 if (local_specializations
)
15051 register_local_specialization (r
, t
);
15053 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15054 r
= argument_pack_select_arg (r
);
15058 if (!mark_used (r
, complain
))
15059 return error_mark_node
;
15062 case NAMESPACE_DECL
:
15066 /* An OVERLOAD will always be a non-dependent overload set; an
15067 overload set from function scope will just be represented with an
15068 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15069 gcc_assert (!uses_template_parms (t
));
15070 /* We must have marked any lookups as persistent. */
15071 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
15075 return tsubst_baselink (t
, current_nonlambda_class_type (),
15076 args
, complain
, in_decl
);
15078 case TEMPLATE_DECL
:
15079 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15080 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15081 args
, complain
, in_decl
);
15082 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15083 return tsubst (t
, args
, complain
, in_decl
);
15084 else if (DECL_CLASS_SCOPE_P (t
)
15085 && uses_template_parms (DECL_CONTEXT (t
)))
15087 /* Template template argument like the following example need
15090 template <template <class> class TT> struct C {};
15091 template <class T> struct D {
15092 template <class U> struct E {};
15097 We are processing the template argument `E' in #1 for
15098 the template instantiation #2. Originally, `E' is a
15099 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15100 have to substitute this with one having context `D<int>'. */
15102 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15103 if (dependent_scope_p (context
))
15105 /* When rewriting a constructor into a deduction guide, a
15106 non-dependent name can become dependent, so memtmpl<args>
15107 becomes context::template memtmpl<args>. */
15108 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15109 return build_qualified_name (type
, context
, DECL_NAME (t
),
15112 return lookup_field (context
, DECL_NAME(t
), 0, false);
15115 /* Ordinary template template argument. */
15118 case NON_LVALUE_EXPR
:
15119 case VIEW_CONVERT_EXPR
:
15121 /* Handle location wrappers by substituting the wrapped node
15122 first, *then* reusing the resulting type. Doing the type
15123 first ensures that we handle template parameters and
15124 parameter pack expansions. */
15125 gcc_assert (location_wrapper_p (t
));
15126 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15127 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15131 case REINTERPRET_CAST_EXPR
:
15132 case CONST_CAST_EXPR
:
15133 case STATIC_CAST_EXPR
:
15134 case DYNAMIC_CAST_EXPR
:
15135 case IMPLICIT_CONV_EXPR
:
15139 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15140 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15141 return build1 (code
, type
, op0
);
15145 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15146 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15148 tree expanded
, op
= TREE_OPERAND (t
, 0);
15151 if (SIZEOF_EXPR_TYPE_P (t
))
15152 op
= TREE_TYPE (op
);
15154 ++cp_unevaluated_operand
;
15155 ++c_inhibit_evaluation_warnings
;
15156 /* We only want to compute the number of arguments. */
15157 if (PACK_EXPANSION_P (op
))
15158 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15160 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15161 args
, complain
, in_decl
);
15162 --cp_unevaluated_operand
;
15163 --c_inhibit_evaluation_warnings
;
15165 if (TREE_CODE (expanded
) == TREE_VEC
)
15167 len
= TREE_VEC_LENGTH (expanded
);
15168 /* Set TREE_USED for the benefit of -Wunused. */
15169 for (int i
= 0; i
< len
; i
++)
15170 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15171 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15174 if (expanded
== error_mark_node
)
15175 return error_mark_node
;
15176 else if (PACK_EXPANSION_P (expanded
)
15177 || (TREE_CODE (expanded
) == TREE_VEC
15178 && pack_expansion_args_count (expanded
)))
15181 if (PACK_EXPANSION_P (expanded
))
15183 else if (TREE_VEC_LENGTH (expanded
) == 1)
15184 expanded
= TREE_VEC_ELT (expanded
, 0);
15186 expanded
= make_argument_pack (expanded
);
15188 if (TYPE_P (expanded
))
15189 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15190 complain
& tf_error
);
15192 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15193 complain
& tf_error
);
15196 return build_int_cst (size_type_node
, len
);
15198 if (SIZEOF_EXPR_TYPE_P (t
))
15200 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15201 args
, complain
, in_decl
);
15202 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15203 r
= build1 (SIZEOF_EXPR
,
15204 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15205 SIZEOF_EXPR_TYPE_P (r
) = 1;
15212 case TRUTH_NOT_EXPR
:
15215 case UNARY_PLUS_EXPR
: /* Unary + */
15217 case AT_ENCODE_EXPR
:
15221 case REALPART_EXPR
:
15222 case IMAGPART_EXPR
:
15225 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15226 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15227 return build1 (code
, type
, op0
);
15230 case COMPONENT_REF
:
15235 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15236 name
= TREE_OPERAND (t
, 1);
15237 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15239 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15240 complain
, in_decl
);
15241 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15243 else if (TREE_CODE (name
) == SCOPE_REF
15244 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15246 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15247 complain
, in_decl
);
15248 name
= TREE_OPERAND (name
, 1);
15249 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15250 complain
, in_decl
);
15251 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15252 name
= build_qualified_name (/*type=*/NULL_TREE
,
15254 /*template_p=*/false);
15256 else if (BASELINK_P (name
))
15257 name
= tsubst_baselink (name
,
15258 non_reference (TREE_TYPE (object
)),
15262 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15263 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15269 case TRUNC_DIV_EXPR
:
15270 case CEIL_DIV_EXPR
:
15271 case FLOOR_DIV_EXPR
:
15272 case ROUND_DIV_EXPR
:
15273 case EXACT_DIV_EXPR
:
15277 case TRUNC_MOD_EXPR
:
15278 case FLOOR_MOD_EXPR
:
15279 case TRUTH_ANDIF_EXPR
:
15280 case TRUTH_ORIF_EXPR
:
15281 case TRUTH_AND_EXPR
:
15282 case TRUTH_OR_EXPR
:
15295 case COMPOUND_EXPR
:
15298 case PREDECREMENT_EXPR
:
15299 case PREINCREMENT_EXPR
:
15300 case POSTDECREMENT_EXPR
:
15301 case POSTINCREMENT_EXPR
:
15303 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15304 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15305 return build_nt (code
, op0
, op1
);
15310 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15311 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15312 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15313 QUALIFIED_NAME_IS_TEMPLATE (t
));
15318 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15319 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15320 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15325 int n
= VL_EXP_OPERAND_LENGTH (t
);
15326 tree result
= build_vl_exp (CALL_EXPR
, n
);
15328 for (i
= 0; i
< n
; i
++)
15329 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15330 complain
, in_decl
);
15336 case PSEUDO_DTOR_EXPR
:
15337 case VEC_PERM_EXPR
:
15339 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15340 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15341 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15342 r
= build_nt (code
, op0
, op1
, op2
);
15343 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15349 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15350 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15351 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15352 r
= build_nt (code
, op0
, op1
, op2
);
15353 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15359 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15360 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15361 r
= build_nt (code
, op0
, op1
);
15362 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15363 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15367 case TEMPLATE_ID_EXPR
:
15369 /* Substituted template arguments */
15370 tree fn
= TREE_OPERAND (t
, 0);
15371 tree targs
= TREE_OPERAND (t
, 1);
15373 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15375 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15377 return lookup_template_function (fn
, targs
);
15382 tree purpose
, value
, chain
;
15384 if (t
== void_list_node
)
15387 purpose
= TREE_PURPOSE (t
);
15389 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15390 value
= TREE_VALUE (t
);
15392 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15393 chain
= TREE_CHAIN (t
);
15394 if (chain
&& chain
!= void_type_node
)
15395 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15396 if (purpose
== TREE_PURPOSE (t
)
15397 && value
== TREE_VALUE (t
)
15398 && chain
== TREE_CHAIN (t
))
15400 return tree_cons (purpose
, value
, chain
);
15405 case ENUMERAL_TYPE
:
15407 case TEMPLATE_TYPE_PARM
:
15408 case TEMPLATE_TEMPLATE_PARM
:
15409 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15410 case TEMPLATE_PARM_INDEX
:
15412 case REFERENCE_TYPE
:
15414 case FUNCTION_TYPE
:
15417 case TYPENAME_TYPE
:
15418 case UNBOUND_CLASS_TEMPLATE
:
15420 case DECLTYPE_TYPE
:
15422 return tsubst (t
, args
, complain
, in_decl
);
15426 /* Fall through. */
15427 case IDENTIFIER_NODE
:
15428 if (IDENTIFIER_CONV_OP_P (t
))
15430 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15431 return make_conv_op_name (new_type
);
15437 /* This is handled by tsubst_copy_and_build. */
15438 gcc_unreachable ();
15442 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15443 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15444 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15447 case CLEANUP_POINT_EXPR
:
15448 /* We shouldn't have built any of these during initial template
15449 generation. Instead, they should be built during instantiation
15450 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15451 gcc_unreachable ();
15455 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15456 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15457 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15458 r
= build2 (code
, type
, op0
, op1
);
15459 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15460 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15461 && !(complain
& tf_error
))
15462 return error_mark_node
;
15466 case EXPR_PACK_EXPANSION
:
15467 error ("invalid use of pack expansion expression");
15468 return error_mark_node
;
15470 case NONTYPE_ARGUMENT_PACK
:
15471 error ("use %<...%> to expand argument pack");
15472 return error_mark_node
;
15475 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15483 /* Instantiate any typedefs in the type. */
15484 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15485 r
= fold_convert (type
, t
);
15486 gcc_assert (TREE_CODE (r
) == code
);
15491 /* These can sometimes show up in a partial instantiation, but never
15492 involve template parms. */
15493 gcc_assert (!uses_template_parms (t
));
15496 case UNARY_LEFT_FOLD_EXPR
:
15497 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15498 case UNARY_RIGHT_FOLD_EXPR
:
15499 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15500 case BINARY_LEFT_FOLD_EXPR
:
15501 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15502 case BINARY_RIGHT_FOLD_EXPR
:
15503 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15507 case DEBUG_BEGIN_STMT
:
15508 /* ??? There's no point in copying it for now, but maybe some
15509 day it will contain more information, such as a pointer back
15510 to the containing function, inlined copy or so. */
15514 /* We shouldn't get here, but keep going if !flag_checking. */
15516 gcc_unreachable ();
15521 /* Helper function for tsubst_omp_clauses, used for instantiation of
15522 OMP_CLAUSE_DECL of clauses. */
15525 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15528 if (decl
== NULL_TREE
)
15531 /* Handle an OpenMP array section represented as a TREE_LIST (or
15532 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15533 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15534 TREE_LIST. We can handle it exactly the same as an array section
15535 (purpose, value, and a chain), even though the nomenclature
15536 (low_bound, length, etc) is different. */
15537 if (TREE_CODE (decl
) == TREE_LIST
)
15540 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15541 /*integral_constant_expression_p=*/false);
15542 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15543 /*integral_constant_expression_p=*/false);
15544 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15546 if (TREE_PURPOSE (decl
) == low_bound
15547 && TREE_VALUE (decl
) == length
15548 && TREE_CHAIN (decl
) == chain
)
15550 tree ret
= tree_cons (low_bound
, length
, chain
);
15551 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15552 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15555 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15556 /*integral_constant_expression_p=*/false);
15557 /* Undo convert_from_reference tsubst_expr could have called. */
15559 && REFERENCE_REF_P (ret
)
15560 && !REFERENCE_REF_P (decl
))
15561 ret
= TREE_OPERAND (ret
, 0);
15565 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15568 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15569 tree args
, tsubst_flags_t complain
, tree in_decl
)
15571 tree new_clauses
= NULL_TREE
, nc
, oc
;
15572 tree linear_no_step
= NULL_TREE
;
15574 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15576 nc
= copy_node (oc
);
15577 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15580 switch (OMP_CLAUSE_CODE (nc
))
15582 case OMP_CLAUSE_LASTPRIVATE
:
15583 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15585 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15586 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15587 in_decl
, /*integral_constant_expression_p=*/false);
15588 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15589 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15592 case OMP_CLAUSE_PRIVATE
:
15593 case OMP_CLAUSE_SHARED
:
15594 case OMP_CLAUSE_FIRSTPRIVATE
:
15595 case OMP_CLAUSE_COPYIN
:
15596 case OMP_CLAUSE_COPYPRIVATE
:
15597 case OMP_CLAUSE_UNIFORM
:
15598 case OMP_CLAUSE_DEPEND
:
15599 case OMP_CLAUSE_FROM
:
15600 case OMP_CLAUSE_TO
:
15601 case OMP_CLAUSE_MAP
:
15602 case OMP_CLAUSE_USE_DEVICE_PTR
:
15603 case OMP_CLAUSE_IS_DEVICE_PTR
:
15604 OMP_CLAUSE_DECL (nc
)
15605 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15608 case OMP_CLAUSE_TILE
:
15609 case OMP_CLAUSE_IF
:
15610 case OMP_CLAUSE_NUM_THREADS
:
15611 case OMP_CLAUSE_SCHEDULE
:
15612 case OMP_CLAUSE_COLLAPSE
:
15613 case OMP_CLAUSE_FINAL
:
15614 case OMP_CLAUSE_DEVICE
:
15615 case OMP_CLAUSE_DIST_SCHEDULE
:
15616 case OMP_CLAUSE_NUM_TEAMS
:
15617 case OMP_CLAUSE_THREAD_LIMIT
:
15618 case OMP_CLAUSE_SAFELEN
:
15619 case OMP_CLAUSE_SIMDLEN
:
15620 case OMP_CLAUSE_NUM_TASKS
:
15621 case OMP_CLAUSE_GRAINSIZE
:
15622 case OMP_CLAUSE_PRIORITY
:
15623 case OMP_CLAUSE_ORDERED
:
15624 case OMP_CLAUSE_HINT
:
15625 case OMP_CLAUSE_NUM_GANGS
:
15626 case OMP_CLAUSE_NUM_WORKERS
:
15627 case OMP_CLAUSE_VECTOR_LENGTH
:
15628 case OMP_CLAUSE_WORKER
:
15629 case OMP_CLAUSE_VECTOR
:
15630 case OMP_CLAUSE_ASYNC
:
15631 case OMP_CLAUSE_WAIT
:
15632 OMP_CLAUSE_OPERAND (nc
, 0)
15633 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15634 in_decl
, /*integral_constant_expression_p=*/false);
15636 case OMP_CLAUSE_REDUCTION
:
15637 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15639 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15640 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15642 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15643 complain
, in_decl
);
15644 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15645 = build_qualified_name (NULL_TREE
, scope
,
15646 TREE_OPERAND (placeholder
, 1),
15650 gcc_assert (identifier_p (placeholder
));
15652 OMP_CLAUSE_DECL (nc
)
15653 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15656 case OMP_CLAUSE_GANG
:
15657 case OMP_CLAUSE_ALIGNED
:
15658 OMP_CLAUSE_DECL (nc
)
15659 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15661 OMP_CLAUSE_OPERAND (nc
, 1)
15662 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15663 in_decl
, /*integral_constant_expression_p=*/false);
15665 case OMP_CLAUSE_LINEAR
:
15666 OMP_CLAUSE_DECL (nc
)
15667 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15669 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15671 gcc_assert (!linear_no_step
);
15672 linear_no_step
= nc
;
15674 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15675 OMP_CLAUSE_LINEAR_STEP (nc
)
15676 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15677 complain
, in_decl
);
15679 OMP_CLAUSE_LINEAR_STEP (nc
)
15680 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15682 /*integral_constant_expression_p=*/false);
15684 case OMP_CLAUSE_NOWAIT
:
15685 case OMP_CLAUSE_DEFAULT
:
15686 case OMP_CLAUSE_UNTIED
:
15687 case OMP_CLAUSE_MERGEABLE
:
15688 case OMP_CLAUSE_INBRANCH
:
15689 case OMP_CLAUSE_NOTINBRANCH
:
15690 case OMP_CLAUSE_PROC_BIND
:
15691 case OMP_CLAUSE_FOR
:
15692 case OMP_CLAUSE_PARALLEL
:
15693 case OMP_CLAUSE_SECTIONS
:
15694 case OMP_CLAUSE_TASKGROUP
:
15695 case OMP_CLAUSE_NOGROUP
:
15696 case OMP_CLAUSE_THREADS
:
15697 case OMP_CLAUSE_SIMD
:
15698 case OMP_CLAUSE_DEFAULTMAP
:
15699 case OMP_CLAUSE_INDEPENDENT
:
15700 case OMP_CLAUSE_AUTO
:
15701 case OMP_CLAUSE_SEQ
:
15704 gcc_unreachable ();
15706 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15707 switch (OMP_CLAUSE_CODE (nc
))
15709 case OMP_CLAUSE_SHARED
:
15710 case OMP_CLAUSE_PRIVATE
:
15711 case OMP_CLAUSE_FIRSTPRIVATE
:
15712 case OMP_CLAUSE_LASTPRIVATE
:
15713 case OMP_CLAUSE_COPYPRIVATE
:
15714 case OMP_CLAUSE_LINEAR
:
15715 case OMP_CLAUSE_REDUCTION
:
15716 case OMP_CLAUSE_USE_DEVICE_PTR
:
15717 case OMP_CLAUSE_IS_DEVICE_PTR
:
15718 /* tsubst_expr on SCOPE_REF results in returning
15719 finish_non_static_data_member result. Undo that here. */
15720 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15721 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15722 == IDENTIFIER_NODE
))
15724 tree t
= OMP_CLAUSE_DECL (nc
);
15727 switch (TREE_CODE (v
))
15729 case COMPONENT_REF
:
15733 case POINTER_PLUS_EXPR
:
15734 v
= TREE_OPERAND (v
, 0);
15737 if (DECL_CONTEXT (v
) == current_function_decl
15738 && DECL_ARTIFICIAL (v
)
15739 && DECL_NAME (v
) == this_identifier
)
15740 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15747 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15748 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15749 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15750 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15751 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15753 tree decl
= OMP_CLAUSE_DECL (nc
);
15756 retrofit_lang_decl (decl
);
15757 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15766 new_clauses
= nreverse (new_clauses
);
15767 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15769 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15770 if (linear_no_step
)
15771 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15772 if (nc
== linear_no_step
)
15774 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15778 return new_clauses
;
15781 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15784 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15787 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15789 tree purpose
, value
, chain
;
15794 if (TREE_CODE (t
) != TREE_LIST
)
15795 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15796 /*function_p=*/false,
15797 /*integral_constant_expression_p=*/false);
15799 if (t
== void_list_node
)
15802 purpose
= TREE_PURPOSE (t
);
15804 purpose
= RECUR (purpose
);
15805 value
= TREE_VALUE (t
);
15808 if (TREE_CODE (value
) != LABEL_DECL
)
15809 value
= RECUR (value
);
15812 value
= lookup_label (DECL_NAME (value
));
15813 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15814 TREE_USED (value
) = 1;
15817 chain
= TREE_CHAIN (t
);
15818 if (chain
&& chain
!= void_type_node
)
15819 chain
= RECUR (chain
);
15820 return tree_cons (purpose
, value
, chain
);
15824 /* Used to temporarily communicate the list of #pragma omp parallel
15825 clauses to #pragma omp for instantiation if they are combined
15828 static tree
*omp_parallel_combined_clauses
;
15830 /* Substitute one OMP_FOR iterator. */
15833 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15834 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15835 tree args
, tsubst_flags_t complain
, tree in_decl
,
15836 bool integral_constant_expression_p
)
15838 #define RECUR(NODE) \
15839 tsubst_expr ((NODE), args, complain, in_decl, \
15840 integral_constant_expression_p)
15841 tree decl
, init
, cond
, incr
;
15843 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15844 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15846 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15848 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15849 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15852 decl
= TREE_OPERAND (init
, 0);
15853 init
= TREE_OPERAND (init
, 1);
15854 tree decl_expr
= NULL_TREE
;
15855 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15857 /* We need to jump through some hoops to handle declarations in the
15858 init-statement, since we might need to handle auto deduction,
15859 but we need to keep control of initialization. */
15861 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15862 decl
= tsubst_decl (decl
, args
, complain
);
15866 if (TREE_CODE (decl
) == SCOPE_REF
)
15868 decl
= RECUR (decl
);
15869 if (TREE_CODE (decl
) == COMPONENT_REF
)
15873 switch (TREE_CODE (v
))
15875 case COMPONENT_REF
:
15879 case POINTER_PLUS_EXPR
:
15880 v
= TREE_OPERAND (v
, 0);
15883 if (DECL_CONTEXT (v
) == current_function_decl
15884 && DECL_ARTIFICIAL (v
)
15885 && DECL_NAME (v
) == this_identifier
)
15887 decl
= TREE_OPERAND (decl
, 1);
15888 decl
= omp_privatize_field (decl
, false);
15898 decl
= RECUR (decl
);
15900 init
= RECUR (init
);
15902 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15903 if (auto_node
&& init
)
15905 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
15907 gcc_assert (!type_dependent_expression_p (decl
));
15909 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15913 /* Declare the variable, but don't let that initialize it. */
15914 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15915 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15917 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15920 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15921 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15922 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15924 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15925 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15926 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15927 NOP_EXPR
, rhs
, complain
);
15930 incr
= RECUR (incr
);
15931 TREE_VEC_ELT (declv
, i
) = decl
;
15932 TREE_VEC_ELT (initv
, i
) = init
;
15933 TREE_VEC_ELT (condv
, i
) = cond
;
15934 TREE_VEC_ELT (incrv
, i
) = incr
;
15940 /* Declare and initialize the variable. */
15948 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15950 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15952 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15953 && OMP_CLAUSE_DECL (*pc
) == decl
)
15955 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15956 && OMP_CLAUSE_DECL (*pc
) == decl
)
15960 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15962 *pc
= OMP_CLAUSE_CHAIN (c
);
15963 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15966 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15967 && OMP_CLAUSE_DECL (*pc
) == decl
)
15969 error ("iteration variable %qD should not be firstprivate",
15971 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15973 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15974 && OMP_CLAUSE_DECL (*pc
) == decl
)
15976 error ("iteration variable %qD should not be reduction",
15978 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15981 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15986 if (*pc
== NULL_TREE
)
15988 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15989 OMP_CLAUSE_DECL (c
) = decl
;
15990 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15993 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15998 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15999 if (COMPARISON_CLASS_P (cond
))
16001 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16002 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16003 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16006 cond
= RECUR (cond
);
16007 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16008 switch (TREE_CODE (incr
))
16010 case PREINCREMENT_EXPR
:
16011 case PREDECREMENT_EXPR
:
16012 case POSTINCREMENT_EXPR
:
16013 case POSTDECREMENT_EXPR
:
16014 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16015 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16018 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16019 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16021 tree rhs
= TREE_OPERAND (incr
, 1);
16022 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16023 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16024 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16025 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16026 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16030 incr
= RECUR (incr
);
16033 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16034 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16036 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16037 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16038 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16039 TREE_TYPE (decl
), lhs
,
16040 RECUR (TREE_OPERAND (incr
, 2))));
16042 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16043 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16044 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16046 tree rhs
= TREE_OPERAND (incr
, 2);
16047 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16048 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16049 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16050 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16051 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16055 incr
= RECUR (incr
);
16058 incr
= RECUR (incr
);
16062 TREE_VEC_ELT (declv
, i
) = decl
;
16063 TREE_VEC_ELT (initv
, i
) = init
;
16064 TREE_VEC_ELT (condv
, i
) = cond
;
16065 TREE_VEC_ELT (incrv
, i
) = incr
;
16069 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16070 of OMP_TARGET's body. */
16073 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16075 *walk_subtrees
= 0;
16076 switch (TREE_CODE (*tp
))
16081 case STATEMENT_LIST
:
16082 *walk_subtrees
= 1;
16090 /* Helper function for tsubst_expr. For decomposition declaration
16091 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16092 also the corresponding decls representing the identifiers
16093 of the decomposition declaration. Return DECL if successful
16094 or error_mark_node otherwise, set *FIRST to the first decl
16095 in the list chained through DECL_CHAIN and *CNT to the number
16099 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16100 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16103 tree decl2
, decl3
, prev
= decl
;
16105 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16106 for (decl2
= DECL_CHAIN (pattern_decl
);
16109 && DECL_DECOMPOSITION_P (decl2
)
16110 && DECL_NAME (decl2
);
16111 decl2
= DECL_CHAIN (decl2
))
16113 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16115 gcc_assert (errorcount
);
16116 return error_mark_node
;
16119 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16120 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16121 tree v
= DECL_VALUE_EXPR (decl2
);
16122 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16123 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16124 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16125 SET_DECL_VALUE_EXPR (decl2
, v
);
16126 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16128 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16129 maybe_push_decl (decl3
);
16130 if (error_operand_p (decl3
))
16131 decl
= error_mark_node
;
16132 else if (decl
!= error_mark_node
16133 && DECL_CHAIN (decl3
) != prev
)
16135 gcc_assert (errorcount
);
16136 decl
= error_mark_node
;
16145 /* Like tsubst_copy for expressions, etc. but also does semantic
16149 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16150 bool integral_constant_expression_p
)
16152 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16153 #define RECUR(NODE) \
16154 tsubst_expr ((NODE), args, complain, in_decl, \
16155 integral_constant_expression_p)
16161 if (t
== NULL_TREE
|| t
== error_mark_node
)
16164 loc
= input_location
;
16165 if (EXPR_HAS_LOCATION (t
))
16166 input_location
= EXPR_LOCATION (t
);
16167 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16168 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16170 switch (TREE_CODE (t
))
16172 case STATEMENT_LIST
:
16174 tree_stmt_iterator i
;
16175 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16176 RECUR (tsi_stmt (i
));
16180 case CTOR_INITIALIZER
:
16181 finish_mem_initializers (tsubst_initializer_list
16182 (TREE_OPERAND (t
, 0), args
));
16186 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16190 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16191 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16192 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16194 finish_expr_stmt (tmp
);
16198 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16199 /*attribs=*/NULL_TREE
);
16204 tree decl
, pattern_decl
;
16207 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16208 if (TREE_CODE (decl
) == LABEL_DECL
)
16209 finish_label_decl (DECL_NAME (decl
));
16210 else if (TREE_CODE (decl
) == USING_DECL
)
16212 tree scope
= USING_DECL_SCOPE (decl
);
16213 tree name
= DECL_NAME (decl
);
16215 scope
= tsubst (scope
, args
, complain
, in_decl
);
16216 decl
= lookup_qualified_name (scope
, name
,
16217 /*is_type_p=*/false,
16218 /*complain=*/false);
16219 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16220 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16222 finish_local_using_decl (decl
, scope
, name
);
16224 else if (is_capture_proxy (decl
)
16225 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16227 /* We're in tsubst_lambda_expr, we've already inserted a new
16228 capture proxy, so look it up and register it. */
16230 if (DECL_PACK_P (decl
))
16232 inst
= (retrieve_local_specialization
16233 (DECL_CAPTURED_VARIABLE (decl
)));
16234 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16238 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16239 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16240 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16242 register_local_specialization (inst
, decl
);
16245 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16246 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16247 /* Don't copy the old closure; we'll create a new one in
16248 tsubst_lambda_expr. */
16252 init
= DECL_INITIAL (decl
);
16253 decl
= tsubst (decl
, args
, complain
, in_decl
);
16254 if (decl
!= error_mark_node
)
16256 /* By marking the declaration as instantiated, we avoid
16257 trying to instantiate it. Since instantiate_decl can't
16258 handle local variables, and since we've already done
16259 all that needs to be done, that's the right thing to
16262 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16264 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16265 /* Anonymous aggregates are a special case. */
16266 finish_anon_union (decl
);
16267 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16269 DECL_CONTEXT (decl
) = current_function_decl
;
16270 if (DECL_NAME (decl
) == this_identifier
)
16272 tree lam
= DECL_CONTEXT (current_function_decl
);
16273 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16274 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16276 insert_capture_proxy (decl
);
16278 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16279 /* We already did a pushtag. */;
16280 else if (TREE_CODE (decl
) == FUNCTION_DECL
16281 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16282 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16284 DECL_CONTEXT (decl
) = NULL_TREE
;
16286 DECL_CONTEXT (decl
) = current_function_decl
;
16287 cp_check_omp_declare_reduction (decl
);
16291 int const_init
= false;
16292 maybe_push_decl (decl
);
16294 && DECL_PRETTY_FUNCTION_P (decl
))
16296 /* For __PRETTY_FUNCTION__ we have to adjust the
16298 const char *const name
16299 = cxx_printable_name (current_function_decl
, 2);
16300 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16303 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16306 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16309 && DECL_DECOMPOSITION_P (decl
)
16310 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16315 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16316 complain
, in_decl
, &first
, &cnt
);
16317 if (ndecl
!= error_mark_node
)
16318 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16319 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16320 if (ndecl
!= error_mark_node
)
16321 cp_finish_decomp (ndecl
, first
, cnt
);
16324 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16333 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16334 RECUR (FOR_INIT_STMT (t
));
16335 finish_init_stmt (stmt
);
16336 tmp
= RECUR (FOR_COND (t
));
16337 finish_for_cond (tmp
, stmt
, false, 0);
16338 tmp
= RECUR (FOR_EXPR (t
));
16339 finish_for_expr (tmp
, stmt
);
16341 bool prev
= note_iteration_stmt_body_start ();
16342 RECUR (FOR_BODY (t
));
16343 note_iteration_stmt_body_end (prev
);
16345 finish_for_stmt (stmt
);
16348 case RANGE_FOR_STMT
:
16350 /* Construct another range_for, if this is not a final
16351 substitution (for inside inside a generic lambda of a
16352 template). Otherwise convert to a regular for. */
16354 stmt
= (processing_template_decl
16355 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16356 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16357 decl
= RANGE_FOR_DECL (t
);
16358 decl
= tsubst (decl
, args
, complain
, in_decl
);
16359 maybe_push_decl (decl
);
16360 expr
= RECUR (RANGE_FOR_EXPR (t
));
16362 tree decomp_first
= NULL_TREE
;
16363 unsigned decomp_cnt
= 0;
16364 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16365 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16367 &decomp_first
, &decomp_cnt
);
16369 if (processing_template_decl
)
16371 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16372 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16373 finish_range_for_decl (stmt
, decl
, expr
);
16377 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16378 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16379 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16380 decomp_first
, decomp_cnt
,
16381 RANGE_FOR_IVDEP (t
), unroll
);
16384 bool prev
= note_iteration_stmt_body_start ();
16385 RECUR (RANGE_FOR_BODY (t
));
16386 note_iteration_stmt_body_end (prev
);
16387 finish_for_stmt (stmt
);
16392 stmt
= begin_while_stmt ();
16393 tmp
= RECUR (WHILE_COND (t
));
16394 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16396 bool prev
= note_iteration_stmt_body_start ();
16397 RECUR (WHILE_BODY (t
));
16398 note_iteration_stmt_body_end (prev
);
16400 finish_while_stmt (stmt
);
16404 stmt
= begin_do_stmt ();
16406 bool prev
= note_iteration_stmt_body_start ();
16407 RECUR (DO_BODY (t
));
16408 note_iteration_stmt_body_end (prev
);
16410 finish_do_body (stmt
);
16411 tmp
= RECUR (DO_COND (t
));
16412 finish_do_stmt (tmp
, stmt
, false, 0);
16416 stmt
= begin_if_stmt ();
16417 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16418 tmp
= RECUR (IF_COND (t
));
16419 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16420 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16421 /* Don't instantiate the THEN_CLAUSE. */;
16424 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16426 ++c_inhibit_evaluation_warnings
;
16427 RECUR (THEN_CLAUSE (t
));
16429 --c_inhibit_evaluation_warnings
;
16431 finish_then_clause (stmt
);
16433 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16434 /* Don't instantiate the ELSE_CLAUSE. */;
16435 else if (ELSE_CLAUSE (t
))
16437 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16438 begin_else_clause (stmt
);
16440 ++c_inhibit_evaluation_warnings
;
16441 RECUR (ELSE_CLAUSE (t
));
16443 --c_inhibit_evaluation_warnings
;
16444 finish_else_clause (stmt
);
16447 finish_if_stmt (stmt
);
16451 if (BIND_EXPR_BODY_BLOCK (t
))
16452 stmt
= begin_function_body ();
16454 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16455 ? BCS_TRY_BLOCK
: 0);
16457 RECUR (BIND_EXPR_BODY (t
));
16459 if (BIND_EXPR_BODY_BLOCK (t
))
16460 finish_function_body (stmt
);
16462 finish_compound_stmt (stmt
);
16466 finish_break_stmt ();
16469 case CONTINUE_STMT
:
16470 finish_continue_stmt ();
16474 stmt
= begin_switch_stmt ();
16475 tmp
= RECUR (SWITCH_STMT_COND (t
));
16476 finish_switch_cond (tmp
, stmt
);
16477 RECUR (SWITCH_STMT_BODY (t
));
16478 finish_switch_stmt (stmt
);
16481 case CASE_LABEL_EXPR
:
16483 tree low
= RECUR (CASE_LOW (t
));
16484 tree high
= RECUR (CASE_HIGH (t
));
16485 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16486 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16487 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16488 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16494 tree decl
= LABEL_EXPR_LABEL (t
);
16497 label
= finish_label_stmt (DECL_NAME (decl
));
16498 if (TREE_CODE (label
) == LABEL_DECL
)
16499 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16500 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16501 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16506 tmp
= GOTO_DESTINATION (t
);
16507 if (TREE_CODE (tmp
) != LABEL_DECL
)
16508 /* Computed goto's must be tsubst'd into. On the other hand,
16509 non-computed gotos must not be; the identifier in question
16510 will have no binding. */
16513 tmp
= DECL_NAME (tmp
);
16514 finish_goto_stmt (tmp
);
16519 tree string
= RECUR (ASM_STRING (t
));
16520 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16521 complain
, in_decl
);
16522 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16523 complain
, in_decl
);
16524 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16525 complain
, in_decl
);
16526 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16527 complain
, in_decl
);
16528 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16530 tree asm_expr
= tmp
;
16531 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16532 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16533 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16540 stmt
= begin_try_block ();
16541 RECUR (TRY_STMTS (t
));
16542 finish_cleanup_try_block (stmt
);
16543 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16547 tree compound_stmt
= NULL_TREE
;
16549 if (FN_TRY_BLOCK_P (t
))
16550 stmt
= begin_function_try_block (&compound_stmt
);
16552 stmt
= begin_try_block ();
16554 RECUR (TRY_STMTS (t
));
16556 if (FN_TRY_BLOCK_P (t
))
16557 finish_function_try_block (stmt
);
16559 finish_try_block (stmt
);
16561 RECUR (TRY_HANDLERS (t
));
16562 if (FN_TRY_BLOCK_P (t
))
16563 finish_function_handler_sequence (stmt
, compound_stmt
);
16565 finish_handler_sequence (stmt
);
16571 tree decl
= HANDLER_PARMS (t
);
16575 decl
= tsubst (decl
, args
, complain
, in_decl
);
16576 /* Prevent instantiate_decl from trying to instantiate
16577 this variable. We've already done all that needs to be
16579 if (decl
!= error_mark_node
)
16580 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16582 stmt
= begin_handler ();
16583 finish_handler_parms (decl
, stmt
);
16584 RECUR (HANDLER_BODY (t
));
16585 finish_handler (stmt
);
16590 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16591 if (CLASS_TYPE_P (tmp
))
16593 /* Local classes are not independent templates; they are
16594 instantiated along with their containing function. And this
16595 way we don't have to deal with pushing out of one local class
16596 to instantiate a member of another local class. */
16597 /* Closures are handled by the LAMBDA_EXPR. */
16598 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16599 complete_type (tmp
);
16600 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16602 || (TREE_CODE (fld
) == FUNCTION_DECL
16603 && !DECL_ARTIFICIAL (fld
)))
16604 && DECL_TEMPLATE_INSTANTIATION (fld
))
16605 instantiate_decl (fld
, /*defer_ok=*/false,
16606 /*expl_inst_class=*/false);
16610 case STATIC_ASSERT
:
16614 ++c_inhibit_evaluation_warnings
;
16616 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16619 /*integral_constant_expression_p=*/true);
16620 --c_inhibit_evaluation_warnings
;
16622 finish_static_assert (condition
,
16623 STATIC_ASSERT_MESSAGE (t
),
16624 STATIC_ASSERT_SOURCE_LOCATION (t
),
16625 /*member_p=*/false);
16630 case OACC_PARALLEL
:
16631 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16633 stmt
= begin_omp_parallel ();
16634 RECUR (OMP_BODY (t
));
16635 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16639 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16640 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16641 complain
, in_decl
);
16642 if (OMP_PARALLEL_COMBINED (t
))
16643 omp_parallel_combined_clauses
= &tmp
;
16644 stmt
= begin_omp_parallel ();
16645 RECUR (OMP_PARALLEL_BODY (t
));
16646 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16647 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16648 = OMP_PARALLEL_COMBINED (t
);
16649 pop_omp_privatization_clauses (r
);
16653 r
= push_omp_privatization_clauses (false);
16654 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16655 complain
, in_decl
);
16656 stmt
= begin_omp_task ();
16657 RECUR (OMP_TASK_BODY (t
));
16658 finish_omp_task (tmp
, stmt
);
16659 pop_omp_privatization_clauses (r
);
16664 case OMP_DISTRIBUTE
:
16668 tree clauses
, body
, pre_body
;
16669 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16670 tree orig_declv
= NULL_TREE
;
16671 tree incrv
= NULL_TREE
;
16672 enum c_omp_region_type ort
= C_ORT_OMP
;
16675 if (TREE_CODE (t
) == OACC_LOOP
)
16678 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16679 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16681 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16683 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16684 if (OMP_FOR_ORIG_DECLS (t
))
16685 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16686 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16687 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16688 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16691 stmt
= begin_omp_structured_block ();
16693 pre_body
= push_stmt_list ();
16694 RECUR (OMP_FOR_PRE_BODY (t
));
16695 pre_body
= pop_stmt_list (pre_body
);
16697 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16698 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16699 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16700 incrv
, &clauses
, args
, complain
, in_decl
,
16701 integral_constant_expression_p
);
16702 omp_parallel_combined_clauses
= NULL
;
16704 body
= push_stmt_list ();
16705 RECUR (OMP_FOR_BODY (t
));
16706 body
= pop_stmt_list (body
);
16708 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16709 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16710 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16714 t
= make_node (TREE_CODE (t
));
16715 TREE_TYPE (t
) = void_type_node
;
16716 OMP_FOR_BODY (t
) = body
;
16717 OMP_FOR_PRE_BODY (t
) = pre_body
;
16718 OMP_FOR_CLAUSES (t
) = clauses
;
16719 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16723 add_stmt (finish_omp_structured_block (stmt
));
16724 pop_omp_privatization_clauses (r
);
16729 omp_parallel_combined_clauses
= NULL
;
16734 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16735 && OMP_TEAMS_COMBINED (t
));
16736 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16738 stmt
= push_stmt_list ();
16739 RECUR (OMP_BODY (t
));
16740 stmt
= pop_stmt_list (stmt
);
16743 OMP_BODY (t
) = stmt
;
16744 OMP_CLAUSES (t
) = tmp
;
16746 pop_omp_privatization_clauses (r
);
16750 case OMP_TARGET_DATA
:
16752 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16753 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16755 keep_next_level (true);
16756 stmt
= begin_omp_structured_block ();
16758 RECUR (OMP_BODY (t
));
16759 stmt
= finish_omp_structured_block (stmt
);
16762 OMP_BODY (t
) = stmt
;
16763 OMP_CLAUSES (t
) = tmp
;
16764 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16766 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16769 /* For combined target teams, ensure the num_teams and
16770 thread_limit clause expressions are evaluated on the host,
16771 before entering the target construct. */
16773 for (c
= OMP_TEAMS_CLAUSES (teams
);
16774 c
; c
= OMP_CLAUSE_CHAIN (c
))
16775 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16776 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16777 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16779 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16780 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16781 if (expr
== error_mark_node
)
16783 tmp
= TARGET_EXPR_SLOT (expr
);
16785 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16786 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16787 OMP_CLAUSE_FIRSTPRIVATE
);
16788 OMP_CLAUSE_DECL (tc
) = tmp
;
16789 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16790 OMP_TARGET_CLAUSES (t
) = tc
;
16799 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16800 complain
, in_decl
);
16801 OACC_DECLARE_CLAUSES (t
) = tmp
;
16805 case OMP_TARGET_UPDATE
:
16806 case OMP_TARGET_ENTER_DATA
:
16807 case OMP_TARGET_EXIT_DATA
:
16808 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16809 complain
, in_decl
);
16811 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16815 case OACC_ENTER_DATA
:
16816 case OACC_EXIT_DATA
:
16818 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16819 complain
, in_decl
);
16821 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16826 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16827 complain
, in_decl
);
16828 stmt
= push_stmt_list ();
16829 RECUR (OMP_BODY (t
));
16830 stmt
= pop_stmt_list (stmt
);
16833 OMP_BODY (t
) = stmt
;
16834 OMP_ORDERED_CLAUSES (t
) = tmp
;
16840 case OMP_TASKGROUP
:
16841 stmt
= push_stmt_list ();
16842 RECUR (OMP_BODY (t
));
16843 stmt
= pop_stmt_list (stmt
);
16846 OMP_BODY (t
) = stmt
;
16851 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16852 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16854 tree op1
= TREE_OPERAND (t
, 1);
16855 tree rhs1
= NULL_TREE
;
16857 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16859 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16860 op1
= TREE_OPERAND (op1
, 1);
16862 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16863 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16864 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16865 NULL_TREE
, NULL_TREE
, rhs1
,
16866 OMP_ATOMIC_SEQ_CST (t
));
16870 tree op1
= TREE_OPERAND (t
, 1);
16871 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16872 tree rhs1
= NULL_TREE
;
16873 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16874 enum tree_code opcode
= NOP_EXPR
;
16875 if (code
== OMP_ATOMIC_READ
)
16877 v
= RECUR (TREE_OPERAND (op1
, 0));
16878 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16880 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16881 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16883 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16884 v
= RECUR (TREE_OPERAND (op1
, 0));
16885 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16886 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16888 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16889 op11
= TREE_OPERAND (op11
, 1);
16891 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16892 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16893 opcode
= TREE_CODE (op11
);
16894 if (opcode
== MODIFY_EXPR
)
16900 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16901 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16903 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16904 OMP_ATOMIC_SEQ_CST (t
));
16908 case TRANSACTION_EXPR
:
16911 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16912 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16914 if (TRANSACTION_EXPR_IS_STMT (t
))
16916 tree body
= TRANSACTION_EXPR_BODY (t
);
16917 tree noex
= NULL_TREE
;
16918 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16920 noex
= MUST_NOT_THROW_COND (body
);
16921 if (noex
== NULL_TREE
)
16922 noex
= boolean_true_node
;
16923 body
= TREE_OPERAND (body
, 0);
16925 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16927 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16931 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16932 RECUR (TRANSACTION_EXPR_BODY (t
)),
16939 case MUST_NOT_THROW_EXPR
:
16941 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16942 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16943 RETURN (build_must_not_throw_expr (op0
, cond
));
16946 case EXPR_PACK_EXPANSION
:
16947 error ("invalid use of pack expansion expression");
16948 RETURN (error_mark_node
);
16950 case NONTYPE_ARGUMENT_PACK
:
16951 error ("use %<...%> to expand argument pack");
16952 RETURN (error_mark_node
);
16954 case COMPOUND_EXPR
:
16955 tmp
= RECUR (TREE_OPERAND (t
, 0));
16956 if (tmp
== NULL_TREE
)
16957 /* If the first operand was a statement, we're done with it. */
16958 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16959 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16960 RECUR (TREE_OPERAND (t
, 1)),
16963 case ANNOTATE_EXPR
:
16964 tmp
= RECUR (TREE_OPERAND (t
, 0));
16965 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16966 TREE_TYPE (tmp
), tmp
,
16967 RECUR (TREE_OPERAND (t
, 1)),
16968 RECUR (TREE_OPERAND (t
, 2))));
16971 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16973 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16974 /*function_p=*/false,
16975 integral_constant_expression_p
));
16978 RETURN (NULL_TREE
);
16980 input_location
= loc
;
16986 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16987 function. For description of the body see comment above
16988 cp_parser_omp_declare_reduction_exprs. */
16991 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16993 if (t
== NULL_TREE
|| t
== error_mark_node
)
16996 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16998 tree_stmt_iterator tsi
;
17001 memset (stmts
, 0, sizeof stmts
);
17002 for (i
= 0, tsi
= tsi_start (t
);
17003 i
< 7 && !tsi_end_p (tsi
);
17004 i
++, tsi_next (&tsi
))
17005 stmts
[i
] = tsi_stmt (tsi
);
17006 gcc_assert (tsi_end_p (tsi
));
17010 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17011 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17012 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17013 args
, complain
, in_decl
);
17014 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17015 args
, complain
, in_decl
);
17016 DECL_CONTEXT (omp_out
) = current_function_decl
;
17017 DECL_CONTEXT (omp_in
) = current_function_decl
;
17018 keep_next_level (true);
17019 tree block
= begin_omp_structured_block ();
17020 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17021 block
= finish_omp_structured_block (block
);
17022 block
= maybe_cleanup_point_expr_void (block
);
17023 add_decl_expr (omp_out
);
17024 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17025 TREE_NO_WARNING (omp_out
) = 1;
17026 add_decl_expr (omp_in
);
17027 finish_expr_stmt (block
);
17031 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17032 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17033 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17034 args
, complain
, in_decl
);
17035 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17036 args
, complain
, in_decl
);
17037 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17038 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17039 keep_next_level (true);
17040 tree block
= begin_omp_structured_block ();
17041 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17042 block
= finish_omp_structured_block (block
);
17043 block
= maybe_cleanup_point_expr_void (block
);
17044 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17045 add_decl_expr (omp_priv
);
17046 add_decl_expr (omp_orig
);
17047 finish_expr_stmt (block
);
17049 add_decl_expr (omp_orig
);
17053 /* T is a postfix-expression that is not being used in a function
17054 call. Return the substituted version of T. */
17057 tsubst_non_call_postfix_expression (tree t
, tree args
,
17058 tsubst_flags_t complain
,
17061 if (TREE_CODE (t
) == SCOPE_REF
)
17062 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17063 /*done=*/false, /*address_p=*/false);
17065 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17066 /*function_p=*/false,
17067 /*integral_constant_expression_p=*/false);
17072 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17073 instantiation context. Instantiating a pack expansion containing a lambda
17074 might result in multiple lambdas all based on the same lambda in the
17078 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17080 tree oldfn
= lambda_function (t
);
17083 tree r
= build_lambda_expr ();
17085 LAMBDA_EXPR_LOCATION (r
)
17086 = LAMBDA_EXPR_LOCATION (t
);
17087 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17088 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17089 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17091 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17092 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
17094 record_lambda_scope (r
);
17096 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17097 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17099 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17100 cap
= TREE_CHAIN (cap
))
17102 tree field
= TREE_PURPOSE (cap
);
17103 if (PACK_EXPANSION_P (field
))
17104 field
= PACK_EXPANSION_PATTERN (field
);
17105 field
= tsubst_decl (field
, args
, complain
);
17107 if (field
== error_mark_node
)
17108 return error_mark_node
;
17110 tree init
= TREE_VALUE (cap
);
17111 if (PACK_EXPANSION_P (init
))
17112 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17114 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17115 /*fn*/false, /*constexpr*/false);
17117 if (TREE_CODE (field
) == TREE_VEC
)
17119 int len
= TREE_VEC_LENGTH (field
);
17120 gcc_assert (TREE_CODE (init
) == TREE_VEC
17121 && TREE_VEC_LENGTH (init
) == len
);
17122 for (int i
= 0; i
< len
; ++i
)
17123 LAMBDA_EXPR_CAPTURE_LIST (r
)
17124 = tree_cons (TREE_VEC_ELT (field
, i
),
17125 TREE_VEC_ELT (init
, i
),
17126 LAMBDA_EXPR_CAPTURE_LIST (r
));
17130 LAMBDA_EXPR_CAPTURE_LIST (r
)
17131 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17133 if (id_equal (DECL_NAME (field
), "__this"))
17134 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17138 tree type
= begin_lambda_type (r
);
17140 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17141 determine_visibility (TYPE_NAME (type
));
17143 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17145 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17146 ? DECL_TI_TEMPLATE (oldfn
)
17149 tree fntype
= static_fn_type (oldfn
);
17151 ++processing_template_decl
;
17152 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17154 --processing_template_decl
;
17156 if (fntype
== error_mark_node
)
17157 r
= error_mark_node
;
17160 /* Fix the type of 'this'. */
17161 fntype
= build_memfn_type (fntype
, type
,
17162 type_memfn_quals (fntype
),
17163 type_memfn_rqual (fntype
));
17167 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17168 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17169 finish_member_declaration (tmpl
);
17174 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17175 finish_member_declaration (fn
);
17178 /* Let finish_function set this. */
17179 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17181 bool nested
= cfun
;
17183 push_function_context ();
17185 /* Still increment function_depth so that we don't GC in the
17186 middle of an expression. */
17189 local_specialization_stack
s (lss_copy
);
17191 tree body
= start_lambda_function (fn
, r
);
17193 register_parameter_specializations (oldfn
, fn
);
17195 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17196 /*constexpr*/false);
17198 finish_lambda_function (body
);
17201 pop_function_context ();
17205 /* The capture list was built up in reverse order; fix that now. */
17206 LAMBDA_EXPR_CAPTURE_LIST (r
)
17207 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17209 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17211 maybe_add_lambda_conv_op (type
);
17214 finish_struct (type
, /*attr*/NULL_TREE
);
17216 insert_pending_capture_proxies ();
17221 /* Like tsubst but deals with expressions and performs semantic
17222 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17225 tsubst_copy_and_build (tree t
,
17227 tsubst_flags_t complain
,
17230 bool integral_constant_expression_p
)
17232 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17233 #define RECUR(NODE) \
17234 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17235 /*function_p=*/false, \
17236 integral_constant_expression_p)
17241 if (t
== NULL_TREE
|| t
== error_mark_node
)
17244 loc
= input_location
;
17245 if (EXPR_HAS_LOCATION (t
))
17246 input_location
= EXPR_LOCATION (t
);
17248 /* N3276 decltype magic only applies to calls at the top level or on the
17249 right side of a comma. */
17250 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17251 complain
&= ~tf_decltype
;
17253 switch (TREE_CODE (t
))
17257 /* Fall through. */
17258 case IDENTIFIER_NODE
:
17262 bool non_integral_constant_expression_p
;
17263 const char *error_msg
;
17265 if (IDENTIFIER_CONV_OP_P (t
))
17267 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17268 t
= make_conv_op_name (new_type
);
17271 /* Look up the name. */
17272 decl
= lookup_name (t
);
17274 /* By convention, expressions use ERROR_MARK_NODE to indicate
17275 failure, not NULL_TREE. */
17276 if (decl
== NULL_TREE
)
17277 decl
= error_mark_node
;
17279 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17281 integral_constant_expression_p
,
17282 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17283 &non_integral_constant_expression_p
,
17284 /*template_p=*/false,
17286 /*address_p=*/false,
17287 /*template_arg_p=*/false,
17292 if (!function_p
&& identifier_p (decl
))
17294 if (complain
& tf_error
)
17295 unqualified_name_lookup_error (decl
);
17296 decl
= error_mark_node
;
17301 case TEMPLATE_ID_EXPR
:
17304 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17305 tree targs
= TREE_OPERAND (t
, 1);
17308 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17309 if (targs
== error_mark_node
)
17310 return error_mark_node
;
17312 if (TREE_CODE (templ
) == SCOPE_REF
)
17314 tree name
= TREE_OPERAND (templ
, 1);
17315 tree tid
= lookup_template_function (name
, targs
);
17316 TREE_OPERAND (templ
, 1) = tid
;
17320 if (variable_template_p (templ
))
17321 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17323 if (TREE_CODE (templ
) == COMPONENT_REF
)
17325 object
= TREE_OPERAND (templ
, 0);
17326 templ
= TREE_OPERAND (templ
, 1);
17329 object
= NULL_TREE
;
17330 templ
= lookup_template_function (templ
, targs
);
17333 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17334 object
, templ
, NULL_TREE
));
17336 RETURN (baselink_for_fns (templ
));
17341 tree r
= RECUR (TREE_OPERAND (t
, 0));
17343 if (REFERENCE_REF_P (t
))
17345 /* A type conversion to reference type will be enclosed in
17346 such an indirect ref, but the substitution of the cast
17347 will have also added such an indirect ref. */
17348 r
= convert_from_reference (r
);
17351 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17352 complain
|decltype_flag
);
17354 if (REF_PARENTHESIZED_P (t
))
17355 r
= force_paren_expr (r
);
17362 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17363 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17364 RETURN (build_nop (type
, op0
));
17367 case IMPLICIT_CONV_EXPR
:
17369 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17370 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17371 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17373 retval
= copy_node (t
);
17374 TREE_TYPE (retval
) = type
;
17375 TREE_OPERAND (retval
, 0) = expr
;
17378 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17379 /* We'll pass this to convert_nontype_argument again, we don't need
17380 to actually perform any conversion here. */
17382 int flags
= LOOKUP_IMPLICIT
;
17383 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17384 flags
= LOOKUP_NORMAL
;
17385 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17391 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17392 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17393 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17397 case REINTERPRET_CAST_EXPR
:
17398 case CONST_CAST_EXPR
:
17399 case DYNAMIC_CAST_EXPR
:
17400 case STATIC_CAST_EXPR
:
17403 tree op
, r
= NULL_TREE
;
17405 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17406 if (integral_constant_expression_p
17407 && !cast_valid_in_integral_constant_expression_p (type
))
17409 if (complain
& tf_error
)
17410 error ("a cast to a type other than an integral or "
17411 "enumeration type cannot appear in a constant-expression");
17412 RETURN (error_mark_node
);
17415 op
= RECUR (TREE_OPERAND (t
, 0));
17417 warning_sentinel
s(warn_useless_cast
);
17418 warning_sentinel
s2(warn_ignored_qualifiers
);
17419 switch (TREE_CODE (t
))
17422 r
= build_functional_cast (type
, op
, complain
);
17424 case REINTERPRET_CAST_EXPR
:
17425 r
= build_reinterpret_cast (type
, op
, complain
);
17427 case CONST_CAST_EXPR
:
17428 r
= build_const_cast (type
, op
, complain
);
17430 case DYNAMIC_CAST_EXPR
:
17431 r
= build_dynamic_cast (type
, op
, complain
);
17433 case STATIC_CAST_EXPR
:
17434 r
= build_static_cast (type
, op
, complain
);
17437 gcc_unreachable ();
17443 case POSTDECREMENT_EXPR
:
17444 case POSTINCREMENT_EXPR
:
17445 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17446 args
, complain
, in_decl
);
17447 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17448 complain
|decltype_flag
));
17450 case PREDECREMENT_EXPR
:
17451 case PREINCREMENT_EXPR
:
17455 case TRUTH_NOT_EXPR
:
17456 case UNARY_PLUS_EXPR
: /* Unary + */
17457 case REALPART_EXPR
:
17458 case IMAGPART_EXPR
:
17459 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17460 RECUR (TREE_OPERAND (t
, 0)),
17461 complain
|decltype_flag
));
17463 case FIX_TRUNC_EXPR
:
17464 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17468 op1
= TREE_OPERAND (t
, 0);
17469 if (TREE_CODE (op1
) == LABEL_DECL
)
17470 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17471 EXPR_LOCATION (op1
)));
17472 if (TREE_CODE (op1
) == SCOPE_REF
)
17473 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17474 /*done=*/true, /*address_p=*/true);
17476 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17478 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17479 complain
|decltype_flag
));
17484 case TRUNC_DIV_EXPR
:
17485 case CEIL_DIV_EXPR
:
17486 case FLOOR_DIV_EXPR
:
17487 case ROUND_DIV_EXPR
:
17488 case EXACT_DIV_EXPR
:
17492 case TRUNC_MOD_EXPR
:
17493 case FLOOR_MOD_EXPR
:
17494 case TRUTH_ANDIF_EXPR
:
17495 case TRUTH_ORIF_EXPR
:
17496 case TRUTH_AND_EXPR
:
17497 case TRUTH_OR_EXPR
:
17513 warning_sentinel
s1(warn_type_limits
);
17514 warning_sentinel
s2(warn_div_by_zero
);
17515 warning_sentinel
s3(warn_logical_op
);
17516 warning_sentinel
s4(warn_tautological_compare
);
17517 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17518 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17519 tree r
= build_x_binary_op
17520 (input_location
, TREE_CODE (t
),
17522 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17524 : TREE_CODE (TREE_OPERAND (t
, 0))),
17526 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17528 : TREE_CODE (TREE_OPERAND (t
, 1))),
17530 complain
|decltype_flag
);
17531 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17532 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17537 case POINTER_PLUS_EXPR
:
17539 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17540 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17541 return fold_build_pointer_plus (op0
, op1
);
17545 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17546 /*address_p=*/false));
17548 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17549 args
, complain
, in_decl
);
17550 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17551 RECUR (TREE_OPERAND (t
, 1)),
17552 complain
|decltype_flag
));
17555 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17556 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17557 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17564 op1
= TREE_OPERAND (t
, 0);
17565 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17566 op1
= TREE_TYPE (op1
);
17569 /* When there are no ARGS, we are trying to evaluate a
17570 non-dependent expression from the parser. Trying to do
17571 the substitutions may not work. */
17573 op1
= TREE_TYPE (op1
);
17577 ++cp_unevaluated_operand
;
17578 ++c_inhibit_evaluation_warnings
;
17580 op1
= tsubst (op1
, args
, complain
, in_decl
);
17582 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17583 /*function_p=*/false,
17584 /*integral_constant_expression_p=*/
17586 --cp_unevaluated_operand
;
17587 --c_inhibit_evaluation_warnings
;
17590 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17591 complain
& tf_error
);
17593 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17594 complain
& tf_error
);
17595 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17597 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17599 if (!processing_template_decl
&& TYPE_P (op1
))
17601 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17602 build1 (NOP_EXPR
, op1
, error_mark_node
));
17603 SIZEOF_EXPR_TYPE_P (r
) = 1;
17606 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17607 TREE_SIDE_EFFECTS (r
) = 0;
17608 TREE_READONLY (r
) = 1;
17610 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17615 case AT_ENCODE_EXPR
:
17617 op1
= TREE_OPERAND (t
, 0);
17618 ++cp_unevaluated_operand
;
17619 ++c_inhibit_evaluation_warnings
;
17620 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17621 /*function_p=*/false,
17622 /*integral_constant_expression_p=*/false);
17623 --cp_unevaluated_operand
;
17624 --c_inhibit_evaluation_warnings
;
17625 RETURN (objc_build_encode_expr (op1
));
17628 case NOEXCEPT_EXPR
:
17629 op1
= TREE_OPERAND (t
, 0);
17630 ++cp_unevaluated_operand
;
17631 ++c_inhibit_evaluation_warnings
;
17632 ++cp_noexcept_operand
;
17633 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17634 /*function_p=*/false,
17635 /*integral_constant_expression_p=*/false);
17636 --cp_unevaluated_operand
;
17637 --c_inhibit_evaluation_warnings
;
17638 --cp_noexcept_operand
;
17639 RETURN (finish_noexcept_expr (op1
, complain
));
17643 warning_sentinel
s(warn_div_by_zero
);
17644 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17645 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17646 tree r
= build_x_modify_expr
17647 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17648 complain
|decltype_flag
);
17649 /* TREE_NO_WARNING must be set if either the expression was
17650 parenthesized or it uses an operator such as >>= rather
17651 than plain assignment. In the former case, it was already
17652 set and must be copied. In the latter case,
17653 build_x_modify_expr sets it and it must not be reset
17655 if (TREE_NO_WARNING (t
))
17656 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17662 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17663 args
, complain
, in_decl
);
17664 /* Remember that there was a reference to this entity. */
17666 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17667 RETURN (error_mark_node
);
17668 RETURN (build_x_arrow (input_location
, op1
, complain
));
17672 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17673 tree init
= RECUR (TREE_OPERAND (t
, 3));
17674 vec
<tree
, va_gc
> *placement_vec
;
17675 vec
<tree
, va_gc
> *init_vec
;
17678 if (placement
== NULL_TREE
)
17679 placement_vec
= NULL
;
17682 placement_vec
= make_tree_vector ();
17683 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17684 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17687 /* If there was an initializer in the original tree, but it
17688 instantiated to an empty list, then we should pass a
17689 non-NULL empty vector to tell build_new that it was an
17690 empty initializer() rather than no initializer. This can
17691 only happen when the initializer is a pack expansion whose
17692 parameter packs are of length zero. */
17693 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17697 init_vec
= make_tree_vector ();
17698 if (init
== void_node
)
17699 gcc_assert (init_vec
!= NULL
);
17702 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17703 vec_safe_push (init_vec
, TREE_VALUE (init
));
17707 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17708 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17709 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17710 NEW_EXPR_USE_GLOBAL (t
),
17713 if (placement_vec
!= NULL
)
17714 release_tree_vector (placement_vec
);
17715 if (init_vec
!= NULL
)
17716 release_tree_vector (init_vec
);
17723 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17724 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17725 RETURN (delete_sanity (op0
, op1
,
17726 DELETE_EXPR_USE_VEC (t
),
17727 DELETE_EXPR_USE_GLOBAL (t
),
17731 case COMPOUND_EXPR
:
17733 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17734 complain
& ~tf_decltype
, in_decl
,
17735 /*function_p=*/false,
17736 integral_constant_expression_p
);
17737 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17739 RECUR (TREE_OPERAND (t
, 1)),
17740 complain
|decltype_flag
));
17746 vec
<tree
, va_gc
> *call_args
;
17747 unsigned int nargs
, i
;
17752 function
= CALL_EXPR_FN (t
);
17753 /* Internal function with no arguments. */
17754 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17757 /* When we parsed the expression, we determined whether or
17758 not Koenig lookup should be performed. */
17759 koenig_p
= KOENIG_LOOKUP_P (t
);
17760 if (function
== NULL_TREE
)
17763 qualified_p
= false;
17765 else if (TREE_CODE (function
) == SCOPE_REF
)
17767 qualified_p
= true;
17768 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17770 /*address_p=*/false);
17772 else if (koenig_p
&& identifier_p (function
))
17774 /* Do nothing; calling tsubst_copy_and_build on an identifier
17775 would incorrectly perform unqualified lookup again.
17777 Note that we can also have an IDENTIFIER_NODE if the earlier
17778 unqualified lookup found a member function; in that case
17779 koenig_p will be false and we do want to do the lookup
17780 again to find the instantiated member function.
17782 FIXME but doing that causes c++/15272, so we need to stop
17783 using IDENTIFIER_NODE in that situation. */
17784 qualified_p
= false;
17788 if (TREE_CODE (function
) == COMPONENT_REF
)
17790 tree op
= TREE_OPERAND (function
, 1);
17792 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17793 || (BASELINK_P (op
)
17794 && BASELINK_QUALIFIED_P (op
)));
17797 qualified_p
= false;
17799 if (TREE_CODE (function
) == ADDR_EXPR
17800 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17801 /* Avoid error about taking the address of a constructor. */
17802 function
= TREE_OPERAND (function
, 0);
17804 function
= tsubst_copy_and_build (function
, args
, complain
,
17807 integral_constant_expression_p
);
17809 if (BASELINK_P (function
))
17810 qualified_p
= true;
17813 nargs
= call_expr_nargs (t
);
17814 call_args
= make_tree_vector ();
17815 for (i
= 0; i
< nargs
; ++i
)
17817 tree arg
= CALL_EXPR_ARG (t
, i
);
17819 if (!PACK_EXPANSION_P (arg
))
17820 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17823 /* Expand the pack expansion and push each entry onto
17825 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17826 if (TREE_CODE (arg
) == TREE_VEC
)
17828 unsigned int len
, j
;
17830 len
= TREE_VEC_LENGTH (arg
);
17831 for (j
= 0; j
< len
; ++j
)
17833 tree value
= TREE_VEC_ELT (arg
, j
);
17834 if (value
!= NULL_TREE
)
17835 value
= convert_from_reference (value
);
17836 vec_safe_push (call_args
, value
);
17841 /* A partial substitution. Add one entry. */
17842 vec_safe_push (call_args
, arg
);
17847 /* We do not perform argument-dependent lookup if normal
17848 lookup finds a non-function, in accordance with the
17849 expected resolution of DR 218. */
17851 && ((is_overloaded_fn (function
)
17852 /* If lookup found a member function, the Koenig lookup is
17853 not appropriate, even if an unqualified-name was used
17854 to denote the function. */
17855 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17856 || identifier_p (function
))
17857 /* Only do this when substitution turns a dependent call
17858 into a non-dependent call. */
17859 && type_dependent_expression_p_push (t
)
17860 && !any_type_dependent_arguments_p (call_args
))
17861 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17863 if (function
!= NULL_TREE
17864 && identifier_p (function
)
17865 && !any_type_dependent_arguments_p (call_args
))
17867 if (koenig_p
&& (complain
& tf_warning_or_error
))
17869 /* For backwards compatibility and good diagnostics, try
17870 the unqualified lookup again if we aren't in SFINAE
17872 tree unq
= (tsubst_copy_and_build
17873 (function
, args
, complain
, in_decl
, true,
17874 integral_constant_expression_p
));
17875 if (unq
== error_mark_node
)
17877 release_tree_vector (call_args
);
17878 RETURN (error_mark_node
);
17881 if (unq
!= function
)
17883 /* In a lambda fn, we have to be careful to not
17884 introduce new this captures. Legacy code can't
17885 be using lambdas anyway, so it's ok to be
17887 bool in_lambda
= (current_class_type
17888 && LAMBDA_TYPE_P (current_class_type
));
17889 char const *const msg
17890 = G_("%qD was not declared in this scope, "
17891 "and no declarations were found by "
17892 "argument-dependent lookup at the point "
17893 "of instantiation");
17897 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17900 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17906 if (INDIRECT_REF_P (fn
))
17907 fn
= TREE_OPERAND (fn
, 0);
17908 if (is_overloaded_fn (fn
))
17909 fn
= get_first_fn (fn
);
17912 /* Can't say anything more. */;
17913 else if (DECL_CLASS_SCOPE_P (fn
))
17915 location_t loc
= EXPR_LOC_OR_LOC (t
,
17918 "declarations in dependent base %qT are "
17919 "not found by unqualified lookup",
17920 DECL_CLASS_CONTEXT (fn
));
17921 if (current_class_ptr
)
17923 "use %<this->%D%> instead", function
);
17926 "use %<%T::%D%> instead",
17927 current_class_name
, function
);
17930 inform (DECL_SOURCE_LOCATION (fn
),
17931 "%qD declared here, later in the "
17932 "translation unit", fn
);
17935 release_tree_vector (call_args
);
17936 RETURN (error_mark_node
);
17943 if (identifier_p (function
))
17945 if (complain
& tf_error
)
17946 unqualified_name_lookup_error (function
);
17947 release_tree_vector (call_args
);
17948 RETURN (error_mark_node
);
17952 /* Remember that there was a reference to this entity. */
17953 if (function
!= NULL_TREE
17954 && DECL_P (function
)
17955 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17957 release_tree_vector (call_args
);
17958 RETURN (error_mark_node
);
17961 /* Put back tf_decltype for the actual call. */
17962 complain
|= decltype_flag
;
17964 if (function
== NULL_TREE
)
17965 switch (CALL_EXPR_IFN (t
))
17968 gcc_assert (nargs
== 1);
17969 if (vec_safe_length (call_args
) != 1)
17971 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17972 "wrong number of arguments to "
17973 "%<__builtin_launder%>");
17974 ret
= error_mark_node
;
17977 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17979 (*call_args
)[0], complain
);
17983 /* Unsupported internal function with arguments. */
17984 gcc_unreachable ();
17986 else if (TREE_CODE (function
) == OFFSET_REF
)
17987 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17989 else if (TREE_CODE (function
) == COMPONENT_REF
)
17991 tree instance
= TREE_OPERAND (function
, 0);
17992 tree fn
= TREE_OPERAND (function
, 1);
17994 if (processing_template_decl
17995 && (type_dependent_expression_p (instance
)
17996 || (!BASELINK_P (fn
)
17997 && TREE_CODE (fn
) != FIELD_DECL
)
17998 || type_dependent_expression_p (fn
)
17999 || any_type_dependent_arguments_p (call_args
)))
18000 ret
= build_min_nt_call_vec (function
, call_args
);
18001 else if (!BASELINK_P (fn
))
18002 ret
= finish_call_expr (function
, &call_args
,
18003 /*disallow_virtual=*/false,
18004 /*koenig_p=*/false,
18007 ret
= (build_new_method_call
18009 &call_args
, NULL_TREE
,
18010 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18015 ret
= finish_call_expr (function
, &call_args
,
18016 /*disallow_virtual=*/qualified_p
,
18020 release_tree_vector (call_args
);
18022 if (ret
!= error_mark_node
)
18024 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18025 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18026 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18027 bool thk
= CALL_FROM_THUNK_P (t
);
18028 if (op
|| ord
|| rev
|| thk
)
18030 function
= extract_call_expr (ret
);
18031 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18032 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18033 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18036 if (TREE_CODE (function
) == CALL_EXPR
)
18037 CALL_FROM_THUNK_P (function
) = true;
18039 AGGR_INIT_FROM_THUNK_P (function
) = true;
18040 /* The thunk location is not interesting. */
18041 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
18051 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18052 tree folded_cond
= fold_non_dependent_expr (cond
);
18055 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
18057 if (integer_zerop (folded_cond
))
18059 ++c_inhibit_evaluation_warnings
;
18060 exp1
= RECUR (TREE_OPERAND (t
, 1));
18061 --c_inhibit_evaluation_warnings
;
18062 exp2
= RECUR (TREE_OPERAND (t
, 2));
18066 exp1
= RECUR (TREE_OPERAND (t
, 1));
18067 ++c_inhibit_evaluation_warnings
;
18068 exp2
= RECUR (TREE_OPERAND (t
, 2));
18069 --c_inhibit_evaluation_warnings
;
18071 cond
= folded_cond
;
18075 exp1
= RECUR (TREE_OPERAND (t
, 1));
18076 exp2
= RECUR (TREE_OPERAND (t
, 2));
18079 warning_sentinel
s(warn_duplicated_branches
);
18080 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
18081 cond
, exp1
, exp2
, complain
));
18084 case PSEUDO_DTOR_EXPR
:
18086 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18087 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18088 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
18089 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
18095 tree purpose
, value
, chain
;
18097 if (t
== void_list_node
)
18100 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
18101 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
18103 /* We have pack expansions, so expand those and
18104 create a new list out of it. */
18105 tree purposevec
= NULL_TREE
;
18106 tree valuevec
= NULL_TREE
;
18110 /* Expand the argument expressions. */
18111 if (TREE_PURPOSE (t
))
18112 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18113 complain
, in_decl
);
18114 if (TREE_VALUE (t
))
18115 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18116 complain
, in_decl
);
18118 /* Build the rest of the list. */
18119 chain
= TREE_CHAIN (t
);
18120 if (chain
&& chain
!= void_type_node
)
18121 chain
= RECUR (chain
);
18123 /* Determine the number of arguments. */
18124 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18126 len
= TREE_VEC_LENGTH (purposevec
);
18127 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18129 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18130 len
= TREE_VEC_LENGTH (valuevec
);
18133 /* Since we only performed a partial substitution into
18134 the argument pack, we only RETURN (a single list
18136 if (purposevec
== TREE_PURPOSE (t
)
18137 && valuevec
== TREE_VALUE (t
)
18138 && chain
== TREE_CHAIN (t
))
18141 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18144 /* Convert the argument vectors into a TREE_LIST */
18148 /* Grab the Ith values. */
18150 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18153 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18156 /* Build the list (backwards). */
18157 chain
= tree_cons (purpose
, value
, chain
);
18163 purpose
= TREE_PURPOSE (t
);
18165 purpose
= RECUR (purpose
);
18166 value
= TREE_VALUE (t
);
18168 value
= RECUR (value
);
18169 chain
= TREE_CHAIN (t
);
18170 if (chain
&& chain
!= void_type_node
)
18171 chain
= RECUR (chain
);
18172 if (purpose
== TREE_PURPOSE (t
)
18173 && value
== TREE_VALUE (t
)
18174 && chain
== TREE_CHAIN (t
))
18176 RETURN (tree_cons (purpose
, value
, chain
));
18179 case COMPONENT_REF
:
18186 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18187 args
, complain
, in_decl
);
18188 /* Remember that there was a reference to this entity. */
18189 if (DECL_P (object
)
18190 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18191 RETURN (error_mark_node
);
18192 object_type
= TREE_TYPE (object
);
18194 member
= TREE_OPERAND (t
, 1);
18195 if (BASELINK_P (member
))
18196 member
= tsubst_baselink (member
,
18197 non_reference (TREE_TYPE (object
)),
18198 args
, complain
, in_decl
);
18200 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18201 if (member
== error_mark_node
)
18202 RETURN (error_mark_node
);
18204 if (TREE_CODE (member
) == FIELD_DECL
)
18206 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18207 if (TREE_CODE (r
) == COMPONENT_REF
)
18208 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18211 else if (type_dependent_expression_p (object
))
18212 /* We can't do much here. */;
18213 else if (!CLASS_TYPE_P (object_type
))
18215 if (scalarish_type_p (object_type
))
18217 tree s
= NULL_TREE
;
18218 tree dtor
= member
;
18220 if (TREE_CODE (dtor
) == SCOPE_REF
)
18222 s
= TREE_OPERAND (dtor
, 0);
18223 dtor
= TREE_OPERAND (dtor
, 1);
18225 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18227 dtor
= TREE_OPERAND (dtor
, 0);
18229 RETURN (finish_pseudo_destructor_expr
18230 (object
, s
, dtor
, input_location
));
18234 else if (TREE_CODE (member
) == SCOPE_REF
18235 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18237 /* Lookup the template functions now that we know what the
18239 tree scope
= TREE_OPERAND (member
, 0);
18240 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18241 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18242 member
= lookup_qualified_name (scope
, tmpl
,
18243 /*is_type_p=*/false,
18244 /*complain=*/false);
18245 if (BASELINK_P (member
))
18247 BASELINK_FUNCTIONS (member
)
18248 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18250 member
= (adjust_result_of_qualified_name_lookup
18251 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18256 qualified_name_lookup_error (scope
, tmpl
, member
,
18258 RETURN (error_mark_node
);
18261 else if (TREE_CODE (member
) == SCOPE_REF
18262 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18263 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18265 if (complain
& tf_error
)
18267 if (TYPE_P (TREE_OPERAND (member
, 0)))
18268 error ("%qT is not a class or namespace",
18269 TREE_OPERAND (member
, 0));
18271 error ("%qD is not a class or namespace",
18272 TREE_OPERAND (member
, 0));
18274 RETURN (error_mark_node
);
18277 r
= finish_class_member_access_expr (object
, member
,
18278 /*template_p=*/false,
18280 if (TREE_CODE (r
) == COMPONENT_REF
)
18281 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18286 RETURN (build_throw
18287 (RECUR (TREE_OPERAND (t
, 0))));
18291 vec
<constructor_elt
, va_gc
> *n
;
18292 constructor_elt
*ce
;
18293 unsigned HOST_WIDE_INT idx
;
18294 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18295 bool process_index_p
;
18297 bool need_copy_p
= false;
18300 if (type
== error_mark_node
)
18301 RETURN (error_mark_node
);
18303 /* We do not want to process the index of aggregate
18304 initializers as they are identifier nodes which will be
18305 looked up by digest_init. */
18306 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18308 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18309 newlen
= vec_safe_length (n
);
18310 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18312 if (ce
->index
&& process_index_p
18313 /* An identifier index is looked up in the type
18314 being initialized, not the current scope. */
18315 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18316 ce
->index
= RECUR (ce
->index
);
18318 if (PACK_EXPANSION_P (ce
->value
))
18320 /* Substitute into the pack expansion. */
18321 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18324 if (ce
->value
== error_mark_node
18325 || PACK_EXPANSION_P (ce
->value
))
18327 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18328 /* Just move the argument into place. */
18329 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18332 /* Update the length of the final CONSTRUCTOR
18333 arguments vector, and note that we will need to
18335 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18336 need_copy_p
= true;
18340 ce
->value
= RECUR (ce
->value
);
18345 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18347 vec_alloc (n
, newlen
);
18348 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18350 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18352 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18353 for (i
= 0; i
< len
; ++i
)
18354 CONSTRUCTOR_APPEND_ELT (n
, 0,
18355 TREE_VEC_ELT (ce
->value
, i
));
18358 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18362 r
= build_constructor (init_list_type_node
, n
);
18363 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18365 if (TREE_HAS_CONSTRUCTOR (t
))
18367 fcl_t cl
= fcl_functional
;
18368 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18370 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18373 TREE_TYPE (r
) = type
;
18379 tree operand_0
= TREE_OPERAND (t
, 0);
18380 if (TYPE_P (operand_0
))
18382 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18383 RETURN (get_typeid (operand_0
, complain
));
18387 operand_0
= RECUR (operand_0
);
18388 RETURN (build_typeid (operand_0
, complain
));
18399 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18400 /* ??? We're doing a subset of finish_id_expression here. */
18402 && !processing_template_decl
18403 && !cp_unevaluated_operand
18404 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18405 && CP_DECL_THREAD_LOCAL_P (r
))
18407 if (tree wrap
= get_tls_wrapper_fn (r
))
18408 /* Replace an evaluated use of the thread_local variable with
18409 a call to its wrapper. */
18410 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18412 else if (outer_automatic_var_p (r
))
18413 r
= process_outer_var_ref (r
, complain
);
18415 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18416 /* If the original type was a reference, we'll be wrapped in
18417 the appropriate INDIRECT_REF. */
18418 r
= convert_from_reference (r
);
18424 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18425 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18426 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18429 case OFFSETOF_EXPR
:
18432 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18433 in_decl
, /*function_p=*/false,
18434 /*integral_constant_expression_p=*/false);
18435 RETURN (finish_offsetof (object_ptr
,
18436 RECUR (TREE_OPERAND (t
, 0)),
18437 EXPR_LOCATION (t
)));
18440 case ADDRESSOF_EXPR
:
18441 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18442 RECUR (TREE_OPERAND (t
, 0)), complain
));
18446 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18447 complain
, in_decl
);
18449 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18450 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18451 type2
= RECUR (type2
);
18453 type2
= tsubst (type2
, args
, complain
, in_decl
);
18455 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18460 tree old_stmt_expr
= cur_stmt_expr
;
18461 tree stmt_expr
= begin_stmt_expr ();
18463 cur_stmt_expr
= stmt_expr
;
18464 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18465 integral_constant_expression_p
);
18466 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18467 cur_stmt_expr
= old_stmt_expr
;
18469 /* If the resulting list of expression statement is empty,
18470 fold it further into void_node. */
18471 if (empty_expr_stmt_p (stmt_expr
))
18472 stmt_expr
= void_node
;
18474 RETURN (stmt_expr
);
18479 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18481 RETURN (build_lambda_object (r
));
18485 /* We can get here for a constant initializer of non-dependent type.
18486 FIXME stop folding in cp_parser_initializer_clause. */
18488 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18493 case TRANSACTION_EXPR
:
18494 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18495 integral_constant_expression_p
));
18498 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18500 case VEC_PERM_EXPR
:
18502 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18503 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18504 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18505 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18509 case REQUIRES_EXPR
:
18510 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18512 case NON_LVALUE_EXPR
:
18513 case VIEW_CONVERT_EXPR
:
18514 /* We should only see these for location wrapper nodes, or within
18515 instantiate_non_dependent_expr (when args is NULL_TREE). */
18516 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18517 if (location_wrapper_p (t
))
18518 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18519 EXPR_LOCATION (t
)));
18523 /* Handle Objective-C++ constructs, if appropriate. */
18526 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18527 in_decl
, /*function_p=*/false);
18531 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18537 input_location
= loc
;
18541 /* Verify that the instantiated ARGS are valid. For type arguments,
18542 make sure that the type's linkage is ok. For non-type arguments,
18543 make sure they are constants if they are integral or enumerations.
18544 Emit an error under control of COMPLAIN, and return TRUE on error. */
18547 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18549 if (dependent_template_arg_p (t
))
18551 if (ARGUMENT_PACK_P (t
))
18553 tree vec
= ARGUMENT_PACK_ARGS (t
);
18554 int len
= TREE_VEC_LENGTH (vec
);
18555 bool result
= false;
18558 for (i
= 0; i
< len
; ++i
)
18559 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18563 else if (TYPE_P (t
))
18565 /* [basic.link]: A name with no linkage (notably, the name
18566 of a class or enumeration declared in a local scope)
18567 shall not be used to declare an entity with linkage.
18568 This implies that names with no linkage cannot be used as
18571 DR 757 relaxes this restriction for C++0x. */
18572 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18573 : no_linkage_check (t
, /*relaxed_p=*/false));
18577 /* DR 488 makes use of a type with no linkage cause
18578 type deduction to fail. */
18579 if (complain
& tf_error
)
18581 if (TYPE_UNNAMED_P (nt
))
18582 error ("%qT is/uses unnamed type", t
);
18584 error ("template argument for %qD uses local type %qT",
18589 /* In order to avoid all sorts of complications, we do not
18590 allow variably-modified types as template arguments. */
18591 else if (variably_modified_type_p (t
, NULL_TREE
))
18593 if (complain
& tf_error
)
18594 error ("%qT is a variably modified type", t
);
18598 /* Class template and alias template arguments should be OK. */
18599 else if (DECL_TYPE_TEMPLATE_P (t
))
18601 /* A non-type argument of integral or enumerated type must be a
18603 else if (TREE_TYPE (t
)
18604 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18605 && !REFERENCE_REF_P (t
)
18606 && !TREE_CONSTANT (t
))
18608 if (complain
& tf_error
)
18609 error ("integral expression %qE is not constant", t
);
18616 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18618 int ix
, len
= DECL_NTPARMS (tmpl
);
18619 bool result
= false;
18621 for (ix
= 0; ix
!= len
; ix
++)
18623 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18626 if (result
&& (complain
& tf_error
))
18627 error (" trying to instantiate %qD", tmpl
);
18631 /* We're out of SFINAE context now, so generate diagnostics for the access
18632 errors we saw earlier when instantiating D from TMPL and ARGS. */
18635 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18637 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18638 tree type
= TREE_TYPE (pattern
);
18639 location_t loc
= input_location
;
18641 push_access_scope (d
);
18642 push_deferring_access_checks (dk_no_deferred
);
18643 input_location
= DECL_SOURCE_LOCATION (pattern
);
18644 tsubst (type
, args
, tf_warning_or_error
, d
);
18645 input_location
= loc
;
18646 pop_deferring_access_checks ();
18647 pop_access_scope (d
);
18650 /* Instantiate the indicated variable, function, or alias template TMPL with
18651 the template arguments in TARG_PTR. */
18654 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18656 tree targ_ptr
= orig_args
;
18660 bool access_ok
= true;
18662 if (tmpl
== error_mark_node
)
18663 return error_mark_node
;
18665 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18667 /* If this function is a clone, handle it specially. */
18668 if (DECL_CLONED_FUNCTION_P (tmpl
))
18673 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18674 DECL_CLONED_FUNCTION. */
18675 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18676 targ_ptr
, complain
);
18677 if (spec
== error_mark_node
)
18678 return error_mark_node
;
18680 /* Look for the clone. */
18681 FOR_EACH_CLONE (clone
, spec
)
18682 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18684 /* We should always have found the clone by now. */
18685 gcc_unreachable ();
18689 if (targ_ptr
== error_mark_node
)
18690 return error_mark_node
;
18692 /* Check to see if we already have this specialization. */
18693 gen_tmpl
= most_general_template (tmpl
);
18694 if (TMPL_ARGS_DEPTH (targ_ptr
)
18695 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18696 /* targ_ptr only has the innermost template args, so add the outer ones
18697 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18698 the case of a non-dependent call within a template definition). */
18699 targ_ptr
= (add_outermost_template_args
18700 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18703 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18704 but it doesn't seem to be on the hot path. */
18705 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18707 gcc_assert (tmpl
== gen_tmpl
18708 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18710 || fndecl
== NULL_TREE
);
18712 if (spec
!= NULL_TREE
)
18714 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18716 if (complain
& tf_error
)
18717 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18718 return error_mark_node
;
18723 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18725 return error_mark_node
;
18727 /* We are building a FUNCTION_DECL, during which the access of its
18728 parameters and return types have to be checked. However this
18729 FUNCTION_DECL which is the desired context for access checking
18730 is not built yet. We solve this chicken-and-egg problem by
18731 deferring all checks until we have the FUNCTION_DECL. */
18732 push_deferring_access_checks (dk_deferred
);
18734 /* Instantiation of the function happens in the context of the function
18735 template, not the context of the overload resolution we're doing. */
18736 push_to_top_level ();
18737 /* If there are dependent arguments, e.g. because we're doing partial
18738 ordering, make sure processing_template_decl stays set. */
18739 if (uses_template_parms (targ_ptr
))
18740 ++processing_template_decl
;
18741 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18743 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18744 complain
, gen_tmpl
, true);
18745 push_nested_class (ctx
);
18748 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18750 fndecl
= NULL_TREE
;
18751 if (VAR_P (pattern
))
18753 /* We need to determine if we're using a partial or explicit
18754 specialization now, because the type of the variable could be
18756 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18757 tree elt
= most_specialized_partial_spec (tid
, complain
);
18758 if (elt
== error_mark_node
)
18759 pattern
= error_mark_node
;
18762 tree partial_tmpl
= TREE_VALUE (elt
);
18763 tree partial_args
= TREE_PURPOSE (elt
);
18764 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18765 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18769 /* Substitute template parameters to obtain the specialization. */
18770 if (fndecl
== NULL_TREE
)
18771 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18772 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18773 pop_nested_class ();
18774 pop_from_top_level ();
18776 if (fndecl
== error_mark_node
)
18778 pop_deferring_access_checks ();
18779 return error_mark_node
;
18782 /* The DECL_TI_TEMPLATE should always be the immediate parent
18783 template, not the most general template. */
18784 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18785 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18787 /* Now we know the specialization, compute access previously
18788 deferred. Do no access control for inheriting constructors,
18789 as we already checked access for the inherited constructor. */
18790 if (!(flag_new_inheriting_ctors
18791 && DECL_INHERITED_CTOR (fndecl
)))
18793 push_access_scope (fndecl
);
18794 if (!perform_deferred_access_checks (complain
))
18796 pop_access_scope (fndecl
);
18798 pop_deferring_access_checks ();
18800 /* If we've just instantiated the main entry point for a function,
18801 instantiate all the alternate entry points as well. We do this
18802 by cloning the instantiation of the main entry point, not by
18803 instantiating the template clones. */
18804 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18805 clone_function_decl (fndecl
, /*update_methods=*/false);
18809 if (!(complain
& tf_error
))
18811 /* Remember to reinstantiate when we're out of SFINAE so the user
18812 can see the errors. */
18813 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18815 return error_mark_node
;
18820 /* Wrapper for instantiate_template_1. */
18823 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18826 timevar_push (TV_TEMPLATE_INST
);
18827 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18828 timevar_pop (TV_TEMPLATE_INST
);
18832 /* Instantiate the alias template TMPL with ARGS. Also push a template
18833 instantiation level, which instantiate_template doesn't do because
18834 functions and variables have sufficient context established by the
18838 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18840 struct pending_template
*old_last_pend
= last_pending_template
;
18841 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18842 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18843 return error_mark_node
;
18844 tree tinst
= build_tree_list (tmpl
, args
);
18845 if (!push_tinst_level (tinst
))
18848 return error_mark_node
;
18852 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18853 args
, tmpl
, complain
,
18854 /*require_all_args=*/true,
18855 /*use_default_args=*/true);
18857 tree r
= instantiate_template (tmpl
, args
, complain
);
18858 pop_tinst_level ();
18859 /* We can't free this if a pending_template entry or last_error_tinst_level
18860 is pointing at it. */
18861 if (last_pending_template
== old_last_pend
18862 && last_error_tinst_level
== old_error_tinst
)
18868 /* PARM is a template parameter pack for FN. Returns true iff
18869 PARM is used in a deducible way in the argument list of FN. */
18872 pack_deducible_p (tree parm
, tree fn
)
18874 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18875 for (; t
; t
= TREE_CHAIN (t
))
18877 tree type
= TREE_VALUE (t
);
18879 if (!PACK_EXPANSION_P (type
))
18881 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18882 packs
; packs
= TREE_CHAIN (packs
))
18883 if (template_args_equal (TREE_VALUE (packs
), parm
))
18885 /* The template parameter pack is used in a function parameter
18886 pack. If this is the end of the parameter list, the
18887 template parameter pack is deducible. */
18888 if (TREE_CHAIN (t
) == void_list_node
)
18891 /* Otherwise, not. Well, it could be deduced from
18892 a non-pack parameter, but doing so would end up with
18893 a deduction mismatch, so don't bother. */
18897 /* The template parameter pack isn't used in any function parameter
18898 packs, but it might be used deeper, e.g. tuple<Args...>. */
18902 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18903 NARGS elements of the arguments that are being used when calling
18904 it. TARGS is a vector into which the deduced template arguments
18907 Returns either a FUNCTION_DECL for the matching specialization of FN or
18908 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18909 true, diagnostics will be printed to explain why it failed.
18911 If FN is a conversion operator, or we are trying to produce a specific
18912 specialization, RETURN_TYPE is the return type desired.
18914 The EXPLICIT_TARGS are explicit template arguments provided via a
18917 The parameter STRICT is one of:
18920 We are deducing arguments for a function call, as in
18921 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18922 deducing arguments for a call to the result of a conversion
18923 function template, as in [over.call.object].
18926 We are deducing arguments for a conversion function, as in
18927 [temp.deduct.conv].
18930 We are deducing arguments when doing an explicit instantiation
18931 as in [temp.explicit], when determining an explicit specialization
18932 as in [temp.expl.spec], or when taking the address of a function
18933 template, as in [temp.deduct.funcaddr]. */
18936 fn_type_unification (tree fn
,
18937 tree explicit_targs
,
18940 unsigned int nargs
,
18942 unification_kind_t strict
,
18949 tree decl
= NULL_TREE
;
18950 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18952 static int deduction_depth
;
18953 struct pending_template
*old_last_pend
= last_pending_template
;
18954 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18957 if (flag_new_inheriting_ctors
)
18958 fn
= strip_inheriting_ctors (fn
);
18960 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18962 tree r
= error_mark_node
;
18964 tree full_targs
= targs
;
18965 if (TMPL_ARGS_DEPTH (targs
)
18966 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18967 full_targs
= (add_outermost_template_args
18968 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18972 complain
|= tf_decltype
;
18974 /* In C++0x, it's possible to have a function template whose type depends
18975 on itself recursively. This is most obvious with decltype, but can also
18976 occur with enumeration scope (c++/48969). So we need to catch infinite
18977 recursion and reject the substitution at deduction time; this function
18978 will return error_mark_node for any repeated substitution.
18980 This also catches excessive recursion such as when f<N> depends on
18981 f<N-1> across all integers, and returns error_mark_node for all the
18982 substitutions back up to the initial one.
18984 This is, of course, not reentrant. */
18985 if (excessive_deduction_depth
)
18986 return error_mark_node
;
18987 tinst
= build_tree_list (fn
, NULL_TREE
);
18990 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18992 fntype
= TREE_TYPE (fn
);
18993 if (explicit_targs
)
18997 The specified template arguments must match the template
18998 parameters in kind (i.e., type, nontype, template), and there
18999 must not be more arguments than there are parameters;
19000 otherwise type deduction fails.
19002 Nontype arguments must match the types of the corresponding
19003 nontype template parameters, or must be convertible to the
19004 types of the corresponding nontype parameters as specified in
19005 _temp.arg.nontype_, otherwise type deduction fails.
19007 All references in the function type of the function template
19008 to the corresponding template parameters are replaced by the
19009 specified template argument values. If a substitution in a
19010 template parameter or in the function type of the function
19011 template results in an invalid type, type deduction fails. */
19012 int i
, len
= TREE_VEC_LENGTH (tparms
);
19013 location_t loc
= input_location
;
19014 bool incomplete
= false;
19016 if (explicit_targs
== error_mark_node
)
19019 if (TMPL_ARGS_DEPTH (explicit_targs
)
19020 < TMPL_ARGS_DEPTH (full_targs
))
19021 explicit_targs
= add_outermost_template_args (full_targs
,
19024 /* Adjust any explicit template arguments before entering the
19025 substitution context. */
19027 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
19029 /*require_all_args=*/false,
19030 /*use_default_args=*/false));
19031 if (explicit_targs
== error_mark_node
)
19034 /* Substitute the explicit args into the function type. This is
19035 necessary so that, for instance, explicitly declared function
19036 arguments can match null pointed constants. If we were given
19037 an incomplete set of explicit args, we must not do semantic
19038 processing during substitution as we could create partial
19040 for (i
= 0; i
< len
; i
++)
19042 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
19043 bool parameter_pack
= false;
19044 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
19046 /* Dig out the actual parm. */
19047 if (TREE_CODE (parm
) == TYPE_DECL
19048 || TREE_CODE (parm
) == TEMPLATE_DECL
)
19050 parm
= TREE_TYPE (parm
);
19051 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
19053 else if (TREE_CODE (parm
) == PARM_DECL
)
19055 parm
= DECL_INITIAL (parm
);
19056 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
19059 if (!parameter_pack
&& targ
== NULL_TREE
)
19060 /* No explicit argument for this template parameter. */
19063 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
19065 /* Mark the argument pack as "incomplete". We could
19066 still deduce more arguments during unification.
19067 We remove this mark in type_unification_real. */
19070 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
19071 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
19072 = ARGUMENT_PACK_ARGS (targ
);
19075 /* We have some incomplete argument packs. */
19080 TREE_VALUE (tinst
) = explicit_targs
;
19081 if (!push_tinst_level (tinst
))
19083 excessive_deduction_depth
= true;
19086 processing_template_decl
+= incomplete
;
19087 input_location
= DECL_SOURCE_LOCATION (fn
);
19088 /* Ignore any access checks; we'll see them again in
19089 instantiate_template and they might have the wrong
19090 access path at this point. */
19091 push_deferring_access_checks (dk_deferred
);
19092 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
19093 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
19094 pop_deferring_access_checks ();
19095 input_location
= loc
;
19096 processing_template_decl
-= incomplete
;
19097 pop_tinst_level ();
19099 if (fntype
== error_mark_node
)
19102 /* Place the explicitly specified arguments in TARGS. */
19103 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
19104 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
19105 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
19108 /* Never do unification on the 'this' parameter. */
19109 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
19111 if (return_type
&& strict
== DEDUCE_CALL
)
19113 /* We're deducing for a call to the result of a template conversion
19114 function. The parms we really want are in return_type. */
19115 if (POINTER_TYPE_P (return_type
))
19116 return_type
= TREE_TYPE (return_type
);
19117 parms
= TYPE_ARG_TYPES (return_type
);
19119 else if (return_type
)
19123 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19124 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19125 new_args
[0] = return_type
;
19126 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19131 /* We allow incomplete unification without an error message here
19132 because the standard doesn't seem to explicitly prohibit it. Our
19133 callers must be ready to deal with unification failures in any
19136 TREE_VALUE (tinst
) = targs
;
19137 /* If we aren't explaining yet, push tinst context so we can see where
19138 any errors (e.g. from class instantiations triggered by instantiation
19139 of default template arguments) come from. If we are explaining, this
19140 context is redundant. */
19141 if (!explain_p
&& !push_tinst_level (tinst
))
19143 excessive_deduction_depth
= true;
19147 /* type_unification_real will pass back any access checks from default
19148 template argument substitution. */
19149 vec
<deferred_access_check
, va_gc
> *checks
;
19152 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19153 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19154 strict
, flags
, &checks
, explain_p
);
19156 pop_tinst_level ();
19160 /* Now that we have bindings for all of the template arguments,
19161 ensure that the arguments deduced for the template template
19162 parameters have compatible template parameter lists. We cannot
19163 check this property before we have deduced all template
19164 arguments, because the template parameter types of a template
19165 template parameter might depend on prior template parameters
19166 deduced after the template template parameter. The following
19167 ill-formed example illustrates this issue:
19169 template<typename T, template<T> class C> void f(C<5>, T);
19171 template<int N> struct X {};
19174 f(X<5>(), 5l); // error: template argument deduction fails
19177 The template parameter list of 'C' depends on the template type
19178 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19179 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19180 time that we deduce 'C'. */
19181 if (!template_template_parm_bindings_ok_p
19182 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19184 unify_inconsistent_template_template_parameters (explain_p
);
19188 /* All is well so far. Now, check:
19192 When all template arguments have been deduced, all uses of
19193 template parameters in nondeduced contexts are replaced with
19194 the corresponding deduced argument values. If the
19195 substitution results in an invalid type, as described above,
19196 type deduction fails. */
19197 TREE_VALUE (tinst
) = targs
;
19198 if (!push_tinst_level (tinst
))
19200 excessive_deduction_depth
= true;
19204 /* Also collect access checks from the instantiation. */
19205 reopen_deferring_access_checks (checks
);
19207 decl
= instantiate_template (fn
, targs
, complain
);
19209 checks
= get_deferred_access_checks ();
19210 pop_deferring_access_checks ();
19212 pop_tinst_level ();
19214 if (decl
== error_mark_node
)
19217 /* Now perform any access checks encountered during substitution. */
19218 push_access_scope (decl
);
19219 ok
= perform_access_checks (checks
, complain
);
19220 pop_access_scope (decl
);
19224 /* If we're looking for an exact match, check that what we got
19225 is indeed an exact match. It might not be if some template
19226 parameters are used in non-deduced contexts. But don't check
19227 for an exact match if we have dependent template arguments;
19228 in that case we're doing partial ordering, and we already know
19229 that we have two candidates that will provide the actual type. */
19230 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19232 tree substed
= TREE_TYPE (decl
);
19236 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19238 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19239 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19240 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19242 unify_type_mismatch (explain_p
, args
[i
],
19243 TREE_VALUE (sarg
));
19248 /* After doing deduction with the inherited constructor, actually return an
19249 instantiation of the inheriting constructor. */
19251 decl
= instantiate_template (orig_fn
, targs
, complain
);
19257 if (excessive_deduction_depth
)
19259 if (deduction_depth
== 0)
19260 /* Reset once we're all the way out. */
19261 excessive_deduction_depth
= false;
19264 /* We can't free this if a pending_template entry or last_error_tinst_level
19265 is pointing at it. */
19266 if (last_pending_template
== old_last_pend
19267 && last_error_tinst_level
== old_error_tinst
)
19273 /* Adjust types before performing type deduction, as described in
19274 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19275 sections are symmetric. PARM is the type of a function parameter
19276 or the return type of the conversion function. ARG is the type of
19277 the argument passed to the call, or the type of the value
19278 initialized with the result of the conversion function.
19279 ARG_EXPR is the original argument expression, which may be null. */
19282 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19295 /* Swap PARM and ARG throughout the remainder of this
19296 function; the handling is precisely symmetric since PARM
19297 will initialize ARG rather than vice versa. */
19298 std::swap (parm
, arg
);
19302 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19303 too, but here handle it by stripping the reference from PARM
19304 rather than by adding it to ARG. */
19305 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19306 && TYPE_REF_IS_RVALUE (*parm
)
19307 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19308 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19309 && TREE_CODE (*arg
) == REFERENCE_TYPE
19310 && !TYPE_REF_IS_RVALUE (*arg
))
19311 *parm
= TREE_TYPE (*parm
);
19312 /* Nothing else to do in this case. */
19316 gcc_unreachable ();
19319 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19321 /* [temp.deduct.call]
19323 If P is not a reference type:
19325 --If A is an array type, the pointer type produced by the
19326 array-to-pointer standard conversion (_conv.array_) is
19327 used in place of A for type deduction; otherwise,
19329 --If A is a function type, the pointer type produced by
19330 the function-to-pointer standard conversion
19331 (_conv.func_) is used in place of A for type deduction;
19334 --If A is a cv-qualified type, the top level
19335 cv-qualifiers of A's type are ignored for type
19337 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19338 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19339 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19340 *arg
= build_pointer_type (*arg
);
19342 *arg
= TYPE_MAIN_VARIANT (*arg
);
19345 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19346 reference to a cv-unqualified template parameter that does not represent a
19347 template parameter of a class template (during class template argument
19348 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19349 an lvalue, the type "lvalue reference to A" is used in place of A for type
19351 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19352 && TYPE_REF_IS_RVALUE (*parm
)
19353 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19354 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19355 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19356 && (arg_expr
? lvalue_p (arg_expr
)
19357 /* try_one_overload doesn't provide an arg_expr, but
19358 functions are always lvalues. */
19359 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19360 *arg
= build_reference_type (*arg
);
19362 /* [temp.deduct.call]
19364 If P is a cv-qualified type, the top level cv-qualifiers
19365 of P's type are ignored for type deduction. If P is a
19366 reference type, the type referred to by P is used for
19368 *parm
= TYPE_MAIN_VARIANT (*parm
);
19369 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19371 *parm
= TREE_TYPE (*parm
);
19372 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19375 /* DR 322. For conversion deduction, remove a reference type on parm
19376 too (which has been swapped into ARG). */
19377 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19378 *arg
= TREE_TYPE (*arg
);
19383 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19384 template which does contain any deducible template parameters; check if
19385 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19386 unify_one_argument. */
19389 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19390 int flags
, bool explain_p
)
19395 type
= TREE_TYPE (arg
);
19399 if (same_type_p (parm
, type
))
19400 return unify_success (explain_p
);
19402 if (strict
== DEDUCE_CONV
)
19404 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19405 explain_p
? tf_warning_or_error
: tf_none
))
19406 return unify_success (explain_p
);
19408 else if (strict
!= DEDUCE_EXACT
)
19410 if (can_convert_arg (parm
, type
,
19411 TYPE_P (arg
) ? NULL_TREE
: arg
,
19412 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19413 return unify_success (explain_p
);
19416 if (strict
== DEDUCE_EXACT
)
19417 return unify_type_mismatch (explain_p
, parm
, arg
);
19419 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19422 static bool uses_deducible_template_parms (tree type
);
19424 /* Returns true iff the expression EXPR is one from which a template
19425 argument can be deduced. In other words, if it's an undecorated
19426 use of a template non-type parameter. */
19429 deducible_expression (tree expr
)
19431 /* Strip implicit conversions. */
19432 while (CONVERT_EXPR_P (expr
))
19433 expr
= TREE_OPERAND (expr
, 0);
19434 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19437 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19438 deducible way; that is, if it has a max value of <PARM> - 1. */
19441 deducible_array_bound (tree domain
)
19443 if (domain
== NULL_TREE
)
19446 tree max
= TYPE_MAX_VALUE (domain
);
19447 if (TREE_CODE (max
) != MINUS_EXPR
)
19450 return deducible_expression (TREE_OPERAND (max
, 0));
19453 /* Returns true iff the template arguments ARGS use a template parameter
19454 in a deducible way. */
19457 deducible_template_args (tree args
)
19459 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19462 tree elt
= TREE_VEC_ELT (args
, i
);
19463 if (ARGUMENT_PACK_P (elt
))
19464 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19467 if (PACK_EXPANSION_P (elt
))
19468 elt
= PACK_EXPANSION_PATTERN (elt
);
19469 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19471 else if (TYPE_P (elt
))
19472 deducible
= uses_deducible_template_parms (elt
);
19474 deducible
= deducible_expression (elt
);
19482 /* Returns true iff TYPE contains any deducible references to template
19483 parameters, as per 14.8.2.5. */
19486 uses_deducible_template_parms (tree type
)
19488 if (PACK_EXPANSION_P (type
))
19489 type
= PACK_EXPANSION_PATTERN (type
);
19496 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19497 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19503 if (POINTER_TYPE_P (type
))
19504 return uses_deducible_template_parms (TREE_TYPE (type
));
19506 /* T[integer-constant ]
19508 if (TREE_CODE (type
) == ARRAY_TYPE
)
19509 return (uses_deducible_template_parms (TREE_TYPE (type
))
19510 || deducible_array_bound (TYPE_DOMAIN (type
)));
19522 if (TYPE_PTRMEM_P (type
))
19523 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19524 || (uses_deducible_template_parms
19525 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19527 /* template-name <T> (where template-name refers to a class template)
19528 template-name <i> (where template-name refers to a class template) */
19529 if (CLASS_TYPE_P (type
)
19530 && CLASSTYPE_TEMPLATE_INFO (type
)
19531 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19532 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19533 (CLASSTYPE_TI_ARGS (type
)));
19538 if (TREE_CODE (type
) == FUNCTION_TYPE
19539 || TREE_CODE (type
) == METHOD_TYPE
)
19541 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19543 tree parm
= TYPE_ARG_TYPES (type
);
19544 if (TREE_CODE (type
) == METHOD_TYPE
)
19545 parm
= TREE_CHAIN (parm
);
19546 for (; parm
; parm
= TREE_CHAIN (parm
))
19547 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19554 /* Subroutine of type_unification_real and unify_pack_expansion to
19555 handle unification of a single P/A pair. Parameters are as
19556 for those functions. */
19559 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19560 int subr
, unification_kind_t strict
,
19563 tree arg_expr
= NULL_TREE
;
19566 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19567 return unify_invalid (explain_p
);
19568 if (arg
== unknown_type_node
)
19569 /* We can't deduce anything from this, but we might get all the
19570 template args from other function args. */
19571 return unify_success (explain_p
);
19573 /* Implicit conversions (Clause 4) will be performed on a function
19574 argument to convert it to the type of the corresponding function
19575 parameter if the parameter type contains no template-parameters that
19576 participate in template argument deduction. */
19577 if (strict
!= DEDUCE_EXACT
19578 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19579 /* For function parameters with no deducible template parameters,
19580 just return. We'll check non-dependent conversions later. */
19581 return unify_success (explain_p
);
19586 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19587 | UNIFY_ALLOW_MORE_CV_QUAL
19588 | UNIFY_ALLOW_DERIVED
);
19592 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19596 arg_strict
= UNIFY_ALLOW_NONE
;
19600 gcc_unreachable ();
19603 /* We only do these transformations if this is the top-level
19604 parameter_type_list in a call or declaration matching; in other
19605 situations (nested function declarators, template argument lists) we
19606 won't be comparing a type to an expression, and we don't do any type
19612 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19613 if (type_unknown_p (arg
))
19615 /* [temp.deduct.type] A template-argument can be
19616 deduced from a pointer to function or pointer
19617 to member function argument if the set of
19618 overloaded functions does not contain function
19619 templates and at most one of a set of
19620 overloaded functions provides a unique
19622 resolve_overloaded_unification (tparms
, targs
, parm
,
19624 arg_strict
, explain_p
);
19625 /* If a unique match was not found, this is a
19626 non-deduced context, so we still succeed. */
19627 return unify_success (explain_p
);
19631 arg
= unlowered_expr_type (arg
);
19632 if (arg
== error_mark_node
)
19633 return unify_invalid (explain_p
);
19637 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19640 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19641 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19642 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19644 /* For deduction from an init-list we need the actual list. */
19645 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19647 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19650 /* for_each_template_parm callback that always returns 0. */
19653 zero_r (tree
, void *)
19658 /* for_each_template_parm any_fn callback to handle deduction of a template
19659 type argument from the type of an array bound. */
19662 array_deduction_r (tree t
, void *data
)
19664 tree_pair_p d
= (tree_pair_p
)data
;
19665 tree
&tparms
= d
->purpose
;
19666 tree
&targs
= d
->value
;
19668 if (TREE_CODE (t
) == ARRAY_TYPE
)
19669 if (tree dom
= TYPE_DOMAIN (t
))
19670 if (tree max
= TYPE_MAX_VALUE (dom
))
19672 if (TREE_CODE (max
) == MINUS_EXPR
)
19673 max
= TREE_OPERAND (max
, 0);
19674 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19675 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19676 UNIFY_ALLOW_NONE
, /*explain*/false);
19679 /* Keep walking. */
19683 /* Try to deduce any not-yet-deduced template type arguments from the type of
19684 an array bound. This is handled separately from unify because 14.8.2.5 says
19685 "The type of a type parameter is only deduced from an array bound if it is
19686 not otherwise deduced." */
19689 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19691 tree_pair_s data
= { tparms
, targs
};
19692 hash_set
<tree
> visited
;
19693 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19694 /*nondeduced*/false, array_deduction_r
);
19697 /* Most parms like fn_type_unification.
19699 If SUBR is 1, we're being called recursively (to unify the
19700 arguments of a function or method parameter of a function
19703 CHECKS is a pointer to a vector of access checks encountered while
19704 substituting default template arguments. */
19707 type_unification_real (tree tparms
,
19711 unsigned int xnargs
,
19713 unification_kind_t strict
,
19715 vec
<deferred_access_check
, va_gc
> **checks
,
19720 int ntparms
= TREE_VEC_LENGTH (tparms
);
19721 int saw_undeduced
= 0;
19724 unsigned int nargs
;
19727 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19728 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19729 gcc_assert (ntparms
> 0);
19731 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19733 /* Reset the number of non-defaulted template arguments contained
19735 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19743 while (parms
&& parms
!= void_list_node
19746 parm
= TREE_VALUE (parms
);
19748 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19749 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19750 /* For a function parameter pack that occurs at the end of the
19751 parameter-declaration-list, the type A of each remaining
19752 argument of the call is compared with the type P of the
19753 declarator-id of the function parameter pack. */
19756 parms
= TREE_CHAIN (parms
);
19758 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19759 /* For a function parameter pack that does not occur at the
19760 end of the parameter-declaration-list, the type of the
19761 parameter pack is a non-deduced context. */
19767 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19773 && parms
!= void_list_node
19774 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19776 /* Unify the remaining arguments with the pack expansion type. */
19778 tree parmvec
= make_tree_vec (1);
19780 /* Allocate a TREE_VEC and copy in all of the arguments */
19781 argvec
= make_tree_vec (nargs
- ia
);
19782 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19783 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19785 /* Copy the parameter into parmvec. */
19786 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19787 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19788 /*subr=*/subr
, explain_p
))
19791 /* Advance to the end of the list of parameters. */
19792 parms
= TREE_CHAIN (parms
);
19795 /* Fail if we've reached the end of the parm list, and more args
19796 are present, and the parm list isn't variadic. */
19797 if (ia
< nargs
&& parms
== void_list_node
)
19798 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19799 /* Fail if parms are left and they don't have default values and
19800 they aren't all deduced as empty packs (c++/57397). This is
19801 consistent with sufficient_parms_p. */
19802 if (parms
&& parms
!= void_list_node
19803 && TREE_PURPOSE (parms
) == NULL_TREE
)
19805 unsigned int count
= nargs
;
19810 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19813 p
= TREE_CHAIN (p
);
19815 while (p
&& p
!= void_list_node
);
19816 if (count
!= nargs
)
19817 return unify_too_few_arguments (explain_p
, ia
, count
,
19823 tsubst_flags_t complain
= (explain_p
19824 ? tf_warning_or_error
19826 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19828 for (i
= 0; i
< ntparms
; i
++)
19830 tree targ
= TREE_VEC_ELT (targs
, i
);
19831 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19833 /* Clear the "incomplete" flags on all argument packs now so that
19834 substituting them into later default arguments works. */
19835 if (targ
&& ARGUMENT_PACK_P (targ
))
19837 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19838 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19841 if (targ
|| tparm
== error_mark_node
)
19843 tparm
= TREE_VALUE (tparm
);
19845 if (TREE_CODE (tparm
) == TYPE_DECL
19846 && !tried_array_deduction
)
19848 try_array_deduction (tparms
, targs
, xparms
);
19849 tried_array_deduction
= true;
19850 if (TREE_VEC_ELT (targs
, i
))
19854 /* If this is an undeduced nontype parameter that depends on
19855 a type parameter, try another pass; its type may have been
19856 deduced from a later argument than the one from which
19857 this parameter can be deduced. */
19858 if (TREE_CODE (tparm
) == PARM_DECL
19859 && uses_template_parms (TREE_TYPE (tparm
))
19860 && saw_undeduced
< 2)
19866 /* Core issue #226 (C++0x) [temp.deduct]:
19868 If a template argument has not been deduced, its
19869 default template argument, if any, is used.
19871 When we are in C++98 mode, TREE_PURPOSE will either
19872 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19873 to explicitly check cxx_dialect here. */
19874 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19875 /* OK, there is a default argument. Wait until after the
19876 conversion check to do substitution. */
19879 /* If the type parameter is a parameter pack, then it will
19880 be deduced to an empty parameter pack. */
19881 if (template_parameter_pack_p (tparm
))
19885 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19887 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19888 TREE_CONSTANT (arg
) = 1;
19891 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19893 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19895 TREE_VEC_ELT (targs
, i
) = arg
;
19899 return unify_parameter_deduction_failure (explain_p
, tparm
);
19902 /* DR 1391: All parameters have args, now check non-dependent parms for
19904 if (saw_undeduced
< 2)
19905 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19906 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19908 parm
= TREE_VALUE (parms
);
19910 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19911 && (!TREE_CHAIN (parms
)
19912 || TREE_CHAIN (parms
) == void_list_node
))
19913 /* For a function parameter pack that occurs at the end of the
19914 parameter-declaration-list, the type A of each remaining
19915 argument of the call is compared with the type P of the
19916 declarator-id of the function parameter pack. */
19919 parms
= TREE_CHAIN (parms
);
19921 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19922 /* For a function parameter pack that does not occur at the
19923 end of the parameter-declaration-list, the type of the
19924 parameter pack is a non-deduced context. */
19930 if (uses_template_parms (parm
))
19932 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19937 /* Now substitute into the default template arguments. */
19938 for (i
= 0; i
< ntparms
; i
++)
19940 tree targ
= TREE_VEC_ELT (targs
, i
);
19941 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19943 if (targ
|| tparm
== error_mark_node
)
19945 tree parm
= TREE_VALUE (tparm
);
19947 tsubst_flags_t fcomplain
= complain
;
19948 if (saw_undeduced
== 1)
19950 /* When saw_undeduced == 1, substitution into parm and arg might
19951 fail or not replace all template parameters, and that's
19953 fcomplain
= tf_none
;
19954 if (TREE_CODE (parm
) == PARM_DECL
19955 && uses_template_parms (TREE_TYPE (parm
)))
19959 tree arg
= TREE_PURPOSE (tparm
);
19960 reopen_deferring_access_checks (*checks
);
19961 location_t save_loc
= input_location
;
19963 input_location
= DECL_SOURCE_LOCATION (parm
);
19964 arg
= tsubst_template_arg (arg
, full_targs
, fcomplain
, NULL_TREE
);
19965 if (arg
!= error_mark_node
&& !uses_template_parms (arg
))
19966 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19968 else if (saw_undeduced
== 1)
19971 arg
= error_mark_node
;
19972 input_location
= save_loc
;
19973 *checks
= get_deferred_access_checks ();
19974 pop_deferring_access_checks ();
19975 if (arg
== error_mark_node
)
19979 TREE_VEC_ELT (targs
, i
) = arg
;
19980 /* The position of the first default template argument,
19981 is also the number of non-defaulted arguments in TARGS.
19983 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19988 if (saw_undeduced
++ == 1)
19992 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19993 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19995 return unify_success (explain_p
);
19998 /* Subroutine of type_unification_real. Args are like the variables
19999 at the call site. ARG is an overloaded function (or template-id);
20000 we try deducing template args from each of the overloads, and if
20001 only one succeeds, we go with that. Modifies TARGS and returns
20002 true on success. */
20005 resolve_overloaded_unification (tree tparms
,
20009 unification_kind_t strict
,
20013 tree tempargs
= copy_node (targs
);
20015 tree goodfn
= NULL_TREE
;
20018 if (TREE_CODE (arg
) == ADDR_EXPR
)
20020 arg
= TREE_OPERAND (arg
, 0);
20026 if (TREE_CODE (arg
) == COMPONENT_REF
)
20027 /* Handle `&x' where `x' is some static or non-static member
20029 arg
= TREE_OPERAND (arg
, 1);
20031 if (TREE_CODE (arg
) == OFFSET_REF
)
20032 arg
= TREE_OPERAND (arg
, 1);
20034 /* Strip baselink information. */
20035 if (BASELINK_P (arg
))
20036 arg
= BASELINK_FUNCTIONS (arg
);
20038 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
20040 /* If we got some explicit template args, we need to plug them into
20041 the affected templates before we try to unify, in case the
20042 explicit args will completely resolve the templates in question. */
20045 tree expl_subargs
= TREE_OPERAND (arg
, 1);
20046 arg
= TREE_OPERAND (arg
, 0);
20048 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20051 tree subargs
, elem
;
20053 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20056 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20057 expl_subargs
, NULL_TREE
, tf_none
,
20058 /*require_all_args=*/true,
20059 /*use_default_args=*/true);
20060 if (subargs
!= error_mark_node
20061 && !any_dependent_template_arguments_p (subargs
))
20063 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
20064 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
20065 elem
, strict
, sub_strict
, addr_p
, explain_p
)
20066 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
20075 /* If no templates (or more than one) are fully resolved by the
20076 explicit arguments, this template-id is a non-deduced context; it
20077 could still be OK if we deduce all template arguments for the
20078 enclosing call through other arguments. */
20082 else if (TREE_CODE (arg
) != OVERLOAD
20083 && TREE_CODE (arg
) != FUNCTION_DECL
)
20084 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20085 -- but the deduction does not succeed because the expression is
20086 not just the function on its own. */
20089 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20092 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
20093 strict
, sub_strict
, addr_p
, explain_p
)
20094 && (!goodfn
|| !decls_match (goodfn
, fn
)))
20101 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20102 to function or pointer to member function argument if the set of
20103 overloaded functions does not contain function templates and at most
20104 one of a set of overloaded functions provides a unique match.
20106 So if we found multiple possibilities, we return success but don't
20107 deduce anything. */
20111 int i
= TREE_VEC_LENGTH (targs
);
20113 if (TREE_VEC_ELT (tempargs
, i
))
20115 tree old
= TREE_VEC_ELT (targs
, i
);
20116 tree new_
= TREE_VEC_ELT (tempargs
, i
);
20117 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
20118 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
20119 /* Don't forget explicit template arguments in a pack. */
20120 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
20121 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
20122 TREE_VEC_ELT (targs
, i
) = new_
;
20131 /* Core DR 115: In contexts where deduction is done and fails, or in
20132 contexts where deduction is not done, if a template argument list is
20133 specified and it, along with any default template arguments, identifies
20134 a single function template specialization, then the template-id is an
20135 lvalue for the function template specialization. */
20138 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20140 tree expr
, offset
, baselink
;
20143 if (!type_unknown_p (orig_expr
))
20148 offset
= NULL_TREE
;
20149 baselink
= NULL_TREE
;
20151 if (TREE_CODE (expr
) == ADDR_EXPR
)
20153 expr
= TREE_OPERAND (expr
, 0);
20156 if (TREE_CODE (expr
) == OFFSET_REF
)
20159 expr
= TREE_OPERAND (expr
, 1);
20161 if (BASELINK_P (expr
))
20164 expr
= BASELINK_FUNCTIONS (expr
);
20167 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20170 tree goodfn
= NULL_TREE
;
20172 /* If we got some explicit template args, we need to plug them into
20173 the affected templates before we try to unify, in case the
20174 explicit args will completely resolve the templates in question. */
20176 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20177 tree arg
= TREE_OPERAND (expr
, 0);
20178 tree badfn
= NULL_TREE
;
20179 tree badargs
= NULL_TREE
;
20181 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20184 tree subargs
, elem
;
20186 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20189 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20190 expl_subargs
, NULL_TREE
, tf_none
,
20191 /*require_all_args=*/true,
20192 /*use_default_args=*/true);
20193 if (subargs
!= error_mark_node
20194 && !any_dependent_template_arguments_p (subargs
))
20196 elem
= instantiate_template (fn
, subargs
, tf_none
);
20197 if (elem
== error_mark_node
)
20202 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20211 mark_used (goodfn
);
20214 expr
= build_baselink (BASELINK_BINFO (baselink
),
20215 BASELINK_ACCESS_BINFO (baselink
),
20216 expr
, BASELINK_OPTYPE (baselink
));
20220 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20221 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20224 expr
= cp_build_addr_expr (expr
, complain
);
20227 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20228 /* There were no good options and at least one bad one, so let the
20229 user know what the problem is. */
20230 instantiate_template (badfn
, badargs
, complain
);
20235 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20236 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20237 different overloads deduce different arguments for a given parm.
20238 ADDR_P is true if the expression for which deduction is being
20239 performed was of the form "& fn" rather than simply "fn".
20241 Returns 1 on success. */
20244 try_one_overload (tree tparms
,
20249 unification_kind_t strict
,
20258 if (arg
== error_mark_node
)
20261 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20262 to function or pointer to member function argument if the set of
20263 overloaded functions does not contain function templates and at most
20264 one of a set of overloaded functions provides a unique match.
20266 So if this is a template, just return success. */
20268 if (uses_template_parms (arg
))
20271 if (TREE_CODE (arg
) == METHOD_TYPE
)
20272 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20274 arg
= build_pointer_type (arg
);
20276 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20278 /* We don't copy orig_targs for this because if we have already deduced
20279 some template args from previous args, unify would complain when we
20280 try to deduce a template parameter for the same argument, even though
20281 there isn't really a conflict. */
20282 nargs
= TREE_VEC_LENGTH (targs
);
20283 tempargs
= make_tree_vec (nargs
);
20285 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20288 /* First make sure we didn't deduce anything that conflicts with
20289 explicitly specified args. */
20290 for (i
= nargs
; i
--; )
20292 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20293 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20297 else if (uses_template_parms (elt
))
20298 /* Since we're unifying against ourselves, we will fill in
20299 template args used in the function parm list with our own
20300 template parms. Discard them. */
20301 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20302 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20304 /* Check that the argument at each index of the deduced argument pack
20305 is equivalent to the corresponding explicitly specified argument.
20306 We may have deduced more arguments than were explicitly specified,
20309 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20310 that's wrong if we deduce the same argument pack from multiple
20311 function arguments: it's only incomplete the first time. */
20313 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20314 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20316 if (TREE_VEC_LENGTH (deduced_pack
)
20317 < TREE_VEC_LENGTH (explicit_pack
))
20320 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20321 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20322 TREE_VEC_ELT (deduced_pack
, j
)))
20325 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20329 for (i
= nargs
; i
--; )
20331 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20334 TREE_VEC_ELT (targs
, i
) = elt
;
20340 /* PARM is a template class (perhaps with unbound template
20341 parameters). ARG is a fully instantiated type. If ARG can be
20342 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20343 TARGS are as for unify. */
20346 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20349 tree copy_of_targs
;
20351 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20353 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20354 /* Matches anything. */;
20355 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20356 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20359 /* We need to make a new template argument vector for the call to
20360 unify. If we used TARGS, we'd clutter it up with the result of
20361 the attempted unification, even if this class didn't work out.
20362 We also don't want to commit ourselves to all the unifications
20363 we've already done, since unification is supposed to be done on
20364 an argument-by-argument basis. In other words, consider the
20365 following pathological case:
20367 template <int I, int J, int K>
20370 template <int I, int J>
20371 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20373 template <int I, int J, int K>
20374 void f(S<I, J, K>, S<I, I, I>);
20383 Now, by the time we consider the unification involving `s2', we
20384 already know that we must have `f<0, 0, 0>'. But, even though
20385 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20386 because there are two ways to unify base classes of S<0, 1, 2>
20387 with S<I, I, I>. If we kept the already deduced knowledge, we
20388 would reject the possibility I=1. */
20389 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20391 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20393 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20398 /* If unification failed, we're done. */
20399 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20400 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20406 /* Given a template type PARM and a class type ARG, find the unique
20407 base type in ARG that is an instance of PARM. We do not examine
20408 ARG itself; only its base-classes. If there is not exactly one
20409 appropriate base class, return NULL_TREE. PARM may be the type of
20410 a partial specialization, as well as a plain template type. Used
20413 static enum template_base_result
20414 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20415 bool explain_p
, tree
*result
)
20417 tree rval
= NULL_TREE
;
20420 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20422 binfo
= TYPE_BINFO (complete_type (arg
));
20425 /* The type could not be completed. */
20426 *result
= NULL_TREE
;
20427 return tbr_incomplete_type
;
20430 /* Walk in inheritance graph order. The search order is not
20431 important, and this avoids multiple walks of virtual bases. */
20432 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20434 tree r
= try_class_unification (tparms
, targs
, parm
,
20435 BINFO_TYPE (binfo
), explain_p
);
20439 /* If there is more than one satisfactory baseclass, then:
20443 If they yield more than one possible deduced A, the type
20447 if (rval
&& !same_type_p (r
, rval
))
20449 *result
= NULL_TREE
;
20450 return tbr_ambiguous_baseclass
;
20458 return tbr_success
;
20461 /* Returns the level of DECL, which declares a template parameter. */
20464 template_decl_level (tree decl
)
20466 switch (TREE_CODE (decl
))
20469 case TEMPLATE_DECL
:
20470 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20473 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20476 gcc_unreachable ();
20481 /* Decide whether ARG can be unified with PARM, considering only the
20482 cv-qualifiers of each type, given STRICT as documented for unify.
20483 Returns nonzero iff the unification is OK on that basis. */
20486 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20488 int arg_quals
= cp_type_quals (arg
);
20489 int parm_quals
= cp_type_quals (parm
);
20491 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20492 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20494 /* Although a CVR qualifier is ignored when being applied to a
20495 substituted template parameter ([8.3.2]/1 for example), that
20496 does not allow us to unify "const T" with "int&" because both
20497 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20498 It is ok when we're allowing additional CV qualifiers
20499 at the outer level [14.8.2.1]/3,1st bullet. */
20500 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20501 || TREE_CODE (arg
) == FUNCTION_TYPE
20502 || TREE_CODE (arg
) == METHOD_TYPE
)
20503 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20506 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20507 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20511 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20512 && (arg_quals
& parm_quals
) != parm_quals
)
20515 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20516 && (parm_quals
& arg_quals
) != arg_quals
)
20522 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20524 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20526 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20527 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20528 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20530 *index
= TEMPLATE_TYPE_IDX (parm
);
20531 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20535 *index
= TEMPLATE_PARM_IDX (parm
);
20536 *level
= TEMPLATE_PARM_LEVEL (parm
);
20540 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20542 if (unify (TP, TA, P, A, S, EP)) \
20546 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20547 expansion at the end of PACKED_PARMS. Returns 0 if the type
20548 deduction succeeds, 1 otherwise. STRICT is the same as in
20549 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20550 function call argument list. We'll need to adjust the arguments to make them
20551 types. SUBR tells us if this is from a recursive call to
20552 type_unification_real, or for comparing two template argument
20556 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20557 tree packed_args
, unification_kind_t strict
,
20558 bool subr
, bool explain_p
)
20561 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20562 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20563 tree pack
, packs
= NULL_TREE
;
20564 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20566 /* Add in any args remembered from an earlier partial instantiation. */
20567 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20568 int levels
= TMPL_ARGS_DEPTH (targs
);
20570 packed_args
= expand_template_argument_pack (packed_args
);
20572 int len
= TREE_VEC_LENGTH (packed_args
);
20574 /* Determine the parameter packs we will be deducing from the
20575 pattern, and record their current deductions. */
20576 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20577 pack
; pack
= TREE_CHAIN (pack
))
20579 tree parm_pack
= TREE_VALUE (pack
);
20582 /* Determine the index and level of this parameter pack. */
20583 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20584 if (level
< levels
)
20587 /* Keep track of the parameter packs and their corresponding
20589 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20590 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20593 /* Loop through all of the arguments that have not yet been
20594 unified and unify each with the pattern. */
20595 for (i
= start
; i
< len
; i
++)
20598 bool any_explicit
= false;
20599 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20601 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20602 or the element of its argument pack at the current index if
20603 this argument was explicitly specified. */
20604 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20608 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20611 if (TREE_VALUE (pack
)
20612 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20613 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20615 any_explicit
= true;
20616 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20618 TMPL_ARG (targs
, level
, idx
) = arg
;
20621 /* If we had explicit template arguments, substitute them into the
20622 pattern before deduction. */
20625 /* Some arguments might still be unspecified or dependent. */
20627 ++processing_template_decl
;
20628 dependent
= any_dependent_template_arguments_p (targs
);
20630 --processing_template_decl
;
20631 parm
= tsubst (pattern
, targs
,
20632 explain_p
? tf_warning_or_error
: tf_none
,
20635 --processing_template_decl
;
20636 if (parm
== error_mark_node
)
20642 /* Unify the pattern with the current argument. */
20643 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20647 /* For each parameter pack, collect the deduced value. */
20648 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20651 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20653 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20654 TMPL_ARG (targs
, level
, idx
);
20658 /* Verify that the results of unification with the parameter packs
20659 produce results consistent with what we've seen before, and make
20660 the deduced argument packs available. */
20661 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20663 tree old_pack
= TREE_VALUE (pack
);
20664 tree new_args
= TREE_TYPE (pack
);
20665 int i
, len
= TREE_VEC_LENGTH (new_args
);
20667 bool nondeduced_p
= false;
20669 /* By default keep the original deduced argument pack.
20670 If necessary, more specific code is going to update the
20671 resulting deduced argument later down in this function. */
20672 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20673 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20675 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20676 actually deduce anything. */
20677 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20678 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20679 nondeduced_p
= true;
20683 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20685 /* If we had fewer function args than explicit template args,
20686 just use the explicits. */
20687 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20688 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20689 if (len
< explicit_len
)
20690 new_args
= explicit_args
;
20696 /* Build the deduced *_ARGUMENT_PACK. */
20697 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20699 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20700 TREE_CONSTANT (result
) = 1;
20703 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20705 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20707 /* Note the deduced argument packs for this parameter
20709 TMPL_ARG (targs
, level
, idx
) = result
;
20711 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20712 && (ARGUMENT_PACK_ARGS (old_pack
)
20713 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20715 /* We only had the explicitly-provided arguments before, but
20716 now we have a complete set of arguments. */
20717 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20719 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20720 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20721 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20725 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20726 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20728 if (!comp_template_args (old_args
, new_args
,
20729 &bad_old_arg
, &bad_new_arg
))
20730 /* Inconsistent unification of this parameter pack. */
20731 return unify_parameter_pack_inconsistent (explain_p
,
20737 return unify_success (explain_p
);
20740 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20741 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20742 parameters and return value are as for unify. */
20745 unify_array_domain (tree tparms
, tree targs
,
20746 tree parm_dom
, tree arg_dom
,
20754 /* Our representation of array types uses "N - 1" as the
20755 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20756 not an integer constant. We cannot unify arbitrarily
20757 complex expressions, so we eliminate the MINUS_EXPRs
20759 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20760 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20763 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20764 parm_max
= TREE_OPERAND (parm_max
, 0);
20766 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20767 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20770 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20771 trying to unify the type of a variable with the type
20772 of a template parameter. For example:
20774 template <unsigned int N>
20775 void f (char (&) [N]);
20782 Here, the type of the ARG will be "int [g(i)]", and
20783 may be a SAVE_EXPR, etc. */
20784 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20785 return unify_vla_arg (explain_p
, arg_dom
);
20786 arg_max
= TREE_OPERAND (arg_max
, 0);
20789 /* If only one of the bounds used a MINUS_EXPR, compensate
20790 by adding one to the other bound. */
20791 if (parm_cst
&& !arg_cst
)
20792 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20796 else if (arg_cst
&& !parm_cst
)
20797 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20802 return unify (tparms
, targs
, parm_max
, arg_max
,
20803 UNIFY_ALLOW_INTEGER
, explain_p
);
20806 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20808 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20813 if (PACK_EXPANSION_P (t
))
20814 t
= PACK_EXPANSION_PATTERN (t
);
20815 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20816 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20817 || DECL_TYPE_TEMPLATE_P (t
))
20819 else if (TYPE_P (t
))
20825 /* Deduce the value of template parameters. TPARMS is the (innermost)
20826 set of template parameters to a template. TARGS is the bindings
20827 for those template parameters, as determined thus far; TARGS may
20828 include template arguments for outer levels of template parameters
20829 as well. PARM is a parameter to a template function, or a
20830 subcomponent of that parameter; ARG is the corresponding argument.
20831 This function attempts to match PARM with ARG in a manner
20832 consistent with the existing assignments in TARGS. If more values
20833 are deduced, then TARGS is updated.
20835 Returns 0 if the type deduction succeeds, 1 otherwise. The
20836 parameter STRICT is a bitwise or of the following flags:
20839 Require an exact match between PARM and ARG.
20840 UNIFY_ALLOW_MORE_CV_QUAL:
20841 Allow the deduced ARG to be more cv-qualified (by qualification
20842 conversion) than ARG.
20843 UNIFY_ALLOW_LESS_CV_QUAL:
20844 Allow the deduced ARG to be less cv-qualified than ARG.
20845 UNIFY_ALLOW_DERIVED:
20846 Allow the deduced ARG to be a template base class of ARG,
20847 or a pointer to a template base class of the type pointed to by
20849 UNIFY_ALLOW_INTEGER:
20850 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20851 case for more information.
20852 UNIFY_ALLOW_OUTER_LEVEL:
20853 This is the outermost level of a deduction. Used to determine validity
20854 of qualification conversions. A valid qualification conversion must
20855 have const qualified pointers leading up to the inner type which
20856 requires additional CV quals, except at the outer level, where const
20857 is not required [conv.qual]. It would be normal to set this flag in
20858 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20859 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20860 This is the outermost level of a deduction, and PARM can be more CV
20861 qualified at this point.
20862 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20863 This is the outermost level of a deduction, and PARM can be less CV
20864 qualified at this point. */
20867 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20873 int strict_in
= strict
;
20874 tsubst_flags_t complain
= (explain_p
20875 ? tf_warning_or_error
20878 /* I don't think this will do the right thing with respect to types.
20879 But the only case I've seen it in so far has been array bounds, where
20880 signedness is the only information lost, and I think that will be
20882 while (CONVERT_EXPR_P (parm
))
20883 parm
= TREE_OPERAND (parm
, 0);
20885 if (arg
== error_mark_node
)
20886 return unify_invalid (explain_p
);
20887 if (arg
== unknown_type_node
20888 || arg
== init_list_type_node
)
20889 /* We can't deduce anything from this, but we might get all the
20890 template args from other function args. */
20891 return unify_success (explain_p
);
20893 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20894 return unify_success (explain_p
);
20896 /* If PARM uses template parameters, then we can't bail out here,
20897 even if ARG == PARM, since we won't record unifications for the
20898 template parameters. We might need them if we're trying to
20899 figure out which of two things is more specialized. */
20900 if (arg
== parm
&& !uses_template_parms (parm
))
20901 return unify_success (explain_p
);
20903 /* Handle init lists early, so the rest of the function can assume
20904 we're dealing with a type. */
20905 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20909 tree orig_parm
= parm
;
20911 /* Replace T with std::initializer_list<T> for deduction. */
20912 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20913 && flag_deduce_init_list
)
20914 parm
= listify (parm
);
20916 if (!is_std_init_list (parm
)
20917 && TREE_CODE (parm
) != ARRAY_TYPE
)
20918 /* We can only deduce from an initializer list argument if the
20919 parameter is std::initializer_list or an array; otherwise this
20920 is a non-deduced context. */
20921 return unify_success (explain_p
);
20923 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20924 elttype
= TREE_TYPE (parm
);
20927 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20928 /* Deduction is defined in terms of a single type, so just punt
20929 on the (bizarre) std::initializer_list<T...>. */
20930 if (PACK_EXPANSION_P (elttype
))
20931 return unify_success (explain_p
);
20934 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20936 int elt_strict
= strict
;
20938 if (elt
== error_mark_node
)
20939 return unify_invalid (explain_p
);
20941 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20943 tree type
= TREE_TYPE (elt
);
20944 if (type
== error_mark_node
)
20945 return unify_invalid (explain_p
);
20946 /* It should only be possible to get here for a call. */
20947 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20948 elt_strict
|= maybe_adjust_types_for_deduction
20949 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20953 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20957 if (TREE_CODE (parm
) == ARRAY_TYPE
20958 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20960 /* Also deduce from the length of the initializer list. */
20961 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20962 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20963 if (idx
== error_mark_node
)
20964 return unify_invalid (explain_p
);
20965 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20969 /* If the std::initializer_list<T> deduction worked, replace the
20970 deduced A with std::initializer_list<A>. */
20971 if (orig_parm
!= parm
)
20973 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20974 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20975 targ
= listify (targ
);
20976 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20978 return unify_success (explain_p
);
20981 /* If parm and arg aren't the same kind of thing (template, type, or
20982 expression), fail early. */
20983 if (pa_kind (parm
) != pa_kind (arg
))
20984 return unify_invalid (explain_p
);
20986 /* Immediately reject some pairs that won't unify because of
20987 cv-qualification mismatches. */
20988 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20990 /* It is the elements of the array which hold the cv quals of an array
20991 type, and the elements might be template type parms. We'll check
20992 when we recurse. */
20993 && TREE_CODE (arg
) != ARRAY_TYPE
20994 /* We check the cv-qualifiers when unifying with template type
20995 parameters below. We want to allow ARG `const T' to unify with
20996 PARM `T' for example, when computing which of two templates
20997 is more specialized, for example. */
20998 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20999 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
21000 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21002 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
21003 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
21004 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
21005 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
21006 strict
&= ~UNIFY_ALLOW_DERIVED
;
21007 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21008 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
21010 switch (TREE_CODE (parm
))
21012 case TYPENAME_TYPE
:
21014 case UNBOUND_CLASS_TEMPLATE
:
21015 /* In a type which contains a nested-name-specifier, template
21016 argument values cannot be deduced for template parameters used
21017 within the nested-name-specifier. */
21018 return unify_success (explain_p
);
21020 case TEMPLATE_TYPE_PARM
:
21021 case TEMPLATE_TEMPLATE_PARM
:
21022 case BOUND_TEMPLATE_TEMPLATE_PARM
:
21023 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21024 if (error_operand_p (tparm
))
21025 return unify_invalid (explain_p
);
21027 if (TEMPLATE_TYPE_LEVEL (parm
)
21028 != template_decl_level (tparm
))
21029 /* The PARM is not one we're trying to unify. Just check
21030 to see if it matches ARG. */
21032 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21033 && (is_auto (parm
) ? is_auto (arg
)
21034 : same_type_p (parm
, arg
)))
21035 return unify_success (explain_p
);
21037 return unify_type_mismatch (explain_p
, parm
, arg
);
21039 idx
= TEMPLATE_TYPE_IDX (parm
);
21040 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21041 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
21042 if (error_operand_p (tparm
))
21043 return unify_invalid (explain_p
);
21045 /* Check for mixed types and values. */
21046 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21047 && TREE_CODE (tparm
) != TYPE_DECL
)
21048 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21049 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
21050 gcc_unreachable ();
21052 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21054 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
21055 && CLASS_TYPE_P (arg
))
21057 /* First try to match ARG directly. */
21058 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
21062 /* Otherwise, look for a suitable base of ARG, as below. */
21063 enum template_base_result r
;
21064 r
= get_template_base (tparms
, targs
, parm
, arg
,
21067 return unify_no_common_base (explain_p
, r
, parm
, arg
);
21071 /* ARG must be constructed from a template class or a template
21072 template parameter. */
21073 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
21074 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21075 return unify_template_deduction_failure (explain_p
, parm
, arg
);
21077 /* Deduce arguments T, i from TT<T> or TT<i>. */
21078 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
21081 arg
= TYPE_TI_TEMPLATE (arg
);
21083 /* Fall through to deduce template name. */
21086 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21087 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21089 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21091 /* Simple cases: Value already set, does match or doesn't. */
21092 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
21093 return unify_success (explain_p
);
21095 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21099 /* If PARM is `const T' and ARG is only `int', we don't have
21100 a match unless we are allowing additional qualification.
21101 If ARG is `const int' and PARM is just `T' that's OK;
21102 that binds `const int' to `T'. */
21103 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
21105 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21107 /* Consider the case where ARG is `const volatile int' and
21108 PARM is `const T'. Then, T should be `volatile int'. */
21109 arg
= cp_build_qualified_type_real
21110 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
21111 if (arg
== error_mark_node
)
21112 return unify_invalid (explain_p
);
21114 /* Simple cases: Value already set, does match or doesn't. */
21115 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
21116 return unify_success (explain_p
);
21118 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21120 /* Make sure that ARG is not a variable-sized array. (Note
21121 that were talking about variable-sized arrays (like
21122 `int[n]'), rather than arrays of unknown size (like
21123 `int[]').) We'll get very confused by such a type since
21124 the bound of the array is not constant, and therefore
21125 not mangleable. Besides, such types are not allowed in
21126 ISO C++, so we can do as we please here. We do allow
21127 them for 'auto' deduction, since that isn't ABI-exposed. */
21128 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21129 return unify_vla_arg (explain_p
, arg
);
21131 /* Strip typedefs as in convert_template_argument. */
21132 arg
= canonicalize_type_argument (arg
, tf_none
);
21135 /* If ARG is a parameter pack or an expansion, we cannot unify
21136 against it unless PARM is also a parameter pack. */
21137 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21138 && !template_parameter_pack_p (parm
))
21139 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21141 /* If the argument deduction results is a METHOD_TYPE,
21142 then there is a problem.
21143 METHOD_TYPE doesn't map to any real C++ type the result of
21144 the deduction can not be of that type. */
21145 if (TREE_CODE (arg
) == METHOD_TYPE
)
21146 return unify_method_type_error (explain_p
, arg
);
21148 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21149 return unify_success (explain_p
);
21151 case TEMPLATE_PARM_INDEX
:
21152 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21153 if (error_operand_p (tparm
))
21154 return unify_invalid (explain_p
);
21156 if (TEMPLATE_PARM_LEVEL (parm
)
21157 != template_decl_level (tparm
))
21159 /* The PARM is not one we're trying to unify. Just check
21160 to see if it matches ARG. */
21161 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21162 && cp_tree_equal (parm
, arg
));
21164 unify_expression_unequal (explain_p
, parm
, arg
);
21168 idx
= TEMPLATE_PARM_IDX (parm
);
21169 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21173 if ((strict
& UNIFY_ALLOW_INTEGER
)
21174 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21175 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21176 /* We're deducing from an array bound, the type doesn't matter. */
21177 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21178 int x
= !cp_tree_equal (targ
, arg
);
21180 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21184 /* [temp.deduct.type] If, in the declaration of a function template
21185 with a non-type template-parameter, the non-type
21186 template-parameter is used in an expression in the function
21187 parameter-list and, if the corresponding template-argument is
21188 deduced, the template-argument type shall match the type of the
21189 template-parameter exactly, except that a template-argument
21190 deduced from an array bound may be of any integral type.
21191 The non-type parameter might use already deduced type parameters. */
21192 ++processing_template_decl
;
21193 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
21194 --processing_template_decl
;
21195 if (tree a
= type_uses_auto (tparm
))
21197 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21198 if (tparm
== error_mark_node
)
21202 if (!TREE_TYPE (arg
))
21203 /* Template-parameter dependent expression. Just accept it for now.
21204 It will later be processed in convert_template_argument. */
21206 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21207 non_reference (tparm
)))
21209 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21210 && CP_INTEGRAL_TYPE_P (tparm
))
21211 /* Convert the ARG to the type of PARM; the deduced non-type
21212 template argument must exactly match the types of the
21213 corresponding parameter. */
21214 arg
= fold (build_nop (tparm
, arg
));
21215 else if (uses_template_parms (tparm
))
21217 /* We haven't deduced the type of this parameter yet. */
21218 if (cxx_dialect
>= cxx17
21219 /* We deduce from array bounds in try_array_deduction. */
21220 && !(strict
& UNIFY_ALLOW_INTEGER
))
21222 /* Deduce it from the non-type argument. */
21223 tree atype
= TREE_TYPE (arg
);
21224 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21226 UNIFY_ALLOW_NONE
, explain_p
);
21229 /* Try again later. */
21230 return unify_success (explain_p
);
21233 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21235 /* If ARG is a parameter pack or an expansion, we cannot unify
21236 against it unless PARM is also a parameter pack. */
21237 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21238 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21239 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21242 bool removed_attr
= false;
21243 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21245 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21246 return unify_success (explain_p
);
21250 /* A pointer-to-member constant can be unified only with
21251 another constant. */
21252 if (TREE_CODE (arg
) != PTRMEM_CST
)
21253 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21255 /* Just unify the class member. It would be useless (and possibly
21256 wrong, depending on the strict flags) to unify also
21257 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21258 arg refer to the same variable, even if through different
21259 classes. For instance:
21261 struct A { int x; };
21264 Unification of &A::x and &B::x must succeed. */
21265 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21266 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21271 if (!TYPE_PTR_P (arg
))
21272 return unify_type_mismatch (explain_p
, parm
, arg
);
21274 /* [temp.deduct.call]
21276 A can be another pointer or pointer to member type that can
21277 be converted to the deduced A via a qualification
21278 conversion (_conv.qual_).
21280 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21281 This will allow for additional cv-qualification of the
21282 pointed-to types if appropriate. */
21284 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21285 /* The derived-to-base conversion only persists through one
21286 level of pointers. */
21287 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21289 return unify (tparms
, targs
, TREE_TYPE (parm
),
21290 TREE_TYPE (arg
), strict
, explain_p
);
21293 case REFERENCE_TYPE
:
21294 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21295 return unify_type_mismatch (explain_p
, parm
, arg
);
21296 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21297 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21300 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21301 return unify_type_mismatch (explain_p
, parm
, arg
);
21302 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21303 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21304 return unify_type_mismatch (explain_p
, parm
, arg
);
21305 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21306 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21307 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21308 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21309 TYPE_DOMAIN (arg
), explain_p
);
21310 return unify_success (explain_p
);
21317 case ENUMERAL_TYPE
:
21320 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21321 return unify_type_mismatch (explain_p
, parm
, arg
);
21323 /* We have already checked cv-qualification at the top of the
21325 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21326 return unify_type_mismatch (explain_p
, parm
, arg
);
21328 /* As far as unification is concerned, this wins. Later checks
21329 will invalidate it if necessary. */
21330 return unify_success (explain_p
);
21332 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21333 /* Type INTEGER_CST can come from ordinary constant template args. */
21335 while (CONVERT_EXPR_P (arg
))
21336 arg
= TREE_OPERAND (arg
, 0);
21338 if (TREE_CODE (arg
) != INTEGER_CST
)
21339 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21340 return (tree_int_cst_equal (parm
, arg
)
21341 ? unify_success (explain_p
)
21342 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21346 int i
, len
, argslen
;
21347 int parm_variadic_p
= 0;
21349 if (TREE_CODE (arg
) != TREE_VEC
)
21350 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21352 len
= TREE_VEC_LENGTH (parm
);
21353 argslen
= TREE_VEC_LENGTH (arg
);
21355 /* Check for pack expansions in the parameters. */
21356 for (i
= 0; i
< len
; ++i
)
21358 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21361 /* We can unify against something with a trailing
21363 parm_variadic_p
= 1;
21365 /* [temp.deduct.type]/9: If the template argument list of
21366 P contains a pack expansion that is not the last
21367 template argument, the entire template argument list
21368 is a non-deduced context. */
21369 return unify_success (explain_p
);
21373 /* If we don't have enough arguments to satisfy the parameters
21374 (not counting the pack expression at the end), or we have
21375 too many arguments for a parameter list that doesn't end in
21376 a pack expression, we can't unify. */
21377 if (parm_variadic_p
21378 ? argslen
< len
- parm_variadic_p
21380 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21382 /* Unify all of the parameters that precede the (optional)
21383 pack expression. */
21384 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21386 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21387 TREE_VEC_ELT (parm
, i
),
21388 TREE_VEC_ELT (arg
, i
),
21389 UNIFY_ALLOW_NONE
, explain_p
);
21391 if (parm_variadic_p
)
21392 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21394 /*subr=*/true, explain_p
);
21395 return unify_success (explain_p
);
21400 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21401 return unify_type_mismatch (explain_p
, parm
, arg
);
21403 if (TYPE_PTRMEMFUNC_P (parm
))
21405 if (!TYPE_PTRMEMFUNC_P (arg
))
21406 return unify_type_mismatch (explain_p
, parm
, arg
);
21408 return unify (tparms
, targs
,
21409 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21410 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21411 strict
, explain_p
);
21413 else if (TYPE_PTRMEMFUNC_P (arg
))
21414 return unify_type_mismatch (explain_p
, parm
, arg
);
21416 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21418 tree t
= NULL_TREE
;
21420 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21422 /* First, we try to unify the PARM and ARG directly. */
21423 t
= try_class_unification (tparms
, targs
,
21424 parm
, arg
, explain_p
);
21428 /* Fallback to the special case allowed in
21429 [temp.deduct.call]:
21431 If P is a class, and P has the form
21432 template-id, then A can be a derived class of
21433 the deduced A. Likewise, if P is a pointer to
21434 a class of the form template-id, A can be a
21435 pointer to a derived class pointed to by the
21437 enum template_base_result r
;
21438 r
= get_template_base (tparms
, targs
, parm
, arg
,
21443 /* Don't give the derived diagnostic if we're
21444 already dealing with the same template. */
21446 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21447 && (CLASSTYPE_TI_TEMPLATE (parm
)
21448 == CLASSTYPE_TI_TEMPLATE (arg
)));
21449 return unify_no_common_base (explain_p
&& !same_template
,
21454 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21455 && (CLASSTYPE_TI_TEMPLATE (parm
)
21456 == CLASSTYPE_TI_TEMPLATE (arg
)))
21457 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21458 Then, we should unify `int' and `U'. */
21461 /* There's no chance of unification succeeding. */
21462 return unify_type_mismatch (explain_p
, parm
, arg
);
21464 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21465 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21467 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21468 return unify_type_mismatch (explain_p
, parm
, arg
);
21469 return unify_success (explain_p
);
21472 case FUNCTION_TYPE
:
21474 unsigned int nargs
;
21479 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21480 return unify_type_mismatch (explain_p
, parm
, arg
);
21482 /* CV qualifications for methods can never be deduced, they must
21483 match exactly. We need to check them explicitly here,
21484 because type_unification_real treats them as any other
21485 cv-qualified parameter. */
21486 if (TREE_CODE (parm
) == METHOD_TYPE
21487 && (!check_cv_quals_for_unify
21489 class_of_this_parm (arg
),
21490 class_of_this_parm (parm
))))
21491 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21492 if (TREE_CODE (arg
) == FUNCTION_TYPE
21493 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21494 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21495 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21496 return unify_type_mismatch (explain_p
, parm
, arg
);
21498 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21499 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21501 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21502 args
= XALLOCAVEC (tree
, nargs
);
21503 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21504 a
!= NULL_TREE
&& a
!= void_list_node
;
21505 a
= TREE_CHAIN (a
), ++i
)
21506 args
[i
] = TREE_VALUE (a
);
21509 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21510 args
, nargs
, 1, DEDUCE_EXACT
,
21511 LOOKUP_NORMAL
, NULL
, explain_p
))
21514 if (flag_noexcept_type
)
21516 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21517 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21518 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21519 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21520 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21521 && uses_template_parms (TREE_PURPOSE (pspec
)))
21522 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21523 TREE_PURPOSE (aspec
),
21524 UNIFY_ALLOW_NONE
, explain_p
);
21525 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21526 return unify_type_mismatch (explain_p
, parm
, arg
);
21533 /* Unify a pointer to member with a pointer to member function, which
21534 deduces the type of the member as a function type. */
21535 if (TYPE_PTRMEMFUNC_P (arg
))
21537 /* Check top-level cv qualifiers */
21538 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21539 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21541 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21542 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21543 UNIFY_ALLOW_NONE
, explain_p
);
21545 /* Determine the type of the function we are unifying against. */
21546 tree fntype
= static_fn_type (arg
);
21548 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21551 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21552 return unify_type_mismatch (explain_p
, parm
, arg
);
21553 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21554 TYPE_OFFSET_BASETYPE (arg
),
21555 UNIFY_ALLOW_NONE
, explain_p
);
21556 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21557 strict
, explain_p
);
21560 if (DECL_TEMPLATE_PARM_P (parm
))
21561 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21562 if (arg
!= scalar_constant_value (parm
))
21563 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21564 return unify_success (explain_p
);
21567 case TEMPLATE_DECL
:
21568 /* Matched cases are handled by the ARG == PARM test above. */
21569 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21572 /* We might get a variable as a non-type template argument in parm if the
21573 corresponding parameter is type-dependent. Make any necessary
21574 adjustments based on whether arg is a reference. */
21575 if (CONSTANT_CLASS_P (arg
))
21576 parm
= fold_non_dependent_expr (parm
);
21577 else if (REFERENCE_REF_P (arg
))
21579 tree sub
= TREE_OPERAND (arg
, 0);
21581 if (TREE_CODE (sub
) == ADDR_EXPR
)
21582 arg
= TREE_OPERAND (sub
, 0);
21584 /* Now use the normal expression code to check whether they match. */
21587 case TYPE_ARGUMENT_PACK
:
21588 case NONTYPE_ARGUMENT_PACK
:
21589 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21590 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21593 case DECLTYPE_TYPE
:
21594 case UNDERLYING_TYPE
:
21595 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21596 or UNDERLYING_TYPE nodes. */
21597 return unify_success (explain_p
);
21600 /* Unification fails if we hit an error node. */
21601 return unify_invalid (explain_p
);
21604 if (REFERENCE_REF_P (parm
))
21606 bool pexp
= PACK_EXPANSION_P (arg
);
21608 arg
= PACK_EXPANSION_PATTERN (arg
);
21609 if (REFERENCE_REF_P (arg
))
21610 arg
= TREE_OPERAND (arg
, 0);
21612 arg
= make_pack_expansion (arg
, complain
);
21613 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21614 strict
, explain_p
);
21619 /* An unresolved overload is a nondeduced context. */
21620 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21621 return unify_success (explain_p
);
21622 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21624 /* We must be looking at an expression. This can happen with
21628 void foo(S<I>, S<I + 2>);
21630 This is a "nondeduced context":
21634 The nondeduced contexts are:
21636 --A type that is a template-id in which one or more of
21637 the template-arguments is an expression that references
21638 a template-parameter.
21640 In these cases, we assume deduction succeeded, but don't
21641 actually infer any unifications. */
21643 if (!uses_template_parms (parm
)
21644 && !template_args_equal (parm
, arg
))
21645 return unify_expression_unequal (explain_p
, parm
, arg
);
21647 return unify_success (explain_p
);
21650 #undef RECUR_AND_CHECK_FAILURE
21652 /* Note that DECL can be defined in this translation unit, if
21656 mark_definable (tree decl
)
21659 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21660 FOR_EACH_CLONE (clone
, decl
)
21661 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21664 /* Called if RESULT is explicitly instantiated, or is a member of an
21665 explicitly instantiated class. */
21668 mark_decl_instantiated (tree result
, int extern_p
)
21670 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21672 /* If this entity has already been written out, it's too late to
21673 make any modifications. */
21674 if (TREE_ASM_WRITTEN (result
))
21677 /* For anonymous namespace we don't need to do anything. */
21678 if (decl_anon_ns_mem_p (result
))
21680 gcc_assert (!TREE_PUBLIC (result
));
21684 if (TREE_CODE (result
) != FUNCTION_DECL
)
21685 /* The TREE_PUBLIC flag for function declarations will have been
21686 set correctly by tsubst. */
21687 TREE_PUBLIC (result
) = 1;
21689 /* This might have been set by an earlier implicit instantiation. */
21690 DECL_COMDAT (result
) = 0;
21693 DECL_NOT_REALLY_EXTERN (result
) = 0;
21696 mark_definable (result
);
21697 mark_needed (result
);
21698 /* Always make artificials weak. */
21699 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21700 comdat_linkage (result
);
21701 /* For WIN32 we also want to put explicit instantiations in
21702 linkonce sections. */
21703 else if (TREE_PUBLIC (result
))
21704 maybe_make_one_only (result
);
21707 /* If EXTERN_P, then this function will not be emitted -- unless
21708 followed by an explicit instantiation, at which point its linkage
21709 will be adjusted. If !EXTERN_P, then this function will be
21710 emitted here. In neither circumstance do we want
21711 import_export_decl to adjust the linkage. */
21712 DECL_INTERFACE_KNOWN (result
) = 1;
21715 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21716 important template arguments. If any are missing, we check whether
21717 they're important by using error_mark_node for substituting into any
21718 args that were used for partial ordering (the ones between ARGS and END)
21719 and seeing if it bubbles up. */
21722 check_undeduced_parms (tree targs
, tree args
, tree end
)
21724 bool found
= false;
21726 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21727 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21730 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21734 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21735 if (substed
== error_mark_node
)
21741 /* Given two function templates PAT1 and PAT2, return:
21743 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21744 -1 if PAT2 is more specialized than PAT1.
21745 0 if neither is more specialized.
21747 LEN indicates the number of parameters we should consider
21748 (defaulted parameters should not be considered).
21750 The 1998 std underspecified function template partial ordering, and
21751 DR214 addresses the issue. We take pairs of arguments, one from
21752 each of the templates, and deduce them against each other. One of
21753 the templates will be more specialized if all the *other*
21754 template's arguments deduce against its arguments and at least one
21755 of its arguments *does* *not* deduce against the other template's
21756 corresponding argument. Deduction is done as for class templates.
21757 The arguments used in deduction have reference and top level cv
21758 qualifiers removed. Iff both arguments were originally reference
21759 types *and* deduction succeeds in both directions, an lvalue reference
21760 wins against an rvalue reference and otherwise the template
21761 with the more cv-qualified argument wins for that pairing (if
21762 neither is more cv-qualified, they both are equal). Unlike regular
21763 deduction, after all the arguments have been deduced in this way,
21764 we do *not* verify the deduced template argument values can be
21765 substituted into non-deduced contexts.
21767 The logic can be a bit confusing here, because we look at deduce1 and
21768 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21769 can find template arguments for pat1 to make arg1 look like arg2, that
21770 means that arg2 is at least as specialized as arg1. */
21773 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21775 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21776 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21777 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21778 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21779 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21780 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21781 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21782 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21783 tree origs1
, origs2
;
21784 bool lose1
= false;
21785 bool lose2
= false;
21787 /* Remove the this parameter from non-static member functions. If
21788 one is a non-static member function and the other is not a static
21789 member function, remove the first parameter from that function
21790 also. This situation occurs for operator functions where we
21791 locate both a member function (with this pointer) and non-member
21792 operator (with explicit first operand). */
21793 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21795 len
--; /* LEN is the number of significant arguments for DECL1 */
21796 args1
= TREE_CHAIN (args1
);
21797 if (!DECL_STATIC_FUNCTION_P (decl2
))
21798 args2
= TREE_CHAIN (args2
);
21800 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21802 args2
= TREE_CHAIN (args2
);
21803 if (!DECL_STATIC_FUNCTION_P (decl1
))
21806 args1
= TREE_CHAIN (args1
);
21810 /* If only one is a conversion operator, they are unordered. */
21811 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21814 /* Consider the return type for a conversion function */
21815 if (DECL_CONV_FN_P (decl1
))
21817 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21818 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21822 processing_template_decl
++;
21828 /* Stop when an ellipsis is seen. */
21829 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21831 tree arg1
= TREE_VALUE (args1
);
21832 tree arg2
= TREE_VALUE (args2
);
21833 int deduce1
, deduce2
;
21839 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21840 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21842 /* When both arguments are pack expansions, we need only
21843 unify the patterns themselves. */
21844 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21845 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21847 /* This is the last comparison we need to do. */
21851 /* DR 1847: If a particular P contains no template-parameters that
21852 participate in template argument deduction, that P is not used to
21853 determine the ordering. */
21854 if (!uses_deducible_template_parms (arg1
)
21855 && !uses_deducible_template_parms (arg2
))
21858 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21860 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21861 arg1
= TREE_TYPE (arg1
);
21862 quals1
= cp_type_quals (arg1
);
21865 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21867 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21868 arg2
= TREE_TYPE (arg2
);
21869 quals2
= cp_type_quals (arg2
);
21872 arg1
= TYPE_MAIN_VARIANT (arg1
);
21873 arg2
= TYPE_MAIN_VARIANT (arg2
);
21875 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21877 int i
, len2
= remaining_arguments (args2
);
21878 tree parmvec
= make_tree_vec (1);
21879 tree argvec
= make_tree_vec (len2
);
21882 /* Setup the parameter vector, which contains only ARG1. */
21883 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21885 /* Setup the argument vector, which contains the remaining
21887 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21888 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21890 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21891 argvec
, DEDUCE_EXACT
,
21892 /*subr=*/true, /*explain_p=*/false)
21895 /* We cannot deduce in the other direction, because ARG1 is
21896 a pack expansion but ARG2 is not. */
21899 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21901 int i
, len1
= remaining_arguments (args1
);
21902 tree parmvec
= make_tree_vec (1);
21903 tree argvec
= make_tree_vec (len1
);
21906 /* Setup the parameter vector, which contains only ARG1. */
21907 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21909 /* Setup the argument vector, which contains the remaining
21911 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21912 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21914 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21915 argvec
, DEDUCE_EXACT
,
21916 /*subr=*/true, /*explain_p=*/false)
21919 /* We cannot deduce in the other direction, because ARG2 is
21920 a pack expansion but ARG1 is not.*/
21926 /* The normal case, where neither argument is a pack
21928 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21929 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21931 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21932 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21936 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21937 arg2, then arg2 is not as specialized as arg1. */
21943 /* "If, for a given type, deduction succeeds in both directions
21944 (i.e., the types are identical after the transformations above)
21945 and both P and A were reference types (before being replaced with
21946 the type referred to above):
21947 - if the type from the argument template was an lvalue reference and
21948 the type from the parameter template was not, the argument type is
21949 considered to be more specialized than the other; otherwise,
21950 - if the type from the argument template is more cv-qualified
21951 than the type from the parameter template (as described above),
21952 the argument type is considered to be more specialized than the other;
21954 - neither type is more specialized than the other." */
21956 if (deduce1
&& deduce2
)
21958 if (ref1
&& ref2
&& ref1
!= ref2
)
21965 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21967 if ((quals1
& quals2
) == quals2
)
21969 if ((quals1
& quals2
) == quals1
)
21974 if (lose1
&& lose2
)
21975 /* We've failed to deduce something in either direction.
21976 These must be unordered. */
21981 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21982 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21983 /* We have already processed all of the arguments in our
21984 handing of the pack expansion type. */
21987 args1
= TREE_CHAIN (args1
);
21988 args2
= TREE_CHAIN (args2
);
21991 /* "In most cases, all template parameters must have values in order for
21992 deduction to succeed, but for partial ordering purposes a template
21993 parameter may remain without a value provided it is not used in the
21994 types being used for partial ordering."
21996 Thus, if we are missing any of the targs1 we need to substitute into
21997 origs1, then pat2 is not as specialized as pat1. This can happen when
21998 there is a nondeduced context. */
21999 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
22001 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
22004 processing_template_decl
--;
22006 /* If both deductions succeed, the partial ordering selects the more
22007 constrained template. */
22008 if (!lose1
&& !lose2
)
22010 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
22011 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
22012 lose1
= !subsumes_constraints (c1
, c2
);
22013 lose2
= !subsumes_constraints (c2
, c1
);
22016 /* All things being equal, if the next argument is a pack expansion
22017 for one function but not for the other, prefer the
22018 non-variadic function. FIXME this is bogus; see c++/41958. */
22020 && args1
&& TREE_VALUE (args1
)
22021 && args2
&& TREE_VALUE (args2
))
22023 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
22024 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
22027 if (lose1
== lose2
)
22035 /* Determine which of two partial specializations of TMPL is more
22038 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22039 to the first partial specialization. The TREE_PURPOSE is the
22040 innermost set of template parameters for the partial
22041 specialization. PAT2 is similar, but for the second template.
22043 Return 1 if the first partial specialization is more specialized;
22044 -1 if the second is more specialized; 0 if neither is more
22047 See [temp.class.order] for information about determining which of
22048 two templates is more specialized. */
22051 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
22055 bool any_deductions
= false;
22057 tree tmpl1
= TREE_VALUE (pat1
);
22058 tree tmpl2
= TREE_VALUE (pat2
);
22059 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
22060 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
22062 /* Just like what happens for functions, if we are ordering between
22063 different template specializations, we may encounter dependent
22064 types in the arguments, and we need our dependency check functions
22065 to behave correctly. */
22066 ++processing_template_decl
;
22067 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
22071 any_deductions
= true;
22074 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
22078 any_deductions
= true;
22080 --processing_template_decl
;
22082 /* If both deductions succeed, the partial ordering selects the more
22083 constrained template. */
22084 if (!winner
&& any_deductions
)
22085 return more_constrained (tmpl1
, tmpl2
);
22087 /* In the case of a tie where at least one of the templates
22088 has a parameter pack at the end, the template with the most
22089 non-packed parameters wins. */
22092 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
22093 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
22095 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
22096 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
22097 int len1
= TREE_VEC_LENGTH (args1
);
22098 int len2
= TREE_VEC_LENGTH (args2
);
22100 /* We don't count the pack expansion at the end. */
22101 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
22103 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
22108 else if (len1
< len2
)
22115 /* Return the template arguments that will produce the function signature
22116 DECL from the function template FN, with the explicit template
22117 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22118 also match. Return NULL_TREE if no satisfactory arguments could be
22122 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
22124 int ntparms
= DECL_NTPARMS (fn
);
22125 tree targs
= make_tree_vec (ntparms
);
22126 tree decl_type
= TREE_TYPE (decl
);
22127 tree decl_arg_types
;
22129 unsigned int nargs
, ix
;
22132 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22134 /* Never do unification on the 'this' parameter. */
22135 decl_arg_types
= skip_artificial_parms_for (decl
,
22136 TYPE_ARG_TYPES (decl_type
));
22138 nargs
= list_length (decl_arg_types
);
22139 args
= XALLOCAVEC (tree
, nargs
);
22140 for (arg
= decl_arg_types
, ix
= 0;
22141 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22142 arg
= TREE_CHAIN (arg
), ++ix
)
22143 args
[ix
] = TREE_VALUE (arg
);
22145 if (fn_type_unification (fn
, explicit_args
, targs
,
22147 (check_rettype
|| DECL_CONV_FN_P (fn
)
22148 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22149 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
22151 == error_mark_node
)
22157 /* Return the innermost template arguments that, when applied to a partial
22158 specialization SPEC_TMPL of TMPL, yield the ARGS.
22160 For example, suppose we have:
22162 template <class T, class U> struct S {};
22163 template <class T> struct S<T*, int> {};
22165 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22166 partial specialization and the ARGS will be {double*, int}. The resulting
22167 vector will be {double}, indicating that `T' is bound to `double'. */
22170 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22172 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22174 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22175 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22177 tree innermost_deduced_args
;
22179 innermost_deduced_args
= make_tree_vec (ntparms
);
22180 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22182 deduced_args
= copy_node (args
);
22183 SET_TMPL_ARGS_LEVEL (deduced_args
,
22184 TMPL_ARGS_DEPTH (deduced_args
),
22185 innermost_deduced_args
);
22188 deduced_args
= innermost_deduced_args
;
22190 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22192 if (unify (tparms
, deduced_args
,
22193 INNERMOST_TEMPLATE_ARGS (spec_args
),
22194 INNERMOST_TEMPLATE_ARGS (args
),
22195 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22198 for (i
= 0; i
< ntparms
; ++i
)
22199 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22201 if (!tried_array_deduction
)
22203 try_array_deduction (tparms
, innermost_deduced_args
,
22204 INNERMOST_TEMPLATE_ARGS (spec_args
));
22205 tried_array_deduction
= true;
22206 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22212 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22213 if (!push_tinst_level (tinst
))
22215 excessive_deduction_depth
= true;
22219 /* Verify that nondeduced template arguments agree with the type
22220 obtained from argument deduction.
22224 struct A { typedef int X; };
22225 template <class T, class U> struct C {};
22226 template <class T> struct C<T, typename T::X> {};
22228 Then with the instantiation `C<A, int>', we can deduce that
22229 `T' is `A' but unify () does not check whether `typename T::X'
22231 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22233 if (spec_args
!= error_mark_node
)
22234 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22235 INNERMOST_TEMPLATE_ARGS (spec_args
),
22236 tmpl
, tf_none
, false, false);
22238 pop_tinst_level ();
22240 if (spec_args
== error_mark_node
22241 /* We only need to check the innermost arguments; the other
22242 arguments will always agree. */
22243 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22244 INNERMOST_TEMPLATE_ARGS (args
)))
22247 /* Now that we have bindings for all of the template arguments,
22248 ensure that the arguments deduced for the template template
22249 parameters have compatible template parameter lists. See the use
22250 of template_template_parm_bindings_ok_p in fn_type_unification
22251 for more information. */
22252 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22255 return deduced_args
;
22258 // Compare two function templates T1 and T2 by deducing bindings
22259 // from one against the other. If both deductions succeed, compare
22260 // constraints to see which is more constrained.
22262 more_specialized_inst (tree t1
, tree t2
)
22267 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22273 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22279 // If both deductions succeed, then one may be more constrained.
22280 if (count
== 2 && fate
== 0)
22281 fate
= more_constrained (t1
, t2
);
22286 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22287 Return the TREE_LIST node with the most specialized template, if
22288 any. If there is no most specialized template, the error_mark_node
22291 Note that this function does not look at, or modify, the
22292 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22293 returned is one of the elements of INSTANTIATIONS, callers may
22294 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22295 and retrieve it from the value returned. */
22298 most_specialized_instantiation (tree templates
)
22302 ++processing_template_decl
;
22305 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22307 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22308 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22313 /* Equally specialized, move to next function. If there
22314 is no next function, nothing's most specialized. */
22315 fn
= TREE_CHAIN (fn
);
22323 /* Now verify that champ is better than everything earlier in the
22324 instantiation list. */
22325 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22326 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22333 processing_template_decl
--;
22336 return error_mark_node
;
22341 /* If DECL is a specialization of some template, return the most
22342 general such template. Otherwise, returns NULL_TREE.
22344 For example, given:
22346 template <class T> struct S { template <class U> void f(U); };
22348 if TMPL is `template <class U> void S<int>::f(U)' this will return
22349 the full template. This function will not trace past partial
22350 specializations, however. For example, given in addition:
22352 template <class T> struct S<T*> { template <class U> void f(U); };
22354 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22355 `template <class T> template <class U> S<T*>::f(U)'. */
22358 most_general_template (tree decl
)
22360 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22362 if (tree tinfo
= get_template_info (decl
))
22363 decl
= TI_TEMPLATE (tinfo
);
22364 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22365 template friend, or a FIELD_DECL for a capture pack. */
22366 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22370 /* Look for more and more general templates. */
22371 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22373 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22374 (See cp-tree.h for details.) */
22375 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22378 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22379 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22380 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22383 /* Stop if we run into an explicitly specialized class template. */
22384 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22385 && DECL_CONTEXT (decl
)
22386 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22389 decl
= DECL_TI_TEMPLATE (decl
);
22395 /* Return the most specialized of the template partial specializations
22396 which can produce TARGET, a specialization of some class or variable
22397 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22398 a TEMPLATE_DECL node corresponding to the partial specialization, while
22399 the TREE_PURPOSE is the set of template arguments that must be
22400 substituted into the template pattern in order to generate TARGET.
22402 If the choice of partial specialization is ambiguous, a diagnostic
22403 is issued, and the error_mark_node is returned. If there are no
22404 partial specializations matching TARGET, then NULL_TREE is
22405 returned, indicating that the primary template should be used. */
22408 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22410 tree list
= NULL_TREE
;
22415 tree outer_args
= NULL_TREE
;
22418 if (TYPE_P (target
))
22420 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22421 tmpl
= TI_TEMPLATE (tinfo
);
22422 args
= TI_ARGS (tinfo
);
22424 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22426 tmpl
= TREE_OPERAND (target
, 0);
22427 args
= TREE_OPERAND (target
, 1);
22429 else if (VAR_P (target
))
22431 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22432 tmpl
= TI_TEMPLATE (tinfo
);
22433 args
= TI_ARGS (tinfo
);
22436 gcc_unreachable ();
22438 tree main_tmpl
= most_general_template (tmpl
);
22440 /* For determining which partial specialization to use, only the
22441 innermost args are interesting. */
22442 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22444 outer_args
= strip_innermost_template_args (args
, 1);
22445 args
= INNERMOST_TEMPLATE_ARGS (args
);
22448 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22451 tree spec_tmpl
= TREE_VALUE (t
);
22455 /* Substitute in the template args from the enclosing class. */
22456 ++processing_template_decl
;
22457 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22458 --processing_template_decl
;
22461 if (spec_tmpl
== error_mark_node
)
22462 return error_mark_node
;
22464 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22468 spec_args
= add_to_template_args (outer_args
, spec_args
);
22470 /* Keep the candidate only if the constraints are satisfied,
22471 or if we're not compiling with concepts. */
22473 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22475 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22476 TREE_TYPE (list
) = TREE_TYPE (t
);
22484 ambiguous_p
= false;
22487 t
= TREE_CHAIN (t
);
22488 for (; t
; t
= TREE_CHAIN (t
))
22490 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22497 t
= TREE_CHAIN (t
);
22500 ambiguous_p
= true;
22509 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22511 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22514 ambiguous_p
= true;
22522 char *spaces
= NULL
;
22523 if (!(complain
& tf_error
))
22524 return error_mark_node
;
22525 if (TYPE_P (target
))
22526 error ("ambiguous template instantiation for %q#T", target
);
22528 error ("ambiguous template instantiation for %q#D", target
);
22529 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22530 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22532 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22533 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22534 "%s %#qS", spaces
? spaces
: str
, subst
);
22535 spaces
= spaces
? spaces
: get_spaces (str
);
22538 return error_mark_node
;
22544 /* Explicitly instantiate DECL. */
22547 do_decl_instantiation (tree decl
, tree storage
)
22549 tree result
= NULL_TREE
;
22552 if (!decl
|| decl
== error_mark_node
)
22553 /* An error occurred, for which grokdeclarator has already issued
22554 an appropriate message. */
22556 else if (! DECL_LANG_SPECIFIC (decl
))
22558 error ("explicit instantiation of non-template %q#D", decl
);
22562 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22563 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22565 if (VAR_P (decl
) && !var_templ
)
22567 /* There is an asymmetry here in the way VAR_DECLs and
22568 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22569 the latter, the DECL we get back will be marked as a
22570 template instantiation, and the appropriate
22571 DECL_TEMPLATE_INFO will be set up. This does not happen for
22572 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22573 should handle VAR_DECLs as it currently handles
22575 if (!DECL_CLASS_SCOPE_P (decl
))
22577 error ("%qD is not a static data member of a class template", decl
);
22580 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22581 if (!result
|| !VAR_P (result
))
22583 error ("no matching template for %qD found", decl
);
22586 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22588 error ("type %qT for explicit instantiation %qD does not match "
22589 "declared type %qT", TREE_TYPE (result
), decl
,
22594 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22596 error ("explicit instantiation of %q#D", decl
);
22602 /* Check for various error cases. Note that if the explicit
22603 instantiation is valid the RESULT will currently be marked as an
22604 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22605 until we get here. */
22607 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22609 /* DR 259 [temp.spec].
22611 Both an explicit instantiation and a declaration of an explicit
22612 specialization shall not appear in a program unless the explicit
22613 instantiation follows a declaration of the explicit specialization.
22615 For a given set of template parameters, if an explicit
22616 instantiation of a template appears after a declaration of an
22617 explicit specialization for that template, the explicit
22618 instantiation has no effect. */
22621 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22625 No program shall explicitly instantiate any template more
22628 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22629 the first instantiation was `extern' and the second is not,
22630 and EXTERN_P for the opposite case. */
22631 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22632 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22633 /* If an "extern" explicit instantiation follows an ordinary
22634 explicit instantiation, the template is instantiated. */
22638 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22640 error ("no matching template for %qD found", result
);
22643 else if (!DECL_TEMPLATE_INFO (result
))
22645 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22649 if (storage
== NULL_TREE
)
22651 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22653 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22654 pedwarn (input_location
, OPT_Wpedantic
,
22655 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22660 error ("storage class %qD applied to template instantiation", storage
);
22662 check_explicit_instantiation_namespace (result
);
22663 mark_decl_instantiated (result
, extern_p
);
22665 instantiate_decl (result
, /*defer_ok=*/true,
22666 /*expl_inst_class_mem_p=*/false);
22670 mark_class_instantiated (tree t
, int extern_p
)
22672 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22673 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22674 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22675 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22678 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22679 rest_of_type_compilation (t
, 1);
22683 /* Called from do_type_instantiation through binding_table_foreach to
22684 do recursive instantiation for the type bound in ENTRY. */
22686 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22688 tree storage
= *(tree
*) data
;
22690 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22691 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22692 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22695 /* Perform an explicit instantiation of template class T. STORAGE, if
22696 non-null, is the RID for extern, inline or static. COMPLAIN is
22697 nonzero if this is called from the parser, zero if called recursively,
22698 since the standard is unclear (as detailed below). */
22701 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22706 int previous_instantiation_extern_p
= 0;
22708 if (TREE_CODE (t
) == TYPE_DECL
)
22711 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22714 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22716 error ("explicit instantiation of non-class template %qD", tmpl
);
22718 error ("explicit instantiation of non-template type %qT", t
);
22724 if (!COMPLETE_TYPE_P (t
))
22726 if (complain
& tf_error
)
22727 error ("explicit instantiation of %q#T before definition of template",
22732 if (storage
!= NULL_TREE
)
22734 if (!in_system_header_at (input_location
))
22736 if (storage
== ridpointers
[(int) RID_EXTERN
])
22738 if (cxx_dialect
== cxx98
)
22739 pedwarn (input_location
, OPT_Wpedantic
,
22740 "ISO C++ 1998 forbids the use of %<extern%> on "
22741 "explicit instantiations");
22744 pedwarn (input_location
, OPT_Wpedantic
,
22745 "ISO C++ forbids the use of %qE"
22746 " on explicit instantiations", storage
);
22749 if (storage
== ridpointers
[(int) RID_INLINE
])
22751 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22753 else if (storage
== ridpointers
[(int) RID_STATIC
])
22757 error ("storage class %qD applied to template instantiation",
22763 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22765 /* DR 259 [temp.spec].
22767 Both an explicit instantiation and a declaration of an explicit
22768 specialization shall not appear in a program unless the explicit
22769 instantiation follows a declaration of the explicit specialization.
22771 For a given set of template parameters, if an explicit
22772 instantiation of a template appears after a declaration of an
22773 explicit specialization for that template, the explicit
22774 instantiation has no effect. */
22777 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22781 No program shall explicitly instantiate any template more
22784 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22785 instantiation was `extern'. If EXTERN_P then the second is.
22786 These cases are OK. */
22787 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22789 if (!previous_instantiation_extern_p
&& !extern_p
22790 && (complain
& tf_error
))
22791 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22793 /* If we've already instantiated the template, just return now. */
22794 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22798 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22799 mark_class_instantiated (t
, extern_p
);
22804 /* In contrast to implicit instantiation, where only the
22805 declarations, and not the definitions, of members are
22806 instantiated, we have here:
22810 The explicit instantiation of a class template specialization
22811 implies the instantiation of all of its members not
22812 previously explicitly specialized in the translation unit
22813 containing the explicit instantiation.
22815 Of course, we can't instantiate member template classes, since we
22816 don't have any arguments for them. Note that the standard is
22817 unclear on whether the instantiation of the members are
22818 *explicit* instantiations or not. However, the most natural
22819 interpretation is that it should be an explicit
22821 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22823 || (TREE_CODE (fld
) == FUNCTION_DECL
22825 && user_provided_p (fld
)))
22826 && DECL_TEMPLATE_INSTANTIATION (fld
))
22828 mark_decl_instantiated (fld
, extern_p
);
22830 instantiate_decl (fld
, /*defer_ok=*/true,
22831 /*expl_inst_class_mem_p=*/true);
22834 if (CLASSTYPE_NESTED_UTDS (t
))
22835 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22836 bt_instantiate_type_proc
, &storage
);
22839 /* Given a function DECL, which is a specialization of TMPL, modify
22840 DECL to be a re-instantiation of TMPL with the same template
22841 arguments. TMPL should be the template into which tsubst'ing
22842 should occur for DECL, not the most general template.
22844 One reason for doing this is a scenario like this:
22847 void f(const T&, int i);
22849 void g() { f(3, 7); }
22852 void f(const T& t, const int i) { }
22854 Note that when the template is first instantiated, with
22855 instantiate_template, the resulting DECL will have no name for the
22856 first parameter, and the wrong type for the second. So, when we go
22857 to instantiate the DECL, we regenerate it. */
22860 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22862 /* The arguments used to instantiate DECL, from the most general
22866 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22868 /* Make sure that we can see identifiers, and compute access
22870 push_access_scope (decl
);
22872 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22880 args_depth
= TMPL_ARGS_DEPTH (args
);
22881 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22882 if (args_depth
> parms_depth
)
22883 args
= get_innermost_template_args (args
, parms_depth
);
22885 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22886 args
, tf_error
, NULL_TREE
,
22887 /*defer_ok*/false);
22888 if (specs
&& specs
!= error_mark_node
)
22889 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22892 /* Merge parameter declarations. */
22893 decl_parm
= skip_artificial_parms_for (decl
,
22894 DECL_ARGUMENTS (decl
));
22896 = skip_artificial_parms_for (code_pattern
,
22897 DECL_ARGUMENTS (code_pattern
));
22898 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22903 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22904 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22905 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22907 parm_type
= type_decays_to (parm_type
);
22908 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22909 TREE_TYPE (decl_parm
) = parm_type
;
22910 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22911 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22913 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22914 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22916 decl_parm
= DECL_CHAIN (decl_parm
);
22917 pattern_parm
= DECL_CHAIN (pattern_parm
);
22919 /* Merge any parameters that match with the function parameter
22921 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22924 tree expanded_types
;
22925 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22926 the parameters in this function parameter pack. */
22927 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22928 args
, tf_error
, NULL_TREE
);
22929 len
= TREE_VEC_LENGTH (expanded_types
);
22930 for (i
= 0; i
< len
; i
++)
22935 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22936 /* Rename the parameter to include the index. */
22937 DECL_NAME (decl_parm
) =
22938 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22939 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22940 parm_type
= type_decays_to (parm_type
);
22941 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22942 TREE_TYPE (decl_parm
) = parm_type
;
22943 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22944 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22946 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22947 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22949 decl_parm
= DECL_CHAIN (decl_parm
);
22952 /* Merge additional specifiers from the CODE_PATTERN. */
22953 if (DECL_DECLARED_INLINE_P (code_pattern
)
22954 && !DECL_DECLARED_INLINE_P (decl
))
22955 DECL_DECLARED_INLINE_P (decl
) = 1;
22957 else if (VAR_P (decl
))
22959 start_lambda_scope (decl
);
22960 DECL_INITIAL (decl
) =
22961 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22962 tf_error
, DECL_TI_TEMPLATE (decl
),
22963 /*integral_constant_expression_p=*/false);
22964 finish_lambda_scope ();
22965 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22966 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22967 tf_error
, DECL_TI_TEMPLATE (decl
));
22970 gcc_unreachable ();
22972 pop_access_scope (decl
);
22975 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22976 substituted to get DECL. */
22979 template_for_substitution (tree decl
)
22981 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22983 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22984 for the instantiation. This is not always the most general
22985 template. Consider, for example:
22988 struct S { template <class U> void f();
22989 template <> void f<int>(); };
22991 and an instantiation of S<double>::f<int>. We want TD to be the
22992 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22993 while (/* An instantiation cannot have a definition, so we need a
22994 more general template. */
22995 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22996 /* We must also deal with friend templates. Given:
22998 template <class T> struct S {
22999 template <class U> friend void f() {};
23002 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23003 so far as the language is concerned, but that's still
23004 where we get the pattern for the instantiation from. On
23005 other hand, if the definition comes outside the class, say:
23007 template <class T> struct S {
23008 template <class U> friend void f();
23010 template <class U> friend void f() {}
23012 we don't need to look any further. That's what the check for
23013 DECL_INITIAL is for. */
23014 || (TREE_CODE (decl
) == FUNCTION_DECL
23015 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
23016 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
23018 /* The present template, TD, should not be a definition. If it
23019 were a definition, we should be using it! Note that we
23020 cannot restructure the loop to just keep going until we find
23021 a template with a definition, since that might go too far if
23022 a specialization was declared, but not defined. */
23024 /* Fetch the more general template. */
23025 tmpl
= DECL_TI_TEMPLATE (tmpl
);
23031 /* Returns true if we need to instantiate this template instance even if we
23032 know we aren't going to emit it. */
23035 always_instantiate_p (tree decl
)
23037 /* We always instantiate inline functions so that we can inline them. An
23038 explicit instantiation declaration prohibits implicit instantiation of
23039 non-inline functions. With high levels of optimization, we would
23040 normally inline non-inline functions -- but we're not allowed to do
23041 that for "extern template" functions. Therefore, we check
23042 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23043 return ((TREE_CODE (decl
) == FUNCTION_DECL
23044 && (DECL_DECLARED_INLINE_P (decl
)
23045 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
23046 /* And we need to instantiate static data members so that
23047 their initializers are available in integral constant
23050 && decl_maybe_constant_var_p (decl
)));
23053 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23054 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23055 error, true otherwise. */
23058 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
23060 tree fntype
, spec
, noex
, clone
;
23062 /* Don't instantiate a noexcept-specification from template context. */
23063 if (processing_template_decl
)
23066 if (DECL_CLONED_FUNCTION_P (fn
))
23067 fn
= DECL_CLONED_FUNCTION (fn
);
23068 fntype
= TREE_TYPE (fn
);
23069 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
23071 if (!spec
|| !TREE_PURPOSE (spec
))
23074 noex
= TREE_PURPOSE (spec
);
23076 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
23078 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
23079 bool added
= false;
23080 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
23081 spec
= get_defaulted_eh_spec (fn
, complain
);
23082 else if (!(added
= !fns
->add (fn
)))
23084 /* If hash_set::add returns true, the element was already there. */
23085 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
23086 DECL_SOURCE_LOCATION (fn
));
23088 "exception specification of %qD depends on itself",
23090 spec
= noexcept_false_spec
;
23092 else if (push_tinst_level (fn
))
23094 push_access_scope (fn
);
23095 push_deferring_access_checks (dk_no_deferred
);
23096 input_location
= DECL_SOURCE_LOCATION (fn
);
23097 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
23098 DEFERRED_NOEXCEPT_ARGS (noex
),
23099 tf_warning_or_error
, fn
,
23100 /*function_p=*/false,
23101 /*integral_constant_expression_p=*/true);
23102 pop_deferring_access_checks ();
23103 pop_access_scope (fn
);
23104 pop_tinst_level ();
23105 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
23106 if (spec
== error_mark_node
)
23107 spec
= noexcept_false_spec
;
23110 spec
= noexcept_false_spec
;
23115 if (spec
== error_mark_node
)
23118 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
23121 FOR_EACH_CLONE (clone
, fn
)
23123 if (TREE_TYPE (clone
) == fntype
)
23124 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23126 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23132 /* We're starting to process the function INST, an instantiation of PATTERN;
23133 add their parameters to local_specializations. */
23136 register_parameter_specializations (tree pattern
, tree inst
)
23138 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23139 tree spec_parm
= DECL_ARGUMENTS (inst
);
23140 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23142 register_local_specialization (spec_parm
, tmpl_parm
);
23143 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23144 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23146 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23148 if (!DECL_PACK_P (tmpl_parm
))
23150 register_local_specialization (spec_parm
, tmpl_parm
);
23151 spec_parm
= DECL_CHAIN (spec_parm
);
23155 /* Register the (value) argument pack as a specialization of
23156 TMPL_PARM, then move on. */
23157 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23158 register_local_specialization (argpack
, tmpl_parm
);
23161 gcc_assert (!spec_parm
);
23164 /* Produce the definition of D, a _DECL generated from a template. If
23165 DEFER_OK is true, then we don't have to actually do the
23166 instantiation now; we just have to do it sometime. Normally it is
23167 an error if this is an explicit instantiation but D is undefined.
23168 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23169 instantiated class template. */
23172 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23174 tree tmpl
= DECL_TI_TEMPLATE (d
);
23181 bool pattern_defined
;
23182 location_t saved_loc
= input_location
;
23183 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23184 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23188 /* This function should only be used to instantiate templates for
23189 functions and static member variables. */
23190 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23192 /* A concept is never instantiated. */
23193 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23195 /* Variables are never deferred; if instantiation is required, they
23196 are instantiated right away. That allows for better code in the
23197 case that an expression refers to the value of the variable --
23198 if the variable has a constant value the referring expression can
23199 take advantage of that fact. */
23203 /* Don't instantiate cloned functions. Instead, instantiate the
23204 functions they cloned. */
23205 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23206 d
= DECL_CLONED_FUNCTION (d
);
23208 if (DECL_TEMPLATE_INSTANTIATED (d
)
23209 || (TREE_CODE (d
) == FUNCTION_DECL
23210 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23211 || DECL_TEMPLATE_SPECIALIZATION (d
))
23212 /* D has already been instantiated or explicitly specialized, so
23213 there's nothing for us to do here.
23215 It might seem reasonable to check whether or not D is an explicit
23216 instantiation, and, if so, stop here. But when an explicit
23217 instantiation is deferred until the end of the compilation,
23218 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23219 the instantiation. */
23222 /* Check to see whether we know that this template will be
23223 instantiated in some other file, as with "extern template"
23225 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23227 /* In general, we do not instantiate such templates. */
23228 if (external_p
&& !always_instantiate_p (d
))
23231 gen_tmpl
= most_general_template (tmpl
);
23232 gen_args
= DECL_TI_ARGS (d
);
23234 if (tmpl
!= gen_tmpl
)
23235 /* We should already have the extra args. */
23236 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23237 == TMPL_ARGS_DEPTH (gen_args
));
23238 /* And what's in the hash table should match D. */
23239 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23240 || spec
== NULL_TREE
);
23242 /* This needs to happen before any tsubsting. */
23243 if (! push_tinst_level (d
))
23246 timevar_push (TV_TEMPLATE_INST
);
23248 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23249 for the instantiation. */
23250 td
= template_for_substitution (d
);
23255 /* Look up an explicit specialization, if any. */
23256 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23257 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23258 if (elt
&& elt
!= error_mark_node
)
23260 td
= TREE_VALUE (elt
);
23261 args
= TREE_PURPOSE (elt
);
23265 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23267 /* We should never be trying to instantiate a member of a class
23268 template or partial specialization. */
23269 gcc_assert (d
!= code_pattern
);
23271 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23272 || DECL_TEMPLATE_SPECIALIZATION (td
))
23273 /* In the case of a friend template whose definition is provided
23274 outside the class, we may have too many arguments. Drop the
23275 ones we don't need. The same is true for specializations. */
23276 args
= get_innermost_template_args
23277 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23279 if (TREE_CODE (d
) == FUNCTION_DECL
)
23281 deleted_p
= DECL_DELETED_FN (code_pattern
);
23282 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23283 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23284 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23290 if (DECL_CLASS_SCOPE_P (code_pattern
))
23291 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23292 || DECL_INLINE_VAR_P (code_pattern
));
23294 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23297 /* We may be in the middle of deferred access check. Disable it now. */
23298 push_deferring_access_checks (dk_no_deferred
);
23300 /* Unless an explicit instantiation directive has already determined
23301 the linkage of D, remember that a definition is available for
23303 if (pattern_defined
23304 && !DECL_INTERFACE_KNOWN (d
)
23305 && !DECL_NOT_REALLY_EXTERN (d
))
23306 mark_definable (d
);
23308 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23309 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23310 input_location
= DECL_SOURCE_LOCATION (d
);
23312 /* If D is a member of an explicitly instantiated class template,
23313 and no definition is available, treat it like an implicit
23315 if (!pattern_defined
&& expl_inst_class_mem_p
23316 && DECL_EXPLICIT_INSTANTIATION (d
))
23318 /* Leave linkage flags alone on instantiations with anonymous
23320 if (TREE_PUBLIC (d
))
23322 DECL_NOT_REALLY_EXTERN (d
) = 0;
23323 DECL_INTERFACE_KNOWN (d
) = 0;
23325 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23328 /* Defer all other templates, unless we have been explicitly
23329 forbidden from doing so. */
23330 if (/* If there is no definition, we cannot instantiate the
23333 /* If it's OK to postpone instantiation, do so. */
23335 /* If this is a static data member that will be defined
23336 elsewhere, we don't want to instantiate the entire data
23337 member, but we do want to instantiate the initializer so that
23338 we can substitute that elsewhere. */
23339 || (external_p
&& VAR_P (d
))
23340 /* Handle here a deleted function too, avoid generating
23341 its body (c++/61080). */
23344 /* The definition of the static data member is now required so
23345 we must substitute the initializer. */
23347 && !DECL_INITIAL (d
)
23348 && DECL_INITIAL (code_pattern
))
23352 bool const_init
= false;
23353 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23355 ns
= decl_namespace_context (d
);
23356 push_nested_namespace (ns
);
23358 push_nested_class (DECL_CONTEXT (d
));
23359 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23361 tf_warning_or_error
, NULL_TREE
,
23362 /*integral_constant_expression_p=*/false);
23363 /* If instantiating the initializer involved instantiating this
23364 again, don't call cp_finish_decl twice. */
23365 if (!DECL_INITIAL (d
))
23367 /* Make sure the initializer is still constant, in case of
23368 circular dependency (template/instantiate6.C). */
23370 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23371 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23372 /*asmspec_tree=*/NULL_TREE
,
23373 LOOKUP_ONLYCONVERTING
);
23376 pop_nested_class ();
23377 pop_nested_namespace (ns
);
23380 /* We restore the source position here because it's used by
23381 add_pending_template. */
23382 input_location
= saved_loc
;
23384 if (at_eof
&& !pattern_defined
23385 && DECL_EXPLICIT_INSTANTIATION (d
)
23386 && DECL_NOT_REALLY_EXTERN (d
))
23389 The definition of a non-exported function template, a
23390 non-exported member function template, or a non-exported
23391 member function or static data member of a class template
23392 shall be present in every translation unit in which it is
23393 explicitly instantiated. */
23394 permerror (input_location
, "explicit instantiation of %qD "
23395 "but no definition available", d
);
23397 /* If we're in unevaluated context, we just wanted to get the
23398 constant value; this isn't an odr use, so don't queue
23399 a full instantiation. */
23400 if (cp_unevaluated_operand
!= 0)
23402 /* ??? Historically, we have instantiated inline functions, even
23403 when marked as "extern template". */
23404 if (!(external_p
&& VAR_P (d
)))
23405 add_pending_template (d
);
23408 /* Tell the repository that D is available in this translation unit
23409 -- and see if it is supposed to be instantiated here. */
23410 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23412 /* In a PCH file, despite the fact that the repository hasn't
23413 requested instantiation in the PCH it is still possible that
23414 an instantiation will be required in a file that includes the
23417 add_pending_template (d
);
23418 /* Instantiate inline functions so that the inliner can do its
23419 job, even though we'll not be emitting a copy of this
23421 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23425 bool push_to_top
, nested
;
23427 fn_context
= decl_function_context (d
);
23428 nested
= current_function_decl
!= NULL_TREE
;
23429 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23431 vec
<tree
> omp_privatization_save
;
23433 save_omp_privatization_clauses (omp_privatization_save
);
23436 push_to_top_level ();
23439 push_function_context ();
23440 cp_unevaluated_operand
= 0;
23441 c_inhibit_evaluation_warnings
= 0;
23444 /* Mark D as instantiated so that recursive calls to
23445 instantiate_decl do not try to instantiate it again. */
23446 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23448 /* Regenerate the declaration in case the template has been modified
23449 by a subsequent redeclaration. */
23450 regenerate_decl_from_template (d
, td
, args
);
23452 /* We already set the file and line above. Reset them now in case
23453 they changed as a result of calling regenerate_decl_from_template. */
23454 input_location
= DECL_SOURCE_LOCATION (d
);
23459 bool const_init
= false;
23461 /* Clear out DECL_RTL; whatever was there before may not be right
23462 since we've reset the type of the declaration. */
23463 SET_DECL_RTL (d
, NULL
);
23464 DECL_IN_AGGR_P (d
) = 0;
23466 /* The initializer is placed in DECL_INITIAL by
23467 regenerate_decl_from_template so we don't need to
23468 push/pop_access_scope again here. Pull it out so that
23469 cp_finish_decl can process it. */
23470 init
= DECL_INITIAL (d
);
23471 DECL_INITIAL (d
) = NULL_TREE
;
23472 DECL_INITIALIZED_P (d
) = 0;
23474 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23475 initializer. That function will defer actual emission until
23476 we have a chance to determine linkage. */
23477 DECL_EXTERNAL (d
) = 0;
23479 /* Enter the scope of D so that access-checking works correctly. */
23480 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23482 push_nested_class (DECL_CONTEXT (d
));
23484 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23485 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23488 pop_nested_class ();
23490 if (variable_template_p (gen_tmpl
))
23491 note_variable_template_instantiation (d
);
23493 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23494 synthesize_method (d
);
23495 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23497 /* Set up the list of local specializations. */
23498 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23499 tree block
= NULL_TREE
;
23501 /* Set up context. */
23502 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23503 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23504 block
= push_stmt_list ();
23506 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23508 /* Some typedefs referenced from within the template code need to be
23509 access checked at template instantiation time, i.e now. These
23510 types were added to the template at parsing time. Let's get those
23511 and perform the access checks then. */
23512 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23515 /* Create substitution entries for the parameters. */
23516 register_parameter_specializations (code_pattern
, d
);
23518 /* Substitute into the body of the function. */
23519 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23520 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23521 tf_warning_or_error
, tmpl
);
23524 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23525 tf_warning_or_error
, tmpl
,
23526 /*integral_constant_expression_p=*/false);
23528 /* Set the current input_location to the end of the function
23529 so that finish_function knows where we are. */
23531 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23533 /* Remember if we saw an infinite loop in the template. */
23534 current_function_infinite_loop
23535 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23538 /* Finish the function. */
23539 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23540 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23541 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23544 d
= finish_function (/*inline_p=*/false);
23545 expand_or_defer_fn (d
);
23548 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23549 cp_check_omp_declare_reduction (d
);
23552 /* We're not deferring instantiation any more. */
23553 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23556 pop_from_top_level ();
23558 pop_function_context ();
23561 restore_omp_privatization_clauses (omp_privatization_save
);
23564 pop_deferring_access_checks ();
23565 timevar_pop (TV_TEMPLATE_INST
);
23566 pop_tinst_level ();
23567 input_location
= saved_loc
;
23568 cp_unevaluated_operand
= saved_unevaluated_operand
;
23569 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23574 /* Run through the list of templates that we wish we could
23575 instantiate, and instantiate any we can. RETRIES is the
23576 number of times we retry pending template instantiation. */
23579 instantiate_pending_templates (int retries
)
23582 location_t saved_loc
= input_location
;
23584 /* Instantiating templates may trigger vtable generation. This in turn
23585 may require further template instantiations. We place a limit here
23586 to avoid infinite loop. */
23587 if (pending_templates
&& retries
>= max_tinst_depth
)
23589 tree decl
= pending_templates
->tinst
->decl
;
23591 fatal_error (input_location
,
23592 "template instantiation depth exceeds maximum of %d"
23593 " instantiating %q+D, possibly from virtual table generation"
23594 " (use -ftemplate-depth= to increase the maximum)",
23595 max_tinst_depth
, decl
);
23596 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23597 /* Pretend that we defined it. */
23598 DECL_INITIAL (decl
) = error_mark_node
;
23604 struct pending_template
**t
= &pending_templates
;
23605 struct pending_template
*last
= NULL
;
23609 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23610 bool complete
= false;
23612 if (TYPE_P (instantiation
))
23614 if (!COMPLETE_TYPE_P (instantiation
))
23616 instantiate_class_template (instantiation
);
23617 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23618 for (tree fld
= TYPE_FIELDS (instantiation
);
23619 fld
; fld
= TREE_CHAIN (fld
))
23621 || (TREE_CODE (fld
) == FUNCTION_DECL
23622 && !DECL_ARTIFICIAL (fld
)))
23623 && DECL_TEMPLATE_INSTANTIATION (fld
))
23624 instantiate_decl (fld
,
23625 /*defer_ok=*/false,
23626 /*expl_inst_class_mem_p=*/false);
23628 if (COMPLETE_TYPE_P (instantiation
))
23632 complete
= COMPLETE_TYPE_P (instantiation
);
23636 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23637 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23640 = instantiate_decl (instantiation
,
23641 /*defer_ok=*/false,
23642 /*expl_inst_class_mem_p=*/false);
23643 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23647 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23648 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23652 /* If INSTANTIATION has been instantiated, then we don't
23653 need to consider it again in the future. */
23661 current_tinst_level
= NULL
;
23663 last_pending_template
= last
;
23665 while (reconsider
);
23667 input_location
= saved_loc
;
23670 /* Substitute ARGVEC into T, which is a list of initializers for
23671 either base class or a non-static data member. The TREE_PURPOSEs
23672 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23673 instantiate_decl. */
23676 tsubst_initializer_list (tree t
, tree argvec
)
23678 tree inits
= NULL_TREE
;
23679 tree target_ctor
= error_mark_node
;
23681 for (; t
; t
= TREE_CHAIN (t
))
23685 tree expanded_bases
= NULL_TREE
;
23686 tree expanded_arguments
= NULL_TREE
;
23689 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23694 /* Expand the base class expansion type into separate base
23696 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23697 tf_warning_or_error
,
23699 if (expanded_bases
== error_mark_node
)
23702 /* We'll be building separate TREE_LISTs of arguments for
23704 len
= TREE_VEC_LENGTH (expanded_bases
);
23705 expanded_arguments
= make_tree_vec (len
);
23706 for (i
= 0; i
< len
; i
++)
23707 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23709 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23710 expand each argument in the TREE_VALUE of t. */
23711 expr
= make_node (EXPR_PACK_EXPANSION
);
23712 PACK_EXPANSION_LOCAL_P (expr
) = true;
23713 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23714 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23716 if (TREE_VALUE (t
) == void_type_node
)
23717 /* VOID_TYPE_NODE is used to indicate
23718 value-initialization. */
23720 for (i
= 0; i
< len
; i
++)
23721 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23725 /* Substitute parameter packs into each argument in the
23727 in_base_initializer
= 1;
23728 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23730 tree expanded_exprs
;
23732 /* Expand the argument. */
23733 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23735 = tsubst_pack_expansion (expr
, argvec
,
23736 tf_warning_or_error
,
23738 if (expanded_exprs
== error_mark_node
)
23741 /* Prepend each of the expanded expressions to the
23742 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23743 for (i
= 0; i
< len
; i
++)
23745 TREE_VEC_ELT (expanded_arguments
, i
) =
23746 tree_cons (NULL_TREE
,
23747 TREE_VEC_ELT (expanded_exprs
, i
),
23748 TREE_VEC_ELT (expanded_arguments
, i
));
23751 in_base_initializer
= 0;
23753 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23754 since we built them backwards. */
23755 for (i
= 0; i
< len
; i
++)
23757 TREE_VEC_ELT (expanded_arguments
, i
) =
23758 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23763 for (i
= 0; i
< len
; ++i
)
23765 if (expanded_bases
)
23767 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23768 decl
= expand_member_init (decl
);
23769 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23774 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23775 tf_warning_or_error
, NULL_TREE
);
23777 decl
= expand_member_init (decl
);
23778 if (decl
&& !DECL_P (decl
))
23779 in_base_initializer
= 1;
23781 init
= TREE_VALUE (t
);
23783 if (init
!= void_type_node
)
23784 init
= tsubst_expr (init
, argvec
,
23785 tf_warning_or_error
, NULL_TREE
,
23786 /*integral_constant_expression_p=*/false);
23787 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23788 /* If we had an initializer but it instantiated to nothing,
23789 value-initialize the object. This will only occur when
23790 the initializer was a pack expansion where the parameter
23791 packs used in that expansion were of length zero. */
23792 init
= void_type_node
;
23793 in_base_initializer
= 0;
23796 if (target_ctor
!= error_mark_node
23797 && init
!= error_mark_node
)
23799 error ("mem-initializer for %qD follows constructor delegation",
23803 /* Look for a target constructor. */
23804 if (init
!= error_mark_node
23805 && decl
&& CLASS_TYPE_P (decl
)
23806 && same_type_p (decl
, current_class_type
))
23808 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
23811 error ("constructor delegation follows mem-initializer for %qD",
23812 TREE_PURPOSE (inits
));
23815 target_ctor
= init
;
23820 init
= build_tree_list (decl
, init
);
23821 TREE_CHAIN (init
) = inits
;
23829 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23832 set_current_access_from_decl (tree decl
)
23834 if (TREE_PRIVATE (decl
))
23835 current_access_specifier
= access_private_node
;
23836 else if (TREE_PROTECTED (decl
))
23837 current_access_specifier
= access_protected_node
;
23839 current_access_specifier
= access_public_node
;
23842 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23843 is the instantiation (which should have been created with
23844 start_enum) and ARGS are the template arguments to use. */
23847 tsubst_enum (tree tag
, tree newtag
, tree args
)
23851 if (SCOPED_ENUM_P (newtag
))
23852 begin_scope (sk_scoped_enum
, newtag
);
23854 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23859 decl
= TREE_VALUE (e
);
23860 /* Note that in a template enum, the TREE_VALUE is the
23861 CONST_DECL, not the corresponding INTEGER_CST. */
23862 value
= tsubst_expr (DECL_INITIAL (decl
),
23863 args
, tf_warning_or_error
, NULL_TREE
,
23864 /*integral_constant_expression_p=*/true);
23866 /* Give this enumeration constant the correct access. */
23867 set_current_access_from_decl (decl
);
23869 /* Actually build the enumerator itself. Here we're assuming that
23870 enumerators can't have dependent attributes. */
23871 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23872 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23875 if (SCOPED_ENUM_P (newtag
))
23878 finish_enum_value_list (newtag
);
23879 finish_enum (newtag
);
23881 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23882 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23885 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23886 its type -- but without substituting the innermost set of template
23887 arguments. So, innermost set of template parameters will appear in
23891 get_mostly_instantiated_function_type (tree decl
)
23893 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23894 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23897 /* Return truthvalue if we're processing a template different from
23898 the last one involved in diagnostics. */
23900 problematic_instantiation_changed (void)
23902 return current_tinst_level
!= last_error_tinst_level
;
23905 /* Remember current template involved in diagnostics. */
23907 record_last_problematic_instantiation (void)
23909 last_error_tinst_level
= current_tinst_level
;
23912 struct tinst_level
*
23913 current_instantiation (void)
23915 return current_tinst_level
;
23918 /* Return TRUE if current_function_decl is being instantiated, false
23922 instantiating_current_function_p (void)
23924 return (current_instantiation ()
23925 && current_instantiation ()->decl
== current_function_decl
);
23928 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23929 type. Return false for ok, true for disallowed. Issue error and
23930 inform messages under control of COMPLAIN. */
23933 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23935 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23937 else if (POINTER_TYPE_P (type
))
23939 else if (TYPE_PTRMEM_P (type
))
23941 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23943 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23945 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23947 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23949 /* A bound template template parm could later be instantiated to have a valid
23950 nontype parm type via an alias template. */
23951 else if (cxx_dialect
>= cxx11
23952 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23955 if (complain
& tf_error
)
23957 if (type
== error_mark_node
)
23958 inform (input_location
, "invalid template non-type parameter");
23960 error ("%q#T is not a valid type for a template non-type parameter",
23966 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23967 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23970 dependent_type_p_r (tree type
)
23976 A type is dependent if it is:
23978 -- a template parameter. Template template parameters are types
23979 for us (since TYPE_P holds true for them) so we handle
23981 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23982 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23984 /* -- a qualified-id with a nested-name-specifier which contains a
23985 class-name that names a dependent type or whose unqualified-id
23986 names a dependent type. */
23987 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23990 /* An alias template specialization can be dependent even if the
23991 resulting type is not. */
23992 if (dependent_alias_template_spec_p (type
))
23995 /* -- a cv-qualified type where the cv-unqualified type is
23997 No code is necessary for this bullet; the code below handles
23998 cv-qualified types, and we don't want to strip aliases with
23999 TYPE_MAIN_VARIANT because of DR 1558. */
24000 /* -- a compound type constructed from any dependent type. */
24001 if (TYPE_PTRMEM_P (type
))
24002 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
24003 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24005 else if (TYPE_PTR_P (type
)
24006 || TREE_CODE (type
) == REFERENCE_TYPE
)
24007 return dependent_type_p (TREE_TYPE (type
));
24008 else if (TREE_CODE (type
) == FUNCTION_TYPE
24009 || TREE_CODE (type
) == METHOD_TYPE
)
24013 if (dependent_type_p (TREE_TYPE (type
)))
24015 for (arg_type
= TYPE_ARG_TYPES (type
);
24017 arg_type
= TREE_CHAIN (arg_type
))
24018 if (dependent_type_p (TREE_VALUE (arg_type
)))
24020 if (cxx_dialect
>= cxx17
)
24021 /* A value-dependent noexcept-specifier makes the type dependent. */
24022 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
24023 if (tree noex
= TREE_PURPOSE (spec
))
24024 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24025 affect overload resolution and treating it as dependent breaks
24027 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
24028 && value_dependent_expression_p (noex
))
24032 /* -- an array type constructed from any dependent type or whose
24033 size is specified by a constant expression that is
24036 We checked for type- and value-dependence of the bounds in
24037 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24038 if (TREE_CODE (type
) == ARRAY_TYPE
)
24040 if (TYPE_DOMAIN (type
)
24041 && dependent_type_p (TYPE_DOMAIN (type
)))
24043 return dependent_type_p (TREE_TYPE (type
));
24046 /* -- a template-id in which either the template name is a template
24048 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24050 /* ... or any of the template arguments is a dependent type or
24051 an expression that is type-dependent or value-dependent. */
24052 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
24053 && (any_dependent_template_arguments_p
24054 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
24057 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24058 dependent; if the argument of the `typeof' expression is not
24059 type-dependent, then it should already been have resolved. */
24060 if (TREE_CODE (type
) == TYPEOF_TYPE
24061 || TREE_CODE (type
) == DECLTYPE_TYPE
24062 || TREE_CODE (type
) == UNDERLYING_TYPE
)
24065 /* A template argument pack is dependent if any of its packed
24067 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
24069 tree args
= ARGUMENT_PACK_ARGS (type
);
24070 int i
, len
= TREE_VEC_LENGTH (args
);
24071 for (i
= 0; i
< len
; ++i
)
24072 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24076 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24077 be template parameters. */
24078 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
24081 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
24084 /* The standard does not specifically mention types that are local
24085 to template functions or local classes, but they should be
24086 considered dependent too. For example:
24088 template <int I> void f() {
24093 The size of `E' cannot be known until the value of `I' has been
24094 determined. Therefore, `E' must be considered dependent. */
24095 scope
= TYPE_CONTEXT (type
);
24096 if (scope
&& TYPE_P (scope
))
24097 return dependent_type_p (scope
);
24098 /* Don't use type_dependent_expression_p here, as it can lead
24099 to infinite recursion trying to determine whether a lambda
24100 nested in a lambda is dependent (c++/47687). */
24101 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
24102 && DECL_LANG_SPECIFIC (scope
)
24103 && DECL_TEMPLATE_INFO (scope
)
24104 && (any_dependent_template_arguments_p
24105 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
24108 /* Other types are non-dependent. */
24112 /* Returns TRUE if TYPE is dependent, in the sense of
24113 [temp.dep.type]. Note that a NULL type is considered dependent. */
24116 dependent_type_p (tree type
)
24118 /* If there are no template parameters in scope, then there can't be
24119 any dependent types. */
24120 if (!processing_template_decl
)
24122 /* If we are not processing a template, then nobody should be
24123 providing us with a dependent type. */
24125 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
24129 /* If the type is NULL, we have not computed a type for the entity
24130 in question; in that case, the type is dependent. */
24134 /* Erroneous types can be considered non-dependent. */
24135 if (type
== error_mark_node
)
24138 /* Getting here with global_type_node means we improperly called this
24139 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24140 gcc_checking_assert (type
!= global_type_node
);
24142 /* If we have not already computed the appropriate value for TYPE,
24144 if (!TYPE_DEPENDENT_P_VALID (type
))
24146 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
24147 TYPE_DEPENDENT_P_VALID (type
) = 1;
24150 return TYPE_DEPENDENT_P (type
);
24153 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24154 lookup. In other words, a dependent type that is not the current
24158 dependent_scope_p (tree scope
)
24160 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24161 && !currently_open_class (scope
));
24164 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24165 an unknown base of 'this' (and is therefore instantiation-dependent). */
24168 unknown_base_ref_p (tree t
)
24170 if (!current_class_ptr
)
24173 tree mem
= TREE_OPERAND (t
, 1);
24174 if (shared_member_p (mem
))
24177 tree cur
= current_nonlambda_class_type ();
24178 if (!any_dependent_bases_p (cur
))
24181 tree ctx
= TREE_OPERAND (t
, 0);
24182 if (DERIVED_FROM_P (ctx
, cur
))
24188 /* T is a SCOPE_REF; return whether we need to consider it
24189 instantiation-dependent so that we can check access at instantiation
24190 time even though we know which member it resolves to. */
24193 instantiation_dependent_scope_ref_p (tree t
)
24195 if (DECL_P (TREE_OPERAND (t
, 1))
24196 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24197 && !unknown_base_ref_p (t
)
24198 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24199 TREE_OPERAND (t
, 1)))
24205 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24206 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24209 /* Note that this predicate is not appropriate for general expressions;
24210 only constant expressions (that satisfy potential_constant_expression)
24211 can be tested for value dependence. */
24214 value_dependent_expression_p (tree expression
)
24216 if (!processing_template_decl
|| expression
== NULL_TREE
)
24219 /* A type-dependent expression is also value-dependent. */
24220 if (type_dependent_expression_p (expression
))
24223 switch (TREE_CODE (expression
))
24226 /* A dependent member function of the current instantiation. */
24227 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24229 case FUNCTION_DECL
:
24230 /* A dependent member function of the current instantiation. */
24231 if (DECL_CLASS_SCOPE_P (expression
)
24232 && dependent_type_p (DECL_CONTEXT (expression
)))
24236 case IDENTIFIER_NODE
:
24237 /* A name that has not been looked up -- must be dependent. */
24240 case TEMPLATE_PARM_INDEX
:
24241 /* A non-type template parm. */
24245 /* A non-type template parm. */
24246 if (DECL_TEMPLATE_PARM_P (expression
))
24248 return value_dependent_expression_p (DECL_INITIAL (expression
));
24251 /* A constant with literal type and is initialized
24252 with an expression that is value-dependent. */
24253 if (DECL_DEPENDENT_INIT_P (expression
)
24254 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24255 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24257 if (DECL_HAS_VALUE_EXPR_P (expression
))
24259 tree value_expr
= DECL_VALUE_EXPR (expression
);
24260 if (value_dependent_expression_p (value_expr
))
24265 case DYNAMIC_CAST_EXPR
:
24266 case STATIC_CAST_EXPR
:
24267 case CONST_CAST_EXPR
:
24268 case REINTERPRET_CAST_EXPR
:
24270 case IMPLICIT_CONV_EXPR
:
24271 /* These expressions are value-dependent if the type to which
24272 the cast occurs is dependent or the expression being casted
24273 is value-dependent. */
24275 tree type
= TREE_TYPE (expression
);
24277 if (dependent_type_p (type
))
24280 /* A functional cast has a list of operands. */
24281 expression
= TREE_OPERAND (expression
, 0);
24284 /* If there are no operands, it must be an expression such
24285 as "int()". This should not happen for aggregate types
24286 because it would form non-constant expressions. */
24287 gcc_assert (cxx_dialect
>= cxx11
24288 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24293 if (TREE_CODE (expression
) == TREE_LIST
)
24294 return any_value_dependent_elements_p (expression
);
24296 return value_dependent_expression_p (expression
);
24300 if (SIZEOF_EXPR_TYPE_P (expression
))
24301 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24305 /* A `sizeof' expression is value-dependent if the operand is
24306 type-dependent or is a pack expansion. */
24307 expression
= TREE_OPERAND (expression
, 0);
24308 if (PACK_EXPANSION_P (expression
))
24310 else if (TYPE_P (expression
))
24311 return dependent_type_p (expression
);
24312 return instantiation_dependent_uneval_expression_p (expression
);
24314 case AT_ENCODE_EXPR
:
24315 /* An 'encode' expression is value-dependent if the operand is
24317 expression
= TREE_OPERAND (expression
, 0);
24318 return dependent_type_p (expression
);
24320 case NOEXCEPT_EXPR
:
24321 expression
= TREE_OPERAND (expression
, 0);
24322 return instantiation_dependent_uneval_expression_p (expression
);
24325 /* All instantiation-dependent expressions should also be considered
24326 value-dependent. */
24327 return instantiation_dependent_scope_ref_p (expression
);
24329 case COMPONENT_REF
:
24330 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24331 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24333 case NONTYPE_ARGUMENT_PACK
:
24334 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24335 is value-dependent. */
24337 tree values
= ARGUMENT_PACK_ARGS (expression
);
24338 int i
, len
= TREE_VEC_LENGTH (values
);
24340 for (i
= 0; i
< len
; ++i
)
24341 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24349 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24351 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24357 if (TREE_CODE (type2
) != TREE_LIST
)
24358 return dependent_type_p (type2
);
24360 for (; type2
; type2
= TREE_CHAIN (type2
))
24361 if (dependent_type_p (TREE_VALUE (type2
)))
24368 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24369 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24372 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24373 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24377 tree op
= TREE_OPERAND (expression
, 0);
24378 return (value_dependent_expression_p (op
)
24379 || has_value_dependent_address (op
));
24382 case REQUIRES_EXPR
:
24383 /* Treat all requires-expressions as value-dependent so
24384 we don't try to fold them. */
24388 return dependent_type_p (TREE_OPERAND (expression
, 0));
24392 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24394 tree fn
= get_callee_fndecl (expression
);
24396 nargs
= call_expr_nargs (expression
);
24397 for (i
= 0; i
< nargs
; ++i
)
24399 tree op
= CALL_EXPR_ARG (expression
, i
);
24400 /* In a call to a constexpr member function, look through the
24401 implicit ADDR_EXPR on the object argument so that it doesn't
24402 cause the call to be considered value-dependent. We also
24403 look through it in potential_constant_expression. */
24404 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24405 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24406 && TREE_CODE (op
) == ADDR_EXPR
)
24407 op
= TREE_OPERAND (op
, 0);
24408 if (value_dependent_expression_p (op
))
24414 case TEMPLATE_ID_EXPR
:
24415 return variable_concept_p (TREE_OPERAND (expression
, 0));
24421 if (dependent_type_p (TREE_TYPE (expression
)))
24423 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24424 if (value_dependent_expression_p (val
))
24430 /* Treat a GNU statement expression as dependent to avoid crashing
24431 under instantiate_non_dependent_expr; it can't be constant. */
24435 /* A constant expression is value-dependent if any subexpression is
24436 value-dependent. */
24437 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24439 case tcc_reference
:
24441 case tcc_comparison
:
24443 case tcc_expression
:
24446 int i
, len
= cp_tree_operand_length (expression
);
24448 for (i
= 0; i
< len
; i
++)
24450 tree t
= TREE_OPERAND (expression
, i
);
24452 /* In some cases, some of the operands may be missing.
24453 (For example, in the case of PREDECREMENT_EXPR, the
24454 amount to increment by may be missing.) That doesn't
24455 make the expression dependent. */
24456 if (t
&& value_dependent_expression_p (t
))
24467 /* The expression is not value-dependent. */
24471 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24472 [temp.dep.expr]. Note that an expression with no type is
24473 considered dependent. Other parts of the compiler arrange for an
24474 expression with type-dependent subexpressions to have no type, so
24475 this function doesn't have to be fully recursive. */
24478 type_dependent_expression_p (tree expression
)
24480 if (!processing_template_decl
)
24483 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24486 STRIP_ANY_LOCATION_WRAPPER (expression
);
24488 /* An unresolved name is always dependent. */
24489 if (identifier_p (expression
)
24490 || TREE_CODE (expression
) == USING_DECL
24491 || TREE_CODE (expression
) == WILDCARD_DECL
)
24494 /* A fold expression is type-dependent. */
24495 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24496 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24497 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24498 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24501 /* Some expression forms are never type-dependent. */
24502 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24503 || TREE_CODE (expression
) == SIZEOF_EXPR
24504 || TREE_CODE (expression
) == ALIGNOF_EXPR
24505 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24506 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24507 || TREE_CODE (expression
) == TRAIT_EXPR
24508 || TREE_CODE (expression
) == TYPEID_EXPR
24509 || TREE_CODE (expression
) == DELETE_EXPR
24510 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24511 || TREE_CODE (expression
) == THROW_EXPR
24512 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24515 /* The types of these expressions depends only on the type to which
24516 the cast occurs. */
24517 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24518 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24519 || TREE_CODE (expression
) == CONST_CAST_EXPR
24520 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24521 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24522 || TREE_CODE (expression
) == CAST_EXPR
)
24523 return dependent_type_p (TREE_TYPE (expression
));
24525 /* The types of these expressions depends only on the type created
24526 by the expression. */
24527 if (TREE_CODE (expression
) == NEW_EXPR
24528 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24530 /* For NEW_EXPR tree nodes created inside a template, either
24531 the object type itself or a TREE_LIST may appear as the
24533 tree type
= TREE_OPERAND (expression
, 1);
24534 if (TREE_CODE (type
) == TREE_LIST
)
24535 /* This is an array type. We need to check array dimensions
24537 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24538 || value_dependent_expression_p
24539 (TREE_OPERAND (TREE_VALUE (type
), 1));
24541 return dependent_type_p (type
);
24544 if (TREE_CODE (expression
) == SCOPE_REF
)
24546 tree scope
= TREE_OPERAND (expression
, 0);
24547 tree name
= TREE_OPERAND (expression
, 1);
24549 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24550 contains an identifier associated by name lookup with one or more
24551 declarations declared with a dependent type, or...a
24552 nested-name-specifier or qualified-id that names a member of an
24553 unknown specialization. */
24554 return (type_dependent_expression_p (name
)
24555 || dependent_scope_p (scope
));
24558 if (TREE_CODE (expression
) == TEMPLATE_DECL
24559 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24560 return uses_outer_template_parms (expression
);
24562 if (TREE_CODE (expression
) == STMT_EXPR
)
24563 expression
= stmt_expr_value_expr (expression
);
24565 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24570 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24572 if (type_dependent_expression_p (elt
))
24578 /* A static data member of the current instantiation with incomplete
24579 array type is type-dependent, as the definition and specializations
24580 can have different bounds. */
24581 if (VAR_P (expression
)
24582 && DECL_CLASS_SCOPE_P (expression
)
24583 && dependent_type_p (DECL_CONTEXT (expression
))
24584 && VAR_HAD_UNKNOWN_BOUND (expression
))
24587 /* An array of unknown bound depending on a variadic parameter, eg:
24589 template<typename... Args>
24590 void foo (Args... args)
24592 int arr[] = { args... };
24595 template<int... vals>
24598 int arr[] = { vals... };
24601 If the array has no length and has an initializer, it must be that
24602 we couldn't determine its length in cp_complete_array_type because
24603 it is dependent. */
24604 if (VAR_P (expression
)
24605 && TREE_TYPE (expression
) != NULL_TREE
24606 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24607 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24608 && DECL_INITIAL (expression
))
24611 /* A function or variable template-id is type-dependent if it has any
24612 dependent template arguments. */
24613 if (VAR_OR_FUNCTION_DECL_P (expression
)
24614 && DECL_LANG_SPECIFIC (expression
)
24615 && DECL_TEMPLATE_INFO (expression
))
24617 /* Consider the innermost template arguments, since those are the ones
24618 that come from the template-id; the template arguments for the
24619 enclosing class do not make it type-dependent unless they are used in
24620 the type of the decl. */
24621 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24622 && (any_dependent_template_arguments_p
24623 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24627 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24628 type-dependent. Checking this is important for functions with auto return
24629 type, which looks like a dependent type. */
24630 if (TREE_CODE (expression
) == FUNCTION_DECL
24631 && !(DECL_CLASS_SCOPE_P (expression
)
24632 && dependent_type_p (DECL_CONTEXT (expression
)))
24633 && !(DECL_FRIEND_P (expression
)
24634 && (!DECL_FRIEND_CONTEXT (expression
)
24635 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24636 && !DECL_LOCAL_FUNCTION_P (expression
))
24638 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24639 || undeduced_auto_decl (expression
));
24643 /* Always dependent, on the number of arguments if nothing else. */
24644 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24647 if (TREE_TYPE (expression
) == unknown_type_node
)
24649 if (TREE_CODE (expression
) == ADDR_EXPR
)
24650 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24651 if (TREE_CODE (expression
) == COMPONENT_REF
24652 || TREE_CODE (expression
) == OFFSET_REF
)
24654 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24656 expression
= TREE_OPERAND (expression
, 1);
24657 if (identifier_p (expression
))
24660 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24661 if (TREE_CODE (expression
) == SCOPE_REF
)
24664 if (BASELINK_P (expression
))
24666 if (BASELINK_OPTYPE (expression
)
24667 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24669 expression
= BASELINK_FUNCTIONS (expression
);
24672 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24674 if (any_dependent_template_arguments_p
24675 (TREE_OPERAND (expression
, 1)))
24677 expression
= TREE_OPERAND (expression
, 0);
24678 if (identifier_p (expression
))
24682 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24683 || TREE_CODE (expression
) == FUNCTION_DECL
);
24685 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24686 if (type_dependent_expression_p (*iter
))
24692 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24694 /* Dependent type attributes might not have made it from the decl to
24696 if (DECL_P (expression
)
24697 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24700 return (dependent_type_p (TREE_TYPE (expression
)));
24703 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24704 type-dependent if the expression refers to a member of the current
24705 instantiation and the type of the referenced member is dependent, or the
24706 class member access expression refers to a member of an unknown
24709 This function returns true if the OBJECT in such a class member access
24710 expression is of an unknown specialization. */
24713 type_dependent_object_expression_p (tree object
)
24715 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24717 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24719 tree scope
= TREE_TYPE (object
);
24720 return (!scope
|| dependent_scope_p (scope
));
24723 /* walk_tree callback function for instantiation_dependent_expression_p,
24724 below. Returns non-zero if a dependent subexpression is found. */
24727 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24732 /* We don't have to worry about decltype currently because decltype
24733 of an instantiation-dependent expr is a dependent type. This
24734 might change depending on the resolution of DR 1172. */
24735 *walk_subtrees
= false;
24738 enum tree_code code
= TREE_CODE (*tp
);
24741 /* Don't treat an argument list as dependent just because it has no
24747 case TEMPLATE_PARM_INDEX
:
24750 /* Handle expressions with type operands. */
24754 case AT_ENCODE_EXPR
:
24756 tree op
= TREE_OPERAND (*tp
, 0);
24757 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24758 op
= TREE_TYPE (op
);
24761 if (dependent_type_p (op
))
24765 *walk_subtrees
= false;
24772 case COMPONENT_REF
:
24773 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24774 /* In a template, finish_class_member_access_expr creates a
24775 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24776 type-dependent, so that we can check access control at
24777 instantiation time (PR 42277). See also Core issue 1273. */
24782 if (instantiation_dependent_scope_ref_p (*tp
))
24787 /* Treat statement-expressions as dependent. */
24791 /* Treat requires-expressions as dependent. */
24792 case REQUIRES_EXPR
:
24796 /* Treat calls to function concepts as dependent. */
24797 if (function_concept_check_p (*tp
))
24801 case TEMPLATE_ID_EXPR
:
24802 /* And variable concepts. */
24803 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24811 if (type_dependent_expression_p (*tp
))
24817 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24818 sense defined by the ABI:
24820 "An expression is instantiation-dependent if it is type-dependent
24821 or value-dependent, or it has a subexpression that is type-dependent
24822 or value-dependent."
24824 Except don't actually check value-dependence for unevaluated expressions,
24825 because in sizeof(i) we don't care about the value of i. Checking
24826 type-dependence will in turn check value-dependence of array bounds/template
24827 arguments as needed. */
24830 instantiation_dependent_uneval_expression_p (tree expression
)
24834 if (!processing_template_decl
)
24837 if (expression
== error_mark_node
)
24840 result
= cp_walk_tree_without_duplicates (&expression
,
24841 instantiation_dependent_r
, NULL
);
24842 return result
!= NULL_TREE
;
24845 /* As above, but also check value-dependence of the expression as a whole. */
24848 instantiation_dependent_expression_p (tree expression
)
24850 return (instantiation_dependent_uneval_expression_p (expression
)
24851 || value_dependent_expression_p (expression
));
24854 /* Like type_dependent_expression_p, but it also works while not processing
24855 a template definition, i.e. during substitution or mangling. */
24858 type_dependent_expression_p_push (tree expr
)
24861 ++processing_template_decl
;
24862 b
= type_dependent_expression_p (expr
);
24863 --processing_template_decl
;
24867 /* Returns TRUE if ARGS contains a type-dependent expression. */
24870 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24875 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24877 if (type_dependent_expression_p (arg
))
24883 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24884 expressions) contains any type-dependent expressions. */
24887 any_type_dependent_elements_p (const_tree list
)
24889 for (; list
; list
= TREE_CHAIN (list
))
24890 if (type_dependent_expression_p (TREE_VALUE (list
)))
24896 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24897 expressions) contains any value-dependent expressions. */
24900 any_value_dependent_elements_p (const_tree list
)
24902 for (; list
; list
= TREE_CHAIN (list
))
24903 if (value_dependent_expression_p (TREE_VALUE (list
)))
24909 /* Returns TRUE if the ARG (a template argument) is dependent. */
24912 dependent_template_arg_p (tree arg
)
24914 if (!processing_template_decl
)
24917 /* Assume a template argument that was wrongly written by the user
24918 is dependent. This is consistent with what
24919 any_dependent_template_arguments_p [that calls this function]
24921 if (!arg
|| arg
== error_mark_node
)
24924 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24925 arg
= argument_pack_select_arg (arg
);
24927 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24929 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24931 if (DECL_TEMPLATE_PARM_P (arg
))
24933 /* A member template of a dependent class is not necessarily
24934 type-dependent, but it is a dependent template argument because it
24935 will be a member of an unknown specialization to that template. */
24936 tree scope
= CP_DECL_CONTEXT (arg
);
24937 return TYPE_P (scope
) && dependent_type_p (scope
);
24939 else if (ARGUMENT_PACK_P (arg
))
24941 tree args
= ARGUMENT_PACK_ARGS (arg
);
24942 int i
, len
= TREE_VEC_LENGTH (args
);
24943 for (i
= 0; i
< len
; ++i
)
24945 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24951 else if (TYPE_P (arg
))
24952 return dependent_type_p (arg
);
24954 return (type_dependent_expression_p (arg
)
24955 || value_dependent_expression_p (arg
));
24958 /* Returns true if ARGS (a collection of template arguments) contains
24959 any types that require structural equality testing. */
24962 any_template_arguments_need_structural_equality_p (tree args
)
24969 if (args
== error_mark_node
)
24972 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24974 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24975 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24977 tree arg
= TREE_VEC_ELT (level
, j
);
24978 tree packed_args
= NULL_TREE
;
24981 if (ARGUMENT_PACK_P (arg
))
24983 /* Look inside the argument pack. */
24984 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24985 len
= TREE_VEC_LENGTH (packed_args
);
24988 for (k
= 0; k
< len
; ++k
)
24991 arg
= TREE_VEC_ELT (packed_args
, k
);
24993 if (error_operand_p (arg
))
24995 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24997 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24999 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
25000 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
25009 /* Returns true if ARGS (a collection of template arguments) contains
25010 any dependent arguments. */
25013 any_dependent_template_arguments_p (const_tree args
)
25020 if (args
== error_mark_node
)
25023 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25025 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25026 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25027 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
25034 /* Returns TRUE if the template TMPL is type-dependent. */
25037 dependent_template_p (tree tmpl
)
25039 if (TREE_CODE (tmpl
) == OVERLOAD
)
25041 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
25042 if (dependent_template_p (*iter
))
25047 /* Template template parameters are dependent. */
25048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
25049 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
25051 /* So are names that have not been looked up. */
25052 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
25057 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25060 dependent_template_id_p (tree tmpl
, tree args
)
25062 return (dependent_template_p (tmpl
)
25063 || any_dependent_template_arguments_p (args
));
25066 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25070 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
25074 if (!processing_template_decl
)
25077 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
25079 tree decl
= TREE_VEC_ELT (declv
, i
);
25080 tree init
= TREE_VEC_ELT (initv
, i
);
25081 tree cond
= TREE_VEC_ELT (condv
, i
);
25082 tree incr
= TREE_VEC_ELT (incrv
, i
);
25084 if (type_dependent_expression_p (decl
)
25085 || TREE_CODE (decl
) == SCOPE_REF
)
25088 if (init
&& type_dependent_expression_p (init
))
25091 if (type_dependent_expression_p (cond
))
25094 if (COMPARISON_CLASS_P (cond
)
25095 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
25096 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
25099 if (TREE_CODE (incr
) == MODOP_EXPR
)
25101 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
25102 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
25105 else if (type_dependent_expression_p (incr
))
25107 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
25109 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
25111 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
25113 tree t
= TREE_OPERAND (incr
, 1);
25114 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
25115 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
25124 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25125 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25126 no such TYPE can be found. Note that this function peers inside
25127 uninstantiated templates and therefore should be used only in
25128 extremely limited situations. ONLY_CURRENT_P restricts this
25129 peering to the currently open classes hierarchy (which is required
25130 when comparing types). */
25133 resolve_typename_type (tree type
, bool only_current_p
)
25142 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
25144 scope
= TYPE_CONTEXT (type
);
25145 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25146 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25147 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25148 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25149 identifier of the TYPENAME_TYPE anymore.
25150 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25151 TYPENAME_TYPE instead, we avoid messing up with a possible
25152 typedef variant case. */
25153 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25155 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25156 it first before we can figure out what NAME refers to. */
25157 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25159 if (TYPENAME_IS_RESOLVING_P (scope
))
25160 /* Given a class template A with a dependent base with nested type C,
25161 typedef typename A::C::C C will land us here, as trying to resolve
25162 the initial A::C leads to the local C typedef, which leads back to
25163 A::C::C. So we break the recursion now. */
25166 scope
= resolve_typename_type (scope
, only_current_p
);
25168 /* If we don't know what SCOPE refers to, then we cannot resolve the
25170 if (!CLASS_TYPE_P (scope
))
25172 /* If this is a typedef, we don't want to look inside (c++/11987). */
25173 if (typedef_variant_p (type
))
25175 /* If SCOPE isn't the template itself, it will not have a valid
25176 TYPE_FIELDS list. */
25177 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25178 /* scope is either the template itself or a compatible instantiation
25179 like X<T>, so look up the name in the original template. */
25180 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25181 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25182 gcc_checking_assert (uses_template_parms (scope
));
25183 /* If scope has no fields, it can't be a current instantiation. Check this
25184 before currently_open_class to avoid infinite recursion (71515). */
25185 if (!TYPE_FIELDS (scope
))
25187 /* If the SCOPE is not the current instantiation, there's no reason
25188 to look inside it. */
25189 if (only_current_p
&& !currently_open_class (scope
))
25191 /* Enter the SCOPE so that name lookup will be resolved as if we
25192 were in the class definition. In particular, SCOPE will no
25193 longer be considered a dependent type. */
25194 pushed_scope
= push_scope (scope
);
25195 /* Look up the declaration. */
25196 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25197 tf_warning_or_error
);
25199 result
= NULL_TREE
;
25201 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25202 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25203 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25206 else if (identifier_p (fullname
)
25207 && TREE_CODE (decl
) == TYPE_DECL
)
25209 result
= TREE_TYPE (decl
);
25210 if (result
== error_mark_node
)
25211 result
= NULL_TREE
;
25213 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25214 && DECL_CLASS_TEMPLATE_P (decl
))
25216 /* Obtain the template and the arguments. */
25217 tree tmpl
= TREE_OPERAND (fullname
, 0);
25218 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25220 /* We get here with a plain identifier because a previous tentative
25221 parse of the nested-name-specifier as part of a ptr-operator saw
25222 ::template X<A>. The use of ::template is necessary in a
25223 ptr-operator, but wrong in a declarator-id.
25225 [temp.names]: In a qualified-id of a declarator-id, the keyword
25226 template shall not appear at the top level. */
25227 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25228 "keyword %<template%> not allowed in declarator-id");
25231 tree args
= TREE_OPERAND (fullname
, 1);
25232 /* Instantiate the template. */
25233 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25234 /*entering_scope=*/true,
25235 tf_error
| tf_user
);
25236 if (result
== error_mark_node
)
25237 result
= NULL_TREE
;
25240 /* Leave the SCOPE. */
25242 pop_scope (pushed_scope
);
25244 /* If we failed to resolve it, return the original typename. */
25248 /* If lookup found a typename type, resolve that too. */
25249 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25251 /* Ill-formed programs can cause infinite recursion here, so we
25252 must catch that. */
25253 TYPENAME_IS_RESOLVING_P (result
) = 1;
25254 result
= resolve_typename_type (result
, only_current_p
);
25255 TYPENAME_IS_RESOLVING_P (result
) = 0;
25258 /* Qualify the resulting type. */
25259 quals
= cp_type_quals (type
);
25261 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25266 /* EXPR is an expression which is not type-dependent. Return a proxy
25267 for EXPR that can be used to compute the types of larger
25268 expressions containing EXPR. */
25271 build_non_dependent_expr (tree expr
)
25273 tree orig_expr
= expr
;
25276 /* When checking, try to get a constant value for all non-dependent
25277 expressions in order to expose bugs in *_dependent_expression_p
25278 and constexpr. This can affect code generation, see PR70704, so
25279 only do this for -fchecking=2. */
25280 if (flag_checking
> 1
25281 && cxx_dialect
>= cxx11
25282 /* Don't do this during nsdmi parsing as it can lead to
25283 unexpected recursive instantiations. */
25284 && !parsing_nsdmi ()
25285 /* Don't do this during concept expansion either and for
25286 the same reason. */
25287 && !expanding_concept ())
25288 fold_non_dependent_expr (expr
);
25290 STRIP_ANY_LOCATION_WRAPPER (expr
);
25292 /* Preserve OVERLOADs; the functions must be available to resolve
25295 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25296 inner_expr
= stmt_expr_value_expr (inner_expr
);
25297 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25298 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25299 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25300 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25301 if (is_overloaded_fn (inner_expr
)
25302 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25304 /* There is no need to return a proxy for a variable. */
25307 /* Preserve string constants; conversions from string constants to
25308 "char *" are allowed, even though normally a "const char *"
25309 cannot be used to initialize a "char *". */
25310 if (TREE_CODE (expr
) == STRING_CST
)
25312 /* Preserve void and arithmetic constants, as an optimization -- there is no
25313 reason to create a new node. */
25314 if (TREE_CODE (expr
) == VOID_CST
25315 || TREE_CODE (expr
) == INTEGER_CST
25316 || TREE_CODE (expr
) == REAL_CST
)
25318 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25319 There is at least one place where we want to know that a
25320 particular expression is a throw-expression: when checking a ?:
25321 expression, there are special rules if the second or third
25322 argument is a throw-expression. */
25323 if (TREE_CODE (expr
) == THROW_EXPR
)
25326 /* Don't wrap an initializer list, we need to be able to look inside. */
25327 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25330 /* Don't wrap a dummy object, we need to be able to test for it. */
25331 if (is_dummy_object (expr
))
25334 if (TREE_CODE (expr
) == COND_EXPR
)
25335 return build3 (COND_EXPR
,
25337 TREE_OPERAND (expr
, 0),
25338 (TREE_OPERAND (expr
, 1)
25339 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25340 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25341 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25342 if (TREE_CODE (expr
) == COMPOUND_EXPR
25343 && !COMPOUND_EXPR_OVERLOADED (expr
))
25344 return build2 (COMPOUND_EXPR
,
25346 TREE_OPERAND (expr
, 0),
25347 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25349 /* If the type is unknown, it can't really be non-dependent */
25350 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25352 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25353 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25356 /* ARGS is a vector of expressions as arguments to a function call.
25357 Replace the arguments with equivalent non-dependent expressions.
25358 This modifies ARGS in place. */
25361 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25366 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25368 tree newarg
= build_non_dependent_expr (arg
);
25370 (*args
)[ix
] = newarg
;
25374 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25375 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25376 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25379 make_auto_1 (tree name
, bool set_canonical
)
25381 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25382 TYPE_NAME (au
) = build_decl (input_location
,
25383 TYPE_DECL
, name
, au
);
25384 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25385 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25386 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25387 TYPE_NAME (au
), NULL_TREE
);
25389 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25390 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25391 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25397 make_decltype_auto (void)
25399 return make_auto_1 (decltype_auto_identifier
, true);
25405 return make_auto_1 (auto_identifier
, true);
25408 /* Return a C++17 deduction placeholder for class template TMPL. */
25411 make_template_placeholder (tree tmpl
)
25413 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25414 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25418 /* True iff T is a C++17 class template deduction placeholder. */
25421 template_placeholder_p (tree t
)
25423 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25426 /* Make a "constrained auto" type-specifier. This is an
25427 auto type with constraints that must be associated after
25428 deduction. The constraint is formed from the given
25429 CONC and its optional sequence of arguments, which are
25430 non-null if written as partial-concept-id. */
25433 make_constrained_auto (tree con
, tree args
)
25435 tree type
= make_auto_1 (auto_identifier
, false);
25437 /* Build the constraint. */
25438 tree tmpl
= DECL_TI_TEMPLATE (con
);
25439 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25440 expr
= build_concept_check (expr
, type
, args
);
25442 tree constr
= normalize_expression (expr
);
25443 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25445 /* Our canonical type depends on the constraint. */
25446 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25448 /* Attach the constraint to the type declaration. */
25449 tree decl
= TYPE_NAME (type
);
25453 /* Given type ARG, return std::initializer_list<ARG>. */
25458 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25460 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25462 gcc_rich_location
richloc (input_location
);
25463 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25464 error_at (&richloc
,
25465 "deducing from brace-enclosed initializer list"
25466 " requires %<#include <initializer_list>%>");
25468 return error_mark_node
;
25470 tree argvec
= make_tree_vec (1);
25471 TREE_VEC_ELT (argvec
, 0) = arg
;
25473 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25474 NULL_TREE
, 0, tf_warning_or_error
);
25477 /* Replace auto in TYPE with std::initializer_list<auto>. */
25480 listify_autos (tree type
, tree auto_node
)
25482 tree init_auto
= listify (auto_node
);
25483 tree argvec
= make_tree_vec (1);
25484 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25485 if (processing_template_decl
)
25486 argvec
= add_to_template_args (current_template_args (), argvec
);
25487 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25490 /* Hash traits for hashing possibly constrained 'auto'
25491 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25493 struct auto_hash
: default_hash_traits
<tree
>
25495 static inline hashval_t
hash (tree
);
25496 static inline bool equal (tree
, tree
);
25499 /* Hash the 'auto' T. */
25502 auto_hash::hash (tree t
)
25504 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25505 /* Matching constrained-type-specifiers denote the same template
25506 parameter, so hash the constraint. */
25507 return hash_placeholder_constraint (c
);
25509 /* But unconstrained autos are all separate, so just hash the pointer. */
25510 return iterative_hash_object (t
, 0);
25513 /* Compare two 'auto's. */
25516 auto_hash::equal (tree t1
, tree t2
)
25521 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25522 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25524 /* Two unconstrained autos are distinct. */
25528 return equivalent_placeholder_constraints (c1
, c2
);
25531 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25532 constrained) auto, add it to the vector. */
25535 extract_autos_r (tree t
, void *data
)
25537 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25540 /* All the autos were built with index 0; fix that up now. */
25541 tree
*p
= hash
.find_slot (t
, INSERT
);
25544 /* If this is a repeated constrained-type-specifier, use the index we
25546 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25549 /* Otherwise this is new, so use the current count. */
25551 idx
= hash
.elements () - 1;
25553 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25556 /* Always keep walking. */
25560 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25561 says they can appear anywhere in the type. */
25564 extract_autos (tree type
)
25566 hash_set
<tree
> visited
;
25567 hash_table
<auto_hash
> hash (2);
25569 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25571 tree tree_vec
= make_tree_vec (hash
.elements());
25572 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25573 iter
!= hash
.end(); ++iter
)
25576 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25577 TREE_VEC_ELT (tree_vec
, i
)
25578 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25584 /* The stem for deduction guide names. */
25585 const char *const dguide_base
= "__dguide_";
25587 /* Return the name for a deduction guide for class template TMPL. */
25590 dguide_name (tree tmpl
)
25592 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25593 tree tname
= TYPE_IDENTIFIER (type
);
25594 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25595 + IDENTIFIER_LENGTH (tname
));
25596 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25597 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25598 IDENTIFIER_LENGTH (tname
) + 1);
25599 tree dname
= get_identifier (buf
);
25600 TREE_TYPE (dname
) = type
;
25604 /* True if NAME is the name of a deduction guide. */
25607 dguide_name_p (tree name
)
25609 return (TREE_CODE (name
) == IDENTIFIER_NODE
25610 && TREE_TYPE (name
)
25611 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25612 strlen (dguide_base
)));
25615 /* True if FN is a deduction guide. */
25618 deduction_guide_p (const_tree fn
)
25621 if (tree name
= DECL_NAME (fn
))
25622 return dguide_name_p (name
);
25626 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25629 copy_guide_p (const_tree fn
)
25631 gcc_assert (deduction_guide_p (fn
));
25632 if (!DECL_ARTIFICIAL (fn
))
25634 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25635 return (TREE_CHAIN (parms
) == void_list_node
25636 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25639 /* True if FN is a guide generated from a constructor template. */
25642 template_guide_p (const_tree fn
)
25644 gcc_assert (deduction_guide_p (fn
));
25645 if (!DECL_ARTIFICIAL (fn
))
25647 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25648 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25649 return PRIMARY_TEMPLATE_P (org
);
25653 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25654 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25655 template parameter types. Note that the handling of template template
25656 parameters relies on current_template_parms being set appropriately for the
25660 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25661 tree tsubst_args
, tsubst_flags_t complain
)
25663 tree oldidx
= get_template_parm_index (olddecl
);
25666 if (TREE_CODE (olddecl
) == TYPE_DECL
25667 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25669 tree oldtype
= TREE_TYPE (olddecl
);
25670 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25671 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25672 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25673 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25674 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25677 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25678 complain
, NULL_TREE
);
25681 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25682 DECL_NAME (olddecl
), newtype
);
25683 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25686 if (TREE_CODE (olddecl
) == TYPE_DECL
25687 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25689 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25690 = build_template_parm_index (index
, level
, level
,
25692 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25693 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25694 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25695 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25697 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25699 DECL_TEMPLATE_RESULT (newdecl
)
25700 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25701 DECL_NAME (olddecl
), newtype
);
25702 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25703 // First create a copy (ttargs) of tsubst_args with an
25704 // additional level for the template template parameter's own
25705 // template parameters (ttparms).
25706 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25707 (DECL_TEMPLATE_PARMS (olddecl
)));
25708 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25709 tree ttargs
= make_tree_vec (depth
+ 1);
25710 for (int i
= 0; i
< depth
; ++i
)
25711 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25712 TREE_VEC_ELT (ttargs
, depth
)
25713 = template_parms_level_to_args (ttparms
);
25714 // Substitute ttargs into ttparms to fix references to
25715 // other template parameters.
25716 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25717 complain
|tf_partial
);
25718 // Now substitute again with args based on tparms, to reduce
25719 // the level of the ttparms.
25720 ttargs
= current_template_args ();
25721 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25723 // Finally, tack the adjusted parms onto tparms.
25724 ttparms
= tree_cons (size_int (depth
), ttparms
,
25725 current_template_parms
);
25726 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25731 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25733 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25734 TREE_CODE (oldconst
),
25735 DECL_NAME (oldconst
), newtype
);
25736 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25737 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25738 SET_DECL_TEMPLATE_PARM_P (newconst
);
25739 newidx
= build_template_parm_index (index
, level
, level
,
25740 newconst
, newtype
);
25741 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25742 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25743 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25749 /* Returns a C++17 class deduction guide template based on the constructor
25750 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25751 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25754 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25756 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25757 bool memtmpl
= false;
25760 tree fn_tmpl
= NULL_TREE
;
25765 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25768 type
= TREE_TYPE (type
);
25769 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25772 fparms
= void_list_node
;
25774 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25775 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25776 targs
= CLASSTYPE_TI_ARGS (type
);
25779 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25780 explicit_p
= false;
25784 ++processing_template_decl
;
25787 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25788 : DECL_TI_TEMPLATE (ctor
));
25790 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25791 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25793 type
= DECL_CONTEXT (ctor
);
25795 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25796 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25797 fully specialized args for the enclosing class. Strip those off, as
25798 the deduction guide won't have those template parameters. */
25799 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25800 TMPL_PARMS_DEPTH (tparms
));
25801 /* Discard the 'this' parameter. */
25802 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25803 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25804 ci
= get_constraints (ctor
);
25805 loc
= DECL_SOURCE_LOCATION (ctor
);
25806 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25808 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25812 /* For a member template constructor, we need to flatten the two
25813 template parameter lists into one, and then adjust the function
25814 signature accordingly. This gets...complicated. */
25815 tree save_parms
= current_template_parms
;
25817 /* For a member template we should have two levels of parms/args, one
25818 for the class and one for the constructor. We stripped
25819 specialized args for further enclosing classes above. */
25820 const int depth
= 2;
25821 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25823 /* Template args for translating references to the two-level template
25824 parameters into references to the one-level template parameters we
25826 tree tsubst_args
= copy_node (targs
);
25827 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25828 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25830 /* Template parms for the constructor template. */
25831 tree ftparms
= TREE_VALUE (tparms
);
25832 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25833 /* Template parms for the class template. */
25834 tparms
= TREE_CHAIN (tparms
);
25835 tree ctparms
= TREE_VALUE (tparms
);
25836 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25837 /* Template parms for the deduction guide start as a copy of the
25838 template parms for the class. We set current_template_parms for
25839 lookup_template_class_1. */
25840 current_template_parms
= tparms
= copy_node (tparms
);
25841 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25842 for (unsigned i
= 0; i
< clen
; ++i
)
25843 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25845 /* Now we need to rewrite the constructor parms to append them to the
25847 for (unsigned i
= 0; i
< flen
; ++i
)
25849 unsigned index
= i
+ clen
;
25850 unsigned level
= 1;
25851 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25852 tree olddecl
= TREE_VALUE (oldelt
);
25853 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25854 tsubst_args
, complain
);
25855 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25856 tsubst_args
, complain
, ctor
);
25857 tree list
= build_tree_list (newdef
, newdecl
);
25858 TEMPLATE_PARM_CONSTRAINTS (list
)
25859 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25860 tsubst_args
, complain
, ctor
);
25861 TREE_VEC_ELT (new_vec
, index
) = list
;
25862 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25865 /* Now we have a final set of template parms to substitute into the
25866 function signature. */
25867 targs
= template_parms_to_args (tparms
);
25868 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25870 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25872 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25874 current_template_parms
= save_parms
;
25876 --processing_template_decl
;
25881 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25882 tparms
= copy_node (tparms
);
25883 INNERMOST_TEMPLATE_PARMS (tparms
)
25884 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25887 tree fntype
= build_function_type (type
, fparms
);
25888 tree ded_fn
= build_lang_decl_loc (loc
,
25890 dguide_name (type
), fntype
);
25891 DECL_ARGUMENTS (ded_fn
) = fargs
;
25892 DECL_ARTIFICIAL (ded_fn
) = true;
25893 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25894 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25895 DECL_ARTIFICIAL (ded_tmpl
) = true;
25896 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25897 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25898 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25899 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25901 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25903 set_constraints (ded_tmpl
, ci
);
25908 /* Deduce template arguments for the class template placeholder PTYPE for
25909 template TMPL based on the initializer INIT, and return the resulting
25913 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25914 tsubst_flags_t complain
)
25916 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25918 /* We should have handled this in the caller. */
25919 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25921 if (complain
& tf_error
)
25922 error ("non-class template %qT used without template arguments", tmpl
);
25923 return error_mark_node
;
25926 tree type
= TREE_TYPE (tmpl
);
25928 bool try_list_ctor
= false;
25930 vec
<tree
,va_gc
> *args
;
25931 if (init
== NULL_TREE
25932 || TREE_CODE (init
) == TREE_LIST
)
25933 args
= make_tree_vector_from_list (init
);
25934 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25936 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25937 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25939 /* As an exception, the first phase in 16.3.1.7 (considering the
25940 initializer list as a single argument) is omitted if the
25941 initializer list consists of a single expression of type cv U,
25942 where U is a specialization of C or a class derived from a
25943 specialization of C. */
25944 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25945 tree etype
= TREE_TYPE (elt
);
25947 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25948 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25949 int err
= unify (tparms
, targs
, type
, etype
,
25950 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25952 try_list_ctor
= false;
25955 if (try_list_ctor
|| is_std_init_list (type
))
25956 args
= make_tree_vector_single (init
);
25958 args
= make_tree_vector_from_ctor (init
);
25961 args
= make_tree_vector_single (init
);
25963 tree dname
= dguide_name (tmpl
);
25964 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25965 /*type*/false, /*complain*/false,
25967 bool elided
= false;
25968 if (cands
== error_mark_node
)
25971 /* Prune explicit deduction guides in copy-initialization context. */
25972 if (flags
& LOOKUP_ONLYCONVERTING
)
25974 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25975 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25980 /* Found a nonconverting guide, prune the candidates. */
25981 tree pruned
= NULL_TREE
;
25982 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25983 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25984 pruned
= lookup_add (*iter
, pruned
);
25990 tree outer_args
= NULL_TREE
;
25991 if (DECL_CLASS_SCOPE_P (tmpl
)
25992 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25994 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25995 type
= TREE_TYPE (most_general_template (tmpl
));
25998 bool saw_ctor
= false;
25999 // FIXME cache artificial deduction guides
26000 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
26002 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
26003 if ((flags
& LOOKUP_ONLYCONVERTING
)
26004 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
26007 cands
= lookup_add (guide
, cands
);
26012 tree call
= error_mark_node
;
26014 /* If this is list-initialization and the class has a list constructor, first
26015 try deducing from the list as a single argument, as [over.match.list]. */
26016 tree list_cands
= NULL_TREE
;
26017 if (try_list_ctor
&& cands
)
26018 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26021 if (is_list_ctor (dg
))
26022 list_cands
= lookup_add (dg
, list_cands
);
26026 ++cp_unevaluated_operand
;
26027 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
26028 --cp_unevaluated_operand
;
26030 if (call
== error_mark_node
)
26032 /* That didn't work, now try treating the list as a sequence of
26034 release_tree_vector (args
);
26035 args
= make_tree_vector_from_ctor (init
);
26039 /* Maybe generate an implicit deduction guide. */
26040 if (call
== error_mark_node
&& args
->length () < 2)
26042 tree gtype
= NULL_TREE
;
26044 if (args
->length () == 1)
26045 /* Generate a copy guide. */
26046 gtype
= build_reference_type (type
);
26047 else if (!saw_ctor
)
26048 /* Generate a default guide. */
26053 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
26054 cands
= lookup_add (guide
, cands
);
26058 if (elided
&& !cands
)
26060 error ("cannot deduce template arguments for copy-initialization"
26061 " of %qT, as it has no non-explicit deduction guides or "
26062 "user-declared constructors", type
);
26063 return error_mark_node
;
26065 else if (!cands
&& call
== error_mark_node
)
26067 error ("cannot deduce template arguments of %qT, as it has no viable "
26068 "deduction guides", type
);
26069 return error_mark_node
;
26072 if (call
== error_mark_node
)
26074 ++cp_unevaluated_operand
;
26075 call
= build_new_function_call (cands
, &args
, tf_decltype
);
26076 --cp_unevaluated_operand
;
26079 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
26081 error ("class template argument deduction failed:");
26083 ++cp_unevaluated_operand
;
26084 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
26085 --cp_unevaluated_operand
;
26088 inform (input_location
, "explicit deduction guides not considered "
26089 "for copy-initialization");
26092 release_tree_vector (args
);
26094 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
26097 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26098 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26099 The CONTEXT determines the context in which auto deduction is performed
26100 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26101 OUTER_TARGS are used during template argument deduction
26102 (context == adc_unify) to properly substitute the result, and is ignored
26105 For partial-concept-ids, extra args may be appended to the list of deduced
26106 template arguments prior to determining constraint satisfaction. */
26109 do_auto_deduction (tree type
, tree init
, tree auto_node
,
26110 tsubst_flags_t complain
, auto_deduction_context context
,
26111 tree outer_targs
, int flags
)
26115 if (init
== error_mark_node
)
26116 return error_mark_node
;
26118 if (init
&& type_dependent_expression_p (init
)
26119 && context
!= adc_unify
)
26120 /* Defining a subset of type-dependent expressions that we can deduce
26121 from ahead of time isn't worth the trouble. */
26124 /* Similarly, we can't deduce from another undeduced decl. */
26125 if (init
&& undeduced_auto_decl (init
))
26128 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
26129 /* C++17 class template argument deduction. */
26130 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
26132 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
26133 /* Nothing we can do with this, even in deduction context. */
26136 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26137 with either a new invented type template parameter U or, if the
26138 initializer is a braced-init-list (8.5.4), with
26139 std::initializer_list<U>. */
26140 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26142 if (!DIRECT_LIST_INIT_P (init
))
26143 type
= listify_autos (type
, auto_node
);
26144 else if (CONSTRUCTOR_NELTS (init
) == 1)
26145 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26148 if (complain
& tf_warning_or_error
)
26150 if (permerror (input_location
, "direct-list-initialization of "
26151 "%<auto%> requires exactly one element"))
26152 inform (input_location
,
26153 "for deduction to %<std::initializer_list%>, use copy-"
26154 "list-initialization (i.e. add %<=%> before the %<{%>)");
26156 type
= listify_autos (type
, auto_node
);
26160 if (type
== error_mark_node
)
26161 return error_mark_node
;
26163 init
= resolve_nondeduced_context (init
, complain
);
26165 if (context
== adc_decomp_type
26166 && auto_node
== type
26167 && init
!= error_mark_node
26168 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26169 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26170 and initializer has array type, deduce cv-qualified array type. */
26171 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26173 else if (AUTO_IS_DECLTYPE (auto_node
))
26175 bool id
= (DECL_P (init
)
26176 || ((TREE_CODE (init
) == COMPONENT_REF
26177 || TREE_CODE (init
) == SCOPE_REF
)
26178 && !REF_PARENTHESIZED_P (init
)));
26179 targs
= make_tree_vec (1);
26180 TREE_VEC_ELT (targs
, 0)
26181 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26182 if (type
!= auto_node
)
26184 if (complain
& tf_error
)
26185 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26186 return error_mark_node
;
26191 tree parms
= build_tree_list (NULL_TREE
, type
);
26195 tparms
= extract_autos (type
);
26198 tparms
= make_tree_vec (1);
26199 TREE_VEC_ELT (tparms
, 0)
26200 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26203 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26204 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26205 DEDUCE_CALL
, LOOKUP_NORMAL
,
26206 NULL
, /*explain_p=*/false);
26209 if (processing_template_decl
)
26210 /* Try again at instantiation time. */
26212 if (type
&& type
!= error_mark_node
26213 && (complain
& tf_error
))
26214 /* If type is error_mark_node a diagnostic must have been
26215 emitted by now. Also, having a mention to '<type error>'
26216 in the diagnostic is not really useful to the user. */
26218 if (cfun
&& auto_node
== current_function_auto_return_pattern
26219 && LAMBDA_FUNCTION_P (current_function_decl
))
26220 error ("unable to deduce lambda return type from %qE", init
);
26222 error ("unable to deduce %qT from %qE", type
, init
);
26223 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26224 DEDUCE_CALL
, LOOKUP_NORMAL
,
26225 NULL
, /*explain_p=*/true);
26227 return error_mark_node
;
26231 /* Check any placeholder constraints against the deduced type. */
26232 if (flag_concepts
&& !processing_template_decl
)
26233 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26235 /* Use the deduced type to check the associated constraints. If we
26236 have a partial-concept-id, rebuild the argument list so that
26237 we check using the extra arguments. */
26238 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26239 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26240 if (TREE_VEC_LENGTH (cargs
) > 1)
26242 cargs
= copy_node (cargs
);
26243 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26247 if (!constraints_satisfied_p (constr
, cargs
))
26249 if (complain
& tf_warning_or_error
)
26253 case adc_unspecified
:
26255 error("placeholder constraints not satisfied");
26257 case adc_variable_type
:
26258 case adc_decomp_type
:
26259 error ("deduced initializer does not satisfy "
26260 "placeholder constraints");
26262 case adc_return_type
:
26263 error ("deduced return type does not satisfy "
26264 "placeholder constraints");
26266 case adc_requirement
:
26267 error ("deduced expression type does not satisfy "
26268 "placeholder constraints");
26271 diagnose_constraints (input_location
, constr
, targs
);
26273 return error_mark_node
;
26277 if (processing_template_decl
&& context
!= adc_unify
)
26278 outer_targs
= current_template_args ();
26279 targs
= add_to_template_args (outer_targs
, targs
);
26280 return tsubst (type
, targs
, complain
, NULL_TREE
);
26283 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26287 splice_late_return_type (tree type
, tree late_return_type
)
26289 if (is_auto (type
))
26291 if (late_return_type
)
26292 return late_return_type
;
26294 tree idx
= get_template_parm_index (type
);
26295 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26296 /* In an abbreviated function template we didn't know we were dealing
26297 with a function template when we saw the auto return type, so update
26298 it to have the correct level. */
26299 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26304 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26305 'decltype(auto)' or a deduced class template. */
26308 is_auto (const_tree type
)
26310 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26311 && (TYPE_IDENTIFIER (type
) == auto_identifier
26312 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26313 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26319 /* for_each_template_parm callback for type_uses_auto. */
26322 is_auto_r (tree tp
, void */
*data*/
)
26324 return is_auto (tp
);
26327 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26328 a use of `auto'. Returns NULL_TREE otherwise. */
26331 type_uses_auto (tree type
)
26333 if (type
== NULL_TREE
)
26335 else if (flag_concepts
)
26337 /* The Concepts TS allows multiple autos in one type-specifier; just
26338 return the first one we find, do_auto_deduction will collect all of
26340 if (uses_template_parms (type
))
26341 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26342 /*visited*/NULL
, /*nondeduced*/true);
26347 return find_type_usage (type
, is_auto
);
26350 /* For a given template T, return the vector of typedefs referenced
26351 in T for which access check is needed at T instantiation time.
26352 T is either a FUNCTION_DECL or a RECORD_TYPE.
26353 Those typedefs were added to T by the function
26354 append_type_to_template_for_access_check. */
26356 vec
<qualified_typedef_usage_t
, va_gc
> *
26357 get_types_needing_access_check (tree t
)
26360 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26362 if (!t
|| t
== error_mark_node
)
26365 if (!(ti
= get_template_info (t
)))
26368 if (CLASS_TYPE_P (t
)
26369 || TREE_CODE (t
) == FUNCTION_DECL
)
26371 if (!TI_TEMPLATE (ti
))
26374 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26380 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26381 tied to T. That list of typedefs will be access checked at
26382 T instantiation time.
26383 T is either a FUNCTION_DECL or a RECORD_TYPE.
26384 TYPE_DECL is a TYPE_DECL node representing a typedef.
26385 SCOPE is the scope through which TYPE_DECL is accessed.
26386 LOCATION is the location of the usage point of TYPE_DECL.
26388 This function is a subroutine of
26389 append_type_to_template_for_access_check. */
26392 append_type_to_template_for_access_check_1 (tree t
,
26395 location_t location
)
26397 qualified_typedef_usage_t typedef_usage
;
26400 if (!t
|| t
== error_mark_node
)
26403 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26404 || CLASS_TYPE_P (t
))
26406 && TREE_CODE (type_decl
) == TYPE_DECL
26409 if (!(ti
= get_template_info (t
)))
26412 gcc_assert (TI_TEMPLATE (ti
));
26414 typedef_usage
.typedef_decl
= type_decl
;
26415 typedef_usage
.context
= scope
;
26416 typedef_usage
.locus
= location
;
26418 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26421 /* Append TYPE_DECL to the template TEMPL.
26422 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26423 At TEMPL instanciation time, TYPE_DECL will be checked to see
26424 if it can be accessed through SCOPE.
26425 LOCATION is the location of the usage point of TYPE_DECL.
26427 e.g. consider the following code snippet:
26434 template<class U> struct S
26436 C::myint mi; // <-- usage point of the typedef C::myint
26441 At S<char> instantiation time, we need to check the access of C::myint
26442 In other words, we need to check the access of the myint typedef through
26443 the C scope. For that purpose, this function will add the myint typedef
26444 and the scope C through which its being accessed to a list of typedefs
26445 tied to the template S. That list will be walked at template instantiation
26446 time and access check performed on each typedefs it contains.
26447 Note that this particular code snippet should yield an error because
26448 myint is private to C. */
26451 append_type_to_template_for_access_check (tree templ
,
26454 location_t location
)
26456 qualified_typedef_usage_t
*iter
;
26459 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26461 /* Make sure we don't append the type to the template twice. */
26462 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26463 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26466 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26470 /* Convert the generic type parameters in PARM that match the types given in the
26471 range [START_IDX, END_IDX) from the current_template_parms into generic type
26475 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26477 tree current
= current_template_parms
;
26478 int depth
= TMPL_PARMS_DEPTH (current
);
26479 current
= INNERMOST_TEMPLATE_PARMS (current
);
26480 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26482 for (int i
= 0; i
< start_idx
; ++i
)
26483 TREE_VEC_ELT (replacement
, i
)
26484 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26486 for (int i
= start_idx
; i
< end_idx
; ++i
)
26488 /* Create a distinct parameter pack type from the current parm and add it
26489 to the replacement args to tsubst below into the generic function
26492 tree o
= TREE_TYPE (TREE_VALUE
26493 (TREE_VEC_ELT (current
, i
)));
26494 tree t
= copy_type (o
);
26495 TEMPLATE_TYPE_PARM_INDEX (t
)
26496 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26498 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26499 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26500 TYPE_MAIN_VARIANT (t
) = t
;
26501 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26502 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26503 TREE_VEC_ELT (replacement
, i
) = t
;
26504 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26507 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26508 TREE_VEC_ELT (replacement
, i
)
26509 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26511 /* If there are more levels then build up the replacement with the outer
26514 replacement
= add_to_template_args (template_parms_to_args
26515 (TREE_CHAIN (current_template_parms
)),
26518 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26521 /* Entries in the decl_constraint hash table. */
26522 struct GTY((for_user
)) constr_entry
26528 /* Hashing function and equality for constraint entries. */
26529 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26531 static hashval_t
hash (constr_entry
*e
)
26533 return (hashval_t
)DECL_UID (e
->decl
);
26536 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26538 return e1
->decl
== e2
->decl
;
26542 /* A mapping from declarations to constraint information. Note that
26543 both templates and their underlying declarations are mapped to the
26544 same constraint information.
26546 FIXME: This is defined in pt.c because garbage collection
26547 code is not being generated for constraint.cc. */
26549 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26551 /* Returns the template constraints of declaration T. If T is not
26552 constrained, return NULL_TREE. Note that T must be non-null. */
26555 get_constraints (tree t
)
26557 if (!flag_concepts
)
26560 gcc_assert (DECL_P (t
));
26561 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26562 t
= DECL_TEMPLATE_RESULT (t
);
26563 constr_entry elt
= { t
, NULL_TREE
};
26564 constr_entry
* found
= decl_constraints
->find (&elt
);
26571 /* Associate the given constraint information CI with the declaration
26572 T. If T is a template, then the constraints are associated with
26573 its underlying declaration. Don't build associations if CI is
26577 set_constraints (tree t
, tree ci
)
26581 gcc_assert (t
&& flag_concepts
);
26582 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26583 t
= DECL_TEMPLATE_RESULT (t
);
26584 gcc_assert (!get_constraints (t
));
26585 constr_entry elt
= {t
, ci
};
26586 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26587 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26592 /* Remove the associated constraints of the declaration T. */
26595 remove_constraints (tree t
)
26597 gcc_assert (DECL_P (t
));
26598 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26599 t
= DECL_TEMPLATE_RESULT (t
);
26601 constr_entry elt
= {t
, NULL_TREE
};
26602 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26604 decl_constraints
->clear_slot (slot
);
26607 /* Memoized satisfaction results for declarations. This
26608 maps the pair (constraint_info, arguments) to the result computed
26609 by constraints_satisfied_p. */
26611 struct GTY((for_user
)) constraint_sat_entry
26618 /* Hashing function and equality for constraint entries. */
26620 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26622 static hashval_t
hash (constraint_sat_entry
*e
)
26624 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26625 return iterative_hash_template_arg (e
->args
, val
);
26628 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26630 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26634 /* Memoized satisfaction results for concept checks. */
26636 struct GTY((for_user
)) concept_spec_entry
26643 /* Hashing function and equality for constraint entries. */
26645 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26647 static hashval_t
hash (concept_spec_entry
*e
)
26649 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26652 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26654 ++comparing_specializations
;
26655 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26656 --comparing_specializations
;
26661 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26662 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26664 /* Search for a memoized satisfaction result. Returns one of the
26665 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26668 lookup_constraint_satisfaction (tree ci
, tree args
)
26670 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26671 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26673 return found
->result
;
26678 /* Memoize the result of a satisfication test. Returns the saved result. */
26681 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26683 constraint_sat_entry elt
= {ci
, args
, result
};
26684 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26685 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26691 /* Search for a memoized satisfaction result for a concept. */
26694 lookup_concept_satisfaction (tree tmpl
, tree args
)
26696 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26697 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26699 return found
->result
;
26704 /* Memoize the result of a concept check. Returns the saved result. */
26707 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26709 concept_spec_entry elt
= {tmpl
, args
, result
};
26710 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26711 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26717 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26719 /* Returns a prior concept specialization. This returns the substituted
26720 and normalized constraints defined by the concept. */
26723 get_concept_expansion (tree tmpl
, tree args
)
26725 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26726 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26728 return found
->result
;
26733 /* Save a concept expansion for later. */
26736 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26738 concept_spec_entry elt
= {tmpl
, args
, def
};
26739 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26740 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26747 hash_subsumption_args (tree t1
, tree t2
)
26749 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26750 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26752 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26753 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26754 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26755 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26759 /* Compare the constraints of two subsumption entries. The LEFT1 and
26760 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26761 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26764 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26766 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26767 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26768 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26769 CHECK_CONSTR_ARGS (right1
)))
26770 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26771 CHECK_CONSTR_ARGS (right2
));
26775 /* Key/value pair for learning and memoizing subsumption results. This
26776 associates a pair of check constraints (including arguments) with
26777 a boolean value indicating the result. */
26779 struct GTY((for_user
)) subsumption_entry
26786 /* Hashing function and equality for constraint entries. */
26788 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26790 static hashval_t
hash (subsumption_entry
*e
)
26792 return hash_subsumption_args (e
->t1
, e
->t2
);
26795 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26797 ++comparing_specializations
;
26798 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26799 --comparing_specializations
;
26804 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26806 /* Search for a previously cached subsumption result. */
26809 lookup_subsumption_result (tree t1
, tree t2
)
26811 subsumption_entry elt
= { t1
, t2
, false };
26812 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26814 return &found
->result
;
26819 /* Save a subsumption result. */
26822 save_subsumption_result (tree t1
, tree t2
, bool result
)
26824 subsumption_entry elt
= {t1
, t2
, result
};
26825 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26826 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26832 /* Set up the hash table for constraint association. */
26835 init_constraint_processing (void)
26837 if (!flag_concepts
)
26840 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26841 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26842 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26843 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26844 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26847 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26851 declare_integer_pack (void)
26853 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26854 build_function_type_list (integer_type_node
,
26857 NULL_TREE
, ECF_CONST
);
26858 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26859 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26862 /* Set up the hash tables for template instantiations. */
26865 init_template_processing (void)
26867 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26868 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26870 if (cxx_dialect
>= cxx11
)
26871 declare_integer_pack ();
26874 /* Print stats about the template hash tables for -fstats. */
26877 print_template_statistics (void)
26879 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26880 "%f collisions\n", (long) decl_specializations
->size (),
26881 (long) decl_specializations
->elements (),
26882 decl_specializations
->collisions ());
26883 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26884 "%f collisions\n", (long) type_specializations
->size (),
26885 (long) type_specializations
->elements (),
26886 type_specializations
->collisions ());
26891 namespace selftest
{
26893 /* Verify that build_non_dependent_expr () works, for various expressions,
26894 and that location wrappers don't affect the results. */
26897 test_build_non_dependent_expr ()
26899 location_t loc
= BUILTINS_LOCATION
;
26901 /* Verify constants, without and with location wrappers. */
26902 tree int_cst
= build_int_cst (integer_type_node
, 42);
26903 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26905 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26906 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26907 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26909 tree string_lit
= build_string (4, "foo");
26910 TREE_TYPE (string_lit
) = char_array_type_node
;
26911 string_lit
= fix_string_type (string_lit
);
26912 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26914 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26915 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26916 ASSERT_EQ (wrapped_string_lit
,
26917 build_non_dependent_expr (wrapped_string_lit
));
26920 /* Verify that type_dependent_expression_p () works correctly, even
26921 in the presence of location wrapper nodes. */
26924 test_type_dependent_expression_p ()
26926 location_t loc
= BUILTINS_LOCATION
;
26928 tree name
= get_identifier ("foo");
26930 /* If no templates are involved, nothing is type-dependent. */
26931 gcc_assert (!processing_template_decl
);
26932 ASSERT_FALSE (type_dependent_expression_p (name
));
26934 ++processing_template_decl
;
26936 /* Within a template, an unresolved name is always type-dependent. */
26937 ASSERT_TRUE (type_dependent_expression_p (name
));
26939 /* Ensure it copes with NULL_TREE and errors. */
26940 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26941 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26943 /* A USING_DECL in a template should be type-dependent, even if wrapped
26944 with a location wrapper (PR c++/83799). */
26945 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26946 TREE_TYPE (using_decl
) = integer_type_node
;
26947 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26948 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26949 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26950 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26952 --processing_template_decl
;
26955 /* Run all of the selftests within this file. */
26960 test_build_non_dependent_expr ();
26961 test_type_dependent_expression_p ();
26964 } // namespace selftest
26966 #endif /* #if CHECKING_P */
26968 #include "gt-cp-pt.h"