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 /* Remove, from the set of candidates, all those functions
2207 whose constraints are not satisfied. */
2208 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2211 // Then, try to form the new function type.
2212 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2213 if (insttype
== error_mark_node
)
2216 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2217 if (!compparms (fn_arg_types
, decl_arg_types
))
2220 /* Save this template, and the arguments deduced. */
2221 templates
= tree_cons (targs
, fn
, templates
);
2223 else if (need_member_template
)
2224 /* FN is an ordinary member function, and we need a
2225 specialization of a member template. */
2227 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2228 /* We can get IDENTIFIER_NODEs here in certain erroneous
2231 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2232 /* This is just an ordinary non-member function. Nothing can
2233 be a specialization of that. */
2235 else if (DECL_ARTIFICIAL (fn
))
2236 /* Cannot specialize functions that are created implicitly. */
2240 tree decl_arg_types
;
2242 /* This is an ordinary member function. However, since
2243 we're here, we can assume its enclosing class is a
2244 template class. For example,
2246 template <typename T> struct S { void f(); };
2247 template <> void S<int>::f() {}
2249 Here, S<int>::f is a non-template, but S<int> is a
2250 template class. If FN has the same type as DECL, we
2251 might be in business. */
2253 if (!DECL_TEMPLATE_INFO (fn
))
2254 /* Its enclosing class is an explicit specialization
2255 of a template class. This is not a candidate. */
2258 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2259 TREE_TYPE (TREE_TYPE (fn
))))
2260 /* The return types differ. */
2263 /* Adjust the type of DECL in case FN is a static member. */
2264 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2265 if (DECL_STATIC_FUNCTION_P (fn
)
2266 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2267 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2269 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2273 // If the deduced arguments do not satisfy the constraints,
2274 // this is not a candidate.
2275 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2278 // Add the candidate.
2279 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2283 if (templates
&& TREE_CHAIN (templates
))
2289 It is possible for a specialization with a given function
2290 signature to be instantiated from more than one function
2291 template. In such cases, explicit specification of the
2292 template arguments must be used to uniquely identify the
2293 function template specialization being specialized.
2295 Note that here, there's no suggestion that we're supposed to
2296 determine which of the candidate templates is most
2297 specialized. However, we, also have:
2301 Partial ordering of overloaded function template
2302 declarations is used in the following contexts to select
2303 the function template to which a function template
2304 specialization refers:
2306 -- when an explicit specialization refers to a function
2309 So, we do use the partial ordering rules, at least for now.
2310 This extension can only serve to make invalid programs valid,
2311 so it's safe. And, there is strong anecdotal evidence that
2312 the committee intended the partial ordering rules to apply;
2313 the EDG front end has that behavior, and John Spicer claims
2314 that the committee simply forgot to delete the wording in
2315 [temp.expl.spec]. */
2316 tree tmpl
= most_specialized_instantiation (templates
);
2317 if (tmpl
!= error_mark_node
)
2320 TREE_CHAIN (templates
) = NULL_TREE
;
2324 // Concepts allows multiple declarations of member functions
2325 // with the same signature. Like above, we need to rely on
2326 // on the partial ordering of those candidates to determine which
2328 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2330 if (tree cand
= most_constrained_function (candidates
))
2333 TREE_CHAIN (cand
) = NULL_TREE
;
2337 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2339 error ("template-id %qD for %q+D does not match any template "
2340 "declaration", template_id
, decl
);
2341 if (header_count
&& header_count
!= template_count
+ 1)
2342 inform (input_location
, "saw %d %<template<>%>, need %d for "
2343 "specializing a member function template",
2344 header_count
, template_count
+ 1);
2346 print_candidates (orig_fns
);
2347 return error_mark_node
;
2349 else if ((templates
&& TREE_CHAIN (templates
))
2350 || (candidates
&& TREE_CHAIN (candidates
))
2351 || (templates
&& candidates
))
2353 error ("ambiguous template specialization %qD for %q+D",
2355 candidates
= chainon (candidates
, templates
);
2356 print_candidates (candidates
);
2357 return error_mark_node
;
2360 /* We have one, and exactly one, match. */
2363 tree fn
= TREE_VALUE (candidates
);
2364 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2366 // Propagate the candidate's constraints to the declaration.
2367 set_constraints (decl
, get_constraints (fn
));
2369 /* DECL is a re-declaration or partial instantiation of a template
2371 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2373 /* It was a specialization of an ordinary member function in a
2375 return DECL_TI_TEMPLATE (fn
);
2378 /* It was a specialization of a template. */
2379 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2380 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2382 *targs_out
= copy_node (targs
);
2383 SET_TMPL_ARGS_LEVEL (*targs_out
,
2384 TMPL_ARGS_DEPTH (*targs_out
),
2385 TREE_PURPOSE (templates
));
2388 *targs_out
= TREE_PURPOSE (templates
);
2389 return TREE_VALUE (templates
);
2392 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2393 but with the default argument values filled in from those in the
2397 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2400 tree new_spec_types
;
2405 if (spec_types
== void_list_node
)
2406 return void_list_node
;
2408 /* Substitute into the rest of the list. */
2410 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2411 TREE_CHAIN (tmpl_types
));
2413 /* Add the default argument for this parameter. */
2414 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2415 TREE_VALUE (spec_types
),
2419 /* DECL is an explicit specialization. Replicate default arguments
2420 from the template it specializes. (That way, code like:
2422 template <class T> void f(T = 3);
2423 template <> void f(double);
2426 works, as required.) An alternative approach would be to look up
2427 the correct default arguments at the call-site, but this approach
2428 is consistent with how implicit instantiations are handled. */
2431 copy_default_args_to_explicit_spec (tree decl
)
2436 tree new_spec_types
;
2440 tree object_type
= NULL_TREE
;
2441 tree in_charge
= NULL_TREE
;
2442 tree vtt
= NULL_TREE
;
2444 /* See if there's anything we need to do. */
2445 tmpl
= DECL_TI_TEMPLATE (decl
);
2446 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2447 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2448 if (TREE_PURPOSE (t
))
2453 old_type
= TREE_TYPE (decl
);
2454 spec_types
= TYPE_ARG_TYPES (old_type
);
2456 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2458 /* Remove the this pointer, but remember the object's type for
2460 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2461 spec_types
= TREE_CHAIN (spec_types
);
2462 tmpl_types
= TREE_CHAIN (tmpl_types
);
2464 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2466 /* DECL may contain more parameters than TMPL due to the extra
2467 in-charge parameter in constructors and destructors. */
2468 in_charge
= spec_types
;
2469 spec_types
= TREE_CHAIN (spec_types
);
2471 if (DECL_HAS_VTT_PARM_P (decl
))
2474 spec_types
= TREE_CHAIN (spec_types
);
2478 /* Compute the merged default arguments. */
2480 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2482 /* Compute the new FUNCTION_TYPE. */
2486 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2491 /* Put the in-charge parameter back. */
2492 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2493 TREE_VALUE (in_charge
),
2496 new_type
= build_method_type_directly (object_type
,
2497 TREE_TYPE (old_type
),
2501 new_type
= build_function_type (TREE_TYPE (old_type
),
2503 new_type
= cp_build_type_attribute_variant (new_type
,
2504 TYPE_ATTRIBUTES (old_type
));
2505 new_type
= build_exception_variant (new_type
,
2506 TYPE_RAISES_EXCEPTIONS (old_type
));
2508 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2509 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2511 TREE_TYPE (decl
) = new_type
;
2514 /* Return the number of template headers we expect to see for a definition
2515 or specialization of CTYPE or one of its non-template members. */
2518 num_template_headers_for_class (tree ctype
)
2520 int num_templates
= 0;
2522 while (ctype
&& CLASS_TYPE_P (ctype
))
2524 /* You're supposed to have one `template <...>' for every
2525 template class, but you don't need one for a full
2526 specialization. For example:
2528 template <class T> struct S{};
2529 template <> struct S<int> { void f(); };
2530 void S<int>::f () {}
2532 is correct; there shouldn't be a `template <>' for the
2533 definition of `S<int>::f'. */
2534 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2535 /* If CTYPE does not have template information of any
2536 kind, then it is not a template, nor is it nested
2537 within a template. */
2539 if (explicit_class_specialization_p (ctype
))
2541 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2544 ctype
= TYPE_CONTEXT (ctype
);
2547 return num_templates
;
2550 /* Do a simple sanity check on the template headers that precede the
2551 variable declaration DECL. */
2554 check_template_variable (tree decl
)
2556 tree ctx
= CP_DECL_CONTEXT (decl
);
2557 int wanted
= num_template_headers_for_class (ctx
);
2558 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2559 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2561 if (cxx_dialect
< cxx14
)
2562 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2563 "variable templates only available with "
2564 "-std=c++14 or -std=gnu++14");
2566 // Namespace-scope variable templates should have a template header.
2569 if (template_header_count
> wanted
)
2571 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2572 "too many template headers for %qD "
2575 if (warned
&& CLASS_TYPE_P (ctx
)
2576 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2577 inform (DECL_SOURCE_LOCATION (decl
),
2578 "members of an explicitly specialized class are defined "
2579 "without a template header");
2583 /* An explicit specialization whose declarator-id or class-head-name is not
2584 qualified shall be declared in the nearest enclosing namespace of the
2585 template, or, if the namespace is inline (7.3.1), any namespace from its
2586 enclosing namespace set.
2588 If the name declared in the explicit instantiation is an unqualified name,
2589 the explicit instantiation shall appear in the namespace where its template
2590 is declared or, if that namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set. */
2594 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2596 tree tmpl
= most_general_template (t
);
2597 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2598 && !is_nested_namespace (current_namespace
,
2599 CP_DECL_CONTEXT (tmpl
), true))
2601 if (processing_specialization
)
2602 permerror (loc
, "explicit specialization of %qD outside its "
2603 "namespace must use a nested-name-specifier", tmpl
);
2604 else if (processing_explicit_instantiation
2605 && cxx_dialect
>= cxx11
)
2606 /* This was allowed in C++98, so only pedwarn. */
2607 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2608 "outside its namespace must use a nested-name-"
2613 /* Check to see if the function just declared, as indicated in
2614 DECLARATOR, and in DECL, is a specialization of a function
2615 template. We may also discover that the declaration is an explicit
2616 instantiation at this point.
2618 Returns DECL, or an equivalent declaration that should be used
2619 instead if all goes well. Issues an error message if something is
2620 amiss. Returns error_mark_node if the error is not easily
2623 FLAGS is a bitmask consisting of the following flags:
2625 2: The function has a definition.
2626 4: The function is a friend.
2628 The TEMPLATE_COUNT is the number of references to qualifying
2629 template classes that appeared in the name of the function. For
2632 template <class T> struct S { void f(); };
2635 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2636 classes are not counted in the TEMPLATE_COUNT, so that in
2638 template <class T> struct S {};
2639 template <> struct S<int> { void f(); }
2640 template <> void S<int>::f();
2642 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2643 invalid; there should be no template <>.)
2645 If the function is a specialization, it is marked as such via
2646 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2647 is set up correctly, and it is added to the list of specializations
2648 for that template. */
2651 check_explicit_specialization (tree declarator
,
2656 int have_def
= flags
& 2;
2657 int is_friend
= flags
& 4;
2658 bool is_concept
= flags
& 8;
2659 int specialization
= 0;
2660 int explicit_instantiation
= 0;
2661 int member_specialization
= 0;
2662 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2663 tree dname
= DECL_NAME (decl
);
2668 if (!processing_specialization
)
2671 tsk
= tsk_excessive_parms
;
2674 tsk
= current_tmpl_spec_kind (template_count
);
2679 if (processing_specialization
&& !VAR_P (decl
))
2682 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2684 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2687 /* This could be something like:
2689 template <class T> void f(T);
2690 class S { friend void f<>(int); } */
2694 /* This case handles bogus declarations like template <>
2695 template <class T> void f<int>(); */
2697 error ("template-id %qD in declaration of primary template",
2704 case tsk_invalid_member_spec
:
2705 /* The error has already been reported in
2706 check_specialization_scope. */
2707 return error_mark_node
;
2709 case tsk_invalid_expl_inst
:
2710 error ("template parameter list used in explicit instantiation");
2716 error ("definition provided for explicit instantiation");
2718 explicit_instantiation
= 1;
2721 case tsk_excessive_parms
:
2722 case tsk_insufficient_parms
:
2723 if (tsk
== tsk_excessive_parms
)
2724 error ("too many template parameter lists in declaration of %qD",
2726 else if (template_header_count
)
2727 error("too few template parameter lists in declaration of %qD", decl
);
2729 error("explicit specialization of %qD must be introduced by "
2730 "%<template <>%>", decl
);
2735 error ("explicit specialization declared %<concept%>");
2737 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2738 /* In cases like template<> constexpr bool v = true;
2739 We'll give an error in check_template_variable. */
2742 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2744 member_specialization
= 1;
2750 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2752 /* This case handles bogus declarations like template <>
2753 template <class T> void f<int>(); */
2755 if (!uses_template_parms (declarator
))
2756 error ("template-id %qD in declaration of primary template",
2758 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2760 /* Partial specialization of variable template. */
2761 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2765 else if (cxx_dialect
< cxx14
)
2766 error ("non-type partial specialization %qD "
2767 "is not allowed", declarator
);
2769 error ("non-class, non-variable partial specialization %qD "
2770 "is not allowed", declarator
);
2775 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2776 /* This is a specialization of a member template, without
2777 specialization the containing class. Something like:
2779 template <class T> struct S {
2780 template <class U> void f (U);
2782 template <> template <class U> void S<int>::f(U) {}
2784 That's a specialization -- but of the entire template. */
2792 if ((specialization
|| member_specialization
)
2793 /* This doesn't apply to variable templates. */
2794 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2795 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2797 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2798 for (; t
; t
= TREE_CHAIN (t
))
2799 if (TREE_PURPOSE (t
))
2801 permerror (input_location
,
2802 "default argument specified in explicit specialization");
2807 if (specialization
|| member_specialization
|| explicit_instantiation
)
2809 tree tmpl
= NULL_TREE
;
2810 tree targs
= NULL_TREE
;
2811 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2813 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2814 if (!was_template_id
)
2818 gcc_assert (identifier_p (declarator
));
2823 /* If there is no class context, the explicit instantiation
2824 must be at namespace scope. */
2825 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2827 /* Find the namespace binding, using the declaration
2829 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2831 if (fns
== error_mark_node
)
2832 /* If lookup fails, look for a friend declaration so we can
2833 give a better diagnostic. */
2834 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2835 /*type*/false, /*complain*/true,
2838 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2840 error ("%qD is not a template function", dname
);
2841 fns
= error_mark_node
;
2845 declarator
= lookup_template_function (fns
, NULL_TREE
);
2848 if (declarator
== error_mark_node
)
2849 return error_mark_node
;
2851 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2853 if (!explicit_instantiation
)
2854 /* A specialization in class scope. This is invalid,
2855 but the error will already have been flagged by
2856 check_specialization_scope. */
2857 return error_mark_node
;
2860 /* It's not valid to write an explicit instantiation in
2863 class C { template void f(); }
2865 This case is caught by the parser. However, on
2868 template class C { void f(); };
2870 (which is invalid) we can get here. The error will be
2877 else if (ctype
!= NULL_TREE
2878 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2880 // We'll match variable templates in start_decl.
2884 /* Find the list of functions in ctype that have the same
2885 name as the declared function. */
2886 tree name
= TREE_OPERAND (declarator
, 0);
2888 if (constructor_name_p (name
, ctype
))
2890 if (DECL_CONSTRUCTOR_P (decl
)
2891 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2892 : !CLASSTYPE_DESTRUCTOR (ctype
))
2894 /* From [temp.expl.spec]:
2896 If such an explicit specialization for the member
2897 of a class template names an implicitly-declared
2898 special member function (clause _special_), the
2899 program is ill-formed.
2901 Similar language is found in [temp.explicit]. */
2902 error ("specialization of implicitly-declared special member function");
2903 return error_mark_node
;
2906 name
= DECL_NAME (decl
);
2909 /* For a type-conversion operator, We might be looking for
2910 `operator int' which will be a specialization of
2911 `operator T'. Grab all the conversion operators, and
2912 then select from them. */
2913 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2914 ? conv_op_identifier
: name
);
2916 if (fns
== NULL_TREE
)
2918 error ("no member function %qD declared in %qT", name
, ctype
);
2919 return error_mark_node
;
2922 TREE_OPERAND (declarator
, 0) = fns
;
2925 /* Figure out what exactly is being specialized at this point.
2926 Note that for an explicit instantiation, even one for a
2927 member function, we cannot tell a priori whether the
2928 instantiation is for a member template, or just a member
2929 function of a template class. Even if a member template is
2930 being instantiated, the member template arguments may be
2931 elided if they can be deduced from the rest of the
2933 tmpl
= determine_specialization (declarator
, decl
,
2935 member_specialization
,
2939 if (!tmpl
|| tmpl
== error_mark_node
)
2940 /* We couldn't figure out what this declaration was
2942 return error_mark_node
;
2945 if (TREE_CODE (decl
) == FUNCTION_DECL
2946 && DECL_HIDDEN_FRIEND_P (tmpl
))
2948 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2949 "friend declaration %qD is not visible to "
2950 "explicit specialization", tmpl
))
2951 inform (DECL_SOURCE_LOCATION (tmpl
),
2952 "friend declaration here");
2954 else if (!ctype
&& !is_friend
2955 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2956 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2958 tree gen_tmpl
= most_general_template (tmpl
);
2960 if (explicit_instantiation
)
2962 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2963 is done by do_decl_instantiation later. */
2965 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2966 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2968 if (arg_depth
> parm_depth
)
2970 /* If TMPL is not the most general template (for
2971 example, if TMPL is a friend template that is
2972 injected into namespace scope), then there will
2973 be too many levels of TARGS. Remove some of them
2978 new_targs
= make_tree_vec (parm_depth
);
2979 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2980 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2981 = TREE_VEC_ELT (targs
, i
);
2985 return instantiate_template (tmpl
, targs
, tf_error
);
2988 /* If we thought that the DECL was a member function, but it
2989 turns out to be specializing a static member function,
2990 make DECL a static member function as well. */
2991 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2992 && DECL_STATIC_FUNCTION_P (tmpl
)
2993 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2994 revert_static_member_fn (decl
);
2996 /* If this is a specialization of a member template of a
2997 template class, we want to return the TEMPLATE_DECL, not
2998 the specialization of it. */
2999 if (tsk
== tsk_template
&& !was_template_id
)
3001 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3002 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3003 DECL_INITIAL (result
) = NULL_TREE
;
3007 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3008 DECL_SOURCE_LOCATION (result
)
3009 = DECL_SOURCE_LOCATION (decl
);
3010 /* We want to use the argument list specified in the
3011 definition, not in the original declaration. */
3012 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3013 for (parm
= DECL_ARGUMENTS (result
); parm
;
3014 parm
= DECL_CHAIN (parm
))
3015 DECL_CONTEXT (parm
) = result
;
3017 return register_specialization (tmpl
, gen_tmpl
, targs
,
3021 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3022 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3024 if (was_template_id
)
3025 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3027 /* Inherit default function arguments from the template
3028 DECL is specializing. */
3029 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3030 copy_default_args_to_explicit_spec (decl
);
3032 /* This specialization has the same protection as the
3033 template it specializes. */
3034 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3035 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3037 /* 7.1.1-1 [dcl.stc]
3039 A storage-class-specifier shall not be specified in an
3040 explicit specialization...
3042 The parser rejects these, so unless action is taken here,
3043 explicit function specializations will always appear with
3046 The action recommended by the C++ CWG in response to C++
3047 defect report 605 is to make the storage class and linkage
3048 of the explicit specialization match the templated function:
3050 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3052 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3054 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3055 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3057 /* A concept cannot be specialized. */
3058 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3060 error ("explicit specialization of function concept %qD",
3062 return error_mark_node
;
3065 /* This specialization has the same linkage and visibility as
3066 the function template it specializes. */
3067 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3068 if (! TREE_PUBLIC (decl
))
3070 DECL_INTERFACE_KNOWN (decl
) = 1;
3071 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3073 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3074 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3076 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3077 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3081 /* If DECL is a friend declaration, declared using an
3082 unqualified name, the namespace associated with DECL may
3083 have been set incorrectly. For example, in:
3085 template <typename T> void f(T);
3087 struct S { friend void f<int>(int); }
3090 we will have set the DECL_CONTEXT for the friend
3091 declaration to N, rather than to the global namespace. */
3092 if (DECL_NAMESPACE_SCOPE_P (decl
))
3093 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3095 if (is_friend
&& !have_def
)
3096 /* This is not really a declaration of a specialization.
3097 It's just the name of an instantiation. But, it's not
3098 a request for an instantiation, either. */
3099 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3100 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3101 /* A specialization is not necessarily COMDAT. */
3102 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3103 && DECL_DECLARED_INLINE_P (decl
));
3104 else if (VAR_P (decl
))
3105 DECL_COMDAT (decl
) = false;
3107 /* If this is a full specialization, register it so that we can find
3108 it again. Partial specializations will be registered in
3109 process_partial_specialization. */
3110 if (!processing_template_decl
)
3111 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3114 /* A 'structor should already have clones. */
3115 gcc_assert (decl
== error_mark_node
3116 || variable_template_p (tmpl
)
3117 || !(DECL_CONSTRUCTOR_P (decl
)
3118 || DECL_DESTRUCTOR_P (decl
))
3119 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3126 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3127 parameters. These are represented in the same format used for
3128 DECL_TEMPLATE_PARMS. */
3131 comp_template_parms (const_tree parms1
, const_tree parms2
)
3136 if (parms1
== parms2
)
3139 for (p1
= parms1
, p2
= parms2
;
3140 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3141 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3143 tree t1
= TREE_VALUE (p1
);
3144 tree t2
= TREE_VALUE (p2
);
3147 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3148 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3150 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3153 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3155 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3156 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3158 /* If either of the template parameters are invalid, assume
3159 they match for the sake of error recovery. */
3160 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3163 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3166 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3167 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3168 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3170 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3175 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3176 /* One set of parameters has more parameters lists than the
3183 /* Determine whether PARM is a parameter pack. */
3186 template_parameter_pack_p (const_tree parm
)
3188 /* Determine if we have a non-type template parameter pack. */
3189 if (TREE_CODE (parm
) == PARM_DECL
)
3190 return (DECL_TEMPLATE_PARM_P (parm
)
3191 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3192 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3193 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3195 /* If this is a list of template parameters, we could get a
3196 TYPE_DECL or a TEMPLATE_DECL. */
3197 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3198 parm
= TREE_TYPE (parm
);
3200 /* Otherwise it must be a type template parameter. */
3201 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3202 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3203 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3206 /* Determine if T is a function parameter pack. */
3209 function_parameter_pack_p (const_tree t
)
3211 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3212 return DECL_PACK_P (t
);
3216 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3217 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3220 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3222 if (! primary_func_tmpl_inst
3223 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3224 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3227 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3230 /* Return true iff the function parameter PARAM_DECL was expanded
3231 from the function parameter pack PACK. */
3234 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3236 if (DECL_ARTIFICIAL (param_decl
)
3237 || !function_parameter_pack_p (pack
))
3240 /* The parameter pack and its pack arguments have the same
3242 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3245 /* Determine whether ARGS describes a variadic template args list,
3246 i.e., one that is terminated by a template argument pack. */
3249 template_args_variadic_p (tree args
)
3254 if (args
== NULL_TREE
)
3257 args
= INNERMOST_TEMPLATE_ARGS (args
);
3258 nargs
= TREE_VEC_LENGTH (args
);
3263 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3265 return ARGUMENT_PACK_P (last_parm
);
3268 /* Generate a new name for the parameter pack name NAME (an
3269 IDENTIFIER_NODE) that incorporates its */
3272 make_ith_pack_parameter_name (tree name
, int i
)
3274 /* Munge the name to include the parameter index. */
3275 #define NUMBUF_LEN 128
3276 char numbuf
[NUMBUF_LEN
];
3280 if (name
== NULL_TREE
)
3282 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3283 newname_len
= IDENTIFIER_LENGTH (name
)
3284 + strlen (numbuf
) + 2;
3285 newname
= (char*)alloca (newname_len
);
3286 snprintf (newname
, newname_len
,
3287 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3288 return get_identifier (newname
);
3291 /* Return true if T is a primary function, class or alias template
3292 specialization, not including the template pattern. */
3295 primary_template_specialization_p (const_tree t
)
3300 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3301 return (DECL_LANG_SPECIFIC (t
)
3302 && DECL_USE_TEMPLATE (t
)
3303 && DECL_TEMPLATE_INFO (t
)
3304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3305 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3306 return (CLASSTYPE_TEMPLATE_INFO (t
)
3307 && CLASSTYPE_USE_TEMPLATE (t
)
3308 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3309 else if (alias_template_specialization_p (t
))
3314 /* Return true if PARM is a template template parameter. */
3317 template_template_parameter_p (const_tree parm
)
3319 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3322 /* Return true iff PARM is a DECL representing a type template
3326 template_type_parameter_p (const_tree parm
)
3329 && (TREE_CODE (parm
) == TYPE_DECL
3330 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3331 && DECL_TEMPLATE_PARM_P (parm
));
3334 /* Return the template parameters of T if T is a
3335 primary template instantiation, NULL otherwise. */
3338 get_primary_template_innermost_parameters (const_tree t
)
3340 tree parms
= NULL
, template_info
= NULL
;
3342 if ((template_info
= get_template_info (t
))
3343 && primary_template_specialization_p (t
))
3344 parms
= INNERMOST_TEMPLATE_PARMS
3345 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3350 /* Return the template parameters of the LEVELth level from the full list
3351 of template parameters PARMS. */
3354 get_template_parms_at_level (tree parms
, int level
)
3358 || TREE_CODE (parms
) != TREE_LIST
3359 || level
> TMPL_PARMS_DEPTH (parms
))
3362 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3363 if (TMPL_PARMS_DEPTH (p
) == level
)
3369 /* Returns the template arguments of T if T is a template instantiation,
3373 get_template_innermost_arguments (const_tree t
)
3375 tree args
= NULL
, template_info
= NULL
;
3377 if ((template_info
= get_template_info (t
))
3378 && TI_ARGS (template_info
))
3379 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3384 /* Return the argument pack elements of T if T is a template argument pack,
3388 get_template_argument_pack_elems (const_tree t
)
3390 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3391 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3394 return ARGUMENT_PACK_ARGS (t
);
3397 /* True iff FN is a function representing a built-in variadic parameter
3401 builtin_pack_fn_p (tree fn
)
3404 || TREE_CODE (fn
) != FUNCTION_DECL
3405 || !DECL_IS_BUILTIN (fn
))
3408 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3414 /* True iff CALL is a call to a function representing a built-in variadic
3418 builtin_pack_call_p (tree call
)
3420 if (TREE_CODE (call
) != CALL_EXPR
)
3422 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3425 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3428 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3431 tree ohi
= CALL_EXPR_ARG (call
, 0);
3432 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3433 false/*fn*/, true/*int_cst*/);
3435 if (value_dependent_expression_p (hi
))
3439 call
= copy_node (call
);
3440 CALL_EXPR_ARG (call
, 0) = hi
;
3442 tree ex
= make_pack_expansion (call
, complain
);
3443 tree vec
= make_tree_vec (1);
3444 TREE_VEC_ELT (vec
, 0) = ex
;
3449 hi
= cxx_constant_value (hi
);
3450 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3452 /* Calculate the largest value of len that won't make the size of the vec
3453 overflow an int. The compiler will exceed resource limits long before
3454 this, but it seems a decent place to diagnose. */
3455 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3457 if (len
< 0 || len
> max
)
3459 if ((complain
& tf_error
)
3460 && hi
!= error_mark_node
)
3461 error ("argument to __integer_pack must be between 0 and %d", max
);
3462 return error_mark_node
;
3465 tree vec
= make_tree_vec (len
);
3467 for (int i
= 0; i
< len
; ++i
)
3468 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3474 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3478 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3481 if (!builtin_pack_call_p (call
))
3484 tree fn
= CALL_EXPR_FN (call
);
3486 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3487 return expand_integer_pack (call
, args
, complain
, in_decl
);
3492 /* Structure used to track the progress of find_parameter_packs_r. */
3493 struct find_parameter_pack_data
3495 /* TREE_LIST that will contain all of the parameter packs found by
3497 tree
* parameter_packs
;
3499 /* Set of AST nodes that have been visited by the traversal. */
3500 hash_set
<tree
> *visited
;
3502 /* True iff we're making a type pack expansion. */
3503 bool type_pack_expansion_p
;
3506 /* Identifies all of the argument packs that occur in a template
3507 argument and appends them to the TREE_LIST inside DATA, which is a
3508 find_parameter_pack_data structure. This is a subroutine of
3509 make_pack_expansion and uses_parameter_packs. */
3511 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3514 struct find_parameter_pack_data
* ppd
=
3515 (struct find_parameter_pack_data
*)data
;
3516 bool parameter_pack_p
= false;
3518 /* Handle type aliases/typedefs. */
3519 if (TYPE_ALIAS_P (t
))
3521 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3522 cp_walk_tree (&TI_ARGS (tinfo
),
3523 &find_parameter_packs_r
,
3529 /* Identify whether this is a parameter pack or not. */
3530 switch (TREE_CODE (t
))
3532 case TEMPLATE_PARM_INDEX
:
3533 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3534 parameter_pack_p
= true;
3537 case TEMPLATE_TYPE_PARM
:
3538 t
= TYPE_MAIN_VARIANT (t
);
3540 case TEMPLATE_TEMPLATE_PARM
:
3541 /* If the placeholder appears in the decl-specifier-seq of a function
3542 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3543 is a pack expansion, the invented template parameter is a template
3545 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3546 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3547 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3548 parameter_pack_p
= true;
3553 if (DECL_PACK_P (t
))
3555 /* We don't want to walk into the type of a PARM_DECL,
3556 because we don't want to see the type parameter pack. */
3558 parameter_pack_p
= true;
3562 /* Look through a lambda capture proxy to the field pack. */
3564 if (DECL_HAS_VALUE_EXPR_P (t
))
3566 tree v
= DECL_VALUE_EXPR (t
);
3568 &find_parameter_packs_r
,
3572 else if (variable_template_specialization_p (t
))
3574 cp_walk_tree (&DECL_TI_ARGS (t
),
3575 find_parameter_packs_r
,
3582 if (builtin_pack_call_p (t
))
3583 parameter_pack_p
= true;
3587 parameter_pack_p
= true;
3590 /* Not a parameter pack. */
3594 if (parameter_pack_p
)
3596 /* Add this parameter pack to the list. */
3597 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3601 cp_walk_tree (&TYPE_CONTEXT (t
),
3602 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3604 /* This switch statement will return immediately if we don't find a
3606 switch (TREE_CODE (t
))
3608 case TEMPLATE_PARM_INDEX
:
3611 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3612 /* Check the template itself. */
3613 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3614 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3615 /* Check the template arguments. */
3616 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3621 case TEMPLATE_TYPE_PARM
:
3622 case TEMPLATE_TEMPLATE_PARM
:
3629 /* Ignore the declaration of a capture proxy for a parameter pack. */
3630 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3635 if (TYPE_PTRMEMFUNC_P (t
))
3641 if (TYPE_TEMPLATE_INFO (t
))
3642 cp_walk_tree (&TYPE_TI_ARGS (t
),
3643 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3649 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3654 cp_walk_tree (&TREE_TYPE (t
),
3655 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3659 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3664 case TYPE_PACK_EXPANSION
:
3665 case EXPR_PACK_EXPANSION
:
3670 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3675 case IDENTIFIER_NODE
:
3676 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3683 tree fn
= lambda_function (t
);
3684 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3692 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3693 type_pack_expansion_p to false so that any placeholders
3694 within the expression don't get marked as parameter packs. */
3695 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3696 ppd
->type_pack_expansion_p
= false;
3697 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3699 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3711 /* Determines if the expression or type T uses any parameter packs. */
3713 uses_parameter_packs (tree t
)
3715 tree parameter_packs
= NULL_TREE
;
3716 struct find_parameter_pack_data ppd
;
3717 ppd
.parameter_packs
= ¶meter_packs
;
3718 ppd
.visited
= new hash_set
<tree
>;
3719 ppd
.type_pack_expansion_p
= false;
3720 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3722 return parameter_packs
!= NULL_TREE
;
3725 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3726 representation a base-class initializer into a parameter pack
3727 expansion. If all goes well, the resulting node will be an
3728 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3731 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3734 tree parameter_packs
= NULL_TREE
;
3735 bool for_types
= false;
3736 struct find_parameter_pack_data ppd
;
3738 if (!arg
|| arg
== error_mark_node
)
3741 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3743 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3744 class initializer. In this case, the TREE_PURPOSE will be a
3745 _TYPE node (representing the base class expansion we're
3746 initializing) and the TREE_VALUE will be a TREE_LIST
3747 containing the initialization arguments.
3749 The resulting expansion looks somewhat different from most
3750 expansions. Rather than returning just one _EXPANSION, we
3751 return a TREE_LIST whose TREE_PURPOSE is a
3752 TYPE_PACK_EXPANSION containing the bases that will be
3753 initialized. The TREE_VALUE will be identical to the
3754 original TREE_VALUE, which is a list of arguments that will
3755 be passed to each base. We do not introduce any new pack
3756 expansion nodes into the TREE_VALUE (although it is possible
3757 that some already exist), because the TREE_PURPOSE and
3758 TREE_VALUE all need to be expanded together with the same
3759 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3760 resulting TREE_PURPOSE will mention the parameter packs in
3761 both the bases and the arguments to the bases. */
3764 tree parameter_packs
= NULL_TREE
;
3766 /* Determine which parameter packs will be used by the base
3768 ppd
.visited
= new hash_set
<tree
>;
3769 ppd
.parameter_packs
= ¶meter_packs
;
3770 ppd
.type_pack_expansion_p
= true;
3771 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3772 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3775 if (parameter_packs
== NULL_TREE
)
3777 if (complain
& tf_error
)
3778 error ("base initializer expansion %qT contains no parameter packs",
3781 return error_mark_node
;
3784 if (TREE_VALUE (arg
) != void_type_node
)
3786 /* Collect the sets of parameter packs used in each of the
3787 initialization arguments. */
3788 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3790 /* Determine which parameter packs will be expanded in this
3792 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3799 /* Create the pack expansion type for the base type. */
3800 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3801 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3802 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3803 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3805 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3806 they will rarely be compared to anything. */
3807 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3809 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3812 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3815 /* Build the PACK_EXPANSION_* node. */
3817 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3818 : make_node (EXPR_PACK_EXPANSION
);
3819 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3820 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3822 /* Propagate type and const-expression information. */
3823 TREE_TYPE (result
) = TREE_TYPE (arg
);
3824 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3825 /* Mark this read now, since the expansion might be length 0. */
3826 mark_exp_read (arg
);
3829 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3830 they will rarely be compared to anything. */
3831 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3833 /* Determine which parameter packs will be expanded. */
3834 ppd
.parameter_packs
= ¶meter_packs
;
3835 ppd
.visited
= new hash_set
<tree
>;
3836 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3837 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3840 /* Make sure we found some parameter packs. */
3841 if (parameter_packs
== NULL_TREE
)
3843 if (complain
& tf_error
)
3846 error ("expansion pattern %qT contains no argument packs", arg
);
3848 error ("expansion pattern %qE contains no argument packs", arg
);
3850 return error_mark_node
;
3852 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3854 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3859 /* Checks T for any "bare" parameter packs, which have not yet been
3860 expanded, and issues an error if any are found. This operation can
3861 only be done on full expressions or types (e.g., an expression
3862 statement, "if" condition, etc.), because we could have expressions like:
3864 foo(f(g(h(args)))...)
3866 where "args" is a parameter pack. check_for_bare_parameter_packs
3867 should not be called for the subexpressions args, h(args),
3868 g(h(args)), or f(g(h(args))), because we would produce erroneous
3871 Returns TRUE and emits an error if there were bare parameter packs,
3872 returns FALSE otherwise. */
3874 check_for_bare_parameter_packs (tree t
)
3876 tree parameter_packs
= NULL_TREE
;
3877 struct find_parameter_pack_data ppd
;
3879 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3882 /* A lambda might use a parameter pack from the containing context. */
3883 if (current_function_decl
&& LAMBDA_FUNCTION_P (current_function_decl
))
3886 if (TREE_CODE (t
) == TYPE_DECL
)
3889 ppd
.parameter_packs
= ¶meter_packs
;
3890 ppd
.visited
= new hash_set
<tree
>;
3891 ppd
.type_pack_expansion_p
= false;
3892 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3895 if (parameter_packs
)
3897 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3898 error_at (loc
, "parameter packs not expanded with %<...%>:");
3899 while (parameter_packs
)
3901 tree pack
= TREE_VALUE (parameter_packs
);
3902 tree name
= NULL_TREE
;
3904 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3905 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3906 name
= TYPE_NAME (pack
);
3907 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3908 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3909 else if (TREE_CODE (pack
) == CALL_EXPR
)
3910 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3912 name
= DECL_NAME (pack
);
3915 inform (loc
, " %qD", name
);
3917 inform (loc
, " <anonymous>");
3919 parameter_packs
= TREE_CHAIN (parameter_packs
);
3928 /* Expand any parameter packs that occur in the template arguments in
3931 expand_template_argument_pack (tree args
)
3933 if (args
== error_mark_node
)
3934 return error_mark_node
;
3936 tree result_args
= NULL_TREE
;
3937 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3938 int num_result_args
= -1;
3939 int non_default_args_count
= -1;
3941 /* First, determine if we need to expand anything, and the number of
3942 slots we'll need. */
3943 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3945 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3946 if (arg
== NULL_TREE
)
3948 if (ARGUMENT_PACK_P (arg
))
3950 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3951 if (num_result_args
< 0)
3952 num_result_args
= in_arg
+ num_packed
;
3954 num_result_args
+= num_packed
;
3958 if (num_result_args
>= 0)
3963 /* If no expansion is necessary, we're done. */
3964 if (num_result_args
< 0)
3967 /* Expand arguments. */
3968 result_args
= make_tree_vec (num_result_args
);
3969 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3970 non_default_args_count
=
3971 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3972 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3974 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3975 if (ARGUMENT_PACK_P (arg
))
3977 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3978 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3979 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3980 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3981 if (non_default_args_count
> 0)
3982 non_default_args_count
+= num_packed
- 1;
3986 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3990 if (non_default_args_count
>= 0)
3991 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3995 /* Checks if DECL shadows a template parameter.
3997 [temp.local]: A template-parameter shall not be redeclared within its
3998 scope (including nested scopes).
4000 Emits an error and returns TRUE if the DECL shadows a parameter,
4001 returns FALSE otherwise. */
4004 check_template_shadow (tree decl
)
4008 /* If we're not in a template, we can't possibly shadow a template
4010 if (!current_template_parms
)
4013 /* Figure out what we're shadowing. */
4014 decl
= OVL_FIRST (decl
);
4015 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4017 /* If there's no previous binding for this name, we're not shadowing
4018 anything, let alone a template parameter. */
4022 /* If we're not shadowing a template parameter, we're done. Note
4023 that OLDDECL might be an OVERLOAD (or perhaps even an
4024 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4026 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4029 /* We check for decl != olddecl to avoid bogus errors for using a
4030 name inside a class. We check TPFI to avoid duplicate errors for
4031 inline member templates. */
4033 || (DECL_TEMPLATE_PARM_P (decl
)
4034 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4037 /* Don't complain about the injected class name, as we've already
4038 complained about the class itself. */
4039 if (DECL_SELF_REFERENCE_P (decl
))
4042 if (DECL_TEMPLATE_PARM_P (decl
))
4043 error ("declaration of template parameter %q+D shadows "
4044 "template parameter", decl
);
4046 error ("declaration of %q+#D shadows template parameter", decl
);
4047 inform (DECL_SOURCE_LOCATION (olddecl
),
4048 "template parameter %qD declared here", olddecl
);
4052 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4053 ORIG_LEVEL, DECL, and TYPE. */
4056 build_template_parm_index (int index
,
4062 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4063 TEMPLATE_PARM_IDX (t
) = index
;
4064 TEMPLATE_PARM_LEVEL (t
) = level
;
4065 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4066 TEMPLATE_PARM_DECL (t
) = decl
;
4067 TREE_TYPE (t
) = type
;
4068 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4069 TREE_READONLY (t
) = TREE_READONLY (decl
);
4074 /* Find the canonical type parameter for the given template type
4075 parameter. Returns the canonical type parameter, which may be TYPE
4076 if no such parameter existed. */
4079 canonical_type_parameter (tree type
)
4082 int idx
= TEMPLATE_TYPE_IDX (type
);
4083 if (!canonical_template_parms
)
4084 vec_alloc (canonical_template_parms
, idx
+ 1);
4086 if (canonical_template_parms
->length () <= (unsigned) idx
)
4087 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4089 list
= (*canonical_template_parms
)[idx
];
4090 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4091 list
= TREE_CHAIN (list
);
4094 return TREE_VALUE (list
);
4097 (*canonical_template_parms
)[idx
]
4098 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4103 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4104 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4105 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4106 new one is created. */
4109 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4110 tsubst_flags_t complain
)
4112 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4113 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4114 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4115 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4117 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4120 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4121 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4122 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4123 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4124 DECL_ARTIFICIAL (decl
) = 1;
4125 SET_DECL_TEMPLATE_PARM_P (decl
);
4127 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4128 TEMPLATE_PARM_LEVEL (index
) - levels
,
4129 TEMPLATE_PARM_ORIG_LEVEL (index
),
4131 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4132 TEMPLATE_PARM_PARAMETER_PACK (t
)
4133 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4135 /* Template template parameters need this. */
4136 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4138 DECL_TEMPLATE_RESULT (decl
)
4139 = build_decl (DECL_SOURCE_LOCATION (decl
),
4140 TYPE_DECL
, DECL_NAME (decl
), type
);
4141 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4142 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4143 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4147 return TEMPLATE_PARM_DESCENDANTS (index
);
4150 /* Process information from new template parameter PARM and append it
4151 to the LIST being built. This new parameter is a non-type
4152 parameter iff IS_NON_TYPE is true. This new parameter is a
4153 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4157 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4158 bool is_non_type
, bool is_parameter_pack
)
4163 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4164 tree defval
= TREE_PURPOSE (parm
);
4165 tree constr
= TREE_TYPE (parm
);
4169 tree p
= tree_last (list
);
4171 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4174 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4175 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4177 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4185 parm
= TREE_VALUE (parm
);
4187 SET_DECL_TEMPLATE_PARM_P (parm
);
4189 if (TREE_TYPE (parm
) != error_mark_node
)
4193 The top-level cv-qualifiers on the template-parameter are
4194 ignored when determining its type. */
4195 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4196 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4197 TREE_TYPE (parm
) = error_mark_node
;
4198 else if (uses_parameter_packs (TREE_TYPE (parm
))
4199 && !is_parameter_pack
4200 /* If we're in a nested template parameter list, the template
4201 template parameter could be a parameter pack. */
4202 && processing_template_parmlist
== 1)
4204 /* This template parameter is not a parameter pack, but it
4205 should be. Complain about "bare" parameter packs. */
4206 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4208 /* Recover by calling this a parameter pack. */
4209 is_parameter_pack
= true;
4213 /* A template parameter is not modifiable. */
4214 TREE_CONSTANT (parm
) = 1;
4215 TREE_READONLY (parm
) = 1;
4216 decl
= build_decl (parm_loc
,
4217 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4218 TREE_CONSTANT (decl
) = 1;
4219 TREE_READONLY (decl
) = 1;
4220 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4221 = build_template_parm_index (idx
, processing_template_decl
,
4222 processing_template_decl
,
4223 decl
, TREE_TYPE (parm
));
4225 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4226 = is_parameter_pack
;
4231 parm
= TREE_VALUE (TREE_VALUE (parm
));
4233 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4235 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4236 /* This is for distinguishing between real templates and template
4237 template parameters */
4238 TREE_TYPE (parm
) = t
;
4239 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4244 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4245 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4246 decl
= build_decl (parm_loc
,
4247 TYPE_DECL
, parm
, t
);
4250 TYPE_NAME (t
) = decl
;
4251 TYPE_STUB_DECL (t
) = decl
;
4253 TEMPLATE_TYPE_PARM_INDEX (t
)
4254 = build_template_parm_index (idx
, processing_template_decl
,
4255 processing_template_decl
,
4256 decl
, TREE_TYPE (parm
));
4257 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4258 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4260 DECL_ARTIFICIAL (decl
) = 1;
4261 SET_DECL_TEMPLATE_PARM_P (decl
);
4263 /* Build requirements for the type/template parameter.
4264 This must be done after SET_DECL_TEMPLATE_PARM_P or
4265 process_template_parm could fail. */
4266 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4270 /* Build the parameter node linking the parameter declaration,
4271 its default argument (if any), and its constraints (if any). */
4272 parm
= build_tree_list (defval
, parm
);
4273 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4275 return chainon (list
, parm
);
4278 /* The end of a template parameter list has been reached. Process the
4279 tree list into a parameter vector, converting each parameter into a more
4280 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4284 end_template_parm_list (tree parms
)
4288 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4290 /* Pop the dummy parameter level and add the real one. */
4291 current_template_parms
= TREE_CHAIN (current_template_parms
);
4293 current_template_parms
4294 = tree_cons (size_int (processing_template_decl
),
4295 saved_parmlist
, current_template_parms
);
4297 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4299 next
= TREE_CHAIN (parm
);
4300 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4301 TREE_CHAIN (parm
) = NULL_TREE
;
4304 --processing_template_parmlist
;
4306 return saved_parmlist
;
4309 // Explicitly indicate the end of the template parameter list. We assume
4310 // that the current template parameters have been constructed and/or
4311 // managed explicitly, as when creating new template template parameters
4312 // from a shorthand constraint.
4314 end_template_parm_list ()
4316 --processing_template_parmlist
;
4319 /* end_template_decl is called after a template declaration is seen. */
4322 end_template_decl (void)
4324 reset_specialization ();
4326 if (! processing_template_decl
)
4329 /* This matches the pushlevel in begin_template_parm_list. */
4332 --processing_template_decl
;
4333 current_template_parms
= TREE_CHAIN (current_template_parms
);
4336 /* Takes a TREE_LIST representing a template parameter and convert it
4337 into an argument suitable to be passed to the type substitution
4338 functions. Note that If the TREE_LIST contains an error_mark
4339 node, the returned argument is error_mark_node. */
4342 template_parm_to_arg (tree t
)
4346 || TREE_CODE (t
) != TREE_LIST
)
4349 if (error_operand_p (TREE_VALUE (t
)))
4350 return error_mark_node
;
4354 if (TREE_CODE (t
) == TYPE_DECL
4355 || TREE_CODE (t
) == TEMPLATE_DECL
)
4359 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4361 /* Turn this argument into a TYPE_ARGUMENT_PACK
4362 with a single element, which expands T. */
4363 tree vec
= make_tree_vec (1);
4365 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4367 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4369 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4370 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4375 t
= DECL_INITIAL (t
);
4377 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4379 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4380 with a single element, which expands T. */
4381 tree vec
= make_tree_vec (1);
4383 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4385 t
= convert_from_reference (t
);
4386 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4388 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4389 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4392 t
= convert_from_reference (t
);
4397 /* Given a single level of template parameters (a TREE_VEC), return it
4398 as a set of template arguments. */
4401 template_parms_level_to_args (tree parms
)
4403 tree a
= copy_node (parms
);
4404 TREE_TYPE (a
) = NULL_TREE
;
4405 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4406 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4409 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4414 /* Given a set of template parameters, return them as a set of template
4415 arguments. The template parameters are represented as a TREE_VEC, in
4416 the form documented in cp-tree.h for template arguments. */
4419 template_parms_to_args (tree parms
)
4422 tree args
= NULL_TREE
;
4423 int length
= TMPL_PARMS_DEPTH (parms
);
4426 /* If there is only one level of template parameters, we do not
4427 create a TREE_VEC of TREE_VECs. Instead, we return a single
4428 TREE_VEC containing the arguments. */
4430 args
= make_tree_vec (length
);
4432 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4434 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4437 TREE_VEC_ELT (args
, --l
) = a
;
4445 /* Within the declaration of a template, return the currently active
4446 template parameters as an argument TREE_VEC. */
4449 current_template_args (void)
4451 return template_parms_to_args (current_template_parms
);
4454 /* Update the declared TYPE by doing any lookups which were thought to be
4455 dependent, but are not now that we know the SCOPE of the declarator. */
4458 maybe_update_decl_type (tree orig_type
, tree scope
)
4460 tree type
= orig_type
;
4462 if (type
== NULL_TREE
)
4465 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4466 type
= TREE_TYPE (type
);
4468 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4469 && dependent_type_p (type
)
4470 /* Don't bother building up the args in this case. */
4471 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4473 /* tsubst in the args corresponding to the template parameters,
4474 including auto if present. Most things will be unchanged, but
4475 make_typename_type and tsubst_qualified_id will resolve
4476 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4477 tree args
= current_template_args ();
4478 tree auto_node
= type_uses_auto (type
);
4482 tree auto_vec
= make_tree_vec (1);
4483 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4484 args
= add_to_template_args (args
, auto_vec
);
4486 pushed
= push_scope (scope
);
4487 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4492 if (type
== error_mark_node
)
4495 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4497 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4500 type
= TYPE_NAME (type
);
4505 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4506 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4507 the new template is a member template. */
4510 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4512 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4513 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4514 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4515 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4516 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4521 struct template_parm_data
4523 /* The level of the template parameters we are currently
4527 /* The index of the specialization argument we are currently
4531 /* An array whose size is the number of template parameters. The
4532 elements are nonzero if the parameter has been used in any one
4533 of the arguments processed so far. */
4536 /* An array whose size is the number of template arguments. The
4537 elements are nonzero if the argument makes use of template
4538 parameters of this level. */
4539 int* arg_uses_template_parms
;
4542 /* Subroutine of push_template_decl used to see if each template
4543 parameter in a partial specialization is used in the explicit
4544 argument list. If T is of the LEVEL given in DATA (which is
4545 treated as a template_parm_data*), then DATA->PARMS is marked
4549 mark_template_parm (tree t
, void* data
)
4553 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4555 template_parm_level_and_index (t
, &level
, &idx
);
4557 if (level
== tpd
->level
)
4559 tpd
->parms
[idx
] = 1;
4560 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4563 /* In C++17 the type of a non-type argument is a deduced context. */
4564 if (cxx_dialect
>= cxx17
4565 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4566 for_each_template_parm (TREE_TYPE (t
),
4567 &mark_template_parm
,
4570 /*include_nondeduced_p=*/false);
4572 /* Return zero so that for_each_template_parm will continue the
4573 traversal of the tree; we want to mark *every* template parm. */
4577 /* Process the partial specialization DECL. */
4580 process_partial_specialization (tree decl
)
4582 tree type
= TREE_TYPE (decl
);
4583 tree tinfo
= get_template_info (decl
);
4584 tree maintmpl
= TI_TEMPLATE (tinfo
);
4585 tree specargs
= TI_ARGS (tinfo
);
4586 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4587 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4590 int nargs
= TREE_VEC_LENGTH (inner_args
);
4593 bool did_error_intro
= false;
4594 struct template_parm_data tpd
;
4595 struct template_parm_data tpd2
;
4597 gcc_assert (current_template_parms
);
4599 /* A concept cannot be specialized. */
4600 if (flag_concepts
&& variable_concept_p (maintmpl
))
4602 error ("specialization of variable concept %q#D", maintmpl
);
4603 return error_mark_node
;
4606 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4607 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4609 /* We check that each of the template parameters given in the
4610 partial specialization is used in the argument list to the
4611 specialization. For example:
4613 template <class T> struct S;
4614 template <class T> struct S<T*>;
4616 The second declaration is OK because `T*' uses the template
4617 parameter T, whereas
4619 template <class T> struct S<int>;
4621 is no good. Even trickier is:
4632 The S2<T> declaration is actually invalid; it is a
4633 full-specialization. Of course,
4636 struct S2<T (*)(U)>;
4638 or some such would have been OK. */
4639 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4640 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4641 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4643 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4644 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4645 for (i
= 0; i
< nargs
; ++i
)
4647 tpd
.current_arg
= i
;
4648 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4649 &mark_template_parm
,
4652 /*include_nondeduced_p=*/false);
4654 for (i
= 0; i
< ntparms
; ++i
)
4655 if (tpd
.parms
[i
] == 0)
4657 /* One of the template parms was not used in a deduced context in the
4659 if (!did_error_intro
)
4661 error ("template parameters not deducible in "
4662 "partial specialization:");
4663 did_error_intro
= true;
4666 inform (input_location
, " %qD",
4667 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4670 if (did_error_intro
)
4671 return error_mark_node
;
4673 /* [temp.class.spec]
4675 The argument list of the specialization shall not be identical to
4676 the implicit argument list of the primary template. */
4678 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4679 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4681 || !strictly_subsumes (current_template_constraints (),
4682 get_constraints (maintmpl
))))
4685 error ("partial specialization %q+D does not specialize "
4686 "any template arguments", decl
);
4688 error ("partial specialization %q+D does not specialize any "
4689 "template arguments and is not more constrained than", decl
);
4690 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4693 /* A partial specialization that replaces multiple parameters of the
4694 primary template with a pack expansion is less specialized for those
4696 if (nargs
< DECL_NTPARMS (maintmpl
))
4698 error ("partial specialization is not more specialized than the "
4699 "primary template because it replaces multiple parameters "
4700 "with a pack expansion");
4701 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4702 /* Avoid crash in process_partial_specialization. */
4706 /* If we aren't in a dependent class, we can actually try deduction. */
4707 else if (tpd
.level
== 1
4708 /* FIXME we should be able to handle a partial specialization of a
4709 partial instantiation, but currently we can't (c++/41727). */
4710 && TMPL_ARGS_DEPTH (specargs
) == 1
4711 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4713 if (permerror (input_location
, "partial specialization %qD is not "
4714 "more specialized than", decl
))
4715 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4719 /* [temp.class.spec]
4721 A partially specialized non-type argument expression shall not
4722 involve template parameters of the partial specialization except
4723 when the argument expression is a simple identifier.
4725 The type of a template parameter corresponding to a specialized
4726 non-type argument shall not be dependent on a parameter of the
4729 Also, we verify that pack expansions only occur at the
4730 end of the argument list. */
4731 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4733 for (i
= 0; i
< nargs
; ++i
)
4735 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4736 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4737 tree packed_args
= NULL_TREE
;
4740 if (ARGUMENT_PACK_P (arg
))
4742 /* Extract the arguments from the argument pack. We'll be
4743 iterating over these in the following loop. */
4744 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4745 len
= TREE_VEC_LENGTH (packed_args
);
4748 for (j
= 0; j
< len
; j
++)
4751 /* Get the Jth argument in the parameter pack. */
4752 arg
= TREE_VEC_ELT (packed_args
, j
);
4754 if (PACK_EXPANSION_P (arg
))
4756 /* Pack expansions must come at the end of the
4758 if ((packed_args
&& j
< len
- 1)
4759 || (!packed_args
&& i
< nargs
- 1))
4761 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4762 error ("parameter pack argument %qE must be at the "
4763 "end of the template argument list", arg
);
4765 error ("parameter pack argument %qT must be at the "
4766 "end of the template argument list", arg
);
4770 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4771 /* We only care about the pattern. */
4772 arg
= PACK_EXPANSION_PATTERN (arg
);
4774 if (/* These first two lines are the `non-type' bit. */
4776 && TREE_CODE (arg
) != TEMPLATE_DECL
4777 /* This next two lines are the `argument expression is not just a
4778 simple identifier' condition and also the `specialized
4779 non-type argument' bit. */
4780 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4781 && !(REFERENCE_REF_P (arg
)
4782 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4784 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4785 || (packed_args
&& uses_template_parms (arg
)))
4786 error ("template argument %qE involves template parameter(s)",
4790 /* Look at the corresponding template parameter,
4791 marking which template parameters its type depends
4793 tree type
= TREE_TYPE (parm
);
4797 /* We haven't yet initialized TPD2. Do so now. */
4798 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4799 /* The number of parameters here is the number in the
4800 main template, which, as checked in the assertion
4802 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4804 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4807 /* Mark the template parameters. But this time, we're
4808 looking for the template parameters of the main
4809 template, not in the specialization. */
4810 tpd2
.current_arg
= i
;
4811 tpd2
.arg_uses_template_parms
[i
] = 0;
4812 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4813 for_each_template_parm (type
,
4814 &mark_template_parm
,
4817 /*include_nondeduced_p=*/false);
4819 if (tpd2
.arg_uses_template_parms
[i
])
4821 /* The type depended on some template parameters.
4822 If they are fully specialized in the
4823 specialization, that's OK. */
4826 for (j
= 0; j
< nargs
; ++j
)
4827 if (tpd2
.parms
[j
] != 0
4828 && tpd
.arg_uses_template_parms
[j
])
4831 error_n (input_location
, count
,
4832 "type %qT of template argument %qE depends "
4833 "on a template parameter",
4834 "type %qT of template argument %qE depends "
4835 "on template parameters",
4844 /* We should only get here once. */
4845 if (TREE_CODE (decl
) == TYPE_DECL
)
4846 gcc_assert (!COMPLETE_TYPE_P (type
));
4848 // Build the template decl.
4849 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4850 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4851 TREE_TYPE (tmpl
) = type
;
4852 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4853 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4854 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4855 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4857 /* Give template template parms a DECL_CONTEXT of the template
4858 for which they are a parameter. */
4859 for (i
= 0; i
< ntparms
; ++i
)
4861 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4862 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4863 DECL_CONTEXT (parm
) = tmpl
;
4867 /* We didn't register this in check_explicit_specialization so we could
4868 wait until the constraints were set. */
4869 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4871 associate_classtype_constraints (type
);
4873 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4874 = tree_cons (specargs
, tmpl
,
4875 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4876 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4878 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4879 inst
= TREE_CHAIN (inst
))
4881 tree instance
= TREE_VALUE (inst
);
4882 if (TYPE_P (instance
)
4883 ? (COMPLETE_TYPE_P (instance
)
4884 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4885 : DECL_TEMPLATE_INSTANTIATION (instance
))
4887 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4888 tree inst_decl
= (DECL_P (instance
)
4889 ? instance
: TYPE_NAME (instance
));
4892 else if (spec
== error_mark_node
)
4893 permerror (input_location
,
4894 "declaration of %qD ambiguates earlier template "
4895 "instantiation for %qD", decl
, inst_decl
);
4896 else if (TREE_VALUE (spec
) == tmpl
)
4897 permerror (input_location
,
4898 "partial specialization of %qD after instantiation "
4899 "of %qD", decl
, inst_decl
);
4906 /* PARM is a template parameter of some form; return the corresponding
4907 TEMPLATE_PARM_INDEX. */
4910 get_template_parm_index (tree parm
)
4912 if (TREE_CODE (parm
) == PARM_DECL
4913 || TREE_CODE (parm
) == CONST_DECL
)
4914 parm
= DECL_INITIAL (parm
);
4915 else if (TREE_CODE (parm
) == TYPE_DECL
4916 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4917 parm
= TREE_TYPE (parm
);
4918 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4919 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4920 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4921 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4922 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4926 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4927 parameter packs used by the template parameter PARM. */
4930 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4932 /* A type parm can't refer to another parm. */
4933 if (TREE_CODE (parm
) == TYPE_DECL
)
4935 else if (TREE_CODE (parm
) == PARM_DECL
)
4937 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4942 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4944 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4945 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4946 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4949 /* PARM is a template parameter pack. Return any parameter packs used in
4950 its type or the type of any of its template parameters. If there are
4951 any such packs, it will be instantiated into a fixed template parameter
4952 list by partial instantiation rather than be fully deduced. */
4955 fixed_parameter_pack_p (tree parm
)
4957 /* This can only be true in a member template. */
4958 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4960 /* This can only be true for a parameter pack. */
4961 if (!template_parameter_pack_p (parm
))
4963 /* A type parm can't refer to another parm. */
4964 if (TREE_CODE (parm
) == TYPE_DECL
)
4967 tree parameter_packs
= NULL_TREE
;
4968 struct find_parameter_pack_data ppd
;
4969 ppd
.parameter_packs
= ¶meter_packs
;
4970 ppd
.visited
= new hash_set
<tree
>;
4971 ppd
.type_pack_expansion_p
= false;
4973 fixed_parameter_pack_p_1 (parm
, &ppd
);
4976 return parameter_packs
;
4979 /* Check that a template declaration's use of default arguments and
4980 parameter packs is not invalid. Here, PARMS are the template
4981 parameters. IS_PRIMARY is true if DECL is the thing declared by
4982 a primary template. IS_PARTIAL is true if DECL is a partial
4985 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
4986 function template declaration or a friend class template
4987 declaration. In the function case, 1 indicates a declaration, 2
4988 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4989 emitted for extraneous default arguments.
4991 Returns TRUE if there were no errors found, FALSE otherwise. */
4994 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4995 bool is_partial
, int is_friend_decl
)
4998 int last_level_to_check
;
5000 bool no_errors
= true;
5004 A default template-argument shall not be specified in a
5005 function template declaration or a function template definition, nor
5006 in the template-parameter-list of the definition of a member of a
5009 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5010 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5011 /* You can't have a function template declaration in a local
5012 scope, nor you can you define a member of a class template in a
5016 if ((TREE_CODE (decl
) == TYPE_DECL
5018 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5019 || (TREE_CODE (decl
) == FUNCTION_DECL
5020 && LAMBDA_FUNCTION_P (decl
)))
5021 /* A lambda doesn't have an explicit declaration; don't complain
5022 about the parms of the enclosing class. */
5025 if (current_class_type
5026 && !TYPE_BEING_DEFINED (current_class_type
)
5027 && DECL_LANG_SPECIFIC (decl
)
5028 && DECL_DECLARES_FUNCTION_P (decl
)
5029 /* If this is either a friend defined in the scope of the class
5030 or a member function. */
5031 && (DECL_FUNCTION_MEMBER_P (decl
)
5032 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5033 : DECL_FRIEND_CONTEXT (decl
)
5034 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5036 /* And, if it was a member function, it really was defined in
5037 the scope of the class. */
5038 && (!DECL_FUNCTION_MEMBER_P (decl
)
5039 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5040 /* We already checked these parameters when the template was
5041 declared, so there's no need to do it again now. This function
5042 was defined in class scope, but we're processing its body now
5043 that the class is complete. */
5046 /* Core issue 226 (C++0x only): the following only applies to class
5049 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5053 If a template-parameter has a default template-argument, all
5054 subsequent template-parameters shall have a default
5055 template-argument supplied. */
5056 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5058 tree inner_parms
= TREE_VALUE (parm_level
);
5059 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5060 int seen_def_arg_p
= 0;
5063 for (i
= 0; i
< ntparms
; ++i
)
5065 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5067 if (parm
== error_mark_node
)
5070 if (TREE_PURPOSE (parm
))
5072 else if (seen_def_arg_p
5073 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5075 error ("no default argument for %qD", TREE_VALUE (parm
));
5076 /* For better subsequent error-recovery, we indicate that
5077 there should have been a default argument. */
5078 TREE_PURPOSE (parm
) = error_mark_node
;
5081 else if (!is_partial
5083 /* Don't complain about an enclosing partial
5085 && parm_level
== parms
5086 && TREE_CODE (decl
) == TYPE_DECL
5088 && template_parameter_pack_p (TREE_VALUE (parm
))
5089 /* A fixed parameter pack will be partially
5090 instantiated into a fixed length list. */
5091 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5093 /* A primary class template can only have one
5094 parameter pack, at the end of the template
5097 error ("parameter pack %q+D must be at the end of the"
5098 " template parameter list", TREE_VALUE (parm
));
5100 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5108 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5112 /* For an ordinary class template, default template arguments are
5113 allowed at the innermost level, e.g.:
5114 template <class T = int>
5116 but, in a partial specialization, they're not allowed even
5117 there, as we have in [temp.class.spec]:
5119 The template parameter list of a specialization shall not
5120 contain default template argument values.
5122 So, for a partial specialization, or for a function template
5123 (in C++98/C++03), we look at all of them. */
5126 /* But, for a primary class template that is not a partial
5127 specialization we look at all template parameters except the
5129 parms
= TREE_CHAIN (parms
);
5131 /* Figure out what error message to issue. */
5132 if (is_friend_decl
== 2)
5133 msg
= G_("default template arguments may not be used in function template "
5134 "friend re-declaration");
5135 else if (is_friend_decl
)
5136 msg
= G_("default template arguments may not be used in template "
5137 "friend declarations");
5138 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5139 msg
= G_("default template arguments may not be used in function templates "
5140 "without -std=c++11 or -std=gnu++11");
5141 else if (is_partial
)
5142 msg
= G_("default template arguments may not be used in "
5143 "partial specializations");
5144 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5145 msg
= G_("default argument for template parameter for class enclosing %qD");
5147 /* Per [temp.param]/9, "A default template-argument shall not be
5148 specified in the template-parameter-lists of the definition of
5149 a member of a class template that appears outside of the member's
5150 class.", thus if we aren't handling a member of a class template
5151 there is no need to examine the parameters. */
5154 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5155 /* If we're inside a class definition, there's no need to
5156 examine the parameters to the class itself. On the one
5157 hand, they will be checked when the class is defined, and,
5158 on the other, default arguments are valid in things like:
5159 template <class T = double>
5160 struct S { template <class U> void f(U); };
5161 Here the default argument for `S' has no bearing on the
5162 declaration of `f'. */
5163 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5165 /* Check everything. */
5166 last_level_to_check
= 0;
5168 for (parm_level
= parms
;
5169 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5170 parm_level
= TREE_CHAIN (parm_level
))
5172 tree inner_parms
= TREE_VALUE (parm_level
);
5176 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5177 for (i
= 0; i
< ntparms
; ++i
)
5179 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5182 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5187 if (is_friend_decl
== 2)
5194 /* Clear out the default argument so that we are not
5196 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5200 /* At this point, if we're still interested in issuing messages,
5201 they must apply to classes surrounding the object declared. */
5203 msg
= G_("default argument for template parameter for class "
5210 /* Worker for push_template_decl_real, called via
5211 for_each_template_parm. DATA is really an int, indicating the
5212 level of the parameters we are interested in. If T is a template
5213 parameter of that level, return nonzero. */
5216 template_parm_this_level_p (tree t
, void* data
)
5218 int this_level
= *(int *)data
;
5221 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5222 level
= TEMPLATE_PARM_LEVEL (t
);
5224 level
= TEMPLATE_TYPE_LEVEL (t
);
5225 return level
== this_level
;
5228 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5229 DATA is really an int, indicating the innermost outer level of parameters.
5230 If T is a template parameter of that level or further out, return
5234 template_parm_outer_level (tree t
, void *data
)
5236 int this_level
= *(int *)data
;
5239 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5240 level
= TEMPLATE_PARM_LEVEL (t
);
5242 level
= TEMPLATE_TYPE_LEVEL (t
);
5243 return level
<= this_level
;
5246 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5247 parameters given by current_template_args, or reuses a
5248 previously existing one, if appropriate. Returns the DECL, or an
5249 equivalent one, if it is replaced via a call to duplicate_decls.
5251 If IS_FRIEND is true, DECL is a friend declaration. */
5254 push_template_decl_real (tree decl
, bool is_friend
)
5262 int new_template_p
= 0;
5263 /* True if the template is a member template, in the sense of
5265 bool member_template_p
= false;
5267 if (decl
== error_mark_node
|| !current_template_parms
)
5268 return error_mark_node
;
5270 /* See if this is a partial specialization. */
5271 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5272 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5273 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5275 && DECL_LANG_SPECIFIC (decl
)
5276 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5277 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5279 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5283 /* For a friend, we want the context of the friend, not
5284 the type of which it is a friend. */
5285 ctx
= CP_DECL_CONTEXT (decl
);
5286 else if (CP_DECL_CONTEXT (decl
)
5287 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5288 /* In the case of a virtual function, we want the class in which
5290 ctx
= CP_DECL_CONTEXT (decl
);
5292 /* Otherwise, if we're currently defining some class, the DECL
5293 is assumed to be a member of the class. */
5294 ctx
= current_scope ();
5296 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5299 if (!DECL_CONTEXT (decl
))
5300 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5302 /* See if this is a primary template. */
5303 if (is_friend
&& ctx
5304 && uses_template_parms_level (ctx
, processing_template_decl
))
5305 /* A friend template that specifies a class context, i.e.
5306 template <typename T> friend void A<T>::f();
5309 else if (TREE_CODE (decl
) == TYPE_DECL
5310 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5313 is_primary
= template_parm_scope_p ();
5317 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5319 if (DECL_CLASS_SCOPE_P (decl
))
5320 member_template_p
= true;
5321 if (TREE_CODE (decl
) == TYPE_DECL
5322 && anon_aggrname_p (DECL_NAME (decl
)))
5324 error ("template class without a name");
5325 return error_mark_node
;
5327 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5329 if (member_template_p
)
5331 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5332 error ("member template %qD may not have virt-specifiers", decl
);
5334 if (DECL_DESTRUCTOR_P (decl
))
5338 A destructor shall not be a member template. */
5339 error ("destructor %qD declared as member template", decl
);
5340 return error_mark_node
;
5342 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5343 && (!prototype_p (TREE_TYPE (decl
))
5344 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5345 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5346 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5347 == void_list_node
)))
5349 /* [basic.stc.dynamic.allocation]
5351 An allocation function can be a function
5352 template. ... Template allocation functions shall
5353 have two or more parameters. */
5354 error ("invalid template declaration of %qD", decl
);
5355 return error_mark_node
;
5358 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5359 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5361 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5362 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5363 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5365 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5366 if (TREE_CODE (t
) == TYPE_DECL
)
5368 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5369 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5372 else if (TREE_CODE (decl
) == TYPE_DECL
5373 && TYPE_DECL_ALIAS_P (decl
))
5374 /* alias-declaration */
5375 gcc_assert (!DECL_ARTIFICIAL (decl
));
5376 else if (VAR_P (decl
))
5377 /* C++14 variable template. */;
5380 error ("template declaration of %q#D", decl
);
5381 return error_mark_node
;
5385 /* Check to see that the rules regarding the use of default
5386 arguments are not being violated. We check args for a friend
5387 functions when we know whether it's a definition, introducing
5388 declaration or re-declaration. */
5389 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5390 check_default_tmpl_args (decl
, current_template_parms
,
5391 is_primary
, is_partial
, is_friend
);
5393 /* Ensure that there are no parameter packs in the type of this
5394 declaration that have not been expanded. */
5395 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5397 /* Check each of the arguments individually to see if there are
5398 any bare parameter packs. */
5399 tree type
= TREE_TYPE (decl
);
5400 tree arg
= DECL_ARGUMENTS (decl
);
5401 tree argtype
= TYPE_ARG_TYPES (type
);
5403 while (arg
&& argtype
)
5405 if (!DECL_PACK_P (arg
)
5406 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5408 /* This is a PARM_DECL that contains unexpanded parameter
5409 packs. We have already complained about this in the
5410 check_for_bare_parameter_packs call, so just replace
5411 these types with ERROR_MARK_NODE. */
5412 TREE_TYPE (arg
) = error_mark_node
;
5413 TREE_VALUE (argtype
) = error_mark_node
;
5416 arg
= DECL_CHAIN (arg
);
5417 argtype
= TREE_CHAIN (argtype
);
5420 /* Check for bare parameter packs in the return type and the
5421 exception specifiers. */
5422 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5423 /* Errors were already issued, set return type to int
5424 as the frontend doesn't expect error_mark_node as
5426 TREE_TYPE (type
) = integer_type_node
;
5427 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5428 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5430 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5431 && TYPE_DECL_ALIAS_P (decl
))
5432 ? DECL_ORIGINAL_TYPE (decl
)
5433 : TREE_TYPE (decl
)))
5435 TREE_TYPE (decl
) = error_mark_node
;
5436 return error_mark_node
;
5440 return process_partial_specialization (decl
);
5442 args
= current_template_args ();
5445 || TREE_CODE (ctx
) == FUNCTION_DECL
5446 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5447 || (TREE_CODE (decl
) == TYPE_DECL
5448 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5449 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5451 if (DECL_LANG_SPECIFIC (decl
)
5452 && DECL_TEMPLATE_INFO (decl
)
5453 && DECL_TI_TEMPLATE (decl
))
5454 tmpl
= DECL_TI_TEMPLATE (decl
);
5455 /* If DECL is a TYPE_DECL for a class-template, then there won't
5456 be DECL_LANG_SPECIFIC. The information equivalent to
5457 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5458 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5459 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5460 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5462 /* Since a template declaration already existed for this
5463 class-type, we must be redeclaring it here. Make sure
5464 that the redeclaration is valid. */
5465 redeclare_class_template (TREE_TYPE (decl
),
5466 current_template_parms
,
5467 current_template_constraints ());
5468 /* We don't need to create a new TEMPLATE_DECL; just use the
5469 one we already had. */
5470 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5474 tmpl
= build_template_decl (decl
, current_template_parms
,
5478 if (DECL_LANG_SPECIFIC (decl
)
5479 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5481 /* A specialization of a member template of a template
5483 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5484 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5485 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5491 tree a
, t
, current
, parms
;
5493 tree tinfo
= get_template_info (decl
);
5497 error ("template definition of non-template %q#D", decl
);
5498 return error_mark_node
;
5501 tmpl
= TI_TEMPLATE (tinfo
);
5503 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5504 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5505 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5506 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5510 /* The declaration is a specialization of a member
5511 template, declared outside the class. Therefore, the
5512 innermost template arguments will be NULL, so we
5513 replace them with the arguments determined by the
5514 earlier call to check_explicit_specialization. */
5515 args
= DECL_TI_ARGS (decl
);
5518 = build_template_decl (decl
, current_template_parms
,
5520 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5521 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5522 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5523 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5524 DECL_TEMPLATE_INFO (new_tmpl
)
5525 = build_template_info (tmpl
, args
);
5527 register_specialization (new_tmpl
,
5528 most_general_template (tmpl
),
5534 /* Make sure the template headers we got make sense. */
5536 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5537 i
= TMPL_PARMS_DEPTH (parms
);
5538 if (TMPL_ARGS_DEPTH (args
) != i
)
5540 error ("expected %d levels of template parms for %q#D, got %d",
5541 i
, decl
, TMPL_ARGS_DEPTH (args
));
5542 DECL_INTERFACE_KNOWN (decl
) = 1;
5543 return error_mark_node
;
5546 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5548 a
= TMPL_ARGS_LEVEL (args
, i
);
5549 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5551 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5553 if (current
== decl
)
5554 error ("got %d template parameters for %q#D",
5555 TREE_VEC_LENGTH (a
), decl
);
5557 error ("got %d template parameters for %q#T",
5558 TREE_VEC_LENGTH (a
), current
);
5559 error (" but %d required", TREE_VEC_LENGTH (t
));
5560 /* Avoid crash in import_export_decl. */
5561 DECL_INTERFACE_KNOWN (decl
) = 1;
5562 return error_mark_node
;
5565 if (current
== decl
)
5567 else if (current
== NULL_TREE
)
5568 /* Can happen in erroneous input. */
5571 current
= get_containing_scope (current
);
5574 /* Check that the parms are used in the appropriate qualifying scopes
5575 in the declarator. */
5576 if (!comp_template_args
5578 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5580 error ("template arguments to %qD do not match original "
5581 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5582 if (!uses_template_parms (TI_ARGS (tinfo
)))
5583 inform (input_location
, "use %<template<>%> for"
5584 " an explicit specialization");
5585 /* Avoid crash in import_export_decl. */
5586 DECL_INTERFACE_KNOWN (decl
) = 1;
5587 return error_mark_node
;
5591 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5592 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5594 /* Push template declarations for global functions and types. Note
5595 that we do not try to push a global template friend declared in a
5596 template class; such a thing may well depend on the template
5597 parameters of the class. */
5598 if (new_template_p
&& !ctx
5599 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5601 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5602 if (tmpl
== error_mark_node
)
5603 return error_mark_node
;
5605 /* Hide template friend classes that haven't been declared yet. */
5606 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5608 DECL_ANTICIPATED (tmpl
) = 1;
5609 DECL_FRIEND_P (tmpl
) = 1;
5615 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5617 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5619 /* Give template template parms a DECL_CONTEXT of the template
5620 for which they are a parameter. */
5621 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5622 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5624 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5625 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5626 DECL_CONTEXT (parm
) = tmpl
;
5629 if (TREE_CODE (decl
) == TYPE_DECL
5630 && TYPE_DECL_ALIAS_P (decl
)
5631 && complex_alias_template_p (tmpl
))
5632 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5635 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5636 back to its most general template. If TMPL is a specialization,
5637 ARGS may only have the innermost set of arguments. Add the missing
5638 argument levels if necessary. */
5639 if (DECL_TEMPLATE_INFO (tmpl
))
5640 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5642 info
= build_template_info (tmpl
, args
);
5644 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5645 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5649 retrofit_lang_decl (decl
);
5650 if (DECL_LANG_SPECIFIC (decl
))
5651 DECL_TEMPLATE_INFO (decl
) = info
;
5654 if (flag_implicit_templates
5656 && TREE_PUBLIC (decl
)
5657 && VAR_OR_FUNCTION_DECL_P (decl
))
5658 /* Set DECL_COMDAT on template instantiations; if we force
5659 them to be emitted by explicit instantiation or -frepo,
5660 mark_needed will tell cgraph to do the right thing. */
5661 DECL_COMDAT (decl
) = true;
5663 return DECL_TEMPLATE_RESULT (tmpl
);
5667 push_template_decl (tree decl
)
5669 return push_template_decl_real (decl
, false);
5672 /* FN is an inheriting constructor that inherits from the constructor
5673 template INHERITED; turn FN into a constructor template with a matching
5677 add_inherited_template_parms (tree fn
, tree inherited
)
5680 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5681 inner_parms
= copy_node (inner_parms
);
5683 = tree_cons (size_int (processing_template_decl
+ 1),
5684 inner_parms
, current_template_parms
);
5685 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5686 tree args
= template_parms_to_args (parms
);
5687 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5688 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5689 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5690 DECL_ARTIFICIAL (tmpl
) = true;
5691 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5695 /* Called when a class template TYPE is redeclared with the indicated
5696 template PARMS, e.g.:
5698 template <class T> struct S;
5699 template <class T> struct S {}; */
5702 redeclare_class_template (tree type
, tree parms
, tree cons
)
5708 if (!TYPE_TEMPLATE_INFO (type
))
5710 error ("%qT is not a template type", type
);
5714 tmpl
= TYPE_TI_TEMPLATE (type
);
5715 if (!PRIMARY_TEMPLATE_P (tmpl
))
5716 /* The type is nested in some template class. Nothing to worry
5717 about here; there are no new template parameters for the nested
5723 error ("template specifiers not specified in declaration of %qD",
5728 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5729 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5731 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5733 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5734 "redeclared with %d template parameter",
5735 "redeclared with %d template parameters",
5736 TREE_VEC_LENGTH (parms
));
5737 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5738 "previous declaration %qD used %d template parameter",
5739 "previous declaration %qD used %d template parameters",
5740 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5744 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5751 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5752 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5755 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5756 if (error_operand_p (tmpl_parm
))
5759 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5760 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5761 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5763 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5765 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5766 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5767 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5768 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5769 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5770 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5771 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5772 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5773 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5775 error ("template parameter %q+#D", tmpl_parm
);
5776 error ("redeclared here as %q#D", parm
);
5780 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5782 /* We have in [temp.param]:
5784 A template-parameter may not be given default arguments
5785 by two different declarations in the same scope. */
5786 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5787 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5788 "original definition appeared here");
5792 if (parm_default
!= NULL_TREE
)
5793 /* Update the previous template parameters (which are the ones
5794 that will really count) with the new default value. */
5795 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5796 else if (tmpl_default
!= NULL_TREE
)
5797 /* Update the new parameters, too; they'll be used as the
5798 parameters for any members. */
5799 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5801 /* Give each template template parm in this redeclaration a
5802 DECL_CONTEXT of the template for which they are a parameter. */
5803 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5805 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5806 DECL_CONTEXT (parm
) = tmpl
;
5809 if (TREE_CODE (parm
) == TYPE_DECL
)
5810 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5813 // Cannot redeclare a class template with a different set of constraints.
5814 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5816 error_at (input_location
, "redeclaration %q#D with different "
5817 "constraints", tmpl
);
5818 inform (DECL_SOURCE_LOCATION (tmpl
),
5819 "original declaration appeared here");
5825 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5826 to be used when the caller has already checked
5827 (processing_template_decl
5828 && !instantiation_dependent_expression_p (expr)
5829 && potential_constant_expression (expr))
5830 and cleared processing_template_decl. */
5833 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5835 return tsubst_copy_and_build (expr
,
5838 /*in_decl=*/NULL_TREE
,
5839 /*function_p=*/false,
5840 /*integral_constant_expression_p=*/true);
5843 /* Simplify EXPR if it is a non-dependent expression. Returns the
5844 (possibly simplified) expression. */
5847 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5849 if (expr
== NULL_TREE
)
5852 /* If we're in a template, but EXPR isn't value dependent, simplify
5853 it. We're supposed to treat:
5855 template <typename T> void f(T[1 + 1]);
5856 template <typename T> void f(T[2]);
5858 as two declarations of the same function, for example. */
5859 if (processing_template_decl
5860 && is_nondependent_constant_expression (expr
))
5862 processing_template_decl_sentinel s
;
5863 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5869 instantiate_non_dependent_expr (tree expr
)
5871 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5874 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5875 an uninstantiated expression. */
5878 instantiate_non_dependent_or_null (tree expr
)
5880 if (expr
== NULL_TREE
)
5882 if (processing_template_decl
)
5884 if (!is_nondependent_constant_expression (expr
))
5888 processing_template_decl_sentinel s
;
5889 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5895 /* True iff T is a specialization of a variable template. */
5898 variable_template_specialization_p (tree t
)
5900 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5902 tree tmpl
= DECL_TI_TEMPLATE (t
);
5903 return variable_template_p (tmpl
);
5906 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5907 template declaration, or a TYPE_DECL for an alias declaration. */
5910 alias_type_or_template_p (tree t
)
5914 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5917 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5918 || DECL_ALIAS_TEMPLATE_P (t
));
5921 /* Return TRUE iff T is a specialization of an alias template. */
5924 alias_template_specialization_p (const_tree t
)
5926 /* It's an alias template specialization if it's an alias and its
5927 TYPE_NAME is a specialization of a primary template. */
5928 if (TYPE_ALIAS_P (t
))
5929 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5930 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5935 /* An alias template is complex from a SFINAE perspective if a template-id
5936 using that alias can be ill-formed when the expansion is not, as with
5937 the void_t template. We determine this by checking whether the
5938 expansion for the alias template uses all its template parameters. */
5940 struct uses_all_template_parms_data
5947 uses_all_template_parms_r (tree t
, void *data_
)
5949 struct uses_all_template_parms_data
&data
5950 = *(struct uses_all_template_parms_data
*)data_
;
5951 tree idx
= get_template_parm_index (t
);
5953 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5954 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5959 complex_alias_template_p (const_tree tmpl
)
5961 struct uses_all_template_parms_data data
;
5962 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5963 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5964 data
.level
= TMPL_PARMS_DEPTH (parms
);
5965 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5966 data
.seen
= XALLOCAVEC (bool, len
);
5967 for (int i
= 0; i
< len
; ++i
)
5968 data
.seen
[i
] = false;
5970 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5971 for (int i
= 0; i
< len
; ++i
)
5977 /* Return TRUE iff T is a specialization of a complex alias template with
5978 dependent template-arguments. */
5981 dependent_alias_template_spec_p (const_tree t
)
5983 if (!alias_template_specialization_p (t
))
5986 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5987 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5990 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5991 if (!any_dependent_template_arguments_p (args
))
5997 /* Return the number of innermost template parameters in TMPL. */
6000 num_innermost_template_parms (tree tmpl
)
6002 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6003 return TREE_VEC_LENGTH (parms
);
6006 /* Return either TMPL or another template that it is equivalent to under DR
6007 1286: An alias that just changes the name of a template is equivalent to
6008 the other template. */
6011 get_underlying_template (tree tmpl
)
6013 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6014 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6016 /* Determine if the alias is equivalent to an underlying template. */
6017 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6018 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6022 tree underlying
= TI_TEMPLATE (tinfo
);
6023 if (!PRIMARY_TEMPLATE_P (underlying
)
6024 || (num_innermost_template_parms (tmpl
)
6025 != num_innermost_template_parms (underlying
)))
6028 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6029 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6030 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6033 /* Alias is equivalent. Strip it and repeat. */
6040 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6041 must be a reference-to-function or a pointer-to-function type, as specified
6042 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6043 and check that the resulting function has external linkage. */
6046 convert_nontype_argument_function (tree type
, tree expr
,
6047 tsubst_flags_t complain
)
6051 linkage_kind linkage
;
6053 fn
= instantiate_type (type
, fns
, tf_none
);
6054 if (fn
== error_mark_node
)
6055 return error_mark_node
;
6057 if (value_dependent_expression_p (fn
))
6060 fn_no_ptr
= strip_fnptr_conv (fn
);
6061 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6062 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6063 if (BASELINK_P (fn_no_ptr
))
6064 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6066 /* [temp.arg.nontype]/1
6068 A template-argument for a non-type, non-template template-parameter
6071 -- the address of an object or function with external [C++11: or
6072 internal] linkage. */
6074 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6076 if (complain
& tf_error
)
6078 error ("%qE is not a valid template argument for type %qT",
6080 if (TYPE_PTR_P (type
))
6081 inform (input_location
, "it must be the address of a function "
6082 "with external linkage");
6084 inform (input_location
, "it must be the name of a function with "
6085 "external linkage");
6090 linkage
= decl_linkage (fn_no_ptr
);
6091 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6093 if (complain
& tf_error
)
6095 if (cxx_dialect
>= cxx11
)
6096 error ("%qE is not a valid template argument for type %qT "
6097 "because %qD has no linkage",
6098 expr
, type
, fn_no_ptr
);
6100 error ("%qE is not a valid template argument for type %qT "
6101 "because %qD does not have external linkage",
6102 expr
, type
, fn_no_ptr
);
6108 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6109 fn
= build_address (fn
);
6110 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6111 fn
= build_nop (type
, fn
);
6116 /* Subroutine of convert_nontype_argument.
6117 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6118 Emit an error otherwise. */
6121 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6122 tsubst_flags_t complain
)
6124 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6125 tree orig_expr
= expr
;
6127 if (null_ptr_cst_p (expr
))
6129 if (TREE_CODE (expr
) == PTRMEM_CST
6130 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6131 PTRMEM_CST_CLASS (expr
)))
6133 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6135 if (processing_template_decl
6136 && TREE_CODE (expr
) == ADDR_EXPR
6137 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6139 if (complain
& tf_error
)
6141 error_at (loc
, "%qE is not a valid template argument for type %qT",
6143 if (TREE_CODE (expr
) != PTRMEM_CST
)
6144 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6146 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6151 /* Returns TRUE iff the address of OP is value-dependent.
6153 14.6.2.4 [temp.dep.temp]:
6154 A non-integral non-type template-argument is dependent if its type is
6155 dependent or it has either of the following forms
6158 and contains a nested-name-specifier which specifies a class-name that
6159 names a dependent type.
6161 We generalize this to just say that the address of a member of a
6162 dependent class is value-dependent; the above doesn't cover the
6163 address of a static data member named with an unqualified-id. */
6166 has_value_dependent_address (tree op
)
6168 /* We could use get_inner_reference here, but there's no need;
6169 this is only relevant for template non-type arguments, which
6170 can only be expressed as &id-expression. */
6173 tree ctx
= CP_DECL_CONTEXT (op
);
6174 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6181 /* The next set of functions are used for providing helpful explanatory
6182 diagnostics for failed overload resolution. Their messages should be
6183 indented by two spaces for consistency with the messages in
6187 unify_success (bool /*explain_p*/)
6192 /* Other failure functions should call this one, to provide a single function
6193 for setting a breakpoint on. */
6196 unify_invalid (bool /*explain_p*/)
6202 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6205 inform (input_location
,
6206 " couldn't deduce template parameter %qD", parm
);
6207 return unify_invalid (explain_p
);
6211 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6214 inform (input_location
,
6215 " types %qT and %qT have incompatible cv-qualifiers",
6217 return unify_invalid (explain_p
);
6221 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6224 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6225 return unify_invalid (explain_p
);
6229 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6232 inform (input_location
,
6233 " template parameter %qD is not a parameter pack, but "
6236 return unify_invalid (explain_p
);
6240 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6243 inform (input_location
,
6244 " template argument %qE does not match "
6245 "pointer-to-member constant %qE",
6247 return unify_invalid (explain_p
);
6251 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6254 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6255 return unify_invalid (explain_p
);
6259 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6262 inform (input_location
,
6263 " inconsistent parameter pack deduction with %qT and %qT",
6265 return unify_invalid (explain_p
);
6269 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6274 inform (input_location
,
6275 " deduced conflicting types for parameter %qT (%qT and %qT)",
6276 parm
, first
, second
);
6278 inform (input_location
,
6279 " deduced conflicting values for non-type parameter "
6280 "%qE (%qE and %qE)", parm
, first
, second
);
6282 return unify_invalid (explain_p
);
6286 unify_vla_arg (bool explain_p
, tree arg
)
6289 inform (input_location
,
6290 " variable-sized array type %qT is not "
6291 "a valid template argument",
6293 return unify_invalid (explain_p
);
6297 unify_method_type_error (bool explain_p
, tree arg
)
6300 inform (input_location
,
6301 " member function type %qT is not a valid template argument",
6303 return unify_invalid (explain_p
);
6307 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6312 inform_n (input_location
, wanted
,
6313 " candidate expects at least %d argument, %d provided",
6314 " candidate expects at least %d arguments, %d provided",
6317 inform_n (input_location
, wanted
,
6318 " candidate expects %d argument, %d provided",
6319 " candidate expects %d arguments, %d provided",
6322 return unify_invalid (explain_p
);
6326 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6328 return unify_arity (explain_p
, have
, wanted
);
6332 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6333 bool least_p
= false)
6335 return unify_arity (explain_p
, have
, wanted
, least_p
);
6339 unify_arg_conversion (bool explain_p
, tree to_type
,
6340 tree from_type
, tree arg
)
6343 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6344 " cannot convert %qE (type %qT) to type %qT",
6345 arg
, from_type
, to_type
);
6346 return unify_invalid (explain_p
);
6350 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6351 tree parm
, tree arg
)
6356 case tbr_ambiguous_baseclass
:
6357 inform (input_location
, " %qT is an ambiguous base class of %qT",
6361 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6364 return unify_invalid (explain_p
);
6368 unify_inconsistent_template_template_parameters (bool explain_p
)
6371 inform (input_location
,
6372 " template parameters of a template template argument are "
6373 "inconsistent with other deduced template arguments");
6374 return unify_invalid (explain_p
);
6378 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6381 inform (input_location
,
6382 " can't deduce a template for %qT from non-template type %qT",
6384 return unify_invalid (explain_p
);
6388 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6391 inform (input_location
,
6392 " template argument %qE does not match %qE", arg
, parm
);
6393 return unify_invalid (explain_p
);
6396 /* Attempt to convert the non-type template parameter EXPR to the
6397 indicated TYPE. If the conversion is successful, return the
6398 converted value. If the conversion is unsuccessful, return
6399 NULL_TREE if we issued an error message, or error_mark_node if we
6400 did not. We issue error messages for out-and-out bad template
6401 parameters, but not simply because the conversion failed, since we
6402 might be just trying to do argument deduction. Both TYPE and EXPR
6403 must be non-dependent.
6405 The conversion follows the special rules described in
6406 [temp.arg.nontype], and it is much more strict than an implicit
6409 This function is called twice for each template argument (see
6410 lookup_template_class for a more accurate description of this
6411 problem). This means that we need to handle expressions which
6412 are not valid in a C++ source, but can be created from the
6413 first call (for instance, casts to perform conversions). These
6414 hacks can go away after we fix the double coercion problem. */
6417 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6420 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6421 tree orig_expr
= expr
;
6423 /* Detect immediately string literals as invalid non-type argument.
6424 This special-case is not needed for correctness (we would easily
6425 catch this later), but only to provide better diagnostic for this
6426 common user mistake. As suggested by DR 100, we do not mention
6427 linkage issues in the diagnostic as this is not the point. */
6428 /* FIXME we're making this OK. */
6429 if (TREE_CODE (expr
) == STRING_CST
)
6431 if (complain
& tf_error
)
6432 error ("%qE is not a valid template argument for type %qT "
6433 "because string literals can never be used in this context",
6438 /* Add the ADDR_EXPR now for the benefit of
6439 value_dependent_expression_p. */
6440 if (TYPE_PTROBV_P (type
)
6441 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6443 expr
= decay_conversion (expr
, complain
);
6444 if (expr
== error_mark_node
)
6445 return error_mark_node
;
6448 /* If we are in a template, EXPR may be non-dependent, but still
6449 have a syntactic, rather than semantic, form. For example, EXPR
6450 might be a SCOPE_REF, rather than the VAR_DECL to which the
6451 SCOPE_REF refers. Preserving the qualifying scope is necessary
6452 so that access checking can be performed when the template is
6453 instantiated -- but here we need the resolved form so that we can
6454 convert the argument. */
6455 bool non_dep
= false;
6456 if (TYPE_REF_OBJ_P (type
)
6457 && has_value_dependent_address (expr
))
6458 /* If we want the address and it's value-dependent, don't fold. */;
6459 else if (processing_template_decl
6460 && is_nondependent_constant_expression (expr
))
6462 if (error_operand_p (expr
))
6463 return error_mark_node
;
6464 expr_type
= TREE_TYPE (expr
);
6466 /* If the argument is non-dependent, perform any conversions in
6467 non-dependent context as well. */
6468 processing_template_decl_sentinel
s (non_dep
);
6470 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6472 if (value_dependent_expression_p (expr
))
6473 expr
= canonicalize_expr_argument (expr
, complain
);
6475 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6476 to a non-type argument of "nullptr". */
6477 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6478 expr
= fold_simple (convert (type
, expr
));
6480 /* In C++11, integral or enumeration non-type template arguments can be
6481 arbitrary constant expressions. Pointer and pointer to
6482 member arguments can be general constant expressions that evaluate
6483 to a null value, but otherwise still need to be of a specific form. */
6484 if (cxx_dialect
>= cxx11
)
6486 if (TREE_CODE (expr
) == PTRMEM_CST
)
6487 /* A PTRMEM_CST is already constant, and a valid template
6488 argument for a parameter of pointer to member type, we just want
6489 to leave it in that form rather than lower it to a
6491 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6492 || cxx_dialect
>= cxx17
)
6494 /* C++17: A template-argument for a non-type template-parameter shall
6495 be a converted constant expression (8.20) of the type of the
6496 template-parameter. */
6497 expr
= build_converted_constant_expr (type
, expr
, complain
);
6498 if (expr
== error_mark_node
)
6499 return error_mark_node
;
6500 expr
= maybe_constant_value (expr
);
6501 expr
= convert_from_reference (expr
);
6503 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6505 tree folded
= maybe_constant_value (expr
);
6506 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6507 : null_member_pointer_value_p (folded
))
6512 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6513 expr
= mark_lvalue_use (expr
);
6515 expr
= mark_rvalue_use (expr
);
6517 /* HACK: Due to double coercion, we can get a
6518 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6519 which is the tree that we built on the first call (see
6520 below when coercing to reference to object or to reference to
6521 function). We just strip everything and get to the arg.
6522 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6524 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6526 tree probe_type
, probe
= expr
;
6527 if (REFERENCE_REF_P (probe
))
6528 probe
= TREE_OPERAND (probe
, 0);
6529 probe_type
= TREE_TYPE (probe
);
6530 if (TREE_CODE (probe
) == NOP_EXPR
)
6532 /* ??? Maybe we could use convert_from_reference here, but we
6533 would need to relax its constraints because the NOP_EXPR
6534 could actually change the type to something more cv-qualified,
6535 and this is not folded by convert_from_reference. */
6536 tree addr
= TREE_OPERAND (probe
, 0);
6537 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6538 && TREE_CODE (addr
) == ADDR_EXPR
6539 && TYPE_PTR_P (TREE_TYPE (addr
))
6540 && (same_type_ignoring_top_level_qualifiers_p
6541 (TREE_TYPE (probe_type
),
6542 TREE_TYPE (TREE_TYPE (addr
)))))
6544 expr
= TREE_OPERAND (addr
, 0);
6545 expr_type
= TREE_TYPE (probe_type
);
6550 /* [temp.arg.nontype]/5, bullet 1
6552 For a non-type template-parameter of integral or enumeration type,
6553 integral promotions (_conv.prom_) and integral conversions
6554 (_conv.integral_) are applied. */
6555 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6557 if (cxx_dialect
< cxx11
)
6559 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6560 t
= maybe_constant_value (t
);
6561 if (t
!= error_mark_node
)
6565 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6566 return error_mark_node
;
6568 /* Notice that there are constant expressions like '4 % 0' which
6569 do not fold into integer constants. */
6570 if (TREE_CODE (expr
) != INTEGER_CST
6571 && !value_dependent_expression_p (expr
))
6573 if (complain
& tf_error
)
6575 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6576 if (!require_potential_constant_expression (expr
))
6577 expr
= error_mark_node
;
6579 expr
= cxx_constant_value (expr
);
6580 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6581 inform (loc
, "in template argument for type %qT ", type
);
6582 if (expr
== error_mark_node
)
6584 /* else cxx_constant_value complained but gave us
6585 a real constant, so go ahead. */
6586 if (TREE_CODE (expr
) != INTEGER_CST
)
6588 /* Some assemble time constant expressions like
6589 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6590 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6591 as we can emit them into .rodata initializers of
6592 variables, yet they can't fold into an INTEGER_CST at
6593 compile time. Refuse them here. */
6594 gcc_checking_assert (reduced_constant_expression_p (expr
));
6595 error_at (loc
, "template argument %qE for type %qT not "
6596 "a constant integer", expr
, type
);
6604 /* Avoid typedef problems. */
6605 if (TREE_TYPE (expr
) != type
)
6606 expr
= fold_convert (type
, expr
);
6608 /* [temp.arg.nontype]/5, bullet 2
6610 For a non-type template-parameter of type pointer to object,
6611 qualification conversions (_conv.qual_) and the array-to-pointer
6612 conversion (_conv.array_) are applied. */
6613 else if (TYPE_PTROBV_P (type
))
6615 tree decayed
= expr
;
6617 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6618 decay_conversion or an explicit cast. If it's a problematic cast,
6619 we'll complain about it below. */
6620 if (TREE_CODE (expr
) == NOP_EXPR
)
6624 if (TREE_CODE (probe
) == ADDR_EXPR
6625 && TYPE_PTR_P (TREE_TYPE (probe
)))
6628 expr_type
= TREE_TYPE (expr
);
6632 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6634 A template-argument for a non-type, non-template template-parameter
6635 shall be one of: [...]
6637 -- the name of a non-type template-parameter;
6638 -- the address of an object or function with external linkage, [...]
6639 expressed as "& id-expression" where the & is optional if the name
6640 refers to a function or array, or if the corresponding
6641 template-parameter is a reference.
6643 Here, we do not care about functions, as they are invalid anyway
6644 for a parameter of type pointer-to-object. */
6646 if (value_dependent_expression_p (expr
))
6647 /* Non-type template parameters are OK. */
6649 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6650 /* Null pointer values are OK in C++11. */;
6651 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6655 if (complain
& tf_error
)
6656 error ("%qD is not a valid template argument "
6657 "because %qD is a variable, not the address of "
6658 "a variable", orig_expr
, expr
);
6661 if (POINTER_TYPE_P (expr_type
))
6663 if (complain
& tf_error
)
6664 error ("%qE is not a valid template argument for %qT "
6665 "because it is not the address of a variable",
6669 /* Other values, like integer constants, might be valid
6670 non-type arguments of some other type. */
6671 return error_mark_node
;
6675 tree decl
= TREE_OPERAND (expr
, 0);
6679 if (complain
& tf_error
)
6680 error ("%qE is not a valid template argument of type %qT "
6681 "because %qE is not a variable", orig_expr
, type
, decl
);
6684 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6686 if (complain
& tf_error
)
6687 error ("%qE is not a valid template argument of type %qT "
6688 "because %qD does not have external linkage",
6689 orig_expr
, type
, decl
);
6692 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6693 && decl_linkage (decl
) == lk_none
)
6695 if (complain
& tf_error
)
6696 error ("%qE is not a valid template argument of type %qT "
6697 "because %qD has no linkage", orig_expr
, type
, decl
);
6700 /* C++17: For a non-type template-parameter of reference or pointer
6701 type, the value of the constant expression shall not refer to (or
6702 for a pointer type, shall not be the address of):
6703 * a subobject (4.5),
6704 * a temporary object (15.2),
6705 * a string literal (5.13.5),
6706 * the result of a typeid expression (8.2.8), or
6707 * a predefined __func__ variable (11.4.1). */
6708 else if (DECL_ARTIFICIAL (decl
))
6710 if (complain
& tf_error
)
6711 error ("the address of %qD is not a valid template argument",
6715 else if (!same_type_ignoring_top_level_qualifiers_p
6716 (strip_array_types (TREE_TYPE (type
)),
6717 strip_array_types (TREE_TYPE (decl
))))
6719 if (complain
& tf_error
)
6720 error ("the address of the %qT subobject of %qD is not a "
6721 "valid template argument", TREE_TYPE (type
), decl
);
6724 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6726 if (complain
& tf_error
)
6727 error ("the address of %qD is not a valid template argument "
6728 "because it does not have static storage duration",
6736 expr
= perform_qualification_conversions (type
, expr
);
6737 if (expr
== error_mark_node
)
6738 return error_mark_node
;
6740 /* [temp.arg.nontype]/5, bullet 3
6742 For a non-type template-parameter of type reference to object, no
6743 conversions apply. The type referred to by the reference may be more
6744 cv-qualified than the (otherwise identical) type of the
6745 template-argument. The template-parameter is bound directly to the
6746 template-argument, which must be an lvalue. */
6747 else if (TYPE_REF_OBJ_P (type
))
6749 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6751 return error_mark_node
;
6753 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6755 if (complain
& tf_error
)
6756 error ("%qE is not a valid template argument for type %qT "
6757 "because of conflicts in cv-qualification", expr
, type
);
6761 if (!lvalue_p (expr
))
6763 if (complain
& tf_error
)
6764 error ("%qE is not a valid template argument for type %qT "
6765 "because it is not an lvalue", expr
, type
);
6769 /* [temp.arg.nontype]/1
6771 A template-argument for a non-type, non-template template-parameter
6772 shall be one of: [...]
6774 -- the address of an object or function with external linkage. */
6775 if (INDIRECT_REF_P (expr
)
6776 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6778 expr
= TREE_OPERAND (expr
, 0);
6781 if (complain
& tf_error
)
6782 error ("%q#D is not a valid template argument for type %qT "
6783 "because a reference variable does not have a constant "
6784 "address", expr
, type
);
6789 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6790 && value_dependent_expression_p (expr
))
6791 /* OK, dependent reference. We don't want to ask whether a DECL is
6792 itself value-dependent, since what we want here is its address. */;
6797 if (complain
& tf_error
)
6798 error ("%qE is not a valid template argument for type %qT "
6799 "because it is not an object with linkage",
6804 /* DR 1155 allows internal linkage in C++11 and up. */
6805 linkage_kind linkage
= decl_linkage (expr
);
6806 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6808 if (complain
& tf_error
)
6809 error ("%qE is not a valid template argument for type %qT "
6810 "because object %qD does not have linkage",
6815 expr
= build_address (expr
);
6818 if (!same_type_p (type
, TREE_TYPE (expr
)))
6819 expr
= build_nop (type
, expr
);
6821 /* [temp.arg.nontype]/5, bullet 4
6823 For a non-type template-parameter of type pointer to function, only
6824 the function-to-pointer conversion (_conv.func_) is applied. If the
6825 template-argument represents a set of overloaded functions (or a
6826 pointer to such), the matching function is selected from the set
6828 else if (TYPE_PTRFN_P (type
))
6830 /* If the argument is a template-id, we might not have enough
6831 context information to decay the pointer. */
6832 if (!type_unknown_p (expr_type
))
6834 expr
= decay_conversion (expr
, complain
);
6835 if (expr
== error_mark_node
)
6836 return error_mark_node
;
6839 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6840 /* Null pointer values are OK in C++11. */
6841 return perform_qualification_conversions (type
, expr
);
6843 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6844 if (!expr
|| expr
== error_mark_node
)
6847 /* [temp.arg.nontype]/5, bullet 5
6849 For a non-type template-parameter of type reference to function, no
6850 conversions apply. If the template-argument represents a set of
6851 overloaded functions, the matching function is selected from the set
6853 else if (TYPE_REFFN_P (type
))
6855 if (TREE_CODE (expr
) == ADDR_EXPR
)
6857 if (complain
& tf_error
)
6859 error ("%qE is not a valid template argument for type %qT "
6860 "because it is a pointer", expr
, type
);
6861 inform (input_location
, "try using %qE instead",
6862 TREE_OPERAND (expr
, 0));
6867 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6868 if (!expr
|| expr
== error_mark_node
)
6871 /* [temp.arg.nontype]/5, bullet 6
6873 For a non-type template-parameter of type pointer to member function,
6874 no conversions apply. If the template-argument represents a set of
6875 overloaded member functions, the matching member function is selected
6876 from the set (_over.over_). */
6877 else if (TYPE_PTRMEMFUNC_P (type
))
6879 expr
= instantiate_type (type
, expr
, tf_none
);
6880 if (expr
== error_mark_node
)
6881 return error_mark_node
;
6883 /* [temp.arg.nontype] bullet 1 says the pointer to member
6884 expression must be a pointer-to-member constant. */
6885 if (!value_dependent_expression_p (expr
)
6886 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6889 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6890 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6891 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6892 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6894 /* [temp.arg.nontype]/5, bullet 7
6896 For a non-type template-parameter of type pointer to data member,
6897 qualification conversions (_conv.qual_) are applied. */
6898 else if (TYPE_PTRDATAMEM_P (type
))
6900 /* [temp.arg.nontype] bullet 1 says the pointer to member
6901 expression must be a pointer-to-member constant. */
6902 if (!value_dependent_expression_p (expr
)
6903 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6906 expr
= perform_qualification_conversions (type
, expr
);
6907 if (expr
== error_mark_node
)
6910 else if (NULLPTR_TYPE_P (type
))
6912 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6914 if (complain
& tf_error
)
6915 error ("%qE is not a valid template argument for type %qT "
6916 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6921 /* A template non-type parameter must be one of the above. */
6925 /* Sanity check: did we actually convert the argument to the
6927 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6928 (type
, TREE_TYPE (expr
)));
6929 return convert_from_reference (expr
);
6932 /* Subroutine of coerce_template_template_parms, which returns 1 if
6933 PARM_PARM and ARG_PARM match using the rule for the template
6934 parameters of template template parameters. Both PARM and ARG are
6935 template parameters; the rest of the arguments are the same as for
6936 coerce_template_template_parms.
6939 coerce_template_template_parm (tree parm
,
6941 tsubst_flags_t complain
,
6945 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6946 || parm
== NULL_TREE
|| error_operand_p (parm
))
6949 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6952 switch (TREE_CODE (parm
))
6955 /* We encounter instantiations of templates like
6956 template <template <template <class> class> class TT>
6959 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6960 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6962 if (!coerce_template_template_parms
6963 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6969 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6970 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6971 /* Argument is a parameter pack but parameter is not. */
6976 /* The tsubst call is used to handle cases such as
6978 template <int> class C {};
6979 template <class T, template <T> class TT> class D {};
6982 i.e. the parameter list of TT depends on earlier parameters. */
6983 if (!uses_template_parms (TREE_TYPE (arg
)))
6985 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6986 if (!uses_template_parms (t
)
6987 && !same_type_p (t
, TREE_TYPE (arg
)))
6991 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6992 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6993 /* Argument is a parameter pack but parameter is not. */
7005 /* Coerce template argument list ARGLIST for use with template
7006 template-parameter TEMPL. */
7009 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7010 tsubst_flags_t complain
)
7012 /* Consider an example where a template template parameter declared as
7014 template <class T, class U = std::allocator<T> > class TT
7016 The template parameter level of T and U are one level larger than
7017 of TT. To proper process the default argument of U, say when an
7018 instantiation `TT<int>' is seen, we need to build the full
7019 arguments containing {int} as the innermost level. Outer levels,
7020 available when not appearing as default template argument, can be
7021 obtained from the arguments of the enclosing template.
7023 Suppose that TT is later substituted with std::vector. The above
7024 instantiation is `TT<int, std::allocator<T> >' with TT at
7025 level 1, and T at level 2, while the template arguments at level 1
7026 becomes {std::vector} and the inner level 2 is {int}. */
7028 tree outer
= DECL_CONTEXT (templ
);
7031 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7032 /* We want arguments for the partial specialization, not arguments for
7033 the primary template. */
7034 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7036 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7038 else if (current_template_parms
)
7040 /* This is an argument of the current template, so we haven't set
7041 DECL_CONTEXT yet. */
7042 tree relevant_template_parms
;
7044 /* Parameter levels that are greater than the level of the given
7045 template template parm are irrelevant. */
7046 relevant_template_parms
= current_template_parms
;
7047 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7048 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7049 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7051 outer
= template_parms_to_args (relevant_template_parms
);
7055 arglist
= add_to_template_args (outer
, arglist
);
7057 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7058 return coerce_template_parms (parmlist
, arglist
, templ
,
7060 /*require_all_args=*/true,
7061 /*use_default_args=*/true);
7064 /* A cache of template template parameters with match-all default
7066 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7068 store_defaulted_ttp (tree v
, tree t
)
7070 if (!defaulted_ttp_cache
)
7071 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7072 defaulted_ttp_cache
->put (v
, t
);
7075 lookup_defaulted_ttp (tree v
)
7077 if (defaulted_ttp_cache
)
7078 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7083 /* T is a bound template template-parameter. Copy its arguments into default
7084 arguments of the template template-parameter's template parameters. */
7087 add_defaults_to_ttp (tree otmpl
)
7089 if (tree c
= lookup_defaulted_ttp (otmpl
))
7092 tree ntmpl
= copy_node (otmpl
);
7094 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7095 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7096 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7097 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7098 TYPE_NAME (ntype
) = ntmpl
;
7099 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7101 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7102 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7103 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7104 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7106 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7107 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7108 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7109 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7110 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7112 tree o
= TREE_VEC_ELT (vec
, i
);
7113 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7115 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7116 TREE_PURPOSE (n
) = any_targ_node
;
7120 store_defaulted_ttp (otmpl
, ntmpl
);
7124 /* ARG is a bound potential template template-argument, and PARGS is a list
7125 of arguments for the corresponding template template-parameter. Adjust
7126 PARGS as appropriate for application to ARG's template, and if ARG is a
7127 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7128 arguments to the template template parameter. */
7131 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7133 ++processing_template_decl
;
7134 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7135 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7137 /* When comparing two template template-parameters in partial ordering,
7138 rewrite the one currently being used as an argument to have default
7139 arguments for all parameters. */
7140 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7141 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7142 if (pargs
!= error_mark_node
)
7143 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7144 TYPE_TI_ARGS (arg
));
7149 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7150 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7151 /*require_all*/true,
7152 /*use_default*/true);
7154 --processing_template_decl
;
7158 /* Subroutine of unify for the case when PARM is a
7159 BOUND_TEMPLATE_TEMPLATE_PARM. */
7162 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7165 tree parmvec
= TYPE_TI_ARGS (parm
);
7166 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7168 /* The template template parm might be variadic and the argument
7169 not, so flatten both argument lists. */
7170 parmvec
= expand_template_argument_pack (parmvec
);
7171 argvec
= expand_template_argument_pack (argvec
);
7175 /* In keeping with P0522R0, adjust P's template arguments
7176 to apply to A's template; then flatten it again. */
7177 tree nparmvec
= parmvec
;
7178 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7179 nparmvec
= expand_template_argument_pack (nparmvec
);
7181 if (unify (tparms
, targs
, nparmvec
, argvec
,
7182 UNIFY_ALLOW_NONE
, explain_p
))
7185 /* If the P0522 adjustment eliminated a pack expansion, deduce
7188 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7189 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7190 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7195 /* Deduce arguments T, i from TT<T> or TT<i>.
7196 We check each element of PARMVEC and ARGVEC individually
7197 rather than the whole TREE_VEC since they can have
7198 different number of elements, which is allowed under N2555. */
7200 int len
= TREE_VEC_LENGTH (parmvec
);
7202 /* Check if the parameters end in a pack, making them
7204 int parm_variadic_p
= 0;
7206 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7207 parm_variadic_p
= 1;
7209 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7210 /* If the template argument list of P contains a pack
7211 expansion that is not the last template argument, the
7212 entire template argument list is a non-deduced
7214 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7215 return unify_success (explain_p
);
7217 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7218 return unify_too_few_arguments (explain_p
,
7219 TREE_VEC_LENGTH (argvec
), len
);
7221 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7222 if (unify (tparms
, targs
,
7223 TREE_VEC_ELT (parmvec
, i
),
7224 TREE_VEC_ELT (argvec
, i
),
7225 UNIFY_ALLOW_NONE
, explain_p
))
7229 && unify_pack_expansion (tparms
, targs
,
7232 /*subr=*/true, explain_p
))
7239 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7240 template template parameters. Both PARM_PARMS and ARG_PARMS are
7241 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7244 Consider the example:
7245 template <class T> class A;
7246 template<template <class U> class TT> class B;
7248 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7249 the parameters to A, and OUTER_ARGS contains A. */
7252 coerce_template_template_parms (tree parm_parms
,
7254 tsubst_flags_t complain
,
7258 int nparms
, nargs
, i
;
7262 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7263 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7265 nparms
= TREE_VEC_LENGTH (parm_parms
);
7266 nargs
= TREE_VEC_LENGTH (arg_parms
);
7270 /* P0522R0: A template template-parameter P is at least as specialized as
7271 a template template-argument A if, given the following rewrite to two
7272 function templates, the function template corresponding to P is at
7273 least as specialized as the function template corresponding to A
7274 according to the partial ordering rules for function templates
7275 ([temp.func.order]). Given an invented class template X with the
7276 template parameter list of A (including default arguments):
7278 * Each of the two function templates has the same template parameters,
7279 respectively, as P or A.
7281 * Each function template has a single function parameter whose type is
7282 a specialization of X with template arguments corresponding to the
7283 template parameters from the respective function template where, for
7284 each template parameter PP in the template parameter list of the
7285 function template, a corresponding template argument AA is formed. If
7286 PP declares a parameter pack, then AA is the pack expansion
7287 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7289 If the rewrite produces an invalid type, then P is not at least as
7290 specialized as A. */
7292 /* So coerce P's args to apply to A's parms, and then deduce between A's
7293 args and the converted args. If that succeeds, A is at least as
7294 specialized as P, so they match.*/
7295 tree pargs
= template_parms_level_to_args (parm_parms
);
7296 ++processing_template_decl
;
7297 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7298 /*require_all*/true, /*use_default*/true);
7299 --processing_template_decl
;
7300 if (pargs
!= error_mark_node
)
7302 tree targs
= make_tree_vec (nargs
);
7303 tree aargs
= template_parms_level_to_args (arg_parms
);
7304 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7310 /* Determine whether we have a parameter pack at the end of the
7311 template template parameter's template parameter list. */
7312 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7314 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7316 if (error_operand_p (parm
))
7319 switch (TREE_CODE (parm
))
7323 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7328 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7338 && !(variadic_p
&& nargs
>= nparms
- 1))
7341 /* Check all of the template parameters except the parameter pack at
7342 the end (if any). */
7343 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7345 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7346 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7349 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7350 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7352 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7360 /* Check each of the template parameters in the template
7361 argument against the template parameter pack at the end of
7362 the template template parameter. */
7363 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7366 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7368 for (; i
< nargs
; ++i
)
7370 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7373 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7375 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7384 /* Verifies that the deduced template arguments (in TARGS) for the
7385 template template parameters (in TPARMS) represent valid bindings,
7386 by comparing the template parameter list of each template argument
7387 to the template parameter list of its corresponding template
7388 template parameter, in accordance with DR150. This
7389 routine can only be called after all template arguments have been
7390 deduced. It will return TRUE if all of the template template
7391 parameter bindings are okay, FALSE otherwise. */
7393 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7395 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7398 /* We're dealing with template parms in this process. */
7399 ++processing_template_decl
;
7401 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7403 for (i
= 0; i
< ntparms
; ++i
)
7405 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7406 tree targ
= TREE_VEC_ELT (targs
, i
);
7408 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7410 tree packed_args
= NULL_TREE
;
7413 if (ARGUMENT_PACK_P (targ
))
7415 /* Look inside the argument pack. */
7416 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7417 len
= TREE_VEC_LENGTH (packed_args
);
7420 for (idx
= 0; idx
< len
; ++idx
)
7422 tree targ_parms
= NULL_TREE
;
7425 /* Extract the next argument from the argument
7427 targ
= TREE_VEC_ELT (packed_args
, idx
);
7429 if (PACK_EXPANSION_P (targ
))
7430 /* Look at the pattern of the pack expansion. */
7431 targ
= PACK_EXPANSION_PATTERN (targ
);
7433 /* Extract the template parameters from the template
7435 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7436 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7437 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7438 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7440 /* Verify that we can coerce the template template
7441 parameters from the template argument to the template
7442 parameter. This requires an exact match. */
7444 && !coerce_template_template_parms
7445 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7460 --processing_template_decl
;
7464 /* Since type attributes aren't mangled, we need to strip them from
7465 template type arguments. */
7468 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7470 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7472 bool removed_attributes
= false;
7473 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7474 if (removed_attributes
7475 && (complain
& tf_warning
))
7476 warning (OPT_Wignored_attributes
,
7477 "ignoring attributes on template argument %qT", arg
);
7481 /* And from inside dependent non-type arguments like sizeof(Type). */
7484 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7486 if (!arg
|| arg
== error_mark_node
)
7488 bool removed_attributes
= false;
7489 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7490 if (removed_attributes
7491 && (complain
& tf_warning
))
7492 warning (OPT_Wignored_attributes
,
7493 "ignoring attributes in template argument %qE", arg
);
7497 // A template declaration can be substituted for a constrained
7498 // template template parameter only when the argument is more
7499 // constrained than the parameter.
7501 is_compatible_template_arg (tree parm
, tree arg
)
7503 tree parm_cons
= get_constraints (parm
);
7505 /* For now, allow constrained template template arguments
7506 and unconstrained template template parameters. */
7507 if (parm_cons
== NULL_TREE
)
7510 tree arg_cons
= get_constraints (arg
);
7512 // If the template parameter is constrained, we need to rewrite its
7513 // constraints in terms of the ARG's template parameters. This ensures
7514 // that all of the template parameter types will have the same depth.
7516 // Note that this is only valid when coerce_template_template_parm is
7517 // true for the innermost template parameters of PARM and ARG. In other
7518 // words, because coercion is successful, this conversion will be valid.
7521 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7522 parm_cons
= tsubst_constraint_info (parm_cons
,
7523 INNERMOST_TEMPLATE_ARGS (args
),
7524 tf_none
, NULL_TREE
);
7525 if (parm_cons
== error_mark_node
)
7529 return subsumes (parm_cons
, arg_cons
);
7532 // Convert a placeholder argument into a binding to the original
7533 // parameter. The original parameter is saved as the TREE_TYPE of
7536 convert_wildcard_argument (tree parm
, tree arg
)
7538 TREE_TYPE (arg
) = parm
;
7542 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7543 because one of them is dependent. But we need to represent the
7544 conversion for the benefit of cp_tree_equal. */
7547 maybe_convert_nontype_argument (tree type
, tree arg
)
7549 /* Auto parms get no conversion. */
7550 if (type_uses_auto (type
))
7552 /* We don't need or want to add this conversion now if we're going to use the
7553 argument for deduction. */
7554 if (value_dependent_expression_p (arg
))
7557 type
= cv_unqualified (type
);
7558 tree argtype
= TREE_TYPE (arg
);
7559 if (same_type_p (type
, argtype
))
7562 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7563 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7567 /* Convert the indicated template ARG as necessary to match the
7568 indicated template PARM. Returns the converted ARG, or
7569 error_mark_node if the conversion was unsuccessful. Error and
7570 warning messages are issued under control of COMPLAIN. This
7571 conversion is for the Ith parameter in the parameter list. ARGS is
7572 the full set of template arguments deduced so far. */
7575 convert_template_argument (tree parm
,
7578 tsubst_flags_t complain
,
7584 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7586 if (parm
== error_mark_node
)
7587 return error_mark_node
;
7589 /* Trivially convert placeholders. */
7590 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7591 return convert_wildcard_argument (parm
, arg
);
7593 if (arg
== any_targ_node
)
7596 if (TREE_CODE (arg
) == TREE_LIST
7597 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7599 /* The template argument was the name of some
7600 member function. That's usually
7601 invalid, but static members are OK. In any
7602 case, grab the underlying fields/functions
7603 and issue an error later if required. */
7604 orig_arg
= TREE_VALUE (arg
);
7605 TREE_TYPE (arg
) = unknown_type_node
;
7610 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7611 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7612 || requires_tmpl_type
);
7614 /* When determining whether an argument pack expansion is a template,
7615 look at the pattern. */
7616 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7617 arg
= PACK_EXPANSION_PATTERN (arg
);
7619 /* Deal with an injected-class-name used as a template template arg. */
7620 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7622 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7623 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7625 if (cxx_dialect
>= cxx11
)
7626 /* OK under DR 1004. */;
7627 else if (complain
& tf_warning_or_error
)
7628 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7629 " used as template template argument", TYPE_NAME (arg
));
7630 else if (flag_pedantic_errors
)
7638 ((TREE_CODE (arg
) == TEMPLATE_DECL
7639 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7640 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7641 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7642 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7645 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7646 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7647 arg
= TYPE_STUB_DECL (arg
);
7649 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7651 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7652 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7654 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7656 if (complain
& tf_error
)
7657 error ("invalid use of destructor %qE as a type", orig_arg
);
7658 return error_mark_node
;
7661 permerror (input_location
,
7662 "to refer to a type member of a template parameter, "
7663 "use %<typename %E%>", orig_arg
);
7665 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7666 TREE_OPERAND (arg
, 1),
7672 if (is_type
!= requires_type
)
7676 if (complain
& tf_error
)
7678 error ("type/value mismatch at argument %d in template "
7679 "parameter list for %qD",
7682 inform (input_location
,
7683 " expected a constant of type %qT, got %qT",
7685 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7686 else if (requires_tmpl_type
)
7687 inform (input_location
,
7688 " expected a class template, got %qE", orig_arg
);
7690 inform (input_location
,
7691 " expected a type, got %qE", orig_arg
);
7694 return error_mark_node
;
7696 if (is_tmpl_type
^ requires_tmpl_type
)
7698 if (in_decl
&& (complain
& tf_error
))
7700 error ("type/value mismatch at argument %d in template "
7701 "parameter list for %qD",
7704 inform (input_location
,
7705 " expected a type, got %qT", DECL_NAME (arg
));
7707 inform (input_location
,
7708 " expected a class template, got %qT", orig_arg
);
7710 return error_mark_node
;
7713 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7714 /* We already did the appropriate conversion when packing args. */
7718 if (requires_tmpl_type
)
7720 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7721 /* The number of argument required is not known yet.
7722 Just accept it for now. */
7726 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7729 /* Strip alias templates that are equivalent to another
7731 arg
= get_underlying_template (arg
);
7732 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7734 if (coerce_template_template_parms (parmparm
, argparm
,
7740 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7742 if (val
!= error_mark_node
)
7744 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7745 val
= TREE_TYPE (val
);
7746 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7747 val
= make_pack_expansion (val
, complain
);
7752 if (in_decl
&& (complain
& tf_error
))
7754 error ("type/value mismatch at argument %d in "
7755 "template parameter list for %qD",
7757 inform (input_location
,
7758 " expected a template of type %qD, got %qT",
7762 val
= error_mark_node
;
7765 // Check that the constraints are compatible before allowing the
7767 if (val
!= error_mark_node
)
7768 if (!is_compatible_template_arg (parm
, arg
))
7770 if (in_decl
&& (complain
& tf_error
))
7772 error ("constraint mismatch at argument %d in "
7773 "template parameter list for %qD",
7775 inform (input_location
, " expected %qD but got %qD",
7778 val
= error_mark_node
;
7784 /* We only form one instance of each template specialization.
7785 Therefore, if we use a non-canonical variant (i.e., a
7786 typedef), any future messages referring to the type will use
7787 the typedef, which is confusing if those future uses do not
7788 themselves also use the typedef. */
7790 val
= canonicalize_type_argument (val
, complain
);
7794 tree t
= TREE_TYPE (parm
);
7796 if (tree a
= type_uses_auto (t
))
7798 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7799 if (t
== error_mark_node
)
7800 return error_mark_node
;
7803 t
= tsubst (t
, args
, complain
, in_decl
);
7805 if (invalid_nontype_parm_type_p (t
, complain
))
7806 return error_mark_node
;
7808 if (!type_dependent_expression_p (orig_arg
)
7809 && !uses_template_parms (t
))
7810 /* We used to call digest_init here. However, digest_init
7811 will report errors, which we don't want when complain
7812 is zero. More importantly, digest_init will try too
7813 hard to convert things: for example, `0' should not be
7814 converted to pointer type at this point according to
7815 the standard. Accepting this is not merely an
7816 extension, since deciding whether or not these
7817 conversions can occur is part of determining which
7818 function template to call, or whether a given explicit
7819 argument specification is valid. */
7820 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7823 val
= canonicalize_expr_argument (orig_arg
, complain
);
7824 val
= maybe_convert_nontype_argument (t
, val
);
7828 if (val
== NULL_TREE
)
7829 val
= error_mark_node
;
7830 else if (val
== error_mark_node
&& (complain
& tf_error
))
7831 error ("could not convert template argument %qE from %qT to %qT",
7832 orig_arg
, TREE_TYPE (orig_arg
), t
);
7834 if (INDIRECT_REF_P (val
))
7836 /* Reject template arguments that are references to built-in
7837 functions with no library fallbacks. */
7838 const_tree inner
= TREE_OPERAND (val
, 0);
7839 const_tree innertype
= TREE_TYPE (inner
);
7841 && TREE_CODE (innertype
) == REFERENCE_TYPE
7842 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7843 && TREE_OPERAND_LENGTH (inner
) > 0
7844 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7845 return error_mark_node
;
7848 if (TREE_CODE (val
) == SCOPE_REF
)
7850 /* Strip typedefs from the SCOPE_REF. */
7851 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7852 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7854 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7855 QUALIFIED_NAME_IS_TEMPLATE (val
));
7862 /* Coerces the remaining template arguments in INNER_ARGS (from
7863 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7864 Returns the coerced argument pack. PARM_IDX is the position of this
7865 parameter in the template parameter list. ARGS is the original
7866 template argument list. */
7868 coerce_template_parameter_pack (tree parms
,
7876 tsubst_flags_t complain
)
7878 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7879 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7882 tree packed_parms
= NULL_TREE
;
7884 if (arg_idx
> nargs
)
7887 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7889 /* When the template parameter is a non-type template parameter pack
7890 or template template parameter pack whose type or template
7891 parameters use parameter packs, we know exactly how many arguments
7892 we are looking for. Build a vector of the instantiated decls for
7893 these template parameters in PACKED_PARMS. */
7894 /* We can't use make_pack_expansion here because it would interpret a
7895 _DECL as a use rather than a declaration. */
7896 tree decl
= TREE_VALUE (parm
);
7897 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7898 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7899 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7900 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7902 TREE_VEC_LENGTH (args
)--;
7903 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7904 TREE_VEC_LENGTH (args
)++;
7906 if (packed_parms
== error_mark_node
)
7907 return error_mark_node
;
7909 /* If we're doing a partial instantiation of a member template,
7910 verify that all of the types used for the non-type
7911 template parameter pack are, in fact, valid for non-type
7912 template parameters. */
7914 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7916 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7917 for (j
= 0; j
< len
; ++j
)
7919 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7920 if (invalid_nontype_parm_type_p (t
, complain
))
7921 return error_mark_node
;
7923 /* We don't know how many args we have yet, just
7924 use the unconverted ones for now. */
7928 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7930 /* Check if we have a placeholder pack, which indicates we're
7931 in the context of a introduction list. In that case we want
7932 to match this pack to the single placeholder. */
7933 else if (arg_idx
< nargs
7934 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7935 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7937 nargs
= arg_idx
+ 1;
7938 packed_args
= make_tree_vec (1);
7941 packed_args
= make_tree_vec (nargs
- arg_idx
);
7943 /* Convert the remaining arguments, which will be a part of the
7944 parameter pack "parm". */
7945 int first_pack_arg
= arg_idx
;
7946 for (; arg_idx
< nargs
; ++arg_idx
)
7948 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7949 tree actual_parm
= TREE_VALUE (parm
);
7950 int pack_idx
= arg_idx
- first_pack_arg
;
7954 /* Once we've packed as many args as we have types, stop. */
7955 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7957 else if (PACK_EXPANSION_P (arg
))
7958 /* We don't know how many args we have yet, just
7959 use the unconverted ones for now. */
7962 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7965 if (arg
== error_mark_node
)
7967 if (complain
& tf_error
)
7968 error ("template argument %d is invalid", arg_idx
+ 1);
7971 arg
= convert_template_argument (actual_parm
,
7972 arg
, new_args
, complain
, parm_idx
,
7974 if (arg
== error_mark_node
)
7976 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7979 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7980 && TREE_VEC_LENGTH (packed_args
) > 0)
7982 if (complain
& tf_error
)
7983 error ("wrong number of template arguments (%d, should be %d)",
7984 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7985 return error_mark_node
;
7988 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7989 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7990 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7993 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7994 TREE_CONSTANT (argument_pack
) = 1;
7997 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7999 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8000 TREE_VEC_LENGTH (packed_args
));
8001 return argument_pack
;
8004 /* Returns the number of pack expansions in the template argument vector
8008 pack_expansion_args_count (tree args
)
8013 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8015 tree elt
= TREE_VEC_ELT (args
, i
);
8016 if (elt
&& PACK_EXPANSION_P (elt
))
8022 /* Convert all template arguments to their appropriate types, and
8023 return a vector containing the innermost resulting template
8024 arguments. If any error occurs, return error_mark_node. Error and
8025 warning messages are issued under control of COMPLAIN.
8027 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8028 for arguments not specified in ARGS. Otherwise, if
8029 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8030 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8031 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8035 coerce_template_parms (tree parms
,
8038 tsubst_flags_t complain
,
8039 bool require_all_args
,
8040 bool use_default_args
)
8042 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8043 tree orig_inner_args
;
8046 tree new_inner_args
;
8047 int saved_unevaluated_operand
;
8048 int saved_inhibit_evaluation_warnings
;
8050 /* When used as a boolean value, indicates whether this is a
8051 variadic template parameter list. Since it's an int, we can also
8052 subtract it from nparms to get the number of non-variadic
8055 int variadic_args_p
= 0;
8056 int post_variadic_parms
= 0;
8058 /* Likewise for parameters with default arguments. */
8061 if (args
== error_mark_node
)
8062 return error_mark_node
;
8064 nparms
= TREE_VEC_LENGTH (parms
);
8066 /* Determine if there are any parameter packs or default arguments. */
8067 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8069 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8071 ++post_variadic_parms
;
8072 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8074 if (TREE_PURPOSE (parm
))
8078 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8079 /* If there are no parameters that follow a parameter pack, we need to
8080 expand any argument packs so that we can deduce a parameter pack from
8081 some non-packed args followed by an argument pack, as in variadic85.C.
8082 If there are such parameters, we need to leave argument packs intact
8083 so the arguments are assigned properly. This can happen when dealing
8084 with a nested class inside a partial specialization of a class
8085 template, as in variadic92.C, or when deducing a template parameter pack
8086 from a sub-declarator, as in variadic114.C. */
8087 if (!post_variadic_parms
)
8088 inner_args
= expand_template_argument_pack (inner_args
);
8090 /* Count any pack expansion args. */
8091 variadic_args_p
= pack_expansion_args_count (inner_args
);
8093 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8094 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8095 || (nargs
< nparms
- variadic_p
8098 && (!use_default_args
8099 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8100 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8102 if (complain
& tf_error
)
8104 if (variadic_p
|| default_p
)
8106 nparms
-= variadic_p
+ default_p
;
8107 error ("wrong number of template arguments "
8108 "(%d, should be at least %d)", nargs
, nparms
);
8111 error ("wrong number of template arguments "
8112 "(%d, should be %d)", nargs
, nparms
);
8115 inform (DECL_SOURCE_LOCATION (in_decl
),
8116 "provided for %qD", in_decl
);
8119 return error_mark_node
;
8121 /* We can't pass a pack expansion to a non-pack parameter of an alias
8122 template (DR 1430). */
8124 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8125 || concept_template_p (in_decl
))
8127 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8129 if (complain
& tf_error
)
8131 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8133 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8134 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8136 if (PACK_EXPANSION_P (arg
)
8137 && !template_parameter_pack_p (parm
))
8139 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8140 error_at (location_of (arg
),
8141 "pack expansion argument for non-pack parameter "
8142 "%qD of alias template %qD", parm
, in_decl
);
8144 error_at (location_of (arg
),
8145 "pack expansion argument for non-pack parameter "
8146 "%qD of concept %qD", parm
, in_decl
);
8147 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8154 return error_mark_node
;
8157 /* We need to evaluate the template arguments, even though this
8158 template-id may be nested within a "sizeof". */
8159 saved_unevaluated_operand
= cp_unevaluated_operand
;
8160 cp_unevaluated_operand
= 0;
8161 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8162 c_inhibit_evaluation_warnings
= 0;
8163 new_inner_args
= make_tree_vec (nparms
);
8164 new_args
= add_outermost_template_args (args
, new_inner_args
);
8165 int pack_adjust
= 0;
8166 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8171 /* Get the Ith template parameter. */
8172 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8174 if (parm
== error_mark_node
)
8176 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8180 /* Calculate the next argument. */
8181 if (arg_idx
< nargs
)
8182 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8186 if (template_parameter_pack_p (TREE_VALUE (parm
))
8187 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8189 /* Some arguments will be placed in the
8190 template parameter pack PARM. */
8191 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8192 inner_args
, arg_idx
,
8196 if (arg
== NULL_TREE
)
8198 /* We don't know how many args we have yet, just use the
8199 unconverted (and still packed) ones for now. */
8200 new_inner_args
= orig_inner_args
;
8205 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8207 /* Store this argument. */
8208 if (arg
== error_mark_node
)
8211 /* We are done with all of the arguments. */
8216 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8217 arg_idx
+= pack_adjust
;
8224 if (PACK_EXPANSION_P (arg
))
8226 /* "If every valid specialization of a variadic template
8227 requires an empty template parameter pack, the template is
8228 ill-formed, no diagnostic required." So check that the
8229 pattern works with this parameter. */
8230 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8231 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8235 if (conv
== error_mark_node
)
8237 if (complain
& tf_error
)
8238 inform (input_location
, "so any instantiation with a "
8239 "non-empty parameter pack would be ill-formed");
8242 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8243 /* Recover from missing typename. */
8244 TREE_VEC_ELT (inner_args
, arg_idx
)
8245 = make_pack_expansion (conv
, complain
);
8247 /* We don't know how many args we have yet, just
8248 use the unconverted ones for now. */
8249 new_inner_args
= inner_args
;
8254 else if (require_all_args
)
8256 /* There must be a default arg in this case. */
8257 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8259 /* The position of the first default template argument,
8260 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8262 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8264 arg_idx
- pack_adjust
);
8269 if (arg
== error_mark_node
)
8271 if (complain
& tf_error
)
8272 error ("template argument %d is invalid", arg_idx
+ 1);
8275 /* This only occurs if there was an error in the template
8276 parameter list itself (which we would already have
8277 reported) that we are trying to recover from, e.g., a class
8278 template with a parameter list such as
8279 template<typename..., typename>. */
8282 arg
= convert_template_argument (TREE_VALUE (parm
),
8283 arg
, new_args
, complain
,
8286 if (arg
== error_mark_node
)
8288 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8290 cp_unevaluated_operand
= saved_unevaluated_operand
;
8291 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8293 if (variadic_p
&& arg_idx
< nargs
)
8295 if (complain
& tf_error
)
8297 error ("wrong number of template arguments "
8298 "(%d, should be %d)", nargs
, arg_idx
);
8300 error ("provided for %q+D", in_decl
);
8302 return error_mark_node
;
8306 return error_mark_node
;
8308 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8310 TREE_VEC_LENGTH (new_inner_args
));
8312 return new_inner_args
;
8315 /* Convert all template arguments to their appropriate types, and
8316 return a vector containing the innermost resulting template
8317 arguments. If any error occurs, return error_mark_node. Error and
8318 warning messages are not issued.
8320 Note that no function argument deduction is performed, and default
8321 arguments are used to fill in unspecified arguments. */
8323 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8325 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8328 /* Convert all template arguments to their appropriate type, and
8329 instantiate default arguments as needed. This returns a vector
8330 containing the innermost resulting template arguments, or
8331 error_mark_node if unsuccessful. */
8333 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8334 tsubst_flags_t complain
)
8336 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8339 /* Like coerce_template_parms. If PARMS represents all template
8340 parameters levels, this function returns a vector of vectors
8341 representing all the resulting argument levels. Note that in this
8342 case, only the innermost arguments are coerced because the
8343 outermost ones are supposed to have been coerced already.
8345 Otherwise, if PARMS represents only (the innermost) vector of
8346 parameters, this function returns a vector containing just the
8347 innermost resulting arguments. */
8350 coerce_innermost_template_parms (tree parms
,
8353 tsubst_flags_t complain
,
8354 bool require_all_args
,
8355 bool use_default_args
)
8357 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8358 int args_depth
= TMPL_ARGS_DEPTH (args
);
8361 if (parms_depth
> 1)
8363 coerced_args
= make_tree_vec (parms_depth
);
8367 for (level
= parms
, cur_depth
= parms_depth
;
8368 parms_depth
> 0 && level
!= NULL_TREE
;
8369 level
= TREE_CHAIN (level
), --cur_depth
)
8372 if (cur_depth
== args_depth
)
8373 l
= coerce_template_parms (TREE_VALUE (level
),
8374 args
, in_decl
, complain
,
8378 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8380 if (l
== error_mark_node
)
8381 return error_mark_node
;
8383 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8387 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8388 args
, in_decl
, complain
,
8391 return coerced_args
;
8394 /* Returns 1 if template args OT and NT are equivalent. */
8397 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8401 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8403 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8406 if (TREE_CODE (nt
) == TREE_VEC
)
8407 /* For member templates */
8408 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8409 else if (PACK_EXPANSION_P (ot
))
8410 return (PACK_EXPANSION_P (nt
)
8411 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8412 PACK_EXPANSION_PATTERN (nt
))
8413 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8414 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8415 else if (ARGUMENT_PACK_P (ot
))
8420 if (!ARGUMENT_PACK_P (nt
))
8423 opack
= ARGUMENT_PACK_ARGS (ot
);
8424 npack
= ARGUMENT_PACK_ARGS (nt
);
8425 len
= TREE_VEC_LENGTH (opack
);
8426 if (TREE_VEC_LENGTH (npack
) != len
)
8428 for (i
= 0; i
< len
; ++i
)
8429 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8430 TREE_VEC_ELT (npack
, i
)))
8434 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8436 else if (TYPE_P (nt
))
8440 /* Don't treat an alias template specialization with dependent
8441 arguments as equivalent to its underlying type when used as a
8442 template argument; we need them to be distinct so that we
8443 substitute into the specialization arguments at instantiation
8444 time. And aliases can't be equivalent without being ==, so
8445 we don't need to look any deeper.
8447 During partial ordering, however, we need to treat them normally so
8448 that we can order uses of the same alias with different
8449 cv-qualification (79960). */
8451 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8454 return same_type_p (ot
, nt
);
8456 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8460 /* Try to treat a template non-type argument that has been converted
8461 to the parameter type as equivalent to one that hasn't yet. */
8462 for (enum tree_code code1
= TREE_CODE (ot
);
8463 CONVERT_EXPR_CODE_P (code1
)
8464 || code1
== NON_LVALUE_EXPR
;
8465 code1
= TREE_CODE (ot
))
8466 ot
= TREE_OPERAND (ot
, 0);
8467 for (enum tree_code code2
= TREE_CODE (nt
);
8468 CONVERT_EXPR_CODE_P (code2
)
8469 || code2
== NON_LVALUE_EXPR
;
8470 code2
= TREE_CODE (nt
))
8471 nt
= TREE_OPERAND (nt
, 0);
8473 return cp_tree_equal (ot
, nt
);
8477 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8478 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8479 NEWARG_PTR with the offending arguments if they are non-NULL. */
8482 comp_template_args (tree oldargs
, tree newargs
,
8483 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8488 if (oldargs
== newargs
)
8491 if (!oldargs
|| !newargs
)
8494 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8497 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8499 tree nt
= TREE_VEC_ELT (newargs
, i
);
8500 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8502 if (! template_args_equal (ot
, nt
, partial_order
))
8504 if (oldarg_ptr
!= NULL
)
8506 if (newarg_ptr
!= NULL
)
8515 comp_template_args_porder (tree oargs
, tree nargs
)
8517 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8521 add_pending_template (tree d
)
8523 tree ti
= (TYPE_P (d
)
8524 ? CLASSTYPE_TEMPLATE_INFO (d
)
8525 : DECL_TEMPLATE_INFO (d
));
8526 struct pending_template
*pt
;
8529 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8532 /* We are called both from instantiate_decl, where we've already had a
8533 tinst_level pushed, and instantiate_template, where we haven't.
8535 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8538 push_tinst_level (d
);
8540 pt
= ggc_alloc
<pending_template
> ();
8542 pt
->tinst
= current_tinst_level
;
8543 if (last_pending_template
)
8544 last_pending_template
->next
= pt
;
8546 pending_templates
= pt
;
8548 last_pending_template
= pt
;
8550 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8557 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8558 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8559 documentation for TEMPLATE_ID_EXPR. */
8562 lookup_template_function (tree fns
, tree arglist
)
8566 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8567 return error_mark_node
;
8569 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8571 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8573 error ("%q#D is not a function template", fns
);
8574 return error_mark_node
;
8577 if (BASELINK_P (fns
))
8579 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8581 BASELINK_FUNCTIONS (fns
),
8586 type
= TREE_TYPE (fns
);
8587 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8588 type
= unknown_type_node
;
8590 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8593 /* Within the scope of a template class S<T>, the name S gets bound
8594 (in build_self_reference) to a TYPE_DECL for the class, not a
8595 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8596 or one of its enclosing classes, and that type is a template,
8597 return the associated TEMPLATE_DECL. Otherwise, the original
8600 Also handle the case when DECL is a TREE_LIST of ambiguous
8601 injected-class-names from different bases. */
8604 maybe_get_template_decl_from_type_decl (tree decl
)
8606 if (decl
== NULL_TREE
)
8609 /* DR 176: A lookup that finds an injected-class-name (10.2
8610 [class.member.lookup]) can result in an ambiguity in certain cases
8611 (for example, if it is found in more than one base class). If all of
8612 the injected-class-names that are found refer to specializations of
8613 the same class template, and if the name is followed by a
8614 template-argument-list, the reference refers to the class template
8615 itself and not a specialization thereof, and is not ambiguous. */
8616 if (TREE_CODE (decl
) == TREE_LIST
)
8618 tree t
, tmpl
= NULL_TREE
;
8619 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8621 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8624 else if (tmpl
!= elt
)
8627 if (tmpl
&& t
== NULL_TREE
)
8633 return (decl
!= NULL_TREE
8634 && DECL_SELF_REFERENCE_P (decl
)
8635 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8636 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8639 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8640 parameters, find the desired type.
8642 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8644 IN_DECL, if non-NULL, is the template declaration we are trying to
8647 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8648 the class we are looking up.
8650 Issue error and warning messages under control of COMPLAIN.
8652 If the template class is really a local class in a template
8653 function, then the FUNCTION_CONTEXT is the function in which it is
8656 ??? Note that this function is currently called *twice* for each
8657 template-id: the first time from the parser, while creating the
8658 incomplete type (finish_template_type), and the second type during the
8659 real instantiation (instantiate_template_class). This is surely something
8660 that we want to avoid. It also causes some problems with argument
8661 coercion (see convert_nontype_argument for more information on this). */
8664 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8665 int entering_scope
, tsubst_flags_t complain
)
8667 tree templ
= NULL_TREE
, parmlist
;
8674 if (identifier_p (d1
))
8676 tree value
= innermost_non_namespace_value (d1
);
8677 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8682 push_decl_namespace (context
);
8683 templ
= lookup_name (d1
);
8684 templ
= maybe_get_template_decl_from_type_decl (templ
);
8686 pop_decl_namespace ();
8689 context
= DECL_CONTEXT (templ
);
8691 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8693 tree type
= TREE_TYPE (d1
);
8695 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8696 an implicit typename for the second A. Deal with it. */
8697 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8698 type
= TREE_TYPE (type
);
8700 if (CLASSTYPE_TEMPLATE_INFO (type
))
8702 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8703 d1
= DECL_NAME (templ
);
8706 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8707 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8709 templ
= TYPE_TI_TEMPLATE (d1
);
8710 d1
= DECL_NAME (templ
);
8712 else if (DECL_TYPE_TEMPLATE_P (d1
))
8715 d1
= DECL_NAME (templ
);
8716 context
= DECL_CONTEXT (templ
);
8718 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8721 d1
= DECL_NAME (templ
);
8724 /* Issue an error message if we didn't find a template. */
8727 if (complain
& tf_error
)
8728 error ("%qT is not a template", d1
);
8729 return error_mark_node
;
8732 if (TREE_CODE (templ
) != TEMPLATE_DECL
8733 /* Make sure it's a user visible template, if it was named by
8735 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8736 && !PRIMARY_TEMPLATE_P (templ
)))
8738 if (complain
& tf_error
)
8740 error ("non-template type %qT used as a template", d1
);
8742 error ("for template declaration %q+D", in_decl
);
8744 return error_mark_node
;
8747 complain
&= ~tf_user
;
8749 /* An alias that just changes the name of a template is equivalent to the
8750 other template, so if any of the arguments are pack expansions, strip
8751 the alias to avoid problems with a pack expansion passed to a non-pack
8752 alias template parameter (DR 1430). */
8753 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8754 templ
= get_underlying_template (templ
);
8756 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8759 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8760 if (arglist2
== error_mark_node
8761 || (!uses_template_parms (arglist2
)
8762 && check_instantiated_args (templ
, arglist2
, complain
)))
8763 return error_mark_node
;
8765 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8770 tree template_type
= TREE_TYPE (templ
);
8773 tree found
= NULL_TREE
;
8776 int is_dependent_type
;
8777 int use_partial_inst_tmpl
= false;
8779 if (template_type
== error_mark_node
)
8780 /* An error occurred while building the template TEMPL, and a
8781 diagnostic has most certainly been emitted for that
8782 already. Let's propagate that error. */
8783 return error_mark_node
;
8785 gen_tmpl
= most_general_template (templ
);
8786 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8787 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8788 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8790 if (arg_depth
== 1 && parm_depth
> 1)
8792 /* We've been given an incomplete set of template arguments.
8795 template <class T> struct S1 {
8796 template <class U> struct S2 {};
8797 template <class U> struct S2<U*> {};
8800 we will be called with an ARGLIST of `U*', but the
8801 TEMPLATE will be `template <class T> template
8802 <class U> struct S1<T>::S2'. We must fill in the missing
8804 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8805 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8806 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8809 /* Now we should have enough arguments. */
8810 gcc_assert (parm_depth
== arg_depth
);
8812 /* From here on, we're only interested in the most general
8815 /* Calculate the BOUND_ARGS. These will be the args that are
8816 actually tsubst'd into the definition to create the
8818 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8820 /*require_all_args=*/true,
8821 /*use_default_args=*/true);
8823 if (arglist
== error_mark_node
)
8824 /* We were unable to bind the arguments. */
8825 return error_mark_node
;
8827 /* In the scope of a template class, explicit references to the
8828 template class refer to the type of the template, not any
8829 instantiation of it. For example, in:
8831 template <class T> class C { void f(C<T>); }
8833 the `C<T>' is just the same as `C'. Outside of the
8834 class, however, such a reference is an instantiation. */
8836 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8837 || currently_open_class (template_type
))
8839 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8841 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8842 return template_type
;
8845 /* If we already have this specialization, return it. */
8846 elt
.tmpl
= gen_tmpl
;
8848 elt
.spec
= NULL_TREE
;
8849 hash
= spec_hasher::hash (&elt
);
8850 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8855 /* If the the template's constraints are not satisfied,
8856 then we cannot form a valid type.
8858 Note that the check is deferred until after the hash
8859 lookup. This prevents redundant checks on previously
8860 instantiated specializations. */
8861 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8863 if (complain
& tf_error
)
8865 error ("template constraint failure");
8866 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8868 return error_mark_node
;
8871 is_dependent_type
= uses_template_parms (arglist
);
8873 /* If the deduced arguments are invalid, then the binding
8875 if (!is_dependent_type
8876 && check_instantiated_args (gen_tmpl
,
8877 INNERMOST_TEMPLATE_ARGS (arglist
),
8879 return error_mark_node
;
8881 if (!is_dependent_type
8882 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8883 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8884 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8886 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8887 DECL_NAME (gen_tmpl
),
8888 /*tag_scope=*/ts_global
);
8892 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8894 if (context
== error_mark_node
)
8895 return error_mark_node
;
8898 context
= global_namespace
;
8900 /* Create the type. */
8901 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8903 /* The user referred to a specialization of an alias
8904 template represented by GEN_TMPL.
8906 [temp.alias]/2 says:
8908 When a template-id refers to the specialization of an
8909 alias template, it is equivalent to the associated
8910 type obtained by substitution of its
8911 template-arguments for the template-parameters in the
8912 type-id of the alias template. */
8914 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8915 /* Note that the call above (by indirectly calling
8916 register_specialization in tsubst_decl) registers the
8917 TYPE_DECL representing the specialization of the alias
8918 template. So next time someone substitutes ARGLIST for
8919 the template parms into the alias template (GEN_TMPL),
8920 she'll get that TYPE_DECL back. */
8922 if (t
== error_mark_node
)
8925 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8927 if (!is_dependent_type
)
8929 set_current_access_from_decl (TYPE_NAME (template_type
));
8930 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8931 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8932 arglist
, complain
, in_decl
),
8933 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8934 arglist
, complain
, in_decl
),
8935 SCOPED_ENUM_P (template_type
), NULL
);
8937 if (t
== error_mark_node
)
8942 /* We don't want to call start_enum for this type, since
8943 the values for the enumeration constants may involve
8944 template parameters. And, no one should be interested
8945 in the enumeration constants for such a type. */
8946 t
= cxx_make_type (ENUMERAL_TYPE
);
8947 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8949 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8950 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8951 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8953 else if (CLASS_TYPE_P (template_type
))
8955 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8956 instantiated here. */
8957 gcc_assert (!LAMBDA_TYPE_P (template_type
));
8959 t
= make_class_type (TREE_CODE (template_type
));
8960 CLASSTYPE_DECLARED_CLASS (t
)
8961 = CLASSTYPE_DECLARED_CLASS (template_type
);
8962 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8964 /* A local class. Make sure the decl gets registered properly. */
8965 if (context
== current_function_decl
)
8966 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8968 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8969 /* This instantiation is another name for the primary
8970 template type. Set the TYPE_CANONICAL field
8972 TYPE_CANONICAL (t
) = template_type
;
8973 else if (any_template_arguments_need_structural_equality_p (arglist
))
8974 /* Some of the template arguments require structural
8975 equality testing, so this template class requires
8976 structural equality testing. */
8977 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8982 /* If we called start_enum or pushtag above, this information
8983 will already be set up. */
8986 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8988 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8989 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8990 DECL_SOURCE_LOCATION (type_decl
)
8991 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8994 type_decl
= TYPE_NAME (t
);
8996 if (CLASS_TYPE_P (template_type
))
8998 TREE_PRIVATE (type_decl
)
8999 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9000 TREE_PROTECTED (type_decl
)
9001 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9002 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9004 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9005 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9009 if (OVERLOAD_TYPE_P (t
)
9010 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9012 static const char *tags
[] = {"abi_tag", "may_alias"};
9014 for (unsigned ix
= 0; ix
!= 2; ix
++)
9017 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9021 = tree_cons (TREE_PURPOSE (attributes
),
9022 TREE_VALUE (attributes
),
9023 TYPE_ATTRIBUTES (t
));
9027 /* Let's consider the explicit specialization of a member
9028 of a class template specialization that is implicitly instantiated,
9033 template<class U> struct M {}; //#0
9038 struct S<int>::M<char> //#1
9042 [temp.expl.spec]/4 says this is valid.
9044 In this case, when we write:
9047 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9050 When we encounter #1, we want to store the partial instantiation
9051 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9053 For all cases other than this "explicit specialization of member of a
9054 class template", we just want to store the most general template into
9055 the CLASSTYPE_TI_TEMPLATE of M.
9057 This case of "explicit specialization of member of a class template"
9059 1/ the enclosing class is an instantiation of, and therefore not
9060 the same as, the context of the most general template, and
9061 2/ we aren't looking at the partial instantiation itself, i.e.
9062 the innermost arguments are not the same as the innermost parms of
9063 the most general template.
9065 So it's only when 1/ and 2/ happens that we want to use the partial
9066 instantiation of the member template in lieu of its most general
9069 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9070 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9071 /* the enclosing class must be an instantiation... */
9072 && CLASS_TYPE_P (context
)
9073 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9075 TREE_VEC_LENGTH (arglist
)--;
9076 ++processing_template_decl
;
9077 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9078 tree partial_inst_args
=
9079 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9080 arglist
, complain
, NULL_TREE
);
9081 --processing_template_decl
;
9082 TREE_VEC_LENGTH (arglist
)++;
9083 if (partial_inst_args
== error_mark_node
)
9084 return error_mark_node
;
9085 use_partial_inst_tmpl
=
9086 /*...and we must not be looking at the partial instantiation
9088 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9092 if (!use_partial_inst_tmpl
)
9093 /* This case is easy; there are no member templates involved. */
9097 /* This is a full instantiation of a member template. Find
9098 the partial instantiation of which this is an instance. */
9100 /* Temporarily reduce by one the number of levels in the ARGLIST
9101 so as to avoid comparing the last set of arguments. */
9102 TREE_VEC_LENGTH (arglist
)--;
9103 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9104 TREE_VEC_LENGTH (arglist
)++;
9105 /* FOUND is either a proper class type, or an alias
9106 template specialization. In the later case, it's a
9107 TYPE_DECL, resulting from the substituting of arguments
9108 for parameters in the TYPE_DECL of the alias template
9109 done earlier. So be careful while getting the template
9111 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9113 : (TREE_CODE (found
) == TYPE_DECL
9114 ? DECL_TI_TEMPLATE (found
)
9115 : CLASSTYPE_TI_TEMPLATE (found
)));
9118 // Build template info for the new specialization.
9119 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9122 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9123 entry
= ggc_alloc
<spec_entry
> ();
9127 /* Note this use of the partial instantiation so we can check it
9128 later in maybe_process_partial_specialization. */
9129 DECL_TEMPLATE_INSTANTIATIONS (found
)
9130 = tree_cons (arglist
, t
,
9131 DECL_TEMPLATE_INSTANTIATIONS (found
));
9133 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9134 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9135 /* Now that the type has been registered on the instantiations
9136 list, we set up the enumerators. Because the enumeration
9137 constants may involve the enumeration type itself, we make
9138 sure to register the type first, and then create the
9139 constants. That way, doing tsubst_expr for the enumeration
9140 constants won't result in recursive calls here; we'll find
9141 the instantiation and exit above. */
9142 tsubst_enum (template_type
, t
, arglist
);
9144 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9145 /* If the type makes use of template parameters, the
9146 code that generates debugging information will crash. */
9147 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9149 /* Possibly limit visibility based on template args. */
9150 TREE_PUBLIC (type_decl
) = 1;
9151 determine_visibility (type_decl
);
9153 inherit_targ_abi_tags (t
);
9159 /* Wrapper for lookup_template_class_1. */
9162 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9163 int entering_scope
, tsubst_flags_t complain
)
9166 timevar_push (TV_TEMPLATE_INST
);
9167 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9168 entering_scope
, complain
);
9169 timevar_pop (TV_TEMPLATE_INST
);
9173 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9176 lookup_template_variable (tree templ
, tree arglist
)
9178 /* The type of the expression is NULL_TREE since the template-id could refer
9179 to an explicit or partial specialization. */
9180 tree type
= NULL_TREE
;
9181 if (flag_concepts
&& variable_concept_p (templ
))
9182 /* Except that concepts are always bool. */
9183 type
= boolean_type_node
;
9184 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9187 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9190 finish_template_variable (tree var
, tsubst_flags_t complain
)
9192 tree templ
= TREE_OPERAND (var
, 0);
9193 tree arglist
= TREE_OPERAND (var
, 1);
9195 /* We never want to return a VAR_DECL for a variable concept, since they
9196 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9197 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9198 if (concept_p
&& processing_template_decl
)
9201 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9202 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9204 templ
= most_general_template (templ
);
9205 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9206 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9208 /*use_default*/true);
9210 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9212 if (complain
& tf_error
)
9214 error ("use of invalid variable template %qE", var
);
9215 diagnose_constraints (location_of (var
), templ
, arglist
);
9217 return error_mark_node
;
9220 /* If a template-id refers to a specialization of a variable
9221 concept, then the expression is true if and only if the
9222 concept's constraints are satisfied by the given template
9225 NOTE: This is an extension of Concepts Lite TS that
9226 allows constraints to be used in expressions. */
9229 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9230 return evaluate_variable_concept (decl
, arglist
);
9233 return instantiate_template (templ
, arglist
, complain
);
9236 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9237 TARGS template args, and instantiate it if it's not dependent. */
9240 lookup_and_finish_template_variable (tree templ
, tree targs
,
9241 tsubst_flags_t complain
)
9243 templ
= lookup_template_variable (templ
, targs
);
9244 if (!any_dependent_template_arguments_p (targs
))
9246 templ
= finish_template_variable (templ
, complain
);
9250 return convert_from_reference (templ
);
9259 /* True when we should also visit template parameters that occur in
9260 non-deduced contexts. */
9261 bool include_nondeduced_p
;
9262 hash_set
<tree
> *visited
;
9265 /* Called from for_each_template_parm via walk_tree. */
9268 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9271 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9272 tree_fn_t fn
= pfd
->fn
;
9273 void *data
= pfd
->data
;
9274 tree result
= NULL_TREE
;
9276 #define WALK_SUBTREE(NODE) \
9279 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9280 pfd->include_nondeduced_p, \
9282 if (result) goto out; \
9286 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9290 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9291 WALK_SUBTREE (TYPE_CONTEXT (t
));
9293 switch (TREE_CODE (t
))
9296 if (TYPE_PTRMEMFUNC_P (t
))
9302 if (!TYPE_TEMPLATE_INFO (t
))
9305 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9309 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9310 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9314 /* Since we're not going to walk subtrees, we have to do this
9316 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9320 /* Check the return type. */
9321 WALK_SUBTREE (TREE_TYPE (t
));
9323 /* Check the parameter types. Since default arguments are not
9324 instantiated until they are needed, the TYPE_ARG_TYPES may
9325 contain expressions that involve template parameters. But,
9326 no-one should be looking at them yet. And, once they're
9327 instantiated, they don't contain template parameters, so
9328 there's no point in looking at them then, either. */
9332 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9333 WALK_SUBTREE (TREE_VALUE (parm
));
9335 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9336 want walk_tree walking into them itself. */
9340 if (flag_noexcept_type
)
9342 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9344 WALK_SUBTREE (TREE_PURPOSE (spec
));
9349 case UNDERLYING_TYPE
:
9350 if (pfd
->include_nondeduced_p
9351 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9353 pfd
->include_nondeduced_p
,
9355 return error_mark_node
;
9360 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9361 WALK_SUBTREE (DECL_TI_ARGS (t
));
9366 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9367 WALK_SUBTREE (DECL_INITIAL (t
));
9368 if (DECL_CONTEXT (t
)
9369 && pfd
->include_nondeduced_p
)
9370 WALK_SUBTREE (DECL_CONTEXT (t
));
9373 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9374 /* Record template parameters such as `T' inside `TT<T>'. */
9375 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9378 case TEMPLATE_TEMPLATE_PARM
:
9379 case TEMPLATE_TYPE_PARM
:
9380 case TEMPLATE_PARM_INDEX
:
9381 if (fn
&& (*fn
)(t
, data
))
9388 /* A template template parameter is encountered. */
9389 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9390 WALK_SUBTREE (TREE_TYPE (t
));
9392 /* Already substituted template template parameter */
9397 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9398 partial instantiation. */
9399 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9403 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9404 && pfd
->include_nondeduced_p
)
9405 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9410 /* If there's no type, then this thing must be some expression
9411 involving template parameters. */
9412 if (!fn
&& !TREE_TYPE (t
))
9413 return error_mark_node
;
9418 case IMPLICIT_CONV_EXPR
:
9419 case REINTERPRET_CAST_EXPR
:
9420 case CONST_CAST_EXPR
:
9421 case STATIC_CAST_EXPR
:
9422 case DYNAMIC_CAST_EXPR
:
9426 case PSEUDO_DTOR_EXPR
:
9428 return error_mark_node
;
9437 /* We didn't find any template parameters we liked. */
9442 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9443 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9444 call FN with the parameter and the DATA.
9445 If FN returns nonzero, the iteration is terminated, and
9446 for_each_template_parm returns 1. Otherwise, the iteration
9447 continues. If FN never returns a nonzero value, the value
9448 returned by for_each_template_parm is 0. If FN is NULL, it is
9449 considered to be the function which always returns 1.
9451 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9452 parameters that occur in non-deduced contexts. When false, only
9453 visits those template parameters that can be deduced. */
9456 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9457 hash_set
<tree
> *visited
,
9458 bool include_nondeduced_p
,
9461 struct pair_fn_data pfd
;
9466 pfd
.any_fn
= any_fn
;
9468 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9470 /* Walk the tree. (Conceptually, we would like to walk without
9471 duplicates, but for_each_template_parm_r recursively calls
9472 for_each_template_parm, so we would need to reorganize a fair
9473 bit to use walk_tree_without_duplicates, so we keep our own
9476 pfd
.visited
= visited
;
9478 pfd
.visited
= new hash_set
<tree
>;
9479 result
= cp_walk_tree (&t
,
9480 for_each_template_parm_r
,
9494 /* Returns true if T depends on any template parameter. */
9497 uses_template_parms (tree t
)
9503 int saved_processing_template_decl
;
9505 saved_processing_template_decl
= processing_template_decl
;
9506 if (!saved_processing_template_decl
)
9507 processing_template_decl
= 1;
9509 dependent_p
= dependent_type_p (t
);
9510 else if (TREE_CODE (t
) == TREE_VEC
)
9511 dependent_p
= any_dependent_template_arguments_p (t
);
9512 else if (TREE_CODE (t
) == TREE_LIST
)
9513 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9514 || uses_template_parms (TREE_CHAIN (t
)));
9515 else if (TREE_CODE (t
) == TYPE_DECL
)
9516 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9519 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9520 || TREE_CODE (t
) == OVERLOAD
9523 || TREE_CODE (t
) == TRAIT_EXPR
9524 || TREE_CODE (t
) == CONSTRUCTOR
9525 || CONSTANT_CLASS_P (t
))
9526 dependent_p
= (type_dependent_expression_p (t
)
9527 || value_dependent_expression_p (t
));
9530 gcc_assert (t
== error_mark_node
);
9531 dependent_p
= false;
9534 processing_template_decl
= saved_processing_template_decl
;
9539 /* Returns true iff current_function_decl is an incompletely instantiated
9540 template. Useful instead of processing_template_decl because the latter
9541 is set to 0 during instantiate_non_dependent_expr. */
9544 in_template_function (void)
9546 tree fn
= current_function_decl
;
9548 ++processing_template_decl
;
9549 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9550 && DECL_TEMPLATE_INFO (fn
)
9551 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9552 --processing_template_decl
;
9556 /* Returns true if T depends on any template parameter with level LEVEL. */
9559 uses_template_parms_level (tree t
, int level
)
9561 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9562 /*include_nondeduced_p=*/true);
9565 /* Returns true if the signature of DECL depends on any template parameter from
9566 its enclosing class. */
9569 uses_outer_template_parms (tree decl
)
9571 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9574 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9575 &depth
, NULL
, /*include_nondeduced_p=*/true))
9577 if (PRIMARY_TEMPLATE_P (decl
)
9578 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9579 (DECL_TEMPLATE_PARMS (decl
)),
9580 template_parm_outer_level
,
9581 &depth
, NULL
, /*include_nondeduced_p=*/true))
9583 tree ci
= get_constraints (decl
);
9585 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9586 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9587 &depth
, NULL
, /*nondeduced*/true))
9592 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9593 ill-formed translation unit, i.e. a variable or function that isn't
9594 usable in a constant expression. */
9597 neglectable_inst_p (tree d
)
9600 && !undeduced_auto_decl (d
)
9601 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9602 : decl_maybe_constant_var_p (d
)));
9605 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9606 neglectable and instantiated from within an erroneous instantiation. */
9609 limit_bad_template_recursion (tree decl
)
9611 struct tinst_level
*lev
= current_tinst_level
;
9612 int errs
= errorcount
+ sorrycount
;
9613 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9616 for (; lev
; lev
= lev
->next
)
9617 if (neglectable_inst_p (lev
->decl
))
9620 return (lev
&& errs
> lev
->errors
);
9623 static int tinst_depth
;
9624 extern int max_tinst_depth
;
9627 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9629 /* We're starting to instantiate D; record the template instantiation context
9630 for diagnostics and to restore it later. */
9633 push_tinst_level (tree d
)
9635 return push_tinst_level_loc (d
, input_location
);
9638 /* We're starting to instantiate D; record the template instantiation context
9639 at LOC for diagnostics and to restore it later. */
9642 push_tinst_level_loc (tree d
, location_t loc
)
9644 struct tinst_level
*new_level
;
9646 if (tinst_depth
>= max_tinst_depth
)
9648 /* Tell error.c not to try to instantiate any templates. */
9650 fatal_error (input_location
,
9651 "template instantiation depth exceeds maximum of %d"
9652 " (use -ftemplate-depth= to increase the maximum)",
9657 /* If the current instantiation caused problems, don't let it instantiate
9658 anything else. Do allow deduction substitution and decls usable in
9659 constant expressions. */
9660 if (limit_bad_template_recursion (d
))
9663 /* When not -quiet, dump template instantiations other than functions, since
9664 announce_function will take care of those. */
9666 && TREE_CODE (d
) != TREE_LIST
9667 && TREE_CODE (d
) != FUNCTION_DECL
)
9668 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9670 new_level
= ggc_alloc
<tinst_level
> ();
9671 new_level
->decl
= d
;
9672 new_level
->locus
= loc
;
9673 new_level
->errors
= errorcount
+sorrycount
;
9674 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9675 new_level
->next
= current_tinst_level
;
9676 current_tinst_level
= new_level
;
9679 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9680 depth_reached
= tinst_depth
;
9685 /* We're done instantiating this template; return to the instantiation
9689 pop_tinst_level (void)
9691 /* Restore the filename and line number stashed away when we started
9692 this instantiation. */
9693 input_location
= current_tinst_level
->locus
;
9694 current_tinst_level
= current_tinst_level
->next
;
9698 /* We're instantiating a deferred template; restore the template
9699 instantiation context in which the instantiation was requested, which
9700 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9703 reopen_tinst_level (struct tinst_level
*level
)
9705 struct tinst_level
*t
;
9708 for (t
= level
; t
; t
= t
->next
)
9711 current_tinst_level
= level
;
9713 if (current_tinst_level
)
9714 current_tinst_level
->errors
= errorcount
+sorrycount
;
9718 /* Returns the TINST_LEVEL which gives the original instantiation
9721 struct tinst_level
*
9722 outermost_tinst_level (void)
9724 struct tinst_level
*level
= current_tinst_level
;
9727 level
= level
->next
;
9731 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9732 vector of template arguments, as for tsubst.
9734 Returns an appropriate tsubst'd friend declaration. */
9737 tsubst_friend_function (tree decl
, tree args
)
9741 if (TREE_CODE (decl
) == FUNCTION_DECL
9742 && DECL_TEMPLATE_INSTANTIATION (decl
)
9743 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9744 /* This was a friend declared with an explicit template
9745 argument list, e.g.:
9749 to indicate that f was a template instantiation, not a new
9750 function declaration. Now, we have to figure out what
9751 instantiation of what template. */
9753 tree template_id
, arglist
, fns
;
9756 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9758 /* Friend functions are looked up in the containing namespace scope.
9759 We must enter that scope, to avoid finding member functions of the
9760 current class with same name. */
9761 push_nested_namespace (ns
);
9762 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9763 tf_warning_or_error
, NULL_TREE
,
9764 /*integral_constant_expression_p=*/false);
9765 pop_nested_namespace (ns
);
9766 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9767 tf_warning_or_error
, NULL_TREE
);
9768 template_id
= lookup_template_function (fns
, arglist
);
9770 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9771 tmpl
= determine_specialization (template_id
, new_friend
,
9773 /*need_member_template=*/0,
9774 TREE_VEC_LENGTH (args
),
9776 return instantiate_template (tmpl
, new_args
, tf_error
);
9779 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9781 /* The NEW_FRIEND will look like an instantiation, to the
9782 compiler, but is not an instantiation from the point of view of
9783 the language. For example, we might have had:
9785 template <class T> struct S {
9786 template <class U> friend void f(T, U);
9789 Then, in S<int>, template <class U> void f(int, U) is not an
9790 instantiation of anything. */
9791 if (new_friend
== error_mark_node
)
9792 return error_mark_node
;
9794 DECL_USE_TEMPLATE (new_friend
) = 0;
9795 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9797 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9798 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9799 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9802 /* The mangled name for the NEW_FRIEND is incorrect. The function
9803 is not a template instantiation and should not be mangled like
9804 one. Therefore, we forget the mangling here; we'll recompute it
9805 later if we need it. */
9806 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9808 SET_DECL_RTL (new_friend
, NULL
);
9809 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9812 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9815 tree new_friend_template_info
;
9816 tree new_friend_result_template_info
;
9818 int new_friend_is_defn
;
9820 /* We must save some information from NEW_FRIEND before calling
9821 duplicate decls since that function will free NEW_FRIEND if
9823 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9824 new_friend_is_defn
=
9825 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9826 (template_for_substitution (new_friend
)))
9828 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9830 /* This declaration is a `primary' template. */
9831 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9833 new_friend_result_template_info
9834 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9837 new_friend_result_template_info
= NULL_TREE
;
9839 /* Inside pushdecl_namespace_level, we will push into the
9840 current namespace. However, the friend function should go
9841 into the namespace of the template. */
9842 ns
= decl_namespace_context (new_friend
);
9843 push_nested_namespace (ns
);
9844 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9845 pop_nested_namespace (ns
);
9847 if (old_decl
== error_mark_node
)
9848 return error_mark_node
;
9850 if (old_decl
!= new_friend
)
9852 /* This new friend declaration matched an existing
9853 declaration. For example, given:
9855 template <class T> void f(T);
9856 template <class U> class C {
9857 template <class T> friend void f(T) {}
9860 the friend declaration actually provides the definition
9861 of `f', once C has been instantiated for some type. So,
9862 old_decl will be the out-of-class template declaration,
9863 while new_friend is the in-class definition.
9865 But, if `f' was called before this point, the
9866 instantiation of `f' will have DECL_TI_ARGS corresponding
9867 to `T' but not to `U', references to which might appear
9868 in the definition of `f'. Previously, the most general
9869 template for an instantiation of `f' was the out-of-class
9870 version; now it is the in-class version. Therefore, we
9871 run through all specialization of `f', adding to their
9872 DECL_TI_ARGS appropriately. In particular, they need a
9873 new set of outer arguments, corresponding to the
9874 arguments for this class instantiation.
9876 The same situation can arise with something like this:
9879 template <class T> class C {
9883 when `C<int>' is instantiated. Now, `f(int)' is defined
9886 if (!new_friend_is_defn
)
9887 /* On the other hand, if the in-class declaration does
9888 *not* provide a definition, then we don't want to alter
9889 existing definitions. We can just leave everything
9894 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9895 tree new_args
= TI_ARGS (new_friend_template_info
);
9897 /* Overwrite whatever template info was there before, if
9898 any, with the new template information pertaining to
9900 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9902 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9904 /* We should have called reregister_specialization in
9906 gcc_assert (retrieve_specialization (new_template
,
9910 /* Instantiate it if the global has already been used. */
9911 if (DECL_ODR_USED (old_decl
))
9912 instantiate_decl (old_decl
, /*defer_ok=*/true,
9913 /*expl_inst_class_mem_p=*/false);
9919 /* Indicate that the old function template is a partial
9921 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9922 = new_friend_result_template_info
;
9924 gcc_assert (new_template
9925 == most_general_template (new_template
));
9926 gcc_assert (new_template
!= old_decl
);
9928 /* Reassign any specializations already in the hash table
9929 to the new more general template, and add the
9930 additional template args. */
9931 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9935 tree spec
= TREE_VALUE (t
);
9938 elt
.tmpl
= old_decl
;
9939 elt
.args
= DECL_TI_ARGS (spec
);
9940 elt
.spec
= NULL_TREE
;
9942 decl_specializations
->remove_elt (&elt
);
9945 = add_outermost_template_args (new_args
,
9946 DECL_TI_ARGS (spec
));
9948 register_specialization
9949 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9952 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9956 /* The information from NEW_FRIEND has been merged into OLD_DECL
9957 by duplicate_decls. */
9958 new_friend
= old_decl
;
9963 tree context
= DECL_CONTEXT (new_friend
);
9967 template <class T> class C {
9968 template <class U> friend void C1<U>::f (); // case 1
9969 friend void C2<T>::f (); // case 2
9971 we only need to make sure CONTEXT is a complete type for
9972 case 2. To distinguish between the two cases, we note that
9973 CONTEXT of case 1 remains dependent type after tsubst while
9974 this isn't true for case 2. */
9975 ++processing_template_decl
;
9976 dependent_p
= dependent_type_p (context
);
9977 --processing_template_decl
;
9980 && !complete_type_or_else (context
, NULL_TREE
))
9981 return error_mark_node
;
9983 if (COMPLETE_TYPE_P (context
))
9985 tree fn
= new_friend
;
9986 /* do_friend adds the TEMPLATE_DECL for any member friend
9987 template even if it isn't a member template, i.e.
9988 template <class T> friend A<T>::f();
9989 Look through it in that case. */
9990 if (TREE_CODE (fn
) == TEMPLATE_DECL
9991 && !PRIMARY_TEMPLATE_P (fn
))
9992 fn
= DECL_TEMPLATE_RESULT (fn
);
9993 /* Check to see that the declaration is really present, and,
9994 possibly obtain an improved declaration. */
9995 fn
= check_classfn (context
, fn
, NULL_TREE
);
10005 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10006 template arguments, as for tsubst.
10008 Returns an appropriate tsubst'd friend type or error_mark_node on
10012 tsubst_friend_class (tree friend_tmpl
, tree args
)
10016 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10018 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10019 return TREE_TYPE (tmpl
);
10022 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10023 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10024 push_nested_namespace (context
);
10026 push_nested_class (context
);
10028 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10029 /*non_class=*/false, /*block_p=*/false,
10030 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10032 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10034 /* The friend template has already been declared. Just
10035 check to see that the declarations match, and install any new
10036 default parameters. We must tsubst the default parameters,
10037 of course. We only need the innermost template parameters
10038 because that is all that redeclare_class_template will look
10040 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10041 > TMPL_ARGS_DEPTH (args
))
10043 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10044 args
, tf_warning_or_error
);
10045 location_t saved_input_location
= input_location
;
10046 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10047 tree cons
= get_constraints (tmpl
);
10048 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10049 input_location
= saved_input_location
;
10054 /* The friend template has not already been declared. In this
10055 case, the instantiation of the template class will cause the
10056 injection of this template into the namespace scope. */
10057 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10059 if (tmpl
!= error_mark_node
)
10061 /* The new TMPL is not an instantiation of anything, so we
10062 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10063 for the new type because that is supposed to be the
10064 corresponding template decl, i.e., TMPL. */
10065 DECL_USE_TEMPLATE (tmpl
) = 0;
10066 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10067 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10068 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10069 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10071 /* It is hidden. */
10072 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10073 DECL_ANTICIPATED (tmpl
)
10074 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10076 /* Inject this template into the enclosing namspace scope. */
10077 tmpl
= pushdecl_namespace_level (tmpl
, true);
10081 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10082 pop_nested_namespace (context
);
10084 pop_nested_class ();
10086 return TREE_TYPE (tmpl
);
10089 /* Returns zero if TYPE cannot be completed later due to circularity.
10090 Otherwise returns one. */
10093 can_complete_type_without_circularity (tree type
)
10095 if (type
== NULL_TREE
|| type
== error_mark_node
)
10097 else if (COMPLETE_TYPE_P (type
))
10099 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10100 return can_complete_type_without_circularity (TREE_TYPE (type
));
10101 else if (CLASS_TYPE_P (type
)
10102 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10108 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10109 tsubst_flags_t
, tree
);
10111 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10112 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10115 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10116 tsubst_flags_t complain
, tree in_decl
)
10118 gcc_assert (ATTR_IS_DEPENDENT (t
));
10120 tree val
= TREE_VALUE (t
);
10121 if (val
== NULL_TREE
)
10122 /* Nothing to do. */;
10123 else if ((flag_openmp
|| flag_openmp_simd
)
10124 && is_attribute_p ("omp declare simd",
10125 get_attribute_name (t
)))
10127 tree clauses
= TREE_VALUE (val
);
10128 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10129 complain
, in_decl
);
10130 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10131 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10132 tree parms
= DECL_ARGUMENTS (*decl_p
);
10134 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10136 val
= build_tree_list (NULL_TREE
, clauses
);
10140 /* If the first attribute argument is an identifier, don't
10141 pass it through tsubst. Attributes like mode, format,
10142 cleanup and several target specific attributes expect it
10144 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10147 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10148 /*integral_constant_expression_p=*/false);
10149 if (chain
!= TREE_CHAIN (val
))
10150 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10152 else if (PACK_EXPANSION_P (val
))
10154 /* An attribute pack expansion. */
10155 tree purp
= TREE_PURPOSE (t
);
10156 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10157 if (pack
== error_mark_node
)
10158 return error_mark_node
;
10159 int len
= TREE_VEC_LENGTH (pack
);
10160 tree list
= NULL_TREE
;
10162 for (int i
= 0; i
< len
; ++i
)
10164 tree elt
= TREE_VEC_ELT (pack
, i
);
10165 *q
= build_tree_list (purp
, elt
);
10166 q
= &TREE_CHAIN (*q
);
10171 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10172 /*integral_constant_expression_p=*/false);
10174 if (val
!= TREE_VALUE (t
))
10175 return build_tree_list (TREE_PURPOSE (t
), val
);
10179 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10180 unchanged or a new TREE_LIST chain. */
10183 tsubst_attributes (tree attributes
, tree args
,
10184 tsubst_flags_t complain
, tree in_decl
)
10186 tree last_dep
= NULL_TREE
;
10188 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10189 if (ATTR_IS_DEPENDENT (t
))
10192 attributes
= copy_list (attributes
);
10197 for (tree
*p
= &attributes
; *p
; )
10200 if (ATTR_IS_DEPENDENT (t
))
10202 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10207 p
= &TREE_CHAIN (*p
);
10209 *p
= TREE_CHAIN (t
);
10213 p
= &TREE_CHAIN (*p
);
10219 /* Apply any attributes which had to be deferred until instantiation
10220 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10221 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10224 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10225 tree args
, tsubst_flags_t complain
, tree in_decl
)
10227 tree last_dep
= NULL_TREE
;
10231 if (attributes
== NULL_TREE
)
10234 if (DECL_P (*decl_p
))
10236 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10238 p
= &DECL_ATTRIBUTES (*decl_p
);
10239 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10240 to our attributes parameter. */
10241 gcc_assert (*p
== attributes
);
10245 p
= &TYPE_ATTRIBUTES (*decl_p
);
10246 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10247 lookup_template_class_1, and should be preserved. */
10248 gcc_assert (*p
!= attributes
);
10250 p
= &TREE_CHAIN (*p
);
10253 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10254 if (ATTR_IS_DEPENDENT (t
))
10257 attributes
= copy_list (attributes
);
10264 tree late_attrs
= NULL_TREE
;
10265 tree
*q
= &late_attrs
;
10270 if (ATTR_IS_DEPENDENT (t
))
10272 *p
= TREE_CHAIN (t
);
10273 TREE_CHAIN (t
) = NULL_TREE
;
10274 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10276 q
= &TREE_CHAIN (*q
);
10280 p
= &TREE_CHAIN (t
);
10283 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10287 /* Perform (or defer) access check for typedefs that were referenced
10288 from within the template TMPL code.
10289 This is a subroutine of instantiate_decl and instantiate_class_template.
10290 TMPL is the template to consider and TARGS is the list of arguments of
10294 perform_typedefs_access_check (tree tmpl
, tree targs
)
10296 location_t saved_location
;
10298 qualified_typedef_usage_t
*iter
;
10301 || (!CLASS_TYPE_P (tmpl
)
10302 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10305 saved_location
= input_location
;
10306 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10308 tree type_decl
= iter
->typedef_decl
;
10309 tree type_scope
= iter
->context
;
10311 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10314 if (uses_template_parms (type_decl
))
10315 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10316 if (uses_template_parms (type_scope
))
10317 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10319 /* Make access check error messages point to the location
10320 of the use of the typedef. */
10321 input_location
= iter
->locus
;
10322 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10323 type_decl
, type_decl
,
10324 tf_warning_or_error
);
10326 input_location
= saved_location
;
10330 instantiate_class_template_1 (tree type
)
10332 tree templ
, args
, pattern
, t
, member
;
10336 unsigned int saved_maximum_field_alignment
;
10339 if (type
== error_mark_node
)
10340 return error_mark_node
;
10342 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10343 || uses_template_parms (type
))
10346 /* Figure out which template is being instantiated. */
10347 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10348 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10350 /* Determine what specialization of the original template to
10352 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10353 if (t
== error_mark_node
)
10355 TYPE_BEING_DEFINED (type
) = 1;
10356 return error_mark_node
;
10360 /* This TYPE is actually an instantiation of a partial
10361 specialization. We replace the innermost set of ARGS with
10362 the arguments appropriate for substitution. For example,
10365 template <class T> struct S {};
10366 template <class T> struct S<T*> {};
10368 and supposing that we are instantiating S<int*>, ARGS will
10369 presently be {int*} -- but we need {int}. */
10370 pattern
= TREE_TYPE (t
);
10371 args
= TREE_PURPOSE (t
);
10375 pattern
= TREE_TYPE (templ
);
10376 args
= CLASSTYPE_TI_ARGS (type
);
10379 /* If the template we're instantiating is incomplete, then clearly
10380 there's nothing we can do. */
10381 if (!COMPLETE_TYPE_P (pattern
))
10384 /* If we've recursively instantiated too many templates, stop. */
10385 if (! push_tinst_level (type
))
10388 /* Now we're really doing the instantiation. Mark the type as in
10389 the process of being defined. */
10390 TYPE_BEING_DEFINED (type
) = 1;
10392 /* We may be in the middle of deferred access check. Disable
10394 push_deferring_access_checks (dk_no_deferred
);
10396 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10397 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10399 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10400 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10401 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10402 fn_context
= error_mark_node
;
10404 push_to_top_level ();
10407 cp_unevaluated_operand
= 0;
10408 c_inhibit_evaluation_warnings
= 0;
10410 /* Use #pragma pack from the template context. */
10411 saved_maximum_field_alignment
= maximum_field_alignment
;
10412 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10414 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10416 /* Set the input location to the most specialized template definition.
10417 This is needed if tsubsting causes an error. */
10418 typedecl
= TYPE_MAIN_DECL (pattern
);
10419 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10420 DECL_SOURCE_LOCATION (typedecl
);
10422 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10423 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10424 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10425 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10426 if (ANON_AGGR_TYPE_P (pattern
))
10427 SET_ANON_AGGR_TYPE_P (type
);
10428 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10430 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10431 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10432 /* Adjust visibility for template arguments. */
10433 determine_visibility (TYPE_MAIN_DECL (type
));
10435 if (CLASS_TYPE_P (type
))
10436 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10438 pbinfo
= TYPE_BINFO (pattern
);
10440 /* We should never instantiate a nested class before its enclosing
10441 class; we need to look up the nested class by name before we can
10442 instantiate it, and that lookup should instantiate the enclosing
10444 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10445 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10447 base_list
= NULL_TREE
;
10448 if (BINFO_N_BASE_BINFOS (pbinfo
))
10454 /* We must enter the scope containing the type, as that is where
10455 the accessibility of types named in dependent bases are
10457 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10459 /* Substitute into each of the bases to determine the actual
10461 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10464 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10465 tree expanded_bases
= NULL_TREE
;
10468 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10471 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10472 args
, tf_error
, NULL_TREE
);
10473 if (expanded_bases
== error_mark_node
)
10476 len
= TREE_VEC_LENGTH (expanded_bases
);
10479 for (idx
= 0; idx
< len
; idx
++)
10481 if (expanded_bases
)
10482 /* Extract the already-expanded base class. */
10483 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10485 /* Substitute to figure out the base class. */
10486 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10489 if (base
== error_mark_node
)
10492 base_list
= tree_cons (access
, base
, base_list
);
10493 if (BINFO_VIRTUAL_P (pbase_binfo
))
10494 TREE_TYPE (base_list
) = integer_type_node
;
10498 /* The list is now in reverse order; correct that. */
10499 base_list
= nreverse (base_list
);
10502 pop_scope (pushed_scope
);
10504 /* Now call xref_basetypes to set up all the base-class
10506 xref_basetypes (type
, base_list
);
10508 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10509 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10510 args
, tf_error
, NULL_TREE
);
10511 fixup_attribute_variants (type
);
10513 /* Now that our base classes are set up, enter the scope of the
10514 class, so that name lookups into base classes, etc. will work
10515 correctly. This is precisely analogous to what we do in
10516 begin_class_definition when defining an ordinary non-template
10517 class, except we also need to push the enclosing classes. */
10518 push_nested_class (type
);
10520 /* Now members are processed in the order of declaration. */
10521 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10522 member
; member
= TREE_CHAIN (member
))
10524 tree t
= TREE_VALUE (member
);
10526 if (TREE_PURPOSE (member
))
10530 if (LAMBDA_TYPE_P (t
))
10531 /* A closure type for a lambda in an NSDMI or default argument.
10532 Ignore it; it will be regenerated when needed. */
10535 /* Build new CLASSTYPE_NESTED_UTDS. */
10538 bool class_template_p
;
10540 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10541 && TYPE_LANG_SPECIFIC (t
)
10542 && CLASSTYPE_IS_TEMPLATE (t
));
10543 /* If the member is a class template, then -- even after
10544 substitution -- there may be dependent types in the
10545 template argument list for the class. We increment
10546 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10547 that function will assume that no types are dependent
10548 when outside of a template. */
10549 if (class_template_p
)
10550 ++processing_template_decl
;
10551 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10552 if (class_template_p
)
10553 --processing_template_decl
;
10554 if (newtag
== error_mark_node
)
10557 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10559 tree name
= TYPE_IDENTIFIER (t
);
10561 if (class_template_p
)
10562 /* Unfortunately, lookup_template_class sets
10563 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10564 instantiation (i.e., for the type of a member
10565 template class nested within a template class.)
10566 This behavior is required for
10567 maybe_process_partial_specialization to work
10568 correctly, but is not accurate in this case;
10569 the TAG is not an instantiation of anything.
10570 (The corresponding TEMPLATE_DECL is an
10571 instantiation, but the TYPE is not.) */
10572 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10574 /* Now, we call pushtag to put this NEWTAG into the scope of
10575 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10576 pushtag calling push_template_decl. We don't have to do
10577 this for enums because it will already have been done in
10580 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10581 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10584 else if (DECL_DECLARES_FUNCTION_P (t
))
10588 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10589 ++processing_template_decl
;
10590 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10591 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10592 --processing_template_decl
;
10593 set_current_access_from_decl (r
);
10594 finish_member_declaration (r
);
10595 /* Instantiate members marked with attribute used. */
10596 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10598 if (TREE_CODE (r
) == FUNCTION_DECL
10599 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10600 cp_check_omp_declare_reduction (r
);
10602 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
10603 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10604 /* A closure type for a lambda in an NSDMI or default argument.
10605 Ignore it; it will be regenerated when needed. */;
10608 /* Build new TYPE_FIELDS. */
10609 if (TREE_CODE (t
) == STATIC_ASSERT
)
10613 ++c_inhibit_evaluation_warnings
;
10615 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10616 tf_warning_or_error
, NULL_TREE
,
10617 /*integral_constant_expression_p=*/true);
10618 --c_inhibit_evaluation_warnings
;
10620 finish_static_assert (condition
,
10621 STATIC_ASSERT_MESSAGE (t
),
10622 STATIC_ASSERT_SOURCE_LOCATION (t
),
10623 /*member_p=*/true);
10625 else if (TREE_CODE (t
) != CONST_DECL
)
10628 tree vec
= NULL_TREE
;
10631 /* The file and line for this declaration, to
10632 assist in error message reporting. Since we
10633 called push_tinst_level above, we don't need to
10635 input_location
= DECL_SOURCE_LOCATION (t
);
10637 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10638 ++processing_template_decl
;
10639 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10640 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10641 --processing_template_decl
;
10643 if (TREE_CODE (r
) == TREE_VEC
)
10645 /* A capture pack became multiple fields. */
10647 len
= TREE_VEC_LENGTH (vec
);
10650 for (int i
= 0; i
< len
; ++i
)
10653 r
= TREE_VEC_ELT (vec
, i
);
10658 [t]he initialization (and any associated
10659 side-effects) of a static data member does
10660 not occur unless the static data member is
10661 itself used in a way that requires the
10662 definition of the static data member to
10665 Therefore, we do not substitute into the
10666 initialized for the static data member here. */
10667 finish_static_data_member_decl
10669 /*init=*/NULL_TREE
,
10670 /*init_const_expr_p=*/false,
10671 /*asmspec_tree=*/NULL_TREE
,
10673 /* Instantiate members marked with attribute used. */
10674 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10677 else if (TREE_CODE (r
) == FIELD_DECL
)
10679 /* Determine whether R has a valid type and can be
10680 completed later. If R is invalid, then its type
10681 is replaced by error_mark_node. */
10682 tree rtype
= TREE_TYPE (r
);
10683 if (can_complete_type_without_circularity (rtype
))
10684 complete_type (rtype
);
10686 if (!complete_or_array_type_p (rtype
))
10688 /* If R's type couldn't be completed and
10689 it isn't a flexible array member (whose
10690 type is incomplete by definition) give
10692 cxx_incomplete_type_error (r
, rtype
);
10693 TREE_TYPE (r
) = error_mark_node
;
10697 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10698 such a thing will already have been added to the field
10699 list by tsubst_enum in finish_member_declaration in the
10700 CLASSTYPE_NESTED_UTDS case above. */
10701 if (!(TREE_CODE (r
) == TYPE_DECL
10702 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10703 && DECL_ARTIFICIAL (r
)))
10705 set_current_access_from_decl (r
);
10706 finish_member_declaration (r
);
10714 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10715 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10717 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10719 tree friend_type
= t
;
10720 bool adjust_processing_template_decl
= false;
10722 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10724 /* template <class T> friend class C; */
10725 friend_type
= tsubst_friend_class (friend_type
, args
);
10726 adjust_processing_template_decl
= true;
10728 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10730 /* template <class T> friend class C::D; */
10731 friend_type
= tsubst (friend_type
, args
,
10732 tf_warning_or_error
, NULL_TREE
);
10733 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10734 friend_type
= TREE_TYPE (friend_type
);
10735 adjust_processing_template_decl
= true;
10737 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10738 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10740 /* This could be either
10744 when dependent_type_p is false or
10746 template <class U> friend class T::C;
10749 /* Bump processing_template_decl in case this is something like
10750 template <class T> friend struct A<T>::B. */
10751 ++processing_template_decl
;
10752 friend_type
= tsubst (friend_type
, args
,
10753 tf_warning_or_error
, NULL_TREE
);
10754 if (dependent_type_p (friend_type
))
10755 adjust_processing_template_decl
= true;
10756 --processing_template_decl
;
10758 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10759 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10760 && TYPE_HIDDEN_P (friend_type
))
10764 where C hasn't been declared yet. Let's lookup name
10765 from namespace scope directly, bypassing any name that
10766 come from dependent base class. */
10767 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10769 /* The call to xref_tag_from_type does injection for friend
10771 push_nested_namespace (ns
);
10773 xref_tag_from_type (friend_type
, NULL_TREE
,
10774 /*tag_scope=*/ts_current
);
10775 pop_nested_namespace (ns
);
10777 else if (uses_template_parms (friend_type
))
10778 /* friend class C<T>; */
10779 friend_type
= tsubst (friend_type
, args
,
10780 tf_warning_or_error
, NULL_TREE
);
10785 where C is already declared or
10787 friend class C<int>;
10789 We don't have to do anything in these cases. */
10791 if (adjust_processing_template_decl
)
10792 /* Trick make_friend_class into realizing that the friend
10793 we're adding is a template, not an ordinary class. It's
10794 important that we use make_friend_class since it will
10795 perform some error-checking and output cross-reference
10797 ++processing_template_decl
;
10799 if (friend_type
!= error_mark_node
)
10800 make_friend_class (type
, friend_type
, /*complain=*/false);
10802 if (adjust_processing_template_decl
)
10803 --processing_template_decl
;
10807 /* Build new DECL_FRIENDLIST. */
10810 /* The file and line for this declaration, to
10811 assist in error message reporting. Since we
10812 called push_tinst_level above, we don't need to
10814 input_location
= DECL_SOURCE_LOCATION (t
);
10816 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10818 ++processing_template_decl
;
10819 push_deferring_access_checks (dk_no_check
);
10822 r
= tsubst_friend_function (t
, args
);
10823 add_friend (type
, r
, /*complain=*/false);
10824 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10826 pop_deferring_access_checks ();
10827 --processing_template_decl
;
10835 /* Restore these before substituting into the lambda capture
10837 cp_unevaluated_operand
= saved_unevaluated_operand
;
10838 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10841 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10843 tree decl
= lambda_function (type
);
10846 if (cxx_dialect
>= cxx17
)
10847 CLASSTYPE_LITERAL_P (type
) = true;
10849 if (!DECL_TEMPLATE_INFO (decl
)
10850 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10852 /* Set function_depth to avoid garbage collection. */
10854 instantiate_decl (decl
, /*defer_ok=*/false, false);
10858 /* We need to instantiate the capture list from the template
10859 after we've instantiated the closure members, but before we
10860 consider adding the conversion op. Also keep any captures
10861 that may have been added during instantiation of the op(). */
10862 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10864 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10865 args
, tf_warning_or_error
, NULL_TREE
,
10868 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10869 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10871 maybe_add_lambda_conv_op (type
);
10874 gcc_assert (errorcount
);
10877 /* Set the file and line number information to whatever is given for
10878 the class itself. This puts error messages involving generated
10879 implicit functions at a predictable point, and the same point
10880 that would be used for non-template classes. */
10881 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10883 unreverse_member_declarations (type
);
10884 finish_struct_1 (type
);
10885 TYPE_BEING_DEFINED (type
) = 0;
10887 /* We don't instantiate default arguments for member functions. 14.7.1:
10889 The implicit instantiation of a class template specialization causes
10890 the implicit instantiation of the declarations, but not of the
10891 definitions or default arguments, of the class member functions,
10892 member classes, static data members and member templates.... */
10894 /* Some typedefs referenced from within the template code need to be access
10895 checked at template instantiation time, i.e now. These types were
10896 added to the template at parsing time. Let's get those and perform
10897 the access checks then. */
10898 perform_typedefs_access_check (pattern
, args
);
10899 perform_deferred_access_checks (tf_warning_or_error
);
10900 pop_nested_class ();
10901 maximum_field_alignment
= saved_maximum_field_alignment
;
10903 pop_from_top_level ();
10904 pop_deferring_access_checks ();
10905 pop_tinst_level ();
10907 /* The vtable for a template class can be emitted in any translation
10908 unit in which the class is instantiated. When there is no key
10909 method, however, finish_struct_1 will already have added TYPE to
10910 the keyed_classes. */
10911 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10912 vec_safe_push (keyed_classes
, type
);
10917 /* Wrapper for instantiate_class_template_1. */
10920 instantiate_class_template (tree type
)
10923 timevar_push (TV_TEMPLATE_INST
);
10924 ret
= instantiate_class_template_1 (type
);
10925 timevar_pop (TV_TEMPLATE_INST
);
10930 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10936 else if (TYPE_P (t
))
10937 r
= tsubst (t
, args
, complain
, in_decl
);
10940 if (!(complain
& tf_warning
))
10941 ++c_inhibit_evaluation_warnings
;
10942 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10943 /*integral_constant_expression_p=*/true);
10944 if (!(complain
& tf_warning
))
10945 --c_inhibit_evaluation_warnings
;
10950 /* Given a function parameter pack TMPL_PARM and some function parameters
10951 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10952 and set *SPEC_P to point at the next point in the list. */
10955 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10957 /* Collect all of the extra "packed" parameters into an
10960 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10961 tree spec_parm
= *spec_p
;
10964 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10966 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10969 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10970 parmvec
= make_tree_vec (len
);
10971 spec_parm
= *spec_p
;
10972 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10974 tree elt
= spec_parm
;
10975 if (DECL_PACK_P (elt
))
10976 elt
= make_pack_expansion (elt
);
10977 TREE_VEC_ELT (parmvec
, i
) = elt
;
10980 /* Build the argument packs. */
10981 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10982 *spec_p
= spec_parm
;
10987 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10988 NONTYPE_ARGUMENT_PACK. */
10991 make_fnparm_pack (tree spec_parm
)
10993 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10996 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10997 pack expansion with no extra args, 2 if it has extra args, or 0
10998 if it is not a pack expansion. */
11001 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11003 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11004 if (i
>= TREE_VEC_LENGTH (vec
))
11006 tree elt
= TREE_VEC_ELT (vec
, i
);
11008 /* A decl pack is itself an expansion. */
11009 elt
= TREE_TYPE (elt
);
11010 if (!PACK_EXPANSION_P (elt
))
11012 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11018 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11021 make_argument_pack_select (tree arg_pack
, unsigned index
)
11023 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11025 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11026 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11031 /* This is a subroutine of tsubst_pack_expansion.
11033 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11034 mechanism to store the (non complete list of) arguments of the
11035 substitution and return a non substituted pack expansion, in order
11036 to wait for when we have enough arguments to really perform the
11040 use_pack_expansion_extra_args_p (tree parm_packs
,
11042 bool has_empty_arg
)
11044 /* If one pack has an expansion and another pack has a normal
11045 argument or if one pack has an empty argument and an another
11046 one hasn't then tsubst_pack_expansion cannot perform the
11047 substitution and need to fall back on the
11048 PACK_EXPANSION_EXTRA mechanism. */
11049 if (parm_packs
== NULL_TREE
)
11051 else if (has_empty_arg
)
11054 bool has_expansion_arg
= false;
11055 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11057 bool has_non_expansion_arg
= false;
11058 for (tree parm_pack
= parm_packs
;
11060 parm_pack
= TREE_CHAIN (parm_pack
))
11062 tree arg
= TREE_VALUE (parm_pack
);
11064 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11066 /* We can't substitute a pack expansion with extra args into
11070 has_expansion_arg
= true;
11072 has_non_expansion_arg
= true;
11075 if (has_expansion_arg
&& has_non_expansion_arg
)
11081 /* [temp.variadic]/6 says that:
11083 The instantiation of a pack expansion [...]
11084 produces a list E1,E2, ..., En, where N is the number of elements
11085 in the pack expansion parameters.
11087 This subroutine of tsubst_pack_expansion produces one of these Ei.
11089 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11090 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11091 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11092 INDEX is the index 'i' of the element Ei to produce. ARGS,
11093 COMPLAIN, and IN_DECL are the same parameters as for the
11094 tsubst_pack_expansion function.
11096 The function returns the resulting Ei upon successful completion,
11097 or error_mark_node.
11099 Note that this function possibly modifies the ARGS parameter, so
11100 it's the responsibility of the caller to restore it. */
11103 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11106 tree args
/* This parm gets
11108 tsubst_flags_t complain
,
11112 bool ith_elem_is_expansion
= false;
11114 /* For each parameter pack, change the substitution of the parameter
11115 pack to the ith argument in its argument pack, then expand the
11117 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11119 tree parm
= TREE_PURPOSE (pack
);
11120 tree arg_pack
= TREE_VALUE (pack
);
11121 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11123 ith_elem_is_expansion
|=
11124 argument_pack_element_is_expansion_p (arg_pack
, index
);
11126 /* Select the Ith argument from the pack. */
11127 if (TREE_CODE (parm
) == PARM_DECL
11128 || TREE_CODE (parm
) == FIELD_DECL
)
11132 aps
= make_argument_pack_select (arg_pack
, index
);
11133 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11134 return error_mark_node
;
11135 register_local_specialization (aps
, parm
);
11138 aps
= retrieve_local_specialization (parm
);
11143 template_parm_level_and_index (parm
, &level
, &idx
);
11147 aps
= make_argument_pack_select (arg_pack
, index
);
11148 /* Update the corresponding argument. */
11149 TMPL_ARG (args
, level
, idx
) = aps
;
11152 /* Re-use the ARGUMENT_PACK_SELECT. */
11153 aps
= TMPL_ARG (args
, level
, idx
);
11155 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11158 /* Substitute into the PATTERN with the (possibly altered)
11160 if (pattern
== in_decl
)
11161 /* Expanding a fixed parameter pack from
11162 coerce_template_parameter_pack. */
11163 t
= tsubst_decl (pattern
, args
, complain
);
11164 else if (pattern
== error_mark_node
)
11165 t
= error_mark_node
;
11166 else if (constraint_p (pattern
))
11168 if (processing_template_decl
)
11169 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11171 t
= (constraints_satisfied_p (pattern
, args
)
11172 ? boolean_true_node
: boolean_false_node
);
11174 else if (!TYPE_P (pattern
))
11175 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11176 /*integral_constant_expression_p=*/false);
11178 t
= tsubst (pattern
, args
, complain
, in_decl
);
11180 /* If the Ith argument pack element is a pack expansion, then
11181 the Ith element resulting from the substituting is going to
11182 be a pack expansion as well. */
11183 if (ith_elem_is_expansion
)
11184 t
= make_pack_expansion (t
, complain
);
11189 /* When the unexpanded parameter pack in a fold expression expands to an empty
11190 sequence, the value of the expression is as follows; the program is
11191 ill-formed if the operator is not listed in this table.
11198 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11200 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11201 if (!FOLD_EXPR_MODIFY_P (t
))
11204 case TRUTH_ANDIF_EXPR
:
11205 return boolean_true_node
;
11206 case TRUTH_ORIF_EXPR
:
11207 return boolean_false_node
;
11208 case COMPOUND_EXPR
:
11214 if (complain
& tf_error
)
11215 error_at (location_of (t
),
11216 "fold of empty expansion over %O", code
);
11217 return error_mark_node
;
11220 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11221 form an expression that combines the two terms using the
11225 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11227 tree op
= FOLD_EXPR_OP (t
);
11228 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11230 // Handle compound assignment operators.
11231 if (FOLD_EXPR_MODIFY_P (t
))
11232 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11236 case COMPOUND_EXPR
:
11237 return build_x_compound_expr (input_location
, left
, right
, complain
);
11239 return build_m_component_ref (left
, right
, complain
);
11241 return build_x_binary_op (input_location
, code
,
11242 left
, TREE_CODE (left
),
11243 right
, TREE_CODE (right
),
11249 /* Substitute ARGS into the pack of a fold expression T. */
11252 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11254 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11257 /* Substitute ARGS into the pack of a fold expression T. */
11260 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11262 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11265 /* Expand a PACK of arguments into a grouped as left fold.
11266 Given a pack containing elements A0, A1, ..., An and an
11267 operator @, this builds the expression:
11269 ((A0 @ A1) @ A2) ... @ An
11271 Note that PACK must not be empty.
11273 The operator is defined by the original fold expression T. */
11276 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11278 tree left
= TREE_VEC_ELT (pack
, 0);
11279 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11281 tree right
= TREE_VEC_ELT (pack
, i
);
11282 left
= fold_expression (t
, left
, right
, complain
);
11287 /* Substitute into a unary left fold expression. */
11290 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11293 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11294 if (pack
== error_mark_node
)
11295 return error_mark_node
;
11296 if (PACK_EXPANSION_P (pack
))
11298 tree r
= copy_node (t
);
11299 FOLD_EXPR_PACK (r
) = pack
;
11302 if (TREE_VEC_LENGTH (pack
) == 0)
11303 return expand_empty_fold (t
, complain
);
11305 return expand_left_fold (t
, pack
, complain
);
11308 /* Substitute into a binary left fold expression.
11310 Do ths by building a single (non-empty) vector of argumnts and
11311 building the expression from those elements. */
11314 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11317 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11318 if (pack
== error_mark_node
)
11319 return error_mark_node
;
11320 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11321 if (init
== error_mark_node
)
11322 return error_mark_node
;
11324 if (PACK_EXPANSION_P (pack
))
11326 tree r
= copy_node (t
);
11327 FOLD_EXPR_PACK (r
) = pack
;
11328 FOLD_EXPR_INIT (r
) = init
;
11332 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11333 TREE_VEC_ELT (vec
, 0) = init
;
11334 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11335 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11337 return expand_left_fold (t
, vec
, complain
);
11340 /* Expand a PACK of arguments into a grouped as right fold.
11341 Given a pack containing elementns A0, A1, ..., and an
11342 operator @, this builds the expression:
11344 A0@ ... (An-2 @ (An-1 @ An))
11346 Note that PACK must not be empty.
11348 The operator is defined by the original fold expression T. */
11351 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11353 // Build the expression.
11354 int n
= TREE_VEC_LENGTH (pack
);
11355 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11356 for (--n
; n
!= 0; --n
)
11358 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11359 right
= fold_expression (t
, left
, right
, complain
);
11364 /* Substitute into a unary right fold expression. */
11367 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11370 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11371 if (pack
== error_mark_node
)
11372 return error_mark_node
;
11373 if (PACK_EXPANSION_P (pack
))
11375 tree r
= copy_node (t
);
11376 FOLD_EXPR_PACK (r
) = pack
;
11379 if (TREE_VEC_LENGTH (pack
) == 0)
11380 return expand_empty_fold (t
, complain
);
11382 return expand_right_fold (t
, pack
, complain
);
11385 /* Substitute into a binary right fold expression.
11387 Do ths by building a single (non-empty) vector of arguments and
11388 building the expression from those elements. */
11391 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11394 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11395 if (pack
== error_mark_node
)
11396 return error_mark_node
;
11397 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11398 if (init
== error_mark_node
)
11399 return error_mark_node
;
11401 if (PACK_EXPANSION_P (pack
))
11403 tree r
= copy_node (t
);
11404 FOLD_EXPR_PACK (r
) = pack
;
11405 FOLD_EXPR_INIT (r
) = init
;
11409 int n
= TREE_VEC_LENGTH (pack
);
11410 tree vec
= make_tree_vec (n
+ 1);
11411 for (int i
= 0; i
< n
; ++i
)
11412 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11413 TREE_VEC_ELT (vec
, n
) = init
;
11415 return expand_right_fold (t
, vec
, complain
);
11418 /* Walk through the pattern of a pack expansion, adding everything in
11419 local_specializations to a list. */
11422 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data
)
11424 tree
*extra
= reinterpret_cast<tree
*>(data
);
11425 if (tree spec
= retrieve_local_specialization (*tp
))
11427 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11429 /* Pull out the actual PARM_DECL for the partial instantiation. */
11430 tree args
= ARGUMENT_PACK_ARGS (spec
);
11431 gcc_assert (TREE_VEC_LENGTH (args
) == 1);
11432 tree arg
= TREE_VEC_ELT (args
, 0);
11433 spec
= PACK_EXPANSION_PATTERN (arg
);
11435 *extra
= tree_cons (*tp
, spec
, *extra
);
11440 extract_local_specs (tree pattern
)
11442 tree extra
= NULL_TREE
;
11443 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &extra
);
11447 /* Substitute ARGS into T, which is an pack expansion
11448 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11449 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11450 (if only a partial substitution could be performed) or
11451 ERROR_MARK_NODE if there was an error. */
11453 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11457 tree pack
, packs
= NULL_TREE
;
11458 bool unsubstituted_packs
= false;
11459 bool unsubstituted_fn_pack
= false;
11462 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11463 bool need_local_specializations
= false;
11466 gcc_assert (PACK_EXPANSION_P (t
));
11467 pattern
= PACK_EXPANSION_PATTERN (t
);
11469 /* Add in any args remembered from an earlier partial instantiation. */
11470 tree extra
= PACK_EXPANSION_EXTRA_ARGS (t
);
11471 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
11473 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
11475 /* The partial instantiation involved local declarations collected in
11476 extract_local_specs; map from the general template to our local
11478 tree gen
= TREE_PURPOSE (elt
);
11479 tree partial
= TREE_VALUE (elt
);
11480 tree inst
= retrieve_local_specialization (partial
);
11481 register_local_specialization (inst
, gen
);
11483 gcc_assert (!TREE_PURPOSE (extra
));
11484 extra
= TREE_VALUE (extra
);
11486 args
= add_to_template_args (extra
, args
);
11488 levels
= TMPL_ARGS_DEPTH (args
);
11490 /* Determine the argument packs that will instantiate the parameter
11491 packs used in the expansion expression. While we're at it,
11492 compute the number of arguments to be expanded and make sure it
11494 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11495 pack
= TREE_CHAIN (pack
))
11497 tree parm_pack
= TREE_VALUE (pack
);
11498 tree arg_pack
= NULL_TREE
;
11499 tree orig_arg
= NULL_TREE
;
11502 if (TREE_CODE (parm_pack
) == BASES
)
11504 gcc_assert (parm_pack
== pattern
);
11505 if (BASES_DIRECT (parm_pack
))
11506 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11507 args
, complain
, in_decl
, false));
11509 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11510 args
, complain
, in_decl
, false));
11512 else if (builtin_pack_call_p (parm_pack
))
11514 /* ??? Support use in other patterns. */
11515 gcc_assert (parm_pack
== pattern
);
11516 return expand_builtin_pack_call (parm_pack
, args
,
11517 complain
, in_decl
);
11519 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11521 /* We know we have correct local_specializations if this
11522 expansion is at function scope, or if we're dealing with a
11523 local parameter in a requires expression; for the latter,
11524 tsubst_requires_expr set it up appropriately. */
11525 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11526 arg_pack
= retrieve_local_specialization (parm_pack
);
11528 /* We can't rely on local_specializations for a parameter
11529 name used later in a function declaration (such as in a
11530 late-specified return type). Even if it exists, it might
11531 have the wrong value for a recursive call. */
11532 need_local_specializations
= true;
11536 /* This parameter pack was used in an unevaluated context. Just
11537 make a dummy decl, since it's only used for its type. */
11538 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11539 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11540 /* Partial instantiation of the parm_pack, we can't build
11541 up an argument pack yet. */
11542 arg_pack
= NULL_TREE
;
11544 arg_pack
= make_fnparm_pack (arg_pack
);
11546 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11547 /* This argument pack isn't fully instantiated yet. We set this
11548 flag rather than clear arg_pack because we do want to do the
11549 optimization below, and we don't want to substitute directly
11550 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11551 where it isn't expected). */
11552 unsubstituted_fn_pack
= true;
11554 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11555 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11559 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11561 if (level
<= levels
)
11562 arg_pack
= TMPL_ARG (args
, level
, idx
);
11565 orig_arg
= arg_pack
;
11566 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11567 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11569 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11570 /* This can only happen if we forget to expand an argument
11571 pack somewhere else. Just return an error, silently. */
11573 result
= make_tree_vec (1);
11574 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11581 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11583 /* Don't bother trying to do a partial substitution with
11584 incomplete packs; we'll try again after deduction. */
11585 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11590 else if (len
!= my_len
11591 && !unsubstituted_fn_pack
)
11593 if (!(complain
& tf_error
))
11594 /* Fail quietly. */;
11595 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11596 error ("mismatched argument pack lengths while expanding %qT",
11599 error ("mismatched argument pack lengths while expanding %qE",
11601 return error_mark_node
;
11604 /* Keep track of the parameter packs and their corresponding
11606 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11607 TREE_TYPE (packs
) = orig_arg
;
11611 /* We can't substitute for this parameter pack. We use a flag as
11612 well as the missing_level counter because function parameter
11613 packs don't have a level. */
11614 gcc_assert (processing_template_decl
);
11615 unsubstituted_packs
= true;
11619 /* If the expansion is just T..., return the matching argument pack, unless
11620 we need to call convert_from_reference on all the elements. This is an
11621 important optimization; see c++/68422. */
11622 if (!unsubstituted_packs
11623 && TREE_PURPOSE (packs
) == pattern
)
11625 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11627 /* If the argument pack is a single pack expansion, pull it out. */
11628 if (TREE_VEC_LENGTH (args
) == 1
11629 && pack_expansion_args_count (args
))
11630 return TREE_VEC_ELT (args
, 0);
11632 /* Types need no adjustment, nor does sizeof..., and if we still have
11633 some pack expansion args we won't do anything yet. */
11634 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11635 || PACK_EXPANSION_SIZEOF_P (t
)
11636 || pack_expansion_args_count (args
))
11638 /* Also optimize expression pack expansions if we can tell that the
11639 elements won't have reference type. */
11640 tree type
= TREE_TYPE (pattern
);
11641 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11642 && !PACK_EXPANSION_P (type
)
11643 && !WILDCARD_TYPE_P (type
))
11645 /* Otherwise use the normal path so we get convert_from_reference. */
11648 /* We cannot expand this expansion expression, because we don't have
11649 all of the argument packs we need. */
11650 if (use_pack_expansion_extra_args_p (packs
, len
, (unsubstituted_packs
11651 || unsubstituted_fn_pack
)))
11653 /* We got some full packs, but we can't substitute them in until we
11654 have values for all the packs. So remember these until then. */
11656 t
= make_pack_expansion (pattern
, complain
);
11658 if (unsubstituted_fn_pack
)
11659 if (tree locals
= extract_local_specs (pattern
))
11660 extra
= tree_cons (NULL_TREE
, extra
, locals
);
11661 PACK_EXPANSION_EXTRA_ARGS (t
) = extra
;
11664 else if (unsubstituted_packs
)
11666 /* There were no real arguments, we're just replacing a parameter
11667 pack with another version of itself. Substitute into the
11668 pattern and return a PACK_EXPANSION_*. The caller will need to
11670 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11671 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11672 /*integral_constant_expression_p=*/false);
11674 t
= tsubst (pattern
, args
, complain
, in_decl
);
11675 t
= make_pack_expansion (t
, complain
);
11679 gcc_assert (len
>= 0);
11681 if (need_local_specializations
)
11683 /* We're in a late-specified return type, so create our own local
11684 specializations map; the current map is either NULL or (in the
11685 case of recursive unification) might have bindings that we don't
11686 want to use or alter. */
11687 saved_local_specializations
= local_specializations
;
11688 local_specializations
= new hash_map
<tree
, tree
>;
11691 /* For each argument in each argument pack, substitute into the
11693 result
= make_tree_vec (len
);
11694 tree elem_args
= copy_template_args (args
);
11695 for (i
= 0; i
< len
; ++i
)
11697 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11699 elem_args
, complain
,
11701 TREE_VEC_ELT (result
, i
) = t
;
11702 if (t
== error_mark_node
)
11704 result
= error_mark_node
;
11709 /* Update ARGS to restore the substitution from parameter packs to
11710 their argument packs. */
11711 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11713 tree parm
= TREE_PURPOSE (pack
);
11715 if (TREE_CODE (parm
) == PARM_DECL
11716 || TREE_CODE (parm
) == FIELD_DECL
)
11717 register_local_specialization (TREE_TYPE (pack
), parm
);
11722 if (TREE_VALUE (pack
) == NULL_TREE
)
11725 template_parm_level_and_index (parm
, &level
, &idx
);
11727 /* Update the corresponding argument. */
11728 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11729 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11732 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11736 if (need_local_specializations
)
11738 delete local_specializations
;
11739 local_specializations
= saved_local_specializations
;
11742 /* If the dependent pack arguments were such that we end up with only a
11743 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11744 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11745 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11746 return TREE_VEC_ELT (result
, 0);
11751 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11752 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11753 parameter packs; all parms generated from a function parameter pack will
11754 have the same DECL_PARM_INDEX. */
11757 get_pattern_parm (tree parm
, tree tmpl
)
11759 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11762 if (DECL_ARTIFICIAL (parm
))
11764 for (patparm
= DECL_ARGUMENTS (pattern
);
11765 patparm
; patparm
= DECL_CHAIN (patparm
))
11766 if (DECL_ARTIFICIAL (patparm
)
11767 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11772 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11773 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11774 gcc_assert (DECL_PARM_INDEX (patparm
)
11775 == DECL_PARM_INDEX (parm
));
11781 /* Make an argument pack out of the TREE_VEC VEC. */
11784 make_argument_pack (tree vec
)
11787 tree elt
= TREE_VEC_ELT (vec
, 0);
11789 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11792 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11793 TREE_CONSTANT (pack
) = 1;
11795 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11799 /* Return an exact copy of template args T that can be modified
11803 copy_template_args (tree t
)
11805 if (t
== error_mark_node
)
11808 int len
= TREE_VEC_LENGTH (t
);
11809 tree new_vec
= make_tree_vec (len
);
11811 for (int i
= 0; i
< len
; ++i
)
11813 tree elt
= TREE_VEC_ELT (t
, i
);
11814 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11815 elt
= copy_template_args (elt
);
11816 TREE_VEC_ELT (new_vec
, i
) = elt
;
11819 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11820 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11825 /* Substitute ARGS into the vector or list of template arguments T. */
11828 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11831 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11834 if (t
== error_mark_node
)
11835 return error_mark_node
;
11837 len
= TREE_VEC_LENGTH (t
);
11838 elts
= XALLOCAVEC (tree
, len
);
11840 for (i
= 0; i
< len
; i
++)
11842 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11845 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11846 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11847 else if (PACK_EXPANSION_P (orig_arg
))
11849 /* Substitute into an expansion expression. */
11850 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11852 if (TREE_CODE (new_arg
) == TREE_VEC
)
11853 /* Add to the expanded length adjustment the number of
11854 expanded arguments. We subtract one from this
11855 measurement, because the argument pack expression
11856 itself is already counted as 1 in
11857 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11858 the argument pack is empty. */
11859 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11861 else if (ARGUMENT_PACK_P (orig_arg
))
11863 /* Substitute into each of the arguments. */
11864 new_arg
= TYPE_P (orig_arg
)
11865 ? cxx_make_type (TREE_CODE (orig_arg
))
11866 : make_node (TREE_CODE (orig_arg
));
11868 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11869 args
, complain
, in_decl
);
11870 if (pack_args
== error_mark_node
)
11871 new_arg
= error_mark_node
;
11873 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11875 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11876 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11879 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11881 if (new_arg
== error_mark_node
)
11882 return error_mark_node
;
11885 if (new_arg
!= orig_arg
)
11892 /* Make space for the expanded arguments coming from template
11894 t
= make_tree_vec (len
+ expanded_len_adjust
);
11895 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11896 arguments for a member template.
11897 In that case each TREE_VEC in ORIG_T represents a level of template
11898 arguments, and ORIG_T won't carry any non defaulted argument count.
11899 It will rather be the nested TREE_VECs that will carry one.
11900 In other words, ORIG_T carries a non defaulted argument count only
11901 if it doesn't contain any nested TREE_VEC. */
11902 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11904 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11905 count
+= expanded_len_adjust
;
11906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11908 for (i
= 0, out
= 0; i
< len
; i
++)
11910 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11911 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11912 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11916 /* Now expand the template argument pack "in place". */
11917 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11918 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11922 TREE_VEC_ELT (t
, out
) = elts
[i
];
11930 /* Substitute ARGS into one level PARMS of template parameters. */
11933 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11935 if (parms
== error_mark_node
)
11936 return error_mark_node
;
11938 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11940 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11942 tree tuple
= TREE_VEC_ELT (parms
, i
);
11944 if (tuple
== error_mark_node
)
11947 TREE_VEC_ELT (new_vec
, i
) =
11948 tsubst_template_parm (tuple
, args
, complain
);
11954 /* Return the result of substituting ARGS into the template parameters
11955 given by PARMS. If there are m levels of ARGS and m + n levels of
11956 PARMS, then the result will contain n levels of PARMS. For
11957 example, if PARMS is `template <class T> template <class U>
11958 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11959 result will be `template <int*, double, class V>'. */
11962 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11964 tree r
= NULL_TREE
;
11967 /* When substituting into a template, we must set
11968 PROCESSING_TEMPLATE_DECL as the template parameters may be
11969 dependent if they are based on one-another, and the dependency
11970 predicates are short-circuit outside of templates. */
11971 ++processing_template_decl
;
11973 for (new_parms
= &r
;
11974 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11975 new_parms
= &(TREE_CHAIN (*new_parms
)),
11976 parms
= TREE_CHAIN (parms
))
11978 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11981 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11982 - TMPL_ARGS_DEPTH (args
)),
11983 new_vec
, NULL_TREE
);
11986 --processing_template_decl
;
11991 /* Return the result of substituting ARGS into one template parameter
11992 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11993 parameter and which TREE_PURPOSE is the default argument of the
11994 template parameter. */
11997 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11999 tree default_value
, parm_decl
;
12001 if (args
== NULL_TREE
12003 || t
== error_mark_node
)
12006 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12008 default_value
= TREE_PURPOSE (t
);
12009 parm_decl
= TREE_VALUE (t
);
12011 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12012 if (TREE_CODE (parm_decl
) == PARM_DECL
12013 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12014 parm_decl
= error_mark_node
;
12015 default_value
= tsubst_template_arg (default_value
, args
,
12016 complain
, NULL_TREE
);
12018 return build_tree_list (default_value
, parm_decl
);
12021 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12022 type T. If T is not an aggregate or enumeration type, it is
12023 handled as if by tsubst. IN_DECL is as for tsubst. If
12024 ENTERING_SCOPE is nonzero, T is the context for a template which
12025 we are presently tsubst'ing. Return the substituted value. */
12028 tsubst_aggr_type (tree t
,
12030 tsubst_flags_t complain
,
12032 int entering_scope
)
12034 if (t
== NULL_TREE
)
12037 switch (TREE_CODE (t
))
12040 if (TYPE_PTRMEMFUNC_P (t
))
12041 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12043 /* Fall through. */
12044 case ENUMERAL_TYPE
:
12046 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12051 int saved_unevaluated_operand
;
12052 int saved_inhibit_evaluation_warnings
;
12054 /* In "sizeof(X<I>)" we need to evaluate "I". */
12055 saved_unevaluated_operand
= cp_unevaluated_operand
;
12056 cp_unevaluated_operand
= 0;
12057 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12058 c_inhibit_evaluation_warnings
= 0;
12060 /* First, determine the context for the type we are looking
12062 context
= TYPE_CONTEXT (t
);
12063 if (context
&& TYPE_P (context
))
12065 context
= tsubst_aggr_type (context
, args
, complain
,
12066 in_decl
, /*entering_scope=*/1);
12067 /* If context is a nested class inside a class template,
12068 it may still need to be instantiated (c++/33959). */
12069 context
= complete_type (context
);
12072 /* Then, figure out what arguments are appropriate for the
12073 type we are trying to find. For example, given:
12075 template <class T> struct S;
12076 template <class T, class U> void f(T, U) { S<U> su; }
12078 and supposing that we are instantiating f<int, double>,
12079 then our ARGS will be {int, double}, but, when looking up
12080 S we only want {double}. */
12081 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12082 complain
, in_decl
);
12083 if (argvec
== error_mark_node
)
12084 r
= error_mark_node
;
12087 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12088 entering_scope
, complain
);
12089 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12092 cp_unevaluated_operand
= saved_unevaluated_operand
;
12093 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12098 /* This is not a template type, so there's nothing to do. */
12102 return tsubst (t
, args
, complain
, in_decl
);
12106 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12108 /* Substitute into the default argument ARG (a default argument for
12109 FN), which has the indicated TYPE. */
12112 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12113 tsubst_flags_t complain
)
12115 tree saved_class_ptr
= NULL_TREE
;
12116 tree saved_class_ref
= NULL_TREE
;
12117 int errs
= errorcount
+ sorrycount
;
12119 /* This can happen in invalid code. */
12120 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12123 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12124 parm
= chain_index (parmnum
, parm
);
12125 tree parmtype
= TREE_TYPE (parm
);
12126 if (DECL_BY_REFERENCE (parm
))
12127 parmtype
= TREE_TYPE (parmtype
);
12128 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12131 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12134 /* This default argument came from a template. Instantiate the
12135 default argument here, not in tsubst. In the case of
12144 we must be careful to do name lookup in the scope of S<T>,
12145 rather than in the current class. */
12146 push_access_scope (fn
);
12147 /* The "this" pointer is not valid in a default argument. */
12150 saved_class_ptr
= current_class_ptr
;
12151 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12152 saved_class_ref
= current_class_ref
;
12153 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12156 start_lambda_scope (parm
);
12158 push_deferring_access_checks(dk_no_deferred
);
12159 /* The default argument expression may cause implicitly defined
12160 member functions to be synthesized, which will result in garbage
12161 collection. We must treat this situation as if we were within
12162 the body of function so as to avoid collecting live data on the
12165 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12166 complain
, NULL_TREE
,
12167 /*integral_constant_expression_p=*/false);
12169 pop_deferring_access_checks();
12171 finish_lambda_scope ();
12173 /* Restore the "this" pointer. */
12176 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12177 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12180 if (errorcount
+sorrycount
> errs
12181 && (complain
& tf_warning_or_error
))
12182 inform (input_location
,
12183 " when instantiating default argument for call to %qD", fn
);
12185 /* Make sure the default argument is reasonable. */
12186 arg
= check_default_argument (type
, arg
, complain
);
12188 pop_access_scope (fn
);
12190 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12193 defarg_inst
= tree_cache_map::create_ggc (37);
12194 defarg_inst
->put (parm
, arg
);
12200 /* Substitute into all the default arguments for FN. */
12203 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12208 tmpl_args
= DECL_TI_ARGS (fn
);
12210 /* If this function is not yet instantiated, we certainly don't need
12211 its default arguments. */
12212 if (uses_template_parms (tmpl_args
))
12214 /* Don't do this again for clones. */
12215 if (DECL_CLONED_FUNCTION_P (fn
))
12219 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12221 arg
= TREE_CHAIN (arg
), ++i
)
12222 if (TREE_PURPOSE (arg
))
12223 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12225 TREE_PURPOSE (arg
),
12229 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12232 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12233 tree lambda_fntype
)
12235 tree gen_tmpl
, argvec
;
12236 hashval_t hash
= 0;
12239 /* Nobody should be tsubst'ing into non-template functions. */
12240 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12242 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12244 /* If T is not dependent, just return it. */
12245 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12248 /* Calculate the most general template of which R is a
12250 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12252 /* We're substituting a lambda function under tsubst_lambda_expr but not
12253 directly from it; find the matching function we're already inside.
12254 But don't do this if T is a generic lambda with a single level of
12255 template parms, as in that case we're doing a normal instantiation. */
12256 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12257 && (!generic_lambda_fn_p (t
)
12258 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12259 return enclosing_instantiation_of (t
);
12261 /* Calculate the complete set of arguments used to
12263 argvec
= tsubst_template_args (DECL_TI_ARGS
12264 (DECL_TEMPLATE_RESULT
12265 (DECL_TI_TEMPLATE (t
))),
12266 args
, complain
, in_decl
);
12267 if (argvec
== error_mark_node
)
12268 return error_mark_node
;
12270 /* Check to see if we already have this specialization. */
12271 if (!lambda_fntype
)
12273 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12274 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12278 /* We can see more levels of arguments than parameters if
12279 there was a specialization of a member template, like
12282 template <class T> struct S { template <class U> void f(); }
12283 template <> template <class U> void S<int>::f(U);
12285 Here, we'll be substituting into the specialization,
12286 because that's where we can find the code we actually
12287 want to generate, but we'll have enough arguments for
12288 the most general template.
12290 We also deal with the peculiar case:
12292 template <class T> struct S {
12293 template <class U> friend void f();
12295 template <class U> void f() {}
12297 template void f<double>();
12299 Here, the ARGS for the instantiation of will be {int,
12300 double}. But, we only need as many ARGS as there are
12301 levels of template parameters in CODE_PATTERN. We are
12302 careful not to get fooled into reducing the ARGS in
12305 template <class T> struct S { template <class U> void f(U); }
12306 template <class T> template <> void S<T>::f(int) {}
12308 which we can spot because the pattern will be a
12309 specialization in this case. */
12310 int args_depth
= TMPL_ARGS_DEPTH (args
);
12312 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12314 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12315 args
= get_innermost_template_args (args
, parms_depth
);
12319 /* This special case arises when we have something like this:
12321 template <class T> struct S {
12322 friend void f<int>(int, double);
12325 Here, the DECL_TI_TEMPLATE for the friend declaration
12326 will be an IDENTIFIER_NODE. We are being called from
12327 tsubst_friend_function, and we want only to create a
12328 new decl (R) with appropriate types so that we can call
12329 determine_specialization. */
12330 gen_tmpl
= NULL_TREE
;
12331 argvec
= NULL_TREE
;
12334 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12336 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12337 bool member
= ctx
&& TYPE_P (ctx
);
12339 if (member
&& !closure
)
12340 ctx
= tsubst_aggr_type (ctx
, args
,
12341 complain
, t
, /*entering_scope=*/1);
12343 tree type
= (lambda_fntype
? lambda_fntype
12344 : tsubst (TREE_TYPE (t
), args
,
12345 complain
| tf_fndecl_type
, in_decl
));
12346 if (type
== error_mark_node
)
12347 return error_mark_node
;
12349 /* If we hit excessive deduction depth, the type is bogus even if
12350 it isn't error_mark_node, so don't build a decl. */
12351 if (excessive_deduction_depth
)
12352 return error_mark_node
;
12354 /* We do NOT check for matching decls pushed separately at this
12355 point, as they may not represent instantiations of this
12356 template, and in any case are considered separate under the
12358 tree r
= copy_decl (t
);
12359 DECL_USE_TEMPLATE (r
) = 0;
12360 TREE_TYPE (r
) = type
;
12361 /* Clear out the mangled name and RTL for the instantiation. */
12362 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12363 SET_DECL_RTL (r
, NULL
);
12364 /* Leave DECL_INITIAL set on deleted instantiations. */
12365 if (!DECL_DELETED_FN (r
))
12366 DECL_INITIAL (r
) = NULL_TREE
;
12367 DECL_CONTEXT (r
) = ctx
;
12369 /* OpenMP UDRs have the only argument a reference to the declared
12370 type. We want to diagnose if the declared type is a reference,
12371 which is invalid, but as references to references are usually
12372 quietly merged, diagnose it here. */
12373 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12376 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12377 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12378 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12379 error_at (DECL_SOURCE_LOCATION (t
),
12380 "reference type %qT in "
12381 "%<#pragma omp declare reduction%>", argtype
);
12382 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12383 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12387 if (member
&& DECL_CONV_FN_P (r
))
12388 /* Type-conversion operator. Reconstruct the name, in
12389 case it's the name of one of the template's parameters. */
12390 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12392 tree parms
= DECL_ARGUMENTS (t
);
12394 parms
= DECL_CHAIN (parms
);
12395 parms
= tsubst (parms
, args
, complain
, t
);
12396 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12397 DECL_CONTEXT (parm
) = r
;
12400 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12401 DECL_CHAIN (tparm
) = parms
;
12404 DECL_ARGUMENTS (r
) = parms
;
12405 DECL_RESULT (r
) = NULL_TREE
;
12407 TREE_STATIC (r
) = 0;
12408 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12409 DECL_EXTERNAL (r
) = 1;
12410 /* If this is an instantiation of a function with internal
12411 linkage, we already know what object file linkage will be
12412 assigned to the instantiation. */
12413 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12414 DECL_DEFER_OUTPUT (r
) = 0;
12415 DECL_CHAIN (r
) = NULL_TREE
;
12416 DECL_PENDING_INLINE_INFO (r
) = 0;
12417 DECL_PENDING_INLINE_P (r
) = 0;
12418 DECL_SAVED_TREE (r
) = NULL_TREE
;
12419 DECL_STRUCT_FUNCTION (r
) = NULL
;
12421 /* We'll re-clone as appropriate in instantiate_template. */
12422 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12424 /* If we aren't complaining now, return on error before we register
12425 the specialization so that we'll complain eventually. */
12426 if ((complain
& tf_error
) == 0
12427 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12428 && !grok_op_properties (r
, /*complain=*/false))
12429 return error_mark_node
;
12431 /* When instantiating a constrained member, substitute
12432 into the constraints to create a new constraint. */
12433 if (tree ci
= get_constraints (t
))
12436 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12437 set_constraints (r
, ci
);
12440 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12441 this in the special friend case mentioned above where
12442 GEN_TMPL is NULL. */
12443 if (gen_tmpl
&& !closure
)
12445 DECL_TEMPLATE_INFO (r
)
12446 = build_template_info (gen_tmpl
, argvec
);
12447 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12450 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12452 /* We instantiated this while substituting into
12453 the type earlier (template/friend54.C). */
12456 /* We're not supposed to instantiate default arguments
12457 until they are called, for a template. But, for a
12460 template <class T> void f ()
12461 { extern void g(int i = T()); }
12463 we should do the substitution when the template is
12464 instantiated. We handle the member function case in
12465 instantiate_class_template since the default arguments
12466 might refer to other members of the class. */
12468 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12469 && !uses_template_parms (argvec
))
12470 tsubst_default_arguments (r
, complain
);
12473 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12475 /* Copy the list of befriending classes. */
12476 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12478 friends
= &TREE_CHAIN (*friends
))
12480 *friends
= copy_node (*friends
);
12481 TREE_VALUE (*friends
)
12482 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12485 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12487 maybe_retrofit_in_chrg (r
);
12488 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12489 return error_mark_node
;
12490 /* If this is an instantiation of a member template, clone it.
12491 If it isn't, that'll be handled by
12492 clone_constructors_and_destructors. */
12493 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12494 clone_function_decl (r
, /*update_methods=*/false);
12496 else if ((complain
& tf_error
) != 0
12497 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12498 && !grok_op_properties (r
, /*complain=*/true))
12499 return error_mark_node
;
12501 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12502 SET_DECL_FRIEND_CONTEXT (r
,
12503 tsubst (DECL_FRIEND_CONTEXT (t
),
12504 args
, complain
, in_decl
));
12506 /* Possibly limit visibility based on template args. */
12507 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12508 if (DECL_VISIBILITY_SPECIFIED (t
))
12510 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12511 DECL_ATTRIBUTES (r
)
12512 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12514 determine_visibility (r
);
12515 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12516 && !processing_template_decl
)
12517 defaulted_late_check (r
);
12519 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12520 args
, complain
, in_decl
);
12524 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12527 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12528 tree lambda_fntype
)
12530 /* We can get here when processing a member function template,
12531 member class template, or template template parameter. */
12532 tree decl
= DECL_TEMPLATE_RESULT (t
);
12538 hashval_t hash
= 0;
12540 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12542 /* Template template parameter is treated here. */
12543 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12544 if (new_type
== error_mark_node
)
12545 r
= error_mark_node
;
12546 /* If we get a real template back, return it. This can happen in
12547 the context of most_specialized_partial_spec. */
12548 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12551 /* The new TEMPLATE_DECL was built in
12552 reduce_template_parm_level. */
12553 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12557 if (!lambda_fntype
)
12559 /* We might already have an instance of this template.
12560 The ARGS are for the surrounding class type, so the
12561 full args contain the tsubst'd args for the context,
12562 plus the innermost args from the template decl. */
12563 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12564 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12565 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12566 /* Because this is a template, the arguments will still be
12567 dependent, even after substitution. If
12568 PROCESSING_TEMPLATE_DECL is not set, the dependency
12569 predicates will short-circuit. */
12570 ++processing_template_decl
;
12571 full_args
= tsubst_template_args (tmpl_args
, args
,
12572 complain
, in_decl
);
12573 --processing_template_decl
;
12574 if (full_args
== error_mark_node
)
12575 return error_mark_node
;
12577 /* If this is a default template template argument,
12578 tsubst might not have changed anything. */
12579 if (full_args
== tmpl_args
)
12582 hash
= hash_tmpl_and_args (t
, full_args
);
12583 spec
= retrieve_specialization (t
, full_args
, hash
);
12584 if (spec
!= NULL_TREE
)
12588 /* Make a new template decl. It will be similar to the
12589 original, but will record the current template arguments.
12590 We also create a new function declaration, which is just
12591 like the old one, but points to this new template, rather
12592 than the old one. */
12594 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12595 DECL_CHAIN (r
) = NULL_TREE
;
12597 // Build new template info linking to the original template decl.
12598 if (!lambda_fntype
)
12600 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12601 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12604 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12606 /* The template parameters for this new template are all the
12607 template parameters for the old template, except the
12608 outermost level of parameters. */
12609 DECL_TEMPLATE_PARMS (r
)
12610 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12613 if (TREE_CODE (decl
) == TYPE_DECL
12614 && !TYPE_DECL_ALIAS_P (decl
))
12617 ++processing_template_decl
;
12618 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12619 --processing_template_decl
;
12620 if (new_type
== error_mark_node
)
12621 return error_mark_node
;
12623 TREE_TYPE (r
) = new_type
;
12624 /* For a partial specialization, we need to keep pointing to
12625 the primary template. */
12626 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12627 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12628 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12629 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12630 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12635 ++processing_template_decl
;
12636 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12637 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12639 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12640 --processing_template_decl
;
12641 if (new_decl
== error_mark_node
)
12642 return error_mark_node
;
12644 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12645 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12646 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12649 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12650 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12654 DECL_TI_TEMPLATE (new_decl
) = r
;
12655 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12659 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12660 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12662 if (PRIMARY_TEMPLATE_P (t
))
12663 DECL_PRIMARY_TEMPLATE (r
) = r
;
12665 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12667 /* Record this non-type partial instantiation. */
12668 register_specialization (r
, t
,
12669 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12675 /* True if FN is the op() for a lambda in an uninstantiated template. */
12678 lambda_fn_in_template_p (tree fn
)
12680 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12682 tree closure
= DECL_CONTEXT (fn
);
12683 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12686 /* We're instantiating a variable from template function TCTX. Return the
12687 corresponding current enclosing scope. This gets complicated because lambda
12688 functions in templates are regenerated rather than instantiated, but generic
12689 lambda functions are subsequently instantiated. */
12692 enclosing_instantiation_of (tree otctx
)
12695 tree fn
= current_function_decl
;
12696 int lambda_count
= 0;
12698 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12699 tctx
= decl_function_context (tctx
))
12701 for (; fn
; fn
= decl_function_context (fn
))
12704 int flambda_count
= 0;
12705 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12706 fn
= decl_function_context (fn
))
12708 if (DECL_TEMPLATE_INFO (fn
)
12709 ? most_general_template (fn
) != most_general_template (tctx
)
12712 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12713 || DECL_CONV_FN_P (ofn
));
12716 gcc_unreachable ();
12719 /* Substitute the ARGS into the T, which is a _DECL. Return the
12720 result of the substitution. Issue error and warning messages under
12721 control of COMPLAIN. */
12724 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12726 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12727 location_t saved_loc
;
12728 tree r
= NULL_TREE
;
12730 hashval_t hash
= 0;
12732 /* Set the filename and linenumber to improve error-reporting. */
12733 saved_loc
= input_location
;
12734 input_location
= DECL_SOURCE_LOCATION (t
);
12736 switch (TREE_CODE (t
))
12738 case TEMPLATE_DECL
:
12739 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12742 case FUNCTION_DECL
:
12743 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12748 tree type
= NULL_TREE
;
12750 tree expanded_types
= NULL_TREE
;
12751 tree prev_r
= NULL_TREE
;
12752 tree first_r
= NULL_TREE
;
12754 if (DECL_PACK_P (t
))
12756 /* If there is a local specialization that isn't a
12757 parameter pack, it means that we're doing a "simple"
12758 substitution from inside tsubst_pack_expansion. Just
12759 return the local specialization (which will be a single
12761 tree spec
= retrieve_local_specialization (t
);
12763 && TREE_CODE (spec
) == PARM_DECL
12764 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12767 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12768 the parameters in this function parameter pack. */
12769 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12770 complain
, in_decl
);
12771 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12773 len
= TREE_VEC_LENGTH (expanded_types
);
12775 /* Zero-length parameter packs are boring. Just substitute
12778 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12783 /* All we did was update the type. Make a note of that. */
12784 type
= expanded_types
;
12785 expanded_types
= NULL_TREE
;
12789 /* Loop through all of the parameters we'll build. When T is
12790 a function parameter pack, LEN is the number of expanded
12791 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12793 for (i
= 0; i
< len
; ++i
)
12797 if (DECL_TEMPLATE_PARM_P (t
))
12798 SET_DECL_TEMPLATE_PARM_P (r
);
12800 if (expanded_types
)
12801 /* We're on the Ith parameter of the function parameter
12804 /* Get the Ith type. */
12805 type
= TREE_VEC_ELT (expanded_types
, i
);
12807 /* Rename the parameter to include the index. */
12809 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12812 /* We're dealing with a normal parameter. */
12813 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12815 type
= type_decays_to (type
);
12816 TREE_TYPE (r
) = type
;
12817 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12819 if (DECL_INITIAL (r
))
12821 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12822 DECL_INITIAL (r
) = TREE_TYPE (r
);
12824 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12825 complain
, in_decl
);
12828 DECL_CONTEXT (r
) = NULL_TREE
;
12830 if (!DECL_TEMPLATE_PARM_P (r
))
12831 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12833 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12834 args
, complain
, in_decl
);
12836 /* Keep track of the first new parameter we
12837 generate. That's what will be returned to the
12842 /* Build a proper chain of parameters when substituting
12843 into a function parameter pack. */
12845 DECL_CHAIN (prev_r
) = r
;
12848 /* If cp_unevaluated_operand is set, we're just looking for a
12849 single dummy parameter, so don't keep going. */
12850 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12851 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12852 complain
, DECL_CHAIN (t
));
12854 /* FIRST_R contains the start of the chain we've built. */
12861 tree type
= NULL_TREE
;
12862 tree vec
= NULL_TREE
;
12863 tree expanded_types
= NULL_TREE
;
12866 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12868 /* This field is a lambda capture pack. Return a TREE_VEC of
12869 the expanded fields to instantiate_class_template_1 and
12870 store them in the specializations hash table as a
12871 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12872 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12873 complain
, in_decl
);
12874 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12876 len
= TREE_VEC_LENGTH (expanded_types
);
12877 vec
= make_tree_vec (len
);
12881 /* All we did was update the type. Make a note of that. */
12882 type
= expanded_types
;
12883 expanded_types
= NULL_TREE
;
12887 for (int i
= 0; i
< len
; ++i
)
12890 if (expanded_types
)
12892 type
= TREE_VEC_ELT (expanded_types
, i
);
12894 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12897 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12899 if (type
== error_mark_node
)
12900 RETURN (error_mark_node
);
12901 TREE_TYPE (r
) = type
;
12902 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12904 if (DECL_C_BIT_FIELD (r
))
12905 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12907 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12908 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12910 /*integral_constant_expression_p=*/true);
12911 if (DECL_INITIAL (t
))
12913 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12914 NSDMI in perform_member_init. Still set DECL_INITIAL
12915 so that we know there is one. */
12916 DECL_INITIAL (r
) = void_node
;
12917 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12918 retrofit_lang_decl (r
);
12919 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12921 /* We don't have to set DECL_CONTEXT here; it is set by
12922 finish_member_declaration. */
12923 DECL_CHAIN (r
) = NULL_TREE
;
12925 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12926 args
, complain
, in_decl
);
12929 TREE_VEC_ELT (vec
, i
) = r
;
12935 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12936 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12937 register_specialization (pack
, t
, args
, false, 0);
12943 /* We reach here only for member using decls. We also need to check
12944 uses_template_parms because DECL_DEPENDENT_P is not set for a
12945 using-declaration that designates a member of the current
12946 instantiation (c++/53549). */
12947 if (DECL_DEPENDENT_P (t
)
12948 || uses_template_parms (USING_DECL_SCOPE (t
)))
12950 tree scope
= USING_DECL_SCOPE (t
);
12951 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12952 if (PACK_EXPANSION_P (scope
))
12954 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12955 int len
= TREE_VEC_LENGTH (vec
);
12956 r
= make_tree_vec (len
);
12957 for (int i
= 0; i
< len
; ++i
)
12959 tree escope
= TREE_VEC_ELT (vec
, i
);
12960 tree elt
= do_class_using_decl (escope
, name
);
12963 r
= error_mark_node
;
12968 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12969 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12971 TREE_VEC_ELT (r
, i
) = elt
;
12976 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12977 complain
, in_decl
);
12978 r
= do_class_using_decl (inst_scope
, name
);
12980 r
= error_mark_node
;
12983 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12984 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12991 DECL_CHAIN (r
) = NULL_TREE
;
12998 tree argvec
= NULL_TREE
;
12999 tree gen_tmpl
= NULL_TREE
;
13001 tree tmpl
= NULL_TREE
;
13003 tree type
= NULL_TREE
;
13006 if (TREE_TYPE (t
) == error_mark_node
)
13007 RETURN (error_mark_node
);
13009 if (TREE_CODE (t
) == TYPE_DECL
13010 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13012 /* If this is the canonical decl, we don't have to
13013 mess with instantiations, and often we can't (for
13014 typename, template type parms and such). Note that
13015 TYPE_NAME is not correct for the above test if
13016 we've copied the type for a typedef. */
13017 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13018 if (type
== error_mark_node
)
13019 RETURN (error_mark_node
);
13020 r
= TYPE_NAME (type
);
13024 /* Check to see if we already have the specialization we
13027 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13029 /* T is a static data member or namespace-scope entity.
13030 We have to substitute into namespace-scope variables
13031 (not just variable templates) because of cases like:
13033 template <class T> void f() { extern T t; }
13035 where the entity referenced is not known until
13036 instantiation time. */
13038 ctx
= DECL_CONTEXT (t
);
13039 if (DECL_CLASS_SCOPE_P (t
))
13041 ctx
= tsubst_aggr_type (ctx
, args
,
13043 in_decl
, /*entering_scope=*/1);
13044 /* If CTX is unchanged, then T is in fact the
13045 specialization we want. That situation occurs when
13046 referencing a static data member within in its own
13047 class. We can use pointer equality, rather than
13048 same_type_p, because DECL_CONTEXT is always
13050 if (ctx
== DECL_CONTEXT (t
)
13051 /* ... unless T is a member template; in which
13052 case our caller can be willing to create a
13053 specialization of that template represented
13055 && !(DECL_TI_TEMPLATE (t
)
13056 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13062 tmpl
= DECL_TI_TEMPLATE (t
);
13063 gen_tmpl
= most_general_template (tmpl
);
13064 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13065 if (argvec
!= error_mark_node
)
13066 argvec
= (coerce_innermost_template_parms
13067 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13068 argvec
, t
, complain
,
13069 /*all*/true, /*defarg*/true));
13070 if (argvec
== error_mark_node
)
13071 RETURN (error_mark_node
);
13072 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13073 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13078 /* A local variable. */
13080 /* Subsequent calls to pushdecl will fill this in. */
13082 /* Unless this is a reference to a static variable from an
13083 enclosing function, in which case we need to fill it in now. */
13084 if (TREE_STATIC (t
))
13086 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13087 if (fn
!= current_function_decl
)
13090 spec
= retrieve_local_specialization (t
);
13092 /* If we already have the specialization we need, there is
13093 nothing more to do. */
13100 /* Create a new node for the specialization we need. */
13102 if (type
== NULL_TREE
)
13104 if (is_typedef_decl (t
))
13105 type
= DECL_ORIGINAL_TYPE (t
);
13107 type
= TREE_TYPE (t
);
13109 && VAR_HAD_UNKNOWN_BOUND (t
)
13110 && type
!= error_mark_node
)
13111 type
= strip_array_domain (type
);
13112 tree sub_args
= args
;
13113 if (tree auto_node
= type_uses_auto (type
))
13115 /* Mask off any template args past the variable's context so we
13116 don't replace the auto with an unrelated argument. */
13117 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13118 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13120 /* This should never happen with the new lambda instantiation
13121 model, but keep the handling just in case. */
13122 gcc_assert (!CHECKING_P
),
13123 sub_args
= strip_innermost_template_args (args
, extra
);
13125 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13129 /* Even if the original location is out of scope, the
13130 newly substituted one is not. */
13131 DECL_DEAD_FOR_LOCAL (r
) = 0;
13132 DECL_INITIALIZED_P (r
) = 0;
13133 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13134 if (type
== error_mark_node
)
13135 RETURN (error_mark_node
);
13136 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13138 /* It may seem that this case cannot occur, since:
13143 declares a function, not a variable. However:
13146 template <typename T> void g() { T t; }
13147 template void g<f>();
13149 is an attempt to declare a variable with function
13151 error ("variable %qD has function type",
13152 /* R is not yet sufficiently initialized, so we
13153 just use its name. */
13155 RETURN (error_mark_node
);
13157 type
= complete_type (type
);
13158 /* Wait until cp_finish_decl to set this again, to handle
13159 circular dependency (template/instantiate6.C). */
13160 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13161 type
= check_var_type (DECL_NAME (r
), type
);
13163 if (DECL_HAS_VALUE_EXPR_P (t
))
13165 tree ve
= DECL_VALUE_EXPR (t
);
13166 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13167 /*constant_expression_p=*/false);
13168 if (REFERENCE_REF_P (ve
))
13170 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13171 ve
= TREE_OPERAND (ve
, 0);
13173 SET_DECL_VALUE_EXPR (r
, ve
);
13175 if (CP_DECL_THREAD_LOCAL_P (r
)
13176 && !processing_template_decl
)
13177 set_decl_tls_model (r
, decl_default_tls_model (r
));
13179 else if (DECL_SELF_REFERENCE_P (t
))
13180 SET_DECL_SELF_REFERENCE_P (r
);
13181 TREE_TYPE (r
) = type
;
13182 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13183 DECL_CONTEXT (r
) = ctx
;
13184 /* Clear out the mangled name and RTL for the instantiation. */
13185 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13186 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13187 SET_DECL_RTL (r
, NULL
);
13188 /* The initializer must not be expanded until it is required;
13189 see [temp.inst]. */
13190 DECL_INITIAL (r
) = NULL_TREE
;
13191 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13194 if (DECL_LANG_SPECIFIC (r
))
13195 SET_DECL_DEPENDENT_INIT_P (r
, false);
13197 SET_DECL_MODE (r
, VOIDmode
);
13199 /* Possibly limit visibility based on template args. */
13200 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13201 if (DECL_VISIBILITY_SPECIFIED (t
))
13203 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13204 DECL_ATTRIBUTES (r
)
13205 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13207 determine_visibility (r
);
13212 /* A static data member declaration is always marked
13213 external when it is declared in-class, even if an
13214 initializer is present. We mimic the non-template
13215 processing here. */
13216 DECL_EXTERNAL (r
) = 1;
13217 if (DECL_NAMESPACE_SCOPE_P (t
))
13218 DECL_NOT_REALLY_EXTERN (r
) = 1;
13220 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13221 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13222 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13226 if (DECL_LANG_SPECIFIC (r
))
13227 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13228 if (!cp_unevaluated_operand
)
13229 register_local_specialization (r
, t
);
13232 DECL_CHAIN (r
) = NULL_TREE
;
13234 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13236 args
, complain
, in_decl
);
13238 /* Preserve a typedef that names a type. */
13239 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13241 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13242 set_underlying_type (r
);
13243 if (TYPE_DECL_ALIAS_P (r
))
13244 /* An alias template specialization can be dependent
13245 even if its underlying type is not. */
13246 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13249 layout_decl (r
, 0);
13254 gcc_unreachable ();
13259 /* Restore the file and line information. */
13260 input_location
= saved_loc
;
13265 /* Substitute into the ARG_TYPES of a function type.
13266 If END is a TREE_CHAIN, leave it and any following types
13270 tsubst_arg_types (tree arg_types
,
13273 tsubst_flags_t complain
,
13276 tree remaining_arg_types
;
13277 tree type
= NULL_TREE
;
13279 tree expanded_args
= NULL_TREE
;
13282 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13285 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13286 args
, end
, complain
, in_decl
);
13287 if (remaining_arg_types
== error_mark_node
)
13288 return error_mark_node
;
13290 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13292 /* For a pack expansion, perform substitution on the
13293 entire expression. Later on, we'll handle the arguments
13295 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13296 args
, complain
, in_decl
);
13298 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13299 /* So that we'll spin through the parameters, one by one. */
13300 i
= TREE_VEC_LENGTH (expanded_args
);
13303 /* We only partially substituted into the parameter
13304 pack. Our type is TYPE_PACK_EXPANSION. */
13305 type
= expanded_args
;
13306 expanded_args
= NULL_TREE
;
13314 type
= TREE_VEC_ELT (expanded_args
, i
);
13316 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13318 if (type
== error_mark_node
)
13319 return error_mark_node
;
13320 if (VOID_TYPE_P (type
))
13322 if (complain
& tf_error
)
13324 error ("invalid parameter type %qT", type
);
13326 error ("in declaration %q+D", in_decl
);
13328 return error_mark_node
;
13331 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13332 return error_mark_node
;
13334 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13335 top-level qualifiers as required. */
13336 type
= cv_unqualified (type_decays_to (type
));
13338 /* We do not substitute into default arguments here. The standard
13339 mandates that they be instantiated only when needed, which is
13340 done in build_over_call. */
13341 default_arg
= TREE_PURPOSE (arg_types
);
13343 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13344 since the new op() won't have any associated template arguments for us
13345 to refer to later. */
13346 if (lambda_fn_in_template_p (in_decl
))
13347 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13348 false/*fn*/, false/*constexpr*/);
13350 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13352 /* We've instantiated a template before its default arguments
13353 have been parsed. This can happen for a nested template
13354 class, and is not an error unless we require the default
13355 argument in a call of this function. */
13356 remaining_arg_types
=
13357 tree_cons (default_arg
, type
, remaining_arg_types
);
13358 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13361 remaining_arg_types
=
13362 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13365 return remaining_arg_types
;
13368 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13369 *not* handle the exception-specification for FNTYPE, because the
13370 initial substitution of explicitly provided template parameters
13371 during argument deduction forbids substitution into the
13372 exception-specification:
13376 All references in the function type of the function template to the
13377 corresponding template parameters are replaced by the specified tem-
13378 plate argument values. If a substitution in a template parameter or
13379 in the function type of the function template results in an invalid
13380 type, type deduction fails. [Note: The equivalent substitution in
13381 exception specifications is done only when the function is instanti-
13382 ated, at which point a program is ill-formed if the substitution
13383 results in an invalid type.] */
13386 tsubst_function_type (tree t
,
13388 tsubst_flags_t complain
,
13392 tree arg_types
= NULL_TREE
;
13395 /* The TYPE_CONTEXT is not used for function/method types. */
13396 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13398 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13400 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13402 if (late_return_type_p
)
13404 /* Substitute the argument types. */
13405 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13406 complain
, in_decl
);
13407 if (arg_types
== error_mark_node
)
13408 return error_mark_node
;
13410 tree save_ccp
= current_class_ptr
;
13411 tree save_ccr
= current_class_ref
;
13412 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13413 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13414 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13417 /* DR 1207: 'this' is in scope in the trailing return type. */
13418 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13421 /* Substitute the return type. */
13422 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13426 current_class_ptr
= save_ccp
;
13427 current_class_ref
= save_ccr
;
13431 /* Substitute the return type. */
13432 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13434 if (return_type
== error_mark_node
)
13435 return error_mark_node
;
13436 /* DR 486 clarifies that creation of a function type with an
13437 invalid return type is a deduction failure. */
13438 if (TREE_CODE (return_type
) == ARRAY_TYPE
13439 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13441 if (complain
& tf_error
)
13443 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13444 error ("function returning an array");
13446 error ("function returning a function");
13448 return error_mark_node
;
13451 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13452 return error_mark_node
;
13454 if (!late_return_type_p
)
13456 /* Substitute the argument types. */
13457 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13458 complain
, in_decl
);
13459 if (arg_types
== error_mark_node
)
13460 return error_mark_node
;
13463 /* Construct a new type node and return it. */
13464 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13466 fntype
= build_function_type (return_type
, arg_types
);
13467 fntype
= apply_memfn_quals (fntype
,
13468 type_memfn_quals (t
),
13469 type_memfn_rqual (t
));
13473 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13474 /* Don't pick up extra function qualifiers from the basetype. */
13475 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13476 if (! MAYBE_CLASS_TYPE_P (r
))
13480 Type deduction may fail for any of the following
13483 -- Attempting to create "pointer to member of T" when T
13484 is not a class type. */
13485 if (complain
& tf_error
)
13486 error ("creating pointer to member function of non-class type %qT",
13488 return error_mark_node
;
13491 fntype
= build_method_type_directly (r
, return_type
,
13492 TREE_CHAIN (arg_types
));
13493 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13495 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13497 if (late_return_type_p
)
13498 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13503 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13504 ARGS into that specification, and return the substituted
13505 specification. If there is no specification, return NULL_TREE. */
13508 tsubst_exception_specification (tree fntype
,
13510 tsubst_flags_t complain
,
13517 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13518 new_specs
= NULL_TREE
;
13519 if (specs
&& TREE_PURPOSE (specs
))
13521 /* A noexcept-specifier. */
13522 tree expr
= TREE_PURPOSE (specs
);
13523 if (TREE_CODE (expr
) == INTEGER_CST
)
13527 /* Defer instantiation of noexcept-specifiers to avoid
13528 excessive instantiations (c++/49107). */
13529 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13530 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13532 /* We already partially instantiated this member template,
13533 so combine the new args with the old. */
13534 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13535 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13536 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13537 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13541 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13542 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13546 new_specs
= tsubst_copy_and_build
13547 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13548 /*integral_constant_expression_p=*/true);
13549 new_specs
= build_noexcept_spec (new_specs
, complain
);
13553 if (! TREE_VALUE (specs
))
13560 tree expanded_specs
= NULL_TREE
;
13562 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13564 /* Expand the pack expansion type. */
13565 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13569 if (expanded_specs
== error_mark_node
)
13570 return error_mark_node
;
13571 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13572 len
= TREE_VEC_LENGTH (expanded_specs
);
13575 /* We're substituting into a member template, so
13576 we got a TYPE_PACK_EXPANSION back. Add that
13577 expansion and move on. */
13578 gcc_assert (TREE_CODE (expanded_specs
)
13579 == TYPE_PACK_EXPANSION
);
13580 new_specs
= add_exception_specifier (new_specs
,
13583 specs
= TREE_CHAIN (specs
);
13588 for (i
= 0; i
< len
; ++i
)
13590 if (expanded_specs
)
13591 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13593 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13594 if (spec
== error_mark_node
)
13596 new_specs
= add_exception_specifier (new_specs
, spec
,
13600 specs
= TREE_CHAIN (specs
);
13606 /* Take the tree structure T and replace template parameters used
13607 therein with the argument vector ARGS. IN_DECL is an associated
13608 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13609 Issue error and warning messages under control of COMPLAIN. Note
13610 that we must be relatively non-tolerant of extensions here, in
13611 order to preserve conformance; if we allow substitutions that
13612 should not be allowed, we may allow argument deductions that should
13613 not succeed, and therefore report ambiguous overload situations
13614 where there are none. In theory, we could allow the substitution,
13615 but indicate that it should have failed, and allow our caller to
13616 make sure that the right thing happens, but we don't try to do this
13619 This function is used for dealing with types, decls and the like;
13620 for expressions, use tsubst_expr or tsubst_copy. */
13623 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13625 enum tree_code code
;
13626 tree type
, r
= NULL_TREE
;
13628 if (t
== NULL_TREE
|| t
== error_mark_node
13629 || t
== integer_type_node
13630 || t
== void_type_node
13631 || t
== char_type_node
13632 || t
== unknown_type_node
13633 || TREE_CODE (t
) == NAMESPACE_DECL
13634 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13638 return tsubst_decl (t
, args
, complain
);
13640 if (args
== NULL_TREE
)
13643 code
= TREE_CODE (t
);
13645 if (code
== IDENTIFIER_NODE
)
13646 type
= IDENTIFIER_TYPE_VALUE (t
);
13648 type
= TREE_TYPE (t
);
13650 gcc_assert (type
!= unknown_type_node
);
13652 /* Reuse typedefs. We need to do this to handle dependent attributes,
13653 such as attribute aligned. */
13655 && typedef_variant_p (t
))
13657 tree decl
= TYPE_NAME (t
);
13659 if (alias_template_specialization_p (t
))
13661 /* DECL represents an alias template and we want to
13663 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13664 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13665 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13667 else if (DECL_CLASS_SCOPE_P (decl
)
13668 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13669 && uses_template_parms (DECL_CONTEXT (decl
)))
13671 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13672 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13673 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13675 else if (DECL_FUNCTION_SCOPE_P (decl
)
13676 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13677 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13678 r
= retrieve_local_specialization (decl
);
13680 /* The typedef is from a non-template context. */
13686 r
= cp_build_qualified_type_real
13687 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13688 complain
| tf_ignore_bad_quals
);
13693 /* We don't have an instantiation yet, so drop the typedef. */
13694 int quals
= cp_type_quals (t
);
13695 t
= DECL_ORIGINAL_TYPE (decl
);
13696 t
= cp_build_qualified_type_real (t
, quals
,
13697 complain
| tf_ignore_bad_quals
);
13701 bool fndecl_type
= (complain
& tf_fndecl_type
);
13702 complain
&= ~tf_fndecl_type
;
13705 && code
!= TYPENAME_TYPE
13706 && code
!= TEMPLATE_TYPE_PARM
13707 && code
!= TEMPLATE_PARM_INDEX
13708 && code
!= IDENTIFIER_NODE
13709 && code
!= FUNCTION_TYPE
13710 && code
!= METHOD_TYPE
)
13711 type
= tsubst (type
, args
, complain
, in_decl
);
13712 if (type
== error_mark_node
)
13713 return error_mark_node
;
13719 case ENUMERAL_TYPE
:
13720 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13721 /*entering_scope=*/0);
13724 case IDENTIFIER_NODE
:
13735 if (t
== integer_type_node
)
13738 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13739 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13743 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13745 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13746 /*integral_constant_expression_p=*/false);
13748 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13750 if (TREE_CODE (max
) == NOP_EXPR
13751 && TREE_SIDE_EFFECTS (omax
)
13752 && !TREE_TYPE (max
))
13753 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13755 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13756 with TREE_SIDE_EFFECTS that indicates this is not an integral
13757 constant expression. */
13758 if (processing_template_decl
13759 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13761 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13762 TREE_SIDE_EFFECTS (max
) = 1;
13765 return compute_array_index_type (NULL_TREE
, max
, complain
);
13768 case TEMPLATE_TYPE_PARM
:
13769 case TEMPLATE_TEMPLATE_PARM
:
13770 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13771 case TEMPLATE_PARM_INDEX
:
13776 tree arg
= NULL_TREE
;
13778 /* Early in template argument deduction substitution, we don't
13779 want to reduce the level of 'auto', or it will be confused
13780 with a normal template parm in subsequent deduction. */
13781 if (is_auto (t
) && (complain
& tf_partial
))
13786 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13787 template_parm_level_and_index (t
, &level
, &idx
);
13789 levels
= TMPL_ARGS_DEPTH (args
);
13790 if (level
<= levels
13791 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13793 arg
= TMPL_ARG (args
, level
, idx
);
13795 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13797 /* See through ARGUMENT_PACK_SELECT arguments. */
13798 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13799 /* If the selected argument is an expansion E, that most
13800 likely means we were called from
13801 gen_elem_of_pack_expansion_instantiation during the
13802 substituting of pack an argument pack (which Ith
13803 element is a pack expansion, where I is
13804 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13805 In this case, the Ith element resulting from this
13806 substituting is going to be a pack expansion, which
13807 pattern is the pattern of E. Let's return the
13809 gen_elem_of_pack_expansion_instantiation will
13810 build the resulting pack expansion from it. */
13811 if (PACK_EXPANSION_P (arg
))
13813 /* Make sure we aren't throwing away arg info. */
13814 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13815 arg
= PACK_EXPANSION_PATTERN (arg
);
13820 if (arg
== error_mark_node
)
13821 return error_mark_node
;
13822 else if (arg
!= NULL_TREE
)
13824 if (ARGUMENT_PACK_P (arg
))
13825 /* If ARG is an argument pack, we don't actually want to
13826 perform a substitution here, because substitutions
13827 for argument packs are only done
13828 element-by-element. We can get to this point when
13829 substituting the type of a non-type template
13830 parameter pack, when that type actually contains
13831 template parameter packs from an outer template, e.g.,
13833 template<typename... Types> struct A {
13834 template<Types... Values> struct B { };
13838 if (code
== TEMPLATE_TYPE_PARM
)
13841 gcc_assert (TYPE_P (arg
));
13843 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13845 return cp_build_qualified_type_real
13846 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13848 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13850 /* We are processing a type constructed from a
13851 template template parameter. */
13852 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13853 args
, complain
, in_decl
);
13854 if (argvec
== error_mark_node
)
13855 return error_mark_node
;
13857 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13858 || TREE_CODE (arg
) == TEMPLATE_DECL
13859 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13861 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13862 /* Consider this code:
13864 template <template <class> class Template>
13866 template <class Arg> using Bind = Template<Arg>;
13869 template <template <class> class Template, class Arg>
13870 using Instantiate = Template<Arg>; //#0
13872 template <template <class> class Template,
13875 Instantiate<Internal<Template>::template Bind,
13878 When #1 is parsed, the
13879 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13880 parameter `Template' in #0 matches the
13881 UNBOUND_CLASS_TEMPLATE representing the argument
13882 `Internal<Template>::template Bind'; We then want
13883 to assemble the type `Bind<Argument>' that can't
13884 be fully created right now, because
13885 `Internal<Template>' not being complete, the Bind
13886 template cannot be looked up in that context. So
13887 we need to "store" `Bind<Argument>' for later
13888 when the context of Bind becomes complete. Let's
13889 store that in a TYPENAME_TYPE. */
13890 return make_typename_type (TYPE_CONTEXT (arg
),
13891 build_nt (TEMPLATE_ID_EXPR
,
13892 TYPE_IDENTIFIER (arg
),
13897 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13898 are resolving nested-types in the signature of a
13899 member function templates. Otherwise ARG is a
13900 TEMPLATE_DECL and is the real template to be
13902 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13903 arg
= TYPE_NAME (arg
);
13905 r
= lookup_template_class (arg
,
13907 DECL_CONTEXT (arg
),
13908 /*entering_scope=*/0,
13910 return cp_build_qualified_type_real
13911 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13913 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13916 /* TEMPLATE_PARM_INDEX. */
13917 return convert_from_reference (unshare_expr (arg
));
13921 /* This can happen during the attempted tsubst'ing in
13922 unify. This means that we don't yet have any information
13923 about the template parameter in question. */
13926 /* If we get here, we must have been looking at a parm for a
13927 more deeply nested template. Make a new version of this
13928 template parameter, but with a lower level. */
13931 case TEMPLATE_TYPE_PARM
:
13932 case TEMPLATE_TEMPLATE_PARM
:
13933 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13934 if (cp_type_quals (t
))
13936 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13937 r
= cp_build_qualified_type_real
13938 (r
, cp_type_quals (t
),
13939 complain
| (code
== TEMPLATE_TYPE_PARM
13940 ? tf_ignore_bad_quals
: 0));
13942 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13943 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13944 && (r
= (TEMPLATE_PARM_DESCENDANTS
13945 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13946 && (r
= TREE_TYPE (r
))
13947 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13948 /* Break infinite recursion when substituting the constraints
13949 of a constrained placeholder. */;
13953 TEMPLATE_TYPE_PARM_INDEX (r
)
13954 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13955 r
, levels
, args
, complain
);
13956 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13957 TYPE_MAIN_VARIANT (r
) = r
;
13958 TYPE_POINTER_TO (r
) = NULL_TREE
;
13959 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13961 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13963 /* Propagate constraints on placeholders. */
13964 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13965 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13966 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13967 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13969 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13970 pl
= tsubst (pl
, args
, complain
, in_decl
);
13971 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13975 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13976 /* We have reduced the level of the template
13977 template parameter, but not the levels of its
13978 template parameters, so canonical_type_parameter
13979 will not be able to find the canonical template
13980 template parameter for this level. Thus, we
13981 require structural equality checking to compare
13982 TEMPLATE_TEMPLATE_PARMs. */
13983 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13984 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13985 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13987 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13989 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13991 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13992 /* We might need to substitute into the types of non-type
13993 template parameters. */
13994 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13995 complain
, in_decl
);
13996 if (tmpl
== error_mark_node
)
13997 return error_mark_node
;
13998 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13999 complain
, in_decl
);
14000 if (argvec
== error_mark_node
)
14001 return error_mark_node
;
14003 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14004 = build_template_info (tmpl
, argvec
);
14009 case TEMPLATE_PARM_INDEX
:
14010 /* OK, now substitute the type of the non-type parameter. We
14011 couldn't do it earlier because it might be an auto parameter,
14012 and we wouldn't need to if we had an argument. */
14013 type
= tsubst (type
, args
, complain
, in_decl
);
14014 if (type
== error_mark_node
)
14015 return error_mark_node
;
14016 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14020 gcc_unreachable ();
14028 tree purpose
, value
, chain
;
14030 if (t
== void_list_node
)
14033 purpose
= TREE_PURPOSE (t
);
14036 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14037 if (purpose
== error_mark_node
)
14038 return error_mark_node
;
14040 value
= TREE_VALUE (t
);
14043 value
= tsubst (value
, args
, complain
, in_decl
);
14044 if (value
== error_mark_node
)
14045 return error_mark_node
;
14047 chain
= TREE_CHAIN (t
);
14048 if (chain
&& chain
!= void_type_node
)
14050 chain
= tsubst (chain
, args
, complain
, in_decl
);
14051 if (chain
== error_mark_node
)
14052 return error_mark_node
;
14054 if (purpose
== TREE_PURPOSE (t
)
14055 && value
== TREE_VALUE (t
)
14056 && chain
== TREE_CHAIN (t
))
14058 return hash_tree_cons (purpose
, value
, chain
);
14062 /* We should never be tsubsting a binfo. */
14063 gcc_unreachable ();
14066 /* A vector of template arguments. */
14067 gcc_assert (!type
);
14068 return tsubst_template_args (t
, args
, complain
, in_decl
);
14071 case REFERENCE_TYPE
:
14073 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14078 Type deduction may fail for any of the following
14081 -- Attempting to create a pointer to reference type.
14082 -- Attempting to create a reference to a reference type or
14083 a reference to void.
14085 Core issue 106 says that creating a reference to a reference
14086 during instantiation is no longer a cause for failure. We
14087 only enforce this check in strict C++98 mode. */
14088 if ((TREE_CODE (type
) == REFERENCE_TYPE
14089 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14090 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14092 static location_t last_loc
;
14094 /* We keep track of the last time we issued this error
14095 message to avoid spewing a ton of messages during a
14096 single bad template instantiation. */
14097 if (complain
& tf_error
14098 && last_loc
!= input_location
)
14100 if (VOID_TYPE_P (type
))
14101 error ("forming reference to void");
14102 else if (code
== POINTER_TYPE
)
14103 error ("forming pointer to reference type %qT", type
);
14105 error ("forming reference to reference type %qT", type
);
14106 last_loc
= input_location
;
14109 return error_mark_node
;
14111 else if (TREE_CODE (type
) == FUNCTION_TYPE
14112 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14113 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14115 if (complain
& tf_error
)
14117 if (code
== POINTER_TYPE
)
14118 error ("forming pointer to qualified function type %qT",
14121 error ("forming reference to qualified function type %qT",
14124 return error_mark_node
;
14126 else if (code
== POINTER_TYPE
)
14128 r
= build_pointer_type (type
);
14129 if (TREE_CODE (type
) == METHOD_TYPE
)
14130 r
= build_ptrmemfunc_type (r
);
14132 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14133 /* In C++0x, during template argument substitution, when there is an
14134 attempt to create a reference to a reference type, reference
14135 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14137 "If a template-argument for a template-parameter T names a type
14138 that is a reference to a type A, an attempt to create the type
14139 'lvalue reference to cv T' creates the type 'lvalue reference to
14140 A,' while an attempt to create the type type rvalue reference to
14141 cv T' creates the type T"
14143 r
= cp_build_reference_type
14145 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14147 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14148 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14150 if (r
!= error_mark_node
)
14151 /* Will this ever be needed for TYPE_..._TO values? */
14158 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14159 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14163 Type deduction may fail for any of the following
14166 -- Attempting to create "pointer to member of T" when T
14167 is not a class type. */
14168 if (complain
& tf_error
)
14169 error ("creating pointer to member of non-class type %qT", r
);
14170 return error_mark_node
;
14172 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14174 if (complain
& tf_error
)
14175 error ("creating pointer to member reference type %qT", type
);
14176 return error_mark_node
;
14178 if (VOID_TYPE_P (type
))
14180 if (complain
& tf_error
)
14181 error ("creating pointer to member of type void");
14182 return error_mark_node
;
14184 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14185 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14187 /* The type of the implicit object parameter gets its
14188 cv-qualifiers from the FUNCTION_TYPE. */
14191 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14192 type_memfn_rqual (type
));
14193 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14194 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14198 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14202 case FUNCTION_TYPE
:
14207 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14208 if (fntype
== error_mark_node
)
14209 return error_mark_node
;
14211 /* Substitute the exception specification. */
14212 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14213 /*defer_ok*/fndecl_type
);
14214 if (specs
== error_mark_node
)
14215 return error_mark_node
;
14217 fntype
= build_exception_variant (fntype
, specs
);
14222 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14223 if (domain
== error_mark_node
)
14224 return error_mark_node
;
14226 /* As an optimization, we avoid regenerating the array type if
14227 it will obviously be the same as T. */
14228 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14231 /* These checks should match the ones in create_array_type_for_decl.
14235 The deduction may fail for any of the following reasons:
14237 -- Attempting to create an array with an element type that
14238 is void, a function type, or a reference type, or [DR337]
14239 an abstract class type. */
14240 if (VOID_TYPE_P (type
)
14241 || TREE_CODE (type
) == FUNCTION_TYPE
14242 || (TREE_CODE (type
) == ARRAY_TYPE
14243 && TYPE_DOMAIN (type
) == NULL_TREE
)
14244 || TREE_CODE (type
) == REFERENCE_TYPE
)
14246 if (complain
& tf_error
)
14247 error ("creating array of %qT", type
);
14248 return error_mark_node
;
14251 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14252 return error_mark_node
;
14254 r
= build_cplus_array_type (type
, domain
);
14256 if (TYPE_USER_ALIGN (t
))
14258 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14259 TYPE_USER_ALIGN (r
) = 1;
14265 case TYPENAME_TYPE
:
14267 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14268 in_decl
, /*entering_scope=*/1);
14269 if (ctx
== error_mark_node
)
14270 return error_mark_node
;
14272 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14273 complain
, in_decl
);
14274 if (f
== error_mark_node
)
14275 return error_mark_node
;
14277 if (!MAYBE_CLASS_TYPE_P (ctx
))
14279 if (complain
& tf_error
)
14280 error ("%qT is not a class, struct, or union type", ctx
);
14281 return error_mark_node
;
14283 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14285 /* Normally, make_typename_type does not require that the CTX
14286 have complete type in order to allow things like:
14288 template <class T> struct S { typename S<T>::X Y; };
14290 But, such constructs have already been resolved by this
14291 point, so here CTX really should have complete type, unless
14292 it's a partial instantiation. */
14293 ctx
= complete_type (ctx
);
14294 if (!COMPLETE_TYPE_P (ctx
))
14296 if (complain
& tf_error
)
14297 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14298 return error_mark_node
;
14302 f
= make_typename_type (ctx
, f
, typename_type
,
14303 complain
| tf_keep_type_decl
);
14304 if (f
== error_mark_node
)
14306 if (TREE_CODE (f
) == TYPE_DECL
)
14308 complain
|= tf_ignore_bad_quals
;
14312 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14314 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14316 if (complain
& tf_error
)
14317 error ("%qT resolves to %qT, which is not an enumeration type",
14320 return error_mark_node
;
14322 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14324 if (complain
& tf_error
)
14325 error ("%qT resolves to %qT, which is is not a class type",
14328 return error_mark_node
;
14332 return cp_build_qualified_type_real
14333 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14336 case UNBOUND_CLASS_TEMPLATE
:
14338 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14339 in_decl
, /*entering_scope=*/1);
14340 tree name
= TYPE_IDENTIFIER (t
);
14341 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14343 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14344 return error_mark_node
;
14347 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14348 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14355 ++cp_unevaluated_operand
;
14356 ++c_inhibit_evaluation_warnings
;
14358 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14360 /*integral_constant_expression_p=*/false);
14362 --cp_unevaluated_operand
;
14363 --c_inhibit_evaluation_warnings
;
14365 type
= finish_typeof (type
);
14366 return cp_build_qualified_type_real (type
,
14368 | cp_type_quals (type
),
14372 case DECLTYPE_TYPE
:
14376 ++cp_unevaluated_operand
;
14377 ++c_inhibit_evaluation_warnings
;
14379 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14380 complain
|tf_decltype
, in_decl
,
14381 /*function_p*/false,
14382 /*integral_constant_expression*/false);
14384 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14386 if (type
== NULL_TREE
)
14388 if (complain
& tf_error
)
14389 error ("empty initializer in lambda init-capture");
14390 type
= error_mark_node
;
14392 else if (TREE_CODE (type
) == TREE_LIST
)
14393 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14396 --cp_unevaluated_operand
;
14397 --c_inhibit_evaluation_warnings
;
14399 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14400 type
= lambda_capture_field_type (type
,
14401 DECLTYPE_FOR_INIT_CAPTURE (t
),
14402 DECLTYPE_FOR_REF_CAPTURE (t
));
14403 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14404 type
= lambda_proxy_type (type
);
14407 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14408 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14410 /* In a template ~id could be either a complement expression
14411 or an unqualified-id naming a destructor; if instantiating
14412 it produces an expression, it's not an id-expression or
14415 type
= finish_decltype_type (type
, id
, complain
);
14417 return cp_build_qualified_type_real (type
,
14419 | cp_type_quals (type
),
14420 complain
| tf_ignore_bad_quals
);
14423 case UNDERLYING_TYPE
:
14425 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14426 complain
, in_decl
);
14427 return finish_underlying_type (type
);
14430 case TYPE_ARGUMENT_PACK
:
14431 case NONTYPE_ARGUMENT_PACK
:
14435 if (code
== NONTYPE_ARGUMENT_PACK
)
14436 r
= make_node (code
);
14438 r
= cxx_make_type (code
);
14440 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14441 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14442 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14460 /* We should use one of the expression tsubsts for these codes. */
14461 gcc_unreachable ();
14464 sorry ("use of %qs in template", get_tree_code_name (code
));
14465 return error_mark_node
;
14469 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14470 expression on the left-hand side of the "." or "->" operator. We
14471 only do the lookup if we had a dependent BASELINK. Otherwise we
14472 adjust it onto the instantiated heirarchy. */
14475 tsubst_baselink (tree baselink
, tree object_type
,
14476 tree args
, tsubst_flags_t complain
, tree in_decl
)
14478 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14479 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14480 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14482 tree optype
= BASELINK_OPTYPE (baselink
);
14483 optype
= tsubst (optype
, args
, complain
, in_decl
);
14485 tree template_args
= NULL_TREE
;
14486 bool template_id_p
= false;
14487 tree fns
= BASELINK_FUNCTIONS (baselink
);
14488 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14490 template_id_p
= true;
14491 template_args
= TREE_OPERAND (fns
, 1);
14492 fns
= TREE_OPERAND (fns
, 0);
14494 template_args
= tsubst_template_args (template_args
, args
,
14495 complain
, in_decl
);
14498 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14499 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14500 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14504 tree name
= OVL_NAME (fns
);
14505 if (IDENTIFIER_CONV_OP_P (name
))
14506 name
= make_conv_op_name (optype
);
14508 if (name
== complete_dtor_identifier
)
14509 /* Treat as-if non-dependent below. */
14510 dependent_p
= false;
14512 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14515 if ((complain
& tf_error
)
14516 && constructor_name_p (name
, qualifying_scope
))
14517 error ("cannot call constructor %<%T::%D%> directly",
14518 qualifying_scope
, name
);
14519 return error_mark_node
;
14522 if (BASELINK_P (baselink
))
14523 fns
= BASELINK_FUNCTIONS (baselink
);
14526 /* We're going to overwrite pieces below, make a duplicate. */
14527 baselink
= copy_node (baselink
);
14529 /* If lookup found a single function, mark it as used at this point.
14530 (If lookup found multiple functions the one selected later by
14531 overload resolution will be marked as used at that point.) */
14532 if (!template_id_p
&& !really_overloaded_fn (fns
)
14533 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14534 return error_mark_node
;
14536 if (BASELINK_P (baselink
))
14538 /* Add back the template arguments, if present. */
14540 BASELINK_FUNCTIONS (baselink
)
14541 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14543 /* Update the conversion operator type. */
14544 BASELINK_OPTYPE (baselink
) = optype
;
14548 object_type
= current_class_type
;
14550 if (qualified_p
|| !dependent_p
)
14552 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14556 /* We need to call adjust_result_of_qualified_name_lookup in case the
14557 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14558 so that we still get virtual function binding. */
14559 BASELINK_QUALIFIED_P (baselink
) = false;
14565 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14566 true if the qualified-id will be a postfix-expression in-and-of
14567 itself; false if more of the postfix-expression follows the
14568 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14572 tsubst_qualified_id (tree qualified_id
, tree args
,
14573 tsubst_flags_t complain
, tree in_decl
,
14574 bool done
, bool address_p
)
14580 tree template_args
;
14581 location_t loc
= UNKNOWN_LOCATION
;
14583 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14585 /* Figure out what name to look up. */
14586 name
= TREE_OPERAND (qualified_id
, 1);
14587 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14589 is_template
= true;
14590 loc
= EXPR_LOCATION (name
);
14591 template_args
= TREE_OPERAND (name
, 1);
14593 template_args
= tsubst_template_args (template_args
, args
,
14594 complain
, in_decl
);
14595 if (template_args
== error_mark_node
)
14596 return error_mark_node
;
14597 name
= TREE_OPERAND (name
, 0);
14601 is_template
= false;
14602 template_args
= NULL_TREE
;
14605 /* Substitute into the qualifying scope. When there are no ARGS, we
14606 are just trying to simplify a non-dependent expression. In that
14607 case the qualifying scope may be dependent, and, in any case,
14608 substituting will not help. */
14609 scope
= TREE_OPERAND (qualified_id
, 0);
14612 scope
= tsubst (scope
, args
, complain
, in_decl
);
14613 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14618 if (dependent_scope_p (scope
))
14621 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14622 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14623 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14624 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14628 if (!BASELINK_P (name
) && !DECL_P (expr
))
14630 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14632 /* A BIT_NOT_EXPR is used to represent a destructor. */
14633 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14635 error ("qualifying type %qT does not match destructor name ~%qT",
14636 scope
, TREE_OPERAND (expr
, 0));
14637 expr
= error_mark_node
;
14640 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14641 /*is_type_p=*/0, false);
14644 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14645 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14646 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14648 if (complain
& tf_error
)
14650 error ("dependent-name %qE is parsed as a non-type, but "
14651 "instantiation yields a type", qualified_id
);
14652 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14654 return error_mark_node
;
14660 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14662 /* Remember that there was a reference to this entity. */
14663 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14664 return error_mark_node
;
14667 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14669 if (complain
& tf_error
)
14670 qualified_name_lookup_error (scope
,
14671 TREE_OPERAND (qualified_id
, 1),
14672 expr
, input_location
);
14673 return error_mark_node
;
14678 if (variable_template_p (expr
))
14679 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14682 expr
= lookup_template_function (expr
, template_args
);
14685 if (expr
== error_mark_node
&& complain
& tf_error
)
14686 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14687 expr
, input_location
);
14688 else if (TYPE_P (scope
))
14690 expr
= (adjust_result_of_qualified_name_lookup
14691 (expr
, scope
, current_nonlambda_class_type ()));
14692 expr
= (finish_qualified_id_expr
14693 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14694 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14695 /*template_arg_p=*/false, complain
));
14698 /* Expressions do not generally have reference type. */
14699 if (TREE_CODE (expr
) != SCOPE_REF
14700 /* However, if we're about to form a pointer-to-member, we just
14701 want the referenced member referenced. */
14702 && TREE_CODE (expr
) != OFFSET_REF
)
14703 expr
= convert_from_reference (expr
);
14705 if (REF_PARENTHESIZED_P (qualified_id
))
14706 expr
= force_paren_expr (expr
);
14711 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14712 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14716 tsubst_init (tree init
, tree decl
, tree args
,
14717 tsubst_flags_t complain
, tree in_decl
)
14722 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14724 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14726 /* If we had an initializer but it
14727 instantiated to nothing,
14728 value-initialize the object. This will
14729 only occur when the initializer was a
14730 pack expansion where the parameter packs
14731 used in that expansion were of length
14733 init
= build_value_init (TREE_TYPE (decl
),
14735 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14736 init
= get_target_expr_sfinae (init
, complain
);
14737 if (TREE_CODE (init
) == TARGET_EXPR
)
14738 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14744 /* Like tsubst, but deals with expressions. This function just replaces
14745 template parms; to finish processing the resultant expression, use
14746 tsubst_copy_and_build or tsubst_expr. */
14749 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14751 enum tree_code code
;
14754 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14757 code
= TREE_CODE (t
);
14762 r
= retrieve_local_specialization (t
);
14764 if (r
== NULL_TREE
)
14766 /* We get here for a use of 'this' in an NSDMI. */
14767 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14768 return current_class_ptr
;
14770 /* This can happen for a parameter name used later in a function
14771 declaration (such as in a late-specified return type). Just
14772 make a dummy decl, since it's only used for its type. */
14773 gcc_assert (cp_unevaluated_operand
!= 0);
14774 r
= tsubst_decl (t
, args
, complain
);
14775 /* Give it the template pattern as its context; its true context
14776 hasn't been instantiated yet and this is good enough for
14778 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14781 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14782 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14783 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14784 return error_mark_node
;
14792 if (DECL_TEMPLATE_PARM_P (t
))
14793 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14794 /* There is no need to substitute into namespace-scope
14796 if (DECL_NAMESPACE_SCOPE_P (t
))
14798 /* If ARGS is NULL, then T is known to be non-dependent. */
14799 if (args
== NULL_TREE
)
14800 return scalar_constant_value (t
);
14802 /* Unfortunately, we cannot just call lookup_name here.
14805 template <int I> int f() {
14807 struct S { void g() { E e = a; } };
14810 When we instantiate f<7>::S::g(), say, lookup_name is not
14811 clever enough to find f<7>::a. */
14813 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14814 /*entering_scope=*/0);
14816 for (v
= TYPE_VALUES (enum_type
);
14818 v
= TREE_CHAIN (v
))
14819 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14820 return TREE_VALUE (v
);
14822 /* We didn't find the name. That should never happen; if
14823 name-lookup found it during preliminary parsing, we
14824 should find it again here during instantiation. */
14825 gcc_unreachable ();
14830 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14832 /* Check for a local specialization set up by
14833 tsubst_pack_expansion. */
14834 if (tree r
= retrieve_local_specialization (t
))
14836 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14837 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14841 /* When retrieving a capture pack from a generic lambda, remove the
14842 lambda call op's own template argument list from ARGS. Only the
14843 template arguments active for the closure type should be used to
14844 retrieve the pack specialization. */
14845 if (LAMBDA_FUNCTION_P (current_function_decl
)
14846 && (template_class_depth (DECL_CONTEXT (t
))
14847 != TMPL_ARGS_DEPTH (args
)))
14848 args
= strip_innermost_template_args (args
, 1);
14850 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14851 tsubst_decl put in the hash table. */
14852 return retrieve_specialization (t
, args
, 0);
14855 if (DECL_CONTEXT (t
))
14859 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14860 /*entering_scope=*/1);
14861 if (ctx
!= DECL_CONTEXT (t
))
14863 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14866 if (complain
& tf_error
)
14867 error ("using invalid field %qD", t
);
14868 return error_mark_node
;
14877 case FUNCTION_DECL
:
14878 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14879 r
= tsubst (t
, args
, complain
, in_decl
);
14880 else if (local_variable_p (t
)
14881 && uses_template_parms (DECL_CONTEXT (t
)))
14883 r
= retrieve_local_specialization (t
);
14884 if (r
== NULL_TREE
)
14886 /* First try name lookup to find the instantiation. */
14887 r
= lookup_name (DECL_NAME (t
));
14888 if (r
&& !is_capture_proxy (r
))
14890 /* Make sure that the one we found is the one we want. */
14891 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14892 if (ctx
!= DECL_CONTEXT (r
))
14900 /* This can happen for a variable used in a
14901 late-specified return type of a local lambda, or for a
14902 local static or constant. Building a new VAR_DECL
14903 should be OK in all those cases. */
14904 r
= tsubst_decl (t
, args
, complain
);
14905 if (local_specializations
)
14906 /* Avoid infinite recursion (79640). */
14907 register_local_specialization (r
, t
);
14908 if (decl_maybe_constant_var_p (r
))
14910 /* We can't call cp_finish_decl, so handle the
14911 initializer by hand. */
14912 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14913 complain
, in_decl
);
14914 if (!processing_template_decl
)
14915 init
= maybe_constant_init (init
);
14916 if (processing_template_decl
14917 ? potential_constant_expression (init
)
14918 : reduced_constant_expression_p (init
))
14919 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14920 = TREE_CONSTANT (r
) = true;
14921 DECL_INITIAL (r
) = init
;
14922 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14924 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14925 complain
, adc_variable_type
);
14927 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14928 || decl_constant_var_p (r
)
14929 || errorcount
|| sorrycount
);
14930 if (!processing_template_decl
14931 && !TREE_STATIC (r
))
14932 r
= process_outer_var_ref (r
, complain
);
14934 /* Remember this for subsequent uses. */
14935 if (local_specializations
)
14936 register_local_specialization (r
, t
);
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
);
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 (DECL_PACK_P (decl
))
16109 /* Don't build up decls for a variadic capture proxy, we'll
16110 instantiate the elements directly as needed. */
16113 else if (is_capture_proxy (decl
)
16114 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16116 /* We're in tsubst_lambda_expr, we've already inserted a new
16117 capture proxy, so look it up and register it. */
16118 tree inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16119 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16120 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16121 register_local_specialization (inst
, decl
);
16124 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16125 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16126 /* Don't copy the old closure; we'll create a new one in
16127 tsubst_lambda_expr. */
16131 init
= DECL_INITIAL (decl
);
16132 decl
= tsubst (decl
, args
, complain
, in_decl
);
16133 if (decl
!= error_mark_node
)
16135 /* By marking the declaration as instantiated, we avoid
16136 trying to instantiate it. Since instantiate_decl can't
16137 handle local variables, and since we've already done
16138 all that needs to be done, that's the right thing to
16141 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16143 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16144 /* Anonymous aggregates are a special case. */
16145 finish_anon_union (decl
);
16146 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16148 DECL_CONTEXT (decl
) = current_function_decl
;
16149 if (DECL_NAME (decl
) == this_identifier
)
16151 tree lam
= DECL_CONTEXT (current_function_decl
);
16152 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16153 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16155 insert_capture_proxy (decl
);
16157 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16158 /* We already did a pushtag. */;
16159 else if (TREE_CODE (decl
) == FUNCTION_DECL
16160 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16161 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16163 DECL_CONTEXT (decl
) = NULL_TREE
;
16165 DECL_CONTEXT (decl
) = current_function_decl
;
16166 cp_check_omp_declare_reduction (decl
);
16170 int const_init
= false;
16171 maybe_push_decl (decl
);
16173 && DECL_PRETTY_FUNCTION_P (decl
))
16175 /* For __PRETTY_FUNCTION__ we have to adjust the
16177 const char *const name
16178 = cxx_printable_name (current_function_decl
, 2);
16179 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16182 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16185 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16188 && DECL_DECOMPOSITION_P (decl
)
16189 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16194 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16195 complain
, in_decl
, &first
, &cnt
);
16196 if (ndecl
!= error_mark_node
)
16197 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16198 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16199 if (ndecl
!= error_mark_node
)
16200 cp_finish_decomp (ndecl
, first
, cnt
);
16203 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16212 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16213 RECUR (FOR_INIT_STMT (t
));
16214 finish_init_stmt (stmt
);
16215 tmp
= RECUR (FOR_COND (t
));
16216 finish_for_cond (tmp
, stmt
, false, 0);
16217 tmp
= RECUR (FOR_EXPR (t
));
16218 finish_for_expr (tmp
, stmt
);
16220 bool prev
= note_iteration_stmt_body_start ();
16221 RECUR (FOR_BODY (t
));
16222 note_iteration_stmt_body_end (prev
);
16224 finish_for_stmt (stmt
);
16227 case RANGE_FOR_STMT
:
16229 /* Construct another range_for, if this is not a final
16230 substitution (for inside inside a generic lambda of a
16231 template). Otherwise convert to a regular for. */
16233 stmt
= (processing_template_decl
16234 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16235 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16236 decl
= RANGE_FOR_DECL (t
);
16237 decl
= tsubst (decl
, args
, complain
, in_decl
);
16238 maybe_push_decl (decl
);
16239 expr
= RECUR (RANGE_FOR_EXPR (t
));
16241 tree decomp_first
= NULL_TREE
;
16242 unsigned decomp_cnt
= 0;
16243 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16244 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16246 &decomp_first
, &decomp_cnt
);
16248 if (processing_template_decl
)
16250 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16251 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16252 finish_range_for_decl (stmt
, decl
, expr
);
16256 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16257 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16258 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16259 decomp_first
, decomp_cnt
,
16260 RANGE_FOR_IVDEP (t
), unroll
);
16263 bool prev
= note_iteration_stmt_body_start ();
16264 RECUR (RANGE_FOR_BODY (t
));
16265 note_iteration_stmt_body_end (prev
);
16266 finish_for_stmt (stmt
);
16271 stmt
= begin_while_stmt ();
16272 tmp
= RECUR (WHILE_COND (t
));
16273 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16275 bool prev
= note_iteration_stmt_body_start ();
16276 RECUR (WHILE_BODY (t
));
16277 note_iteration_stmt_body_end (prev
);
16279 finish_while_stmt (stmt
);
16283 stmt
= begin_do_stmt ();
16285 bool prev
= note_iteration_stmt_body_start ();
16286 RECUR (DO_BODY (t
));
16287 note_iteration_stmt_body_end (prev
);
16289 finish_do_body (stmt
);
16290 tmp
= RECUR (DO_COND (t
));
16291 finish_do_stmt (tmp
, stmt
, false, 0);
16295 stmt
= begin_if_stmt ();
16296 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16297 tmp
= RECUR (IF_COND (t
));
16298 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16299 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16300 /* Don't instantiate the THEN_CLAUSE. */;
16303 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16305 ++c_inhibit_evaluation_warnings
;
16306 RECUR (THEN_CLAUSE (t
));
16308 --c_inhibit_evaluation_warnings
;
16310 finish_then_clause (stmt
);
16312 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16313 /* Don't instantiate the ELSE_CLAUSE. */;
16314 else if (ELSE_CLAUSE (t
))
16316 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16317 begin_else_clause (stmt
);
16319 ++c_inhibit_evaluation_warnings
;
16320 RECUR (ELSE_CLAUSE (t
));
16322 --c_inhibit_evaluation_warnings
;
16323 finish_else_clause (stmt
);
16326 finish_if_stmt (stmt
);
16330 if (BIND_EXPR_BODY_BLOCK (t
))
16331 stmt
= begin_function_body ();
16333 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16334 ? BCS_TRY_BLOCK
: 0);
16336 RECUR (BIND_EXPR_BODY (t
));
16338 if (BIND_EXPR_BODY_BLOCK (t
))
16339 finish_function_body (stmt
);
16341 finish_compound_stmt (stmt
);
16345 finish_break_stmt ();
16348 case CONTINUE_STMT
:
16349 finish_continue_stmt ();
16353 stmt
= begin_switch_stmt ();
16354 tmp
= RECUR (SWITCH_STMT_COND (t
));
16355 finish_switch_cond (tmp
, stmt
);
16356 RECUR (SWITCH_STMT_BODY (t
));
16357 finish_switch_stmt (stmt
);
16360 case CASE_LABEL_EXPR
:
16362 tree low
= RECUR (CASE_LOW (t
));
16363 tree high
= RECUR (CASE_HIGH (t
));
16364 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16365 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16366 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16367 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16373 tree decl
= LABEL_EXPR_LABEL (t
);
16376 label
= finish_label_stmt (DECL_NAME (decl
));
16377 if (TREE_CODE (label
) == LABEL_DECL
)
16378 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16379 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16380 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16385 tmp
= GOTO_DESTINATION (t
);
16386 if (TREE_CODE (tmp
) != LABEL_DECL
)
16387 /* Computed goto's must be tsubst'd into. On the other hand,
16388 non-computed gotos must not be; the identifier in question
16389 will have no binding. */
16392 tmp
= DECL_NAME (tmp
);
16393 finish_goto_stmt (tmp
);
16398 tree string
= RECUR (ASM_STRING (t
));
16399 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16400 complain
, in_decl
);
16401 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16402 complain
, in_decl
);
16403 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16404 complain
, in_decl
);
16405 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16406 complain
, in_decl
);
16407 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16409 tree asm_expr
= tmp
;
16410 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16411 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16412 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16419 stmt
= begin_try_block ();
16420 RECUR (TRY_STMTS (t
));
16421 finish_cleanup_try_block (stmt
);
16422 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16426 tree compound_stmt
= NULL_TREE
;
16428 if (FN_TRY_BLOCK_P (t
))
16429 stmt
= begin_function_try_block (&compound_stmt
);
16431 stmt
= begin_try_block ();
16433 RECUR (TRY_STMTS (t
));
16435 if (FN_TRY_BLOCK_P (t
))
16436 finish_function_try_block (stmt
);
16438 finish_try_block (stmt
);
16440 RECUR (TRY_HANDLERS (t
));
16441 if (FN_TRY_BLOCK_P (t
))
16442 finish_function_handler_sequence (stmt
, compound_stmt
);
16444 finish_handler_sequence (stmt
);
16450 tree decl
= HANDLER_PARMS (t
);
16454 decl
= tsubst (decl
, args
, complain
, in_decl
);
16455 /* Prevent instantiate_decl from trying to instantiate
16456 this variable. We've already done all that needs to be
16458 if (decl
!= error_mark_node
)
16459 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16461 stmt
= begin_handler ();
16462 finish_handler_parms (decl
, stmt
);
16463 RECUR (HANDLER_BODY (t
));
16464 finish_handler (stmt
);
16469 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16470 if (CLASS_TYPE_P (tmp
))
16472 /* Local classes are not independent templates; they are
16473 instantiated along with their containing function. And this
16474 way we don't have to deal with pushing out of one local class
16475 to instantiate a member of another local class. */
16476 /* Closures are handled by the LAMBDA_EXPR. */
16477 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16478 complete_type (tmp
);
16479 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16481 || (TREE_CODE (fld
) == FUNCTION_DECL
16482 && !DECL_ARTIFICIAL (fld
)))
16483 && DECL_TEMPLATE_INSTANTIATION (fld
))
16484 instantiate_decl (fld
, /*defer_ok=*/false,
16485 /*expl_inst_class=*/false);
16489 case STATIC_ASSERT
:
16493 ++c_inhibit_evaluation_warnings
;
16495 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16498 /*integral_constant_expression_p=*/true);
16499 --c_inhibit_evaluation_warnings
;
16501 finish_static_assert (condition
,
16502 STATIC_ASSERT_MESSAGE (t
),
16503 STATIC_ASSERT_SOURCE_LOCATION (t
),
16504 /*member_p=*/false);
16509 case OACC_PARALLEL
:
16510 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16512 stmt
= begin_omp_parallel ();
16513 RECUR (OMP_BODY (t
));
16514 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16518 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16519 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16520 complain
, in_decl
);
16521 if (OMP_PARALLEL_COMBINED (t
))
16522 omp_parallel_combined_clauses
= &tmp
;
16523 stmt
= begin_omp_parallel ();
16524 RECUR (OMP_PARALLEL_BODY (t
));
16525 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16526 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16527 = OMP_PARALLEL_COMBINED (t
);
16528 pop_omp_privatization_clauses (r
);
16532 r
= push_omp_privatization_clauses (false);
16533 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16534 complain
, in_decl
);
16535 stmt
= begin_omp_task ();
16536 RECUR (OMP_TASK_BODY (t
));
16537 finish_omp_task (tmp
, stmt
);
16538 pop_omp_privatization_clauses (r
);
16543 case OMP_DISTRIBUTE
:
16547 tree clauses
, body
, pre_body
;
16548 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16549 tree orig_declv
= NULL_TREE
;
16550 tree incrv
= NULL_TREE
;
16551 enum c_omp_region_type ort
= C_ORT_OMP
;
16554 if (TREE_CODE (t
) == OACC_LOOP
)
16557 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16558 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16560 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16562 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16563 if (OMP_FOR_ORIG_DECLS (t
))
16564 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16565 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16566 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16567 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16570 stmt
= begin_omp_structured_block ();
16572 pre_body
= push_stmt_list ();
16573 RECUR (OMP_FOR_PRE_BODY (t
));
16574 pre_body
= pop_stmt_list (pre_body
);
16576 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16577 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16578 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16579 incrv
, &clauses
, args
, complain
, in_decl
,
16580 integral_constant_expression_p
);
16581 omp_parallel_combined_clauses
= NULL
;
16583 body
= push_stmt_list ();
16584 RECUR (OMP_FOR_BODY (t
));
16585 body
= pop_stmt_list (body
);
16587 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16588 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16589 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16593 t
= make_node (TREE_CODE (t
));
16594 TREE_TYPE (t
) = void_type_node
;
16595 OMP_FOR_BODY (t
) = body
;
16596 OMP_FOR_PRE_BODY (t
) = pre_body
;
16597 OMP_FOR_CLAUSES (t
) = clauses
;
16598 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16602 add_stmt (finish_omp_structured_block (stmt
));
16603 pop_omp_privatization_clauses (r
);
16608 omp_parallel_combined_clauses
= NULL
;
16613 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16614 && OMP_TEAMS_COMBINED (t
));
16615 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16617 stmt
= push_stmt_list ();
16618 RECUR (OMP_BODY (t
));
16619 stmt
= pop_stmt_list (stmt
);
16622 OMP_BODY (t
) = stmt
;
16623 OMP_CLAUSES (t
) = tmp
;
16625 pop_omp_privatization_clauses (r
);
16629 case OMP_TARGET_DATA
:
16631 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16632 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16634 keep_next_level (true);
16635 stmt
= begin_omp_structured_block ();
16637 RECUR (OMP_BODY (t
));
16638 stmt
= finish_omp_structured_block (stmt
);
16641 OMP_BODY (t
) = stmt
;
16642 OMP_CLAUSES (t
) = tmp
;
16643 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16645 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16648 /* For combined target teams, ensure the num_teams and
16649 thread_limit clause expressions are evaluated on the host,
16650 before entering the target construct. */
16652 for (c
= OMP_TEAMS_CLAUSES (teams
);
16653 c
; c
= OMP_CLAUSE_CHAIN (c
))
16654 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16655 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16656 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16658 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16659 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16660 if (expr
== error_mark_node
)
16662 tmp
= TARGET_EXPR_SLOT (expr
);
16664 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16665 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16666 OMP_CLAUSE_FIRSTPRIVATE
);
16667 OMP_CLAUSE_DECL (tc
) = tmp
;
16668 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16669 OMP_TARGET_CLAUSES (t
) = tc
;
16678 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16679 complain
, in_decl
);
16680 OACC_DECLARE_CLAUSES (t
) = tmp
;
16684 case OMP_TARGET_UPDATE
:
16685 case OMP_TARGET_ENTER_DATA
:
16686 case OMP_TARGET_EXIT_DATA
:
16687 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16688 complain
, in_decl
);
16690 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16694 case OACC_ENTER_DATA
:
16695 case OACC_EXIT_DATA
:
16697 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16698 complain
, in_decl
);
16700 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16705 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16706 complain
, in_decl
);
16707 stmt
= push_stmt_list ();
16708 RECUR (OMP_BODY (t
));
16709 stmt
= pop_stmt_list (stmt
);
16712 OMP_BODY (t
) = stmt
;
16713 OMP_ORDERED_CLAUSES (t
) = tmp
;
16719 case OMP_TASKGROUP
:
16720 stmt
= push_stmt_list ();
16721 RECUR (OMP_BODY (t
));
16722 stmt
= pop_stmt_list (stmt
);
16725 OMP_BODY (t
) = stmt
;
16730 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16731 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16733 tree op1
= TREE_OPERAND (t
, 1);
16734 tree rhs1
= NULL_TREE
;
16736 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16738 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16739 op1
= TREE_OPERAND (op1
, 1);
16741 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16742 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16743 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16744 NULL_TREE
, NULL_TREE
, rhs1
,
16745 OMP_ATOMIC_SEQ_CST (t
));
16749 tree op1
= TREE_OPERAND (t
, 1);
16750 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16751 tree rhs1
= NULL_TREE
;
16752 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16753 enum tree_code opcode
= NOP_EXPR
;
16754 if (code
== OMP_ATOMIC_READ
)
16756 v
= RECUR (TREE_OPERAND (op1
, 0));
16757 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16759 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16760 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16762 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16763 v
= RECUR (TREE_OPERAND (op1
, 0));
16764 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16765 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16767 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16768 op11
= TREE_OPERAND (op11
, 1);
16770 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16771 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16772 opcode
= TREE_CODE (op11
);
16773 if (opcode
== MODIFY_EXPR
)
16779 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16780 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16782 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16783 OMP_ATOMIC_SEQ_CST (t
));
16787 case TRANSACTION_EXPR
:
16790 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16791 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16793 if (TRANSACTION_EXPR_IS_STMT (t
))
16795 tree body
= TRANSACTION_EXPR_BODY (t
);
16796 tree noex
= NULL_TREE
;
16797 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16799 noex
= MUST_NOT_THROW_COND (body
);
16800 if (noex
== NULL_TREE
)
16801 noex
= boolean_true_node
;
16802 body
= TREE_OPERAND (body
, 0);
16804 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16806 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16810 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16811 RECUR (TRANSACTION_EXPR_BODY (t
)),
16818 case MUST_NOT_THROW_EXPR
:
16820 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16821 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16822 RETURN (build_must_not_throw_expr (op0
, cond
));
16825 case EXPR_PACK_EXPANSION
:
16826 error ("invalid use of pack expansion expression");
16827 RETURN (error_mark_node
);
16829 case NONTYPE_ARGUMENT_PACK
:
16830 error ("use %<...%> to expand argument pack");
16831 RETURN (error_mark_node
);
16833 case COMPOUND_EXPR
:
16834 tmp
= RECUR (TREE_OPERAND (t
, 0));
16835 if (tmp
== NULL_TREE
)
16836 /* If the first operand was a statement, we're done with it. */
16837 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16838 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16839 RECUR (TREE_OPERAND (t
, 1)),
16842 case ANNOTATE_EXPR
:
16843 tmp
= RECUR (TREE_OPERAND (t
, 0));
16844 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16845 TREE_TYPE (tmp
), tmp
,
16846 RECUR (TREE_OPERAND (t
, 1)),
16847 RECUR (TREE_OPERAND (t
, 2))));
16850 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16852 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16853 /*function_p=*/false,
16854 integral_constant_expression_p
));
16857 RETURN (NULL_TREE
);
16859 input_location
= loc
;
16865 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16866 function. For description of the body see comment above
16867 cp_parser_omp_declare_reduction_exprs. */
16870 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16872 if (t
== NULL_TREE
|| t
== error_mark_node
)
16875 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16877 tree_stmt_iterator tsi
;
16880 memset (stmts
, 0, sizeof stmts
);
16881 for (i
= 0, tsi
= tsi_start (t
);
16882 i
< 7 && !tsi_end_p (tsi
);
16883 i
++, tsi_next (&tsi
))
16884 stmts
[i
] = tsi_stmt (tsi
);
16885 gcc_assert (tsi_end_p (tsi
));
16889 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16890 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16891 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16892 args
, complain
, in_decl
);
16893 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16894 args
, complain
, in_decl
);
16895 DECL_CONTEXT (omp_out
) = current_function_decl
;
16896 DECL_CONTEXT (omp_in
) = current_function_decl
;
16897 keep_next_level (true);
16898 tree block
= begin_omp_structured_block ();
16899 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16900 block
= finish_omp_structured_block (block
);
16901 block
= maybe_cleanup_point_expr_void (block
);
16902 add_decl_expr (omp_out
);
16903 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16904 TREE_NO_WARNING (omp_out
) = 1;
16905 add_decl_expr (omp_in
);
16906 finish_expr_stmt (block
);
16910 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16911 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16912 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16913 args
, complain
, in_decl
);
16914 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16915 args
, complain
, in_decl
);
16916 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16917 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16918 keep_next_level (true);
16919 tree block
= begin_omp_structured_block ();
16920 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16921 block
= finish_omp_structured_block (block
);
16922 block
= maybe_cleanup_point_expr_void (block
);
16923 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16924 add_decl_expr (omp_priv
);
16925 add_decl_expr (omp_orig
);
16926 finish_expr_stmt (block
);
16928 add_decl_expr (omp_orig
);
16932 /* T is a postfix-expression that is not being used in a function
16933 call. Return the substituted version of T. */
16936 tsubst_non_call_postfix_expression (tree t
, tree args
,
16937 tsubst_flags_t complain
,
16940 if (TREE_CODE (t
) == SCOPE_REF
)
16941 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16942 /*done=*/false, /*address_p=*/false);
16944 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16945 /*function_p=*/false,
16946 /*integral_constant_expression_p=*/false);
16951 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16952 instantiation context. Instantiating a pack expansion containing a lambda
16953 might result in multiple lambdas all based on the same lambda in the
16957 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16959 tree oldfn
= lambda_function (t
);
16962 tree r
= build_lambda_expr ();
16964 LAMBDA_EXPR_LOCATION (r
)
16965 = LAMBDA_EXPR_LOCATION (t
);
16966 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16967 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16968 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16970 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16971 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16973 record_lambda_scope (r
);
16975 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16976 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16978 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16979 cap
= TREE_CHAIN (cap
))
16981 tree field
= TREE_PURPOSE (cap
);
16982 if (PACK_EXPANSION_P (field
))
16983 field
= PACK_EXPANSION_PATTERN (field
);
16984 field
= tsubst_decl (field
, args
, complain
);
16986 if (field
== error_mark_node
)
16987 return error_mark_node
;
16989 tree init
= TREE_VALUE (cap
);
16990 if (PACK_EXPANSION_P (init
))
16991 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
16993 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
16994 /*fn*/false, /*constexpr*/false);
16996 if (TREE_CODE (field
) == TREE_VEC
)
16998 int len
= TREE_VEC_LENGTH (field
);
16999 gcc_assert (TREE_CODE (init
) == TREE_VEC
17000 && TREE_VEC_LENGTH (init
) == len
);
17001 for (int i
= 0; i
< len
; ++i
)
17002 LAMBDA_EXPR_CAPTURE_LIST (r
)
17003 = tree_cons (TREE_VEC_ELT (field
, i
),
17004 TREE_VEC_ELT (init
, i
),
17005 LAMBDA_EXPR_CAPTURE_LIST (r
));
17009 LAMBDA_EXPR_CAPTURE_LIST (r
)
17010 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17012 if (id_equal (DECL_NAME (field
), "__this"))
17013 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17017 tree type
= begin_lambda_type (r
);
17019 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17020 determine_visibility (TYPE_NAME (type
));
17022 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17024 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17025 ? DECL_TI_TEMPLATE (oldfn
)
17028 tree fntype
= static_fn_type (oldfn
);
17030 ++processing_template_decl
;
17031 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17033 --processing_template_decl
;
17035 if (fntype
== error_mark_node
)
17036 r
= error_mark_node
;
17039 /* Fix the type of 'this'. */
17040 fntype
= build_memfn_type (fntype
, type
,
17041 type_memfn_quals (fntype
),
17042 type_memfn_rqual (fntype
));
17046 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17047 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17048 finish_member_declaration (tmpl
);
17053 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17054 finish_member_declaration (fn
);
17057 /* Let finish_function set this. */
17058 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17060 bool nested
= cfun
;
17062 push_function_context ();
17064 local_specialization_stack
s (lss_copy
);
17066 tree body
= start_lambda_function (fn
, r
);
17068 register_parameter_specializations (oldfn
, fn
);
17070 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17071 /*constexpr*/false);
17073 finish_lambda_function (body
);
17076 pop_function_context ();
17078 /* The capture list was built up in reverse order; fix that now. */
17079 LAMBDA_EXPR_CAPTURE_LIST (r
)
17080 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17082 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17084 maybe_add_lambda_conv_op (type
);
17087 finish_struct (type
, /*attr*/NULL_TREE
);
17089 insert_pending_capture_proxies ();
17094 /* Like tsubst but deals with expressions and performs semantic
17095 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17098 tsubst_copy_and_build (tree t
,
17100 tsubst_flags_t complain
,
17103 bool integral_constant_expression_p
)
17105 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17106 #define RECUR(NODE) \
17107 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17108 /*function_p=*/false, \
17109 integral_constant_expression_p)
17114 if (t
== NULL_TREE
|| t
== error_mark_node
)
17117 loc
= input_location
;
17118 if (EXPR_HAS_LOCATION (t
))
17119 input_location
= EXPR_LOCATION (t
);
17121 /* N3276 decltype magic only applies to calls at the top level or on the
17122 right side of a comma. */
17123 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17124 complain
&= ~tf_decltype
;
17126 switch (TREE_CODE (t
))
17130 /* Fall through. */
17131 case IDENTIFIER_NODE
:
17135 bool non_integral_constant_expression_p
;
17136 const char *error_msg
;
17138 if (IDENTIFIER_CONV_OP_P (t
))
17140 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17141 t
= make_conv_op_name (new_type
);
17144 /* Look up the name. */
17145 decl
= lookup_name (t
);
17147 /* By convention, expressions use ERROR_MARK_NODE to indicate
17148 failure, not NULL_TREE. */
17149 if (decl
== NULL_TREE
)
17150 decl
= error_mark_node
;
17152 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17154 integral_constant_expression_p
,
17155 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17156 &non_integral_constant_expression_p
,
17157 /*template_p=*/false,
17159 /*address_p=*/false,
17160 /*template_arg_p=*/false,
17165 if (!function_p
&& identifier_p (decl
))
17167 if (complain
& tf_error
)
17168 unqualified_name_lookup_error (decl
);
17169 decl
= error_mark_node
;
17174 case TEMPLATE_ID_EXPR
:
17177 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17178 tree targs
= TREE_OPERAND (t
, 1);
17181 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17182 if (targs
== error_mark_node
)
17183 return error_mark_node
;
17185 if (TREE_CODE (templ
) == SCOPE_REF
)
17187 tree name
= TREE_OPERAND (templ
, 1);
17188 tree tid
= lookup_template_function (name
, targs
);
17189 TREE_OPERAND (templ
, 1) = tid
;
17193 if (variable_template_p (templ
))
17194 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17196 if (TREE_CODE (templ
) == COMPONENT_REF
)
17198 object
= TREE_OPERAND (templ
, 0);
17199 templ
= TREE_OPERAND (templ
, 1);
17202 object
= NULL_TREE
;
17203 templ
= lookup_template_function (templ
, targs
);
17206 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17207 object
, templ
, NULL_TREE
));
17209 RETURN (baselink_for_fns (templ
));
17214 tree r
= RECUR (TREE_OPERAND (t
, 0));
17216 if (REFERENCE_REF_P (t
))
17218 /* A type conversion to reference type will be enclosed in
17219 such an indirect ref, but the substitution of the cast
17220 will have also added such an indirect ref. */
17221 r
= convert_from_reference (r
);
17224 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17225 complain
|decltype_flag
);
17227 if (REF_PARENTHESIZED_P (t
))
17228 r
= force_paren_expr (r
);
17235 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17236 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17237 RETURN (build_nop (type
, op0
));
17240 case IMPLICIT_CONV_EXPR
:
17242 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17243 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17244 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17246 retval
= copy_node (t
);
17247 TREE_TYPE (retval
) = type
;
17248 TREE_OPERAND (retval
, 0) = expr
;
17251 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17252 /* We'll pass this to convert_nontype_argument again, we don't need
17253 to actually perform any conversion here. */
17255 int flags
= LOOKUP_IMPLICIT
;
17256 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17257 flags
= LOOKUP_NORMAL
;
17258 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17264 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17265 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17266 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17270 case REINTERPRET_CAST_EXPR
:
17271 case CONST_CAST_EXPR
:
17272 case DYNAMIC_CAST_EXPR
:
17273 case STATIC_CAST_EXPR
:
17276 tree op
, r
= NULL_TREE
;
17278 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17279 if (integral_constant_expression_p
17280 && !cast_valid_in_integral_constant_expression_p (type
))
17282 if (complain
& tf_error
)
17283 error ("a cast to a type other than an integral or "
17284 "enumeration type cannot appear in a constant-expression");
17285 RETURN (error_mark_node
);
17288 op
= RECUR (TREE_OPERAND (t
, 0));
17290 warning_sentinel
s(warn_useless_cast
);
17291 warning_sentinel
s2(warn_ignored_qualifiers
);
17292 switch (TREE_CODE (t
))
17295 r
= build_functional_cast (type
, op
, complain
);
17297 case REINTERPRET_CAST_EXPR
:
17298 r
= build_reinterpret_cast (type
, op
, complain
);
17300 case CONST_CAST_EXPR
:
17301 r
= build_const_cast (type
, op
, complain
);
17303 case DYNAMIC_CAST_EXPR
:
17304 r
= build_dynamic_cast (type
, op
, complain
);
17306 case STATIC_CAST_EXPR
:
17307 r
= build_static_cast (type
, op
, complain
);
17310 gcc_unreachable ();
17316 case POSTDECREMENT_EXPR
:
17317 case POSTINCREMENT_EXPR
:
17318 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17319 args
, complain
, in_decl
);
17320 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17321 complain
|decltype_flag
));
17323 case PREDECREMENT_EXPR
:
17324 case PREINCREMENT_EXPR
:
17328 case TRUTH_NOT_EXPR
:
17329 case UNARY_PLUS_EXPR
: /* Unary + */
17330 case REALPART_EXPR
:
17331 case IMAGPART_EXPR
:
17332 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17333 RECUR (TREE_OPERAND (t
, 0)),
17334 complain
|decltype_flag
));
17336 case FIX_TRUNC_EXPR
:
17337 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17341 op1
= TREE_OPERAND (t
, 0);
17342 if (TREE_CODE (op1
) == LABEL_DECL
)
17343 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17344 EXPR_LOCATION (op1
)));
17345 if (TREE_CODE (op1
) == SCOPE_REF
)
17346 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17347 /*done=*/true, /*address_p=*/true);
17349 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17351 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17352 complain
|decltype_flag
));
17357 case TRUNC_DIV_EXPR
:
17358 case CEIL_DIV_EXPR
:
17359 case FLOOR_DIV_EXPR
:
17360 case ROUND_DIV_EXPR
:
17361 case EXACT_DIV_EXPR
:
17365 case TRUNC_MOD_EXPR
:
17366 case FLOOR_MOD_EXPR
:
17367 case TRUTH_ANDIF_EXPR
:
17368 case TRUTH_ORIF_EXPR
:
17369 case TRUTH_AND_EXPR
:
17370 case TRUTH_OR_EXPR
:
17386 warning_sentinel
s1(warn_type_limits
);
17387 warning_sentinel
s2(warn_div_by_zero
);
17388 warning_sentinel
s3(warn_logical_op
);
17389 warning_sentinel
s4(warn_tautological_compare
);
17390 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17391 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17392 tree r
= build_x_binary_op
17393 (input_location
, TREE_CODE (t
),
17395 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17397 : TREE_CODE (TREE_OPERAND (t
, 0))),
17399 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17401 : TREE_CODE (TREE_OPERAND (t
, 1))),
17403 complain
|decltype_flag
);
17404 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17405 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17410 case POINTER_PLUS_EXPR
:
17412 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17413 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17414 return fold_build_pointer_plus (op0
, op1
);
17418 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17419 /*address_p=*/false));
17421 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17422 args
, complain
, in_decl
);
17423 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17424 RECUR (TREE_OPERAND (t
, 1)),
17425 complain
|decltype_flag
));
17428 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17429 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17430 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17437 op1
= TREE_OPERAND (t
, 0);
17438 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17439 op1
= TREE_TYPE (op1
);
17442 /* When there are no ARGS, we are trying to evaluate a
17443 non-dependent expression from the parser. Trying to do
17444 the substitutions may not work. */
17446 op1
= TREE_TYPE (op1
);
17450 ++cp_unevaluated_operand
;
17451 ++c_inhibit_evaluation_warnings
;
17453 op1
= tsubst (op1
, args
, complain
, in_decl
);
17455 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17456 /*function_p=*/false,
17457 /*integral_constant_expression_p=*/
17459 --cp_unevaluated_operand
;
17460 --c_inhibit_evaluation_warnings
;
17463 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17464 complain
& tf_error
);
17466 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17467 complain
& tf_error
);
17468 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17470 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17472 if (!processing_template_decl
&& TYPE_P (op1
))
17474 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17475 build1 (NOP_EXPR
, op1
, error_mark_node
));
17476 SIZEOF_EXPR_TYPE_P (r
) = 1;
17479 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17480 TREE_SIDE_EFFECTS (r
) = 0;
17481 TREE_READONLY (r
) = 1;
17483 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17488 case AT_ENCODE_EXPR
:
17490 op1
= TREE_OPERAND (t
, 0);
17491 ++cp_unevaluated_operand
;
17492 ++c_inhibit_evaluation_warnings
;
17493 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17494 /*function_p=*/false,
17495 /*integral_constant_expression_p=*/false);
17496 --cp_unevaluated_operand
;
17497 --c_inhibit_evaluation_warnings
;
17498 RETURN (objc_build_encode_expr (op1
));
17501 case NOEXCEPT_EXPR
:
17502 op1
= TREE_OPERAND (t
, 0);
17503 ++cp_unevaluated_operand
;
17504 ++c_inhibit_evaluation_warnings
;
17505 ++cp_noexcept_operand
;
17506 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17507 /*function_p=*/false,
17508 /*integral_constant_expression_p=*/false);
17509 --cp_unevaluated_operand
;
17510 --c_inhibit_evaluation_warnings
;
17511 --cp_noexcept_operand
;
17512 RETURN (finish_noexcept_expr (op1
, complain
));
17516 warning_sentinel
s(warn_div_by_zero
);
17517 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17518 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17519 tree r
= build_x_modify_expr
17520 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17521 complain
|decltype_flag
);
17522 /* TREE_NO_WARNING must be set if either the expression was
17523 parenthesized or it uses an operator such as >>= rather
17524 than plain assignment. In the former case, it was already
17525 set and must be copied. In the latter case,
17526 build_x_modify_expr sets it and it must not be reset
17528 if (TREE_NO_WARNING (t
))
17529 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17535 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17536 args
, complain
, in_decl
);
17537 /* Remember that there was a reference to this entity. */
17539 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17540 RETURN (error_mark_node
);
17541 RETURN (build_x_arrow (input_location
, op1
, complain
));
17545 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17546 tree init
= RECUR (TREE_OPERAND (t
, 3));
17547 vec
<tree
, va_gc
> *placement_vec
;
17548 vec
<tree
, va_gc
> *init_vec
;
17551 if (placement
== NULL_TREE
)
17552 placement_vec
= NULL
;
17555 placement_vec
= make_tree_vector ();
17556 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17557 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17560 /* If there was an initializer in the original tree, but it
17561 instantiated to an empty list, then we should pass a
17562 non-NULL empty vector to tell build_new that it was an
17563 empty initializer() rather than no initializer. This can
17564 only happen when the initializer is a pack expansion whose
17565 parameter packs are of length zero. */
17566 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17570 init_vec
= make_tree_vector ();
17571 if (init
== void_node
)
17572 gcc_assert (init_vec
!= NULL
);
17575 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17576 vec_safe_push (init_vec
, TREE_VALUE (init
));
17580 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17581 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17582 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17583 NEW_EXPR_USE_GLOBAL (t
),
17586 if (placement_vec
!= NULL
)
17587 release_tree_vector (placement_vec
);
17588 if (init_vec
!= NULL
)
17589 release_tree_vector (init_vec
);
17596 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17597 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17598 RETURN (delete_sanity (op0
, op1
,
17599 DELETE_EXPR_USE_VEC (t
),
17600 DELETE_EXPR_USE_GLOBAL (t
),
17604 case COMPOUND_EXPR
:
17606 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17607 complain
& ~tf_decltype
, in_decl
,
17608 /*function_p=*/false,
17609 integral_constant_expression_p
);
17610 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17612 RECUR (TREE_OPERAND (t
, 1)),
17613 complain
|decltype_flag
));
17619 vec
<tree
, va_gc
> *call_args
;
17620 unsigned int nargs
, i
;
17625 function
= CALL_EXPR_FN (t
);
17626 /* Internal function with no arguments. */
17627 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17630 /* When we parsed the expression, we determined whether or
17631 not Koenig lookup should be performed. */
17632 koenig_p
= KOENIG_LOOKUP_P (t
);
17633 if (function
== NULL_TREE
)
17636 qualified_p
= false;
17638 else if (TREE_CODE (function
) == SCOPE_REF
)
17640 qualified_p
= true;
17641 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17643 /*address_p=*/false);
17645 else if (koenig_p
&& identifier_p (function
))
17647 /* Do nothing; calling tsubst_copy_and_build on an identifier
17648 would incorrectly perform unqualified lookup again.
17650 Note that we can also have an IDENTIFIER_NODE if the earlier
17651 unqualified lookup found a member function; in that case
17652 koenig_p will be false and we do want to do the lookup
17653 again to find the instantiated member function.
17655 FIXME but doing that causes c++/15272, so we need to stop
17656 using IDENTIFIER_NODE in that situation. */
17657 qualified_p
= false;
17661 if (TREE_CODE (function
) == COMPONENT_REF
)
17663 tree op
= TREE_OPERAND (function
, 1);
17665 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17666 || (BASELINK_P (op
)
17667 && BASELINK_QUALIFIED_P (op
)));
17670 qualified_p
= false;
17672 if (TREE_CODE (function
) == ADDR_EXPR
17673 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17674 /* Avoid error about taking the address of a constructor. */
17675 function
= TREE_OPERAND (function
, 0);
17677 function
= tsubst_copy_and_build (function
, args
, complain
,
17680 integral_constant_expression_p
);
17682 if (BASELINK_P (function
))
17683 qualified_p
= true;
17686 nargs
= call_expr_nargs (t
);
17687 call_args
= make_tree_vector ();
17688 for (i
= 0; i
< nargs
; ++i
)
17690 tree arg
= CALL_EXPR_ARG (t
, i
);
17692 if (!PACK_EXPANSION_P (arg
))
17693 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17696 /* Expand the pack expansion and push each entry onto
17698 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17699 if (TREE_CODE (arg
) == TREE_VEC
)
17701 unsigned int len
, j
;
17703 len
= TREE_VEC_LENGTH (arg
);
17704 for (j
= 0; j
< len
; ++j
)
17706 tree value
= TREE_VEC_ELT (arg
, j
);
17707 if (value
!= NULL_TREE
)
17708 value
= convert_from_reference (value
);
17709 vec_safe_push (call_args
, value
);
17714 /* A partial substitution. Add one entry. */
17715 vec_safe_push (call_args
, arg
);
17720 /* We do not perform argument-dependent lookup if normal
17721 lookup finds a non-function, in accordance with the
17722 expected resolution of DR 218. */
17724 && ((is_overloaded_fn (function
)
17725 /* If lookup found a member function, the Koenig lookup is
17726 not appropriate, even if an unqualified-name was used
17727 to denote the function. */
17728 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17729 || identifier_p (function
))
17730 /* Only do this when substitution turns a dependent call
17731 into a non-dependent call. */
17732 && type_dependent_expression_p_push (t
)
17733 && !any_type_dependent_arguments_p (call_args
))
17734 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17736 if (function
!= NULL_TREE
17737 && identifier_p (function
)
17738 && !any_type_dependent_arguments_p (call_args
))
17740 if (koenig_p
&& (complain
& tf_warning_or_error
))
17742 /* For backwards compatibility and good diagnostics, try
17743 the unqualified lookup again if we aren't in SFINAE
17745 tree unq
= (tsubst_copy_and_build
17746 (function
, args
, complain
, in_decl
, true,
17747 integral_constant_expression_p
));
17748 if (unq
== error_mark_node
)
17750 release_tree_vector (call_args
);
17751 RETURN (error_mark_node
);
17754 if (unq
!= function
)
17756 /* In a lambda fn, we have to be careful to not
17757 introduce new this captures. Legacy code can't
17758 be using lambdas anyway, so it's ok to be
17760 bool in_lambda
= (current_class_type
17761 && LAMBDA_TYPE_P (current_class_type
));
17762 char const *const msg
17763 = G_("%qD was not declared in this scope, "
17764 "and no declarations were found by "
17765 "argument-dependent lookup at the point "
17766 "of instantiation");
17770 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17773 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17779 if (INDIRECT_REF_P (fn
))
17780 fn
= TREE_OPERAND (fn
, 0);
17781 if (is_overloaded_fn (fn
))
17782 fn
= get_first_fn (fn
);
17785 /* Can't say anything more. */;
17786 else if (DECL_CLASS_SCOPE_P (fn
))
17788 location_t loc
= EXPR_LOC_OR_LOC (t
,
17791 "declarations in dependent base %qT are "
17792 "not found by unqualified lookup",
17793 DECL_CLASS_CONTEXT (fn
));
17794 if (current_class_ptr
)
17796 "use %<this->%D%> instead", function
);
17799 "use %<%T::%D%> instead",
17800 current_class_name
, function
);
17803 inform (DECL_SOURCE_LOCATION (fn
),
17804 "%qD declared here, later in the "
17805 "translation unit", fn
);
17808 release_tree_vector (call_args
);
17809 RETURN (error_mark_node
);
17816 if (identifier_p (function
))
17818 if (complain
& tf_error
)
17819 unqualified_name_lookup_error (function
);
17820 release_tree_vector (call_args
);
17821 RETURN (error_mark_node
);
17825 /* Remember that there was a reference to this entity. */
17826 if (function
!= NULL_TREE
17827 && DECL_P (function
)
17828 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17830 release_tree_vector (call_args
);
17831 RETURN (error_mark_node
);
17834 /* Put back tf_decltype for the actual call. */
17835 complain
|= decltype_flag
;
17837 if (function
== NULL_TREE
)
17838 switch (CALL_EXPR_IFN (t
))
17841 gcc_assert (nargs
== 1);
17842 if (vec_safe_length (call_args
) != 1)
17844 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17845 "wrong number of arguments to "
17846 "%<__builtin_launder%>");
17847 ret
= error_mark_node
;
17850 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17852 (*call_args
)[0], complain
);
17856 /* Unsupported internal function with arguments. */
17857 gcc_unreachable ();
17859 else if (TREE_CODE (function
) == OFFSET_REF
)
17860 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17862 else if (TREE_CODE (function
) == COMPONENT_REF
)
17864 tree instance
= TREE_OPERAND (function
, 0);
17865 tree fn
= TREE_OPERAND (function
, 1);
17867 if (processing_template_decl
17868 && (type_dependent_expression_p (instance
)
17869 || (!BASELINK_P (fn
)
17870 && TREE_CODE (fn
) != FIELD_DECL
)
17871 || type_dependent_expression_p (fn
)
17872 || any_type_dependent_arguments_p (call_args
)))
17873 ret
= build_min_nt_call_vec (function
, call_args
);
17874 else if (!BASELINK_P (fn
))
17875 ret
= finish_call_expr (function
, &call_args
,
17876 /*disallow_virtual=*/false,
17877 /*koenig_p=*/false,
17880 ret
= (build_new_method_call
17882 &call_args
, NULL_TREE
,
17883 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17888 ret
= finish_call_expr (function
, &call_args
,
17889 /*disallow_virtual=*/qualified_p
,
17893 release_tree_vector (call_args
);
17895 if (ret
!= error_mark_node
)
17897 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17898 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17899 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17900 bool thk
= CALL_FROM_THUNK_P (t
);
17901 if (op
|| ord
|| rev
|| thk
)
17903 function
= extract_call_expr (ret
);
17904 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17905 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17906 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17909 if (TREE_CODE (function
) == CALL_EXPR
)
17910 CALL_FROM_THUNK_P (function
) = true;
17912 AGGR_INIT_FROM_THUNK_P (function
) = true;
17913 /* The thunk location is not interesting. */
17914 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17924 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17925 tree folded_cond
= fold_non_dependent_expr (cond
);
17928 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17930 if (integer_zerop (folded_cond
))
17932 ++c_inhibit_evaluation_warnings
;
17933 exp1
= RECUR (TREE_OPERAND (t
, 1));
17934 --c_inhibit_evaluation_warnings
;
17935 exp2
= RECUR (TREE_OPERAND (t
, 2));
17939 exp1
= RECUR (TREE_OPERAND (t
, 1));
17940 ++c_inhibit_evaluation_warnings
;
17941 exp2
= RECUR (TREE_OPERAND (t
, 2));
17942 --c_inhibit_evaluation_warnings
;
17944 cond
= folded_cond
;
17948 exp1
= RECUR (TREE_OPERAND (t
, 1));
17949 exp2
= RECUR (TREE_OPERAND (t
, 2));
17952 warning_sentinel
s(warn_duplicated_branches
);
17953 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17954 cond
, exp1
, exp2
, complain
));
17957 case PSEUDO_DTOR_EXPR
:
17959 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17960 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17961 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17962 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17968 tree purpose
, value
, chain
;
17970 if (t
== void_list_node
)
17973 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17974 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17976 /* We have pack expansions, so expand those and
17977 create a new list out of it. */
17978 tree purposevec
= NULL_TREE
;
17979 tree valuevec
= NULL_TREE
;
17983 /* Expand the argument expressions. */
17984 if (TREE_PURPOSE (t
))
17985 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17986 complain
, in_decl
);
17987 if (TREE_VALUE (t
))
17988 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17989 complain
, in_decl
);
17991 /* Build the rest of the list. */
17992 chain
= TREE_CHAIN (t
);
17993 if (chain
&& chain
!= void_type_node
)
17994 chain
= RECUR (chain
);
17996 /* Determine the number of arguments. */
17997 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17999 len
= TREE_VEC_LENGTH (purposevec
);
18000 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18002 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18003 len
= TREE_VEC_LENGTH (valuevec
);
18006 /* Since we only performed a partial substitution into
18007 the argument pack, we only RETURN (a single list
18009 if (purposevec
== TREE_PURPOSE (t
)
18010 && valuevec
== TREE_VALUE (t
)
18011 && chain
== TREE_CHAIN (t
))
18014 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18017 /* Convert the argument vectors into a TREE_LIST */
18021 /* Grab the Ith values. */
18023 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18026 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18029 /* Build the list (backwards). */
18030 chain
= tree_cons (purpose
, value
, chain
);
18036 purpose
= TREE_PURPOSE (t
);
18038 purpose
= RECUR (purpose
);
18039 value
= TREE_VALUE (t
);
18041 value
= RECUR (value
);
18042 chain
= TREE_CHAIN (t
);
18043 if (chain
&& chain
!= void_type_node
)
18044 chain
= RECUR (chain
);
18045 if (purpose
== TREE_PURPOSE (t
)
18046 && value
== TREE_VALUE (t
)
18047 && chain
== TREE_CHAIN (t
))
18049 RETURN (tree_cons (purpose
, value
, chain
));
18052 case COMPONENT_REF
:
18059 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18060 args
, complain
, in_decl
);
18061 /* Remember that there was a reference to this entity. */
18062 if (DECL_P (object
)
18063 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18064 RETURN (error_mark_node
);
18065 object_type
= TREE_TYPE (object
);
18067 member
= TREE_OPERAND (t
, 1);
18068 if (BASELINK_P (member
))
18069 member
= tsubst_baselink (member
,
18070 non_reference (TREE_TYPE (object
)),
18071 args
, complain
, in_decl
);
18073 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18074 if (member
== error_mark_node
)
18075 RETURN (error_mark_node
);
18077 if (TREE_CODE (member
) == FIELD_DECL
)
18079 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18080 if (TREE_CODE (r
) == COMPONENT_REF
)
18081 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18084 else if (type_dependent_expression_p (object
))
18085 /* We can't do much here. */;
18086 else if (!CLASS_TYPE_P (object_type
))
18088 if (scalarish_type_p (object_type
))
18090 tree s
= NULL_TREE
;
18091 tree dtor
= member
;
18093 if (TREE_CODE (dtor
) == SCOPE_REF
)
18095 s
= TREE_OPERAND (dtor
, 0);
18096 dtor
= TREE_OPERAND (dtor
, 1);
18098 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18100 dtor
= TREE_OPERAND (dtor
, 0);
18102 RETURN (finish_pseudo_destructor_expr
18103 (object
, s
, dtor
, input_location
));
18107 else if (TREE_CODE (member
) == SCOPE_REF
18108 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18110 /* Lookup the template functions now that we know what the
18112 tree scope
= TREE_OPERAND (member
, 0);
18113 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18114 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18115 member
= lookup_qualified_name (scope
, tmpl
,
18116 /*is_type_p=*/false,
18117 /*complain=*/false);
18118 if (BASELINK_P (member
))
18120 BASELINK_FUNCTIONS (member
)
18121 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18123 member
= (adjust_result_of_qualified_name_lookup
18124 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18129 qualified_name_lookup_error (scope
, tmpl
, member
,
18131 RETURN (error_mark_node
);
18134 else if (TREE_CODE (member
) == SCOPE_REF
18135 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18136 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18138 if (complain
& tf_error
)
18140 if (TYPE_P (TREE_OPERAND (member
, 0)))
18141 error ("%qT is not a class or namespace",
18142 TREE_OPERAND (member
, 0));
18144 error ("%qD is not a class or namespace",
18145 TREE_OPERAND (member
, 0));
18147 RETURN (error_mark_node
);
18150 r
= finish_class_member_access_expr (object
, member
,
18151 /*template_p=*/false,
18153 if (TREE_CODE (r
) == COMPONENT_REF
)
18154 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18159 RETURN (build_throw
18160 (RECUR (TREE_OPERAND (t
, 0))));
18164 vec
<constructor_elt
, va_gc
> *n
;
18165 constructor_elt
*ce
;
18166 unsigned HOST_WIDE_INT idx
;
18167 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18168 bool process_index_p
;
18170 bool need_copy_p
= false;
18173 if (type
== error_mark_node
)
18174 RETURN (error_mark_node
);
18176 /* We do not want to process the index of aggregate
18177 initializers as they are identifier nodes which will be
18178 looked up by digest_init. */
18179 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18181 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18182 newlen
= vec_safe_length (n
);
18183 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18185 if (ce
->index
&& process_index_p
18186 /* An identifier index is looked up in the type
18187 being initialized, not the current scope. */
18188 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18189 ce
->index
= RECUR (ce
->index
);
18191 if (PACK_EXPANSION_P (ce
->value
))
18193 /* Substitute into the pack expansion. */
18194 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18197 if (ce
->value
== error_mark_node
18198 || PACK_EXPANSION_P (ce
->value
))
18200 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18201 /* Just move the argument into place. */
18202 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18205 /* Update the length of the final CONSTRUCTOR
18206 arguments vector, and note that we will need to
18208 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18209 need_copy_p
= true;
18213 ce
->value
= RECUR (ce
->value
);
18218 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18220 vec_alloc (n
, newlen
);
18221 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18223 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18225 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18226 for (i
= 0; i
< len
; ++i
)
18227 CONSTRUCTOR_APPEND_ELT (n
, 0,
18228 TREE_VEC_ELT (ce
->value
, i
));
18231 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18235 r
= build_constructor (init_list_type_node
, n
);
18236 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18238 if (TREE_HAS_CONSTRUCTOR (t
))
18240 fcl_t cl
= fcl_functional
;
18241 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18243 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18246 TREE_TYPE (r
) = type
;
18252 tree operand_0
= TREE_OPERAND (t
, 0);
18253 if (TYPE_P (operand_0
))
18255 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18256 RETURN (get_typeid (operand_0
, complain
));
18260 operand_0
= RECUR (operand_0
);
18261 RETURN (build_typeid (operand_0
, complain
));
18268 else if (DECL_PACK_P (t
))
18270 /* We don't build decls for an instantiation of a
18271 variadic capture proxy, we instantiate the elements
18273 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
18274 return RECUR (DECL_VALUE_EXPR (t
));
18280 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18281 /* ??? We're doing a subset of finish_id_expression here. */
18283 && !processing_template_decl
18284 && !cp_unevaluated_operand
18285 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18286 && CP_DECL_THREAD_LOCAL_P (r
))
18288 if (tree wrap
= get_tls_wrapper_fn (r
))
18289 /* Replace an evaluated use of the thread_local variable with
18290 a call to its wrapper. */
18291 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18293 else if (outer_automatic_var_p (r
))
18294 r
= process_outer_var_ref (r
, complain
);
18296 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18297 /* If the original type was a reference, we'll be wrapped in
18298 the appropriate INDIRECT_REF. */
18299 r
= convert_from_reference (r
);
18305 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18306 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18307 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18310 case OFFSETOF_EXPR
:
18313 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18314 in_decl
, /*function_p=*/false,
18315 /*integral_constant_expression_p=*/false);
18316 RETURN (finish_offsetof (object_ptr
,
18317 RECUR (TREE_OPERAND (t
, 0)),
18318 EXPR_LOCATION (t
)));
18321 case ADDRESSOF_EXPR
:
18322 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18323 RECUR (TREE_OPERAND (t
, 0)), complain
));
18327 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18328 complain
, in_decl
);
18330 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18331 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18332 type2
= RECUR (type2
);
18334 type2
= tsubst (type2
, args
, complain
, in_decl
);
18336 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18341 tree old_stmt_expr
= cur_stmt_expr
;
18342 tree stmt_expr
= begin_stmt_expr ();
18344 cur_stmt_expr
= stmt_expr
;
18345 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18346 integral_constant_expression_p
);
18347 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18348 cur_stmt_expr
= old_stmt_expr
;
18350 /* If the resulting list of expression statement is empty,
18351 fold it further into void_node. */
18352 if (empty_expr_stmt_p (stmt_expr
))
18353 stmt_expr
= void_node
;
18355 RETURN (stmt_expr
);
18360 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18362 RETURN (build_lambda_object (r
));
18366 /* We can get here for a constant initializer of non-dependent type.
18367 FIXME stop folding in cp_parser_initializer_clause. */
18369 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18374 case TRANSACTION_EXPR
:
18375 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18376 integral_constant_expression_p
));
18379 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18381 case VEC_PERM_EXPR
:
18383 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18384 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18385 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18386 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18390 case REQUIRES_EXPR
:
18391 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18393 case NON_LVALUE_EXPR
:
18394 case VIEW_CONVERT_EXPR
:
18395 /* We should only see these for location wrapper nodes, or within
18396 instantiate_non_dependent_expr (when args is NULL_TREE). */
18397 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18398 if (location_wrapper_p (t
))
18399 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18400 EXPR_LOCATION (t
)));
18404 /* Handle Objective-C++ constructs, if appropriate. */
18407 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18408 in_decl
, /*function_p=*/false);
18412 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18418 input_location
= loc
;
18422 /* Verify that the instantiated ARGS are valid. For type arguments,
18423 make sure that the type's linkage is ok. For non-type arguments,
18424 make sure they are constants if they are integral or enumerations.
18425 Emit an error under control of COMPLAIN, and return TRUE on error. */
18428 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18430 if (dependent_template_arg_p (t
))
18432 if (ARGUMENT_PACK_P (t
))
18434 tree vec
= ARGUMENT_PACK_ARGS (t
);
18435 int len
= TREE_VEC_LENGTH (vec
);
18436 bool result
= false;
18439 for (i
= 0; i
< len
; ++i
)
18440 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18444 else if (TYPE_P (t
))
18446 /* [basic.link]: A name with no linkage (notably, the name
18447 of a class or enumeration declared in a local scope)
18448 shall not be used to declare an entity with linkage.
18449 This implies that names with no linkage cannot be used as
18452 DR 757 relaxes this restriction for C++0x. */
18453 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18454 : no_linkage_check (t
, /*relaxed_p=*/false));
18458 /* DR 488 makes use of a type with no linkage cause
18459 type deduction to fail. */
18460 if (complain
& tf_error
)
18462 if (TYPE_UNNAMED_P (nt
))
18463 error ("%qT is/uses unnamed type", t
);
18465 error ("template argument for %qD uses local type %qT",
18470 /* In order to avoid all sorts of complications, we do not
18471 allow variably-modified types as template arguments. */
18472 else if (variably_modified_type_p (t
, NULL_TREE
))
18474 if (complain
& tf_error
)
18475 error ("%qT is a variably modified type", t
);
18479 /* Class template and alias template arguments should be OK. */
18480 else if (DECL_TYPE_TEMPLATE_P (t
))
18482 /* A non-type argument of integral or enumerated type must be a
18484 else if (TREE_TYPE (t
)
18485 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18486 && !REFERENCE_REF_P (t
)
18487 && !TREE_CONSTANT (t
))
18489 if (complain
& tf_error
)
18490 error ("integral expression %qE is not constant", t
);
18497 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18499 int ix
, len
= DECL_NTPARMS (tmpl
);
18500 bool result
= false;
18502 for (ix
= 0; ix
!= len
; ix
++)
18504 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18507 if (result
&& (complain
& tf_error
))
18508 error (" trying to instantiate %qD", tmpl
);
18512 /* We're out of SFINAE context now, so generate diagnostics for the access
18513 errors we saw earlier when instantiating D from TMPL and ARGS. */
18516 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18518 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18519 tree type
= TREE_TYPE (pattern
);
18520 location_t loc
= input_location
;
18522 push_access_scope (d
);
18523 push_deferring_access_checks (dk_no_deferred
);
18524 input_location
= DECL_SOURCE_LOCATION (pattern
);
18525 tsubst (type
, args
, tf_warning_or_error
, d
);
18526 input_location
= loc
;
18527 pop_deferring_access_checks ();
18528 pop_access_scope (d
);
18531 /* Instantiate the indicated variable, function, or alias template TMPL with
18532 the template arguments in TARG_PTR. */
18535 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18537 tree targ_ptr
= orig_args
;
18541 bool access_ok
= true;
18543 if (tmpl
== error_mark_node
)
18544 return error_mark_node
;
18546 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18548 /* If this function is a clone, handle it specially. */
18549 if (DECL_CLONED_FUNCTION_P (tmpl
))
18554 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18555 DECL_CLONED_FUNCTION. */
18556 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18557 targ_ptr
, complain
);
18558 if (spec
== error_mark_node
)
18559 return error_mark_node
;
18561 /* Look for the clone. */
18562 FOR_EACH_CLONE (clone
, spec
)
18563 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18565 /* We should always have found the clone by now. */
18566 gcc_unreachable ();
18570 if (targ_ptr
== error_mark_node
)
18571 return error_mark_node
;
18573 /* Check to see if we already have this specialization. */
18574 gen_tmpl
= most_general_template (tmpl
);
18575 if (TMPL_ARGS_DEPTH (targ_ptr
)
18576 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18577 /* targ_ptr only has the innermost template args, so add the outer ones
18578 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18579 the case of a non-dependent call within a template definition). */
18580 targ_ptr
= (add_outermost_template_args
18581 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18584 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18585 but it doesn't seem to be on the hot path. */
18586 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18588 gcc_assert (tmpl
== gen_tmpl
18589 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18591 || fndecl
== NULL_TREE
);
18593 if (spec
!= NULL_TREE
)
18595 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18597 if (complain
& tf_error
)
18598 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18599 return error_mark_node
;
18604 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18606 return error_mark_node
;
18608 /* We are building a FUNCTION_DECL, during which the access of its
18609 parameters and return types have to be checked. However this
18610 FUNCTION_DECL which is the desired context for access checking
18611 is not built yet. We solve this chicken-and-egg problem by
18612 deferring all checks until we have the FUNCTION_DECL. */
18613 push_deferring_access_checks (dk_deferred
);
18615 /* Instantiation of the function happens in the context of the function
18616 template, not the context of the overload resolution we're doing. */
18617 push_to_top_level ();
18618 /* If there are dependent arguments, e.g. because we're doing partial
18619 ordering, make sure processing_template_decl stays set. */
18620 if (uses_template_parms (targ_ptr
))
18621 ++processing_template_decl
;
18622 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18624 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18625 complain
, gen_tmpl
, true);
18626 push_nested_class (ctx
);
18629 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18631 fndecl
= NULL_TREE
;
18632 if (VAR_P (pattern
))
18634 /* We need to determine if we're using a partial or explicit
18635 specialization now, because the type of the variable could be
18637 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18638 tree elt
= most_specialized_partial_spec (tid
, complain
);
18639 if (elt
== error_mark_node
)
18640 pattern
= error_mark_node
;
18643 tree partial_tmpl
= TREE_VALUE (elt
);
18644 tree partial_args
= TREE_PURPOSE (elt
);
18645 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18646 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18650 /* Substitute template parameters to obtain the specialization. */
18651 if (fndecl
== NULL_TREE
)
18652 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18653 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18654 pop_nested_class ();
18655 pop_from_top_level ();
18657 if (fndecl
== error_mark_node
)
18659 pop_deferring_access_checks ();
18660 return error_mark_node
;
18663 /* The DECL_TI_TEMPLATE should always be the immediate parent
18664 template, not the most general template. */
18665 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18666 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18668 /* Now we know the specialization, compute access previously
18669 deferred. Do no access control for inheriting constructors,
18670 as we already checked access for the inherited constructor. */
18671 if (!(flag_new_inheriting_ctors
18672 && DECL_INHERITED_CTOR (fndecl
)))
18674 push_access_scope (fndecl
);
18675 if (!perform_deferred_access_checks (complain
))
18677 pop_access_scope (fndecl
);
18679 pop_deferring_access_checks ();
18681 /* If we've just instantiated the main entry point for a function,
18682 instantiate all the alternate entry points as well. We do this
18683 by cloning the instantiation of the main entry point, not by
18684 instantiating the template clones. */
18685 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18686 clone_function_decl (fndecl
, /*update_methods=*/false);
18690 if (!(complain
& tf_error
))
18692 /* Remember to reinstantiate when we're out of SFINAE so the user
18693 can see the errors. */
18694 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18696 return error_mark_node
;
18701 /* Wrapper for instantiate_template_1. */
18704 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18707 timevar_push (TV_TEMPLATE_INST
);
18708 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18709 timevar_pop (TV_TEMPLATE_INST
);
18713 /* Instantiate the alias template TMPL with ARGS. Also push a template
18714 instantiation level, which instantiate_template doesn't do because
18715 functions and variables have sufficient context established by the
18719 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18721 struct pending_template
*old_last_pend
= last_pending_template
;
18722 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18723 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18724 return error_mark_node
;
18725 tree tinst
= build_tree_list (tmpl
, args
);
18726 if (!push_tinst_level (tinst
))
18729 return error_mark_node
;
18733 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18734 args
, tmpl
, complain
,
18735 /*require_all_args=*/true,
18736 /*use_default_args=*/true);
18738 tree r
= instantiate_template (tmpl
, args
, complain
);
18739 pop_tinst_level ();
18740 /* We can't free this if a pending_template entry or last_error_tinst_level
18741 is pointing at it. */
18742 if (last_pending_template
== old_last_pend
18743 && last_error_tinst_level
== old_error_tinst
)
18749 /* PARM is a template parameter pack for FN. Returns true iff
18750 PARM is used in a deducible way in the argument list of FN. */
18753 pack_deducible_p (tree parm
, tree fn
)
18755 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18756 for (; t
; t
= TREE_CHAIN (t
))
18758 tree type
= TREE_VALUE (t
);
18760 if (!PACK_EXPANSION_P (type
))
18762 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18763 packs
; packs
= TREE_CHAIN (packs
))
18764 if (template_args_equal (TREE_VALUE (packs
), parm
))
18766 /* The template parameter pack is used in a function parameter
18767 pack. If this is the end of the parameter list, the
18768 template parameter pack is deducible. */
18769 if (TREE_CHAIN (t
) == void_list_node
)
18772 /* Otherwise, not. Well, it could be deduced from
18773 a non-pack parameter, but doing so would end up with
18774 a deduction mismatch, so don't bother. */
18778 /* The template parameter pack isn't used in any function parameter
18779 packs, but it might be used deeper, e.g. tuple<Args...>. */
18783 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18784 NARGS elements of the arguments that are being used when calling
18785 it. TARGS is a vector into which the deduced template arguments
18788 Returns either a FUNCTION_DECL for the matching specialization of FN or
18789 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18790 true, diagnostics will be printed to explain why it failed.
18792 If FN is a conversion operator, or we are trying to produce a specific
18793 specialization, RETURN_TYPE is the return type desired.
18795 The EXPLICIT_TARGS are explicit template arguments provided via a
18798 The parameter STRICT is one of:
18801 We are deducing arguments for a function call, as in
18802 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18803 deducing arguments for a call to the result of a conversion
18804 function template, as in [over.call.object].
18807 We are deducing arguments for a conversion function, as in
18808 [temp.deduct.conv].
18811 We are deducing arguments when doing an explicit instantiation
18812 as in [temp.explicit], when determining an explicit specialization
18813 as in [temp.expl.spec], or when taking the address of a function
18814 template, as in [temp.deduct.funcaddr]. */
18817 fn_type_unification (tree fn
,
18818 tree explicit_targs
,
18821 unsigned int nargs
,
18823 unification_kind_t strict
,
18830 tree decl
= NULL_TREE
;
18831 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18833 static int deduction_depth
;
18834 struct pending_template
*old_last_pend
= last_pending_template
;
18835 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18838 if (flag_new_inheriting_ctors
)
18839 fn
= strip_inheriting_ctors (fn
);
18841 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18843 tree r
= error_mark_node
;
18845 tree full_targs
= targs
;
18846 if (TMPL_ARGS_DEPTH (targs
)
18847 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18848 full_targs
= (add_outermost_template_args
18849 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18853 complain
|= tf_decltype
;
18855 /* In C++0x, it's possible to have a function template whose type depends
18856 on itself recursively. This is most obvious with decltype, but can also
18857 occur with enumeration scope (c++/48969). So we need to catch infinite
18858 recursion and reject the substitution at deduction time; this function
18859 will return error_mark_node for any repeated substitution.
18861 This also catches excessive recursion such as when f<N> depends on
18862 f<N-1> across all integers, and returns error_mark_node for all the
18863 substitutions back up to the initial one.
18865 This is, of course, not reentrant. */
18866 if (excessive_deduction_depth
)
18867 return error_mark_node
;
18868 tinst
= build_tree_list (fn
, NULL_TREE
);
18871 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18873 fntype
= TREE_TYPE (fn
);
18874 if (explicit_targs
)
18878 The specified template arguments must match the template
18879 parameters in kind (i.e., type, nontype, template), and there
18880 must not be more arguments than there are parameters;
18881 otherwise type deduction fails.
18883 Nontype arguments must match the types of the corresponding
18884 nontype template parameters, or must be convertible to the
18885 types of the corresponding nontype parameters as specified in
18886 _temp.arg.nontype_, otherwise type deduction fails.
18888 All references in the function type of the function template
18889 to the corresponding template parameters are replaced by the
18890 specified template argument values. If a substitution in a
18891 template parameter or in the function type of the function
18892 template results in an invalid type, type deduction fails. */
18893 int i
, len
= TREE_VEC_LENGTH (tparms
);
18894 location_t loc
= input_location
;
18895 bool incomplete
= false;
18897 if (explicit_targs
== error_mark_node
)
18900 if (TMPL_ARGS_DEPTH (explicit_targs
)
18901 < TMPL_ARGS_DEPTH (full_targs
))
18902 explicit_targs
= add_outermost_template_args (full_targs
,
18905 /* Adjust any explicit template arguments before entering the
18906 substitution context. */
18908 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18910 /*require_all_args=*/false,
18911 /*use_default_args=*/false));
18912 if (explicit_targs
== error_mark_node
)
18915 /* Substitute the explicit args into the function type. This is
18916 necessary so that, for instance, explicitly declared function
18917 arguments can match null pointed constants. If we were given
18918 an incomplete set of explicit args, we must not do semantic
18919 processing during substitution as we could create partial
18921 for (i
= 0; i
< len
; i
++)
18923 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18924 bool parameter_pack
= false;
18925 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18927 /* Dig out the actual parm. */
18928 if (TREE_CODE (parm
) == TYPE_DECL
18929 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18931 parm
= TREE_TYPE (parm
);
18932 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18934 else if (TREE_CODE (parm
) == PARM_DECL
)
18936 parm
= DECL_INITIAL (parm
);
18937 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18940 if (!parameter_pack
&& targ
== NULL_TREE
)
18941 /* No explicit argument for this template parameter. */
18944 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18946 /* Mark the argument pack as "incomplete". We could
18947 still deduce more arguments during unification.
18948 We remove this mark in type_unification_real. */
18951 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18952 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18953 = ARGUMENT_PACK_ARGS (targ
);
18956 /* We have some incomplete argument packs. */
18961 TREE_VALUE (tinst
) = explicit_targs
;
18962 if (!push_tinst_level (tinst
))
18964 excessive_deduction_depth
= true;
18967 processing_template_decl
+= incomplete
;
18968 input_location
= DECL_SOURCE_LOCATION (fn
);
18969 /* Ignore any access checks; we'll see them again in
18970 instantiate_template and they might have the wrong
18971 access path at this point. */
18972 push_deferring_access_checks (dk_deferred
);
18973 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18974 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18975 pop_deferring_access_checks ();
18976 input_location
= loc
;
18977 processing_template_decl
-= incomplete
;
18978 pop_tinst_level ();
18980 if (fntype
== error_mark_node
)
18983 /* Place the explicitly specified arguments in TARGS. */
18984 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18985 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18986 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18989 /* Never do unification on the 'this' parameter. */
18990 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18992 if (return_type
&& strict
== DEDUCE_CALL
)
18994 /* We're deducing for a call to the result of a template conversion
18995 function. The parms we really want are in return_type. */
18996 if (POINTER_TYPE_P (return_type
))
18997 return_type
= TREE_TYPE (return_type
);
18998 parms
= TYPE_ARG_TYPES (return_type
);
19000 else if (return_type
)
19004 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19005 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19006 new_args
[0] = return_type
;
19007 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19012 /* We allow incomplete unification without an error message here
19013 because the standard doesn't seem to explicitly prohibit it. Our
19014 callers must be ready to deal with unification failures in any
19017 TREE_VALUE (tinst
) = targs
;
19018 /* If we aren't explaining yet, push tinst context so we can see where
19019 any errors (e.g. from class instantiations triggered by instantiation
19020 of default template arguments) come from. If we are explaining, this
19021 context is redundant. */
19022 if (!explain_p
&& !push_tinst_level (tinst
))
19024 excessive_deduction_depth
= true;
19028 /* type_unification_real will pass back any access checks from default
19029 template argument substitution. */
19030 vec
<deferred_access_check
, va_gc
> *checks
;
19033 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19034 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19035 strict
, flags
, &checks
, explain_p
);
19037 pop_tinst_level ();
19041 /* Now that we have bindings for all of the template arguments,
19042 ensure that the arguments deduced for the template template
19043 parameters have compatible template parameter lists. We cannot
19044 check this property before we have deduced all template
19045 arguments, because the template parameter types of a template
19046 template parameter might depend on prior template parameters
19047 deduced after the template template parameter. The following
19048 ill-formed example illustrates this issue:
19050 template<typename T, template<T> class C> void f(C<5>, T);
19052 template<int N> struct X {};
19055 f(X<5>(), 5l); // error: template argument deduction fails
19058 The template parameter list of 'C' depends on the template type
19059 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19060 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19061 time that we deduce 'C'. */
19062 if (!template_template_parm_bindings_ok_p
19063 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19065 unify_inconsistent_template_template_parameters (explain_p
);
19069 /* All is well so far. Now, check:
19073 When all template arguments have been deduced, all uses of
19074 template parameters in nondeduced contexts are replaced with
19075 the corresponding deduced argument values. If the
19076 substitution results in an invalid type, as described above,
19077 type deduction fails. */
19078 TREE_VALUE (tinst
) = targs
;
19079 if (!push_tinst_level (tinst
))
19081 excessive_deduction_depth
= true;
19085 /* Also collect access checks from the instantiation. */
19086 reopen_deferring_access_checks (checks
);
19088 decl
= instantiate_template (fn
, targs
, complain
);
19090 checks
= get_deferred_access_checks ();
19091 pop_deferring_access_checks ();
19093 pop_tinst_level ();
19095 if (decl
== error_mark_node
)
19098 /* Now perform any access checks encountered during substitution. */
19099 push_access_scope (decl
);
19100 ok
= perform_access_checks (checks
, complain
);
19101 pop_access_scope (decl
);
19105 /* If we're looking for an exact match, check that what we got
19106 is indeed an exact match. It might not be if some template
19107 parameters are used in non-deduced contexts. But don't check
19108 for an exact match if we have dependent template arguments;
19109 in that case we're doing partial ordering, and we already know
19110 that we have two candidates that will provide the actual type. */
19111 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19113 tree substed
= TREE_TYPE (decl
);
19117 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19119 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19120 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19121 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19123 unify_type_mismatch (explain_p
, args
[i
],
19124 TREE_VALUE (sarg
));
19129 /* After doing deduction with the inherited constructor, actually return an
19130 instantiation of the inheriting constructor. */
19132 decl
= instantiate_template (orig_fn
, targs
, complain
);
19138 if (excessive_deduction_depth
)
19140 if (deduction_depth
== 0)
19141 /* Reset once we're all the way out. */
19142 excessive_deduction_depth
= false;
19145 /* We can't free this if a pending_template entry or last_error_tinst_level
19146 is pointing at it. */
19147 if (last_pending_template
== old_last_pend
19148 && last_error_tinst_level
== old_error_tinst
)
19154 /* Adjust types before performing type deduction, as described in
19155 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19156 sections are symmetric. PARM is the type of a function parameter
19157 or the return type of the conversion function. ARG is the type of
19158 the argument passed to the call, or the type of the value
19159 initialized with the result of the conversion function.
19160 ARG_EXPR is the original argument expression, which may be null. */
19163 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19176 /* Swap PARM and ARG throughout the remainder of this
19177 function; the handling is precisely symmetric since PARM
19178 will initialize ARG rather than vice versa. */
19179 std::swap (parm
, arg
);
19183 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19184 too, but here handle it by stripping the reference from PARM
19185 rather than by adding it to ARG. */
19186 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19187 && TYPE_REF_IS_RVALUE (*parm
)
19188 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19189 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19190 && TREE_CODE (*arg
) == REFERENCE_TYPE
19191 && !TYPE_REF_IS_RVALUE (*arg
))
19192 *parm
= TREE_TYPE (*parm
);
19193 /* Nothing else to do in this case. */
19197 gcc_unreachable ();
19200 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19202 /* [temp.deduct.call]
19204 If P is not a reference type:
19206 --If A is an array type, the pointer type produced by the
19207 array-to-pointer standard conversion (_conv.array_) is
19208 used in place of A for type deduction; otherwise,
19210 --If A is a function type, the pointer type produced by
19211 the function-to-pointer standard conversion
19212 (_conv.func_) is used in place of A for type deduction;
19215 --If A is a cv-qualified type, the top level
19216 cv-qualifiers of A's type are ignored for type
19218 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19219 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19220 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19221 *arg
= build_pointer_type (*arg
);
19223 *arg
= TYPE_MAIN_VARIANT (*arg
);
19226 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19227 reference to a cv-unqualified template parameter that does not represent a
19228 template parameter of a class template (during class template argument
19229 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19230 an lvalue, the type "lvalue reference to A" is used in place of A for type
19232 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19233 && TYPE_REF_IS_RVALUE (*parm
)
19234 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19235 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19236 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19237 && (arg_expr
? lvalue_p (arg_expr
)
19238 /* try_one_overload doesn't provide an arg_expr, but
19239 functions are always lvalues. */
19240 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19241 *arg
= build_reference_type (*arg
);
19243 /* [temp.deduct.call]
19245 If P is a cv-qualified type, the top level cv-qualifiers
19246 of P's type are ignored for type deduction. If P is a
19247 reference type, the type referred to by P is used for
19249 *parm
= TYPE_MAIN_VARIANT (*parm
);
19250 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19252 *parm
= TREE_TYPE (*parm
);
19253 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19256 /* DR 322. For conversion deduction, remove a reference type on parm
19257 too (which has been swapped into ARG). */
19258 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19259 *arg
= TREE_TYPE (*arg
);
19264 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19265 template which does contain any deducible template parameters; check if
19266 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19267 unify_one_argument. */
19270 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19271 int flags
, bool explain_p
)
19276 type
= TREE_TYPE (arg
);
19280 if (same_type_p (parm
, type
))
19281 return unify_success (explain_p
);
19283 if (strict
== DEDUCE_CONV
)
19285 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19286 explain_p
? tf_warning_or_error
: tf_none
))
19287 return unify_success (explain_p
);
19289 else if (strict
!= DEDUCE_EXACT
)
19291 if (can_convert_arg (parm
, type
,
19292 TYPE_P (arg
) ? NULL_TREE
: arg
,
19293 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19294 return unify_success (explain_p
);
19297 if (strict
== DEDUCE_EXACT
)
19298 return unify_type_mismatch (explain_p
, parm
, arg
);
19300 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19303 static bool uses_deducible_template_parms (tree type
);
19305 /* Returns true iff the expression EXPR is one from which a template
19306 argument can be deduced. In other words, if it's an undecorated
19307 use of a template non-type parameter. */
19310 deducible_expression (tree expr
)
19312 /* Strip implicit conversions. */
19313 while (CONVERT_EXPR_P (expr
))
19314 expr
= TREE_OPERAND (expr
, 0);
19315 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19318 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19319 deducible way; that is, if it has a max value of <PARM> - 1. */
19322 deducible_array_bound (tree domain
)
19324 if (domain
== NULL_TREE
)
19327 tree max
= TYPE_MAX_VALUE (domain
);
19328 if (TREE_CODE (max
) != MINUS_EXPR
)
19331 return deducible_expression (TREE_OPERAND (max
, 0));
19334 /* Returns true iff the template arguments ARGS use a template parameter
19335 in a deducible way. */
19338 deducible_template_args (tree args
)
19340 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19343 tree elt
= TREE_VEC_ELT (args
, i
);
19344 if (ARGUMENT_PACK_P (elt
))
19345 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19348 if (PACK_EXPANSION_P (elt
))
19349 elt
= PACK_EXPANSION_PATTERN (elt
);
19350 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19352 else if (TYPE_P (elt
))
19353 deducible
= uses_deducible_template_parms (elt
);
19355 deducible
= deducible_expression (elt
);
19363 /* Returns true iff TYPE contains any deducible references to template
19364 parameters, as per 14.8.2.5. */
19367 uses_deducible_template_parms (tree type
)
19369 if (PACK_EXPANSION_P (type
))
19370 type
= PACK_EXPANSION_PATTERN (type
);
19377 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19378 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19384 if (POINTER_TYPE_P (type
))
19385 return uses_deducible_template_parms (TREE_TYPE (type
));
19387 /* T[integer-constant ]
19389 if (TREE_CODE (type
) == ARRAY_TYPE
)
19390 return (uses_deducible_template_parms (TREE_TYPE (type
))
19391 || deducible_array_bound (TYPE_DOMAIN (type
)));
19403 if (TYPE_PTRMEM_P (type
))
19404 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19405 || (uses_deducible_template_parms
19406 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19408 /* template-name <T> (where template-name refers to a class template)
19409 template-name <i> (where template-name refers to a class template) */
19410 if (CLASS_TYPE_P (type
)
19411 && CLASSTYPE_TEMPLATE_INFO (type
)
19412 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19413 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19414 (CLASSTYPE_TI_ARGS (type
)));
19419 if (TREE_CODE (type
) == FUNCTION_TYPE
19420 || TREE_CODE (type
) == METHOD_TYPE
)
19422 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19424 tree parm
= TYPE_ARG_TYPES (type
);
19425 if (TREE_CODE (type
) == METHOD_TYPE
)
19426 parm
= TREE_CHAIN (parm
);
19427 for (; parm
; parm
= TREE_CHAIN (parm
))
19428 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19435 /* Subroutine of type_unification_real and unify_pack_expansion to
19436 handle unification of a single P/A pair. Parameters are as
19437 for those functions. */
19440 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19441 int subr
, unification_kind_t strict
,
19444 tree arg_expr
= NULL_TREE
;
19447 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19448 return unify_invalid (explain_p
);
19449 if (arg
== unknown_type_node
)
19450 /* We can't deduce anything from this, but we might get all the
19451 template args from other function args. */
19452 return unify_success (explain_p
);
19454 /* Implicit conversions (Clause 4) will be performed on a function
19455 argument to convert it to the type of the corresponding function
19456 parameter if the parameter type contains no template-parameters that
19457 participate in template argument deduction. */
19458 if (strict
!= DEDUCE_EXACT
19459 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19460 /* For function parameters with no deducible template parameters,
19461 just return. We'll check non-dependent conversions later. */
19462 return unify_success (explain_p
);
19467 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19468 | UNIFY_ALLOW_MORE_CV_QUAL
19469 | UNIFY_ALLOW_DERIVED
);
19473 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19477 arg_strict
= UNIFY_ALLOW_NONE
;
19481 gcc_unreachable ();
19484 /* We only do these transformations if this is the top-level
19485 parameter_type_list in a call or declaration matching; in other
19486 situations (nested function declarators, template argument lists) we
19487 won't be comparing a type to an expression, and we don't do any type
19493 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19494 if (type_unknown_p (arg
))
19496 /* [temp.deduct.type] A template-argument can be
19497 deduced from a pointer to function or pointer
19498 to member function argument if the set of
19499 overloaded functions does not contain function
19500 templates and at most one of a set of
19501 overloaded functions provides a unique
19503 resolve_overloaded_unification (tparms
, targs
, parm
,
19505 arg_strict
, explain_p
);
19506 /* If a unique match was not found, this is a
19507 non-deduced context, so we still succeed. */
19508 return unify_success (explain_p
);
19512 arg
= unlowered_expr_type (arg
);
19513 if (arg
== error_mark_node
)
19514 return unify_invalid (explain_p
);
19518 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19521 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19522 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19523 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19525 /* For deduction from an init-list we need the actual list. */
19526 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19528 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19531 /* for_each_template_parm callback that always returns 0. */
19534 zero_r (tree
, void *)
19539 /* for_each_template_parm any_fn callback to handle deduction of a template
19540 type argument from the type of an array bound. */
19543 array_deduction_r (tree t
, void *data
)
19545 tree_pair_p d
= (tree_pair_p
)data
;
19546 tree
&tparms
= d
->purpose
;
19547 tree
&targs
= d
->value
;
19549 if (TREE_CODE (t
) == ARRAY_TYPE
)
19550 if (tree dom
= TYPE_DOMAIN (t
))
19551 if (tree max
= TYPE_MAX_VALUE (dom
))
19553 if (TREE_CODE (max
) == MINUS_EXPR
)
19554 max
= TREE_OPERAND (max
, 0);
19555 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19556 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19557 UNIFY_ALLOW_NONE
, /*explain*/false);
19560 /* Keep walking. */
19564 /* Try to deduce any not-yet-deduced template type arguments from the type of
19565 an array bound. This is handled separately from unify because 14.8.2.5 says
19566 "The type of a type parameter is only deduced from an array bound if it is
19567 not otherwise deduced." */
19570 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19572 tree_pair_s data
= { tparms
, targs
};
19573 hash_set
<tree
> visited
;
19574 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19575 /*nondeduced*/false, array_deduction_r
);
19578 /* Most parms like fn_type_unification.
19580 If SUBR is 1, we're being called recursively (to unify the
19581 arguments of a function or method parameter of a function
19584 CHECKS is a pointer to a vector of access checks encountered while
19585 substituting default template arguments. */
19588 type_unification_real (tree tparms
,
19592 unsigned int xnargs
,
19594 unification_kind_t strict
,
19596 vec
<deferred_access_check
, va_gc
> **checks
,
19601 int ntparms
= TREE_VEC_LENGTH (tparms
);
19602 int saw_undeduced
= 0;
19605 unsigned int nargs
;
19608 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19609 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19610 gcc_assert (ntparms
> 0);
19612 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19614 /* Reset the number of non-defaulted template arguments contained
19616 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19624 while (parms
&& parms
!= void_list_node
19627 parm
= TREE_VALUE (parms
);
19629 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19630 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19631 /* For a function parameter pack that occurs at the end of the
19632 parameter-declaration-list, the type A of each remaining
19633 argument of the call is compared with the type P of the
19634 declarator-id of the function parameter pack. */
19637 parms
= TREE_CHAIN (parms
);
19639 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19640 /* For a function parameter pack that does not occur at the
19641 end of the parameter-declaration-list, the type of the
19642 parameter pack is a non-deduced context. */
19648 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19654 && parms
!= void_list_node
19655 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19657 /* Unify the remaining arguments with the pack expansion type. */
19659 tree parmvec
= make_tree_vec (1);
19661 /* Allocate a TREE_VEC and copy in all of the arguments */
19662 argvec
= make_tree_vec (nargs
- ia
);
19663 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19664 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19666 /* Copy the parameter into parmvec. */
19667 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19668 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19669 /*subr=*/subr
, explain_p
))
19672 /* Advance to the end of the list of parameters. */
19673 parms
= TREE_CHAIN (parms
);
19676 /* Fail if we've reached the end of the parm list, and more args
19677 are present, and the parm list isn't variadic. */
19678 if (ia
< nargs
&& parms
== void_list_node
)
19679 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19680 /* Fail if parms are left and they don't have default values and
19681 they aren't all deduced as empty packs (c++/57397). This is
19682 consistent with sufficient_parms_p. */
19683 if (parms
&& parms
!= void_list_node
19684 && TREE_PURPOSE (parms
) == NULL_TREE
)
19686 unsigned int count
= nargs
;
19691 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19694 p
= TREE_CHAIN (p
);
19696 while (p
&& p
!= void_list_node
);
19697 if (count
!= nargs
)
19698 return unify_too_few_arguments (explain_p
, ia
, count
,
19704 tsubst_flags_t complain
= (explain_p
19705 ? tf_warning_or_error
19707 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19709 for (i
= 0; i
< ntparms
; i
++)
19711 tree targ
= TREE_VEC_ELT (targs
, i
);
19712 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19714 /* Clear the "incomplete" flags on all argument packs now so that
19715 substituting them into later default arguments works. */
19716 if (targ
&& ARGUMENT_PACK_P (targ
))
19718 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19719 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19722 if (targ
|| tparm
== error_mark_node
)
19724 tparm
= TREE_VALUE (tparm
);
19726 if (TREE_CODE (tparm
) == TYPE_DECL
19727 && !tried_array_deduction
)
19729 try_array_deduction (tparms
, targs
, xparms
);
19730 tried_array_deduction
= true;
19731 if (TREE_VEC_ELT (targs
, i
))
19735 /* If this is an undeduced nontype parameter that depends on
19736 a type parameter, try another pass; its type may have been
19737 deduced from a later argument than the one from which
19738 this parameter can be deduced. */
19739 if (TREE_CODE (tparm
) == PARM_DECL
19740 && uses_template_parms (TREE_TYPE (tparm
))
19741 && saw_undeduced
< 2)
19747 /* Core issue #226 (C++0x) [temp.deduct]:
19749 If a template argument has not been deduced, its
19750 default template argument, if any, is used.
19752 When we are in C++98 mode, TREE_PURPOSE will either
19753 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19754 to explicitly check cxx_dialect here. */
19755 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19756 /* OK, there is a default argument. Wait until after the
19757 conversion check to do substitution. */
19760 /* If the type parameter is a parameter pack, then it will
19761 be deduced to an empty parameter pack. */
19762 if (template_parameter_pack_p (tparm
))
19766 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19768 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19769 TREE_CONSTANT (arg
) = 1;
19772 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19774 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19776 TREE_VEC_ELT (targs
, i
) = arg
;
19780 return unify_parameter_deduction_failure (explain_p
, tparm
);
19783 /* DR 1391: All parameters have args, now check non-dependent parms for
19785 if (saw_undeduced
< 2)
19786 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19787 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19789 parm
= TREE_VALUE (parms
);
19791 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19792 && (!TREE_CHAIN (parms
)
19793 || TREE_CHAIN (parms
) == void_list_node
))
19794 /* For a function parameter pack that occurs at the end of the
19795 parameter-declaration-list, the type A of each remaining
19796 argument of the call is compared with the type P of the
19797 declarator-id of the function parameter pack. */
19800 parms
= TREE_CHAIN (parms
);
19802 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19803 /* For a function parameter pack that does not occur at the
19804 end of the parameter-declaration-list, the type of the
19805 parameter pack is a non-deduced context. */
19811 if (uses_template_parms (parm
))
19813 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19818 /* Now substitute into the default template arguments. */
19819 for (i
= 0; i
< ntparms
; i
++)
19821 tree targ
= TREE_VEC_ELT (targs
, i
);
19822 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19824 if (targ
|| tparm
== error_mark_node
)
19826 tree parm
= TREE_VALUE (tparm
);
19828 if (TREE_CODE (parm
) == PARM_DECL
19829 && uses_template_parms (TREE_TYPE (parm
))
19830 && saw_undeduced
< 2)
19833 tree arg
= TREE_PURPOSE (tparm
);
19834 reopen_deferring_access_checks (*checks
);
19835 location_t save_loc
= input_location
;
19837 input_location
= DECL_SOURCE_LOCATION (parm
);
19838 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19839 if (!uses_template_parms (arg
))
19840 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19842 else if (saw_undeduced
< 2)
19845 arg
= error_mark_node
;
19846 input_location
= save_loc
;
19847 *checks
= get_deferred_access_checks ();
19848 pop_deferring_access_checks ();
19849 if (arg
== error_mark_node
)
19853 TREE_VEC_ELT (targs
, i
) = arg
;
19854 /* The position of the first default template argument,
19855 is also the number of non-defaulted arguments in TARGS.
19857 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19862 if (saw_undeduced
++ == 1)
19866 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19867 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19869 return unify_success (explain_p
);
19872 /* Subroutine of type_unification_real. Args are like the variables
19873 at the call site. ARG is an overloaded function (or template-id);
19874 we try deducing template args from each of the overloads, and if
19875 only one succeeds, we go with that. Modifies TARGS and returns
19876 true on success. */
19879 resolve_overloaded_unification (tree tparms
,
19883 unification_kind_t strict
,
19887 tree tempargs
= copy_node (targs
);
19889 tree goodfn
= NULL_TREE
;
19892 if (TREE_CODE (arg
) == ADDR_EXPR
)
19894 arg
= TREE_OPERAND (arg
, 0);
19900 if (TREE_CODE (arg
) == COMPONENT_REF
)
19901 /* Handle `&x' where `x' is some static or non-static member
19903 arg
= TREE_OPERAND (arg
, 1);
19905 if (TREE_CODE (arg
) == OFFSET_REF
)
19906 arg
= TREE_OPERAND (arg
, 1);
19908 /* Strip baselink information. */
19909 if (BASELINK_P (arg
))
19910 arg
= BASELINK_FUNCTIONS (arg
);
19912 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19914 /* If we got some explicit template args, we need to plug them into
19915 the affected templates before we try to unify, in case the
19916 explicit args will completely resolve the templates in question. */
19919 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19920 arg
= TREE_OPERAND (arg
, 0);
19922 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19925 tree subargs
, elem
;
19927 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19930 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19931 expl_subargs
, NULL_TREE
, tf_none
,
19932 /*require_all_args=*/true,
19933 /*use_default_args=*/true);
19934 if (subargs
!= error_mark_node
19935 && !any_dependent_template_arguments_p (subargs
))
19937 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19938 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19939 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19940 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19949 /* If no templates (or more than one) are fully resolved by the
19950 explicit arguments, this template-id is a non-deduced context; it
19951 could still be OK if we deduce all template arguments for the
19952 enclosing call through other arguments. */
19956 else if (TREE_CODE (arg
) != OVERLOAD
19957 && TREE_CODE (arg
) != FUNCTION_DECL
)
19958 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19959 -- but the deduction does not succeed because the expression is
19960 not just the function on its own. */
19963 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19966 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19967 strict
, sub_strict
, addr_p
, explain_p
)
19968 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19975 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19976 to function or pointer to member function argument if the set of
19977 overloaded functions does not contain function templates and at most
19978 one of a set of overloaded functions provides a unique match.
19980 So if we found multiple possibilities, we return success but don't
19981 deduce anything. */
19985 int i
= TREE_VEC_LENGTH (targs
);
19987 if (TREE_VEC_ELT (tempargs
, i
))
19989 tree old
= TREE_VEC_ELT (targs
, i
);
19990 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19991 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19992 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19993 /* Don't forget explicit template arguments in a pack. */
19994 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19995 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19996 TREE_VEC_ELT (targs
, i
) = new_
;
20005 /* Core DR 115: In contexts where deduction is done and fails, or in
20006 contexts where deduction is not done, if a template argument list is
20007 specified and it, along with any default template arguments, identifies
20008 a single function template specialization, then the template-id is an
20009 lvalue for the function template specialization. */
20012 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20014 tree expr
, offset
, baselink
;
20017 if (!type_unknown_p (orig_expr
))
20022 offset
= NULL_TREE
;
20023 baselink
= NULL_TREE
;
20025 if (TREE_CODE (expr
) == ADDR_EXPR
)
20027 expr
= TREE_OPERAND (expr
, 0);
20030 if (TREE_CODE (expr
) == OFFSET_REF
)
20033 expr
= TREE_OPERAND (expr
, 1);
20035 if (BASELINK_P (expr
))
20038 expr
= BASELINK_FUNCTIONS (expr
);
20041 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20044 tree goodfn
= NULL_TREE
;
20046 /* If we got some explicit template args, we need to plug them into
20047 the affected templates before we try to unify, in case the
20048 explicit args will completely resolve the templates in question. */
20050 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20051 tree arg
= TREE_OPERAND (expr
, 0);
20052 tree badfn
= NULL_TREE
;
20053 tree badargs
= NULL_TREE
;
20055 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20058 tree subargs
, elem
;
20060 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20063 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20064 expl_subargs
, NULL_TREE
, tf_none
,
20065 /*require_all_args=*/true,
20066 /*use_default_args=*/true);
20067 if (subargs
!= error_mark_node
20068 && !any_dependent_template_arguments_p (subargs
))
20070 elem
= instantiate_template (fn
, subargs
, tf_none
);
20071 if (elem
== error_mark_node
)
20076 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20085 mark_used (goodfn
);
20088 expr
= build_baselink (BASELINK_BINFO (baselink
),
20089 BASELINK_ACCESS_BINFO (baselink
),
20090 expr
, BASELINK_OPTYPE (baselink
));
20094 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20095 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20098 expr
= cp_build_addr_expr (expr
, complain
);
20101 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20102 /* There were no good options and at least one bad one, so let the
20103 user know what the problem is. */
20104 instantiate_template (badfn
, badargs
, complain
);
20109 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20110 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20111 different overloads deduce different arguments for a given parm.
20112 ADDR_P is true if the expression for which deduction is being
20113 performed was of the form "& fn" rather than simply "fn".
20115 Returns 1 on success. */
20118 try_one_overload (tree tparms
,
20123 unification_kind_t strict
,
20132 if (arg
== error_mark_node
)
20135 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20136 to function or pointer to member function argument if the set of
20137 overloaded functions does not contain function templates and at most
20138 one of a set of overloaded functions provides a unique match.
20140 So if this is a template, just return success. */
20142 if (uses_template_parms (arg
))
20145 if (TREE_CODE (arg
) == METHOD_TYPE
)
20146 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20148 arg
= build_pointer_type (arg
);
20150 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20152 /* We don't copy orig_targs for this because if we have already deduced
20153 some template args from previous args, unify would complain when we
20154 try to deduce a template parameter for the same argument, even though
20155 there isn't really a conflict. */
20156 nargs
= TREE_VEC_LENGTH (targs
);
20157 tempargs
= make_tree_vec (nargs
);
20159 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20162 /* First make sure we didn't deduce anything that conflicts with
20163 explicitly specified args. */
20164 for (i
= nargs
; i
--; )
20166 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20167 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20171 else if (uses_template_parms (elt
))
20172 /* Since we're unifying against ourselves, we will fill in
20173 template args used in the function parm list with our own
20174 template parms. Discard them. */
20175 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20176 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20178 /* Check that the argument at each index of the deduced argument pack
20179 is equivalent to the corresponding explicitly specified argument.
20180 We may have deduced more arguments than were explicitly specified,
20183 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20184 that's wrong if we deduce the same argument pack from multiple
20185 function arguments: it's only incomplete the first time. */
20187 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20188 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20190 if (TREE_VEC_LENGTH (deduced_pack
)
20191 < TREE_VEC_LENGTH (explicit_pack
))
20194 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20195 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20196 TREE_VEC_ELT (deduced_pack
, j
)))
20199 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20203 for (i
= nargs
; i
--; )
20205 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20208 TREE_VEC_ELT (targs
, i
) = elt
;
20214 /* PARM is a template class (perhaps with unbound template
20215 parameters). ARG is a fully instantiated type. If ARG can be
20216 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20217 TARGS are as for unify. */
20220 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20223 tree copy_of_targs
;
20225 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20227 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20228 /* Matches anything. */;
20229 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20230 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20233 /* We need to make a new template argument vector for the call to
20234 unify. If we used TARGS, we'd clutter it up with the result of
20235 the attempted unification, even if this class didn't work out.
20236 We also don't want to commit ourselves to all the unifications
20237 we've already done, since unification is supposed to be done on
20238 an argument-by-argument basis. In other words, consider the
20239 following pathological case:
20241 template <int I, int J, int K>
20244 template <int I, int J>
20245 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20247 template <int I, int J, int K>
20248 void f(S<I, J, K>, S<I, I, I>);
20257 Now, by the time we consider the unification involving `s2', we
20258 already know that we must have `f<0, 0, 0>'. But, even though
20259 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20260 because there are two ways to unify base classes of S<0, 1, 2>
20261 with S<I, I, I>. If we kept the already deduced knowledge, we
20262 would reject the possibility I=1. */
20263 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20265 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20267 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20272 /* If unification failed, we're done. */
20273 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20274 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20280 /* Given a template type PARM and a class type ARG, find the unique
20281 base type in ARG that is an instance of PARM. We do not examine
20282 ARG itself; only its base-classes. If there is not exactly one
20283 appropriate base class, return NULL_TREE. PARM may be the type of
20284 a partial specialization, as well as a plain template type. Used
20287 static enum template_base_result
20288 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20289 bool explain_p
, tree
*result
)
20291 tree rval
= NULL_TREE
;
20294 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20296 binfo
= TYPE_BINFO (complete_type (arg
));
20299 /* The type could not be completed. */
20300 *result
= NULL_TREE
;
20301 return tbr_incomplete_type
;
20304 /* Walk in inheritance graph order. The search order is not
20305 important, and this avoids multiple walks of virtual bases. */
20306 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20308 tree r
= try_class_unification (tparms
, targs
, parm
,
20309 BINFO_TYPE (binfo
), explain_p
);
20313 /* If there is more than one satisfactory baseclass, then:
20317 If they yield more than one possible deduced A, the type
20321 if (rval
&& !same_type_p (r
, rval
))
20323 *result
= NULL_TREE
;
20324 return tbr_ambiguous_baseclass
;
20332 return tbr_success
;
20335 /* Returns the level of DECL, which declares a template parameter. */
20338 template_decl_level (tree decl
)
20340 switch (TREE_CODE (decl
))
20343 case TEMPLATE_DECL
:
20344 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20347 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20350 gcc_unreachable ();
20355 /* Decide whether ARG can be unified with PARM, considering only the
20356 cv-qualifiers of each type, given STRICT as documented for unify.
20357 Returns nonzero iff the unification is OK on that basis. */
20360 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20362 int arg_quals
= cp_type_quals (arg
);
20363 int parm_quals
= cp_type_quals (parm
);
20365 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20366 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20368 /* Although a CVR qualifier is ignored when being applied to a
20369 substituted template parameter ([8.3.2]/1 for example), that
20370 does not allow us to unify "const T" with "int&" because both
20371 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20372 It is ok when we're allowing additional CV qualifiers
20373 at the outer level [14.8.2.1]/3,1st bullet. */
20374 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20375 || TREE_CODE (arg
) == FUNCTION_TYPE
20376 || TREE_CODE (arg
) == METHOD_TYPE
)
20377 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20380 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20381 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20385 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20386 && (arg_quals
& parm_quals
) != parm_quals
)
20389 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20390 && (parm_quals
& arg_quals
) != arg_quals
)
20396 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20398 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20400 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20401 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20402 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20404 *index
= TEMPLATE_TYPE_IDX (parm
);
20405 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20409 *index
= TEMPLATE_PARM_IDX (parm
);
20410 *level
= TEMPLATE_PARM_LEVEL (parm
);
20414 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20416 if (unify (TP, TA, P, A, S, EP)) \
20420 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20421 expansion at the end of PACKED_PARMS. Returns 0 if the type
20422 deduction succeeds, 1 otherwise. STRICT is the same as in
20423 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20424 function call argument list. We'll need to adjust the arguments to make them
20425 types. SUBR tells us if this is from a recursive call to
20426 type_unification_real, or for comparing two template argument
20430 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20431 tree packed_args
, unification_kind_t strict
,
20432 bool subr
, bool explain_p
)
20435 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20436 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20437 tree pack
, packs
= NULL_TREE
;
20438 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20440 /* Add in any args remembered from an earlier partial instantiation. */
20441 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20442 int levels
= TMPL_ARGS_DEPTH (targs
);
20444 packed_args
= expand_template_argument_pack (packed_args
);
20446 int len
= TREE_VEC_LENGTH (packed_args
);
20448 /* Determine the parameter packs we will be deducing from the
20449 pattern, and record their current deductions. */
20450 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20451 pack
; pack
= TREE_CHAIN (pack
))
20453 tree parm_pack
= TREE_VALUE (pack
);
20456 /* Determine the index and level of this parameter pack. */
20457 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20458 if (level
< levels
)
20461 /* Keep track of the parameter packs and their corresponding
20463 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20464 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20467 /* Loop through all of the arguments that have not yet been
20468 unified and unify each with the pattern. */
20469 for (i
= start
; i
< len
; i
++)
20472 bool any_explicit
= false;
20473 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20475 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20476 or the element of its argument pack at the current index if
20477 this argument was explicitly specified. */
20478 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20482 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20485 if (TREE_VALUE (pack
)
20486 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20487 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20489 any_explicit
= true;
20490 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20492 TMPL_ARG (targs
, level
, idx
) = arg
;
20495 /* If we had explicit template arguments, substitute them into the
20496 pattern before deduction. */
20499 /* Some arguments might still be unspecified or dependent. */
20501 ++processing_template_decl
;
20502 dependent
= any_dependent_template_arguments_p (targs
);
20504 --processing_template_decl
;
20505 parm
= tsubst (pattern
, targs
,
20506 explain_p
? tf_warning_or_error
: tf_none
,
20509 --processing_template_decl
;
20510 if (parm
== error_mark_node
)
20516 /* Unify the pattern with the current argument. */
20517 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20521 /* For each parameter pack, collect the deduced value. */
20522 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20525 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20527 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20528 TMPL_ARG (targs
, level
, idx
);
20532 /* Verify that the results of unification with the parameter packs
20533 produce results consistent with what we've seen before, and make
20534 the deduced argument packs available. */
20535 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20537 tree old_pack
= TREE_VALUE (pack
);
20538 tree new_args
= TREE_TYPE (pack
);
20539 int i
, len
= TREE_VEC_LENGTH (new_args
);
20541 bool nondeduced_p
= false;
20543 /* By default keep the original deduced argument pack.
20544 If necessary, more specific code is going to update the
20545 resulting deduced argument later down in this function. */
20546 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20547 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20549 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20550 actually deduce anything. */
20551 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20552 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20553 nondeduced_p
= true;
20557 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20559 /* If we had fewer function args than explicit template args,
20560 just use the explicits. */
20561 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20562 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20563 if (len
< explicit_len
)
20564 new_args
= explicit_args
;
20570 /* Build the deduced *_ARGUMENT_PACK. */
20571 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20573 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20574 TREE_CONSTANT (result
) = 1;
20577 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20579 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20581 /* Note the deduced argument packs for this parameter
20583 TMPL_ARG (targs
, level
, idx
) = result
;
20585 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20586 && (ARGUMENT_PACK_ARGS (old_pack
)
20587 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20589 /* We only had the explicitly-provided arguments before, but
20590 now we have a complete set of arguments. */
20591 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20593 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20594 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20595 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20599 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20600 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20602 if (!comp_template_args (old_args
, new_args
,
20603 &bad_old_arg
, &bad_new_arg
))
20604 /* Inconsistent unification of this parameter pack. */
20605 return unify_parameter_pack_inconsistent (explain_p
,
20611 return unify_success (explain_p
);
20614 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20615 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20616 parameters and return value are as for unify. */
20619 unify_array_domain (tree tparms
, tree targs
,
20620 tree parm_dom
, tree arg_dom
,
20628 /* Our representation of array types uses "N - 1" as the
20629 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20630 not an integer constant. We cannot unify arbitrarily
20631 complex expressions, so we eliminate the MINUS_EXPRs
20633 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20634 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20637 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20638 parm_max
= TREE_OPERAND (parm_max
, 0);
20640 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20641 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20644 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20645 trying to unify the type of a variable with the type
20646 of a template parameter. For example:
20648 template <unsigned int N>
20649 void f (char (&) [N]);
20656 Here, the type of the ARG will be "int [g(i)]", and
20657 may be a SAVE_EXPR, etc. */
20658 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20659 return unify_vla_arg (explain_p
, arg_dom
);
20660 arg_max
= TREE_OPERAND (arg_max
, 0);
20663 /* If only one of the bounds used a MINUS_EXPR, compensate
20664 by adding one to the other bound. */
20665 if (parm_cst
&& !arg_cst
)
20666 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20670 else if (arg_cst
&& !parm_cst
)
20671 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20676 return unify (tparms
, targs
, parm_max
, arg_max
,
20677 UNIFY_ALLOW_INTEGER
, explain_p
);
20680 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20682 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20687 if (PACK_EXPANSION_P (t
))
20688 t
= PACK_EXPANSION_PATTERN (t
);
20689 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20690 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20691 || DECL_TYPE_TEMPLATE_P (t
))
20693 else if (TYPE_P (t
))
20699 /* Deduce the value of template parameters. TPARMS is the (innermost)
20700 set of template parameters to a template. TARGS is the bindings
20701 for those template parameters, as determined thus far; TARGS may
20702 include template arguments for outer levels of template parameters
20703 as well. PARM is a parameter to a template function, or a
20704 subcomponent of that parameter; ARG is the corresponding argument.
20705 This function attempts to match PARM with ARG in a manner
20706 consistent with the existing assignments in TARGS. If more values
20707 are deduced, then TARGS is updated.
20709 Returns 0 if the type deduction succeeds, 1 otherwise. The
20710 parameter STRICT is a bitwise or of the following flags:
20713 Require an exact match between PARM and ARG.
20714 UNIFY_ALLOW_MORE_CV_QUAL:
20715 Allow the deduced ARG to be more cv-qualified (by qualification
20716 conversion) than ARG.
20717 UNIFY_ALLOW_LESS_CV_QUAL:
20718 Allow the deduced ARG to be less cv-qualified than ARG.
20719 UNIFY_ALLOW_DERIVED:
20720 Allow the deduced ARG to be a template base class of ARG,
20721 or a pointer to a template base class of the type pointed to by
20723 UNIFY_ALLOW_INTEGER:
20724 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20725 case for more information.
20726 UNIFY_ALLOW_OUTER_LEVEL:
20727 This is the outermost level of a deduction. Used to determine validity
20728 of qualification conversions. A valid qualification conversion must
20729 have const qualified pointers leading up to the inner type which
20730 requires additional CV quals, except at the outer level, where const
20731 is not required [conv.qual]. It would be normal to set this flag in
20732 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20733 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20734 This is the outermost level of a deduction, and PARM can be more CV
20735 qualified at this point.
20736 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20737 This is the outermost level of a deduction, and PARM can be less CV
20738 qualified at this point. */
20741 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20747 int strict_in
= strict
;
20748 tsubst_flags_t complain
= (explain_p
20749 ? tf_warning_or_error
20752 /* I don't think this will do the right thing with respect to types.
20753 But the only case I've seen it in so far has been array bounds, where
20754 signedness is the only information lost, and I think that will be
20756 while (CONVERT_EXPR_P (parm
))
20757 parm
= TREE_OPERAND (parm
, 0);
20759 if (arg
== error_mark_node
)
20760 return unify_invalid (explain_p
);
20761 if (arg
== unknown_type_node
20762 || arg
== init_list_type_node
)
20763 /* We can't deduce anything from this, but we might get all the
20764 template args from other function args. */
20765 return unify_success (explain_p
);
20767 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20768 return unify_success (explain_p
);
20770 /* If PARM uses template parameters, then we can't bail out here,
20771 even if ARG == PARM, since we won't record unifications for the
20772 template parameters. We might need them if we're trying to
20773 figure out which of two things is more specialized. */
20774 if (arg
== parm
&& !uses_template_parms (parm
))
20775 return unify_success (explain_p
);
20777 /* Handle init lists early, so the rest of the function can assume
20778 we're dealing with a type. */
20779 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20783 tree orig_parm
= parm
;
20785 /* Replace T with std::initializer_list<T> for deduction. */
20786 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20787 && flag_deduce_init_list
)
20788 parm
= listify (parm
);
20790 if (!is_std_init_list (parm
)
20791 && TREE_CODE (parm
) != ARRAY_TYPE
)
20792 /* We can only deduce from an initializer list argument if the
20793 parameter is std::initializer_list or an array; otherwise this
20794 is a non-deduced context. */
20795 return unify_success (explain_p
);
20797 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20798 elttype
= TREE_TYPE (parm
);
20801 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20802 /* Deduction is defined in terms of a single type, so just punt
20803 on the (bizarre) std::initializer_list<T...>. */
20804 if (PACK_EXPANSION_P (elttype
))
20805 return unify_success (explain_p
);
20808 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20810 int elt_strict
= strict
;
20812 if (elt
== error_mark_node
)
20813 return unify_invalid (explain_p
);
20815 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20817 tree type
= TREE_TYPE (elt
);
20818 if (type
== error_mark_node
)
20819 return unify_invalid (explain_p
);
20820 /* It should only be possible to get here for a call. */
20821 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20822 elt_strict
|= maybe_adjust_types_for_deduction
20823 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20827 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20831 if (TREE_CODE (parm
) == ARRAY_TYPE
20832 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20834 /* Also deduce from the length of the initializer list. */
20835 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20836 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20837 if (idx
== error_mark_node
)
20838 return unify_invalid (explain_p
);
20839 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20843 /* If the std::initializer_list<T> deduction worked, replace the
20844 deduced A with std::initializer_list<A>. */
20845 if (orig_parm
!= parm
)
20847 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20848 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20849 targ
= listify (targ
);
20850 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20852 return unify_success (explain_p
);
20855 /* If parm and arg aren't the same kind of thing (template, type, or
20856 expression), fail early. */
20857 if (pa_kind (parm
) != pa_kind (arg
))
20858 return unify_invalid (explain_p
);
20860 /* Immediately reject some pairs that won't unify because of
20861 cv-qualification mismatches. */
20862 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20864 /* It is the elements of the array which hold the cv quals of an array
20865 type, and the elements might be template type parms. We'll check
20866 when we recurse. */
20867 && TREE_CODE (arg
) != ARRAY_TYPE
20868 /* We check the cv-qualifiers when unifying with template type
20869 parameters below. We want to allow ARG `const T' to unify with
20870 PARM `T' for example, when computing which of two templates
20871 is more specialized, for example. */
20872 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20873 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20874 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20876 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20877 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20878 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20879 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20880 strict
&= ~UNIFY_ALLOW_DERIVED
;
20881 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20882 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20884 switch (TREE_CODE (parm
))
20886 case TYPENAME_TYPE
:
20888 case UNBOUND_CLASS_TEMPLATE
:
20889 /* In a type which contains a nested-name-specifier, template
20890 argument values cannot be deduced for template parameters used
20891 within the nested-name-specifier. */
20892 return unify_success (explain_p
);
20894 case TEMPLATE_TYPE_PARM
:
20895 case TEMPLATE_TEMPLATE_PARM
:
20896 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20897 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20898 if (error_operand_p (tparm
))
20899 return unify_invalid (explain_p
);
20901 if (TEMPLATE_TYPE_LEVEL (parm
)
20902 != template_decl_level (tparm
))
20903 /* The PARM is not one we're trying to unify. Just check
20904 to see if it matches ARG. */
20906 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20907 && (is_auto (parm
) ? is_auto (arg
)
20908 : same_type_p (parm
, arg
)))
20909 return unify_success (explain_p
);
20911 return unify_type_mismatch (explain_p
, parm
, arg
);
20913 idx
= TEMPLATE_TYPE_IDX (parm
);
20914 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20915 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20916 if (error_operand_p (tparm
))
20917 return unify_invalid (explain_p
);
20919 /* Check for mixed types and values. */
20920 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20921 && TREE_CODE (tparm
) != TYPE_DECL
)
20922 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20923 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20924 gcc_unreachable ();
20926 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20928 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20929 && CLASS_TYPE_P (arg
))
20931 /* First try to match ARG directly. */
20932 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20936 /* Otherwise, look for a suitable base of ARG, as below. */
20937 enum template_base_result r
;
20938 r
= get_template_base (tparms
, targs
, parm
, arg
,
20941 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20945 /* ARG must be constructed from a template class or a template
20946 template parameter. */
20947 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20948 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20949 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20951 /* Deduce arguments T, i from TT<T> or TT<i>. */
20952 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20955 arg
= TYPE_TI_TEMPLATE (arg
);
20957 /* Fall through to deduce template name. */
20960 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20961 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20963 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20965 /* Simple cases: Value already set, does match or doesn't. */
20966 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20967 return unify_success (explain_p
);
20969 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20973 /* If PARM is `const T' and ARG is only `int', we don't have
20974 a match unless we are allowing additional qualification.
20975 If ARG is `const int' and PARM is just `T' that's OK;
20976 that binds `const int' to `T'. */
20977 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20979 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20981 /* Consider the case where ARG is `const volatile int' and
20982 PARM is `const T'. Then, T should be `volatile int'. */
20983 arg
= cp_build_qualified_type_real
20984 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20985 if (arg
== error_mark_node
)
20986 return unify_invalid (explain_p
);
20988 /* Simple cases: Value already set, does match or doesn't. */
20989 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20990 return unify_success (explain_p
);
20992 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20994 /* Make sure that ARG is not a variable-sized array. (Note
20995 that were talking about variable-sized arrays (like
20996 `int[n]'), rather than arrays of unknown size (like
20997 `int[]').) We'll get very confused by such a type since
20998 the bound of the array is not constant, and therefore
20999 not mangleable. Besides, such types are not allowed in
21000 ISO C++, so we can do as we please here. We do allow
21001 them for 'auto' deduction, since that isn't ABI-exposed. */
21002 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21003 return unify_vla_arg (explain_p
, arg
);
21005 /* Strip typedefs as in convert_template_argument. */
21006 arg
= canonicalize_type_argument (arg
, tf_none
);
21009 /* If ARG is a parameter pack or an expansion, we cannot unify
21010 against it unless PARM is also a parameter pack. */
21011 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21012 && !template_parameter_pack_p (parm
))
21013 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21015 /* If the argument deduction results is a METHOD_TYPE,
21016 then there is a problem.
21017 METHOD_TYPE doesn't map to any real C++ type the result of
21018 the deduction can not be of that type. */
21019 if (TREE_CODE (arg
) == METHOD_TYPE
)
21020 return unify_method_type_error (explain_p
, arg
);
21022 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21023 return unify_success (explain_p
);
21025 case TEMPLATE_PARM_INDEX
:
21026 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21027 if (error_operand_p (tparm
))
21028 return unify_invalid (explain_p
);
21030 if (TEMPLATE_PARM_LEVEL (parm
)
21031 != template_decl_level (tparm
))
21033 /* The PARM is not one we're trying to unify. Just check
21034 to see if it matches ARG. */
21035 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21036 && cp_tree_equal (parm
, arg
));
21038 unify_expression_unequal (explain_p
, parm
, arg
);
21042 idx
= TEMPLATE_PARM_IDX (parm
);
21043 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21047 if ((strict
& UNIFY_ALLOW_INTEGER
)
21048 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21049 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21050 /* We're deducing from an array bound, the type doesn't matter. */
21051 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21052 int x
= !cp_tree_equal (targ
, arg
);
21054 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21058 /* [temp.deduct.type] If, in the declaration of a function template
21059 with a non-type template-parameter, the non-type
21060 template-parameter is used in an expression in the function
21061 parameter-list and, if the corresponding template-argument is
21062 deduced, the template-argument type shall match the type of the
21063 template-parameter exactly, except that a template-argument
21064 deduced from an array bound may be of any integral type.
21065 The non-type parameter might use already deduced type parameters. */
21066 ++processing_template_decl
;
21067 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
21068 --processing_template_decl
;
21069 if (tree a
= type_uses_auto (tparm
))
21071 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21072 if (tparm
== error_mark_node
)
21076 if (!TREE_TYPE (arg
))
21077 /* Template-parameter dependent expression. Just accept it for now.
21078 It will later be processed in convert_template_argument. */
21080 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21081 non_reference (tparm
)))
21083 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21084 && CP_INTEGRAL_TYPE_P (tparm
))
21085 /* Convert the ARG to the type of PARM; the deduced non-type
21086 template argument must exactly match the types of the
21087 corresponding parameter. */
21088 arg
= fold (build_nop (tparm
, arg
));
21089 else if (uses_template_parms (tparm
))
21091 /* We haven't deduced the type of this parameter yet. */
21092 if (cxx_dialect
>= cxx17
21093 /* We deduce from array bounds in try_array_deduction. */
21094 && !(strict
& UNIFY_ALLOW_INTEGER
))
21096 /* Deduce it from the non-type argument. */
21097 tree atype
= TREE_TYPE (arg
);
21098 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21100 UNIFY_ALLOW_NONE
, explain_p
);
21103 /* Try again later. */
21104 return unify_success (explain_p
);
21107 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21109 /* If ARG is a parameter pack or an expansion, we cannot unify
21110 against it unless PARM is also a parameter pack. */
21111 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21112 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21113 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21116 bool removed_attr
= false;
21117 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21119 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21120 return unify_success (explain_p
);
21124 /* A pointer-to-member constant can be unified only with
21125 another constant. */
21126 if (TREE_CODE (arg
) != PTRMEM_CST
)
21127 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21129 /* Just unify the class member. It would be useless (and possibly
21130 wrong, depending on the strict flags) to unify also
21131 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21132 arg refer to the same variable, even if through different
21133 classes. For instance:
21135 struct A { int x; };
21138 Unification of &A::x and &B::x must succeed. */
21139 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21140 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21145 if (!TYPE_PTR_P (arg
))
21146 return unify_type_mismatch (explain_p
, parm
, arg
);
21148 /* [temp.deduct.call]
21150 A can be another pointer or pointer to member type that can
21151 be converted to the deduced A via a qualification
21152 conversion (_conv.qual_).
21154 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21155 This will allow for additional cv-qualification of the
21156 pointed-to types if appropriate. */
21158 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21159 /* The derived-to-base conversion only persists through one
21160 level of pointers. */
21161 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21163 return unify (tparms
, targs
, TREE_TYPE (parm
),
21164 TREE_TYPE (arg
), strict
, explain_p
);
21167 case REFERENCE_TYPE
:
21168 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21169 return unify_type_mismatch (explain_p
, parm
, arg
);
21170 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21171 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21174 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21175 return unify_type_mismatch (explain_p
, parm
, arg
);
21176 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21177 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21178 return unify_type_mismatch (explain_p
, parm
, arg
);
21179 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21180 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21181 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21182 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21183 TYPE_DOMAIN (arg
), explain_p
);
21184 return unify_success (explain_p
);
21191 case ENUMERAL_TYPE
:
21194 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21195 return unify_type_mismatch (explain_p
, parm
, arg
);
21197 /* We have already checked cv-qualification at the top of the
21199 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21200 return unify_type_mismatch (explain_p
, parm
, arg
);
21202 /* As far as unification is concerned, this wins. Later checks
21203 will invalidate it if necessary. */
21204 return unify_success (explain_p
);
21206 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21207 /* Type INTEGER_CST can come from ordinary constant template args. */
21209 while (CONVERT_EXPR_P (arg
))
21210 arg
= TREE_OPERAND (arg
, 0);
21212 if (TREE_CODE (arg
) != INTEGER_CST
)
21213 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21214 return (tree_int_cst_equal (parm
, arg
)
21215 ? unify_success (explain_p
)
21216 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21220 int i
, len
, argslen
;
21221 int parm_variadic_p
= 0;
21223 if (TREE_CODE (arg
) != TREE_VEC
)
21224 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21226 len
= TREE_VEC_LENGTH (parm
);
21227 argslen
= TREE_VEC_LENGTH (arg
);
21229 /* Check for pack expansions in the parameters. */
21230 for (i
= 0; i
< len
; ++i
)
21232 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21235 /* We can unify against something with a trailing
21237 parm_variadic_p
= 1;
21239 /* [temp.deduct.type]/9: If the template argument list of
21240 P contains a pack expansion that is not the last
21241 template argument, the entire template argument list
21242 is a non-deduced context. */
21243 return unify_success (explain_p
);
21247 /* If we don't have enough arguments to satisfy the parameters
21248 (not counting the pack expression at the end), or we have
21249 too many arguments for a parameter list that doesn't end in
21250 a pack expression, we can't unify. */
21251 if (parm_variadic_p
21252 ? argslen
< len
- parm_variadic_p
21254 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21256 /* Unify all of the parameters that precede the (optional)
21257 pack expression. */
21258 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21260 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21261 TREE_VEC_ELT (parm
, i
),
21262 TREE_VEC_ELT (arg
, i
),
21263 UNIFY_ALLOW_NONE
, explain_p
);
21265 if (parm_variadic_p
)
21266 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21268 /*subr=*/true, explain_p
);
21269 return unify_success (explain_p
);
21274 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21275 return unify_type_mismatch (explain_p
, parm
, arg
);
21277 if (TYPE_PTRMEMFUNC_P (parm
))
21279 if (!TYPE_PTRMEMFUNC_P (arg
))
21280 return unify_type_mismatch (explain_p
, parm
, arg
);
21282 return unify (tparms
, targs
,
21283 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21284 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21285 strict
, explain_p
);
21287 else if (TYPE_PTRMEMFUNC_P (arg
))
21288 return unify_type_mismatch (explain_p
, parm
, arg
);
21290 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21292 tree t
= NULL_TREE
;
21294 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21296 /* First, we try to unify the PARM and ARG directly. */
21297 t
= try_class_unification (tparms
, targs
,
21298 parm
, arg
, explain_p
);
21302 /* Fallback to the special case allowed in
21303 [temp.deduct.call]:
21305 If P is a class, and P has the form
21306 template-id, then A can be a derived class of
21307 the deduced A. Likewise, if P is a pointer to
21308 a class of the form template-id, A can be a
21309 pointer to a derived class pointed to by the
21311 enum template_base_result r
;
21312 r
= get_template_base (tparms
, targs
, parm
, arg
,
21317 /* Don't give the derived diagnostic if we're
21318 already dealing with the same template. */
21320 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21321 && (CLASSTYPE_TI_TEMPLATE (parm
)
21322 == CLASSTYPE_TI_TEMPLATE (arg
)));
21323 return unify_no_common_base (explain_p
&& !same_template
,
21328 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21329 && (CLASSTYPE_TI_TEMPLATE (parm
)
21330 == CLASSTYPE_TI_TEMPLATE (arg
)))
21331 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21332 Then, we should unify `int' and `U'. */
21335 /* There's no chance of unification succeeding. */
21336 return unify_type_mismatch (explain_p
, parm
, arg
);
21338 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21339 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21341 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21342 return unify_type_mismatch (explain_p
, parm
, arg
);
21343 return unify_success (explain_p
);
21346 case FUNCTION_TYPE
:
21348 unsigned int nargs
;
21353 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21354 return unify_type_mismatch (explain_p
, parm
, arg
);
21356 /* CV qualifications for methods can never be deduced, they must
21357 match exactly. We need to check them explicitly here,
21358 because type_unification_real treats them as any other
21359 cv-qualified parameter. */
21360 if (TREE_CODE (parm
) == METHOD_TYPE
21361 && (!check_cv_quals_for_unify
21363 class_of_this_parm (arg
),
21364 class_of_this_parm (parm
))))
21365 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21366 if (TREE_CODE (arg
) == FUNCTION_TYPE
21367 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21368 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21369 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21370 return unify_type_mismatch (explain_p
, parm
, arg
);
21372 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21373 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21375 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21376 args
= XALLOCAVEC (tree
, nargs
);
21377 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21378 a
!= NULL_TREE
&& a
!= void_list_node
;
21379 a
= TREE_CHAIN (a
), ++i
)
21380 args
[i
] = TREE_VALUE (a
);
21383 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21384 args
, nargs
, 1, DEDUCE_EXACT
,
21385 LOOKUP_NORMAL
, NULL
, explain_p
))
21388 if (flag_noexcept_type
)
21390 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21391 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21392 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21393 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21394 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21395 && uses_template_parms (TREE_PURPOSE (pspec
)))
21396 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21397 TREE_PURPOSE (aspec
),
21398 UNIFY_ALLOW_NONE
, explain_p
);
21399 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21400 return unify_type_mismatch (explain_p
, parm
, arg
);
21407 /* Unify a pointer to member with a pointer to member function, which
21408 deduces the type of the member as a function type. */
21409 if (TYPE_PTRMEMFUNC_P (arg
))
21411 /* Check top-level cv qualifiers */
21412 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21413 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21415 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21416 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21417 UNIFY_ALLOW_NONE
, explain_p
);
21419 /* Determine the type of the function we are unifying against. */
21420 tree fntype
= static_fn_type (arg
);
21422 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21425 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21426 return unify_type_mismatch (explain_p
, parm
, arg
);
21427 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21428 TYPE_OFFSET_BASETYPE (arg
),
21429 UNIFY_ALLOW_NONE
, explain_p
);
21430 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21431 strict
, explain_p
);
21434 if (DECL_TEMPLATE_PARM_P (parm
))
21435 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21436 if (arg
!= scalar_constant_value (parm
))
21437 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21438 return unify_success (explain_p
);
21441 case TEMPLATE_DECL
:
21442 /* Matched cases are handled by the ARG == PARM test above. */
21443 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21446 /* We might get a variable as a non-type template argument in parm if the
21447 corresponding parameter is type-dependent. Make any necessary
21448 adjustments based on whether arg is a reference. */
21449 if (CONSTANT_CLASS_P (arg
))
21450 parm
= fold_non_dependent_expr (parm
);
21451 else if (REFERENCE_REF_P (arg
))
21453 tree sub
= TREE_OPERAND (arg
, 0);
21455 if (TREE_CODE (sub
) == ADDR_EXPR
)
21456 arg
= TREE_OPERAND (sub
, 0);
21458 /* Now use the normal expression code to check whether they match. */
21461 case TYPE_ARGUMENT_PACK
:
21462 case NONTYPE_ARGUMENT_PACK
:
21463 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21464 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21467 case DECLTYPE_TYPE
:
21468 case UNDERLYING_TYPE
:
21469 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21470 or UNDERLYING_TYPE nodes. */
21471 return unify_success (explain_p
);
21474 /* Unification fails if we hit an error node. */
21475 return unify_invalid (explain_p
);
21478 if (REFERENCE_REF_P (parm
))
21480 bool pexp
= PACK_EXPANSION_P (arg
);
21482 arg
= PACK_EXPANSION_PATTERN (arg
);
21483 if (REFERENCE_REF_P (arg
))
21484 arg
= TREE_OPERAND (arg
, 0);
21486 arg
= make_pack_expansion (arg
, complain
);
21487 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21488 strict
, explain_p
);
21493 /* An unresolved overload is a nondeduced context. */
21494 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21495 return unify_success (explain_p
);
21496 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21498 /* We must be looking at an expression. This can happen with
21502 void foo(S<I>, S<I + 2>);
21504 This is a "nondeduced context":
21508 The nondeduced contexts are:
21510 --A type that is a template-id in which one or more of
21511 the template-arguments is an expression that references
21512 a template-parameter.
21514 In these cases, we assume deduction succeeded, but don't
21515 actually infer any unifications. */
21517 if (!uses_template_parms (parm
)
21518 && !template_args_equal (parm
, arg
))
21519 return unify_expression_unequal (explain_p
, parm
, arg
);
21521 return unify_success (explain_p
);
21524 #undef RECUR_AND_CHECK_FAILURE
21526 /* Note that DECL can be defined in this translation unit, if
21530 mark_definable (tree decl
)
21533 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21534 FOR_EACH_CLONE (clone
, decl
)
21535 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21538 /* Called if RESULT is explicitly instantiated, or is a member of an
21539 explicitly instantiated class. */
21542 mark_decl_instantiated (tree result
, int extern_p
)
21544 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21546 /* If this entity has already been written out, it's too late to
21547 make any modifications. */
21548 if (TREE_ASM_WRITTEN (result
))
21551 /* For anonymous namespace we don't need to do anything. */
21552 if (decl_anon_ns_mem_p (result
))
21554 gcc_assert (!TREE_PUBLIC (result
));
21558 if (TREE_CODE (result
) != FUNCTION_DECL
)
21559 /* The TREE_PUBLIC flag for function declarations will have been
21560 set correctly by tsubst. */
21561 TREE_PUBLIC (result
) = 1;
21563 /* This might have been set by an earlier implicit instantiation. */
21564 DECL_COMDAT (result
) = 0;
21567 DECL_NOT_REALLY_EXTERN (result
) = 0;
21570 mark_definable (result
);
21571 mark_needed (result
);
21572 /* Always make artificials weak. */
21573 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21574 comdat_linkage (result
);
21575 /* For WIN32 we also want to put explicit instantiations in
21576 linkonce sections. */
21577 else if (TREE_PUBLIC (result
))
21578 maybe_make_one_only (result
);
21581 /* If EXTERN_P, then this function will not be emitted -- unless
21582 followed by an explicit instantiation, at which point its linkage
21583 will be adjusted. If !EXTERN_P, then this function will be
21584 emitted here. In neither circumstance do we want
21585 import_export_decl to adjust the linkage. */
21586 DECL_INTERFACE_KNOWN (result
) = 1;
21589 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21590 important template arguments. If any are missing, we check whether
21591 they're important by using error_mark_node for substituting into any
21592 args that were used for partial ordering (the ones between ARGS and END)
21593 and seeing if it bubbles up. */
21596 check_undeduced_parms (tree targs
, tree args
, tree end
)
21598 bool found
= false;
21600 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21601 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21604 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21608 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21609 if (substed
== error_mark_node
)
21615 /* Given two function templates PAT1 and PAT2, return:
21617 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21618 -1 if PAT2 is more specialized than PAT1.
21619 0 if neither is more specialized.
21621 LEN indicates the number of parameters we should consider
21622 (defaulted parameters should not be considered).
21624 The 1998 std underspecified function template partial ordering, and
21625 DR214 addresses the issue. We take pairs of arguments, one from
21626 each of the templates, and deduce them against each other. One of
21627 the templates will be more specialized if all the *other*
21628 template's arguments deduce against its arguments and at least one
21629 of its arguments *does* *not* deduce against the other template's
21630 corresponding argument. Deduction is done as for class templates.
21631 The arguments used in deduction have reference and top level cv
21632 qualifiers removed. Iff both arguments were originally reference
21633 types *and* deduction succeeds in both directions, an lvalue reference
21634 wins against an rvalue reference and otherwise the template
21635 with the more cv-qualified argument wins for that pairing (if
21636 neither is more cv-qualified, they both are equal). Unlike regular
21637 deduction, after all the arguments have been deduced in this way,
21638 we do *not* verify the deduced template argument values can be
21639 substituted into non-deduced contexts.
21641 The logic can be a bit confusing here, because we look at deduce1 and
21642 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21643 can find template arguments for pat1 to make arg1 look like arg2, that
21644 means that arg2 is at least as specialized as arg1. */
21647 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21649 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21650 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21651 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21652 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21653 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21654 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21655 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21656 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21657 tree origs1
, origs2
;
21658 bool lose1
= false;
21659 bool lose2
= false;
21661 /* Remove the this parameter from non-static member functions. If
21662 one is a non-static member function and the other is not a static
21663 member function, remove the first parameter from that function
21664 also. This situation occurs for operator functions where we
21665 locate both a member function (with this pointer) and non-member
21666 operator (with explicit first operand). */
21667 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21669 len
--; /* LEN is the number of significant arguments for DECL1 */
21670 args1
= TREE_CHAIN (args1
);
21671 if (!DECL_STATIC_FUNCTION_P (decl2
))
21672 args2
= TREE_CHAIN (args2
);
21674 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21676 args2
= TREE_CHAIN (args2
);
21677 if (!DECL_STATIC_FUNCTION_P (decl1
))
21680 args1
= TREE_CHAIN (args1
);
21684 /* If only one is a conversion operator, they are unordered. */
21685 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21688 /* Consider the return type for a conversion function */
21689 if (DECL_CONV_FN_P (decl1
))
21691 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21692 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21696 processing_template_decl
++;
21702 /* Stop when an ellipsis is seen. */
21703 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21705 tree arg1
= TREE_VALUE (args1
);
21706 tree arg2
= TREE_VALUE (args2
);
21707 int deduce1
, deduce2
;
21713 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21714 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21716 /* When both arguments are pack expansions, we need only
21717 unify the patterns themselves. */
21718 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21719 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21721 /* This is the last comparison we need to do. */
21725 /* DR 1847: If a particular P contains no template-parameters that
21726 participate in template argument deduction, that P is not used to
21727 determine the ordering. */
21728 if (!uses_deducible_template_parms (arg1
)
21729 && !uses_deducible_template_parms (arg2
))
21732 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21734 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21735 arg1
= TREE_TYPE (arg1
);
21736 quals1
= cp_type_quals (arg1
);
21739 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21741 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21742 arg2
= TREE_TYPE (arg2
);
21743 quals2
= cp_type_quals (arg2
);
21746 arg1
= TYPE_MAIN_VARIANT (arg1
);
21747 arg2
= TYPE_MAIN_VARIANT (arg2
);
21749 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21751 int i
, len2
= remaining_arguments (args2
);
21752 tree parmvec
= make_tree_vec (1);
21753 tree argvec
= make_tree_vec (len2
);
21756 /* Setup the parameter vector, which contains only ARG1. */
21757 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21759 /* Setup the argument vector, which contains the remaining
21761 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21762 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21764 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21765 argvec
, DEDUCE_EXACT
,
21766 /*subr=*/true, /*explain_p=*/false)
21769 /* We cannot deduce in the other direction, because ARG1 is
21770 a pack expansion but ARG2 is not. */
21773 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21775 int i
, len1
= remaining_arguments (args1
);
21776 tree parmvec
= make_tree_vec (1);
21777 tree argvec
= make_tree_vec (len1
);
21780 /* Setup the parameter vector, which contains only ARG1. */
21781 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21783 /* Setup the argument vector, which contains the remaining
21785 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21786 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21788 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21789 argvec
, DEDUCE_EXACT
,
21790 /*subr=*/true, /*explain_p=*/false)
21793 /* We cannot deduce in the other direction, because ARG2 is
21794 a pack expansion but ARG1 is not.*/
21800 /* The normal case, where neither argument is a pack
21802 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21803 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21805 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21806 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21810 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21811 arg2, then arg2 is not as specialized as arg1. */
21817 /* "If, for a given type, deduction succeeds in both directions
21818 (i.e., the types are identical after the transformations above)
21819 and both P and A were reference types (before being replaced with
21820 the type referred to above):
21821 - if the type from the argument template was an lvalue reference and
21822 the type from the parameter template was not, the argument type is
21823 considered to be more specialized than the other; otherwise,
21824 - if the type from the argument template is more cv-qualified
21825 than the type from the parameter template (as described above),
21826 the argument type is considered to be more specialized than the other;
21828 - neither type is more specialized than the other." */
21830 if (deduce1
&& deduce2
)
21832 if (ref1
&& ref2
&& ref1
!= ref2
)
21839 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21841 if ((quals1
& quals2
) == quals2
)
21843 if ((quals1
& quals2
) == quals1
)
21848 if (lose1
&& lose2
)
21849 /* We've failed to deduce something in either direction.
21850 These must be unordered. */
21855 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21856 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21857 /* We have already processed all of the arguments in our
21858 handing of the pack expansion type. */
21861 args1
= TREE_CHAIN (args1
);
21862 args2
= TREE_CHAIN (args2
);
21865 /* "In most cases, all template parameters must have values in order for
21866 deduction to succeed, but for partial ordering purposes a template
21867 parameter may remain without a value provided it is not used in the
21868 types being used for partial ordering."
21870 Thus, if we are missing any of the targs1 we need to substitute into
21871 origs1, then pat2 is not as specialized as pat1. This can happen when
21872 there is a nondeduced context. */
21873 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21875 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21878 processing_template_decl
--;
21880 /* If both deductions succeed, the partial ordering selects the more
21881 constrained template. */
21882 if (!lose1
&& !lose2
)
21884 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21885 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21886 lose1
= !subsumes_constraints (c1
, c2
);
21887 lose2
= !subsumes_constraints (c2
, c1
);
21890 /* All things being equal, if the next argument is a pack expansion
21891 for one function but not for the other, prefer the
21892 non-variadic function. FIXME this is bogus; see c++/41958. */
21894 && args1
&& TREE_VALUE (args1
)
21895 && args2
&& TREE_VALUE (args2
))
21897 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21898 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21901 if (lose1
== lose2
)
21909 /* Determine which of two partial specializations of TMPL is more
21912 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21913 to the first partial specialization. The TREE_PURPOSE is the
21914 innermost set of template parameters for the partial
21915 specialization. PAT2 is similar, but for the second template.
21917 Return 1 if the first partial specialization is more specialized;
21918 -1 if the second is more specialized; 0 if neither is more
21921 See [temp.class.order] for information about determining which of
21922 two templates is more specialized. */
21925 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21929 bool any_deductions
= false;
21931 tree tmpl1
= TREE_VALUE (pat1
);
21932 tree tmpl2
= TREE_VALUE (pat2
);
21933 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21934 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21936 /* Just like what happens for functions, if we are ordering between
21937 different template specializations, we may encounter dependent
21938 types in the arguments, and we need our dependency check functions
21939 to behave correctly. */
21940 ++processing_template_decl
;
21941 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21945 any_deductions
= true;
21948 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21952 any_deductions
= true;
21954 --processing_template_decl
;
21956 /* If both deductions succeed, the partial ordering selects the more
21957 constrained template. */
21958 if (!winner
&& any_deductions
)
21959 return more_constrained (tmpl1
, tmpl2
);
21961 /* In the case of a tie where at least one of the templates
21962 has a parameter pack at the end, the template with the most
21963 non-packed parameters wins. */
21966 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21967 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21969 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21970 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21971 int len1
= TREE_VEC_LENGTH (args1
);
21972 int len2
= TREE_VEC_LENGTH (args2
);
21974 /* We don't count the pack expansion at the end. */
21975 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21977 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21982 else if (len1
< len2
)
21989 /* Return the template arguments that will produce the function signature
21990 DECL from the function template FN, with the explicit template
21991 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21992 also match. Return NULL_TREE if no satisfactory arguments could be
21996 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21998 int ntparms
= DECL_NTPARMS (fn
);
21999 tree targs
= make_tree_vec (ntparms
);
22000 tree decl_type
= TREE_TYPE (decl
);
22001 tree decl_arg_types
;
22003 unsigned int nargs
, ix
;
22006 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22008 /* Never do unification on the 'this' parameter. */
22009 decl_arg_types
= skip_artificial_parms_for (decl
,
22010 TYPE_ARG_TYPES (decl_type
));
22012 nargs
= list_length (decl_arg_types
);
22013 args
= XALLOCAVEC (tree
, nargs
);
22014 for (arg
= decl_arg_types
, ix
= 0;
22015 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22016 arg
= TREE_CHAIN (arg
), ++ix
)
22017 args
[ix
] = TREE_VALUE (arg
);
22019 if (fn_type_unification (fn
, explicit_args
, targs
,
22021 (check_rettype
|| DECL_CONV_FN_P (fn
)
22022 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22023 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
22025 == error_mark_node
)
22031 /* Return the innermost template arguments that, when applied to a partial
22032 specialization SPEC_TMPL of TMPL, yield the ARGS.
22034 For example, suppose we have:
22036 template <class T, class U> struct S {};
22037 template <class T> struct S<T*, int> {};
22039 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22040 partial specialization and the ARGS will be {double*, int}. The resulting
22041 vector will be {double}, indicating that `T' is bound to `double'. */
22044 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22046 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22048 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22049 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22051 tree innermost_deduced_args
;
22053 innermost_deduced_args
= make_tree_vec (ntparms
);
22054 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22056 deduced_args
= copy_node (args
);
22057 SET_TMPL_ARGS_LEVEL (deduced_args
,
22058 TMPL_ARGS_DEPTH (deduced_args
),
22059 innermost_deduced_args
);
22062 deduced_args
= innermost_deduced_args
;
22064 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22066 if (unify (tparms
, deduced_args
,
22067 INNERMOST_TEMPLATE_ARGS (spec_args
),
22068 INNERMOST_TEMPLATE_ARGS (args
),
22069 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22072 for (i
= 0; i
< ntparms
; ++i
)
22073 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22075 if (!tried_array_deduction
)
22077 try_array_deduction (tparms
, innermost_deduced_args
,
22078 INNERMOST_TEMPLATE_ARGS (spec_args
));
22079 tried_array_deduction
= true;
22080 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22086 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22087 if (!push_tinst_level (tinst
))
22089 excessive_deduction_depth
= true;
22093 /* Verify that nondeduced template arguments agree with the type
22094 obtained from argument deduction.
22098 struct A { typedef int X; };
22099 template <class T, class U> struct C {};
22100 template <class T> struct C<T, typename T::X> {};
22102 Then with the instantiation `C<A, int>', we can deduce that
22103 `T' is `A' but unify () does not check whether `typename T::X'
22105 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22107 if (spec_args
!= error_mark_node
)
22108 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22109 INNERMOST_TEMPLATE_ARGS (spec_args
),
22110 tmpl
, tf_none
, false, false);
22112 pop_tinst_level ();
22114 if (spec_args
== error_mark_node
22115 /* We only need to check the innermost arguments; the other
22116 arguments will always agree. */
22117 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22118 INNERMOST_TEMPLATE_ARGS (args
)))
22121 /* Now that we have bindings for all of the template arguments,
22122 ensure that the arguments deduced for the template template
22123 parameters have compatible template parameter lists. See the use
22124 of template_template_parm_bindings_ok_p in fn_type_unification
22125 for more information. */
22126 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22129 return deduced_args
;
22132 // Compare two function templates T1 and T2 by deducing bindings
22133 // from one against the other. If both deductions succeed, compare
22134 // constraints to see which is more constrained.
22136 more_specialized_inst (tree t1
, tree t2
)
22141 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22147 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22153 // If both deductions succeed, then one may be more constrained.
22154 if (count
== 2 && fate
== 0)
22155 fate
= more_constrained (t1
, t2
);
22160 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22161 Return the TREE_LIST node with the most specialized template, if
22162 any. If there is no most specialized template, the error_mark_node
22165 Note that this function does not look at, or modify, the
22166 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22167 returned is one of the elements of INSTANTIATIONS, callers may
22168 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22169 and retrieve it from the value returned. */
22172 most_specialized_instantiation (tree templates
)
22176 ++processing_template_decl
;
22179 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22181 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22182 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22187 /* Equally specialized, move to next function. If there
22188 is no next function, nothing's most specialized. */
22189 fn
= TREE_CHAIN (fn
);
22197 /* Now verify that champ is better than everything earlier in the
22198 instantiation list. */
22199 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22200 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22207 processing_template_decl
--;
22210 return error_mark_node
;
22215 /* If DECL is a specialization of some template, return the most
22216 general such template. Otherwise, returns NULL_TREE.
22218 For example, given:
22220 template <class T> struct S { template <class U> void f(U); };
22222 if TMPL is `template <class U> void S<int>::f(U)' this will return
22223 the full template. This function will not trace past partial
22224 specializations, however. For example, given in addition:
22226 template <class T> struct S<T*> { template <class U> void f(U); };
22228 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22229 `template <class T> template <class U> S<T*>::f(U)'. */
22232 most_general_template (tree decl
)
22234 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22236 if (tree tinfo
= get_template_info (decl
))
22237 decl
= TI_TEMPLATE (tinfo
);
22238 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22239 template friend, or a FIELD_DECL for a capture pack. */
22240 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22244 /* Look for more and more general templates. */
22245 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22247 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22248 (See cp-tree.h for details.) */
22249 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22252 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22253 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22254 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22257 /* Stop if we run into an explicitly specialized class template. */
22258 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22259 && DECL_CONTEXT (decl
)
22260 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22263 decl
= DECL_TI_TEMPLATE (decl
);
22269 /* Return the most specialized of the template partial specializations
22270 which can produce TARGET, a specialization of some class or variable
22271 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22272 a TEMPLATE_DECL node corresponding to the partial specialization, while
22273 the TREE_PURPOSE is the set of template arguments that must be
22274 substituted into the template pattern in order to generate TARGET.
22276 If the choice of partial specialization is ambiguous, a diagnostic
22277 is issued, and the error_mark_node is returned. If there are no
22278 partial specializations matching TARGET, then NULL_TREE is
22279 returned, indicating that the primary template should be used. */
22282 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22284 tree list
= NULL_TREE
;
22289 tree outer_args
= NULL_TREE
;
22292 if (TYPE_P (target
))
22294 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22295 tmpl
= TI_TEMPLATE (tinfo
);
22296 args
= TI_ARGS (tinfo
);
22298 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22300 tmpl
= TREE_OPERAND (target
, 0);
22301 args
= TREE_OPERAND (target
, 1);
22303 else if (VAR_P (target
))
22305 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22306 tmpl
= TI_TEMPLATE (tinfo
);
22307 args
= TI_ARGS (tinfo
);
22310 gcc_unreachable ();
22312 tree main_tmpl
= most_general_template (tmpl
);
22314 /* For determining which partial specialization to use, only the
22315 innermost args are interesting. */
22316 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22318 outer_args
= strip_innermost_template_args (args
, 1);
22319 args
= INNERMOST_TEMPLATE_ARGS (args
);
22322 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22325 tree spec_tmpl
= TREE_VALUE (t
);
22329 /* Substitute in the template args from the enclosing class. */
22330 ++processing_template_decl
;
22331 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22332 --processing_template_decl
;
22335 if (spec_tmpl
== error_mark_node
)
22336 return error_mark_node
;
22338 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22342 spec_args
= add_to_template_args (outer_args
, spec_args
);
22344 /* Keep the candidate only if the constraints are satisfied,
22345 or if we're not compiling with concepts. */
22347 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22349 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22350 TREE_TYPE (list
) = TREE_TYPE (t
);
22358 ambiguous_p
= false;
22361 t
= TREE_CHAIN (t
);
22362 for (; t
; t
= TREE_CHAIN (t
))
22364 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22371 t
= TREE_CHAIN (t
);
22374 ambiguous_p
= true;
22383 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22385 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22388 ambiguous_p
= true;
22396 char *spaces
= NULL
;
22397 if (!(complain
& tf_error
))
22398 return error_mark_node
;
22399 if (TYPE_P (target
))
22400 error ("ambiguous template instantiation for %q#T", target
);
22402 error ("ambiguous template instantiation for %q#D", target
);
22403 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22404 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22406 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22407 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22408 "%s %#qS", spaces
? spaces
: str
, subst
);
22409 spaces
= spaces
? spaces
: get_spaces (str
);
22412 return error_mark_node
;
22418 /* Explicitly instantiate DECL. */
22421 do_decl_instantiation (tree decl
, tree storage
)
22423 tree result
= NULL_TREE
;
22426 if (!decl
|| decl
== error_mark_node
)
22427 /* An error occurred, for which grokdeclarator has already issued
22428 an appropriate message. */
22430 else if (! DECL_LANG_SPECIFIC (decl
))
22432 error ("explicit instantiation of non-template %q#D", decl
);
22436 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22437 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22439 if (VAR_P (decl
) && !var_templ
)
22441 /* There is an asymmetry here in the way VAR_DECLs and
22442 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22443 the latter, the DECL we get back will be marked as a
22444 template instantiation, and the appropriate
22445 DECL_TEMPLATE_INFO will be set up. This does not happen for
22446 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22447 should handle VAR_DECLs as it currently handles
22449 if (!DECL_CLASS_SCOPE_P (decl
))
22451 error ("%qD is not a static data member of a class template", decl
);
22454 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22455 if (!result
|| !VAR_P (result
))
22457 error ("no matching template for %qD found", decl
);
22460 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22462 error ("type %qT for explicit instantiation %qD does not match "
22463 "declared type %qT", TREE_TYPE (result
), decl
,
22468 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22470 error ("explicit instantiation of %q#D", decl
);
22476 /* Check for various error cases. Note that if the explicit
22477 instantiation is valid the RESULT will currently be marked as an
22478 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22479 until we get here. */
22481 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22483 /* DR 259 [temp.spec].
22485 Both an explicit instantiation and a declaration of an explicit
22486 specialization shall not appear in a program unless the explicit
22487 instantiation follows a declaration of the explicit specialization.
22489 For a given set of template parameters, if an explicit
22490 instantiation of a template appears after a declaration of an
22491 explicit specialization for that template, the explicit
22492 instantiation has no effect. */
22495 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22499 No program shall explicitly instantiate any template more
22502 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22503 the first instantiation was `extern' and the second is not,
22504 and EXTERN_P for the opposite case. */
22505 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22506 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22507 /* If an "extern" explicit instantiation follows an ordinary
22508 explicit instantiation, the template is instantiated. */
22512 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22514 error ("no matching template for %qD found", result
);
22517 else if (!DECL_TEMPLATE_INFO (result
))
22519 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22523 if (storage
== NULL_TREE
)
22525 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22527 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22528 pedwarn (input_location
, OPT_Wpedantic
,
22529 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22534 error ("storage class %qD applied to template instantiation", storage
);
22536 check_explicit_instantiation_namespace (result
);
22537 mark_decl_instantiated (result
, extern_p
);
22539 instantiate_decl (result
, /*defer_ok=*/true,
22540 /*expl_inst_class_mem_p=*/false);
22544 mark_class_instantiated (tree t
, int extern_p
)
22546 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22547 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22548 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22549 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22552 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22553 rest_of_type_compilation (t
, 1);
22557 /* Called from do_type_instantiation through binding_table_foreach to
22558 do recursive instantiation for the type bound in ENTRY. */
22560 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22562 tree storage
= *(tree
*) data
;
22564 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22565 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22566 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22569 /* Perform an explicit instantiation of template class T. STORAGE, if
22570 non-null, is the RID for extern, inline or static. COMPLAIN is
22571 nonzero if this is called from the parser, zero if called recursively,
22572 since the standard is unclear (as detailed below). */
22575 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22580 int previous_instantiation_extern_p
= 0;
22582 if (TREE_CODE (t
) == TYPE_DECL
)
22585 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22588 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22590 error ("explicit instantiation of non-class template %qD", tmpl
);
22592 error ("explicit instantiation of non-template type %qT", t
);
22598 if (!COMPLETE_TYPE_P (t
))
22600 if (complain
& tf_error
)
22601 error ("explicit instantiation of %q#T before definition of template",
22606 if (storage
!= NULL_TREE
)
22608 if (!in_system_header_at (input_location
))
22610 if (storage
== ridpointers
[(int) RID_EXTERN
])
22612 if (cxx_dialect
== cxx98
)
22613 pedwarn (input_location
, OPT_Wpedantic
,
22614 "ISO C++ 1998 forbids the use of %<extern%> on "
22615 "explicit instantiations");
22618 pedwarn (input_location
, OPT_Wpedantic
,
22619 "ISO C++ forbids the use of %qE"
22620 " on explicit instantiations", storage
);
22623 if (storage
== ridpointers
[(int) RID_INLINE
])
22625 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22627 else if (storage
== ridpointers
[(int) RID_STATIC
])
22631 error ("storage class %qD applied to template instantiation",
22637 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22639 /* DR 259 [temp.spec].
22641 Both an explicit instantiation and a declaration of an explicit
22642 specialization shall not appear in a program unless the explicit
22643 instantiation follows a declaration of the explicit specialization.
22645 For a given set of template parameters, if an explicit
22646 instantiation of a template appears after a declaration of an
22647 explicit specialization for that template, the explicit
22648 instantiation has no effect. */
22651 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22655 No program shall explicitly instantiate any template more
22658 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22659 instantiation was `extern'. If EXTERN_P then the second is.
22660 These cases are OK. */
22661 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22663 if (!previous_instantiation_extern_p
&& !extern_p
22664 && (complain
& tf_error
))
22665 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22667 /* If we've already instantiated the template, just return now. */
22668 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22672 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22673 mark_class_instantiated (t
, extern_p
);
22678 /* In contrast to implicit instantiation, where only the
22679 declarations, and not the definitions, of members are
22680 instantiated, we have here:
22684 The explicit instantiation of a class template specialization
22685 implies the instantiation of all of its members not
22686 previously explicitly specialized in the translation unit
22687 containing the explicit instantiation.
22689 Of course, we can't instantiate member template classes, since we
22690 don't have any arguments for them. Note that the standard is
22691 unclear on whether the instantiation of the members are
22692 *explicit* instantiations or not. However, the most natural
22693 interpretation is that it should be an explicit
22695 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22697 || (TREE_CODE (fld
) == FUNCTION_DECL
22699 && user_provided_p (fld
)))
22700 && DECL_TEMPLATE_INSTANTIATION (fld
))
22702 mark_decl_instantiated (fld
, extern_p
);
22704 instantiate_decl (fld
, /*defer_ok=*/true,
22705 /*expl_inst_class_mem_p=*/true);
22708 if (CLASSTYPE_NESTED_UTDS (t
))
22709 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22710 bt_instantiate_type_proc
, &storage
);
22713 /* Given a function DECL, which is a specialization of TMPL, modify
22714 DECL to be a re-instantiation of TMPL with the same template
22715 arguments. TMPL should be the template into which tsubst'ing
22716 should occur for DECL, not the most general template.
22718 One reason for doing this is a scenario like this:
22721 void f(const T&, int i);
22723 void g() { f(3, 7); }
22726 void f(const T& t, const int i) { }
22728 Note that when the template is first instantiated, with
22729 instantiate_template, the resulting DECL will have no name for the
22730 first parameter, and the wrong type for the second. So, when we go
22731 to instantiate the DECL, we regenerate it. */
22734 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22736 /* The arguments used to instantiate DECL, from the most general
22740 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22742 /* Make sure that we can see identifiers, and compute access
22744 push_access_scope (decl
);
22746 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22754 args_depth
= TMPL_ARGS_DEPTH (args
);
22755 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22756 if (args_depth
> parms_depth
)
22757 args
= get_innermost_template_args (args
, parms_depth
);
22759 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22760 args
, tf_error
, NULL_TREE
,
22761 /*defer_ok*/false);
22762 if (specs
&& specs
!= error_mark_node
)
22763 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22766 /* Merge parameter declarations. */
22767 decl_parm
= skip_artificial_parms_for (decl
,
22768 DECL_ARGUMENTS (decl
));
22770 = skip_artificial_parms_for (code_pattern
,
22771 DECL_ARGUMENTS (code_pattern
));
22772 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22777 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22778 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22779 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22781 parm_type
= type_decays_to (parm_type
);
22782 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22783 TREE_TYPE (decl_parm
) = parm_type
;
22784 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22785 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22787 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22788 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22790 decl_parm
= DECL_CHAIN (decl_parm
);
22791 pattern_parm
= DECL_CHAIN (pattern_parm
);
22793 /* Merge any parameters that match with the function parameter
22795 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22798 tree expanded_types
;
22799 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22800 the parameters in this function parameter pack. */
22801 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22802 args
, tf_error
, NULL_TREE
);
22803 len
= TREE_VEC_LENGTH (expanded_types
);
22804 for (i
= 0; i
< len
; i
++)
22809 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22810 /* Rename the parameter to include the index. */
22811 DECL_NAME (decl_parm
) =
22812 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22813 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22814 parm_type
= type_decays_to (parm_type
);
22815 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22816 TREE_TYPE (decl_parm
) = parm_type
;
22817 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22818 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22820 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22821 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22823 decl_parm
= DECL_CHAIN (decl_parm
);
22826 /* Merge additional specifiers from the CODE_PATTERN. */
22827 if (DECL_DECLARED_INLINE_P (code_pattern
)
22828 && !DECL_DECLARED_INLINE_P (decl
))
22829 DECL_DECLARED_INLINE_P (decl
) = 1;
22831 else if (VAR_P (decl
))
22833 start_lambda_scope (decl
);
22834 DECL_INITIAL (decl
) =
22835 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22836 tf_error
, DECL_TI_TEMPLATE (decl
),
22837 /*integral_constant_expression_p=*/false);
22838 finish_lambda_scope ();
22839 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22840 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22841 tf_error
, DECL_TI_TEMPLATE (decl
));
22844 gcc_unreachable ();
22846 pop_access_scope (decl
);
22849 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22850 substituted to get DECL. */
22853 template_for_substitution (tree decl
)
22855 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22857 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22858 for the instantiation. This is not always the most general
22859 template. Consider, for example:
22862 struct S { template <class U> void f();
22863 template <> void f<int>(); };
22865 and an instantiation of S<double>::f<int>. We want TD to be the
22866 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22867 while (/* An instantiation cannot have a definition, so we need a
22868 more general template. */
22869 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22870 /* We must also deal with friend templates. Given:
22872 template <class T> struct S {
22873 template <class U> friend void f() {};
22876 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22877 so far as the language is concerned, but that's still
22878 where we get the pattern for the instantiation from. On
22879 other hand, if the definition comes outside the class, say:
22881 template <class T> struct S {
22882 template <class U> friend void f();
22884 template <class U> friend void f() {}
22886 we don't need to look any further. That's what the check for
22887 DECL_INITIAL is for. */
22888 || (TREE_CODE (decl
) == FUNCTION_DECL
22889 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22890 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22892 /* The present template, TD, should not be a definition. If it
22893 were a definition, we should be using it! Note that we
22894 cannot restructure the loop to just keep going until we find
22895 a template with a definition, since that might go too far if
22896 a specialization was declared, but not defined. */
22898 /* Fetch the more general template. */
22899 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22905 /* Returns true if we need to instantiate this template instance even if we
22906 know we aren't going to emit it. */
22909 always_instantiate_p (tree decl
)
22911 /* We always instantiate inline functions so that we can inline them. An
22912 explicit instantiation declaration prohibits implicit instantiation of
22913 non-inline functions. With high levels of optimization, we would
22914 normally inline non-inline functions -- but we're not allowed to do
22915 that for "extern template" functions. Therefore, we check
22916 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22917 return ((TREE_CODE (decl
) == FUNCTION_DECL
22918 && (DECL_DECLARED_INLINE_P (decl
)
22919 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22920 /* And we need to instantiate static data members so that
22921 their initializers are available in integral constant
22924 && decl_maybe_constant_var_p (decl
)));
22927 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22928 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22929 error, true otherwise. */
22932 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22934 tree fntype
, spec
, noex
, clone
;
22936 /* Don't instantiate a noexcept-specification from template context. */
22937 if (processing_template_decl
)
22940 if (DECL_CLONED_FUNCTION_P (fn
))
22941 fn
= DECL_CLONED_FUNCTION (fn
);
22942 fntype
= TREE_TYPE (fn
);
22943 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22945 if (!spec
|| !TREE_PURPOSE (spec
))
22948 noex
= TREE_PURPOSE (spec
);
22950 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22952 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22953 bool added
= false;
22954 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22955 spec
= get_defaulted_eh_spec (fn
, complain
);
22956 else if (!(added
= !fns
->add (fn
)))
22958 /* If hash_set::add returns true, the element was already there. */
22959 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22960 DECL_SOURCE_LOCATION (fn
));
22962 "exception specification of %qD depends on itself",
22964 spec
= noexcept_false_spec
;
22966 else if (push_tinst_level (fn
))
22968 push_access_scope (fn
);
22969 push_deferring_access_checks (dk_no_deferred
);
22970 input_location
= DECL_SOURCE_LOCATION (fn
);
22971 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22972 DEFERRED_NOEXCEPT_ARGS (noex
),
22973 tf_warning_or_error
, fn
,
22974 /*function_p=*/false,
22975 /*integral_constant_expression_p=*/true);
22976 pop_deferring_access_checks ();
22977 pop_access_scope (fn
);
22978 pop_tinst_level ();
22979 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22980 if (spec
== error_mark_node
)
22981 spec
= noexcept_false_spec
;
22984 spec
= noexcept_false_spec
;
22989 if (spec
== error_mark_node
)
22992 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22995 FOR_EACH_CLONE (clone
, fn
)
22997 if (TREE_TYPE (clone
) == fntype
)
22998 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23000 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23006 /* We're starting to process the function INST, an instantiation of PATTERN;
23007 add their parameters to local_specializations. */
23010 register_parameter_specializations (tree pattern
, tree inst
)
23012 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23013 tree spec_parm
= DECL_ARGUMENTS (inst
);
23014 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23016 register_local_specialization (spec_parm
, tmpl_parm
);
23017 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23018 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23020 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23022 if (!DECL_PACK_P (tmpl_parm
))
23024 register_local_specialization (spec_parm
, tmpl_parm
);
23025 spec_parm
= DECL_CHAIN (spec_parm
);
23029 /* Register the (value) argument pack as a specialization of
23030 TMPL_PARM, then move on. */
23031 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23032 register_local_specialization (argpack
, tmpl_parm
);
23035 gcc_assert (!spec_parm
);
23038 /* Produce the definition of D, a _DECL generated from a template. If
23039 DEFER_OK is true, then we don't have to actually do the
23040 instantiation now; we just have to do it sometime. Normally it is
23041 an error if this is an explicit instantiation but D is undefined.
23042 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23043 instantiated class template. */
23046 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23048 tree tmpl
= DECL_TI_TEMPLATE (d
);
23055 bool pattern_defined
;
23056 location_t saved_loc
= input_location
;
23057 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23058 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23062 /* This function should only be used to instantiate templates for
23063 functions and static member variables. */
23064 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23066 /* A concept is never instantiated. */
23067 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23069 /* Variables are never deferred; if instantiation is required, they
23070 are instantiated right away. That allows for better code in the
23071 case that an expression refers to the value of the variable --
23072 if the variable has a constant value the referring expression can
23073 take advantage of that fact. */
23077 /* Don't instantiate cloned functions. Instead, instantiate the
23078 functions they cloned. */
23079 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23080 d
= DECL_CLONED_FUNCTION (d
);
23082 if (DECL_TEMPLATE_INSTANTIATED (d
)
23083 || (TREE_CODE (d
) == FUNCTION_DECL
23084 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23085 || DECL_TEMPLATE_SPECIALIZATION (d
))
23086 /* D has already been instantiated or explicitly specialized, so
23087 there's nothing for us to do here.
23089 It might seem reasonable to check whether or not D is an explicit
23090 instantiation, and, if so, stop here. But when an explicit
23091 instantiation is deferred until the end of the compilation,
23092 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23093 the instantiation. */
23096 /* Check to see whether we know that this template will be
23097 instantiated in some other file, as with "extern template"
23099 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23101 /* In general, we do not instantiate such templates. */
23102 if (external_p
&& !always_instantiate_p (d
))
23105 gen_tmpl
= most_general_template (tmpl
);
23106 gen_args
= DECL_TI_ARGS (d
);
23108 if (tmpl
!= gen_tmpl
)
23109 /* We should already have the extra args. */
23110 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23111 == TMPL_ARGS_DEPTH (gen_args
));
23112 /* And what's in the hash table should match D. */
23113 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23114 || spec
== NULL_TREE
);
23116 /* This needs to happen before any tsubsting. */
23117 if (! push_tinst_level (d
))
23120 timevar_push (TV_TEMPLATE_INST
);
23122 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23123 for the instantiation. */
23124 td
= template_for_substitution (d
);
23129 /* Look up an explicit specialization, if any. */
23130 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23131 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23132 if (elt
&& elt
!= error_mark_node
)
23134 td
= TREE_VALUE (elt
);
23135 args
= TREE_PURPOSE (elt
);
23139 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23141 /* We should never be trying to instantiate a member of a class
23142 template or partial specialization. */
23143 gcc_assert (d
!= code_pattern
);
23145 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23146 || DECL_TEMPLATE_SPECIALIZATION (td
))
23147 /* In the case of a friend template whose definition is provided
23148 outside the class, we may have too many arguments. Drop the
23149 ones we don't need. The same is true for specializations. */
23150 args
= get_innermost_template_args
23151 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23153 if (TREE_CODE (d
) == FUNCTION_DECL
)
23155 deleted_p
= DECL_DELETED_FN (code_pattern
);
23156 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23157 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23158 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23164 if (DECL_CLASS_SCOPE_P (code_pattern
))
23165 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23166 || DECL_INLINE_VAR_P (code_pattern
));
23168 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23171 /* We may be in the middle of deferred access check. Disable it now. */
23172 push_deferring_access_checks (dk_no_deferred
);
23174 /* Unless an explicit instantiation directive has already determined
23175 the linkage of D, remember that a definition is available for
23177 if (pattern_defined
23178 && !DECL_INTERFACE_KNOWN (d
)
23179 && !DECL_NOT_REALLY_EXTERN (d
))
23180 mark_definable (d
);
23182 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23183 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23184 input_location
= DECL_SOURCE_LOCATION (d
);
23186 /* If D is a member of an explicitly instantiated class template,
23187 and no definition is available, treat it like an implicit
23189 if (!pattern_defined
&& expl_inst_class_mem_p
23190 && DECL_EXPLICIT_INSTANTIATION (d
))
23192 /* Leave linkage flags alone on instantiations with anonymous
23194 if (TREE_PUBLIC (d
))
23196 DECL_NOT_REALLY_EXTERN (d
) = 0;
23197 DECL_INTERFACE_KNOWN (d
) = 0;
23199 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23202 /* Defer all other templates, unless we have been explicitly
23203 forbidden from doing so. */
23204 if (/* If there is no definition, we cannot instantiate the
23207 /* If it's OK to postpone instantiation, do so. */
23209 /* If this is a static data member that will be defined
23210 elsewhere, we don't want to instantiate the entire data
23211 member, but we do want to instantiate the initializer so that
23212 we can substitute that elsewhere. */
23213 || (external_p
&& VAR_P (d
))
23214 /* Handle here a deleted function too, avoid generating
23215 its body (c++/61080). */
23218 /* The definition of the static data member is now required so
23219 we must substitute the initializer. */
23221 && !DECL_INITIAL (d
)
23222 && DECL_INITIAL (code_pattern
))
23226 bool const_init
= false;
23227 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23229 ns
= decl_namespace_context (d
);
23230 push_nested_namespace (ns
);
23232 push_nested_class (DECL_CONTEXT (d
));
23233 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23235 tf_warning_or_error
, NULL_TREE
,
23236 /*integral_constant_expression_p=*/false);
23237 /* If instantiating the initializer involved instantiating this
23238 again, don't call cp_finish_decl twice. */
23239 if (!DECL_INITIAL (d
))
23241 /* Make sure the initializer is still constant, in case of
23242 circular dependency (template/instantiate6.C). */
23244 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23245 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23246 /*asmspec_tree=*/NULL_TREE
,
23247 LOOKUP_ONLYCONVERTING
);
23250 pop_nested_class ();
23251 pop_nested_namespace (ns
);
23254 /* We restore the source position here because it's used by
23255 add_pending_template. */
23256 input_location
= saved_loc
;
23258 if (at_eof
&& !pattern_defined
23259 && DECL_EXPLICIT_INSTANTIATION (d
)
23260 && DECL_NOT_REALLY_EXTERN (d
))
23263 The definition of a non-exported function template, a
23264 non-exported member function template, or a non-exported
23265 member function or static data member of a class template
23266 shall be present in every translation unit in which it is
23267 explicitly instantiated. */
23268 permerror (input_location
, "explicit instantiation of %qD "
23269 "but no definition available", d
);
23271 /* If we're in unevaluated context, we just wanted to get the
23272 constant value; this isn't an odr use, so don't queue
23273 a full instantiation. */
23274 if (cp_unevaluated_operand
!= 0)
23276 /* ??? Historically, we have instantiated inline functions, even
23277 when marked as "extern template". */
23278 if (!(external_p
&& VAR_P (d
)))
23279 add_pending_template (d
);
23282 /* Tell the repository that D is available in this translation unit
23283 -- and see if it is supposed to be instantiated here. */
23284 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23286 /* In a PCH file, despite the fact that the repository hasn't
23287 requested instantiation in the PCH it is still possible that
23288 an instantiation will be required in a file that includes the
23291 add_pending_template (d
);
23292 /* Instantiate inline functions so that the inliner can do its
23293 job, even though we'll not be emitting a copy of this
23295 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23299 bool push_to_top
, nested
;
23301 fn_context
= decl_function_context (d
);
23302 nested
= current_function_decl
!= NULL_TREE
;
23303 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23305 vec
<tree
> omp_privatization_save
;
23307 save_omp_privatization_clauses (omp_privatization_save
);
23310 push_to_top_level ();
23313 push_function_context ();
23314 cp_unevaluated_operand
= 0;
23315 c_inhibit_evaluation_warnings
= 0;
23318 /* Mark D as instantiated so that recursive calls to
23319 instantiate_decl do not try to instantiate it again. */
23320 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23322 /* Regenerate the declaration in case the template has been modified
23323 by a subsequent redeclaration. */
23324 regenerate_decl_from_template (d
, td
, args
);
23326 /* We already set the file and line above. Reset them now in case
23327 they changed as a result of calling regenerate_decl_from_template. */
23328 input_location
= DECL_SOURCE_LOCATION (d
);
23333 bool const_init
= false;
23335 /* Clear out DECL_RTL; whatever was there before may not be right
23336 since we've reset the type of the declaration. */
23337 SET_DECL_RTL (d
, NULL
);
23338 DECL_IN_AGGR_P (d
) = 0;
23340 /* The initializer is placed in DECL_INITIAL by
23341 regenerate_decl_from_template so we don't need to
23342 push/pop_access_scope again here. Pull it out so that
23343 cp_finish_decl can process it. */
23344 init
= DECL_INITIAL (d
);
23345 DECL_INITIAL (d
) = NULL_TREE
;
23346 DECL_INITIALIZED_P (d
) = 0;
23348 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23349 initializer. That function will defer actual emission until
23350 we have a chance to determine linkage. */
23351 DECL_EXTERNAL (d
) = 0;
23353 /* Enter the scope of D so that access-checking works correctly. */
23354 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23356 push_nested_class (DECL_CONTEXT (d
));
23358 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23359 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23362 pop_nested_class ();
23364 if (variable_template_p (gen_tmpl
))
23365 note_variable_template_instantiation (d
);
23367 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23368 synthesize_method (d
);
23369 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23371 /* Set up the list of local specializations. */
23372 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23373 tree block
= NULL_TREE
;
23375 /* Set up context. */
23376 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23377 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23378 block
= push_stmt_list ();
23380 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23382 /* Some typedefs referenced from within the template code need to be
23383 access checked at template instantiation time, i.e now. These
23384 types were added to the template at parsing time. Let's get those
23385 and perform the access checks then. */
23386 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23389 /* Create substitution entries for the parameters. */
23390 register_parameter_specializations (code_pattern
, d
);
23392 /* Substitute into the body of the function. */
23393 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23394 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23395 tf_warning_or_error
, tmpl
);
23398 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23399 tf_warning_or_error
, tmpl
,
23400 /*integral_constant_expression_p=*/false);
23402 /* Set the current input_location to the end of the function
23403 so that finish_function knows where we are. */
23405 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23407 /* Remember if we saw an infinite loop in the template. */
23408 current_function_infinite_loop
23409 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23412 /* Finish the function. */
23413 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23414 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23415 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23418 d
= finish_function (/*inline_p=*/false);
23419 expand_or_defer_fn (d
);
23422 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23423 cp_check_omp_declare_reduction (d
);
23426 /* We're not deferring instantiation any more. */
23427 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23430 pop_from_top_level ();
23432 pop_function_context ();
23435 restore_omp_privatization_clauses (omp_privatization_save
);
23438 pop_deferring_access_checks ();
23439 timevar_pop (TV_TEMPLATE_INST
);
23440 pop_tinst_level ();
23441 input_location
= saved_loc
;
23442 cp_unevaluated_operand
= saved_unevaluated_operand
;
23443 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23448 /* Run through the list of templates that we wish we could
23449 instantiate, and instantiate any we can. RETRIES is the
23450 number of times we retry pending template instantiation. */
23453 instantiate_pending_templates (int retries
)
23456 location_t saved_loc
= input_location
;
23458 /* Instantiating templates may trigger vtable generation. This in turn
23459 may require further template instantiations. We place a limit here
23460 to avoid infinite loop. */
23461 if (pending_templates
&& retries
>= max_tinst_depth
)
23463 tree decl
= pending_templates
->tinst
->decl
;
23465 fatal_error (input_location
,
23466 "template instantiation depth exceeds maximum of %d"
23467 " instantiating %q+D, possibly from virtual table generation"
23468 " (use -ftemplate-depth= to increase the maximum)",
23469 max_tinst_depth
, decl
);
23470 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23471 /* Pretend that we defined it. */
23472 DECL_INITIAL (decl
) = error_mark_node
;
23478 struct pending_template
**t
= &pending_templates
;
23479 struct pending_template
*last
= NULL
;
23483 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23484 bool complete
= false;
23486 if (TYPE_P (instantiation
))
23488 if (!COMPLETE_TYPE_P (instantiation
))
23490 instantiate_class_template (instantiation
);
23491 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23492 for (tree fld
= TYPE_FIELDS (instantiation
);
23493 fld
; fld
= TREE_CHAIN (fld
))
23495 || (TREE_CODE (fld
) == FUNCTION_DECL
23496 && !DECL_ARTIFICIAL (fld
)))
23497 && DECL_TEMPLATE_INSTANTIATION (fld
))
23498 instantiate_decl (fld
,
23499 /*defer_ok=*/false,
23500 /*expl_inst_class_mem_p=*/false);
23502 if (COMPLETE_TYPE_P (instantiation
))
23506 complete
= COMPLETE_TYPE_P (instantiation
);
23510 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23511 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23514 = instantiate_decl (instantiation
,
23515 /*defer_ok=*/false,
23516 /*expl_inst_class_mem_p=*/false);
23517 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23521 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23522 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23526 /* If INSTANTIATION has been instantiated, then we don't
23527 need to consider it again in the future. */
23535 current_tinst_level
= NULL
;
23537 last_pending_template
= last
;
23539 while (reconsider
);
23541 input_location
= saved_loc
;
23544 /* Substitute ARGVEC into T, which is a list of initializers for
23545 either base class or a non-static data member. The TREE_PURPOSEs
23546 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23547 instantiate_decl. */
23550 tsubst_initializer_list (tree t
, tree argvec
)
23552 tree inits
= NULL_TREE
;
23554 for (; t
; t
= TREE_CHAIN (t
))
23558 tree expanded_bases
= NULL_TREE
;
23559 tree expanded_arguments
= NULL_TREE
;
23562 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23567 /* Expand the base class expansion type into separate base
23569 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23570 tf_warning_or_error
,
23572 if (expanded_bases
== error_mark_node
)
23575 /* We'll be building separate TREE_LISTs of arguments for
23577 len
= TREE_VEC_LENGTH (expanded_bases
);
23578 expanded_arguments
= make_tree_vec (len
);
23579 for (i
= 0; i
< len
; i
++)
23580 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23582 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23583 expand each argument in the TREE_VALUE of t. */
23584 expr
= make_node (EXPR_PACK_EXPANSION
);
23585 PACK_EXPANSION_LOCAL_P (expr
) = true;
23586 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23587 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23589 if (TREE_VALUE (t
) == void_type_node
)
23590 /* VOID_TYPE_NODE is used to indicate
23591 value-initialization. */
23593 for (i
= 0; i
< len
; i
++)
23594 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23598 /* Substitute parameter packs into each argument in the
23600 in_base_initializer
= 1;
23601 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23603 tree expanded_exprs
;
23605 /* Expand the argument. */
23606 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23608 = tsubst_pack_expansion (expr
, argvec
,
23609 tf_warning_or_error
,
23611 if (expanded_exprs
== error_mark_node
)
23614 /* Prepend each of the expanded expressions to the
23615 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23616 for (i
= 0; i
< len
; i
++)
23618 TREE_VEC_ELT (expanded_arguments
, i
) =
23619 tree_cons (NULL_TREE
,
23620 TREE_VEC_ELT (expanded_exprs
, i
),
23621 TREE_VEC_ELT (expanded_arguments
, i
));
23624 in_base_initializer
= 0;
23626 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23627 since we built them backwards. */
23628 for (i
= 0; i
< len
; i
++)
23630 TREE_VEC_ELT (expanded_arguments
, i
) =
23631 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23636 for (i
= 0; i
< len
; ++i
)
23638 if (expanded_bases
)
23640 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23641 decl
= expand_member_init (decl
);
23642 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23647 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23648 tf_warning_or_error
, NULL_TREE
);
23650 decl
= expand_member_init (decl
);
23651 if (decl
&& !DECL_P (decl
))
23652 in_base_initializer
= 1;
23654 init
= TREE_VALUE (t
);
23656 if (init
!= void_type_node
)
23657 init
= tsubst_expr (init
, argvec
,
23658 tf_warning_or_error
, NULL_TREE
,
23659 /*integral_constant_expression_p=*/false);
23660 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23661 /* If we had an initializer but it instantiated to nothing,
23662 value-initialize the object. This will only occur when
23663 the initializer was a pack expansion where the parameter
23664 packs used in that expansion were of length zero. */
23665 init
= void_type_node
;
23666 in_base_initializer
= 0;
23671 init
= build_tree_list (decl
, init
);
23672 TREE_CHAIN (init
) = inits
;
23680 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23683 set_current_access_from_decl (tree decl
)
23685 if (TREE_PRIVATE (decl
))
23686 current_access_specifier
= access_private_node
;
23687 else if (TREE_PROTECTED (decl
))
23688 current_access_specifier
= access_protected_node
;
23690 current_access_specifier
= access_public_node
;
23693 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23694 is the instantiation (which should have been created with
23695 start_enum) and ARGS are the template arguments to use. */
23698 tsubst_enum (tree tag
, tree newtag
, tree args
)
23702 if (SCOPED_ENUM_P (newtag
))
23703 begin_scope (sk_scoped_enum
, newtag
);
23705 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23710 decl
= TREE_VALUE (e
);
23711 /* Note that in a template enum, the TREE_VALUE is the
23712 CONST_DECL, not the corresponding INTEGER_CST. */
23713 value
= tsubst_expr (DECL_INITIAL (decl
),
23714 args
, tf_warning_or_error
, NULL_TREE
,
23715 /*integral_constant_expression_p=*/true);
23717 /* Give this enumeration constant the correct access. */
23718 set_current_access_from_decl (decl
);
23720 /* Actually build the enumerator itself. Here we're assuming that
23721 enumerators can't have dependent attributes. */
23722 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23723 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23726 if (SCOPED_ENUM_P (newtag
))
23729 finish_enum_value_list (newtag
);
23730 finish_enum (newtag
);
23732 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23733 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23736 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23737 its type -- but without substituting the innermost set of template
23738 arguments. So, innermost set of template parameters will appear in
23742 get_mostly_instantiated_function_type (tree decl
)
23744 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23745 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23748 /* Return truthvalue if we're processing a template different from
23749 the last one involved in diagnostics. */
23751 problematic_instantiation_changed (void)
23753 return current_tinst_level
!= last_error_tinst_level
;
23756 /* Remember current template involved in diagnostics. */
23758 record_last_problematic_instantiation (void)
23760 last_error_tinst_level
= current_tinst_level
;
23763 struct tinst_level
*
23764 current_instantiation (void)
23766 return current_tinst_level
;
23769 /* Return TRUE if current_function_decl is being instantiated, false
23773 instantiating_current_function_p (void)
23775 return (current_instantiation ()
23776 && current_instantiation ()->decl
== current_function_decl
);
23779 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23780 type. Return false for ok, true for disallowed. Issue error and
23781 inform messages under control of COMPLAIN. */
23784 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23786 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23788 else if (POINTER_TYPE_P (type
))
23790 else if (TYPE_PTRMEM_P (type
))
23792 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23794 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23796 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23798 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23800 /* A bound template template parm could later be instantiated to have a valid
23801 nontype parm type via an alias template. */
23802 else if (cxx_dialect
>= cxx11
23803 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23806 if (complain
& tf_error
)
23808 if (type
== error_mark_node
)
23809 inform (input_location
, "invalid template non-type parameter");
23811 error ("%q#T is not a valid type for a template non-type parameter",
23817 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23818 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23821 dependent_type_p_r (tree type
)
23827 A type is dependent if it is:
23829 -- a template parameter. Template template parameters are types
23830 for us (since TYPE_P holds true for them) so we handle
23832 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23833 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23835 /* -- a qualified-id with a nested-name-specifier which contains a
23836 class-name that names a dependent type or whose unqualified-id
23837 names a dependent type. */
23838 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23841 /* An alias template specialization can be dependent even if the
23842 resulting type is not. */
23843 if (dependent_alias_template_spec_p (type
))
23846 /* -- a cv-qualified type where the cv-unqualified type is
23848 No code is necessary for this bullet; the code below handles
23849 cv-qualified types, and we don't want to strip aliases with
23850 TYPE_MAIN_VARIANT because of DR 1558. */
23851 /* -- a compound type constructed from any dependent type. */
23852 if (TYPE_PTRMEM_P (type
))
23853 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23854 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23856 else if (TYPE_PTR_P (type
)
23857 || TREE_CODE (type
) == REFERENCE_TYPE
)
23858 return dependent_type_p (TREE_TYPE (type
));
23859 else if (TREE_CODE (type
) == FUNCTION_TYPE
23860 || TREE_CODE (type
) == METHOD_TYPE
)
23864 if (dependent_type_p (TREE_TYPE (type
)))
23866 for (arg_type
= TYPE_ARG_TYPES (type
);
23868 arg_type
= TREE_CHAIN (arg_type
))
23869 if (dependent_type_p (TREE_VALUE (arg_type
)))
23871 if (cxx_dialect
>= cxx17
)
23872 /* A value-dependent noexcept-specifier makes the type dependent. */
23873 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
23874 if (tree noex
= TREE_PURPOSE (spec
))
23875 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23876 affect overload resolution and treating it as dependent breaks
23878 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
23879 && value_dependent_expression_p (noex
))
23883 /* -- an array type constructed from any dependent type or whose
23884 size is specified by a constant expression that is
23887 We checked for type- and value-dependence of the bounds in
23888 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23889 if (TREE_CODE (type
) == ARRAY_TYPE
)
23891 if (TYPE_DOMAIN (type
)
23892 && dependent_type_p (TYPE_DOMAIN (type
)))
23894 return dependent_type_p (TREE_TYPE (type
));
23897 /* -- a template-id in which either the template name is a template
23899 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23901 /* ... or any of the template arguments is a dependent type or
23902 an expression that is type-dependent or value-dependent. */
23903 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23904 && (any_dependent_template_arguments_p
23905 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23908 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23909 dependent; if the argument of the `typeof' expression is not
23910 type-dependent, then it should already been have resolved. */
23911 if (TREE_CODE (type
) == TYPEOF_TYPE
23912 || TREE_CODE (type
) == DECLTYPE_TYPE
23913 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23916 /* A template argument pack is dependent if any of its packed
23918 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23920 tree args
= ARGUMENT_PACK_ARGS (type
);
23921 int i
, len
= TREE_VEC_LENGTH (args
);
23922 for (i
= 0; i
< len
; ++i
)
23923 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23927 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23928 be template parameters. */
23929 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23932 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23935 /* The standard does not specifically mention types that are local
23936 to template functions or local classes, but they should be
23937 considered dependent too. For example:
23939 template <int I> void f() {
23944 The size of `E' cannot be known until the value of `I' has been
23945 determined. Therefore, `E' must be considered dependent. */
23946 scope
= TYPE_CONTEXT (type
);
23947 if (scope
&& TYPE_P (scope
))
23948 return dependent_type_p (scope
);
23949 /* Don't use type_dependent_expression_p here, as it can lead
23950 to infinite recursion trying to determine whether a lambda
23951 nested in a lambda is dependent (c++/47687). */
23952 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23953 && DECL_LANG_SPECIFIC (scope
)
23954 && DECL_TEMPLATE_INFO (scope
)
23955 && (any_dependent_template_arguments_p
23956 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23959 /* Other types are non-dependent. */
23963 /* Returns TRUE if TYPE is dependent, in the sense of
23964 [temp.dep.type]. Note that a NULL type is considered dependent. */
23967 dependent_type_p (tree type
)
23969 /* If there are no template parameters in scope, then there can't be
23970 any dependent types. */
23971 if (!processing_template_decl
)
23973 /* If we are not processing a template, then nobody should be
23974 providing us with a dependent type. */
23976 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23980 /* If the type is NULL, we have not computed a type for the entity
23981 in question; in that case, the type is dependent. */
23985 /* Erroneous types can be considered non-dependent. */
23986 if (type
== error_mark_node
)
23989 /* Getting here with global_type_node means we improperly called this
23990 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23991 gcc_checking_assert (type
!= global_type_node
);
23993 /* If we have not already computed the appropriate value for TYPE,
23995 if (!TYPE_DEPENDENT_P_VALID (type
))
23997 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23998 TYPE_DEPENDENT_P_VALID (type
) = 1;
24001 return TYPE_DEPENDENT_P (type
);
24004 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24005 lookup. In other words, a dependent type that is not the current
24009 dependent_scope_p (tree scope
)
24011 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24012 && !currently_open_class (scope
));
24015 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24016 an unknown base of 'this' (and is therefore instantiation-dependent). */
24019 unknown_base_ref_p (tree t
)
24021 if (!current_class_ptr
)
24024 tree mem
= TREE_OPERAND (t
, 1);
24025 if (shared_member_p (mem
))
24028 tree cur
= current_nonlambda_class_type ();
24029 if (!any_dependent_bases_p (cur
))
24032 tree ctx
= TREE_OPERAND (t
, 0);
24033 if (DERIVED_FROM_P (ctx
, cur
))
24039 /* T is a SCOPE_REF; return whether we need to consider it
24040 instantiation-dependent so that we can check access at instantiation
24041 time even though we know which member it resolves to. */
24044 instantiation_dependent_scope_ref_p (tree t
)
24046 if (DECL_P (TREE_OPERAND (t
, 1))
24047 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24048 && !unknown_base_ref_p (t
)
24049 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24050 TREE_OPERAND (t
, 1)))
24056 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24057 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24060 /* Note that this predicate is not appropriate for general expressions;
24061 only constant expressions (that satisfy potential_constant_expression)
24062 can be tested for value dependence. */
24065 value_dependent_expression_p (tree expression
)
24067 if (!processing_template_decl
|| expression
== NULL_TREE
)
24070 /* A type-dependent expression is also value-dependent. */
24071 if (type_dependent_expression_p (expression
))
24074 switch (TREE_CODE (expression
))
24077 /* A dependent member function of the current instantiation. */
24078 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24080 case FUNCTION_DECL
:
24081 /* A dependent member function of the current instantiation. */
24082 if (DECL_CLASS_SCOPE_P (expression
)
24083 && dependent_type_p (DECL_CONTEXT (expression
)))
24087 case IDENTIFIER_NODE
:
24088 /* A name that has not been looked up -- must be dependent. */
24091 case TEMPLATE_PARM_INDEX
:
24092 /* A non-type template parm. */
24096 /* A non-type template parm. */
24097 if (DECL_TEMPLATE_PARM_P (expression
))
24099 return value_dependent_expression_p (DECL_INITIAL (expression
));
24102 /* A constant with literal type and is initialized
24103 with an expression that is value-dependent. */
24104 if (DECL_DEPENDENT_INIT_P (expression
)
24105 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24106 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24108 if (DECL_HAS_VALUE_EXPR_P (expression
))
24110 tree value_expr
= DECL_VALUE_EXPR (expression
);
24111 if (value_dependent_expression_p (value_expr
))
24116 case DYNAMIC_CAST_EXPR
:
24117 case STATIC_CAST_EXPR
:
24118 case CONST_CAST_EXPR
:
24119 case REINTERPRET_CAST_EXPR
:
24121 case IMPLICIT_CONV_EXPR
:
24122 /* These expressions are value-dependent if the type to which
24123 the cast occurs is dependent or the expression being casted
24124 is value-dependent. */
24126 tree type
= TREE_TYPE (expression
);
24128 if (dependent_type_p (type
))
24131 /* A functional cast has a list of operands. */
24132 expression
= TREE_OPERAND (expression
, 0);
24135 /* If there are no operands, it must be an expression such
24136 as "int()". This should not happen for aggregate types
24137 because it would form non-constant expressions. */
24138 gcc_assert (cxx_dialect
>= cxx11
24139 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24144 if (TREE_CODE (expression
) == TREE_LIST
)
24145 return any_value_dependent_elements_p (expression
);
24147 return value_dependent_expression_p (expression
);
24151 if (SIZEOF_EXPR_TYPE_P (expression
))
24152 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24156 /* A `sizeof' expression is value-dependent if the operand is
24157 type-dependent or is a pack expansion. */
24158 expression
= TREE_OPERAND (expression
, 0);
24159 if (PACK_EXPANSION_P (expression
))
24161 else if (TYPE_P (expression
))
24162 return dependent_type_p (expression
);
24163 return instantiation_dependent_uneval_expression_p (expression
);
24165 case AT_ENCODE_EXPR
:
24166 /* An 'encode' expression is value-dependent if the operand is
24168 expression
= TREE_OPERAND (expression
, 0);
24169 return dependent_type_p (expression
);
24171 case NOEXCEPT_EXPR
:
24172 expression
= TREE_OPERAND (expression
, 0);
24173 return instantiation_dependent_uneval_expression_p (expression
);
24176 /* All instantiation-dependent expressions should also be considered
24177 value-dependent. */
24178 return instantiation_dependent_scope_ref_p (expression
);
24180 case COMPONENT_REF
:
24181 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24182 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24184 case NONTYPE_ARGUMENT_PACK
:
24185 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24186 is value-dependent. */
24188 tree values
= ARGUMENT_PACK_ARGS (expression
);
24189 int i
, len
= TREE_VEC_LENGTH (values
);
24191 for (i
= 0; i
< len
; ++i
)
24192 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24200 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24202 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24208 if (TREE_CODE (type2
) != TREE_LIST
)
24209 return dependent_type_p (type2
);
24211 for (; type2
; type2
= TREE_CHAIN (type2
))
24212 if (dependent_type_p (TREE_VALUE (type2
)))
24219 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24220 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24223 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24224 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24228 tree op
= TREE_OPERAND (expression
, 0);
24229 return (value_dependent_expression_p (op
)
24230 || has_value_dependent_address (op
));
24233 case REQUIRES_EXPR
:
24234 /* Treat all requires-expressions as value-dependent so
24235 we don't try to fold them. */
24239 return dependent_type_p (TREE_OPERAND (expression
, 0));
24243 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24245 tree fn
= get_callee_fndecl (expression
);
24247 nargs
= call_expr_nargs (expression
);
24248 for (i
= 0; i
< nargs
; ++i
)
24250 tree op
= CALL_EXPR_ARG (expression
, i
);
24251 /* In a call to a constexpr member function, look through the
24252 implicit ADDR_EXPR on the object argument so that it doesn't
24253 cause the call to be considered value-dependent. We also
24254 look through it in potential_constant_expression. */
24255 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24256 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24257 && TREE_CODE (op
) == ADDR_EXPR
)
24258 op
= TREE_OPERAND (op
, 0);
24259 if (value_dependent_expression_p (op
))
24265 case TEMPLATE_ID_EXPR
:
24266 return variable_concept_p (TREE_OPERAND (expression
, 0));
24272 if (dependent_type_p (TREE_TYPE (expression
)))
24274 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24275 if (value_dependent_expression_p (val
))
24281 /* Treat a GNU statement expression as dependent to avoid crashing
24282 under instantiate_non_dependent_expr; it can't be constant. */
24286 /* A constant expression is value-dependent if any subexpression is
24287 value-dependent. */
24288 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24290 case tcc_reference
:
24292 case tcc_comparison
:
24294 case tcc_expression
:
24297 int i
, len
= cp_tree_operand_length (expression
);
24299 for (i
= 0; i
< len
; i
++)
24301 tree t
= TREE_OPERAND (expression
, i
);
24303 /* In some cases, some of the operands may be missing.
24304 (For example, in the case of PREDECREMENT_EXPR, the
24305 amount to increment by may be missing.) That doesn't
24306 make the expression dependent. */
24307 if (t
&& value_dependent_expression_p (t
))
24318 /* The expression is not value-dependent. */
24322 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24323 [temp.dep.expr]. Note that an expression with no type is
24324 considered dependent. Other parts of the compiler arrange for an
24325 expression with type-dependent subexpressions to have no type, so
24326 this function doesn't have to be fully recursive. */
24329 type_dependent_expression_p (tree expression
)
24331 if (!processing_template_decl
)
24334 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24337 STRIP_ANY_LOCATION_WRAPPER (expression
);
24339 /* An unresolved name is always dependent. */
24340 if (identifier_p (expression
)
24341 || TREE_CODE (expression
) == USING_DECL
24342 || TREE_CODE (expression
) == WILDCARD_DECL
)
24345 /* A fold expression is type-dependent. */
24346 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24347 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24348 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24349 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24352 /* Some expression forms are never type-dependent. */
24353 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24354 || TREE_CODE (expression
) == SIZEOF_EXPR
24355 || TREE_CODE (expression
) == ALIGNOF_EXPR
24356 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24357 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24358 || TREE_CODE (expression
) == TRAIT_EXPR
24359 || TREE_CODE (expression
) == TYPEID_EXPR
24360 || TREE_CODE (expression
) == DELETE_EXPR
24361 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24362 || TREE_CODE (expression
) == THROW_EXPR
24363 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24366 /* The types of these expressions depends only on the type to which
24367 the cast occurs. */
24368 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24369 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24370 || TREE_CODE (expression
) == CONST_CAST_EXPR
24371 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24372 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24373 || TREE_CODE (expression
) == CAST_EXPR
)
24374 return dependent_type_p (TREE_TYPE (expression
));
24376 /* The types of these expressions depends only on the type created
24377 by the expression. */
24378 if (TREE_CODE (expression
) == NEW_EXPR
24379 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24381 /* For NEW_EXPR tree nodes created inside a template, either
24382 the object type itself or a TREE_LIST may appear as the
24384 tree type
= TREE_OPERAND (expression
, 1);
24385 if (TREE_CODE (type
) == TREE_LIST
)
24386 /* This is an array type. We need to check array dimensions
24388 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24389 || value_dependent_expression_p
24390 (TREE_OPERAND (TREE_VALUE (type
), 1));
24392 return dependent_type_p (type
);
24395 if (TREE_CODE (expression
) == SCOPE_REF
)
24397 tree scope
= TREE_OPERAND (expression
, 0);
24398 tree name
= TREE_OPERAND (expression
, 1);
24400 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24401 contains an identifier associated by name lookup with one or more
24402 declarations declared with a dependent type, or...a
24403 nested-name-specifier or qualified-id that names a member of an
24404 unknown specialization. */
24405 return (type_dependent_expression_p (name
)
24406 || dependent_scope_p (scope
));
24409 if (TREE_CODE (expression
) == TEMPLATE_DECL
24410 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24411 return uses_outer_template_parms (expression
);
24413 if (TREE_CODE (expression
) == STMT_EXPR
)
24414 expression
= stmt_expr_value_expr (expression
);
24416 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24421 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24423 if (type_dependent_expression_p (elt
))
24429 /* A static data member of the current instantiation with incomplete
24430 array type is type-dependent, as the definition and specializations
24431 can have different bounds. */
24432 if (VAR_P (expression
)
24433 && DECL_CLASS_SCOPE_P (expression
)
24434 && dependent_type_p (DECL_CONTEXT (expression
))
24435 && VAR_HAD_UNKNOWN_BOUND (expression
))
24438 /* An array of unknown bound depending on a variadic parameter, eg:
24440 template<typename... Args>
24441 void foo (Args... args)
24443 int arr[] = { args... };
24446 template<int... vals>
24449 int arr[] = { vals... };
24452 If the array has no length and has an initializer, it must be that
24453 we couldn't determine its length in cp_complete_array_type because
24454 it is dependent. */
24455 if (VAR_P (expression
)
24456 && TREE_TYPE (expression
) != NULL_TREE
24457 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24458 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24459 && DECL_INITIAL (expression
))
24462 /* A function or variable template-id is type-dependent if it has any
24463 dependent template arguments. */
24464 if (VAR_OR_FUNCTION_DECL_P (expression
)
24465 && DECL_LANG_SPECIFIC (expression
)
24466 && DECL_TEMPLATE_INFO (expression
))
24468 /* Consider the innermost template arguments, since those are the ones
24469 that come from the template-id; the template arguments for the
24470 enclosing class do not make it type-dependent unless they are used in
24471 the type of the decl. */
24472 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24473 && (any_dependent_template_arguments_p
24474 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24478 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24479 type-dependent. Checking this is important for functions with auto return
24480 type, which looks like a dependent type. */
24481 if (TREE_CODE (expression
) == FUNCTION_DECL
24482 && !(DECL_CLASS_SCOPE_P (expression
)
24483 && dependent_type_p (DECL_CONTEXT (expression
)))
24484 && !(DECL_FRIEND_P (expression
)
24485 && (!DECL_FRIEND_CONTEXT (expression
)
24486 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24487 && !DECL_LOCAL_FUNCTION_P (expression
))
24489 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24490 || undeduced_auto_decl (expression
));
24494 /* Always dependent, on the number of arguments if nothing else. */
24495 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24498 if (TREE_TYPE (expression
) == unknown_type_node
)
24500 if (TREE_CODE (expression
) == ADDR_EXPR
)
24501 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24502 if (TREE_CODE (expression
) == COMPONENT_REF
24503 || TREE_CODE (expression
) == OFFSET_REF
)
24505 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24507 expression
= TREE_OPERAND (expression
, 1);
24508 if (identifier_p (expression
))
24511 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24512 if (TREE_CODE (expression
) == SCOPE_REF
)
24515 if (BASELINK_P (expression
))
24517 if (BASELINK_OPTYPE (expression
)
24518 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24520 expression
= BASELINK_FUNCTIONS (expression
);
24523 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24525 if (any_dependent_template_arguments_p
24526 (TREE_OPERAND (expression
, 1)))
24528 expression
= TREE_OPERAND (expression
, 0);
24529 if (identifier_p (expression
))
24533 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24534 || TREE_CODE (expression
) == FUNCTION_DECL
);
24536 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24537 if (type_dependent_expression_p (*iter
))
24543 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24545 /* Dependent type attributes might not have made it from the decl to
24547 if (DECL_P (expression
)
24548 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24551 return (dependent_type_p (TREE_TYPE (expression
)));
24554 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24555 type-dependent if the expression refers to a member of the current
24556 instantiation and the type of the referenced member is dependent, or the
24557 class member access expression refers to a member of an unknown
24560 This function returns true if the OBJECT in such a class member access
24561 expression is of an unknown specialization. */
24564 type_dependent_object_expression_p (tree object
)
24566 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24568 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24570 tree scope
= TREE_TYPE (object
);
24571 return (!scope
|| dependent_scope_p (scope
));
24574 /* walk_tree callback function for instantiation_dependent_expression_p,
24575 below. Returns non-zero if a dependent subexpression is found. */
24578 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24583 /* We don't have to worry about decltype currently because decltype
24584 of an instantiation-dependent expr is a dependent type. This
24585 might change depending on the resolution of DR 1172. */
24586 *walk_subtrees
= false;
24589 enum tree_code code
= TREE_CODE (*tp
);
24592 /* Don't treat an argument list as dependent just because it has no
24598 case TEMPLATE_PARM_INDEX
:
24601 /* Handle expressions with type operands. */
24605 case AT_ENCODE_EXPR
:
24607 tree op
= TREE_OPERAND (*tp
, 0);
24608 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24609 op
= TREE_TYPE (op
);
24612 if (dependent_type_p (op
))
24616 *walk_subtrees
= false;
24623 case COMPONENT_REF
:
24624 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24625 /* In a template, finish_class_member_access_expr creates a
24626 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24627 type-dependent, so that we can check access control at
24628 instantiation time (PR 42277). See also Core issue 1273. */
24633 if (instantiation_dependent_scope_ref_p (*tp
))
24638 /* Treat statement-expressions as dependent. */
24642 /* Treat requires-expressions as dependent. */
24643 case REQUIRES_EXPR
:
24647 /* Treat calls to function concepts as dependent. */
24648 if (function_concept_check_p (*tp
))
24652 case TEMPLATE_ID_EXPR
:
24653 /* And variable concepts. */
24654 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24662 if (type_dependent_expression_p (*tp
))
24668 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24669 sense defined by the ABI:
24671 "An expression is instantiation-dependent if it is type-dependent
24672 or value-dependent, or it has a subexpression that is type-dependent
24673 or value-dependent."
24675 Except don't actually check value-dependence for unevaluated expressions,
24676 because in sizeof(i) we don't care about the value of i. Checking
24677 type-dependence will in turn check value-dependence of array bounds/template
24678 arguments as needed. */
24681 instantiation_dependent_uneval_expression_p (tree expression
)
24685 if (!processing_template_decl
)
24688 if (expression
== error_mark_node
)
24691 result
= cp_walk_tree_without_duplicates (&expression
,
24692 instantiation_dependent_r
, NULL
);
24693 return result
!= NULL_TREE
;
24696 /* As above, but also check value-dependence of the expression as a whole. */
24699 instantiation_dependent_expression_p (tree expression
)
24701 return (instantiation_dependent_uneval_expression_p (expression
)
24702 || value_dependent_expression_p (expression
));
24705 /* Like type_dependent_expression_p, but it also works while not processing
24706 a template definition, i.e. during substitution or mangling. */
24709 type_dependent_expression_p_push (tree expr
)
24712 ++processing_template_decl
;
24713 b
= type_dependent_expression_p (expr
);
24714 --processing_template_decl
;
24718 /* Returns TRUE if ARGS contains a type-dependent expression. */
24721 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24726 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24728 if (type_dependent_expression_p (arg
))
24734 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24735 expressions) contains any type-dependent expressions. */
24738 any_type_dependent_elements_p (const_tree list
)
24740 for (; list
; list
= TREE_CHAIN (list
))
24741 if (type_dependent_expression_p (TREE_VALUE (list
)))
24747 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24748 expressions) contains any value-dependent expressions. */
24751 any_value_dependent_elements_p (const_tree list
)
24753 for (; list
; list
= TREE_CHAIN (list
))
24754 if (value_dependent_expression_p (TREE_VALUE (list
)))
24760 /* Returns TRUE if the ARG (a template argument) is dependent. */
24763 dependent_template_arg_p (tree arg
)
24765 if (!processing_template_decl
)
24768 /* Assume a template argument that was wrongly written by the user
24769 is dependent. This is consistent with what
24770 any_dependent_template_arguments_p [that calls this function]
24772 if (!arg
|| arg
== error_mark_node
)
24775 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24776 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24778 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24780 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24782 if (DECL_TEMPLATE_PARM_P (arg
))
24784 /* A member template of a dependent class is not necessarily
24785 type-dependent, but it is a dependent template argument because it
24786 will be a member of an unknown specialization to that template. */
24787 tree scope
= CP_DECL_CONTEXT (arg
);
24788 return TYPE_P (scope
) && dependent_type_p (scope
);
24790 else if (ARGUMENT_PACK_P (arg
))
24792 tree args
= ARGUMENT_PACK_ARGS (arg
);
24793 int i
, len
= TREE_VEC_LENGTH (args
);
24794 for (i
= 0; i
< len
; ++i
)
24796 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24802 else if (TYPE_P (arg
))
24803 return dependent_type_p (arg
);
24805 return (type_dependent_expression_p (arg
)
24806 || value_dependent_expression_p (arg
));
24809 /* Returns true if ARGS (a collection of template arguments) contains
24810 any types that require structural equality testing. */
24813 any_template_arguments_need_structural_equality_p (tree args
)
24820 if (args
== error_mark_node
)
24823 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24825 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24826 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24828 tree arg
= TREE_VEC_ELT (level
, j
);
24829 tree packed_args
= NULL_TREE
;
24832 if (ARGUMENT_PACK_P (arg
))
24834 /* Look inside the argument pack. */
24835 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24836 len
= TREE_VEC_LENGTH (packed_args
);
24839 for (k
= 0; k
< len
; ++k
)
24842 arg
= TREE_VEC_ELT (packed_args
, k
);
24844 if (error_operand_p (arg
))
24846 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24848 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24850 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24851 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24860 /* Returns true if ARGS (a collection of template arguments) contains
24861 any dependent arguments. */
24864 any_dependent_template_arguments_p (const_tree args
)
24871 if (args
== error_mark_node
)
24874 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24876 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24877 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24878 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24885 /* Returns TRUE if the template TMPL is type-dependent. */
24888 dependent_template_p (tree tmpl
)
24890 if (TREE_CODE (tmpl
) == OVERLOAD
)
24892 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24893 if (dependent_template_p (*iter
))
24898 /* Template template parameters are dependent. */
24899 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24900 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24902 /* So are names that have not been looked up. */
24903 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24908 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24911 dependent_template_id_p (tree tmpl
, tree args
)
24913 return (dependent_template_p (tmpl
)
24914 || any_dependent_template_arguments_p (args
));
24917 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24921 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24925 if (!processing_template_decl
)
24928 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24930 tree decl
= TREE_VEC_ELT (declv
, i
);
24931 tree init
= TREE_VEC_ELT (initv
, i
);
24932 tree cond
= TREE_VEC_ELT (condv
, i
);
24933 tree incr
= TREE_VEC_ELT (incrv
, i
);
24935 if (type_dependent_expression_p (decl
)
24936 || TREE_CODE (decl
) == SCOPE_REF
)
24939 if (init
&& type_dependent_expression_p (init
))
24942 if (type_dependent_expression_p (cond
))
24945 if (COMPARISON_CLASS_P (cond
)
24946 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24947 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24950 if (TREE_CODE (incr
) == MODOP_EXPR
)
24952 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24953 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24956 else if (type_dependent_expression_p (incr
))
24958 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24960 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24962 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24964 tree t
= TREE_OPERAND (incr
, 1);
24965 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24966 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24975 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24976 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24977 no such TYPE can be found. Note that this function peers inside
24978 uninstantiated templates and therefore should be used only in
24979 extremely limited situations. ONLY_CURRENT_P restricts this
24980 peering to the currently open classes hierarchy (which is required
24981 when comparing types). */
24984 resolve_typename_type (tree type
, bool only_current_p
)
24993 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24995 scope
= TYPE_CONTEXT (type
);
24996 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24997 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24998 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24999 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25000 identifier of the TYPENAME_TYPE anymore.
25001 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25002 TYPENAME_TYPE instead, we avoid messing up with a possible
25003 typedef variant case. */
25004 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25006 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25007 it first before we can figure out what NAME refers to. */
25008 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25010 if (TYPENAME_IS_RESOLVING_P (scope
))
25011 /* Given a class template A with a dependent base with nested type C,
25012 typedef typename A::C::C C will land us here, as trying to resolve
25013 the initial A::C leads to the local C typedef, which leads back to
25014 A::C::C. So we break the recursion now. */
25017 scope
= resolve_typename_type (scope
, only_current_p
);
25019 /* If we don't know what SCOPE refers to, then we cannot resolve the
25021 if (!CLASS_TYPE_P (scope
))
25023 /* If this is a typedef, we don't want to look inside (c++/11987). */
25024 if (typedef_variant_p (type
))
25026 /* If SCOPE isn't the template itself, it will not have a valid
25027 TYPE_FIELDS list. */
25028 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25029 /* scope is either the template itself or a compatible instantiation
25030 like X<T>, so look up the name in the original template. */
25031 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25032 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25033 gcc_checking_assert (uses_template_parms (scope
));
25034 /* If scope has no fields, it can't be a current instantiation. Check this
25035 before currently_open_class to avoid infinite recursion (71515). */
25036 if (!TYPE_FIELDS (scope
))
25038 /* If the SCOPE is not the current instantiation, there's no reason
25039 to look inside it. */
25040 if (only_current_p
&& !currently_open_class (scope
))
25042 /* Enter the SCOPE so that name lookup will be resolved as if we
25043 were in the class definition. In particular, SCOPE will no
25044 longer be considered a dependent type. */
25045 pushed_scope
= push_scope (scope
);
25046 /* Look up the declaration. */
25047 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25048 tf_warning_or_error
);
25050 result
= NULL_TREE
;
25052 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25053 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25054 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25057 else if (identifier_p (fullname
)
25058 && TREE_CODE (decl
) == TYPE_DECL
)
25060 result
= TREE_TYPE (decl
);
25061 if (result
== error_mark_node
)
25062 result
= NULL_TREE
;
25064 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25065 && DECL_CLASS_TEMPLATE_P (decl
))
25067 /* Obtain the template and the arguments. */
25068 tree tmpl
= TREE_OPERAND (fullname
, 0);
25069 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25071 /* We get here with a plain identifier because a previous tentative
25072 parse of the nested-name-specifier as part of a ptr-operator saw
25073 ::template X<A>. The use of ::template is necessary in a
25074 ptr-operator, but wrong in a declarator-id.
25076 [temp.names]: In a qualified-id of a declarator-id, the keyword
25077 template shall not appear at the top level. */
25078 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25079 "keyword %<template%> not allowed in declarator-id");
25082 tree args
= TREE_OPERAND (fullname
, 1);
25083 /* Instantiate the template. */
25084 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25085 /*entering_scope=*/true,
25086 tf_error
| tf_user
);
25087 if (result
== error_mark_node
)
25088 result
= NULL_TREE
;
25091 /* Leave the SCOPE. */
25093 pop_scope (pushed_scope
);
25095 /* If we failed to resolve it, return the original typename. */
25099 /* If lookup found a typename type, resolve that too. */
25100 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25102 /* Ill-formed programs can cause infinite recursion here, so we
25103 must catch that. */
25104 TYPENAME_IS_RESOLVING_P (result
) = 1;
25105 result
= resolve_typename_type (result
, only_current_p
);
25106 TYPENAME_IS_RESOLVING_P (result
) = 0;
25109 /* Qualify the resulting type. */
25110 quals
= cp_type_quals (type
);
25112 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25117 /* EXPR is an expression which is not type-dependent. Return a proxy
25118 for EXPR that can be used to compute the types of larger
25119 expressions containing EXPR. */
25122 build_non_dependent_expr (tree expr
)
25124 tree orig_expr
= expr
;
25127 /* When checking, try to get a constant value for all non-dependent
25128 expressions in order to expose bugs in *_dependent_expression_p
25129 and constexpr. This can affect code generation, see PR70704, so
25130 only do this for -fchecking=2. */
25131 if (flag_checking
> 1
25132 && cxx_dialect
>= cxx11
25133 /* Don't do this during nsdmi parsing as it can lead to
25134 unexpected recursive instantiations. */
25135 && !parsing_nsdmi ()
25136 /* Don't do this during concept expansion either and for
25137 the same reason. */
25138 && !expanding_concept ())
25139 fold_non_dependent_expr (expr
);
25141 STRIP_ANY_LOCATION_WRAPPER (expr
);
25143 /* Preserve OVERLOADs; the functions must be available to resolve
25146 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25147 inner_expr
= stmt_expr_value_expr (inner_expr
);
25148 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25149 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25150 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25151 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25152 if (is_overloaded_fn (inner_expr
)
25153 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25155 /* There is no need to return a proxy for a variable. */
25158 /* Preserve string constants; conversions from string constants to
25159 "char *" are allowed, even though normally a "const char *"
25160 cannot be used to initialize a "char *". */
25161 if (TREE_CODE (expr
) == STRING_CST
)
25163 /* Preserve void and arithmetic constants, as an optimization -- there is no
25164 reason to create a new node. */
25165 if (TREE_CODE (expr
) == VOID_CST
25166 || TREE_CODE (expr
) == INTEGER_CST
25167 || TREE_CODE (expr
) == REAL_CST
)
25169 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25170 There is at least one place where we want to know that a
25171 particular expression is a throw-expression: when checking a ?:
25172 expression, there are special rules if the second or third
25173 argument is a throw-expression. */
25174 if (TREE_CODE (expr
) == THROW_EXPR
)
25177 /* Don't wrap an initializer list, we need to be able to look inside. */
25178 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25181 /* Don't wrap a dummy object, we need to be able to test for it. */
25182 if (is_dummy_object (expr
))
25185 if (TREE_CODE (expr
) == COND_EXPR
)
25186 return build3 (COND_EXPR
,
25188 TREE_OPERAND (expr
, 0),
25189 (TREE_OPERAND (expr
, 1)
25190 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25191 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25192 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25193 if (TREE_CODE (expr
) == COMPOUND_EXPR
25194 && !COMPOUND_EXPR_OVERLOADED (expr
))
25195 return build2 (COMPOUND_EXPR
,
25197 TREE_OPERAND (expr
, 0),
25198 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25200 /* If the type is unknown, it can't really be non-dependent */
25201 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25203 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25204 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25207 /* ARGS is a vector of expressions as arguments to a function call.
25208 Replace the arguments with equivalent non-dependent expressions.
25209 This modifies ARGS in place. */
25212 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25217 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25219 tree newarg
= build_non_dependent_expr (arg
);
25221 (*args
)[ix
] = newarg
;
25225 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25226 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25227 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25230 make_auto_1 (tree name
, bool set_canonical
)
25232 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25233 TYPE_NAME (au
) = build_decl (input_location
,
25234 TYPE_DECL
, name
, au
);
25235 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25236 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25237 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25238 TYPE_NAME (au
), NULL_TREE
);
25240 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25241 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25242 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25248 make_decltype_auto (void)
25250 return make_auto_1 (decltype_auto_identifier
, true);
25256 return make_auto_1 (auto_identifier
, true);
25259 /* Return a C++17 deduction placeholder for class template TMPL. */
25262 make_template_placeholder (tree tmpl
)
25264 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25265 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25269 /* True iff T is a C++17 class template deduction placeholder. */
25272 template_placeholder_p (tree t
)
25274 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25277 /* Make a "constrained auto" type-specifier. This is an
25278 auto type with constraints that must be associated after
25279 deduction. The constraint is formed from the given
25280 CONC and its optional sequence of arguments, which are
25281 non-null if written as partial-concept-id. */
25284 make_constrained_auto (tree con
, tree args
)
25286 tree type
= make_auto_1 (auto_identifier
, false);
25288 /* Build the constraint. */
25289 tree tmpl
= DECL_TI_TEMPLATE (con
);
25290 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25291 expr
= build_concept_check (expr
, type
, args
);
25293 tree constr
= normalize_expression (expr
);
25294 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25296 /* Our canonical type depends on the constraint. */
25297 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25299 /* Attach the constraint to the type declaration. */
25300 tree decl
= TYPE_NAME (type
);
25304 /* Given type ARG, return std::initializer_list<ARG>. */
25309 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25311 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25313 gcc_rich_location
richloc (input_location
);
25314 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25315 error_at (&richloc
,
25316 "deducing from brace-enclosed initializer list"
25317 " requires %<#include <initializer_list>%>");
25319 return error_mark_node
;
25321 tree argvec
= make_tree_vec (1);
25322 TREE_VEC_ELT (argvec
, 0) = arg
;
25324 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25325 NULL_TREE
, 0, tf_warning_or_error
);
25328 /* Replace auto in TYPE with std::initializer_list<auto>. */
25331 listify_autos (tree type
, tree auto_node
)
25333 tree init_auto
= listify (auto_node
);
25334 tree argvec
= make_tree_vec (1);
25335 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25336 if (processing_template_decl
)
25337 argvec
= add_to_template_args (current_template_args (), argvec
);
25338 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25341 /* Hash traits for hashing possibly constrained 'auto'
25342 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25344 struct auto_hash
: default_hash_traits
<tree
>
25346 static inline hashval_t
hash (tree
);
25347 static inline bool equal (tree
, tree
);
25350 /* Hash the 'auto' T. */
25353 auto_hash::hash (tree t
)
25355 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25356 /* Matching constrained-type-specifiers denote the same template
25357 parameter, so hash the constraint. */
25358 return hash_placeholder_constraint (c
);
25360 /* But unconstrained autos are all separate, so just hash the pointer. */
25361 return iterative_hash_object (t
, 0);
25364 /* Compare two 'auto's. */
25367 auto_hash::equal (tree t1
, tree t2
)
25372 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25373 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25375 /* Two unconstrained autos are distinct. */
25379 return equivalent_placeholder_constraints (c1
, c2
);
25382 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25383 constrained) auto, add it to the vector. */
25386 extract_autos_r (tree t
, void *data
)
25388 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25391 /* All the autos were built with index 0; fix that up now. */
25392 tree
*p
= hash
.find_slot (t
, INSERT
);
25395 /* If this is a repeated constrained-type-specifier, use the index we
25397 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25400 /* Otherwise this is new, so use the current count. */
25402 idx
= hash
.elements () - 1;
25404 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25407 /* Always keep walking. */
25411 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25412 says they can appear anywhere in the type. */
25415 extract_autos (tree type
)
25417 hash_set
<tree
> visited
;
25418 hash_table
<auto_hash
> hash (2);
25420 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25422 tree tree_vec
= make_tree_vec (hash
.elements());
25423 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25424 iter
!= hash
.end(); ++iter
)
25427 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25428 TREE_VEC_ELT (tree_vec
, i
)
25429 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25435 /* The stem for deduction guide names. */
25436 const char *const dguide_base
= "__dguide_";
25438 /* Return the name for a deduction guide for class template TMPL. */
25441 dguide_name (tree tmpl
)
25443 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25444 tree tname
= TYPE_IDENTIFIER (type
);
25445 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25446 + IDENTIFIER_LENGTH (tname
));
25447 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25448 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25449 IDENTIFIER_LENGTH (tname
) + 1);
25450 tree dname
= get_identifier (buf
);
25451 TREE_TYPE (dname
) = type
;
25455 /* True if NAME is the name of a deduction guide. */
25458 dguide_name_p (tree name
)
25460 return (TREE_TYPE (name
)
25461 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25462 strlen (dguide_base
)));
25465 /* True if FN is a deduction guide. */
25468 deduction_guide_p (const_tree fn
)
25471 if (tree name
= DECL_NAME (fn
))
25472 return dguide_name_p (name
);
25476 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25479 copy_guide_p (const_tree fn
)
25481 gcc_assert (deduction_guide_p (fn
));
25482 if (!DECL_ARTIFICIAL (fn
))
25484 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25485 return (TREE_CHAIN (parms
) == void_list_node
25486 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25489 /* True if FN is a guide generated from a constructor template. */
25492 template_guide_p (const_tree fn
)
25494 gcc_assert (deduction_guide_p (fn
));
25495 if (!DECL_ARTIFICIAL (fn
))
25497 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25498 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25499 return PRIMARY_TEMPLATE_P (org
);
25503 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25504 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25505 template parameter types. Note that the handling of template template
25506 parameters relies on current_template_parms being set appropriately for the
25510 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25511 tree tsubst_args
, tsubst_flags_t complain
)
25513 tree oldidx
= get_template_parm_index (olddecl
);
25516 if (TREE_CODE (olddecl
) == TYPE_DECL
25517 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25519 tree oldtype
= TREE_TYPE (olddecl
);
25520 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25521 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25522 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25523 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25524 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25527 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25528 complain
, NULL_TREE
);
25531 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25532 DECL_NAME (olddecl
), newtype
);
25533 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25536 if (TREE_CODE (olddecl
) == TYPE_DECL
25537 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25539 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25540 = build_template_parm_index (index
, level
, level
,
25542 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25543 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25544 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25545 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25547 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25549 DECL_TEMPLATE_RESULT (newdecl
)
25550 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25551 DECL_NAME (olddecl
), newtype
);
25552 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25553 // First create a copy (ttargs) of tsubst_args with an
25554 // additional level for the template template parameter's own
25555 // template parameters (ttparms).
25556 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25557 (DECL_TEMPLATE_PARMS (olddecl
)));
25558 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25559 tree ttargs
= make_tree_vec (depth
+ 1);
25560 for (int i
= 0; i
< depth
; ++i
)
25561 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25562 TREE_VEC_ELT (ttargs
, depth
)
25563 = template_parms_level_to_args (ttparms
);
25564 // Substitute ttargs into ttparms to fix references to
25565 // other template parameters.
25566 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25568 // Now substitute again with args based on tparms, to reduce
25569 // the level of the ttparms.
25570 ttargs
= current_template_args ();
25571 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25573 // Finally, tack the adjusted parms onto tparms.
25574 ttparms
= tree_cons (size_int (depth
), ttparms
,
25575 current_template_parms
);
25576 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25581 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25583 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25584 TREE_CODE (oldconst
),
25585 DECL_NAME (oldconst
), newtype
);
25586 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25587 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25588 SET_DECL_TEMPLATE_PARM_P (newconst
);
25589 newidx
= build_template_parm_index (index
, level
, level
,
25590 newconst
, newtype
);
25591 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25592 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25593 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25599 /* Returns a C++17 class deduction guide template based on the constructor
25600 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25601 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25604 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25606 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25607 bool memtmpl
= false;
25610 tree fn_tmpl
= NULL_TREE
;
25615 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25618 type
= TREE_TYPE (type
);
25619 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25622 fparms
= void_list_node
;
25624 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25625 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25626 targs
= CLASSTYPE_TI_ARGS (type
);
25629 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25630 explicit_p
= false;
25634 ++processing_template_decl
;
25637 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25638 : DECL_TI_TEMPLATE (ctor
));
25640 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25641 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25643 type
= DECL_CONTEXT (ctor
);
25645 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25646 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25647 fully specialized args for the enclosing class. Strip those off, as
25648 the deduction guide won't have those template parameters. */
25649 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25650 TMPL_PARMS_DEPTH (tparms
));
25651 /* Discard the 'this' parameter. */
25652 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25653 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25654 ci
= get_constraints (ctor
);
25655 loc
= DECL_SOURCE_LOCATION (ctor
);
25656 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25658 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25662 /* For a member template constructor, we need to flatten the two
25663 template parameter lists into one, and then adjust the function
25664 signature accordingly. This gets...complicated. */
25665 tree save_parms
= current_template_parms
;
25667 /* For a member template we should have two levels of parms/args, one
25668 for the class and one for the constructor. We stripped
25669 specialized args for further enclosing classes above. */
25670 const int depth
= 2;
25671 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25673 /* Template args for translating references to the two-level template
25674 parameters into references to the one-level template parameters we
25676 tree tsubst_args
= copy_node (targs
);
25677 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25678 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25680 /* Template parms for the constructor template. */
25681 tree ftparms
= TREE_VALUE (tparms
);
25682 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25683 /* Template parms for the class template. */
25684 tparms
= TREE_CHAIN (tparms
);
25685 tree ctparms
= TREE_VALUE (tparms
);
25686 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25687 /* Template parms for the deduction guide start as a copy of the
25688 template parms for the class. We set current_template_parms for
25689 lookup_template_class_1. */
25690 current_template_parms
= tparms
= copy_node (tparms
);
25691 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25692 for (unsigned i
= 0; i
< clen
; ++i
)
25693 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25695 /* Now we need to rewrite the constructor parms to append them to the
25697 for (unsigned i
= 0; i
< flen
; ++i
)
25699 unsigned index
= i
+ clen
;
25700 unsigned level
= 1;
25701 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25702 tree olddecl
= TREE_VALUE (oldelt
);
25703 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25704 tsubst_args
, complain
);
25705 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25706 tsubst_args
, complain
, ctor
);
25707 tree list
= build_tree_list (newdef
, newdecl
);
25708 TEMPLATE_PARM_CONSTRAINTS (list
)
25709 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25710 tsubst_args
, complain
, ctor
);
25711 TREE_VEC_ELT (new_vec
, index
) = list
;
25712 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25715 /* Now we have a final set of template parms to substitute into the
25716 function signature. */
25717 targs
= template_parms_to_args (tparms
);
25718 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25720 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25722 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25724 current_template_parms
= save_parms
;
25726 --processing_template_decl
;
25731 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25732 tparms
= copy_node (tparms
);
25733 INNERMOST_TEMPLATE_PARMS (tparms
)
25734 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25737 tree fntype
= build_function_type (type
, fparms
);
25738 tree ded_fn
= build_lang_decl_loc (loc
,
25740 dguide_name (type
), fntype
);
25741 DECL_ARGUMENTS (ded_fn
) = fargs
;
25742 DECL_ARTIFICIAL (ded_fn
) = true;
25743 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25744 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25745 DECL_ARTIFICIAL (ded_tmpl
) = true;
25746 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25747 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25748 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25749 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25751 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25753 set_constraints (ded_tmpl
, ci
);
25758 /* Deduce template arguments for the class template placeholder PTYPE for
25759 template TMPL based on the initializer INIT, and return the resulting
25763 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25764 tsubst_flags_t complain
)
25766 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25768 /* We should have handled this in the caller. */
25769 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25771 if (complain
& tf_error
)
25772 error ("non-class template %qT used without template arguments", tmpl
);
25773 return error_mark_node
;
25776 tree type
= TREE_TYPE (tmpl
);
25778 bool try_list_ctor
= false;
25780 vec
<tree
,va_gc
> *args
;
25781 if (init
== NULL_TREE
25782 || TREE_CODE (init
) == TREE_LIST
)
25783 args
= make_tree_vector_from_list (init
);
25784 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25786 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25787 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25789 /* As an exception, the first phase in 16.3.1.7 (considering the
25790 initializer list as a single argument) is omitted if the
25791 initializer list consists of a single expression of type cv U,
25792 where U is a specialization of C or a class derived from a
25793 specialization of C. */
25794 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25795 tree etype
= TREE_TYPE (elt
);
25797 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25798 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25799 int err
= unify (tparms
, targs
, type
, etype
,
25800 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25802 try_list_ctor
= false;
25805 if (try_list_ctor
|| is_std_init_list (type
))
25806 args
= make_tree_vector_single (init
);
25808 args
= make_tree_vector_from_ctor (init
);
25811 args
= make_tree_vector_single (init
);
25813 tree dname
= dguide_name (tmpl
);
25814 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25815 /*type*/false, /*complain*/false,
25817 bool elided
= false;
25818 if (cands
== error_mark_node
)
25821 /* Prune explicit deduction guides in copy-initialization context. */
25822 if (flags
& LOOKUP_ONLYCONVERTING
)
25824 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25825 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25830 /* Found a nonconverting guide, prune the candidates. */
25831 tree pruned
= NULL_TREE
;
25832 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25833 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25834 pruned
= lookup_add (*iter
, pruned
);
25840 tree outer_args
= NULL_TREE
;
25841 if (DECL_CLASS_SCOPE_P (tmpl
)
25842 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25844 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25845 type
= TREE_TYPE (most_general_template (tmpl
));
25848 bool saw_ctor
= false;
25849 // FIXME cache artificial deduction guides
25850 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25852 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25853 if ((flags
& LOOKUP_ONLYCONVERTING
)
25854 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25857 cands
= lookup_add (guide
, cands
);
25862 tree call
= error_mark_node
;
25864 /* If this is list-initialization and the class has a list constructor, first
25865 try deducing from the list as a single argument, as [over.match.list]. */
25866 tree list_cands
= NULL_TREE
;
25867 if (try_list_ctor
&& cands
)
25868 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25871 if (is_list_ctor (dg
))
25872 list_cands
= lookup_add (dg
, list_cands
);
25876 ++cp_unevaluated_operand
;
25877 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25878 --cp_unevaluated_operand
;
25880 if (call
== error_mark_node
)
25882 /* That didn't work, now try treating the list as a sequence of
25884 release_tree_vector (args
);
25885 args
= make_tree_vector_from_ctor (init
);
25889 /* Maybe generate an implicit deduction guide. */
25890 if (call
== error_mark_node
&& args
->length () < 2)
25892 tree gtype
= NULL_TREE
;
25894 if (args
->length () == 1)
25895 /* Generate a copy guide. */
25896 gtype
= build_reference_type (type
);
25897 else if (!saw_ctor
)
25898 /* Generate a default guide. */
25903 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25904 cands
= lookup_add (guide
, cands
);
25908 if (elided
&& !cands
)
25910 error ("cannot deduce template arguments for copy-initialization"
25911 " of %qT, as it has no non-explicit deduction guides or "
25912 "user-declared constructors", type
);
25913 return error_mark_node
;
25915 else if (!cands
&& call
== error_mark_node
)
25917 error ("cannot deduce template arguments of %qT, as it has no viable "
25918 "deduction guides", type
);
25919 return error_mark_node
;
25922 if (call
== error_mark_node
)
25924 ++cp_unevaluated_operand
;
25925 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25926 --cp_unevaluated_operand
;
25929 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25931 error ("class template argument deduction failed:");
25933 ++cp_unevaluated_operand
;
25934 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25935 --cp_unevaluated_operand
;
25938 inform (input_location
, "explicit deduction guides not considered "
25939 "for copy-initialization");
25942 release_tree_vector (args
);
25944 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25947 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25948 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25951 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25953 return do_auto_deduction (type
, init
, auto_node
,
25954 tf_warning_or_error
,
25958 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25959 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25960 The CONTEXT determines the context in which auto deduction is performed
25961 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25962 OUTER_TARGS are used during template argument deduction
25963 (context == adc_unify) to properly substitute the result, and is ignored
25966 For partial-concept-ids, extra args may be appended to the list of deduced
25967 template arguments prior to determining constraint satisfaction. */
25970 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25971 tsubst_flags_t complain
, auto_deduction_context context
,
25972 tree outer_targs
, int flags
)
25976 if (init
== error_mark_node
)
25977 return error_mark_node
;
25979 if (init
&& type_dependent_expression_p (init
)
25980 && context
!= adc_unify
)
25981 /* Defining a subset of type-dependent expressions that we can deduce
25982 from ahead of time isn't worth the trouble. */
25985 /* Similarly, we can't deduce from another undeduced decl. */
25986 if (init
&& undeduced_auto_decl (init
))
25989 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25990 /* C++17 class template argument deduction. */
25991 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25993 if (TREE_TYPE (init
) == NULL_TREE
)
25994 /* Nothing we can do with this, even in deduction context. */
25997 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25998 with either a new invented type template parameter U or, if the
25999 initializer is a braced-init-list (8.5.4), with
26000 std::initializer_list<U>. */
26001 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26003 if (!DIRECT_LIST_INIT_P (init
))
26004 type
= listify_autos (type
, auto_node
);
26005 else if (CONSTRUCTOR_NELTS (init
) == 1)
26006 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26009 if (complain
& tf_warning_or_error
)
26011 if (permerror (input_location
, "direct-list-initialization of "
26012 "%<auto%> requires exactly one element"))
26013 inform (input_location
,
26014 "for deduction to %<std::initializer_list%>, use copy-"
26015 "list-initialization (i.e. add %<=%> before the %<{%>)");
26017 type
= listify_autos (type
, auto_node
);
26021 if (type
== error_mark_node
)
26022 return error_mark_node
;
26024 init
= resolve_nondeduced_context (init
, complain
);
26026 if (context
== adc_decomp_type
26027 && auto_node
== type
26028 && init
!= error_mark_node
26029 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26030 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26031 and initializer has array type, deduce cv-qualified array type. */
26032 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26034 else if (AUTO_IS_DECLTYPE (auto_node
))
26036 bool id
= (DECL_P (init
)
26037 || ((TREE_CODE (init
) == COMPONENT_REF
26038 || TREE_CODE (init
) == SCOPE_REF
)
26039 && !REF_PARENTHESIZED_P (init
)));
26040 targs
= make_tree_vec (1);
26041 TREE_VEC_ELT (targs
, 0)
26042 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26043 if (type
!= auto_node
)
26045 if (complain
& tf_error
)
26046 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26047 return error_mark_node
;
26052 tree parms
= build_tree_list (NULL_TREE
, type
);
26056 tparms
= extract_autos (type
);
26059 tparms
= make_tree_vec (1);
26060 TREE_VEC_ELT (tparms
, 0)
26061 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26064 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26065 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26066 DEDUCE_CALL
, LOOKUP_NORMAL
,
26067 NULL
, /*explain_p=*/false);
26070 if (processing_template_decl
)
26071 /* Try again at instantiation time. */
26073 if (type
&& type
!= error_mark_node
26074 && (complain
& tf_error
))
26075 /* If type is error_mark_node a diagnostic must have been
26076 emitted by now. Also, having a mention to '<type error>'
26077 in the diagnostic is not really useful to the user. */
26079 if (cfun
&& auto_node
== current_function_auto_return_pattern
26080 && LAMBDA_FUNCTION_P (current_function_decl
))
26081 error ("unable to deduce lambda return type from %qE", init
);
26083 error ("unable to deduce %qT from %qE", type
, init
);
26084 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26085 DEDUCE_CALL
, LOOKUP_NORMAL
,
26086 NULL
, /*explain_p=*/true);
26088 return error_mark_node
;
26092 /* Check any placeholder constraints against the deduced type. */
26093 if (flag_concepts
&& !processing_template_decl
)
26094 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26096 /* Use the deduced type to check the associated constraints. If we
26097 have a partial-concept-id, rebuild the argument list so that
26098 we check using the extra arguments. */
26099 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26100 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26101 if (TREE_VEC_LENGTH (cargs
) > 1)
26103 cargs
= copy_node (cargs
);
26104 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26108 if (!constraints_satisfied_p (constr
, cargs
))
26110 if (complain
& tf_warning_or_error
)
26114 case adc_unspecified
:
26116 error("placeholder constraints not satisfied");
26118 case adc_variable_type
:
26119 case adc_decomp_type
:
26120 error ("deduced initializer does not satisfy "
26121 "placeholder constraints");
26123 case adc_return_type
:
26124 error ("deduced return type does not satisfy "
26125 "placeholder constraints");
26127 case adc_requirement
:
26128 error ("deduced expression type does not satisfy "
26129 "placeholder constraints");
26132 diagnose_constraints (input_location
, constr
, targs
);
26134 return error_mark_node
;
26138 if (processing_template_decl
&& context
!= adc_unify
)
26139 outer_targs
= current_template_args ();
26140 targs
= add_to_template_args (outer_targs
, targs
);
26141 return tsubst (type
, targs
, complain
, NULL_TREE
);
26144 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26148 splice_late_return_type (tree type
, tree late_return_type
)
26150 if (is_auto (type
))
26152 if (late_return_type
)
26153 return late_return_type
;
26155 tree idx
= get_template_parm_index (type
);
26156 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26157 /* In an abbreviated function template we didn't know we were dealing
26158 with a function template when we saw the auto return type, so update
26159 it to have the correct level. */
26160 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26165 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26166 'decltype(auto)' or a deduced class template. */
26169 is_auto (const_tree type
)
26171 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26172 && (TYPE_IDENTIFIER (type
) == auto_identifier
26173 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26174 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26180 /* for_each_template_parm callback for type_uses_auto. */
26183 is_auto_r (tree tp
, void */
*data*/
)
26185 return is_auto (tp
);
26188 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26189 a use of `auto'. Returns NULL_TREE otherwise. */
26192 type_uses_auto (tree type
)
26194 if (type
== NULL_TREE
)
26196 else if (flag_concepts
)
26198 /* The Concepts TS allows multiple autos in one type-specifier; just
26199 return the first one we find, do_auto_deduction will collect all of
26201 if (uses_template_parms (type
))
26202 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26203 /*visited*/NULL
, /*nondeduced*/true);
26208 return find_type_usage (type
, is_auto
);
26211 /* For a given template T, return the vector of typedefs referenced
26212 in T for which access check is needed at T instantiation time.
26213 T is either a FUNCTION_DECL or a RECORD_TYPE.
26214 Those typedefs were added to T by the function
26215 append_type_to_template_for_access_check. */
26217 vec
<qualified_typedef_usage_t
, va_gc
> *
26218 get_types_needing_access_check (tree t
)
26221 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26223 if (!t
|| t
== error_mark_node
)
26226 if (!(ti
= get_template_info (t
)))
26229 if (CLASS_TYPE_P (t
)
26230 || TREE_CODE (t
) == FUNCTION_DECL
)
26232 if (!TI_TEMPLATE (ti
))
26235 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26241 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26242 tied to T. That list of typedefs will be access checked at
26243 T instantiation time.
26244 T is either a FUNCTION_DECL or a RECORD_TYPE.
26245 TYPE_DECL is a TYPE_DECL node representing a typedef.
26246 SCOPE is the scope through which TYPE_DECL is accessed.
26247 LOCATION is the location of the usage point of TYPE_DECL.
26249 This function is a subroutine of
26250 append_type_to_template_for_access_check. */
26253 append_type_to_template_for_access_check_1 (tree t
,
26256 location_t location
)
26258 qualified_typedef_usage_t typedef_usage
;
26261 if (!t
|| t
== error_mark_node
)
26264 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26265 || CLASS_TYPE_P (t
))
26267 && TREE_CODE (type_decl
) == TYPE_DECL
26270 if (!(ti
= get_template_info (t
)))
26273 gcc_assert (TI_TEMPLATE (ti
));
26275 typedef_usage
.typedef_decl
= type_decl
;
26276 typedef_usage
.context
= scope
;
26277 typedef_usage
.locus
= location
;
26279 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26282 /* Append TYPE_DECL to the template TEMPL.
26283 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26284 At TEMPL instanciation time, TYPE_DECL will be checked to see
26285 if it can be accessed through SCOPE.
26286 LOCATION is the location of the usage point of TYPE_DECL.
26288 e.g. consider the following code snippet:
26295 template<class U> struct S
26297 C::myint mi; // <-- usage point of the typedef C::myint
26302 At S<char> instantiation time, we need to check the access of C::myint
26303 In other words, we need to check the access of the myint typedef through
26304 the C scope. For that purpose, this function will add the myint typedef
26305 and the scope C through which its being accessed to a list of typedefs
26306 tied to the template S. That list will be walked at template instantiation
26307 time and access check performed on each typedefs it contains.
26308 Note that this particular code snippet should yield an error because
26309 myint is private to C. */
26312 append_type_to_template_for_access_check (tree templ
,
26315 location_t location
)
26317 qualified_typedef_usage_t
*iter
;
26320 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26322 /* Make sure we don't append the type to the template twice. */
26323 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26324 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26327 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26331 /* Convert the generic type parameters in PARM that match the types given in the
26332 range [START_IDX, END_IDX) from the current_template_parms into generic type
26336 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26338 tree current
= current_template_parms
;
26339 int depth
= TMPL_PARMS_DEPTH (current
);
26340 current
= INNERMOST_TEMPLATE_PARMS (current
);
26341 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26343 for (int i
= 0; i
< start_idx
; ++i
)
26344 TREE_VEC_ELT (replacement
, i
)
26345 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26347 for (int i
= start_idx
; i
< end_idx
; ++i
)
26349 /* Create a distinct parameter pack type from the current parm and add it
26350 to the replacement args to tsubst below into the generic function
26353 tree o
= TREE_TYPE (TREE_VALUE
26354 (TREE_VEC_ELT (current
, i
)));
26355 tree t
= copy_type (o
);
26356 TEMPLATE_TYPE_PARM_INDEX (t
)
26357 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26359 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26360 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26361 TYPE_MAIN_VARIANT (t
) = t
;
26362 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26363 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26364 TREE_VEC_ELT (replacement
, i
) = t
;
26365 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26368 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26369 TREE_VEC_ELT (replacement
, i
)
26370 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26372 /* If there are more levels then build up the replacement with the outer
26375 replacement
= add_to_template_args (template_parms_to_args
26376 (TREE_CHAIN (current_template_parms
)),
26379 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26382 /* Entries in the decl_constraint hash table. */
26383 struct GTY((for_user
)) constr_entry
26389 /* Hashing function and equality for constraint entries. */
26390 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26392 static hashval_t
hash (constr_entry
*e
)
26394 return (hashval_t
)DECL_UID (e
->decl
);
26397 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26399 return e1
->decl
== e2
->decl
;
26403 /* A mapping from declarations to constraint information. Note that
26404 both templates and their underlying declarations are mapped to the
26405 same constraint information.
26407 FIXME: This is defined in pt.c because garbage collection
26408 code is not being generated for constraint.cc. */
26410 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26412 /* Returns the template constraints of declaration T. If T is not
26413 constrained, return NULL_TREE. Note that T must be non-null. */
26416 get_constraints (tree t
)
26418 if (!flag_concepts
)
26421 gcc_assert (DECL_P (t
));
26422 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26423 t
= DECL_TEMPLATE_RESULT (t
);
26424 constr_entry elt
= { t
, NULL_TREE
};
26425 constr_entry
* found
= decl_constraints
->find (&elt
);
26432 /* Associate the given constraint information CI with the declaration
26433 T. If T is a template, then the constraints are associated with
26434 its underlying declaration. Don't build associations if CI is
26438 set_constraints (tree t
, tree ci
)
26442 gcc_assert (t
&& flag_concepts
);
26443 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26444 t
= DECL_TEMPLATE_RESULT (t
);
26445 gcc_assert (!get_constraints (t
));
26446 constr_entry elt
= {t
, ci
};
26447 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26448 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26453 /* Remove the associated constraints of the declaration T. */
26456 remove_constraints (tree t
)
26458 gcc_assert (DECL_P (t
));
26459 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26460 t
= DECL_TEMPLATE_RESULT (t
);
26462 constr_entry elt
= {t
, NULL_TREE
};
26463 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26465 decl_constraints
->clear_slot (slot
);
26468 /* Memoized satisfaction results for declarations. This
26469 maps the pair (constraint_info, arguments) to the result computed
26470 by constraints_satisfied_p. */
26472 struct GTY((for_user
)) constraint_sat_entry
26479 /* Hashing function and equality for constraint entries. */
26481 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26483 static hashval_t
hash (constraint_sat_entry
*e
)
26485 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26486 return iterative_hash_template_arg (e
->args
, val
);
26489 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26491 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26495 /* Memoized satisfaction results for concept checks. */
26497 struct GTY((for_user
)) concept_spec_entry
26504 /* Hashing function and equality for constraint entries. */
26506 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26508 static hashval_t
hash (concept_spec_entry
*e
)
26510 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26513 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26515 ++comparing_specializations
;
26516 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26517 --comparing_specializations
;
26522 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26523 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26525 /* Search for a memoized satisfaction result. Returns one of the
26526 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26529 lookup_constraint_satisfaction (tree ci
, tree args
)
26531 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26532 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26534 return found
->result
;
26539 /* Memoize the result of a satisfication test. Returns the saved result. */
26542 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26544 constraint_sat_entry elt
= {ci
, args
, result
};
26545 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26546 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26552 /* Search for a memoized satisfaction result for a concept. */
26555 lookup_concept_satisfaction (tree tmpl
, tree args
)
26557 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26558 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26560 return found
->result
;
26565 /* Memoize the result of a concept check. Returns the saved result. */
26568 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26570 concept_spec_entry elt
= {tmpl
, args
, result
};
26571 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26572 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26578 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26580 /* Returns a prior concept specialization. This returns the substituted
26581 and normalized constraints defined by the concept. */
26584 get_concept_expansion (tree tmpl
, tree args
)
26586 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26587 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26589 return found
->result
;
26594 /* Save a concept expansion for later. */
26597 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26599 concept_spec_entry elt
= {tmpl
, args
, def
};
26600 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26601 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26608 hash_subsumption_args (tree t1
, tree t2
)
26610 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26611 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26613 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26614 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26615 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26616 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26620 /* Compare the constraints of two subsumption entries. The LEFT1 and
26621 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26622 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26625 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26627 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26628 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26629 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26630 CHECK_CONSTR_ARGS (right1
)))
26631 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26632 CHECK_CONSTR_ARGS (right2
));
26636 /* Key/value pair for learning and memoizing subsumption results. This
26637 associates a pair of check constraints (including arguments) with
26638 a boolean value indicating the result. */
26640 struct GTY((for_user
)) subsumption_entry
26647 /* Hashing function and equality for constraint entries. */
26649 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26651 static hashval_t
hash (subsumption_entry
*e
)
26653 return hash_subsumption_args (e
->t1
, e
->t2
);
26656 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26658 ++comparing_specializations
;
26659 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26660 --comparing_specializations
;
26665 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26667 /* Search for a previously cached subsumption result. */
26670 lookup_subsumption_result (tree t1
, tree t2
)
26672 subsumption_entry elt
= { t1
, t2
, false };
26673 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26675 return &found
->result
;
26680 /* Save a subsumption result. */
26683 save_subsumption_result (tree t1
, tree t2
, bool result
)
26685 subsumption_entry elt
= {t1
, t2
, result
};
26686 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26687 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26693 /* Set up the hash table for constraint association. */
26696 init_constraint_processing (void)
26698 if (!flag_concepts
)
26701 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26702 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26703 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26704 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26705 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26708 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26712 declare_integer_pack (void)
26714 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26715 build_function_type_list (integer_type_node
,
26718 NULL_TREE
, ECF_CONST
);
26719 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26720 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26723 /* Set up the hash tables for template instantiations. */
26726 init_template_processing (void)
26728 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26729 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26731 if (cxx_dialect
>= cxx11
)
26732 declare_integer_pack ();
26735 /* Print stats about the template hash tables for -fstats. */
26738 print_template_statistics (void)
26740 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26741 "%f collisions\n", (long) decl_specializations
->size (),
26742 (long) decl_specializations
->elements (),
26743 decl_specializations
->collisions ());
26744 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26745 "%f collisions\n", (long) type_specializations
->size (),
26746 (long) type_specializations
->elements (),
26747 type_specializations
->collisions ());
26752 namespace selftest
{
26754 /* Verify that build_non_dependent_expr () works, for various expressions,
26755 and that location wrappers don't affect the results. */
26758 test_build_non_dependent_expr ()
26760 location_t loc
= BUILTINS_LOCATION
;
26762 /* Verify constants, without and with location wrappers. */
26763 tree int_cst
= build_int_cst (integer_type_node
, 42);
26764 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26766 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26767 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26768 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26770 tree string_lit
= build_string (4, "foo");
26771 TREE_TYPE (string_lit
) = char_array_type_node
;
26772 string_lit
= fix_string_type (string_lit
);
26773 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26775 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26776 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26777 ASSERT_EQ (wrapped_string_lit
,
26778 build_non_dependent_expr (wrapped_string_lit
));
26781 /* Verify that type_dependent_expression_p () works correctly, even
26782 in the presence of location wrapper nodes. */
26785 test_type_dependent_expression_p ()
26787 location_t loc
= BUILTINS_LOCATION
;
26789 tree name
= get_identifier ("foo");
26791 /* If no templates are involved, nothing is type-dependent. */
26792 gcc_assert (!processing_template_decl
);
26793 ASSERT_FALSE (type_dependent_expression_p (name
));
26795 ++processing_template_decl
;
26797 /* Within a template, an unresolved name is always type-dependent. */
26798 ASSERT_TRUE (type_dependent_expression_p (name
));
26800 /* Ensure it copes with NULL_TREE and errors. */
26801 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26802 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26804 /* A USING_DECL in a template should be type-dependent, even if wrapped
26805 with a location wrapper (PR c++/83799). */
26806 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26807 TREE_TYPE (using_decl
) = integer_type_node
;
26808 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26809 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26810 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26811 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26813 --processing_template_decl
;
26816 /* Run all of the selftests within this file. */
26821 test_build_non_dependent_expr ();
26822 test_type_dependent_expression_p ();
26825 } // namespace selftest
26827 #endif /* #if CHECKING_P */
26829 #include "gt-cp-pt.h"