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". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
81 local_specialization_stack::local_specialization_stack (lss_policy policy
)
82 : saved (local_specializations
)
84 if (policy
== lss_blank
|| !saved
)
85 local_specializations
= new hash_map
<tree
, tree
>;
87 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations
;
93 local_specializations
= saved
;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth
;
99 struct GTY((for_user
)) spec_entry
106 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
108 static hashval_t
hash (spec_entry
*);
109 static bool equal (spec_entry
*, spec_entry
*);
112 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
114 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result
{
133 tbr_ambiguous_baseclass
,
137 static void push_access_scope (tree
);
138 static void pop_access_scope (tree
);
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
153 static void tsubst_enum (tree
, tree
, tree
);
154 static tree
add_to_template_args (tree
, tree
);
155 static tree
add_outermost_template_args (tree
, tree
);
156 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
157 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
, int,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static void template_parm_level_and_index (tree
, int*, int*);
184 static int unify_pack_expansion (tree
, tree
, tree
,
185 tree
, unification_kind_t
, bool, bool);
186 static tree
copy_template_args (tree
);
187 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
189 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
190 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
191 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
192 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
193 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
194 static bool check_specialization_scope (void);
195 static tree
process_partial_specialization (tree
);
196 static void set_current_access_from_decl (tree
);
197 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
199 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
200 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
202 static bool template_template_parm_bindings_ok_p (tree
, tree
);
203 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
204 static tree
for_each_template_parm_r (tree
*, int *, void *);
205 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
206 static void copy_default_args_to_explicit_spec (tree
);
207 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
208 static bool dependent_template_arg_p (tree
);
209 static bool any_template_arguments_need_structural_equality_p (tree
);
210 static bool dependent_type_p_r (tree
);
211 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
212 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
213 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
214 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
216 static tree
listify (tree
);
217 static tree
listify_autos (tree
, tree
);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
220 static bool complex_alias_template_p (const_tree tmpl
);
221 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
222 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
223 static tree
make_argument_pack (tree
);
224 static void register_parameter_specializations (tree
, tree
);
225 static tree
enclosing_instantiation_of (tree tctx
);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
233 push_access_scope (tree t
)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
236 || TREE_CODE (t
) == TYPE_DECL
);
238 if (DECL_FRIEND_CONTEXT (t
))
239 push_nested_class (DECL_FRIEND_CONTEXT (t
));
240 else if (DECL_CLASS_SCOPE_P (t
))
241 push_nested_class (DECL_CONTEXT (t
));
243 push_to_top_level ();
245 if (TREE_CODE (t
) == FUNCTION_DECL
)
247 saved_access_scope
= tree_cons
248 (NULL_TREE
, current_function_decl
, saved_access_scope
);
249 current_function_decl
= t
;
253 /* Restore the scope set up by push_access_scope. T is the node we
257 pop_access_scope (tree t
)
259 if (TREE_CODE (t
) == FUNCTION_DECL
)
261 current_function_decl
= TREE_VALUE (saved_access_scope
);
262 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
265 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
277 finish_member_template_decl (tree decl
)
279 if (decl
== error_mark_node
)
280 return error_mark_node
;
282 gcc_assert (DECL_P (decl
));
284 if (TREE_CODE (decl
) == TYPE_DECL
)
288 type
= TREE_TYPE (decl
);
289 if (type
== error_mark_node
)
290 return error_mark_node
;
291 if (MAYBE_CLASS_TYPE_P (type
)
292 && CLASSTYPE_TEMPLATE_INFO (type
)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
295 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
296 check_member_template (tmpl
);
301 else if (TREE_CODE (decl
) == FIELD_DECL
)
302 error ("data member %qD cannot be a member template", decl
);
303 else if (DECL_TEMPLATE_INFO (decl
))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
307 check_member_template (DECL_TI_TEMPLATE (decl
));
308 return DECL_TI_TEMPLATE (decl
);
314 error ("invalid member template declaration %qD", decl
);
316 return error_mark_node
;
319 /* Create a template info node. */
322 build_template_info (tree template_decl
, tree template_args
)
324 tree result
= make_node (TEMPLATE_INFO
);
325 TI_TEMPLATE (result
) = template_decl
;
326 TI_ARGS (result
) = template_args
;
330 /* Return the template info node corresponding to T, whatever T is. */
333 get_template_info (const_tree t
)
335 tree tinfo
= NULL_TREE
;
337 if (!t
|| t
== error_mark_node
)
340 if (TREE_CODE (t
) == NAMESPACE_DECL
341 || TREE_CODE (t
) == PARM_DECL
)
344 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
345 tinfo
= DECL_TEMPLATE_INFO (t
);
347 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
350 if (OVERLOAD_TYPE_P (t
))
351 tinfo
= TYPE_TEMPLATE_INFO (t
);
352 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
353 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
358 /* Returns the template nesting level of the indicated class TYPE.
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
377 template_class_depth (tree type
)
381 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
383 tree tinfo
= get_template_info (type
);
385 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
390 type
= CP_DECL_CONTEXT (type
);
391 else if (LAMBDA_TYPE_P (type
))
392 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
394 type
= CP_TYPE_CONTEXT (type
);
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
404 inline_needs_template_parms (tree decl
, bool nsdmi
)
406 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
410 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
419 push_inline_template_parms_recursive (tree parmlist
, int levels
)
421 tree parms
= TREE_VALUE (parmlist
);
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
427 ++processing_template_decl
;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl
),
430 parms
, current_template_parms
);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
435 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
437 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
439 if (error_operand_p (parm
))
442 gcc_assert (DECL_P (parm
));
444 switch (TREE_CODE (parm
))
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
467 maybe_begin_member_template_processing (tree decl
)
471 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
475 tree ctx
= DECL_CONTEXT (decl
);
476 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx
)
479 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
482 if (inline_needs_template_parms (decl
, nsdmi
))
484 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
485 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
490 parms
= TREE_CHAIN (parms
);
493 push_inline_template_parms_recursive (parms
, levels
);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels
.safe_push (levels
);
501 /* Undo the effects of maybe_begin_member_template_processing. */
504 maybe_end_member_template_processing (void)
509 if (inline_parm_levels
.length () == 0)
512 last
= inline_parm_levels
.pop ();
513 for (i
= 0; i
< last
; ++i
)
515 --processing_template_decl
;
516 current_template_parms
= TREE_CHAIN (current_template_parms
);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
525 add_to_template_args (tree args
, tree extra_args
)
532 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
535 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
536 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
538 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
539 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
541 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
555 add_outermost_template_args (tree args
, tree extra_args
)
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
565 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
571 new_args
= add_to_template_args (args
, extra_args
);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
579 /* Return the N levels of innermost template arguments from the ARGS. */
582 get_innermost_template_args (tree args
, int n
)
590 /* If N is 1, just return the innermost set of template arguments. */
592 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
594 /* If we're not removing anything, just return the arguments we were
596 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
597 gcc_assert (extra_levels
>= 0);
598 if (extra_levels
== 0)
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args
= make_tree_vec (n
);
603 for (i
= 1; i
<= n
; ++i
)
604 SET_TMPL_ARGS_LEVEL (new_args
, i
,
605 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
614 strip_innermost_template_args (tree args
, int extra_levels
)
617 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
622 /* If N is 1, just return the outermost set of template arguments. */
624 return TMPL_ARGS_LEVEL (args
, 1);
626 /* If we're not removing anything, just return the arguments we were
628 gcc_assert (extra_levels
>= 0);
629 if (extra_levels
== 0)
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args
= make_tree_vec (n
);
634 for (i
= 1; i
<= n
; ++i
)
635 SET_TMPL_ARGS_LEVEL (new_args
, i
,
636 TMPL_ARGS_LEVEL (args
, i
));
641 /* We've got a template header coming up; push to a new level for storing
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms
, NULL
);
662 ++processing_template_decl
;
663 ++processing_template_parmlist
;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl
),
670 current_template_parms
);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
678 check_specialization_scope (void)
680 tree scope
= current_scope ();
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
691 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
693 error ("explicit specialization in non-namespace scope %qD", scope
);
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms
)
707 error ("enclosing class templates are not explicitly specialized");
714 /* We've just seen template <>. */
717 begin_specialization (void)
719 begin_scope (sk_template_spec
, NULL
);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
728 end_specialization (void)
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
738 reset_specialization (void)
740 processing_specialization
= 0;
741 template_header_count
= 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
748 note_template_header (int specialization
)
750 processing_specialization
= specialization
;
751 template_header_count
++;
754 /* We're beginning an explicit instantiation. */
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation
);
760 processing_explicit_instantiation
= true;
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation
);
768 processing_explicit_instantiation
= false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
777 check_specialization_namespace (tree tmpl
)
779 tree tpl_ns
= decl_namespace_context (tmpl
);
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl
)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl
);
795 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
796 /* Same or enclosing namespace. */
800 permerror (input_location
,
801 "specialization of %qD in different namespace", tmpl
);
802 inform (DECL_SOURCE_LOCATION (tmpl
),
803 " from definition of %q#D", tmpl
);
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
812 check_explicit_instantiation_namespace (tree spec
)
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns
= decl_namespace_context (spec
);
819 if (!is_nested_namespace (current_namespace
, ns
))
820 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec
, current_namespace
, ns
);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
829 maybe_new_partial_specialization (tree type
)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
837 // template<typename T>
840 // Here, S<T*> is an implicit instantiation of S whose type
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
863 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
864 tree args
= CLASSTYPE_TI_ARGS (type
);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms
)
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr
= current_template_constraints ();
879 if (type
== TREE_TYPE (tmpl
))
881 tree main_constr
= get_constraints (tmpl
);
882 if (equivalent_constraints (type_constr
, main_constr
))
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
891 tree spec_tmpl
= TREE_VALUE (specs
);
892 tree spec_args
= TREE_PURPOSE (specs
);
893 tree spec_constr
= get_constraints (spec_tmpl
);
894 if (comp_template_args (args
, spec_args
)
895 && equivalent_constraints (type_constr
, spec_constr
))
897 specs
= TREE_CHAIN (specs
);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t
= make_class_type (TREE_CODE (type
));
903 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
904 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
911 // Build the corresponding type decl.
912 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
913 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
914 DECL_SOURCE_LOCATION (d
) = input_location
;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
926 maybe_process_partial_specialization (tree type
)
930 if (type
== error_mark_node
)
931 return error_mark_node
;
933 /* A lambda that appears in specialization context is not itself a
935 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
938 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
940 error ("name of class shadows template template parameter %qD",
942 return error_mark_node
;
945 context
= TYPE_CONTEXT (type
);
947 if (TYPE_ALIAS_P (type
))
949 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
951 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo
));
955 error ("explicit specialization of non-template %qT", type
);
956 return error_mark_node
;
958 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t
= maybe_new_partial_specialization (type
))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
974 && !at_namespace_scope_p ())
975 return error_mark_node
;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
978 if (processing_template_decl
)
980 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
981 if (decl
== error_mark_node
)
982 return error_mark_node
;
983 return TREE_TYPE (decl
);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
987 error ("specialization of %qT after instantiation", type
);
988 else if (errorcount
&& !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node
;
996 else if (CLASS_TYPE_P (type
)
997 && !CLASSTYPE_USE_TEMPLATE (type
)
998 && CLASSTYPE_TEMPLATE_INFO (type
)
999 && context
&& CLASS_TYPE_P (context
)
1000 && CLASSTYPE_TEMPLATE_INFO (context
))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1019 && !COMPLETE_TYPE_P (type
))
1022 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl
))
1027 permerror (input_location
,
1028 "specializing %q#T in different namespace", type
);
1029 permerror (DECL_SOURCE_LOCATION (tmpl
),
1030 " from definition of %q#D", tmpl
);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1039 t
; t
= TREE_CHAIN (t
))
1041 tree inst
= TREE_VALUE (t
);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1052 elt
.tmpl
= most_general_template (tmpl
);
1053 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1056 type_specializations
->remove_elt (&elt
);
1059 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1062 = type_specializations
->find_slot (&elt
, INSERT
);
1063 entry
= ggc_alloc
<spec_entry
> ();
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1079 CLASSTYPE_TI_ARGS (type
)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1083 else if (processing_specialization
)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1088 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1089 "of %qD not allowed by ISO C++", type
);
1092 error ("explicit specialization of non-template %qT", type
);
1093 return error_mark_node
;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1105 optimize_specialization_lookup_p (tree tmpl
)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1108 && DECL_CLASS_SCOPE_P (tmpl
)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1119 && !DECL_CONV_FN_P (tmpl
)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1137 verify_unstripped_args (tree args
)
1139 ++processing_template_decl
;
1140 if (!any_dependent_template_arguments_p (args
))
1142 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1143 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1145 tree arg
= TREE_VEC_ELT (inner
, i
);
1146 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1148 else if (TYPE_P (arg
))
1149 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1150 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1154 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1157 --processing_template_decl
;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1175 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1177 if (tmpl
== NULL_TREE
)
1180 if (args
== error_mark_node
)
1183 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl
) == FIELD_DECL
);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args
)
1189 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1191 : template_class_depth (DECL_CONTEXT (tmpl
))));
1194 verify_unstripped_args (args
);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl
))
1201 if (optimize_specialization_lookup_p (tmpl
))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1206 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1207 tree class_specialization
1208 = retrieve_specialization (class_template
, args
, 0);
1209 if (!class_specialization
)
1212 /* Find the instance of TMPL. */
1213 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1214 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1217 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn
) == class_specialization
)
1229 hash_table
<spec_hasher
> *specializations
;
1233 elt
.spec
= NULL_TREE
;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1236 specializations
= type_specializations
;
1238 specializations
= decl_specializations
;
1241 hash
= spec_hasher::hash (&elt
);
1242 found
= specializations
->find_with_hash (&elt
, hash
);
1250 /* Like retrieve_specialization, but for local declarations. */
1253 retrieve_local_specialization (tree tmpl
)
1255 if (local_specializations
== NULL
)
1258 tree
*slot
= local_specializations
->get (tmpl
);
1259 return slot
? *slot
: NULL_TREE
;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl
, tree tmpl
)
1269 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1273 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1279 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1281 for (t
= TREE_TYPE (decl
);
1283 t
= CLASSTYPE_USE_TEMPLATE (t
)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1285 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1296 is_specialization_of_friend (tree decl
, tree friend_decl
)
1298 bool need_template
= true;
1301 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1302 || TREE_CODE (decl
) == TYPE_DECL
);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1307 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl
)
1309 && !DECL_USE_TEMPLATE (friend_decl
))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1313 need_template
= false;
1315 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl
))
1317 need_template
= false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1323 if (is_specialization_of (decl
, friend_decl
))
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1348 && DECL_CLASS_SCOPE_P (decl
)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context
= DECL_CONTEXT (decl
);
1369 tree args
= NULL_TREE
;
1370 int current_depth
= 0;
1372 while (current_depth
< template_depth
)
1374 if (CLASSTYPE_TEMPLATE_INFO (context
))
1376 if (current_depth
== 0)
1377 args
= TYPE_TI_ARGS (context
);
1379 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1382 context
= TYPE_CONTEXT (context
);
1385 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1390 tree friend_args_type
;
1391 tree decl_args_type
;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1395 is_template
= DECL_TEMPLATE_INFO (decl
)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1397 if (need_template
^ is_template
)
1399 else if (is_template
)
1401 /* If both are templates, check template parameter list. */
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1410 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1413 decl_type
= TREE_TYPE (decl
);
1415 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1416 tf_none
, NULL_TREE
);
1417 if (friend_type
== error_mark_node
)
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1427 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1429 friend_args_type
= TREE_CHAIN (friend_args_type
);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1431 decl_args_type
= TREE_CHAIN (decl_args_type
);
1433 return compparms (decl_args_type
, friend_args_type
);
1437 /* DECL is a TYPE_DECL */
1439 tree decl_type
= TREE_TYPE (decl
);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1447 if (need_template
^ is_template
)
1449 else if (is_template
)
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1455 != DECL_NAME (friend_decl
))
1458 /* Now check template parameter list. */
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1467 return (DECL_NAME (decl
)
1468 == DECL_NAME (friend_decl
));
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1483 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1487 spec_entry
**slot
= NULL
;
1490 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1491 || (TREE_CODE (tmpl
) == FIELD_DECL
1492 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1494 if (TREE_CODE (spec
) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec
)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1509 if (optimize_specialization_lookup_p (tmpl
))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn
= retrieve_specialization (tmpl
, args
, 0);
1520 hash
= spec_hasher::hash (&elt
);
1523 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1525 fn
= ((spec_entry
*) *slot
)->spec
;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1537 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1541 if (DECL_ODR_USED (fn
)
1542 || DECL_EXPLICIT_INSTANTIATION (fn
))
1544 error ("specialization of %qD after instantiation",
1546 return error_mark_node
;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn
) = NULL_TREE
;
1575 duplicate_decls (spec
, fn
, is_friend
);
1576 /* The call to duplicate_decls will have applied
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone
, fn
)
1588 DECL_DECLARED_INLINE_P (clone
)
1589 = DECL_DECLARED_INLINE_P (fn
);
1590 DECL_SOURCE_LOCATION (clone
)
1591 = DECL_SOURCE_LOCATION (fn
);
1592 DECL_DELETED_FN (clone
)
1593 = DECL_DELETED_FN (fn
);
1595 check_specialization_namespace (tmpl
);
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1602 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1603 if (dd
== error_mark_node
)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node
;
1607 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1611 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1617 return duplicate_decls (spec
, fn
, is_friend
);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1622 && !check_specialization_namespace (tmpl
))
1623 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1625 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1627 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1628 gcc_assert (tmpl
&& args
&& spec
);
1631 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1632 && PRIMARY_TEMPLATE_P (tmpl
)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1634 || variable_template_p (tmpl
))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1643 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1651 int comparing_specializations
;
1654 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1658 ++comparing_specializations
;
1659 equal
= (e1
->tmpl
== e2
->tmpl
1660 && comp_template_args (e1
->args
, e2
->args
));
1661 if (equal
&& flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1665 && uses_template_parms (e1
->args
))
1667 /* Partial specializations of a variable template can be distinguished by
1669 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1670 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1671 equal
= equivalent_constraints (c1
, c2
);
1673 --comparing_specializations
;
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1681 hash_tmpl_and_args (tree tmpl
, tree args
)
1683 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1684 return iterative_hash_template_arg (args
, val
);
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1691 spec_hasher::hash (spec_entry
*e
)
1693 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1700 iterative_hash_template_arg (tree arg
, hashval_t val
)
1702 unsigned HOST_WIDE_INT i
;
1703 enum tree_code code
;
1706 if (arg
== NULL_TREE
)
1707 return iterative_hash_object (arg
, val
);
1712 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1715 code
= TREE_CODE (arg
);
1716 tclass
= TREE_CODE_CLASS (code
);
1718 val
= iterative_hash_object (code
, val
);
1725 case IDENTIFIER_NODE
:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1730 int i
, len
= TREE_VEC_LENGTH (arg
);
1731 for (i
= 0; i
< len
; ++i
)
1732 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1736 case TYPE_PACK_EXPANSION
:
1737 case EXPR_PACK_EXPANSION
:
1738 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1741 case TYPE_ARGUMENT_PACK
:
1742 case NONTYPE_ARGUMENT_PACK
:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1746 for (; arg
; arg
= TREE_CHAIN (arg
))
1747 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1751 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1752 val
= iterative_hash_template_arg (*iter
, val
);
1758 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1761 val
= iterative_hash_template_arg (field
, val
);
1762 val
= iterative_hash_template_arg (value
, val
);
1768 if (!DECL_ARTIFICIAL (arg
))
1770 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1771 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1773 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1779 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1782 case TEMPLATE_PARM_INDEX
:
1783 val
= iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1785 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1789 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1790 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1794 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1800 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1801 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1802 val
= iterative_hash_object (code
, val
);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1806 /* A lambda can't appear in a template arg, but don't crash on
1808 gcc_assert (seen_error ());
1812 case IMPLICIT_CONV_EXPR
:
1813 case STATIC_CAST_EXPR
:
1814 case REINTERPRET_CAST_EXPR
:
1815 case CONST_CAST_EXPR
:
1816 case DYNAMIC_CAST_EXPR
:
1818 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1819 /* Now hash operands as usual. */
1829 if (alias_template_specialization_p (arg
))
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1841 if (TYPE_CANONICAL (arg
))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1844 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1846 /* Otherwise just compare the types during lookup. */
1849 case tcc_declaration
:
1851 return iterative_hash_expr (arg
, val
);
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1856 unsigned n
= cp_tree_operand_length (arg
);
1857 for (i
= 0; i
< n
; ++i
)
1858 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1873 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1878 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1879 elt
.args
= TI_ARGS (tinfo
);
1880 elt
.spec
= NULL_TREE
;
1882 entry
= decl_specializations
->find (&elt
);
1885 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1886 gcc_assert (new_spec
!= NULL_TREE
);
1887 entry
->spec
= new_spec
;
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1898 register_local_specialization (tree spec
, tree tmpl
)
1900 gcc_assert (tmpl
!= spec
);
1901 local_specializations
->put (tmpl
, spec
);
1904 /* TYPE is a class type. Returns true if TYPE is an explicitly
1905 specialized class. */
1908 explicit_class_specialization_p (tree type
)
1910 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1912 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1915 /* Print the list of functions at FNS, going through all the overloads
1916 for each element of the list. Alternatively, FNS can not be a
1917 TREE_LIST, in which case it will be printed together with all the
1920 MORE and *STR should respectively be FALSE and NULL when the function
1921 is called from the outside. They are used internally on recursive
1922 calls. print_candidates manages the two parameters and leaves NULL
1923 in *STR when it ends. */
1926 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1928 if (TREE_CODE (fns
) == TREE_LIST
)
1929 for (; fns
; fns
= TREE_CHAIN (fns
))
1930 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1932 for (lkp_iterator
iter (fns
); iter
;)
1937 const char *pfx
= *str
;
1941 pfx
= _("candidates are:");
1943 pfx
= _("candidate is:");
1944 *str
= get_spaces (pfx
);
1946 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1950 /* Print the list of candidate FNS in an error message. FNS can also
1951 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1954 print_candidates (tree fns
)
1957 print_candidates_1 (fns
, &str
);
1961 /* Get a (possibly) constrained template declaration for the
1962 purpose of ordering candidates. */
1964 get_template_for_ordering (tree list
)
1966 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1967 tree f
= TREE_VALUE (list
);
1968 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1969 return TI_TEMPLATE (ti
);
1973 /* Among candidates having the same signature, return the
1974 most constrained or NULL_TREE if there is no best candidate.
1975 If the signatures of candidates vary (e.g., template
1976 specialization vs. member function), then there can be no
1979 Note that we don't compare constraints on the functions
1980 themselves, but rather those of their templates. */
1982 most_constrained_function (tree candidates
)
1984 // Try to find the best candidate in a first pass.
1985 tree champ
= candidates
;
1986 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1988 int winner
= more_constrained (get_template_for_ordering (champ
),
1989 get_template_for_ordering (c
));
1991 champ
= c
; // The candidate is more constrained
1992 else if (winner
== 0)
1993 return NULL_TREE
; // Neither is more constrained
1996 // Verify that the champ is better than previous candidates.
1997 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
1998 if (!more_constrained (get_template_for_ordering (champ
),
1999 get_template_for_ordering (c
)))
2007 /* Returns the template (one of the functions given by TEMPLATE_ID)
2008 which can be specialized to match the indicated DECL with the
2009 explicit template args given in TEMPLATE_ID. The DECL may be
2010 NULL_TREE if none is available. In that case, the functions in
2011 TEMPLATE_ID are non-members.
2013 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2014 specialization of a member template.
2016 The TEMPLATE_COUNT is the number of references to qualifying
2017 template classes that appeared in the name of the function. See
2018 check_explicit_specialization for a more accurate description.
2020 TSK indicates what kind of template declaration (if any) is being
2021 declared. TSK_TEMPLATE indicates that the declaration given by
2022 DECL, though a FUNCTION_DECL, has template parameters, and is
2023 therefore a template function.
2025 The template args (those explicitly specified and those deduced)
2026 are output in a newly created vector *TARGS_OUT.
2028 If it is impossible to determine the result, an error message is
2029 issued. The error_mark_node is returned to indicate failure. */
2032 determine_specialization (tree template_id
,
2035 int need_member_template
,
2041 tree explicit_targs
;
2042 tree candidates
= NULL_TREE
;
2044 /* A TREE_LIST of templates of which DECL may be a specialization.
2045 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2046 corresponding TREE_PURPOSE is the set of template arguments that,
2047 when used to instantiate the template, would produce a function
2048 with the signature of DECL. */
2049 tree templates
= NULL_TREE
;
2051 cp_binding_level
*b
;
2053 *targs_out
= NULL_TREE
;
2055 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2056 return error_mark_node
;
2058 /* We shouldn't be specializing a member template of an
2059 unspecialized class template; we already gave an error in
2060 check_specialization_scope, now avoid crashing. */
2061 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2062 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2064 gcc_assert (errorcount
);
2065 return error_mark_node
;
2068 fns
= TREE_OPERAND (template_id
, 0);
2069 explicit_targs
= TREE_OPERAND (template_id
, 1);
2071 if (fns
== error_mark_node
)
2072 return error_mark_node
;
2074 /* Check for baselinks. */
2075 if (BASELINK_P (fns
))
2076 fns
= BASELINK_FUNCTIONS (fns
);
2078 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2080 error ("%qD is not a function template", fns
);
2081 return error_mark_node
;
2083 else if (VAR_P (decl
) && !variable_template_p (fns
))
2085 error ("%qD is not a variable template", fns
);
2086 return error_mark_node
;
2089 /* Count the number of template headers specified for this
2092 for (b
= current_binding_level
;
2093 b
->kind
== sk_template_parms
;
2097 tree orig_fns
= fns
;
2099 if (variable_template_p (fns
))
2101 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2102 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2103 tf_warning_or_error
,
2104 /*req_all*/true, /*use_defarg*/true);
2105 if (targs
!= error_mark_node
)
2106 templates
= tree_cons (targs
, fns
, templates
);
2108 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2112 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2114 tree decl_arg_types
;
2118 /* In case of explicit specialization, we need to check if
2119 the number of template headers appearing in the specialization
2120 is correct. This is usually done in check_explicit_specialization,
2121 but the check done there cannot be exhaustive when specializing
2122 member functions. Consider the following code:
2124 template <> void A<int>::f(int);
2125 template <> template <> void A<int>::f(int);
2127 Assuming that A<int> is not itself an explicit specialization
2128 already, the first line specializes "f" which is a non-template
2129 member function, whilst the second line specializes "f" which
2130 is a template member function. So both lines are syntactically
2131 correct, and check_explicit_specialization does not reject
2134 Here, we can do better, as we are matching the specialization
2135 against the declarations. We count the number of template
2136 headers, and we check if they match TEMPLATE_COUNT + 1
2137 (TEMPLATE_COUNT is the number of qualifying template classes,
2138 plus there must be another header for the member template
2141 Notice that if header_count is zero, this is not a
2142 specialization but rather a template instantiation, so there
2143 is no check we can perform here. */
2144 if (header_count
&& header_count
!= template_count
+ 1)
2147 /* Check that the number of template arguments at the
2148 innermost level for DECL is the same as for FN. */
2149 if (current_binding_level
->kind
== sk_template_parms
2150 && !current_binding_level
->explicit_spec_p
2151 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2152 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2153 (current_template_parms
))))
2156 /* DECL might be a specialization of FN. */
2157 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2158 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2160 /* For a non-static member function, we need to make sure
2161 that the const qualification is the same. Since
2162 get_bindings does not try to merge the "this" parameter,
2163 we must do the comparison explicitly. */
2164 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2165 && !same_type_p (TREE_VALUE (fn_arg_types
),
2166 TREE_VALUE (decl_arg_types
)))
2169 /* Skip the "this" parameter and, for constructors of
2170 classes with virtual bases, the VTT parameter. A
2171 full specialization of a constructor will have a VTT
2172 parameter, but a template never will. */
2174 = skip_artificial_parms_for (decl
, decl_arg_types
);
2176 = skip_artificial_parms_for (fn
, fn_arg_types
);
2178 /* Function templates cannot be specializations; there are
2179 no partial specializations of functions. Therefore, if
2180 the type of DECL does not match FN, there is no
2183 Note that it should never be the case that we have both
2184 candidates added here, and for regular member functions
2186 if (tsk
== tsk_template
)
2188 if (compparms (fn_arg_types
, decl_arg_types
))
2189 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2193 /* See whether this function might be a specialization of this
2194 template. Suppress access control because we might be trying
2195 to make this specialization a friend, and we have already done
2196 access control for the declaration of the specialization. */
2197 push_deferring_access_checks (dk_no_check
);
2198 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2199 pop_deferring_access_checks ();
2202 /* We cannot deduce template arguments that when used to
2203 specialize TMPL will produce DECL. */
2206 if (uses_template_parms (targs
))
2207 /* We deduced something involving 'auto', which isn't a valid
2208 template argument. */
2211 /* Remove, from the set of candidates, all those functions
2212 whose constraints are not satisfied. */
2213 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2216 // Then, try to form the new function type.
2217 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2218 if (insttype
== error_mark_node
)
2221 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2222 if (!compparms (fn_arg_types
, decl_arg_types
))
2225 /* Save this template, and the arguments deduced. */
2226 templates
= tree_cons (targs
, fn
, templates
);
2228 else if (need_member_template
)
2229 /* FN is an ordinary member function, and we need a
2230 specialization of a member template. */
2232 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2233 /* We can get IDENTIFIER_NODEs here in certain erroneous
2236 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2237 /* This is just an ordinary non-member function. Nothing can
2238 be a specialization of that. */
2240 else if (DECL_ARTIFICIAL (fn
))
2241 /* Cannot specialize functions that are created implicitly. */
2245 tree decl_arg_types
;
2247 /* This is an ordinary member function. However, since
2248 we're here, we can assume its enclosing class is a
2249 template class. For example,
2251 template <typename T> struct S { void f(); };
2252 template <> void S<int>::f() {}
2254 Here, S<int>::f is a non-template, but S<int> is a
2255 template class. If FN has the same type as DECL, we
2256 might be in business. */
2258 if (!DECL_TEMPLATE_INFO (fn
))
2259 /* Its enclosing class is an explicit specialization
2260 of a template class. This is not a candidate. */
2263 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2264 TREE_TYPE (TREE_TYPE (fn
))))
2265 /* The return types differ. */
2268 /* Adjust the type of DECL in case FN is a static member. */
2269 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2270 if (DECL_STATIC_FUNCTION_P (fn
)
2271 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2272 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2274 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2278 // If the deduced arguments do not satisfy the constraints,
2279 // this is not a candidate.
2280 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2283 // Add the candidate.
2284 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2288 if (templates
&& TREE_CHAIN (templates
))
2294 It is possible for a specialization with a given function
2295 signature to be instantiated from more than one function
2296 template. In such cases, explicit specification of the
2297 template arguments must be used to uniquely identify the
2298 function template specialization being specialized.
2300 Note that here, there's no suggestion that we're supposed to
2301 determine which of the candidate templates is most
2302 specialized. However, we, also have:
2306 Partial ordering of overloaded function template
2307 declarations is used in the following contexts to select
2308 the function template to which a function template
2309 specialization refers:
2311 -- when an explicit specialization refers to a function
2314 So, we do use the partial ordering rules, at least for now.
2315 This extension can only serve to make invalid programs valid,
2316 so it's safe. And, there is strong anecdotal evidence that
2317 the committee intended the partial ordering rules to apply;
2318 the EDG front end has that behavior, and John Spicer claims
2319 that the committee simply forgot to delete the wording in
2320 [temp.expl.spec]. */
2321 tree tmpl
= most_specialized_instantiation (templates
);
2322 if (tmpl
!= error_mark_node
)
2325 TREE_CHAIN (templates
) = NULL_TREE
;
2329 // Concepts allows multiple declarations of member functions
2330 // with the same signature. Like above, we need to rely on
2331 // on the partial ordering of those candidates to determine which
2333 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2335 if (tree cand
= most_constrained_function (candidates
))
2338 TREE_CHAIN (cand
) = NULL_TREE
;
2342 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2344 error ("template-id %qD for %q+D does not match any template "
2345 "declaration", template_id
, decl
);
2346 if (header_count
&& header_count
!= template_count
+ 1)
2347 inform (input_location
, "saw %d %<template<>%>, need %d for "
2348 "specializing a member function template",
2349 header_count
, template_count
+ 1);
2351 print_candidates (orig_fns
);
2352 return error_mark_node
;
2354 else if ((templates
&& TREE_CHAIN (templates
))
2355 || (candidates
&& TREE_CHAIN (candidates
))
2356 || (templates
&& candidates
))
2358 error ("ambiguous template specialization %qD for %q+D",
2360 candidates
= chainon (candidates
, templates
);
2361 print_candidates (candidates
);
2362 return error_mark_node
;
2365 /* We have one, and exactly one, match. */
2368 tree fn
= TREE_VALUE (candidates
);
2369 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2371 // Propagate the candidate's constraints to the declaration.
2372 set_constraints (decl
, get_constraints (fn
));
2374 /* DECL is a re-declaration or partial instantiation of a template
2376 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2378 /* It was a specialization of an ordinary member function in a
2380 return DECL_TI_TEMPLATE (fn
);
2383 /* It was a specialization of a template. */
2384 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2385 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2387 *targs_out
= copy_node (targs
);
2388 SET_TMPL_ARGS_LEVEL (*targs_out
,
2389 TMPL_ARGS_DEPTH (*targs_out
),
2390 TREE_PURPOSE (templates
));
2393 *targs_out
= TREE_PURPOSE (templates
);
2394 return TREE_VALUE (templates
);
2397 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2398 but with the default argument values filled in from those in the
2402 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2405 tree new_spec_types
;
2410 if (spec_types
== void_list_node
)
2411 return void_list_node
;
2413 /* Substitute into the rest of the list. */
2415 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2416 TREE_CHAIN (tmpl_types
));
2418 /* Add the default argument for this parameter. */
2419 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2420 TREE_VALUE (spec_types
),
2424 /* DECL is an explicit specialization. Replicate default arguments
2425 from the template it specializes. (That way, code like:
2427 template <class T> void f(T = 3);
2428 template <> void f(double);
2431 works, as required.) An alternative approach would be to look up
2432 the correct default arguments at the call-site, but this approach
2433 is consistent with how implicit instantiations are handled. */
2436 copy_default_args_to_explicit_spec (tree decl
)
2441 tree new_spec_types
;
2445 tree object_type
= NULL_TREE
;
2446 tree in_charge
= NULL_TREE
;
2447 tree vtt
= NULL_TREE
;
2449 /* See if there's anything we need to do. */
2450 tmpl
= DECL_TI_TEMPLATE (decl
);
2451 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2452 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2453 if (TREE_PURPOSE (t
))
2458 old_type
= TREE_TYPE (decl
);
2459 spec_types
= TYPE_ARG_TYPES (old_type
);
2461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2463 /* Remove the this pointer, but remember the object's type for
2465 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2466 spec_types
= TREE_CHAIN (spec_types
);
2467 tmpl_types
= TREE_CHAIN (tmpl_types
);
2469 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2471 /* DECL may contain more parameters than TMPL due to the extra
2472 in-charge parameter in constructors and destructors. */
2473 in_charge
= spec_types
;
2474 spec_types
= TREE_CHAIN (spec_types
);
2476 if (DECL_HAS_VTT_PARM_P (decl
))
2479 spec_types
= TREE_CHAIN (spec_types
);
2483 /* Compute the merged default arguments. */
2485 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2487 /* Compute the new FUNCTION_TYPE. */
2491 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2496 /* Put the in-charge parameter back. */
2497 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2498 TREE_VALUE (in_charge
),
2501 new_type
= build_method_type_directly (object_type
,
2502 TREE_TYPE (old_type
),
2506 new_type
= build_function_type (TREE_TYPE (old_type
),
2508 new_type
= cp_build_type_attribute_variant (new_type
,
2509 TYPE_ATTRIBUTES (old_type
));
2510 new_type
= build_exception_variant (new_type
,
2511 TYPE_RAISES_EXCEPTIONS (old_type
));
2513 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2514 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2516 TREE_TYPE (decl
) = new_type
;
2519 /* Return the number of template headers we expect to see for a definition
2520 or specialization of CTYPE or one of its non-template members. */
2523 num_template_headers_for_class (tree ctype
)
2525 int num_templates
= 0;
2527 while (ctype
&& CLASS_TYPE_P (ctype
))
2529 /* You're supposed to have one `template <...>' for every
2530 template class, but you don't need one for a full
2531 specialization. For example:
2533 template <class T> struct S{};
2534 template <> struct S<int> { void f(); };
2535 void S<int>::f () {}
2537 is correct; there shouldn't be a `template <>' for the
2538 definition of `S<int>::f'. */
2539 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2540 /* If CTYPE does not have template information of any
2541 kind, then it is not a template, nor is it nested
2542 within a template. */
2544 if (explicit_class_specialization_p (ctype
))
2546 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2549 ctype
= TYPE_CONTEXT (ctype
);
2552 return num_templates
;
2555 /* Do a simple sanity check on the template headers that precede the
2556 variable declaration DECL. */
2559 check_template_variable (tree decl
)
2561 tree ctx
= CP_DECL_CONTEXT (decl
);
2562 int wanted
= num_template_headers_for_class (ctx
);
2563 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2566 if (cxx_dialect
< cxx14
)
2567 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2568 "variable templates only available with "
2569 "-std=c++14 or -std=gnu++14");
2571 // Namespace-scope variable templates should have a template header.
2574 if (template_header_count
> wanted
)
2576 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2577 "too many template headers for %qD "
2580 if (warned
&& CLASS_TYPE_P (ctx
)
2581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2582 inform (DECL_SOURCE_LOCATION (decl
),
2583 "members of an explicitly specialized class are defined "
2584 "without a template header");
2588 /* An explicit specialization whose declarator-id or class-head-name is not
2589 qualified shall be declared in the nearest enclosing namespace of the
2590 template, or, if the namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set.
2593 If the name declared in the explicit instantiation is an unqualified name,
2594 the explicit instantiation shall appear in the namespace where its template
2595 is declared or, if that namespace is inline (7.3.1), any namespace from its
2596 enclosing namespace set. */
2599 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2601 tree tmpl
= most_general_template (t
);
2602 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2603 && !is_nested_namespace (current_namespace
,
2604 CP_DECL_CONTEXT (tmpl
), true))
2606 if (processing_specialization
)
2607 permerror (loc
, "explicit specialization of %qD outside its "
2608 "namespace must use a nested-name-specifier", tmpl
);
2609 else if (processing_explicit_instantiation
2610 && cxx_dialect
>= cxx11
)
2611 /* This was allowed in C++98, so only pedwarn. */
2612 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2613 "outside its namespace must use a nested-name-"
2618 /* Check to see if the function just declared, as indicated in
2619 DECLARATOR, and in DECL, is a specialization of a function
2620 template. We may also discover that the declaration is an explicit
2621 instantiation at this point.
2623 Returns DECL, or an equivalent declaration that should be used
2624 instead if all goes well. Issues an error message if something is
2625 amiss. Returns error_mark_node if the error is not easily
2628 FLAGS is a bitmask consisting of the following flags:
2630 2: The function has a definition.
2631 4: The function is a friend.
2633 The TEMPLATE_COUNT is the number of references to qualifying
2634 template classes that appeared in the name of the function. For
2637 template <class T> struct S { void f(); };
2640 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2641 classes are not counted in the TEMPLATE_COUNT, so that in
2643 template <class T> struct S {};
2644 template <> struct S<int> { void f(); }
2645 template <> void S<int>::f();
2647 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2648 invalid; there should be no template <>.)
2650 If the function is a specialization, it is marked as such via
2651 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2652 is set up correctly, and it is added to the list of specializations
2653 for that template. */
2656 check_explicit_specialization (tree declarator
,
2661 int have_def
= flags
& 2;
2662 int is_friend
= flags
& 4;
2663 bool is_concept
= flags
& 8;
2664 int specialization
= 0;
2665 int explicit_instantiation
= 0;
2666 int member_specialization
= 0;
2667 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2668 tree dname
= DECL_NAME (decl
);
2673 if (!processing_specialization
)
2676 tsk
= tsk_excessive_parms
;
2679 tsk
= current_tmpl_spec_kind (template_count
);
2684 if (processing_specialization
&& !VAR_P (decl
))
2687 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2689 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2692 /* This could be something like:
2694 template <class T> void f(T);
2695 class S { friend void f<>(int); } */
2699 /* This case handles bogus declarations like template <>
2700 template <class T> void f<int>(); */
2702 error ("template-id %qD in declaration of primary template",
2709 case tsk_invalid_member_spec
:
2710 /* The error has already been reported in
2711 check_specialization_scope. */
2712 return error_mark_node
;
2714 case tsk_invalid_expl_inst
:
2715 error ("template parameter list used in explicit instantiation");
2721 error ("definition provided for explicit instantiation");
2723 explicit_instantiation
= 1;
2726 case tsk_excessive_parms
:
2727 case tsk_insufficient_parms
:
2728 if (tsk
== tsk_excessive_parms
)
2729 error ("too many template parameter lists in declaration of %qD",
2731 else if (template_header_count
)
2732 error("too few template parameter lists in declaration of %qD", decl
);
2734 error("explicit specialization of %qD must be introduced by "
2735 "%<template <>%>", decl
);
2740 error ("explicit specialization declared %<concept%>");
2742 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2743 /* In cases like template<> constexpr bool v = true;
2744 We'll give an error in check_template_variable. */
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2749 member_specialization
= 1;
2755 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2757 /* This case handles bogus declarations like template <>
2758 template <class T> void f<int>(); */
2760 if (!uses_template_parms (declarator
))
2761 error ("template-id %qD in declaration of primary template",
2763 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2765 /* Partial specialization of variable template. */
2766 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2770 else if (cxx_dialect
< cxx14
)
2771 error ("non-type partial specialization %qD "
2772 "is not allowed", declarator
);
2774 error ("non-class, non-variable partial specialization %qD "
2775 "is not allowed", declarator
);
2780 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2781 /* This is a specialization of a member template, without
2782 specialization the containing class. Something like:
2784 template <class T> struct S {
2785 template <class U> void f (U);
2787 template <> template <class U> void S<int>::f(U) {}
2789 That's a specialization -- but of the entire template. */
2797 if ((specialization
|| member_specialization
)
2798 /* This doesn't apply to variable templates. */
2799 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2800 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2802 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2803 for (; t
; t
= TREE_CHAIN (t
))
2804 if (TREE_PURPOSE (t
))
2806 permerror (input_location
,
2807 "default argument specified in explicit specialization");
2812 if (specialization
|| member_specialization
|| explicit_instantiation
)
2814 tree tmpl
= NULL_TREE
;
2815 tree targs
= NULL_TREE
;
2816 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2818 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2819 if (!was_template_id
)
2823 gcc_assert (identifier_p (declarator
));
2828 /* If there is no class context, the explicit instantiation
2829 must be at namespace scope. */
2830 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2832 /* Find the namespace binding, using the declaration
2834 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2836 if (fns
== error_mark_node
)
2837 /* If lookup fails, look for a friend declaration so we can
2838 give a better diagnostic. */
2839 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2840 /*type*/false, /*complain*/true,
2843 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2845 error ("%qD is not a template function", dname
);
2846 fns
= error_mark_node
;
2850 declarator
= lookup_template_function (fns
, NULL_TREE
);
2853 if (declarator
== error_mark_node
)
2854 return error_mark_node
;
2856 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2858 if (!explicit_instantiation
)
2859 /* A specialization in class scope. This is invalid,
2860 but the error will already have been flagged by
2861 check_specialization_scope. */
2862 return error_mark_node
;
2865 /* It's not valid to write an explicit instantiation in
2868 class C { template void f(); }
2870 This case is caught by the parser. However, on
2873 template class C { void f(); };
2875 (which is invalid) we can get here. The error will be
2882 else if (ctype
!= NULL_TREE
2883 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2885 // We'll match variable templates in start_decl.
2889 /* Find the list of functions in ctype that have the same
2890 name as the declared function. */
2891 tree name
= TREE_OPERAND (declarator
, 0);
2893 if (constructor_name_p (name
, ctype
))
2895 if (DECL_CONSTRUCTOR_P (decl
)
2896 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2897 : !CLASSTYPE_DESTRUCTOR (ctype
))
2899 /* From [temp.expl.spec]:
2901 If such an explicit specialization for the member
2902 of a class template names an implicitly-declared
2903 special member function (clause _special_), the
2904 program is ill-formed.
2906 Similar language is found in [temp.explicit]. */
2907 error ("specialization of implicitly-declared special member function");
2908 return error_mark_node
;
2911 name
= DECL_NAME (decl
);
2914 /* For a type-conversion operator, We might be looking for
2915 `operator int' which will be a specialization of
2916 `operator T'. Grab all the conversion operators, and
2917 then select from them. */
2918 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2919 ? conv_op_identifier
: name
);
2921 if (fns
== NULL_TREE
)
2923 error ("no member function %qD declared in %qT", name
, ctype
);
2924 return error_mark_node
;
2927 TREE_OPERAND (declarator
, 0) = fns
;
2930 /* Figure out what exactly is being specialized at this point.
2931 Note that for an explicit instantiation, even one for a
2932 member function, we cannot tell a priori whether the
2933 instantiation is for a member template, or just a member
2934 function of a template class. Even if a member template is
2935 being instantiated, the member template arguments may be
2936 elided if they can be deduced from the rest of the
2938 tmpl
= determine_specialization (declarator
, decl
,
2940 member_specialization
,
2944 if (!tmpl
|| tmpl
== error_mark_node
)
2945 /* We couldn't figure out what this declaration was
2947 return error_mark_node
;
2950 if (TREE_CODE (decl
) == FUNCTION_DECL
2951 && DECL_HIDDEN_FRIEND_P (tmpl
))
2953 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2954 "friend declaration %qD is not visible to "
2955 "explicit specialization", tmpl
))
2956 inform (DECL_SOURCE_LOCATION (tmpl
),
2957 "friend declaration here");
2959 else if (!ctype
&& !is_friend
2960 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2961 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2963 tree gen_tmpl
= most_general_template (tmpl
);
2965 if (explicit_instantiation
)
2967 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2968 is done by do_decl_instantiation later. */
2970 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2971 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2973 if (arg_depth
> parm_depth
)
2975 /* If TMPL is not the most general template (for
2976 example, if TMPL is a friend template that is
2977 injected into namespace scope), then there will
2978 be too many levels of TARGS. Remove some of them
2983 new_targs
= make_tree_vec (parm_depth
);
2984 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2985 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2986 = TREE_VEC_ELT (targs
, i
);
2990 return instantiate_template (tmpl
, targs
, tf_error
);
2993 /* If we thought that the DECL was a member function, but it
2994 turns out to be specializing a static member function,
2995 make DECL a static member function as well. */
2996 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2997 && DECL_STATIC_FUNCTION_P (tmpl
)
2998 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2999 revert_static_member_fn (decl
);
3001 /* If this is a specialization of a member template of a
3002 template class, we want to return the TEMPLATE_DECL, not
3003 the specialization of it. */
3004 if (tsk
== tsk_template
&& !was_template_id
)
3006 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3007 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3008 DECL_INITIAL (result
) = NULL_TREE
;
3012 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3013 DECL_SOURCE_LOCATION (result
)
3014 = DECL_SOURCE_LOCATION (decl
);
3015 /* We want to use the argument list specified in the
3016 definition, not in the original declaration. */
3017 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3018 for (parm
= DECL_ARGUMENTS (result
); parm
;
3019 parm
= DECL_CHAIN (parm
))
3020 DECL_CONTEXT (parm
) = result
;
3022 return register_specialization (tmpl
, gen_tmpl
, targs
,
3026 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3027 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3029 if (was_template_id
)
3030 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3032 /* Inherit default function arguments from the template
3033 DECL is specializing. */
3034 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3035 copy_default_args_to_explicit_spec (decl
);
3037 /* This specialization has the same protection as the
3038 template it specializes. */
3039 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3040 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3042 /* 7.1.1-1 [dcl.stc]
3044 A storage-class-specifier shall not be specified in an
3045 explicit specialization...
3047 The parser rejects these, so unless action is taken here,
3048 explicit function specializations will always appear with
3051 The action recommended by the C++ CWG in response to C++
3052 defect report 605 is to make the storage class and linkage
3053 of the explicit specialization match the templated function:
3055 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3057 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3059 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3060 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3062 /* A concept cannot be specialized. */
3063 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3065 error ("explicit specialization of function concept %qD",
3067 return error_mark_node
;
3070 /* This specialization has the same linkage and visibility as
3071 the function template it specializes. */
3072 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3073 if (! TREE_PUBLIC (decl
))
3075 DECL_INTERFACE_KNOWN (decl
) = 1;
3076 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3078 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3079 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3081 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3082 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3086 /* If DECL is a friend declaration, declared using an
3087 unqualified name, the namespace associated with DECL may
3088 have been set incorrectly. For example, in:
3090 template <typename T> void f(T);
3092 struct S { friend void f<int>(int); }
3095 we will have set the DECL_CONTEXT for the friend
3096 declaration to N, rather than to the global namespace. */
3097 if (DECL_NAMESPACE_SCOPE_P (decl
))
3098 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3100 if (is_friend
&& !have_def
)
3101 /* This is not really a declaration of a specialization.
3102 It's just the name of an instantiation. But, it's not
3103 a request for an instantiation, either. */
3104 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3105 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3106 /* A specialization is not necessarily COMDAT. */
3107 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3108 && DECL_DECLARED_INLINE_P (decl
));
3109 else if (VAR_P (decl
))
3110 DECL_COMDAT (decl
) = false;
3112 /* If this is a full specialization, register it so that we can find
3113 it again. Partial specializations will be registered in
3114 process_partial_specialization. */
3115 if (!processing_template_decl
)
3116 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3119 /* A 'structor should already have clones. */
3120 gcc_assert (decl
== error_mark_node
3121 || variable_template_p (tmpl
)
3122 || !(DECL_CONSTRUCTOR_P (decl
)
3123 || DECL_DESTRUCTOR_P (decl
))
3124 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3131 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3132 parameters. These are represented in the same format used for
3133 DECL_TEMPLATE_PARMS. */
3136 comp_template_parms (const_tree parms1
, const_tree parms2
)
3141 if (parms1
== parms2
)
3144 for (p1
= parms1
, p2
= parms2
;
3145 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3146 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3148 tree t1
= TREE_VALUE (p1
);
3149 tree t2
= TREE_VALUE (p2
);
3152 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3153 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3155 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3158 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3160 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3161 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3163 /* If either of the template parameters are invalid, assume
3164 they match for the sake of error recovery. */
3165 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3168 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3171 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3172 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3173 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3175 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3180 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3181 /* One set of parameters has more parameters lists than the
3188 /* Determine whether PARM is a parameter pack. */
3191 template_parameter_pack_p (const_tree parm
)
3193 /* Determine if we have a non-type template parameter pack. */
3194 if (TREE_CODE (parm
) == PARM_DECL
)
3195 return (DECL_TEMPLATE_PARM_P (parm
)
3196 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3197 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3198 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3200 /* If this is a list of template parameters, we could get a
3201 TYPE_DECL or a TEMPLATE_DECL. */
3202 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3203 parm
= TREE_TYPE (parm
);
3205 /* Otherwise it must be a type template parameter. */
3206 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3207 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3208 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3211 /* Determine if T is a function parameter pack. */
3214 function_parameter_pack_p (const_tree t
)
3216 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3217 return DECL_PACK_P (t
);
3221 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3222 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3225 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3227 if (! primary_func_tmpl_inst
3228 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3229 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3232 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3235 /* Return true iff the function parameter PARAM_DECL was expanded
3236 from the function parameter pack PACK. */
3239 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3241 if (DECL_ARTIFICIAL (param_decl
)
3242 || !function_parameter_pack_p (pack
))
3245 /* The parameter pack and its pack arguments have the same
3247 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3250 /* Determine whether ARGS describes a variadic template args list,
3251 i.e., one that is terminated by a template argument pack. */
3254 template_args_variadic_p (tree args
)
3259 if (args
== NULL_TREE
)
3262 args
= INNERMOST_TEMPLATE_ARGS (args
);
3263 nargs
= TREE_VEC_LENGTH (args
);
3268 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3270 return ARGUMENT_PACK_P (last_parm
);
3273 /* Generate a new name for the parameter pack name NAME (an
3274 IDENTIFIER_NODE) that incorporates its */
3277 make_ith_pack_parameter_name (tree name
, int i
)
3279 /* Munge the name to include the parameter index. */
3280 #define NUMBUF_LEN 128
3281 char numbuf
[NUMBUF_LEN
];
3285 if (name
== NULL_TREE
)
3287 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3288 newname_len
= IDENTIFIER_LENGTH (name
)
3289 + strlen (numbuf
) + 2;
3290 newname
= (char*)alloca (newname_len
);
3291 snprintf (newname
, newname_len
,
3292 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3293 return get_identifier (newname
);
3296 /* Return true if T is a primary function, class or alias template
3297 specialization, not including the template pattern. */
3300 primary_template_specialization_p (const_tree t
)
3305 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3306 return (DECL_LANG_SPECIFIC (t
)
3307 && DECL_USE_TEMPLATE (t
)
3308 && DECL_TEMPLATE_INFO (t
)
3309 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3310 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3311 return (CLASSTYPE_TEMPLATE_INFO (t
)
3312 && CLASSTYPE_USE_TEMPLATE (t
)
3313 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3314 else if (alias_template_specialization_p (t
))
3319 /* Return true if PARM is a template template parameter. */
3322 template_template_parameter_p (const_tree parm
)
3324 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3327 /* Return true iff PARM is a DECL representing a type template
3331 template_type_parameter_p (const_tree parm
)
3334 && (TREE_CODE (parm
) == TYPE_DECL
3335 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3336 && DECL_TEMPLATE_PARM_P (parm
));
3339 /* Return the template parameters of T if T is a
3340 primary template instantiation, NULL otherwise. */
3343 get_primary_template_innermost_parameters (const_tree t
)
3345 tree parms
= NULL
, template_info
= NULL
;
3347 if ((template_info
= get_template_info (t
))
3348 && primary_template_specialization_p (t
))
3349 parms
= INNERMOST_TEMPLATE_PARMS
3350 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3355 /* Return the template parameters of the LEVELth level from the full list
3356 of template parameters PARMS. */
3359 get_template_parms_at_level (tree parms
, int level
)
3363 || TREE_CODE (parms
) != TREE_LIST
3364 || level
> TMPL_PARMS_DEPTH (parms
))
3367 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3368 if (TMPL_PARMS_DEPTH (p
) == level
)
3374 /* Returns the template arguments of T if T is a template instantiation,
3378 get_template_innermost_arguments (const_tree t
)
3380 tree args
= NULL
, template_info
= NULL
;
3382 if ((template_info
= get_template_info (t
))
3383 && TI_ARGS (template_info
))
3384 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3389 /* Return the argument pack elements of T if T is a template argument pack,
3393 get_template_argument_pack_elems (const_tree t
)
3395 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3396 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3399 return ARGUMENT_PACK_ARGS (t
);
3402 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3403 ARGUMENT_PACK_SELECT represents. */
3406 argument_pack_select_arg (tree t
)
3408 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3409 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3411 /* If the selected argument is an expansion E, that most likely means we were
3412 called from gen_elem_of_pack_expansion_instantiation during the
3413 substituting of an argument pack (of which the Ith element is a pack
3414 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3415 In this case, the Ith element resulting from this substituting is going to
3416 be a pack expansion, which pattern is the pattern of E. Let's return the
3417 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3418 resulting pack expansion from it. */
3419 if (PACK_EXPANSION_P (arg
))
3421 /* Make sure we aren't throwing away arg info. */
3422 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3423 arg
= PACK_EXPANSION_PATTERN (arg
);
3430 /* True iff FN is a function representing a built-in variadic parameter
3434 builtin_pack_fn_p (tree fn
)
3437 || TREE_CODE (fn
) != FUNCTION_DECL
3438 || !DECL_IS_BUILTIN (fn
))
3441 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3447 /* True iff CALL is a call to a function representing a built-in variadic
3451 builtin_pack_call_p (tree call
)
3453 if (TREE_CODE (call
) != CALL_EXPR
)
3455 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3458 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3461 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3464 tree ohi
= CALL_EXPR_ARG (call
, 0);
3465 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3466 false/*fn*/, true/*int_cst*/);
3468 if (value_dependent_expression_p (hi
))
3472 call
= copy_node (call
);
3473 CALL_EXPR_ARG (call
, 0) = hi
;
3475 tree ex
= make_pack_expansion (call
, complain
);
3476 tree vec
= make_tree_vec (1);
3477 TREE_VEC_ELT (vec
, 0) = ex
;
3482 hi
= cxx_constant_value (hi
);
3483 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3485 /* Calculate the largest value of len that won't make the size of the vec
3486 overflow an int. The compiler will exceed resource limits long before
3487 this, but it seems a decent place to diagnose. */
3488 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3490 if (len
< 0 || len
> max
)
3492 if ((complain
& tf_error
)
3493 && hi
!= error_mark_node
)
3494 error ("argument to __integer_pack must be between 0 and %d", max
);
3495 return error_mark_node
;
3498 tree vec
= make_tree_vec (len
);
3500 for (int i
= 0; i
< len
; ++i
)
3501 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3507 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3511 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3514 if (!builtin_pack_call_p (call
))
3517 tree fn
= CALL_EXPR_FN (call
);
3519 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3520 return expand_integer_pack (call
, args
, complain
, in_decl
);
3525 /* Structure used to track the progress of find_parameter_packs_r. */
3526 struct find_parameter_pack_data
3528 /* TREE_LIST that will contain all of the parameter packs found by
3530 tree
* parameter_packs
;
3532 /* Set of AST nodes that have been visited by the traversal. */
3533 hash_set
<tree
> *visited
;
3535 /* True iff we're making a type pack expansion. */
3536 bool type_pack_expansion_p
;
3539 /* Identifies all of the argument packs that occur in a template
3540 argument and appends them to the TREE_LIST inside DATA, which is a
3541 find_parameter_pack_data structure. This is a subroutine of
3542 make_pack_expansion and uses_parameter_packs. */
3544 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3547 struct find_parameter_pack_data
* ppd
=
3548 (struct find_parameter_pack_data
*)data
;
3549 bool parameter_pack_p
= false;
3551 /* Handle type aliases/typedefs. */
3552 if (TYPE_ALIAS_P (t
))
3554 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3555 cp_walk_tree (&TI_ARGS (tinfo
),
3556 &find_parameter_packs_r
,
3562 /* Identify whether this is a parameter pack or not. */
3563 switch (TREE_CODE (t
))
3565 case TEMPLATE_PARM_INDEX
:
3566 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3567 parameter_pack_p
= true;
3570 case TEMPLATE_TYPE_PARM
:
3571 t
= TYPE_MAIN_VARIANT (t
);
3573 case TEMPLATE_TEMPLATE_PARM
:
3574 /* If the placeholder appears in the decl-specifier-seq of a function
3575 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3576 is a pack expansion, the invented template parameter is a template
3578 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3579 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3580 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3581 parameter_pack_p
= true;
3586 if (DECL_PACK_P (t
))
3588 /* We don't want to walk into the type of a PARM_DECL,
3589 because we don't want to see the type parameter pack. */
3591 parameter_pack_p
= true;
3596 if (DECL_PACK_P (t
))
3598 /* We don't want to walk into the type of a variadic capture proxy,
3599 because we don't want to see the type parameter pack. */
3601 parameter_pack_p
= true;
3603 else if (variable_template_specialization_p (t
))
3605 cp_walk_tree (&DECL_TI_ARGS (t
),
3606 find_parameter_packs_r
,
3613 if (builtin_pack_call_p (t
))
3614 parameter_pack_p
= true;
3618 parameter_pack_p
= true;
3621 /* Not a parameter pack. */
3625 if (parameter_pack_p
)
3627 /* Add this parameter pack to the list. */
3628 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3632 cp_walk_tree (&TYPE_CONTEXT (t
),
3633 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3635 /* This switch statement will return immediately if we don't find a
3637 switch (TREE_CODE (t
))
3639 case TEMPLATE_PARM_INDEX
:
3642 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3643 /* Check the template itself. */
3644 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3645 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3646 /* Check the template arguments. */
3647 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3652 case TEMPLATE_TYPE_PARM
:
3653 case TEMPLATE_TEMPLATE_PARM
:
3660 /* Ignore the declaration of a capture proxy for a parameter pack. */
3661 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3666 if (TYPE_PTRMEMFUNC_P (t
))
3672 if (TYPE_TEMPLATE_INFO (t
))
3673 cp_walk_tree (&TYPE_TI_ARGS (t
),
3674 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3680 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3685 cp_walk_tree (&TREE_TYPE (t
),
3686 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3690 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3695 case TYPE_PACK_EXPANSION
:
3696 case EXPR_PACK_EXPANSION
:
3701 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3706 case IDENTIFIER_NODE
:
3707 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3714 /* Look at explicit captures. */
3715 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3716 cap
; cap
= TREE_CHAIN (cap
))
3717 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3719 /* Since we defer implicit capture, look in the body as well. */
3720 tree fn
= lambda_function (t
);
3721 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3729 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3730 type_pack_expansion_p to false so that any placeholders
3731 within the expression don't get marked as parameter packs. */
3732 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3733 ppd
->type_pack_expansion_p
= false;
3734 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3736 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3748 /* Determines if the expression or type T uses any parameter packs. */
3750 uses_parameter_packs (tree t
)
3752 tree parameter_packs
= NULL_TREE
;
3753 struct find_parameter_pack_data ppd
;
3754 ppd
.parameter_packs
= ¶meter_packs
;
3755 ppd
.visited
= new hash_set
<tree
>;
3756 ppd
.type_pack_expansion_p
= false;
3757 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3759 return parameter_packs
!= NULL_TREE
;
3762 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3763 representation a base-class initializer into a parameter pack
3764 expansion. If all goes well, the resulting node will be an
3765 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3768 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3771 tree parameter_packs
= NULL_TREE
;
3772 bool for_types
= false;
3773 struct find_parameter_pack_data ppd
;
3775 if (!arg
|| arg
== error_mark_node
)
3778 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3780 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3781 class initializer. In this case, the TREE_PURPOSE will be a
3782 _TYPE node (representing the base class expansion we're
3783 initializing) and the TREE_VALUE will be a TREE_LIST
3784 containing the initialization arguments.
3786 The resulting expansion looks somewhat different from most
3787 expansions. Rather than returning just one _EXPANSION, we
3788 return a TREE_LIST whose TREE_PURPOSE is a
3789 TYPE_PACK_EXPANSION containing the bases that will be
3790 initialized. The TREE_VALUE will be identical to the
3791 original TREE_VALUE, which is a list of arguments that will
3792 be passed to each base. We do not introduce any new pack
3793 expansion nodes into the TREE_VALUE (although it is possible
3794 that some already exist), because the TREE_PURPOSE and
3795 TREE_VALUE all need to be expanded together with the same
3796 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3797 resulting TREE_PURPOSE will mention the parameter packs in
3798 both the bases and the arguments to the bases. */
3801 tree parameter_packs
= NULL_TREE
;
3803 /* Determine which parameter packs will be used by the base
3805 ppd
.visited
= new hash_set
<tree
>;
3806 ppd
.parameter_packs
= ¶meter_packs
;
3807 ppd
.type_pack_expansion_p
= true;
3808 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3809 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3812 if (parameter_packs
== NULL_TREE
)
3814 if (complain
& tf_error
)
3815 error ("base initializer expansion %qT contains no parameter packs",
3818 return error_mark_node
;
3821 if (TREE_VALUE (arg
) != void_type_node
)
3823 /* Collect the sets of parameter packs used in each of the
3824 initialization arguments. */
3825 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3827 /* Determine which parameter packs will be expanded in this
3829 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3836 /* Create the pack expansion type for the base type. */
3837 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3838 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3839 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3840 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3842 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3843 they will rarely be compared to anything. */
3844 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3846 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3849 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3852 /* Build the PACK_EXPANSION_* node. */
3854 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3855 : make_node (EXPR_PACK_EXPANSION
);
3856 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3857 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3859 /* Propagate type and const-expression information. */
3860 TREE_TYPE (result
) = TREE_TYPE (arg
);
3861 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3862 /* Mark this read now, since the expansion might be length 0. */
3863 mark_exp_read (arg
);
3866 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3867 they will rarely be compared to anything. */
3868 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3870 /* Determine which parameter packs will be expanded. */
3871 ppd
.parameter_packs
= ¶meter_packs
;
3872 ppd
.visited
= new hash_set
<tree
>;
3873 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3874 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3877 /* Make sure we found some parameter packs. */
3878 if (parameter_packs
== NULL_TREE
)
3880 if (complain
& tf_error
)
3883 error ("expansion pattern %qT contains no argument packs", arg
);
3885 error ("expansion pattern %qE contains no argument packs", arg
);
3887 return error_mark_node
;
3889 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3891 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3896 /* Checks T for any "bare" parameter packs, which have not yet been
3897 expanded, and issues an error if any are found. This operation can
3898 only be done on full expressions or types (e.g., an expression
3899 statement, "if" condition, etc.), because we could have expressions like:
3901 foo(f(g(h(args)))...)
3903 where "args" is a parameter pack. check_for_bare_parameter_packs
3904 should not be called for the subexpressions args, h(args),
3905 g(h(args)), or f(g(h(args))), because we would produce erroneous
3908 Returns TRUE and emits an error if there were bare parameter packs,
3909 returns FALSE otherwise. */
3911 check_for_bare_parameter_packs (tree t
)
3913 tree parameter_packs
= NULL_TREE
;
3914 struct find_parameter_pack_data ppd
;
3916 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3919 /* A lambda might use a parameter pack from the containing context. */
3920 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
3923 if (TREE_CODE (t
) == TYPE_DECL
)
3926 ppd
.parameter_packs
= ¶meter_packs
;
3927 ppd
.visited
= new hash_set
<tree
>;
3928 ppd
.type_pack_expansion_p
= false;
3929 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3932 if (parameter_packs
)
3934 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3935 error_at (loc
, "parameter packs not expanded with %<...%>:");
3936 while (parameter_packs
)
3938 tree pack
= TREE_VALUE (parameter_packs
);
3939 tree name
= NULL_TREE
;
3941 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3942 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3943 name
= TYPE_NAME (pack
);
3944 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3945 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3946 else if (TREE_CODE (pack
) == CALL_EXPR
)
3947 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3949 name
= DECL_NAME (pack
);
3952 inform (loc
, " %qD", name
);
3954 inform (loc
, " <anonymous>");
3956 parameter_packs
= TREE_CHAIN (parameter_packs
);
3965 /* Expand any parameter packs that occur in the template arguments in
3968 expand_template_argument_pack (tree args
)
3970 if (args
== error_mark_node
)
3971 return error_mark_node
;
3973 tree result_args
= NULL_TREE
;
3974 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3975 int num_result_args
= -1;
3976 int non_default_args_count
= -1;
3978 /* First, determine if we need to expand anything, and the number of
3979 slots we'll need. */
3980 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3982 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3983 if (arg
== NULL_TREE
)
3985 if (ARGUMENT_PACK_P (arg
))
3987 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3988 if (num_result_args
< 0)
3989 num_result_args
= in_arg
+ num_packed
;
3991 num_result_args
+= num_packed
;
3995 if (num_result_args
>= 0)
4000 /* If no expansion is necessary, we're done. */
4001 if (num_result_args
< 0)
4004 /* Expand arguments. */
4005 result_args
= make_tree_vec (num_result_args
);
4006 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4007 non_default_args_count
=
4008 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4009 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4011 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4012 if (ARGUMENT_PACK_P (arg
))
4014 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4015 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4016 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4017 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4018 if (non_default_args_count
> 0)
4019 non_default_args_count
+= num_packed
- 1;
4023 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4027 if (non_default_args_count
>= 0)
4028 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4032 /* Checks if DECL shadows a template parameter.
4034 [temp.local]: A template-parameter shall not be redeclared within its
4035 scope (including nested scopes).
4037 Emits an error and returns TRUE if the DECL shadows a parameter,
4038 returns FALSE otherwise. */
4041 check_template_shadow (tree decl
)
4045 /* If we're not in a template, we can't possibly shadow a template
4047 if (!current_template_parms
)
4050 /* Figure out what we're shadowing. */
4051 decl
= OVL_FIRST (decl
);
4052 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4054 /* If there's no previous binding for this name, we're not shadowing
4055 anything, let alone a template parameter. */
4059 /* If we're not shadowing a template parameter, we're done. Note
4060 that OLDDECL might be an OVERLOAD (or perhaps even an
4061 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4063 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4066 /* We check for decl != olddecl to avoid bogus errors for using a
4067 name inside a class. We check TPFI to avoid duplicate errors for
4068 inline member templates. */
4070 || (DECL_TEMPLATE_PARM_P (decl
)
4071 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4074 /* Don't complain about the injected class name, as we've already
4075 complained about the class itself. */
4076 if (DECL_SELF_REFERENCE_P (decl
))
4079 if (DECL_TEMPLATE_PARM_P (decl
))
4080 error ("declaration of template parameter %q+D shadows "
4081 "template parameter", decl
);
4083 error ("declaration of %q+#D shadows template parameter", decl
);
4084 inform (DECL_SOURCE_LOCATION (olddecl
),
4085 "template parameter %qD declared here", olddecl
);
4089 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4090 ORIG_LEVEL, DECL, and TYPE. */
4093 build_template_parm_index (int index
,
4099 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4100 TEMPLATE_PARM_IDX (t
) = index
;
4101 TEMPLATE_PARM_LEVEL (t
) = level
;
4102 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4103 TEMPLATE_PARM_DECL (t
) = decl
;
4104 TREE_TYPE (t
) = type
;
4105 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4106 TREE_READONLY (t
) = TREE_READONLY (decl
);
4111 /* Find the canonical type parameter for the given template type
4112 parameter. Returns the canonical type parameter, which may be TYPE
4113 if no such parameter existed. */
4116 canonical_type_parameter (tree type
)
4119 int idx
= TEMPLATE_TYPE_IDX (type
);
4120 if (!canonical_template_parms
)
4121 vec_alloc (canonical_template_parms
, idx
+ 1);
4123 if (canonical_template_parms
->length () <= (unsigned) idx
)
4124 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4126 list
= (*canonical_template_parms
)[idx
];
4127 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4128 list
= TREE_CHAIN (list
);
4131 return TREE_VALUE (list
);
4134 (*canonical_template_parms
)[idx
]
4135 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4140 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4141 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4142 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4143 new one is created. */
4146 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4147 tsubst_flags_t complain
)
4149 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4150 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4151 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4152 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4154 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4157 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4158 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4159 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4160 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4161 DECL_ARTIFICIAL (decl
) = 1;
4162 SET_DECL_TEMPLATE_PARM_P (decl
);
4164 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4165 TEMPLATE_PARM_LEVEL (index
) - levels
,
4166 TEMPLATE_PARM_ORIG_LEVEL (index
),
4168 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4169 TEMPLATE_PARM_PARAMETER_PACK (t
)
4170 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4172 /* Template template parameters need this. */
4173 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4175 DECL_TEMPLATE_RESULT (decl
)
4176 = build_decl (DECL_SOURCE_LOCATION (decl
),
4177 TYPE_DECL
, DECL_NAME (decl
), type
);
4178 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4179 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4180 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4184 return TEMPLATE_PARM_DESCENDANTS (index
);
4187 /* Process information from new template parameter PARM and append it
4188 to the LIST being built. This new parameter is a non-type
4189 parameter iff IS_NON_TYPE is true. This new parameter is a
4190 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4194 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4195 bool is_non_type
, bool is_parameter_pack
)
4200 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4201 tree defval
= TREE_PURPOSE (parm
);
4202 tree constr
= TREE_TYPE (parm
);
4206 tree p
= tree_last (list
);
4208 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4211 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4212 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4214 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4222 parm
= TREE_VALUE (parm
);
4224 SET_DECL_TEMPLATE_PARM_P (parm
);
4226 if (TREE_TYPE (parm
) != error_mark_node
)
4230 The top-level cv-qualifiers on the template-parameter are
4231 ignored when determining its type. */
4232 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4233 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4234 TREE_TYPE (parm
) = error_mark_node
;
4235 else if (uses_parameter_packs (TREE_TYPE (parm
))
4236 && !is_parameter_pack
4237 /* If we're in a nested template parameter list, the template
4238 template parameter could be a parameter pack. */
4239 && processing_template_parmlist
== 1)
4241 /* This template parameter is not a parameter pack, but it
4242 should be. Complain about "bare" parameter packs. */
4243 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4245 /* Recover by calling this a parameter pack. */
4246 is_parameter_pack
= true;
4250 /* A template parameter is not modifiable. */
4251 TREE_CONSTANT (parm
) = 1;
4252 TREE_READONLY (parm
) = 1;
4253 decl
= build_decl (parm_loc
,
4254 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4255 TREE_CONSTANT (decl
) = 1;
4256 TREE_READONLY (decl
) = 1;
4257 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4258 = build_template_parm_index (idx
, processing_template_decl
,
4259 processing_template_decl
,
4260 decl
, TREE_TYPE (parm
));
4262 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4263 = is_parameter_pack
;
4268 parm
= TREE_VALUE (TREE_VALUE (parm
));
4270 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4272 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4273 /* This is for distinguishing between real templates and template
4274 template parameters */
4275 TREE_TYPE (parm
) = t
;
4276 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4281 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4282 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4283 decl
= build_decl (parm_loc
,
4284 TYPE_DECL
, parm
, t
);
4287 TYPE_NAME (t
) = decl
;
4288 TYPE_STUB_DECL (t
) = decl
;
4290 TEMPLATE_TYPE_PARM_INDEX (t
)
4291 = build_template_parm_index (idx
, processing_template_decl
,
4292 processing_template_decl
,
4293 decl
, TREE_TYPE (parm
));
4294 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4295 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4297 DECL_ARTIFICIAL (decl
) = 1;
4298 SET_DECL_TEMPLATE_PARM_P (decl
);
4300 /* Build requirements for the type/template parameter.
4301 This must be done after SET_DECL_TEMPLATE_PARM_P or
4302 process_template_parm could fail. */
4303 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4307 /* Build the parameter node linking the parameter declaration,
4308 its default argument (if any), and its constraints (if any). */
4309 parm
= build_tree_list (defval
, parm
);
4310 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4312 return chainon (list
, parm
);
4315 /* The end of a template parameter list has been reached. Process the
4316 tree list into a parameter vector, converting each parameter into a more
4317 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4321 end_template_parm_list (tree parms
)
4325 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4327 /* Pop the dummy parameter level and add the real one. */
4328 current_template_parms
= TREE_CHAIN (current_template_parms
);
4330 current_template_parms
4331 = tree_cons (size_int (processing_template_decl
),
4332 saved_parmlist
, current_template_parms
);
4334 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4336 next
= TREE_CHAIN (parm
);
4337 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4338 TREE_CHAIN (parm
) = NULL_TREE
;
4341 --processing_template_parmlist
;
4343 return saved_parmlist
;
4346 // Explicitly indicate the end of the template parameter list. We assume
4347 // that the current template parameters have been constructed and/or
4348 // managed explicitly, as when creating new template template parameters
4349 // from a shorthand constraint.
4351 end_template_parm_list ()
4353 --processing_template_parmlist
;
4356 /* end_template_decl is called after a template declaration is seen. */
4359 end_template_decl (void)
4361 reset_specialization ();
4363 if (! processing_template_decl
)
4366 /* This matches the pushlevel in begin_template_parm_list. */
4369 --processing_template_decl
;
4370 current_template_parms
= TREE_CHAIN (current_template_parms
);
4373 /* Takes a TREE_LIST representing a template parameter and convert it
4374 into an argument suitable to be passed to the type substitution
4375 functions. Note that If the TREE_LIST contains an error_mark
4376 node, the returned argument is error_mark_node. */
4379 template_parm_to_arg (tree t
)
4383 || TREE_CODE (t
) != TREE_LIST
)
4386 if (error_operand_p (TREE_VALUE (t
)))
4387 return error_mark_node
;
4391 if (TREE_CODE (t
) == TYPE_DECL
4392 || TREE_CODE (t
) == TEMPLATE_DECL
)
4396 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4398 /* Turn this argument into a TYPE_ARGUMENT_PACK
4399 with a single element, which expands T. */
4400 tree vec
= make_tree_vec (1);
4402 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4404 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4406 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4407 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4412 t
= DECL_INITIAL (t
);
4414 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4416 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4417 with a single element, which expands T. */
4418 tree vec
= make_tree_vec (1);
4420 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4422 t
= convert_from_reference (t
);
4423 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4425 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4426 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4429 t
= convert_from_reference (t
);
4434 /* Given a single level of template parameters (a TREE_VEC), return it
4435 as a set of template arguments. */
4438 template_parms_level_to_args (tree parms
)
4440 tree a
= copy_node (parms
);
4441 TREE_TYPE (a
) = NULL_TREE
;
4442 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4443 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4446 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4451 /* Given a set of template parameters, return them as a set of template
4452 arguments. The template parameters are represented as a TREE_VEC, in
4453 the form documented in cp-tree.h for template arguments. */
4456 template_parms_to_args (tree parms
)
4459 tree args
= NULL_TREE
;
4460 int length
= TMPL_PARMS_DEPTH (parms
);
4463 /* If there is only one level of template parameters, we do not
4464 create a TREE_VEC of TREE_VECs. Instead, we return a single
4465 TREE_VEC containing the arguments. */
4467 args
= make_tree_vec (length
);
4469 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4471 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4474 TREE_VEC_ELT (args
, --l
) = a
;
4482 /* Within the declaration of a template, return the currently active
4483 template parameters as an argument TREE_VEC. */
4486 current_template_args (void)
4488 return template_parms_to_args (current_template_parms
);
4491 /* Update the declared TYPE by doing any lookups which were thought to be
4492 dependent, but are not now that we know the SCOPE of the declarator. */
4495 maybe_update_decl_type (tree orig_type
, tree scope
)
4497 tree type
= orig_type
;
4499 if (type
== NULL_TREE
)
4502 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4503 type
= TREE_TYPE (type
);
4505 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4506 && dependent_type_p (type
)
4507 /* Don't bother building up the args in this case. */
4508 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4510 /* tsubst in the args corresponding to the template parameters,
4511 including auto if present. Most things will be unchanged, but
4512 make_typename_type and tsubst_qualified_id will resolve
4513 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4514 tree args
= current_template_args ();
4515 tree auto_node
= type_uses_auto (type
);
4519 tree auto_vec
= make_tree_vec (1);
4520 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4521 args
= add_to_template_args (args
, auto_vec
);
4523 pushed
= push_scope (scope
);
4524 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4529 if (type
== error_mark_node
)
4532 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4534 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4537 type
= TYPE_NAME (type
);
4542 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4543 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4544 the new template is a member template. */
4547 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4549 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4550 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4551 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4552 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4553 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4558 struct template_parm_data
4560 /* The level of the template parameters we are currently
4564 /* The index of the specialization argument we are currently
4568 /* An array whose size is the number of template parameters. The
4569 elements are nonzero if the parameter has been used in any one
4570 of the arguments processed so far. */
4573 /* An array whose size is the number of template arguments. The
4574 elements are nonzero if the argument makes use of template
4575 parameters of this level. */
4576 int* arg_uses_template_parms
;
4579 /* Subroutine of push_template_decl used to see if each template
4580 parameter in a partial specialization is used in the explicit
4581 argument list. If T is of the LEVEL given in DATA (which is
4582 treated as a template_parm_data*), then DATA->PARMS is marked
4586 mark_template_parm (tree t
, void* data
)
4590 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4592 template_parm_level_and_index (t
, &level
, &idx
);
4594 if (level
== tpd
->level
)
4596 tpd
->parms
[idx
] = 1;
4597 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4600 /* In C++17 the type of a non-type argument is a deduced context. */
4601 if (cxx_dialect
>= cxx17
4602 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4603 for_each_template_parm (TREE_TYPE (t
),
4604 &mark_template_parm
,
4607 /*include_nondeduced_p=*/false);
4609 /* Return zero so that for_each_template_parm will continue the
4610 traversal of the tree; we want to mark *every* template parm. */
4614 /* Process the partial specialization DECL. */
4617 process_partial_specialization (tree decl
)
4619 tree type
= TREE_TYPE (decl
);
4620 tree tinfo
= get_template_info (decl
);
4621 tree maintmpl
= TI_TEMPLATE (tinfo
);
4622 tree specargs
= TI_ARGS (tinfo
);
4623 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4624 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4627 int nargs
= TREE_VEC_LENGTH (inner_args
);
4630 bool did_error_intro
= false;
4631 struct template_parm_data tpd
;
4632 struct template_parm_data tpd2
;
4634 gcc_assert (current_template_parms
);
4636 /* A concept cannot be specialized. */
4637 if (flag_concepts
&& variable_concept_p (maintmpl
))
4639 error ("specialization of variable concept %q#D", maintmpl
);
4640 return error_mark_node
;
4643 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4644 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4646 /* We check that each of the template parameters given in the
4647 partial specialization is used in the argument list to the
4648 specialization. For example:
4650 template <class T> struct S;
4651 template <class T> struct S<T*>;
4653 The second declaration is OK because `T*' uses the template
4654 parameter T, whereas
4656 template <class T> struct S<int>;
4658 is no good. Even trickier is:
4669 The S2<T> declaration is actually invalid; it is a
4670 full-specialization. Of course,
4673 struct S2<T (*)(U)>;
4675 or some such would have been OK. */
4676 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4677 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4678 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4680 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4681 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4682 for (i
= 0; i
< nargs
; ++i
)
4684 tpd
.current_arg
= i
;
4685 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4686 &mark_template_parm
,
4689 /*include_nondeduced_p=*/false);
4691 for (i
= 0; i
< ntparms
; ++i
)
4692 if (tpd
.parms
[i
] == 0)
4694 /* One of the template parms was not used in a deduced context in the
4696 if (!did_error_intro
)
4698 error ("template parameters not deducible in "
4699 "partial specialization:");
4700 did_error_intro
= true;
4703 inform (input_location
, " %qD",
4704 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4707 if (did_error_intro
)
4708 return error_mark_node
;
4710 /* [temp.class.spec]
4712 The argument list of the specialization shall not be identical to
4713 the implicit argument list of the primary template. */
4715 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4716 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4718 || !strictly_subsumes (current_template_constraints (),
4719 get_constraints (maintmpl
))))
4722 error ("partial specialization %q+D does not specialize "
4723 "any template arguments", decl
);
4725 error ("partial specialization %q+D does not specialize any "
4726 "template arguments and is not more constrained than", decl
);
4727 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4730 /* A partial specialization that replaces multiple parameters of the
4731 primary template with a pack expansion is less specialized for those
4733 if (nargs
< DECL_NTPARMS (maintmpl
))
4735 error ("partial specialization is not more specialized than the "
4736 "primary template because it replaces multiple parameters "
4737 "with a pack expansion");
4738 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4739 /* Avoid crash in process_partial_specialization. */
4743 /* If we aren't in a dependent class, we can actually try deduction. */
4744 else if (tpd
.level
== 1
4745 /* FIXME we should be able to handle a partial specialization of a
4746 partial instantiation, but currently we can't (c++/41727). */
4747 && TMPL_ARGS_DEPTH (specargs
) == 1
4748 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4750 if (permerror (input_location
, "partial specialization %qD is not "
4751 "more specialized than", decl
))
4752 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4756 /* [temp.class.spec]
4758 A partially specialized non-type argument expression shall not
4759 involve template parameters of the partial specialization except
4760 when the argument expression is a simple identifier.
4762 The type of a template parameter corresponding to a specialized
4763 non-type argument shall not be dependent on a parameter of the
4766 Also, we verify that pack expansions only occur at the
4767 end of the argument list. */
4768 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4770 for (i
= 0; i
< nargs
; ++i
)
4772 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4773 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4774 tree packed_args
= NULL_TREE
;
4777 if (ARGUMENT_PACK_P (arg
))
4779 /* Extract the arguments from the argument pack. We'll be
4780 iterating over these in the following loop. */
4781 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4782 len
= TREE_VEC_LENGTH (packed_args
);
4785 for (j
= 0; j
< len
; j
++)
4788 /* Get the Jth argument in the parameter pack. */
4789 arg
= TREE_VEC_ELT (packed_args
, j
);
4791 if (PACK_EXPANSION_P (arg
))
4793 /* Pack expansions must come at the end of the
4795 if ((packed_args
&& j
< len
- 1)
4796 || (!packed_args
&& i
< nargs
- 1))
4798 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4799 error ("parameter pack argument %qE must be at the "
4800 "end of the template argument list", arg
);
4802 error ("parameter pack argument %qT must be at the "
4803 "end of the template argument list", arg
);
4807 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4808 /* We only care about the pattern. */
4809 arg
= PACK_EXPANSION_PATTERN (arg
);
4811 if (/* These first two lines are the `non-type' bit. */
4813 && TREE_CODE (arg
) != TEMPLATE_DECL
4814 /* This next two lines are the `argument expression is not just a
4815 simple identifier' condition and also the `specialized
4816 non-type argument' bit. */
4817 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4818 && !(REFERENCE_REF_P (arg
)
4819 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4821 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4822 || (packed_args
&& uses_template_parms (arg
)))
4823 error ("template argument %qE involves template parameter(s)",
4827 /* Look at the corresponding template parameter,
4828 marking which template parameters its type depends
4830 tree type
= TREE_TYPE (parm
);
4834 /* We haven't yet initialized TPD2. Do so now. */
4835 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4836 /* The number of parameters here is the number in the
4837 main template, which, as checked in the assertion
4839 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4841 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4844 /* Mark the template parameters. But this time, we're
4845 looking for the template parameters of the main
4846 template, not in the specialization. */
4847 tpd2
.current_arg
= i
;
4848 tpd2
.arg_uses_template_parms
[i
] = 0;
4849 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4850 for_each_template_parm (type
,
4851 &mark_template_parm
,
4854 /*include_nondeduced_p=*/false);
4856 if (tpd2
.arg_uses_template_parms
[i
])
4858 /* The type depended on some template parameters.
4859 If they are fully specialized in the
4860 specialization, that's OK. */
4863 for (j
= 0; j
< nargs
; ++j
)
4864 if (tpd2
.parms
[j
] != 0
4865 && tpd
.arg_uses_template_parms
[j
])
4868 error_n (input_location
, count
,
4869 "type %qT of template argument %qE depends "
4870 "on a template parameter",
4871 "type %qT of template argument %qE depends "
4872 "on template parameters",
4881 /* We should only get here once. */
4882 if (TREE_CODE (decl
) == TYPE_DECL
)
4883 gcc_assert (!COMPLETE_TYPE_P (type
));
4885 // Build the template decl.
4886 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4887 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4888 TREE_TYPE (tmpl
) = type
;
4889 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4890 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4891 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4892 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4894 /* Give template template parms a DECL_CONTEXT of the template
4895 for which they are a parameter. */
4896 for (i
= 0; i
< ntparms
; ++i
)
4898 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4899 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4900 DECL_CONTEXT (parm
) = tmpl
;
4904 /* We didn't register this in check_explicit_specialization so we could
4905 wait until the constraints were set. */
4906 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4908 associate_classtype_constraints (type
);
4910 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4911 = tree_cons (specargs
, tmpl
,
4912 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4913 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4915 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4916 inst
= TREE_CHAIN (inst
))
4918 tree instance
= TREE_VALUE (inst
);
4919 if (TYPE_P (instance
)
4920 ? (COMPLETE_TYPE_P (instance
)
4921 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4922 : DECL_TEMPLATE_INSTANTIATION (instance
))
4924 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4925 tree inst_decl
= (DECL_P (instance
)
4926 ? instance
: TYPE_NAME (instance
));
4929 else if (spec
== error_mark_node
)
4930 permerror (input_location
,
4931 "declaration of %qD ambiguates earlier template "
4932 "instantiation for %qD", decl
, inst_decl
);
4933 else if (TREE_VALUE (spec
) == tmpl
)
4934 permerror (input_location
,
4935 "partial specialization of %qD after instantiation "
4936 "of %qD", decl
, inst_decl
);
4943 /* PARM is a template parameter of some form; return the corresponding
4944 TEMPLATE_PARM_INDEX. */
4947 get_template_parm_index (tree parm
)
4949 if (TREE_CODE (parm
) == PARM_DECL
4950 || TREE_CODE (parm
) == CONST_DECL
)
4951 parm
= DECL_INITIAL (parm
);
4952 else if (TREE_CODE (parm
) == TYPE_DECL
4953 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4954 parm
= TREE_TYPE (parm
);
4955 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4956 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4957 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4958 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4959 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4963 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4964 parameter packs used by the template parameter PARM. */
4967 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4969 /* A type parm can't refer to another parm. */
4970 if (TREE_CODE (parm
) == TYPE_DECL
)
4972 else if (TREE_CODE (parm
) == PARM_DECL
)
4974 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4979 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4981 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4982 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4983 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4986 /* PARM is a template parameter pack. Return any parameter packs used in
4987 its type or the type of any of its template parameters. If there are
4988 any such packs, it will be instantiated into a fixed template parameter
4989 list by partial instantiation rather than be fully deduced. */
4992 fixed_parameter_pack_p (tree parm
)
4994 /* This can only be true in a member template. */
4995 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4997 /* This can only be true for a parameter pack. */
4998 if (!template_parameter_pack_p (parm
))
5000 /* A type parm can't refer to another parm. */
5001 if (TREE_CODE (parm
) == TYPE_DECL
)
5004 tree parameter_packs
= NULL_TREE
;
5005 struct find_parameter_pack_data ppd
;
5006 ppd
.parameter_packs
= ¶meter_packs
;
5007 ppd
.visited
= new hash_set
<tree
>;
5008 ppd
.type_pack_expansion_p
= false;
5010 fixed_parameter_pack_p_1 (parm
, &ppd
);
5013 return parameter_packs
;
5016 /* Check that a template declaration's use of default arguments and
5017 parameter packs is not invalid. Here, PARMS are the template
5018 parameters. IS_PRIMARY is true if DECL is the thing declared by
5019 a primary template. IS_PARTIAL is true if DECL is a partial
5022 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5023 function template declaration or a friend class template
5024 declaration. In the function case, 1 indicates a declaration, 2
5025 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5026 emitted for extraneous default arguments.
5028 Returns TRUE if there were no errors found, FALSE otherwise. */
5031 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5032 bool is_partial
, int is_friend_decl
)
5035 int last_level_to_check
;
5037 bool no_errors
= true;
5041 A default template-argument shall not be specified in a
5042 function template declaration or a function template definition, nor
5043 in the template-parameter-list of the definition of a member of a
5046 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5047 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5048 /* You can't have a function template declaration in a local
5049 scope, nor you can you define a member of a class template in a
5053 if ((TREE_CODE (decl
) == TYPE_DECL
5055 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5056 || (TREE_CODE (decl
) == FUNCTION_DECL
5057 && LAMBDA_FUNCTION_P (decl
)))
5058 /* A lambda doesn't have an explicit declaration; don't complain
5059 about the parms of the enclosing class. */
5062 if (current_class_type
5063 && !TYPE_BEING_DEFINED (current_class_type
)
5064 && DECL_LANG_SPECIFIC (decl
)
5065 && DECL_DECLARES_FUNCTION_P (decl
)
5066 /* If this is either a friend defined in the scope of the class
5067 or a member function. */
5068 && (DECL_FUNCTION_MEMBER_P (decl
)
5069 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5070 : DECL_FRIEND_CONTEXT (decl
)
5071 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5073 /* And, if it was a member function, it really was defined in
5074 the scope of the class. */
5075 && (!DECL_FUNCTION_MEMBER_P (decl
)
5076 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5077 /* We already checked these parameters when the template was
5078 declared, so there's no need to do it again now. This function
5079 was defined in class scope, but we're processing its body now
5080 that the class is complete. */
5083 /* Core issue 226 (C++0x only): the following only applies to class
5086 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5090 If a template-parameter has a default template-argument, all
5091 subsequent template-parameters shall have a default
5092 template-argument supplied. */
5093 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5095 tree inner_parms
= TREE_VALUE (parm_level
);
5096 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5097 int seen_def_arg_p
= 0;
5100 for (i
= 0; i
< ntparms
; ++i
)
5102 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5104 if (parm
== error_mark_node
)
5107 if (TREE_PURPOSE (parm
))
5109 else if (seen_def_arg_p
5110 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5112 error ("no default argument for %qD", TREE_VALUE (parm
));
5113 /* For better subsequent error-recovery, we indicate that
5114 there should have been a default argument. */
5115 TREE_PURPOSE (parm
) = error_mark_node
;
5118 else if (!is_partial
5120 /* Don't complain about an enclosing partial
5122 && parm_level
== parms
5123 && TREE_CODE (decl
) == TYPE_DECL
5125 && template_parameter_pack_p (TREE_VALUE (parm
))
5126 /* A fixed parameter pack will be partially
5127 instantiated into a fixed length list. */
5128 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5130 /* A primary class template can only have one
5131 parameter pack, at the end of the template
5134 error ("parameter pack %q+D must be at the end of the"
5135 " template parameter list", TREE_VALUE (parm
));
5137 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5145 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5149 /* For an ordinary class template, default template arguments are
5150 allowed at the innermost level, e.g.:
5151 template <class T = int>
5153 but, in a partial specialization, they're not allowed even
5154 there, as we have in [temp.class.spec]:
5156 The template parameter list of a specialization shall not
5157 contain default template argument values.
5159 So, for a partial specialization, or for a function template
5160 (in C++98/C++03), we look at all of them. */
5163 /* But, for a primary class template that is not a partial
5164 specialization we look at all template parameters except the
5166 parms
= TREE_CHAIN (parms
);
5168 /* Figure out what error message to issue. */
5169 if (is_friend_decl
== 2)
5170 msg
= G_("default template arguments may not be used in function template "
5171 "friend re-declaration");
5172 else if (is_friend_decl
)
5173 msg
= G_("default template arguments may not be used in template "
5174 "friend declarations");
5175 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5176 msg
= G_("default template arguments may not be used in function templates "
5177 "without -std=c++11 or -std=gnu++11");
5178 else if (is_partial
)
5179 msg
= G_("default template arguments may not be used in "
5180 "partial specializations");
5181 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5182 msg
= G_("default argument for template parameter for class enclosing %qD");
5184 /* Per [temp.param]/9, "A default template-argument shall not be
5185 specified in the template-parameter-lists of the definition of
5186 a member of a class template that appears outside of the member's
5187 class.", thus if we aren't handling a member of a class template
5188 there is no need to examine the parameters. */
5191 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5192 /* If we're inside a class definition, there's no need to
5193 examine the parameters to the class itself. On the one
5194 hand, they will be checked when the class is defined, and,
5195 on the other, default arguments are valid in things like:
5196 template <class T = double>
5197 struct S { template <class U> void f(U); };
5198 Here the default argument for `S' has no bearing on the
5199 declaration of `f'. */
5200 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5202 /* Check everything. */
5203 last_level_to_check
= 0;
5205 for (parm_level
= parms
;
5206 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5207 parm_level
= TREE_CHAIN (parm_level
))
5209 tree inner_parms
= TREE_VALUE (parm_level
);
5213 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5214 for (i
= 0; i
< ntparms
; ++i
)
5216 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5219 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5224 if (is_friend_decl
== 2)
5231 /* Clear out the default argument so that we are not
5233 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5237 /* At this point, if we're still interested in issuing messages,
5238 they must apply to classes surrounding the object declared. */
5240 msg
= G_("default argument for template parameter for class "
5247 /* Worker for push_template_decl_real, called via
5248 for_each_template_parm. DATA is really an int, indicating the
5249 level of the parameters we are interested in. If T is a template
5250 parameter of that level, return nonzero. */
5253 template_parm_this_level_p (tree t
, void* data
)
5255 int this_level
= *(int *)data
;
5258 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5259 level
= TEMPLATE_PARM_LEVEL (t
);
5261 level
= TEMPLATE_TYPE_LEVEL (t
);
5262 return level
== this_level
;
5265 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5266 DATA is really an int, indicating the innermost outer level of parameters.
5267 If T is a template parameter of that level or further out, return
5271 template_parm_outer_level (tree t
, void *data
)
5273 int this_level
= *(int *)data
;
5276 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5277 level
= TEMPLATE_PARM_LEVEL (t
);
5279 level
= TEMPLATE_TYPE_LEVEL (t
);
5280 return level
<= this_level
;
5283 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5284 parameters given by current_template_args, or reuses a
5285 previously existing one, if appropriate. Returns the DECL, or an
5286 equivalent one, if it is replaced via a call to duplicate_decls.
5288 If IS_FRIEND is true, DECL is a friend declaration. */
5291 push_template_decl_real (tree decl
, bool is_friend
)
5299 int new_template_p
= 0;
5300 /* True if the template is a member template, in the sense of
5302 bool member_template_p
= false;
5304 if (decl
== error_mark_node
|| !current_template_parms
)
5305 return error_mark_node
;
5307 /* See if this is a partial specialization. */
5308 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5309 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5310 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5312 && DECL_LANG_SPECIFIC (decl
)
5313 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5314 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5316 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5320 /* For a friend, we want the context of the friend, not
5321 the type of which it is a friend. */
5322 ctx
= CP_DECL_CONTEXT (decl
);
5323 else if (CP_DECL_CONTEXT (decl
)
5324 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5325 /* In the case of a virtual function, we want the class in which
5327 ctx
= CP_DECL_CONTEXT (decl
);
5329 /* Otherwise, if we're currently defining some class, the DECL
5330 is assumed to be a member of the class. */
5331 ctx
= current_scope ();
5333 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5336 if (!DECL_CONTEXT (decl
))
5337 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5339 /* See if this is a primary template. */
5340 if (is_friend
&& ctx
5341 && uses_template_parms_level (ctx
, processing_template_decl
))
5342 /* A friend template that specifies a class context, i.e.
5343 template <typename T> friend void A<T>::f();
5346 else if (TREE_CODE (decl
) == TYPE_DECL
5347 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5350 is_primary
= template_parm_scope_p ();
5354 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5356 if (DECL_CLASS_SCOPE_P (decl
))
5357 member_template_p
= true;
5358 if (TREE_CODE (decl
) == TYPE_DECL
5359 && anon_aggrname_p (DECL_NAME (decl
)))
5361 error ("template class without a name");
5362 return error_mark_node
;
5364 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5366 if (member_template_p
)
5368 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5369 error ("member template %qD may not have virt-specifiers", decl
);
5371 if (DECL_DESTRUCTOR_P (decl
))
5375 A destructor shall not be a member template. */
5376 error ("destructor %qD declared as member template", decl
);
5377 return error_mark_node
;
5379 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5380 && (!prototype_p (TREE_TYPE (decl
))
5381 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5382 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5383 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5384 == void_list_node
)))
5386 /* [basic.stc.dynamic.allocation]
5388 An allocation function can be a function
5389 template. ... Template allocation functions shall
5390 have two or more parameters. */
5391 error ("invalid template declaration of %qD", decl
);
5392 return error_mark_node
;
5395 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5396 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5398 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5399 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5400 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5402 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5403 if (TREE_CODE (t
) == TYPE_DECL
)
5405 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5406 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5409 else if (TREE_CODE (decl
) == TYPE_DECL
5410 && TYPE_DECL_ALIAS_P (decl
))
5411 /* alias-declaration */
5412 gcc_assert (!DECL_ARTIFICIAL (decl
));
5413 else if (VAR_P (decl
))
5414 /* C++14 variable template. */;
5417 error ("template declaration of %q#D", decl
);
5418 return error_mark_node
;
5422 /* Check to see that the rules regarding the use of default
5423 arguments are not being violated. We check args for a friend
5424 functions when we know whether it's a definition, introducing
5425 declaration or re-declaration. */
5426 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5427 check_default_tmpl_args (decl
, current_template_parms
,
5428 is_primary
, is_partial
, is_friend
);
5430 /* Ensure that there are no parameter packs in the type of this
5431 declaration that have not been expanded. */
5432 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5434 /* Check each of the arguments individually to see if there are
5435 any bare parameter packs. */
5436 tree type
= TREE_TYPE (decl
);
5437 tree arg
= DECL_ARGUMENTS (decl
);
5438 tree argtype
= TYPE_ARG_TYPES (type
);
5440 while (arg
&& argtype
)
5442 if (!DECL_PACK_P (arg
)
5443 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5445 /* This is a PARM_DECL that contains unexpanded parameter
5446 packs. We have already complained about this in the
5447 check_for_bare_parameter_packs call, so just replace
5448 these types with ERROR_MARK_NODE. */
5449 TREE_TYPE (arg
) = error_mark_node
;
5450 TREE_VALUE (argtype
) = error_mark_node
;
5453 arg
= DECL_CHAIN (arg
);
5454 argtype
= TREE_CHAIN (argtype
);
5457 /* Check for bare parameter packs in the return type and the
5458 exception specifiers. */
5459 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5460 /* Errors were already issued, set return type to int
5461 as the frontend doesn't expect error_mark_node as
5463 TREE_TYPE (type
) = integer_type_node
;
5464 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5465 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5467 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5468 && TYPE_DECL_ALIAS_P (decl
))
5469 ? DECL_ORIGINAL_TYPE (decl
)
5470 : TREE_TYPE (decl
)))
5472 TREE_TYPE (decl
) = error_mark_node
;
5473 return error_mark_node
;
5477 return process_partial_specialization (decl
);
5479 args
= current_template_args ();
5482 || TREE_CODE (ctx
) == FUNCTION_DECL
5483 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5484 || (TREE_CODE (decl
) == TYPE_DECL
5485 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5486 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5488 if (DECL_LANG_SPECIFIC (decl
)
5489 && DECL_TEMPLATE_INFO (decl
)
5490 && DECL_TI_TEMPLATE (decl
))
5491 tmpl
= DECL_TI_TEMPLATE (decl
);
5492 /* If DECL is a TYPE_DECL for a class-template, then there won't
5493 be DECL_LANG_SPECIFIC. The information equivalent to
5494 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5495 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5496 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5497 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5499 /* Since a template declaration already existed for this
5500 class-type, we must be redeclaring it here. Make sure
5501 that the redeclaration is valid. */
5502 redeclare_class_template (TREE_TYPE (decl
),
5503 current_template_parms
,
5504 current_template_constraints ());
5505 /* We don't need to create a new TEMPLATE_DECL; just use the
5506 one we already had. */
5507 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5511 tmpl
= build_template_decl (decl
, current_template_parms
,
5515 if (DECL_LANG_SPECIFIC (decl
)
5516 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5518 /* A specialization of a member template of a template
5520 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5521 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5522 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5528 tree a
, t
, current
, parms
;
5530 tree tinfo
= get_template_info (decl
);
5534 error ("template definition of non-template %q#D", decl
);
5535 return error_mark_node
;
5538 tmpl
= TI_TEMPLATE (tinfo
);
5540 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5541 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5542 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5543 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5547 /* The declaration is a specialization of a member
5548 template, declared outside the class. Therefore, the
5549 innermost template arguments will be NULL, so we
5550 replace them with the arguments determined by the
5551 earlier call to check_explicit_specialization. */
5552 args
= DECL_TI_ARGS (decl
);
5555 = build_template_decl (decl
, current_template_parms
,
5557 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5558 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5559 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5560 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5561 DECL_TEMPLATE_INFO (new_tmpl
)
5562 = build_template_info (tmpl
, args
);
5564 register_specialization (new_tmpl
,
5565 most_general_template (tmpl
),
5571 /* Make sure the template headers we got make sense. */
5573 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5574 i
= TMPL_PARMS_DEPTH (parms
);
5575 if (TMPL_ARGS_DEPTH (args
) != i
)
5577 error ("expected %d levels of template parms for %q#D, got %d",
5578 i
, decl
, TMPL_ARGS_DEPTH (args
));
5579 DECL_INTERFACE_KNOWN (decl
) = 1;
5580 return error_mark_node
;
5583 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5585 a
= TMPL_ARGS_LEVEL (args
, i
);
5586 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5588 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5590 if (current
== decl
)
5591 error ("got %d template parameters for %q#D",
5592 TREE_VEC_LENGTH (a
), decl
);
5594 error ("got %d template parameters for %q#T",
5595 TREE_VEC_LENGTH (a
), current
);
5596 error (" but %d required", TREE_VEC_LENGTH (t
));
5597 /* Avoid crash in import_export_decl. */
5598 DECL_INTERFACE_KNOWN (decl
) = 1;
5599 return error_mark_node
;
5602 if (current
== decl
)
5604 else if (current
== NULL_TREE
)
5605 /* Can happen in erroneous input. */
5608 current
= get_containing_scope (current
);
5611 /* Check that the parms are used in the appropriate qualifying scopes
5612 in the declarator. */
5613 if (!comp_template_args
5615 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5617 error ("template arguments to %qD do not match original "
5618 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5619 if (!uses_template_parms (TI_ARGS (tinfo
)))
5620 inform (input_location
, "use %<template<>%> for"
5621 " an explicit specialization");
5622 /* Avoid crash in import_export_decl. */
5623 DECL_INTERFACE_KNOWN (decl
) = 1;
5624 return error_mark_node
;
5628 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5629 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5631 /* Push template declarations for global functions and types. Note
5632 that we do not try to push a global template friend declared in a
5633 template class; such a thing may well depend on the template
5634 parameters of the class. */
5635 if (new_template_p
&& !ctx
5636 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5638 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5639 if (tmpl
== error_mark_node
)
5640 return error_mark_node
;
5642 /* Hide template friend classes that haven't been declared yet. */
5643 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5645 DECL_ANTICIPATED (tmpl
) = 1;
5646 DECL_FRIEND_P (tmpl
) = 1;
5652 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5654 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5656 /* Give template template parms a DECL_CONTEXT of the template
5657 for which they are a parameter. */
5658 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5659 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5661 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5662 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5663 DECL_CONTEXT (parm
) = tmpl
;
5666 if (TREE_CODE (decl
) == TYPE_DECL
5667 && TYPE_DECL_ALIAS_P (decl
)
5668 && complex_alias_template_p (tmpl
))
5669 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5672 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5673 back to its most general template. If TMPL is a specialization,
5674 ARGS may only have the innermost set of arguments. Add the missing
5675 argument levels if necessary. */
5676 if (DECL_TEMPLATE_INFO (tmpl
))
5677 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5679 info
= build_template_info (tmpl
, args
);
5681 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5682 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5686 retrofit_lang_decl (decl
);
5687 if (DECL_LANG_SPECIFIC (decl
))
5688 DECL_TEMPLATE_INFO (decl
) = info
;
5691 if (flag_implicit_templates
5693 && TREE_PUBLIC (decl
)
5694 && VAR_OR_FUNCTION_DECL_P (decl
))
5695 /* Set DECL_COMDAT on template instantiations; if we force
5696 them to be emitted by explicit instantiation or -frepo,
5697 mark_needed will tell cgraph to do the right thing. */
5698 DECL_COMDAT (decl
) = true;
5700 return DECL_TEMPLATE_RESULT (tmpl
);
5704 push_template_decl (tree decl
)
5706 return push_template_decl_real (decl
, false);
5709 /* FN is an inheriting constructor that inherits from the constructor
5710 template INHERITED; turn FN into a constructor template with a matching
5714 add_inherited_template_parms (tree fn
, tree inherited
)
5717 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5718 inner_parms
= copy_node (inner_parms
);
5720 = tree_cons (size_int (processing_template_decl
+ 1),
5721 inner_parms
, current_template_parms
);
5722 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5723 tree args
= template_parms_to_args (parms
);
5724 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5725 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5726 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5727 DECL_ARTIFICIAL (tmpl
) = true;
5728 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5732 /* Called when a class template TYPE is redeclared with the indicated
5733 template PARMS, e.g.:
5735 template <class T> struct S;
5736 template <class T> struct S {}; */
5739 redeclare_class_template (tree type
, tree parms
, tree cons
)
5745 if (!TYPE_TEMPLATE_INFO (type
))
5747 error ("%qT is not a template type", type
);
5751 tmpl
= TYPE_TI_TEMPLATE (type
);
5752 if (!PRIMARY_TEMPLATE_P (tmpl
))
5753 /* The type is nested in some template class. Nothing to worry
5754 about here; there are no new template parameters for the nested
5760 error ("template specifiers not specified in declaration of %qD",
5765 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5766 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5768 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5770 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5771 "redeclared with %d template parameter",
5772 "redeclared with %d template parameters",
5773 TREE_VEC_LENGTH (parms
));
5774 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5775 "previous declaration %qD used %d template parameter",
5776 "previous declaration %qD used %d template parameters",
5777 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5781 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5788 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5789 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5792 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5793 if (error_operand_p (tmpl_parm
))
5796 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5797 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5798 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5800 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5802 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5803 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5804 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5805 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5806 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5807 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5808 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5809 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5810 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5812 error ("template parameter %q+#D", tmpl_parm
);
5813 error ("redeclared here as %q#D", parm
);
5817 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5819 /* We have in [temp.param]:
5821 A template-parameter may not be given default arguments
5822 by two different declarations in the same scope. */
5823 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5824 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5825 "original definition appeared here");
5829 if (parm_default
!= NULL_TREE
)
5830 /* Update the previous template parameters (which are the ones
5831 that will really count) with the new default value. */
5832 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5833 else if (tmpl_default
!= NULL_TREE
)
5834 /* Update the new parameters, too; they'll be used as the
5835 parameters for any members. */
5836 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5838 /* Give each template template parm in this redeclaration a
5839 DECL_CONTEXT of the template for which they are a parameter. */
5840 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5842 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5843 DECL_CONTEXT (parm
) = tmpl
;
5846 if (TREE_CODE (parm
) == TYPE_DECL
)
5847 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5850 // Cannot redeclare a class template with a different set of constraints.
5851 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5853 error_at (input_location
, "redeclaration %q#D with different "
5854 "constraints", tmpl
);
5855 inform (DECL_SOURCE_LOCATION (tmpl
),
5856 "original declaration appeared here");
5862 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5863 to be used when the caller has already checked
5864 (processing_template_decl
5865 && !instantiation_dependent_expression_p (expr)
5866 && potential_constant_expression (expr))
5867 and cleared processing_template_decl. */
5870 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5872 return tsubst_copy_and_build (expr
,
5875 /*in_decl=*/NULL_TREE
,
5876 /*function_p=*/false,
5877 /*integral_constant_expression_p=*/true);
5880 /* Simplify EXPR if it is a non-dependent expression. Returns the
5881 (possibly simplified) expression. */
5884 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5886 if (expr
== NULL_TREE
)
5889 /* If we're in a template, but EXPR isn't value dependent, simplify
5890 it. We're supposed to treat:
5892 template <typename T> void f(T[1 + 1]);
5893 template <typename T> void f(T[2]);
5895 as two declarations of the same function, for example. */
5896 if (processing_template_decl
5897 && is_nondependent_constant_expression (expr
))
5899 processing_template_decl_sentinel s
;
5900 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5906 instantiate_non_dependent_expr (tree expr
)
5908 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5911 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5912 an uninstantiated expression. */
5915 instantiate_non_dependent_or_null (tree expr
)
5917 if (expr
== NULL_TREE
)
5919 if (processing_template_decl
)
5921 if (!is_nondependent_constant_expression (expr
))
5925 processing_template_decl_sentinel s
;
5926 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5932 /* True iff T is a specialization of a variable template. */
5935 variable_template_specialization_p (tree t
)
5937 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5939 tree tmpl
= DECL_TI_TEMPLATE (t
);
5940 return variable_template_p (tmpl
);
5943 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5944 template declaration, or a TYPE_DECL for an alias declaration. */
5947 alias_type_or_template_p (tree t
)
5951 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5954 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5955 || DECL_ALIAS_TEMPLATE_P (t
));
5958 /* Return TRUE iff T is a specialization of an alias template. */
5961 alias_template_specialization_p (const_tree t
)
5963 /* It's an alias template specialization if it's an alias and its
5964 TYPE_NAME is a specialization of a primary template. */
5965 if (TYPE_ALIAS_P (t
))
5966 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5967 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5972 /* An alias template is complex from a SFINAE perspective if a template-id
5973 using that alias can be ill-formed when the expansion is not, as with
5974 the void_t template. We determine this by checking whether the
5975 expansion for the alias template uses all its template parameters. */
5977 struct uses_all_template_parms_data
5984 uses_all_template_parms_r (tree t
, void *data_
)
5986 struct uses_all_template_parms_data
&data
5987 = *(struct uses_all_template_parms_data
*)data_
;
5988 tree idx
= get_template_parm_index (t
);
5990 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5991 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5996 complex_alias_template_p (const_tree tmpl
)
5998 struct uses_all_template_parms_data data
;
5999 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6000 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6001 data
.level
= TMPL_PARMS_DEPTH (parms
);
6002 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6003 data
.seen
= XALLOCAVEC (bool, len
);
6004 for (int i
= 0; i
< len
; ++i
)
6005 data
.seen
[i
] = false;
6007 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6008 for (int i
= 0; i
< len
; ++i
)
6014 /* Return TRUE iff T is a specialization of a complex alias template with
6015 dependent template-arguments. */
6018 dependent_alias_template_spec_p (const_tree t
)
6020 if (!alias_template_specialization_p (t
))
6023 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6024 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6027 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6028 if (!any_dependent_template_arguments_p (args
))
6034 /* Return the number of innermost template parameters in TMPL. */
6037 num_innermost_template_parms (tree tmpl
)
6039 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6040 return TREE_VEC_LENGTH (parms
);
6043 /* Return either TMPL or another template that it is equivalent to under DR
6044 1286: An alias that just changes the name of a template is equivalent to
6045 the other template. */
6048 get_underlying_template (tree tmpl
)
6050 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6051 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6053 /* Determine if the alias is equivalent to an underlying template. */
6054 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6055 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6059 tree underlying
= TI_TEMPLATE (tinfo
);
6060 if (!PRIMARY_TEMPLATE_P (underlying
)
6061 || (num_innermost_template_parms (tmpl
)
6062 != num_innermost_template_parms (underlying
)))
6065 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6066 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6067 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6070 /* Alias is equivalent. Strip it and repeat. */
6077 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6078 must be a reference-to-function or a pointer-to-function type, as specified
6079 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6080 and check that the resulting function has external linkage. */
6083 convert_nontype_argument_function (tree type
, tree expr
,
6084 tsubst_flags_t complain
)
6088 linkage_kind linkage
;
6090 fn
= instantiate_type (type
, fns
, tf_none
);
6091 if (fn
== error_mark_node
)
6092 return error_mark_node
;
6094 if (value_dependent_expression_p (fn
))
6097 fn_no_ptr
= strip_fnptr_conv (fn
);
6098 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6099 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6100 if (BASELINK_P (fn_no_ptr
))
6101 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6103 /* [temp.arg.nontype]/1
6105 A template-argument for a non-type, non-template template-parameter
6108 -- the address of an object or function with external [C++11: or
6109 internal] linkage. */
6111 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6113 if (complain
& tf_error
)
6115 error ("%qE is not a valid template argument for type %qT",
6117 if (TYPE_PTR_P (type
))
6118 inform (input_location
, "it must be the address of a function "
6119 "with external linkage");
6121 inform (input_location
, "it must be the name of a function with "
6122 "external linkage");
6127 linkage
= decl_linkage (fn_no_ptr
);
6128 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6130 if (complain
& tf_error
)
6132 if (cxx_dialect
>= cxx11
)
6133 error ("%qE is not a valid template argument for type %qT "
6134 "because %qD has no linkage",
6135 expr
, type
, fn_no_ptr
);
6137 error ("%qE is not a valid template argument for type %qT "
6138 "because %qD does not have external linkage",
6139 expr
, type
, fn_no_ptr
);
6145 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6146 fn
= build_address (fn
);
6147 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6148 fn
= build_nop (type
, fn
);
6153 /* Subroutine of convert_nontype_argument.
6154 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6155 Emit an error otherwise. */
6158 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6159 tsubst_flags_t complain
)
6161 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6162 tree orig_expr
= expr
;
6164 if (null_ptr_cst_p (expr
))
6166 if (TREE_CODE (expr
) == PTRMEM_CST
6167 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6168 PTRMEM_CST_CLASS (expr
)))
6170 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6172 if (processing_template_decl
6173 && TREE_CODE (expr
) == ADDR_EXPR
6174 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6176 if (complain
& tf_error
)
6178 error_at (loc
, "%qE is not a valid template argument for type %qT",
6180 if (TREE_CODE (expr
) != PTRMEM_CST
)
6181 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6183 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6188 /* Returns TRUE iff the address of OP is value-dependent.
6190 14.6.2.4 [temp.dep.temp]:
6191 A non-integral non-type template-argument is dependent if its type is
6192 dependent or it has either of the following forms
6195 and contains a nested-name-specifier which specifies a class-name that
6196 names a dependent type.
6198 We generalize this to just say that the address of a member of a
6199 dependent class is value-dependent; the above doesn't cover the
6200 address of a static data member named with an unqualified-id. */
6203 has_value_dependent_address (tree op
)
6205 /* We could use get_inner_reference here, but there's no need;
6206 this is only relevant for template non-type arguments, which
6207 can only be expressed as &id-expression. */
6210 tree ctx
= CP_DECL_CONTEXT (op
);
6211 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6218 /* The next set of functions are used for providing helpful explanatory
6219 diagnostics for failed overload resolution. Their messages should be
6220 indented by two spaces for consistency with the messages in
6224 unify_success (bool /*explain_p*/)
6229 /* Other failure functions should call this one, to provide a single function
6230 for setting a breakpoint on. */
6233 unify_invalid (bool /*explain_p*/)
6239 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6242 inform (input_location
,
6243 " couldn't deduce template parameter %qD", parm
);
6244 return unify_invalid (explain_p
);
6248 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6251 inform (input_location
,
6252 " types %qT and %qT have incompatible cv-qualifiers",
6254 return unify_invalid (explain_p
);
6258 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6261 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6262 return unify_invalid (explain_p
);
6266 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6269 inform (input_location
,
6270 " template parameter %qD is not a parameter pack, but "
6273 return unify_invalid (explain_p
);
6277 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6280 inform (input_location
,
6281 " template argument %qE does not match "
6282 "pointer-to-member constant %qE",
6284 return unify_invalid (explain_p
);
6288 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6291 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6292 return unify_invalid (explain_p
);
6296 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6299 inform (input_location
,
6300 " inconsistent parameter pack deduction with %qT and %qT",
6302 return unify_invalid (explain_p
);
6306 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6311 inform (input_location
,
6312 " deduced conflicting types for parameter %qT (%qT and %qT)",
6313 parm
, first
, second
);
6315 inform (input_location
,
6316 " deduced conflicting values for non-type parameter "
6317 "%qE (%qE and %qE)", parm
, first
, second
);
6319 return unify_invalid (explain_p
);
6323 unify_vla_arg (bool explain_p
, tree arg
)
6326 inform (input_location
,
6327 " variable-sized array type %qT is not "
6328 "a valid template argument",
6330 return unify_invalid (explain_p
);
6334 unify_method_type_error (bool explain_p
, tree arg
)
6337 inform (input_location
,
6338 " member function type %qT is not a valid template argument",
6340 return unify_invalid (explain_p
);
6344 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6349 inform_n (input_location
, wanted
,
6350 " candidate expects at least %d argument, %d provided",
6351 " candidate expects at least %d arguments, %d provided",
6354 inform_n (input_location
, wanted
,
6355 " candidate expects %d argument, %d provided",
6356 " candidate expects %d arguments, %d provided",
6359 return unify_invalid (explain_p
);
6363 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6365 return unify_arity (explain_p
, have
, wanted
);
6369 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6370 bool least_p
= false)
6372 return unify_arity (explain_p
, have
, wanted
, least_p
);
6376 unify_arg_conversion (bool explain_p
, tree to_type
,
6377 tree from_type
, tree arg
)
6380 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6381 " cannot convert %qE (type %qT) to type %qT",
6382 arg
, from_type
, to_type
);
6383 return unify_invalid (explain_p
);
6387 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6388 tree parm
, tree arg
)
6393 case tbr_ambiguous_baseclass
:
6394 inform (input_location
, " %qT is an ambiguous base class of %qT",
6398 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6401 return unify_invalid (explain_p
);
6405 unify_inconsistent_template_template_parameters (bool explain_p
)
6408 inform (input_location
,
6409 " template parameters of a template template argument are "
6410 "inconsistent with other deduced template arguments");
6411 return unify_invalid (explain_p
);
6415 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6418 inform (input_location
,
6419 " can't deduce a template for %qT from non-template type %qT",
6421 return unify_invalid (explain_p
);
6425 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6428 inform (input_location
,
6429 " template argument %qE does not match %qE", arg
, parm
);
6430 return unify_invalid (explain_p
);
6433 /* Attempt to convert the non-type template parameter EXPR to the
6434 indicated TYPE. If the conversion is successful, return the
6435 converted value. If the conversion is unsuccessful, return
6436 NULL_TREE if we issued an error message, or error_mark_node if we
6437 did not. We issue error messages for out-and-out bad template
6438 parameters, but not simply because the conversion failed, since we
6439 might be just trying to do argument deduction. Both TYPE and EXPR
6440 must be non-dependent.
6442 The conversion follows the special rules described in
6443 [temp.arg.nontype], and it is much more strict than an implicit
6446 This function is called twice for each template argument (see
6447 lookup_template_class for a more accurate description of this
6448 problem). This means that we need to handle expressions which
6449 are not valid in a C++ source, but can be created from the
6450 first call (for instance, casts to perform conversions). These
6451 hacks can go away after we fix the double coercion problem. */
6454 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6457 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6458 tree orig_expr
= expr
;
6460 /* Detect immediately string literals as invalid non-type argument.
6461 This special-case is not needed for correctness (we would easily
6462 catch this later), but only to provide better diagnostic for this
6463 common user mistake. As suggested by DR 100, we do not mention
6464 linkage issues in the diagnostic as this is not the point. */
6465 /* FIXME we're making this OK. */
6466 if (TREE_CODE (expr
) == STRING_CST
)
6468 if (complain
& tf_error
)
6469 error ("%qE is not a valid template argument for type %qT "
6470 "because string literals can never be used in this context",
6475 /* Add the ADDR_EXPR now for the benefit of
6476 value_dependent_expression_p. */
6477 if (TYPE_PTROBV_P (type
)
6478 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6480 expr
= decay_conversion (expr
, complain
);
6481 if (expr
== error_mark_node
)
6482 return error_mark_node
;
6485 /* If we are in a template, EXPR may be non-dependent, but still
6486 have a syntactic, rather than semantic, form. For example, EXPR
6487 might be a SCOPE_REF, rather than the VAR_DECL to which the
6488 SCOPE_REF refers. Preserving the qualifying scope is necessary
6489 so that access checking can be performed when the template is
6490 instantiated -- but here we need the resolved form so that we can
6491 convert the argument. */
6492 bool non_dep
= false;
6493 if (TYPE_REF_OBJ_P (type
)
6494 && has_value_dependent_address (expr
))
6495 /* If we want the address and it's value-dependent, don't fold. */;
6496 else if (processing_template_decl
6497 && is_nondependent_constant_expression (expr
))
6499 if (error_operand_p (expr
))
6500 return error_mark_node
;
6501 expr_type
= TREE_TYPE (expr
);
6503 /* If the argument is non-dependent, perform any conversions in
6504 non-dependent context as well. */
6505 processing_template_decl_sentinel
s (non_dep
);
6507 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6509 if (value_dependent_expression_p (expr
))
6510 expr
= canonicalize_expr_argument (expr
, complain
);
6512 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6513 to a non-type argument of "nullptr". */
6514 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6515 expr
= fold_simple (convert (type
, expr
));
6517 /* In C++11, integral or enumeration non-type template arguments can be
6518 arbitrary constant expressions. Pointer and pointer to
6519 member arguments can be general constant expressions that evaluate
6520 to a null value, but otherwise still need to be of a specific form. */
6521 if (cxx_dialect
>= cxx11
)
6523 if (TREE_CODE (expr
) == PTRMEM_CST
)
6524 /* A PTRMEM_CST is already constant, and a valid template
6525 argument for a parameter of pointer to member type, we just want
6526 to leave it in that form rather than lower it to a
6528 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6529 || cxx_dialect
>= cxx17
)
6531 /* C++17: A template-argument for a non-type template-parameter shall
6532 be a converted constant expression (8.20) of the type of the
6533 template-parameter. */
6534 expr
= build_converted_constant_expr (type
, expr
, complain
);
6535 if (expr
== error_mark_node
)
6536 return error_mark_node
;
6537 expr
= maybe_constant_value (expr
);
6538 expr
= convert_from_reference (expr
);
6540 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6542 tree folded
= maybe_constant_value (expr
);
6543 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6544 : null_member_pointer_value_p (folded
))
6549 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6550 expr
= mark_lvalue_use (expr
);
6552 expr
= mark_rvalue_use (expr
);
6554 /* HACK: Due to double coercion, we can get a
6555 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6556 which is the tree that we built on the first call (see
6557 below when coercing to reference to object or to reference to
6558 function). We just strip everything and get to the arg.
6559 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6561 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6563 tree probe_type
, probe
= expr
;
6564 if (REFERENCE_REF_P (probe
))
6565 probe
= TREE_OPERAND (probe
, 0);
6566 probe_type
= TREE_TYPE (probe
);
6567 if (TREE_CODE (probe
) == NOP_EXPR
)
6569 /* ??? Maybe we could use convert_from_reference here, but we
6570 would need to relax its constraints because the NOP_EXPR
6571 could actually change the type to something more cv-qualified,
6572 and this is not folded by convert_from_reference. */
6573 tree addr
= TREE_OPERAND (probe
, 0);
6574 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6575 && TREE_CODE (addr
) == ADDR_EXPR
6576 && TYPE_PTR_P (TREE_TYPE (addr
))
6577 && (same_type_ignoring_top_level_qualifiers_p
6578 (TREE_TYPE (probe_type
),
6579 TREE_TYPE (TREE_TYPE (addr
)))))
6581 expr
= TREE_OPERAND (addr
, 0);
6582 expr_type
= TREE_TYPE (probe_type
);
6587 /* [temp.arg.nontype]/5, bullet 1
6589 For a non-type template-parameter of integral or enumeration type,
6590 integral promotions (_conv.prom_) and integral conversions
6591 (_conv.integral_) are applied. */
6592 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6594 if (cxx_dialect
< cxx11
)
6596 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6597 t
= maybe_constant_value (t
);
6598 if (t
!= error_mark_node
)
6602 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6603 return error_mark_node
;
6605 /* Notice that there are constant expressions like '4 % 0' which
6606 do not fold into integer constants. */
6607 if (TREE_CODE (expr
) != INTEGER_CST
6608 && !value_dependent_expression_p (expr
))
6610 if (complain
& tf_error
)
6612 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6613 if (!require_potential_constant_expression (expr
))
6614 expr
= error_mark_node
;
6616 expr
= cxx_constant_value (expr
);
6617 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6618 inform (loc
, "in template argument for type %qT ", type
);
6619 if (expr
== error_mark_node
)
6621 /* else cxx_constant_value complained but gave us
6622 a real constant, so go ahead. */
6623 if (TREE_CODE (expr
) != INTEGER_CST
)
6625 /* Some assemble time constant expressions like
6626 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6627 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6628 as we can emit them into .rodata initializers of
6629 variables, yet they can't fold into an INTEGER_CST at
6630 compile time. Refuse them here. */
6631 gcc_checking_assert (reduced_constant_expression_p (expr
));
6632 error_at (loc
, "template argument %qE for type %qT not "
6633 "a constant integer", expr
, type
);
6641 /* Avoid typedef problems. */
6642 if (TREE_TYPE (expr
) != type
)
6643 expr
= fold_convert (type
, expr
);
6645 /* [temp.arg.nontype]/5, bullet 2
6647 For a non-type template-parameter of type pointer to object,
6648 qualification conversions (_conv.qual_) and the array-to-pointer
6649 conversion (_conv.array_) are applied. */
6650 else if (TYPE_PTROBV_P (type
))
6652 tree decayed
= expr
;
6654 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6655 decay_conversion or an explicit cast. If it's a problematic cast,
6656 we'll complain about it below. */
6657 if (TREE_CODE (expr
) == NOP_EXPR
)
6661 if (TREE_CODE (probe
) == ADDR_EXPR
6662 && TYPE_PTR_P (TREE_TYPE (probe
)))
6665 expr_type
= TREE_TYPE (expr
);
6669 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6671 A template-argument for a non-type, non-template template-parameter
6672 shall be one of: [...]
6674 -- the name of a non-type template-parameter;
6675 -- the address of an object or function with external linkage, [...]
6676 expressed as "& id-expression" where the & is optional if the name
6677 refers to a function or array, or if the corresponding
6678 template-parameter is a reference.
6680 Here, we do not care about functions, as they are invalid anyway
6681 for a parameter of type pointer-to-object. */
6683 if (value_dependent_expression_p (expr
))
6684 /* Non-type template parameters are OK. */
6686 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6687 /* Null pointer values are OK in C++11. */;
6688 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6692 if (complain
& tf_error
)
6693 error ("%qD is not a valid template argument "
6694 "because %qD is a variable, not the address of "
6695 "a variable", orig_expr
, expr
);
6698 if (POINTER_TYPE_P (expr_type
))
6700 if (complain
& tf_error
)
6701 error ("%qE is not a valid template argument for %qT "
6702 "because it is not the address of a variable",
6706 /* Other values, like integer constants, might be valid
6707 non-type arguments of some other type. */
6708 return error_mark_node
;
6712 tree decl
= TREE_OPERAND (expr
, 0);
6716 if (complain
& tf_error
)
6717 error ("%qE is not a valid template argument of type %qT "
6718 "because %qE is not a variable", orig_expr
, type
, decl
);
6721 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6723 if (complain
& tf_error
)
6724 error ("%qE is not a valid template argument of type %qT "
6725 "because %qD does not have external linkage",
6726 orig_expr
, type
, decl
);
6729 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6730 && decl_linkage (decl
) == lk_none
)
6732 if (complain
& tf_error
)
6733 error ("%qE is not a valid template argument of type %qT "
6734 "because %qD has no linkage", orig_expr
, type
, decl
);
6737 /* C++17: For a non-type template-parameter of reference or pointer
6738 type, the value of the constant expression shall not refer to (or
6739 for a pointer type, shall not be the address of):
6740 * a subobject (4.5),
6741 * a temporary object (15.2),
6742 * a string literal (5.13.5),
6743 * the result of a typeid expression (8.2.8), or
6744 * a predefined __func__ variable (11.4.1). */
6745 else if (DECL_ARTIFICIAL (decl
))
6747 if (complain
& tf_error
)
6748 error ("the address of %qD is not a valid template argument",
6752 else if (!same_type_ignoring_top_level_qualifiers_p
6753 (strip_array_types (TREE_TYPE (type
)),
6754 strip_array_types (TREE_TYPE (decl
))))
6756 if (complain
& tf_error
)
6757 error ("the address of the %qT subobject of %qD is not a "
6758 "valid template argument", TREE_TYPE (type
), decl
);
6761 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6763 if (complain
& tf_error
)
6764 error ("the address of %qD is not a valid template argument "
6765 "because it does not have static storage duration",
6773 expr
= perform_qualification_conversions (type
, expr
);
6774 if (expr
== error_mark_node
)
6775 return error_mark_node
;
6777 /* [temp.arg.nontype]/5, bullet 3
6779 For a non-type template-parameter of type reference to object, no
6780 conversions apply. The type referred to by the reference may be more
6781 cv-qualified than the (otherwise identical) type of the
6782 template-argument. The template-parameter is bound directly to the
6783 template-argument, which must be an lvalue. */
6784 else if (TYPE_REF_OBJ_P (type
))
6786 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6788 return error_mark_node
;
6790 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6792 if (complain
& tf_error
)
6793 error ("%qE is not a valid template argument for type %qT "
6794 "because of conflicts in cv-qualification", expr
, type
);
6798 if (!lvalue_p (expr
))
6800 if (complain
& tf_error
)
6801 error ("%qE is not a valid template argument for type %qT "
6802 "because it is not an lvalue", expr
, type
);
6806 /* [temp.arg.nontype]/1
6808 A template-argument for a non-type, non-template template-parameter
6809 shall be one of: [...]
6811 -- the address of an object or function with external linkage. */
6812 if (INDIRECT_REF_P (expr
)
6813 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6815 expr
= TREE_OPERAND (expr
, 0);
6818 if (complain
& tf_error
)
6819 error ("%q#D is not a valid template argument for type %qT "
6820 "because a reference variable does not have a constant "
6821 "address", expr
, type
);
6826 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6827 && value_dependent_expression_p (expr
))
6828 /* OK, dependent reference. We don't want to ask whether a DECL is
6829 itself value-dependent, since what we want here is its address. */;
6834 if (complain
& tf_error
)
6835 error ("%qE is not a valid template argument for type %qT "
6836 "because it is not an object with linkage",
6841 /* DR 1155 allows internal linkage in C++11 and up. */
6842 linkage_kind linkage
= decl_linkage (expr
);
6843 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6845 if (complain
& tf_error
)
6846 error ("%qE is not a valid template argument for type %qT "
6847 "because object %qD does not have linkage",
6852 expr
= build_address (expr
);
6855 if (!same_type_p (type
, TREE_TYPE (expr
)))
6856 expr
= build_nop (type
, expr
);
6858 /* [temp.arg.nontype]/5, bullet 4
6860 For a non-type template-parameter of type pointer to function, only
6861 the function-to-pointer conversion (_conv.func_) is applied. If the
6862 template-argument represents a set of overloaded functions (or a
6863 pointer to such), the matching function is selected from the set
6865 else if (TYPE_PTRFN_P (type
))
6867 /* If the argument is a template-id, we might not have enough
6868 context information to decay the pointer. */
6869 if (!type_unknown_p (expr_type
))
6871 expr
= decay_conversion (expr
, complain
);
6872 if (expr
== error_mark_node
)
6873 return error_mark_node
;
6876 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6877 /* Null pointer values are OK in C++11. */
6878 return perform_qualification_conversions (type
, expr
);
6880 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6881 if (!expr
|| expr
== error_mark_node
)
6884 /* [temp.arg.nontype]/5, bullet 5
6886 For a non-type template-parameter of type reference to function, no
6887 conversions apply. If the template-argument represents a set of
6888 overloaded functions, the matching function is selected from the set
6890 else if (TYPE_REFFN_P (type
))
6892 if (TREE_CODE (expr
) == ADDR_EXPR
)
6894 if (complain
& tf_error
)
6896 error ("%qE is not a valid template argument for type %qT "
6897 "because it is a pointer", expr
, type
);
6898 inform (input_location
, "try using %qE instead",
6899 TREE_OPERAND (expr
, 0));
6904 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6905 if (!expr
|| expr
== error_mark_node
)
6908 /* [temp.arg.nontype]/5, bullet 6
6910 For a non-type template-parameter of type pointer to member function,
6911 no conversions apply. If the template-argument represents a set of
6912 overloaded member functions, the matching member function is selected
6913 from the set (_over.over_). */
6914 else if (TYPE_PTRMEMFUNC_P (type
))
6916 expr
= instantiate_type (type
, expr
, tf_none
);
6917 if (expr
== error_mark_node
)
6918 return error_mark_node
;
6920 /* [temp.arg.nontype] bullet 1 says the pointer to member
6921 expression must be a pointer-to-member constant. */
6922 if (!value_dependent_expression_p (expr
)
6923 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6926 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6927 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6928 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6929 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6931 /* [temp.arg.nontype]/5, bullet 7
6933 For a non-type template-parameter of type pointer to data member,
6934 qualification conversions (_conv.qual_) are applied. */
6935 else if (TYPE_PTRDATAMEM_P (type
))
6937 /* [temp.arg.nontype] bullet 1 says the pointer to member
6938 expression must be a pointer-to-member constant. */
6939 if (!value_dependent_expression_p (expr
)
6940 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6943 expr
= perform_qualification_conversions (type
, expr
);
6944 if (expr
== error_mark_node
)
6947 else if (NULLPTR_TYPE_P (type
))
6949 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6951 if (complain
& tf_error
)
6952 error ("%qE is not a valid template argument for type %qT "
6953 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6958 /* A template non-type parameter must be one of the above. */
6962 /* Sanity check: did we actually convert the argument to the
6964 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6965 (type
, TREE_TYPE (expr
)));
6966 return convert_from_reference (expr
);
6969 /* Subroutine of coerce_template_template_parms, which returns 1 if
6970 PARM_PARM and ARG_PARM match using the rule for the template
6971 parameters of template template parameters. Both PARM and ARG are
6972 template parameters; the rest of the arguments are the same as for
6973 coerce_template_template_parms.
6976 coerce_template_template_parm (tree parm
,
6978 tsubst_flags_t complain
,
6982 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6983 || parm
== NULL_TREE
|| error_operand_p (parm
))
6986 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6989 switch (TREE_CODE (parm
))
6992 /* We encounter instantiations of templates like
6993 template <template <template <class> class> class TT>
6996 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6997 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6999 if (!coerce_template_template_parms
7000 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7006 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7007 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7008 /* Argument is a parameter pack but parameter is not. */
7013 /* The tsubst call is used to handle cases such as
7015 template <int> class C {};
7016 template <class T, template <T> class TT> class D {};
7019 i.e. the parameter list of TT depends on earlier parameters. */
7020 if (!uses_template_parms (TREE_TYPE (arg
)))
7022 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7023 if (!uses_template_parms (t
)
7024 && !same_type_p (t
, TREE_TYPE (arg
)))
7028 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7029 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7030 /* Argument is a parameter pack but parameter is not. */
7042 /* Coerce template argument list ARGLIST for use with template
7043 template-parameter TEMPL. */
7046 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7047 tsubst_flags_t complain
)
7049 /* Consider an example where a template template parameter declared as
7051 template <class T, class U = std::allocator<T> > class TT
7053 The template parameter level of T and U are one level larger than
7054 of TT. To proper process the default argument of U, say when an
7055 instantiation `TT<int>' is seen, we need to build the full
7056 arguments containing {int} as the innermost level. Outer levels,
7057 available when not appearing as default template argument, can be
7058 obtained from the arguments of the enclosing template.
7060 Suppose that TT is later substituted with std::vector. The above
7061 instantiation is `TT<int, std::allocator<T> >' with TT at
7062 level 1, and T at level 2, while the template arguments at level 1
7063 becomes {std::vector} and the inner level 2 is {int}. */
7065 tree outer
= DECL_CONTEXT (templ
);
7068 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7069 /* We want arguments for the partial specialization, not arguments for
7070 the primary template. */
7071 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7073 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7075 else if (current_template_parms
)
7077 /* This is an argument of the current template, so we haven't set
7078 DECL_CONTEXT yet. */
7079 tree relevant_template_parms
;
7081 /* Parameter levels that are greater than the level of the given
7082 template template parm are irrelevant. */
7083 relevant_template_parms
= current_template_parms
;
7084 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7085 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7086 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7088 outer
= template_parms_to_args (relevant_template_parms
);
7092 arglist
= add_to_template_args (outer
, arglist
);
7094 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7095 return coerce_template_parms (parmlist
, arglist
, templ
,
7097 /*require_all_args=*/true,
7098 /*use_default_args=*/true);
7101 /* A cache of template template parameters with match-all default
7103 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7105 store_defaulted_ttp (tree v
, tree t
)
7107 if (!defaulted_ttp_cache
)
7108 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7109 defaulted_ttp_cache
->put (v
, t
);
7112 lookup_defaulted_ttp (tree v
)
7114 if (defaulted_ttp_cache
)
7115 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7120 /* T is a bound template template-parameter. Copy its arguments into default
7121 arguments of the template template-parameter's template parameters. */
7124 add_defaults_to_ttp (tree otmpl
)
7126 if (tree c
= lookup_defaulted_ttp (otmpl
))
7129 tree ntmpl
= copy_node (otmpl
);
7131 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7132 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7133 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7134 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7135 TYPE_NAME (ntype
) = ntmpl
;
7136 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7138 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7139 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7140 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7141 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7143 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7144 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7145 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7146 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7147 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7149 tree o
= TREE_VEC_ELT (vec
, i
);
7150 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7152 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7153 TREE_PURPOSE (n
) = any_targ_node
;
7157 store_defaulted_ttp (otmpl
, ntmpl
);
7161 /* ARG is a bound potential template template-argument, and PARGS is a list
7162 of arguments for the corresponding template template-parameter. Adjust
7163 PARGS as appropriate for application to ARG's template, and if ARG is a
7164 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7165 arguments to the template template parameter. */
7168 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7170 ++processing_template_decl
;
7171 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7172 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7174 /* When comparing two template template-parameters in partial ordering,
7175 rewrite the one currently being used as an argument to have default
7176 arguments for all parameters. */
7177 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7178 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7179 if (pargs
!= error_mark_node
)
7180 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7181 TYPE_TI_ARGS (arg
));
7186 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7187 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7188 /*require_all*/true,
7189 /*use_default*/true);
7191 --processing_template_decl
;
7195 /* Subroutine of unify for the case when PARM is a
7196 BOUND_TEMPLATE_TEMPLATE_PARM. */
7199 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7202 tree parmvec
= TYPE_TI_ARGS (parm
);
7203 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7205 /* The template template parm might be variadic and the argument
7206 not, so flatten both argument lists. */
7207 parmvec
= expand_template_argument_pack (parmvec
);
7208 argvec
= expand_template_argument_pack (argvec
);
7212 /* In keeping with P0522R0, adjust P's template arguments
7213 to apply to A's template; then flatten it again. */
7214 tree nparmvec
= parmvec
;
7215 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7216 nparmvec
= expand_template_argument_pack (nparmvec
);
7218 if (unify (tparms
, targs
, nparmvec
, argvec
,
7219 UNIFY_ALLOW_NONE
, explain_p
))
7222 /* If the P0522 adjustment eliminated a pack expansion, deduce
7225 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7226 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7227 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7232 /* Deduce arguments T, i from TT<T> or TT<i>.
7233 We check each element of PARMVEC and ARGVEC individually
7234 rather than the whole TREE_VEC since they can have
7235 different number of elements, which is allowed under N2555. */
7237 int len
= TREE_VEC_LENGTH (parmvec
);
7239 /* Check if the parameters end in a pack, making them
7241 int parm_variadic_p
= 0;
7243 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7244 parm_variadic_p
= 1;
7246 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7247 /* If the template argument list of P contains a pack
7248 expansion that is not the last template argument, the
7249 entire template argument list is a non-deduced
7251 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7252 return unify_success (explain_p
);
7254 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7255 return unify_too_few_arguments (explain_p
,
7256 TREE_VEC_LENGTH (argvec
), len
);
7258 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7259 if (unify (tparms
, targs
,
7260 TREE_VEC_ELT (parmvec
, i
),
7261 TREE_VEC_ELT (argvec
, i
),
7262 UNIFY_ALLOW_NONE
, explain_p
))
7266 && unify_pack_expansion (tparms
, targs
,
7269 /*subr=*/true, explain_p
))
7276 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7277 template template parameters. Both PARM_PARMS and ARG_PARMS are
7278 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7281 Consider the example:
7282 template <class T> class A;
7283 template<template <class U> class TT> class B;
7285 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7286 the parameters to A, and OUTER_ARGS contains A. */
7289 coerce_template_template_parms (tree parm_parms
,
7291 tsubst_flags_t complain
,
7295 int nparms
, nargs
, i
;
7299 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7300 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7302 nparms
= TREE_VEC_LENGTH (parm_parms
);
7303 nargs
= TREE_VEC_LENGTH (arg_parms
);
7307 /* P0522R0: A template template-parameter P is at least as specialized as
7308 a template template-argument A if, given the following rewrite to two
7309 function templates, the function template corresponding to P is at
7310 least as specialized as the function template corresponding to A
7311 according to the partial ordering rules for function templates
7312 ([temp.func.order]). Given an invented class template X with the
7313 template parameter list of A (including default arguments):
7315 * Each of the two function templates has the same template parameters,
7316 respectively, as P or A.
7318 * Each function template has a single function parameter whose type is
7319 a specialization of X with template arguments corresponding to the
7320 template parameters from the respective function template where, for
7321 each template parameter PP in the template parameter list of the
7322 function template, a corresponding template argument AA is formed. If
7323 PP declares a parameter pack, then AA is the pack expansion
7324 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7326 If the rewrite produces an invalid type, then P is not at least as
7327 specialized as A. */
7329 /* So coerce P's args to apply to A's parms, and then deduce between A's
7330 args and the converted args. If that succeeds, A is at least as
7331 specialized as P, so they match.*/
7332 tree pargs
= template_parms_level_to_args (parm_parms
);
7333 ++processing_template_decl
;
7334 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7335 /*require_all*/true, /*use_default*/true);
7336 --processing_template_decl
;
7337 if (pargs
!= error_mark_node
)
7339 tree targs
= make_tree_vec (nargs
);
7340 tree aargs
= template_parms_level_to_args (arg_parms
);
7341 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7347 /* Determine whether we have a parameter pack at the end of the
7348 template template parameter's template parameter list. */
7349 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7351 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7353 if (error_operand_p (parm
))
7356 switch (TREE_CODE (parm
))
7360 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7365 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7375 && !(variadic_p
&& nargs
>= nparms
- 1))
7378 /* Check all of the template parameters except the parameter pack at
7379 the end (if any). */
7380 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7382 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7383 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7386 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7387 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7389 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7397 /* Check each of the template parameters in the template
7398 argument against the template parameter pack at the end of
7399 the template template parameter. */
7400 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7403 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7405 for (; i
< nargs
; ++i
)
7407 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7410 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7412 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7421 /* Verifies that the deduced template arguments (in TARGS) for the
7422 template template parameters (in TPARMS) represent valid bindings,
7423 by comparing the template parameter list of each template argument
7424 to the template parameter list of its corresponding template
7425 template parameter, in accordance with DR150. This
7426 routine can only be called after all template arguments have been
7427 deduced. It will return TRUE if all of the template template
7428 parameter bindings are okay, FALSE otherwise. */
7430 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7432 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7435 /* We're dealing with template parms in this process. */
7436 ++processing_template_decl
;
7438 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7440 for (i
= 0; i
< ntparms
; ++i
)
7442 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7443 tree targ
= TREE_VEC_ELT (targs
, i
);
7445 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7447 tree packed_args
= NULL_TREE
;
7450 if (ARGUMENT_PACK_P (targ
))
7452 /* Look inside the argument pack. */
7453 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7454 len
= TREE_VEC_LENGTH (packed_args
);
7457 for (idx
= 0; idx
< len
; ++idx
)
7459 tree targ_parms
= NULL_TREE
;
7462 /* Extract the next argument from the argument
7464 targ
= TREE_VEC_ELT (packed_args
, idx
);
7466 if (PACK_EXPANSION_P (targ
))
7467 /* Look at the pattern of the pack expansion. */
7468 targ
= PACK_EXPANSION_PATTERN (targ
);
7470 /* Extract the template parameters from the template
7472 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7473 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7474 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7475 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7477 /* Verify that we can coerce the template template
7478 parameters from the template argument to the template
7479 parameter. This requires an exact match. */
7481 && !coerce_template_template_parms
7482 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7497 --processing_template_decl
;
7501 /* Since type attributes aren't mangled, we need to strip them from
7502 template type arguments. */
7505 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7507 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7509 bool removed_attributes
= false;
7510 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7511 if (removed_attributes
7512 && (complain
& tf_warning
))
7513 warning (OPT_Wignored_attributes
,
7514 "ignoring attributes on template argument %qT", arg
);
7518 /* And from inside dependent non-type arguments like sizeof(Type). */
7521 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7523 if (!arg
|| arg
== error_mark_node
)
7525 bool removed_attributes
= false;
7526 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7527 if (removed_attributes
7528 && (complain
& tf_warning
))
7529 warning (OPT_Wignored_attributes
,
7530 "ignoring attributes in template argument %qE", arg
);
7534 // A template declaration can be substituted for a constrained
7535 // template template parameter only when the argument is more
7536 // constrained than the parameter.
7538 is_compatible_template_arg (tree parm
, tree arg
)
7540 tree parm_cons
= get_constraints (parm
);
7542 /* For now, allow constrained template template arguments
7543 and unconstrained template template parameters. */
7544 if (parm_cons
== NULL_TREE
)
7547 tree arg_cons
= get_constraints (arg
);
7549 // If the template parameter is constrained, we need to rewrite its
7550 // constraints in terms of the ARG's template parameters. This ensures
7551 // that all of the template parameter types will have the same depth.
7553 // Note that this is only valid when coerce_template_template_parm is
7554 // true for the innermost template parameters of PARM and ARG. In other
7555 // words, because coercion is successful, this conversion will be valid.
7558 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7559 parm_cons
= tsubst_constraint_info (parm_cons
,
7560 INNERMOST_TEMPLATE_ARGS (args
),
7561 tf_none
, NULL_TREE
);
7562 if (parm_cons
== error_mark_node
)
7566 return subsumes (parm_cons
, arg_cons
);
7569 // Convert a placeholder argument into a binding to the original
7570 // parameter. The original parameter is saved as the TREE_TYPE of
7573 convert_wildcard_argument (tree parm
, tree arg
)
7575 TREE_TYPE (arg
) = parm
;
7579 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7580 because one of them is dependent. But we need to represent the
7581 conversion for the benefit of cp_tree_equal. */
7584 maybe_convert_nontype_argument (tree type
, tree arg
)
7586 /* Auto parms get no conversion. */
7587 if (type_uses_auto (type
))
7589 /* We don't need or want to add this conversion now if we're going to use the
7590 argument for deduction. */
7591 if (value_dependent_expression_p (arg
))
7594 type
= cv_unqualified (type
);
7595 tree argtype
= TREE_TYPE (arg
);
7596 if (same_type_p (type
, argtype
))
7599 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7600 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7604 /* Convert the indicated template ARG as necessary to match the
7605 indicated template PARM. Returns the converted ARG, or
7606 error_mark_node if the conversion was unsuccessful. Error and
7607 warning messages are issued under control of COMPLAIN. This
7608 conversion is for the Ith parameter in the parameter list. ARGS is
7609 the full set of template arguments deduced so far. */
7612 convert_template_argument (tree parm
,
7615 tsubst_flags_t complain
,
7621 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7623 if (parm
== error_mark_node
)
7624 return error_mark_node
;
7626 /* Trivially convert placeholders. */
7627 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7628 return convert_wildcard_argument (parm
, arg
);
7630 if (arg
== any_targ_node
)
7633 if (TREE_CODE (arg
) == TREE_LIST
7634 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7636 /* The template argument was the name of some
7637 member function. That's usually
7638 invalid, but static members are OK. In any
7639 case, grab the underlying fields/functions
7640 and issue an error later if required. */
7641 orig_arg
= TREE_VALUE (arg
);
7642 TREE_TYPE (arg
) = unknown_type_node
;
7647 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7648 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7649 || requires_tmpl_type
);
7651 /* When determining whether an argument pack expansion is a template,
7652 look at the pattern. */
7653 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7654 arg
= PACK_EXPANSION_PATTERN (arg
);
7656 /* Deal with an injected-class-name used as a template template arg. */
7657 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7659 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7660 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7662 if (cxx_dialect
>= cxx11
)
7663 /* OK under DR 1004. */;
7664 else if (complain
& tf_warning_or_error
)
7665 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7666 " used as template template argument", TYPE_NAME (arg
));
7667 else if (flag_pedantic_errors
)
7675 ((TREE_CODE (arg
) == TEMPLATE_DECL
7676 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7677 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7678 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7679 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7682 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7683 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7684 arg
= TYPE_STUB_DECL (arg
);
7686 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7688 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7689 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7691 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7693 if (complain
& tf_error
)
7694 error ("invalid use of destructor %qE as a type", orig_arg
);
7695 return error_mark_node
;
7698 permerror (input_location
,
7699 "to refer to a type member of a template parameter, "
7700 "use %<typename %E%>", orig_arg
);
7702 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7703 TREE_OPERAND (arg
, 1),
7709 if (is_type
!= requires_type
)
7713 if (complain
& tf_error
)
7715 error ("type/value mismatch at argument %d in template "
7716 "parameter list for %qD",
7719 inform (input_location
,
7720 " expected a constant of type %qT, got %qT",
7722 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7723 else if (requires_tmpl_type
)
7724 inform (input_location
,
7725 " expected a class template, got %qE", orig_arg
);
7727 inform (input_location
,
7728 " expected a type, got %qE", orig_arg
);
7731 return error_mark_node
;
7733 if (is_tmpl_type
^ requires_tmpl_type
)
7735 if (in_decl
&& (complain
& tf_error
))
7737 error ("type/value mismatch at argument %d in template "
7738 "parameter list for %qD",
7741 inform (input_location
,
7742 " expected a type, got %qT", DECL_NAME (arg
));
7744 inform (input_location
,
7745 " expected a class template, got %qT", orig_arg
);
7747 return error_mark_node
;
7750 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7751 /* We already did the appropriate conversion when packing args. */
7755 if (requires_tmpl_type
)
7757 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7758 /* The number of argument required is not known yet.
7759 Just accept it for now. */
7763 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7766 /* Strip alias templates that are equivalent to another
7768 arg
= get_underlying_template (arg
);
7769 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7771 if (coerce_template_template_parms (parmparm
, argparm
,
7777 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7779 if (val
!= error_mark_node
)
7781 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7782 val
= TREE_TYPE (val
);
7783 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7784 val
= make_pack_expansion (val
, complain
);
7789 if (in_decl
&& (complain
& tf_error
))
7791 error ("type/value mismatch at argument %d in "
7792 "template parameter list for %qD",
7794 inform (input_location
,
7795 " expected a template of type %qD, got %qT",
7799 val
= error_mark_node
;
7802 // Check that the constraints are compatible before allowing the
7804 if (val
!= error_mark_node
)
7805 if (!is_compatible_template_arg (parm
, arg
))
7807 if (in_decl
&& (complain
& tf_error
))
7809 error ("constraint mismatch at argument %d in "
7810 "template parameter list for %qD",
7812 inform (input_location
, " expected %qD but got %qD",
7815 val
= error_mark_node
;
7821 /* We only form one instance of each template specialization.
7822 Therefore, if we use a non-canonical variant (i.e., a
7823 typedef), any future messages referring to the type will use
7824 the typedef, which is confusing if those future uses do not
7825 themselves also use the typedef. */
7827 val
= canonicalize_type_argument (val
, complain
);
7831 tree t
= TREE_TYPE (parm
);
7833 if (tree a
= type_uses_auto (t
))
7835 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7836 if (t
== error_mark_node
)
7837 return error_mark_node
;
7840 t
= tsubst (t
, args
, complain
, in_decl
);
7842 if (invalid_nontype_parm_type_p (t
, complain
))
7843 return error_mark_node
;
7845 if (!type_dependent_expression_p (orig_arg
)
7846 && !uses_template_parms (t
))
7847 /* We used to call digest_init here. However, digest_init
7848 will report errors, which we don't want when complain
7849 is zero. More importantly, digest_init will try too
7850 hard to convert things: for example, `0' should not be
7851 converted to pointer type at this point according to
7852 the standard. Accepting this is not merely an
7853 extension, since deciding whether or not these
7854 conversions can occur is part of determining which
7855 function template to call, or whether a given explicit
7856 argument specification is valid. */
7857 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7860 val
= canonicalize_expr_argument (orig_arg
, complain
);
7861 val
= maybe_convert_nontype_argument (t
, val
);
7865 if (val
== NULL_TREE
)
7866 val
= error_mark_node
;
7867 else if (val
== error_mark_node
&& (complain
& tf_error
))
7868 error ("could not convert template argument %qE from %qT to %qT",
7869 orig_arg
, TREE_TYPE (orig_arg
), t
);
7871 if (INDIRECT_REF_P (val
))
7873 /* Reject template arguments that are references to built-in
7874 functions with no library fallbacks. */
7875 const_tree inner
= TREE_OPERAND (val
, 0);
7876 const_tree innertype
= TREE_TYPE (inner
);
7878 && TREE_CODE (innertype
) == REFERENCE_TYPE
7879 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7880 && TREE_OPERAND_LENGTH (inner
) > 0
7881 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7882 return error_mark_node
;
7885 if (TREE_CODE (val
) == SCOPE_REF
)
7887 /* Strip typedefs from the SCOPE_REF. */
7888 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7889 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7891 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7892 QUALIFIED_NAME_IS_TEMPLATE (val
));
7899 /* Coerces the remaining template arguments in INNER_ARGS (from
7900 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7901 Returns the coerced argument pack. PARM_IDX is the position of this
7902 parameter in the template parameter list. ARGS is the original
7903 template argument list. */
7905 coerce_template_parameter_pack (tree parms
,
7913 tsubst_flags_t complain
)
7915 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7916 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7919 tree packed_parms
= NULL_TREE
;
7921 if (arg_idx
> nargs
)
7924 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7926 /* When the template parameter is a non-type template parameter pack
7927 or template template parameter pack whose type or template
7928 parameters use parameter packs, we know exactly how many arguments
7929 we are looking for. Build a vector of the instantiated decls for
7930 these template parameters in PACKED_PARMS. */
7931 /* We can't use make_pack_expansion here because it would interpret a
7932 _DECL as a use rather than a declaration. */
7933 tree decl
= TREE_VALUE (parm
);
7934 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7935 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7936 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7937 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7939 TREE_VEC_LENGTH (args
)--;
7940 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7941 TREE_VEC_LENGTH (args
)++;
7943 if (packed_parms
== error_mark_node
)
7944 return error_mark_node
;
7946 /* If we're doing a partial instantiation of a member template,
7947 verify that all of the types used for the non-type
7948 template parameter pack are, in fact, valid for non-type
7949 template parameters. */
7951 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7953 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7954 for (j
= 0; j
< len
; ++j
)
7956 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7957 if (invalid_nontype_parm_type_p (t
, complain
))
7958 return error_mark_node
;
7960 /* We don't know how many args we have yet, just
7961 use the unconverted ones for now. */
7965 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7967 /* Check if we have a placeholder pack, which indicates we're
7968 in the context of a introduction list. In that case we want
7969 to match this pack to the single placeholder. */
7970 else if (arg_idx
< nargs
7971 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7972 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7974 nargs
= arg_idx
+ 1;
7975 packed_args
= make_tree_vec (1);
7978 packed_args
= make_tree_vec (nargs
- arg_idx
);
7980 /* Convert the remaining arguments, which will be a part of the
7981 parameter pack "parm". */
7982 int first_pack_arg
= arg_idx
;
7983 for (; arg_idx
< nargs
; ++arg_idx
)
7985 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7986 tree actual_parm
= TREE_VALUE (parm
);
7987 int pack_idx
= arg_idx
- first_pack_arg
;
7991 /* Once we've packed as many args as we have types, stop. */
7992 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7994 else if (PACK_EXPANSION_P (arg
))
7995 /* We don't know how many args we have yet, just
7996 use the unconverted ones for now. */
7999 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8002 if (arg
== error_mark_node
)
8004 if (complain
& tf_error
)
8005 error ("template argument %d is invalid", arg_idx
+ 1);
8008 arg
= convert_template_argument (actual_parm
,
8009 arg
, new_args
, complain
, parm_idx
,
8011 if (arg
== error_mark_node
)
8013 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8016 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8017 && TREE_VEC_LENGTH (packed_args
) > 0)
8019 if (complain
& tf_error
)
8020 error ("wrong number of template arguments (%d, should be %d)",
8021 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8022 return error_mark_node
;
8025 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8026 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8027 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8030 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8031 TREE_CONSTANT (argument_pack
) = 1;
8034 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8036 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8037 TREE_VEC_LENGTH (packed_args
));
8038 return argument_pack
;
8041 /* Returns the number of pack expansions in the template argument vector
8045 pack_expansion_args_count (tree args
)
8050 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8052 tree elt
= TREE_VEC_ELT (args
, i
);
8053 if (elt
&& PACK_EXPANSION_P (elt
))
8059 /* Convert all template arguments to their appropriate types, and
8060 return a vector containing the innermost resulting template
8061 arguments. If any error occurs, return error_mark_node. Error and
8062 warning messages are issued under control of COMPLAIN.
8064 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8065 for arguments not specified in ARGS. Otherwise, if
8066 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8067 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8068 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8072 coerce_template_parms (tree parms
,
8075 tsubst_flags_t complain
,
8076 bool require_all_args
,
8077 bool use_default_args
)
8079 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8080 tree orig_inner_args
;
8083 tree new_inner_args
;
8084 int saved_unevaluated_operand
;
8085 int saved_inhibit_evaluation_warnings
;
8087 /* When used as a boolean value, indicates whether this is a
8088 variadic template parameter list. Since it's an int, we can also
8089 subtract it from nparms to get the number of non-variadic
8092 int variadic_args_p
= 0;
8093 int post_variadic_parms
= 0;
8095 /* Likewise for parameters with default arguments. */
8098 if (args
== error_mark_node
)
8099 return error_mark_node
;
8101 nparms
= TREE_VEC_LENGTH (parms
);
8103 /* Determine if there are any parameter packs or default arguments. */
8104 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8106 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8108 ++post_variadic_parms
;
8109 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8111 if (TREE_PURPOSE (parm
))
8115 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8116 /* If there are no parameters that follow a parameter pack, we need to
8117 expand any argument packs so that we can deduce a parameter pack from
8118 some non-packed args followed by an argument pack, as in variadic85.C.
8119 If there are such parameters, we need to leave argument packs intact
8120 so the arguments are assigned properly. This can happen when dealing
8121 with a nested class inside a partial specialization of a class
8122 template, as in variadic92.C, or when deducing a template parameter pack
8123 from a sub-declarator, as in variadic114.C. */
8124 if (!post_variadic_parms
)
8125 inner_args
= expand_template_argument_pack (inner_args
);
8127 /* Count any pack expansion args. */
8128 variadic_args_p
= pack_expansion_args_count (inner_args
);
8130 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8131 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8132 || (nargs
< nparms
- variadic_p
8135 && (!use_default_args
8136 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8137 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8139 if (complain
& tf_error
)
8141 if (variadic_p
|| default_p
)
8143 nparms
-= variadic_p
+ default_p
;
8144 error ("wrong number of template arguments "
8145 "(%d, should be at least %d)", nargs
, nparms
);
8148 error ("wrong number of template arguments "
8149 "(%d, should be %d)", nargs
, nparms
);
8152 inform (DECL_SOURCE_LOCATION (in_decl
),
8153 "provided for %qD", in_decl
);
8156 return error_mark_node
;
8158 /* We can't pass a pack expansion to a non-pack parameter of an alias
8159 template (DR 1430). */
8161 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8162 || concept_template_p (in_decl
))
8164 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8166 if (complain
& tf_error
)
8168 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8170 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8171 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8173 if (PACK_EXPANSION_P (arg
)
8174 && !template_parameter_pack_p (parm
))
8176 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8177 error_at (location_of (arg
),
8178 "pack expansion argument for non-pack parameter "
8179 "%qD of alias template %qD", parm
, in_decl
);
8181 error_at (location_of (arg
),
8182 "pack expansion argument for non-pack parameter "
8183 "%qD of concept %qD", parm
, in_decl
);
8184 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8191 return error_mark_node
;
8194 /* We need to evaluate the template arguments, even though this
8195 template-id may be nested within a "sizeof". */
8196 saved_unevaluated_operand
= cp_unevaluated_operand
;
8197 cp_unevaluated_operand
= 0;
8198 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8199 c_inhibit_evaluation_warnings
= 0;
8200 new_inner_args
= make_tree_vec (nparms
);
8201 new_args
= add_outermost_template_args (args
, new_inner_args
);
8202 int pack_adjust
= 0;
8203 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8208 /* Get the Ith template parameter. */
8209 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8211 if (parm
== error_mark_node
)
8213 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8217 /* Calculate the next argument. */
8218 if (arg_idx
< nargs
)
8219 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8223 if (template_parameter_pack_p (TREE_VALUE (parm
))
8224 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8226 /* Some arguments will be placed in the
8227 template parameter pack PARM. */
8228 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8229 inner_args
, arg_idx
,
8233 if (arg
== NULL_TREE
)
8235 /* We don't know how many args we have yet, just use the
8236 unconverted (and still packed) ones for now. */
8237 new_inner_args
= orig_inner_args
;
8242 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8244 /* Store this argument. */
8245 if (arg
== error_mark_node
)
8248 /* We are done with all of the arguments. */
8253 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8254 arg_idx
+= pack_adjust
;
8261 if (PACK_EXPANSION_P (arg
))
8263 /* "If every valid specialization of a variadic template
8264 requires an empty template parameter pack, the template is
8265 ill-formed, no diagnostic required." So check that the
8266 pattern works with this parameter. */
8267 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8268 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8272 if (conv
== error_mark_node
)
8274 if (complain
& tf_error
)
8275 inform (input_location
, "so any instantiation with a "
8276 "non-empty parameter pack would be ill-formed");
8279 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8280 /* Recover from missing typename. */
8281 TREE_VEC_ELT (inner_args
, arg_idx
)
8282 = make_pack_expansion (conv
, complain
);
8284 /* We don't know how many args we have yet, just
8285 use the unconverted ones for now. */
8286 new_inner_args
= inner_args
;
8291 else if (require_all_args
)
8293 /* There must be a default arg in this case. */
8294 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8296 /* The position of the first default template argument,
8297 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8299 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8300 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8301 arg_idx
- pack_adjust
);
8306 if (arg
== error_mark_node
)
8308 if (complain
& tf_error
)
8309 error ("template argument %d is invalid", arg_idx
+ 1);
8312 /* This only occurs if there was an error in the template
8313 parameter list itself (which we would already have
8314 reported) that we are trying to recover from, e.g., a class
8315 template with a parameter list such as
8316 template<typename..., typename>. */
8319 arg
= convert_template_argument (TREE_VALUE (parm
),
8320 arg
, new_args
, complain
,
8323 if (arg
== error_mark_node
)
8325 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8327 cp_unevaluated_operand
= saved_unevaluated_operand
;
8328 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8330 if (variadic_p
&& arg_idx
< nargs
)
8332 if (complain
& tf_error
)
8334 error ("wrong number of template arguments "
8335 "(%d, should be %d)", nargs
, arg_idx
);
8337 error ("provided for %q+D", in_decl
);
8339 return error_mark_node
;
8343 return error_mark_node
;
8345 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8346 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8347 TREE_VEC_LENGTH (new_inner_args
));
8349 return new_inner_args
;
8352 /* Convert all template arguments to their appropriate types, and
8353 return a vector containing the innermost resulting template
8354 arguments. If any error occurs, return error_mark_node. Error and
8355 warning messages are not issued.
8357 Note that no function argument deduction is performed, and default
8358 arguments are used to fill in unspecified arguments. */
8360 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8362 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8365 /* Convert all template arguments to their appropriate type, and
8366 instantiate default arguments as needed. This returns a vector
8367 containing the innermost resulting template arguments, or
8368 error_mark_node if unsuccessful. */
8370 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8371 tsubst_flags_t complain
)
8373 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8376 /* Like coerce_template_parms. If PARMS represents all template
8377 parameters levels, this function returns a vector of vectors
8378 representing all the resulting argument levels. Note that in this
8379 case, only the innermost arguments are coerced because the
8380 outermost ones are supposed to have been coerced already.
8382 Otherwise, if PARMS represents only (the innermost) vector of
8383 parameters, this function returns a vector containing just the
8384 innermost resulting arguments. */
8387 coerce_innermost_template_parms (tree parms
,
8390 tsubst_flags_t complain
,
8391 bool require_all_args
,
8392 bool use_default_args
)
8394 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8395 int args_depth
= TMPL_ARGS_DEPTH (args
);
8398 if (parms_depth
> 1)
8400 coerced_args
= make_tree_vec (parms_depth
);
8404 for (level
= parms
, cur_depth
= parms_depth
;
8405 parms_depth
> 0 && level
!= NULL_TREE
;
8406 level
= TREE_CHAIN (level
), --cur_depth
)
8409 if (cur_depth
== args_depth
)
8410 l
= coerce_template_parms (TREE_VALUE (level
),
8411 args
, in_decl
, complain
,
8415 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8417 if (l
== error_mark_node
)
8418 return error_mark_node
;
8420 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8424 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8425 args
, in_decl
, complain
,
8428 return coerced_args
;
8431 /* Returns 1 if template args OT and NT are equivalent. */
8434 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8438 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8440 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8443 if (TREE_CODE (nt
) == TREE_VEC
)
8444 /* For member templates */
8445 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8446 else if (PACK_EXPANSION_P (ot
))
8447 return (PACK_EXPANSION_P (nt
)
8448 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8449 PACK_EXPANSION_PATTERN (nt
))
8450 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8451 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8452 else if (ARGUMENT_PACK_P (ot
))
8457 if (!ARGUMENT_PACK_P (nt
))
8460 opack
= ARGUMENT_PACK_ARGS (ot
);
8461 npack
= ARGUMENT_PACK_ARGS (nt
);
8462 len
= TREE_VEC_LENGTH (opack
);
8463 if (TREE_VEC_LENGTH (npack
) != len
)
8465 for (i
= 0; i
< len
; ++i
)
8466 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8467 TREE_VEC_ELT (npack
, i
)))
8471 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8473 else if (TYPE_P (nt
))
8477 /* Don't treat an alias template specialization with dependent
8478 arguments as equivalent to its underlying type when used as a
8479 template argument; we need them to be distinct so that we
8480 substitute into the specialization arguments at instantiation
8481 time. And aliases can't be equivalent without being ==, so
8482 we don't need to look any deeper.
8484 During partial ordering, however, we need to treat them normally so
8485 that we can order uses of the same alias with different
8486 cv-qualification (79960). */
8488 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8491 return same_type_p (ot
, nt
);
8493 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8497 /* Try to treat a template non-type argument that has been converted
8498 to the parameter type as equivalent to one that hasn't yet. */
8499 for (enum tree_code code1
= TREE_CODE (ot
);
8500 CONVERT_EXPR_CODE_P (code1
)
8501 || code1
== NON_LVALUE_EXPR
;
8502 code1
= TREE_CODE (ot
))
8503 ot
= TREE_OPERAND (ot
, 0);
8504 for (enum tree_code code2
= TREE_CODE (nt
);
8505 CONVERT_EXPR_CODE_P (code2
)
8506 || code2
== NON_LVALUE_EXPR
;
8507 code2
= TREE_CODE (nt
))
8508 nt
= TREE_OPERAND (nt
, 0);
8510 return cp_tree_equal (ot
, nt
);
8514 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8515 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8516 NEWARG_PTR with the offending arguments if they are non-NULL. */
8519 comp_template_args (tree oldargs
, tree newargs
,
8520 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8525 if (oldargs
== newargs
)
8528 if (!oldargs
|| !newargs
)
8531 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8534 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8536 tree nt
= TREE_VEC_ELT (newargs
, i
);
8537 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8539 if (! template_args_equal (ot
, nt
, partial_order
))
8541 if (oldarg_ptr
!= NULL
)
8543 if (newarg_ptr
!= NULL
)
8552 comp_template_args_porder (tree oargs
, tree nargs
)
8554 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8558 add_pending_template (tree d
)
8560 tree ti
= (TYPE_P (d
)
8561 ? CLASSTYPE_TEMPLATE_INFO (d
)
8562 : DECL_TEMPLATE_INFO (d
));
8563 struct pending_template
*pt
;
8566 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8569 /* We are called both from instantiate_decl, where we've already had a
8570 tinst_level pushed, and instantiate_template, where we haven't.
8572 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8575 push_tinst_level (d
);
8577 pt
= ggc_alloc
<pending_template
> ();
8579 pt
->tinst
= current_tinst_level
;
8580 if (last_pending_template
)
8581 last_pending_template
->next
= pt
;
8583 pending_templates
= pt
;
8585 last_pending_template
= pt
;
8587 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8594 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8595 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8596 documentation for TEMPLATE_ID_EXPR. */
8599 lookup_template_function (tree fns
, tree arglist
)
8603 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8604 return error_mark_node
;
8606 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8608 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8610 error ("%q#D is not a function template", fns
);
8611 return error_mark_node
;
8614 if (BASELINK_P (fns
))
8616 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8618 BASELINK_FUNCTIONS (fns
),
8623 type
= TREE_TYPE (fns
);
8624 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8625 type
= unknown_type_node
;
8627 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8630 /* Within the scope of a template class S<T>, the name S gets bound
8631 (in build_self_reference) to a TYPE_DECL for the class, not a
8632 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8633 or one of its enclosing classes, and that type is a template,
8634 return the associated TEMPLATE_DECL. Otherwise, the original
8637 Also handle the case when DECL is a TREE_LIST of ambiguous
8638 injected-class-names from different bases. */
8641 maybe_get_template_decl_from_type_decl (tree decl
)
8643 if (decl
== NULL_TREE
)
8646 /* DR 176: A lookup that finds an injected-class-name (10.2
8647 [class.member.lookup]) can result in an ambiguity in certain cases
8648 (for example, if it is found in more than one base class). If all of
8649 the injected-class-names that are found refer to specializations of
8650 the same class template, and if the name is followed by a
8651 template-argument-list, the reference refers to the class template
8652 itself and not a specialization thereof, and is not ambiguous. */
8653 if (TREE_CODE (decl
) == TREE_LIST
)
8655 tree t
, tmpl
= NULL_TREE
;
8656 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8658 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8661 else if (tmpl
!= elt
)
8664 if (tmpl
&& t
== NULL_TREE
)
8670 return (decl
!= NULL_TREE
8671 && DECL_SELF_REFERENCE_P (decl
)
8672 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8673 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8676 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8677 parameters, find the desired type.
8679 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8681 IN_DECL, if non-NULL, is the template declaration we are trying to
8684 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8685 the class we are looking up.
8687 Issue error and warning messages under control of COMPLAIN.
8689 If the template class is really a local class in a template
8690 function, then the FUNCTION_CONTEXT is the function in which it is
8693 ??? Note that this function is currently called *twice* for each
8694 template-id: the first time from the parser, while creating the
8695 incomplete type (finish_template_type), and the second type during the
8696 real instantiation (instantiate_template_class). This is surely something
8697 that we want to avoid. It also causes some problems with argument
8698 coercion (see convert_nontype_argument for more information on this). */
8701 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8702 int entering_scope
, tsubst_flags_t complain
)
8704 tree templ
= NULL_TREE
, parmlist
;
8711 if (identifier_p (d1
))
8713 tree value
= innermost_non_namespace_value (d1
);
8714 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8719 push_decl_namespace (context
);
8720 templ
= lookup_name (d1
);
8721 templ
= maybe_get_template_decl_from_type_decl (templ
);
8723 pop_decl_namespace ();
8726 context
= DECL_CONTEXT (templ
);
8728 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8730 tree type
= TREE_TYPE (d1
);
8732 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8733 an implicit typename for the second A. Deal with it. */
8734 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8735 type
= TREE_TYPE (type
);
8737 if (CLASSTYPE_TEMPLATE_INFO (type
))
8739 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8740 d1
= DECL_NAME (templ
);
8743 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8744 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8746 templ
= TYPE_TI_TEMPLATE (d1
);
8747 d1
= DECL_NAME (templ
);
8749 else if (DECL_TYPE_TEMPLATE_P (d1
))
8752 d1
= DECL_NAME (templ
);
8753 context
= DECL_CONTEXT (templ
);
8755 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8758 d1
= DECL_NAME (templ
);
8761 /* Issue an error message if we didn't find a template. */
8764 if (complain
& tf_error
)
8765 error ("%qT is not a template", d1
);
8766 return error_mark_node
;
8769 if (TREE_CODE (templ
) != TEMPLATE_DECL
8770 /* Make sure it's a user visible template, if it was named by
8772 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8773 && !PRIMARY_TEMPLATE_P (templ
)))
8775 if (complain
& tf_error
)
8777 error ("non-template type %qT used as a template", d1
);
8779 error ("for template declaration %q+D", in_decl
);
8781 return error_mark_node
;
8784 complain
&= ~tf_user
;
8786 /* An alias that just changes the name of a template is equivalent to the
8787 other template, so if any of the arguments are pack expansions, strip
8788 the alias to avoid problems with a pack expansion passed to a non-pack
8789 alias template parameter (DR 1430). */
8790 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8791 templ
= get_underlying_template (templ
);
8793 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8796 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8797 if (arglist2
== error_mark_node
8798 || (!uses_template_parms (arglist2
)
8799 && check_instantiated_args (templ
, arglist2
, complain
)))
8800 return error_mark_node
;
8802 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8807 tree template_type
= TREE_TYPE (templ
);
8810 tree found
= NULL_TREE
;
8813 int is_dependent_type
;
8814 int use_partial_inst_tmpl
= false;
8816 if (template_type
== error_mark_node
)
8817 /* An error occurred while building the template TEMPL, and a
8818 diagnostic has most certainly been emitted for that
8819 already. Let's propagate that error. */
8820 return error_mark_node
;
8822 gen_tmpl
= most_general_template (templ
);
8823 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8824 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8825 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8827 if (arg_depth
== 1 && parm_depth
> 1)
8829 /* We've been given an incomplete set of template arguments.
8832 template <class T> struct S1 {
8833 template <class U> struct S2 {};
8834 template <class U> struct S2<U*> {};
8837 we will be called with an ARGLIST of `U*', but the
8838 TEMPLATE will be `template <class T> template
8839 <class U> struct S1<T>::S2'. We must fill in the missing
8841 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8842 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8843 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8846 /* Now we should have enough arguments. */
8847 gcc_assert (parm_depth
== arg_depth
);
8849 /* From here on, we're only interested in the most general
8852 /* Calculate the BOUND_ARGS. These will be the args that are
8853 actually tsubst'd into the definition to create the
8855 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8857 /*require_all_args=*/true,
8858 /*use_default_args=*/true);
8860 if (arglist
== error_mark_node
)
8861 /* We were unable to bind the arguments. */
8862 return error_mark_node
;
8864 /* In the scope of a template class, explicit references to the
8865 template class refer to the type of the template, not any
8866 instantiation of it. For example, in:
8868 template <class T> class C { void f(C<T>); }
8870 the `C<T>' is just the same as `C'. Outside of the
8871 class, however, such a reference is an instantiation. */
8873 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8874 || currently_open_class (template_type
))
8876 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8878 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8879 return template_type
;
8882 /* If we already have this specialization, return it. */
8883 elt
.tmpl
= gen_tmpl
;
8885 elt
.spec
= NULL_TREE
;
8886 hash
= spec_hasher::hash (&elt
);
8887 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8892 /* If the the template's constraints are not satisfied,
8893 then we cannot form a valid type.
8895 Note that the check is deferred until after the hash
8896 lookup. This prevents redundant checks on previously
8897 instantiated specializations. */
8898 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8900 if (complain
& tf_error
)
8902 error ("template constraint failure");
8903 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8905 return error_mark_node
;
8908 is_dependent_type
= uses_template_parms (arglist
);
8910 /* If the deduced arguments are invalid, then the binding
8912 if (!is_dependent_type
8913 && check_instantiated_args (gen_tmpl
,
8914 INNERMOST_TEMPLATE_ARGS (arglist
),
8916 return error_mark_node
;
8918 if (!is_dependent_type
8919 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8920 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8921 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8923 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8924 DECL_NAME (gen_tmpl
),
8925 /*tag_scope=*/ts_global
);
8929 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8931 if (context
== error_mark_node
)
8932 return error_mark_node
;
8935 context
= global_namespace
;
8937 /* Create the type. */
8938 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8940 /* The user referred to a specialization of an alias
8941 template represented by GEN_TMPL.
8943 [temp.alias]/2 says:
8945 When a template-id refers to the specialization of an
8946 alias template, it is equivalent to the associated
8947 type obtained by substitution of its
8948 template-arguments for the template-parameters in the
8949 type-id of the alias template. */
8951 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8952 /* Note that the call above (by indirectly calling
8953 register_specialization in tsubst_decl) registers the
8954 TYPE_DECL representing the specialization of the alias
8955 template. So next time someone substitutes ARGLIST for
8956 the template parms into the alias template (GEN_TMPL),
8957 she'll get that TYPE_DECL back. */
8959 if (t
== error_mark_node
)
8962 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8964 if (!is_dependent_type
)
8966 set_current_access_from_decl (TYPE_NAME (template_type
));
8967 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8968 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8969 arglist
, complain
, in_decl
),
8970 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8971 arglist
, complain
, in_decl
),
8972 SCOPED_ENUM_P (template_type
), NULL
);
8974 if (t
== error_mark_node
)
8979 /* We don't want to call start_enum for this type, since
8980 the values for the enumeration constants may involve
8981 template parameters. And, no one should be interested
8982 in the enumeration constants for such a type. */
8983 t
= cxx_make_type (ENUMERAL_TYPE
);
8984 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8986 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8987 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8988 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8990 else if (CLASS_TYPE_P (template_type
))
8992 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8993 instantiated here. */
8994 gcc_assert (!LAMBDA_TYPE_P (template_type
));
8996 t
= make_class_type (TREE_CODE (template_type
));
8997 CLASSTYPE_DECLARED_CLASS (t
)
8998 = CLASSTYPE_DECLARED_CLASS (template_type
);
8999 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9001 /* A local class. Make sure the decl gets registered properly. */
9002 if (context
== current_function_decl
)
9003 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
9005 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9006 /* This instantiation is another name for the primary
9007 template type. Set the TYPE_CANONICAL field
9009 TYPE_CANONICAL (t
) = template_type
;
9010 else if (any_template_arguments_need_structural_equality_p (arglist
))
9011 /* Some of the template arguments require structural
9012 equality testing, so this template class requires
9013 structural equality testing. */
9014 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9019 /* If we called start_enum or pushtag above, this information
9020 will already be set up. */
9023 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9025 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9026 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9027 DECL_SOURCE_LOCATION (type_decl
)
9028 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9031 type_decl
= TYPE_NAME (t
);
9033 if (CLASS_TYPE_P (template_type
))
9035 TREE_PRIVATE (type_decl
)
9036 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9037 TREE_PROTECTED (type_decl
)
9038 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9039 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9041 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9042 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9046 if (OVERLOAD_TYPE_P (t
)
9047 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9049 static const char *tags
[] = {"abi_tag", "may_alias"};
9051 for (unsigned ix
= 0; ix
!= 2; ix
++)
9054 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9058 = tree_cons (TREE_PURPOSE (attributes
),
9059 TREE_VALUE (attributes
),
9060 TYPE_ATTRIBUTES (t
));
9064 /* Let's consider the explicit specialization of a member
9065 of a class template specialization that is implicitly instantiated,
9070 template<class U> struct M {}; //#0
9075 struct S<int>::M<char> //#1
9079 [temp.expl.spec]/4 says this is valid.
9081 In this case, when we write:
9084 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9087 When we encounter #1, we want to store the partial instantiation
9088 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9090 For all cases other than this "explicit specialization of member of a
9091 class template", we just want to store the most general template into
9092 the CLASSTYPE_TI_TEMPLATE of M.
9094 This case of "explicit specialization of member of a class template"
9096 1/ the enclosing class is an instantiation of, and therefore not
9097 the same as, the context of the most general template, and
9098 2/ we aren't looking at the partial instantiation itself, i.e.
9099 the innermost arguments are not the same as the innermost parms of
9100 the most general template.
9102 So it's only when 1/ and 2/ happens that we want to use the partial
9103 instantiation of the member template in lieu of its most general
9106 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9107 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9108 /* the enclosing class must be an instantiation... */
9109 && CLASS_TYPE_P (context
)
9110 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9112 TREE_VEC_LENGTH (arglist
)--;
9113 ++processing_template_decl
;
9114 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9115 tree partial_inst_args
=
9116 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9117 arglist
, complain
, NULL_TREE
);
9118 --processing_template_decl
;
9119 TREE_VEC_LENGTH (arglist
)++;
9120 if (partial_inst_args
== error_mark_node
)
9121 return error_mark_node
;
9122 use_partial_inst_tmpl
=
9123 /*...and we must not be looking at the partial instantiation
9125 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9129 if (!use_partial_inst_tmpl
)
9130 /* This case is easy; there are no member templates involved. */
9134 /* This is a full instantiation of a member template. Find
9135 the partial instantiation of which this is an instance. */
9137 /* Temporarily reduce by one the number of levels in the ARGLIST
9138 so as to avoid comparing the last set of arguments. */
9139 TREE_VEC_LENGTH (arglist
)--;
9140 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9141 TREE_VEC_LENGTH (arglist
)++;
9142 /* FOUND is either a proper class type, or an alias
9143 template specialization. In the later case, it's a
9144 TYPE_DECL, resulting from the substituting of arguments
9145 for parameters in the TYPE_DECL of the alias template
9146 done earlier. So be careful while getting the template
9148 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9150 : (TREE_CODE (found
) == TYPE_DECL
9151 ? DECL_TI_TEMPLATE (found
)
9152 : CLASSTYPE_TI_TEMPLATE (found
)));
9155 // Build template info for the new specialization.
9156 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9159 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9160 entry
= ggc_alloc
<spec_entry
> ();
9164 /* Note this use of the partial instantiation so we can check it
9165 later in maybe_process_partial_specialization. */
9166 DECL_TEMPLATE_INSTANTIATIONS (found
)
9167 = tree_cons (arglist
, t
,
9168 DECL_TEMPLATE_INSTANTIATIONS (found
));
9170 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9171 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9172 /* Now that the type has been registered on the instantiations
9173 list, we set up the enumerators. Because the enumeration
9174 constants may involve the enumeration type itself, we make
9175 sure to register the type first, and then create the
9176 constants. That way, doing tsubst_expr for the enumeration
9177 constants won't result in recursive calls here; we'll find
9178 the instantiation and exit above. */
9179 tsubst_enum (template_type
, t
, arglist
);
9181 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9182 /* If the type makes use of template parameters, the
9183 code that generates debugging information will crash. */
9184 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9186 /* Possibly limit visibility based on template args. */
9187 TREE_PUBLIC (type_decl
) = 1;
9188 determine_visibility (type_decl
);
9190 inherit_targ_abi_tags (t
);
9196 /* Wrapper for lookup_template_class_1. */
9199 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9200 int entering_scope
, tsubst_flags_t complain
)
9203 timevar_push (TV_TEMPLATE_INST
);
9204 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9205 entering_scope
, complain
);
9206 timevar_pop (TV_TEMPLATE_INST
);
9210 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9213 lookup_template_variable (tree templ
, tree arglist
)
9215 /* The type of the expression is NULL_TREE since the template-id could refer
9216 to an explicit or partial specialization. */
9217 tree type
= NULL_TREE
;
9218 if (flag_concepts
&& variable_concept_p (templ
))
9219 /* Except that concepts are always bool. */
9220 type
= boolean_type_node
;
9221 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9224 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9227 finish_template_variable (tree var
, tsubst_flags_t complain
)
9229 tree templ
= TREE_OPERAND (var
, 0);
9230 tree arglist
= TREE_OPERAND (var
, 1);
9232 /* We never want to return a VAR_DECL for a variable concept, since they
9233 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9234 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9235 if (concept_p
&& processing_template_decl
)
9238 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9239 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9241 templ
= most_general_template (templ
);
9242 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9243 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9245 /*use_default*/true);
9247 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9249 if (complain
& tf_error
)
9251 error ("use of invalid variable template %qE", var
);
9252 diagnose_constraints (location_of (var
), templ
, arglist
);
9254 return error_mark_node
;
9257 /* If a template-id refers to a specialization of a variable
9258 concept, then the expression is true if and only if the
9259 concept's constraints are satisfied by the given template
9262 NOTE: This is an extension of Concepts Lite TS that
9263 allows constraints to be used in expressions. */
9266 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9267 return evaluate_variable_concept (decl
, arglist
);
9270 return instantiate_template (templ
, arglist
, complain
);
9273 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9274 TARGS template args, and instantiate it if it's not dependent. */
9277 lookup_and_finish_template_variable (tree templ
, tree targs
,
9278 tsubst_flags_t complain
)
9280 templ
= lookup_template_variable (templ
, targs
);
9281 if (!any_dependent_template_arguments_p (targs
))
9283 templ
= finish_template_variable (templ
, complain
);
9287 return convert_from_reference (templ
);
9296 /* True when we should also visit template parameters that occur in
9297 non-deduced contexts. */
9298 bool include_nondeduced_p
;
9299 hash_set
<tree
> *visited
;
9302 /* Called from for_each_template_parm via walk_tree. */
9305 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9308 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9309 tree_fn_t fn
= pfd
->fn
;
9310 void *data
= pfd
->data
;
9311 tree result
= NULL_TREE
;
9313 #define WALK_SUBTREE(NODE) \
9316 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9317 pfd->include_nondeduced_p, \
9319 if (result) goto out; \
9323 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9327 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9328 WALK_SUBTREE (TYPE_CONTEXT (t
));
9330 switch (TREE_CODE (t
))
9333 if (TYPE_PTRMEMFUNC_P (t
))
9339 if (!TYPE_TEMPLATE_INFO (t
))
9342 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9346 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9347 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9351 /* Since we're not going to walk subtrees, we have to do this
9353 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9357 /* Check the return type. */
9358 WALK_SUBTREE (TREE_TYPE (t
));
9360 /* Check the parameter types. Since default arguments are not
9361 instantiated until they are needed, the TYPE_ARG_TYPES may
9362 contain expressions that involve template parameters. But,
9363 no-one should be looking at them yet. And, once they're
9364 instantiated, they don't contain template parameters, so
9365 there's no point in looking at them then, either. */
9369 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9370 WALK_SUBTREE (TREE_VALUE (parm
));
9372 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9373 want walk_tree walking into them itself. */
9377 if (flag_noexcept_type
)
9379 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9381 WALK_SUBTREE (TREE_PURPOSE (spec
));
9386 case UNDERLYING_TYPE
:
9387 if (pfd
->include_nondeduced_p
9388 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9390 pfd
->include_nondeduced_p
,
9392 return error_mark_node
;
9397 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9398 WALK_SUBTREE (DECL_TI_ARGS (t
));
9403 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9404 WALK_SUBTREE (DECL_INITIAL (t
));
9405 if (DECL_CONTEXT (t
)
9406 && pfd
->include_nondeduced_p
)
9407 WALK_SUBTREE (DECL_CONTEXT (t
));
9410 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9411 /* Record template parameters such as `T' inside `TT<T>'. */
9412 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9415 case TEMPLATE_TEMPLATE_PARM
:
9416 case TEMPLATE_TYPE_PARM
:
9417 case TEMPLATE_PARM_INDEX
:
9418 if (fn
&& (*fn
)(t
, data
))
9425 /* A template template parameter is encountered. */
9426 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9427 WALK_SUBTREE (TREE_TYPE (t
));
9429 /* Already substituted template template parameter */
9434 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9435 partial instantiation. */
9436 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9440 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9441 && pfd
->include_nondeduced_p
)
9442 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9447 /* If there's no type, then this thing must be some expression
9448 involving template parameters. */
9449 if (!fn
&& !TREE_TYPE (t
))
9450 return error_mark_node
;
9455 case IMPLICIT_CONV_EXPR
:
9456 case REINTERPRET_CAST_EXPR
:
9457 case CONST_CAST_EXPR
:
9458 case STATIC_CAST_EXPR
:
9459 case DYNAMIC_CAST_EXPR
:
9463 case PSEUDO_DTOR_EXPR
:
9465 return error_mark_node
;
9474 /* We didn't find any template parameters we liked. */
9479 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9480 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9481 call FN with the parameter and the DATA.
9482 If FN returns nonzero, the iteration is terminated, and
9483 for_each_template_parm returns 1. Otherwise, the iteration
9484 continues. If FN never returns a nonzero value, the value
9485 returned by for_each_template_parm is 0. If FN is NULL, it is
9486 considered to be the function which always returns 1.
9488 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9489 parameters that occur in non-deduced contexts. When false, only
9490 visits those template parameters that can be deduced. */
9493 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9494 hash_set
<tree
> *visited
,
9495 bool include_nondeduced_p
,
9498 struct pair_fn_data pfd
;
9503 pfd
.any_fn
= any_fn
;
9505 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9507 /* Walk the tree. (Conceptually, we would like to walk without
9508 duplicates, but for_each_template_parm_r recursively calls
9509 for_each_template_parm, so we would need to reorganize a fair
9510 bit to use walk_tree_without_duplicates, so we keep our own
9513 pfd
.visited
= visited
;
9515 pfd
.visited
= new hash_set
<tree
>;
9516 result
= cp_walk_tree (&t
,
9517 for_each_template_parm_r
,
9531 /* Returns true if T depends on any template parameter. */
9534 uses_template_parms (tree t
)
9540 int saved_processing_template_decl
;
9542 saved_processing_template_decl
= processing_template_decl
;
9543 if (!saved_processing_template_decl
)
9544 processing_template_decl
= 1;
9546 dependent_p
= dependent_type_p (t
);
9547 else if (TREE_CODE (t
) == TREE_VEC
)
9548 dependent_p
= any_dependent_template_arguments_p (t
);
9549 else if (TREE_CODE (t
) == TREE_LIST
)
9550 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9551 || uses_template_parms (TREE_CHAIN (t
)));
9552 else if (TREE_CODE (t
) == TYPE_DECL
)
9553 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9556 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9557 || TREE_CODE (t
) == OVERLOAD
9560 || TREE_CODE (t
) == TRAIT_EXPR
9561 || TREE_CODE (t
) == CONSTRUCTOR
9562 || CONSTANT_CLASS_P (t
))
9563 dependent_p
= (type_dependent_expression_p (t
)
9564 || value_dependent_expression_p (t
));
9567 gcc_assert (t
== error_mark_node
);
9568 dependent_p
= false;
9571 processing_template_decl
= saved_processing_template_decl
;
9576 /* Returns true iff current_function_decl is an incompletely instantiated
9577 template. Useful instead of processing_template_decl because the latter
9578 is set to 0 during instantiate_non_dependent_expr. */
9581 in_template_function (void)
9583 tree fn
= current_function_decl
;
9585 ++processing_template_decl
;
9586 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9587 && DECL_TEMPLATE_INFO (fn
)
9588 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9589 --processing_template_decl
;
9593 /* Returns true if T depends on any template parameter with level LEVEL. */
9596 uses_template_parms_level (tree t
, int level
)
9598 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9599 /*include_nondeduced_p=*/true);
9602 /* Returns true if the signature of DECL depends on any template parameter from
9603 its enclosing class. */
9606 uses_outer_template_parms (tree decl
)
9608 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9611 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9612 &depth
, NULL
, /*include_nondeduced_p=*/true))
9614 if (PRIMARY_TEMPLATE_P (decl
)
9615 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9616 (DECL_TEMPLATE_PARMS (decl
)),
9617 template_parm_outer_level
,
9618 &depth
, NULL
, /*include_nondeduced_p=*/true))
9620 tree ci
= get_constraints (decl
);
9622 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9623 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9624 &depth
, NULL
, /*nondeduced*/true))
9629 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9630 ill-formed translation unit, i.e. a variable or function that isn't
9631 usable in a constant expression. */
9634 neglectable_inst_p (tree d
)
9637 && !undeduced_auto_decl (d
)
9638 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9639 : decl_maybe_constant_var_p (d
)));
9642 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9643 neglectable and instantiated from within an erroneous instantiation. */
9646 limit_bad_template_recursion (tree decl
)
9648 struct tinst_level
*lev
= current_tinst_level
;
9649 int errs
= errorcount
+ sorrycount
;
9650 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9653 for (; lev
; lev
= lev
->next
)
9654 if (neglectable_inst_p (lev
->decl
))
9657 return (lev
&& errs
> lev
->errors
);
9660 static int tinst_depth
;
9661 extern int max_tinst_depth
;
9664 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9666 /* We're starting to instantiate D; record the template instantiation context
9667 for diagnostics and to restore it later. */
9670 push_tinst_level (tree d
)
9672 return push_tinst_level_loc (d
, input_location
);
9675 /* We're starting to instantiate D; record the template instantiation context
9676 at LOC for diagnostics and to restore it later. */
9679 push_tinst_level_loc (tree d
, location_t loc
)
9681 struct tinst_level
*new_level
;
9683 if (tinst_depth
>= max_tinst_depth
)
9685 /* Tell error.c not to try to instantiate any templates. */
9687 fatal_error (input_location
,
9688 "template instantiation depth exceeds maximum of %d"
9689 " (use -ftemplate-depth= to increase the maximum)",
9694 /* If the current instantiation caused problems, don't let it instantiate
9695 anything else. Do allow deduction substitution and decls usable in
9696 constant expressions. */
9697 if (limit_bad_template_recursion (d
))
9700 /* When not -quiet, dump template instantiations other than functions, since
9701 announce_function will take care of those. */
9703 && TREE_CODE (d
) != TREE_LIST
9704 && TREE_CODE (d
) != FUNCTION_DECL
)
9705 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9707 new_level
= ggc_alloc
<tinst_level
> ();
9708 new_level
->decl
= d
;
9709 new_level
->locus
= loc
;
9710 new_level
->errors
= errorcount
+sorrycount
;
9711 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9712 new_level
->next
= current_tinst_level
;
9713 current_tinst_level
= new_level
;
9716 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9717 depth_reached
= tinst_depth
;
9722 /* We're done instantiating this template; return to the instantiation
9726 pop_tinst_level (void)
9728 /* Restore the filename and line number stashed away when we started
9729 this instantiation. */
9730 input_location
= current_tinst_level
->locus
;
9731 current_tinst_level
= current_tinst_level
->next
;
9735 /* We're instantiating a deferred template; restore the template
9736 instantiation context in which the instantiation was requested, which
9737 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9740 reopen_tinst_level (struct tinst_level
*level
)
9742 struct tinst_level
*t
;
9745 for (t
= level
; t
; t
= t
->next
)
9748 current_tinst_level
= level
;
9750 if (current_tinst_level
)
9751 current_tinst_level
->errors
= errorcount
+sorrycount
;
9755 /* Returns the TINST_LEVEL which gives the original instantiation
9758 struct tinst_level
*
9759 outermost_tinst_level (void)
9761 struct tinst_level
*level
= current_tinst_level
;
9764 level
= level
->next
;
9768 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9769 vector of template arguments, as for tsubst.
9771 Returns an appropriate tsubst'd friend declaration. */
9774 tsubst_friend_function (tree decl
, tree args
)
9778 if (TREE_CODE (decl
) == FUNCTION_DECL
9779 && DECL_TEMPLATE_INSTANTIATION (decl
)
9780 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9781 /* This was a friend declared with an explicit template
9782 argument list, e.g.:
9786 to indicate that f was a template instantiation, not a new
9787 function declaration. Now, we have to figure out what
9788 instantiation of what template. */
9790 tree template_id
, arglist
, fns
;
9793 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9795 /* Friend functions are looked up in the containing namespace scope.
9796 We must enter that scope, to avoid finding member functions of the
9797 current class with same name. */
9798 push_nested_namespace (ns
);
9799 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9800 tf_warning_or_error
, NULL_TREE
,
9801 /*integral_constant_expression_p=*/false);
9802 pop_nested_namespace (ns
);
9803 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9804 tf_warning_or_error
, NULL_TREE
);
9805 template_id
= lookup_template_function (fns
, arglist
);
9807 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9808 tmpl
= determine_specialization (template_id
, new_friend
,
9810 /*need_member_template=*/0,
9811 TREE_VEC_LENGTH (args
),
9813 return instantiate_template (tmpl
, new_args
, tf_error
);
9816 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9818 /* The NEW_FRIEND will look like an instantiation, to the
9819 compiler, but is not an instantiation from the point of view of
9820 the language. For example, we might have had:
9822 template <class T> struct S {
9823 template <class U> friend void f(T, U);
9826 Then, in S<int>, template <class U> void f(int, U) is not an
9827 instantiation of anything. */
9828 if (new_friend
== error_mark_node
)
9829 return error_mark_node
;
9831 DECL_USE_TEMPLATE (new_friend
) = 0;
9832 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9834 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9835 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9836 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9839 /* The mangled name for the NEW_FRIEND is incorrect. The function
9840 is not a template instantiation and should not be mangled like
9841 one. Therefore, we forget the mangling here; we'll recompute it
9842 later if we need it. */
9843 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9845 SET_DECL_RTL (new_friend
, NULL
);
9846 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9849 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9852 tree new_friend_template_info
;
9853 tree new_friend_result_template_info
;
9855 int new_friend_is_defn
;
9857 /* We must save some information from NEW_FRIEND before calling
9858 duplicate decls since that function will free NEW_FRIEND if
9860 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9861 new_friend_is_defn
=
9862 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9863 (template_for_substitution (new_friend
)))
9865 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9867 /* This declaration is a `primary' template. */
9868 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9870 new_friend_result_template_info
9871 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9874 new_friend_result_template_info
= NULL_TREE
;
9876 /* Inside pushdecl_namespace_level, we will push into the
9877 current namespace. However, the friend function should go
9878 into the namespace of the template. */
9879 ns
= decl_namespace_context (new_friend
);
9880 push_nested_namespace (ns
);
9881 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9882 pop_nested_namespace (ns
);
9884 if (old_decl
== error_mark_node
)
9885 return error_mark_node
;
9887 if (old_decl
!= new_friend
)
9889 /* This new friend declaration matched an existing
9890 declaration. For example, given:
9892 template <class T> void f(T);
9893 template <class U> class C {
9894 template <class T> friend void f(T) {}
9897 the friend declaration actually provides the definition
9898 of `f', once C has been instantiated for some type. So,
9899 old_decl will be the out-of-class template declaration,
9900 while new_friend is the in-class definition.
9902 But, if `f' was called before this point, the
9903 instantiation of `f' will have DECL_TI_ARGS corresponding
9904 to `T' but not to `U', references to which might appear
9905 in the definition of `f'. Previously, the most general
9906 template for an instantiation of `f' was the out-of-class
9907 version; now it is the in-class version. Therefore, we
9908 run through all specialization of `f', adding to their
9909 DECL_TI_ARGS appropriately. In particular, they need a
9910 new set of outer arguments, corresponding to the
9911 arguments for this class instantiation.
9913 The same situation can arise with something like this:
9916 template <class T> class C {
9920 when `C<int>' is instantiated. Now, `f(int)' is defined
9923 if (!new_friend_is_defn
)
9924 /* On the other hand, if the in-class declaration does
9925 *not* provide a definition, then we don't want to alter
9926 existing definitions. We can just leave everything
9931 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9932 tree new_args
= TI_ARGS (new_friend_template_info
);
9934 /* Overwrite whatever template info was there before, if
9935 any, with the new template information pertaining to
9937 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9939 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9941 /* We should have called reregister_specialization in
9943 gcc_assert (retrieve_specialization (new_template
,
9947 /* Instantiate it if the global has already been used. */
9948 if (DECL_ODR_USED (old_decl
))
9949 instantiate_decl (old_decl
, /*defer_ok=*/true,
9950 /*expl_inst_class_mem_p=*/false);
9956 /* Indicate that the old function template is a partial
9958 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9959 = new_friend_result_template_info
;
9961 gcc_assert (new_template
9962 == most_general_template (new_template
));
9963 gcc_assert (new_template
!= old_decl
);
9965 /* Reassign any specializations already in the hash table
9966 to the new more general template, and add the
9967 additional template args. */
9968 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9972 tree spec
= TREE_VALUE (t
);
9975 elt
.tmpl
= old_decl
;
9976 elt
.args
= DECL_TI_ARGS (spec
);
9977 elt
.spec
= NULL_TREE
;
9979 decl_specializations
->remove_elt (&elt
);
9982 = add_outermost_template_args (new_args
,
9983 DECL_TI_ARGS (spec
));
9985 register_specialization
9986 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9989 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9993 /* The information from NEW_FRIEND has been merged into OLD_DECL
9994 by duplicate_decls. */
9995 new_friend
= old_decl
;
10000 tree context
= DECL_CONTEXT (new_friend
);
10004 template <class T> class C {
10005 template <class U> friend void C1<U>::f (); // case 1
10006 friend void C2<T>::f (); // case 2
10008 we only need to make sure CONTEXT is a complete type for
10009 case 2. To distinguish between the two cases, we note that
10010 CONTEXT of case 1 remains dependent type after tsubst while
10011 this isn't true for case 2. */
10012 ++processing_template_decl
;
10013 dependent_p
= dependent_type_p (context
);
10014 --processing_template_decl
;
10017 && !complete_type_or_else (context
, NULL_TREE
))
10018 return error_mark_node
;
10020 if (COMPLETE_TYPE_P (context
))
10022 tree fn
= new_friend
;
10023 /* do_friend adds the TEMPLATE_DECL for any member friend
10024 template even if it isn't a member template, i.e.
10025 template <class T> friend A<T>::f();
10026 Look through it in that case. */
10027 if (TREE_CODE (fn
) == TEMPLATE_DECL
10028 && !PRIMARY_TEMPLATE_P (fn
))
10029 fn
= DECL_TEMPLATE_RESULT (fn
);
10030 /* Check to see that the declaration is really present, and,
10031 possibly obtain an improved declaration. */
10032 fn
= check_classfn (context
, fn
, NULL_TREE
);
10042 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10043 template arguments, as for tsubst.
10045 Returns an appropriate tsubst'd friend type or error_mark_node on
10049 tsubst_friend_class (tree friend_tmpl
, tree args
)
10053 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10055 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10056 return TREE_TYPE (tmpl
);
10059 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10060 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10061 push_nested_namespace (context
);
10063 push_nested_class (context
);
10065 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10066 /*non_class=*/false, /*block_p=*/false,
10067 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10069 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10071 /* The friend template has already been declared. Just
10072 check to see that the declarations match, and install any new
10073 default parameters. We must tsubst the default parameters,
10074 of course. We only need the innermost template parameters
10075 because that is all that redeclare_class_template will look
10077 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10078 > TMPL_ARGS_DEPTH (args
))
10080 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10081 args
, tf_warning_or_error
);
10082 location_t saved_input_location
= input_location
;
10083 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10084 tree cons
= get_constraints (tmpl
);
10085 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10086 input_location
= saved_input_location
;
10091 /* The friend template has not already been declared. In this
10092 case, the instantiation of the template class will cause the
10093 injection of this template into the namespace scope. */
10094 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10096 if (tmpl
!= error_mark_node
)
10098 /* The new TMPL is not an instantiation of anything, so we
10099 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10100 for the new type because that is supposed to be the
10101 corresponding template decl, i.e., TMPL. */
10102 DECL_USE_TEMPLATE (tmpl
) = 0;
10103 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10104 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10105 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10106 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10108 /* It is hidden. */
10109 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10110 DECL_ANTICIPATED (tmpl
)
10111 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10113 /* Inject this template into the enclosing namspace scope. */
10114 tmpl
= pushdecl_namespace_level (tmpl
, true);
10118 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10119 pop_nested_namespace (context
);
10121 pop_nested_class ();
10123 return TREE_TYPE (tmpl
);
10126 /* Returns zero if TYPE cannot be completed later due to circularity.
10127 Otherwise returns one. */
10130 can_complete_type_without_circularity (tree type
)
10132 if (type
== NULL_TREE
|| type
== error_mark_node
)
10134 else if (COMPLETE_TYPE_P (type
))
10136 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10137 return can_complete_type_without_circularity (TREE_TYPE (type
));
10138 else if (CLASS_TYPE_P (type
)
10139 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10145 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10146 tsubst_flags_t
, tree
);
10148 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10149 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10152 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10153 tsubst_flags_t complain
, tree in_decl
)
10155 gcc_assert (ATTR_IS_DEPENDENT (t
));
10157 tree val
= TREE_VALUE (t
);
10158 if (val
== NULL_TREE
)
10159 /* Nothing to do. */;
10160 else if ((flag_openmp
|| flag_openmp_simd
)
10161 && is_attribute_p ("omp declare simd",
10162 get_attribute_name (t
)))
10164 tree clauses
= TREE_VALUE (val
);
10165 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10166 complain
, in_decl
);
10167 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10168 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10169 tree parms
= DECL_ARGUMENTS (*decl_p
);
10171 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10173 val
= build_tree_list (NULL_TREE
, clauses
);
10177 /* If the first attribute argument is an identifier, don't
10178 pass it through tsubst. Attributes like mode, format,
10179 cleanup and several target specific attributes expect it
10181 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10184 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10185 /*integral_constant_expression_p=*/false);
10186 if (chain
!= TREE_CHAIN (val
))
10187 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10189 else if (PACK_EXPANSION_P (val
))
10191 /* An attribute pack expansion. */
10192 tree purp
= TREE_PURPOSE (t
);
10193 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10194 if (pack
== error_mark_node
)
10195 return error_mark_node
;
10196 int len
= TREE_VEC_LENGTH (pack
);
10197 tree list
= NULL_TREE
;
10199 for (int i
= 0; i
< len
; ++i
)
10201 tree elt
= TREE_VEC_ELT (pack
, i
);
10202 *q
= build_tree_list (purp
, elt
);
10203 q
= &TREE_CHAIN (*q
);
10208 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10209 /*integral_constant_expression_p=*/false);
10211 if (val
!= TREE_VALUE (t
))
10212 return build_tree_list (TREE_PURPOSE (t
), val
);
10216 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10217 unchanged or a new TREE_LIST chain. */
10220 tsubst_attributes (tree attributes
, tree args
,
10221 tsubst_flags_t complain
, tree in_decl
)
10223 tree last_dep
= NULL_TREE
;
10225 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10226 if (ATTR_IS_DEPENDENT (t
))
10229 attributes
= copy_list (attributes
);
10234 for (tree
*p
= &attributes
; *p
; )
10237 if (ATTR_IS_DEPENDENT (t
))
10239 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10244 p
= &TREE_CHAIN (*p
);
10246 *p
= TREE_CHAIN (t
);
10250 p
= &TREE_CHAIN (*p
);
10256 /* Apply any attributes which had to be deferred until instantiation
10257 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10258 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10261 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10262 tree args
, tsubst_flags_t complain
, tree in_decl
)
10264 tree last_dep
= NULL_TREE
;
10268 if (attributes
== NULL_TREE
)
10271 if (DECL_P (*decl_p
))
10273 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10275 p
= &DECL_ATTRIBUTES (*decl_p
);
10276 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10277 to our attributes parameter. */
10278 gcc_assert (*p
== attributes
);
10282 p
= &TYPE_ATTRIBUTES (*decl_p
);
10283 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10284 lookup_template_class_1, and should be preserved. */
10285 gcc_assert (*p
!= attributes
);
10287 p
= &TREE_CHAIN (*p
);
10290 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10291 if (ATTR_IS_DEPENDENT (t
))
10294 attributes
= copy_list (attributes
);
10301 tree late_attrs
= NULL_TREE
;
10302 tree
*q
= &late_attrs
;
10307 if (ATTR_IS_DEPENDENT (t
))
10309 *p
= TREE_CHAIN (t
);
10310 TREE_CHAIN (t
) = NULL_TREE
;
10311 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10313 q
= &TREE_CHAIN (*q
);
10317 p
= &TREE_CHAIN (t
);
10320 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10324 /* Perform (or defer) access check for typedefs that were referenced
10325 from within the template TMPL code.
10326 This is a subroutine of instantiate_decl and instantiate_class_template.
10327 TMPL is the template to consider and TARGS is the list of arguments of
10331 perform_typedefs_access_check (tree tmpl
, tree targs
)
10333 location_t saved_location
;
10335 qualified_typedef_usage_t
*iter
;
10338 || (!CLASS_TYPE_P (tmpl
)
10339 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10342 saved_location
= input_location
;
10343 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10345 tree type_decl
= iter
->typedef_decl
;
10346 tree type_scope
= iter
->context
;
10348 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10351 if (uses_template_parms (type_decl
))
10352 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10353 if (uses_template_parms (type_scope
))
10354 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10356 /* Make access check error messages point to the location
10357 of the use of the typedef. */
10358 input_location
= iter
->locus
;
10359 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10360 type_decl
, type_decl
,
10361 tf_warning_or_error
);
10363 input_location
= saved_location
;
10367 instantiate_class_template_1 (tree type
)
10369 tree templ
, args
, pattern
, t
, member
;
10373 unsigned int saved_maximum_field_alignment
;
10376 if (type
== error_mark_node
)
10377 return error_mark_node
;
10379 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10380 || uses_template_parms (type
))
10383 /* Figure out which template is being instantiated. */
10384 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10385 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10387 /* Mark the type as in the process of being defined. */
10388 TYPE_BEING_DEFINED (type
) = 1;
10390 /* Determine what specialization of the original template to
10392 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10393 if (t
== error_mark_node
)
10394 return error_mark_node
;
10397 /* This TYPE is actually an instantiation of a partial
10398 specialization. We replace the innermost set of ARGS with
10399 the arguments appropriate for substitution. For example,
10402 template <class T> struct S {};
10403 template <class T> struct S<T*> {};
10405 and supposing that we are instantiating S<int*>, ARGS will
10406 presently be {int*} -- but we need {int}. */
10407 pattern
= TREE_TYPE (t
);
10408 args
= TREE_PURPOSE (t
);
10412 pattern
= TREE_TYPE (templ
);
10413 args
= CLASSTYPE_TI_ARGS (type
);
10416 /* If the template we're instantiating is incomplete, then clearly
10417 there's nothing we can do. */
10418 if (!COMPLETE_TYPE_P (pattern
))
10420 /* We can try again later. */
10421 TYPE_BEING_DEFINED (type
) = 0;
10425 /* If we've recursively instantiated too many templates, stop. */
10426 if (! push_tinst_level (type
))
10429 /* We may be in the middle of deferred access check. Disable
10431 push_deferring_access_checks (dk_no_deferred
);
10433 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10434 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10436 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10437 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10438 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10439 fn_context
= error_mark_node
;
10441 push_to_top_level ();
10444 cp_unevaluated_operand
= 0;
10445 c_inhibit_evaluation_warnings
= 0;
10447 /* Use #pragma pack from the template context. */
10448 saved_maximum_field_alignment
= maximum_field_alignment
;
10449 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10451 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10453 /* Set the input location to the most specialized template definition.
10454 This is needed if tsubsting causes an error. */
10455 typedecl
= TYPE_MAIN_DECL (pattern
);
10456 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10457 DECL_SOURCE_LOCATION (typedecl
);
10459 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10460 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10461 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10462 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10463 if (ANON_AGGR_TYPE_P (pattern
))
10464 SET_ANON_AGGR_TYPE_P (type
);
10465 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10467 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10468 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10469 /* Adjust visibility for template arguments. */
10470 determine_visibility (TYPE_MAIN_DECL (type
));
10472 if (CLASS_TYPE_P (type
))
10473 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10475 pbinfo
= TYPE_BINFO (pattern
);
10477 /* We should never instantiate a nested class before its enclosing
10478 class; we need to look up the nested class by name before we can
10479 instantiate it, and that lookup should instantiate the enclosing
10481 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10482 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10484 base_list
= NULL_TREE
;
10485 if (BINFO_N_BASE_BINFOS (pbinfo
))
10491 /* We must enter the scope containing the type, as that is where
10492 the accessibility of types named in dependent bases are
10494 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10496 /* Substitute into each of the bases to determine the actual
10498 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10501 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10502 tree expanded_bases
= NULL_TREE
;
10505 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10508 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10509 args
, tf_error
, NULL_TREE
);
10510 if (expanded_bases
== error_mark_node
)
10513 len
= TREE_VEC_LENGTH (expanded_bases
);
10516 for (idx
= 0; idx
< len
; idx
++)
10518 if (expanded_bases
)
10519 /* Extract the already-expanded base class. */
10520 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10522 /* Substitute to figure out the base class. */
10523 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10526 if (base
== error_mark_node
)
10529 base_list
= tree_cons (access
, base
, base_list
);
10530 if (BINFO_VIRTUAL_P (pbase_binfo
))
10531 TREE_TYPE (base_list
) = integer_type_node
;
10535 /* The list is now in reverse order; correct that. */
10536 base_list
= nreverse (base_list
);
10539 pop_scope (pushed_scope
);
10541 /* Now call xref_basetypes to set up all the base-class
10543 xref_basetypes (type
, base_list
);
10545 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10546 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10547 args
, tf_error
, NULL_TREE
);
10548 fixup_attribute_variants (type
);
10550 /* Now that our base classes are set up, enter the scope of the
10551 class, so that name lookups into base classes, etc. will work
10552 correctly. This is precisely analogous to what we do in
10553 begin_class_definition when defining an ordinary non-template
10554 class, except we also need to push the enclosing classes. */
10555 push_nested_class (type
);
10557 /* Now members are processed in the order of declaration. */
10558 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10559 member
; member
= TREE_CHAIN (member
))
10561 tree t
= TREE_VALUE (member
);
10563 if (TREE_PURPOSE (member
))
10567 if (LAMBDA_TYPE_P (t
))
10568 /* A closure type for a lambda in an NSDMI or default argument.
10569 Ignore it; it will be regenerated when needed. */
10572 /* Build new CLASSTYPE_NESTED_UTDS. */
10575 bool class_template_p
;
10577 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10578 && TYPE_LANG_SPECIFIC (t
)
10579 && CLASSTYPE_IS_TEMPLATE (t
));
10580 /* If the member is a class template, then -- even after
10581 substitution -- there may be dependent types in the
10582 template argument list for the class. We increment
10583 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10584 that function will assume that no types are dependent
10585 when outside of a template. */
10586 if (class_template_p
)
10587 ++processing_template_decl
;
10588 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10589 if (class_template_p
)
10590 --processing_template_decl
;
10591 if (newtag
== error_mark_node
)
10594 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10596 tree name
= TYPE_IDENTIFIER (t
);
10598 if (class_template_p
)
10599 /* Unfortunately, lookup_template_class sets
10600 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10601 instantiation (i.e., for the type of a member
10602 template class nested within a template class.)
10603 This behavior is required for
10604 maybe_process_partial_specialization to work
10605 correctly, but is not accurate in this case;
10606 the TAG is not an instantiation of anything.
10607 (The corresponding TEMPLATE_DECL is an
10608 instantiation, but the TYPE is not.) */
10609 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10611 /* Now, we call pushtag to put this NEWTAG into the scope of
10612 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10613 pushtag calling push_template_decl. We don't have to do
10614 this for enums because it will already have been done in
10617 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10618 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10621 else if (DECL_DECLARES_FUNCTION_P (t
))
10625 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10626 ++processing_template_decl
;
10627 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10628 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10629 --processing_template_decl
;
10630 set_current_access_from_decl (r
);
10631 finish_member_declaration (r
);
10632 /* Instantiate members marked with attribute used. */
10633 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10635 if (TREE_CODE (r
) == FUNCTION_DECL
10636 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10637 cp_check_omp_declare_reduction (r
);
10639 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
10640 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10641 /* A closure type for a lambda in an NSDMI or default argument.
10642 Ignore it; it will be regenerated when needed. */;
10645 /* Build new TYPE_FIELDS. */
10646 if (TREE_CODE (t
) == STATIC_ASSERT
)
10650 ++c_inhibit_evaluation_warnings
;
10652 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10653 tf_warning_or_error
, NULL_TREE
,
10654 /*integral_constant_expression_p=*/true);
10655 --c_inhibit_evaluation_warnings
;
10657 finish_static_assert (condition
,
10658 STATIC_ASSERT_MESSAGE (t
),
10659 STATIC_ASSERT_SOURCE_LOCATION (t
),
10660 /*member_p=*/true);
10662 else if (TREE_CODE (t
) != CONST_DECL
)
10665 tree vec
= NULL_TREE
;
10668 /* The file and line for this declaration, to
10669 assist in error message reporting. Since we
10670 called push_tinst_level above, we don't need to
10672 input_location
= DECL_SOURCE_LOCATION (t
);
10674 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10675 ++processing_template_decl
;
10676 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10677 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10678 --processing_template_decl
;
10680 if (TREE_CODE (r
) == TREE_VEC
)
10682 /* A capture pack became multiple fields. */
10684 len
= TREE_VEC_LENGTH (vec
);
10687 for (int i
= 0; i
< len
; ++i
)
10690 r
= TREE_VEC_ELT (vec
, i
);
10695 [t]he initialization (and any associated
10696 side-effects) of a static data member does
10697 not occur unless the static data member is
10698 itself used in a way that requires the
10699 definition of the static data member to
10702 Therefore, we do not substitute into the
10703 initialized for the static data member here. */
10704 finish_static_data_member_decl
10706 /*init=*/NULL_TREE
,
10707 /*init_const_expr_p=*/false,
10708 /*asmspec_tree=*/NULL_TREE
,
10710 /* Instantiate members marked with attribute used. */
10711 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10714 else if (TREE_CODE (r
) == FIELD_DECL
)
10716 /* Determine whether R has a valid type and can be
10717 completed later. If R is invalid, then its type
10718 is replaced by error_mark_node. */
10719 tree rtype
= TREE_TYPE (r
);
10720 if (can_complete_type_without_circularity (rtype
))
10721 complete_type (rtype
);
10723 if (!complete_or_array_type_p (rtype
))
10725 /* If R's type couldn't be completed and
10726 it isn't a flexible array member (whose
10727 type is incomplete by definition) give
10729 cxx_incomplete_type_error (r
, rtype
);
10730 TREE_TYPE (r
) = error_mark_node
;
10734 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10735 such a thing will already have been added to the field
10736 list by tsubst_enum in finish_member_declaration in the
10737 CLASSTYPE_NESTED_UTDS case above. */
10738 if (!(TREE_CODE (r
) == TYPE_DECL
10739 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10740 && DECL_ARTIFICIAL (r
)))
10742 set_current_access_from_decl (r
);
10743 finish_member_declaration (r
);
10751 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10752 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10754 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10756 tree friend_type
= t
;
10757 bool adjust_processing_template_decl
= false;
10759 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10761 /* template <class T> friend class C; */
10762 friend_type
= tsubst_friend_class (friend_type
, args
);
10763 adjust_processing_template_decl
= true;
10765 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10767 /* template <class T> friend class C::D; */
10768 friend_type
= tsubst (friend_type
, args
,
10769 tf_warning_or_error
, NULL_TREE
);
10770 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10771 friend_type
= TREE_TYPE (friend_type
);
10772 adjust_processing_template_decl
= true;
10774 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10775 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10777 /* This could be either
10781 when dependent_type_p is false or
10783 template <class U> friend class T::C;
10786 /* Bump processing_template_decl in case this is something like
10787 template <class T> friend struct A<T>::B. */
10788 ++processing_template_decl
;
10789 friend_type
= tsubst (friend_type
, args
,
10790 tf_warning_or_error
, NULL_TREE
);
10791 if (dependent_type_p (friend_type
))
10792 adjust_processing_template_decl
= true;
10793 --processing_template_decl
;
10795 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10796 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10797 && TYPE_HIDDEN_P (friend_type
))
10801 where C hasn't been declared yet. Let's lookup name
10802 from namespace scope directly, bypassing any name that
10803 come from dependent base class. */
10804 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10806 /* The call to xref_tag_from_type does injection for friend
10808 push_nested_namespace (ns
);
10810 xref_tag_from_type (friend_type
, NULL_TREE
,
10811 /*tag_scope=*/ts_current
);
10812 pop_nested_namespace (ns
);
10814 else if (uses_template_parms (friend_type
))
10815 /* friend class C<T>; */
10816 friend_type
= tsubst (friend_type
, args
,
10817 tf_warning_or_error
, NULL_TREE
);
10822 where C is already declared or
10824 friend class C<int>;
10826 We don't have to do anything in these cases. */
10828 if (adjust_processing_template_decl
)
10829 /* Trick make_friend_class into realizing that the friend
10830 we're adding is a template, not an ordinary class. It's
10831 important that we use make_friend_class since it will
10832 perform some error-checking and output cross-reference
10834 ++processing_template_decl
;
10836 if (friend_type
!= error_mark_node
)
10837 make_friend_class (type
, friend_type
, /*complain=*/false);
10839 if (adjust_processing_template_decl
)
10840 --processing_template_decl
;
10844 /* Build new DECL_FRIENDLIST. */
10847 /* The file and line for this declaration, to
10848 assist in error message reporting. Since we
10849 called push_tinst_level above, we don't need to
10851 input_location
= DECL_SOURCE_LOCATION (t
);
10853 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10855 ++processing_template_decl
;
10856 push_deferring_access_checks (dk_no_check
);
10859 r
= tsubst_friend_function (t
, args
);
10860 add_friend (type
, r
, /*complain=*/false);
10861 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10863 pop_deferring_access_checks ();
10864 --processing_template_decl
;
10872 /* Restore these before substituting into the lambda capture
10874 cp_unevaluated_operand
= saved_unevaluated_operand
;
10875 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10878 /* Set the file and line number information to whatever is given for
10879 the class itself. This puts error messages involving generated
10880 implicit functions at a predictable point, and the same point
10881 that would be used for non-template classes. */
10882 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10884 unreverse_member_declarations (type
);
10885 finish_struct_1 (type
);
10886 TYPE_BEING_DEFINED (type
) = 0;
10888 /* We don't instantiate default arguments for member functions. 14.7.1:
10890 The implicit instantiation of a class template specialization causes
10891 the implicit instantiation of the declarations, but not of the
10892 definitions or default arguments, of the class member functions,
10893 member classes, static data members and member templates.... */
10895 /* Some typedefs referenced from within the template code need to be access
10896 checked at template instantiation time, i.e now. These types were
10897 added to the template at parsing time. Let's get those and perform
10898 the access checks then. */
10899 perform_typedefs_access_check (pattern
, args
);
10900 perform_deferred_access_checks (tf_warning_or_error
);
10901 pop_nested_class ();
10902 maximum_field_alignment
= saved_maximum_field_alignment
;
10904 pop_from_top_level ();
10905 pop_deferring_access_checks ();
10906 pop_tinst_level ();
10908 /* The vtable for a template class can be emitted in any translation
10909 unit in which the class is instantiated. When there is no key
10910 method, however, finish_struct_1 will already have added TYPE to
10911 the keyed_classes. */
10912 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10913 vec_safe_push (keyed_classes
, type
);
10918 /* Wrapper for instantiate_class_template_1. */
10921 instantiate_class_template (tree type
)
10924 timevar_push (TV_TEMPLATE_INST
);
10925 ret
= instantiate_class_template_1 (type
);
10926 timevar_pop (TV_TEMPLATE_INST
);
10931 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10937 else if (TYPE_P (t
))
10938 r
= tsubst (t
, args
, complain
, in_decl
);
10941 if (!(complain
& tf_warning
))
10942 ++c_inhibit_evaluation_warnings
;
10943 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10944 /*integral_constant_expression_p=*/true);
10945 if (!(complain
& tf_warning
))
10946 --c_inhibit_evaluation_warnings
;
10951 /* Given a function parameter pack TMPL_PARM and some function parameters
10952 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10953 and set *SPEC_P to point at the next point in the list. */
10956 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10958 /* Collect all of the extra "packed" parameters into an
10961 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10962 tree spec_parm
= *spec_p
;
10965 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10967 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10970 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10971 parmvec
= make_tree_vec (len
);
10972 spec_parm
= *spec_p
;
10973 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10975 tree elt
= spec_parm
;
10976 if (DECL_PACK_P (elt
))
10977 elt
= make_pack_expansion (elt
);
10978 TREE_VEC_ELT (parmvec
, i
) = elt
;
10981 /* Build the argument packs. */
10982 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10983 *spec_p
= spec_parm
;
10988 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10989 NONTYPE_ARGUMENT_PACK. */
10992 make_fnparm_pack (tree spec_parm
)
10994 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10997 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10998 pack expansion with no extra args, 2 if it has extra args, or 0
10999 if it is not a pack expansion. */
11002 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11004 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11005 if (i
>= TREE_VEC_LENGTH (vec
))
11007 tree elt
= TREE_VEC_ELT (vec
, i
);
11009 /* A decl pack is itself an expansion. */
11010 elt
= TREE_TYPE (elt
);
11011 if (!PACK_EXPANSION_P (elt
))
11013 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11019 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11022 make_argument_pack_select (tree arg_pack
, unsigned index
)
11024 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11026 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11027 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11032 /* This is a subroutine of tsubst_pack_expansion.
11034 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11035 mechanism to store the (non complete list of) arguments of the
11036 substitution and return a non substituted pack expansion, in order
11037 to wait for when we have enough arguments to really perform the
11041 use_pack_expansion_extra_args_p (tree parm_packs
,
11043 bool has_empty_arg
)
11045 /* If one pack has an expansion and another pack has a normal
11046 argument or if one pack has an empty argument and an another
11047 one hasn't then tsubst_pack_expansion cannot perform the
11048 substitution and need to fall back on the
11049 PACK_EXPANSION_EXTRA mechanism. */
11050 if (parm_packs
== NULL_TREE
)
11052 else if (has_empty_arg
)
11055 bool has_expansion_arg
= false;
11056 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11058 bool has_non_expansion_arg
= false;
11059 for (tree parm_pack
= parm_packs
;
11061 parm_pack
= TREE_CHAIN (parm_pack
))
11063 tree arg
= TREE_VALUE (parm_pack
);
11065 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11067 /* We can't substitute a pack expansion with extra args into
11071 has_expansion_arg
= true;
11073 has_non_expansion_arg
= true;
11076 if (has_expansion_arg
&& has_non_expansion_arg
)
11082 /* [temp.variadic]/6 says that:
11084 The instantiation of a pack expansion [...]
11085 produces a list E1,E2, ..., En, where N is the number of elements
11086 in the pack expansion parameters.
11088 This subroutine of tsubst_pack_expansion produces one of these Ei.
11090 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11091 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11092 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11093 INDEX is the index 'i' of the element Ei to produce. ARGS,
11094 COMPLAIN, and IN_DECL are the same parameters as for the
11095 tsubst_pack_expansion function.
11097 The function returns the resulting Ei upon successful completion,
11098 or error_mark_node.
11100 Note that this function possibly modifies the ARGS parameter, so
11101 it's the responsibility of the caller to restore it. */
11104 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11107 tree args
/* This parm gets
11109 tsubst_flags_t complain
,
11113 bool ith_elem_is_expansion
= false;
11115 /* For each parameter pack, change the substitution of the parameter
11116 pack to the ith argument in its argument pack, then expand the
11118 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11120 tree parm
= TREE_PURPOSE (pack
);
11121 tree arg_pack
= TREE_VALUE (pack
);
11122 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11124 ith_elem_is_expansion
|=
11125 argument_pack_element_is_expansion_p (arg_pack
, index
);
11127 /* Select the Ith argument from the pack. */
11128 if (TREE_CODE (parm
) == PARM_DECL
11130 || TREE_CODE (parm
) == FIELD_DECL
)
11134 aps
= make_argument_pack_select (arg_pack
, index
);
11135 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11136 return error_mark_node
;
11137 register_local_specialization (aps
, parm
);
11140 aps
= retrieve_local_specialization (parm
);
11145 template_parm_level_and_index (parm
, &level
, &idx
);
11149 aps
= make_argument_pack_select (arg_pack
, index
);
11150 /* Update the corresponding argument. */
11151 TMPL_ARG (args
, level
, idx
) = aps
;
11154 /* Re-use the ARGUMENT_PACK_SELECT. */
11155 aps
= TMPL_ARG (args
, level
, idx
);
11157 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11160 /* Substitute into the PATTERN with the (possibly altered)
11162 if (pattern
== in_decl
)
11163 /* Expanding a fixed parameter pack from
11164 coerce_template_parameter_pack. */
11165 t
= tsubst_decl (pattern
, args
, complain
);
11166 else if (pattern
== error_mark_node
)
11167 t
= error_mark_node
;
11168 else if (constraint_p (pattern
))
11170 if (processing_template_decl
)
11171 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11173 t
= (constraints_satisfied_p (pattern
, args
)
11174 ? boolean_true_node
: boolean_false_node
);
11176 else if (!TYPE_P (pattern
))
11177 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11178 /*integral_constant_expression_p=*/false);
11180 t
= tsubst (pattern
, args
, complain
, in_decl
);
11182 /* If the Ith argument pack element is a pack expansion, then
11183 the Ith element resulting from the substituting is going to
11184 be a pack expansion as well. */
11185 if (ith_elem_is_expansion
)
11186 t
= make_pack_expansion (t
, complain
);
11191 /* When the unexpanded parameter pack in a fold expression expands to an empty
11192 sequence, the value of the expression is as follows; the program is
11193 ill-formed if the operator is not listed in this table.
11200 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11202 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11203 if (!FOLD_EXPR_MODIFY_P (t
))
11206 case TRUTH_ANDIF_EXPR
:
11207 return boolean_true_node
;
11208 case TRUTH_ORIF_EXPR
:
11209 return boolean_false_node
;
11210 case COMPOUND_EXPR
:
11216 if (complain
& tf_error
)
11217 error_at (location_of (t
),
11218 "fold of empty expansion over %O", code
);
11219 return error_mark_node
;
11222 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11223 form an expression that combines the two terms using the
11227 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11229 tree op
= FOLD_EXPR_OP (t
);
11230 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11232 // Handle compound assignment operators.
11233 if (FOLD_EXPR_MODIFY_P (t
))
11234 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11238 case COMPOUND_EXPR
:
11239 return build_x_compound_expr (input_location
, left
, right
, complain
);
11241 return build_m_component_ref (left
, right
, complain
);
11243 return build_x_binary_op (input_location
, code
,
11244 left
, TREE_CODE (left
),
11245 right
, TREE_CODE (right
),
11251 /* Substitute ARGS into the pack of a fold expression T. */
11254 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11256 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11259 /* Substitute ARGS into the pack of a fold expression T. */
11262 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11264 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11267 /* Expand a PACK of arguments into a grouped as left fold.
11268 Given a pack containing elements A0, A1, ..., An and an
11269 operator @, this builds the expression:
11271 ((A0 @ A1) @ A2) ... @ An
11273 Note that PACK must not be empty.
11275 The operator is defined by the original fold expression T. */
11278 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11280 tree left
= TREE_VEC_ELT (pack
, 0);
11281 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11283 tree right
= TREE_VEC_ELT (pack
, i
);
11284 left
= fold_expression (t
, left
, right
, complain
);
11289 /* Substitute into a unary left fold expression. */
11292 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11295 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11296 if (pack
== error_mark_node
)
11297 return error_mark_node
;
11298 if (PACK_EXPANSION_P (pack
))
11300 tree r
= copy_node (t
);
11301 FOLD_EXPR_PACK (r
) = pack
;
11304 if (TREE_VEC_LENGTH (pack
) == 0)
11305 return expand_empty_fold (t
, complain
);
11307 return expand_left_fold (t
, pack
, complain
);
11310 /* Substitute into a binary left fold expression.
11312 Do ths by building a single (non-empty) vector of argumnts and
11313 building the expression from those elements. */
11316 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11319 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11320 if (pack
== error_mark_node
)
11321 return error_mark_node
;
11322 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11323 if (init
== error_mark_node
)
11324 return error_mark_node
;
11326 if (PACK_EXPANSION_P (pack
))
11328 tree r
= copy_node (t
);
11329 FOLD_EXPR_PACK (r
) = pack
;
11330 FOLD_EXPR_INIT (r
) = init
;
11334 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11335 TREE_VEC_ELT (vec
, 0) = init
;
11336 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11337 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11339 return expand_left_fold (t
, vec
, complain
);
11342 /* Expand a PACK of arguments into a grouped as right fold.
11343 Given a pack containing elementns A0, A1, ..., and an
11344 operator @, this builds the expression:
11346 A0@ ... (An-2 @ (An-1 @ An))
11348 Note that PACK must not be empty.
11350 The operator is defined by the original fold expression T. */
11353 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11355 // Build the expression.
11356 int n
= TREE_VEC_LENGTH (pack
);
11357 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11358 for (--n
; n
!= 0; --n
)
11360 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11361 right
= fold_expression (t
, left
, right
, complain
);
11366 /* Substitute into a unary right fold expression. */
11369 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11372 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11373 if (pack
== error_mark_node
)
11374 return error_mark_node
;
11375 if (PACK_EXPANSION_P (pack
))
11377 tree r
= copy_node (t
);
11378 FOLD_EXPR_PACK (r
) = pack
;
11381 if (TREE_VEC_LENGTH (pack
) == 0)
11382 return expand_empty_fold (t
, complain
);
11384 return expand_right_fold (t
, pack
, complain
);
11387 /* Substitute into a binary right fold expression.
11389 Do ths by building a single (non-empty) vector of arguments and
11390 building the expression from those elements. */
11393 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11396 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11397 if (pack
== error_mark_node
)
11398 return error_mark_node
;
11399 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11400 if (init
== error_mark_node
)
11401 return error_mark_node
;
11403 if (PACK_EXPANSION_P (pack
))
11405 tree r
= copy_node (t
);
11406 FOLD_EXPR_PACK (r
) = pack
;
11407 FOLD_EXPR_INIT (r
) = init
;
11411 int n
= TREE_VEC_LENGTH (pack
);
11412 tree vec
= make_tree_vec (n
+ 1);
11413 for (int i
= 0; i
< n
; ++i
)
11414 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11415 TREE_VEC_ELT (vec
, n
) = init
;
11417 return expand_right_fold (t
, vec
, complain
);
11420 /* Walk through the pattern of a pack expansion, adding everything in
11421 local_specializations to a list. */
11426 tsubst_flags_t complain
;
11429 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11431 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11432 tree
*extra
= &data
.extra
;
11433 tsubst_flags_t complain
= data
.complain
;
11434 if (tree spec
= retrieve_local_specialization (*tp
))
11436 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11438 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11439 tree args
= ARGUMENT_PACK_ARGS (spec
);
11440 if (TREE_VEC_LENGTH (args
) == 1)
11442 tree elt
= TREE_VEC_ELT (args
, 0);
11443 if (PACK_EXPANSION_P (elt
))
11444 elt
= PACK_EXPANSION_PATTERN (elt
);
11445 if (DECL_PACK_P (elt
))
11448 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11450 /* Handle lambda capture here, since we aren't doing any
11451 substitution now, and so tsubst_copy won't call
11452 process_outer_var_ref. */
11453 tree args
= ARGUMENT_PACK_ARGS (spec
);
11454 int len
= TREE_VEC_LENGTH (args
);
11455 for (int i
= 0; i
< len
; ++i
)
11457 tree arg
= TREE_VEC_ELT (args
, i
);
11459 if (outer_automatic_var_p (arg
))
11460 carg
= process_outer_var_ref (arg
, complain
);
11463 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11467 spec
= copy_node (spec
);
11468 args
= copy_node (args
);
11469 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11470 register_local_specialization (spec
, *tp
);
11472 TREE_VEC_ELT (args
, i
) = carg
;
11477 if (outer_automatic_var_p (spec
))
11478 spec
= process_outer_var_ref (spec
, complain
);
11479 *extra
= tree_cons (*tp
, spec
, *extra
);
11484 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11486 el_data data
= { NULL_TREE
, complain
};
11487 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
11491 /* Substitute ARGS into T, which is an pack expansion
11492 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11493 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11494 (if only a partial substitution could be performed) or
11495 ERROR_MARK_NODE if there was an error. */
11497 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11501 tree pack
, packs
= NULL_TREE
;
11502 bool unsubstituted_packs
= false;
11503 bool unsubstituted_fn_pack
= false;
11506 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11507 bool need_local_specializations
= false;
11510 gcc_assert (PACK_EXPANSION_P (t
));
11511 pattern
= PACK_EXPANSION_PATTERN (t
);
11513 /* Add in any args remembered from an earlier partial instantiation. */
11514 tree extra
= PACK_EXPANSION_EXTRA_ARGS (t
);
11515 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
11517 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
11519 /* The partial instantiation involved local declarations collected in
11520 extract_local_specs; map from the general template to our local
11522 tree gen
= TREE_PURPOSE (elt
);
11523 tree inst
= TREE_VALUE (elt
);
11524 if (DECL_PACK_P (inst
))
11525 inst
= retrieve_local_specialization (inst
);
11526 /* else inst is already a full instantiation of the pack. */
11527 register_local_specialization (inst
, gen
);
11529 gcc_assert (!TREE_PURPOSE (extra
));
11530 extra
= TREE_VALUE (extra
);
11532 args
= add_to_template_args (extra
, args
);
11534 levels
= TMPL_ARGS_DEPTH (args
);
11536 /* Determine the argument packs that will instantiate the parameter
11537 packs used in the expansion expression. While we're at it,
11538 compute the number of arguments to be expanded and make sure it
11540 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11541 pack
= TREE_CHAIN (pack
))
11543 tree parm_pack
= TREE_VALUE (pack
);
11544 tree arg_pack
= NULL_TREE
;
11545 tree orig_arg
= NULL_TREE
;
11548 if (TREE_CODE (parm_pack
) == BASES
)
11550 gcc_assert (parm_pack
== pattern
);
11551 if (BASES_DIRECT (parm_pack
))
11552 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11553 args
, complain
, in_decl
, false));
11555 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11556 args
, complain
, in_decl
, false));
11558 else if (builtin_pack_call_p (parm_pack
))
11560 /* ??? Support use in other patterns. */
11561 gcc_assert (parm_pack
== pattern
);
11562 return expand_builtin_pack_call (parm_pack
, args
,
11563 complain
, in_decl
);
11565 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11567 /* We know we have correct local_specializations if this
11568 expansion is at function scope, or if we're dealing with a
11569 local parameter in a requires expression; for the latter,
11570 tsubst_requires_expr set it up appropriately. */
11571 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11572 arg_pack
= retrieve_local_specialization (parm_pack
);
11574 /* We can't rely on local_specializations for a parameter
11575 name used later in a function declaration (such as in a
11576 late-specified return type). Even if it exists, it might
11577 have the wrong value for a recursive call. */
11578 need_local_specializations
= true;
11582 /* This parameter pack was used in an unevaluated context. Just
11583 make a dummy decl, since it's only used for its type. */
11584 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11585 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11586 /* Partial instantiation of the parm_pack, we can't build
11587 up an argument pack yet. */
11588 arg_pack
= NULL_TREE
;
11590 arg_pack
= make_fnparm_pack (arg_pack
);
11592 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11593 /* This argument pack isn't fully instantiated yet. We set this
11594 flag rather than clear arg_pack because we do want to do the
11595 optimization below, and we don't want to substitute directly
11596 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11597 where it isn't expected). */
11598 unsubstituted_fn_pack
= true;
11600 else if (is_normal_capture_proxy (parm_pack
))
11602 arg_pack
= retrieve_local_specialization (parm_pack
);
11603 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11604 unsubstituted_fn_pack
= true;
11609 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11611 if (level
<= levels
)
11612 arg_pack
= TMPL_ARG (args
, level
, idx
);
11615 orig_arg
= arg_pack
;
11616 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11617 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11619 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11620 /* This can only happen if we forget to expand an argument
11621 pack somewhere else. Just return an error, silently. */
11623 result
= make_tree_vec (1);
11624 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11631 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11633 /* Don't bother trying to do a partial substitution with
11634 incomplete packs; we'll try again after deduction. */
11635 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11640 else if (len
!= my_len
11641 && !unsubstituted_fn_pack
)
11643 if (!(complain
& tf_error
))
11644 /* Fail quietly. */;
11645 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11646 error ("mismatched argument pack lengths while expanding %qT",
11649 error ("mismatched argument pack lengths while expanding %qE",
11651 return error_mark_node
;
11654 /* Keep track of the parameter packs and their corresponding
11656 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11657 TREE_TYPE (packs
) = orig_arg
;
11661 /* We can't substitute for this parameter pack. We use a flag as
11662 well as the missing_level counter because function parameter
11663 packs don't have a level. */
11664 gcc_assert (processing_template_decl
);
11665 unsubstituted_packs
= true;
11669 /* If the expansion is just T..., return the matching argument pack, unless
11670 we need to call convert_from_reference on all the elements. This is an
11671 important optimization; see c++/68422. */
11672 if (!unsubstituted_packs
11673 && TREE_PURPOSE (packs
) == pattern
)
11675 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11677 /* If the argument pack is a single pack expansion, pull it out. */
11678 if (TREE_VEC_LENGTH (args
) == 1
11679 && pack_expansion_args_count (args
))
11680 return TREE_VEC_ELT (args
, 0);
11682 /* Types need no adjustment, nor does sizeof..., and if we still have
11683 some pack expansion args we won't do anything yet. */
11684 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11685 || PACK_EXPANSION_SIZEOF_P (t
)
11686 || pack_expansion_args_count (args
))
11688 /* Also optimize expression pack expansions if we can tell that the
11689 elements won't have reference type. */
11690 tree type
= TREE_TYPE (pattern
);
11691 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11692 && !PACK_EXPANSION_P (type
)
11693 && !WILDCARD_TYPE_P (type
))
11695 /* Otherwise use the normal path so we get convert_from_reference. */
11698 /* We cannot expand this expansion expression, because we don't have
11699 all of the argument packs we need. */
11700 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11702 /* We got some full packs, but we can't substitute them in until we
11703 have values for all the packs. So remember these until then. */
11705 t
= make_pack_expansion (pattern
, complain
);
11707 if (local_specializations
)
11708 if (tree locals
= extract_local_specs (pattern
, complain
))
11709 extra
= tree_cons (NULL_TREE
, extra
, locals
);
11710 PACK_EXPANSION_EXTRA_ARGS (t
) = extra
;
11713 else if (unsubstituted_packs
)
11715 /* There were no real arguments, we're just replacing a parameter
11716 pack with another version of itself. Substitute into the
11717 pattern and return a PACK_EXPANSION_*. The caller will need to
11719 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11720 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11721 /*integral_constant_expression_p=*/false);
11723 t
= tsubst (pattern
, args
, complain
, in_decl
);
11724 t
= make_pack_expansion (t
, complain
);
11728 gcc_assert (len
>= 0);
11730 if (need_local_specializations
)
11732 /* We're in a late-specified return type, so create our own local
11733 specializations map; the current map is either NULL or (in the
11734 case of recursive unification) might have bindings that we don't
11735 want to use or alter. */
11736 saved_local_specializations
= local_specializations
;
11737 local_specializations
= new hash_map
<tree
, tree
>;
11740 /* For each argument in each argument pack, substitute into the
11742 result
= make_tree_vec (len
);
11743 tree elem_args
= copy_template_args (args
);
11744 for (i
= 0; i
< len
; ++i
)
11746 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11748 elem_args
, complain
,
11750 TREE_VEC_ELT (result
, i
) = t
;
11751 if (t
== error_mark_node
)
11753 result
= error_mark_node
;
11758 /* Update ARGS to restore the substitution from parameter packs to
11759 their argument packs. */
11760 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11762 tree parm
= TREE_PURPOSE (pack
);
11764 if (TREE_CODE (parm
) == PARM_DECL
11766 || TREE_CODE (parm
) == FIELD_DECL
)
11767 register_local_specialization (TREE_TYPE (pack
), parm
);
11772 if (TREE_VALUE (pack
) == NULL_TREE
)
11775 template_parm_level_and_index (parm
, &level
, &idx
);
11777 /* Update the corresponding argument. */
11778 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11779 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11782 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11786 if (need_local_specializations
)
11788 delete local_specializations
;
11789 local_specializations
= saved_local_specializations
;
11792 /* If the dependent pack arguments were such that we end up with only a
11793 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11794 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11795 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11796 return TREE_VEC_ELT (result
, 0);
11801 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11802 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11803 parameter packs; all parms generated from a function parameter pack will
11804 have the same DECL_PARM_INDEX. */
11807 get_pattern_parm (tree parm
, tree tmpl
)
11809 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11812 if (DECL_ARTIFICIAL (parm
))
11814 for (patparm
= DECL_ARGUMENTS (pattern
);
11815 patparm
; patparm
= DECL_CHAIN (patparm
))
11816 if (DECL_ARTIFICIAL (patparm
)
11817 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11822 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11823 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11824 gcc_assert (DECL_PARM_INDEX (patparm
)
11825 == DECL_PARM_INDEX (parm
));
11831 /* Make an argument pack out of the TREE_VEC VEC. */
11834 make_argument_pack (tree vec
)
11837 tree elt
= TREE_VEC_ELT (vec
, 0);
11839 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11842 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11843 TREE_CONSTANT (pack
) = 1;
11845 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11849 /* Return an exact copy of template args T that can be modified
11853 copy_template_args (tree t
)
11855 if (t
== error_mark_node
)
11858 int len
= TREE_VEC_LENGTH (t
);
11859 tree new_vec
= make_tree_vec (len
);
11861 for (int i
= 0; i
< len
; ++i
)
11863 tree elt
= TREE_VEC_ELT (t
, i
);
11864 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11865 elt
= copy_template_args (elt
);
11866 TREE_VEC_ELT (new_vec
, i
) = elt
;
11869 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11870 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11875 /* Substitute ARGS into the vector or list of template arguments T. */
11878 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11881 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11884 if (t
== error_mark_node
)
11885 return error_mark_node
;
11887 len
= TREE_VEC_LENGTH (t
);
11888 elts
= XALLOCAVEC (tree
, len
);
11890 for (i
= 0; i
< len
; i
++)
11892 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11895 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11896 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11897 else if (PACK_EXPANSION_P (orig_arg
))
11899 /* Substitute into an expansion expression. */
11900 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11902 if (TREE_CODE (new_arg
) == TREE_VEC
)
11903 /* Add to the expanded length adjustment the number of
11904 expanded arguments. We subtract one from this
11905 measurement, because the argument pack expression
11906 itself is already counted as 1 in
11907 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11908 the argument pack is empty. */
11909 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11911 else if (ARGUMENT_PACK_P (orig_arg
))
11913 /* Substitute into each of the arguments. */
11914 new_arg
= TYPE_P (orig_arg
)
11915 ? cxx_make_type (TREE_CODE (orig_arg
))
11916 : make_node (TREE_CODE (orig_arg
));
11918 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11919 args
, complain
, in_decl
);
11920 if (pack_args
== error_mark_node
)
11921 new_arg
= error_mark_node
;
11923 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11925 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11926 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11929 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11931 if (new_arg
== error_mark_node
)
11932 return error_mark_node
;
11935 if (new_arg
!= orig_arg
)
11942 /* Make space for the expanded arguments coming from template
11944 t
= make_tree_vec (len
+ expanded_len_adjust
);
11945 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11946 arguments for a member template.
11947 In that case each TREE_VEC in ORIG_T represents a level of template
11948 arguments, and ORIG_T won't carry any non defaulted argument count.
11949 It will rather be the nested TREE_VECs that will carry one.
11950 In other words, ORIG_T carries a non defaulted argument count only
11951 if it doesn't contain any nested TREE_VEC. */
11952 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11954 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11955 count
+= expanded_len_adjust
;
11956 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11958 for (i
= 0, out
= 0; i
< len
; i
++)
11960 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11961 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11962 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11966 /* Now expand the template argument pack "in place". */
11967 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11968 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11972 TREE_VEC_ELT (t
, out
) = elts
[i
];
11980 /* Substitute ARGS into one level PARMS of template parameters. */
11983 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11985 if (parms
== error_mark_node
)
11986 return error_mark_node
;
11988 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11990 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11992 tree tuple
= TREE_VEC_ELT (parms
, i
);
11994 if (tuple
== error_mark_node
)
11997 TREE_VEC_ELT (new_vec
, i
) =
11998 tsubst_template_parm (tuple
, args
, complain
);
12004 /* Return the result of substituting ARGS into the template parameters
12005 given by PARMS. If there are m levels of ARGS and m + n levels of
12006 PARMS, then the result will contain n levels of PARMS. For
12007 example, if PARMS is `template <class T> template <class U>
12008 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12009 result will be `template <int*, double, class V>'. */
12012 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12014 tree r
= NULL_TREE
;
12017 /* When substituting into a template, we must set
12018 PROCESSING_TEMPLATE_DECL as the template parameters may be
12019 dependent if they are based on one-another, and the dependency
12020 predicates are short-circuit outside of templates. */
12021 ++processing_template_decl
;
12023 for (new_parms
= &r
;
12024 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12025 new_parms
= &(TREE_CHAIN (*new_parms
)),
12026 parms
= TREE_CHAIN (parms
))
12028 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12031 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12032 - TMPL_ARGS_DEPTH (args
)),
12033 new_vec
, NULL_TREE
);
12036 --processing_template_decl
;
12041 /* Return the result of substituting ARGS into one template parameter
12042 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12043 parameter and which TREE_PURPOSE is the default argument of the
12044 template parameter. */
12047 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12049 tree default_value
, parm_decl
;
12051 if (args
== NULL_TREE
12053 || t
== error_mark_node
)
12056 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12058 default_value
= TREE_PURPOSE (t
);
12059 parm_decl
= TREE_VALUE (t
);
12061 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12062 if (TREE_CODE (parm_decl
) == PARM_DECL
12063 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12064 parm_decl
= error_mark_node
;
12065 default_value
= tsubst_template_arg (default_value
, args
,
12066 complain
, NULL_TREE
);
12068 return build_tree_list (default_value
, parm_decl
);
12071 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12072 type T. If T is not an aggregate or enumeration type, it is
12073 handled as if by tsubst. IN_DECL is as for tsubst. If
12074 ENTERING_SCOPE is nonzero, T is the context for a template which
12075 we are presently tsubst'ing. Return the substituted value. */
12078 tsubst_aggr_type (tree t
,
12080 tsubst_flags_t complain
,
12082 int entering_scope
)
12084 if (t
== NULL_TREE
)
12087 switch (TREE_CODE (t
))
12090 if (TYPE_PTRMEMFUNC_P (t
))
12091 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12093 /* Fall through. */
12094 case ENUMERAL_TYPE
:
12096 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12101 int saved_unevaluated_operand
;
12102 int saved_inhibit_evaluation_warnings
;
12104 /* In "sizeof(X<I>)" we need to evaluate "I". */
12105 saved_unevaluated_operand
= cp_unevaluated_operand
;
12106 cp_unevaluated_operand
= 0;
12107 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12108 c_inhibit_evaluation_warnings
= 0;
12110 /* First, determine the context for the type we are looking
12112 context
= TYPE_CONTEXT (t
);
12113 if (context
&& TYPE_P (context
))
12115 context
= tsubst_aggr_type (context
, args
, complain
,
12116 in_decl
, /*entering_scope=*/1);
12117 /* If context is a nested class inside a class template,
12118 it may still need to be instantiated (c++/33959). */
12119 context
= complete_type (context
);
12122 /* Then, figure out what arguments are appropriate for the
12123 type we are trying to find. For example, given:
12125 template <class T> struct S;
12126 template <class T, class U> void f(T, U) { S<U> su; }
12128 and supposing that we are instantiating f<int, double>,
12129 then our ARGS will be {int, double}, but, when looking up
12130 S we only want {double}. */
12131 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12132 complain
, in_decl
);
12133 if (argvec
== error_mark_node
)
12134 r
= error_mark_node
;
12137 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12138 entering_scope
, complain
);
12139 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12142 cp_unevaluated_operand
= saved_unevaluated_operand
;
12143 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12148 /* This is not a template type, so there's nothing to do. */
12152 return tsubst (t
, args
, complain
, in_decl
);
12156 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12158 /* Substitute into the default argument ARG (a default argument for
12159 FN), which has the indicated TYPE. */
12162 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12163 tsubst_flags_t complain
)
12165 tree saved_class_ptr
= NULL_TREE
;
12166 tree saved_class_ref
= NULL_TREE
;
12167 int errs
= errorcount
+ sorrycount
;
12169 /* This can happen in invalid code. */
12170 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12173 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12174 parm
= chain_index (parmnum
, parm
);
12175 tree parmtype
= TREE_TYPE (parm
);
12176 if (DECL_BY_REFERENCE (parm
))
12177 parmtype
= TREE_TYPE (parmtype
);
12178 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12181 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12184 /* This default argument came from a template. Instantiate the
12185 default argument here, not in tsubst. In the case of
12194 we must be careful to do name lookup in the scope of S<T>,
12195 rather than in the current class. */
12196 push_access_scope (fn
);
12197 /* The "this" pointer is not valid in a default argument. */
12200 saved_class_ptr
= current_class_ptr
;
12201 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12202 saved_class_ref
= current_class_ref
;
12203 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12206 start_lambda_scope (parm
);
12208 push_deferring_access_checks(dk_no_deferred
);
12209 /* The default argument expression may cause implicitly defined
12210 member functions to be synthesized, which will result in garbage
12211 collection. We must treat this situation as if we were within
12212 the body of function so as to avoid collecting live data on the
12215 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12216 complain
, NULL_TREE
,
12217 /*integral_constant_expression_p=*/false);
12219 pop_deferring_access_checks();
12221 finish_lambda_scope ();
12223 /* Restore the "this" pointer. */
12226 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12227 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12230 if (errorcount
+sorrycount
> errs
12231 && (complain
& tf_warning_or_error
))
12232 inform (input_location
,
12233 " when instantiating default argument for call to %qD", fn
);
12235 /* Make sure the default argument is reasonable. */
12236 arg
= check_default_argument (type
, arg
, complain
);
12238 pop_access_scope (fn
);
12240 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12243 defarg_inst
= tree_cache_map::create_ggc (37);
12244 defarg_inst
->put (parm
, arg
);
12250 /* Substitute into all the default arguments for FN. */
12253 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12258 tmpl_args
= DECL_TI_ARGS (fn
);
12260 /* If this function is not yet instantiated, we certainly don't need
12261 its default arguments. */
12262 if (uses_template_parms (tmpl_args
))
12264 /* Don't do this again for clones. */
12265 if (DECL_CLONED_FUNCTION_P (fn
))
12269 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12271 arg
= TREE_CHAIN (arg
), ++i
)
12272 if (TREE_PURPOSE (arg
))
12273 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12275 TREE_PURPOSE (arg
),
12279 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12282 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12283 tree lambda_fntype
)
12285 tree gen_tmpl
, argvec
;
12286 hashval_t hash
= 0;
12289 /* Nobody should be tsubst'ing into non-template functions. */
12290 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12292 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12294 /* If T is not dependent, just return it. */
12295 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12298 /* Calculate the most general template of which R is a
12300 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12302 /* We're substituting a lambda function under tsubst_lambda_expr but not
12303 directly from it; find the matching function we're already inside.
12304 But don't do this if T is a generic lambda with a single level of
12305 template parms, as in that case we're doing a normal instantiation. */
12306 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12307 && (!generic_lambda_fn_p (t
)
12308 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12309 return enclosing_instantiation_of (t
);
12311 /* Calculate the complete set of arguments used to
12313 argvec
= tsubst_template_args (DECL_TI_ARGS
12314 (DECL_TEMPLATE_RESULT
12315 (DECL_TI_TEMPLATE (t
))),
12316 args
, complain
, in_decl
);
12317 if (argvec
== error_mark_node
)
12318 return error_mark_node
;
12320 /* Check to see if we already have this specialization. */
12321 if (!lambda_fntype
)
12323 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12324 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12328 /* We can see more levels of arguments than parameters if
12329 there was a specialization of a member template, like
12332 template <class T> struct S { template <class U> void f(); }
12333 template <> template <class U> void S<int>::f(U);
12335 Here, we'll be substituting into the specialization,
12336 because that's where we can find the code we actually
12337 want to generate, but we'll have enough arguments for
12338 the most general template.
12340 We also deal with the peculiar case:
12342 template <class T> struct S {
12343 template <class U> friend void f();
12345 template <class U> void f() {}
12347 template void f<double>();
12349 Here, the ARGS for the instantiation of will be {int,
12350 double}. But, we only need as many ARGS as there are
12351 levels of template parameters in CODE_PATTERN. We are
12352 careful not to get fooled into reducing the ARGS in
12355 template <class T> struct S { template <class U> void f(U); }
12356 template <class T> template <> void S<T>::f(int) {}
12358 which we can spot because the pattern will be a
12359 specialization in this case. */
12360 int args_depth
= TMPL_ARGS_DEPTH (args
);
12362 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12364 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12365 args
= get_innermost_template_args (args
, parms_depth
);
12369 /* This special case arises when we have something like this:
12371 template <class T> struct S {
12372 friend void f<int>(int, double);
12375 Here, the DECL_TI_TEMPLATE for the friend declaration
12376 will be an IDENTIFIER_NODE. We are being called from
12377 tsubst_friend_function, and we want only to create a
12378 new decl (R) with appropriate types so that we can call
12379 determine_specialization. */
12380 gen_tmpl
= NULL_TREE
;
12381 argvec
= NULL_TREE
;
12384 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12386 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12387 bool member
= ctx
&& TYPE_P (ctx
);
12389 if (member
&& !closure
)
12390 ctx
= tsubst_aggr_type (ctx
, args
,
12391 complain
, t
, /*entering_scope=*/1);
12393 tree type
= (lambda_fntype
? lambda_fntype
12394 : tsubst (TREE_TYPE (t
), args
,
12395 complain
| tf_fndecl_type
, in_decl
));
12396 if (type
== error_mark_node
)
12397 return error_mark_node
;
12399 /* If we hit excessive deduction depth, the type is bogus even if
12400 it isn't error_mark_node, so don't build a decl. */
12401 if (excessive_deduction_depth
)
12402 return error_mark_node
;
12404 /* We do NOT check for matching decls pushed separately at this
12405 point, as they may not represent instantiations of this
12406 template, and in any case are considered separate under the
12408 tree r
= copy_decl (t
);
12409 DECL_USE_TEMPLATE (r
) = 0;
12410 TREE_TYPE (r
) = type
;
12411 /* Clear out the mangled name and RTL for the instantiation. */
12412 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12413 SET_DECL_RTL (r
, NULL
);
12414 /* Leave DECL_INITIAL set on deleted instantiations. */
12415 if (!DECL_DELETED_FN (r
))
12416 DECL_INITIAL (r
) = NULL_TREE
;
12417 DECL_CONTEXT (r
) = ctx
;
12419 /* OpenMP UDRs have the only argument a reference to the declared
12420 type. We want to diagnose if the declared type is a reference,
12421 which is invalid, but as references to references are usually
12422 quietly merged, diagnose it here. */
12423 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12426 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12427 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12428 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12429 error_at (DECL_SOURCE_LOCATION (t
),
12430 "reference type %qT in "
12431 "%<#pragma omp declare reduction%>", argtype
);
12432 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12433 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12437 if (member
&& DECL_CONV_FN_P (r
))
12438 /* Type-conversion operator. Reconstruct the name, in
12439 case it's the name of one of the template's parameters. */
12440 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12442 tree parms
= DECL_ARGUMENTS (t
);
12444 parms
= DECL_CHAIN (parms
);
12445 parms
= tsubst (parms
, args
, complain
, t
);
12446 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12447 DECL_CONTEXT (parm
) = r
;
12450 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12451 DECL_CHAIN (tparm
) = parms
;
12454 DECL_ARGUMENTS (r
) = parms
;
12455 DECL_RESULT (r
) = NULL_TREE
;
12457 TREE_STATIC (r
) = 0;
12458 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12459 DECL_EXTERNAL (r
) = 1;
12460 /* If this is an instantiation of a function with internal
12461 linkage, we already know what object file linkage will be
12462 assigned to the instantiation. */
12463 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12464 DECL_DEFER_OUTPUT (r
) = 0;
12465 DECL_CHAIN (r
) = NULL_TREE
;
12466 DECL_PENDING_INLINE_INFO (r
) = 0;
12467 DECL_PENDING_INLINE_P (r
) = 0;
12468 DECL_SAVED_TREE (r
) = NULL_TREE
;
12469 DECL_STRUCT_FUNCTION (r
) = NULL
;
12471 /* We'll re-clone as appropriate in instantiate_template. */
12472 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12474 /* If we aren't complaining now, return on error before we register
12475 the specialization so that we'll complain eventually. */
12476 if ((complain
& tf_error
) == 0
12477 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12478 && !grok_op_properties (r
, /*complain=*/false))
12479 return error_mark_node
;
12481 /* When instantiating a constrained member, substitute
12482 into the constraints to create a new constraint. */
12483 if (tree ci
= get_constraints (t
))
12486 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12487 set_constraints (r
, ci
);
12490 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12491 this in the special friend case mentioned above where
12492 GEN_TMPL is NULL. */
12493 if (gen_tmpl
&& !closure
)
12495 DECL_TEMPLATE_INFO (r
)
12496 = build_template_info (gen_tmpl
, argvec
);
12497 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12500 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12502 /* We instantiated this while substituting into
12503 the type earlier (template/friend54.C). */
12506 /* We're not supposed to instantiate default arguments
12507 until they are called, for a template. But, for a
12510 template <class T> void f ()
12511 { extern void g(int i = T()); }
12513 we should do the substitution when the template is
12514 instantiated. We handle the member function case in
12515 instantiate_class_template since the default arguments
12516 might refer to other members of the class. */
12518 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12519 && !uses_template_parms (argvec
))
12520 tsubst_default_arguments (r
, complain
);
12523 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12525 /* Copy the list of befriending classes. */
12526 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12528 friends
= &TREE_CHAIN (*friends
))
12530 *friends
= copy_node (*friends
);
12531 TREE_VALUE (*friends
)
12532 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12535 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12537 maybe_retrofit_in_chrg (r
);
12538 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12539 return error_mark_node
;
12540 /* If this is an instantiation of a member template, clone it.
12541 If it isn't, that'll be handled by
12542 clone_constructors_and_destructors. */
12543 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12544 clone_function_decl (r
, /*update_methods=*/false);
12546 else if ((complain
& tf_error
) != 0
12547 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12548 && !grok_op_properties (r
, /*complain=*/true))
12549 return error_mark_node
;
12551 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12552 SET_DECL_FRIEND_CONTEXT (r
,
12553 tsubst (DECL_FRIEND_CONTEXT (t
),
12554 args
, complain
, in_decl
));
12556 /* Possibly limit visibility based on template args. */
12557 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12558 if (DECL_VISIBILITY_SPECIFIED (t
))
12560 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12561 DECL_ATTRIBUTES (r
)
12562 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12564 determine_visibility (r
);
12565 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12566 && !processing_template_decl
)
12567 defaulted_late_check (r
);
12569 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12570 args
, complain
, in_decl
);
12574 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12577 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12578 tree lambda_fntype
)
12580 /* We can get here when processing a member function template,
12581 member class template, or template template parameter. */
12582 tree decl
= DECL_TEMPLATE_RESULT (t
);
12588 hashval_t hash
= 0;
12590 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12592 /* Template template parameter is treated here. */
12593 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12594 if (new_type
== error_mark_node
)
12595 r
= error_mark_node
;
12596 /* If we get a real template back, return it. This can happen in
12597 the context of most_specialized_partial_spec. */
12598 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12601 /* The new TEMPLATE_DECL was built in
12602 reduce_template_parm_level. */
12603 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12607 if (!lambda_fntype
)
12609 /* We might already have an instance of this template.
12610 The ARGS are for the surrounding class type, so the
12611 full args contain the tsubst'd args for the context,
12612 plus the innermost args from the template decl. */
12613 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12614 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12615 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12616 /* Because this is a template, the arguments will still be
12617 dependent, even after substitution. If
12618 PROCESSING_TEMPLATE_DECL is not set, the dependency
12619 predicates will short-circuit. */
12620 ++processing_template_decl
;
12621 full_args
= tsubst_template_args (tmpl_args
, args
,
12622 complain
, in_decl
);
12623 --processing_template_decl
;
12624 if (full_args
== error_mark_node
)
12625 return error_mark_node
;
12627 /* If this is a default template template argument,
12628 tsubst might not have changed anything. */
12629 if (full_args
== tmpl_args
)
12632 hash
= hash_tmpl_and_args (t
, full_args
);
12633 spec
= retrieve_specialization (t
, full_args
, hash
);
12634 if (spec
!= NULL_TREE
)
12638 /* Make a new template decl. It will be similar to the
12639 original, but will record the current template arguments.
12640 We also create a new function declaration, which is just
12641 like the old one, but points to this new template, rather
12642 than the old one. */
12644 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12645 DECL_CHAIN (r
) = NULL_TREE
;
12647 // Build new template info linking to the original template decl.
12648 if (!lambda_fntype
)
12650 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12651 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12654 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12656 /* The template parameters for this new template are all the
12657 template parameters for the old template, except the
12658 outermost level of parameters. */
12659 DECL_TEMPLATE_PARMS (r
)
12660 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12663 if (TREE_CODE (decl
) == TYPE_DECL
12664 && !TYPE_DECL_ALIAS_P (decl
))
12667 ++processing_template_decl
;
12668 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12669 --processing_template_decl
;
12670 if (new_type
== error_mark_node
)
12671 return error_mark_node
;
12673 TREE_TYPE (r
) = new_type
;
12674 /* For a partial specialization, we need to keep pointing to
12675 the primary template. */
12676 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12677 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12678 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12679 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12680 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12685 ++processing_template_decl
;
12686 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12687 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12689 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12690 --processing_template_decl
;
12691 if (new_decl
== error_mark_node
)
12692 return error_mark_node
;
12694 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12695 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12696 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12699 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12700 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12704 DECL_TI_TEMPLATE (new_decl
) = r
;
12705 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12709 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12710 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12712 if (PRIMARY_TEMPLATE_P (t
))
12713 DECL_PRIMARY_TEMPLATE (r
) = r
;
12715 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12717 /* Record this non-type partial instantiation. */
12718 register_specialization (r
, t
,
12719 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12725 /* True if FN is the op() for a lambda in an uninstantiated template. */
12728 lambda_fn_in_template_p (tree fn
)
12730 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12732 tree closure
= DECL_CONTEXT (fn
);
12733 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12736 /* We're instantiating a variable from template function TCTX. Return the
12737 corresponding current enclosing scope. This gets complicated because lambda
12738 functions in templates are regenerated rather than instantiated, but generic
12739 lambda functions are subsequently instantiated. */
12742 enclosing_instantiation_of (tree otctx
)
12745 tree fn
= current_function_decl
;
12746 int lambda_count
= 0;
12748 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12749 tctx
= decl_function_context (tctx
))
12751 for (; fn
; fn
= decl_function_context (fn
))
12754 int flambda_count
= 0;
12755 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12756 fn
= decl_function_context (fn
))
12758 if (DECL_TEMPLATE_INFO (fn
)
12759 ? most_general_template (fn
) != most_general_template (tctx
)
12762 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12763 || DECL_CONV_FN_P (ofn
));
12766 gcc_unreachable ();
12769 /* Substitute the ARGS into the T, which is a _DECL. Return the
12770 result of the substitution. Issue error and warning messages under
12771 control of COMPLAIN. */
12774 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12776 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12777 location_t saved_loc
;
12778 tree r
= NULL_TREE
;
12780 hashval_t hash
= 0;
12782 /* Set the filename and linenumber to improve error-reporting. */
12783 saved_loc
= input_location
;
12784 input_location
= DECL_SOURCE_LOCATION (t
);
12786 switch (TREE_CODE (t
))
12788 case TEMPLATE_DECL
:
12789 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12792 case FUNCTION_DECL
:
12793 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12798 tree type
= NULL_TREE
;
12800 tree expanded_types
= NULL_TREE
;
12801 tree prev_r
= NULL_TREE
;
12802 tree first_r
= NULL_TREE
;
12804 if (DECL_PACK_P (t
))
12806 /* If there is a local specialization that isn't a
12807 parameter pack, it means that we're doing a "simple"
12808 substitution from inside tsubst_pack_expansion. Just
12809 return the local specialization (which will be a single
12811 tree spec
= retrieve_local_specialization (t
);
12813 && TREE_CODE (spec
) == PARM_DECL
12814 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12817 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12818 the parameters in this function parameter pack. */
12819 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12820 complain
, in_decl
);
12821 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12823 len
= TREE_VEC_LENGTH (expanded_types
);
12825 /* Zero-length parameter packs are boring. Just substitute
12828 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12833 /* All we did was update the type. Make a note of that. */
12834 type
= expanded_types
;
12835 expanded_types
= NULL_TREE
;
12839 /* Loop through all of the parameters we'll build. When T is
12840 a function parameter pack, LEN is the number of expanded
12841 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12843 for (i
= 0; i
< len
; ++i
)
12847 if (DECL_TEMPLATE_PARM_P (t
))
12848 SET_DECL_TEMPLATE_PARM_P (r
);
12850 if (expanded_types
)
12851 /* We're on the Ith parameter of the function parameter
12854 /* Get the Ith type. */
12855 type
= TREE_VEC_ELT (expanded_types
, i
);
12857 /* Rename the parameter to include the index. */
12859 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12862 /* We're dealing with a normal parameter. */
12863 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12865 type
= type_decays_to (type
);
12866 TREE_TYPE (r
) = type
;
12867 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12869 if (DECL_INITIAL (r
))
12871 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12872 DECL_INITIAL (r
) = TREE_TYPE (r
);
12874 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12875 complain
, in_decl
);
12878 DECL_CONTEXT (r
) = NULL_TREE
;
12880 if (!DECL_TEMPLATE_PARM_P (r
))
12881 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12883 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12884 args
, complain
, in_decl
);
12886 /* Keep track of the first new parameter we
12887 generate. That's what will be returned to the
12892 /* Build a proper chain of parameters when substituting
12893 into a function parameter pack. */
12895 DECL_CHAIN (prev_r
) = r
;
12898 /* If cp_unevaluated_operand is set, we're just looking for a
12899 single dummy parameter, so don't keep going. */
12900 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12901 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12902 complain
, DECL_CHAIN (t
));
12904 /* FIRST_R contains the start of the chain we've built. */
12911 tree type
= NULL_TREE
;
12912 tree vec
= NULL_TREE
;
12913 tree expanded_types
= NULL_TREE
;
12916 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12918 /* This field is a lambda capture pack. Return a TREE_VEC of
12919 the expanded fields to instantiate_class_template_1. */
12920 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12921 complain
, in_decl
);
12922 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12924 len
= TREE_VEC_LENGTH (expanded_types
);
12925 vec
= make_tree_vec (len
);
12929 /* All we did was update the type. Make a note of that. */
12930 type
= expanded_types
;
12931 expanded_types
= NULL_TREE
;
12935 for (int i
= 0; i
< len
; ++i
)
12938 if (expanded_types
)
12940 type
= TREE_VEC_ELT (expanded_types
, i
);
12942 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12945 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12947 if (type
== error_mark_node
)
12948 RETURN (error_mark_node
);
12949 TREE_TYPE (r
) = type
;
12950 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12952 if (DECL_C_BIT_FIELD (r
))
12953 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12955 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12956 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12958 /*integral_constant_expression_p=*/true);
12959 if (DECL_INITIAL (t
))
12961 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12962 NSDMI in perform_member_init. Still set DECL_INITIAL
12963 so that we know there is one. */
12964 DECL_INITIAL (r
) = void_node
;
12965 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12966 retrofit_lang_decl (r
);
12967 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12969 /* We don't have to set DECL_CONTEXT here; it is set by
12970 finish_member_declaration. */
12971 DECL_CHAIN (r
) = NULL_TREE
;
12973 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12974 args
, complain
, in_decl
);
12977 TREE_VEC_ELT (vec
, i
) = r
;
12986 /* We reach here only for member using decls. We also need to check
12987 uses_template_parms because DECL_DEPENDENT_P is not set for a
12988 using-declaration that designates a member of the current
12989 instantiation (c++/53549). */
12990 if (DECL_DEPENDENT_P (t
)
12991 || uses_template_parms (USING_DECL_SCOPE (t
)))
12993 tree scope
= USING_DECL_SCOPE (t
);
12994 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12995 if (PACK_EXPANSION_P (scope
))
12997 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12998 int len
= TREE_VEC_LENGTH (vec
);
12999 r
= make_tree_vec (len
);
13000 for (int i
= 0; i
< len
; ++i
)
13002 tree escope
= TREE_VEC_ELT (vec
, i
);
13003 tree elt
= do_class_using_decl (escope
, name
);
13006 r
= error_mark_node
;
13011 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13012 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13014 TREE_VEC_ELT (r
, i
) = elt
;
13019 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13020 complain
, in_decl
);
13021 r
= do_class_using_decl (inst_scope
, name
);
13023 r
= error_mark_node
;
13026 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13027 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13034 DECL_CHAIN (r
) = NULL_TREE
;
13041 tree argvec
= NULL_TREE
;
13042 tree gen_tmpl
= NULL_TREE
;
13044 tree tmpl
= NULL_TREE
;
13046 tree type
= NULL_TREE
;
13049 if (TREE_TYPE (t
) == error_mark_node
)
13050 RETURN (error_mark_node
);
13052 if (TREE_CODE (t
) == TYPE_DECL
13053 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13055 /* If this is the canonical decl, we don't have to
13056 mess with instantiations, and often we can't (for
13057 typename, template type parms and such). Note that
13058 TYPE_NAME is not correct for the above test if
13059 we've copied the type for a typedef. */
13060 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13061 if (type
== error_mark_node
)
13062 RETURN (error_mark_node
);
13063 r
= TYPE_NAME (type
);
13067 /* Check to see if we already have the specialization we
13070 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13072 /* T is a static data member or namespace-scope entity.
13073 We have to substitute into namespace-scope variables
13074 (not just variable templates) because of cases like:
13076 template <class T> void f() { extern T t; }
13078 where the entity referenced is not known until
13079 instantiation time. */
13081 ctx
= DECL_CONTEXT (t
);
13082 if (DECL_CLASS_SCOPE_P (t
))
13084 ctx
= tsubst_aggr_type (ctx
, args
,
13086 in_decl
, /*entering_scope=*/1);
13087 /* If CTX is unchanged, then T is in fact the
13088 specialization we want. That situation occurs when
13089 referencing a static data member within in its own
13090 class. We can use pointer equality, rather than
13091 same_type_p, because DECL_CONTEXT is always
13093 if (ctx
== DECL_CONTEXT (t
)
13094 /* ... unless T is a member template; in which
13095 case our caller can be willing to create a
13096 specialization of that template represented
13098 && !(DECL_TI_TEMPLATE (t
)
13099 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13105 tmpl
= DECL_TI_TEMPLATE (t
);
13106 gen_tmpl
= most_general_template (tmpl
);
13107 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13108 if (argvec
!= error_mark_node
)
13109 argvec
= (coerce_innermost_template_parms
13110 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13111 argvec
, t
, complain
,
13112 /*all*/true, /*defarg*/true));
13113 if (argvec
== error_mark_node
)
13114 RETURN (error_mark_node
);
13115 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13116 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13121 /* A local variable. */
13123 /* Subsequent calls to pushdecl will fill this in. */
13125 /* Unless this is a reference to a static variable from an
13126 enclosing function, in which case we need to fill it in now. */
13127 if (TREE_STATIC (t
))
13129 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13130 if (fn
!= current_function_decl
)
13133 spec
= retrieve_local_specialization (t
);
13135 /* If we already have the specialization we need, there is
13136 nothing more to do. */
13143 /* Create a new node for the specialization we need. */
13145 if (type
== NULL_TREE
)
13147 if (is_typedef_decl (t
))
13148 type
= DECL_ORIGINAL_TYPE (t
);
13150 type
= TREE_TYPE (t
);
13152 && VAR_HAD_UNKNOWN_BOUND (t
)
13153 && type
!= error_mark_node
)
13154 type
= strip_array_domain (type
);
13155 tree sub_args
= args
;
13156 if (tree auto_node
= type_uses_auto (type
))
13158 /* Mask off any template args past the variable's context so we
13159 don't replace the auto with an unrelated argument. */
13160 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13161 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13163 /* This should never happen with the new lambda instantiation
13164 model, but keep the handling just in case. */
13165 gcc_assert (!CHECKING_P
),
13166 sub_args
= strip_innermost_template_args (args
, extra
);
13168 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13172 /* Even if the original location is out of scope, the
13173 newly substituted one is not. */
13174 DECL_DEAD_FOR_LOCAL (r
) = 0;
13175 DECL_INITIALIZED_P (r
) = 0;
13176 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13177 if (type
== error_mark_node
)
13178 RETURN (error_mark_node
);
13179 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13181 /* It may seem that this case cannot occur, since:
13186 declares a function, not a variable. However:
13189 template <typename T> void g() { T t; }
13190 template void g<f>();
13192 is an attempt to declare a variable with function
13194 error ("variable %qD has function type",
13195 /* R is not yet sufficiently initialized, so we
13196 just use its name. */
13198 RETURN (error_mark_node
);
13200 type
= complete_type (type
);
13201 /* Wait until cp_finish_decl to set this again, to handle
13202 circular dependency (template/instantiate6.C). */
13203 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13204 type
= check_var_type (DECL_NAME (r
), type
);
13206 if (DECL_HAS_VALUE_EXPR_P (t
))
13208 tree ve
= DECL_VALUE_EXPR (t
);
13209 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13210 /*constant_expression_p=*/false);
13211 if (REFERENCE_REF_P (ve
))
13213 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13214 ve
= TREE_OPERAND (ve
, 0);
13216 SET_DECL_VALUE_EXPR (r
, ve
);
13218 if (CP_DECL_THREAD_LOCAL_P (r
)
13219 && !processing_template_decl
)
13220 set_decl_tls_model (r
, decl_default_tls_model (r
));
13222 else if (DECL_SELF_REFERENCE_P (t
))
13223 SET_DECL_SELF_REFERENCE_P (r
);
13224 TREE_TYPE (r
) = type
;
13225 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13226 DECL_CONTEXT (r
) = ctx
;
13227 /* Clear out the mangled name and RTL for the instantiation. */
13228 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13229 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13230 SET_DECL_RTL (r
, NULL
);
13231 /* The initializer must not be expanded until it is required;
13232 see [temp.inst]. */
13233 DECL_INITIAL (r
) = NULL_TREE
;
13234 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13237 if (DECL_LANG_SPECIFIC (r
))
13238 SET_DECL_DEPENDENT_INIT_P (r
, false);
13240 SET_DECL_MODE (r
, VOIDmode
);
13242 /* Possibly limit visibility based on template args. */
13243 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13244 if (DECL_VISIBILITY_SPECIFIED (t
))
13246 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13247 DECL_ATTRIBUTES (r
)
13248 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13250 determine_visibility (r
);
13255 /* A static data member declaration is always marked
13256 external when it is declared in-class, even if an
13257 initializer is present. We mimic the non-template
13258 processing here. */
13259 DECL_EXTERNAL (r
) = 1;
13260 if (DECL_NAMESPACE_SCOPE_P (t
))
13261 DECL_NOT_REALLY_EXTERN (r
) = 1;
13263 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13264 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13265 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13269 if (DECL_LANG_SPECIFIC (r
))
13270 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13271 if (!cp_unevaluated_operand
)
13272 register_local_specialization (r
, t
);
13275 DECL_CHAIN (r
) = NULL_TREE
;
13277 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13279 args
, complain
, in_decl
);
13281 /* Preserve a typedef that names a type. */
13282 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13284 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13285 set_underlying_type (r
);
13286 if (TYPE_DECL_ALIAS_P (r
))
13287 /* An alias template specialization can be dependent
13288 even if its underlying type is not. */
13289 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13292 layout_decl (r
, 0);
13297 gcc_unreachable ();
13302 /* Restore the file and line information. */
13303 input_location
= saved_loc
;
13308 /* Substitute into the ARG_TYPES of a function type.
13309 If END is a TREE_CHAIN, leave it and any following types
13313 tsubst_arg_types (tree arg_types
,
13316 tsubst_flags_t complain
,
13319 tree remaining_arg_types
;
13320 tree type
= NULL_TREE
;
13322 tree expanded_args
= NULL_TREE
;
13325 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13328 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13329 args
, end
, complain
, in_decl
);
13330 if (remaining_arg_types
== error_mark_node
)
13331 return error_mark_node
;
13333 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13335 /* For a pack expansion, perform substitution on the
13336 entire expression. Later on, we'll handle the arguments
13338 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13339 args
, complain
, in_decl
);
13341 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13342 /* So that we'll spin through the parameters, one by one. */
13343 i
= TREE_VEC_LENGTH (expanded_args
);
13346 /* We only partially substituted into the parameter
13347 pack. Our type is TYPE_PACK_EXPANSION. */
13348 type
= expanded_args
;
13349 expanded_args
= NULL_TREE
;
13357 type
= TREE_VEC_ELT (expanded_args
, i
);
13359 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13361 if (type
== error_mark_node
)
13362 return error_mark_node
;
13363 if (VOID_TYPE_P (type
))
13365 if (complain
& tf_error
)
13367 error ("invalid parameter type %qT", type
);
13369 error ("in declaration %q+D", in_decl
);
13371 return error_mark_node
;
13374 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13375 return error_mark_node
;
13377 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13378 top-level qualifiers as required. */
13379 type
= cv_unqualified (type_decays_to (type
));
13381 /* We do not substitute into default arguments here. The standard
13382 mandates that they be instantiated only when needed, which is
13383 done in build_over_call. */
13384 default_arg
= TREE_PURPOSE (arg_types
);
13386 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13387 since the new op() won't have any associated template arguments for us
13388 to refer to later. */
13389 if (lambda_fn_in_template_p (in_decl
))
13390 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13391 false/*fn*/, false/*constexpr*/);
13393 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13395 /* We've instantiated a template before its default arguments
13396 have been parsed. This can happen for a nested template
13397 class, and is not an error unless we require the default
13398 argument in a call of this function. */
13399 remaining_arg_types
=
13400 tree_cons (default_arg
, type
, remaining_arg_types
);
13401 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13404 remaining_arg_types
=
13405 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13408 return remaining_arg_types
;
13411 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13412 *not* handle the exception-specification for FNTYPE, because the
13413 initial substitution of explicitly provided template parameters
13414 during argument deduction forbids substitution into the
13415 exception-specification:
13419 All references in the function type of the function template to the
13420 corresponding template parameters are replaced by the specified tem-
13421 plate argument values. If a substitution in a template parameter or
13422 in the function type of the function template results in an invalid
13423 type, type deduction fails. [Note: The equivalent substitution in
13424 exception specifications is done only when the function is instanti-
13425 ated, at which point a program is ill-formed if the substitution
13426 results in an invalid type.] */
13429 tsubst_function_type (tree t
,
13431 tsubst_flags_t complain
,
13435 tree arg_types
= NULL_TREE
;
13438 /* The TYPE_CONTEXT is not used for function/method types. */
13439 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13441 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13443 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13445 if (late_return_type_p
)
13447 /* Substitute the argument types. */
13448 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13449 complain
, in_decl
);
13450 if (arg_types
== error_mark_node
)
13451 return error_mark_node
;
13453 tree save_ccp
= current_class_ptr
;
13454 tree save_ccr
= current_class_ref
;
13455 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13456 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13457 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13460 /* DR 1207: 'this' is in scope in the trailing return type. */
13461 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13464 /* Substitute the return type. */
13465 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13469 current_class_ptr
= save_ccp
;
13470 current_class_ref
= save_ccr
;
13474 /* Substitute the return type. */
13475 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13477 if (return_type
== error_mark_node
)
13478 return error_mark_node
;
13479 /* DR 486 clarifies that creation of a function type with an
13480 invalid return type is a deduction failure. */
13481 if (TREE_CODE (return_type
) == ARRAY_TYPE
13482 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13484 if (complain
& tf_error
)
13486 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13487 error ("function returning an array");
13489 error ("function returning a function");
13491 return error_mark_node
;
13494 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13495 return error_mark_node
;
13497 if (!late_return_type_p
)
13499 /* Substitute the argument types. */
13500 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13501 complain
, in_decl
);
13502 if (arg_types
== error_mark_node
)
13503 return error_mark_node
;
13506 /* Construct a new type node and return it. */
13507 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13509 fntype
= build_function_type (return_type
, arg_types
);
13510 fntype
= apply_memfn_quals (fntype
,
13511 type_memfn_quals (t
),
13512 type_memfn_rqual (t
));
13516 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13517 /* Don't pick up extra function qualifiers from the basetype. */
13518 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13519 if (! MAYBE_CLASS_TYPE_P (r
))
13523 Type deduction may fail for any of the following
13526 -- Attempting to create "pointer to member of T" when T
13527 is not a class type. */
13528 if (complain
& tf_error
)
13529 error ("creating pointer to member function of non-class type %qT",
13531 return error_mark_node
;
13534 fntype
= build_method_type_directly (r
, return_type
,
13535 TREE_CHAIN (arg_types
));
13536 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13538 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13540 if (late_return_type_p
)
13541 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13546 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13547 ARGS into that specification, and return the substituted
13548 specification. If there is no specification, return NULL_TREE. */
13551 tsubst_exception_specification (tree fntype
,
13553 tsubst_flags_t complain
,
13560 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13561 new_specs
= NULL_TREE
;
13562 if (specs
&& TREE_PURPOSE (specs
))
13564 /* A noexcept-specifier. */
13565 tree expr
= TREE_PURPOSE (specs
);
13566 if (TREE_CODE (expr
) == INTEGER_CST
)
13570 /* Defer instantiation of noexcept-specifiers to avoid
13571 excessive instantiations (c++/49107). */
13572 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13573 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13575 /* We already partially instantiated this member template,
13576 so combine the new args with the old. */
13577 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13578 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13579 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13580 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13584 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13585 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13589 new_specs
= tsubst_copy_and_build
13590 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13591 /*integral_constant_expression_p=*/true);
13592 new_specs
= build_noexcept_spec (new_specs
, complain
);
13596 if (! TREE_VALUE (specs
))
13603 tree expanded_specs
= NULL_TREE
;
13605 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13607 /* Expand the pack expansion type. */
13608 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13612 if (expanded_specs
== error_mark_node
)
13613 return error_mark_node
;
13614 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13615 len
= TREE_VEC_LENGTH (expanded_specs
);
13618 /* We're substituting into a member template, so
13619 we got a TYPE_PACK_EXPANSION back. Add that
13620 expansion and move on. */
13621 gcc_assert (TREE_CODE (expanded_specs
)
13622 == TYPE_PACK_EXPANSION
);
13623 new_specs
= add_exception_specifier (new_specs
,
13626 specs
= TREE_CHAIN (specs
);
13631 for (i
= 0; i
< len
; ++i
)
13633 if (expanded_specs
)
13634 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13636 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13637 if (spec
== error_mark_node
)
13639 new_specs
= add_exception_specifier (new_specs
, spec
,
13643 specs
= TREE_CHAIN (specs
);
13649 /* Take the tree structure T and replace template parameters used
13650 therein with the argument vector ARGS. IN_DECL is an associated
13651 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13652 Issue error and warning messages under control of COMPLAIN. Note
13653 that we must be relatively non-tolerant of extensions here, in
13654 order to preserve conformance; if we allow substitutions that
13655 should not be allowed, we may allow argument deductions that should
13656 not succeed, and therefore report ambiguous overload situations
13657 where there are none. In theory, we could allow the substitution,
13658 but indicate that it should have failed, and allow our caller to
13659 make sure that the right thing happens, but we don't try to do this
13662 This function is used for dealing with types, decls and the like;
13663 for expressions, use tsubst_expr or tsubst_copy. */
13666 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13668 enum tree_code code
;
13669 tree type
, r
= NULL_TREE
;
13671 if (t
== NULL_TREE
|| t
== error_mark_node
13672 || t
== integer_type_node
13673 || t
== void_type_node
13674 || t
== char_type_node
13675 || t
== unknown_type_node
13676 || TREE_CODE (t
) == NAMESPACE_DECL
13677 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13681 return tsubst_decl (t
, args
, complain
);
13683 if (args
== NULL_TREE
)
13686 code
= TREE_CODE (t
);
13688 if (code
== IDENTIFIER_NODE
)
13689 type
= IDENTIFIER_TYPE_VALUE (t
);
13691 type
= TREE_TYPE (t
);
13693 gcc_assert (type
!= unknown_type_node
);
13695 /* Reuse typedefs. We need to do this to handle dependent attributes,
13696 such as attribute aligned. */
13698 && typedef_variant_p (t
))
13700 tree decl
= TYPE_NAME (t
);
13702 if (alias_template_specialization_p (t
))
13704 /* DECL represents an alias template and we want to
13706 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13707 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13708 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13710 else if (DECL_CLASS_SCOPE_P (decl
)
13711 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13712 && uses_template_parms (DECL_CONTEXT (decl
)))
13714 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13715 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13716 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13718 else if (DECL_FUNCTION_SCOPE_P (decl
)
13719 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13720 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13721 r
= retrieve_local_specialization (decl
);
13723 /* The typedef is from a non-template context. */
13729 r
= cp_build_qualified_type_real
13730 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13731 complain
| tf_ignore_bad_quals
);
13736 /* We don't have an instantiation yet, so drop the typedef. */
13737 int quals
= cp_type_quals (t
);
13738 t
= DECL_ORIGINAL_TYPE (decl
);
13739 t
= cp_build_qualified_type_real (t
, quals
,
13740 complain
| tf_ignore_bad_quals
);
13744 bool fndecl_type
= (complain
& tf_fndecl_type
);
13745 complain
&= ~tf_fndecl_type
;
13748 && code
!= TYPENAME_TYPE
13749 && code
!= TEMPLATE_TYPE_PARM
13750 && code
!= TEMPLATE_PARM_INDEX
13751 && code
!= IDENTIFIER_NODE
13752 && code
!= FUNCTION_TYPE
13753 && code
!= METHOD_TYPE
)
13754 type
= tsubst (type
, args
, complain
, in_decl
);
13755 if (type
== error_mark_node
)
13756 return error_mark_node
;
13762 case ENUMERAL_TYPE
:
13763 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13764 /*entering_scope=*/0);
13767 case IDENTIFIER_NODE
:
13778 if (t
== integer_type_node
)
13781 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13782 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13786 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13788 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13789 /*integral_constant_expression_p=*/false);
13791 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13793 if (TREE_CODE (max
) == NOP_EXPR
13794 && TREE_SIDE_EFFECTS (omax
)
13795 && !TREE_TYPE (max
))
13796 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13798 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13799 with TREE_SIDE_EFFECTS that indicates this is not an integral
13800 constant expression. */
13801 if (processing_template_decl
13802 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13804 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13805 TREE_SIDE_EFFECTS (max
) = 1;
13808 return compute_array_index_type (NULL_TREE
, max
, complain
);
13811 case TEMPLATE_TYPE_PARM
:
13812 case TEMPLATE_TEMPLATE_PARM
:
13813 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13814 case TEMPLATE_PARM_INDEX
:
13819 tree arg
= NULL_TREE
;
13821 /* Early in template argument deduction substitution, we don't
13822 want to reduce the level of 'auto', or it will be confused
13823 with a normal template parm in subsequent deduction. */
13824 if (is_auto (t
) && (complain
& tf_partial
))
13829 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13830 template_parm_level_and_index (t
, &level
, &idx
);
13832 levels
= TMPL_ARGS_DEPTH (args
);
13833 if (level
<= levels
13834 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13836 arg
= TMPL_ARG (args
, level
, idx
);
13838 /* See through ARGUMENT_PACK_SELECT arguments. */
13839 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13840 arg
= argument_pack_select_arg (arg
);
13843 if (arg
== error_mark_node
)
13844 return error_mark_node
;
13845 else if (arg
!= NULL_TREE
)
13847 if (ARGUMENT_PACK_P (arg
))
13848 /* If ARG is an argument pack, we don't actually want to
13849 perform a substitution here, because substitutions
13850 for argument packs are only done
13851 element-by-element. We can get to this point when
13852 substituting the type of a non-type template
13853 parameter pack, when that type actually contains
13854 template parameter packs from an outer template, e.g.,
13856 template<typename... Types> struct A {
13857 template<Types... Values> struct B { };
13861 if (code
== TEMPLATE_TYPE_PARM
)
13864 gcc_assert (TYPE_P (arg
));
13866 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13868 return cp_build_qualified_type_real
13869 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13871 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13873 /* We are processing a type constructed from a
13874 template template parameter. */
13875 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13876 args
, complain
, in_decl
);
13877 if (argvec
== error_mark_node
)
13878 return error_mark_node
;
13880 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13881 || TREE_CODE (arg
) == TEMPLATE_DECL
13882 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13884 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13885 /* Consider this code:
13887 template <template <class> class Template>
13889 template <class Arg> using Bind = Template<Arg>;
13892 template <template <class> class Template, class Arg>
13893 using Instantiate = Template<Arg>; //#0
13895 template <template <class> class Template,
13898 Instantiate<Internal<Template>::template Bind,
13901 When #1 is parsed, the
13902 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13903 parameter `Template' in #0 matches the
13904 UNBOUND_CLASS_TEMPLATE representing the argument
13905 `Internal<Template>::template Bind'; We then want
13906 to assemble the type `Bind<Argument>' that can't
13907 be fully created right now, because
13908 `Internal<Template>' not being complete, the Bind
13909 template cannot be looked up in that context. So
13910 we need to "store" `Bind<Argument>' for later
13911 when the context of Bind becomes complete. Let's
13912 store that in a TYPENAME_TYPE. */
13913 return make_typename_type (TYPE_CONTEXT (arg
),
13914 build_nt (TEMPLATE_ID_EXPR
,
13915 TYPE_IDENTIFIER (arg
),
13920 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13921 are resolving nested-types in the signature of a
13922 member function templates. Otherwise ARG is a
13923 TEMPLATE_DECL and is the real template to be
13925 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13926 arg
= TYPE_NAME (arg
);
13928 r
= lookup_template_class (arg
,
13930 DECL_CONTEXT (arg
),
13931 /*entering_scope=*/0,
13933 return cp_build_qualified_type_real
13934 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13936 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13939 /* TEMPLATE_PARM_INDEX. */
13940 return convert_from_reference (unshare_expr (arg
));
13944 /* This can happen during the attempted tsubst'ing in
13945 unify. This means that we don't yet have any information
13946 about the template parameter in question. */
13949 /* If we get here, we must have been looking at a parm for a
13950 more deeply nested template. Make a new version of this
13951 template parameter, but with a lower level. */
13954 case TEMPLATE_TYPE_PARM
:
13955 case TEMPLATE_TEMPLATE_PARM
:
13956 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13957 if (cp_type_quals (t
))
13959 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13960 r
= cp_build_qualified_type_real
13961 (r
, cp_type_quals (t
),
13962 complain
| (code
== TEMPLATE_TYPE_PARM
13963 ? tf_ignore_bad_quals
: 0));
13965 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13966 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13967 && (r
= (TEMPLATE_PARM_DESCENDANTS
13968 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13969 && (r
= TREE_TYPE (r
))
13970 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13971 /* Break infinite recursion when substituting the constraints
13972 of a constrained placeholder. */;
13976 TEMPLATE_TYPE_PARM_INDEX (r
)
13977 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13978 r
, levels
, args
, complain
);
13979 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13980 TYPE_MAIN_VARIANT (r
) = r
;
13981 TYPE_POINTER_TO (r
) = NULL_TREE
;
13982 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13984 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13986 /* Propagate constraints on placeholders. */
13987 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13988 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13989 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13990 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13992 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13993 pl
= tsubst (pl
, args
, complain
, in_decl
);
13994 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13998 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13999 /* We have reduced the level of the template
14000 template parameter, but not the levels of its
14001 template parameters, so canonical_type_parameter
14002 will not be able to find the canonical template
14003 template parameter for this level. Thus, we
14004 require structural equality checking to compare
14005 TEMPLATE_TEMPLATE_PARMs. */
14006 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14007 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14008 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14010 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14012 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14014 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14015 /* We might need to substitute into the types of non-type
14016 template parameters. */
14017 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14018 complain
, in_decl
);
14019 if (tmpl
== error_mark_node
)
14020 return error_mark_node
;
14021 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14022 complain
, in_decl
);
14023 if (argvec
== error_mark_node
)
14024 return error_mark_node
;
14026 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14027 = build_template_info (tmpl
, argvec
);
14032 case TEMPLATE_PARM_INDEX
:
14033 /* OK, now substitute the type of the non-type parameter. We
14034 couldn't do it earlier because it might be an auto parameter,
14035 and we wouldn't need to if we had an argument. */
14036 type
= tsubst (type
, args
, complain
, in_decl
);
14037 if (type
== error_mark_node
)
14038 return error_mark_node
;
14039 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14043 gcc_unreachable ();
14051 tree purpose
, value
, chain
;
14053 if (t
== void_list_node
)
14056 purpose
= TREE_PURPOSE (t
);
14059 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14060 if (purpose
== error_mark_node
)
14061 return error_mark_node
;
14063 value
= TREE_VALUE (t
);
14066 value
= tsubst (value
, args
, complain
, in_decl
);
14067 if (value
== error_mark_node
)
14068 return error_mark_node
;
14070 chain
= TREE_CHAIN (t
);
14071 if (chain
&& chain
!= void_type_node
)
14073 chain
= tsubst (chain
, args
, complain
, in_decl
);
14074 if (chain
== error_mark_node
)
14075 return error_mark_node
;
14077 if (purpose
== TREE_PURPOSE (t
)
14078 && value
== TREE_VALUE (t
)
14079 && chain
== TREE_CHAIN (t
))
14081 return hash_tree_cons (purpose
, value
, chain
);
14085 /* We should never be tsubsting a binfo. */
14086 gcc_unreachable ();
14089 /* A vector of template arguments. */
14090 gcc_assert (!type
);
14091 return tsubst_template_args (t
, args
, complain
, in_decl
);
14094 case REFERENCE_TYPE
:
14096 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14101 Type deduction may fail for any of the following
14104 -- Attempting to create a pointer to reference type.
14105 -- Attempting to create a reference to a reference type or
14106 a reference to void.
14108 Core issue 106 says that creating a reference to a reference
14109 during instantiation is no longer a cause for failure. We
14110 only enforce this check in strict C++98 mode. */
14111 if ((TREE_CODE (type
) == REFERENCE_TYPE
14112 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14113 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14115 static location_t last_loc
;
14117 /* We keep track of the last time we issued this error
14118 message to avoid spewing a ton of messages during a
14119 single bad template instantiation. */
14120 if (complain
& tf_error
14121 && last_loc
!= input_location
)
14123 if (VOID_TYPE_P (type
))
14124 error ("forming reference to void");
14125 else if (code
== POINTER_TYPE
)
14126 error ("forming pointer to reference type %qT", type
);
14128 error ("forming reference to reference type %qT", type
);
14129 last_loc
= input_location
;
14132 return error_mark_node
;
14134 else if (TREE_CODE (type
) == FUNCTION_TYPE
14135 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14136 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14138 if (complain
& tf_error
)
14140 if (code
== POINTER_TYPE
)
14141 error ("forming pointer to qualified function type %qT",
14144 error ("forming reference to qualified function type %qT",
14147 return error_mark_node
;
14149 else if (code
== POINTER_TYPE
)
14151 r
= build_pointer_type (type
);
14152 if (TREE_CODE (type
) == METHOD_TYPE
)
14153 r
= build_ptrmemfunc_type (r
);
14155 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14156 /* In C++0x, during template argument substitution, when there is an
14157 attempt to create a reference to a reference type, reference
14158 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14160 "If a template-argument for a template-parameter T names a type
14161 that is a reference to a type A, an attempt to create the type
14162 'lvalue reference to cv T' creates the type 'lvalue reference to
14163 A,' while an attempt to create the type type rvalue reference to
14164 cv T' creates the type T"
14166 r
= cp_build_reference_type
14168 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14170 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14171 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14173 if (r
!= error_mark_node
)
14174 /* Will this ever be needed for TYPE_..._TO values? */
14181 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14182 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14186 Type deduction may fail for any of the following
14189 -- Attempting to create "pointer to member of T" when T
14190 is not a class type. */
14191 if (complain
& tf_error
)
14192 error ("creating pointer to member of non-class type %qT", r
);
14193 return error_mark_node
;
14195 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14197 if (complain
& tf_error
)
14198 error ("creating pointer to member reference type %qT", type
);
14199 return error_mark_node
;
14201 if (VOID_TYPE_P (type
))
14203 if (complain
& tf_error
)
14204 error ("creating pointer to member of type void");
14205 return error_mark_node
;
14207 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14208 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14210 /* The type of the implicit object parameter gets its
14211 cv-qualifiers from the FUNCTION_TYPE. */
14214 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14215 type_memfn_rqual (type
));
14216 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14217 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14221 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14225 case FUNCTION_TYPE
:
14230 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14231 if (fntype
== error_mark_node
)
14232 return error_mark_node
;
14234 /* Substitute the exception specification. */
14235 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14236 /*defer_ok*/fndecl_type
);
14237 if (specs
== error_mark_node
)
14238 return error_mark_node
;
14240 fntype
= build_exception_variant (fntype
, specs
);
14245 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14246 if (domain
== error_mark_node
)
14247 return error_mark_node
;
14249 /* As an optimization, we avoid regenerating the array type if
14250 it will obviously be the same as T. */
14251 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14254 /* These checks should match the ones in create_array_type_for_decl.
14258 The deduction may fail for any of the following reasons:
14260 -- Attempting to create an array with an element type that
14261 is void, a function type, or a reference type, or [DR337]
14262 an abstract class type. */
14263 if (VOID_TYPE_P (type
)
14264 || TREE_CODE (type
) == FUNCTION_TYPE
14265 || (TREE_CODE (type
) == ARRAY_TYPE
14266 && TYPE_DOMAIN (type
) == NULL_TREE
)
14267 || TREE_CODE (type
) == REFERENCE_TYPE
)
14269 if (complain
& tf_error
)
14270 error ("creating array of %qT", type
);
14271 return error_mark_node
;
14274 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14275 return error_mark_node
;
14277 r
= build_cplus_array_type (type
, domain
);
14279 if (TYPE_USER_ALIGN (t
))
14281 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14282 TYPE_USER_ALIGN (r
) = 1;
14288 case TYPENAME_TYPE
:
14290 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14291 in_decl
, /*entering_scope=*/1);
14292 if (ctx
== error_mark_node
)
14293 return error_mark_node
;
14295 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14296 complain
, in_decl
);
14297 if (f
== error_mark_node
)
14298 return error_mark_node
;
14300 if (!MAYBE_CLASS_TYPE_P (ctx
))
14302 if (complain
& tf_error
)
14303 error ("%qT is not a class, struct, or union type", ctx
);
14304 return error_mark_node
;
14306 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14308 /* Normally, make_typename_type does not require that the CTX
14309 have complete type in order to allow things like:
14311 template <class T> struct S { typename S<T>::X Y; };
14313 But, such constructs have already been resolved by this
14314 point, so here CTX really should have complete type, unless
14315 it's a partial instantiation. */
14316 ctx
= complete_type (ctx
);
14317 if (!COMPLETE_TYPE_P (ctx
))
14319 if (complain
& tf_error
)
14320 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14321 return error_mark_node
;
14325 f
= make_typename_type (ctx
, f
, typename_type
,
14326 complain
| tf_keep_type_decl
);
14327 if (f
== error_mark_node
)
14329 if (TREE_CODE (f
) == TYPE_DECL
)
14331 complain
|= tf_ignore_bad_quals
;
14335 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14337 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14339 if (complain
& tf_error
)
14340 error ("%qT resolves to %qT, which is not an enumeration type",
14343 return error_mark_node
;
14345 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14347 if (complain
& tf_error
)
14348 error ("%qT resolves to %qT, which is is not a class type",
14351 return error_mark_node
;
14355 return cp_build_qualified_type_real
14356 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14359 case UNBOUND_CLASS_TEMPLATE
:
14361 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14362 in_decl
, /*entering_scope=*/1);
14363 tree name
= TYPE_IDENTIFIER (t
);
14364 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14366 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14367 return error_mark_node
;
14370 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14371 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14378 ++cp_unevaluated_operand
;
14379 ++c_inhibit_evaluation_warnings
;
14381 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14383 /*integral_constant_expression_p=*/false);
14385 --cp_unevaluated_operand
;
14386 --c_inhibit_evaluation_warnings
;
14388 type
= finish_typeof (type
);
14389 return cp_build_qualified_type_real (type
,
14391 | cp_type_quals (type
),
14395 case DECLTYPE_TYPE
:
14399 ++cp_unevaluated_operand
;
14400 ++c_inhibit_evaluation_warnings
;
14402 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14403 complain
|tf_decltype
, in_decl
,
14404 /*function_p*/false,
14405 /*integral_constant_expression*/false);
14407 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14409 if (type
== NULL_TREE
)
14411 if (complain
& tf_error
)
14412 error ("empty initializer in lambda init-capture");
14413 type
= error_mark_node
;
14415 else if (TREE_CODE (type
) == TREE_LIST
)
14416 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14419 --cp_unevaluated_operand
;
14420 --c_inhibit_evaluation_warnings
;
14422 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14423 type
= lambda_capture_field_type (type
,
14424 DECLTYPE_FOR_INIT_CAPTURE (t
),
14425 DECLTYPE_FOR_REF_CAPTURE (t
));
14426 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14427 type
= lambda_proxy_type (type
);
14430 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14431 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14433 /* In a template ~id could be either a complement expression
14434 or an unqualified-id naming a destructor; if instantiating
14435 it produces an expression, it's not an id-expression or
14438 type
= finish_decltype_type (type
, id
, complain
);
14440 return cp_build_qualified_type_real (type
,
14442 | cp_type_quals (type
),
14443 complain
| tf_ignore_bad_quals
);
14446 case UNDERLYING_TYPE
:
14448 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14449 complain
, in_decl
);
14450 return finish_underlying_type (type
);
14453 case TYPE_ARGUMENT_PACK
:
14454 case NONTYPE_ARGUMENT_PACK
:
14458 if (code
== NONTYPE_ARGUMENT_PACK
)
14459 r
= make_node (code
);
14461 r
= cxx_make_type (code
);
14463 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14464 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14465 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14483 /* We should use one of the expression tsubsts for these codes. */
14484 gcc_unreachable ();
14487 sorry ("use of %qs in template", get_tree_code_name (code
));
14488 return error_mark_node
;
14492 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14493 expression on the left-hand side of the "." or "->" operator. We
14494 only do the lookup if we had a dependent BASELINK. Otherwise we
14495 adjust it onto the instantiated heirarchy. */
14498 tsubst_baselink (tree baselink
, tree object_type
,
14499 tree args
, tsubst_flags_t complain
, tree in_decl
)
14501 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14502 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14503 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14505 tree optype
= BASELINK_OPTYPE (baselink
);
14506 optype
= tsubst (optype
, args
, complain
, in_decl
);
14508 tree template_args
= NULL_TREE
;
14509 bool template_id_p
= false;
14510 tree fns
= BASELINK_FUNCTIONS (baselink
);
14511 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14513 template_id_p
= true;
14514 template_args
= TREE_OPERAND (fns
, 1);
14515 fns
= TREE_OPERAND (fns
, 0);
14517 template_args
= tsubst_template_args (template_args
, args
,
14518 complain
, in_decl
);
14521 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14522 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14523 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14527 tree name
= OVL_NAME (fns
);
14528 if (IDENTIFIER_CONV_OP_P (name
))
14529 name
= make_conv_op_name (optype
);
14531 if (name
== complete_dtor_identifier
)
14532 /* Treat as-if non-dependent below. */
14533 dependent_p
= false;
14535 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14538 if ((complain
& tf_error
)
14539 && constructor_name_p (name
, qualifying_scope
))
14540 error ("cannot call constructor %<%T::%D%> directly",
14541 qualifying_scope
, name
);
14542 return error_mark_node
;
14545 if (BASELINK_P (baselink
))
14546 fns
= BASELINK_FUNCTIONS (baselink
);
14549 /* We're going to overwrite pieces below, make a duplicate. */
14550 baselink
= copy_node (baselink
);
14552 /* If lookup found a single function, mark it as used at this point.
14553 (If lookup found multiple functions the one selected later by
14554 overload resolution will be marked as used at that point.) */
14555 if (!template_id_p
&& !really_overloaded_fn (fns
)
14556 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14557 return error_mark_node
;
14559 if (BASELINK_P (baselink
))
14561 /* Add back the template arguments, if present. */
14563 BASELINK_FUNCTIONS (baselink
)
14564 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14566 /* Update the conversion operator type. */
14567 BASELINK_OPTYPE (baselink
) = optype
;
14571 object_type
= current_class_type
;
14573 if (qualified_p
|| !dependent_p
)
14575 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14579 /* We need to call adjust_result_of_qualified_name_lookup in case the
14580 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14581 so that we still get virtual function binding. */
14582 BASELINK_QUALIFIED_P (baselink
) = false;
14588 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14589 true if the qualified-id will be a postfix-expression in-and-of
14590 itself; false if more of the postfix-expression follows the
14591 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14595 tsubst_qualified_id (tree qualified_id
, tree args
,
14596 tsubst_flags_t complain
, tree in_decl
,
14597 bool done
, bool address_p
)
14603 tree template_args
;
14604 location_t loc
= UNKNOWN_LOCATION
;
14606 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14608 /* Figure out what name to look up. */
14609 name
= TREE_OPERAND (qualified_id
, 1);
14610 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14612 is_template
= true;
14613 loc
= EXPR_LOCATION (name
);
14614 template_args
= TREE_OPERAND (name
, 1);
14616 template_args
= tsubst_template_args (template_args
, args
,
14617 complain
, in_decl
);
14618 if (template_args
== error_mark_node
)
14619 return error_mark_node
;
14620 name
= TREE_OPERAND (name
, 0);
14624 is_template
= false;
14625 template_args
= NULL_TREE
;
14628 /* Substitute into the qualifying scope. When there are no ARGS, we
14629 are just trying to simplify a non-dependent expression. In that
14630 case the qualifying scope may be dependent, and, in any case,
14631 substituting will not help. */
14632 scope
= TREE_OPERAND (qualified_id
, 0);
14635 scope
= tsubst (scope
, args
, complain
, in_decl
);
14636 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14641 if (dependent_scope_p (scope
))
14644 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14645 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14646 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14647 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14651 if (!BASELINK_P (name
) && !DECL_P (expr
))
14653 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14655 /* A BIT_NOT_EXPR is used to represent a destructor. */
14656 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14658 error ("qualifying type %qT does not match destructor name ~%qT",
14659 scope
, TREE_OPERAND (expr
, 0));
14660 expr
= error_mark_node
;
14663 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14664 /*is_type_p=*/0, false);
14667 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14668 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14669 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14671 if (complain
& tf_error
)
14673 error ("dependent-name %qE is parsed as a non-type, but "
14674 "instantiation yields a type", qualified_id
);
14675 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14677 return error_mark_node
;
14683 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14685 /* Remember that there was a reference to this entity. */
14686 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14687 return error_mark_node
;
14690 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14692 if (complain
& tf_error
)
14693 qualified_name_lookup_error (scope
,
14694 TREE_OPERAND (qualified_id
, 1),
14695 expr
, input_location
);
14696 return error_mark_node
;
14701 if (variable_template_p (expr
))
14702 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14705 expr
= lookup_template_function (expr
, template_args
);
14708 if (expr
== error_mark_node
&& complain
& tf_error
)
14709 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14710 expr
, input_location
);
14711 else if (TYPE_P (scope
))
14713 expr
= (adjust_result_of_qualified_name_lookup
14714 (expr
, scope
, current_nonlambda_class_type ()));
14715 expr
= (finish_qualified_id_expr
14716 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14717 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14718 /*template_arg_p=*/false, complain
));
14721 /* Expressions do not generally have reference type. */
14722 if (TREE_CODE (expr
) != SCOPE_REF
14723 /* However, if we're about to form a pointer-to-member, we just
14724 want the referenced member referenced. */
14725 && TREE_CODE (expr
) != OFFSET_REF
)
14726 expr
= convert_from_reference (expr
);
14728 if (REF_PARENTHESIZED_P (qualified_id
))
14729 expr
= force_paren_expr (expr
);
14734 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14735 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14739 tsubst_init (tree init
, tree decl
, tree args
,
14740 tsubst_flags_t complain
, tree in_decl
)
14745 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14747 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14749 /* If we had an initializer but it
14750 instantiated to nothing,
14751 value-initialize the object. This will
14752 only occur when the initializer was a
14753 pack expansion where the parameter packs
14754 used in that expansion were of length
14756 init
= build_value_init (TREE_TYPE (decl
),
14758 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14759 init
= get_target_expr_sfinae (init
, complain
);
14760 if (TREE_CODE (init
) == TARGET_EXPR
)
14761 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14767 /* Like tsubst, but deals with expressions. This function just replaces
14768 template parms; to finish processing the resultant expression, use
14769 tsubst_copy_and_build or tsubst_expr. */
14772 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14774 enum tree_code code
;
14777 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14780 code
= TREE_CODE (t
);
14785 r
= retrieve_local_specialization (t
);
14787 if (r
== NULL_TREE
)
14789 /* We get here for a use of 'this' in an NSDMI. */
14790 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14791 return current_class_ptr
;
14793 /* This can happen for a parameter name used later in a function
14794 declaration (such as in a late-specified return type). Just
14795 make a dummy decl, since it's only used for its type. */
14796 gcc_assert (cp_unevaluated_operand
!= 0);
14797 r
= tsubst_decl (t
, args
, complain
);
14798 /* Give it the template pattern as its context; its true context
14799 hasn't been instantiated yet and this is good enough for
14801 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14804 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14805 r
= argument_pack_select_arg (r
);
14806 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14807 return error_mark_node
;
14815 if (DECL_TEMPLATE_PARM_P (t
))
14816 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14817 /* There is no need to substitute into namespace-scope
14819 if (DECL_NAMESPACE_SCOPE_P (t
))
14821 /* If ARGS is NULL, then T is known to be non-dependent. */
14822 if (args
== NULL_TREE
)
14823 return scalar_constant_value (t
);
14825 /* Unfortunately, we cannot just call lookup_name here.
14828 template <int I> int f() {
14830 struct S { void g() { E e = a; } };
14833 When we instantiate f<7>::S::g(), say, lookup_name is not
14834 clever enough to find f<7>::a. */
14836 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14837 /*entering_scope=*/0);
14839 for (v
= TYPE_VALUES (enum_type
);
14841 v
= TREE_CHAIN (v
))
14842 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14843 return TREE_VALUE (v
);
14845 /* We didn't find the name. That should never happen; if
14846 name-lookup found it during preliminary parsing, we
14847 should find it again here during instantiation. */
14848 gcc_unreachable ();
14853 if (DECL_CONTEXT (t
))
14857 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14858 /*entering_scope=*/1);
14859 if (ctx
!= DECL_CONTEXT (t
))
14861 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14864 if (complain
& tf_error
)
14865 error ("using invalid field %qD", t
);
14866 return error_mark_node
;
14875 case FUNCTION_DECL
:
14876 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14877 r
= tsubst (t
, args
, complain
, in_decl
);
14878 else if (local_variable_p (t
)
14879 && uses_template_parms (DECL_CONTEXT (t
)))
14881 r
= retrieve_local_specialization (t
);
14882 if (r
== NULL_TREE
)
14884 /* First try name lookup to find the instantiation. */
14885 r
= lookup_name (DECL_NAME (t
));
14886 if (r
&& !is_capture_proxy (r
))
14888 /* Make sure that the one we found is the one we want. */
14889 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14890 if (ctx
!= DECL_CONTEXT (r
))
14898 /* This can happen for a variable used in a
14899 late-specified return type of a local lambda, or for a
14900 local static or constant. Building a new VAR_DECL
14901 should be OK in all those cases. */
14902 r
= tsubst_decl (t
, args
, complain
);
14903 if (local_specializations
)
14904 /* Avoid infinite recursion (79640). */
14905 register_local_specialization (r
, t
);
14906 if (decl_maybe_constant_var_p (r
))
14908 /* We can't call cp_finish_decl, so handle the
14909 initializer by hand. */
14910 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14911 complain
, in_decl
);
14912 if (!processing_template_decl
)
14913 init
= maybe_constant_init (init
);
14914 if (processing_template_decl
14915 ? potential_constant_expression (init
)
14916 : reduced_constant_expression_p (init
))
14917 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14918 = TREE_CONSTANT (r
) = true;
14919 DECL_INITIAL (r
) = init
;
14920 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14922 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14923 complain
, adc_variable_type
);
14925 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14926 || decl_constant_var_p (r
)
14927 || errorcount
|| sorrycount
);
14928 if (!processing_template_decl
14929 && !TREE_STATIC (r
))
14930 r
= process_outer_var_ref (r
, complain
);
14932 /* Remember this for subsequent uses. */
14933 if (local_specializations
)
14934 register_local_specialization (r
, t
);
14936 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14937 r
= argument_pack_select_arg (r
);
14941 if (!mark_used (r
, complain
))
14942 return error_mark_node
;
14945 case NAMESPACE_DECL
:
14949 /* An OVERLOAD will always be a non-dependent overload set; an
14950 overload set from function scope will just be represented with an
14951 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14952 gcc_assert (!uses_template_parms (t
));
14953 /* We must have marked any lookups as persistent. */
14954 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14958 return tsubst_baselink (t
, current_nonlambda_class_type (),
14959 args
, complain
, in_decl
);
14961 case TEMPLATE_DECL
:
14962 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14963 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14964 args
, complain
, in_decl
);
14965 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14966 return tsubst (t
, args
, complain
, in_decl
);
14967 else if (DECL_CLASS_SCOPE_P (t
)
14968 && uses_template_parms (DECL_CONTEXT (t
)))
14970 /* Template template argument like the following example need
14973 template <template <class> class TT> struct C {};
14974 template <class T> struct D {
14975 template <class U> struct E {};
14980 We are processing the template argument `E' in #1 for
14981 the template instantiation #2. Originally, `E' is a
14982 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14983 have to substitute this with one having context `D<int>'. */
14985 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14986 if (dependent_scope_p (context
))
14988 /* When rewriting a constructor into a deduction guide, a
14989 non-dependent name can become dependent, so memtmpl<args>
14990 becomes context::template memtmpl<args>. */
14991 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14992 return build_qualified_name (type
, context
, DECL_NAME (t
),
14995 return lookup_field (context
, DECL_NAME(t
), 0, false);
14998 /* Ordinary template template argument. */
15001 case NON_LVALUE_EXPR
:
15002 case VIEW_CONVERT_EXPR
:
15004 /* Handle location wrappers by substituting the wrapped node
15005 first, *then* reusing the resulting type. Doing the type
15006 first ensures that we handle template parameters and
15007 parameter pack expansions. */
15008 gcc_assert (location_wrapper_p (t
));
15009 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15010 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15014 case REINTERPRET_CAST_EXPR
:
15015 case CONST_CAST_EXPR
:
15016 case STATIC_CAST_EXPR
:
15017 case DYNAMIC_CAST_EXPR
:
15018 case IMPLICIT_CONV_EXPR
:
15022 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15023 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15024 return build1 (code
, type
, op0
);
15028 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15029 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15031 tree expanded
, op
= TREE_OPERAND (t
, 0);
15034 if (SIZEOF_EXPR_TYPE_P (t
))
15035 op
= TREE_TYPE (op
);
15037 ++cp_unevaluated_operand
;
15038 ++c_inhibit_evaluation_warnings
;
15039 /* We only want to compute the number of arguments. */
15040 if (PACK_EXPANSION_P (op
))
15041 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15043 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15044 args
, complain
, in_decl
);
15045 --cp_unevaluated_operand
;
15046 --c_inhibit_evaluation_warnings
;
15048 if (TREE_CODE (expanded
) == TREE_VEC
)
15050 len
= TREE_VEC_LENGTH (expanded
);
15051 /* Set TREE_USED for the benefit of -Wunused. */
15052 for (int i
= 0; i
< len
; i
++)
15053 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15054 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15057 if (expanded
== error_mark_node
)
15058 return error_mark_node
;
15059 else if (PACK_EXPANSION_P (expanded
)
15060 || (TREE_CODE (expanded
) == TREE_VEC
15061 && pack_expansion_args_count (expanded
)))
15064 if (PACK_EXPANSION_P (expanded
))
15066 else if (TREE_VEC_LENGTH (expanded
) == 1)
15067 expanded
= TREE_VEC_ELT (expanded
, 0);
15069 expanded
= make_argument_pack (expanded
);
15071 if (TYPE_P (expanded
))
15072 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15073 complain
& tf_error
);
15075 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15076 complain
& tf_error
);
15079 return build_int_cst (size_type_node
, len
);
15081 if (SIZEOF_EXPR_TYPE_P (t
))
15083 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15084 args
, complain
, in_decl
);
15085 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15086 r
= build1 (SIZEOF_EXPR
,
15087 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15088 SIZEOF_EXPR_TYPE_P (r
) = 1;
15095 case TRUTH_NOT_EXPR
:
15098 case UNARY_PLUS_EXPR
: /* Unary + */
15100 case AT_ENCODE_EXPR
:
15104 case REALPART_EXPR
:
15105 case IMAGPART_EXPR
:
15108 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15109 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15110 return build1 (code
, type
, op0
);
15113 case COMPONENT_REF
:
15118 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15119 name
= TREE_OPERAND (t
, 1);
15120 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15122 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15123 complain
, in_decl
);
15124 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15126 else if (TREE_CODE (name
) == SCOPE_REF
15127 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15129 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15130 complain
, in_decl
);
15131 name
= TREE_OPERAND (name
, 1);
15132 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15133 complain
, in_decl
);
15134 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15135 name
= build_qualified_name (/*type=*/NULL_TREE
,
15137 /*template_p=*/false);
15139 else if (BASELINK_P (name
))
15140 name
= tsubst_baselink (name
,
15141 non_reference (TREE_TYPE (object
)),
15145 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15146 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15152 case TRUNC_DIV_EXPR
:
15153 case CEIL_DIV_EXPR
:
15154 case FLOOR_DIV_EXPR
:
15155 case ROUND_DIV_EXPR
:
15156 case EXACT_DIV_EXPR
:
15160 case TRUNC_MOD_EXPR
:
15161 case FLOOR_MOD_EXPR
:
15162 case TRUTH_ANDIF_EXPR
:
15163 case TRUTH_ORIF_EXPR
:
15164 case TRUTH_AND_EXPR
:
15165 case TRUTH_OR_EXPR
:
15178 case COMPOUND_EXPR
:
15181 case PREDECREMENT_EXPR
:
15182 case PREINCREMENT_EXPR
:
15183 case POSTDECREMENT_EXPR
:
15184 case POSTINCREMENT_EXPR
:
15186 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15187 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15188 return build_nt (code
, op0
, op1
);
15193 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15194 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15195 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15196 QUALIFIED_NAME_IS_TEMPLATE (t
));
15201 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15202 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15203 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15208 int n
= VL_EXP_OPERAND_LENGTH (t
);
15209 tree result
= build_vl_exp (CALL_EXPR
, n
);
15211 for (i
= 0; i
< n
; i
++)
15212 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15213 complain
, in_decl
);
15219 case PSEUDO_DTOR_EXPR
:
15220 case VEC_PERM_EXPR
:
15222 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15223 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15224 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15225 r
= build_nt (code
, op0
, op1
, op2
);
15226 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15232 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15233 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15234 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15235 r
= build_nt (code
, op0
, op1
, op2
);
15236 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15242 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15243 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15244 r
= build_nt (code
, op0
, op1
);
15245 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15246 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15250 case TEMPLATE_ID_EXPR
:
15252 /* Substituted template arguments */
15253 tree fn
= TREE_OPERAND (t
, 0);
15254 tree targs
= TREE_OPERAND (t
, 1);
15256 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15258 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15260 return lookup_template_function (fn
, targs
);
15265 tree purpose
, value
, chain
;
15267 if (t
== void_list_node
)
15270 purpose
= TREE_PURPOSE (t
);
15272 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15273 value
= TREE_VALUE (t
);
15275 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15276 chain
= TREE_CHAIN (t
);
15277 if (chain
&& chain
!= void_type_node
)
15278 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15279 if (purpose
== TREE_PURPOSE (t
)
15280 && value
== TREE_VALUE (t
)
15281 && chain
== TREE_CHAIN (t
))
15283 return tree_cons (purpose
, value
, chain
);
15288 case ENUMERAL_TYPE
:
15290 case TEMPLATE_TYPE_PARM
:
15291 case TEMPLATE_TEMPLATE_PARM
:
15292 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15293 case TEMPLATE_PARM_INDEX
:
15295 case REFERENCE_TYPE
:
15297 case FUNCTION_TYPE
:
15300 case TYPENAME_TYPE
:
15301 case UNBOUND_CLASS_TEMPLATE
:
15303 case DECLTYPE_TYPE
:
15305 return tsubst (t
, args
, complain
, in_decl
);
15309 /* Fall through. */
15310 case IDENTIFIER_NODE
:
15311 if (IDENTIFIER_CONV_OP_P (t
))
15313 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15314 return make_conv_op_name (new_type
);
15320 /* This is handled by tsubst_copy_and_build. */
15321 gcc_unreachable ();
15325 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15326 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15327 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15330 case CLEANUP_POINT_EXPR
:
15331 /* We shouldn't have built any of these during initial template
15332 generation. Instead, they should be built during instantiation
15333 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15334 gcc_unreachable ();
15338 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
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 r
= build2 (code
, type
, op0
, op1
);
15342 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15343 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15344 && !(complain
& tf_error
))
15345 return error_mark_node
;
15349 case EXPR_PACK_EXPANSION
:
15350 error ("invalid use of pack expansion expression");
15351 return error_mark_node
;
15353 case NONTYPE_ARGUMENT_PACK
:
15354 error ("use %<...%> to expand argument pack");
15355 return error_mark_node
;
15358 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15366 /* Instantiate any typedefs in the type. */
15367 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15368 r
= fold_convert (type
, t
);
15369 gcc_assert (TREE_CODE (r
) == code
);
15374 /* These can sometimes show up in a partial instantiation, but never
15375 involve template parms. */
15376 gcc_assert (!uses_template_parms (t
));
15379 case UNARY_LEFT_FOLD_EXPR
:
15380 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15381 case UNARY_RIGHT_FOLD_EXPR
:
15382 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15383 case BINARY_LEFT_FOLD_EXPR
:
15384 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15385 case BINARY_RIGHT_FOLD_EXPR
:
15386 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15390 case DEBUG_BEGIN_STMT
:
15391 /* ??? There's no point in copying it for now, but maybe some
15392 day it will contain more information, such as a pointer back
15393 to the containing function, inlined copy or so. */
15397 /* We shouldn't get here, but keep going if !flag_checking. */
15399 gcc_unreachable ();
15404 /* Helper function for tsubst_omp_clauses, used for instantiation of
15405 OMP_CLAUSE_DECL of clauses. */
15408 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15411 if (decl
== NULL_TREE
)
15414 /* Handle an OpenMP array section represented as a TREE_LIST (or
15415 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15416 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15417 TREE_LIST. We can handle it exactly the same as an array section
15418 (purpose, value, and a chain), even though the nomenclature
15419 (low_bound, length, etc) is different. */
15420 if (TREE_CODE (decl
) == TREE_LIST
)
15423 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15424 /*integral_constant_expression_p=*/false);
15425 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15426 /*integral_constant_expression_p=*/false);
15427 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15429 if (TREE_PURPOSE (decl
) == low_bound
15430 && TREE_VALUE (decl
) == length
15431 && TREE_CHAIN (decl
) == chain
)
15433 tree ret
= tree_cons (low_bound
, length
, chain
);
15434 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15435 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15438 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15439 /*integral_constant_expression_p=*/false);
15440 /* Undo convert_from_reference tsubst_expr could have called. */
15442 && REFERENCE_REF_P (ret
)
15443 && !REFERENCE_REF_P (decl
))
15444 ret
= TREE_OPERAND (ret
, 0);
15448 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15451 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15452 tree args
, tsubst_flags_t complain
, tree in_decl
)
15454 tree new_clauses
= NULL_TREE
, nc
, oc
;
15455 tree linear_no_step
= NULL_TREE
;
15457 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15459 nc
= copy_node (oc
);
15460 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15463 switch (OMP_CLAUSE_CODE (nc
))
15465 case OMP_CLAUSE_LASTPRIVATE
:
15466 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15468 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15469 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15470 in_decl
, /*integral_constant_expression_p=*/false);
15471 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15472 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15475 case OMP_CLAUSE_PRIVATE
:
15476 case OMP_CLAUSE_SHARED
:
15477 case OMP_CLAUSE_FIRSTPRIVATE
:
15478 case OMP_CLAUSE_COPYIN
:
15479 case OMP_CLAUSE_COPYPRIVATE
:
15480 case OMP_CLAUSE_UNIFORM
:
15481 case OMP_CLAUSE_DEPEND
:
15482 case OMP_CLAUSE_FROM
:
15483 case OMP_CLAUSE_TO
:
15484 case OMP_CLAUSE_MAP
:
15485 case OMP_CLAUSE_USE_DEVICE_PTR
:
15486 case OMP_CLAUSE_IS_DEVICE_PTR
:
15487 OMP_CLAUSE_DECL (nc
)
15488 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15491 case OMP_CLAUSE_TILE
:
15492 case OMP_CLAUSE_IF
:
15493 case OMP_CLAUSE_NUM_THREADS
:
15494 case OMP_CLAUSE_SCHEDULE
:
15495 case OMP_CLAUSE_COLLAPSE
:
15496 case OMP_CLAUSE_FINAL
:
15497 case OMP_CLAUSE_DEVICE
:
15498 case OMP_CLAUSE_DIST_SCHEDULE
:
15499 case OMP_CLAUSE_NUM_TEAMS
:
15500 case OMP_CLAUSE_THREAD_LIMIT
:
15501 case OMP_CLAUSE_SAFELEN
:
15502 case OMP_CLAUSE_SIMDLEN
:
15503 case OMP_CLAUSE_NUM_TASKS
:
15504 case OMP_CLAUSE_GRAINSIZE
:
15505 case OMP_CLAUSE_PRIORITY
:
15506 case OMP_CLAUSE_ORDERED
:
15507 case OMP_CLAUSE_HINT
:
15508 case OMP_CLAUSE_NUM_GANGS
:
15509 case OMP_CLAUSE_NUM_WORKERS
:
15510 case OMP_CLAUSE_VECTOR_LENGTH
:
15511 case OMP_CLAUSE_WORKER
:
15512 case OMP_CLAUSE_VECTOR
:
15513 case OMP_CLAUSE_ASYNC
:
15514 case OMP_CLAUSE_WAIT
:
15515 OMP_CLAUSE_OPERAND (nc
, 0)
15516 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15517 in_decl
, /*integral_constant_expression_p=*/false);
15519 case OMP_CLAUSE_REDUCTION
:
15520 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15522 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15523 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15525 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15526 complain
, in_decl
);
15527 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15528 = build_qualified_name (NULL_TREE
, scope
,
15529 TREE_OPERAND (placeholder
, 1),
15533 gcc_assert (identifier_p (placeholder
));
15535 OMP_CLAUSE_DECL (nc
)
15536 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15539 case OMP_CLAUSE_GANG
:
15540 case OMP_CLAUSE_ALIGNED
:
15541 OMP_CLAUSE_DECL (nc
)
15542 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15544 OMP_CLAUSE_OPERAND (nc
, 1)
15545 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15546 in_decl
, /*integral_constant_expression_p=*/false);
15548 case OMP_CLAUSE_LINEAR
:
15549 OMP_CLAUSE_DECL (nc
)
15550 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15552 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15554 gcc_assert (!linear_no_step
);
15555 linear_no_step
= nc
;
15557 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15558 OMP_CLAUSE_LINEAR_STEP (nc
)
15559 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15560 complain
, in_decl
);
15562 OMP_CLAUSE_LINEAR_STEP (nc
)
15563 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15565 /*integral_constant_expression_p=*/false);
15567 case OMP_CLAUSE_NOWAIT
:
15568 case OMP_CLAUSE_DEFAULT
:
15569 case OMP_CLAUSE_UNTIED
:
15570 case OMP_CLAUSE_MERGEABLE
:
15571 case OMP_CLAUSE_INBRANCH
:
15572 case OMP_CLAUSE_NOTINBRANCH
:
15573 case OMP_CLAUSE_PROC_BIND
:
15574 case OMP_CLAUSE_FOR
:
15575 case OMP_CLAUSE_PARALLEL
:
15576 case OMP_CLAUSE_SECTIONS
:
15577 case OMP_CLAUSE_TASKGROUP
:
15578 case OMP_CLAUSE_NOGROUP
:
15579 case OMP_CLAUSE_THREADS
:
15580 case OMP_CLAUSE_SIMD
:
15581 case OMP_CLAUSE_DEFAULTMAP
:
15582 case OMP_CLAUSE_INDEPENDENT
:
15583 case OMP_CLAUSE_AUTO
:
15584 case OMP_CLAUSE_SEQ
:
15587 gcc_unreachable ();
15589 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15590 switch (OMP_CLAUSE_CODE (nc
))
15592 case OMP_CLAUSE_SHARED
:
15593 case OMP_CLAUSE_PRIVATE
:
15594 case OMP_CLAUSE_FIRSTPRIVATE
:
15595 case OMP_CLAUSE_LASTPRIVATE
:
15596 case OMP_CLAUSE_COPYPRIVATE
:
15597 case OMP_CLAUSE_LINEAR
:
15598 case OMP_CLAUSE_REDUCTION
:
15599 case OMP_CLAUSE_USE_DEVICE_PTR
:
15600 case OMP_CLAUSE_IS_DEVICE_PTR
:
15601 /* tsubst_expr on SCOPE_REF results in returning
15602 finish_non_static_data_member result. Undo that here. */
15603 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15604 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15605 == IDENTIFIER_NODE
))
15607 tree t
= OMP_CLAUSE_DECL (nc
);
15610 switch (TREE_CODE (v
))
15612 case COMPONENT_REF
:
15616 case POINTER_PLUS_EXPR
:
15617 v
= TREE_OPERAND (v
, 0);
15620 if (DECL_CONTEXT (v
) == current_function_decl
15621 && DECL_ARTIFICIAL (v
)
15622 && DECL_NAME (v
) == this_identifier
)
15623 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15630 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15631 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15632 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15633 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15634 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15636 tree decl
= OMP_CLAUSE_DECL (nc
);
15639 retrofit_lang_decl (decl
);
15640 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15649 new_clauses
= nreverse (new_clauses
);
15650 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15652 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15653 if (linear_no_step
)
15654 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15655 if (nc
== linear_no_step
)
15657 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15661 return new_clauses
;
15664 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15667 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15670 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15672 tree purpose
, value
, chain
;
15677 if (TREE_CODE (t
) != TREE_LIST
)
15678 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15679 /*function_p=*/false,
15680 /*integral_constant_expression_p=*/false);
15682 if (t
== void_list_node
)
15685 purpose
= TREE_PURPOSE (t
);
15687 purpose
= RECUR (purpose
);
15688 value
= TREE_VALUE (t
);
15691 if (TREE_CODE (value
) != LABEL_DECL
)
15692 value
= RECUR (value
);
15695 value
= lookup_label (DECL_NAME (value
));
15696 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15697 TREE_USED (value
) = 1;
15700 chain
= TREE_CHAIN (t
);
15701 if (chain
&& chain
!= void_type_node
)
15702 chain
= RECUR (chain
);
15703 return tree_cons (purpose
, value
, chain
);
15707 /* Used to temporarily communicate the list of #pragma omp parallel
15708 clauses to #pragma omp for instantiation if they are combined
15711 static tree
*omp_parallel_combined_clauses
;
15713 /* Substitute one OMP_FOR iterator. */
15716 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15717 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15718 tree args
, tsubst_flags_t complain
, tree in_decl
,
15719 bool integral_constant_expression_p
)
15721 #define RECUR(NODE) \
15722 tsubst_expr ((NODE), args, complain, in_decl, \
15723 integral_constant_expression_p)
15724 tree decl
, init
, cond
, incr
;
15726 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15727 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15729 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15731 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15732 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15735 decl
= TREE_OPERAND (init
, 0);
15736 init
= TREE_OPERAND (init
, 1);
15737 tree decl_expr
= NULL_TREE
;
15738 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15740 /* We need to jump through some hoops to handle declarations in the
15741 init-statement, since we might need to handle auto deduction,
15742 but we need to keep control of initialization. */
15744 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15745 decl
= tsubst_decl (decl
, args
, complain
);
15749 if (TREE_CODE (decl
) == SCOPE_REF
)
15751 decl
= RECUR (decl
);
15752 if (TREE_CODE (decl
) == COMPONENT_REF
)
15756 switch (TREE_CODE (v
))
15758 case COMPONENT_REF
:
15762 case POINTER_PLUS_EXPR
:
15763 v
= TREE_OPERAND (v
, 0);
15766 if (DECL_CONTEXT (v
) == current_function_decl
15767 && DECL_ARTIFICIAL (v
)
15768 && DECL_NAME (v
) == this_identifier
)
15770 decl
= TREE_OPERAND (decl
, 1);
15771 decl
= omp_privatize_field (decl
, false);
15781 decl
= RECUR (decl
);
15783 init
= RECUR (init
);
15785 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15786 if (auto_node
&& init
)
15788 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
15790 gcc_assert (!type_dependent_expression_p (decl
));
15792 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15796 /* Declare the variable, but don't let that initialize it. */
15797 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15798 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15800 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15803 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15804 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15805 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15807 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15808 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15809 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15810 NOP_EXPR
, rhs
, complain
);
15813 incr
= RECUR (incr
);
15814 TREE_VEC_ELT (declv
, i
) = decl
;
15815 TREE_VEC_ELT (initv
, i
) = init
;
15816 TREE_VEC_ELT (condv
, i
) = cond
;
15817 TREE_VEC_ELT (incrv
, i
) = incr
;
15823 /* Declare and initialize the variable. */
15831 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15833 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15835 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15836 && OMP_CLAUSE_DECL (*pc
) == decl
)
15838 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15839 && OMP_CLAUSE_DECL (*pc
) == decl
)
15843 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15845 *pc
= OMP_CLAUSE_CHAIN (c
);
15846 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15849 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15850 && OMP_CLAUSE_DECL (*pc
) == decl
)
15852 error ("iteration variable %qD should not be firstprivate",
15854 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15856 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15857 && OMP_CLAUSE_DECL (*pc
) == decl
)
15859 error ("iteration variable %qD should not be reduction",
15861 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15864 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15869 if (*pc
== NULL_TREE
)
15871 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15872 OMP_CLAUSE_DECL (c
) = decl
;
15873 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15876 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15881 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15882 if (COMPARISON_CLASS_P (cond
))
15884 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15885 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15886 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15889 cond
= RECUR (cond
);
15890 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15891 switch (TREE_CODE (incr
))
15893 case PREINCREMENT_EXPR
:
15894 case PREDECREMENT_EXPR
:
15895 case POSTINCREMENT_EXPR
:
15896 case POSTDECREMENT_EXPR
:
15897 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15898 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15901 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15902 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15904 tree rhs
= TREE_OPERAND (incr
, 1);
15905 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15906 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15907 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15908 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15909 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15913 incr
= RECUR (incr
);
15916 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15917 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15919 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15920 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15921 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15922 TREE_TYPE (decl
), lhs
,
15923 RECUR (TREE_OPERAND (incr
, 2))));
15925 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15926 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15927 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15929 tree rhs
= TREE_OPERAND (incr
, 2);
15930 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15931 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15932 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15933 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15934 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15938 incr
= RECUR (incr
);
15941 incr
= RECUR (incr
);
15945 TREE_VEC_ELT (declv
, i
) = decl
;
15946 TREE_VEC_ELT (initv
, i
) = init
;
15947 TREE_VEC_ELT (condv
, i
) = cond
;
15948 TREE_VEC_ELT (incrv
, i
) = incr
;
15952 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15953 of OMP_TARGET's body. */
15956 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15958 *walk_subtrees
= 0;
15959 switch (TREE_CODE (*tp
))
15964 case STATEMENT_LIST
:
15965 *walk_subtrees
= 1;
15973 /* Helper function for tsubst_expr. For decomposition declaration
15974 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15975 also the corresponding decls representing the identifiers
15976 of the decomposition declaration. Return DECL if successful
15977 or error_mark_node otherwise, set *FIRST to the first decl
15978 in the list chained through DECL_CHAIN and *CNT to the number
15982 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15983 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15986 tree decl2
, decl3
, prev
= decl
;
15988 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15989 for (decl2
= DECL_CHAIN (pattern_decl
);
15992 && DECL_DECOMPOSITION_P (decl2
)
15993 && DECL_NAME (decl2
);
15994 decl2
= DECL_CHAIN (decl2
))
15996 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15998 gcc_assert (errorcount
);
15999 return error_mark_node
;
16002 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16003 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16004 tree v
= DECL_VALUE_EXPR (decl2
);
16005 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16006 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16007 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16008 SET_DECL_VALUE_EXPR (decl2
, v
);
16009 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16011 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16012 maybe_push_decl (decl3
);
16013 if (error_operand_p (decl3
))
16014 decl
= error_mark_node
;
16015 else if (decl
!= error_mark_node
16016 && DECL_CHAIN (decl3
) != prev
)
16018 gcc_assert (errorcount
);
16019 decl
= error_mark_node
;
16028 /* Like tsubst_copy for expressions, etc. but also does semantic
16032 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16033 bool integral_constant_expression_p
)
16035 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16036 #define RECUR(NODE) \
16037 tsubst_expr ((NODE), args, complain, in_decl, \
16038 integral_constant_expression_p)
16044 if (t
== NULL_TREE
|| t
== error_mark_node
)
16047 loc
= input_location
;
16048 if (EXPR_HAS_LOCATION (t
))
16049 input_location
= EXPR_LOCATION (t
);
16050 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16051 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16053 switch (TREE_CODE (t
))
16055 case STATEMENT_LIST
:
16057 tree_stmt_iterator i
;
16058 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16059 RECUR (tsi_stmt (i
));
16063 case CTOR_INITIALIZER
:
16064 finish_mem_initializers (tsubst_initializer_list
16065 (TREE_OPERAND (t
, 0), args
));
16069 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16073 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16074 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16075 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16077 finish_expr_stmt (tmp
);
16081 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16082 /*attribs=*/NULL_TREE
);
16087 tree decl
, pattern_decl
;
16090 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16091 if (TREE_CODE (decl
) == LABEL_DECL
)
16092 finish_label_decl (DECL_NAME (decl
));
16093 else if (TREE_CODE (decl
) == USING_DECL
)
16095 tree scope
= USING_DECL_SCOPE (decl
);
16096 tree name
= DECL_NAME (decl
);
16098 scope
= tsubst (scope
, args
, complain
, in_decl
);
16099 decl
= lookup_qualified_name (scope
, name
,
16100 /*is_type_p=*/false,
16101 /*complain=*/false);
16102 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16103 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16105 finish_local_using_decl (decl
, scope
, name
);
16107 else if (is_capture_proxy (decl
)
16108 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16110 /* We're in tsubst_lambda_expr, we've already inserted a new
16111 capture proxy, so look it up and register it. */
16113 if (DECL_PACK_P (decl
))
16115 inst
= (retrieve_local_specialization
16116 (DECL_CAPTURED_VARIABLE (decl
)));
16117 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16121 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16122 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16123 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16125 register_local_specialization (inst
, decl
);
16128 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16129 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16130 /* Don't copy the old closure; we'll create a new one in
16131 tsubst_lambda_expr. */
16135 init
= DECL_INITIAL (decl
);
16136 decl
= tsubst (decl
, args
, complain
, in_decl
);
16137 if (decl
!= error_mark_node
)
16139 /* By marking the declaration as instantiated, we avoid
16140 trying to instantiate it. Since instantiate_decl can't
16141 handle local variables, and since we've already done
16142 all that needs to be done, that's the right thing to
16145 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16147 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16148 /* Anonymous aggregates are a special case. */
16149 finish_anon_union (decl
);
16150 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16152 DECL_CONTEXT (decl
) = current_function_decl
;
16153 if (DECL_NAME (decl
) == this_identifier
)
16155 tree lam
= DECL_CONTEXT (current_function_decl
);
16156 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16157 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16159 insert_capture_proxy (decl
);
16161 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16162 /* We already did a pushtag. */;
16163 else if (TREE_CODE (decl
) == FUNCTION_DECL
16164 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16165 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16167 DECL_CONTEXT (decl
) = NULL_TREE
;
16169 DECL_CONTEXT (decl
) = current_function_decl
;
16170 cp_check_omp_declare_reduction (decl
);
16174 int const_init
= false;
16175 maybe_push_decl (decl
);
16177 && DECL_PRETTY_FUNCTION_P (decl
))
16179 /* For __PRETTY_FUNCTION__ we have to adjust the
16181 const char *const name
16182 = cxx_printable_name (current_function_decl
, 2);
16183 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16186 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16189 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16192 && DECL_DECOMPOSITION_P (decl
)
16193 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16198 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16199 complain
, in_decl
, &first
, &cnt
);
16200 if (ndecl
!= error_mark_node
)
16201 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16202 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16203 if (ndecl
!= error_mark_node
)
16204 cp_finish_decomp (ndecl
, first
, cnt
);
16207 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16216 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16217 RECUR (FOR_INIT_STMT (t
));
16218 finish_init_stmt (stmt
);
16219 tmp
= RECUR (FOR_COND (t
));
16220 finish_for_cond (tmp
, stmt
, false, 0);
16221 tmp
= RECUR (FOR_EXPR (t
));
16222 finish_for_expr (tmp
, stmt
);
16224 bool prev
= note_iteration_stmt_body_start ();
16225 RECUR (FOR_BODY (t
));
16226 note_iteration_stmt_body_end (prev
);
16228 finish_for_stmt (stmt
);
16231 case RANGE_FOR_STMT
:
16233 /* Construct another range_for, if this is not a final
16234 substitution (for inside inside a generic lambda of a
16235 template). Otherwise convert to a regular for. */
16237 stmt
= (processing_template_decl
16238 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16239 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16240 decl
= RANGE_FOR_DECL (t
);
16241 decl
= tsubst (decl
, args
, complain
, in_decl
);
16242 maybe_push_decl (decl
);
16243 expr
= RECUR (RANGE_FOR_EXPR (t
));
16245 tree decomp_first
= NULL_TREE
;
16246 unsigned decomp_cnt
= 0;
16247 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16248 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16250 &decomp_first
, &decomp_cnt
);
16252 if (processing_template_decl
)
16254 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16255 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16256 finish_range_for_decl (stmt
, decl
, expr
);
16260 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16261 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16262 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16263 decomp_first
, decomp_cnt
,
16264 RANGE_FOR_IVDEP (t
), unroll
);
16267 bool prev
= note_iteration_stmt_body_start ();
16268 RECUR (RANGE_FOR_BODY (t
));
16269 note_iteration_stmt_body_end (prev
);
16270 finish_for_stmt (stmt
);
16275 stmt
= begin_while_stmt ();
16276 tmp
= RECUR (WHILE_COND (t
));
16277 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16279 bool prev
= note_iteration_stmt_body_start ();
16280 RECUR (WHILE_BODY (t
));
16281 note_iteration_stmt_body_end (prev
);
16283 finish_while_stmt (stmt
);
16287 stmt
= begin_do_stmt ();
16289 bool prev
= note_iteration_stmt_body_start ();
16290 RECUR (DO_BODY (t
));
16291 note_iteration_stmt_body_end (prev
);
16293 finish_do_body (stmt
);
16294 tmp
= RECUR (DO_COND (t
));
16295 finish_do_stmt (tmp
, stmt
, false, 0);
16299 stmt
= begin_if_stmt ();
16300 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16301 tmp
= RECUR (IF_COND (t
));
16302 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16303 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16304 /* Don't instantiate the THEN_CLAUSE. */;
16307 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16309 ++c_inhibit_evaluation_warnings
;
16310 RECUR (THEN_CLAUSE (t
));
16312 --c_inhibit_evaluation_warnings
;
16314 finish_then_clause (stmt
);
16316 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16317 /* Don't instantiate the ELSE_CLAUSE. */;
16318 else if (ELSE_CLAUSE (t
))
16320 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16321 begin_else_clause (stmt
);
16323 ++c_inhibit_evaluation_warnings
;
16324 RECUR (ELSE_CLAUSE (t
));
16326 --c_inhibit_evaluation_warnings
;
16327 finish_else_clause (stmt
);
16330 finish_if_stmt (stmt
);
16334 if (BIND_EXPR_BODY_BLOCK (t
))
16335 stmt
= begin_function_body ();
16337 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16338 ? BCS_TRY_BLOCK
: 0);
16340 RECUR (BIND_EXPR_BODY (t
));
16342 if (BIND_EXPR_BODY_BLOCK (t
))
16343 finish_function_body (stmt
);
16345 finish_compound_stmt (stmt
);
16349 finish_break_stmt ();
16352 case CONTINUE_STMT
:
16353 finish_continue_stmt ();
16357 stmt
= begin_switch_stmt ();
16358 tmp
= RECUR (SWITCH_STMT_COND (t
));
16359 finish_switch_cond (tmp
, stmt
);
16360 RECUR (SWITCH_STMT_BODY (t
));
16361 finish_switch_stmt (stmt
);
16364 case CASE_LABEL_EXPR
:
16366 tree low
= RECUR (CASE_LOW (t
));
16367 tree high
= RECUR (CASE_HIGH (t
));
16368 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16369 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16370 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16371 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16377 tree decl
= LABEL_EXPR_LABEL (t
);
16380 label
= finish_label_stmt (DECL_NAME (decl
));
16381 if (TREE_CODE (label
) == LABEL_DECL
)
16382 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16383 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16384 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16389 tmp
= GOTO_DESTINATION (t
);
16390 if (TREE_CODE (tmp
) != LABEL_DECL
)
16391 /* Computed goto's must be tsubst'd into. On the other hand,
16392 non-computed gotos must not be; the identifier in question
16393 will have no binding. */
16396 tmp
= DECL_NAME (tmp
);
16397 finish_goto_stmt (tmp
);
16402 tree string
= RECUR (ASM_STRING (t
));
16403 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16404 complain
, in_decl
);
16405 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16406 complain
, in_decl
);
16407 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16408 complain
, in_decl
);
16409 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16410 complain
, in_decl
);
16411 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16413 tree asm_expr
= tmp
;
16414 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16415 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16416 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16423 stmt
= begin_try_block ();
16424 RECUR (TRY_STMTS (t
));
16425 finish_cleanup_try_block (stmt
);
16426 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16430 tree compound_stmt
= NULL_TREE
;
16432 if (FN_TRY_BLOCK_P (t
))
16433 stmt
= begin_function_try_block (&compound_stmt
);
16435 stmt
= begin_try_block ();
16437 RECUR (TRY_STMTS (t
));
16439 if (FN_TRY_BLOCK_P (t
))
16440 finish_function_try_block (stmt
);
16442 finish_try_block (stmt
);
16444 RECUR (TRY_HANDLERS (t
));
16445 if (FN_TRY_BLOCK_P (t
))
16446 finish_function_handler_sequence (stmt
, compound_stmt
);
16448 finish_handler_sequence (stmt
);
16454 tree decl
= HANDLER_PARMS (t
);
16458 decl
= tsubst (decl
, args
, complain
, in_decl
);
16459 /* Prevent instantiate_decl from trying to instantiate
16460 this variable. We've already done all that needs to be
16462 if (decl
!= error_mark_node
)
16463 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16465 stmt
= begin_handler ();
16466 finish_handler_parms (decl
, stmt
);
16467 RECUR (HANDLER_BODY (t
));
16468 finish_handler (stmt
);
16473 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16474 if (CLASS_TYPE_P (tmp
))
16476 /* Local classes are not independent templates; they are
16477 instantiated along with their containing function. And this
16478 way we don't have to deal with pushing out of one local class
16479 to instantiate a member of another local class. */
16480 /* Closures are handled by the LAMBDA_EXPR. */
16481 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16482 complete_type (tmp
);
16483 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16485 || (TREE_CODE (fld
) == FUNCTION_DECL
16486 && !DECL_ARTIFICIAL (fld
)))
16487 && DECL_TEMPLATE_INSTANTIATION (fld
))
16488 instantiate_decl (fld
, /*defer_ok=*/false,
16489 /*expl_inst_class=*/false);
16493 case STATIC_ASSERT
:
16497 ++c_inhibit_evaluation_warnings
;
16499 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16502 /*integral_constant_expression_p=*/true);
16503 --c_inhibit_evaluation_warnings
;
16505 finish_static_assert (condition
,
16506 STATIC_ASSERT_MESSAGE (t
),
16507 STATIC_ASSERT_SOURCE_LOCATION (t
),
16508 /*member_p=*/false);
16513 case OACC_PARALLEL
:
16514 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16516 stmt
= begin_omp_parallel ();
16517 RECUR (OMP_BODY (t
));
16518 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16522 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16523 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16524 complain
, in_decl
);
16525 if (OMP_PARALLEL_COMBINED (t
))
16526 omp_parallel_combined_clauses
= &tmp
;
16527 stmt
= begin_omp_parallel ();
16528 RECUR (OMP_PARALLEL_BODY (t
));
16529 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16530 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16531 = OMP_PARALLEL_COMBINED (t
);
16532 pop_omp_privatization_clauses (r
);
16536 r
= push_omp_privatization_clauses (false);
16537 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16538 complain
, in_decl
);
16539 stmt
= begin_omp_task ();
16540 RECUR (OMP_TASK_BODY (t
));
16541 finish_omp_task (tmp
, stmt
);
16542 pop_omp_privatization_clauses (r
);
16547 case OMP_DISTRIBUTE
:
16551 tree clauses
, body
, pre_body
;
16552 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16553 tree orig_declv
= NULL_TREE
;
16554 tree incrv
= NULL_TREE
;
16555 enum c_omp_region_type ort
= C_ORT_OMP
;
16558 if (TREE_CODE (t
) == OACC_LOOP
)
16561 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16562 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16564 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16566 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16567 if (OMP_FOR_ORIG_DECLS (t
))
16568 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16569 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16570 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16571 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16574 stmt
= begin_omp_structured_block ();
16576 pre_body
= push_stmt_list ();
16577 RECUR (OMP_FOR_PRE_BODY (t
));
16578 pre_body
= pop_stmt_list (pre_body
);
16580 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16581 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16582 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16583 incrv
, &clauses
, args
, complain
, in_decl
,
16584 integral_constant_expression_p
);
16585 omp_parallel_combined_clauses
= NULL
;
16587 body
= push_stmt_list ();
16588 RECUR (OMP_FOR_BODY (t
));
16589 body
= pop_stmt_list (body
);
16591 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16592 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16593 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16597 t
= make_node (TREE_CODE (t
));
16598 TREE_TYPE (t
) = void_type_node
;
16599 OMP_FOR_BODY (t
) = body
;
16600 OMP_FOR_PRE_BODY (t
) = pre_body
;
16601 OMP_FOR_CLAUSES (t
) = clauses
;
16602 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16606 add_stmt (finish_omp_structured_block (stmt
));
16607 pop_omp_privatization_clauses (r
);
16612 omp_parallel_combined_clauses
= NULL
;
16617 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16618 && OMP_TEAMS_COMBINED (t
));
16619 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16621 stmt
= push_stmt_list ();
16622 RECUR (OMP_BODY (t
));
16623 stmt
= pop_stmt_list (stmt
);
16626 OMP_BODY (t
) = stmt
;
16627 OMP_CLAUSES (t
) = tmp
;
16629 pop_omp_privatization_clauses (r
);
16633 case OMP_TARGET_DATA
:
16635 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16636 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16638 keep_next_level (true);
16639 stmt
= begin_omp_structured_block ();
16641 RECUR (OMP_BODY (t
));
16642 stmt
= finish_omp_structured_block (stmt
);
16645 OMP_BODY (t
) = stmt
;
16646 OMP_CLAUSES (t
) = tmp
;
16647 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16649 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16652 /* For combined target teams, ensure the num_teams and
16653 thread_limit clause expressions are evaluated on the host,
16654 before entering the target construct. */
16656 for (c
= OMP_TEAMS_CLAUSES (teams
);
16657 c
; c
= OMP_CLAUSE_CHAIN (c
))
16658 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16659 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16660 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16662 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16663 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16664 if (expr
== error_mark_node
)
16666 tmp
= TARGET_EXPR_SLOT (expr
);
16668 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16669 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16670 OMP_CLAUSE_FIRSTPRIVATE
);
16671 OMP_CLAUSE_DECL (tc
) = tmp
;
16672 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16673 OMP_TARGET_CLAUSES (t
) = tc
;
16682 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16683 complain
, in_decl
);
16684 OACC_DECLARE_CLAUSES (t
) = tmp
;
16688 case OMP_TARGET_UPDATE
:
16689 case OMP_TARGET_ENTER_DATA
:
16690 case OMP_TARGET_EXIT_DATA
:
16691 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16692 complain
, in_decl
);
16694 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16698 case OACC_ENTER_DATA
:
16699 case OACC_EXIT_DATA
:
16701 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16702 complain
, in_decl
);
16704 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16709 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16710 complain
, in_decl
);
16711 stmt
= push_stmt_list ();
16712 RECUR (OMP_BODY (t
));
16713 stmt
= pop_stmt_list (stmt
);
16716 OMP_BODY (t
) = stmt
;
16717 OMP_ORDERED_CLAUSES (t
) = tmp
;
16723 case OMP_TASKGROUP
:
16724 stmt
= push_stmt_list ();
16725 RECUR (OMP_BODY (t
));
16726 stmt
= pop_stmt_list (stmt
);
16729 OMP_BODY (t
) = stmt
;
16734 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16735 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16737 tree op1
= TREE_OPERAND (t
, 1);
16738 tree rhs1
= NULL_TREE
;
16740 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16742 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16743 op1
= TREE_OPERAND (op1
, 1);
16745 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16746 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16747 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16748 NULL_TREE
, NULL_TREE
, rhs1
,
16749 OMP_ATOMIC_SEQ_CST (t
));
16753 tree op1
= TREE_OPERAND (t
, 1);
16754 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16755 tree rhs1
= NULL_TREE
;
16756 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16757 enum tree_code opcode
= NOP_EXPR
;
16758 if (code
== OMP_ATOMIC_READ
)
16760 v
= RECUR (TREE_OPERAND (op1
, 0));
16761 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16763 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16764 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16766 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16767 v
= RECUR (TREE_OPERAND (op1
, 0));
16768 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16769 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16771 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16772 op11
= TREE_OPERAND (op11
, 1);
16774 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16775 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16776 opcode
= TREE_CODE (op11
);
16777 if (opcode
== MODIFY_EXPR
)
16783 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16784 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16786 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16787 OMP_ATOMIC_SEQ_CST (t
));
16791 case TRANSACTION_EXPR
:
16794 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16795 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16797 if (TRANSACTION_EXPR_IS_STMT (t
))
16799 tree body
= TRANSACTION_EXPR_BODY (t
);
16800 tree noex
= NULL_TREE
;
16801 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16803 noex
= MUST_NOT_THROW_COND (body
);
16804 if (noex
== NULL_TREE
)
16805 noex
= boolean_true_node
;
16806 body
= TREE_OPERAND (body
, 0);
16808 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16810 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16814 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16815 RECUR (TRANSACTION_EXPR_BODY (t
)),
16822 case MUST_NOT_THROW_EXPR
:
16824 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16825 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16826 RETURN (build_must_not_throw_expr (op0
, cond
));
16829 case EXPR_PACK_EXPANSION
:
16830 error ("invalid use of pack expansion expression");
16831 RETURN (error_mark_node
);
16833 case NONTYPE_ARGUMENT_PACK
:
16834 error ("use %<...%> to expand argument pack");
16835 RETURN (error_mark_node
);
16837 case COMPOUND_EXPR
:
16838 tmp
= RECUR (TREE_OPERAND (t
, 0));
16839 if (tmp
== NULL_TREE
)
16840 /* If the first operand was a statement, we're done with it. */
16841 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16842 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16843 RECUR (TREE_OPERAND (t
, 1)),
16846 case ANNOTATE_EXPR
:
16847 tmp
= RECUR (TREE_OPERAND (t
, 0));
16848 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16849 TREE_TYPE (tmp
), tmp
,
16850 RECUR (TREE_OPERAND (t
, 1)),
16851 RECUR (TREE_OPERAND (t
, 2))));
16854 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16856 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16857 /*function_p=*/false,
16858 integral_constant_expression_p
));
16861 RETURN (NULL_TREE
);
16863 input_location
= loc
;
16869 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16870 function. For description of the body see comment above
16871 cp_parser_omp_declare_reduction_exprs. */
16874 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16876 if (t
== NULL_TREE
|| t
== error_mark_node
)
16879 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16881 tree_stmt_iterator tsi
;
16884 memset (stmts
, 0, sizeof stmts
);
16885 for (i
= 0, tsi
= tsi_start (t
);
16886 i
< 7 && !tsi_end_p (tsi
);
16887 i
++, tsi_next (&tsi
))
16888 stmts
[i
] = tsi_stmt (tsi
);
16889 gcc_assert (tsi_end_p (tsi
));
16893 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16894 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16895 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16896 args
, complain
, in_decl
);
16897 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16898 args
, complain
, in_decl
);
16899 DECL_CONTEXT (omp_out
) = current_function_decl
;
16900 DECL_CONTEXT (omp_in
) = current_function_decl
;
16901 keep_next_level (true);
16902 tree block
= begin_omp_structured_block ();
16903 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16904 block
= finish_omp_structured_block (block
);
16905 block
= maybe_cleanup_point_expr_void (block
);
16906 add_decl_expr (omp_out
);
16907 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16908 TREE_NO_WARNING (omp_out
) = 1;
16909 add_decl_expr (omp_in
);
16910 finish_expr_stmt (block
);
16914 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16915 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16916 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16917 args
, complain
, in_decl
);
16918 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16919 args
, complain
, in_decl
);
16920 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16921 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16922 keep_next_level (true);
16923 tree block
= begin_omp_structured_block ();
16924 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16925 block
= finish_omp_structured_block (block
);
16926 block
= maybe_cleanup_point_expr_void (block
);
16927 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16928 add_decl_expr (omp_priv
);
16929 add_decl_expr (omp_orig
);
16930 finish_expr_stmt (block
);
16932 add_decl_expr (omp_orig
);
16936 /* T is a postfix-expression that is not being used in a function
16937 call. Return the substituted version of T. */
16940 tsubst_non_call_postfix_expression (tree t
, tree args
,
16941 tsubst_flags_t complain
,
16944 if (TREE_CODE (t
) == SCOPE_REF
)
16945 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16946 /*done=*/false, /*address_p=*/false);
16948 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16949 /*function_p=*/false,
16950 /*integral_constant_expression_p=*/false);
16955 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16956 instantiation context. Instantiating a pack expansion containing a lambda
16957 might result in multiple lambdas all based on the same lambda in the
16961 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16963 tree oldfn
= lambda_function (t
);
16966 tree r
= build_lambda_expr ();
16968 LAMBDA_EXPR_LOCATION (r
)
16969 = LAMBDA_EXPR_LOCATION (t
);
16970 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16971 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16972 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16974 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16975 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16977 record_lambda_scope (r
);
16979 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16980 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16982 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16983 cap
= TREE_CHAIN (cap
))
16985 tree field
= TREE_PURPOSE (cap
);
16986 if (PACK_EXPANSION_P (field
))
16987 field
= PACK_EXPANSION_PATTERN (field
);
16988 field
= tsubst_decl (field
, args
, complain
);
16990 if (field
== error_mark_node
)
16991 return error_mark_node
;
16993 tree init
= TREE_VALUE (cap
);
16994 if (PACK_EXPANSION_P (init
))
16995 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
16997 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
16998 /*fn*/false, /*constexpr*/false);
17000 if (TREE_CODE (field
) == TREE_VEC
)
17002 int len
= TREE_VEC_LENGTH (field
);
17003 gcc_assert (TREE_CODE (init
) == TREE_VEC
17004 && TREE_VEC_LENGTH (init
) == len
);
17005 for (int i
= 0; i
< len
; ++i
)
17006 LAMBDA_EXPR_CAPTURE_LIST (r
)
17007 = tree_cons (TREE_VEC_ELT (field
, i
),
17008 TREE_VEC_ELT (init
, i
),
17009 LAMBDA_EXPR_CAPTURE_LIST (r
));
17013 LAMBDA_EXPR_CAPTURE_LIST (r
)
17014 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17016 if (id_equal (DECL_NAME (field
), "__this"))
17017 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17021 tree type
= begin_lambda_type (r
);
17023 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17024 determine_visibility (TYPE_NAME (type
));
17026 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17028 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17029 ? DECL_TI_TEMPLATE (oldfn
)
17032 tree fntype
= static_fn_type (oldfn
);
17034 ++processing_template_decl
;
17035 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17037 --processing_template_decl
;
17039 if (fntype
== error_mark_node
)
17040 r
= error_mark_node
;
17043 /* Fix the type of 'this'. */
17044 fntype
= build_memfn_type (fntype
, type
,
17045 type_memfn_quals (fntype
),
17046 type_memfn_rqual (fntype
));
17050 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17051 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17052 finish_member_declaration (tmpl
);
17057 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17058 finish_member_declaration (fn
);
17061 /* Let finish_function set this. */
17062 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17064 bool nested
= cfun
;
17066 push_function_context ();
17068 local_specialization_stack
s (lss_copy
);
17070 tree body
= start_lambda_function (fn
, r
);
17072 register_parameter_specializations (oldfn
, fn
);
17074 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17075 /*constexpr*/false);
17077 finish_lambda_function (body
);
17080 pop_function_context ();
17082 /* The capture list was built up in reverse order; fix that now. */
17083 LAMBDA_EXPR_CAPTURE_LIST (r
)
17084 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17086 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17088 maybe_add_lambda_conv_op (type
);
17091 finish_struct (type
, /*attr*/NULL_TREE
);
17093 insert_pending_capture_proxies ();
17098 /* Like tsubst but deals with expressions and performs semantic
17099 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17102 tsubst_copy_and_build (tree t
,
17104 tsubst_flags_t complain
,
17107 bool integral_constant_expression_p
)
17109 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17110 #define RECUR(NODE) \
17111 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17112 /*function_p=*/false, \
17113 integral_constant_expression_p)
17118 if (t
== NULL_TREE
|| t
== error_mark_node
)
17121 loc
= input_location
;
17122 if (EXPR_HAS_LOCATION (t
))
17123 input_location
= EXPR_LOCATION (t
);
17125 /* N3276 decltype magic only applies to calls at the top level or on the
17126 right side of a comma. */
17127 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17128 complain
&= ~tf_decltype
;
17130 switch (TREE_CODE (t
))
17134 /* Fall through. */
17135 case IDENTIFIER_NODE
:
17139 bool non_integral_constant_expression_p
;
17140 const char *error_msg
;
17142 if (IDENTIFIER_CONV_OP_P (t
))
17144 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17145 t
= make_conv_op_name (new_type
);
17148 /* Look up the name. */
17149 decl
= lookup_name (t
);
17151 /* By convention, expressions use ERROR_MARK_NODE to indicate
17152 failure, not NULL_TREE. */
17153 if (decl
== NULL_TREE
)
17154 decl
= error_mark_node
;
17156 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17158 integral_constant_expression_p
,
17159 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17160 &non_integral_constant_expression_p
,
17161 /*template_p=*/false,
17163 /*address_p=*/false,
17164 /*template_arg_p=*/false,
17169 if (!function_p
&& identifier_p (decl
))
17171 if (complain
& tf_error
)
17172 unqualified_name_lookup_error (decl
);
17173 decl
= error_mark_node
;
17178 case TEMPLATE_ID_EXPR
:
17181 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17182 tree targs
= TREE_OPERAND (t
, 1);
17185 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17186 if (targs
== error_mark_node
)
17187 return error_mark_node
;
17189 if (TREE_CODE (templ
) == SCOPE_REF
)
17191 tree name
= TREE_OPERAND (templ
, 1);
17192 tree tid
= lookup_template_function (name
, targs
);
17193 TREE_OPERAND (templ
, 1) = tid
;
17197 if (variable_template_p (templ
))
17198 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17200 if (TREE_CODE (templ
) == COMPONENT_REF
)
17202 object
= TREE_OPERAND (templ
, 0);
17203 templ
= TREE_OPERAND (templ
, 1);
17206 object
= NULL_TREE
;
17207 templ
= lookup_template_function (templ
, targs
);
17210 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17211 object
, templ
, NULL_TREE
));
17213 RETURN (baselink_for_fns (templ
));
17218 tree r
= RECUR (TREE_OPERAND (t
, 0));
17220 if (REFERENCE_REF_P (t
))
17222 /* A type conversion to reference type will be enclosed in
17223 such an indirect ref, but the substitution of the cast
17224 will have also added such an indirect ref. */
17225 r
= convert_from_reference (r
);
17228 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17229 complain
|decltype_flag
);
17231 if (REF_PARENTHESIZED_P (t
))
17232 r
= force_paren_expr (r
);
17239 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17240 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17241 RETURN (build_nop (type
, op0
));
17244 case IMPLICIT_CONV_EXPR
:
17246 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17247 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17248 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17250 retval
= copy_node (t
);
17251 TREE_TYPE (retval
) = type
;
17252 TREE_OPERAND (retval
, 0) = expr
;
17255 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17256 /* We'll pass this to convert_nontype_argument again, we don't need
17257 to actually perform any conversion here. */
17259 int flags
= LOOKUP_IMPLICIT
;
17260 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17261 flags
= LOOKUP_NORMAL
;
17262 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17268 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17269 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17270 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17274 case REINTERPRET_CAST_EXPR
:
17275 case CONST_CAST_EXPR
:
17276 case DYNAMIC_CAST_EXPR
:
17277 case STATIC_CAST_EXPR
:
17280 tree op
, r
= NULL_TREE
;
17282 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17283 if (integral_constant_expression_p
17284 && !cast_valid_in_integral_constant_expression_p (type
))
17286 if (complain
& tf_error
)
17287 error ("a cast to a type other than an integral or "
17288 "enumeration type cannot appear in a constant-expression");
17289 RETURN (error_mark_node
);
17292 op
= RECUR (TREE_OPERAND (t
, 0));
17294 warning_sentinel
s(warn_useless_cast
);
17295 warning_sentinel
s2(warn_ignored_qualifiers
);
17296 switch (TREE_CODE (t
))
17299 r
= build_functional_cast (type
, op
, complain
);
17301 case REINTERPRET_CAST_EXPR
:
17302 r
= build_reinterpret_cast (type
, op
, complain
);
17304 case CONST_CAST_EXPR
:
17305 r
= build_const_cast (type
, op
, complain
);
17307 case DYNAMIC_CAST_EXPR
:
17308 r
= build_dynamic_cast (type
, op
, complain
);
17310 case STATIC_CAST_EXPR
:
17311 r
= build_static_cast (type
, op
, complain
);
17314 gcc_unreachable ();
17320 case POSTDECREMENT_EXPR
:
17321 case POSTINCREMENT_EXPR
:
17322 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17323 args
, complain
, in_decl
);
17324 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17325 complain
|decltype_flag
));
17327 case PREDECREMENT_EXPR
:
17328 case PREINCREMENT_EXPR
:
17332 case TRUTH_NOT_EXPR
:
17333 case UNARY_PLUS_EXPR
: /* Unary + */
17334 case REALPART_EXPR
:
17335 case IMAGPART_EXPR
:
17336 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17337 RECUR (TREE_OPERAND (t
, 0)),
17338 complain
|decltype_flag
));
17340 case FIX_TRUNC_EXPR
:
17341 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17345 op1
= TREE_OPERAND (t
, 0);
17346 if (TREE_CODE (op1
) == LABEL_DECL
)
17347 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17348 EXPR_LOCATION (op1
)));
17349 if (TREE_CODE (op1
) == SCOPE_REF
)
17350 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17351 /*done=*/true, /*address_p=*/true);
17353 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17355 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17356 complain
|decltype_flag
));
17361 case TRUNC_DIV_EXPR
:
17362 case CEIL_DIV_EXPR
:
17363 case FLOOR_DIV_EXPR
:
17364 case ROUND_DIV_EXPR
:
17365 case EXACT_DIV_EXPR
:
17369 case TRUNC_MOD_EXPR
:
17370 case FLOOR_MOD_EXPR
:
17371 case TRUTH_ANDIF_EXPR
:
17372 case TRUTH_ORIF_EXPR
:
17373 case TRUTH_AND_EXPR
:
17374 case TRUTH_OR_EXPR
:
17390 warning_sentinel
s1(warn_type_limits
);
17391 warning_sentinel
s2(warn_div_by_zero
);
17392 warning_sentinel
s3(warn_logical_op
);
17393 warning_sentinel
s4(warn_tautological_compare
);
17394 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17395 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17396 tree r
= build_x_binary_op
17397 (input_location
, TREE_CODE (t
),
17399 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17401 : TREE_CODE (TREE_OPERAND (t
, 0))),
17403 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17405 : TREE_CODE (TREE_OPERAND (t
, 1))),
17407 complain
|decltype_flag
);
17408 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17409 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17414 case POINTER_PLUS_EXPR
:
17416 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17417 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17418 return fold_build_pointer_plus (op0
, op1
);
17422 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17423 /*address_p=*/false));
17425 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17426 args
, complain
, in_decl
);
17427 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17428 RECUR (TREE_OPERAND (t
, 1)),
17429 complain
|decltype_flag
));
17432 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17433 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17434 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17441 op1
= TREE_OPERAND (t
, 0);
17442 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17443 op1
= TREE_TYPE (op1
);
17446 /* When there are no ARGS, we are trying to evaluate a
17447 non-dependent expression from the parser. Trying to do
17448 the substitutions may not work. */
17450 op1
= TREE_TYPE (op1
);
17454 ++cp_unevaluated_operand
;
17455 ++c_inhibit_evaluation_warnings
;
17457 op1
= tsubst (op1
, args
, complain
, in_decl
);
17459 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17460 /*function_p=*/false,
17461 /*integral_constant_expression_p=*/
17463 --cp_unevaluated_operand
;
17464 --c_inhibit_evaluation_warnings
;
17467 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17468 complain
& tf_error
);
17470 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17471 complain
& tf_error
);
17472 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17474 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17476 if (!processing_template_decl
&& TYPE_P (op1
))
17478 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17479 build1 (NOP_EXPR
, op1
, error_mark_node
));
17480 SIZEOF_EXPR_TYPE_P (r
) = 1;
17483 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17484 TREE_SIDE_EFFECTS (r
) = 0;
17485 TREE_READONLY (r
) = 1;
17487 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17492 case AT_ENCODE_EXPR
:
17494 op1
= TREE_OPERAND (t
, 0);
17495 ++cp_unevaluated_operand
;
17496 ++c_inhibit_evaluation_warnings
;
17497 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17498 /*function_p=*/false,
17499 /*integral_constant_expression_p=*/false);
17500 --cp_unevaluated_operand
;
17501 --c_inhibit_evaluation_warnings
;
17502 RETURN (objc_build_encode_expr (op1
));
17505 case NOEXCEPT_EXPR
:
17506 op1
= TREE_OPERAND (t
, 0);
17507 ++cp_unevaluated_operand
;
17508 ++c_inhibit_evaluation_warnings
;
17509 ++cp_noexcept_operand
;
17510 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17511 /*function_p=*/false,
17512 /*integral_constant_expression_p=*/false);
17513 --cp_unevaluated_operand
;
17514 --c_inhibit_evaluation_warnings
;
17515 --cp_noexcept_operand
;
17516 RETURN (finish_noexcept_expr (op1
, complain
));
17520 warning_sentinel
s(warn_div_by_zero
);
17521 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17522 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17523 tree r
= build_x_modify_expr
17524 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17525 complain
|decltype_flag
);
17526 /* TREE_NO_WARNING must be set if either the expression was
17527 parenthesized or it uses an operator such as >>= rather
17528 than plain assignment. In the former case, it was already
17529 set and must be copied. In the latter case,
17530 build_x_modify_expr sets it and it must not be reset
17532 if (TREE_NO_WARNING (t
))
17533 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17539 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17540 args
, complain
, in_decl
);
17541 /* Remember that there was a reference to this entity. */
17543 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17544 RETURN (error_mark_node
);
17545 RETURN (build_x_arrow (input_location
, op1
, complain
));
17549 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17550 tree init
= RECUR (TREE_OPERAND (t
, 3));
17551 vec
<tree
, va_gc
> *placement_vec
;
17552 vec
<tree
, va_gc
> *init_vec
;
17555 if (placement
== NULL_TREE
)
17556 placement_vec
= NULL
;
17559 placement_vec
= make_tree_vector ();
17560 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17561 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17564 /* If there was an initializer in the original tree, but it
17565 instantiated to an empty list, then we should pass a
17566 non-NULL empty vector to tell build_new that it was an
17567 empty initializer() rather than no initializer. This can
17568 only happen when the initializer is a pack expansion whose
17569 parameter packs are of length zero. */
17570 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17574 init_vec
= make_tree_vector ();
17575 if (init
== void_node
)
17576 gcc_assert (init_vec
!= NULL
);
17579 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17580 vec_safe_push (init_vec
, TREE_VALUE (init
));
17584 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17585 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17586 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17587 NEW_EXPR_USE_GLOBAL (t
),
17590 if (placement_vec
!= NULL
)
17591 release_tree_vector (placement_vec
);
17592 if (init_vec
!= NULL
)
17593 release_tree_vector (init_vec
);
17600 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17601 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17602 RETURN (delete_sanity (op0
, op1
,
17603 DELETE_EXPR_USE_VEC (t
),
17604 DELETE_EXPR_USE_GLOBAL (t
),
17608 case COMPOUND_EXPR
:
17610 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17611 complain
& ~tf_decltype
, in_decl
,
17612 /*function_p=*/false,
17613 integral_constant_expression_p
);
17614 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17616 RECUR (TREE_OPERAND (t
, 1)),
17617 complain
|decltype_flag
));
17623 vec
<tree
, va_gc
> *call_args
;
17624 unsigned int nargs
, i
;
17629 function
= CALL_EXPR_FN (t
);
17630 /* Internal function with no arguments. */
17631 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17634 /* When we parsed the expression, we determined whether or
17635 not Koenig lookup should be performed. */
17636 koenig_p
= KOENIG_LOOKUP_P (t
);
17637 if (function
== NULL_TREE
)
17640 qualified_p
= false;
17642 else if (TREE_CODE (function
) == SCOPE_REF
)
17644 qualified_p
= true;
17645 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17647 /*address_p=*/false);
17649 else if (koenig_p
&& identifier_p (function
))
17651 /* Do nothing; calling tsubst_copy_and_build on an identifier
17652 would incorrectly perform unqualified lookup again.
17654 Note that we can also have an IDENTIFIER_NODE if the earlier
17655 unqualified lookup found a member function; in that case
17656 koenig_p will be false and we do want to do the lookup
17657 again to find the instantiated member function.
17659 FIXME but doing that causes c++/15272, so we need to stop
17660 using IDENTIFIER_NODE in that situation. */
17661 qualified_p
= false;
17665 if (TREE_CODE (function
) == COMPONENT_REF
)
17667 tree op
= TREE_OPERAND (function
, 1);
17669 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17670 || (BASELINK_P (op
)
17671 && BASELINK_QUALIFIED_P (op
)));
17674 qualified_p
= false;
17676 if (TREE_CODE (function
) == ADDR_EXPR
17677 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17678 /* Avoid error about taking the address of a constructor. */
17679 function
= TREE_OPERAND (function
, 0);
17681 function
= tsubst_copy_and_build (function
, args
, complain
,
17684 integral_constant_expression_p
);
17686 if (BASELINK_P (function
))
17687 qualified_p
= true;
17690 nargs
= call_expr_nargs (t
);
17691 call_args
= make_tree_vector ();
17692 for (i
= 0; i
< nargs
; ++i
)
17694 tree arg
= CALL_EXPR_ARG (t
, i
);
17696 if (!PACK_EXPANSION_P (arg
))
17697 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17700 /* Expand the pack expansion and push each entry onto
17702 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17703 if (TREE_CODE (arg
) == TREE_VEC
)
17705 unsigned int len
, j
;
17707 len
= TREE_VEC_LENGTH (arg
);
17708 for (j
= 0; j
< len
; ++j
)
17710 tree value
= TREE_VEC_ELT (arg
, j
);
17711 if (value
!= NULL_TREE
)
17712 value
= convert_from_reference (value
);
17713 vec_safe_push (call_args
, value
);
17718 /* A partial substitution. Add one entry. */
17719 vec_safe_push (call_args
, arg
);
17724 /* We do not perform argument-dependent lookup if normal
17725 lookup finds a non-function, in accordance with the
17726 expected resolution of DR 218. */
17728 && ((is_overloaded_fn (function
)
17729 /* If lookup found a member function, the Koenig lookup is
17730 not appropriate, even if an unqualified-name was used
17731 to denote the function. */
17732 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17733 || identifier_p (function
))
17734 /* Only do this when substitution turns a dependent call
17735 into a non-dependent call. */
17736 && type_dependent_expression_p_push (t
)
17737 && !any_type_dependent_arguments_p (call_args
))
17738 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17740 if (function
!= NULL_TREE
17741 && identifier_p (function
)
17742 && !any_type_dependent_arguments_p (call_args
))
17744 if (koenig_p
&& (complain
& tf_warning_or_error
))
17746 /* For backwards compatibility and good diagnostics, try
17747 the unqualified lookup again if we aren't in SFINAE
17749 tree unq
= (tsubst_copy_and_build
17750 (function
, args
, complain
, in_decl
, true,
17751 integral_constant_expression_p
));
17752 if (unq
== error_mark_node
)
17754 release_tree_vector (call_args
);
17755 RETURN (error_mark_node
);
17758 if (unq
!= function
)
17760 /* In a lambda fn, we have to be careful to not
17761 introduce new this captures. Legacy code can't
17762 be using lambdas anyway, so it's ok to be
17764 bool in_lambda
= (current_class_type
17765 && LAMBDA_TYPE_P (current_class_type
));
17766 char const *const msg
17767 = G_("%qD was not declared in this scope, "
17768 "and no declarations were found by "
17769 "argument-dependent lookup at the point "
17770 "of instantiation");
17774 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17777 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17783 if (INDIRECT_REF_P (fn
))
17784 fn
= TREE_OPERAND (fn
, 0);
17785 if (is_overloaded_fn (fn
))
17786 fn
= get_first_fn (fn
);
17789 /* Can't say anything more. */;
17790 else if (DECL_CLASS_SCOPE_P (fn
))
17792 location_t loc
= EXPR_LOC_OR_LOC (t
,
17795 "declarations in dependent base %qT are "
17796 "not found by unqualified lookup",
17797 DECL_CLASS_CONTEXT (fn
));
17798 if (current_class_ptr
)
17800 "use %<this->%D%> instead", function
);
17803 "use %<%T::%D%> instead",
17804 current_class_name
, function
);
17807 inform (DECL_SOURCE_LOCATION (fn
),
17808 "%qD declared here, later in the "
17809 "translation unit", fn
);
17812 release_tree_vector (call_args
);
17813 RETURN (error_mark_node
);
17820 if (identifier_p (function
))
17822 if (complain
& tf_error
)
17823 unqualified_name_lookup_error (function
);
17824 release_tree_vector (call_args
);
17825 RETURN (error_mark_node
);
17829 /* Remember that there was a reference to this entity. */
17830 if (function
!= NULL_TREE
17831 && DECL_P (function
)
17832 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17834 release_tree_vector (call_args
);
17835 RETURN (error_mark_node
);
17838 /* Put back tf_decltype for the actual call. */
17839 complain
|= decltype_flag
;
17841 if (function
== NULL_TREE
)
17842 switch (CALL_EXPR_IFN (t
))
17845 gcc_assert (nargs
== 1);
17846 if (vec_safe_length (call_args
) != 1)
17848 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17849 "wrong number of arguments to "
17850 "%<__builtin_launder%>");
17851 ret
= error_mark_node
;
17854 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17856 (*call_args
)[0], complain
);
17860 /* Unsupported internal function with arguments. */
17861 gcc_unreachable ();
17863 else if (TREE_CODE (function
) == OFFSET_REF
)
17864 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17866 else if (TREE_CODE (function
) == COMPONENT_REF
)
17868 tree instance
= TREE_OPERAND (function
, 0);
17869 tree fn
= TREE_OPERAND (function
, 1);
17871 if (processing_template_decl
17872 && (type_dependent_expression_p (instance
)
17873 || (!BASELINK_P (fn
)
17874 && TREE_CODE (fn
) != FIELD_DECL
)
17875 || type_dependent_expression_p (fn
)
17876 || any_type_dependent_arguments_p (call_args
)))
17877 ret
= build_min_nt_call_vec (function
, call_args
);
17878 else if (!BASELINK_P (fn
))
17879 ret
= finish_call_expr (function
, &call_args
,
17880 /*disallow_virtual=*/false,
17881 /*koenig_p=*/false,
17884 ret
= (build_new_method_call
17886 &call_args
, NULL_TREE
,
17887 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17892 ret
= finish_call_expr (function
, &call_args
,
17893 /*disallow_virtual=*/qualified_p
,
17897 release_tree_vector (call_args
);
17899 if (ret
!= error_mark_node
)
17901 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17902 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17903 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17904 bool thk
= CALL_FROM_THUNK_P (t
);
17905 if (op
|| ord
|| rev
|| thk
)
17907 function
= extract_call_expr (ret
);
17908 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17909 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17910 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17913 if (TREE_CODE (function
) == CALL_EXPR
)
17914 CALL_FROM_THUNK_P (function
) = true;
17916 AGGR_INIT_FROM_THUNK_P (function
) = true;
17917 /* The thunk location is not interesting. */
17918 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17928 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17929 tree folded_cond
= fold_non_dependent_expr (cond
);
17932 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17934 if (integer_zerop (folded_cond
))
17936 ++c_inhibit_evaluation_warnings
;
17937 exp1
= RECUR (TREE_OPERAND (t
, 1));
17938 --c_inhibit_evaluation_warnings
;
17939 exp2
= RECUR (TREE_OPERAND (t
, 2));
17943 exp1
= RECUR (TREE_OPERAND (t
, 1));
17944 ++c_inhibit_evaluation_warnings
;
17945 exp2
= RECUR (TREE_OPERAND (t
, 2));
17946 --c_inhibit_evaluation_warnings
;
17948 cond
= folded_cond
;
17952 exp1
= RECUR (TREE_OPERAND (t
, 1));
17953 exp2
= RECUR (TREE_OPERAND (t
, 2));
17956 warning_sentinel
s(warn_duplicated_branches
);
17957 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17958 cond
, exp1
, exp2
, complain
));
17961 case PSEUDO_DTOR_EXPR
:
17963 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17964 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17965 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17966 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17972 tree purpose
, value
, chain
;
17974 if (t
== void_list_node
)
17977 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17978 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17980 /* We have pack expansions, so expand those and
17981 create a new list out of it. */
17982 tree purposevec
= NULL_TREE
;
17983 tree valuevec
= NULL_TREE
;
17987 /* Expand the argument expressions. */
17988 if (TREE_PURPOSE (t
))
17989 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17990 complain
, in_decl
);
17991 if (TREE_VALUE (t
))
17992 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17993 complain
, in_decl
);
17995 /* Build the rest of the list. */
17996 chain
= TREE_CHAIN (t
);
17997 if (chain
&& chain
!= void_type_node
)
17998 chain
= RECUR (chain
);
18000 /* Determine the number of arguments. */
18001 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18003 len
= TREE_VEC_LENGTH (purposevec
);
18004 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18006 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18007 len
= TREE_VEC_LENGTH (valuevec
);
18010 /* Since we only performed a partial substitution into
18011 the argument pack, we only RETURN (a single list
18013 if (purposevec
== TREE_PURPOSE (t
)
18014 && valuevec
== TREE_VALUE (t
)
18015 && chain
== TREE_CHAIN (t
))
18018 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18021 /* Convert the argument vectors into a TREE_LIST */
18025 /* Grab the Ith values. */
18027 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18030 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18033 /* Build the list (backwards). */
18034 chain
= tree_cons (purpose
, value
, chain
);
18040 purpose
= TREE_PURPOSE (t
);
18042 purpose
= RECUR (purpose
);
18043 value
= TREE_VALUE (t
);
18045 value
= RECUR (value
);
18046 chain
= TREE_CHAIN (t
);
18047 if (chain
&& chain
!= void_type_node
)
18048 chain
= RECUR (chain
);
18049 if (purpose
== TREE_PURPOSE (t
)
18050 && value
== TREE_VALUE (t
)
18051 && chain
== TREE_CHAIN (t
))
18053 RETURN (tree_cons (purpose
, value
, chain
));
18056 case COMPONENT_REF
:
18063 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18064 args
, complain
, in_decl
);
18065 /* Remember that there was a reference to this entity. */
18066 if (DECL_P (object
)
18067 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18068 RETURN (error_mark_node
);
18069 object_type
= TREE_TYPE (object
);
18071 member
= TREE_OPERAND (t
, 1);
18072 if (BASELINK_P (member
))
18073 member
= tsubst_baselink (member
,
18074 non_reference (TREE_TYPE (object
)),
18075 args
, complain
, in_decl
);
18077 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18078 if (member
== error_mark_node
)
18079 RETURN (error_mark_node
);
18081 if (TREE_CODE (member
) == FIELD_DECL
)
18083 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18084 if (TREE_CODE (r
) == COMPONENT_REF
)
18085 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18088 else if (type_dependent_expression_p (object
))
18089 /* We can't do much here. */;
18090 else if (!CLASS_TYPE_P (object_type
))
18092 if (scalarish_type_p (object_type
))
18094 tree s
= NULL_TREE
;
18095 tree dtor
= member
;
18097 if (TREE_CODE (dtor
) == SCOPE_REF
)
18099 s
= TREE_OPERAND (dtor
, 0);
18100 dtor
= TREE_OPERAND (dtor
, 1);
18102 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18104 dtor
= TREE_OPERAND (dtor
, 0);
18106 RETURN (finish_pseudo_destructor_expr
18107 (object
, s
, dtor
, input_location
));
18111 else if (TREE_CODE (member
) == SCOPE_REF
18112 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18114 /* Lookup the template functions now that we know what the
18116 tree scope
= TREE_OPERAND (member
, 0);
18117 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18118 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18119 member
= lookup_qualified_name (scope
, tmpl
,
18120 /*is_type_p=*/false,
18121 /*complain=*/false);
18122 if (BASELINK_P (member
))
18124 BASELINK_FUNCTIONS (member
)
18125 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18127 member
= (adjust_result_of_qualified_name_lookup
18128 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18133 qualified_name_lookup_error (scope
, tmpl
, member
,
18135 RETURN (error_mark_node
);
18138 else if (TREE_CODE (member
) == SCOPE_REF
18139 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18140 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18142 if (complain
& tf_error
)
18144 if (TYPE_P (TREE_OPERAND (member
, 0)))
18145 error ("%qT is not a class or namespace",
18146 TREE_OPERAND (member
, 0));
18148 error ("%qD is not a class or namespace",
18149 TREE_OPERAND (member
, 0));
18151 RETURN (error_mark_node
);
18154 r
= finish_class_member_access_expr (object
, member
,
18155 /*template_p=*/false,
18157 if (TREE_CODE (r
) == COMPONENT_REF
)
18158 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18163 RETURN (build_throw
18164 (RECUR (TREE_OPERAND (t
, 0))));
18168 vec
<constructor_elt
, va_gc
> *n
;
18169 constructor_elt
*ce
;
18170 unsigned HOST_WIDE_INT idx
;
18171 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18172 bool process_index_p
;
18174 bool need_copy_p
= false;
18177 if (type
== error_mark_node
)
18178 RETURN (error_mark_node
);
18180 /* We do not want to process the index of aggregate
18181 initializers as they are identifier nodes which will be
18182 looked up by digest_init. */
18183 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18185 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18186 newlen
= vec_safe_length (n
);
18187 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18189 if (ce
->index
&& process_index_p
18190 /* An identifier index is looked up in the type
18191 being initialized, not the current scope. */
18192 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18193 ce
->index
= RECUR (ce
->index
);
18195 if (PACK_EXPANSION_P (ce
->value
))
18197 /* Substitute into the pack expansion. */
18198 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18201 if (ce
->value
== error_mark_node
18202 || PACK_EXPANSION_P (ce
->value
))
18204 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18205 /* Just move the argument into place. */
18206 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18209 /* Update the length of the final CONSTRUCTOR
18210 arguments vector, and note that we will need to
18212 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18213 need_copy_p
= true;
18217 ce
->value
= RECUR (ce
->value
);
18222 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18224 vec_alloc (n
, newlen
);
18225 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18227 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18229 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18230 for (i
= 0; i
< len
; ++i
)
18231 CONSTRUCTOR_APPEND_ELT (n
, 0,
18232 TREE_VEC_ELT (ce
->value
, i
));
18235 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18239 r
= build_constructor (init_list_type_node
, n
);
18240 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18242 if (TREE_HAS_CONSTRUCTOR (t
))
18244 fcl_t cl
= fcl_functional
;
18245 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18247 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18250 TREE_TYPE (r
) = type
;
18256 tree operand_0
= TREE_OPERAND (t
, 0);
18257 if (TYPE_P (operand_0
))
18259 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18260 RETURN (get_typeid (operand_0
, complain
));
18264 operand_0
= RECUR (operand_0
);
18265 RETURN (build_typeid (operand_0
, complain
));
18276 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18277 /* ??? We're doing a subset of finish_id_expression here. */
18279 && !processing_template_decl
18280 && !cp_unevaluated_operand
18281 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18282 && CP_DECL_THREAD_LOCAL_P (r
))
18284 if (tree wrap
= get_tls_wrapper_fn (r
))
18285 /* Replace an evaluated use of the thread_local variable with
18286 a call to its wrapper. */
18287 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18289 else if (outer_automatic_var_p (r
))
18290 r
= process_outer_var_ref (r
, complain
);
18292 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18293 /* If the original type was a reference, we'll be wrapped in
18294 the appropriate INDIRECT_REF. */
18295 r
= convert_from_reference (r
);
18301 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18302 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18303 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18306 case OFFSETOF_EXPR
:
18309 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18310 in_decl
, /*function_p=*/false,
18311 /*integral_constant_expression_p=*/false);
18312 RETURN (finish_offsetof (object_ptr
,
18313 RECUR (TREE_OPERAND (t
, 0)),
18314 EXPR_LOCATION (t
)));
18317 case ADDRESSOF_EXPR
:
18318 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18319 RECUR (TREE_OPERAND (t
, 0)), complain
));
18323 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18324 complain
, in_decl
);
18326 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18327 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18328 type2
= RECUR (type2
);
18330 type2
= tsubst (type2
, args
, complain
, in_decl
);
18332 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18337 tree old_stmt_expr
= cur_stmt_expr
;
18338 tree stmt_expr
= begin_stmt_expr ();
18340 cur_stmt_expr
= stmt_expr
;
18341 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18342 integral_constant_expression_p
);
18343 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18344 cur_stmt_expr
= old_stmt_expr
;
18346 /* If the resulting list of expression statement is empty,
18347 fold it further into void_node. */
18348 if (empty_expr_stmt_p (stmt_expr
))
18349 stmt_expr
= void_node
;
18351 RETURN (stmt_expr
);
18356 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18358 RETURN (build_lambda_object (r
));
18362 /* We can get here for a constant initializer of non-dependent type.
18363 FIXME stop folding in cp_parser_initializer_clause. */
18365 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18370 case TRANSACTION_EXPR
:
18371 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18372 integral_constant_expression_p
));
18375 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18377 case VEC_PERM_EXPR
:
18379 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18380 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18381 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18382 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18386 case REQUIRES_EXPR
:
18387 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18389 case NON_LVALUE_EXPR
:
18390 case VIEW_CONVERT_EXPR
:
18391 /* We should only see these for location wrapper nodes, or within
18392 instantiate_non_dependent_expr (when args is NULL_TREE). */
18393 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18394 if (location_wrapper_p (t
))
18395 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18396 EXPR_LOCATION (t
)));
18400 /* Handle Objective-C++ constructs, if appropriate. */
18403 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18404 in_decl
, /*function_p=*/false);
18408 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18414 input_location
= loc
;
18418 /* Verify that the instantiated ARGS are valid. For type arguments,
18419 make sure that the type's linkage is ok. For non-type arguments,
18420 make sure they are constants if they are integral or enumerations.
18421 Emit an error under control of COMPLAIN, and return TRUE on error. */
18424 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18426 if (dependent_template_arg_p (t
))
18428 if (ARGUMENT_PACK_P (t
))
18430 tree vec
= ARGUMENT_PACK_ARGS (t
);
18431 int len
= TREE_VEC_LENGTH (vec
);
18432 bool result
= false;
18435 for (i
= 0; i
< len
; ++i
)
18436 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18440 else if (TYPE_P (t
))
18442 /* [basic.link]: A name with no linkage (notably, the name
18443 of a class or enumeration declared in a local scope)
18444 shall not be used to declare an entity with linkage.
18445 This implies that names with no linkage cannot be used as
18448 DR 757 relaxes this restriction for C++0x. */
18449 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18450 : no_linkage_check (t
, /*relaxed_p=*/false));
18454 /* DR 488 makes use of a type with no linkage cause
18455 type deduction to fail. */
18456 if (complain
& tf_error
)
18458 if (TYPE_UNNAMED_P (nt
))
18459 error ("%qT is/uses unnamed type", t
);
18461 error ("template argument for %qD uses local type %qT",
18466 /* In order to avoid all sorts of complications, we do not
18467 allow variably-modified types as template arguments. */
18468 else if (variably_modified_type_p (t
, NULL_TREE
))
18470 if (complain
& tf_error
)
18471 error ("%qT is a variably modified type", t
);
18475 /* Class template and alias template arguments should be OK. */
18476 else if (DECL_TYPE_TEMPLATE_P (t
))
18478 /* A non-type argument of integral or enumerated type must be a
18480 else if (TREE_TYPE (t
)
18481 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18482 && !REFERENCE_REF_P (t
)
18483 && !TREE_CONSTANT (t
))
18485 if (complain
& tf_error
)
18486 error ("integral expression %qE is not constant", t
);
18493 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18495 int ix
, len
= DECL_NTPARMS (tmpl
);
18496 bool result
= false;
18498 for (ix
= 0; ix
!= len
; ix
++)
18500 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18503 if (result
&& (complain
& tf_error
))
18504 error (" trying to instantiate %qD", tmpl
);
18508 /* We're out of SFINAE context now, so generate diagnostics for the access
18509 errors we saw earlier when instantiating D from TMPL and ARGS. */
18512 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18514 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18515 tree type
= TREE_TYPE (pattern
);
18516 location_t loc
= input_location
;
18518 push_access_scope (d
);
18519 push_deferring_access_checks (dk_no_deferred
);
18520 input_location
= DECL_SOURCE_LOCATION (pattern
);
18521 tsubst (type
, args
, tf_warning_or_error
, d
);
18522 input_location
= loc
;
18523 pop_deferring_access_checks ();
18524 pop_access_scope (d
);
18527 /* Instantiate the indicated variable, function, or alias template TMPL with
18528 the template arguments in TARG_PTR. */
18531 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18533 tree targ_ptr
= orig_args
;
18537 bool access_ok
= true;
18539 if (tmpl
== error_mark_node
)
18540 return error_mark_node
;
18542 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18544 /* If this function is a clone, handle it specially. */
18545 if (DECL_CLONED_FUNCTION_P (tmpl
))
18550 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18551 DECL_CLONED_FUNCTION. */
18552 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18553 targ_ptr
, complain
);
18554 if (spec
== error_mark_node
)
18555 return error_mark_node
;
18557 /* Look for the clone. */
18558 FOR_EACH_CLONE (clone
, spec
)
18559 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18561 /* We should always have found the clone by now. */
18562 gcc_unreachable ();
18566 if (targ_ptr
== error_mark_node
)
18567 return error_mark_node
;
18569 /* Check to see if we already have this specialization. */
18570 gen_tmpl
= most_general_template (tmpl
);
18571 if (TMPL_ARGS_DEPTH (targ_ptr
)
18572 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18573 /* targ_ptr only has the innermost template args, so add the outer ones
18574 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18575 the case of a non-dependent call within a template definition). */
18576 targ_ptr
= (add_outermost_template_args
18577 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18580 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18581 but it doesn't seem to be on the hot path. */
18582 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18584 gcc_assert (tmpl
== gen_tmpl
18585 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18587 || fndecl
== NULL_TREE
);
18589 if (spec
!= NULL_TREE
)
18591 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18593 if (complain
& tf_error
)
18594 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18595 return error_mark_node
;
18600 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18602 return error_mark_node
;
18604 /* We are building a FUNCTION_DECL, during which the access of its
18605 parameters and return types have to be checked. However this
18606 FUNCTION_DECL which is the desired context for access checking
18607 is not built yet. We solve this chicken-and-egg problem by
18608 deferring all checks until we have the FUNCTION_DECL. */
18609 push_deferring_access_checks (dk_deferred
);
18611 /* Instantiation of the function happens in the context of the function
18612 template, not the context of the overload resolution we're doing. */
18613 push_to_top_level ();
18614 /* If there are dependent arguments, e.g. because we're doing partial
18615 ordering, make sure processing_template_decl stays set. */
18616 if (uses_template_parms (targ_ptr
))
18617 ++processing_template_decl
;
18618 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18620 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18621 complain
, gen_tmpl
, true);
18622 push_nested_class (ctx
);
18625 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18627 fndecl
= NULL_TREE
;
18628 if (VAR_P (pattern
))
18630 /* We need to determine if we're using a partial or explicit
18631 specialization now, because the type of the variable could be
18633 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18634 tree elt
= most_specialized_partial_spec (tid
, complain
);
18635 if (elt
== error_mark_node
)
18636 pattern
= error_mark_node
;
18639 tree partial_tmpl
= TREE_VALUE (elt
);
18640 tree partial_args
= TREE_PURPOSE (elt
);
18641 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18642 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18646 /* Substitute template parameters to obtain the specialization. */
18647 if (fndecl
== NULL_TREE
)
18648 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18649 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18650 pop_nested_class ();
18651 pop_from_top_level ();
18653 if (fndecl
== error_mark_node
)
18655 pop_deferring_access_checks ();
18656 return error_mark_node
;
18659 /* The DECL_TI_TEMPLATE should always be the immediate parent
18660 template, not the most general template. */
18661 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18662 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18664 /* Now we know the specialization, compute access previously
18665 deferred. Do no access control for inheriting constructors,
18666 as we already checked access for the inherited constructor. */
18667 if (!(flag_new_inheriting_ctors
18668 && DECL_INHERITED_CTOR (fndecl
)))
18670 push_access_scope (fndecl
);
18671 if (!perform_deferred_access_checks (complain
))
18673 pop_access_scope (fndecl
);
18675 pop_deferring_access_checks ();
18677 /* If we've just instantiated the main entry point for a function,
18678 instantiate all the alternate entry points as well. We do this
18679 by cloning the instantiation of the main entry point, not by
18680 instantiating the template clones. */
18681 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18682 clone_function_decl (fndecl
, /*update_methods=*/false);
18686 if (!(complain
& tf_error
))
18688 /* Remember to reinstantiate when we're out of SFINAE so the user
18689 can see the errors. */
18690 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18692 return error_mark_node
;
18697 /* Wrapper for instantiate_template_1. */
18700 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18703 timevar_push (TV_TEMPLATE_INST
);
18704 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18705 timevar_pop (TV_TEMPLATE_INST
);
18709 /* Instantiate the alias template TMPL with ARGS. Also push a template
18710 instantiation level, which instantiate_template doesn't do because
18711 functions and variables have sufficient context established by the
18715 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18717 struct pending_template
*old_last_pend
= last_pending_template
;
18718 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18719 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18720 return error_mark_node
;
18721 tree tinst
= build_tree_list (tmpl
, args
);
18722 if (!push_tinst_level (tinst
))
18725 return error_mark_node
;
18729 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18730 args
, tmpl
, complain
,
18731 /*require_all_args=*/true,
18732 /*use_default_args=*/true);
18734 tree r
= instantiate_template (tmpl
, args
, complain
);
18735 pop_tinst_level ();
18736 /* We can't free this if a pending_template entry or last_error_tinst_level
18737 is pointing at it. */
18738 if (last_pending_template
== old_last_pend
18739 && last_error_tinst_level
== old_error_tinst
)
18745 /* PARM is a template parameter pack for FN. Returns true iff
18746 PARM is used in a deducible way in the argument list of FN. */
18749 pack_deducible_p (tree parm
, tree fn
)
18751 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18752 for (; t
; t
= TREE_CHAIN (t
))
18754 tree type
= TREE_VALUE (t
);
18756 if (!PACK_EXPANSION_P (type
))
18758 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18759 packs
; packs
= TREE_CHAIN (packs
))
18760 if (template_args_equal (TREE_VALUE (packs
), parm
))
18762 /* The template parameter pack is used in a function parameter
18763 pack. If this is the end of the parameter list, the
18764 template parameter pack is deducible. */
18765 if (TREE_CHAIN (t
) == void_list_node
)
18768 /* Otherwise, not. Well, it could be deduced from
18769 a non-pack parameter, but doing so would end up with
18770 a deduction mismatch, so don't bother. */
18774 /* The template parameter pack isn't used in any function parameter
18775 packs, but it might be used deeper, e.g. tuple<Args...>. */
18779 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18780 NARGS elements of the arguments that are being used when calling
18781 it. TARGS is a vector into which the deduced template arguments
18784 Returns either a FUNCTION_DECL for the matching specialization of FN or
18785 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18786 true, diagnostics will be printed to explain why it failed.
18788 If FN is a conversion operator, or we are trying to produce a specific
18789 specialization, RETURN_TYPE is the return type desired.
18791 The EXPLICIT_TARGS are explicit template arguments provided via a
18794 The parameter STRICT is one of:
18797 We are deducing arguments for a function call, as in
18798 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18799 deducing arguments for a call to the result of a conversion
18800 function template, as in [over.call.object].
18803 We are deducing arguments for a conversion function, as in
18804 [temp.deduct.conv].
18807 We are deducing arguments when doing an explicit instantiation
18808 as in [temp.explicit], when determining an explicit specialization
18809 as in [temp.expl.spec], or when taking the address of a function
18810 template, as in [temp.deduct.funcaddr]. */
18813 fn_type_unification (tree fn
,
18814 tree explicit_targs
,
18817 unsigned int nargs
,
18819 unification_kind_t strict
,
18826 tree decl
= NULL_TREE
;
18827 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18829 static int deduction_depth
;
18830 struct pending_template
*old_last_pend
= last_pending_template
;
18831 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18834 if (flag_new_inheriting_ctors
)
18835 fn
= strip_inheriting_ctors (fn
);
18837 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18839 tree r
= error_mark_node
;
18841 tree full_targs
= targs
;
18842 if (TMPL_ARGS_DEPTH (targs
)
18843 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18844 full_targs
= (add_outermost_template_args
18845 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18849 complain
|= tf_decltype
;
18851 /* In C++0x, it's possible to have a function template whose type depends
18852 on itself recursively. This is most obvious with decltype, but can also
18853 occur with enumeration scope (c++/48969). So we need to catch infinite
18854 recursion and reject the substitution at deduction time; this function
18855 will return error_mark_node for any repeated substitution.
18857 This also catches excessive recursion such as when f<N> depends on
18858 f<N-1> across all integers, and returns error_mark_node for all the
18859 substitutions back up to the initial one.
18861 This is, of course, not reentrant. */
18862 if (excessive_deduction_depth
)
18863 return error_mark_node
;
18864 tinst
= build_tree_list (fn
, NULL_TREE
);
18867 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18869 fntype
= TREE_TYPE (fn
);
18870 if (explicit_targs
)
18874 The specified template arguments must match the template
18875 parameters in kind (i.e., type, nontype, template), and there
18876 must not be more arguments than there are parameters;
18877 otherwise type deduction fails.
18879 Nontype arguments must match the types of the corresponding
18880 nontype template parameters, or must be convertible to the
18881 types of the corresponding nontype parameters as specified in
18882 _temp.arg.nontype_, otherwise type deduction fails.
18884 All references in the function type of the function template
18885 to the corresponding template parameters are replaced by the
18886 specified template argument values. If a substitution in a
18887 template parameter or in the function type of the function
18888 template results in an invalid type, type deduction fails. */
18889 int i
, len
= TREE_VEC_LENGTH (tparms
);
18890 location_t loc
= input_location
;
18891 bool incomplete
= false;
18893 if (explicit_targs
== error_mark_node
)
18896 if (TMPL_ARGS_DEPTH (explicit_targs
)
18897 < TMPL_ARGS_DEPTH (full_targs
))
18898 explicit_targs
= add_outermost_template_args (full_targs
,
18901 /* Adjust any explicit template arguments before entering the
18902 substitution context. */
18904 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18906 /*require_all_args=*/false,
18907 /*use_default_args=*/false));
18908 if (explicit_targs
== error_mark_node
)
18911 /* Substitute the explicit args into the function type. This is
18912 necessary so that, for instance, explicitly declared function
18913 arguments can match null pointed constants. If we were given
18914 an incomplete set of explicit args, we must not do semantic
18915 processing during substitution as we could create partial
18917 for (i
= 0; i
< len
; i
++)
18919 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18920 bool parameter_pack
= false;
18921 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18923 /* Dig out the actual parm. */
18924 if (TREE_CODE (parm
) == TYPE_DECL
18925 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18927 parm
= TREE_TYPE (parm
);
18928 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18930 else if (TREE_CODE (parm
) == PARM_DECL
)
18932 parm
= DECL_INITIAL (parm
);
18933 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18936 if (!parameter_pack
&& targ
== NULL_TREE
)
18937 /* No explicit argument for this template parameter. */
18940 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18942 /* Mark the argument pack as "incomplete". We could
18943 still deduce more arguments during unification.
18944 We remove this mark in type_unification_real. */
18947 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18948 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18949 = ARGUMENT_PACK_ARGS (targ
);
18952 /* We have some incomplete argument packs. */
18957 TREE_VALUE (tinst
) = explicit_targs
;
18958 if (!push_tinst_level (tinst
))
18960 excessive_deduction_depth
= true;
18963 processing_template_decl
+= incomplete
;
18964 input_location
= DECL_SOURCE_LOCATION (fn
);
18965 /* Ignore any access checks; we'll see them again in
18966 instantiate_template and they might have the wrong
18967 access path at this point. */
18968 push_deferring_access_checks (dk_deferred
);
18969 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18970 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18971 pop_deferring_access_checks ();
18972 input_location
= loc
;
18973 processing_template_decl
-= incomplete
;
18974 pop_tinst_level ();
18976 if (fntype
== error_mark_node
)
18979 /* Place the explicitly specified arguments in TARGS. */
18980 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18981 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18982 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18985 /* Never do unification on the 'this' parameter. */
18986 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18988 if (return_type
&& strict
== DEDUCE_CALL
)
18990 /* We're deducing for a call to the result of a template conversion
18991 function. The parms we really want are in return_type. */
18992 if (POINTER_TYPE_P (return_type
))
18993 return_type
= TREE_TYPE (return_type
);
18994 parms
= TYPE_ARG_TYPES (return_type
);
18996 else if (return_type
)
19000 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19001 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19002 new_args
[0] = return_type
;
19003 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19008 /* We allow incomplete unification without an error message here
19009 because the standard doesn't seem to explicitly prohibit it. Our
19010 callers must be ready to deal with unification failures in any
19013 TREE_VALUE (tinst
) = targs
;
19014 /* If we aren't explaining yet, push tinst context so we can see where
19015 any errors (e.g. from class instantiations triggered by instantiation
19016 of default template arguments) come from. If we are explaining, this
19017 context is redundant. */
19018 if (!explain_p
&& !push_tinst_level (tinst
))
19020 excessive_deduction_depth
= true;
19024 /* type_unification_real will pass back any access checks from default
19025 template argument substitution. */
19026 vec
<deferred_access_check
, va_gc
> *checks
;
19029 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19030 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19031 strict
, flags
, &checks
, explain_p
);
19033 pop_tinst_level ();
19037 /* Now that we have bindings for all of the template arguments,
19038 ensure that the arguments deduced for the template template
19039 parameters have compatible template parameter lists. We cannot
19040 check this property before we have deduced all template
19041 arguments, because the template parameter types of a template
19042 template parameter might depend on prior template parameters
19043 deduced after the template template parameter. The following
19044 ill-formed example illustrates this issue:
19046 template<typename T, template<T> class C> void f(C<5>, T);
19048 template<int N> struct X {};
19051 f(X<5>(), 5l); // error: template argument deduction fails
19054 The template parameter list of 'C' depends on the template type
19055 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19056 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19057 time that we deduce 'C'. */
19058 if (!template_template_parm_bindings_ok_p
19059 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19061 unify_inconsistent_template_template_parameters (explain_p
);
19065 /* All is well so far. Now, check:
19069 When all template arguments have been deduced, all uses of
19070 template parameters in nondeduced contexts are replaced with
19071 the corresponding deduced argument values. If the
19072 substitution results in an invalid type, as described above,
19073 type deduction fails. */
19074 TREE_VALUE (tinst
) = targs
;
19075 if (!push_tinst_level (tinst
))
19077 excessive_deduction_depth
= true;
19081 /* Also collect access checks from the instantiation. */
19082 reopen_deferring_access_checks (checks
);
19084 decl
= instantiate_template (fn
, targs
, complain
);
19086 checks
= get_deferred_access_checks ();
19087 pop_deferring_access_checks ();
19089 pop_tinst_level ();
19091 if (decl
== error_mark_node
)
19094 /* Now perform any access checks encountered during substitution. */
19095 push_access_scope (decl
);
19096 ok
= perform_access_checks (checks
, complain
);
19097 pop_access_scope (decl
);
19101 /* If we're looking for an exact match, check that what we got
19102 is indeed an exact match. It might not be if some template
19103 parameters are used in non-deduced contexts. But don't check
19104 for an exact match if we have dependent template arguments;
19105 in that case we're doing partial ordering, and we already know
19106 that we have two candidates that will provide the actual type. */
19107 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19109 tree substed
= TREE_TYPE (decl
);
19113 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19115 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19116 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19117 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19119 unify_type_mismatch (explain_p
, args
[i
],
19120 TREE_VALUE (sarg
));
19125 /* After doing deduction with the inherited constructor, actually return an
19126 instantiation of the inheriting constructor. */
19128 decl
= instantiate_template (orig_fn
, targs
, complain
);
19134 if (excessive_deduction_depth
)
19136 if (deduction_depth
== 0)
19137 /* Reset once we're all the way out. */
19138 excessive_deduction_depth
= false;
19141 /* We can't free this if a pending_template entry or last_error_tinst_level
19142 is pointing at it. */
19143 if (last_pending_template
== old_last_pend
19144 && last_error_tinst_level
== old_error_tinst
)
19150 /* Adjust types before performing type deduction, as described in
19151 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19152 sections are symmetric. PARM is the type of a function parameter
19153 or the return type of the conversion function. ARG is the type of
19154 the argument passed to the call, or the type of the value
19155 initialized with the result of the conversion function.
19156 ARG_EXPR is the original argument expression, which may be null. */
19159 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19172 /* Swap PARM and ARG throughout the remainder of this
19173 function; the handling is precisely symmetric since PARM
19174 will initialize ARG rather than vice versa. */
19175 std::swap (parm
, arg
);
19179 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19180 too, but here handle it by stripping the reference from PARM
19181 rather than by adding it to ARG. */
19182 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19183 && TYPE_REF_IS_RVALUE (*parm
)
19184 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19185 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19186 && TREE_CODE (*arg
) == REFERENCE_TYPE
19187 && !TYPE_REF_IS_RVALUE (*arg
))
19188 *parm
= TREE_TYPE (*parm
);
19189 /* Nothing else to do in this case. */
19193 gcc_unreachable ();
19196 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19198 /* [temp.deduct.call]
19200 If P is not a reference type:
19202 --If A is an array type, the pointer type produced by the
19203 array-to-pointer standard conversion (_conv.array_) is
19204 used in place of A for type deduction; otherwise,
19206 --If A is a function type, the pointer type produced by
19207 the function-to-pointer standard conversion
19208 (_conv.func_) is used in place of A for type deduction;
19211 --If A is a cv-qualified type, the top level
19212 cv-qualifiers of A's type are ignored for type
19214 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19215 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19216 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19217 *arg
= build_pointer_type (*arg
);
19219 *arg
= TYPE_MAIN_VARIANT (*arg
);
19222 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19223 reference to a cv-unqualified template parameter that does not represent a
19224 template parameter of a class template (during class template argument
19225 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19226 an lvalue, the type "lvalue reference to A" is used in place of A for type
19228 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19229 && TYPE_REF_IS_RVALUE (*parm
)
19230 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19231 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19232 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19233 && (arg_expr
? lvalue_p (arg_expr
)
19234 /* try_one_overload doesn't provide an arg_expr, but
19235 functions are always lvalues. */
19236 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19237 *arg
= build_reference_type (*arg
);
19239 /* [temp.deduct.call]
19241 If P is a cv-qualified type, the top level cv-qualifiers
19242 of P's type are ignored for type deduction. If P is a
19243 reference type, the type referred to by P is used for
19245 *parm
= TYPE_MAIN_VARIANT (*parm
);
19246 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19248 *parm
= TREE_TYPE (*parm
);
19249 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19252 /* DR 322. For conversion deduction, remove a reference type on parm
19253 too (which has been swapped into ARG). */
19254 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19255 *arg
= TREE_TYPE (*arg
);
19260 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19261 template which does contain any deducible template parameters; check if
19262 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19263 unify_one_argument. */
19266 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19267 int flags
, bool explain_p
)
19272 type
= TREE_TYPE (arg
);
19276 if (same_type_p (parm
, type
))
19277 return unify_success (explain_p
);
19279 if (strict
== DEDUCE_CONV
)
19281 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19282 explain_p
? tf_warning_or_error
: tf_none
))
19283 return unify_success (explain_p
);
19285 else if (strict
!= DEDUCE_EXACT
)
19287 if (can_convert_arg (parm
, type
,
19288 TYPE_P (arg
) ? NULL_TREE
: arg
,
19289 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19290 return unify_success (explain_p
);
19293 if (strict
== DEDUCE_EXACT
)
19294 return unify_type_mismatch (explain_p
, parm
, arg
);
19296 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19299 static bool uses_deducible_template_parms (tree type
);
19301 /* Returns true iff the expression EXPR is one from which a template
19302 argument can be deduced. In other words, if it's an undecorated
19303 use of a template non-type parameter. */
19306 deducible_expression (tree expr
)
19308 /* Strip implicit conversions. */
19309 while (CONVERT_EXPR_P (expr
))
19310 expr
= TREE_OPERAND (expr
, 0);
19311 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19314 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19315 deducible way; that is, if it has a max value of <PARM> - 1. */
19318 deducible_array_bound (tree domain
)
19320 if (domain
== NULL_TREE
)
19323 tree max
= TYPE_MAX_VALUE (domain
);
19324 if (TREE_CODE (max
) != MINUS_EXPR
)
19327 return deducible_expression (TREE_OPERAND (max
, 0));
19330 /* Returns true iff the template arguments ARGS use a template parameter
19331 in a deducible way. */
19334 deducible_template_args (tree args
)
19336 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19339 tree elt
= TREE_VEC_ELT (args
, i
);
19340 if (ARGUMENT_PACK_P (elt
))
19341 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19344 if (PACK_EXPANSION_P (elt
))
19345 elt
= PACK_EXPANSION_PATTERN (elt
);
19346 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19348 else if (TYPE_P (elt
))
19349 deducible
= uses_deducible_template_parms (elt
);
19351 deducible
= deducible_expression (elt
);
19359 /* Returns true iff TYPE contains any deducible references to template
19360 parameters, as per 14.8.2.5. */
19363 uses_deducible_template_parms (tree type
)
19365 if (PACK_EXPANSION_P (type
))
19366 type
= PACK_EXPANSION_PATTERN (type
);
19373 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19374 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19380 if (POINTER_TYPE_P (type
))
19381 return uses_deducible_template_parms (TREE_TYPE (type
));
19383 /* T[integer-constant ]
19385 if (TREE_CODE (type
) == ARRAY_TYPE
)
19386 return (uses_deducible_template_parms (TREE_TYPE (type
))
19387 || deducible_array_bound (TYPE_DOMAIN (type
)));
19399 if (TYPE_PTRMEM_P (type
))
19400 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19401 || (uses_deducible_template_parms
19402 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19404 /* template-name <T> (where template-name refers to a class template)
19405 template-name <i> (where template-name refers to a class template) */
19406 if (CLASS_TYPE_P (type
)
19407 && CLASSTYPE_TEMPLATE_INFO (type
)
19408 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19409 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19410 (CLASSTYPE_TI_ARGS (type
)));
19415 if (TREE_CODE (type
) == FUNCTION_TYPE
19416 || TREE_CODE (type
) == METHOD_TYPE
)
19418 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19420 tree parm
= TYPE_ARG_TYPES (type
);
19421 if (TREE_CODE (type
) == METHOD_TYPE
)
19422 parm
= TREE_CHAIN (parm
);
19423 for (; parm
; parm
= TREE_CHAIN (parm
))
19424 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19431 /* Subroutine of type_unification_real and unify_pack_expansion to
19432 handle unification of a single P/A pair. Parameters are as
19433 for those functions. */
19436 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19437 int subr
, unification_kind_t strict
,
19440 tree arg_expr
= NULL_TREE
;
19443 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19444 return unify_invalid (explain_p
);
19445 if (arg
== unknown_type_node
)
19446 /* We can't deduce anything from this, but we might get all the
19447 template args from other function args. */
19448 return unify_success (explain_p
);
19450 /* Implicit conversions (Clause 4) will be performed on a function
19451 argument to convert it to the type of the corresponding function
19452 parameter if the parameter type contains no template-parameters that
19453 participate in template argument deduction. */
19454 if (strict
!= DEDUCE_EXACT
19455 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19456 /* For function parameters with no deducible template parameters,
19457 just return. We'll check non-dependent conversions later. */
19458 return unify_success (explain_p
);
19463 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19464 | UNIFY_ALLOW_MORE_CV_QUAL
19465 | UNIFY_ALLOW_DERIVED
);
19469 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19473 arg_strict
= UNIFY_ALLOW_NONE
;
19477 gcc_unreachable ();
19480 /* We only do these transformations if this is the top-level
19481 parameter_type_list in a call or declaration matching; in other
19482 situations (nested function declarators, template argument lists) we
19483 won't be comparing a type to an expression, and we don't do any type
19489 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19490 if (type_unknown_p (arg
))
19492 /* [temp.deduct.type] A template-argument can be
19493 deduced from a pointer to function or pointer
19494 to member function argument if the set of
19495 overloaded functions does not contain function
19496 templates and at most one of a set of
19497 overloaded functions provides a unique
19499 resolve_overloaded_unification (tparms
, targs
, parm
,
19501 arg_strict
, explain_p
);
19502 /* If a unique match was not found, this is a
19503 non-deduced context, so we still succeed. */
19504 return unify_success (explain_p
);
19508 arg
= unlowered_expr_type (arg
);
19509 if (arg
== error_mark_node
)
19510 return unify_invalid (explain_p
);
19514 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19517 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19518 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19519 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19521 /* For deduction from an init-list we need the actual list. */
19522 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19524 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19527 /* for_each_template_parm callback that always returns 0. */
19530 zero_r (tree
, void *)
19535 /* for_each_template_parm any_fn callback to handle deduction of a template
19536 type argument from the type of an array bound. */
19539 array_deduction_r (tree t
, void *data
)
19541 tree_pair_p d
= (tree_pair_p
)data
;
19542 tree
&tparms
= d
->purpose
;
19543 tree
&targs
= d
->value
;
19545 if (TREE_CODE (t
) == ARRAY_TYPE
)
19546 if (tree dom
= TYPE_DOMAIN (t
))
19547 if (tree max
= TYPE_MAX_VALUE (dom
))
19549 if (TREE_CODE (max
) == MINUS_EXPR
)
19550 max
= TREE_OPERAND (max
, 0);
19551 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19552 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19553 UNIFY_ALLOW_NONE
, /*explain*/false);
19556 /* Keep walking. */
19560 /* Try to deduce any not-yet-deduced template type arguments from the type of
19561 an array bound. This is handled separately from unify because 14.8.2.5 says
19562 "The type of a type parameter is only deduced from an array bound if it is
19563 not otherwise deduced." */
19566 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19568 tree_pair_s data
= { tparms
, targs
};
19569 hash_set
<tree
> visited
;
19570 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19571 /*nondeduced*/false, array_deduction_r
);
19574 /* Most parms like fn_type_unification.
19576 If SUBR is 1, we're being called recursively (to unify the
19577 arguments of a function or method parameter of a function
19580 CHECKS is a pointer to a vector of access checks encountered while
19581 substituting default template arguments. */
19584 type_unification_real (tree tparms
,
19588 unsigned int xnargs
,
19590 unification_kind_t strict
,
19592 vec
<deferred_access_check
, va_gc
> **checks
,
19597 int ntparms
= TREE_VEC_LENGTH (tparms
);
19598 int saw_undeduced
= 0;
19601 unsigned int nargs
;
19604 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19605 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19606 gcc_assert (ntparms
> 0);
19608 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19610 /* Reset the number of non-defaulted template arguments contained
19612 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19620 while (parms
&& parms
!= void_list_node
19623 parm
= TREE_VALUE (parms
);
19625 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19626 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19627 /* For a function parameter pack that occurs at the end of the
19628 parameter-declaration-list, the type A of each remaining
19629 argument of the call is compared with the type P of the
19630 declarator-id of the function parameter pack. */
19633 parms
= TREE_CHAIN (parms
);
19635 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19636 /* For a function parameter pack that does not occur at the
19637 end of the parameter-declaration-list, the type of the
19638 parameter pack is a non-deduced context. */
19644 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19650 && parms
!= void_list_node
19651 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19653 /* Unify the remaining arguments with the pack expansion type. */
19655 tree parmvec
= make_tree_vec (1);
19657 /* Allocate a TREE_VEC and copy in all of the arguments */
19658 argvec
= make_tree_vec (nargs
- ia
);
19659 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19660 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19662 /* Copy the parameter into parmvec. */
19663 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19664 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19665 /*subr=*/subr
, explain_p
))
19668 /* Advance to the end of the list of parameters. */
19669 parms
= TREE_CHAIN (parms
);
19672 /* Fail if we've reached the end of the parm list, and more args
19673 are present, and the parm list isn't variadic. */
19674 if (ia
< nargs
&& parms
== void_list_node
)
19675 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19676 /* Fail if parms are left and they don't have default values and
19677 they aren't all deduced as empty packs (c++/57397). This is
19678 consistent with sufficient_parms_p. */
19679 if (parms
&& parms
!= void_list_node
19680 && TREE_PURPOSE (parms
) == NULL_TREE
)
19682 unsigned int count
= nargs
;
19687 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19690 p
= TREE_CHAIN (p
);
19692 while (p
&& p
!= void_list_node
);
19693 if (count
!= nargs
)
19694 return unify_too_few_arguments (explain_p
, ia
, count
,
19700 tsubst_flags_t complain
= (explain_p
19701 ? tf_warning_or_error
19703 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19705 for (i
= 0; i
< ntparms
; i
++)
19707 tree targ
= TREE_VEC_ELT (targs
, i
);
19708 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19710 /* Clear the "incomplete" flags on all argument packs now so that
19711 substituting them into later default arguments works. */
19712 if (targ
&& ARGUMENT_PACK_P (targ
))
19714 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19715 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19718 if (targ
|| tparm
== error_mark_node
)
19720 tparm
= TREE_VALUE (tparm
);
19722 if (TREE_CODE (tparm
) == TYPE_DECL
19723 && !tried_array_deduction
)
19725 try_array_deduction (tparms
, targs
, xparms
);
19726 tried_array_deduction
= true;
19727 if (TREE_VEC_ELT (targs
, i
))
19731 /* If this is an undeduced nontype parameter that depends on
19732 a type parameter, try another pass; its type may have been
19733 deduced from a later argument than the one from which
19734 this parameter can be deduced. */
19735 if (TREE_CODE (tparm
) == PARM_DECL
19736 && uses_template_parms (TREE_TYPE (tparm
))
19737 && saw_undeduced
< 2)
19743 /* Core issue #226 (C++0x) [temp.deduct]:
19745 If a template argument has not been deduced, its
19746 default template argument, if any, is used.
19748 When we are in C++98 mode, TREE_PURPOSE will either
19749 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19750 to explicitly check cxx_dialect here. */
19751 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19752 /* OK, there is a default argument. Wait until after the
19753 conversion check to do substitution. */
19756 /* If the type parameter is a parameter pack, then it will
19757 be deduced to an empty parameter pack. */
19758 if (template_parameter_pack_p (tparm
))
19762 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19764 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19765 TREE_CONSTANT (arg
) = 1;
19768 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19770 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19772 TREE_VEC_ELT (targs
, i
) = arg
;
19776 return unify_parameter_deduction_failure (explain_p
, tparm
);
19779 /* DR 1391: All parameters have args, now check non-dependent parms for
19781 if (saw_undeduced
< 2)
19782 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19783 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19785 parm
= TREE_VALUE (parms
);
19787 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19788 && (!TREE_CHAIN (parms
)
19789 || TREE_CHAIN (parms
) == void_list_node
))
19790 /* For a function parameter pack that occurs at the end of the
19791 parameter-declaration-list, the type A of each remaining
19792 argument of the call is compared with the type P of the
19793 declarator-id of the function parameter pack. */
19796 parms
= TREE_CHAIN (parms
);
19798 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19799 /* For a function parameter pack that does not occur at the
19800 end of the parameter-declaration-list, the type of the
19801 parameter pack is a non-deduced context. */
19807 if (uses_template_parms (parm
))
19809 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19814 /* Now substitute into the default template arguments. */
19815 for (i
= 0; i
< ntparms
; i
++)
19817 tree targ
= TREE_VEC_ELT (targs
, i
);
19818 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19820 if (targ
|| tparm
== error_mark_node
)
19822 tree parm
= TREE_VALUE (tparm
);
19824 if (TREE_CODE (parm
) == PARM_DECL
19825 && uses_template_parms (TREE_TYPE (parm
))
19826 && saw_undeduced
< 2)
19829 tree arg
= TREE_PURPOSE (tparm
);
19830 reopen_deferring_access_checks (*checks
);
19831 location_t save_loc
= input_location
;
19833 input_location
= DECL_SOURCE_LOCATION (parm
);
19834 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19835 if (!uses_template_parms (arg
))
19836 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19838 else if (saw_undeduced
< 2)
19841 arg
= error_mark_node
;
19842 input_location
= save_loc
;
19843 *checks
= get_deferred_access_checks ();
19844 pop_deferring_access_checks ();
19845 if (arg
== error_mark_node
)
19849 TREE_VEC_ELT (targs
, i
) = arg
;
19850 /* The position of the first default template argument,
19851 is also the number of non-defaulted arguments in TARGS.
19853 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19858 if (saw_undeduced
++ == 1)
19862 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19863 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19865 return unify_success (explain_p
);
19868 /* Subroutine of type_unification_real. Args are like the variables
19869 at the call site. ARG is an overloaded function (or template-id);
19870 we try deducing template args from each of the overloads, and if
19871 only one succeeds, we go with that. Modifies TARGS and returns
19872 true on success. */
19875 resolve_overloaded_unification (tree tparms
,
19879 unification_kind_t strict
,
19883 tree tempargs
= copy_node (targs
);
19885 tree goodfn
= NULL_TREE
;
19888 if (TREE_CODE (arg
) == ADDR_EXPR
)
19890 arg
= TREE_OPERAND (arg
, 0);
19896 if (TREE_CODE (arg
) == COMPONENT_REF
)
19897 /* Handle `&x' where `x' is some static or non-static member
19899 arg
= TREE_OPERAND (arg
, 1);
19901 if (TREE_CODE (arg
) == OFFSET_REF
)
19902 arg
= TREE_OPERAND (arg
, 1);
19904 /* Strip baselink information. */
19905 if (BASELINK_P (arg
))
19906 arg
= BASELINK_FUNCTIONS (arg
);
19908 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19910 /* If we got some explicit template args, we need to plug them into
19911 the affected templates before we try to unify, in case the
19912 explicit args will completely resolve the templates in question. */
19915 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19916 arg
= TREE_OPERAND (arg
, 0);
19918 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19921 tree subargs
, elem
;
19923 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19926 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19927 expl_subargs
, NULL_TREE
, tf_none
,
19928 /*require_all_args=*/true,
19929 /*use_default_args=*/true);
19930 if (subargs
!= error_mark_node
19931 && !any_dependent_template_arguments_p (subargs
))
19933 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19934 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19935 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19936 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19945 /* If no templates (or more than one) are fully resolved by the
19946 explicit arguments, this template-id is a non-deduced context; it
19947 could still be OK if we deduce all template arguments for the
19948 enclosing call through other arguments. */
19952 else if (TREE_CODE (arg
) != OVERLOAD
19953 && TREE_CODE (arg
) != FUNCTION_DECL
)
19954 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19955 -- but the deduction does not succeed because the expression is
19956 not just the function on its own. */
19959 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19962 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19963 strict
, sub_strict
, addr_p
, explain_p
)
19964 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19971 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19972 to function or pointer to member function argument if the set of
19973 overloaded functions does not contain function templates and at most
19974 one of a set of overloaded functions provides a unique match.
19976 So if we found multiple possibilities, we return success but don't
19977 deduce anything. */
19981 int i
= TREE_VEC_LENGTH (targs
);
19983 if (TREE_VEC_ELT (tempargs
, i
))
19985 tree old
= TREE_VEC_ELT (targs
, i
);
19986 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19987 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19988 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19989 /* Don't forget explicit template arguments in a pack. */
19990 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19991 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19992 TREE_VEC_ELT (targs
, i
) = new_
;
20001 /* Core DR 115: In contexts where deduction is done and fails, or in
20002 contexts where deduction is not done, if a template argument list is
20003 specified and it, along with any default template arguments, identifies
20004 a single function template specialization, then the template-id is an
20005 lvalue for the function template specialization. */
20008 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20010 tree expr
, offset
, baselink
;
20013 if (!type_unknown_p (orig_expr
))
20018 offset
= NULL_TREE
;
20019 baselink
= NULL_TREE
;
20021 if (TREE_CODE (expr
) == ADDR_EXPR
)
20023 expr
= TREE_OPERAND (expr
, 0);
20026 if (TREE_CODE (expr
) == OFFSET_REF
)
20029 expr
= TREE_OPERAND (expr
, 1);
20031 if (BASELINK_P (expr
))
20034 expr
= BASELINK_FUNCTIONS (expr
);
20037 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20040 tree goodfn
= NULL_TREE
;
20042 /* If we got some explicit template args, we need to plug them into
20043 the affected templates before we try to unify, in case the
20044 explicit args will completely resolve the templates in question. */
20046 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20047 tree arg
= TREE_OPERAND (expr
, 0);
20048 tree badfn
= NULL_TREE
;
20049 tree badargs
= NULL_TREE
;
20051 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20054 tree subargs
, elem
;
20056 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20059 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20060 expl_subargs
, NULL_TREE
, tf_none
,
20061 /*require_all_args=*/true,
20062 /*use_default_args=*/true);
20063 if (subargs
!= error_mark_node
20064 && !any_dependent_template_arguments_p (subargs
))
20066 elem
= instantiate_template (fn
, subargs
, tf_none
);
20067 if (elem
== error_mark_node
)
20072 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20081 mark_used (goodfn
);
20084 expr
= build_baselink (BASELINK_BINFO (baselink
),
20085 BASELINK_ACCESS_BINFO (baselink
),
20086 expr
, BASELINK_OPTYPE (baselink
));
20090 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20091 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20094 expr
= cp_build_addr_expr (expr
, complain
);
20097 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20098 /* There were no good options and at least one bad one, so let the
20099 user know what the problem is. */
20100 instantiate_template (badfn
, badargs
, complain
);
20105 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20106 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20107 different overloads deduce different arguments for a given parm.
20108 ADDR_P is true if the expression for which deduction is being
20109 performed was of the form "& fn" rather than simply "fn".
20111 Returns 1 on success. */
20114 try_one_overload (tree tparms
,
20119 unification_kind_t strict
,
20128 if (arg
== error_mark_node
)
20131 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20132 to function or pointer to member function argument if the set of
20133 overloaded functions does not contain function templates and at most
20134 one of a set of overloaded functions provides a unique match.
20136 So if this is a template, just return success. */
20138 if (uses_template_parms (arg
))
20141 if (TREE_CODE (arg
) == METHOD_TYPE
)
20142 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20144 arg
= build_pointer_type (arg
);
20146 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20148 /* We don't copy orig_targs for this because if we have already deduced
20149 some template args from previous args, unify would complain when we
20150 try to deduce a template parameter for the same argument, even though
20151 there isn't really a conflict. */
20152 nargs
= TREE_VEC_LENGTH (targs
);
20153 tempargs
= make_tree_vec (nargs
);
20155 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20158 /* First make sure we didn't deduce anything that conflicts with
20159 explicitly specified args. */
20160 for (i
= nargs
; i
--; )
20162 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20163 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20167 else if (uses_template_parms (elt
))
20168 /* Since we're unifying against ourselves, we will fill in
20169 template args used in the function parm list with our own
20170 template parms. Discard them. */
20171 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20172 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20174 /* Check that the argument at each index of the deduced argument pack
20175 is equivalent to the corresponding explicitly specified argument.
20176 We may have deduced more arguments than were explicitly specified,
20179 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20180 that's wrong if we deduce the same argument pack from multiple
20181 function arguments: it's only incomplete the first time. */
20183 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20184 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20186 if (TREE_VEC_LENGTH (deduced_pack
)
20187 < TREE_VEC_LENGTH (explicit_pack
))
20190 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20191 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20192 TREE_VEC_ELT (deduced_pack
, j
)))
20195 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20199 for (i
= nargs
; i
--; )
20201 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20204 TREE_VEC_ELT (targs
, i
) = elt
;
20210 /* PARM is a template class (perhaps with unbound template
20211 parameters). ARG is a fully instantiated type. If ARG can be
20212 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20213 TARGS are as for unify. */
20216 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20219 tree copy_of_targs
;
20221 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20223 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20224 /* Matches anything. */;
20225 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20226 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20229 /* We need to make a new template argument vector for the call to
20230 unify. If we used TARGS, we'd clutter it up with the result of
20231 the attempted unification, even if this class didn't work out.
20232 We also don't want to commit ourselves to all the unifications
20233 we've already done, since unification is supposed to be done on
20234 an argument-by-argument basis. In other words, consider the
20235 following pathological case:
20237 template <int I, int J, int K>
20240 template <int I, int J>
20241 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20243 template <int I, int J, int K>
20244 void f(S<I, J, K>, S<I, I, I>);
20253 Now, by the time we consider the unification involving `s2', we
20254 already know that we must have `f<0, 0, 0>'. But, even though
20255 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20256 because there are two ways to unify base classes of S<0, 1, 2>
20257 with S<I, I, I>. If we kept the already deduced knowledge, we
20258 would reject the possibility I=1. */
20259 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20261 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20263 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20268 /* If unification failed, we're done. */
20269 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20270 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20276 /* Given a template type PARM and a class type ARG, find the unique
20277 base type in ARG that is an instance of PARM. We do not examine
20278 ARG itself; only its base-classes. If there is not exactly one
20279 appropriate base class, return NULL_TREE. PARM may be the type of
20280 a partial specialization, as well as a plain template type. Used
20283 static enum template_base_result
20284 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20285 bool explain_p
, tree
*result
)
20287 tree rval
= NULL_TREE
;
20290 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20292 binfo
= TYPE_BINFO (complete_type (arg
));
20295 /* The type could not be completed. */
20296 *result
= NULL_TREE
;
20297 return tbr_incomplete_type
;
20300 /* Walk in inheritance graph order. The search order is not
20301 important, and this avoids multiple walks of virtual bases. */
20302 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20304 tree r
= try_class_unification (tparms
, targs
, parm
,
20305 BINFO_TYPE (binfo
), explain_p
);
20309 /* If there is more than one satisfactory baseclass, then:
20313 If they yield more than one possible deduced A, the type
20317 if (rval
&& !same_type_p (r
, rval
))
20319 *result
= NULL_TREE
;
20320 return tbr_ambiguous_baseclass
;
20328 return tbr_success
;
20331 /* Returns the level of DECL, which declares a template parameter. */
20334 template_decl_level (tree decl
)
20336 switch (TREE_CODE (decl
))
20339 case TEMPLATE_DECL
:
20340 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20343 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20346 gcc_unreachable ();
20351 /* Decide whether ARG can be unified with PARM, considering only the
20352 cv-qualifiers of each type, given STRICT as documented for unify.
20353 Returns nonzero iff the unification is OK on that basis. */
20356 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20358 int arg_quals
= cp_type_quals (arg
);
20359 int parm_quals
= cp_type_quals (parm
);
20361 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20362 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20364 /* Although a CVR qualifier is ignored when being applied to a
20365 substituted template parameter ([8.3.2]/1 for example), that
20366 does not allow us to unify "const T" with "int&" because both
20367 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20368 It is ok when we're allowing additional CV qualifiers
20369 at the outer level [14.8.2.1]/3,1st bullet. */
20370 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20371 || TREE_CODE (arg
) == FUNCTION_TYPE
20372 || TREE_CODE (arg
) == METHOD_TYPE
)
20373 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20376 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20377 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20381 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20382 && (arg_quals
& parm_quals
) != parm_quals
)
20385 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20386 && (parm_quals
& arg_quals
) != arg_quals
)
20392 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20394 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20396 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20397 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20398 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20400 *index
= TEMPLATE_TYPE_IDX (parm
);
20401 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20405 *index
= TEMPLATE_PARM_IDX (parm
);
20406 *level
= TEMPLATE_PARM_LEVEL (parm
);
20410 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20412 if (unify (TP, TA, P, A, S, EP)) \
20416 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20417 expansion at the end of PACKED_PARMS. Returns 0 if the type
20418 deduction succeeds, 1 otherwise. STRICT is the same as in
20419 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20420 function call argument list. We'll need to adjust the arguments to make them
20421 types. SUBR tells us if this is from a recursive call to
20422 type_unification_real, or for comparing two template argument
20426 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20427 tree packed_args
, unification_kind_t strict
,
20428 bool subr
, bool explain_p
)
20431 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20432 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20433 tree pack
, packs
= NULL_TREE
;
20434 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20436 /* Add in any args remembered from an earlier partial instantiation. */
20437 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20438 int levels
= TMPL_ARGS_DEPTH (targs
);
20440 packed_args
= expand_template_argument_pack (packed_args
);
20442 int len
= TREE_VEC_LENGTH (packed_args
);
20444 /* Determine the parameter packs we will be deducing from the
20445 pattern, and record their current deductions. */
20446 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20447 pack
; pack
= TREE_CHAIN (pack
))
20449 tree parm_pack
= TREE_VALUE (pack
);
20452 /* Determine the index and level of this parameter pack. */
20453 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20454 if (level
< levels
)
20457 /* Keep track of the parameter packs and their corresponding
20459 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20460 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20463 /* Loop through all of the arguments that have not yet been
20464 unified and unify each with the pattern. */
20465 for (i
= start
; i
< len
; i
++)
20468 bool any_explicit
= false;
20469 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20471 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20472 or the element of its argument pack at the current index if
20473 this argument was explicitly specified. */
20474 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20478 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20481 if (TREE_VALUE (pack
)
20482 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20483 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20485 any_explicit
= true;
20486 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20488 TMPL_ARG (targs
, level
, idx
) = arg
;
20491 /* If we had explicit template arguments, substitute them into the
20492 pattern before deduction. */
20495 /* Some arguments might still be unspecified or dependent. */
20497 ++processing_template_decl
;
20498 dependent
= any_dependent_template_arguments_p (targs
);
20500 --processing_template_decl
;
20501 parm
= tsubst (pattern
, targs
,
20502 explain_p
? tf_warning_or_error
: tf_none
,
20505 --processing_template_decl
;
20506 if (parm
== error_mark_node
)
20512 /* Unify the pattern with the current argument. */
20513 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20517 /* For each parameter pack, collect the deduced value. */
20518 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20521 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20523 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20524 TMPL_ARG (targs
, level
, idx
);
20528 /* Verify that the results of unification with the parameter packs
20529 produce results consistent with what we've seen before, and make
20530 the deduced argument packs available. */
20531 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20533 tree old_pack
= TREE_VALUE (pack
);
20534 tree new_args
= TREE_TYPE (pack
);
20535 int i
, len
= TREE_VEC_LENGTH (new_args
);
20537 bool nondeduced_p
= false;
20539 /* By default keep the original deduced argument pack.
20540 If necessary, more specific code is going to update the
20541 resulting deduced argument later down in this function. */
20542 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20543 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20545 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20546 actually deduce anything. */
20547 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20548 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20549 nondeduced_p
= true;
20553 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20555 /* If we had fewer function args than explicit template args,
20556 just use the explicits. */
20557 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20558 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20559 if (len
< explicit_len
)
20560 new_args
= explicit_args
;
20566 /* Build the deduced *_ARGUMENT_PACK. */
20567 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20569 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20570 TREE_CONSTANT (result
) = 1;
20573 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20575 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20577 /* Note the deduced argument packs for this parameter
20579 TMPL_ARG (targs
, level
, idx
) = result
;
20581 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20582 && (ARGUMENT_PACK_ARGS (old_pack
)
20583 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20585 /* We only had the explicitly-provided arguments before, but
20586 now we have a complete set of arguments. */
20587 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20589 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20590 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20591 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20595 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20596 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20598 if (!comp_template_args (old_args
, new_args
,
20599 &bad_old_arg
, &bad_new_arg
))
20600 /* Inconsistent unification of this parameter pack. */
20601 return unify_parameter_pack_inconsistent (explain_p
,
20607 return unify_success (explain_p
);
20610 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20611 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20612 parameters and return value are as for unify. */
20615 unify_array_domain (tree tparms
, tree targs
,
20616 tree parm_dom
, tree arg_dom
,
20624 /* Our representation of array types uses "N - 1" as the
20625 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20626 not an integer constant. We cannot unify arbitrarily
20627 complex expressions, so we eliminate the MINUS_EXPRs
20629 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20630 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20633 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20634 parm_max
= TREE_OPERAND (parm_max
, 0);
20636 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20637 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20640 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20641 trying to unify the type of a variable with the type
20642 of a template parameter. For example:
20644 template <unsigned int N>
20645 void f (char (&) [N]);
20652 Here, the type of the ARG will be "int [g(i)]", and
20653 may be a SAVE_EXPR, etc. */
20654 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20655 return unify_vla_arg (explain_p
, arg_dom
);
20656 arg_max
= TREE_OPERAND (arg_max
, 0);
20659 /* If only one of the bounds used a MINUS_EXPR, compensate
20660 by adding one to the other bound. */
20661 if (parm_cst
&& !arg_cst
)
20662 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20666 else if (arg_cst
&& !parm_cst
)
20667 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20672 return unify (tparms
, targs
, parm_max
, arg_max
,
20673 UNIFY_ALLOW_INTEGER
, explain_p
);
20676 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20678 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20683 if (PACK_EXPANSION_P (t
))
20684 t
= PACK_EXPANSION_PATTERN (t
);
20685 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20686 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20687 || DECL_TYPE_TEMPLATE_P (t
))
20689 else if (TYPE_P (t
))
20695 /* Deduce the value of template parameters. TPARMS is the (innermost)
20696 set of template parameters to a template. TARGS is the bindings
20697 for those template parameters, as determined thus far; TARGS may
20698 include template arguments for outer levels of template parameters
20699 as well. PARM is a parameter to a template function, or a
20700 subcomponent of that parameter; ARG is the corresponding argument.
20701 This function attempts to match PARM with ARG in a manner
20702 consistent with the existing assignments in TARGS. If more values
20703 are deduced, then TARGS is updated.
20705 Returns 0 if the type deduction succeeds, 1 otherwise. The
20706 parameter STRICT is a bitwise or of the following flags:
20709 Require an exact match between PARM and ARG.
20710 UNIFY_ALLOW_MORE_CV_QUAL:
20711 Allow the deduced ARG to be more cv-qualified (by qualification
20712 conversion) than ARG.
20713 UNIFY_ALLOW_LESS_CV_QUAL:
20714 Allow the deduced ARG to be less cv-qualified than ARG.
20715 UNIFY_ALLOW_DERIVED:
20716 Allow the deduced ARG to be a template base class of ARG,
20717 or a pointer to a template base class of the type pointed to by
20719 UNIFY_ALLOW_INTEGER:
20720 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20721 case for more information.
20722 UNIFY_ALLOW_OUTER_LEVEL:
20723 This is the outermost level of a deduction. Used to determine validity
20724 of qualification conversions. A valid qualification conversion must
20725 have const qualified pointers leading up to the inner type which
20726 requires additional CV quals, except at the outer level, where const
20727 is not required [conv.qual]. It would be normal to set this flag in
20728 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20729 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20730 This is the outermost level of a deduction, and PARM can be more CV
20731 qualified at this point.
20732 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20733 This is the outermost level of a deduction, and PARM can be less CV
20734 qualified at this point. */
20737 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20743 int strict_in
= strict
;
20744 tsubst_flags_t complain
= (explain_p
20745 ? tf_warning_or_error
20748 /* I don't think this will do the right thing with respect to types.
20749 But the only case I've seen it in so far has been array bounds, where
20750 signedness is the only information lost, and I think that will be
20752 while (CONVERT_EXPR_P (parm
))
20753 parm
= TREE_OPERAND (parm
, 0);
20755 if (arg
== error_mark_node
)
20756 return unify_invalid (explain_p
);
20757 if (arg
== unknown_type_node
20758 || arg
== init_list_type_node
)
20759 /* We can't deduce anything from this, but we might get all the
20760 template args from other function args. */
20761 return unify_success (explain_p
);
20763 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20764 return unify_success (explain_p
);
20766 /* If PARM uses template parameters, then we can't bail out here,
20767 even if ARG == PARM, since we won't record unifications for the
20768 template parameters. We might need them if we're trying to
20769 figure out which of two things is more specialized. */
20770 if (arg
== parm
&& !uses_template_parms (parm
))
20771 return unify_success (explain_p
);
20773 /* Handle init lists early, so the rest of the function can assume
20774 we're dealing with a type. */
20775 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20779 tree orig_parm
= parm
;
20781 /* Replace T with std::initializer_list<T> for deduction. */
20782 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20783 && flag_deduce_init_list
)
20784 parm
= listify (parm
);
20786 if (!is_std_init_list (parm
)
20787 && TREE_CODE (parm
) != ARRAY_TYPE
)
20788 /* We can only deduce from an initializer list argument if the
20789 parameter is std::initializer_list or an array; otherwise this
20790 is a non-deduced context. */
20791 return unify_success (explain_p
);
20793 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20794 elttype
= TREE_TYPE (parm
);
20797 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20798 /* Deduction is defined in terms of a single type, so just punt
20799 on the (bizarre) std::initializer_list<T...>. */
20800 if (PACK_EXPANSION_P (elttype
))
20801 return unify_success (explain_p
);
20804 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20806 int elt_strict
= strict
;
20808 if (elt
== error_mark_node
)
20809 return unify_invalid (explain_p
);
20811 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20813 tree type
= TREE_TYPE (elt
);
20814 if (type
== error_mark_node
)
20815 return unify_invalid (explain_p
);
20816 /* It should only be possible to get here for a call. */
20817 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20818 elt_strict
|= maybe_adjust_types_for_deduction
20819 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20823 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20827 if (TREE_CODE (parm
) == ARRAY_TYPE
20828 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20830 /* Also deduce from the length of the initializer list. */
20831 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20832 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20833 if (idx
== error_mark_node
)
20834 return unify_invalid (explain_p
);
20835 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20839 /* If the std::initializer_list<T> deduction worked, replace the
20840 deduced A with std::initializer_list<A>. */
20841 if (orig_parm
!= parm
)
20843 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20844 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20845 targ
= listify (targ
);
20846 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20848 return unify_success (explain_p
);
20851 /* If parm and arg aren't the same kind of thing (template, type, or
20852 expression), fail early. */
20853 if (pa_kind (parm
) != pa_kind (arg
))
20854 return unify_invalid (explain_p
);
20856 /* Immediately reject some pairs that won't unify because of
20857 cv-qualification mismatches. */
20858 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20860 /* It is the elements of the array which hold the cv quals of an array
20861 type, and the elements might be template type parms. We'll check
20862 when we recurse. */
20863 && TREE_CODE (arg
) != ARRAY_TYPE
20864 /* We check the cv-qualifiers when unifying with template type
20865 parameters below. We want to allow ARG `const T' to unify with
20866 PARM `T' for example, when computing which of two templates
20867 is more specialized, for example. */
20868 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20869 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20870 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20872 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20873 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20874 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20875 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20876 strict
&= ~UNIFY_ALLOW_DERIVED
;
20877 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20878 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20880 switch (TREE_CODE (parm
))
20882 case TYPENAME_TYPE
:
20884 case UNBOUND_CLASS_TEMPLATE
:
20885 /* In a type which contains a nested-name-specifier, template
20886 argument values cannot be deduced for template parameters used
20887 within the nested-name-specifier. */
20888 return unify_success (explain_p
);
20890 case TEMPLATE_TYPE_PARM
:
20891 case TEMPLATE_TEMPLATE_PARM
:
20892 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20893 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20894 if (error_operand_p (tparm
))
20895 return unify_invalid (explain_p
);
20897 if (TEMPLATE_TYPE_LEVEL (parm
)
20898 != template_decl_level (tparm
))
20899 /* The PARM is not one we're trying to unify. Just check
20900 to see if it matches ARG. */
20902 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20903 && (is_auto (parm
) ? is_auto (arg
)
20904 : same_type_p (parm
, arg
)))
20905 return unify_success (explain_p
);
20907 return unify_type_mismatch (explain_p
, parm
, arg
);
20909 idx
= TEMPLATE_TYPE_IDX (parm
);
20910 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20911 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20912 if (error_operand_p (tparm
))
20913 return unify_invalid (explain_p
);
20915 /* Check for mixed types and values. */
20916 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20917 && TREE_CODE (tparm
) != TYPE_DECL
)
20918 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20919 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20920 gcc_unreachable ();
20922 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20924 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20925 && CLASS_TYPE_P (arg
))
20927 /* First try to match ARG directly. */
20928 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20932 /* Otherwise, look for a suitable base of ARG, as below. */
20933 enum template_base_result r
;
20934 r
= get_template_base (tparms
, targs
, parm
, arg
,
20937 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20941 /* ARG must be constructed from a template class or a template
20942 template parameter. */
20943 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20944 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20945 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20947 /* Deduce arguments T, i from TT<T> or TT<i>. */
20948 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20951 arg
= TYPE_TI_TEMPLATE (arg
);
20953 /* Fall through to deduce template name. */
20956 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20957 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20959 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20961 /* Simple cases: Value already set, does match or doesn't. */
20962 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20963 return unify_success (explain_p
);
20965 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20969 /* If PARM is `const T' and ARG is only `int', we don't have
20970 a match unless we are allowing additional qualification.
20971 If ARG is `const int' and PARM is just `T' that's OK;
20972 that binds `const int' to `T'. */
20973 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20975 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20977 /* Consider the case where ARG is `const volatile int' and
20978 PARM is `const T'. Then, T should be `volatile int'. */
20979 arg
= cp_build_qualified_type_real
20980 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20981 if (arg
== error_mark_node
)
20982 return unify_invalid (explain_p
);
20984 /* Simple cases: Value already set, does match or doesn't. */
20985 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20986 return unify_success (explain_p
);
20988 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20990 /* Make sure that ARG is not a variable-sized array. (Note
20991 that were talking about variable-sized arrays (like
20992 `int[n]'), rather than arrays of unknown size (like
20993 `int[]').) We'll get very confused by such a type since
20994 the bound of the array is not constant, and therefore
20995 not mangleable. Besides, such types are not allowed in
20996 ISO C++, so we can do as we please here. We do allow
20997 them for 'auto' deduction, since that isn't ABI-exposed. */
20998 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20999 return unify_vla_arg (explain_p
, arg
);
21001 /* Strip typedefs as in convert_template_argument. */
21002 arg
= canonicalize_type_argument (arg
, tf_none
);
21005 /* If ARG is a parameter pack or an expansion, we cannot unify
21006 against it unless PARM is also a parameter pack. */
21007 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21008 && !template_parameter_pack_p (parm
))
21009 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21011 /* If the argument deduction results is a METHOD_TYPE,
21012 then there is a problem.
21013 METHOD_TYPE doesn't map to any real C++ type the result of
21014 the deduction can not be of that type. */
21015 if (TREE_CODE (arg
) == METHOD_TYPE
)
21016 return unify_method_type_error (explain_p
, arg
);
21018 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21019 return unify_success (explain_p
);
21021 case TEMPLATE_PARM_INDEX
:
21022 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21023 if (error_operand_p (tparm
))
21024 return unify_invalid (explain_p
);
21026 if (TEMPLATE_PARM_LEVEL (parm
)
21027 != template_decl_level (tparm
))
21029 /* The PARM is not one we're trying to unify. Just check
21030 to see if it matches ARG. */
21031 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21032 && cp_tree_equal (parm
, arg
));
21034 unify_expression_unequal (explain_p
, parm
, arg
);
21038 idx
= TEMPLATE_PARM_IDX (parm
);
21039 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21043 if ((strict
& UNIFY_ALLOW_INTEGER
)
21044 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21045 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21046 /* We're deducing from an array bound, the type doesn't matter. */
21047 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21048 int x
= !cp_tree_equal (targ
, arg
);
21050 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21054 /* [temp.deduct.type] If, in the declaration of a function template
21055 with a non-type template-parameter, the non-type
21056 template-parameter is used in an expression in the function
21057 parameter-list and, if the corresponding template-argument is
21058 deduced, the template-argument type shall match the type of the
21059 template-parameter exactly, except that a template-argument
21060 deduced from an array bound may be of any integral type.
21061 The non-type parameter might use already deduced type parameters. */
21062 ++processing_template_decl
;
21063 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
21064 --processing_template_decl
;
21065 if (tree a
= type_uses_auto (tparm
))
21067 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21068 if (tparm
== error_mark_node
)
21072 if (!TREE_TYPE (arg
))
21073 /* Template-parameter dependent expression. Just accept it for now.
21074 It will later be processed in convert_template_argument. */
21076 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21077 non_reference (tparm
)))
21079 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21080 && CP_INTEGRAL_TYPE_P (tparm
))
21081 /* Convert the ARG to the type of PARM; the deduced non-type
21082 template argument must exactly match the types of the
21083 corresponding parameter. */
21084 arg
= fold (build_nop (tparm
, arg
));
21085 else if (uses_template_parms (tparm
))
21087 /* We haven't deduced the type of this parameter yet. */
21088 if (cxx_dialect
>= cxx17
21089 /* We deduce from array bounds in try_array_deduction. */
21090 && !(strict
& UNIFY_ALLOW_INTEGER
))
21092 /* Deduce it from the non-type argument. */
21093 tree atype
= TREE_TYPE (arg
);
21094 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21096 UNIFY_ALLOW_NONE
, explain_p
);
21099 /* Try again later. */
21100 return unify_success (explain_p
);
21103 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21105 /* If ARG is a parameter pack or an expansion, we cannot unify
21106 against it unless PARM is also a parameter pack. */
21107 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21108 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21109 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21112 bool removed_attr
= false;
21113 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21115 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21116 return unify_success (explain_p
);
21120 /* A pointer-to-member constant can be unified only with
21121 another constant. */
21122 if (TREE_CODE (arg
) != PTRMEM_CST
)
21123 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21125 /* Just unify the class member. It would be useless (and possibly
21126 wrong, depending on the strict flags) to unify also
21127 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21128 arg refer to the same variable, even if through different
21129 classes. For instance:
21131 struct A { int x; };
21134 Unification of &A::x and &B::x must succeed. */
21135 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21136 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21141 if (!TYPE_PTR_P (arg
))
21142 return unify_type_mismatch (explain_p
, parm
, arg
);
21144 /* [temp.deduct.call]
21146 A can be another pointer or pointer to member type that can
21147 be converted to the deduced A via a qualification
21148 conversion (_conv.qual_).
21150 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21151 This will allow for additional cv-qualification of the
21152 pointed-to types if appropriate. */
21154 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21155 /* The derived-to-base conversion only persists through one
21156 level of pointers. */
21157 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21159 return unify (tparms
, targs
, TREE_TYPE (parm
),
21160 TREE_TYPE (arg
), strict
, explain_p
);
21163 case REFERENCE_TYPE
:
21164 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21165 return unify_type_mismatch (explain_p
, parm
, arg
);
21166 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21167 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21170 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21171 return unify_type_mismatch (explain_p
, parm
, arg
);
21172 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21173 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21174 return unify_type_mismatch (explain_p
, parm
, arg
);
21175 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21176 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21177 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21178 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21179 TYPE_DOMAIN (arg
), explain_p
);
21180 return unify_success (explain_p
);
21187 case ENUMERAL_TYPE
:
21190 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21191 return unify_type_mismatch (explain_p
, parm
, arg
);
21193 /* We have already checked cv-qualification at the top of the
21195 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21196 return unify_type_mismatch (explain_p
, parm
, arg
);
21198 /* As far as unification is concerned, this wins. Later checks
21199 will invalidate it if necessary. */
21200 return unify_success (explain_p
);
21202 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21203 /* Type INTEGER_CST can come from ordinary constant template args. */
21205 while (CONVERT_EXPR_P (arg
))
21206 arg
= TREE_OPERAND (arg
, 0);
21208 if (TREE_CODE (arg
) != INTEGER_CST
)
21209 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21210 return (tree_int_cst_equal (parm
, arg
)
21211 ? unify_success (explain_p
)
21212 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21216 int i
, len
, argslen
;
21217 int parm_variadic_p
= 0;
21219 if (TREE_CODE (arg
) != TREE_VEC
)
21220 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21222 len
= TREE_VEC_LENGTH (parm
);
21223 argslen
= TREE_VEC_LENGTH (arg
);
21225 /* Check for pack expansions in the parameters. */
21226 for (i
= 0; i
< len
; ++i
)
21228 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21231 /* We can unify against something with a trailing
21233 parm_variadic_p
= 1;
21235 /* [temp.deduct.type]/9: If the template argument list of
21236 P contains a pack expansion that is not the last
21237 template argument, the entire template argument list
21238 is a non-deduced context. */
21239 return unify_success (explain_p
);
21243 /* If we don't have enough arguments to satisfy the parameters
21244 (not counting the pack expression at the end), or we have
21245 too many arguments for a parameter list that doesn't end in
21246 a pack expression, we can't unify. */
21247 if (parm_variadic_p
21248 ? argslen
< len
- parm_variadic_p
21250 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21252 /* Unify all of the parameters that precede the (optional)
21253 pack expression. */
21254 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21256 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21257 TREE_VEC_ELT (parm
, i
),
21258 TREE_VEC_ELT (arg
, i
),
21259 UNIFY_ALLOW_NONE
, explain_p
);
21261 if (parm_variadic_p
)
21262 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21264 /*subr=*/true, explain_p
);
21265 return unify_success (explain_p
);
21270 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21271 return unify_type_mismatch (explain_p
, parm
, arg
);
21273 if (TYPE_PTRMEMFUNC_P (parm
))
21275 if (!TYPE_PTRMEMFUNC_P (arg
))
21276 return unify_type_mismatch (explain_p
, parm
, arg
);
21278 return unify (tparms
, targs
,
21279 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21280 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21281 strict
, explain_p
);
21283 else if (TYPE_PTRMEMFUNC_P (arg
))
21284 return unify_type_mismatch (explain_p
, parm
, arg
);
21286 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21288 tree t
= NULL_TREE
;
21290 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21292 /* First, we try to unify the PARM and ARG directly. */
21293 t
= try_class_unification (tparms
, targs
,
21294 parm
, arg
, explain_p
);
21298 /* Fallback to the special case allowed in
21299 [temp.deduct.call]:
21301 If P is a class, and P has the form
21302 template-id, then A can be a derived class of
21303 the deduced A. Likewise, if P is a pointer to
21304 a class of the form template-id, A can be a
21305 pointer to a derived class pointed to by the
21307 enum template_base_result r
;
21308 r
= get_template_base (tparms
, targs
, parm
, arg
,
21313 /* Don't give the derived diagnostic if we're
21314 already dealing with the same template. */
21316 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21317 && (CLASSTYPE_TI_TEMPLATE (parm
)
21318 == CLASSTYPE_TI_TEMPLATE (arg
)));
21319 return unify_no_common_base (explain_p
&& !same_template
,
21324 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21325 && (CLASSTYPE_TI_TEMPLATE (parm
)
21326 == CLASSTYPE_TI_TEMPLATE (arg
)))
21327 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21328 Then, we should unify `int' and `U'. */
21331 /* There's no chance of unification succeeding. */
21332 return unify_type_mismatch (explain_p
, parm
, arg
);
21334 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21335 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21337 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21338 return unify_type_mismatch (explain_p
, parm
, arg
);
21339 return unify_success (explain_p
);
21342 case FUNCTION_TYPE
:
21344 unsigned int nargs
;
21349 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21350 return unify_type_mismatch (explain_p
, parm
, arg
);
21352 /* CV qualifications for methods can never be deduced, they must
21353 match exactly. We need to check them explicitly here,
21354 because type_unification_real treats them as any other
21355 cv-qualified parameter. */
21356 if (TREE_CODE (parm
) == METHOD_TYPE
21357 && (!check_cv_quals_for_unify
21359 class_of_this_parm (arg
),
21360 class_of_this_parm (parm
))))
21361 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21362 if (TREE_CODE (arg
) == FUNCTION_TYPE
21363 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21364 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21365 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21366 return unify_type_mismatch (explain_p
, parm
, arg
);
21368 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21369 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21371 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21372 args
= XALLOCAVEC (tree
, nargs
);
21373 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21374 a
!= NULL_TREE
&& a
!= void_list_node
;
21375 a
= TREE_CHAIN (a
), ++i
)
21376 args
[i
] = TREE_VALUE (a
);
21379 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21380 args
, nargs
, 1, DEDUCE_EXACT
,
21381 LOOKUP_NORMAL
, NULL
, explain_p
))
21384 if (flag_noexcept_type
)
21386 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21387 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21388 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21389 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21390 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21391 && uses_template_parms (TREE_PURPOSE (pspec
)))
21392 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21393 TREE_PURPOSE (aspec
),
21394 UNIFY_ALLOW_NONE
, explain_p
);
21395 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21396 return unify_type_mismatch (explain_p
, parm
, arg
);
21403 /* Unify a pointer to member with a pointer to member function, which
21404 deduces the type of the member as a function type. */
21405 if (TYPE_PTRMEMFUNC_P (arg
))
21407 /* Check top-level cv qualifiers */
21408 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21409 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21411 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21412 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21413 UNIFY_ALLOW_NONE
, explain_p
);
21415 /* Determine the type of the function we are unifying against. */
21416 tree fntype
= static_fn_type (arg
);
21418 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21421 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21422 return unify_type_mismatch (explain_p
, parm
, arg
);
21423 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21424 TYPE_OFFSET_BASETYPE (arg
),
21425 UNIFY_ALLOW_NONE
, explain_p
);
21426 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21427 strict
, explain_p
);
21430 if (DECL_TEMPLATE_PARM_P (parm
))
21431 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21432 if (arg
!= scalar_constant_value (parm
))
21433 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21434 return unify_success (explain_p
);
21437 case TEMPLATE_DECL
:
21438 /* Matched cases are handled by the ARG == PARM test above. */
21439 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21442 /* We might get a variable as a non-type template argument in parm if the
21443 corresponding parameter is type-dependent. Make any necessary
21444 adjustments based on whether arg is a reference. */
21445 if (CONSTANT_CLASS_P (arg
))
21446 parm
= fold_non_dependent_expr (parm
);
21447 else if (REFERENCE_REF_P (arg
))
21449 tree sub
= TREE_OPERAND (arg
, 0);
21451 if (TREE_CODE (sub
) == ADDR_EXPR
)
21452 arg
= TREE_OPERAND (sub
, 0);
21454 /* Now use the normal expression code to check whether they match. */
21457 case TYPE_ARGUMENT_PACK
:
21458 case NONTYPE_ARGUMENT_PACK
:
21459 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21460 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21463 case DECLTYPE_TYPE
:
21464 case UNDERLYING_TYPE
:
21465 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21466 or UNDERLYING_TYPE nodes. */
21467 return unify_success (explain_p
);
21470 /* Unification fails if we hit an error node. */
21471 return unify_invalid (explain_p
);
21474 if (REFERENCE_REF_P (parm
))
21476 bool pexp
= PACK_EXPANSION_P (arg
);
21478 arg
= PACK_EXPANSION_PATTERN (arg
);
21479 if (REFERENCE_REF_P (arg
))
21480 arg
= TREE_OPERAND (arg
, 0);
21482 arg
= make_pack_expansion (arg
, complain
);
21483 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21484 strict
, explain_p
);
21489 /* An unresolved overload is a nondeduced context. */
21490 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21491 return unify_success (explain_p
);
21492 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21494 /* We must be looking at an expression. This can happen with
21498 void foo(S<I>, S<I + 2>);
21500 This is a "nondeduced context":
21504 The nondeduced contexts are:
21506 --A type that is a template-id in which one or more of
21507 the template-arguments is an expression that references
21508 a template-parameter.
21510 In these cases, we assume deduction succeeded, but don't
21511 actually infer any unifications. */
21513 if (!uses_template_parms (parm
)
21514 && !template_args_equal (parm
, arg
))
21515 return unify_expression_unequal (explain_p
, parm
, arg
);
21517 return unify_success (explain_p
);
21520 #undef RECUR_AND_CHECK_FAILURE
21522 /* Note that DECL can be defined in this translation unit, if
21526 mark_definable (tree decl
)
21529 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21530 FOR_EACH_CLONE (clone
, decl
)
21531 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21534 /* Called if RESULT is explicitly instantiated, or is a member of an
21535 explicitly instantiated class. */
21538 mark_decl_instantiated (tree result
, int extern_p
)
21540 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21542 /* If this entity has already been written out, it's too late to
21543 make any modifications. */
21544 if (TREE_ASM_WRITTEN (result
))
21547 /* For anonymous namespace we don't need to do anything. */
21548 if (decl_anon_ns_mem_p (result
))
21550 gcc_assert (!TREE_PUBLIC (result
));
21554 if (TREE_CODE (result
) != FUNCTION_DECL
)
21555 /* The TREE_PUBLIC flag for function declarations will have been
21556 set correctly by tsubst. */
21557 TREE_PUBLIC (result
) = 1;
21559 /* This might have been set by an earlier implicit instantiation. */
21560 DECL_COMDAT (result
) = 0;
21563 DECL_NOT_REALLY_EXTERN (result
) = 0;
21566 mark_definable (result
);
21567 mark_needed (result
);
21568 /* Always make artificials weak. */
21569 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21570 comdat_linkage (result
);
21571 /* For WIN32 we also want to put explicit instantiations in
21572 linkonce sections. */
21573 else if (TREE_PUBLIC (result
))
21574 maybe_make_one_only (result
);
21577 /* If EXTERN_P, then this function will not be emitted -- unless
21578 followed by an explicit instantiation, at which point its linkage
21579 will be adjusted. If !EXTERN_P, then this function will be
21580 emitted here. In neither circumstance do we want
21581 import_export_decl to adjust the linkage. */
21582 DECL_INTERFACE_KNOWN (result
) = 1;
21585 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21586 important template arguments. If any are missing, we check whether
21587 they're important by using error_mark_node for substituting into any
21588 args that were used for partial ordering (the ones between ARGS and END)
21589 and seeing if it bubbles up. */
21592 check_undeduced_parms (tree targs
, tree args
, tree end
)
21594 bool found
= false;
21596 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21597 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21600 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21604 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21605 if (substed
== error_mark_node
)
21611 /* Given two function templates PAT1 and PAT2, return:
21613 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21614 -1 if PAT2 is more specialized than PAT1.
21615 0 if neither is more specialized.
21617 LEN indicates the number of parameters we should consider
21618 (defaulted parameters should not be considered).
21620 The 1998 std underspecified function template partial ordering, and
21621 DR214 addresses the issue. We take pairs of arguments, one from
21622 each of the templates, and deduce them against each other. One of
21623 the templates will be more specialized if all the *other*
21624 template's arguments deduce against its arguments and at least one
21625 of its arguments *does* *not* deduce against the other template's
21626 corresponding argument. Deduction is done as for class templates.
21627 The arguments used in deduction have reference and top level cv
21628 qualifiers removed. Iff both arguments were originally reference
21629 types *and* deduction succeeds in both directions, an lvalue reference
21630 wins against an rvalue reference and otherwise the template
21631 with the more cv-qualified argument wins for that pairing (if
21632 neither is more cv-qualified, they both are equal). Unlike regular
21633 deduction, after all the arguments have been deduced in this way,
21634 we do *not* verify the deduced template argument values can be
21635 substituted into non-deduced contexts.
21637 The logic can be a bit confusing here, because we look at deduce1 and
21638 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21639 can find template arguments for pat1 to make arg1 look like arg2, that
21640 means that arg2 is at least as specialized as arg1. */
21643 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21645 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21646 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21647 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21648 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21649 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21650 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21651 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21652 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21653 tree origs1
, origs2
;
21654 bool lose1
= false;
21655 bool lose2
= false;
21657 /* Remove the this parameter from non-static member functions. If
21658 one is a non-static member function and the other is not a static
21659 member function, remove the first parameter from that function
21660 also. This situation occurs for operator functions where we
21661 locate both a member function (with this pointer) and non-member
21662 operator (with explicit first operand). */
21663 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21665 len
--; /* LEN is the number of significant arguments for DECL1 */
21666 args1
= TREE_CHAIN (args1
);
21667 if (!DECL_STATIC_FUNCTION_P (decl2
))
21668 args2
= TREE_CHAIN (args2
);
21670 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21672 args2
= TREE_CHAIN (args2
);
21673 if (!DECL_STATIC_FUNCTION_P (decl1
))
21676 args1
= TREE_CHAIN (args1
);
21680 /* If only one is a conversion operator, they are unordered. */
21681 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21684 /* Consider the return type for a conversion function */
21685 if (DECL_CONV_FN_P (decl1
))
21687 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21688 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21692 processing_template_decl
++;
21698 /* Stop when an ellipsis is seen. */
21699 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21701 tree arg1
= TREE_VALUE (args1
);
21702 tree arg2
= TREE_VALUE (args2
);
21703 int deduce1
, deduce2
;
21709 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21710 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21712 /* When both arguments are pack expansions, we need only
21713 unify the patterns themselves. */
21714 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21715 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21717 /* This is the last comparison we need to do. */
21721 /* DR 1847: If a particular P contains no template-parameters that
21722 participate in template argument deduction, that P is not used to
21723 determine the ordering. */
21724 if (!uses_deducible_template_parms (arg1
)
21725 && !uses_deducible_template_parms (arg2
))
21728 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21730 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21731 arg1
= TREE_TYPE (arg1
);
21732 quals1
= cp_type_quals (arg1
);
21735 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21737 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21738 arg2
= TREE_TYPE (arg2
);
21739 quals2
= cp_type_quals (arg2
);
21742 arg1
= TYPE_MAIN_VARIANT (arg1
);
21743 arg2
= TYPE_MAIN_VARIANT (arg2
);
21745 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21747 int i
, len2
= remaining_arguments (args2
);
21748 tree parmvec
= make_tree_vec (1);
21749 tree argvec
= make_tree_vec (len2
);
21752 /* Setup the parameter vector, which contains only ARG1. */
21753 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21755 /* Setup the argument vector, which contains the remaining
21757 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21758 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21760 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21761 argvec
, DEDUCE_EXACT
,
21762 /*subr=*/true, /*explain_p=*/false)
21765 /* We cannot deduce in the other direction, because ARG1 is
21766 a pack expansion but ARG2 is not. */
21769 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21771 int i
, len1
= remaining_arguments (args1
);
21772 tree parmvec
= make_tree_vec (1);
21773 tree argvec
= make_tree_vec (len1
);
21776 /* Setup the parameter vector, which contains only ARG1. */
21777 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21779 /* Setup the argument vector, which contains the remaining
21781 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21782 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21784 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21785 argvec
, DEDUCE_EXACT
,
21786 /*subr=*/true, /*explain_p=*/false)
21789 /* We cannot deduce in the other direction, because ARG2 is
21790 a pack expansion but ARG1 is not.*/
21796 /* The normal case, where neither argument is a pack
21798 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21799 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21801 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21802 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21806 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21807 arg2, then arg2 is not as specialized as arg1. */
21813 /* "If, for a given type, deduction succeeds in both directions
21814 (i.e., the types are identical after the transformations above)
21815 and both P and A were reference types (before being replaced with
21816 the type referred to above):
21817 - if the type from the argument template was an lvalue reference and
21818 the type from the parameter template was not, the argument type is
21819 considered to be more specialized than the other; otherwise,
21820 - if the type from the argument template is more cv-qualified
21821 than the type from the parameter template (as described above),
21822 the argument type is considered to be more specialized than the other;
21824 - neither type is more specialized than the other." */
21826 if (deduce1
&& deduce2
)
21828 if (ref1
&& ref2
&& ref1
!= ref2
)
21835 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21837 if ((quals1
& quals2
) == quals2
)
21839 if ((quals1
& quals2
) == quals1
)
21844 if (lose1
&& lose2
)
21845 /* We've failed to deduce something in either direction.
21846 These must be unordered. */
21851 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21852 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21853 /* We have already processed all of the arguments in our
21854 handing of the pack expansion type. */
21857 args1
= TREE_CHAIN (args1
);
21858 args2
= TREE_CHAIN (args2
);
21861 /* "In most cases, all template parameters must have values in order for
21862 deduction to succeed, but for partial ordering purposes a template
21863 parameter may remain without a value provided it is not used in the
21864 types being used for partial ordering."
21866 Thus, if we are missing any of the targs1 we need to substitute into
21867 origs1, then pat2 is not as specialized as pat1. This can happen when
21868 there is a nondeduced context. */
21869 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21871 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21874 processing_template_decl
--;
21876 /* If both deductions succeed, the partial ordering selects the more
21877 constrained template. */
21878 if (!lose1
&& !lose2
)
21880 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21881 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21882 lose1
= !subsumes_constraints (c1
, c2
);
21883 lose2
= !subsumes_constraints (c2
, c1
);
21886 /* All things being equal, if the next argument is a pack expansion
21887 for one function but not for the other, prefer the
21888 non-variadic function. FIXME this is bogus; see c++/41958. */
21890 && args1
&& TREE_VALUE (args1
)
21891 && args2
&& TREE_VALUE (args2
))
21893 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21894 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21897 if (lose1
== lose2
)
21905 /* Determine which of two partial specializations of TMPL is more
21908 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21909 to the first partial specialization. The TREE_PURPOSE is the
21910 innermost set of template parameters for the partial
21911 specialization. PAT2 is similar, but for the second template.
21913 Return 1 if the first partial specialization is more specialized;
21914 -1 if the second is more specialized; 0 if neither is more
21917 See [temp.class.order] for information about determining which of
21918 two templates is more specialized. */
21921 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21925 bool any_deductions
= false;
21927 tree tmpl1
= TREE_VALUE (pat1
);
21928 tree tmpl2
= TREE_VALUE (pat2
);
21929 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21930 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21932 /* Just like what happens for functions, if we are ordering between
21933 different template specializations, we may encounter dependent
21934 types in the arguments, and we need our dependency check functions
21935 to behave correctly. */
21936 ++processing_template_decl
;
21937 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21941 any_deductions
= true;
21944 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21948 any_deductions
= true;
21950 --processing_template_decl
;
21952 /* If both deductions succeed, the partial ordering selects the more
21953 constrained template. */
21954 if (!winner
&& any_deductions
)
21955 return more_constrained (tmpl1
, tmpl2
);
21957 /* In the case of a tie where at least one of the templates
21958 has a parameter pack at the end, the template with the most
21959 non-packed parameters wins. */
21962 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21963 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21965 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21966 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21967 int len1
= TREE_VEC_LENGTH (args1
);
21968 int len2
= TREE_VEC_LENGTH (args2
);
21970 /* We don't count the pack expansion at the end. */
21971 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21973 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21978 else if (len1
< len2
)
21985 /* Return the template arguments that will produce the function signature
21986 DECL from the function template FN, with the explicit template
21987 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21988 also match. Return NULL_TREE if no satisfactory arguments could be
21992 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21994 int ntparms
= DECL_NTPARMS (fn
);
21995 tree targs
= make_tree_vec (ntparms
);
21996 tree decl_type
= TREE_TYPE (decl
);
21997 tree decl_arg_types
;
21999 unsigned int nargs
, ix
;
22002 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22004 /* Never do unification on the 'this' parameter. */
22005 decl_arg_types
= skip_artificial_parms_for (decl
,
22006 TYPE_ARG_TYPES (decl_type
));
22008 nargs
= list_length (decl_arg_types
);
22009 args
= XALLOCAVEC (tree
, nargs
);
22010 for (arg
= decl_arg_types
, ix
= 0;
22011 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22012 arg
= TREE_CHAIN (arg
), ++ix
)
22013 args
[ix
] = TREE_VALUE (arg
);
22015 if (fn_type_unification (fn
, explicit_args
, targs
,
22017 (check_rettype
|| DECL_CONV_FN_P (fn
)
22018 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22019 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
22021 == error_mark_node
)
22027 /* Return the innermost template arguments that, when applied to a partial
22028 specialization SPEC_TMPL of TMPL, yield the ARGS.
22030 For example, suppose we have:
22032 template <class T, class U> struct S {};
22033 template <class T> struct S<T*, int> {};
22035 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22036 partial specialization and the ARGS will be {double*, int}. The resulting
22037 vector will be {double}, indicating that `T' is bound to `double'. */
22040 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22042 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22044 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22045 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22047 tree innermost_deduced_args
;
22049 innermost_deduced_args
= make_tree_vec (ntparms
);
22050 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22052 deduced_args
= copy_node (args
);
22053 SET_TMPL_ARGS_LEVEL (deduced_args
,
22054 TMPL_ARGS_DEPTH (deduced_args
),
22055 innermost_deduced_args
);
22058 deduced_args
= innermost_deduced_args
;
22060 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22062 if (unify (tparms
, deduced_args
,
22063 INNERMOST_TEMPLATE_ARGS (spec_args
),
22064 INNERMOST_TEMPLATE_ARGS (args
),
22065 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22068 for (i
= 0; i
< ntparms
; ++i
)
22069 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22071 if (!tried_array_deduction
)
22073 try_array_deduction (tparms
, innermost_deduced_args
,
22074 INNERMOST_TEMPLATE_ARGS (spec_args
));
22075 tried_array_deduction
= true;
22076 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22082 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22083 if (!push_tinst_level (tinst
))
22085 excessive_deduction_depth
= true;
22089 /* Verify that nondeduced template arguments agree with the type
22090 obtained from argument deduction.
22094 struct A { typedef int X; };
22095 template <class T, class U> struct C {};
22096 template <class T> struct C<T, typename T::X> {};
22098 Then with the instantiation `C<A, int>', we can deduce that
22099 `T' is `A' but unify () does not check whether `typename T::X'
22101 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22103 if (spec_args
!= error_mark_node
)
22104 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22105 INNERMOST_TEMPLATE_ARGS (spec_args
),
22106 tmpl
, tf_none
, false, false);
22108 pop_tinst_level ();
22110 if (spec_args
== error_mark_node
22111 /* We only need to check the innermost arguments; the other
22112 arguments will always agree. */
22113 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22114 INNERMOST_TEMPLATE_ARGS (args
)))
22117 /* Now that we have bindings for all of the template arguments,
22118 ensure that the arguments deduced for the template template
22119 parameters have compatible template parameter lists. See the use
22120 of template_template_parm_bindings_ok_p in fn_type_unification
22121 for more information. */
22122 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22125 return deduced_args
;
22128 // Compare two function templates T1 and T2 by deducing bindings
22129 // from one against the other. If both deductions succeed, compare
22130 // constraints to see which is more constrained.
22132 more_specialized_inst (tree t1
, tree t2
)
22137 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22143 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22149 // If both deductions succeed, then one may be more constrained.
22150 if (count
== 2 && fate
== 0)
22151 fate
= more_constrained (t1
, t2
);
22156 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22157 Return the TREE_LIST node with the most specialized template, if
22158 any. If there is no most specialized template, the error_mark_node
22161 Note that this function does not look at, or modify, the
22162 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22163 returned is one of the elements of INSTANTIATIONS, callers may
22164 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22165 and retrieve it from the value returned. */
22168 most_specialized_instantiation (tree templates
)
22172 ++processing_template_decl
;
22175 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22177 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22178 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22183 /* Equally specialized, move to next function. If there
22184 is no next function, nothing's most specialized. */
22185 fn
= TREE_CHAIN (fn
);
22193 /* Now verify that champ is better than everything earlier in the
22194 instantiation list. */
22195 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22196 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22203 processing_template_decl
--;
22206 return error_mark_node
;
22211 /* If DECL is a specialization of some template, return the most
22212 general such template. Otherwise, returns NULL_TREE.
22214 For example, given:
22216 template <class T> struct S { template <class U> void f(U); };
22218 if TMPL is `template <class U> void S<int>::f(U)' this will return
22219 the full template. This function will not trace past partial
22220 specializations, however. For example, given in addition:
22222 template <class T> struct S<T*> { template <class U> void f(U); };
22224 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22225 `template <class T> template <class U> S<T*>::f(U)'. */
22228 most_general_template (tree decl
)
22230 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22232 if (tree tinfo
= get_template_info (decl
))
22233 decl
= TI_TEMPLATE (tinfo
);
22234 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22235 template friend, or a FIELD_DECL for a capture pack. */
22236 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22240 /* Look for more and more general templates. */
22241 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22243 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22244 (See cp-tree.h for details.) */
22245 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22248 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22249 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22250 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22253 /* Stop if we run into an explicitly specialized class template. */
22254 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22255 && DECL_CONTEXT (decl
)
22256 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22259 decl
= DECL_TI_TEMPLATE (decl
);
22265 /* Return the most specialized of the template partial specializations
22266 which can produce TARGET, a specialization of some class or variable
22267 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22268 a TEMPLATE_DECL node corresponding to the partial specialization, while
22269 the TREE_PURPOSE is the set of template arguments that must be
22270 substituted into the template pattern in order to generate TARGET.
22272 If the choice of partial specialization is ambiguous, a diagnostic
22273 is issued, and the error_mark_node is returned. If there are no
22274 partial specializations matching TARGET, then NULL_TREE is
22275 returned, indicating that the primary template should be used. */
22278 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22280 tree list
= NULL_TREE
;
22285 tree outer_args
= NULL_TREE
;
22288 if (TYPE_P (target
))
22290 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22291 tmpl
= TI_TEMPLATE (tinfo
);
22292 args
= TI_ARGS (tinfo
);
22294 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22296 tmpl
= TREE_OPERAND (target
, 0);
22297 args
= TREE_OPERAND (target
, 1);
22299 else if (VAR_P (target
))
22301 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22302 tmpl
= TI_TEMPLATE (tinfo
);
22303 args
= TI_ARGS (tinfo
);
22306 gcc_unreachable ();
22308 tree main_tmpl
= most_general_template (tmpl
);
22310 /* For determining which partial specialization to use, only the
22311 innermost args are interesting. */
22312 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22314 outer_args
= strip_innermost_template_args (args
, 1);
22315 args
= INNERMOST_TEMPLATE_ARGS (args
);
22318 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22321 tree spec_tmpl
= TREE_VALUE (t
);
22325 /* Substitute in the template args from the enclosing class. */
22326 ++processing_template_decl
;
22327 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22328 --processing_template_decl
;
22331 if (spec_tmpl
== error_mark_node
)
22332 return error_mark_node
;
22334 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22338 spec_args
= add_to_template_args (outer_args
, spec_args
);
22340 /* Keep the candidate only if the constraints are satisfied,
22341 or if we're not compiling with concepts. */
22343 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22345 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22346 TREE_TYPE (list
) = TREE_TYPE (t
);
22354 ambiguous_p
= false;
22357 t
= TREE_CHAIN (t
);
22358 for (; t
; t
= TREE_CHAIN (t
))
22360 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22367 t
= TREE_CHAIN (t
);
22370 ambiguous_p
= true;
22379 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22381 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22384 ambiguous_p
= true;
22392 char *spaces
= NULL
;
22393 if (!(complain
& tf_error
))
22394 return error_mark_node
;
22395 if (TYPE_P (target
))
22396 error ("ambiguous template instantiation for %q#T", target
);
22398 error ("ambiguous template instantiation for %q#D", target
);
22399 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22400 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22402 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22403 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22404 "%s %#qS", spaces
? spaces
: str
, subst
);
22405 spaces
= spaces
? spaces
: get_spaces (str
);
22408 return error_mark_node
;
22414 /* Explicitly instantiate DECL. */
22417 do_decl_instantiation (tree decl
, tree storage
)
22419 tree result
= NULL_TREE
;
22422 if (!decl
|| decl
== error_mark_node
)
22423 /* An error occurred, for which grokdeclarator has already issued
22424 an appropriate message. */
22426 else if (! DECL_LANG_SPECIFIC (decl
))
22428 error ("explicit instantiation of non-template %q#D", decl
);
22432 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22433 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22435 if (VAR_P (decl
) && !var_templ
)
22437 /* There is an asymmetry here in the way VAR_DECLs and
22438 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22439 the latter, the DECL we get back will be marked as a
22440 template instantiation, and the appropriate
22441 DECL_TEMPLATE_INFO will be set up. This does not happen for
22442 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22443 should handle VAR_DECLs as it currently handles
22445 if (!DECL_CLASS_SCOPE_P (decl
))
22447 error ("%qD is not a static data member of a class template", decl
);
22450 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22451 if (!result
|| !VAR_P (result
))
22453 error ("no matching template for %qD found", decl
);
22456 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22458 error ("type %qT for explicit instantiation %qD does not match "
22459 "declared type %qT", TREE_TYPE (result
), decl
,
22464 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22466 error ("explicit instantiation of %q#D", decl
);
22472 /* Check for various error cases. Note that if the explicit
22473 instantiation is valid the RESULT will currently be marked as an
22474 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22475 until we get here. */
22477 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22479 /* DR 259 [temp.spec].
22481 Both an explicit instantiation and a declaration of an explicit
22482 specialization shall not appear in a program unless the explicit
22483 instantiation follows a declaration of the explicit specialization.
22485 For a given set of template parameters, if an explicit
22486 instantiation of a template appears after a declaration of an
22487 explicit specialization for that template, the explicit
22488 instantiation has no effect. */
22491 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22495 No program shall explicitly instantiate any template more
22498 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22499 the first instantiation was `extern' and the second is not,
22500 and EXTERN_P for the opposite case. */
22501 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22502 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22503 /* If an "extern" explicit instantiation follows an ordinary
22504 explicit instantiation, the template is instantiated. */
22508 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22510 error ("no matching template for %qD found", result
);
22513 else if (!DECL_TEMPLATE_INFO (result
))
22515 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22519 if (storage
== NULL_TREE
)
22521 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22523 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22524 pedwarn (input_location
, OPT_Wpedantic
,
22525 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22530 error ("storage class %qD applied to template instantiation", storage
);
22532 check_explicit_instantiation_namespace (result
);
22533 mark_decl_instantiated (result
, extern_p
);
22535 instantiate_decl (result
, /*defer_ok=*/true,
22536 /*expl_inst_class_mem_p=*/false);
22540 mark_class_instantiated (tree t
, int extern_p
)
22542 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22543 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22544 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22545 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22548 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22549 rest_of_type_compilation (t
, 1);
22553 /* Called from do_type_instantiation through binding_table_foreach to
22554 do recursive instantiation for the type bound in ENTRY. */
22556 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22558 tree storage
= *(tree
*) data
;
22560 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22561 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22562 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22565 /* Perform an explicit instantiation of template class T. STORAGE, if
22566 non-null, is the RID for extern, inline or static. COMPLAIN is
22567 nonzero if this is called from the parser, zero if called recursively,
22568 since the standard is unclear (as detailed below). */
22571 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22576 int previous_instantiation_extern_p
= 0;
22578 if (TREE_CODE (t
) == TYPE_DECL
)
22581 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22584 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22586 error ("explicit instantiation of non-class template %qD", tmpl
);
22588 error ("explicit instantiation of non-template type %qT", t
);
22594 if (!COMPLETE_TYPE_P (t
))
22596 if (complain
& tf_error
)
22597 error ("explicit instantiation of %q#T before definition of template",
22602 if (storage
!= NULL_TREE
)
22604 if (!in_system_header_at (input_location
))
22606 if (storage
== ridpointers
[(int) RID_EXTERN
])
22608 if (cxx_dialect
== cxx98
)
22609 pedwarn (input_location
, OPT_Wpedantic
,
22610 "ISO C++ 1998 forbids the use of %<extern%> on "
22611 "explicit instantiations");
22614 pedwarn (input_location
, OPT_Wpedantic
,
22615 "ISO C++ forbids the use of %qE"
22616 " on explicit instantiations", storage
);
22619 if (storage
== ridpointers
[(int) RID_INLINE
])
22621 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22623 else if (storage
== ridpointers
[(int) RID_STATIC
])
22627 error ("storage class %qD applied to template instantiation",
22633 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22635 /* DR 259 [temp.spec].
22637 Both an explicit instantiation and a declaration of an explicit
22638 specialization shall not appear in a program unless the explicit
22639 instantiation follows a declaration of the explicit specialization.
22641 For a given set of template parameters, if an explicit
22642 instantiation of a template appears after a declaration of an
22643 explicit specialization for that template, the explicit
22644 instantiation has no effect. */
22647 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22651 No program shall explicitly instantiate any template more
22654 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22655 instantiation was `extern'. If EXTERN_P then the second is.
22656 These cases are OK. */
22657 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22659 if (!previous_instantiation_extern_p
&& !extern_p
22660 && (complain
& tf_error
))
22661 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22663 /* If we've already instantiated the template, just return now. */
22664 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22668 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22669 mark_class_instantiated (t
, extern_p
);
22674 /* In contrast to implicit instantiation, where only the
22675 declarations, and not the definitions, of members are
22676 instantiated, we have here:
22680 The explicit instantiation of a class template specialization
22681 implies the instantiation of all of its members not
22682 previously explicitly specialized in the translation unit
22683 containing the explicit instantiation.
22685 Of course, we can't instantiate member template classes, since we
22686 don't have any arguments for them. Note that the standard is
22687 unclear on whether the instantiation of the members are
22688 *explicit* instantiations or not. However, the most natural
22689 interpretation is that it should be an explicit
22691 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22693 || (TREE_CODE (fld
) == FUNCTION_DECL
22695 && user_provided_p (fld
)))
22696 && DECL_TEMPLATE_INSTANTIATION (fld
))
22698 mark_decl_instantiated (fld
, extern_p
);
22700 instantiate_decl (fld
, /*defer_ok=*/true,
22701 /*expl_inst_class_mem_p=*/true);
22704 if (CLASSTYPE_NESTED_UTDS (t
))
22705 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22706 bt_instantiate_type_proc
, &storage
);
22709 /* Given a function DECL, which is a specialization of TMPL, modify
22710 DECL to be a re-instantiation of TMPL with the same template
22711 arguments. TMPL should be the template into which tsubst'ing
22712 should occur for DECL, not the most general template.
22714 One reason for doing this is a scenario like this:
22717 void f(const T&, int i);
22719 void g() { f(3, 7); }
22722 void f(const T& t, const int i) { }
22724 Note that when the template is first instantiated, with
22725 instantiate_template, the resulting DECL will have no name for the
22726 first parameter, and the wrong type for the second. So, when we go
22727 to instantiate the DECL, we regenerate it. */
22730 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22732 /* The arguments used to instantiate DECL, from the most general
22736 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22738 /* Make sure that we can see identifiers, and compute access
22740 push_access_scope (decl
);
22742 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22750 args_depth
= TMPL_ARGS_DEPTH (args
);
22751 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22752 if (args_depth
> parms_depth
)
22753 args
= get_innermost_template_args (args
, parms_depth
);
22755 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22756 args
, tf_error
, NULL_TREE
,
22757 /*defer_ok*/false);
22758 if (specs
&& specs
!= error_mark_node
)
22759 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22762 /* Merge parameter declarations. */
22763 decl_parm
= skip_artificial_parms_for (decl
,
22764 DECL_ARGUMENTS (decl
));
22766 = skip_artificial_parms_for (code_pattern
,
22767 DECL_ARGUMENTS (code_pattern
));
22768 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22773 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22774 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22775 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22777 parm_type
= type_decays_to (parm_type
);
22778 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22779 TREE_TYPE (decl_parm
) = parm_type
;
22780 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22781 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22783 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22784 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22786 decl_parm
= DECL_CHAIN (decl_parm
);
22787 pattern_parm
= DECL_CHAIN (pattern_parm
);
22789 /* Merge any parameters that match with the function parameter
22791 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22794 tree expanded_types
;
22795 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22796 the parameters in this function parameter pack. */
22797 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22798 args
, tf_error
, NULL_TREE
);
22799 len
= TREE_VEC_LENGTH (expanded_types
);
22800 for (i
= 0; i
< len
; i
++)
22805 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22806 /* Rename the parameter to include the index. */
22807 DECL_NAME (decl_parm
) =
22808 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22809 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22810 parm_type
= type_decays_to (parm_type
);
22811 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22812 TREE_TYPE (decl_parm
) = parm_type
;
22813 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22814 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22816 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22817 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22819 decl_parm
= DECL_CHAIN (decl_parm
);
22822 /* Merge additional specifiers from the CODE_PATTERN. */
22823 if (DECL_DECLARED_INLINE_P (code_pattern
)
22824 && !DECL_DECLARED_INLINE_P (decl
))
22825 DECL_DECLARED_INLINE_P (decl
) = 1;
22827 else if (VAR_P (decl
))
22829 start_lambda_scope (decl
);
22830 DECL_INITIAL (decl
) =
22831 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22832 tf_error
, DECL_TI_TEMPLATE (decl
),
22833 /*integral_constant_expression_p=*/false);
22834 finish_lambda_scope ();
22835 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22836 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22837 tf_error
, DECL_TI_TEMPLATE (decl
));
22840 gcc_unreachable ();
22842 pop_access_scope (decl
);
22845 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22846 substituted to get DECL. */
22849 template_for_substitution (tree decl
)
22851 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22853 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22854 for the instantiation. This is not always the most general
22855 template. Consider, for example:
22858 struct S { template <class U> void f();
22859 template <> void f<int>(); };
22861 and an instantiation of S<double>::f<int>. We want TD to be the
22862 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22863 while (/* An instantiation cannot have a definition, so we need a
22864 more general template. */
22865 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22866 /* We must also deal with friend templates. Given:
22868 template <class T> struct S {
22869 template <class U> friend void f() {};
22872 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22873 so far as the language is concerned, but that's still
22874 where we get the pattern for the instantiation from. On
22875 other hand, if the definition comes outside the class, say:
22877 template <class T> struct S {
22878 template <class U> friend void f();
22880 template <class U> friend void f() {}
22882 we don't need to look any further. That's what the check for
22883 DECL_INITIAL is for. */
22884 || (TREE_CODE (decl
) == FUNCTION_DECL
22885 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22886 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22888 /* The present template, TD, should not be a definition. If it
22889 were a definition, we should be using it! Note that we
22890 cannot restructure the loop to just keep going until we find
22891 a template with a definition, since that might go too far if
22892 a specialization was declared, but not defined. */
22894 /* Fetch the more general template. */
22895 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22901 /* Returns true if we need to instantiate this template instance even if we
22902 know we aren't going to emit it. */
22905 always_instantiate_p (tree decl
)
22907 /* We always instantiate inline functions so that we can inline them. An
22908 explicit instantiation declaration prohibits implicit instantiation of
22909 non-inline functions. With high levels of optimization, we would
22910 normally inline non-inline functions -- but we're not allowed to do
22911 that for "extern template" functions. Therefore, we check
22912 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22913 return ((TREE_CODE (decl
) == FUNCTION_DECL
22914 && (DECL_DECLARED_INLINE_P (decl
)
22915 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22916 /* And we need to instantiate static data members so that
22917 their initializers are available in integral constant
22920 && decl_maybe_constant_var_p (decl
)));
22923 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22924 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22925 error, true otherwise. */
22928 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22930 tree fntype
, spec
, noex
, clone
;
22932 /* Don't instantiate a noexcept-specification from template context. */
22933 if (processing_template_decl
)
22936 if (DECL_CLONED_FUNCTION_P (fn
))
22937 fn
= DECL_CLONED_FUNCTION (fn
);
22938 fntype
= TREE_TYPE (fn
);
22939 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22941 if (!spec
|| !TREE_PURPOSE (spec
))
22944 noex
= TREE_PURPOSE (spec
);
22946 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22948 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22949 bool added
= false;
22950 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22951 spec
= get_defaulted_eh_spec (fn
, complain
);
22952 else if (!(added
= !fns
->add (fn
)))
22954 /* If hash_set::add returns true, the element was already there. */
22955 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22956 DECL_SOURCE_LOCATION (fn
));
22958 "exception specification of %qD depends on itself",
22960 spec
= noexcept_false_spec
;
22962 else if (push_tinst_level (fn
))
22964 push_access_scope (fn
);
22965 push_deferring_access_checks (dk_no_deferred
);
22966 input_location
= DECL_SOURCE_LOCATION (fn
);
22967 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22968 DEFERRED_NOEXCEPT_ARGS (noex
),
22969 tf_warning_or_error
, fn
,
22970 /*function_p=*/false,
22971 /*integral_constant_expression_p=*/true);
22972 pop_deferring_access_checks ();
22973 pop_access_scope (fn
);
22974 pop_tinst_level ();
22975 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22976 if (spec
== error_mark_node
)
22977 spec
= noexcept_false_spec
;
22980 spec
= noexcept_false_spec
;
22985 if (spec
== error_mark_node
)
22988 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22991 FOR_EACH_CLONE (clone
, fn
)
22993 if (TREE_TYPE (clone
) == fntype
)
22994 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22996 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23002 /* We're starting to process the function INST, an instantiation of PATTERN;
23003 add their parameters to local_specializations. */
23006 register_parameter_specializations (tree pattern
, tree inst
)
23008 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23009 tree spec_parm
= DECL_ARGUMENTS (inst
);
23010 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23012 register_local_specialization (spec_parm
, tmpl_parm
);
23013 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23014 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23016 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23018 if (!DECL_PACK_P (tmpl_parm
))
23020 register_local_specialization (spec_parm
, tmpl_parm
);
23021 spec_parm
= DECL_CHAIN (spec_parm
);
23025 /* Register the (value) argument pack as a specialization of
23026 TMPL_PARM, then move on. */
23027 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23028 register_local_specialization (argpack
, tmpl_parm
);
23031 gcc_assert (!spec_parm
);
23034 /* Produce the definition of D, a _DECL generated from a template. If
23035 DEFER_OK is true, then we don't have to actually do the
23036 instantiation now; we just have to do it sometime. Normally it is
23037 an error if this is an explicit instantiation but D is undefined.
23038 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23039 instantiated class template. */
23042 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23044 tree tmpl
= DECL_TI_TEMPLATE (d
);
23051 bool pattern_defined
;
23052 location_t saved_loc
= input_location
;
23053 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23054 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23058 /* This function should only be used to instantiate templates for
23059 functions and static member variables. */
23060 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23062 /* A concept is never instantiated. */
23063 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23065 /* Variables are never deferred; if instantiation is required, they
23066 are instantiated right away. That allows for better code in the
23067 case that an expression refers to the value of the variable --
23068 if the variable has a constant value the referring expression can
23069 take advantage of that fact. */
23073 /* Don't instantiate cloned functions. Instead, instantiate the
23074 functions they cloned. */
23075 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23076 d
= DECL_CLONED_FUNCTION (d
);
23078 if (DECL_TEMPLATE_INSTANTIATED (d
)
23079 || (TREE_CODE (d
) == FUNCTION_DECL
23080 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23081 || DECL_TEMPLATE_SPECIALIZATION (d
))
23082 /* D has already been instantiated or explicitly specialized, so
23083 there's nothing for us to do here.
23085 It might seem reasonable to check whether or not D is an explicit
23086 instantiation, and, if so, stop here. But when an explicit
23087 instantiation is deferred until the end of the compilation,
23088 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23089 the instantiation. */
23092 /* Check to see whether we know that this template will be
23093 instantiated in some other file, as with "extern template"
23095 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23097 /* In general, we do not instantiate such templates. */
23098 if (external_p
&& !always_instantiate_p (d
))
23101 gen_tmpl
= most_general_template (tmpl
);
23102 gen_args
= DECL_TI_ARGS (d
);
23104 if (tmpl
!= gen_tmpl
)
23105 /* We should already have the extra args. */
23106 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23107 == TMPL_ARGS_DEPTH (gen_args
));
23108 /* And what's in the hash table should match D. */
23109 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23110 || spec
== NULL_TREE
);
23112 /* This needs to happen before any tsubsting. */
23113 if (! push_tinst_level (d
))
23116 timevar_push (TV_TEMPLATE_INST
);
23118 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23119 for the instantiation. */
23120 td
= template_for_substitution (d
);
23125 /* Look up an explicit specialization, if any. */
23126 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23127 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23128 if (elt
&& elt
!= error_mark_node
)
23130 td
= TREE_VALUE (elt
);
23131 args
= TREE_PURPOSE (elt
);
23135 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23137 /* We should never be trying to instantiate a member of a class
23138 template or partial specialization. */
23139 gcc_assert (d
!= code_pattern
);
23141 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23142 || DECL_TEMPLATE_SPECIALIZATION (td
))
23143 /* In the case of a friend template whose definition is provided
23144 outside the class, we may have too many arguments. Drop the
23145 ones we don't need. The same is true for specializations. */
23146 args
= get_innermost_template_args
23147 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23149 if (TREE_CODE (d
) == FUNCTION_DECL
)
23151 deleted_p
= DECL_DELETED_FN (code_pattern
);
23152 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23153 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23154 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23160 if (DECL_CLASS_SCOPE_P (code_pattern
))
23161 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23162 || DECL_INLINE_VAR_P (code_pattern
));
23164 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23167 /* We may be in the middle of deferred access check. Disable it now. */
23168 push_deferring_access_checks (dk_no_deferred
);
23170 /* Unless an explicit instantiation directive has already determined
23171 the linkage of D, remember that a definition is available for
23173 if (pattern_defined
23174 && !DECL_INTERFACE_KNOWN (d
)
23175 && !DECL_NOT_REALLY_EXTERN (d
))
23176 mark_definable (d
);
23178 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23179 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23180 input_location
= DECL_SOURCE_LOCATION (d
);
23182 /* If D is a member of an explicitly instantiated class template,
23183 and no definition is available, treat it like an implicit
23185 if (!pattern_defined
&& expl_inst_class_mem_p
23186 && DECL_EXPLICIT_INSTANTIATION (d
))
23188 /* Leave linkage flags alone on instantiations with anonymous
23190 if (TREE_PUBLIC (d
))
23192 DECL_NOT_REALLY_EXTERN (d
) = 0;
23193 DECL_INTERFACE_KNOWN (d
) = 0;
23195 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23198 /* Defer all other templates, unless we have been explicitly
23199 forbidden from doing so. */
23200 if (/* If there is no definition, we cannot instantiate the
23203 /* If it's OK to postpone instantiation, do so. */
23205 /* If this is a static data member that will be defined
23206 elsewhere, we don't want to instantiate the entire data
23207 member, but we do want to instantiate the initializer so that
23208 we can substitute that elsewhere. */
23209 || (external_p
&& VAR_P (d
))
23210 /* Handle here a deleted function too, avoid generating
23211 its body (c++/61080). */
23214 /* The definition of the static data member is now required so
23215 we must substitute the initializer. */
23217 && !DECL_INITIAL (d
)
23218 && DECL_INITIAL (code_pattern
))
23222 bool const_init
= false;
23223 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23225 ns
= decl_namespace_context (d
);
23226 push_nested_namespace (ns
);
23228 push_nested_class (DECL_CONTEXT (d
));
23229 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23231 tf_warning_or_error
, NULL_TREE
,
23232 /*integral_constant_expression_p=*/false);
23233 /* If instantiating the initializer involved instantiating this
23234 again, don't call cp_finish_decl twice. */
23235 if (!DECL_INITIAL (d
))
23237 /* Make sure the initializer is still constant, in case of
23238 circular dependency (template/instantiate6.C). */
23240 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23241 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23242 /*asmspec_tree=*/NULL_TREE
,
23243 LOOKUP_ONLYCONVERTING
);
23246 pop_nested_class ();
23247 pop_nested_namespace (ns
);
23250 /* We restore the source position here because it's used by
23251 add_pending_template. */
23252 input_location
= saved_loc
;
23254 if (at_eof
&& !pattern_defined
23255 && DECL_EXPLICIT_INSTANTIATION (d
)
23256 && DECL_NOT_REALLY_EXTERN (d
))
23259 The definition of a non-exported function template, a
23260 non-exported member function template, or a non-exported
23261 member function or static data member of a class template
23262 shall be present in every translation unit in which it is
23263 explicitly instantiated. */
23264 permerror (input_location
, "explicit instantiation of %qD "
23265 "but no definition available", d
);
23267 /* If we're in unevaluated context, we just wanted to get the
23268 constant value; this isn't an odr use, so don't queue
23269 a full instantiation. */
23270 if (cp_unevaluated_operand
!= 0)
23272 /* ??? Historically, we have instantiated inline functions, even
23273 when marked as "extern template". */
23274 if (!(external_p
&& VAR_P (d
)))
23275 add_pending_template (d
);
23278 /* Tell the repository that D is available in this translation unit
23279 -- and see if it is supposed to be instantiated here. */
23280 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23282 /* In a PCH file, despite the fact that the repository hasn't
23283 requested instantiation in the PCH it is still possible that
23284 an instantiation will be required in a file that includes the
23287 add_pending_template (d
);
23288 /* Instantiate inline functions so that the inliner can do its
23289 job, even though we'll not be emitting a copy of this
23291 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23295 bool push_to_top
, nested
;
23297 fn_context
= decl_function_context (d
);
23298 nested
= current_function_decl
!= NULL_TREE
;
23299 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23301 vec
<tree
> omp_privatization_save
;
23303 save_omp_privatization_clauses (omp_privatization_save
);
23306 push_to_top_level ();
23309 push_function_context ();
23310 cp_unevaluated_operand
= 0;
23311 c_inhibit_evaluation_warnings
= 0;
23314 /* Mark D as instantiated so that recursive calls to
23315 instantiate_decl do not try to instantiate it again. */
23316 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23318 /* Regenerate the declaration in case the template has been modified
23319 by a subsequent redeclaration. */
23320 regenerate_decl_from_template (d
, td
, args
);
23322 /* We already set the file and line above. Reset them now in case
23323 they changed as a result of calling regenerate_decl_from_template. */
23324 input_location
= DECL_SOURCE_LOCATION (d
);
23329 bool const_init
= false;
23331 /* Clear out DECL_RTL; whatever was there before may not be right
23332 since we've reset the type of the declaration. */
23333 SET_DECL_RTL (d
, NULL
);
23334 DECL_IN_AGGR_P (d
) = 0;
23336 /* The initializer is placed in DECL_INITIAL by
23337 regenerate_decl_from_template so we don't need to
23338 push/pop_access_scope again here. Pull it out so that
23339 cp_finish_decl can process it. */
23340 init
= DECL_INITIAL (d
);
23341 DECL_INITIAL (d
) = NULL_TREE
;
23342 DECL_INITIALIZED_P (d
) = 0;
23344 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23345 initializer. That function will defer actual emission until
23346 we have a chance to determine linkage. */
23347 DECL_EXTERNAL (d
) = 0;
23349 /* Enter the scope of D so that access-checking works correctly. */
23350 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23352 push_nested_class (DECL_CONTEXT (d
));
23354 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23355 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23358 pop_nested_class ();
23360 if (variable_template_p (gen_tmpl
))
23361 note_variable_template_instantiation (d
);
23363 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23364 synthesize_method (d
);
23365 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23367 /* Set up the list of local specializations. */
23368 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23369 tree block
= NULL_TREE
;
23371 /* Set up context. */
23372 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23373 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23374 block
= push_stmt_list ();
23376 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23378 /* Some typedefs referenced from within the template code need to be
23379 access checked at template instantiation time, i.e now. These
23380 types were added to the template at parsing time. Let's get those
23381 and perform the access checks then. */
23382 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23385 /* Create substitution entries for the parameters. */
23386 register_parameter_specializations (code_pattern
, d
);
23388 /* Substitute into the body of the function. */
23389 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23390 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23391 tf_warning_or_error
, tmpl
);
23394 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23395 tf_warning_or_error
, tmpl
,
23396 /*integral_constant_expression_p=*/false);
23398 /* Set the current input_location to the end of the function
23399 so that finish_function knows where we are. */
23401 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23403 /* Remember if we saw an infinite loop in the template. */
23404 current_function_infinite_loop
23405 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23408 /* Finish the function. */
23409 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23410 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23411 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23414 d
= finish_function (/*inline_p=*/false);
23415 expand_or_defer_fn (d
);
23418 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23419 cp_check_omp_declare_reduction (d
);
23422 /* We're not deferring instantiation any more. */
23423 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23426 pop_from_top_level ();
23428 pop_function_context ();
23431 restore_omp_privatization_clauses (omp_privatization_save
);
23434 pop_deferring_access_checks ();
23435 timevar_pop (TV_TEMPLATE_INST
);
23436 pop_tinst_level ();
23437 input_location
= saved_loc
;
23438 cp_unevaluated_operand
= saved_unevaluated_operand
;
23439 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23444 /* Run through the list of templates that we wish we could
23445 instantiate, and instantiate any we can. RETRIES is the
23446 number of times we retry pending template instantiation. */
23449 instantiate_pending_templates (int retries
)
23452 location_t saved_loc
= input_location
;
23454 /* Instantiating templates may trigger vtable generation. This in turn
23455 may require further template instantiations. We place a limit here
23456 to avoid infinite loop. */
23457 if (pending_templates
&& retries
>= max_tinst_depth
)
23459 tree decl
= pending_templates
->tinst
->decl
;
23461 fatal_error (input_location
,
23462 "template instantiation depth exceeds maximum of %d"
23463 " instantiating %q+D, possibly from virtual table generation"
23464 " (use -ftemplate-depth= to increase the maximum)",
23465 max_tinst_depth
, decl
);
23466 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23467 /* Pretend that we defined it. */
23468 DECL_INITIAL (decl
) = error_mark_node
;
23474 struct pending_template
**t
= &pending_templates
;
23475 struct pending_template
*last
= NULL
;
23479 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23480 bool complete
= false;
23482 if (TYPE_P (instantiation
))
23484 if (!COMPLETE_TYPE_P (instantiation
))
23486 instantiate_class_template (instantiation
);
23487 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23488 for (tree fld
= TYPE_FIELDS (instantiation
);
23489 fld
; fld
= TREE_CHAIN (fld
))
23491 || (TREE_CODE (fld
) == FUNCTION_DECL
23492 && !DECL_ARTIFICIAL (fld
)))
23493 && DECL_TEMPLATE_INSTANTIATION (fld
))
23494 instantiate_decl (fld
,
23495 /*defer_ok=*/false,
23496 /*expl_inst_class_mem_p=*/false);
23498 if (COMPLETE_TYPE_P (instantiation
))
23502 complete
= COMPLETE_TYPE_P (instantiation
);
23506 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23507 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23510 = instantiate_decl (instantiation
,
23511 /*defer_ok=*/false,
23512 /*expl_inst_class_mem_p=*/false);
23513 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23517 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23518 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23522 /* If INSTANTIATION has been instantiated, then we don't
23523 need to consider it again in the future. */
23531 current_tinst_level
= NULL
;
23533 last_pending_template
= last
;
23535 while (reconsider
);
23537 input_location
= saved_loc
;
23540 /* Substitute ARGVEC into T, which is a list of initializers for
23541 either base class or a non-static data member. The TREE_PURPOSEs
23542 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23543 instantiate_decl. */
23546 tsubst_initializer_list (tree t
, tree argvec
)
23548 tree inits
= NULL_TREE
;
23550 for (; t
; t
= TREE_CHAIN (t
))
23554 tree expanded_bases
= NULL_TREE
;
23555 tree expanded_arguments
= NULL_TREE
;
23558 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23563 /* Expand the base class expansion type into separate base
23565 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23566 tf_warning_or_error
,
23568 if (expanded_bases
== error_mark_node
)
23571 /* We'll be building separate TREE_LISTs of arguments for
23573 len
= TREE_VEC_LENGTH (expanded_bases
);
23574 expanded_arguments
= make_tree_vec (len
);
23575 for (i
= 0; i
< len
; i
++)
23576 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23578 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23579 expand each argument in the TREE_VALUE of t. */
23580 expr
= make_node (EXPR_PACK_EXPANSION
);
23581 PACK_EXPANSION_LOCAL_P (expr
) = true;
23582 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23583 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23585 if (TREE_VALUE (t
) == void_type_node
)
23586 /* VOID_TYPE_NODE is used to indicate
23587 value-initialization. */
23589 for (i
= 0; i
< len
; i
++)
23590 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23594 /* Substitute parameter packs into each argument in the
23596 in_base_initializer
= 1;
23597 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23599 tree expanded_exprs
;
23601 /* Expand the argument. */
23602 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23604 = tsubst_pack_expansion (expr
, argvec
,
23605 tf_warning_or_error
,
23607 if (expanded_exprs
== error_mark_node
)
23610 /* Prepend each of the expanded expressions to the
23611 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23612 for (i
= 0; i
< len
; i
++)
23614 TREE_VEC_ELT (expanded_arguments
, i
) =
23615 tree_cons (NULL_TREE
,
23616 TREE_VEC_ELT (expanded_exprs
, i
),
23617 TREE_VEC_ELT (expanded_arguments
, i
));
23620 in_base_initializer
= 0;
23622 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23623 since we built them backwards. */
23624 for (i
= 0; i
< len
; i
++)
23626 TREE_VEC_ELT (expanded_arguments
, i
) =
23627 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23632 for (i
= 0; i
< len
; ++i
)
23634 if (expanded_bases
)
23636 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23637 decl
= expand_member_init (decl
);
23638 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23643 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23644 tf_warning_or_error
, NULL_TREE
);
23646 decl
= expand_member_init (decl
);
23647 if (decl
&& !DECL_P (decl
))
23648 in_base_initializer
= 1;
23650 init
= TREE_VALUE (t
);
23652 if (init
!= void_type_node
)
23653 init
= tsubst_expr (init
, argvec
,
23654 tf_warning_or_error
, NULL_TREE
,
23655 /*integral_constant_expression_p=*/false);
23656 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23657 /* If we had an initializer but it instantiated to nothing,
23658 value-initialize the object. This will only occur when
23659 the initializer was a pack expansion where the parameter
23660 packs used in that expansion were of length zero. */
23661 init
= void_type_node
;
23662 in_base_initializer
= 0;
23667 init
= build_tree_list (decl
, init
);
23668 TREE_CHAIN (init
) = inits
;
23676 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23679 set_current_access_from_decl (tree decl
)
23681 if (TREE_PRIVATE (decl
))
23682 current_access_specifier
= access_private_node
;
23683 else if (TREE_PROTECTED (decl
))
23684 current_access_specifier
= access_protected_node
;
23686 current_access_specifier
= access_public_node
;
23689 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23690 is the instantiation (which should have been created with
23691 start_enum) and ARGS are the template arguments to use. */
23694 tsubst_enum (tree tag
, tree newtag
, tree args
)
23698 if (SCOPED_ENUM_P (newtag
))
23699 begin_scope (sk_scoped_enum
, newtag
);
23701 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23706 decl
= TREE_VALUE (e
);
23707 /* Note that in a template enum, the TREE_VALUE is the
23708 CONST_DECL, not the corresponding INTEGER_CST. */
23709 value
= tsubst_expr (DECL_INITIAL (decl
),
23710 args
, tf_warning_or_error
, NULL_TREE
,
23711 /*integral_constant_expression_p=*/true);
23713 /* Give this enumeration constant the correct access. */
23714 set_current_access_from_decl (decl
);
23716 /* Actually build the enumerator itself. Here we're assuming that
23717 enumerators can't have dependent attributes. */
23718 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23719 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23722 if (SCOPED_ENUM_P (newtag
))
23725 finish_enum_value_list (newtag
);
23726 finish_enum (newtag
);
23728 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23729 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23732 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23733 its type -- but without substituting the innermost set of template
23734 arguments. So, innermost set of template parameters will appear in
23738 get_mostly_instantiated_function_type (tree decl
)
23740 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23741 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23744 /* Return truthvalue if we're processing a template different from
23745 the last one involved in diagnostics. */
23747 problematic_instantiation_changed (void)
23749 return current_tinst_level
!= last_error_tinst_level
;
23752 /* Remember current template involved in diagnostics. */
23754 record_last_problematic_instantiation (void)
23756 last_error_tinst_level
= current_tinst_level
;
23759 struct tinst_level
*
23760 current_instantiation (void)
23762 return current_tinst_level
;
23765 /* Return TRUE if current_function_decl is being instantiated, false
23769 instantiating_current_function_p (void)
23771 return (current_instantiation ()
23772 && current_instantiation ()->decl
== current_function_decl
);
23775 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23776 type. Return false for ok, true for disallowed. Issue error and
23777 inform messages under control of COMPLAIN. */
23780 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23782 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23784 else if (POINTER_TYPE_P (type
))
23786 else if (TYPE_PTRMEM_P (type
))
23788 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23790 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23792 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23794 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23796 /* A bound template template parm could later be instantiated to have a valid
23797 nontype parm type via an alias template. */
23798 else if (cxx_dialect
>= cxx11
23799 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23802 if (complain
& tf_error
)
23804 if (type
== error_mark_node
)
23805 inform (input_location
, "invalid template non-type parameter");
23807 error ("%q#T is not a valid type for a template non-type parameter",
23813 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23814 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23817 dependent_type_p_r (tree type
)
23823 A type is dependent if it is:
23825 -- a template parameter. Template template parameters are types
23826 for us (since TYPE_P holds true for them) so we handle
23828 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23829 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23831 /* -- a qualified-id with a nested-name-specifier which contains a
23832 class-name that names a dependent type or whose unqualified-id
23833 names a dependent type. */
23834 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23837 /* An alias template specialization can be dependent even if the
23838 resulting type is not. */
23839 if (dependent_alias_template_spec_p (type
))
23842 /* -- a cv-qualified type where the cv-unqualified type is
23844 No code is necessary for this bullet; the code below handles
23845 cv-qualified types, and we don't want to strip aliases with
23846 TYPE_MAIN_VARIANT because of DR 1558. */
23847 /* -- a compound type constructed from any dependent type. */
23848 if (TYPE_PTRMEM_P (type
))
23849 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23850 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23852 else if (TYPE_PTR_P (type
)
23853 || TREE_CODE (type
) == REFERENCE_TYPE
)
23854 return dependent_type_p (TREE_TYPE (type
));
23855 else if (TREE_CODE (type
) == FUNCTION_TYPE
23856 || TREE_CODE (type
) == METHOD_TYPE
)
23860 if (dependent_type_p (TREE_TYPE (type
)))
23862 for (arg_type
= TYPE_ARG_TYPES (type
);
23864 arg_type
= TREE_CHAIN (arg_type
))
23865 if (dependent_type_p (TREE_VALUE (arg_type
)))
23867 if (cxx_dialect
>= cxx17
)
23868 /* A value-dependent noexcept-specifier makes the type dependent. */
23869 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
23870 if (tree noex
= TREE_PURPOSE (spec
))
23871 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23872 affect overload resolution and treating it as dependent breaks
23874 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
23875 && value_dependent_expression_p (noex
))
23879 /* -- an array type constructed from any dependent type or whose
23880 size is specified by a constant expression that is
23883 We checked for type- and value-dependence of the bounds in
23884 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23885 if (TREE_CODE (type
) == ARRAY_TYPE
)
23887 if (TYPE_DOMAIN (type
)
23888 && dependent_type_p (TYPE_DOMAIN (type
)))
23890 return dependent_type_p (TREE_TYPE (type
));
23893 /* -- a template-id in which either the template name is a template
23895 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23897 /* ... or any of the template arguments is a dependent type or
23898 an expression that is type-dependent or value-dependent. */
23899 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23900 && (any_dependent_template_arguments_p
23901 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23904 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23905 dependent; if the argument of the `typeof' expression is not
23906 type-dependent, then it should already been have resolved. */
23907 if (TREE_CODE (type
) == TYPEOF_TYPE
23908 || TREE_CODE (type
) == DECLTYPE_TYPE
23909 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23912 /* A template argument pack is dependent if any of its packed
23914 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23916 tree args
= ARGUMENT_PACK_ARGS (type
);
23917 int i
, len
= TREE_VEC_LENGTH (args
);
23918 for (i
= 0; i
< len
; ++i
)
23919 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23923 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23924 be template parameters. */
23925 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23928 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23931 /* The standard does not specifically mention types that are local
23932 to template functions or local classes, but they should be
23933 considered dependent too. For example:
23935 template <int I> void f() {
23940 The size of `E' cannot be known until the value of `I' has been
23941 determined. Therefore, `E' must be considered dependent. */
23942 scope
= TYPE_CONTEXT (type
);
23943 if (scope
&& TYPE_P (scope
))
23944 return dependent_type_p (scope
);
23945 /* Don't use type_dependent_expression_p here, as it can lead
23946 to infinite recursion trying to determine whether a lambda
23947 nested in a lambda is dependent (c++/47687). */
23948 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23949 && DECL_LANG_SPECIFIC (scope
)
23950 && DECL_TEMPLATE_INFO (scope
)
23951 && (any_dependent_template_arguments_p
23952 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23955 /* Other types are non-dependent. */
23959 /* Returns TRUE if TYPE is dependent, in the sense of
23960 [temp.dep.type]. Note that a NULL type is considered dependent. */
23963 dependent_type_p (tree type
)
23965 /* If there are no template parameters in scope, then there can't be
23966 any dependent types. */
23967 if (!processing_template_decl
)
23969 /* If we are not processing a template, then nobody should be
23970 providing us with a dependent type. */
23972 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23976 /* If the type is NULL, we have not computed a type for the entity
23977 in question; in that case, the type is dependent. */
23981 /* Erroneous types can be considered non-dependent. */
23982 if (type
== error_mark_node
)
23985 /* Getting here with global_type_node means we improperly called this
23986 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23987 gcc_checking_assert (type
!= global_type_node
);
23989 /* If we have not already computed the appropriate value for TYPE,
23991 if (!TYPE_DEPENDENT_P_VALID (type
))
23993 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23994 TYPE_DEPENDENT_P_VALID (type
) = 1;
23997 return TYPE_DEPENDENT_P (type
);
24000 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24001 lookup. In other words, a dependent type that is not the current
24005 dependent_scope_p (tree scope
)
24007 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24008 && !currently_open_class (scope
));
24011 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24012 an unknown base of 'this' (and is therefore instantiation-dependent). */
24015 unknown_base_ref_p (tree t
)
24017 if (!current_class_ptr
)
24020 tree mem
= TREE_OPERAND (t
, 1);
24021 if (shared_member_p (mem
))
24024 tree cur
= current_nonlambda_class_type ();
24025 if (!any_dependent_bases_p (cur
))
24028 tree ctx
= TREE_OPERAND (t
, 0);
24029 if (DERIVED_FROM_P (ctx
, cur
))
24035 /* T is a SCOPE_REF; return whether we need to consider it
24036 instantiation-dependent so that we can check access at instantiation
24037 time even though we know which member it resolves to. */
24040 instantiation_dependent_scope_ref_p (tree t
)
24042 if (DECL_P (TREE_OPERAND (t
, 1))
24043 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24044 && !unknown_base_ref_p (t
)
24045 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24046 TREE_OPERAND (t
, 1)))
24052 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24053 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24056 /* Note that this predicate is not appropriate for general expressions;
24057 only constant expressions (that satisfy potential_constant_expression)
24058 can be tested for value dependence. */
24061 value_dependent_expression_p (tree expression
)
24063 if (!processing_template_decl
|| expression
== NULL_TREE
)
24066 /* A type-dependent expression is also value-dependent. */
24067 if (type_dependent_expression_p (expression
))
24070 switch (TREE_CODE (expression
))
24073 /* A dependent member function of the current instantiation. */
24074 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24076 case FUNCTION_DECL
:
24077 /* A dependent member function of the current instantiation. */
24078 if (DECL_CLASS_SCOPE_P (expression
)
24079 && dependent_type_p (DECL_CONTEXT (expression
)))
24083 case IDENTIFIER_NODE
:
24084 /* A name that has not been looked up -- must be dependent. */
24087 case TEMPLATE_PARM_INDEX
:
24088 /* A non-type template parm. */
24092 /* A non-type template parm. */
24093 if (DECL_TEMPLATE_PARM_P (expression
))
24095 return value_dependent_expression_p (DECL_INITIAL (expression
));
24098 /* A constant with literal type and is initialized
24099 with an expression that is value-dependent. */
24100 if (DECL_DEPENDENT_INIT_P (expression
)
24101 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24102 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24104 if (DECL_HAS_VALUE_EXPR_P (expression
))
24106 tree value_expr
= DECL_VALUE_EXPR (expression
);
24107 if (value_dependent_expression_p (value_expr
))
24112 case DYNAMIC_CAST_EXPR
:
24113 case STATIC_CAST_EXPR
:
24114 case CONST_CAST_EXPR
:
24115 case REINTERPRET_CAST_EXPR
:
24117 case IMPLICIT_CONV_EXPR
:
24118 /* These expressions are value-dependent if the type to which
24119 the cast occurs is dependent or the expression being casted
24120 is value-dependent. */
24122 tree type
= TREE_TYPE (expression
);
24124 if (dependent_type_p (type
))
24127 /* A functional cast has a list of operands. */
24128 expression
= TREE_OPERAND (expression
, 0);
24131 /* If there are no operands, it must be an expression such
24132 as "int()". This should not happen for aggregate types
24133 because it would form non-constant expressions. */
24134 gcc_assert (cxx_dialect
>= cxx11
24135 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24140 if (TREE_CODE (expression
) == TREE_LIST
)
24141 return any_value_dependent_elements_p (expression
);
24143 return value_dependent_expression_p (expression
);
24147 if (SIZEOF_EXPR_TYPE_P (expression
))
24148 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24152 /* A `sizeof' expression is value-dependent if the operand is
24153 type-dependent or is a pack expansion. */
24154 expression
= TREE_OPERAND (expression
, 0);
24155 if (PACK_EXPANSION_P (expression
))
24157 else if (TYPE_P (expression
))
24158 return dependent_type_p (expression
);
24159 return instantiation_dependent_uneval_expression_p (expression
);
24161 case AT_ENCODE_EXPR
:
24162 /* An 'encode' expression is value-dependent if the operand is
24164 expression
= TREE_OPERAND (expression
, 0);
24165 return dependent_type_p (expression
);
24167 case NOEXCEPT_EXPR
:
24168 expression
= TREE_OPERAND (expression
, 0);
24169 return instantiation_dependent_uneval_expression_p (expression
);
24172 /* All instantiation-dependent expressions should also be considered
24173 value-dependent. */
24174 return instantiation_dependent_scope_ref_p (expression
);
24176 case COMPONENT_REF
:
24177 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24178 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24180 case NONTYPE_ARGUMENT_PACK
:
24181 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24182 is value-dependent. */
24184 tree values
= ARGUMENT_PACK_ARGS (expression
);
24185 int i
, len
= TREE_VEC_LENGTH (values
);
24187 for (i
= 0; i
< len
; ++i
)
24188 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24196 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24198 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24204 if (TREE_CODE (type2
) != TREE_LIST
)
24205 return dependent_type_p (type2
);
24207 for (; type2
; type2
= TREE_CHAIN (type2
))
24208 if (dependent_type_p (TREE_VALUE (type2
)))
24215 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24216 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24219 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24220 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24224 tree op
= TREE_OPERAND (expression
, 0);
24225 return (value_dependent_expression_p (op
)
24226 || has_value_dependent_address (op
));
24229 case REQUIRES_EXPR
:
24230 /* Treat all requires-expressions as value-dependent so
24231 we don't try to fold them. */
24235 return dependent_type_p (TREE_OPERAND (expression
, 0));
24239 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24241 tree fn
= get_callee_fndecl (expression
);
24243 nargs
= call_expr_nargs (expression
);
24244 for (i
= 0; i
< nargs
; ++i
)
24246 tree op
= CALL_EXPR_ARG (expression
, i
);
24247 /* In a call to a constexpr member function, look through the
24248 implicit ADDR_EXPR on the object argument so that it doesn't
24249 cause the call to be considered value-dependent. We also
24250 look through it in potential_constant_expression. */
24251 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24252 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24253 && TREE_CODE (op
) == ADDR_EXPR
)
24254 op
= TREE_OPERAND (op
, 0);
24255 if (value_dependent_expression_p (op
))
24261 case TEMPLATE_ID_EXPR
:
24262 return variable_concept_p (TREE_OPERAND (expression
, 0));
24268 if (dependent_type_p (TREE_TYPE (expression
)))
24270 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24271 if (value_dependent_expression_p (val
))
24277 /* Treat a GNU statement expression as dependent to avoid crashing
24278 under instantiate_non_dependent_expr; it can't be constant. */
24282 /* A constant expression is value-dependent if any subexpression is
24283 value-dependent. */
24284 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24286 case tcc_reference
:
24288 case tcc_comparison
:
24290 case tcc_expression
:
24293 int i
, len
= cp_tree_operand_length (expression
);
24295 for (i
= 0; i
< len
; i
++)
24297 tree t
= TREE_OPERAND (expression
, i
);
24299 /* In some cases, some of the operands may be missing.
24300 (For example, in the case of PREDECREMENT_EXPR, the
24301 amount to increment by may be missing.) That doesn't
24302 make the expression dependent. */
24303 if (t
&& value_dependent_expression_p (t
))
24314 /* The expression is not value-dependent. */
24318 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24319 [temp.dep.expr]. Note that an expression with no type is
24320 considered dependent. Other parts of the compiler arrange for an
24321 expression with type-dependent subexpressions to have no type, so
24322 this function doesn't have to be fully recursive. */
24325 type_dependent_expression_p (tree expression
)
24327 if (!processing_template_decl
)
24330 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24333 STRIP_ANY_LOCATION_WRAPPER (expression
);
24335 /* An unresolved name is always dependent. */
24336 if (identifier_p (expression
)
24337 || TREE_CODE (expression
) == USING_DECL
24338 || TREE_CODE (expression
) == WILDCARD_DECL
)
24341 /* A fold expression is type-dependent. */
24342 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24343 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24344 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24345 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24348 /* Some expression forms are never type-dependent. */
24349 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24350 || TREE_CODE (expression
) == SIZEOF_EXPR
24351 || TREE_CODE (expression
) == ALIGNOF_EXPR
24352 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24353 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24354 || TREE_CODE (expression
) == TRAIT_EXPR
24355 || TREE_CODE (expression
) == TYPEID_EXPR
24356 || TREE_CODE (expression
) == DELETE_EXPR
24357 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24358 || TREE_CODE (expression
) == THROW_EXPR
24359 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24362 /* The types of these expressions depends only on the type to which
24363 the cast occurs. */
24364 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24365 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24366 || TREE_CODE (expression
) == CONST_CAST_EXPR
24367 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24368 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24369 || TREE_CODE (expression
) == CAST_EXPR
)
24370 return dependent_type_p (TREE_TYPE (expression
));
24372 /* The types of these expressions depends only on the type created
24373 by the expression. */
24374 if (TREE_CODE (expression
) == NEW_EXPR
24375 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24377 /* For NEW_EXPR tree nodes created inside a template, either
24378 the object type itself or a TREE_LIST may appear as the
24380 tree type
= TREE_OPERAND (expression
, 1);
24381 if (TREE_CODE (type
) == TREE_LIST
)
24382 /* This is an array type. We need to check array dimensions
24384 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24385 || value_dependent_expression_p
24386 (TREE_OPERAND (TREE_VALUE (type
), 1));
24388 return dependent_type_p (type
);
24391 if (TREE_CODE (expression
) == SCOPE_REF
)
24393 tree scope
= TREE_OPERAND (expression
, 0);
24394 tree name
= TREE_OPERAND (expression
, 1);
24396 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24397 contains an identifier associated by name lookup with one or more
24398 declarations declared with a dependent type, or...a
24399 nested-name-specifier or qualified-id that names a member of an
24400 unknown specialization. */
24401 return (type_dependent_expression_p (name
)
24402 || dependent_scope_p (scope
));
24405 if (TREE_CODE (expression
) == TEMPLATE_DECL
24406 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24407 return uses_outer_template_parms (expression
);
24409 if (TREE_CODE (expression
) == STMT_EXPR
)
24410 expression
= stmt_expr_value_expr (expression
);
24412 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24417 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24419 if (type_dependent_expression_p (elt
))
24425 /* A static data member of the current instantiation with incomplete
24426 array type is type-dependent, as the definition and specializations
24427 can have different bounds. */
24428 if (VAR_P (expression
)
24429 && DECL_CLASS_SCOPE_P (expression
)
24430 && dependent_type_p (DECL_CONTEXT (expression
))
24431 && VAR_HAD_UNKNOWN_BOUND (expression
))
24434 /* An array of unknown bound depending on a variadic parameter, eg:
24436 template<typename... Args>
24437 void foo (Args... args)
24439 int arr[] = { args... };
24442 template<int... vals>
24445 int arr[] = { vals... };
24448 If the array has no length and has an initializer, it must be that
24449 we couldn't determine its length in cp_complete_array_type because
24450 it is dependent. */
24451 if (VAR_P (expression
)
24452 && TREE_TYPE (expression
) != NULL_TREE
24453 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24454 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24455 && DECL_INITIAL (expression
))
24458 /* A function or variable template-id is type-dependent if it has any
24459 dependent template arguments. */
24460 if (VAR_OR_FUNCTION_DECL_P (expression
)
24461 && DECL_LANG_SPECIFIC (expression
)
24462 && DECL_TEMPLATE_INFO (expression
))
24464 /* Consider the innermost template arguments, since those are the ones
24465 that come from the template-id; the template arguments for the
24466 enclosing class do not make it type-dependent unless they are used in
24467 the type of the decl. */
24468 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24469 && (any_dependent_template_arguments_p
24470 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24474 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24475 type-dependent. Checking this is important for functions with auto return
24476 type, which looks like a dependent type. */
24477 if (TREE_CODE (expression
) == FUNCTION_DECL
24478 && !(DECL_CLASS_SCOPE_P (expression
)
24479 && dependent_type_p (DECL_CONTEXT (expression
)))
24480 && !(DECL_FRIEND_P (expression
)
24481 && (!DECL_FRIEND_CONTEXT (expression
)
24482 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24483 && !DECL_LOCAL_FUNCTION_P (expression
))
24485 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24486 || undeduced_auto_decl (expression
));
24490 /* Always dependent, on the number of arguments if nothing else. */
24491 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24494 if (TREE_TYPE (expression
) == unknown_type_node
)
24496 if (TREE_CODE (expression
) == ADDR_EXPR
)
24497 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24498 if (TREE_CODE (expression
) == COMPONENT_REF
24499 || TREE_CODE (expression
) == OFFSET_REF
)
24501 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24503 expression
= TREE_OPERAND (expression
, 1);
24504 if (identifier_p (expression
))
24507 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24508 if (TREE_CODE (expression
) == SCOPE_REF
)
24511 if (BASELINK_P (expression
))
24513 if (BASELINK_OPTYPE (expression
)
24514 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24516 expression
= BASELINK_FUNCTIONS (expression
);
24519 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24521 if (any_dependent_template_arguments_p
24522 (TREE_OPERAND (expression
, 1)))
24524 expression
= TREE_OPERAND (expression
, 0);
24525 if (identifier_p (expression
))
24529 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24530 || TREE_CODE (expression
) == FUNCTION_DECL
);
24532 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24533 if (type_dependent_expression_p (*iter
))
24539 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24541 /* Dependent type attributes might not have made it from the decl to
24543 if (DECL_P (expression
)
24544 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24547 return (dependent_type_p (TREE_TYPE (expression
)));
24550 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24551 type-dependent if the expression refers to a member of the current
24552 instantiation and the type of the referenced member is dependent, or the
24553 class member access expression refers to a member of an unknown
24556 This function returns true if the OBJECT in such a class member access
24557 expression is of an unknown specialization. */
24560 type_dependent_object_expression_p (tree object
)
24562 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24564 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24566 tree scope
= TREE_TYPE (object
);
24567 return (!scope
|| dependent_scope_p (scope
));
24570 /* walk_tree callback function for instantiation_dependent_expression_p,
24571 below. Returns non-zero if a dependent subexpression is found. */
24574 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24579 /* We don't have to worry about decltype currently because decltype
24580 of an instantiation-dependent expr is a dependent type. This
24581 might change depending on the resolution of DR 1172. */
24582 *walk_subtrees
= false;
24585 enum tree_code code
= TREE_CODE (*tp
);
24588 /* Don't treat an argument list as dependent just because it has no
24594 case TEMPLATE_PARM_INDEX
:
24597 /* Handle expressions with type operands. */
24601 case AT_ENCODE_EXPR
:
24603 tree op
= TREE_OPERAND (*tp
, 0);
24604 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24605 op
= TREE_TYPE (op
);
24608 if (dependent_type_p (op
))
24612 *walk_subtrees
= false;
24619 case COMPONENT_REF
:
24620 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24621 /* In a template, finish_class_member_access_expr creates a
24622 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24623 type-dependent, so that we can check access control at
24624 instantiation time (PR 42277). See also Core issue 1273. */
24629 if (instantiation_dependent_scope_ref_p (*tp
))
24634 /* Treat statement-expressions as dependent. */
24638 /* Treat requires-expressions as dependent. */
24639 case REQUIRES_EXPR
:
24643 /* Treat calls to function concepts as dependent. */
24644 if (function_concept_check_p (*tp
))
24648 case TEMPLATE_ID_EXPR
:
24649 /* And variable concepts. */
24650 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24658 if (type_dependent_expression_p (*tp
))
24664 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24665 sense defined by the ABI:
24667 "An expression is instantiation-dependent if it is type-dependent
24668 or value-dependent, or it has a subexpression that is type-dependent
24669 or value-dependent."
24671 Except don't actually check value-dependence for unevaluated expressions,
24672 because in sizeof(i) we don't care about the value of i. Checking
24673 type-dependence will in turn check value-dependence of array bounds/template
24674 arguments as needed. */
24677 instantiation_dependent_uneval_expression_p (tree expression
)
24681 if (!processing_template_decl
)
24684 if (expression
== error_mark_node
)
24687 result
= cp_walk_tree_without_duplicates (&expression
,
24688 instantiation_dependent_r
, NULL
);
24689 return result
!= NULL_TREE
;
24692 /* As above, but also check value-dependence of the expression as a whole. */
24695 instantiation_dependent_expression_p (tree expression
)
24697 return (instantiation_dependent_uneval_expression_p (expression
)
24698 || value_dependent_expression_p (expression
));
24701 /* Like type_dependent_expression_p, but it also works while not processing
24702 a template definition, i.e. during substitution or mangling. */
24705 type_dependent_expression_p_push (tree expr
)
24708 ++processing_template_decl
;
24709 b
= type_dependent_expression_p (expr
);
24710 --processing_template_decl
;
24714 /* Returns TRUE if ARGS contains a type-dependent expression. */
24717 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24722 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24724 if (type_dependent_expression_p (arg
))
24730 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24731 expressions) contains any type-dependent expressions. */
24734 any_type_dependent_elements_p (const_tree list
)
24736 for (; list
; list
= TREE_CHAIN (list
))
24737 if (type_dependent_expression_p (TREE_VALUE (list
)))
24743 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24744 expressions) contains any value-dependent expressions. */
24747 any_value_dependent_elements_p (const_tree list
)
24749 for (; list
; list
= TREE_CHAIN (list
))
24750 if (value_dependent_expression_p (TREE_VALUE (list
)))
24756 /* Returns TRUE if the ARG (a template argument) is dependent. */
24759 dependent_template_arg_p (tree arg
)
24761 if (!processing_template_decl
)
24764 /* Assume a template argument that was wrongly written by the user
24765 is dependent. This is consistent with what
24766 any_dependent_template_arguments_p [that calls this function]
24768 if (!arg
|| arg
== error_mark_node
)
24771 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24772 arg
= argument_pack_select_arg (arg
);
24774 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24776 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24778 if (DECL_TEMPLATE_PARM_P (arg
))
24780 /* A member template of a dependent class is not necessarily
24781 type-dependent, but it is a dependent template argument because it
24782 will be a member of an unknown specialization to that template. */
24783 tree scope
= CP_DECL_CONTEXT (arg
);
24784 return TYPE_P (scope
) && dependent_type_p (scope
);
24786 else if (ARGUMENT_PACK_P (arg
))
24788 tree args
= ARGUMENT_PACK_ARGS (arg
);
24789 int i
, len
= TREE_VEC_LENGTH (args
);
24790 for (i
= 0; i
< len
; ++i
)
24792 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24798 else if (TYPE_P (arg
))
24799 return dependent_type_p (arg
);
24801 return (type_dependent_expression_p (arg
)
24802 || value_dependent_expression_p (arg
));
24805 /* Returns true if ARGS (a collection of template arguments) contains
24806 any types that require structural equality testing. */
24809 any_template_arguments_need_structural_equality_p (tree args
)
24816 if (args
== error_mark_node
)
24819 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24821 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24822 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24824 tree arg
= TREE_VEC_ELT (level
, j
);
24825 tree packed_args
= NULL_TREE
;
24828 if (ARGUMENT_PACK_P (arg
))
24830 /* Look inside the argument pack. */
24831 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24832 len
= TREE_VEC_LENGTH (packed_args
);
24835 for (k
= 0; k
< len
; ++k
)
24838 arg
= TREE_VEC_ELT (packed_args
, k
);
24840 if (error_operand_p (arg
))
24842 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24844 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24846 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24847 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24856 /* Returns true if ARGS (a collection of template arguments) contains
24857 any dependent arguments. */
24860 any_dependent_template_arguments_p (const_tree args
)
24867 if (args
== error_mark_node
)
24870 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24872 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24873 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24874 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24881 /* Returns TRUE if the template TMPL is type-dependent. */
24884 dependent_template_p (tree tmpl
)
24886 if (TREE_CODE (tmpl
) == OVERLOAD
)
24888 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24889 if (dependent_template_p (*iter
))
24894 /* Template template parameters are dependent. */
24895 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24896 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24898 /* So are names that have not been looked up. */
24899 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24904 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24907 dependent_template_id_p (tree tmpl
, tree args
)
24909 return (dependent_template_p (tmpl
)
24910 || any_dependent_template_arguments_p (args
));
24913 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24917 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24921 if (!processing_template_decl
)
24924 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24926 tree decl
= TREE_VEC_ELT (declv
, i
);
24927 tree init
= TREE_VEC_ELT (initv
, i
);
24928 tree cond
= TREE_VEC_ELT (condv
, i
);
24929 tree incr
= TREE_VEC_ELT (incrv
, i
);
24931 if (type_dependent_expression_p (decl
)
24932 || TREE_CODE (decl
) == SCOPE_REF
)
24935 if (init
&& type_dependent_expression_p (init
))
24938 if (type_dependent_expression_p (cond
))
24941 if (COMPARISON_CLASS_P (cond
)
24942 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24943 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24946 if (TREE_CODE (incr
) == MODOP_EXPR
)
24948 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24949 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24952 else if (type_dependent_expression_p (incr
))
24954 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24956 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24958 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24960 tree t
= TREE_OPERAND (incr
, 1);
24961 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24962 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24971 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24972 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24973 no such TYPE can be found. Note that this function peers inside
24974 uninstantiated templates and therefore should be used only in
24975 extremely limited situations. ONLY_CURRENT_P restricts this
24976 peering to the currently open classes hierarchy (which is required
24977 when comparing types). */
24980 resolve_typename_type (tree type
, bool only_current_p
)
24989 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24991 scope
= TYPE_CONTEXT (type
);
24992 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24993 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24994 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24995 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24996 identifier of the TYPENAME_TYPE anymore.
24997 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24998 TYPENAME_TYPE instead, we avoid messing up with a possible
24999 typedef variant case. */
25000 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25002 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25003 it first before we can figure out what NAME refers to. */
25004 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25006 if (TYPENAME_IS_RESOLVING_P (scope
))
25007 /* Given a class template A with a dependent base with nested type C,
25008 typedef typename A::C::C C will land us here, as trying to resolve
25009 the initial A::C leads to the local C typedef, which leads back to
25010 A::C::C. So we break the recursion now. */
25013 scope
= resolve_typename_type (scope
, only_current_p
);
25015 /* If we don't know what SCOPE refers to, then we cannot resolve the
25017 if (!CLASS_TYPE_P (scope
))
25019 /* If this is a typedef, we don't want to look inside (c++/11987). */
25020 if (typedef_variant_p (type
))
25022 /* If SCOPE isn't the template itself, it will not have a valid
25023 TYPE_FIELDS list. */
25024 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25025 /* scope is either the template itself or a compatible instantiation
25026 like X<T>, so look up the name in the original template. */
25027 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25028 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25029 gcc_checking_assert (uses_template_parms (scope
));
25030 /* If scope has no fields, it can't be a current instantiation. Check this
25031 before currently_open_class to avoid infinite recursion (71515). */
25032 if (!TYPE_FIELDS (scope
))
25034 /* If the SCOPE is not the current instantiation, there's no reason
25035 to look inside it. */
25036 if (only_current_p
&& !currently_open_class (scope
))
25038 /* Enter the SCOPE so that name lookup will be resolved as if we
25039 were in the class definition. In particular, SCOPE will no
25040 longer be considered a dependent type. */
25041 pushed_scope
= push_scope (scope
);
25042 /* Look up the declaration. */
25043 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25044 tf_warning_or_error
);
25046 result
= NULL_TREE
;
25048 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25049 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25050 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25053 else if (identifier_p (fullname
)
25054 && TREE_CODE (decl
) == TYPE_DECL
)
25056 result
= TREE_TYPE (decl
);
25057 if (result
== error_mark_node
)
25058 result
= NULL_TREE
;
25060 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25061 && DECL_CLASS_TEMPLATE_P (decl
))
25063 /* Obtain the template and the arguments. */
25064 tree tmpl
= TREE_OPERAND (fullname
, 0);
25065 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25067 /* We get here with a plain identifier because a previous tentative
25068 parse of the nested-name-specifier as part of a ptr-operator saw
25069 ::template X<A>. The use of ::template is necessary in a
25070 ptr-operator, but wrong in a declarator-id.
25072 [temp.names]: In a qualified-id of a declarator-id, the keyword
25073 template shall not appear at the top level. */
25074 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25075 "keyword %<template%> not allowed in declarator-id");
25078 tree args
= TREE_OPERAND (fullname
, 1);
25079 /* Instantiate the template. */
25080 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25081 /*entering_scope=*/true,
25082 tf_error
| tf_user
);
25083 if (result
== error_mark_node
)
25084 result
= NULL_TREE
;
25087 /* Leave the SCOPE. */
25089 pop_scope (pushed_scope
);
25091 /* If we failed to resolve it, return the original typename. */
25095 /* If lookup found a typename type, resolve that too. */
25096 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25098 /* Ill-formed programs can cause infinite recursion here, so we
25099 must catch that. */
25100 TYPENAME_IS_RESOLVING_P (result
) = 1;
25101 result
= resolve_typename_type (result
, only_current_p
);
25102 TYPENAME_IS_RESOLVING_P (result
) = 0;
25105 /* Qualify the resulting type. */
25106 quals
= cp_type_quals (type
);
25108 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25113 /* EXPR is an expression which is not type-dependent. Return a proxy
25114 for EXPR that can be used to compute the types of larger
25115 expressions containing EXPR. */
25118 build_non_dependent_expr (tree expr
)
25120 tree orig_expr
= expr
;
25123 /* When checking, try to get a constant value for all non-dependent
25124 expressions in order to expose bugs in *_dependent_expression_p
25125 and constexpr. This can affect code generation, see PR70704, so
25126 only do this for -fchecking=2. */
25127 if (flag_checking
> 1
25128 && cxx_dialect
>= cxx11
25129 /* Don't do this during nsdmi parsing as it can lead to
25130 unexpected recursive instantiations. */
25131 && !parsing_nsdmi ()
25132 /* Don't do this during concept expansion either and for
25133 the same reason. */
25134 && !expanding_concept ())
25135 fold_non_dependent_expr (expr
);
25137 STRIP_ANY_LOCATION_WRAPPER (expr
);
25139 /* Preserve OVERLOADs; the functions must be available to resolve
25142 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25143 inner_expr
= stmt_expr_value_expr (inner_expr
);
25144 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25145 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25146 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25147 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25148 if (is_overloaded_fn (inner_expr
)
25149 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25151 /* There is no need to return a proxy for a variable. */
25154 /* Preserve string constants; conversions from string constants to
25155 "char *" are allowed, even though normally a "const char *"
25156 cannot be used to initialize a "char *". */
25157 if (TREE_CODE (expr
) == STRING_CST
)
25159 /* Preserve void and arithmetic constants, as an optimization -- there is no
25160 reason to create a new node. */
25161 if (TREE_CODE (expr
) == VOID_CST
25162 || TREE_CODE (expr
) == INTEGER_CST
25163 || TREE_CODE (expr
) == REAL_CST
)
25165 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25166 There is at least one place where we want to know that a
25167 particular expression is a throw-expression: when checking a ?:
25168 expression, there are special rules if the second or third
25169 argument is a throw-expression. */
25170 if (TREE_CODE (expr
) == THROW_EXPR
)
25173 /* Don't wrap an initializer list, we need to be able to look inside. */
25174 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25177 /* Don't wrap a dummy object, we need to be able to test for it. */
25178 if (is_dummy_object (expr
))
25181 if (TREE_CODE (expr
) == COND_EXPR
)
25182 return build3 (COND_EXPR
,
25184 TREE_OPERAND (expr
, 0),
25185 (TREE_OPERAND (expr
, 1)
25186 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25187 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25188 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25189 if (TREE_CODE (expr
) == COMPOUND_EXPR
25190 && !COMPOUND_EXPR_OVERLOADED (expr
))
25191 return build2 (COMPOUND_EXPR
,
25193 TREE_OPERAND (expr
, 0),
25194 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25196 /* If the type is unknown, it can't really be non-dependent */
25197 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25199 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25200 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25203 /* ARGS is a vector of expressions as arguments to a function call.
25204 Replace the arguments with equivalent non-dependent expressions.
25205 This modifies ARGS in place. */
25208 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25213 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25215 tree newarg
= build_non_dependent_expr (arg
);
25217 (*args
)[ix
] = newarg
;
25221 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25222 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25223 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25226 make_auto_1 (tree name
, bool set_canonical
)
25228 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25229 TYPE_NAME (au
) = build_decl (input_location
,
25230 TYPE_DECL
, name
, au
);
25231 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25232 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25233 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25234 TYPE_NAME (au
), NULL_TREE
);
25236 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25237 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25238 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25244 make_decltype_auto (void)
25246 return make_auto_1 (decltype_auto_identifier
, true);
25252 return make_auto_1 (auto_identifier
, true);
25255 /* Return a C++17 deduction placeholder for class template TMPL. */
25258 make_template_placeholder (tree tmpl
)
25260 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25261 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25265 /* True iff T is a C++17 class template deduction placeholder. */
25268 template_placeholder_p (tree t
)
25270 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25273 /* Make a "constrained auto" type-specifier. This is an
25274 auto type with constraints that must be associated after
25275 deduction. The constraint is formed from the given
25276 CONC and its optional sequence of arguments, which are
25277 non-null if written as partial-concept-id. */
25280 make_constrained_auto (tree con
, tree args
)
25282 tree type
= make_auto_1 (auto_identifier
, false);
25284 /* Build the constraint. */
25285 tree tmpl
= DECL_TI_TEMPLATE (con
);
25286 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25287 expr
= build_concept_check (expr
, type
, args
);
25289 tree constr
= normalize_expression (expr
);
25290 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25292 /* Our canonical type depends on the constraint. */
25293 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25295 /* Attach the constraint to the type declaration. */
25296 tree decl
= TYPE_NAME (type
);
25300 /* Given type ARG, return std::initializer_list<ARG>. */
25305 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25307 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25309 gcc_rich_location
richloc (input_location
);
25310 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25311 error_at (&richloc
,
25312 "deducing from brace-enclosed initializer list"
25313 " requires %<#include <initializer_list>%>");
25315 return error_mark_node
;
25317 tree argvec
= make_tree_vec (1);
25318 TREE_VEC_ELT (argvec
, 0) = arg
;
25320 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25321 NULL_TREE
, 0, tf_warning_or_error
);
25324 /* Replace auto in TYPE with std::initializer_list<auto>. */
25327 listify_autos (tree type
, tree auto_node
)
25329 tree init_auto
= listify (auto_node
);
25330 tree argvec
= make_tree_vec (1);
25331 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25332 if (processing_template_decl
)
25333 argvec
= add_to_template_args (current_template_args (), argvec
);
25334 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25337 /* Hash traits for hashing possibly constrained 'auto'
25338 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25340 struct auto_hash
: default_hash_traits
<tree
>
25342 static inline hashval_t
hash (tree
);
25343 static inline bool equal (tree
, tree
);
25346 /* Hash the 'auto' T. */
25349 auto_hash::hash (tree t
)
25351 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25352 /* Matching constrained-type-specifiers denote the same template
25353 parameter, so hash the constraint. */
25354 return hash_placeholder_constraint (c
);
25356 /* But unconstrained autos are all separate, so just hash the pointer. */
25357 return iterative_hash_object (t
, 0);
25360 /* Compare two 'auto's. */
25363 auto_hash::equal (tree t1
, tree t2
)
25368 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25369 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25371 /* Two unconstrained autos are distinct. */
25375 return equivalent_placeholder_constraints (c1
, c2
);
25378 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25379 constrained) auto, add it to the vector. */
25382 extract_autos_r (tree t
, void *data
)
25384 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25387 /* All the autos were built with index 0; fix that up now. */
25388 tree
*p
= hash
.find_slot (t
, INSERT
);
25391 /* If this is a repeated constrained-type-specifier, use the index we
25393 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25396 /* Otherwise this is new, so use the current count. */
25398 idx
= hash
.elements () - 1;
25400 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25403 /* Always keep walking. */
25407 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25408 says they can appear anywhere in the type. */
25411 extract_autos (tree type
)
25413 hash_set
<tree
> visited
;
25414 hash_table
<auto_hash
> hash (2);
25416 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25418 tree tree_vec
= make_tree_vec (hash
.elements());
25419 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25420 iter
!= hash
.end(); ++iter
)
25423 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25424 TREE_VEC_ELT (tree_vec
, i
)
25425 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25431 /* The stem for deduction guide names. */
25432 const char *const dguide_base
= "__dguide_";
25434 /* Return the name for a deduction guide for class template TMPL. */
25437 dguide_name (tree tmpl
)
25439 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25440 tree tname
= TYPE_IDENTIFIER (type
);
25441 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25442 + IDENTIFIER_LENGTH (tname
));
25443 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25444 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25445 IDENTIFIER_LENGTH (tname
) + 1);
25446 tree dname
= get_identifier (buf
);
25447 TREE_TYPE (dname
) = type
;
25451 /* True if NAME is the name of a deduction guide. */
25454 dguide_name_p (tree name
)
25456 return (TREE_TYPE (name
)
25457 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25458 strlen (dguide_base
)));
25461 /* True if FN is a deduction guide. */
25464 deduction_guide_p (const_tree fn
)
25467 if (tree name
= DECL_NAME (fn
))
25468 return dguide_name_p (name
);
25472 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25475 copy_guide_p (const_tree fn
)
25477 gcc_assert (deduction_guide_p (fn
));
25478 if (!DECL_ARTIFICIAL (fn
))
25480 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25481 return (TREE_CHAIN (parms
) == void_list_node
25482 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25485 /* True if FN is a guide generated from a constructor template. */
25488 template_guide_p (const_tree fn
)
25490 gcc_assert (deduction_guide_p (fn
));
25491 if (!DECL_ARTIFICIAL (fn
))
25493 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25494 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25495 return PRIMARY_TEMPLATE_P (org
);
25499 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25500 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25501 template parameter types. Note that the handling of template template
25502 parameters relies on current_template_parms being set appropriately for the
25506 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25507 tree tsubst_args
, tsubst_flags_t complain
)
25509 tree oldidx
= get_template_parm_index (olddecl
);
25512 if (TREE_CODE (olddecl
) == TYPE_DECL
25513 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25515 tree oldtype
= TREE_TYPE (olddecl
);
25516 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25517 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25518 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25519 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25520 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25523 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25524 complain
, NULL_TREE
);
25527 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25528 DECL_NAME (olddecl
), newtype
);
25529 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25532 if (TREE_CODE (olddecl
) == TYPE_DECL
25533 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25535 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25536 = build_template_parm_index (index
, level
, level
,
25538 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25539 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25540 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25541 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25543 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25545 DECL_TEMPLATE_RESULT (newdecl
)
25546 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25547 DECL_NAME (olddecl
), newtype
);
25548 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25549 // First create a copy (ttargs) of tsubst_args with an
25550 // additional level for the template template parameter's own
25551 // template parameters (ttparms).
25552 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25553 (DECL_TEMPLATE_PARMS (olddecl
)));
25554 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25555 tree ttargs
= make_tree_vec (depth
+ 1);
25556 for (int i
= 0; i
< depth
; ++i
)
25557 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25558 TREE_VEC_ELT (ttargs
, depth
)
25559 = template_parms_level_to_args (ttparms
);
25560 // Substitute ttargs into ttparms to fix references to
25561 // other template parameters.
25562 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25564 // Now substitute again with args based on tparms, to reduce
25565 // the level of the ttparms.
25566 ttargs
= current_template_args ();
25567 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25569 // Finally, tack the adjusted parms onto tparms.
25570 ttparms
= tree_cons (size_int (depth
), ttparms
,
25571 current_template_parms
);
25572 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25577 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25579 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25580 TREE_CODE (oldconst
),
25581 DECL_NAME (oldconst
), newtype
);
25582 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25583 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25584 SET_DECL_TEMPLATE_PARM_P (newconst
);
25585 newidx
= build_template_parm_index (index
, level
, level
,
25586 newconst
, newtype
);
25587 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25588 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25589 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25595 /* Returns a C++17 class deduction guide template based on the constructor
25596 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25597 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25600 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25602 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25603 bool memtmpl
= false;
25606 tree fn_tmpl
= NULL_TREE
;
25611 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25614 type
= TREE_TYPE (type
);
25615 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25618 fparms
= void_list_node
;
25620 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25621 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25622 targs
= CLASSTYPE_TI_ARGS (type
);
25625 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25626 explicit_p
= false;
25630 ++processing_template_decl
;
25633 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25634 : DECL_TI_TEMPLATE (ctor
));
25636 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25637 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25639 type
= DECL_CONTEXT (ctor
);
25641 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25642 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25643 fully specialized args for the enclosing class. Strip those off, as
25644 the deduction guide won't have those template parameters. */
25645 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25646 TMPL_PARMS_DEPTH (tparms
));
25647 /* Discard the 'this' parameter. */
25648 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25649 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25650 ci
= get_constraints (ctor
);
25651 loc
= DECL_SOURCE_LOCATION (ctor
);
25652 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25654 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25658 /* For a member template constructor, we need to flatten the two
25659 template parameter lists into one, and then adjust the function
25660 signature accordingly. This gets...complicated. */
25661 tree save_parms
= current_template_parms
;
25663 /* For a member template we should have two levels of parms/args, one
25664 for the class and one for the constructor. We stripped
25665 specialized args for further enclosing classes above. */
25666 const int depth
= 2;
25667 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25669 /* Template args for translating references to the two-level template
25670 parameters into references to the one-level template parameters we
25672 tree tsubst_args
= copy_node (targs
);
25673 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25674 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25676 /* Template parms for the constructor template. */
25677 tree ftparms
= TREE_VALUE (tparms
);
25678 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25679 /* Template parms for the class template. */
25680 tparms
= TREE_CHAIN (tparms
);
25681 tree ctparms
= TREE_VALUE (tparms
);
25682 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25683 /* Template parms for the deduction guide start as a copy of the
25684 template parms for the class. We set current_template_parms for
25685 lookup_template_class_1. */
25686 current_template_parms
= tparms
= copy_node (tparms
);
25687 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25688 for (unsigned i
= 0; i
< clen
; ++i
)
25689 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25691 /* Now we need to rewrite the constructor parms to append them to the
25693 for (unsigned i
= 0; i
< flen
; ++i
)
25695 unsigned index
= i
+ clen
;
25696 unsigned level
= 1;
25697 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25698 tree olddecl
= TREE_VALUE (oldelt
);
25699 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25700 tsubst_args
, complain
);
25701 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25702 tsubst_args
, complain
, ctor
);
25703 tree list
= build_tree_list (newdef
, newdecl
);
25704 TEMPLATE_PARM_CONSTRAINTS (list
)
25705 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25706 tsubst_args
, complain
, ctor
);
25707 TREE_VEC_ELT (new_vec
, index
) = list
;
25708 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25711 /* Now we have a final set of template parms to substitute into the
25712 function signature. */
25713 targs
= template_parms_to_args (tparms
);
25714 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25716 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25718 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25720 current_template_parms
= save_parms
;
25722 --processing_template_decl
;
25727 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25728 tparms
= copy_node (tparms
);
25729 INNERMOST_TEMPLATE_PARMS (tparms
)
25730 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25733 tree fntype
= build_function_type (type
, fparms
);
25734 tree ded_fn
= build_lang_decl_loc (loc
,
25736 dguide_name (type
), fntype
);
25737 DECL_ARGUMENTS (ded_fn
) = fargs
;
25738 DECL_ARTIFICIAL (ded_fn
) = true;
25739 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25740 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25741 DECL_ARTIFICIAL (ded_tmpl
) = true;
25742 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25743 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25744 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25745 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25747 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25749 set_constraints (ded_tmpl
, ci
);
25754 /* Deduce template arguments for the class template placeholder PTYPE for
25755 template TMPL based on the initializer INIT, and return the resulting
25759 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25760 tsubst_flags_t complain
)
25762 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25764 /* We should have handled this in the caller. */
25765 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25767 if (complain
& tf_error
)
25768 error ("non-class template %qT used without template arguments", tmpl
);
25769 return error_mark_node
;
25772 tree type
= TREE_TYPE (tmpl
);
25774 bool try_list_ctor
= false;
25776 vec
<tree
,va_gc
> *args
;
25777 if (init
== NULL_TREE
25778 || TREE_CODE (init
) == TREE_LIST
)
25779 args
= make_tree_vector_from_list (init
);
25780 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25782 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25783 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25785 /* As an exception, the first phase in 16.3.1.7 (considering the
25786 initializer list as a single argument) is omitted if the
25787 initializer list consists of a single expression of type cv U,
25788 where U is a specialization of C or a class derived from a
25789 specialization of C. */
25790 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25791 tree etype
= TREE_TYPE (elt
);
25793 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25794 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25795 int err
= unify (tparms
, targs
, type
, etype
,
25796 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25798 try_list_ctor
= false;
25801 if (try_list_ctor
|| is_std_init_list (type
))
25802 args
= make_tree_vector_single (init
);
25804 args
= make_tree_vector_from_ctor (init
);
25807 args
= make_tree_vector_single (init
);
25809 tree dname
= dguide_name (tmpl
);
25810 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25811 /*type*/false, /*complain*/false,
25813 bool elided
= false;
25814 if (cands
== error_mark_node
)
25817 /* Prune explicit deduction guides in copy-initialization context. */
25818 if (flags
& LOOKUP_ONLYCONVERTING
)
25820 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25821 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25826 /* Found a nonconverting guide, prune the candidates. */
25827 tree pruned
= NULL_TREE
;
25828 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25829 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25830 pruned
= lookup_add (*iter
, pruned
);
25836 tree outer_args
= NULL_TREE
;
25837 if (DECL_CLASS_SCOPE_P (tmpl
)
25838 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25840 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25841 type
= TREE_TYPE (most_general_template (tmpl
));
25844 bool saw_ctor
= false;
25845 // FIXME cache artificial deduction guides
25846 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25848 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25849 if ((flags
& LOOKUP_ONLYCONVERTING
)
25850 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25853 cands
= lookup_add (guide
, cands
);
25858 tree call
= error_mark_node
;
25860 /* If this is list-initialization and the class has a list constructor, first
25861 try deducing from the list as a single argument, as [over.match.list]. */
25862 tree list_cands
= NULL_TREE
;
25863 if (try_list_ctor
&& cands
)
25864 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25867 if (is_list_ctor (dg
))
25868 list_cands
= lookup_add (dg
, list_cands
);
25872 ++cp_unevaluated_operand
;
25873 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25874 --cp_unevaluated_operand
;
25876 if (call
== error_mark_node
)
25878 /* That didn't work, now try treating the list as a sequence of
25880 release_tree_vector (args
);
25881 args
= make_tree_vector_from_ctor (init
);
25885 /* Maybe generate an implicit deduction guide. */
25886 if (call
== error_mark_node
&& args
->length () < 2)
25888 tree gtype
= NULL_TREE
;
25890 if (args
->length () == 1)
25891 /* Generate a copy guide. */
25892 gtype
= build_reference_type (type
);
25893 else if (!saw_ctor
)
25894 /* Generate a default guide. */
25899 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25900 cands
= lookup_add (guide
, cands
);
25904 if (elided
&& !cands
)
25906 error ("cannot deduce template arguments for copy-initialization"
25907 " of %qT, as it has no non-explicit deduction guides or "
25908 "user-declared constructors", type
);
25909 return error_mark_node
;
25911 else if (!cands
&& call
== error_mark_node
)
25913 error ("cannot deduce template arguments of %qT, as it has no viable "
25914 "deduction guides", type
);
25915 return error_mark_node
;
25918 if (call
== error_mark_node
)
25920 ++cp_unevaluated_operand
;
25921 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25922 --cp_unevaluated_operand
;
25925 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25927 error ("class template argument deduction failed:");
25929 ++cp_unevaluated_operand
;
25930 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25931 --cp_unevaluated_operand
;
25934 inform (input_location
, "explicit deduction guides not considered "
25935 "for copy-initialization");
25938 release_tree_vector (args
);
25940 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25943 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25944 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25945 The CONTEXT determines the context in which auto deduction is performed
25946 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25947 OUTER_TARGS are used during template argument deduction
25948 (context == adc_unify) to properly substitute the result, and is ignored
25951 For partial-concept-ids, extra args may be appended to the list of deduced
25952 template arguments prior to determining constraint satisfaction. */
25955 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25956 tsubst_flags_t complain
, auto_deduction_context context
,
25957 tree outer_targs
, int flags
)
25961 if (init
== error_mark_node
)
25962 return error_mark_node
;
25964 if (init
&& type_dependent_expression_p (init
)
25965 && context
!= adc_unify
)
25966 /* Defining a subset of type-dependent expressions that we can deduce
25967 from ahead of time isn't worth the trouble. */
25970 /* Similarly, we can't deduce from another undeduced decl. */
25971 if (init
&& undeduced_auto_decl (init
))
25974 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25975 /* C++17 class template argument deduction. */
25976 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25978 if (TREE_TYPE (init
) == NULL_TREE
)
25979 /* Nothing we can do with this, even in deduction context. */
25982 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25983 with either a new invented type template parameter U or, if the
25984 initializer is a braced-init-list (8.5.4), with
25985 std::initializer_list<U>. */
25986 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25988 if (!DIRECT_LIST_INIT_P (init
))
25989 type
= listify_autos (type
, auto_node
);
25990 else if (CONSTRUCTOR_NELTS (init
) == 1)
25991 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25994 if (complain
& tf_warning_or_error
)
25996 if (permerror (input_location
, "direct-list-initialization of "
25997 "%<auto%> requires exactly one element"))
25998 inform (input_location
,
25999 "for deduction to %<std::initializer_list%>, use copy-"
26000 "list-initialization (i.e. add %<=%> before the %<{%>)");
26002 type
= listify_autos (type
, auto_node
);
26006 if (type
== error_mark_node
)
26007 return error_mark_node
;
26009 init
= resolve_nondeduced_context (init
, complain
);
26011 if (context
== adc_decomp_type
26012 && auto_node
== type
26013 && init
!= error_mark_node
26014 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26015 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26016 and initializer has array type, deduce cv-qualified array type. */
26017 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26019 else if (AUTO_IS_DECLTYPE (auto_node
))
26021 bool id
= (DECL_P (init
)
26022 || ((TREE_CODE (init
) == COMPONENT_REF
26023 || TREE_CODE (init
) == SCOPE_REF
)
26024 && !REF_PARENTHESIZED_P (init
)));
26025 targs
= make_tree_vec (1);
26026 TREE_VEC_ELT (targs
, 0)
26027 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26028 if (type
!= auto_node
)
26030 if (complain
& tf_error
)
26031 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26032 return error_mark_node
;
26037 tree parms
= build_tree_list (NULL_TREE
, type
);
26041 tparms
= extract_autos (type
);
26044 tparms
= make_tree_vec (1);
26045 TREE_VEC_ELT (tparms
, 0)
26046 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26049 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26050 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26051 DEDUCE_CALL
, LOOKUP_NORMAL
,
26052 NULL
, /*explain_p=*/false);
26055 if (processing_template_decl
)
26056 /* Try again at instantiation time. */
26058 if (type
&& type
!= error_mark_node
26059 && (complain
& tf_error
))
26060 /* If type is error_mark_node a diagnostic must have been
26061 emitted by now. Also, having a mention to '<type error>'
26062 in the diagnostic is not really useful to the user. */
26064 if (cfun
&& auto_node
== current_function_auto_return_pattern
26065 && LAMBDA_FUNCTION_P (current_function_decl
))
26066 error ("unable to deduce lambda return type from %qE", init
);
26068 error ("unable to deduce %qT from %qE", type
, init
);
26069 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26070 DEDUCE_CALL
, LOOKUP_NORMAL
,
26071 NULL
, /*explain_p=*/true);
26073 return error_mark_node
;
26077 /* Check any placeholder constraints against the deduced type. */
26078 if (flag_concepts
&& !processing_template_decl
)
26079 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26081 /* Use the deduced type to check the associated constraints. If we
26082 have a partial-concept-id, rebuild the argument list so that
26083 we check using the extra arguments. */
26084 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26085 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26086 if (TREE_VEC_LENGTH (cargs
) > 1)
26088 cargs
= copy_node (cargs
);
26089 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26093 if (!constraints_satisfied_p (constr
, cargs
))
26095 if (complain
& tf_warning_or_error
)
26099 case adc_unspecified
:
26101 error("placeholder constraints not satisfied");
26103 case adc_variable_type
:
26104 case adc_decomp_type
:
26105 error ("deduced initializer does not satisfy "
26106 "placeholder constraints");
26108 case adc_return_type
:
26109 error ("deduced return type does not satisfy "
26110 "placeholder constraints");
26112 case adc_requirement
:
26113 error ("deduced expression type does not satisfy "
26114 "placeholder constraints");
26117 diagnose_constraints (input_location
, constr
, targs
);
26119 return error_mark_node
;
26123 if (processing_template_decl
&& context
!= adc_unify
)
26124 outer_targs
= current_template_args ();
26125 targs
= add_to_template_args (outer_targs
, targs
);
26126 return tsubst (type
, targs
, complain
, NULL_TREE
);
26129 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26133 splice_late_return_type (tree type
, tree late_return_type
)
26135 if (is_auto (type
))
26137 if (late_return_type
)
26138 return late_return_type
;
26140 tree idx
= get_template_parm_index (type
);
26141 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26142 /* In an abbreviated function template we didn't know we were dealing
26143 with a function template when we saw the auto return type, so update
26144 it to have the correct level. */
26145 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26150 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26151 'decltype(auto)' or a deduced class template. */
26154 is_auto (const_tree type
)
26156 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26157 && (TYPE_IDENTIFIER (type
) == auto_identifier
26158 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26159 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26165 /* for_each_template_parm callback for type_uses_auto. */
26168 is_auto_r (tree tp
, void */
*data*/
)
26170 return is_auto (tp
);
26173 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26174 a use of `auto'. Returns NULL_TREE otherwise. */
26177 type_uses_auto (tree type
)
26179 if (type
== NULL_TREE
)
26181 else if (flag_concepts
)
26183 /* The Concepts TS allows multiple autos in one type-specifier; just
26184 return the first one we find, do_auto_deduction will collect all of
26186 if (uses_template_parms (type
))
26187 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26188 /*visited*/NULL
, /*nondeduced*/true);
26193 return find_type_usage (type
, is_auto
);
26196 /* For a given template T, return the vector of typedefs referenced
26197 in T for which access check is needed at T instantiation time.
26198 T is either a FUNCTION_DECL or a RECORD_TYPE.
26199 Those typedefs were added to T by the function
26200 append_type_to_template_for_access_check. */
26202 vec
<qualified_typedef_usage_t
, va_gc
> *
26203 get_types_needing_access_check (tree t
)
26206 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26208 if (!t
|| t
== error_mark_node
)
26211 if (!(ti
= get_template_info (t
)))
26214 if (CLASS_TYPE_P (t
)
26215 || TREE_CODE (t
) == FUNCTION_DECL
)
26217 if (!TI_TEMPLATE (ti
))
26220 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26226 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26227 tied to T. That list of typedefs will be access checked at
26228 T instantiation time.
26229 T is either a FUNCTION_DECL or a RECORD_TYPE.
26230 TYPE_DECL is a TYPE_DECL node representing a typedef.
26231 SCOPE is the scope through which TYPE_DECL is accessed.
26232 LOCATION is the location of the usage point of TYPE_DECL.
26234 This function is a subroutine of
26235 append_type_to_template_for_access_check. */
26238 append_type_to_template_for_access_check_1 (tree t
,
26241 location_t location
)
26243 qualified_typedef_usage_t typedef_usage
;
26246 if (!t
|| t
== error_mark_node
)
26249 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26250 || CLASS_TYPE_P (t
))
26252 && TREE_CODE (type_decl
) == TYPE_DECL
26255 if (!(ti
= get_template_info (t
)))
26258 gcc_assert (TI_TEMPLATE (ti
));
26260 typedef_usage
.typedef_decl
= type_decl
;
26261 typedef_usage
.context
= scope
;
26262 typedef_usage
.locus
= location
;
26264 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26267 /* Append TYPE_DECL to the template TEMPL.
26268 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26269 At TEMPL instanciation time, TYPE_DECL will be checked to see
26270 if it can be accessed through SCOPE.
26271 LOCATION is the location of the usage point of TYPE_DECL.
26273 e.g. consider the following code snippet:
26280 template<class U> struct S
26282 C::myint mi; // <-- usage point of the typedef C::myint
26287 At S<char> instantiation time, we need to check the access of C::myint
26288 In other words, we need to check the access of the myint typedef through
26289 the C scope. For that purpose, this function will add the myint typedef
26290 and the scope C through which its being accessed to a list of typedefs
26291 tied to the template S. That list will be walked at template instantiation
26292 time and access check performed on each typedefs it contains.
26293 Note that this particular code snippet should yield an error because
26294 myint is private to C. */
26297 append_type_to_template_for_access_check (tree templ
,
26300 location_t location
)
26302 qualified_typedef_usage_t
*iter
;
26305 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26307 /* Make sure we don't append the type to the template twice. */
26308 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26309 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26312 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26316 /* Convert the generic type parameters in PARM that match the types given in the
26317 range [START_IDX, END_IDX) from the current_template_parms into generic type
26321 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26323 tree current
= current_template_parms
;
26324 int depth
= TMPL_PARMS_DEPTH (current
);
26325 current
= INNERMOST_TEMPLATE_PARMS (current
);
26326 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26328 for (int i
= 0; i
< start_idx
; ++i
)
26329 TREE_VEC_ELT (replacement
, i
)
26330 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26332 for (int i
= start_idx
; i
< end_idx
; ++i
)
26334 /* Create a distinct parameter pack type from the current parm and add it
26335 to the replacement args to tsubst below into the generic function
26338 tree o
= TREE_TYPE (TREE_VALUE
26339 (TREE_VEC_ELT (current
, i
)));
26340 tree t
= copy_type (o
);
26341 TEMPLATE_TYPE_PARM_INDEX (t
)
26342 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26344 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26345 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26346 TYPE_MAIN_VARIANT (t
) = t
;
26347 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26348 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26349 TREE_VEC_ELT (replacement
, i
) = t
;
26350 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26353 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26354 TREE_VEC_ELT (replacement
, i
)
26355 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26357 /* If there are more levels then build up the replacement with the outer
26360 replacement
= add_to_template_args (template_parms_to_args
26361 (TREE_CHAIN (current_template_parms
)),
26364 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26367 /* Entries in the decl_constraint hash table. */
26368 struct GTY((for_user
)) constr_entry
26374 /* Hashing function and equality for constraint entries. */
26375 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26377 static hashval_t
hash (constr_entry
*e
)
26379 return (hashval_t
)DECL_UID (e
->decl
);
26382 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26384 return e1
->decl
== e2
->decl
;
26388 /* A mapping from declarations to constraint information. Note that
26389 both templates and their underlying declarations are mapped to the
26390 same constraint information.
26392 FIXME: This is defined in pt.c because garbage collection
26393 code is not being generated for constraint.cc. */
26395 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26397 /* Returns the template constraints of declaration T. If T is not
26398 constrained, return NULL_TREE. Note that T must be non-null. */
26401 get_constraints (tree t
)
26403 if (!flag_concepts
)
26406 gcc_assert (DECL_P (t
));
26407 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26408 t
= DECL_TEMPLATE_RESULT (t
);
26409 constr_entry elt
= { t
, NULL_TREE
};
26410 constr_entry
* found
= decl_constraints
->find (&elt
);
26417 /* Associate the given constraint information CI with the declaration
26418 T. If T is a template, then the constraints are associated with
26419 its underlying declaration. Don't build associations if CI is
26423 set_constraints (tree t
, tree ci
)
26427 gcc_assert (t
&& flag_concepts
);
26428 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26429 t
= DECL_TEMPLATE_RESULT (t
);
26430 gcc_assert (!get_constraints (t
));
26431 constr_entry elt
= {t
, ci
};
26432 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26433 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26438 /* Remove the associated constraints of the declaration T. */
26441 remove_constraints (tree t
)
26443 gcc_assert (DECL_P (t
));
26444 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26445 t
= DECL_TEMPLATE_RESULT (t
);
26447 constr_entry elt
= {t
, NULL_TREE
};
26448 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26450 decl_constraints
->clear_slot (slot
);
26453 /* Memoized satisfaction results for declarations. This
26454 maps the pair (constraint_info, arguments) to the result computed
26455 by constraints_satisfied_p. */
26457 struct GTY((for_user
)) constraint_sat_entry
26464 /* Hashing function and equality for constraint entries. */
26466 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26468 static hashval_t
hash (constraint_sat_entry
*e
)
26470 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26471 return iterative_hash_template_arg (e
->args
, val
);
26474 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26476 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26480 /* Memoized satisfaction results for concept checks. */
26482 struct GTY((for_user
)) concept_spec_entry
26489 /* Hashing function and equality for constraint entries. */
26491 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26493 static hashval_t
hash (concept_spec_entry
*e
)
26495 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26498 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26500 ++comparing_specializations
;
26501 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26502 --comparing_specializations
;
26507 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26508 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26510 /* Search for a memoized satisfaction result. Returns one of the
26511 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26514 lookup_constraint_satisfaction (tree ci
, tree args
)
26516 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26517 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26519 return found
->result
;
26524 /* Memoize the result of a satisfication test. Returns the saved result. */
26527 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26529 constraint_sat_entry elt
= {ci
, args
, result
};
26530 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26531 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26537 /* Search for a memoized satisfaction result for a concept. */
26540 lookup_concept_satisfaction (tree tmpl
, tree args
)
26542 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26543 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26545 return found
->result
;
26550 /* Memoize the result of a concept check. Returns the saved result. */
26553 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26555 concept_spec_entry elt
= {tmpl
, args
, result
};
26556 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26557 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26563 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26565 /* Returns a prior concept specialization. This returns the substituted
26566 and normalized constraints defined by the concept. */
26569 get_concept_expansion (tree tmpl
, tree args
)
26571 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26572 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26574 return found
->result
;
26579 /* Save a concept expansion for later. */
26582 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26584 concept_spec_entry elt
= {tmpl
, args
, def
};
26585 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26586 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26593 hash_subsumption_args (tree t1
, tree t2
)
26595 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26596 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26598 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26599 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26600 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26601 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26605 /* Compare the constraints of two subsumption entries. The LEFT1 and
26606 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26607 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26610 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26612 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26613 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26614 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26615 CHECK_CONSTR_ARGS (right1
)))
26616 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26617 CHECK_CONSTR_ARGS (right2
));
26621 /* Key/value pair for learning and memoizing subsumption results. This
26622 associates a pair of check constraints (including arguments) with
26623 a boolean value indicating the result. */
26625 struct GTY((for_user
)) subsumption_entry
26632 /* Hashing function and equality for constraint entries. */
26634 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26636 static hashval_t
hash (subsumption_entry
*e
)
26638 return hash_subsumption_args (e
->t1
, e
->t2
);
26641 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26643 ++comparing_specializations
;
26644 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26645 --comparing_specializations
;
26650 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26652 /* Search for a previously cached subsumption result. */
26655 lookup_subsumption_result (tree t1
, tree t2
)
26657 subsumption_entry elt
= { t1
, t2
, false };
26658 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26660 return &found
->result
;
26665 /* Save a subsumption result. */
26668 save_subsumption_result (tree t1
, tree t2
, bool result
)
26670 subsumption_entry elt
= {t1
, t2
, result
};
26671 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26672 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26678 /* Set up the hash table for constraint association. */
26681 init_constraint_processing (void)
26683 if (!flag_concepts
)
26686 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26687 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26688 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26689 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26690 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26693 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26697 declare_integer_pack (void)
26699 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26700 build_function_type_list (integer_type_node
,
26703 NULL_TREE
, ECF_CONST
);
26704 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26705 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26708 /* Set up the hash tables for template instantiations. */
26711 init_template_processing (void)
26713 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26714 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26716 if (cxx_dialect
>= cxx11
)
26717 declare_integer_pack ();
26720 /* Print stats about the template hash tables for -fstats. */
26723 print_template_statistics (void)
26725 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26726 "%f collisions\n", (long) decl_specializations
->size (),
26727 (long) decl_specializations
->elements (),
26728 decl_specializations
->collisions ());
26729 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26730 "%f collisions\n", (long) type_specializations
->size (),
26731 (long) type_specializations
->elements (),
26732 type_specializations
->collisions ());
26737 namespace selftest
{
26739 /* Verify that build_non_dependent_expr () works, for various expressions,
26740 and that location wrappers don't affect the results. */
26743 test_build_non_dependent_expr ()
26745 location_t loc
= BUILTINS_LOCATION
;
26747 /* Verify constants, without and with location wrappers. */
26748 tree int_cst
= build_int_cst (integer_type_node
, 42);
26749 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26751 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26752 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26753 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26755 tree string_lit
= build_string (4, "foo");
26756 TREE_TYPE (string_lit
) = char_array_type_node
;
26757 string_lit
= fix_string_type (string_lit
);
26758 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26760 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26761 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26762 ASSERT_EQ (wrapped_string_lit
,
26763 build_non_dependent_expr (wrapped_string_lit
));
26766 /* Verify that type_dependent_expression_p () works correctly, even
26767 in the presence of location wrapper nodes. */
26770 test_type_dependent_expression_p ()
26772 location_t loc
= BUILTINS_LOCATION
;
26774 tree name
= get_identifier ("foo");
26776 /* If no templates are involved, nothing is type-dependent. */
26777 gcc_assert (!processing_template_decl
);
26778 ASSERT_FALSE (type_dependent_expression_p (name
));
26780 ++processing_template_decl
;
26782 /* Within a template, an unresolved name is always type-dependent. */
26783 ASSERT_TRUE (type_dependent_expression_p (name
));
26785 /* Ensure it copes with NULL_TREE and errors. */
26786 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26787 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26789 /* A USING_DECL in a template should be type-dependent, even if wrapped
26790 with a location wrapper (PR c++/83799). */
26791 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26792 TREE_TYPE (using_decl
) = integer_type_node
;
26793 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26794 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26795 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26796 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26798 --processing_template_decl
;
26801 /* Run all of the selftests within this file. */
26806 test_build_non_dependent_expr ();
26807 test_type_dependent_expression_p ();
26810 } // namespace selftest
26812 #endif /* #if CHECKING_P */
26814 #include "gt-cp-pt.h"